]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv4/netfilter/ip_conntrack_proto_tcp.c
Pull ar-k0-usage into release branch
[karo-tx-linux.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 long ip_ct_tcp_timeout_syn_sent =      2 MINS;
89 unsigned long ip_ct_tcp_timeout_syn_recv =     60 SECS;
90 unsigned long ip_ct_tcp_timeout_established =   5 DAYS;
91 unsigned long ip_ct_tcp_timeout_fin_wait =      2 MINS;
92 unsigned long ip_ct_tcp_timeout_close_wait =   60 SECS;
93 unsigned long ip_ct_tcp_timeout_last_ack =     30 SECS;
94 unsigned long ip_ct_tcp_timeout_time_wait =     2 MINS;
95 unsigned long 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 long ip_ct_tcp_timeout_max_retrans =     5 MINS;
101  
102 static unsigned long * 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 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, sIV, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
276 /*
277  *      sSS -> sIV      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 = NFA_NEST(skb, CTA_PROTOINFO_TCP);
345         
346         read_lock_bh(&tcp_lock);
347         NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
348                 &ct->proto.tcp.state);
349         read_unlock_bh(&tcp_lock);
350
351         NFA_NEST_END(skb, nest_parms);
352
353         return 0;
354
355 nfattr_failure:
356         read_unlock_bh(&tcp_lock);
357         return -1;
358 }
359
360 static int nfattr_to_tcp(struct nfattr *cda[], struct ip_conntrack *ct)
361 {
362         struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
363         struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
364
365         if (nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr) < 0)
366                 goto nfattr_failure;
367
368         if (!tb[CTA_PROTOINFO_TCP_STATE-1])
369                 return -EINVAL;
370
371         write_lock_bh(&tcp_lock);
372         ct->proto.tcp.state = 
373                 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
374         write_unlock_bh(&tcp_lock);
375
376         return 0;
377
378 nfattr_failure:
379         return -1;
380 }
381 #endif
382
383 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
384 {
385         if (tcph->rst) return TCP_RST_SET;
386         else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
387         else if (tcph->fin) return TCP_FIN_SET;
388         else if (tcph->ack) return TCP_ACK_SET;
389         else return TCP_NONE_SET;
390 }
391
392 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
393    in IP Filter' by Guido van Rooij.
394    
395    http://www.nluug.nl/events/sane2000/papers.html
396    http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
397    
398    The boundaries and the conditions are changed according to RFC793:
399    the packet must intersect the window (i.e. segments may be
400    after the right or before the left edge) and thus receivers may ACK
401    segments after the right edge of the window.
402
403         td_maxend = max(sack + max(win,1)) seen in reply packets
404         td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
405         td_maxwin += seq + len - sender.td_maxend
406                         if seq + len > sender.td_maxend
407         td_end    = max(seq + len) seen in sent packets
408    
409    I.   Upper bound for valid data:     seq <= sender.td_maxend
410    II.  Lower bound for valid data:     seq + len >= sender.td_end - receiver.td_maxwin
411    III. Upper bound for valid ack:      sack <= receiver.td_end
412    IV.  Lower bound for valid ack:      ack >= receiver.td_end - MAXACKWINDOW
413         
414    where sack is the highest right edge of sack block found in the packet.
415         
416    The upper bound limit for a valid ack is not ignored - 
417    we doesn't have to deal with fragments. 
418 */
419
420 static inline __u32 segment_seq_plus_len(__u32 seq,
421                                          size_t len,
422                                          struct iphdr *iph,
423                                          struct tcphdr *tcph)
424 {
425         return (seq + len - (iph->ihl + tcph->doff)*4
426                 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
427 }
428   
429 /* Fixme: what about big packets? */
430 #define MAXACKWINCONST                  66000
431 #define MAXACKWINDOW(sender)                                            \
432         ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin     \
433                                               : MAXACKWINCONST)
434   
435 /*
436  * Simplified tcp_parse_options routine from tcp_input.c
437  */
438 static void tcp_options(const struct sk_buff *skb,
439                         struct iphdr *iph,
440                         struct tcphdr *tcph, 
441                         struct ip_ct_tcp_state *state)
442 {
443         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
444         unsigned char *ptr;
445         int length = (tcph->doff*4) - sizeof(struct tcphdr);
446         
447         if (!length)
448                 return;
449
450         ptr = skb_header_pointer(skb,
451                                  (iph->ihl * 4) + sizeof(struct tcphdr),
452                                  length, buff);
453         BUG_ON(ptr == NULL);
454
455         state->td_scale = 
456         state->flags = 0;
457         
458         while (length > 0) {
459                 int opcode=*ptr++;
460                 int opsize;
461                 
462                 switch (opcode) {
463                 case TCPOPT_EOL:
464                         return;
465                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
466                         length--;
467                         continue;
468                 default:
469                         opsize=*ptr++;
470                         if (opsize < 2) /* "silly options" */
471                                 return;
472                         if (opsize > length)
473                                 break;  /* don't parse partial options */
474
475                         if (opcode == TCPOPT_SACK_PERM 
476                             && opsize == TCPOLEN_SACK_PERM)
477                                 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
478                         else if (opcode == TCPOPT_WINDOW
479                                  && opsize == TCPOLEN_WINDOW) {
480                                 state->td_scale = *(u_int8_t *)ptr;
481                                 
482                                 if (state->td_scale > 14) {
483                                         /* See RFC1323 */
484                                         state->td_scale = 14;
485                                 }
486                                 state->flags |=
487                                         IP_CT_TCP_FLAG_WINDOW_SCALE;
488                         }
489                         ptr += opsize - 2;
490                         length -= opsize;
491                 }
492         }
493 }
494
495 static void tcp_sack(const struct sk_buff *skb,
496                      struct iphdr *iph,
497                      struct tcphdr *tcph,
498                      __u32 *sack)
499 {
500         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
501         unsigned char *ptr;
502         int length = (tcph->doff*4) - sizeof(struct tcphdr);
503         __u32 tmp;
504
505         if (!length)
506                 return;
507
508         ptr = skb_header_pointer(skb,
509                                  (iph->ihl * 4) + sizeof(struct tcphdr),
510                                  length, buff);
511         BUG_ON(ptr == NULL);
512
513         /* Fast path for timestamp-only option */
514         if (length == TCPOLEN_TSTAMP_ALIGNED*4
515             && *(__u32 *)ptr ==
516                 __constant_ntohl((TCPOPT_NOP << 24) 
517                                  | (TCPOPT_NOP << 16)
518                                  | (TCPOPT_TIMESTAMP << 8)
519                                  | TCPOLEN_TIMESTAMP))
520                 return;
521                 
522         while (length > 0) {
523                 int opcode=*ptr++;
524                 int opsize, i;
525                 
526                 switch (opcode) {
527                 case TCPOPT_EOL:
528                         return;
529                 case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
530                         length--;
531                         continue;
532                 default:
533                         opsize=*ptr++;
534                         if (opsize < 2) /* "silly options" */
535                                 return;
536                         if (opsize > length)
537                                 break;  /* don't parse partial options */
538
539                         if (opcode == TCPOPT_SACK 
540                             && opsize >= (TCPOLEN_SACK_BASE 
541                                           + TCPOLEN_SACK_PERBLOCK)
542                             && !((opsize - TCPOLEN_SACK_BASE) 
543                                  % TCPOLEN_SACK_PERBLOCK)) {
544                                 for (i = 0;
545                                      i < (opsize - TCPOLEN_SACK_BASE);
546                                      i += TCPOLEN_SACK_PERBLOCK) {
547                                         tmp = ntohl(*((u_int32_t *)(ptr+i)+1));
548                                         
549                                         if (after(tmp, *sack))
550                                                 *sack = tmp;
551                                 }
552                                 return;
553                         }
554                         ptr += opsize - 2;
555                         length -= opsize;
556                 }
557         }
558 }
559
560 static int tcp_in_window(struct ip_ct_tcp *state, 
561                          enum ip_conntrack_dir dir,
562                          unsigned int index,
563                          const struct sk_buff *skb,
564                          struct iphdr *iph,
565                          struct tcphdr *tcph)
566 {
567         struct ip_ct_tcp_state *sender = &state->seen[dir];
568         struct ip_ct_tcp_state *receiver = &state->seen[!dir];
569         __u32 seq, ack, sack, end, win, swin;
570         int res;
571         
572         /*
573          * Get the required data from the packet.
574          */
575         seq = ntohl(tcph->seq);
576         ack = sack = ntohl(tcph->ack_seq);
577         win = ntohs(tcph->window);
578         end = segment_seq_plus_len(seq, skb->len, iph, tcph);
579         
580         if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
581                 tcp_sack(skb, iph, tcph, &sack);
582                 
583         DEBUGP("tcp_in_window: START\n");
584         DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
585                "seq=%u ack=%u sack=%u win=%u end=%u\n",
586                 NIPQUAD(iph->saddr), ntohs(tcph->source), 
587                 NIPQUAD(iph->daddr), ntohs(tcph->dest),
588                 seq, ack, sack, win, end);
589         DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
590                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
591                 sender->td_end, sender->td_maxend, sender->td_maxwin,
592                 sender->td_scale, 
593                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin, 
594                 receiver->td_scale);
595                 
596         if (sender->td_end == 0) {
597                 /*
598                  * Initialize sender data.
599                  */
600                 if (tcph->syn && tcph->ack) {
601                         /*
602                          * Outgoing SYN-ACK in reply to a SYN.
603                          */
604                         sender->td_end = 
605                         sender->td_maxend = end;
606                         sender->td_maxwin = (win == 0 ? 1 : win);
607
608                         tcp_options(skb, iph, tcph, sender);
609                         /* 
610                          * RFC 1323:
611                          * Both sides must send the Window Scale option
612                          * to enable window scaling in either direction.
613                          */
614                         if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
615                               && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
616                                 sender->td_scale = 
617                                 receiver->td_scale = 0;
618                 } else {
619                         /*
620                          * We are in the middle of a connection,
621                          * its history is lost for us.
622                          * Let's try to use the data from the packet.
623                          */
624                         sender->td_end = end;
625                         sender->td_maxwin = (win == 0 ? 1 : win);
626                         sender->td_maxend = end + sender->td_maxwin;
627                 }
628         } else if (((state->state == TCP_CONNTRACK_SYN_SENT
629                      && dir == IP_CT_DIR_ORIGINAL)
630                     || (state->state == TCP_CONNTRACK_SYN_RECV
631                         && dir == IP_CT_DIR_REPLY))
632                     && after(end, sender->td_end)) {
633                 /*
634                  * RFC 793: "if a TCP is reinitialized ... then it need
635                  * not wait at all; it must only be sure to use sequence 
636                  * numbers larger than those recently used."
637                  */
638                 sender->td_end =
639                 sender->td_maxend = end;
640                 sender->td_maxwin = (win == 0 ? 1 : win);
641
642                 tcp_options(skb, iph, tcph, sender);
643         }
644         
645         if (!(tcph->ack)) {
646                 /*
647                  * If there is no ACK, just pretend it was set and OK.
648                  */
649                 ack = sack = receiver->td_end;
650         } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) == 
651                     (TCP_FLAG_ACK|TCP_FLAG_RST)) 
652                    && (ack == 0)) {
653                 /*
654                  * Broken TCP stacks, that set ACK in RST packets as well
655                  * with zero ack value.
656                  */
657                 ack = sack = receiver->td_end;
658         }
659
660         if (seq == end
661             && (!tcph->rst 
662                 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
663                 /*
664                  * Packets contains no data: we assume it is valid
665                  * and check the ack value only.
666                  * However RST segments are always validated by their
667                  * SEQ number, except when seq == 0 (reset sent answering
668                  * SYN.
669                  */
670                 seq = end = sender->td_end;
671                 
672         DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
673                "seq=%u ack=%u sack =%u win=%u end=%u\n",
674                 NIPQUAD(iph->saddr), ntohs(tcph->source),
675                 NIPQUAD(iph->daddr), ntohs(tcph->dest),
676                 seq, ack, sack, win, end);
677         DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
678                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
679                 sender->td_end, sender->td_maxend, sender->td_maxwin,
680                 sender->td_scale, 
681                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
682                 receiver->td_scale);
683         
684         DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
685                 before(seq, sender->td_maxend + 1),
686                 after(end, sender->td_end - receiver->td_maxwin - 1),
687                 before(sack, receiver->td_end + 1),
688                 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
689         
690         if (sender->loose || receiver->loose ||
691             (before(seq, sender->td_maxend + 1) &&
692              after(end, sender->td_end - receiver->td_maxwin - 1) &&
693              before(sack, receiver->td_end + 1) &&
694              after(ack, receiver->td_end - MAXACKWINDOW(sender)))) {
695                 /*
696                  * Take into account window scaling (RFC 1323).
697                  */
698                 if (!tcph->syn)
699                         win <<= sender->td_scale;
700                 
701                 /*
702                  * Update sender data.
703                  */
704                 swin = win + (sack - ack);
705                 if (sender->td_maxwin < swin)
706                         sender->td_maxwin = swin;
707                 if (after(end, sender->td_end))
708                         sender->td_end = end;
709                 /*
710                  * Update receiver data.
711                  */
712                 if (after(end, sender->td_maxend))
713                         receiver->td_maxwin += end - sender->td_maxend;
714                 if (after(sack + win, receiver->td_maxend - 1)) {
715                         receiver->td_maxend = sack + win;
716                         if (win == 0)
717                                 receiver->td_maxend++;
718                 }
719
720                 /* 
721                  * Check retransmissions.
722                  */
723                 if (index == TCP_ACK_SET) {
724                         if (state->last_dir == dir
725                             && state->last_seq == seq
726                             && state->last_ack == ack
727                             && state->last_end == end)
728                                 state->retrans++;
729                         else {
730                                 state->last_dir = dir;
731                                 state->last_seq = seq;
732                                 state->last_ack = ack;
733                                 state->last_end = end;
734                                 state->retrans = 0;
735                         }
736                 }
737                 /*
738                  * Close the window of disabled window tracking :-)
739                  */
740                 if (sender->loose)
741                         sender->loose--;
742                 
743                 res = 1;
744         } else {
745                 if (LOG_INVALID(IPPROTO_TCP))
746                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
747                         "ip_ct_tcp: %s ",
748                         before(seq, sender->td_maxend + 1) ?
749                         after(end, sender->td_end - receiver->td_maxwin - 1) ?
750                         before(sack, receiver->td_end + 1) ?
751                         after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
752                         : "ACK is under the lower bound (possible overly delayed ACK)"
753                         : "ACK is over the upper bound (ACKed data not seen yet)"
754                         : "SEQ is under the lower bound (already ACKed data retransmitted)"
755                         : "SEQ is over the upper bound (over the window of the receiver)");
756
757                 res = ip_ct_tcp_be_liberal;
758         }
759   
760         DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
761                "receiver end=%u maxend=%u maxwin=%u\n",
762                 res, sender->td_end, sender->td_maxend, sender->td_maxwin, 
763                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
764
765         return res;
766 }
767
768 #ifdef CONFIG_IP_NF_NAT_NEEDED
769 /* Update sender->td_end after NAT successfully mangled the packet */
770 void ip_conntrack_tcp_update(struct sk_buff *skb,
771                              struct ip_conntrack *conntrack, 
772                              enum ip_conntrack_dir dir)
773 {
774         struct iphdr *iph = skb->nh.iph;
775         struct tcphdr *tcph = (void *)skb->nh.iph + skb->nh.iph->ihl*4;
776         __u32 end;
777 #ifdef DEBUGP_VARS
778         struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
779         struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
780 #endif
781
782         end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, iph, tcph);
783         
784         write_lock_bh(&tcp_lock);
785         /*
786          * We have to worry for the ack in the reply packet only...
787          */
788         if (after(end, conntrack->proto.tcp.seen[dir].td_end))
789                 conntrack->proto.tcp.seen[dir].td_end = end;
790         conntrack->proto.tcp.last_end = end;
791         write_unlock_bh(&tcp_lock);
792         DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
793                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
794                 sender->td_end, sender->td_maxend, sender->td_maxwin,
795                 sender->td_scale, 
796                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
797                 receiver->td_scale);
798 }
799  
800 #endif
801
802 #define TH_FIN  0x01
803 #define TH_SYN  0x02
804 #define TH_RST  0x04
805 #define TH_PUSH 0x08
806 #define TH_ACK  0x10
807 #define TH_URG  0x20
808 #define TH_ECE  0x40
809 #define TH_CWR  0x80
810
811 /* table of valid flag combinations - ECE and CWR are always valid */
812 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
813 {
814         [TH_SYN]                        = 1,
815         [TH_SYN|TH_ACK]                 = 1,
816         [TH_SYN|TH_ACK|TH_PUSH]         = 1,
817         [TH_RST]                        = 1,
818         [TH_RST|TH_ACK]                 = 1,
819         [TH_RST|TH_ACK|TH_PUSH]         = 1,
820         [TH_FIN|TH_ACK]                 = 1,
821         [TH_ACK]                        = 1,
822         [TH_ACK|TH_PUSH]                = 1,
823         [TH_ACK|TH_URG]                 = 1,
824         [TH_ACK|TH_URG|TH_PUSH]         = 1,
825         [TH_FIN|TH_ACK|TH_PUSH]         = 1,
826         [TH_FIN|TH_ACK|TH_URG]          = 1,
827         [TH_FIN|TH_ACK|TH_URG|TH_PUSH]  = 1,
828 };
829
830 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c.  */
831 static int tcp_error(struct sk_buff *skb,
832                      enum ip_conntrack_info *ctinfo,
833                      unsigned int hooknum)
834 {
835         struct iphdr *iph = skb->nh.iph;
836         struct tcphdr _tcph, *th;
837         unsigned int tcplen = skb->len - iph->ihl * 4;
838         u_int8_t tcpflags;
839
840         /* Smaller that minimal TCP header? */
841         th = skb_header_pointer(skb, iph->ihl * 4,
842                                 sizeof(_tcph), &_tcph);
843         if (th == NULL) {
844                 if (LOG_INVALID(IPPROTO_TCP))
845                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
846                                 "ip_ct_tcp: short packet ");
847                 return -NF_ACCEPT;
848         }
849   
850         /* Not whole TCP header or malformed packet */
851         if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
852                 if (LOG_INVALID(IPPROTO_TCP))
853                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
854                                 "ip_ct_tcp: truncated/malformed packet ");
855                 return -NF_ACCEPT;
856         }
857   
858         /* Checksum invalid? Ignore.
859          * We skip checking packets on the outgoing path
860          * because the semantic of CHECKSUM_HW is different there 
861          * and moreover root might send raw packets.
862          */
863         /* FIXME: Source route IP option packets --RR */
864         if (hooknum == NF_IP_PRE_ROUTING
865             && skb->ip_summed != CHECKSUM_UNNECESSARY
866             && csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen, IPPROTO_TCP,
867                                  skb->ip_summed == CHECKSUM_HW ? skb->csum
868                                  : skb_checksum(skb, iph->ihl*4, tcplen, 0))) {
869                 if (LOG_INVALID(IPPROTO_TCP))
870                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
871                                   "ip_ct_tcp: bad TCP checksum ");
872                 return -NF_ACCEPT;
873         }
874
875         /* Check TCP flags. */
876         tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
877         if (!tcp_valid_flags[tcpflags]) {
878                 if (LOG_INVALID(IPPROTO_TCP))
879                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
880                                   "ip_ct_tcp: invalid TCP flag combination ");
881                 return -NF_ACCEPT;
882         }
883
884         return NF_ACCEPT;
885 }
886
887 /* Returns verdict for packet, or -1 for invalid. */
888 static int tcp_packet(struct ip_conntrack *conntrack,
889                       const struct sk_buff *skb,
890                       enum ip_conntrack_info ctinfo)
891 {
892         enum tcp_conntrack new_state, old_state;
893         enum ip_conntrack_dir dir;
894         struct iphdr *iph = skb->nh.iph;
895         struct tcphdr *th, _tcph;
896         unsigned long timeout;
897         unsigned int index;
898         
899         th = skb_header_pointer(skb, iph->ihl * 4,
900                                 sizeof(_tcph), &_tcph);
901         BUG_ON(th == NULL);
902         
903         write_lock_bh(&tcp_lock);
904         old_state = conntrack->proto.tcp.state;
905         dir = CTINFO2DIR(ctinfo);
906         index = get_conntrack_index(th);
907         new_state = tcp_conntracks[dir][index][old_state];
908
909         switch (new_state) {
910         case TCP_CONNTRACK_IGNORE:
911                 /* Either SYN in ORIGINAL
912                  * or SYN/ACK in REPLY. */
913                 if (index == TCP_SYNACK_SET
914                     && conntrack->proto.tcp.last_index == TCP_SYN_SET
915                     && conntrack->proto.tcp.last_dir != dir
916                     && ntohl(th->ack_seq) ==
917                              conntrack->proto.tcp.last_end) {
918                         /* This SYN/ACK acknowledges a SYN that we earlier 
919                          * ignored as invalid. This means that the client and
920                          * the server are both in sync, while the firewall is
921                          * not. We kill this session and block the SYN/ACK so
922                          * that the client cannot but retransmit its SYN and 
923                          * thus initiate a clean new session.
924                          */
925                         write_unlock_bh(&tcp_lock);
926                         if (LOG_INVALID(IPPROTO_TCP))
927                                 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
928                                               NULL, "ip_ct_tcp: "
929                                               "killing out of sync session ");
930                         if (del_timer(&conntrack->timeout))
931                                 conntrack->timeout.function((unsigned long)
932                                                             conntrack);
933                         return -NF_DROP;
934                 }
935                 conntrack->proto.tcp.last_index = index;
936                 conntrack->proto.tcp.last_dir = dir;
937                 conntrack->proto.tcp.last_seq = ntohl(th->seq);
938                 conntrack->proto.tcp.last_end = 
939                     segment_seq_plus_len(ntohl(th->seq), skb->len, iph, th);
940                 
941                 write_unlock_bh(&tcp_lock);
942                 if (LOG_INVALID(IPPROTO_TCP))
943                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
944                                   "ip_ct_tcp: invalid packet ignored ");
945                 return NF_ACCEPT;
946         case TCP_CONNTRACK_MAX:
947                 /* Invalid packet */
948                 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
949                        dir, get_conntrack_index(th),
950                        old_state);
951                 write_unlock_bh(&tcp_lock);
952                 if (LOG_INVALID(IPPROTO_TCP))
953                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
954                                   "ip_ct_tcp: invalid state ");
955                 return -NF_ACCEPT;
956         case TCP_CONNTRACK_SYN_SENT:
957                 if (old_state < TCP_CONNTRACK_TIME_WAIT)
958                         break;
959                 if ((conntrack->proto.tcp.seen[dir].flags &
960                          IP_CT_TCP_FLAG_CLOSE_INIT)
961                     || after(ntohl(th->seq),
962                              conntrack->proto.tcp.seen[dir].td_end)) {  
963                         /* Attempt to reopen a closed connection.
964                         * Delete this connection and look up again. */
965                         write_unlock_bh(&tcp_lock);
966                         if (del_timer(&conntrack->timeout))
967                                 conntrack->timeout.function((unsigned long)
968                                                             conntrack);
969                         return -NF_REPEAT;
970                 } else {
971                         write_unlock_bh(&tcp_lock);
972                         if (LOG_INVALID(IPPROTO_TCP))
973                                 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
974                                               NULL, "ip_ct_tcp: invalid SYN");
975                         return -NF_ACCEPT;
976                 }
977         case TCP_CONNTRACK_CLOSE:
978                 if (index == TCP_RST_SET
979                     && test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
980                     && conntrack->proto.tcp.last_index == TCP_SYN_SET
981                     && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
982                         /* RST sent to invalid SYN we had let trough
983                          * SYN was in window then, tear down connection.
984                          * We skip window checking, because packet might ACK
985                          * segments we ignored in the SYN. */
986                         goto in_window;
987                 }
988                 /* Just fall trough */
989         default:
990                 /* Keep compilers happy. */
991                 break;
992         }
993
994         if (!tcp_in_window(&conntrack->proto.tcp, dir, index, 
995                            skb, iph, th)) {
996                 write_unlock_bh(&tcp_lock);
997                 return -NF_ACCEPT;
998         }
999     in_window:
1000         /* From now on we have got in-window packets */ 
1001         conntrack->proto.tcp.last_index = index;
1002
1003         DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
1004                "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1005                 NIPQUAD(iph->saddr), ntohs(th->source),
1006                 NIPQUAD(iph->daddr), ntohs(th->dest),
1007                 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1008                 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1009                 old_state, new_state);
1010
1011         conntrack->proto.tcp.state = new_state;
1012         if (old_state != new_state 
1013             && (new_state == TCP_CONNTRACK_FIN_WAIT
1014                 || new_state == TCP_CONNTRACK_CLOSE))
1015                 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1016         timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
1017                   && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans
1018                   ? ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
1019         write_unlock_bh(&tcp_lock);
1020
1021         ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
1022         if (new_state != old_state)
1023                 ip_conntrack_event_cache(IPCT_PROTOINFO, skb);
1024
1025         if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
1026                 /* If only reply is a RST, we can consider ourselves not to
1027                    have an established connection: this is a fairly common
1028                    problem case, so we can delete the conntrack
1029                    immediately.  --RR */
1030                 if (th->rst) {
1031                         if (del_timer(&conntrack->timeout))
1032                                 conntrack->timeout.function((unsigned long)
1033                                                             conntrack);
1034                         return NF_ACCEPT;
1035                 }
1036         } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
1037                    && (old_state == TCP_CONNTRACK_SYN_RECV
1038                        || old_state == TCP_CONNTRACK_ESTABLISHED)
1039                    && new_state == TCP_CONNTRACK_ESTABLISHED) {
1040                 /* Set ASSURED if we see see valid ack in ESTABLISHED 
1041                    after SYN_RECV or a valid answer for a picked up 
1042                    connection. */
1043                 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1044                 ip_conntrack_event_cache(IPCT_STATUS, skb);
1045         }
1046         ip_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1047
1048         return NF_ACCEPT;
1049 }
1050  
1051 /* Called when a new connection for this protocol found. */
1052 static int tcp_new(struct ip_conntrack *conntrack,
1053                    const struct sk_buff *skb)
1054 {
1055         enum tcp_conntrack new_state;
1056         struct iphdr *iph = skb->nh.iph;
1057         struct tcphdr *th, _tcph;
1058 #ifdef DEBUGP_VARS
1059         struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1060         struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1061 #endif
1062
1063         th = skb_header_pointer(skb, iph->ihl * 4,
1064                                 sizeof(_tcph), &_tcph);
1065         BUG_ON(th == NULL);
1066         
1067         /* Don't need lock here: this conntrack not in circulation yet */
1068         new_state
1069                 = tcp_conntracks[0][get_conntrack_index(th)]
1070                 [TCP_CONNTRACK_NONE];
1071
1072         /* Invalid: delete conntrack */
1073         if (new_state >= TCP_CONNTRACK_MAX) {
1074                 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1075                 return 0;
1076         }
1077
1078         if (new_state == TCP_CONNTRACK_SYN_SENT) {
1079                 /* SYN packet */
1080                 conntrack->proto.tcp.seen[0].td_end =
1081                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1082                                              iph, th);
1083                 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1084                 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1085                         conntrack->proto.tcp.seen[0].td_maxwin = 1;
1086                 conntrack->proto.tcp.seen[0].td_maxend =
1087                         conntrack->proto.tcp.seen[0].td_end;
1088
1089                 tcp_options(skb, iph, th, &conntrack->proto.tcp.seen[0]);
1090                 conntrack->proto.tcp.seen[1].flags = 0;
1091                 conntrack->proto.tcp.seen[0].loose = 
1092                 conntrack->proto.tcp.seen[1].loose = 0;
1093         } else if (ip_ct_tcp_loose == 0) {
1094                 /* Don't try to pick up connections. */
1095                 return 0;
1096         } else {
1097                 /*
1098                  * We are in the middle of a connection,
1099                  * its history is lost for us.
1100                  * Let's try to use the data from the packet.
1101                  */
1102                 conntrack->proto.tcp.seen[0].td_end =
1103                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1104                                              iph, th);
1105                 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1106                 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1107                         conntrack->proto.tcp.seen[0].td_maxwin = 1;
1108                 conntrack->proto.tcp.seen[0].td_maxend =
1109                         conntrack->proto.tcp.seen[0].td_end + 
1110                         conntrack->proto.tcp.seen[0].td_maxwin;
1111                 conntrack->proto.tcp.seen[0].td_scale = 0;
1112
1113                 /* We assume SACK. Should we assume window scaling too? */
1114                 conntrack->proto.tcp.seen[0].flags =
1115                 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
1116                 conntrack->proto.tcp.seen[0].loose = 
1117                 conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
1118         }
1119     
1120         conntrack->proto.tcp.seen[1].td_end = 0;
1121         conntrack->proto.tcp.seen[1].td_maxend = 0;
1122         conntrack->proto.tcp.seen[1].td_maxwin = 1;
1123         conntrack->proto.tcp.seen[1].td_scale = 0;      
1124
1125         /* tcp_packet will set them */
1126         conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1127         conntrack->proto.tcp.last_index = TCP_NONE_SET;
1128          
1129         DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1130                "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1131                 sender->td_end, sender->td_maxend, sender->td_maxwin,
1132                 sender->td_scale, 
1133                 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1134                 receiver->td_scale);
1135         return 1;
1136 }
1137   
1138 struct ip_conntrack_protocol ip_conntrack_protocol_tcp =
1139 {
1140         .proto                  = IPPROTO_TCP,
1141         .name                   = "tcp",
1142         .pkt_to_tuple           = tcp_pkt_to_tuple,
1143         .invert_tuple           = tcp_invert_tuple,
1144         .print_tuple            = tcp_print_tuple,
1145         .print_conntrack        = tcp_print_conntrack,
1146         .packet                 = tcp_packet,
1147         .new                    = tcp_new,
1148         .error                  = tcp_error,
1149 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
1150     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
1151         .to_nfattr              = tcp_to_nfattr,
1152         .from_nfattr            = nfattr_to_tcp,
1153         .tuple_to_nfattr        = ip_ct_port_tuple_to_nfattr,
1154         .nfattr_to_tuple        = ip_ct_port_nfattr_to_tuple,
1155 #endif
1156 };