免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1263 | 回复: 2
打印 上一主题 下一主题

[网络子系统] 在local_out点挂载了一个模块,运行时出现了一个错误,求解决啊 [复制链接]

求职 : 通讯/电信开
论坛徽章:
2
2015亚冠之鹿岛鹿角
日期:2015-07-08 11:58:2615-16赛季CBA联赛之佛山
日期:2015-12-21 17:28:04
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-10-24 21:33 |只看该作者 |倒序浏览
各位前辈,小弟是仿照http://bbs.chinaunix.net/thread-1941060-1-1.html这位大神的第二个例子,不过不同的是我现在的版本是3.10所以有些函数改变了,运行时出现了一个错误。如下图所示:
  1. [   86.380000] Call Trace:                                                      
  2. [   86.380000] [<802134a8>] skb_copy+0x14/0xc8                                 
  3. [   86.380000] [<8722a030>] 0x8722a030                                          
  4. [   86.380000]                                                                  
  5. [   86.380000]                                                                  
  6. Code: afb00010  afbf001c  afb20018 <8c8200a4> 8c8300a0  8c9200a8  00808821  240
  7. [   86.590000] ---[ end trace 61c61b56e93aa6c0 ]---                             
  8. [   91.610000] CPU 0 Unable to handle kernel paging request at virtual address 0
  9. [   91.620000] Oops[#2]:                                                        
  10. [   91.620000] CPU: 0 PID: 1058 Comm: ntpd Tainted: G      D      3.10.49 #11   
  11. [   91.620000] task: 871cd920 ti: 871a8000 task.ti: 871a8000                    
  12. [   91.620000] $ 0   : 00000000 00000000 8722a000 80112b94                     
  13. [   91.620000] $ 4   : 00000000 00000020 00000000 8787c400                     
  14. [   91.620000] $ 8   : 00000000 8718e824 00000000 0133076f                     
  15. [   91.620000] $12   : 70656e77 8718e854 00000000 72740470                     
  16. [   91.620000] $16   : 871a9bc8 802ffdbc 870cdf00 00000003                     
  17. [   91.620000] $20   : 00000000 00000001 00000004 00000003                     
  18. [   91.620000] $24   : 00000000 80258114                                       
  19. [   91.620000] $28   : 871a8000 871a9b18 ffffff62 8722a030                     
  20. [   91.620000] Hi    : 00000008                                                
  21. [   91.620000] Lo    : 0000061a                                                
  22. [   91.620000] epc   : 802134a8 skb_copy+0x14/0xc8                              
  23. [   91.620000]     Tainted: G      D                                            
  24. [   91.620000] ra    : 8722a030 0x8722a030                                      
  25. [   91.620000] Status: 1000f403 KERNEL EXL IE                                   
  26. [   91.620000] Cause : 00800008                                                
  27. [   91.620000] BadVA : 000000a4                                                
  28. [   91.620000] PrId  : 00019374 (MIPS 24Kc)                                    
  29. [   91.620000] Modules linked in: skbtest ath9k ath9k_common pppoe ppp_async ipn
  30. [   91.620000] Process ntpd (pid: 1058, threadinfo=871a8000, task=871cd920, tls)
  31. [   91.620000] Stack : 803029b0 801adaa8 001a9b54 871a9b68 871a9bc8 802ffdbc 870
  32.           00000000 80218990 879894ac 00000000 871a9bc8 802ffdbc 870cdf00 0000003
  33.           00000000 00000001 00000004 801a21e8 00000000 8794a880 871a9c64 0000004
  34.           80112b94 00000000 8722a5b0 8718e810 870cdf00 870cdf00 00000003 802ffdc
  35.           00000002 00000000 00000001 801a1dac 00000028 8008e368 80302ecc 0000000
  36.           ...                                                                  
  37. [   91.620000] Call Trace:                                                      
  38. [   91.620000] [<802134a8>] skb_copy+0x14/0xc8                                 
  39. [   91.620000] [<8722a030>] 0x8722a030                                          
  40. [   91.620000]                                                                  
  41. [   91.620000]                                                                  
  42. Code: afb00010  afbf001c  afb20018 <8c8200a4> 8c8300a0  8c9200a8  00808821  240
  43. [   91.830000] ---[ end trace 61c61b56e93aa6c1 ]---                             
  44. [   96.850000] CPU 0 Unable to handle kernel paging request at virtual address 0
  45. [   96.860000] Oops[#3]:                                                        
  46. [   96.860000] CPU: 0 PID: 1059 Comm: ntpd Tainted: G      D      3.10.49 #11   
  47. [   96.860000] task: 871cd920 ti: 871a8000 task.ti: 871a8000                    
  48. [   96.860000] $ 0   : 00000000 00000000 8722a000 80112b94                     
  49. [   96.860000] $ 4   : 00000000 00000020 00000000 8787c400                     
  50. [   96.860000] $ 8   : 00000000 8718ec24 00000000 0133076f                     
  51. [   96.860000] $12   : 70656e77 8718ec54 00 96.860000] $16   : 871a9bc8 802ffdb3
  52. [   96.860000] $20   : 00000000 00000001 00000004 00000003                     
  53. [   96.860000] $24   : 00000000 80258114                                       
  54. [   96.860000] $28   : 871a8000 871a9b18 ffffff62 8722a030                     
  55. [   96.860000] Hi    : 0000000d                                                
  56. [   96.860000] Lo    : 000006bb                                                
  57. [   96.860000] epc   : 802134a8 skb_copy+0x14/0xc8                              
  58. [   96.860000]     Tainted: G      D                                            
  59. [   96.860000] ra    : 8722a030 0x8722a030                                      
  60. [   96.860000] Status: 1000f403 KERNEL EXL IE                                   
  61. [   96.860000] Cause : 00800008                                                
  62. [   96.860000] BadVA : 000000a4                                                
  63. [   96.860000] PrId  : 00019374 (MIPS 24Kc)                                    
  64. [   96.860000] Modules linked in: skbtest ath9k ath9k_common pppoe ppp_async ipn
  65. [   96.860000] Process ntpd (pid: 1059, threadinfo=871a8000, task=871cd920, tls)
  66. [   96.860000] Stack : 80301d90 801c03f0 00300000 801d552c 871a9bc8 802ffdbc 870
  67.           00000000 80218990 80301d90 80067ee8 871a9bc8 802ffdbc 87b9df00 0000003
  68.           00000000 00000001 00000004 801a21e8 00000000 8794aaa0 871a9c64 0000004
  69.           80112b94 00000000 8722a5b0 8718ec10 87b9df00 87b9df00 00000003 802ffdc
  70.           00000002 00000000 00000001 801a1dac 00000028 8008e368 00000060 8024500
  71.           ...                                                                  
复制代码
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>

  4. #include <linux/netfilter.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/ip.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_packet.h>
  10. #include <linux/inet.h>
  11. #include <net/tcp.h>
  12. #include <net/udp.h>
  13. #include <net/icmp.h>
  14. #include <linux/netfilter_ipv4.h>


  15. MODULE_LICENSE("GPL");
  16. MODULE_AUTHOR("shenshenlei@163.com");

  17. #define    ETH    "eth1"
  18. unsigned char   SMAC[ETH_ALEN] = {0x4C,0x5E,0x0C,0x85,0x06,0xA9};
  19. unsigned char   DMAC[ETH_ALEN] = {0x00,0x0c,0x29,0x3b,0xd8,0xa9};

  20. static struct nf_hook_ops modify_ops;

  21. static unsigned int modify(unsigned int hooknum, struct sk_buff ** skb,
  22.                                   const struct net_device * in, const struct net_device * out,
  23.                                   int (*okfn)(struct sk_buff *))
  24. {
  25.     struct sk_buff* nskb;
  26.     struct iphdr* nip_hdr;
  27.     unsigned int   nip_hdr_off;
  28.     struct tcphdr* ntcp_hdr;
  29.     unsigned int ntcp_hdr_off;
  30.     struct ethhdr* neth_hdr;
  31.     int ret = 0;
  32.    printk("Prepare skb_copy");
  33.    nskb = skb_copy(*skb, GFP_ATOMIC);
  34.    if(nskb == NULL)
  35.      {
  36.              printk("%s\n", "skb_copy return NULL");
  37.              return NF_ACCEPT;
  38.      }
  39.    if(ip_hdr(nskb)->protocol!=IPPROTO_TCP){
  40.           kfree_skb(nskb);
  41.       return NF_ACCEPT;
  42.     }

  43. /*
  44.         tcp header
  45. */
  46.     nip_hdr = ip_hdr(nskb);
  47.     nip_hdr_off = nip_hdr->ihl << 2;
  48.    
  49.     ntcp_hdr = tcp_hdr(nskb);
  50.     ntcp_hdr_off = ntcp_hdr->doff << 2;
  51.     if(!ntcp_hdr->syn)
  52.     {
  53.             kfree_skb(nskb);
  54.         return NF_ACCEPT;
  55.      }
  56.    
  57.     //evil!
  58.     nip_hdr->daddr = in_aton("192.168.1.168");

  59.     nip_hdr->check = 0;
  60.     nip_hdr->check = ip_fast_csum((unsigned char *)nip_hdr, nip_hdr->ihl);
  61.         
  62.     nskb->csum = csum_partial((unsigned char *)(ntcp_hdr + ntcp_hdr_off),
  63.                                       ntohs(nip_hdr->tot_len) - nip_hdr_off - ntcp_hdr_off, 0);

  64.     ntcp_hdr->check = 0;
  65.     ntcp_hdr->check = csum_tcpudp_magic(nip_hdr->saddr, nip_hdr->daddr,
  66.                                         ntohs(nip_hdr->tot_len) - nip_hdr_off, nip_hdr->protocol,
  67.                                         csum_partial((unsigned char *)ntcp_hdr, ntcp_hdr_off, nskb->csum));


  68.     nskb->ip_summed = CHECKSUM_NONE;
  69.     nskb->pkt_type  = PACKET_OTHERHOST;
  70.    
  71.     nskb->dev = dev_get_by_name(&init_net,ETH);
  72.     if(nskb->dev==NULL)
  73.     {
  74.             printk("%s\n", "dev_get_by_name return NULL");
  75.             kfree_skb(nskb);
  76.             return NF_ACCEPT;
  77.             }
  78.     //ETH        
  79.     nskb->mac_header = skb_push(nskb, ETH_HLEN);
  80.      { //eth headeri
  81.       neth_hdr = (struct ethhdr *)nskb->mac_header;
  82.       memcpy (neth_hdr->h_dest, DMAC, ETH_ALEN);
  83.       memcpy (neth_hdr->h_source, SMAC, ETH_ALEN);
  84.       neth_hdr->h_proto = __constant_htons (ETH_P_IP);
  85.     }
  86.    

  87.     dev_hold(nskb->dev);
  88.     printk(KERN_ERR"dev_hold ok,start xmiting");
  89.    
  90.     ret = dev_queue_xmit(nskb);
  91.     printk(KERN_ERR"ret:%d\n", ret);
  92.     return NF_STOLEN;
  93. }


  94. static int __init init(void)
  95. {
  96.   int  ret = 0;
  97.   modify_ops.hook = modify;
  98.   modify_ops.hooknum = NF_INET_LOCAL_OUT;
  99.   modify_ops.pf = PF_INET;
  100.   modify_ops.priority = NF_IP_PRI_FIRST;

  101.   ret = nf_register_hook(&modify_ops);
  102.   if (ret < 0)
  103.    {
  104.      printk("%s\n", "can't modify skb hook!");
  105.      return ret;
  106.    }

  107.     printk("%s\n", "insmod modify skb module");
  108.     return 0;
  109. }

  110. static void __exit fini(void)
  111. {
  112.     nf_unregister_hook(&modify_ops);
  113.     printk("%s\n", "remove modify skb module.");
  114. }

  115. module_init(init);
  116. module_exit(fini);
复制代码

求职 : 通讯/电信开
论坛徽章:
2
2015亚冠之鹿岛鹿角
日期:2015-07-08 11:58:2615-16赛季CBA联赛之佛山
日期:2015-12-21 17:28:04
2 [报告]
发表于 2015-10-25 10:10 |只看该作者
然后小弟分析这个地方是由于空指针引起的,因此加了个判断*skb是否为空的这里不报错了,接着没有复制转发。然后我又加入了如下的一段代码
if(ip_hdr(nskb)->protocol==IPPROTO_TCP){
        printk(KERN_ERR"RECEIVE  TCP\n");
}
if(ip_hdr(nskb)->protocol!=IPPROTO_TCP){
                printk(KERN_ERR"%d\n",ip_hdr(nskb)->protocol);
                printk(KERN_ERR"RECEIVE NOT TCP\n");
        kfree_skb(nskb);
        return NF_ACCEPT;
然后用iperf测试怎么发现打印的是47 RSVP协议,还请各位帮忙看下啊

求职 : 通讯/电信开
论坛徽章:
2
2015亚冠之鹿岛鹿角
日期:2015-07-08 11:58:2615-16赛季CBA联赛之佛山
日期:2015-12-21 17:28:04
3 [报告]
发表于 2015-10-25 11:52 |只看该作者
问题解决了
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>

  4. #include <linux/netfilter.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/ip.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_packet.h>
  10. #include <linux/inet.h>
  11. #include <net/tcp.h>
  12. #include <net/udp.h>
  13. #include <linux/netfilter_ipv4.h>


  14. MODULE_LICENSE("GPL");
  15. MODULE_AUTHOR("shenshenlei@163.com");

  16. #define    ETH    "eth1"
  17. unsigned char   SMAC[ETH_ALEN] = {0x4C,0x5E,0x0C,0x85,0x06,0xA9};
  18. //VM
  19. //unsigned char   DMAC[ETH_ALEN] = {0x00,0x0c,0x29,0x3b,0xd8,0xa9};
  20. //host
  21. unsigned char   DMAC[ETH_ALEN] = {0xAC,0x22,0x0B,0x1A,0xF4,0xD8};

  22. static struct nf_hook_ops modify_ops;

  23. static unsigned int modify(unsigned int hooknum, struct sk_buff * skb,
  24.                                   const struct net_device * in, const struct net_device * out,
  25.                                   int (*okfn)(struct sk_buff *))
  26. {
  27.        
  28.     struct sk_buff* nskb;
  29.     struct iphdr* nip_hdr;
  30.     unsigned int   nip_hdr_off;
  31.     struct tcphdr* ntcp_hdr;
  32.     unsigned int ntcp_hdr_off;
  33.     struct ethhdr* neth_hdr;
  34.     int ret = 0;
  35.    if(skb!=NULL)
  36.    nskb = skb_copy(skb, GFP_ATOMIC);
  37.    if(nskb == NULL)
  38.      {
  39.              printk(KERN_ERR"%s\n", "skb_copy return NULL");
  40.              return NF_ACCEPT;
  41.      }
  42. if(ip_hdr(nskb)->protocol!=IPPROTO_TCP){
  43.                 //printk(KERN_ERR"%d\n",ip_hdr(nskb)->protocol);
  44.                 //printk(KERN_ERR"RECEIVE NOT TCP\n");
  45.         kfree_skb(nskb);
  46.         return NF_ACCEPT;
  47.   }

  48. /*
  49.         tcp header
  50. */
  51.     nip_hdr = ip_hdr(nskb);
  52.     nip_hdr_off = nip_hdr->ihl << 2;
  53.    
  54.     ntcp_hdr = tcp_hdr(nskb);
  55.     ntcp_hdr_off = ntcp_hdr->doff << 2;
  56.     nip_hdr->daddr = in_aton("192.168.1.148");

  57.     nip_hdr->check = 0;
  58.     nip_hdr->check = ip_fast_csum((unsigned char *)nip_hdr, nip_hdr->ihl);
  59.         
  60.     nskb->csum = csum_partial((unsigned char *)(ntcp_hdr + ntcp_hdr_off),
  61.                                       ntohs(nip_hdr->tot_len) - nip_hdr_off - ntcp_hdr_off, 0);

  62.     ntcp_hdr->check = 0;
  63.     ntcp_hdr->check = csum_tcpudp_magic(nip_hdr->saddr, nip_hdr->daddr,
  64.                                         ntohs(nip_hdr->tot_len) - nip_hdr_off, nip_hdr->protocol,
  65.                                         csum_partial((unsigned char *)ntcp_hdr, ntcp_hdr_off, nskb->csum));


  66.     nskb->ip_summed = CHECKSUM_NONE;
  67.     nskb->pkt_type  = PACKET_OTHERHOST;
  68.    
  69.     nskb->dev = dev_get_by_name(&init_net,ETH);
  70.     if(nskb->dev==NULL)
  71.     {
  72.             printk("%s\n", "dev_get_by_name return NULL");
  73.             kfree_skb(nskb);
  74.             return NF_ACCEPT;
  75.             }
  76.     //ETH        
  77.     nskb->mac_header = skb_push(nskb, ETH_HLEN);
  78.     //eth header
  79.       neth_hdr = (struct ethhdr *)nskb->mac_header;
  80.       memcpy (neth_hdr->h_dest, DMAC, ETH_ALEN);
  81.       memcpy (neth_hdr->h_source, SMAC, ETH_ALEN);
  82.       neth_hdr->h_proto = __constant_htons (ETH_P_IP);

  83.     dev_hold(nskb->dev);
  84.     printk(KERN_ERR"dev_hold ok,start xmiting");
  85.     ret = dev_queue_xmit(nskb);
  86.     printk(KERN_ERR"ret:%d\n", ret);
  87.     return NF_ACCEPT;
  88. }


  89. static int __init init(void)
  90. {
  91.   int  ret = 0;
  92.   modify_ops.hook = modify;
  93.   modify_ops.hooknum = NF_INET_LOCAL_OUT;
  94.   modify_ops.pf = PF_INET;
  95.   modify_ops.priority = NF_IP_PRI_FIRST;

  96.   ret = nf_register_hook(&modify_ops);
  97.   if (ret < 0)
  98.    {
  99.      printk("%s\n", "can't modify skb hook!");
  100.      return ret;
  101.    }

  102.     printk("%s\n", "insmod modify skb module");
  103.     return 0;
  104. }

  105. static void __exit fini(void)
  106. {
  107.     nf_unregister_hook(&modify_ops);
  108.     printk("%s\n", "remove modify skb module.");
  109. }

  110. module_init(init);
  111. module_exit(fini);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP