[NETFILTER]: connection tracking event notifiers
[linux-2.6/sactl.git] / net / ipv4 / netfilter / ip_conntrack_proto_tcp.c
bloba569ad1ee4d91d6acf2acc68ba8252d93201a546
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>
40 #if 0
41 #define DEBUGP printk
42 #define DEBUGP_VARS
43 #else
44 #define DEBUGP(format, args...)
45 #endif
47 /* Protects conntrack->proto.tcp */
48 static DEFINE_RWLOCK(tcp_lock);
50 /* "Be conservative in what you do,
51 be liberal in what you accept from others."
52 If it's non-zero, we mark only out of window RST segments as INVALID. */
53 int ip_ct_tcp_be_liberal = 0;
55 /* When connection is picked up from the middle, how many packets are required
56 to pass in each direction when we assume we are in sync - if any side uses
57 window scaling, we lost the game.
58 If it is set to zero, we disable picking up already established
59 connections. */
60 int ip_ct_tcp_loose = 3;
62 /* Max number of the retransmitted packets without receiving an (acceptable)
63 ACK from the destination. If this number is reached, a shorter timer
64 will be started. */
65 int ip_ct_tcp_max_retrans = 3;
67 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
68 closely. They're more complex. --RR */
70 static const char *tcp_conntrack_names[] = {
71 "NONE",
72 "SYN_SENT",
73 "SYN_RECV",
74 "ESTABLISHED",
75 "FIN_WAIT",
76 "CLOSE_WAIT",
77 "LAST_ACK",
78 "TIME_WAIT",
79 "CLOSE",
80 "LISTEN"
83 #define SECS * HZ
84 #define MINS * 60 SECS
85 #define HOURS * 60 MINS
86 #define DAYS * 24 HOURS
88 unsigned long ip_ct_tcp_timeout_syn_sent = 2 MINS;
89 unsigned long ip_ct_tcp_timeout_syn_recv = 60 SECS;
90 unsigned long ip_ct_tcp_timeout_established = 5 DAYS;
91 unsigned long ip_ct_tcp_timeout_fin_wait = 2 MINS;
92 unsigned long ip_ct_tcp_timeout_close_wait = 60 SECS;
93 unsigned long ip_ct_tcp_timeout_last_ack = 30 SECS;
94 unsigned long ip_ct_tcp_timeout_time_wait = 2 MINS;
95 unsigned long ip_ct_tcp_timeout_close = 10 SECS;
97 /* RFC1122 says the R2 limit should be at least 100 seconds.
98 Linux uses 15 packets as limit, which corresponds
99 to ~13-30min depending on RTO. */
100 unsigned long ip_ct_tcp_timeout_max_retrans = 5 MINS;
102 static unsigned long * tcp_timeouts[]
103 = { NULL, /* TCP_CONNTRACK_NONE */
104 &ip_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
105 &ip_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
106 &ip_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
107 &ip_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
108 &ip_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
109 &ip_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
110 &ip_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
111 &ip_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
112 NULL, /* TCP_CONNTRACK_LISTEN */
115 #define sNO TCP_CONNTRACK_NONE
116 #define sSS TCP_CONNTRACK_SYN_SENT
117 #define sSR TCP_CONNTRACK_SYN_RECV
118 #define sES TCP_CONNTRACK_ESTABLISHED
119 #define sFW TCP_CONNTRACK_FIN_WAIT
120 #define sCW TCP_CONNTRACK_CLOSE_WAIT
121 #define sLA TCP_CONNTRACK_LAST_ACK
122 #define sTW TCP_CONNTRACK_TIME_WAIT
123 #define sCL TCP_CONNTRACK_CLOSE
124 #define sLI TCP_CONNTRACK_LISTEN
125 #define sIV TCP_CONNTRACK_MAX
126 #define sIG TCP_CONNTRACK_IGNORE
128 /* What TCP flags are set from RST/SYN/FIN/ACK. */
129 enum tcp_bit_set {
130 TCP_SYN_SET,
131 TCP_SYNACK_SET,
132 TCP_FIN_SET,
133 TCP_ACK_SET,
134 TCP_RST_SET,
135 TCP_NONE_SET,
139 * The TCP state transition table needs a few words...
141 * We are the man in the middle. All the packets go through us
142 * but might get lost in transit to the destination.
143 * It is assumed that the destinations can't receive segments
144 * we haven't seen.
146 * The checked segment is in window, but our windows are *not*
147 * equivalent with the ones of the sender/receiver. We always
148 * try to guess the state of the current sender.
150 * The meaning of the states are:
152 * NONE: initial state
153 * SYN_SENT: SYN-only packet seen
154 * SYN_RECV: SYN-ACK packet seen
155 * ESTABLISHED: ACK packet seen
156 * FIN_WAIT: FIN packet seen
157 * CLOSE_WAIT: ACK seen (after FIN)
158 * LAST_ACK: FIN seen (after FIN)
159 * TIME_WAIT: last ACK seen
160 * CLOSE: closed connection
162 * LISTEN state is not used.
164 * Packets marked as IGNORED (sIG):
165 * if they may be either invalid or valid
166 * and the receiver may send back a connection
167 * closing RST or a SYN/ACK.
169 * Packets marked as INVALID (sIV):
170 * if they are invalid
171 * or we do not support the request (simultaneous open)
173 static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
175 /* ORIGINAL */
176 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
177 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
179 * sNO -> sSS Initialize a new connection
180 * sSS -> sSS Retransmitted SYN
181 * sSR -> sIG Late retransmitted SYN?
182 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
183 * are errors. Receiver will reply with RST
184 * and close the connection.
185 * Or we are not in sync and hold a dead connection.
186 * sFW -> sIG
187 * sCW -> sIG
188 * sLA -> sIG
189 * sTW -> sSS Reopened connection (RFC 1122).
190 * sCL -> sSS
192 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
193 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
195 * A SYN/ACK from the client is always invalid:
196 * - either it tries to set up a simultaneous open, which is
197 * not supported;
198 * - or the firewall has just been inserted between the two hosts
199 * during the session set-up. The SYN will be retransmitted
200 * by the true client (or it'll time out).
202 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
203 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
205 * sNO -> sIV Too late and no reason to do anything...
206 * sSS -> sIV Client migth not send FIN in this state:
207 * we enforce waiting for a SYN/ACK reply first.
208 * sSR -> sFW Close started.
209 * sES -> sFW
210 * sFW -> sLA FIN seen in both directions, waiting for
211 * the last ACK.
212 * Migth be a retransmitted FIN as well...
213 * sCW -> sLA
214 * sLA -> sLA Retransmitted FIN. Remain in the same state.
215 * sTW -> sTW
216 * sCL -> sCL
218 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
219 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
221 * sNO -> sES Assumed.
222 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
223 * sSR -> sES Established state is reached.
224 * sES -> sES :-)
225 * sFW -> sCW Normal close request answered by ACK.
226 * sCW -> sCW
227 * sLA -> sTW Last ACK detected.
228 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
229 * sCL -> sCL
231 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
232 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
233 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
236 /* REPLY */
237 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
238 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
240 * sNO -> sIV Never reached.
241 * sSS -> sIV Simultaneous open, not supported
242 * sSR -> sIV Simultaneous open, not supported.
243 * sES -> sIV Server may not initiate a connection.
244 * sFW -> sIV
245 * sCW -> sIV
246 * sLA -> sIV
247 * sTW -> sIV Reopened connection, but server may not do it.
248 * sCL -> sIV
250 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
251 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
253 * sSS -> sSR Standard open.
254 * sSR -> sSR Retransmitted SYN/ACK.
255 * sES -> sIG Late retransmitted SYN/ACK?
256 * sFW -> sIG Might be SYN/ACK answering ignored SYN
257 * sCW -> sIG
258 * sLA -> sIG
259 * sTW -> sIG
260 * sCL -> sIG
262 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
263 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
265 * sSS -> sIV Server might not send FIN in this state.
266 * sSR -> sFW Close started.
267 * sES -> sFW
268 * sFW -> sLA FIN seen in both directions.
269 * sCW -> sLA
270 * sLA -> sLA Retransmitted FIN.
271 * sTW -> sTW
272 * sCL -> sCL
274 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
275 /*ack*/ { sIV, sIV, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
277 * sSS -> sIV Might be a half-open connection.
278 * sSR -> sSR Might answer late resent SYN.
279 * sES -> sES :-)
280 * sFW -> sCW Normal close request answered by ACK.
281 * sCW -> sCW
282 * sLA -> sTW Last ACK detected.
283 * sTW -> sTW Retransmitted last ACK.
284 * sCL -> sCL
286 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
287 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
288 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
292 static int tcp_pkt_to_tuple(const struct sk_buff *skb,
293 unsigned int dataoff,
294 struct ip_conntrack_tuple *tuple)
296 struct tcphdr _hdr, *hp;
298 /* Actually only need first 8 bytes. */
299 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
300 if (hp == NULL)
301 return 0;
303 tuple->src.u.tcp.port = hp->source;
304 tuple->dst.u.tcp.port = hp->dest;
306 return 1;
309 static int tcp_invert_tuple(struct ip_conntrack_tuple *tuple,
310 const struct ip_conntrack_tuple *orig)
312 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
313 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
314 return 1;
317 /* Print out the per-protocol part of the tuple. */
318 static int tcp_print_tuple(struct seq_file *s,
319 const struct ip_conntrack_tuple *tuple)
321 return seq_printf(s, "sport=%hu dport=%hu ",
322 ntohs(tuple->src.u.tcp.port),
323 ntohs(tuple->dst.u.tcp.port));
326 /* Print out the private part of the conntrack. */
327 static int tcp_print_conntrack(struct seq_file *s,
328 const struct ip_conntrack *conntrack)
330 enum tcp_conntrack state;
332 read_lock_bh(&tcp_lock);
333 state = conntrack->proto.tcp.state;
334 read_unlock_bh(&tcp_lock);
336 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
339 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
341 if (tcph->rst) return TCP_RST_SET;
342 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
343 else if (tcph->fin) return TCP_FIN_SET;
344 else if (tcph->ack) return TCP_ACK_SET;
345 else return TCP_NONE_SET;
348 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
349 in IP Filter' by Guido van Rooij.
351 http://www.nluug.nl/events/sane2000/papers.html
352 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
354 The boundaries and the conditions are changed according to RFC793:
355 the packet must intersect the window (i.e. segments may be
356 after the right or before the left edge) and thus receivers may ACK
357 segments after the right edge of the window.
359 td_maxend = max(sack + max(win,1)) seen in reply packets
360 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
361 td_maxwin += seq + len - sender.td_maxend
362 if seq + len > sender.td_maxend
363 td_end = max(seq + len) seen in sent packets
365 I. Upper bound for valid data: seq <= sender.td_maxend
366 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
367 III. Upper bound for valid ack: sack <= receiver.td_end
368 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
370 where sack is the highest right edge of sack block found in the packet.
372 The upper bound limit for a valid ack is not ignored -
373 we doesn't have to deal with fragments.
376 static inline __u32 segment_seq_plus_len(__u32 seq,
377 size_t len,
378 struct iphdr *iph,
379 struct tcphdr *tcph)
381 return (seq + len - (iph->ihl + tcph->doff)*4
382 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
385 /* Fixme: what about big packets? */
386 #define MAXACKWINCONST 66000
387 #define MAXACKWINDOW(sender) \
388 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
389 : MAXACKWINCONST)
392 * Simplified tcp_parse_options routine from tcp_input.c
394 static void tcp_options(const struct sk_buff *skb,
395 struct iphdr *iph,
396 struct tcphdr *tcph,
397 struct ip_ct_tcp_state *state)
399 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
400 unsigned char *ptr;
401 int length = (tcph->doff*4) - sizeof(struct tcphdr);
403 if (!length)
404 return;
406 ptr = skb_header_pointer(skb,
407 (iph->ihl * 4) + sizeof(struct tcphdr),
408 length, buff);
409 BUG_ON(ptr == NULL);
411 state->td_scale =
412 state->flags = 0;
414 while (length > 0) {
415 int opcode=*ptr++;
416 int opsize;
418 switch (opcode) {
419 case TCPOPT_EOL:
420 return;
421 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
422 length--;
423 continue;
424 default:
425 opsize=*ptr++;
426 if (opsize < 2) /* "silly options" */
427 return;
428 if (opsize > length)
429 break; /* don't parse partial options */
431 if (opcode == TCPOPT_SACK_PERM
432 && opsize == TCPOLEN_SACK_PERM)
433 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
434 else if (opcode == TCPOPT_WINDOW
435 && opsize == TCPOLEN_WINDOW) {
436 state->td_scale = *(u_int8_t *)ptr;
438 if (state->td_scale > 14) {
439 /* See RFC1323 */
440 state->td_scale = 14;
442 state->flags |=
443 IP_CT_TCP_FLAG_WINDOW_SCALE;
445 ptr += opsize - 2;
446 length -= opsize;
451 static void tcp_sack(const struct sk_buff *skb,
452 struct iphdr *iph,
453 struct tcphdr *tcph,
454 __u32 *sack)
456 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
457 unsigned char *ptr;
458 int length = (tcph->doff*4) - sizeof(struct tcphdr);
459 __u32 tmp;
461 if (!length)
462 return;
464 ptr = skb_header_pointer(skb,
465 (iph->ihl * 4) + sizeof(struct tcphdr),
466 length, buff);
467 BUG_ON(ptr == NULL);
469 /* Fast path for timestamp-only option */
470 if (length == TCPOLEN_TSTAMP_ALIGNED*4
471 && *(__u32 *)ptr ==
472 __constant_ntohl((TCPOPT_NOP << 24)
473 | (TCPOPT_NOP << 16)
474 | (TCPOPT_TIMESTAMP << 8)
475 | TCPOLEN_TIMESTAMP))
476 return;
478 while (length > 0) {
479 int opcode=*ptr++;
480 int opsize, i;
482 switch (opcode) {
483 case TCPOPT_EOL:
484 return;
485 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
486 length--;
487 continue;
488 default:
489 opsize=*ptr++;
490 if (opsize < 2) /* "silly options" */
491 return;
492 if (opsize > length)
493 break; /* don't parse partial options */
495 if (opcode == TCPOPT_SACK
496 && opsize >= (TCPOLEN_SACK_BASE
497 + TCPOLEN_SACK_PERBLOCK)
498 && !((opsize - TCPOLEN_SACK_BASE)
499 % TCPOLEN_SACK_PERBLOCK)) {
500 for (i = 0;
501 i < (opsize - TCPOLEN_SACK_BASE);
502 i += TCPOLEN_SACK_PERBLOCK) {
503 tmp = ntohl(*((u_int32_t *)(ptr+i)+1));
505 if (after(tmp, *sack))
506 *sack = tmp;
508 return;
510 ptr += opsize - 2;
511 length -= opsize;
516 static int tcp_in_window(struct ip_ct_tcp *state,
517 enum ip_conntrack_dir dir,
518 unsigned int index,
519 const struct sk_buff *skb,
520 struct iphdr *iph,
521 struct tcphdr *tcph)
523 struct ip_ct_tcp_state *sender = &state->seen[dir];
524 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
525 __u32 seq, ack, sack, end, win, swin;
526 int res;
529 * Get the required data from the packet.
531 seq = ntohl(tcph->seq);
532 ack = sack = ntohl(tcph->ack_seq);
533 win = ntohs(tcph->window);
534 end = segment_seq_plus_len(seq, skb->len, iph, tcph);
536 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
537 tcp_sack(skb, iph, tcph, &sack);
539 DEBUGP("tcp_in_window: START\n");
540 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
541 "seq=%u ack=%u sack=%u win=%u end=%u\n",
542 NIPQUAD(iph->saddr), ntohs(tcph->source),
543 NIPQUAD(iph->daddr), ntohs(tcph->dest),
544 seq, ack, sack, win, end);
545 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
546 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
547 sender->td_end, sender->td_maxend, sender->td_maxwin,
548 sender->td_scale,
549 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
550 receiver->td_scale);
552 if (sender->td_end == 0) {
554 * Initialize sender data.
556 if (tcph->syn && tcph->ack) {
558 * Outgoing SYN-ACK in reply to a SYN.
560 sender->td_end =
561 sender->td_maxend = end;
562 sender->td_maxwin = (win == 0 ? 1 : win);
564 tcp_options(skb, iph, tcph, sender);
566 * RFC 1323:
567 * Both sides must send the Window Scale option
568 * to enable window scaling in either direction.
570 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
571 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
572 sender->td_scale =
573 receiver->td_scale = 0;
574 } else {
576 * We are in the middle of a connection,
577 * its history is lost for us.
578 * Let's try to use the data from the packet.
580 sender->td_end = end;
581 sender->td_maxwin = (win == 0 ? 1 : win);
582 sender->td_maxend = end + sender->td_maxwin;
584 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
585 && dir == IP_CT_DIR_ORIGINAL)
586 || (state->state == TCP_CONNTRACK_SYN_RECV
587 && dir == IP_CT_DIR_REPLY))
588 && after(end, sender->td_end)) {
590 * RFC 793: "if a TCP is reinitialized ... then it need
591 * not wait at all; it must only be sure to use sequence
592 * numbers larger than those recently used."
594 sender->td_end =
595 sender->td_maxend = end;
596 sender->td_maxwin = (win == 0 ? 1 : win);
598 tcp_options(skb, iph, tcph, sender);
601 if (!(tcph->ack)) {
603 * If there is no ACK, just pretend it was set and OK.
605 ack = sack = receiver->td_end;
606 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
607 (TCP_FLAG_ACK|TCP_FLAG_RST))
608 && (ack == 0)) {
610 * Broken TCP stacks, that set ACK in RST packets as well
611 * with zero ack value.
613 ack = sack = receiver->td_end;
616 if (seq == end
617 && (!tcph->rst
618 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
620 * Packets contains no data: we assume it is valid
621 * and check the ack value only.
622 * However RST segments are always validated by their
623 * SEQ number, except when seq == 0 (reset sent answering
624 * SYN.
626 seq = end = sender->td_end;
628 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
629 "seq=%u ack=%u sack =%u win=%u end=%u\n",
630 NIPQUAD(iph->saddr), ntohs(tcph->source),
631 NIPQUAD(iph->daddr), ntohs(tcph->dest),
632 seq, ack, sack, win, end);
633 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
634 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
635 sender->td_end, sender->td_maxend, sender->td_maxwin,
636 sender->td_scale,
637 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
638 receiver->td_scale);
640 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
641 before(seq, sender->td_maxend + 1),
642 after(end, sender->td_end - receiver->td_maxwin - 1),
643 before(sack, receiver->td_end + 1),
644 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
646 if (sender->loose || receiver->loose ||
647 (before(seq, sender->td_maxend + 1) &&
648 after(end, sender->td_end - receiver->td_maxwin - 1) &&
649 before(sack, receiver->td_end + 1) &&
650 after(ack, receiver->td_end - MAXACKWINDOW(sender)))) {
652 * Take into account window scaling (RFC 1323).
654 if (!tcph->syn)
655 win <<= sender->td_scale;
658 * Update sender data.
660 swin = win + (sack - ack);
661 if (sender->td_maxwin < swin)
662 sender->td_maxwin = swin;
663 if (after(end, sender->td_end))
664 sender->td_end = end;
666 * Update receiver data.
668 if (after(end, sender->td_maxend))
669 receiver->td_maxwin += end - sender->td_maxend;
670 if (after(sack + win, receiver->td_maxend - 1)) {
671 receiver->td_maxend = sack + win;
672 if (win == 0)
673 receiver->td_maxend++;
677 * Check retransmissions.
679 if (index == TCP_ACK_SET) {
680 if (state->last_dir == dir
681 && state->last_seq == seq
682 && state->last_ack == ack
683 && state->last_end == end)
684 state->retrans++;
685 else {
686 state->last_dir = dir;
687 state->last_seq = seq;
688 state->last_ack = ack;
689 state->last_end = end;
690 state->retrans = 0;
694 * Close the window of disabled window tracking :-)
696 if (sender->loose)
697 sender->loose--;
699 res = 1;
700 } else {
701 if (LOG_INVALID(IPPROTO_TCP))
702 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
703 "ip_ct_tcp: %s ",
704 before(seq, sender->td_maxend + 1) ?
705 after(end, sender->td_end - receiver->td_maxwin - 1) ?
706 before(sack, receiver->td_end + 1) ?
707 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
708 : "ACK is under the lower bound (possible overly delayed ACK)"
709 : "ACK is over the upper bound (ACKed data not seen yet)"
710 : "SEQ is under the lower bound (already ACKed data retransmitted)"
711 : "SEQ is over the upper bound (over the window of the receiver)");
713 res = ip_ct_tcp_be_liberal;
716 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
717 "receiver end=%u maxend=%u maxwin=%u\n",
718 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
719 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
721 return res;
724 #ifdef CONFIG_IP_NF_NAT_NEEDED
725 /* Update sender->td_end after NAT successfully mangled the packet */
726 void ip_conntrack_tcp_update(struct sk_buff *skb,
727 struct ip_conntrack *conntrack,
728 enum ip_conntrack_dir dir)
730 struct iphdr *iph = skb->nh.iph;
731 struct tcphdr *tcph = (void *)skb->nh.iph + skb->nh.iph->ihl*4;
732 __u32 end;
733 #ifdef DEBUGP_VARS
734 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
735 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
736 #endif
738 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, iph, tcph);
740 write_lock_bh(&tcp_lock);
742 * We have to worry for the ack in the reply packet only...
744 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
745 conntrack->proto.tcp.seen[dir].td_end = end;
746 conntrack->proto.tcp.last_end = end;
747 write_unlock_bh(&tcp_lock);
748 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
749 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
750 sender->td_end, sender->td_maxend, sender->td_maxwin,
751 sender->td_scale,
752 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
753 receiver->td_scale);
756 #endif
758 #define TH_FIN 0x01
759 #define TH_SYN 0x02
760 #define TH_RST 0x04
761 #define TH_PUSH 0x08
762 #define TH_ACK 0x10
763 #define TH_URG 0x20
764 #define TH_ECE 0x40
765 #define TH_CWR 0x80
767 /* table of valid flag combinations - ECE and CWR are always valid */
768 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
770 [TH_SYN] = 1,
771 [TH_SYN|TH_ACK] = 1,
772 [TH_SYN|TH_ACK|TH_PUSH] = 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 && skb->ip_summed != CHECKSUM_UNNECESSARY
822 && csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen, IPPROTO_TCP,
823 skb->ip_summed == CHECKSUM_HW ? skb->csum
824 : skb_checksum(skb, iph->ihl*4, tcplen, 0))) {
825 if (LOG_INVALID(IPPROTO_TCP))
826 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
827 "ip_ct_tcp: bad TCP checksum ");
828 return -NF_ACCEPT;
831 /* Check TCP flags. */
832 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
833 if (!tcp_valid_flags[tcpflags]) {
834 if (LOG_INVALID(IPPROTO_TCP))
835 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
836 "ip_ct_tcp: invalid TCP flag combination ");
837 return -NF_ACCEPT;
840 return NF_ACCEPT;
843 /* Returns verdict for packet, or -1 for invalid. */
844 static int tcp_packet(struct ip_conntrack *conntrack,
845 const struct sk_buff *skb,
846 enum ip_conntrack_info ctinfo)
848 enum tcp_conntrack new_state, old_state;
849 enum ip_conntrack_dir dir;
850 struct iphdr *iph = skb->nh.iph;
851 struct tcphdr *th, _tcph;
852 unsigned long timeout;
853 unsigned int index;
855 th = skb_header_pointer(skb, iph->ihl * 4,
856 sizeof(_tcph), &_tcph);
857 BUG_ON(th == NULL);
859 write_lock_bh(&tcp_lock);
860 old_state = conntrack->proto.tcp.state;
861 dir = CTINFO2DIR(ctinfo);
862 index = get_conntrack_index(th);
863 new_state = tcp_conntracks[dir][index][old_state];
865 switch (new_state) {
866 case TCP_CONNTRACK_IGNORE:
867 /* Either SYN in ORIGINAL
868 * or SYN/ACK in REPLY. */
869 if (index == TCP_SYNACK_SET
870 && conntrack->proto.tcp.last_index == TCP_SYN_SET
871 && conntrack->proto.tcp.last_dir != dir
872 && ntohl(th->ack_seq) ==
873 conntrack->proto.tcp.last_end) {
874 /* This SYN/ACK acknowledges a SYN that we earlier
875 * ignored as invalid. This means that the client and
876 * the server are both in sync, while the firewall is
877 * not. We kill this session and block the SYN/ACK so
878 * that the client cannot but retransmit its SYN and
879 * thus initiate a clean new session.
881 write_unlock_bh(&tcp_lock);
882 if (LOG_INVALID(IPPROTO_TCP))
883 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
884 "ip_ct_tcp: killing out of sync session ");
885 if (del_timer(&conntrack->timeout))
886 conntrack->timeout.function((unsigned long)
887 conntrack);
888 return -NF_DROP;
890 conntrack->proto.tcp.last_index = index;
891 conntrack->proto.tcp.last_dir = dir;
892 conntrack->proto.tcp.last_seq = ntohl(th->seq);
893 conntrack->proto.tcp.last_end =
894 segment_seq_plus_len(ntohl(th->seq), skb->len, iph, th);
896 write_unlock_bh(&tcp_lock);
897 if (LOG_INVALID(IPPROTO_TCP))
898 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
899 "ip_ct_tcp: invalid packet ignored ");
900 return NF_ACCEPT;
901 case TCP_CONNTRACK_MAX:
902 /* Invalid packet */
903 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
904 dir, get_conntrack_index(th),
905 old_state);
906 write_unlock_bh(&tcp_lock);
907 if (LOG_INVALID(IPPROTO_TCP))
908 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
909 "ip_ct_tcp: invalid state ");
910 return -NF_ACCEPT;
911 case TCP_CONNTRACK_SYN_SENT:
912 if (old_state < TCP_CONNTRACK_TIME_WAIT)
913 break;
914 if ((conntrack->proto.tcp.seen[dir].flags &
915 IP_CT_TCP_FLAG_CLOSE_INIT)
916 || after(ntohl(th->seq),
917 conntrack->proto.tcp.seen[dir].td_end)) {
918 /* Attempt to reopen a closed connection.
919 * Delete this connection and look up again. */
920 write_unlock_bh(&tcp_lock);
921 if (del_timer(&conntrack->timeout))
922 conntrack->timeout.function((unsigned long)
923 conntrack);
924 return -NF_REPEAT;
925 } else {
926 write_unlock_bh(&tcp_lock);
927 if (LOG_INVALID(IPPROTO_TCP))
928 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
929 "ip_ct_tcp: invalid SYN");
930 return -NF_ACCEPT;
932 case TCP_CONNTRACK_CLOSE:
933 if (index == TCP_RST_SET
934 && test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
935 && conntrack->proto.tcp.last_index == TCP_SYN_SET
936 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
937 /* RST sent to invalid SYN we had let trough
938 * SYN was in window then, tear down connection.
939 * We skip window checking, because packet might ACK
940 * segments we ignored in the SYN. */
941 goto in_window;
943 /* Just fall trough */
944 default:
945 /* Keep compilers happy. */
946 break;
949 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
950 skb, iph, th)) {
951 write_unlock_bh(&tcp_lock);
952 return -NF_ACCEPT;
954 in_window:
955 /* From now on we have got in-window packets */
956 conntrack->proto.tcp.last_index = index;
958 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
959 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
960 NIPQUAD(iph->saddr), ntohs(th->source),
961 NIPQUAD(iph->daddr), ntohs(th->dest),
962 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
963 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
964 old_state, new_state);
966 conntrack->proto.tcp.state = new_state;
967 if (old_state != new_state
968 && (new_state == TCP_CONNTRACK_FIN_WAIT
969 || new_state == TCP_CONNTRACK_CLOSE))
970 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
971 timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
972 && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans
973 ? ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
974 write_unlock_bh(&tcp_lock);
976 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
977 if (new_state != old_state)
978 ip_conntrack_event_cache(IPCT_PROTOINFO, skb);
980 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
981 /* If only reply is a RST, we can consider ourselves not to
982 have an established connection: this is a fairly common
983 problem case, so we can delete the conntrack
984 immediately. --RR */
985 if (th->rst) {
986 if (del_timer(&conntrack->timeout))
987 conntrack->timeout.function((unsigned long)
988 conntrack);
989 return NF_ACCEPT;
991 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
992 && (old_state == TCP_CONNTRACK_SYN_RECV
993 || old_state == TCP_CONNTRACK_ESTABLISHED)
994 && new_state == TCP_CONNTRACK_ESTABLISHED) {
995 /* Set ASSURED if we see see valid ack in ESTABLISHED
996 after SYN_RECV or a valid answer for a picked up
997 connection. */
998 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1000 ip_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1002 return NF_ACCEPT;
1005 /* Called when a new connection for this protocol found. */
1006 static int tcp_new(struct ip_conntrack *conntrack,
1007 const struct sk_buff *skb)
1009 enum tcp_conntrack new_state;
1010 struct iphdr *iph = skb->nh.iph;
1011 struct tcphdr *th, _tcph;
1012 #ifdef DEBUGP_VARS
1013 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1014 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1015 #endif
1017 th = skb_header_pointer(skb, iph->ihl * 4,
1018 sizeof(_tcph), &_tcph);
1019 BUG_ON(th == NULL);
1021 /* Don't need lock here: this conntrack not in circulation yet */
1022 new_state
1023 = tcp_conntracks[0][get_conntrack_index(th)]
1024 [TCP_CONNTRACK_NONE];
1026 /* Invalid: delete conntrack */
1027 if (new_state >= TCP_CONNTRACK_MAX) {
1028 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1029 return 0;
1032 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1033 /* SYN packet */
1034 conntrack->proto.tcp.seen[0].td_end =
1035 segment_seq_plus_len(ntohl(th->seq), skb->len,
1036 iph, th);
1037 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1038 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1039 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1040 conntrack->proto.tcp.seen[0].td_maxend =
1041 conntrack->proto.tcp.seen[0].td_end;
1043 tcp_options(skb, iph, th, &conntrack->proto.tcp.seen[0]);
1044 conntrack->proto.tcp.seen[1].flags = 0;
1045 conntrack->proto.tcp.seen[0].loose =
1046 conntrack->proto.tcp.seen[1].loose = 0;
1047 } else if (ip_ct_tcp_loose == 0) {
1048 /* Don't try to pick up connections. */
1049 return 0;
1050 } else {
1052 * We are in the middle of a connection,
1053 * its history is lost for us.
1054 * Let's try to use the data from the packet.
1056 conntrack->proto.tcp.seen[0].td_end =
1057 segment_seq_plus_len(ntohl(th->seq), skb->len,
1058 iph, th);
1059 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1060 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1061 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1062 conntrack->proto.tcp.seen[0].td_maxend =
1063 conntrack->proto.tcp.seen[0].td_end +
1064 conntrack->proto.tcp.seen[0].td_maxwin;
1065 conntrack->proto.tcp.seen[0].td_scale = 0;
1067 /* We assume SACK. Should we assume window scaling too? */
1068 conntrack->proto.tcp.seen[0].flags =
1069 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
1070 conntrack->proto.tcp.seen[0].loose =
1071 conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
1074 conntrack->proto.tcp.seen[1].td_end = 0;
1075 conntrack->proto.tcp.seen[1].td_maxend = 0;
1076 conntrack->proto.tcp.seen[1].td_maxwin = 1;
1077 conntrack->proto.tcp.seen[1].td_scale = 0;
1079 /* tcp_packet will set them */
1080 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1081 conntrack->proto.tcp.last_index = TCP_NONE_SET;
1083 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1084 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1085 sender->td_end, sender->td_maxend, sender->td_maxwin,
1086 sender->td_scale,
1087 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1088 receiver->td_scale);
1089 return 1;
1092 struct ip_conntrack_protocol ip_conntrack_protocol_tcp =
1094 .proto = IPPROTO_TCP,
1095 .name = "tcp",
1096 .pkt_to_tuple = tcp_pkt_to_tuple,
1097 .invert_tuple = tcp_invert_tuple,
1098 .print_tuple = tcp_print_tuple,
1099 .print_conntrack = tcp_print_conntrack,
1100 .packet = tcp_packet,
1101 .new = tcp_new,
1102 .error = tcp_error,