Skip to contentSkip to conversation

conntrack and ICMP errors

DISCUSSION Balazs Scheidler 3 participants 6 messages Apr 10, 2003 – Apr 13, 2003 netfilter

lore
Share & tools
lore
#0 lore raw

Hi,

I've ran into a fact recently which may or may not be intended behaviour for conntrack. Given a connection in the ESTABLISHED state and a related ICMP error is received by conntrack the connection state is not affected in any ways. In practice this might mean that when a connection is terminated by an ICMP destination unreachable instead of a normal TCP packet the connection will not terminate.

I encountered this problem when proxying UDP based protocols: a connection is NATed to the proxy and when the proxy quits the stack returns ICMP errors to the sender. The error is correctly deNATed but the connection is not terminated, thus further packets are still being taken as part of the previous connection.

UDP session:
192.168.1.1:1025 -> 1.2.3.4:53

			|
			+> session redirected to the local stack (IP 192.168.1.2:50053)

Proxy runs on port 50053, the conntrack session starts as the first packet is sent to the server, this triggers the DNAT mapping (1.2.3.4:53 -> 192.168.1.2:50053) to be applied.

Now the proxy exits, the local port 192.168.1.2:50053 is closed. Another packet is sent by the client within the session timeout, this packet is assumed to be part of the previously established connection, thus it is NATed to 192.168.1.2:50053, there is no program listening, thus the stack returns the ICMP error destination unreachable (port unreachable), this is taken as a RELATED packet, and correctly deNATed just like anything, but the conntrack session is still not terminated.

Of course using transparent proxying is still not a common usage scenario, but the same problem can be applied to other situations as well: you have an ESTABLISHED TCP connection when one of the endpoints dies (either the host crashes or the corresponding IP connection is broken), no TCP packets are sent prior the connection is broken, thus the conntrack session remains in ESTABLISHED case. Even if the last-hop router returns a host unreachable error the conntrack entry stays in the table for 5 days.

My question is: is this behaviour intended or simply accidental?

Signature · 1 line
Bazsi
#1 lore raw

Balazs,

I'm skipping your UDP redirect stuff (too much for my brain now), but one note on the TCP part.

