当前位置:  技术问答>linux和unix

钩子点注册遇到的问题 急急急啊

    来源: 互联网  发布时间:2016-08-19

    本文导语:  下面是我的源码: #include #include #include //#include #include #include #include #include #include MODULE_LICENSE("GPL"); static struct nf_hook_ops firewall; //static unsigned char *drop_ip ="x77x4bxd5x33"; static unsigned int user_firewall(unsigned int hooknum,...

下面是我的源码:
#include
#include
#include
//#include
#include
#include
#include
#include
#include

MODULE_LICENSE("GPL");

static struct nf_hook_ops firewall;
//static unsigned char *drop_ip ="x77x4bxd5x33";
static unsigned int user_firewall(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 sk_buff *cskb=alloc_skb(skb->len,GFP_ATOMIC);
    cskb = skb_copy(sb,GFP_ATOMIC);
    struct iphdr *iph;
    struct tcphdr *tcph;
    
    //__u32 sip;
    //__u32 dip;    
    
    //iph = sb->nh.iph;  
    iph = ip_hdr(cskb);    //get the IP header
    tcph = (struct tcphdr *)((__u32)iph + iph->ihl);  //Get the tcp header
    
    if(iph->protocol == 6) //If the protocal is not tcp,let the packet go
        {        
            skb_pull(cskb,iph->ihl*4+sizeof(tcph));  
            int i;
            for(i = 0; itail-cskb->data;i++)
            printk("%c",cskb->data[i]);
            return NF_ACCEPT;
                    
        }    
    else                   
        {
            printk("It is not a tcp packet.Let the packet go!n");
            return NF_ACCEPT;
        }
 
    
    
    
}
int init_module(void)
{
    firewall.hook = user_firewall;
    firewall.hooknum = NF_INET_PRE_ROUTING;
    firewall.pf = PF_INET;
    firewall.priority = NF_IP_PRI_FILTER-1;
    nf_register_hook(&firewall);    
    return 0;
}
void cleanup_module(void)
{
    nf_unregister_hook(&firewall);
}

这个模块是可以INSMOD的,但是加载完后立马就会死机,或者过会儿死机,我也不知道什么原因,恳请哪位大虾能解释一下!帮帮忙了啊,本人新手!!

在线等

|

static unsigned int user_firewall(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 sk_buff *cskb=alloc_skb(skb->len,GFP_ATOMIC);
  cskb = skb_copy(sb,GFP_ATOMIC);
  struct iphdr *iph;
  struct tcphdr *tcph;
   
  //__u32 sip;
  //__u32 dip;  
   
  //iph = sb->nh.iph;  
  iph = ip_hdr(cskb); //get the IP header
  tcph = (struct tcphdr *)((__u32)iph + iph->ihl); //Get the tcp header
   
  if(iph->protocol == 6) //If the protocal is not tcp,let the packet go
  {  
  skb_pull(cskb,iph->ihl*4+sizeof(tcph));  
  int i;
  for(i = 0; itail-cskb->data;i++)
  printk("%c",cskb->data[i]);
  return NF_ACCEPT;
   
  }  
  else  
  {
  printk("It is not a tcp packet.Let the packet go!n");
  return NF_ACCEPT;
  }
 
   
   
   
}


死机的问题肯定是在这个函数里, 你先不做任何处理, 直接   return NF_ACCEPT;
然后再一点一点调试, 看看问题出在哪里。

|
看了一点,对sk_buff等不是很了解
但发现代码中一个非常严重的问题,这个问题有可能导致内核崩溃。

static unsigned int user_firewall(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 sk_buff *cskb=alloc_skb(skb->len,GFP_ATOMIC);
  cskb = skb_copy(sb,GFP_ATOMIC);
/* 此处发现了内存泄露:
 * 函数alloc_skb()分配了一些内存给cskb,需要使用free_skb()释放
 * skb_copy()函数中调用了函数alloc_skb(),cskb已经指向了一块新的内存。
 * 刚才分配的内存已经无法回收了。
 * 在该函数返回前,skb_copy()中分配的内存也没有释放
 * 当这个回调函数被经常调用时,内存会逐渐的被泄露掉
 * 当内存不够时,skb_copy()返回值为NULL
 * 建议修正方法:对函数返回值进行检查,防止出错。
 */
  struct iphdr *iph;
  struct tcphdr *tcph;
   
  //__u32 sip;
  //__u32 dip;  
   
  //iph = sb->nh.iph;  
  iph = ip_hdr(cskb); //get the IP header
 //当cskb为NULL时,此处ip_hdr非常危险,很容易让内核崩溃。
  }

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • linux内核注册钩子是个什么东西?
  • 使用钩子如何锁定键盘的方法分享
  • 请问网络钩子函数怎么使用,一百分相谢。
  • linux下怎样给socket挂钩子
  • Solaris系统下函数钩子如何实现!急急急。。。!!
  • java开发知识 iis7站长之家
  • c#钩子本线程内消息拦截的方法
  • 用netfilter钩子函数统计输出包的个数,出错了!
  • c# 系统钩子的实现代码
  • c# 钩子学习笔记
  • 基于C#实现的HOOK键盘钩子实例代码
  • 学习 c# 钩子的小例子
  • Windows的钩子机制详解
  • c# 键盘钩子的实现代码
  • Linux 钩子模块的编写
  • C# Hook钩子实例代码 截取键盘输入
  • C# Hook钩子实例-截取键盘输入


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3