Skip to contentSkip to conversation

tcp: Misc fixes for tcp_v[46]_err().

PATCH In review Kuniyuki Iwashima 4 participants 28 messages Nov 17 – Nov 19

Subsystems (4)
lore
Share & tools
lore

Kuniyuki Iwashima

author reviewer
#0 lore raw

cover tcp: Misc fixes for tcp_v[46]_err().

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
Signature · 1 line
2.56.0.679.gc527c65306-goog
author reviewer
#1 lore raw

PATCH 1/8 tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.

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")
---
 net/ipv4/tcp_ipv4.c | 9 ++++++---
 net/ipv6/tcp_ipv6.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +6 -3
index 2fc92a7884fc..7d58f522068d 100644
@@ -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;
 	}
net/ipv6/tcp_ipv6.c +6 -3
index 5c7ed38f1f56..8691d220cf94 100644
@@ -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;
 	}
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer
#2 lore raw

Re: [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.

Quoted 30 lines · Kuniyuki Iwashima

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}%
Quoted 6 lines

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.

This hints at a separate issue.

An ICMP can only be processed in this window if it was generated locally and looped back while tcp_transmit_skb() is running (netfilter REJECT in OUTPUT for instance).

At this point connect() owns the socket, so we end up in :

        if (!sock_owned_by_user(sk))
                tcp_done_with_error(sk, err);
        else
                WRITE_ONCE(sk->sk_err_soft, err);

The socket stays in TCP_SYN_SENT, SYN is retransmitted until tcp_write_err(), and a non blocking user gets no EPOLLERR.

I think the repro in patch 8 only passes because SO_ERROR falls back to sk_err_soft. Could you check tcpi_state there ?

Also, given an attacker has to guess the ISN anyway, I am not sure this one is net material.

Quoted 24 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/ipv4/tcp_ipv4.c | 9 ++++++---
 net/ipv6/tcp_ipv6.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +1 -1
index 2fc92a7884fc..7d58f522068d 100644
@@ -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);

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 563

