注册钩子点首先要包含响应的头文件,因为这应该已经属于对kernel的编程了。
1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/init.h>
其次,要定义钩子函数
第三,设置相关信息
1 static unsigned int example(unsigned int hooknum,struct sk_buff* skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *)) 2 { 3 //可以定义一些处理 4 //这里一般要做一个skb非空的判断,可以用if(skb)判断 5 /* 6 struct sk_buff* sk = NULL; 7 sk = skb; 8 一些常用的处理函数: 9 struct iphdr *iph; 10 iph = ip_hdr(sk); 11 注:tcp、udp同理。 12 */ 13 return NF_ACCEPT; 14 }
1 struct nf_hook_ops example_ops = { 2 .list = {NULL,NULL}, 3 .hook = example,//Hook函数 4 .pf = PF_INET,//协议 5 .hooknum = NF_INET_PRE_ROUTING,//钩子点 6 .priority = NF_IP_PRI_FILTER+2//钩子的优先级,其实代表钩子处理函数的调用次序。 7 };
调用注册和注销函数
1 nf_register_hook(&example_ops); 2 nf_unregister_hook(&example_ops); 3 可以写入初始化和注销函数里面 4 static int __init example_init() 5 { 6 nf_register_hook(&example_ops); 7 return 0; 8 } 9 static void __exit example_exit() 10 { 11 nf_unregister_hook(&example_ops); 12 }
关于SKB
skb是linux内核存储描述数据包的一种结构体
也是skb双向链表的一个节点结构体。
skb是linux内核存储描述数据包的一种结构体
也是skb双向链表的一个节点结构体。
1 module_init(example_init); 2 module_exit(example_exit); 3 4 MODULE_AUTHOR("*********"); 5 MODULE_VERSION("0.0.1"); 6 MOUDLE_ALIAS("test"); 7 MODULE_DESCRIPTION("example") ;