2 * ip_vs_ftp.c: IPVS ftp application module
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Most code here is taken from ip_masq_ftp.c in kernel 2.2. The difference
15 * is that ip_vs_ftp module handles the reverse direction to ip_masq_ftp.
17 * IP_MASQ_FTP ftp masquerading module
19 * Version: @(#)ip_masq_ftp.c 0.04 02/05/96
21 * Author: Wouter Gadeyne
24 * Code for ip_vs_expect_related and ip_vs_expect_callback is taken from
25 * http://www.ssi.bg/~ja/nfct/:
27 * ip_vs_nfct.c: Netfilter connection tracking support for IPVS
29 * Portions Copyright (C) 2001-2002
30 * Antefacto Ltd, 181 Parnell St, Dublin 1, Ireland.
32 * Portions Copyright (C) 2003-2008
36 #define KMSG_COMPONENT "IPVS"
37 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/kernel.h>
42 #include <linux/skbuff.h>
45 #include <linux/netfilter.h>
46 #include <net/netfilter/nf_conntrack.h>
47 #include <net/netfilter/nf_conntrack_expect.h>
48 #include <net/netfilter/nf_nat.h>
49 #include <net/netfilter/nf_nat_helper.h>
50 #include <linux/gfp.h>
51 #include <net/protocol.h>
53 #include <asm/unaligned.h>
55 #include <net/ip_vs.h>
58 #define SERVER_STRING "227 Entering Passive Mode ("
59 #define CLIENT_STRING "PORT "
61 #define FMT_TUPLE "%pI4:%u->%pI4:%u/%u"
62 #define ARG_TUPLE(T) &(T)->src.u3.ip, ntohs((T)->src.u.all), \
63 &(T)->dst.u3.ip, ntohs((T)->dst.u.all), \
66 #define FMT_CONN "%pI4:%u->%pI4:%u->%pI4:%u/%u:%u"
67 #define ARG_CONN(C) &((C)->caddr.ip), ntohs((C)->cport), \
68 &((C)->vaddr.ip), ntohs((C)->vport), \
69 &((C)->daddr.ip), ntohs((C)->dport), \
70 (C)->protocol, (C)->state
73 * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper
74 * First port is set to the default port.
76 static unsigned short ports[IP_VS_APP_MAX_PORTS] = {21, 0};
77 module_param_array(ports, ushort, NULL, 0);
78 MODULE_PARM_DESC(ports, "Ports to monitor for FTP control commands");
82 static int ip_vs_ftp_pasv;
86 ip_vs_ftp_init_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
93 ip_vs_ftp_done_conn(struct ip_vs_app *app, struct ip_vs_conn *cp)
100 * Get <addr,port> from the string "xxx.xxx.xxx.xxx,ppp,ppp", started
101 * with the "pattern" and terminated with the "term" character.
102 * <addr,port> is in network order.
104 static int ip_vs_ftp_get_addrport(char *data, char *data_limit,
105 const char *pattern, size_t plen, char term,
106 __be32 *addr, __be16 *port,
107 char **start, char **end)
112 if (data_limit - data < plen) {
113 /* check if there is partial match */
114 if (strnicmp(data, pattern, data_limit - data) == 0)
120 if (strnicmp(data, pattern, plen) != 0) {
123 *start = data + plen;
125 for (data = *start; *data != term; data++) {
126 if (data == data_limit)
131 memset(p, 0, sizeof(p));
132 for (data = *start; data != *end; data++) {
133 if (*data >= '0' && *data <= '9') {
134 p[i] = p[i]*10 + *data - '0';
135 } else if (*data == ',' && i < 5) {
138 /* unexpected character */
146 *addr = get_unaligned((__be32 *)p);
147 *port = get_unaligned((__be16 *)(p + 4));
152 * Called from init_conntrack() as expectfn handler.
155 ip_vs_expect_callback(struct nf_conn *ct,
156 struct nf_conntrack_expect *exp)
158 struct nf_conntrack_tuple *orig, new_reply;
159 struct ip_vs_conn *cp;
161 if (exp->tuple.src.l3num != PF_INET)
165 * We assume that no NF locks are held before this callback.
166 * ip_vs_conn_out_get and ip_vs_conn_in_get should match their
167 * expectations even if they use wildcard values, now we provide the
168 * actual values from the newly created original conntrack direction.
169 * The conntrack is confirmed when packet reaches IPVS hooks.
173 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
174 cp = ip_vs_conn_out_get(exp->tuple.src.l3num, orig->dst.protonum,
175 &orig->src.u3, orig->src.u.tcp.port,
176 &orig->dst.u3, orig->dst.u.tcp.port);
178 /* Change reply CLIENT->RS to CLIENT->VS */
179 new_reply = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
180 IP_VS_DBG(7, "%s(): ct=%p, status=0x%lX, tuples=" FMT_TUPLE ", "
181 FMT_TUPLE ", found inout cp=" FMT_CONN "\n",
182 __func__, ct, ct->status,
183 ARG_TUPLE(orig), ARG_TUPLE(&new_reply),
185 new_reply.dst.u3 = cp->vaddr;
186 new_reply.dst.u.tcp.port = cp->vport;
187 IP_VS_DBG(7, "%s(): ct=%p, new tuples=" FMT_TUPLE ", " FMT_TUPLE
188 ", inout cp=" FMT_CONN "\n",
190 ARG_TUPLE(orig), ARG_TUPLE(&new_reply),
196 cp = ip_vs_conn_in_get(exp->tuple.src.l3num, orig->dst.protonum,
197 &orig->src.u3, orig->src.u.tcp.port,
198 &orig->dst.u3, orig->dst.u.tcp.port);
200 /* Change reply VS->CLIENT to RS->CLIENT */
201 new_reply = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
202 IP_VS_DBG(7, "%s(): ct=%p, status=0x%lX, tuples=" FMT_TUPLE ", "
203 FMT_TUPLE ", found outin cp=" FMT_CONN "\n",
204 __func__, ct, ct->status,
205 ARG_TUPLE(orig), ARG_TUPLE(&new_reply),
207 new_reply.src.u3 = cp->daddr;
208 new_reply.src.u.tcp.port = cp->dport;
209 IP_VS_DBG(7, "%s(): ct=%p, new tuples=" FMT_TUPLE ", "
210 FMT_TUPLE ", outin cp=" FMT_CONN "\n",
212 ARG_TUPLE(orig), ARG_TUPLE(&new_reply),
217 IP_VS_DBG(7, "%s(): ct=%p, status=0x%lX, tuple=" FMT_TUPLE
218 " - unknown expect\n",
219 __func__, ct, ct->status, ARG_TUPLE(orig));
223 /* Never alter conntrack for non-NAT conns */
224 if (IP_VS_FWD_METHOD(cp) == IP_VS_CONN_F_MASQ)
225 nf_conntrack_alter_reply(ct, &new_reply);
231 * Create NF conntrack expectation with wildcard (optional) source port.
232 * Then the default callback function will alter the reply and will confirm
233 * the conntrack entry when the first packet comes.
236 ip_vs_expect_related(struct sk_buff *skb, struct nf_conn *ct,
237 struct ip_vs_conn *cp, u_int8_t proto,
238 const __be16 *port, int from_rs)
240 struct nf_conntrack_expect *exp;
242 BUG_ON(!ct || ct == &nf_conntrack_untracked);
244 exp = nf_ct_expect_alloc(ct);
249 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
250 nf_ct_l3num(ct), &cp->daddr, &cp->caddr,
251 proto, port, &cp->cport);
253 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
254 nf_ct_l3num(ct), &cp->caddr, &cp->vaddr,
255 proto, port, &cp->vport);
257 exp->expectfn = ip_vs_expect_callback;
259 IP_VS_DBG(7, "%s(): ct=%p, expect tuple=" FMT_TUPLE "\n",
260 __func__, ct, ARG_TUPLE(&exp->tuple));
261 nf_ct_expect_related(exp);
262 nf_ct_expect_put(exp);
266 * Look at outgoing ftp packets to catch the response to a PASV command
267 * from the server (inside-to-outside).
268 * When we see one, we build a connection entry with the client address,
269 * client port 0 (unknown at the moment), the server address and the
270 * server port. Mark the current connection entry as a control channel
271 * of the new entry. All this work is just to make the data connection
272 * can be scheduled to the right server later.
274 * The outgoing packet should be something like
275 * "227 Entering Passive Mode (xxx,xxx,xxx,xxx,ppp,ppp)".
276 * xxx,xxx,xxx,xxx is the server address, ppp,ppp is the server port number.
278 static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,
279 struct sk_buff *skb, int *diff)
283 char *data, *data_limit;
285 union nf_inet_addr from;
287 struct ip_vs_conn *n_cp;
288 char buf[24]; /* xxx.xxx.xxx.xxx,ppp,ppp\000 */
291 enum ip_conntrack_info ctinfo;
294 #ifdef CONFIG_IP_VS_IPV6
295 /* This application helper doesn't work with IPv6 yet,
296 * so turn this into a no-op for IPv6 packets
298 if (cp->af == AF_INET6)
304 /* Only useful for established sessions */
305 if (cp->state != IP_VS_TCP_S_ESTABLISHED)
308 /* Linear packets are much easier to deal with. */
309 if (!skb_make_writable(skb, skb->len))
312 if (cp->app_data == &ip_vs_ftp_pasv) {
314 th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
315 data = (char *)th + (th->doff << 2);
316 data_limit = skb_tail_pointer(skb);
318 if (ip_vs_ftp_get_addrport(data, data_limit,
320 sizeof(SERVER_STRING)-1, ')',
325 IP_VS_DBG(7, "PASV response (%pI4:%d) -> %pI4:%d detected\n",
326 &from.ip, ntohs(port), &cp->caddr.ip, 0);
329 * Now update or create an connection entry for it
331 n_cp = ip_vs_conn_out_get(AF_INET, iph->protocol, &from, port,
334 n_cp = ip_vs_conn_new(AF_INET, IPPROTO_TCP,
338 IP_VS_CONN_F_NO_CPORT,
343 /* add its controller */
344 ip_vs_control_add(n_cp, cp);
348 * Replace the old passive address with the new one
350 from.ip = n_cp->vaddr.ip;
352 snprintf(buf, sizeof(buf), "%u,%u,%u,%u,%u,%u",
353 ((unsigned char *)&from.ip)[0],
354 ((unsigned char *)&from.ip)[1],
355 ((unsigned char *)&from.ip)[2],
356 ((unsigned char *)&from.ip)[3],
360 buf_len = strlen(buf);
362 ct = nf_ct_get(skb, &ctinfo);
363 if (ct && !nf_ct_is_untracked(ct) && nfct_nat(ct)) {
364 /* If mangling fails this function will return 0
365 * which will cause the packet to be dropped.
366 * Mangling can only fail under memory pressure,
367 * hopefully it will succeed on the retransmitted
370 ret = nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
371 start-data, end-start,
374 ip_vs_expect_related(skb, ct, n_cp,
375 IPPROTO_TCP, NULL, 0);
379 * Not setting 'diff' is intentional, otherwise the sequence
380 * would be adjusted twice.
384 ip_vs_tcp_conn_listen(n_cp);
385 ip_vs_conn_put(n_cp);
393 * Look at incoming ftp packets to catch the PASV/PORT command
394 * (outside-to-inside).
396 * The incoming packet having the PORT command should be something like
397 * "PORT xxx,xxx,xxx,xxx,ppp,ppp\n".
398 * xxx,xxx,xxx,xxx is the client address, ppp,ppp is the client port number.
399 * In this case, we create a connection entry using the client address and
400 * port, so that the active ftp data connection from the server can reach
403 static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
404 struct sk_buff *skb, int *diff)
408 char *data, *data_start, *data_limit;
410 union nf_inet_addr to;
412 struct ip_vs_conn *n_cp;
414 #ifdef CONFIG_IP_VS_IPV6
415 /* This application helper doesn't work with IPv6 yet,
416 * so turn this into a no-op for IPv6 packets
418 if (cp->af == AF_INET6)
422 /* no diff required for incoming packets */
425 /* Only useful for established sessions */
426 if (cp->state != IP_VS_TCP_S_ESTABLISHED)
429 /* Linear packets are much easier to deal with. */
430 if (!skb_make_writable(skb, skb->len))
434 * Detecting whether it is passive
437 th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
439 /* Since there may be OPTIONS in the TCP packet and the HLEN is
440 the length of the header in 32-bit multiples, it is accurate
441 to calculate data address by th+HLEN*4 */
442 data = data_start = (char *)th + (th->doff << 2);
443 data_limit = skb_tail_pointer(skb);
445 while (data <= data_limit - 6) {
446 if (strnicmp(data, "PASV\r\n", 6) == 0) {
447 /* Passive mode on */
448 IP_VS_DBG(7, "got PASV at %td of %td\n",
450 data_limit - data_start);
451 cp->app_data = &ip_vs_ftp_pasv;
458 * To support virtual FTP server, the scenerio is as follows:
459 * FTP client ----> Load Balancer ----> FTP server
460 * First detect the port number in the application data,
461 * then create a new connection entry for the coming data
464 if (ip_vs_ftp_get_addrport(data_start, data_limit,
465 CLIENT_STRING, sizeof(CLIENT_STRING)-1,
470 IP_VS_DBG(7, "PORT %pI4:%d detected\n", &to.ip, ntohs(port));
472 /* Passive mode off */
476 * Now update or create a connection entry for it
478 IP_VS_DBG(7, "protocol %s %pI4:%d %pI4:%d\n",
479 ip_vs_proto_name(iph->protocol),
480 &to.ip, ntohs(port), &cp->vaddr.ip, 0);
482 n_cp = ip_vs_conn_in_get(AF_INET, iph->protocol,
484 &cp->vaddr, htons(ntohs(cp->vport)-1));
486 n_cp = ip_vs_conn_new(AF_INET, IPPROTO_TCP,
488 &cp->vaddr, htons(ntohs(cp->vport)-1),
489 &cp->daddr, htons(ntohs(cp->dport)-1),
495 /* add its controller */
496 ip_vs_control_add(n_cp, cp);
500 * Move tunnel to listen state
502 ip_vs_tcp_conn_listen(n_cp);
503 ip_vs_conn_put(n_cp);
509 static struct ip_vs_app ip_vs_ftp = {
511 .type = IP_VS_APP_TYPE_FTP,
512 .protocol = IPPROTO_TCP,
513 .module = THIS_MODULE,
514 .incs_list = LIST_HEAD_INIT(ip_vs_ftp.incs_list),
515 .init_conn = ip_vs_ftp_init_conn,
516 .done_conn = ip_vs_ftp_done_conn,
519 .pkt_out = ip_vs_ftp_out,
520 .pkt_in = ip_vs_ftp_in,
525 * ip_vs_ftp initialization
527 static int __init ip_vs_ftp_init(void)
530 struct ip_vs_app *app = &ip_vs_ftp;
532 ret = register_ip_vs_app(app);
536 for (i=0; i<IP_VS_APP_MAX_PORTS; i++) {
539 ret = register_ip_vs_app_inc(app, app->protocol, ports[i]);
542 pr_info("%s: loaded support on port[%d] = %d\n",
543 app->name, i, ports[i]);
547 unregister_ip_vs_app(app);
556 static void __exit ip_vs_ftp_exit(void)
558 unregister_ip_vs_app(&ip_vs_ftp);
562 module_init(ip_vs_ftp_init);
563 module_exit(ip_vs_ftp_exit);
564 MODULE_LICENSE("GPL");