我想试一下使用往netfliter上某个hook点装个钩子函数上去,然后在网上找了一下相关的文章,然后把网上的代码大致改了一下,如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/ip.h>
#include <linux/version.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/moduleparam.h>
#include <linux/netfilter_ipv4/ip_tables.h>
static unsigned int myhook_func(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
return NF_DROP;
}
static struct nf_hook_ops nfho={
.hook = myhook_func,
.owner = THIS_MODULE,
.pf = PF_INET,
.hooknum = NF_IP_LOCAL_OUT,
.priority = NF_IP_PRI_FIRST,
};
static int __init myhook_init(void)
{
return nf_register_hook(&nfho);
}
static void __exit myhook_fini(void)
{
nf_unregister_hook(&nfho);
}
module_init(myhook_init);
module_exit(myhook_fini);
/******************************************************/
Makefile如下:
ifneq ($(KERNELRELEASE),)
mymodule-objs:=test0.c
obj-m += test0.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
endif
/**********************************************/
make以后的结果如下:
make -C /lib/modules/3.16.0-23-generic/build M=/home/author/workspace/TestNF modules
make[1]: Entering directory '/usr/src/linux-headers-3.16.0-23-generic'
CC [M] /home/author/workspace/TestNF/test0.o
/home/author/workspace/TestNF/test0.c:17:9: warning: initialization from incompatible pointer type
.hook = myhook_func,
^
/home/author/workspace/TestNF/test0.c:17:9: warning: (near initialization for ‘nfho.hook’)
/home/author/workspace/TestNF/test0.c:20:27: error: ‘NF_IP_LOCAL_OUT’ undeclared here (not in a function)
.hooknum = NF_IP_LOCAL_OUT,
^
scripts/Makefile.build:263: recipe for target '/home/author/workspace/TestNF/test0.o' failed
make[2]: *** [/home/sineatos/workspace/TestNF/test0.o] Error 1
Makefile:1345: recipe for target '_module_/home/author/workspace/TestNF' failed
make[1]: *** [_module_/home/author/workspace/TestNF] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-23-generic'
Makefile:11: recipe for target 'all' failed
make: *** [all] Error 2
请问究竟是什么问题?
需要怎么解决?
将其修改为NF_INET_LOCAL_IN (include/linux/netfilter.h)
在uapi/linux/netfilter_ipv4.h中对NF_IP_LOCAL_IN中对其定义,前面有 #ifndef KERNEL 限制,并且存在注释/* Only for userspace compability*/