blob: 299edc6add5a6ac2c729e4caa6f9550b1cebe3df [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
Eric W. Biederman09858702015-09-21 13:02:33 -0500130ip_vs_app_inc_release(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct ip_vs_protocol *pp;
133
134 if (!(pp = ip_vs_proto_get(inc->protocol)))
135 return;
136
137 if (pp->unregister_app)
Eric W. Biederman19648912015-09-21 13:02:29 -0500138 pp->unregister_app(ipvs, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200141 pp->name, inc->name, ntohs(inc->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 list_del(&inc->a_list);
144
Julian Anastasov363c97d2013-03-21 11:58:07 +0200145 call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148
149/*
150 * Get reference to app inc (only called from softirq)
151 *
152 */
153int ip_vs_app_inc_get(struct ip_vs_app *inc)
154{
155 int result;
156
Julian Anastasov363c97d2013-03-21 11:58:07 +0200157 result = ip_vs_app_get(inc->app);
158 if (result)
159 atomic_inc(&inc->usecnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return result;
161}
162
163
164/*
165 * Put the app inc (only called from timer or net softirq)
166 */
167void ip_vs_app_inc_put(struct ip_vs_app *inc)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 atomic_dec(&inc->usecnt);
Julian Anastasov363c97d2013-03-21 11:58:07 +0200170 ip_vs_app_put(inc->app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173
174/*
175 * Register an application incarnation in protocol applications
176 */
177int
Eric W. Biederman3250dc92015-09-21 13:02:31 -0500178register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100179 __u16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int result;
182
Simon Horman736561a2011-03-21 15:18:01 +0000183 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Eric W. Biedermana080ce32015-09-21 13:02:30 -0500185 result = ip_vs_app_inc_new(ipvs, app, proto, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Simon Horman736561a2011-03-21 15:18:01 +0000187 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return result;
190}
191
192
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300193/* Register application for netns */
Eric W. Biederman9f8128a2015-09-21 13:02:32 -0500194struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300196 struct ip_vs_app *a;
197 int err = 0;
198
Simon Horman736561a2011-03-21 15:18:01 +0000199 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300201 list_for_each_entry(a, &ipvs->app_list, a_list) {
202 if (!strcmp(app->name, a->name)) {
203 err = -EEXIST;
204 goto out_unlock;
205 }
206 }
207 a = kmemdup(app, sizeof(*app), GFP_KERNEL);
208 if (!a) {
209 err = -ENOMEM;
210 goto out_unlock;
211 }
212 INIT_LIST_HEAD(&a->incs_list);
213 list_add(&a->a_list, &ipvs->app_list);
214 /* increase the module use count */
215 ip_vs_use_count_inc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300217out_unlock:
Simon Horman736561a2011-03-21 15:18:01 +0000218 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300220 return err ? ERR_PTR(err) : a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223
224/*
225 * ip_vs_app unregistration routine
226 * We are sure there are no app incarnations attached to services
Julian Anastasov363c97d2013-03-21 11:58:07 +0200227 * Caller should use synchronize_rcu() or rcu_barrier()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Eric W. Biederman9f8128a2015-09-21 13:02:32 -0500229void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300231 struct ip_vs_app *a, *anxt, *inc, *nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Simon Horman736561a2011-03-21 15:18:01 +0000233 mutex_lock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300235 list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) {
236 if (app && strcmp(app->name, a->name))
237 continue;
238 list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) {
Eric W. Biederman09858702015-09-21 13:02:33 -0500239 ip_vs_app_inc_release(ipvs, inc);
Julian Anastasovbe97fdb2012-07-12 23:06:20 +0300240 }
241
242 list_del(&a->a_list);
243 kfree(a);
244
245 /* decrease the module use count */
246 ip_vs_use_count_dec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Simon Horman736561a2011-03-21 15:18:01 +0000249 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
255 */
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100256int ip_vs_bind_app(struct ip_vs_conn *cp,
257 struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 return pp->app_conn_bind(cp);
260}
261
262
263/*
264 * Unbind cp from application incarnation (called by cp destructor)
265 */
266void ip_vs_unbind_app(struct ip_vs_conn *cp)
267{
268 struct ip_vs_app *inc = cp->app;
269
270 if (!inc)
271 return;
272
273 if (inc->unbind_conn)
274 inc->unbind_conn(inc, cp);
275 if (inc->done_conn)
276 inc->done_conn(inc, cp);
277 ip_vs_app_inc_put(inc);
278 cp->app = NULL;
279}
280
281
282/*
283 * Fixes th->seq based on ip_vs_seq info.
284 */
285static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
286{
287 __u32 seq = ntohl(th->seq);
288
289 /*
290 * Adjust seq with delta-offset for all packets after
291 * the most recent resized pkt seq and with previous_delta offset
292 * for all packets before most recent resized pkt seq.
293 */
294 if (vseq->delta || vseq->previous_delta) {
295 if(after(seq, vseq->init_seq)) {
296 th->seq = htonl(seq + vseq->delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000297 IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
298 __func__, vseq->delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 th->seq = htonl(seq + vseq->previous_delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000301 IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
302 __func__, vseq->previous_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 }
305}
306
307
308/*
309 * Fixes th->ack_seq based on ip_vs_seq info.
310 */
311static inline void
312vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
313{
314 __u32 ack_seq = ntohl(th->ack_seq);
315
316 /*
317 * Adjust ack_seq with delta-offset for
318 * the packets AFTER most recent resized pkt has caused a shift
319 * for packets before most recent resized pkt, use previous_delta
320 */
321 if (vseq->delta || vseq->previous_delta) {
322 /* since ack_seq is the number of octet that is expected
323 to receive next, so compare it with init_seq+delta */
324 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
325 th->ack_seq = htonl(ack_seq - vseq->delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000326 IP_VS_DBG(9, "%s(): subtracted delta "
327 "(%d) from ack_seq\n", __func__, vseq->delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 } else {
330 th->ack_seq = htonl(ack_seq - vseq->previous_delta);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000331 IP_VS_DBG(9, "%s(): subtracted "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 "previous_delta (%d) from ack_seq\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000333 __func__, vseq->previous_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
336}
337
338
339/*
340 * Updates ip_vs_seq if pkt has been resized
341 * Assumes already checked proto==IPPROTO_TCP and diff!=0.
342 */
343static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
Eric Dumazet95c96172012-04-15 05:58:06 +0000344 unsigned int flag, __u32 seq, int diff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 /* spinlock is to keep updating cp->flags atomic */
Julian Anastasovac692692013-03-22 11:46:54 +0200347 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
349 vseq->previous_delta = vseq->delta;
350 vseq->delta += diff;
351 vseq->init_seq = seq;
352 cp->flags |= flag;
353 }
Julian Anastasovac692692013-03-22 11:46:54 +0200354 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Herbert Xu3db05fe2007-10-15 00:53:15 -0700357static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct ip_vs_app *app)
359{
360 int diff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700361 const unsigned int tcp_offset = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct tcphdr *th;
363 __u32 seq;
364
Herbert Xu3db05fe2007-10-15 00:53:15 -0700365 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367
Herbert Xu3db05fe2007-10-15 00:53:15 -0700368 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /*
371 * Remember seq number in case this pkt gets resized
372 */
373 seq = ntohl(th->seq);
374
375 /*
376 * Fix seq stuff if flagged as so.
377 */
378 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
379 vs_fix_seq(&cp->out_seq, th);
380 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
381 vs_fix_ack_seq(&cp->in_seq, th);
382
383 /*
384 * Call private output hook function
385 */
386 if (app->pkt_out == NULL)
387 return 1;
388
Herbert Xu3db05fe2007-10-15 00:53:15 -0700389 if (!app->pkt_out(app, cp, skb, &diff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391
392 /*
393 * Update ip_vs seq stuff if len has changed.
394 */
395 if (diff != 0)
396 vs_seq_update(cp, &cp->out_seq,
397 IP_VS_CONN_F_OUT_SEQ, seq, diff);
398
399 return 1;
400}
401
402/*
403 * Output pkt hook. Will call bound ip_vs_app specific function
404 * called by ipvs packet handler, assumes previously checked cp!=NULL
405 * returns false if it can't handle packet (oom)
406 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700407int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct ip_vs_app *app;
410
411 /*
412 * check if application module is bound to
413 * this ip_vs_conn.
414 */
415 if ((app = cp->app) == NULL)
416 return 1;
417
418 /* TCP is complicated */
419 if (cp->protocol == IPPROTO_TCP)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700420 return app_tcp_pkt_out(cp, skb, app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /*
423 * Call private output hook function
424 */
425 if (app->pkt_out == NULL)
426 return 1;
427
Herbert Xu3db05fe2007-10-15 00:53:15 -0700428 return app->pkt_out(app, cp, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431
Herbert Xu3db05fe2007-10-15 00:53:15 -0700432static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct ip_vs_app *app)
434{
435 int diff;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700436 const unsigned int tcp_offset = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct tcphdr *th;
438 __u32 seq;
439
Herbert Xu3db05fe2007-10-15 00:53:15 -0700440 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442
Herbert Xu3db05fe2007-10-15 00:53:15 -0700443 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /*
446 * Remember seq number in case this pkt gets resized
447 */
448 seq = ntohl(th->seq);
449
450 /*
451 * Fix seq stuff if flagged as so.
452 */
453 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
454 vs_fix_seq(&cp->in_seq, th);
455 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
456 vs_fix_ack_seq(&cp->out_seq, th);
457
458 /*
459 * Call private input hook function
460 */
461 if (app->pkt_in == NULL)
462 return 1;
463
Herbert Xu3db05fe2007-10-15 00:53:15 -0700464 if (!app->pkt_in(app, cp, skb, &diff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466
467 /*
468 * Update ip_vs seq stuff if len has changed.
469 */
470 if (diff != 0)
471 vs_seq_update(cp, &cp->in_seq,
472 IP_VS_CONN_F_IN_SEQ, seq, diff);
473
474 return 1;
475}
476
477/*
478 * Input pkt hook. Will call bound ip_vs_app specific function
479 * called by ipvs packet handler, assumes previously checked cp!=NULL.
480 * returns false if can't handle packet (oom).
481 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700482int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct ip_vs_app *app;
485
486 /*
487 * check if application module is bound to
488 * this ip_vs_conn.
489 */
490 if ((app = cp->app) == NULL)
491 return 1;
492
493 /* TCP is complicated */
494 if (cp->protocol == IPPROTO_TCP)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700495 return app_tcp_pkt_in(cp, skb, app);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /*
498 * Call private input hook function
499 */
500 if (app->pkt_in == NULL)
501 return 1;
502
Herbert Xu3db05fe2007-10-15 00:53:15 -0700503 return app->pkt_in(app, cp, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506
507#ifdef CONFIG_PROC_FS
508/*
509 * /proc/net/ip_vs_app entry function
510 */
511
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100512static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct ip_vs_app *app, *inc;
515
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100516 list_for_each_entry(app, &ipvs->app_list, a_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 list_for_each_entry(inc, &app->incs_list, a_list) {
518 if (pos-- == 0)
519 return inc;
520 }
521 }
522 return NULL;
523
524}
525
526static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
527{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100528 struct net *net = seq_file_net(seq);
529 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Simon Horman736561a2011-03-21 15:18:01 +0000531 mutex_lock(&__ip_vs_app_mutex);
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100532
533 return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
537{
538 struct ip_vs_app *inc, *app;
539 struct list_head *e;
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100540 struct net *net = seq_file_net(seq);
541 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 ++*pos;
544 if (v == SEQ_START_TOKEN)
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100545 return ip_vs_app_idx(ipvs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 inc = v;
548 app = inc->app;
549
550 if ((e = inc->a_list.next) != &app->incs_list)
551 return list_entry(e, struct ip_vs_app, a_list);
552
553 /* go on to next application */
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100554 for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 app = list_entry(e, struct ip_vs_app, a_list);
556 list_for_each_entry(inc, &app->incs_list, a_list) {
557 return inc;
558 }
559 }
560 return NULL;
561}
562
563static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
564{
Simon Horman736561a2011-03-21 15:18:01 +0000565 mutex_unlock(&__ip_vs_app_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
569{
570 if (v == SEQ_START_TOKEN)
571 seq_puts(seq, "prot port usecnt name\n");
572 else {
573 const struct ip_vs_app *inc = v;
574
575 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
576 ip_vs_proto_name(inc->protocol),
577 ntohs(inc->port),
578 atomic_read(&inc->usecnt),
579 inc->name);
580 }
581 return 0;
582}
583
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700584static const struct seq_operations ip_vs_app_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .start = ip_vs_app_seq_start,
586 .next = ip_vs_app_seq_next,
587 .stop = ip_vs_app_seq_stop,
588 .show = ip_vs_app_seq_show,
589};
590
591static int ip_vs_app_open(struct inode *inode, struct file *file)
592{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100593 return seq_open_net(inode, file, &ip_vs_app_seq_ops,
594 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Arjan van de Ven9a321442007-02-12 00:55:35 -0800597static const struct file_operations ip_vs_app_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .owner = THIS_MODULE,
599 .open = ip_vs_app_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +0200602 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604#endif
605
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -0500606int __net_init ip_vs_app_net_init(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100607{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100608 INIT_LIST_HEAD(&ipvs->app_list);
Arnd Bergmannf6ca9f42016-01-27 14:52:01 +0100609 proc_create("ip_vs_app", 0, ipvs->net->proc_net, &ip_vs_app_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100610 return 0;
611}
612
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -0500613void __net_exit ip_vs_app_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100614{
Eric W. Biederman9f8128a2015-09-21 13:02:32 -0500615 unregister_ip_vs_app(ipvs, NULL /* all */);
Arnd Bergmannf6ca9f42016-01-27 14:52:01 +0100616 remove_proc_entry("ip_vs_app", ipvs->net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100617}