In the situation you allude to at the end of your mail, think about how the _client_ will react to the ICMP error (which you route through to it, don't you?) What will the client do? What effect will that have on the netfilter box? I can tell you: it will sit there and patiently wait for the other side to come back up, keeping the TCP connection ESTABLISHED, just like the iptables box. And, better yet: if the intermittent reason for the "destination unreachable" goes away, and we find out the server did NOT crash, but its link was flakey, my ssh connection STILL WORKS THE SECOND I TYPE AGAIN!

Yes, I would call that intentional.

To summarize:

ICMP messages DO NOT TERMINATE TCP CONNECTIONS.

Please print that out, and eat the printout. You may add salt and pepper.

best regards
Patrick

#2 lore raw
Quoted 15 lines · Patrick Schaaf

Balazs,

I'm skipping your UDP redirect stuff (too much for my brain now), but one note on the TCP part.

In the situation you allude to at the end of your mail, think about how the _client_ will react to the ICMP error (which you route through to it, don't you?) What will the client do? What effect will that have on the netfilter box? I can tell you: it will sit there and patiently wait for the other side to come back up, keeping the TCP connection ESTABLISHED, just like the iptables box. And, better yet: if the intermittent reason for the "destination unreachable" goes away, and we find out the server did NOT crash, but its link was flakey, my ssh connection STILL WORKS THE SECOND I TYPE AGAIN!

Quoted 1 line

Yes, I would call that intentional.

ok. what about port unreachable messages? see below.

Quoted 6 lines

To summarize:

ICMP messages DO NOT TERMINATE TCP CONNECTIONS.

Please print that out, and eat the printout. You may add salt and pepper.

I thought the TCP example would not be very good, but I would rather not eat printouts (not even salted ones ;).

Here's an example when an ICMP packet DOES terminate a TCP connection:

When a SYN is received on a port which is not listening the stack is free to return either a TCP RST packet or an ICMP port unreachable packet: Windows stacks return ICMP port unreachable, Linux returns TCP RST and our REJECT target sends an ICMP port unreachable (by default).

The client handles these packets the same way: 'Connection refused' error is propagated to userspace.

While TCP RST terminates the conntrack session ICMP port unreachable does not. I would not call this consistent.

On the other hand connection tracking an UDP session is less robust than TCP, simply because UDP has no state. Therefore we are using time based session tracking which is blind at least compared to TCP conntrack. Therefore it might make use of additional hints like ICMP port unreachables.

Signature · 2 lines
Bazsi
PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
#3 lore raw
Quoted 9 lines
Quoted 5 lines

To summarize:

ICMP messages DO NOT TERMINATE TCP CONNECTIONS.

Please print that out, and eat the printout. You may add salt and pepper.

I thought the TCP example would not be very good, but I would rather not eat printouts (not even salted ones ;).

Quoted 6 lines

Here's an example when an ICMP packet DOES terminate a TCP connection:

When a SYN is received on a port which is not listening the stack is free to return either a TCP RST packet or an ICMP port unreachable packet: Windows stacks return ICMP port unreachable, Linux returns TCP RST and our REJECT target sends an ICMP port unreachable (by default).

Sorry, but that's not a valid argument against my point. When a SYN is received, there IS no TCP connection to terminate, yet. Do you see conntracking keeping an ESTABLISHED connection, in this case? I don't think so.

Does Windows return ICMP port unreachable for non-SYN frames, too? And, does it accept port unreachable messages and terminates local sockets? Same questions regarding Linux and non-SYN frames?

Quoted 2 lines

While TCP RST terminates the conntrack session ICMP port unreachable does not. I would not call this consistent.

Depends on your answers to my previous paragraph. If normal behaviour for non-SYN packet is receiving back port unreachables, AND this makes an endpoint terminate an already established TCP connection, then you have a point, and I have learned something new. In that case, I could agree that conntracking may also need to learn something new.

Quoted 4 lines

On the other hand connection tracking an UDP session is less robust than TCP, simply because UDP has no state. Therefore we are using time based session tracking which is blind at least compared to TCP conntrack. Therefore it might make use of additional hints like ICMP port unreachables.

That may make sense. I'm not very familiar with the requirements of UDP based protocols.

best regards
Patrick

#4 lore raw
Quoted 16 lines · Patrick Schaaf
Quoted 9 lines
Quoted 5 lines

To summarize:

ICMP messages DO NOT TERMINATE TCP CONNECTIONS.

Please print that out, and eat the printout. You may add salt and pepper.

I thought the TCP example would not be very good, but I would rather not eat printouts (not even salted ones ;).

Quoted 6 lines

Here's an example when an ICMP packet DOES terminate a TCP connection:

When a SYN is received on a port which is not listening the stack is free to return either a TCP RST packet or an ICMP port unreachable packet: Windows stacks return ICMP port unreachable, Linux returns TCP RST and our REJECT target sends an ICMP port unreachable (by default).

Quoted 8 lines

Sorry, but that's not a valid argument against my point. When a SYN is received, there IS no TCP connection to terminate, yet. Do you see conntracking keeping an ESTABLISHED connection, in this case? I don't think so.

Does Windows return ICMP port unreachable for non-SYN frames, too? And, does it accept port unreachable messages and terminates local sockets? Same questions regarding Linux and non-SYN frames?

You are right, ICMP port unreachable is sent only in reaction to a SYN. So I was not right, however:

when an RST is received to a SYN the timeout becomes 60 secs (ip_conntrack_proto_tcp.c, timeout in state CONNTRACK_CLOSE)

when an ICMP error is received to a SYN it is not considered therefore the SYN_SENT timeout applies which is 120 secs

OK, this is not a grave problem but is still an inconsistency.

My point is that ICMP errors _might_ be useful for conntrack consideration.

Quoted 9 lines
Quoted 3 lines

While TCP RST terminates the conntrack session ICMP port unreachable does not. I would not call this consistent.

Depends on your answers to my previous paragraph. If normal behaviour for non-SYN packet is receiving back port unreachables, AND this makes an endpoint terminate an already established TCP connection, then you have a point, and I have learned something new. In that case, I could agree that conntracking may also need to learn something new.

Checking tcp_ipv4.c, function tcp_v4_err() reveals that ICMP destination unreachables are converted to an error code:

        case ICMP_DEST_UNREACH:
                if (code > NR_ICMP_UNREACH)
                        goto out;

                if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
                        if (sk->lock.users == 0)
                                do_pmtu_discovery(sk, iph, info);
                        goto out;
                }

                err = icmp_err_convert[code].errno;
                break;

But this error is propagated to userspace if IP_RECVERR socket option is enabled for the socket:

        if (sk->lock.users == 0 && sk->protinfo.af_inet.recverr) {
                sk->err = err;
                sk->error_report(sk);
        } else  { /* Only an error on timeout */
                sk->err_soft = err;
        }

err_soft is picked up only in case send() timeouts. Therefore other than the small timeout difference between the SYN-RST and SYN-ICMP UNREACH scenarios TCP would not necessary need to consider ICMP errors.

Quoted 8 lines
Quoted 5 lines

On the other hand connection tracking an UDP session is less robust than TCP, simply because UDP has no state. Therefore we are using time based session tracking which is blind at least compared to TCP conntrack. Therefore it might make use of additional hints like ICMP port unreachables.

That may make sense. I'm not very familiar with the requirements of UDP based protocols.

Currently UDP uses 30 secs timeout when the first packet arrives and 180 seconds afterwards. A port unreachable usually means that the target service is down. ICMP errors are handled by client stacks by always propagating the error:

	... parse error ... 
        if (!sk->protinfo.af_inet.recverr) {
                if (!harderr || sk->state != TCP_ESTABLISHED)
                        goto out;
        } else {
                ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
        }
        sk->err = err;
        sk->error_report(sk);

Although the application does not necessarily terminate communication after receiving an error from send() but it is highly probable. And this might be reflected in the conntrack timeout.

The more I think about this the more I understand the my scenario (with the proxies) would not be solved by terminating sessions when an ICMP error occurs, the solution would be more to explicitly request a state removal from userspace. Hmm... it might be time to check ctnetlink?

Signature · 2 lines
Bazsi
PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
#5 lore raw
Quoted 10 lines · Balazs Scheidler

You are right, ICMP port unreachable is sent only in reaction to a SYN. So I was not right, however:

when an RST is received to a SYN the timeout becomes 60 secs (ip_conntrack_proto_tcp.c, timeout in state CONNTRACK_CLOSE)

when an ICMP error is received to a SYN it is not considered therefore the SYN_SENT timeout applies which is 120 secs

OK, this is not a grave problem but is still an inconsistency.

Yes, it is. If there is an easy fix, I'm happy to include it.

Quoted 2 lines

But this error is propagated to userspace if IP_RECVERR socket option is enabled for the socket:

which is the case for 0.1% of all applications (unfortunately) :(

Quoted 3 lines

err_soft is picked up only in case send() timeouts. Therefore other than the small timeout difference between the SYN-RST and SYN-ICMP UNREACH scenarios TCP would not necessary need to consider ICMP errors.

ok. So we are talking about a tiny inconsistency that doesn't have that grave practical implications. The question would be: How to solve this interdependency of icmp/tcp resp. icmp/udp within our current modular 'ip_conntrack_protocol' approach, without making the code too much interdependent.

Quoted 4 lines

The more I think about this the more I understand the my scenario (with the proxies) would not be solved by terminating sessions when an ICMP error occurs, the solution would be more to explicitly request a state removal from userspace. Hmm... it might be time to check ctnetlink?

removing a conntrack entry by ctnetlink is one of the few things that are in the current ctnetlink code. ctnltest.c even uses it.

Quoted 1 line

Bazsi

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie