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