Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_tcp.c
blobe800b16fc9209dd5a26e65d2be4eb83d77a7d7af
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
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
14 * Willy Tarreau:
15 * - State table bugfixes
16 * - More robust state changes
17 * - Tuning timer parameters
19 * version 2.2
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>
33 #include <net/tcp.h>
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 #include <linux/netfilter_ipv4/lockhelp.h>
41 #if 0
42 #define DEBUGP printk
43 #define DEBUGP_VARS
44 #else
45 #define DEBUGP(format, args...)
46 #endif
48 /* Protects conntrack->proto.tcp */
49 static DECLARE_RWLOCK(tcp_lock);
51 /* "Be conservative in what you do,
52 be liberal in what you accept from others."
53 If it's non-zero, we mark only out of window RST segments as INVALID. */
54 int ip_ct_tcp_be_liberal = 0;
56 /* When connection is picked up from the middle, how many packets are required
57 to pass in each direction when we assume we are in sync - if any side uses
58 window scaling, we lost the game.
59 If it is set to zero, we disable picking up already established
60 connections. */
61 int ip_ct_tcp_loose = 3;
63 /* Max number of the retransmitted packets without receiving an (acceptable)
64 ACK from the destination. If this number is reached, a shorter timer
65 will be started. */
66 int ip_ct_tcp_max_retrans = 3;
68 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
69 closely. They're more complex. --RR */
71 static const char *tcp_conntrack_names[] = {
72 "NONE",
73 "SYN_SENT",
74 "SYN_RECV",
75 "ESTABLISHED",
76 "FIN_WAIT",
77 "CLOSE_WAIT",
78 "LAST_ACK",
79 "TIME_WAIT",
80 "CLOSE",
81 "LISTEN"
84 #define SECS * HZ
85 #define MINS * 60 SECS
86 #define HOURS * 60 MINS
87 #define DAYS * 24 HOURS
89 unsigned long ip_ct_tcp_timeout_syn_sent = 2 MINS;
90 unsigned long ip_ct_tcp_timeout_syn_recv = 60 SECS;
91 unsigned long ip_ct_tcp_timeout_established = 5 DAYS;
92 unsigned long ip_ct_tcp_timeout_fin_wait = 2 MINS;
93 unsigned long ip_ct_tcp_timeout_close_wait = 60 SECS;
94 unsigned long ip_ct_tcp_timeout_last_ack = 30 SECS;
95 unsigned long ip_ct_tcp_timeout_time_wait = 2 MINS;
96 unsigned long ip_ct_tcp_timeout_close = 10 SECS;
98 /* RFC1122 says the R2 limit should be at least 100 seconds.
99 Linux uses 15 packets as limit, which corresponds
100 to ~13-30min depending on RTO. */
101 unsigned long ip_ct_tcp_timeout_max_retrans = 5 MINS;
103 static unsigned long * tcp_timeouts[]
104 = { NULL, /* TCP_CONNTRACK_NONE */
105 &ip_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
106 &ip_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
107 &ip_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
108 &ip_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
109 &ip_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
110 &ip_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
111 &ip_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
112 &ip_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
113 NULL, /* TCP_CONNTRACK_LISTEN */
116 #define sNO TCP_CONNTRACK_NONE
117 #define sSS TCP_CONNTRACK_SYN_SENT
118 #define sSR TCP_CONNTRACK_SYN_RECV
119 #define sES TCP_CONNTRACK_ESTABLISHED
120 #define sFW TCP_CONNTRACK_FIN_WAIT
121 #define sCW TCP_CONNTRACK_CLOSE_WAIT
122 #define sLA TCP_CONNTRACK_LAST_ACK
123 #define sTW TCP_CONNTRACK_TIME_WAIT
124 #define sCL TCP_CONNTRACK_CLOSE
125 #define sLI TCP_CONNTRACK_LISTEN
126 #define sIV TCP_CONNTRACK_MAX
127 #define sIG TCP_CONNTRACK_IGNORE
129 /* What TCP flags are set from RST/SYN/FIN/ACK. */
130 enum tcp_bit_set {
131 TCP_SYN_SET,
132 TCP_SYNACK_SET,
133 TCP_FIN_SET,
134 TCP_ACK_SET,
135 TCP_RST_SET,
136 TCP_NONE_SET,
140 * The TCP state transition table needs a few words...
142 * We are the man in the middle. All the packets go through us
143 * but might get lost in transit to the destination.
144 * It is assumed that the destinations can't receive segments
145 * we haven't seen.
147 * The checked segment is in window, but our windows are *not*
148 * equivalent with the ones of the sender/receiver. We always
149 * try to guess the state of the current sender.
151 * The meaning of the states are:
153 * NONE: initial state
154 * SYN_SENT: SYN-only packet seen
155 * SYN_RECV: SYN-ACK packet seen
156 * ESTABLISHED: ACK packet seen
157 * FIN_WAIT: FIN packet seen
158 * CLOSE_WAIT: ACK seen (after FIN)
159 * LAST_ACK: FIN seen (after FIN)
160 * TIME_WAIT: last ACK seen
161 * CLOSE: closed connection
163 * LISTEN state is not used.
165 * Packets marked as IGNORED (sIG):
166 * if they may be either invalid or valid
167 * and the receiver may send back a connection
168 * closing RST or a SYN/ACK.
170 * Packets marked as INVALID (sIV):
171 * if they are invalid
172 * or we do not support the request (simultaneous open)
174 static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
176 /* ORIGINAL */
177 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
178 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
180 * sNO -> sSS Initialize a new connection
181 * sSS -> sSS Retransmitted SYN
182 * sSR -> sIG Late retransmitted SYN?
183 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
184 * are errors. Receiver will reply with RST
185 * and close the connection.
186 * Or we are not in sync and hold a dead connection.
187 * sFW -> sIG
188 * sCW -> sIG
189 * sLA -> sIG
190 * sTW -> sSS Reopened connection (RFC 1122).
191 * sCL -> sSS
193 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
194 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
196 * A SYN/ACK from the client is always invalid:
197 * - either it tries to set up a simultaneous open, which is
198 * not supported;
199 * - or the firewall has just been inserted between the two hosts
200 * during the session set-up. The SYN will be retransmitted
201 * by the true client (or it'll time out).
203 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
204 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
206 * sNO -> sIV Too late and no reason to do anything...
207 * sSS -> sIV Client migth not send FIN in this state:
208 * we enforce waiting for a SYN/ACK reply first.
209 * sSR -> sFW Close started.
210 * sES -> sFW
211 * sFW -> sLA FIN seen in both directions, waiting for
212 * the last ACK.
213 * Migth be a retransmitted FIN as well...
214 * sCW -> sLA
215 * sLA -> sLA Retransmitted FIN. Remain in the same state.
216 * sTW -> sTW
217 * sCL -> sCL
219 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
220 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
222 * sNO -> sES Assumed.
223 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
224 * sSR -> sES Established state is reached.
225 * sES -> sES :-)
226 * sFW -> sCW Normal close request answered by ACK.
227 * sCW -> sCW
228 * sLA -> sTW Last ACK detected.
229 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
230 * sCL -> sCL
232 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
233 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
234 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
237 /* REPLY */
238 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
239 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
241 * sNO -> sIV Never reached.
242 * sSS -> sIV Simultaneous open, not supported
243 * sSR -> sIV Simultaneous open, not supported.
244 * sES -> sIV Server may not initiate a connection.
245 * sFW -> sIV
246 * sCW -> sIV
247 * sLA -> sIV
248 * sTW -> sIV Reopened connection, but server may not do it.
249 * sCL -> sIV
251 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
252 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
254 * sSS -> sSR Standard open.
255 * sSR -> sSR Retransmitted SYN/ACK.
256 * sES -> sIG Late retransmitted SYN/ACK?
257 * sFW -> sIG Might be SYN/ACK answering ignored SYN
258 * sCW -> sIG
259 * sLA -> sIG
260 * sTW -> sIG
261 * sCL -> sIG
263 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
264 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
266 * sSS -> sIV Server might not send FIN in this state.
267 * sSR -> sFW Close started.
268 * sES -> sFW
269 * sFW -> sLA FIN seen in both directions.
270 * sCW -> sLA
271 * sLA -> sLA Retransmitted FIN.
272 * sTW -> sTW
273 * sCL -> sCL
275 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
276 /*ack*/ { sIV, sIV, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
278 * sSS -> sIV Might be a half-open connection.
279 * sSR -> sSR Might answer late resent SYN.
280 * sES -> sES :-)
281 * sFW -> sCW Normal close request answered by ACK.
282 * sCW -> sCW
283 * sLA -> sTW Last ACK detected.
284 * sTW -> sTW Retransmitted last ACK.
285 * sCL -> sCL
287 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
288 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
289 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
293 static int tcp_pkt_to_tuple(const struct sk_buff *skb,
294 unsigned int dataoff,
295 struct ip_conntrack_tuple *tuple)
297 struct tcphdr _hdr, *hp;
299 /* Actually only need first 8 bytes. */
300 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
301 if (hp == NULL)
302 return 0;
304 tuple->src.u.tcp.port = hp->source;
305 tuple->dst.u.tcp.port = hp->dest;
307 return 1;
310 static int tcp_invert_tuple(struct ip_conntrack_tuple *tuple,
311 const struct ip_conntrack_tuple *orig)
313 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
314 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
315 return 1;
318 /* Print out the per-protocol part of the tuple. */
319 static int tcp_print_tuple(struct seq_file *s,
320 const struct ip_conntrack_tuple *tuple)
322 return seq_printf(s, "sport=%hu dport=%hu ",
323 ntohs(tuple->src.u.tcp.port),
324 ntohs(tuple->dst.u.tcp.port));
327 /* Print out the private part of the conntrack. */
328 static int tcp_print_conntrack(struct seq_file *s,
329 const struct ip_conntrack *conntrack)
331 enum tcp_conntrack state;
333 READ_LOCK(&tcp_lock);
334 state = conntrack->proto.tcp.state;
335 READ_UNLOCK(&tcp_lock);
337 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
340 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
342 if (tcph->rst) return TCP_RST_SET;
343 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
344 else if (tcph->fin) return TCP_FIN_SET;
345 else if (tcph->ack) return TCP_ACK_SET;
346 else return TCP_NONE_SET;
349 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
350 in IP Filter' by Guido van Rooij.
352 http://www.nluug.nl/events/sane2000/papers.html
353 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
355 The boundaries and the conditions are changed according to RFC793:
356 the packet must intersect the window (i.e. segments may be
357 after the right or before the left edge) and thus receivers may ACK
358 segments after the right edge of the window.
360 td_maxend = max(sack + max(win,1)) seen in reply packets
361 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
362 td_maxwin += seq + len - sender.td_maxend
363 if seq + len > sender.td_maxend
364 td_end = max(seq + len) seen in sent packets
366 I. Upper bound for valid data: seq <= sender.td_maxend
367 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
368 III. Upper bound for valid ack: sack <= receiver.td_end
369 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
371 where sack is the highest right edge of sack block found in the packet.
373 The upper bound limit for a valid ack is not ignored -
374 we doesn't have to deal with fragments.
377 static inline __u32 segment_seq_plus_len(__u32 seq,
378 size_t len,
379 struct iphdr *iph,
380 struct tcphdr *tcph)
382 return (seq + len - (iph->ihl + tcph->doff)*4
383 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
386 /* Fixme: what about big packets? */
387 #define MAXACKWINCONST 66000
388 #define MAXACKWINDOW(sender) \
389 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
390 : MAXACKWINCONST)
393 * Simplified tcp_parse_options routine from tcp_input.c
395 static void tcp_options(const struct sk_buff *skb,
396 struct iphdr *iph,
397 struct tcphdr *tcph,
398 struct ip_ct_tcp_state *state)
400 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
401 unsigned char *ptr;
402 int length = (tcph->doff*4) - sizeof(struct tcphdr);
404 if (!length)
405 return;
407 ptr = skb_header_pointer(skb,
408 (iph->ihl * 4) + sizeof(struct tcphdr),
409 length, buff);
410 BUG_ON(ptr == NULL);
412 state->td_scale =
413 state->flags = 0;
415 while (length > 0) {
416 int opcode=*ptr++;
417 int opsize;
419 switch (opcode) {
420 case TCPOPT_EOL:
421 return;
422 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
423 length--;
424 continue;
425 default:
426 opsize=*ptr++;
427 if (opsize < 2) /* "silly options" */
428 return;
429 if (opsize > length)
430 break; /* don't parse partial options */
432 if (opcode == TCPOPT_SACK_PERM
433 && opsize == TCPOLEN_SACK_PERM)
434 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
435 else if (opcode == TCPOPT_WINDOW
436 && opsize == TCPOLEN_WINDOW) {
437 state->td_scale = *(u_int8_t *)ptr;
439 if (state->td_scale > 14) {
440 /* See RFC1323 */
441 state->td_scale = 14;
443 state->flags |=
444 IP_CT_TCP_FLAG_WINDOW_SCALE;
446 ptr += opsize - 2;
447 length -= opsize;
452 static void tcp_sack(const struct sk_buff *skb,
453 struct iphdr *iph,
454 struct tcphdr *tcph,
455 __u32 *sack)
457 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
458 unsigned char *ptr;
459 int length = (tcph->doff*4) - sizeof(struct tcphdr);
460 __u32 tmp;
462 if (!length)
463 return;
465 ptr = skb_header_pointer(skb,
466 (iph->ihl * 4) + sizeof(struct tcphdr),
467 length, buff);
468 BUG_ON(ptr == NULL);
470 /* Fast path for timestamp-only option */
471 if (length == TCPOLEN_TSTAMP_ALIGNED*4
472 && *(__u32 *)ptr ==
473 __constant_ntohl((TCPOPT_NOP << 24)
474 | (TCPOPT_NOP << 16)
475 | (TCPOPT_TIMESTAMP << 8)
476 | TCPOLEN_TIMESTAMP))
477 return;
479 while (length > 0) {
480 int opcode=*ptr++;
481 int opsize, i;
483 switch (opcode) {
484 case TCPOPT_EOL:
485 return;
486 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
487 length--;
488 continue;
489 default:
490 opsize=*ptr++;
491 if (opsize < 2) /* "silly options" */
492 return;
493 if (opsize > length)
494 break; /* don't parse partial options */
496 if (opcode == TCPOPT_SACK
497 && opsize >= (TCPOLEN_SACK_BASE
498 + TCPOLEN_SACK_PERBLOCK)
499 && !((opsize - TCPOLEN_SACK_BASE)
500 % TCPOLEN_SACK_PERBLOCK)) {
501 for (i = 0;
502 i < (opsize - TCPOLEN_SACK_BASE);
503 i += TCPOLEN_SACK_PERBLOCK) {
504 tmp = ntohl(*((u_int32_t *)(ptr+i)+1));
506 if (after(tmp, *sack))
507 *sack = tmp;
509 return;
511 ptr += opsize - 2;
512 length -= opsize;
517 static int tcp_in_window(struct ip_ct_tcp *state,
518 enum ip_conntrack_dir dir,
519 unsigned int index,
520 const struct sk_buff *skb,
521 struct iphdr *iph,
522 struct tcphdr *tcph)
524 struct ip_ct_tcp_state *sender = &state->seen[dir];
525 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
526 __u32 seq, ack, sack, end, win, swin;
527 int res;
530 * Get the required data from the packet.
532 seq = ntohl(tcph->seq);
533 ack = sack = ntohl(tcph->ack_seq);
534 win = ntohs(tcph->window);
535 end = segment_seq_plus_len(seq, skb->len, iph, tcph);
537 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
538 tcp_sack(skb, iph, tcph, &sack);
540 DEBUGP("tcp_in_window: START\n");
541 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
542 "seq=%u ack=%u sack=%u win=%u end=%u\n",
543 NIPQUAD(iph->saddr), ntohs(tcph->source),
544 NIPQUAD(iph->daddr), ntohs(tcph->dest),
545 seq, ack, sack, win, end);
546 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
547 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
548 sender->td_end, sender->td_maxend, sender->td_maxwin,
549 sender->td_scale,
550 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
551 receiver->td_scale);
553 if (sender->td_end == 0) {
555 * Initialize sender data.
557 if (tcph->syn && tcph->ack) {
559 * Outgoing SYN-ACK in reply to a SYN.
561 sender->td_end =
562 sender->td_maxend = end;
563 sender->td_maxwin = (win == 0 ? 1 : win);
565 tcp_options(skb, iph, tcph, sender);
567 * RFC 1323:
568 * Both sides must send the Window Scale option
569 * to enable window scaling in either direction.
571 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
572 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
573 sender->td_scale =
574 receiver->td_scale = 0;
575 } else {
577 * We are in the middle of a connection,
578 * its history is lost for us.
579 * Let's try to use the data from the packet.
581 sender->td_end = end;
582 sender->td_maxwin = (win == 0 ? 1 : win);
583 sender->td_maxend = end + sender->td_maxwin;
585 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
586 && dir == IP_CT_DIR_ORIGINAL)
587 || (state->state == TCP_CONNTRACK_SYN_RECV
588 && dir == IP_CT_DIR_REPLY))
589 && after(end, sender->td_end)) {
591 * RFC 793: "if a TCP is reinitialized ... then it need
592 * not wait at all; it must only be sure to use sequence
593 * numbers larger than those recently used."
595 sender->td_end =
596 sender->td_maxend = end;
597 sender->td_maxwin = (win == 0 ? 1 : win);
599 tcp_options(skb, iph, tcph, sender);
602 if (!(tcph->ack)) {
604 * If there is no ACK, just pretend it was set and OK.
606 ack = sack = receiver->td_end;
607 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
608 (TCP_FLAG_ACK|TCP_FLAG_RST))
609 && (ack == 0)) {
611 * Broken TCP stacks, that set ACK in RST packets as well
612 * with zero ack value.
614 ack = sack = receiver->td_end;
617 if (seq == end
618 && (!tcph->rst
619 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
621 * Packets contains no data: we assume it is valid
622 * and check the ack value only.
623 * However RST segments are always validated by their
624 * SEQ number, except when seq == 0 (reset sent answering
625 * SYN.
627 seq = end = sender->td_end;
629 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
630 "seq=%u ack=%u sack =%u win=%u end=%u\n",
631 NIPQUAD(iph->saddr), ntohs(tcph->source),
632 NIPQUAD(iph->daddr), ntohs(tcph->dest),
633 seq, ack, sack, win, end);
634 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
635 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
636 sender->td_end, sender->td_maxend, sender->td_maxwin,
637 sender->td_scale,
638 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
639 receiver->td_scale);
641 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
642 before(seq, sender->td_maxend + 1),
643 after(end, sender->td_end - receiver->td_maxwin - 1),
644 before(sack, receiver->td_end + 1),
645 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
647 if (sender->loose || receiver->loose ||
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)))) {
653 * Take into account window scaling (RFC 1323).
655 if (!tcph->syn)
656 win <<= sender->td_scale;
659 * Update sender data.
661 swin = win + (sack - ack);
662 if (sender->td_maxwin < swin)
663 sender->td_maxwin = swin;
664 if (after(end, sender->td_end))
665 sender->td_end = end;
667 * Update receiver data.
669 if (after(end, sender->td_maxend))
670 receiver->td_maxwin += end - sender->td_maxend;
671 if (after(sack + win, receiver->td_maxend - 1)) {
672 receiver->td_maxend = sack + win;
673 if (win == 0)
674 receiver->td_maxend++;
678 * Check retransmissions.
680 if (index == TCP_ACK_SET) {
681 if (state->last_dir == dir
682 && state->last_seq == seq
683 && state->last_ack == ack
684 && state->last_end == end)
685 state->retrans++;
686 else {
687 state->last_dir = dir;
688 state->last_seq = seq;
689 state->last_ack = ack;
690 state->last_end = end;
691 state->retrans = 0;
695 * Close the window of disabled window tracking :-)
697 if (sender->loose)
698 sender->loose--;
700 res = 1;
701 } else {
702 if (LOG_INVALID(IPPROTO_TCP))
703 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
704 "ip_ct_tcp: %s ",
705 before(seq, sender->td_maxend + 1) ?
706 after(end, sender->td_end - receiver->td_maxwin - 1) ?
707 before(sack, receiver->td_end + 1) ?
708 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
709 : "ACK is under the lower bound (possible overly delayed ACK)"
710 : "ACK is over the upper bound (ACKed data not seen yet)"
711 : "SEQ is under the lower bound (already ACKed data retransmitted)"
712 : "SEQ is over the upper bound (over the window of the receiver)");
714 res = ip_ct_tcp_be_liberal;
717 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
718 "receiver end=%u maxend=%u maxwin=%u\n",
719 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
720 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
722 return res;
725 #ifdef CONFIG_IP_NF_NAT_NEEDED
726 /* Update sender->td_end after NAT successfully mangled the packet */
727 void ip_conntrack_tcp_update(struct sk_buff *skb,
728 struct ip_conntrack *conntrack,
729 enum ip_conntrack_dir dir)
731 struct iphdr *iph = skb->nh.iph;
732 struct tcphdr *tcph = (void *)skb->nh.iph + skb->nh.iph->ihl*4;
733 __u32 end;
734 #ifdef DEBUGP_VARS
735 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
736 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
737 #endif
739 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, iph, tcph);
741 WRITE_LOCK(&tcp_lock);
743 * We have to worry for the ack in the reply packet only...
745 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
746 conntrack->proto.tcp.seen[dir].td_end = end;
747 conntrack->proto.tcp.last_end = end;
748 WRITE_UNLOCK(&tcp_lock);
749 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
750 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
751 sender->td_end, sender->td_maxend, sender->td_maxwin,
752 sender->td_scale,
753 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
754 receiver->td_scale);
757 #endif
759 #define TH_FIN 0x01
760 #define TH_SYN 0x02
761 #define TH_RST 0x04
762 #define TH_PUSH 0x08
763 #define TH_ACK 0x10
764 #define TH_URG 0x20
765 #define TH_ECE 0x40
766 #define TH_CWR 0x80
768 /* table of valid flag combinations - ECE and CWR are always valid */
769 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
771 [TH_SYN] = 1,
772 [TH_SYN|TH_ACK] = 1,
773 [TH_RST] = 1,
774 [TH_RST|TH_ACK] = 1,
775 [TH_RST|TH_ACK|TH_PUSH] = 1,
776 [TH_FIN|TH_ACK] = 1,
777 [TH_ACK] = 1,
778 [TH_ACK|TH_PUSH] = 1,
779 [TH_ACK|TH_URG] = 1,
780 [TH_ACK|TH_URG|TH_PUSH] = 1,
781 [TH_FIN|TH_ACK|TH_PUSH] = 1,
782 [TH_FIN|TH_ACK|TH_URG] = 1,
783 [TH_FIN|TH_ACK|TH_URG|TH_PUSH] = 1,
786 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
787 static int tcp_error(struct sk_buff *skb,
788 enum ip_conntrack_info *ctinfo,
789 unsigned int hooknum)
791 struct iphdr *iph = skb->nh.iph;
792 struct tcphdr _tcph, *th;
793 unsigned int tcplen = skb->len - iph->ihl * 4;
794 u_int8_t tcpflags;
796 /* Smaller that minimal TCP header? */
797 th = skb_header_pointer(skb, iph->ihl * 4,
798 sizeof(_tcph), &_tcph);
799 if (th == NULL) {
800 if (LOG_INVALID(IPPROTO_TCP))
801 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
802 "ip_ct_tcp: short packet ");
803 return -NF_ACCEPT;
806 /* Not whole TCP header or malformed packet */
807 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
808 if (LOG_INVALID(IPPROTO_TCP))
809 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
810 "ip_ct_tcp: truncated/malformed packet ");
811 return -NF_ACCEPT;
814 /* Checksum invalid? Ignore.
815 * We skip checking packets on the outgoing path
816 * because the semantic of CHECKSUM_HW is different there
817 * and moreover root might send raw packets.
819 /* FIXME: Source route IP option packets --RR */
820 if (hooknum == NF_IP_PRE_ROUTING
821 && csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen, IPPROTO_TCP,
822 skb->ip_summed == CHECKSUM_HW ? skb->csum
823 : skb_checksum(skb, iph->ihl*4, tcplen, 0))) {
824 if (LOG_INVALID(IPPROTO_TCP))
825 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
826 "ip_ct_tcp: bad TCP checksum ");
827 return -NF_ACCEPT;
830 /* Check TCP flags. */
831 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
832 if (!tcp_valid_flags[tcpflags]) {
833 if (LOG_INVALID(IPPROTO_TCP))
834 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
835 "ip_ct_tcp: invalid TCP flag combination ");
836 return -NF_ACCEPT;
839 return NF_ACCEPT;
842 /* Returns verdict for packet, or -1 for invalid. */
843 static int tcp_packet(struct ip_conntrack *conntrack,
844 const struct sk_buff *skb,
845 enum ip_conntrack_info ctinfo)
847 enum tcp_conntrack new_state, old_state;
848 enum ip_conntrack_dir dir;
849 struct iphdr *iph = skb->nh.iph;
850 struct tcphdr *th, _tcph;
851 unsigned long timeout;
852 unsigned int index;
854 th = skb_header_pointer(skb, iph->ihl * 4,
855 sizeof(_tcph), &_tcph);
856 BUG_ON(th == NULL);
858 WRITE_LOCK(&tcp_lock);
859 old_state = conntrack->proto.tcp.state;
860 dir = CTINFO2DIR(ctinfo);
861 index = get_conntrack_index(th);
862 new_state = tcp_conntracks[dir][index][old_state];
864 switch (new_state) {
865 case TCP_CONNTRACK_IGNORE:
866 /* Either SYN in ORIGINAL
867 * or SYN/ACK in REPLY. */
868 if (index == TCP_SYNACK_SET
869 && conntrack->proto.tcp.last_index == TCP_SYN_SET
870 && conntrack->proto.tcp.last_dir != dir
871 && ntohl(th->ack_seq) ==
872 conntrack->proto.tcp.last_end) {
873 /* This SYN/ACK acknowledges a SYN that we earlier
874 * ignored as invalid. This means that the client and
875 * the server are both in sync, while the firewall is
876 * not. We kill this session and block the SYN/ACK so
877 * that the client cannot but retransmit its SYN and
878 * thus initiate a clean new session.
880 WRITE_UNLOCK(&tcp_lock);
881 if (LOG_INVALID(IPPROTO_TCP))
882 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
883 "ip_ct_tcp: killing out of sync session ");
884 if (del_timer(&conntrack->timeout))
885 conntrack->timeout.function((unsigned long)
886 conntrack);
887 return -NF_DROP;
889 conntrack->proto.tcp.last_index = index;
890 conntrack->proto.tcp.last_dir = dir;
891 conntrack->proto.tcp.last_seq = ntohl(th->seq);
892 conntrack->proto.tcp.last_end =
893 segment_seq_plus_len(ntohl(th->seq), skb->len, iph, th);
895 WRITE_UNLOCK(&tcp_lock);
896 if (LOG_INVALID(IPPROTO_TCP))
897 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
898 "ip_ct_tcp: invalid packet ignored ");
899 return NF_ACCEPT;
900 case TCP_CONNTRACK_MAX:
901 /* Invalid packet */
902 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
903 dir, get_conntrack_index(th),
904 old_state);
905 WRITE_UNLOCK(&tcp_lock);
906 if (LOG_INVALID(IPPROTO_TCP))
907 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
908 "ip_ct_tcp: invalid state ");
909 return -NF_ACCEPT;
910 case TCP_CONNTRACK_SYN_SENT:
911 if (old_state < TCP_CONNTRACK_TIME_WAIT)
912 break;
913 if ((conntrack->proto.tcp.seen[dir].flags &
914 IP_CT_TCP_FLAG_CLOSE_INIT)
915 || after(ntohl(th->seq),
916 conntrack->proto.tcp.seen[dir].td_end)) {
917 /* Attempt to reopen a closed connection.
918 * Delete this connection and look up again. */
919 WRITE_UNLOCK(&tcp_lock);
920 if (del_timer(&conntrack->timeout))
921 conntrack->timeout.function((unsigned long)
922 conntrack);
923 return -NF_REPEAT;
924 } else {
925 WRITE_UNLOCK(&tcp_lock);
926 if (LOG_INVALID(IPPROTO_TCP))
927 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
928 "ip_ct_tcp: invalid SYN");
929 return -NF_ACCEPT;
931 case TCP_CONNTRACK_CLOSE:
932 if (index == TCP_RST_SET
933 && test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
934 && conntrack->proto.tcp.last_index == TCP_SYN_SET
935 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
936 /* RST sent to invalid SYN we had let trough
937 * SYN was in window then, tear down connection.
938 * We skip window checking, because packet might ACK
939 * segments we ignored in the SYN. */
940 goto in_window;
942 /* Just fall trough */
943 default:
944 /* Keep compilers happy. */
945 break;
948 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
949 skb, iph, th)) {
950 WRITE_UNLOCK(&tcp_lock);
951 return -NF_ACCEPT;
953 in_window:
954 /* From now on we have got in-window packets */
955 conntrack->proto.tcp.last_index = index;
957 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
958 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
959 NIPQUAD(iph->saddr), ntohs(th->source),
960 NIPQUAD(iph->daddr), ntohs(th->dest),
961 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
962 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
963 old_state, new_state);
965 conntrack->proto.tcp.state = new_state;
966 if (old_state != new_state
967 && (new_state == TCP_CONNTRACK_FIN_WAIT
968 || new_state == TCP_CONNTRACK_CLOSE))
969 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
970 timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
971 && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans
972 ? ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
973 WRITE_UNLOCK(&tcp_lock);
975 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
976 /* If only reply is a RST, we can consider ourselves not to
977 have an established connection: this is a fairly common
978 problem case, so we can delete the conntrack
979 immediately. --RR */
980 if (th->rst) {
981 if (del_timer(&conntrack->timeout))
982 conntrack->timeout.function((unsigned long)
983 conntrack);
984 return NF_ACCEPT;
986 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
987 && (old_state == TCP_CONNTRACK_SYN_RECV
988 || old_state == TCP_CONNTRACK_ESTABLISHED)
989 && new_state == TCP_CONNTRACK_ESTABLISHED) {
990 /* Set ASSURED if we see see valid ack in ESTABLISHED
991 after SYN_RECV or a valid answer for a picked up
992 connection. */
993 set_bit(IPS_ASSURED_BIT, &conntrack->status);
995 ip_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
997 return NF_ACCEPT;
1000 /* Called when a new connection for this protocol found. */
1001 static int tcp_new(struct ip_conntrack *conntrack,
1002 const struct sk_buff *skb)
1004 enum tcp_conntrack new_state;
1005 struct iphdr *iph = skb->nh.iph;
1006 struct tcphdr *th, _tcph;
1007 #ifdef DEBUGP_VARS
1008 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1009 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1010 #endif
1012 th = skb_header_pointer(skb, iph->ihl * 4,
1013 sizeof(_tcph), &_tcph);
1014 BUG_ON(th == NULL);
1016 /* Don't need lock here: this conntrack not in circulation yet */
1017 new_state
1018 = tcp_conntracks[0][get_conntrack_index(th)]
1019 [TCP_CONNTRACK_NONE];
1021 /* Invalid: delete conntrack */
1022 if (new_state >= TCP_CONNTRACK_MAX) {
1023 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1024 return 0;
1027 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1028 /* SYN packet */
1029 conntrack->proto.tcp.seen[0].td_end =
1030 segment_seq_plus_len(ntohl(th->seq), skb->len,
1031 iph, th);
1032 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1033 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1034 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1035 conntrack->proto.tcp.seen[0].td_maxend =
1036 conntrack->proto.tcp.seen[0].td_end;
1038 tcp_options(skb, iph, th, &conntrack->proto.tcp.seen[0]);
1039 conntrack->proto.tcp.seen[1].flags = 0;
1040 conntrack->proto.tcp.seen[0].loose =
1041 conntrack->proto.tcp.seen[1].loose = 0;
1042 } else if (ip_ct_tcp_loose == 0) {
1043 /* Don't try to pick up connections. */
1044 return 0;
1045 } else {
1047 * We are in the middle of a connection,
1048 * its history is lost for us.
1049 * Let's try to use the data from the packet.
1051 conntrack->proto.tcp.seen[0].td_end =
1052 segment_seq_plus_len(ntohl(th->seq), skb->len,
1053 iph, th);
1054 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1055 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1056 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1057 conntrack->proto.tcp.seen[0].td_maxend =
1058 conntrack->proto.tcp.seen[0].td_end +
1059 conntrack->proto.tcp.seen[0].td_maxwin;
1060 conntrack->proto.tcp.seen[0].td_scale = 0;
1062 /* We assume SACK. Should we assume window scaling too? */
1063 conntrack->proto.tcp.seen[0].flags =
1064 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
1065 conntrack->proto.tcp.seen[0].loose =
1066 conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
1069 conntrack->proto.tcp.seen[1].td_end = 0;
1070 conntrack->proto.tcp.seen[1].td_maxend = 0;
1071 conntrack->proto.tcp.seen[1].td_maxwin = 1;
1072 conntrack->proto.tcp.seen[1].td_scale = 0;
1074 /* tcp_packet will set them */
1075 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1076 conntrack->proto.tcp.last_index = TCP_NONE_SET;
1078 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1079 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1080 sender->td_end, sender->td_maxend, sender->td_maxwin,
1081 sender->td_scale,
1082 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1083 receiver->td_scale);
1084 return 1;
1087 struct ip_conntrack_protocol ip_conntrack_protocol_tcp =
1089 .proto = IPPROTO_TCP,
1090 .name = "tcp",
1091 .pkt_to_tuple = tcp_pkt_to_tuple,
1092 .invert_tuple = tcp_invert_tuple,
1093 .print_tuple = tcp_print_tuple,
1094 .print_conntrack = tcp_print_conntrack,
1095 .packet = tcp_packet,
1096 .new = tcp_new,
1097 .error = tcp_error,