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 if (inc
->timeout_table
)
114 kfree(inc
->timeout_table
);
121 * Release app incarnation
124 ip_vs_app_inc_release(struct ip_vs_app
*inc
)
126 struct ip_vs_protocol
*pp
;
128 if (!(pp
= ip_vs_proto_get(inc
->protocol
)))
131 if (pp
->unregister_app
)
132 pp
->unregister_app(inc
);
134 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
135 pp
->name
, inc
->name
, inc
->port
);
137 list_del(&inc
->a_list
);
139 if (inc
->timeout_table
!= NULL
)
140 kfree(inc
->timeout_table
);
146 * Get reference to app inc (only called from softirq)
149 int ip_vs_app_inc_get(struct ip_vs_app
*inc
)
153 atomic_inc(&inc
->usecnt
);
154 if (unlikely((result
= ip_vs_app_get(inc
->app
)) != 1))
155 atomic_dec(&inc
->usecnt
);
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
)
178 down(&__ip_vs_app_mutex
);
180 result
= ip_vs_app_inc_new(app
, proto
, port
);
182 up(&__ip_vs_app_mutex
);
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 down(&__ip_vs_app_mutex
);
198 list_add(&app
->a_list
, &ip_vs_app_list
);
200 up(&__ip_vs_app_mutex
);
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 down(&__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 up(&__ip_vs_app_mutex
);
224 /* decrease the module use count */
225 ip_vs_use_count_dec();
231 * Get reference to app by name (called from user context)
233 struct ip_vs_app
*ip_vs_app_get_by_name(char *appname
)
235 struct ip_vs_app
*app
, *a
= NULL
;
237 down(&__ip_vs_app_mutex
);
239 list_for_each_entry(ent
, &ip_vs_app_list
, a_list
) {
240 if (strcmp(app
->name
, appname
))
243 /* softirq may call ip_vs_app_get too, so the caller
244 must disable softirq on the current CPU */
245 if (ip_vs_app_get(app
))
250 up(&__ip_vs_app_mutex
);
258 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
260 int ip_vs_bind_app(struct ip_vs_conn
*cp
, struct ip_vs_protocol
*pp
)
262 return pp
->app_conn_bind(cp
);
267 * Unbind cp from application incarnation (called by cp destructor)
269 void ip_vs_unbind_app(struct ip_vs_conn
*cp
)
271 struct ip_vs_app
*inc
= cp
->app
;
276 if (inc
->unbind_conn
)
277 inc
->unbind_conn(inc
, cp
);
279 inc
->done_conn(inc
, cp
);
280 ip_vs_app_inc_put(inc
);
286 * Fixes th->seq based on ip_vs_seq info.
288 static inline void vs_fix_seq(const struct ip_vs_seq
*vseq
, struct tcphdr
*th
)
290 __u32 seq
= ntohl(th
->seq
);
293 * Adjust seq with delta-offset for all packets after
294 * the most recent resized pkt seq and with previous_delta offset
295 * for all packets before most recent resized pkt seq.
297 if (vseq
->delta
|| vseq
->previous_delta
) {
298 if(after(seq
, vseq
->init_seq
)) {
299 th
->seq
= htonl(seq
+ vseq
->delta
);
300 IP_VS_DBG(9, "vs_fix_seq(): added delta (%d) to seq\n",
303 th
->seq
= htonl(seq
+ vseq
->previous_delta
);
304 IP_VS_DBG(9, "vs_fix_seq(): added previous_delta "
305 "(%d) to seq\n", vseq
->previous_delta
);
312 * Fixes th->ack_seq based on ip_vs_seq info.
315 vs_fix_ack_seq(const struct ip_vs_seq
*vseq
, struct tcphdr
*th
)
317 __u32 ack_seq
= ntohl(th
->ack_seq
);
320 * Adjust ack_seq with delta-offset for
321 * the packets AFTER most recent resized pkt has caused a shift
322 * for packets before most recent resized pkt, use previous_delta
324 if (vseq
->delta
|| vseq
->previous_delta
) {
325 /* since ack_seq is the number of octet that is expected
326 to receive next, so compare it with init_seq+delta */
327 if(after(ack_seq
, vseq
->init_seq
+vseq
->delta
)) {
328 th
->ack_seq
= htonl(ack_seq
- vseq
->delta
);
329 IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted delta "
330 "(%d) from ack_seq\n", vseq
->delta
);
333 th
->ack_seq
= htonl(ack_seq
- vseq
->previous_delta
);
334 IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted "
335 "previous_delta (%d) from ack_seq\n",
336 vseq
->previous_delta
);
343 * Updates ip_vs_seq if pkt has been resized
344 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
346 static inline void vs_seq_update(struct ip_vs_conn
*cp
, struct ip_vs_seq
*vseq
,
347 unsigned flag
, __u32 seq
, int diff
)
349 /* spinlock is to keep updating cp->flags atomic */
350 spin_lock(&cp
->lock
);
351 if (!(cp
->flags
& flag
) || after(seq
, vseq
->init_seq
)) {
352 vseq
->previous_delta
= vseq
->delta
;
354 vseq
->init_seq
= seq
;
357 spin_unlock(&cp
->lock
);
360 static inline int app_tcp_pkt_out(struct ip_vs_conn
*cp
, struct sk_buff
**pskb
,
361 struct ip_vs_app
*app
)
364 unsigned int tcp_offset
= (*pskb
)->nh
.iph
->ihl
*4;
368 if (!ip_vs_make_skb_writable(pskb
, tcp_offset
+ sizeof(*th
)))
371 th
= (struct tcphdr
*)((*pskb
)->nh
.raw
+ tcp_offset
);
374 * Remember seq number in case this pkt gets resized
376 seq
= ntohl(th
->seq
);
379 * Fix seq stuff if flagged as so.
381 if (cp
->flags
& IP_VS_CONN_F_OUT_SEQ
)
382 vs_fix_seq(&cp
->out_seq
, th
);
383 if (cp
->flags
& IP_VS_CONN_F_IN_SEQ
)
384 vs_fix_ack_seq(&cp
->in_seq
, th
);
387 * Call private output hook function
389 if (app
->pkt_out
== NULL
)
392 if (!app
->pkt_out(app
, cp
, pskb
, &diff
))
396 * Update ip_vs seq stuff if len has changed.
399 vs_seq_update(cp
, &cp
->out_seq
,
400 IP_VS_CONN_F_OUT_SEQ
, seq
, diff
);
406 * Output pkt hook. Will call bound ip_vs_app specific function
407 * called by ipvs packet handler, assumes previously checked cp!=NULL
408 * returns false if it can't handle packet (oom)
410 int ip_vs_app_pkt_out(struct ip_vs_conn
*cp
, struct sk_buff
**pskb
)
412 struct ip_vs_app
*app
;
415 * check if application module is bound to
418 if ((app
= cp
->app
) == NULL
)
421 /* TCP is complicated */
422 if (cp
->protocol
== IPPROTO_TCP
)
423 return app_tcp_pkt_out(cp
, pskb
, app
);
426 * Call private output hook function
428 if (app
->pkt_out
== NULL
)
431 return app
->pkt_out(app
, cp
, pskb
, NULL
);
435 static inline int app_tcp_pkt_in(struct ip_vs_conn
*cp
, struct sk_buff
**pskb
,
436 struct ip_vs_app
*app
)
439 unsigned int tcp_offset
= (*pskb
)->nh
.iph
->ihl
*4;
443 if (!ip_vs_make_skb_writable(pskb
, tcp_offset
+ sizeof(*th
)))
446 th
= (struct tcphdr
*)((*pskb
)->nh
.raw
+ tcp_offset
);
449 * Remember seq number in case this pkt gets resized
451 seq
= ntohl(th
->seq
);
454 * Fix seq stuff if flagged as so.
456 if (cp
->flags
& IP_VS_CONN_F_IN_SEQ
)
457 vs_fix_seq(&cp
->in_seq
, th
);
458 if (cp
->flags
& IP_VS_CONN_F_OUT_SEQ
)
459 vs_fix_ack_seq(&cp
->out_seq
, th
);
462 * Call private input hook function
464 if (app
->pkt_in
== NULL
)
467 if (!app
->pkt_in(app
, cp
, pskb
, &diff
))
471 * Update ip_vs seq stuff if len has changed.
474 vs_seq_update(cp
, &cp
->in_seq
,
475 IP_VS_CONN_F_IN_SEQ
, seq
, diff
);
481 * Input pkt hook. Will call bound ip_vs_app specific function
482 * called by ipvs packet handler, assumes previously checked cp!=NULL.
483 * returns false if can't handle packet (oom).
485 int ip_vs_app_pkt_in(struct ip_vs_conn
*cp
, struct sk_buff
**pskb
)
487 struct ip_vs_app
*app
;
490 * check if application module is bound to
493 if ((app
= cp
->app
) == NULL
)
496 /* TCP is complicated */
497 if (cp
->protocol
== IPPROTO_TCP
)
498 return app_tcp_pkt_in(cp
, pskb
, app
);
501 * Call private input hook function
503 if (app
->pkt_in
== NULL
)
506 return app
->pkt_in(app
, cp
, pskb
, NULL
);
510 #ifdef CONFIG_PROC_FS
512 * /proc/net/ip_vs_app entry function
515 static struct ip_vs_app
*ip_vs_app_idx(loff_t pos
)
517 struct ip_vs_app
*app
, *inc
;
519 list_for_each_entry(app
, &ip_vs_app_list
, a_list
) {
520 list_for_each_entry(inc
, &app
->incs_list
, a_list
) {
529 static void *ip_vs_app_seq_start(struct seq_file
*seq
, loff_t
*pos
)
531 down(&__ip_vs_app_mutex
);
533 return *pos
? ip_vs_app_idx(*pos
- 1) : SEQ_START_TOKEN
;
536 static void *ip_vs_app_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
538 struct ip_vs_app
*inc
, *app
;
542 if (v
== SEQ_START_TOKEN
)
543 return ip_vs_app_idx(0);
548 if ((e
= inc
->a_list
.next
) != &app
->incs_list
)
549 return list_entry(e
, struct ip_vs_app
, a_list
);
551 /* go on to next application */
552 for (e
= app
->a_list
.next
; e
!= &ip_vs_app_list
; e
= e
->next
) {
553 app
= list_entry(e
, struct ip_vs_app
, a_list
);
554 list_for_each_entry(inc
, &app
->incs_list
, a_list
) {
561 static void ip_vs_app_seq_stop(struct seq_file
*seq
, void *v
)
563 up(&__ip_vs_app_mutex
);
566 static int ip_vs_app_seq_show(struct seq_file
*seq
, void *v
)
568 if (v
== SEQ_START_TOKEN
)
569 seq_puts(seq
, "prot port usecnt name\n");
571 const struct ip_vs_app
*inc
= v
;
573 seq_printf(seq
, "%-3s %-7u %-6d %-17s\n",
574 ip_vs_proto_name(inc
->protocol
),
576 atomic_read(&inc
->usecnt
),
582 static struct seq_operations ip_vs_app_seq_ops
= {
583 .start
= ip_vs_app_seq_start
,
584 .next
= ip_vs_app_seq_next
,
585 .stop
= ip_vs_app_seq_stop
,
586 .show
= ip_vs_app_seq_show
,
589 static int ip_vs_app_open(struct inode
*inode
, struct file
*file
)
591 return seq_open(file
, &ip_vs_app_seq_ops
);
594 static struct file_operations ip_vs_app_fops
= {
595 .owner
= THIS_MODULE
,
596 .open
= ip_vs_app_open
,
599 .release
= seq_release
,
605 * Replace a segment of data with a new segment
607 int ip_vs_skb_replace(struct sk_buff
*skb
, int pri
,
608 char *o_buf
, int o_len
, char *n_buf
, int n_len
)
617 diff
= n_len
- o_len
;
618 o_offset
= o_buf
- (char *)skb
->data
;
619 /* The length of left data after o_buf+o_len in the skb data */
620 o_left
= skb
->len
- (o_offset
+ o_len
);
623 memmove(o_buf
+ n_len
, o_buf
+ o_len
, o_left
);
624 memcpy(o_buf
, n_buf
, n_len
);
625 skb_trim(skb
, skb
->len
+ diff
);
626 } else if (diff
<= skb_tailroom(skb
)) {
628 memmove(o_buf
+ n_len
, o_buf
+ o_len
, o_left
);
629 memcpy(o_buf
, n_buf
, n_len
);
631 if (pskb_expand_head(skb
, skb_headroom(skb
), diff
, pri
))
634 memmove(skb
->data
+ o_offset
+ n_len
,
635 skb
->data
+ o_offset
+ o_len
, o_left
);
636 memcpy(skb
->data
+ o_offset
, n_buf
, n_len
);
639 /* must update the iph total length here */
641 iph
->tot_len
= htons(skb
->len
);
648 int ip_vs_app_init(void)
650 /* we will replace it with proc_net_ipvs_create() soon */
651 proc_net_fops_create("ip_vs_app", 0, &ip_vs_app_fops
);
656 void ip_vs_app_cleanup(void)
658 proc_net_remove("ip_vs_app");