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
15 * - State table bugfixes
16 * - More robust state changes
17 * - Tuning timer parameters
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>
30 #include <linux/tcp.h>
31 #include <linux/spinlock.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>
44 #define DEBUGP(format, args...)
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
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
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[] = {
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. */
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
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] = {
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.
189 * sTW -> sSS Reopened connection (RFC 1122).
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
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.
210 * sFW -> sLA FIN seen in both directions, waiting for
212 * Migth be a retransmitted FIN as well...
214 * sLA -> sLA Retransmitted FIN. Remain in the same state.
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.
225 * sFW -> sCW Normal close request answered by ACK.
227 * sLA -> sTW Last ACK detected.
228 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
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 }
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.
247 * sTW -> sIV Reopened connection, but server may not do it.
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
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.
268 * sFW -> sLA FIN seen in both directions.
270 * sLA -> sLA Retransmitted FIN.
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.
280 * sFW -> sCW Normal close request answered by ACK.
282 * sLA -> sTW Last ACK detected.
283 * sTW -> sTW Retransmitted last ACK.
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);
303 tuple->src.u.tcp.port = hp->source;
304 tuple->dst.u.tcp.port = hp->dest;
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;
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 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
340 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
341 static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
342 const struct ip_conntrack *ct)
344 struct nfattr *nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
346 read_lock_bh(&tcp_lock);
347 NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
348 &ct->proto.tcp.state);
349 read_unlock_bh(&tcp_lock);
351 NFA_NEST_END(skb, nest_parms);
356 read_unlock_bh(&tcp_lock);
360 static int nfattr_to_tcp(struct nfattr *cda[], struct ip_conntrack *ct)
362 struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
363 struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
365 nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
367 if (!tb[CTA_PROTOINFO_TCP_STATE-1])
370 write_lock_bh(&tcp_lock);
371 ct->proto.tcp.state =
372 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
373 write_unlock_bh(&tcp_lock);
379 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
381 if (tcph->rst) return TCP_RST_SET;
382 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
383 else if (tcph->fin) return TCP_FIN_SET;
384 else if (tcph->ack) return TCP_ACK_SET;
385 else return TCP_NONE_SET;
388 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
389 in IP Filter' by Guido van Rooij.
391 http://www.nluug.nl/events/sane2000/papers.html
392 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
394 The boundaries and the conditions are changed according to RFC793:
395 the packet must intersect the window (i.e. segments may be
396 after the right or before the left edge) and thus receivers may ACK
397 segments after the right edge of the window.
399 td_maxend = max(sack + max(win,1)) seen in reply packets
400 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
401 td_maxwin += seq + len - sender.td_maxend
402 if seq + len > sender.td_maxend
403 td_end = max(seq + len) seen in sent packets
405 I. Upper bound for valid data: seq <= sender.td_maxend
406 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
407 III. Upper bound for valid ack: sack <= receiver.td_end
408 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
410 where sack is the highest right edge of sack block found in the packet.
412 The upper bound limit for a valid ack is not ignored -
413 we doesn't have to deal with fragments.
416 static inline __u32 segment_seq_plus_len(__u32 seq,
421 return (seq + len - (iph->ihl + tcph->doff)*4
422 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
425 /* Fixme: what about big packets? */
426 #define MAXACKWINCONST 66000
427 #define MAXACKWINDOW(sender) \
428 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
432 * Simplified tcp_parse_options routine from tcp_input.c
434 static void tcp_options(const struct sk_buff *skb,
437 struct ip_ct_tcp_state *state)
439 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
441 int length = (tcph->doff*4) - sizeof(struct tcphdr);
446 ptr = skb_header_pointer(skb,
447 (iph->ihl * 4) + sizeof(struct tcphdr),
461 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
466 if (opsize < 2) /* "silly options" */
469 break; /* don't parse partial options */
471 if (opcode == TCPOPT_SACK_PERM
472 && opsize == TCPOLEN_SACK_PERM)
473 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
474 else if (opcode == TCPOPT_WINDOW
475 && opsize == TCPOLEN_WINDOW) {
476 state->td_scale = *(u_int8_t *)ptr;
478 if (state->td_scale > 14) {
480 state->td_scale = 14;
483 IP_CT_TCP_FLAG_WINDOW_SCALE;
491 static void tcp_sack(const struct sk_buff *skb,
496 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
498 int length = (tcph->doff*4) - sizeof(struct tcphdr);
504 ptr = skb_header_pointer(skb,
505 (iph->ihl * 4) + sizeof(struct tcphdr),
509 /* Fast path for timestamp-only option */
510 if (length == TCPOLEN_TSTAMP_ALIGNED*4
512 __constant_ntohl((TCPOPT_NOP << 24)
514 | (TCPOPT_TIMESTAMP << 8)
515 | TCPOLEN_TIMESTAMP))
525 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
530 if (opsize < 2) /* "silly options" */
533 break; /* don't parse partial options */
535 if (opcode == TCPOPT_SACK
536 && opsize >= (TCPOLEN_SACK_BASE
537 + TCPOLEN_SACK_PERBLOCK)
538 && !((opsize - TCPOLEN_SACK_BASE)
539 % TCPOLEN_SACK_PERBLOCK)) {
541 i < (opsize - TCPOLEN_SACK_BASE);
542 i += TCPOLEN_SACK_PERBLOCK) {
543 tmp = ntohl(*((u_int32_t *)(ptr+i)+1));
545 if (after(tmp, *sack))
556 static int tcp_in_window(struct ip_ct_tcp *state,
557 enum ip_conntrack_dir dir,
559 const struct sk_buff *skb,
563 struct ip_ct_tcp_state *sender = &state->seen[dir];
564 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
565 __u32 seq, ack, sack, end, win, swin;
569 * Get the required data from the packet.
571 seq = ntohl(tcph->seq);
572 ack = sack = ntohl(tcph->ack_seq);
573 win = ntohs(tcph->window);
574 end = segment_seq_plus_len(seq, skb->len, iph, tcph);
576 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
577 tcp_sack(skb, iph, tcph, &sack);
579 DEBUGP("tcp_in_window: START\n");
580 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
581 "seq=%u ack=%u sack=%u win=%u end=%u\n",
582 NIPQUAD(iph->saddr), ntohs(tcph->source),
583 NIPQUAD(iph->daddr), ntohs(tcph->dest),
584 seq, ack, sack, win, end);
585 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
586 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
587 sender->td_end, sender->td_maxend, sender->td_maxwin,
589 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
592 if (sender->td_end == 0) {
594 * Initialize sender data.
596 if (tcph->syn && tcph->ack) {
598 * Outgoing SYN-ACK in reply to a SYN.
601 sender->td_maxend = end;
602 sender->td_maxwin = (win == 0 ? 1 : win);
604 tcp_options(skb, iph, tcph, sender);
607 * Both sides must send the Window Scale option
608 * to enable window scaling in either direction.
610 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
611 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
613 receiver->td_scale = 0;
616 * We are in the middle of a connection,
617 * its history is lost for us.
618 * Let's try to use the data from the packet.
620 sender->td_end = end;
621 sender->td_maxwin = (win == 0 ? 1 : win);
622 sender->td_maxend = end + sender->td_maxwin;
624 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
625 && dir == IP_CT_DIR_ORIGINAL)
626 || (state->state == TCP_CONNTRACK_SYN_RECV
627 && dir == IP_CT_DIR_REPLY))
628 && after(end, sender->td_end)) {
630 * RFC 793: "if a TCP is reinitialized ... then it need
631 * not wait at all; it must only be sure to use sequence
632 * numbers larger than those recently used."
635 sender->td_maxend = end;
636 sender->td_maxwin = (win == 0 ? 1 : win);
638 tcp_options(skb, iph, tcph, sender);
643 * If there is no ACK, just pretend it was set and OK.
645 ack = sack = receiver->td_end;
646 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
647 (TCP_FLAG_ACK|TCP_FLAG_RST))
650 * Broken TCP stacks, that set ACK in RST packets as well
651 * with zero ack value.
653 ack = sack = receiver->td_end;
658 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
660 * Packets contains no data: we assume it is valid
661 * and check the ack value only.
662 * However RST segments are always validated by their
663 * SEQ number, except when seq == 0 (reset sent answering
666 seq = end = sender->td_end;
668 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
669 "seq=%u ack=%u sack =%u win=%u end=%u\n",
670 NIPQUAD(iph->saddr), ntohs(tcph->source),
671 NIPQUAD(iph->daddr), ntohs(tcph->dest),
672 seq, ack, sack, win, end);
673 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
674 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
675 sender->td_end, sender->td_maxend, sender->td_maxwin,
677 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
680 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
681 before(seq, sender->td_maxend + 1),
682 after(end, sender->td_end - receiver->td_maxwin - 1),
683 before(sack, receiver->td_end + 1),
684 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
686 if (sender->loose || receiver->loose ||
687 (before(seq, sender->td_maxend + 1) &&
688 after(end, sender->td_end - receiver->td_maxwin - 1) &&
689 before(sack, receiver->td_end + 1) &&
690 after(ack, receiver->td_end - MAXACKWINDOW(sender)))) {
692 * Take into account window scaling (RFC 1323).
695 win <<= sender->td_scale;
698 * Update sender data.
700 swin = win + (sack - ack);
701 if (sender->td_maxwin < swin)
702 sender->td_maxwin = swin;
703 if (after(end, sender->td_end))
704 sender->td_end = end;
706 * Update receiver data.
708 if (after(end, sender->td_maxend))
709 receiver->td_maxwin += end - sender->td_maxend;
710 if (after(sack + win, receiver->td_maxend - 1)) {
711 receiver->td_maxend = sack + win;
713 receiver->td_maxend++;
717 * Check retransmissions.
719 if (index == TCP_ACK_SET) {
720 if (state->last_dir == dir
721 && state->last_seq == seq
722 && state->last_ack == ack
723 && state->last_end == end)
726 state->last_dir = dir;
727 state->last_seq = seq;
728 state->last_ack = ack;
729 state->last_end = end;
734 * Close the window of disabled window tracking :-)
741 if (LOG_INVALID(IPPROTO_TCP))
742 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
744 before(seq, sender->td_maxend + 1) ?
745 after(end, sender->td_end - receiver->td_maxwin - 1) ?
746 before(sack, receiver->td_end + 1) ?
747 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
748 : "ACK is under the lower bound (possible overly delayed ACK)"
749 : "ACK is over the upper bound (ACKed data not seen yet)"
750 : "SEQ is under the lower bound (already ACKed data retransmitted)"
751 : "SEQ is over the upper bound (over the window of the receiver)");
753 res = ip_ct_tcp_be_liberal;
756 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
757 "receiver end=%u maxend=%u maxwin=%u\n",
758 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
759 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
764 #ifdef CONFIG_IP_NF_NAT_NEEDED
765 /* Update sender->td_end after NAT successfully mangled the packet */
766 void ip_conntrack_tcp_update(struct sk_buff *skb,
767 struct ip_conntrack *conntrack,
768 enum ip_conntrack_dir dir)
770 struct iphdr *iph = skb->nh.iph;
771 struct tcphdr *tcph = (void *)skb->nh.iph + skb->nh.iph->ihl*4;
774 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
775 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
778 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, iph, tcph);
780 write_lock_bh(&tcp_lock);
782 * We have to worry for the ack in the reply packet only...
784 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
785 conntrack->proto.tcp.seen[dir].td_end = end;
786 conntrack->proto.tcp.last_end = end;
787 write_unlock_bh(&tcp_lock);
788 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
789 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
790 sender->td_end, sender->td_maxend, sender->td_maxwin,
792 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
807 /* table of valid flag combinations - ECE and CWR are always valid */
808 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
812 [TH_SYN|TH_ACK|TH_PUSH] = 1,
815 [TH_RST|TH_ACK|TH_PUSH] = 1,
818 [TH_ACK|TH_PUSH] = 1,
820 [TH_ACK|TH_URG|TH_PUSH] = 1,
821 [TH_FIN|TH_ACK|TH_PUSH] = 1,
822 [TH_FIN|TH_ACK|TH_URG] = 1,
823 [TH_FIN|TH_ACK|TH_URG|TH_PUSH] = 1,
826 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
827 static int tcp_error(struct sk_buff *skb,
828 enum ip_conntrack_info *ctinfo,
829 unsigned int hooknum)
831 struct iphdr *iph = skb->nh.iph;
832 struct tcphdr _tcph, *th;
833 unsigned int tcplen = skb->len - iph->ihl * 4;
836 /* Smaller that minimal TCP header? */
837 th = skb_header_pointer(skb, iph->ihl * 4,
838 sizeof(_tcph), &_tcph);
840 if (LOG_INVALID(IPPROTO_TCP))
841 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
842 "ip_ct_tcp: short packet ");
846 /* Not whole TCP header or malformed packet */
847 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
848 if (LOG_INVALID(IPPROTO_TCP))
849 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
850 "ip_ct_tcp: truncated/malformed packet ");
854 /* Checksum invalid? Ignore.
855 * We skip checking packets on the outgoing path
856 * because the semantic of CHECKSUM_HW is different there
857 * and moreover root might send raw packets.
859 /* FIXME: Source route IP option packets --RR */
860 if (hooknum == NF_IP_PRE_ROUTING
861 && skb->ip_summed != CHECKSUM_UNNECESSARY
862 && csum_tcpudp_magic(iph->saddr, iph->daddr, tcplen, IPPROTO_TCP,
863 skb->ip_summed == CHECKSUM_HW ? skb->csum
864 : skb_checksum(skb, iph->ihl*4, tcplen, 0))) {
865 if (LOG_INVALID(IPPROTO_TCP))
866 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
867 "ip_ct_tcp: bad TCP checksum ");
871 /* Check TCP flags. */
872 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
873 if (!tcp_valid_flags[tcpflags]) {
874 if (LOG_INVALID(IPPROTO_TCP))
875 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
876 "ip_ct_tcp: invalid TCP flag combination ");
883 /* Returns verdict for packet, or -1 for invalid. */
884 static int tcp_packet(struct ip_conntrack *conntrack,
885 const struct sk_buff *skb,
886 enum ip_conntrack_info ctinfo)
888 enum tcp_conntrack new_state, old_state;
889 enum ip_conntrack_dir dir;
890 struct iphdr *iph = skb->nh.iph;
891 struct tcphdr *th, _tcph;
892 unsigned long timeout;
895 th = skb_header_pointer(skb, iph->ihl * 4,
896 sizeof(_tcph), &_tcph);
899 write_lock_bh(&tcp_lock);
900 old_state = conntrack->proto.tcp.state;
901 dir = CTINFO2DIR(ctinfo);
902 index = get_conntrack_index(th);
903 new_state = tcp_conntracks[dir][index][old_state];
906 case TCP_CONNTRACK_IGNORE:
907 /* Either SYN in ORIGINAL
908 * or SYN/ACK in REPLY. */
909 if (index == TCP_SYNACK_SET
910 && conntrack->proto.tcp.last_index == TCP_SYN_SET
911 && conntrack->proto.tcp.last_dir != dir
912 && ntohl(th->ack_seq) ==
913 conntrack->proto.tcp.last_end) {
914 /* This SYN/ACK acknowledges a SYN that we earlier
915 * ignored as invalid. This means that the client and
916 * the server are both in sync, while the firewall is
917 * not. We kill this session and block the SYN/ACK so
918 * that the client cannot but retransmit its SYN and
919 * thus initiate a clean new session.
921 write_unlock_bh(&tcp_lock);
922 if (LOG_INVALID(IPPROTO_TCP))
923 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
925 "killing out of sync session ");
926 if (del_timer(&conntrack->timeout))
927 conntrack->timeout.function((unsigned long)
931 conntrack->proto.tcp.last_index = index;
932 conntrack->proto.tcp.last_dir = dir;
933 conntrack->proto.tcp.last_seq = ntohl(th->seq);
934 conntrack->proto.tcp.last_end =
935 segment_seq_plus_len(ntohl(th->seq), skb->len, iph, th);
937 write_unlock_bh(&tcp_lock);
938 if (LOG_INVALID(IPPROTO_TCP))
939 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
940 "ip_ct_tcp: invalid packet ignored ");
942 case TCP_CONNTRACK_MAX:
944 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
945 dir, get_conntrack_index(th),
947 write_unlock_bh(&tcp_lock);
948 if (LOG_INVALID(IPPROTO_TCP))
949 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
950 "ip_ct_tcp: invalid state ");
952 case TCP_CONNTRACK_SYN_SENT:
953 if (old_state < TCP_CONNTRACK_TIME_WAIT)
955 if ((conntrack->proto.tcp.seen[dir].flags &
956 IP_CT_TCP_FLAG_CLOSE_INIT)
957 || after(ntohl(th->seq),
958 conntrack->proto.tcp.seen[dir].td_end)) {
959 /* Attempt to reopen a closed connection.
960 * Delete this connection and look up again. */
961 write_unlock_bh(&tcp_lock);
962 if (del_timer(&conntrack->timeout))
963 conntrack->timeout.function((unsigned long)
967 write_unlock_bh(&tcp_lock);
968 if (LOG_INVALID(IPPROTO_TCP))
969 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
970 NULL, "ip_ct_tcp: invalid SYN");
973 case TCP_CONNTRACK_CLOSE:
974 if (index == TCP_RST_SET
975 && test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
976 && conntrack->proto.tcp.last_index == TCP_SYN_SET
977 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
978 /* RST sent to invalid SYN we had let trough
979 * SYN was in window then, tear down connection.
980 * We skip window checking, because packet might ACK
981 * segments we ignored in the SYN. */
984 /* Just fall trough */
986 /* Keep compilers happy. */
990 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
992 write_unlock_bh(&tcp_lock);
996 /* From now on we have got in-window packets */
997 conntrack->proto.tcp.last_index = index;
999 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
1000 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1001 NIPQUAD(iph->saddr), ntohs(th->source),
1002 NIPQUAD(iph->daddr), ntohs(th->dest),
1003 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1004 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1005 old_state, new_state);
1007 conntrack->proto.tcp.state = new_state;
1008 if (old_state != new_state
1009 && (new_state == TCP_CONNTRACK_FIN_WAIT
1010 || new_state == TCP_CONNTRACK_CLOSE))
1011 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1012 timeout = conntrack->proto.tcp.retrans >= ip_ct_tcp_max_retrans
1013 && *tcp_timeouts[new_state] > ip_ct_tcp_timeout_max_retrans
1014 ? ip_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
1015 write_unlock_bh(&tcp_lock);
1017 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
1018 if (new_state != old_state)
1019 ip_conntrack_event_cache(IPCT_PROTOINFO, skb);
1021 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
1022 /* If only reply is a RST, we can consider ourselves not to
1023 have an established connection: this is a fairly common
1024 problem case, so we can delete the conntrack
1025 immediately. --RR */
1027 if (del_timer(&conntrack->timeout))
1028 conntrack->timeout.function((unsigned long)
1032 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
1033 && (old_state == TCP_CONNTRACK_SYN_RECV
1034 || old_state == TCP_CONNTRACK_ESTABLISHED)
1035 && new_state == TCP_CONNTRACK_ESTABLISHED) {
1036 /* Set ASSURED if we see see valid ack in ESTABLISHED
1037 after SYN_RECV or a valid answer for a picked up
1039 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1040 ip_conntrack_event_cache(IPCT_STATUS, skb);
1042 ip_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1047 /* Called when a new connection for this protocol found. */
1048 static int tcp_new(struct ip_conntrack *conntrack,
1049 const struct sk_buff *skb)
1051 enum tcp_conntrack new_state;
1052 struct iphdr *iph = skb->nh.iph;
1053 struct tcphdr *th, _tcph;
1055 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1056 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1059 th = skb_header_pointer(skb, iph->ihl * 4,
1060 sizeof(_tcph), &_tcph);
1063 /* Don't need lock here: this conntrack not in circulation yet */
1065 = tcp_conntracks[0][get_conntrack_index(th)]
1066 [TCP_CONNTRACK_NONE];
1068 /* Invalid: delete conntrack */
1069 if (new_state >= TCP_CONNTRACK_MAX) {
1070 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1074 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1076 conntrack->proto.tcp.seen[0].td_end =
1077 segment_seq_plus_len(ntohl(th->seq), skb->len,
1079 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1080 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1081 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1082 conntrack->proto.tcp.seen[0].td_maxend =
1083 conntrack->proto.tcp.seen[0].td_end;
1085 tcp_options(skb, iph, th, &conntrack->proto.tcp.seen[0]);
1086 conntrack->proto.tcp.seen[1].flags = 0;
1087 conntrack->proto.tcp.seen[0].loose =
1088 conntrack->proto.tcp.seen[1].loose = 0;
1089 } else if (ip_ct_tcp_loose == 0) {
1090 /* Don't try to pick up connections. */
1094 * We are in the middle of a connection,
1095 * its history is lost for us.
1096 * Let's try to use the data from the packet.
1098 conntrack->proto.tcp.seen[0].td_end =
1099 segment_seq_plus_len(ntohl(th->seq), skb->len,
1101 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1102 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1103 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1104 conntrack->proto.tcp.seen[0].td_maxend =
1105 conntrack->proto.tcp.seen[0].td_end +
1106 conntrack->proto.tcp.seen[0].td_maxwin;
1107 conntrack->proto.tcp.seen[0].td_scale = 0;
1109 /* We assume SACK. Should we assume window scaling too? */
1110 conntrack->proto.tcp.seen[0].flags =
1111 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM;
1112 conntrack->proto.tcp.seen[0].loose =
1113 conntrack->proto.tcp.seen[1].loose = ip_ct_tcp_loose;
1116 conntrack->proto.tcp.seen[1].td_end = 0;
1117 conntrack->proto.tcp.seen[1].td_maxend = 0;
1118 conntrack->proto.tcp.seen[1].td_maxwin = 1;
1119 conntrack->proto.tcp.seen[1].td_scale = 0;
1121 /* tcp_packet will set them */
1122 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1123 conntrack->proto.tcp.last_index = TCP_NONE_SET;
1125 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1126 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1127 sender->td_end, sender->td_maxend, sender->td_maxwin,
1129 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1130 receiver->td_scale);
1134 struct ip_conntrack_protocol ip_conntrack_protocol_tcp =
1136 .proto = IPPROTO_TCP,
1138 .pkt_to_tuple = tcp_pkt_to_tuple,
1139 .invert_tuple = tcp_invert_tuple,
1140 .print_tuple = tcp_print_tuple,
1141 .print_conntrack = tcp_print_conntrack,
1142 .packet = tcp_packet,
1145 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
1146 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
1147 .to_nfattr = tcp_to_nfattr,
1148 .from_nfattr = nfattr_to_tcp,
1149 .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr,
1150 .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple,