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/ipv6.h>
17 #include <linux/ctype.h>
18 #include <linux/inet.h>
19 #include <net/checksum.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");
32 MODULE_ALIAS_NFCT_HELPER("ftp");
34 /* This is slow, but it's simple. --RR */
35 static char *ftp_buffer
;
37 static DEFINE_SPINLOCK(nf_ftp_lock
);
40 static u_int16_t ports
[MAX_PORTS
];
41 static unsigned int ports_c
;
42 module_param_array(ports
, ushort
, &ports_c
, 0400);
45 module_param(loose
, bool, 0600);
47 unsigned int (*nf_nat_ftp_hook
)(struct sk_buff
*skb
,
48 enum ip_conntrack_info ctinfo
,
49 enum nf_ct_ftp_type type
,
50 unsigned int matchoff
,
51 unsigned int matchlen
,
52 struct nf_conntrack_expect
*exp
);
53 EXPORT_SYMBOL_GPL(nf_nat_ftp_hook
);
55 static int try_rfc959(const char *, size_t, struct nf_conntrack_man
*, char);
56 static int try_eprt(const char *, size_t, struct nf_conntrack_man
*, char);
57 static int try_epsv_response(const char *, size_t, struct nf_conntrack_man
*,
60 static struct ftp_search
{
65 enum nf_ct_ftp_type ftptype
;
66 int (*getnum
)(const char *, size_t, struct nf_conntrack_man
*, char);
67 } search
[IP_CT_DIR_MAX
][2] = {
68 [IP_CT_DIR_ORIGINAL
] = {
71 .plen
= sizeof("PORT") - 1,
74 .ftptype
= NF_CT_FTP_PORT
,
79 .plen
= sizeof("EPRT") - 1,
82 .ftptype
= NF_CT_FTP_EPRT
,
89 .plen
= sizeof("227 ") - 1,
92 .ftptype
= NF_CT_FTP_PASV
,
97 .plen
= sizeof("229 ") - 1,
100 .ftptype
= NF_CT_FTP_EPSV
,
101 .getnum
= try_epsv_response
,
107 get_ipv6_addr(const char *src
, size_t dlen
, struct in6_addr
*dst
, u_int8_t term
)
110 int ret
= in6_pton(src
, min_t(size_t, dlen
, 0xffff), (u8
*)dst
, term
, &end
);
112 return (int)(end
- src
);
116 static int try_number(const char *data
, size_t dlen
, u_int32_t array
[],
117 int array_size
, char sep
, char term
)
121 memset(array
, 0, sizeof(array
[0])*array_size
);
123 /* Keep data pointing at next char. */
124 for (i
= 0, len
= 0; len
< dlen
&& i
< array_size
; len
++, data
++) {
125 if (*data
>= '0' && *data
<= '9') {
126 array
[i
] = array
[i
]*10 + *data
- '0';
128 else if (*data
== sep
)
131 /* Unexpected character; true if it's the
132 terminator and we're finished. */
133 if (*data
== term
&& i
== array_size
- 1)
136 pr_debug("Char %u (got %u nums) `%u' unexpected\n",
141 pr_debug("Failed to fill %u numbers separated by %c\n",
146 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
147 static int try_rfc959(const char *data
, size_t dlen
,
148 struct nf_conntrack_man
*cmd
, char term
)
153 length
= try_number(data
, dlen
, array
, 6, ',', term
);
157 cmd
->u3
.ip
= htonl((array
[0] << 24) | (array
[1] << 16) |
158 (array
[2] << 8) | array
[3]);
159 cmd
->u
.tcp
.port
= htons((array
[4] << 8) | array
[5]);
163 /* Grab port: number up to delimiter */
164 static int get_port(const char *data
, int start
, size_t dlen
, char delim
,
167 u_int16_t tmp_port
= 0;
170 for (i
= start
; i
< dlen
; i
++) {
172 if (data
[i
] == delim
) {
175 *port
= htons(tmp_port
);
176 pr_debug("get_port: return %d\n", tmp_port
);
179 else if (data
[i
] >= '0' && data
[i
] <= '9')
180 tmp_port
= tmp_port
*10 + data
[i
] - '0';
181 else { /* Some other crap */
182 pr_debug("get_port: invalid char.\n");
189 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
190 static int try_eprt(const char *data
, size_t dlen
, struct nf_conntrack_man
*cmd
,
196 /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
197 then delimiter again. */
199 pr_debug("EPRT: too short\n");
203 if (isdigit(delim
) || delim
< 33 || delim
> 126 || data
[2] != delim
) {
204 pr_debug("try_eprt: invalid delimitter.\n");
208 if ((cmd
->l3num
== PF_INET
&& data
[1] != '1') ||
209 (cmd
->l3num
== PF_INET6
&& data
[1] != '2')) {
210 pr_debug("EPRT: invalid protocol number.\n");
214 pr_debug("EPRT: Got %c%c%c\n", delim
, data
[1], delim
);
216 if (data
[1] == '1') {
219 /* Now we have IP address. */
220 length
= try_number(data
+ 3, dlen
- 3, array
, 4, '.', delim
);
222 cmd
->u3
.ip
= htonl((array
[0] << 24) | (array
[1] << 16)
223 | (array
[2] << 8) | array
[3]);
225 /* Now we have IPv6 address. */
226 length
= get_ipv6_addr(data
+ 3, dlen
- 3,
227 (struct in6_addr
*)cmd
->u3
.ip6
, delim
);
232 pr_debug("EPRT: Got IP address!\n");
233 /* Start offset includes initial "|1|", and trailing delimiter */
234 return get_port(data
, 3 + length
+ 1, dlen
, delim
, &cmd
->u
.tcp
.port
);
237 /* Returns 0, or length of numbers: |||6446| */
238 static int try_epsv_response(const char *data
, size_t dlen
,
239 struct nf_conntrack_man
*cmd
, char term
)
243 /* Three delimiters. */
244 if (dlen
<= 3) return 0;
246 if (isdigit(delim
) || delim
< 33 || delim
> 126
247 || data
[1] != delim
|| data
[2] != delim
)
250 return get_port(data
, 3, dlen
, delim
, &cmd
->u
.tcp
.port
);
253 /* Return 1 for match, 0 for accept, -1 for partial. */
254 static int find_pattern(const char *data
, size_t dlen
,
255 const char *pattern
, size_t plen
,
256 char skip
, char term
,
257 unsigned int *numoff
,
258 unsigned int *numlen
,
259 struct nf_conntrack_man
*cmd
,
260 int (*getnum
)(const char *, size_t,
261 struct nf_conntrack_man
*, char))
265 pr_debug("find_pattern `%s': dlen = %Zu\n", pattern
, dlen
);
270 /* Short packet: try for partial? */
271 if (strnicmp(data
, pattern
, dlen
) == 0)
276 if (strnicmp(data
, pattern
, plen
) != 0) {
280 pr_debug("ftp: string mismatch\n");
281 for (i
= 0; i
< plen
; i
++) {
282 pr_debug("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
284 pattern
[i
], pattern
[i
]);
290 pr_debug("Pattern matches!\n");
291 /* Now we've found the constant string, try to skip
292 to the 'skip' character */
293 for (i
= plen
; data
[i
] != skip
; i
++)
294 if (i
== dlen
- 1) return -1;
296 /* Skip over the last character */
299 pr_debug("Skipped up to `%c'!\n", skip
);
302 *numlen
= getnum(data
+ i
, dlen
- i
, cmd
, term
);
306 pr_debug("Match succeeded!\n");
310 /* Look up to see if we're just after a \n. */
311 static int find_nl_seq(u32 seq
, const struct nf_ct_ftp_master
*info
, int dir
)
315 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++)
316 if (info
->seq_aft_nl
[dir
][i
] == seq
)
321 /* We don't update if it's older than what we have. */
322 static void update_nl_seq(struct nf_conn
*ct
, u32 nl_seq
,
323 struct nf_ct_ftp_master
*info
, int dir
,
326 unsigned int i
, oldest
= NUM_SEQ_TO_REMEMBER
;
328 /* Look for oldest: if we find exact match, we're done. */
329 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++) {
330 if (info
->seq_aft_nl
[dir
][i
] == nl_seq
)
333 if (oldest
== info
->seq_aft_nl_num
[dir
] ||
334 before(info
->seq_aft_nl
[dir
][i
],
335 info
->seq_aft_nl
[dir
][oldest
]))
339 if (info
->seq_aft_nl_num
[dir
] < NUM_SEQ_TO_REMEMBER
) {
340 info
->seq_aft_nl
[dir
][info
->seq_aft_nl_num
[dir
]++] = nl_seq
;
341 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE
, ct
);
342 } else if (oldest
!= NUM_SEQ_TO_REMEMBER
&&
343 after(nl_seq
, info
->seq_aft_nl
[dir
][oldest
])) {
344 info
->seq_aft_nl
[dir
][oldest
] = nl_seq
;
345 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE
, ct
);
349 static int help(struct sk_buff
*skb
,
350 unsigned int protoff
,
352 enum ip_conntrack_info ctinfo
)
354 unsigned int dataoff
, datalen
;
355 const struct tcphdr
*th
;
360 int dir
= CTINFO2DIR(ctinfo
);
361 unsigned int uninitialized_var(matchlen
), uninitialized_var(matchoff
);
362 struct nf_ct_ftp_master
*ct_ftp_info
= &nfct_help(ct
)->help
.ct_ftp_info
;
363 struct nf_conntrack_expect
*exp
;
364 union nf_inet_addr
*daddr
;
365 struct nf_conntrack_man cmd
= {};
367 int found
= 0, ends_in_nl
;
368 typeof(nf_nat_ftp_hook
) nf_nat_ftp
;
370 /* Until there's been traffic both ways, don't look in packets. */
371 if (ctinfo
!= IP_CT_ESTABLISHED
372 && ctinfo
!= IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
) {
373 pr_debug("ftp: Conntrackinfo = %u\n", ctinfo
);
377 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
381 dataoff
= protoff
+ th
->doff
* 4;
383 if (dataoff
>= skb
->len
) {
384 pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff
,
388 datalen
= skb
->len
- dataoff
;
390 spin_lock_bh(&nf_ftp_lock
);
391 fb_ptr
= skb_header_pointer(skb
, dataoff
, datalen
, ftp_buffer
);
392 BUG_ON(fb_ptr
== NULL
);
394 ends_in_nl
= (fb_ptr
[datalen
- 1] == '\n');
395 seq
= ntohl(th
->seq
) + datalen
;
397 /* Look up to see if we're just after a \n. */
398 if (!find_nl_seq(ntohl(th
->seq
), ct_ftp_info
, dir
)) {
399 /* Now if this ends in \n, update ftp info. */
400 pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
401 ct_ftp_info
->seq_aft_nl_num
[dir
] > 0 ? "" : "(UNSET)",
402 ct_ftp_info
->seq_aft_nl
[dir
][0],
403 ct_ftp_info
->seq_aft_nl_num
[dir
] > 1 ? "" : "(UNSET)",
404 ct_ftp_info
->seq_aft_nl
[dir
][1]);
409 /* Initialize IP/IPv6 addr to expected address (it's not mentioned
410 in EPSV responses) */
411 cmd
.l3num
= nf_ct_l3num(ct
);
412 memcpy(cmd
.u3
.all
, &ct
->tuplehash
[dir
].tuple
.src
.u3
.all
,
415 for (i
= 0; i
< ARRAY_SIZE(search
[dir
]); i
++) {
416 found
= find_pattern(fb_ptr
, datalen
,
417 search
[dir
][i
].pattern
,
421 &matchoff
, &matchlen
,
423 search
[dir
][i
].getnum
);
427 /* We don't usually drop packets. After all, this is
428 connection tracking, not packet filtering.
429 However, it is necessary for accurate tracking in
431 pr_debug("conntrack_ftp: partial %s %u+%u\n",
432 search
[dir
][i
].pattern
, ntohl(th
->seq
), datalen
);
435 } else if (found
== 0) { /* No match */
440 pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
441 matchlen
, fb_ptr
+ matchoff
,
442 matchlen
, ntohl(th
->seq
) + matchoff
);
444 exp
= nf_ct_expect_alloc(ct
);
450 /* We refer to the reverse direction ("!dir") tuples here,
451 * because we're expecting something in the other direction.
452 * Doesn't matter unless NAT is happening. */
453 daddr
= &ct
->tuplehash
[!dir
].tuple
.dst
.u3
;
455 /* Update the ftp info */
456 if ((cmd
.l3num
== nf_ct_l3num(ct
)) &&
457 memcmp(&cmd
.u3
.all
, &ct
->tuplehash
[dir
].tuple
.src
.u3
.all
,
458 sizeof(cmd
.u3
.all
))) {
459 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
460 server: it really wants us to connect to a
461 different IP address. Simply don't record it for
463 if (cmd
.l3num
== PF_INET
) {
464 pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n",
466 &ct
->tuplehash
[dir
].tuple
.src
.u3
.ip
);
468 pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n",
470 ct
->tuplehash
[dir
].tuple
.src
.u3
.ip6
);
473 /* Thanks to Cristiano Lincoln Mattos
474 <lincoln@cesar.org.br> for reporting this potential
475 problem (DMZ machines opening holes to internal
476 networks, or the packet filter itself). */
484 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, cmd
.l3num
,
485 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, daddr
,
486 IPPROTO_TCP
, NULL
, &cmd
.u
.tcp
.port
);
488 /* Now, NAT might want to mangle the packet, and register the
489 * (possibly changed) expectation itself. */
490 nf_nat_ftp
= rcu_dereference(nf_nat_ftp_hook
);
491 if (nf_nat_ftp
&& ct
->status
& IPS_NAT_MASK
)
492 ret
= nf_nat_ftp(skb
, ctinfo
, search
[dir
][i
].ftptype
,
493 matchoff
, matchlen
, exp
);
495 /* Can't expect this? Best to drop packet now. */
496 if (nf_ct_expect_related(exp
) != 0)
503 nf_ct_expect_put(exp
);
506 /* Now if this ends in \n, update ftp info. Seq may have been
507 * adjusted by NAT code. */
509 update_nl_seq(ct
, seq
, ct_ftp_info
, dir
, skb
);
511 spin_unlock_bh(&nf_ftp_lock
);
515 static struct nf_conntrack_helper ftp
[MAX_PORTS
][2] __read_mostly
;
516 static char ftp_names
[MAX_PORTS
][2][sizeof("ftp-65535")] __read_mostly
;
518 static const struct nf_conntrack_expect_policy ftp_exp_policy
= {
523 /* don't make this __exit, since it's called from __init ! */
524 static void nf_conntrack_ftp_fini(void)
527 for (i
= 0; i
< ports_c
; i
++) {
528 for (j
= 0; j
< 2; j
++) {
529 if (ftp
[i
][j
].me
== NULL
)
532 pr_debug("nf_ct_ftp: unregistering helper for pf: %d "
534 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
535 nf_conntrack_helper_unregister(&ftp
[i
][j
]);
542 static int __init
nf_conntrack_ftp_init(void)
544 int i
, j
= -1, ret
= 0;
547 ftp_buffer
= kmalloc(65536, GFP_KERNEL
);
552 ports
[ports_c
++] = FTP_PORT
;
554 /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
555 are tracked or not - YK */
556 for (i
= 0; i
< ports_c
; i
++) {
557 ftp
[i
][0].tuple
.src
.l3num
= PF_INET
;
558 ftp
[i
][1].tuple
.src
.l3num
= PF_INET6
;
559 for (j
= 0; j
< 2; j
++) {
560 ftp
[i
][j
].tuple
.src
.u
.tcp
.port
= htons(ports
[i
]);
561 ftp
[i
][j
].tuple
.dst
.protonum
= IPPROTO_TCP
;
562 ftp
[i
][j
].expect_policy
= &ftp_exp_policy
;
563 ftp
[i
][j
].me
= THIS_MODULE
;
564 ftp
[i
][j
].help
= help
;
565 tmpname
= &ftp_names
[i
][j
][0];
566 if (ports
[i
] == FTP_PORT
)
567 sprintf(tmpname
, "ftp");
569 sprintf(tmpname
, "ftp-%d", ports
[i
]);
570 ftp
[i
][j
].name
= tmpname
;
572 pr_debug("nf_ct_ftp: registering helper for pf: %d "
574 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
575 ret
= nf_conntrack_helper_register(&ftp
[i
][j
]);
577 printk("nf_ct_ftp: failed to register helper "
578 " for pf: %d port: %d\n",
579 ftp
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
580 nf_conntrack_ftp_fini();
589 module_init(nf_conntrack_ftp_init
);
590 module_exit(nf_conntrack_ftp_fini
);