From nobody Tue Nov 24 16:51:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795214840; b=15Ar21HdSlEp5yyG1WO6H8bzYDzdBorTFWamNh2Z8pm14Dx26+YR784JmOnn8n6LAdVv RimnAN3SIUenA7H/V1gQSRU6nRqu5Jf8MTRDtoF7tY2aFNIL3XGaqSlImXD1QsokKoVSJ F95FrSO+sC3szxMdKug5HuPGwa/XGpF2S32bsJO6jCAKgX/kw8Ago/WHv0oGo4XRhXLf/ IMPT8UZQ3MasVNNdtNjquioUleuRcXXr24YDTlPFhpp/dnwOjTYNHjNqRlDgZ4jPo6OOK r7XCiArRPgxpGKLP82smjsQ4nsIBhJh9MzkD7grzEi+ClJNSA/J6voN70bCqE+eM1dw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795214840; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=ibmGnkWpCk1ySH29hUtKouFOmchlIp92zZIoz8M3D1M=; b=AFRJqaLrB6jhV+OSEVDwpMplvLn3Bwr8I6r8bn0LpndGnM82XfxJRRG0LH7H9MxWuWjU Zkm+M06XTg8EGiYbUBkOpwqAqUNFKbsAY9u7YJPblXjqm1hJEn5ULWgEUyfVllaVtt8Uw LaWN3A+dBy9e6bP1AhZKgZg6k36GMNS3Uj7uGFPUSZjd9BGa74FY146QchSnUs4lJN662 3lXkRSMVRjzXmUEL2Q1j8iHbEXOiZK0O5jIbUhTSrCPC2CSlNwUfxo/2A7muBKx6d2wGZ jRmWiDu5M1W40X+T3G4S67nyB5CgJEdCoOMBvlYasxaB7rP8qXGhpchoIt/BQlk2fhA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bry9DOL3; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C12A606A68B; Fri, 20 Nov 2026 22:47:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bry9DOL3 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795214839; b=EPj0rcRGjifLaG1Jxtbcml6vU+cMcwWkgfFFiDU3NWtRMKnaDhMtJ6N1jBcAtVKAjXPB +HPI9qv2jH0WD71I7bOK1GqFoXWVZH+2cX6QxwFLt+ih0FQesCgyn0sLXCUH4sCR1DDXu 1D+vffvEAkISH+2QFkB8NATLwEhat/cItIGvp8nKYtzJkP2FvwTBT2c0wWmXLvOcp/o3o xZdMsaSkWVnl+fG1Kiv7JN+Za53I7EWc2YTEZxHGjne8zSlI+T9sceKe/BUs3QMbZqYUz tMq/cbaHwrIbedFbFK70urXj8FcINtFF9E+DWmDFUtxnDk8D4mn6Tqtk31e4atl40ng== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795214839; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=ibmGnkWpCk1ySH29hUtKouFOmchlIp92zZIoz8M3D1M=; b=zNMPGL6acz6OaQ5avcyiWthCDF4TghGqTsX7M9dLqg4lfEtgAsHyxoQ9BaY+Jq0VRxMR k5IaSp1S/qQCOkrVIZhMrQtBTJ7RZ8CyiHaEAxaWD21rxCnSbxvZrRA5EJznK7pqHCMm7 VbYPGZd9KbIfAP7jEwFI0LdOlftqOD5kI5vyT5j8vFw7M1nB9eAO7iBKGGiUgGLw5WFT9 DEULLo+geH+mIxm6lj5o87RsVpYhsc06LRpbcOUNpUT8uVt40u2hID/VTR3zcDloQ2U68 FhsfOCGVeYw0RMq2yYpTF0Ub+pUx//2t47cTs0s0YmwqdOdBQXTrLncPbqzbcR8x/ZQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 091C11F000D2; Fri, 20 Nov 2026 22:47:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795214839; bh=ibmGnkWpCk1ySH29hUtKouFOmchlIp92zZIoz8M3D1M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=bry9DOL30tVGR9gecXFg64JdeDWS0IuZKMQcVEaSCGUzWCAhBPK5ijvumMG0nLcvN 9nxfVKFKYX2GmyQlWMXjkZj67BVNpFeoV7gAnUoqOJI7/2unnoBkEtpiC/Pzh6mLJj mrx7jfsLarNNt/Ci1neCz9THdLNx4xeJYPCds251MZL5o3vkDFuwOVaajwWCyDFVig x0eif91l/XQcJWnX5ORq+EaH1DkToi9XIKeYjeBs3FlIvGKjTr76Vn7FJ6lXo6fZZ5 nRB/AAjq2HOxMO8HYjCVJYAAg3LkN+RBOr+fJinUYa8noxBEiKAUzYsZXMajntuJZn h5M75WGZ8SCKA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 7AC634BA4C; Fri, 20 Nov 2026 22:47:06 +0000 (UTC) From: Kalash Nainwal via B4 Relay Date: Fri, 20 Nov 2026 15:47:05 -0700 Subject: [PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261120-tcp-l3mdev-exempt-ports-v1-1-ccc2a3a6cd09@nexthop.ai> References: <20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09@nexthop.ai> In-Reply-To: <20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09@nexthop.ai> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , David Ahern , Ido Schimmel , Neal Cardwell , Kuniyuki Iwashima Cc: netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Kalash Nainwal X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6449; i=kalash@nexthop.ai; h=from:subject:message-id; bh=fge2VqvDTzCL4vkfcc2/Isy48/QGoz89laMvzHs6z7U=; b=owEB7QES/pANAwAKAdTKslu1X9B2AcsmYgBrAM3p/bhC7/61T3tuKXc7PadU0EisdV+PNqnlY kmxGTBYd6WJAbMEAAEKAB0WIQSWKdfHnHcAucb08LDUyrJbtV/QdgUCawDN6QAKCRDUyrJbtV/Q dpwbC/9wbi57/yCvWFVnu8hG63SWQ1CwqTln+ghvRO+cSd/yOouLFDWnSEomZ11Mm9gsw/lHvew x9s38cXuG0muUYXR9Oje/+wCeDF2w0Ob/wRQLxDYFf/rw7XCXliML5BrU8t3vytjE3jMZFnvV5T FybYpcMg/zmOoEVkVRTvEaPkDA76a5KHTBAafwHa2pnDXvgN6jKK6xK25ech6FfUTQf64xFXoyx IcoiubWlIMKD678zAxBKQo/l7G9F8fUBjnUM83jeH5wYYsBSW9BtW0nF7KtuC44KzcEWjbbbOns rKvDr1QqH+zoFatTyyJ2J7+gwwqjAoDn5lAjS4Zg2AwE/L5hVhpNmYawiuAFfqd8IqepObdWbxm YZ4LAF08vZ1MGBelcNXZMc/j85QlWuw8TXT+Fij4Ln4bVY+RRd0ZCo2Lkh+0DLxmahyUrpL/+gb r8pcxri5luOBXHK9ojQT8GD6tjJvM4uIzQI6W2oVrV0ZRzk5Bc/ru55KlXy7OHvXkmsok= X-Developer-Key: i=kalash@nexthop.ai; a=openpgp; fpr=9629D7C79C7700B9C6F4F0B0D4CAB25BB55FD076 X-Endpoint-Received: by B4 Relay for kalash@nexthop.ai/default with auth_id=1588 X-Original-From: Kalash Nainwal Reply-To: kalash@nexthop.ai Content-Transfer-Encoding: quoted-printable From: Kalash Nainwal With tcp_l3mdev_accept=3D1, 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/network= ing/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 =20 Default: 0 (disabled) =20 +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. =20 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(str= uct net *net, unsigned sho } #endif =20 +/* 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); =20 /* 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 { =20 #ifdef CONFIG_SYSCTL unsigned long *sysctl_local_reserved_ports; + unsigned long *sysctl_tcp_l3mdev_exempt_ports; int sysctl_ip_prot_sock; #endif =20 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, =20 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 =3D sk->sk_bound_dev_if ? 2 : 1; =20 if (sk->sk_family =3D=3D 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[] =3D { .mode =3D 0644, .proc_handler =3D proc_do_large_bitmap, }, + { + .procname =3D "tcp_l3mdev_exempt_ports", + .data =3D &init_net.ipv4.sysctl_tcp_l3mdev_exempt_ports, + .maxlen =3D 65536, + .mode =3D 0644, + .proc_handler =3D proc_do_large_bitmap, + }, { .procname =3D "ip_no_pmtu_disc", .data =3D &init_net.ipv4.sysctl_ip_no_pmtu_disc, @@ -1708,10 +1715,16 @@ static __net_init int ipv4_sysctl_init_net(struct n= et *net) if (!net->ipv4.sysctl_local_reserved_ports) goto err_ports; =20 + net->ipv4.sysctl_tcp_l3mdev_exempt_ports =3D kzalloc(65536 / 8, GFP_KERNE= L); + if (!net->ipv4.sysctl_tcp_l3mdev_exempt_ports) + goto err_l3mdev_ports; + proc_fib_multipath_hash_set_seed(net, 0); =20 return 0; =20 +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 ne= t *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); } =20 static __net_initdata struct pernet_operations ipv4_sysctl_ops =3D { 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; =20 + if (!sk->sk_bound_dev_if && sdif && + tcp_port_l3mdev_exempt(net, hnum)) + return -1; + score =3D sk->sk_bound_dev_if ? 2 : 1; if (READ_ONCE(sk->sk_incoming_cpu) =3D=3D raw_smp_processor_id()) score++; --=20 2.43.0 From nobody Tue Nov 24 16:51:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795214833; b=nA+2I+eEaXeqPtQDxkoCyLjAwG0MP0Ib7wSHsrWAUgb2+3z3MNuUPahmO8XTZOU1WgEf HbsEJpx89ixIm9au2UaV8DVCqy4fp9y40OyNvPDXs1s8jHWjw9Gw6w/77ayMNNmV9evZq wEUGGM3D3UpL23yr7w5f1udALh8iSVjrxK4hK1CN00QL1TFWu3hIS6Ez6dBQnkaNlIcX5 itJKsMA8CDhWe4WU5aEFb8PiKqUvlij9V3MNlNDpcOyogpMuIOdi2ZrhrB+2s0gQXTUAM b8TaFmkjR/7EJJ/jDL87EWNLpLxQBzMg6KV1rD8Gn+ecrW3z1xx2W8/YB+L2cUHa7kQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795214833; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=sQk3umOpv10DrnEpRLSYg0JUGXu/IQfMVXqwisTX700=; b=gXfQLwIwNYULy0bugplaTIft1s7YHL/l24BtwpVcec7ph3Dq2oI//A94dns4ILGXpFP9 OgvOqWAi2vIW+Lp89zq5mn4LV3SV8I9j46fLRsFyMZ+xKKQp1u99BTPyuQph8szNdxjp1 08AvYVn+jZcMmuN/57I14hkeQxLMGNhPpHsjVFpzL3K8x9PjspzerPBgrm+Kf5sjZbomg yCXakbHHADieVZLvJ+Y8qvbaTpgaLfpes6DWhVMWSxLJfbtkN2NYsaMkFVoyCcpUhRiHM u5k5xifU8FzePyx+d40Lf3/16xei9jcgbiFpEaxvVC4DOAO4qNAJ8rjqlxUneNQWSLw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UEiAoM5q; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDBDE606A69F; Fri, 20 Nov 2026 22:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UEiAoM5q Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795214833; b=k2Tw0ppKHH4J9WHZzRhwT3dhq7OXORgrJfGDjmY1Huc/TqZOJPbfGEJ5CwDBmhz8Ru3H hZ2oPNm3Qxs7pj6BlOapWVlP1FSH4SAuScL8lnL7Md/ezMsayXMuffVQ9YJGFr/WxhRGJ LKIKQrVYEk4WfqE94sFvCj8wZe44azpMsTJUeuCtvTQWpo/BPu8u7X/M8XxXQWMvHp4UD bNQ6MglZ7wa4K1AXZVqdVYwqYb9TIyg9Qt8tKmzlIW5eTuFCewem0xynQzpuUzT1fkJlK lKLOVAqv/TJcgVz3hFnUL52DB+66fm+G6spq+ovD//xuIbCpgyQS+iFHdce9FwzuPGA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795214833; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=sQk3umOpv10DrnEpRLSYg0JUGXu/IQfMVXqwisTX700=; b=v123Hb9VAbvYqIr4LWWBWIxfJDVswuRnYIIlrO3a6rFs8RtbbzGT0hAqmOnmnfwNUpfX 7Aa5I+FC893cUY+EZFeUCR3OqYPcKGOdPINJW0I1560DcRTGIAd2l8s12eETqUSalRJcn C2rL8CM3QD70itgSkRXMdv7VaWMTTjubj8l1b77L0JmM6llwNh9T39IpH5iJl1Jf/Z/cC +B3SBp8IXPaQFEiQWJpCjEqTTdEZuRMyBoWQtY1jc7iWIrv8s0Cj07T5oD9KJjdmitZXL iIVmgShOaQS2IWfJxl2Ybhn0ad9bNUsYn1CFXkwb0+x97EIUTgxa3faGBlyufd3K5wA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 19DF91F000D3; Fri, 20 Nov 2026 22:47:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795214833; bh=sQk3umOpv10DrnEpRLSYg0JUGXu/IQfMVXqwisTX700=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=UEiAoM5qTvr0iSY81kibgejWRRbD5NDmt/h/zKIm4qyG6Zvs4q23/2LG+Devgk2U7 gqJTTX9C0GXREopWqIxzEnuy9xmSou1XG+GgGKJ5fweBTKX8ftROEtnS9d6sTh4gk/ QTigrQYB4LGd2RM6cac6dPiFdDQx2QF6GOVLZqdT8SrK2HKSxJgoBjKPwvDInLZIBE Ij4Bk2dfbctuHXtoLGPcAVubZ0kbBu8aHYGJPNqtA5nQgzfg4dgzEiFcgSwatdCpL4 DSlhlKr1Jcivpa3vwr6BOhxaQQ8NvxI3agAgs9UBBbT5AlebE95d9afN168DyZNbCO VXvC0huONC/UA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 963494BA65; Fri, 20 Nov 2026 22:47:06 +0000 (UTC) From: Kalash Nainwal via B4 Relay Date: Fri, 20 Nov 2026 15:47:06 -0700 Subject: [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261120-tcp-l3mdev-exempt-ports-v1-2-ccc2a3a6cd09@nexthop.ai> References: <20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09@nexthop.ai> In-Reply-To: <20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09@nexthop.ai> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , David Ahern , Ido Schimmel , Neal Cardwell , Kuniyuki Iwashima Cc: netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Kalash Nainwal X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7022; i=kalash@nexthop.ai; h=from:subject:message-id; bh=R6KTlYSmq1uGZkz3MBtbOKA3zSyYsCV0/QsHaQ3Fk+0=; b=owEB7QES/pANAwAKAdTKslu1X9B2AcsmYgBrAM3pAST6voGxbnPGaT8u6hs00fOprOQr0YwwL +FJ6jCnKTmJAbMEAAEKAB0WIQSWKdfHnHcAucb08LDUyrJbtV/QdgUCawDN6QAKCRDUyrJbtV/Q dkL5C/9mWc9H7+7qkzzUto3wpJID7vwg26Q5C2w1dQ5WEBU5Yfrbx8OGpDQioGQLTVnXwgO7EKr y++eqnjrQ2Pb5VUz+fDUCt9S1FQVddtugi1ACRYnn93JNmeuOLZeLY88HOH3k55iUIrZ/PsYdfU dPwgV6dTz0GYNu+iKGnxn9YpyedN6gtZ1YDrGXBR1m3Y+IYu5V116Tb6SDYN0NTKdzLDSvsm5As sGtp8oCoOHy0YBmxJp3O+AvGt2+2F6xP0M6xViR+YK1WfhdFgci6NXG2LBgDPx9xRldSH6amsmI 7T8E2wWPkQFrKXD+6X48u8m/HWmbGSqycArLyLktP2zOhDj34bPG8tf61DMa2QnQfkfzEORMrRK dXbFy7VhJvswRDS/abArPXVioPXcLs6ELbA6Sc+huux+8kCbXPX4i27iL9w0mnzk7Ww5kwiOEiP doBhWvpIVpPG/FvWgEaaq2ICVQAC3mH/nClDK0yEhkr8ewcUvet2lM/jolNWhTA/sDdNo= X-Developer-Key: i=kalash@nexthop.ai; a=openpgp; fpr=9629D7C79C7700B9C6F4F0B0D4CAB25BB55FD076 X-Endpoint-Received: by B4 Relay for kalash@nexthop.ai/default with auth_id=1588 X-Original-From: Kalash Nainwal Reply-To: kalash@nexthop.ai Content-Transfer-Encoding: quoted-printable From: Kalash Nainwal Check that with tcp_l3mdev_accept=3D1 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 :=3D \ 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=3D1, 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 de= fault VRF) +# veth0 ------------- veth_vrf [vrf red] +# veth_def ----------------- veth0 + +source lib.sh + +PORT=3D12345 +OTHER_PORT=3D23456 +VRF=3Dred +VRF_TABLE=3D1000 + +VRF_SRV4=3D172.16.1.1 +VRF_CLI4=3D172.16.1.2 +DEF_SRV4=3D172.16.2.1 +DEF_CLI4=3D172.16.2.2 +VRF_SRV6=3D2001:db8:1::1 +VRF_CLI6=3D2001:db8:1::2 +DEF_SRV6=3D2001:db8:2::1 +DEF_CLI6=3D2001:db8:2::2 + +TESTS=3D" + 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=3D1 +} + +ipver() +{ + [ "$1" =3D "-6" ] && echo 6 || echo 4 +} + +set_exempt() +{ + ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_exempt_ports=3D"$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=3D"" + + [ "$2" =3D "vrf" ] && dev_arg=3D"-I $VRF" + # shellcheck disable=3DSC2086 + 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=3DSC2086 + 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=3D$VRF_SRV4 + [ "$fam" =3D "-6" ] && addr=3D$VRF_SRV6 + + RET=3D0 + set_exempt "$OTHER_PORT" + start_server "$fam" unbound + pid=3D$! + 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 a= ccepted" + + RET=3D0 + set_exempt "$PORT" + start_server "$fam" unbound + pid=3D$! + 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 rejec= ted" + 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=3D$VRF_SRV4 + [ "$fam" =3D "-6" ] && addr=3D$VRF_SRV6 + + for order in "unbound vrf" "vrf unbound"; do + read -r first second <<< "$order" + RET=3D0 + + start_server "$fam" "$first" + pid_first=3D$! + wait_local_port_listen "$ns_s" "$PORT" tcp + start_server "$fam" "$second" + pid_second=3D$! + sleep 0.2 + + connect "$ns_c" "$fam" "$addr" + check_err $? "VRF connection failed" + + wait $pid_first + rc_first=3D$? + wait $pid_second + rc_second=3D$? + if [ "$first" =3D "vrf" ]; then + rc_vrf=3D$rc_first + rc_unbound=3D$rc_second + else + rc_vrf=3D$rc_second + rc_unbound=3D$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=3D$DEF_SRV4 + [ "$fam" =3D "-6" ] && addr=3D$DEF_SRV6 + + RET=3D0 + start_server "$fam" unbound + pid=3D$! + 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 listen= er 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_po= rts; then + echo "SKIP: net.ipv4.tcp_l3mdev_exempt_ports not supported" + exit "$ksft_skip" +fi + +tests_run + +exit "$EXIT_STATUS" --=20 2.43.0