ip_vs_ftp.c 9.3 KB

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