Netdev List
 help / color / mirror / Atom feed
* [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err().
@ 2026-11-17  5:35 Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT Kuniyuki Iwashima
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

A packetdrill-equipped LLM found a bunch of bugs with repro.

This is the first series fixing bugs in tcp_v4_err() and
tcp_v6_err().

Patches 1-5, 7, and 8 are fixes, and Patch 6 is a selftest
for the bugs fixed by Patches 1-5.


Kuniyuki Iwashima (8):
  tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.
  tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().
  tcp: Don't bypass GTSM check for reqsk.
  tcp: Fix reqsk abort condition on ICMP errors.
  tcp: Check ip_sk_accept_pmtu() in tcp_v4_err().
  selftest: net: packetdrill: Add ICMP and ICMPv6 tests.
  tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().
  tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err().

 net/ipv4/tcp_ipv4.c                           |  42 ++++---
 net/ipv6/tcp_ipv6.c                           |  33 ++++--
 .../net/packetdrill/tcp_icmp_ipv4.pkt         |  98 ++++++++++++++++
 .../net/packetdrill/tcp_icmp_ipv6.pkt         | 111 ++++++++++++++++++
 4 files changed, 253 insertions(+), 31 deletions(-)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt

-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err() Kuniyuki Iwashima
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

RFC 5927 states that the acceptable ICMP has seq in
SND.UNA =< SEG.SEQ < SND.NXT:

  These implementations check that the TCP sequence number
  contained in the payload of the ICMP error message is within
  the range SND.UNA =< SEG.SEQ < SND.NXT.

tcp_v4_err() and tcp_v6_err() accept packets if seq meets

  between(seq, snd_una, tp->snd_nxt)
  -> tp->snd_nxt - snd_una >= seq - snd_una

, which includes both ends.

This is fine once connection is established because
tcp_acceptable_seq() uses SND_NXT for ACK and RST.

However, we should not accept ICMP to SND_NXT for SYN_SENT.

The minimal repro is:

   --ip_version=ipv4
    0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
   +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
   +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
   +0 > S 0:0(0) <...>
   +0 < icmp unreachable host_unreachable [1:1(0)]
   +0 %{assert tcpi_state == TCP_SYN_SENT}%

Let's fix off-by-one.

Note that we need to check seq != snd_una because tp->snd_nxt
is only advanced to tp->write_seq after tcp_transmit_skb().
Otherwise, ICMP for SYN could be dropped before tcp_connect()
advances tp->snd_nxt.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 net/ipv4/tcp_ipv4.c | 9 ++++++---
 net/ipv6/tcp_ipv6.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 2fc92a7884fc..7d58f522068d 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -499,8 +499,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 	const int type = icmp_hdr(skb)->type;
 	const int code = icmp_hdr(skb)->code;
 	struct request_sock *fastopen;
+	u32 seq, snd_una, snd_nxt;
 	struct tcp_sock *tp;
-	u32 seq, snd_una;
 	struct sock *sk;
 	int err;
 
@@ -555,9 +555,12 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 	tp = tcp_sk(sk);
 	/* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
 	fastopen = rcu_dereference(tp->fastopen_rsk);
-	snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
+	snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : READ_ONCE(tp->snd_una);
+	snd_nxt = READ_ONCE(tp->snd_nxt);
 	if (sk->sk_state != TCP_LISTEN &&
-	    !between(seq, snd_una, tp->snd_nxt)) {
+	    (!between(seq, snd_una, snd_nxt) ||
+	     (sk->sk_state == TCP_SYN_SENT &&
+	      seq != snd_una && seq == snd_nxt))) {
 		__NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
 		goto out;
 	}
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 5c7ed38f1f56..8691d220cf94 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -410,9 +410,9 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 	const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
 	struct net *net = dev_net_rcu(skb->dev);
 	struct request_sock *fastopen;
+	__u32 seq, snd_una, snd_nxt;
 	struct ipv6_pinfo *np;
 	struct tcp_sock *tp;
-	__u32 seq, snd_una;
 	struct sock *sk;
 	bool fatal;
 	int err;
@@ -463,9 +463,12 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 	tp = tcp_sk(sk);
 	/* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
 	fastopen = rcu_dereference(tp->fastopen_rsk);
-	snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
+	snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : READ_ONCE(tp->snd_una);
+	snd_nxt = READ_ONCE(tp->snd_nxt);
 	if (sk->sk_state != TCP_LISTEN &&
-	    !between(seq, snd_una, tp->snd_nxt)) {
+	    (!between(seq, snd_una, snd_nxt) ||
+	     (sk->sk_state == TCP_SYN_SENT &&
+	      seq != snd_una && seq == snd_nxt))) {
 		__NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
 		goto out;
 	}
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk Kuniyuki Iwashima
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev,
	Stephen Hemminger, stephen hemminger

RFC 5082 introduced Generalized TTL Security Mechanism (GTSM),
and IP_MINTTL and IPV6_MINHOPCOUNT follow that.

If an incoming IPv4 packet has TTL less than inet_sk(sk)->min_ttl
configured via IP_MINTTL, the packet is dropped.

However, tcp_v4_err() checks the TTL extracted from the ICMP
payload, not the one in the outer IP header.

The minimal repro is:

  --ip_version=ipv4
   0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
  +0 setsockopt(3, SOL_IP, 21, [250], 4) = 0
  +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
  +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
  +0 > S 0:0(0) <...>
  +0 < (ttl 10) icmp unreachable host_unreachable [0:0(0)]
  +0 %{assert tcpi_state == TCP_SYN_SENT}%

Let's check ip_hdr(skb)->ttl.

Now GTSM is applied to locally generated ICMP errors, but this
behaviour matches tcp_v6_err().

Note that tcp_v6_err() properly checks ipv6_hdr(skb)->hop_limit.

Fixes: 97e3ecd112ba ("TCP: check min TTL on received ICMP packets")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
Cc: Stephen Hemminger <stephen@networkplumber.org>
Cc: stephen hemminger <shemminger@vyatta.com>
---
 net/ipv4/tcp_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 7d58f522068d..bf8542368032 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -546,7 +546,7 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 
 	if (static_branch_unlikely(&ip4_min_ttl)) {
 		/* min_ttl can be changed concurrently from do_ip_setsockopt() */
-		if (unlikely(iph->ttl < READ_ONCE(inet_sk(sk)->min_ttl))) {
+		if (unlikely(ip_hdr(skb)->ttl < READ_ONCE(inet_sk(sk)->min_ttl))) {
 			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
 			goto out;
 		}
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk.
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err() Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors Kuniyuki Iwashima
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

Since cited commits moved up TCP_NEW_SYN_RECV handling in
tcp_v4_err() and tcp_v6_err(), ICMP messages bypass IP_MINTTL
and IPV6_MINHOPCOUNT checks.

The minimal repro is:

  --ip_version=ipv6
   0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
  +0 setsockopt(3, SOL_IPV6, 73, [255], 4) = 0
  +0 bind(3, ..., ...) = 0
  +0 listen(3, 10) = 0
  +0 < (ttl 255) S 0:0(0) win 65535 <mss 1440,nop,nop,sackOK>
  +0 > S. 0:0(0) ack 1 <mss 1440,nop,nop,sackOK>
  +0 < (ttl 1) icmp unreachable port_unreachable [0:0(0)]
  +0 < (ttl 255) . 1:1(0) ack 1 win 65535
  +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
  +0 accept(3, ..., ...) = 4
  +0 %{assert tcpi_state == TCP_ESTABLISHED}%

Let's restore IP_MINTTL/IPV6_MINHOPCOUNT for reqsk.

Fixes: 26e3736090e1 ("ipv4: tcp: handle ICMP messages on TCP_NEW_SYN_RECV request sockets")
Fixes: 2215089b2244 ("ipv6: tcp: handle ICMP messages on TCP_NEW_SYN_RECV request sockets")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 net/ipv4/tcp_ipv4.c | 19 +++++++++++--------
 net/ipv6/tcp_ipv6.c | 19 +++++++++++--------
 2 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index bf8542368032..fd9588bba967 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -516,6 +516,17 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 		inet_twsk_put(inet_twsk(sk));
 		return 0;
 	}
+
+	if (static_branch_unlikely(&ip4_min_ttl)) {
+		/* min_ttl can be changed concurrently from do_ip_setsockopt() */
+		if (unlikely(ip_hdr(skb)->ttl <
+			     READ_ONCE(inet_sk(sk_to_full_sk(sk))->min_ttl))) {
+			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
+			sock_gen_put(sk);
+			return 0;
+		}
+	}
+
 	seq = ntohl(th->seq);
 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
 		tcp_req_err(sk, seq, type == ICMP_PARAMETERPROB ||
@@ -544,14 +555,6 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 	if (sk->sk_state == TCP_CLOSE || inet_test_bit(DEFER_CONNECT, sk))
 		goto out;
 
-	if (static_branch_unlikely(&ip4_min_ttl)) {
-		/* min_ttl can be changed concurrently from do_ip_setsockopt() */
-		if (unlikely(ip_hdr(skb)->ttl < READ_ONCE(inet_sk(sk)->min_ttl))) {
-			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
-			goto out;
-		}
-	}
-
 	tp = tcp_sk(sk);
 	/* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
 	fastopen = rcu_dereference(tp->fastopen_rsk);
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 8691d220cf94..0f1523fff939 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -433,6 +433,17 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 		inet_twsk_put(inet_twsk(sk));
 		return 0;
 	}
+
+	if (static_branch_unlikely(&ip6_min_hopcount)) {
+		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
+		if (ipv6_hdr(skb)->hop_limit <
+		    READ_ONCE(tcp_inet6_sk(sk_to_full_sk(sk))->min_hopcount)) {
+			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
+			sock_gen_put(sk);
+			return 0;
+		}
+	}
+
 	seq = ntohl(th->seq);
 	fatal = icmpv6_err_convert(type, code, &err);
 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
@@ -452,14 +463,6 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 	if (sk->sk_state == TCP_CLOSE || inet_test_bit(DEFER_CONNECT, sk))
 		goto out;
 
-	if (static_branch_unlikely(&ip6_min_hopcount)) {
-		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
-		if (ipv6_hdr(skb)->hop_limit < READ_ONCE(tcp_inet6_sk(sk)->min_hopcount)) {
-			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
-			goto out;
-		}
-	}
-
 	tp = tcp_sk(sk);
 	/* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
 	fastopen = rcu_dereference(tp->fastopen_rsk);
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors.
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  2026-11-17  5:35 ` [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err() Kuniyuki Iwashima
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

RFC 1122 Section 4.2.3.9 states the following soft errors
must not abort connections:

  o    Destination Unreachable -- codes 0, 1, 5

       Since these Unreachable messages indicate soft error
       conditions, TCP MUST NOT abort the connection, and it
       SHOULD make the information available to the
       application.

  o    Time Exceeded -- codes 0, 1
  o    Parameter Problem

       This should be handled the same way as Destination
       Unreachable codes 0, 1, 5 (see above).

Commit 9cf7490360bf ("tcp: do not drop syn_recv on all icmp
reports") introduced the abort arg to tcp_req_err() to avoid
aborting connection on ICMP_REDIRECT.

However, tcp_v4_err() still abort on soft errors (ICMP_NET_UNREACH
(0), ICMP_HOST_UNREACH (1)) and ignore hard errors.

For example, we can see SYN+ACK retransmitted after receiving
hard error:

  --ip_version=ipv4
   0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
  +0 bind(3, ..., ...) = 0
  +0 listen(3, 10) = 0

  +0 < S 0:0(0) win 65535 <mss 1460,nop,nop,sackOK>
  +0 > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK>
  +0 < icmp unreachable port_unreachable [0:0(0)]
  +1 > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK>

Also, tcp_v6_err() uses icmpv6_err_convert(), which handles
ICMPV6_PARAMPROB messages as hard error while RFC 5461 defines
as soft error.

But we should handle ICMPV6_UNK_NEXTHDR as hard error because
it is used as IPv6 version ICMP_PROT_UNREACH.

Let's correct the abort condition in tcp_v4_err() and
tcp_v6_err().

Note that icmpv6_err_convert() is used by other protocols.

Fixes: 9cf7490360bf ("tcp: do not drop syn_recv on all icmp reports")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 net/ipv4/tcp_ipv4.c | 7 ++-----
 net/ipv6/tcp_ipv6.c | 3 ++-
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index fd9588bba967..f11e090d04fb 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -529,11 +529,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 
 	seq = ntohl(th->seq);
 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
-		tcp_req_err(sk, seq, type == ICMP_PARAMETERPROB ||
-				     type == ICMP_TIME_EXCEEDED ||
-				     (type == ICMP_DEST_UNREACH &&
-				      (code == ICMP_NET_UNREACH ||
-				       code == ICMP_HOST_UNREACH)));
+		tcp_req_err(sk, seq, (type == ICMP_DEST_UNREACH &&
+				      icmp_err_convert[code].fatal));
 		return 0;
 	}
 
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 0f1523fff939..29fd0a084c91 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -447,7 +447,8 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 	seq = ntohl(th->seq);
 	fatal = icmpv6_err_convert(type, code, &err);
 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
-		tcp_req_err(sk, seq, fatal);
+		tcp_req_err(sk, seq, type == ICMPV6_PARAMPROB ?
+			    code == ICMPV6_UNK_NEXTHDR : fatal);
 		return 0;
 	}
 
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err().
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
                   ` (3 preceding siblings ...)
  2026-11-17  5:35 ` [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests Kuniyuki Iwashima
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev,
	Hannes Frederic Sowa

Currently, tcp_v4_mtu_reduced() checks ip_sk_accept_pmtu()
after calling inet_csk_update_pmtu(), which is too late.

Even if we configure a socket not to perform Path MTU
Discovery with IP_PMTUDISC_INTERFACE, ICMP_FRAG_NEEDED
can overwrite MTU.

The last getsockopt() returns 1200 MTU:

  --ip_version=ipv4
   0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
  +0 bind(3, ..., ...) = 0
  +0 listen(3, 1) = 0
  +0 < S 0:0(0) win 65535 <mss 1460,sackOK,nop,nop,nop,wscale 8>
  +0 > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK,nop,wscale 9>
  +0 < . 1:1(0) ack 1 win 257
  +0 accept(3, ..., ...) = 4
  +0 setsockopt(4, SOL_IP, IP_MTU_DISCOVER, [4], 4) = 0
  +0 getsockopt(4, SOL_IP, IP_MTU, [1500], [4]) = 0
  +0 write(4, ..., 1000) = 1000
  +0 > P. 1:1001(1000) ack 1
  +0 < icmp unreachable frag_needed mtu 1200 [1:1001(1000)]
  +0 getsockopt(4, SOL_IP, IP_MTU, [1500], [4]) = 0

Let's check ip_sk_accept_pmtu() in tcp_v4_err() as done
in tcp_v6_err().

Note that inet_sk(sk)->pmtudisc is configured locklessly,
so we do not recheck it in tcp_v4_mtu_reduced().  This is
same with tcp_v6_mtu_reduced() too.

Fixes: 482fc6094afa ("ipv4: introduce new IP_MTU_DISCOVER mode IP_PMTUDISC_INTERFACE")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
---
 net/ipv4/tcp_ipv4.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index f11e090d04fb..bc909fedda3a 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -391,7 +391,6 @@ void tcp_v4_mtu_reduced(struct sock *sk)
 		WRITE_ONCE(sk->sk_err_soft, EMSGSIZE);
 
 	if (inet->pmtudisc != IP_PMTUDISC_DONT &&
-	    ip_sk_accept_pmtu(sk) &&
 	    inet_csk(sk)->icsk_pmtu_cookie > dmtu) {
 		tcp_sync_mss(sk, dmtu);
 
@@ -588,6 +587,9 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 			if (sk->sk_state == TCP_LISTEN)
 				goto out;
 
+			if (!ip_sk_accept_pmtu(sk))
+				goto out;
+
 			WRITE_ONCE(tp->mtu_info, info);
 			if (!sock_owned_by_user(sk)) {
 				tcp_v4_mtu_reduced(sk);
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests.
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
                   ` (4 preceding siblings ...)
  2026-11-17  5:35 ` [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err() Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err() Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err() Kuniyuki Iwashima
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

tcp_icmp_ipv4.pkt and tcp_icmp_ipv6.pkt cover the scenarios
fixed by the preceding patches.

tcp_v6_err() already checks the outer hop limit and
ip6_sk_accept_pmtu(), so Test 2 and 5 in tcp_icmp_ipv6.pkt
pass without the series.

ICMP and ICMPv6 behaviour are unlikely to change, so splitting
into multiple files per test case is not worth it.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 .../net/packetdrill/tcp_icmp_ipv4.pkt         |  98 ++++++++++++++++
 .../net/packetdrill/tcp_icmp_ipv6.pkt         | 111 ++++++++++++++++++
 2 files changed, 209 insertions(+)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt

diff --git a/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt b/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt
new file mode 100644
index 000000000000..122bf9a14862
--- /dev/null
+++ b/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0
+--ip_version=ipv4
+
+`./defaults.sh`
+
+// Test 1: Client in TCP_SYN_SENT rejects ICMP quoting seq == snd_nxt (RFC 5927 4.1)
+// In SYN_SENT, SND.UNA = ISN, SND.NXT = ISN + 1.
+// The sequence number in ICMP must satisfy SND.UNA <= SEG.SEQ < SND.NXT.
+// An ICMP message quoting seq == 1 (snd_nxt) must be dropped, while seq == 0 (snd_una) aborts.
+    0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
+   +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
+   +0 > S 0:0(0) <...>
+   // Out-of-window ICMP quoting seq == 1 (snd_nxt) must be dropped
+   +0 < icmp unreachable host_unreachable [1:1(0)]
+   // Socket stays in TCP_SYN_SENT, so SYN retransmission occurs after 1s
+   +1 > S 0:0(0) <...>
+   // In-window ICMP quoting seq == 0 (snd_una) is accepted and aborts connection
+   +0 < icmp unreachable host_unreachable [0:0(0)]
+   +0 %{ assert tcpi_state == TCP_CLOSE }%
+   +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [EHOSTUNREACH], [4]) = 0
+   +0 close(3) = 0
+
+// Test 2: Client with IP_MINTTL (21) checks outer IP TTL in tcp_v4_err()
+// An ICMP with outer TTL < min_ttl (250) must be dropped, while outer TTL >= min_ttl
+// must be accepted even though the quoted inner IP header has lower TTL (64).
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_IP, 21, [250], 4) = 0
+   +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
+   +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
+   +0 > S 0:0(0) <...>
+   // Outer TTL 10 < min_ttl (250): dropped, socket stays in TCP_SYN_SENT
+   +0 < (ttl 10) icmp unreachable host_unreachable [0:0(0)]
+   +1 > S 0:0(0) <...>
+   // Outer TTL 255 >= min_ttl (250): accepted, socket aborts
+   +0 < (ttl 255) icmp unreachable host_unreachable [0:0(0)]
+   +0 %{ assert tcpi_state == TCP_CLOSE }%
+   +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [EHOSTUNREACH], [4]) = 0
+   +0 close(3) = 0
+
+// Test 3: Listener with IP_MINTTL (21) checks TTL before reqsk handling
+// An incoming fatal ICMP with outer TTL < min_ttl (255) must not abort TCP_NEW_SYN_RECV,
+// while outer TTL >= min_ttl (255) must abort it.
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 setsockopt(3, SOL_IP, 21, [255], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 10) = 0
+   +0 < (ttl 255) S 0:0(0) win 65535 <mss 1460,nop,nop,sackOK>
+   +0 > (ttl 64) S. 0:0(0) ack 1 <...>
+   // Outer TTL 10 < min_ttl (255): dropped, reqsk survives
+   +0 < (ttl 10) icmp unreachable port_unreachable [0:0(0)]
+   +1 > (ttl 64) S. 0:0(0) ack 1 <...>
+   // Outer TTL 255 >= min_ttl (255): accepted, fatal ICMP aborts reqsk
+   +0 < (ttl 255) icmp unreachable port_unreachable [0:0(0)]
+   +0 < (ttl 255) . 1:1(0) ack 1 win 65535
+   +0 > R 1:1(0)
+   +0 close(3) = 0
+
+// Test 4: Server in TCP_NEW_SYN_RECV does not abort on soft errors but aborts on fatal error
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 10) = 0
+   +0 < S 0:0(0) win 65535 <mss 1460,nop,nop,sackOK>
+   +0 > S. 0:0(0) ack 1 <...>
+   // 4a: Soft errors must not abort reqsk
+   +0 < icmp unreachable host_unreachable [0:0(0)]
+   +0 < icmp unreachable net_unreachable [0:0(0)]
+   +0 < icmp time_exceeded [0:0(0)]
+   +0 < icmp parameter_problem [0:0(0)]
+   +1 > S. 0:0(0) ack 1 <...>
+   // 4b: Fatal error (port_unreachable) must abort reqsk
+   +0 < icmp unreachable port_unreachable [0:0(0)]
+   // Remote sends 3rd ACK: listener responds with RST because reqsk was aborted
+   +0 < . 1:1(0) ack 1 win 65535
+   +0 > R 1:1(0)
+   +0 close(3) = 0
+
+// Test 5: Socket with IP_PMTUDISC_INTERFACE ignores ICMP_FRAG_NEEDED
+// Socket configured with IP_PMTUDISC_INTERFACE (4) must not overwrite MTU
+// upon receiving ICMP_DEST_UNREACH with code ICMP_FRAG_NEEDED.
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 1) = 0
+   +0 < S 0:0(0) win 65535 <mss 1460,sackOK,nop,nop,nop,wscale 8>
+   +0 > S. 0:0(0) ack 1 <...>
+   +0 < . 1:1(0) ack 1 win 257
+   +0 accept(3, ..., ...) = 4
+   +0 setsockopt(4, SOL_IP, IP_MTU_DISCOVER, [4], 4) = 0
+   +0 getsockopt(4, SOL_IP, IP_MTU, [1500], [4]) = 0
+   +0 write(4, ..., 1000) = 1000
+   +0 > P. 1:1001(1000) ack 1 <...>
+   +0 < icmp unreachable frag_needed mtu 1200 [1:1001(1000)]
+   +0 getsockopt(4, SOL_IP, IP_MTU, [1500], [4]) = 0
+   +0 close(4) = 0
+   +0 close(3) = 0
diff --git a/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt b/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt
new file mode 100644
index 000000000000..bd0cc365a088
--- /dev/null
+++ b/tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+--ip_version=ipv6
+
+`./defaults.sh`
+
+// Test 1: Client in TCP_SYN_SENT rejects ICMPv6 quoting seq == snd_nxt (RFC 5927 4.1)
+// In SYN_SENT, SND.UNA = ISN, SND.NXT = ISN + 1.
+// The sequence number in ICMP must satisfy SND.UNA <= SEG.SEQ < SND.NXT.
+// An ICMP message quoting seq == 1 (snd_nxt) must be dropped, while seq == 0 (snd_una) aborts.
+    0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
+   +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
+   +0 > S 0:0(0) <...>
+   // Out-of-window ICMPv6 quoting seq == 1 (snd_nxt) must be dropped
+   +0 < icmp unreachable address_unreachable [1:1(0)]
+   // Socket stays in TCP_SYN_SENT, so SYN retransmission occurs after 1s
+   +1 > S 0:0(0) <...>
+   // In-window ICMPv6 quoting seq == 0 (snd_una) is accepted and aborts connection
+   +0 < icmp unreachable address_unreachable [0:0(0)]
+   +0 %{ assert tcpi_state == TCP_CLOSE }%
+   +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [EHOSTUNREACH], [4]) = 0
+   +0 close(3) = 0
+
+// Test 2: Client with IPV6_MINHOPCOUNT (73) checks outer IPv6 hop_limit in tcp_v6_err()
+// An ICMPv6 with outer hop_limit < min_hopcount (250) must be dropped, while outer
+// hop_limit >= min_hopcount must be accepted even though the quoted inner hop_limit is 64.
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_IPV6, 73, [250], 4) = 0
+   +0 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
+   +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
+   +0 > S 0:0(0) <...>
+   // Outer hop_limit 10 < min_hopcount (250): dropped, socket stays in TCP_SYN_SENT
+   +0 < (ttl 10) icmp unreachable address_unreachable [0:0(0)]
+   +1 > S 0:0(0) <...>
+   // Outer hop_limit 255 >= min_hopcount (250): accepted, socket aborts
+   +0 < (ttl 255) icmp unreachable address_unreachable [0:0(0)]
+   +0 %{ assert tcpi_state == TCP_CLOSE }%
+   +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [EHOSTUNREACH], [4]) = 0
+   +0 close(3) = 0
+
+// Test 3: Listener with IPV6_MINHOPCOUNT (73) checks hop_limit before reqsk handling
+// An incoming fatal ICMPv6 with outer hop_limit < min_hopcount (255) must not abort
+// TCP_NEW_SYN_RECV, while outer hop_limit >= min_hopcount (255) must abort it.
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 setsockopt(3, SOL_IPV6, 73, [255], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 10) = 0
+   +0 < (ttl 255) S 0:0(0) win 65535 <mss 1440,nop,nop,sackOK>
+   +0 > (ttl 64) S. 0:0(0) ack 1 <...>
+   // Outer hop_limit 10 < min_hopcount (255): dropped, reqsk survives
+   +0 < (ttl 10) icmp unreachable port_unreachable [0:0(0)]
+   +1 > (ttl 64) S. 0:0(0) ack 1 <...>
+   // Outer hop_limit 255 >= min_hopcount (255): accepted, fatal ICMPv6 aborts reqsk
+   +0 < (ttl 255) icmp unreachable port_unreachable [0:0(0)]
+   +0 < (ttl 255) . 1:1(0) ack 1 win 65535
+   +0 > R 1:1(0)
+   +0 close(3) = 0
+
+// Test 4: Server in TCP_NEW_SYN_RECV does not abort on soft error or
+// non-UNK_NEXTHDR ICMPV6_PARAMPROB, but aborts on fatal error or
+// ICMPV6_UNK_NEXTHDR.
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 10) = 0
+   +0 < S 0:0(0) win 65535 <mss 1440,nop,nop,sackOK>
+   +0 > S. 0:0(0) ack 1 <...>
+   // 4a: Soft error (address_unreachable) must not abort reqsk
+   +0 < icmp unreachable address_unreachable [0:0(0)]
+   +1 > S. 0:0(0) ack 1 <...>
+   // 4b: Non-UNK_NEXTHDR parameter problem (header_field) must not abort reqsk
+   +0 < icmp parameter_problem header_field [0:0(0)]
+   +2 > S. 0:0(0) ack 1 <...>
+   // 4c: ICMPV6_UNK_NEXTHDR parameter problem (unknown_next_header) must abort reqsk
+   +0 < icmp parameter_problem unknown_next_header [0:0(0)]
+   // Remote sends 3rd ACK: listener responds with RST because reqsk was aborted
+   +0 < . 1:1(0) ack 1 win 65535
+   +0 > R 1:1(0)
+   +0 close(3) = 0
+   // 4d: Fatal unreachable error (port_unreachable) must abort reqsk
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 10) = 0
+   +0 < S 0:0(0) win 65535 <mss 1440,nop,nop,sackOK>
+   +0 > S. 0:0(0) ack 1 <...>
+   +0 < icmp unreachable port_unreachable [0:0(0)]
+   +0 < . 1:1(0) ack 1 win 65535
+   +0 > R 1:1(0)
+   +0 close(3) = 0
+
+// Test 5: Socket with IPV6_PMTUDISC_INTERFACE ignores ICMPV6_PKT_TOOBIG
+// Socket configured with IPV6_PMTUDISC_INTERFACE (4) must not overwrite MTU
+// upon receiving ICMPV6_PKT_TOOBIG.
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 1) = 0
+   +0 < S 0:0(0) win 65535 <mss 1440,sackOK,nop,nop,nop,wscale 8>
+   +0 > S. 0:0(0) ack 1 <...>
+   +0 < . 1:1(0) ack 1 win 257
+   +0 accept(3, ..., ...) = 4
+   +0 setsockopt(4, SOL_IPV6, IPV6_MTU_DISCOVER, [4], 4) = 0
+   +0 getsockopt(4, SOL_IPV6, IPV6_MTU, [1520], [4]) = 0
+   +0 write(4, ..., 1000) = 1000
+   +0 > P. 1:1001(1000) ack 1 <...>
+   +0 < icmp packet_too_big mtu 1280 [1:1001(1000)]
+   +0 getsockopt(4, SOL_IPV6, IPV6_MTU, [1520], [4]) = 0
+   +0 close(4) = 0
+   +0 close(3) = 0
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
                   ` (5 preceding siblings ...)
  2026-11-17  5:35 ` [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  2026-11-17  5:35 ` [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err() Kuniyuki Iwashima
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev,
	David Ahern, David Ahern

tcp_v4_err() passes 0 sdif to __inet_lookup_established().

So, ICMP error coming from VRF can be leaked to a socket not
bound to the device.

For example, this test fails at the last assert because the
socket receives ICMP error:

  --ip_version=ipv4
   0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
  +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
  +0 > S 0:0(0) <...>
  +0 `ip link add vrf0 type vrf table 10`
  +0 `ip link set vrf0 up`
  +0 `ip link set dev tun0 master vrf0`
  +0 `ip route add 192.0.2.0/24 dev tun0 table 10`
  +0 < icmp unreachable port_unreachable [0:0(0)]
  +0 %{ assert tcpi_state == TCP_SYN_SENT, tcpi_state }%

Let's pass inet_sdif() to __inet_lookup_established().

Fixes: 3fa6f616a7a4 ("net: ipv4: add second dif to inet socket lookups")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
Cc: David Ahern <dsahern@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
---
 net/ipv4/tcp_ipv4.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index bc909fedda3a..35a1309f8b21 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -504,7 +504,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 	int err;
 
 	sk = __inet_lookup_established(net, iph->daddr, th->dest, iph->saddr,
-				       ntohs(th->source), inet_iif(skb), 0);
+				       ntohs(th->source), inet_iif(skb),
+				       inet_sdif(skb));
 	if (!sk) {
 		__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
 		return -ENOENT;
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err().
  2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
                   ` (6 preceding siblings ...)
  2026-11-17  5:35 ` [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err() Kuniyuki Iwashima
@ 2026-11-17  5:35 ` Kuniyuki Iwashima
  7 siblings, 0 replies; 9+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-17  5:35 UTC (permalink / raw)
  To: Eric Dumazet, Neal Cardwell, David S . Miller, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

tcp_v6_err() passes skb->dev->ifindex to __inet6_lookup_established().

When ICMP is sent to a local address not on loopback device,
ip6_rt_get_dev_rcu() sets dst->dev to lo and ip6_rt_copy_init()
sets rt6i_idev to the actual device.

In ip6_rcv_core(), skb->dev is still lo, but IP6CB(skb)->iif
is set to ip6_dst_idev(skb_dst(skb))->dev->ifindex.

So skb->dev->ifindex is 1 while inet6_iif(skb) points to the
actual device.

Since tcp_v6_err() passes the wrong ifindex, such an ICMP error
is not delivered.

For example, this script creates a socket bound to tun0 and
generates ICMPv6 Port Unreachable to it, but the socket cannot
receive it and getsockopt() fails:

  --ip_version=ipv6
   0 `ip link set lo up`
  +0 `ip6tables -A OUTPUT -p tcp -j REJECT`
  +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
  +0 setsockopt(3, SOL_SOCKET, SO_BINDTODEVICE, "tun0", 4) = 0
  +0 connect(3, ..., ...) = -1 EINPROGRESS (Operation now in progress)
  +0 getsockopt(3, SOL_SOCKET, SO_ERROR, [ECONNREFUSED], [4]) = 0

Let's pass inet6_iif() to __inet6_lookup_established().

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 29fd0a084c91..ab0eff884d3d 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -419,7 +419,7 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 
 	sk = __inet6_lookup_established(net, &hdr->daddr, th->dest,
 					&hdr->saddr, ntohs(th->source),
-					skb->dev->ifindex, inet6_sdif(skb));
+					inet6_iif(skb), inet6_sdif(skb));
 
 	if (!sk) {
 		__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-17  5:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  5:35 [PATCH v1 net 0/8] tcp: Misc fixes for tcp_v[46]_err() Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err() Kuniyuki Iwashima
2026-11-17  5:35 ` [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err() Kuniyuki Iwashima

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox