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>
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>
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);
41 static u_int16_t ports[MAX_PORTS];
42 static unsigned int ports_c;
43 module_param_array(ports, ushort, &ports_c, 0400);
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 *,
57 char, unsigned int *);
58 static int try_rfc1123(const char *, size_t, struct nf_conntrack_man *,
59 char, unsigned int *);
60 static int try_eprt(const char *, size_t, struct nf_conntrack_man *,
61 char, unsigned int *);
62 static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *,
63 char, unsigned int *);
65 static struct ftp_search {
70 enum nf_ct_ftp_type ftptype;
71 int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char, unsigned int *);
72 } search[IP_CT_DIR_MAX][2] = {
73 [IP_CT_DIR_ORIGINAL] = {
76 .plen = sizeof("PORT") - 1,
79 .ftptype = NF_CT_FTP_PORT,
84 .plen = sizeof("EPRT") - 1,
87 .ftptype = NF_CT_FTP_EPRT,
94 .plen = sizeof("227 ") - 1,
95 .ftptype = NF_CT_FTP_PASV,
96 .getnum = try_rfc1123,
100 .plen = sizeof("229 ") - 1,
103 .ftptype = NF_CT_FTP_EPSV,
104 .getnum = try_epsv_response,
110 get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term)
113 int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end);
115 return (int)(end - src);
119 static int try_number(const char *data, size_t dlen, u_int32_t array[],
120 int array_size, char sep, char term)
124 memset(array, 0, sizeof(array[0])*array_size);
126 /* Keep data pointing at next char. */
127 for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
128 if (*data >= '0' && *data <= '9') {
129 array[i] = array[i]*10 + *data - '0';
131 else if (*data == sep)
134 /* Unexpected character; true if it's the
135 terminator (or we don't care about one)
136 and we're finished. */
137 if ((*data == term || !term) && i == array_size - 1)
140 pr_debug("Char %u (got %u nums) `%u' unexpected\n",
145 pr_debug("Failed to fill %u numbers separated by %c\n",
150 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
151 static int try_rfc959(const char *data, size_t dlen,
152 struct nf_conntrack_man *cmd, char term,
153 unsigned int *offset)
158 length = try_number(data, dlen, array, 6, ',', term);
162 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
163 (array[2] << 8) | array[3]);
164 cmd->u.tcp.port = htons((array[4] << 8) | array[5]);
170 * The format of the 227 reply to a PASV command is not
171 * well standardized. In particular, an FTP client cannot
172 * assume that the parentheses shown on page 40 of RFC-959
173 * will be present (and in fact, Figure 3 on page 43 omits
174 * them). Therefore, a User-FTP program that interprets
175 * the PASV reply must scan the reply for the first digit
176 * of the host and port numbers.
178 static int try_rfc1123(const char *data, size_t dlen,
179 struct nf_conntrack_man *cmd, char term,
180 unsigned int *offset)
183 for (i = 0; i < dlen; i++)
184 if (isdigit(data[i]))
192 return try_rfc959(data + i, dlen - i, cmd, 0, offset);
195 /* Grab port: number up to delimiter */
196 static int get_port(const char *data, int start, size_t dlen, char delim,
199 u_int16_t tmp_port = 0;
202 for (i = start; i < dlen; i++) {
204 if (data[i] == delim) {
207 *port = htons(tmp_port);
208 pr_debug("get_port: return %d\n", tmp_port);
211 else if (data[i] >= '0' && data[i] <= '9')
212 tmp_port = tmp_port*10 + data[i] - '0';
213 else { /* Some other crap */
214 pr_debug("get_port: invalid char.\n");
221 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
222 static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd,
223 char term, unsigned int *offset)
228 /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
229 then delimiter again. */
231 pr_debug("EPRT: too short\n");
235 if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) {
236 pr_debug("try_eprt: invalid delimitter.\n");
240 if ((cmd->l3num == PF_INET && data[1] != '1') ||
241 (cmd->l3num == PF_INET6 && data[1] != '2')) {
242 pr_debug("EPRT: invalid protocol number.\n");
246 pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim);
248 if (data[1] == '1') {
251 /* Now we have IP address. */
252 length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
254 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16)
255 | (array[2] << 8) | array[3]);
257 /* Now we have IPv6 address. */
258 length = get_ipv6_addr(data + 3, dlen - 3,
259 (struct in6_addr *)cmd->u3.ip6, delim);
264 pr_debug("EPRT: Got IP address!\n");
265 /* Start offset includes initial "|1|", and trailing delimiter */
266 return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port);
269 /* Returns 0, or length of numbers: |||6446| */
270 static int try_epsv_response(const char *data, size_t dlen,
271 struct nf_conntrack_man *cmd, char term,
272 unsigned int *offset)
276 /* Three delimiters. */
277 if (dlen <= 3) return 0;
279 if (isdigit(delim) || delim < 33 || delim > 126 ||
280 data[1] != delim || data[2] != delim)
283 return get_port(data, 3, dlen, delim, &cmd->u.tcp.port);
286 /* Return 1 for match, 0 for accept, -1 for partial. */
287 static int find_pattern(const char *data, size_t dlen,
288 const char *pattern, size_t plen,
289 char skip, char term,
290 unsigned int *numoff,
291 unsigned int *numlen,
292 struct nf_conntrack_man *cmd,
293 int (*getnum)(const char *, size_t,
294 struct nf_conntrack_man *, char,
299 pr_debug("find_pattern `%s': dlen = %Zu\n", pattern, dlen);
304 /* Short packet: try for partial? */
305 if (strnicmp(data, pattern, dlen) == 0)
310 if (strnicmp(data, pattern, plen) != 0) {
314 pr_debug("ftp: string mismatch\n");
315 for (i = 0; i < plen; i++) {
316 pr_debug("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
318 pattern[i], pattern[i]);
324 pr_debug("Pattern matches!\n");
325 /* Now we've found the constant string, try to skip
326 to the 'skip' character */
328 for (i = plen; data[i] != skip; i++)
329 if (i == dlen - 1) return -1;
331 /* Skip over the last character */
335 pr_debug("Skipped up to `%c'!\n", skip);
338 *numlen = getnum(data + i, dlen - i, cmd, term, numoff);
342 pr_debug("Match succeeded!\n");
346 /* Look up to see if we're just after a \n. */
347 static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir)
351 for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
352 if (info->seq_aft_nl[dir][i] == seq)
357 /* We don't update if it's older than what we have. */
358 static void update_nl_seq(struct nf_conn *ct, u32 nl_seq,
359 struct nf_ct_ftp_master *info, int dir,
362 unsigned int i, oldest;
364 /* Look for oldest: if we find exact match, we're done. */
365 for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
366 if (info->seq_aft_nl[dir][i] == nl_seq)
370 if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
371 info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
373 if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1]))
378 if (after(nl_seq, info->seq_aft_nl[dir][oldest]))
379 info->seq_aft_nl[dir][oldest] = nl_seq;
383 static int help(struct sk_buff *skb,
384 unsigned int protoff,
386 enum ip_conntrack_info ctinfo)
388 unsigned int dataoff, datalen;
389 const struct tcphdr *th;
394 int dir = CTINFO2DIR(ctinfo);
395 unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff);
396 struct nf_ct_ftp_master *ct_ftp_info = &nfct_help(ct)->help.ct_ftp_info;
397 struct nf_conntrack_expect *exp;
398 union nf_inet_addr *daddr;
399 struct nf_conntrack_man cmd = {};
401 int found = 0, ends_in_nl;
402 typeof(nf_nat_ftp_hook) nf_nat_ftp;
404 /* Until there's been traffic both ways, don't look in packets. */
405 if (ctinfo != IP_CT_ESTABLISHED &&
406 ctinfo != IP_CT_ESTABLISHED_REPLY) {
407 pr_debug("ftp: Conntrackinfo = %u\n", ctinfo);
411 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
415 dataoff = protoff + th->doff * 4;
417 if (dataoff >= skb->len) {
418 pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff,
422 datalen = skb->len - dataoff;
424 spin_lock_bh(&nf_ftp_lock);
425 fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer);
426 BUG_ON(fb_ptr == NULL);
428 ends_in_nl = (fb_ptr[datalen - 1] == '\n');
429 seq = ntohl(th->seq) + datalen;
431 /* Look up to see if we're just after a \n. */
432 if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
433 /* Now if this ends in \n, update ftp info. */
434 pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
435 ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
436 ct_ftp_info->seq_aft_nl[dir][0],
437 ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)",
438 ct_ftp_info->seq_aft_nl[dir][1]);
443 /* Initialize IP/IPv6 addr to expected address (it's not mentioned
444 in EPSV responses) */
445 cmd.l3num = nf_ct_l3num(ct);
446 memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
449 for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
450 found = find_pattern(fb_ptr, datalen,
451 search[dir][i].pattern,
455 &matchoff, &matchlen,
457 search[dir][i].getnum);
461 /* We don't usually drop packets. After all, this is
462 connection tracking, not packet filtering.
463 However, it is necessary for accurate tracking in
465 pr_debug("conntrack_ftp: partial %s %u+%u\n",
466 search[dir][i].pattern, ntohl(th->seq), datalen);
469 } else if (found == 0) { /* No match */
474 pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
475 matchlen, fb_ptr + matchoff,
476 matchlen, ntohl(th->seq) + matchoff);
478 exp = nf_ct_expect_alloc(ct);
484 /* We refer to the reverse direction ("!dir") tuples here,
485 * because we're expecting something in the other direction.
486 * Doesn't matter unless NAT is happening. */
487 daddr = &ct->tuplehash[!dir].tuple.dst.u3;
489 /* Update the ftp info */
490 if ((cmd.l3num == nf_ct_l3num(ct)) &&
491 memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
492 sizeof(cmd.u3.all))) {
493 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
494 server: it really wants us to connect to a
495 different IP address. Simply don't record it for
497 if (cmd.l3num == PF_INET) {
498 pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n",
500 &ct->tuplehash[dir].tuple.src.u3.ip);
502 pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n",
504 ct->tuplehash[dir].tuple.src.u3.ip6);
507 /* Thanks to Cristiano Lincoln Mattos
508 <lincoln@cesar.org.br> for reporting this potential
509 problem (DMZ machines opening holes to internal
510 networks, or the packet filter itself). */
518 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num,
519 &ct->tuplehash[!dir].tuple.src.u3, daddr,
520 IPPROTO_TCP, NULL, &cmd.u.tcp.port);
522 /* Now, NAT might want to mangle the packet, and register the
523 * (possibly changed) expectation itself. */
524 nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook);
525 if (nf_nat_ftp && ct->status & IPS_NAT_MASK)
526 ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype,
527 matchoff, matchlen, exp);
529 /* Can't expect this? Best to drop packet now. */
530 if (nf_ct_expect_related(exp) != 0)
537 nf_ct_expect_put(exp);
540 /* Now if this ends in \n, update ftp info. Seq may have been
541 * adjusted by NAT code. */
543 update_nl_seq(ct, seq, ct_ftp_info, dir, skb);
545 spin_unlock_bh(&nf_ftp_lock);
549 static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly;
550 static char ftp_names[MAX_PORTS][2][sizeof("ftp-65535")] __read_mostly;
552 static const struct nf_conntrack_expect_policy ftp_exp_policy = {
557 /* don't make this __exit, since it's called from __init ! */
558 static void nf_conntrack_ftp_fini(void)
561 for (i = 0; i < ports_c; i++) {
562 for (j = 0; j < 2; j++) {
563 if (ftp[i][j].me == NULL)
566 pr_debug("nf_ct_ftp: unregistering helper for pf: %d "
568 ftp[i][j].tuple.src.l3num, ports[i]);
569 nf_conntrack_helper_unregister(&ftp[i][j]);
576 static int __init nf_conntrack_ftp_init(void)
578 int i, j = -1, ret = 0;
581 ftp_buffer = kmalloc(65536, GFP_KERNEL);
586 ports[ports_c++] = FTP_PORT;
588 /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
589 are tracked or not - YK */
590 for (i = 0; i < ports_c; i++) {
591 ftp[i][0].tuple.src.l3num = PF_INET;
592 ftp[i][1].tuple.src.l3num = PF_INET6;
593 for (j = 0; j < 2; j++) {
594 ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]);
595 ftp[i][j].tuple.dst.protonum = IPPROTO_TCP;
596 ftp[i][j].expect_policy = &ftp_exp_policy;
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");
603 sprintf(tmpname, "ftp-%d", ports[i]);
604 ftp[i][j].name = tmpname;
606 pr_debug("nf_ct_ftp: registering helper for pf: %d "
608 ftp[i][j].tuple.src.l3num, ports[i]);
609 ret = nf_conntrack_helper_register(&ftp[i][j]);
611 printk(KERN_ERR "nf_ct_ftp: failed to register"
612 " helper for pf: %d port: %d\n",
613 ftp[i][j].tuple.src.l3num, ports[i]);
614 nf_conntrack_ftp_fini();
623 module_init(nf_conntrack_ftp_init);
624 module_exit(nf_conntrack_ftp_fini);