Chinaunix

标题: 防火墙注册函数出错,谢谢大家帮我查查。 [打印本页]

作者: unix_luntan    时间: 2010-02-25 10:15
标题: 防火墙注册函数出错,谢谢大家帮我查查。
各位兄弟:
    新年好!
    说正题,我想在netfilter的NF_IP_PRE_ROUTING点上注册一个函数,函数很简单,就是打印出数据包的源IP地址,不知道为什么模块加载后运行后,电脑出现‘死机’,键盘的caps lock 灯和scroll lock灯拼命闪。问题应该是出现在:
printk(KERN_ALERT "a packet from : %d.%d.%d.%d \n", NIPQUAD(iph->saddr));

    更细点,应该是出现在NIPQUAD(iph->saddr)这里,不知道是什么错。请大家帮我查查!

   查网上资料说,如果在内核态运行printk过多会出现这种情况,可是,即使我只让注册函数里的printk运行一次,也会出现这样的情况。问题还是在NIPQUAD(iph->saddr)这里,我这里的错误跟运行多次printk无关。
   系统为fedora8 内核版本为2.6.28.



#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.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 *))
{
   struct sk_buff * sb=*skb;
   struct iphdr  *iph;
   iph=ip_hdr(sb);

   printk(KERN_ALERT "a packet from : %d.%d.%d.%d \n", NIPQUAD(iph->saddr));
   
return NF_ACCEPT;
}

static int hello_init(void)
{
   nfho.hook=hook_func;
   nfho.hooknum= 0;
   nfho.pf= PF_INET;
   nfho.priority =NF_IP_PRI_FIRST;
   nf_register_hook(&nfho);
   printk(KERN_ALERT "hello,world\n");
   return 0;
}

static void hello_exit(void)
{
   nf_unregister_hook(&nfho);
   printk(KERN_ALERT "goodbye,cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
作者: unix_luntan    时间: 2010-02-25 14:25
问题已经解决了,谢谢大家!
作者: platinum    时间: 2010-02-25 22:55
即使问题解决了,也最好告诉大家一下是什么问题以及如何解决的,与大家一同分享不是更好吗?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2