GNU Linux-libre 4.19.314-gnu1
[releases.git] / net / netfilter / ipvs / ip_vs_app.c
1 /*
2  * ip_vs_app.c: Application module support for IPVS
3  *
4  * 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
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/skbuff.h>
27 #include <linux/in.h>
28 #include <linux/ip.h>
29 #include <linux/netfilter.h>
30 #include <linux/slab.h>
31 #include <net/net_namespace.h>
32 #include <net/protocol.h>
33 #include <net/tcp.h>
34 #include <linux/stat.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/mutex.h>
38
39 #include <net/ip_vs.h>
40
41 EXPORT_SYMBOL(register_ip_vs_app);
42 EXPORT_SYMBOL(unregister_ip_vs_app);
43 EXPORT_SYMBOL(register_ip_vs_app_inc);
44
45 static DEFINE_MUTEX(__ip_vs_app_mutex);
46
47 /*
48  *      Get an ip_vs_app object
49  */
50 static inline int ip_vs_app_get(struct ip_vs_app *app)
51 {
52         return try_module_get(app->module);
53 }
54
55
56 static inline void ip_vs_app_put(struct ip_vs_app *app)
57 {
58         module_put(app->module);
59 }
60
61 static void ip_vs_app_inc_destroy(struct ip_vs_app *inc)
62 {
63         kfree(inc->timeout_table);
64         kfree(inc);
65 }
66
67 static 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 }
73
74 /*
75  *      Allocate/initialize app incarnation and register it in proto apps.
76  */
77 static int
78 ip_vs_app_inc_new(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
79                   __u16 port)
80 {
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
91         inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
92         if (!inc)
93                 return -ENOMEM;
94         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
110         ret = pp->register_app(ipvs, inc);
111         if (ret)
112                 goto out;
113
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));
117
118         return 0;
119
120   out:
121         ip_vs_app_inc_destroy(inc);
122         return ret;
123 }
124
125
126 /*
127  *      Release app incarnation
128  */
129 static void
130 ip_vs_app_inc_release(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
131 {
132         struct ip_vs_protocol *pp;
133
134         if (!(pp = ip_vs_proto_get(inc->protocol)))
135                 return;
136
137         if (pp->unregister_app)
138                 pp->unregister_app(ipvs, inc);
139
140         IP_VS_DBG(9, "%s App %s:%u unregistered\n",
141                   pp->name, inc->name, ntohs(inc->port));
142
143         list_del(&inc->a_list);
144
145         call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free);
146 }
147
148
149 /*
150  *      Get reference to app inc (only called from softirq)
151  *
152  */
153 int ip_vs_app_inc_get(struct ip_vs_app *inc)
154 {
155         int result;
156
157         result = ip_vs_app_get(inc->app);
158         if (result)
159                 atomic_inc(&inc->usecnt);
160         return result;
161 }
162
163
164 /*
165  *      Put the app inc (only called from timer or net softirq)
166  */
167 void ip_vs_app_inc_put(struct ip_vs_app *inc)
168 {
169         atomic_dec(&inc->usecnt);
170         ip_vs_app_put(inc->app);
171 }
172
173
174 /*
175  *      Register an application incarnation in protocol applications
176  */
177 int
178 register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
179                        __u16 port)
180 {
181         int result;
182
183         mutex_lock(&__ip_vs_app_mutex);
184
185         result = ip_vs_app_inc_new(ipvs, app, proto, port);
186
187         mutex_unlock(&__ip_vs_app_mutex);
188
189         return result;
190 }
191
192
193 /* Register application for netns */
194 struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
195 {
196         struct ip_vs_app *a;
197         int err = 0;
198
199         mutex_lock(&__ip_vs_app_mutex);
200
201         /* increase the module use count */
202         if (!ip_vs_use_count_inc()) {
203                 err = -ENOENT;
204                 goto out_unlock;
205         }
206
207         list_for_each_entry(a, &ipvs->app_list, a_list) {
208                 if (!strcmp(app->name, a->name)) {
209                         err = -EEXIST;
210                         /* decrease the module use count */
211                         ip_vs_use_count_dec();
212                         goto out_unlock;
213                 }
214         }
215         a = kmemdup(app, sizeof(*app), GFP_KERNEL);
216         if (!a) {
217                 err = -ENOMEM;
218                 /* decrease the module use count */
219                 ip_vs_use_count_dec();
220                 goto out_unlock;
221         }
222         INIT_LIST_HEAD(&a->incs_list);
223         list_add(&a->a_list, &ipvs->app_list);
224
225 out_unlock:
226         mutex_unlock(&__ip_vs_app_mutex);
227
228         return err ? ERR_PTR(err) : a;
229 }
230
231
232 /*
233  *      ip_vs_app unregistration routine
234  *      We are sure there are no app incarnations attached to services
235  *      Caller should use synchronize_rcu() or rcu_barrier()
236  */
237 void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
238 {
239         struct ip_vs_app *a, *anxt, *inc, *nxt;
240
241         mutex_lock(&__ip_vs_app_mutex);
242
243         list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) {
244                 if (app && strcmp(app->name, a->name))
245                         continue;
246                 list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) {
247                         ip_vs_app_inc_release(ipvs, inc);
248                 }
249
250                 list_del(&a->a_list);
251                 kfree(a);
252
253                 /* decrease the module use count */
254                 ip_vs_use_count_dec();
255         }
256
257         mutex_unlock(&__ip_vs_app_mutex);
258 }
259
260
261 /*
262  *      Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
263  */
264 int ip_vs_bind_app(struct ip_vs_conn *cp,
265                    struct ip_vs_protocol *pp)
266 {
267         return pp->app_conn_bind(cp);
268 }
269
270
271 /*
272  *      Unbind cp from application incarnation (called by cp destructor)
273  */
274 void ip_vs_unbind_app(struct ip_vs_conn *cp)
275 {
276         struct ip_vs_app *inc = cp->app;
277
278         if (!inc)
279                 return;
280
281         if (inc->unbind_conn)
282                 inc->unbind_conn(inc, cp);
283         if (inc->done_conn)
284                 inc->done_conn(inc, cp);
285         ip_vs_app_inc_put(inc);
286         cp->app = NULL;
287 }
288
289
290 /*
291  *      Fixes th->seq based on ip_vs_seq info.
292  */
293 static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
294 {
295         __u32 seq = ntohl(th->seq);
296
297         /*
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.
301          */
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);
307                 } else {
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);
311                 }
312         }
313 }
314
315
316 /*
317  *      Fixes th->ack_seq based on ip_vs_seq info.
318  */
319 static inline void
320 vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
321 {
322         __u32 ack_seq = ntohl(th->ack_seq);
323
324         /*
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
328          */
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);
336
337                 } else {
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);
342                 }
343         }
344 }
345
346
347 /*
348  *      Updates ip_vs_seq if pkt has been resized
349  *      Assumes already checked proto==IPPROTO_TCP and diff!=0.
350  */
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)
353 {
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;
358                 vseq->delta += diff;
359                 vseq->init_seq = seq;
360                 cp->flags |= flag;
361         }
362         spin_unlock_bh(&cp->lock);
363 }
364
365 static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
366                                   struct ip_vs_app *app,
367                                   struct ip_vs_iphdr *ipvsh)
368 {
369         int diff;
370         const unsigned int tcp_offset = ip_hdrlen(skb);
371         struct tcphdr *th;
372         __u32 seq;
373
374         if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
375                 return 0;
376
377         th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
378
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
398         if (!app->pkt_out(app, cp, skb, &diff, ipvsh))
399                 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  */
416 int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
417                       struct ip_vs_iphdr *ipvsh)
418 {
419         struct ip_vs_app *app;
420
421         /*
422          *      check if application module is bound to
423          *      this ip_vs_conn.
424          */
425         if ((app = cp->app) == NULL)
426                 return 1;
427
428         /* TCP is complicated */
429         if (cp->protocol == IPPROTO_TCP)
430                 return app_tcp_pkt_out(cp, skb, app, ipvsh);
431
432         /*
433          *      Call private output hook function
434          */
435         if (app->pkt_out == NULL)
436                 return 1;
437
438         return app->pkt_out(app, cp, skb, NULL, ipvsh);
439 }
440
441
442 static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
443                                  struct ip_vs_app *app,
444                                  struct ip_vs_iphdr *ipvsh)
445 {
446         int diff;
447         const unsigned int tcp_offset = ip_hdrlen(skb);
448         struct tcphdr *th;
449         __u32 seq;
450
451         if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
452                 return 0;
453
454         th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
455
456         /*
457          *      Remember seq number in case this pkt gets resized
458          */
459         seq = ntohl(th->seq);
460
461         /*
462          *      Fix seq stuff if flagged as so.
463          */
464         if (cp->flags & IP_VS_CONN_F_IN_SEQ)
465                 vs_fix_seq(&cp->in_seq, th);
466         if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
467                 vs_fix_ack_seq(&cp->out_seq, th);
468
469         /*
470          *      Call private input hook function
471          */
472         if (app->pkt_in == NULL)
473                 return 1;
474
475         if (!app->pkt_in(app, cp, skb, &diff, ipvsh))
476                 return 0;
477
478         /*
479          *      Update ip_vs seq stuff if len has changed.
480          */
481         if (diff != 0)
482                 vs_seq_update(cp, &cp->in_seq,
483                               IP_VS_CONN_F_IN_SEQ, seq, diff);
484
485         return 1;
486 }
487
488 /*
489  *      Input pkt hook. Will call bound ip_vs_app specific function
490  *      called by ipvs packet handler, assumes previously checked cp!=NULL.
491  *      returns false if can't handle packet (oom).
492  */
493 int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
494                      struct ip_vs_iphdr *ipvsh)
495 {
496         struct ip_vs_app *app;
497
498         /*
499          *      check if application module is bound to
500          *      this ip_vs_conn.
501          */
502         if ((app = cp->app) == NULL)
503                 return 1;
504
505         /* TCP is complicated */
506         if (cp->protocol == IPPROTO_TCP)
507                 return app_tcp_pkt_in(cp, skb, app, ipvsh);
508
509         /*
510          *      Call private input hook function
511          */
512         if (app->pkt_in == NULL)
513                 return 1;
514
515         return app->pkt_in(app, cp, skb, NULL, ipvsh);
516 }
517
518
519 #ifdef CONFIG_PROC_FS
520 /*
521  *      /proc/net/ip_vs_app entry function
522  */
523
524 static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos)
525 {
526         struct ip_vs_app *app, *inc;
527
528         list_for_each_entry(app, &ipvs->app_list, a_list) {
529                 list_for_each_entry(inc, &app->incs_list, a_list) {
530                         if (pos-- == 0)
531                                 return inc;
532                 }
533         }
534         return NULL;
535
536 }
537
538 static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
539 {
540         struct net *net = seq_file_net(seq);
541         struct netns_ipvs *ipvs = net_ipvs(net);
542
543         mutex_lock(&__ip_vs_app_mutex);
544
545         return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN;
546 }
547
548 static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
549 {
550         struct ip_vs_app *inc, *app;
551         struct list_head *e;
552         struct net *net = seq_file_net(seq);
553         struct netns_ipvs *ipvs = net_ipvs(net);
554
555         ++*pos;
556         if (v == SEQ_START_TOKEN)
557                 return ip_vs_app_idx(ipvs, 0);
558
559         inc = v;
560         app = inc->app;
561
562         if ((e = inc->a_list.next) != &app->incs_list)
563                 return list_entry(e, struct ip_vs_app, a_list);
564
565         /* go on to next application */
566         for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) {
567                 app = list_entry(e, struct ip_vs_app, a_list);
568                 list_for_each_entry(inc, &app->incs_list, a_list) {
569                         return inc;
570                 }
571         }
572         return NULL;
573 }
574
575 static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
576 {
577         mutex_unlock(&__ip_vs_app_mutex);
578 }
579
580 static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
581 {
582         if (v == SEQ_START_TOKEN)
583                 seq_puts(seq, "prot port    usecnt name\n");
584         else {
585                 const struct ip_vs_app *inc = v;
586
587                 seq_printf(seq, "%-3s  %-7u %-6d %-17s\n",
588                            ip_vs_proto_name(inc->protocol),
589                            ntohs(inc->port),
590                            atomic_read(&inc->usecnt),
591                            inc->name);
592         }
593         return 0;
594 }
595
596 static const struct seq_operations ip_vs_app_seq_ops = {
597         .start = ip_vs_app_seq_start,
598         .next  = ip_vs_app_seq_next,
599         .stop  = ip_vs_app_seq_stop,
600         .show  = ip_vs_app_seq_show,
601 };
602 #endif
603
604 int __net_init ip_vs_app_net_init(struct netns_ipvs *ipvs)
605 {
606         INIT_LIST_HEAD(&ipvs->app_list);
607 #ifdef CONFIG_PROC_FS
608         if (!proc_create_net("ip_vs_app", 0, ipvs->net->proc_net,
609                              &ip_vs_app_seq_ops,
610                              sizeof(struct seq_net_private)))
611                 return -ENOMEM;
612 #endif
613         return 0;
614 }
615
616 void __net_exit ip_vs_app_net_cleanup(struct netns_ipvs *ipvs)
617 {
618         unregister_ip_vs_app(ipvs, NULL /* all */);
619 #ifdef CONFIG_PROC_FS
620         remove_proc_entry("ip_vs_app", ipvs->net->proc_net);
621 #endif
622 }