Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9fb9cbb1 YK |
2 | /* (C) 1999-2001 Paul `Rusty' Russell |
3 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
fe03d474 | 4 | * (C) 2002-2013 Jozsef Kadlecsik <kadlec@netfilter.org> |
f229f6ce | 5 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
9fb9cbb1 YK |
6 | */ |
7 | ||
9fb9cbb1 | 8 | #include <linux/types.h> |
9fb9cbb1 | 9 | #include <linux/timer.h> |
9fb9cbb1 YK |
10 | #include <linux/module.h> |
11 | #include <linux/in.h> | |
12 | #include <linux/tcp.h> | |
13 | #include <linux/spinlock.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/ipv6.h> | |
16 | #include <net/ip6_checksum.h> | |
534f81a5 | 17 | #include <asm/unaligned.h> |
9fb9cbb1 YK |
18 | |
19 | #include <net/tcp.h> | |
20 | ||
21 | #include <linux/netfilter.h> | |
22 | #include <linux/netfilter_ipv4.h> | |
23 | #include <linux/netfilter_ipv6.h> | |
24 | #include <net/netfilter/nf_conntrack.h> | |
605dcad6 | 25 | #include <net/netfilter/nf_conntrack_l4proto.h> |
f6180121 | 26 | #include <net/netfilter/nf_conntrack_ecache.h> |
41d73ec0 | 27 | #include <net/netfilter/nf_conntrack_seqadj.h> |
48b1de4c | 28 | #include <net/netfilter/nf_conntrack_synproxy.h> |
c779e849 | 29 | #include <net/netfilter/nf_conntrack_timeout.h> |
f01ffbd6 | 30 | #include <net/netfilter/nf_log.h> |
9d2493f8 CP |
31 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
32 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> | |
9fb9cbb1 | 33 | |
9fb9cbb1 YK |
34 | /* FIXME: Examine ipfilter's timeouts and conntrack transitions more |
35 | closely. They're more complex. --RR */ | |
36 | ||
82f568fc | 37 | static const char *const tcp_conntrack_names[] = { |
9fb9cbb1 YK |
38 | "NONE", |
39 | "SYN_SENT", | |
40 | "SYN_RECV", | |
41 | "ESTABLISHED", | |
42 | "FIN_WAIT", | |
43 | "CLOSE_WAIT", | |
44 | "LAST_ACK", | |
45 | "TIME_WAIT", | |
46 | "CLOSE", | |
874ab923 | 47 | "SYN_SENT2", |
9fb9cbb1 | 48 | }; |
601e68e1 | 49 | |
d9a6f0d0 | 50 | enum nf_ct_tcp_action { |
6e250dcb | 51 | NFCT_TCP_IGNORE, |
d9a6f0d0 FW |
52 | NFCT_TCP_INVALID, |
53 | NFCT_TCP_ACCEPT, | |
54 | }; | |
55 | ||
9fb9cbb1 YK |
56 | #define SECS * HZ |
57 | #define MINS * 60 SECS | |
58 | #define HOURS * 60 MINS | |
59 | #define DAYS * 24 HOURS | |
60 | ||
2c9e8637 | 61 | static const unsigned int tcp_timeouts[TCP_CONNTRACK_TIMEOUT_MAX] = { |
2d646286 PM |
62 | [TCP_CONNTRACK_SYN_SENT] = 2 MINS, |
63 | [TCP_CONNTRACK_SYN_RECV] = 60 SECS, | |
64 | [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS, | |
65 | [TCP_CONNTRACK_FIN_WAIT] = 2 MINS, | |
66 | [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS, | |
67 | [TCP_CONNTRACK_LAST_ACK] = 30 SECS, | |
68 | [TCP_CONNTRACK_TIME_WAIT] = 2 MINS, | |
69 | [TCP_CONNTRACK_CLOSE] = 10 SECS, | |
874ab923 | 70 | [TCP_CONNTRACK_SYN_SENT2] = 2 MINS, |
33ee4464 PNA |
71 | /* RFC1122 says the R2 limit should be at least 100 seconds. |
72 | Linux uses 15 packets as limit, which corresponds | |
73 | to ~13-30min depending on RTO. */ | |
74 | [TCP_CONNTRACK_RETRANS] = 5 MINS, | |
75 | [TCP_CONNTRACK_UNACK] = 5 MINS, | |
2d646286 | 76 | }; |
601e68e1 | 77 | |
9fb9cbb1 YK |
78 | #define sNO TCP_CONNTRACK_NONE |
79 | #define sSS TCP_CONNTRACK_SYN_SENT | |
80 | #define sSR TCP_CONNTRACK_SYN_RECV | |
81 | #define sES TCP_CONNTRACK_ESTABLISHED | |
82 | #define sFW TCP_CONNTRACK_FIN_WAIT | |
83 | #define sCW TCP_CONNTRACK_CLOSE_WAIT | |
84 | #define sLA TCP_CONNTRACK_LAST_ACK | |
85 | #define sTW TCP_CONNTRACK_TIME_WAIT | |
86 | #define sCL TCP_CONNTRACK_CLOSE | |
874ab923 | 87 | #define sS2 TCP_CONNTRACK_SYN_SENT2 |
9fb9cbb1 YK |
88 | #define sIV TCP_CONNTRACK_MAX |
89 | #define sIG TCP_CONNTRACK_IGNORE | |
90 | ||
91 | /* What TCP flags are set from RST/SYN/FIN/ACK. */ | |
92 | enum tcp_bit_set { | |
93 | TCP_SYN_SET, | |
94 | TCP_SYNACK_SET, | |
95 | TCP_FIN_SET, | |
96 | TCP_ACK_SET, | |
97 | TCP_RST_SET, | |
98 | TCP_NONE_SET, | |
99 | }; | |
601e68e1 | 100 | |
9fb9cbb1 YK |
101 | /* |
102 | * The TCP state transition table needs a few words... | |
103 | * | |
104 | * We are the man in the middle. All the packets go through us | |
105 | * but might get lost in transit to the destination. | |
601e68e1 | 106 | * It is assumed that the destinations can't receive segments |
9fb9cbb1 YK |
107 | * we haven't seen. |
108 | * | |
109 | * The checked segment is in window, but our windows are *not* | |
110 | * equivalent with the ones of the sender/receiver. We always | |
111 | * try to guess the state of the current sender. | |
112 | * | |
113 | * The meaning of the states are: | |
114 | * | |
115 | * NONE: initial state | |
601e68e1 | 116 | * SYN_SENT: SYN-only packet seen |
874ab923 | 117 | * SYN_SENT2: SYN-only packet seen from reply dir, simultaneous open |
9fb9cbb1 YK |
118 | * SYN_RECV: SYN-ACK packet seen |
119 | * ESTABLISHED: ACK packet seen | |
120 | * FIN_WAIT: FIN packet seen | |
601e68e1 | 121 | * CLOSE_WAIT: ACK seen (after FIN) |
9fb9cbb1 YK |
122 | * LAST_ACK: FIN seen (after FIN) |
123 | * TIME_WAIT: last ACK seen | |
b2155e7f | 124 | * CLOSE: closed connection (RST) |
9fb9cbb1 | 125 | * |
9fb9cbb1 | 126 | * Packets marked as IGNORED (sIG): |
601e68e1 YH |
127 | * if they may be either invalid or valid |
128 | * and the receiver may send back a connection | |
9fb9cbb1 YK |
129 | * closing RST or a SYN/ACK. |
130 | * | |
131 | * Packets marked as INVALID (sIV): | |
874ab923 | 132 | * if we regard them as truly invalid packets |
9fb9cbb1 | 133 | */ |
a5e73c29 | 134 | static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = { |
9fb9cbb1 YK |
135 | { |
136 | /* ORIGINAL */ | |
874ab923 JK |
137 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
138 | /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sS2 }, | |
9fb9cbb1 YK |
139 | /* |
140 | * sNO -> sSS Initialize a new connection | |
141 | * sSS -> sSS Retransmitted SYN | |
874ab923 JK |
142 | * sS2 -> sS2 Late retransmitted SYN |
143 | * sSR -> sIG | |
9fb9cbb1 | 144 | * sES -> sIG Error: SYNs in window outside the SYN_SENT state |
601e68e1 | 145 | * are errors. Receiver will reply with RST |
9fb9cbb1 YK |
146 | * and close the connection. |
147 | * Or we are not in sync and hold a dead connection. | |
148 | * sFW -> sIG | |
149 | * sCW -> sIG | |
150 | * sLA -> sIG | |
151 | * sTW -> sSS Reopened connection (RFC 1122). | |
152 | * sCL -> sSS | |
153 | */ | |
874ab923 | 154 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
64f509ce | 155 | /*synack*/ { sIV, sIV, sSR, sIV, sIV, sIV, sIV, sIV, sIV, sSR }, |
9fb9cbb1 | 156 | /* |
874ab923 JK |
157 | * sNO -> sIV Too late and no reason to do anything |
158 | * sSS -> sIV Client can't send SYN and then SYN/ACK | |
159 | * sS2 -> sSR SYN/ACK sent to SYN2 in simultaneous open | |
64f509ce JK |
160 | * sSR -> sSR Late retransmitted SYN/ACK in simultaneous open |
161 | * sES -> sIV Invalid SYN/ACK packets sent by the client | |
162 | * sFW -> sIV | |
163 | * sCW -> sIV | |
164 | * sLA -> sIV | |
165 | * sTW -> sIV | |
166 | * sCL -> sIV | |
9fb9cbb1 | 167 | */ |
874ab923 | 168 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
9fb9cbb1 YK |
169 | /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV }, |
170 | /* | |
171 | * sNO -> sIV Too late and no reason to do anything... | |
172 | * sSS -> sIV Client migth not send FIN in this state: | |
173 | * we enforce waiting for a SYN/ACK reply first. | |
874ab923 | 174 | * sS2 -> sIV |
9fb9cbb1 YK |
175 | * sSR -> sFW Close started. |
176 | * sES -> sFW | |
177 | * sFW -> sLA FIN seen in both directions, waiting for | |
601e68e1 | 178 | * the last ACK. |
9fb9cbb1 YK |
179 | * Migth be a retransmitted FIN as well... |
180 | * sCW -> sLA | |
181 | * sLA -> sLA Retransmitted FIN. Remain in the same state. | |
182 | * sTW -> sTW | |
183 | * sCL -> sCL | |
184 | */ | |
874ab923 | 185 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
9fb9cbb1 YK |
186 | /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV }, |
187 | /* | |
188 | * sNO -> sES Assumed. | |
189 | * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet. | |
874ab923 | 190 | * sS2 -> sIV |
9fb9cbb1 YK |
191 | * sSR -> sES Established state is reached. |
192 | * sES -> sES :-) | |
193 | * sFW -> sCW Normal close request answered by ACK. | |
194 | * sCW -> sCW | |
b3cad287 | 195 | * sLA -> sTW Last ACK detected (RFC5961 challenged) |
9fb9cbb1 YK |
196 | * sTW -> sTW Retransmitted last ACK. Remain in the same state. |
197 | * sCL -> sCL | |
198 | */ | |
874ab923 JK |
199 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
200 | /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL }, | |
9fb9cbb1 YK |
201 | /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV } |
202 | }, | |
203 | { | |
204 | /* REPLY */ | |
874ab923 | 205 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
e37ad9fd | 206 | /*syn*/ { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sSS, sIV, sS2 }, |
9fb9cbb1 YK |
207 | /* |
208 | * sNO -> sIV Never reached. | |
874ab923 JK |
209 | * sSS -> sS2 Simultaneous open |
210 | * sS2 -> sS2 Retransmitted simultaneous SYN | |
211 | * sSR -> sIV Invalid SYN packets sent by the server | |
212 | * sES -> sIV | |
9fb9cbb1 YK |
213 | * sFW -> sIV |
214 | * sCW -> sIV | |
215 | * sLA -> sIV | |
e37ad9fd | 216 | * sTW -> sSS Reopened connection, but server may have switched role |
9fb9cbb1 YK |
217 | * sCL -> sIV |
218 | */ | |
874ab923 | 219 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
8a80c79a | 220 | /*synack*/ { sIV, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR }, |
9fb9cbb1 YK |
221 | /* |
222 | * sSS -> sSR Standard open. | |
874ab923 | 223 | * sS2 -> sSR Simultaneous open |
8a80c79a | 224 | * sSR -> sIG Retransmitted SYN/ACK, ignore it. |
9fb9cbb1 YK |
225 | * sES -> sIG Late retransmitted SYN/ACK? |
226 | * sFW -> sIG Might be SYN/ACK answering ignored SYN | |
227 | * sCW -> sIG | |
228 | * sLA -> sIG | |
229 | * sTW -> sIG | |
230 | * sCL -> sIG | |
231 | */ | |
874ab923 | 232 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
9fb9cbb1 YK |
233 | /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV }, |
234 | /* | |
235 | * sSS -> sIV Server might not send FIN in this state. | |
874ab923 | 236 | * sS2 -> sIV |
9fb9cbb1 YK |
237 | * sSR -> sFW Close started. |
238 | * sES -> sFW | |
239 | * sFW -> sLA FIN seen in both directions. | |
240 | * sCW -> sLA | |
241 | * sLA -> sLA Retransmitted FIN. | |
242 | * sTW -> sTW | |
243 | * sCL -> sCL | |
244 | */ | |
874ab923 JK |
245 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
246 | /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIG }, | |
9fb9cbb1 | 247 | /* |
73f30602 | 248 | * sSS -> sIG Might be a half-open connection. |
874ab923 | 249 | * sS2 -> sIG |
9fb9cbb1 YK |
250 | * sSR -> sSR Might answer late resent SYN. |
251 | * sES -> sES :-) | |
252 | * sFW -> sCW Normal close request answered by ACK. | |
253 | * sCW -> sCW | |
b3cad287 | 254 | * sLA -> sTW Last ACK detected (RFC5961 challenged) |
9fb9cbb1 YK |
255 | * sTW -> sTW Retransmitted last ACK. |
256 | * sCL -> sCL | |
257 | */ | |
874ab923 JK |
258 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */ |
259 | /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL }, | |
9fb9cbb1 | 260 | /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV } |
601e68e1 | 261 | } |
9fb9cbb1 YK |
262 | }; |
263 | ||
ea48cc83 | 264 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
9fb9cbb1 | 265 | /* Print out the private part of the conntrack. */ |
37246a58 | 266 | static void tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
9fb9cbb1 | 267 | { |
90964016 PNA |
268 | if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) |
269 | return; | |
270 | ||
a163f2cb | 271 | seq_printf(s, "%s ", tcp_conntrack_names[ct->proto.tcp.state]); |
9fb9cbb1 | 272 | } |
ea48cc83 | 273 | #endif |
9fb9cbb1 YK |
274 | |
275 | static unsigned int get_conntrack_index(const struct tcphdr *tcph) | |
276 | { | |
277 | if (tcph->rst) return TCP_RST_SET; | |
278 | else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET); | |
279 | else if (tcph->fin) return TCP_FIN_SET; | |
280 | else if (tcph->ack) return TCP_ACK_SET; | |
281 | else return TCP_NONE_SET; | |
282 | } | |
283 | ||
284 | /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering | |
285 | in IP Filter' by Guido van Rooij. | |
601e68e1 | 286 | |
631dd1a8 JM |
287 | http://www.sane.nl/events/sane2000/papers.html |
288 | http://www.darkart.com/mirrors/www.obfuscation.org/ipf/ | |
601e68e1 | 289 | |
9fb9cbb1 YK |
290 | The boundaries and the conditions are changed according to RFC793: |
291 | the packet must intersect the window (i.e. segments may be | |
292 | after the right or before the left edge) and thus receivers may ACK | |
293 | segments after the right edge of the window. | |
294 | ||
601e68e1 | 295 | td_maxend = max(sack + max(win,1)) seen in reply packets |
9fb9cbb1 YK |
296 | td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets |
297 | td_maxwin += seq + len - sender.td_maxend | |
298 | if seq + len > sender.td_maxend | |
299 | td_end = max(seq + len) seen in sent packets | |
601e68e1 | 300 | |
9fb9cbb1 YK |
301 | I. Upper bound for valid data: seq <= sender.td_maxend |
302 | II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin | |
84ebe1cd JK |
303 | III. Upper bound for valid (s)ack: sack <= receiver.td_end |
304 | IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW | |
9fb9cbb1 | 305 | |
84ebe1cd JK |
306 | where sack is the highest right edge of sack block found in the packet |
307 | or ack in the case of packet without SACK option. | |
9fb9cbb1 | 308 | |
84ebe1cd | 309 | The upper bound limit for a valid (s)ack is not ignored - |
601e68e1 | 310 | we doesn't have to deal with fragments. |
9fb9cbb1 YK |
311 | */ |
312 | ||
313 | static inline __u32 segment_seq_plus_len(__u32 seq, | |
314 | size_t len, | |
315 | unsigned int dataoff, | |
82f568fc | 316 | const struct tcphdr *tcph) |
9fb9cbb1 YK |
317 | { |
318 | /* XXX Should I use payload length field in IP/IPv6 header ? | |
319 | * - YK */ | |
320 | return (seq + len - dataoff - tcph->doff*4 | |
321 | + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0)); | |
322 | } | |
601e68e1 | 323 | |
9fb9cbb1 YK |
324 | /* Fixme: what about big packets? */ |
325 | #define MAXACKWINCONST 66000 | |
326 | #define MAXACKWINDOW(sender) \ | |
327 | ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \ | |
328 | : MAXACKWINCONST) | |
601e68e1 | 329 | |
9fb9cbb1 YK |
330 | /* |
331 | * Simplified tcp_parse_options routine from tcp_input.c | |
332 | */ | |
333 | static void tcp_options(const struct sk_buff *skb, | |
334 | unsigned int dataoff, | |
82f568fc | 335 | const struct tcphdr *tcph, |
9fb9cbb1 YK |
336 | struct ip_ct_tcp_state *state) |
337 | { | |
338 | unsigned char buff[(15 * 4) - sizeof(struct tcphdr)]; | |
82f568fc | 339 | const unsigned char *ptr; |
9fb9cbb1 YK |
340 | int length = (tcph->doff*4) - sizeof(struct tcphdr); |
341 | ||
342 | if (!length) | |
343 | return; | |
344 | ||
345 | ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr), | |
346 | length, buff); | |
198ad973 PNA |
347 | if (!ptr) |
348 | return; | |
9fb9cbb1 | 349 | |
f2dd495a PNA |
350 | state->td_scale = 0; |
351 | state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL; | |
9fb9cbb1 YK |
352 | |
353 | while (length > 0) { | |
354 | int opcode=*ptr++; | |
355 | int opsize; | |
356 | ||
357 | switch (opcode) { | |
358 | case TCPOPT_EOL: | |
359 | return; | |
360 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ | |
361 | length--; | |
362 | continue; | |
363 | default: | |
644c7e48 JK |
364 | if (length < 2) |
365 | return; | |
9fb9cbb1 YK |
366 | opsize=*ptr++; |
367 | if (opsize < 2) /* "silly options" */ | |
368 | return; | |
369 | if (opsize > length) | |
4a5cc84a | 370 | return; /* don't parse partial options */ |
9fb9cbb1 | 371 | |
601e68e1 | 372 | if (opcode == TCPOPT_SACK_PERM |
9fb9cbb1 YK |
373 | && opsize == TCPOLEN_SACK_PERM) |
374 | state->flags |= IP_CT_TCP_FLAG_SACK_PERM; | |
375 | else if (opcode == TCPOPT_WINDOW | |
376 | && opsize == TCPOLEN_WINDOW) { | |
377 | state->td_scale = *(u_int8_t *)ptr; | |
378 | ||
122868b3 GF |
379 | if (state->td_scale > TCP_MAX_WSCALE) |
380 | state->td_scale = TCP_MAX_WSCALE; | |
381 | ||
9fb9cbb1 YK |
382 | state->flags |= |
383 | IP_CT_TCP_FLAG_WINDOW_SCALE; | |
384 | } | |
385 | ptr += opsize - 2; | |
386 | length -= opsize; | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, | |
82f568fc | 392 | const struct tcphdr *tcph, __u32 *sack) |
9fb9cbb1 | 393 | { |
601e68e1 | 394 | unsigned char buff[(15 * 4) - sizeof(struct tcphdr)]; |
82f568fc | 395 | const unsigned char *ptr; |
9fb9cbb1 YK |
396 | int length = (tcph->doff*4) - sizeof(struct tcphdr); |
397 | __u32 tmp; | |
398 | ||
399 | if (!length) | |
400 | return; | |
401 | ||
402 | ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr), | |
403 | length, buff); | |
198ad973 PNA |
404 | if (!ptr) |
405 | return; | |
9fb9cbb1 YK |
406 | |
407 | /* Fast path for timestamp-only option */ | |
bb9fc373 | 408 | if (length == TCPOLEN_TSTAMP_ALIGNED |
8f05ce91 YH |
409 | && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24) |
410 | | (TCPOPT_NOP << 16) | |
411 | | (TCPOPT_TIMESTAMP << 8) | |
412 | | TCPOLEN_TIMESTAMP)) | |
9fb9cbb1 YK |
413 | return; |
414 | ||
415 | while (length > 0) { | |
416 | int opcode = *ptr++; | |
417 | int opsize, i; | |
418 | ||
419 | switch (opcode) { | |
420 | case TCPOPT_EOL: | |
421 | return; | |
422 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ | |
423 | length--; | |
424 | continue; | |
425 | default: | |
644c7e48 JK |
426 | if (length < 2) |
427 | return; | |
9fb9cbb1 YK |
428 | opsize = *ptr++; |
429 | if (opsize < 2) /* "silly options" */ | |
430 | return; | |
431 | if (opsize > length) | |
4a5cc84a | 432 | return; /* don't parse partial options */ |
9fb9cbb1 | 433 | |
601e68e1 YH |
434 | if (opcode == TCPOPT_SACK |
435 | && opsize >= (TCPOLEN_SACK_BASE | |
436 | + TCPOLEN_SACK_PERBLOCK) | |
437 | && !((opsize - TCPOLEN_SACK_BASE) | |
438 | % TCPOLEN_SACK_PERBLOCK)) { | |
439 | for (i = 0; | |
440 | i < (opsize - TCPOLEN_SACK_BASE); | |
441 | i += TCPOLEN_SACK_PERBLOCK) { | |
534f81a5 | 442 | tmp = get_unaligned_be32((__be32 *)(ptr+i)+1); |
9fb9cbb1 YK |
443 | |
444 | if (after(tmp, *sack)) | |
445 | *sack = tmp; | |
446 | } | |
447 | return; | |
448 | } | |
449 | ptr += opsize - 2; | |
450 | length -= opsize; | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
cc4f9d62 FW |
455 | static void tcp_init_sender(struct ip_ct_tcp_state *sender, |
456 | struct ip_ct_tcp_state *receiver, | |
457 | const struct sk_buff *skb, | |
458 | unsigned int dataoff, | |
459 | const struct tcphdr *tcph, | |
460 | u32 end, u32 win) | |
461 | { | |
462 | /* SYN-ACK in reply to a SYN | |
463 | * or SYN from reply direction in simultaneous open. | |
464 | */ | |
465 | sender->td_end = | |
466 | sender->td_maxend = end; | |
467 | sender->td_maxwin = (win == 0 ? 1 : win); | |
468 | ||
469 | tcp_options(skb, dataoff, tcph, sender); | |
470 | /* RFC 1323: | |
471 | * Both sides must send the Window Scale option | |
472 | * to enable window scaling in either direction. | |
473 | */ | |
474 | if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE && | |
475 | receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE)) { | |
476 | sender->td_scale = 0; | |
477 | receiver->td_scale = 0; | |
478 | } | |
479 | } | |
480 | ||
d9a6f0d0 FW |
481 | __printf(6, 7) |
482 | static enum nf_ct_tcp_action nf_tcp_log_invalid(const struct sk_buff *skb, | |
483 | const struct nf_conn *ct, | |
484 | const struct nf_hook_state *state, | |
485 | const struct ip_ct_tcp_state *sender, | |
486 | enum nf_ct_tcp_action ret, | |
487 | const char *fmt, ...) | |
488 | { | |
489 | const struct nf_tcp_net *tn = nf_tcp_pernet(nf_ct_net(ct)); | |
490 | struct va_format vaf; | |
491 | va_list args; | |
492 | bool be_liberal; | |
493 | ||
494 | be_liberal = sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL || tn->tcp_be_liberal; | |
495 | if (be_liberal) | |
496 | return NFCT_TCP_ACCEPT; | |
497 | ||
498 | va_start(args, fmt); | |
499 | vaf.fmt = fmt; | |
500 | vaf.va = &args; | |
501 | nf_ct_l4proto_log_invalid(skb, ct, state, "%pV", &vaf); | |
502 | va_end(args); | |
503 | ||
504 | return ret; | |
505 | } | |
506 | ||
507 | static enum nf_ct_tcp_action | |
508 | tcp_in_window(struct nf_conn *ct, enum ip_conntrack_dir dir, | |
509 | unsigned int index, const struct sk_buff *skb, | |
510 | unsigned int dataoff, const struct tcphdr *tcph, | |
511 | const struct nf_hook_state *hook_state) | |
9fb9cbb1 | 512 | { |
62eec0d7 | 513 | struct ip_ct_tcp *state = &ct->proto.tcp; |
9fb9cbb1 YK |
514 | struct ip_ct_tcp_state *sender = &state->seen[dir]; |
515 | struct ip_ct_tcp_state *receiver = &state->seen[!dir]; | |
516 | __u32 seq, ack, sack, end, win, swin; | |
d9a6f0d0 | 517 | bool in_recv_win, seq_ok; |
2d89c68a | 518 | s32 receiver_offset; |
d9a6f0d0 | 519 | u16 win_raw; |
9fb9cbb1 YK |
520 | |
521 | /* | |
522 | * Get the required data from the packet. | |
523 | */ | |
524 | seq = ntohl(tcph->seq); | |
525 | ack = sack = ntohl(tcph->ack_seq); | |
959b69ef FW |
526 | win_raw = ntohs(tcph->window); |
527 | win = win_raw; | |
9fb9cbb1 YK |
528 | end = segment_seq_plus_len(seq, skb->len, dataoff, tcph); |
529 | ||
530 | if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM) | |
531 | tcp_sack(skb, dataoff, tcph, &sack); | |
532 | ||
f9dd09c7 | 533 | /* Take into account NAT sequence number mangling */ |
41d73ec0 | 534 | receiver_offset = nf_ct_seq_offset(ct, !dir, ack - 1); |
f9dd09c7 JK |
535 | ack -= receiver_offset; |
536 | sack -= receiver_offset; | |
537 | ||
874ab923 | 538 | if (sender->td_maxwin == 0) { |
9fb9cbb1 YK |
539 | /* |
540 | * Initialize sender data. | |
541 | */ | |
874ab923 | 542 | if (tcph->syn) { |
cc4f9d62 FW |
543 | tcp_init_sender(sender, receiver, |
544 | skb, dataoff, tcph, | |
545 | end, win); | |
874ab923 JK |
546 | if (!tcph->ack) |
547 | /* Simultaneous open */ | |
d9a6f0d0 | 548 | return NFCT_TCP_ACCEPT; |
9fb9cbb1 YK |
549 | } else { |
550 | /* | |
551 | * We are in the middle of a connection, | |
552 | * its history is lost for us. | |
553 | * Let's try to use the data from the packet. | |
601e68e1 | 554 | */ |
9fb9cbb1 | 555 | sender->td_end = end; |
6ee0b693 CG |
556 | swin = win << sender->td_scale; |
557 | sender->td_maxwin = (swin == 0 ? 1 : swin); | |
9fb9cbb1 | 558 | sender->td_maxend = end + sender->td_maxwin; |
4f25434b FR |
559 | if (receiver->td_maxwin == 0) { |
560 | /* We haven't seen traffic in the other | |
561 | * direction yet but we have to tweak window | |
562 | * tracking to pass III and IV until that | |
563 | * happens. | |
564 | */ | |
fac42a9a | 565 | receiver->td_end = receiver->td_maxend = sack; |
4f25434b FR |
566 | } else if (sack == receiver->td_end + 1) { |
567 | /* Likely a reply to a keepalive. | |
568 | * Needed for III. | |
569 | */ | |
570 | receiver->td_end++; | |
571 | } | |
572 | ||
9fb9cbb1 | 573 | } |
c7aab4f1 FW |
574 | } else if (tcph->syn && |
575 | after(end, sender->td_end) && | |
576 | (state->state == TCP_CONNTRACK_SYN_SENT || | |
577 | state->state == TCP_CONNTRACK_SYN_RECV)) { | |
9fb9cbb1 YK |
578 | /* |
579 | * RFC 793: "if a TCP is reinitialized ... then it need | |
601e68e1 | 580 | * not wait at all; it must only be sure to use sequence |
9fb9cbb1 | 581 | * numbers larger than those recently used." |
82b72cb9 FW |
582 | * |
583 | * Re-init state for this direction, just like for the first | |
584 | * syn(-ack) reply, it might differ in seq, ack or tcp options. | |
585 | */ | |
586 | tcp_init_sender(sender, receiver, | |
587 | skb, dataoff, tcph, | |
588 | end, win); | |
c7aab4f1 FW |
589 | |
590 | if (dir == IP_CT_DIR_REPLY && !tcph->ack) | |
d9a6f0d0 | 591 | return NFCT_TCP_ACCEPT; |
9fb9cbb1 YK |
592 | } |
593 | ||
594 | if (!(tcph->ack)) { | |
595 | /* | |
596 | * If there is no ACK, just pretend it was set and OK. | |
597 | */ | |
598 | ack = sack = receiver->td_end; | |
601e68e1 YH |
599 | } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) == |
600 | (TCP_FLAG_ACK|TCP_FLAG_RST)) | |
9fb9cbb1 YK |
601 | && (ack == 0)) { |
602 | /* | |
603 | * Broken TCP stacks, that set ACK in RST packets as well | |
604 | * with zero ack value. | |
605 | */ | |
606 | ack = sack = receiver->td_end; | |
607 | } | |
608 | ||
4a70bbfa | 609 | if (tcph->rst && seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT) |
9fb9cbb1 | 610 | /* |
4a70bbfa | 611 | * RST sent answering SYN. |
9fb9cbb1 YK |
612 | */ |
613 | seq = end = sender->td_end; | |
614 | ||
d9a6f0d0 FW |
615 | seq_ok = before(seq, sender->td_maxend + 1); |
616 | if (!seq_ok) { | |
617 | u32 overshot = end - sender->td_maxend + 1; | |
618 | bool ack_ok; | |
619 | ||
620 | ack_ok = after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1); | |
621 | in_recv_win = receiver->td_maxwin && | |
622 | after(end, sender->td_end - receiver->td_maxwin - 1); | |
623 | ||
624 | if (in_recv_win && | |
625 | ack_ok && | |
626 | overshot <= receiver->td_maxwin && | |
627 | before(sack, receiver->td_end + 1)) { | |
628 | /* Work around TCPs that send more bytes than allowed by | |
629 | * the receive window. | |
630 | * | |
631 | * If the (marked as invalid) packet is allowed to pass by | |
632 | * the ruleset and the peer acks this data, then its possible | |
633 | * all future packets will trigger 'ACK is over upper bound' check. | |
634 | * | |
635 | * Thus if only the sequence check fails then do update td_end so | |
636 | * possible ACK for this data can update internal state. | |
637 | */ | |
638 | sender->td_end = end; | |
639 | sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED; | |
640 | ||
6e250dcb | 641 | return nf_tcp_log_invalid(skb, ct, hook_state, sender, NFCT_TCP_IGNORE, |
d9a6f0d0 | 642 | "%u bytes more than expected", overshot); |
d9a6f0d0 FW |
643 | } |
644 | ||
645 | return nf_tcp_log_invalid(skb, ct, hook_state, sender, NFCT_TCP_INVALID, | |
646 | "SEQ is over upper bound %u (over the window of the receiver)", | |
647 | sender->td_maxend + 1); | |
648 | } | |
649 | ||
650 | if (!before(sack, receiver->td_end + 1)) | |
651 | return nf_tcp_log_invalid(skb, ct, hook_state, sender, NFCT_TCP_INVALID, | |
652 | "ACK is over upper bound %u (ACKed data not seen yet)", | |
653 | receiver->td_end + 1); | |
654 | ||
356d7d88 YC |
655 | /* Is the ending sequence in the receive window (if available)? */ |
656 | in_recv_win = !receiver->td_maxwin || | |
657 | after(end, sender->td_end - receiver->td_maxwin - 1); | |
6e250dcb FW |
658 | if (!in_recv_win) |
659 | return nf_tcp_log_invalid(skb, ct, hook_state, sender, NFCT_TCP_IGNORE, | |
660 | "SEQ is under lower bound %u (already ACKed data retransmitted)", | |
661 | sender->td_end - receiver->td_maxwin - 1); | |
662 | if (!after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) | |
663 | return nf_tcp_log_invalid(skb, ct, hook_state, sender, NFCT_TCP_IGNORE, | |
664 | "ignored ACK under lower bound %u (possible overly delayed)", | |
665 | receiver->td_end - MAXACKWINDOW(sender) - 1); | |
bfcaa502 | 666 | |
09a59001 FW |
667 | /* Take into account window scaling (RFC 1323). */ |
668 | if (!tcph->syn) | |
669 | win <<= sender->td_scale; | |
670 | ||
671 | /* 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; | |
678 | } | |
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; | |
9fb9cbb1 | 685 | } |
09a59001 | 686 | } |
9fb9cbb1 | 687 | |
09a59001 FW |
688 | /* Update receiver data. */ |
689 | if (receiver->td_maxwin != 0 && after(end, sender->td_maxend)) | |
690 | receiver->td_maxwin += end - sender->td_maxend; | |
691 | if (after(sack + win, receiver->td_maxend - 1)) { | |
692 | receiver->td_maxend = sack + win; | |
693 | if (win == 0) | |
694 | receiver->td_maxend++; | |
695 | } | |
696 | if (ack == receiver->td_end) | |
697 | receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED; | |
698 | ||
699 | /* Check retransmissions. */ | |
700 | if (index == TCP_ACK_SET) { | |
701 | if (state->last_dir == dir && | |
702 | state->last_seq == seq && | |
703 | state->last_ack == ack && | |
704 | state->last_end == end && | |
705 | state->last_win == win_raw) { | |
706 | state->retrans++; | |
707 | } else { | |
708 | state->last_dir = dir; | |
709 | state->last_seq = seq; | |
710 | state->last_ack = ack; | |
711 | state->last_end = end; | |
712 | state->last_win = win_raw; | |
713 | state->retrans = 0; | |
9fb9cbb1 | 714 | } |
601e68e1 YH |
715 | } |
716 | ||
d9a6f0d0 | 717 | return NFCT_TCP_ACCEPT; |
9fb9cbb1 YK |
718 | } |
719 | ||
628d6943 FW |
720 | static void __cold nf_tcp_handle_invalid(struct nf_conn *ct, |
721 | enum ip_conntrack_dir dir, | |
722 | int index, | |
723 | const struct sk_buff *skb, | |
724 | const struct nf_hook_state *hook_state) | |
725 | { | |
726 | const unsigned int *timeouts; | |
727 | const struct nf_tcp_net *tn; | |
728 | unsigned int timeout; | |
729 | u32 expires; | |
730 | ||
731 | if (!test_bit(IPS_ASSURED_BIT, &ct->status) || | |
732 | test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) | |
733 | return; | |
734 | ||
735 | /* We don't want to have connections hanging around in ESTABLISHED | |
736 | * state for long time 'just because' conntrack deemed a FIN/RST | |
737 | * out-of-window. | |
738 | * | |
739 | * Shrink the timeout just like when there is unacked data. | |
740 | * This speeds up eviction of 'dead' connections where the | |
741 | * connection and conntracks internal state are out of sync. | |
742 | */ | |
743 | switch (index) { | |
744 | case TCP_RST_SET: | |
745 | case TCP_FIN_SET: | |
746 | break; | |
747 | default: | |
748 | return; | |
749 | } | |
750 | ||
751 | if (ct->proto.tcp.last_dir != dir && | |
752 | (ct->proto.tcp.last_index == TCP_FIN_SET || | |
753 | ct->proto.tcp.last_index == TCP_RST_SET)) { | |
754 | expires = nf_ct_expires(ct); | |
755 | if (expires < 120 * HZ) | |
756 | return; | |
757 | ||
758 | tn = nf_tcp_pernet(nf_ct_net(ct)); | |
759 | timeouts = nf_ct_timeout_lookup(ct); | |
760 | if (!timeouts) | |
761 | timeouts = tn->timeouts; | |
762 | ||
763 | timeout = READ_ONCE(timeouts[TCP_CONNTRACK_UNACK]); | |
764 | if (expires > timeout) { | |
765 | nf_ct_l4proto_log_invalid(skb, ct, hook_state, | |
766 | "packet (index %d, dir %d) response for index %d lower timeout to %u", | |
767 | index, dir, ct->proto.tcp.last_index, timeout); | |
768 | ||
769 | WRITE_ONCE(ct->timeout, timeout + nfct_time_stamp); | |
770 | } | |
771 | } else { | |
772 | ct->proto.tcp.last_index = index; | |
773 | ct->proto.tcp.last_dir = dir; | |
774 | } | |
775 | } | |
776 | ||
5c8ce7c9 | 777 | /* table of valid flag combinations - PUSH, ECE and CWR are always valid */ |
a3433f35 CG |
778 | static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK| |
779 | TCPHDR_URG) + 1] = | |
9fb9cbb1 | 780 | { |
a3433f35 CG |
781 | [TCPHDR_SYN] = 1, |
782 | [TCPHDR_SYN|TCPHDR_URG] = 1, | |
783 | [TCPHDR_SYN|TCPHDR_ACK] = 1, | |
784 | [TCPHDR_RST] = 1, | |
785 | [TCPHDR_RST|TCPHDR_ACK] = 1, | |
786 | [TCPHDR_FIN|TCPHDR_ACK] = 1, | |
787 | [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG] = 1, | |
788 | [TCPHDR_ACK] = 1, | |
789 | [TCPHDR_ACK|TCPHDR_URG] = 1, | |
9fb9cbb1 YK |
790 | }; |
791 | ||
93e66024 FW |
792 | static void tcp_error_log(const struct sk_buff *skb, |
793 | const struct nf_hook_state *state, | |
794 | const char *msg) | |
c4f3db15 | 795 | { |
62eec0d7 | 796 | nf_l4proto_log_invalid(skb, state, IPPROTO_TCP, "%s", msg); |
c4f3db15 FW |
797 | } |
798 | ||
9fb9cbb1 | 799 | /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */ |
0150ffba FW |
800 | static bool tcp_error(const struct tcphdr *th, |
801 | struct sk_buff *skb, | |
802 | unsigned int dataoff, | |
803 | const struct nf_hook_state *state) | |
9fb9cbb1 | 804 | { |
9fb9cbb1 | 805 | unsigned int tcplen = skb->len - dataoff; |
0150ffba | 806 | u8 tcpflags; |
601e68e1 | 807 | |
9fb9cbb1 YK |
808 | /* Not whole TCP header or malformed packet */ |
809 | if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) { | |
93e66024 | 810 | tcp_error_log(skb, state, "truncated packet"); |
0150ffba | 811 | return true; |
9fb9cbb1 | 812 | } |
601e68e1 | 813 | |
9fb9cbb1 YK |
814 | /* Checksum invalid? Ignore. |
815 | * We skip checking packets on the outgoing path | |
84fa7933 | 816 | * because the checksum is assumed to be correct. |
9fb9cbb1 YK |
817 | */ |
818 | /* FIXME: Source route IP option packets --RR */ | |
93e66024 FW |
819 | if (state->net->ct.sysctl_checksum && |
820 | state->hook == NF_INET_PRE_ROUTING && | |
821 | nf_checksum(skb, state->hook, dataoff, IPPROTO_TCP, state->pf)) { | |
822 | tcp_error_log(skb, state, "bad checksum"); | |
0150ffba | 823 | return true; |
9fb9cbb1 YK |
824 | } |
825 | ||
826 | /* Check TCP flags. */ | |
a3433f35 | 827 | tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH)); |
9fb9cbb1 | 828 | if (!tcp_valid_flags[tcpflags]) { |
93e66024 | 829 | tcp_error_log(skb, state, "invalid tcp flag combination"); |
0150ffba | 830 | return true; |
9fb9cbb1 YK |
831 | } |
832 | ||
0150ffba | 833 | return false; |
9fb9cbb1 YK |
834 | } |
835 | ||
9976fc6e FW |
836 | static noinline bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb, |
837 | unsigned int dataoff, | |
838 | const struct tcphdr *th) | |
839 | { | |
840 | enum tcp_conntrack new_state; | |
841 | struct net *net = nf_ct_net(ct); | |
a95a7774 | 842 | const struct nf_tcp_net *tn = nf_tcp_pernet(net); |
9976fc6e FW |
843 | |
844 | /* Don't need lock here: this conntrack not in circulation yet */ | |
845 | new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE]; | |
846 | ||
847 | /* Invalid: delete conntrack */ | |
848 | if (new_state >= TCP_CONNTRACK_MAX) { | |
849 | pr_debug("nf_ct_tcp: invalid new deleting.\n"); | |
850 | return false; | |
851 | } | |
852 | ||
853 | if (new_state == TCP_CONNTRACK_SYN_SENT) { | |
854 | memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp)); | |
855 | /* SYN packet */ | |
856 | ct->proto.tcp.seen[0].td_end = | |
857 | segment_seq_plus_len(ntohl(th->seq), skb->len, | |
858 | dataoff, th); | |
859 | ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window); | |
860 | if (ct->proto.tcp.seen[0].td_maxwin == 0) | |
861 | ct->proto.tcp.seen[0].td_maxwin = 1; | |
862 | ct->proto.tcp.seen[0].td_maxend = | |
863 | ct->proto.tcp.seen[0].td_end; | |
864 | ||
865 | tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]); | |
866 | } else if (tn->tcp_loose == 0) { | |
867 | /* Don't try to pick up connections. */ | |
868 | return false; | |
869 | } else { | |
870 | memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp)); | |
871 | /* | |
872 | * We are in the middle of a connection, | |
873 | * its history is lost for us. | |
874 | * Let's try to use the data from the packet. | |
875 | */ | |
876 | ct->proto.tcp.seen[0].td_end = | |
877 | segment_seq_plus_len(ntohl(th->seq), skb->len, | |
878 | dataoff, th); | |
879 | ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window); | |
880 | if (ct->proto.tcp.seen[0].td_maxwin == 0) | |
881 | ct->proto.tcp.seen[0].td_maxwin = 1; | |
882 | ct->proto.tcp.seen[0].td_maxend = | |
883 | ct->proto.tcp.seen[0].td_end + | |
884 | ct->proto.tcp.seen[0].td_maxwin; | |
885 | ||
886 | /* We assume SACK and liberal window checking to handle | |
887 | * window scaling */ | |
888 | ct->proto.tcp.seen[0].flags = | |
889 | ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM | | |
890 | IP_CT_TCP_FLAG_BE_LIBERAL; | |
891 | } | |
892 | ||
893 | /* tcp_packet will set them */ | |
894 | ct->proto.tcp.last_index = TCP_NONE_SET; | |
9976fc6e | 895 | return true; |
9fb9cbb1 YK |
896 | } |
897 | ||
c4edc3cc AA |
898 | static bool tcp_can_early_drop(const struct nf_conn *ct) |
899 | { | |
900 | switch (ct->proto.tcp.state) { | |
901 | case TCP_CONNTRACK_FIN_WAIT: | |
902 | case TCP_CONNTRACK_LAST_ACK: | |
903 | case TCP_CONNTRACK_TIME_WAIT: | |
904 | case TCP_CONNTRACK_CLOSE: | |
905 | case TCP_CONNTRACK_CLOSE_WAIT: | |
906 | return true; | |
907 | default: | |
908 | break; | |
909 | } | |
910 | ||
911 | return false; | |
912 | } | |
913 | ||
2954fe60 FW |
914 | void nf_conntrack_tcp_set_closing(struct nf_conn *ct) |
915 | { | |
916 | enum tcp_conntrack old_state; | |
917 | const unsigned int *timeouts; | |
918 | u32 timeout; | |
919 | ||
920 | if (!nf_ct_is_confirmed(ct)) | |
921 | return; | |
922 | ||
923 | spin_lock_bh(&ct->lock); | |
924 | old_state = ct->proto.tcp.state; | |
925 | ct->proto.tcp.state = TCP_CONNTRACK_CLOSE; | |
926 | ||
927 | if (old_state == TCP_CONNTRACK_CLOSE || | |
928 | test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) { | |
929 | spin_unlock_bh(&ct->lock); | |
930 | return; | |
931 | } | |
932 | ||
933 | timeouts = nf_ct_timeout_lookup(ct); | |
934 | if (!timeouts) { | |
935 | const struct nf_tcp_net *tn; | |
936 | ||
937 | tn = nf_tcp_pernet(nf_ct_net(ct)); | |
938 | timeouts = tn->timeouts; | |
939 | } | |
940 | ||
941 | timeout = timeouts[TCP_CONNTRACK_CLOSE]; | |
942 | WRITE_ONCE(ct->timeout, timeout + nfct_time_stamp); | |
943 | ||
944 | spin_unlock_bh(&ct->lock); | |
945 | ||
946 | nf_conntrack_event_cache(IPCT_PROTOINFO, ct); | |
947 | } | |
948 | ||
f2dd495a PNA |
949 | static void nf_ct_tcp_state_reset(struct ip_ct_tcp_state *state) |
950 | { | |
951 | state->td_end = 0; | |
952 | state->td_maxend = 0; | |
953 | state->td_maxwin = 0; | |
954 | state->td_maxack = 0; | |
955 | state->td_scale = 0; | |
956 | state->flags &= IP_CT_TCP_FLAG_BE_LIBERAL; | |
957 | } | |
958 | ||
9fb9cbb1 | 959 | /* Returns verdict for packet, or -1 for invalid. */ |
a47c5404 FW |
960 | int nf_conntrack_tcp_packet(struct nf_conn *ct, |
961 | struct sk_buff *skb, | |
962 | unsigned int dataoff, | |
963 | enum ip_conntrack_info ctinfo, | |
964 | const struct nf_hook_state *state) | |
9fb9cbb1 | 965 | { |
c2a2c7e0 | 966 | struct net *net = nf_ct_net(ct); |
a95a7774 | 967 | struct nf_tcp_net *tn = nf_tcp_pernet(net); |
9fb9cbb1 | 968 | enum tcp_conntrack new_state, old_state; |
c779e849 | 969 | unsigned int index, *timeouts; |
6e250dcb | 970 | enum nf_ct_tcp_action res; |
9fb9cbb1 | 971 | enum ip_conntrack_dir dir; |
82f568fc JE |
972 | const struct tcphdr *th; |
973 | struct tcphdr _tcph; | |
9fb9cbb1 | 974 | unsigned long timeout; |
9fb9cbb1 YK |
975 | |
976 | th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph); | |
9976fc6e FW |
977 | if (th == NULL) |
978 | return -NF_ACCEPT; | |
979 | ||
0150ffba FW |
980 | if (tcp_error(th, skb, dataoff, state)) |
981 | return -NF_ACCEPT; | |
982 | ||
9976fc6e FW |
983 | if (!nf_ct_is_confirmed(ct) && !tcp_new(ct, skb, dataoff, th)) |
984 | return -NF_ACCEPT; | |
9fb9cbb1 | 985 | |
440f0d58 | 986 | spin_lock_bh(&ct->lock); |
c88130bc | 987 | old_state = ct->proto.tcp.state; |
9fb9cbb1 YK |
988 | dir = CTINFO2DIR(ctinfo); |
989 | index = get_conntrack_index(th); | |
990 | new_state = tcp_conntracks[dir][index][old_state]; | |
991 | ||
992 | switch (new_state) { | |
17311393 JK |
993 | case TCP_CONNTRACK_SYN_SENT: |
994 | if (old_state < TCP_CONNTRACK_TIME_WAIT) | |
995 | break; | |
b2155e7f JK |
996 | /* RFC 1122: "When a connection is closed actively, |
997 | * it MUST linger in TIME-WAIT state for a time 2xMSL | |
998 | * (Maximum Segment Lifetime). However, it MAY accept | |
999 | * a new SYN from the remote TCP to reopen the connection | |
1000 | * directly from TIME-WAIT state, if..." | |
1001 | * We ignore the conditions because we are in the | |
1002 | * TIME-WAIT state anyway. | |
1003 | * | |
1004 | * Handle aborted connections: we and the server | |
1005 | * think there is an existing connection but the client | |
1006 | * aborts it and starts a new one. | |
1007 | */ | |
1008 | if (((ct->proto.tcp.seen[dir].flags | |
1009 | | ct->proto.tcp.seen[!dir].flags) | |
1010 | & IP_CT_TCP_FLAG_CLOSE_INIT) | |
c88130bc PM |
1011 | || (ct->proto.tcp.last_dir == dir |
1012 | && ct->proto.tcp.last_index == TCP_RST_SET)) { | |
bc34b841 JK |
1013 | /* Attempt to reopen a closed/aborted connection. |
1014 | * Delete this connection and look up again. */ | |
440f0d58 | 1015 | spin_unlock_bh(&ct->lock); |
2aec609f | 1016 | |
6b69fe0c PM |
1017 | /* Only repeat if we can actually remove the timer. |
1018 | * Destruction may already be in progress in process | |
1019 | * context and we must give it a chance to terminate. | |
1020 | */ | |
2aec609f | 1021 | if (nf_ct_kill(ct)) |
6b69fe0c | 1022 | return -NF_REPEAT; |
ec8d5409 | 1023 | return NF_DROP; |
17311393 | 1024 | } |
954d8297 | 1025 | fallthrough; |
9fb9cbb1 | 1026 | case TCP_CONNTRACK_IGNORE: |
73f30602 | 1027 | /* Ignored packets: |
b2155e7f JK |
1028 | * |
1029 | * Our connection entry may be out of sync, so ignore | |
1030 | * packets which may signal the real connection between | |
1031 | * the client and the server. | |
73f30602 JK |
1032 | * |
1033 | * a) SYN in ORIGINAL | |
1034 | * b) SYN/ACK in REPLY | |
601e68e1 | 1035 | * c) ACK in reply direction after initial SYN in original. |
b2155e7f JK |
1036 | * |
1037 | * If the ignored packet is invalid, the receiver will send | |
1038 | * a RST we'll catch below. | |
73f30602 | 1039 | */ |
9fb9cbb1 | 1040 | if (index == TCP_SYNACK_SET |
c88130bc PM |
1041 | && ct->proto.tcp.last_index == TCP_SYN_SET |
1042 | && ct->proto.tcp.last_dir != dir | |
1043 | && ntohl(th->ack_seq) == ct->proto.tcp.last_end) { | |
b2155e7f | 1044 | /* b) This SYN/ACK acknowledges a SYN that we earlier |
9fb9cbb1 YK |
1045 | * ignored as invalid. This means that the client and |
1046 | * the server are both in sync, while the firewall is | |
c4832c7b PNA |
1047 | * not. We get in sync from the previously annotated |
1048 | * values. | |
9fb9cbb1 | 1049 | */ |
c4832c7b PNA |
1050 | old_state = TCP_CONNTRACK_SYN_SENT; |
1051 | new_state = TCP_CONNTRACK_SYN_RECV; | |
1052 | ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end = | |
1053 | ct->proto.tcp.last_end; | |
1054 | ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend = | |
1055 | ct->proto.tcp.last_end; | |
1056 | ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin = | |
1057 | ct->proto.tcp.last_win == 0 ? | |
1058 | 1 : ct->proto.tcp.last_win; | |
1059 | ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale = | |
1060 | ct->proto.tcp.last_wscale; | |
b3cad287 | 1061 | ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK; |
c4832c7b PNA |
1062 | ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags = |
1063 | ct->proto.tcp.last_flags; | |
f2dd495a | 1064 | nf_ct_tcp_state_reset(&ct->proto.tcp.seen[dir]); |
c4832c7b | 1065 | break; |
9fb9cbb1 | 1066 | } |
c88130bc PM |
1067 | ct->proto.tcp.last_index = index; |
1068 | ct->proto.tcp.last_dir = dir; | |
1069 | ct->proto.tcp.last_seq = ntohl(th->seq); | |
1070 | ct->proto.tcp.last_end = | |
9fb9cbb1 | 1071 | segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th); |
c4832c7b PNA |
1072 | ct->proto.tcp.last_win = ntohs(th->window); |
1073 | ||
1074 | /* a) This is a SYN in ORIGINAL. The client and the server | |
1075 | * may be in sync but we are not. In that case, we annotate | |
1076 | * the TCP options and let the packet go through. If it is a | |
1077 | * valid SYN packet, the server will reply with a SYN/ACK, and | |
b3cad287 JDB |
1078 | * then we'll get in sync. Otherwise, the server potentially |
1079 | * responds with a challenge ACK if implementing RFC5961. | |
1080 | */ | |
c4832c7b PNA |
1081 | if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) { |
1082 | struct ip_ct_tcp_state seen = {}; | |
1083 | ||
1084 | ct->proto.tcp.last_flags = | |
1085 | ct->proto.tcp.last_wscale = 0; | |
1086 | tcp_options(skb, dataoff, th, &seen); | |
1087 | if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) { | |
1088 | ct->proto.tcp.last_flags |= | |
1089 | IP_CT_TCP_FLAG_WINDOW_SCALE; | |
1090 | ct->proto.tcp.last_wscale = seen.td_scale; | |
1091 | } | |
1092 | if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) { | |
1093 | ct->proto.tcp.last_flags |= | |
1094 | IP_CT_TCP_FLAG_SACK_PERM; | |
1095 | } | |
b3cad287 JDB |
1096 | /* Mark the potential for RFC5961 challenge ACK, |
1097 | * this pose a special problem for LAST_ACK state | |
1098 | * as ACK is intrepretated as ACKing last FIN. | |
1099 | */ | |
1100 | if (old_state == TCP_CONNTRACK_LAST_ACK) | |
1101 | ct->proto.tcp.last_flags |= | |
1102 | IP_CT_EXP_CHALLENGE_ACK; | |
c4832c7b | 1103 | } |
c410cb97 FW |
1104 | |
1105 | /* possible challenge ack reply to syn */ | |
1106 | if (old_state == TCP_CONNTRACK_SYN_SENT && | |
1107 | index == TCP_ACK_SET && | |
1108 | dir == IP_CT_DIR_REPLY) | |
1109 | ct->proto.tcp.last_ack = ntohl(th->ack_seq); | |
1110 | ||
440f0d58 | 1111 | spin_unlock_bh(&ct->lock); |
62eec0d7 | 1112 | nf_ct_l4proto_log_invalid(skb, ct, state, |
07b5a76e FW |
1113 | "packet (index %d) in dir %d ignored, state %s", |
1114 | index, dir, | |
1115 | tcp_conntrack_names[old_state]); | |
9fb9cbb1 YK |
1116 | return NF_ACCEPT; |
1117 | case TCP_CONNTRACK_MAX: | |
48b1de4c PM |
1118 | /* Special case for SYN proxy: when the SYN to the server or |
1119 | * the SYN/ACK from the server is lost, the client may transmit | |
1120 | * a keep-alive packet while in SYN_SENT state. This needs to | |
1121 | * be associated with the original conntrack entry in order to | |
1122 | * generate a new SYN with the correct sequence number. | |
1123 | */ | |
1124 | if (nfct_synproxy(ct) && old_state == TCP_CONNTRACK_SYN_SENT && | |
1125 | index == TCP_ACK_SET && dir == IP_CT_DIR_ORIGINAL && | |
1126 | ct->proto.tcp.last_dir == IP_CT_DIR_ORIGINAL && | |
1127 | ct->proto.tcp.seen[dir].td_end - 1 == ntohl(th->seq)) { | |
1128 | pr_debug("nf_ct_tcp: SYN proxy client keep alive\n"); | |
1129 | spin_unlock_bh(&ct->lock); | |
1130 | return NF_ACCEPT; | |
1131 | } | |
1132 | ||
9fb9cbb1 | 1133 | /* Invalid packet */ |
440f0d58 | 1134 | spin_unlock_bh(&ct->lock); |
f74360d3 FW |
1135 | nf_ct_l4proto_log_invalid(skb, ct, state, |
1136 | "packet (index %d) in dir %d invalid, state %s", | |
1137 | index, dir, | |
1138 | tcp_conntrack_names[old_state]); | |
9fb9cbb1 | 1139 | return -NF_ACCEPT; |
b3cad287 JDB |
1140 | case TCP_CONNTRACK_TIME_WAIT: |
1141 | /* RFC5961 compliance cause stack to send "challenge-ACK" | |
1142 | * e.g. in response to spurious SYNs. Conntrack MUST | |
1143 | * not believe this ACK is acking last FIN. | |
1144 | */ | |
1145 | if (old_state == TCP_CONNTRACK_LAST_ACK && | |
1146 | index == TCP_ACK_SET && | |
1147 | ct->proto.tcp.last_dir != dir && | |
1148 | ct->proto.tcp.last_index == TCP_SYN_SET && | |
1149 | (ct->proto.tcp.last_flags & IP_CT_EXP_CHALLENGE_ACK)) { | |
1150 | /* Detected RFC5961 challenge ACK */ | |
1151 | ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK; | |
1152 | spin_unlock_bh(&ct->lock); | |
62eec0d7 | 1153 | nf_ct_l4proto_log_invalid(skb, ct, state, "challenge-ack ignored"); |
b3cad287 JDB |
1154 | return NF_ACCEPT; /* Don't change state */ |
1155 | } | |
1156 | break; | |
72d4d3e3 JK |
1157 | case TCP_CONNTRACK_SYN_SENT2: |
1158 | /* tcp_conntracks table is not smart enough to handle | |
1159 | * simultaneous open. | |
1160 | */ | |
1161 | ct->proto.tcp.last_flags |= IP_CT_TCP_SIMULTANEOUS_OPEN; | |
1162 | break; | |
1163 | case TCP_CONNTRACK_SYN_RECV: | |
1164 | if (dir == IP_CT_DIR_REPLY && index == TCP_ACK_SET && | |
1165 | ct->proto.tcp.last_flags & IP_CT_TCP_SIMULTANEOUS_OPEN) | |
1166 | new_state = TCP_CONNTRACK_ESTABLISHED; | |
1167 | break; | |
9fb9cbb1 | 1168 | case TCP_CONNTRACK_CLOSE: |
be0502a3 FW |
1169 | if (index != TCP_RST_SET) |
1170 | break; | |
1171 | ||
c4edc3cc AA |
1172 | /* If we are closing, tuple might have been re-used already. |
1173 | * last_index, last_ack, and all other ct fields used for | |
1174 | * sequence/window validation are outdated in that case. | |
1175 | * | |
1176 | * As the conntrack can already be expired by GC under pressure, | |
1177 | * just skip validation checks. | |
1178 | */ | |
1179 | if (tcp_can_early_drop(ct)) | |
1180 | goto in_window; | |
1181 | ||
1182 | /* td_maxack might be outdated if we let a SYN through earlier */ | |
1183 | if ((ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET) && | |
1184 | ct->proto.tcp.last_index != TCP_SYN_SET) { | |
be0502a3 FW |
1185 | u32 seq = ntohl(th->seq); |
1186 | ||
c4edc3cc AA |
1187 | /* If we are not in established state and SEQ=0 this is most |
1188 | * likely an answer to a SYN we let go through above (last_index | |
1189 | * can be updated due to out-of-order ACKs). | |
1190 | */ | |
1191 | if (seq == 0 && !nf_conntrack_tcp_established(ct)) | |
1192 | break; | |
1193 | ||
1da4cd82 AA |
1194 | if (before(seq, ct->proto.tcp.seen[!dir].td_maxack) && |
1195 | !tn->tcp_ignore_invalid_rst) { | |
be0502a3 FW |
1196 | /* Invalid RST */ |
1197 | spin_unlock_bh(&ct->lock); | |
62eec0d7 | 1198 | nf_ct_l4proto_log_invalid(skb, ct, state, "invalid rst"); |
be0502a3 FW |
1199 | return -NF_ACCEPT; |
1200 | } | |
1201 | ||
1202 | if (!nf_conntrack_tcp_established(ct) || | |
1203 | seq == ct->proto.tcp.seen[!dir].td_maxack) | |
1204 | break; | |
1205 | ||
1206 | /* Check if rst is part of train, such as | |
1207 | * foo:80 > bar:4379: P, 235946583:235946602(19) ack 42 | |
1208 | * foo:80 > bar:4379: R, 235946602:235946602(0) ack 42 | |
1209 | */ | |
1210 | if (ct->proto.tcp.last_index == TCP_ACK_SET && | |
1211 | ct->proto.tcp.last_dir == dir && | |
1212 | seq == ct->proto.tcp.last_end) | |
1213 | break; | |
1214 | ||
1215 | /* ... RST sequence number doesn't match exactly, keep | |
1216 | * established state to allow a possible challenge ACK. | |
1217 | */ | |
1218 | new_state = old_state; | |
bfcaa502 | 1219 | } |
be0502a3 | 1220 | if (((test_bit(IPS_SEEN_REPLY_BIT, &ct->status) |
c88130bc PM |
1221 | && ct->proto.tcp.last_index == TCP_SYN_SET) |
1222 | || (!test_bit(IPS_ASSURED_BIT, &ct->status) | |
1223 | && ct->proto.tcp.last_index == TCP_ACK_SET)) | |
1224 | && ntohl(th->ack_seq) == ct->proto.tcp.last_end) { | |
93b1fae4 | 1225 | /* RST sent to invalid SYN or ACK we had let through |
73f30602 JK |
1226 | * at a) and c) above: |
1227 | * | |
1228 | * a) SYN was in window then | |
1229 | * c) we hold a half-open connection. | |
1230 | * | |
1231 | * Delete our connection entry. | |
9fb9cbb1 | 1232 | * We skip window checking, because packet might ACK |
73f30602 | 1233 | * segments we ignored. */ |
9fb9cbb1 YK |
1234 | goto in_window; |
1235 | } | |
c410cb97 FW |
1236 | |
1237 | /* Reset in response to a challenge-ack we let through earlier */ | |
1238 | if (old_state == TCP_CONNTRACK_SYN_SENT && | |
1239 | ct->proto.tcp.last_index == TCP_ACK_SET && | |
1240 | ct->proto.tcp.last_dir == IP_CT_DIR_REPLY && | |
1241 | ntohl(th->seq) == ct->proto.tcp.last_ack) | |
1242 | goto in_window; | |
1243 | ||
be0502a3 | 1244 | break; |
9fb9cbb1 YK |
1245 | default: |
1246 | /* Keep compilers happy. */ | |
1247 | break; | |
1248 | } | |
1249 | ||
6e250dcb FW |
1250 | res = tcp_in_window(ct, dir, index, |
1251 | skb, dataoff, th, state); | |
1252 | switch (res) { | |
1253 | case NFCT_TCP_IGNORE: | |
1254 | spin_unlock_bh(&ct->lock); | |
1255 | return NF_ACCEPT; | |
1256 | case NFCT_TCP_INVALID: | |
628d6943 | 1257 | nf_tcp_handle_invalid(ct, dir, index, skb, state); |
440f0d58 | 1258 | spin_unlock_bh(&ct->lock); |
9fb9cbb1 | 1259 | return -NF_ACCEPT; |
6e250dcb FW |
1260 | case NFCT_TCP_ACCEPT: |
1261 | break; | |
9fb9cbb1 YK |
1262 | } |
1263 | in_window: | |
1264 | /* From now on we have got in-window packets */ | |
c88130bc PM |
1265 | ct->proto.tcp.last_index = index; |
1266 | ct->proto.tcp.last_dir = dir; | |
9fb9cbb1 | 1267 | |
c88130bc | 1268 | ct->proto.tcp.state = new_state; |
9fb9cbb1 | 1269 | if (old_state != new_state |
d0c1fd7a | 1270 | && new_state == TCP_CONNTRACK_FIN_WAIT) |
c88130bc | 1271 | ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT; |
ae375044 | 1272 | |
c779e849 FW |
1273 | timeouts = nf_ct_timeout_lookup(ct); |
1274 | if (!timeouts) | |
1275 | timeouts = tn->timeouts; | |
1276 | ||
d2ba1fde | 1277 | if (ct->proto.tcp.retrans >= tn->tcp_max_retrans && |
2c8503f5 PNA |
1278 | timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS]) |
1279 | timeout = timeouts[TCP_CONNTRACK_RETRANS]; | |
be0502a3 FW |
1280 | else if (unlikely(index == TCP_RST_SET)) |
1281 | timeout = timeouts[TCP_CONNTRACK_CLOSE]; | |
ae375044 PM |
1282 | else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) & |
1283 | IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED && | |
2c8503f5 PNA |
1284 | timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK]) |
1285 | timeout = timeouts[TCP_CONNTRACK_UNACK]; | |
fbcd253d FW |
1286 | else if (ct->proto.tcp.last_win == 0 && |
1287 | timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS]) | |
1288 | timeout = timeouts[TCP_CONNTRACK_RETRANS]; | |
ae375044 | 1289 | else |
2c8503f5 | 1290 | timeout = timeouts[new_state]; |
440f0d58 | 1291 | spin_unlock_bh(&ct->lock); |
9fb9cbb1 | 1292 | |
9fb9cbb1 | 1293 | if (new_state != old_state) |
a71996fc | 1294 | nf_conntrack_event_cache(IPCT_PROTOINFO, ct); |
9fb9cbb1 | 1295 | |
c88130bc | 1296 | if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
9fb9cbb1 YK |
1297 | /* If only reply is a RST, we can consider ourselves not to |
1298 | have an established connection: this is a fairly common | |
1299 | problem case, so we can delete the conntrack | |
1300 | immediately. --RR */ | |
1301 | if (th->rst) { | |
718d4ad9 | 1302 | nf_ct_kill_acct(ct, ctinfo, skb); |
9fb9cbb1 YK |
1303 | return NF_ACCEPT; |
1304 | } | |
e15d4cdf FW |
1305 | |
1306 | if (index == TCP_SYN_SET && old_state == TCP_CONNTRACK_SYN_SENT) { | |
1307 | /* do not renew timeout on SYN retransmit. | |
1308 | * | |
1309 | * Else port reuse by client or NAT middlebox can keep | |
1310 | * entry alive indefinitely (including nat info). | |
1311 | */ | |
1312 | return NF_ACCEPT; | |
1313 | } | |
1314 | ||
6547a221 FW |
1315 | /* ESTABLISHED without SEEN_REPLY, i.e. mid-connection |
1316 | * pickup with loose=1. Avoid large ESTABLISHED timeout. | |
1317 | */ | |
1318 | if (new_state == TCP_CONNTRACK_ESTABLISHED && | |
1319 | timeout > timeouts[TCP_CONNTRACK_UNACK]) | |
1320 | timeout = timeouts[TCP_CONNTRACK_UNACK]; | |
c88130bc | 1321 | } else if (!test_bit(IPS_ASSURED_BIT, &ct->status) |
9fb9cbb1 YK |
1322 | && (old_state == TCP_CONNTRACK_SYN_RECV |
1323 | || old_state == TCP_CONNTRACK_ESTABLISHED) | |
1324 | && new_state == TCP_CONNTRACK_ESTABLISHED) { | |
4b7ddc58 | 1325 | /* Set ASSURED if we see valid ack in ESTABLISHED |
601e68e1 | 1326 | after SYN_RECV or a valid answer for a picked up |
9fb9cbb1 | 1327 | connection. */ |
c88130bc | 1328 | set_bit(IPS_ASSURED_BIT, &ct->status); |
858b3133 | 1329 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
9fb9cbb1 | 1330 | } |
c88130bc | 1331 | nf_ct_refresh_acct(ct, ctinfo, skb, timeout); |
9fb9cbb1 YK |
1332 | |
1333 | return NF_ACCEPT; | |
1334 | } | |
601e68e1 | 1335 | |
c0cd1156 | 1336 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c1d10adb PNA |
1337 | |
1338 | #include <linux/netfilter/nfnetlink.h> | |
1339 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
1340 | ||
fdf70832 | 1341 | static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, |
86d21fc7 | 1342 | struct nf_conn *ct, bool destroy) |
c1d10adb | 1343 | { |
df6fb868 | 1344 | struct nlattr *nest_parms; |
c8e2078c | 1345 | struct nf_ct_tcp_flags tmp = {}; |
601e68e1 | 1346 | |
440f0d58 | 1347 | spin_lock_bh(&ct->lock); |
ae0be8de | 1348 | nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP); |
df6fb868 PM |
1349 | if (!nest_parms) |
1350 | goto nla_put_failure; | |
1351 | ||
86d21fc7 FW |
1352 | if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state)) |
1353 | goto nla_put_failure; | |
1354 | ||
1355 | if (destroy) | |
1356 | goto skip_state; | |
1357 | ||
1358 | if (nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL, | |
4925a459 DM |
1359 | ct->proto.tcp.seen[0].td_scale) || |
1360 | nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY, | |
1361 | ct->proto.tcp.seen[1].td_scale)) | |
1362 | goto nla_put_failure; | |
c8e2078c PNA |
1363 | |
1364 | tmp.flags = ct->proto.tcp.seen[0].flags; | |
4925a459 DM |
1365 | if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL, |
1366 | sizeof(struct nf_ct_tcp_flags), &tmp)) | |
1367 | goto nla_put_failure; | |
c8e2078c PNA |
1368 | |
1369 | tmp.flags = ct->proto.tcp.seen[1].flags; | |
4925a459 DM |
1370 | if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY, |
1371 | sizeof(struct nf_ct_tcp_flags), &tmp)) | |
1372 | goto nla_put_failure; | |
86d21fc7 | 1373 | skip_state: |
440f0d58 | 1374 | spin_unlock_bh(&ct->lock); |
df6fb868 | 1375 | nla_nest_end(skb, nest_parms); |
c1d10adb PNA |
1376 | |
1377 | return 0; | |
1378 | ||
df6fb868 | 1379 | nla_put_failure: |
440f0d58 | 1380 | spin_unlock_bh(&ct->lock); |
c1d10adb PNA |
1381 | return -1; |
1382 | } | |
1383 | ||
f73e924c PM |
1384 | static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = { |
1385 | [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 }, | |
1386 | [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 }, | |
1387 | [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 }, | |
1388 | [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) }, | |
7e59b3fe | 1389 | [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) }, |
c1d10adb PNA |
1390 | }; |
1391 | ||
39215846 FW |
1392 | #define TCP_NLATTR_SIZE ( \ |
1393 | NLA_ALIGN(NLA_HDRLEN + 1) + \ | |
1394 | NLA_ALIGN(NLA_HDRLEN + 1) + \ | |
346fa83d | 1395 | NLA_ALIGN(NLA_HDRLEN + sizeof(struct nf_ct_tcp_flags)) + \ |
1396 | NLA_ALIGN(NLA_HDRLEN + sizeof(struct nf_ct_tcp_flags))) | |
39215846 | 1397 | |
fdf70832 | 1398 | static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct) |
c1d10adb | 1399 | { |
2f0d2f10 | 1400 | struct nlattr *pattr = cda[CTA_PROTOINFO_TCP]; |
df6fb868 | 1401 | struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1]; |
f73e924c | 1402 | int err; |
c1d10adb PNA |
1403 | |
1404 | /* updates could not contain anything about the private | |
1405 | * protocol info, in that case skip the parsing */ | |
2f0d2f10 | 1406 | if (!pattr) |
c1d10adb PNA |
1407 | return 0; |
1408 | ||
8cb08174 JB |
1409 | err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_TCP_MAX, pattr, |
1410 | tcp_nla_policy, NULL); | |
f73e924c PM |
1411 | if (err < 0) |
1412 | return err; | |
c1d10adb | 1413 | |
5f7da4d2 PM |
1414 | if (tb[CTA_PROTOINFO_TCP_STATE] && |
1415 | nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX) | |
c1d10adb PNA |
1416 | return -EINVAL; |
1417 | ||
440f0d58 | 1418 | spin_lock_bh(&ct->lock); |
5f7da4d2 PM |
1419 | if (tb[CTA_PROTOINFO_TCP_STATE]) |
1420 | ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]); | |
c8e2078c | 1421 | |
df6fb868 | 1422 | if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) { |
c8e2078c | 1423 | struct nf_ct_tcp_flags *attr = |
df6fb868 | 1424 | nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]); |
c8e2078c PNA |
1425 | ct->proto.tcp.seen[0].flags &= ~attr->mask; |
1426 | ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask; | |
1427 | } | |
1428 | ||
df6fb868 | 1429 | if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) { |
c8e2078c | 1430 | struct nf_ct_tcp_flags *attr = |
df6fb868 | 1431 | nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]); |
c8e2078c PNA |
1432 | ct->proto.tcp.seen[1].flags &= ~attr->mask; |
1433 | ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask; | |
1434 | } | |
1435 | ||
df6fb868 PM |
1436 | if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] && |
1437 | tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] && | |
c8e2078c PNA |
1438 | ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE && |
1439 | ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) { | |
77236b6e PM |
1440 | ct->proto.tcp.seen[0].td_scale = |
1441 | nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]); | |
1442 | ct->proto.tcp.seen[1].td_scale = | |
1443 | nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]); | |
c8e2078c | 1444 | } |
440f0d58 | 1445 | spin_unlock_bh(&ct->lock); |
c1d10adb PNA |
1446 | |
1447 | return 0; | |
1448 | } | |
a400c30e | 1449 | |
5caaed15 | 1450 | static unsigned int tcp_nlattr_tuple_size(void) |
a400c30e | 1451 | { |
5caaed15 FW |
1452 | static unsigned int size __read_mostly; |
1453 | ||
1454 | if (!size) | |
1455 | size = nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1); | |
1456 | ||
1457 | return size; | |
a400c30e | 1458 | } |
c1d10adb | 1459 | #endif |
933a41e7 | 1460 | |
a874752a | 1461 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
1462 | |
1463 | #include <linux/netfilter/nfnetlink.h> | |
1464 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
1465 | ||
8264deb8 G |
1466 | static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[], |
1467 | struct net *net, void *data) | |
50978462 | 1468 | { |
a95a7774 | 1469 | struct nf_tcp_net *tn = nf_tcp_pernet(net); |
c779e849 | 1470 | unsigned int *timeouts = data; |
50978462 PNA |
1471 | int i; |
1472 | ||
c779e849 FW |
1473 | if (!timeouts) |
1474 | timeouts = tn->timeouts; | |
50978462 PNA |
1475 | /* set default TCP timeouts. */ |
1476 | for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++) | |
8264deb8 | 1477 | timeouts[i] = tn->timeouts[i]; |
50978462 PNA |
1478 | |
1479 | if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) { | |
1480 | timeouts[TCP_CONNTRACK_SYN_SENT] = | |
1481 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ; | |
1482 | } | |
ef39078d | 1483 | |
50978462 PNA |
1484 | if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) { |
1485 | timeouts[TCP_CONNTRACK_SYN_RECV] = | |
1486 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ; | |
1487 | } | |
1488 | if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) { | |
1489 | timeouts[TCP_CONNTRACK_ESTABLISHED] = | |
1490 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ; | |
1491 | } | |
1492 | if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) { | |
1493 | timeouts[TCP_CONNTRACK_FIN_WAIT] = | |
1494 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ; | |
1495 | } | |
1496 | if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) { | |
1497 | timeouts[TCP_CONNTRACK_CLOSE_WAIT] = | |
1498 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ; | |
1499 | } | |
1500 | if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) { | |
1501 | timeouts[TCP_CONNTRACK_LAST_ACK] = | |
1502 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ; | |
1503 | } | |
1504 | if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) { | |
1505 | timeouts[TCP_CONNTRACK_TIME_WAIT] = | |
1506 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ; | |
1507 | } | |
1508 | if (tb[CTA_TIMEOUT_TCP_CLOSE]) { | |
1509 | timeouts[TCP_CONNTRACK_CLOSE] = | |
1510 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ; | |
1511 | } | |
1512 | if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) { | |
1513 | timeouts[TCP_CONNTRACK_SYN_SENT2] = | |
1514 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ; | |
1515 | } | |
1516 | if (tb[CTA_TIMEOUT_TCP_RETRANS]) { | |
1517 | timeouts[TCP_CONNTRACK_RETRANS] = | |
1518 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ; | |
1519 | } | |
1520 | if (tb[CTA_TIMEOUT_TCP_UNACK]) { | |
1521 | timeouts[TCP_CONNTRACK_UNACK] = | |
1522 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ; | |
1523 | } | |
ef39078d FW |
1524 | |
1525 | timeouts[CTA_TIMEOUT_TCP_UNSPEC] = timeouts[CTA_TIMEOUT_TCP_SYN_SENT]; | |
50978462 PNA |
1526 | return 0; |
1527 | } | |
1528 | ||
1529 | static int | |
1530 | tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
1531 | { | |
1532 | const unsigned int *timeouts = data; | |
1533 | ||
4925a459 DM |
1534 | if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT, |
1535 | htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) || | |
1536 | nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV, | |
1537 | htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) || | |
1538 | nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED, | |
1539 | htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) || | |
1540 | nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT, | |
1541 | htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) || | |
1542 | nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT, | |
1543 | htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) || | |
1544 | nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK, | |
1545 | htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) || | |
1546 | nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT, | |
1547 | htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) || | |
1548 | nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE, | |
1549 | htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) || | |
1550 | nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2, | |
1551 | htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) || | |
1552 | nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS, | |
1553 | htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) || | |
1554 | nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK, | |
1555 | htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ))) | |
1556 | goto nla_put_failure; | |
50978462 PNA |
1557 | return 0; |
1558 | ||
1559 | nla_put_failure: | |
1560 | return -ENOSPC; | |
1561 | } | |
1562 | ||
1563 | static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = { | |
1564 | [CTA_TIMEOUT_TCP_SYN_SENT] = { .type = NLA_U32 }, | |
1565 | [CTA_TIMEOUT_TCP_SYN_RECV] = { .type = NLA_U32 }, | |
1566 | [CTA_TIMEOUT_TCP_ESTABLISHED] = { .type = NLA_U32 }, | |
1567 | [CTA_TIMEOUT_TCP_FIN_WAIT] = { .type = NLA_U32 }, | |
1568 | [CTA_TIMEOUT_TCP_CLOSE_WAIT] = { .type = NLA_U32 }, | |
1569 | [CTA_TIMEOUT_TCP_LAST_ACK] = { .type = NLA_U32 }, | |
1570 | [CTA_TIMEOUT_TCP_TIME_WAIT] = { .type = NLA_U32 }, | |
1571 | [CTA_TIMEOUT_TCP_CLOSE] = { .type = NLA_U32 }, | |
1572 | [CTA_TIMEOUT_TCP_SYN_SENT2] = { .type = NLA_U32 }, | |
6d1fafca FW |
1573 | [CTA_TIMEOUT_TCP_RETRANS] = { .type = NLA_U32 }, |
1574 | [CTA_TIMEOUT_TCP_UNACK] = { .type = NLA_U32 }, | |
50978462 | 1575 | }; |
a874752a | 1576 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
50978462 | 1577 | |
2a389de8 | 1578 | void nf_conntrack_tcp_init_net(struct net *net) |
d2ba1fde | 1579 | { |
a95a7774 | 1580 | struct nf_tcp_net *tn = nf_tcp_pernet(net); |
2a389de8 | 1581 | int i; |
d2ba1fde | 1582 | |
2a389de8 FW |
1583 | for (i = 0; i < TCP_CONNTRACK_TIMEOUT_MAX; i++) |
1584 | tn->timeouts[i] = tcp_timeouts[i]; | |
d2ba1fde | 1585 | |
2a389de8 FW |
1586 | /* timeouts[0] is unused, make it same as SYN_SENT so |
1587 | * ->timeouts[0] contains 'new' timeout, like udp or icmp. | |
1588 | */ | |
1589 | tn->timeouts[0] = tcp_timeouts[TCP_CONNTRACK_SYN_SENT]; | |
9b1a4d0f FW |
1590 | |
1591 | /* If it is set to zero, we disable picking up already established | |
1592 | * connections. | |
1593 | */ | |
1594 | tn->tcp_loose = 1; | |
1595 | ||
1596 | /* "Be conservative in what you do, | |
1597 | * be liberal in what you accept from others." | |
1598 | * If it's non-zero, we mark only out of window RST segments as INVALID. | |
1599 | */ | |
1600 | tn->tcp_be_liberal = 0; | |
1601 | ||
1da4cd82 AA |
1602 | /* If it's non-zero, we turn off RST sequence number check */ |
1603 | tn->tcp_ignore_invalid_rst = 0; | |
1604 | ||
9b1a4d0f FW |
1605 | /* Max number of the retransmitted packets without receiving an (acceptable) |
1606 | * ACK from the destination. If this number is reached, a shorter timer | |
1607 | * will be started. | |
1608 | */ | |
1609 | tn->tcp_max_retrans = 3; | |
ef8ed5ea OS |
1610 | |
1611 | #if IS_ENABLED(CONFIG_NF_FLOW_TABLE) | |
1612 | tn->offload_timeout = 30 * HZ; | |
ef8ed5ea | 1613 | #endif |
08911475 PNA |
1614 | } |
1615 | ||
dd2934a9 | 1616 | const struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp = |
9fb9cbb1 | 1617 | { |
605dcad6 | 1618 | .l4proto = IPPROTO_TCP, |
ea48cc83 | 1619 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
9fb9cbb1 | 1620 | .print_conntrack = tcp_print_conntrack, |
ea48cc83 | 1621 | #endif |
c6dd940b | 1622 | .can_early_drop = tcp_can_early_drop, |
c0cd1156 | 1623 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 PM |
1624 | .to_nlattr = tcp_to_nlattr, |
1625 | .from_nlattr = nlattr_to_tcp, | |
1626 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, | |
1627 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
a400c30e | 1628 | .nlattr_tuple_size = tcp_nlattr_tuple_size, |
39215846 | 1629 | .nlattr_size = TCP_NLATTR_SIZE, |
f73e924c | 1630 | .nla_policy = nf_ct_port_nla_policy, |
c1d10adb | 1631 | #endif |
a874752a | 1632 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
1633 | .ctnl_timeout = { |
1634 | .nlattr_to_obj = tcp_timeout_nlattr_to_obj, | |
1635 | .obj_to_nlattr = tcp_timeout_obj_to_nlattr, | |
1636 | .nlattr_max = CTA_TIMEOUT_TCP_MAX, | |
1637 | .obj_size = sizeof(unsigned int) * | |
1638 | TCP_CONNTRACK_TIMEOUT_MAX, | |
1639 | .nla_policy = tcp_timeout_nla_policy, | |
1640 | }, | |
a874752a | 1641 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
9fb9cbb1 | 1642 | }; |