Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Kalash Nainwal via B4 Relay
Fri, 20 Nov 2026 22:47
[PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
Author: Kalash Nainwal via B4 Relay
Date: Fri, 20 Nov 2026 22:47
Date: Fri, 20 Nov 2026 22:47
175 lines
17325 bytes
17325 bytes
From: Kalash Nainwal <kalash@nexthop.ai>
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 <kalash@nexthop.ai>
---
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
Re: [PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
Author: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 23:25
Date: Tue, 24 Nov 2026 23:25
282 lines
18850 bytes
18850 bytes
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 4
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The commit message describes a bug in TCP listener lookup, and
the patch works around it with a new opt-in UAPI knob instead of…
- [Medium] The documentation and code comment promise more than the code
does.
- [Low] tcp_l3mdev_exempt_ports only changes how tcp_l3mdev_accept
behaves, but it is not config-gated the way that sysctl is.
- [Low] The patch adds `unsigned long *sysctl_tcp_l3mdev_exempt_ports` to
struct netns_ipv4 (include/net/netns/ipv4.h:274) but does not update…
- [Low] The new ip-sysctl.rst text says that for a listed port "only a
listen socket bound to the L3 master device" matches VRF connections.
- [Low] Updates to the new per-netns bitmap are not atomic with respect
to the TCP listener lookup that reads it.
--
tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
This adds a per-netns port bitmap sysctl that uses the same format as
ip_local_reserved_ports. When tcp_l3mdev_accept=1, an unbound TCP listener
on a listed port no longer matches connections that arrive on an L3 master
device slave, so only a VRF-bound listener can take them. The check is
added to the IPv4 and IPv6 listener compute_score() functions.
> diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
> index 3cc80d9b024b1..1de0ee5c0d5b5 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
[Severity: Medium]
Is "tcp_l3mdev_accept is ignored" accurate? The comment above
tcp_port_l3mdev_exempt() in include/net/ip.h makes a similar claim, that
listed ports "behave as if tcp_l3mdev_accept were 0".
The only new check is in the two listener compute_score() functions. Other
users of sysctl_tcp_l3mdev_accept still apply the global value to listed
ports.
Established and request socket matching in inet_match() and inet6_match()
goes through inet_sk_bound_dev_eq(), which never looks at the bitmap. An
unbound established socket on a listed port still matches segments with
the same 4-tuple that arrive on a VRF slave.
Bind buckets are keyed on inet_sk_bound_l3mdev():
include/net/inet_sock.h:inet_sk_bound_l3mdev() {
...
if (!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
return l3mdev_master_ifindex_by_index(net,
sk->sk_bound_dev_if);
...
}
With tcp_l3mdev_accept=1 this returns 0. The callers are
inet_csk_get_port(), inet_csk_find_open_port(), __inet_inherit_port(),
__inet_hash_connect() and __inet_bhash2_update_saddr(). So an unbound
socket and a VRF-bound socket on a listed port still share one bind
bucket. They still fail with EADDRINUSE unless SO_REUSEPORT is set. With
tcp_l3mdev_accept=0 they would land in separate buckets.
inet_request_bound_dev_if() also still uses tcp_l3mdev_accept when it
sets the child's bound device.
Should the documentation and the comment say this only covers listener
lookup? Or should the exemption also apply to these paths?
> + 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
[Severity: Low]
Is "only a listen socket bound to the L3 master device does" accurate?
The new check only filters out sockets with sk_bound_dev_if == 0. For
bound sockets, inet_bound_dev_eq() still does:
return bound_dev_if == dif || bound_dev_if == sdif;
That means a listener bound to the enslaved ingress device (dif) can
also match these connections.
> + are not affected. This applies to both IPv4 and IPv6.
> +
[ ... ]
> diff --git a/include/net/ip.h b/include/net/ip.h
> index 5035b313a26b0..c6581080a9a47 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);
[Severity: Low]
compute_score() reads this bitmap without a lock in softirq context. The
writer, proc_do_large_bitmap(), takes no lock either and updates the
bitmap in place:
kernel/sysctl.c:proc_do_large_bitmap() {
...
if (*ppos)
bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
else
bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
...
}
If the written list is longer than PAGE_SIZE-1 bytes, only the first
chunk is parsed. That chunk replaces the whole bitmap and the write
returns short. The rest comes in a later write() at *ppos > 0 and is
ORed in.
Consider a port that is in both the old and the new list but appears late
in the string. Between those two write() calls, could a SYN for that port
arriving in a VRF be accepted by the unbound listener, without the TCP-MD5
or TCP-AO key that the VRF-bound listener has?
Two concurrent writers could also interleave and leave a mix of both
lists.
> +#endif
> + return false;
> +}
[ ... ]
> diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
> index 3efbc7f4b4f7d..899d90f34a716 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;
[Severity: Low]
This isn't a bug, but should
Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst be updated
for the new field? That file lists the neighbouring
sysctl_local_reserved_ports and sysctl_ip_prot_sock fields, and lists
sysctl_tcp_l3mdev_accept as RX read_mostly.
The new pointer is read on the TCP RX listener lookup path. It sits in
the CONFIG_SYSCTL area, not in the netns_ipv4_read_rx group, which is
where net_namespace.c asserts sysctl_tcp_l3mdev_accept lives.
> int sysctl_ip_prot_sock;
> #endif
>
> diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
> index 860145d810ed4..063f6dd00a64d 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;
[Severity: Medium]
The commit message describes a bug in the listener lookup itself. Would
it be better to fix the lookup than to add an opt-in knob?
In inet_lhash2_lookup(), and likewise in inet6_lhash2_lookup(), the
first socket with score > hiscore that belongs to a reuseport group is
returned immediately:
if (score > hiscore) {
result = inet_lookup_reuseport(net, sk, skb, doff,
saddr, sport, daddr, hnum, inet_ehashfn);
if (result)
return result;
Sockets later in the bucket are never checked, even if they would score
higher.
inet_reuseport_add_sock() only groups sockets with the same bound device:
sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
So an unbound listener and a VRF-bound listener are always in different
reuseport groups, and whichever group comes first in the bucket wins.
__inet_hash() adds AF_INET6 reuseport sockets at the tail and everything
else at the head. That is why IPv4 and IPv6 end up picking different
listeners.
The new sysctl is empty by default. Unless the admin knows to list the
port, the IPv4/IPv6 inconsistency remains. So does the TCP-MD5/AO bypass
for IPv6 VRF peers that the commit message describes.
The check is also broader than the problem requires. For a listed port,
it rejects the unbound listener for traffic from every VRF, including
VRFs that have no bound listener of their own. A setup with a dedicated
listener for VRF A and an unbound listener serving the other VRFs on the
same port can't be configured.
Could the commit message explain why the lookup wasn't fixed instead?
A lookup fix would also need a scoring change. The sk_incoming_cpu bonus
can tie a bound IPv4 listener (2+1=3) with an unbound IPv4 listener on
the matching CPU (1+1+1=3).
> +
> 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 9df93ad219db8..83d0030512aa5 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,
> + },
[Severity: Low]
Should this entry be under #ifdef CONFIG_NET_L3_MASTER_DEV, as
tcp_l3mdev_accept is? Its documentation says it is "Only valid when the
kernel was compiled with CONFIG_NET_L3_MASTER_DEV". The documentation for
tcp_l3mdev_exempt_ports doesn't mention that dependency.
Without CONFIG_NET_L3_MASTER_DEV, inet_sdif(), tcp_v4_sdif() and
tcp_v6_sdif() always return 0. The new check in both compute_score()
functions can then never fire.
ipv4_sysctl_init_net() still allocates the bitmap for every netns:
net->ipv4.sysctl_tcp_l3mdev_exempt_ports = kzalloc(65536 / 8, GFP_KERNEL);
So on those kernels the knob accepts writes, has no effect, and costs
8 KiB per network namespace.
> {
> .procname = "ip_no_pmtu_disc",
> .data = &init_net.ipv4.sysctl_ip_no_pmtu_disc,
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09%40nexthop.ai
Re: [PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
Author: Kalash Nainwal
Date: Wed, 25 Nov 2026 21:15
Date: Wed, 25 Nov 2026 21:15
45 lines
13445 bytes
13445 bytes
Thanks for the review. v2 addresses these as follows. > [Medium] fix the lookup instead of adding a sysctl I kept the sysctl. With both listeners present, today's result is stable but depends on creation order and address family. Making the VRF-bound listener always win would change that for every tcp_l3mdev_accept user and may break some existing clients, so v2 keeps the default and makes the new behavior user configurable per port. That said, I can change the default lookup instead if maintainers prefer that. The per-port scope is intended: on a listed port, a VRF with no listener of its own refuses the connection instead of falling back to the unbound listener. The sk_incoming_cpu tie does not apply to listed ports: the unbound socket returns -1 before any score is added. > [Medium] the docs say more than the code does Fixed. The docs now say that only listener lookup changes, and that established lookup and bind() conflict checks still follow tcp_l3mdev_accept. > [Low] a listener bound to the receiving device also matches Fixed in the docs. > [Low] CONFIG_NET_L3_MASTER_DEV Fixed. The sysctl, field and bitmap now exist only with CONFIG_NET_L3_MASTER_DEV, next to tcp_l3mdev_accept. > [Low] net_cachelines documentation Fixed. > [Low] bitmap updates are not atomic This is the same handler and behavior as ip_local_reserved_ports, and it only matters for a single write longer than a page. I left it as is.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads