blackfin: use set_current_blocked() and block_sigmask()
[linux-2.6.git] / net / netfilter / nf_conntrack_proto_tcp.c
blob21ff1a99f5341c1fe475c10a0170a1b62fc9a5f5
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>
29 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
30 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
32 /* "Be conservative in what you do,
33 be liberal in what you accept from others."
34 If it's non-zero, we mark only out of window RST segments as INVALID. */
35 static int nf_ct_tcp_be_liberal __read_mostly = 0;
37 /* If it is set to zero, we disable picking up already established
38 connections. */
39 static int nf_ct_tcp_loose __read_mostly = 1;
41 /* Max number of the retransmitted packets without receiving an (acceptable)
42 ACK from the destination. If this number is reached, a shorter timer
43 will be started. */
44 static int nf_ct_tcp_max_retrans __read_mostly = 3;
46 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
47 closely. They're more complex. --RR */
49 static const char *const tcp_conntrack_names[] = {
50 "NONE",
51 "SYN_SENT",
52 "SYN_RECV",
53 "ESTABLISHED",
54 "FIN_WAIT",
55 "CLOSE_WAIT",
56 "LAST_ACK",
57 "TIME_WAIT",
58 "CLOSE",
59 "SYN_SENT2",
62 #define SECS * HZ
63 #define MINS * 60 SECS
64 #define HOURS * 60 MINS
65 #define DAYS * 24 HOURS
67 static unsigned int tcp_timeouts[TCP_CONNTRACK_TIMEOUT_MAX] __read_mostly = {
68 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
69 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
70 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
71 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
72 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
73 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
74 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
75 [TCP_CONNTRACK_CLOSE] = 10 SECS,
76 [TCP_CONNTRACK_SYN_SENT2] = 2 MINS,
77 /* RFC1122 says the R2 limit should be at least 100 seconds.
78 Linux uses 15 packets as limit, which corresponds
79 to ~13-30min depending on RTO. */
80 [TCP_CONNTRACK_RETRANS] = 5 MINS,
81 [TCP_CONNTRACK_UNACK] = 5 MINS,
84 #define sNO TCP_CONNTRACK_NONE
85 #define sSS TCP_CONNTRACK_SYN_SENT
86 #define sSR TCP_CONNTRACK_SYN_RECV
87 #define sES TCP_CONNTRACK_ESTABLISHED
88 #define sFW TCP_CONNTRACK_FIN_WAIT
89 #define sCW TCP_CONNTRACK_CLOSE_WAIT
90 #define sLA TCP_CONNTRACK_LAST_ACK
91 #define sTW TCP_CONNTRACK_TIME_WAIT
92 #define sCL TCP_CONNTRACK_CLOSE
93 #define sS2 TCP_CONNTRACK_SYN_SENT2
94 #define sIV TCP_CONNTRACK_MAX
95 #define sIG TCP_CONNTRACK_IGNORE
97 /* What TCP flags are set from RST/SYN/FIN/ACK. */
98 enum tcp_bit_set {
99 TCP_SYN_SET,
100 TCP_SYNACK_SET,
101 TCP_FIN_SET,
102 TCP_ACK_SET,
103 TCP_RST_SET,
104 TCP_NONE_SET,
108 * The TCP state transition table needs a few words...
110 * We are the man in the middle. All the packets go through us
111 * but might get lost in transit to the destination.
112 * It is assumed that the destinations can't receive segments
113 * we haven't seen.
115 * The checked segment is in window, but our windows are *not*
116 * equivalent with the ones of the sender/receiver. We always
117 * try to guess the state of the current sender.
119 * The meaning of the states are:
121 * NONE: initial state
122 * SYN_SENT: SYN-only packet seen
123 * SYN_SENT2: SYN-only packet seen from reply dir, simultaneous open
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 * Packets marked as IGNORED (sIG):
133 * if they may be either invalid or valid
134 * and the receiver may send back a connection
135 * closing RST or a SYN/ACK.
137 * Packets marked as INVALID (sIV):
138 * if we regard them as truly invalid packets
140 static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
142 /* ORIGINAL */
143 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
144 /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sS2 },
146 * sNO -> sSS Initialize a new connection
147 * sSS -> sSS Retransmitted SYN
148 * sS2 -> sS2 Late retransmitted SYN
149 * sSR -> sIG
150 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
151 * are errors. Receiver will reply with RST
152 * and close the connection.
153 * Or we are not in sync and hold a dead connection.
154 * sFW -> sIG
155 * sCW -> sIG
156 * sLA -> sIG
157 * sTW -> sSS Reopened connection (RFC 1122).
158 * sCL -> sSS
160 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
161 /*synack*/ { sIV, sIV, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR },
163 * sNO -> sIV Too late and no reason to do anything
164 * sSS -> sIV Client can't send SYN and then SYN/ACK
165 * sS2 -> sSR SYN/ACK sent to SYN2 in simultaneous open
166 * sSR -> sIG
167 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
168 * are errors. Receiver will reply with RST
169 * and close the connection.
170 * Or we are not in sync and hold a dead connection.
171 * sFW -> sIG
172 * sCW -> sIG
173 * sLA -> sIG
174 * sTW -> sIG
175 * sCL -> sIG
177 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
178 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
180 * sNO -> sIV Too late and no reason to do anything...
181 * sSS -> sIV Client migth not send FIN in this state:
182 * we enforce waiting for a SYN/ACK reply first.
183 * sS2 -> sIV
184 * sSR -> sFW Close started.
185 * sES -> sFW
186 * sFW -> sLA FIN seen in both directions, waiting for
187 * the last ACK.
188 * Migth be a retransmitted FIN as well...
189 * sCW -> sLA
190 * sLA -> sLA Retransmitted FIN. Remain in the same state.
191 * sTW -> sTW
192 * sCL -> sCL
194 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
195 /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
197 * sNO -> sES Assumed.
198 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
199 * sS2 -> sIV
200 * sSR -> sES Established state is reached.
201 * sES -> sES :-)
202 * sFW -> sCW Normal close request answered by ACK.
203 * sCW -> sCW
204 * sLA -> sTW Last ACK detected.
205 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
206 * sCL -> sCL
208 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
209 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
210 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
213 /* REPLY */
214 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
215 /*syn*/ { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sS2 },
217 * sNO -> sIV Never reached.
218 * sSS -> sS2 Simultaneous open
219 * sS2 -> sS2 Retransmitted simultaneous SYN
220 * sSR -> sIV Invalid SYN packets sent by the server
221 * sES -> sIV
222 * sFW -> sIV
223 * sCW -> sIV
224 * sLA -> sIV
225 * sTW -> sIV Reopened connection, but server may not do it.
226 * sCL -> sIV
228 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
229 /*synack*/ { sIV, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR },
231 * sSS -> sSR Standard open.
232 * sS2 -> sSR Simultaneous open
233 * sSR -> sIG Retransmitted SYN/ACK, ignore it.
234 * sES -> sIG Late retransmitted SYN/ACK?
235 * sFW -> sIG Might be SYN/ACK answering ignored SYN
236 * sCW -> sIG
237 * sLA -> sIG
238 * sTW -> sIG
239 * sCL -> sIG
241 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
242 /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
244 * sSS -> sIV Server might not send FIN in this state.
245 * sS2 -> sIV
246 * sSR -> sFW Close started.
247 * sES -> sFW
248 * sFW -> sLA FIN seen in both directions.
249 * sCW -> sLA
250 * sLA -> sLA Retransmitted FIN.
251 * sTW -> sTW
252 * sCL -> sCL
254 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
255 /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIG },
257 * sSS -> sIG Might be a half-open connection.
258 * sS2 -> sIG
259 * sSR -> sSR Might answer late resent SYN.
260 * sES -> sES :-)
261 * sFW -> sCW Normal close request answered by ACK.
262 * sCW -> sCW
263 * sLA -> sTW Last ACK detected.
264 * sTW -> sTW Retransmitted last ACK.
265 * sCL -> sCL
267 /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
268 /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
269 /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
273 static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
274 struct nf_conntrack_tuple *tuple)
276 const struct tcphdr *hp;
277 struct tcphdr _hdr;
279 /* Actually only need first 8 bytes. */
280 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
281 if (hp == NULL)
282 return false;
284 tuple->src.u.tcp.port = hp->source;
285 tuple->dst.u.tcp.port = hp->dest;
287 return true;
290 static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
291 const struct nf_conntrack_tuple *orig)
293 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
294 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
295 return true;
298 /* Print out the per-protocol part of the tuple. */
299 static int tcp_print_tuple(struct seq_file *s,
300 const struct nf_conntrack_tuple *tuple)
302 return seq_printf(s, "sport=%hu dport=%hu ",
303 ntohs(tuple->src.u.tcp.port),
304 ntohs(tuple->dst.u.tcp.port));
307 /* Print out the private part of the conntrack. */
308 static int tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
310 enum tcp_conntrack state;
312 spin_lock_bh(&ct->lock);
313 state = ct->proto.tcp.state;
314 spin_unlock_bh(&ct->lock);
316 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
319 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
321 if (tcph->rst) return TCP_RST_SET;
322 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
323 else if (tcph->fin) return TCP_FIN_SET;
324 else if (tcph->ack) return TCP_ACK_SET;
325 else return TCP_NONE_SET;
328 /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
329 in IP Filter' by Guido van Rooij.
331 http://www.sane.nl/events/sane2000/papers.html
332 http://www.darkart.com/mirrors/www.obfuscation.org/ipf/
334 The boundaries and the conditions are changed according to RFC793:
335 the packet must intersect the window (i.e. segments may be
336 after the right or before the left edge) and thus receivers may ACK
337 segments after the right edge of the window.
339 td_maxend = max(sack + max(win,1)) seen in reply packets
340 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
341 td_maxwin += seq + len - sender.td_maxend
342 if seq + len > sender.td_maxend
343 td_end = max(seq + len) seen in sent packets
345 I. Upper bound for valid data: seq <= sender.td_maxend
346 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
347 III. Upper bound for valid (s)ack: sack <= receiver.td_end
348 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
350 where sack is the highest right edge of sack block found in the packet
351 or ack in the case of packet without SACK option.
353 The upper bound limit for a valid (s)ack is not ignored -
354 we doesn't have to deal with fragments.
357 static inline __u32 segment_seq_plus_len(__u32 seq,
358 size_t len,
359 unsigned int dataoff,
360 const struct tcphdr *tcph)
362 /* XXX Should I use payload length field in IP/IPv6 header ?
363 * - YK */
364 return (seq + len - dataoff - tcph->doff*4
365 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
368 /* Fixme: what about big packets? */
369 #define MAXACKWINCONST 66000
370 #define MAXACKWINDOW(sender) \
371 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
372 : MAXACKWINCONST)
375 * Simplified tcp_parse_options routine from tcp_input.c
377 static void tcp_options(const struct sk_buff *skb,
378 unsigned int dataoff,
379 const struct tcphdr *tcph,
380 struct ip_ct_tcp_state *state)
382 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
383 const unsigned char *ptr;
384 int length = (tcph->doff*4) - sizeof(struct tcphdr);
386 if (!length)
387 return;
389 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
390 length, buff);
391 BUG_ON(ptr == NULL);
393 state->td_scale =
394 state->flags = 0;
396 while (length > 0) {
397 int opcode=*ptr++;
398 int opsize;
400 switch (opcode) {
401 case TCPOPT_EOL:
402 return;
403 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
404 length--;
405 continue;
406 default:
407 opsize=*ptr++;
408 if (opsize < 2) /* "silly options" */
409 return;
410 if (opsize > length)
411 return; /* don't parse partial options */
413 if (opcode == TCPOPT_SACK_PERM
414 && opsize == TCPOLEN_SACK_PERM)
415 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
416 else if (opcode == TCPOPT_WINDOW
417 && opsize == TCPOLEN_WINDOW) {
418 state->td_scale = *(u_int8_t *)ptr;
420 if (state->td_scale > 14) {
421 /* See RFC1323 */
422 state->td_scale = 14;
424 state->flags |=
425 IP_CT_TCP_FLAG_WINDOW_SCALE;
427 ptr += opsize - 2;
428 length -= opsize;
433 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
434 const struct tcphdr *tcph, __u32 *sack)
436 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
437 const unsigned char *ptr;
438 int length = (tcph->doff*4) - sizeof(struct tcphdr);
439 __u32 tmp;
441 if (!length)
442 return;
444 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
445 length, buff);
446 BUG_ON(ptr == NULL);
448 /* Fast path for timestamp-only option */
449 if (length == TCPOLEN_TSTAMP_ALIGNED
450 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
451 | (TCPOPT_NOP << 16)
452 | (TCPOPT_TIMESTAMP << 8)
453 | TCPOLEN_TIMESTAMP))
454 return;
456 while (length > 0) {
457 int opcode = *ptr++;
458 int opsize, i;
460 switch (opcode) {
461 case TCPOPT_EOL:
462 return;
463 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
464 length--;
465 continue;
466 default:
467 opsize = *ptr++;
468 if (opsize < 2) /* "silly options" */
469 return;
470 if (opsize > length)
471 return; /* don't parse partial options */
473 if (opcode == TCPOPT_SACK
474 && opsize >= (TCPOLEN_SACK_BASE
475 + TCPOLEN_SACK_PERBLOCK)
476 && !((opsize - TCPOLEN_SACK_BASE)
477 % TCPOLEN_SACK_PERBLOCK)) {
478 for (i = 0;
479 i < (opsize - TCPOLEN_SACK_BASE);
480 i += TCPOLEN_SACK_PERBLOCK) {
481 tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
483 if (after(tmp, *sack))
484 *sack = tmp;
486 return;
488 ptr += opsize - 2;
489 length -= opsize;
494 #ifdef CONFIG_NF_NAT_NEEDED
495 static inline s16 nat_offset(const struct nf_conn *ct,
496 enum ip_conntrack_dir dir,
497 u32 seq)
499 typeof(nf_ct_nat_offset) get_offset = rcu_dereference(nf_ct_nat_offset);
501 return get_offset != NULL ? get_offset(ct, dir, seq) : 0;
503 #define NAT_OFFSET(pf, ct, dir, seq) \
504 (pf == NFPROTO_IPV4 ? nat_offset(ct, dir, seq) : 0)
505 #else
506 #define NAT_OFFSET(pf, ct, dir, seq) 0
507 #endif
509 static bool tcp_in_window(const struct nf_conn *ct,
510 struct ip_ct_tcp *state,
511 enum ip_conntrack_dir dir,
512 unsigned int index,
513 const struct sk_buff *skb,
514 unsigned int dataoff,
515 const struct tcphdr *tcph,
516 u_int8_t pf)
518 struct net *net = nf_ct_net(ct);
519 struct ip_ct_tcp_state *sender = &state->seen[dir];
520 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
521 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
522 __u32 seq, ack, sack, end, win, swin;
523 s16 receiver_offset;
524 bool res;
527 * Get the required data from the packet.
529 seq = ntohl(tcph->seq);
530 ack = sack = ntohl(tcph->ack_seq);
531 win = ntohs(tcph->window);
532 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
534 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
535 tcp_sack(skb, dataoff, tcph, &sack);
537 /* Take into account NAT sequence number mangling */
538 receiver_offset = NAT_OFFSET(pf, ct, !dir, ack - 1);
539 ack -= receiver_offset;
540 sack -= receiver_offset;
542 pr_debug("tcp_in_window: START\n");
543 pr_debug("tcp_in_window: ");
544 nf_ct_dump_tuple(tuple);
545 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
546 seq, ack, receiver_offset, sack, receiver_offset, win, end);
547 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
548 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
549 sender->td_end, sender->td_maxend, sender->td_maxwin,
550 sender->td_scale,
551 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
552 receiver->td_scale);
554 if (sender->td_maxwin == 0) {
556 * Initialize sender data.
558 if (tcph->syn) {
560 * SYN-ACK in reply to a SYN
561 * or SYN from reply direction in simultaneous open.
563 sender->td_end =
564 sender->td_maxend = end;
565 sender->td_maxwin = (win == 0 ? 1 : win);
567 tcp_options(skb, dataoff, tcph, sender);
569 * RFC 1323:
570 * Both sides must send the Window Scale option
571 * to enable window scaling in either direction.
573 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
574 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
575 sender->td_scale =
576 receiver->td_scale = 0;
577 if (!tcph->ack)
578 /* Simultaneous open */
579 return true;
580 } else {
582 * We are in the middle of a connection,
583 * its history is lost for us.
584 * Let's try to use the data from the packet.
586 sender->td_end = end;
587 swin = win << sender->td_scale;
588 sender->td_maxwin = (swin == 0 ? 1 : swin);
589 sender->td_maxend = end + sender->td_maxwin;
591 * We haven't seen traffic in the other direction yet
592 * but we have to tweak window tracking to pass III
593 * and IV until that happens.
595 if (receiver->td_maxwin == 0)
596 receiver->td_end = receiver->td_maxend = sack;
598 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
599 && dir == IP_CT_DIR_ORIGINAL)
600 || (state->state == TCP_CONNTRACK_SYN_RECV
601 && dir == IP_CT_DIR_REPLY))
602 && after(end, sender->td_end)) {
604 * RFC 793: "if a TCP is reinitialized ... then it need
605 * not wait at all; it must only be sure to use sequence
606 * numbers larger than those recently used."
608 sender->td_end =
609 sender->td_maxend = end;
610 sender->td_maxwin = (win == 0 ? 1 : win);
612 tcp_options(skb, dataoff, tcph, sender);
615 if (!(tcph->ack)) {
617 * If there is no ACK, just pretend it was set and OK.
619 ack = sack = receiver->td_end;
620 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
621 (TCP_FLAG_ACK|TCP_FLAG_RST))
622 && (ack == 0)) {
624 * Broken TCP stacks, that set ACK in RST packets as well
625 * with zero ack value.
627 ack = sack = receiver->td_end;
630 if (seq == end
631 && (!tcph->rst
632 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
634 * Packets contains no data: we assume it is valid
635 * and check the ack value only.
636 * However RST segments are always validated by their
637 * SEQ number, except when seq == 0 (reset sent answering
638 * SYN.
640 seq = end = sender->td_end;
642 pr_debug("tcp_in_window: ");
643 nf_ct_dump_tuple(tuple);
644 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
645 seq, ack, receiver_offset, sack, receiver_offset, win, end);
646 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
647 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
648 sender->td_end, sender->td_maxend, sender->td_maxwin,
649 sender->td_scale,
650 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
651 receiver->td_scale);
653 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
654 before(seq, sender->td_maxend + 1),
655 after(end, sender->td_end - receiver->td_maxwin - 1),
656 before(sack, receiver->td_end + 1),
657 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
659 if (before(seq, sender->td_maxend + 1) &&
660 after(end, sender->td_end - receiver->td_maxwin - 1) &&
661 before(sack, receiver->td_end + 1) &&
662 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
664 * Take into account window scaling (RFC 1323).
666 if (!tcph->syn)
667 win <<= sender->td_scale;
670 * Update sender data.
672 swin = win + (sack - ack);
673 if (sender->td_maxwin < swin)
674 sender->td_maxwin = swin;
675 if (after(end, sender->td_end)) {
676 sender->td_end = end;
677 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
679 if (tcph->ack) {
680 if (!(sender->flags & IP_CT_TCP_FLAG_MAXACK_SET)) {
681 sender->td_maxack = ack;
682 sender->flags |= IP_CT_TCP_FLAG_MAXACK_SET;
683 } else if (after(ack, sender->td_maxack))
684 sender->td_maxack = ack;
688 * Update receiver data.
690 if (receiver->td_maxwin != 0 && after(end, sender->td_maxend))
691 receiver->td_maxwin += end - sender->td_maxend;
692 if (after(sack + win, receiver->td_maxend - 1)) {
693 receiver->td_maxend = sack + win;
694 if (win == 0)
695 receiver->td_maxend++;
697 if (ack == receiver->td_end)
698 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
701 * Check retransmissions.
703 if (index == TCP_ACK_SET) {
704 if (state->last_dir == dir
705 && state->last_seq == seq
706 && state->last_ack == ack
707 && state->last_end == end
708 && state->last_win == win)
709 state->retrans++;
710 else {
711 state->last_dir = dir;
712 state->last_seq = seq;
713 state->last_ack = ack;
714 state->last_end = end;
715 state->last_win = win;
716 state->retrans = 0;
719 res = true;
720 } else {
721 res = false;
722 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
723 nf_ct_tcp_be_liberal)
724 res = true;
725 if (!res && LOG_INVALID(net, IPPROTO_TCP))
726 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
727 "nf_ct_tcp: %s ",
728 before(seq, sender->td_maxend + 1) ?
729 after(end, sender->td_end - receiver->td_maxwin - 1) ?
730 before(sack, receiver->td_end + 1) ?
731 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1) ? "BUG"
732 : "ACK is under the lower bound (possible overly delayed ACK)"
733 : "ACK is over the upper bound (ACKed data not seen yet)"
734 : "SEQ is under the lower bound (already ACKed data retransmitted)"
735 : "SEQ is over the upper bound (over the window of the receiver)");
738 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
739 "receiver end=%u maxend=%u maxwin=%u\n",
740 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
741 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
743 return res;
746 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
747 static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|
748 TCPHDR_URG) + 1] =
750 [TCPHDR_SYN] = 1,
751 [TCPHDR_SYN|TCPHDR_URG] = 1,
752 [TCPHDR_SYN|TCPHDR_ACK] = 1,
753 [TCPHDR_RST] = 1,
754 [TCPHDR_RST|TCPHDR_ACK] = 1,
755 [TCPHDR_FIN|TCPHDR_ACK] = 1,
756 [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG] = 1,
757 [TCPHDR_ACK] = 1,
758 [TCPHDR_ACK|TCPHDR_URG] = 1,
761 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
762 static int tcp_error(struct net *net, struct nf_conn *tmpl,
763 struct sk_buff *skb,
764 unsigned int dataoff,
765 enum ip_conntrack_info *ctinfo,
766 u_int8_t pf,
767 unsigned int hooknum)
769 const struct tcphdr *th;
770 struct tcphdr _tcph;
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(net, 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(net, 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 (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
797 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
798 if (LOG_INVALID(net, IPPROTO_TCP))
799 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
800 "nf_ct_tcp: bad TCP checksum ");
801 return -NF_ACCEPT;
804 /* Check TCP flags. */
805 tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH));
806 if (!tcp_valid_flags[tcpflags]) {
807 if (LOG_INVALID(net, IPPROTO_TCP))
808 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
809 "nf_ct_tcp: invalid TCP flag combination ");
810 return -NF_ACCEPT;
813 return NF_ACCEPT;
816 static unsigned int *tcp_get_timeouts(struct net *net)
818 return tcp_timeouts;
821 /* Returns verdict for packet, or -1 for invalid. */
822 static int tcp_packet(struct nf_conn *ct,
823 const struct sk_buff *skb,
824 unsigned int dataoff,
825 enum ip_conntrack_info ctinfo,
826 u_int8_t pf,
827 unsigned int hooknum,
828 unsigned int *timeouts)
830 struct net *net = nf_ct_net(ct);
831 struct nf_conntrack_tuple *tuple;
832 enum tcp_conntrack new_state, old_state;
833 enum ip_conntrack_dir dir;
834 const struct tcphdr *th;
835 struct tcphdr _tcph;
836 unsigned long timeout;
837 unsigned int index;
839 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
840 BUG_ON(th == NULL);
842 spin_lock_bh(&ct->lock);
843 old_state = ct->proto.tcp.state;
844 dir = CTINFO2DIR(ctinfo);
845 index = get_conntrack_index(th);
846 new_state = tcp_conntracks[dir][index][old_state];
847 tuple = &ct->tuplehash[dir].tuple;
849 switch (new_state) {
850 case TCP_CONNTRACK_SYN_SENT:
851 if (old_state < TCP_CONNTRACK_TIME_WAIT)
852 break;
853 /* RFC 1122: "When a connection is closed actively,
854 * it MUST linger in TIME-WAIT state for a time 2xMSL
855 * (Maximum Segment Lifetime). However, it MAY accept
856 * a new SYN from the remote TCP to reopen the connection
857 * directly from TIME-WAIT state, if..."
858 * We ignore the conditions because we are in the
859 * TIME-WAIT state anyway.
861 * Handle aborted connections: we and the server
862 * think there is an existing connection but the client
863 * aborts it and starts a new one.
865 if (((ct->proto.tcp.seen[dir].flags
866 | ct->proto.tcp.seen[!dir].flags)
867 & IP_CT_TCP_FLAG_CLOSE_INIT)
868 || (ct->proto.tcp.last_dir == dir
869 && ct->proto.tcp.last_index == TCP_RST_SET)) {
870 /* Attempt to reopen a closed/aborted connection.
871 * Delete this connection and look up again. */
872 spin_unlock_bh(&ct->lock);
874 /* Only repeat if we can actually remove the timer.
875 * Destruction may already be in progress in process
876 * context and we must give it a chance to terminate.
878 if (nf_ct_kill(ct))
879 return -NF_REPEAT;
880 return NF_DROP;
882 /* Fall through */
883 case TCP_CONNTRACK_IGNORE:
884 /* Ignored packets:
886 * Our connection entry may be out of sync, so ignore
887 * packets which may signal the real connection between
888 * the client and the server.
890 * a) SYN in ORIGINAL
891 * b) SYN/ACK in REPLY
892 * c) ACK in reply direction after initial SYN in original.
894 * If the ignored packet is invalid, the receiver will send
895 * a RST we'll catch below.
897 if (index == TCP_SYNACK_SET
898 && ct->proto.tcp.last_index == TCP_SYN_SET
899 && ct->proto.tcp.last_dir != dir
900 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
901 /* b) This SYN/ACK acknowledges a SYN that we earlier
902 * ignored as invalid. This means that the client and
903 * the server are both in sync, while the firewall is
904 * not. We get in sync from the previously annotated
905 * values.
907 old_state = TCP_CONNTRACK_SYN_SENT;
908 new_state = TCP_CONNTRACK_SYN_RECV;
909 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end =
910 ct->proto.tcp.last_end;
911 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend =
912 ct->proto.tcp.last_end;
913 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin =
914 ct->proto.tcp.last_win == 0 ?
915 1 : ct->proto.tcp.last_win;
916 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale =
917 ct->proto.tcp.last_wscale;
918 ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
919 ct->proto.tcp.last_flags;
920 memset(&ct->proto.tcp.seen[dir], 0,
921 sizeof(struct ip_ct_tcp_state));
922 break;
924 ct->proto.tcp.last_index = index;
925 ct->proto.tcp.last_dir = dir;
926 ct->proto.tcp.last_seq = ntohl(th->seq);
927 ct->proto.tcp.last_end =
928 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
929 ct->proto.tcp.last_win = ntohs(th->window);
931 /* a) This is a SYN in ORIGINAL. The client and the server
932 * may be in sync but we are not. In that case, we annotate
933 * the TCP options and let the packet go through. If it is a
934 * valid SYN packet, the server will reply with a SYN/ACK, and
935 * then we'll get in sync. Otherwise, the server ignores it. */
936 if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) {
937 struct ip_ct_tcp_state seen = {};
939 ct->proto.tcp.last_flags =
940 ct->proto.tcp.last_wscale = 0;
941 tcp_options(skb, dataoff, th, &seen);
942 if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
943 ct->proto.tcp.last_flags |=
944 IP_CT_TCP_FLAG_WINDOW_SCALE;
945 ct->proto.tcp.last_wscale = seen.td_scale;
947 if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) {
948 ct->proto.tcp.last_flags |=
949 IP_CT_TCP_FLAG_SACK_PERM;
952 spin_unlock_bh(&ct->lock);
953 if (LOG_INVALID(net, IPPROTO_TCP))
954 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
955 "nf_ct_tcp: invalid packet ignored in "
956 "state %s ", tcp_conntrack_names[old_state]);
957 return NF_ACCEPT;
958 case TCP_CONNTRACK_MAX:
959 /* Invalid packet */
960 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
961 dir, get_conntrack_index(th), old_state);
962 spin_unlock_bh(&ct->lock);
963 if (LOG_INVALID(net, IPPROTO_TCP))
964 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
965 "nf_ct_tcp: invalid state ");
966 return -NF_ACCEPT;
967 case TCP_CONNTRACK_CLOSE:
968 if (index == TCP_RST_SET
969 && (ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET)
970 && before(ntohl(th->seq), ct->proto.tcp.seen[!dir].td_maxack)) {
971 /* Invalid RST */
972 spin_unlock_bh(&ct->lock);
973 if (LOG_INVALID(net, IPPROTO_TCP))
974 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
975 "nf_ct_tcp: invalid RST ");
976 return -NF_ACCEPT;
978 if (index == TCP_RST_SET
979 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
980 && ct->proto.tcp.last_index == TCP_SYN_SET)
981 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
982 && ct->proto.tcp.last_index == TCP_ACK_SET))
983 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
984 /* RST sent to invalid SYN or ACK we had let through
985 * at a) and c) above:
987 * a) SYN was in window then
988 * c) we hold a half-open connection.
990 * Delete our connection entry.
991 * We skip window checking, because packet might ACK
992 * segments we ignored. */
993 goto in_window;
995 /* Just fall through */
996 default:
997 /* Keep compilers happy. */
998 break;
1001 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
1002 skb, dataoff, th, pf)) {
1003 spin_unlock_bh(&ct->lock);
1004 return -NF_ACCEPT;
1006 in_window:
1007 /* From now on we have got in-window packets */
1008 ct->proto.tcp.last_index = index;
1009 ct->proto.tcp.last_dir = dir;
1011 pr_debug("tcp_conntracks: ");
1012 nf_ct_dump_tuple(tuple);
1013 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1014 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1015 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1016 old_state, new_state);
1018 ct->proto.tcp.state = new_state;
1019 if (old_state != new_state
1020 && new_state == TCP_CONNTRACK_FIN_WAIT)
1021 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1023 if (ct->proto.tcp.retrans >= nf_ct_tcp_max_retrans &&
1024 timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1025 timeout = timeouts[TCP_CONNTRACK_RETRANS];
1026 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
1027 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
1028 timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK])
1029 timeout = timeouts[TCP_CONNTRACK_UNACK];
1030 else
1031 timeout = timeouts[new_state];
1032 spin_unlock_bh(&ct->lock);
1034 if (new_state != old_state)
1035 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
1037 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1038 /* If only reply is a RST, we can consider ourselves not to
1039 have an established connection: this is a fairly common
1040 problem case, so we can delete the conntrack
1041 immediately. --RR */
1042 if (th->rst) {
1043 nf_ct_kill_acct(ct, ctinfo, skb);
1044 return NF_ACCEPT;
1046 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
1047 && (old_state == TCP_CONNTRACK_SYN_RECV
1048 || old_state == TCP_CONNTRACK_ESTABLISHED)
1049 && new_state == TCP_CONNTRACK_ESTABLISHED) {
1050 /* Set ASSURED if we see see valid ack in ESTABLISHED
1051 after SYN_RECV or a valid answer for a picked up
1052 connection. */
1053 set_bit(IPS_ASSURED_BIT, &ct->status);
1054 nf_conntrack_event_cache(IPCT_ASSURED, ct);
1056 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
1058 return NF_ACCEPT;
1061 /* Called when a new connection for this protocol found. */
1062 static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
1063 unsigned int dataoff, unsigned int *timeouts)
1065 enum tcp_conntrack new_state;
1066 const struct tcphdr *th;
1067 struct tcphdr _tcph;
1068 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1069 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1071 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1072 BUG_ON(th == NULL);
1074 /* Don't need lock here: this conntrack not in circulation yet */
1075 new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE];
1077 /* Invalid: delete conntrack */
1078 if (new_state >= TCP_CONNTRACK_MAX) {
1079 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1080 return false;
1083 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1084 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1085 /* SYN packet */
1086 ct->proto.tcp.seen[0].td_end =
1087 segment_seq_plus_len(ntohl(th->seq), skb->len,
1088 dataoff, th);
1089 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1090 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1091 ct->proto.tcp.seen[0].td_maxwin = 1;
1092 ct->proto.tcp.seen[0].td_maxend =
1093 ct->proto.tcp.seen[0].td_end;
1095 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1096 } else if (nf_ct_tcp_loose == 0) {
1097 /* Don't try to pick up connections. */
1098 return false;
1099 } else {
1100 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1102 * We are in the middle of a connection,
1103 * its history is lost for us.
1104 * Let's try to use the data from the packet.
1106 ct->proto.tcp.seen[0].td_end =
1107 segment_seq_plus_len(ntohl(th->seq), skb->len,
1108 dataoff, th);
1109 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1110 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1111 ct->proto.tcp.seen[0].td_maxwin = 1;
1112 ct->proto.tcp.seen[0].td_maxend =
1113 ct->proto.tcp.seen[0].td_end +
1114 ct->proto.tcp.seen[0].td_maxwin;
1116 /* We assume SACK and liberal window checking to handle
1117 * window scaling */
1118 ct->proto.tcp.seen[0].flags =
1119 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1120 IP_CT_TCP_FLAG_BE_LIBERAL;
1123 /* tcp_packet will set them */
1124 ct->proto.tcp.last_index = TCP_NONE_SET;
1126 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1127 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1128 sender->td_end, sender->td_maxend, sender->td_maxwin,
1129 sender->td_scale,
1130 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1131 receiver->td_scale);
1132 return true;
1135 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1137 #include <linux/netfilter/nfnetlink.h>
1138 #include <linux/netfilter/nfnetlink_conntrack.h>
1140 static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1141 struct nf_conn *ct)
1143 struct nlattr *nest_parms;
1144 struct nf_ct_tcp_flags tmp = {};
1146 spin_lock_bh(&ct->lock);
1147 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1148 if (!nest_parms)
1149 goto nla_put_failure;
1151 if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) ||
1152 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1153 ct->proto.tcp.seen[0].td_scale) ||
1154 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1155 ct->proto.tcp.seen[1].td_scale))
1156 goto nla_put_failure;
1158 tmp.flags = ct->proto.tcp.seen[0].flags;
1159 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1160 sizeof(struct nf_ct_tcp_flags), &tmp))
1161 goto nla_put_failure;
1163 tmp.flags = ct->proto.tcp.seen[1].flags;
1164 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1165 sizeof(struct nf_ct_tcp_flags), &tmp))
1166 goto nla_put_failure;
1167 spin_unlock_bh(&ct->lock);
1169 nla_nest_end(skb, nest_parms);
1171 return 0;
1173 nla_put_failure:
1174 spin_unlock_bh(&ct->lock);
1175 return -1;
1178 static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1179 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1180 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1181 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1182 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1183 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
1186 static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1188 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1189 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1190 int err;
1192 /* updates could not contain anything about the private
1193 * protocol info, in that case skip the parsing */
1194 if (!pattr)
1195 return 0;
1197 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
1198 if (err < 0)
1199 return err;
1201 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1202 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1203 return -EINVAL;
1205 spin_lock_bh(&ct->lock);
1206 if (tb[CTA_PROTOINFO_TCP_STATE])
1207 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1209 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1210 struct nf_ct_tcp_flags *attr =
1211 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1212 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1213 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1216 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1217 struct nf_ct_tcp_flags *attr =
1218 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1219 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1220 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1223 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1224 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
1225 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1226 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1227 ct->proto.tcp.seen[0].td_scale =
1228 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1229 ct->proto.tcp.seen[1].td_scale =
1230 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
1232 spin_unlock_bh(&ct->lock);
1234 return 0;
1237 static int tcp_nlattr_size(void)
1239 return nla_total_size(0) /* CTA_PROTOINFO_TCP */
1240 + nla_policy_len(tcp_nla_policy, CTA_PROTOINFO_TCP_MAX + 1);
1243 static int tcp_nlattr_tuple_size(void)
1245 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1247 #endif
1249 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1251 #include <linux/netfilter/nfnetlink.h>
1252 #include <linux/netfilter/nfnetlink_cttimeout.h>
1254 static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[], void *data)
1256 unsigned int *timeouts = data;
1257 int i;
1259 /* set default TCP timeouts. */
1260 for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++)
1261 timeouts[i] = tcp_timeouts[i];
1263 if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) {
1264 timeouts[TCP_CONNTRACK_SYN_SENT] =
1265 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ;
1267 if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) {
1268 timeouts[TCP_CONNTRACK_SYN_RECV] =
1269 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ;
1271 if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) {
1272 timeouts[TCP_CONNTRACK_ESTABLISHED] =
1273 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ;
1275 if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) {
1276 timeouts[TCP_CONNTRACK_FIN_WAIT] =
1277 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ;
1279 if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) {
1280 timeouts[TCP_CONNTRACK_CLOSE_WAIT] =
1281 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ;
1283 if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) {
1284 timeouts[TCP_CONNTRACK_LAST_ACK] =
1285 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ;
1287 if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) {
1288 timeouts[TCP_CONNTRACK_TIME_WAIT] =
1289 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ;
1291 if (tb[CTA_TIMEOUT_TCP_CLOSE]) {
1292 timeouts[TCP_CONNTRACK_CLOSE] =
1293 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ;
1295 if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) {
1296 timeouts[TCP_CONNTRACK_SYN_SENT2] =
1297 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ;
1299 if (tb[CTA_TIMEOUT_TCP_RETRANS]) {
1300 timeouts[TCP_CONNTRACK_RETRANS] =
1301 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ;
1303 if (tb[CTA_TIMEOUT_TCP_UNACK]) {
1304 timeouts[TCP_CONNTRACK_UNACK] =
1305 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ;
1307 return 0;
1310 static int
1311 tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
1313 const unsigned int *timeouts = data;
1315 if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1316 htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) ||
1317 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1318 htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) ||
1319 nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1320 htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) ||
1321 nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1322 htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) ||
1323 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1324 htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) ||
1325 nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1326 htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) ||
1327 nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1328 htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) ||
1329 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1330 htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) ||
1331 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
1332 htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) ||
1333 nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS,
1334 htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) ||
1335 nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK,
1336 htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ)))
1337 goto nla_put_failure;
1338 return 0;
1340 nla_put_failure:
1341 return -ENOSPC;
1344 static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = {
1345 [CTA_TIMEOUT_TCP_SYN_SENT] = { .type = NLA_U32 },
1346 [CTA_TIMEOUT_TCP_SYN_RECV] = { .type = NLA_U32 },
1347 [CTA_TIMEOUT_TCP_ESTABLISHED] = { .type = NLA_U32 },
1348 [CTA_TIMEOUT_TCP_FIN_WAIT] = { .type = NLA_U32 },
1349 [CTA_TIMEOUT_TCP_CLOSE_WAIT] = { .type = NLA_U32 },
1350 [CTA_TIMEOUT_TCP_LAST_ACK] = { .type = NLA_U32 },
1351 [CTA_TIMEOUT_TCP_TIME_WAIT] = { .type = NLA_U32 },
1352 [CTA_TIMEOUT_TCP_CLOSE] = { .type = NLA_U32 },
1353 [CTA_TIMEOUT_TCP_SYN_SENT2] = { .type = NLA_U32 },
1355 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1357 #ifdef CONFIG_SYSCTL
1358 static unsigned int tcp_sysctl_table_users;
1359 static struct ctl_table_header *tcp_sysctl_header;
1360 static struct ctl_table tcp_sysctl_table[] = {
1362 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1363 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1364 .maxlen = sizeof(unsigned int),
1365 .mode = 0644,
1366 .proc_handler = proc_dointvec_jiffies,
1369 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1370 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1371 .maxlen = sizeof(unsigned int),
1372 .mode = 0644,
1373 .proc_handler = proc_dointvec_jiffies,
1376 .procname = "nf_conntrack_tcp_timeout_established",
1377 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1378 .maxlen = sizeof(unsigned int),
1379 .mode = 0644,
1380 .proc_handler = proc_dointvec_jiffies,
1383 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1384 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1385 .maxlen = sizeof(unsigned int),
1386 .mode = 0644,
1387 .proc_handler = proc_dointvec_jiffies,
1390 .procname = "nf_conntrack_tcp_timeout_close_wait",
1391 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1392 .maxlen = sizeof(unsigned int),
1393 .mode = 0644,
1394 .proc_handler = proc_dointvec_jiffies,
1397 .procname = "nf_conntrack_tcp_timeout_last_ack",
1398 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1399 .maxlen = sizeof(unsigned int),
1400 .mode = 0644,
1401 .proc_handler = proc_dointvec_jiffies,
1404 .procname = "nf_conntrack_tcp_timeout_time_wait",
1405 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1406 .maxlen = sizeof(unsigned int),
1407 .mode = 0644,
1408 .proc_handler = proc_dointvec_jiffies,
1411 .procname = "nf_conntrack_tcp_timeout_close",
1412 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1413 .maxlen = sizeof(unsigned int),
1414 .mode = 0644,
1415 .proc_handler = proc_dointvec_jiffies,
1418 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1419 .data = &tcp_timeouts[TCP_CONNTRACK_RETRANS],
1420 .maxlen = sizeof(unsigned int),
1421 .mode = 0644,
1422 .proc_handler = proc_dointvec_jiffies,
1425 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
1426 .data = &tcp_timeouts[TCP_CONNTRACK_UNACK],
1427 .maxlen = sizeof(unsigned int),
1428 .mode = 0644,
1429 .proc_handler = proc_dointvec_jiffies,
1432 .procname = "nf_conntrack_tcp_loose",
1433 .data = &nf_ct_tcp_loose,
1434 .maxlen = sizeof(unsigned int),
1435 .mode = 0644,
1436 .proc_handler = proc_dointvec,
1439 .procname = "nf_conntrack_tcp_be_liberal",
1440 .data = &nf_ct_tcp_be_liberal,
1441 .maxlen = sizeof(unsigned int),
1442 .mode = 0644,
1443 .proc_handler = proc_dointvec,
1446 .procname = "nf_conntrack_tcp_max_retrans",
1447 .data = &nf_ct_tcp_max_retrans,
1448 .maxlen = sizeof(unsigned int),
1449 .mode = 0644,
1450 .proc_handler = proc_dointvec,
1455 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1456 static struct ctl_table tcp_compat_sysctl_table[] = {
1458 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1459 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT],
1460 .maxlen = sizeof(unsigned int),
1461 .mode = 0644,
1462 .proc_handler = proc_dointvec_jiffies,
1465 .procname = "ip_conntrack_tcp_timeout_syn_sent2",
1466 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_SENT2],
1467 .maxlen = sizeof(unsigned int),
1468 .mode = 0644,
1469 .proc_handler = proc_dointvec_jiffies,
1472 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1473 .data = &tcp_timeouts[TCP_CONNTRACK_SYN_RECV],
1474 .maxlen = sizeof(unsigned int),
1475 .mode = 0644,
1476 .proc_handler = proc_dointvec_jiffies,
1479 .procname = "ip_conntrack_tcp_timeout_established",
1480 .data = &tcp_timeouts[TCP_CONNTRACK_ESTABLISHED],
1481 .maxlen = sizeof(unsigned int),
1482 .mode = 0644,
1483 .proc_handler = proc_dointvec_jiffies,
1486 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1487 .data = &tcp_timeouts[TCP_CONNTRACK_FIN_WAIT],
1488 .maxlen = sizeof(unsigned int),
1489 .mode = 0644,
1490 .proc_handler = proc_dointvec_jiffies,
1493 .procname = "ip_conntrack_tcp_timeout_close_wait",
1494 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE_WAIT],
1495 .maxlen = sizeof(unsigned int),
1496 .mode = 0644,
1497 .proc_handler = proc_dointvec_jiffies,
1500 .procname = "ip_conntrack_tcp_timeout_last_ack",
1501 .data = &tcp_timeouts[TCP_CONNTRACK_LAST_ACK],
1502 .maxlen = sizeof(unsigned int),
1503 .mode = 0644,
1504 .proc_handler = proc_dointvec_jiffies,
1507 .procname = "ip_conntrack_tcp_timeout_time_wait",
1508 .data = &tcp_timeouts[TCP_CONNTRACK_TIME_WAIT],
1509 .maxlen = sizeof(unsigned int),
1510 .mode = 0644,
1511 .proc_handler = proc_dointvec_jiffies,
1514 .procname = "ip_conntrack_tcp_timeout_close",
1515 .data = &tcp_timeouts[TCP_CONNTRACK_CLOSE],
1516 .maxlen = sizeof(unsigned int),
1517 .mode = 0644,
1518 .proc_handler = proc_dointvec_jiffies,
1521 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1522 .data = &tcp_timeouts[TCP_CONNTRACK_RETRANS],
1523 .maxlen = sizeof(unsigned int),
1524 .mode = 0644,
1525 .proc_handler = proc_dointvec_jiffies,
1528 .procname = "ip_conntrack_tcp_loose",
1529 .data = &nf_ct_tcp_loose,
1530 .maxlen = sizeof(unsigned int),
1531 .mode = 0644,
1532 .proc_handler = proc_dointvec,
1535 .procname = "ip_conntrack_tcp_be_liberal",
1536 .data = &nf_ct_tcp_be_liberal,
1537 .maxlen = sizeof(unsigned int),
1538 .mode = 0644,
1539 .proc_handler = proc_dointvec,
1542 .procname = "ip_conntrack_tcp_max_retrans",
1543 .data = &nf_ct_tcp_max_retrans,
1544 .maxlen = sizeof(unsigned int),
1545 .mode = 0644,
1546 .proc_handler = proc_dointvec,
1550 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1551 #endif /* CONFIG_SYSCTL */
1553 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1555 .l3proto = PF_INET,
1556 .l4proto = IPPROTO_TCP,
1557 .name = "tcp",
1558 .pkt_to_tuple = tcp_pkt_to_tuple,
1559 .invert_tuple = tcp_invert_tuple,
1560 .print_tuple = tcp_print_tuple,
1561 .print_conntrack = tcp_print_conntrack,
1562 .packet = tcp_packet,
1563 .get_timeouts = tcp_get_timeouts,
1564 .new = tcp_new,
1565 .error = tcp_error,
1566 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1567 .to_nlattr = tcp_to_nlattr,
1568 .nlattr_size = tcp_nlattr_size,
1569 .from_nlattr = nlattr_to_tcp,
1570 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1571 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1572 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1573 .nla_policy = nf_ct_port_nla_policy,
1574 #endif
1575 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1576 .ctnl_timeout = {
1577 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1578 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1579 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1580 .obj_size = sizeof(unsigned int) *
1581 TCP_CONNTRACK_TIMEOUT_MAX,
1582 .nla_policy = tcp_timeout_nla_policy,
1584 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1585 #ifdef CONFIG_SYSCTL
1586 .ctl_table_users = &tcp_sysctl_table_users,
1587 .ctl_table_header = &tcp_sysctl_header,
1588 .ctl_table = tcp_sysctl_table,
1589 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1590 .ctl_compat_table = tcp_compat_sysctl_table,
1591 #endif
1592 #endif
1594 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1596 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1598 .l3proto = PF_INET6,
1599 .l4proto = IPPROTO_TCP,
1600 .name = "tcp",
1601 .pkt_to_tuple = tcp_pkt_to_tuple,
1602 .invert_tuple = tcp_invert_tuple,
1603 .print_tuple = tcp_print_tuple,
1604 .print_conntrack = tcp_print_conntrack,
1605 .packet = tcp_packet,
1606 .get_timeouts = tcp_get_timeouts,
1607 .new = tcp_new,
1608 .error = tcp_error,
1609 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1610 .to_nlattr = tcp_to_nlattr,
1611 .nlattr_size = tcp_nlattr_size,
1612 .from_nlattr = nlattr_to_tcp,
1613 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1614 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1615 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1616 .nla_policy = nf_ct_port_nla_policy,
1617 #endif
1618 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1619 .ctnl_timeout = {
1620 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1621 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1622 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1623 .obj_size = sizeof(unsigned int) *
1624 TCP_CONNTRACK_TIMEOUT_MAX,
1625 .nla_policy = tcp_timeout_nla_policy,
1627 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1628 #ifdef CONFIG_SYSCTL
1629 .ctl_table_users = &tcp_sysctl_table_users,
1630 .ctl_table_header = &tcp_sysctl_header,
1631 .ctl_table = tcp_sysctl_table,
1632 #endif
1634 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);