Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Commit 01c445a

Browse files
committed
Merge branch 'soreuseport-mixed-v4-v6-fixes'
Craig Gallek says: ==================== Fixes for SO_REUSEPORT and mixed v4/v6 sockets Recent changes to the datastructures associated with SO_REUSEPORT broke an existing behavior when equivalent SO_REUSEPORT sockets are created using both AF_INET and AF_INET6. This patch series restores the previous behavior and includes a test to validate it. This series should be a trivial merge to stable kernels (if deemed necessary), but will have conflicts in net-next. The following patches recently replaced the use of hlist_nulls with hlists for UDP and TCP socket lists: ca065d0 ("udp: no longer use SLAB_DESTROY_BY_RCU") 3b24d85 ("tcp/dccp: do not touch listener sk_refcnt under synflood") If this series is accepted, I will send an RFC for the net-next change to assist with the merge. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents c5b5343 + d6a61f8 commit 01c445a

File tree

6 files changed

+261
-4
lines changed

6 files changed

+261
-4
lines changed

include/linux/rculist_nulls.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,45 @@ static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
9898
if (!is_a_nulls(first))
9999
first->pprev = &n->next;
100100
}
101+
102+
/**
103+
* hlist_nulls_add_tail_rcu
104+
* @n: the element to add to the hash list.
105+
* @h: the list to add to.
106+
*
107+
* Description:
108+
* Adds the specified element to the end of the specified hlist_nulls,
109+
* while permitting racing traversals. NOTE: tail insertion requires
110+
* list traversal.
111+
*
112+
* The caller must take whatever precautions are necessary
113+
* (such as holding appropriate locks) to avoid racing
114+
* with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
115+
* or hlist_nulls_del_rcu(), running on this same list.
116+
* However, it is perfectly legal to run concurrently with
117+
* the _rcu list-traversal primitives, such as
118+
* hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
119+
* problems on Alpha CPUs. Regardless of the type of CPU, the
120+
* list-traversal primitive must be guarded by rcu_read_lock().
121+
*/
122+
static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
123+
struct hlist_nulls_head *h)
124+
{
125+
struct hlist_nulls_node *i, *last = NULL;
126+
127+
for (i = hlist_nulls_first_rcu(h); !is_a_nulls(i);
128+
i = hlist_nulls_next_rcu(i))
129+
last = i;
130+
131+
if (last) {
132+
n->next = last->next;
133+
n->pprev = &last->next;
134+
rcu_assign_pointer(hlist_nulls_next_rcu(last), n);
135+
} else {
136+
hlist_nulls_add_head_rcu(n, h);
137+
}
138+
}
139+
101140
/**
102141
* hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
103142
* @tpos: the type * to use as a loop cursor.

include/net/sock.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,11 @@ static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
630630

631631
static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
632632
{
633-
hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
633+
if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
634+
sk->sk_family == AF_INET6)
635+
hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list);
636+
else
637+
hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
634638
}
635639

636640
static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)

net/ipv4/udp.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,13 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
339339

340340
hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
341341
spin_lock(&hslot2->lock);
342-
hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
343-
&hslot2->head);
342+
if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
343+
sk->sk_family == AF_INET6)
344+
hlist_nulls_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
345+
&hslot2->head);
346+
else
347+
hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
348+
&hslot2->head);
344349
hslot2->count++;
345350
spin_unlock(&hslot2->lock);
346351
}

tools/testing/selftests/net/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ psock_fanout
33
psock_tpacket
44
reuseport_bpf
55
reuseport_bpf_cpu
6+
reuseport_dualstack

tools/testing/selftests/net/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CFLAGS = -Wall -O2 -g
44

55
CFLAGS += -I../../../../usr/include/
66

7-
NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf reuseport_bpf_cpu
7+
NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf reuseport_bpf_cpu reuseport_dualstack
88

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

0 commit comments

Comments
 (0)