sound: seq_midi_event: fix decoding of (N)RPN events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / nf_conntrack_proto_tcp.c
blob56ac4ee77a1d8f273713adcd5d0747ee6125721b
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.h>
12 #include <linux/in.h>
13 #include <linux/tcp.h>
14 #include <linux/spinlock.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <asm/unaligned.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>
28 #include <net/netfilter/nf_log.h>
30 /* Protects ct->proto.tcp */
31 static DEFINE_RWLOCK(tcp_lock);
33 /* "Be conservative in what you do,
34 be liberal in what you accept from others."
35 If it's non-zero, we mark only out of window RST segments as INVALID. */
36 static int nf_ct_tcp_be_liberal __read_mostly = 0;
38 /* If it is set to zero, we disable picking up already established
39 connections. */
40 static int nf_ct_tcp_loose __read_mostly = 1;
42 /* Max number of the retransmitted packets without receiving an (acceptable)
43 ACK from the destination. If this number is reached, a shorter timer
44 will be started. */
45 static int nf_ct_tcp_max_retrans __read_mostly = 3;
47 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
48 closely. They're more complex. --RR */
50 static const char *const tcp_conntrack_names[] = {
51 "NONE",
52 "SYN_SENT",
53 "SYN_RECV",
54 "ESTABLISHED",
55 "FIN_WAIT",
56 "CLOSE_WAIT",
57 "LAST_ACK",
58 "TIME_WAIT",
59 "CLOSE",
60 "LISTEN"
63 #define SECS * HZ
64 #define MINS * 60 SECS
65 #define HOURS * 60 MINS
66 #define DAYS * 24 HOURS
68 /* RFC1122 says the R2 limit should be at least 100 seconds.
69 Linux uses 15 packets as limit, which corresponds
70 to ~13-30min depending on RTO. */
71 static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
72 static unsigned int nf_ct_tcp_timeout_unacknowledged __read_mostly = 5 MINS;
74 static unsigned int tcp_timeouts[TCP_CONNTRACK_MAX] __read_mostly = {
75 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
76 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
77 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
78 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
79 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
80 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
81 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
82 [TCP_CONNTRACK_CLOSE] = 10 SECS,
85 #define sNO TCP_CONNTRACK_NONE
86 #define sSS TCP_CONNTRACK_SYN_SENT
87 #define sSR TCP_CONNTRACK_SYN_RECV
88 #define sES TCP_CONNTRACK_ESTABLISHED
89 #define sFW TCP_CONNTRACK_FIN_WAIT
90 #define sCW TCP_CONNTRACK_CLOSE_WAIT
91 #define sLA TCP_CONNTRACK_LAST_ACK
92 #define sTW TCP_CONNTRACK_TIME_WAIT
93 #define sCL TCP_CONNTRACK_CLOSE
94 #define sLI TCP_CONNTRACK_LISTEN
95 #define sIV TCP_CONNTRACK_MAX
96 #define sIG TCP_CONNTRACK_IGNORE
98 /* What TCP flags are set from RST/SYN/FIN/ACK. */
99 enum tcp_bit_set {
100 TCP_SYN_SET,
101 TCP_SYNACK_SET,
102 TCP_FIN_SET,
103 TCP_ACK_SET,
104 TCP_RST_SET,
105 TCP_NONE_SET,
109 * The TCP state transition table needs a few words...
111 * We are the man in the middle. All the packets go through us
112 * but might get lost in transit to the destination.
113 * It is assumed that the destinations can't receive segments
114 * we haven't seen.
116 * The checked segment is in window, but our windows are *not*
117 * equivalent with the ones of the sender/receiver. We always
118 * try to guess the state of the current sender.
120 * The meaning of the states are:
122 * NONE: initial state
123 * SYN_SENT: SYN-only packet seen
124 * SYN_RECV: SYN-ACK packet seen
125 * ESTABLISHED: ACK packet seen
126 * FIN_WAIT: FIN packet seen
127 * CLOSE_WAIT: ACK seen (after FIN)
128 * LAST_ACK: FIN seen (after FIN)
129 * TIME_WAIT: last ACK seen
130 * CLOSE: closed connection (RST)
132 * LISTEN state is not used.
134 * Packets marked as IGNORED (sIG):
135 * if they may be either invalid or valid
136 * and the receiver may send back a connection
137 * closing RST or a SYN/ACK.
139 * Packets marked as INVALID (sIV):
140 * if they are invalid
141 * or we do not support the request (simultaneous open)
143 static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
145 /* ORIGINAL */
146 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
147 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
149 * sNO -> sSS Initialize a new connection
150 * sSS -> sSS Retransmitted SYN
151 * sSR -> sIG Late retransmitted SYN?
152 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
153 * are errors. Receiver will reply with RST
154 * and close the connection.
155 * Or we are not in sync and hold a dead connection.
156 * sFW -> sIG
157 * sCW -> sIG
158 * sLA -> sIG
159 * sTW -> sSS Reopened connection (RFC 1122).
160 * sCL -> sSS
162 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
163 /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
165 * A SYN/ACK from the client is always invalid:
166 * - either it tries to set up a simultaneous open, which is
167 * not supported;
168 * - or the firewall has just been inserted between the two hosts
169 * during the session set-up. The SYN will be retransmitted
170 * by the true client (or it'll time out).
172 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
173 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
175 * sNO -> sIV Too late and no reason to do anything...
176 * sSS -> sIV Client migth not send FIN in this state:
177 * we enforce waiting for a SYN/ACK reply first.
178 * sSR -> sFW Close started.
179 * sES -> sFW
180 * sFW -> sLA FIN seen in both directions, waiting for
181 * the last ACK.
182 * Migth be a retransmitted FIN as well...
183 * sCW -> sLA
184 * sLA -> sLA Retransmitted FIN. Remain in the same state.
185 * sTW -> sTW
186 * sCL -> sCL
188 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
189 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
191 * sNO -> sES Assumed.
192 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
193 * sSR -> sES Established state is reached.
194 * sES -> sES :-)
195 * sFW -> sCW Normal close request answered by ACK.
196 * sCW -> sCW
197 * sLA -> sTW Last ACK detected.
198 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
199 * sCL -> sCL
201 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
202 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
203 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
206 /* REPLY */
207 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
208 /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
210 * sNO -> sIV Never reached.
211 * sSS -> sIV Simultaneous open, not supported
212 * sSR -> sIV Simultaneous open, not supported.
213 * sES -> sIV Server may not initiate a connection.
214 * sFW -> sIV
215 * sCW -> sIV
216 * sLA -> sIV
217 * sTW -> sIV Reopened connection, but server may not do it.
218 * sCL -> sIV
220 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
221 /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
223 * sSS -> sSR Standard open.
224 * sSR -> sSR Retransmitted SYN/ACK.
225 * sES -> sIG Late retransmitted SYN/ACK?
226 * sFW -> sIG Might be SYN/ACK answering ignored SYN
227 * sCW -> sIG
228 * sLA -> sIG
229 * sTW -> sIG
230 * sCL -> sIG
232 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
233 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
235 * sSS -> sIV Server might not send FIN in this state.
236 * sSR -> sFW Close started.
237 * sES -> sFW
238 * sFW -> sLA FIN seen in both directions.
239 * sCW -> sLA
240 * sLA -> sLA Retransmitted FIN.
241 * sTW -> sTW
242 * sCL -> sCL
244 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
245 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
247 * sSS -> sIG Might be a half-open connection.
248 * sSR -> sSR Might answer late resent SYN.
249 * sES -> sES :-)
250 * sFW -> sCW Normal close request answered by ACK.
251 * sCW -> sCW
252 * sLA -> sTW Last ACK detected.
253 * sTW -> sTW Retransmitted last ACK.
254 * sCL -> sCL
256 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
257 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
258 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
262 static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
263 struct nf_conntrack_tuple *tuple)
265 const struct tcphdr *hp;
266 struct tcphdr _hdr;
268 /* Actually only need first 8 bytes. */
269 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
270 if (hp == NULL)
271 return false;
273 tuple->src.u.tcp.port = hp->source;
274 tuple->dst.u.tcp.port = hp->dest;
276 return true;
279 static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
280 const struct nf_conntrack_tuple *orig)
282 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
283 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
284 return true;
287 /* Print out the per-protocol part of the tuple. */
288 static int tcp_print_tuple(struct seq_file *s,
289 const struct nf_conntrack_tuple *tuple)
291 return seq_printf(s, "sport=%hu dport=%hu ",
292 ntohs(tuple->src.u.tcp.port),
293 ntohs(tuple->dst.u.tcp.port));
296 /* Print out the private part of the conntrack. */
297 static int tcp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
299 enum tcp_conntrack state;
301 read_lock_bh(&tcp_lock);
302 state = ct->proto.tcp.state;
303 read_unlock_bh(&tcp_lock);
305 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
308 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
310 if (tcph->rst) return TCP_RST_SET;
311 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
312 else if (tcph->fin) return TCP_FIN_SET;
313 else if (tcph->ack) return TCP_ACK_SET;
314 else return TCP_NONE_SET;
317 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
318 in IP Filter' by Guido van Rooij.
320 http://www.nluug.nl/events/sane2000/papers.html
321 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
323 The boundaries and the conditions are changed according to RFC793:
324 the packet must intersect the window (i.e. segments may be
325 after the right or before the left edge) and thus receivers may ACK
326 segments after the right edge of the window.
328 td_maxend = max(sack + max(win,1)) seen in reply packets
329 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
330 td_maxwin += seq + len - sender.td_maxend
331 if seq + len > sender.td_maxend
332 td_end = max(seq + len) seen in sent packets
334 I. Upper bound for valid data: seq <= sender.td_maxend
335 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
336 III. Upper bound for valid (s)ack: sack <= receiver.td_end
337 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
339 where sack is the highest right edge of sack block found in the packet
340 or ack in the case of packet without SACK option.
342 The upper bound limit for a valid (s)ack is not ignored -
343 we doesn't have to deal with fragments.
346 static inline __u32 segment_seq_plus_len(__u32 seq,
347 size_t len,
348 unsigned int dataoff,
349 const struct tcphdr *tcph)
351 /* XXX Should I use payload length field in IP/IPv6 header ?
352 * - YK */
353 return (seq + len - dataoff - tcph->doff*4
354 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
357 /* Fixme: what about big packets? */
358 #define MAXACKWINCONST 66000
359 #define MAXACKWINDOW(sender) \
360 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
361 : MAXACKWINCONST)
364 * Simplified tcp_parse_options routine from tcp_input.c
366 static void tcp_options(const struct sk_buff *skb,
367 unsigned int dataoff,
368 const struct tcphdr *tcph,
369 struct ip_ct_tcp_state *state)
371 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
372 const unsigned char *ptr;
373 int length = (tcph->doff*4) - sizeof(struct tcphdr);
375 if (!length)
376 return;
378 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
379 length, buff);
380 BUG_ON(ptr == NULL);
382 state->td_scale =
383 state->flags = 0;
385 while (length > 0) {
386 int opcode=*ptr++;
387 int opsize;
389 switch (opcode) {
390 case TCPOPT_EOL:
391 return;
392 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
393 length--;
394 continue;
395 default:
396 opsize=*ptr++;
397 if (opsize < 2) /* "silly options" */
398 return;
399 if (opsize > length)
400 break; /* don't parse partial options */
402 if (opcode == TCPOPT_SACK_PERM
403 && opsize == TCPOLEN_SACK_PERM)
404 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
405 else if (opcode == TCPOPT_WINDOW
406 && opsize == TCPOLEN_WINDOW) {
407 state->td_scale = *(u_int8_t *)ptr;
409 if (state->td_scale > 14) {
410 /* See RFC1323 */
411 state->td_scale = 14;
413 state->flags |=
414 IP_CT_TCP_FLAG_WINDOW_SCALE;
416 ptr += opsize - 2;
417 length -= opsize;
422 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
423 const struct tcphdr *tcph, __u32 *sack)
425 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
426 const unsigned char *ptr;
427 int length = (tcph->doff*4) - sizeof(struct tcphdr);
428 __u32 tmp;
430 if (!length)
431 return;
433 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
434 length, buff);
435 BUG_ON(ptr == NULL);
437 /* Fast path for timestamp-only option */
438 if (length == TCPOLEN_TSTAMP_ALIGNED*4
439 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
440 | (TCPOPT_NOP << 16)
441 | (TCPOPT_TIMESTAMP << 8)
442 | TCPOLEN_TIMESTAMP))
443 return;
445 while (length > 0) {
446 int opcode = *ptr++;
447 int opsize, i;
449 switch (opcode) {
450 case TCPOPT_EOL:
451 return;
452 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
453 length--;
454 continue;
455 default:
456 opsize = *ptr++;
457 if (opsize < 2) /* "silly options" */
458 return;
459 if (opsize > length)
460 break; /* don't parse partial options */
462 if (opcode == TCPOPT_SACK
463 && opsize >= (TCPOLEN_SACK_BASE
464 + TCPOLEN_SACK_PERBLOCK)
465 && !((opsize - TCPOLEN_SACK_BASE)
466 % TCPOLEN_SACK_PERBLOCK)) {
467 for (i = 0;
468 i < (opsize - TCPOLEN_SACK_BASE);
469 i += TCPOLEN_SACK_PERBLOCK) {
470 tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
472 if (after(tmp, *sack))
473 *sack = tmp;
475 return;
477 ptr += opsize - 2;
478 length -= opsize;
483 static bool tcp_in_window(const struct nf_conn *ct,
484 struct ip_ct_tcp *state,
485 enum ip_conntrack_dir dir,
486 unsigned int index,
487 const struct sk_buff *skb,
488 unsigned int dataoff,
489 const struct tcphdr *tcph,
490 u_int8_t pf)
492 struct net *net = nf_ct_net(ct);
493 struct ip_ct_tcp_state *sender = &state->seen[dir];
494 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
495 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
496 __u32 seq, ack, sack, end, win, swin;
497 bool res;
500 * Get the required data from the packet.
502 seq = ntohl(tcph->seq);
503 ack = sack = ntohl(tcph->ack_seq);
504 win = ntohs(tcph->window);
505 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
507 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
508 tcp_sack(skb, dataoff, tcph, &sack);
510 pr_debug("tcp_in_window: START\n");
511 pr_debug("tcp_in_window: ");
512 nf_ct_dump_tuple(tuple);
513 pr_debug("seq=%u ack=%u sack=%u win=%u end=%u\n",
514 seq, ack, sack, win, end);
515 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
516 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
517 sender->td_end, sender->td_maxend, sender->td_maxwin,
518 sender->td_scale,
519 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
520 receiver->td_scale);
522 if (sender->td_end == 0) {
524 * Initialize sender data.
526 if (tcph->syn && tcph->ack) {
528 * Outgoing SYN-ACK in reply to a SYN.
530 sender->td_end =
531 sender->td_maxend = end;
532 sender->td_maxwin = (win == 0 ? 1 : win);
534 tcp_options(skb, dataoff, tcph, sender);
536 * RFC 1323:
537 * Both sides must send the Window Scale option
538 * to enable window scaling in either direction.
540 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
541 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
542 sender->td_scale =
543 receiver->td_scale = 0;
544 } else {
546 * We are in the middle of a connection,
547 * its history is lost for us.
548 * Let's try to use the data from the packet.
550 sender->td_end = end;
551 sender->td_maxwin = (win == 0 ? 1 : win);
552 sender->td_maxend = end + sender->td_maxwin;
554 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
555 && dir == IP_CT_DIR_ORIGINAL)
556 || (state->state == TCP_CONNTRACK_SYN_RECV
557 && dir == IP_CT_DIR_REPLY))
558 && after(end, sender->td_end)) {
560 * RFC 793: "if a TCP is reinitialized ... then it need
561 * not wait at all; it must only be sure to use sequence
562 * numbers larger than those recently used."
564 sender->td_end =
565 sender->td_maxend = end;
566 sender->td_maxwin = (win == 0 ? 1 : win);
568 tcp_options(skb, dataoff, tcph, sender);
571 if (!(tcph->ack)) {
573 * If there is no ACK, just pretend it was set and OK.
575 ack = sack = receiver->td_end;
576 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
577 (TCP_FLAG_ACK|TCP_FLAG_RST))
578 && (ack == 0)) {
580 * Broken TCP stacks, that set ACK in RST packets as well
581 * with zero ack value.
583 ack = sack = receiver->td_end;
586 if (seq == end
587 && (!tcph->rst
588 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
590 * Packets contains no data: we assume it is valid
591 * and check the ack value only.
592 * However RST segments are always validated by their
593 * SEQ number, except when seq == 0 (reset sent answering
594 * SYN.
596 seq = end = sender->td_end;
598 pr_debug("tcp_in_window: ");
599 nf_ct_dump_tuple(tuple);
600 pr_debug("seq=%u ack=%u sack =%u win=%u end=%u\n",
601 seq, ack, sack, win, end);
602 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
603 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
604 sender->td_end, sender->td_maxend, sender->td_maxwin,
605 sender->td_scale,
606 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
607 receiver->td_scale);
609 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
610 before(seq, sender->td_maxend + 1),
611 after(end, sender->td_end - receiver->td_maxwin - 1),
612 before(sack, receiver->td_end + 1),
613 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
615 if (before(seq, sender->td_maxend + 1) &&
616 after(end, sender->td_end - receiver->td_maxwin - 1) &&
617 before(sack, receiver->td_end + 1) &&
618 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
620 * Take into account window scaling (RFC 1323).
622 if (!tcph->syn)
623 win <<= sender->td_scale;
626 * Update sender data.
628 swin = win + (sack - ack);
629 if (sender->td_maxwin < swin)
630 sender->td_maxwin = swin;
631 if (after(end, sender->td_end)) {
632 sender->td_end = end;
633 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
636 * Update receiver data.
638 if (after(end, sender->td_maxend))
639 receiver->td_maxwin += end - sender->td_maxend;
640 if (after(sack + win, receiver->td_maxend - 1)) {
641 receiver->td_maxend = sack + win;
642 if (win == 0)
643 receiver->td_maxend++;
645 if (ack == receiver->td_end)
646 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
649 * Check retransmissions.
651 if (index == TCP_ACK_SET) {
652 if (state->last_dir == dir
653 && state->last_seq == seq
654 && state->last_ack == ack
655 && state->last_end == end
656 && state->last_win == win)
657 state->retrans++;
658 else {
659 state->last_dir = dir;
660 state->last_seq = seq;
661 state->last_ack = ack;
662 state->last_end = end;
663 state->last_win = win;
664 state->retrans = 0;
667 res = true;
668 } else {
669 res = false;
670 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
671 nf_ct_tcp_be_liberal)
672 res = true;
673 if (!res && LOG_INVALID(net, IPPROTO_TCP))
674 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
675 "nf_ct_tcp: %s ",
676 before(seq, sender->td_maxend + 1) ?
677 after(end, sender->td_end - receiver->td_maxwin - 1) ?
678 before(sack, receiver->td_end + 1) ?
679 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
680 : "ACK is under the lower bound (possible overly delayed ACK)"
681 : "ACK is over the upper bound (ACKed data not seen yet)"
682 : "SEQ is under the lower bound (already ACKed data retransmitted)"
683 : "SEQ is over the upper bound (over the window of the receiver)");
686 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
687 "receiver end=%u maxend=%u maxwin=%u\n",
688 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
689 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
691 return res;
694 #ifdef CONFIG_NF_NAT_NEEDED
695 /* Update sender->td_end after NAT successfully mangled the packet */
696 /* Caller must linearize skb at tcp header. */
697 void nf_conntrack_tcp_update(const struct sk_buff *skb,
698 unsigned int dataoff,
699 struct nf_conn *ct,
700 int dir)
702 const struct tcphdr *tcph = (const void *)skb->data + dataoff;
703 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[dir];
704 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[!dir];
705 __u32 end;
707 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
709 write_lock_bh(&tcp_lock);
711 * We have to worry for the ack in the reply packet only...
713 if (after(end, ct->proto.tcp.seen[dir].td_end))
714 ct->proto.tcp.seen[dir].td_end = end;
715 ct->proto.tcp.last_end = end;
716 write_unlock_bh(&tcp_lock);
717 pr_debug("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
718 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
719 sender->td_end, sender->td_maxend, sender->td_maxwin,
720 sender->td_scale,
721 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
722 receiver->td_scale);
724 EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
725 #endif
727 #define TH_FIN 0x01
728 #define TH_SYN 0x02
729 #define TH_RST 0x04
730 #define TH_PUSH 0x08
731 #define TH_ACK 0x10
732 #define TH_URG 0x20
733 #define TH_ECE 0x40
734 #define TH_CWR 0x80
736 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
737 static const u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
739 [TH_SYN] = 1,
740 [TH_SYN|TH_URG] = 1,
741 [TH_SYN|TH_ACK] = 1,
742 [TH_RST] = 1,
743 [TH_RST|TH_ACK] = 1,
744 [TH_FIN|TH_ACK] = 1,
745 [TH_FIN|TH_ACK|TH_URG] = 1,
746 [TH_ACK] = 1,
747 [TH_ACK|TH_URG] = 1,
750 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
751 static int tcp_error(struct net *net,
752 struct sk_buff *skb,
753 unsigned int dataoff,
754 enum ip_conntrack_info *ctinfo,
755 u_int8_t pf,
756 unsigned int hooknum)
758 const struct tcphdr *th;
759 struct tcphdr _tcph;
760 unsigned int tcplen = skb->len - dataoff;
761 u_int8_t tcpflags;
763 /* Smaller that minimal TCP header? */
764 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
765 if (th == NULL) {
766 if (LOG_INVALID(net, IPPROTO_TCP))
767 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
768 "nf_ct_tcp: short packet ");
769 return -NF_ACCEPT;
772 /* Not whole TCP header or malformed packet */
773 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
774 if (LOG_INVALID(net, IPPROTO_TCP))
775 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
776 "nf_ct_tcp: truncated/malformed packet ");
777 return -NF_ACCEPT;
780 /* Checksum invalid? Ignore.
781 * We skip checking packets on the outgoing path
782 * because the checksum is assumed to be correct.
784 /* FIXME: Source route IP option packets --RR */
785 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
786 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
787 if (LOG_INVALID(net, IPPROTO_TCP))
788 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
789 "nf_ct_tcp: bad TCP checksum ");
790 return -NF_ACCEPT;
793 /* Check TCP flags. */
794 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
795 if (!tcp_valid_flags[tcpflags]) {
796 if (LOG_INVALID(net, IPPROTO_TCP))
797 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
798 "nf_ct_tcp: invalid TCP flag combination ");
799 return -NF_ACCEPT;
802 return NF_ACCEPT;
805 /* Returns verdict for packet, or -1 for invalid. */
806 static int tcp_packet(struct nf_conn *ct,
807 const struct sk_buff *skb,
808 unsigned int dataoff,
809 enum ip_conntrack_info ctinfo,
810 u_int8_t pf,
811 unsigned int hooknum)
813 struct net *net = nf_ct_net(ct);
814 struct nf_conntrack_tuple *tuple;
815 enum tcp_conntrack new_state, old_state;
816 enum ip_conntrack_dir dir;
817 const struct tcphdr *th;
818 struct tcphdr _tcph;
819 unsigned long timeout;
820 unsigned int index;
822 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
823 BUG_ON(th == NULL);
825 write_lock_bh(&tcp_lock);
826 old_state = ct->proto.tcp.state;
827 dir = CTINFO2DIR(ctinfo);
828 index = get_conntrack_index(th);
829 new_state = tcp_conntracks[dir][index][old_state];
830 tuple = &ct->tuplehash[dir].tuple;
832 switch (new_state) {
833 case TCP_CONNTRACK_SYN_SENT:
834 if (old_state < TCP_CONNTRACK_TIME_WAIT)
835 break;
836 /* RFC 1122: "When a connection is closed actively,
837 * it MUST linger in TIME-WAIT state for a time 2xMSL
838 * (Maximum Segment Lifetime). However, it MAY accept
839 * a new SYN from the remote TCP to reopen the connection
840 * directly from TIME-WAIT state, if..."
841 * We ignore the conditions because we are in the
842 * TIME-WAIT state anyway.
844 * Handle aborted connections: we and the server
845 * think there is an existing connection but the client
846 * aborts it and starts a new one.
848 if (((ct->proto.tcp.seen[dir].flags
849 | ct->proto.tcp.seen[!dir].flags)
850 & IP_CT_TCP_FLAG_CLOSE_INIT)
851 || (ct->proto.tcp.last_dir == dir
852 && ct->proto.tcp.last_index == TCP_RST_SET)) {
853 /* Attempt to reopen a closed/aborted connection.
854 * Delete this connection and look up again. */
855 write_unlock_bh(&tcp_lock);
857 /* Only repeat if we can actually remove the timer.
858 * Destruction may already be in progress in process
859 * context and we must give it a chance to terminate.
861 if (nf_ct_kill(ct))
862 return -NF_REPEAT;
863 return NF_DROP;
865 /* Fall through */
866 case TCP_CONNTRACK_IGNORE:
867 /* Ignored packets:
869 * Our connection entry may be out of sync, so ignore
870 * packets which may signal the real connection between
871 * the client and the server.
873 * a) SYN in ORIGINAL
874 * b) SYN/ACK in REPLY
875 * c) ACK in reply direction after initial SYN in original.
877 * If the ignored packet is invalid, the receiver will send
878 * a RST we'll catch below.
880 if (index == TCP_SYNACK_SET
881 && ct->proto.tcp.last_index == TCP_SYN_SET
882 && ct->proto.tcp.last_dir != dir
883 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
884 /* b) This SYN/ACK acknowledges a SYN that we earlier
885 * ignored as invalid. This means that the client and
886 * the server are both in sync, while the firewall is
887 * not. We kill this session and block the SYN/ACK so
888 * that the client cannot but retransmit its SYN and
889 * thus initiate a clean new session.
891 write_unlock_bh(&tcp_lock);
892 if (LOG_INVALID(net, IPPROTO_TCP))
893 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
894 "nf_ct_tcp: killing out of sync session ");
895 nf_ct_kill(ct);
896 return NF_DROP;
898 ct->proto.tcp.last_index = index;
899 ct->proto.tcp.last_dir = dir;
900 ct->proto.tcp.last_seq = ntohl(th->seq);
901 ct->proto.tcp.last_end =
902 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
904 write_unlock_bh(&tcp_lock);
905 if (LOG_INVALID(net, IPPROTO_TCP))
906 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
907 "nf_ct_tcp: invalid packet ignored ");
908 return NF_ACCEPT;
909 case TCP_CONNTRACK_MAX:
910 /* Invalid packet */
911 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
912 dir, get_conntrack_index(th), old_state);
913 write_unlock_bh(&tcp_lock);
914 if (LOG_INVALID(net, IPPROTO_TCP))
915 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
916 "nf_ct_tcp: invalid state ");
917 return -NF_ACCEPT;
918 case TCP_CONNTRACK_CLOSE:
919 if (index == TCP_RST_SET
920 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
921 && ct->proto.tcp.last_index == TCP_SYN_SET)
922 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
923 && ct->proto.tcp.last_index == TCP_ACK_SET))
924 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
925 /* RST sent to invalid SYN or ACK we had let through
926 * at a) and c) above:
928 * a) SYN was in window then
929 * c) we hold a half-open connection.
931 * Delete our connection entry.
932 * We skip window checking, because packet might ACK
933 * segments we ignored. */
934 goto in_window;
936 /* Just fall through */
937 default:
938 /* Keep compilers happy. */
939 break;
942 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
943 skb, dataoff, th, pf)) {
944 write_unlock_bh(&tcp_lock);
945 return -NF_ACCEPT;
947 in_window:
948 /* From now on we have got in-window packets */
949 ct->proto.tcp.last_index = index;
950 ct->proto.tcp.last_dir = dir;
952 pr_debug("tcp_conntracks: ");
953 nf_ct_dump_tuple(tuple);
954 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
955 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
956 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
957 old_state, new_state);
959 ct->proto.tcp.state = new_state;
960 if (old_state != new_state
961 && new_state == TCP_CONNTRACK_FIN_WAIT)
962 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
964 if (ct->proto.tcp.retrans >= nf_ct_tcp_max_retrans &&
965 tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans)
966 timeout = nf_ct_tcp_timeout_max_retrans;
967 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
968 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
969 tcp_timeouts[new_state] > nf_ct_tcp_timeout_unacknowledged)
970 timeout = nf_ct_tcp_timeout_unacknowledged;
971 else
972 timeout = tcp_timeouts[new_state];
973 write_unlock_bh(&tcp_lock);
975 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, ct);
976 if (new_state != old_state)
977 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
979 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
980 /* If only reply is a RST, we can consider ourselves not to
981 have an established connection: this is a fairly common
982 problem case, so we can delete the conntrack
983 immediately. --RR */
984 if (th->rst) {
985 nf_ct_kill_acct(ct, ctinfo, skb);
986 return NF_ACCEPT;
988 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
989 && (old_state == TCP_CONNTRACK_SYN_RECV
990 || old_state == TCP_CONNTRACK_ESTABLISHED)
991 && new_state == TCP_CONNTRACK_ESTABLISHED) {
992 /* Set ASSURED if we see see valid ack in ESTABLISHED
993 after SYN_RECV or a valid answer for a picked up
994 connection. */
995 set_bit(IPS_ASSURED_BIT, &ct->status);
996 nf_conntrack_event_cache(IPCT_STATUS, ct);
998 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
1000 return NF_ACCEPT;
1003 /* Called when a new connection for this protocol found. */
1004 static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
1005 unsigned int dataoff)
1007 enum tcp_conntrack new_state;
1008 const struct tcphdr *th;
1009 struct tcphdr _tcph;
1010 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1011 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1013 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1014 BUG_ON(th == NULL);
1016 /* Don't need lock here: this conntrack not in circulation yet */
1017 new_state
1018 = tcp_conntracks[0][get_conntrack_index(th)]
1019 [TCP_CONNTRACK_NONE];
1021 /* Invalid: delete conntrack */
1022 if (new_state >= TCP_CONNTRACK_MAX) {
1023 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1024 return false;
1027 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1028 /* SYN packet */
1029 ct->proto.tcp.seen[0].td_end =
1030 segment_seq_plus_len(ntohl(th->seq), skb->len,
1031 dataoff, th);
1032 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1033 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1034 ct->proto.tcp.seen[0].td_maxwin = 1;
1035 ct->proto.tcp.seen[0].td_maxend =
1036 ct->proto.tcp.seen[0].td_end;
1038 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1039 ct->proto.tcp.seen[1].flags = 0;
1040 } else if (nf_ct_tcp_loose == 0) {
1041 /* Don't try to pick up connections. */
1042 return false;
1043 } else {
1045 * We are in the middle of a connection,
1046 * its history is lost for us.
1047 * Let's try to use the data from the packet.
1049 ct->proto.tcp.seen[0].td_end =
1050 segment_seq_plus_len(ntohl(th->seq), skb->len,
1051 dataoff, th);
1052 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1053 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1054 ct->proto.tcp.seen[0].td_maxwin = 1;
1055 ct->proto.tcp.seen[0].td_maxend =
1056 ct->proto.tcp.seen[0].td_end +
1057 ct->proto.tcp.seen[0].td_maxwin;
1058 ct->proto.tcp.seen[0].td_scale = 0;
1060 /* We assume SACK and liberal window checking to handle
1061 * window scaling */
1062 ct->proto.tcp.seen[0].flags =
1063 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1064 IP_CT_TCP_FLAG_BE_LIBERAL;
1067 ct->proto.tcp.seen[1].td_end = 0;
1068 ct->proto.tcp.seen[1].td_maxend = 0;
1069 ct->proto.tcp.seen[1].td_maxwin = 1;
1070 ct->proto.tcp.seen[1].td_scale = 0;
1072 /* tcp_packet will set them */
1073 ct->proto.tcp.state = TCP_CONNTRACK_NONE;
1074 ct->proto.tcp.last_index = TCP_NONE_SET;
1076 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1077 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1078 sender->td_end, sender->td_maxend, sender->td_maxwin,
1079 sender->td_scale,
1080 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1081 receiver->td_scale);
1082 return true;
1085 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1087 #include <linux/netfilter/nfnetlink.h>
1088 #include <linux/netfilter/nfnetlink_conntrack.h>
1090 static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1091 const struct nf_conn *ct)
1093 struct nlattr *nest_parms;
1094 struct nf_ct_tcp_flags tmp = {};
1096 read_lock_bh(&tcp_lock);
1097 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1098 if (!nest_parms)
1099 goto nla_put_failure;
1101 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state);
1103 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1104 ct->proto.tcp.seen[0].td_scale);
1106 NLA_PUT_U8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1107 ct->proto.tcp.seen[1].td_scale);
1109 tmp.flags = ct->proto.tcp.seen[0].flags;
1110 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1111 sizeof(struct nf_ct_tcp_flags), &tmp);
1113 tmp.flags = ct->proto.tcp.seen[1].flags;
1114 NLA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1115 sizeof(struct nf_ct_tcp_flags), &tmp);
1116 read_unlock_bh(&tcp_lock);
1118 nla_nest_end(skb, nest_parms);
1120 return 0;
1122 nla_put_failure:
1123 read_unlock_bh(&tcp_lock);
1124 return -1;
1127 static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1128 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1129 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1130 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1131 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1132 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
1135 static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1137 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1138 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1139 int err;
1141 /* updates could not contain anything about the private
1142 * protocol info, in that case skip the parsing */
1143 if (!pattr)
1144 return 0;
1146 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
1147 if (err < 0)
1148 return err;
1150 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1151 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1152 return -EINVAL;
1154 write_lock_bh(&tcp_lock);
1155 if (tb[CTA_PROTOINFO_TCP_STATE])
1156 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1158 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1159 struct nf_ct_tcp_flags *attr =
1160 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1161 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1162 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1165 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1166 struct nf_ct_tcp_flags *attr =
1167 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1168 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1169 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1172 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1173 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
1174 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1175 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1176 ct->proto.tcp.seen[0].td_scale =
1177 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1178 ct->proto.tcp.seen[1].td_scale =
1179 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
1181 write_unlock_bh(&tcp_lock);
1183 return 0;
1185 #endif
1187 #ifdef CONFIG_SYSCTL
1188 static unsigned int tcp_sysctl_table_users;
1189 static struct ctl_table_header *tcp_sysctl_header;
1190 static struct ctl_table tcp_sysctl_table[] = {
1192 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1193 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1194 .maxlen = sizeof(unsigned int),
1195 .mode = 0644,
1196 .proc_handler = proc_dointvec_jiffies,
1199 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1200 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1201 .maxlen = sizeof(unsigned int),
1202 .mode = 0644,
1203 .proc_handler = proc_dointvec_jiffies,
1206 .procname = "nf_conntrack_tcp_timeout_established",
1207 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1208 .maxlen = sizeof(unsigned int),
1209 .mode = 0644,
1210 .proc_handler = proc_dointvec_jiffies,
1213 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1214 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1215 .maxlen = sizeof(unsigned int),
1216 .mode = 0644,
1217 .proc_handler = proc_dointvec_jiffies,
1220 .procname = "nf_conntrack_tcp_timeout_close_wait",
1221 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1222 .maxlen = sizeof(unsigned int),
1223 .mode = 0644,
1224 .proc_handler = proc_dointvec_jiffies,
1227 .procname = "nf_conntrack_tcp_timeout_last_ack",
1228 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1229 .maxlen = sizeof(unsigned int),
1230 .mode = 0644,
1231 .proc_handler = proc_dointvec_jiffies,
1234 .procname = "nf_conntrack_tcp_timeout_time_wait",
1235 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1236 .maxlen = sizeof(unsigned int),
1237 .mode = 0644,
1238 .proc_handler = proc_dointvec_jiffies,
1241 .procname = "nf_conntrack_tcp_timeout_close",
1242 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1243 .maxlen = sizeof(unsigned int),
1244 .mode = 0644,
1245 .proc_handler = proc_dointvec_jiffies,
1248 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1249 .data = &nf_ct_tcp_timeout_max_retrans,
1250 .maxlen = sizeof(unsigned int),
1251 .mode = 0644,
1252 .proc_handler = proc_dointvec_jiffies,
1255 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
1256 .data = &nf_ct_tcp_timeout_unacknowledged,
1257 .maxlen = sizeof(unsigned int),
1258 .mode = 0644,
1259 .proc_handler = proc_dointvec_jiffies,
1262 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1263 .procname = "nf_conntrack_tcp_loose",
1264 .data = &nf_ct_tcp_loose,
1265 .maxlen = sizeof(unsigned int),
1266 .mode = 0644,
1267 .proc_handler = proc_dointvec,
1270 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1271 .procname = "nf_conntrack_tcp_be_liberal",
1272 .data = &nf_ct_tcp_be_liberal,
1273 .maxlen = sizeof(unsigned int),
1274 .mode = 0644,
1275 .proc_handler = proc_dointvec,
1278 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1279 .procname = "nf_conntrack_tcp_max_retrans",
1280 .data = &nf_ct_tcp_max_retrans,
1281 .maxlen = sizeof(unsigned int),
1282 .mode = 0644,
1283 .proc_handler = proc_dointvec,
1286 .ctl_name = 0
1290 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1291 static struct ctl_table tcp_compat_sysctl_table[] = {
1293 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1294 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1295 .maxlen = sizeof(unsigned int),
1296 .mode = 0644,
1297 .proc_handler = proc_dointvec_jiffies,
1300 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1301 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1302 .maxlen = sizeof(unsigned int),
1303 .mode = 0644,
1304 .proc_handler = proc_dointvec_jiffies,
1307 .procname = "ip_conntrack_tcp_timeout_established",
1308 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1309 .maxlen = sizeof(unsigned int),
1310 .mode = 0644,
1311 .proc_handler = proc_dointvec_jiffies,
1314 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1315 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1316 .maxlen = sizeof(unsigned int),
1317 .mode = 0644,
1318 .proc_handler = proc_dointvec_jiffies,
1321 .procname = "ip_conntrack_tcp_timeout_close_wait",
1322 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1323 .maxlen = sizeof(unsigned int),
1324 .mode = 0644,
1325 .proc_handler = proc_dointvec_jiffies,
1328 .procname = "ip_conntrack_tcp_timeout_last_ack",
1329 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1330 .maxlen = sizeof(unsigned int),
1331 .mode = 0644,
1332 .proc_handler = proc_dointvec_jiffies,
1335 .procname = "ip_conntrack_tcp_timeout_time_wait",
1336 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1337 .maxlen = sizeof(unsigned int),
1338 .mode = 0644,
1339 .proc_handler = proc_dointvec_jiffies,
1342 .procname = "ip_conntrack_tcp_timeout_close",
1343 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1344 .maxlen = sizeof(unsigned int),
1345 .mode = 0644,
1346 .proc_handler = proc_dointvec_jiffies,
1349 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1350 .data = &nf_ct_tcp_timeout_max_retrans,
1351 .maxlen = sizeof(unsigned int),
1352 .mode = 0644,
1353 .proc_handler = proc_dointvec_jiffies,
1356 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1357 .procname = "ip_conntrack_tcp_loose",
1358 .data = &nf_ct_tcp_loose,
1359 .maxlen = sizeof(unsigned int),
1360 .mode = 0644,
1361 .proc_handler = proc_dointvec,
1364 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1365 .procname = "ip_conntrack_tcp_be_liberal",
1366 .data = &nf_ct_tcp_be_liberal,
1367 .maxlen = sizeof(unsigned int),
1368 .mode = 0644,
1369 .proc_handler = proc_dointvec,
1372 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1373 .procname = "ip_conntrack_tcp_max_retrans",
1374 .data = &nf_ct_tcp_max_retrans,
1375 .maxlen = sizeof(unsigned int),
1376 .mode = 0644,
1377 .proc_handler = proc_dointvec,
1380 .ctl_name = 0
1383 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1384 #endif /* CONFIG_SYSCTL */
1386 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1388 .l3proto = PF_INET,
1389 .l4proto = IPPROTO_TCP,
1390 .name = "tcp",
1391 .pkt_to_tuple = tcp_pkt_to_tuple,
1392 .invert_tuple = tcp_invert_tuple,
1393 .print_tuple = tcp_print_tuple,
1394 .print_conntrack = tcp_print_conntrack,
1395 .packet = tcp_packet,
1396 .new = tcp_new,
1397 .error = tcp_error,
1398 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1399 .to_nlattr = tcp_to_nlattr,
1400 .from_nlattr = nlattr_to_tcp,
1401 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1402 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1403 .nla_policy = nf_ct_port_nla_policy,
1404 #endif
1405 #ifdef CONFIG_SYSCTL
1406 .ctl_table_users = &tcp_sysctl_table_users,
1407 .ctl_table_header = &tcp_sysctl_header,
1408 .ctl_table = tcp_sysctl_table,
1409 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1410 .ctl_compat_table = tcp_compat_sysctl_table,
1411 #endif
1412 #endif
1414 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1416 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1418 .l3proto = PF_INET6,
1419 .l4proto = IPPROTO_TCP,
1420 .name = "tcp",
1421 .pkt_to_tuple = tcp_pkt_to_tuple,
1422 .invert_tuple = tcp_invert_tuple,
1423 .print_tuple = tcp_print_tuple,
1424 .print_conntrack = tcp_print_conntrack,
1425 .packet = tcp_packet,
1426 .new = tcp_new,
1427 .error = tcp_error,
1428 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
1429 .to_nlattr = tcp_to_nlattr,
1430 .from_nlattr = nlattr_to_tcp,
1431 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1432 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1433 .nla_policy = nf_ct_port_nla_policy,
1434 #endif
1435 #ifdef CONFIG_SYSCTL
1436 .ctl_table_users = &tcp_sysctl_table_users,
1437 .ctl_table_header = &tcp_sysctl_header,
1438 .ctl_table = tcp_sysctl_table,
1439 #endif
1441 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);