inotify: fix race
[linux-2.6.22.y-op.git] / net / netfilter / nf_conntrack_proto_tcp.c
blobbaff1f42087545304c37cbfb548c6725ef369cf8
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/netfilter.h>
12 #include <linux/module.h>
13 #include <linux/in.h>
14 #include <linux/tcp.h>
15 #include <linux/spinlock.h>
16 #include <linux/skbuff.h>
17 #include <linux/ipv6.h>
18 #include <net/ip6_checksum.h>
20 #include <net/tcp.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv6.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_ecache.h>
29 #if 0
30 #define DEBUGP printk
31 #define DEBUGP_VARS
32 #else
33 #define DEBUGP(format, args...)
34 #endif
36 /* Protects conntrack->proto.tcp */
37 static DEFINE_RWLOCK(tcp_lock);
39 /* "Be conservative in what you do,
40 be liberal in what you accept from others."
41 If it's non-zero, we mark only out of window RST segments as INVALID. */
42 static int nf_ct_tcp_be_liberal __read_mostly = 0;
44 /* If it is set to zero, we disable picking up already established
45 connections. */
46 static int nf_ct_tcp_loose __read_mostly = 1;
48 /* Max number of the retransmitted packets without receiving an (acceptable)
49 ACK from the destination. If this number is reached, a shorter timer
50 will be started. */
51 static int nf_ct_tcp_max_retrans __read_mostly = 3;
53 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
54 closely. They're more complex. --RR */
56 static const char *tcp_conntrack_names[] = {
57 "NONE",
58 "SYN_SENT",
59 "SYN_RECV",
60 "ESTABLISHED",
61 "FIN_WAIT",
62 "CLOSE_WAIT",
63 "LAST_ACK",
64 "TIME_WAIT",
65 "CLOSE",
66 "LISTEN"
69 #define SECS * HZ
70 #define MINS * 60 SECS
71 #define HOURS * 60 MINS
72 #define DAYS * 24 HOURS
74 static unsigned int nf_ct_tcp_timeout_syn_sent __read_mostly = 2 MINS;
75 static unsigned int nf_ct_tcp_timeout_syn_recv __read_mostly = 60 SECS;
76 static unsigned int nf_ct_tcp_timeout_established __read_mostly = 5 DAYS;
77 static unsigned int nf_ct_tcp_timeout_fin_wait __read_mostly = 2 MINS;
78 static unsigned int nf_ct_tcp_timeout_close_wait __read_mostly = 60 SECS;
79 static unsigned int nf_ct_tcp_timeout_last_ack __read_mostly = 30 SECS;
80 static unsigned int nf_ct_tcp_timeout_time_wait __read_mostly = 2 MINS;
81 static unsigned int nf_ct_tcp_timeout_close __read_mostly = 10 SECS;
83 /* RFC1122 says the R2 limit should be at least 100 seconds.
84 Linux uses 15 packets as limit, which corresponds
85 to ~13-30min depending on RTO. */
86 static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
88 static unsigned int * tcp_timeouts[] = {
89 NULL, /* TCP_CONNTRACK_NONE */
90 &nf_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
91 &nf_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
92 &nf_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
93 &nf_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
94 &nf_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
95 &nf_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
96 &nf_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
97 &nf_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
98 NULL, /* TCP_CONNTRACK_LISTEN */
101 #define sNO TCP_CONNTRACK_NONE
102 #define sSS TCP_CONNTRACK_SYN_SENT
103 #define sSR TCP_CONNTRACK_SYN_RECV
104 #define sES TCP_CONNTRACK_ESTABLISHED
105 #define sFW TCP_CONNTRACK_FIN_WAIT
106 #define sCW TCP_CONNTRACK_CLOSE_WAIT
107 #define sLA TCP_CONNTRACK_LAST_ACK
108 #define sTW TCP_CONNTRACK_TIME_WAIT
109 #define sCL TCP_CONNTRACK_CLOSE
110 #define sLI TCP_CONNTRACK_LISTEN
111 #define sIV TCP_CONNTRACK_MAX
112 #define sIG TCP_CONNTRACK_IGNORE
114 /* What TCP flags are set from RST/SYN/FIN/ACK. */
115 enum tcp_bit_set {
116 TCP_SYN_SET,
117 TCP_SYNACK_SET,
118 TCP_FIN_SET,
119 TCP_ACK_SET,
120 TCP_RST_SET,
121 TCP_NONE_SET,
125 * The TCP state transition table needs a few words...
127 * We are the man in the middle. All the packets go through us
128 * but might get lost in transit to the destination.
129 * It is assumed that the destinations can't receive segments
130 * we haven't seen.
132 * The checked segment is in window, but our windows are *not*
133 * equivalent with the ones of the sender/receiver. We always
134 * try to guess the state of the current sender.
136 * The meaning of the states are:
138 * NONE: initial state
139 * SYN_SENT: SYN-only packet seen
140 * SYN_RECV: SYN-ACK packet seen
141 * ESTABLISHED: ACK packet seen
142 * FIN_WAIT: FIN packet seen
143 * CLOSE_WAIT: ACK seen (after FIN)
144 * LAST_ACK: FIN seen (after FIN)
145 * TIME_WAIT: last ACK seen
146 * CLOSE: closed connection (RST)
148 * LISTEN state is not used.
150 * Packets marked as IGNORED (sIG):
151 * if they may be either invalid or valid
152 * and the receiver may send back a connection
153 * closing RST or a SYN/ACK.
155 * Packets marked as INVALID (sIV):
156 * if they are invalid
157 * or we do not support the request (simultaneous open)
159 static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
161 /* ORIGINAL */
162 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
163 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
165 * sNO -> sSS Initialize a new connection
166 * sSS -> sSS Retransmitted SYN
167 * sSR -> sIG Late retransmitted SYN?
168 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
169 * are errors. Receiver will reply with RST
170 * and close the connection.
171 * Or we are not in sync and hold a dead connection.
172 * sFW -> sIG
173 * sCW -> sIG
174 * sLA -> sIG
175 * sTW -> sSS Reopened connection (RFC 1122).
176 * sCL -> sSS
178 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
179 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
181 * A SYN/ACK from the client is always invalid:
182 * - either it tries to set up a simultaneous open, which is
183 * not supported;
184 * - or the firewall has just been inserted between the two hosts
185 * during the session set-up. The SYN will be retransmitted
186 * by the true client (or it'll time out).
188 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
189 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
191 * sNO -> sIV Too late and no reason to do anything...
192 * sSS -> sIV Client migth not send FIN in this state:
193 * we enforce waiting for a SYN/ACK reply first.
194 * sSR -> sFW Close started.
195 * sES -> sFW
196 * sFW -> sLA FIN seen in both directions, waiting for
197 * the last ACK.
198 * Migth be a retransmitted FIN as well...
199 * sCW -> sLA
200 * sLA -> sLA Retransmitted FIN. Remain in the same state.
201 * sTW -> sTW
202 * sCL -> sCL
204 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
205 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
207 * sNO -> sES Assumed.
208 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
209 * sSR -> sES Established state is reached.
210 * sES -> sES :-)
211 * sFW -> sCW Normal close request answered by ACK.
212 * sCW -> sCW
213 * sLA -> sTW Last ACK detected.
214 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
215 * sCL -> sCL
217 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
218 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
219 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
222 /* REPLY */
223 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
224 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
226 * sNO -> sIV Never reached.
227 * sSS -> sIV Simultaneous open, not supported
228 * sSR -> sIV Simultaneous open, not supported.
229 * sES -> sIV Server may not initiate a connection.
230 * sFW -> sIV
231 * sCW -> sIV
232 * sLA -> sIV
233 * sTW -> sIV Reopened connection, but server may not do it.
234 * sCL -> sIV
236 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
237 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
239 * sSS -> sSR Standard open.
240 * sSR -> sSR Retransmitted SYN/ACK.
241 * sES -> sIG Late retransmitted SYN/ACK?
242 * sFW -> sIG Might be SYN/ACK answering ignored SYN
243 * sCW -> sIG
244 * sLA -> sIG
245 * sTW -> sIG
246 * sCL -> sIG
248 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
249 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
251 * sSS -> sIV Server might not send FIN in this state.
252 * sSR -> sFW Close started.
253 * sES -> sFW
254 * sFW -> sLA FIN seen in both directions.
255 * sCW -> sLA
256 * sLA -> sLA Retransmitted FIN.
257 * sTW -> sTW
258 * sCL -> sCL
260 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
261 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
263 * sSS -> sIG Might be a half-open connection.
264 * sSR -> sSR Might answer late resent SYN.
265 * sES -> sES :-)
266 * sFW -> sCW Normal close request answered by ACK.
267 * sCW -> sCW
268 * sLA -> sTW Last ACK detected.
269 * sTW -> sTW Retransmitted last ACK.
270 * sCL -> sCL
272 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
273 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
274 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
278 static int tcp_pkt_to_tuple(const struct sk_buff *skb,
279 unsigned int dataoff,
280 struct nf_conntrack_tuple *tuple)
282 struct tcphdr _hdr, *hp;
284 /* Actually only need first 8 bytes. */
285 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
286 if (hp == NULL)
287 return 0;
289 tuple->src.u.tcp.port = hp->source;
290 tuple->dst.u.tcp.port = hp->dest;
292 return 1;
295 static int tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
296 const struct nf_conntrack_tuple *orig)
298 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
299 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
300 return 1;
303 /* Print out the per-protocol part of the tuple. */
304 static int tcp_print_tuple(struct seq_file *s,
305 const struct nf_conntrack_tuple *tuple)
307 return seq_printf(s, "sport=%hu dport=%hu ",
308 ntohs(tuple->src.u.tcp.port),
309 ntohs(tuple->dst.u.tcp.port));
312 /* Print out the private part of the conntrack. */
313 static int tcp_print_conntrack(struct seq_file *s,
314 const struct nf_conn *conntrack)
316 enum tcp_conntrack state;
318 read_lock_bh(&tcp_lock);
319 state = conntrack->proto.tcp.state;
320 read_unlock_bh(&tcp_lock);
322 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
325 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
327 if (tcph->rst) return TCP_RST_SET;
328 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
329 else if (tcph->fin) return TCP_FIN_SET;
330 else if (tcph->ack) return TCP_ACK_SET;
331 else return TCP_NONE_SET;
334 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
335 in IP Filter' by Guido van Rooij.
337 http://www.nluug.nl/events/sane2000/papers.html
338 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
340 The boundaries and the conditions are changed according to RFC793:
341 the packet must intersect the window (i.e. segments may be
342 after the right or before the left edge) and thus receivers may ACK
343 segments after the right edge of the window.
345 td_maxend = max(sack + max(win,1)) seen in reply packets
346 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
347 td_maxwin += seq + len - sender.td_maxend
348 if seq + len > sender.td_maxend
349 td_end = max(seq + len) seen in sent packets
351 I. Upper bound for valid data: seq <= sender.td_maxend
352 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
353 III. Upper bound for valid ack: sack <= receiver.td_end
354 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
356 where sack is the highest right edge of sack block found in the packet.
358 The upper bound limit for a valid ack is not ignored -
359 we doesn't have to deal with fragments.
362 static inline __u32 segment_seq_plus_len(__u32 seq,
363 size_t len,
364 unsigned int dataoff,
365 struct tcphdr *tcph)
367 /* XXX Should I use payload length field in IP/IPv6 header ?
368 * - YK */
369 return (seq + len - dataoff - tcph->doff*4
370 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
373 /* Fixme: what about big packets? */
374 #define MAXACKWINCONST 66000
375 #define MAXACKWINDOW(sender) \
376 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
377 : MAXACKWINCONST)
380 * Simplified tcp_parse_options routine from tcp_input.c
382 static void tcp_options(const struct sk_buff *skb,
383 unsigned int dataoff,
384 struct tcphdr *tcph,
385 struct ip_ct_tcp_state *state)
387 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
388 unsigned char *ptr;
389 int length = (tcph->doff*4) - sizeof(struct tcphdr);
391 if (!length)
392 return;
394 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
395 length, buff);
396 BUG_ON(ptr == NULL);
398 state->td_scale =
399 state->flags = 0;
401 while (length > 0) {
402 int opcode=*ptr++;
403 int opsize;
405 switch (opcode) {
406 case TCPOPT_EOL:
407 return;
408 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
409 length--;
410 continue;
411 default:
412 opsize=*ptr++;
413 if (opsize < 2) /* "silly options" */
414 return;
415 if (opsize > length)
416 break; /* don't parse partial options */
418 if (opcode == TCPOPT_SACK_PERM
419 && opsize == TCPOLEN_SACK_PERM)
420 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
421 else if (opcode == TCPOPT_WINDOW
422 && opsize == TCPOLEN_WINDOW) {
423 state->td_scale = *(u_int8_t *)ptr;
425 if (state->td_scale > 14) {
426 /* See RFC1323 */
427 state->td_scale = 14;
429 state->flags |=
430 IP_CT_TCP_FLAG_WINDOW_SCALE;
432 ptr += opsize - 2;
433 length -= opsize;
438 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
439 struct tcphdr *tcph, __u32 *sack)
441 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
442 unsigned char *ptr;
443 int length = (tcph->doff*4) - sizeof(struct tcphdr);
444 __u32 tmp;
446 if (!length)
447 return;
449 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
450 length, buff);
451 BUG_ON(ptr == NULL);
453 /* Fast path for timestamp-only option */
454 if (length == TCPOLEN_TSTAMP_ALIGNED*4
455 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
456 | (TCPOPT_NOP << 16)
457 | (TCPOPT_TIMESTAMP << 8)
458 | TCPOLEN_TIMESTAMP))
459 return;
461 while (length > 0) {
462 int opcode = *ptr++;
463 int opsize, i;
465 switch (opcode) {
466 case TCPOPT_EOL:
467 return;
468 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
469 length--;
470 continue;
471 default:
472 opsize = *ptr++;
473 if (opsize < 2) /* "silly options" */
474 return;
475 if (opsize > length)
476 break; /* don't parse partial options */
478 if (opcode == TCPOPT_SACK
479 && opsize >= (TCPOLEN_SACK_BASE
480 + TCPOLEN_SACK_PERBLOCK)
481 && !((opsize - TCPOLEN_SACK_BASE)
482 % TCPOLEN_SACK_PERBLOCK)) {
483 for (i = 0;
484 i < (opsize - TCPOLEN_SACK_BASE);
485 i += TCPOLEN_SACK_PERBLOCK) {
486 tmp = ntohl(*((__be32 *)(ptr+i)+1));
488 if (after(tmp, *sack))
489 *sack = tmp;
491 return;
493 ptr += opsize - 2;
494 length -= opsize;
499 static int tcp_in_window(struct ip_ct_tcp *state,
500 enum ip_conntrack_dir dir,
501 unsigned int index,
502 const struct sk_buff *skb,
503 unsigned int dataoff,
504 struct tcphdr *tcph,
505 int pf)
507 struct ip_ct_tcp_state *sender = &state->seen[dir];
508 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
509 __u32 seq, ack, sack, end, win, swin;
510 int res;
513 * Get the required data from the packet.
515 seq = ntohl(tcph->seq);
516 ack = sack = ntohl(tcph->ack_seq);
517 win = ntohs(tcph->window);
518 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
520 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
521 tcp_sack(skb, dataoff, tcph, &sack);
523 DEBUGP("tcp_in_window: START\n");
524 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
525 "seq=%u ack=%u sack=%u win=%u end=%u\n",
526 NIPQUAD(iph->saddr), ntohs(tcph->source),
527 NIPQUAD(iph->daddr), ntohs(tcph->dest),
528 seq, ack, sack, win, end);
529 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
530 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
531 sender->td_end, sender->td_maxend, sender->td_maxwin,
532 sender->td_scale,
533 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
534 receiver->td_scale);
536 if (sender->td_end == 0) {
538 * Initialize sender data.
540 if (tcph->syn && tcph->ack) {
542 * Outgoing SYN-ACK in reply to a SYN.
544 sender->td_end =
545 sender->td_maxend = end;
546 sender->td_maxwin = (win == 0 ? 1 : win);
548 tcp_options(skb, dataoff, tcph, sender);
550 * RFC 1323:
551 * Both sides must send the Window Scale option
552 * to enable window scaling in either direction.
554 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
555 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
556 sender->td_scale =
557 receiver->td_scale = 0;
558 } else {
560 * We are in the middle of a connection,
561 * its history is lost for us.
562 * Let's try to use the data from the packet.
564 sender->td_end = end;
565 sender->td_maxwin = (win == 0 ? 1 : win);
566 sender->td_maxend = end + sender->td_maxwin;
568 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
569 && dir == IP_CT_DIR_ORIGINAL)
570 || (state->state == TCP_CONNTRACK_SYN_RECV
571 && dir == IP_CT_DIR_REPLY))
572 && after(end, sender->td_end)) {
574 * RFC 793: "if a TCP is reinitialized ... then it need
575 * not wait at all; it must only be sure to use sequence
576 * numbers larger than those recently used."
578 sender->td_end =
579 sender->td_maxend = end;
580 sender->td_maxwin = (win == 0 ? 1 : win);
582 tcp_options(skb, dataoff, tcph, sender);
585 if (!(tcph->ack)) {
587 * If there is no ACK, just pretend it was set and OK.
589 ack = sack = receiver->td_end;
590 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
591 (TCP_FLAG_ACK|TCP_FLAG_RST))
592 && (ack == 0)) {
594 * Broken TCP stacks, that set ACK in RST packets as well
595 * with zero ack value.
597 ack = sack = receiver->td_end;
600 if (seq == end
601 && (!tcph->rst
602 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
604 * Packets contains no data: we assume it is valid
605 * and check the ack value only.
606 * However RST segments are always validated by their
607 * SEQ number, except when seq == 0 (reset sent answering
608 * SYN.
610 seq = end = sender->td_end;
612 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
613 "seq=%u ack=%u sack =%u win=%u end=%u\n",
614 NIPQUAD(iph->saddr), ntohs(tcph->source),
615 NIPQUAD(iph->daddr), ntohs(tcph->dest),
616 seq, ack, sack, win, end);
617 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
618 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
619 sender->td_end, sender->td_maxend, sender->td_maxwin,
620 sender->td_scale,
621 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
622 receiver->td_scale);
624 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
625 before(seq, sender->td_maxend + 1),
626 after(end, sender->td_end - receiver->td_maxwin - 1),
627 before(sack, receiver->td_end + 1),
628 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
630 if (before(seq, sender->td_maxend + 1) &&
631 after(end, sender->td_end - receiver->td_maxwin - 1) &&
632 before(sack, receiver->td_end + 1) &&
633 after(ack, receiver->td_end - MAXACKWINDOW(sender))) {
635 * Take into account window scaling (RFC 1323).
637 if (!tcph->syn)
638 win <<= sender->td_scale;
641 * Update sender data.
643 swin = win + (sack - ack);
644 if (sender->td_maxwin < swin)
645 sender->td_maxwin = swin;
646 if (after(end, sender->td_end))
647 sender->td_end = end;
649 * Update receiver data.
651 if (after(end, sender->td_maxend))
652 receiver->td_maxwin += end - sender->td_maxend;
653 if (after(sack + win, receiver->td_maxend - 1)) {
654 receiver->td_maxend = sack + win;
655 if (win == 0)
656 receiver->td_maxend++;
660 * Check retransmissions.
662 if (index == TCP_ACK_SET) {
663 if (state->last_dir == dir
664 && state->last_seq == seq
665 && state->last_ack == ack
666 && state->last_end == end
667 && state->last_win == win)
668 state->retrans++;
669 else {
670 state->last_dir = dir;
671 state->last_seq = seq;
672 state->last_ack = ack;
673 state->last_end = end;
674 state->last_win = win;
675 state->retrans = 0;
678 res = 1;
679 } else {
680 res = 0;
681 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
682 nf_ct_tcp_be_liberal)
683 res = 1;
684 if (!res && LOG_INVALID(IPPROTO_TCP))
685 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
686 "nf_ct_tcp: %s ",
687 before(seq, sender->td_maxend + 1) ?
688 after(end, sender->td_end - receiver->td_maxwin - 1) ?
689 before(sack, receiver->td_end + 1) ?
690 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
691 : "ACK is under the lower bound (possible overly delayed ACK)"
692 : "ACK is over the upper bound (ACKed data not seen yet)"
693 : "SEQ is under the lower bound (already ACKed data retransmitted)"
694 : "SEQ is over the upper bound (over the window of the receiver)");
697 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
698 "receiver end=%u maxend=%u maxwin=%u\n",
699 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
700 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
702 return res;
705 #ifdef CONFIG_NF_NAT_NEEDED
706 /* Update sender->td_end after NAT successfully mangled the packet */
707 /* Caller must linearize skb at tcp header. */
708 void nf_conntrack_tcp_update(struct sk_buff *skb,
709 unsigned int dataoff,
710 struct nf_conn *conntrack,
711 int dir)
713 struct tcphdr *tcph = (void *)skb->data + dataoff;
714 __u32 end;
715 #ifdef DEBUGP_VARS
716 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
717 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
718 #endif
720 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
722 write_lock_bh(&tcp_lock);
724 * We have to worry for the ack in the reply packet only...
726 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
727 conntrack->proto.tcp.seen[dir].td_end = end;
728 conntrack->proto.tcp.last_end = end;
729 write_unlock_bh(&tcp_lock);
730 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
731 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
732 sender->td_end, sender->td_maxend, sender->td_maxwin,
733 sender->td_scale,
734 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
735 receiver->td_scale);
737 EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
738 #endif
740 #define TH_FIN 0x01
741 #define TH_SYN 0x02
742 #define TH_RST 0x04
743 #define TH_PUSH 0x08
744 #define TH_ACK 0x10
745 #define TH_URG 0x20
746 #define TH_ECE 0x40
747 #define TH_CWR 0x80
749 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
750 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
752 [TH_SYN] = 1,
753 [TH_SYN|TH_URG] = 1,
754 [TH_SYN|TH_ACK] = 1,
755 [TH_RST] = 1,
756 [TH_RST|TH_ACK] = 1,
757 [TH_FIN|TH_ACK] = 1,
758 [TH_FIN|TH_ACK|TH_URG] = 1,
759 [TH_ACK] = 1,
760 [TH_ACK|TH_URG] = 1,
763 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
764 static int tcp_error(struct sk_buff *skb,
765 unsigned int dataoff,
766 enum ip_conntrack_info *ctinfo,
767 int pf,
768 unsigned int hooknum)
770 struct tcphdr _tcph, *th;
771 unsigned int tcplen = skb->len - dataoff;
772 u_int8_t tcpflags;
774 /* Smaller that minimal TCP header? */
775 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
776 if (th == NULL) {
777 if (LOG_INVALID(IPPROTO_TCP))
778 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
779 "nf_ct_tcp: short packet ");
780 return -NF_ACCEPT;
783 /* Not whole TCP header or malformed packet */
784 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
785 if (LOG_INVALID(IPPROTO_TCP))
786 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
787 "nf_ct_tcp: truncated/malformed packet ");
788 return -NF_ACCEPT;
791 /* Checksum invalid? Ignore.
792 * We skip checking packets on the outgoing path
793 * because the checksum is assumed to be correct.
795 /* FIXME: Source route IP option packets --RR */
796 if (nf_conntrack_checksum &&
797 ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
798 (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
799 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
800 if (LOG_INVALID(IPPROTO_TCP))
801 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
802 "nf_ct_tcp: bad TCP checksum ");
803 return -NF_ACCEPT;
806 /* Check TCP flags. */
807 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
808 if (!tcp_valid_flags[tcpflags]) {
809 if (LOG_INVALID(IPPROTO_TCP))
810 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
811 "nf_ct_tcp: invalid TCP flag combination ");
812 return -NF_ACCEPT;
815 return NF_ACCEPT;
818 /* Returns verdict for packet, or -1 for invalid. */
819 static int tcp_packet(struct nf_conn *conntrack,
820 const struct sk_buff *skb,
821 unsigned int dataoff,
822 enum ip_conntrack_info ctinfo,
823 int pf,
824 unsigned int hooknum)
826 enum tcp_conntrack new_state, old_state;
827 enum ip_conntrack_dir dir;
828 struct tcphdr *th, _tcph;
829 unsigned long timeout;
830 unsigned int index;
832 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
833 BUG_ON(th == NULL);
835 write_lock_bh(&tcp_lock);
836 old_state = conntrack->proto.tcp.state;
837 dir = CTINFO2DIR(ctinfo);
838 index = get_conntrack_index(th);
839 new_state = tcp_conntracks[dir][index][old_state];
841 switch (new_state) {
842 case TCP_CONNTRACK_SYN_SENT:
843 if (old_state < TCP_CONNTRACK_TIME_WAIT)
844 break;
845 /* RFC 1122: "When a connection is closed actively,
846 * it MUST linger in TIME-WAIT state for a time 2xMSL
847 * (Maximum Segment Lifetime). However, it MAY accept
848 * a new SYN from the remote TCP to reopen the connection
849 * directly from TIME-WAIT state, if..."
850 * We ignore the conditions because we are in the
851 * TIME-WAIT state anyway.
853 * Handle aborted connections: we and the server
854 * think there is an existing connection but the client
855 * aborts it and starts a new one.
857 if (((conntrack->proto.tcp.seen[dir].flags
858 | conntrack->proto.tcp.seen[!dir].flags)
859 & IP_CT_TCP_FLAG_CLOSE_INIT)
860 || (conntrack->proto.tcp.last_dir == dir
861 && conntrack->proto.tcp.last_index == TCP_RST_SET)) {
862 /* Attempt to reopen a closed/aborted connection.
863 * Delete this connection and look up again. */
864 write_unlock_bh(&tcp_lock);
865 if (del_timer(&conntrack->timeout))
866 conntrack->timeout.function((unsigned long)
867 conntrack);
868 return -NF_REPEAT;
870 /* Fall through */
871 case TCP_CONNTRACK_IGNORE:
872 /* Ignored packets:
874 * Our connection entry may be out of sync, so ignore
875 * packets which may signal the real connection between
876 * the client and the server.
878 * a) SYN in ORIGINAL
879 * b) SYN/ACK in REPLY
880 * c) ACK in reply direction after initial SYN in original.
882 * If the ignored packet is invalid, the receiver will send
883 * a RST we'll catch below.
885 if (index == TCP_SYNACK_SET
886 && conntrack->proto.tcp.last_index == TCP_SYN_SET
887 && conntrack->proto.tcp.last_dir != dir
888 && ntohl(th->ack_seq) ==
889 conntrack->proto.tcp.last_end) {
890 /* b) This SYN/ACK acknowledges a SYN that we earlier
891 * ignored as invalid. This means that the client and
892 * the server are both in sync, while the firewall is
893 * not. We kill this session and block the SYN/ACK so
894 * that the client cannot but retransmit its SYN and
895 * thus initiate a clean new session.
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: killing out of sync session ");
901 if (del_timer(&conntrack->timeout))
902 conntrack->timeout.function((unsigned long)
903 conntrack);
904 return -NF_DROP;
906 conntrack->proto.tcp.last_index = index;
907 conntrack->proto.tcp.last_dir = dir;
908 conntrack->proto.tcp.last_seq = ntohl(th->seq);
909 conntrack->proto.tcp.last_end =
910 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
912 write_unlock_bh(&tcp_lock);
913 if (LOG_INVALID(IPPROTO_TCP))
914 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
915 "nf_ct_tcp: invalid packet ignored ");
916 return NF_ACCEPT;
917 case TCP_CONNTRACK_MAX:
918 /* Invalid packet */
919 DEBUGP("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
920 dir, get_conntrack_index(th),
921 old_state);
922 write_unlock_bh(&tcp_lock);
923 if (LOG_INVALID(IPPROTO_TCP))
924 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
925 "nf_ct_tcp: invalid state ");
926 return -NF_ACCEPT;
927 case TCP_CONNTRACK_CLOSE:
928 if (index == TCP_RST_SET
929 && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
930 && conntrack->proto.tcp.last_index == TCP_SYN_SET)
931 || (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
932 && conntrack->proto.tcp.last_index == TCP_ACK_SET))
933 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
934 /* RST sent to invalid SYN or ACK we had let through
935 * at a) and c) above:
937 * a) SYN was in window then
938 * c) we hold a half-open connection.
940 * Delete our connection entry.
941 * We skip window checking, because packet might ACK
942 * segments we ignored. */
943 goto in_window;
945 /* Just fall through */
946 default:
947 /* Keep compilers happy. */
948 break;
951 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
952 skb, dataoff, th, pf)) {
953 write_unlock_bh(&tcp_lock);
954 return -NF_ACCEPT;
956 in_window:
957 /* From now on we have got in-window packets */
958 conntrack->proto.tcp.last_index = index;
959 conntrack->proto.tcp.last_dir = dir;
961 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
962 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
963 NIPQUAD(iph->saddr), ntohs(th->source),
964 NIPQUAD(iph->daddr), ntohs(th->dest),
965 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
966 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
967 old_state, new_state);
969 conntrack->proto.tcp.state = new_state;
970 if (old_state != new_state
971 && new_state == TCP_CONNTRACK_FIN_WAIT)
972 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
973 timeout = conntrack->proto.tcp.retrans >= nf_ct_tcp_max_retrans
974 && *tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans
975 ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
976 write_unlock_bh(&tcp_lock);
978 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
979 if (new_state != old_state)
980 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
982 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
983 /* If only reply is a RST, we can consider ourselves not to
984 have an established connection: this is a fairly common
985 problem case, so we can delete the conntrack
986 immediately. --RR */
987 if (th->rst) {
988 if (del_timer(&conntrack->timeout))
989 conntrack->timeout.function((unsigned long)
990 conntrack);
991 return NF_ACCEPT;
993 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
994 && (old_state == TCP_CONNTRACK_SYN_RECV
995 || old_state == TCP_CONNTRACK_ESTABLISHED)
996 && new_state == TCP_CONNTRACK_ESTABLISHED) {
997 /* Set ASSURED if we see see valid ack in ESTABLISHED
998 after SYN_RECV or a valid answer for a picked up
999 connection. */
1000 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1001 nf_conntrack_event_cache(IPCT_STATUS, skb);
1003 nf_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1005 return NF_ACCEPT;
1008 /* Called when a new connection for this protocol found. */
1009 static int tcp_new(struct nf_conn *conntrack,
1010 const struct sk_buff *skb,
1011 unsigned int dataoff)
1013 enum tcp_conntrack new_state;
1014 struct tcphdr *th, _tcph;
1015 #ifdef DEBUGP_VARS
1016 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1017 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1018 #endif
1020 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1021 BUG_ON(th == NULL);
1023 /* Don't need lock here: this conntrack not in circulation yet */
1024 new_state
1025 = tcp_conntracks[0][get_conntrack_index(th)]
1026 [TCP_CONNTRACK_NONE];
1028 /* Invalid: delete conntrack */
1029 if (new_state >= TCP_CONNTRACK_MAX) {
1030 DEBUGP("nf_ct_tcp: invalid new deleting.\n");
1031 return 0;
1034 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1035 /* SYN packet */
1036 conntrack->proto.tcp.seen[0].td_end =
1037 segment_seq_plus_len(ntohl(th->seq), skb->len,
1038 dataoff, th);
1039 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1040 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1041 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1042 conntrack->proto.tcp.seen[0].td_maxend =
1043 conntrack->proto.tcp.seen[0].td_end;
1045 tcp_options(skb, dataoff, th, &conntrack->proto.tcp.seen[0]);
1046 conntrack->proto.tcp.seen[1].flags = 0;
1047 } else if (nf_ct_tcp_loose == 0) {
1048 /* Don't try to pick up connections. */
1049 return 0;
1050 } else {
1052 * We are in the middle of a connection,
1053 * its history is lost for us.
1054 * Let's try to use the data from the packet.
1056 conntrack->proto.tcp.seen[0].td_end =
1057 segment_seq_plus_len(ntohl(th->seq), skb->len,
1058 dataoff, th);
1059 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1060 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1061 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1062 conntrack->proto.tcp.seen[0].td_maxend =
1063 conntrack->proto.tcp.seen[0].td_end +
1064 conntrack->proto.tcp.seen[0].td_maxwin;
1065 conntrack->proto.tcp.seen[0].td_scale = 0;
1067 /* We assume SACK and liberal window checking to handle
1068 * window scaling */
1069 conntrack->proto.tcp.seen[0].flags =
1070 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1071 IP_CT_TCP_FLAG_BE_LIBERAL;
1074 conntrack->proto.tcp.seen[1].td_end = 0;
1075 conntrack->proto.tcp.seen[1].td_maxend = 0;
1076 conntrack->proto.tcp.seen[1].td_maxwin = 1;
1077 conntrack->proto.tcp.seen[1].td_scale = 0;
1079 /* tcp_packet will set them */
1080 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1081 conntrack->proto.tcp.last_index = TCP_NONE_SET;
1083 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1084 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1085 sender->td_end, sender->td_maxend, sender->td_maxwin,
1086 sender->td_scale,
1087 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1088 receiver->td_scale);
1089 return 1;
1092 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1094 #include <linux/netfilter/nfnetlink.h>
1095 #include <linux/netfilter/nfnetlink_conntrack.h>
1097 static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
1098 const struct nf_conn *ct)
1100 struct nfattr *nest_parms;
1101 struct nf_ct_tcp_flags tmp = {};
1103 read_lock_bh(&tcp_lock);
1104 nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
1105 NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
1106 &ct->proto.tcp.state);
1108 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL, sizeof(u_int8_t),
1109 &ct->proto.tcp.seen[0].td_scale);
1111 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY, sizeof(u_int8_t),
1112 &ct->proto.tcp.seen[1].td_scale);
1114 tmp.flags = ct->proto.tcp.seen[0].flags;
1115 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1116 sizeof(struct nf_ct_tcp_flags), &tmp);
1118 tmp.flags = ct->proto.tcp.seen[1].flags;
1119 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1120 sizeof(struct nf_ct_tcp_flags), &tmp);
1121 read_unlock_bh(&tcp_lock);
1123 NFA_NEST_END(skb, nest_parms);
1125 return 0;
1127 nfattr_failure:
1128 read_unlock_bh(&tcp_lock);
1129 return -1;
1132 static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = {
1133 [CTA_PROTOINFO_TCP_STATE-1] = sizeof(u_int8_t),
1134 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] = sizeof(u_int8_t),
1135 [CTA_PROTOINFO_TCP_WSCALE_REPLY-1] = sizeof(u_int8_t),
1136 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1] = sizeof(struct nf_ct_tcp_flags),
1137 [CTA_PROTOINFO_TCP_FLAGS_REPLY-1] = sizeof(struct nf_ct_tcp_flags)
1140 static int nfattr_to_tcp(struct nfattr *cda[], struct nf_conn *ct)
1142 struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
1143 struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
1145 /* updates could not contain anything about the private
1146 * protocol info, in that case skip the parsing */
1147 if (!attr)
1148 return 0;
1150 nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
1152 if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp))
1153 return -EINVAL;
1155 if (!tb[CTA_PROTOINFO_TCP_STATE-1])
1156 return -EINVAL;
1158 write_lock_bh(&tcp_lock);
1159 ct->proto.tcp.state =
1160 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
1162 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]) {
1163 struct nf_ct_tcp_flags *attr =
1164 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]);
1165 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1166 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1169 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]) {
1170 struct nf_ct_tcp_flags *attr =
1171 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]);
1172 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1173 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1176 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] &&
1177 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1] &&
1178 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1179 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1180 ct->proto.tcp.seen[0].td_scale = *(u_int8_t *)
1181 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1]);
1182 ct->proto.tcp.seen[1].td_scale = *(u_int8_t *)
1183 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1]);
1185 write_unlock_bh(&tcp_lock);
1187 return 0;
1189 #endif
1191 #ifdef CONFIG_SYSCTL
1192 static unsigned int tcp_sysctl_table_users;
1193 static struct ctl_table_header *tcp_sysctl_header;
1194 static struct ctl_table tcp_sysctl_table[] = {
1196 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1197 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1198 .data = &nf_ct_tcp_timeout_syn_sent,
1199 .maxlen = sizeof(unsigned int),
1200 .mode = 0644,
1201 .proc_handler = &proc_dointvec_jiffies,
1204 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1205 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1206 .data = &nf_ct_tcp_timeout_syn_recv,
1207 .maxlen = sizeof(unsigned int),
1208 .mode = 0644,
1209 .proc_handler = &proc_dointvec_jiffies,
1212 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1213 .procname = "nf_conntrack_tcp_timeout_established",
1214 .data = &nf_ct_tcp_timeout_established,
1215 .maxlen = sizeof(unsigned int),
1216 .mode = 0644,
1217 .proc_handler = &proc_dointvec_jiffies,
1220 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1221 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1222 .data = &nf_ct_tcp_timeout_fin_wait,
1223 .maxlen = sizeof(unsigned int),
1224 .mode = 0644,
1225 .proc_handler = &proc_dointvec_jiffies,
1228 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1229 .procname = "nf_conntrack_tcp_timeout_close_wait",
1230 .data = &nf_ct_tcp_timeout_close_wait,
1231 .maxlen = sizeof(unsigned int),
1232 .mode = 0644,
1233 .proc_handler = &proc_dointvec_jiffies,
1236 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1237 .procname = "nf_conntrack_tcp_timeout_last_ack",
1238 .data = &nf_ct_tcp_timeout_last_ack,
1239 .maxlen = sizeof(unsigned int),
1240 .mode = 0644,
1241 .proc_handler = &proc_dointvec_jiffies,
1244 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1245 .procname = "nf_conntrack_tcp_timeout_time_wait",
1246 .data = &nf_ct_tcp_timeout_time_wait,
1247 .maxlen = sizeof(unsigned int),
1248 .mode = 0644,
1249 .proc_handler = &proc_dointvec_jiffies,
1252 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1253 .procname = "nf_conntrack_tcp_timeout_close",
1254 .data = &nf_ct_tcp_timeout_close,
1255 .maxlen = sizeof(unsigned int),
1256 .mode = 0644,
1257 .proc_handler = &proc_dointvec_jiffies,
1260 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1261 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1262 .data = &nf_ct_tcp_timeout_max_retrans,
1263 .maxlen = sizeof(unsigned int),
1264 .mode = 0644,
1265 .proc_handler = &proc_dointvec_jiffies,
1268 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1269 .procname = "nf_conntrack_tcp_loose",
1270 .data = &nf_ct_tcp_loose,
1271 .maxlen = sizeof(unsigned int),
1272 .mode = 0644,
1273 .proc_handler = &proc_dointvec,
1276 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1277 .procname = "nf_conntrack_tcp_be_liberal",
1278 .data = &nf_ct_tcp_be_liberal,
1279 .maxlen = sizeof(unsigned int),
1280 .mode = 0644,
1281 .proc_handler = &proc_dointvec,
1284 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1285 .procname = "nf_conntrack_tcp_max_retrans",
1286 .data = &nf_ct_tcp_max_retrans,
1287 .maxlen = sizeof(unsigned int),
1288 .mode = 0644,
1289 .proc_handler = &proc_dointvec,
1292 .ctl_name = 0
1296 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1297 static struct ctl_table tcp_compat_sysctl_table[] = {
1299 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1300 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1301 .data = &nf_ct_tcp_timeout_syn_sent,
1302 .maxlen = sizeof(unsigned int),
1303 .mode = 0644,
1304 .proc_handler = &proc_dointvec_jiffies,
1307 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1308 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1309 .data = &nf_ct_tcp_timeout_syn_recv,
1310 .maxlen = sizeof(unsigned int),
1311 .mode = 0644,
1312 .proc_handler = &proc_dointvec_jiffies,
1315 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1316 .procname = "ip_conntrack_tcp_timeout_established",
1317 .data = &nf_ct_tcp_timeout_established,
1318 .maxlen = sizeof(unsigned int),
1319 .mode = 0644,
1320 .proc_handler = &proc_dointvec_jiffies,
1323 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1324 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1325 .data = &nf_ct_tcp_timeout_fin_wait,
1326 .maxlen = sizeof(unsigned int),
1327 .mode = 0644,
1328 .proc_handler = &proc_dointvec_jiffies,
1331 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1332 .procname = "ip_conntrack_tcp_timeout_close_wait",
1333 .data = &nf_ct_tcp_timeout_close_wait,
1334 .maxlen = sizeof(unsigned int),
1335 .mode = 0644,
1336 .proc_handler = &proc_dointvec_jiffies,
1339 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1340 .procname = "ip_conntrack_tcp_timeout_last_ack",
1341 .data = &nf_ct_tcp_timeout_last_ack,
1342 .maxlen = sizeof(unsigned int),
1343 .mode = 0644,
1344 .proc_handler = &proc_dointvec_jiffies,
1347 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1348 .procname = "ip_conntrack_tcp_timeout_time_wait",
1349 .data = &nf_ct_tcp_timeout_time_wait,
1350 .maxlen = sizeof(unsigned int),
1351 .mode = 0644,
1352 .proc_handler = &proc_dointvec_jiffies,
1355 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1356 .procname = "ip_conntrack_tcp_timeout_close",
1357 .data = &nf_ct_tcp_timeout_close,
1358 .maxlen = sizeof(unsigned int),
1359 .mode = 0644,
1360 .proc_handler = &proc_dointvec_jiffies,
1363 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1364 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1365 .data = &nf_ct_tcp_timeout_max_retrans,
1366 .maxlen = sizeof(unsigned int),
1367 .mode = 0644,
1368 .proc_handler = &proc_dointvec_jiffies,
1371 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1372 .procname = "ip_conntrack_tcp_loose",
1373 .data = &nf_ct_tcp_loose,
1374 .maxlen = sizeof(unsigned int),
1375 .mode = 0644,
1376 .proc_handler = &proc_dointvec,
1379 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1380 .procname = "ip_conntrack_tcp_be_liberal",
1381 .data = &nf_ct_tcp_be_liberal,
1382 .maxlen = sizeof(unsigned int),
1383 .mode = 0644,
1384 .proc_handler = &proc_dointvec,
1387 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1388 .procname = "ip_conntrack_tcp_max_retrans",
1389 .data = &nf_ct_tcp_max_retrans,
1390 .maxlen = sizeof(unsigned int),
1391 .mode = 0644,
1392 .proc_handler = &proc_dointvec,
1395 .ctl_name = 0
1398 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1399 #endif /* CONFIG_SYSCTL */
1401 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 =
1403 .l3proto = PF_INET,
1404 .l4proto = IPPROTO_TCP,
1405 .name = "tcp",
1406 .pkt_to_tuple = tcp_pkt_to_tuple,
1407 .invert_tuple = tcp_invert_tuple,
1408 .print_tuple = tcp_print_tuple,
1409 .print_conntrack = tcp_print_conntrack,
1410 .packet = tcp_packet,
1411 .new = tcp_new,
1412 .error = tcp_error,
1413 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1414 .to_nfattr = tcp_to_nfattr,
1415 .from_nfattr = nfattr_to_tcp,
1416 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1417 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1418 #endif
1419 #ifdef CONFIG_SYSCTL
1420 .ctl_table_users = &tcp_sysctl_table_users,
1421 .ctl_table_header = &tcp_sysctl_header,
1422 .ctl_table = tcp_sysctl_table,
1423 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1424 .ctl_compat_table = tcp_compat_sysctl_table,
1425 #endif
1426 #endif
1428 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1430 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 =
1432 .l3proto = PF_INET6,
1433 .l4proto = IPPROTO_TCP,
1434 .name = "tcp",
1435 .pkt_to_tuple = tcp_pkt_to_tuple,
1436 .invert_tuple = tcp_invert_tuple,
1437 .print_tuple = tcp_print_tuple,
1438 .print_conntrack = tcp_print_conntrack,
1439 .packet = tcp_packet,
1440 .new = tcp_new,
1441 .error = tcp_error,
1442 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1443 .to_nfattr = tcp_to_nfattr,
1444 .from_nfattr = nfattr_to_tcp,
1445 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1446 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1447 #endif
1448 #ifdef CONFIG_SYSCTL
1449 .ctl_table_users = &tcp_sysctl_table_users,
1450 .ctl_table_header = &tcp_sysctl_header,
1451 .ctl_table = tcp_sysctl_table,
1452 #endif
1454 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);