1422d8dc6SJosef Bacik /*
2422d8dc6SJosef Bacik * Test for the regression introduced by
3422d8dc6SJosef Bacik *
4422d8dc6SJosef Bacik * b9470c27607b ("inet: kill smallest_size and smallest_port")
5422d8dc6SJosef Bacik *
6422d8dc6SJosef Bacik * If we open an ipv4 socket on a port with reuseaddr we shouldn't reset the tb
7422d8dc6SJosef Bacik * when we open the ipv6 conterpart, which is what was happening previously.
8422d8dc6SJosef Bacik */
9422d8dc6SJosef Bacik #include <errno.h>
10422d8dc6SJosef Bacik #include <error.h>
11422d8dc6SJosef Bacik #include <arpa/inet.h>
12422d8dc6SJosef Bacik #include <netinet/in.h>
13422d8dc6SJosef Bacik #include <stdbool.h>
14422d8dc6SJosef Bacik #include <stdio.h>
15422d8dc6SJosef Bacik #include <sys/socket.h>
16422d8dc6SJosef Bacik #include <sys/types.h>
17422d8dc6SJosef Bacik #include <unistd.h>
18422d8dc6SJosef Bacik
19422d8dc6SJosef Bacik #define PORT 9999
20422d8dc6SJosef Bacik
open_port(int ipv6,int any)21422d8dc6SJosef Bacik int open_port(int ipv6, int any)
22422d8dc6SJosef Bacik {
23422d8dc6SJosef Bacik int fd = -1;
24422d8dc6SJosef Bacik int reuseaddr = 1;
25422d8dc6SJosef Bacik int v6only = 1;
26422d8dc6SJosef Bacik int addrlen;
27422d8dc6SJosef Bacik int ret = -1;
28422d8dc6SJosef Bacik struct sockaddr *addr;
29422d8dc6SJosef Bacik int family = ipv6 ? AF_INET6 : AF_INET;
30422d8dc6SJosef Bacik
31422d8dc6SJosef Bacik struct sockaddr_in6 addr6 = {
32422d8dc6SJosef Bacik .sin6_family = AF_INET6,
33422d8dc6SJosef Bacik .sin6_port = htons(PORT),
34422d8dc6SJosef Bacik .sin6_addr = in6addr_any
35422d8dc6SJosef Bacik };
36422d8dc6SJosef Bacik struct sockaddr_in addr4 = {
37422d8dc6SJosef Bacik .sin_family = AF_INET,
38422d8dc6SJosef Bacik .sin_port = htons(PORT),
39422d8dc6SJosef Bacik .sin_addr.s_addr = any ? htonl(INADDR_ANY) : inet_addr("127.0.0.1"),
40422d8dc6SJosef Bacik };
41422d8dc6SJosef Bacik
42422d8dc6SJosef Bacik
43422d8dc6SJosef Bacik if (ipv6) {
44422d8dc6SJosef Bacik addr = (struct sockaddr*)&addr6;
45422d8dc6SJosef Bacik addrlen = sizeof(addr6);
46422d8dc6SJosef Bacik } else {
47422d8dc6SJosef Bacik addr = (struct sockaddr*)&addr4;
48422d8dc6SJosef Bacik addrlen = sizeof(addr4);
49422d8dc6SJosef Bacik }
50422d8dc6SJosef Bacik
51422d8dc6SJosef Bacik if ((fd = socket(family, SOCK_STREAM, IPPROTO_TCP)) < 0) {
52422d8dc6SJosef Bacik perror("socket");
53422d8dc6SJosef Bacik goto out;
54422d8dc6SJosef Bacik }
55422d8dc6SJosef Bacik
56422d8dc6SJosef Bacik if (ipv6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&v6only,
57422d8dc6SJosef Bacik sizeof(v6only)) < 0) {
58422d8dc6SJosef Bacik perror("setsockopt IPV6_V6ONLY");
59422d8dc6SJosef Bacik goto out;
60422d8dc6SJosef Bacik }
61422d8dc6SJosef Bacik
62422d8dc6SJosef Bacik if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
63422d8dc6SJosef Bacik sizeof(reuseaddr)) < 0) {
64422d8dc6SJosef Bacik perror("setsockopt SO_REUSEADDR");
65422d8dc6SJosef Bacik goto out;
66422d8dc6SJosef Bacik }
67422d8dc6SJosef Bacik
68422d8dc6SJosef Bacik if (bind(fd, addr, addrlen) < 0) {
69422d8dc6SJosef Bacik perror("bind");
70422d8dc6SJosef Bacik goto out;
71422d8dc6SJosef Bacik }
72422d8dc6SJosef Bacik
73422d8dc6SJosef Bacik if (any)
74422d8dc6SJosef Bacik return fd;
75422d8dc6SJosef Bacik
76422d8dc6SJosef Bacik if (listen(fd, 1) < 0) {
77422d8dc6SJosef Bacik perror("listen");
78422d8dc6SJosef Bacik goto out;
79422d8dc6SJosef Bacik }
80422d8dc6SJosef Bacik return fd;
81422d8dc6SJosef Bacik out:
82422d8dc6SJosef Bacik close(fd);
83422d8dc6SJosef Bacik return ret;
84422d8dc6SJosef Bacik }
85422d8dc6SJosef Bacik
main(void)86422d8dc6SJosef Bacik int main(void)
87422d8dc6SJosef Bacik {
88422d8dc6SJosef Bacik int listenfd;
89422d8dc6SJosef Bacik int fd1, fd2;
90422d8dc6SJosef Bacik
91422d8dc6SJosef Bacik fprintf(stderr, "Opening 127.0.0.1:%d\n", PORT);
92422d8dc6SJosef Bacik listenfd = open_port(0, 0);
93422d8dc6SJosef Bacik if (listenfd < 0)
94422d8dc6SJosef Bacik error(1, errno, "Couldn't open listen socket");
95422d8dc6SJosef Bacik fprintf(stderr, "Opening INADDR_ANY:%d\n", PORT);
96422d8dc6SJosef Bacik fd1 = open_port(0, 1);
97422d8dc6SJosef Bacik if (fd1 >= 0)
98422d8dc6SJosef Bacik error(1, 0, "Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket");
99422d8dc6SJosef Bacik fprintf(stderr, "Opening in6addr_any:%d\n", PORT);
100422d8dc6SJosef Bacik fd1 = open_port(1, 1);
101422d8dc6SJosef Bacik if (fd1 < 0)
102422d8dc6SJosef Bacik error(1, errno, "Couldn't open ipv6 reuseport");
103422d8dc6SJosef Bacik fprintf(stderr, "Opening INADDR_ANY:%d\n", PORT);
104422d8dc6SJosef Bacik fd2 = open_port(0, 1);
105422d8dc6SJosef Bacik if (fd2 >= 0)
106422d8dc6SJosef Bacik error(1, 0, "Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket");
107422d8dc6SJosef Bacik close(fd1);
108422d8dc6SJosef Bacik fprintf(stderr, "Opening INADDR_ANY:%d after closing ipv6 socket\n", PORT);
109422d8dc6SJosef Bacik fd1 = open_port(0, 1);
110422d8dc6SJosef Bacik if (fd1 >= 0)
111422d8dc6SJosef Bacik error(1, 0, "Was allowed to create an ipv4 reuseport on an already bound non-reuseport socket with no ipv6");
112*31974122SJakub Kicinski fprintf(stderr, "Success\n");
113422d8dc6SJosef Bacik return 0;
114422d8dc6SJosef Bacik }
115