[NETFILTER]: Fix timeout sysctls on big-endian 64bit architectures
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_tcp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>:
9  *      - Real stateful connection tracking
10  *      - Modified state transitions table
11  *      - Window scaling support added
12  *      - SACK support added
13  *
14  * Willy Tarreau:
15  *      - State table bugfixes
16  *      - More robust state changes
17  *      - Tuning timer parameters
18  *
19  * version 2.2
20  */
21
22 #include <linux/config.h>
23 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/netfilter.h>
27 #include <linux/module.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/tcp.h>
31 #include <linux/spinlock.h>
32
33 #include <net/tcp.h>
34
35 #include <linux/netfilter.h>
36 #include <linux/netfilter_ipv4.h>
37 #include <linux/netfilter_ipv4/ip_conntrack.h>
38 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
39
40 #if 0
41 #define DEBUGP printk
42 #define DEBUGP_VARS
43 #else
44 #define DEBUGP(format, args...)
45 #endif
46
47 /* Protects conntrack->proto.tcp */
48 static DEFINE_RWLOCK(tcp_lock);
49
50 /* "Be conservative in what you do, 
51     be liberal in what you accept from others." 
52     If it's non-zero, we mark only out of window RST segments as INVALID. */
53 int ip_ct_tcp_be_liberal = 0;
54
55 /* When connection is picked up from the middle, how many packets are required
56    to pass in each direction when we assume we are in sync - if any side uses
57    window scaling, we lost the game. 
58    If it is set to zero, we disable picking up already established 
59    connections. */
60 int ip_ct_tcp_loose = 3;
61
62 /* Max number of the retransmitted packets without receiving an (acceptable) 
63    ACK from the destination. If this number is reached, a shorter timer 
64    will be started. */
65 int ip_ct_tcp_max_retrans = 3;
66
67   /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
68      closely.  They're more complex. --RR */
69
70 static const char *tcp_conntrack_names[] = {
71         "NONE",
72         "SYN_SENT",
73         "SYN_RECV",
74         "ESTABLISHED",
75         "FIN_WAIT",
76         "CLOSE_WAIT",
77         "LAST_ACK",
78         "TIME_WAIT",
79         "CLOSE",
80         "LISTEN"
81 };
82   
83 #define SECS * HZ
84 #define MINS * 60 SECS
85 #define HOURS * 60 MINS
86 #define DAYS * 24 HOURS
87
88 unsigned int ip_ct_tcp_timeout_syn_sent =      2 MINS;
89 unsigned int ip_ct_tcp_timeout_syn_recv =     60 SECS;
90 unsigned int ip_ct_tcp_timeout_established =   5 DAYS;
91 unsigned int ip_ct_tcp_timeout_fin_wait =      2 MINS;
92 unsigned int ip_ct_tcp_timeout_close_wait =   60 SECS;
93 unsigned int ip_ct_tcp_timeout_last_ack =     30 SECS;
94 unsigned int ip_ct_tcp_timeout_time_wait =     2 MINS;
95 unsigned int ip_ct_tcp_timeout_close =        10 SECS;
96
97 /* RFC1122 says the R2 limit should be at least 100 seconds.
98    Linux uses 15 packets as limit, which corresponds 
99    to ~13-30min depending on RTO. */
100 unsigned int ip_ct_tcp_timeout_max_retrans =     5 MINS;
101  
102 static const unsigned int * tcp_timeouts[]
103 = { NULL,                              /*      TCP_CONNTRACK_NONE */
104     &ip_ct_tcp_timeout_syn_sent,       /*      TCP_CONNTRACK_SYN_SENT, */
105     &ip_ct_tcp_timeout_syn_recv,       /*      TCP_CONNTRACK_SYN_RECV, */
106     &ip_ct_tcp_timeout_established,    /*      TCP_CONNTRACK_ESTABLISHED,      */
107     &ip_ct_tcp_timeout_fin_wait,       /*      TCP_CONNTRACK_FIN_WAIT, */
108     &ip_ct_tcp_timeout_close_wait,     /*      TCP_CONNTRACK_CLOSE_WAIT,       */
109     &ip_ct_tcp_timeout_last_ack,       /*      TCP_CONNTRACK_LAST_ACK, */
110     &ip_ct_tcp_timeout_time_wait,      /*      TCP_CONNTRACK_TIME_WAIT,        */
111     &ip_ct_tcp_timeout_close,          /*      TCP_CONNTRACK_CLOSE,    */
112     NULL,                              /*      TCP_CONNTRACK_LISTEN */
113  };
114  
115 #define sNO TCP_CONNTRACK_NONE
116 #define sSS TCP_CONNTRACK_SYN_SENT
117 #define sSR TCP_CONNTRACK_SYN_RECV
118 #define sES TCP_CONNTRACK_ESTABLISHED
119 #define sFW TCP_CONNTRACK_FIN_WAIT
120 #define sCW TCP_CONNTRACK_CLOSE_WAIT
121 #define sLA TCP_CONNTRACK_LAST_ACK
122 #define sTW TCP_CONNTRACK_TIME_WAIT
123 #define sCL TCP_CONNTRACK_CLOSE
124 #define sLI TCP_CONNTRACK_LISTEN
125 #define sIV TCP_CONNTRACK_MAX
126 #define sIG TCP_CONNTRACK_IGNORE
127
128 /* What TCP flags are set from RST/SYN/FIN/ACK. */
129 enum tcp_bit_set {
130         TCP_SYN_SET,
131         TCP_SYNACK_SET,
132         TCP_FIN_SET,
133         TCP_ACK_SET,
134         TCP_RST_SET,
135         TCP_NONE_SET,
136 };
137   
138 /*
139  * The TCP state transition table needs a few words...
140  *
141  * We are the man in the middle. All the packets go through us
142  * but might get lost in transit to the destination.
143  * It is assumed that the destinations can't receive segments 
144  * we haven't seen.
145  *
146  * The checked segment is in window, but our windows are *not*
147  * equivalent with the ones of the sender/receiver. We always
148  * try to guess the state of the current sender.
149  *
150  * The meaning of the states are:
151  *
152  * NONE:        initial state
153  * SYN_SENT:    SYN-only packet seen 
154  * SYN_RECV:    SYN-ACK packet seen
155  * ESTABLISHED: ACK packet seen
156  * FIN_WAIT:    FIN packet seen
157  * CLOSE_WAIT:  ACK seen (after FIN) 
158  * LAST_ACK:    FIN seen (after FIN)
159  * TIME_WAIT:   last ACK seen
160  * CLOSE:       closed connection
161  *
162  * LISTEN state is not used.
163  *
164  * Packets marked as IGNORED (sIG):
165  *      if they may be either invalid or valid 
166  *      and the receiver may send back a connection 
167  *      closing RST or a SYN/ACK.
168  *
169  * Packets marked as INVALID (sIV):
170  *      if they are invalid
171  *      or we do not support the request (simultaneous open)
172  */
173 static const enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
174         {
175 /* ORIGINAL */
176 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
177 /*syn*/    { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
178 /*
179  *      sNO -> sSS      Initialize a new connection
180  *      sSS -> sSS      Retransmitted SYN
181  *      sSR -> sIG      Late retransmitted SYN?
182  *      sES -> sIG      Error: SYNs in window outside the SYN_SENT state
183  *                      are errors. Receiver will reply with RST 
184  *                      and close the connection.
185  *                      Or we are not in sync and hold a dead connection.
186  *      sFW -> sIG
187  *      sCW -> sIG
188  *      sLA -> sIG
189  *      sTW -> sSS      Reopened connection (RFC 1122).
190  *      sCL -> sSS
191  */
192 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
193 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
194 /*
195  * A SYN/ACK from the client is always invalid:
196  *      - either it tries to set up a simultaneous open, which is 
197  *        not supported;
198  *      - or the firewall has just been inserted between the two hosts
199  *        during the session set-up. The SYN will be retransmitted 
200  *        by the true client (or it'll time out).
201  */
202 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
203 /*fin*/    { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
204 /*
205  *      sNO -> sIV      Too late and no reason to do anything...
206  *      sSS -> sIV      Client migth not send FIN in this state:
207  *                      we enforce waiting for a SYN/ACK reply first.
208  *      sSR -> sFW      Close started.
209  *      sES -> sFW      
210  *      sFW -> sLA      FIN seen in both directions, waiting for
211  *                      the last ACK. 
212  *                      Migth be a retransmitted FIN as well...
213  *      sCW -> sLA
214  *      sLA -> sLA      Retransmitted FIN. Remain in the same state.
215  *      sTW -> sTW
216  *      sCL -> sCL
217  */
218 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
219 /*ack*/    { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
220 /*
221  *      sNO -> sES      Assumed.
222  *      sSS -> sIV      ACK is invalid: we haven't seen a SYN/ACK yet.
223  *      sSR -> sES      Established state is reached.
224  *      sES -> sES      :-)
225  *      sFW -> sCW      Normal close request answered by ACK.
226  *      sCW -> sCW
227  *      sLA -> sTW      Last ACK detected.
228  *      sTW -> sTW      Retransmitted last ACK. Remain in the same state.
229  *      sCL -> sCL
230  */
231 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
232 /*rst*/    { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
233 /*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
234         },
235         {
236 /* REPLY */
237 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
238 /*syn*/    { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
239 /*
240  *      sNO -> sIV      Never reached.
241  *      sSS -> sIV      Simultaneous open, not supported
242  *      sSR -> sIV      Simultaneous open, not supported.
243  *      sES -> sIV      Server may not initiate a connection.
244  *      sFW -> sIV
245  *      sCW -> sIV
246  *      sLA -> sIV
247  *      sTW -> sIV      Reopened connection, but server may not do it.
248  *      sCL -> sIV
249  */
250 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
251 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
252 /*
253  *      sSS -> sSR      Standard open.
254  *      sSR -> sSR      Retransmitted SYN/ACK.
255  *      sES -> sIG      Late retransmitted SYN/ACK?
256  *      sFW -> sIG      Might be SYN/ACK answering ignored SYN
257  *      sCW -> sIG
258  *      sLA -> sIG
259  *      sTW -> sIG
260  *      sCL -> sIG
261  */
262 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
263 /*fin*/    { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
264 /*
265  *      sSS -> sIV      Server might not send FIN in this state.
266  *      sSR -> sFW      Close started.
267  *      sES -> sFW
268  *      sFW -> sLA      FIN seen in both directions.
269  *      sCW -> sLA
270  *      sLA -> sLA      Retransmitted FIN.
271  *      sTW -> sTW
272  *      sCL -> sCL
273  */
274 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
275 /*ack*/    { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
276 /*
277  *      sSS -> sIG      Might be a half-open connection.
278  *      sSR -> sSR      Might answer late resent SYN.
279  *      sES -> sES      :-)
280  *      sFW -> sCW      Normal close request answered by ACK.
281  *      sCW -> sCW
282  *      sLA -> sTW      Last ACK detected.
283  *      sTW -> sTW      Retransmitted last ACK.
284  *      sCL -> sCL
285  */
286 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI   */
287 /*rst*/    { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
288 /*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
289         }
290 };
291
292 static int tcp_pkt_to_tuple(const struct sk_buff *skb,
293                             unsigned int dataoff,
294                             struct ip_conntrack_tuple *tuple)
295 {
296         struct tcphdr _hdr, *hp;
297
298         /* Actually only need first 8 bytes. */
299         hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
300         if (hp == NULL)
301                 return 0;
302
303         tuple->src.u.tcp.port = hp->source;
304         tuple->dst.u.tcp.port = hp->dest;
305
306         return 1;
307 }
308
309 static int tcp_invert_tuple(struct ip_conntrack_tuple *tuple,
310                             const struct ip_conntrack_tuple *orig)
311 {
312         tuple->src.u.tcp.port = orig->dst.u.tcp.port;
313         tuple->dst.u.tcp.port = orig->src.u.tcp.port;
314         return 1;
315 }
316
317 /* Print out the per-protocol part of the tuple. */
318 static int tcp_print_tuple(struct seq_file *s,
319                            const struct ip_conntrack_tuple *tuple)
320 {
321         return seq_printf(s, "sport=%hu dport=%hu ",
322                           ntohs(tuple->src.u.tcp.port),
323                           ntohs(tuple->dst.u.tcp.port));
324 }
325
326 /* Print out the private part of the conntrack. */
327 static int tcp_print_conntrack(struct seq_file *s,
328                                const struct ip_conntrack *conntrack)
329 {
330         enum tcp_conntrack state;
331
332         read_lock_bh(&tcp_lock);
333         state = conntrack->proto.tcp.state;
334         read_unlock_bh(&tcp_lock);
335
336         return seq_printf(s, "%s ", tcp_conntrack_names[state]);
337 }
338
339 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
340     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
341 static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
342                          const struct ip_conntrack *ct)
343 {
344         struct nfattr *nest_parms;
345         
346         read_lock_bh(&tcp_lock);
347         nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
348         NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
349                 &ct->proto.tcp.state);
350         read_unlock_bh(&tcp_lock);
351
352         NFA_NEST_END(skb, nest_parms);
353
354         return 0;
355
356 nfattr_failure:
357         read_unlock_bh(&tcp_lock);
358         return -1;
359 }
360
361 static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = {
362         [CTA_PROTOINFO_TCP_STATE-1]     = sizeof(u_int8_t),
363 };
364
365 static int nfattr_to_tcp(struct nfattr *cda[], struct ip_conntrack *ct)
366 {
367         struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
368         struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
369
370         /* updates could not contain anything about the private
371          * protocol info, in that case skip the parsing */
372         if (!attr)
373                 return 0;
374
375         nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
376
377         if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp))
378                 return -EINVAL;
379
380         if (!tb[CTA_PROTOINFO_TCP_STATE-1])
381                 return -EINVAL;
382
383         write_lock_bh(&tcp_lock);
384         ct->proto.tcp.state = 
385                 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
386         write_unlock_bh(&tcp_lock);
387
388         return 0;
389 }
390 #endif
391
392 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
393 {
394         if (tcph->rst) return TCP_RST_SET;
395         else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
396         else if (tcph->fin) return TCP_FIN_SET;
397         else if (tcph->ack) return TCP_ACK_SET;
398         else return TCP_NONE_SET;
399 }
400
401 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
402    in IP Filter' by Guido van Rooij.
403    
404    http://www.nluug.nl/events/sane2000/papers.html
405    http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
406    
407    The boundaries and the conditions are changed according to RFC793:
408    the packet must intersect the window (i.e. segments may be
409    after the right or before the left edge) and thus receivers may ACK
410    segments after the right edge of the window.
411
412         td_maxend = max(sack + max(win,1)) seen in reply packets
413         td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
414         td_maxwin += seq + len - sender.td_maxend
415                         if seq + len > sender.td_maxend
416         td_end    = max(seq + len) seen in sent packets
417    
418    I.   Upper bound for valid data:     seq <= sender.td_maxend
419    II.  Lower bound for valid data:     seq + len >= sender.td_end - receiver.td_maxwin
420    III. Upper bound for valid ack:      sack <= receiver.td_end
421    IV.  Lower bound for valid ack:      ack >= receiver.td_end - MAXACKWINDOW
422         
423    where sack is the highest right edge of sack block found in the packet.
424         
425    The upper bound limit for a valid ack is not ignored - 
426    we doesn't have to deal with fragments. 
427 */
428
429 static inline __u32 segment_seq_plus_len(__u32 seq,
430                                          size_t len,
431                                          struct iphdr *iph,
432                                          struct tcphdr *tcph)
433 {
434         return (seq + len - (iph->ihl + tcph->doff)*4
435                 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
436 }
437   
438 /* Fixme: what about big packets? */
439 #define MAXACKWINCONST                  66000
440 #define MAXACKWINDOW(sender)                                            \
441         ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin     \
442                                               : MAXACKWINCONST)
443   
444 /*
445  * Simplified tcp_parse_options routine from tcp_input.c
446  */
447 static void tcp_options(const struct sk_buff *skb,
448                         struct iphdr *iph,
449                         struct tcphdr *tcph, 
450                         struct ip_ct_tcp_state *state)
451 {
452         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
453         unsigned char *ptr;
454         int length = (tcph->doff*4) - sizeof(struct tcphdr);
455         
456         if (!length)
457                 return;
458
459         ptr = skb_header_pointer(skb,
460                                  (iph->ihl * 4) + sizeof(struct tcphdr),
461                                  length, buff);
462         BUG_ON(ptr == NULL);
463
464         state->td_scale = 
465         state->flags = 0;
466         
467         while (length > 0) {
468                 int opcode=*ptr++;
469                 int opsize;
470                 
471                 switch (opcode) {
472                 case TCPOPT_EOL:
473                         return;
474                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
475                         length--;
476                         continue;
477                 default:
478                         opsize=*ptr++;
479                         if (opsize < 2) /* "silly options" */
480                                 return;
481                         if (opsize > length)
482                                 break;  /* don't parse partial options */
483
484                         if (opcode == TCPOPT_SACK_PERM 
485                             && opsize == TCPOLEN_SACK_PERM)
486                                 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
487                         else if (opcode == TCPOPT_WINDOW
488                                  && opsize == TCPOLEN_WINDOW) {
489                                 state->td_scale = *(u_int8_t *)ptr;
490                                 
491                                 if (state->td_scale > 14) {
492                                         /* See RFC1323 */
493                                         state->td_scale = 14;
494                                 }
495                                 state->flags |=
496                                         IP_CT_TCP_FLAG_WINDOW_SCALE;
497                         }
498                         ptr += opsize - 2;
499                         length -= opsize;
500                 }
501         }
502 }
503
504 static void tcp_sack(const struct sk_buff *skb,
505                      struct iphdr *iph,
506                      struct tcphdr *tcph,
507                      __u32 *sack)
508 {
509         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
510         unsigned char *ptr;
511         int length = (tcph->doff*4) - sizeof(struct tcphdr);
512         __u32 tmp;
513
514         if (!length)
515                 return;
516
517         ptr = skb_header_pointer(skb,
518                                  (iph->ihl * 4) + sizeof(struct tcphdr),
519                                  length, buff);
520         BUG_ON(ptr == NULL);
521
522         /* Fast path for timestamp-only option */
523         if (length == TCPOLEN_TSTAMP_ALIGNED*4
524             && *(__u32 *)ptr ==
525                 __constant_ntohl((TCPOPT_NOP << 24) 
526                                  | (TCPOPT_NOP << 16)
527                                  | (TCPOPT_TIMESTAMP << 8)
528                                  | TCPOLEN_TIMESTAMP))
529                 return;
530                 
531         while (length > 0) {
532                 int opcode=*ptr++;
533                 int opsize, i;
534                 
535                 switch (opcode) {
536                 case TCPOPT_EOL:
537                         return;
538                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
539                         length--;
540                         continue;
541                 default:
542                         opsize=*ptr++;
543                         if (opsize < 2) /* "silly options" */
544                                 return;
545                         if (opsize > length)
546                                 break;  /* don't parse partial options */
547
548                         if (opcode == TCPOPT_SACK 
549                             && opsize >= (TCPOLEN_SACK_BASE 
550                                           + TCPOLEN_SACK_PERBLOCK)
551                             && !((opsize - TCPOLEN_SACK_BASE) 
552                                  % TCPOLEN_SACK_PERBLOCK)) {
553                                 for (i = 0;
554                                      i < (opsize - TCPOLEN_SACK_BASE);
555                                      i += TCPOLEN_SACK_PERBLOCK) {
556                                         tmp = ntohl(*((u_int32_t *)(ptr+i)+1));
557                                         
558                                         if (after(tmp, *sack))
559                                                 *sack = tmp;
560                                 }
561                                 return;
562                         }
563                         ptr += opsize - 2;
564                         length -= opsize;
565                 }
566         }
567 }
568
569 static int tcp_in_window(struct ip_ct_tcp *state, 
570                          enum ip_conntrack_dir dir,
571                          unsigned int index,
572                          const struct sk_buff *skb,
573                          struct iphdr *iph,
574                          struct tcphdr *tcph)
575 {
576         struct ip_ct_tcp_state *sender = &state->seen[dir];
577         struct ip_ct_tcp_state *receiver = &state->seen[!dir];
578         __u32 seq, ack, sack, end, win, swin;
579         int res;
580         
581         /*
582          * Get the required data from the packet.
583          */
584         seq = ntohl(tcph->seq);
585         ack = sack = ntohl(tcph->ack_seq);
586         win = ntohs(tcph->window);
587         end = segment_seq_plus_len(seq, skb->len, iph, tcph);
588         
589         if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
590                 tcp_sack(skb, iph, tcph, &sack);
591                 
592         DEBUGP("tcp_in_window: START\n");
593         DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
594                "seq=%u ack=%u sack=%u win=%u end=%u\n",
595                 NIPQUAD(iph->saddr), ntohs(tcph->source), 
596                 NIPQUAD(iph->daddr), ntohs(tcph->dest),
597                 seq, ack, sack, win, end);
598         DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
599                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
600                 sender->td_end, sender->td_maxend, sender->td_maxwin,
601                 sender->td_scale, 
602                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin, 
603                 receiver->td_scale);
604                 
605         if (sender->td_end == 0) {
606                 /*
607                  * Initialize sender data.
608                  */
609                 if (tcph->syn && tcph->ack) {
610                         /*
611                          * Outgoing SYN-ACK in reply to a SYN.
612                          */
613                         sender->td_end = 
614                         sender->td_maxend = end;
615                         sender->td_maxwin = (win == 0 ? 1 : win);
616
617                         tcp_options(skb, iph, tcph, sender);
618                         /* 
619                          * RFC 1323:
620                          * Both sides must send the Window Scale option
621                          * to enable window scaling in either direction.
622                          */
623                         if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
624                               && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
625                                 sender->td_scale = 
626                                 receiver->td_scale = 0;
627                 } else {
628                         /*
629                          * We are in the middle of a connection,
630                          * its history is lost for us.
631                          * Let's try to use the data from the packet.
632                          */
633                         sender->td_end = end;
634                         sender->td_maxwin = (win == 0 ? 1 : win);
635                         sender->td_maxend = end + sender->td_maxwin;
636                 }
637         } else if (((state->state == TCP_CONNTRACK_SYN_SENT
638                      && dir == IP_CT_DIR_ORIGINAL)
639                     || (state->state == TCP_CONNTRACK_SYN_RECV
640                         && dir == IP_CT_DIR_REPLY))
641                     && after(end, sender->td_end)) {
642                 /*
643                  * RFC 793: "if a TCP is reinitialized ... then it need
644                  * not wait at all; it must only be sure to use sequence 
645                  * numbers larger than those recently used."
646                  */
647                 sender->td_end =
648                 sender->td_maxend = end;
649                 sender->td_maxwin = (win == 0 ? 1 : win);
650
651                 tcp_options(skb, iph, tcph, sender);
652         }
653         
654         if (!(tcph->ack)) {
655                 /*
656                  * If there is no ACK, just pretend it was set and OK.
657                  */
658                 ack = sack = receiver->td_end;
659         } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) == 
660                     (TCP_FLAG_ACK|TCP_FLAG_RST)) 
661                    && (ack == 0)) {
662                 /*
663                  * Broken TCP stacks, that set ACK in RST packets as well
664                  * with zero ack value.
665                  */
666                 ack = sack = receiver->td_end;
667         }
668
669         if (seq == end
670             && (!tcph->rst 
671                 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
672                 /*
673                  * Packets contains no data: we assume it is valid
674                  * and check the ack value only.
675                  * However RST segments are always validated by their
676                  * SEQ number, except when seq == 0 (reset sent answering
677                  * SYN.
678                  */
679                 seq = end = sender->td_end;
680                 
681         DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
682                "seq=%u ack=%u sack =%u win=%u end=%u\n",
683                 NIPQUAD(iph->saddr), ntohs(tcph->source),
684                 NIPQUAD(iph->daddr), ntohs(tcph->dest),
685                 seq, ack, sack, win, end);
686         DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
687                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
688                 sender->td_end, sender->td_maxend, sender->td_maxwin,
689                 sender->td_scale, 
690                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
691                 receiver->td_scale);
692         
693         DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
694                 before(seq, sender->td_maxend + 1),
695                 after(end, sender->td_end - receiver->td_maxwin - 1),
696                 before(sack, receiver->td_end + 1),
697                 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
698         
699         if (sender->loose || receiver->loose ||
700             (before(seq, sender->td_maxend + 1) &&
701              after(end, sender->td_end - receiver->td_maxwin - 1) &&
702              before(sack, receiver->td_end + 1) &&
703              after(ack, receiver->td_end - MAXACKWINDOW(sender)))) {
704                 /*
705                  * Take into account window scaling (RFC 1323).
706                  */
707                 if (!tcph->syn)
708                         win <<= sender->td_scale;
709                 
710                 /*
711                  * Update sender data.
712                  */
713                 swin = win + (sack - ack);
714                 if (sender->td_maxwin < swin)
715                         sender->td_maxwin = swin;
716                 if (after(end, sender->td_end))
717                         sender->td_end = end;
718                 /*
719                  * Update receiver data.
720                  */
721                 if (after(end, sender->td_maxend))
722                         receiver->td_maxwin += end - sender->td_maxend;
723                 if (after(sack + win, receiver->td_maxend - 1)) {
724                         receiver->td_maxend = sack + win;
725                         if (win == 0)
726                                 receiver->td_maxend++;
727                 }
728
729                 /* 
730                  * Check retransmissions.
731                  */
732                 if (index == TCP_ACK_SET) {
733                         if (state->last_dir == dir
734                             && state->last_seq == seq
735                             && state->last_ack == ack
736                             && state->last_end == end)
737                                 state->retrans++;
738                         else {
739                                 state->last_dir = dir;
740                                 state->last_seq = seq;
741                                 state->last_ack = ack;
742                                 state->last_end = end;
743                                 state->retrans = 0;
744                         }
745                 }
746                 /*
747                  * Close the window of disabled window tracking :-)
748                  */
749                 if (sender->loose)
750                         sender->loose--;
751                 
752                 res = 1;
753         } else {
754                 if (LOG_INVALID(IPPROTO_TCP))
755                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
756                         "ip_ct_tcp: %s ",
757                         before(seq, sender->td_maxend + 1) ?
758                         after(end, sender->td_end - receiver->td_maxwin - 1) ?
759                         before(sack, receiver->td_end + 1) ?
760                         after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
761                         : "ACK is under the lower bound (possible overly delayed ACK)"
762                         : "ACK is over the upper bound (ACKed data not seen yet)"
763                         : "SEQ is under the lower bound (already ACKed data retransmitted)"
764                         : "SEQ is over the upper bound (over the window of the receiver)");
765
766                 res = ip_ct_tcp_be_liberal;
767         }
768   
769         DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
770                "receiver end=%u maxend=%u maxwin=%u\n",
771                 res, sender->td_end, sender->td_maxend, sender->td_maxwin, 
772                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
773
774         return res;
775 }
776
777 #ifdef CONFIG_IP_NF_NAT_NEEDED
778 /* Update sender->td_end after NAT successfully mangled the packet */
779 void ip_conntrack_tcp_update(struct sk_buff *skb,
780                              struct ip_conntrack *conntrack, 
781                              enum ip_conntrack_dir dir)
782 {
783         struct iphdr *iph = skb->nh.iph;
784         struct tcphdr *tcph = (void *)skb->nh.iph + skb->nh.iph->ihl*4;
785         __u32 end;
786 #ifdef DEBUGP_VARS
787         struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
788         struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
789 #endif
790
791         end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, iph, tcph);
792         
793         write_lock_bh(&tcp_lock);
794         /*
795          * We have to worry for the ack in the reply packet only...
796          */
797         if (after(end, conntrack->proto.tcp.seen[dir].td_end))
798                 conntrack->proto.tcp.seen[dir].td_end = end;
799         conntrack->proto.tcp.last_end = end;
800         write_unlock_bh(&tcp_lock);
801         DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
802                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
803                 sender->td_end, sender->td_maxend, sender->td_maxwin,
804                 sender->td_scale, 
805                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
806                 receiver->td_scale);
807 }
808  
809 #endif
810
811 #define TH_FIN  0x01
812 #define TH_SYN  0x02
813 #define TH_RST  0x04
814 #define TH_PUSH 0x08
815 #define TH_ACK  0x10
816 #define TH_URG  0x20
817 #define TH_ECE  0x40
818 #define TH_CWR  0x80
819
820 /* table of valid flag combinations - ECE and CWR are always valid */
821 static const u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
822 {
823         [TH_SYN]                        = 1,
824         [TH_SYN|TH_ACK]                 = 1,
825         [TH_SYN|TH_PUSH]                = 1,
826         [TH_SYN|TH_ACK|TH_PUSH]         = 1,
827         [TH_RST]                        = 1,
828         [TH_RST|TH_ACK]                 = 1,
829         [TH_RST|TH_ACK|TH_PUSH]         = 1,
830         [TH_FIN|TH_ACK]                 = 1,
831         [TH_ACK]                        = 1,
832         [TH_ACK|TH_PUSH]                = 1,
833         [TH_ACK|TH_URG]                 = 1,
834         [TH_ACK|TH_URG|TH_PUSH]         = 1,
835         [TH_FIN|TH_ACK|TH_PUSH]         = 1,
836         [TH_FIN|TH_ACK|TH_URG]          = 1,
837         [TH_FIN|TH_ACK|TH_URG|TH_PUSH]  = 1,
838 };
839
840 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c.  */
841 static int tcp_error(struct sk_buff *skb,
842                      enum ip_conntrack_info *ctinfo,
843                      unsigned int hooknum)
844 {
845         struct iphdr *iph = skb->nh.iph;
846         struct tcphdr _tcph, *th;
847         unsigned int tcplen = skb->len - iph->ihl * 4;
848         u_int8_t tcpflags;
849
850         /* Smaller that minimal TCP header? */
851         th = skb_header_pointer(skb, iph->ihl * 4,
852                                 sizeof(_tcph), &_tcph);
853         if (th == NULL) {
854                 if (LOG_INVALID(IPPROTO_TCP))
855                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
856                                 "ip_ct_tcp: short packet ");
857                 return -NF_ACCEPT;
858         }
859   
860         /* Not whole TCP header or malformed packet */
861         if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
862                 if (LOG_INVALID(IPPROTO_TCP))
863                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
864                                 "ip_ct_tcp: truncated/malformed packet ");
865                 return -NF_ACCEPT;
866         }
867   
868         /* Checksum invalid? Ignore.
869          * We skip checking packets on the outgoing path
870          * because the semantic of CHECKSUM_HW is different there 
871          * and moreover root might send raw packets.
872          */
873         /* FIXME: Source route IP option packets --RR */
874         if (hooknum == NF_IP_PRE_ROUTING
875             && skb->ip_summed != CHECKSUM_UNNECESSARY
876             && csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen, IPPROTO_TCP,
877                                  skb->ip_summed == CHECKSUM_HW ? skb->csum
878                                  : skb_checksum(skb, iph->ihl*4, tcplen, 0))) {
879                 if (LOG_INVALID(IPPROTO_TCP))
880                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
881                                   "ip_ct_tcp: bad TCP checksum ");
882                 return -NF_ACCEPT;
883         }
884
885         /* Check TCP flags. */
886         tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
887         if (!tcp_valid_flags[tcpflags]) {
888                 if (LOG_INVALID(IPPROTO_TCP))
889                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
890                                   "ip_ct_tcp: invalid TCP flag combination ");
891                 return -NF_ACCEPT;
892         }
893
894         return NF_ACCEPT;
895 }
896
897 /* Returns verdict for packet, or -1 for invalid. */
898 static int tcp_packet(struct ip_conntrack *conntrack,
899                       const struct sk_buff *skb,
900                       enum ip_conntrack_info ctinfo)
901 {
902         enum tcp_conntrack new_state, old_state;
903         enum ip_conntrack_dir dir;
904         struct iphdr *iph = skb->nh.iph;
905         struct tcphdr *th, _tcph;
906         unsigned long timeout;
907         unsigned int index;
908         
909         th = skb_header_pointer(skb, iph->ihl * 4,
910                                 sizeof(_tcph), &_tcph);
911         BUG_ON(th == NULL);
912         
913         write_lock_bh(&tcp_lock);
914         old_state = conntrack->proto.tcp.state;
915         dir = CTINFO2DIR(ctinfo);
916         index = get_conntrack_index(th);
917         new_state = tcp_conntracks[dir][index][old_state];
918
919         switch (new_state) {
920         case TCP_CONNTRACK_IGNORE:
921                 /* Ignored packets:
922                  * 
923                  * a) SYN in ORIGINAL
924                  * b) SYN/ACK in REPLY
925                  * c) ACK in reply direction after initial SYN in original.
926                  */
927                 if (index == TCP_SYNACK_SET
928                     && conntrack->proto.tcp.last_index == TCP_SYN_SET
929                     && conntrack->proto.tcp.last_dir != dir
930                     && ntohl(th->ack_seq) ==
931                              conntrack->proto.tcp.last_end) {
932                         /* This SYN/ACK acknowledges a SYN that we earlier 
933                          * ignored as invalid. This means that the client and
934                          * the server are both in sync, while the firewall is
935                          * not. We kill this session and block the SYN/ACK so
936                          * that the client cannot but retransmit its SYN and 
937                          * thus initiate a clean new session.
938                          */
939                         write_unlock_bh(&tcp_lock);
940                         if (LOG_INVALID(IPPROTO_TCP))
941                                 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
942                                               NULL, "ip_ct_tcp: "
943                                               "killing out of sync session ");
944                         if (del_timer(&conntrack->timeout))
945                                 conntrack->timeout.function((unsigned long)
946                                                             conntrack);
947                         return -NF_DROP;
948                 }
949                 conntrack->proto.tcp.last_index = index;
950                 conntrack->proto.tcp.last_dir = dir;
951                 conntrack->proto.tcp.last_seq = ntohl(th->seq);
952                 conntrack->proto.tcp.last_end = 
953                     segment_seq_plus_len(ntohl(th->seq), skb->len, iph, th);
954                 
955                 write_unlock_bh(&tcp_lock);
956                 if (LOG_INVALID(IPPROTO_TCP))
957                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
958                                   "ip_ct_tcp: invalid packet ignored ");
959                 return NF_ACCEPT;
960         case TCP_CONNTRACK_MAX:
961                 /* Invalid packet */
962                 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
963                        dir, get_conntrack_index(th),
964                        old_state);
965                 write_unlock_bh(&tcp_lock);
966                 if (LOG_INVALID(IPPROTO_TCP))
967                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
968                                   "ip_ct_tcp: invalid state ");
969                 return -NF_ACCEPT;
970         case TCP_CONNTRACK_SYN_SENT:
971                 if (old_state < TCP_CONNTRACK_TIME_WAIT)
972                         break;
973                 if ((conntrack->proto.tcp.seen[dir].flags &
974                          IP_CT_TCP_FLAG_CLOSE_INIT)
975                     || after(ntohl(th->seq),
976                              conntrack->proto.tcp.seen[dir].td_end)) {  
977                         /* Attempt to reopen a closed connection.
978                         * Delete this connection and look up again. */
979                         write_unlock_bh(&tcp_lock);
980                         if (del_timer(&conntrack->timeout))
981                                 conntrack->timeout.function((unsigned long)
982                                                             conntrack);
983                         return -NF_REPEAT;
984                 } else {
985                         write_unlock_bh(&tcp_lock);
986                         if (LOG_INVALID(IPPROTO_TCP))
987                                 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
988                                               NULL, "ip_ct_tcp: invalid SYN");
989                         return -NF_ACCEPT;
990                 }
991         case TCP_CONNTRACK_CLOSE:
992                 if (index == TCP_RST_SET
993                     && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
994                          && conntrack->proto.tcp.last_index == TCP_SYN_SET)
995                         || (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
996                             && conntrack->proto.tcp.last_index == TCP_ACK_SET))
997                     && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
998                         /* RST sent to invalid SYN or ACK we had let through
999                          * at a) and c) above:
1000                          *
1001                          * a) SYN was in window then
1002                          * c) we hold a half-open connection.
1003                          *
1004                          * Delete our connection entry.
1005                          * We skip window checking, because packet might ACK
1006                          * segments we ignored. */
1007                         goto in_window;
1008                 }
1009                 /* Just fall through */
1010         default:
1011                 /* Keep compilers happy. */
1012                 break;
1013         }
1014
1015         if (!tcp_in_window(&conntrack->proto.tcp, dir, index, 
1016                            skb, iph, th)) {
1017                 write_unlock_bh(&tcp_lock);
1018                 return -NF_ACCEPT;
1019         }
1020     in_window:
1021         /* From now on we have got in-window packets */ 
1022         conntrack->proto.tcp.last_index = index;
1023
1024         DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
1025                "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1026                 NIPQUAD(iph->saddr), ntohs(th->source),
1027                 NIPQUAD(iph->daddr), ntohs(th->dest),
1028                 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1029                 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1030                 old_state, new_state);
1031
1032         conntrack->proto.tcp.state = new_state;
1033         if (old_state != new_state 
1034             && (new_state == TCP_CONNTRACK_FIN_WAIT
1035                 || new_state == TCP_CONNTRACK_CLOSE))
1036                 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1037         timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
1038                   && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans
1039                   ? ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
1040         write_unlock_bh(&tcp_lock);
1041
1042         ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
1043         if (new_state != old_state)
1044                 ip_conntrack_event_cache(IPCT_PROTOINFO, skb);
1045
1046         if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
1047                 /* If only reply is a RST, we can consider ourselves not to
1048                    have an established connection: this is a fairly common
1049                    problem case, so we can delete the conntrack
1050                    immediately.  --RR */
1051                 if (th->rst) {
1052                         if (del_timer(&conntrack->timeout))
1053                                 conntrack->timeout.function((unsigned long)
1054                                                             conntrack);
1055                         return NF_ACCEPT;
1056                 }
1057         } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
1058                    && (old_state == TCP_CONNTRACK_SYN_RECV
1059                        || old_state == TCP_CONNTRACK_ESTABLISHED)
1060                    && new_state == TCP_CONNTRACK_ESTABLISHED) {
1061                 /* Set ASSURED if we see see valid ack in ESTABLISHED 
1062                    after SYN_RECV or a valid answer for a picked up 
1063                    connection. */
1064                 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1065                 ip_conntrack_event_cache(IPCT_STATUS, skb);
1066         }
1067         ip_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1068
1069         return NF_ACCEPT;
1070 }
1071  
1072 /* Called when a new connection for this protocol found. */
1073 static int tcp_new(struct ip_conntrack *conntrack,
1074                    const struct sk_buff *skb)
1075 {
1076         enum tcp_conntrack new_state;
1077         struct iphdr *iph = skb->nh.iph;
1078         struct tcphdr *th, _tcph;
1079 #ifdef DEBUGP_VARS
1080         struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1081         struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1082 #endif
1083
1084         th = skb_header_pointer(skb, iph->ihl * 4,
1085                                 sizeof(_tcph), &_tcph);
1086         BUG_ON(th == NULL);
1087         
1088         /* Don't need lock here: this conntrack not in circulation yet */
1089         new_state
1090                 = tcp_conntracks[0][get_conntrack_index(th)]
1091                 [TCP_CONNTRACK_NONE];
1092
1093         /* Invalid: delete conntrack */
1094         if (new_state >= TCP_CONNTRACK_MAX) {
1095                 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1096                 return 0;
1097         }
1098
1099         if (new_state == TCP_CONNTRACK_SYN_SENT) {
1100                 /* SYN packet */
1101                 conntrack->proto.tcp.seen[0].td_end =
1102                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1103                                              iph, th);
1104                 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1105                 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1106                         conntrack->proto.tcp.seen[0].td_maxwin = 1;
1107                 conntrack->proto.tcp.seen[0].td_maxend =
1108                         conntrack->proto.tcp.seen[0].td_end;
1109
1110                 tcp_options(skb, iph, th, &conntrack->proto.tcp.seen[0]);
1111                 conntrack->proto.tcp.seen[1].flags = 0;
1112                 conntrack->proto.tcp.seen[0].loose = 
1113                 conntrack->proto.tcp.seen[1].loose = 0;
1114         } else if (ip_ct_tcp_loose == 0) {
1115                 /* Don't try to pick up connections. */
1116                 return 0;
1117         } else {
1118                 /*
1119                  * We are in the middle of a connection,
1120                  * its history is lost for us.
1121                  * Let's try to use the data from the packet.
1122                  */
1123                 conntrack->proto.tcp.seen[0].td_end =
1124                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1125                                              iph, th);
1126                 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1127                 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1128                         conntrack->proto.tcp.seen[0].td_maxwin = 1;
1129                 conntrack->proto.tcp.seen[0].td_maxend =
1130                         conntrack->proto.tcp.seen[0].td_end + 
1131                         conntrack->proto.tcp.seen[0].td_maxwin;
1132                 conntrack->proto.tcp.seen[0].td_scale = 0;
1133
1134                 /* We assume SACK. Should we assume window scaling too? */
1135                 conntrack->proto.tcp.seen[0].flags =
1136                 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
1137                 conntrack->proto.tcp.seen[0].loose = 
1138                 conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
1139         }
1140     
1141         conntrack->proto.tcp.seen[1].td_end = 0;
1142         conntrack->proto.tcp.seen[1].td_maxend = 0;
1143         conntrack->proto.tcp.seen[1].td_maxwin = 1;
1144         conntrack->proto.tcp.seen[1].td_scale = 0;      
1145
1146         /* tcp_packet will set them */
1147         conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1148         conntrack->proto.tcp.last_index = TCP_NONE_SET;
1149          
1150         DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1151                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1152                 sender->td_end, sender->td_maxend, sender->td_maxwin,
1153                 sender->td_scale, 
1154                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1155                 receiver->td_scale);
1156         return 1;
1157 }
1158   
1159 struct ip_conntrack_protocol ip_conntrack_protocol_tcp =
1160 {
1161         .proto                  = IPPROTO_TCP,
1162         .name                   = "tcp",
1163         .pkt_to_tuple           = tcp_pkt_to_tuple,
1164         .invert_tuple           = tcp_invert_tuple,
1165         .print_tuple            = tcp_print_tuple,
1166         .print_conntrack        = tcp_print_conntrack,
1167         .packet                 = tcp_packet,
1168         .new                    = tcp_new,
1169         .error                  = tcp_error,
1170 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
1171     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
1172         .to_nfattr              = tcp_to_nfattr,
1173         .from_nfattr            = nfattr_to_tcp,
1174         .tuple_to_nfattr        = ip_ct_port_tuple_to_nfattr,
1175         .nfattr_to_tuple        = ip_ct_port_nfattr_to_tuple,
1176 #endif
1177 };