Linux驱动4_NETFILTER

#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/init.h>  
#include <linux/types.h>  
#include <linux/netdevice.h>  
#include <linux/skbuff.h>  
#include <linux/netfilter_ipv4.h>  
#include <linux/inet.h>  
#include <linux/in.h>  
#include <linux/ip.h>
//  hook函数
static unsigned int hook_func(unsigned int hooknum, struct sk_buff * skb, const struct net_device *in, 
    const struct net_device *out, int (*okfn) (struct sk_buff *))  
{
  unsigned int ret = NF_ACCEPT;
  if(!skb)
  {
    printk(KERN_ALERT "hook_func null skb.\n");
    goto err_null_skb;
  }
  printk(KERN_ALERT "hook_func get a skb.\n");
  return ret;
err_null_skb:
  return ret;
}  

struct nf_hook_ops hook_ops = {  
  .list =  {NULL,NULL},
  .hook = hook_func,
  //hook的协议族
  .pf = PF_INET,
  //接收到的包,路由之前的hook点  
  .hooknum = NF_INET_PRE_ROUTING,  
  .priority = NF_IP_PRI_FILTER
 };  
  
static int __init hook_init(void) 
{
  nf_register_hook(&hook_ops);  
  return 0;  
}  
  
static void __exit hook_exit(void) 
{  
  nf_unregister_hook(&hook_ops); 
}  
 
 MODULE_LICENSE("GPL");  
 module_init(hook_init);  
 module_exit(hook_exit);  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。