ip_vs_ftp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. /* 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. __u32 *addr, __u16 *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 = (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  98. *port = (p[5]<<8) | 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 **pskb, int *diff)
  116. {
  117. struct iphdr *iph;
  118. struct tcphdr *th;
  119. char *data, *data_limit;
  120. char *start, *end;
  121. __u32 from;
  122. __u16 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. *diff = 0;
  128. /* Only useful for established sessions */
  129. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  130. return 1;
  131. /* Linear packets are much easier to deal with. */
  132. if (!ip_vs_make_skb_writable(pskb, (*pskb)->len))
  133. return 0;
  134. if (cp->app_data == &ip_vs_ftp_pasv) {
  135. iph = (*pskb)->nh.iph;
  136. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  137. data = (char *)th + (th->doff << 2);
  138. data_limit = (*pskb)->tail;
  139. if (ip_vs_ftp_get_addrport(data, data_limit,
  140. SERVER_STRING,
  141. sizeof(SERVER_STRING)-1, ')',
  142. &from, &port,
  143. &start, &end) != 1)
  144. return 1;
  145. IP_VS_DBG(7, "PASV response (%u.%u.%u.%u:%d) -> "
  146. "%u.%u.%u.%u:%d detected\n",
  147. NIPQUAD(from), ntohs(port), NIPQUAD(cp->caddr), 0);
  148. /*
  149. * Now update or create an connection entry for it
  150. */
  151. n_cp = ip_vs_conn_out_get(iph->protocol, from, port,
  152. cp->caddr, 0);
  153. if (!n_cp) {
  154. n_cp = ip_vs_conn_new(IPPROTO_TCP,
  155. cp->caddr, 0,
  156. cp->vaddr, port,
  157. from, port,
  158. IP_VS_CONN_F_NO_CPORT,
  159. cp->dest);
  160. if (!n_cp)
  161. return 0;
  162. /* add its controller */
  163. ip_vs_control_add(n_cp, cp);
  164. }
  165. /*
  166. * Replace the old passive address with the new one
  167. */
  168. from = n_cp->vaddr;
  169. port = n_cp->vport;
  170. sprintf(buf,"%d,%d,%d,%d,%d,%d", NIPQUAD(from),
  171. port&255, (port>>8)&255);
  172. buf_len = strlen(buf);
  173. /*
  174. * Calculate required delta-offset to keep TCP happy
  175. */
  176. *diff = buf_len - (end-start);
  177. if (*diff == 0) {
  178. /* simply replace it with new passive address */
  179. memcpy(start, buf, buf_len);
  180. ret = 1;
  181. } else {
  182. ret = !ip_vs_skb_replace(*pskb, GFP_ATOMIC, start,
  183. end-start, buf, buf_len);
  184. }
  185. cp->app_data = NULL;
  186. ip_vs_tcp_conn_listen(n_cp);
  187. ip_vs_conn_put(n_cp);
  188. return ret;
  189. }
  190. return 1;
  191. }
  192. /*
  193. * Look at incoming ftp packets to catch the PASV/PORT command
  194. * (outside-to-inside).
  195. *
  196. * The incoming packet having the PORT command should be something like
  197. * "PORT xxx,xxx,xxx,xxx,ppp,ppp\n".
  198. * xxx,xxx,xxx,xxx is the client address, ppp,ppp is the client port number.
  199. * In this case, we create a connection entry using the client address and
  200. * port, so that the active ftp data connection from the server can reach
  201. * the client.
  202. */
  203. static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
  204. struct sk_buff **pskb, int *diff)
  205. {
  206. struct iphdr *iph;
  207. struct tcphdr *th;
  208. char *data, *data_start, *data_limit;
  209. char *start, *end;
  210. __u32 to;
  211. __u16 port;
  212. struct ip_vs_conn *n_cp;
  213. /* no diff required for incoming packets */
  214. *diff = 0;
  215. /* Only useful for established sessions */
  216. if (cp->state != IP_VS_TCP_S_ESTABLISHED)
  217. return 1;
  218. /* Linear packets are much easier to deal with. */
  219. if (!ip_vs_make_skb_writable(pskb, (*pskb)->len))
  220. return 0;
  221. /*
  222. * Detecting whether it is passive
  223. */
  224. iph = (*pskb)->nh.iph;
  225. th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
  226. /* Since there may be OPTIONS in the TCP packet and the HLEN is
  227. the length of the header in 32-bit multiples, it is accurate
  228. to calculate data address by th+HLEN*4 */
  229. data = data_start = (char *)th + (th->doff << 2);
  230. data_limit = (*pskb)->tail;
  231. while (data <= data_limit - 6) {
  232. if (strnicmp(data, "PASV\r\n", 6) == 0) {
  233. /* Passive mode on */
  234. IP_VS_DBG(7, "got PASV at %zd of %zd\n",
  235. data - data_start,
  236. data_limit - data_start);
  237. cp->app_data = &ip_vs_ftp_pasv;
  238. return 1;
  239. }
  240. data++;
  241. }
  242. /*
  243. * To support virtual FTP server, the scenerio is as follows:
  244. * FTP client ----> Load Balancer ----> FTP server
  245. * First detect the port number in the application data,
  246. * then create a new connection entry for the coming data
  247. * connection.
  248. */
  249. if (ip_vs_ftp_get_addrport(data_start, data_limit,
  250. CLIENT_STRING, sizeof(CLIENT_STRING)-1,
  251. '\r', &to, &port,
  252. &start, &end) != 1)
  253. return 1;
  254. IP_VS_DBG(7, "PORT %u.%u.%u.%u:%d detected\n",
  255. NIPQUAD(to), ntohs(port));
  256. /* Passive mode off */
  257. cp->app_data = NULL;
  258. /*
  259. * Now update or create a connection entry for it
  260. */
  261. IP_VS_DBG(7, "protocol %s %u.%u.%u.%u:%d %u.%u.%u.%u:%d\n",
  262. ip_vs_proto_name(iph->protocol),
  263. NIPQUAD(to), ntohs(port), NIPQUAD(cp->vaddr), 0);
  264. n_cp = ip_vs_conn_in_get(iph->protocol,
  265. to, port,
  266. cp->vaddr, htons(ntohs(cp->vport)-1));
  267. if (!n_cp) {
  268. n_cp = ip_vs_conn_new(IPPROTO_TCP,
  269. to, port,
  270. cp->vaddr, htons(ntohs(cp->vport)-1),
  271. cp->daddr, htons(ntohs(cp->dport)-1),
  272. 0,
  273. cp->dest);
  274. if (!n_cp)
  275. return 0;
  276. /* add its controller */
  277. ip_vs_control_add(n_cp, cp);
  278. }
  279. /*
  280. * Move tunnel to listen state
  281. */
  282. ip_vs_tcp_conn_listen(n_cp);
  283. ip_vs_conn_put(n_cp);
  284. return 1;
  285. }
  286. static struct ip_vs_app ip_vs_ftp = {
  287. .name = "ftp",
  288. .type = IP_VS_APP_TYPE_FTP,
  289. .protocol = IPPROTO_TCP,
  290. .module = THIS_MODULE,
  291. .incs_list = LIST_HEAD_INIT(ip_vs_ftp.incs_list),
  292. .init_conn = ip_vs_ftp_init_conn,
  293. .done_conn = ip_vs_ftp_done_conn,
  294. .bind_conn = NULL,
  295. .unbind_conn = NULL,
  296. .pkt_out = ip_vs_ftp_out,
  297. .pkt_in = ip_vs_ftp_in,
  298. };
  299. /*
  300. * ip_vs_ftp initialization
  301. */
  302. static int __init ip_vs_ftp_init(void)
  303. {
  304. int i, ret;
  305. struct ip_vs_app *app = &ip_vs_ftp;
  306. ret = register_ip_vs_app(app);
  307. if (ret)
  308. return ret;
  309. for (i=0; i<IP_VS_APP_MAX_PORTS; i++) {
  310. if (!ports[i])
  311. continue;
  312. if (ports[i] < 0 || ports[i] > 0xffff) {
  313. IP_VS_WARNING("ip_vs_ftp: Ignoring invalid "
  314. "configuration port[%d] = %d\n",
  315. i, ports[i]);
  316. continue;
  317. }
  318. ret = register_ip_vs_app_inc(app, app->protocol, ports[i]);
  319. if (ret)
  320. break;
  321. IP_VS_INFO("%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");