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