diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack_tcp.h linux-2.5.74-tcp-window/include/linux/netfilter_ipv4/ip_conntrack_tcp.h
--- linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack_tcp.h	2003-07-02 22:46:03.000000000 +0200
+++ linux-2.5.74-tcp-window/include/linux/netfilter_ipv4/ip_conntrack_tcp.h	2003-07-17 12:30:46.000000000 +0200
@@ -4,25 +4,46 @@
 
 enum tcp_conntrack {
 	TCP_CONNTRACK_NONE,
-	TCP_CONNTRACK_ESTABLISHED,
 	TCP_CONNTRACK_SYN_SENT,
 	TCP_CONNTRACK_SYN_RECV,
+	TCP_CONNTRACK_ESTABLISHED,
 	TCP_CONNTRACK_FIN_WAIT,
-	TCP_CONNTRACK_TIME_WAIT,
-	TCP_CONNTRACK_CLOSE,
 	TCP_CONNTRACK_CLOSE_WAIT,
 	TCP_CONNTRACK_LAST_ACK,
+	TCP_CONNTRACK_TIME_WAIT,
+	TCP_CONNTRACK_CLOSE,
 	TCP_CONNTRACK_LISTEN,
 	TCP_CONNTRACK_MAX
 };
 
+/* Window scaling is advertised by the sender */
+#define IP_CT_TCP_FLAG_WINDOW_SCALE	0x01
+
+/* SACK is permitted by the sender */
+#define IP_CT_TCP_FLAG_SACK_PERM	0x02
+
+struct ip_ct_tcp_state {
+	u_int32_t	td_end;		/* max of seq + len */
+	u_int32_t	td_maxend;	/* max of ack + max(win, 1) */
+	u_int32_t	td_maxwin;	/* max(win) */
+	u_int8_t	td_scale;	/* window scale factor */
+	u_int8_t	loose;		/* used when connection picked up from the middle */
+	unsigned char	flags;		/* option flags */
+};
+
 struct ip_ct_tcp
 {
-	enum tcp_conntrack state;
-
-	/* Poor man's window tracking: sequence number of valid ACK
-           handshake completion packet */
-	u_int32_t handshake_ack;
+	enum tcp_conntrack state;	/* state of the connection */
+	struct ip_ct_tcp_state seen[2];	/* connection parameters per direction */
+	/* For detecting aborted connections */
+	enum ip_conntrack_dir last_dir;	/* Direction of the last packet */
+	u_int32_t	last_seq;	/* Last sequence number seen in dir */
+	u_int32_t	last_end;	/* Last seq + len */
+	u_int8_t	retrans;	/* Number of retransmitted packets */
 };
 
+/* For NAT, when it mangles the packet */
+extern int ip_conntrack_tcp_update(struct ip_conntrack *conntrack, int dir,   
+                                   struct iphdr *iph, size_t newlen,
+                                   struct tcphdr *tcph);
 #endif /* _IP_CONNTRACK_TCP_H */
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/include/linux/sysctl.h linux-2.5.74-tcp-window/include/linux/sysctl.h
--- linux-2.5.74-locking/include/linux/sysctl.h	2003-07-17 10:20:47.000000000 +0200
+++ linux-2.5.74-tcp-window/include/linux/sysctl.h	2003-07-17 12:07:46.000000000 +0200
@@ -378,6 +378,13 @@
 	NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM=11,
 	NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT=12,
 	NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT=13,
+	NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS=14,
+	NET_IPV4_NF_CONNTRACK_TCP_LOG_INVALID_SCALE=15,
+	NET_IPV4_NF_CONNTRACK_TCP_LOG_OUT_OF_WINDOW=16,
+	NET_IPV4_NF_CONNTRACK_TCP_LOOSE=17,
+	NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL=18,
+	NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS=19,
+	NET_IPV4_NF_CONNTRACK_TCP_MAY_EXCEED_WINDOW=20,
 };
  
 /* /proc/sys/net/ipv6 */
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_ftp.c linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_ftp.c
--- linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_ftp.c	2003-07-02 22:52:59.000000000 +0200
+++ linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_ftp.c	2003-07-17 12:07:46.000000000 +0200
@@ -4,7 +4,6 @@
 #include <linux/netfilter.h>
 #include <linux/ip.h>
 #include <linux/ctype.h>
-#include <net/checksum.h>
 #include <net/tcp.h>
 
 #include <linux/netfilter_ipv4/lockhelp.h>
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_irc.c linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_irc.c
--- linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_irc.c	2003-07-02 22:52:57.000000000 +0200
+++ linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_irc.c	2003-07-17 12:07:46.000000000 +0200
@@ -26,7 +26,6 @@
 #include <linux/module.h>
 #include <linux/netfilter.h>
 #include <linux/ip.h>
-#include <net/checksum.h>
 #include <net/tcp.h>
 
 #include <linux/netfilter_ipv4/lockhelp.h>
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_proto_tcp.c linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_proto_tcp.c
--- linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_proto_tcp.c	2003-07-18 09:02:59.000000000 +0200
+++ linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_proto_tcp.c	2003-07-18 17:09:50.000000000 +0200
@@ -1,3 +1,21 @@
+/*
+ * TCP connection tracking
+ */
+
+/* (c) 1999 Paul `Rusty' Russell.  Licenced under the GNU General
+ * Public Licence. 
+ *
+ * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>:
+ *	- Real stateful connection tracking
+ *	- Modified state transitions table
+ *	- Window scaling support added
+ *	- SACK support added
+ *
+ * version 1.9
+ */
+
+#define __NO_VERSION__
+#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/sched.h>
 #include <linux/timer.h>
