nf_conntrack_ftp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* FTP extension for connection tracking. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  4. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  5. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  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. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/ip.h>
  15. #include <linux/slab.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/ctype.h>
  18. #include <linux/inet.h>
  19. #include <net/checksum.h>
  20. #include <net/tcp.h>
  21. #include <net/netfilter/nf_conntrack.h>
  22. #include <net/netfilter/nf_conntrack_expect.h>
  23. #include <net/netfilter/nf_conntrack_ecache.h>
  24. #include <net/netfilter/nf_conntrack_helper.h>
  25. #include <linux/netfilter/nf_conntrack_ftp.h>
  26. MODULE_LICENSE("GPL");
  27. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  28. MODULE_DESCRIPTION("ftp connection tracking helper");
  29. MODULE_ALIAS("ip_conntrack_ftp");
  30. MODULE_ALIAS_NFCT_HELPER("ftp");
  31. /* This is slow, but it's simple. --RR */
  32. static char *ftp_buffer;
  33. static DEFINE_SPINLOCK(nf_ftp_lock);
  34. #define MAX_PORTS 8
  35. static u_int16_t ports[MAX_PORTS];
  36. static unsigned int ports_c;
  37. module_param_array(ports, ushort, &ports_c, 0400);
  38. static bool loose;
  39. module_param(loose, bool, 0600);
  40. unsigned int (*nf_nat_ftp_hook)(struct sk_buff *skb,
  41. enum ip_conntrack_info ctinfo,
  42. enum nf_ct_ftp_type type,
  43. unsigned int protoff,
  44. unsigned int matchoff,
  45. unsigned int matchlen,
  46. struct nf_conntrack_expect *exp);
  47. EXPORT_SYMBOL_GPL(nf_nat_ftp_hook);
  48. static int try_rfc959(const char *, size_t, struct nf_conntrack_man *,
  49. char, unsigned int *);
  50. static int try_rfc1123(const char *, size_t, struct nf_conntrack_man *,
  51. char, unsigned int *);
  52. static int try_eprt(const char *, size_t, struct nf_conntrack_man *,
  53. char, unsigned int *);
  54. static int try_epsv_response(const char *, size_t, struct nf_conntrack_man *,
  55. char, unsigned int *);
  56. static struct ftp_search {
  57. const char *pattern;
  58. size_t plen;
  59. char skip;
  60. char term;
  61. enum nf_ct_ftp_type ftptype;
  62. int (*getnum)(const char *, size_t, struct nf_conntrack_man *, char, unsigned int *);
  63. } search[IP_CT_DIR_MAX][2] = {
  64. [IP_CT_DIR_ORIGINAL] = {
  65. {
  66. .pattern = "PORT",
  67. .plen = sizeof("PORT") - 1,
  68. .skip = ' ',
  69. .term = '\r',
  70. .ftptype = NF_CT_FTP_PORT,
  71. .getnum = try_rfc959,
  72. },
  73. {
  74. .pattern = "EPRT",
  75. .plen = sizeof("EPRT") - 1,
  76. .skip = ' ',
  77. .term = '\r',
  78. .ftptype = NF_CT_FTP_EPRT,
  79. .getnum = try_eprt,
  80. },
  81. },
  82. [IP_CT_DIR_REPLY] = {
  83. {
  84. .pattern = "227 ",
  85. .plen = sizeof("227 ") - 1,
  86. .ftptype = NF_CT_FTP_PASV,
  87. .getnum = try_rfc1123,
  88. },
  89. {
  90. .pattern = "229 ",
  91. .plen = sizeof("229 ") - 1,
  92. .skip = '(',
  93. .term = ')',
  94. .ftptype = NF_CT_FTP_EPSV,
  95. .getnum = try_epsv_response,
  96. },
  97. },
  98. };
  99. static int
  100. get_ipv6_addr(const char *src, size_t dlen, struct in6_addr *dst, u_int8_t term)
  101. {
  102. const char *end;
  103. int ret = in6_pton(src, min_t(size_t, dlen, 0xffff), (u8 *)dst, term, &end);
  104. if (ret > 0)
  105. return (int)(end - src);
  106. return 0;
  107. }
  108. static int try_number(const char *data, size_t dlen, u_int32_t array[],
  109. int array_size, char sep, char term)
  110. {
  111. u_int32_t i, len;
  112. memset(array, 0, sizeof(array[0])*array_size);
  113. /* Keep data pointing at next char. */
  114. for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
  115. if (*data >= '0' && *data <= '9') {
  116. array[i] = array[i]*10 + *data - '0';
  117. }
  118. else if (*data == sep)
  119. i++;
  120. else {
  121. /* Unexpected character; true if it's the
  122. terminator (or we don't care about one)
  123. and we're finished. */
  124. if ((*data == term || !term) && i == array_size - 1)
  125. return len;
  126. pr_debug("Char %u (got %u nums) `%u' unexpected\n",
  127. len, i, *data);
  128. return 0;
  129. }
  130. }
  131. pr_debug("Failed to fill %u numbers separated by %c\n",
  132. array_size, sep);
  133. return 0;
  134. }
  135. /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
  136. static int try_rfc959(const char *data, size_t dlen,
  137. struct nf_conntrack_man *cmd, char term,
  138. unsigned int *offset)
  139. {
  140. int length;
  141. u_int32_t array[6];
  142. length = try_number(data, dlen, array, 6, ',', term);
  143. if (length == 0)
  144. return 0;
  145. cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
  146. (array[2] << 8) | array[3]);
  147. cmd->u.tcp.port = htons((array[4] << 8) | array[5]);
  148. return length;
  149. }
  150. /*
  151. * From RFC 1123:
  152. * The format of the 227 reply to a PASV command is not
  153. * well standardized. In particular, an FTP client cannot
  154. * assume that the parentheses shown on page 40 of RFC-959
  155. * will be present (and in fact, Figure 3 on page 43 omits
  156. * them). Therefore, a User-FTP program that interprets
  157. * the PASV reply must scan the reply for the first digit
  158. * of the host and port numbers.
  159. */
  160. static int try_rfc1123(const char *data, size_t dlen,
  161. struct nf_conntrack_man *cmd, char term,
  162. unsigned int *offset)
  163. {
  164. int i;
  165. for (i = 0; i < dlen; i++)
  166. if (isdigit(data[i]))
  167. break;
  168. if (i == dlen)
  169. return 0;
  170. *offset += i;
  171. return try_rfc959(data + i, dlen - i, cmd, 0, offset);
  172. }
  173. /* Grab port: number up to delimiter */
  174. static int get_port(const char *data, int start, size_t dlen, char delim,
  175. __be16 *port)
  176. {
  177. u_int16_t tmp_port = 0;
  178. int i;
  179. for (i = start; i < dlen; i++) {
  180. /* Finished? */
  181. if (data[i] == delim) {
  182. if (tmp_port == 0)
  183. break;
  184. *port = htons(tmp_port);
  185. pr_debug("get_port: return %d\n", tmp_port);
  186. return i + 1;
  187. }
  188. else if (data[i] >= '0' && data[i] <= '9')
  189. tmp_port = tmp_port*10 + data[i] - '0';
  190. else { /* Some other crap */
  191. pr_debug("get_port: invalid char.\n");
  192. break;
  193. }
  194. }
  195. return 0;
  196. }
  197. /* Returns 0, or length of numbers: |1|132.235.1.2|6275| or |2|3ffe::1|6275| */
  198. static int try_eprt(const char *data, size_t dlen, struct nf_conntrack_man *cmd,
  199. char term, unsigned int *offset)
  200. {
  201. char delim;
  202. int length;
  203. /* First character is delimiter, then "1" for IPv4 or "2" for IPv6,
  204. then delimiter again. */
  205. if (dlen <= 3) {
  206. pr_debug("EPRT: too short\n");
  207. return 0;
  208. }
  209. delim = data[0];
  210. if (isdigit(delim) || delim < 33 || delim > 126 || data[2] != delim) {
  211. pr_debug("try_eprt: invalid delimitter.\n");
  212. return 0;
  213. }
  214. if ((cmd->l3num == PF_INET && data[1] != '1') ||
  215. (cmd->l3num == PF_INET6 && data[1] != '2')) {
  216. pr_debug("EPRT: invalid protocol number.\n");
  217. return 0;
  218. }
  219. pr_debug("EPRT: Got %c%c%c\n", delim, data[1], delim);
  220. if (data[1] == '1') {
  221. u_int32_t array[4];
  222. /* Now we have IP address. */
  223. length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
  224. if (length != 0)
  225. cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16)
  226. | (array[2] << 8) | array[3]);
  227. } else {
  228. /* Now we have IPv6 address. */
  229. length = get_ipv6_addr(data + 3, dlen - 3,
  230. (struct in6_addr *)cmd->u3.ip6, delim);
  231. }
  232. if (length == 0)
  233. return 0;
  234. pr_debug("EPRT: Got IP address!\n");
  235. /* Start offset includes initial "|1|", and trailing delimiter */
  236. return get_port(data, 3 + length + 1, dlen, delim, &cmd->u.tcp.port);
  237. }
  238. /* Returns 0, or length of numbers: |||6446| */
  239. static int try_epsv_response(const char *data, size_t dlen,
  240. struct nf_conntrack_man *cmd, char term,
  241. unsigned int *offset)
  242. {
  243. char delim;
  244. /* Three delimiters. */
  245. if (dlen <= 3) return 0;
  246. delim = data[0];
  247. if (isdigit(delim) || delim < 33 || delim > 126 ||
  248. data[1] != delim || data[2] != delim)
  249. return 0;
  250. return get_port(data, 3, dlen, delim, &cmd->u.tcp.port);
  251. }
  252. /* Return 1 for match, 0 for accept, -1 for partial. */
  253. static int find_pattern(const char *data, size_t dlen,
  254. const char *pattern, size_t plen,
  255. char skip, char term,
  256. unsigned int *numoff,
  257. unsigned int *numlen,
  258. struct nf_conntrack_man *cmd,
  259. int (*getnum)(const char *, size_t,
  260. struct nf_conntrack_man *, char,
  261. unsigned int *))
  262. {
  263. size_t i = plen;
  264. pr_debug("find_pattern `%s': dlen = %Zu\n", pattern, dlen);
  265. if (dlen == 0)
  266. return 0;
  267. if (dlen <= plen) {
  268. /* Short packet: try for partial? */
  269. if (strncasecmp(data, pattern, dlen) == 0)
  270. return -1;
  271. else return 0;
  272. }
  273. if (strncasecmp(data, pattern, plen) != 0) {
  274. #if 0
  275. size_t i;
  276. pr_debug("ftp: string mismatch\n");
  277. for (i = 0; i < plen; i++) {
  278. pr_debug("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
  279. i, data[i], data[i],
  280. pattern[i], pattern[i]);
  281. }
  282. #endif
  283. return 0;
  284. }
  285. pr_debug("Pattern matches!\n");
  286. /* Now we've found the constant string, try to skip
  287. to the 'skip' character */
  288. if (skip) {
  289. for (i = plen; data[i] != skip; i++)
  290. if (i == dlen - 1) return -1;
  291. /* Skip over the last character */
  292. i++;
  293. }
  294. pr_debug("Skipped up to `%c'!\n", skip);
  295. *numoff = i;
  296. *numlen = getnum(data + i, dlen - i, cmd, term, numoff);
  297. if (!*numlen)
  298. return -1;
  299. pr_debug("Match succeeded!\n");
  300. return 1;
  301. }
  302. /* Look up to see if we're just after a \n. */
  303. static int find_nl_seq(u32 seq, const struct nf_ct_ftp_master *info, int dir)
  304. {
  305. unsigned int i;
  306. for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
  307. if (info->seq_aft_nl[dir][i] == seq)
  308. return 1;
  309. return 0;
  310. }
  311. /* We don't update if it's older than what we have. */
  312. static void update_nl_seq(struct nf_conn *ct, u32 nl_seq,
  313. struct nf_ct_ftp_master *info, int dir,
  314. struct sk_buff *skb)
  315. {
  316. unsigned int i, oldest;
  317. /* Look for oldest: if we find exact match, we're done. */
  318. for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
  319. if (info->seq_aft_nl[dir][i] == nl_seq)
  320. return;
  321. }
  322. if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
  323. info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
  324. } else {
  325. if (before(info->seq_aft_nl[dir][0], info->seq_aft_nl[dir][1]))
  326. oldest = 0;
  327. else
  328. oldest = 1;
  329. if (after(nl_seq, info->seq_aft_nl[dir][oldest]))
  330. info->seq_aft_nl[dir][oldest] = nl_seq;
  331. }
  332. }
  333. static int help(struct sk_buff *skb,
  334. unsigned int protoff,
  335. struct nf_conn *ct,
  336. enum ip_conntrack_info ctinfo)
  337. {
  338. unsigned int dataoff, datalen;
  339. const struct tcphdr *th;
  340. struct tcphdr _tcph;
  341. const char *fb_ptr;
  342. int ret;
  343. u32 seq;
  344. int dir = CTINFO2DIR(ctinfo);
  345. unsigned int uninitialized_var(matchlen), uninitialized_var(matchoff);
  346. struct nf_ct_ftp_master *ct_ftp_info = nfct_help_data(ct);
  347. struct nf_conntrack_expect *exp;
  348. union nf_inet_addr *daddr;
  349. struct nf_conntrack_man cmd = {};
  350. unsigned int i;
  351. int found = 0, ends_in_nl;
  352. typeof(nf_nat_ftp_hook) nf_nat_ftp;
  353. /* Until there's been traffic both ways, don't look in packets. */
  354. if (ctinfo != IP_CT_ESTABLISHED &&
  355. ctinfo != IP_CT_ESTABLISHED_REPLY) {
  356. pr_debug("ftp: Conntrackinfo = %u\n", ctinfo);
  357. return NF_ACCEPT;
  358. }
  359. th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  360. if (th == NULL)
  361. return NF_ACCEPT;
  362. dataoff = protoff + th->doff * 4;
  363. /* No data? */
  364. if (dataoff >= skb->len) {
  365. pr_debug("ftp: dataoff(%u) >= skblen(%u)\n", dataoff,
  366. skb->len);
  367. return NF_ACCEPT;
  368. }
  369. datalen = skb->len - dataoff;
  370. spin_lock_bh(&nf_ftp_lock);
  371. fb_ptr = skb_header_pointer(skb, dataoff, datalen, ftp_buffer);
  372. BUG_ON(fb_ptr == NULL);
  373. ends_in_nl = (fb_ptr[datalen - 1] == '\n');
  374. seq = ntohl(th->seq) + datalen;
  375. /* Look up to see if we're just after a \n. */
  376. if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
  377. /* We're picking up this, clear flags and let it continue */
  378. if (unlikely(ct_ftp_info->flags[dir] & NF_CT_FTP_SEQ_PICKUP)) {
  379. ct_ftp_info->flags[dir] ^= NF_CT_FTP_SEQ_PICKUP;
  380. goto skip_nl_seq;
  381. }
  382. /* Now if this ends in \n, update ftp info. */
  383. pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
  384. ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
  385. ct_ftp_info->seq_aft_nl[dir][0],
  386. ct_ftp_info->seq_aft_nl_num[dir] > 1 ? "" : "(UNSET)",
  387. ct_ftp_info->seq_aft_nl[dir][1]);
  388. ret = NF_ACCEPT;
  389. goto out_update_nl;
  390. }
  391. skip_nl_seq:
  392. /* Initialize IP/IPv6 addr to expected address (it's not mentioned
  393. in EPSV responses) */
  394. cmd.l3num = nf_ct_l3num(ct);
  395. memcpy(cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
  396. sizeof(cmd.u3.all));
  397. for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
  398. found = find_pattern(fb_ptr, datalen,
  399. search[dir][i].pattern,
  400. search[dir][i].plen,
  401. search[dir][i].skip,
  402. search[dir][i].term,
  403. &matchoff, &matchlen,
  404. &cmd,
  405. search[dir][i].getnum);
  406. if (found) break;
  407. }
  408. if (found == -1) {
  409. /* We don't usually drop packets. After all, this is
  410. connection tracking, not packet filtering.
  411. However, it is necessary for accurate tracking in
  412. this case. */
  413. nf_ct_helper_log(skb, ct, "partial matching of `%s'",
  414. search[dir][i].pattern);
  415. ret = NF_DROP;
  416. goto out;
  417. } else if (found == 0) { /* No match */
  418. ret = NF_ACCEPT;
  419. goto out_update_nl;
  420. }
  421. pr_debug("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
  422. matchlen, fb_ptr + matchoff,
  423. matchlen, ntohl(th->seq) + matchoff);
  424. exp = nf_ct_expect_alloc(ct);
  425. if (exp == NULL) {
  426. nf_ct_helper_log(skb, ct, "cannot alloc expectation");
  427. ret = NF_DROP;
  428. goto out;
  429. }
  430. /* We refer to the reverse direction ("!dir") tuples here,
  431. * because we're expecting something in the other direction.
  432. * Doesn't matter unless NAT is happening. */
  433. daddr = &ct->tuplehash[!dir].tuple.dst.u3;
  434. /* Update the ftp info */
  435. if ((cmd.l3num == nf_ct_l3num(ct)) &&
  436. memcmp(&cmd.u3.all, &ct->tuplehash[dir].tuple.src.u3.all,
  437. sizeof(cmd.u3.all))) {
  438. /* Enrico Scholz's passive FTP to partially RNAT'd ftp
  439. server: it really wants us to connect to a
  440. different IP address. Simply don't record it for
  441. NAT. */
  442. if (cmd.l3num == PF_INET) {
  443. pr_debug("conntrack_ftp: NOT RECORDING: %pI4 != %pI4\n",
  444. &cmd.u3.ip,
  445. &ct->tuplehash[dir].tuple.src.u3.ip);
  446. } else {
  447. pr_debug("conntrack_ftp: NOT RECORDING: %pI6 != %pI6\n",
  448. cmd.u3.ip6,
  449. ct->tuplehash[dir].tuple.src.u3.ip6);
  450. }
  451. /* Thanks to Cristiano Lincoln Mattos
  452. <lincoln@cesar.org.br> for reporting this potential
  453. problem (DMZ machines opening holes to internal
  454. networks, or the packet filter itself). */
  455. if (!loose) {
  456. ret = NF_ACCEPT;
  457. goto out_put_expect;
  458. }
  459. daddr = &cmd.u3;
  460. }
  461. nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, cmd.l3num,
  462. &ct->tuplehash[!dir].tuple.src.u3, daddr,
  463. IPPROTO_TCP, NULL, &cmd.u.tcp.port);
  464. /* Now, NAT might want to mangle the packet, and register the
  465. * (possibly changed) expectation itself. */
  466. nf_nat_ftp = rcu_dereference(nf_nat_ftp_hook);
  467. if (nf_nat_ftp && ct->status & IPS_NAT_MASK)
  468. ret = nf_nat_ftp(skb, ctinfo, search[dir][i].ftptype,
  469. protoff, matchoff, matchlen, exp);
  470. else {
  471. /* Can't expect this? Best to drop packet now. */
  472. if (nf_ct_expect_related(exp) != 0) {
  473. nf_ct_helper_log(skb, ct, "cannot add expectation");
  474. ret = NF_DROP;
  475. } else
  476. ret = NF_ACCEPT;
  477. }
  478. out_put_expect:
  479. nf_ct_expect_put(exp);
  480. out_update_nl:
  481. /* Now if this ends in \n, update ftp info. Seq may have been
  482. * adjusted by NAT code. */
  483. if (ends_in_nl)
  484. update_nl_seq(ct, seq, ct_ftp_info, dir, skb);
  485. out:
  486. spin_unlock_bh(&nf_ftp_lock);
  487. return ret;
  488. }
  489. static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct)
  490. {
  491. struct nf_ct_ftp_master *ftp = nfct_help_data(ct);
  492. /* This conntrack has been injected from user-space, always pick up
  493. * sequence tracking. Otherwise, the first FTP command after the
  494. * failover breaks.
  495. */
  496. ftp->flags[IP_CT_DIR_ORIGINAL] |= NF_CT_FTP_SEQ_PICKUP;
  497. ftp->flags[IP_CT_DIR_REPLY] |= NF_CT_FTP_SEQ_PICKUP;
  498. return 0;
  499. }
  500. static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly;
  501. static const struct nf_conntrack_expect_policy ftp_exp_policy = {
  502. .max_expected = 1,
  503. .timeout = 5 * 60,
  504. };
  505. /* don't make this __exit, since it's called from __init ! */
  506. static void nf_conntrack_ftp_fini(void)
  507. {
  508. int i, j;
  509. for (i = 0; i < ports_c; i++) {
  510. for (j = 0; j < 2; j++) {
  511. if (ftp[i][j].me == NULL)
  512. continue;
  513. pr_debug("nf_ct_ftp: unregistering helper for pf: %d "
  514. "port: %d\n",
  515. ftp[i][j].tuple.src.l3num, ports[i]);
  516. nf_conntrack_helper_unregister(&ftp[i][j]);
  517. }
  518. }
  519. kfree(ftp_buffer);
  520. }
  521. static int __init nf_conntrack_ftp_init(void)
  522. {
  523. int i, j = -1, ret = 0;
  524. ftp_buffer = kmalloc(65536, GFP_KERNEL);
  525. if (!ftp_buffer)
  526. return -ENOMEM;
  527. if (ports_c == 0)
  528. ports[ports_c++] = FTP_PORT;
  529. /* FIXME should be configurable whether IPv4 and IPv6 FTP connections
  530. are tracked or not - YK */
  531. for (i = 0; i < ports_c; i++) {
  532. ftp[i][0].tuple.src.l3num = PF_INET;
  533. ftp[i][1].tuple.src.l3num = PF_INET6;
  534. for (j = 0; j < 2; j++) {
  535. ftp[i][j].data_len = sizeof(struct nf_ct_ftp_master);
  536. ftp[i][j].tuple.src.u.tcp.port = htons(ports[i]);
  537. ftp[i][j].tuple.dst.protonum = IPPROTO_TCP;
  538. ftp[i][j].expect_policy = &ftp_exp_policy;
  539. ftp[i][j].me = THIS_MODULE;
  540. ftp[i][j].help = help;
  541. ftp[i][j].from_nlattr = nf_ct_ftp_from_nlattr;
  542. if (ports[i] == FTP_PORT)
  543. sprintf(ftp[i][j].name, "ftp");
  544. else
  545. sprintf(ftp[i][j].name, "ftp-%d", ports[i]);
  546. pr_debug("nf_ct_ftp: registering helper for pf: %d "
  547. "port: %d\n",
  548. ftp[i][j].tuple.src.l3num, ports[i]);
  549. ret = nf_conntrack_helper_register(&ftp[i][j]);
  550. if (ret) {
  551. printk(KERN_ERR "nf_ct_ftp: failed to register"
  552. " helper for pf: %d port: %d\n",
  553. ftp[i][j].tuple.src.l3num, ports[i]);
  554. nf_conntrack_ftp_fini();
  555. return ret;
  556. }
  557. }
  558. }
  559. return 0;
  560. }
  561. module_init(nf_conntrack_ftp_init);
  562. module_exit(nf_conntrack_ftp_fini);