为什么 nf_hookfn、nf_hook_ops 等类型定义在 netfilter.h 中缺失?

Why are typedefs of nf_hookfn, nf_hook_ops, etc. missing from netfilter.h?

我正在尝试编写一个 netfilter 模块。我的第一次尝试。

我的 Makefile 是:

obj-m += someModule.o
all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我收到编译时错误,清楚地表明 structs nf_hookfnnf_hook_ops 等的 typedefs 未被选择或错误选择。

我知道钩子函数的参数列表在后来的内核中发生了变化。无论哪种方式,我都没有在 /usr/include/linux/netfilter.h.

中看到任何我需要的 typedefs

我尝试了什么?

我尝试更新内核头文件:

 sudo apt-get install linux-headers-$(uname -r)
 [sudo] password for xxx: 
 Reading package lists... Done
 Building dependency tree       
 Reading state information... Done
 linux-headers-4.15.0-33-generic is already the newest version (4.15.0-33.36).

0 个已升级,0 个新安装,0 个要删除,397 个未升级。

我在

中看到了所有必需的定义
/usr/src/linux-headers-4.15.0-33/include/linux/netfilter.h

当然,我可以包含此文件,或者对它添加 link。

这似乎不是正确的做法。什么是正确的做法?

编辑: make -V 1 的输出是:

  gcc -Wp,-MD,/home/somedir/.someModule.o.d  -nostdinc 
  -isystem /usr/lib/gcc/x86_64-linux-gnu/7/include 
  -I./arch/x86/include -I./arch/x86/include/generated  
  -I./include -I./arch/x86/include/uapi 
  -I./arch/x86/include/generated/uapi 
   -I./include/uapi 
  -I./include/generated/uapi -include ./include/linux/kconfig.h 
  -Iubuntu/include  -D__KERNEL__ -Wall -Wundef 
  -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing 
  -fno-common -fshort-wchar -Werror-implicit-function-declaration 
  -Wno-format-security -std=gnu89 -fno-PIE -mno-sse -mno-mmx -mno-sse2 
  -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 
  -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup 
  -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time 
  -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 
  -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 
  -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 
  -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 
  -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe 
  -Wno-sign-compare -fno-asynchronous-unwind-tables 
  -mindirect-branch=thunk-extern -mindirect-branch-register 
  -DRETPOLINE -fno-delete-null-pointer-checks -Wno-frame-address 
  -Wno-format-truncation -Wno-format-overflow -Wno-int-in-bool-context -O2 
  --param=allow-store-data-races=0 -DCC_HAVE_ASM_GOTO -Wframe-larger-than=1024 
  -fstack-protector-strong -Wno-unused-but-set-variable -Wno-unused-const-variable 
  -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments 
  -pg -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign 
  -fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check 
  -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -Werror=date-time 
  -Werror=incompatible-pointer-types -Werror=designated-init  -DMODULE  -DKBUILD_BASENAME='"someModule"'  
  -DKBUILD_MODNAME='"someModule"' -c -o /home/somedir/someModule.o /home/somedir/someModule.c

编辑:我的代码中的错误是我遵循了我在网上遇到的一个例子。钩子函数的签名和一些函数名确实也发生了变化。如果您在网上遇到使用 netfilter 函数的示例,请确保您使用的是 API 的正确版本。

如果您的内核模块包含 /usr/include/linux 中的 header,那确实是出了问题:/usr/include/linux 中的文件供用户态应用程序使用(请参阅 What's the difference between /usr/include/linux and the include folder in linux kernel source?), 不是内核模块。

相反,您的模块应该在内核源代码中包含 header 文件。

就其价值而言,这似乎是我系统上的默认设置。如果我编译这个 not-a-kernel-module:

#include <linux/netfilter.h>

void my_fn(void) {
    struct nf_hook_ops ops;
}

使用与您的代码段相同的 Makefile,它成功了,当我 运行 make V=1 报告它正在调用 gcc 并带有标志以跳过标准包含目录,并在正确的位置添加 include 目录:

gcc ... -nostdinc ... -I./include ...

如果你的没有,那就是那里出了问题,但如果不看代码就很难说出是什么。