@@ -7,6 +25,7 @@
 #include <linux/ip.h>
 #include <linux/tcp.h>
 #include <linux/string.h>
+#include <linux/socket.h>
 
 #include <net/tcp.h>
 
@@ -17,32 +36,53 @@
 
 #if 0
 #define DEBUGP printk
+#define DEBUGP_VARS
+#define NET_RATELIMIT(foo)	(foo)
 #else
 #define DEBUGP(format, args...)
+#define NET_RATELIMIT(foo)	((foo) && net_ratelimit())
 #endif
 
+/* Logging options */ 
+int ip_ct_tcp_log_invalid_scale = 1;
+int ip_ct_tcp_log_out_of_window = 1;
+
+/* "Be conservative in what you do, 
+    be liberal in what you accept from others." 
+    We mark only out of window RST segments as INVALID. */
+int ip_ct_tcp_be_liberal = 0;
+
+/* When connection is picked up from the middle, how many packets are required
+   to pass in each direction when we assume we are in sync - if any side uses
+   window scaling, we lost the game. 
+   If it is set to zero, we disable picking up already established connections. */
+int ip_ct_tcp_loose = 3;
+
+/* Max number of the retransmitted packets without receiving an (acceptable) 
+   ACK from the destination. If this number is reached, a shorter timer 
+   will be started. */
+int ip_ct_tcp_max_retrans = 3;
+
+/* The tail of the segment may exceed the window (RFC 793, p. 69). */
+int ip_ct_tcp_may_exceed_window = 1;
+
 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
    closely.  They're more complex. --RR */
 
-/* Actually, I believe that neither ipmasq (where this code is stolen
-   from) nor ipfilter do it exactly right.  A new conntrack machine taking
-   into account packet loss (which creates uncertainty as to exactly
-   the conntrack of the connection) is required.  RSN.  --RR */
-
 static const char *tcp_conntrack_names[] = {
 	"NONE",
-	"ESTABLISHED",
 	"SYN_SENT",
 	"SYN_RECV",
+	"ESTABLISHED",
 	"FIN_WAIT",
-	"TIME_WAIT",
-	"CLOSE",
 	"CLOSE_WAIT",
 	"LAST_ACK",
+	"TIME_WAIT",
+	"CLOSE",
 	"LISTEN"
 };
 
-#define SECS *HZ
+#define SECS * HZ
 #define MINS * 60 SECS
 #define HOURS * 60 MINS
 #define DAYS * 24 HOURS
@@ -56,49 +96,180 @@
 unsigned long ip_ct_tcp_timeout_time_wait =     2 MINS;
 unsigned long ip_ct_tcp_timeout_close =        10 SECS;
 
+/* RFC1122 says the R2 limit should be at least 100 seconds.
+   Linux uses 15 packets as limit, which corresponds 
+   to ~13-30min depending on RTO. */
+unsigned long ip_ct_tcp_timeout_max_retrans =     5 MINS;
+
 static unsigned long * tcp_timeouts[]
 = { 0,                                 /*      TCP_CONNTRACK_NONE */
-    &ip_ct_tcp_timeout_established,    /*      TCP_CONNTRACK_ESTABLISHED,      */
     &ip_ct_tcp_timeout_syn_sent,       /*      TCP_CONNTRACK_SYN_SENT, */
     &ip_ct_tcp_timeout_syn_recv,       /*      TCP_CONNTRACK_SYN_RECV, */
+    &ip_ct_tcp_timeout_established,    /*      TCP_CONNTRACK_ESTABLISHED,      */
     &ip_ct_tcp_timeout_fin_wait,       /*      TCP_CONNTRACK_FIN_WAIT, */
-    &ip_ct_tcp_timeout_time_wait,      /*      TCP_CONNTRACK_TIME_WAIT,        */
-    &ip_ct_tcp_timeout_close,          /*      TCP_CONNTRACK_CLOSE,    */
     &ip_ct_tcp_timeout_close_wait,     /*      TCP_CONNTRACK_CLOSE_WAIT,       */
     &ip_ct_tcp_timeout_last_ack,       /*      TCP_CONNTRACK_LAST_ACK, */
+    &ip_ct_tcp_timeout_time_wait,      /*      TCP_CONNTRACK_TIME_WAIT,        */
+    &ip_ct_tcp_timeout_close,          /*      TCP_CONNTRACK_CLOSE,    */
     0,                                 /*      TCP_CONNTRACK_LISTEN */
  };
  
 #define sNO TCP_CONNTRACK_NONE
-#define sES TCP_CONNTRACK_ESTABLISHED
 #define sSS TCP_CONNTRACK_SYN_SENT
 #define sSR TCP_CONNTRACK_SYN_RECV
+#define sES TCP_CONNTRACK_ESTABLISHED
 #define sFW TCP_CONNTRACK_FIN_WAIT
-#define sTW TCP_CONNTRACK_TIME_WAIT
-#define sCL TCP_CONNTRACK_CLOSE
 #define sCW TCP_CONNTRACK_CLOSE_WAIT
 #define sLA TCP_CONNTRACK_LAST_ACK
+#define sTW TCP_CONNTRACK_TIME_WAIT
+#define sCL TCP_CONNTRACK_CLOSE
 #define sLI TCP_CONNTRACK_LISTEN
 #define sIV TCP_CONNTRACK_MAX
 
