1 /* FTP extension for IP connection tracking. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/netfilter.h>
15 #include <linux/ctype.h>
16 #include <net/checksum.h>
19 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
20 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
21 #include <linux/moduleparam.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25 MODULE_DESCRIPTION("ftp connection tracking helper");
27 /* This is slow, but it's simple. --RR */
28 static char ftp_buffer
[65536];
30 static DEFINE_SPINLOCK(ip_ftp_lock
);
33 static int ports
[MAX_PORTS
];
35 module_param_array(ports
, int, &ports_c
, 0400);
38 module_param(loose
, int, 0600);
40 unsigned int (*ip_nat_ftp_hook
)(struct sk_buff
**pskb
,
41 enum ip_conntrack_info ctinfo
,
42 enum ip_ct_ftp_type type
,
43 unsigned int matchoff
,
44 unsigned int matchlen
,
45 struct ip_conntrack_expect
*exp
,
47 EXPORT_SYMBOL_GPL(ip_nat_ftp_hook
);
52 #define DEBUGP(format, args...)
55 static int try_rfc959(const char *, size_t, u_int32_t
[], char);
56 static int try_eprt(const char *, size_t, u_int32_t
[], char);
57 static int try_epsv_response(const char *, size_t, u_int32_t
[], char);
59 static struct ftp_search
{
60 enum ip_conntrack_dir dir
;
65 enum ip_ct_ftp_type ftptype
;
66 int (*getnum
)(const char *, size_t, u_int32_t
[], char);
70 "PORT", sizeof("PORT") - 1, ' ', '\r',
76 "227 ", sizeof("227 ") - 1, '(', ')',
82 "EPRT", sizeof("EPRT") - 1, ' ', '\r',
88 "229 ", sizeof("229 ") - 1, '(', ')',
94 static int try_number(const char *data
, size_t dlen
, u_int32_t array
[],
95 int array_size
, char sep
, char term
)
99 memset(array
, 0, sizeof(array
[0])*array_size
);
101 /* Keep data pointing at next char. */
102 for (i
= 0, len
= 0; len
< dlen
&& i
< array_size
; len
++, data
++) {
103 if (*data
>= '0' && *data
<= '9') {
104 array
[i
] = array
[i
]*10 + *data
- '0';
106 else if (*data
== sep
)
109 /* Unexpected character; true if it's the
110 terminator and we're finished. */
111 if (*data
== term
&& i
== array_size
- 1)
114 DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
119 DEBUGP("Failed to fill %u numbers separated by %c\n", array_size
, sep
);
124 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
125 static int try_rfc959(const char *data
, size_t dlen
, u_int32_t array
[6],
128 return try_number(data
, dlen
, array
, 6, ',', term
);
131 /* Grab port: number up to delimiter */
132 static int get_port(const char *data
, int start
, size_t dlen
, char delim
,
138 for (i
= start
; i
< dlen
; i
++) {
140 if (data
[i
] == delim
) {
143 array
[0] = port
>> 8;
147 else if (data
[i
] >= '0' && data
[i
] <= '9')
148 port
= port
*10 + data
[i
] - '0';
149 else /* Some other crap */
155 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| */
156 static int try_eprt(const char *data
, size_t dlen
, u_int32_t array
[6],
162 /* First character is delimiter, then "1" for IPv4, then
164 if (dlen
<= 3) return 0;
166 if (isdigit(delim
) || delim
< 33 || delim
> 126
167 || data
[1] != '1' || data
[2] != delim
)
170 DEBUGP("EPRT: Got |1|!\n");
171 /* Now we have IP address. */
172 length
= try_number(data
+ 3, dlen
- 3, array
, 4, '.', delim
);
176 DEBUGP("EPRT: Got IP address!\n");
177 /* Start offset includes initial "|1|", and trailing delimiter */
178 return get_port(data
, 3 + length
+ 1, dlen
, delim
, array
+4);
181 /* Returns 0, or length of numbers: |||6446| */
182 static int try_epsv_response(const char *data
, size_t dlen
, u_int32_t array
[6],
187 /* Three delimiters. */
188 if (dlen
<= 3) return 0;
190 if (isdigit(delim
) || delim
< 33 || delim
> 126
191 || data
[1] != delim
|| data
[2] != delim
)
194 return get_port(data
, 3, dlen
, delim
, array
+4);
197 /* Return 1 for match, 0 for accept, -1 for partial. */
198 static int find_pattern(const char *data
, size_t dlen
,
199 const char *pattern
, size_t plen
,
200 char skip
, char term
,
201 unsigned int *numoff
,
202 unsigned int *numlen
,
204 int (*getnum
)(const char *, size_t, u_int32_t
[], char))
208 DEBUGP("find_pattern `%s': dlen = %u\n", pattern
, dlen
);
213 /* Short packet: try for partial? */
214 if (strnicmp(data
, pattern
, dlen
) == 0)
219 if (strnicmp(data
, pattern
, plen
) != 0) {
223 DEBUGP("ftp: string mismatch\n");
224 for (i
= 0; i
< plen
; i
++) {
225 DEBUGP("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
227 pattern
[i
], pattern
[i
]);
233 DEBUGP("Pattern matches!\n");
234 /* Now we've found the constant string, try to skip
235 to the 'skip' character */
236 for (i
= plen
; data
[i
] != skip
; i
++)
237 if (i
== dlen
- 1) return -1;
239 /* Skip over the last character */
242 DEBUGP("Skipped up to `%c'!\n", skip
);
245 *numlen
= getnum(data
+ i
, dlen
- i
, array
, term
);
249 DEBUGP("Match succeeded!\n");
253 /* Look up to see if we're just after a \n. */
254 static int find_nl_seq(u32 seq
, const struct ip_ct_ftp_master
*info
, int dir
)
258 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++)
259 if (info
->seq_aft_nl
[dir
][i
] == seq
)
264 /* We don't update if it's older than what we have. */
265 static void update_nl_seq(u32 nl_seq
, struct ip_ct_ftp_master
*info
, int dir
)
267 unsigned int i
, oldest
= NUM_SEQ_TO_REMEMBER
;
269 /* Look for oldest: if we find exact match, we're done. */
270 for (i
= 0; i
< info
->seq_aft_nl_num
[dir
]; i
++) {
271 if (info
->seq_aft_nl
[dir
][i
] == nl_seq
)
274 if (oldest
== info
->seq_aft_nl_num
[dir
]
275 || before(info
->seq_aft_nl
[dir
][i
], oldest
))
279 if (info
->seq_aft_nl_num
[dir
] < NUM_SEQ_TO_REMEMBER
)
280 info
->seq_aft_nl
[dir
][info
->seq_aft_nl_num
[dir
]++] = nl_seq
;
281 else if (oldest
!= NUM_SEQ_TO_REMEMBER
)
282 info
->seq_aft_nl
[dir
][oldest
] = nl_seq
;
285 static int help(struct sk_buff
**pskb
,
286 struct ip_conntrack
*ct
,
287 enum ip_conntrack_info ctinfo
)
289 unsigned int dataoff
, datalen
;
290 struct tcphdr _tcph
, *th
;
293 u32 seq
, array
[6] = { 0 };
294 int dir
= CTINFO2DIR(ctinfo
);
295 unsigned int matchlen
, matchoff
;
296 struct ip_ct_ftp_master
*ct_ftp_info
= &ct
->help
.ct_ftp_info
;
297 struct ip_conntrack_expect
*exp
;
299 int found
= 0, ends_in_nl
;
301 /* Until there's been traffic both ways, don't look in packets. */
302 if (ctinfo
!= IP_CT_ESTABLISHED
303 && ctinfo
!= IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
) {
304 DEBUGP("ftp: Conntrackinfo = %u\n", ctinfo
);
308 th
= skb_header_pointer(*pskb
, (*pskb
)->nh
.iph
->ihl
*4,
309 sizeof(_tcph
), &_tcph
);
313 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + th
->doff
*4;
315 if (dataoff
>= (*pskb
)->len
) {
316 DEBUGP("ftp: pskblen = %u\n", (*pskb
)->len
);
319 datalen
= (*pskb
)->len
- dataoff
;
321 spin_lock_bh(&ip_ftp_lock
);
322 fb_ptr
= skb_header_pointer(*pskb
, dataoff
,
323 (*pskb
)->len
- dataoff
, ftp_buffer
);
324 BUG_ON(fb_ptr
== NULL
);
326 ends_in_nl
= (fb_ptr
[datalen
- 1] == '\n');
327 seq
= ntohl(th
->seq
) + datalen
;
329 /* Look up to see if we're just after a \n. */
330 if (!find_nl_seq(ntohl(th
->seq
), ct_ftp_info
, dir
)) {
331 /* Now if this ends in \n, update ftp info. */
332 DEBUGP("ip_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
333 ct_ftp_info
->seq_aft_nl
[0][dir
]
334 old_seq_aft_nl_set
? "":"(UNSET) ", old_seq_aft_nl
);
339 /* Initialize IP array to expected address (it's not mentioned
340 in EPSV responses) */
341 array
[0] = (ntohl(ct
->tuplehash
[dir
].tuple
.src
.ip
) >> 24) & 0xFF;
342 array
[1] = (ntohl(ct
->tuplehash
[dir
].tuple
.src
.ip
) >> 16) & 0xFF;
343 array
[2] = (ntohl(ct
->tuplehash
[dir
].tuple
.src
.ip
) >> 8) & 0xFF;
344 array
[3] = ntohl(ct
->tuplehash
[dir
].tuple
.src
.ip
) & 0xFF;
346 for (i
= 0; i
< ARRAY_SIZE(search
); i
++) {
347 if (search
[i
].dir
!= dir
) continue;
349 found
= find_pattern(fb_ptr
, (*pskb
)->len
- dataoff
,
354 &matchoff
, &matchlen
,
360 /* We don't usually drop packets. After all, this is
361 connection tracking, not packet filtering.
362 However, it is necessary for accurate tracking in
365 printk("conntrack_ftp: partial %s %u+%u\n",
367 ntohl(th
->seq
), datalen
);
370 } else if (found
== 0) { /* No match */
375 DEBUGP("conntrack_ftp: match `%s' (%u bytes at %u)\n",
376 fb_ptr
+ matchoff
, matchlen
, ntohl(th
->seq
) + matchoff
);
378 /* Allocate expectation which will be inserted */
379 exp
= ip_conntrack_expect_alloc(ct
);
385 /* We refer to the reverse direction ("!dir") tuples here,
386 * because we're expecting something in the other direction.
387 * Doesn't matter unless NAT is happening. */
388 exp
->tuple
.dst
.ip
= ct
->tuplehash
[!dir
].tuple
.dst
.ip
;
390 if (htonl((array
[0] << 24) | (array
[1] << 16) | (array
[2] << 8) | array
[3])
391 != ct
->tuplehash
[dir
].tuple
.src
.ip
) {
392 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
393 server: it really wants us to connect to a
394 different IP address. Simply don't record it for
396 DEBUGP("conntrack_ftp: NOT RECORDING: %u,%u,%u,%u != %u.%u.%u.%u\n",
397 array
[0], array
[1], array
[2], array
[3],
398 NIPQUAD(ct
->tuplehash
[dir
].tuple
.src
.ip
));
400 /* Thanks to Cristiano Lincoln Mattos
401 <lincoln@cesar.org.br> for reporting this potential
402 problem (DMZ machines opening holes to internal
403 networks, or the packet filter itself). */
408 exp
->tuple
.dst
.ip
= htonl((array
[0] << 24) | (array
[1] << 16)
409 | (array
[2] << 8) | array
[3]);
412 exp
->tuple
.src
.ip
= ct
->tuplehash
[!dir
].tuple
.src
.ip
;
413 exp
->tuple
.dst
.u
.tcp
.port
= htons(array
[4] << 8 | array
[5]);
414 exp
->tuple
.src
.u
.tcp
.port
= 0; /* Don't care. */
415 exp
->tuple
.dst
.protonum
= IPPROTO_TCP
;
416 exp
->mask
= ((struct ip_conntrack_tuple
)
417 { { 0xFFFFFFFF, { 0 } },
418 { 0xFFFFFFFF, { .tcp
= { 0xFFFF } }, 0xFF }});
420 exp
->expectfn
= NULL
;
422 /* Now, NAT might want to mangle the packet, and register the
423 * (possibly changed) expectation itself. */
425 ret
= ip_nat_ftp_hook(pskb
, ctinfo
, search
[i
].ftptype
,
426 matchoff
, matchlen
, exp
, &seq
);
428 /* Can't expect this? Best to drop packet now. */
429 if (ip_conntrack_expect_related(exp
) != 0)
436 ip_conntrack_expect_put(exp
);
439 /* Now if this ends in \n, update ftp info. Seq may have been
440 * adjusted by NAT code. */
442 update_nl_seq(seq
, ct_ftp_info
,dir
);
444 spin_unlock_bh(&ip_ftp_lock
);
448 static struct ip_conntrack_helper ftp
[MAX_PORTS
];
449 static char ftp_names
[MAX_PORTS
][10];
451 /* Not __exit: called from init() */
452 static void fini(void)
455 for (i
= 0; i
< ports_c
; i
++) {
456 DEBUGP("ip_ct_ftp: unregistering helper for port %d\n",
458 ip_conntrack_helper_unregister(&ftp
[i
]);
462 static int __init
init(void)
468 ports
[ports_c
++] = FTP_PORT
;
470 for (i
= 0; i
< ports_c
; i
++) {
471 ftp
[i
].tuple
.src
.u
.tcp
.port
= htons(ports
[i
]);
472 ftp
[i
].tuple
.dst
.protonum
= IPPROTO_TCP
;
473 ftp
[i
].mask
.src
.u
.tcp
.port
= 0xFFFF;
474 ftp
[i
].mask
.dst
.protonum
= 0xFF;
475 ftp
[i
].max_expected
= 1;
476 ftp
[i
].timeout
= 5 * 60; /* 5 minutes */
477 ftp
[i
].me
= THIS_MODULE
;
480 tmpname
= &ftp_names
[i
][0];
481 if (ports
[i
] == FTP_PORT
)
482 sprintf(tmpname
, "ftp");
484 sprintf(tmpname
, "ftp-%d", ports
[i
]);
485 ftp
[i
].name
= tmpname
;
487 DEBUGP("ip_ct_ftp: registering helper for port %d\n",
489 ret
= ip_conntrack_helper_register(&ftp
[i
]);