Quoted 10 lines
net/ipv4/tcp_ipv4.c +5 -2
        /* 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))) {

In TCP_SYN_SENT the only sequence we ever put on the wire is the ISN, including the Fast Open case (SYN+data is sent with seq == ISN, and the RTO retransmits the bare SYN).

So this could simply be :

        if (sk->sk_state != TCP_LISTEN &&
            (!between(seq, snd_una, snd_nxt) ||
             (sk->sk_state == TCP_SYN_SENT && seq != snd_una))) {

This mirrors the (seq != tcp_rsk(req)->snt_isn) test in tcp_req_err(), and does not need the special case for the window where snd_nxt has not been advanced yet.

Quoted 36 lines
net/ipv4/tcp_ipv4.c +0 -0
                __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
                goto out;
        }
net/ipv6/tcp_ipv6.c +6 -4
index 5c7ed38f1f56..8691d220cf94 100644
@@ -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

author reviewer
#3 lore raw

Re: [PATCH v1 net 1/8] tcp: Don't accept ICMP for SND.NXT in TCP_SYN_SENT.

Quoted 51 lines · Eric Dumazet
Quoted 30 lines · Kuniyuki Iwashima

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}%
Quoted 6 lines

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.

This hints at a separate issue.

An ICMP can only be processed in this window if it was generated locally and looped back while tcp_transmit_skb() is running (netfilter REJECT in OUTPUT for instance).

At this point connect() owns the socket, so we end up in :

        if (!sock_owned_by_user(sk))
                tcp_done_with_error(sk, err);
        else
                WRITE_ONCE(sk->sk_err_soft, err);
Quoted 5 lines

The socket stays in TCP_SYN_SENT, SYN is retransmitted until tcp_write_err(), and a non blocking user gets no EPOLLERR.

I think the repro in patch 8 only passes because SO_ERROR falls back to sk_err_soft. Could you check tcpi_state there ?

Ah yes, just after checking SO_ERROR, sk->sk_state is still at TCP_SYN_SENT and it transitions to TCP_CLOSE only at SYN retrans.

--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 +0 %{ assert tcpi_state == TCP_SYN_SENT, tcpi_state }%
+1.2 %{ assert tcpi_state == TCP_CLOSE, tcpi_state }%

Maybe we could change TCP_MTU_REDUCED_DEFERRED to TCP_ICMP_DEFERRED and have a new flag set for mtu etc in a hole in cold cache line ?

Quoted 3 lines

Also, given an attacker has to guess the ISN anyway, I am not sure this one is net material.

Fair, I'll post this to net-next later.

Quoted 43 lines
Quoted 25 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/ipv4/tcp_ipv4.c | 9 ++++++---
 net/ipv6/tcp_ipv6.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +1 -1
index 2fc92a7884fc..7d58f522068d 100644
@@ -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);

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 563

Quoted 10 lines
net/ipv4/tcp_ipv4.c +5 -2
        /* 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))) {

In TCP_SYN_SENT the only sequence we ever put on the wire is the ISN, including the Fast Open case (SYN+data is sent with seq == ISN, and the RTO retransmits the bare SYN).

So this could simply be :

Quoted 7 lines
        if (sk->sk_state != TCP_LISTEN &&
            (!between(seq, snd_una, snd_nxt) ||
             (sk->sk_state == TCP_SYN_SENT && seq != snd_una))) {

This mirrors the (seq != tcp_rsk(req)->snt_isn) test in tcp_req_err(), and does not need the special case for the window where snd_nxt has not been advanced yet.

Looks much better, I'll use it.

Thank you !

Quoted 37 lines
Quoted 37 lines
net/ipv4/tcp_ipv4.c +0 -0
                __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
                goto out;
        }
net/ipv6/tcp_ipv6.c +6 -4
index 5c7ed38f1f56..8691d220cf94 100644
@@ -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

author reviewer
#4 lore raw

PATCH 2/8 tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().

In reply to Kuniyuki Iwashima

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")

---
Cc: Stephen Hemminger <[email protected]>
Cc: stephen hemminger <[email protected]>

---
 net/ipv4/tcp_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +1 -1
index 7d58f522068d..bf8542368032 100644
@@ -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;
 		}
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer Reviewed-by on v1 2/8
#5 lore raw

Re: [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().

Quoted 42 lines · Kuniyuki Iwashima

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")

---
Cc: Stephen Hemminger <[email protected]>
Cc: stephen hemminger <[email protected]>

---
 net/ipv4/tcp_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +0 -0
index 7d58f522068d..bf8542368032 100644

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 552

Quoted 8 lines
net/ipv4/tcp_ipv4.c +1 -1
@@ -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;
                }

Linux sends ICMP errors with the default TTL (64), so locally generated errors are now dropped for any min_ttl above 64.

For the strict GTSM case (min_ttl == 255) there is no router between the peers, so nothing else changes.

But IP_MINTTL is also used with lower values for multi-hop sessions ("ttl-security hops N"). There, an ICMP from an intermediate router using an initial TTL of 64 was accepted before this patch (the quoted TTL is the one we sent, minus the hops to that router), and is dropped after.

I think this is the right thing to do, since the inner TTL is under the sender's control, but I would mention this in the changelog.

author reviewer
#6 lore raw

Re: [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().

Quoted 58 lines · Eric Dumazet
Quoted 42 lines · Kuniyuki Iwashima

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")

---
Cc: Stephen Hemminger <[email protected]>
Cc: stephen hemminger <[email protected]>

---
 net/ipv4/tcp_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +0 -0
index 7d58f522068d..bf8542368032 100644

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 552

Quoted 8 lines
net/ipv4/tcp_ipv4.c +1 -1
@@ -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;
                }

Linux sends ICMP errors with the default TTL (64), so locally generated errors are now dropped for any min_ttl above 64.

For the strict GTSM case (min_ttl == 255) there is no router between the peers, so nothing else changes.

Quoted 8 lines

But IP_MINTTL is also used with lower values for multi-hop sessions ("ttl-security hops N"). There, an ICMP from an intermediate router using an initial TTL of 64 was accepted before this patch (the quoted TTL is the one we sent, minus the hops to that router), and is dropped after.

I think this is the right thing to do, since the inner TTL is under the sender's control, but I would mention this in the changelog.

Will add it to changelog.

Thanks !

#7 lore raw

Re: [PATCH v1 net 2/8] tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().

In reply to Kuniyuki Iwashima
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

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] In tcp_v4_err() (net/ipv4/tcp_ipv4.c), the IP_MINTTL check now compares ip_hdr(skb)->ttl, the outer TTL, with min_ttl.

--

tcp: Don't use inner IP TTL for IP_MINTTL in tcp_v4_err().

This commit changes the IP_MINTTL check in tcp_v4_err(). The check now compares min_ttl against the TTL of the outer IP header that carries the ICMP error, instead of the TTL in the quoted inner IP header. This makes IPv4 behave like tcp_v6_err(), which checks ipv6_hdr(skb)->hop_limit.

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 552

Quoted 13 lines
net/ipv4/tcp_ipv4.c +1 -1
index 7d58f522068d6..bf8542368032f 100644
@@ -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;
 		}

[Severity: Medium]
Will this drop ICMP errors that the host generates about its own TCP packets on GTSM sockets?

__icmp_send() never sets a TTL, so __ip_make_skb() falls back to:

net/ipv4/ip_output.c:__ip_make_skb() {
    ...
	else
		ttl = ip_select_ttl(inet, &rt->dst);
    ...
}

The ICMP socket's uc_ttl is -1, so this gives the default hop limit, normally sysctl_ip_default_ttl = 64. The error is delivered over loopback without being decremented, so ip_hdr(skb)->ttl is 64 here.

A typical GTSM socket (IP_TTL 255, IP_MINTTL 254 or 255) now takes the goto out path and counts TCPMINTTLDROP. Before this patch, the quoted inner header was our own packet. It had not been forwarded, so it still carried TTL 255 and passed the check.

One local source of these errors is ip_fragment(), for DF packets that exceed the route MTU:

net/ipv4/ip_output.c:ip_fragment() {
    ...
		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
			  htonl(mtu));
    ...
}

tnl_update_pmtu() and vti_xmit() send ICMP_FRAG_NEEDED locally through icmp_ndo_send(). The link failure path sends ICMP_HOST_UNREACH.

tcp_v4_err() already expects these local errors:

/* If too many ICMPs get dropped on busy
* servers this needs to be solved differently.
* We do take care of PMTU discovery (RFC1191) special case :
* we can receive locally generated ICMP messages while socket is held. */

