Commit 01c445a4 authored by David S. Miller's avatar David S. Miller

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:
ca065d0c ("udp: no longer use SLAB_DESTROY_BY_RCU")
3b24d854 ("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: default avatarDavid S. Miller <davem@davemloft.net>
parents c5b5343c d6a61f80
......@@ -98,6 +98,45 @@ static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
if (!is_a_nulls(first))
first->pprev = &n->next;
}
/**
* hlist_nulls_add_tail_rcu
* @n: the element to add to the hash list.
* @h: the list to add to.
*
* Description:
* Adds the specified element to the end of the specified hlist_nulls,
* while permitting racing traversals. NOTE: tail insertion requires
* list traversal.
*
* The caller must take whatever precautions are necessary
* (such as holding appropriate locks) to avoid racing
* with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
* or hlist_nulls_del_rcu(), running on this same list.
* However, it is perfectly legal to run concurrently with
* the _rcu list-traversal primitives, such as
* hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
* problems on Alpha CPUs. Regardless of the type of CPU, the
* list-traversal primitive must be guarded by rcu_read_lock().
*/
static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
struct hlist_nulls_head *h)
{
struct hlist_nulls_node *i, *last = NULL;
for (i = hlist_nulls_first_rcu(h); !is_a_nulls(i);
i = hlist_nulls_next_rcu(i))
last = i;
if (last) {
n->next = last->next;
n->pprev = &last->next;
rcu_assign_pointer(hlist_nulls_next_rcu(last), n);
} else {
hlist_nulls_add_head_rcu(n, h);
}
}
/**
* hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
* @tpos: the type * to use as a loop cursor.
......
......@@ -630,7 +630,11 @@ static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
{
hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
sk->sk_family == AF_INET6)
hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list);
else
hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
}
static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
......
......@@ -339,8 +339,13 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
spin_lock(&hslot2->lock);
hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
&hslot2->head);
if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
sk->sk_family == AF_INET6)
hlist_nulls_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
&hslot2->head);
else
hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
&hslot2->head);
hslot2->count++;
spin_unlock(&hslot2->lock);
}
......
......@@ -3,3 +3,4 @@ psock_fanout
psock_tpacket
reuseport_bpf
reuseport_bpf_cpu
reuseport_dualstack
......@@ -4,7 +4,7 @@ CFLAGS = -Wall -O2 -g
CFLAGS += -I../../../../usr/include/
NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf reuseport_bpf_cpu
NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf reuseport_bpf_cpu reuseport_dualstack
all: $(NET_PROGS)
%: %.c
......
/*
* It is possible to use SO_REUSEPORT to open multiple sockets bound to
* equivalent local addresses using AF_INET and AF_INET6 at the same time. If
* the AF_INET6 socket has IPV6_V6ONLY set, it's clear which socket should
* receive a given incoming packet. However, when it is not set, incoming v4
* packets should prefer the AF_INET socket(s). This behavior was defined with
* the original SO_REUSEPORT implementation, but broke with
* e32ea7e74727 ("soreuseport: fast reuseport UDP socket selection")
* This test creates these mixed AF_INET/AF_INET6 sockets and asserts the
* AF_INET preference for v4 packets.
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
#include <linux/in.h>
#include <linux/unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
static const int PORT = 8888;
static void build_rcv_fd(int family, int proto, int *rcv_fds, int count)
{
struct sockaddr_storage addr;
struct sockaddr_in *addr4;
struct sockaddr_in6 *addr6;
int opt, i;
switch (family) {
case AF_INET:
addr4 = (struct sockaddr_in *)&addr;
addr4->sin_family = AF_INET;
addr4->sin_addr.s_addr = htonl(INADDR_ANY);
addr4->sin_port = htons(PORT);
break;
case AF_INET6:
addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_addr = in6addr_any;
addr6->sin6_port = htons(PORT);
break;
default:
error(1, 0, "Unsupported family %d", family);
}
for (i = 0; i < count; ++i) {
rcv_fds[i] = socket(family, proto, 0);
if (rcv_fds[i] < 0)
error(1, errno, "failed to create receive socket");
opt = 1;
if (setsockopt(rcv_fds[i], SOL_SOCKET, SO_REUSEPORT, &opt,
sizeof(opt)))
error(1, errno, "failed to set SO_REUSEPORT");
if (bind(rcv_fds[i], (struct sockaddr *)&addr, sizeof(addr)))
error(1, errno, "failed to bind receive socket");
if (proto == SOCK_STREAM && listen(rcv_fds[i], 10))
error(1, errno, "failed to listen on receive port");
}
}
static void send_from_v4(int proto)
{
struct sockaddr_in saddr, daddr;
int fd;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = 0;
daddr.sin_family = AF_INET;
daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
daddr.sin_port = htons(PORT);
fd = socket(AF_INET, proto, 0);
if (fd < 0)
error(1, errno, "failed to create send socket");
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
error(1, errno, "failed to bind send socket");
if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr)))
error(1, errno, "failed to connect send socket");
if (send(fd, "a", 1, 0) < 0)
error(1, errno, "failed to send message");
close(fd);
}
static int receive_once(int epfd, int proto)
{
struct epoll_event ev;
int i, fd;
char buf[8];
i = epoll_wait(epfd, &ev, 1, -1);
if (i < 0)
error(1, errno, "epoll_wait failed");
if (proto == SOCK_STREAM) {
fd = accept(ev.data.fd, NULL, NULL);
if (fd < 0)
error(1, errno, "failed to accept");
i = recv(fd, buf, sizeof(buf), 0);
close(fd);
} else {
i = recv(ev.data.fd, buf, sizeof(buf), 0);
}
if (i < 0)
error(1, errno, "failed to recv");
return ev.data.fd;
}
static void test(int *rcv_fds, int count, int proto)
{
struct epoll_event ev;
int epfd, i, test_fd;
uint16_t test_family;
socklen_t len;
epfd = epoll_create(1);
if (epfd < 0)
error(1, errno, "failed to create epoll");
ev.events = EPOLLIN;
for (i = 0; i < count; ++i) {
ev.data.fd = rcv_fds[i];
if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fds[i], &ev))
error(1, errno, "failed to register sock epoll");
}
send_from_v4(proto);
test_fd = receive_once(epfd, proto);
if (getsockopt(test_fd, SOL_SOCKET, SO_DOMAIN, &test_family, &len))
error(1, errno, "failed to read socket domain");
if (test_family != AF_INET)
error(1, 0, "expected to receive on v4 socket but got v6 (%d)",
test_family);
close(epfd);
}
int main(void)
{
int rcv_fds[32], i;
fprintf(stderr, "---- UDP IPv4 created before IPv6 ----\n");
build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 5);
build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[5]), 5);
test(rcv_fds, 10, SOCK_DGRAM);
for (i = 0; i < 10; ++i)
close(rcv_fds[i]);
fprintf(stderr, "---- UDP IPv6 created before IPv4 ----\n");
build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 5);
build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[5]), 5);
test(rcv_fds, 10, SOCK_DGRAM);
for (i = 0; i < 10; ++i)
close(rcv_fds[i]);
/* NOTE: UDP socket lookups traverse a different code path when there
* are > 10 sockets in a group.
*/
fprintf(stderr, "---- UDP IPv4 created before IPv6 (large) ----\n");
build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 16);
build_rcv_fd(AF_INET6, SOCK_DGRAM, &(rcv_fds[16]), 16);
test(rcv_fds, 32, SOCK_DGRAM);
for (i = 0; i < 32; ++i)
close(rcv_fds[i]);
fprintf(stderr, "---- UDP IPv6 created before IPv4 (large) ----\n");
build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds, 16);
build_rcv_fd(AF_INET, SOCK_DGRAM, &(rcv_fds[16]), 16);
test(rcv_fds, 32, SOCK_DGRAM);
for (i = 0; i < 32; ++i)
close(rcv_fds[i]);
fprintf(stderr, "---- TCP IPv4 created before IPv6 ----\n");
build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 5);
build_rcv_fd(AF_INET6, SOCK_STREAM, &(rcv_fds[5]), 5);
test(rcv_fds, 10, SOCK_STREAM);
for (i = 0; i < 10; ++i)
close(rcv_fds[i]);
fprintf(stderr, "---- TCP IPv6 created before IPv4 ----\n");
build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds, 5);
build_rcv_fd(AF_INET, SOCK_STREAM, &(rcv_fds[5]), 5);
test(rcv_fds, 10, SOCK_STREAM);
for (i = 0; i < 10; ++i)
close(rcv_fds[i]);
fprintf(stderr, "SUCCESS\n");
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment