一个netfilter 例 子


#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif


#include   <linux/module.h>  
#include   <linux/kernel.h>  
#include   <linux/skbuff.h>  
#include   <linux/in.h>  
#include   <linux/ip.h>  
#include   <linux/tcp.h>  
#include   <linux/icmp.h>  
#include   <linux/netdevice.h>  
#include   <linux/netfilter.h>  
#include   <linux/netfilter_ipv4.h>  
#include   <linux/if_arp.h>  
#include   <linux/if_ether.h>  
#include   <linux/if_packet.h>  
   
static   struct   nf_hook_ops   nfho;  
unsigned   char*   ip   =   "/x7f/x00/x00/x01";  
unsigned   char*   drop_ip   =   "/xC0/xA8/x08/xC9";  
           
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   *))  
{        
          struct   sk_buff*   sb   =   *skb;  
          struct   tcphdr   *tcp   =   NULL;  
          unsigned   int   d_ip   ,s_ip   =   0;  
          unsigned   int   d_port   =   0,s_port   =   0;  
          if(   sb->nh.iph->protocol   ==   IPPROTO_TCP   )  
                          tcp   =   (struct   tcphdr   *)((sb->data)   +   (sb->nh.iph->ihl   *   4));  
          d_ip   =   sb->nh.iph->daddr;  
          s_ip   =   sb->nh.iph->saddr;  
          if( tcp)  
          {  
                          d_port   =   tcp->source;  
                          s_port   =   tcp->dest;  
          }  
          if(   sb->nh.iph->saddr   ==   *(unsigned   int*)drop_ip   )  
          {  
                                          printk(   "Droped   packet   from   ..%d.%d.%d.%d:%d/n",   NIPQUAD(s_ip),   s_port   );  
                                          return   NF_DROP;  
          }  
            printk(   "FROM:%d.%d.%d.%d:%d   TO:%d.%d.%d.%d:%d   ETH:%s/n",  NIPQUAD(s_ip),   s_port,   NIPQUAD(d_ip),   d_port,   in->name   );  
          return   NF_ACCEPT;  
}  

int   init_module()  
{  
          printk(   "begin   init   module   test2/n"   );  
  nfho.hook           =   hook_func;  
  nfho.hooknum     =   NF_IP_PRE_ROUTING;  
  nfho.pf               =   PF_INET;  
  nfho.priority   =   NF_IP_PRI_FIRST;  

  nf_register_hook(&nfho);  
          printk("init   module   test2/n");  
  return   0;  
}  

void   cleanup_module()  
{  
  nf_unregister_hook(&nfho);  
          printk("cleanup   module   test2/n");  
}  

MODULE_LICENSE("GPL");


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值