Remove obsolete #include <linux/config.h>
[linux-2.6.git] / net / netfilter / nf_conntrack_ftp.c
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.
10  *
11  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
12  *      - enable working with Layer 3 protocol independent connection tracking.
13  *      - track EPRT and EPSV commands with IPv6 address.
14  *
15  * Derived from net/ipv4/netfilter/ip_conntrack_ftp.c
16  */
17
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/netfilter.h>
21 #include <linux/ip.h>
22 #include <linux/ipv6.h>
23 #include <linux/ctype.h>
24 #include <net/checksum.h>
25 #include <net/tcp.h>
26
27 #include <net/netfilter/nf_conntrack.h>
28 #include <net/netfilter/nf_conntrack_helper.h>
29 #include <linux/netfilter/nf_conntrack_ftp.h>
30
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
33 MODULE_DESCRIPTION("ftp connection tracking helper");
34
35 /* This is slow, but it's simple. --RR */
36 static char *ftp_buffer;
37
38 static DEFINE_SPINLOCK(nf_ftp_lock);
39
40 #define MAX_PORTS 8
41 static u_int16_t ports[MAX_PORTS];
42 static unsigned int ports_c;
43 module_param_array(ports, ushort, &ports_c, 0400);
44
45 static int loose;
46 module_param(loose, bool, 0600);
47
48 unsigned int (*nf_nat_ftp_hook)(struct sk_buff **pskb,
49                                 enum ip_conntrack_info ctinfo,
50                                 enum ip_ct_ftp_type type,
51                                 unsigned int matchoff,
52                                 unsigned int matchlen,
53                                 struct nf_conntrack_expect *exp,
54                                 u32 *seq);
55 EXPORT_SYMBOL_GPL(nf_nat_ftp_hook);
56
57 #if 0
58 #define DEBUGP printk
59 #else
60 #define DEBUGP(format, args...)
61 #endif
62
63 static int try_rfc959(const char *, size_t, struct nf_conntrack_man *, char);
64 static int try_eprt(const char *, size_t, struct nf_conntrack_man *, char);
65 static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *,
66                              char);
67
68 static struct ftp_search {
69         const char *pattern;
70         size_t plen;
71         char skip;
72         char term;
73         enum ip_ct_ftp_type ftptype;
74         int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char);
75 } search[IP_CT_DIR_MAX][2] = {
76         [IP_CT_DIR_ORIGINAL] = {
77                 {
78                         .pattern        = "PORT",
79                         .plen           = sizeof("PORT") - 1,
80                         .skip           = ' ',
81                         .term           = '\r',
82                         .ftptype        = IP_CT_FTP_PORT,
83                         .getnum         = try_rfc959,
84                 },
85                 {
86                         .pattern        = "EPRT",
87                         .plen           = sizeof("EPRT") - 1,
88                         .skip           = ' ',
89                         .term           = '\r',
90                         .ftptype        = IP_CT_FTP_EPRT,
91                         .getnum         = try_eprt,
92                 },
93         },
94         [IP_CT_DIR_REPLY] = {
95                 {
96                         .pattern        = "227 ",
97                         .plen           = sizeof("227 ") - 1,
98                         .skip           = '(',
99                         .term           = ')',
100                         .ftptype        = IP_CT_FTP_PASV,
101                         .getnum         = try_rfc959,
102                 },
103                 {
104                         .pattern        = "229 ",
105                         .plen           = sizeof("229 ") - 1,
106                         .skip           = '(',
107                         .term           = ')',
108                         .ftptype        = IP_CT_FTP_EPSV,
109                         .getnum         = try_epsv_response,
110                 },
111         },
112 };
113
114 /* This code is based on inet_pton() in glibc-2.2.4 */
115 static int
116 get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term)
117 {
118         static const char xdigits[] = "0123456789abcdef";
119         u_int8_t tmp[16], *tp, *endp, *colonp;
120         int ch, saw_xdigit;
121         u_int32_t val;
122         size_t clen = 0;
123
124         tp = memset(tmp, '\0', sizeof(tmp));
125         endp = tp + sizeof(tmp);
126         colonp = NULL;
127
128         /* Leading :: requires some special handling. */
129         if (*src == ':'){
130                 if (*++src != ':') {
131                         DEBUGP("invalid \":\" at the head of addr\n");
132                         return 0;
133                 }
134                 clen++;
135         }
136
137         saw_xdigit = 0;
138         val = 0;
139         while ((clen < dlen) && (*src != term)) {
140                 const char *pch;
141
142                 ch = tolower(*src++);
143                 clen++;
144
145                 pch = strchr(xdigits, ch);
146                 if (pch != NULL) {
147                         val <<= 4;
148                         val |= (pch - xdigits);
149                         if (val > 0xffff)
150                                 return 0;
151
152                         saw_xdigit = 1;
153                         continue;
154                 }
155                 if (ch != ':') {
156                         DEBUGP("get_ipv6_addr: invalid char. \'%c\'\n", ch);
157                         return 0;
158                 }
159
160                 if (!saw_xdigit) {
161                         if (colonp) {
162                                 DEBUGP("invalid location of \"::\".\n");
163                                 return 0;
164                         }
165                         colonp = tp;
166                         continue;
167                 } else if (*src == term) {
168                         DEBUGP("trancated IPv6 addr\n");
169                         return 0;
170                 }
171
172                 if (tp + 2 > endp)
173                         return 0;
174                 *tp++ = (u_int8_t) (val >> 8) & 0xff;
175                 *tp++ = (u_int8_t) val & 0xff;
176
177                 saw_xdigit = 0;
178                 val = 0;
179                 continue;
180         }
181         if (saw_xdigit) {
182                 if (tp + 2 > endp)
183                         return 0;
184                 *tp++ = (u_int8_t) (val >> 8) & 0xff;
185                 *tp++ = (u_int8_t) val & 0xff;
186         }
187         if (colonp != NULL) {
188                 /*
189                  * Since some memmove()'s erroneously fail to handle
190                  * overlapping regions, we'll do the shift by hand.
191                  */
192                 const int n = tp - colonp;
193                 int i;
194
195                 if (tp == endp)
196                         return 0;
197
198                 for (i = 1; i <= n; i++) {
199                         endp[- i] = colonp[n - i];
200                         colonp[n - i] = 0;
201                 }
202                 tp = endp;
203         }
204         if (tp != endp || (*src != term))
205                 return 0;
206
207         memcpy(dst->s6_addr, tmp, sizeof(dst->s6_addr));
208         return clen;
209 }
210
211 static int try_number(const char *data, size_t dlen, u_int32_t array[],
212                       int array_size, char sep, char term)
213 {
214         u_int32_t i, len;
215
216         memset(array, 0, sizeof(array[0])*array_size);
217
218         /* Keep data pointing at next char. */
219         for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
220                 if (*data >= '0' && *data <= '9') {
221                         array[i] = array[i]*10 + *data - '0';
222                 }
223                 else if (*data == sep)
224                         i++;
225                 else {
226                         /* Unexpected character; true if it's the
227                            terminator and we're finished. */
228                         if (*data == term && i == array_size - 1)
229                                 return len;
230
231                         DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
232                                len, i, *data);
233                         return 0;
234                 }
235         }
236         DEBUGP("Failed to fill %u numbers separated by %c\n", array_size, sep);
237
238         return 0;
239 }
240
241 /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
242 static int try_rfc959(const char *data, size_t dlen,
243                       struct nf_conntrack_man *cmd, char term)
244 {
245         int length;
246         u_int32_t array[6];
247
248         length = try_number(data, dlen, array, 6, ',', term);
249         if (length == 0)
250                 return 0;
251
252         cmd->u3.ip =  htonl((array[0] << 24) | (array[1] << 16) |
253                                     (array[2] << 8) | array[3]);
254         cmd->u.tcp.port = htons((array[4] << 8) | array[5]);
255         return length;
256 }
257
258 /* Grab port: number up to delimiter */
259 static int get_port(const char *data, int start, size_t dlen, char delim,
260                     u_int16_t *port)
261 {
262         u_int16_t tmp_port = 0;
263         int i;
264
265         for (i = start; i < dlen; i++) {
266                 /* Finished? */
267                 if (data[i] == delim) {
268                         if (tmp_port == 0)
269                                 break;
270                         *port = htons(tmp_port);
271                         DEBUGP("get_port: return %d\n", tmp_port);
272                         return i + 1;
273                 }
274                 else if (data[i] >= '0' && data[i] <= '9')
275                         tmp_port = tmp_port*10 + data[i] - '0';
276                 else { /* Some other crap */
277                         DEBUGP("get_port: invalid char.\n");
278                         break;
279                 }
280         }
281         return 0;
282 }
283
284 /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
285 static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd,
286                     char term)
287 {
288         char delim;
289         int length;
290
291         /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
292            then delimiter again. */
293         if (dlen <= 3) {
294                 DEBUGP("EPRT: too short\n");
295                 return 0;
296         }
297         delim = data[0];
298         if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) {
299                 DEBUGP("try_eprt: invalid delimitter.\n");
300                 return 0;
301         }
302
303         if ((cmd->l3num == PF_INET && data[1] != '1') ||
304             (cmd->l3num == PF_INET6 && data[1] != '2')) {
305                 DEBUGP("EPRT: invalid protocol number.\n");
306                 return 0;
307         }
308
309         DEBUGP("EPRT: Got %c%c%c\n", delim, data[1], delim);
310
311         if (data[1] == '1') {
312                 u_int32_t array[4];
313
314                 /* Now we have IP address. */
315                 length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
316                 if (length != 0)
317                         cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16)
318                                            | (array[2] << 8) | array[3]);
319         } else {
320                 /* Now we have IPv6 address. */
321                 length = get_ipv6_addr(data + 3, dlen - 3,
322                                        (struct in6_addr *)cmd->u3.ip6, delim);
323         }
324
325         if (length == 0)
326                 return 0;
327         DEBUGP("EPRT: Got IP address!\n");
328         /* Start offset includes initial "|1|", and trailing delimiter */
329         return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port);
330 }
331
332 /* Returns 0, or length of numbers: |||6446| */
333 static int try_epsv_response(const char *data, size_t dlen,
334                              struct nf_conntrack_man *cmd, char term)
335 {
336         char delim;
337
338         /* Three delimiters. */
339         if (dlen <= 3) return 0;
340         delim = data[0];
341         if (isdigit(delim) || delim < 33 || delim > 126
342             || data[1] != delim || data[2] != delim)
343                 return 0;
344
345         return get_port(data, 3, dlen, delim, &cmd->u.tcp.port);
346 }
347
348 /* Return 1 for match, 0 for accept, -1 for partial. */
349 static int find_pattern(const char *data, size_t dlen,
350                         const char *pattern, size_t plen,
351                         char skip, char term,
352                         unsigned int *numoff,
353                         unsigned int *numlen,
354                         struct nf_conntrack_man *cmd,
355                         int (*getnum)(const char *, size_t,
356                                       struct nf_conntrack_man *, char))
357 {
358         size_t i;
359
360         DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
361         if (dlen == 0)
362                 return 0;
363
364         if (dlen <= plen) {
365                 /* Short packet: try for partial? */
366                 if (strnicmp(data, pattern, dlen) == 0)
367                         return -1;
368                 else return 0;
369         }
370
371         if (strnicmp(data, pattern, plen) != 0) {
372 #if 0
373                 size_t i;
374
375                 DEBUGP("ftp: string mismatch\n");
376                 for (i = 0; i < plen; i++) {
377                         DEBUGP("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
378                                 i, data[i], data[i],
379                                 pattern[i], pattern[i]);
380                 }
381 #endif
382                 return 0;
383         }
384
385         DEBUGP("Pattern matches!\n");
386         /* Now we've found the constant string, try to skip
387            to the 'skip' character */
388         for (i = plen; data[i] != skip; i++)
389                 if (i == dlen - 1) return -1;
390
391         /* Skip over the last character */
392         i++;
393
394         DEBUGP("Skipped up to `%c'!\n", skip);
395
396         *numoff = i;
397         *numlen = getnum(data + i, dlen - i, cmd, term);
398         if (!*numlen)
399                 return -1;
400
401         DEBUGP("Match succeeded!\n");
402         return 1;
403 }
404
405 /* Look up to see if we're just after a \n. */
406 static int find_nl_seq(u32 seq, const struct ip_ct_ftp_master *info, int dir)
407 {
408         unsigned int i;
409
410         for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
411                 if (info->seq_aft_nl[dir][i] == seq)
412                         return 1;
413         return 0;
414 }
415
416 /* We don't update if it's older than what we have. */
417 static void update_nl_seq(u32 nl_seq, struct ip_ct_ftp_master *info, int dir,
418                           struct sk_buff *skb)
419 {
420         unsigned int i, oldest = NUM_SEQ_TO_REMEMBER;
421
422         /* Look for oldest: if we find exact match, we're done. */
423         for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
424                 if (info->seq_aft_nl[dir][i] == nl_seq)
425                         return;
426
427                 if (oldest == info->seq_aft_nl_num[dir]
428                     || before(info->seq_aft_nl[dir][i], oldest))
429                         oldest = i;
430         }
431
432         if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
433                 info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
434                 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE, skb);
435         } else if (oldest != NUM_SEQ_TO_REMEMBER) {
436                 info->seq_aft_nl[dir][oldest] = nl_seq;
437                 nf_conntrack_event_cache(IPCT_HELPINFO_VOLATILE, skb);
438         }
439 }
440
441 static int help(struct sk_buff **pskb,
442                 unsigned int protoff,
443                 struct nf_conn *ct,
444                 enum ip_conntrack_info ctinfo)
445 {
446         unsigned int dataoff, datalen;
447         struct tcphdr _tcph, *th;
448         char *fb_ptr;
449         int ret;
450         u32 seq;
451         int dir = CTINFO2DIR(ctinfo);
452         unsigned int matchlen, matchoff;
453         struct ip_ct_ftp_master *ct_ftp_info = &nfct_help(ct)->help.ct_ftp_info;
454         struct nf_conntrack_expect *exp;
455         struct nf_conntrack_man cmd = {};
456
457         unsigned int i;
458         int found = 0, ends_in_nl;
459
460         /* Until there's been traffic both ways, don't look in packets. */
461         if (ctinfo != IP_CT_ESTABLISHED
462             && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
463                 DEBUGP("ftp: Conntrackinfo = %u\n", ctinfo);
464                 return NF_ACCEPT;
465         }
466
467         th = skb_header_pointer(*pskb, protoff, sizeof(_tcph), &_tcph);
468         if (th == NULL)
469                 return NF_ACCEPT;
470
471         dataoff = protoff + th->doff * 4;
472         /* No data? */
473         if (dataoff >= (*pskb)->len) {
474                 DEBUGP("ftp: dataoff(%u) >= skblen(%u)\n", dataoff,
475                         (*pskb)->len);
476                 return NF_ACCEPT;
477         }
478         datalen = (*pskb)->len - dataoff;
479
480         spin_lock_bh(&nf_ftp_lock);
481         fb_ptr = skb_header_pointer(*pskb, dataoff, datalen, ftp_buffer);
482         BUG_ON(fb_ptr == NULL);
483
484         ends_in_nl = (fb_ptr[datalen - 1] == '\n');
485         seq = ntohl(th->seq) + datalen;
486
487         /* Look up to see if we're just after a \n. */
488         if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
489                 /* Now if this ends in \n, update ftp info. */
490                 DEBUGP("nf_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
491                        ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
492                        ct_ftp_info->seq_aft_nl[dir][0],
493                        ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)",
494                        ct_ftp_info->seq_aft_nl[dir][1]);
495                 ret = NF_ACCEPT;
496                 goto out_update_nl;
497         }
498
499         /* Initialize IP/IPv6 addr to expected address (it's not mentioned
500            in EPSV responses) */
501         cmd.l3num = ct->tuplehash[dir].tuple.src.l3num;
502         memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
503                sizeof(cmd.u3.all));
504
505         for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
506                 found = find_pattern(fb_ptr, datalen,
507                                      search[dir][i].pattern,
508                                      search[dir][i].plen,
509                                      search[dir][i].skip,
510                                      search[dir][i].term,
511                                      &matchoff, &matchlen,
512                                      &cmd,
513                                      search[dir][i].getnum);
514                 if (found) break;
515         }
516         if (found == -1) {
517                 /* We don't usually drop packets.  After all, this is
518                    connection tracking, not packet filtering.
519                    However, it is necessary for accurate tracking in
520                    this case. */
521                 if (net_ratelimit())
522                         printk("conntrack_ftp: partial %s %u+%u\n",
523                                search[dir][i].pattern,
524                                ntohl(th->seq), datalen);
525                 ret = NF_DROP;
526                 goto out;
527         } else if (found == 0) { /* No match */
528                 ret = NF_ACCEPT;
529                 goto out_update_nl;
530         }
531
532         DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
533                (int)matchlen, fb_ptr + matchoff,
534                matchlen, ntohl(th->seq) + matchoff);
535
536         exp = nf_conntrack_expect_alloc(ct);
537         if (exp == NULL) {
538                 ret = NF_DROP;
539                 goto out;
540         }
541
542         /* We refer to the reverse direction ("!dir") tuples here,
543          * because we're expecting something in the other direction.
544          * Doesn't matter unless NAT is happening.  */
545         exp->tuple.dst.u3 = ct->tuplehash[!dir].tuple.dst.u3;
546
547         /* Update the ftp info */
548         if ((cmd.l3num == ct->tuplehash[dir].tuple.src.l3num) &&
549             memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
550                      sizeof(cmd.u3.all))) {
551                 /* Enrico Scholz's passive FTP to partially RNAT'd ftp
552                    server: it really wants us to connect to a
553                    different IP address.  Simply don't record it for
554                    NAT. */
555                 if (cmd.l3num == PF_INET) {
556                         DEBUGP("conntrack_ftp: NOT RECORDING: " NIPQUAD_FMT " != " NIPQUAD_FMT "\n",
557                                NIPQUAD(cmd.u3.ip),
558                                NIPQUAD(ct->tuplehash[dir].tuple.src.u3.ip));
559                 } else {
560                         DEBUGP("conntrack_ftp: NOT RECORDING: " NIP6_FMT " != " NIP6_FMT "\n",
561                                NIP6(*((struct in6_addr *)cmd.u3.ip6)),
562                                NIP6(*((struct in6_addr *)ct->tuplehash[dir]
563                                                         .tuple.src.u3.ip6)));
564                 }
565
566                 /* Thanks to Cristiano Lincoln Mattos
567                    <lincoln@cesar.org.br> for reporting this potential
568                    problem (DMZ machines opening holes to internal
569                    networks, or the packet filter itself). */
570                 if (!loose) {
571                         ret = NF_ACCEPT;
572                         goto out_put_expect;
573                 }
574                 memcpy(&exp->tuple.dst.u3, &cmd.u3.all,
575                        sizeof(exp->tuple.dst.u3));
576         }
577
578         exp->tuple.src.u3 = ct->tuplehash[!dir].tuple.src.u3;
579         exp->tuple.src.l3num = cmd.l3num;
580         exp->tuple.src.u.tcp.port = 0;
581         exp->tuple.dst.u.tcp.port = cmd.u.tcp.port;
582         exp->tuple.dst.protonum = IPPROTO_TCP;
583
584         exp->mask = (struct nf_conntrack_tuple)
585                     { .src = { .l3num = 0xFFFF,
586                                .u = { .tcp = { 0 }},
587                              },
588                       .dst = { .protonum = 0xFF,
589                                .u = { .tcp = { 0xFFFF }},
590                              },
591                     };
592         if (cmd.l3num == PF_INET) {
593                 exp->mask.src.u3.ip = 0xFFFFFFFF;
594                 exp->mask.dst.u3.ip = 0xFFFFFFFF;
595         } else {
596                 memset(exp->mask.src.u3.ip6, 0xFF,
597                        sizeof(exp->mask.src.u3.ip6));
598                 memset(exp->mask.dst.u3.ip6, 0xFF,
599                        sizeof(exp->mask.src.u3.ip6));
600         }
601
602         exp->expectfn = NULL;
603         exp->flags = 0;
604
605         /* Now, NAT might want to mangle the packet, and register the
606          * (possibly changed) expectation itself. */
607         if (nf_nat_ftp_hook)
608                 ret = nf_nat_ftp_hook(pskb, ctinfo, search[dir][i].ftptype,
609                                       matchoff, matchlen, exp, &seq);
610         else {
611                 /* Can't expect this?  Best to drop packet now. */
612                 if (nf_conntrack_expect_related(exp) != 0)
613                         ret = NF_DROP;
614                 else
615                         ret = NF_ACCEPT;
616         }
617
618 out_put_expect:
619         nf_conntrack_expect_put(exp);
620
621 out_update_nl:
622         /* Now if this ends in \n, update ftp info.  Seq may have been
623          * adjusted by NAT code. */
624         if (ends_in_nl)
625                 update_nl_seq(seq, ct_ftp_info, dir, *pskb);
626  out:
627         spin_unlock_bh(&nf_ftp_lock);
628         return ret;
629 }
630
631 static struct nf_conntrack_helper ftp[MAX_PORTS][2];
632 static char ftp_names[MAX_PORTS][2][sizeof("ftp-65535")];
633
634 /* don't make this __exit, since it's called from __init ! */
635 static void nf_conntrack_ftp_fini(void)
636 {
637         int i, j;
638         for (i = 0; i < ports_c; i++) {
639                 for (j = 0; j < 2; j++) {
640                         if (ftp[i][j].me == NULL)
641                                 continue;
642
643                         DEBUGP("nf_ct_ftp: unregistering helper for pf: %d "
644                                "port: %d\n",
645                                 ftp[i][j].tuple.src.l3num, ports[i]);
646                         nf_conntrack_helper_unregister(&ftp[i][j]);
647                 }
648         }
649
650         kfree(ftp_buffer);
651 }
652
653 static int __init nf_conntrack_ftp_init(void)
654 {
655         int i, j = -1, ret = 0;
656         char *tmpname;
657
658         ftp_buffer = kmalloc(65536, GFP_KERNEL);
659         if (!ftp_buffer)
660                 return -ENOMEM;
661
662         if (ports_c == 0)
663                 ports[ports_c++] = FTP_PORT;
664
665         /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
666                  are tracked or not - YK */
667         for (i = 0; i < ports_c; i++) {
668                 ftp[i][0].tuple.src.l3num = PF_INET;
669                 ftp[i][1].tuple.src.l3num = PF_INET6;
670                 for (j = 0; j < 2; j++) {
671                         ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]);
672                         ftp[i][j].tuple.dst.protonum = IPPROTO_TCP;
673                         ftp[i][j].mask.src.u.tcp.port = 0xFFFF;
674                         ftp[i][j].mask.dst.protonum = 0xFF;
675                         ftp[i][j].max_expected = 1;
676                         ftp[i][j].timeout = 5 * 60;     /* 5 Minutes */
677                         ftp[i][j].me = THIS_MODULE;
678                         ftp[i][j].help = help;
679                         tmpname = &ftp_names[i][j][0];
680                         if (ports[i] == FTP_PORT)
681                                 sprintf(tmpname, "ftp");
682                         else
683                                 sprintf(tmpname, "ftp-%d", ports[i]);
684                         ftp[i][j].name = tmpname;
685
686                         DEBUGP("nf_ct_ftp: registering helper for pf: %d "
687                                "port: %d\n",
688                                 ftp[i][j].tuple.src.l3num, ports[i]);
689                         ret = nf_conntrack_helper_register(&ftp[i][j]);
690                         if (ret) {
691                                 printk("nf_ct_ftp: failed to register helper "
692                                        " for pf: %d port: %d\n",
693                                         ftp[i][j].tuple.src.l3num, ports[i]);
694                                 nf_conntrack_ftp_fini();
695                                 return ret;
696                         }
697                 }
698         }
699
700         return 0;
701 }
702
703 module_init(nf_conntrack_ftp_init);
704 module_exit(nf_conntrack_ftp_fini);