18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#define _GNU_SOURCE 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <arpa/inet.h> 68c2ecf20Sopenharmony_ci#include <errno.h> 78c2ecf20Sopenharmony_ci#include <error.h> 88c2ecf20Sopenharmony_ci#include <linux/in.h> 98c2ecf20Sopenharmony_ci#include <netinet/ip.h> 108c2ecf20Sopenharmony_ci#include <netinet/ip6.h> 118c2ecf20Sopenharmony_ci#include <netinet/udp.h> 128c2ecf20Sopenharmony_ci#include <stdbool.h> 138c2ecf20Sopenharmony_ci#include <stdio.h> 148c2ecf20Sopenharmony_ci#include <stdlib.h> 158c2ecf20Sopenharmony_ci#include <string.h> 168c2ecf20Sopenharmony_ci#include <time.h> 178c2ecf20Sopenharmony_ci#include <unistd.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic bool cfg_do_ipv4; 208c2ecf20Sopenharmony_cistatic bool cfg_do_ipv6; 218c2ecf20Sopenharmony_cistatic bool cfg_verbose; 228c2ecf20Sopenharmony_cistatic bool cfg_overlap; 238c2ecf20Sopenharmony_cistatic bool cfg_permissive; 248c2ecf20Sopenharmony_cistatic unsigned short cfg_port = 9000; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciconst struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) }; 278c2ecf20Sopenharmony_ciconst struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define IP4_HLEN (sizeof(struct iphdr)) 308c2ecf20Sopenharmony_ci#define IP6_HLEN (sizeof(struct ip6_hdr)) 318c2ecf20Sopenharmony_ci#define UDP_HLEN (sizeof(struct udphdr)) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* IPv6 fragment header lenth. */ 348c2ecf20Sopenharmony_ci#define FRAG_HLEN 8 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic int payload_len; 378c2ecf20Sopenharmony_cistatic int max_frag_len; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define MSG_LEN_MAX 10000 /* Max UDP payload length. */ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define IP4_MF (1u << 13) /* IPv4 MF flag. */ 428c2ecf20Sopenharmony_ci#define IP6_MF (1) /* IPv6 MF flag. */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#define CSUM_MANGLED_0 (0xffff) 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic uint8_t udp_payload[MSG_LEN_MAX]; 478c2ecf20Sopenharmony_cistatic uint8_t ip_frame[IP_MAXPACKET]; 488c2ecf20Sopenharmony_cistatic uint32_t ip_id = 0xabcd; 498c2ecf20Sopenharmony_cistatic int msg_counter; 508c2ecf20Sopenharmony_cistatic int frag_counter; 518c2ecf20Sopenharmony_cistatic unsigned int seed; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* Receive a UDP packet. Validate it matches udp_payload. */ 548c2ecf20Sopenharmony_cistatic void recv_validate_udp(int fd_udp) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci ssize_t ret; 578c2ecf20Sopenharmony_ci static uint8_t recv_buff[MSG_LEN_MAX]; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci ret = recv(fd_udp, recv_buff, payload_len, 0); 608c2ecf20Sopenharmony_ci msg_counter++; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (cfg_overlap) { 638c2ecf20Sopenharmony_ci if (ret == -1 && (errno == ETIMEDOUT || errno == EAGAIN)) 648c2ecf20Sopenharmony_ci return; /* OK */ 658c2ecf20Sopenharmony_ci if (!cfg_permissive) { 668c2ecf20Sopenharmony_ci if (ret != -1) 678c2ecf20Sopenharmony_ci error(1, 0, "recv: expected timeout; got %d", 688c2ecf20Sopenharmony_ci (int)ret); 698c2ecf20Sopenharmony_ci error(1, errno, "recv: expected timeout: %d", errno); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (ret == -1) 748c2ecf20Sopenharmony_ci error(1, errno, "recv: payload_len = %d max_frag_len = %d", 758c2ecf20Sopenharmony_ci payload_len, max_frag_len); 768c2ecf20Sopenharmony_ci if (ret != payload_len) 778c2ecf20Sopenharmony_ci error(1, 0, "recv: wrong size: %d vs %d", (int)ret, payload_len); 788c2ecf20Sopenharmony_ci if (memcmp(udp_payload, recv_buff, payload_len)) 798c2ecf20Sopenharmony_ci error(1, 0, "recv: wrong data"); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic uint32_t raw_checksum(uint8_t *buf, int len, uint32_t sum) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci int i; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci for (i = 0; i < (len & ~1U); i += 2) { 878c2ecf20Sopenharmony_ci sum += (u_int16_t)ntohs(*((u_int16_t *)(buf + i))); 888c2ecf20Sopenharmony_ci if (sum > 0xffff) 898c2ecf20Sopenharmony_ci sum -= 0xffff; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (i < len) { 938c2ecf20Sopenharmony_ci sum += buf[i] << 8; 948c2ecf20Sopenharmony_ci if (sum > 0xffff) 958c2ecf20Sopenharmony_ci sum -= 0xffff; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci return sum; 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic uint16_t udp_checksum(struct ip *iphdr, struct udphdr *udphdr) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci uint32_t sum = 0; 1048c2ecf20Sopenharmony_ci uint16_t res; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)&iphdr->ip_src, 2 * sizeof(iphdr->ip_src), 1078c2ecf20Sopenharmony_ci IPPROTO_UDP + (uint32_t)(UDP_HLEN + payload_len)); 1088c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)udphdr, UDP_HLEN, sum); 1098c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)udp_payload, payload_len, sum); 1108c2ecf20Sopenharmony_ci res = 0xffff & ~sum; 1118c2ecf20Sopenharmony_ci if (res) 1128c2ecf20Sopenharmony_ci return htons(res); 1138c2ecf20Sopenharmony_ci else 1148c2ecf20Sopenharmony_ci return CSUM_MANGLED_0; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic uint16_t udp6_checksum(struct ip6_hdr *iphdr, struct udphdr *udphdr) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci uint32_t sum = 0; 1208c2ecf20Sopenharmony_ci uint16_t res; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)&iphdr->ip6_src, 2 * sizeof(iphdr->ip6_src), 1238c2ecf20Sopenharmony_ci IPPROTO_UDP); 1248c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)&udphdr->len, sizeof(udphdr->len), sum); 1258c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)udphdr, UDP_HLEN, sum); 1268c2ecf20Sopenharmony_ci sum = raw_checksum((uint8_t *)udp_payload, payload_len, sum); 1278c2ecf20Sopenharmony_ci res = 0xffff & ~sum; 1288c2ecf20Sopenharmony_ci if (res) 1298c2ecf20Sopenharmony_ci return htons(res); 1308c2ecf20Sopenharmony_ci else 1318c2ecf20Sopenharmony_ci return CSUM_MANGLED_0; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic void send_fragment(int fd_raw, struct sockaddr *addr, socklen_t alen, 1358c2ecf20Sopenharmony_ci int offset, bool ipv6) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci int frag_len; 1388c2ecf20Sopenharmony_ci int res; 1398c2ecf20Sopenharmony_ci int payload_offset = offset > 0 ? offset - UDP_HLEN : 0; 1408c2ecf20Sopenharmony_ci uint8_t *frag_start = ipv6 ? ip_frame + IP6_HLEN + FRAG_HLEN : 1418c2ecf20Sopenharmony_ci ip_frame + IP4_HLEN; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (offset == 0) { 1448c2ecf20Sopenharmony_ci struct udphdr udphdr; 1458c2ecf20Sopenharmony_ci udphdr.source = htons(cfg_port + 1); 1468c2ecf20Sopenharmony_ci udphdr.dest = htons(cfg_port); 1478c2ecf20Sopenharmony_ci udphdr.len = htons(UDP_HLEN + payload_len); 1488c2ecf20Sopenharmony_ci udphdr.check = 0; 1498c2ecf20Sopenharmony_ci if (ipv6) 1508c2ecf20Sopenharmony_ci udphdr.check = udp6_checksum((struct ip6_hdr *)ip_frame, &udphdr); 1518c2ecf20Sopenharmony_ci else 1528c2ecf20Sopenharmony_ci udphdr.check = udp_checksum((struct ip *)ip_frame, &udphdr); 1538c2ecf20Sopenharmony_ci memcpy(frag_start, &udphdr, UDP_HLEN); 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci if (ipv6) { 1578c2ecf20Sopenharmony_ci struct ip6_hdr *ip6hdr = (struct ip6_hdr *)ip_frame; 1588c2ecf20Sopenharmony_ci struct ip6_frag *fraghdr = (struct ip6_frag *)(ip_frame + IP6_HLEN); 1598c2ecf20Sopenharmony_ci if (payload_len - payload_offset <= max_frag_len && offset > 0) { 1608c2ecf20Sopenharmony_ci /* This is the last fragment. */ 1618c2ecf20Sopenharmony_ci frag_len = FRAG_HLEN + payload_len - payload_offset; 1628c2ecf20Sopenharmony_ci fraghdr->ip6f_offlg = htons(offset); 1638c2ecf20Sopenharmony_ci } else { 1648c2ecf20Sopenharmony_ci frag_len = FRAG_HLEN + max_frag_len; 1658c2ecf20Sopenharmony_ci fraghdr->ip6f_offlg = htons(offset | IP6_MF); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci ip6hdr->ip6_plen = htons(frag_len); 1688c2ecf20Sopenharmony_ci if (offset == 0) 1698c2ecf20Sopenharmony_ci memcpy(frag_start + UDP_HLEN, udp_payload, 1708c2ecf20Sopenharmony_ci frag_len - FRAG_HLEN - UDP_HLEN); 1718c2ecf20Sopenharmony_ci else 1728c2ecf20Sopenharmony_ci memcpy(frag_start, udp_payload + payload_offset, 1738c2ecf20Sopenharmony_ci frag_len - FRAG_HLEN); 1748c2ecf20Sopenharmony_ci frag_len += IP6_HLEN; 1758c2ecf20Sopenharmony_ci } else { 1768c2ecf20Sopenharmony_ci struct ip *iphdr = (struct ip *)ip_frame; 1778c2ecf20Sopenharmony_ci if (payload_len - payload_offset <= max_frag_len && offset > 0) { 1788c2ecf20Sopenharmony_ci /* This is the last fragment. */ 1798c2ecf20Sopenharmony_ci frag_len = IP4_HLEN + payload_len - payload_offset; 1808c2ecf20Sopenharmony_ci iphdr->ip_off = htons(offset / 8); 1818c2ecf20Sopenharmony_ci } else { 1828c2ecf20Sopenharmony_ci frag_len = IP4_HLEN + max_frag_len; 1838c2ecf20Sopenharmony_ci iphdr->ip_off = htons(offset / 8 | IP4_MF); 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci iphdr->ip_len = htons(frag_len); 1868c2ecf20Sopenharmony_ci if (offset == 0) 1878c2ecf20Sopenharmony_ci memcpy(frag_start + UDP_HLEN, udp_payload, 1888c2ecf20Sopenharmony_ci frag_len - IP4_HLEN - UDP_HLEN); 1898c2ecf20Sopenharmony_ci else 1908c2ecf20Sopenharmony_ci memcpy(frag_start, udp_payload + payload_offset, 1918c2ecf20Sopenharmony_ci frag_len - IP4_HLEN); 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci res = sendto(fd_raw, ip_frame, frag_len, 0, addr, alen); 1958c2ecf20Sopenharmony_ci if (res < 0 && errno != EPERM) 1968c2ecf20Sopenharmony_ci error(1, errno, "send_fragment"); 1978c2ecf20Sopenharmony_ci if (res >= 0 && res != frag_len) 1988c2ecf20Sopenharmony_ci error(1, 0, "send_fragment: %d vs %d", res, frag_len); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci frag_counter++; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic void send_udp_frags(int fd_raw, struct sockaddr *addr, 2048c2ecf20Sopenharmony_ci socklen_t alen, bool ipv6) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci struct ip *iphdr = (struct ip *)ip_frame; 2078c2ecf20Sopenharmony_ci struct ip6_hdr *ip6hdr = (struct ip6_hdr *)ip_frame; 2088c2ecf20Sopenharmony_ci int res; 2098c2ecf20Sopenharmony_ci int offset; 2108c2ecf20Sopenharmony_ci int frag_len; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* Send the UDP datagram using raw IP fragments: the 0th fragment 2138c2ecf20Sopenharmony_ci * has the UDP header; other fragments are pieces of udp_payload 2148c2ecf20Sopenharmony_ci * split in chunks of frag_len size. 2158c2ecf20Sopenharmony_ci * 2168c2ecf20Sopenharmony_ci * Odd fragments (1st, 3rd, 5th, etc.) are sent out first, then 2178c2ecf20Sopenharmony_ci * even fragments (0th, 2nd, etc.) are sent out. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_ci if (ipv6) { 2208c2ecf20Sopenharmony_ci struct ip6_frag *fraghdr = (struct ip6_frag *)(ip_frame + IP6_HLEN); 2218c2ecf20Sopenharmony_ci ((struct sockaddr_in6 *)addr)->sin6_port = 0; 2228c2ecf20Sopenharmony_ci memset(ip6hdr, 0, sizeof(*ip6hdr)); 2238c2ecf20Sopenharmony_ci ip6hdr->ip6_flow = htonl(6<<28); /* Version. */ 2248c2ecf20Sopenharmony_ci ip6hdr->ip6_nxt = IPPROTO_FRAGMENT; 2258c2ecf20Sopenharmony_ci ip6hdr->ip6_hops = 255; 2268c2ecf20Sopenharmony_ci ip6hdr->ip6_src = addr6; 2278c2ecf20Sopenharmony_ci ip6hdr->ip6_dst = addr6; 2288c2ecf20Sopenharmony_ci fraghdr->ip6f_nxt = IPPROTO_UDP; 2298c2ecf20Sopenharmony_ci fraghdr->ip6f_reserved = 0; 2308c2ecf20Sopenharmony_ci fraghdr->ip6f_ident = htonl(ip_id++); 2318c2ecf20Sopenharmony_ci } else { 2328c2ecf20Sopenharmony_ci memset(iphdr, 0, sizeof(*iphdr)); 2338c2ecf20Sopenharmony_ci iphdr->ip_hl = 5; 2348c2ecf20Sopenharmony_ci iphdr->ip_v = 4; 2358c2ecf20Sopenharmony_ci iphdr->ip_tos = 0; 2368c2ecf20Sopenharmony_ci iphdr->ip_id = htons(ip_id++); 2378c2ecf20Sopenharmony_ci iphdr->ip_ttl = 0x40; 2388c2ecf20Sopenharmony_ci iphdr->ip_p = IPPROTO_UDP; 2398c2ecf20Sopenharmony_ci iphdr->ip_src.s_addr = htonl(INADDR_LOOPBACK); 2408c2ecf20Sopenharmony_ci iphdr->ip_dst = addr4; 2418c2ecf20Sopenharmony_ci iphdr->ip_sum = 0; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* Occasionally test in-order fragments. */ 2458c2ecf20Sopenharmony_ci if (!cfg_overlap && (rand() % 100 < 15)) { 2468c2ecf20Sopenharmony_ci offset = 0; 2478c2ecf20Sopenharmony_ci while (offset < (UDP_HLEN + payload_len)) { 2488c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2498c2ecf20Sopenharmony_ci offset += max_frag_len; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci return; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci /* Occasionally test IPv4 "runs" (see net/ipv4/ip_fragment.c) */ 2558c2ecf20Sopenharmony_ci if (!cfg_overlap && (rand() % 100 < 20) && 2568c2ecf20Sopenharmony_ci (payload_len > 9 * max_frag_len)) { 2578c2ecf20Sopenharmony_ci offset = 6 * max_frag_len; 2588c2ecf20Sopenharmony_ci while (offset < (UDP_HLEN + payload_len)) { 2598c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2608c2ecf20Sopenharmony_ci offset += max_frag_len; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci offset = 3 * max_frag_len; 2638c2ecf20Sopenharmony_ci while (offset < 6 * max_frag_len) { 2648c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2658c2ecf20Sopenharmony_ci offset += max_frag_len; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci offset = 0; 2688c2ecf20Sopenharmony_ci while (offset < 3 * max_frag_len) { 2698c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2708c2ecf20Sopenharmony_ci offset += max_frag_len; 2718c2ecf20Sopenharmony_ci } 2728c2ecf20Sopenharmony_ci return; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* Odd fragments. */ 2768c2ecf20Sopenharmony_ci offset = max_frag_len; 2778c2ecf20Sopenharmony_ci while (offset < (UDP_HLEN + payload_len)) { 2788c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2798c2ecf20Sopenharmony_ci /* IPv4 ignores duplicates, so randomly send a duplicate. */ 2808c2ecf20Sopenharmony_ci if (rand() % 100 == 1) 2818c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 2828c2ecf20Sopenharmony_ci offset += 2 * max_frag_len; 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci if (cfg_overlap) { 2868c2ecf20Sopenharmony_ci /* Send an extra random fragment. 2878c2ecf20Sopenharmony_ci * 2888c2ecf20Sopenharmony_ci * Duplicates and some fragments completely inside 2898c2ecf20Sopenharmony_ci * previously sent fragments are dropped/ignored. So 2908c2ecf20Sopenharmony_ci * random offset and frag_len can result in a dropped 2918c2ecf20Sopenharmony_ci * fragment instead of a dropped queue/packet. Thus we 2928c2ecf20Sopenharmony_ci * hard-code offset and frag_len. 2938c2ecf20Sopenharmony_ci */ 2948c2ecf20Sopenharmony_ci if (max_frag_len * 4 < payload_len || max_frag_len < 16) { 2958c2ecf20Sopenharmony_ci /* not enough payload for random offset and frag_len. */ 2968c2ecf20Sopenharmony_ci offset = 8; 2978c2ecf20Sopenharmony_ci frag_len = UDP_HLEN + max_frag_len; 2988c2ecf20Sopenharmony_ci } else { 2998c2ecf20Sopenharmony_ci offset = rand() % (payload_len / 2); 3008c2ecf20Sopenharmony_ci frag_len = 2 * max_frag_len + 1 + rand() % 256; 3018c2ecf20Sopenharmony_ci } 3028c2ecf20Sopenharmony_ci if (ipv6) { 3038c2ecf20Sopenharmony_ci struct ip6_frag *fraghdr = (struct ip6_frag *)(ip_frame + IP6_HLEN); 3048c2ecf20Sopenharmony_ci /* sendto() returns EINVAL if offset + frag_len is too small. */ 3058c2ecf20Sopenharmony_ci /* In IPv6 if !!(frag_len % 8), the fragment is dropped. */ 3068c2ecf20Sopenharmony_ci frag_len &= ~0x7; 3078c2ecf20Sopenharmony_ci fraghdr->ip6f_offlg = htons(offset / 8 | IP6_MF); 3088c2ecf20Sopenharmony_ci ip6hdr->ip6_plen = htons(frag_len); 3098c2ecf20Sopenharmony_ci frag_len += IP6_HLEN; 3108c2ecf20Sopenharmony_ci } else { 3118c2ecf20Sopenharmony_ci frag_len += IP4_HLEN; 3128c2ecf20Sopenharmony_ci iphdr->ip_off = htons(offset / 8 | IP4_MF); 3138c2ecf20Sopenharmony_ci iphdr->ip_len = htons(frag_len); 3148c2ecf20Sopenharmony_ci } 3158c2ecf20Sopenharmony_ci res = sendto(fd_raw, ip_frame, frag_len, 0, addr, alen); 3168c2ecf20Sopenharmony_ci if (res < 0 && errno != EPERM) 3178c2ecf20Sopenharmony_ci error(1, errno, "sendto overlap: %d", frag_len); 3188c2ecf20Sopenharmony_ci if (res >= 0 && res != frag_len) 3198c2ecf20Sopenharmony_ci error(1, 0, "sendto overlap: %d vs %d", (int)res, frag_len); 3208c2ecf20Sopenharmony_ci frag_counter++; 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* Event fragments. */ 3248c2ecf20Sopenharmony_ci offset = 0; 3258c2ecf20Sopenharmony_ci while (offset < (UDP_HLEN + payload_len)) { 3268c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 3278c2ecf20Sopenharmony_ci /* IPv4 ignores duplicates, so randomly send a duplicate. */ 3288c2ecf20Sopenharmony_ci if (rand() % 100 == 1) 3298c2ecf20Sopenharmony_ci send_fragment(fd_raw, addr, alen, offset, ipv6); 3308c2ecf20Sopenharmony_ci offset += 2 * max_frag_len; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_cistatic void run_test(struct sockaddr *addr, socklen_t alen, bool ipv6) 3358c2ecf20Sopenharmony_ci{ 3368c2ecf20Sopenharmony_ci int fd_tx_raw, fd_rx_udp; 3378c2ecf20Sopenharmony_ci /* Frag queue timeout is set to one second in the calling script; 3388c2ecf20Sopenharmony_ci * socket timeout should be just a bit longer to avoid tests interfering 3398c2ecf20Sopenharmony_ci * with each other. 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci struct timeval tv = { .tv_sec = 1, .tv_usec = 10 }; 3428c2ecf20Sopenharmony_ci int idx; 3438c2ecf20Sopenharmony_ci int min_frag_len = 8; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci /* Initialize the payload. */ 3468c2ecf20Sopenharmony_ci for (idx = 0; idx < MSG_LEN_MAX; ++idx) 3478c2ecf20Sopenharmony_ci udp_payload[idx] = idx % 256; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci /* Open sockets. */ 3508c2ecf20Sopenharmony_ci fd_tx_raw = socket(addr->sa_family, SOCK_RAW, IPPROTO_RAW); 3518c2ecf20Sopenharmony_ci if (fd_tx_raw == -1) 3528c2ecf20Sopenharmony_ci error(1, errno, "socket tx_raw"); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci fd_rx_udp = socket(addr->sa_family, SOCK_DGRAM, 0); 3558c2ecf20Sopenharmony_ci if (fd_rx_udp == -1) 3568c2ecf20Sopenharmony_ci error(1, errno, "socket rx_udp"); 3578c2ecf20Sopenharmony_ci if (bind(fd_rx_udp, addr, alen)) 3588c2ecf20Sopenharmony_ci error(1, errno, "bind"); 3598c2ecf20Sopenharmony_ci /* Fail fast. */ 3608c2ecf20Sopenharmony_ci if (setsockopt(fd_rx_udp, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))) 3618c2ecf20Sopenharmony_ci error(1, errno, "setsockopt rcv timeout"); 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci for (payload_len = min_frag_len; payload_len < MSG_LEN_MAX; 3648c2ecf20Sopenharmony_ci payload_len += (rand() % 4096)) { 3658c2ecf20Sopenharmony_ci if (cfg_verbose) 3668c2ecf20Sopenharmony_ci printf("payload_len: %d\n", payload_len); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci if (cfg_overlap) { 3698c2ecf20Sopenharmony_ci /* With overlaps, one send/receive pair below takes 3708c2ecf20Sopenharmony_ci * at least one second (== timeout) to run, so there 3718c2ecf20Sopenharmony_ci * is not enough test time to run a nested loop: 3728c2ecf20Sopenharmony_ci * the full overlap test takes 20-30 seconds. 3738c2ecf20Sopenharmony_ci */ 3748c2ecf20Sopenharmony_ci max_frag_len = min_frag_len + 3758c2ecf20Sopenharmony_ci rand() % (1500 - FRAG_HLEN - min_frag_len); 3768c2ecf20Sopenharmony_ci send_udp_frags(fd_tx_raw, addr, alen, ipv6); 3778c2ecf20Sopenharmony_ci recv_validate_udp(fd_rx_udp); 3788c2ecf20Sopenharmony_ci } else { 3798c2ecf20Sopenharmony_ci /* Without overlaps, each packet reassembly (== one 3808c2ecf20Sopenharmony_ci * send/receive pair below) takes very little time to 3818c2ecf20Sopenharmony_ci * run, so we can easily afford more thourough testing 3828c2ecf20Sopenharmony_ci * with a nested loop: the full non-overlap test takes 3838c2ecf20Sopenharmony_ci * less than one second). 3848c2ecf20Sopenharmony_ci */ 3858c2ecf20Sopenharmony_ci max_frag_len = min_frag_len; 3868c2ecf20Sopenharmony_ci do { 3878c2ecf20Sopenharmony_ci send_udp_frags(fd_tx_raw, addr, alen, ipv6); 3888c2ecf20Sopenharmony_ci recv_validate_udp(fd_rx_udp); 3898c2ecf20Sopenharmony_ci max_frag_len += 8 * (rand() % 8); 3908c2ecf20Sopenharmony_ci } while (max_frag_len < (1500 - FRAG_HLEN) && 3918c2ecf20Sopenharmony_ci max_frag_len <= payload_len); 3928c2ecf20Sopenharmony_ci } 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci /* Cleanup. */ 3968c2ecf20Sopenharmony_ci if (close(fd_tx_raw)) 3978c2ecf20Sopenharmony_ci error(1, errno, "close tx_raw"); 3988c2ecf20Sopenharmony_ci if (close(fd_rx_udp)) 3998c2ecf20Sopenharmony_ci error(1, errno, "close rx_udp"); 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci if (cfg_verbose) 4028c2ecf20Sopenharmony_ci printf("processed %d messages, %d fragments\n", 4038c2ecf20Sopenharmony_ci msg_counter, frag_counter); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci fprintf(stderr, "PASS\n"); 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_cistatic void run_test_v4(void) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci struct sockaddr_in addr = {0}; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci addr.sin_family = AF_INET; 4148c2ecf20Sopenharmony_ci addr.sin_port = htons(cfg_port); 4158c2ecf20Sopenharmony_ci addr.sin_addr = addr4; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci run_test((void *)&addr, sizeof(addr), false /* !ipv6 */); 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_cistatic void run_test_v6(void) 4218c2ecf20Sopenharmony_ci{ 4228c2ecf20Sopenharmony_ci struct sockaddr_in6 addr = {0}; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci addr.sin6_family = AF_INET6; 4258c2ecf20Sopenharmony_ci addr.sin6_port = htons(cfg_port); 4268c2ecf20Sopenharmony_ci addr.sin6_addr = addr6; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci run_test((void *)&addr, sizeof(addr), true /* ipv6 */); 4298c2ecf20Sopenharmony_ci} 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_cistatic void parse_opts(int argc, char **argv) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci int c; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci while ((c = getopt(argc, argv, "46opv")) != -1) { 4368c2ecf20Sopenharmony_ci switch (c) { 4378c2ecf20Sopenharmony_ci case '4': 4388c2ecf20Sopenharmony_ci cfg_do_ipv4 = true; 4398c2ecf20Sopenharmony_ci break; 4408c2ecf20Sopenharmony_ci case '6': 4418c2ecf20Sopenharmony_ci cfg_do_ipv6 = true; 4428c2ecf20Sopenharmony_ci break; 4438c2ecf20Sopenharmony_ci case 'o': 4448c2ecf20Sopenharmony_ci cfg_overlap = true; 4458c2ecf20Sopenharmony_ci break; 4468c2ecf20Sopenharmony_ci case 'p': 4478c2ecf20Sopenharmony_ci cfg_permissive = true; 4488c2ecf20Sopenharmony_ci break; 4498c2ecf20Sopenharmony_ci case 'v': 4508c2ecf20Sopenharmony_ci cfg_verbose = true; 4518c2ecf20Sopenharmony_ci break; 4528c2ecf20Sopenharmony_ci default: 4538c2ecf20Sopenharmony_ci error(1, 0, "%s: parse error", argv[0]); 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci } 4568c2ecf20Sopenharmony_ci} 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ciint main(int argc, char **argv) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci parse_opts(argc, argv); 4618c2ecf20Sopenharmony_ci seed = time(NULL); 4628c2ecf20Sopenharmony_ci srand(seed); 4638c2ecf20Sopenharmony_ci /* Print the seed to track/reproduce potential failures. */ 4648c2ecf20Sopenharmony_ci printf("seed = %d\n", seed); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (cfg_do_ipv4) 4678c2ecf20Sopenharmony_ci run_test_v4(); 4688c2ecf20Sopenharmony_ci if (cfg_do_ipv6) 4698c2ecf20Sopenharmony_ci run_test_v6(); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci return 0; 4728c2ecf20Sopenharmony_ci} 473