ip_vs_ftp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/kernel.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/in.h>
  29. #include <linux/ip.h>
  30. #include <linux/netfilter.h>
  31. #include <net/protocol.h>
  32. #include <net/tcp.h>
  33. #include <asm/unaligned.h>
  34. #include <net/ip_vs.h>
  35. #define SERVER_STRING "227 Entering Passive Mode ("
  36. #define CLIENT_STRING "PORT "
  37. /*
  38. * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper
  39. * First port is set to the default port.
  40. */
  41. static unsigned short ports[IP_VS_APP_MAX_PORTS] = {21, 0};
  42. module_param_array(ports, ushort, NULL, 0);
  43. MODULE_PARM_DESC(ports, "Ports to monitor for FTP control commands");
  44. /* Dummy variable */
  45. static int ip_vs_ftp_pasv;
  46. static int
  47. ip_vs_ftp_init_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
  48. {
  49. return 0;
  50. }
  51. static int
  52. ip_vs_ftp_done_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
  53. {
  54. return 0;
  55. }
  56. /*
  57. * Get <addr,port> from the string "xxx.xxx.xxx.xxx,ppp,ppp", started
  58. * with the "pattern" and terminated with the "term" character.
  59. * <addr,port> is in network order.
  60. */
  61. static int ip_vs_ftp_get_addrport(char *data, char *data_limit,
  62. const char *pattern, size_t plen, char term,
  63. __be32 *addr, __be16 *port,
  64. char **start, char **end)
  65. {
  66. unsigned char p[6];
  67. int i = 0;
  68. if (data_limit - data < plen) {
  69. /* check if there is partial match */
  70. if (strnicmp(data, pattern, data_limit - data) == 0)
  71. return -1;
  72. else
  73. return 0;
  74. }
  75. if (strnicmp(data, pattern, plen) != 0) {
  76. return 0;
  77. }
  78. *start = data + plen;
  79. for (data = *start; *data != term; data++) {
  80. if (data == data_limit)
  81. return -1;
  82. }
  83. *end = data;
  84. memset(p, 0, sizeof(p));
  85. for (data = *start; data != *end; data++) {
  86. if (*data >= '0' && *data <= '9') {
  87. p[i] = p[i]*10 + *data - '0';
  88. } else if (*data == ',' && i < 5) {
  89. i++;
  90. } else {
  91. /* unexpected character */
  92. return -1;
  93. }
  94. }
  95. if (i != 5)
  96. return -1;
  97. *addr = get_unaligned((__be32 *)p);
  98. *port = get_unaligned((__be16 *)(p + 4));
  99. return 1;
  100. }
  101. /*
  102. * Look at outgoing ftp packets to catch the response to a PASV command
  103. * from the server (inside-to-outside).
  104. * When we see one, we build a connection entry with the client address,
  105. * client port 0 (unknown at the moment), the server address and the
  106. * server port. Mark the current connection entry as a control channel
  107. * of the new entry. All this work is just to make the data connection
  108. * can be scheduled to the right server later.
  109. *
  110. * The outgoing packet should be something like
  111. * "227 Entering Passive Mode (xxx,xxx,xxx,xxx,ppp,ppp)".
  112. * xxx,xxx,xxx,xxx is the server address, ppp,ppp is the server port number.
  113. */
  114. static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,
  115. struct sk_buff *skb, int *diff)
  116. {
  117. struct iphdr *iph;
  118. struct tcphdr *th;
  119. char *data, *data_limit;
  120. char *start, *end;
  121. union nf_inet_addr from;
  122. __be16 port;
  123. struct ip_vs_conn *n_cp;
  124. char buf[24]; /* xxx.xxx.xxx.xxx,ppp,ppp\000 */
  125. unsigned buf_len;
  126. int ret;
  127. #ifdef CONFIG_IP_VS_IPV6
  128. /* This application helper doesn't work with IPv6 yet,
  129. * so turn this into a no-op for IPv6 packets
  130. */
  131. if (cp->af == AF_INET6)
  132. return 1;
  133. #endif
  134. *diff = 0;
  135. /* Only useful for established sessions */
  136. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  137. return 1;
  138. /* Linear packets are much easier to deal with. */
  139. if (!skb_make_writable(skb, skb->len))
  140. return 0;
  141. if (cp->app_data == &ip_vs_ftp_pasv) {
  142. iph = ip_hdr(skb);
  143. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  144. data = (char *)th + (th->doff << 2);
  145. data_limit = skb_tail_pointer(skb);
  146. if (ip_vs_ftp_get_addrport(data, data_limit,
  147. SERVER_STRING,
  148. sizeof(SERVER_STRING)-1, ')',
  149. &from.ip, &port,
  150. &start, &end) != 1)
  151. return 1;
  152. IP_VS_DBG(7, "PASV response (%u.%u.%u.%u:%d) -> "
  153. "%u.%u.%u.%u:%d detected\n",
  154. NIPQUAD(from.ip), ntohs(port),
  155. NIPQUAD(cp->caddr.ip), 0);
  156. /*
  157. * Now update or create an connection entry for it
  158. */
  159. n_cp = ip_vs_conn_out_get(AF_INET, iph->protocol, &from, port,
  160. &cp->caddr, 0);
  161. if (!n_cp) {
  162. n_cp = ip_vs_conn_new(AF_INET, IPPROTO_TCP,
  163. &cp->caddr, 0,
  164. &cp->vaddr, port,
  165. &from, port,
  166. IP_VS_CONN_F_NO_CPORT,
  167. cp->dest);
  168. if (!n_cp)
  169. return 0;
  170. /* add its controller */
  171. ip_vs_control_add(n_cp, cp);
  172. }
  173. /*
  174. * Replace the old passive address with the new one
  175. */
  176. from.ip = n_cp->vaddr.ip;
  177. port = n_cp->vport;
  178. sprintf(buf, "%d,%d,%d,%d,%d,%d", NIPQUAD(from.ip),
  179. (ntohs(port)>>8)&255, ntohs(port)&255);
  180. buf_len = strlen(buf);
  181. /*
  182. * Calculate required delta-offset to keep TCP happy
  183. */
  184. *diff = buf_len - (end-start);
  185. if (*diff == 0) {
  186. /* simply replace it with new passive address */
  187. memcpy(start, buf, buf_len);
  188. ret = 1;
  189. } else {
  190. ret = !ip_vs_skb_replace(skb, GFP_ATOMIC, start,
  191. end-start, buf, buf_len);
  192. }
  193. cp->app_data = NULL;
  194. ip_vs_tcp_conn_listen(n_cp);
  195. ip_vs_conn_put(n_cp);
  196. return ret;
  197. }
  198. return 1;
  199. }
  200. /*
  201. * Look at incoming ftp packets to catch the PASV/PORT command
  202. * (outside-to-inside).
  203. *
  204. * The incoming packet having the PORT command should be something like
  205. * "PORT xxx,xxx,xxx,xxx,ppp,ppp\n".
  206. * xxx,xxx,xxx,xxx is the client address, ppp,ppp is the client port number.
  207. * In this case, we create a connection entry using the client address and
  208. * port, so that the active ftp data connection from the server can reach
  209. * the client.
  210. */
  211. static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
  212. struct sk_buff *skb, int *diff)
  213. {
  214. struct iphdr *iph;
  215. struct tcphdr *th;
  216. char *data, *data_start, *data_limit;
  217. char *start, *end;
  218. union nf_inet_addr to;
  219. __be16 port;
  220. struct ip_vs_conn *n_cp;
  221. #ifdef CONFIG_IP_VS_IPV6
  222. /* This application helper doesn't work with IPv6 yet,
  223. * so turn this into a no-op for IPv6 packets
  224. */
  225. if (cp->af == AF_INET6)
  226. return 1;
  227. #endif
  228. /* no diff required for incoming packets */
  229. *diff = 0;
  230. /* Only useful for established sessions */
  231. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  232. return 1;
  233. /* Linear packets are much easier to deal with. */
  234. if (!skb_make_writable(skb, skb->len))
  235. return 0;
  236. /*
  237. * Detecting whether it is passive
  238. */
  239. iph = ip_hdr(skb);
  240. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  241. /* Since there may be OPTIONS in the TCP packet and the HLEN is
  242. the length of the header in 32-bit multiples, it is accurate
  243. to calculate data address by th+HLEN*4 */
  244. data = data_start = (char *)th + (th->doff << 2);
  245. data_limit = skb_tail_pointer(skb);
  246. while (data <= data_limit - 6) {
  247. if (strnicmp(data, "PASV\r\n", 6) == 0) {
  248. /* Passive mode on */
  249. IP_VS_DBG(7, "got PASV at %td of %td\n",
  250. data - data_start,
  251. data_limit - data_start);
  252. cp->app_data = &ip_vs_ftp_pasv;
  253. return 1;
  254. }
  255. data++;
  256. }
  257. /*
  258. * To support virtual FTP server, the scenerio is as follows:
  259. * FTP client ----> Load Balancer ----> FTP server
  260. * First detect the port number in the application data,
  261. * then create a new connection entry for the coming data
  262. * connection.
  263. */
  264. if (ip_vs_ftp_get_addrport(data_start, data_limit,
  265. CLIENT_STRING, sizeof(CLIENT_STRING)-1,
  266. '\r', &to.ip, &port,
  267. &start, &end) != 1)
  268. return 1;
  269. IP_VS_DBG(7, "PORT %u.%u.%u.%u:%d detected\n",
  270. NIPQUAD(to.ip), ntohs(port));
  271. /* Passive mode off */
  272. cp->app_data = NULL;
  273. /*
  274. * Now update or create a connection entry for it
  275. */
  276. IP_VS_DBG(7, "protocol %s %u.%u.%u.%u:%d %u.%u.%u.%u:%d\n",
  277. ip_vs_proto_name(iph->protocol),
  278. NIPQUAD(to.ip), ntohs(port), NIPQUAD(cp->vaddr.ip), 0);
  279. n_cp = ip_vs_conn_in_get(AF_INET, iph->protocol,
  280. &to, port,
  281. &cp->vaddr, htons(ntohs(cp->vport)-1));
  282. if (!n_cp) {
  283. n_cp = ip_vs_conn_new(AF_INET, IPPROTO_TCP,
  284. &to, port,
  285. &cp->vaddr, htons(ntohs(cp->vport)-1),
  286. &cp->daddr, htons(ntohs(cp->dport)-1),
  287. 0,
  288. cp->dest);
  289. if (!n_cp)
  290. return 0;
  291. /* add its controller */
  292. ip_vs_control_add(n_cp, cp);
  293. }
  294. /*
  295. * Move tunnel to listen state
  296. */
  297. ip_vs_tcp_conn_listen(n_cp);
  298. ip_vs_conn_put(n_cp);
  299. return 1;
  300. }
  301. static struct ip_vs_app ip_vs_ftp = {
  302. .name = "ftp",
  303. .type = IP_VS_APP_TYPE_FTP,
  304. .protocol = IPPROTO_TCP,
  305. .module = THIS_MODULE,
  306. .incs_list = LIST_HEAD_INIT(ip_vs_ftp.incs_list),
  307. .init_conn = ip_vs_ftp_init_conn,
  308. .done_conn = ip_vs_ftp_done_conn,
  309. .bind_conn = NULL,
  310. .unbind_conn = NULL,
  311. .pkt_out = ip_vs_ftp_out,
  312. .pkt_in = ip_vs_ftp_in,
  313. };
  314. /*
  315. * ip_vs_ftp initialization
  316. */
  317. static int __init ip_vs_ftp_init(void)
  318. {
  319. int i, ret;
  320. struct ip_vs_app *app = &ip_vs_ftp;
  321. ret = register_ip_vs_app(app);
  322. if (ret)
  323. return ret;
  324. for (i=0; i<IP_VS_APP_MAX_PORTS; i++) {
  325. if (!ports[i])
  326. continue;
  327. ret = register_ip_vs_app_inc(app, app->protocol, ports[i]);
  328. if (ret)
  329. break;
  330. IP_VS_INFO("%s: loaded support on port[%d] = %d\n",
  331. app->name, i, ports[i]);
  332. }
  333. if (ret)
  334. unregister_ip_vs_app(app);
  335. return ret;
  336. }
  337. /*
  338. * ip_vs_ftp finish.
  339. */
  340. static void __exit ip_vs_ftp_exit(void)
  341. {
  342. unregister_ip_vs_app(&ip_vs_ftp);
  343. }
  344. module_init(ip_vs_ftp_init);
  345. module_exit(ip_vs_ftp_exit);
  346. MODULE_LICENSE("GPL");