1

我正在从这里制作一个类似于 simpletun 的程序。

  1. 应用程序将 UDP 广播发送到 tap 接口。

  2. 在 Tap 接口上侦听的程序(类似于 simpletun)接收数据包并将其作为广播再次回显。

  3. 我能够在 Wireshark 上看到两个数据包,但应用程序没有收到它。似乎内核正在丢弃它们。

  4. 如果类似的数据包是从不同的机器发送的。它工作正常。

tun/tap 代码:-

char datagram[4096];
struct pseudo_header
{
        u_int32_t source_address;
        u_int32_t dest_address;
        u_int8_t placeholder;
        u_int8_t protocol;
        u_int16_t udp_length;
};
struct pseudo_header psh;
char *pseudogram;

 nread = cread(tap_fd, buffer, BUFSIZE);

    memcpy(&source_port,(char *)(buffer+14+20),2);  

    memset (datagram, 0, 4096);

    memcpy(datagram,buffer,14); //copy eth-header
    datagram[6] = 0xXX;//change mac address
    datagram[7] = 0xXX;
    datagram[8] = 0xXX;
    //IP header
    struct iphdr *iph = (struct iphdr *) (datagram +14);
    //TCP header
    struct udphdr *udph = (struct udphdr *) (datagram + 14 + sizeof (struct iphdr));

    data = datagram + sizeof(struct iphdr) + sizeof(struct udphdr) +14;     
    strcpy(data,"Any string");
    iph->ihl = 5;
    iph->version = 4;
    iph->tos = 0;
    iph->tot_len = sizeof (struct iphdr) + sizeof (struct udphdr) + strlen(data);
    iph->id = htonl (54321);        //Id of this packet
    iph->frag_off = 0;
    iph->ttl = 255;

    iph->protocol = IPPROTO_UDP;
    iph->check = 0;         //Set to 0 before calculating checksum

    strcpy(source_ip , "192.168.0.99");
    iph->saddr = inet_addr ( source_ip );   //Spoof the source ip address
    strcpy(destination_ip , "255.255.255.255");
    iph->daddr = inet_addr (destination_ip);


     udph->source = htons(5933);
    //udph->dest = htons(source_port);
    udph->dest = (source_port);
    udph->len = htons(sizeof(struct udphdr) + strlen(data));


    //Now the UDP checksum
    psh.source_address = inet_addr( source_ip );
    psh.dest_address = inet_addr(destination_ip);
    psh.placeholder = 0;
    psh.protocol = IPPROTO_UDP;
    psh.udp_length = htons(sizeof(struct udphdr) + strlen(data) );

    int psize = sizeof(struct pseudo_header) + sizeof(struct udphdr) + strlen(data);
    pseudogram = malloc(psize);

    memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
    memcpy(pseudogram + sizeof(struct pseudo_header) , udph , sizeof(struct udphdr) + strlen(data));
    memcpy(pseudogram + sizeof(struct pseudo_header) + sizeof(struct udphdr), data , strlen(data));

    udph->check = 0;
    udph->check = csum( (unsigned short*) pseudogram , psize);
    //Ip checksum
    iph->check = csum ((unsigned short *) datagram, iph->tot_len);

    //writeback
    cwrite(tap_fd,datagram,iph->tot_len +14);

上面的代码在使用原始套接字读取和写入的另一台机器上运行时效果很好。(没有 eth 标头)。

接收申请代码:-

int broadcast =1;

if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      err("socket");

setsockopt(sockfd,SOL_SOCKET,SO_BINDTODEVICE,"tun0",5);

bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
memset(&my_addr.sin_addr.s_addr,255,sizeof(my_addr.sin_addr.s_addr));

ret = setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast));
    if(ret<0)
      printf("setsockopt bindto failed");

    if (sendto(sockfd,buf,500,0, (struct sockaddr*)&my_addr,slen_len) == -1)
        err("send()");

    recvfrom(sockfd, buf, 500, 0, (struct sockaddr*)&cli_addr, &slen);
4

3 回答 3

1

通过对 ip 标头的 tot_len 使用正确的校验和和正确的字节顺序来解决它

iph->tot_len = (htons)sizeof (struct iphdr) + sizeof (struct udphdr) + strlen(data);

iph->id = htons (54321); //Id of this packet

iph->check = htons(csum ((unsigned short *) datagram,sizeof(struct ip_hdr))); **其中 csum 是计算校验和的常用函数。

收到来自不同机器的数据包,因为内核的网络堆栈正在重新计算 iph->tot_len 和 ip 校验和。所以数据包是正确形成的。

在更正应用程序接收到 tot_len 数据包的校验和和字节顺序后,使用我的 tun 接口。

于 2013-03-19T05:56:50.030 回答
1

你检查过主机防火墙规则吗?

于 2013-03-15T09:16:24.020 回答
0

暗中猜测:

但是您可以尝试更改您的 TUN/TAP 代码,将目标MAC地址设置为广播(FF:FF:FF:FF:FF:FF),当它将 IP 目标视为广播 IP(255.255.255.255)?

于 2013-03-15T09:56:15.337 回答