免费注册 查看新帖 |

Chinaunix

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

[内核模块] NF_BR_PRE_ROUTING 这个hook点 构造的数据包 对方无法收到数据 [复制链接]

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

在桥接模式下,构造了一个数据包 调用dev_queue_xmit(skb)函数发送,在发送端tcpdump能抓到数据包,但在对端抓不到包。这个怎么个情况呢?

代码如下

  struct sk_buff * skb = NULL;

  struct net_device * dev = NULL;

  struct ethhdr * ethdr = NULL;

  struct iphdr * iph = NULL;

  struct tcphdr * tcph = NULL;

  u_char * pdata = NULL;

  int nret = 1;


  if (NULL == smac || NULL == dmac)
  {
        printk("MAC is NULL\n");
        goto out;
}

  dev = dev_get_by_name(&init_net, eth);

  if (NULL == dev)
  {
        printk("----not find device\n");
        goto out;
        }

  skb = alloc_skb (pkt_len + sizeof (struct iphdr) + sizeof (struct tcphdr) + LL_RESERVED_SPACE (dev), GFP_ATOMIC);

  

  if (NULL == skb)
  {  printk("skb is NULL\n");
    goto out;}

  

  skb_reserve (skb, LL_RESERVED_SPACE (dev));


  skb->dev = dev;

  skb->pkt_type = PACKET_OTHERHOST;

  skb->protocol = __constant_htons(ETH_P_IP);

  skb->ip_summed = CHECKSUM_NONE;

  skb->priority = 0;

  iph = ip_hdr(skb);
  
  tcph = tcp_hdr(skb);
  
   skb->network_header = skb_put (skb, sizeof (struct iphdr));
       
  skb->transport_header = skb_put (skb, sizeof (struct tcphdr));
  
// iph= (struct iphdr*)skb_put(skb, sizeof (struct iphdr));

// tcph = (struct tcphdr*)skb_put(skb, sizeof (struct tcphdr));


  pdata = skb_put (skb, pkt_len);
  {
  
    if (NULL == pkt)
        printk("pkt is NULL\n");
     memcpy (pdata, pkt, pkt_len);
  
  }

  


  {

    tcph = (struct tcphdr *) skb->transport_header;

    memset (tcph, 0, sizeof (struct tcphdr));

    tcph->source = sport;

    tcph->dest = dport;

    tcph->seq = seq;

    tcph->ack_seq = ack_seq;

    tcph->doff = 5;

    tcph->psh = psh;

    tcph->fin = fin;

    tcph->syn = 1;

    tcph->ack = 0;

    tcph->window = __constant_htons (5840);

    skb->csum = 0;

    tcph->check = 0;

  }

  

  {

    iph =  (struct iphdr*) skb->network_header;

    iph->version = 4;

    iph->ihl = sizeof(struct iphdr)>>2;

    iph->frag_off = 0;

    iph->protocol = IPPROTO_TCP;

    iph->tos = 0;

    iph->daddr = dip;

    iph->saddr = sip;

    iph->ttl = 0x40;

    iph->tot_len = __constant_htons(skb->len);

    iph->check = 0;

  }

  

  skb->csum = skb_checksum (skb, iph->ihl*4, skb->len - iph->ihl * 4, 0);

  tcph->check = csum_tcpudp_magic (sip, dip, skb->len - iph->ihl * 4, IPPROTO_TCP, skb->csum);


  

  skb->mac_header  = skb_push (skb, 14);

  {
    ethdr = (struct ethhdr *)skb->mac_header;

    memcpy (ethdr->h_dest, dmac, ETH_ALEN);

    memcpy (ethdr->h_source, smac, ETH_ALEN);

    ethdr->h_proto = __constant_htons (ETH_P_IP);

  }

        //netif_rx(skb);
        int a = dev_queue_xmit(skb);
       
        if (0 > a)
        {
                goto out;
        }
  

  nret = 0;

out:

  if (0 != nret && NULL != skb) { dev_put (dev); kfree_skb (skb);}

  

  return (nret);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP