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/types.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/netfilter.h>
26 #include <linux/module.h>
29 #include <linux/tcp.h>
30 #include <linux/spinlock.h>
34 #include <linux/netfilter_ipv4.h>
35 #include <linux/netfilter_ipv4/ip_conntrack.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
42 #define DEBUGP(format, args...)
45 /* Protects conntrack->proto.tcp */
46 static DEFINE_RWLOCK(tcp_lock
);
48 /* "Be conservative in what you do,
49 be liberal in what you accept from others."
50 If it's non-zero, we mark only out of window RST segments as INVALID. */
51 int ip_ct_tcp_be_liberal __read_mostly
= 0;
53 /* If it is set to zero, we disable picking up already established
55 int ip_ct_tcp_loose __read_mostly
= 1;
57 /* Max number of the retransmitted packets without receiving an (acceptable)
58 ACK from the destination. If this number is reached, a shorter timer
60 int ip_ct_tcp_max_retrans __read_mostly
= 3;
62 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
63 closely. They're more complex. --RR */
65 static const char *tcp_conntrack_names
[] = {
79 #define MINS * 60 SECS
80 #define HOURS * 60 MINS
81 #define DAYS * 24 HOURS
83 unsigned int ip_ct_tcp_timeout_syn_sent __read_mostly
= 2 MINS
;
84 unsigned int ip_ct_tcp_timeout_syn_recv __read_mostly
= 60 SECS
;
85 unsigned int ip_ct_tcp_timeout_established __read_mostly
= 5 DAYS
;
86 unsigned int ip_ct_tcp_timeout_fin_wait __read_mostly
= 2 MINS
;
87 unsigned int ip_ct_tcp_timeout_close_wait __read_mostly
= 60 SECS
;
88 unsigned int ip_ct_tcp_timeout_last_ack __read_mostly
= 30 SECS
;
89 unsigned int ip_ct_tcp_timeout_time_wait __read_mostly
= 2 MINS
;
90 unsigned int ip_ct_tcp_timeout_close __read_mostly
= 10 SECS
;
92 /* RFC1122 says the R2 limit should be at least 100 seconds.
93 Linux uses 15 packets as limit, which corresponds
94 to ~13-30min depending on RTO. */
95 unsigned int ip_ct_tcp_timeout_max_retrans __read_mostly
= 5 MINS
;
97 static const unsigned int * tcp_timeouts
[]
98 = { NULL
, /* TCP_CONNTRACK_NONE */
99 &ip_ct_tcp_timeout_syn_sent
, /* TCP_CONNTRACK_SYN_SENT, */
100 &ip_ct_tcp_timeout_syn_recv
, /* TCP_CONNTRACK_SYN_RECV, */
101 &ip_ct_tcp_timeout_established
, /* TCP_CONNTRACK_ESTABLISHED, */
102 &ip_ct_tcp_timeout_fin_wait
, /* TCP_CONNTRACK_FIN_WAIT, */
103 &ip_ct_tcp_timeout_close_wait
, /* TCP_CONNTRACK_CLOSE_WAIT, */
104 &ip_ct_tcp_timeout_last_ack
, /* TCP_CONNTRACK_LAST_ACK, */
105 &ip_ct_tcp_timeout_time_wait
, /* TCP_CONNTRACK_TIME_WAIT, */
106 &ip_ct_tcp_timeout_close
, /* TCP_CONNTRACK_CLOSE, */
107 NULL
, /* TCP_CONNTRACK_LISTEN */
110 #define sNO TCP_CONNTRACK_NONE
111 #define sSS TCP_CONNTRACK_SYN_SENT
112 #define sSR TCP_CONNTRACK_SYN_RECV
113 #define sES TCP_CONNTRACK_ESTABLISHED
114 #define sFW TCP_CONNTRACK_FIN_WAIT
115 #define sCW TCP_CONNTRACK_CLOSE_WAIT
116 #define sLA TCP_CONNTRACK_LAST_ACK
117 #define sTW TCP_CONNTRACK_TIME_WAIT
118 #define sCL TCP_CONNTRACK_CLOSE
119 #define sLI TCP_CONNTRACK_LISTEN
120 #define sIV TCP_CONNTRACK_MAX
121 #define sIG TCP_CONNTRACK_IGNORE
123 /* What TCP flags are set from RST/SYN/FIN/ACK. */
134 * The TCP state transition table needs a few words...
136 * We are the man in the middle. All the packets go through us
137 * but might get lost in transit to the destination.
138 * It is assumed that the destinations can't receive segments
141 * The checked segment is in window, but our windows are *not*
142 * equivalent with the ones of the sender/receiver. We always
143 * try to guess the state of the current sender.
145 * The meaning of the states are:
147 * NONE: initial state
148 * SYN_SENT: SYN-only packet seen
149 * SYN_RECV: SYN-ACK packet seen
150 * ESTABLISHED: ACK packet seen
151 * FIN_WAIT: FIN packet seen
152 * CLOSE_WAIT: ACK seen (after FIN)
153 * LAST_ACK: FIN seen (after FIN)
154 * TIME_WAIT: last ACK seen
155 * CLOSE: closed connection
157 * LISTEN state is not used.
159 * Packets marked as IGNORED (sIG):
160 * if they may be either invalid or valid
161 * and the receiver may send back a connection
162 * closing RST or a SYN/ACK.
164 * Packets marked as INVALID (sIV):
165 * if they are invalid
166 * or we do not support the request (simultaneous open)
168 static const enum tcp_conntrack tcp_conntracks
[2][6][TCP_CONNTRACK_MAX
] = {
171 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
172 /*syn*/ { sSS
, sSS
, sIG
, sIG
, sIG
, sIG
, sIG
, sSS
, sSS
, sIV
},
174 * sNO -> sSS Initialize a new connection
175 * sSS -> sSS Retransmitted SYN
176 * sSR -> sIG Late retransmitted SYN?
177 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
178 * are errors. Receiver will reply with RST
179 * and close the connection.
180 * Or we are not in sync and hold a dead connection.
184 * sTW -> sSS Reopened connection (RFC 1122).
187 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
188 /*synack*/ { sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
},
190 * A SYN/ACK from the client is always invalid:
191 * - either it tries to set up a simultaneous open, which is
193 * - or the firewall has just been inserted between the two hosts
194 * during the session set-up. The SYN will be retransmitted
195 * by the true client (or it'll time out).
197 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
198 /*fin*/ { sIV
, sIV
, sFW
, sFW
, sLA
, sLA
, sLA
, sTW
, sCL
, sIV
},
200 * sNO -> sIV Too late and no reason to do anything...
201 * sSS -> sIV Client migth not send FIN in this state:
202 * we enforce waiting for a SYN/ACK reply first.
203 * sSR -> sFW Close started.
205 * sFW -> sLA FIN seen in both directions, waiting for
207 * Migth be a retransmitted FIN as well...
209 * sLA -> sLA Retransmitted FIN. Remain in the same state.
213 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
214 /*ack*/ { sES
, sIV
, sES
, sES
, sCW
, sCW
, sTW
, sTW
, sCL
, sIV
},
216 * sNO -> sES Assumed.
217 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
218 * sSR -> sES Established state is reached.
220 * sFW -> sCW Normal close request answered by ACK.
222 * sLA -> sTW Last ACK detected.
223 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
226 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
227 /*rst*/ { sIV
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sIV
},
228 /*none*/ { sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
}
232 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
233 /*syn*/ { sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
},
235 * sNO -> sIV Never reached.
236 * sSS -> sIV Simultaneous open, not supported
237 * sSR -> sIV Simultaneous open, not supported.
238 * sES -> sIV Server may not initiate a connection.
242 * sTW -> sIV Reopened connection, but server may not do it.
245 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
246 /*synack*/ { sIV
, sSR
, sSR
, sIG
, sIG
, sIG
, sIG
, sIG
, sIG
, sIV
},
248 * sSS -> sSR Standard open.
249 * sSR -> sSR Retransmitted SYN/ACK.
250 * sES -> sIG Late retransmitted SYN/ACK?
251 * sFW -> sIG Might be SYN/ACK answering ignored SYN
257 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
258 /*fin*/ { sIV
, sIV
, sFW
, sFW
, sLA
, sLA
, sLA
, sTW
, sCL
, sIV
},
260 * sSS -> sIV Server might not send FIN in this state.
261 * sSR -> sFW Close started.
263 * sFW -> sLA FIN seen in both directions.
265 * sLA -> sLA Retransmitted FIN.
269 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
270 /*ack*/ { sIV
, sIG
, sSR
, sES
, sCW
, sCW
, sTW
, sTW
, sCL
, sIV
},
272 * sSS -> sIG Might be a half-open connection.
273 * sSR -> sSR Might answer late resent SYN.
275 * sFW -> sCW Normal close request answered by ACK.
277 * sLA -> sTW Last ACK detected.
278 * sTW -> sTW Retransmitted last ACK.
281 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
282 /*rst*/ { sIV
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sCL
, sIV
},
283 /*none*/ { sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
, sIV
}
287 static int tcp_pkt_to_tuple(const struct sk_buff
*skb
,
288 unsigned int dataoff
,
289 struct ip_conntrack_tuple
*tuple
)
291 struct tcphdr _hdr
, *hp
;
293 /* Actually only need first 8 bytes. */
294 hp
= skb_header_pointer(skb
, dataoff
, 8, &_hdr
);
298 tuple
->src
.u
.tcp
.port
= hp
->source
;
299 tuple
->dst
.u
.tcp
.port
= hp
->dest
;
304 static int tcp_invert_tuple(struct ip_conntrack_tuple
*tuple
,
305 const struct ip_conntrack_tuple
*orig
)
307 tuple
->src
.u
.tcp
.port
= orig
->dst
.u
.tcp
.port
;
308 tuple
->dst
.u
.tcp
.port
= orig
->src
.u
.tcp
.port
;
312 /* Print out the per-protocol part of the tuple. */
313 static int tcp_print_tuple(struct seq_file
*s
,
314 const struct ip_conntrack_tuple
*tuple
)
316 return seq_printf(s
, "sport=%hu dport=%hu ",
317 ntohs(tuple
->src
.u
.tcp
.port
),
318 ntohs(tuple
->dst
.u
.tcp
.port
));
321 /* Print out the private part of the conntrack. */
322 static int tcp_print_conntrack(struct seq_file
*s
,
323 const struct ip_conntrack
*conntrack
)
325 enum tcp_conntrack state
;
327 read_lock_bh(&tcp_lock
);
328 state
= conntrack
->proto
.tcp
.state
;
329 read_unlock_bh(&tcp_lock
);
331 return seq_printf(s
, "%s ", tcp_conntrack_names
[state
]);
334 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
335 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
336 static int tcp_to_nfattr(struct sk_buff
*skb
, struct nfattr
*nfa
,
337 const struct ip_conntrack
*ct
)
339 struct nfattr
*nest_parms
;
341 read_lock_bh(&tcp_lock
);
342 nest_parms
= NFA_NEST(skb
, CTA_PROTOINFO_TCP
);
343 NFA_PUT(skb
, CTA_PROTOINFO_TCP_STATE
, sizeof(u_int8_t
),
344 &ct
->proto
.tcp
.state
);
345 read_unlock_bh(&tcp_lock
);
347 NFA_NEST_END(skb
, nest_parms
);
352 read_unlock_bh(&tcp_lock
);
356 static const size_t cta_min_tcp
[CTA_PROTOINFO_TCP_MAX
] = {
357 [CTA_PROTOINFO_TCP_STATE
-1] = sizeof(u_int8_t
),
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 /* updates could not contain anything about the private
366 * protocol info, in that case skip the parsing */
370 nfattr_parse_nested(tb
, CTA_PROTOINFO_TCP_MAX
, attr
);
372 if (nfattr_bad_size(tb
, CTA_PROTOINFO_TCP_MAX
, cta_min_tcp
))
375 if (!tb
[CTA_PROTOINFO_TCP_STATE
-1])
378 write_lock_bh(&tcp_lock
);
379 ct
->proto
.tcp
.state
=
380 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTOINFO_TCP_STATE
-1]);
381 write_unlock_bh(&tcp_lock
);
387 static unsigned int get_conntrack_index(const struct tcphdr
*tcph
)
389 if (tcph
->rst
) return TCP_RST_SET
;
390 else if (tcph
->syn
) return (tcph
->ack
? TCP_SYNACK_SET
: TCP_SYN_SET
);
391 else if (tcph
->fin
) return TCP_FIN_SET
;
392 else if (tcph
->ack
) return TCP_ACK_SET
;
393 else return TCP_NONE_SET
;
396 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
397 in IP Filter' by Guido van Rooij.
399 http://www.nluug.nl/events/sane2000/papers.html
400 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
402 The boundaries and the conditions are changed according to RFC793:
403 the packet must intersect the window (i.e. segments may be
404 after the right or before the left edge) and thus receivers may ACK
405 segments after the right edge of the window.
407 td_maxend = max(sack + max(win,1)) seen in reply packets
408 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
409 td_maxwin += seq + len - sender.td_maxend
410 if seq + len > sender.td_maxend
411 td_end = max(seq + len) seen in sent packets
413 I. Upper bound for valid data: seq <= sender.td_maxend
414 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
415 III. Upper bound for valid ack: sack <= receiver.td_end
416 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
418 where sack is the highest right edge of sack block found in the packet.
420 The upper bound limit for a valid ack is not ignored -
421 we doesn't have to deal with fragments.
424 static inline __u32
segment_seq_plus_len(__u32 seq
,
429 return (seq
+ len
- (iph
->ihl
+ tcph
->doff
)*4
430 + (tcph
->syn
? 1 : 0) + (tcph
->fin
? 1 : 0));
433 /* Fixme: what about big packets? */
434 #define MAXACKWINCONST 66000
435 #define MAXACKWINDOW(sender) \
436 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
440 * Simplified tcp_parse_options routine from tcp_input.c
442 static void tcp_options(const struct sk_buff
*skb
,
445 struct ip_ct_tcp_state
*state
)
447 unsigned char buff
[(15 * 4) - sizeof(struct tcphdr
)];
449 int length
= (tcph
->doff
*4) - sizeof(struct tcphdr
);
454 ptr
= skb_header_pointer(skb
,
455 (iph
->ihl
* 4) + sizeof(struct tcphdr
),
469 case TCPOPT_NOP
: /* Ref: RFC 793 section 3.1 */
474 if (opsize
< 2) /* "silly options" */
477 break; /* don't parse partial options */
479 if (opcode
== TCPOPT_SACK_PERM
480 && opsize
== TCPOLEN_SACK_PERM
)
481 state
->flags
|= IP_CT_TCP_FLAG_SACK_PERM
;
482 else if (opcode
== TCPOPT_WINDOW
483 && opsize
== TCPOLEN_WINDOW
) {
484 state
->td_scale
= *(u_int8_t
*)ptr
;
486 if (state
->td_scale
> 14) {
488 state
->td_scale
= 14;
491 IP_CT_TCP_FLAG_WINDOW_SCALE
;
499 static void tcp_sack(const struct sk_buff
*skb
,
504 unsigned char buff
[(15 * 4) - sizeof(struct tcphdr
)];
506 int length
= (tcph
->doff
*4) - sizeof(struct tcphdr
);
512 ptr
= skb_header_pointer(skb
,
513 (iph
->ihl
* 4) + sizeof(struct tcphdr
),
517 /* Fast path for timestamp-only option */
518 if (length
== TCPOLEN_TSTAMP_ALIGNED
*4
520 __constant_htonl((TCPOPT_NOP
<< 24)
522 | (TCPOPT_TIMESTAMP
<< 8)
523 | TCPOLEN_TIMESTAMP
))
533 case TCPOPT_NOP
: /* Ref: RFC 793 section 3.1 */
538 if (opsize
< 2) /* "silly options" */
541 break; /* don't parse partial options */
543 if (opcode
== TCPOPT_SACK
544 && opsize
>= (TCPOLEN_SACK_BASE
545 + TCPOLEN_SACK_PERBLOCK
)
546 && !((opsize
- TCPOLEN_SACK_BASE
)
547 % TCPOLEN_SACK_PERBLOCK
)) {
549 i
< (opsize
- TCPOLEN_SACK_BASE
);
550 i
+= TCPOLEN_SACK_PERBLOCK
) {
551 tmp
= ntohl(*((__be32
*)(ptr
+i
)+1));
553 if (after(tmp
, *sack
))
564 static int tcp_in_window(struct ip_ct_tcp
*state
,
565 enum ip_conntrack_dir dir
,
567 const struct sk_buff
*skb
,
571 struct ip_ct_tcp_state
*sender
= &state
->seen
[dir
];
572 struct ip_ct_tcp_state
*receiver
= &state
->seen
[!dir
];
573 __u32 seq
, ack
, sack
, end
, win
, swin
;
577 * Get the required data from the packet.
579 seq
= ntohl(tcph
->seq
);
580 ack
= sack
= ntohl(tcph
->ack_seq
);
581 win
= ntohs(tcph
->window
);
582 end
= segment_seq_plus_len(seq
, skb
->len
, iph
, tcph
);
584 if (receiver
->flags
& IP_CT_TCP_FLAG_SACK_PERM
)
585 tcp_sack(skb
, iph
, tcph
, &sack
);
587 DEBUGP("tcp_in_window: START\n");
588 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
589 "seq=%u ack=%u sack=%u win=%u end=%u\n",
590 NIPQUAD(iph
->saddr
), ntohs(tcph
->source
),
591 NIPQUAD(iph
->daddr
), ntohs(tcph
->dest
),
592 seq
, ack
, sack
, win
, end
);
593 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
594 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
595 sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
597 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
600 if (sender
->td_end
== 0) {
602 * Initialize sender data.
604 if (tcph
->syn
&& tcph
->ack
) {
606 * Outgoing SYN-ACK in reply to a SYN.
609 sender
->td_maxend
= end
;
610 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
612 tcp_options(skb
, iph
, tcph
, sender
);
615 * Both sides must send the Window Scale option
616 * to enable window scaling in either direction.
618 if (!(sender
->flags
& IP_CT_TCP_FLAG_WINDOW_SCALE
619 && receiver
->flags
& IP_CT_TCP_FLAG_WINDOW_SCALE
))
621 receiver
->td_scale
= 0;
624 * We are in the middle of a connection,
625 * its history is lost for us.
626 * Let's try to use the data from the packet.
628 sender
->td_end
= end
;
629 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
630 sender
->td_maxend
= end
+ sender
->td_maxwin
;
632 } else if (((state
->state
== TCP_CONNTRACK_SYN_SENT
633 && dir
== IP_CT_DIR_ORIGINAL
)
634 || (state
->state
== TCP_CONNTRACK_SYN_RECV
635 && dir
== IP_CT_DIR_REPLY
))
636 && after(end
, sender
->td_end
)) {
638 * RFC 793: "if a TCP is reinitialized ... then it need
639 * not wait at all; it must only be sure to use sequence
640 * numbers larger than those recently used."
643 sender
->td_maxend
= end
;
644 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
646 tcp_options(skb
, iph
, tcph
, sender
);
651 * If there is no ACK, just pretend it was set and OK.
653 ack
= sack
= receiver
->td_end
;
654 } else if (((tcp_flag_word(tcph
) & (TCP_FLAG_ACK
|TCP_FLAG_RST
)) ==
655 (TCP_FLAG_ACK
|TCP_FLAG_RST
))
658 * Broken TCP stacks, that set ACK in RST packets as well
659 * with zero ack value.
661 ack
= sack
= receiver
->td_end
;
666 || (seq
== 0 && state
->state
== TCP_CONNTRACK_SYN_SENT
)))
668 * Packets contains no data: we assume it is valid
669 * and check the ack value only.
670 * However RST segments are always validated by their
671 * SEQ number, except when seq == 0 (reset sent answering
674 seq
= end
= sender
->td_end
;
676 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
677 "seq=%u ack=%u sack =%u win=%u end=%u\n",
678 NIPQUAD(iph
->saddr
), ntohs(tcph
->source
),
679 NIPQUAD(iph
->daddr
), ntohs(tcph
->dest
),
680 seq
, ack
, sack
, win
, end
);
681 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
682 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
683 sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
685 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
688 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
689 before(seq
, sender
->td_maxend
+ 1),
690 after(end
, sender
->td_end
- receiver
->td_maxwin
- 1),
691 before(sack
, receiver
->td_end
+ 1),
692 after(ack
, receiver
->td_end
- MAXACKWINDOW(sender
)));
694 if (before(seq
, sender
->td_maxend
+ 1) &&
695 after(end
, sender
->td_end
- receiver
->td_maxwin
- 1) &&
696 before(sack
, receiver
->td_end
+ 1) &&
697 after(ack
, receiver
->td_end
- MAXACKWINDOW(sender
))) {
699 * Take into account window scaling (RFC 1323).
702 win
<<= sender
->td_scale
;
705 * Update sender data.
707 swin
= win
+ (sack
- ack
);
708 if (sender
->td_maxwin
< swin
)
709 sender
->td_maxwin
= swin
;
710 if (after(end
, sender
->td_end
))
711 sender
->td_end
= end
;
713 * Update receiver data.
715 if (after(end
, sender
->td_maxend
))
716 receiver
->td_maxwin
+= end
- sender
->td_maxend
;
717 if (after(sack
+ win
, receiver
->td_maxend
- 1)) {
718 receiver
->td_maxend
= sack
+ win
;
720 receiver
->td_maxend
++;
724 * Check retransmissions.
726 if (index
== TCP_ACK_SET
) {
727 if (state
->last_dir
== dir
728 && state
->last_seq
== seq
729 && state
->last_ack
== ack
730 && state
->last_end
== end
731 && state
->last_win
== win
)
734 state
->last_dir
= dir
;
735 state
->last_seq
= seq
;
736 state
->last_ack
= ack
;
737 state
->last_end
= end
;
738 state
->last_win
= win
;
745 if (sender
->flags
& IP_CT_TCP_FLAG_BE_LIBERAL
||
746 ip_ct_tcp_be_liberal
)
748 if (!res
&& LOG_INVALID(IPPROTO_TCP
))
749 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
751 before(seq
, sender
->td_maxend
+ 1) ?
752 after(end
, sender
->td_end
- receiver
->td_maxwin
- 1) ?
753 before(sack
, receiver
->td_end
+ 1) ?
754 after(ack
, receiver
->td_end
- MAXACKWINDOW(sender
)) ? "BUG"
755 : "ACK is under the lower bound (possible overly delayed ACK)"
756 : "ACK is over the upper bound (ACKed data not seen yet)"
757 : "SEQ is under the lower bound (already ACKed data retransmitted)"
758 : "SEQ is over the upper bound (over the window of the receiver)");
761 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
762 "receiver end=%u maxend=%u maxwin=%u\n",
763 res
, sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
764 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
);
769 #ifdef CONFIG_IP_NF_NAT_NEEDED
770 /* Update sender->td_end after NAT successfully mangled the packet */
771 void ip_conntrack_tcp_update(struct sk_buff
*skb
,
772 struct ip_conntrack
*conntrack
,
773 enum ip_conntrack_dir dir
)
775 struct iphdr
*iph
= skb
->nh
.iph
;
776 struct tcphdr
*tcph
= (void *)skb
->nh
.iph
+ skb
->nh
.iph
->ihl
*4;
779 struct ip_ct_tcp_state
*sender
= &conntrack
->proto
.tcp
.seen
[dir
];
780 struct ip_ct_tcp_state
*receiver
= &conntrack
->proto
.tcp
.seen
[!dir
];
783 end
= segment_seq_plus_len(ntohl(tcph
->seq
), skb
->len
, iph
, tcph
);
785 write_lock_bh(&tcp_lock
);
787 * We have to worry for the ack in the reply packet only...
789 if (after(end
, conntrack
->proto
.tcp
.seen
[dir
].td_end
))
790 conntrack
->proto
.tcp
.seen
[dir
].td_end
= end
;
791 conntrack
->proto
.tcp
.last_end
= end
;
792 write_unlock_bh(&tcp_lock
);
793 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
794 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
795 sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
797 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
812 /* table of valid flag combinations - ECE and CWR are always valid */
813 static const u8 tcp_valid_flags
[(TH_FIN
|TH_SYN
|TH_RST
|TH_PUSH
|TH_ACK
|TH_URG
) + 1] =
817 [TH_SYN
|TH_PUSH
] = 1,
818 [TH_SYN
|TH_ACK
|TH_PUSH
] = 1,
821 [TH_RST
|TH_ACK
|TH_PUSH
] = 1,
824 [TH_ACK
|TH_PUSH
] = 1,
826 [TH_ACK
|TH_URG
|TH_PUSH
] = 1,
827 [TH_FIN
|TH_ACK
|TH_PUSH
] = 1,
828 [TH_FIN
|TH_ACK
|TH_URG
] = 1,
829 [TH_FIN
|TH_ACK
|TH_URG
|TH_PUSH
] = 1,
832 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
833 static int tcp_error(struct sk_buff
*skb
,
834 enum ip_conntrack_info
*ctinfo
,
835 unsigned int hooknum
)
837 struct iphdr
*iph
= skb
->nh
.iph
;
838 struct tcphdr _tcph
, *th
;
839 unsigned int tcplen
= skb
->len
- iph
->ihl
* 4;
842 /* Smaller that minimal TCP header? */
843 th
= skb_header_pointer(skb
, iph
->ihl
* 4,
844 sizeof(_tcph
), &_tcph
);
846 if (LOG_INVALID(IPPROTO_TCP
))
847 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
848 "ip_ct_tcp: short packet ");
852 /* Not whole TCP header or malformed packet */
853 if (th
->doff
*4 < sizeof(struct tcphdr
) || tcplen
< th
->doff
*4) {
854 if (LOG_INVALID(IPPROTO_TCP
))
855 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
856 "ip_ct_tcp: truncated/malformed packet ");
860 /* Checksum invalid? Ignore.
861 * We skip checking packets on the outgoing path
862 * because it is assumed to be correct.
864 /* FIXME: Source route IP option packets --RR */
865 if (ip_conntrack_checksum
&& hooknum
== NF_IP_PRE_ROUTING
&&
866 nf_ip_checksum(skb
, hooknum
, iph
->ihl
* 4, IPPROTO_TCP
)) {
867 if (LOG_INVALID(IPPROTO_TCP
))
868 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
869 "ip_ct_tcp: bad TCP checksum ");
873 /* Check TCP flags. */
874 tcpflags
= (((u_int8_t
*)th
)[13] & ~(TH_ECE
|TH_CWR
));
875 if (!tcp_valid_flags
[tcpflags
]) {
876 if (LOG_INVALID(IPPROTO_TCP
))
877 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
878 "ip_ct_tcp: invalid TCP flag combination ");
885 /* Returns verdict for packet, or -1 for invalid. */
886 static int tcp_packet(struct ip_conntrack
*conntrack
,
887 const struct sk_buff
*skb
,
888 enum ip_conntrack_info ctinfo
)
890 enum tcp_conntrack new_state
, old_state
;
891 enum ip_conntrack_dir dir
;
892 struct iphdr
*iph
= skb
->nh
.iph
;
893 struct tcphdr
*th
, _tcph
;
894 unsigned long timeout
;
897 th
= skb_header_pointer(skb
, iph
->ihl
* 4,
898 sizeof(_tcph
), &_tcph
);
901 write_lock_bh(&tcp_lock
);
902 old_state
= conntrack
->proto
.tcp
.state
;
903 dir
= CTINFO2DIR(ctinfo
);
904 index
= get_conntrack_index(th
);
905 new_state
= tcp_conntracks
[dir
][index
][old_state
];
908 case TCP_CONNTRACK_IGNORE
:
912 * b) SYN/ACK in REPLY
913 * c) ACK in reply direction after initial SYN in original.
915 if (index
== TCP_SYNACK_SET
916 && conntrack
->proto
.tcp
.last_index
== TCP_SYN_SET
917 && conntrack
->proto
.tcp
.last_dir
!= dir
918 && ntohl(th
->ack_seq
) ==
919 conntrack
->proto
.tcp
.last_end
) {
920 /* This SYN/ACK acknowledges a SYN that we earlier
921 * ignored as invalid. This means that the client and
922 * the server are both in sync, while the firewall is
923 * not. We kill this session and block the SYN/ACK so
924 * that the client cannot but retransmit its SYN and
925 * thus initiate a clean new session.
927 write_unlock_bh(&tcp_lock
);
928 if (LOG_INVALID(IPPROTO_TCP
))
929 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
931 "killing out of sync session ");
932 if (del_timer(&conntrack
->timeout
))
933 conntrack
->timeout
.function((unsigned long)
937 conntrack
->proto
.tcp
.last_index
= index
;
938 conntrack
->proto
.tcp
.last_dir
= dir
;
939 conntrack
->proto
.tcp
.last_seq
= ntohl(th
->seq
);
940 conntrack
->proto
.tcp
.last_end
=
941 segment_seq_plus_len(ntohl(th
->seq
), skb
->len
, iph
, th
);
943 write_unlock_bh(&tcp_lock
);
944 if (LOG_INVALID(IPPROTO_TCP
))
945 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
946 "ip_ct_tcp: invalid packet ignored ");
948 case TCP_CONNTRACK_MAX
:
950 DEBUGP("ip_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
951 dir
, get_conntrack_index(th
),
953 write_unlock_bh(&tcp_lock
);
954 if (LOG_INVALID(IPPROTO_TCP
))
955 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
956 "ip_ct_tcp: invalid state ");
958 case TCP_CONNTRACK_SYN_SENT
:
959 if (old_state
< TCP_CONNTRACK_TIME_WAIT
)
961 if ((conntrack
->proto
.tcp
.seen
[dir
].flags
&
962 IP_CT_TCP_FLAG_CLOSE_INIT
)
963 || after(ntohl(th
->seq
),
964 conntrack
->proto
.tcp
.seen
[dir
].td_end
)) {
965 /* Attempt to reopen a closed connection.
966 * Delete this connection and look up again. */
967 write_unlock_bh(&tcp_lock
);
968 if (del_timer(&conntrack
->timeout
))
969 conntrack
->timeout
.function((unsigned long)
973 write_unlock_bh(&tcp_lock
);
974 if (LOG_INVALID(IPPROTO_TCP
))
975 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
976 NULL
, "ip_ct_tcp: invalid SYN");
979 case TCP_CONNTRACK_CLOSE
:
980 if (index
== TCP_RST_SET
981 && ((test_bit(IPS_SEEN_REPLY_BIT
, &conntrack
->status
)
982 && conntrack
->proto
.tcp
.last_index
== TCP_SYN_SET
)
983 || (!test_bit(IPS_ASSURED_BIT
, &conntrack
->status
)
984 && conntrack
->proto
.tcp
.last_index
== TCP_ACK_SET
))
985 && ntohl(th
->ack_seq
) == conntrack
->proto
.tcp
.last_end
) {
986 /* RST sent to invalid SYN or ACK we had let through
987 * at a) and c) above:
989 * a) SYN was in window then
990 * c) we hold a half-open connection.
992 * Delete our connection entry.
993 * We skip window checking, because packet might ACK
994 * segments we ignored. */
997 /* Just fall through */
999 /* Keep compilers happy. */
1003 if (!tcp_in_window(&conntrack
->proto
.tcp
, dir
, index
,
1005 write_unlock_bh(&tcp_lock
);
1009 /* From now on we have got in-window packets */
1010 conntrack
->proto
.tcp
.last_index
= index
;
1012 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
1013 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1014 NIPQUAD(iph
->saddr
), ntohs(th
->source
),
1015 NIPQUAD(iph
->daddr
), ntohs(th
->dest
),
1016 (th
->syn
? 1 : 0), (th
->ack
? 1 : 0),
1017 (th
->fin
? 1 : 0), (th
->rst
? 1 : 0),
1018 old_state
, new_state
);
1020 conntrack
->proto
.tcp
.state
= new_state
;
1021 if (old_state
!= new_state
1022 && (new_state
== TCP_CONNTRACK_FIN_WAIT
1023 || new_state
== TCP_CONNTRACK_CLOSE
))
1024 conntrack
->proto
.tcp
.seen
[dir
].flags
|= IP_CT_TCP_FLAG_CLOSE_INIT
;
1025 timeout
= conntrack
->proto
.tcp
.retrans
>= ip_ct_tcp_max_retrans
1026 && *tcp_timeouts
[new_state
] > ip_ct_tcp_timeout_max_retrans
1027 ? ip_ct_tcp_timeout_max_retrans
: *tcp_timeouts
[new_state
];
1028 write_unlock_bh(&tcp_lock
);
1030 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE
, skb
);
1031 if (new_state
!= old_state
)
1032 ip_conntrack_event_cache(IPCT_PROTOINFO
, skb
);
1034 if (!test_bit(IPS_SEEN_REPLY_BIT
, &conntrack
->status
)) {
1035 /* If only reply is a RST, we can consider ourselves not to
1036 have an established connection: this is a fairly common
1037 problem case, so we can delete the conntrack
1038 immediately. --RR */
1040 if (del_timer(&conntrack
->timeout
))
1041 conntrack
->timeout
.function((unsigned long)
1045 } else if (!test_bit(IPS_ASSURED_BIT
, &conntrack
->status
)
1046 && (old_state
== TCP_CONNTRACK_SYN_RECV
1047 || old_state
== TCP_CONNTRACK_ESTABLISHED
)
1048 && new_state
== TCP_CONNTRACK_ESTABLISHED
) {
1049 /* Set ASSURED if we see see valid ack in ESTABLISHED
1050 after SYN_RECV or a valid answer for a picked up
1052 set_bit(IPS_ASSURED_BIT
, &conntrack
->status
);
1053 ip_conntrack_event_cache(IPCT_STATUS
, skb
);
1055 ip_ct_refresh_acct(conntrack
, ctinfo
, skb
, timeout
);
1060 /* Called when a new connection for this protocol found. */
1061 static int tcp_new(struct ip_conntrack
*conntrack
,
1062 const struct sk_buff
*skb
)
1064 enum tcp_conntrack new_state
;
1065 struct iphdr
*iph
= skb
->nh
.iph
;
1066 struct tcphdr
*th
, _tcph
;
1068 struct ip_ct_tcp_state
*sender
= &conntrack
->proto
.tcp
.seen
[0];
1069 struct ip_ct_tcp_state
*receiver
= &conntrack
->proto
.tcp
.seen
[1];
1072 th
= skb_header_pointer(skb
, iph
->ihl
* 4,
1073 sizeof(_tcph
), &_tcph
);
1076 /* Don't need lock here: this conntrack not in circulation yet */
1078 = tcp_conntracks
[0][get_conntrack_index(th
)]
1079 [TCP_CONNTRACK_NONE
];
1081 /* Invalid: delete conntrack */
1082 if (new_state
>= TCP_CONNTRACK_MAX
) {
1083 DEBUGP("ip_ct_tcp: invalid new deleting.\n");
1087 if (new_state
== TCP_CONNTRACK_SYN_SENT
) {
1089 conntrack
->proto
.tcp
.seen
[0].td_end
=
1090 segment_seq_plus_len(ntohl(th
->seq
), skb
->len
,
1092 conntrack
->proto
.tcp
.seen
[0].td_maxwin
= ntohs(th
->window
);
1093 if (conntrack
->proto
.tcp
.seen
[0].td_maxwin
== 0)
1094 conntrack
->proto
.tcp
.seen
[0].td_maxwin
= 1;
1095 conntrack
->proto
.tcp
.seen
[0].td_maxend
=
1096 conntrack
->proto
.tcp
.seen
[0].td_end
;
1098 tcp_options(skb
, iph
, th
, &conntrack
->proto
.tcp
.seen
[0]);
1099 conntrack
->proto
.tcp
.seen
[1].flags
= 0;
1100 } else if (ip_ct_tcp_loose
== 0) {
1101 /* Don't try to pick up connections. */
1105 * We are in the middle of a connection,
1106 * its history is lost for us.
1107 * Let's try to use the data from the packet.
1109 conntrack
->proto
.tcp
.seen
[0].td_end
=
1110 segment_seq_plus_len(ntohl(th
->seq
), skb
->len
,
1112 conntrack
->proto
.tcp
.seen
[0].td_maxwin
= ntohs(th
->window
);
1113 if (conntrack
->proto
.tcp
.seen
[0].td_maxwin
== 0)
1114 conntrack
->proto
.tcp
.seen
[0].td_maxwin
= 1;
1115 conntrack
->proto
.tcp
.seen
[0].td_maxend
=
1116 conntrack
->proto
.tcp
.seen
[0].td_end
+
1117 conntrack
->proto
.tcp
.seen
[0].td_maxwin
;
1118 conntrack
->proto
.tcp
.seen
[0].td_scale
= 0;
1120 /* We assume SACK and liberal window checking to handle
1122 conntrack
->proto
.tcp
.seen
[0].flags
=
1123 conntrack
->proto
.tcp
.seen
[1].flags
= IP_CT_TCP_FLAG_SACK_PERM
|
1124 IP_CT_TCP_FLAG_BE_LIBERAL
;
1127 conntrack
->proto
.tcp
.seen
[1].td_end
= 0;
1128 conntrack
->proto
.tcp
.seen
[1].td_maxend
= 0;
1129 conntrack
->proto
.tcp
.seen
[1].td_maxwin
= 1;
1130 conntrack
->proto
.tcp
.seen
[1].td_scale
= 0;
1132 /* tcp_packet will set them */
1133 conntrack
->proto
.tcp
.state
= TCP_CONNTRACK_NONE
;
1134 conntrack
->proto
.tcp
.last_index
= TCP_NONE_SET
;
1136 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1137 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1138 sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
1140 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
1141 receiver
->td_scale
);
1145 struct ip_conntrack_protocol ip_conntrack_protocol_tcp
=
1147 .proto
= IPPROTO_TCP
,
1149 .pkt_to_tuple
= tcp_pkt_to_tuple
,
1150 .invert_tuple
= tcp_invert_tuple
,
1151 .print_tuple
= tcp_print_tuple
,
1152 .print_conntrack
= tcp_print_conntrack
,
1153 .packet
= tcp_packet
,
1156 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
1157 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
1158 .to_nfattr
= tcp_to_nfattr
,
1159 .from_nfattr
= nfattr_to_tcp
,
1160 .tuple_to_nfattr
= ip_ct_port_tuple_to_nfattr
,
1161 .nfattr_to_tuple
= ip_ct_port_nfattr_to_tuple
,