ip_vs_ftp.c 9.2 KB

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