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