17d157501SCoco Li // SPDX-License-Identifier: GPL-2.0 27d157501SCoco Li /* 37d157501SCoco Li * This testsuite provides conformance testing for GRO coalescing. 47d157501SCoco Li * 57d157501SCoco Li * Test cases: 67d157501SCoco Li * 1.data 77d157501SCoco Li * Data packets of the same size and same header setup with correct 87d157501SCoco Li * sequence numbers coalesce. The one exception being the last data 97d157501SCoco Li * packet coalesced: it can be smaller than the rest and coalesced 107d157501SCoco Li * as long as it is in the same flow. 117d157501SCoco Li * 2.ack 127d157501SCoco Li * Pure ACK does not coalesce. 137d157501SCoco Li * 3.flags 147d157501SCoco Li * Specific test cases: no packets with PSH, SYN, URG, RST set will 157d157501SCoco Li * be coalesced. 167d157501SCoco Li * 4.tcp 177d157501SCoco Li * Packets with incorrect checksum, non-consecutive seqno and 187d157501SCoco Li * different TCP header options shouldn't coalesce. Nit: given that 197d157501SCoco Li * some extension headers have paddings, such as timestamp, headers 207d157501SCoco Li * that are padding differently would not be coalesced. 217d157501SCoco Li * 5.ip: 227d157501SCoco Li * Packets with different (ECN, TTL, TOS) header, ip options or 237d157501SCoco Li * ip fragments (ipv6) shouldn't coalesce. 247d157501SCoco Li * 6.large: 257d157501SCoco Li * Packets larger than GRO_MAX_SIZE packets shouldn't coalesce. 267d157501SCoco Li * 277d157501SCoco Li * MSS is defined as 4096 - header because if it is too small 287d157501SCoco Li * (i.e. 1500 MTU - header), it will result in many packets, 297d157501SCoco Li * increasing the "large" test case's flakiness. This is because 307d157501SCoco Li * due to time sensitivity in the coalescing window, the receiver 317d157501SCoco Li * may not coalesce all of the packets. 327d157501SCoco Li * 337d157501SCoco Li * Note the timing issue applies to all of the test cases, so some 347d157501SCoco Li * flakiness is to be expected. 357d157501SCoco Li * 367d157501SCoco Li */ 377d157501SCoco Li 387d157501SCoco Li #define _GNU_SOURCE 397d157501SCoco Li 407d157501SCoco Li #include <arpa/inet.h> 417d157501SCoco Li #include <errno.h> 427d157501SCoco Li #include <error.h> 437d157501SCoco Li #include <getopt.h> 447d157501SCoco Li #include <linux/filter.h> 457d157501SCoco Li #include <linux/if_packet.h> 467d157501SCoco Li #include <linux/ipv6.h> 477d157501SCoco Li #include <net/ethernet.h> 487d157501SCoco Li #include <net/if.h> 497d157501SCoco Li #include <netinet/in.h> 507d157501SCoco Li #include <netinet/ip.h> 517d157501SCoco Li #include <netinet/ip6.h> 527d157501SCoco Li #include <netinet/tcp.h> 537d157501SCoco Li #include <stdbool.h> 547d157501SCoco Li #include <stddef.h> 557d157501SCoco Li #include <stdio.h> 567d157501SCoco Li #include <stdarg.h> 577d157501SCoco Li #include <string.h> 587d157501SCoco Li #include <unistd.h> 597d157501SCoco Li 60*1329e40eSShuah Khan #include "../kselftest.h" 61*1329e40eSShuah Khan 627d157501SCoco Li #define DPORT 8000 637d157501SCoco Li #define SPORT 1500 647d157501SCoco Li #define PAYLOAD_LEN 100 657d157501SCoco Li #define NUM_PACKETS 4 667d157501SCoco Li #define START_SEQ 100 677d157501SCoco Li #define START_ACK 100 687d157501SCoco Li #define SIP6 "fdaa::2" 697d157501SCoco Li #define DIP6 "fdaa::1" 707d157501SCoco Li #define SIP4 "192.168.1.200" 717d157501SCoco Li #define DIP4 "192.168.1.100" 727d157501SCoco Li #define ETH_P_NONE 0 737d157501SCoco Li #define TOTAL_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 747d157501SCoco Li #define MSS (4096 - sizeof(struct tcphdr) - sizeof(struct ipv6hdr)) 757d157501SCoco Li #define MAX_PAYLOAD (IP_MAXPACKET - sizeof(struct tcphdr) - sizeof(struct ipv6hdr)) 767d157501SCoco Li #define NUM_LARGE_PKT (MAX_PAYLOAD / MSS) 777d157501SCoco Li #define MAX_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 787d157501SCoco Li 797d157501SCoco Li static int proto = -1; 807d157501SCoco Li static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN]; 817d157501SCoco Li static char *testname = "data"; 827d157501SCoco Li static char *ifname = "eth0"; 837d157501SCoco Li static char *smac = "aa:00:00:00:00:02"; 847d157501SCoco Li static char *dmac = "aa:00:00:00:00:01"; 857d157501SCoco Li static bool verbose; 867d157501SCoco Li static bool tx_socket = true; 877d157501SCoco Li static int tcp_offset = -1; 887d157501SCoco Li static int total_hdr_len = -1; 897d157501SCoco Li static int ethhdr_proto = -1; 907d157501SCoco Li 917d157501SCoco Li static void vlog(const char *fmt, ...) 927d157501SCoco Li { 937d157501SCoco Li va_list args; 947d157501SCoco Li 957d157501SCoco Li if (verbose) { 967d157501SCoco Li va_start(args, fmt); 977d157501SCoco Li vfprintf(stderr, fmt, args); 987d157501SCoco Li va_end(args); 997d157501SCoco Li } 1007d157501SCoco Li } 1017d157501SCoco Li 1027d157501SCoco Li static void setup_sock_filter(int fd) 1037d157501SCoco Li { 1047d157501SCoco Li const int dport_off = tcp_offset + offsetof(struct tcphdr, dest); 1057d157501SCoco Li const int ethproto_off = offsetof(struct ethhdr, h_proto); 1067d157501SCoco Li int optlen = 0; 1077d157501SCoco Li int ipproto_off; 1087d157501SCoco Li int next_off; 1097d157501SCoco Li 1107d157501SCoco Li if (proto == PF_INET) 1117d157501SCoco Li next_off = offsetof(struct iphdr, protocol); 1127d157501SCoco Li else 1137d157501SCoco Li next_off = offsetof(struct ipv6hdr, nexthdr); 1147d157501SCoco Li ipproto_off = ETH_HLEN + next_off; 1157d157501SCoco Li 1167d157501SCoco Li if (strcmp(testname, "ip") == 0) { 1177d157501SCoco Li if (proto == PF_INET) 1187d157501SCoco Li optlen = sizeof(struct ip_timestamp); 1197d157501SCoco Li else 1207d157501SCoco Li optlen = sizeof(struct ip6_frag); 1217d157501SCoco Li } 1227d157501SCoco Li 1237d157501SCoco Li struct sock_filter filter[] = { 1247d157501SCoco Li BPF_STMT(BPF_LD + BPF_H + BPF_ABS, ethproto_off), 1257d157501SCoco Li BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 7), 1267d157501SCoco Li BPF_STMT(BPF_LD + BPF_B + BPF_ABS, ipproto_off), 1277d157501SCoco Li BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5), 1287d157501SCoco Li BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off), 1297d157501SCoco Li BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0), 1307d157501SCoco Li BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off + optlen), 1317d157501SCoco Li BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1), 1327d157501SCoco Li BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF), 1337d157501SCoco Li BPF_STMT(BPF_RET + BPF_K, 0), 1347d157501SCoco Li }; 1357d157501SCoco Li 1367d157501SCoco Li struct sock_fprog bpf = { 1377d157501SCoco Li .len = ARRAY_SIZE(filter), 1387d157501SCoco Li .filter = filter, 1397d157501SCoco Li }; 1407d157501SCoco Li 1417d157501SCoco Li if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0) 1427d157501SCoco Li error(1, errno, "error setting filter"); 1437d157501SCoco Li } 1447d157501SCoco Li 1457d157501SCoco Li static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum) 1467d157501SCoco Li { 1477d157501SCoco Li uint16_t *words = data; 1487d157501SCoco Li int i; 1497d157501SCoco Li 1507d157501SCoco Li for (i = 0; i < len / 2; i++) 1517d157501SCoco Li sum += words[i]; 1527d157501SCoco Li if (len & 1) 1537d157501SCoco Li sum += ((char *)data)[len - 1]; 1547d157501SCoco Li return sum; 1557d157501SCoco Li } 1567d157501SCoco Li 1577d157501SCoco Li static uint16_t checksum_fold(void *data, size_t len, uint32_t sum) 1587d157501SCoco Li { 1597d157501SCoco Li sum = checksum_nofold(data, len, sum); 1607d157501SCoco Li while (sum > 0xFFFF) 1617d157501SCoco Li sum = (sum & 0xFFFF) + (sum >> 16); 1627d157501SCoco Li return ~sum; 1637d157501SCoco Li } 1647d157501SCoco Li 1657d157501SCoco Li static uint16_t tcp_checksum(void *buf, int payload_len) 1667d157501SCoco Li { 1677d157501SCoco Li struct pseudo_header6 { 1687d157501SCoco Li struct in6_addr saddr; 1697d157501SCoco Li struct in6_addr daddr; 1707d157501SCoco Li uint16_t protocol; 1717d157501SCoco Li uint16_t payload_len; 1727d157501SCoco Li } ph6; 1737d157501SCoco Li struct pseudo_header4 { 1747d157501SCoco Li struct in_addr saddr; 1757d157501SCoco Li struct in_addr daddr; 1767d157501SCoco Li uint16_t protocol; 1777d157501SCoco Li uint16_t payload_len; 1787d157501SCoco Li } ph4; 1797d157501SCoco Li uint32_t sum = 0; 1807d157501SCoco Li 1817d157501SCoco Li if (proto == PF_INET6) { 1827d157501SCoco Li if (inet_pton(AF_INET6, SIP6, &ph6.saddr) != 1) 1837d157501SCoco Li error(1, errno, "inet_pton6 source ip pseudo"); 1847d157501SCoco Li if (inet_pton(AF_INET6, DIP6, &ph6.daddr) != 1) 1857d157501SCoco Li error(1, errno, "inet_pton6 dest ip pseudo"); 1867d157501SCoco Li ph6.protocol = htons(IPPROTO_TCP); 1877d157501SCoco Li ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len); 1887d157501SCoco Li 1897d157501SCoco Li sum = checksum_nofold(&ph6, sizeof(ph6), 0); 1907d157501SCoco Li } else if (proto == PF_INET) { 1917d157501SCoco Li if (inet_pton(AF_INET, SIP4, &ph4.saddr) != 1) 1927d157501SCoco Li error(1, errno, "inet_pton source ip pseudo"); 1937d157501SCoco Li if (inet_pton(AF_INET, DIP4, &ph4.daddr) != 1) 1947d157501SCoco Li error(1, errno, "inet_pton dest ip pseudo"); 1957d157501SCoco Li ph4.protocol = htons(IPPROTO_TCP); 1967d157501SCoco Li ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len); 1977d157501SCoco Li 1987d157501SCoco Li sum = checksum_nofold(&ph4, sizeof(ph4), 0); 1997d157501SCoco Li } 2007d157501SCoco Li 2017d157501SCoco Li return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum); 2027d157501SCoco Li } 2037d157501SCoco Li 2047d157501SCoco Li static void read_MAC(uint8_t *mac_addr, char *mac) 2057d157501SCoco Li { 2067d157501SCoco Li if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 2077d157501SCoco Li &mac_addr[0], &mac_addr[1], &mac_addr[2], 2087d157501SCoco Li &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) 2097d157501SCoco Li error(1, 0, "sscanf"); 2107d157501SCoco Li } 2117d157501SCoco Li 2127d157501SCoco Li static void fill_datalinklayer(void *buf) 2137d157501SCoco Li { 2147d157501SCoco Li struct ethhdr *eth = buf; 2157d157501SCoco Li 2167d157501SCoco Li memcpy(eth->h_dest, dst_mac, ETH_ALEN); 2177d157501SCoco Li memcpy(eth->h_source, src_mac, ETH_ALEN); 2187d157501SCoco Li eth->h_proto = ethhdr_proto; 2197d157501SCoco Li } 2207d157501SCoco Li 2217d157501SCoco Li static void fill_networklayer(void *buf, int payload_len) 2227d157501SCoco Li { 2237d157501SCoco Li struct ipv6hdr *ip6h = buf; 2247d157501SCoco Li struct iphdr *iph = buf; 2257d157501SCoco Li 2267d157501SCoco Li if (proto == PF_INET6) { 2277d157501SCoco Li memset(ip6h, 0, sizeof(*ip6h)); 2287d157501SCoco Li 2297d157501SCoco Li ip6h->version = 6; 2307d157501SCoco Li ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len); 2317d157501SCoco Li ip6h->nexthdr = IPPROTO_TCP; 2327d157501SCoco Li ip6h->hop_limit = 8; 2337d157501SCoco Li if (inet_pton(AF_INET6, SIP6, &ip6h->saddr) != 1) 2347d157501SCoco Li error(1, errno, "inet_pton source ip6"); 2357d157501SCoco Li if (inet_pton(AF_INET6, DIP6, &ip6h->daddr) != 1) 2367d157501SCoco Li error(1, errno, "inet_pton dest ip6"); 2377d157501SCoco Li } else if (proto == PF_INET) { 2387d157501SCoco Li memset(iph, 0, sizeof(*iph)); 2397d157501SCoco Li 2407d157501SCoco Li iph->version = 4; 2417d157501SCoco Li iph->ihl = 5; 2427d157501SCoco Li iph->ttl = 8; 2437d157501SCoco Li iph->protocol = IPPROTO_TCP; 2447d157501SCoco Li iph->tot_len = htons(sizeof(struct tcphdr) + 2457d157501SCoco Li payload_len + sizeof(struct iphdr)); 2467d157501SCoco Li iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */ 2477d157501SCoco Li if (inet_pton(AF_INET, SIP4, &iph->saddr) != 1) 2487d157501SCoco Li error(1, errno, "inet_pton source ip"); 2497d157501SCoco Li if (inet_pton(AF_INET, DIP4, &iph->daddr) != 1) 2507d157501SCoco Li error(1, errno, "inet_pton dest ip"); 2517d157501SCoco Li iph->check = checksum_fold(buf, sizeof(struct iphdr), 0); 2527d157501SCoco Li } 2537d157501SCoco Li } 2547d157501SCoco Li 2557d157501SCoco Li static void fill_transportlayer(void *buf, int seq_offset, int ack_offset, 2567d157501SCoco Li int payload_len, int fin) 2577d157501SCoco Li { 2587d157501SCoco Li struct tcphdr *tcph = buf; 2597d157501SCoco Li 2607d157501SCoco Li memset(tcph, 0, sizeof(*tcph)); 2617d157501SCoco Li 2627d157501SCoco Li tcph->source = htons(SPORT); 2637d157501SCoco Li tcph->dest = htons(DPORT); 2647d157501SCoco Li tcph->seq = ntohl(START_SEQ + seq_offset); 2657d157501SCoco Li tcph->ack_seq = ntohl(START_ACK + ack_offset); 2667d157501SCoco Li tcph->ack = 1; 2677d157501SCoco Li tcph->fin = fin; 2687d157501SCoco Li tcph->doff = 5; 2697d157501SCoco Li tcph->window = htons(TCP_MAXWIN); 2707d157501SCoco Li tcph->urg_ptr = 0; 2717d157501SCoco Li tcph->check = tcp_checksum(tcph, payload_len); 2727d157501SCoco Li } 2737d157501SCoco Li 2747d157501SCoco Li static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr) 2757d157501SCoco Li { 2767d157501SCoco Li int ret = -1; 2777d157501SCoco Li 2787d157501SCoco Li ret = sendto(fd, buf, len, 0, (struct sockaddr *)daddr, sizeof(*daddr)); 2797d157501SCoco Li if (ret == -1) 2807d157501SCoco Li error(1, errno, "sendto failure"); 2817d157501SCoco Li if (ret != len) 2827d157501SCoco Li error(1, errno, "sendto wrong length"); 2837d157501SCoco Li } 2847d157501SCoco Li 2857d157501SCoco Li static void create_packet(void *buf, int seq_offset, int ack_offset, 2867d157501SCoco Li int payload_len, int fin) 2877d157501SCoco Li { 2887d157501SCoco Li memset(buf, 0, total_hdr_len); 2897d157501SCoco Li memset(buf + total_hdr_len, 'a', payload_len); 2907d157501SCoco Li fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset, 2917d157501SCoco Li payload_len, fin); 2927d157501SCoco Li fill_networklayer(buf + ETH_HLEN, payload_len); 2937d157501SCoco Li fill_datalinklayer(buf); 2947d157501SCoco Li } 2957d157501SCoco Li 2967d157501SCoco Li /* send one extra flag, not first and not last pkt */ 2977d157501SCoco Li static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn, 2987d157501SCoco Li int rst, int urg) 2997d157501SCoco Li { 3007d157501SCoco Li static char flag_buf[MAX_HDR_LEN + PAYLOAD_LEN]; 3017d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 3027d157501SCoco Li int payload_len, pkt_size, flag, i; 3037d157501SCoco Li struct tcphdr *tcph; 3047d157501SCoco Li 3057d157501SCoco Li payload_len = PAYLOAD_LEN * psh; 3067d157501SCoco Li pkt_size = total_hdr_len + payload_len; 3077d157501SCoco Li flag = NUM_PACKETS / 2; 3087d157501SCoco Li 3097d157501SCoco Li create_packet(flag_buf, flag * payload_len, 0, payload_len, 0); 3107d157501SCoco Li 3117d157501SCoco Li tcph = (struct tcphdr *)(flag_buf + tcp_offset); 3127d157501SCoco Li tcph->psh = psh; 3137d157501SCoco Li tcph->syn = syn; 3147d157501SCoco Li tcph->rst = rst; 3157d157501SCoco Li tcph->urg = urg; 3167d157501SCoco Li tcph->check = 0; 3177d157501SCoco Li tcph->check = tcp_checksum(tcph, payload_len); 3187d157501SCoco Li 3197d157501SCoco Li for (i = 0; i < NUM_PACKETS + 1; i++) { 3207d157501SCoco Li if (i == flag) { 3217d157501SCoco Li write_packet(fd, flag_buf, pkt_size, daddr); 3227d157501SCoco Li continue; 3237d157501SCoco Li } 3247d157501SCoco Li create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 3257d157501SCoco Li write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 3267d157501SCoco Li } 3277d157501SCoco Li } 3287d157501SCoco Li 3297d157501SCoco Li /* Test for data of same length, smaller than previous 3307d157501SCoco Li * and of different lengths 3317d157501SCoco Li */ 3327d157501SCoco Li static void send_data_pkts(int fd, struct sockaddr_ll *daddr, 3337d157501SCoco Li int payload_len1, int payload_len2) 3347d157501SCoco Li { 3357d157501SCoco Li static char buf[ETH_HLEN + IP_MAXPACKET]; 3367d157501SCoco Li 3377d157501SCoco Li create_packet(buf, 0, 0, payload_len1, 0); 3387d157501SCoco Li write_packet(fd, buf, total_hdr_len + payload_len1, daddr); 3397d157501SCoco Li create_packet(buf, payload_len1, 0, payload_len2, 0); 3407d157501SCoco Li write_packet(fd, buf, total_hdr_len + payload_len2, daddr); 3417d157501SCoco Li } 3427d157501SCoco Li 3437d157501SCoco Li /* If incoming segments make tracked segment length exceed 3447d157501SCoco Li * legal IP datagram length, do not coalesce 3457d157501SCoco Li */ 3467d157501SCoco Li static void send_large(int fd, struct sockaddr_ll *daddr, int remainder) 3477d157501SCoco Li { 3487d157501SCoco Li static char pkts[NUM_LARGE_PKT][TOTAL_HDR_LEN + MSS]; 3497d157501SCoco Li static char last[TOTAL_HDR_LEN + MSS]; 3507d157501SCoco Li static char new_seg[TOTAL_HDR_LEN + MSS]; 3517d157501SCoco Li int i; 3527d157501SCoco Li 3537d157501SCoco Li for (i = 0; i < NUM_LARGE_PKT; i++) 3547d157501SCoco Li create_packet(pkts[i], i * MSS, 0, MSS, 0); 3557d157501SCoco Li create_packet(last, NUM_LARGE_PKT * MSS, 0, remainder, 0); 3567d157501SCoco Li create_packet(new_seg, (NUM_LARGE_PKT + 1) * MSS, 0, remainder, 0); 3577d157501SCoco Li 3587d157501SCoco Li for (i = 0; i < NUM_LARGE_PKT; i++) 3597d157501SCoco Li write_packet(fd, pkts[i], total_hdr_len + MSS, daddr); 3607d157501SCoco Li write_packet(fd, last, total_hdr_len + remainder, daddr); 3617d157501SCoco Li write_packet(fd, new_seg, total_hdr_len + remainder, daddr); 3627d157501SCoco Li } 3637d157501SCoco Li 3647d157501SCoco Li /* Pure acks and dup acks don't coalesce */ 3657d157501SCoco Li static void send_ack(int fd, struct sockaddr_ll *daddr) 3667d157501SCoco Li { 3677d157501SCoco Li static char buf[MAX_HDR_LEN]; 3687d157501SCoco Li 3697d157501SCoco Li create_packet(buf, 0, 0, 0, 0); 3707d157501SCoco Li write_packet(fd, buf, total_hdr_len, daddr); 3717d157501SCoco Li write_packet(fd, buf, total_hdr_len, daddr); 3727d157501SCoco Li create_packet(buf, 0, 1, 0, 0); 3737d157501SCoco Li write_packet(fd, buf, total_hdr_len, daddr); 3747d157501SCoco Li } 3757d157501SCoco Li 3767d157501SCoco Li static void recompute_packet(char *buf, char *no_ext, int extlen) 3777d157501SCoco Li { 3787d157501SCoco Li struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset); 3797d157501SCoco Li struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN); 3807d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 3817d157501SCoco Li 3827d157501SCoco Li memmove(buf, no_ext, total_hdr_len); 3837d157501SCoco Li memmove(buf + total_hdr_len + extlen, 3847d157501SCoco Li no_ext + total_hdr_len, PAYLOAD_LEN); 3857d157501SCoco Li 3867d157501SCoco Li tcphdr->doff = tcphdr->doff + (extlen / 4); 3877d157501SCoco Li tcphdr->check = 0; 3887d157501SCoco Li tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen); 3897d157501SCoco Li if (proto == PF_INET) { 3907d157501SCoco Li iph->tot_len = htons(ntohs(iph->tot_len) + extlen); 3917d157501SCoco Li iph->check = 0; 3927d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 3937d157501SCoco Li } else { 3947d157501SCoco Li ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen); 3957d157501SCoco Li } 3967d157501SCoco Li } 3977d157501SCoco Li 3987d157501SCoco Li static void tcp_write_options(char *buf, int kind, int ts) 3997d157501SCoco Li { 4007d157501SCoco Li struct tcp_option_ts { 4017d157501SCoco Li uint8_t kind; 4027d157501SCoco Li uint8_t len; 4037d157501SCoco Li uint32_t tsval; 4047d157501SCoco Li uint32_t tsecr; 4057d157501SCoco Li } *opt_ts = (void *)buf; 4067d157501SCoco Li struct tcp_option_window { 4077d157501SCoco Li uint8_t kind; 4087d157501SCoco Li uint8_t len; 4097d157501SCoco Li uint8_t shift; 4107d157501SCoco Li } *opt_window = (void *)buf; 4117d157501SCoco Li 4127d157501SCoco Li switch (kind) { 4137d157501SCoco Li case TCPOPT_NOP: 4147d157501SCoco Li buf[0] = TCPOPT_NOP; 4157d157501SCoco Li break; 4167d157501SCoco Li case TCPOPT_WINDOW: 4177d157501SCoco Li memset(opt_window, 0, sizeof(struct tcp_option_window)); 4187d157501SCoco Li opt_window->kind = TCPOPT_WINDOW; 4197d157501SCoco Li opt_window->len = TCPOLEN_WINDOW; 4207d157501SCoco Li opt_window->shift = 0; 4217d157501SCoco Li break; 4227d157501SCoco Li case TCPOPT_TIMESTAMP: 4237d157501SCoco Li memset(opt_ts, 0, sizeof(struct tcp_option_ts)); 4247d157501SCoco Li opt_ts->kind = TCPOPT_TIMESTAMP; 4257d157501SCoco Li opt_ts->len = TCPOLEN_TIMESTAMP; 4267d157501SCoco Li opt_ts->tsval = ts; 4277d157501SCoco Li opt_ts->tsecr = 0; 4287d157501SCoco Li break; 4297d157501SCoco Li default: 4307d157501SCoco Li error(1, 0, "unimplemented TCP option"); 4317d157501SCoco Li break; 4327d157501SCoco Li } 4337d157501SCoco Li } 4347d157501SCoco Li 4357d157501SCoco Li /* TCP with options is always a permutation of {TS, NOP, NOP}. 4367d157501SCoco Li * Implement different orders to verify coalescing stops. 4377d157501SCoco Li */ 4387d157501SCoco Li static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order) 4397d157501SCoco Li { 4407d157501SCoco Li switch (order) { 4417d157501SCoco Li case 0: 4427d157501SCoco Li tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0); 4437d157501SCoco Li tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0); 4447d157501SCoco Li tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */, 4457d157501SCoco Li TCPOPT_TIMESTAMP, ts); 4467d157501SCoco Li break; 4477d157501SCoco Li case 1: 4487d157501SCoco Li tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0); 4497d157501SCoco Li tcp_write_options(buf + total_hdr_len + 1, 4507d157501SCoco Li TCPOPT_TIMESTAMP, ts); 4517d157501SCoco Li tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP, 4527d157501SCoco Li TCPOPT_NOP, 0); 4537d157501SCoco Li break; 4547d157501SCoco Li case 2: 4557d157501SCoco Li tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts); 4567d157501SCoco Li tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1, 4577d157501SCoco Li TCPOPT_NOP, 0); 4587d157501SCoco Li tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2, 4597d157501SCoco Li TCPOPT_NOP, 0); 4607d157501SCoco Li break; 4617d157501SCoco Li default: 4627d157501SCoco Li error(1, 0, "unknown order"); 4637d157501SCoco Li break; 4647d157501SCoco Li } 4657d157501SCoco Li recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA); 4667d157501SCoco Li } 4677d157501SCoco Li 4687d157501SCoco Li /* Packets with invalid checksum don't coalesce. */ 4697d157501SCoco Li static void send_changed_checksum(int fd, struct sockaddr_ll *daddr) 4707d157501SCoco Li { 4717d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 4727d157501SCoco Li struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset); 4737d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 4747d157501SCoco Li 4757d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 4767d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 4777d157501SCoco Li 4787d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 4797d157501SCoco Li tcph->check = tcph->check - 1; 4807d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 4817d157501SCoco Li } 4827d157501SCoco Li 4837d157501SCoco Li /* Packets with non-consecutive sequence number don't coalesce.*/ 4847d157501SCoco Li static void send_changed_seq(int fd, struct sockaddr_ll *daddr) 4857d157501SCoco Li { 4867d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 4877d157501SCoco Li struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset); 4887d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 4897d157501SCoco Li 4907d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 4917d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 4927d157501SCoco Li 4937d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 4947d157501SCoco Li tcph->seq = ntohl(htonl(tcph->seq) + 1); 4957d157501SCoco Li tcph->check = 0; 4967d157501SCoco Li tcph->check = tcp_checksum(tcph, PAYLOAD_LEN); 4977d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 4987d157501SCoco Li } 4997d157501SCoco Li 5007d157501SCoco Li /* Packet with different timestamp option or different timestamps 5017d157501SCoco Li * don't coalesce. 5027d157501SCoco Li */ 5037d157501SCoco Li static void send_changed_ts(int fd, struct sockaddr_ll *daddr) 5047d157501SCoco Li { 5057d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 5067d157501SCoco Li static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA]; 5077d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA; 5087d157501SCoco Li 5097d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 5107d157501SCoco Li add_standard_tcp_options(extpkt, buf, 0, 0); 5117d157501SCoco Li write_packet(fd, extpkt, pkt_size, daddr); 5127d157501SCoco Li 5137d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 5147d157501SCoco Li add_standard_tcp_options(extpkt, buf, 0, 0); 5157d157501SCoco Li write_packet(fd, extpkt, pkt_size, daddr); 5167d157501SCoco Li 5177d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 5187d157501SCoco Li add_standard_tcp_options(extpkt, buf, 100, 0); 5197d157501SCoco Li write_packet(fd, extpkt, pkt_size, daddr); 5207d157501SCoco Li 5217d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0); 5227d157501SCoco Li add_standard_tcp_options(extpkt, buf, 100, 1); 5237d157501SCoco Li write_packet(fd, extpkt, pkt_size, daddr); 5247d157501SCoco Li 5257d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0); 5267d157501SCoco Li add_standard_tcp_options(extpkt, buf, 100, 2); 5277d157501SCoco Li write_packet(fd, extpkt, pkt_size, daddr); 5287d157501SCoco Li } 5297d157501SCoco Li 5307d157501SCoco Li /* Packet with different tcp options don't coalesce. */ 5317d157501SCoco Li static void send_diff_opt(int fd, struct sockaddr_ll *daddr) 5327d157501SCoco Li { 5337d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 5347d157501SCoco Li static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA]; 5357d157501SCoco Li static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG]; 5367d157501SCoco Li int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA; 5377d157501SCoco Li int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG; 5387d157501SCoco Li 5397d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 5407d157501SCoco Li add_standard_tcp_options(extpkt1, buf, 0, 0); 5417d157501SCoco Li write_packet(fd, extpkt1, extpkt1_size, daddr); 5427d157501SCoco Li 5437d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 5447d157501SCoco Li add_standard_tcp_options(extpkt1, buf, 0, 0); 5457d157501SCoco Li write_packet(fd, extpkt1, extpkt1_size, daddr); 5467d157501SCoco Li 5477d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 5487d157501SCoco Li tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0); 5497d157501SCoco Li tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0); 5507d157501SCoco Li recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1); 5517d157501SCoco Li write_packet(fd, extpkt2, extpkt2_size, daddr); 5527d157501SCoco Li } 5537d157501SCoco Li 5547d157501SCoco Li static void add_ipv4_ts_option(void *buf, void *optpkt) 5557d157501SCoco Li { 5567d157501SCoco Li struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset); 5577d157501SCoco Li int optlen = sizeof(struct ip_timestamp); 5587d157501SCoco Li struct iphdr *iph; 5597d157501SCoco Li 5607d157501SCoco Li if (optlen % 4) 5617d157501SCoco Li error(1, 0, "ipv4 timestamp length is not a multiple of 4B"); 5627d157501SCoco Li 5637d157501SCoco Li ts->ipt_code = IPOPT_TS; 5647d157501SCoco Li ts->ipt_len = optlen; 5657d157501SCoco Li ts->ipt_ptr = 5; 5667d157501SCoco Li ts->ipt_flg = IPOPT_TS_TSONLY; 5677d157501SCoco Li 5687d157501SCoco Li memcpy(optpkt, buf, tcp_offset); 5697d157501SCoco Li memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset, 5707d157501SCoco Li sizeof(struct tcphdr) + PAYLOAD_LEN); 5717d157501SCoco Li 5727d157501SCoco Li iph = (struct iphdr *)(optpkt + ETH_HLEN); 5737d157501SCoco Li iph->ihl = 5 + (optlen / 4); 5747d157501SCoco Li iph->tot_len = htons(ntohs(iph->tot_len) + optlen); 5757d157501SCoco Li iph->check = 0; 5767d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0); 5777d157501SCoco Li } 5787d157501SCoco Li 5797d157501SCoco Li /* IPv4 options shouldn't coalesce */ 5807d157501SCoco Li static void send_ip_options(int fd, struct sockaddr_ll *daddr) 5817d157501SCoco Li { 5827d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 5837d157501SCoco Li static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)]; 5847d157501SCoco Li int optlen = sizeof(struct ip_timestamp); 5857d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen; 5867d157501SCoco Li 5877d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 5887d157501SCoco Li write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 5897d157501SCoco Li 5907d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0); 5917d157501SCoco Li add_ipv4_ts_option(buf, optpkt); 5927d157501SCoco Li write_packet(fd, optpkt, pkt_size, daddr); 5937d157501SCoco Li 5947d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 5957d157501SCoco Li write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 5967d157501SCoco Li } 5977d157501SCoco Li 5987d157501SCoco Li /* IPv4 fragments shouldn't coalesce */ 5997d157501SCoco Li static void send_fragment4(int fd, struct sockaddr_ll *daddr) 6007d157501SCoco Li { 6017d157501SCoco Li static char buf[IP_MAXPACKET]; 6027d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 6037d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 6047d157501SCoco Li 6057d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 6067d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6077d157501SCoco Li 6087d157501SCoco Li /* Once fragmented, packet would retain the total_len. 6097d157501SCoco Li * Tcp header is prepared as if rest of data is in follow-up frags, 6107d157501SCoco Li * but follow up frags aren't actually sent. 6117d157501SCoco Li */ 6127d157501SCoco Li memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2); 6137d157501SCoco Li fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0); 6147d157501SCoco Li fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN); 6157d157501SCoco Li fill_datalinklayer(buf); 6167d157501SCoco Li 6177d157501SCoco Li iph->frag_off = htons(0x6000); // DF = 1, MF = 1 6187d157501SCoco Li iph->check = 0; 6197d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 6207d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6217d157501SCoco Li } 6227d157501SCoco Li 6237d157501SCoco Li /* IPv4 packets with different ttl don't coalesce.*/ 6247d157501SCoco Li static void send_changed_ttl(int fd, struct sockaddr_ll *daddr) 6257d157501SCoco Li { 6267d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 6277d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 6287d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 6297d157501SCoco Li 6307d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 6317d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6327d157501SCoco Li 6337d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 6347d157501SCoco Li iph->ttl = 7; 6357d157501SCoco Li iph->check = 0; 6367d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 6377d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6387d157501SCoco Li } 6397d157501SCoco Li 6407d157501SCoco Li /* Packets with different tos don't coalesce.*/ 6417d157501SCoco Li static void send_changed_tos(int fd, struct sockaddr_ll *daddr) 6427d157501SCoco Li { 6437d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 6447d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 6457d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 6467d157501SCoco Li struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN); 6477d157501SCoco Li 6487d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 6497d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6507d157501SCoco Li 6517d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 6527d157501SCoco Li if (proto == PF_INET) { 6537d157501SCoco Li iph->tos = 1; 6547d157501SCoco Li iph->check = 0; 6557d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 6567d157501SCoco Li } else if (proto == PF_INET6) { 6577d157501SCoco Li ip6h->priority = 0xf; 6587d157501SCoco Li } 6597d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6607d157501SCoco Li } 6617d157501SCoco Li 6627d157501SCoco Li /* Packets with different ECN don't coalesce.*/ 6637d157501SCoco Li static void send_changed_ECN(int fd, struct sockaddr_ll *daddr) 6647d157501SCoco Li { 6657d157501SCoco Li int pkt_size = total_hdr_len + PAYLOAD_LEN; 6667d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 6677d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 6687d157501SCoco Li 6697d157501SCoco Li create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 6707d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6717d157501SCoco Li 6727d157501SCoco Li create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 6737d157501SCoco Li if (proto == PF_INET) { 6747d157501SCoco Li buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10 6757d157501SCoco Li iph->check = 0; 6767d157501SCoco Li iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 6777d157501SCoco Li } else { 6787d157501SCoco Li buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10 6797d157501SCoco Li } 6807d157501SCoco Li write_packet(fd, buf, pkt_size, daddr); 6817d157501SCoco Li } 6827d157501SCoco Li 6837d157501SCoco Li /* IPv6 fragments and packets with extensions don't coalesce.*/ 6847d157501SCoco Li static void send_fragment6(int fd, struct sockaddr_ll *daddr) 6857d157501SCoco Li { 6867d157501SCoco Li static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 6877d157501SCoco Li static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN + 6887d157501SCoco Li sizeof(struct ip6_frag)]; 6897d157501SCoco Li struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN); 6907d157501SCoco Li struct ip6_frag *frag = (void *)(extpkt + tcp_offset); 6917d157501SCoco Li int extlen = sizeof(struct ip6_frag); 6927d157501SCoco Li int bufpkt_len = total_hdr_len + PAYLOAD_LEN; 6937d157501SCoco Li int extpkt_len = bufpkt_len + extlen; 6947d157501SCoco Li int i; 6957d157501SCoco Li 6967d157501SCoco Li for (i = 0; i < 2; i++) { 6977d157501SCoco Li create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0); 6987d157501SCoco Li write_packet(fd, buf, bufpkt_len, daddr); 6997d157501SCoco Li } 7007d157501SCoco Li 7017d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 7027d157501SCoco Li memset(extpkt, 0, extpkt_len); 7037d157501SCoco Li 7047d157501SCoco Li ip6h->nexthdr = IPPROTO_FRAGMENT; 7057d157501SCoco Li ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen); 7067d157501SCoco Li frag->ip6f_nxt = IPPROTO_TCP; 7077d157501SCoco Li 7087d157501SCoco Li memcpy(extpkt, buf, tcp_offset); 7097d157501SCoco Li memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset, 7107d157501SCoco Li sizeof(struct tcphdr) + PAYLOAD_LEN); 7117d157501SCoco Li write_packet(fd, extpkt, extpkt_len, daddr); 7127d157501SCoco Li 7137d157501SCoco Li create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0); 7147d157501SCoco Li write_packet(fd, buf, bufpkt_len, daddr); 7157d157501SCoco Li } 7167d157501SCoco Li 7177d157501SCoco Li static void bind_packetsocket(int fd) 7187d157501SCoco Li { 7197d157501SCoco Li struct sockaddr_ll daddr = {}; 7207d157501SCoco Li 7217d157501SCoco Li daddr.sll_family = AF_PACKET; 7227d157501SCoco Li daddr.sll_protocol = ethhdr_proto; 7237d157501SCoco Li daddr.sll_ifindex = if_nametoindex(ifname); 7247d157501SCoco Li if (daddr.sll_ifindex == 0) 7257d157501SCoco Li error(1, errno, "if_nametoindex"); 7267d157501SCoco Li 7277d157501SCoco Li if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0) 7287d157501SCoco Li error(1, errno, "could not bind socket"); 7297d157501SCoco Li } 7307d157501SCoco Li 7317d157501SCoco Li static void set_timeout(int fd) 7327d157501SCoco Li { 7337d157501SCoco Li struct timeval timeout; 7347d157501SCoco Li 7357d157501SCoco Li timeout.tv_sec = 120; 7367d157501SCoco Li timeout.tv_usec = 0; 7377d157501SCoco Li if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, 7387d157501SCoco Li sizeof(timeout)) < 0) 7397d157501SCoco Li error(1, errno, "cannot set timeout, setsockopt failed"); 7407d157501SCoco Li } 7417d157501SCoco Li 7427d157501SCoco Li static void check_recv_pkts(int fd, int *correct_payload, 7437d157501SCoco Li int correct_num_pkts) 7447d157501SCoco Li { 7457d157501SCoco Li static char buffer[IP_MAXPACKET + ETH_HLEN + 1]; 7467d157501SCoco Li struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN); 7477d157501SCoco Li struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN); 7487d157501SCoco Li struct tcphdr *tcph; 7497d157501SCoco Li bool bad_packet = false; 7507d157501SCoco Li int tcp_ext_len = 0; 7517d157501SCoco Li int ip_ext_len = 0; 7527d157501SCoco Li int pkt_size = -1; 7537d157501SCoco Li int data_len = 0; 7547d157501SCoco Li int num_pkt = 0; 7557d157501SCoco Li int i; 7567d157501SCoco Li 7577d157501SCoco Li vlog("Expected {"); 7587d157501SCoco Li for (i = 0; i < correct_num_pkts; i++) 7597d157501SCoco Li vlog("%d ", correct_payload[i]); 7607d157501SCoco Li vlog("}, Total %d packets\nReceived {", correct_num_pkts); 7617d157501SCoco Li 7627d157501SCoco Li while (1) { 7637d157501SCoco Li pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0); 7647d157501SCoco Li if (pkt_size < 0) 7657d157501SCoco Li error(1, errno, "could not receive"); 7667d157501SCoco Li 7677d157501SCoco Li if (iph->version == 4) 7687d157501SCoco Li ip_ext_len = (iph->ihl - 5) * 4; 7697d157501SCoco Li else if (ip6h->version == 6 && ip6h->nexthdr != IPPROTO_TCP) 7707d157501SCoco Li ip_ext_len = sizeof(struct ip6_frag); 7717d157501SCoco Li 7727d157501SCoco Li tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len); 7737d157501SCoco Li 7747d157501SCoco Li if (tcph->fin) 7757d157501SCoco Li break; 7767d157501SCoco Li 7777d157501SCoco Li tcp_ext_len = (tcph->doff - 5) * 4; 7787d157501SCoco Li data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len; 7797d157501SCoco Li /* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3. 7807d157501SCoco Li * Ipv4/tcp packets without at least 6 bytes of data will be padded. 7817d157501SCoco Li * Packet sockets are protocol agnostic, and will not trim the padding. 7827d157501SCoco Li */ 7837d157501SCoco Li if (pkt_size == ETH_ZLEN && iph->version == 4) { 7847d157501SCoco Li data_len = ntohs(iph->tot_len) 7857d157501SCoco Li - sizeof(struct tcphdr) - sizeof(struct iphdr); 7867d157501SCoco Li } 7877d157501SCoco Li vlog("%d ", data_len); 7887d157501SCoco Li if (data_len != correct_payload[num_pkt]) { 7897d157501SCoco Li vlog("[!=%d]", correct_payload[num_pkt]); 7907d157501SCoco Li bad_packet = true; 7917d157501SCoco Li } 7927d157501SCoco Li num_pkt++; 7937d157501SCoco Li } 7947d157501SCoco Li vlog("}, Total %d packets.\n", num_pkt); 7957d157501SCoco Li if (num_pkt != correct_num_pkts) 7967d157501SCoco Li error(1, 0, "incorrect number of packets"); 7977d157501SCoco Li if (bad_packet) 7987d157501SCoco Li error(1, 0, "incorrect packet geometry"); 7997d157501SCoco Li 8007d157501SCoco Li printf("Test succeeded\n\n"); 8017d157501SCoco Li } 8027d157501SCoco Li 8037d157501SCoco Li static void gro_sender(void) 8047d157501SCoco Li { 8057d157501SCoco Li static char fin_pkt[MAX_HDR_LEN]; 8067d157501SCoco Li struct sockaddr_ll daddr = {}; 8077d157501SCoco Li int txfd = -1; 8087d157501SCoco Li 8097d157501SCoco Li txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW); 8107d157501SCoco Li if (txfd < 0) 8117d157501SCoco Li error(1, errno, "socket creation"); 8127d157501SCoco Li 8137d157501SCoco Li memset(&daddr, 0, sizeof(daddr)); 8147d157501SCoco Li daddr.sll_ifindex = if_nametoindex(ifname); 8157d157501SCoco Li if (daddr.sll_ifindex == 0) 8167d157501SCoco Li error(1, errno, "if_nametoindex"); 8177d157501SCoco Li daddr.sll_family = AF_PACKET; 8187d157501SCoco Li memcpy(daddr.sll_addr, dst_mac, ETH_ALEN); 8197d157501SCoco Li daddr.sll_halen = ETH_ALEN; 8207d157501SCoco Li create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1); 8217d157501SCoco Li 8227d157501SCoco Li if (strcmp(testname, "data") == 0) { 8237d157501SCoco Li send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN); 8247d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8257d157501SCoco Li 8267d157501SCoco Li send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2); 8277d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8287d157501SCoco Li 8297d157501SCoco Li send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN); 8307d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8317d157501SCoco Li } else if (strcmp(testname, "ack") == 0) { 8327d157501SCoco Li send_ack(txfd, &daddr); 8337d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8347d157501SCoco Li } else if (strcmp(testname, "flags") == 0) { 8357d157501SCoco Li send_flags(txfd, &daddr, 1, 0, 0, 0); 8367d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8377d157501SCoco Li 8387d157501SCoco Li send_flags(txfd, &daddr, 0, 1, 0, 0); 8397d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8407d157501SCoco Li 8417d157501SCoco Li send_flags(txfd, &daddr, 0, 0, 1, 0); 8427d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8437d157501SCoco Li 8447d157501SCoco Li send_flags(txfd, &daddr, 0, 0, 0, 1); 8457d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8467d157501SCoco Li } else if (strcmp(testname, "tcp") == 0) { 8477d157501SCoco Li send_changed_checksum(txfd, &daddr); 8487d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8497d157501SCoco Li 8507d157501SCoco Li send_changed_seq(txfd, &daddr); 8517d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8527d157501SCoco Li 8537d157501SCoco Li send_changed_ts(txfd, &daddr); 8547d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8557d157501SCoco Li 8567d157501SCoco Li send_diff_opt(txfd, &daddr); 8577d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8587d157501SCoco Li } else if (strcmp(testname, "ip") == 0) { 8597d157501SCoco Li send_changed_ECN(txfd, &daddr); 8607d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8617d157501SCoco Li 8627d157501SCoco Li send_changed_tos(txfd, &daddr); 8637d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8647d157501SCoco Li if (proto == PF_INET) { 8657d157501SCoco Li /* Modified packets may be received out of order. 8667d157501SCoco Li * Sleep function added to enforce test boundaries 8677d157501SCoco Li * so that fin pkts are not received prior to other pkts. 8687d157501SCoco Li */ 8697d157501SCoco Li sleep(1); 8707d157501SCoco Li send_changed_ttl(txfd, &daddr); 8717d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8727d157501SCoco Li 8737d157501SCoco Li sleep(1); 8747d157501SCoco Li send_ip_options(txfd, &daddr); 8757d157501SCoco Li sleep(1); 8767d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8777d157501SCoco Li 8787d157501SCoco Li sleep(1); 8797d157501SCoco Li send_fragment4(txfd, &daddr); 8807d157501SCoco Li sleep(1); 8817d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8827d157501SCoco Li } else if (proto == PF_INET6) { 8837d157501SCoco Li send_fragment6(txfd, &daddr); 8847d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8857d157501SCoco Li } 8867d157501SCoco Li } else if (strcmp(testname, "large") == 0) { 8877d157501SCoco Li /* 20 is the difference between min iphdr size 8887d157501SCoco Li * and min ipv6hdr size. Like MAX_HDR_SIZE, 8897d157501SCoco Li * MAX_PAYLOAD is defined with the larger header of the two. 8907d157501SCoco Li */ 8917d157501SCoco Li int offset = proto == PF_INET ? 20 : 0; 8927d157501SCoco Li int remainder = (MAX_PAYLOAD + offset) % MSS; 8937d157501SCoco Li 8947d157501SCoco Li send_large(txfd, &daddr, remainder); 8957d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8967d157501SCoco Li 8977d157501SCoco Li send_large(txfd, &daddr, remainder + 1); 8987d157501SCoco Li write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 8997d157501SCoco Li } else { 9007d157501SCoco Li error(1, 0, "Unknown testcase"); 9017d157501SCoco Li } 9027d157501SCoco Li 9037d157501SCoco Li if (close(txfd)) 9047d157501SCoco Li error(1, errno, "socket close"); 9057d157501SCoco Li } 9067d157501SCoco Li 9077d157501SCoco Li static void gro_receiver(void) 9087d157501SCoco Li { 9097d157501SCoco Li static int correct_payload[NUM_PACKETS]; 9107d157501SCoco Li int rxfd = -1; 9117d157501SCoco Li 9127d157501SCoco Li rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE)); 9137d157501SCoco Li if (rxfd < 0) 9147d157501SCoco Li error(1, 0, "socket creation"); 9157d157501SCoco Li setup_sock_filter(rxfd); 9167d157501SCoco Li set_timeout(rxfd); 9177d157501SCoco Li bind_packetsocket(rxfd); 9187d157501SCoco Li 9197d157501SCoco Li memset(correct_payload, 0, sizeof(correct_payload)); 9207d157501SCoco Li 9217d157501SCoco Li if (strcmp(testname, "data") == 0) { 9227d157501SCoco Li printf("pure data packet of same size: "); 9237d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 2; 9247d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 1); 9257d157501SCoco Li 9267d157501SCoco Li printf("large data packets followed by a smaller one: "); 9277d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 1.5; 9287d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 1); 9297d157501SCoco Li 9307d157501SCoco Li printf("small data packets followed by a larger one: "); 9317d157501SCoco Li correct_payload[0] = PAYLOAD_LEN / 2; 9327d157501SCoco Li correct_payload[1] = PAYLOAD_LEN; 9337d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9347d157501SCoco Li } else if (strcmp(testname, "ack") == 0) { 9357d157501SCoco Li printf("duplicate ack and pure ack: "); 9367d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 9377d157501SCoco Li } else if (strcmp(testname, "flags") == 0) { 9387d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 3; 9397d157501SCoco Li correct_payload[1] = PAYLOAD_LEN * 2; 9407d157501SCoco Li 9417d157501SCoco Li printf("psh flag ends coalescing: "); 9427d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9437d157501SCoco Li 9447d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 2; 9457d157501SCoco Li correct_payload[1] = 0; 9467d157501SCoco Li correct_payload[2] = PAYLOAD_LEN * 2; 9477d157501SCoco Li printf("syn flag ends coalescing: "); 9487d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 9497d157501SCoco Li 9507d157501SCoco Li printf("rst flag ends coalescing: "); 9517d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 9527d157501SCoco Li 9537d157501SCoco Li printf("urg flag ends coalescing: "); 9547d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 9557d157501SCoco Li } else if (strcmp(testname, "tcp") == 0) { 9567d157501SCoco Li correct_payload[0] = PAYLOAD_LEN; 9577d157501SCoco Li correct_payload[1] = PAYLOAD_LEN; 9587d157501SCoco Li correct_payload[2] = PAYLOAD_LEN; 9597d157501SCoco Li correct_payload[3] = PAYLOAD_LEN; 9607d157501SCoco Li 9617d157501SCoco Li printf("changed checksum does not coalesce: "); 9627d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9637d157501SCoco Li 9647d157501SCoco Li printf("Wrong Seq number doesn't coalesce: "); 9657d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9667d157501SCoco Li 9677d157501SCoco Li printf("Different timestamp doesn't coalesce: "); 9687d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 2; 9697d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 4); 9707d157501SCoco Li 9717d157501SCoco Li printf("Different options doesn't coalesce: "); 9727d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 2; 9737d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9747d157501SCoco Li } else if (strcmp(testname, "ip") == 0) { 9757d157501SCoco Li correct_payload[0] = PAYLOAD_LEN; 9767d157501SCoco Li correct_payload[1] = PAYLOAD_LEN; 9777d157501SCoco Li 9787d157501SCoco Li printf("different ECN doesn't coalesce: "); 9797d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9807d157501SCoco Li 9817d157501SCoco Li printf("different tos doesn't coalesce: "); 9827d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9837d157501SCoco Li 9847d157501SCoco Li if (proto == PF_INET) { 9857d157501SCoco Li printf("different ttl doesn't coalesce: "); 9867d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9877d157501SCoco Li 9887d157501SCoco Li printf("ip options doesn't coalesce: "); 9897d157501SCoco Li correct_payload[2] = PAYLOAD_LEN; 9907d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 9917d157501SCoco Li 9927d157501SCoco Li printf("fragmented ip4 doesn't coalesce: "); 9937d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 9947d157501SCoco Li } else if (proto == PF_INET6) { 9957d157501SCoco Li /* GRO doesn't check for ipv6 hop limit when flushing. 9967d157501SCoco Li * Hence no corresponding test to the ipv4 case. 9977d157501SCoco Li */ 9987d157501SCoco Li printf("fragmented ip6 doesn't coalesce: "); 9997d157501SCoco Li correct_payload[0] = PAYLOAD_LEN * 2; 10007d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 10017d157501SCoco Li } 10027d157501SCoco Li } else if (strcmp(testname, "large") == 0) { 10037d157501SCoco Li int offset = proto == PF_INET ? 20 : 0; 10047d157501SCoco Li int remainder = (MAX_PAYLOAD + offset) % MSS; 10057d157501SCoco Li 10067d157501SCoco Li correct_payload[0] = (MAX_PAYLOAD + offset); 10077d157501SCoco Li correct_payload[1] = remainder; 10087d157501SCoco Li printf("Shouldn't coalesce if exceed IP max pkt size: "); 10097d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 2); 10107d157501SCoco Li 10117d157501SCoco Li /* last segment sent individually, doesn't start new segment */ 10127d157501SCoco Li correct_payload[0] = correct_payload[0] - remainder; 10137d157501SCoco Li correct_payload[1] = remainder + 1; 10147d157501SCoco Li correct_payload[2] = remainder + 1; 10157d157501SCoco Li check_recv_pkts(rxfd, correct_payload, 3); 10167d157501SCoco Li } else { 10177d157501SCoco Li error(1, 0, "Test case error, should never trigger"); 10187d157501SCoco Li } 10197d157501SCoco Li 10207d157501SCoco Li if (close(rxfd)) 10217d157501SCoco Li error(1, 0, "socket close"); 10227d157501SCoco Li } 10237d157501SCoco Li 10247d157501SCoco Li static void parse_args(int argc, char **argv) 10257d157501SCoco Li { 10267d157501SCoco Li static const struct option opts[] = { 10277d157501SCoco Li { "dmac", required_argument, NULL, 'D' }, 10287d157501SCoco Li { "iface", required_argument, NULL, 'i' }, 10297d157501SCoco Li { "ipv4", no_argument, NULL, '4' }, 10307d157501SCoco Li { "ipv6", no_argument, NULL, '6' }, 10317d157501SCoco Li { "rx", no_argument, NULL, 'r' }, 10327d157501SCoco Li { "smac", required_argument, NULL, 'S' }, 10337d157501SCoco Li { "test", required_argument, NULL, 't' }, 10347d157501SCoco Li { "verbose", no_argument, NULL, 'v' }, 10357d157501SCoco Li { 0, 0, 0, 0 } 10367d157501SCoco Li }; 10377d157501SCoco Li int c; 10387d157501SCoco Li 10397d157501SCoco Li while ((c = getopt_long(argc, argv, "46D:i:rS:t:v", opts, NULL)) != -1) { 10407d157501SCoco Li switch (c) { 10417d157501SCoco Li case '4': 10427d157501SCoco Li proto = PF_INET; 10437d157501SCoco Li ethhdr_proto = htons(ETH_P_IP); 10447d157501SCoco Li break; 10457d157501SCoco Li case '6': 10467d157501SCoco Li proto = PF_INET6; 10477d157501SCoco Li ethhdr_proto = htons(ETH_P_IPV6); 10487d157501SCoco Li break; 10497d157501SCoco Li case 'D': 10507d157501SCoco Li dmac = optarg; 10517d157501SCoco Li break; 10527d157501SCoco Li case 'i': 10537d157501SCoco Li ifname = optarg; 10547d157501SCoco Li break; 10557d157501SCoco Li case 'r': 10567d157501SCoco Li tx_socket = false; 10577d157501SCoco Li break; 10587d157501SCoco Li case 'S': 10597d157501SCoco Li smac = optarg; 10607d157501SCoco Li break; 10617d157501SCoco Li case 't': 10627d157501SCoco Li testname = optarg; 10637d157501SCoco Li break; 10647d157501SCoco Li case 'v': 10657d157501SCoco Li verbose = true; 10667d157501SCoco Li break; 10677d157501SCoco Li default: 10687d157501SCoco Li error(1, 0, "%s invalid option %c\n", __func__, c); 10697d157501SCoco Li break; 10707d157501SCoco Li } 10717d157501SCoco Li } 10727d157501SCoco Li } 10737d157501SCoco Li 10747d157501SCoco Li int main(int argc, char **argv) 10757d157501SCoco Li { 10767d157501SCoco Li parse_args(argc, argv); 10777d157501SCoco Li 10787d157501SCoco Li if (proto == PF_INET) { 10797d157501SCoco Li tcp_offset = ETH_HLEN + sizeof(struct iphdr); 10807d157501SCoco Li total_hdr_len = tcp_offset + sizeof(struct tcphdr); 10817d157501SCoco Li } else if (proto == PF_INET6) { 10827d157501SCoco Li tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr); 10837d157501SCoco Li total_hdr_len = MAX_HDR_LEN; 10847d157501SCoco Li } else { 10857d157501SCoco Li error(1, 0, "Protocol family is not ipv4 or ipv6"); 10867d157501SCoco Li } 10877d157501SCoco Li 10887d157501SCoco Li read_MAC(src_mac, smac); 10897d157501SCoco Li read_MAC(dst_mac, dmac); 10907d157501SCoco Li 10917d157501SCoco Li if (tx_socket) 10927d157501SCoco Li gro_sender(); 10937d157501SCoco Li else 10947d157501SCoco Li gro_receiver(); 10957d157501SCoco Li return 0; 10967d157501SCoco Li } 1097