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