From: Kalash Nainwal With tcp_l3mdev_accept=1, an unbound listener also matches connections that arrive in a VRF. If a VRF-bound listener exists on the same port and both use SO_REUSEPORT, the lookup returns whichever is first in the listen bucket, not the VRF-bound one. Since AF_INET and AF_INET6 reuseport sockets are inserted at opposite ends of the bucket, the same setup can pick the VRF-bound listener for IPv4 and the unbound one for IPv6. This breaks, for example, a BGP daemon with a TCP-MD5 key only on its VRF-bound listener: IPv6 peers in the VRF reach the unbound listener and the session comes up without MD5. Add net.ipv4.tcp_l3mdev_exempt_ports, a per-netns port list in the same format as ip_local_reserved_ports. For a listed port, an unbound socket does not match connections that arrive in a VRF, so only a VRF-bound listener can take them. Other ports and default-VRF traffic are not affected. The default is empty. Signed-off-by: Kalash Nainwal --- Documentation/networking/ip-sysctl.rst | 19 +++++++++++++++++++ include/net/ip.h | 14 ++++++++++++++ include/net/netns/ipv4.h | 1 + net/ipv4/inet_hashtables.c | 5 +++++ net/ipv4/sysctl_net_ipv4.c | 14 ++++++++++++++ net/ipv6/inet6_hashtables.c | 4 ++++ 6 files changed, 57 insertions(+) diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst index 3cc80d9b024..1de0ee5c0d5 100644 --- a/Documentation/networking/ip-sysctl.rst +++ b/Documentation/networking/ip-sysctl.rst @@ -619,6 +619,25 @@ tcp_l3mdev_accept - BOOLEAN Default: 0 (disabled) +tcp_l3mdev_exempt_ports - list of comma separated ranges + TCP ports for which tcp_l3mdev_accept is ignored. When + tcp_l3mdev_accept is enabled, a listen socket that is not bound to + an L3 master device also matches connections that arrive in an L3 + domain (e.g., a VRF). For a port in this list, such a socket does + not match these connections; only a listen socket bound to the L3 + master device does. Connections that do not arrive in an L3 domain + are not affected. This applies to both IPv4 and IPv6. + + This is useful when the same port has both an unbound listener and + a listener bound to a VRF, and VRF connections must reach the + VRF-bound one (for example, when only it has TCP-MD5 or TCP-AO + keys configured). + + The format is the same as ip_local_reserved_ports, e.g. + "179,5000-5010". Writing to the file replaces the current list. + + Default: empty + tcp_low_latency - BOOLEAN This is a legacy option, it has no effect anymore. diff --git a/include/net/ip.h b/include/net/ip.h index 5035b313a26..c6581080a9a 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -396,6 +396,20 @@ static inline bool inet_port_requires_bind_service(struct net *net, unsigned sho } #endif +/* Ports listed in net.ipv4.tcp_l3mdev_exempt_ports behave as if + * tcp_l3mdev_accept were 0: an unbound listener does not match traffic + * received on an L3 master device slave. Applies to IPv4 and IPv6. + */ +static inline bool tcp_port_l3mdev_exempt(const struct net *net, + unsigned short port) +{ +#ifdef CONFIG_SYSCTL + if (net->ipv4.sysctl_tcp_l3mdev_exempt_ports) + return test_bit(port, net->ipv4.sysctl_tcp_l3mdev_exempt_ports); +#endif + return false; +} + __be32 inet_current_timestamp(void); /* From inetpeer.c */ diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h index 3efbc7f4b4f..899d90f34a7 100644 --- a/include/net/netns/ipv4.h +++ b/include/net/netns/ipv4.h @@ -271,6 +271,7 @@ struct netns_ipv4 { #ifdef CONFIG_SYSCTL unsigned long *sysctl_local_reserved_ports; + unsigned long *sysctl_tcp_l3mdev_exempt_ports; int sysctl_ip_prot_sock; #endif diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 860145d810e..063f6dd00a6 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -362,6 +362,11 @@ static inline int compute_score(struct sock *sk, const struct net *net, if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) return -1; + + if (!sk->sk_bound_dev_if && sdif && + tcp_port_l3mdev_exempt(net, hnum)) + return -1; + score = sk->sk_bound_dev_if ? 2 : 1; if (sk->sk_family == PF_INET) diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c index 9df93ad219d..83d0030512a 100644 --- a/net/ipv4/sysctl_net_ipv4.c +++ b/net/ipv4/sysctl_net_ipv4.c @@ -840,6 +840,13 @@ static const struct ctl_table ipv4_net_table[] = { .mode = 0644, .proc_handler = proc_do_large_bitmap, }, + { + .procname = "tcp_l3mdev_exempt_ports", + .data = &init_net.ipv4.sysctl_tcp_l3mdev_exempt_ports, + .maxlen = 65536, + .mode = 0644, + .proc_handler = proc_do_large_bitmap, + }, { .procname = "ip_no_pmtu_disc", .data = &init_net.ipv4.sysctl_ip_no_pmtu_disc, @@ -1708,10 +1715,16 @@ static __net_init int ipv4_sysctl_init_net(struct net *net) if (!net->ipv4.sysctl_local_reserved_ports) goto err_ports; + net->ipv4.sysctl_tcp_l3mdev_exempt_ports = kzalloc(65536 / 8, GFP_KERNEL); + if (!net->ipv4.sysctl_tcp_l3mdev_exempt_ports) + goto err_l3mdev_ports; + proc_fib_multipath_hash_set_seed(net, 0); return 0; +err_l3mdev_ports: + kfree(net->ipv4.sysctl_local_reserved_ports); err_ports: unregister_net_sysctl_table(net->ipv4.ipv4_hdr); err_reg: @@ -1729,6 +1742,7 @@ static __net_exit void ipv4_sysctl_exit_net(struct net *net) unregister_net_sysctl_table(net->ipv4.ipv4_hdr); kfree(table); kfree(net->ipv4.sysctl_local_reserved_ports); + kfree(net->ipv4.sysctl_tcp_l3mdev_exempt_ports); } static __net_initdata struct pernet_operations ipv4_sysctl_ops = { diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index 1eea584d80d..43c52b56e8b 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c @@ -141,6 +141,10 @@ static inline int compute_score(struct sock *sk, const struct net *net, if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) return -1; + if (!sk->sk_bound_dev_if && sdif && + tcp_port_l3mdev_exempt(net, hnum)) + return -1; + score = sk->sk_bound_dev_if ? 2 : 1; if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) score++; -- 2.43.0 From: Kalash Nainwal Check that with tcp_l3mdev_accept=1 and the port listed in tcp_l3mdev_exempt_ports, a VRF connection goes to the VRF-bound listener (in both listener creation orders) and is refused by an unbound-only listener, and that default-VRF connections still reach the unbound listener. Covers IPv4 and IPv6. Signed-off-by: Kalash Nainwal --- tools/testing/selftests/net/Makefile | 1 + .../selftests/net/tcp_l3mdev_exempt_ports.sh | 213 +++++++++++++++++++++ 2 files changed, 214 insertions(+) diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index c80dabbcc90..2a0b8453bfd 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -109,6 +109,7 @@ TEST_PROGS := \ stress_reuseport_listen.sh \ tcp_ecmp_failover.sh \ tcp_fastopen_backup_key.sh \ + tcp_l3mdev_exempt_ports.sh \ test_bridge_backup_port.sh \ test_bridge_neigh_suppress.sh \ test_ingress_egress_chaining.sh \ diff --git a/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh new file mode 100755 index 00000000000..f075abb9e8f --- /dev/null +++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh @@ -0,0 +1,213 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Test net.ipv4.tcp_l3mdev_exempt_ports. +# +# With tcp_l3mdev_accept=1, a listener that is not bound to a device also +# matches connections that arrive in a VRF. For a port listed in +# tcp_l3mdev_exempt_ports it must not: only a listener bound to the VRF +# may take such a connection. Connections in the default VRF are not +# affected. +# +# ns_c (client in VRF) ns_s (server) ns_d (client in default VRF) +# veth0 ------------- veth_vrf [vrf red] +# veth_def ----------------- veth0 + +source lib.sh + +PORT=12345 +OTHER_PORT=23456 +VRF=red +VRF_TABLE=1000 + +VRF_SRV4=172.16.1.1 +VRF_CLI4=172.16.1.2 +DEF_SRV4=172.16.2.1 +DEF_CLI4=172.16.2.2 +VRF_SRV6=2001:db8:1::1 +VRF_CLI6=2001:db8:1::2 +DEF_SRV6=2001:db8:2::1 +DEF_CLI6=2001:db8:2::2 + +TESTS=" + unbound_listener + vrf_and_unbound_listeners + default_vrf_unaffected +" + +cleanup() +{ + cleanup_all_ns +} + +setup() +{ + setup_ns ns_s ns_c ns_d || exit $ksft_skip + + ip -n "$ns_s" link add "$VRF" type vrf table "$VRF_TABLE" + ip -n "$ns_s" link set "$VRF" up + + ip -n "$ns_s" link add veth_vrf type veth peer name veth0 netns "$ns_c" + ip -n "$ns_s" link set veth_vrf master "$VRF" + ip -n "$ns_s" link set veth_vrf up + ip -n "$ns_s" addr add "$VRF_SRV4/24" dev veth_vrf + ip -n "$ns_s" addr add "$VRF_SRV6/64" dev veth_vrf nodad + ip -n "$ns_c" link set veth0 up + ip -n "$ns_c" addr add "$VRF_CLI4/24" dev veth0 + ip -n "$ns_c" addr add "$VRF_CLI6/64" dev veth0 nodad + + ip -n "$ns_s" link add veth_def type veth peer name veth0 netns "$ns_d" + ip -n "$ns_s" link set veth_def up + ip -n "$ns_s" addr add "$DEF_SRV4/24" dev veth_def + ip -n "$ns_s" addr add "$DEF_SRV6/64" dev veth_def nodad + ip -n "$ns_d" link set veth0 up + ip -n "$ns_d" addr add "$DEF_CLI4/24" dev veth0 + ip -n "$ns_d" addr add "$DEF_CLI6/64" dev veth0 nodad + + ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_accept=1 +} + +ipver() +{ + [ "$1" = "-6" ] && echo 6 || echo 4 +} + +set_exempt() +{ + ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_exempt_ports="$1" +} + +# Start a server. nettest sets SO_REUSEPORT on its listeners, so an +# unbound and a VRF-bound listener can share the port. +# $1: "-6" or "", $2: "vrf" or "unbound" +start_server() +{ + local dev_arg="" + + [ "$2" = "vrf" ] && dev_arg="-I $VRF" + # shellcheck disable=SC2086 + ip netns exec "$ns_s" ./nettest $1 -s $dev_arg -p "$PORT" -t 3 -q & +} + +# $1: client netns, $2: "-6" or "", $3: server address +connect() +{ + # shellcheck disable=SC2086 + ip netns exec "$1" ./nettest $2 -r "$3" -p "$PORT" -t 2 -q +} + +unbound_listener() +{ + local fam addr pid + + for fam in "" "-6"; do + addr=$VRF_SRV4 + [ "$fam" = "-6" ] && addr=$VRF_SRV6 + + RET=0 + set_exempt "$OTHER_PORT" + start_server "$fam" unbound + pid=$! + wait_local_port_listen "$ns_s" "$PORT" tcp + connect "$ns_c" "$fam" "$addr" + check_err $? "VRF connection not accepted by unbound listener" + wait $pid + log_test "IPv$(ipver "$fam"): not exempt, VRF conn to unbound listener accepted" + + RET=0 + set_exempt "$PORT" + start_server "$fam" unbound + pid=$! + wait_local_port_listen "$ns_s" "$PORT" tcp + connect "$ns_c" "$fam" "$addr" + check_fail $? "VRF connection accepted by unbound listener" + wait $pid + log_test "IPv$(ipver "$fam"): exempt, VRF conn to unbound listener rejected" + done + set_exempt "" +} + +# Both an unbound and a VRF-bound listener on the port. The VRF connection +# must reach the VRF-bound one, whichever listener was created first. +vrf_and_unbound_listeners() +{ + local fam addr order first second pid_first pid_second + local rc_first rc_second rc_vrf rc_unbound + + set_exempt "$PORT" + for fam in "" "-6"; do + addr=$VRF_SRV4 + [ "$fam" = "-6" ] && addr=$VRF_SRV6 + + for order in "unbound vrf" "vrf unbound"; do + read -r first second <<< "$order" + RET=0 + + start_server "$fam" "$first" + pid_first=$! + wait_local_port_listen "$ns_s" "$PORT" tcp + start_server "$fam" "$second" + pid_second=$! + sleep 0.2 + + connect "$ns_c" "$fam" "$addr" + check_err $? "VRF connection failed" + + wait $pid_first + rc_first=$? + wait $pid_second + rc_second=$? + if [ "$first" = "vrf" ]; then + rc_vrf=$rc_first + rc_unbound=$rc_second + else + rc_vrf=$rc_second + rc_unbound=$rc_first + fi + check_err $rc_vrf "VRF-bound listener did not get the connection" + check_fail $rc_unbound "unbound listener got the connection" + + log_test "IPv$(ipver "$fam"): exempt, $first first, VRF-bound listener wins" + done + done + set_exempt "" +} + +default_vrf_unaffected() +{ + local fam addr pid + + set_exempt "$PORT" + for fam in "" "-6"; do + addr=$DEF_SRV4 + [ "$fam" = "-6" ] && addr=$DEF_SRV6 + + RET=0 + start_server "$fam" unbound + pid=$! + wait_local_port_listen "$ns_s" "$PORT" tcp + connect "$ns_d" "$fam" "$addr" + check_err $? "default VRF connection not accepted" + wait $pid + check_err $? "unbound listener did not get the connection" + log_test "IPv$(ipver "$fam"): exempt, default VRF conn to unbound listener accepted" + done + set_exempt "" +} + +trap cleanup EXIT + +if [ ! -x ./nettest ]; then + echo "SKIP: nettest not built" + exit "$ksft_skip" +fi + +setup +if ! ip netns exec "$ns_s" test -e /proc/sys/net/ipv4/tcp_l3mdev_exempt_ports; then + echo "SKIP: net.ipv4.tcp_l3mdev_exempt_ports not supported" + exit "$ksft_skip" +fi + +tests_run + +exit "$EXIT_STATUS" -- 2.43.0