With this change, a GTSM socket no longer reaches the ICMP_FRAG_NEEDED handling for a local PMTU drop. tcp_v4_mtu_reduced() is skipped, so recovery waits for an RTO or for tcp_current_mss() to see the new dst MTU.

Also, a connect() in TCP_SYN_SENT no longer fails fast with EHOSTUNREACH on a local link failure. It runs the full SYN retry timeout instead.

The commit message says:

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

Could the commit message describe these effects on locally generated PMTU and host unreachable errors? The Fixes: tag will bring this to stable for all IPv4 GTSM users, such as BGP with ttl-security.

Would it make sense to exempt locally generated or loopback ICMP errors from the outer TTL check? As far as I can tell, the final state of the series keeps the outer TTL check without such an exemption.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117053543.84032-1-kuniyu%40google.com
author reviewer
#8 lore raw

PATCH 3/8 tcp: Don't bypass GTSM check for reqsk.

In reply to Kuniyuki Iwashima

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")
---
 net/ipv4/tcp_ipv4.c | 19 +++++++++++--------
 net/ipv6/tcp_ipv6.c | 19 +++++++++++--------
 2 files changed, 22 insertions(+), 16 deletions(-)
net/ipv4/tcp_ipv4.c +11 -8
index bf8542368032..fd9588bba967 100644
@@ -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);
net/ipv6/tcp_ipv6.c +11 -8
index 8691d220cf94..0f1523fff939 100644
@@ -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);
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer Reviewed-by on v1 3/8
#9 lore raw

Re: [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk.

Quoted 20 lines · Kuniyuki Iwashima

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}%

Quoted 6 lines

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")

---

Note the same bypass exists in the receive path : in tcp_v4_rcv() and tcp_v6_rcv(), the TCP_NEW_SYN_RECV branch calls tcp_check_req() / tcp_child_process() and returns before reaching the min_ttl / min_hopcount check done after the process: label.

author reviewer
#10 lore raw

Re: [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk.

Quoted 28 lines · Eric Dumazet
Quoted 20 lines · Kuniyuki Iwashima

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}%

Quoted 6 lines

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")

---

Quoted 4 lines

Note the same bypass exists in the receive path : in tcp_v4_rcv() and tcp_v6_rcv(), the TCP_NEW_SYN_RECV branch calls tcp_check_req() / tcp_child_process() and returns before reaching the min_ttl / min_hopcount check done after the process: label.