-static enum tcp_conntrack tcp_conntracks[2][5][TCP_CONNTRACK_MAX] = {
+/*
+ * The TCP state transition table needs a few words...
+ *
+ * We are the man in the middle. All the packets go through us
+ * but might get lost in transit to the destination.
+ * It is assumed that the destinations can't receive segments 
+ * we haven't seen.
+ *
+ * The checked segment is in window.
+ *
+ * The meaning of the states are:
+ *
+ * NONE:	initial state
+ * SYN_SENT:	SYN-only packet seen 
+ * SYN_RECV:	SYN-ACK packet seen
+ * ESTABLISHED:	ACK packet seen
+ * FIN_WAIT:	FIN packet seen
+ * CLOSE_WAIT:	ACK seen (after FIN) 
+ * LAST_ACK:	FIN seen (after FIN)
+ * TIME_WAIT:	last ACK seen
+ * CLOSE:	closed connection
+ *
+ * LISTEN state is not used.
+ *
+ */
+static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
 	{
-/*	ORIGINAL */
-/* 	  sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI 	*/
-/*syn*/	{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI },
-/*fin*/	{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI },
-/*ack*/	{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES },
-/*rst*/ {sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL },
-/*none*/{sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
+/* ORIGINAL */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*syn*/	   { sSS, sSS, sCL, sCL, sCL, sCL, sCL, sSS, sSS, sIV },
+/*
+ *	sNO -> sSS	Initialize a new connection
+ *	sSS -> sSS	Retransmitted SYN
+ *	sSR -> sCL	Error: SYNs in window outside the SYN_SENT state 
+ *			are errors. Receiver will either go back to the 
+ *			LISTEN state or reply with RST.
+ *	sES -> sCL
+ *	sFW -> sCL
+ *	sCW -> sCL
+ *	sLA -> sCL
+ *	sTW -> sSS	Reopened connection (RFC 1122).
+ *	sCL -> sSS  
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*synack*/ { sSR, sSR, sES, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
+/*
+ *	sNO -> sSR	Assumed: hey, we've just started up!
+ *	sSS -> sSR	Simultaneous open.
+ *	sSR -> sES	Ditto.
+ *	sES -> sCL	Error.
+ *	sFW -> sCL
+ *	sCW -> sCL
+ *	sLA -> sCL
+ *	sTW -> sCL
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*fin*/    { sTW, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
+/*
+ *	sNO -> sTW	We assume TIME-WAIT state.
+ *	sSS -> sIV	Client migth not send FIN in this state.
+ *	sSR -> sFW	Close started.
+ *	sES -> sFW	
+ *	sFW -> sLA	FIN seen in both directions, waiting for
+ *			the last ACK. 
+ *			Migth be a retransmitted FIN as well...
+ *	sCW -> sLA
+ *	sLA -> sLA	Retransmitted FIN. Remain in the same state.
+ *	sTW -> sTW
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*ack*/	   { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
+/*
+ *	sNO -> sES	Assumed.
+ *	sSS -> sIV	ACK is invalid: we haven't seen a SYN/ACK yet.
+ *	sSR -> sES	Established state is reached.
+ *	sES -> sES	:-)
+ *	sFW -> sCW	Normal close request answered by ACK.
+ *	sCW -> sCW
+ *	sLA -> sTW	Last ACK detected.
+ *	sTW -> sTW	Retransmitted last ACK. Remain in the same state.
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*rst*/    { sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
+/*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
 	},
 	{
-/*	REPLY */
-/* 	  sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI 	*/
-/*syn*/	{sSR, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR },
-/*fin*/	{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI },
-/*ack*/	{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI },
-/*rst*/ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sLA, sLI },
-/*none*/{sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
+/* REPLY */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*syn*/	   { sIV, sSS, sSR, sCL, sCL, sCL, sCL, sSS, sSS, sIV },
+/*
+ *	sNO -> sIV	Never reached.
+ *	sSS -> sSS	Simultaneous open.
+ *	sSR -> sSR	Simultaneous open, retransmitted SYN.
+ *			We have seen a SYN/ACK, but it seems 
+ *			it is delayed or got lost.
+ *	sES -> sCL	Error.
+ *	sFW -> sCL
+ *	sCW -> sCL
+ *	sLA -> sCL
+ *	sTW -> sSS	Reopened connection.
+ *	sCL -> sSS
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*synack*/ { sIV, sSR, sES, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
+/*
+ *	sSS -> sSR	Standard open.
+ *	sSR -> sES	Simultaneous open.
+ *	sES -> sCL	Error.
+ *	sFW -> sCL
+ *	sCW -> sCL
+ *	sLA -> sCL
+ *	sTW -> sCL
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*fin*/    { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
+/*
+ *	sSS -> sIV	Server might not send FIN in this state.
+ *	sSR -> sFW	Close started.
+ *	sES -> sFW
+ *	sFW -> sLA	FIN seen in both directions.
+ *	sCW -> sLA
+ *	sLA -> sLA	Retransmitted FIN.
+ *	sTW -> sTW
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*ack*/	   { sIV, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
+/*
+ *	sSS -> sIV	ACK is invalid: we haven't seen a SYN/ACK yet.
+ *	sSR -> sES	Simultaneous open.
+ *	sES -> sES	:-)
+ *	sFW -> sCW	Normal close request answered by ACK.
+ *	sCW -> sCW
+ *	sLA -> sTW	Last ACK detected.
+ *	sTW -> sTW	Retransmitted last ACK.
+ *	sCL -> sCL
+ */
+/* 	     sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI	*/
+/*rst*/    { sIV, sCL, sCL, sCL, sCL, sIV, sCL, sCL, sCL, sIV },
+/*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
 	}
 };
 
@@ -150,11 +321,377 @@
 
 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
 {
-	if (tcph->rst) return 3;
-	else if (tcph->syn) return 0;
-	else if (tcph->fin) return 1;
-	else if (tcph->ack) return 2;
-	else return 4;
+	if (tcph->rst) return 4;
+	else if (tcph->syn) return (tcph->ack ? 1 : 0);
+	else if (tcph->fin) return 2;
+	else if (tcph->ack) return 3;
+	else return 5;
+}
+
+/* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
+   in IP Filter' by Guido van Rooij.
+   
+   http://www.nluug.nl/events/sane2000/papers.html
+   http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
+   
+   The boundaries and the conditions are slightly changed:
+   
+   	td_maxend = max(sack + max(win,1)) seen in reply packets
+	td_maxwin = max(max(win, 1) + sack - ack) seen in sent packets
+	td_end    = max(seq + len) seen in sent packets
+   
+   I. 	Upper bound for valid data:	seq + len <= sender.td_maxend
+   II. 	Lower bound for valid data:	seq >= sender.td_end - receiver.td_maxwin
+   III.	Upper bound for valid ack:     sack <= receiver.td_end
+   IV.	Lower bound for valid ack:	ack >= receiver.td_end - MAXACKWINDOW
+   	
+   The upper bound limit for a valid ack is not ignored - 
+   we doesn't have to deal with fragments. 
+*/
+
+#define SEGMENT_SEQ_PLUS_LEN(seq, len, iph, tcph)	\
+	(seq + len - ((iph)->ihl + (tcph)->doff)*4 \
+	 + ((tcph)->syn ? 1 : 0) + ((tcph)->fin ? 1 : 0))
+
+/* Fixme: what about big packets? */
+#define MAXACKWINCONST			66000
+#define MAXACKWINDOW(sender)		((sender)->td_maxwin > MAXACKWINCONST ? \
+					 (sender)->td_maxwin : MAXACKWINCONST)
+
+/*
+ * Simplified tcp_parse_options routine from tcp_input.c
+ */
+static void tcp_options(const struct sk_buff *skb,
+			struct tcphdr *tcph,
+			struct ip_ct_tcp_state *state)
+{
+	int length = tcph->doff * 4 - sizeof(struct tcphdr);
+	unsigned char *ptr = (unsigned char *)(tcph + 1);
+	
+	state->td_scale = 
+	state->flags = 0;
+	
+	while (length > 0) {
+		int opcode=*ptr++;
+		int opsize;
+		
+		switch (opcode) {
+			case TCPOPT_EOL:
+				return;
+			case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
+				length--;
+				continue;
+			default:
+				opsize=*ptr++;
+				if (opsize < 2) /* "silly options" */
+					return;
+				if (opsize > length)
+					break;	/* don't parse partial options */
+
+				if (opcode == TCPOPT_SACK_PERM && opsize == TCPOLEN_SACK_PERM)
+					state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
+				else if (opcode == TCPOPT_WINDOW && opsize == TCPOLEN_WINDOW) {
+					u_int8_t scale = *(u_int8_t *)ptr;
+					
+					if (scale > 14) {
+						/* See RFC1323 for an explanation of the limit to 14 */
+						if (NET_RATELIMIT(ip_ct_tcp_log_invalid_scale))
+							nf_log_packet(PF_INET, 0, skb, NULL, NULL,
+								  "Illegal window scaling value %u > 14 ignored ", 
+								  scale);
+						state->td_scale = 14;
+					}
+					state->td_scale = scale;
+					state->flags |= IP_CT_TCP_FLAG_WINDOW_SCALE;
+				}
+				ptr += opsize - 2;
+				length -= opsize;
+		}
+	}
+}
+
+static void tcp_sack(struct tcphdr *tcph, __u32 *sack)
+{
+	__u32 tmp;
+	int length = tcph->doff * 4 - sizeof(struct tcphdr);
+	unsigned char *ptr = (unsigned char *)(tcph + 1);
+	
+	/* Fast path for timestamp-only option */
+	if (length == TCPOLEN_TSTAMP_ALIGNED*4
+	    && *(__u32 *)ptr == 
+	    	__constant_ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
+	    			 | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP))
+		return;
+		
+	while (length > 0) {
+		int opcode=*ptr++;
+		int opsize, i;
+		
+		switch (opcode) {
+			case TCPOPT_EOL:
+				return;
+			case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
+				length--;
+				continue;
+			default:
+				opsize=*ptr++;
+				if (opsize < 2) /* "silly options" */
+					return;
+				if (opsize > length)
+					break;	/* don't parse partial options */
+
+				if (opcode == TCPOPT_SACK
+				    && opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)
+				    && !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK)) {
+				    	for (i = 0; 
+				    	     i < (opsize - TCPOLEN_SACK_BASE); 
+				    	     i += TCPOLEN_SACK_PERBLOCK) {
+						tmp = ntohl(*((u_int32_t *)(ptr+i) + 1));
+				    		if (after(tmp, *sack))
+				    			*sack = tmp;
+				    	}
+				    	return;
+				}
+				ptr += opsize - 2;
+				length -= opsize;
+		}
+	}
+}
+	
+static int tcp_in_window(struct ip_ct_tcp *state, 
+                         enum ip_conntrack_dir dir,
+                         const struct sk_buff *skb,
+                         struct iphdr *iph,
+                         struct tcphdr *tcph)
+{
+	struct ip_ct_tcp_state *sender = &state->seen[dir];
+	struct ip_ct_tcp_state *receiver = &state->seen[!dir];
+	__u32 seq, ack, sack, end, win, swin;
+	int res;
+	
+	/*
+	 * Get the required data from the packet.
+	 */
+	seq = ntohl(tcph->seq);
+	ack = sack = ntohl(tcph->ack_seq);
+	if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
+		tcp_sack(tcph, &sack);
+	win = ntohs(tcph->window);
+	end = SEGMENT_SEQ_PLUS_LEN(seq, skb->len, iph, tcph);
+	
+	DEBUGP("tcp_in_window: START\n");
+	DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu seq=%u ack=%u sack=%u win=%u end=%u\n",
+		NIPQUAD(iph->saddr), ntohs(tcph->source), NIPQUAD(iph->daddr), ntohs(tcph->dest),
+		seq, ack, sack, win, end);
+	DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i receiver end=%u maxend=%u maxwin=%u scale=%i\n",
+		sender->td_end, sender->td_maxend, sender->td_maxwin, sender->td_scale, 
+		receiver->td_end, receiver->td_maxend, receiver->td_maxwin, receiver->td_scale);
+		
+	if (sender->td_end == 0) {
+		/*
+		 * Initialize sender data.
+		 */
+		if (tcph->syn && tcph->ack) {
+			/*
+			 * Outgoing SYN-ACK in reply to a SYN.
+			 *
+			 * Fixme: supporting simultaneous open is lost...
+			 */
+			sender->td_end = 
+			sender->td_maxend = end;
+			sender->td_maxwin = (win == 0 ? 1 : win);
+
+			tcp_options(skb, tcph, sender);
+			/* 
+			 * RFC 1323:
+			 * Both sides must send the Window Scale option
+			 * to enable window scaling in either direction.
+			 */
+			if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
+			      && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
+				sender->td_scale = 
+				receiver->td_scale = 0;
+		} else {
+			/*
+			 * We are in the middle of a connection,
+			 * its history is lost for us.
+			 * Let's try to use the data from the packet.
+		 	 */
+			sender->td_end = end;
+			sender->td_maxwin = (win == 0 ? 1 : win);
+			sender->td_maxend = end + sender->td_maxwin;
+		}
+	}
+	
+	if (!(tcph->ack)) {
+		/*
+		 * If there is no ACK, just pretend it was set and OK.
+		 */
+		ack = sack = receiver->td_end;
+	} else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) == (TCP_FLAG_ACK|TCP_FLAG_RST)) 
+		   && (ack == 0)) {
+		/*
+		 * Broken TCP stacks, that set ACK in RST packets as well
+		 * with zero ack value.
+		 */
+		ack = sack = receiver->td_end;
+	}
+
+	if (seq == end)
+		/*
+		 * Packets contains no data: we assume it is valid
+		 * and check the ack value only.
+		 */
+		seq = end = sender->td_end;
+		
+	if (sender->loose)
+		sender->loose--;
+
+	DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu seq=%u ack=%u sack=%u win=%u end=%u\n",
+		NIPQUAD(iph->saddr), ntohs(tcph->source), NIPQUAD(iph->daddr), ntohs(tcph->dest),
+		seq, ack, sack, win, end);
+	DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i receiver end=%u maxend=%u maxwin=%u scale=%i\n",
+		sender->td_end, sender->td_maxend, sender->td_maxwin, sender->td_scale, 
+		receiver->td_end, receiver->td_maxend, receiver->td_maxwin, receiver->td_scale);
+	DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
+		before(ip_ct_tcp_may_exceed_window ? seq : end, sender->td_maxend + 1),
+	    	after(seq, sender->td_end - receiver->td_maxwin - 1),
+	    	before(sack, receiver->td_end + 1),
+	    	after(ack, receiver->td_end - MAXACKWINDOW(sender)));
+		
+	if (sender->loose || receiver->loose ||
+	    (before(ip_ct_tcp_may_exceed_window ? seq : end, sender->td_maxend + 1) &&
+	     after(seq, sender->td_end - receiver->td_maxwin - 1) &&
+	     before(sack, receiver->td_end + 1) &&
+	     after(ack, receiver->td_end - MAXACKWINDOW(sender)))) {
+	    	/*
+		 * Take into account window scaling (RFC 1323).
+		 */
+		if (!tcph->syn)
+			win <<= sender->td_scale;
+		
+		/*
+		 * Update sender data.
+		 */
+		swin = sack - ack + win;
+		if (sender->td_maxwin < swin)
+			sender->td_maxwin = swin;
+		if (after(end, sender->td_end))
+			sender->td_end = end;
+		if (after(sack + win, receiver->td_maxend - 1)) {
+			receiver->td_maxend = sack + win;
+			if (win == 0)
+				receiver->td_maxend++;
+		}
+
+		/* 
+		 * Check retransmissions 
+		 */
+		if (state->last_dir == dir
+		    && state->last_seq == seq
+		    && state->last_end == end)
+			state->retrans++;
+		else {
+			state->last_dir = dir;
+			state->last_seq = seq;
+			state->last_end = end;
+			state->retrans = 0;
+		}
+		
+		res = 1;
+	} else {
+		if (NET_RATELIMIT(ip_ct_tcp_log_out_of_window))
+			nf_log_packet(PF_INET, 0, skb, NULL, NULL,
+				"ip_conntrack_tcp: INVALID: Out of window data; %s\n",
+				before(ip_ct_tcp_may_exceed_window ? seq: end, sender->td_maxend + 1) ?
+					after(seq, sender->td_end - receiver->td_maxwin - 1) ?
+						before(sack, receiver->td_end + 1) ?
+							after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
+							: "ACK is under the lower bound (possibly overly delayed ACK)"
+						: "(S)ACK is over the upper bound (ACKed data has never seen yet)"
+					: "SEQ is under the lower bound (retransmitted already ACKed data)"
+				: "SEQ is over the upper bound (over the window of the receiver)");
+
+		res = ip_ct_tcp_be_liberal && !tcph->rst;
+	}
+
+	DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u receiver end=%u maxend=%u maxwin=%u\n",
+		res, sender->td_end, sender->td_maxend, sender->td_maxwin, 
+		receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
+
+	return res;
+}
+
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+/* Update sender->td_end after NAT successfully mangled the packet */
+int ip_conntrack_tcp_update(struct ip_conntrack *conntrack, int dir,
+                             struct iphdr *iph, size_t newlen,
+                             struct tcphdr *tcph)
+{
+	__u32 end;
+#ifdef DEBUGP_VARS
+	struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
+	struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
+#endif
+
+	end = SEGMENT_SEQ_PLUS_LEN(ntohl(tcph->seq), newlen, iph, tcph);
+	
+	write_lock_ct(conntrack);
+	/*
+	 * We have to worry for the ack in the reply packet only...
+	 */
+	if (after(end, conntrack->proto.tcp.seen[dir].td_end))
+		conntrack->proto.tcp.seen[dir].td_end = end;
+
+	DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i receiver end=%u maxend=%u maxwin=%u scale=%i\n",
+		sender->td_end, sender->td_maxend, sender->td_maxwin, sender->td_scale, 
+		receiver->td_end, receiver->td_maxend, receiver->td_maxwin, receiver->td_scale);
+	write_unlock_ct(conntrack);
+	
+	return 1;
+}
+ 
+EXPORT_SYMBOL(ip_conntrack_tcp_update);
+#endif
+
+#define	TH_FIN	0x01
+#define	TH_SYN	0x02
+#define	TH_RST	0x04
+#define	TH_PUSH	0x08
+#define	TH_ACK	0x10
+#define	TH_URG	0x20
+#define	TH_ECE	0x40
+#define	TH_CWR	0x80
+
+/* Protect conntrack agaist unclean packets. Code taken from ipt_unclean.c.  */
+static int unclean(const struct sk_buff *skb, struct tcphdr *tcph)
+{
+	u_int8_t tcpflags;
+
+	/* FIXME: Ignore packets with invalid checksum. */
+
+	/* CHECK: TCP flags. */
+	tcpflags = (((u_int8_t *)tcph)[13] & ~(TH_ECE|TH_CWR));
+	if (tcpflags != TH_SYN
+	    && tcpflags != (TH_SYN|TH_ACK)
+	    && tcpflags != TH_RST
+	    && tcpflags != (TH_RST|TH_ACK)
+	    && tcpflags != (TH_RST|TH_ACK|TH_PUSH)
+	    && tcpflags != (TH_FIN|TH_ACK)
+	    && tcpflags != TH_ACK
+	    && tcpflags != (TH_ACK|TH_PUSH)
+	    && tcpflags != (TH_ACK|TH_URG)
+	    && tcpflags != (TH_ACK|TH_URG|TH_PUSH)
+	    && tcpflags != (TH_FIN|TH_ACK|TH_PUSH)
+	    && tcpflags != (TH_FIN|TH_ACK|TH_URG)
+	    && tcpflags != (TH_FIN|TH_ACK|TH_URG|TH_PUSH)) {
+		if (NET_RATELIMIT(ip_ct_tcp_log_out_of_window))
+			nf_log_packet(PF_INET, 0, skb, NULL, NULL, 
+				"ip_conntrack_tcp: INVALID: invalid TCP flag combination ");
+		return 1;
+	}
+
+	return 0;
 }
 
 /* Returns verdict for packet, or -1 for invalid. */
