Ric*_*rin 5 linux linux-kernel
在 Linux 内核模块中,我需要以某种方式禁用 rp_filter。这通常可以通过几个简单的 sysctl 调用从用户空间实现:
sysctl net.ipv4.conf.all.rp_filter=0
sysctl net.ipv4.conf.[ifname].rp_filter=0
Run Code Online (Sandbox Code Playgroud)
如何从内核空间获得相同的结果?我的第一个想法是我可能需要写入相关的 proc 文件。如果是这样,正确的方法是什么?
sysctl
我正在查看内核中的实现,我发现执行 asysctl
与向文件系统中的文件写入值相同/proc
。
在你的情况下,你只想做相当于:
echo 0 >/proc/sys/net/ipv4/conf/all/rp_filter
Run Code Online (Sandbox Code Playgroud)
这是一个粗略的代码示例,基于我在刚刚阅读的内核代码中看到的内容:
struct vfsmount *mnt;
struct file *file;
ssize_t result;
char *pathname = "sys/net/ipv4/conf/all/rp_filter";
int flags = O_WRONLY;
mnt = task_active_pid_ns(current)->proc_mnt;
file = file_open_root(mnt->mnt_root, mnt, pathname, flags);
result = PTR_ERR(file);
if (IS_ERR(file)) {
// oops, something bad happened
} else {
char *buffer = "\0";
result = kernel_write(file, buffer, 1, 0); // last 2 args are 'count' and 'pos'
if (result < 0) {
// oops, something else bad happened
}
}
fput(file);
Run Code Online (Sandbox Code Playgroud)
这只是一个粗略的示例,因此您必须自己进行研究和测试才能使其正常工作。祝你好运!