Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72815
  • 博文数量: 27
  • 博客积分: 2120
  • 博客等级: 大尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-25 20:51
文章分类
文章存档

2011年(1)

2010年(10)

2009年(16)

我的朋友

分类: LINUX

2009-10-25 21:09:35

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

#include 
<linux/module.h>
#include 
<linux/kernel.h>
#include 
<linux/netfilter.h>
#include 
<linux/skbuff.h>
#include 
<linux/ip.h>
#include 
<linux/netdevice.h>
#include 
<linux/if_ether.h>
#include 
<linux/if_packet.h>
#include 
<net/tcp.h>
#include 
<linux/netfilter_ipv4.h>

static struct nf_hook_ops nfho;

/*回调函数,进行报文分析*/
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 *))
{
    
if(skb)
    {
        
int static num = 0;
        
struct sk_buff *sb = skb;
        
struct iphdr *iph = (struct iphdr *)(sb->network_header);

        
if(iph)
        {
            
char pro_name[10= {0};
            
            
/*先读协议类型*/
            
switch(iph->protocol)
            {
                
case IPPROTO_UDP:
                    sprintf(pro_name, 
"UDP"); 
                    
break;
           
                
case IPPROTO_TCP:
                    sprintf(pro_name, 
"TCP"); 
                    
break;

               
default:
                    sprintf(pro_name, 
"UNKOWN");
           }
            printk(
"get a ip packet, protocl is :%d(%s)\n", iph->protocol, pro_name);
        }
        
else
        {
            printk(
"iph is null\n");
        }
    }
    
else
    {
        printk(
"skb is null,hooknum:%d\n", hooknum);
    }

    
return NF_ACCEPT;         
}


int init_module()
{
 
    nfho.hook 
= hook_func;        
    nfho.hooknum  
= NF_INET_PRE_ROUTING;
    nfho.pf       
= PF_INET;
    nfho.priority 
= NF_IP_PRI_FIRST; 

    nf_register_hook(
&nfho);

    printk(
"init module NF_INET_PRE_ROUTING ok\n");

    
return 0;
}

void cleanup_module()
{
    nf_unregister_hook(
&nfho);
}
阅读(656) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~