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/lockhelp.h>
20 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
21 #include <linux/netfilter_ipv4/ip_conntrack_ftp.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 DECLARE_LOCK(ip_ftp_lock);
31 struct module *ip_conntrack_ftp = THIS_MODULE;
34 static int ports[MAX_PORTS];
36 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
39 MODULE_PARM(loose, "i");
44 #define DEBUGP(format, args...)
47 static int try_rfc959(const char *, size_t, u_int32_t [], char);
48 static int try_eprt(const char *, size_t, u_int32_t [], char);
49 static int try_epsv_response(const char *, size_t, u_int32_t [], char);
51 static struct ftp_search {
52 enum ip_conntrack_dir dir;
57 enum ip_ct_ftp_type ftptype;
58 int (*getnum)(const char *, size_t, u_int32_t[], char);
62 "PORT", sizeof("PORT") - 1, ' ', '\r',
68 "227 ", sizeof("227 ") - 1, '(', ')',
74 "EPRT", sizeof("EPRT") - 1, ' ', '\r',
80 "229 ", sizeof("229 ") - 1, '(', ')',
86 static int try_number(const char *data, size_t dlen, u_int32_t array[],
87 int array_size, char sep, char term)
91 memset(array, 0, sizeof(array[0])*array_size);
93 /* Keep data pointing at next char. */
94 for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
95 if (*data >= '0' && *data <= '9') {
96 array[i] = array[i]*10 + *data - '0';
98 else if (*data == sep)
101 /* Unexpected character; true if it's the
102 terminator and we're finished. */
103 if (*data == term && i == array_size - 1)
106 DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
111 DEBUGP("Failed to fill %u numbers separated by %c\n", array_size, sep);
116 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
117 static int try_rfc959(const char *data, size_t dlen, u_int32_t array[6],
120 return try_number(data, dlen, array, 6, ',', term);
123 /* Grab port: number up to delimiter */
124 static int get_port(const char *data, int start, size_t dlen, char delim,
130 for (i = start; i < dlen; i++) {
132 if (data[i] == delim) {
135 array[0] = port >> 8;
139 else if (data[i] >= '0' && data[i] <= '9')
140 port = port*10 + data[i] - '0';
141 else /* Some other crap */
147 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| */
148 static int try_eprt(const char *data, size_t dlen, u_int32_t array[6],
154 /* First character is delimiter, then "1" for IPv4, then
156 if (dlen <= 3) return 0;
158 if (isdigit(delim) || delim < 33 || delim > 126
159 || data[1] != '1' || data[2] != delim)
162 DEBUGP("EPRT: Got |1|!\n");
163 /* Now we have IP address. */
164 length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
168 DEBUGP("EPRT: Got IP address!\n");
169 /* Start offset includes initial "|1|", and trailing delimiter */
170 return get_port(data, 3 + length + 1, dlen, delim, array+4);
173 /* Returns 0, or length of numbers: |||6446| */
174 static int try_epsv_response(const char *data, size_t dlen, u_int32_t array[6],
179 /* Three delimiters. */
180 if (dlen <= 3) return 0;
182 if (isdigit(delim) || delim < 33 || delim > 126
183 || data[1] != delim || data[2] != delim)
186 return get_port(data, 3, dlen, delim, array+4);
189 /* Return 1 for match, 0 for accept, -1 for partial. */
190 static int find_pattern(const char *data, size_t dlen,
191 const char *pattern, size_t plen,
192 char skip, char term,
193 unsigned int *numoff,
194 unsigned int *numlen,
196 int (*getnum)(const char *, size_t, u_int32_t[], char))
200 DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
205 /* Short packet: try for partial? */
206 if (strnicmp(data, pattern, dlen) == 0)
211 if (strnicmp(data, pattern, plen) != 0) {
215 DEBUGP("ftp: string mismatch\n");
216 for (i = 0; i < plen; i++) {
217 DEBUGP("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
219 pattern[i], pattern[i]);
225 DEBUGP("Pattern matches!\n");
226 /* Now we've found the constant string, try to skip
227 to the 'skip' character */
228 for (i = plen; data[i] != skip; i++)
229 if (i == dlen - 1) return -1;
231 /* Skip over the last character */
234 DEBUGP("Skipped up to `%c'!\n", skip);
237 *numlen = getnum(data + i, dlen - i, array, term);
241 DEBUGP("Match succeeded!\n");
245 static int help(struct sk_buff *skb,
246 struct ip_conntrack *ct,
247 enum ip_conntrack_info ctinfo)
249 unsigned int dataoff, datalen;
251 u_int32_t old_seq_aft_nl;
252 int old_seq_aft_nl_set, ret;
253 u_int32_t array[6] = { 0 };
254 int dir = CTINFO2DIR(ctinfo);
255 unsigned int matchlen, matchoff;
256 struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
257 struct ip_conntrack_expect *exp;
258 struct ip_ct_ftp_expect *exp_ftp_info;
263 /* Until there's been traffic both ways, don't look in packets. */
264 if (ctinfo != IP_CT_ESTABLISHED
265 && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
266 DEBUGP("ftp: Conntrackinfo = %u\n", ctinfo);
270 if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) != 0)
273 dataoff = skb->nh.iph->ihl*4 + tcph.doff*4;
275 if (dataoff >= skb->len) {
276 DEBUGP("ftp: skblen = %u\n", skb->len);
279 datalen = skb->len - dataoff;
281 LOCK_BH(&ip_ftp_lock);
282 skb_copy_bits(skb, dataoff, ftp_buffer, skb->len - dataoff);
284 old_seq_aft_nl_set = ct_ftp_info->seq_aft_nl_set[dir];
285 old_seq_aft_nl = ct_ftp_info->seq_aft_nl[dir];
287 DEBUGP("conntrack_ftp: datalen %u\n", datalen);
288 if (ftp_buffer[datalen - 1] == '\n') {
289 DEBUGP("conntrack_ftp: datalen %u ends in \\n\n", datalen);
290 if (!old_seq_aft_nl_set
291 || after(ntohl(tcph.seq) + datalen, old_seq_aft_nl)) {
292 DEBUGP("conntrack_ftp: updating nl to %u\n",
293 ntohl(tcph.seq) + datalen);
294 ct_ftp_info->seq_aft_nl[dir] =
295 ntohl(tcph.seq) + datalen;
296 ct_ftp_info->seq_aft_nl_set[dir] = 1;
300 if(!old_seq_aft_nl_set ||
301 (ntohl(tcph.seq) != old_seq_aft_nl)) {
302 DEBUGP("ip_conntrack_ftp_help: wrong seq pos %s(%u)\n",
303 old_seq_aft_nl_set ? "":"(UNSET) ", old_seq_aft_nl);
308 /* Initialize IP array to expected address (it's not mentioned
309 in EPSV responses) */
310 array[0] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 24) & 0xFF;
311 array[1] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 16) & 0xFF;
312 array[2] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 8) & 0xFF;
313 array[3] = ntohl(ct->tuplehash[dir].tuple.src.ip) & 0xFF;
315 for (i = 0; i < ARRAY_SIZE(search); i++) {
316 if (search[i].dir != dir) continue;
318 found = find_pattern(ftp_buffer, skb->len - dataoff,
323 &matchoff, &matchlen,
329 /* We don't usually drop packets. After all, this is
330 connection tracking, not packet filtering.
331 However, it is necessary for accurate tracking in
334 printk("conntrack_ftp: partial %s %u+%u\n",
336 ntohl(tcph.seq), datalen);
339 } else if (found == 0) { /* No match */
344 DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
345 (int)matchlen, data + matchoff,
346 matchlen, ntohl(tcph.seq) + matchoff);
348 /* Allocate expectation which will be inserted */
349 exp = ip_conntrack_expect_alloc();
355 exp_ftp_info = &exp->help.exp_ftp_info;
357 /* Update the ftp info */
358 if (htonl((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3])
359 == ct->tuplehash[dir].tuple.src.ip) {
360 exp->seq = ntohl(tcph.seq) + matchoff;
361 exp_ftp_info->len = matchlen;
362 exp_ftp_info->ftptype = search[i].ftptype;
363 exp_ftp_info->port = array[4] << 8 | array[5];
365 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
366 server: it really wants us to connect to a
367 different IP address. Simply don't record it for
369 DEBUGP("conntrack_ftp: NOT RECORDING: %u,%u,%u,%u != %u.%u.%u.%u\n",
370 array[0], array[1], array[2], array[3],
371 NIPQUAD(ct->tuplehash[dir].tuple.src.ip));
373 /* Thanks to Cristiano Lincoln Mattos
374 <lincoln@cesar.org.br> for reporting this potential
375 problem (DMZ machines opening holes to internal
376 networks, or the packet filter itself). */
383 exp->tuple = ((struct ip_conntrack_tuple)
384 { { ct->tuplehash[!dir].tuple.src.ip,
386 { htonl((array[0] << 24) | (array[1] << 16)
387 | (array[2] << 8) | array[3]),
388 { .tcp = { htons(array[4] << 8 | array[5]) } },
390 exp->mask = ((struct ip_conntrack_tuple)
391 { { 0xFFFFFFFF, { 0 } },
392 { 0xFFFFFFFF, { .tcp = { 0xFFFF } }, 0xFFFF }});
394 exp->expectfn = NULL;
396 /* Ignore failure; should only happen with NAT */
397 ip_conntrack_expect_related(exp, ct);
400 UNLOCK_BH(&ip_ftp_lock);
404 static struct ip_conntrack_helper ftp[MAX_PORTS];
405 static char ftp_names[MAX_PORTS][10];
407 /* Not __exit: called from init() */
408 static void fini(void)
411 for (i = 0; i < ports_c; i++) {
412 DEBUGP("ip_ct_ftp: unregistering helper for port %d\n",
414 ip_conntrack_helper_unregister(&ftp[i]);
418 static int __init init(void)
426 for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
427 ftp[i].tuple.src.u.tcp.port = htons(ports[i]);
428 ftp[i].tuple.dst.protonum = IPPROTO_TCP;
429 ftp[i].mask.src.u.tcp.port = 0xFFFF;
430 ftp[i].mask.dst.protonum = 0xFFFF;
431 ftp[i].max_expected = 1;
433 ftp[i].flags = IP_CT_HELPER_F_REUSE_EXPECT;
434 ftp[i].me = ip_conntrack_ftp;
437 tmpname = &ftp_names[i][0];
438 if (ports[i] == FTP_PORT)
439 sprintf(tmpname, "ftp");
441 sprintf(tmpname, "ftp-%d", ports[i]);
442 ftp[i].name = tmpname;
444 DEBUGP("ip_ct_ftp: registering helper for port %d\n",
446 ret = ip_conntrack_helper_register(&ftp[i]);
457 PROVIDES_CONNTRACK(ftp);
458 EXPORT_SYMBOL(ip_ftp_lock);