18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * It is possible to use SO_REUSEPORT to open multiple sockets bound to 48c2ecf20Sopenharmony_ci * equivalent local addresses using AF_INET and AF_INET6 at the same time. If 58c2ecf20Sopenharmony_ci * the AF_INET6 socket has IPV6_V6ONLY set, it's clear which socket should 68c2ecf20Sopenharmony_ci * receive a given incoming packet. However, when it is not set, incoming v4 78c2ecf20Sopenharmony_ci * packets should prefer the AF_INET socket(s). This behavior was defined with 88c2ecf20Sopenharmony_ci * the original SO_REUSEPORT implementation, but broke with 98c2ecf20Sopenharmony_ci * e32ea7e74727 ("soreuseport: fast reuseport UDP socket selection") 108c2ecf20Sopenharmony_ci * This test creates these mixed AF_INET/AF_INET6 sockets and asserts the 118c2ecf20Sopenharmony_ci * AF_INET preference for v4 packets. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define _GNU_SOURCE 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <arpa/inet.h> 178c2ecf20Sopenharmony_ci#include <errno.h> 188c2ecf20Sopenharmony_ci#include <error.h> 198c2ecf20Sopenharmony_ci#include <linux/in.h> 208c2ecf20Sopenharmony_ci#include <linux/unistd.h> 218c2ecf20Sopenharmony_ci#include <stdio.h> 228c2ecf20Sopenharmony_ci#include <stdlib.h> 238c2ecf20Sopenharmony_ci#include <string.h> 248c2ecf20Sopenharmony_ci#include <sys/epoll.h> 258c2ecf20Sopenharmony_ci#include <sys/types.h> 268c2ecf20Sopenharmony_ci#include <sys/socket.h> 278c2ecf20Sopenharmony_ci#include <unistd.h> 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic const int PORT = 8888; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic void build_rcv_fd(int family, int proto, int *rcv_fds, int count) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci struct sockaddr_storage addr; 348c2ecf20Sopenharmony_ci struct sockaddr_in *addr4; 358c2ecf20Sopenharmony_ci struct sockaddr_in6 *addr6; 368c2ecf20Sopenharmony_ci int opt, i; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci switch (family) { 398c2ecf20Sopenharmony_ci case AF_INET: 408c2ecf20Sopenharmony_ci addr4 = (struct sockaddr_in *)&addr; 418c2ecf20Sopenharmony_ci addr4->sin_family = AF_INET; 428c2ecf20Sopenharmony_ci addr4->sin_addr.s_addr = htonl(INADDR_ANY); 438c2ecf20Sopenharmony_ci addr4->sin_port = htons(PORT); 448c2ecf20Sopenharmony_ci break; 458c2ecf20Sopenharmony_ci case AF_INET6: 468c2ecf20Sopenharmony_ci addr6 = (struct sockaddr_in6 *)&addr; 478c2ecf20Sopenharmony_ci addr6->sin6_family = AF_INET6; 488c2ecf20Sopenharmony_ci addr6->sin6_addr = in6addr_any; 498c2ecf20Sopenharmony_ci addr6->sin6_port = htons(PORT); 508c2ecf20Sopenharmony_ci break; 518c2ecf20Sopenharmony_ci default: 528c2ecf20Sopenharmony_ci error(1, 0, "Unsupported family %d", family); 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci for (i = 0; i < count; ++i) { 568c2ecf20Sopenharmony_ci rcv_fds[i] = socket(family, proto, 0); 578c2ecf20Sopenharmony_ci if (rcv_fds[i] < 0) 588c2ecf20Sopenharmony_ci error(1, errno, "failed to create receive socket"); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci opt = 1; 618c2ecf20Sopenharmony_ci if (setsockopt(rcv_fds[i], SOL_SOCKET, SO_REUSEPORT, &opt, 628c2ecf20Sopenharmony_ci sizeof(opt))) 638c2ecf20Sopenharmony_ci error(1, errno, "failed to set SO_REUSEPORT"); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci if (bind(rcv_fds[i], (struct sockaddr *)&addr, sizeof(addr))) 668c2ecf20Sopenharmony_ci error(1, errno, "failed to bind receive socket"); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (proto == SOCK_STREAM && listen(rcv_fds[i], 10)) 698c2ecf20Sopenharmony_ci error(1, errno, "failed to listen on receive port"); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic void send_from_v4(int proto) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct sockaddr_in saddr, daddr; 768c2ecf20Sopenharmony_ci int fd; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci saddr.sin_family = AF_INET; 798c2ecf20Sopenharmony_ci saddr.sin_addr.s_addr = htonl(INADDR_ANY); 808c2ecf20Sopenharmony_ci saddr.sin_port = 0; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci daddr.sin_family = AF_INET; 838c2ecf20Sopenharmony_ci daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 848c2ecf20Sopenharmony_ci daddr.sin_port = htons(PORT); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci fd = socket(AF_INET, proto, 0); 878c2ecf20Sopenharmony_ci if (fd < 0) 888c2ecf20Sopenharmony_ci error(1, errno, "failed to create send socket"); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr))) 918c2ecf20Sopenharmony_ci error(1, errno, "failed to bind send socket"); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr))) 948c2ecf20Sopenharmony_ci error(1, errno, "failed to connect send socket"); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci if (send(fd, "a", 1, 0) < 0) 978c2ecf20Sopenharmony_ci error(1, errno, "failed to send message"); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci close(fd); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic int receive_once(int epfd, int proto) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci struct epoll_event ev; 1058c2ecf20Sopenharmony_ci int i, fd; 1068c2ecf20Sopenharmony_ci char buf[8]; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci i = epoll_wait(epfd, &ev, 1, -1); 1098c2ecf20Sopenharmony_ci if (i < 0) 1108c2ecf20Sopenharmony_ci error(1, errno, "epoll_wait failed"); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (proto == SOCK_STREAM) { 1138c2ecf20Sopenharmony_ci fd = accept(ev.data.fd, NULL, NULL); 1148c2ecf20Sopenharmony_ci if (fd < 0) 1158c2ecf20Sopenharmony_ci error(1, errno, "failed to accept"); 1168c2ecf20Sopenharmony_ci i = recv(fd, buf, sizeof(buf), 0); 1178c2ecf20Sopenharmony_ci close(fd); 1188c2ecf20Sopenharmony_ci } else { 1198c2ecf20Sopenharmony_ci i = recv(ev.data.fd, buf, sizeof(buf), 0); 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci if (i < 0) 1238c2ecf20Sopenharmony_ci error(1, errno, "failed to recv"); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci return ev.data.fd; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic void test(int *rcv_fds, int count, int proto) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci struct epoll_event ev; 1318c2ecf20Sopenharmony_ci int epfd, i, test_fd; 1328c2ecf20Sopenharmony_ci int test_family; 1338c2ecf20Sopenharmony_ci socklen_t len; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci epfd = epoll_create(1); 1368c2ecf20Sopenharmony_ci if (epfd < 0) 1378c2ecf20Sopenharmony_ci error(1, errno, "failed to create epoll"); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci ev.events = EPOLLIN; 1408c2ecf20Sopenharmony_ci for (i = 0; i < count; ++i) { 1418c2ecf20Sopenharmony_ci ev.data.fd = rcv_fds[i]; 1428c2ecf20Sopenharmony_ci if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fds[i], &ev)) 1438c2ecf20Sopenharmony_ci error(1, errno, "failed to register sock epoll"); 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci send_from_v4(proto); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci test_fd = receive_once(epfd, proto); 1498c2ecf20Sopenharmony_ci len = sizeof(test_family); 1508c2ecf20Sopenharmony_ci if (getsockopt(test_fd, SOL_SOCKET, SO_DOMAIN, &test_family, &len)) 1518c2ecf20Sopenharmony_ci error(1, errno, "failed to read socket domain"); 1528c2ecf20Sopenharmony_ci if (test_family != AF_INET) 1538c2ecf20Sopenharmony_ci error(1, 0, "expected to receive on v4 socket but got v6 (%d)", 1548c2ecf20Sopenharmony_ci test_family); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci close(epfd); 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciint main(void) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci int rcv_fds[32], i; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci fprintf(stderr, "---- UDP IPv4 created before IPv6 ----\n"); 1648c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 5); 1658c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[5]), 5); 1668c2ecf20Sopenharmony_ci test(rcv_fds, 10, SOCK_DGRAM); 1678c2ecf20Sopenharmony_ci for (i = 0; i < 10; ++i) 1688c2ecf20Sopenharmony_ci close(rcv_fds[i]); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci fprintf(stderr, "---- UDP IPv6 created before IPv4 ----\n"); 1718c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 5); 1728c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[5]), 5); 1738c2ecf20Sopenharmony_ci test(rcv_fds, 10, SOCK_DGRAM); 1748c2ecf20Sopenharmony_ci for (i = 0; i < 10; ++i) 1758c2ecf20Sopenharmony_ci close(rcv_fds[i]); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci /* NOTE: UDP socket lookups traverse a different code path when there 1788c2ecf20Sopenharmony_ci * are > 10 sockets in a group. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_ci fprintf(stderr, "---- UDP IPv4 created before IPv6 (large) ----\n"); 1818c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 16); 1828c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[16]), 16); 1838c2ecf20Sopenharmony_ci test(rcv_fds, 32, SOCK_DGRAM); 1848c2ecf20Sopenharmony_ci for (i = 0; i < 32; ++i) 1858c2ecf20Sopenharmony_ci close(rcv_fds[i]); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci fprintf(stderr, "---- UDP IPv6 created before IPv4 (large) ----\n"); 1888c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 16); 1898c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[16]), 16); 1908c2ecf20Sopenharmony_ci test(rcv_fds, 32, SOCK_DGRAM); 1918c2ecf20Sopenharmony_ci for (i = 0; i < 32; ++i) 1928c2ecf20Sopenharmony_ci close(rcv_fds[i]); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci fprintf(stderr, "---- TCP IPv4 created before IPv6 ----\n"); 1958c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 5); 1968c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_STREAM, &(rcv_fds[5]), 5); 1978c2ecf20Sopenharmony_ci test(rcv_fds, 10, SOCK_STREAM); 1988c2ecf20Sopenharmony_ci for (i = 0; i < 10; ++i) 1998c2ecf20Sopenharmony_ci close(rcv_fds[i]); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci fprintf(stderr, "---- TCP IPv6 created before IPv4 ----\n"); 2028c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds, 5); 2038c2ecf20Sopenharmony_ci build_rcv_fd(AF_INET, SOCK_STREAM, &(rcv_fds[5]), 5); 2048c2ecf20Sopenharmony_ci test(rcv_fds, 10, SOCK_STREAM); 2058c2ecf20Sopenharmony_ci for (i = 0; i < 10; ++i) 2068c2ecf20Sopenharmony_ci close(rcv_fds[i]); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci fprintf(stderr, "SUCCESS\n"); 2098c2ecf20Sopenharmony_ci return 0; 2108c2ecf20Sopenharmony_ci} 211