echoall2.cc revision 2c431f66
12c431f66SShuo Chen#include "faketcp.h" 22c431f66SShuo Chen 32c431f66SShuo Chen#include <map> 42c431f66SShuo Chen 52c431f66SShuo Chen#include <stdio.h> 62c431f66SShuo Chen#include <stdlib.h> 72c431f66SShuo Chen#include <unistd.h> 82c431f66SShuo Chen#include <netinet/ip.h> 92c431f66SShuo Chen#include <netinet/tcp.h> 102c431f66SShuo Chen#include <linux/if_ether.h> 112c431f66SShuo Chen 122c431f66SShuo Chenstruct TcpState 132c431f66SShuo Chen{ 142c431f66SShuo Chen uint32_t rcv_nxt; 152c431f66SShuo Chen uint32_t snd_una; 162c431f66SShuo Chen}; 172c431f66SShuo Chen 182c431f66SShuo Chenstd::map<SocketAddr, TcpState> expectedSeqs; 192c431f66SShuo Chen 202c431f66SShuo Chenvoid tcp_input(int fd, const void* input, const void* ippayload, int tot_len) 212c431f66SShuo Chen{ 222c431f66SShuo Chen const struct iphdr* iphdr = static_cast<const struct iphdr*>(input); 232c431f66SShuo Chen const struct tcphdr* tcphdr = static_cast<const struct tcphdr*>(ippayload); 242c431f66SShuo Chen const int iphdr_len = iphdr->ihl*4; 252c431f66SShuo Chen const int tcp_seg_len = tot_len - iphdr_len; 262c431f66SShuo Chen const int tcphdr_size = sizeof(*tcphdr); 272c431f66SShuo Chen if (tcp_seg_len >= tcphdr_size 282c431f66SShuo Chen && tcp_seg_len >= tcphdr->doff*4) 292c431f66SShuo Chen { 302c431f66SShuo Chen const int tcphdr_len = tcphdr->doff*4; 312c431f66SShuo Chen const void* payload = ippayload + tcphdr_len; 322c431f66SShuo Chen const int payload_len = tot_len - iphdr_len - tcphdr_len; 332c431f66SShuo Chen 342c431f66SShuo Chen char source[INET_ADDRSTRLEN]; 352c431f66SShuo Chen char dest[INET_ADDRSTRLEN]; 362c431f66SShuo Chen inet_ntop(AF_INET, &iphdr->saddr, source, INET_ADDRSTRLEN); 372c431f66SShuo Chen inet_ntop(AF_INET, &iphdr->daddr, dest, INET_ADDRSTRLEN); 382c431f66SShuo Chen printf("IP %s.%d > %s.%d: ", 392c431f66SShuo Chen source, ntohs(tcphdr->source), dest, ntohs(tcphdr->dest)); 402c431f66SShuo Chen printf("Flags [%c], seq %u, win %d, length %d\n", 412c431f66SShuo Chen tcphdr->syn ? 'S' : (tcphdr->fin ? 'F' : '.'), 422c431f66SShuo Chen ntohl(tcphdr->seq), 432c431f66SShuo Chen ntohs(tcphdr->window), 442c431f66SShuo Chen payload_len); 452c431f66SShuo Chen 462c431f66SShuo Chen union 472c431f66SShuo Chen { 482c431f66SShuo Chen unsigned char output[ETH_FRAME_LEN]; 492c431f66SShuo Chen struct 502c431f66SShuo Chen { 512c431f66SShuo Chen struct iphdr iphdr; 522c431f66SShuo Chen struct tcphdr tcphdr; 532c431f66SShuo Chen } out; 542c431f66SShuo Chen }; 552c431f66SShuo Chen 562c431f66SShuo Chen assert(sizeof(out) == sizeof(struct iphdr) + sizeof(struct tcphdr)); 572c431f66SShuo Chen int output_len = sizeof(out); 582c431f66SShuo Chen bzero(&out, output_len + 4); 592c431f66SShuo Chen memcpy(output, input, sizeof(struct iphdr)); 602c431f66SShuo Chen 612c431f66SShuo Chen std::swap(out.iphdr.saddr, out.iphdr.daddr); 622c431f66SShuo Chen out.iphdr.check = 0; 632c431f66SShuo Chen 642c431f66SShuo Chen out.tcphdr.source = tcphdr->dest; 652c431f66SShuo Chen out.tcphdr.dest = tcphdr->source; 662c431f66SShuo Chen out.tcphdr.doff = sizeof(struct tcphdr) / 4; 672c431f66SShuo Chen out.tcphdr.window = htons(5000); 682c431f66SShuo Chen 692c431f66SShuo Chen SocketAddr addr = { out.iphdr.saddr, out.iphdr.daddr, out.tcphdr.source, out.tcphdr.dest }; 702c431f66SShuo Chen bool response = false; 712c431f66SShuo Chen const uint32_t seq = ntohl(tcphdr->seq); 722c431f66SShuo Chen if (tcphdr->syn) 732c431f66SShuo Chen { 742c431f66SShuo Chen out.tcphdr.seq = htonl(seq); 752c431f66SShuo Chen out.tcphdr.ack_seq = htonl(seq+1); 762c431f66SShuo Chen out.tcphdr.syn = 1; 772c431f66SShuo Chen out.tcphdr.ack = 1; 782c431f66SShuo Chen TcpState s = { seq + 1, seq + 1 }; 792c431f66SShuo Chen expectedSeqs[addr] = s; 802c431f66SShuo Chen response = true; 812c431f66SShuo Chen } 822c431f66SShuo Chen else if (tcphdr->fin) 832c431f66SShuo Chen { 842c431f66SShuo Chen out.tcphdr.seq = htonl(seq); 852c431f66SShuo Chen out.tcphdr.ack_seq = htonl(seq+1); 862c431f66SShuo Chen out.tcphdr.fin = 1; 872c431f66SShuo Chen out.tcphdr.ack = 1; 882c431f66SShuo Chen expectedSeqs.erase(addr); 892c431f66SShuo Chen response = true; 902c431f66SShuo Chen } 912c431f66SShuo Chen else 922c431f66SShuo Chen { 932c431f66SShuo Chen out.tcphdr.seq = htonl(seq); 942c431f66SShuo Chen out.tcphdr.ack_seq = htonl(seq+payload_len); 952c431f66SShuo Chen out.tcphdr.psh = 1; 962c431f66SShuo Chen out.tcphdr.ack = 1; 972c431f66SShuo Chen assert(output + output_len + payload_len < output + sizeof(output)); 982c431f66SShuo Chen memcpy(output + output_len, payload, payload_len); 992c431f66SShuo Chen output_len += payload_len; 1002c431f66SShuo Chen auto it = expectedSeqs.find(addr); 1012c431f66SShuo Chen if (it != expectedSeqs.end()) 1022c431f66SShuo Chen { 1032c431f66SShuo Chen const uint32_t ack = ntohl(tcphdr->ack_seq); 1042c431f66SShuo Chen printf("seq = %u, ack = %u\n", seq, ack); 1052c431f66SShuo Chen printf("rcv_nxt = %u, snd_una = %u\n", it->second.rcv_nxt, it->second.snd_una); 1062c431f66SShuo Chen if (payload_len > 0 && it->second.rcv_nxt == seq) 1072c431f66SShuo Chen { 1082c431f66SShuo Chen it->second.snd_una = seq+payload_len; 1092c431f66SShuo Chen response = true; 1102c431f66SShuo Chen } 1112c431f66SShuo Chen if (tcphdr->ack && ack == it->second.snd_una) 1122c431f66SShuo Chen { 1132c431f66SShuo Chen it->second.rcv_nxt = ack; 1142c431f66SShuo Chen } 1152c431f66SShuo Chen printf("rcv_nxt = %u, snd_una = %u\n", it->second.rcv_nxt, it->second.snd_una); 1162c431f66SShuo Chen } 1172c431f66SShuo Chen else 1182c431f66SShuo Chen { 1192c431f66SShuo Chen // RST 1202c431f66SShuo Chen } 1212c431f66SShuo Chen } 1222c431f66SShuo Chen 1232c431f66SShuo Chen out.iphdr.tot_len = htons(output_len); 1242c431f66SShuo Chen out.iphdr.check = in_checksum(output, sizeof(struct iphdr)); 1252c431f66SShuo Chen 1262c431f66SShuo Chen unsigned char* pseudo = output + output_len; 1272c431f66SShuo Chen if (payload_len % 2 == 1) 1282c431f66SShuo Chen { 1292c431f66SShuo Chen *pseudo = 0; 1302c431f66SShuo Chen ++pseudo; 1312c431f66SShuo Chen } 1322c431f66SShuo Chen unsigned int len = sizeof(struct tcphdr)+payload_len; 1332c431f66SShuo Chen pseudo[0] = 0; 1342c431f66SShuo Chen pseudo[1] = IPPROTO_TCP; 1352c431f66SShuo Chen pseudo[2] = len / 256; 1362c431f66SShuo Chen pseudo[3] = len % 256; 1372c431f66SShuo Chen out.tcphdr.check = in_checksum(&out.iphdr.saddr, len + 12 + (payload_len % 2)); 1382c431f66SShuo Chen if (response) 1392c431f66SShuo Chen { 1402c431f66SShuo Chen write(fd, output, output_len); 1412c431f66SShuo Chen } 1422c431f66SShuo Chen } 1432c431f66SShuo Chen} 1442c431f66SShuo Chen 1452c431f66SShuo Chenint main() 1462c431f66SShuo Chen{ 1472c431f66SShuo Chen char ifname[IFNAMSIZ] = "tun%d"; 1482c431f66SShuo Chen int fd = tun_alloc(ifname); 1492c431f66SShuo Chen 1502c431f66SShuo Chen if (fd < 0) 1512c431f66SShuo Chen { 1522c431f66SShuo Chen fprintf(stderr, "tunnel interface allocation failed\n"); 1532c431f66SShuo Chen exit(1); 1542c431f66SShuo Chen } 1552c431f66SShuo Chen 1562c431f66SShuo Chen printf("allocted tunnel interface %s\n", ifname); 1572c431f66SShuo Chen sleep(1); 1582c431f66SShuo Chen 1592c431f66SShuo Chen for (;;) 1602c431f66SShuo Chen { 1612c431f66SShuo Chen union 1622c431f66SShuo Chen { 1632c431f66SShuo Chen unsigned char buf[ETH_FRAME_LEN]; 1642c431f66SShuo Chen struct iphdr iphdr; 1652c431f66SShuo Chen }; 1662c431f66SShuo Chen 1672c431f66SShuo Chen const int iphdr_size = sizeof iphdr; 1682c431f66SShuo Chen 1692c431f66SShuo Chen int nread = read(fd, buf, sizeof(buf)); 1702c431f66SShuo Chen if (nread < 0) 1712c431f66SShuo Chen { 1722c431f66SShuo Chen perror("read"); 1732c431f66SShuo Chen close(fd); 1742c431f66SShuo Chen exit(1); 1752c431f66SShuo Chen } 1762c431f66SShuo Chen printf("read %d bytes from tunnel interface %s.\n", nread, ifname); 1772c431f66SShuo Chen 1782c431f66SShuo Chen const int iphdr_len = iphdr.ihl*4; 1792c431f66SShuo Chen if (nread >= iphdr_size 1802c431f66SShuo Chen && iphdr.version == 4 1812c431f66SShuo Chen && iphdr_len >= iphdr_size 1822c431f66SShuo Chen && iphdr_len <= nread 1832c431f66SShuo Chen && iphdr.tot_len == htons(nread) 1842c431f66SShuo Chen && in_checksum(buf, iphdr_len) == 0) 1852c431f66SShuo Chen { 1862c431f66SShuo Chen const void* payload = buf + iphdr_len; 1872c431f66SShuo Chen if (iphdr.protocol == IPPROTO_ICMP) 1882c431f66SShuo Chen { 1892c431f66SShuo Chen icmp_input(fd, buf, payload, nread); 1902c431f66SShuo Chen } 1912c431f66SShuo Chen else if (iphdr.protocol == IPPROTO_TCP) 1922c431f66SShuo Chen { 1932c431f66SShuo Chen tcp_input(fd, buf, payload, nread); 1942c431f66SShuo Chen } 1952c431f66SShuo Chen } 1962c431f66SShuo Chen else 1972c431f66SShuo Chen { 1982c431f66SShuo Chen printf("bad packet\n"); 1992c431f66SShuo Chen for (int i = 0; i < nread; ++i) 2002c431f66SShuo Chen { 2012c431f66SShuo Chen if (i % 4 == 0) printf("\n"); 2022c431f66SShuo Chen printf("%02x ", buf[i]); 2032c431f66SShuo Chen } 2042c431f66SShuo Chen printf("\n"); 2052c431f66SShuo Chen } 2062c431f66SShuo Chen } 2072c431f66SShuo Chen 2082c431f66SShuo Chen return 0; 2092c431f66SShuo Chen} 210