ip_conntrack_ftp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* FTP extension for IP connection tracking. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/netfilter.h>
  11. #include <linux/ip.h>
  12. #include <linux/ctype.h>
  13. #include <net/checksum.h>
  14. #include <net/tcp.h>
  15. #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
  16. #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
  17. #include <linux/moduleparam.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
  20. MODULE_DESCRIPTION("ftp connection tracking helper");
  21. /* This is slow, but it's simple. --RR */
  22. static char *ftp_buffer;
  23. static DEFINE_SPINLOCK(ip_ftp_lock);
  24. #define MAX_PORTS 8
  25. static unsigned short ports[MAX_PORTS];
  26. static int ports_c;
  27. module_param_array(ports, ushort, &ports_c, 0400);
  28. static int loose;
  29. module_param(loose, bool, 0600);
  30. unsigned int (*ip_nat_ftp_hook)(struct sk_buff **pskb,
  31. enum ip_conntrack_info ctinfo,
  32. enum ip_ct_ftp_type type,
  33. unsigned int matchoff,
  34. unsigned int matchlen,
  35. struct ip_conntrack_expect *exp,
  36. u32 *seq);
  37. EXPORT_SYMBOL_GPL(ip_nat_ftp_hook);
  38. #if 0
  39. #define DEBUGP printk
  40. #else
  41. #define DEBUGP(format, args...)
  42. #endif
  43. static int try_rfc959(const char *, size_t, u_int32_t [], char);
  44. static int try_eprt(const char *, size_t, u_int32_t [], char);
  45. static int try_epsv_response(const char *, size_t, u_int32_t [], char);
  46. static const struct ftp_search {
  47. const char *pattern;
  48. size_t plen;
  49. char skip;
  50. char term;
  51. enum ip_ct_ftp_type ftptype;
  52. int (*getnum)(const char *, size_t, u_int32_t[], char);
  53. } search[IP_CT_DIR_MAX][2] = {
  54. [IP_CT_DIR_ORIGINAL] = {
  55. {
  56. .pattern = "PORT",
  57. .plen = sizeof("PORT") - 1,
  58. .skip = ' ',
  59. .term = '\r',
  60. .ftptype = IP_CT_FTP_PORT,
  61. .getnum = try_rfc959,
  62. },
  63. {
  64. .pattern = "EPRT",
  65. .plen = sizeof("EPRT") - 1,
  66. .skip = ' ',
  67. .term = '\r',
  68. .ftptype = IP_CT_FTP_EPRT,
  69. .getnum = try_eprt,
  70. },
  71. },
  72. [IP_CT_DIR_REPLY] = {
  73. {
  74. .pattern = "227 ",
  75. .plen = sizeof("227 ") - 1,
  76. .skip = '(',
  77. .term = ')',
  78. .ftptype = IP_CT_FTP_PASV,
  79. .getnum = try_rfc959,
  80. },
  81. {
  82. .pattern = "229 ",
  83. .plen = sizeof("229 ") - 1,
  84. .skip = '(',
  85. .term = ')',
  86. .ftptype = IP_CT_FTP_EPSV,
  87. .getnum = try_epsv_response,
  88. },
  89. },
  90. };
  91. static int try_number(const char *data, size_t dlen, u_int32_t array[],
  92. int array_size, char sep, char term)
  93. {
  94. u_int32_t i, len;
  95. memset(array, 0, sizeof(array[0])*array_size);
  96. /* Keep data pointing at next char. */
  97. for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
  98. if (*data >= '0' && *data <= '9') {
  99. array[i] = array[i]*10 + *data - '0';
  100. }
  101. else if (*data == sep)
  102. i++;
  103. else {
  104. /* Unexpected character; true if it's the
  105. terminator and we're finished. */
  106. if (*data == term && i == array_size - 1)
  107. return len;
  108. DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
  109. len, i, *data);
  110. return 0;
  111. }
  112. }
  113. DEBUGP("Failed to fill %u numbers separated by %c\n", array_size, sep);
  114. return 0;
  115. }
  116. /* Returns 0, or length of numbers: 192,168,1,1,5,6 */
  117. static int try_rfc959(const char *data, size_t dlen, u_int32_t array[6],
  118. char term)
  119. {
  120. return try_number(data, dlen, array, 6, ',', term);
  121. }
  122. /* Grab port: number up to delimiter */
  123. static int get_port(const char *data, int start, size_t dlen, char delim,
  124. u_int32_t array[2])
  125. {
  126. u_int16_t port = 0;
  127. int i;
  128. for (i = start; i < dlen; i++) {
  129. /* Finished? */
  130. if (data[i] == delim) {
  131. if (port == 0)
  132. break;
  133. array[0] = port >> 8;
  134. array[1] = port;
  135. return i + 1;
  136. }
  137. else if (data[i] >= '0' && data[i] <= '9')
  138. port = port*10 + data[i] - '0';
  139. else /* Some other crap */
  140. break;
  141. }
  142. return 0;
  143. }
  144. /* Returns 0, or length of numbers: |1|132.235.1.2|6275| */
  145. static int try_eprt(const char *data, size_t dlen, u_int32_t array[6],
  146. char term)
  147. {
  148. char delim;
  149. int length;
  150. /* First character is delimiter, then "1" for IPv4, then
  151. delimiter again. */
  152. if (dlen <= 3) return 0;
  153. delim = data[0];
  154. if (isdigit(delim) || delim < 33 || delim > 126
  155. || data[1] != '1' || data[2] != delim)
  156. return 0;
  157. DEBUGP("EPRT: Got |1|!\n");
  158. /* Now we have IP address. */
  159. length = try_number(data + 3, dlen - 3, array, 4, '.', delim);
  160. if (length == 0)
  161. return 0;
  162. DEBUGP("EPRT: Got IP address!\n");
  163. /* Start offset includes initial "|1|", and trailing delimiter */
  164. return get_port(data, 3 + length + 1, dlen, delim, array+4);
  165. }
  166. /* Returns 0, or length of numbers: |||6446| */
  167. static int try_epsv_response(const char *data, size_t dlen, u_int32_t array[6],
  168. char term)
  169. {
  170. char delim;
  171. /* Three delimiters. */
  172. if (dlen <= 3) return 0;
  173. delim = data[0];
  174. if (isdigit(delim) || delim < 33 || delim > 126
  175. || data[1] != delim || data[2] != delim)
  176. return 0;
  177. return get_port(data, 3, dlen, delim, array+4);
  178. }
  179. /* Return 1 for match, 0 for accept, -1 for partial. */
  180. static int find_pattern(const char *data, size_t dlen,
  181. const char *pattern, size_t plen,
  182. char skip, char term,
  183. unsigned int *numoff,
  184. unsigned int *numlen,
  185. u_int32_t array[6],
  186. int (*getnum)(const char *, size_t, u_int32_t[], char))
  187. {
  188. size_t i;
  189. DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
  190. if (dlen == 0)
  191. return 0;
  192. if (dlen <= plen) {
  193. /* Short packet: try for partial? */
  194. if (strnicmp(data, pattern, dlen) == 0)
  195. return -1;
  196. else return 0;
  197. }
  198. if (strnicmp(data, pattern, plen) != 0) {
  199. #if 0
  200. size_t i;
  201. DEBUGP("ftp: string mismatch\n");
  202. for (i = 0; i < plen; i++) {
  203. DEBUGP("ftp:char %u `%c'(%u) vs `%c'(%u)\n",
  204. i, data[i], data[i],
  205. pattern[i], pattern[i]);
  206. }
  207. #endif
  208. return 0;
  209. }
  210. DEBUGP("Pattern matches!\n");
  211. /* Now we've found the constant string, try to skip
  212. to the 'skip' character */
  213. for (i = plen; data[i] != skip; i++)
  214. if (i == dlen - 1) return -1;
  215. /* Skip over the last character */
  216. i++;
  217. DEBUGP("Skipped up to `%c'!\n", skip);
  218. *numoff = i;
  219. *numlen = getnum(data + i, dlen - i, array, term);
  220. if (!*numlen)
  221. return -1;
  222. DEBUGP("Match succeeded!\n");
  223. return 1;
  224. }
  225. /* Look up to see if we're just after a \n. */
  226. static int find_nl_seq(u32 seq, const struct ip_ct_ftp_master *info, int dir)
  227. {
  228. unsigned int i;
  229. for (i = 0; i < info->seq_aft_nl_num[dir]; i++)
  230. if (info->seq_aft_nl[dir][i] == seq)
  231. return 1;
  232. return 0;
  233. }
  234. /* We don't update if it's older than what we have. */
  235. static void update_nl_seq(u32 nl_seq, struct ip_ct_ftp_master *info, int dir,
  236. struct sk_buff *skb)
  237. {
  238. unsigned int i, oldest = NUM_SEQ_TO_REMEMBER;
  239. /* Look for oldest: if we find exact match, we're done. */
  240. for (i = 0; i < info->seq_aft_nl_num[dir]; i++) {
  241. if (info->seq_aft_nl[dir][i] == nl_seq)
  242. return;
  243. if (oldest == info->seq_aft_nl_num[dir]
  244. || before(info->seq_aft_nl[dir][i], oldest))
  245. oldest = i;
  246. }
  247. if (info->seq_aft_nl_num[dir] < NUM_SEQ_TO_REMEMBER) {
  248. info->seq_aft_nl[dir][info->seq_aft_nl_num[dir]++] = nl_seq;
  249. ip_conntrack_event_cache(IPCT_HELPINFO_VOLATILE, skb);
  250. } else if (oldest != NUM_SEQ_TO_REMEMBER) {
  251. info->seq_aft_nl[dir][oldest] = nl_seq;
  252. ip_conntrack_event_cache(IPCT_HELPINFO_VOLATILE, skb);
  253. }
  254. }
  255. static int help(struct sk_buff **pskb,
  256. struct ip_conntrack *ct,
  257. enum ip_conntrack_info ctinfo)
  258. {
  259. unsigned int dataoff, datalen;
  260. struct tcphdr _tcph, *th;
  261. char *fb_ptr;
  262. int ret;
  263. u32 seq, array[6] = { 0 };
  264. int dir = CTINFO2DIR(ctinfo);
  265. unsigned int matchlen, matchoff;
  266. struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
  267. struct ip_conntrack_expect *exp;
  268. unsigned int i;
  269. int found = 0, ends_in_nl;
  270. typeof(ip_nat_ftp_hook) ip_nat_ftp;
  271. /* Until there's been traffic both ways, don't look in packets. */
  272. if (ctinfo != IP_CT_ESTABLISHED
  273. && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
  274. DEBUGP("ftp: Conntrackinfo = %u\n", ctinfo);
  275. return NF_ACCEPT;
  276. }
  277. th = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
  278. sizeof(_tcph), &_tcph);
  279. if (th == NULL)
  280. return NF_ACCEPT;
  281. dataoff = (*pskb)->nh.iph->ihl*4 + th->doff*4;
  282. /* No data? */
  283. if (dataoff >= (*pskb)->len) {
  284. DEBUGP("ftp: pskblen = %u\n", (*pskb)->len);
  285. return NF_ACCEPT;
  286. }
  287. datalen = (*pskb)->len - dataoff;
  288. spin_lock_bh(&ip_ftp_lock);
  289. fb_ptr = skb_header_pointer(*pskb, dataoff,
  290. (*pskb)->len - dataoff, ftp_buffer);
  291. BUG_ON(fb_ptr == NULL);
  292. ends_in_nl = (fb_ptr[datalen - 1] == '\n');
  293. seq = ntohl(th->seq) + datalen;
  294. /* Look up to see if we're just after a \n. */
  295. if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
  296. /* Now if this ends in \n, update ftp info. */
  297. DEBUGP("ip_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
  298. ct_ftp_info->seq_aft_nl[0][dir]
  299. old_seq_aft_nl_set ? "":"(UNSET) ", old_seq_aft_nl);
  300. ret = NF_ACCEPT;
  301. goto out_update_nl;
  302. }
  303. /* Initialize IP array to expected address (it's not mentioned
  304. in EPSV responses) */
  305. array[0] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 24) & 0xFF;
  306. array[1] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 16) & 0xFF;
  307. array[2] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 8) & 0xFF;
  308. array[3] = ntohl(ct->tuplehash[dir].tuple.src.ip) & 0xFF;
  309. for (i = 0; i < ARRAY_SIZE(search[dir]); i++) {
  310. found = find_pattern(fb_ptr, (*pskb)->len - dataoff,
  311. search[dir][i].pattern,
  312. search[dir][i].plen,
  313. search[dir][i].skip,
  314. search[dir][i].term,
  315. &matchoff, &matchlen,
  316. array,
  317. search[dir][i].getnum);
  318. if (found) break;
  319. }
  320. if (found == -1) {
  321. /* We don't usually drop packets. After all, this is
  322. connection tracking, not packet filtering.
  323. However, it is necessary for accurate tracking in
  324. this case. */
  325. if (net_ratelimit())
  326. printk("conntrack_ftp: partial %s %u+%u\n",
  327. search[dir][i].pattern,
  328. ntohl(th->seq), datalen);
  329. ret = NF_DROP;
  330. goto out;
  331. } else if (found == 0) { /* No match */
  332. ret = NF_ACCEPT;
  333. goto out_update_nl;
  334. }
  335. DEBUGP("conntrack_ftp: match `%s' (%u bytes at %u)\n",
  336. fb_ptr + matchoff, matchlen, ntohl(th->seq) + matchoff);
  337. /* Allocate expectation which will be inserted */
  338. exp = ip_conntrack_expect_alloc(ct);
  339. if (exp == NULL) {
  340. ret = NF_DROP;
  341. goto out;
  342. }
  343. /* We refer to the reverse direction ("!dir") tuples here,
  344. * because we're expecting something in the other direction.
  345. * Doesn't matter unless NAT is happening. */
  346. exp->tuple.dst.ip = ct->tuplehash[!dir].tuple.dst.ip;
  347. if (htonl((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3])
  348. != ct->tuplehash[dir].tuple.src.ip) {
  349. /* Enrico Scholz's passive FTP to partially RNAT'd ftp
  350. server: it really wants us to connect to a
  351. different IP address. Simply don't record it for
  352. NAT. */
  353. DEBUGP("conntrack_ftp: NOT RECORDING: %u,%u,%u,%u != %u.%u.%u.%u\n",
  354. array[0], array[1], array[2], array[3],
  355. NIPQUAD(ct->tuplehash[dir].tuple.src.ip));
  356. /* Thanks to Cristiano Lincoln Mattos
  357. <lincoln@cesar.org.br> for reporting this potential
  358. problem (DMZ machines opening holes to internal
  359. networks, or the packet filter itself). */
  360. if (!loose) {
  361. ret = NF_ACCEPT;
  362. goto out_put_expect;
  363. }
  364. exp->tuple.dst.ip = htonl((array[0] << 24) | (array[1] << 16)
  365. | (array[2] << 8) | array[3]);
  366. }
  367. exp->tuple.src.ip = ct->tuplehash[!dir].tuple.src.ip;
  368. exp->tuple.dst.u.tcp.port = htons(array[4] << 8 | array[5]);
  369. exp->tuple.src.u.tcp.port = 0; /* Don't care. */
  370. exp->tuple.dst.protonum = IPPROTO_TCP;
  371. exp->mask = ((struct ip_conntrack_tuple)
  372. { { htonl(0xFFFFFFFF), { 0 } },
  373. { htonl(0xFFFFFFFF), { .tcp = { htons(0xFFFF) } }, 0xFF }});
  374. exp->expectfn = NULL;
  375. exp->flags = 0;
  376. /* Now, NAT might want to mangle the packet, and register the
  377. * (possibly changed) expectation itself. */
  378. ip_nat_ftp = rcu_dereference(ip_nat_ftp_hook);
  379. if (ip_nat_ftp)
  380. ret = ip_nat_ftp(pskb, ctinfo, search[dir][i].ftptype,
  381. matchoff, matchlen, exp, &seq);
  382. else {
  383. /* Can't expect this? Best to drop packet now. */
  384. if (ip_conntrack_expect_related(exp) != 0)
  385. ret = NF_DROP;
  386. else
  387. ret = NF_ACCEPT;
  388. }
  389. out_put_expect:
  390. ip_conntrack_expect_put(exp);
  391. out_update_nl:
  392. /* Now if this ends in \n, update ftp info. Seq may have been
  393. * adjusted by NAT code. */
  394. if (ends_in_nl)
  395. update_nl_seq(seq, ct_ftp_info,dir, *pskb);
  396. out:
  397. spin_unlock_bh(&ip_ftp_lock);
  398. return ret;
  399. }
  400. static struct ip_conntrack_helper ftp[MAX_PORTS];
  401. static char ftp_names[MAX_PORTS][sizeof("ftp-65535")];
  402. /* Not __exit: called from init() */
  403. static void ip_conntrack_ftp_fini(void)
  404. {
  405. int i;
  406. for (i = 0; i < ports_c; i++) {
  407. DEBUGP("ip_ct_ftp: unregistering helper for port %d\n",
  408. ports[i]);
  409. ip_conntrack_helper_unregister(&ftp[i]);
  410. }
  411. kfree(ftp_buffer);
  412. }
  413. static int __init ip_conntrack_ftp_init(void)
  414. {
  415. int i, ret;
  416. char *tmpname;
  417. ftp_buffer = kmalloc(65536, GFP_KERNEL);
  418. if (!ftp_buffer)
  419. return -ENOMEM;
  420. if (ports_c == 0)
  421. ports[ports_c++] = FTP_PORT;
  422. for (i = 0; i < ports_c; i++) {
  423. ftp[i].tuple.src.u.tcp.port = htons(ports[i]);
  424. ftp[i].tuple.dst.protonum = IPPROTO_TCP;
  425. ftp[i].mask.src.u.tcp.port = htons(0xFFFF);
  426. ftp[i].mask.dst.protonum = 0xFF;
  427. ftp[i].max_expected = 1;
  428. ftp[i].timeout = 5 * 60; /* 5 minutes */
  429. ftp[i].me = THIS_MODULE;
  430. ftp[i].help = help;
  431. tmpname = &ftp_names[i][0];
  432. if (ports[i] == FTP_PORT)
  433. sprintf(tmpname, "ftp");
  434. else
  435. sprintf(tmpname, "ftp-%d", ports[i]);
  436. ftp[i].name = tmpname;
  437. DEBUGP("ip_ct_ftp: registering helper for port %d\n",
  438. ports[i]);
  439. ret = ip_conntrack_helper_register(&ftp[i]);
  440. if (ret) {
  441. ip_conntrack_ftp_fini();
  442. return ret;
  443. }
  444. }
  445. return 0;
  446. }
  447. module_init(ip_conntrack_ftp_init);
  448. module_exit(ip_conntrack_ftp_fini);