ip_vs_ftp.c 9.2 KB

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