2 * ip_vs_app.c: Application module support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Most code here is taken from ip_masq_app.c in kernel 2.2. The difference
12 * is that ip_vs_app module handles the reverse direction (incoming requests
13 * and outgoing responses).
15 * IP_MASQ_APP application masquerading module
17 * Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/skbuff.h>
29 #include <linux/netfilter.h>
30 #include <net/net_namespace.h>
31 #include <net/protocol.h>
33 #include <asm/system.h>
34 #include <linux/stat.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/mutex.h>
39 #include <net/ip_vs.h>
41 EXPORT_SYMBOL(register_ip_vs_app
);
42 EXPORT_SYMBOL(unregister_ip_vs_app
);
43 EXPORT_SYMBOL(register_ip_vs_app_inc
);
45 /* ipvs application list head */
46 static LIST_HEAD(ip_vs_app_list
);
47 static DEFINE_MUTEX(__ip_vs_app_mutex
);
51 * Get an ip_vs_app object
53 static inline int ip_vs_app_get(struct ip_vs_app
*app
)
55 return try_module_get(app
->module
);
59 static inline void ip_vs_app_put(struct ip_vs_app
*app
)
61 module_put(app
->module
);
66 * Allocate/initialize app incarnation and register it in proto apps.
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
;
75 if (!(pp
= ip_vs_proto_get(proto
)))
76 return -EPROTONOSUPPORT
;
78 if (!pp
->unregister_app
)
81 inc
= kmemdup(app
, sizeof(*inc
), GFP_KERNEL
);
84 INIT_LIST_HEAD(&inc
->p_list
);
85 INIT_LIST_HEAD(&inc
->incs_list
);
87 inc
->port
= htons(port
);
88 atomic_set(&inc
->usecnt
, 0);
92 ip_vs_create_timeout_table(app
->timeouts
,
94 if (!inc
->timeout_table
) {
100 ret
= pp
->register_app(inc
);
104 list_add(&inc
->a_list
, &app
->incs_list
);
105 IP_VS_DBG(9, "%s application %s:%u registered\n",
106 pp
->name
, inc
->name
, inc
->port
);
111 kfree(inc
->timeout_table
);
118 * Release app incarnation
121 ip_vs_app_inc_release(struct ip_vs_app
*inc
)
123 struct ip_vs_protocol
*pp
;
125 if (!(pp
= ip_vs_proto_get(inc
->protocol
)))
128 if (pp
->unregister_app
)
129 pp
->unregister_app(inc
);
131 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
132 pp
->name
, inc
->name
, inc
->port
);
134 list_del(&inc
->a_list
);
136 kfree(inc
->timeout_table
);
142 * Get reference to app inc (only called from softirq)
145 int ip_vs_app_inc_get(struct ip_vs_app
*inc
)
149 atomic_inc(&inc
->usecnt
);
150 if (unlikely((result
= ip_vs_app_get(inc
->app
)) != 1))
151 atomic_dec(&inc
->usecnt
);
157 * Put the app inc (only called from timer or net softirq)
159 void ip_vs_app_inc_put(struct ip_vs_app
*inc
)
161 ip_vs_app_put(inc
->app
);
162 atomic_dec(&inc
->usecnt
);
167 * Register an application incarnation in protocol applications
170 register_ip_vs_app_inc(struct ip_vs_app
*app
, __u16 proto
, __u16 port
)
174 mutex_lock(&__ip_vs_app_mutex
);
176 result
= ip_vs_app_inc_new(app
, proto
, port
);
178 mutex_unlock(&__ip_vs_app_mutex
);
185 * ip_vs_app registration routine
187 int register_ip_vs_app(struct ip_vs_app
*app
)
189 /* increase the module use count */
190 ip_vs_use_count_inc();
192 mutex_lock(&__ip_vs_app_mutex
);
194 list_add(&app
->a_list
, &ip_vs_app_list
);
196 mutex_unlock(&__ip_vs_app_mutex
);
203 * ip_vs_app unregistration routine
204 * We are sure there are no app incarnations attached to services
206 void unregister_ip_vs_app(struct ip_vs_app
*app
)
208 struct ip_vs_app
*inc
, *nxt
;
210 mutex_lock(&__ip_vs_app_mutex
);
212 list_for_each_entry_safe(inc
, nxt
, &app
->incs_list
, a_list
) {
213 ip_vs_app_inc_release(inc
);
216 list_del(&app
->a_list
);
218 mutex_unlock(&__ip_vs_app_mutex
);
220 /* decrease the module use count */
221 ip_vs_use_count_dec();
226 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
228 int ip_vs_bind_app(struct ip_vs_conn
*cp
, struct ip_vs_protocol
*pp
)
230 return pp
->app_conn_bind(cp
);
235 * Unbind cp from application incarnation (called by cp destructor)
237 void ip_vs_unbind_app(struct ip_vs_conn
*cp
)
239 struct ip_vs_app
*inc
= cp
->app
;
244 if (inc
->unbind_conn
)
245 inc
->unbind_conn(inc
, cp
);
247 inc
->done_conn(inc
, cp
);
248 ip_vs_app_inc_put(inc
);
254 * Fixes th->seq based on ip_vs_seq info.
256 static inline void vs_fix_seq(const struct ip_vs_seq
*vseq
, struct tcphdr
*th
)
258 __u32 seq
= ntohl(th
->seq
);
261 * Adjust seq with delta-offset for all packets after
262 * the most recent resized pkt seq and with previous_delta offset
263 * for all packets before most recent resized pkt seq.
265 if (vseq
->delta
|| vseq
->previous_delta
) {
266 if(after(seq
, vseq
->init_seq
)) {
267 th
->seq
= htonl(seq
+ vseq
->delta
);
268 IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
269 __func__
, vseq
->delta
);
271 th
->seq
= htonl(seq
+ vseq
->previous_delta
);
272 IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
273 __func__
, vseq
->previous_delta
);
280 * Fixes th->ack_seq based on ip_vs_seq info.
283 vs_fix_ack_seq(const struct ip_vs_seq
*vseq
, struct tcphdr
*th
)
285 __u32 ack_seq
= ntohl(th
->ack_seq
);
288 * Adjust ack_seq with delta-offset for
289 * the packets AFTER most recent resized pkt has caused a shift
290 * for packets before most recent resized pkt, use previous_delta
292 if (vseq
->delta
|| vseq
->previous_delta
) {
293 /* since ack_seq is the number of octet that is expected
294 to receive next, so compare it with init_seq+delta */
295 if(after(ack_seq
, vseq
->init_seq
+vseq
->delta
)) {
296 th
->ack_seq
= htonl(ack_seq
- vseq
->delta
);
297 IP_VS_DBG(9, "%s(): subtracted delta "
298 "(%d) from ack_seq\n", __func__
, vseq
->delta
);
301 th
->ack_seq
= htonl(ack_seq
- vseq
->previous_delta
);
302 IP_VS_DBG(9, "%s(): subtracted "
303 "previous_delta (%d) from ack_seq\n",
304 __func__
, vseq
->previous_delta
);
311 * Updates ip_vs_seq if pkt has been resized
312 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
314 static inline void vs_seq_update(struct ip_vs_conn
*cp
, struct ip_vs_seq
*vseq
,
315 unsigned flag
, __u32 seq
, int diff
)
317 /* spinlock is to keep updating cp->flags atomic */
318 spin_lock(&cp
->lock
);
319 if (!(cp
->flags
& flag
) || after(seq
, vseq
->init_seq
)) {
320 vseq
->previous_delta
= vseq
->delta
;
322 vseq
->init_seq
= seq
;
325 spin_unlock(&cp
->lock
);
328 static inline int app_tcp_pkt_out(struct ip_vs_conn
*cp
, struct sk_buff
*skb
,
329 struct ip_vs_app
*app
)
332 const unsigned int tcp_offset
= ip_hdrlen(skb
);
336 if (!skb_make_writable(skb
, tcp_offset
+ sizeof(*th
)))
339 th
= (struct tcphdr
*)(skb_network_header(skb
) + tcp_offset
);
342 * Remember seq number in case this pkt gets resized
344 seq
= ntohl(th
->seq
);
347 * Fix seq stuff if flagged as so.
349 if (cp
->flags
& IP_VS_CONN_F_OUT_SEQ
)
350 vs_fix_seq(&cp
->out_seq
, th
);
351 if (cp
->flags
& IP_VS_CONN_F_IN_SEQ
)
352 vs_fix_ack_seq(&cp
->in_seq
, th
);
355 * Call private output hook function
357 if (app
->pkt_out
== NULL
)
360 if (!app
->pkt_out(app
, cp
, skb
, &diff
))
364 * Update ip_vs seq stuff if len has changed.
367 vs_seq_update(cp
, &cp
->out_seq
,
368 IP_VS_CONN_F_OUT_SEQ
, seq
, diff
);
374 * Output pkt hook. Will call bound ip_vs_app specific function
375 * called by ipvs packet handler, assumes previously checked cp!=NULL
376 * returns false if it can't handle packet (oom)
378 int ip_vs_app_pkt_out(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
380 struct ip_vs_app
*app
;
383 * check if application module is bound to
386 if ((app
= cp
->app
) == NULL
)
389 /* TCP is complicated */
390 if (cp
->protocol
== IPPROTO_TCP
)
391 return app_tcp_pkt_out(cp
, skb
, app
);
394 * Call private output hook function
396 if (app
->pkt_out
== NULL
)
399 return app
->pkt_out(app
, cp
, skb
, NULL
);
403 static inline int app_tcp_pkt_in(struct ip_vs_conn
*cp
, struct sk_buff
*skb
,
404 struct ip_vs_app
*app
)
407 const unsigned int tcp_offset
= ip_hdrlen(skb
);
411 if (!skb_make_writable(skb
, tcp_offset
+ sizeof(*th
)))
414 th
= (struct tcphdr
*)(skb_network_header(skb
) + tcp_offset
);
417 * Remember seq number in case this pkt gets resized
419 seq
= ntohl(th
->seq
);
422 * Fix seq stuff if flagged as so.
424 if (cp
->flags
& IP_VS_CONN_F_IN_SEQ
)
425 vs_fix_seq(&cp
->in_seq
, th
);
426 if (cp
->flags
& IP_VS_CONN_F_OUT_SEQ
)
427 vs_fix_ack_seq(&cp
->out_seq
, th
);
430 * Call private input hook function
432 if (app
->pkt_in
== NULL
)
435 if (!app
->pkt_in(app
, cp
, skb
, &diff
))
439 * Update ip_vs seq stuff if len has changed.
442 vs_seq_update(cp
, &cp
->in_seq
,
443 IP_VS_CONN_F_IN_SEQ
, seq
, diff
);
449 * Input pkt hook. Will call bound ip_vs_app specific function
450 * called by ipvs packet handler, assumes previously checked cp!=NULL.
451 * returns false if can't handle packet (oom).
453 int ip_vs_app_pkt_in(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
455 struct ip_vs_app
*app
;
458 * check if application module is bound to
461 if ((app
= cp
->app
) == NULL
)
464 /* TCP is complicated */
465 if (cp
->protocol
== IPPROTO_TCP
)
466 return app_tcp_pkt_in(cp
, skb
, app
);
469 * Call private input hook function
471 if (app
->pkt_in
== NULL
)
474 return app
->pkt_in(app
, cp
, skb
, NULL
);
478 #ifdef CONFIG_PROC_FS
480 * /proc/net/ip_vs_app entry function
483 static struct ip_vs_app
*ip_vs_app_idx(loff_t pos
)
485 struct ip_vs_app
*app
, *inc
;
487 list_for_each_entry(app
, &ip_vs_app_list
, a_list
) {
488 list_for_each_entry(inc
, &app
->incs_list
, a_list
) {
497 static void *ip_vs_app_seq_start(struct seq_file
*seq
, loff_t
*pos
)
499 mutex_lock(&__ip_vs_app_mutex
);
501 return *pos
? ip_vs_app_idx(*pos
- 1) : SEQ_START_TOKEN
;
504 static void *ip_vs_app_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
506 struct ip_vs_app
*inc
, *app
;
510 if (v
== SEQ_START_TOKEN
)
511 return ip_vs_app_idx(0);
516 if ((e
= inc
->a_list
.next
) != &app
->incs_list
)
517 return list_entry(e
, struct ip_vs_app
, a_list
);
519 /* go on to next application */
520 for (e
= app
->a_list
.next
; e
!= &ip_vs_app_list
; e
= e
->next
) {
521 app
= list_entry(e
, struct ip_vs_app
, a_list
);
522 list_for_each_entry(inc
, &app
->incs_list
, a_list
) {
529 static void ip_vs_app_seq_stop(struct seq_file
*seq
, void *v
)
531 mutex_unlock(&__ip_vs_app_mutex
);
534 static int ip_vs_app_seq_show(struct seq_file
*seq
, void *v
)
536 if (v
== SEQ_START_TOKEN
)
537 seq_puts(seq
, "prot port usecnt name\n");
539 const struct ip_vs_app
*inc
= v
;
541 seq_printf(seq
, "%-3s %-7u %-6d %-17s\n",
542 ip_vs_proto_name(inc
->protocol
),
544 atomic_read(&inc
->usecnt
),
550 static const struct seq_operations ip_vs_app_seq_ops
= {
551 .start
= ip_vs_app_seq_start
,
552 .next
= ip_vs_app_seq_next
,
553 .stop
= ip_vs_app_seq_stop
,
554 .show
= ip_vs_app_seq_show
,
557 static int ip_vs_app_open(struct inode
*inode
, struct file
*file
)
559 return seq_open(file
, &ip_vs_app_seq_ops
);
562 static const struct file_operations ip_vs_app_fops
= {
563 .owner
= THIS_MODULE
,
564 .open
= ip_vs_app_open
,
567 .release
= seq_release
,
573 * Replace a segment of data with a new segment
575 int ip_vs_skb_replace(struct sk_buff
*skb
, gfp_t pri
,
576 char *o_buf
, int o_len
, char *n_buf
, int n_len
)
584 diff
= n_len
- o_len
;
585 o_offset
= o_buf
- (char *)skb
->data
;
586 /* The length of left data after o_buf+o_len in the skb data */
587 o_left
= skb
->len
- (o_offset
+ o_len
);
590 memmove(o_buf
+ n_len
, o_buf
+ o_len
, o_left
);
591 memcpy(o_buf
, n_buf
, n_len
);
592 skb_trim(skb
, skb
->len
+ diff
);
593 } else if (diff
<= skb_tailroom(skb
)) {
595 memmove(o_buf
+ n_len
, o_buf
+ o_len
, o_left
);
596 memcpy(o_buf
, n_buf
, n_len
);
598 if (pskb_expand_head(skb
, skb_headroom(skb
), diff
, pri
))
601 memmove(skb
->data
+ o_offset
+ n_len
,
602 skb
->data
+ o_offset
+ o_len
, o_left
);
603 skb_copy_to_linear_data_offset(skb
, o_offset
, n_buf
, n_len
);
606 /* must update the iph total length here */
607 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
614 int __init
ip_vs_app_init(void)
616 /* we will replace it with proc_net_ipvs_create() soon */
617 proc_net_fops_create(&init_net
, "ip_vs_app", 0, &ip_vs_app_fops
);
622 void ip_vs_app_cleanup(void)
624 proc_net_remove(&init_net
, "ip_vs_app");