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 <linux/slab.h>
31 #include <net/net_namespace.h>
32 #include <net/protocol.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 static DEFINE_MUTEX(__ip_vs_app_mutex);
48 * Get an ip_vs_app object
50 static inline int ip_vs_app_get(struct ip_vs_app *app)
52 return try_module_get(app->module);
56 static inline void ip_vs_app_put(struct ip_vs_app *app)
58 module_put(app->module);
61 static void ip_vs_app_inc_destroy(struct ip_vs_app *inc)
63 kfree(inc->timeout_table);
67 static void ip_vs_app_inc_rcu_free(struct rcu_head *head)
69 struct ip_vs_app *inc = container_of(head, struct ip_vs_app, rcu_head);
71 ip_vs_app_inc_destroy(inc);
75 * Allocate/initialize app incarnation and register it in proto apps.
78 ip_vs_app_inc_new(struct net *net, struct ip_vs_app *app, __u16 proto,
81 struct ip_vs_protocol *pp;
82 struct ip_vs_app *inc;
85 if (!(pp = ip_vs_proto_get(proto)))
86 return -EPROTONOSUPPORT;
88 if (!pp->unregister_app)
91 inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
94 INIT_LIST_HEAD(&inc->p_list);
95 INIT_LIST_HEAD(&inc->incs_list);
97 inc->port = htons(port);
98 atomic_set(&inc->usecnt, 0);
102 ip_vs_create_timeout_table(app->timeouts,
104 if (!inc->timeout_table) {
110 ret = pp->register_app(net, inc);
114 list_add(&inc->a_list, &app->incs_list);
115 IP_VS_DBG(9, "%s App %s:%u registered\n",
116 pp->name, inc->name, ntohs(inc->port));
121 ip_vs_app_inc_destroy(inc);
127 * Release app incarnation
130 ip_vs_app_inc_release(struct net *net, struct ip_vs_app *inc)
132 struct ip_vs_protocol *pp;
134 if (!(pp = ip_vs_proto_get(inc->protocol)))
137 if (pp->unregister_app)
138 pp->unregister_app(net, inc);
140 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
141 pp->name, inc->name, ntohs(inc->port));
143 list_del(&inc->a_list);
145 call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free);
150 * Get reference to app inc (only called from softirq)
153 int ip_vs_app_inc_get(struct ip_vs_app *inc)
157 result = ip_vs_app_get(inc->app);
159 atomic_inc(&inc->usecnt);
165 * Put the app inc (only called from timer or net softirq)
167 void ip_vs_app_inc_put(struct ip_vs_app *inc)
169 atomic_dec(&inc->usecnt);
170 ip_vs_app_put(inc->app);
175 * Register an application incarnation in protocol applications
178 register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app, __u16 proto,
183 mutex_lock(&__ip_vs_app_mutex);
185 result = ip_vs_app_inc_new(net, app, proto, port);
187 mutex_unlock(&__ip_vs_app_mutex);
193 /* Register application for netns */
194 struct ip_vs_app *register_ip_vs_app(struct net *net, struct ip_vs_app *app)
196 struct netns_ipvs *ipvs = net_ipvs(net);
201 return ERR_PTR(-ENOENT);
203 mutex_lock(&__ip_vs_app_mutex);
205 list_for_each_entry(a, &ipvs->app_list, a_list) {
206 if (!strcmp(app->name, a->name)) {
211 a = kmemdup(app, sizeof(*app), GFP_KERNEL);
216 INIT_LIST_HEAD(&a->incs_list);
217 list_add(&a->a_list, &ipvs->app_list);
218 /* increase the module use count */
219 ip_vs_use_count_inc();
222 mutex_unlock(&__ip_vs_app_mutex);
224 return err ? ERR_PTR(err) : a;
229 * ip_vs_app unregistration routine
230 * We are sure there are no app incarnations attached to services
231 * Caller should use synchronize_rcu() or rcu_barrier()
233 void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app)
235 struct netns_ipvs *ipvs = net_ipvs(net);
236 struct ip_vs_app *a, *anxt, *inc, *nxt;
241 mutex_lock(&__ip_vs_app_mutex);
243 list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) {
244 if (app && strcmp(app->name, a->name))
246 list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) {
247 ip_vs_app_inc_release(net, inc);
250 list_del(&a->a_list);
253 /* decrease the module use count */
254 ip_vs_use_count_dec();
257 mutex_unlock(&__ip_vs_app_mutex);
262 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
264 int ip_vs_bind_app(struct ip_vs_conn *cp,
265 struct ip_vs_protocol *pp)
267 return pp->app_conn_bind(cp);
272 * Unbind cp from application incarnation (called by cp destructor)
274 void ip_vs_unbind_app(struct ip_vs_conn *cp)
276 struct ip_vs_app *inc = cp->app;
281 if (inc->unbind_conn)
282 inc->unbind_conn(inc, cp);
284 inc->done_conn(inc, cp);
285 ip_vs_app_inc_put(inc);
291 * Fixes th->seq based on ip_vs_seq info.
293 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
295 __u32 seq = ntohl(th->seq);
298 * Adjust seq with delta-offset for all packets after
299 * the most recent resized pkt seq and with previous_delta offset
300 * for all packets before most recent resized pkt seq.
302 if (vseq->delta || vseq->previous_delta) {
303 if(after(seq, vseq->init_seq)) {
304 th->seq = htonl(seq + vseq->delta);
305 IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
306 __func__, vseq->delta);
308 th->seq = htonl(seq + vseq->previous_delta);
309 IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
310 __func__, vseq->previous_delta);
317 * Fixes th->ack_seq based on ip_vs_seq info.
320 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
322 __u32 ack_seq = ntohl(th->ack_seq);
325 * Adjust ack_seq with delta-offset for
326 * the packets AFTER most recent resized pkt has caused a shift
327 * for packets before most recent resized pkt, use previous_delta
329 if (vseq->delta || vseq->previous_delta) {
330 /* since ack_seq is the number of octet that is expected
331 to receive next, so compare it with init_seq+delta */
332 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
333 th->ack_seq = htonl(ack_seq - vseq->delta);
334 IP_VS_DBG(9, "%s(): subtracted delta "
335 "(%d) from ack_seq\n", __func__, vseq->delta);
338 th->ack_seq = htonl(ack_seq - vseq->previous_delta);
339 IP_VS_DBG(9, "%s(): subtracted "
340 "previous_delta (%d) from ack_seq\n",
341 __func__, vseq->previous_delta);
348 * Updates ip_vs_seq if pkt has been resized
349 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
351 static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
352 unsigned int flag, __u32 seq, int diff)
354 /* spinlock is to keep updating cp->flags atomic */
355 spin_lock_bh(&cp->lock);
356 if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
357 vseq->previous_delta = vseq->delta;
359 vseq->init_seq = seq;
362 spin_unlock_bh(&cp->lock);
365 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
366 struct ip_vs_app *app)
369 const unsigned int tcp_offset = ip_hdrlen(skb);
373 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
376 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
379 * Remember seq number in case this pkt gets resized
381 seq = ntohl(th->seq);
384 * Fix seq stuff if flagged as so.
386 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
387 vs_fix_seq(&cp->out_seq, th);
388 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
389 vs_fix_ack_seq(&cp->in_seq, th);
392 * Call private output hook function
394 if (app->pkt_out == NULL)
397 if (!app->pkt_out(app, cp, skb, &diff))
401 * Update ip_vs seq stuff if len has changed.
404 vs_seq_update(cp, &cp->out_seq,
405 IP_VS_CONN_F_OUT_SEQ, seq, diff);
411 * Output pkt hook. Will call bound ip_vs_app specific function
412 * called by ipvs packet handler, assumes previously checked cp!=NULL
413 * returns false if it can't handle packet (oom)
415 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
417 struct ip_vs_app *app;
420 * check if application module is bound to
423 if ((app = cp->app) == NULL)
426 /* TCP is complicated */
427 if (cp->protocol == IPPROTO_TCP)
428 return app_tcp_pkt_out(cp, skb, app);
431 * Call private output hook function
433 if (app->pkt_out == NULL)
436 return app->pkt_out(app, cp, skb, NULL);
440 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
441 struct ip_vs_app *app)
444 const unsigned int tcp_offset = ip_hdrlen(skb);
448 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
451 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
454 * Remember seq number in case this pkt gets resized
456 seq = ntohl(th->seq);
459 * Fix seq stuff if flagged as so.
461 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
462 vs_fix_seq(&cp->in_seq, th);
463 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
464 vs_fix_ack_seq(&cp->out_seq, th);
467 * Call private input hook function
469 if (app->pkt_in == NULL)
472 if (!app->pkt_in(app, cp, skb, &diff))
476 * Update ip_vs seq stuff if len has changed.
479 vs_seq_update(cp, &cp->in_seq,
480 IP_VS_CONN_F_IN_SEQ, seq, diff);
486 * Input pkt hook. Will call bound ip_vs_app specific function
487 * called by ipvs packet handler, assumes previously checked cp!=NULL.
488 * returns false if can't handle packet (oom).
490 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
492 struct ip_vs_app *app;
495 * check if application module is bound to
498 if ((app = cp->app) == NULL)
501 /* TCP is complicated */
502 if (cp->protocol == IPPROTO_TCP)
503 return app_tcp_pkt_in(cp, skb, app);
506 * Call private input hook function
508 if (app->pkt_in == NULL)
511 return app->pkt_in(app, cp, skb, NULL);
515 #ifdef CONFIG_PROC_FS
517 * /proc/net/ip_vs_app entry function
520 static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos)
522 struct ip_vs_app *app, *inc;
524 list_for_each_entry(app, &ipvs->app_list, a_list) {
525 list_for_each_entry(inc, &app->incs_list, a_list) {
534 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
536 struct net *net = seq_file_net(seq);
537 struct netns_ipvs *ipvs = net_ipvs(net);
539 mutex_lock(&__ip_vs_app_mutex);
541 return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN;
544 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
546 struct ip_vs_app *inc, *app;
548 struct net *net = seq_file_net(seq);
549 struct netns_ipvs *ipvs = net_ipvs(net);
552 if (v == SEQ_START_TOKEN)
553 return ip_vs_app_idx(ipvs, 0);
558 if ((e = inc->a_list.next) != &app->incs_list)
559 return list_entry(e, struct ip_vs_app, a_list);
561 /* go on to next application */
562 for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) {
563 app = list_entry(e, struct ip_vs_app, a_list);
564 list_for_each_entry(inc, &app->incs_list, a_list) {
571 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
573 mutex_unlock(&__ip_vs_app_mutex);
576 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
578 if (v == SEQ_START_TOKEN)
579 seq_puts(seq, "prot port usecnt name\n");
581 const struct ip_vs_app *inc = v;
583 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
584 ip_vs_proto_name(inc->protocol),
586 atomic_read(&inc->usecnt),
592 static const struct seq_operations ip_vs_app_seq_ops = {
593 .start = ip_vs_app_seq_start,
594 .next = ip_vs_app_seq_next,
595 .stop = ip_vs_app_seq_stop,
596 .show = ip_vs_app_seq_show,
599 static int ip_vs_app_open(struct inode *inode, struct file *file)
601 return seq_open_net(inode, file, &ip_vs_app_seq_ops,
602 sizeof(struct seq_net_private));
605 static const struct file_operations ip_vs_app_fops = {
606 .owner = THIS_MODULE,
607 .open = ip_vs_app_open,
610 .release = seq_release_net,
614 int __net_init ip_vs_app_net_init(struct net *net)
616 struct netns_ipvs *ipvs = net_ipvs(net);
618 INIT_LIST_HEAD(&ipvs->app_list);
619 proc_create("ip_vs_app", 0, net->proc_net, &ip_vs_app_fops);
623 void __net_exit ip_vs_app_net_cleanup(struct net *net)
625 unregister_ip_vs_app(net, NULL /* all */);
626 remove_proc_entry("ip_vs_app", net->proc_net);