From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.55 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795167411; b=agEsf6aO6YFbksLA5PpODGKSuGST3+/MAGfvWF1Uee1Jt1NlJERo521e/V71pfWVejMd 48BFa5ySli3dkucdzLCsyB5naigw/qOHkv5GTyJRC46Yu3cKOpKYhNRBL5U4Rg9u0DE1Y NirdRMM3YoGegFz8OYvWiQqxmwz65XGO7iF368Pdti8iJX9vAQUhM4h3rs77ymSRgJE5X d2Fc0aCWqrZ8lAlKiRWC4dRdH929WS4fjJkLuids9KFoQijaKGli4SMEhBdRwOTqBO/24 QdyGQGlyEenEa4JMqCv17giDE768I6sVmH4/gLy6aMuX1hxkaNoDBcJ7cQ6IubjhViA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795167411; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=fkGVvY6GduZYzaBDvNKY22wv/gwUUfpELzvQO+W4DW4=; b=R/Sh5LX36uyK3/dG6arH89GbZGoyN5QOgmQYs/Es4Ch4NVUTwuCTnYcf28N2pUzfsVo6 S9fdxrXV/lE0SlWA5X25klYqrSdEeP5Kdrg6M7c0xaonV6LS0T75sEe+tP9C9pqStWQPy qrUFota2WJxTXsO6T8oYz23sP8ryeBG8GJKifaIzjiHe2VBfR1MVveIwJVrjRwinWaH1L NBbclSSdcyCvsFqNakk1rJm8ip+vPt3M4R7OMmAkyjtBwyyqfbDsT7Pk27uT1Jit6MZ0e nnDjLgm5rGaX1swtnE1XcHLquTUd3yW2tKMQmjXpNP9mxOrievMO9S4KZRLuCX6xjWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=dYW6C3Hn; arc=none smtp.remote-ip=195.10.208.55 Received: from mout-b-110.mailbox.org (mout-b-110.mailbox.org [195.10.208.55]) (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.subspace.kernel.org (Postfix) with ESMTPS id A66A6606A32C; Fri, 20 Nov 2026 09:36:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A66A6606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=dYW6C3Hn Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-110.mailbox.org (Postfix) with ESMTPS id 4jW9G66DQRzNkR2; Fri, 20 Nov 2026 10:36:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1795167406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fkGVvY6GduZYzaBDvNKY22wv/gwUUfpELzvQO+W4DW4=; b=dYW6C3HnEaar4dKNW5KjUODHeOayDoLKlTUlA3BIbF/YNVQSV2cwitSLf58TEM8btue/JG SOSYSLBfT2JjqTkYGvk1l6OVvXXOFhOtsmurcPZVN0rVKlWFoeiDB4nVGiPjM1O5MuMCYB fJBoYDkNR+/Z1EvVb4Gplf1Pc+2taocMhPnDez7OPYaiMbS3wEC34475PrvlSaue8LzL/k 3KTslgtEyVBHHnSJoOjDJYHqhdgtecLFmyQza8JryjRLZck7vF4K+fuVOzpVzaOerMKPfX OE04DW7vMg8GFCpWr8nuo5EwbIcMaR4ET9mBbRf2xSet6/9UlFR0TLf5f/oQ+A== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=ralf@mandelbit.com From: Ralf Lici To: netdev@vger.kernel.org Cc: David Ahern , Ido Schimmel , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Neal Cardwell , Kuniyuki Iwashima , Willem de Bruijn , Christian Brauner , Xia Yan , Jeff Layton , Sebastian Andrzej Siewior , Qi Tang , Steffen Klassert , Herbert Xu , Florian Westphal , Sabrina Dubroca , Antonio Quartulli , linux-kernel@vger.kernel.org Subject: [PATCH net-next v4 5/5] ipv6: use lookup cookies in selected socket dst cache paths Date: Fri, 20 Nov 2026 10:36:20 +0100 Message-ID: <039fb276f1c0cb9ac4542c79ea89d8127e6b9602.1795166508.git.ralf@mandelbit.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: References: 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-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4jW9G66DQRzNkR2 tcp_v6_connect, ip6_datagram_dst_update and ip6_sk_dst_lookup_flow obtain a dst and sample its validation cookie afterwards. If a matching xfrm policy is inserted between lookup and cookie sampling, the plain route can be cached with a post-insertion serial. Subsequent packets then pass dst validation and bypass the new policy until another relevant FIB change. Store the cookie returned by ip6_dst_lookup_flow_cookie in these paths instead. Add ip6_dst_store_cookie to accept the supplied cookie, retaining ip6_dst_store for unconverted callers. This is a partial conversion. Established TCP route refills, header rebuilds and accepted-socket setup still use late cookie sampling. Those paths and other persistent caches, including SCTP and dst_cache users, are left for follow-up conversions. This race was found while investigating how mutable socket routing properties affect the dst cache in ovpn's UDP TX path. The socket-cache case was then reproduced in a VM by adding a debug-only kernel delay after the route and policy lookup, then inserting a matching xfrm policy before cookie sampling. TCP connect and connected UDP sockets continued sending plaintext after the policy insertion. With lookup cookies, the stale cached route was rejected on reuse. Signed-off-by: Ralf Lici --- Changes since v3 https://lore.kernel.org/netdev/60907e12930f38ba6c45c4ccfdca7abee068b765.1794906743.git.ralf@mandelbit.com/ - Drop the Fixes tag: this targets net-next and no backport is requested. (Sashiko) Changes since v2 https://lore.kernel.org/all/43917b9119e13e54dcde0e2941d3f4a1fe255df7.1794384146.git.ralf@mandelbit.com/ - Add a Fixes tag referencing the initial git import. (Sashiko) - Clearly state that this is only a partial conversion to the new API. No changes since v1 https://lore.kernel.org/netdev/832892eaa7d2371f20459c3134c72914e42970b0.1793806941.git.ralf@mandelbit.com/ include/net/ip6_route.h | 18 +++++++++++++----- net/ipv6/datagram.c | 6 ++++-- net/ipv6/ip6_output.c | 6 ++++-- net/ipv6/route.c | 12 ++++++------ net/ipv6/tcp_ipv6.c | 5 +++-- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h index 95f936be32c6..2684f6069a5e 100644 --- a/include/net/ip6_route.h +++ b/include/net/ip6_route.h @@ -252,13 +252,13 @@ static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb) /* * Store a destination cache entry in a socket */ -static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst, - bool daddr_set, - bool saddr_set) +static inline void ip6_dst_store_cookie(struct sock *sk, struct dst_entry *dst, + u32 cookie, bool daddr_set, + bool saddr_set) { struct ipv6_pinfo *np = inet6_sk(sk); - np->dst_cookie = rt6_get_cookie(dst_rt6_info(dst)); + np->dst_cookie = cookie; sk_setup_caps(sk, dst); np->daddr_cache = daddr_set; #ifdef CONFIG_IPV6_SUBTREES @@ -266,8 +266,16 @@ static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst, #endif } +static inline void ip6_dst_store(struct sock *sk, struct dst_entry *dst, + bool daddr_set, + bool saddr_set) +{ + ip6_dst_store_cookie(sk, dst, rt6_get_cookie(dst_rt6_info(dst)), + daddr_set, saddr_set); +} + void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, - const struct flowi6 *fl6); + const struct flowi6 *fl6, u32 cookie); static inline bool ipv6_unicast_destination(const struct sk_buff *skb) { diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c index ff7a7ffbf983..c18609bab366 100644 --- a/net/ipv6/datagram.c +++ b/net/ipv6/datagram.c @@ -78,6 +78,7 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) struct in6_addr *final_p; struct dst_entry *dst; struct flowi6 *fl6; + u32 cookie; int err = 0; if (inet6_test_bit(SNDFLOW, sk) && @@ -94,7 +95,8 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) final_p = fl6_update_dst(fl6, opt, &np->final); rcu_read_unlock(); - dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p); + dst = ip6_dst_lookup_flow_cookie(sock_net(sk), sk, fl6, final_p, + &cookie); if (IS_ERR(dst)) { err = PTR_ERR(dst); goto out; @@ -112,7 +114,7 @@ int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr) } } - ip6_sk_dst_store_flow(sk, dst, fl6); + ip6_sk_dst_store_flow(sk, dst, fl6, cookie); out: fl6_sock_release(flowlabel); diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 083ffdc8f404..7edb63299eb5 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1376,6 +1376,7 @@ struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, bool connected) { struct dst_entry *dst; + u32 cookie; if (connected) { dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie); @@ -1384,9 +1385,10 @@ struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, return dst; } - dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst); + dst = ip6_dst_lookup_flow_cookie(sock_net(sk), sk, fl6, final_dst, + &cookie); if (connected && !IS_ERR(dst)) - ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6); + ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6, cookie); return dst; } diff --git a/net/ipv6/route.c b/net/ipv6/route.c index 4c02c5168163..91847ef480ac 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -3050,19 +3050,19 @@ void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) } void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, - const struct flowi6 *fl6) + const struct flowi6 *fl6, u32 cookie) { #ifdef CONFIG_IPV6_SUBTREES struct ipv6_pinfo *np = inet6_sk(sk); #endif - ip6_dst_store(sk, dst, - ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr), + ip6_dst_store_cookie(sk, dst, cookie, + ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr), #ifdef CONFIG_IPV6_SUBTREES - ipv6_addr_equal(&fl6->saddr, &np->saddr) ? - true : + ipv6_addr_equal(&fl6->saddr, &np->saddr) ? + true : #endif - false); + false); } static bool ip6_redirect_nh_match(const struct fib6_result *res, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 9e22ed61a6de..9dc031437b42 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -145,6 +145,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr, struct dst_entry *dst; struct flowi6 *fl6; int addr_type; + u32 cookie; int err; if (addr_len < SIN6_LEN_RFC2133) @@ -288,7 +289,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr, */ ip6_ecmp_set_mp_hash(net, fl6, sk->sk_txhash); - dst = ip6_dst_lookup_flow(net, sk, fl6, final_p); + dst = ip6_dst_lookup_flow_cookie(net, sk, fl6, final_p, &cookie); if (IS_ERR(dst)) { err = PTR_ERR(dst); goto failure; @@ -312,7 +313,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr, inet->inet_rcv_saddr = LOOPBACK4_IPV6; sk->sk_gso_type = SKB_GSO_TCPV6; - ip6_dst_store(sk, dst, false, false); + ip6_dst_store_cookie(sk, dst, cookie, false, false); icsk->icsk_ext_hdr_len = 0; if (opt) -- 2.56.0