免费注册 查看新帖 |

Chinaunix

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

[网络子系统] 在网络二层构造数据包失败,求指教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-27 15:43 |只看该作者 |倒序浏览
本帖最后由 silver0924 于 2013-03-27 15:43 编辑

目标:在dev.c内函数netif_receive_skb内,把接收到的目标地址为10.0.3.7的数据包封装到一个新的IP包内,
然后传给三层进行操作,不报错,但是不知道为何失败

数据包的构造过程是: 分配一个skb,先调用skb_put拷贝IP包的负载,然后再push IP头,push 以太网头部
最后调用deliver_skb将数据包送给ip_rcv处,但是在ip_rcv函数内并没有发现自己封装的数据包。。。
  1.        
  2.         oeth = eth_hdr(skb);
  3.         if(ntohs(oeth->h_proto) != ETH_P_IP)
  4.                 goto nf2_out;
  5.         oiph = (struct iphdr *)skb->network_header;
  6.         //if(!strncmp(skb->dev->name, "nf2", 3)){
  7.         if(oiph->daddr == in_aton("10.0.3.7")){

  8.                 printk(KERN_DEBUG "------------------found dip=10.0.3.7-------------------\n");
  9.                 nfdev = dev_get_by_name(&init_net, "eth0");
  10.                 if(nfdev == NULL){
  11.                         printk(KERN_DEBUG "nf2 dev_get_by_name failed.\n");
  12.                         goto nf2_out;
  13.                 }

  14.                 nfskb = alloc_skb(skb->len + sizeof(struct iphdr) + LL_MAX_HEADER, GFP_ATOMIC);       
  15.                 if(nfskb == NULL){
  16.                         printk(KERN_DEBUG "nf2 alloc_skb failed.\n");
  17.                         goto nf2_out;
  18.                 }

  19.                 skb_reserve(nfskb, LL_MAX_HEADER);

  20.                 nfskb->dev = nfdev;
  21.                 nfskb->pkt_type = PACKET_HOST;
  22.                 nfskb->protocol = __constant_htons(ETH_P_IP);
  23.                 skb->ip_summed = CHECKSUM_NONE;
  24.                 skb->priority = 0;

  25.                 //copy data from skb->data to nfskb->data
  26.                 nfdata = (u_char *)skb_put(nfskb, skb->len);
  27.                 if(nfdata != NULL){
  28.                         printk(KERN_DEBUG "nfdata not null, copy length: %d bytes\n", skb->len);
  29.                         memcpy(nfdata, nfskb->data, skb->len);
  30.                 }
  31.                 else
  32.                         goto nf2_out;
  33.        

  34.                 //fill ip header
  35.                 skb_push(nfskb, sizeof(struct iphdr));
  36.                 skb_reset_network_header(nfskb);
  37.                 iph = ip_hdr(nfskb);
  38.                 iph->version = 4;
  39.                 iph->ihl = sizeof(struct iphdr)>>2;
  40.                 iph->frag_off = 0;
  41.                 iph->protocol = htons(IPPROTO_IP);
  42.                 iph->daddr = in_aton(dip);
  43.                 iph->saddr = in_aton(sip);
  44.                 iph->tot_len = IP_HLEN+skb->len;
  45.                 iph->ttl = 0xff;
  46.                 iph->check = 0;
  47.                 iph->check = ip_fast_csum(nfskb, iph->ihl);

  48.                 //back to ether header
  49.                 skb_push(nfskb, ETH_HLEN);
  50.                 skb_reset_mac_header(nfskb);
  51.                 eth_h = eth_hdr(nfskb);
  52.                 memcpy(eth_h->h_dest, DMAC, ETH_ALEN);
  53.                 memcpy(eth_h->h_source, SMAC, ETH_ALEN);
  54.                 eth_h->h_proto = __constant_htons(ETH_P_IP);
  55. //                kfree_skb(skb);
  56.         //        skb = nfskb;
  57.                 /*
  58.                         get the packet_type structure, tell deliver_skb which
  59.                         network function registered process this packet, IMPORTANT!!
  60.                 */
  61.                 pt_prev = NULL;
  62.                 list_for_each_entry_rcu(ptype, &ptype_all, list) {
  63.                         if (ptype->type == htons(ETH_P_IP)) {
  64.                                 ret = deliver_skb(nfskb, ptype, orig_dev);
  65.                         }
  66.                         else
  67.                                 printk(KERN_DEBUG "******cannot deliver*********\n");
  68.                 }

  69.                 printk(KERN_DEBUG "nf2 end~\n");
  70.         }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP