Broadcom SDK and wireless driver: another attempt to update to ver. 5.10.147.0
[tomato.git] / release / src-rt / linux / linux-2.6 / net / netfilter / nf_conntrack_proto_tcp.c
blob6f7dabaa891ed0a10c6cffc05d7168dc9e6e566c
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 extern int clean_flag; // 2009.04 James. wanduck.
35 #define DEBUGP(format, args...)
36 #endif
38 #ifdef HNDCTF
39 #include <ctf/hndctf.h>
40 extern ctf_t *kcih;
41 extern int ip_conntrack_ipct_delete(struct nf_conn *ct, int ct_timeout);
42 #endif /* HNDCTF */
44 /* Protects conntrack->proto.tcp */
45 static DEFINE_RWLOCK(tcp_lock);
47 /* "Be conservative in what you do,
48 be liberal in what you accept from others."
49 If it's non-zero, we mark only out of window RST segments as INVALID. */
50 static int nf_ct_tcp_be_liberal __read_mostly = 0;
52 /* If it is set to zero, we disable picking up already established
53 connections. */
54 static int nf_ct_tcp_loose __read_mostly = 1;
56 /* Max number of the retransmitted packets without receiving an (acceptable)
57 ACK from the destination. If this number is reached, a shorter timer
58 will be started. */
59 static int nf_ct_tcp_max_retrans __read_mostly = 3;
61 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
62 closely. They're more complex. --RR */
64 static const char *tcp_conntrack_names[] = {
65 "NONE",
66 "SYN_SENT",
67 "SYN_RECV",
68 "ESTABLISHED",
69 "FIN_WAIT",
70 "CLOSE_WAIT",
71 "LAST_ACK",
72 "TIME_WAIT",
73 "CLOSE",
74 "LISTEN"
77 #define SECS * HZ
78 #define MINS * 60 SECS
79 #define HOURS * 60 MINS
80 #define DAYS * 24 HOURS
82 static unsigned int nf_ct_tcp_timeout_syn_sent __read_mostly = 2 MINS;
83 static unsigned int nf_ct_tcp_timeout_syn_recv __read_mostly = 60 SECS;
84 static unsigned int nf_ct_tcp_timeout_established __read_mostly = 20 MINS; // was 5 days zzz
85 static unsigned int nf_ct_tcp_timeout_fin_wait __read_mostly = 2 MINS;
86 static unsigned int nf_ct_tcp_timeout_close_wait __read_mostly = 60 SECS;
87 static unsigned int nf_ct_tcp_timeout_last_ack __read_mostly = 30 SECS;
88 static unsigned int nf_ct_tcp_timeout_time_wait __read_mostly = 2 MINS;
89 static unsigned int nf_ct_tcp_timeout_close __read_mostly = 10 SECS;
91 /* RFC1122 says the R2 limit should be at least 100 seconds.
92 Linux uses 15 packets as limit, which corresponds
93 to ~13-30min depending on RTO. */
94 static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
96 static unsigned int * tcp_timeouts[] = {
97 NULL, /* TCP_CONNTRACK_NONE */
98 &nf_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
99 &nf_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
100 &nf_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
101 &nf_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
102 &nf_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
103 &nf_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
104 &nf_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
105 &nf_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
106 NULL, /* TCP_CONNTRACK_LISTEN */
109 #define sNO TCP_CONNTRACK_NONE
110 #define sSS TCP_CONNTRACK_SYN_SENT
111 #define sSR TCP_CONNTRACK_SYN_RECV
112 #define sES TCP_CONNTRACK_ESTABLISHED
113 #define sFW TCP_CONNTRACK_FIN_WAIT
114 #define sCW TCP_CONNTRACK_CLOSE_WAIT
115 #define sLA TCP_CONNTRACK_LAST_ACK
116 #define sTW TCP_CONNTRACK_TIME_WAIT
117 #define sCL TCP_CONNTRACK_CLOSE
118 #define sLI TCP_CONNTRACK_LISTEN
119 #define sIV TCP_CONNTRACK_MAX
120 #define sIG TCP_CONNTRACK_IGNORE
122 /* What TCP flags are set from RST/SYN/FIN/ACK. */
123 enum tcp_bit_set {
124 TCP_SYN_SET,
125 TCP_SYNACK_SET,
126 TCP_FIN_SET,
127 TCP_ACK_SET,
128 TCP_RST_SET,
129 TCP_NONE_SET,
133 * The TCP state transition table needs a few words...
135 * We are the man in the middle. All the packets go through us
136 * but might get lost in transit to the destination.
137 * It is assumed that the destinations can't receive segments
138 * we haven't seen.
140 * The checked segment is in window, but our windows are *not*
141 * equivalent with the ones of the sender/receiver. We always
142 * try to guess the state of the current sender.
144 * The meaning of the states are:
146 * NONE: initial state
147 * SYN_SENT: SYN-only packet seen
148 * SYN_RECV: SYN-ACK packet seen
149 * ESTABLISHED: ACK packet seen
150 * FIN_WAIT: FIN packet seen
151 * CLOSE_WAIT: ACK seen (after FIN)
152 * LAST_ACK: FIN seen (after FIN)
153 * TIME_WAIT: last ACK seen
154 * CLOSE: closed connection (RST)
156 * LISTEN state is not used.
158 * Packets marked as IGNORED (sIG):
159 * if they may be either invalid or valid
160 * and the receiver may send back a connection
161 * closing RST or a SYN/ACK.
163 * Packets marked as INVALID (sIV):
164 * if they are invalid
165 * or we do not support the request (simultaneous open)
167 static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
169 /* ORIGINAL */
170 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
171 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
173 * sNO -> sSS Initialize a new connection
174 * sSS -> sSS Retransmitted SYN
175 * sSR -> sIG Late retransmitted SYN?
176 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
177 * are errors. Receiver will reply with RST
178 * and close the connection.
179 * Or we are not in sync and hold a dead connection.
180 * sFW -> sIG
181 * sCW -> sIG
182 * sLA -> sIG
183 * sTW -> sSS Reopened connection (RFC 1122).
184 * sCL -> sSS
186 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
187 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
189 * A SYN/ACK from the client is always invalid:
190 * - either it tries to set up a simultaneous open, which is
191 * not supported;
192 * - or the firewall has just been inserted between the two hosts
193 * during the session set-up. The SYN will be retransmitted
194 * by the true client (or it'll time out).
196 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
197 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
199 * sNO -> sIV Too late and no reason to do anything...
200 * sSS -> sIV Client migth not send FIN in this state:
201 * we enforce waiting for a SYN/ACK reply first.
202 * sSR -> sFW Close started.
203 * sES -> sFW
204 * sFW -> sLA FIN seen in both directions, waiting for
205 * the last ACK.
206 * Migth be a retransmitted FIN as well...
207 * sCW -> sLA
208 * sLA -> sLA Retransmitted FIN. Remain in the same state.
209 * sTW -> sTW
210 * sCL -> sCL
212 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
213 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
215 * sNO -> sES Assumed.
216 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
217 * sSR -> sES Established state is reached.
218 * sES -> sES :-)
219 * sFW -> sCW Normal close request answered by ACK.
220 * sCW -> sCW
221 * sLA -> sTW Last ACK detected.
222 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
223 * sCL -> sCL
225 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
226 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
227 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
230 /* REPLY */
231 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
232 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
234 * sNO -> sIV Never reached.
235 * sSS -> sIV Simultaneous open, not supported
236 * sSR -> sIV Simultaneous open, not supported.
237 * sES -> sIV Server may not initiate a connection.
238 * sFW -> sIV
239 * sCW -> sIV
240 * sLA -> sIV
241 * sTW -> sIV Reopened connection, but server may not do it.
242 * sCL -> sIV
244 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
245 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
247 * sSS -> sSR Standard open.
248 * sSR -> sSR Retransmitted SYN/ACK.
249 * sES -> sIG Late retransmitted SYN/ACK?
250 * sFW -> sIG Might be SYN/ACK answering ignored SYN
251 * sCW -> sIG
252 * sLA -> sIG
253 * sTW -> sIG
254 * sCL -> sIG
256 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
257 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
259 * sSS -> sIV Server might not send FIN in this state.
260 * sSR -> sFW Close started.
261 * sES -> sFW
262 * sFW -> sLA FIN seen in both directions.
263 * sCW -> sLA
264 * sLA -> sLA Retransmitted FIN.
265 * sTW -> sTW
266 * sCL -> sCL
268 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
269 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
271 * sSS -> sIG Might be a half-open connection.
272 * sSR -> sSR Might answer late resent SYN.
273 * sES -> sES :-)
274 * sFW -> sCW Normal close request answered by ACK.
275 * sCW -> sCW
276 * sLA -> sTW Last ACK detected.
277 * sTW -> sTW Retransmitted last ACK.
278 * sCL -> sCL
280 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
281 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
282 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
286 static int tcp_pkt_to_tuple(const struct sk_buff *skb,
287 unsigned int dataoff,
288 struct nf_conntrack_tuple *tuple)
290 struct tcphdr _hdr, *hp;
292 /* Actually only need first 8 bytes. */
293 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
294 if (hp == NULL)
295 return 0;
297 tuple->src.u.tcp.port = hp->source;
298 tuple->dst.u.tcp.port = hp->dest;
300 return 1;
303 static int tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
304 const struct nf_conntrack_tuple *orig)
306 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
307 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
308 return 1;
311 /* Print out the per-protocol part of the tuple. */
312 static int tcp_print_tuple(struct seq_file *s,
313 const struct nf_conntrack_tuple *tuple)
315 return seq_printf(s, "sport=%hu dport=%hu ",
316 ntohs(tuple->src.u.tcp.port),
317 ntohs(tuple->dst.u.tcp.port));
320 /* Print out the private part of the conntrack. */
321 static int tcp_print_conntrack(struct seq_file *s,
322 const struct nf_conn *conntrack)
324 enum tcp_conntrack state;
326 // 2009.04 James. wanduck. {
327 if(clean_flag == 101)
328 nf_ct_refresh(conntrack, NULL, 0);
329 // 2009.04 James. wanduck. }
331 read_lock_bh(&tcp_lock);
332 state = conntrack->proto.tcp.state;
333 read_unlock_bh(&tcp_lock);
335 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
338 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
340 if (tcph->rst) return TCP_RST_SET;
341 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
342 else if (tcph->fin) return TCP_FIN_SET;
343 else if (tcph->ack) return TCP_ACK_SET;
344 else return TCP_NONE_SET;
347 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
348 in IP Filter' by Guido van Rooij.
350 http://www.nluug.nl/events/sane2000/papers.html
351 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
353 The boundaries and the conditions are changed according to RFC793:
354 the packet must intersect the window (i.e. segments may be
355 after the right or before the left edge) and thus receivers may ACK
356 segments after the right edge of the window.
358 td_maxend = max(sack + max(win,1)) seen in reply packets
359 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
360 td_maxwin += seq + len - sender.td_maxend
361 if seq + len > sender.td_maxend
362 td_end = max(seq + len) seen in sent packets
364 I. Upper bound for valid data: seq <= sender.td_maxend
365 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
366 III. Upper bound for valid ack: sack <= receiver.td_end
367 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
369 where sack is the highest right edge of sack block found in the packet.
371 The upper bound limit for a valid ack is not ignored -
372 we doesn't have to deal with fragments.
375 static inline __u32 segment_seq_plus_len(__u32 seq,
376 size_t len,
377 unsigned int dataoff,
378 struct tcphdr *tcph)
380 /* XXX Should I use payload length field in IP/IPv6 header ?
381 * - YK */
382 return (seq + len - dataoff - tcph->doff*4
383 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
386 /* Fixme: what about big packets? */
387 #define MAXACKWINCONST 66000
388 #define MAXACKWINDOW(sender) \
389 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
390 : MAXACKWINCONST)
393 * Simplified tcp_parse_options routine from tcp_input.c
395 static void tcp_options(const struct sk_buff *skb,
396 unsigned int dataoff,
397 struct tcphdr *tcph,
398 struct ip_ct_tcp_state *state)
400 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
401 unsigned char *ptr;
402 int length = (tcph->doff*4) - sizeof(struct tcphdr);
404 if (!length)
405 return;
407 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
408 length, buff);
409 BUG_ON(ptr == NULL);
411 state->td_scale =
412 state->flags = 0;
414 while (length > 0) {
415 int opcode=*ptr++;
416 int opsize;
418 switch (opcode) {
419 case TCPOPT_EOL:
420 return;
421 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
422 length--;
423 continue;
424 default:
425 opsize=*ptr++;
426 if (opsize < 2) /* "silly options" */
427 return;
428 if (opsize > length)
429 break; /* don't parse partial options */
431 if (opcode == TCPOPT_SACK_PERM
432 && opsize == TCPOLEN_SACK_PERM)
433 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
434 else if (opcode == TCPOPT_WINDOW
435 && opsize == TCPOLEN_WINDOW) {
436 state->td_scale = *(u_int8_t *)ptr;
438 if (state->td_scale > 14) {
439 /* See RFC1323 */
440 state->td_scale = 14;
442 state->flags |=
443 IP_CT_TCP_FLAG_WINDOW_SCALE;
445 ptr += opsize - 2;
446 length -= opsize;
451 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
452 struct tcphdr *tcph, __u32 *sack)
454 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
455 unsigned char *ptr;
456 int length = (tcph->doff*4) - sizeof(struct tcphdr);
457 __u32 tmp;
459 if (!length)
460 return;
462 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
463 length, buff);
464 BUG_ON(ptr == NULL);
466 /* Fast path for timestamp-only option */
467 if (length == TCPOLEN_TSTAMP_ALIGNED*4
468 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
469 | (TCPOPT_NOP << 16)
470 | (TCPOPT_TIMESTAMP << 8)
471 | TCPOLEN_TIMESTAMP))
472 return;
474 while (length > 0) {
475 int opcode = *ptr++;
476 int opsize, i;
478 switch (opcode) {
479 case TCPOPT_EOL:
480 return;
481 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
482 length--;
483 continue;
484 default:
485 opsize = *ptr++;
486 if (opsize < 2) /* "silly options" */
487 return;
488 if (opsize > length)
489 break; /* don't parse partial options */
491 if (opcode == TCPOPT_SACK
492 && opsize >= (TCPOLEN_SACK_BASE
493 + TCPOLEN_SACK_PERBLOCK)
494 && !((opsize - TCPOLEN_SACK_BASE)
495 % TCPOLEN_SACK_PERBLOCK)) {
496 for (i = 0;
497 i < (opsize - TCPOLEN_SACK_BASE);
498 i += TCPOLEN_SACK_PERBLOCK) {
499 tmp = ntohl(*((__be32 *)(ptr+i)+1));
501 if (after(tmp, *sack))
502 *sack = tmp;
504 return;
506 ptr += opsize - 2;
507 length -= opsize;
512 static int tcp_in_window(struct ip_ct_tcp *state,
513 enum ip_conntrack_dir dir,
514 unsigned int index,
515 const struct sk_buff *skb,
516 unsigned int dataoff,
517 struct tcphdr *tcph,
518 int pf)
520 struct ip_ct_tcp_state *sender = &state->seen[dir];
521 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
522 __u32 seq, ack, sack, end, win, swin;
523 int res;
526 * Get the required data from the packet.
528 seq = ntohl(tcph->seq);
529 ack = sack = ntohl(tcph->ack_seq);
530 win = ntohs(tcph->window);
531 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
533 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
534 tcp_sack(skb, dataoff, tcph, &sack);
536 DEBUGP("tcp_in_window: START\n");
537 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
538 "seq=%u ack=%u sack=%u win=%u end=%u\n",
539 NIPQUAD(iph->saddr), ntohs(tcph->source),
540 NIPQUAD(iph->daddr), ntohs(tcph->dest),
541 seq, ack, sack, win, end);
542 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
543 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
544 sender->td_end, sender->td_maxend, sender->td_maxwin,
545 sender->td_scale,
546 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
547 receiver->td_scale);
549 if (sender->td_end == 0) {
551 * Initialize sender data.
553 if (tcph->syn && tcph->ack) {
555 * Outgoing SYN-ACK in reply to a SYN.
557 sender->td_end =
558 sender->td_maxend = end;
559 sender->td_maxwin = (win == 0 ? 1 : win);
561 tcp_options(skb, dataoff, tcph, sender);
563 * RFC 1323:
564 * Both sides must send the Window Scale option
565 * to enable window scaling in either direction.
567 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
568 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
569 sender->td_scale =
570 receiver->td_scale = 0;
571 } else {
573 * We are in the middle of a connection,
574 * its history is lost for us.
575 * Let's try to use the data from the packet.
577 sender->td_end = end;
578 sender->td_maxwin = (win == 0 ? 1 : win);
579 sender->td_maxend = end + sender->td_maxwin;
581 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
582 && dir == IP_CT_DIR_ORIGINAL)
583 || (state->state == TCP_CONNTRACK_SYN_RECV
584 && dir == IP_CT_DIR_REPLY))
585 && after(end, sender->td_end)) {
587 * RFC 793: "if a TCP is reinitialized ... then it need
588 * not wait at all; it must only be sure to use sequence
589 * numbers larger than those recently used."
591 sender->td_end =
592 sender->td_maxend = end;
593 sender->td_maxwin = (win == 0 ? 1 : win);
595 tcp_options(skb, dataoff, tcph, sender);
598 if (!(tcph->ack)) {
600 * If there is no ACK, just pretend it was set and OK.
602 ack = sack = receiver->td_end;
603 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
604 (TCP_FLAG_ACK|TCP_FLAG_RST))
605 && (ack == 0)) {
607 * Broken TCP stacks, that set ACK in RST packets as well
608 * with zero ack value.
610 ack = sack = receiver->td_end;
613 if (seq == end
614 && (!tcph->rst
615 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
617 * Packets contains no data: we assume it is valid
618 * and check the ack value only.
619 * However RST segments are always validated by their
620 * SEQ number, except when seq == 0 (reset sent answering
621 * SYN.
623 seq = end = sender->td_end;
625 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
626 "seq=%u ack=%u sack =%u win=%u end=%u\n",
627 NIPQUAD(iph->saddr), ntohs(tcph->source),
628 NIPQUAD(iph->daddr), ntohs(tcph->dest),
629 seq, ack, sack, win, end);
630 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
631 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
632 sender->td_end, sender->td_maxend, sender->td_maxwin,
633 sender->td_scale,
634 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
635 receiver->td_scale);
637 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
638 before(seq, sender->td_maxend + 1),
639 after(end, sender->td_end - receiver->td_maxwin - 1),
640 before(sack, receiver->td_end + 1),
641 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
643 if (before(seq, sender->td_maxend + 1) &&
644 after(end, sender->td_end - receiver->td_maxwin - 1) &&
645 before(sack, receiver->td_end + 1) &&
646 after(ack, receiver->td_end - MAXACKWINDOW(sender))) {
648 * Take into account window scaling (RFC 1323).
650 if (!tcph->syn)
651 win <<= sender->td_scale;
654 * Update sender data.
656 swin = win + (sack - ack);
657 if (sender->td_maxwin < swin)
658 sender->td_maxwin = swin;
659 if (after(end, sender->td_end))
660 sender->td_end = end;
662 * Update receiver data.
664 if (after(end, sender->td_maxend))
665 receiver->td_maxwin += end - sender->td_maxend;
666 if (after(sack + win, receiver->td_maxend - 1)) {
667 receiver->td_maxend = sack + win;
668 if (win == 0)
669 receiver->td_maxend++;
673 * Check retransmissions.
675 if (index == TCP_ACK_SET) {
676 if (state->last_dir == dir
677 && state->last_seq == seq
678 && state->last_ack == ack
679 && state->last_end == end
680 && state->last_win == win)
681 state->retrans++;
682 else {
683 state->last_dir = dir;
684 state->last_seq = seq;
685 state->last_ack = ack;
686 state->last_end = end;
687 state->last_win = win;
688 state->retrans = 0;
691 res = 1;
692 } else {
693 res = 0;
694 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
695 nf_ct_tcp_be_liberal)
696 res = 1;
697 if (!res && LOG_INVALID(IPPROTO_TCP))
698 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
699 "nf_ct_tcp: %s ",
700 before(seq, sender->td_maxend + 1) ?
701 after(end, sender->td_end - receiver->td_maxwin - 1) ?
702 before(sack, receiver->td_end + 1) ?
703 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
704 : "ACK is under the lower bound (possible overly delayed ACK)"
705 : "ACK is over the upper bound (ACKed data not seen yet)"
706 : "SEQ is under the lower bound (already ACKed data retransmitted)"
707 : "SEQ is over the upper bound (over the window of the receiver)");
710 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
711 "receiver end=%u maxend=%u maxwin=%u\n",
712 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
713 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
715 return res;
718 #ifdef CONFIG_NF_NAT_NEEDED
719 /* Update sender->td_end after NAT successfully mangled the packet */
720 /* Caller must linearize skb at tcp header. */
721 void nf_conntrack_tcp_update(struct sk_buff *skb,
722 unsigned int dataoff,
723 struct nf_conn *conntrack,
724 int dir)
726 struct tcphdr *tcph = (void *)skb->data + dataoff;
727 __u32 end;
728 #ifdef DEBUGP_VARS
729 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
730 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
731 #endif
733 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
735 write_lock_bh(&tcp_lock);
737 * We have to worry for the ack in the reply packet only...
739 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
740 conntrack->proto.tcp.seen[dir].td_end = end;
741 conntrack->proto.tcp.last_end = end;
742 write_unlock_bh(&tcp_lock);
743 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
744 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
745 sender->td_end, sender->td_maxend, sender->td_maxwin,
746 sender->td_scale,
747 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
748 receiver->td_scale);
750 EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
751 #endif
753 #define TH_FIN 0x01
754 #define TH_SYN 0x02
755 #define TH_RST 0x04
756 #define TH_PUSH 0x08
757 #define TH_ACK 0x10
758 #define TH_URG 0x20
759 #define TH_ECE 0x40
760 #define TH_CWR 0x80
762 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
763 static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
765 [TH_SYN] = 1,
766 [TH_SYN|TH_URG] = 1,
767 [TH_SYN|TH_ACK] = 1,
768 [TH_RST] = 1,
769 [TH_RST|TH_ACK] = 1,
770 [TH_FIN|TH_ACK] = 1,
771 [TH_FIN|TH_ACK|TH_URG] = 1,
772 [TH_ACK] = 1,
773 [TH_ACK|TH_URG] = 1,
776 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
777 static int tcp_error(struct sk_buff *skb,
778 unsigned int dataoff,
779 enum ip_conntrack_info *ctinfo,
780 int pf,
781 unsigned int hooknum)
783 struct tcphdr _tcph, *th;
784 unsigned int tcplen = skb->len - dataoff;
785 u_int8_t tcpflags;
787 /* Smaller that minimal TCP header? */
788 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
789 if (th == NULL) {
790 if (LOG_INVALID(IPPROTO_TCP))
791 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
792 "nf_ct_tcp: short packet ");
793 return -NF_ACCEPT;
796 /* Not whole TCP header or malformed packet */
797 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
798 if (LOG_INVALID(IPPROTO_TCP))
799 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
800 "nf_ct_tcp: truncated/malformed packet ");
801 return -NF_ACCEPT;
804 /* Checksum invalid? Ignore.
805 * We skip checking packets on the outgoing path
806 * because the checksum is assumed to be correct.
808 /* FIXME: Source route IP option packets --RR */
809 if (nf_conntrack_checksum &&
810 ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
811 (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
812 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
813 if (LOG_INVALID(IPPROTO_TCP))
814 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
815 "nf_ct_tcp: bad TCP checksum ");
816 return -NF_ACCEPT;
819 /* Check TCP flags. */
820 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
821 if (!tcp_valid_flags[tcpflags]) {
822 if (LOG_INVALID(IPPROTO_TCP))
823 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
824 "nf_ct_tcp: invalid TCP flag combination ");
825 return -NF_ACCEPT;
828 return NF_ACCEPT;
831 /* Returns verdict for packet, or -1 for invalid. */
832 static int tcp_packet(struct nf_conn *conntrack,
833 const struct sk_buff *skb,
834 unsigned int dataoff,
835 enum ip_conntrack_info ctinfo,
836 int pf,
837 unsigned int hooknum)
839 enum tcp_conntrack new_state, old_state;
840 enum ip_conntrack_dir dir;
841 struct tcphdr *th, _tcph;
842 unsigned long timeout;
843 unsigned int index;
845 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
846 BUG_ON(th == NULL);
848 write_lock_bh(&tcp_lock);
849 old_state = conntrack->proto.tcp.state;
850 dir = CTINFO2DIR(ctinfo);
851 index = get_conntrack_index(th);
852 new_state = tcp_conntracks[dir][index][old_state];
854 switch (new_state) {
855 case TCP_CONNTRACK_SYN_SENT:
856 if (old_state < TCP_CONNTRACK_TIME_WAIT)
857 break;
858 /* RFC 1122: "When a connection is closed actively,
859 * it MUST linger in TIME-WAIT state for a time 2xMSL
860 * (Maximum Segment Lifetime). However, it MAY accept
861 * a new SYN from the remote TCP to reopen the connection
862 * directly from TIME-WAIT state, if..."
863 * We ignore the conditions because we are in the
864 * TIME-WAIT state anyway.
866 * Handle aborted connections: we and the server
867 * think there is an existing connection but the client
868 * aborts it and starts a new one.
870 if (((conntrack->proto.tcp.seen[dir].flags
871 | conntrack->proto.tcp.seen[!dir].flags)
872 & IP_CT_TCP_FLAG_CLOSE_INIT)
873 || (conntrack->proto.tcp.last_dir == dir
874 && conntrack->proto.tcp.last_index == TCP_RST_SET)) {
875 /* Attempt to reopen a closed/aborted connection.
876 * Delete this connection and look up again. */
877 write_unlock_bh(&tcp_lock);
878 if (del_timer(&conntrack->timeout))
879 conntrack->timeout.function((unsigned long)
880 conntrack);
881 return -NF_REPEAT;
883 /* Fall through */
884 case TCP_CONNTRACK_IGNORE:
885 /* Ignored packets:
887 * Our connection entry may be out of sync, so ignore
888 * packets which may signal the real connection between
889 * the client and the server.
891 * a) SYN in ORIGINAL
892 * b) SYN/ACK in REPLY
893 * c) ACK in reply direction after initial SYN in original.
895 * If the ignored packet is invalid, the receiver will send
896 * a RST we'll catch below.
898 if (index == TCP_SYNACK_SET
899 && conntrack->proto.tcp.last_index == TCP_SYN_SET
900 && conntrack->proto.tcp.last_dir != dir
901 && ntohl(th->ack_seq) ==
902 conntrack->proto.tcp.last_end) {
903 /* b) This SYN/ACK acknowledges a SYN that we earlier
904 * ignored as invalid. This means that the client and
905 * the server are both in sync, while the firewall is
906 * not. We kill this session and block the SYN/ACK so
907 * that the client cannot but retransmit its SYN and
908 * thus initiate a clean new session.
910 write_unlock_bh(&tcp_lock);
911 if (LOG_INVALID(IPPROTO_TCP))
912 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
913 "nf_ct_tcp: killing out of sync session ");
914 if (del_timer(&conntrack->timeout))
915 conntrack->timeout.function((unsigned long)
916 conntrack);
917 return -NF_DROP;
919 conntrack->proto.tcp.last_index = index;
920 conntrack->proto.tcp.last_dir = dir;
921 conntrack->proto.tcp.last_seq = ntohl(th->seq);
922 conntrack->proto.tcp.last_end =
923 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
925 write_unlock_bh(&tcp_lock);
926 if (LOG_INVALID(IPPROTO_TCP))
927 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
928 "nf_ct_tcp: invalid packet ignored ");
929 return NF_ACCEPT;
930 case TCP_CONNTRACK_MAX:
931 /* Invalid packet */
932 DEBUGP("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
933 dir, get_conntrack_index(th),
934 old_state);
935 write_unlock_bh(&tcp_lock);
936 if (LOG_INVALID(IPPROTO_TCP))
937 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
938 "nf_ct_tcp: invalid state ");
939 return -NF_ACCEPT;
940 case TCP_CONNTRACK_CLOSE:
941 if (index == TCP_RST_SET
942 && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
943 && conntrack->proto.tcp.last_index == TCP_SYN_SET)
944 || (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
945 && conntrack->proto.tcp.last_index == TCP_ACK_SET))
946 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
947 /* RST sent to invalid SYN or ACK we had let through
948 * at a) and c) above:
950 * a) SYN was in window then
951 * c) we hold a half-open connection.
953 * Delete our connection entry.
954 * We skip window checking, because packet might ACK
955 * segments we ignored. */
956 goto in_window;
958 /* Just fall through */
959 default:
960 /* Keep compilers happy. */
961 break;
964 #ifdef HNDCTF
965 /* Remove the ipc entries on receipt of FIN or RST */
966 if (CTF_ENAB(kcih) && (th->fin || th->rst)) {
967 if (conntrack->ctf_flags & CTF_FLAGS_CACHED) {
968 ip_conntrack_ipct_delete(conntrack, 0);
969 goto in_window;
972 #endif /* HNDCTF */
974 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
975 skb, dataoff, th, pf)) {
976 write_unlock_bh(&tcp_lock);
977 return -NF_ACCEPT;
979 in_window:
980 /* From now on we have got in-window packets */
981 conntrack->proto.tcp.last_index = index;
982 conntrack->proto.tcp.last_dir = dir;
984 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
985 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
986 NIPQUAD(iph->saddr), ntohs(th->source),
987 NIPQUAD(iph->daddr), ntohs(th->dest),
988 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
989 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
990 old_state, new_state);
992 conntrack->proto.tcp.state = new_state;
993 if (old_state != new_state
994 && new_state == TCP_CONNTRACK_FIN_WAIT)
995 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
996 timeout = conntrack->proto.tcp.retrans >= nf_ct_tcp_max_retrans
997 && *tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans
998 ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
999 write_unlock_bh(&tcp_lock);
1001 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
1002 if (new_state != old_state)
1003 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
1005 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
1006 /* If only reply is a RST, we can consider ourselves not to
1007 have an established connection: this is a fairly common
1008 problem case, so we can delete the conntrack
1009 immediately. --RR */
1010 if (th->rst) {
1011 if (del_timer(&conntrack->timeout))
1012 conntrack->timeout.function((unsigned long)
1013 conntrack);
1014 return NF_ACCEPT;
1016 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
1017 && (old_state == TCP_CONNTRACK_SYN_RECV
1018 || old_state == TCP_CONNTRACK_ESTABLISHED)
1019 && new_state == TCP_CONNTRACK_ESTABLISHED) {
1020 /* Set ASSURED if we see see valid ack in ESTABLISHED
1021 after SYN_RECV or a valid answer for a picked up
1022 connection. */
1023 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1024 nf_conntrack_event_cache(IPCT_STATUS, skb);
1026 nf_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1028 return NF_ACCEPT;
1031 /* Called when a new connection for this protocol found. */
1032 static int tcp_new(struct nf_conn *conntrack,
1033 const struct sk_buff *skb,
1034 unsigned int dataoff)
1036 enum tcp_conntrack new_state;
1037 struct tcphdr *th, _tcph;
1038 #ifdef DEBUGP_VARS
1039 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1040 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1041 #endif
1043 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1044 BUG_ON(th == NULL);
1046 /* Don't need lock here: this conntrack not in circulation yet */
1047 new_state
1048 = tcp_conntracks[0][get_conntrack_index(th)]
1049 [TCP_CONNTRACK_NONE];
1051 /* Invalid: delete conntrack */
1052 if (new_state >= TCP_CONNTRACK_MAX) {
1053 DEBUGP("nf_ct_tcp: invalid new deleting.\n");
1054 return 0;
1057 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1058 /* SYN packet */
1059 conntrack->proto.tcp.seen[0].td_end =
1060 segment_seq_plus_len(ntohl(th->seq), skb->len,
1061 dataoff, th);
1062 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1063 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1064 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1065 conntrack->proto.tcp.seen[0].td_maxend =
1066 conntrack->proto.tcp.seen[0].td_end;
1068 tcp_options(skb, dataoff, th, &conntrack->proto.tcp.seen[0]);
1069 conntrack->proto.tcp.seen[1].flags = 0;
1070 } else if (nf_ct_tcp_loose == 0) {
1071 /* Don't try to pick up connections. */
1072 return 0;
1073 } else {
1075 * We are in the middle of a connection,
1076 * its history is lost for us.
1077 * Let's try to use the data from the packet.
1079 conntrack->proto.tcp.seen[0].td_end =
1080 segment_seq_plus_len(ntohl(th->seq), skb->len,
1081 dataoff, th);
1082 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1083 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1084 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1085 conntrack->proto.tcp.seen[0].td_maxend =
1086 conntrack->proto.tcp.seen[0].td_end +
1087 conntrack->proto.tcp.seen[0].td_maxwin;
1088 conntrack->proto.tcp.seen[0].td_scale = 0;
1090 /* We assume SACK and liberal window checking to handle
1091 * window scaling */
1092 conntrack->proto.tcp.seen[0].flags =
1093 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1094 IP_CT_TCP_FLAG_BE_LIBERAL;
1097 conntrack->proto.tcp.seen[1].td_end = 0;
1098 conntrack->proto.tcp.seen[1].td_maxend = 0;
1099 conntrack->proto.tcp.seen[1].td_maxwin = 1;
1100 conntrack->proto.tcp.seen[1].td_scale = 0;
1102 /* tcp_packet will set them */
1103 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1104 conntrack->proto.tcp.last_index = TCP_NONE_SET;
1106 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1107 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1108 sender->td_end, sender->td_maxend, sender->td_maxwin,
1109 sender->td_scale,
1110 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1111 receiver->td_scale);
1112 return 1;
1115 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1117 #include <linux/netfilter/nfnetlink.h>
1118 #include <linux/netfilter/nfnetlink_conntrack.h>
1120 static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
1121 const struct nf_conn *ct)
1123 struct nfattr *nest_parms;
1124 struct nf_ct_tcp_flags tmp = {};
1126 read_lock_bh(&tcp_lock);
1127 nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
1128 NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
1129 &ct->proto.tcp.state);
1131 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL, sizeof(u_int8_t),
1132 &ct->proto.tcp.seen[0].td_scale);
1134 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY, sizeof(u_int8_t),
1135 &ct->proto.tcp.seen[1].td_scale);
1137 tmp.flags = ct->proto.tcp.seen[0].flags;
1138 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1139 sizeof(struct nf_ct_tcp_flags), &tmp);
1141 tmp.flags = ct->proto.tcp.seen[1].flags;
1142 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1143 sizeof(struct nf_ct_tcp_flags), &tmp);
1144 read_unlock_bh(&tcp_lock);
1146 NFA_NEST_END(skb, nest_parms);
1148 return 0;
1150 nfattr_failure:
1151 read_unlock_bh(&tcp_lock);
1152 return -1;
1155 static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = {
1156 [CTA_PROTOINFO_TCP_STATE-1] = sizeof(u_int8_t),
1157 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] = sizeof(u_int8_t),
1158 [CTA_PROTOINFO_TCP_WSCALE_REPLY-1] = sizeof(u_int8_t),
1159 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1] = sizeof(struct nf_ct_tcp_flags),
1160 [CTA_PROTOINFO_TCP_FLAGS_REPLY-1] = sizeof(struct nf_ct_tcp_flags)
1163 static int nfattr_to_tcp(struct nfattr *cda[], struct nf_conn *ct)
1165 struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
1166 struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
1168 /* updates could not contain anything about the private
1169 * protocol info, in that case skip the parsing */
1170 if (!attr)
1171 return 0;
1173 nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
1175 if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp))
1176 return -EINVAL;
1178 if (!tb[CTA_PROTOINFO_TCP_STATE-1])
1179 return -EINVAL;
1181 write_lock_bh(&tcp_lock);
1182 ct->proto.tcp.state =
1183 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
1185 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]) {
1186 struct nf_ct_tcp_flags *attr =
1187 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]);
1188 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1189 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1192 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]) {
1193 struct nf_ct_tcp_flags *attr =
1194 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]);
1195 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1196 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1199 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] &&
1200 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1] &&
1201 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1202 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1203 ct->proto.tcp.seen[0].td_scale = *(u_int8_t *)
1204 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1]);
1205 ct->proto.tcp.seen[1].td_scale = *(u_int8_t *)
1206 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1]);
1208 write_unlock_bh(&tcp_lock);
1210 return 0;
1212 #endif
1214 #ifdef CONFIG_SYSCTL
1215 static unsigned int tcp_sysctl_table_users;
1216 static struct ctl_table_header *tcp_sysctl_header;
1217 static struct ctl_table tcp_sysctl_table[] = {
1219 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1220 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1221 .data = &nf_ct_tcp_timeout_syn_sent,
1222 .maxlen = sizeof(unsigned int),
1223 .mode = 0644,
1224 .proc_handler = &proc_dointvec_jiffies,
1227 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1228 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1229 .data = &nf_ct_tcp_timeout_syn_recv,
1230 .maxlen = sizeof(unsigned int),
1231 .mode = 0644,
1232 .proc_handler = &proc_dointvec_jiffies,
1235 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1236 .procname = "nf_conntrack_tcp_timeout_established",
1237 .data = &nf_ct_tcp_timeout_established,
1238 .maxlen = sizeof(unsigned int),
1239 .mode = 0644,
1240 .proc_handler = &proc_dointvec_jiffies,
1243 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1244 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1245 .data = &nf_ct_tcp_timeout_fin_wait,
1246 .maxlen = sizeof(unsigned int),
1247 .mode = 0644,
1248 .proc_handler = &proc_dointvec_jiffies,
1251 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1252 .procname = "nf_conntrack_tcp_timeout_close_wait",
1253 .data = &nf_ct_tcp_timeout_close_wait,
1254 .maxlen = sizeof(unsigned int),
1255 .mode = 0644,
1256 .proc_handler = &proc_dointvec_jiffies,
1259 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1260 .procname = "nf_conntrack_tcp_timeout_last_ack",
1261 .data = &nf_ct_tcp_timeout_last_ack,
1262 .maxlen = sizeof(unsigned int),
1263 .mode = 0644,
1264 .proc_handler = &proc_dointvec_jiffies,
1267 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1268 .procname = "nf_conntrack_tcp_timeout_time_wait",
1269 .data = &nf_ct_tcp_timeout_time_wait,
1270 .maxlen = sizeof(unsigned int),
1271 .mode = 0644,
1272 .proc_handler = &proc_dointvec_jiffies,
1275 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1276 .procname = "nf_conntrack_tcp_timeout_close",
1277 .data = &nf_ct_tcp_timeout_close,
1278 .maxlen = sizeof(unsigned int),
1279 .mode = 0644,
1280 .proc_handler = &proc_dointvec_jiffies,
1283 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1284 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1285 .data = &nf_ct_tcp_timeout_max_retrans,
1286 .maxlen = sizeof(unsigned int),
1287 .mode = 0644,
1288 .proc_handler = &proc_dointvec_jiffies,
1291 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1292 .procname = "nf_conntrack_tcp_loose",
1293 .data = &nf_ct_tcp_loose,
1294 .maxlen = sizeof(unsigned int),
1295 .mode = 0644,
1296 .proc_handler = &proc_dointvec,
1299 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1300 .procname = "nf_conntrack_tcp_be_liberal",
1301 .data = &nf_ct_tcp_be_liberal,
1302 .maxlen = sizeof(unsigned int),
1303 .mode = 0644,
1304 .proc_handler = &proc_dointvec,
1307 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1308 .procname = "nf_conntrack_tcp_max_retrans",
1309 .data = &nf_ct_tcp_max_retrans,
1310 .maxlen = sizeof(unsigned int),
1311 .mode = 0644,
1312 .proc_handler = &proc_dointvec,
1315 .ctl_name = 0
1319 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1320 static struct ctl_table tcp_compat_sysctl_table[] = {
1322 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1323 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1324 .data = &nf_ct_tcp_timeout_syn_sent,
1325 .maxlen = sizeof(unsigned int),
1326 .mode = 0644,
1327 .proc_handler = &proc_dointvec_jiffies,
1330 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1331 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1332 .data = &nf_ct_tcp_timeout_syn_recv,
1333 .maxlen = sizeof(unsigned int),
1334 .mode = 0644,
1335 .proc_handler = &proc_dointvec_jiffies,
1338 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1339 .procname = "ip_conntrack_tcp_timeout_established",
1340 .data = &nf_ct_tcp_timeout_established,
1341 .maxlen = sizeof(unsigned int),
1342 .mode = 0644,
1343 .proc_handler = &proc_dointvec_jiffies,
1346 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1347 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1348 .data = &nf_ct_tcp_timeout_fin_wait,
1349 .maxlen = sizeof(unsigned int),
1350 .mode = 0644,
1351 .proc_handler = &proc_dointvec_jiffies,
1354 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1355 .procname = "ip_conntrack_tcp_timeout_close_wait",
1356 .data = &nf_ct_tcp_timeout_close_wait,
1357 .maxlen = sizeof(unsigned int),
1358 .mode = 0644,
1359 .proc_handler = &proc_dointvec_jiffies,
1362 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1363 .procname = "ip_conntrack_tcp_timeout_last_ack",
1364 .data = &nf_ct_tcp_timeout_last_ack,
1365 .maxlen = sizeof(unsigned int),
1366 .mode = 0644,
1367 .proc_handler = &proc_dointvec_jiffies,
1370 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1371 .procname = "ip_conntrack_tcp_timeout_time_wait",
1372 .data = &nf_ct_tcp_timeout_time_wait,
1373 .maxlen = sizeof(unsigned int),
1374 .mode = 0644,
1375 .proc_handler = &proc_dointvec_jiffies,
1378 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1379 .procname = "ip_conntrack_tcp_timeout_close",
1380 .data = &nf_ct_tcp_timeout_close,
1381 .maxlen = sizeof(unsigned int),
1382 .mode = 0644,
1383 .proc_handler = &proc_dointvec_jiffies,
1386 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1387 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1388 .data = &nf_ct_tcp_timeout_max_retrans,
1389 .maxlen = sizeof(unsigned int),
1390 .mode = 0644,
1391 .proc_handler = &proc_dointvec_jiffies,
1394 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1395 .procname = "ip_conntrack_tcp_loose",
1396 .data = &nf_ct_tcp_loose,
1397 .maxlen = sizeof(unsigned int),
1398 .mode = 0644,
1399 .proc_handler = &proc_dointvec,
1402 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1403 .procname = "ip_conntrack_tcp_be_liberal",
1404 .data = &nf_ct_tcp_be_liberal,
1405 .maxlen = sizeof(unsigned int),
1406 .mode = 0644,
1407 .proc_handler = &proc_dointvec,
1410 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1411 .procname = "ip_conntrack_tcp_max_retrans",
1412 .data = &nf_ct_tcp_max_retrans,
1413 .maxlen = sizeof(unsigned int),
1414 .mode = 0644,
1415 .proc_handler = &proc_dointvec,
1418 .ctl_name = 0
1421 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1422 #endif /* CONFIG_SYSCTL */
1424 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 =
1426 .l3proto = PF_INET,
1427 .l4proto = IPPROTO_TCP,
1428 .name = "tcp",
1429 .pkt_to_tuple = tcp_pkt_to_tuple,
1430 .invert_tuple = tcp_invert_tuple,
1431 .print_tuple = tcp_print_tuple,
1432 .print_conntrack = tcp_print_conntrack,
1433 .packet = tcp_packet,
1434 .new = tcp_new,
1435 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
1436 .error = NULL,
1437 #else
1438 .error = tcp_error,
1439 #endif
1440 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1441 .to_nfattr = tcp_to_nfattr,
1442 .from_nfattr = nfattr_to_tcp,
1443 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1444 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1445 #endif
1446 #ifdef CONFIG_SYSCTL
1447 .ctl_table_users = &tcp_sysctl_table_users,
1448 .ctl_table_header = &tcp_sysctl_header,
1449 .ctl_table = tcp_sysctl_table,
1450 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1451 .ctl_compat_table = tcp_compat_sysctl_table,
1452 #endif
1453 #endif
1455 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1457 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 =
1459 .l3proto = PF_INET6,
1460 .l4proto = IPPROTO_TCP,
1461 .name = "tcp",
1462 .pkt_to_tuple = tcp_pkt_to_tuple,
1463 .invert_tuple = tcp_invert_tuple,
1464 .print_tuple = tcp_print_tuple,
1465 .print_conntrack = tcp_print_conntrack,
1466 .packet = tcp_packet,
1467 .new = tcp_new,
1468 .error = tcp_error,
1469 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1470 .to_nfattr = tcp_to_nfattr,
1471 .from_nfattr = nfattr_to_tcp,
1472 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1473 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1474 #endif
1475 #ifdef CONFIG_SYSCTL
1476 .ctl_table_users = &tcp_sysctl_table_users,
1477 .ctl_table_header = &tcp_sysctl_header,
1478 .ctl_table = tcp_sysctl_table,
1479 #endif
1481 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);