@@ -162,81 +699,181 @@
 		      const struct sk_buff *skb,
 		      enum ip_conntrack_info ctinfo)
 {
-	enum tcp_conntrack newconntrack, oldtcpstate;
-	struct tcphdr tcph;
+	enum tcp_conntrack new_state, old_state;
+	enum ip_conntrack_dir dir;
+	struct iphdr *iph = skb->nh.iph;
+	unsigned char buff[15 * 4];
+	struct tcphdr *tcph = (struct tcphdr *)buff;
+	unsigned long timeout;
 
-	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
+	/* Smaller that minimal TCP header? */
+	if (skb_copy_bits(skb, iph->ihl * 4, buff, sizeof(*tcph)) != 0)
+		return -1;
+
+	/* Smaller than actual TCP header? */
+	if (skb_copy_bits(skb, iph->ihl * 4 + sizeof(*tcph), 
+			  buff + sizeof(*tcph), 
+			  tcph->doff * 4 - sizeof(*tcph)) != 0)
+		return -1;
+	
+ 	/* Do not handle unclean packets, which could cause false alarms */
+	if (unclean(skb, tcph))
 		return -1;
 
 	write_lock_ct(conntrack);
-	oldtcpstate = conntrack->proto.tcp.state;
-	newconntrack
+	old_state = conntrack->proto.tcp.state;
+	dir = CTINFO2DIR(ctinfo);
+	
+	new_state
 		= tcp_conntracks
-		[CTINFO2DIR(ctinfo)]
-		[get_conntrack_index(&tcph)][oldtcpstate];
+		[dir]
+		[get_conntrack_index(tcph)][old_state];
+
+	if (new_state == TCP_CONNTRACK_SYN_SENT
+	    && old_state >= TCP_CONNTRACK_TIME_WAIT) {
+	    	/* Attempt to reopen a closed connection.
+	    	* Delete this connection and look up again. */
+	    	write_unlock_ct(conntrack);
+	    	if (del_timer(&conntrack->timeout))
+	    		conntrack->timeout.function((unsigned long)conntrack);
+	    	return NF_REPEAT;
+	} else if (!(new_state == TCP_CONNTRACK_MAX
+		   || tcp_in_window(&conntrack->proto.tcp,
+		   		    dir, skb, iph, tcph)))
+		new_state = TCP_CONNTRACK_MAX;
+
+	DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
+		NIPQUAD(iph->saddr), ntohs(tcph->source), NIPQUAD(iph->daddr), ntohs(tcph->dest),
+		(tcph->syn ? 1 : 0), (tcph->ack ? 1 : 0), (tcph->fin ? 1 : 0), (tcph->rst ? 1 : 0),
+		old_state, new_state);
 
 	/* Invalid */
