* [PATCH net v2] net: avoid clobbering active cork flowi union on datagram connect
@ 2026-11-24 18:06 Dominik Maier
2026-11-27 9:07 ` netdev-bot+sashiko
0 siblings, 1 reply; 3+ messages in thread
From: Dominik Maier @ 2026-11-24 18:06 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Kuniyuki Iwashima, Simon Horman, Willem de Bruijn, netdev,
dmnk+artist
ip6_datagram_dst_update() and __ip4_datagram_connect() use inet->cork.fl
(and np->final in IPv6) for route lookups to avoid stack-allocated flow
structures on the common connect path.
When a UDP or raw socket already has corked frames queued on
sk->sk_write_queue via UDP_CORK or MSG_MORE, calling connect() (or
refreshing the route in ip6_datagram_release_cb()) overwrites the active
union flowi in place. On a dual-stack AF_INET6 UDP socket with a corked
IPv4 datagram, an IPv6 connect() overwrites inet->cork.fl.u.ip4 with
flowi6 bytes, and uncorking sends the pending IPv4 packet with the
aliased IPv6 bytes as saddr without CAP_NET_RAW (and symmetrically for
a corked IPv6 datagram followed by a v4-mapped connect()).
Keep using inet->cork.fl and np->final when sk->sk_write_queue is empty,
and fall back to noinline_for_stack helpers with stack-allocated flow
keys when corked frames are queued. Once a UDP cork finishes on a
connected socket, restore the connected flow in inet->cork.fl (keeping
sk_dst_cache warm on normal IPv4 and IPv6 UDP paths, and resetting
sk_dst_cache only on cross-family IPv4 cork completion on a native-IPv6
socket or non-UDP datagram callers) so subsequent connected sends and
L2TP encapsulation never reuse the corked flow.
Fixes: 3038eeac027d ("ipv4: Lock socket and use cork flow in ip4_datagram_connect().")
Fixes: 4e6c91cf60f2 ("ipv6: use inet->cork.fl.u.ip6 and np->final in ip6_datagram_dst_update()")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
v2:
- Restore the connected flow in inet->cork.fl when a UDP cork finishes
(keeping sk_dst_cache warm on IPv4 and IPv6 UDP paths, and resetting
sk_dst_cache on cross-family or non-UDP datagram cases), keeping both
udp_sendmsg()'s lockless fast path and L2TP encapsulation from sending
with the corked flow.
- Add Fixes: 3038eeac027d for __ip4_datagram_connect() and move the
testing note below ---.
- v1: https://lore.kernel.org/netdev/20261119192147.2640881-1-dmnk@google.com/
Found with AI-assisted code review; tested in QEMU across dual-stack
IPv4/IPv6 cork + connect sequences and connected AF_INET UDP cork/send
fast paths (confirmed tail-call fast path with no stack frame growth).
include/net/ipv6.h | 1 +
include/net/udp.h | 21 +++++++++++++++++++++
net/ipv4/datagram.c | 32 +++++++++++++++++++++++++++-----
net/ipv4/udp.c | 8 ++++++--
net/ipv6/datagram.c | 33 +++++++++++++++++++++++++++------
net/ipv6/udp.c | 19 +++++++++++++++++--
6 files changed, 99 insertions(+), 15 deletions(-)
diff --git a/include/net/ipv6.h b/include/net/ipv6.h
index 3de07e738538..f3a066a4ddbf 100644
--- a/include/net/ipv6.h
+++ b/include/net/ipv6.h
@@ -1151,6 +1151,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,
int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr, int addr_len);
int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *addr,
int addr_len);
+void ip6_datagram_flow_key_init(struct flowi6 *fl6, const struct sock *sk);
int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
void ip6_datagram_release_cb(struct sock *sk);
diff --git a/include/net/udp.h b/include/net/udp.h
index 5bc25dcf25fb..cee5ebd1edec 100644
--- a/include/net/udp.h
+++ b/include/net/udp.h
@@ -417,6 +417,27 @@ INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *sk, struct msghdr *msg,
size_t len, int flags));
void udp_splice_eof(struct socket *sock);
int udp_push_pending_frames(struct sock *sk);
+static inline void udp_cork_restore_connected(struct sock *sk)
+{
+ struct inet_sock *inet = inet_sk(sk);
+ struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
+
+ if (sk->sk_state != TCP_ESTABLISHED)
+ return;
+
+#if IS_ENABLED(CONFIG_IPV6)
+ if (sk->sk_family == AF_INET6 &&
+ !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
+ sk_dst_reset(sk);
+ return;
+ }
+#endif
+ WRITE_ONCE(fl4->daddr, READ_ONCE(inet->inet_daddr));
+ WRITE_ONCE(fl4->saddr, inet->inet_saddr);
+ WRITE_ONCE(fl4->fl4_dport, inet->inet_dport);
+ WRITE_ONCE(fl4->fl4_sport, inet->inet_sport);
+}
+
void udp_flush_pending_frames(struct sock *sk);
int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
index 7d25519a6cdd..987676251db4 100644
--- a/net/ipv4/datagram.c
+++ b/net/ipv4/datagram.c
@@ -16,17 +16,17 @@
#include <net/tcp_states.h>
#include <net/sock_reuseport.h>
-int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
+static int ____ip4_datagram_connect(struct sock *sk,
+ struct sockaddr_unsized *uaddr,
+ int addr_len, struct flowi4 *fl4)
{
+ struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
struct inet_sock *inet = inet_sk(sk);
- struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
- struct flowi4 *fl4;
struct rtable *rt;
__be32 saddr;
int oif;
int err;
-
if (addr_len < sizeof(*usin))
return -EINVAL;
@@ -45,7 +45,6 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
} else if (!oif) {
oif = READ_ONCE(inet->uc_index);
}
- fl4 = &inet->cork.fl.u.ip4;
rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, oif,
sk->sk_protocol, inet->inet_sport,
usin->sin_port, sk);
@@ -82,6 +81,29 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
out:
return err;
}
+
+static noinline_for_stack int
+__ip4_datagram_connect_corked(struct sock *sk, struct sockaddr_unsized *uaddr,
+ int addr_len)
+{
+ struct flowi4 fl4;
+ int err;
+
+ err = ____ip4_datagram_connect(sk, uaddr, addr_len, &fl4);
+ if (!err && !sk_is_udp(sk))
+ sk_dst_reset(sk);
+ return err;
+}
+
+int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
+ int addr_len)
+{
+ if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
+ return __ip4_datagram_connect_corked(sk, uaddr, addr_len);
+
+ return ____ip4_datagram_connect(sk, uaddr, addr_len,
+ &inet_sk(sk)->cork.fl.u.ip4);
+}
EXPORT_SYMBOL(__ip4_datagram_connect);
int ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 2e5562f353f8..8fb1accc7fd4 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1034,8 +1034,9 @@ void udp_flush_pending_frames(struct sock *sk)
if (up->pending) {
up->len = 0;
- WRITE_ONCE(up->pending, 0);
ip_flush_pending_frames(sk);
+ udp_cork_restore_connected(sk);
+ WRITE_ONCE(up->pending, 0);
}
}
@@ -1213,6 +1214,7 @@ int udp_push_pending_frames(struct sock *sk)
out:
up->len = 0;
+ udp_cork_restore_connected(sk);
WRITE_ONCE(up->pending, 0);
return err;
}
@@ -1496,8 +1498,10 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
udp_flush_pending_frames(sk);
else if (!corkreq)
err = udp_push_pending_frames(sk);
- else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
+ else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) {
+ udp_cork_restore_connected(sk);
WRITE_ONCE(up->pending, 0);
+ }
release_sock(sk);
out:
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..d42398835084 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -38,8 +38,7 @@ static bool ipv6_mapped_addr_any(const struct in6_addr *a)
return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
}
-static void ip6_datagram_flow_key_init(struct flowi6 *fl6,
- const struct sock *sk)
+void ip6_datagram_flow_key_init(struct flowi6 *fl6, const struct sock *sk)
{
const struct inet_sock *inet = inet_sk(sk);
const struct ipv6_pinfo *np = inet6_sk(sk);
@@ -69,7 +68,8 @@ static void ip6_datagram_flow_key_init(struct flowi6 *fl6,
security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
}
-int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
+static int __ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr,
+ struct flowi6 *fl6, struct in6_addr *final)
{
struct ip6_flowlabel *flowlabel = NULL;
struct inet_sock *inet = inet_sk(sk);
@@ -77,7 +77,6 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
struct ipv6_txoptions *opt;
struct in6_addr *final_p;
struct dst_entry *dst;
- struct flowi6 *fl6;
int err = 0;
if (inet6_test_bit(SNDFLOW, sk) &&
@@ -86,12 +85,11 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
if (IS_ERR(flowlabel))
return -EINVAL;
}
- fl6 = &inet_sk(sk)->cork.fl.u.ip6;
ip6_datagram_flow_key_init(fl6, sk);
rcu_read_lock();
opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
- final_p = fl6_update_dst(fl6, opt, &np->final);
+ final_p = fl6_update_dst(fl6, opt, final);
rcu_read_unlock();
dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
@@ -119,6 +117,29 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
return err;
}
+static noinline_for_stack int
+ip6_datagram_dst_update_corked(struct sock *sk, bool fix_sk_saddr)
+{
+ struct in6_addr final;
+ struct flowi6 fl6;
+ int err;
+
+ err = __ip6_datagram_dst_update(sk, fix_sk_saddr, &fl6, &final);
+ if (!err && !sk_is_udp(sk))
+ sk_dst_reset(sk);
+ return err;
+}
+
+int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
+{
+ if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
+ return ip6_datagram_dst_update_corked(sk, fix_sk_saddr);
+
+ return __ip6_datagram_dst_update(sk, fix_sk_saddr,
+ &inet_sk(sk)->cork.fl.u.ip6,
+ &inet6_sk(sk)->final);
+}
+
void ip6_datagram_release_cb(struct sock *sk)
{
struct dst_entry *dst;
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index a6886a0432f9..344071574122 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -1267,6 +1267,17 @@ void udp_v6_early_demux(struct sk_buff *skb)
/*
* Throw away all pending data and cancel the corking. Socket is locked.
*/
+static void udp_v6_cork_restore_connected(struct sock *sk)
+{
+ if (sk->sk_state != TCP_ESTABLISHED)
+ return;
+
+ if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
+ udp_cork_restore_connected(sk);
+ else
+ ip6_datagram_flow_key_init(&inet_sk(sk)->cork.fl.u.ip6, sk);
+}
+
static void udp_v6_flush_pending_frames(struct sock *sk)
{
struct udp_sock *up = udp_sk(sk);
@@ -1275,8 +1286,9 @@ static void udp_v6_flush_pending_frames(struct sock *sk)
udp_flush_pending_frames(sk);
else if (up->pending) {
up->len = 0;
- WRITE_ONCE(up->pending, 0);
ip6_flush_pending_frames(sk);
+ udp_v6_cork_restore_connected(sk);
+ WRITE_ONCE(up->pending, 0);
}
}
@@ -1463,6 +1475,7 @@ static int udp_v6_push_pending_frames(struct sock *sk)
&inet_sk(sk)->cork.base);
out:
up->len = 0;
+ udp_v6_cork_restore_connected(sk);
WRITE_ONCE(up->pending, 0);
return err;
}
@@ -1749,8 +1762,10 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
udp_v6_flush_pending_frames(sk);
else if (!corkreq)
err = udp_v6_push_pending_frames(sk);
- else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
+ else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) {
+ udp_v6_cork_restore_connected(sk);
WRITE_ONCE(up->pending, 0);
+ }
if (err > 0)
err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net v2] net: avoid clobbering active cork flowi union on datagram connect
2026-11-24 18:06 [PATCH net v2] net: avoid clobbering active cork flowi union on datagram connect Dominik Maier
@ 2026-11-27 9:07 ` netdev-bot+sashiko
2026-11-30 16:04 ` Dominik Maier
0 siblings, 1 reply; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 9:07 UTC (permalink / raw)
To: dmnk
Cc: davem, edumazet, kuba, pabeni, kuniyu, horms,
willemdebruijn.kernel, netdev, dmnk+artist
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 2 · Low: 1
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).
- [High] udp_sendmsg()'s connected fast path does not take the socket
lock, and it still sends with whatever is stored in the shared…
- [High] Finishing an IPv4 cork on an AF_INET6 UDP socket connected to a
native IPv6 peer only calls sk_dst_reset() (include/net/udp.h:429-432).
- [Medium] For non-UDP datagram sockets, __ip4_datagram_connect_corked()
(net/ipv4/datagram.c) and ip6_datagram_dst_update_corked()…
- [Medium] L2TP UDP-encap transmit still uses the user tunnel socket's
inet->cork.fl as its working flow while the user has frames corked.
- [Low] The comment "Throw away all pending data and cancel the corking.
--
net: avoid clobbering active cork flowi union on datagram connect
This commit makes datagram connect() use stack-allocated flow keys when
frames are corked on sk_write_queue, so the active inet->cork.fl union
is not overwritten. When a UDP cork completes on a connected socket, it
also restores the connected flow into inet->cork.fl. In the
cross-family and non-UDP cases it resets sk_dst_cache instead.
> diff --git a/include/net/udp.h b/include/net/udp.h
> index 5bc25dcf25fba..cee5ebd1edec8 100644
> --- a/include/net/udp.h
> +++ b/include/net/udp.h
> @@ -417,6 +417,27 @@ INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *sk, struct msghdr *msg,
> size_t len, int flags));
> void udp_splice_eof(struct socket *sock);
> int udp_push_pending_frames(struct sock *sk);
> +static inline void udp_cork_restore_connected(struct sock *sk)
> +{
> + struct inet_sock *inet = inet_sk(sk);
> + struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
> +
> + if (sk->sk_state != TCP_ESTABLISHED)
> + return;
> +
> +#if IS_ENABLED(CONFIG_IPV6)
> + if (sk->sk_family == AF_INET6 &&
> + !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
> + sk_dst_reset(sk);
> + return;
> + }
[Severity: High]
Suppose an IPv4 cork completes on an AF_INET6 socket that is connected
to a native IPv6 peer. This branch only drops the dst. Is
inet->cork.fl.u.ip6 ever rebuilt in that case?
udp_sendmsg() wrote the corked IPv4 addresses and ports over the first
bytes of cork.fl.u.ip6.daddr:
fl4 = &inet->cork.fl.u.ip4;
fl4->daddr = daddr;
fl4->saddr = saddr;
fl4->fl4_dport = dport;
udp_v6_cork_restore_connected() does not run here either, because
udp_v6_push_pending_frames() returns early:
if (up->pending == AF_INET)
return udp_push_pending_frames(sk);
udp_v6_flush_pending_frames() likewise goes to
udp_flush_pending_frames().
The next connected native IPv6 send fills the cache again from its
stack flow:
udpv6_sendmsg()
ip6_sk_dst_lookup_flow()
ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6)
After that, an L2TP-over-UDPv6 transmit goes through
l2tp_xmit_core()->l2tp_xmit_queue()->inet6_csk_xmit(). Because
__sk_dst_check() now succeeds, inet6_csk_route_socket() is skipped and
ip6_xmit() builds hdr->daddr from the stale cork.fl.u.ip6.
That would keep sending the tunnel's data packets to a corrupted IPv6
destination until the dst is invalidated or the socket reconnects. It
also seems to contradict the commit message's statement that L2TP
encapsulation never reuses the corked flow.
Should this branch rebuild the flow, for example with
ip6_datagram_flow_key_init(), rather than only calling sk_dst_reset()?
> +#endif
> + WRITE_ONCE(fl4->daddr, READ_ONCE(inet->inet_daddr));
> + WRITE_ONCE(fl4->saddr, inet->inet_saddr);
> + WRITE_ONCE(fl4->fl4_dport, inet->inet_dport);
> + WRITE_ONCE(fl4->fl4_sport, inet->inet_sport);
> +}
[Severity: High]
Can this restore protect the lockless connected fast path in
udp_sendmsg()? That path checks pending once without the lock and then
reads the shared cork union:
udp_sendmsg() {
...
fl4 = &inet->cork.fl.u.ip4;
if (READ_ONCE(up->pending)) {
...
}
...
if (connected)
rt = dst_rtable(sk_dst_check(sk, 0));
if (!rt) {
...
fl4 = &fl4_stack;
...
}
...
saddr = fl4->saddr;
if (!ipc.addr)
daddr = ipc.addr = fl4->daddr;
/* Lockless fast path for the non-corking case. */
if (!corkreq) {
...
skb = ip_make_skb(sk, fl4, ...);
...
err = udp_send_skb(skb, fl4, &cork);
...
}
Other threads on the same socket write the union under lock_sock().
The writers are the IPv4 cork setup in udp_sendmsg(), the
"inet->cork.fl.u.ip6 = *fl6" copy in ip6_append_data(), and this
restore.
The restore only runs after a cork ends. Nothing orders it against the
READ_ONCE(up->pending) check, so a sender that has already passed that
check keeps reading the live union.
On a dual-stack AF_INET6 socket connected to a v4-mapped peer, this
sequence looks possible:
thread C: holds an IPv4 cork and uncorks it under lock_sock()
thread A: send()
udpv6_sendmsg() sees pending == AF_INET
udp_sendmsg() with msg_name == NULL
waits in lock_sock(), sees pending == 0 once C is done,
releases the lock and takes the connected fast path with the
cached IPv4 dst
thread B: sendto(native IPv6 X, MSG_MORE), with IPV6_PKTINFO naming a
native local source so ip6_dst_lookup_tail() accepts it
udpv6_sendmsg()->ip6_append_data()
inet->cork.fl.u.ip6 = *fl6;
thread A:
ip_make_skb()->ip_copy_addrs() and udp_send_skb() read
fl4.saddr = X[0..3], fl4.daddr = X[4..7], dport = X[8..9]
That sends an IPv4 packet with a source and destination the user chose,
over the connected peer's route and without CAP_NET_RAW. This is the
same result the commit message sets out to prevent.
The race was there before this patch. However, the v2 notes say the
restore keeps "udp_sendmsg()'s lockless fast path ... from sending with
the corked flow".
Would it be more robust for the lockless path to stop reading cork.fl,
for example by building the tuple from inet_daddr, inet_dport and
inet_saddr?
> +
> void udp_flush_pending_frames(struct sock *sk);
> int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
> void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst);
> diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
> index 7d25519a6cdd3..987676251db48 100644
> --- a/net/ipv4/datagram.c
> +++ b/net/ipv4/datagram.c
[ ... ]
> @@ -82,6 +81,29 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int
> out:
> return err;
> }
> +
> +static noinline_for_stack int
> +__ip4_datagram_connect_corked(struct sock *sk, struct sockaddr_unsized *uaddr,
> + int addr_len)
> +{
> + struct flowi4 fl4;
> + int err;
> +
> + err = ____ip4_datagram_connect(sk, uaddr, addr_len, &fl4);
> + if (!err && !sk_is_udp(sk))
> + sk_dst_reset(sk);
> + return err;
> +}
[Severity: Medium]
For UDP sockets, this leaves the new dst in sk_dst_cache, but
inet->cork.fl keeps the corked flow until the cork completes. Can L2TP
send data with the corked tuple during that window?
l2tp_xmit_core() only checks sock_owned_by_user(), which is false
between corked sendmsg() calls. It then does:
ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
With a cached dst, __ip_queue_xmit() goes straight to
ip_copy_addrs(iph, fl4) on cork.fl.u.ip4, and inet6_csk_xmit() calls
ip6_xmit() with cork.fl.u.ip6. The L2TP data packets would be built
from the corked tuple and sent over the newly connected route.
ip6_datagram_dst_update_corked() has the same window for UDPv6 tunnel
sockets. There, ip6_sk_dst_store_flow() caches the new dst while
cork.fl.u.ip6 keeps the corked flow.
In the cross-family case, the IPv4 saddr and daddr would come from the
corked IPv6 destination's bytes. That case is an AF_INET6 tunnel socket
with an IPv6 cork to X, followed by connect() to a v4-mapped peer.
Before this patch, a corked connect() wrote the connected flow into
cork.fl, so this particular window looks new.
A related problem already existed before this patch. A route miss
during any active cork makes __ip_queue_xmit() or
inet6_csk_route_socket() rewrite the active cork flow in place. Also,
with a valid cached dst, a sendto(other, MSG_MORE) cork sends L2TP data
to "other".
The v2 notes say the patch keeps L2TP encapsulation "from sending with
the corked flow". Isn't that only true once the cork has finished?
> +
> +int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
> + int addr_len)
> +{
> + if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
> + return __ip4_datagram_connect_corked(sk, uaddr, addr_len);
> +
> + return ____ip4_datagram_connect(sk, uaddr, addr_len,
> + &inet_sk(sk)->cork.fl.u.ip4);
> +}
> EXPORT_SYMBOL(__ip4_datagram_connect);
[ ... ]
> diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
> index ff7a7ffbf9833..d423988350848 100644
> --- a/net/ipv6/datagram.c
> +++ b/net/ipv6/datagram.c
[ ... ]
> @@ -119,6 +117,29 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
> return err;
> }
>
> +static noinline_for_stack int
> +ip6_datagram_dst_update_corked(struct sock *sk, bool fix_sk_saddr)
> +{
> + struct in6_addr final;
> + struct flowi6 fl6;
> + int err;
> +
> + err = __ip6_datagram_dst_update(sk, fix_sk_saddr, &fl6, &final);
> + if (!err && !sk_is_udp(sk))
> + sk_dst_reset(sk);
> + return err;
> +}
[Severity: Medium]
For non-UDP sockets, does this reset only postpone the clobber on an
L2TP IP-encap tunnel socket (l2tp_ip6, IPPROTO_L2TP)?
l2tp_ip6_sendmsg() corks through ip6_append_data(). That stores the
flow in inet->cork.fl.u.ip6, and l2tp_ip6_push_pending_frames() uses it
at uncork time.
Consider a connect() that runs while frames are queued:
l2tp_ip6_connect()
__ip6_datagram_connect()
ip6_datagram_dst_update()
ip6_datagram_dst_update_corked()
sk_dst_reset()
For a v4-mapped peer, which l2tp_ip6_connect() allows, the path goes
through __ip4_datagram_connect_corked() instead. In both cases the next
kernel L2TP data transmit calls l2tp_xmit_core()->l2tp_xmit_queue()
with &inet->cork.fl.
With no cached dst, inet6_csk_xmit() calls inet6_csk_route_socket() on
&cork.fl.u.ip6, which does:
memset(fl6, 0, sizeof(*fl6));
fl6->flowi6_proto = sk->sk_protocol;
fl6->daddr = sk->sk_v6_daddr;
In the v4-mapped case, __ip_queue_xmit() calls inet_sk_init_flowi4()
and ip_route_output_flow() on the overlapping fl4.
On uncork, wouldn't the queued IPv6 frames be finished from the
rewritten flow? They would go to the newly connected peer, or, in the
v4-mapped case, to a destination built from aliased IPv4 bytes.
> +
> +int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
> +{
> + if (unlikely(!skb_queue_empty(&sk->sk_write_queue)))
> + return ip6_datagram_dst_update_corked(sk, fix_sk_saddr);
> +
> + return __ip6_datagram_dst_update(sk, fix_sk_saddr,
> + &inet_sk(sk)->cork.fl.u.ip6,
> + &inet6_sk(sk)->final);
> +}
> +
> void ip6_datagram_release_cb(struct sock *sk)
> {
> struct dst_entry *dst;
> diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
> index a6886a0432f97..344071574122c 100644
> --- a/net/ipv6/udp.c
> +++ b/net/ipv6/udp.c
> @@ -1267,6 +1267,17 @@ void udp_v6_early_demux(struct sk_buff *skb)
> /*
> * Throw away all pending data and cancel the corking. Socket is locked.
> */
> +static void udp_v6_cork_restore_connected(struct sock *sk)
> +{
[Severity: Low]
This isn't a bug, but the "Throw away all pending data and cancel the
corking" comment now sits above udp_v6_cork_restore_connected()
instead of udp_v6_flush_pending_frames(). Should the new helper go
above the comment, as net/ipv4/udp.c still has it?
> + if (sk->sk_state != TCP_ESTABLISHED)
> + return;
> +
> + if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
> + udp_cork_restore_connected(sk);
> + else
> + ip6_datagram_flow_key_init(&inet_sk(sk)->cork.fl.u.ip6, sk);
> +}
> +
> static void udp_v6_flush_pending_frames(struct sock *sk)
> {
> struct udp_sock *up = udp_sk(sk);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124180604.73257-1-dmnk%40google.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net v2] net: avoid clobbering active cork flowi union on datagram connect
2026-11-27 9:07 ` netdev-bot+sashiko
@ 2026-11-30 16:04 ` Dominik Maier
0 siblings, 0 replies; 3+ messages in thread
From: Dominik Maier @ 2026-11-30 16:04 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Dominik Maier, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Kuniyuki Iwashima, Simon Horman, Willem de Bruijn,
netdev, dmnk+artist
On Fri, 27 Nov 2026 09:07:47 +0000, netdev-bot+sashiko@kernel.org wrote:
> - [High] udp_sendmsg()'s connected fast path does not take the socket
> lock, and it still sends with whatever is stored in the shared…
> - [High] Finishing an IPv4 cork on an AF_INET6 UDP socket connected to a
> native IPv6 peer only calls sk_dst_reset() (include/net/udp.h:429-432).
> - [Medium] For non-UDP datagram sockets, __ip4_datagram_connect_corked()
> (net/ipv4/datagram.c) and ip6_datagram_dst_update_corked()…
> - [Medium] L2TP UDP-encap transmit still uses the user tunnel socket's
> inet->cork.fl as its working flow while the user has frames corked.
> - [Low] The comment "Throw away all pending data and cancel the corking.
Thanks, all five points are valid. Sending v3 with:
- udp_sendmsg()'s lockless connected fast path filling fl4_stack from
the socket fields instead of reading shared inet->cork.fl.u.ip4;
- AF_INET6 UDP restoring inet->cork.fl.u.ip6 (and np->final) when an
IPv4 cork completes on a native-IPv6-connected socket;
- l2tp_xmit_core() skipping transmit while sk_write_queue is non-empty,
alongside sock_owned_by_user();
- the udp_v6_flush_pending_frames() comment kept directly above that
function.
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 16:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:06 [PATCH net v2] net: avoid clobbering active cork flowi union on datagram connect Dominik Maier
2026-11-27 9:07 ` netdev-bot+sashiko
2026-11-30 16:04 ` Dominik Maier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox