Skip to contentSkip to conversation

optimization of ip_conntrack_proto_tcp:tcp_packet()

PATCH In review Harald Welte 3 participants 4 messages Mar 29, 2004 – Mar 30, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 optimization of ip_conntrack_proto_tcp:tcp_packet()

Hi Dave!

This is the first in a set of 2.6.x optimization patches. Obviosly, they are meant for 2.6.6, not 2.6.5. Please apply to your tree, thanks.

A: Pablo Neira
D: This patch cleans up tcp_packet(). No semantical change, just juggling
D: code pieces.

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
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +21 -19
@@ -178,6 +178,16 @@
 	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
 		return -1;
 
+	/* If only reply is a RST, we can consider ourselves not to
+	   have an established connection: this is a fairly common
+	   problem case, so we can delete the conntrack
+	   immediately.  --RR */
+	if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status) && tcph.rst) {
+		if (del_timer(&conntrack->timeout))
+			conntrack->timeout.function((unsigned long)conntrack);
+		return NF_ACCEPT;
+	}
+
 	WRITE_LOCK(&tcp_lock);
 	oldtcpstate = conntrack->proto.tcp.state;
 	newconntrack
@@ -199,29 +209,21 @@
 	/* Poor man's window tracking: record SYN/ACK for handshake check */
 	if (oldtcpstate == TCP_CONNTRACK_SYN_SENT
 	    && CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY
-	    && tcph.syn && tcph.ack)
+	    && tcph.syn && tcph.ack) {
 		conntrack->proto.tcp.handshake_ack
 			= htonl(ntohl(tcph.seq) + 1);
+		goto out;
+	}
 
-	/* If only reply is a RST, we can consider ourselves not to
-	   have an established connection: this is a fairly common
-	   problem case, so we can delete the conntrack
-	   immediately.  --RR */
-	if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status) && tcph.rst) {
-		WRITE_UNLOCK(&tcp_lock);
-		if (del_timer(&conntrack->timeout))
-			conntrack->timeout.function((unsigned long)conntrack);
-	} else {
-		/* Set ASSURED if we see see valid ack in ESTABLISHED after SYN_RECV */
-		if (oldtcpstate == TCP_CONNTRACK_SYN_RECV
-		    && CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL
-		    && tcph.ack && !tcph.syn
-		    && tcph.ack_seq == conntrack->proto.tcp.handshake_ack)
-			set_bit(IPS_ASSURED_BIT, &conntrack->status);
+	/* Set ASSURED if we see valid ack in ESTABLISHED after SYN_RECV */
+	if (oldtcpstate == TCP_CONNTRACK_SYN_RECV
+	    && CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL
+	    && tcph.ack && !tcph.syn
+	    && tcph.ack_seq == conntrack->proto.tcp.handshake_ack)
+		set_bit(IPS_ASSURED_BIT, &conntrack->status);
 
-		WRITE_UNLOCK(&tcp_lock);
-		ip_ct_refresh(conntrack, *tcp_timeouts[newconntrack]);
-	}
+out:	WRITE_UNLOCK(&tcp_lock);
+	ip_ct_refresh(conntrack, *tcp_timeouts[newconntrack]);
 
 	return NF_ACCEPT;
 }
#1 lore raw
Quoted 6 lines · On Mon, 29 Mar 2004 12:33:48 +0200 Harald Welte <[email protected]> wrote:

This is the first in a set of 2.6.x optimization patches. Obviosly, they are meant for 2.6.6, not 2.6.5. Please apply to your tree, thanks.

A: Pablo Neira
D: This patch cleans up tcp_packet(). No semantical change, just juggling
D: code pieces.

I'm going to applies these to my 2.6.6 netfilter pending tree.

BUT! Please think about these changes very carefully. The order of testing these state pieces in the TCP header is important.

For example, how it is valid, in this change, to blindly accept a RESET packet to kill a conntrack entry before verifying the sequence number(s) (as appropriate per rfc793 rules)?

This looks really suspicious to me.

#2 lore raw

Hi davem,

Quoted 5 lines · David S. Miller

For example, how it is valid, in this change, to blindly accept a RESET packet to kill a conntrack entry before verifying the sequence number(s) (as appropriate per rfc793 rules)?

as current tcp tracking doesn't perform so strong tcp transitions checking, there won't be any problems. Joszef Kadlecsik is working on a full featured tcp tracking system which will take care of this stuff, but it still needs a bit of time.

Quoted 3 lines

This looks really suspicious to me.

Actually, I think that this change won't modify the current behaviour of the tcp connection tracking system because the conntrack will be drop sooner or later if a rst is received.

regards,
Pablo

#3 lore raw
Quoted 4 lines · On Tue, 30 Mar 2004 12:42:39 +0200 Pablo Neira <[email protected]> wrote:

as current tcp tracking doesn't perform so strong tcp transitions checking, there won't be any problems. Joszef Kadlecsik is working on a full featured tcp tracking system which will take care of this stuff, but it still needs a bit of time.

...

Quoted 3 lines

Actually, I think that this change won't modify the current behaviour of the tcp connection tracking system because the conntrack will be drop sooner or later if a rst is received.

You're right, in fact I keep getting reminded every few weeks about the lack of sequence number checking in conntrack so I should have known this :-)

Thanks for the clarification.