-	if (newconntrack == TCP_CONNTRACK_MAX) {
+	if (new_state == TCP_CONNTRACK_MAX) {
 		DEBUGP("ip_conntrack_tcp: Invalid dir=%i index=%u conntrack=%u\n",
-		       CTINFO2DIR(ctinfo), get_conntrack_index(&tcph),
-		       conntrack->proto.tcp.state);
+		       dir, get_conntrack_index(tcph),
+		       old_state);
 		write_unlock_ct(conntrack);
 		return -1;
 	}
 
-	conntrack->proto.tcp.state = newconntrack;
-
-	/* 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)
-		conntrack->proto.tcp.handshake_ack
-			= htonl(ntohl(tcph.seq) + 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) {
-		write_unlock_ct(conntrack);
-		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);
-
-		write_unlock_ct(conntrack);
-		ip_ct_refresh(conntrack, *tcp_timeouts[newconntrack]);
+	conntrack->proto.tcp.state = new_state;
+	timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
+		  && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans ?
+		  ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
+
+	if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
+		/* 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 (tcph->rst) {
+			write_unlock_ct(conntrack);
+			if (del_timer(&conntrack->timeout))
+				conntrack->timeout.function((unsigned long)conntrack);
+
+			return NF_ACCEPT;
+		} else {
+			/* Set ASSURED if we see see valid ack in ESTABLISHED after SYN_RECV 
+			   or a valid answer for a picked up connection. */
+			if ((old_state == TCP_CONNTRACK_SYN_RECV
+			     || old_state == TCP_CONNTRACK_ESTABLISHED)
+			    && new_state == TCP_CONNTRACK_ESTABLISHED)
+				set_bit(IPS_ASSURED_BIT, &conntrack->status);
+		}
 	}
 