Yes, and we can move the block to after TIME_WAIT check only as we don't save min_ttl/min_hopcount for now.

        if (sk->sk_state == TCP_TIME_WAIT)
                goto do_time_wait;

I'll move the here in v2 (and if needed I can follow up on net-next for TIME_WAIT).

#11 lore raw

Re: [PATCH v1 net 3/8] tcp: Don't bypass GTSM check for reqsk.

In reply to Kuniyuki Iwashima
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

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] Incomplete fix: GTSM (IP_MINTTL / IPV6_MINHOPCOUNT) is still skipped for request sockets on the TCP receive path.

--

tcp: Don't bypass GTSM check for reqsk.

This patch moves the IP_MINTTL and IPV6_MINHOPCOUNT checks in tcp_v4_err() and tcp_v6_err() ahead of the TCP_NEW_SYN_RECV handling. ICMP errors with a low TTL or hop limit can then no longer abort request sockets. For a reqsk, the value is read from the listener through sk_to_full_sk().

Quoted 9 lines
net/ipv4/tcp_ipv4.c +1 -0
index bf8542368032f..fd9588bba9672 100644
@@ -516,6 +516,17 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)
 		inet_twsk_put(inet_twsk(sk));
 		return 0;
 	}
+

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 529

Quoted 10 lines
net/ipv4/tcp_ipv4.c +10 -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;
+		}
+	}
+

[Severity: High]

This covers the ICMP path. Do ordinary segments that match a request socket still skip GTSM, though?

The subject says "Don't bypass GTSM check for reqsk", and the changelog says "Let's restore IP_MINTTL/IPV6_MINHOPCOUNT for reqsk".

In tcp_v4_rcv(), a segment that matches a TCP_NEW_SYN_RECV socket is handled entirely inside the reqsk block:

net/ipv4/tcp_ipv4.c:tcp_v4_rcv() {
    ...
	if (sk->sk_state == TCP_NEW_SYN_RECV) {
		...
		sk = req->rsk_listener;
		...
			nsk = tcp_check_req(sk, skb, req, false, &req_stolen,
					    &drop_reason);
		}
		if (!nsk) {
			...
			goto discard_and_relse;
		}
		...
		} else {
			drop_reason = tcp_child_process(sk, nsk, skb);
			...
			return 0;
		}
	}

	isn = 0;
process:
	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))) {
    ...
}

The only min_ttl check on the receive path is at the process label, and only the nsk == sk case reaches it. If a child is created, the function returns 0. If tcp_check_req() returns NULL, it jumps to discard_and_relse. tcp_check_req(), tcp_inbound_hash() and tcp_child_process() never look at min_ttl.

So a low-TTL sender facing a listener with IP_MINTTL=255 could still do either of the following:

- Drop the embryonic connection. An in-window SYN, or an RST with

    seq == rcv_nxt, goes through
    tcp_check_req()->embryonic_reset->inet_csk_reqsk_queue_drop().

- Complete the handshake and create a child socket by sending the third

    ACK with ack_seq == snt_isn + 1.

The in-window SYN only has to land inside the receive window. That is easier than the exact snt_isn match tcp_req_err() requires on the ICMP path.

In the changelog's repro the final ACK is sent with ttl 255. If it were sent with ttl 1, would accept() still return a TCP_ESTABLISHED socket after this patch?

tcp_v6_rcv() is laid out the same way, and its IPV6_MINHOPCOUNT check also sits only at the process label.

The selftest added later in the series (Test 3 in tcp_icmp_ipv4.pkt and tcp_icmp_ipv6.pkt) only exercises the ICMP path. No later patch in the series changes tcp_v4_rcv() or tcp_v6_rcv().

Should the same check be added to the TCP_NEW_SYN_RECV block of tcp_v4_rcv() and tcp_v6_rcv(), using req->rsk_listener? If not, should the subject and changelog say the fix only covers the ICMP path?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117053543.84032-1-kuniyu%40google.com
author reviewer
#12 lore raw

PATCH 4/8 tcp: Fix reqsk abort condition on ICMP errors.

In reply to Kuniyuki Iwashima

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")
---
 net/ipv4/tcp_ipv4.c | 7 ++-----
 net/ipv6/tcp_ipv6.c | 3 ++-
 2 files changed, 4 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +2 -5
