ip_vs_ftp.c 9.2 KB

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