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>
28 #include <net/protocol.h>
30 #include <asm/system.h>
31 #include <linux/stat.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
35 #include <net/ip_vs.h>
37 EXPORT_SYMBOL(register_ip_vs_app);
38 EXPORT_SYMBOL(unregister_ip_vs_app);
39 EXPORT_SYMBOL(register_ip_vs_app_inc);
41 /* ipvs application list head */
42 static LIST_HEAD(ip_vs_app_list);
43 static DECLARE_MUTEX(__ip_vs_app_mutex);
47 * Get an ip_vs_app object
49 static inline int ip_vs_app_get(struct ip_vs_app *app)
51 /* test and get the module atomically */
53 return try_module_get(app->module);
59 static inline void ip_vs_app_put(struct ip_vs_app *app)
62 module_put(app->module);
67 * Allocate/initialize app incarnation and register it in proto apps.
70 ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
72 struct ip_vs_protocol *pp;
73 struct ip_vs_app *inc;
76 if (!(pp = ip_vs_proto_get(proto)))
77 return -EPROTONOSUPPORT;
79 if (!pp->unregister_app)
82 inc = kmalloc(sizeof(struct ip_vs_app), GFP_KERNEL);
85 memcpy(inc, app, sizeof(*inc));
86 INIT_LIST_HEAD(&inc->p_list);
87 INIT_LIST_HEAD(&inc->incs_list);
89 inc->port = htons(port);
90 atomic_set(&inc->usecnt, 0);
94 ip_vs_create_timeout_table(app->timeouts,
96 if (!inc->timeout_table) {
102 ret = pp->register_app(inc);
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);
113 kfree(inc->timeout_table);
120 * Release app incarnation
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)))
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 kfree(inc->timeout_table);
144 * Get reference to app inc (only called from softirq)
147 int ip_vs_app_inc_get(struct ip_vs_app *inc)
151 atomic_inc(&inc->usecnt);
152 if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
153 atomic_dec(&inc->usecnt);
159 * Put the app inc (only called from timer or net softirq)
161 void ip_vs_app_inc_put(struct ip_vs_app *inc)
163 ip_vs_app_put(inc->app);
164 atomic_dec(&inc->usecnt);
169 * Register an application incarnation in protocol applications
172 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
176 down(&__ip_vs_app_mutex);
178 result = ip_vs_app_inc_new(app, proto, port);
180 up(&__ip_vs_app_mutex);
187 * ip_vs_app registration routine
189 int register_ip_vs_app(struct ip_vs_app *app)
191 /* increase the module use count */
192 ip_vs_use_count_inc();
194 down(&__ip_vs_app_mutex);
196 list_add(&app->a_list, &ip_vs_app_list);
198 up(&__ip_vs_app_mutex);
205 * ip_vs_app unregistration routine
206 * We are sure there are no app incarnations attached to services
208 void unregister_ip_vs_app(struct ip_vs_app *app)
210 struct ip_vs_app *inc, *nxt;
212 down(&__ip_vs_app_mutex);
214 list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
215 ip_vs_app_inc_release(inc);
218 list_del(&app->a_list);
220 up(&__ip_vs_app_mutex);
222 /* decrease the module use count */
223 ip_vs_use_count_dec();
229 * Get reference to app by name (called from user context)
231 struct ip_vs_app *ip_vs_app_get_by_name(char *appname)
233 struct ip_vs_app *app, *a = NULL;
235 down(&__ip_vs_app_mutex);
237 list_for_each_entry(ent, &ip_vs_app_list, a_list) {
238 if (strcmp(app->name, appname))
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))
248 up(&__ip_vs_app_mutex);
256 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
258 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
260 return pp->app_conn_bind(cp);
265 * Unbind cp from application incarnation (called by cp destructor)
267 void ip_vs_unbind_app(struct ip_vs_conn *cp)
269 struct ip_vs_app *inc = cp->app;
274 if (inc->unbind_conn)
275 inc->unbind_conn(inc, cp);
277 inc->done_conn(inc, cp);
278 ip_vs_app_inc_put(inc);
284 * Fixes th->seq based on ip_vs_seq info.
286 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
288 __u32 seq = ntohl(th->seq);
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.
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",
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);
310 * Fixes th->ack_seq based on ip_vs_seq info.
313 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
315 __u32 ack_seq = ntohl(th->ack_seq);
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
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);
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);
341 * Updates ip_vs_seq if pkt has been resized
342 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
344 static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
345 unsigned flag, __u32 seq, int diff)
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;
352 vseq->init_seq = seq;
355 spin_unlock(&cp->lock);
358 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff **pskb,
359 struct ip_vs_app *app)
362 unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
366 if (!ip_vs_make_skb_writable(pskb, tcp_offset + sizeof(*th)))
369 th = (struct tcphdr *)((*pskb)->nh.raw + tcp_offset);
372 * Remember seq number in case this pkt gets resized
374 seq = ntohl(th->seq);
377 * Fix seq stuff if flagged as so.
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);
385 * Call private output hook function
387 if (app->pkt_out == NULL)
390 if (!app->pkt_out(app, cp, pskb, &diff))
394 * Update ip_vs seq stuff if len has changed.
397 vs_seq_update(cp, &cp->out_seq,
398 IP_VS_CONN_F_OUT_SEQ, seq, diff);
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)
408 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff **pskb)
410 struct ip_vs_app *app;
413 * check if application module is bound to
416 if ((app = cp->app) == NULL)
419 /* TCP is complicated */
420 if (cp->protocol == IPPROTO_TCP)
421 return app_tcp_pkt_out(cp, pskb, app);
424 * Call private output hook function
426 if (app->pkt_out == NULL)
429 return app->pkt_out(app, cp, pskb, NULL);
433 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff **pskb,
434 struct ip_vs_app *app)
437 unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
441 if (!ip_vs_make_skb_writable(pskb, tcp_offset + sizeof(*th)))
444 th = (struct tcphdr *)((*pskb)->nh.raw + tcp_offset);
447 * Remember seq number in case this pkt gets resized
449 seq = ntohl(th->seq);
452 * Fix seq stuff if flagged as so.
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);
460 * Call private input hook function
462 if (app->pkt_in == NULL)
465 if (!app->pkt_in(app, cp, pskb, &diff))
469 * Update ip_vs seq stuff if len has changed.
472 vs_seq_update(cp, &cp->in_seq,
473 IP_VS_CONN_F_IN_SEQ, seq, diff);
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).
483 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff **pskb)
485 struct ip_vs_app *app;
488 * check if application module is bound to
491 if ((app = cp->app) == NULL)
494 /* TCP is complicated */
495 if (cp->protocol == IPPROTO_TCP)
496 return app_tcp_pkt_in(cp, pskb, app);
499 * Call private input hook function
501 if (app->pkt_in == NULL)
504 return app->pkt_in(app, cp, pskb, NULL);
508 #ifdef CONFIG_PROC_FS
510 * /proc/net/ip_vs_app entry function
513 static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
515 struct ip_vs_app *app, *inc;
517 list_for_each_entry(app, &ip_vs_app_list, a_list) {
518 list_for_each_entry(inc, &app->incs_list, a_list) {
527 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
529 down(&__ip_vs_app_mutex);
531 return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
534 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
536 struct ip_vs_app *inc, *app;
540 if (v == SEQ_START_TOKEN)
541 return ip_vs_app_idx(0);
546 if ((e = inc->a_list.next) != &app->incs_list)
547 return list_entry(e, struct ip_vs_app, a_list);
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) {
559 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
561 up(&__ip_vs_app_mutex);
564 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
566 if (v == SEQ_START_TOKEN)
567 seq_puts(seq, "prot port usecnt name\n");
569 const struct ip_vs_app *inc = v;
571 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
572 ip_vs_proto_name(inc->protocol),
574 atomic_read(&inc->usecnt),
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,
587 static int ip_vs_app_open(struct inode *inode, struct file *file)
589 return seq_open(file, &ip_vs_app_seq_ops);
592 static struct file_operations ip_vs_app_fops = {
593 .owner = THIS_MODULE,
594 .open = ip_vs_app_open,
597 .release = seq_release,
603 * Replace a segment of data with a new segment
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)
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);
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)) {
626 memmove(o_buf + n_len, o_buf + o_len, o_left);
627 memcpy(o_buf, n_buf, n_len);
629 if (pskb_expand_head(skb, skb_headroom(skb), diff, pri))
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);
637 /* must update the iph total length here */
639 iph->tot_len = htons(skb->len);
646 int ip_vs_app_init(void)
648 /* we will replace it with proc_net_ipvs_create() soon */
649 proc_net_fops_create("ip_vs_app", 0, &ip_vs_app_fops);
654 void ip_vs_app_cleanup(void)
656 proc_net_remove("ip_vs_app");