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