V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
likethewind
V2EX  ›  问与答

使用钩子函数修改 skb,在 skb 前添加 ip 头。加载完模块后系统崩掉。各位看看是什么原因?

  •  
  •   likethewind · 2016-08-26 11:12:56 +08:00 · 1813 次点击
    这是一个创建于 2793 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include <linux/ip.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/netfilter.h>
    #include <linux/netfilter_ipv4.h>
    #include <linux/inet.h>
    
    #define print_addr(addr) printk("            %u.%u.%u.%u    \n", \
      ((addr & 0xff000000) >> 24), ((addr & 0x00ff0000) >> 16), \
      ((addr & 0x0000ff00) >> 8),  (addr & 0x000000ff))
    
    MODULE_LICENSE("Dual BSD/GPL");
    
    static struct nf_hook_ops nfho;
    
    //钩子函数,注意参数格式与开发环境源码树保持一致
    unsigned int hook_func(const struct nf_hook_ops *ops, 
            struct sk_buff *skb,
            const struct net_device *in,
            const struct net_device *out,
            int (*okfn)(struct sk_buff *))
    {
    	struct iphdr *iph = (struct iphdr *)(skb->data);	
    
    	
    	if(iph->protocol == 1){
    	struct iphdr *iph_mod = skb_push(skb,20);
    	
    	memcpy(iph_mod,iph,20);
    	
    	iph_mod->daddr = 1661970624;//192.168.15.99 
    	iph_mod->saddr = 16843009;//1.1.1.1
    	iph_mod->tot_len = htons(ntohs(iph->tot_len) + 20);
    
    	skb_reset_network_header(skb);
    
    
    	struct iphdr *iphm = (struct iphdr *)(skb->data);	
    #if 1
    	printk("----------------in-in--in-in-----------------\n");
    	printk("  _ _ _ _ _ _ _ _IPv4 HDR _ _  _ _ _ _ _");
    	printk("\n");
    	printk("|version | iphdr_len | Tos | total len  |");
    	printk("\n");
    	printk("    %u        %u        %u       %u     ",iphm->version,iphm->ihl,iphm->tos,ntohs(iphm->tot_len));
    	printk("\n");
    	printk("|  identity      |      fragment offset |");
    	printk("\n");
    	printk("     %u                       %u        ",ntohs(iphm->id),iphm->frag_off);
    
    	printk("\n");
    	printk("|   ttl   |    protocol  |   check      |\n");
    	printk("     %u           %u           %u          ",iphm->ttl,iphm->protocol,ntohs(iphm->check));
    
    	printk("\n");
    	printk("|              src_ip                   |\n");
    	print_addr(htonl(iphm->saddr));
    	printk("|              dst_ip                   |\n");
    	print_addr(htonl(iphm->daddr));
    	printk("|---------------------------------------|\n");
    #endif	
    	}
    
    	return NF_ACCEPT;
    }
    
    static int __init hook_init(void)
    {
        nfho.hook = hook_func;//关联对应处理函数
        nfho.hooknum = NF_INET_LOCAL_IN;//钩子函数挂载点
        nfho.pf = PF_INET;//ipv4 ,所以用这个
        nfho.priority = NF_IP_PRI_FIRST;//优先级,第一顺位,首先执行我们定义的函数
    
        nf_register_hook(&nfho);//注册
    
        return 0;
    }
    static void __exit hook_exit(void)
    {
        nf_unregister_hook(&nfho);//注销
    }
    
    module_init(hook_init);
    module_exit(hook_exit);
    
    
    
    添加 ip 头的方法有问题么?每次 insmod 模块后,都会死机是什么原因?
    
    
    4 条回复    2016-08-29 16:23:28 +08:00
    ShadowStar
        1
    ShadowStar  
       2016-08-26 11:46:12 +08:00
    哪个版本的内核?
    死机的 panic 信息是什么?
    helloworld2010
        2
    helloworld2010  
       2016-08-26 16:26:27 +08:00
    dead lock?
    likethewind
        3
    likethewind  
    OP
       2016-08-29 16:23:01 +08:00
    @ShadowStar 3.19.0-25-generic 内核。 死机时大写锁定键(Caps Lock)不停闪烁。
    likethewind
        4
    likethewind  
    OP
       2016-08-29 16:23:28 +08:00
    @helloworld2010 貌似不是...........
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5503 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 03:19 · PVG 11:19 · LAX 20:19 · JFK 23:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.