Netfilter内核模块包发送

时间:2017-08-31 01:28:07

标签: c sockets tcp linux-kernel

我正在尝试从netfilter内核模块发送一个虚拟的syn数据包,但我无法成功,任何机构都有工作示例或告诉我我在哪里做错了

printk(KERN_ALERT "Device a");
struct sk_buff *skb = NULL;
struct iphdr *iph = NULL;
struct tcphdr *tcph = NULL;
printk(KERN_ALERT "Device index");
skb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr), GFP_ATOMIC); // Allocate a new sk_buff with room for L2 header.

if (skb == NULL){
    return;
}

skb->protocol = __constant_htons(ETH_P_IP); // This is an IP packet.
skb->pkt_type = PACKET_OUTGOING; // Its outgoing.
skb->ip_summed = CHECKSUM_NONE; // No need to checksum.

skb_reserve(skb, sizeof(struct iphdr) + sizeof(struct tcphdr)); // Reserve the space for the L3, and L4 headers.
tcph = (struct tcphdr *)skb_push(skb, sizeof(struct tcphdr)); // Setup pointer for the L4 header.
iph = (struct iphdr *)skb_push(skb, sizeof(struct iphdr)); // Setup pointer for the L3 header.

iph->ihl = 5; // IP header length.
iph->version = 4; // IPv4.
iph->tos = 0; // No TOS.
iph->tot_len=htons(sizeof(struct iphdr) + sizeof(struct tcphdr)); // L3 + L4 header length.
iph->id = 0; // What?
iph->frag_off = 0; // No fragmenting.
iph->ttl = 64; // Set a TTL.
iph->protocol = IPPROTO_TCP; // TCP protocol.
iph->check = 0; // No IP checksum yet.
iph->saddr = saddr; // Source IP.
iph->daddr = daddr; // Dest IP.
tcph->check = 0; // No TCP checksum yet.
tcph->source = source; // Source TCP Port.
tcph->dest = dest; // Destination TCP Port.
tcph->seq = htonl(seq - 1); // Current SEQ minus one is used for TCP keepalives.
tcph->ack_seq = htonl( ack_seq - 1); // Ummm not sure yet.
tcph->res1 = 0; // Not sure.
tcph->doff = 5; // TCP Offset.  At least 5 if there are no TCP options.
tcph->fin = 0; // FIN flag.
tcph->syn = 0; // SYN flag.
tcph->rst = 0; // RST flag.
tcph->psh = 0; // PSH flag.
tcph->ack = 1; // ACK flag.
tcph->urg = 0; // URG flag.
tcph->ece = 0; // ECE flag? It should be 0.
tcph->cwr = 0; // CWR flag? It should be 0.

//ip_route_input(skb, daddr, saddr, iph->tos, netdevice); // Populate the skb->dst structure.
//ip_send_check(iph); // Calulcate an IP checksum.
//skb->dev = skb->dst->dev; // Populate skb->dev or it wont send.
//printk(KERN_ALERT "SKB device index: %u.\n",skb->dev->ifindex);
//printk(KERN_ALERT "Route device index: %u.\n",skb->dst->dev->ifindex);

printk(KERN_ALERT "Device index2");
struct net_device *dev;
read_lock(&dev_base_lock);
dev = first_net_device(&init_net);
while (dev) {
    printk(KERN_INFO "found [%s]\n", dev->name);
    if(dev->name=="p1p2"){printk(KERN_INFO "found [%s]\n", dev->name);break;}
    dev = next_net_device(dev);
}

read_unlock(&dev_base_lock);

//NF_HOOK(PF_INET, NF_INET_LOCAL_OUT,NULL, NULL, skb, NULL, dev, dst_output); 
printk(KERN_ALERT "Device index3");
return;

0 个答案:

没有答案