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