- 论坛徽章:
- 0
|
本帖最后由 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函数内并没有发现自己封装的数据包。。。-
- oeth = eth_hdr(skb);
- if(ntohs(oeth->h_proto) != ETH_P_IP)
- goto nf2_out;
- oiph = (struct iphdr *)skb->network_header;
- //if(!strncmp(skb->dev->name, "nf2", 3)){
- if(oiph->daddr == in_aton("10.0.3.7")){
- printk(KERN_DEBUG "------------------found dip=10.0.3.7-------------------\n");
- nfdev = dev_get_by_name(&init_net, "eth0");
- if(nfdev == NULL){
- printk(KERN_DEBUG "nf2 dev_get_by_name failed.\n");
- goto nf2_out;
- }
- nfskb = alloc_skb(skb->len + sizeof(struct iphdr) + LL_MAX_HEADER, GFP_ATOMIC);
- if(nfskb == NULL){
- printk(KERN_DEBUG "nf2 alloc_skb failed.\n");
- goto nf2_out;
- }
- skb_reserve(nfskb, LL_MAX_HEADER);
- nfskb->dev = nfdev;
- nfskb->pkt_type = PACKET_HOST;
- nfskb->protocol = __constant_htons(ETH_P_IP);
- skb->ip_summed = CHECKSUM_NONE;
- skb->priority = 0;
- //copy data from skb->data to nfskb->data
- nfdata = (u_char *)skb_put(nfskb, skb->len);
- if(nfdata != NULL){
- printk(KERN_DEBUG "nfdata not null, copy length: %d bytes\n", skb->len);
- memcpy(nfdata, nfskb->data, skb->len);
- }
- else
- goto nf2_out;
-
- //fill ip header
- skb_push(nfskb, sizeof(struct iphdr));
- skb_reset_network_header(nfskb);
- iph = ip_hdr(nfskb);
- iph->version = 4;
- iph->ihl = sizeof(struct iphdr)>>2;
- iph->frag_off = 0;
- iph->protocol = htons(IPPROTO_IP);
- iph->daddr = in_aton(dip);
- iph->saddr = in_aton(sip);
- iph->tot_len = IP_HLEN+skb->len;
- iph->ttl = 0xff;
- iph->check = 0;
- iph->check = ip_fast_csum(nfskb, iph->ihl);
- //back to ether header
- skb_push(nfskb, ETH_HLEN);
- skb_reset_mac_header(nfskb);
- eth_h = eth_hdr(nfskb);
- memcpy(eth_h->h_dest, DMAC, ETH_ALEN);
- memcpy(eth_h->h_source, SMAC, ETH_ALEN);
- eth_h->h_proto = __constant_htons(ETH_P_IP);
- // kfree_skb(skb);
- // skb = nfskb;
- /*
- get the packet_type structure, tell deliver_skb which
- network function registered process this packet, IMPORTANT!!
- */
- pt_prev = NULL;
- list_for_each_entry_rcu(ptype, &ptype_all, list) {
- if (ptype->type == htons(ETH_P_IP)) {
- ret = deliver_skb(nfskb, ptype, orig_dev);
- }
- else
- printk(KERN_DEBUG "******cannot deliver*********\n");
- }
- printk(KERN_DEBUG "nf2 end~\n");
- }
复制代码 |
|