patch-2.4.4 linux/net/ipv4/netfilter/ip_conntrack_ftp.c
Next file: linux/net/ipv4/netfilter/ip_conntrack_proto_generic.c
Previous file: linux/net/ipv4/netfilter/ip_conntrack_core.c
Back to the patch index
Back to the overall index
- Lines: 374
- Date:
Wed Apr 25 15:02:09 2001
- Orig file:
v2.4.3/linux/net/ipv4/netfilter/ip_conntrack_ftp.c
- Orig date:
Thu Aug 10 12:35:15 2000
diff -u --recursive --new-file v2.4.3/linux/net/ipv4/netfilter/ip_conntrack_ftp.c linux/net/ipv4/netfilter/ip_conntrack_ftp.c
@@ -2,6 +2,7 @@
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
+#include <linux/ctype.h>
#include <net/checksum.h>
#include <net/tcp.h>
@@ -12,8 +13,15 @@
DECLARE_LOCK(ip_ftp_lock);
struct module *ip_conntrack_ftp = THIS_MODULE;
-#define SERVER_STRING "227 Entering Passive Mode ("
-#define CLIENT_STRING "PORT "
+#define MAX_PORTS 8
+static int ports[MAX_PORTS];
+static int ports_c;
+#ifdef MODULE_PARM
+MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
+#endif
+
+static int loose = 0;
+MODULE_PARM(loose, "i");
#if 0
#define DEBUGP printk
@@ -21,32 +29,63 @@
#define DEBUGP(format, args...)
#endif
-static struct {
+static int try_rfc959(const char *, size_t, u_int32_t [], char);
+static int try_eprt(const char *, size_t, u_int32_t [], char);
+static int try_espv_response(const char *, size_t, u_int32_t [], char);
+
+static struct ftp_search {
+ enum ip_conntrack_dir dir;
const char *pattern;
size_t plen;
+ char skip;
char term;
-} search[2] = {
- [IP_CT_FTP_PORT] { CLIENT_STRING, sizeof(CLIENT_STRING) - 1, '\r' },
- [IP_CT_FTP_PASV] { SERVER_STRING, sizeof(SERVER_STRING) - 1, ')' }
+ enum ip_ct_ftp_type ftptype;
+ int (*getnum)(const char *, size_t, u_int32_t[], char);
+} search[] = {
+ {
+ IP_CT_DIR_ORIGINAL,
+ "PORT", sizeof("PORT") - 1, ' ', '\r',
+ IP_CT_FTP_PORT,
+ try_rfc959,
+ },
+ {
+ IP_CT_DIR_REPLY,
+ "227 ", sizeof("227 ") - 1, '(', ')',
+ IP_CT_FTP_PASV,
+ try_rfc959,
+ },
+ {
+ IP_CT_DIR_ORIGINAL,
+ "EPRT", sizeof("EPRT") - 1, ' ', '\r',
+ IP_CT_FTP_EPRT,
+ try_eprt,
+ },
+ {
+ IP_CT_DIR_REPLY,
+ "229 ", sizeof("229 ") - 1, '(', ')',
+ IP_CT_FTP_EPSV,
+ try_espv_response,
+ },
};
-/* Returns 0, or length of numbers */
-static int try_number(const char *data, size_t dlen, u_int32_t array[6],
- char term)
+static int try_number(const char *data, size_t dlen, u_int32_t array[],
+ int array_size, char sep, char term)
{
u_int32_t i, len;
+ memset(array, 0, sizeof(array[0])*array_size);
+
/* Keep data pointing at next char. */
- for (i = 0, len = 0; len < dlen; len++, data++) {
+ for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
if (*data >= '0' && *data <= '9') {
array[i] = array[i]*10 + *data - '0';
}
- else if (*data == ',')
+ else if (*data == sep)
i++;
else {
/* Unexpected character; true if it's the
terminator and we're finished. */
- if (*data == term && i == 5)
+ if (*data == term && i == array_size - 1)
return len;
DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
@@ -54,22 +93,100 @@
return 0;
}
}
+ DEBUGP("Failed to fill %u numbers separated by %c\n", array_size, sep);
return 0;
}
+/* Returns 0, or length of numbers: 192,168,1,1,5,6 */
+static int try_rfc959(const char *data, size_t dlen, u_int32_t array[6],
+ char term)
+{
+ return try_number(data, dlen, array, 6, ',', term);
+}
+
+/* Grab port: number up to delimiter */
+static int get_port(const char *data, int start, size_t dlen, char delim,
+ u_int32_t array[2])
+{
+ u_int16_t port = 0;
+ int i;
+
+ for (i = start; i < dlen; i++) {
+ /* Finished? */
+ if (data[i] == delim) {
+ if (port == 0)
+ break;
+ array[0] = port >> 8;
+ array[1] = port;
+ return i + 1;
+ }
+ else if (data[i] >= '0' && data[i] <= '9')
+ port = port*10 + data[i] - '0';
+ else /* Some other crap */
+ break;
+ }
+ return 0;
+}
+
+/* Returns 0, or length of numbers: |1|132.235.1.2|6275| */
+static int try_eprt(const char *data, size_t dlen, u_int32_t array[6],
+ char term)
+{
+ char delim;
+ int length;
+
+ /* First character is delimiter, then "1" for IPv4, then
+ delimiter again. */
+ if (dlen <= 3) return 0;
+ delim = data[0];
+ if (isdigit(delim) || delim < 33 || delim > 126
+ || data[1] != '1' || data[2] != delim)
+ return 0;
+
+ DEBUGP("EPRT: Got |1|!\n");
+ /* Now we have IP address. */
+ length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
+ if (length == 0)
+ return 0;
+
+ DEBUGP("EPRT: Got IP address!\n");
+ /* Start offset includes initial "|1|", and trailing delimiter */
+ return get_port(data, 3 + length + 1, dlen, delim, array+4);
+}
+
+/* Returns 0, or length of numbers: |||6446| */
+static int try_espv_response(const char *data, size_t dlen, u_int32_t array[6],
+ char term)
+{
+ char delim;
+
+ /* Three delimiters. */
+ if (dlen <= 3) return 0;
+ delim = data[0];
+ if (isdigit(delim) || delim < 33 || delim > 126
+ || data[1] != delim || data[2] != delim)
+ return 0;
+
+ return get_port(data, 3, dlen, delim, array+4);
+}
+
/* Return 1 for match, 0 for accept, -1 for partial. */
static int find_pattern(const char *data, size_t dlen,
const char *pattern, size_t plen,
- char term,
+ char skip, char term,
unsigned int *numoff,
unsigned int *numlen,
- u_int32_t array[6])
+ u_int32_t array[6],
+ int (*getnum)(const char *, size_t, u_int32_t[], char))
{
+ size_t i;
+
+ DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
if (dlen == 0)
return 0;
- if (dlen < plen) {
+ if (dlen <= plen) {
/* Short packet: try for partial? */
if (strnicmp(data, pattern, dlen) == 0)
return -1;
@@ -90,11 +207,23 @@
return 0;
}
- *numoff = plen;
- *numlen = try_number(data + plen, dlen - plen, array, term);
+ DEBUGP("Pattern matches!\n");
+ /* Now we've found the constant string, try to skip
+ to the 'skip' character */
+ for (i = plen; data[i] != skip; i++)
+ if (i == dlen - 1) return -1;
+
+ /* Skip over the last character */
+ i++;
+
+ DEBUGP("Skipped up to `%c'!\n", skip);
+
+ *numoff = i;
+ *numlen = getnum(data + i, dlen - i, array, term);
if (!*numlen)
return -1;
+ DEBUGP("Match succeeded!\n");
return 1;
}
@@ -115,6 +244,8 @@
unsigned int matchlen, matchoff;
struct ip_conntrack_tuple t, mask;
struct ip_ct_ftp *info = &ct->help.ct_ftp_info;
+ unsigned int i;
+ int found = 0;
/* Until there's been traffic both ways, don't look in packets. */
if (ctinfo != IP_CT_ESTABLISHED
@@ -163,25 +294,38 @@
return NF_ACCEPT;
}
- switch (find_pattern(data, datalen,
- search[dir].pattern,
- search[dir].plen, search[dir].term,
- &matchoff, &matchlen,
- array)) {
- case -1: /* partial */
+ /* Initialize IP array to expected address (it's not mentioned
+ in EPSV responses) */
+ array[0] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 24) & 0xFF;
+ array[1] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 16) & 0xFF;
+ array[2] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 8) & 0xFF;
+ array[3] = ntohl(ct->tuplehash[dir].tuple.src.ip) & 0xFF;
+
+ for (i = 0; i < sizeof(search) / sizeof(search[0]); i++) {
+ if (search[i].dir != dir) continue;
+
+ found = find_pattern(data, datalen,
+ search[i].pattern,
+ search[i].plen,
+ search[i].skip,
+ search[i].term,
+ &matchoff, &matchlen,
+ array,
+ search[i].getnum);
+ if (found) break;
+ }
+ if (found == -1) {
/* We don't usually drop packets. After all, this is
connection tracking, not packet filtering.
However, it is neccessary for accurate tracking in
this case. */
if (net_ratelimit())
- printk("conntrack_ftp: partial %u+%u\n",
+ printk("conntrack_ftp: partial %s %u+%u\n",
+ search[i].pattern,
ntohl(tcph->seq), datalen);
return NF_DROP;
-
- case 0: /* no match */
- DEBUGP("ip_conntrack_ftp_help: no match\n");
+ } else if (found == 0) /* No match */
return NF_ACCEPT;
- }
DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
(int)matchlen, data + matchoff,
@@ -194,7 +338,7 @@
info->is_ftp = 1;
info->seq = ntohl(tcph->seq) + matchoff;
info->len = matchlen;
- info->ftptype = dir;
+ info->ftptype = search[i].ftptype;
info->port = array[4] << 8 | array[5];
} else {
/* Enrico Scholz's passive FTP to partially RNAT'd ftp
@@ -204,6 +348,12 @@
DEBUGP("conntrack_ftp: NOT RECORDING: %u,%u,%u,%u != %u.%u.%u.%u\n",
array[0], array[1], array[2], array[3],
NIPQUAD(ct->tuplehash[dir].tuple.src.ip));
+
+ /* Thanks to Cristiano Lincoln Mattos
+ <lincoln@cesar.org.br> for reporting this potential
+ problem (DMZ machines opening holes to internal
+ networks, or the packet filter itself). */
+ if (!loose) goto out;
}
t = ((struct ip_conntrack_tuple)
@@ -218,27 +368,52 @@
{ 0xFFFFFFFF, { 0xFFFF }, 0xFFFF }});
/* Ignore failure; should only happen with NAT */
ip_conntrack_expect_related(ct, &t, &mask, NULL);
+ out:
UNLOCK_BH(&ip_ftp_lock);
return NF_ACCEPT;
}
-static struct ip_conntrack_helper ftp = { { NULL, NULL },
- { { 0, { __constant_htons(21) } },
- { 0, { 0 }, IPPROTO_TCP } },
- { { 0, { 0xFFFF } },
- { 0, { 0 }, 0xFFFF } },
- help };
+static struct ip_conntrack_helper ftp[MAX_PORTS];
-static int __init init(void)
+/* Not __exit: called from init() */
+static void fini(void)
{
- return ip_conntrack_helper_register(&ftp);
+ int i;
+ for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
+ DEBUGP("ip_ct_ftp: unregistering helper for port %d\n",
+ ports[i]);
+ ip_conntrack_helper_unregister(&ftp[i]);
+ }
}
-static void __exit fini(void)
+static int __init init(void)
{
- ip_conntrack_helper_unregister(&ftp);
+ int i, ret;
+
+ if (ports[0] == 0)
+ ports[0] = 21;
+
+ for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
+ memset(&ftp[i], 0, sizeof(struct ip_conntrack_helper));
+ ftp[i].tuple.src.u.tcp.port = htons(ports[i]);
+ ftp[i].tuple.dst.protonum = IPPROTO_TCP;
+ ftp[i].mask.src.u.tcp.port = 0xFFFF;
+ ftp[i].mask.dst.protonum = 0xFFFF;
+ ftp[i].help = help;
+ DEBUGP("ip_ct_ftp: registering helper for port %d\n",
+ ports[i]);
+ ret = ip_conntrack_helper_register(&ftp[i]);
+
+ if (ret) {
+ fini();
+ return ret;
+ }
+ ports_c++;
+ }
+ return 0;
}
+
EXPORT_SYMBOL(ip_ftp_lock);
EXPORT_SYMBOL(ip_conntrack_ftp);
FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)