OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [ipvs/] [ip_vs_ftp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * IP_VS        ftp application module
3
 *
4
 * Version:     $Id: ip_vs_ftp.c,v 1.1.1.1 2004-04-15 01:14:05 phoenix 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
 
27
#include <linux/module.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
 
35
#include <net/ip_vs.h>
36
 
37
 
38
#define SERVER_STRING "227 Entering Passive Mode ("
39
#define CLIENT_STRING "PORT "
40
 
41
 
42
/*
43
 * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper
44
 * First port is set to the default port.
45
 */
46
static int ports[IP_VS_APP_MAX_PORTS] = {21, 0};
47
struct ip_vs_app *incarnations[IP_VS_APP_MAX_PORTS];
48
 
49
/*
50
 *      Debug level
51
 */
52
#ifdef CONFIG_IP_VS_DEBUG
53
static int debug=0;
54
MODULE_PARM(debug, "i");
55
#endif
56
 
57
MODULE_PARM(ports, "1-" __MODULE_STRING(IP_VS_APP_MAX_PORTS) "i");
58
 
59
/*      Dummy variable */
60
static int ip_vs_ftp_pasv;
61
 
62
 
63
static int
64
ip_vs_ftp_init_conn(struct ip_vs_app *vapp, struct ip_vs_conn *cp)
65
{
66
        return 0;
67
}
68
 
69
 
70
static int
71
ip_vs_ftp_done_conn(struct ip_vs_app *vapp, struct ip_vs_conn *cp)
72
{
73
        return 0;
74
}
75
 
76
 
77
/*
78
 * Get <addr,port> from the string "xxx.xxx.xxx.xxx,ppp,ppp", started
79
 * with the "pattern" and terminated with the "term" character.
80
 * <addr,port> is in network order.
81
 */
82
static int ip_vs_ftp_get_addrport(char *data, char *data_limit,
83
                                  const char *pattern, size_t plen, char term,
84
                                  __u32 *addr, __u16 *port,
85
                                  char **start, char **end)
86
{
87
        unsigned char p[6];
88
        int i = 0;
89
 
90
        if (data_limit - data < plen) {
91
                /* check if there is partial match */
92
                if (strnicmp(data, pattern, data_limit - data) == 0)
93
                        return -1;
94
                else
95
                        return 0;
96
        }
97
 
98
        if (strnicmp(data, pattern, plen) != 0) {
99
                return 0;
100
        }
101
        *start = data + plen;
102
 
103
        for (data = *start; *data != term; data++) {
104
                if (data == data_limit)
105
                        return -1;
106
        }
107
        *end = data;
108
 
109
        memset(p, 0, sizeof(p));
110
        for (data = *start; data != *end; data++) {
111
                if (*data >= '0' && *data <= '9') {
112
                        p[i] = p[i]*10 + *data - '0';
113
                } else if (*data == ',' && i < 5) {
114
                        i++;
115
                } else {
116
                        /* unexpected character */
117
                        return -1;
118
                }
119
        }
120
 
121
        if (i != 5)
122
                return -1;
123
 
124
        *addr = (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
125
        *port = (p[5]<<8) | p[4];
126
        return 1;
127
}
128
 
129
 
130
/*
131
 * Look at outgoing ftp packets to catch the response to a PASV command
132
 * from the server (inside-to-outside).
133
 * When we see one, we build a connection entry with the client address,
134
 * client port 0 (unknown at the moment), the server address and the
135
 * server port.  Mark the current connection entry as a control channel
136
 * of the new entry. All this work is just to make the data connection
137
 * can be scheduled to the right server later.
138
 *
139
 * The outgoing packet should be something like
140
 *   "227 Entering Passive Mode (xxx,xxx,xxx,xxx,ppp,ppp)".
141
 * xxx,xxx,xxx,xxx is the server address, ppp,ppp is the server port number.
142
 */
143
static int ip_vs_ftp_out(struct ip_vs_app *vapp,
144
                         struct ip_vs_conn *cp, struct sk_buff *skb)
145
{
146
        struct iphdr *iph;
147
        struct tcphdr *th;
148
        char *data, *data_limit;
149
        char *start, *end;
150
        __u32 from;
151
        __u16 port;
152
        struct ip_vs_conn *n_cp;
153
        char buf[24];           /* xxx.xxx.xxx.xxx,ppp,ppp\000 */
154
        unsigned buf_len;
155
        int diff;
156
 
157
        /* Only useful for established sessions */
158
        if (cp->state != IP_VS_S_ESTABLISHED)
159
                return 0;
160
 
161
        if (cp->app_data == &ip_vs_ftp_pasv) {
162
                iph = skb->nh.iph;
163
                th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
164
                data = (char *)th + (th->doff << 2);
165
                data_limit = skb->tail;
166
 
167
                if (ip_vs_ftp_get_addrport(data, data_limit,
168
                                           SERVER_STRING,
169
                                           sizeof(SERVER_STRING)-1, ')',
170
                                           &from, &port,
171
                                           &start, &end) != 1)
172
                        return 0;
173
 
174
                IP_VS_DBG(1-debug, "PASV response (%u.%u.%u.%u:%d) -> "
175
                          "%u.%u.%u.%u:%d detected\n",
176
                          NIPQUAD(from), ntohs(port), NIPQUAD(cp->caddr), 0);
177
 
178
                /*
179
                 * Now update or create an connection entry for it
180
                 */
181
                n_cp = ip_vs_conn_out_get(iph->protocol, from, port,
182
                                          cp->caddr, 0);
183
                if (!n_cp) {
184
                        n_cp = ip_vs_conn_new(IPPROTO_TCP,
185
                                              cp->caddr, 0,
186
                                              cp->vaddr, port,
187
                                              from, port,
188
                                              IP_VS_CONN_F_NO_CPORT,
189
                                              cp->dest);
190
                        if (!n_cp)
191
                                return 0;
192
 
193
                        /* add its controller */
194
                        ip_vs_control_add(n_cp, cp);
195
 
196
                        /* increase dest's inactive connection counter */
197
                        if (cp->dest)
198
                                atomic_inc(&cp->dest->inactconns);
199
                }
200
 
201
                /*
202
                 * Replace the old passive address with the new one
203
                 */
204
                from = n_cp->vaddr;
205
                port = n_cp->vport;
206
                sprintf(buf,"%d,%d,%d,%d,%d,%d", NIPQUAD(from),
207
                        port&255, port>>8&255);
208
                buf_len = strlen(buf);
209
 
210
                /*
211
                 * Calculate required delta-offset to keep TCP happy
212
                 */
213
                diff = buf_len - (end-start);
214
 
215
                if (diff == 0) {
216
                        /* simply replace it with new passive address */
217
                        memcpy(start, buf, buf_len);
218
                } else {
219
                        /* fixme: return value isn't checked here */
220
                        ip_vs_skb_replace(skb, GFP_ATOMIC, start,
221
                                          end-start, buf, buf_len);
222
                }
223
 
224
                cp->app_data = NULL;
225
                ip_vs_conn_listen(n_cp);
226
                ip_vs_conn_put(n_cp);
227
                return diff;
228
        }
229
        return 0;
230
}
231
 
232
 
233
/*
234
 * Look at incoming ftp packets to catch the PASV/PORT command
235
 * (outside-to-inside).
236
 *
237
 * The incoming packet having the PORT command should be something like
238
 *      "PORT xxx,xxx,xxx,xxx,ppp,ppp\n".
239
 * xxx,xxx,xxx,xxx is the client address, ppp,ppp is the client port number.
240
 * In this case, we create a connection entry using the client address and
241
 * port, so that the active ftp data connection from the server can reach
242
 * the client.
243
 */
244
static int ip_vs_ftp_in(struct ip_vs_app *vapp,
245
                        struct ip_vs_conn *cp, struct sk_buff *skb)
246
{
247
        struct iphdr *iph;
248
        struct tcphdr *th;
249
        char *data, *data_start, *data_limit;
250
        char *start, *end;
251
        __u32 to;
252
        __u16 port;
253
        struct ip_vs_conn *n_cp;
254
 
255
        /* Only useful for established sessions */
256
        if (cp->state != IP_VS_S_ESTABLISHED)
257
                return 0;
258
 
259
        /*
260
         * Detecting whether it is passive
261
         */
262
        iph = skb->nh.iph;
263
        th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
264
 
265
        /* Since there may be OPTIONS in the TCP packet and the HLEN is
266
           the length of the header in 32-bit multiples, it is accurate
267
           to calculate data address by th+HLEN*4 */
268
        data = data_start = (char *)th + (th->doff << 2);
269
        data_limit = skb->tail;
270
 
271
        while (data <= data_limit - 6) {
272
                if (strnicmp(data, "PASV\r\n", 6) == 0) {
273
                        IP_VS_DBG(1-debug, "got PASV at %d of %d\n",
274
                                  data - data_start,
275
                                  data_limit - data_start);
276
                        cp->app_data = &ip_vs_ftp_pasv;
277
                        return 0;
278
                }
279
                data++;
280
        }
281
 
282
        /*
283
         * To support virtual FTP server, the scenerio is as follows:
284
         *       FTP client ----> Load Balancer ----> FTP server
285
         * First detect the port number in the application data,
286
         * then create a new connection entry for the coming data
287
         * connection.
288
         */
289
        if (ip_vs_ftp_get_addrport(data_start, data_limit,
290
                                   CLIENT_STRING, sizeof(CLIENT_STRING)-1,
291
                                   '\r', &to, &port,
292
                                   &start, &end) != 1)
293
                return 0;
294
 
295
        IP_VS_DBG(1-debug, "PORT %u.%u.%u.%u:%d detected\n",
296
                  NIPQUAD(to), ntohs(port));
297
 
298
        /*
299
         * Now update or create a connection entry for it
300
         */
301
        IP_VS_DBG(1-debug, "protocol %s %u.%u.%u.%u:%d %u.%u.%u.%u:%d\n",
302
                  ip_vs_proto_name(iph->protocol),
303
                  NIPQUAD(to), ntohs(port),
304
                  NIPQUAD(cp->vaddr), ntohs(cp->vport) - 1);
305
 
306
        n_cp = ip_vs_conn_in_get(iph->protocol,
307
                                 to, port,
308
                                 cp->vaddr, htons(ntohs(cp->vport)-1));
309
        if (!n_cp) {
310
                n_cp = ip_vs_conn_new(IPPROTO_TCP,
311
                                      to, port,
312
                                      cp->vaddr, htons(ntohs(cp->vport)-1),
313
                                      cp->daddr, htons(ntohs(cp->dport)-1),
314
                                      0,
315
                                      cp->dest);
316
                if (!n_cp)
317
                        return 0;
318
 
319
                /* add its controller */
320
                ip_vs_control_add(n_cp, cp);
321
 
322
                /* increase dest's inactive connection counter */
323
                if (cp->dest)
324
                        atomic_inc(&cp->dest->inactconns);
325
        }
326
 
327
        /*
328
         *      Move tunnel to listen state
329
         */
330
        ip_vs_conn_listen(n_cp);
331
        ip_vs_conn_put(n_cp);
332
 
333
        /* no diff required for incoming packets */
334
        return 0;
335
}
336
 
337
 
338
static struct ip_vs_app ip_vs_ftp = {
339
        {0},                     /* n_list */
340
        "ftp",                  /* name */
341
        0,                      /* type */
342
        THIS_MODULE,            /* this module */
343
        ip_vs_ftp_init_conn,    /* ip_vs_init_conn */
344
        ip_vs_ftp_done_conn,    /* ip_vs_done_conn */
345
        ip_vs_ftp_out,          /* pkt_out */
346
        ip_vs_ftp_in,           /* pkt_in */
347
};
348
 
349
 
350
/*
351
 *      ip_vs_ftp initialization
352
 */
353
static int __init ip_vs_ftp_init(void)
354
{
355
        int i, j;
356
 
357
        for (i=0; i<IP_VS_APP_MAX_PORTS; i++) {
358
                if (ports[i]) {
359
                        if (!(incarnations[i] =
360
                             kmalloc(sizeof(struct ip_vs_app), GFP_KERNEL)))
361
                                return -ENOMEM;
362
 
363
                        memcpy(incarnations[i], &ip_vs_ftp,
364
                               sizeof(struct ip_vs_app));
365
                        if ((j = register_ip_vs_app(incarnations[i],
366
                                                    IPPROTO_TCP,
367
                                                    ports[i]))) {
368
                                return j;
369
                        }
370
                        IP_VS_DBG(1-debug,
371
                                  "Ftp: loaded support on port[%d] = %d\n",
372
                                  i, ports[i]);
373
                } else {
374
                        /* To be safe, force the incarnation table entry
375
                           to be NULL */
376
                        incarnations[i] = NULL;
377
                }
378
        }
379
        return 0;
380
}
381
 
382
 
383
/*
384
 *      ip_vs_ftp finish.
385
 */
386
static void __exit ip_vs_ftp_exit(void)
387
{
388
        int i, j, k;
389
 
390
        k=0;
391
        for (i=0; i<IP_VS_APP_MAX_PORTS; i++) {
392
                if (incarnations[i]) {
393
                        if ((j = unregister_ip_vs_app(incarnations[i]))) {
394
                                k = j;
395
                        } else {
396
                                kfree(incarnations[i]);
397
                                incarnations[i] = NULL;
398
                                IP_VS_DBG(1-debug, "Ftp: unloaded support on port[%d] = %d\n",
399
                                          i, ports[i]);
400
                        }
401
                }
402
        }
403
}
404
 
405
 
406
module_init(ip_vs_ftp_init);
407
module_exit(ip_vs_ftp_exit);
408
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.