Network hooks hanging the system

懵懂的女人 提交于 2019-12-12 01:45:15

问题


I was testing the network hook code given in https://en.wikipedia.org/wiki/Hooking . My kernel version is 3.11.

 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/skbuff.h>
 #include <net/ip.h>
 #include <linux/ip.h>
 #include <linux/tcp.h>
 #include <linux/in.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter_ipv4.h>

 /* Port we want to drop packets on */
 static const uint16_t port = 25;

 /* This is the hook function itself */
 static unsigned int hook_func(unsigned int hooknum,
                   struct sk_buff **pskb,
                   const struct net_device *in,
                   const struct net_device *out,
                   int (*okfn)(struct sk_buff *)){
    struct iphdr *iph = ip_hdr(*pskb);
    struct tcphdr *tcph, tcpbuf;

    if (iph->protocol != IPPROTO_TCP)
            return NF_ACCEPT;

    tcph = skb_header_pointer(*pskb, ip_hdrlen(*pskb), sizeof(*tcph), &tcpbuf);
    if (tcph == NULL)
            return NF_ACCEPT;

    return (tcph->dest == port) ? NF_DROP : NF_ACCEPT;
}

 /* Used to register our hook function */
 static struct nf_hook_ops nfho = {
    .hook     = hook_func,
    .hooknum  = NF_INET_PRE_ROUTING,
    .pf       = NFPROTO_IPV4,
    .priority = NF_IP_PRI_FIRST,
 };

 static __init int my_init(void)
 {
    return nf_register_hook(&nfho);
 }

 static __exit void my_exit(void)
 {
     nf_unregister_hook(&nfho);
 }

 module_init(my_init);
 module_exit(my_exit);

But after loading , the above code is freezing the system. Can anybody please tell, what is the reason for that?. I think that the above code might have already tested in some lower versions of kernel. So I doubt, some kernel parameters or features have been changed.

来源:https://stackoverflow.com/questions/35647509/network-hooks-hanging-the-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!