blob: 20cb947a3ddd5615936e14ae6e01084a6ff53aaa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_app.c: Application module support for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 *
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.
10 *
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).
14 *
15 * IP_MASQ_APP application masquerading module
16 *
17 * Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
18 *
19 */
20
Hannes Eder9aada7a2009-07-30 14:29:44 -070021#define KMSG_COMPONENT "IPVS"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/skbuff.h>
27#include <linux/in.h>
28#include <linux/ip.h>
Herbert Xuaf1e1cf2007-10-14 00:39:33 -070029#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020031#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/protocol.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070033#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/stat.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <net/ip_vs.h>
40
41EXPORT_SYMBOL(register_ip_vs_app);
42EXPORT_SYMBOL(unregister_ip_vs_app);
43EXPORT_SYMBOL(register_ip_vs_app_inc);
44
Simon Horman736561a2011-03-21 15:18:01 +000045static DEFINE_MUTEX(__ip_vs_app_mutex);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Get an ip_vs_app object
49 */
50static inline int ip_vs_app_get(struct ip_vs_app *app)
51{
Pavel Emelyanov85b60682007-11-19 22:52:41 -080052 return try_module_get(app->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55
56static inline void ip_vs_app_put(struct ip_vs_app *app)
57{
Pavel Emelyanov85b60682007-11-19 22:52:41 -080058 module_put(app->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Julian Anastasov363c97d2013-03-21 11:58:07 +020061static void ip_vs_app_inc_destroy(struct ip_vs_app *inc)
62{
63 kfree(inc->timeout_table);
64 kfree(inc);
65}
66
67static void ip_vs_app_inc_rcu_free(struct rcu_head *head)
68{
69 struct ip_vs_app *inc = container_of(head, struct ip_vs_app, rcu_head);
70
71 ip_vs_app_inc_destroy(inc);
72}
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Allocate/initialize app incarnation and register it in proto apps.
76 */
77static int
Eric W. Biedermana080ce32015-09-21 13:02:30 -050078ip_vs_app_inc_new(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
Hans Schillstromab8a5e82011-01-03 14:44:53 +010079 __u16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct ip_vs_protocol *pp;
82 struct ip_vs_app *inc;
83 int ret;
84
85 if (!(pp = ip_vs_proto_get(proto)))
86 return -EPROTONOSUPPORT;
87
88 if (!pp->unregister_app)
89 return -EOPNOTSUPP;
90
Arnaldo Carvalho de Melo8b2ed4b2006-11-21 01:17:18 -020091 inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!inc)
93 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 INIT_LIST_HEAD(&inc->p_list);
95 INIT_LIST_HEAD(&inc->incs_list);
96 inc->app = app;
97 inc->port = htons(port);
98 atomic_set(&inc->usecnt, 0);
99
100 if (app->timeouts) {
101 inc->timeout_table =
102 ip_vs_create_timeout_table(app->timeouts,
103 app->timeouts_size);
104 if (!inc->timeout_table) {
105 ret = -ENOMEM;
106 goto out;
107 }
108 }
109
Eric W. Biederman19648912015-09-21 13:02:29 -0500110 ret = pp->register_app(ipvs, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (ret)
112 goto out;
113
114 list_add(&inc->a_list, &app->incs_list);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200115 IP_VS_DBG(9, "%s App %s:%u registered\n",
116 pp->name, inc->name, ntohs(inc->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return 0;
119
120 out:
Julian Anastasov363c97d2013-03-21 11:58:07 +0200121 ip_vs_app_inc_destroy(inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return ret;
123}
124
125
126/*
127 * Release app incarnation
128 */
129static void
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100130ip_vs_app_inc_release(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Eric W. Biederman19648912015-09-21 13:02:29 -0500132 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct ip_vs_protocol *pp;
134
135 if (!(pp = ip_vs_proto_get(inc->protocol)))
136 return;
137
138 if (pp->unregister_app)
Eric W. Biederman19648912015-09-21 13:02:29 -0500139 pp->unregister_app(ipvs, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200142 pp->name, inc->name, ntohs(inc->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 list_del(&inc->a_list);
145
Julian Anastasov363c97d2013-03-21 11:58:07 +0200146 call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149
150/*
151 * Get reference to app inc (only called from softirq)
152 *
153 */
154int ip_vs_app_inc_get(struct ip_vs_app *inc)
155{
156 int result;
157
Julian Anastasov363c97d2013-03-21 11:58:07 +0200158 result = ip_vs_app_get(inc->app);
159 if (result)
160 atomic_inc(&inc->usecnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return result;
162}
163
164
165/*
166 * Put the app inc (only called from timer or net softirq)
167 */
168void ip_vs_app_inc_put(struct ip_vs_app *inc)
169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 atomic_dec(&inc->usecnt);
Julian Anastasov363c97d2013-03-21 11:58:07 +0200171 ip_vs_app_put(inc->app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174
175/*
176 * Register an application incarnation in protocol applications
177 */
178int
Eric W. Biederman3250dc92015-09-21 13:02:31 -0500179register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100180 __u16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int result;
183
Simon Horman736561a2011-03-21 15:18:01 +0000184 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Eric W. Biedermana080ce32015-09-21 13:02:30 -0500186 result = ip_vs_app_inc_new(ipvs, app, proto, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Simon Horman736561a2011-03-21 15:18:01 +0000188 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 return result;
191}
192
193
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300194/* Register application for netns */
195struct ip_vs_app *register_ip_vs_app(struct net *net, struct ip_vs_app *app)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100197 struct netns_ipvs *ipvs = net_ipvs(net);
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300198 struct ip_vs_app *a;
199 int err = 0;
200
201 if (!ipvs)
202 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Simon Horman736561a2011-03-21 15:18:01 +0000204 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300206 list_for_each_entry(a, &ipvs->app_list, a_list) {
207 if (!strcmp(app->name, a->name)) {
208 err = -EEXIST;
209 goto out_unlock;
210 }
211 }
212 a = kmemdup(app, sizeof(*app), GFP_KERNEL);
213 if (!a) {
214 err = -ENOMEM;
215 goto out_unlock;
216 }
217 INIT_LIST_HEAD(&a->incs_list);
218 list_add(&a->a_list, &ipvs->app_list);
219 /* increase the module use count */
220 ip_vs_use_count_inc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300222out_unlock:
Simon Horman736561a2011-03-21 15:18:01 +0000223 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300225 return err ? ERR_PTR(err) : a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228
229/*
230 * ip_vs_app unregistration routine
231 * We are sure there are no app incarnations attached to services
Julian Anastasov363c97d2013-03-21 11:58:07 +0200232 * Caller should use synchronize_rcu() or rcu_barrier()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100234void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300236 struct netns_ipvs *ipvs = net_ipvs(net);
237 struct ip_vs_app *a, *anxt, *inc, *nxt;
238
239 if (!ipvs)
240 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Simon Horman736561a2011-03-21 15:18:01 +0000242 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300244 list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) {
245 if (app && strcmp(app->name, a->name))
246 continue;
247 list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) {
248 ip_vs_app_inc_release(net, inc);
249 }
250
251 list_del(&a->a_list);
252 kfree(a);
253
254 /* decrease the module use count */
255 ip_vs_use_count_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Simon Horman736561a2011-03-21 15:18:01 +0000258 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
263 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
264 */
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100265int ip_vs_bind_app(struct ip_vs_conn *cp,
266 struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 return pp->app_conn_bind(cp);
269}
270
271
272/*
273 * Unbind cp from application incarnation (called by cp destructor)
274 */
275void ip_vs_unbind_app(struct ip_vs_conn *cp)
276{
277 struct ip_vs_app *inc = cp->app;
278
279 if (!inc)
280 return;
281
282 if (inc->unbind_conn)
283 inc->unbind_conn(inc, cp);
284 if (inc->done_conn)
285 inc->done_conn(inc, cp);
286 ip_vs_app_inc_put(inc);
287 cp->app = NULL;
288}
289
290
291/*
292 * Fixes th->seq based on ip_vs_seq info.
293 */
294static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
295{
296 __u32 seq = ntohl(th->seq);
297
298 /*
299 * Adjust seq with delta-offset for all packets after
300 * the most recent resized pkt seq and with previous_delta offset
301 * for all packets before most recent resized pkt seq.
302 */
303 if (vseq->delta || vseq->previous_delta) {
304 if(after(seq, vseq->init_seq)) {
305 th->seq = htonl(seq + vseq->delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000306 IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
307 __func__, vseq->delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 } else {
309 th->seq = htonl(seq + vseq->previous_delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000310 IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
311 __func__, vseq->previous_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314}
315
316
317/*
318 * Fixes th->ack_seq based on ip_vs_seq info.
319 */
320static inline void
321vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
322{
323 __u32 ack_seq = ntohl(th->ack_seq);
324
325 /*
326 * Adjust ack_seq with delta-offset for
327 * the packets AFTER most recent resized pkt has caused a shift
328 * for packets before most recent resized pkt, use previous_delta
329 */
330 if (vseq->delta || vseq->previous_delta) {
331 /* since ack_seq is the number of octet that is expected
332 to receive next, so compare it with init_seq+delta */
333 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
334 th->ack_seq = htonl(ack_seq - vseq->delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000335 IP_VS_DBG(9, "%s(): subtracted delta "
336 "(%d) from ack_seq\n", __func__, vseq->delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 } else {
339 th->ack_seq = htonl(ack_seq - vseq->previous_delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000340 IP_VS_DBG(9, "%s(): subtracted "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 "previous_delta (%d) from ack_seq\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000342 __func__, vseq->previous_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 }
345}
346
347
348/*
349 * Updates ip_vs_seq if pkt has been resized
350 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
351 */
352static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
Eric Dumazet95c96172012-04-15 05:58:06 +0000353 unsigned int flag, __u32 seq, int diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 /* spinlock is to keep updating cp->flags atomic */
Julian Anastasovac692692013-03-22 11:46:54 +0200356 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
358 vseq->previous_delta = vseq->delta;
359 vseq->delta += diff;
360 vseq->init_seq = seq;
361 cp->flags |= flag;
362 }
Julian Anastasovac692692013-03-22 11:46:54 +0200363 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Herbert Xu3db05fe2007-10-15 00:53:15 -0700366static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct ip_vs_app *app)
368{
369 int diff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700370 const unsigned int tcp_offset = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct tcphdr *th;
372 __u32 seq;
373
Herbert Xu3db05fe2007-10-15 00:53:15 -0700374 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376
Herbert Xu3db05fe2007-10-15 00:53:15 -0700377 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * Remember seq number in case this pkt gets resized
381 */
382 seq = ntohl(th->seq);
383
384 /*
385 * Fix seq stuff if flagged as so.
386 */
387 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
388 vs_fix_seq(&cp->out_seq, th);
389 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
390 vs_fix_ack_seq(&cp->in_seq, th);
391
392 /*
393 * Call private output hook function
394 */
395 if (app->pkt_out == NULL)
396 return 1;
397
Herbert Xu3db05fe2007-10-15 00:53:15 -0700398 if (!app->pkt_out(app, cp, skb, &diff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400
401 /*
402 * Update ip_vs seq stuff if len has changed.
403 */
404 if (diff != 0)
405 vs_seq_update(cp, &cp->out_seq,
406 IP_VS_CONN_F_OUT_SEQ, seq, diff);
407
408 return 1;
409}
410
411/*
412 * Output pkt hook. Will call bound ip_vs_app specific function
413 * called by ipvs packet handler, assumes previously checked cp!=NULL
414 * returns false if it can't handle packet (oom)
415 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700416int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct ip_vs_app *app;
419
420 /*
421 * check if application module is bound to
422 * this ip_vs_conn.
423 */
424 if ((app = cp->app) == NULL)
425 return 1;
426
427 /* TCP is complicated */
428 if (cp->protocol == IPPROTO_TCP)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700429 return app_tcp_pkt_out(cp, skb, app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * Call private output hook function
433 */
434 if (app->pkt_out == NULL)
435 return 1;
436
Herbert Xu3db05fe2007-10-15 00:53:15 -0700437 return app->pkt_out(app, cp, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440
Herbert Xu3db05fe2007-10-15 00:53:15 -0700441static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct ip_vs_app *app)
443{
444 int diff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700445 const unsigned int tcp_offset = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct tcphdr *th;
447 __u32 seq;
448
Herbert Xu3db05fe2007-10-15 00:53:15 -0700449 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451
Herbert Xu3db05fe2007-10-15 00:53:15 -0700452 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /*
455 * Remember seq number in case this pkt gets resized
456 */
457 seq = ntohl(th->seq);
458
459 /*
460 * Fix seq stuff if flagged as so.
461 */
462 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
463 vs_fix_seq(&cp->in_seq, th);
464 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
465 vs_fix_ack_seq(&cp->out_seq, th);
466
467 /*
468 * Call private input hook function
469 */
470 if (app->pkt_in == NULL)
471 return 1;
472
Herbert Xu3db05fe2007-10-15 00:53:15 -0700473 if (!app->pkt_in(app, cp, skb, &diff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475
476 /*
477 * Update ip_vs seq stuff if len has changed.
478 */
479 if (diff != 0)
480 vs_seq_update(cp, &cp->in_seq,
481 IP_VS_CONN_F_IN_SEQ, seq, diff);
482
483 return 1;
484}
485
486/*
487 * Input pkt hook. Will call bound ip_vs_app specific function
488 * called by ipvs packet handler, assumes previously checked cp!=NULL.
489 * returns false if can't handle packet (oom).
490 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700491int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct ip_vs_app *app;
494
495 /*
496 * check if application module is bound to
497 * this ip_vs_conn.
498 */
499 if ((app = cp->app) == NULL)
500 return 1;
501
502 /* TCP is complicated */
503 if (cp->protocol == IPPROTO_TCP)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700504 return app_tcp_pkt_in(cp, skb, app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * Call private input hook function
508 */
509 if (app->pkt_in == NULL)
510 return 1;
511
Herbert Xu3db05fe2007-10-15 00:53:15 -0700512 return app->pkt_in(app, cp, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515
516#ifdef CONFIG_PROC_FS
517/*
518 * /proc/net/ip_vs_app entry function
519 */
520
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100521static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct ip_vs_app *app, *inc;
524
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100525 list_for_each_entry(app, &ipvs->app_list, a_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 list_for_each_entry(inc, &app->incs_list, a_list) {
527 if (pos-- == 0)
528 return inc;
529 }
530 }
531 return NULL;
532
533}
534
535static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
536{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100537 struct net *net = seq_file_net(seq);
538 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Simon Horman736561a2011-03-21 15:18:01 +0000540 mutex_lock(&__ip_vs_app_mutex);
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100541
542 return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
546{
547 struct ip_vs_app *inc, *app;
548 struct list_head *e;
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100549 struct net *net = seq_file_net(seq);
550 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 ++*pos;
553 if (v == SEQ_START_TOKEN)
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100554 return ip_vs_app_idx(ipvs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 inc = v;
557 app = inc->app;
558
559 if ((e = inc->a_list.next) != &app->incs_list)
560 return list_entry(e, struct ip_vs_app, a_list);
561
562 /* go on to next application */
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100563 for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 app = list_entry(e, struct ip_vs_app, a_list);
565 list_for_each_entry(inc, &app->incs_list, a_list) {
566 return inc;
567 }
568 }
569 return NULL;
570}
571
572static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
573{
Simon Horman736561a2011-03-21 15:18:01 +0000574 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
578{
579 if (v == SEQ_START_TOKEN)
580 seq_puts(seq, "prot port usecnt name\n");
581 else {
582 const struct ip_vs_app *inc = v;
583
584 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
585 ip_vs_proto_name(inc->protocol),
586 ntohs(inc->port),
587 atomic_read(&inc->usecnt),
588 inc->name);
589 }
590 return 0;
591}
592
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700593static const struct seq_operations ip_vs_app_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .start = ip_vs_app_seq_start,
595 .next = ip_vs_app_seq_next,
596 .stop = ip_vs_app_seq_stop,
597 .show = ip_vs_app_seq_show,
598};
599
600static int ip_vs_app_open(struct inode *inode, struct file *file)
601{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100602 return seq_open_net(inode, file, &ip_vs_app_seq_ops,
603 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Arjan van de Ven9a321442007-02-12 00:55:35 -0800606static const struct file_operations ip_vs_app_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 .owner = THIS_MODULE,
608 .open = ip_vs_app_open,
609 .read = seq_read,
610 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +0200611 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612};
613#endif
614
Hans Schillstrom503cf152011-05-01 18:50:16 +0200615int __net_init ip_vs_app_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100616{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100617 struct netns_ipvs *ipvs = net_ipvs(net);
618
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100619 INIT_LIST_HEAD(&ipvs->app_list);
Gao fengd4beaa62013-02-18 01:34:54 +0000620 proc_create("ip_vs_app", 0, net->proc_net, &ip_vs_app_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100621 return 0;
622}
623
Hans Schillstrom503cf152011-05-01 18:50:16 +0200624void __net_exit ip_vs_app_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100625{
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300626 unregister_ip_vs_app(net, NULL /* all */);
Gao fengece31ff2013-02-18 01:34:56 +0000627 remove_proc_entry("ip_vs_app", net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100628}