- 论坛徽章:
- 0
|
本帖最后由 lawrence2013 于 2013-04-02 18:01 编辑
我想把在NF_IP_PRE_ROUTING hook到的一些数据包直接送到4层,我直接调用了ip_local_deliver()函数,但似乎有问题,发udp包的时候不能成功送到4层,发icmp包的时候kernel panic。我该如何解决这个问题?谢谢!
我的代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.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/ip.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <linux/netfilter_ipv4.h>
const char* hooks[] ={"NF_IP_PRE_ROUTING"};
unsigned int
header(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* nskb;
struct iphdr *iph = NULL;
nskb = skb;
if(nskb==NULL)
{
printk("%s\n", "*skb is NULL");
return NF_ACCEPT;
}
iph = ip_hdr(nskb);
if(iph == NULL)
{
printk("%s\n", "*iph is NULL");
return NF_ACCEPT;
}
if ((iph->protocol == IPPROTO_UDP) || (iph->protocol == IPPROTO_ICMP)){
ip_local_deliver(nskb);
printk("------delivered --------\n");
return NF_STOLEN;
}
return NF_ACCEPT;
}
static struct nf_hook_ops header_ops[] = {
{
.hook = header,
.owner = THIS_MODULE,
.pf = PF_INET,
.hooknum = 0, //NF_IP_PRE_ROUTING,
.priority = NF_IP_PRI_FIRST,
},
};
static int __init init(void)
{
int ret;
ret = nf_register_hooks(header_ops, ARRAY_SIZE(header_ops));
if (ret < 0) {
printk("http detect:can't register header_ops detect hook!\n");
return ret;
}
printk("insmod header_ops detect module\n");
return 0;
}
static void __exit fini(void)
{
nf_unregister_hooks(header_ops, ARRAY_SIZE(header_ops));
printk("remove header_ops detect module.\n");
}
module_init(init);
module_exit(fini); |
|