blob: e17cb7c7dd8fda61efabf12d8cefb45c4c8d4a78 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* 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 Kozakai9fb9cbb2005-11-09 16:38:16 -080010 */
11
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/netfilter.h>
15#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <linux/ipv6.h>
18#include <linux/ctype.h>
David S. Miller6a28ec82006-08-26 19:48:49 -070019#include <linux/inet.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#include <net/checksum.h>
21#include <net/tcp.h>
22
23#include <net/netfilter/nf_conntrack.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010024#include <net/netfilter/nf_conntrack_expect.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010025#include <net/netfilter/nf_conntrack_ecache.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026#include <net/netfilter/nf_conntrack_helper.h>
27#include <linux/netfilter/nf_conntrack_ftp.h>
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
31MODULE_DESCRIPTION("ftp connection tracking helper");
Patrick McHardyd2483dd2006-12-02 22:06:05 -080032MODULE_ALIAS("ip_conntrack_ftp");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010033MODULE_ALIAS_NFCT_HELPER("ftp");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034
35/* This is slow, but it's simple. --RR */
36static char *ftp_buffer;
37
38static DEFINE_SPINLOCK(nf_ftp_lock);
39
40#define MAX_PORTS 8
41static u_int16_t ports[MAX_PORTS];
42static unsigned int ports_c;
43module_param_array(ports, ushort, &ports_c, 0400);
44
45static int loose;
Patrick McHardye7be6992006-01-05 12:19:46 -080046module_param(loose, bool, 0600);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047
Herbert Xu3db05fe2007-10-15 00:53:15 -070048unsigned int (*nf_nat_ftp_hook)(struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049 enum ip_conntrack_info ctinfo,
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080050 enum nf_ct_ftp_type type,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051 unsigned int matchoff,
52 unsigned int matchlen,
Patrick McHardy25b86e02007-05-24 16:41:50 -070053 struct nf_conntrack_expect *exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054EXPORT_SYMBOL_GPL(nf_nat_ftp_hook);
55
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056static int try_rfc959(const char *, size_t, struct nf_conntrack_man *, char);
57static int try_eprt(const char *, size_t, struct nf_conntrack_man *, char);
58static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *,
59 char);
60
61static struct ftp_search {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080062 const char *pattern;
63 size_t plen;
64 char skip;
65 char term;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080066 enum nf_ct_ftp_type ftptype;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char);
Patrick McHardy7d8c5012006-05-29 18:25:38 -070068} search[IP_CT_DIR_MAX][2] = {
69 [IP_CT_DIR_ORIGINAL] = {
70 {
71 .pattern = "PORT",
72 .plen = sizeof("PORT") - 1,
73 .skip = ' ',
74 .term = '\r',
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080075 .ftptype = NF_CT_FTP_PORT,
Patrick McHardy7d8c5012006-05-29 18:25:38 -070076 .getnum = try_rfc959,
77 },
78 {
79 .pattern = "EPRT",
80 .plen = sizeof("EPRT") - 1,
81 .skip = ' ',
82 .term = '\r',
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080083 .ftptype = NF_CT_FTP_EPRT,
Patrick McHardy7d8c5012006-05-29 18:25:38 -070084 .getnum = try_eprt,
85 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 },
Patrick McHardy7d8c5012006-05-29 18:25:38 -070087 [IP_CT_DIR_REPLY] = {
88 {
89 .pattern = "227 ",
90 .plen = sizeof("227 ") - 1,
91 .skip = '(',
92 .term = ')',
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080093 .ftptype = NF_CT_FTP_PASV,
Patrick McHardy7d8c5012006-05-29 18:25:38 -070094 .getnum = try_rfc959,
95 },
96 {
97 .pattern = "229 ",
98 .plen = sizeof("229 ") - 1,
99 .skip = '(',
100 .term = ')',
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800101 .ftptype = NF_CT_FTP_EPSV,
Patrick McHardy7d8c5012006-05-29 18:25:38 -0700102 .getnum = try_epsv_response,
103 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104 },
105};
106
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107static int
108get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term)
109{
David S. Miller6a28ec82006-08-26 19:48:49 -0700110 const char *end;
111 int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end);
YOSHIFUJI Hideaki1884f782006-06-19 03:20:32 +0900112 if (ret > 0)
113 return (int)(end - src);
114 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115}
116
117static int try_number(const char *data, size_t dlen, u_int32_t array[],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800118 int array_size, char sep, char term)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119{
120 u_int32_t i, len;
121
122 memset(array, 0, sizeof(array[0])*array_size);
123
124 /* Keep data pointing at next char. */
125 for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
126 if (*data >= '0' && *data <= '9') {
127 array[i] = array[i]*10 + *data - '0';
128 }
129 else if (*data == sep)
130 i++;
131 else {
132 /* Unexpected character; true if it's the
133 terminator and we're finished. */
134 if (*data == term && i == array_size - 1)
135 return len;
136
Patrick McHardy0d537782007-07-07 22:39:38 -0700137 pr_debug("Char %u (got %u nums) `%u' unexpected\n",
138 len, i, *data);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139 return 0;
140 }
141 }
Patrick McHardy0d537782007-07-07 22:39:38 -0700142 pr_debug("Failed to fill %u numbers separated by %c\n",
143 array_size, sep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144 return 0;
145}
146
147/* Returns 0, or length of numbers: 192,168,1,1,5,6 */
148static int try_rfc959(const char *data, size_t dlen,
149 struct nf_conntrack_man *cmd, char term)
150{
151 int length;
152 u_int32_t array[6];
153
154 length = try_number(data, dlen, array, 6, ',', term);
155 if (length == 0)
156 return 0;
157
158 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
159 (array[2] << 8) | array[3]);
160 cmd->u.tcp.port = htons((array[4] << 8) | array[5]);
161 return length;
162}
163
164/* Grab port: number up to delimiter */
165static int get_port(const char *data, int start, size_t dlen, char delim,
Patrick McHardybff9a892006-12-02 22:05:08 -0800166 __be16 *port)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800167{
168 u_int16_t tmp_port = 0;
169 int i;
170
171 for (i = start; i < dlen; i++) {
172 /* Finished? */
173 if (data[i] == delim) {
174 if (tmp_port == 0)
175 break;
176 *port = htons(tmp_port);
Patrick McHardy0d537782007-07-07 22:39:38 -0700177 pr_debug("get_port: return %d\n", tmp_port);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178 return i + 1;
179 }
180 else if (data[i] >= '0' && data[i] <= '9')
181 tmp_port = tmp_port*10 + data[i] - '0';
182 else { /* Some other crap */
Patrick McHardy0d537782007-07-07 22:39:38 -0700183 pr_debug("get_port: invalid char.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184 break;
185 }
186 }
187 return 0;
188}
189
190/* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
191static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd,
192 char term)
193{
194 char delim;
195 int length;
196
197 /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
198 then delimiter again. */
199 if (dlen <= 3) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700200 pr_debug("EPRT: too short\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201 return 0;
202 }
203 delim = data[0];
204 if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700205 pr_debug("try_eprt: invalid delimitter.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800206 return 0;
207 }
208
209 if ((cmd->l3num == PF_INET && data[1] != '1') ||
210 (cmd->l3num == PF_INET6 && data[1] != '2')) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700211 pr_debug("EPRT: invalid protocol number.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212 return 0;
213 }
214
Patrick McHardy0d537782007-07-07 22:39:38 -0700215 pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800216
217 if (data[1] == '1') {
218 u_int32_t array[4];
219
220 /* Now we have IP address. */
221 length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
222 if (length != 0)
223 cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16)
224 | (array[2] << 8) | array[3]);
225 } else {
226 /* Now we have IPv6 address. */
227 length = get_ipv6_addr(data + 3, dlen - 3,
228 (struct in6_addr *)cmd->u3.ip6, delim);
229 }
230
231 if (length == 0)
232 return 0;
Patrick McHardy0d537782007-07-07 22:39:38 -0700233 pr_debug("EPRT: Got IP address!\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800234 /* Start offset includes initial "|1|", and trailing delimiter */
235 return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port);
236}
237
238/* Returns 0, or length of numbers: |||6446| */
239static int try_epsv_response(const char *data, size_t dlen,
240 struct nf_conntrack_man *cmd, char term)
241{
242 char delim;
243
244 /* Three delimiters. */
245 if (dlen <= 3) return 0;
246 delim = data[0];
Joe Perchesf64f9e72009-11-29 16:55:45 -0800247 if (isdigit(delim) || delim < 33 || delim > 126 ||
248 data[1] != delim || data[2] != delim)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800249 return 0;
250
251 return get_port(data, 3, dlen, delim, &cmd->u.tcp.port);
252}
253
254/* Return 1 for match, 0 for accept, -1 for partial. */
255static int find_pattern(const char *data, size_t dlen,
256 const char *pattern, size_t plen,
257 char skip, char term,
258 unsigned int *numoff,
259 unsigned int *numlen,
260 struct nf_conntrack_man *cmd,
261 int (*getnum)(const char *, size_t,
262 struct nf_conntrack_man *, char))
263{
264 size_t i;
265
Patrick McHardy0d537782007-07-07 22:39:38 -0700266 pr_debug("find_pattern `%s': dlen = %Zu\n", pattern, dlen);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267 if (dlen == 0)
268 return 0;
269
270 if (dlen <= plen) {
271 /* Short packet: try for partial? */
272 if (strnicmp(data, pattern, dlen) == 0)
273 return -1;
274 else return 0;
275 }
276
277 if (strnicmp(data, pattern, plen) != 0) {
278#if 0
279 size_t i;
280
Patrick McHardy0d537782007-07-07 22:39:38 -0700281 pr_debug("ftp: string mismatch\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800282 for (i = 0; i < plen; i++) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700283 pr_debug("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
284 i, data[i], data[i],
285 pattern[i], pattern[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286 }
287#endif
288 return 0;
289 }
290
Patrick McHardy0d537782007-07-07 22:39:38 -0700291 pr_debug("Pattern matches!\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292 /* Now we've found the constant string, try to skip
293 to the 'skip' character */
294 for (i = plen; data[i] != skip; i++)
295 if (i == dlen - 1) return -1;
296
297 /* Skip over the last character */
298 i++;
299
Patrick McHardy0d537782007-07-07 22:39:38 -0700300 pr_debug("Skipped up to `%c'!\n", skip);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301
302 *numoff = i;
303 *numlen = getnum(data + i, dlen - i, cmd, term);
304 if (!*numlen)
305 return -1;
306
Patrick McHardy0d537782007-07-07 22:39:38 -0700307 pr_debug("Match succeeded!\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800308 return 1;
309}
310
311/* Look up to see if we're just after a \n. */
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800312static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313{
314 unsigned int i;
315
316 for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
317 if (info->seq_aft_nl[dir][i] == seq)
318 return 1;
319 return 0;
320}
321
322/* We don't update if it's older than what we have. */
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200323static void update_nl_seq(struct nf_conn *ct, u32 nl_seq,
324 struct nf_ct_ftp_master *info, int dir,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800325 struct sk_buff *skb)
326{
Patrick McHardyaaff23a2010-01-07 18:33:18 +0100327 unsigned int i, oldest;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328
329 /* Look for oldest: if we find exact match, we're done. */
330 for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
331 if (info->seq_aft_nl[dir][i] == nl_seq)
332 return;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800333 }
334
335 if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
336 info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
Patrick McHardyaaff23a2010-01-07 18:33:18 +0100337 } else {
338 if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1]))
339 oldest = 0;
340 else
341 oldest = 1;
342
343 if (after(nl_seq, info->seq_aft_nl[dir][oldest]))
344 info->seq_aft_nl[dir][oldest] = nl_seq;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345 }
346}
347
Herbert Xu3db05fe2007-10-15 00:53:15 -0700348static int help(struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349 unsigned int protoff,
350 struct nf_conn *ct,
351 enum ip_conntrack_info ctinfo)
352{
353 unsigned int dataoff, datalen;
Jan Engelhardt58c0fb02008-04-14 11:15:42 +0200354 const struct tcphdr *th;
355 struct tcphdr _tcph;
356 const char *fb_ptr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800357 int ret;
358 u32 seq;
359 int dir = CTINFO2DIR(ctinfo);
Ingo Molnard6e8cc62008-11-25 18:23:03 +0100360 unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff);
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800361 struct nf_ct_ftp_master *ct_ftp_info = &nfct_help(ct)->help.ct_ftp_info;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 struct nf_conntrack_expect *exp;
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800363 union nf_inet_addr *daddr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800364 struct nf_conntrack_man cmd = {};
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 unsigned int i;
366 int found = 0, ends_in_nl;
Patrick McHardy337fbc42006-11-29 02:35:25 +0100367 typeof(nf_nat_ftp_hook) nf_nat_ftp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368
369 /* Until there's been traffic both ways, don't look in packets. */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800370 if (ctinfo != IP_CT_ESTABLISHED &&
371 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700372 pr_debug("ftp: Conntrackinfo = %u\n", ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800373 return NF_ACCEPT;
374 }
375
Herbert Xu3db05fe2007-10-15 00:53:15 -0700376 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377 if (th == NULL)
378 return NF_ACCEPT;
379
380 dataoff = protoff + th->doff * 4;
381 /* No data? */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700382 if (dataoff >= skb->len) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700383 pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700384 skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 return NF_ACCEPT;
386 }
Herbert Xu3db05fe2007-10-15 00:53:15 -0700387 datalen = skb->len - dataoff;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800388
389 spin_lock_bh(&nf_ftp_lock);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700390 fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391 BUG_ON(fb_ptr == NULL);
392
393 ends_in_nl = (fb_ptr[datalen - 1] == '\n');
394 seq = ntohl(th->seq) + datalen;
395
396 /* Look up to see if we're just after a \n. */
397 if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
398 /* Now if this ends in \n, update ftp info. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700399 pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
400 ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
401 ct_ftp_info->seq_aft_nl[dir][0],
402 ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)",
403 ct_ftp_info->seq_aft_nl[dir][1]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 ret = NF_ACCEPT;
405 goto out_update_nl;
406 }
407
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800408 /* Initialize IP/IPv6 addr to expected address (it's not mentioned
409 in EPSV responses) */
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200410 cmd.l3num = nf_ct_l3num(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411 memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
412 sizeof(cmd.u3.all));
413
Patrick McHardy7d8c5012006-05-29 18:25:38 -0700414 for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800415 found = find_pattern(fb_ptr, datalen,
Patrick McHardy7d8c5012006-05-29 18:25:38 -0700416 search[dir][i].pattern,
417 search[dir][i].plen,
418 search[dir][i].skip,
419 search[dir][i].term,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420 &matchoff, &matchlen,
421 &cmd,
Patrick McHardy7d8c5012006-05-29 18:25:38 -0700422 search[dir][i].getnum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 if (found) break;
424 }
425 if (found == -1) {
426 /* We don't usually drop packets. After all, this is
427 connection tracking, not packet filtering.
428 However, it is necessary for accurate tracking in
429 this case. */
Patrick McHardy4813ead2008-11-24 18:34:48 +0100430 pr_debug("conntrack_ftp: partial %s %u+%u\n",
431 search[dir][i].pattern, ntohl(th->seq), datalen);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 ret = NF_DROP;
433 goto out;
434 } else if (found == 0) { /* No match */
435 ret = NF_ACCEPT;
436 goto out_update_nl;
437 }
438
Patrick McHardy0d537782007-07-07 22:39:38 -0700439 pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
440 matchlen, fb_ptr + matchoff,
441 matchlen, ntohl(th->seq) + matchoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442
Patrick McHardy68236452007-07-07 22:30:49 -0700443 exp = nf_ct_expect_alloc(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800444 if (exp == NULL) {
445 ret = NF_DROP;
446 goto out;
447 }
448
449 /* We refer to the reverse direction ("!dir") tuples here,
450 * because we're expecting something in the other direction.
451 * Doesn't matter unless NAT is happening. */
Patrick McHardydf43b4e2007-07-07 22:31:07 -0700452 daddr = &ct->tuplehash[!dir].tuple.dst.u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453
454 /* Update the ftp info */
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200455 if ((cmd.l3num == nf_ct_l3num(ct)) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456 memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
457 sizeof(cmd.u3.all))) {
458 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800459 server: it really wants us to connect to a
460 different IP address. Simply don't record it for
461 NAT. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462 if (cmd.l3num == PF_INET) {
Harvey Harrison14d5e832008-10-31 00:54:29 -0700463 pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n",
464 &cmd.u3.ip,
465 &ct->tuplehash[dir].tuple.src.u3.ip);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700467 pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700468 cmd.u3.ip6,
469 ct->tuplehash[dir].tuple.src.u3.ip6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470 }
471
472 /* Thanks to Cristiano Lincoln Mattos
473 <lincoln@cesar.org.br> for reporting this potential
474 problem (DMZ machines opening holes to internal
475 networks, or the packet filter itself). */
476 if (!loose) {
477 ret = NF_ACCEPT;
478 goto out_put_expect;
479 }
Patrick McHardydf43b4e2007-07-07 22:31:07 -0700480 daddr = &cmd.u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481 }
482
Patrick McHardy6002f2662008-03-25 20:09:15 -0700483 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num,
Patrick McHardydf43b4e2007-07-07 22:31:07 -0700484 &ct->tuplehash[!dir].tuple.src.u3, daddr,
485 IPPROTO_TCP, NULL, &cmd.u.tcp.port);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486
487 /* Now, NAT might want to mangle the packet, and register the
488 * (possibly changed) expectation itself. */
Patrick McHardy337fbc42006-11-29 02:35:25 +0100489 nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook);
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800490 if (nf_nat_ftp && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700491 ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype,
Patrick McHardy25b86e02007-05-24 16:41:50 -0700492 matchoff, matchlen, exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800493 else {
494 /* Can't expect this? Best to drop packet now. */
Patrick McHardy68236452007-07-07 22:30:49 -0700495 if (nf_ct_expect_related(exp) != 0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800496 ret = NF_DROP;
497 else
498 ret = NF_ACCEPT;
499 }
500
501out_put_expect:
Patrick McHardy68236452007-07-07 22:30:49 -0700502 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800503
504out_update_nl:
505 /* Now if this ends in \n, update ftp info. Seq may have been
506 * adjusted by NAT code. */
507 if (ends_in_nl)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200508 update_nl_seq(ct, seq, ct_ftp_info, dir, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800509 out:
510 spin_unlock_bh(&nf_ftp_lock);
511 return ret;
512}
513
Patrick McHardyec59a112007-07-07 22:37:03 -0700514static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly;
515static char ftp_names[MAX_PORTS][2][sizeof("ftp-65535")] __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516
Patrick McHardy6002f2662008-03-25 20:09:15 -0700517static const struct nf_conntrack_expect_policy ftp_exp_policy = {
518 .max_expected = 1,
519 .timeout = 5 * 60,
520};
521
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800522/* don't make this __exit, since it's called from __init ! */
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800523static void nf_conntrack_ftp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800524{
525 int i, j;
526 for (i = 0; i < ports_c; i++) {
527 for (j = 0; j < 2; j++) {
528 if (ftp[i][j].me == NULL)
529 continue;
530
Patrick McHardy0d537782007-07-07 22:39:38 -0700531 pr_debug("nf_ct_ftp: unregistering helper for pf: %d "
532 "port: %d\n",
533 ftp[i][j].tuple.src.l3num, ports[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800534 nf_conntrack_helper_unregister(&ftp[i][j]);
535 }
536 }
537
538 kfree(ftp_buffer);
539}
540
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800541static int __init nf_conntrack_ftp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800542{
543 int i, j = -1, ret = 0;
544 char *tmpname;
545
546 ftp_buffer = kmalloc(65536, GFP_KERNEL);
547 if (!ftp_buffer)
548 return -ENOMEM;
549
550 if (ports_c == 0)
551 ports[ports_c++] = FTP_PORT;
552
553 /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
554 are tracked or not - YK */
555 for (i = 0; i < ports_c; i++) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800556 ftp[i][0].tuple.src.l3num = PF_INET;
557 ftp[i][1].tuple.src.l3num = PF_INET6;
558 for (j = 0; j < 2; j++) {
559 ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]);
560 ftp[i][j].tuple.dst.protonum = IPPROTO_TCP;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700561 ftp[i][j].expect_policy = &ftp_exp_policy;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800562 ftp[i][j].me = THIS_MODULE;
563 ftp[i][j].help = help;
564 tmpname = &ftp_names[i][j][0];
565 if (ports[i] == FTP_PORT)
566 sprintf(tmpname, "ftp");
567 else
568 sprintf(tmpname, "ftp-%d", ports[i]);
569 ftp[i][j].name = tmpname;
570
Patrick McHardy0d537782007-07-07 22:39:38 -0700571 pr_debug("nf_ct_ftp: registering helper for pf: %d "
572 "port: %d\n",
573 ftp[i][j].tuple.src.l3num, ports[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800574 ret = nf_conntrack_helper_register(&ftp[i][j]);
575 if (ret) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200576 printk(KERN_ERR "nf_ct_ftp: failed to register"
577 " helper for pf: %d port: %d\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578 ftp[i][j].tuple.src.l3num, ports[i]);
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800579 nf_conntrack_ftp_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800580 return ret;
581 }
582 }
583 }
584
585 return 0;
586}
587
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800588module_init(nf_conntrack_ftp_init);
589module_exit(nf_conntrack_ftp_fini);