blk_end_request: changing viocd (take 4)
[linux-2.6/mini2440.git] / net / ipv4 / ipvs / ip_vs_app.c
blob664cb8e97c1c515202d8bd6f05cca73bd9e789b1
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 <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>
38 #include <net/ip_vs.h>
40 EXPORT_SYMBOL(register_ip_vs_app);
41 EXPORT_SYMBOL(unregister_ip_vs_app);
42 EXPORT_SYMBOL(register_ip_vs_app_inc);
44 /* ipvs application list head */
45 static LIST_HEAD(ip_vs_app_list);
46 static DEFINE_MUTEX(__ip_vs_app_mutex);
50 * Get an ip_vs_app object
52 static inline int ip_vs_app_get(struct ip_vs_app *app)
54 /* test and get the module atomically */
55 if (app->module)
56 return try_module_get(app->module);
57 else
58 return 1;
62 static inline void ip_vs_app_put(struct ip_vs_app *app)
64 if (app->module)
65 module_put(app->module);
70 * Allocate/initialize app incarnation and register it in proto apps.
72 static int
73 ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
75 struct ip_vs_protocol *pp;
76 struct ip_vs_app *inc;
77 int ret;
79 if (!(pp = ip_vs_proto_get(proto)))
80 return -EPROTONOSUPPORT;
82 if (!pp->unregister_app)
83 return -EOPNOTSUPP;
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);
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;
104 ret = pp->register_app(inc);
105 if (ret)
106 goto out;
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);
112 return 0;
114 out:
115 kfree(inc->timeout_table);
116 kfree(inc);
117 return ret;
122 * Release app incarnation
124 static void
125 ip_vs_app_inc_release(struct ip_vs_app *inc)
127 struct ip_vs_protocol *pp;
129 if (!(pp = ip_vs_proto_get(inc->protocol)))
130 return;
132 if (pp->unregister_app)
133 pp->unregister_app(inc);
135 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
136 pp->name, inc->name, inc->port);
138 list_del(&inc->a_list);
140 kfree(inc->timeout_table);
141 kfree(inc);
146 * Get reference to app inc (only called from softirq)
149 int ip_vs_app_inc_get(struct ip_vs_app *inc)
151 int result;
153 atomic_inc(&inc->usecnt);
154 if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
155 atomic_dec(&inc->usecnt);
156 return result;
161 * Put the app inc (only called from timer or net softirq)
163 void ip_vs_app_inc_put(struct ip_vs_app *inc)
165 ip_vs_app_put(inc->app);
166 atomic_dec(&inc->usecnt);
171 * Register an application incarnation in protocol applications
174 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
176 int result;
178 mutex_lock(&__ip_vs_app_mutex);
180 result = ip_vs_app_inc_new(app, proto, port);
182 mutex_unlock(&__ip_vs_app_mutex);
184 return result;
189 * ip_vs_app registration routine
191 int register_ip_vs_app(struct ip_vs_app *app)
193 /* increase the module use count */
194 ip_vs_use_count_inc();
196 mutex_lock(&__ip_vs_app_mutex);
198 list_add(&app->a_list, &ip_vs_app_list);
200 mutex_unlock(&__ip_vs_app_mutex);
202 return 0;
207 * ip_vs_app unregistration routine
208 * We are sure there are no app incarnations attached to services
210 void unregister_ip_vs_app(struct ip_vs_app *app)
212 struct ip_vs_app *inc, *nxt;
214 mutex_lock(&__ip_vs_app_mutex);
216 list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
217 ip_vs_app_inc_release(inc);
220 list_del(&app->a_list);
222 mutex_unlock(&__ip_vs_app_mutex);
224 /* decrease the module use count */
225 ip_vs_use_count_dec();
230 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
232 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
234 return pp->app_conn_bind(cp);
239 * Unbind cp from application incarnation (called by cp destructor)
241 void ip_vs_unbind_app(struct ip_vs_conn *cp)
243 struct ip_vs_app *inc = cp->app;
245 if (!inc)
246 return;
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;
258 * Fixes th->seq based on ip_vs_seq info.
260 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
262 __u32 seq = ntohl(th->seq);
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.
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);
284 * Fixes th->ack_seq based on ip_vs_seq info.
286 static inline void
287 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
289 __u32 ack_seq = ntohl(th->ack_seq);
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
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);
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);
315 * Updates ip_vs_seq if pkt has been resized
316 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
318 static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
319 unsigned flag, __u32 seq, int diff)
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;
329 spin_unlock(&cp->lock);
332 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
333 struct ip_vs_app *app)
335 int diff;
336 const unsigned int tcp_offset = ip_hdrlen(skb);
337 struct tcphdr *th;
338 __u32 seq;
340 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
341 return 0;
343 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
346 * Remember seq number in case this pkt gets resized
348 seq = ntohl(th->seq);
351 * Fix seq stuff if flagged as so.
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);
359 * Call private output hook function
361 if (app->pkt_out == NULL)
362 return 1;
364 if (!app->pkt_out(app, cp, skb, &diff))
365 return 0;
368 * Update ip_vs seq stuff if len has changed.
370 if (diff != 0)
371 vs_seq_update(cp, &cp->out_seq,
372 IP_VS_CONN_F_OUT_SEQ, seq, diff);
374 return 1;
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)
382 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
384 struct ip_vs_app *app;
387 * check if application module is bound to
388 * this ip_vs_conn.
390 if ((app = cp->app) == NULL)
391 return 1;
393 /* TCP is complicated */
394 if (cp->protocol == IPPROTO_TCP)
395 return app_tcp_pkt_out(cp, skb, app);
398 * Call private output hook function
400 if (app->pkt_out == NULL)
401 return 1;
403 return app->pkt_out(app, cp, skb, NULL);
407 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
408 struct ip_vs_app *app)
410 int diff;
411 const unsigned int tcp_offset = ip_hdrlen(skb);
412 struct tcphdr *th;
413 __u32 seq;
415 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
416 return 0;
418 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
421 * Remember seq number in case this pkt gets resized
423 seq = ntohl(th->seq);
426 * Fix seq stuff if flagged as so.
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);
434 * Call private input hook function
436 if (app->pkt_in == NULL)
437 return 1;
439 if (!app->pkt_in(app, cp, skb, &diff))
440 return 0;
443 * Update ip_vs seq stuff if len has changed.
445 if (diff != 0)
446 vs_seq_update(cp, &cp->in_seq,
447 IP_VS_CONN_F_IN_SEQ, seq, diff);
449 return 1;
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).
457 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
459 struct ip_vs_app *app;
462 * check if application module is bound to
463 * this ip_vs_conn.
465 if ((app = cp->app) == NULL)
466 return 1;
468 /* TCP is complicated */
469 if (cp->protocol == IPPROTO_TCP)
470 return app_tcp_pkt_in(cp, skb, app);
473 * Call private input hook function
475 if (app->pkt_in == NULL)
476 return 1;
478 return app->pkt_in(app, cp, skb, NULL);
482 #ifdef CONFIG_PROC_FS
484 * /proc/net/ip_vs_app entry function
487 static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
489 struct ip_vs_app *app, *inc;
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;
497 return NULL;
501 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
503 mutex_lock(&__ip_vs_app_mutex);
505 return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
508 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
510 struct ip_vs_app *inc, *app;
511 struct list_head *e;
513 ++*pos;
514 if (v == SEQ_START_TOKEN)
515 return ip_vs_app_idx(0);
517 inc = v;
518 app = inc->app;
520 if ((e = inc->a_list.next) != &app->incs_list)
521 return list_entry(e, struct ip_vs_app, a_list);
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;
530 return NULL;
533 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
535 mutex_unlock(&__ip_vs_app_mutex);
538 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
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;
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);
551 return 0;
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,
561 static int ip_vs_app_open(struct inode *inode, struct file *file)
563 return seq_open(file, &ip_vs_app_seq_ops);
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,
573 #endif
577 * Replace a segment of data with a new segment
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)
582 int diff;
583 int o_offset;
584 int o_left;
586 EnterFunction(9);
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);
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);
610 /* must update the iph total length here */
611 ip_hdr(skb)->tot_len = htons(skb->len);
613 LeaveFunction(9);
614 return 0;
618 int ip_vs_app_init(void)
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;
626 void ip_vs_app_cleanup(void)
628 proc_net_remove(&init_net, "ip_vs_app");