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.
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.
15 * Derived from net/ipv4/netfilter/ip_conntrack_ftp.c
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/netfilter.h>
22 #include <linux/ipv6.h>
23 #include <linux/ctype.h>
24 #include <net/checksum.h>
27 #include <net/netfilter/nf_conntrack.h>
28 #include <net/netfilter/nf_conntrack_helper.h>
29 #include <linux/netfilter/nf_conntrack_ftp.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
33 MODULE_DESCRIPTION("ftp connection tracking helper");
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
**pskb
,
49 enum ip_conntrack_info ctinfo
,
50 enum ip_ct_ftp_type type
,
51 unsigned int matchoff
,
52 unsigned int matchlen
,
53 struct nf_conntrack_expect
*exp
,
55 EXPORT_SYMBOL_GPL(nf_nat_ftp_hook
);
60 #define DEBUGP(format, args...)
63 static int try_rfc959(const char *, size_t, struct nf_conntrack_man
*, char);
64 static int try_eprt(const char *, size_t, struct nf_conntrack_man
*, char);
65 static int try_epsv_response(const char *, size_t, struct nf_conntrack_man
*,
68 static struct ftp_search
{
73 enum ip_ct_ftp_type ftptype
;
74 int (*getnum
)(const char *, size_t, struct nf_conntrack_man
*, char);
75 } search
[IP_CT_DIR_MAX
][2] = {
76 [IP_CT_DIR_ORIGINAL
] = {
79 .plen
= sizeof("PORT") - 1,
82 .ftptype
= IP_CT_FTP_PORT
,
87 .plen
= sizeof("EPRT") - 1,
90 .ftptype
= IP_CT_FTP_EPRT
,
97 .plen
= sizeof("227 ") - 1,
100 .ftptype
= IP_CT_FTP_PASV
,
101 .getnum
= try_rfc959
,
105 .plen
= sizeof("229 ") - 1,
108 .ftptype
= IP_CT_FTP_EPSV
,
109 .getnum
= try_epsv_response
,
114 /* This code is based on inet_pton() in glibc-2.2.4 */
116 get_ipv6_addr(const char *src
, size_t dlen
, struct in6_addr
*dst
, u_int8_t term
)
118 static const char xdigits
[] = "0123456789abcdef";
119 u_int8_t tmp
[16], *tp
, *endp
, *colonp
;
124 tp
= memset(tmp
, '\0', sizeof(tmp
));
125 endp
= tp
+ sizeof(tmp
);
128 /* Leading :: requires some special handling. */
131 DEBUGP("invalid \":\" at the head of addr\n");
139 while ((clen
< dlen
) && (*src
!= term
)) {
142 ch
= tolower(*src
++);
145 pch
= strchr(xdigits
, ch
);
148 val
|= (pch
- xdigits
);
156 DEBUGP("get_ipv6_addr: invalid char. \'%c\'\n", ch
);
162 DEBUGP("invalid location of \"::\".\n");
167 } else if (*src
== term
) {
168 DEBUGP("trancated IPv6 addr\n");
174 *tp
++ = (u_int8_t
) (val
>> 8) & 0xff;
175 *tp
++ = (u_int8_t
) val
& 0xff;
184 *tp
++ = (u_int8_t
) (val
>> 8) & 0xff;
185 *tp
++ = (u_int8_t
) val
& 0xff;
187 if (colonp
!= NULL
) {
189 * Since some memmove()'s erroneously fail to handle
190 * overlapping regions, we'll do the shift by hand.
192 const int n
= tp
- colonp
;
198 for (i
= 1; i
<= n
; i
++) {
199 endp
[- i
] = colonp
[n
- i
];
204 if (tp
!= endp
|| (*src
!= term
))
207 memcpy(dst
->s6_addr
, tmp
, sizeof(dst
->s6_addr
));
211 static int try_number(const char *data
, size_t dlen
, u_int32_t array
[],
212 int array_size
, char sep
, char term
)
216 memset(array
, 0, sizeof(array
[0])*array_size
);
218 /* Keep data pointing at next char. */
219 for (i
= 0, len
= 0; len
< dlen
&& i
< array_size
; len
++, data
++) {
220 if (*data
>= '0' && *data
<= '9') {
221 array
[i
] = array
[i
]*10 + *data
- '0';
223 else if (*data
== sep
)
226 /* Unexpected character; true if it's the
227 terminator and we're finished. */
228 if (*data
== term
&& i
== array_size
- 1)
231 DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
236 DEBUGP("Failed to fill %u numbers separated by %c\n", array_size
, sep
);
241 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
242 static int try_rfc959(const char *data
, size_t dlen
,
243 struct nf_conntrack_man
*cmd
, char term
)
248 length
= try_number(data
, dlen
, array
, 6, ',', term
);
252 cmd
->u3
.ip
= htonl((array
[0] << 24) | (array
[1] << 16) |
253 (array
[2] << 8) | array
[3]);
254 cmd
->u
.tcp
.port
= htons((array
[4] << 8) | array
[5]);
258 /* Grab port: number up to delimiter */
259 static int get_port(const char *data
, int start
, size_t dlen
, char delim
,
262 u_int16_t tmp_port
= 0;
265 for (i
= start
; i
< dlen
; i
++) {
267 if (data
[i
] == delim
) {
270 *port
= htons(tmp_port
);
271 DEBUGP("get_port: return %d\n", tmp_port
);
274 else if (data
[i
] >= '0' && data
[i
] <= '9')
275 tmp_port
= tmp_port
*10 + data
[i
] - '0';
276 else { /* Some other crap */
277 DEBUGP("get_port: invalid char.\n");
284 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
285 static int try_eprt(const char *data
, size_t dlen
, struct nf_conntrack_man
*cmd
,
291 /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
292 then delimiter again. */
294 DEBUGP("EPRT: too short\n");
298 if (isdigit(delim
) || delim
< 33 || delim
> 126 || data
[2] != delim
) {
299 DEBUGP("try_eprt: invalid delimitter.\n");
303 if ((cmd
->l3num
== PF_INET
&& data
[1] != '1') ||
304 (cmd
->l3num
== PF_INET6
&& data
[1] != '2')) {
305 DEBUGP("EPRT: invalid protocol number.\n");
309 DEBUGP("EPRT: Got %c%c%c\n", delim
, data
[1], delim
);
311 if (data
[1] == '1') {
314 /* Now we have IP address. */
315 length
= try_number(data
+ 3, dlen
- 3, array
, 4, '.', delim
);
317 cmd
->u3
.ip
= htonl((array
[0] << 24) | (array
[1] << 16)
318 | (array
[2] << 8) | array
[3]);
320 /* Now we have IPv6 address. */
321 length
= get_ipv6_addr(data
+ 3, dlen
- 3,
322 (struct in6_addr
*)cmd
->u3
.ip6
, delim
);
327 DEBUGP("EPRT: Got IP address!\n");
328 /* Start offset includes initial "|1|", and trailing delimiter */
329 return get_port(data
, 3 + length
+ 1, dlen
, delim
, &cmd
->u
.tcp
.port
);
332 /* Returns 0, or length of numbers: |||6446| */
333 static int try_epsv_response(const char *data
, size_t dlen
,
334 struct nf_conntrack_man
*cmd
, char term
)
338 /* Three delimiters. */
339 if (dlen
<= 3) return 0;
341 if (isdigit(delim
) || delim
< 33 || delim
> 126
342 || data
[1] != delim
|| data
[2] != delim
)
345 return get_port(data
, 3, dlen
, delim
, &cmd
->u
.tcp
.port
);
348 /* Return 1 for match, 0 for accept, -1 for partial. */
349 static int find_pattern(const char *data
, size_t dlen
,
350 const char *pattern
, size_t plen
,
351 char skip
, char term
,
352 unsigned int *numoff
,
353 unsigned int *numlen
,
354 struct nf_conntrack_man
*cmd
,
355 int (*getnum
)(const char *, size_t,
356 struct nf_conntrack_man
*, char))
360 DEBUGP("find_pattern `%s': dlen = %u\n", pattern
, dlen
);
365 /* Short packet: try for partial? */
366 if (strnicmp(data
, pattern
, dlen
) == 0)
371 if (strnicmp(data
, pattern
, plen
) != 0) {
375 DEBUGP("ftp: string mismatch\n");
376 for (i
= 0; i
< plen
; i
++) {
377 DEBUGP("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
379 pattern
[i
], pattern
[i
]);
385 DEBUGP("Pattern matches!\n");
386 /* Now we've found the constant string, try to skip
387 to the 'skip' character */
388 for (i
= plen
; data
[i
] != skip
; i
++)
389 if (i
== dlen
- 1) return -1;
391 /* Skip over the last character */
394 DEBUGP("Skipped up to `%c'!\n", skip
);
397 *numlen
= getnum(data
+ i
, dlen
- i
, cmd
, term
);
401 DEBUGP("Match succeeded!\n");
405 /* Look up to see if we're just after a \n. */
406 static int find_nl_seq(u32 seq
, const struct ip_ct_ftp_master
*info
, int dir
)
410 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++)
411 if (info
->seq_aft_nl
[dir
][i
] == seq
)
416 /* We don't update if it's older than what we have. */
417 static void update_nl_seq(u32 nl_seq
, struct ip_ct_ftp_master
*info
, int dir
,
420 unsigned int i
, oldest
= NUM_SEQ_TO_REMEMBER
;
422 /* Look for oldest: if we find exact match, we're done. */
423 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++) {
424 if (info
->seq_aft_nl
[dir
][i
] == nl_seq
)
427 if (oldest
== info
->seq_aft_nl_num
[dir
]
428 || before(info
->seq_aft_nl
[dir
][i
], oldest
))
432 if (info
->seq_aft_nl_num
[dir
] < NUM_SEQ_TO_REMEMBER
) {
433 info
->seq_aft_nl
[dir
][info
->seq_aft_nl_num
[dir
]++] = nl_seq
;
434 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE
, skb
);
435 } else if (oldest
!= NUM_SEQ_TO_REMEMBER
) {
436 info
->seq_aft_nl
[dir
][oldest
] = nl_seq
;
437 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE
, skb
);
441 static int help(struct sk_buff
**pskb
,
442 unsigned int protoff
,
444 enum ip_conntrack_info ctinfo
)
446 unsigned int dataoff
, datalen
;
447 struct tcphdr _tcph
, *th
;
451 int dir
= CTINFO2DIR(ctinfo
);
452 unsigned int matchlen
, matchoff
;
453 struct ip_ct_ftp_master
*ct_ftp_info
= &nfct_help(ct
)->help
.ct_ftp_info
;
454 struct nf_conntrack_expect
*exp
;
455 struct nf_conntrack_man cmd
= {};
458 int found
= 0, ends_in_nl
;
460 /* Until there's been traffic both ways, don't look in packets. */
461 if (ctinfo
!= IP_CT_ESTABLISHED
462 && ctinfo
!= IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
) {
463 DEBUGP("ftp: Conntrackinfo = %u\n", ctinfo
);
467 th
= skb_header_pointer(*pskb
, protoff
, sizeof(_tcph
), &_tcph
);
471 dataoff
= protoff
+ th
->doff
* 4;
473 if (dataoff
>= (*pskb
)->len
) {
474 DEBUGP("ftp: dataoff(%u) >= skblen(%u)\n", dataoff
,
478 datalen
= (*pskb
)->len
- dataoff
;
480 spin_lock_bh(&nf_ftp_lock
);
481 fb_ptr
= skb_header_pointer(*pskb
, dataoff
, datalen
, ftp_buffer
);
482 BUG_ON(fb_ptr
== NULL
);
484 ends_in_nl
= (fb_ptr
[datalen
- 1] == '\n');
485 seq
= ntohl(th
->seq
) + datalen
;
487 /* Look up to see if we're just after a \n. */
488 if (!find_nl_seq(ntohl(th
->seq
), ct_ftp_info
, dir
)) {
489 /* Now if this ends in \n, update ftp info. */
490 DEBUGP("nf_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
491 ct_ftp_info
->seq_aft_nl_num
[dir
] > 0 ? "" : "(UNSET)",
492 ct_ftp_info
->seq_aft_nl
[dir
][0],
493 ct_ftp_info
->seq_aft_nl_num
[dir
] > 1 ? "" : "(UNSET)",
494 ct_ftp_info
->seq_aft_nl
[dir
][1]);
499 /* Initialize IP/IPv6 addr to expected address (it's not mentioned
500 in EPSV responses) */
501 cmd
.l3num
= ct
->tuplehash
[dir
].tuple
.src
.l3num
;
502 memcpy(cmd
.u3
.all
, &ct
->tuplehash
[dir
].tuple
.src
.u3
.all
,
505 for (i
= 0; i
< ARRAY_SIZE(search
[dir
]); i
++) {
506 found
= find_pattern(fb_ptr
, datalen
,
507 search
[dir
][i
].pattern
,
511 &matchoff
, &matchlen
,
513 search
[dir
][i
].getnum
);
517 /* We don't usually drop packets. After all, this is
518 connection tracking, not packet filtering.
519 However, it is necessary for accurate tracking in
522 printk("conntrack_ftp: partial %s %u+%u\n",
523 search
[dir
][i
].pattern
,
524 ntohl(th
->seq
), datalen
);
527 } else if (found
== 0) { /* No match */
532 DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
533 (int)matchlen
, fb_ptr
+ matchoff
,
534 matchlen
, ntohl(th
->seq
) + matchoff
);
536 exp
= nf_conntrack_expect_alloc(ct
);
542 /* We refer to the reverse direction ("!dir") tuples here,
543 * because we're expecting something in the other direction.
544 * Doesn't matter unless NAT is happening. */
545 exp
->tuple
.dst
.u3
= ct
->tuplehash
[!dir
].tuple
.dst
.u3
;
547 /* Update the ftp info */
548 if ((cmd
.l3num
== ct
->tuplehash
[dir
].tuple
.src
.l3num
) &&
549 memcmp(&cmd
.u3
.all
, &ct
->tuplehash
[dir
].tuple
.src
.u3
.all
,
550 sizeof(cmd
.u3
.all
))) {
551 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
552 server: it really wants us to connect to a
553 different IP address. Simply don't record it for
555 if (cmd
.l3num
== PF_INET
) {
556 DEBUGP("conntrack_ftp: NOT RECORDING: " NIPQUAD_FMT
" != " NIPQUAD_FMT
"\n",
558 NIPQUAD(ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
));
560 DEBUGP("conntrack_ftp: NOT RECORDING: " NIP6_FMT
" != " NIP6_FMT
"\n",
561 NIP6(*((struct in6_addr
*)cmd
.u3
.ip6
)),
562 NIP6(*((struct in6_addr
*)ct
->tuplehash
[dir
]
563 .tuple
.src
.u3
.ip6
)));
566 /* Thanks to Cristiano Lincoln Mattos
567 <lincoln@cesar.org.br> for reporting this potential
568 problem (DMZ machines opening holes to internal
569 networks, or the packet filter itself). */
574 memcpy(&exp
->tuple
.dst
.u3
, &cmd
.u3
.all
,
575 sizeof(exp
->tuple
.dst
.u3
));
578 exp
->tuple
.src
.u3
= ct
->tuplehash
[!dir
].tuple
.src
.u3
;
579 exp
->tuple
.src
.l3num
= cmd
.l3num
;
580 exp
->tuple
.src
.u
.tcp
.port
= 0;
581 exp
->tuple
.dst
.u
.tcp
.port
= cmd
.u
.tcp
.port
;
582 exp
->tuple
.dst
.protonum
= IPPROTO_TCP
;
584 exp
->mask
= (struct nf_conntrack_tuple
)
585 { .src
= { .l3num
= 0xFFFF,
586 .u
= { .tcp
= { 0 }},
588 .dst
= { .protonum
= 0xFF,
589 .u
= { .tcp
= { 0xFFFF }},
592 if (cmd
.l3num
== PF_INET
) {
593 exp
->mask
.src
.u3
.ip
= 0xFFFFFFFF;
594 exp
->mask
.dst
.u3
.ip
= 0xFFFFFFFF;
596 memset(exp
->mask
.src
.u3
.ip6
, 0xFF,
597 sizeof(exp
->mask
.src
.u3
.ip6
));
598 memset(exp
->mask
.dst
.u3
.ip6
, 0xFF,
599 sizeof(exp
->mask
.src
.u3
.ip6
));
602 exp
->expectfn
= NULL
;
605 /* Now, NAT might want to mangle the packet, and register the
606 * (possibly changed) expectation itself. */
608 ret
= nf_nat_ftp_hook(pskb
, ctinfo
, search
[dir
][i
].ftptype
,
609 matchoff
, matchlen
, exp
, &seq
);
611 /* Can't expect this? Best to drop packet now. */
612 if (nf_conntrack_expect_related(exp
) != 0)
619 nf_conntrack_expect_put(exp
);
622 /* Now if this ends in \n, update ftp info. Seq may have been
623 * adjusted by NAT code. */
625 update_nl_seq(seq
, ct_ftp_info
, dir
, *pskb
);
627 spin_unlock_bh(&nf_ftp_lock
);
631 static struct nf_conntrack_helper ftp
[MAX_PORTS
][2];
632 static char ftp_names
[MAX_PORTS
][2][sizeof("ftp-65535")];
634 /* don't make this __exit, since it's called from __init ! */
635 static void nf_conntrack_ftp_fini(void)
638 for (i
= 0; i
< ports_c
; i
++) {
639 for (j
= 0; j
< 2; j
++) {
640 if (ftp
[i
][j
].me
== NULL
)
643 DEBUGP("nf_ct_ftp: unregistering helper for pf: %d "
645 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
646 nf_conntrack_helper_unregister(&ftp
[i
][j
]);
653 static int __init
nf_conntrack_ftp_init(void)
655 int i
, j
= -1, ret
= 0;
658 ftp_buffer
= kmalloc(65536, GFP_KERNEL
);
663 ports
[ports_c
++] = FTP_PORT
;
665 /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
666 are tracked or not - YK */
667 for (i
= 0; i
< ports_c
; i
++) {
668 ftp
[i
][0].tuple
.src
.l3num
= PF_INET
;
669 ftp
[i
][1].tuple
.src
.l3num
= PF_INET6
;
670 for (j
= 0; j
< 2; j
++) {
671 ftp
[i
][j
].tuple
.src
.u
.tcp
.port
= htons(ports
[i
]);
672 ftp
[i
][j
].tuple
.dst
.protonum
= IPPROTO_TCP
;
673 ftp
[i
][j
].mask
.src
.u
.tcp
.port
= 0xFFFF;
674 ftp
[i
][j
].mask
.dst
.protonum
= 0xFF;
675 ftp
[i
][j
].max_expected
= 1;
676 ftp
[i
][j
].timeout
= 5 * 60; /* 5 Minutes */
677 ftp
[i
][j
].me
= THIS_MODULE
;
678 ftp
[i
][j
].help
= help
;
679 tmpname
= &ftp_names
[i
][j
][0];
680 if (ports
[i
] == FTP_PORT
)
681 sprintf(tmpname
, "ftp");
683 sprintf(tmpname
, "ftp-%d", ports
[i
]);
684 ftp
[i
][j
].name
= tmpname
;
686 DEBUGP("nf_ct_ftp: registering helper for pf: %d "
688 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
689 ret
= nf_conntrack_helper_register(&ftp
[i
][j
]);
691 printk("nf_ct_ftp: failed to register helper "
692 " for pf: %d port: %d\n",
693 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
694 nf_conntrack_ftp_fini();
703 module_init(nf_conntrack_ftp_init
);
704 module_exit(nf_conntrack_ftp_fini
);