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