]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* FTP extension for connection tracking. */ |
2 | ||
3 | /* (C) 1999-2001 Paul `Rusty' Russell | |
4 | * (C) 2002-2004 Netfilter Core Team <[email protected]> | |
5 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> | |
f229f6ce | 6 | * (C) 2006-2012 Patrick McHardy <[email protected]> |
9fb9cbb1 YK |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
11 | */ |
12 | ||
ad6d9503 PNA |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
9fb9cbb1 YK |
15 | #include <linux/module.h> |
16 | #include <linux/moduleparam.h> | |
17 | #include <linux/netfilter.h> | |
18 | #include <linux/ip.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
9fb9cbb1 YK |
20 | #include <linux/ipv6.h> |
21 | #include <linux/ctype.h> | |
6a28ec8c | 22 | #include <linux/inet.h> |
9fb9cbb1 YK |
23 | #include <net/checksum.h> |
24 | #include <net/tcp.h> | |
25 | ||
26 | #include <net/netfilter/nf_conntrack.h> | |
77ab9cff | 27 | #include <net/netfilter/nf_conntrack_expect.h> |
f6180121 | 28 | #include <net/netfilter/nf_conntrack_ecache.h> |
9fb9cbb1 YK |
29 | #include <net/netfilter/nf_conntrack_helper.h> |
30 | #include <linux/netfilter/nf_conntrack_ftp.h> | |
31 | ||
32 | MODULE_LICENSE("GPL"); | |
33 | MODULE_AUTHOR("Rusty Russell <[email protected]>"); | |
34 | MODULE_DESCRIPTION("ftp connection tracking helper"); | |
d2483dde | 35 | MODULE_ALIAS("ip_conntrack_ftp"); |
4dc06f96 | 36 | MODULE_ALIAS_NFCT_HELPER("ftp"); |
9fb9cbb1 YK |
37 | |
38 | /* This is slow, but it's simple. --RR */ | |
39 | static char *ftp_buffer; | |
40 | ||
41 | static DEFINE_SPINLOCK(nf_ftp_lock); | |
42 | ||
43 | #define MAX_PORTS 8 | |
44 | static u_int16_t ports[MAX_PORTS]; | |
45 | static unsigned int ports_c; | |
46 | module_param_array(ports, ushort, &ports_c, 0400); | |
47 | ||
eb939922 | 48 | static bool loose; |
e7be6994 | 49 | module_param(loose, bool, 0600); |
9fb9cbb1 | 50 | |
3db05fea | 51 | unsigned int (*nf_nat_ftp_hook)(struct sk_buff *skb, |
9fb9cbb1 | 52 | enum ip_conntrack_info ctinfo, |
55a73324 | 53 | enum nf_ct_ftp_type type, |
051966c0 | 54 | unsigned int protoff, |
9fb9cbb1 YK |
55 | unsigned int matchoff, |
56 | unsigned int matchlen, | |
25b86e05 | 57 | struct nf_conntrack_expect *exp); |
9fb9cbb1 YK |
58 | EXPORT_SYMBOL_GPL(nf_nat_ftp_hook); |
59 | ||
4e7dba99 JM |
60 | static int try_rfc959(const char *, size_t, struct nf_conntrack_man *, |
61 | char, unsigned int *); | |
62 | static int try_rfc1123(const char *, size_t, struct nf_conntrack_man *, | |
63 | char, unsigned int *); | |
64 | static int try_eprt(const char *, size_t, struct nf_conntrack_man *, | |
65 | char, unsigned int *); | |
9fb9cbb1 | 66 | static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *, |
4e7dba99 | 67 | char, unsigned int *); |
9fb9cbb1 YK |
68 | |
69 | static struct ftp_search { | |
9fb9cbb1 YK |
70 | const char *pattern; |
71 | size_t plen; | |
72 | char skip; | |
73 | char term; | |
55a73324 | 74 | enum nf_ct_ftp_type ftptype; |
4e7dba99 | 75 | int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char, unsigned int *); |
7d8c5018 PM |
76 | } search[IP_CT_DIR_MAX][2] = { |
77 | [IP_CT_DIR_ORIGINAL] = { | |
78 | { | |
79 | .pattern = "PORT", | |
80 | .plen = sizeof("PORT") - 1, | |
81 | .skip = ' ', | |
82 | .term = '\r', | |
55a73324 | 83 | .ftptype = NF_CT_FTP_PORT, |
7d8c5018 PM |
84 | .getnum = try_rfc959, |
85 | }, | |
86 | { | |
87 | .pattern = "EPRT", | |
88 | .plen = sizeof("EPRT") - 1, | |
89 | .skip = ' ', | |
90 | .term = '\r', | |
55a73324 | 91 | .ftptype = NF_CT_FTP_EPRT, |
7d8c5018 PM |
92 | .getnum = try_eprt, |
93 | }, | |
9fb9cbb1 | 94 | }, |
7d8c5018 PM |
95 | [IP_CT_DIR_REPLY] = { |
96 | { | |
97 | .pattern = "227 ", | |
98 | .plen = sizeof("227 ") - 1, | |
55a73324 | 99 | .ftptype = NF_CT_FTP_PASV, |
4e7dba99 | 100 | .getnum = try_rfc1123, |
7d8c5018 PM |
101 | }, |
102 | { | |
103 | .pattern = "229 ", | |
104 | .plen = sizeof("229 ") - 1, | |
105 | .skip = '(', | |
106 | .term = ')', | |
55a73324 | 107 | .ftptype = NF_CT_FTP_EPSV, |
7d8c5018 PM |
108 | .getnum = try_epsv_response, |
109 | }, | |
9fb9cbb1 YK |
110 | }, |
111 | }; | |
112 | ||
9fb9cbb1 YK |
113 | static int |
114 | get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term) | |
115 | { | |
6a28ec8c DM |
116 | const char *end; |
117 | int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end); | |
1884f78c YH |
118 | if (ret > 0) |
119 | return (int)(end - src); | |
120 | return 0; | |
9fb9cbb1 YK |
121 | } |
122 | ||
123 | static int try_number(const char *data, size_t dlen, u_int32_t array[], | |
601e68e1 | 124 | int array_size, char sep, char term) |
9fb9cbb1 YK |
125 | { |
126 | u_int32_t i, len; | |
127 | ||
128 | memset(array, 0, sizeof(array[0])*array_size); | |
129 | ||
130 | /* Keep data pointing at next char. */ | |
131 | for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) { | |
132 | if (*data >= '0' && *data <= '9') { | |
133 | array[i] = array[i]*10 + *data - '0'; | |
134 | } | |
135 | else if (*data == sep) | |
136 | i++; | |
137 | else { | |
138 | /* Unexpected character; true if it's the | |
4e7dba99 JM |
139 | terminator (or we don't care about one) |
140 | and we're finished. */ | |
141 | if ((*data == term || !term) && i == array_size - 1) | |
9fb9cbb1 YK |
142 | return len; |
143 | ||
0d53778e PM |
144 | pr_debug("Char %u (got %u nums) `%u' unexpected\n", |
145 | len, i, *data); | |
9fb9cbb1 YK |
146 | return 0; |
147 | } | |
148 | } | |
0d53778e PM |
149 | pr_debug("Failed to fill %u numbers separated by %c\n", |
150 | array_size, sep); | |
9fb9cbb1 YK |
151 | return 0; |
152 | } | |
153 | ||
154 | /* Returns 0, or length of numbers: 192,168,1,1,5,6 */ | |
155 | static int try_rfc959(const char *data, size_t dlen, | |
4e7dba99 JM |
156 | struct nf_conntrack_man *cmd, char term, |
157 | unsigned int *offset) | |
9fb9cbb1 YK |
158 | { |
159 | int length; | |
160 | u_int32_t array[6]; | |
161 | ||
162 | length = try_number(data, dlen, array, 6, ',', term); | |
163 | if (length == 0) | |
164 | return 0; | |
165 | ||
166 | cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) | | |
167 | (array[2] << 8) | array[3]); | |
168 | cmd->u.tcp.port = htons((array[4] << 8) | array[5]); | |
169 | return length; | |
170 | } | |
171 | ||
4e7dba99 JM |
172 | /* |
173 | * From RFC 1123: | |
174 | * The format of the 227 reply to a PASV command is not | |
175 | * well standardized. In particular, an FTP client cannot | |
176 | * assume that the parentheses shown on page 40 of RFC-959 | |
177 | * will be present (and in fact, Figure 3 on page 43 omits | |
178 | * them). Therefore, a User-FTP program that interprets | |
179 | * the PASV reply must scan the reply for the first digit | |
180 | * of the host and port numbers. | |
181 | */ | |
182 | static int try_rfc1123(const char *data, size_t dlen, | |
183 | struct nf_conntrack_man *cmd, char term, | |
184 | unsigned int *offset) | |
185 | { | |
186 | int i; | |
187 | for (i = 0; i < dlen; i++) | |
188 | if (isdigit(data[i])) | |
189 | break; | |
190 | ||
191 | if (i == dlen) | |
192 | return 0; | |
193 | ||
194 | *offset += i; | |
195 | ||
196 | return try_rfc959(data + i, dlen - i, cmd, 0, offset); | |
197 | } | |
198 | ||
9fb9cbb1 YK |
199 | /* Grab port: number up to delimiter */ |
200 | static int get_port(const char *data, int start, size_t dlen, char delim, | |
bff9a89b | 201 | __be16 *port) |
9fb9cbb1 YK |
202 | { |
203 | u_int16_t tmp_port = 0; | |
204 | int i; | |
205 | ||
206 | for (i = start; i < dlen; i++) { | |
207 | /* Finished? */ | |
208 | if (data[i] == delim) { | |
209 | if (tmp_port == 0) | |
210 | break; | |
211 | *port = htons(tmp_port); | |
0d53778e | 212 | pr_debug("get_port: return %d\n", tmp_port); |
9fb9cbb1 YK |
213 | return i + 1; |
214 | } | |
215 | else if (data[i] >= '0' && data[i] <= '9') | |
216 | tmp_port = tmp_port*10 + data[i] - '0'; | |
217 | else { /* Some other crap */ | |
0d53778e | 218 | pr_debug("get_port: invalid char.\n"); |
9fb9cbb1 YK |
219 | break; |
220 | } | |
221 | } | |
222 | return 0; | |
223 | } | |
224 | ||
225 | /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */ | |
226 | static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd, | |
4e7dba99 | 227 | char term, unsigned int *offset) |
9fb9cbb1 YK |
228 | { |
229 | char delim; | |
230 | int length; | |
231 | ||
232 | /* First character is delimiter, then "1" for IPv4 or "2" for IPv6, | |
233 | then delimiter again. */ | |
234 | if (dlen <= 3) { | |
0d53778e | 235 | pr_debug("EPRT: too short\n"); |
9fb9cbb1 YK |
236 | return 0; |
237 | } | |
238 | delim = data[0]; | |
239 | if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) { | |
8d6c0eaa | 240 | pr_debug("try_eprt: invalid delimiter.\n"); |
9fb9cbb1 YK |
241 | return 0; |
242 | } | |
243 | ||
244 | if ((cmd->l3num == PF_INET && data[1] != '1') || | |
245 | (cmd->l3num == PF_INET6 && data[1] != '2')) { | |
0d53778e | 246 | pr_debug("EPRT: invalid protocol number.\n"); |
9fb9cbb1 YK |
247 | return 0; |
248 | } | |
249 | ||
0d53778e | 250 | pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim); |
9fb9cbb1 YK |
251 | |
252 | if (data[1] == '1') { | |
253 | u_int32_t array[4]; | |
254 | ||
255 | /* Now we have IP address. */ | |
256 | length = try_number(data + 3, dlen - 3, array, 4, '.', delim); | |
257 | if (length != 0) | |
258 | cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) | |
259 | | (array[2] << 8) | array[3]); | |
260 | } else { | |
261 | /* Now we have IPv6 address. */ | |
262 | length = get_ipv6_addr(data + 3, dlen - 3, | |
263 | (struct in6_addr *)cmd->u3.ip6, delim); | |
264 | } | |
265 | ||
266 | if (length == 0) | |
267 | return 0; | |
0d53778e | 268 | pr_debug("EPRT: Got IP address!\n"); |
9fb9cbb1 YK |
269 | /* Start offset includes initial "|1|", and trailing delimiter */ |
270 | return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port); | |
271 | } | |
272 | ||
273 | /* Returns 0, or length of numbers: |||6446| */ | |
274 | static int try_epsv_response(const char *data, size_t dlen, | |
4e7dba99 JM |
275 | struct nf_conntrack_man *cmd, char term, |
276 | unsigned int *offset) | |
9fb9cbb1 YK |
277 | { |
278 | char delim; | |
279 | ||
280 | /* Three delimiters. */ | |
281 | if (dlen <= 3) return 0; | |
282 | delim = data[0]; | |
f64f9e71 JP |
283 | if (isdigit(delim) || delim < 33 || delim > 126 || |
284 | data[1] != delim || data[2] != delim) | |
9fb9cbb1 YK |
285 | return 0; |
286 | ||
287 | return get_port(data, 3, dlen, delim, &cmd->u.tcp.port); | |
288 | } | |
289 | ||
290 | /* Return 1 for match, 0 for accept, -1 for partial. */ | |
291 | static int find_pattern(const char *data, size_t dlen, | |
292 | const char *pattern, size_t plen, | |
293 | char skip, char term, | |
294 | unsigned int *numoff, | |
295 | unsigned int *numlen, | |
296 | struct nf_conntrack_man *cmd, | |
297 | int (*getnum)(const char *, size_t, | |
4e7dba99 JM |
298 | struct nf_conntrack_man *, char, |
299 | unsigned int *)) | |
9fb9cbb1 | 300 | { |
4e7dba99 | 301 | size_t i = plen; |
9fb9cbb1 | 302 | |
5b5e0928 | 303 | pr_debug("find_pattern `%s': dlen = %zu\n", pattern, dlen); |
9fb9cbb1 YK |
304 | |
305 | if (dlen <= plen) { | |
306 | /* Short packet: try for partial? */ | |
18082746 | 307 | if (strncasecmp(data, pattern, dlen) == 0) |
9fb9cbb1 YK |
308 | return -1; |
309 | else return 0; | |
310 | } | |
311 | ||
ddb075b0 | 312 | if (strncasecmp(data, pattern, plen) != 0) |
9fb9cbb1 | 313 | return 0; |
9fb9cbb1 | 314 | |
0d53778e | 315 | pr_debug("Pattern matches!\n"); |
9fb9cbb1 YK |
316 | /* Now we've found the constant string, try to skip |
317 | to the 'skip' character */ | |
4e7dba99 JM |
318 | if (skip) { |
319 | for (i = plen; data[i] != skip; i++) | |
320 | if (i == dlen - 1) return -1; | |
9fb9cbb1 | 321 | |
4e7dba99 JM |
322 | /* Skip over the last character */ |
323 | i++; | |
324 | } | |
9fb9cbb1 | 325 | |
0d53778e | 326 | pr_debug("Skipped up to `%c'!\n", skip); |
9fb9cbb1 YK |
327 | |
328 | *numoff = i; | |
4e7dba99 | 329 | *numlen = getnum(data + i, dlen - i, cmd, term, numoff); |
9fb9cbb1 YK |
330 | if (!*numlen) |
331 | return -1; | |
332 | ||
0d53778e | 333 | pr_debug("Match succeeded!\n"); |
9fb9cbb1 YK |
334 | return 1; |
335 | } | |
336 | ||
337 | /* Look up to see if we're just after a \n. */ | |
55a73324 | 338 | static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir) |
9fb9cbb1 YK |
339 | { |
340 | unsigned int i; | |
341 | ||
342 | for (i = 0; i < info->seq_aft_nl_num[dir]; i++) | |
343 | if (info->seq_aft_nl[dir][i] == seq) | |
344 | return 1; | |
345 | return 0; | |
346 | } | |
347 | ||
348 | /* We don't update if it's older than what we have. */ | |
a71996fc AD |
349 | static void update_nl_seq(struct nf_conn *ct, u32 nl_seq, |
350 | struct nf_ct_ftp_master *info, int dir, | |
9fb9cbb1 YK |
351 | struct sk_buff *skb) |
352 | { | |
aaff23a9 | 353 | unsigned int i, oldest; |
9fb9cbb1 YK |
354 | |
355 | /* Look for oldest: if we find exact match, we're done. */ | |
356 | for (i = 0; i < info->seq_aft_nl_num[dir]; i++) { | |
357 | if (info->seq_aft_nl[dir][i] == nl_seq) | |
358 | return; | |
9fb9cbb1 YK |
359 | } |
360 | ||
361 | if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) { | |
362 | info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq; | |
aaff23a9 PM |
363 | } else { |
364 | if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1])) | |
365 | oldest = 0; | |
366 | else | |
367 | oldest = 1; | |
368 | ||
369 | if (after(nl_seq, info->seq_aft_nl[dir][oldest])) | |
370 | info->seq_aft_nl[dir][oldest] = nl_seq; | |
9fb9cbb1 YK |
371 | } |
372 | } | |
373 | ||
3db05fea | 374 | static int help(struct sk_buff *skb, |
9fb9cbb1 YK |
375 | unsigned int protoff, |
376 | struct nf_conn *ct, | |
377 | enum ip_conntrack_info ctinfo) | |
378 | { | |
379 | unsigned int dataoff, datalen; | |
58c0fb0d JE |
380 | const struct tcphdr *th; |
381 | struct tcphdr _tcph; | |
382 | const char *fb_ptr; | |
9fb9cbb1 YK |
383 | int ret; |
384 | u32 seq; | |
385 | int dir = CTINFO2DIR(ctinfo); | |
d6e8cc6c | 386 | unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff); |
1afc5679 | 387 | struct nf_ct_ftp_master *ct_ftp_info = nfct_help_data(ct); |
9fb9cbb1 | 388 | struct nf_conntrack_expect *exp; |
643a2c15 | 389 | union nf_inet_addr *daddr; |
9fb9cbb1 | 390 | struct nf_conntrack_man cmd = {}; |
9fb9cbb1 YK |
391 | unsigned int i; |
392 | int found = 0, ends_in_nl; | |
337fbc41 | 393 | typeof(nf_nat_ftp_hook) nf_nat_ftp; |
9fb9cbb1 YK |
394 | |
395 | /* Until there's been traffic both ways, don't look in packets. */ | |
f64f9e71 | 396 | if (ctinfo != IP_CT_ESTABLISHED && |
fb048833 | 397 | ctinfo != IP_CT_ESTABLISHED_REPLY) { |
0d53778e | 398 | pr_debug("ftp: Conntrackinfo = %u\n", ctinfo); |
9fb9cbb1 YK |
399 | return NF_ACCEPT; |
400 | } | |
401 | ||
3db05fea | 402 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
9fb9cbb1 YK |
403 | if (th == NULL) |
404 | return NF_ACCEPT; | |
405 | ||
406 | dataoff = protoff + th->doff * 4; | |
407 | /* No data? */ | |
3db05fea | 408 | if (dataoff >= skb->len) { |
0d53778e | 409 | pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff, |
3db05fea | 410 | skb->len); |
9fb9cbb1 YK |
411 | return NF_ACCEPT; |
412 | } | |
3db05fea | 413 | datalen = skb->len - dataoff; |
9fb9cbb1 YK |
414 | |
415 | spin_lock_bh(&nf_ftp_lock); | |
3db05fea | 416 | fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer); |
9fb9cbb1 YK |
417 | BUG_ON(fb_ptr == NULL); |
418 | ||
419 | ends_in_nl = (fb_ptr[datalen - 1] == '\n'); | |
420 | seq = ntohl(th->seq) + datalen; | |
421 | ||
422 | /* Look up to see if we're just after a \n. */ | |
423 | if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) { | |
7be54ca4 PNA |
424 | /* We're picking up this, clear flags and let it continue */ |
425 | if (unlikely(ct_ftp_info->flags[dir] & NF_CT_FTP_SEQ_PICKUP)) { | |
426 | ct_ftp_info->flags[dir] ^= NF_CT_FTP_SEQ_PICKUP; | |
427 | goto skip_nl_seq; | |
428 | } | |
429 | ||
9fb9cbb1 | 430 | /* Now if this ends in \n, update ftp info. */ |
0d53778e PM |
431 | pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n", |
432 | ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)", | |
433 | ct_ftp_info->seq_aft_nl[dir][0], | |
434 | ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)", | |
435 | ct_ftp_info->seq_aft_nl[dir][1]); | |
9fb9cbb1 YK |
436 | ret = NF_ACCEPT; |
437 | goto out_update_nl; | |
438 | } | |
439 | ||
7be54ca4 | 440 | skip_nl_seq: |
601e68e1 YH |
441 | /* Initialize IP/IPv6 addr to expected address (it's not mentioned |
442 | in EPSV responses) */ | |
5e8fbe2a | 443 | cmd.l3num = nf_ct_l3num(ct); |
9fb9cbb1 YK |
444 | memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all, |
445 | sizeof(cmd.u3.all)); | |
446 | ||
7d8c5018 | 447 | for (i = 0; i < ARRAY_SIZE(search[dir]); i++) { |
9fb9cbb1 | 448 | found = find_pattern(fb_ptr, datalen, |
7d8c5018 PM |
449 | search[dir][i].pattern, |
450 | search[dir][i].plen, | |
451 | search[dir][i].skip, | |
452 | search[dir][i].term, | |
9fb9cbb1 YK |
453 | &matchoff, &matchlen, |
454 | &cmd, | |
7d8c5018 | 455 | search[dir][i].getnum); |
9fb9cbb1 YK |
456 | if (found) break; |
457 | } | |
458 | if (found == -1) { | |
459 | /* We don't usually drop packets. After all, this is | |
460 | connection tracking, not packet filtering. | |
461 | However, it is necessary for accurate tracking in | |
462 | this case. */ | |
b20ab9cc PNA |
463 | nf_ct_helper_log(skb, ct, "partial matching of `%s'", |
464 | search[dir][i].pattern); | |
9fb9cbb1 YK |
465 | ret = NF_DROP; |
466 | goto out; | |
467 | } else if (found == 0) { /* No match */ | |
468 | ret = NF_ACCEPT; | |
469 | goto out_update_nl; | |
470 | } | |
471 | ||
0d53778e PM |
472 | pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n", |
473 | matchlen, fb_ptr + matchoff, | |
474 | matchlen, ntohl(th->seq) + matchoff); | |
9fb9cbb1 | 475 | |
6823645d | 476 | exp = nf_ct_expect_alloc(ct); |
9fb9cbb1 | 477 | if (exp == NULL) { |
b20ab9cc | 478 | nf_ct_helper_log(skb, ct, "cannot alloc expectation"); |
9fb9cbb1 YK |
479 | ret = NF_DROP; |
480 | goto out; | |
481 | } | |
482 | ||
483 | /* We refer to the reverse direction ("!dir") tuples here, | |
484 | * because we're expecting something in the other direction. | |
485 | * Doesn't matter unless NAT is happening. */ | |
df43b4e7 | 486 | daddr = &ct->tuplehash[!dir].tuple.dst.u3; |
9fb9cbb1 YK |
487 | |
488 | /* Update the ftp info */ | |
5e8fbe2a | 489 | if ((cmd.l3num == nf_ct_l3num(ct)) && |
9fb9cbb1 YK |
490 | memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all, |
491 | sizeof(cmd.u3.all))) { | |
492 | /* Enrico Scholz's passive FTP to partially RNAT'd ftp | |
601e68e1 YH |
493 | server: it really wants us to connect to a |
494 | different IP address. Simply don't record it for | |
495 | NAT. */ | |
9fb9cbb1 | 496 | if (cmd.l3num == PF_INET) { |
ad6d9503 | 497 | pr_debug("NOT RECORDING: %pI4 != %pI4\n", |
14d5e834 HH |
498 | &cmd.u3.ip, |
499 | &ct->tuplehash[dir].tuple.src.u3.ip); | |
9fb9cbb1 | 500 | } else { |
ad6d9503 | 501 | pr_debug("NOT RECORDING: %pI6 != %pI6\n", |
38ff4fa4 HH |
502 | cmd.u3.ip6, |
503 | ct->tuplehash[dir].tuple.src.u3.ip6); | |
9fb9cbb1 YK |
504 | } |
505 | ||
506 | /* Thanks to Cristiano Lincoln Mattos | |
507 | <[email protected]> for reporting this potential | |
508 | problem (DMZ machines opening holes to internal | |
509 | networks, or the packet filter itself). */ | |
510 | if (!loose) { | |
511 | ret = NF_ACCEPT; | |
512 | goto out_put_expect; | |
513 | } | |
df43b4e7 | 514 | daddr = &cmd.u3; |
9fb9cbb1 YK |
515 | } |
516 | ||
6002f266 | 517 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num, |
df43b4e7 PM |
518 | &ct->tuplehash[!dir].tuple.src.u3, daddr, |
519 | IPPROTO_TCP, NULL, &cmd.u.tcp.port); | |
9fb9cbb1 YK |
520 | |
521 | /* Now, NAT might want to mangle the packet, and register the | |
522 | * (possibly changed) expectation itself. */ | |
337fbc41 | 523 | nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook); |
d33cbeeb | 524 | if (nf_nat_ftp && ct->status & IPS_NAT_MASK) |
3db05fea | 525 | ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype, |
051966c0 | 526 | protoff, matchoff, matchlen, exp); |
9fb9cbb1 YK |
527 | else { |
528 | /* Can't expect this? Best to drop packet now. */ | |
b20ab9cc PNA |
529 | if (nf_ct_expect_related(exp) != 0) { |
530 | nf_ct_helper_log(skb, ct, "cannot add expectation"); | |
9fb9cbb1 | 531 | ret = NF_DROP; |
b20ab9cc | 532 | } else |
9fb9cbb1 YK |
533 | ret = NF_ACCEPT; |
534 | } | |
535 | ||
536 | out_put_expect: | |
6823645d | 537 | nf_ct_expect_put(exp); |
9fb9cbb1 YK |
538 | |
539 | out_update_nl: | |
540 | /* Now if this ends in \n, update ftp info. Seq may have been | |
541 | * adjusted by NAT code. */ | |
542 | if (ends_in_nl) | |
a71996fc | 543 | update_nl_seq(ct, seq, ct_ftp_info, dir, skb); |
9fb9cbb1 YK |
544 | out: |
545 | spin_unlock_bh(&nf_ftp_lock); | |
546 | return ret; | |
547 | } | |
548 | ||
7be54ca4 PNA |
549 | static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct) |
550 | { | |
551 | struct nf_ct_ftp_master *ftp = nfct_help_data(ct); | |
552 | ||
553 | /* This conntrack has been injected from user-space, always pick up | |
554 | * sequence tracking. Otherwise, the first FTP command after the | |
555 | * failover breaks. | |
556 | */ | |
557 | ftp->flags[IP_CT_DIR_ORIGINAL] |= NF_CT_FTP_SEQ_PICKUP; | |
558 | ftp->flags[IP_CT_DIR_REPLY] |= NF_CT_FTP_SEQ_PICKUP; | |
559 | return 0; | |
560 | } | |
561 | ||
82de0be6 | 562 | static struct nf_conntrack_helper ftp[MAX_PORTS * 2] __read_mostly; |
9fb9cbb1 | 563 | |
6002f266 PM |
564 | static const struct nf_conntrack_expect_policy ftp_exp_policy = { |
565 | .max_expected = 1, | |
566 | .timeout = 5 * 60, | |
567 | }; | |
568 | ||
9fb9cbb1 | 569 | /* don't make this __exit, since it's called from __init ! */ |
65b4b4e8 | 570 | static void nf_conntrack_ftp_fini(void) |
9fb9cbb1 | 571 | { |
82de0be6 | 572 | nf_conntrack_helpers_unregister(ftp, ports_c * 2); |
9fb9cbb1 YK |
573 | kfree(ftp_buffer); |
574 | } | |
575 | ||
65b4b4e8 | 576 | static int __init nf_conntrack_ftp_init(void) |
9fb9cbb1 | 577 | { |
82de0be6 | 578 | int i, ret = 0; |
9fb9cbb1 | 579 | |
dcf67740 FW |
580 | NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_ftp_master)); |
581 | ||
9fb9cbb1 YK |
582 | ftp_buffer = kmalloc(65536, GFP_KERNEL); |
583 | if (!ftp_buffer) | |
584 | return -ENOMEM; | |
585 | ||
586 | if (ports_c == 0) | |
587 | ports[ports_c++] = FTP_PORT; | |
588 | ||
589 | /* FIXME should be configurable whether IPv4 and IPv6 FTP connections | |
590 | are tracked or not - YK */ | |
591 | for (i = 0; i < ports_c; i++) { | |
82de0be6 GF |
592 | nf_ct_helper_init(&ftp[2 * i], AF_INET, IPPROTO_TCP, "ftp", |
593 | FTP_PORT, ports[i], ports[i], &ftp_exp_policy, | |
9f0f3ebe | 594 | 0, help, nf_ct_ftp_from_nlattr, THIS_MODULE); |
82de0be6 GF |
595 | nf_ct_helper_init(&ftp[2 * i + 1], AF_INET6, IPPROTO_TCP, "ftp", |
596 | FTP_PORT, ports[i], ports[i], &ftp_exp_policy, | |
9f0f3ebe | 597 | 0, help, nf_ct_ftp_from_nlattr, THIS_MODULE); |
82de0be6 GF |
598 | } |
599 | ||
600 | ret = nf_conntrack_helpers_register(ftp, ports_c * 2); | |
601 | if (ret < 0) { | |
602 | pr_err("failed to register helpers\n"); | |
603 | kfree(ftp_buffer); | |
604 | return ret; | |
9fb9cbb1 YK |
605 | } |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
65b4b4e8 AM |
610 | module_init(nf_conntrack_ftp_init); |
611 | module_exit(nf_conntrack_ftp_fini); |