- 论坛徽章:
- 0
|
本帖最后由 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); |
|