+ -
当前位置:首页 → 问答吧 → 一个内核模块,总是死机,请给看看,谢谢。

一个内核模块,总是死机,请给看看,谢谢。

时间:2010-10-13

来源:互联网

这是我在网上找到的一个内核模块,功能是记录PING,并显示出来。

原来程序是针对老内核的,后来我上网查到,新的内核SKBUFF定义有所变化,便改了改后,MAKE,但是总是死机,请高手给看看,谢谢了。

改动的两处我已经注明。
  1. #include <linux/init.h>
  2. #include <linux/types.h>
  3. #include <linux/netdevice.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/netfilter_ipv4.h>
  6. #include <linux/inet.h>
  7. #include <linux/in.h>
  8. #include <linux/ip.h>

  9. static unsigned int icmp_srv(unsigned int hook,
  10.                                                struct sk_buff **pskb,
  11.                                                const struct net_device *in,
  12.                                                const struct net_device *out,
  13.                                                int (*okfn)(struct sk_buff *)
  14.                                                )
  15. {
  16.        //printk(KERN_INFO"hook_icmp::icmp_srv()\n");
  17.        struct iphdr *iph = (*pskb)->network_header;    //这块我改动过原来是 nh.iph
  18.       
  19.        if(iph->protocol == IPPROTO_ICMP)
  20.        {
  21.               printk(KERN_INFO"hook_icmp::icmp_srv: receive ICMP packet\n");
  22.               printk(KERN_INFO"src: ");
  23.        }
  24.       
  25.        return NF_ACCEPT;
  26. }

  27. static struct nf_hook_ops icmpsrv_ops =
  28. {
  29.        .hook = icmp_srv,
  30.        .pf = PF_INET,
  31.        .hooknum = NF_INET_PRE_ROUTING, //这块我改动过,原来那个是用户态的,不支持了,改成新的了。
  32.        .priority = NF_IP_PRI_FILTER -1,
  33. };

  34. static int __init init_hook_icmp(void)
  35. {
  36.        return nf_register_hook(&icmpsrv_ops);
  37. }

  38. static void __exit fini_hook_icmp(void)
  39. {
  40.        nf_unregister_hook(&icmpsrv_ops);
  41. }

  42. MODULE_LICENSE("GPL");

  43. module_init(init_hook_icmp);
  44. module_exit(fini_hook_icmp);
复制代码

作者: 程序c   发布时间: 2010-10-13

内核版本是多少,确定一下 hook 函数的接口形式是否正确

作者: Godbach   发布时间: 2010-10-13

主要就是第二个参数
     struct sk_buff **pskb,
在你的内核版本中是否仍旧是二级指针

作者: Godbach   发布时间: 2010-10-13

我的内核版本是2.6.31

如何查是否是二级指针麻烦给指点一下

源码地址是 http://lxr.linux.no/#linux+v2.6.31/include/linux/skbuff.h#L1196

作者: 程序c   发布时间: 2010-10-13

static struct nf_hook_ops
这个结构体中的一个成员是函数指针,也就是定义的 hook 函数。看一下函数的参数形式。

作者: Godbach   发布时间: 2010-10-13

实在抱歉。这个我找到了,不过我还是有点看不明白。可能我基础不太好吧。

96struct nf_hook_ops
  97{
  98        struct list_head list;
  99
100        /* User fills in from here down. */
101        nf_hookfn *hook;
102        struct module *owner;
103        u_int8_t pf;
104        unsigned int hooknum;
105        /* Hooks are ordered in ascending priority. */
106        int priority;
107};

作者: 程序c   发布时间: 2010-10-13