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.
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.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>
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
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
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
[] = {
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. */
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
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
] = {
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
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.
157 * sTW -> sSS Reopened connection (RFC 1122).
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
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.
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.
184 * sSR -> sFW Close started.
186 * sFW -> sLA FIN seen in both directions, waiting for
188 * Migth be a retransmitted FIN as well...
190 * sLA -> sLA Retransmitted FIN. Remain in the same state.
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.
200 * sSR -> sES Established state is reached.
202 * sFW -> sCW Normal close request answered by ACK.
204 * sLA -> sTW Last ACK detected.
205 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
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
}
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
225 * sTW -> sIV Reopened connection, but server may not do it.
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
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.
246 * sSR -> sFW Close started.
248 * sFW -> sLA FIN seen in both directions.
250 * sLA -> sLA Retransmitted FIN.
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.
259 * sSR -> sSR Might answer late resent SYN.
261 * sFW -> sCW Normal close request answered by ACK.
263 * sLA -> sTW Last ACK detected.
264 * sTW -> sTW Retransmitted last ACK.
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
;
279 /* Actually only need first 8 bytes. */
280 hp
= skb_header_pointer(skb
, dataoff
, 8, &_hdr
);
284 tuple
->src
.u
.tcp
.port
= hp
->source
;
285 tuple
->dst
.u
.tcp
.port
= hp
->dest
;
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
;
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
,
359 unsigned int dataoff
,
360 const struct tcphdr
*tcph
)
362 /* XXX Should I use payload length field in IP/IPv6 header ?
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 \
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
);
389 ptr
= skb_header_pointer(skb
, dataoff
+ sizeof(struct tcphdr
),
403 case TCPOPT_NOP
: /* Ref: RFC 793 section 3.1 */
408 if (opsize
< 2) /* "silly options" */
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) {
422 state
->td_scale
= 14;
425 IP_CT_TCP_FLAG_WINDOW_SCALE
;
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
);
444 ptr
= skb_header_pointer(skb
, dataoff
+ sizeof(struct tcphdr
),
448 /* Fast path for timestamp-only option */
449 if (length
== TCPOLEN_TSTAMP_ALIGNED
450 && *(__be32
*)ptr
== htonl((TCPOPT_NOP
<< 24)
452 | (TCPOPT_TIMESTAMP
<< 8)
453 | TCPOLEN_TIMESTAMP
))
463 case TCPOPT_NOP
: /* Ref: RFC 793 section 3.1 */
468 if (opsize
< 2) /* "silly options" */
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
)) {
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
))
494 #ifdef CONFIG_NF_NAT_NEEDED
495 static inline s16
nat_offset(const struct nf_conn
*ct
,
496 enum ip_conntrack_dir dir
,
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)
506 #define NAT_OFFSET(pf, ct, dir, seq) 0
509 static bool tcp_in_window(const struct nf_conn
*ct
,
510 struct ip_ct_tcp
*state
,
511 enum ip_conntrack_dir dir
,
513 const struct sk_buff
*skb
,
514 unsigned int dataoff
,
515 const struct tcphdr
*tcph
,
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
;
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
,
551 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
554 if (sender
->td_maxwin
== 0) {
556 * Initialize sender data.
560 * SYN-ACK in reply to a SYN
561 * or SYN from reply direction in simultaneous open.
564 sender
->td_maxend
= end
;
565 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
567 tcp_options(skb
, dataoff
, tcph
, sender
);
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
))
576 receiver
->td_scale
= 0;
578 /* Simultaneous open */
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 win
<<= sender
->td_scale
;
588 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
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."
609 sender
->td_maxend
= end
;
610 sender
->td_maxwin
= (win
== 0 ? 1 : win
);
612 tcp_options(skb
, dataoff
, tcph
, sender
);
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
))
624 * Broken TCP stacks, that set ACK in RST packets as well
625 * with zero ack value.
627 ack
= sack
= receiver
->td_end
;
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
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
,
650 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
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).
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
;
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
;
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
)
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
;
722 if (sender
->flags
& IP_CT_TCP_FLAG_BE_LIBERAL
||
723 nf_ct_tcp_be_liberal
)
725 if (!res
&& LOG_INVALID(net
, IPPROTO_TCP
))
726 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
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
);
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
|
751 [TCPHDR_SYN
|TCPHDR_URG
] = 1,
752 [TCPHDR_SYN
|TCPHDR_ACK
] = 1,
754 [TCPHDR_RST
|TCPHDR_ACK
] = 1,
755 [TCPHDR_FIN
|TCPHDR_ACK
] = 1,
756 [TCPHDR_FIN
|TCPHDR_ACK
|TCPHDR_URG
] = 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
,
764 unsigned int dataoff
,
765 enum ip_conntrack_info
*ctinfo
,
767 unsigned int hooknum
)
769 const struct tcphdr
*th
;
771 unsigned int tcplen
= skb
->len
- dataoff
;
774 /* Smaller that minimal TCP header? */
775 th
= skb_header_pointer(skb
, dataoff
, sizeof(_tcph
), &_tcph
);
777 if (LOG_INVALID(net
, IPPROTO_TCP
))
778 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
779 "nf_ct_tcp: short packet ");
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 ");
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 ");
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 ");
816 static unsigned int *tcp_get_timeouts(struct net
*net
)
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
,
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
;
836 unsigned long timeout
;
839 th
= skb_header_pointer(skb
, dataoff
, sizeof(_tcph
), &_tcph
);
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
;
850 case TCP_CONNTRACK_SYN_SENT
:
851 if (old_state
< TCP_CONNTRACK_TIME_WAIT
)
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.
883 case TCP_CONNTRACK_IGNORE
:
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.
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
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
));
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 ");
957 case TCP_CONNTRACK_MAX
:
959 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
960 dir
, get_conntrack_index(th
), old_state
);
961 spin_unlock_bh(&ct
->lock
);
962 if (LOG_INVALID(net
, IPPROTO_TCP
))
963 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
964 "nf_ct_tcp: invalid state ");
966 case TCP_CONNTRACK_CLOSE
:
967 if (index
== TCP_RST_SET
968 && (ct
->proto
.tcp
.seen
[!dir
].flags
& IP_CT_TCP_FLAG_MAXACK_SET
)
969 && before(ntohl(th
->seq
), ct
->proto
.tcp
.seen
[!dir
].td_maxack
)) {
971 spin_unlock_bh(&ct
->lock
);
972 if (LOG_INVALID(net
, IPPROTO_TCP
))
973 nf_log_packet(pf
, 0, skb
, NULL
, NULL
, NULL
,
974 "nf_ct_tcp: invalid RST ");
977 if (index
== TCP_RST_SET
978 && ((test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)
979 && ct
->proto
.tcp
.last_index
== TCP_SYN_SET
)
980 || (!test_bit(IPS_ASSURED_BIT
, &ct
->status
)
981 && ct
->proto
.tcp
.last_index
== TCP_ACK_SET
))
982 && ntohl(th
->ack_seq
) == ct
->proto
.tcp
.last_end
) {
983 /* RST sent to invalid SYN or ACK we had let through
984 * at a) and c) above:
986 * a) SYN was in window then
987 * c) we hold a half-open connection.
989 * Delete our connection entry.
990 * We skip window checking, because packet might ACK
991 * segments we ignored. */
994 /* Just fall through */
996 /* Keep compilers happy. */
1000 if (!tcp_in_window(ct
, &ct
->proto
.tcp
, dir
, index
,
1001 skb
, dataoff
, th
, pf
)) {
1002 spin_unlock_bh(&ct
->lock
);
1006 /* From now on we have got in-window packets */
1007 ct
->proto
.tcp
.last_index
= index
;
1008 ct
->proto
.tcp
.last_dir
= dir
;
1010 pr_debug("tcp_conntracks: ");
1011 nf_ct_dump_tuple(tuple
);
1012 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1013 (th
->syn
? 1 : 0), (th
->ack
? 1 : 0),
1014 (th
->fin
? 1 : 0), (th
->rst
? 1 : 0),
1015 old_state
, new_state
);
1017 ct
->proto
.tcp
.state
= new_state
;
1018 if (old_state
!= new_state
1019 && new_state
== TCP_CONNTRACK_FIN_WAIT
)
1020 ct
->proto
.tcp
.seen
[dir
].flags
|= IP_CT_TCP_FLAG_CLOSE_INIT
;
1022 if (ct
->proto
.tcp
.retrans
>= nf_ct_tcp_max_retrans
&&
1023 timeouts
[new_state
] > timeouts
[TCP_CONNTRACK_RETRANS
])
1024 timeout
= timeouts
[TCP_CONNTRACK_RETRANS
];
1025 else if ((ct
->proto
.tcp
.seen
[0].flags
| ct
->proto
.tcp
.seen
[1].flags
) &
1026 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED
&&
1027 timeouts
[new_state
] > timeouts
[TCP_CONNTRACK_UNACK
])
1028 timeout
= timeouts
[TCP_CONNTRACK_UNACK
];
1030 timeout
= timeouts
[new_state
];
1031 spin_unlock_bh(&ct
->lock
);
1033 if (new_state
!= old_state
)
1034 nf_conntrack_event_cache(IPCT_PROTOINFO
, ct
);
1036 if (!test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
1037 /* If only reply is a RST, we can consider ourselves not to
1038 have an established connection: this is a fairly common
1039 problem case, so we can delete the conntrack
1040 immediately. --RR */
1042 nf_ct_kill_acct(ct
, ctinfo
, skb
);
1045 } else if (!test_bit(IPS_ASSURED_BIT
, &ct
->status
)
1046 && (old_state
== TCP_CONNTRACK_SYN_RECV
1047 || old_state
== TCP_CONNTRACK_ESTABLISHED
)
1048 && new_state
== TCP_CONNTRACK_ESTABLISHED
) {
1049 /* Set ASSURED if we see see valid ack in ESTABLISHED
1050 after SYN_RECV or a valid answer for a picked up
1052 set_bit(IPS_ASSURED_BIT
, &ct
->status
);
1053 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
1055 nf_ct_refresh_acct(ct
, ctinfo
, skb
, timeout
);
1060 /* Called when a new connection for this protocol found. */
1061 static bool tcp_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
1062 unsigned int dataoff
, unsigned int *timeouts
)
1064 enum tcp_conntrack new_state
;
1065 const struct tcphdr
*th
;
1066 struct tcphdr _tcph
;
1067 const struct ip_ct_tcp_state
*sender
= &ct
->proto
.tcp
.seen
[0];
1068 const struct ip_ct_tcp_state
*receiver
= &ct
->proto
.tcp
.seen
[1];
1070 th
= skb_header_pointer(skb
, dataoff
, sizeof(_tcph
), &_tcph
);
1073 /* Don't need lock here: this conntrack not in circulation yet */
1074 new_state
= tcp_conntracks
[0][get_conntrack_index(th
)][TCP_CONNTRACK_NONE
];
1076 /* Invalid: delete conntrack */
1077 if (new_state
>= TCP_CONNTRACK_MAX
) {
1078 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1082 if (new_state
== TCP_CONNTRACK_SYN_SENT
) {
1083 memset(&ct
->proto
.tcp
, 0, sizeof(ct
->proto
.tcp
));
1085 ct
->proto
.tcp
.seen
[0].td_end
=
1086 segment_seq_plus_len(ntohl(th
->seq
), skb
->len
,
1088 ct
->proto
.tcp
.seen
[0].td_maxwin
= ntohs(th
->window
);
1089 if (ct
->proto
.tcp
.seen
[0].td_maxwin
== 0)
1090 ct
->proto
.tcp
.seen
[0].td_maxwin
= 1;
1091 ct
->proto
.tcp
.seen
[0].td_maxend
=
1092 ct
->proto
.tcp
.seen
[0].td_end
;
1094 tcp_options(skb
, dataoff
, th
, &ct
->proto
.tcp
.seen
[0]);
1095 } else if (nf_ct_tcp_loose
== 0) {
1096 /* Don't try to pick up connections. */
1099 memset(&ct
->proto
.tcp
, 0, sizeof(ct
->proto
.tcp
));
1101 * We are in the middle of a connection,
1102 * its history is lost for us.
1103 * Let's try to use the data from the packet.
1105 ct
->proto
.tcp
.seen
[0].td_end
=
1106 segment_seq_plus_len(ntohl(th
->seq
), skb
->len
,
1108 ct
->proto
.tcp
.seen
[0].td_maxwin
= ntohs(th
->window
);
1109 if (ct
->proto
.tcp
.seen
[0].td_maxwin
== 0)
1110 ct
->proto
.tcp
.seen
[0].td_maxwin
= 1;
1111 ct
->proto
.tcp
.seen
[0].td_maxend
=
1112 ct
->proto
.tcp
.seen
[0].td_end
+
1113 ct
->proto
.tcp
.seen
[0].td_maxwin
;
1115 /* We assume SACK and liberal window checking to handle
1117 ct
->proto
.tcp
.seen
[0].flags
=
1118 ct
->proto
.tcp
.seen
[1].flags
= IP_CT_TCP_FLAG_SACK_PERM
|
1119 IP_CT_TCP_FLAG_BE_LIBERAL
;
1122 /* tcp_packet will set them */
1123 ct
->proto
.tcp
.last_index
= TCP_NONE_SET
;
1125 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1126 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1127 sender
->td_end
, sender
->td_maxend
, sender
->td_maxwin
,
1129 receiver
->td_end
, receiver
->td_maxend
, receiver
->td_maxwin
,
1130 receiver
->td_scale
);
1134 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1136 #include <linux/netfilter/nfnetlink.h>
1137 #include <linux/netfilter/nfnetlink_conntrack.h>
1139 static int tcp_to_nlattr(struct sk_buff
*skb
, struct nlattr
*nla
,
1142 struct nlattr
*nest_parms
;
1143 struct nf_ct_tcp_flags tmp
= {};
1145 spin_lock_bh(&ct
->lock
);
1146 nest_parms
= nla_nest_start(skb
, CTA_PROTOINFO_TCP
| NLA_F_NESTED
);
1148 goto nla_put_failure
;
1150 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
);
1155 NLA_PUT_U8(skb
, CTA_PROTOINFO_TCP_WSCALE_REPLY
,
1156 ct
->proto
.tcp
.seen
[1].td_scale
);
1158 tmp
.flags
= ct
->proto
.tcp
.seen
[0].flags
;
1159 NLA_PUT(skb
, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL
,
1160 sizeof(struct nf_ct_tcp_flags
), &tmp
);
1162 tmp
.flags
= ct
->proto
.tcp
.seen
[1].flags
;
1163 NLA_PUT(skb
, CTA_PROTOINFO_TCP_FLAGS_REPLY
,
1164 sizeof(struct nf_ct_tcp_flags
), &tmp
);
1165 spin_unlock_bh(&ct
->lock
);
1167 nla_nest_end(skb
, nest_parms
);
1172 spin_unlock_bh(&ct
->lock
);
1176 static const struct nla_policy tcp_nla_policy
[CTA_PROTOINFO_TCP_MAX
+1] = {
1177 [CTA_PROTOINFO_TCP_STATE
] = { .type
= NLA_U8
},
1178 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL
] = { .type
= NLA_U8
},
1179 [CTA_PROTOINFO_TCP_WSCALE_REPLY
] = { .type
= NLA_U8
},
1180 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL
] = { .len
= sizeof(struct nf_ct_tcp_flags
) },
1181 [CTA_PROTOINFO_TCP_FLAGS_REPLY
] = { .len
= sizeof(struct nf_ct_tcp_flags
) },
1184 static int nlattr_to_tcp(struct nlattr
*cda
[], struct nf_conn
*ct
)
1186 struct nlattr
*pattr
= cda
[CTA_PROTOINFO_TCP
];
1187 struct nlattr
*tb
[CTA_PROTOINFO_TCP_MAX
+1];
1190 /* updates could not contain anything about the private
1191 * protocol info, in that case skip the parsing */
1195 err
= nla_parse_nested(tb
, CTA_PROTOINFO_TCP_MAX
, pattr
, tcp_nla_policy
);
1199 if (tb
[CTA_PROTOINFO_TCP_STATE
] &&
1200 nla_get_u8(tb
[CTA_PROTOINFO_TCP_STATE
]) >= TCP_CONNTRACK_MAX
)
1203 spin_lock_bh(&ct
->lock
);
1204 if (tb
[CTA_PROTOINFO_TCP_STATE
])
1205 ct
->proto
.tcp
.state
= nla_get_u8(tb
[CTA_PROTOINFO_TCP_STATE
]);
1207 if (tb
[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL
]) {
1208 struct nf_ct_tcp_flags
*attr
=
1209 nla_data(tb
[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL
]);
1210 ct
->proto
.tcp
.seen
[0].flags
&= ~attr
->mask
;
1211 ct
->proto
.tcp
.seen
[0].flags
|= attr
->flags
& attr
->mask
;
1214 if (tb
[CTA_PROTOINFO_TCP_FLAGS_REPLY
]) {
1215 struct nf_ct_tcp_flags
*attr
=
1216 nla_data(tb
[CTA_PROTOINFO_TCP_FLAGS_REPLY
]);
1217 ct
->proto
.tcp
.seen
[1].flags
&= ~attr
->mask
;
1218 ct
->proto
.tcp
.seen
[1].flags
|= attr
->flags
& attr
->mask
;
1221 if (tb
[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL
] &&
1222 tb
[CTA_PROTOINFO_TCP_WSCALE_REPLY
] &&
1223 ct
->proto
.tcp
.seen
[0].flags
& IP_CT_TCP_FLAG_WINDOW_SCALE
&&
1224 ct
->proto
.tcp
.seen
[1].flags
& IP_CT_TCP_FLAG_WINDOW_SCALE
) {
1225 ct
->proto
.tcp
.seen
[0].td_scale
=
1226 nla_get_u8(tb
[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL
]);
1227 ct
->proto
.tcp
.seen
[1].td_scale
=
1228 nla_get_u8(tb
[CTA_PROTOINFO_TCP_WSCALE_REPLY
]);
1230 spin_unlock_bh(&ct
->lock
);
1235 static int tcp_nlattr_size(void)
1237 return nla_total_size(0) /* CTA_PROTOINFO_TCP */
1238 + nla_policy_len(tcp_nla_policy
, CTA_PROTOINFO_TCP_MAX
+ 1);
1241 static int tcp_nlattr_tuple_size(void)
1243 return nla_policy_len(nf_ct_port_nla_policy
, CTA_PROTO_MAX
+ 1);
1247 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1249 #include <linux/netfilter/nfnetlink.h>
1250 #include <linux/netfilter/nfnetlink_cttimeout.h>
1252 static int tcp_timeout_nlattr_to_obj(struct nlattr
*tb
[], void *data
)
1254 unsigned int *timeouts
= data
;
1257 /* set default TCP timeouts. */
1258 for (i
=0; i
<TCP_CONNTRACK_TIMEOUT_MAX
; i
++)
1259 timeouts
[i
] = tcp_timeouts
[i
];
1261 if (tb
[CTA_TIMEOUT_TCP_SYN_SENT
]) {
1262 timeouts
[TCP_CONNTRACK_SYN_SENT
] =
1263 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_SYN_SENT
]))*HZ
;
1265 if (tb
[CTA_TIMEOUT_TCP_SYN_RECV
]) {
1266 timeouts
[TCP_CONNTRACK_SYN_RECV
] =
1267 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_SYN_RECV
]))*HZ
;
1269 if (tb
[CTA_TIMEOUT_TCP_ESTABLISHED
]) {
1270 timeouts
[TCP_CONNTRACK_ESTABLISHED
] =
1271 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_ESTABLISHED
]))*HZ
;
1273 if (tb
[CTA_TIMEOUT_TCP_FIN_WAIT
]) {
1274 timeouts
[TCP_CONNTRACK_FIN_WAIT
] =
1275 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_FIN_WAIT
]))*HZ
;
1277 if (tb
[CTA_TIMEOUT_TCP_CLOSE_WAIT
]) {
1278 timeouts
[TCP_CONNTRACK_CLOSE_WAIT
] =
1279 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_CLOSE_WAIT
]))*HZ
;
1281 if (tb
[CTA_TIMEOUT_TCP_LAST_ACK
]) {
1282 timeouts
[TCP_CONNTRACK_LAST_ACK
] =
1283 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_LAST_ACK
]))*HZ
;
1285 if (tb
[CTA_TIMEOUT_TCP_TIME_WAIT
]) {
1286 timeouts
[TCP_CONNTRACK_TIME_WAIT
] =
1287 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_TIME_WAIT
]))*HZ
;
1289 if (tb
[CTA_TIMEOUT_TCP_CLOSE
]) {
1290 timeouts
[TCP_CONNTRACK_CLOSE
] =
1291 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_CLOSE
]))*HZ
;
1293 if (tb
[CTA_TIMEOUT_TCP_SYN_SENT2
]) {
1294 timeouts
[TCP_CONNTRACK_SYN_SENT2
] =
1295 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_SYN_SENT2
]))*HZ
;
1297 if (tb
[CTA_TIMEOUT_TCP_RETRANS
]) {
1298 timeouts
[TCP_CONNTRACK_RETRANS
] =
1299 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_RETRANS
]))*HZ
;
1301 if (tb
[CTA_TIMEOUT_TCP_UNACK
]) {
1302 timeouts
[TCP_CONNTRACK_UNACK
] =
1303 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_TCP_UNACK
]))*HZ
;
1309 tcp_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
1311 const unsigned int *timeouts
= data
;
1313 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_SYN_SENT
,
1314 htonl(timeouts
[TCP_CONNTRACK_SYN_SENT
] / HZ
));
1315 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_SYN_RECV
,
1316 htonl(timeouts
[TCP_CONNTRACK_SYN_RECV
] / HZ
));
1317 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_ESTABLISHED
,
1318 htonl(timeouts
[TCP_CONNTRACK_ESTABLISHED
] / HZ
));
1319 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_FIN_WAIT
,
1320 htonl(timeouts
[TCP_CONNTRACK_FIN_WAIT
] / HZ
));
1321 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_CLOSE_WAIT
,
1322 htonl(timeouts
[TCP_CONNTRACK_CLOSE_WAIT
] / HZ
));
1323 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_LAST_ACK
,
1324 htonl(timeouts
[TCP_CONNTRACK_LAST_ACK
] / HZ
));
1325 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_TIME_WAIT
,
1326 htonl(timeouts
[TCP_CONNTRACK_TIME_WAIT
] / HZ
));
1327 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_CLOSE
,
1328 htonl(timeouts
[TCP_CONNTRACK_CLOSE
] / HZ
));
1329 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_SYN_SENT2
,
1330 htonl(timeouts
[TCP_CONNTRACK_SYN_SENT2
] / HZ
));
1331 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_RETRANS
,
1332 htonl(timeouts
[TCP_CONNTRACK_RETRANS
] / HZ
));
1333 NLA_PUT_BE32(skb
, CTA_TIMEOUT_TCP_UNACK
,
1334 htonl(timeouts
[TCP_CONNTRACK_UNACK
] / HZ
));
1341 static const struct nla_policy tcp_timeout_nla_policy
[CTA_TIMEOUT_TCP_MAX
+1] = {
1342 [CTA_TIMEOUT_TCP_SYN_SENT
] = { .type
= NLA_U32
},
1343 [CTA_TIMEOUT_TCP_SYN_RECV
] = { .type
= NLA_U32
},
1344 [CTA_TIMEOUT_TCP_ESTABLISHED
] = { .type
= NLA_U32
},
1345 [CTA_TIMEOUT_TCP_FIN_WAIT
] = { .type
= NLA_U32
},
1346 [CTA_TIMEOUT_TCP_CLOSE_WAIT
] = { .type
= NLA_U32
},
1347 [CTA_TIMEOUT_TCP_LAST_ACK
] = { .type
= NLA_U32
},
1348 [CTA_TIMEOUT_TCP_TIME_WAIT
] = { .type
= NLA_U32
},
1349 [CTA_TIMEOUT_TCP_CLOSE
] = { .type
= NLA_U32
},
1350 [CTA_TIMEOUT_TCP_SYN_SENT2
] = { .type
= NLA_U32
},
1352 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1354 #ifdef CONFIG_SYSCTL
1355 static unsigned int tcp_sysctl_table_users
;
1356 static struct ctl_table_header
*tcp_sysctl_header
;
1357 static struct ctl_table tcp_sysctl_table
[] = {
1359 .procname
= "nf_conntrack_tcp_timeout_syn_sent",
1360 .data
= &tcp_timeouts
[TCP_CONNTRACK_SYN_SENT
],
1361 .maxlen
= sizeof(unsigned int),
1363 .proc_handler
= proc_dointvec_jiffies
,
1366 .procname
= "nf_conntrack_tcp_timeout_syn_recv",
1367 .data
= &tcp_timeouts
[TCP_CONNTRACK_SYN_RECV
],
1368 .maxlen
= sizeof(unsigned int),
1370 .proc_handler
= proc_dointvec_jiffies
,
1373 .procname
= "nf_conntrack_tcp_timeout_established",
1374 .data
= &tcp_timeouts
[TCP_CONNTRACK_ESTABLISHED
],
1375 .maxlen
= sizeof(unsigned int),
1377 .proc_handler
= proc_dointvec_jiffies
,
1380 .procname
= "nf_conntrack_tcp_timeout_fin_wait",
1381 .data
= &tcp_timeouts
[TCP_CONNTRACK_FIN_WAIT
],
1382 .maxlen
= sizeof(unsigned int),
1384 .proc_handler
= proc_dointvec_jiffies
,
1387 .procname
= "nf_conntrack_tcp_timeout_close_wait",
1388 .data
= &tcp_timeouts
[TCP_CONNTRACK_CLOSE_WAIT
],
1389 .maxlen
= sizeof(unsigned int),
1391 .proc_handler
= proc_dointvec_jiffies
,
1394 .procname
= "nf_conntrack_tcp_timeout_last_ack",
1395 .data
= &tcp_timeouts
[TCP_CONNTRACK_LAST_ACK
],
1396 .maxlen
= sizeof(unsigned int),
1398 .proc_handler
= proc_dointvec_jiffies
,
1401 .procname
= "nf_conntrack_tcp_timeout_time_wait",
1402 .data
= &tcp_timeouts
[TCP_CONNTRACK_TIME_WAIT
],
1403 .maxlen
= sizeof(unsigned int),
1405 .proc_handler
= proc_dointvec_jiffies
,
1408 .procname
= "nf_conntrack_tcp_timeout_close",
1409 .data
= &tcp_timeouts
[TCP_CONNTRACK_CLOSE
],
1410 .maxlen
= sizeof(unsigned int),
1412 .proc_handler
= proc_dointvec_jiffies
,
1415 .procname
= "nf_conntrack_tcp_timeout_max_retrans",
1416 .data
= &tcp_timeouts
[TCP_CONNTRACK_RETRANS
],
1417 .maxlen
= sizeof(unsigned int),
1419 .proc_handler
= proc_dointvec_jiffies
,
1422 .procname
= "nf_conntrack_tcp_timeout_unacknowledged",
1423 .data
= &tcp_timeouts
[TCP_CONNTRACK_UNACK
],
1424 .maxlen
= sizeof(unsigned int),
1426 .proc_handler
= proc_dointvec_jiffies
,
1429 .procname
= "nf_conntrack_tcp_loose",
1430 .data
= &nf_ct_tcp_loose
,
1431 .maxlen
= sizeof(unsigned int),
1433 .proc_handler
= proc_dointvec
,
1436 .procname
= "nf_conntrack_tcp_be_liberal",
1437 .data
= &nf_ct_tcp_be_liberal
,
1438 .maxlen
= sizeof(unsigned int),
1440 .proc_handler
= proc_dointvec
,
1443 .procname
= "nf_conntrack_tcp_max_retrans",
1444 .data
= &nf_ct_tcp_max_retrans
,
1445 .maxlen
= sizeof(unsigned int),
1447 .proc_handler
= proc_dointvec
,
1452 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1453 static struct ctl_table tcp_compat_sysctl_table
[] = {
1455 .procname
= "ip_conntrack_tcp_timeout_syn_sent",
1456 .data
= &tcp_timeouts
[TCP_CONNTRACK_SYN_SENT
],
1457 .maxlen
= sizeof(unsigned int),
1459 .proc_handler
= proc_dointvec_jiffies
,
1462 .procname
= "ip_conntrack_tcp_timeout_syn_sent2",
1463 .data
= &tcp_timeouts
[TCP_CONNTRACK_SYN_SENT2
],
1464 .maxlen
= sizeof(unsigned int),
1466 .proc_handler
= proc_dointvec_jiffies
,
1469 .procname
= "ip_conntrack_tcp_timeout_syn_recv",
1470 .data
= &tcp_timeouts
[TCP_CONNTRACK_SYN_RECV
],
1471 .maxlen
= sizeof(unsigned int),
1473 .proc_handler
= proc_dointvec_jiffies
,
1476 .procname
= "ip_conntrack_tcp_timeout_established",
1477 .data
= &tcp_timeouts
[TCP_CONNTRACK_ESTABLISHED
],
1478 .maxlen
= sizeof(unsigned int),
1480 .proc_handler
= proc_dointvec_jiffies
,
1483 .procname
= "ip_conntrack_tcp_timeout_fin_wait",
1484 .data
= &tcp_timeouts
[TCP_CONNTRACK_FIN_WAIT
],
1485 .maxlen
= sizeof(unsigned int),
1487 .proc_handler
= proc_dointvec_jiffies
,
1490 .procname
= "ip_conntrack_tcp_timeout_close_wait",
1491 .data
= &tcp_timeouts
[TCP_CONNTRACK_CLOSE_WAIT
],
1492 .maxlen
= sizeof(unsigned int),
1494 .proc_handler
= proc_dointvec_jiffies
,
1497 .procname
= "ip_conntrack_tcp_timeout_last_ack",
1498 .data
= &tcp_timeouts
[TCP_CONNTRACK_LAST_ACK
],
1499 .maxlen
= sizeof(unsigned int),
1501 .proc_handler
= proc_dointvec_jiffies
,
1504 .procname
= "ip_conntrack_tcp_timeout_time_wait",
1505 .data
= &tcp_timeouts
[TCP_CONNTRACK_TIME_WAIT
],
1506 .maxlen
= sizeof(unsigned int),
1508 .proc_handler
= proc_dointvec_jiffies
,
1511 .procname
= "ip_conntrack_tcp_timeout_close",
1512 .data
= &tcp_timeouts
[TCP_CONNTRACK_CLOSE
],
1513 .maxlen
= sizeof(unsigned int),
1515 .proc_handler
= proc_dointvec_jiffies
,
1518 .procname
= "ip_conntrack_tcp_timeout_max_retrans",
1519 .data
= &tcp_timeouts
[TCP_CONNTRACK_RETRANS
],
1520 .maxlen
= sizeof(unsigned int),
1522 .proc_handler
= proc_dointvec_jiffies
,
1525 .procname
= "ip_conntrack_tcp_loose",
1526 .data
= &nf_ct_tcp_loose
,
1527 .maxlen
= sizeof(unsigned int),
1529 .proc_handler
= proc_dointvec
,
1532 .procname
= "ip_conntrack_tcp_be_liberal",
1533 .data
= &nf_ct_tcp_be_liberal
,
1534 .maxlen
= sizeof(unsigned int),
1536 .proc_handler
= proc_dointvec
,
1539 .procname
= "ip_conntrack_tcp_max_retrans",
1540 .data
= &nf_ct_tcp_max_retrans
,
1541 .maxlen
= sizeof(unsigned int),
1543 .proc_handler
= proc_dointvec
,
1547 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
1548 #endif /* CONFIG_SYSCTL */
1550 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly
=
1553 .l4proto
= IPPROTO_TCP
,
1555 .pkt_to_tuple
= tcp_pkt_to_tuple
,
1556 .invert_tuple
= tcp_invert_tuple
,
1557 .print_tuple
= tcp_print_tuple
,
1558 .print_conntrack
= tcp_print_conntrack
,
1559 .packet
= tcp_packet
,
1560 .get_timeouts
= tcp_get_timeouts
,
1563 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1564 .to_nlattr
= tcp_to_nlattr
,
1565 .nlattr_size
= tcp_nlattr_size
,
1566 .from_nlattr
= nlattr_to_tcp
,
1567 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
1568 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
1569 .nlattr_tuple_size
= tcp_nlattr_tuple_size
,
1570 .nla_policy
= nf_ct_port_nla_policy
,
1572 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1574 .nlattr_to_obj
= tcp_timeout_nlattr_to_obj
,
1575 .obj_to_nlattr
= tcp_timeout_obj_to_nlattr
,
1576 .nlattr_max
= CTA_TIMEOUT_TCP_MAX
,
1577 .obj_size
= sizeof(unsigned int) *
1578 TCP_CONNTRACK_TIMEOUT_MAX
,
1579 .nla_policy
= tcp_timeout_nla_policy
,
1581 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1582 #ifdef CONFIG_SYSCTL
1583 .ctl_table_users
= &tcp_sysctl_table_users
,
1584 .ctl_table_header
= &tcp_sysctl_header
,
1585 .ctl_table
= tcp_sysctl_table
,
1586 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1587 .ctl_compat_table
= tcp_compat_sysctl_table
,
1591 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4
);
1593 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly
=
1595 .l3proto
= PF_INET6
,
1596 .l4proto
= IPPROTO_TCP
,
1598 .pkt_to_tuple
= tcp_pkt_to_tuple
,
1599 .invert_tuple
= tcp_invert_tuple
,
1600 .print_tuple
= tcp_print_tuple
,
1601 .print_conntrack
= tcp_print_conntrack
,
1602 .packet
= tcp_packet
,
1603 .get_timeouts
= tcp_get_timeouts
,
1606 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1607 .to_nlattr
= tcp_to_nlattr
,
1608 .nlattr_size
= tcp_nlattr_size
,
1609 .from_nlattr
= nlattr_to_tcp
,
1610 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
1611 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
1612 .nlattr_tuple_size
= tcp_nlattr_tuple_size
,
1613 .nla_policy
= nf_ct_port_nla_policy
,
1615 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1617 .nlattr_to_obj
= tcp_timeout_nlattr_to_obj
,
1618 .obj_to_nlattr
= tcp_timeout_obj_to_nlattr
,
1619 .nlattr_max
= CTA_TIMEOUT_TCP_MAX
,
1620 .obj_size
= sizeof(unsigned int) *
1621 TCP_CONNTRACK_TIMEOUT_MAX
,
1622 .nla_policy
= tcp_timeout_nla_policy
,
1624 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1625 #ifdef CONFIG_SYSCTL
1626 .ctl_table_users
= &tcp_sysctl_table_users
,
1627 .ctl_table_header
= &tcp_sysctl_header
,
1628 .ctl_table
= tcp_sysctl_table
,
1631 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6
);