[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / net / ipv4 / ipvs / ip_vs_app.c
blobd9212addd1933a8a47fe4ca28aaae15b05762bcc
1 /*
2 * ip_vs_app.c: Application module support for IPVS
4 * Version: $Id: ip_vs_app.c,v 1.17 2003/03/22 06:31:21 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
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.
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).
17 * IP_MASQ_APP application masquerading module
19 * Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
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 <asm/system.h>
30 #include <linux/stat.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
34 #include <net/ip_vs.h>
36 EXPORT_SYMBOL(register_ip_vs_app);
37 EXPORT_SYMBOL(unregister_ip_vs_app);
38 EXPORT_SYMBOL(register_ip_vs_app_inc);
40 /* ipvs application list head */
41 static LIST_HEAD(ip_vs_app_list);
42 static DECLARE_MUTEX(__ip_vs_app_mutex);
46 * Get an ip_vs_app object
48 static inline int ip_vs_app_get(struct ip_vs_app *app)
50 /* test and get the module atomically */
51 if (app->module)
52 return try_module_get(app->module);
53 else
54 return 1;
58 static inline void ip_vs_app_put(struct ip_vs_app *app)
60 if (app->module)
61 module_put(app->module);
66 * Allocate/initialize app incarnation and register it in proto apps.
68 static int
69 ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
71 struct ip_vs_protocol *pp;
72 struct ip_vs_app *inc;
73 int ret;
75 if (!(pp = ip_vs_proto_get(proto)))
76 return -EPROTONOSUPPORT;
78 if (!pp->unregister_app)
79 return -EOPNOTSUPP;
81 inc = kmalloc(sizeof(struct ip_vs_app), GFP_KERNEL);
82 if (!inc)
83 return -ENOMEM;
84 memcpy(inc, app, sizeof(*inc));
85 INIT_LIST_HEAD(&inc->p_list);
86 INIT_LIST_HEAD(&inc->incs_list);
87 inc->app = app;
88 inc->port = htons(port);
89 atomic_set(&inc->usecnt, 0);
91 if (app->timeouts) {
92 inc->timeout_table =
93 ip_vs_create_timeout_table(app->timeouts,
94 app->timeouts_size);
95 if (!inc->timeout_table) {
96 ret = -ENOMEM;
97 goto out;
101 ret = pp->register_app(inc);
102 if (ret)
103 goto out;
105 list_add(&inc->a_list, &app->incs_list);
106 IP_VS_DBG(9, "%s application %s:%u registered\n",
107 pp->name, inc->name, inc->port);
109 return 0;
111 out:
112 if (inc->timeout_table)
113 kfree(inc->timeout_table);
114 kfree(inc);
115 return ret;
120 * Release app incarnation
122 static void
123 ip_vs_app_inc_release(struct ip_vs_app *inc)
125 struct ip_vs_protocol *pp;
127 if (!(pp = ip_vs_proto_get(inc->protocol)))
128 return;
130 if (pp->unregister_app)
131 pp->unregister_app(inc);
133 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
134 pp->name, inc->name, inc->port);
136 list_del(&inc->a_list);
138 if (inc->timeout_table != NULL)
139 kfree(inc->timeout_table);
140 kfree(inc);
145 * Get reference to app inc (only called from softirq)
148 int ip_vs_app_inc_get(struct ip_vs_app *inc)
150 int result;
152 atomic_inc(&inc->usecnt);
153 if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
154 atomic_dec(&inc->usecnt);
155 return result;
160 * Put the app inc (only called from timer or net softirq)
162 void ip_vs_app_inc_put(struct ip_vs_app *inc)
164 ip_vs_app_put(inc->app);
165 atomic_dec(&inc->usecnt);
170 * Register an application incarnation in protocol applications
173 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
175 int result;
177 down(&__ip_vs_app_mutex);
179 result = ip_vs_app_inc_new(app, proto, port);
181 up(&__ip_vs_app_mutex);
183 return result;
188 * ip_vs_app registration routine
190 int register_ip_vs_app(struct ip_vs_app *app)
192 /* increase the module use count */
193 ip_vs_use_count_inc();
195 down(&__ip_vs_app_mutex);
197 list_add(&app->a_list, &ip_vs_app_list);
199 up(&__ip_vs_app_mutex);
201 return 0;
206 * ip_vs_app unregistration routine
207 * We are sure there are no app incarnations attached to services
209 void unregister_ip_vs_app(struct ip_vs_app *app)
211 struct ip_vs_app *inc, *nxt;
213 down(&__ip_vs_app_mutex);
215 list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
216 ip_vs_app_inc_release(inc);
219 list_del(&app->a_list);
221 up(&__ip_vs_app_mutex);
223 /* decrease the module use count */
224 ip_vs_use_count_dec();
228 #if 0000
230 * Get reference to app by name (called from user context)
232 struct ip_vs_app *ip_vs_app_get_by_name(char *appname)
234 struct ip_vs_app *app, *a = NULL;
236 down(&__ip_vs_app_mutex);
238 list_for_each_entry(ent, &ip_vs_app_list, a_list) {
239 if (strcmp(app->name, appname))
240 continue;
242 /* softirq may call ip_vs_app_get too, so the caller
243 must disable softirq on the current CPU */
244 if (ip_vs_app_get(app))
245 a = app;
246 break;
249 up(&__ip_vs_app_mutex);
251 return a;
253 #endif
257 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
259 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
261 return pp->app_conn_bind(cp);
266 * Unbind cp from application incarnation (called by cp destructor)
268 void ip_vs_unbind_app(struct ip_vs_conn *cp)
270 struct ip_vs_app *inc = cp->app;
272 if (!inc)
273 return;
275 if (inc->unbind_conn)
276 inc->unbind_conn(inc, cp);
277 if (inc->done_conn)
278 inc->done_conn(inc, cp);
279 ip_vs_app_inc_put(inc);
280 cp->app = NULL;
285 * Fixes th->seq based on ip_vs_seq info.
287 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
289 __u32 seq = ntohl(th->seq);
292 * Adjust seq with delta-offset for all packets after
293 * the most recent resized pkt seq and with previous_delta offset
294 * for all packets before most recent resized pkt seq.
296 if (vseq->delta || vseq->previous_delta) {
297 if(after(seq, vseq->init_seq)) {
298 th->seq = htonl(seq + vseq->delta);
299 IP_VS_DBG(9, "vs_fix_seq(): added delta (%d) to seq\n",
300 vseq->delta);
301 } else {
302 th->seq = htonl(seq + vseq->previous_delta);
303 IP_VS_DBG(9, "vs_fix_seq(): added previous_delta "
304 "(%d) to seq\n", vseq->previous_delta);
311 * Fixes th->ack_seq based on ip_vs_seq info.
313 static inline void
314 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
316 __u32 ack_seq = ntohl(th->ack_seq);
319 * Adjust ack_seq with delta-offset for
320 * the packets AFTER most recent resized pkt has caused a shift
321 * for packets before most recent resized pkt, use previous_delta
323 if (vseq->delta || vseq->previous_delta) {
324 /* since ack_seq is the number of octet that is expected
325 to receive next, so compare it with init_seq+delta */
326 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
327 th->ack_seq = htonl(ack_seq - vseq->delta);
328 IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted delta "
329 "(%d) from ack_seq\n", vseq->delta);
331 } else {
332 th->ack_seq = htonl(ack_seq - vseq->previous_delta);
333 IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted "
334 "previous_delta (%d) from ack_seq\n",
335 vseq->previous_delta);
342 * Updates ip_vs_seq if pkt has been resized
343 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
345 static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
346 unsigned flag, __u32 seq, int diff)
348 /* spinlock is to keep updating cp->flags atomic */
349 spin_lock(&cp->lock);
350 if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
351 vseq->previous_delta = vseq->delta;
352 vseq->delta += diff;
353 vseq->init_seq = seq;
354 cp->flags |= flag;
356 spin_unlock(&cp->lock);
359 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff **pskb,
360 struct ip_vs_app *app)
362 int diff;
363 unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
364 struct tcphdr *th;
365 __u32 seq;
367 if (!ip_vs_make_skb_writable(pskb, tcp_offset + sizeof(*th)))
368 return 0;
370 th = (struct tcphdr *)((*pskb)->nh.raw + tcp_offset);
373 * Remember seq number in case this pkt gets resized
375 seq = ntohl(th->seq);
378 * Fix seq stuff if flagged as so.
380 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
381 vs_fix_seq(&cp->out_seq, th);
382 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
383 vs_fix_ack_seq(&cp->in_seq, th);
386 * Call private output hook function
388 if (app->pkt_out == NULL)
389 return 1;
391 if (!app->pkt_out(app, cp, pskb, &diff))
392 return 0;
395 * Update ip_vs seq stuff if len has changed.
397 if (diff != 0)
398 vs_seq_update(cp, &cp->out_seq,
399 IP_VS_CONN_F_OUT_SEQ, seq, diff);
401 return 1;
405 * Output pkt hook. Will call bound ip_vs_app specific function
406 * called by ipvs packet handler, assumes previously checked cp!=NULL
407 * returns false if it can't handle packet (oom)
409 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff **pskb)
411 struct ip_vs_app *app;
414 * check if application module is bound to
415 * this ip_vs_conn.
417 if ((app = cp->app) == NULL)
418 return 1;
420 /* TCP is complicated */
421 if (cp->protocol == IPPROTO_TCP)
422 return app_tcp_pkt_out(cp, pskb, app);
425 * Call private output hook function
427 if (app->pkt_out == NULL)
428 return 1;
430 return app->pkt_out(app, cp, pskb, NULL);
434 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff **pskb,
435 struct ip_vs_app *app)
437 int diff;
438 unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
439 struct tcphdr *th;
440 __u32 seq;
442 if (!ip_vs_make_skb_writable(pskb, tcp_offset + sizeof(*th)))
443 return 0;
445 th = (struct tcphdr *)((*pskb)->nh.raw + tcp_offset);
448 * Remember seq number in case this pkt gets resized
450 seq = ntohl(th->seq);
453 * Fix seq stuff if flagged as so.
455 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
456 vs_fix_seq(&cp->in_seq, th);
457 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
458 vs_fix_ack_seq(&cp->out_seq, th);
461 * Call private input hook function
463 if (app->pkt_in == NULL)
464 return 1;
466 if (!app->pkt_in(app, cp, pskb, &diff))
467 return 0;
470 * Update ip_vs seq stuff if len has changed.
472 if (diff != 0)
473 vs_seq_update(cp, &cp->in_seq,
474 IP_VS_CONN_F_IN_SEQ, seq, diff);
476 return 1;
480 * Input pkt hook. Will call bound ip_vs_app specific function
481 * called by ipvs packet handler, assumes previously checked cp!=NULL.
482 * returns false if can't handle packet (oom).
484 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff **pskb)
486 struct ip_vs_app *app;
489 * check if application module is bound to
490 * this ip_vs_conn.
492 if ((app = cp->app) == NULL)
493 return 1;
495 /* TCP is complicated */
496 if (cp->protocol == IPPROTO_TCP)
497 return app_tcp_pkt_in(cp, pskb, app);
500 * Call private input hook function
502 if (app->pkt_in == NULL)
503 return 1;
505 return app->pkt_in(app, cp, pskb, NULL);
509 #ifdef CONFIG_PROC_FS
511 * /proc/net/ip_vs_app entry function
514 static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
516 struct ip_vs_app *app, *inc;
518 list_for_each_entry(app, &ip_vs_app_list, a_list) {
519 list_for_each_entry(inc, &app->incs_list, a_list) {
520 if (pos-- == 0)
521 return inc;
524 return NULL;
528 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
530 down(&__ip_vs_app_mutex);
532 return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
535 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
537 struct ip_vs_app *inc, *app;
538 struct list_head *e;
540 ++*pos;
541 if (v == SEQ_START_TOKEN)
542 return ip_vs_app_idx(0);
544 inc = v;
545 app = inc->app;
547 if ((e = inc->a_list.next) != &app->incs_list)
548 return list_entry(e, struct ip_vs_app, a_list);
550 /* go on to next application */
551 for (e = app->a_list.next; e != &ip_vs_app_list; e = e->next) {
552 app = list_entry(e, struct ip_vs_app, a_list);
553 list_for_each_entry(inc, &app->incs_list, a_list) {
554 return inc;
557 return NULL;
560 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
562 up(&__ip_vs_app_mutex);
565 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
567 if (v == SEQ_START_TOKEN)
568 seq_puts(seq, "prot port usecnt name\n");
569 else {
570 const struct ip_vs_app *inc = v;
572 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
573 ip_vs_proto_name(inc->protocol),
574 ntohs(inc->port),
575 atomic_read(&inc->usecnt),
576 inc->name);
578 return 0;
581 static struct seq_operations ip_vs_app_seq_ops = {
582 .start = ip_vs_app_seq_start,
583 .next = ip_vs_app_seq_next,
584 .stop = ip_vs_app_seq_stop,
585 .show = ip_vs_app_seq_show,
588 static int ip_vs_app_open(struct inode *inode, struct file *file)
590 return seq_open(file, &ip_vs_app_seq_ops);
593 static struct file_operations ip_vs_app_fops = {
594 .owner = THIS_MODULE,
595 .open = ip_vs_app_open,
596 .read = seq_read,
597 .llseek = seq_lseek,
598 .release = seq_release,
600 #endif
604 * Replace a segment of data with a new segment
606 int ip_vs_skb_replace(struct sk_buff *skb, int pri,
607 char *o_buf, int o_len, char *n_buf, int n_len)
609 struct iphdr *iph;
610 int diff;
611 int o_offset;
612 int o_left;
614 EnterFunction(9);
616 diff = n_len - o_len;
617 o_offset = o_buf - (char *)skb->data;
618 /* The length of left data after o_buf+o_len in the skb data */
619 o_left = skb->len - (o_offset + o_len);
621 if (diff <= 0) {
622 memmove(o_buf + n_len, o_buf + o_len, o_left);
623 memcpy(o_buf, n_buf, n_len);
624 skb_trim(skb, skb->len + diff);
625 } else if (diff <= skb_tailroom(skb)) {
626 skb_put(skb, diff);
627 memmove(o_buf + n_len, o_buf + o_len, o_left);
628 memcpy(o_buf, n_buf, n_len);
629 } else {
630 if (pskb_expand_head(skb, skb_headroom(skb), diff, pri))
631 return -ENOMEM;
632 skb_put(skb, diff);
633 memmove(skb->data + o_offset + n_len,
634 skb->data + o_offset + o_len, o_left);
635 memcpy(skb->data + o_offset, n_buf, n_len);
638 /* must update the iph total length here */
639 iph = skb->nh.iph;
640 iph->tot_len = htons(skb->len);
642 LeaveFunction(9);
643 return 0;
647 int ip_vs_app_init(void)
649 /* we will replace it with proc_net_ipvs_create() soon */
650 proc_net_fops_create("ip_vs_app", 0, &ip_vs_app_fops);
651 return 0;
655 void ip_vs_app_cleanup(void)
657 proc_net_remove("ip_vs_app");