+	write_unlock_ct(conntrack);
+	ip_ct_refresh(conntrack, timeout);
+
 	return NF_ACCEPT;
 }
 
 /* Called when a new connection for this protocol found. */
 static int tcp_new(struct ip_conntrack *conntrack, const struct sk_buff *skb)
 {
-	enum tcp_conntrack newconntrack;
-	struct tcphdr tcph;
+	enum tcp_conntrack new_state;
+	struct iphdr *iph = skb->nh.iph;
+	unsigned char buff[15 * 4];
+	struct tcphdr *tcph = (struct tcphdr *)buff;
+#ifdef DEBUGP_VARS
+	struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
+	struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
+#endif
 
-	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
+	/* Smaller that minimal TCP header? */
+	if (skb_copy_bits(skb, iph->ihl * 4, buff, sizeof(*tcph)) != 0)
+		return -1;
+
+	/* Smaller than actual TCP header? */
+	if (skb_copy_bits(skb, iph->ihl * 4 + sizeof(*tcph), 
+			  buff + sizeof(*tcph), 
+			  tcph->doff * 4 - sizeof(*tcph)) != 0)
+		return -1;
+	
+ 	/* Skip unclean packets */
+	if (unclean(skb, tcph))
 		return -1;
 
 	/* Don't need lock here: this conntrack not in circulation yet */
-	newconntrack
-		= tcp_conntracks[0][get_conntrack_index(&tcph)]
+	new_state
+		= tcp_conntracks[0][get_conntrack_index(tcph)]
 		[TCP_CONNTRACK_NONE];
 
 	/* Invalid: delete conntrack */
-	if (newconntrack == TCP_CONNTRACK_MAX) {
+	if (new_state == TCP_CONNTRACK_MAX) {
 		DEBUGP("ip_conntrack_tcp: invalid new deleting.\n");
 		return 0;
 	}
 
-	conntrack->proto.tcp.state = newconntrack;
+	if (new_state == TCP_CONNTRACK_SYN_SENT) {
+		conntrack->proto.tcp.seen[0].td_end =
+			SEGMENT_SEQ_PLUS_LEN(ntohl(tcph->seq), skb->len, iph, tcph);
+		conntrack->proto.tcp.seen[0].td_maxwin = ntohs(tcph->window);
+		if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
+			conntrack->proto.tcp.seen[0].td_maxwin = 1;
+		conntrack->proto.tcp.seen[0].td_maxend =
+			conntrack->proto.tcp.seen[0].td_end;
+
+		conntrack->proto.tcp.seen[0].flags =
+		conntrack->proto.tcp.seen[1].flags = 0;
+		tcp_options(skb, tcph, &conntrack->proto.tcp.seen[0]);
+		conntrack->proto.tcp.seen[0].loose = 
+		conntrack->proto.tcp.seen[1].loose = 0;
+	} else {
+		if (ip_ct_tcp_loose == 0)
+			return 0;
+		/*
+		 * We are in the middle of a connection,
+		 * its history is lost for us.
+		 * Let's try to use the data from the packet.
+		 */
+		conntrack->proto.tcp.seen[0].td_end =
+			SEGMENT_SEQ_PLUS_LEN(ntohl(tcph->seq), skb->len, iph, tcph);
+		conntrack->proto.tcp.seen[0].td_maxwin = ntohs(tcph->window);
+		if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
+			conntrack->proto.tcp.seen[0].td_maxwin = 1;
+		conntrack->proto.tcp.seen[0].td_maxend =
+			conntrack->proto.tcp.seen[0].td_end + 
+			conntrack->proto.tcp.seen[0].td_maxwin;
+		conntrack->proto.tcp.seen[0].td_scale = 0;
+
+		/* We assume SACK */
+		conntrack->proto.tcp.seen[0].flags =
+		conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
+		conntrack->proto.tcp.seen[0].loose = 
+		conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
+	}
+    
+	conntrack->proto.tcp.seen[1].td_end = 0;
+	conntrack->proto.tcp.seen[1].td_maxend = 0;
+	conntrack->proto.tcp.seen[1].td_maxwin = 1;
+	conntrack->proto.tcp.seen[1].td_scale = 0;      
+
+	conntrack->proto.tcp.state = new_state;
+ 
+	DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i receiver end=%u maxend=%u maxwin=%u scale=%i\n",
+		sender->td_end, sender->td_maxend, sender->td_maxwin, sender->td_scale, 
+		receiver->td_end, receiver->td_maxend, receiver->td_maxwin, receiver->td_scale);
 	return 1;
 }
 