index fd9588bba967..f11e090d04fb 100644
@@ -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;
 	}
 
net/ipv6/tcp_ipv6.c +2 -1
index 0f1523fff939..29fd0a084c91 100644
@@ -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;
 	}
 
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer
#13 lore raw

Re: [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors.

Quoted 4 lines · Kuniyuki Iwashima

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

Linux has never followed this for non-synchronized states.

tcp_v4_err() and tcp_v6_err() call tcp_done_with_error() for any error in TCP_SYN_SENT and TCP_SYN_RECV, soft or hard. RFC 5461 section 4.1 documents this, and names Linux.

Tests 1 and 2 in patch 6 actually depend on it : host_unreachable aborts the TCP_SYN_SENT socket.

Quoted 57 lines

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")
---
 net/ipv4/tcp_ipv4.c | 7 ++-----
 net/ipv6/tcp_ipv6.c | 3 ++-
 2 files changed, 4 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +0 -0
index fd9588bba967..f11e090d04fb 100644

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 533

Quoted 10 lines
net/ipv4/tcp_ipv4.c +2 -5
@@ -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));

After this patch, a TCP_SYN_SENT socket and a Fast Open TCP_SYN_RECV child abort on net/host unreachable, but a request socket does not.

We also lose the ability to get rid of a request for an unreachable client, and keep retransmitting SYNACK instead.

If the problem is that hard errors are ignored for IPv4 request sockets, I would rather add them than remove the soft ones :

        abort = type == ICMP_PARAMETERPROB ||
                type == ICMP_TIME_EXCEEDED ||
                (type == ICMP_DEST_UNREACH && code != ICMP_FRAG_NEEDED);

This matches what we do for TCP_SYN_SENT. The IPv6 side would then need the same treatment instead of using 'fatal'.

Quoted 20 lines
net/ipv4/tcp_ipv4.c +0 -0
                return 0;
        }
 
net/ipv6/tcp_ipv6.c +2 -2
index 0f1523fff939..29fd0a084c91 100644
@@ -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

author reviewer
#14 lore raw

Re: [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors.

Quoted 11 lines · Eric Dumazet
Quoted 4 lines · Kuniyuki Iwashima

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

Linux has never followed this for non-synchronized states.

tcp_v4_err() and tcp_v6_err() call tcp_done_with_error() for any error in TCP_SYN_SENT and TCP_SYN_RECV, soft or hard. RFC 5461 section 4.1 documents this, and names Linux.

Ah interesting, I missed that.

Quoted 83 lines

Tests 1 and 2 in patch 6 actually depend on it : host_unreachable aborts the TCP_SYN_SENT socket.

Quoted 57 lines

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")
---
 net/ipv4/tcp_ipv4.c | 7 ++-----
 net/ipv6/tcp_ipv6.c | 3 ++-
 2 files changed, 4 insertions(+), 6 deletions(-)
net/ipv4/tcp_ipv4.c +0 -0
index fd9588bba967..f11e090d04fb 100644

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 533

Quoted 10 lines
net/ipv4/tcp_ipv4.c +2 -5
@@ -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));

After this patch, a TCP_SYN_SENT socket and a Fast Open TCP_SYN_RECV child abort on net/host unreachable, but a request socket does not.

We also lose the ability to get rid of a request for an unreachable client, and keep retransmitting SYNACK instead.

If the problem is that hard errors are ignored for IPv4 request sockets, I would rather add them than remove the soft ones :

Quoted 6 lines
        abort = type == ICMP_PARAMETERPROB ||
                type == ICMP_TIME_EXCEEDED ||
                (type == ICMP_DEST_UNREACH && code != ICMP_FRAG_NEEDED);

This matches what we do for TCP_SYN_SENT. The IPv6 side would then need the same treatment instead of using 'fatal'.

I'll update both in that way.

Thanks!

#15 lore raw

Re: [PATCH v1 net 4/8] tcp: Fix reqsk abort condition on ICMP errors.

In reply to Kuniyuki Iwashima
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch makes a comment in the existing TFO selftest wrong.

--

tcp: Fix reqsk abort condition on ICMP errors.

This commit changes the abort condition passed to tcp_req_err() for TCP_NEW_SYN_RECV sockets so that it follows RFC 1122 Section 4.2.3.9. In IPv4, only fatal ICMP_DEST_UNREACH codes now drop the reqsk. In IPv6, ICMPV6_PARAMPROB aborts only for ICMPV6_UNK_NEXTHDR.

