当前位置: 技术问答>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的,但是加载完后立马就会死机,或者过会儿死机,我也不知道什么原因,恳请哪位大虾能解释一下!帮帮忙了啊,本人新手!!
在线等
#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非常危险,很容易让内核崩溃。
}