@@ -247,7 +884,7 @@
 	struct tcphdr tcph;
 	unsigned int datalen;
 
-	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
+	if (skb_copy_bits(skb, iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
 		return 0;
 	datalen = skb->len - iph->ihl*4 - tcph.doff*4;
 
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_standalone.c
--- linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-18 09:04:16.000000000 +0200
+++ linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-18 09:17:47.000000000 +0200
@@ -283,6 +283,13 @@
 extern unsigned long ip_ct_tcp_timeout_last_ack;
 extern unsigned long ip_ct_tcp_timeout_time_wait;
 extern unsigned long ip_ct_tcp_timeout_close;
+extern unsigned long ip_ct_tcp_timeout_max_retrans;
+extern int ip_ct_tcp_log_invalid_scale;
+extern int ip_ct_tcp_log_out_of_window;
+extern int ip_ct_tcp_loose;
+extern int ip_ct_tcp_be_liberal;
+extern int ip_ct_tcp_max_retrans;
+extern int ip_ct_tcp_may_exceed_window;
 
 /* From ip_conntrack_proto_udp.c */
 extern unsigned long ip_ct_udp_timeout;
@@ -389,6 +396,55 @@
 	 	.mode		= 0644,
 	 	.proc_handler	= &proc_dointvec_jiffies
 	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
+		.procname	= "ip_conntrack_tcp_timeout_max_retrans",
+	 	.data		= &ip_ct_tcp_timeout_max_retrans,
+	 	.maxlen		= sizeof(ip_ct_tcp_timeout_max_retrans),
+	 	.mode		= 0644,
+	 	.proc_handler	= &proc_dointvec_jiffies
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_LOG_INVALID_SCALE,
+		.procname	= "ip_conntrack_tcp_log_invalid_scale",
+	 	.data		= &ip_ct_tcp_log_invalid_scale,
+	 	.maxlen		= sizeof(ip_ct_tcp_log_invalid_scale),
+	 	.mode		= 0644,
+		.proc_handler	= &proc_dointvec
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_LOG_OUT_OF_WINDOW,
+		.procname	= "ip_conntrack_tcp_log_out_of_window",
+		.data		= &ip_ct_tcp_log_out_of_window,
+		.maxlen		= sizeof(ip_ct_tcp_log_out_of_window),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
+		.procname	= "ip_conntrack_tcp_loose",
+		.data		= &ip_ct_tcp_loose,
+		.maxlen		= sizeof(ip_ct_tcp_loose),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
+		.procname	= "ip_conntrack_tcp_be_liberal",
+		.data		= &ip_ct_tcp_be_liberal,
+		.maxlen		= sizeof(ip_ct_tcp_be_liberal),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
+		.procname	= "ip_conntrack_tcp_max_retrans",
+		.data		= &ip_ct_tcp_max_retrans,
+		.maxlen		= sizeof(ip_ct_tcp_max_retrans),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec
+	},
+	{	.ctl_name	= NET_IPV4_NF_CONNTRACK_TCP_MAY_EXCEED_WINDOW,
+		.procname	= "ip_conntrack_tcp_may_exceed_window",
+		.data		= &ip_ct_tcp_may_exceed_window,
+		.maxlen		= sizeof(ip_ct_tcp_may_exceed_window),
+		.mode		= 0644,
+		.proc_handler	&proc_dointvec
+	},
 	{	.ctl_name	= 0 }
 };
 
diff -urN --exclude-from=../diff.exclude linux-2.5.74-locking/net/ipv4/netfilter/ip_nat_helper.c linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_nat_helper.c
--- linux-2.5.74-locking/net/ipv4/netfilter/ip_nat_helper.c	2003-07-02 22:42:00.000000000 +0200
+++ linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_nat_helper.c	2003-07-17 17:09:36.000000000 +0200
@@ -390,7 +390,9 @@
 	tcph->seq = newseq;
 	tcph->ack_seq = newack;
 
-	return ip_nat_sack_adjust(pskb, tcph, ct, ctinfo);
+	return (ip_nat_sack_adjust(pskb, tcph, ct, ctinfo)
+		&& ip_conntrack_tcp_update(ct, dir, (*pskb)->nh.iph, 
+					   (*pskb)->len, tcph));
 }
 
 static inline int
