ip_vs_ftp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * ip_vs_ftp.c: IPVS ftp application module
  3. *
  4. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  5. *
  6. * Changes:
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Most code here is taken from ip_masq_ftp.c in kernel 2.2. The difference
  15. * is that ip_vs_ftp module handles the reverse direction to ip_masq_ftp.
  16. *
  17. * IP_MASQ_FTP ftp masquerading module
  18. *
  19. * Version: @(#)ip_masq_ftp.c 0.04 02/05/96
  20. *
  21. * Author: Wouter Gadeyne
  22. *
  23. */
  24. #define KMSG_COMPONENT "IPVS"
  25. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/in.h>
  31. #include <linux/ip.h>
  32. #include <linux/netfilter.h>
  33. #include <net/netfilter/nf_conntrack.h>
  34. #include <net/netfilter/nf_conntrack_expect.h>
  35. #include <net/netfilter/nf_nat.h>
  36. #include <net/netfilter/nf_nat_helper.h>
  37. #include <linux/gfp.h>
  38. #include <net/protocol.h>
  39. #include <net/tcp.h>
  40. #include <asm/unaligned.h>
  41. #include <net/ip_vs.h>
  42. #define SERVER_STRING "227 "
  43. #define CLIENT_STRING "PORT"
  44. /*
  45. * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper
  46. * First port is set to the default port.
  47. */
  48. static unsigned int ports_count = 1;
  49. static unsigned short ports[IP_VS_APP_MAX_PORTS] = {21, 0};
  50. module_param_array(ports, ushort, &ports_count, 0444);
  51. MODULE_PARM_DESC(ports, "Ports to monitor for FTP control commands");
  52. /* Dummy variable */
  53. static int ip_vs_ftp_pasv;
  54. static int
  55. ip_vs_ftp_init_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
  56. {
  57. /* We use connection tracking for the command connection */
  58. cp->flags |= IP_VS_CONN_F_NFCT;
  59. return 0;
  60. }
  61. static int
  62. ip_vs_ftp_done_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
  63. {
  64. return 0;
  65. }
  66. /*
  67. * Get <addr,port> from the string "xxx.xxx.xxx.xxx,ppp,ppp", started
  68. * with the "pattern", ignoring before "skip" and terminated with
  69. * the "term" character.
  70. * <addr,port> is in network order.
  71. */
  72. static int ip_vs_ftp_get_addrport(char *data, char *data_limit,
  73. const char *pattern, size_t plen,
  74. char skip, char term,
  75. __be32 *addr, __be16 *port,
  76. char **start, char **end)
  77. {
  78. char *s, c;
  79. unsigned char p[6];
  80. int i = 0;
  81. if (data_limit - data < plen) {
  82. /* check if there is partial match */
  83. if (strncasecmp(data, pattern, data_limit - data) == 0)
  84. return -1;
  85. else
  86. return 0;
  87. }
  88. if (strncasecmp(data, pattern, plen) != 0) {
  89. return 0;
  90. }
  91. s = data + plen;
  92. if (skip) {
  93. int found = 0;
  94. for (;; s++) {
  95. if (s == data_limit)
  96. return -1;
  97. if (!found) {
  98. if (*s == skip)
  99. found = 1;
  100. } else if (*s != skip) {
  101. break;
  102. }
  103. }
  104. }
  105. for (data = s; ; data++) {
  106. if (data == data_limit)
  107. return -1;
  108. if (*data == term)
  109. break;
  110. }
  111. *end = data;
  112. memset(p, 0, sizeof(p));
  113. for (data = s; ; data++) {
  114. c = *data;
  115. if (c == term)
  116. break;
  117. if (c >= '0' && c <= '9') {
  118. p[i] = p[i]*10 + c - '0';
  119. } else if (c == ',' && i < 5) {
  120. i++;
  121. } else {
  122. /* unexpected character */
  123. return -1;
  124. }
  125. }
  126. if (i != 5)
  127. return -1;
  128. *start = s;
  129. *addr = get_unaligned((__be32 *) p);
  130. *port = get_unaligned((__be16 *) (p + 4));
  131. return 1;
  132. }
  133. /*
  134. * Look at outgoing ftp packets to catch the response to a PASV command
  135. * from the server (inside-to-outside).
  136. * When we see one, we build a connection entry with the client address,
  137. * client port 0 (unknown at the moment), the server address and the
  138. * server port. Mark the current connection entry as a control channel
  139. * of the new entry. All this work is just to make the data connection
  140. * can be scheduled to the right server later.
  141. *
  142. * The outgoing packet should be something like
  143. * "227 Entering Passive Mode (xxx,xxx,xxx,xxx,ppp,ppp)".
  144. * xxx,xxx,xxx,xxx is the server address, ppp,ppp is the server port number.
  145. */
  146. static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,
  147. struct sk_buff *skb, int *diff)
  148. {
  149. struct iphdr *iph;
  150. struct tcphdr *th;
  151. char *data, *data_limit;
  152. char *start, *end;
  153. union nf_inet_addr from;
  154. __be16 port;
  155. struct ip_vs_conn *n_cp;
  156. char buf[24]; /* xxx.xxx.xxx.xxx,ppp,ppp\000 */
  157. unsigned int buf_len;
  158. int ret = 0;
  159. enum ip_conntrack_info ctinfo;
  160. struct nf_conn *ct;
  161. *diff = 0;
  162. #ifdef CONFIG_IP_VS_IPV6
  163. /* This application helper doesn't work with IPv6 yet,
  164. * so turn this into a no-op for IPv6 packets
  165. */
  166. if (cp->af == AF_INET6)
  167. return 1;
  168. #endif
  169. /* Only useful for established sessions */
  170. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  171. return 1;
  172. /* Linear packets are much easier to deal with. */
  173. if (!skb_make_writable(skb, skb->len))
  174. return 0;
  175. if (cp->app_data == &ip_vs_ftp_pasv) {
  176. iph = ip_hdr(skb);
  177. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  178. data = (char *)th + (th->doff << 2);
  179. data_limit = skb_tail_pointer(skb);
  180. if (ip_vs_ftp_get_addrport(data, data_limit,
  181. SERVER_STRING,
  182. sizeof(SERVER_STRING)-1,
  183. '(', ')',
  184. &from.ip, &port,
  185. &start, &end) != 1)
  186. return 1;
  187. IP_VS_DBG(7, "PASV response (%pI4:%d) -> %pI4:%d detected\n",
  188. &from.ip, ntohs(port), &cp->caddr.ip, 0);
  189. /*
  190. * Now update or create an connection entry for it
  191. */
  192. {
  193. struct ip_vs_conn_param p;
  194. ip_vs_conn_fill_param(cp->ipvs, AF_INET,
  195. iph->protocol, &from, port,
  196. &cp->caddr, 0, &p);
  197. n_cp = ip_vs_conn_out_get(&p);
  198. }
  199. if (!n_cp) {
  200. struct ip_vs_conn_param p;
  201. ip_vs_conn_fill_param(cp->ipvs,
  202. AF_INET, IPPROTO_TCP, &cp->caddr,
  203. 0, &cp->vaddr, port, &p);
  204. /* As above, this is ipv4 only */
  205. n_cp = ip_vs_conn_new(&p, AF_INET, &from, port,
  206. IP_VS_CONN_F_NO_CPORT |
  207. IP_VS_CONN_F_NFCT,
  208. cp->dest, skb->mark);
  209. if (!n_cp)
  210. return 0;
  211. /* add its controller */
  212. ip_vs_control_add(n_cp, cp);
  213. }
  214. /*
  215. * Replace the old passive address with the new one
  216. */
  217. from.ip = n_cp->vaddr.ip;
  218. port = n_cp->vport;
  219. snprintf(buf, sizeof(buf), "%u,%u,%u,%u,%u,%u",
  220. ((unsigned char *)&from.ip)[0],
  221. ((unsigned char *)&from.ip)[1],
  222. ((unsigned char *)&from.ip)[2],
  223. ((unsigned char *)&from.ip)[3],
  224. ntohs(port) >> 8,
  225. ntohs(port) & 0xFF);
  226. buf_len = strlen(buf);
  227. ct = nf_ct_get(skb, &ctinfo);
  228. if (ct && !nf_ct_is_untracked(ct) && nfct_nat(ct)) {
  229. /* If mangling fails this function will return 0
  230. * which will cause the packet to be dropped.
  231. * Mangling can only fail under memory pressure,
  232. * hopefully it will succeed on the retransmitted
  233. * packet.
  234. */
  235. rcu_read_lock();
  236. ret = nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
  237. iph->ihl * 4,
  238. start-data, end-start,
  239. buf, buf_len);
  240. rcu_read_unlock();
  241. if (ret) {
  242. ip_vs_nfct_expect_related(skb, ct, n_cp,
  243. IPPROTO_TCP, 0, 0);
  244. if (skb->ip_summed == CHECKSUM_COMPLETE)
  245. skb->ip_summed = CHECKSUM_UNNECESSARY;
  246. /* csum is updated */
  247. ret = 1;
  248. }
  249. }
  250. /*
  251. * Not setting 'diff' is intentional, otherwise the sequence
  252. * would be adjusted twice.
  253. */
  254. cp->app_data = NULL;
  255. ip_vs_tcp_conn_listen(n_cp);
  256. ip_vs_conn_put(n_cp);
  257. return ret;
  258. }
  259. return 1;
  260. }
  261. /*
  262. * Look at incoming ftp packets to catch the PASV/PORT command
  263. * (outside-to-inside).
  264. *
  265. * The incoming packet having the PORT command should be something like
  266. * "PORT xxx,xxx,xxx,xxx,ppp,ppp\n".
  267. * xxx,xxx,xxx,xxx is the client address, ppp,ppp is the client port number.
  268. * In this case, we create a connection entry using the client address and
  269. * port, so that the active ftp data connection from the server can reach
  270. * the client.
  271. */
  272. static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
  273. struct sk_buff *skb, int *diff)
  274. {
  275. struct iphdr *iph;
  276. struct tcphdr *th;
  277. char *data, *data_start, *data_limit;
  278. char *start, *end;
  279. union nf_inet_addr to;
  280. __be16 port;
  281. struct ip_vs_conn *n_cp;
  282. /* no diff required for incoming packets */
  283. *diff = 0;
  284. #ifdef CONFIG_IP_VS_IPV6
  285. /* This application helper doesn't work with IPv6 yet,
  286. * so turn this into a no-op for IPv6 packets
  287. */
  288. if (cp->af == AF_INET6)
  289. return 1;
  290. #endif
  291. /* Only useful for established sessions */
  292. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  293. return 1;
  294. /* Linear packets are much easier to deal with. */
  295. if (!skb_make_writable(skb, skb->len))
  296. return 0;
  297. /*
  298. * Detecting whether it is passive
  299. */
  300. iph = ip_hdr(skb);
  301. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  302. /* Since there may be OPTIONS in the TCP packet and the HLEN is
  303. the length of the header in 32-bit multiples, it is accurate
  304. to calculate data address by th+HLEN*4 */
  305. data = data_start = (char *)th + (th->doff << 2);
  306. data_limit = skb_tail_pointer(skb);
  307. while (data <= data_limit - 6) {
  308. if (strncasecmp(data, "PASV\r\n", 6) == 0) {
  309. /* Passive mode on */
  310. IP_VS_DBG(7, "got PASV at %td of %td\n",
  311. data - data_start,
  312. data_limit - data_start);
  313. cp->app_data = &ip_vs_ftp_pasv;
  314. return 1;
  315. }
  316. data++;
  317. }
  318. /*
  319. * To support virtual FTP server, the scenerio is as follows:
  320. * FTP client ----> Load Balancer ----> FTP server
  321. * First detect the port number in the application data,
  322. * then create a new connection entry for the coming data
  323. * connection.
  324. */
  325. if (ip_vs_ftp_get_addrport(data_start, data_limit,
  326. CLIENT_STRING, sizeof(CLIENT_STRING)-1,
  327. ' ', '\r', &to.ip, &port,
  328. &start, &end) != 1)
  329. return 1;
  330. IP_VS_DBG(7, "PORT %pI4:%d detected\n", &to.ip, ntohs(port));
  331. /* Passive mode off */
  332. cp->app_data = NULL;
  333. /*
  334. * Now update or create a connection entry for it
  335. */
  336. IP_VS_DBG(7, "protocol %s %pI4:%d %pI4:%d\n",
  337. ip_vs_proto_name(iph->protocol),
  338. &to.ip, ntohs(port), &cp->vaddr.ip, 0);
  339. {
  340. struct ip_vs_conn_param p;
  341. ip_vs_conn_fill_param(cp->ipvs, AF_INET,
  342. iph->protocol, &to, port, &cp->vaddr,
  343. htons(ntohs(cp->vport)-1), &p);
  344. n_cp = ip_vs_conn_in_get(&p);
  345. if (!n_cp) {
  346. /* This is ipv4 only */
  347. n_cp = ip_vs_conn_new(&p, AF_INET, &cp->daddr,
  348. htons(ntohs(cp->dport)-1),
  349. IP_VS_CONN_F_NFCT, cp->dest,
  350. skb->mark);
  351. if (!n_cp)
  352. return 0;
  353. /* add its controller */
  354. ip_vs_control_add(n_cp, cp);
  355. }
  356. }
  357. /*
  358. * Move tunnel to listen state
  359. */
  360. ip_vs_tcp_conn_listen(n_cp);
  361. ip_vs_conn_put(n_cp);
  362. return 1;
  363. }
  364. static struct ip_vs_app ip_vs_ftp = {
  365. .name = "ftp",
  366. .type = IP_VS_APP_TYPE_FTP,
  367. .protocol = IPPROTO_TCP,
  368. .module = THIS_MODULE,
  369. .incs_list = LIST_HEAD_INIT(ip_vs_ftp.incs_list),
  370. .init_conn = ip_vs_ftp_init_conn,
  371. .done_conn = ip_vs_ftp_done_conn,
  372. .bind_conn = NULL,
  373. .unbind_conn = NULL,
  374. .pkt_out = ip_vs_ftp_out,
  375. .pkt_in = ip_vs_ftp_in,
  376. };
  377. /*
  378. * per netns ip_vs_ftp initialization
  379. */
  380. static int __net_init __ip_vs_ftp_init(struct net *net)
  381. {
  382. int i, ret;
  383. struct ip_vs_app *app;
  384. struct netns_ipvs *ipvs = net_ipvs(net);
  385. if (!ipvs)
  386. return -ENOENT;
  387. app = register_ip_vs_app(ipvs, &ip_vs_ftp);
  388. if (IS_ERR(app))
  389. return PTR_ERR(app);
  390. for (i = 0; i < ports_count; i++) {
  391. if (!ports[i])
  392. continue;
  393. ret = register_ip_vs_app_inc(ipvs, app, app->protocol, ports[i]);
  394. if (ret)
  395. goto err_unreg;
  396. pr_info("%s: loaded support on port[%d] = %d\n",
  397. app->name, i, ports[i]);
  398. }
  399. return 0;
  400. err_unreg:
  401. unregister_ip_vs_app(ipvs, &ip_vs_ftp);
  402. return ret;
  403. }
  404. /*
  405. * netns exit
  406. */
  407. static void __ip_vs_ftp_exit(struct net *net)
  408. {
  409. struct netns_ipvs *ipvs = net_ipvs(net);
  410. if (!ipvs)
  411. return;
  412. unregister_ip_vs_app(ipvs, &ip_vs_ftp);
  413. }
  414. static struct pernet_operations ip_vs_ftp_ops = {
  415. .init = __ip_vs_ftp_init,
  416. .exit = __ip_vs_ftp_exit,
  417. };
  418. static int __init ip_vs_ftp_init(void)
  419. {
  420. int rv;
  421. rv = register_pernet_subsys(&ip_vs_ftp_ops);
  422. /* rcu_barrier() is called by netns on error */
  423. return rv;
  424. }
  425. /*
  426. * ip_vs_ftp finish.
  427. */
  428. static void __exit ip_vs_ftp_exit(void)
  429. {
  430. unregister_pernet_subsys(&ip_vs_ftp_ops);
  431. /* rcu_barrier() is called by netns */
  432. }
  433. module_init(ip_vs_ftp_init);
  434. module_exit(ip_vs_ftp_exit);
  435. MODULE_LICENSE("GPL");