GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / netfilter / nf_conntrack_ftp.c
blobcd92700112864f638930fe7f3a54ea8fde2e52fa
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/slab.h>
17 #include <linux/ipv6.h>
18 #include <linux/ctype.h>
19 #include <linux/inet.h>
20 #include <net/checksum.h>
21 #include <net/tcp.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_expect.h>
25 #include <net/netfilter/nf_conntrack_ecache.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <linux/netfilter/nf_conntrack_ftp.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
31 MODULE_DESCRIPTION("ftp connection tracking helper");
32 MODULE_ALIAS("ip_conntrack_ftp");
33 MODULE_ALIAS_NFCT_HELPER("ftp");
35 /* This is slow, but it's simple. --RR */
36 static char *ftp_buffer;
38 static DEFINE_SPINLOCK(nf_ftp_lock);
40 #define MAX_PORTS 8
41 static u_int16_t ports[MAX_PORTS];
42 static unsigned int ports_c;
43 module_param_array(ports, ushort, &ports_c, 0400);
45 static int loose;
46 module_param(loose, bool, 0600);
48 unsigned int (*nf_nat_ftp_hook)(struct sk_buff *skb,
49 enum ip_conntrack_info ctinfo,
50 enum nf_ct_ftp_type type,
51 unsigned int matchoff,
52 unsigned int matchlen,
53 struct nf_conntrack_expect *exp);
54 EXPORT_SYMBOL_GPL(nf_nat_ftp_hook);
56 static int try_rfc959(const char *, size_t, struct nf_conntrack_man *, char);
57 static int try_eprt(const char *, size_t, struct nf_conntrack_man *, char);
58 static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *,
59 char);
61 static struct ftp_search {
62 const char *pattern;
63 size_t plen;
64 char skip;
65 char term;
66 enum nf_ct_ftp_type ftptype;
67 int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char);
68 } search[IP_CT_DIR_MAX][2] = {
69 [IP_CT_DIR_ORIGINAL] = {
71 .pattern = "PORT",
72 .plen = sizeof("PORT") - 1,
73 .skip = ' ',
74 .term = '\r',
75 .ftptype = NF_CT_FTP_PORT,
76 .getnum = try_rfc959,
79 .pattern = "EPRT",
80 .plen = sizeof("EPRT") - 1,
81 .skip = ' ',
82 .term = '\r',
83 .ftptype = NF_CT_FTP_EPRT,
84 .getnum = try_eprt,
87 [IP_CT_DIR_REPLY] = {
89 .pattern = "227 ",
90 .plen = sizeof("227 ") - 1,
91 .skip = '(',
92 .term = ')',
93 .ftptype = NF_CT_FTP_PASV,
94 .getnum = try_rfc959,
97 .pattern = "229 ",
98 .plen = sizeof("229 ") - 1,
99 .skip = '(',
100 .term = ')',
101 .ftptype = NF_CT_FTP_EPSV,
102 .getnum = try_epsv_response,
107 static int
108 get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term)
110 const char *end;
111 int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end);
112 if (ret > 0)
113 return (int)(end - src);
114 return 0;
117 static int try_number(const char *data, size_t dlen, u_int32_t array[],
118 int array_size, char sep, char term)
120 u_int32_t i, len;
122 memset(array, 0, sizeof(array[0])*array_size);
124 /* Keep data pointing at next char. */
125 for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
126 if (*data >= '0' && *data <= '9') {
127 array[i] = array[i]*10 + *data - '0';
129 else if (*data == sep)
130 i++;
131 else {
132 /* Unexpected character; true if it's the
133 terminator and we're finished. */
134 if (*data == term && i == array_size - 1)
135 return len;
137 pr_debug("Char %u (got %u nums) `%u' unexpected\n",
138 len, i, *data);
139 return 0;
142 pr_debug("Failed to fill %u numbers separated by %c\n",
143 array_size, sep);
144 return 0;
147 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
148 static int try_rfc959(const char *data, size_t dlen,
149 struct nf_conntrack_man *cmd, char term)
151 int length;
152 u_int32_t array[6];
154 length = try_number(data, dlen, array, 6, ',', term);
155 if (length == 0)
156 return 0;
158 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
159 (array[2] << 8) | array[3]);
160 cmd->u.tcp.port = htons((array[4] << 8) | array[5]);
161 return length;
164 /* Grab port: number up to delimiter */
165 static int get_port(const char *data, int start, size_t dlen, char delim,
166 __be16 *port)
168 u_int16_t tmp_port = 0;
169 int i;
171 for (i = start; i < dlen; i++) {
172 /* Finished? */
173 if (data[i] == delim) {
174 if (tmp_port == 0)
175 break;
176 *port = htons(tmp_port);
177 pr_debug("get_port: return %d\n", tmp_port);
178 return i + 1;
180 else if (data[i] >= '0' && data[i] <= '9')
181 tmp_port = tmp_port*10 + data[i] - '0';
182 else { /* Some other crap */
183 pr_debug("get_port: invalid char.\n");
184 break;
187 return 0;
190 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
191 static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd,
192 char term)
194 char delim;
195 int length;
197 /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
198 then delimiter again. */
199 if (dlen <= 3) {
200 pr_debug("EPRT: too short\n");
201 return 0;
203 delim = data[0];
204 if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) {
205 pr_debug("try_eprt: invalid delimitter.\n");
206 return 0;
209 if ((cmd->l3num == PF_INET && data[1] != '1') ||
210 (cmd->l3num == PF_INET6 && data[1] != '2')) {
211 pr_debug("EPRT: invalid protocol number.\n");
212 return 0;
215 pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim);
217 if (data[1] == '1') {
218 u_int32_t array[4];
220 /* Now we have IP address. */
221 length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
222 if (length != 0)
223 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16)
224 | (array[2] << 8) | array[3]);
225 } else {
226 /* Now we have IPv6 address. */
227 length = get_ipv6_addr(data + 3, dlen - 3,
228 (struct in6_addr *)cmd->u3.ip6, delim);
231 if (length == 0)
232 return 0;
233 pr_debug("EPRT: Got IP address!\n");
234 /* Start offset includes initial "|1|", and trailing delimiter */
235 return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port);
238 /* Returns 0, or length of numbers: |||6446| */
239 static int try_epsv_response(const char *data, size_t dlen,
240 struct nf_conntrack_man *cmd, char term)
242 char delim;
244 /* Three delimiters. */
245 if (dlen <= 3) return 0;
246 delim = data[0];
247 if (isdigit(delim) || delim < 33 || delim > 126 ||
248 data[1] != delim || data[2] != delim)
249 return 0;
251 return get_port(data, 3, dlen, delim, &cmd->u.tcp.port);
254 /* Return 1 for match, 0 for accept, -1 for partial. */
255 static int find_pattern(const char *data, size_t dlen,
256 const char *pattern, size_t plen,
257 char skip, char term,
258 unsigned int *numoff,
259 unsigned int *numlen,
260 struct nf_conntrack_man *cmd,
261 int (*getnum)(const char *, size_t,
262 struct nf_conntrack_man *, char))
264 size_t i;
266 pr_debug("find_pattern `%s': dlen = %Zu\n", pattern, dlen);
267 if (dlen == 0)
268 return 0;
270 if (dlen <= plen) {
271 /* Short packet: try for partial? */
272 if (strnicmp(data, pattern, dlen) == 0)
273 return -1;
274 else return 0;
277 if (strnicmp(data, pattern, plen) != 0) {
278 return 0;
281 pr_debug("Pattern matches!\n");
282 /* Now we've found the constant string, try to skip
283 to the 'skip' character */
284 for (i = plen; data[i] != skip; i++)
285 if (i == dlen - 1) return -1;
287 /* Skip over the last character */
288 i++;
290 pr_debug("Skipped up to `%c'!\n", skip);
292 *numoff = i;
293 *numlen = getnum(data + i, dlen - i, cmd, term);
294 if (!*numlen)
295 return -1;
297 pr_debug("Match succeeded!\n");
298 return 1;
301 /* Look up to see if we're just after a \n. */
302 static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir)
304 unsigned int i;
306 for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
307 if (info->seq_aft_nl[dir][i] == seq)
308 return 1;
309 return 0;
312 /* We don't update if it's older than what we have. */
313 static void update_nl_seq(struct nf_conn *ct, u32 nl_seq,
314 struct nf_ct_ftp_master *info, int dir,
315 struct sk_buff *skb)
317 unsigned int i, oldest;
319 /* Look for oldest: if we find exact match, we're done. */
320 for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
321 if (info->seq_aft_nl[dir][i] == nl_seq)
322 return;
325 if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
326 info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
327 } else {
328 if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1]))
329 oldest = 0;
330 else
331 oldest = 1;
333 if (after(nl_seq, info->seq_aft_nl[dir][oldest]))
334 info->seq_aft_nl[dir][oldest] = nl_seq;
338 static int help(struct sk_buff *skb,
339 unsigned int protoff,
340 struct nf_conn *ct,
341 enum ip_conntrack_info ctinfo)
343 unsigned int dataoff, datalen;
344 const struct tcphdr *th;
345 struct tcphdr _tcph;
346 const char *fb_ptr;
347 int ret;
348 u32 seq;
349 int dir = CTINFO2DIR(ctinfo);
350 unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff);
351 struct nf_ct_ftp_master *ct_ftp_info = &nfct_help(ct)->help.ct_ftp_info;
352 struct nf_conntrack_expect *exp;
353 union nf_inet_addr *daddr;
354 struct nf_conntrack_man cmd = {};
355 unsigned int i;
356 int found = 0, ends_in_nl;
357 typeof(nf_nat_ftp_hook) nf_nat_ftp;
359 /* Until there's been traffic both ways, don't look in packets. */
360 if (ctinfo != IP_CT_ESTABLISHED &&
361 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
362 pr_debug("ftp: Conntrackinfo = %u\n", ctinfo);
363 return NF_ACCEPT;
366 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
367 if (th == NULL)
368 return NF_ACCEPT;
370 dataoff = protoff + th->doff * 4;
371 /* No data? */
372 if (dataoff >= skb->len) {
373 pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff,
374 skb->len);
375 return NF_ACCEPT;
377 datalen = skb->len - dataoff;
379 spin_lock_bh(&nf_ftp_lock);
380 fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer);
381 BUG_ON(fb_ptr == NULL);
383 ends_in_nl = (fb_ptr[datalen - 1] == '\n');
384 seq = ntohl(th->seq) + datalen;
386 /* Look up to see if we're just after a \n. */
387 if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
388 /* Now if this ends in \n, update ftp info. */
389 pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
390 ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
391 ct_ftp_info->seq_aft_nl[dir][0],
392 ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)",
393 ct_ftp_info->seq_aft_nl[dir][1]);
394 ret = NF_ACCEPT;
395 goto out_update_nl;
398 /* Initialize IP/IPv6 addr to expected address (it's not mentioned
399 in EPSV responses) */
400 cmd.l3num = nf_ct_l3num(ct);
401 memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
402 sizeof(cmd.u3.all));
404 for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
405 found = find_pattern(fb_ptr, datalen,
406 search[dir][i].pattern,
407 search[dir][i].plen,
408 search[dir][i].skip,
409 search[dir][i].term,
410 &matchoff, &matchlen,
411 &cmd,
412 search[dir][i].getnum);
413 if (found) break;
415 if (found == -1) {
416 /* We don't usually drop packets. After all, this is
417 connection tracking, not packet filtering.
418 However, it is necessary for accurate tracking in
419 this case. */
420 pr_debug("conntrack_ftp: partial %s %u+%u\n",
421 search[dir][i].pattern, ntohl(th->seq), datalen);
422 ret = NF_DROP;
423 goto out;
424 } else if (found == 0) { /* No match */
425 ret = NF_ACCEPT;
426 goto out_update_nl;
429 pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
430 matchlen, fb_ptr + matchoff,
431 matchlen, ntohl(th->seq) + matchoff);
433 exp = nf_ct_expect_alloc(ct);
434 if (exp == NULL) {
435 ret = NF_DROP;
436 goto out;
439 /* We refer to the reverse direction ("!dir") tuples here,
440 * because we're expecting something in the other direction.
441 * Doesn't matter unless NAT is happening. */
442 daddr = &ct->tuplehash[!dir].tuple.dst.u3;
444 /* Update the ftp info */
445 if ((cmd.l3num == nf_ct_l3num(ct)) &&
446 memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
447 sizeof(cmd.u3.all))) {
448 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
449 server: it really wants us to connect to a
450 different IP address. Simply don't record it for
451 NAT. */
452 if (cmd.l3num == PF_INET) {
453 pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n",
454 &cmd.u3.ip,
455 &ct->tuplehash[dir].tuple.src.u3.ip);
456 } else {
457 pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n",
458 cmd.u3.ip6,
459 ct->tuplehash[dir].tuple.src.u3.ip6);
462 /* Thanks to Cristiano Lincoln Mattos
463 <lincoln@cesar.org.br> for reporting this potential
464 problem (DMZ machines opening holes to internal
465 networks, or the packet filter itself). */
466 if (!loose) {
467 ret = NF_ACCEPT;
468 goto out_put_expect;
470 daddr = &cmd.u3;
473 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num,
474 &ct->tuplehash[!dir].tuple.src.u3, daddr,
475 IPPROTO_TCP, NULL, &cmd.u.tcp.port);
477 /* Now, NAT might want to mangle the packet, and register the
478 * (possibly changed) expectation itself. */
479 nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook);
480 if (nf_nat_ftp && ct->status & IPS_NAT_MASK)
481 ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype,
482 matchoff, matchlen, exp);
483 else {
484 /* Can't expect this? Best to drop packet now. */
485 if (nf_ct_expect_related(exp) != 0)
486 ret = NF_DROP;
487 else
488 ret = NF_ACCEPT;
491 out_put_expect:
492 nf_ct_expect_put(exp);
494 out_update_nl:
495 /* Now if this ends in \n, update ftp info. Seq may have been
496 * adjusted by NAT code. */
497 if (ends_in_nl)
498 update_nl_seq(ct, seq, ct_ftp_info, dir, skb);
499 out:
500 spin_unlock_bh(&nf_ftp_lock);
501 return ret;
504 static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly;
505 static char ftp_names[MAX_PORTS][2][sizeof("ftp-65535")] __read_mostly;
507 static const struct nf_conntrack_expect_policy ftp_exp_policy = {
508 .max_expected = 1,
509 .timeout = 5 * 60,
512 /* don't make this __exit, since it's called from __init ! */
513 static void nf_conntrack_ftp_fini(void)
515 int i, j;
516 for (i = 0; i < ports_c; i++) {
517 for (j = 0; j < 2; j++) {
518 if (ftp[i][j].me == NULL)
519 continue;
521 pr_debug("nf_ct_ftp: unregistering helper for pf: %d "
522 "port: %d\n",
523 ftp[i][j].tuple.src.l3num, ports[i]);
524 nf_conntrack_helper_unregister(&ftp[i][j]);
528 kfree(ftp_buffer);
531 static int __init nf_conntrack_ftp_init(void)
533 int i, j = -1, ret = 0;
534 char *tmpname;
536 ftp_buffer = kmalloc(65536, GFP_KERNEL);
537 if (!ftp_buffer)
538 return -ENOMEM;
540 if (ports_c == 0)
541 ports[ports_c++] = FTP_PORT;
543 for (i = 0; i < ports_c; i++) {
544 ftp[i][0].tuple.src.l3num = PF_INET;
545 ftp[i][1].tuple.src.l3num = PF_INET6;
546 for (j = 0; j < 2; j++) {
547 ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]);
548 ftp[i][j].tuple.dst.protonum = IPPROTO_TCP;
549 ftp[i][j].expect_policy = &ftp_exp_policy;
550 ftp[i][j].me = THIS_MODULE;
551 ftp[i][j].help = help;
552 tmpname = &ftp_names[i][j][0];
553 if (ports[i] == FTP_PORT)
554 sprintf(tmpname, "ftp");
555 else
556 sprintf(tmpname, "ftp-%d", ports[i]);
557 ftp[i][j].name = tmpname;
559 pr_debug("nf_ct_ftp: registering helper for pf: %d "
560 "port: %d\n",
561 ftp[i][j].tuple.src.l3num, ports[i]);
562 ret = nf_conntrack_helper_register(&ftp[i][j]);
563 if (ret) {
564 printk(KERN_ERR "nf_ct_ftp: failed to register"
565 " helper for pf: %d port: %d\n",
566 ftp[i][j].tuple.src.l3num, ports[i]);
567 nf_conntrack_ftp_fini();
568 return ret;
573 return 0;
576 module_init(nf_conntrack_ftp_init);
577 module_exit(nf_conntrack_ftp_fini);