Quoted 5 lines
net/ipv4/tcp_ipv4.c +0 -0
index fd9588bba9672..f11e090d04fb3 100644

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 533

Quoted 10 lines
net/ipv4/tcp_ipv4.c +2 -5
@@ -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));

[Severity: Low]
This isn't a bug, but does this change leave a comment in an existing TFO selftest out of date?

tools/testing/selftests/net/packetdrill/tcp_fastopen_server_icmp-before-accept.pkt sends host_unreachable to the pending request and then says:

    // Valid ICMP unreach.
       +0 < icmp unreachable host_unreachable [0:10(10)]

    // Unlike the non-TFO case, the req is still there to be accepted.
       +0 accept(3, ..., ...) = 4

That contrast was true before this patch, because ICMP_HOST_UNREACH used to drop a non-TFO reqsk.

With this change, icmp_err_convert[ICMP_HOST_UNREACH].fatal is 0, so abort is false. tcp_req_err() then skips inet_csk_reqsk_queue_drop(), and the non-TFO reqsk stays in the queue and can still be accepted.

The difference between the two cases now runs the other way. The TFO child is still moved to TCP_CLOSE by tcp_done_with_error(), but the plain reqsk is left alone.

The tcp_icmp_ipv4.pkt test added later in this series ("4a: Soft errors must not abort reqsk") checks the new non-TFO behaviour, and it contradicts this comment. As far as I can tell, no later patch in the series updates the comment.

Could the comment in tcp_fastopen_server_icmp-before-accept.pkt be updated to match the new behaviour?

Quoted 3 lines
net/ipv4/tcp_ipv4.c +0 -0
 		return 0;
 	}
 

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117053543.84032-1-kuniyu%40google.com
author reviewer
#16 lore raw

PATCH 5/8 tcp: Check ip_sk_accept_pmtu() in tcp_v4_err().

In reply to Kuniyuki Iwashima

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")

---
Cc: Hannes Frederic Sowa <[email protected]>

---
 net/ipv4/tcp_ipv4.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +3 -1
index f11e090d04fb..bc909fedda3a 100644
@@ -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);
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer Reviewed-by on v1 5/8
#17 lore raw

Re: [PATCH v1 net 5/8] tcp: Check ip_sk_accept_pmtu() in tcp_v4_err().

Quoted 28 lines · Kuniyuki Iwashima

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().

Quoted 6 lines

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")
author reviewer
#18 lore raw

PATCH 6/8 selftest: net: packetdrill: Add ICMP and ICMPv6 tests.

In reply to Kuniyuki Iwashima

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.

---
 .../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
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +98 -0
new file mode 100644
index 000000000000..122bf9a14862
@@ -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
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt new file +111 -0

111 lines not on this page · raw message

Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer
#19 lore raw

Re: [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests.

Quoted 43 lines · Kuniyuki Iwashima

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.

---
 .../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
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +18 -0
new file mode 100644
index 000000000000..122bf9a14862
@@ -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

tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt line 28

Quoted 10 lines
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +10 -0
+   +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

Ah.... could we instead update packedrill, adding IP_MINTTL ?

Same for IPV6_MINHOPCOUNT

Quoted 190 lines
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +70 -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
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv6.pkt new file +111 -1
new file mode 100644
index 000000000000..bd0cc365a088
@@ -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

author reviewer
#20 lore raw

Re: [PATCH v1 net 6/8] selftest: net: packetdrill: Add ICMP and ICMPv6 tests.

Quoted 57 lines · Eric Dumazet
Quoted 43 lines · Kuniyuki Iwashima

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.

---
 .../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
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +18 -0
new file mode 100644
index 000000000000..122bf9a14862
@@ -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

tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt line 28

Quoted 10 lines
tools/testing/selftests/net/packetdrill/tcp_icmp_ipv4.pkt new file +10 -0
+   +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

Ah.... could we instead update packedrill, adding IP_MINTTL ?

Quoted 1 line

Same for IPV6_MINHOPCOUNT

Will do.

author reviewer
#21 lore raw

PATCH 7/8 tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().

In reply to Kuniyuki Iwashima

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")

---
Cc: David Ahern <[email protected]>
Cc: David Ahern <[email protected]>

---
 net/ipv4/tcp_ipv4.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +2 -1
index bc909fedda3a..35a1309f8b21 100644
@@ -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;
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer Reviewed-by on v1 7/8
#22 lore raw

Re: [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().

Quoted 20 lines · Kuniyuki Iwashima

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 }%

Quoted 4 lines

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

Fixes 3fa6f616a7a4 ("net: ipv4: add second dif to inet socket lookups")
#23 lore raw

Re: [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().

In reply to Kuniyuki Iwashima
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message for "tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err()." only describes half of…

--

tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().

This commit changes tcp_v4_err() to pass inet_sdif(skb) instead of 0 as the sdif argument to __inet_lookup_established(). The goal is to stop ICMP errors that arrive through a VRF from reaching sockets that are not bound to the device.

net/ipv4/tcp_ipv4.c int tcp_v4_err(struct sk_buff *skb, u32 info) · line 508

Quoted 11 lines
net/ipv4/tcp_ipv4.c +2 -1
index bc909fedda3a8..35a1309f8b21c 100644
@@ -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));

