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