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