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