ipv6: syncookies: free reqsk on xfrm_lookup error
[linux-2.6/mini2440.git] / net / netfilter / nf_conntrack_proto_tcp.c
blob6f61261888eff8861699c5bcbdb767ef1cc9051e
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;
71 static unsigned int nf_ct_tcp_timeout_unacknowledged __read_mostly = 5 MINS;
73 static unsigned int tcp_timeouts[TCP_CONNTRACK_MAX] __read_mostly = {
74 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
75 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
76 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
77 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
78 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
79 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
80 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
81 [TCP_CONNTRACK_CLOSE] = 10 SECS,
84 #define sNO TCP_CONNTRACK_NONE
85 #define sSS TCP_CONNTRACK_SYN_SENT
86 #define sSR TCP_CONNTRACK_SYN_RECV
87 #define sES TCP_CONNTRACK_ESTABLISHED
88 #define sFW TCP_CONNTRACK_FIN_WAIT
89 #define sCW TCP_CONNTRACK_CLOSE_WAIT
90 #define sLA TCP_CONNTRACK_LAST_ACK
91 #define sTW TCP_CONNTRACK_TIME_WAIT
92 #define sCL TCP_CONNTRACK_CLOSE
93 #define sLI TCP_CONNTRACK_LISTEN
94 #define sIV TCP_CONNTRACK_MAX
95 #define sIG TCP_CONNTRACK_IGNORE
97 /* What TCP flags are set from RST/SYN/FIN/ACK. */
98 enum tcp_bit_set {
99 TCP_SYN_SET,
100 TCP_SYNACK_SET,
101 TCP_FIN_SET,
102 TCP_ACK_SET,
103 TCP_RST_SET,
104 TCP_NONE_SET,
108 * The TCP state transition table needs a few words...
110 * We are the man in the middle. All the packets go through us
111 * but might get lost in transit to the destination.
112 * It is assumed that the destinations can't receive segments
113 * we haven't seen.
115 * The checked segment is in window, but our windows are *not*
116 * equivalent with the ones of the sender/receiver. We always
117 * try to guess the state of the current sender.
119 * The meaning of the states are:
121 * NONE: initial state
122 * SYN_SENT: SYN-only packet seen
123 * SYN_RECV: SYN-ACK packet seen
124 * ESTABLISHED: ACK packet seen
125 * FIN_WAIT: FIN packet seen
126 * CLOSE_WAIT: ACK seen (after FIN)
127 * LAST_ACK: FIN seen (after FIN)
128 * TIME_WAIT: last ACK seen
129 * CLOSE: closed connection (RST)
131 * LISTEN state is not used.
133 * Packets marked as IGNORED (sIG):
134 * if they may be either invalid or valid
135 * and the receiver may send back a connection
136 * closing RST or a SYN/ACK.
138 * Packets marked as INVALID (sIV):
139 * if they are invalid
140 * or we do not support the request (simultaneous open)
142 static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
144 /* ORIGINAL */
145 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
146 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
148 * sNO -> sSS Initialize a new connection
149 * sSS -> sSS Retransmitted SYN
150 * sSR -> sIG Late retransmitted SYN?
151 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
152 * are errors. Receiver will reply with RST
153 * and close the connection.
154 * Or we are not in sync and hold a dead connection.
155 * sFW -> sIG
156 * sCW -> sIG
157 * sLA -> sIG
158 * sTW -> sSS Reopened connection (RFC 1122).
159 * sCL -> sSS
161 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
162 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
164 * A SYN/ACK from the client is always invalid:
165 * - either it tries to set up a simultaneous open, which is
166 * not supported;
167 * - or the firewall has just been inserted between the two hosts
168 * during the session set-up. The SYN will be retransmitted
169 * by the true client (or it'll time out).
171 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
172 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
174 * sNO -> sIV Too late and no reason to do anything...
175 * sSS -> sIV Client migth not send FIN in this state:
176 * we enforce waiting for a SYN/ACK reply first.
177 * sSR -> sFW Close started.
178 * sES -> sFW
179 * sFW -> sLA FIN seen in both directions, waiting for
180 * the last ACK.
181 * Migth be a retransmitted FIN as well...
182 * sCW -> sLA
183 * sLA -> sLA Retransmitted FIN. Remain in the same state.
184 * sTW -> sTW
185 * sCL -> sCL
187 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
188 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
190 * sNO -> sES Assumed.
191 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
192 * sSR -> sES Established state is reached.
193 * sES -> sES :-)
194 * sFW -> sCW Normal close request answered by ACK.
195 * sCW -> sCW
196 * sLA -> sTW Last ACK detected.
197 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
198 * sCL -> sCL
200 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
201 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
202 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
205 /* REPLY */
206 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
207 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
209 * sNO -> sIV Never reached.
210 * sSS -> sIV Simultaneous open, not supported
211 * sSR -> sIV Simultaneous open, not supported.
212 * sES -> sIV Server may not initiate a connection.
213 * sFW -> sIV
214 * sCW -> sIV
215 * sLA -> sIV
216 * sTW -> sIV Reopened connection, but server may not do it.
217 * sCL -> sIV
219 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
220 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
222 * sSS -> sSR Standard open.
223 * sSR -> sSR Retransmitted SYN/ACK.
224 * sES -> sIG Late retransmitted SYN/ACK?
225 * sFW -> sIG Might be SYN/ACK answering ignored SYN
226 * sCW -> sIG
227 * sLA -> sIG
228 * sTW -> sIG
229 * sCL -> sIG
231 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
232 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
234 * sSS -> sIV Server might not send FIN in this state.
235 * sSR -> sFW Close started.
236 * sES -> sFW
237 * sFW -> sLA FIN seen in both directions.
238 * sCW -> sLA
239 * sLA -> sLA Retransmitted FIN.
240 * sTW -> sTW
241 * sCL -> sCL
243 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
244 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
246 * sSS -> sIG Might be a half-open connection.
247 * sSR -> sSR Might answer late resent SYN.
248 * sES -> sES :-)
249 * sFW -> sCW Normal close request answered by ACK.
250 * sCW -> sCW
251 * sLA -> sTW Last ACK detected.
252 * sTW -> sTW Retransmitted last ACK.
253 * sCL -> sCL
255 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
256 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
257 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
261 static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
262 struct nf_conntrack_tuple *tuple)
264 const struct tcphdr *hp;
265 struct tcphdr _hdr;
267 /* Actually only need first 8 bytes. */
268 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
269 if (hp == NULL)
270 return false;
272 tuple->src.u.tcp.port = hp->source;
273 tuple->dst.u.tcp.port = hp->dest;
275 return true;
278 static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
279 const struct nf_conntrack_tuple *orig)
281 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
282 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
283 return true;
286 /* Print out the per-protocol part of the tuple. */
287 static int tcp_print_tuple(struct seq_file *s,
288 const struct nf_conntrack_tuple *tuple)
290 return seq_printf(s, "sport=%hu dport=%hu ",
291 ntohs(tuple->src.u.tcp.port),
292 ntohs(tuple->dst.u.tcp.port));
295 /* Print out the private part of the conntrack. */
296 static int tcp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
298 enum tcp_conntrack state;
300 read_lock_bh(&tcp_lock);
301 state = ct->proto.tcp.state;
302 read_unlock_bh(&tcp_lock);
304 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
307 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
309 if (tcph->rst) return TCP_RST_SET;
310 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
311 else if (tcph->fin) return TCP_FIN_SET;
312 else if (tcph->ack) return TCP_ACK_SET;
313 else return TCP_NONE_SET;
316 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
317 in IP Filter' by Guido van Rooij.
319 http://www.nluug.nl/events/sane2000/papers.html
320 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
322 The boundaries and the conditions are changed according to RFC793:
323 the packet must intersect the window (i.e. segments may be
324 after the right or before the left edge) and thus receivers may ACK
325 segments after the right edge of the window.
327 td_maxend = max(sack + max(win,1)) seen in reply packets
328 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
329 td_maxwin += seq + len - sender.td_maxend
330 if seq + len > sender.td_maxend
331 td_end = max(seq + len) seen in sent packets
333 I. Upper bound for valid data: seq <= sender.td_maxend
334 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
335 III. Upper bound for valid (s)ack: sack <= receiver.td_end
336 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
338 where sack is the highest right edge of sack block found in the packet
339 or ack in the case of packet without SACK option.
341 The upper bound limit for a valid (s)ack is not ignored -
342 we doesn't have to deal with fragments.
345 static inline __u32 segment_seq_plus_len(__u32 seq,
346 size_t len,
347 unsigned int dataoff,
348 const struct tcphdr *tcph)
350 /* XXX Should I use payload length field in IP/IPv6 header ?
351 * - YK */
352 return (seq + len - dataoff - tcph->doff*4
353 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
356 /* Fixme: what about big packets? */
357 #define MAXACKWINCONST 66000
358 #define MAXACKWINDOW(sender) \
359 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
360 : MAXACKWINCONST)
363 * Simplified tcp_parse_options routine from tcp_input.c
365 static void tcp_options(const struct sk_buff *skb,
366 unsigned int dataoff,
367 const struct tcphdr *tcph,
368 struct ip_ct_tcp_state *state)
370 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
371 const unsigned char *ptr;
372 int length = (tcph->doff*4) - sizeof(struct tcphdr);
374 if (!length)
375 return;
377 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
378 length, buff);
379 BUG_ON(ptr == NULL);
381 state->td_scale =
382 state->flags = 0;
384 while (length > 0) {
385 int opcode=*ptr++;
386 int opsize;
388 switch (opcode) {
389 case TCPOPT_EOL:
390 return;
391 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
392 length--;
393 continue;
394 default:
395 opsize=*ptr++;
396 if (opsize < 2) /* "silly options" */
397 return;
398 if (opsize > length)
399 break; /* don't parse partial options */
401 if (opcode == TCPOPT_SACK_PERM
402 && opsize == TCPOLEN_SACK_PERM)
403 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
404 else if (opcode == TCPOPT_WINDOW
405 && opsize == TCPOLEN_WINDOW) {
406 state->td_scale = *(u_int8_t *)ptr;
408 if (state->td_scale > 14) {
409 /* See RFC1323 */
410 state->td_scale = 14;
412 state->flags |=
413 IP_CT_TCP_FLAG_WINDOW_SCALE;
415 ptr += opsize - 2;
416 length -= opsize;
421 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
422 const struct tcphdr *tcph, __u32 *sack)
424 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
425 const unsigned char *ptr;
426 int length = (tcph->doff*4) - sizeof(struct tcphdr);
427 __u32 tmp;
429 if (!length)
430 return;
432 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
433 length, buff);
434 BUG_ON(ptr == NULL);
436 /* Fast path for timestamp-only option */
437 if (length == TCPOLEN_TSTAMP_ALIGNED*4
438 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
439 | (TCPOPT_NOP << 16)
440 | (TCPOPT_TIMESTAMP << 8)
441 | TCPOLEN_TIMESTAMP))
442 return;
444 while (length > 0) {
445 int opcode = *ptr++;
446 int opsize, i;
448 switch (opcode) {
449 case TCPOPT_EOL:
450 return;
451 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
452 length--;
453 continue;
454 default:
455 opsize = *ptr++;
456 if (opsize < 2) /* "silly options" */
457 return;
458 if (opsize > length)
459 break; /* don't parse partial options */
461 if (opcode == TCPOPT_SACK
462 && opsize >= (TCPOLEN_SACK_BASE
463 + TCPOLEN_SACK_PERBLOCK)
464 && !((opsize - TCPOLEN_SACK_BASE)
465 % TCPOLEN_SACK_PERBLOCK)) {
466 for (i = 0;
467 i < (opsize - TCPOLEN_SACK_BASE);
468 i += TCPOLEN_SACK_PERBLOCK) {
469 tmp = ntohl(*((__be32 *)(ptr+i)+1));
471 if (after(tmp, *sack))
472 *sack = tmp;
474 return;
476 ptr += opsize - 2;
477 length -= opsize;
482 static bool tcp_in_window(const struct nf_conn *ct,
483 struct ip_ct_tcp *state,
484 enum ip_conntrack_dir dir,
485 unsigned int index,
486 const struct sk_buff *skb,
487 unsigned int dataoff,
488 const struct tcphdr *tcph,
489 int pf)
491 struct ip_ct_tcp_state *sender = &state->seen[dir];
492 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
493 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
494 __u32 seq, ack, sack, end, win, swin;
495 bool res;
498 * Get the required data from the packet.
500 seq = ntohl(tcph->seq);
501 ack = sack = ntohl(tcph->ack_seq);
502 win = ntohs(tcph->window);
503 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
505 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
506 tcp_sack(skb, dataoff, tcph, &sack);
508 pr_debug("tcp_in_window: START\n");
509 pr_debug("tcp_in_window: ");
510 nf_ct_dump_tuple(tuple);
511 pr_debug("seq=%u ack=%u sack=%u win=%u end=%u\n",
512 seq, ack, sack, win, end);
513 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
514 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
515 sender->td_end, sender->td_maxend, sender->td_maxwin,
516 sender->td_scale,
517 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
518 receiver->td_scale);
520 if (sender->td_end == 0) {
522 * Initialize sender data.
524 if (tcph->syn && tcph->ack) {
526 * Outgoing SYN-ACK in reply to a SYN.
528 sender->td_end =
529 sender->td_maxend = end;
530 sender->td_maxwin = (win == 0 ? 1 : win);
532 tcp_options(skb, dataoff, tcph, sender);
534 * RFC 1323:
535 * Both sides must send the Window Scale option
536 * to enable window scaling in either direction.
538 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
539 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
540 sender->td_scale =
541 receiver->td_scale = 0;
542 } else {
544 * We are in the middle of a connection,
545 * its history is lost for us.
546 * Let's try to use the data from the packet.
548 sender->td_end = end;
549 sender->td_maxwin = (win == 0 ? 1 : win);
550 sender->td_maxend = end + sender->td_maxwin;
552 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
553 && dir == IP_CT_DIR_ORIGINAL)
554 || (state->state == TCP_CONNTRACK_SYN_RECV
555 && dir == IP_CT_DIR_REPLY))
556 && after(end, sender->td_end)) {
558 * RFC 793: "if a TCP is reinitialized ... then it need
559 * not wait at all; it must only be sure to use sequence
560 * numbers larger than those recently used."
562 sender->td_end =
563 sender->td_maxend = end;
564 sender->td_maxwin = (win == 0 ? 1 : win);
566 tcp_options(skb, dataoff, tcph, sender);
569 if (!(tcph->ack)) {
571 * If there is no ACK, just pretend it was set and OK.
573 ack = sack = receiver->td_end;
574 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
575 (TCP_FLAG_ACK|TCP_FLAG_RST))
576 && (ack == 0)) {
578 * Broken TCP stacks, that set ACK in RST packets as well
579 * with zero ack value.
581 ack = sack = receiver->td_end;
584 if (seq == end
585 && (!tcph->rst
586 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
588 * Packets contains no data: we assume it is valid
589 * and check the ack value only.
590 * However RST segments are always validated by their
591 * SEQ number, except when seq == 0 (reset sent answering
592 * SYN.
594 seq = end = sender->td_end;
596 pr_debug("tcp_in_window: ");
597 nf_ct_dump_tuple(tuple);
598 pr_debug("seq=%u ack=%u sack =%u win=%u end=%u\n",
599 seq, ack, sack, win, end);
600 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
601 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
602 sender->td_end, sender->td_maxend, sender->td_maxwin,
603 sender->td_scale,
604 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
605 receiver->td_scale);
607 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
608 before(seq, sender->td_maxend + 1),
609 after(end, sender->td_end - receiver->td_maxwin - 1),
610 before(sack, receiver->td_end + 1),
611 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
613 if (before(seq, sender->td_maxend + 1) &&
614 after(end, sender->td_end - receiver->td_maxwin - 1) &&
615 before(sack, receiver->td_end + 1) &&
616 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
618 * Take into account window scaling (RFC 1323).
620 if (!tcph->syn)
621 win <<= sender->td_scale;
624 * Update sender data.
626 swin = win + (sack - ack);
627 if (sender->td_maxwin < swin)
628 sender->td_maxwin = swin;
629 if (after(end, sender->td_end)) {
630 sender->td_end = end;
631 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
634 * Update receiver data.
636 if (after(end, sender->td_maxend))
637 receiver->td_maxwin += end - sender->td_maxend;
638 if (after(sack + win, receiver->td_maxend - 1)) {
639 receiver->td_maxend = sack + win;
640 if (win == 0)
641 receiver->td_maxend++;
643 if (ack == receiver->td_end)
644 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
647 * Check retransmissions.
649 if (index == TCP_ACK_SET) {
650 if (state->last_dir == dir
651 && state->last_seq == seq
652 && state->last_ack == ack
653 && state->last_end == end
654 && state->last_win == win)
655 state->retrans++;
656 else {
657 state->last_dir = dir;
658 state->last_seq = seq;
659 state->last_ack = ack;
660 state->last_end = end;
661 state->last_win = win;
662 state->retrans = 0;
665 res = true;
666 } else {
667 res = false;
668 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
669 nf_ct_tcp_be_liberal)
670 res = true;
671 if (!res && LOG_INVALID(IPPROTO_TCP))
672 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
673 "nf_ct_tcp: %s ",
674 before(seq, sender->td_maxend + 1) ?
675 after(end, sender->td_end - receiver->td_maxwin - 1) ?
676 before(sack, receiver->td_end + 1) ?
677 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
678 : "ACK is under the lower bound (possible overly delayed ACK)"
679 : "ACK is over the upper bound (ACKed data not seen yet)"
680 : "SEQ is under the lower bound (already ACKed data retransmitted)"
681 : "SEQ is over the upper bound (over the window of the receiver)");
684 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
685 "receiver end=%u maxend=%u maxwin=%u\n",
686 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
687 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
689 return res;
692 #ifdef CONFIG_NF_NAT_NEEDED
693 /* Update sender->td_end after NAT successfully mangled the packet */
694 /* Caller must linearize skb at tcp header. */
695 void nf_conntrack_tcp_update(const struct sk_buff *skb,
696 unsigned int dataoff,
697 struct nf_conn *ct,
698 int dir)
700 const struct tcphdr *tcph = (const void *)skb->data + dataoff;
701 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[dir];
702 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[!dir];
703 __u32 end;
705 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
707 write_lock_bh(&tcp_lock);
709 * We have to worry for the ack in the reply packet only...
711 if (after(end, ct->proto.tcp.seen[dir].td_end))
712 ct->proto.tcp.seen[dir].td_end = end;
713 ct->proto.tcp.last_end = end;
714 write_unlock_bh(&tcp_lock);
715 pr_debug("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
716 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
717 sender->td_end, sender->td_maxend, sender->td_maxwin,
718 sender->td_scale,
719 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
720 receiver->td_scale);
722 EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
723 #endif
725 #define TH_FIN 0x01
726 #define TH_SYN 0x02
727 #define TH_RST 0x04
728 #define TH_PUSH 0x08
729 #define TH_ACK 0x10
730 #define TH_URG 0x20
731 #define TH_ECE 0x40
732 #define TH_CWR 0x80
734 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
735 static const u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
737 [TH_SYN] = 1,
738 [TH_SYN|TH_URG] = 1,
739 [TH_SYN|TH_ACK] = 1,
740 [TH_RST] = 1,
741 [TH_RST|TH_ACK] = 1,
742 [TH_FIN|TH_ACK] = 1,
743 [TH_FIN|TH_ACK|TH_URG] = 1,
744 [TH_ACK] = 1,
745 [TH_ACK|TH_URG] = 1,
748 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
749 static int tcp_error(struct sk_buff *skb,
750 unsigned int dataoff,
751 enum ip_conntrack_info *ctinfo,
752 int pf,
753 unsigned int hooknum)
755 const struct tcphdr *th;
756 struct tcphdr _tcph;
757 unsigned int tcplen = skb->len - dataoff;
758 u_int8_t tcpflags;
760 /* Smaller that minimal TCP header? */
761 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
762 if (th == NULL) {
763 if (LOG_INVALID(IPPROTO_TCP))
764 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
765 "nf_ct_tcp: short packet ");
766 return -NF_ACCEPT;
769 /* Not whole TCP header or malformed packet */
770 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
771 if (LOG_INVALID(IPPROTO_TCP))
772 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
773 "nf_ct_tcp: truncated/malformed packet ");
774 return -NF_ACCEPT;
777 /* Checksum invalid? Ignore.
778 * We skip checking packets on the outgoing path
779 * because the checksum is assumed to be correct.
781 /* FIXME: Source route IP option packets --RR */
782 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
783 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
784 if (LOG_INVALID(IPPROTO_TCP))
785 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
786 "nf_ct_tcp: bad TCP checksum ");
787 return -NF_ACCEPT;
790 /* Check TCP flags. */
791 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
792 if (!tcp_valid_flags[tcpflags]) {
793 if (LOG_INVALID(IPPROTO_TCP))
794 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
795 "nf_ct_tcp: invalid TCP flag combination ");
796 return -NF_ACCEPT;
799 return NF_ACCEPT;
802 /* Returns verdict for packet, or -1 for invalid. */
803 static int tcp_packet(struct nf_conn *ct,
804 const struct sk_buff *skb,
805 unsigned int dataoff,
806 enum ip_conntrack_info ctinfo,
807 int pf,
808 unsigned int hooknum)
810 struct nf_conntrack_tuple *tuple;
811 enum tcp_conntrack new_state, old_state;
812 enum ip_conntrack_dir dir;
813 const struct tcphdr *th;
814 struct tcphdr _tcph;
815 unsigned long timeout;
816 unsigned int index;
818 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
819 BUG_ON(th == NULL);
821 write_lock_bh(&tcp_lock);
822 old_state = ct->proto.tcp.state;
823 dir = CTINFO2DIR(ctinfo);
824 index = get_conntrack_index(th);
825 new_state = tcp_conntracks[dir][index][old_state];
826 tuple = &ct->tuplehash[dir].tuple;
828 switch (new_state) {
829 case TCP_CONNTRACK_SYN_SENT:
830 if (old_state < TCP_CONNTRACK_TIME_WAIT)
831 break;
832 /* RFC 1122: "When a connection is closed actively,
833 * it MUST linger in TIME-WAIT state for a time 2xMSL
834 * (Maximum Segment Lifetime). However, it MAY accept
835 * a new SYN from the remote TCP to reopen the connection
836 * directly from TIME-WAIT state, if..."
837 * We ignore the conditions because we are in the
838 * TIME-WAIT state anyway.
840 * Handle aborted connections: we and the server
841 * think there is an existing connection but the client
842 * aborts it and starts a new one.
844 if (((ct->proto.tcp.seen[dir].flags
845 | ct->proto.tcp.seen[!dir].flags)
846 & IP_CT_TCP_FLAG_CLOSE_INIT)
847 || (ct->proto.tcp.last_dir == dir
848 && ct->proto.tcp.last_index == TCP_RST_SET)) {
849 /* Attempt to reopen a closed/aborted connection.
850 * Delete this connection and look up again. */
851 write_unlock_bh(&tcp_lock);
853 /* Only repeat if we can actually remove the timer.
854 * Destruction may already be in progress in process
855 * context and we must give it a chance to terminate.
857 if (nf_ct_kill(ct))
858 return -NF_REPEAT;
859 return -NF_DROP;
861 /* Fall through */
862 case TCP_CONNTRACK_IGNORE:
863 /* Ignored packets:
865 * Our connection entry may be out of sync, so ignore
866 * packets which may signal the real connection between
867 * the client and the server.
869 * a) SYN in ORIGINAL
870 * b) SYN/ACK in REPLY
871 * c) ACK in reply direction after initial SYN in original.
873 * If the ignored packet is invalid, the receiver will send
874 * a RST we'll catch below.
876 if (index == TCP_SYNACK_SET
877 && ct->proto.tcp.last_index == TCP_SYN_SET
878 && ct->proto.tcp.last_dir != dir
879 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
880 /* b) This SYN/ACK acknowledges a SYN that we earlier
881 * ignored as invalid. This means that the client and
882 * the server are both in sync, while the firewall is
883 * not. We kill this session and block the SYN/ACK so
884 * that the client cannot but retransmit its SYN and
885 * thus initiate a clean new session.
887 write_unlock_bh(&tcp_lock);
888 if (LOG_INVALID(IPPROTO_TCP))
889 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
890 "nf_ct_tcp: killing out of sync session ");
891 nf_ct_kill(ct);
892 return -NF_DROP;
894 ct->proto.tcp.last_index = index;
895 ct->proto.tcp.last_dir = dir;
896 ct->proto.tcp.last_seq = ntohl(th->seq);
897 ct->proto.tcp.last_end =
898 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
900 write_unlock_bh(&tcp_lock);
901 if (LOG_INVALID(IPPROTO_TCP))
902 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
903 "nf_ct_tcp: invalid packet ignored ");
904 return NF_ACCEPT;
905 case TCP_CONNTRACK_MAX:
906 /* Invalid packet */
907 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
908 dir, get_conntrack_index(th), old_state);
909 write_unlock_bh(&tcp_lock);
910 if (LOG_INVALID(IPPROTO_TCP))
911 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
912 "nf_ct_tcp: invalid state ");
913 return -NF_ACCEPT;
914 case TCP_CONNTRACK_CLOSE:
915 if (index == TCP_RST_SET
916 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
917 && ct->proto.tcp.last_index == TCP_SYN_SET)
918 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
919 && ct->proto.tcp.last_index == TCP_ACK_SET))
920 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
921 /* RST sent to invalid SYN or ACK we had let through
922 * at a) and c) above:
924 * a) SYN was in window then
925 * c) we hold a half-open connection.
927 * Delete our connection entry.
928 * We skip window checking, because packet might ACK
929 * segments we ignored. */
930 goto in_window;
932 /* Just fall through */
933 default:
934 /* Keep compilers happy. */
935 break;
938 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
939 skb, dataoff, th, pf)) {
940 write_unlock_bh(&tcp_lock);
941 return -NF_ACCEPT;
943 in_window:
944 /* From now on we have got in-window packets */
945 ct->proto.tcp.last_index = index;
946 ct->proto.tcp.last_dir = dir;
948 pr_debug("tcp_conntracks: ");
949 nf_ct_dump_tuple(tuple);
950 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
951 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
952 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
953 old_state, new_state);
955 ct->proto.tcp.state = new_state;
956 if (old_state != new_state
957 && new_state == TCP_CONNTRACK_FIN_WAIT)
958 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
960 if (ct->proto.tcp.retrans >= nf_ct_tcp_max_retrans &&
961 tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans)
962 timeout = nf_ct_tcp_timeout_max_retrans;
963 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
964 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
965 tcp_timeouts[new_state] > nf_ct_tcp_timeout_unacknowledged)
966 timeout = nf_ct_tcp_timeout_unacknowledged;
967 else
968 timeout = tcp_timeouts[new_state];
969 write_unlock_bh(&tcp_lock);
971 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
972 if (new_state != old_state)
973 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
975 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
976 /* If only reply is a RST, we can consider ourselves not to
977 have an established connection: this is a fairly common
978 problem case, so we can delete the conntrack
979 immediately. --RR */
980 if (th->rst) {
981 nf_ct_kill_acct(ct, ctinfo, skb);
982 return NF_ACCEPT;
984 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
985 && (old_state == TCP_CONNTRACK_SYN_RECV
986 || old_state == TCP_CONNTRACK_ESTABLISHED)
987 && new_state == TCP_CONNTRACK_ESTABLISHED) {
988 /* Set ASSURED if we see see valid ack in ESTABLISHED
989 after SYN_RECV or a valid answer for a picked up
990 connection. */
991 set_bit(IPS_ASSURED_BIT, &ct->status);
992 nf_conntrack_event_cache(IPCT_STATUS, skb);
994 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
996 return NF_ACCEPT;
999 /* Called when a new connection for this protocol found. */
1000 static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
1001 unsigned int dataoff)
1003 enum tcp_conntrack new_state;
1004 const struct tcphdr *th;
1005 struct tcphdr _tcph;
1006 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1007 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1009 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1010 BUG_ON(th == NULL);
1012 /* Don't need lock here: this conntrack not in circulation yet */
1013 new_state
1014 = tcp_conntracks[0][get_conntrack_index(th)]
1015 [TCP_CONNTRACK_NONE];
1017 /* Invalid: delete conntrack */
1018 if (new_state >= TCP_CONNTRACK_MAX) {
1019 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1020 return false;
1023 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1024 /* SYN packet */
1025 ct->proto.tcp.seen[0].td_end =
1026 segment_seq_plus_len(ntohl(th->seq), skb->len,
1027 dataoff, th);
1028 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1029 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1030 ct->proto.tcp.seen[0].td_maxwin = 1;
1031 ct->proto.tcp.seen[0].td_maxend =
1032 ct->proto.tcp.seen[0].td_end;
1034 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1035 ct->proto.tcp.seen[1].flags = 0;
1036 } else if (nf_ct_tcp_loose == 0) {
1037 /* Don't try to pick up connections. */
1038 return false;
1039 } else {
1041 * We are in the middle of a connection,
1042 * its history is lost for us.
1043 * Let's try to use the data from the packet.
1045 ct->proto.tcp.seen[0].td_end =
1046 segment_seq_plus_len(ntohl(th->seq), skb->len,
1047 dataoff, th);
1048 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1049 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1050 ct->proto.tcp.seen[0].td_maxwin = 1;
1051 ct->proto.tcp.seen[0].td_maxend =
1052 ct->proto.tcp.seen[0].td_end +
1053 ct->proto.tcp.seen[0].td_maxwin;
1054 ct->proto.tcp.seen[0].td_scale = 0;
1056 /* We assume SACK and liberal window checking to handle
1057 * window scaling */
1058 ct->proto.tcp.seen[0].flags =
1059 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1060 IP_CT_TCP_FLAG_BE_LIBERAL;
1063 ct->proto.tcp.seen[1].td_end = 0;
1064 ct->proto.tcp.seen[1].td_maxend = 0;
1065 ct->proto.tcp.seen[1].td_maxwin = 1;
1066 ct->proto.tcp.seen[1].td_scale = 0;
1068 /* tcp_packet will set them */
1069 ct->proto.tcp.state = TCP_CONNTRACK_NONE;
1070 ct->proto.tcp.last_index = TCP_NONE_SET;
1072 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1073 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1074 sender->td_end, sender->td_maxend, sender->td_maxwin,
1075 sender->td_scale,
1076 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1077 receiver->td_scale);
1078 return true;
1081 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1083 #include <linux/netfilter/nfnetlink.h>
1084 #include <linux/netfilter/nfnetlink_conntrack.h>
1086 static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1087 const struct nf_conn *ct)
1089 struct nlattr *nest_parms;
1090 struct nf_ct_tcp_flags tmp = {};
1092 read_lock_bh(&tcp_lock);
1093 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1094 if (!nest_parms)
1095 goto nla_put_failure;
1097 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state);
1099 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1100 ct->proto.tcp.seen[0].td_scale);
1102 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1103 ct->proto.tcp.seen[1].td_scale);
1105 tmp.flags = ct->proto.tcp.seen[0].flags;
1106 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1107 sizeof(struct nf_ct_tcp_flags), &tmp);
1109 tmp.flags = ct->proto.tcp.seen[1].flags;
1110 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1111 sizeof(struct nf_ct_tcp_flags), &tmp);
1112 read_unlock_bh(&tcp_lock);
1114 nla_nest_end(skb, nest_parms);
1116 return 0;
1118 nla_put_failure:
1119 read_unlock_bh(&tcp_lock);
1120 return -1;
1123 static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1124 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1125 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1126 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1127 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1128 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
1131 static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1133 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1134 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1135 int err;
1137 /* updates could not contain anything about the private
1138 * protocol info, in that case skip the parsing */
1139 if (!pattr)
1140 return 0;
1142 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
1143 if (err < 0)
1144 return err;
1146 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1147 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1148 return -EINVAL;
1150 write_lock_bh(&tcp_lock);
1151 if (tb[CTA_PROTOINFO_TCP_STATE])
1152 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1154 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1155 struct nf_ct_tcp_flags *attr =
1156 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1157 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1158 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1161 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1162 struct nf_ct_tcp_flags *attr =
1163 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1164 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1165 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1168 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1169 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
1170 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1171 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1172 ct->proto.tcp.seen[0].td_scale =
1173 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1174 ct->proto.tcp.seen[1].td_scale =
1175 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
1177 write_unlock_bh(&tcp_lock);
1179 return 0;
1181 #endif
1183 #ifdef CONFIG_SYSCTL
1184 static unsigned int tcp_sysctl_table_users;
1185 static struct ctl_table_header *tcp_sysctl_header;
1186 static struct ctl_table tcp_sysctl_table[] = {
1188 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1189 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1190 .maxlen = sizeof(unsigned int),
1191 .mode = 0644,
1192 .proc_handler = &proc_dointvec_jiffies,
1195 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1196 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1197 .maxlen = sizeof(unsigned int),
1198 .mode = 0644,
1199 .proc_handler = &proc_dointvec_jiffies,
1202 .procname = "nf_conntrack_tcp_timeout_established",
1203 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1204 .maxlen = sizeof(unsigned int),
1205 .mode = 0644,
1206 .proc_handler = &proc_dointvec_jiffies,
1209 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1210 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1211 .maxlen = sizeof(unsigned int),
1212 .mode = 0644,
1213 .proc_handler = &proc_dointvec_jiffies,
1216 .procname = "nf_conntrack_tcp_timeout_close_wait",
1217 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1218 .maxlen = sizeof(unsigned int),
1219 .mode = 0644,
1220 .proc_handler = &proc_dointvec_jiffies,
1223 .procname = "nf_conntrack_tcp_timeout_last_ack",
1224 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1225 .maxlen = sizeof(unsigned int),
1226 .mode = 0644,
1227 .proc_handler = &proc_dointvec_jiffies,
1230 .procname = "nf_conntrack_tcp_timeout_time_wait",
1231 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1232 .maxlen = sizeof(unsigned int),
1233 .mode = 0644,
1234 .proc_handler = &proc_dointvec_jiffies,
1237 .procname = "nf_conntrack_tcp_timeout_close",
1238 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1239 .maxlen = sizeof(unsigned int),
1240 .mode = 0644,
1241 .proc_handler = &proc_dointvec_jiffies,
1244 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1245 .data = &nf_ct_tcp_timeout_max_retrans,
1246 .maxlen = sizeof(unsigned int),
1247 .mode = 0644,
1248 .proc_handler = &proc_dointvec_jiffies,
1251 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
1252 .data = &nf_ct_tcp_timeout_unacknowledged,
1253 .maxlen = sizeof(unsigned int),
1254 .mode = 0644,
1255 .proc_handler = &proc_dointvec_jiffies,
1258 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1259 .procname = "nf_conntrack_tcp_loose",
1260 .data = &nf_ct_tcp_loose,
1261 .maxlen = sizeof(unsigned int),
1262 .mode = 0644,
1263 .proc_handler = &proc_dointvec,
1266 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1267 .procname = "nf_conntrack_tcp_be_liberal",
1268 .data = &nf_ct_tcp_be_liberal,
1269 .maxlen = sizeof(unsigned int),
1270 .mode = 0644,
1271 .proc_handler = &proc_dointvec,
1274 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1275 .procname = "nf_conntrack_tcp_max_retrans",
1276 .data = &nf_ct_tcp_max_retrans,
1277 .maxlen = sizeof(unsigned int),
1278 .mode = 0644,
1279 .proc_handler = &proc_dointvec,
1282 .ctl_name = 0
1286 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1287 static struct ctl_table tcp_compat_sysctl_table[] = {
1289 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1290 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1291 .maxlen = sizeof(unsigned int),
1292 .mode = 0644,
1293 .proc_handler = &proc_dointvec_jiffies,
1296 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1297 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1298 .maxlen = sizeof(unsigned int),
1299 .mode = 0644,
1300 .proc_handler = &proc_dointvec_jiffies,
1303 .procname = "ip_conntrack_tcp_timeout_established",
1304 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1305 .maxlen = sizeof(unsigned int),
1306 .mode = 0644,
1307 .proc_handler = &proc_dointvec_jiffies,
1310 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1311 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1312 .maxlen = sizeof(unsigned int),
1313 .mode = 0644,
1314 .proc_handler = &proc_dointvec_jiffies,
1317 .procname = "ip_conntrack_tcp_timeout_close_wait",
1318 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1319 .maxlen = sizeof(unsigned int),
1320 .mode = 0644,
1321 .proc_handler = &proc_dointvec_jiffies,
1324 .procname = "ip_conntrack_tcp_timeout_last_ack",
1325 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1326 .maxlen = sizeof(unsigned int),
1327 .mode = 0644,
1328 .proc_handler = &proc_dointvec_jiffies,
1331 .procname = "ip_conntrack_tcp_timeout_time_wait",
1332 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1333 .maxlen = sizeof(unsigned int),
1334 .mode = 0644,
1335 .proc_handler = &proc_dointvec_jiffies,
1338 .procname = "ip_conntrack_tcp_timeout_close",
1339 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1340 .maxlen = sizeof(unsigned int),
1341 .mode = 0644,
1342 .proc_handler = &proc_dointvec_jiffies,
1345 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1346 .data = &nf_ct_tcp_timeout_max_retrans,
1347 .maxlen = sizeof(unsigned int),
1348 .mode = 0644,
1349 .proc_handler = &proc_dointvec_jiffies,
1352 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1353 .procname = "ip_conntrack_tcp_loose",
1354 .data = &nf_ct_tcp_loose,
1355 .maxlen = sizeof(unsigned int),
1356 .mode = 0644,
1357 .proc_handler = &proc_dointvec,
1360 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1361 .procname = "ip_conntrack_tcp_be_liberal",
1362 .data = &nf_ct_tcp_be_liberal,
1363 .maxlen = sizeof(unsigned int),
1364 .mode = 0644,
1365 .proc_handler = &proc_dointvec,
1368 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1369 .procname = "ip_conntrack_tcp_max_retrans",
1370 .data = &nf_ct_tcp_max_retrans,
1371 .maxlen = sizeof(unsigned int),
1372 .mode = 0644,
1373 .proc_handler = &proc_dointvec,
1376 .ctl_name = 0
1379 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1380 #endif /* CONFIG_SYSCTL */
1382 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1384 .l3proto = PF_INET,
1385 .l4proto = IPPROTO_TCP,
1386 .name = "tcp",
1387 .pkt_to_tuple = tcp_pkt_to_tuple,
1388 .invert_tuple = tcp_invert_tuple,
1389 .print_tuple = tcp_print_tuple,
1390 .print_conntrack = tcp_print_conntrack,
1391 .packet = tcp_packet,
1392 .new = tcp_new,
1393 .error = tcp_error,
1394 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1395 .to_nlattr = tcp_to_nlattr,
1396 .from_nlattr = nlattr_to_tcp,
1397 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1398 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1399 .nla_policy = nf_ct_port_nla_policy,
1400 #endif
1401 #ifdef CONFIG_SYSCTL
1402 .ctl_table_users = &tcp_sysctl_table_users,
1403 .ctl_table_header = &tcp_sysctl_header,
1404 .ctl_table = tcp_sysctl_table,
1405 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1406 .ctl_compat_table = tcp_compat_sysctl_table,
1407 #endif
1408 #endif
1410 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1412 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1414 .l3proto = PF_INET6,
1415 .l4proto = IPPROTO_TCP,
1416 .name = "tcp",
1417 .pkt_to_tuple = tcp_pkt_to_tuple,
1418 .invert_tuple = tcp_invert_tuple,
1419 .print_tuple = tcp_print_tuple,
1420 .print_conntrack = tcp_print_conntrack,
1421 .packet = tcp_packet,
1422 .new = tcp_new,
1423 .error = tcp_error,
1424 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1425 .to_nlattr = tcp_to_nlattr,
1426 .from_nlattr = nlattr_to_tcp,
1427 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1428 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1429 .nla_policy = nf_ct_port_nla_policy,
1430 #endif
1431 #ifdef CONFIG_SYSCTL
1432 .ctl_table_users = &tcp_sysctl_table_users,
1433 .ctl_table_header = &tcp_sysctl_header,
1434 .ctl_table = tcp_sysctl_table,
1435 #endif
1437 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);