[Severity: Low]
The code change looks correct to me. It now uses the same dif/sdif pair that tcp_v4_rcv() already uses. However, the commit message only covers half of the behavior change:

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

Should the commit message also say what changes for sockets bound to the VRF slave device?

For a packet received through a VRF, inet_iif(skb) returns the VRF master ifindex. inet_sdif(skb) returns IPCB(skb)->iif, which is the slave ifindex. The match is done here:

include/net/inet_sock.h:inet_bound_dev_eq() {
	if (!bound_dev_if)
		return !sdif || l3mdev_accept;
	return bound_dev_if == dif || bound_dev_if == sdif;
}

With the old sdif of 0, a socket whose sk_bound_dev_if is the slave ifindex never matched in tcp_v4_err(). Such sockets got no ICMP errors at all, which could leave them in a PMTU black hole.

After this patch those sockets do match. They will now see:

ICMP_DEST_UNREACH / ICMP_FRAG_NEEDED -> tcp_v4_mtu_reduced() hard errors in TCP_SYN_SENT / TCP_SYN_RECV -> tcp_done_with_error()

Users can see this change. Would it help stable backporters and maintainers to mention it in the changelog?

Quoted 3 lines
net/ipv4/tcp_ipv4.c +0 -0
 	if (!sk) {
 		__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
 		return -ENOENT;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117053543.84032-1-kuniyu%40google.com
Reviewed-by on v1 7/8
#24 lore raw

Re: [PATCH v1 net 7/8] tcp: Pass inet_sdif() to __inet_lookup_established() in tcp_v4_err().

In reply to Kuniyuki Iwashima
Quoted 19 lines · Kuniyuki Iwashima

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 }%

Quoted 6 lines

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

Fixes 3fa6f616a7a4 ("net: ipv4: add second dif to inet socket lookups")

---
Cc: David Ahern <[email protected]>

just this one is needed.

Quoted 4 lines
Cc David Ahern <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
author reviewer
#25 lore raw

PATCH 8/8 tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err().

In reply to Kuniyuki Iwashima

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")
---
 net/ipv6/tcp_ipv6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/tcp_ipv6.c +1 -1
index 29fd0a084c91..ab0eff884d3d 100644
@@ -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),
Signature · 1 line
2.56.0.679.gc527c65306-goog
maintainer Reviewed-by on v1 8/8
#26 lore raw

Re: [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err().

Quoted 28 lines · Kuniyuki Iwashima

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

Quoted 4 lines

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

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

I wonder if you could send (maybe later in net-next) packetdrill tests for 7 and 8 ?

Thanks.

author reviewer
#27 lore raw

Re: [PATCH v1 net 8/8] tcp: Pass inet6_iif() to __inet6_lookup_established() in tcp_v6_err().

Quoted 31 lines · Eric Dumazet
Quoted 30 lines · Kuniyuki Iwashima

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().

Quoted 7 lines
Quoted 2 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

I wonder if you could send (maybe later in net-next) packetdrill tests for 7 and 8 ?

Sure, will add tests in net-next.

The original repro was too hacky to use vrf (with python script embeded in packetdrill or --ip_any and TCP_REPAIR), and I finally used netfilter.

I'll see if there is a good test or a better way, or I'll add CONFIG_NETFILTER_XXX etc to config.

Thanks !