Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* FTP extension for connection tracking. */ |
| 2 | |
| 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> |
| 6 | * |
| 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. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/ip.h> |
| 16 | #include <linux/ipv6.h> |
| 17 | #include <linux/ctype.h> |
David S. Miller | 6a28ec8 | 2006-08-26 19:48:49 -0700 | [diff] [blame] | 18 | #include <linux/inet.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 19 | #include <net/checksum.h> |
| 20 | #include <net/tcp.h> |
| 21 | |
| 22 | #include <net/netfilter/nf_conntrack.h> |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_expect.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_ecache.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_helper.h> |
| 26 | #include <linux/netfilter/nf_conntrack_ftp.h> |
| 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>"); |
| 30 | MODULE_DESCRIPTION("ftp connection tracking helper"); |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 31 | MODULE_ALIAS("ip_conntrack_ftp"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 32 | MODULE_ALIAS_NFCT_HELPER("ftp"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 33 | |
| 34 | /* This is slow, but it's simple. --RR */ |
| 35 | static char *ftp_buffer; |
| 36 | |
| 37 | static DEFINE_SPINLOCK(nf_ftp_lock); |
| 38 | |
| 39 | #define MAX_PORTS 8 |
| 40 | static u_int16_t ports[MAX_PORTS]; |
| 41 | static unsigned int ports_c; |
| 42 | module_param_array(ports, ushort, &ports_c, 0400); |
| 43 | |
| 44 | static int loose; |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 45 | module_param(loose, bool, 0600); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 46 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 47 | unsigned int (*nf_nat_ftp_hook)(struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 48 | enum ip_conntrack_info ctinfo, |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 49 | enum nf_ct_ftp_type type, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 50 | unsigned int matchoff, |
| 51 | unsigned int matchlen, |
Patrick McHardy | 25b86e0 | 2007-05-24 16:41:50 -0700 | [diff] [blame] | 52 | struct nf_conntrack_expect *exp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(nf_nat_ftp_hook); |
| 54 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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 *, |
| 58 | char); |
| 59 | |
| 60 | static struct ftp_search { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 61 | const char *pattern; |
| 62 | size_t plen; |
| 63 | char skip; |
| 64 | char term; |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 65 | enum nf_ct_ftp_type ftptype; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 66 | int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char); |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 67 | } search[IP_CT_DIR_MAX][2] = { |
| 68 | [IP_CT_DIR_ORIGINAL] = { |
| 69 | { |
| 70 | .pattern = "PORT", |
| 71 | .plen = sizeof("PORT") - 1, |
| 72 | .skip = ' ', |
| 73 | .term = '\r', |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 74 | .ftptype = NF_CT_FTP_PORT, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 75 | .getnum = try_rfc959, |
| 76 | }, |
| 77 | { |
| 78 | .pattern = "EPRT", |
| 79 | .plen = sizeof("EPRT") - 1, |
| 80 | .skip = ' ', |
| 81 | .term = '\r', |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 82 | .ftptype = NF_CT_FTP_EPRT, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 83 | .getnum = try_eprt, |
| 84 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 85 | }, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 86 | [IP_CT_DIR_REPLY] = { |
| 87 | { |
| 88 | .pattern = "227 ", |
| 89 | .plen = sizeof("227 ") - 1, |
| 90 | .skip = '(', |
| 91 | .term = ')', |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 92 | .ftptype = NF_CT_FTP_PASV, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 93 | .getnum = try_rfc959, |
| 94 | }, |
| 95 | { |
| 96 | .pattern = "229 ", |
| 97 | .plen = sizeof("229 ") - 1, |
| 98 | .skip = '(', |
| 99 | .term = ')', |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 100 | .ftptype = NF_CT_FTP_EPSV, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 101 | .getnum = try_epsv_response, |
| 102 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 103 | }, |
| 104 | }; |
| 105 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 106 | static int |
| 107 | get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term) |
| 108 | { |
David S. Miller | 6a28ec8 | 2006-08-26 19:48:49 -0700 | [diff] [blame] | 109 | const char *end; |
| 110 | int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end); |
YOSHIFUJI Hideaki | 1884f78 | 2006-06-19 03:20:32 +0900 | [diff] [blame] | 111 | if (ret > 0) |
| 112 | return (int)(end - src); |
| 113 | return 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int try_number(const char *data, size_t dlen, u_int32_t array[], |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 117 | int array_size, char sep, char term) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 118 | { |
| 119 | u_int32_t i, len; |
| 120 | |
| 121 | memset(array, 0, sizeof(array[0])*array_size); |
| 122 | |
| 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'; |
| 127 | } |
| 128 | else if (*data == sep) |
| 129 | i++; |
| 130 | else { |
| 131 | /* Unexpected character; true if it's the |
| 132 | terminator and we're finished. */ |
| 133 | if (*data == term && i == array_size - 1) |
| 134 | return len; |
| 135 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 136 | pr_debug("Char %u (got %u nums) `%u' unexpected\n", |
| 137 | len, i, *data); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | } |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 141 | pr_debug("Failed to fill %u numbers separated by %c\n", |
| 142 | array_size, sep); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 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) |
| 149 | { |
| 150 | int length; |
| 151 | u_int32_t array[6]; |
| 152 | |
| 153 | length = try_number(data, dlen, array, 6, ',', term); |
| 154 | if (length == 0) |
| 155 | return 0; |
| 156 | |
| 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]); |
| 160 | return length; |
| 161 | } |
| 162 | |
| 163 | /* Grab port: number up to delimiter */ |
| 164 | static int get_port(const char *data, int start, size_t dlen, char delim, |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 165 | __be16 *port) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 166 | { |
| 167 | u_int16_t tmp_port = 0; |
| 168 | int i; |
| 169 | |
| 170 | for (i = start; i < dlen; i++) { |
| 171 | /* Finished? */ |
| 172 | if (data[i] == delim) { |
| 173 | if (tmp_port == 0) |
| 174 | break; |
| 175 | *port = htons(tmp_port); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 176 | pr_debug("get_port: return %d\n", tmp_port); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 177 | return i + 1; |
| 178 | } |
| 179 | else if (data[i] >= '0' && data[i] <= '9') |
| 180 | tmp_port = tmp_port*10 + data[i] - '0'; |
| 181 | else { /* Some other crap */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 182 | pr_debug("get_port: invalid char.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 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, |
| 191 | char term) |
| 192 | { |
| 193 | char delim; |
| 194 | int length; |
| 195 | |
| 196 | /* First character is delimiter, then "1" for IPv4 or "2" for IPv6, |
| 197 | then delimiter again. */ |
| 198 | if (dlen <= 3) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 199 | pr_debug("EPRT: too short\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | delim = data[0]; |
| 203 | if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 204 | pr_debug("try_eprt: invalid delimitter.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | if ((cmd->l3num == PF_INET && data[1] != '1') || |
| 209 | (cmd->l3num == PF_INET6 && data[1] != '2')) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 210 | pr_debug("EPRT: invalid protocol number.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 214 | pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 215 | |
| 216 | if (data[1] == '1') { |
| 217 | u_int32_t array[4]; |
| 218 | |
| 219 | /* Now we have IP address. */ |
| 220 | length = try_number(data + 3, dlen - 3, array, 4, '.', delim); |
| 221 | if (length != 0) |
| 222 | cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
| 223 | | (array[2] << 8) | array[3]); |
| 224 | } else { |
| 225 | /* Now we have IPv6 address. */ |
| 226 | length = get_ipv6_addr(data + 3, dlen - 3, |
| 227 | (struct in6_addr *)cmd->u3.ip6, delim); |
| 228 | } |
| 229 | |
| 230 | if (length == 0) |
| 231 | return 0; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 232 | pr_debug("EPRT: Got IP address!\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 233 | /* Start offset includes initial "|1|", and trailing delimiter */ |
| 234 | return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port); |
| 235 | } |
| 236 | |
| 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) |
| 240 | { |
| 241 | char delim; |
| 242 | |
| 243 | /* Three delimiters. */ |
| 244 | if (dlen <= 3) return 0; |
| 245 | delim = data[0]; |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 246 | if (isdigit(delim) || delim < 33 || delim > 126 || |
| 247 | data[1] != delim || data[2] != delim) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 248 | return 0; |
| 249 | |
| 250 | return get_port(data, 3, dlen, delim, &cmd->u.tcp.port); |
| 251 | } |
| 252 | |
| 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)) |
| 262 | { |
| 263 | size_t i; |
| 264 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 265 | pr_debug("find_pattern `%s': dlen = %Zu\n", pattern, dlen); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 266 | if (dlen == 0) |
| 267 | return 0; |
| 268 | |
| 269 | if (dlen <= plen) { |
| 270 | /* Short packet: try for partial? */ |
| 271 | if (strnicmp(data, pattern, dlen) == 0) |
| 272 | return -1; |
| 273 | else return 0; |
| 274 | } |
| 275 | |
| 276 | if (strnicmp(data, pattern, plen) != 0) { |
| 277 | #if 0 |
| 278 | size_t i; |
| 279 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 280 | pr_debug("ftp: string mismatch\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 281 | for (i = 0; i < plen; i++) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 282 | pr_debug("ftp:char %u `%c'(%u) vs `%c'(%u)\n", |
| 283 | i, data[i], data[i], |
| 284 | pattern[i], pattern[i]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 285 | } |
| 286 | #endif |
| 287 | return 0; |
| 288 | } |
| 289 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 290 | pr_debug("Pattern matches!\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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; |
| 295 | |
| 296 | /* Skip over the last character */ |
| 297 | i++; |
| 298 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 299 | pr_debug("Skipped up to `%c'!\n", skip); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 300 | |
| 301 | *numoff = i; |
| 302 | *numlen = getnum(data + i, dlen - i, cmd, term); |
| 303 | if (!*numlen) |
| 304 | return -1; |
| 305 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 306 | pr_debug("Match succeeded!\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | /* Look up to see if we're just after a \n. */ |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 311 | static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 312 | { |
| 313 | unsigned int i; |
| 314 | |
| 315 | for (i = 0; i < info->seq_aft_nl_num[dir]; i++) |
| 316 | if (info->seq_aft_nl[dir][i] == seq) |
| 317 | return 1; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* We don't update if it's older than what we have. */ |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 322 | static void update_nl_seq(struct nf_conn *ct, u32 nl_seq, |
| 323 | struct nf_ct_ftp_master *info, int dir, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 324 | struct sk_buff *skb) |
| 325 | { |
Patrick McHardy | aaff23a | 2010-01-07 18:33:18 +0100 | [diff] [blame] | 326 | unsigned int i, oldest; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 327 | |
| 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) |
| 331 | return; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) { |
| 335 | info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq; |
Patrick McHardy | aaff23a | 2010-01-07 18:33:18 +0100 | [diff] [blame] | 336 | } else { |
| 337 | if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1])) |
| 338 | oldest = 0; |
| 339 | else |
| 340 | oldest = 1; |
| 341 | |
| 342 | if (after(nl_seq, info->seq_aft_nl[dir][oldest])) |
| 343 | info->seq_aft_nl[dir][oldest] = nl_seq; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 347 | static int help(struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 348 | unsigned int protoff, |
| 349 | struct nf_conn *ct, |
| 350 | enum ip_conntrack_info ctinfo) |
| 351 | { |
| 352 | unsigned int dataoff, datalen; |
Jan Engelhardt | 58c0fb0 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 353 | const struct tcphdr *th; |
| 354 | struct tcphdr _tcph; |
| 355 | const char *fb_ptr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 356 | int ret; |
| 357 | u32 seq; |
| 358 | int dir = CTINFO2DIR(ctinfo); |
Ingo Molnar | d6e8cc6 | 2008-11-25 18:23:03 +0100 | [diff] [blame] | 359 | unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff); |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 360 | struct nf_ct_ftp_master *ct_ftp_info = &nfct_help(ct)->help.ct_ftp_info; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 361 | struct nf_conntrack_expect *exp; |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 362 | union nf_inet_addr *daddr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 363 | struct nf_conntrack_man cmd = {}; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 364 | unsigned int i; |
| 365 | int found = 0, ends_in_nl; |
Patrick McHardy | 337fbc4 | 2006-11-29 02:35:25 +0100 | [diff] [blame] | 366 | typeof(nf_nat_ftp_hook) nf_nat_ftp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 367 | |
| 368 | /* Until there's been traffic both ways, don't look in packets. */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 369 | if (ctinfo != IP_CT_ESTABLISHED && |
| 370 | ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 371 | pr_debug("ftp: Conntrackinfo = %u\n", ctinfo); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 372 | return NF_ACCEPT; |
| 373 | } |
| 374 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 375 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 376 | if (th == NULL) |
| 377 | return NF_ACCEPT; |
| 378 | |
| 379 | dataoff = protoff + th->doff * 4; |
| 380 | /* No data? */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 381 | if (dataoff >= skb->len) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 382 | pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 383 | skb->len); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 384 | return NF_ACCEPT; |
| 385 | } |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 386 | datalen = skb->len - dataoff; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 387 | |
| 388 | spin_lock_bh(&nf_ftp_lock); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 389 | fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 390 | BUG_ON(fb_ptr == NULL); |
| 391 | |
| 392 | ends_in_nl = (fb_ptr[datalen - 1] == '\n'); |
| 393 | seq = ntohl(th->seq) + datalen; |
| 394 | |
| 395 | /* Look up to see if we're just after a \n. */ |
| 396 | if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) { |
| 397 | /* Now if this ends in \n, update ftp info. */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 398 | pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n", |
| 399 | ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)", |
| 400 | ct_ftp_info->seq_aft_nl[dir][0], |
| 401 | ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)", |
| 402 | ct_ftp_info->seq_aft_nl[dir][1]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | ret = NF_ACCEPT; |
| 404 | goto out_update_nl; |
| 405 | } |
| 406 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 407 | /* Initialize IP/IPv6 addr to expected address (it's not mentioned |
| 408 | in EPSV responses) */ |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 409 | cmd.l3num = nf_ct_l3num(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 410 | memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all, |
| 411 | sizeof(cmd.u3.all)); |
| 412 | |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 413 | for (i = 0; i < ARRAY_SIZE(search[dir]); i++) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 414 | found = find_pattern(fb_ptr, datalen, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 415 | search[dir][i].pattern, |
| 416 | search[dir][i].plen, |
| 417 | search[dir][i].skip, |
| 418 | search[dir][i].term, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 419 | &matchoff, &matchlen, |
| 420 | &cmd, |
Patrick McHardy | 7d8c501 | 2006-05-29 18:25:38 -0700 | [diff] [blame] | 421 | search[dir][i].getnum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 422 | if (found) break; |
| 423 | } |
| 424 | if (found == -1) { |
| 425 | /* We don't usually drop packets. After all, this is |
| 426 | connection tracking, not packet filtering. |
| 427 | However, it is necessary for accurate tracking in |
| 428 | this case. */ |
Patrick McHardy | 4813ead | 2008-11-24 18:34:48 +0100 | [diff] [blame] | 429 | pr_debug("conntrack_ftp: partial %s %u+%u\n", |
| 430 | search[dir][i].pattern, ntohl(th->seq), datalen); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 431 | ret = NF_DROP; |
| 432 | goto out; |
| 433 | } else if (found == 0) { /* No match */ |
| 434 | ret = NF_ACCEPT; |
| 435 | goto out_update_nl; |
| 436 | } |
| 437 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 438 | pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n", |
| 439 | matchlen, fb_ptr + matchoff, |
| 440 | matchlen, ntohl(th->seq) + matchoff); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 441 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 442 | exp = nf_ct_expect_alloc(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 443 | if (exp == NULL) { |
| 444 | ret = NF_DROP; |
| 445 | goto out; |
| 446 | } |
| 447 | |
| 448 | /* We refer to the reverse direction ("!dir") tuples here, |
| 449 | * because we're expecting something in the other direction. |
| 450 | * Doesn't matter unless NAT is happening. */ |
Patrick McHardy | df43b4e | 2007-07-07 22:31:07 -0700 | [diff] [blame] | 451 | daddr = &ct->tuplehash[!dir].tuple.dst.u3; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 452 | |
| 453 | /* Update the ftp info */ |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 454 | if ((cmd.l3num == nf_ct_l3num(ct)) && |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 455 | memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all, |
| 456 | sizeof(cmd.u3.all))) { |
| 457 | /* Enrico Scholz's passive FTP to partially RNAT'd ftp |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 458 | server: it really wants us to connect to a |
| 459 | different IP address. Simply don't record it for |
| 460 | NAT. */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 461 | if (cmd.l3num == PF_INET) { |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 462 | pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n", |
| 463 | &cmd.u3.ip, |
| 464 | &ct->tuplehash[dir].tuple.src.u3.ip); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 465 | } else { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 466 | pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n", |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 467 | cmd.u3.ip6, |
| 468 | ct->tuplehash[dir].tuple.src.u3.ip6); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* Thanks to Cristiano Lincoln Mattos |
| 472 | <lincoln@cesar.org.br> for reporting this potential |
| 473 | problem (DMZ machines opening holes to internal |
| 474 | networks, or the packet filter itself). */ |
| 475 | if (!loose) { |
| 476 | ret = NF_ACCEPT; |
| 477 | goto out_put_expect; |
| 478 | } |
Patrick McHardy | df43b4e | 2007-07-07 22:31:07 -0700 | [diff] [blame] | 479 | daddr = &cmd.u3; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 482 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num, |
Patrick McHardy | df43b4e | 2007-07-07 22:31:07 -0700 | [diff] [blame] | 483 | &ct->tuplehash[!dir].tuple.src.u3, daddr, |
| 484 | IPPROTO_TCP, NULL, &cmd.u.tcp.port); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 485 | |
| 486 | /* Now, NAT might want to mangle the packet, and register the |
| 487 | * (possibly changed) expectation itself. */ |
Patrick McHardy | 337fbc4 | 2006-11-29 02:35:25 +0100 | [diff] [blame] | 488 | nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook); |
Jozsef Kadlecsik | 55a7332 | 2006-12-02 22:07:44 -0800 | [diff] [blame] | 489 | if (nf_nat_ftp && ct->status & IPS_NAT_MASK) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 490 | ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype, |
Patrick McHardy | 25b86e0 | 2007-05-24 16:41:50 -0700 | [diff] [blame] | 491 | matchoff, matchlen, exp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 492 | else { |
| 493 | /* Can't expect this? Best to drop packet now. */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 494 | if (nf_ct_expect_related(exp) != 0) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 495 | ret = NF_DROP; |
| 496 | else |
| 497 | ret = NF_ACCEPT; |
| 498 | } |
| 499 | |
| 500 | out_put_expect: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 501 | nf_ct_expect_put(exp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 502 | |
| 503 | out_update_nl: |
| 504 | /* Now if this ends in \n, update ftp info. Seq may have been |
| 505 | * adjusted by NAT code. */ |
| 506 | if (ends_in_nl) |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 507 | update_nl_seq(ct, seq, ct_ftp_info, dir, skb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 508 | out: |
| 509 | spin_unlock_bh(&nf_ftp_lock); |
| 510 | return ret; |
| 511 | } |
| 512 | |
Patrick McHardy | ec59a11 | 2007-07-07 22:37:03 -0700 | [diff] [blame] | 513 | static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly; |
| 514 | static char ftp_names[MAX_PORTS][2][sizeof("ftp-65535")] __read_mostly; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 515 | |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 516 | static const struct nf_conntrack_expect_policy ftp_exp_policy = { |
| 517 | .max_expected = 1, |
| 518 | .timeout = 5 * 60, |
| 519 | }; |
| 520 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 521 | /* don't make this __exit, since it's called from __init ! */ |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 522 | static void nf_conntrack_ftp_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 523 | { |
| 524 | int i, j; |
| 525 | for (i = 0; i < ports_c; i++) { |
| 526 | for (j = 0; j < 2; j++) { |
| 527 | if (ftp[i][j].me == NULL) |
| 528 | continue; |
| 529 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 530 | pr_debug("nf_ct_ftp: unregistering helper for pf: %d " |
| 531 | "port: %d\n", |
| 532 | ftp[i][j].tuple.src.l3num, ports[i]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 533 | nf_conntrack_helper_unregister(&ftp[i][j]); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | kfree(ftp_buffer); |
| 538 | } |
| 539 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 540 | static int __init nf_conntrack_ftp_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 541 | { |
| 542 | int i, j = -1, ret = 0; |
| 543 | char *tmpname; |
| 544 | |
| 545 | ftp_buffer = kmalloc(65536, GFP_KERNEL); |
| 546 | if (!ftp_buffer) |
| 547 | return -ENOMEM; |
| 548 | |
| 549 | if (ports_c == 0) |
| 550 | ports[ports_c++] = FTP_PORT; |
| 551 | |
| 552 | /* FIXME should be configurable whether IPv4 and IPv6 FTP connections |
| 553 | are tracked or not - YK */ |
| 554 | for (i = 0; i < ports_c; i++) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 555 | ftp[i][0].tuple.src.l3num = PF_INET; |
| 556 | ftp[i][1].tuple.src.l3num = PF_INET6; |
| 557 | for (j = 0; j < 2; j++) { |
| 558 | ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]); |
| 559 | ftp[i][j].tuple.dst.protonum = IPPROTO_TCP; |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 560 | ftp[i][j].expect_policy = &ftp_exp_policy; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 561 | ftp[i][j].me = THIS_MODULE; |
| 562 | ftp[i][j].help = help; |
| 563 | tmpname = &ftp_names[i][j][0]; |
| 564 | if (ports[i] == FTP_PORT) |
| 565 | sprintf(tmpname, "ftp"); |
| 566 | else |
| 567 | sprintf(tmpname, "ftp-%d", ports[i]); |
| 568 | ftp[i][j].name = tmpname; |
| 569 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 570 | pr_debug("nf_ct_ftp: registering helper for pf: %d " |
| 571 | "port: %d\n", |
| 572 | ftp[i][j].tuple.src.l3num, ports[i]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 573 | ret = nf_conntrack_helper_register(&ftp[i][j]); |
| 574 | if (ret) { |
| 575 | printk("nf_ct_ftp: failed to register helper " |
| 576 | " for pf: %d port: %d\n", |
| 577 | ftp[i][j].tuple.src.l3num, ports[i]); |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 578 | nf_conntrack_ftp_fini(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 579 | return ret; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 587 | module_init(nf_conntrack_ftp_init); |
| 588 | module_exit(nf_conntrack_ftp_fini); |