netfilter: nf_conntrack_tcp: fix endless loop
[linux-2.6/mini2440.git] / net / netfilter / nf_conntrack_proto_tcp.c
blobdd28fb239a60438b28ed3c90b9f5d979c8c6b700
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.
7 */
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.h>
12 #include <linux/in.h>
13 #include <linux/tcp.h>
14 #include <linux/spinlock.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <net/ip6_checksum.h>
19 #include <net/tcp.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_ecache.h>
27 #include <net/netfilter/nf_log.h>
29 /* Protects ct->proto.tcp */
30 static DEFINE_RWLOCK(tcp_lock);
32 /* "Be conservative in what you do,
33 be liberal in what you accept from others."
34 If it's non-zero, we mark only out of window RST segments as INVALID. */
35 static int nf_ct_tcp_be_liberal __read_mostly = 0;
37 /* If it is set to zero, we disable picking up already established
38 connections. */
39 static int nf_ct_tcp_loose __read_mostly = 1;
41 /* Max number of the retransmitted packets without receiving an (acceptable)
42 ACK from the destination. If this number is reached, a shorter timer
43 will be started. */
44 static int nf_ct_tcp_max_retrans __read_mostly = 3;
46 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
47 closely. They're more complex. --RR */
49 static const char *const tcp_conntrack_names[] = {
50 "NONE",
51 "SYN_SENT",
52 "SYN_RECV",
53 "ESTABLISHED",
54 "FIN_WAIT",
55 "CLOSE_WAIT",
56 "LAST_ACK",
57 "TIME_WAIT",
58 "CLOSE",
59 "LISTEN"
62 #define SECS * HZ
63 #define MINS * 60 SECS
64 #define HOURS * 60 MINS
65 #define DAYS * 24 HOURS
67 /* RFC1122 says the R2 limit should be at least 100 seconds.
68 Linux uses 15 packets as limit, which corresponds
69 to ~13-30min depending on RTO. */
70 static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
72 static unsigned int tcp_timeouts[TCP_CONNTRACK_MAX] __read_mostly = {
73 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
74 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
75 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
76 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
77 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
78 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
79 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
80 [TCP_CONNTRACK_CLOSE] = 10 SECS,
83 #define sNO TCP_CONNTRACK_NONE
84 #define sSS TCP_CONNTRACK_SYN_SENT
85 #define sSR TCP_CONNTRACK_SYN_RECV
86 #define sES TCP_CONNTRACK_ESTABLISHED
87 #define sFW TCP_CONNTRACK_FIN_WAIT
88 #define sCW TCP_CONNTRACK_CLOSE_WAIT
89 #define sLA TCP_CONNTRACK_LAST_ACK
90 #define sTW TCP_CONNTRACK_TIME_WAIT
91 #define sCL TCP_CONNTRACK_CLOSE
92 #define sLI TCP_CONNTRACK_LISTEN
93 #define sIV TCP_CONNTRACK_MAX
94 #define sIG TCP_CONNTRACK_IGNORE
96 /* What TCP flags are set from RST/SYN/FIN/ACK. */
97 enum tcp_bit_set {
98 TCP_SYN_SET,
99 TCP_SYNACK_SET,
100 TCP_FIN_SET,
101 TCP_ACK_SET,
102 TCP_RST_SET,
103 TCP_NONE_SET,
107 * The TCP state transition table needs a few words...
109 * We are the man in the middle. All the packets go through us
110 * but might get lost in transit to the destination.
111 * It is assumed that the destinations can't receive segments
112 * we haven't seen.
114 * The checked segment is in window, but our windows are *not*
115 * equivalent with the ones of the sender/receiver. We always
116 * try to guess the state of the current sender.
118 * The meaning of the states are:
120 * NONE: initial state
121 * SYN_SENT: SYN-only packet seen
122 * SYN_RECV: SYN-ACK packet seen
123 * ESTABLISHED: ACK packet seen
124 * FIN_WAIT: FIN packet seen
125 * CLOSE_WAIT: ACK seen (after FIN)
126 * LAST_ACK: FIN seen (after FIN)
127 * TIME_WAIT: last ACK seen
128 * CLOSE: closed connection (RST)
130 * LISTEN state is not used.
132 * Packets marked as IGNORED (sIG):
133 * if they may be either invalid or valid
134 * and the receiver may send back a connection
135 * closing RST or a SYN/ACK.
137 * Packets marked as INVALID (sIV):
138 * if they are invalid
139 * or we do not support the request (simultaneous open)
141 static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
143 /* ORIGINAL */
144 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
145 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
147 * sNO -> sSS Initialize a new connection
148 * sSS -> sSS Retransmitted SYN
149 * sSR -> sIG Late retransmitted SYN?
150 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
151 * are errors. Receiver will reply with RST
152 * and close the connection.
153 * Or we are not in sync and hold a dead connection.
154 * sFW -> sIG
155 * sCW -> sIG
156 * sLA -> sIG
157 * sTW -> sSS Reopened connection (RFC 1122).
158 * sCL -> sSS
160 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
161 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
163 * A SYN/ACK from the client is always invalid:
164 * - either it tries to set up a simultaneous open, which is
165 * not supported;
166 * - or the firewall has just been inserted between the two hosts
167 * during the session set-up. The SYN will be retransmitted
168 * by the true client (or it'll time out).
170 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
171 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
173 * sNO -> sIV Too late and no reason to do anything...
174 * sSS -> sIV Client migth not send FIN in this state:
175 * we enforce waiting for a SYN/ACK reply first.
176 * sSR -> sFW Close started.
177 * sES -> sFW
178 * sFW -> sLA FIN seen in both directions, waiting for
179 * the last ACK.
180 * Migth be a retransmitted FIN as well...
181 * sCW -> sLA
182 * sLA -> sLA Retransmitted FIN. Remain in the same state.
183 * sTW -> sTW
184 * sCL -> sCL
186 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
187 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
189 * sNO -> sES Assumed.
190 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
191 * sSR -> sES Established state is reached.
192 * sES -> sES :-)
193 * sFW -> sCW Normal close request answered by ACK.
194 * sCW -> sCW
195 * sLA -> sTW Last ACK detected.
196 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
197 * sCL -> sCL
199 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
200 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
201 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
204 /* REPLY */
205 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
206 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
208 * sNO -> sIV Never reached.
209 * sSS -> sIV Simultaneous open, not supported
210 * sSR -> sIV Simultaneous open, not supported.
211 * sES -> sIV Server may not initiate a connection.
212 * sFW -> sIV
213 * sCW -> sIV
214 * sLA -> sIV
215 * sTW -> sIV Reopened connection, but server may not do it.
216 * sCL -> sIV
218 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
219 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
221 * sSS -> sSR Standard open.
222 * sSR -> sSR Retransmitted SYN/ACK.
223 * sES -> sIG Late retransmitted SYN/ACK?
224 * sFW -> sIG Might be SYN/ACK answering ignored SYN
225 * sCW -> sIG
226 * sLA -> sIG
227 * sTW -> sIG
228 * sCL -> sIG
230 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
231 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
233 * sSS -> sIV Server might not send FIN in this state.
234 * sSR -> sFW Close started.
235 * sES -> sFW
236 * sFW -> sLA FIN seen in both directions.
237 * sCW -> sLA
238 * sLA -> sLA Retransmitted FIN.
239 * sTW -> sTW
240 * sCL -> sCL
242 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
243 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
245 * sSS -> sIG Might be a half-open connection.
246 * sSR -> sSR Might answer late resent SYN.
247 * sES -> sES :-)
248 * sFW -> sCW Normal close request answered by ACK.
249 * sCW -> sCW
250 * sLA -> sTW Last ACK detected.
251 * sTW -> sTW Retransmitted last ACK.
252 * sCL -> sCL
254 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
255 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
256 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
260 static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
261 struct nf_conntrack_tuple *tuple)
263 const struct tcphdr *hp;
264 struct tcphdr _hdr;
266 /* Actually only need first 8 bytes. */
267 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
268 if (hp == NULL)
269 return false;
271 tuple->src.u.tcp.port = hp->source;
272 tuple->dst.u.tcp.port = hp->dest;
274 return true;
277 static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
278 const struct nf_conntrack_tuple *orig)
280 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
281 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
282 return true;
285 /* Print out the per-protocol part of the tuple. */
286 static int tcp_print_tuple(struct seq_file *s,
287 const struct nf_conntrack_tuple *tuple)
289 return seq_printf(s, "sport=%hu dport=%hu ",
290 ntohs(tuple->src.u.tcp.port),
291 ntohs(tuple->dst.u.tcp.port));
294 /* Print out the private part of the conntrack. */
295 static int tcp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
297 enum tcp_conntrack state;
299 read_lock_bh(&tcp_lock);
300 state = ct->proto.tcp.state;
301 read_unlock_bh(&tcp_lock);
303 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
306 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
308 if (tcph->rst) return TCP_RST_SET;
309 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
310 else if (tcph->fin) return TCP_FIN_SET;
311 else if (tcph->ack) return TCP_ACK_SET;
312 else return TCP_NONE_SET;
315 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
316 in IP Filter' by Guido van Rooij.
318 http://www.nluug.nl/events/sane2000/papers.html
319 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
321 The boundaries and the conditions are changed according to RFC793:
322 the packet must intersect the window (i.e. segments may be
323 after the right or before the left edge) and thus receivers may ACK
324 segments after the right edge of the window.
326 td_maxend = max(sack + max(win,1)) seen in reply packets
327 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
328 td_maxwin += seq + len - sender.td_maxend
329 if seq + len > sender.td_maxend
330 td_end = max(seq + len) seen in sent packets
332 I. Upper bound for valid data: seq <= sender.td_maxend
333 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
334 III. Upper bound for valid (s)ack: sack <= receiver.td_end
335 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
337 where sack is the highest right edge of sack block found in the packet
338 or ack in the case of packet without SACK option.
340 The upper bound limit for a valid (s)ack is not ignored -
341 we doesn't have to deal with fragments.
344 static inline __u32 segment_seq_plus_len(__u32 seq,
345 size_t len,
346 unsigned int dataoff,
347 const struct tcphdr *tcph)
349 /* XXX Should I use payload length field in IP/IPv6 header ?
350 * - YK */
351 return (seq + len - dataoff - tcph->doff*4
352 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
355 /* Fixme: what about big packets? */
356 #define MAXACKWINCONST 66000
357 #define MAXACKWINDOW(sender) \
358 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
359 : MAXACKWINCONST)
362 * Simplified tcp_parse_options routine from tcp_input.c
364 static void tcp_options(const struct sk_buff *skb,
365 unsigned int dataoff,
366 const struct tcphdr *tcph,
367 struct ip_ct_tcp_state *state)
369 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
370 const unsigned char *ptr;
371 int length = (tcph->doff*4) - sizeof(struct tcphdr);
373 if (!length)
374 return;
376 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
377 length, buff);
378 BUG_ON(ptr == NULL);
380 state->td_scale =
381 state->flags = 0;
383 while (length > 0) {
384 int opcode=*ptr++;
385 int opsize;
387 switch (opcode) {
388 case TCPOPT_EOL:
389 return;
390 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
391 length--;
392 continue;
393 default:
394 opsize=*ptr++;
395 if (opsize < 2) /* "silly options" */
396 return;
397 if (opsize > length)
398 break; /* don't parse partial options */
400 if (opcode == TCPOPT_SACK_PERM
401 && opsize == TCPOLEN_SACK_PERM)
402 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
403 else if (opcode == TCPOPT_WINDOW
404 && opsize == TCPOLEN_WINDOW) {
405 state->td_scale = *(u_int8_t *)ptr;
407 if (state->td_scale > 14) {
408 /* See RFC1323 */
409 state->td_scale = 14;
411 state->flags |=
412 IP_CT_TCP_FLAG_WINDOW_SCALE;
414 ptr += opsize - 2;
415 length -= opsize;
420 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
421 const struct tcphdr *tcph, __u32 *sack)
423 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
424 const unsigned char *ptr;
425 int length = (tcph->doff*4) - sizeof(struct tcphdr);
426 __u32 tmp;
428 if (!length)
429 return;
431 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
432 length, buff);
433 BUG_ON(ptr == NULL);
435 /* Fast path for timestamp-only option */
436 if (length == TCPOLEN_TSTAMP_ALIGNED*4
437 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
438 | (TCPOPT_NOP << 16)
439 | (TCPOPT_TIMESTAMP << 8)
440 | TCPOLEN_TIMESTAMP))
441 return;
443 while (length > 0) {
444 int opcode = *ptr++;
445 int opsize, i;
447 switch (opcode) {
448 case TCPOPT_EOL:
449 return;
450 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
451 length--;
452 continue;
453 default:
454 opsize = *ptr++;
455 if (opsize < 2) /* "silly options" */
456 return;
457 if (opsize > length)
458 break; /* don't parse partial options */
460 if (opcode == TCPOPT_SACK
461 && opsize >= (TCPOLEN_SACK_BASE
462 + TCPOLEN_SACK_PERBLOCK)
463 && !((opsize - TCPOLEN_SACK_BASE)
464 % TCPOLEN_SACK_PERBLOCK)) {
465 for (i = 0;
466 i < (opsize - TCPOLEN_SACK_BASE);
467 i += TCPOLEN_SACK_PERBLOCK) {
468 tmp = ntohl(*((__be32 *)(ptr+i)+1));
470 if (after(tmp, *sack))
471 *sack = tmp;
473 return;
475 ptr += opsize - 2;
476 length -= opsize;
481 static bool tcp_in_window(const struct nf_conn *ct,
482 struct ip_ct_tcp *state,
483 enum ip_conntrack_dir dir,
484 unsigned int index,
485 const struct sk_buff *skb,
486 unsigned int dataoff,
487 const struct tcphdr *tcph,
488 int pf)
490 struct ip_ct_tcp_state *sender = &state->seen[dir];
491 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
492 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
493 __u32 seq, ack, sack, end, win, swin;
494 bool res;
497 * Get the required data from the packet.
499 seq = ntohl(tcph->seq);
500 ack = sack = ntohl(tcph->ack_seq);
501 win = ntohs(tcph->window);
502 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
504 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
505 tcp_sack(skb, dataoff, tcph, &sack);
507 pr_debug("tcp_in_window: START\n");
508 pr_debug("tcp_in_window: ");
509 nf_ct_dump_tuple(tuple);
510 pr_debug("seq=%u ack=%u sack=%u win=%u end=%u\n",
511 seq, ack, sack, win, end);
512 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
513 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
514 sender->td_end, sender->td_maxend, sender->td_maxwin,
515 sender->td_scale,
516 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
517 receiver->td_scale);
519 if (sender->td_end == 0) {
521 * Initialize sender data.
523 if (tcph->syn && tcph->ack) {
525 * Outgoing SYN-ACK in reply to a SYN.
527 sender->td_end =
528 sender->td_maxend = end;
529 sender->td_maxwin = (win == 0 ? 1 : win);
531 tcp_options(skb, dataoff, tcph, sender);
533 * RFC 1323:
534 * Both sides must send the Window Scale option
535 * to enable window scaling in either direction.
537 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
538 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
539 sender->td_scale =
540 receiver->td_scale = 0;
541 } else {
543 * We are in the middle of a connection,
544 * its history is lost for us.
545 * Let's try to use the data from the packet.
547 sender->td_end = end;
548 sender->td_maxwin = (win == 0 ? 1 : win);
549 sender->td_maxend = end + sender->td_maxwin;
551 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
552 && dir == IP_CT_DIR_ORIGINAL)
553 || (state->state == TCP_CONNTRACK_SYN_RECV
554 && dir == IP_CT_DIR_REPLY))
555 && after(end, sender->td_end)) {
557 * RFC 793: "if a TCP is reinitialized ... then it need
558 * not wait at all; it must only be sure to use sequence
559 * numbers larger than those recently used."
561 sender->td_end =
562 sender->td_maxend = end;
563 sender->td_maxwin = (win == 0 ? 1 : win);
565 tcp_options(skb, dataoff, tcph, sender);
568 if (!(tcph->ack)) {
570 * If there is no ACK, just pretend it was set and OK.
572 ack = sack = receiver->td_end;
573 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
574 (TCP_FLAG_ACK|TCP_FLAG_RST))
575 && (ack == 0)) {
577 * Broken TCP stacks, that set ACK in RST packets as well
578 * with zero ack value.
580 ack = sack = receiver->td_end;
583 if (seq == end
584 && (!tcph->rst
585 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
587 * Packets contains no data: we assume it is valid
588 * and check the ack value only.
589 * However RST segments are always validated by their
590 * SEQ number, except when seq == 0 (reset sent answering
591 * SYN.
593 seq = end = sender->td_end;
595 pr_debug("tcp_in_window: ");
596 nf_ct_dump_tuple(tuple);
597 pr_debug("seq=%u ack=%u sack =%u win=%u end=%u\n",
598 seq, ack, sack, win, end);
599 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
600 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
601 sender->td_end, sender->td_maxend, sender->td_maxwin,
602 sender->td_scale,
603 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
604 receiver->td_scale);
606 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
607 before(seq, sender->td_maxend + 1),
608 after(end, sender->td_end - receiver->td_maxwin - 1),
609 before(sack, receiver->td_end + 1),
610 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
612 if (before(seq, sender->td_maxend + 1) &&
613 after(end, sender->td_end - receiver->td_maxwin - 1) &&
614 before(sack, receiver->td_end + 1) &&
615 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
617 * Take into account window scaling (RFC 1323).
619 if (!tcph->syn)
620 win <<= sender->td_scale;
623 * Update sender data.
625 swin = win + (sack - ack);
626 if (sender->td_maxwin < swin)
627 sender->td_maxwin = swin;
628 if (after(end, sender->td_end))
629 sender->td_end = end;
631 * Update receiver data.
633 if (after(end, sender->td_maxend))
634 receiver->td_maxwin += end - sender->td_maxend;
635 if (after(sack + win, receiver->td_maxend - 1)) {
636 receiver->td_maxend = sack + win;
637 if (win == 0)
638 receiver->td_maxend++;
642 * Check retransmissions.
644 if (index == TCP_ACK_SET) {
645 if (state->last_dir == dir
646 && state->last_seq == seq
647 && state->last_ack == ack
648 && state->last_end == end
649 && state->last_win == win)
650 state->retrans++;
651 else {
652 state->last_dir = dir;
653 state->last_seq = seq;
654 state->last_ack = ack;
655 state->last_end = end;
656 state->last_win = win;
657 state->retrans = 0;
660 res = true;
661 } else {
662 res = false;
663 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
664 nf_ct_tcp_be_liberal)
665 res = true;
666 if (!res && LOG_INVALID(IPPROTO_TCP))
667 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
668 "nf_ct_tcp: %s ",
669 before(seq, sender->td_maxend + 1) ?
670 after(end, sender->td_end - receiver->td_maxwin - 1) ?
671 before(sack, receiver->td_end + 1) ?
672 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
673 : "ACK is under the lower bound (possible overly delayed ACK)"
674 : "ACK is over the upper bound (ACKed data not seen yet)"
675 : "SEQ is under the lower bound (already ACKed data retransmitted)"
676 : "SEQ is over the upper bound (over the window of the receiver)");
679 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
680 "receiver end=%u maxend=%u maxwin=%u\n",
681 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
682 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
684 return res;
687 #ifdef CONFIG_NF_NAT_NEEDED
688 /* Update sender->td_end after NAT successfully mangled the packet */
689 /* Caller must linearize skb at tcp header. */
690 void nf_conntrack_tcp_update(const struct sk_buff *skb,
691 unsigned int dataoff,
692 struct nf_conn *ct,
693 int dir)
695 const struct tcphdr *tcph = (const void *)skb->data + dataoff;
696 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[dir];
697 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[!dir];
698 __u32 end;
700 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
702 write_lock_bh(&tcp_lock);
704 * We have to worry for the ack in the reply packet only...
706 if (after(end, ct->proto.tcp.seen[dir].td_end))
707 ct->proto.tcp.seen[dir].td_end = end;
708 ct->proto.tcp.last_end = end;
709 write_unlock_bh(&tcp_lock);
710 pr_debug("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
711 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
712 sender->td_end, sender->td_maxend, sender->td_maxwin,
713 sender->td_scale,
714 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
715 receiver->td_scale);
717 EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
718 #endif
720 #define TH_FIN 0x01
721 #define TH_SYN 0x02
722 #define TH_RST 0x04
723 #define TH_PUSH 0x08
724 #define TH_ACK 0x10
725 #define TH_URG 0x20
726 #define TH_ECE 0x40
727 #define TH_CWR 0x80
729 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
730 static const u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
732 [TH_SYN] = 1,
733 [TH_SYN|TH_URG] = 1,
734 [TH_SYN|TH_ACK] = 1,
735 [TH_RST] = 1,
736 [TH_RST|TH_ACK] = 1,
737 [TH_FIN|TH_ACK] = 1,
738 [TH_FIN|TH_ACK|TH_URG] = 1,
739 [TH_ACK] = 1,
740 [TH_ACK|TH_URG] = 1,
743 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
744 static int tcp_error(struct sk_buff *skb,
745 unsigned int dataoff,
746 enum ip_conntrack_info *ctinfo,
747 int pf,
748 unsigned int hooknum)
750 const struct tcphdr *th;
751 struct tcphdr _tcph;
752 unsigned int tcplen = skb->len - dataoff;
753 u_int8_t tcpflags;
755 /* Smaller that minimal TCP header? */
756 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
757 if (th == NULL) {
758 if (LOG_INVALID(IPPROTO_TCP))
759 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
760 "nf_ct_tcp: short packet ");
761 return -NF_ACCEPT;
764 /* Not whole TCP header or malformed packet */
765 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
766 if (LOG_INVALID(IPPROTO_TCP))
767 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
768 "nf_ct_tcp: truncated/malformed packet ");
769 return -NF_ACCEPT;
772 /* Checksum invalid? Ignore.
773 * We skip checking packets on the outgoing path
774 * because the checksum is assumed to be correct.
776 /* FIXME: Source route IP option packets --RR */
777 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
778 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
779 if (LOG_INVALID(IPPROTO_TCP))
780 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
781 "nf_ct_tcp: bad TCP checksum ");
782 return -NF_ACCEPT;
785 /* Check TCP flags. */
786 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
787 if (!tcp_valid_flags[tcpflags]) {
788 if (LOG_INVALID(IPPROTO_TCP))
789 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
790 "nf_ct_tcp: invalid TCP flag combination ");
791 return -NF_ACCEPT;
794 return NF_ACCEPT;
797 /* Returns verdict for packet, or -1 for invalid. */
798 static int tcp_packet(struct nf_conn *ct,
799 const struct sk_buff *skb,
800 unsigned int dataoff,
801 enum ip_conntrack_info ctinfo,
802 int pf,
803 unsigned int hooknum)
805 struct nf_conntrack_tuple *tuple;
806 enum tcp_conntrack new_state, old_state;
807 enum ip_conntrack_dir dir;
808 const struct tcphdr *th;
809 struct tcphdr _tcph;
810 unsigned long timeout;
811 unsigned int index;
813 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
814 BUG_ON(th == NULL);
816 write_lock_bh(&tcp_lock);
817 old_state = ct->proto.tcp.state;
818 dir = CTINFO2DIR(ctinfo);
819 index = get_conntrack_index(th);
820 new_state = tcp_conntracks[dir][index][old_state];
821 tuple = &ct->tuplehash[dir].tuple;
823 switch (new_state) {
824 case TCP_CONNTRACK_SYN_SENT:
825 if (old_state < TCP_CONNTRACK_TIME_WAIT)
826 break;
827 /* RFC 1122: "When a connection is closed actively,
828 * it MUST linger in TIME-WAIT state for a time 2xMSL
829 * (Maximum Segment Lifetime). However, it MAY accept
830 * a new SYN from the remote TCP to reopen the connection
831 * directly from TIME-WAIT state, if..."
832 * We ignore the conditions because we are in the
833 * TIME-WAIT state anyway.
835 * Handle aborted connections: we and the server
836 * think there is an existing connection but the client
837 * aborts it and starts a new one.
839 if (((ct->proto.tcp.seen[dir].flags
840 | ct->proto.tcp.seen[!dir].flags)
841 & IP_CT_TCP_FLAG_CLOSE_INIT)
842 || (ct->proto.tcp.last_dir == dir
843 && ct->proto.tcp.last_index == TCP_RST_SET)) {
844 /* Attempt to reopen a closed/aborted connection.
845 * Delete this connection and look up again. */
846 write_unlock_bh(&tcp_lock);
847 /* Only repeat if we can actually remove the timer.
848 * Destruction may already be in progress in process
849 * context and we must give it a chance to terminate.
851 if (del_timer(&ct->timeout)) {
852 ct->timeout.function((unsigned long)ct);
853 return -NF_REPEAT;
855 return -NF_DROP;
857 /* Fall through */
858 case TCP_CONNTRACK_IGNORE:
859 /* Ignored packets:
861 * Our connection entry may be out of sync, so ignore
862 * packets which may signal the real connection between
863 * the client and the server.
865 * a) SYN in ORIGINAL
866 * b) SYN/ACK in REPLY
867 * c) ACK in reply direction after initial SYN in original.
869 * If the ignored packet is invalid, the receiver will send
870 * a RST we'll catch below.
872 if (index == TCP_SYNACK_SET
873 && ct->proto.tcp.last_index == TCP_SYN_SET
874 && ct->proto.tcp.last_dir != dir
875 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
876 /* b) This SYN/ACK acknowledges a SYN that we earlier
877 * ignored as invalid. This means that the client and
878 * the server are both in sync, while the firewall is
879 * not. We kill this session and block the SYN/ACK so
880 * that the client cannot but retransmit its SYN and
881 * thus initiate a clean new session.
883 write_unlock_bh(&tcp_lock);
884 if (LOG_INVALID(IPPROTO_TCP))
885 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
886 "nf_ct_tcp: killing out of sync session ");
887 if (del_timer(&ct->timeout))
888 ct->timeout.function((unsigned long)ct);
889 return -NF_DROP;
891 ct->proto.tcp.last_index = index;
892 ct->proto.tcp.last_dir = dir;
893 ct->proto.tcp.last_seq = ntohl(th->seq);
894 ct->proto.tcp.last_end =
895 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
897 write_unlock_bh(&tcp_lock);
898 if (LOG_INVALID(IPPROTO_TCP))
899 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
900 "nf_ct_tcp: invalid packet ignored ");
901 return NF_ACCEPT;
902 case TCP_CONNTRACK_MAX:
903 /* Invalid packet */
904 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
905 dir, get_conntrack_index(th), old_state);
906 write_unlock_bh(&tcp_lock);
907 if (LOG_INVALID(IPPROTO_TCP))
908 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
909 "nf_ct_tcp: invalid state ");
910 return -NF_ACCEPT;
911 case TCP_CONNTRACK_CLOSE:
912 if (index == TCP_RST_SET
913 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
914 && ct->proto.tcp.last_index == TCP_SYN_SET)
915 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
916 && ct->proto.tcp.last_index == TCP_ACK_SET))
917 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
918 /* RST sent to invalid SYN or ACK we had let through
919 * at a) and c) above:
921 * a) SYN was in window then
922 * c) we hold a half-open connection.
924 * Delete our connection entry.
925 * We skip window checking, because packet might ACK
926 * segments we ignored. */
927 goto in_window;
929 /* Just fall through */
930 default:
931 /* Keep compilers happy. */
932 break;
935 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
936 skb, dataoff, th, pf)) {
937 write_unlock_bh(&tcp_lock);
938 return -NF_ACCEPT;
940 in_window:
941 /* From now on we have got in-window packets */
942 ct->proto.tcp.last_index = index;
943 ct->proto.tcp.last_dir = dir;
945 pr_debug("tcp_conntracks: ");
946 nf_ct_dump_tuple(tuple);
947 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
948 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
949 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
950 old_state, new_state);
952 ct->proto.tcp.state = new_state;
953 if (old_state != new_state
954 && new_state == TCP_CONNTRACK_FIN_WAIT)
955 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
956 timeout = ct->proto.tcp.retrans >= nf_ct_tcp_max_retrans
957 && tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans
958 ? nf_ct_tcp_timeout_max_retrans : tcp_timeouts[new_state];
959 write_unlock_bh(&tcp_lock);
961 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
962 if (new_state != old_state)
963 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
965 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
966 /* If only reply is a RST, we can consider ourselves not to
967 have an established connection: this is a fairly common
968 problem case, so we can delete the conntrack
969 immediately. --RR */
970 if (th->rst) {
971 if (del_timer(&ct->timeout))
972 ct->timeout.function((unsigned long)ct);
973 return NF_ACCEPT;
975 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
976 && (old_state == TCP_CONNTRACK_SYN_RECV
977 || old_state == TCP_CONNTRACK_ESTABLISHED)
978 && new_state == TCP_CONNTRACK_ESTABLISHED) {
979 /* Set ASSURED if we see see valid ack in ESTABLISHED
980 after SYN_RECV or a valid answer for a picked up
981 connection. */
982 set_bit(IPS_ASSURED_BIT, &ct->status);
983 nf_conntrack_event_cache(IPCT_STATUS, skb);
985 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
987 return NF_ACCEPT;
990 /* Called when a new connection for this protocol found. */
991 static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
992 unsigned int dataoff)
994 enum tcp_conntrack new_state;
995 const struct tcphdr *th;
996 struct tcphdr _tcph;
997 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
998 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1000 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1001 BUG_ON(th == NULL);
1003 /* Don't need lock here: this conntrack not in circulation yet */
1004 new_state
1005 = tcp_conntracks[0][get_conntrack_index(th)]
1006 [TCP_CONNTRACK_NONE];
1008 /* Invalid: delete conntrack */
1009 if (new_state >= TCP_CONNTRACK_MAX) {
1010 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1011 return false;
1014 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1015 /* SYN packet */
1016 ct->proto.tcp.seen[0].td_end =
1017 segment_seq_plus_len(ntohl(th->seq), skb->len,
1018 dataoff, th);
1019 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1020 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1021 ct->proto.tcp.seen[0].td_maxwin = 1;
1022 ct->proto.tcp.seen[0].td_maxend =
1023 ct->proto.tcp.seen[0].td_end;
1025 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1026 ct->proto.tcp.seen[1].flags = 0;
1027 } else if (nf_ct_tcp_loose == 0) {
1028 /* Don't try to pick up connections. */
1029 return false;
1030 } else {
1032 * We are in the middle of a connection,
1033 * its history is lost for us.
1034 * Let's try to use the data from the packet.
1036 ct->proto.tcp.seen[0].td_end =
1037 segment_seq_plus_len(ntohl(th->seq), skb->len,
1038 dataoff, th);
1039 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1040 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1041 ct->proto.tcp.seen[0].td_maxwin = 1;
1042 ct->proto.tcp.seen[0].td_maxend =
1043 ct->proto.tcp.seen[0].td_end +
1044 ct->proto.tcp.seen[0].td_maxwin;
1045 ct->proto.tcp.seen[0].td_scale = 0;
1047 /* We assume SACK and liberal window checking to handle
1048 * window scaling */
1049 ct->proto.tcp.seen[0].flags =
1050 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1051 IP_CT_TCP_FLAG_BE_LIBERAL;
1054 ct->proto.tcp.seen[1].td_end = 0;
1055 ct->proto.tcp.seen[1].td_maxend = 0;
1056 ct->proto.tcp.seen[1].td_maxwin = 1;
1057 ct->proto.tcp.seen[1].td_scale = 0;
1059 /* tcp_packet will set them */
1060 ct->proto.tcp.state = TCP_CONNTRACK_NONE;
1061 ct->proto.tcp.last_index = TCP_NONE_SET;
1063 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1064 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1065 sender->td_end, sender->td_maxend, sender->td_maxwin,
1066 sender->td_scale,
1067 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1068 receiver->td_scale);
1069 return true;
1072 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1074 #include <linux/netfilter/nfnetlink.h>
1075 #include <linux/netfilter/nfnetlink_conntrack.h>
1077 static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1078 const struct nf_conn *ct)
1080 struct nlattr *nest_parms;
1081 struct nf_ct_tcp_flags tmp = {};
1083 read_lock_bh(&tcp_lock);
1084 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1085 if (!nest_parms)
1086 goto nla_put_failure;
1088 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state);
1090 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1091 ct->proto.tcp.seen[0].td_scale);
1093 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1094 ct->proto.tcp.seen[1].td_scale);
1096 tmp.flags = ct->proto.tcp.seen[0].flags;
1097 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1098 sizeof(struct nf_ct_tcp_flags), &tmp);
1100 tmp.flags = ct->proto.tcp.seen[1].flags;
1101 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1102 sizeof(struct nf_ct_tcp_flags), &tmp);
1103 read_unlock_bh(&tcp_lock);
1105 nla_nest_end(skb, nest_parms);
1107 return 0;
1109 nla_put_failure:
1110 read_unlock_bh(&tcp_lock);
1111 return -1;
1114 static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1115 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1116 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1117 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1118 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1119 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
1122 static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1124 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1125 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1126 int err;
1128 /* updates could not contain anything about the private
1129 * protocol info, in that case skip the parsing */
1130 if (!pattr)
1131 return 0;
1133 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
1134 if (err < 0)
1135 return err;
1137 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1138 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1139 return -EINVAL;
1141 write_lock_bh(&tcp_lock);
1142 if (tb[CTA_PROTOINFO_TCP_STATE])
1143 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1145 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1146 struct nf_ct_tcp_flags *attr =
1147 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1148 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1149 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1152 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1153 struct nf_ct_tcp_flags *attr =
1154 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1155 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1156 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1159 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1160 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
1161 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1162 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1163 ct->proto.tcp.seen[0].td_scale =
1164 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1165 ct->proto.tcp.seen[1].td_scale =
1166 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
1168 write_unlock_bh(&tcp_lock);
1170 return 0;
1172 #endif
1174 #ifdef CONFIG_SYSCTL
1175 static unsigned int tcp_sysctl_table_users;
1176 static struct ctl_table_header *tcp_sysctl_header;
1177 static struct ctl_table tcp_sysctl_table[] = {
1179 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1180 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1181 .maxlen = sizeof(unsigned int),
1182 .mode = 0644,
1183 .proc_handler = &proc_dointvec_jiffies,
1186 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1187 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1188 .maxlen = sizeof(unsigned int),
1189 .mode = 0644,
1190 .proc_handler = &proc_dointvec_jiffies,
1193 .procname = "nf_conntrack_tcp_timeout_established",
1194 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1195 .maxlen = sizeof(unsigned int),
1196 .mode = 0644,
1197 .proc_handler = &proc_dointvec_jiffies,
1200 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1201 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1202 .maxlen = sizeof(unsigned int),
1203 .mode = 0644,
1204 .proc_handler = &proc_dointvec_jiffies,
1207 .procname = "nf_conntrack_tcp_timeout_close_wait",
1208 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1209 .maxlen = sizeof(unsigned int),
1210 .mode = 0644,
1211 .proc_handler = &proc_dointvec_jiffies,
1214 .procname = "nf_conntrack_tcp_timeout_last_ack",
1215 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1216 .maxlen = sizeof(unsigned int),
1217 .mode = 0644,
1218 .proc_handler = &proc_dointvec_jiffies,
1221 .procname = "nf_conntrack_tcp_timeout_time_wait",
1222 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1223 .maxlen = sizeof(unsigned int),
1224 .mode = 0644,
1225 .proc_handler = &proc_dointvec_jiffies,
1228 .procname = "nf_conntrack_tcp_timeout_close",
1229 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1230 .maxlen = sizeof(unsigned int),
1231 .mode = 0644,
1232 .proc_handler = &proc_dointvec_jiffies,
1235 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1236 .data = &nf_ct_tcp_timeout_max_retrans,
1237 .maxlen = sizeof(unsigned int),
1238 .mode = 0644,
1239 .proc_handler = &proc_dointvec_jiffies,
1242 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1243 .procname = "nf_conntrack_tcp_loose",
1244 .data = &nf_ct_tcp_loose,
1245 .maxlen = sizeof(unsigned int),
1246 .mode = 0644,
1247 .proc_handler = &proc_dointvec,
1250 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1251 .procname = "nf_conntrack_tcp_be_liberal",
1252 .data = &nf_ct_tcp_be_liberal,
1253 .maxlen = sizeof(unsigned int),
1254 .mode = 0644,
1255 .proc_handler = &proc_dointvec,
1258 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1259 .procname = "nf_conntrack_tcp_max_retrans",
1260 .data = &nf_ct_tcp_max_retrans,
1261 .maxlen = sizeof(unsigned int),
1262 .mode = 0644,
1263 .proc_handler = &proc_dointvec,
1266 .ctl_name = 0
1270 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1271 static struct ctl_table tcp_compat_sysctl_table[] = {
1273 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1274 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1275 .maxlen = sizeof(unsigned int),
1276 .mode = 0644,
1277 .proc_handler = &proc_dointvec_jiffies,
1280 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1281 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1282 .maxlen = sizeof(unsigned int),
1283 .mode = 0644,
1284 .proc_handler = &proc_dointvec_jiffies,
1287 .procname = "ip_conntrack_tcp_timeout_established",
1288 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1289 .maxlen = sizeof(unsigned int),
1290 .mode = 0644,
1291 .proc_handler = &proc_dointvec_jiffies,
1294 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1295 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1296 .maxlen = sizeof(unsigned int),
1297 .mode = 0644,
1298 .proc_handler = &proc_dointvec_jiffies,
1301 .procname = "ip_conntrack_tcp_timeout_close_wait",
1302 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1303 .maxlen = sizeof(unsigned int),
1304 .mode = 0644,
1305 .proc_handler = &proc_dointvec_jiffies,
1308 .procname = "ip_conntrack_tcp_timeout_last_ack",
1309 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1310 .maxlen = sizeof(unsigned int),
1311 .mode = 0644,
1312 .proc_handler = &proc_dointvec_jiffies,
1315 .procname = "ip_conntrack_tcp_timeout_time_wait",
1316 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1317 .maxlen = sizeof(unsigned int),
1318 .mode = 0644,
1319 .proc_handler = &proc_dointvec_jiffies,
1322 .procname = "ip_conntrack_tcp_timeout_close",
1323 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1324 .maxlen = sizeof(unsigned int),
1325 .mode = 0644,
1326 .proc_handler = &proc_dointvec_jiffies,
1329 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1330 .data = &nf_ct_tcp_timeout_max_retrans,
1331 .maxlen = sizeof(unsigned int),
1332 .mode = 0644,
1333 .proc_handler = &proc_dointvec_jiffies,
1336 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1337 .procname = "ip_conntrack_tcp_loose",
1338 .data = &nf_ct_tcp_loose,
1339 .maxlen = sizeof(unsigned int),
1340 .mode = 0644,
1341 .proc_handler = &proc_dointvec,
1344 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1345 .procname = "ip_conntrack_tcp_be_liberal",
1346 .data = &nf_ct_tcp_be_liberal,
1347 .maxlen = sizeof(unsigned int),
1348 .mode = 0644,
1349 .proc_handler = &proc_dointvec,
1352 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1353 .procname = "ip_conntrack_tcp_max_retrans",
1354 .data = &nf_ct_tcp_max_retrans,
1355 .maxlen = sizeof(unsigned int),
1356 .mode = 0644,
1357 .proc_handler = &proc_dointvec,
1360 .ctl_name = 0
1363 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1364 #endif /* CONFIG_SYSCTL */
1366 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1368 .l3proto = PF_INET,
1369 .l4proto = IPPROTO_TCP,
1370 .name = "tcp",
1371 .pkt_to_tuple = tcp_pkt_to_tuple,
1372 .invert_tuple = tcp_invert_tuple,
1373 .print_tuple = tcp_print_tuple,
1374 .print_conntrack = tcp_print_conntrack,
1375 .packet = tcp_packet,
1376 .new = tcp_new,
1377 .error = tcp_error,
1378 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1379 .to_nlattr = tcp_to_nlattr,
1380 .from_nlattr = nlattr_to_tcp,
1381 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1382 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1383 .nla_policy = nf_ct_port_nla_policy,
1384 #endif
1385 #ifdef CONFIG_SYSCTL
1386 .ctl_table_users = &tcp_sysctl_table_users,
1387 .ctl_table_header = &tcp_sysctl_header,
1388 .ctl_table = tcp_sysctl_table,
1389 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1390 .ctl_compat_table = tcp_compat_sysctl_table,
1391 #endif
1392 #endif
1394 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1396 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1398 .l3proto = PF_INET6,
1399 .l4proto = IPPROTO_TCP,
1400 .name = "tcp",
1401 .pkt_to_tuple = tcp_pkt_to_tuple,
1402 .invert_tuple = tcp_invert_tuple,
1403 .print_tuple = tcp_print_tuple,
1404 .print_conntrack = tcp_print_conntrack,
1405 .packet = tcp_packet,
1406 .new = tcp_new,
1407 .error = tcp_error,
1408 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1409 .to_nlattr = tcp_to_nlattr,
1410 .from_nlattr = nlattr_to_tcp,
1411 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1412 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1413 .nla_policy = nf_ct_port_nla_policy,
1414 #endif
1415 #ifdef CONFIG_SYSCTL
1416 .ctl_table_users = &tcp_sysctl_table_users,
1417 .ctl_table_header = &tcp_sysctl_header,
1418 .ctl_table = tcp_sysctl_table,
1419 #endif
1421 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);