]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/ipvs/ip_vs_app.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/avi/kvm
[linux-2.6-omap-h63xx.git] / net / ipv4 / ipvs / ip_vs_app.c
1 /*
2  * ip_vs_app.c: Application module support for IPVS
3  *
4  * Version:     $Id: ip_vs_app.c,v 1.17 2003/03/22 06:31:21 wensong Exp $
5  *
6  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
7  *
8  *              This program is free software; you can redistribute it and/or
9  *              modify it under the terms of the GNU General Public License
10  *              as published by the Free Software Foundation; either version
11  *              2 of the License, or (at your option) any later version.
12  *
13  * Most code here is taken from ip_masq_app.c in kernel 2.2. The difference
14  * is that ip_vs_app module handles the reverse direction (incoming requests
15  * and outgoing responses).
16  *
17  *              IP_MASQ_APP application masquerading module
18  *
19  * Author:      Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
20  *
21  */
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/skbuff.h>
26 #include <linux/in.h>
27 #include <linux/ip.h>
28 #include <linux/netfilter.h>
29 #include <net/net_namespace.h>
30 #include <net/protocol.h>
31 #include <net/tcp.h>
32 #include <asm/system.h>
33 #include <linux/stat.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/mutex.h>
37
38 #include <net/ip_vs.h>
39
40 EXPORT_SYMBOL(register_ip_vs_app);
41 EXPORT_SYMBOL(unregister_ip_vs_app);
42 EXPORT_SYMBOL(register_ip_vs_app_inc);
43
44 /* ipvs application list head */
45 static LIST_HEAD(ip_vs_app_list);
46 static DEFINE_MUTEX(__ip_vs_app_mutex);
47
48
49 /*
50  *      Get an ip_vs_app object
51  */
52 static inline int ip_vs_app_get(struct ip_vs_app *app)
53 {
54         /* test and get the module atomically */
55         if (app->module)
56                 return try_module_get(app->module);
57         else
58                 return 1;
59 }
60
61
62 static inline void ip_vs_app_put(struct ip_vs_app *app)
63 {
64         if (app->module)
65                 module_put(app->module);
66 }
67
68
69 /*
70  *      Allocate/initialize app incarnation and register it in proto apps.
71  */
72 static int
73 ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
74 {
75         struct ip_vs_protocol *pp;
76         struct ip_vs_app *inc;
77         int ret;
78
79         if (!(pp = ip_vs_proto_get(proto)))
80                 return -EPROTONOSUPPORT;
81
82         if (!pp->unregister_app)
83                 return -EOPNOTSUPP;
84
85         inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
86         if (!inc)
87                 return -ENOMEM;
88         INIT_LIST_HEAD(&inc->p_list);
89         INIT_LIST_HEAD(&inc->incs_list);
90         inc->app = app;
91         inc->port = htons(port);
92         atomic_set(&inc->usecnt, 0);
93
94         if (app->timeouts) {
95                 inc->timeout_table =
96                         ip_vs_create_timeout_table(app->timeouts,
97                                                    app->timeouts_size);
98                 if (!inc->timeout_table) {
99                         ret = -ENOMEM;
100                         goto out;
101                 }
102         }
103
104         ret = pp->register_app(inc);
105         if (ret)
106                 goto out;
107
108         list_add(&inc->a_list, &app->incs_list);
109         IP_VS_DBG(9, "%s application %s:%u registered\n",
110                   pp->name, inc->name, inc->port);
111
112         return 0;
113
114   out:
115         kfree(inc->timeout_table);
116         kfree(inc);
117         return ret;
118 }
119
120
121 /*
122  *      Release app incarnation
123  */
124 static void
125 ip_vs_app_inc_release(struct ip_vs_app *inc)
126 {
127         struct ip_vs_protocol *pp;
128
129         if (!(pp = ip_vs_proto_get(inc->protocol)))
130                 return;
131
132         if (pp->unregister_app)
133                 pp->unregister_app(inc);
134
135         IP_VS_DBG(9, "%s App %s:%u unregistered\n",
136                   pp->name, inc->name, inc->port);
137
138         list_del(&inc->a_list);
139
140         kfree(inc->timeout_table);
141         kfree(inc);
142 }
143
144
145 /*
146  *      Get reference to app inc (only called from softirq)
147  *
148  */
149 int ip_vs_app_inc_get(struct ip_vs_app *inc)
150 {
151         int result;
152
153         atomic_inc(&inc->usecnt);
154         if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
155                 atomic_dec(&inc->usecnt);
156         return result;
157 }
158
159
160 /*
161  *      Put the app inc (only called from timer or net softirq)
162  */
163 void ip_vs_app_inc_put(struct ip_vs_app *inc)
164 {
165         ip_vs_app_put(inc->app);
166         atomic_dec(&inc->usecnt);
167 }
168
169
170 /*
171  *      Register an application incarnation in protocol applications
172  */
173 int
174 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
175 {
176         int result;
177
178         mutex_lock(&__ip_vs_app_mutex);
179
180         result = ip_vs_app_inc_new(app, proto, port);
181
182         mutex_unlock(&__ip_vs_app_mutex);
183
184         return result;
185 }
186
187
188 /*
189  *      ip_vs_app registration routine
190  */
191 int register_ip_vs_app(struct ip_vs_app *app)
192 {
193         /* increase the module use count */
194         ip_vs_use_count_inc();
195
196         mutex_lock(&__ip_vs_app_mutex);
197
198         list_add(&app->a_list, &ip_vs_app_list);
199
200         mutex_unlock(&__ip_vs_app_mutex);
201
202         return 0;
203 }
204
205
206 /*
207  *      ip_vs_app unregistration routine
208  *      We are sure there are no app incarnations attached to services
209  */
210 void unregister_ip_vs_app(struct ip_vs_app *app)
211 {
212         struct ip_vs_app *inc, *nxt;
213
214         mutex_lock(&__ip_vs_app_mutex);
215
216         list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
217                 ip_vs_app_inc_release(inc);
218         }
219
220         list_del(&app->a_list);
221
222         mutex_unlock(&__ip_vs_app_mutex);
223
224         /* decrease the module use count */
225         ip_vs_use_count_dec();
226 }
227
228
229 /*
230  *      Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
231  */
232 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
233 {
234         return pp->app_conn_bind(cp);
235 }
236
237
238 /*
239  *      Unbind cp from application incarnation (called by cp destructor)
240  */
241 void ip_vs_unbind_app(struct ip_vs_conn *cp)
242 {
243         struct ip_vs_app *inc = cp->app;
244
245         if (!inc)
246                 return;
247
248         if (inc->unbind_conn)
249                 inc->unbind_conn(inc, cp);
250         if (inc->done_conn)
251                 inc->done_conn(inc, cp);
252         ip_vs_app_inc_put(inc);
253         cp->app = NULL;
254 }
255
256
257 /*
258  *      Fixes th->seq based on ip_vs_seq info.
259  */
260 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
261 {
262         __u32 seq = ntohl(th->seq);
263
264         /*
265          *      Adjust seq with delta-offset for all packets after
266          *      the most recent resized pkt seq and with previous_delta offset
267          *      for all packets before most recent resized pkt seq.
268          */
269         if (vseq->delta || vseq->previous_delta) {
270                 if(after(seq, vseq->init_seq)) {
271                         th->seq = htonl(seq + vseq->delta);
272                         IP_VS_DBG(9, "vs_fix_seq(): added delta (%d) to seq\n",
273                                   vseq->delta);
274                 } else {
275                         th->seq = htonl(seq + vseq->previous_delta);
276                         IP_VS_DBG(9, "vs_fix_seq(): added previous_delta "
277                                   "(%d) to seq\n", vseq->previous_delta);
278                 }
279         }
280 }
281
282
283 /*
284  *      Fixes th->ack_seq based on ip_vs_seq info.
285  */
286 static inline void
287 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
288 {
289         __u32 ack_seq = ntohl(th->ack_seq);
290
291         /*
292          * Adjust ack_seq with delta-offset for
293          * the packets AFTER most recent resized pkt has caused a shift
294          * for packets before most recent resized pkt, use previous_delta
295          */
296         if (vseq->delta || vseq->previous_delta) {
297                 /* since ack_seq is the number of octet that is expected
298                    to receive next, so compare it with init_seq+delta */
299                 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
300                         th->ack_seq = htonl(ack_seq - vseq->delta);
301                         IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted delta "
302                                   "(%d) from ack_seq\n", vseq->delta);
303
304                 } else {
305                         th->ack_seq = htonl(ack_seq - vseq->previous_delta);
306                         IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted "
307                                   "previous_delta (%d) from ack_seq\n",
308                                   vseq->previous_delta);
309                 }
310         }
311 }
312
313
314 /*
315  *      Updates ip_vs_seq if pkt has been resized
316  *      Assumes already checked proto==IPPROTO_TCP and diff!=0.
317  */
318 static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
319                                  unsigned flag, __u32 seq, int diff)
320 {
321         /* spinlock is to keep updating cp->flags atomic */
322         spin_lock(&cp->lock);
323         if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
324                 vseq->previous_delta = vseq->delta;
325                 vseq->delta += diff;
326                 vseq->init_seq = seq;
327                 cp->flags |= flag;
328         }
329         spin_unlock(&cp->lock);
330 }
331
332 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
333                                   struct ip_vs_app *app)
334 {
335         int diff;
336         const unsigned int tcp_offset = ip_hdrlen(skb);
337         struct tcphdr *th;
338         __u32 seq;
339
340         if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
341                 return 0;
342
343         th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
344
345         /*
346          *      Remember seq number in case this pkt gets resized
347          */
348         seq = ntohl(th->seq);
349
350         /*
351          *      Fix seq stuff if flagged as so.
352          */
353         if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
354                 vs_fix_seq(&cp->out_seq, th);
355         if (cp->flags & IP_VS_CONN_F_IN_SEQ)
356                 vs_fix_ack_seq(&cp->in_seq, th);
357
358         /*
359          *      Call private output hook function
360          */
361         if (app->pkt_out == NULL)
362                 return 1;
363
364         if (!app->pkt_out(app, cp, skb, &diff))
365                 return 0;
366
367         /*
368          *      Update ip_vs seq stuff if len has changed.
369          */
370         if (diff != 0)
371                 vs_seq_update(cp, &cp->out_seq,
372                               IP_VS_CONN_F_OUT_SEQ, seq, diff);
373
374         return 1;
375 }
376
377 /*
378  *      Output pkt hook. Will call bound ip_vs_app specific function
379  *      called by ipvs packet handler, assumes previously checked cp!=NULL
380  *      returns false if it can't handle packet (oom)
381  */
382 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
383 {
384         struct ip_vs_app *app;
385
386         /*
387          *      check if application module is bound to
388          *      this ip_vs_conn.
389          */
390         if ((app = cp->app) == NULL)
391                 return 1;
392
393         /* TCP is complicated */
394         if (cp->protocol == IPPROTO_TCP)
395                 return app_tcp_pkt_out(cp, skb, app);
396
397         /*
398          *      Call private output hook function
399          */
400         if (app->pkt_out == NULL)
401                 return 1;
402
403         return app->pkt_out(app, cp, skb, NULL);
404 }
405
406
407 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
408                                  struct ip_vs_app *app)
409 {
410         int diff;
411         const unsigned int tcp_offset = ip_hdrlen(skb);
412         struct tcphdr *th;
413         __u32 seq;
414
415         if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
416                 return 0;
417
418         th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
419
420         /*
421          *      Remember seq number in case this pkt gets resized
422          */
423         seq = ntohl(th->seq);
424
425         /*
426          *      Fix seq stuff if flagged as so.
427          */
428         if (cp->flags & IP_VS_CONN_F_IN_SEQ)
429                 vs_fix_seq(&cp->in_seq, th);
430         if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
431                 vs_fix_ack_seq(&cp->out_seq, th);
432
433         /*
434          *      Call private input hook function
435          */
436         if (app->pkt_in == NULL)
437                 return 1;
438
439         if (!app->pkt_in(app, cp, skb, &diff))
440                 return 0;
441
442         /*
443          *      Update ip_vs seq stuff if len has changed.
444          */
445         if (diff != 0)
446                 vs_seq_update(cp, &cp->in_seq,
447                               IP_VS_CONN_F_IN_SEQ, seq, diff);
448
449         return 1;
450 }
451
452 /*
453  *      Input pkt hook. Will call bound ip_vs_app specific function
454  *      called by ipvs packet handler, assumes previously checked cp!=NULL.
455  *      returns false if can't handle packet (oom).
456  */
457 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
458 {
459         struct ip_vs_app *app;
460
461         /*
462          *      check if application module is bound to
463          *      this ip_vs_conn.
464          */
465         if ((app = cp->app) == NULL)
466                 return 1;
467
468         /* TCP is complicated */
469         if (cp->protocol == IPPROTO_TCP)
470                 return app_tcp_pkt_in(cp, skb, app);
471
472         /*
473          *      Call private input hook function
474          */
475         if (app->pkt_in == NULL)
476                 return 1;
477
478         return app->pkt_in(app, cp, skb, NULL);
479 }
480
481
482 #ifdef CONFIG_PROC_FS
483 /*
484  *      /proc/net/ip_vs_app entry function
485  */
486
487 static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
488 {
489         struct ip_vs_app *app, *inc;
490
491         list_for_each_entry(app, &ip_vs_app_list, a_list) {
492                 list_for_each_entry(inc, &app->incs_list, a_list) {
493                         if (pos-- == 0)
494                                 return inc;
495                 }
496         }
497         return NULL;
498
499 }
500
501 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
502 {
503         mutex_lock(&__ip_vs_app_mutex);
504
505         return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
506 }
507
508 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
509 {
510         struct ip_vs_app *inc, *app;
511         struct list_head *e;
512
513         ++*pos;
514         if (v == SEQ_START_TOKEN)
515                 return ip_vs_app_idx(0);
516
517         inc = v;
518         app = inc->app;
519
520         if ((e = inc->a_list.next) != &app->incs_list)
521                 return list_entry(e, struct ip_vs_app, a_list);
522
523         /* go on to next application */
524         for (e = app->a_list.next; e != &ip_vs_app_list; e = e->next) {
525                 app = list_entry(e, struct ip_vs_app, a_list);
526                 list_for_each_entry(inc, &app->incs_list, a_list) {
527                         return inc;
528                 }
529         }
530         return NULL;
531 }
532
533 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
534 {
535         mutex_unlock(&__ip_vs_app_mutex);
536 }
537
538 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
539 {
540         if (v == SEQ_START_TOKEN)
541                 seq_puts(seq, "prot port    usecnt name\n");
542         else {
543                 const struct ip_vs_app *inc = v;
544
545                 seq_printf(seq, "%-3s  %-7u %-6d %-17s\n",
546                            ip_vs_proto_name(inc->protocol),
547                            ntohs(inc->port),
548                            atomic_read(&inc->usecnt),
549                            inc->name);
550         }
551         return 0;
552 }
553
554 static const struct seq_operations ip_vs_app_seq_ops = {
555         .start = ip_vs_app_seq_start,
556         .next  = ip_vs_app_seq_next,
557         .stop  = ip_vs_app_seq_stop,
558         .show  = ip_vs_app_seq_show,
559 };
560
561 static int ip_vs_app_open(struct inode *inode, struct file *file)
562 {
563         return seq_open(file, &ip_vs_app_seq_ops);
564 }
565
566 static const struct file_operations ip_vs_app_fops = {
567         .owner   = THIS_MODULE,
568         .open    = ip_vs_app_open,
569         .read    = seq_read,
570         .llseek  = seq_lseek,
571         .release = seq_release,
572 };
573 #endif
574
575
576 /*
577  *      Replace a segment of data with a new segment
578  */
579 int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
580                       char *o_buf, int o_len, char *n_buf, int n_len)
581 {
582         int diff;
583         int o_offset;
584         int o_left;
585
586         EnterFunction(9);
587
588         diff = n_len - o_len;
589         o_offset = o_buf - (char *)skb->data;
590         /* The length of left data after o_buf+o_len in the skb data */
591         o_left = skb->len - (o_offset + o_len);
592
593         if (diff <= 0) {
594                 memmove(o_buf + n_len, o_buf + o_len, o_left);
595                 memcpy(o_buf, n_buf, n_len);
596                 skb_trim(skb, skb->len + diff);
597         } else if (diff <= skb_tailroom(skb)) {
598                 skb_put(skb, diff);
599                 memmove(o_buf + n_len, o_buf + o_len, o_left);
600                 memcpy(o_buf, n_buf, n_len);
601         } else {
602                 if (pskb_expand_head(skb, skb_headroom(skb), diff, pri))
603                         return -ENOMEM;
604                 skb_put(skb, diff);
605                 memmove(skb->data + o_offset + n_len,
606                         skb->data + o_offset + o_len, o_left);
607                 skb_copy_to_linear_data_offset(skb, o_offset, n_buf, n_len);
608         }
609
610         /* must update the iph total length here */
611         ip_hdr(skb)->tot_len = htons(skb->len);
612
613         LeaveFunction(9);
614         return 0;
615 }
616
617
618 int ip_vs_app_init(void)
619 {
620         /* we will replace it with proc_net_ipvs_create() soon */
621         proc_net_fops_create(&init_net, "ip_vs_app", 0, &ip_vs_app_fops);
622         return 0;
623 }
624
625
626 void ip_vs_app_cleanup(void)
627 {
628         proc_net_remove(&init_net, "ip_vs_app");
629 }