xref: /linux/tools/testing/selftests/net/reuseport_dualstack.c (revision 16c8373ecf7b3c723a8e765d798ea413bc8345a6)
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 	int 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 	len = sizeof(test_family);
150 	if (getsockopt(test_fd, SOL_SOCKET, SO_DOMAIN, &test_family, &len))
151 		error(1, errno, "failed to read socket domain");
152 	if (test_family != AF_INET)
153 		error(1, 0, "expected to receive on v4 socket but got v6 (%d)",
154 		      test_family);
155 
156 	close(epfd);
157 }
158 
159 int main(void)
160 {
161 	int rcv_fds[32], i;
162 
163 	fprintf(stderr, "---- UDP IPv4 created before IPv6 ----\n");
164 	build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 5);
165 	build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[5]), 5);
166 	test(rcv_fds, 10, SOCK_DGRAM);
167 	for (i = 0; i < 10; ++i)
168 		close(rcv_fds[i]);
169 
170 	fprintf(stderr, "---- UDP IPv6 created before IPv4 ----\n");
171 	build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 5);
172 	build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[5]), 5);
173 	test(rcv_fds, 10, SOCK_DGRAM);
174 	for (i = 0; i < 10; ++i)
175 		close(rcv_fds[i]);
176 
177 	/* NOTE: UDP socket lookups traverse a different code path when there
178 	 * are > 10 sockets in a group.
179 	 */
180 	fprintf(stderr, "---- UDP IPv4 created before IPv6 (large) ----\n");
181 	build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 16);
182 	build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[16]), 16);
183 	test(rcv_fds, 32, SOCK_DGRAM);
184 	for (i = 0; i < 32; ++i)
185 		close(rcv_fds[i]);
186 
187 	fprintf(stderr, "---- UDP IPv6 created before IPv4 (large) ----\n");
188 	build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 16);
189 	build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[16]), 16);
190 	test(rcv_fds, 32, SOCK_DGRAM);
191 	for (i = 0; i < 32; ++i)
192 		close(rcv_fds[i]);
193 
194 	fprintf(stderr, "---- TCP IPv4 created before IPv6 ----\n");
195 	build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 5);
196 	build_rcv_fd(AF_INET6, SOCK_STREAM, &(rcv_fds[5]), 5);
197 	test(rcv_fds, 10, SOCK_STREAM);
198 	for (i = 0; i < 10; ++i)
199 		close(rcv_fds[i]);
200 
201 	fprintf(stderr, "---- TCP IPv6 created before IPv4 ----\n");
202 	build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds, 5);
203 	build_rcv_fd(AF_INET, SOCK_STREAM, &(rcv_fds[5]), 5);
204 	test(rcv_fds, 10, SOCK_STREAM);
205 	for (i = 0; i < 10; ++i)
206 		close(rcv_fds[i]);
207 
208 	fprintf(stderr, "SUCCESS\n");
209 	return 0;
210 }
211