xref: /linux/tools/testing/selftests/net/reuseport_dualstack.c (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * It is possible to use SO_REUSEPORT to open multiple sockets bound to
4  * equivalent local addresses using AF_INET and AF_INET6 at the same time.  If
5  * the AF_INET6 socket has IPV6_V6ONLY set, it's clear which socket should
6  * receive a given incoming packet.  However, when it is not set, incoming v4
7  * packets should prefer the AF_INET socket(s).  This behavior was defined with
8  * the original SO_REUSEPORT implementation, but broke with
9  * e32ea7e74727 ("soreuseport: fast reuseport UDP socket selection")
10  * This test creates these mixed AF_INET/AF_INET6 sockets and asserts the
11  * AF_INET preference for v4 packets.
12  */
13 
14 #define _GNU_SOURCE
15 
16 #include <arpa/inet.h>
17 #include <errno.h>
18 #include <error.h>
19 #include <linux/in.h>
20 #include <linux/unistd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/epoll.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 
29 static const int PORT = 8888;
30 
31 static void build_rcv_fd(int family, int proto, int *rcv_fds, int count)
32 {
33 	struct sockaddr_storage addr;
34 	struct sockaddr_in  *addr4;
35 	struct sockaddr_in6 *addr6;
36 	int opt, i;
37 
38 	switch (family) {
39 	case AF_INET:
40 		addr4 = (struct sockaddr_in *)&addr;
41 		addr4->sin_family = AF_INET;
42 		addr4->sin_addr.s_addr = htonl(INADDR_ANY);
43 		addr4->sin_port = htons(PORT);
44 		break;
45 	case AF_INET6:
46 		addr6 = (struct sockaddr_in6 *)&addr;
47 		addr6->sin6_family = AF_INET6;
48 		addr6->sin6_addr = in6addr_any;
49 		addr6->sin6_port = htons(PORT);
50 		break;
51 	default:
52 		error(1, 0, "Unsupported family %d", family);
53 	}
54 
55 	for (i = 0; i < count; ++i) {
56 		rcv_fds[i] = socket(family, proto, 0);
57 		if (rcv_fds[i] < 0)
58 			error(1, errno, "failed to create receive socket");
59 
60 		opt = 1;
61 		if (setsockopt(rcv_fds[i], SOL_SOCKET, SO_REUSEPORT, &opt,
62 			       sizeof(opt)))
63 			error(1, errno, "failed to set SO_REUSEPORT");
64 
65 		if (bind(rcv_fds[i], (struct sockaddr *)&addr, sizeof(addr)))
66 			error(1, errno, "failed to bind receive socket");
67 
68 		if (proto == SOCK_STREAM && listen(rcv_fds[i], 10))
69 			error(1, errno, "failed to listen on receive port");
70 	}
71 }
72 
73 static void send_from_v4(int proto)
74 {
75 	struct sockaddr_in  saddr, daddr;
76 	int fd;
77 
78 	saddr.sin_family = AF_INET;
79 	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
80 	saddr.sin_port = 0;
81 
82 	daddr.sin_family = AF_INET;
83 	daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
84 	daddr.sin_port = htons(PORT);
85 
86 	fd = socket(AF_INET, proto, 0);
87 	if (fd < 0)
88 		error(1, errno, "failed to create send socket");
89 
90 	if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
91 		error(1, errno, "failed to bind send socket");
92 
93 	if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr)))
94 		error(1, errno, "failed to connect send socket");
95 
96 	if (send(fd, "a", 1, 0) < 0)
97 		error(1, errno, "failed to send message");
98 
99 	close(fd);
100 }
101 
102 static int receive_once(int epfd, int proto)
103 {
104 	struct epoll_event ev;
105 	int i, fd;
106 	char buf[8];
107 
108 	i = epoll_wait(epfd, &ev, 1, -1);
109 	if (i < 0)
110 		error(1, errno, "epoll_wait failed");
111 
112 	if (proto == SOCK_STREAM) {
113 		fd = accept(ev.data.fd, NULL, NULL);
114 		if (fd < 0)
115 			error(1, errno, "failed to accept");
116 		i = recv(fd, buf, sizeof(buf), 0);
117 		close(fd);
118 	} else {
119 		i = recv(ev.data.fd, buf, sizeof(buf), 0);
120 	}
121 
122 	if (i < 0)
123 		error(1, errno, "failed to recv");
124 
125 	return ev.data.fd;
126 }
127 
128 static void test(int *rcv_fds, int count, int proto)
129 {
130 	struct epoll_event ev;
131 	int epfd, i, test_fd;
132 	uint16_t test_family;
133 	socklen_t len;
134 
135 	epfd = epoll_create(1);
136 	if (epfd < 0)
137 		error(1, errno, "failed to create epoll");
138 
139 	ev.events = EPOLLIN;
140 	for (i = 0; i < count; ++i) {
141 		ev.data.fd = rcv_fds[i];
142 		if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fds[i], &ev))
143 			error(1, errno, "failed to register sock epoll");
144 	}
145 
146 	send_from_v4(proto);
147 
148 	test_fd = receive_once(epfd, proto);
149 	if (getsockopt(test_fd, SOL_SOCKET, SO_DOMAIN, &test_family, &len))
150 		error(1, errno, "failed to read socket domain");
151 	if (test_family != AF_INET)
152 		error(1, 0, "expected to receive on v4 socket but got v6 (%d)",
153 		      test_family);
154 
155 	close(epfd);
156 }
157 
158 int main(void)
159 {
160 	int rcv_fds[32], i;
161 
162 	fprintf(stderr, "---- UDP IPv4 created before IPv6 ----\n");
163 	build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 5);
164 	build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[5]), 5);
165 	test(rcv_fds, 10, SOCK_DGRAM);
166 	for (i = 0; i < 10; ++i)
167 		close(rcv_fds[i]);
168 
169 	fprintf(stderr, "---- UDP IPv6 created before IPv4 ----\n");
170 	build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 5);
171 	build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[5]), 5);
172 	test(rcv_fds, 10, SOCK_DGRAM);
173 	for (i = 0; i < 10; ++i)
174 		close(rcv_fds[i]);
175 
176 	/* NOTE: UDP socket lookups traverse a different code path when there
177 	 * are > 10 sockets in a group.
178 	 */
179 	fprintf(stderr, "---- UDP IPv4 created before IPv6 (large) ----\n");
180 	build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 16);
181 	build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[16]), 16);
182 	test(rcv_fds, 32, SOCK_DGRAM);
183 	for (i = 0; i < 32; ++i)
184 		close(rcv_fds[i]);
185 
186 	fprintf(stderr, "---- UDP IPv6 created before IPv4 (large) ----\n");
187 	build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 16);
188 	build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[16]), 16);
189 	test(rcv_fds, 32, SOCK_DGRAM);
190 	for (i = 0; i < 32; ++i)
191 		close(rcv_fds[i]);
192 
193 	fprintf(stderr, "---- TCP IPv4 created before IPv6 ----\n");
194 	build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 5);
195 	build_rcv_fd(AF_INET6, SOCK_STREAM, &(rcv_fds[5]), 5);
196 	test(rcv_fds, 10, SOCK_STREAM);
197 	for (i = 0; i < 10; ++i)
198 		close(rcv_fds[i]);
199 
200 	fprintf(stderr, "---- TCP IPv6 created before IPv4 ----\n");
201 	build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds, 5);
202 	build_rcv_fd(AF_INET, SOCK_STREAM, &(rcv_fds[5]), 5);
203 	test(rcv_fds, 10, SOCK_STREAM);
204 	for (i = 0; i < 10; ++i)
205 		close(rcv_fds[i]);
206 
207 	fprintf(stderr, "SUCCESS\n");
208 	return 0;
209 }
210