1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define KMSG_COMPONENT "IPVS"
17#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/types.h>
22#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/sysctl.h>
25#include <linux/proc_fs.h>
26#include <linux/workqueue.h>
27#include <linux/seq_file.h>
28#include <linux/slab.h>
29
30#include <linux/netfilter.h>
31#include <linux/netfilter_ipv4.h>
32#include <linux/mutex.h>
33
34#include <net/net_namespace.h>
35#include <linux/nsproxy.h>
36#include <net/ip.h>
37#ifdef CONFIG_IP_VS_IPV6
38#include <net/ipv6.h>
39#include <net/ip6_route.h>
40#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
41#endif
42#include <net/route.h>
43#include <net/sock.h>
44#include <net/genetlink.h>
45
46#include <linux/uaccess.h>
47
48#include <net/ip_vs.h>
49
50MODULE_ALIAS_GENL_FAMILY(IPVS_GENL_NAME);
51
52
53static DEFINE_MUTEX(__ip_vs_mutex);
54
55
56
57#ifdef CONFIG_IP_VS_DEBUG
58static int sysctl_ip_vs_debug_level = 0;
59
60int ip_vs_get_debug_level(void)
61{
62 return sysctl_ip_vs_debug_level;
63}
64#endif
65
66
67
68static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
69
70
71#ifdef CONFIG_IP_VS_IPV6
72
73static bool __ip_vs_addr_is_local_v6(struct net *net,
74 const struct in6_addr *addr)
75{
76 struct flowi6 fl6 = {
77 .daddr = *addr,
78 };
79 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
80 bool is_local;
81
82 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
83
84 dst_release(dst);
85 return is_local;
86}
87#endif
88
89#ifdef CONFIG_SYSCTL
90
91
92
93
94static void update_defense_level(struct netns_ipvs *ipvs)
95{
96 struct sysinfo i;
97 int availmem;
98 int nomem;
99 int to_change = -1;
100
101
102 si_meminfo(&i);
103 availmem = i.freeram + i.bufferram;
104
105
106
107
108
109 nomem = (availmem < ipvs->sysctl_amemthresh);
110
111 local_bh_disable();
112
113
114 spin_lock(&ipvs->dropentry_lock);
115 switch (ipvs->sysctl_drop_entry) {
116 case 0:
117 atomic_set(&ipvs->dropentry, 0);
118 break;
119 case 1:
120 if (nomem) {
121 atomic_set(&ipvs->dropentry, 1);
122 ipvs->sysctl_drop_entry = 2;
123 } else {
124 atomic_set(&ipvs->dropentry, 0);
125 }
126 break;
127 case 2:
128 if (nomem) {
129 atomic_set(&ipvs->dropentry, 1);
130 } else {
131 atomic_set(&ipvs->dropentry, 0);
132 ipvs->sysctl_drop_entry = 1;
133 }
134 break;
135 case 3:
136 atomic_set(&ipvs->dropentry, 1);
137 break;
138 }
139 spin_unlock(&ipvs->dropentry_lock);
140
141
142 spin_lock(&ipvs->droppacket_lock);
143 switch (ipvs->sysctl_drop_packet) {
144 case 0:
145 ipvs->drop_rate = 0;
146 break;
147 case 1:
148 if (nomem) {
149 ipvs->drop_rate = ipvs->drop_counter
150 = ipvs->sysctl_amemthresh /
151 (ipvs->sysctl_amemthresh-availmem);
152 ipvs->sysctl_drop_packet = 2;
153 } else {
154 ipvs->drop_rate = 0;
155 }
156 break;
157 case 2:
158 if (nomem) {
159 ipvs->drop_rate = ipvs->drop_counter
160 = ipvs->sysctl_amemthresh /
161 (ipvs->sysctl_amemthresh-availmem);
162 } else {
163 ipvs->drop_rate = 0;
164 ipvs->sysctl_drop_packet = 1;
165 }
166 break;
167 case 3:
168 ipvs->drop_rate = ipvs->sysctl_am_droprate;
169 break;
170 }
171 spin_unlock(&ipvs->droppacket_lock);
172
173
174 spin_lock(&ipvs->securetcp_lock);
175 switch (ipvs->sysctl_secure_tcp) {
176 case 0:
177 if (ipvs->old_secure_tcp >= 2)
178 to_change = 0;
179 break;
180 case 1:
181 if (nomem) {
182 if (ipvs->old_secure_tcp < 2)
183 to_change = 1;
184 ipvs->sysctl_secure_tcp = 2;
185 } else {
186 if (ipvs->old_secure_tcp >= 2)
187 to_change = 0;
188 }
189 break;
190 case 2:
191 if (nomem) {
192 if (ipvs->old_secure_tcp < 2)
193 to_change = 1;
194 } else {
195 if (ipvs->old_secure_tcp >= 2)
196 to_change = 0;
197 ipvs->sysctl_secure_tcp = 1;
198 }
199 break;
200 case 3:
201 if (ipvs->old_secure_tcp < 2)
202 to_change = 1;
203 break;
204 }
205 ipvs->old_secure_tcp = ipvs->sysctl_secure_tcp;
206 if (to_change >= 0)
207 ip_vs_protocol_timeout_change(ipvs,
208 ipvs->sysctl_secure_tcp > 1);
209 spin_unlock(&ipvs->securetcp_lock);
210
211 local_bh_enable();
212}
213
214
215
216
217static void expire_nodest_conn_handler(struct work_struct *work)
218{
219 struct netns_ipvs *ipvs;
220
221 ipvs = container_of(work, struct netns_ipvs,
222 expire_nodest_conn_work.work);
223 ip_vs_expire_nodest_conn_flush(ipvs);
224}
225
226
227
228
229#define DEFENSE_TIMER_PERIOD 1*HZ
230
231static void defense_work_handler(struct work_struct *work)
232{
233 struct netns_ipvs *ipvs =
234 container_of(work, struct netns_ipvs, defense_work.work);
235
236 update_defense_level(ipvs);
237 if (atomic_read(&ipvs->dropentry))
238 ip_vs_random_dropentry(ipvs);
239 queue_delayed_work(system_long_wq, &ipvs->defense_work,
240 DEFENSE_TIMER_PERIOD);
241}
242#endif
243
244int
245ip_vs_use_count_inc(void)
246{
247 return try_module_get(THIS_MODULE);
248}
249
250void
251ip_vs_use_count_dec(void)
252{
253 module_put(THIS_MODULE);
254}
255
256
257
258
259
260#define IP_VS_SVC_TAB_BITS 8
261#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
262#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
263
264
265static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
266
267static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
268
269
270
271
272
273static inline unsigned int
274ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
275 const union nf_inet_addr *addr, __be16 port)
276{
277 unsigned int porth = ntohs(port);
278 __be32 addr_fold = addr->ip;
279 __u32 ahash;
280
281#ifdef CONFIG_IP_VS_IPV6
282 if (af == AF_INET6)
283 addr_fold = addr->ip6[0]^addr->ip6[1]^
284 addr->ip6[2]^addr->ip6[3];
285#endif
286 ahash = ntohl(addr_fold);
287 ahash ^= ((size_t) ipvs >> 8);
288
289 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
290 IP_VS_SVC_TAB_MASK;
291}
292
293
294
295
296static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
297{
298 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
299}
300
301
302
303
304
305
306static int ip_vs_svc_hash(struct ip_vs_service *svc)
307{
308 unsigned int hash;
309
310 if (svc->flags & IP_VS_SVC_F_HASHED) {
311 pr_err("%s(): request for already hashed, called from %pS\n",
312 __func__, __builtin_return_address(0));
313 return 0;
314 }
315
316 if (svc->fwmark == 0) {
317
318
319
320 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
321 &svc->addr, svc->port);
322 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
323 } else {
324
325
326
327 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
328 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
329 }
330
331 svc->flags |= IP_VS_SVC_F_HASHED;
332
333 atomic_inc(&svc->refcnt);
334 return 1;
335}
336
337
338
339
340
341
342static int ip_vs_svc_unhash(struct ip_vs_service *svc)
343{
344 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
345 pr_err("%s(): request for unhash flagged, called from %pS\n",
346 __func__, __builtin_return_address(0));
347 return 0;
348 }
349
350 if (svc->fwmark == 0) {
351
352 hlist_del_rcu(&svc->s_list);
353 } else {
354
355 hlist_del_rcu(&svc->f_list);
356 }
357
358 svc->flags &= ~IP_VS_SVC_F_HASHED;
359 atomic_dec(&svc->refcnt);
360 return 1;
361}
362
363
364
365
366
367static inline struct ip_vs_service *
368__ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
369 const union nf_inet_addr *vaddr, __be16 vport)
370{
371 unsigned int hash;
372 struct ip_vs_service *svc;
373
374
375 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
376
377 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
378 if ((svc->af == af)
379 && ip_vs_addr_equal(af, &svc->addr, vaddr)
380 && (svc->port == vport)
381 && (svc->protocol == protocol)
382 && (svc->ipvs == ipvs)) {
383
384 return svc;
385 }
386 }
387
388 return NULL;
389}
390
391
392
393
394
395static inline struct ip_vs_service *
396__ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
397{
398 unsigned int hash;
399 struct ip_vs_service *svc;
400
401
402 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
403
404 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
405 if (svc->fwmark == fwmark && svc->af == af
406 && (svc->ipvs == ipvs)) {
407
408 return svc;
409 }
410 }
411
412 return NULL;
413}
414
415
416struct ip_vs_service *
417ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
418 const union nf_inet_addr *vaddr, __be16 vport)
419{
420 struct ip_vs_service *svc;
421
422
423
424
425 if (fwmark) {
426 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
427 if (svc)
428 goto out;
429 }
430
431
432
433
434
435 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
436
437 if (!svc && protocol == IPPROTO_TCP &&
438 atomic_read(&ipvs->ftpsvc_counter) &&
439 (vport == FTPDATA || !inet_port_requires_bind_service(ipvs->net, ntohs(vport)))) {
440
441
442
443
444 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
445 }
446
447 if (svc == NULL
448 && atomic_read(&ipvs->nullsvc_counter)) {
449
450
451
452 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
453 }
454
455 out:
456 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
457 fwmark, ip_vs_proto_name(protocol),
458 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
459 svc ? "hit" : "not hit");
460
461 return svc;
462}
463
464
465static inline void
466__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
467{
468 atomic_inc(&svc->refcnt);
469 rcu_assign_pointer(dest->svc, svc);
470}
471
472static void ip_vs_service_free(struct ip_vs_service *svc)
473{
474 free_percpu(svc->stats.cpustats);
475 kfree(svc);
476}
477
478static void ip_vs_service_rcu_free(struct rcu_head *head)
479{
480 struct ip_vs_service *svc;
481
482 svc = container_of(head, struct ip_vs_service, rcu_head);
483 ip_vs_service_free(svc);
484}
485
486static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
487{
488 if (atomic_dec_and_test(&svc->refcnt)) {
489 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
490 svc->fwmark,
491 IP_VS_DBG_ADDR(svc->af, &svc->addr),
492 ntohs(svc->port));
493 if (do_delay)
494 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
495 else
496 ip_vs_service_free(svc);
497 }
498}
499
500
501
502
503
504static inline unsigned int ip_vs_rs_hashkey(int af,
505 const union nf_inet_addr *addr,
506 __be16 port)
507{
508 unsigned int porth = ntohs(port);
509 __be32 addr_fold = addr->ip;
510
511#ifdef CONFIG_IP_VS_IPV6
512 if (af == AF_INET6)
513 addr_fold = addr->ip6[0]^addr->ip6[1]^
514 addr->ip6[2]^addr->ip6[3];
515#endif
516
517 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
518 & IP_VS_RTAB_MASK;
519}
520
521
522static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
523{
524 unsigned int hash;
525 __be16 port;
526
527 if (dest->in_rs_table)
528 return;
529
530 switch (IP_VS_DFWD_METHOD(dest)) {
531 case IP_VS_CONN_F_MASQ:
532 port = dest->port;
533 break;
534 case IP_VS_CONN_F_TUNNEL:
535 switch (dest->tun_type) {
536 case IP_VS_CONN_F_TUNNEL_TYPE_GUE:
537 port = dest->tun_port;
538 break;
539 case IP_VS_CONN_F_TUNNEL_TYPE_IPIP:
540 case IP_VS_CONN_F_TUNNEL_TYPE_GRE:
541 port = 0;
542 break;
543 default:
544 return;
545 }
546 break;
547 default:
548 return;
549 }
550
551
552
553
554
555 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, port);
556
557 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
558 dest->in_rs_table = 1;
559}
560
561
562static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
563{
564
565
566
567 if (dest->in_rs_table) {
568 hlist_del_rcu(&dest->d_list);
569 dest->in_rs_table = 0;
570 }
571}
572
573
574bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
575 const union nf_inet_addr *daddr, __be16 dport)
576{
577 unsigned int hash;
578 struct ip_vs_dest *dest;
579
580
581 hash = ip_vs_rs_hashkey(af, daddr, dport);
582
583 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
584 if (dest->port == dport &&
585 dest->af == af &&
586 ip_vs_addr_equal(af, &dest->addr, daddr) &&
587 (dest->protocol == protocol || dest->vfwmark) &&
588 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
589
590 return true;
591 }
592 }
593
594 return false;
595}
596
597
598
599
600
601
602
603struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
604 __u16 protocol,
605 const union nf_inet_addr *daddr,
606 __be16 dport)
607{
608 unsigned int hash;
609 struct ip_vs_dest *dest;
610
611
612 hash = ip_vs_rs_hashkey(af, daddr, dport);
613
614 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
615 if (dest->port == dport &&
616 dest->af == af &&
617 ip_vs_addr_equal(af, &dest->addr, daddr) &&
618 (dest->protocol == protocol || dest->vfwmark) &&
619 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
620
621 return dest;
622 }
623 }
624
625 return NULL;
626}
627
628
629
630
631
632
633
634struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
635 const union nf_inet_addr *daddr,
636 __be16 tun_port)
637{
638 struct ip_vs_dest *dest;
639 unsigned int hash;
640
641
642 hash = ip_vs_rs_hashkey(af, daddr, tun_port);
643
644 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
645 if (dest->tun_port == tun_port &&
646 dest->af == af &&
647 ip_vs_addr_equal(af, &dest->addr, daddr) &&
648 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_TUNNEL) {
649
650 return dest;
651 }
652 }
653
654 return NULL;
655}
656
657
658
659
660static struct ip_vs_dest *
661ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
662 const union nf_inet_addr *daddr, __be16 dport)
663{
664 struct ip_vs_dest *dest;
665
666
667
668
669 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
670 if ((dest->af == dest_af) &&
671 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
672 (dest->port == dport)) {
673
674 return dest;
675 }
676 }
677
678 return NULL;
679}
680
681
682
683
684
685
686
687
688
689struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
690 const union nf_inet_addr *daddr,
691 __be16 dport,
692 const union nf_inet_addr *vaddr,
693 __be16 vport, __u16 protocol, __u32 fwmark,
694 __u32 flags)
695{
696 struct ip_vs_dest *dest;
697 struct ip_vs_service *svc;
698 __be16 port = dport;
699
700 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
701 if (!svc)
702 return NULL;
703 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
704 port = 0;
705 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
706 if (!dest)
707 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
708 return dest;
709}
710
711void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
712{
713 struct ip_vs_dest_dst *dest_dst = container_of(head,
714 struct ip_vs_dest_dst,
715 rcu_head);
716
717 dst_release(dest_dst->dst_cache);
718 kfree(dest_dst);
719}
720
721
722static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
723{
724 struct ip_vs_dest_dst *old;
725
726 old = rcu_dereference_protected(dest->dest_dst, 1);
727 if (old) {
728 RCU_INIT_POINTER(dest->dest_dst, NULL);
729 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
730 }
731}
732
733
734
735
736
737
738
739
740
741
742
743static struct ip_vs_dest *
744ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
745 const union nf_inet_addr *daddr, __be16 dport)
746{
747 struct ip_vs_dest *dest;
748 struct netns_ipvs *ipvs = svc->ipvs;
749
750
751
752
753 spin_lock_bh(&ipvs->dest_trash_lock);
754 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
755 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
756 "dest->refcnt=%d\n",
757 dest->vfwmark,
758 IP_VS_DBG_ADDR(dest->af, &dest->addr),
759 ntohs(dest->port),
760 refcount_read(&dest->refcnt));
761 if (dest->af == dest_af &&
762 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
763 dest->port == dport &&
764 dest->vfwmark == svc->fwmark &&
765 dest->protocol == svc->protocol &&
766 (svc->fwmark ||
767 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
768 dest->vport == svc->port))) {
769
770 list_del(&dest->t_list);
771 goto out;
772 }
773 }
774
775 dest = NULL;
776
777out:
778 spin_unlock_bh(&ipvs->dest_trash_lock);
779
780 return dest;
781}
782
783static void ip_vs_dest_free(struct ip_vs_dest *dest)
784{
785 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
786
787 __ip_vs_dst_cache_reset(dest);
788 __ip_vs_svc_put(svc, false);
789 free_percpu(dest->stats.cpustats);
790 ip_vs_dest_put_and_free(dest);
791}
792
793
794
795
796
797
798
799
800
801
802static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
803{
804 struct ip_vs_dest *dest, *nxt;
805
806 del_timer_sync(&ipvs->dest_trash_timer);
807
808 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
809 list_del(&dest->t_list);
810 ip_vs_dest_free(dest);
811 }
812}
813
814static void
815ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
816{
817#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
818
819 spin_lock_bh(&src->lock);
820
821 IP_VS_SHOW_STATS_COUNTER(conns);
822 IP_VS_SHOW_STATS_COUNTER(inpkts);
823 IP_VS_SHOW_STATS_COUNTER(outpkts);
824 IP_VS_SHOW_STATS_COUNTER(inbytes);
825 IP_VS_SHOW_STATS_COUNTER(outbytes);
826
827 ip_vs_read_estimator(dst, src);
828
829 spin_unlock_bh(&src->lock);
830}
831
832static void
833ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
834{
835 dst->conns = (u32)src->conns;
836 dst->inpkts = (u32)src->inpkts;
837 dst->outpkts = (u32)src->outpkts;
838 dst->inbytes = src->inbytes;
839 dst->outbytes = src->outbytes;
840 dst->cps = (u32)src->cps;
841 dst->inpps = (u32)src->inpps;
842 dst->outpps = (u32)src->outpps;
843 dst->inbps = (u32)src->inbps;
844 dst->outbps = (u32)src->outbps;
845}
846
847static void
848ip_vs_zero_stats(struct ip_vs_stats *stats)
849{
850 spin_lock_bh(&stats->lock);
851
852
853
854#define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
855
856 IP_VS_ZERO_STATS_COUNTER(conns);
857 IP_VS_ZERO_STATS_COUNTER(inpkts);
858 IP_VS_ZERO_STATS_COUNTER(outpkts);
859 IP_VS_ZERO_STATS_COUNTER(inbytes);
860 IP_VS_ZERO_STATS_COUNTER(outbytes);
861
862 ip_vs_zero_estimator(stats);
863
864 spin_unlock_bh(&stats->lock);
865}
866
867
868
869
870static void
871__ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
872 struct ip_vs_dest_user_kern *udest, int add)
873{
874 struct netns_ipvs *ipvs = svc->ipvs;
875 struct ip_vs_service *old_svc;
876 struct ip_vs_scheduler *sched;
877 int conn_flags;
878
879
880 BUG_ON(!add && udest->af != dest->af);
881
882 if (add && udest->af != svc->af)
883 ipvs->mixed_address_family_dests++;
884
885
886 if (add || udest->weight != 0)
887 atomic_set(&dest->last_weight, udest->weight);
888
889
890 atomic_set(&dest->weight, udest->weight);
891 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
892 conn_flags |= IP_VS_CONN_F_INACTIVE;
893
894
895 if ((udest->conn_flags & IP_VS_CONN_F_FWD_MASK) !=
896 IP_VS_DFWD_METHOD(dest) ||
897 udest->tun_type != dest->tun_type ||
898 udest->tun_port != dest->tun_port)
899 ip_vs_rs_unhash(dest);
900
901
902 dest->tun_type = udest->tun_type;
903 dest->tun_port = udest->tun_port;
904 dest->tun_flags = udest->tun_flags;
905
906
907 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
908 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
909 } else {
910
911 if (svc->port == FTPPORT)
912 ip_vs_register_conntrack(svc);
913 }
914 atomic_set(&dest->conn_flags, conn_flags);
915
916 ip_vs_rs_hash(ipvs, dest);
917
918
919 old_svc = rcu_dereference_protected(dest->svc, 1);
920 if (!old_svc) {
921 __ip_vs_bind_svc(dest, svc);
922 } else {
923 if (old_svc != svc) {
924 ip_vs_zero_stats(&dest->stats);
925 __ip_vs_bind_svc(dest, svc);
926 __ip_vs_svc_put(old_svc, true);
927 }
928 }
929
930
931 dest->flags |= IP_VS_DEST_F_AVAILABLE;
932
933 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
934 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
935 dest->u_threshold = udest->u_threshold;
936 dest->l_threshold = udest->l_threshold;
937
938 dest->af = udest->af;
939
940 spin_lock_bh(&dest->dst_lock);
941 __ip_vs_dst_cache_reset(dest);
942 spin_unlock_bh(&dest->dst_lock);
943
944 if (add) {
945 ip_vs_start_estimator(svc->ipvs, &dest->stats);
946 list_add_rcu(&dest->n_list, &svc->destinations);
947 svc->num_dests++;
948 sched = rcu_dereference_protected(svc->scheduler, 1);
949 if (sched && sched->add_dest)
950 sched->add_dest(svc, dest);
951 } else {
952 sched = rcu_dereference_protected(svc->scheduler, 1);
953 if (sched && sched->upd_dest)
954 sched->upd_dest(svc, dest);
955 }
956}
957
958
959
960
961
962static int
963ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
964 struct ip_vs_dest **dest_p)
965{
966 struct ip_vs_dest *dest;
967 unsigned int atype, i;
968
969 EnterFunction(2);
970
971#ifdef CONFIG_IP_VS_IPV6
972 if (udest->af == AF_INET6) {
973 int ret;
974
975 atype = ipv6_addr_type(&udest->addr.in6);
976 if ((!(atype & IPV6_ADDR_UNICAST) ||
977 atype & IPV6_ADDR_LINKLOCAL) &&
978 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
979 return -EINVAL;
980
981 ret = nf_defrag_ipv6_enable(svc->ipvs->net);
982 if (ret)
983 return ret;
984 } else
985#endif
986 {
987 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
988 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
989 return -EINVAL;
990 }
991
992 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
993 if (dest == NULL)
994 return -ENOMEM;
995
996 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
997 if (!dest->stats.cpustats)
998 goto err_alloc;
999
1000 for_each_possible_cpu(i) {
1001 struct ip_vs_cpu_stats *ip_vs_dest_stats;
1002 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
1003 u64_stats_init(&ip_vs_dest_stats->syncp);
1004 }
1005
1006 dest->af = udest->af;
1007 dest->protocol = svc->protocol;
1008 dest->vaddr = svc->addr;
1009 dest->vport = svc->port;
1010 dest->vfwmark = svc->fwmark;
1011 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
1012 dest->port = udest->port;
1013
1014 atomic_set(&dest->activeconns, 0);
1015 atomic_set(&dest->inactconns, 0);
1016 atomic_set(&dest->persistconns, 0);
1017 refcount_set(&dest->refcnt, 1);
1018
1019 INIT_HLIST_NODE(&dest->d_list);
1020 spin_lock_init(&dest->dst_lock);
1021 spin_lock_init(&dest->stats.lock);
1022 __ip_vs_update_dest(svc, dest, udest, 1);
1023
1024 *dest_p = dest;
1025
1026 LeaveFunction(2);
1027 return 0;
1028
1029err_alloc:
1030 kfree(dest);
1031 return -ENOMEM;
1032}
1033
1034
1035
1036
1037
1038static int
1039ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1040{
1041 struct ip_vs_dest *dest;
1042 union nf_inet_addr daddr;
1043 __be16 dport = udest->port;
1044 int ret;
1045
1046 EnterFunction(2);
1047
1048 if (udest->weight < 0) {
1049 pr_err("%s(): server weight less than zero\n", __func__);
1050 return -ERANGE;
1051 }
1052
1053 if (udest->l_threshold > udest->u_threshold) {
1054 pr_err("%s(): lower threshold is higher than upper threshold\n",
1055 __func__);
1056 return -ERANGE;
1057 }
1058
1059 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1060 if (udest->tun_port == 0) {
1061 pr_err("%s(): tunnel port is zero\n", __func__);
1062 return -EINVAL;
1063 }
1064 }
1065
1066 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1067
1068
1069 rcu_read_lock();
1070 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1071 rcu_read_unlock();
1072
1073 if (dest != NULL) {
1074 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
1075 return -EEXIST;
1076 }
1077
1078
1079
1080
1081
1082 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
1083
1084 if (dest != NULL) {
1085 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1086 "dest->refcnt=%d, service %u/%s:%u\n",
1087 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
1088 refcount_read(&dest->refcnt),
1089 dest->vfwmark,
1090 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1091 ntohs(dest->vport));
1092
1093 __ip_vs_update_dest(svc, dest, udest, 1);
1094 ret = 0;
1095 } else {
1096
1097
1098
1099 ret = ip_vs_new_dest(svc, udest, &dest);
1100 }
1101 LeaveFunction(2);
1102
1103 return ret;
1104}
1105
1106
1107
1108
1109
1110static int
1111ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1112{
1113 struct ip_vs_dest *dest;
1114 union nf_inet_addr daddr;
1115 __be16 dport = udest->port;
1116
1117 EnterFunction(2);
1118
1119 if (udest->weight < 0) {
1120 pr_err("%s(): server weight less than zero\n", __func__);
1121 return -ERANGE;
1122 }
1123
1124 if (udest->l_threshold > udest->u_threshold) {
1125 pr_err("%s(): lower threshold is higher than upper threshold\n",
1126 __func__);
1127 return -ERANGE;
1128 }
1129
1130 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1131 if (udest->tun_port == 0) {
1132 pr_err("%s(): tunnel port is zero\n", __func__);
1133 return -EINVAL;
1134 }
1135 }
1136
1137 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1138
1139
1140 rcu_read_lock();
1141 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1142 rcu_read_unlock();
1143
1144 if (dest == NULL) {
1145 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1146 return -ENOENT;
1147 }
1148
1149 __ip_vs_update_dest(svc, dest, udest, 0);
1150 LeaveFunction(2);
1151
1152 return 0;
1153}
1154
1155
1156
1157
1158static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1159 bool cleanup)
1160{
1161 ip_vs_stop_estimator(ipvs, &dest->stats);
1162
1163
1164
1165
1166 ip_vs_rs_unhash(dest);
1167
1168 spin_lock_bh(&ipvs->dest_trash_lock);
1169 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1170 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1171 refcount_read(&dest->refcnt));
1172 if (list_empty(&ipvs->dest_trash) && !cleanup)
1173 mod_timer(&ipvs->dest_trash_timer,
1174 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1175
1176 list_add(&dest->t_list, &ipvs->dest_trash);
1177 dest->idle_start = 0;
1178 spin_unlock_bh(&ipvs->dest_trash_lock);
1179
1180
1181
1182
1183 if (!cleanup)
1184 ip_vs_enqueue_expire_nodest_conns(ipvs);
1185}
1186
1187
1188
1189
1190
1191static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1192 struct ip_vs_dest *dest,
1193 int svcupd)
1194{
1195 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1196
1197
1198
1199
1200 list_del_rcu(&dest->n_list);
1201 svc->num_dests--;
1202
1203 if (dest->af != svc->af)
1204 svc->ipvs->mixed_address_family_dests--;
1205
1206 if (svcupd) {
1207 struct ip_vs_scheduler *sched;
1208
1209 sched = rcu_dereference_protected(svc->scheduler, 1);
1210 if (sched && sched->del_dest)
1211 sched->del_dest(svc, dest);
1212 }
1213}
1214
1215
1216
1217
1218
1219static int
1220ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1221{
1222 struct ip_vs_dest *dest;
1223 __be16 dport = udest->port;
1224
1225 EnterFunction(2);
1226
1227
1228 rcu_read_lock();
1229 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1230 rcu_read_unlock();
1231
1232 if (dest == NULL) {
1233 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1234 return -ENOENT;
1235 }
1236
1237
1238
1239
1240 __ip_vs_unlink_dest(svc, dest, 1);
1241
1242
1243
1244
1245 __ip_vs_del_dest(svc->ipvs, dest, false);
1246
1247 LeaveFunction(2);
1248
1249 return 0;
1250}
1251
1252static void ip_vs_dest_trash_expire(struct timer_list *t)
1253{
1254 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1255 struct ip_vs_dest *dest, *next;
1256 unsigned long now = jiffies;
1257
1258 spin_lock(&ipvs->dest_trash_lock);
1259 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1260 if (refcount_read(&dest->refcnt) > 1)
1261 continue;
1262 if (dest->idle_start) {
1263 if (time_before(now, dest->idle_start +
1264 IP_VS_DEST_TRASH_PERIOD))
1265 continue;
1266 } else {
1267 dest->idle_start = max(1UL, now);
1268 continue;
1269 }
1270 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1271 dest->vfwmark,
1272 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1273 ntohs(dest->port));
1274 list_del(&dest->t_list);
1275 ip_vs_dest_free(dest);
1276 }
1277 if (!list_empty(&ipvs->dest_trash))
1278 mod_timer(&ipvs->dest_trash_timer,
1279 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1280 spin_unlock(&ipvs->dest_trash_lock);
1281}
1282
1283
1284
1285
1286static int
1287ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1288 struct ip_vs_service **svc_p)
1289{
1290 int ret = 0, i;
1291 struct ip_vs_scheduler *sched = NULL;
1292 struct ip_vs_pe *pe = NULL;
1293 struct ip_vs_service *svc = NULL;
1294 int ret_hooks = -1;
1295
1296
1297 if (!ip_vs_use_count_inc())
1298 return -ENOPROTOOPT;
1299
1300
1301 if (strcmp(u->sched_name, "none")) {
1302 sched = ip_vs_scheduler_get(u->sched_name);
1303 if (!sched) {
1304 pr_info("Scheduler module ip_vs_%s not found\n",
1305 u->sched_name);
1306 ret = -ENOENT;
1307 goto out_err;
1308 }
1309 }
1310
1311 if (u->pe_name && *u->pe_name) {
1312 pe = ip_vs_pe_getbyname(u->pe_name);
1313 if (pe == NULL) {
1314 pr_info("persistence engine module ip_vs_pe_%s "
1315 "not found\n", u->pe_name);
1316 ret = -ENOENT;
1317 goto out_err;
1318 }
1319 }
1320
1321#ifdef CONFIG_IP_VS_IPV6
1322 if (u->af == AF_INET6) {
1323 __u32 plen = (__force __u32) u->netmask;
1324
1325 if (plen < 1 || plen > 128) {
1326 ret = -EINVAL;
1327 goto out_err;
1328 }
1329
1330 ret = nf_defrag_ipv6_enable(ipvs->net);
1331 if (ret)
1332 goto out_err;
1333 }
1334#endif
1335
1336 if ((u->af == AF_INET && !ipvs->num_services) ||
1337 (u->af == AF_INET6 && !ipvs->num_services6)) {
1338 ret = ip_vs_register_hooks(ipvs, u->af);
1339 if (ret < 0)
1340 goto out_err;
1341 ret_hooks = ret;
1342 }
1343
1344 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1345 if (svc == NULL) {
1346 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1347 ret = -ENOMEM;
1348 goto out_err;
1349 }
1350 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1351 if (!svc->stats.cpustats) {
1352 ret = -ENOMEM;
1353 goto out_err;
1354 }
1355
1356 for_each_possible_cpu(i) {
1357 struct ip_vs_cpu_stats *ip_vs_stats;
1358 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1359 u64_stats_init(&ip_vs_stats->syncp);
1360 }
1361
1362
1363
1364 atomic_set(&svc->refcnt, 0);
1365
1366 svc->af = u->af;
1367 svc->protocol = u->protocol;
1368 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1369 svc->port = u->port;
1370 svc->fwmark = u->fwmark;
1371 svc->flags = u->flags & ~IP_VS_SVC_F_HASHED;
1372 svc->timeout = u->timeout * HZ;
1373 svc->netmask = u->netmask;
1374 svc->ipvs = ipvs;
1375
1376 INIT_LIST_HEAD(&svc->destinations);
1377 spin_lock_init(&svc->sched_lock);
1378 spin_lock_init(&svc->stats.lock);
1379
1380
1381 if (sched) {
1382 ret = ip_vs_bind_scheduler(svc, sched);
1383 if (ret)
1384 goto out_err;
1385 sched = NULL;
1386 }
1387
1388
1389 RCU_INIT_POINTER(svc->pe, pe);
1390 pe = NULL;
1391
1392
1393 if (svc->port == FTPPORT)
1394 atomic_inc(&ipvs->ftpsvc_counter);
1395 else if (svc->port == 0)
1396 atomic_inc(&ipvs->nullsvc_counter);
1397 if (svc->pe && svc->pe->conn_out)
1398 atomic_inc(&ipvs->conn_out_counter);
1399
1400 ip_vs_start_estimator(ipvs, &svc->stats);
1401
1402
1403 if (svc->af == AF_INET)
1404 ipvs->num_services++;
1405 else if (svc->af == AF_INET6)
1406 ipvs->num_services6++;
1407
1408
1409 ip_vs_svc_hash(svc);
1410
1411 *svc_p = svc;
1412
1413 ipvs->enable = 1;
1414 return 0;
1415
1416
1417 out_err:
1418 if (ret_hooks >= 0)
1419 ip_vs_unregister_hooks(ipvs, u->af);
1420 if (svc != NULL) {
1421 ip_vs_unbind_scheduler(svc, sched);
1422 ip_vs_service_free(svc);
1423 }
1424 ip_vs_scheduler_put(sched);
1425 ip_vs_pe_put(pe);
1426
1427
1428 ip_vs_use_count_dec();
1429
1430 return ret;
1431}
1432
1433
1434
1435
1436
1437static int
1438ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1439{
1440 struct ip_vs_scheduler *sched = NULL, *old_sched;
1441 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1442 int ret = 0;
1443 bool new_pe_conn_out, old_pe_conn_out;
1444
1445
1446
1447
1448 if (strcmp(u->sched_name, "none")) {
1449 sched = ip_vs_scheduler_get(u->sched_name);
1450 if (!sched) {
1451 pr_info("Scheduler module ip_vs_%s not found\n",
1452 u->sched_name);
1453 return -ENOENT;
1454 }
1455 }
1456 old_sched = sched;
1457
1458 if (u->pe_name && *u->pe_name) {
1459 pe = ip_vs_pe_getbyname(u->pe_name);
1460 if (pe == NULL) {
1461 pr_info("persistence engine module ip_vs_pe_%s "
1462 "not found\n", u->pe_name);
1463 ret = -ENOENT;
1464 goto out;
1465 }
1466 old_pe = pe;
1467 }
1468
1469#ifdef CONFIG_IP_VS_IPV6
1470 if (u->af == AF_INET6) {
1471 __u32 plen = (__force __u32) u->netmask;
1472
1473 if (plen < 1 || plen > 128) {
1474 ret = -EINVAL;
1475 goto out;
1476 }
1477 }
1478#endif
1479
1480 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1481 if (sched != old_sched) {
1482 if (old_sched) {
1483 ip_vs_unbind_scheduler(svc, old_sched);
1484 RCU_INIT_POINTER(svc->scheduler, NULL);
1485
1486 synchronize_rcu();
1487 }
1488
1489 if (sched) {
1490 ret = ip_vs_bind_scheduler(svc, sched);
1491 if (ret) {
1492 ip_vs_scheduler_put(sched);
1493 goto out;
1494 }
1495 }
1496 }
1497
1498
1499
1500
1501 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1502 svc->timeout = u->timeout * HZ;
1503 svc->netmask = u->netmask;
1504
1505 old_pe = rcu_dereference_protected(svc->pe, 1);
1506 if (pe != old_pe) {
1507 rcu_assign_pointer(svc->pe, pe);
1508
1509 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1510 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1511 if (new_pe_conn_out && !old_pe_conn_out)
1512 atomic_inc(&svc->ipvs->conn_out_counter);
1513 if (old_pe_conn_out && !new_pe_conn_out)
1514 atomic_dec(&svc->ipvs->conn_out_counter);
1515 }
1516
1517out:
1518 ip_vs_scheduler_put(old_sched);
1519 ip_vs_pe_put(old_pe);
1520 return ret;
1521}
1522
1523
1524
1525
1526
1527
1528static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1529{
1530 struct ip_vs_dest *dest, *nxt;
1531 struct ip_vs_scheduler *old_sched;
1532 struct ip_vs_pe *old_pe;
1533 struct netns_ipvs *ipvs = svc->ipvs;
1534
1535 if (svc->af == AF_INET) {
1536 ipvs->num_services--;
1537 if (!ipvs->num_services)
1538 ip_vs_unregister_hooks(ipvs, svc->af);
1539 } else if (svc->af == AF_INET6) {
1540 ipvs->num_services6--;
1541 if (!ipvs->num_services6)
1542 ip_vs_unregister_hooks(ipvs, svc->af);
1543 }
1544
1545 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1546
1547
1548 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1549 ip_vs_unbind_scheduler(svc, old_sched);
1550 ip_vs_scheduler_put(old_sched);
1551
1552
1553 old_pe = rcu_dereference_protected(svc->pe, 1);
1554 if (old_pe && old_pe->conn_out)
1555 atomic_dec(&ipvs->conn_out_counter);
1556 ip_vs_pe_put(old_pe);
1557
1558
1559
1560
1561 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1562 __ip_vs_unlink_dest(svc, dest, 0);
1563 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1564 }
1565
1566
1567
1568
1569 if (svc->port == FTPPORT)
1570 atomic_dec(&ipvs->ftpsvc_counter);
1571 else if (svc->port == 0)
1572 atomic_dec(&ipvs->nullsvc_counter);
1573
1574
1575
1576
1577 __ip_vs_svc_put(svc, true);
1578
1579
1580 ip_vs_use_count_dec();
1581}
1582
1583
1584
1585
1586static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1587{
1588 ip_vs_unregister_conntrack(svc);
1589
1590 atomic_inc(&svc->refcnt);
1591
1592
1593
1594 ip_vs_svc_unhash(svc);
1595
1596 __ip_vs_del_service(svc, cleanup);
1597}
1598
1599
1600
1601
1602static int ip_vs_del_service(struct ip_vs_service *svc)
1603{
1604 if (svc == NULL)
1605 return -EEXIST;
1606 ip_vs_unlink_service(svc, false);
1607
1608 return 0;
1609}
1610
1611
1612
1613
1614
1615static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1616{
1617 int idx;
1618 struct ip_vs_service *svc;
1619 struct hlist_node *n;
1620
1621
1622
1623
1624 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1625 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1626 s_list) {
1627 if (svc->ipvs == ipvs)
1628 ip_vs_unlink_service(svc, cleanup);
1629 }
1630 }
1631
1632
1633
1634
1635 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1636 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1637 f_list) {
1638 if (svc->ipvs == ipvs)
1639 ip_vs_unlink_service(svc, cleanup);
1640 }
1641 }
1642
1643 return 0;
1644}
1645
1646
1647
1648
1649
1650void ip_vs_service_nets_cleanup(struct list_head *net_list)
1651{
1652 struct netns_ipvs *ipvs;
1653 struct net *net;
1654
1655 EnterFunction(2);
1656
1657 mutex_lock(&__ip_vs_mutex);
1658 list_for_each_entry(net, net_list, exit_list) {
1659 ipvs = net_ipvs(net);
1660 ip_vs_flush(ipvs, true);
1661 }
1662 mutex_unlock(&__ip_vs_mutex);
1663 LeaveFunction(2);
1664}
1665
1666
1667static inline void
1668ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1669{
1670 struct ip_vs_dest_dst *dest_dst;
1671
1672 spin_lock_bh(&dest->dst_lock);
1673 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1674 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1675 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1676 dev->name,
1677 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1678 ntohs(dest->port),
1679 refcount_read(&dest->refcnt));
1680 __ip_vs_dst_cache_reset(dest);
1681 }
1682 spin_unlock_bh(&dest->dst_lock);
1683
1684}
1685
1686
1687
1688static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1689 void *ptr)
1690{
1691 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1692 struct net *net = dev_net(dev);
1693 struct netns_ipvs *ipvs = net_ipvs(net);
1694 struct ip_vs_service *svc;
1695 struct ip_vs_dest *dest;
1696 unsigned int idx;
1697
1698 if (event != NETDEV_DOWN || !ipvs)
1699 return NOTIFY_DONE;
1700 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1701 EnterFunction(2);
1702 mutex_lock(&__ip_vs_mutex);
1703 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1704 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1705 if (svc->ipvs == ipvs) {
1706 list_for_each_entry(dest, &svc->destinations,
1707 n_list) {
1708 ip_vs_forget_dev(dest, dev);
1709 }
1710 }
1711 }
1712
1713 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1714 if (svc->ipvs == ipvs) {
1715 list_for_each_entry(dest, &svc->destinations,
1716 n_list) {
1717 ip_vs_forget_dev(dest, dev);
1718 }
1719 }
1720
1721 }
1722 }
1723
1724 spin_lock_bh(&ipvs->dest_trash_lock);
1725 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1726 ip_vs_forget_dev(dest, dev);
1727 }
1728 spin_unlock_bh(&ipvs->dest_trash_lock);
1729 mutex_unlock(&__ip_vs_mutex);
1730 LeaveFunction(2);
1731 return NOTIFY_DONE;
1732}
1733
1734
1735
1736
1737static int ip_vs_zero_service(struct ip_vs_service *svc)
1738{
1739 struct ip_vs_dest *dest;
1740
1741 list_for_each_entry(dest, &svc->destinations, n_list) {
1742 ip_vs_zero_stats(&dest->stats);
1743 }
1744 ip_vs_zero_stats(&svc->stats);
1745 return 0;
1746}
1747
1748static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1749{
1750 int idx;
1751 struct ip_vs_service *svc;
1752
1753 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1754 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1755 if (svc->ipvs == ipvs)
1756 ip_vs_zero_service(svc);
1757 }
1758 }
1759
1760 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1761 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1762 if (svc->ipvs == ipvs)
1763 ip_vs_zero_service(svc);
1764 }
1765 }
1766
1767 ip_vs_zero_stats(&ipvs->tot_stats);
1768 return 0;
1769}
1770
1771#ifdef CONFIG_SYSCTL
1772
1773static int three = 3;
1774
1775static int
1776proc_do_defense_mode(struct ctl_table *table, int write,
1777 void *buffer, size_t *lenp, loff_t *ppos)
1778{
1779 struct netns_ipvs *ipvs = table->extra2;
1780 int *valp = table->data;
1781 int val = *valp;
1782 int rc;
1783
1784 struct ctl_table tmp = {
1785 .data = &val,
1786 .maxlen = sizeof(int),
1787 .mode = table->mode,
1788 };
1789
1790 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1791 if (write && (*valp != val)) {
1792 if (val < 0 || val > 3) {
1793 rc = -EINVAL;
1794 } else {
1795 *valp = val;
1796 update_defense_level(ipvs);
1797 }
1798 }
1799 return rc;
1800}
1801
1802static int
1803proc_do_sync_threshold(struct ctl_table *table, int write,
1804 void *buffer, size_t *lenp, loff_t *ppos)
1805{
1806 int *valp = table->data;
1807 int val[2];
1808 int rc;
1809 struct ctl_table tmp = {
1810 .data = &val,
1811 .maxlen = table->maxlen,
1812 .mode = table->mode,
1813 };
1814
1815 memcpy(val, valp, sizeof(val));
1816 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1817 if (write) {
1818 if (val[0] < 0 || val[1] < 0 ||
1819 (val[0] >= val[1] && val[1]))
1820 rc = -EINVAL;
1821 else
1822 memcpy(valp, val, sizeof(val));
1823 }
1824 return rc;
1825}
1826
1827static int
1828proc_do_sync_ports(struct ctl_table *table, int write,
1829 void *buffer, size_t *lenp, loff_t *ppos)
1830{
1831 int *valp = table->data;
1832 int val = *valp;
1833 int rc;
1834
1835 struct ctl_table tmp = {
1836 .data = &val,
1837 .maxlen = sizeof(int),
1838 .mode = table->mode,
1839 };
1840
1841 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1842 if (write && (*valp != val)) {
1843 if (val < 1 || !is_power_of_2(val))
1844 rc = -EINVAL;
1845 else
1846 *valp = val;
1847 }
1848 return rc;
1849}
1850
1851
1852
1853
1854
1855
1856
1857static struct ctl_table vs_vars[] = {
1858 {
1859 .procname = "amemthresh",
1860 .maxlen = sizeof(int),
1861 .mode = 0644,
1862 .proc_handler = proc_dointvec,
1863 },
1864 {
1865 .procname = "am_droprate",
1866 .maxlen = sizeof(int),
1867 .mode = 0644,
1868 .proc_handler = proc_dointvec,
1869 },
1870 {
1871 .procname = "drop_entry",
1872 .maxlen = sizeof(int),
1873 .mode = 0644,
1874 .proc_handler = proc_do_defense_mode,
1875 },
1876 {
1877 .procname = "drop_packet",
1878 .maxlen = sizeof(int),
1879 .mode = 0644,
1880 .proc_handler = proc_do_defense_mode,
1881 },
1882#ifdef CONFIG_IP_VS_NFCT
1883 {
1884 .procname = "conntrack",
1885 .maxlen = sizeof(int),
1886 .mode = 0644,
1887 .proc_handler = &proc_dointvec,
1888 },
1889#endif
1890 {
1891 .procname = "secure_tcp",
1892 .maxlen = sizeof(int),
1893 .mode = 0644,
1894 .proc_handler = proc_do_defense_mode,
1895 },
1896 {
1897 .procname = "snat_reroute",
1898 .maxlen = sizeof(int),
1899 .mode = 0644,
1900 .proc_handler = &proc_dointvec,
1901 },
1902 {
1903 .procname = "sync_version",
1904 .maxlen = sizeof(int),
1905 .mode = 0644,
1906 .proc_handler = proc_dointvec_minmax,
1907 .extra1 = SYSCTL_ZERO,
1908 .extra2 = SYSCTL_ONE,
1909 },
1910 {
1911 .procname = "sync_ports",
1912 .maxlen = sizeof(int),
1913 .mode = 0644,
1914 .proc_handler = proc_do_sync_ports,
1915 },
1916 {
1917 .procname = "sync_persist_mode",
1918 .maxlen = sizeof(int),
1919 .mode = 0644,
1920 .proc_handler = proc_dointvec,
1921 },
1922 {
1923 .procname = "sync_qlen_max",
1924 .maxlen = sizeof(unsigned long),
1925 .mode = 0644,
1926 .proc_handler = proc_doulongvec_minmax,
1927 },
1928 {
1929 .procname = "sync_sock_size",
1930 .maxlen = sizeof(int),
1931 .mode = 0644,
1932 .proc_handler = proc_dointvec,
1933 },
1934 {
1935 .procname = "cache_bypass",
1936 .maxlen = sizeof(int),
1937 .mode = 0644,
1938 .proc_handler = proc_dointvec,
1939 },
1940 {
1941 .procname = "expire_nodest_conn",
1942 .maxlen = sizeof(int),
1943 .mode = 0644,
1944 .proc_handler = proc_dointvec,
1945 },
1946 {
1947 .procname = "sloppy_tcp",
1948 .maxlen = sizeof(int),
1949 .mode = 0644,
1950 .proc_handler = proc_dointvec,
1951 },
1952 {
1953 .procname = "sloppy_sctp",
1954 .maxlen = sizeof(int),
1955 .mode = 0644,
1956 .proc_handler = proc_dointvec,
1957 },
1958 {
1959 .procname = "expire_quiescent_template",
1960 .maxlen = sizeof(int),
1961 .mode = 0644,
1962 .proc_handler = proc_dointvec,
1963 },
1964 {
1965 .procname = "sync_threshold",
1966 .maxlen =
1967 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1968 .mode = 0644,
1969 .proc_handler = proc_do_sync_threshold,
1970 },
1971 {
1972 .procname = "sync_refresh_period",
1973 .maxlen = sizeof(int),
1974 .mode = 0644,
1975 .proc_handler = proc_dointvec_jiffies,
1976 },
1977 {
1978 .procname = "sync_retries",
1979 .maxlen = sizeof(int),
1980 .mode = 0644,
1981 .proc_handler = proc_dointvec_minmax,
1982 .extra1 = SYSCTL_ZERO,
1983 .extra2 = &three,
1984 },
1985 {
1986 .procname = "nat_icmp_send",
1987 .maxlen = sizeof(int),
1988 .mode = 0644,
1989 .proc_handler = proc_dointvec,
1990 },
1991 {
1992 .procname = "pmtu_disc",
1993 .maxlen = sizeof(int),
1994 .mode = 0644,
1995 .proc_handler = proc_dointvec,
1996 },
1997 {
1998 .procname = "backup_only",
1999 .maxlen = sizeof(int),
2000 .mode = 0644,
2001 .proc_handler = proc_dointvec,
2002 },
2003 {
2004 .procname = "conn_reuse_mode",
2005 .maxlen = sizeof(int),
2006 .mode = 0644,
2007 .proc_handler = proc_dointvec,
2008 },
2009 {
2010 .procname = "schedule_icmp",
2011 .maxlen = sizeof(int),
2012 .mode = 0644,
2013 .proc_handler = proc_dointvec,
2014 },
2015 {
2016 .procname = "ignore_tunneled",
2017 .maxlen = sizeof(int),
2018 .mode = 0644,
2019 .proc_handler = proc_dointvec,
2020 },
2021 {
2022 .procname = "run_estimation",
2023 .maxlen = sizeof(int),
2024 .mode = 0644,
2025 .proc_handler = proc_dointvec,
2026 },
2027#ifdef CONFIG_IP_VS_DEBUG
2028 {
2029 .procname = "debug_level",
2030 .data = &sysctl_ip_vs_debug_level,
2031 .maxlen = sizeof(int),
2032 .mode = 0644,
2033 .proc_handler = proc_dointvec,
2034 },
2035#endif
2036 { }
2037};
2038
2039#endif
2040
2041#ifdef CONFIG_PROC_FS
2042
2043struct ip_vs_iter {
2044 struct seq_net_private p;
2045 struct hlist_head *table;
2046 int bucket;
2047};
2048
2049
2050
2051
2052
2053static inline const char *ip_vs_fwd_name(unsigned int flags)
2054{
2055 switch (flags & IP_VS_CONN_F_FWD_MASK) {
2056 case IP_VS_CONN_F_LOCALNODE:
2057 return "Local";
2058 case IP_VS_CONN_F_TUNNEL:
2059 return "Tunnel";
2060 case IP_VS_CONN_F_DROUTE:
2061 return "Route";
2062 default:
2063 return "Masq";
2064 }
2065}
2066
2067
2068
2069static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
2070{
2071 struct net *net = seq_file_net(seq);
2072 struct netns_ipvs *ipvs = net_ipvs(net);
2073 struct ip_vs_iter *iter = seq->private;
2074 int idx;
2075 struct ip_vs_service *svc;
2076
2077
2078 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2079 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
2080 if ((svc->ipvs == ipvs) && pos-- == 0) {
2081 iter->table = ip_vs_svc_table;
2082 iter->bucket = idx;
2083 return svc;
2084 }
2085 }
2086 }
2087
2088
2089 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2090 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
2091 f_list) {
2092 if ((svc->ipvs == ipvs) && pos-- == 0) {
2093 iter->table = ip_vs_svc_fwm_table;
2094 iter->bucket = idx;
2095 return svc;
2096 }
2097 }
2098 }
2099
2100 return NULL;
2101}
2102
2103static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
2104 __acquires(RCU)
2105{
2106 rcu_read_lock();
2107 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
2108}
2109
2110
2111static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2112{
2113 struct hlist_node *e;
2114 struct ip_vs_iter *iter;
2115 struct ip_vs_service *svc;
2116
2117 ++*pos;
2118 if (v == SEQ_START_TOKEN)
2119 return ip_vs_info_array(seq,0);
2120
2121 svc = v;
2122 iter = seq->private;
2123
2124 if (iter->table == ip_vs_svc_table) {
2125
2126 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
2127 if (e)
2128 return hlist_entry(e, struct ip_vs_service, s_list);
2129
2130 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2131 hlist_for_each_entry_rcu(svc,
2132 &ip_vs_svc_table[iter->bucket],
2133 s_list) {
2134 return svc;
2135 }
2136 }
2137
2138 iter->table = ip_vs_svc_fwm_table;
2139 iter->bucket = -1;
2140 goto scan_fwmark;
2141 }
2142
2143
2144 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2145 if (e)
2146 return hlist_entry(e, struct ip_vs_service, f_list);
2147
2148 scan_fwmark:
2149 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2150 hlist_for_each_entry_rcu(svc,
2151 &ip_vs_svc_fwm_table[iter->bucket],
2152 f_list)
2153 return svc;
2154 }
2155
2156 return NULL;
2157}
2158
2159static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2160 __releases(RCU)
2161{
2162 rcu_read_unlock();
2163}
2164
2165
2166static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2167{
2168 if (v == SEQ_START_TOKEN) {
2169 seq_printf(seq,
2170 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2171 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2172 seq_puts(seq,
2173 "Prot LocalAddress:Port Scheduler Flags\n");
2174 seq_puts(seq,
2175 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2176 } else {
2177 struct net *net = seq_file_net(seq);
2178 struct netns_ipvs *ipvs = net_ipvs(net);
2179 const struct ip_vs_service *svc = v;
2180 const struct ip_vs_iter *iter = seq->private;
2181 const struct ip_vs_dest *dest;
2182 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2183 char *sched_name = sched ? sched->name : "none";
2184
2185 if (svc->ipvs != ipvs)
2186 return 0;
2187 if (iter->table == ip_vs_svc_table) {
2188#ifdef CONFIG_IP_VS_IPV6
2189 if (svc->af == AF_INET6)
2190 seq_printf(seq, "%s [%pI6]:%04X %s ",
2191 ip_vs_proto_name(svc->protocol),
2192 &svc->addr.in6,
2193 ntohs(svc->port),
2194 sched_name);
2195 else
2196#endif
2197 seq_printf(seq, "%s %08X:%04X %s %s ",
2198 ip_vs_proto_name(svc->protocol),
2199 ntohl(svc->addr.ip),
2200 ntohs(svc->port),
2201 sched_name,
2202 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2203 } else {
2204 seq_printf(seq, "FWM %08X %s %s",
2205 svc->fwmark, sched_name,
2206 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2207 }
2208
2209 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2210 seq_printf(seq, "persistent %d %08X\n",
2211 svc->timeout,
2212 ntohl(svc->netmask));
2213 else
2214 seq_putc(seq, '\n');
2215
2216 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2217#ifdef CONFIG_IP_VS_IPV6
2218 if (dest->af == AF_INET6)
2219 seq_printf(seq,
2220 " -> [%pI6]:%04X"
2221 " %-7s %-6d %-10d %-10d\n",
2222 &dest->addr.in6,
2223 ntohs(dest->port),
2224 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2225 atomic_read(&dest->weight),
2226 atomic_read(&dest->activeconns),
2227 atomic_read(&dest->inactconns));
2228 else
2229#endif
2230 seq_printf(seq,
2231 " -> %08X:%04X "
2232 "%-7s %-6d %-10d %-10d\n",
2233 ntohl(dest->addr.ip),
2234 ntohs(dest->port),
2235 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2236 atomic_read(&dest->weight),
2237 atomic_read(&dest->activeconns),
2238 atomic_read(&dest->inactconns));
2239
2240 }
2241 }
2242 return 0;
2243}
2244
2245static const struct seq_operations ip_vs_info_seq_ops = {
2246 .start = ip_vs_info_seq_start,
2247 .next = ip_vs_info_seq_next,
2248 .stop = ip_vs_info_seq_stop,
2249 .show = ip_vs_info_seq_show,
2250};
2251
2252static int ip_vs_stats_show(struct seq_file *seq, void *v)
2253{
2254 struct net *net = seq_file_single_net(seq);
2255 struct ip_vs_kstats show;
2256
2257
2258 seq_puts(seq,
2259 " Total Incoming Outgoing Incoming Outgoing\n");
2260 seq_puts(seq,
2261 " Conns Packets Packets Bytes Bytes\n");
2262
2263 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2264 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2265 (unsigned long long)show.conns,
2266 (unsigned long long)show.inpkts,
2267 (unsigned long long)show.outpkts,
2268 (unsigned long long)show.inbytes,
2269 (unsigned long long)show.outbytes);
2270
2271
2272 seq_puts(seq,
2273 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2274 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2275 (unsigned long long)show.cps,
2276 (unsigned long long)show.inpps,
2277 (unsigned long long)show.outpps,
2278 (unsigned long long)show.inbps,
2279 (unsigned long long)show.outbps);
2280
2281 return 0;
2282}
2283
2284static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2285{
2286 struct net *net = seq_file_single_net(seq);
2287 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2288 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2289 struct ip_vs_kstats kstats;
2290 int i;
2291
2292
2293 seq_puts(seq,
2294 " Total Incoming Outgoing Incoming Outgoing\n");
2295 seq_puts(seq,
2296 "CPU Conns Packets Packets Bytes Bytes\n");
2297
2298 for_each_possible_cpu(i) {
2299 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2300 unsigned int start;
2301 u64 conns, inpkts, outpkts, inbytes, outbytes;
2302
2303 do {
2304 start = u64_stats_fetch_begin_irq(&u->syncp);
2305 conns = u->cnt.conns;
2306 inpkts = u->cnt.inpkts;
2307 outpkts = u->cnt.outpkts;
2308 inbytes = u->cnt.inbytes;
2309 outbytes = u->cnt.outbytes;
2310 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2311
2312 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2313 i, (u64)conns, (u64)inpkts,
2314 (u64)outpkts, (u64)inbytes,
2315 (u64)outbytes);
2316 }
2317
2318 ip_vs_copy_stats(&kstats, tot_stats);
2319
2320 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2321 (unsigned long long)kstats.conns,
2322 (unsigned long long)kstats.inpkts,
2323 (unsigned long long)kstats.outpkts,
2324 (unsigned long long)kstats.inbytes,
2325 (unsigned long long)kstats.outbytes);
2326
2327
2328 seq_puts(seq,
2329 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2330 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2331 kstats.cps,
2332 kstats.inpps,
2333 kstats.outpps,
2334 kstats.inbps,
2335 kstats.outbps);
2336
2337 return 0;
2338}
2339#endif
2340
2341
2342
2343
2344static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2345{
2346#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2347 struct ip_vs_proto_data *pd;
2348#endif
2349
2350 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2351 u->tcp_timeout,
2352 u->tcp_fin_timeout,
2353 u->udp_timeout);
2354
2355#ifdef CONFIG_IP_VS_PROTO_TCP
2356 if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2357 u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2358 return -EINVAL;
2359 }
2360#endif
2361
2362#ifdef CONFIG_IP_VS_PROTO_UDP
2363 if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2364 return -EINVAL;
2365#endif
2366
2367#ifdef CONFIG_IP_VS_PROTO_TCP
2368 if (u->tcp_timeout) {
2369 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2370 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2371 = u->tcp_timeout * HZ;
2372 }
2373
2374 if (u->tcp_fin_timeout) {
2375 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2376 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2377 = u->tcp_fin_timeout * HZ;
2378 }
2379#endif
2380
2381#ifdef CONFIG_IP_VS_PROTO_UDP
2382 if (u->udp_timeout) {
2383 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2384 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2385 = u->udp_timeout * HZ;
2386 }
2387#endif
2388 return 0;
2389}
2390
2391#define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2392
2393struct ip_vs_svcdest_user {
2394 struct ip_vs_service_user s;
2395 struct ip_vs_dest_user d;
2396};
2397
2398static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2399 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2400 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2401 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2402 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2403 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2404 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2405 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2406 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2407 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2408 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2409};
2410
2411union ip_vs_set_arglen {
2412 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2413 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2414 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2415 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2416 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2417 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2418 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2419 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2420 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2421 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2422};
2423
2424#define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2425
2426static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2427 struct ip_vs_service_user *usvc_compat)
2428{
2429 memset(usvc, 0, sizeof(*usvc));
2430
2431 usvc->af = AF_INET;
2432 usvc->protocol = usvc_compat->protocol;
2433 usvc->addr.ip = usvc_compat->addr;
2434 usvc->port = usvc_compat->port;
2435 usvc->fwmark = usvc_compat->fwmark;
2436
2437
2438 usvc->sched_name = usvc_compat->sched_name;
2439
2440 usvc->flags = usvc_compat->flags;
2441 usvc->timeout = usvc_compat->timeout;
2442 usvc->netmask = usvc_compat->netmask;
2443}
2444
2445static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2446 struct ip_vs_dest_user *udest_compat)
2447{
2448 memset(udest, 0, sizeof(*udest));
2449
2450 udest->addr.ip = udest_compat->addr;
2451 udest->port = udest_compat->port;
2452 udest->conn_flags = udest_compat->conn_flags;
2453 udest->weight = udest_compat->weight;
2454 udest->u_threshold = udest_compat->u_threshold;
2455 udest->l_threshold = udest_compat->l_threshold;
2456 udest->af = AF_INET;
2457 udest->tun_type = IP_VS_CONN_F_TUNNEL_TYPE_IPIP;
2458}
2459
2460static int
2461do_ip_vs_set_ctl(struct sock *sk, int cmd, sockptr_t ptr, unsigned int len)
2462{
2463 struct net *net = sock_net(sk);
2464 int ret;
2465 unsigned char arg[MAX_SET_ARGLEN];
2466 struct ip_vs_service_user *usvc_compat;
2467 struct ip_vs_service_user_kern usvc;
2468 struct ip_vs_service *svc;
2469 struct ip_vs_dest_user *udest_compat;
2470 struct ip_vs_dest_user_kern udest;
2471 struct netns_ipvs *ipvs = net_ipvs(net);
2472
2473 BUILD_BUG_ON(sizeof(arg) > 255);
2474 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2475 return -EPERM;
2476
2477 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2478 return -EINVAL;
2479 if (len != set_arglen[CMDID(cmd)]) {
2480 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2481 len, set_arglen[CMDID(cmd)]);
2482 return -EINVAL;
2483 }
2484
2485 if (copy_from_sockptr(arg, ptr, len) != 0)
2486 return -EFAULT;
2487
2488
2489 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2490 cmd == IP_VS_SO_SET_STOPDAEMON) {
2491 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2492
2493 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2494 struct ipvs_sync_daemon_cfg cfg;
2495
2496 memset(&cfg, 0, sizeof(cfg));
2497 ret = -EINVAL;
2498 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2499 sizeof(cfg.mcast_ifn)) <= 0)
2500 return ret;
2501 cfg.syncid = dm->syncid;
2502 ret = start_sync_thread(ipvs, &cfg, dm->state);
2503 } else {
2504 ret = stop_sync_thread(ipvs, dm->state);
2505 }
2506 return ret;
2507 }
2508
2509 mutex_lock(&__ip_vs_mutex);
2510 if (cmd == IP_VS_SO_SET_FLUSH) {
2511
2512 ret = ip_vs_flush(ipvs, false);
2513 goto out_unlock;
2514 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2515
2516 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2517 goto out_unlock;
2518 } else if (!len) {
2519
2520 ret = -EINVAL;
2521 goto out_unlock;
2522 }
2523
2524 usvc_compat = (struct ip_vs_service_user *)arg;
2525 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2526
2527
2528
2529 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2530 ip_vs_copy_udest_compat(&udest, udest_compat);
2531
2532 if (cmd == IP_VS_SO_SET_ZERO) {
2533
2534 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2535 ret = ip_vs_zero_all(ipvs);
2536 goto out_unlock;
2537 }
2538 }
2539
2540 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2541 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2542 IP_VS_SCHEDNAME_MAXLEN) {
2543 ret = -EINVAL;
2544 goto out_unlock;
2545 }
2546
2547
2548 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2549 usvc.protocol != IPPROTO_SCTP) {
2550 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2551 usvc.protocol, &usvc.addr.ip,
2552 ntohs(usvc.port));
2553 ret = -EFAULT;
2554 goto out_unlock;
2555 }
2556
2557
2558 rcu_read_lock();
2559 if (usvc.fwmark == 0)
2560 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2561 &usvc.addr, usvc.port);
2562 else
2563 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2564 rcu_read_unlock();
2565
2566 if (cmd != IP_VS_SO_SET_ADD
2567 && (svc == NULL || svc->protocol != usvc.protocol)) {
2568 ret = -ESRCH;
2569 goto out_unlock;
2570 }
2571
2572 switch (cmd) {
2573 case IP_VS_SO_SET_ADD:
2574 if (svc != NULL)
2575 ret = -EEXIST;
2576 else
2577 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2578 break;
2579 case IP_VS_SO_SET_EDIT:
2580 ret = ip_vs_edit_service(svc, &usvc);
2581 break;
2582 case IP_VS_SO_SET_DEL:
2583 ret = ip_vs_del_service(svc);
2584 if (!ret)
2585 goto out_unlock;
2586 break;
2587 case IP_VS_SO_SET_ZERO:
2588 ret = ip_vs_zero_service(svc);
2589 break;
2590 case IP_VS_SO_SET_ADDDEST:
2591 ret = ip_vs_add_dest(svc, &udest);
2592 break;
2593 case IP_VS_SO_SET_EDITDEST:
2594 ret = ip_vs_edit_dest(svc, &udest);
2595 break;
2596 case IP_VS_SO_SET_DELDEST:
2597 ret = ip_vs_del_dest(svc, &udest);
2598 }
2599
2600 out_unlock:
2601 mutex_unlock(&__ip_vs_mutex);
2602 return ret;
2603}
2604
2605
2606static void
2607ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2608{
2609 struct ip_vs_scheduler *sched;
2610 struct ip_vs_kstats kstats;
2611 char *sched_name;
2612
2613 sched = rcu_dereference_protected(src->scheduler, 1);
2614 sched_name = sched ? sched->name : "none";
2615 dst->protocol = src->protocol;
2616 dst->addr = src->addr.ip;
2617 dst->port = src->port;
2618 dst->fwmark = src->fwmark;
2619 strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2620 dst->flags = src->flags;
2621 dst->timeout = src->timeout / HZ;
2622 dst->netmask = src->netmask;
2623 dst->num_dests = src->num_dests;
2624 ip_vs_copy_stats(&kstats, &src->stats);
2625 ip_vs_export_stats_user(&dst->stats, &kstats);
2626}
2627
2628static inline int
2629__ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2630 const struct ip_vs_get_services *get,
2631 struct ip_vs_get_services __user *uptr)
2632{
2633 int idx, count=0;
2634 struct ip_vs_service *svc;
2635 struct ip_vs_service_entry entry;
2636 int ret = 0;
2637
2638 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2639 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2640
2641 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2642 continue;
2643
2644 if (count >= get->num_services)
2645 goto out;
2646 memset(&entry, 0, sizeof(entry));
2647 ip_vs_copy_service(&entry, svc);
2648 if (copy_to_user(&uptr->entrytable[count],
2649 &entry, sizeof(entry))) {
2650 ret = -EFAULT;
2651 goto out;
2652 }
2653 count++;
2654 }
2655 }
2656
2657 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2658 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2659
2660 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2661 continue;
2662
2663 if (count >= get->num_services)
2664 goto out;
2665 memset(&entry, 0, sizeof(entry));
2666 ip_vs_copy_service(&entry, svc);
2667 if (copy_to_user(&uptr->entrytable[count],
2668 &entry, sizeof(entry))) {
2669 ret = -EFAULT;
2670 goto out;
2671 }
2672 count++;
2673 }
2674 }
2675out:
2676 return ret;
2677}
2678
2679static inline int
2680__ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2681 struct ip_vs_get_dests __user *uptr)
2682{
2683 struct ip_vs_service *svc;
2684 union nf_inet_addr addr = { .ip = get->addr };
2685 int ret = 0;
2686
2687 rcu_read_lock();
2688 if (get->fwmark)
2689 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2690 else
2691 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2692 get->port);
2693 rcu_read_unlock();
2694
2695 if (svc) {
2696 int count = 0;
2697 struct ip_vs_dest *dest;
2698 struct ip_vs_dest_entry entry;
2699 struct ip_vs_kstats kstats;
2700
2701 memset(&entry, 0, sizeof(entry));
2702 list_for_each_entry(dest, &svc->destinations, n_list) {
2703 if (count >= get->num_dests)
2704 break;
2705
2706
2707
2708
2709 if (dest->af != svc->af)
2710 continue;
2711
2712 entry.addr = dest->addr.ip;
2713 entry.port = dest->port;
2714 entry.conn_flags = atomic_read(&dest->conn_flags);
2715 entry.weight = atomic_read(&dest->weight);
2716 entry.u_threshold = dest->u_threshold;
2717 entry.l_threshold = dest->l_threshold;
2718 entry.activeconns = atomic_read(&dest->activeconns);
2719 entry.inactconns = atomic_read(&dest->inactconns);
2720 entry.persistconns = atomic_read(&dest->persistconns);
2721 ip_vs_copy_stats(&kstats, &dest->stats);
2722 ip_vs_export_stats_user(&entry.stats, &kstats);
2723 if (copy_to_user(&uptr->entrytable[count],
2724 &entry, sizeof(entry))) {
2725 ret = -EFAULT;
2726 break;
2727 }
2728 count++;
2729 }
2730 } else
2731 ret = -ESRCH;
2732 return ret;
2733}
2734
2735static inline void
2736__ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2737{
2738#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2739 struct ip_vs_proto_data *pd;
2740#endif
2741
2742 memset(u, 0, sizeof (*u));
2743
2744#ifdef CONFIG_IP_VS_PROTO_TCP
2745 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2746 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2747 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2748#endif
2749#ifdef CONFIG_IP_VS_PROTO_UDP
2750 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2751 u->udp_timeout =
2752 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2753#endif
2754}
2755
2756static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2757 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2758 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2759 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2760 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2761 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2762 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2763 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2764};
2765
2766union ip_vs_get_arglen {
2767 char field_IP_VS_SO_GET_VERSION[64];
2768 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2769 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2770 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2771 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2772 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2773 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2774};
2775
2776#define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2777
2778static int
2779do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2780{
2781 unsigned char arg[MAX_GET_ARGLEN];
2782 int ret = 0;
2783 unsigned int copylen;
2784 struct net *net = sock_net(sk);
2785 struct netns_ipvs *ipvs = net_ipvs(net);
2786
2787 BUG_ON(!net);
2788 BUILD_BUG_ON(sizeof(arg) > 255);
2789 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2790 return -EPERM;
2791
2792 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2793 return -EINVAL;
2794
2795 copylen = get_arglen[CMDID(cmd)];
2796 if (*len < (int) copylen) {
2797 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2798 return -EINVAL;
2799 }
2800
2801 if (copy_from_user(arg, user, copylen) != 0)
2802 return -EFAULT;
2803
2804
2805
2806 if (cmd == IP_VS_SO_GET_DAEMON) {
2807 struct ip_vs_daemon_user d[2];
2808
2809 memset(&d, 0, sizeof(d));
2810 mutex_lock(&ipvs->sync_mutex);
2811 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2812 d[0].state = IP_VS_STATE_MASTER;
2813 strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2814 sizeof(d[0].mcast_ifn));
2815 d[0].syncid = ipvs->mcfg.syncid;
2816 }
2817 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2818 d[1].state = IP_VS_STATE_BACKUP;
2819 strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2820 sizeof(d[1].mcast_ifn));
2821 d[1].syncid = ipvs->bcfg.syncid;
2822 }
2823 if (copy_to_user(user, &d, sizeof(d)) != 0)
2824 ret = -EFAULT;
2825 mutex_unlock(&ipvs->sync_mutex);
2826 return ret;
2827 }
2828
2829 mutex_lock(&__ip_vs_mutex);
2830 switch (cmd) {
2831 case IP_VS_SO_GET_VERSION:
2832 {
2833 char buf[64];
2834
2835 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2836 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2837 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2838 ret = -EFAULT;
2839 goto out;
2840 }
2841 *len = strlen(buf)+1;
2842 }
2843 break;
2844
2845 case IP_VS_SO_GET_INFO:
2846 {
2847 struct ip_vs_getinfo info;
2848 info.version = IP_VS_VERSION_CODE;
2849 info.size = ip_vs_conn_tab_size;
2850 info.num_services = ipvs->num_services;
2851 if (copy_to_user(user, &info, sizeof(info)) != 0)
2852 ret = -EFAULT;
2853 }
2854 break;
2855
2856 case IP_VS_SO_GET_SERVICES:
2857 {
2858 struct ip_vs_get_services *get;
2859 int size;
2860
2861 get = (struct ip_vs_get_services *)arg;
2862 size = struct_size(get, entrytable, get->num_services);
2863 if (*len != size) {
2864 pr_err("length: %u != %u\n", *len, size);
2865 ret = -EINVAL;
2866 goto out;
2867 }
2868 ret = __ip_vs_get_service_entries(ipvs, get, user);
2869 }
2870 break;
2871
2872 case IP_VS_SO_GET_SERVICE:
2873 {
2874 struct ip_vs_service_entry *entry;
2875 struct ip_vs_service *svc;
2876 union nf_inet_addr addr;
2877
2878 entry = (struct ip_vs_service_entry *)arg;
2879 addr.ip = entry->addr;
2880 rcu_read_lock();
2881 if (entry->fwmark)
2882 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2883 else
2884 svc = __ip_vs_service_find(ipvs, AF_INET,
2885 entry->protocol, &addr,
2886 entry->port);
2887 rcu_read_unlock();
2888 if (svc) {
2889 ip_vs_copy_service(entry, svc);
2890 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2891 ret = -EFAULT;
2892 } else
2893 ret = -ESRCH;
2894 }
2895 break;
2896
2897 case IP_VS_SO_GET_DESTS:
2898 {
2899 struct ip_vs_get_dests *get;
2900 int size;
2901
2902 get = (struct ip_vs_get_dests *)arg;
2903 size = struct_size(get, entrytable, get->num_dests);
2904 if (*len != size) {
2905 pr_err("length: %u != %u\n", *len, size);
2906 ret = -EINVAL;
2907 goto out;
2908 }
2909 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2910 }
2911 break;
2912
2913 case IP_VS_SO_GET_TIMEOUT:
2914 {
2915 struct ip_vs_timeout_user t;
2916
2917 __ip_vs_get_timeouts(ipvs, &t);
2918 if (copy_to_user(user, &t, sizeof(t)) != 0)
2919 ret = -EFAULT;
2920 }
2921 break;
2922
2923 default:
2924 ret = -EINVAL;
2925 }
2926
2927out:
2928 mutex_unlock(&__ip_vs_mutex);
2929 return ret;
2930}
2931
2932
2933static struct nf_sockopt_ops ip_vs_sockopts = {
2934 .pf = PF_INET,
2935 .set_optmin = IP_VS_BASE_CTL,
2936 .set_optmax = IP_VS_SO_SET_MAX+1,
2937 .set = do_ip_vs_set_ctl,
2938 .get_optmin = IP_VS_BASE_CTL,
2939 .get_optmax = IP_VS_SO_GET_MAX+1,
2940 .get = do_ip_vs_get_ctl,
2941 .owner = THIS_MODULE,
2942};
2943
2944
2945
2946
2947
2948
2949static struct genl_family ip_vs_genl_family;
2950
2951
2952static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2953 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2954 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2955 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2956 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2957 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2958 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2959};
2960
2961
2962static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2963 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2964 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2965 .len = IP_VS_IFNAME_MAXLEN - 1 },
2966 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2967 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2968 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2969 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2970 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2971 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2972};
2973
2974
2975static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2976 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2977 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2978 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2979 .len = sizeof(union nf_inet_addr) },
2980 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2981 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2982 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2983 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
2984 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2985 .len = IP_VS_PENAME_MAXLEN },
2986 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2987 .len = sizeof(struct ip_vs_flags) },
2988 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2989 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2990 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2991};
2992
2993
2994static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2995 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2996 .len = sizeof(union nf_inet_addr) },
2997 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2998 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2999 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
3000 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
3001 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
3002 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
3003 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
3004 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
3005 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
3006 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
3007 [IPVS_DEST_ATTR_TUN_TYPE] = { .type = NLA_U8 },
3008 [IPVS_DEST_ATTR_TUN_PORT] = { .type = NLA_U16 },
3009 [IPVS_DEST_ATTR_TUN_FLAGS] = { .type = NLA_U16 },
3010};
3011
3012static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
3013 struct ip_vs_kstats *kstats)
3014{
3015 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3016
3017 if (!nl_stats)
3018 return -EMSGSIZE;
3019
3020 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
3021 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
3022 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
3023 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3024 IPVS_STATS_ATTR_PAD) ||
3025 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3026 IPVS_STATS_ATTR_PAD) ||
3027 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
3028 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
3029 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
3030 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
3031 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
3032 goto nla_put_failure;
3033 nla_nest_end(skb, nl_stats);
3034
3035 return 0;
3036
3037nla_put_failure:
3038 nla_nest_cancel(skb, nl_stats);
3039 return -EMSGSIZE;
3040}
3041
3042static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
3043 struct ip_vs_kstats *kstats)
3044{
3045 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3046
3047 if (!nl_stats)
3048 return -EMSGSIZE;
3049
3050 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
3051 IPVS_STATS_ATTR_PAD) ||
3052 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
3053 IPVS_STATS_ATTR_PAD) ||
3054 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
3055 IPVS_STATS_ATTR_PAD) ||
3056 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3057 IPVS_STATS_ATTR_PAD) ||
3058 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3059 IPVS_STATS_ATTR_PAD) ||
3060 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
3061 IPVS_STATS_ATTR_PAD) ||
3062 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
3063 IPVS_STATS_ATTR_PAD) ||
3064 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
3065 IPVS_STATS_ATTR_PAD) ||
3066 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
3067 IPVS_STATS_ATTR_PAD) ||
3068 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
3069 IPVS_STATS_ATTR_PAD))
3070 goto nla_put_failure;
3071 nla_nest_end(skb, nl_stats);
3072
3073 return 0;
3074
3075nla_put_failure:
3076 nla_nest_cancel(skb, nl_stats);
3077 return -EMSGSIZE;
3078}
3079
3080static int ip_vs_genl_fill_service(struct sk_buff *skb,
3081 struct ip_vs_service *svc)
3082{
3083 struct ip_vs_scheduler *sched;
3084 struct ip_vs_pe *pe;
3085 struct nlattr *nl_service;
3086 struct ip_vs_flags flags = { .flags = svc->flags,
3087 .mask = ~0 };
3088 struct ip_vs_kstats kstats;
3089 char *sched_name;
3090
3091 nl_service = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_SERVICE);
3092 if (!nl_service)
3093 return -EMSGSIZE;
3094
3095 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
3096 goto nla_put_failure;
3097 if (svc->fwmark) {
3098 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
3099 goto nla_put_failure;
3100 } else {
3101 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
3102 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
3103 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
3104 goto nla_put_failure;
3105 }
3106
3107 sched = rcu_dereference_protected(svc->scheduler, 1);
3108 sched_name = sched ? sched->name : "none";
3109 pe = rcu_dereference_protected(svc->pe, 1);
3110 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
3111 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
3112 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3113 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
3114 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
3115 goto nla_put_failure;
3116 ip_vs_copy_stats(&kstats, &svc->stats);
3117 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3118 goto nla_put_failure;
3119 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
3120 goto nla_put_failure;
3121
3122 nla_nest_end(skb, nl_service);
3123
3124 return 0;
3125
3126nla_put_failure:
3127 nla_nest_cancel(skb, nl_service);
3128 return -EMSGSIZE;
3129}
3130
3131static int ip_vs_genl_dump_service(struct sk_buff *skb,
3132 struct ip_vs_service *svc,
3133 struct netlink_callback *cb)
3134{
3135 void *hdr;
3136
3137 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3138 &ip_vs_genl_family, NLM_F_MULTI,
3139 IPVS_CMD_NEW_SERVICE);
3140 if (!hdr)
3141 return -EMSGSIZE;
3142
3143 if (ip_vs_genl_fill_service(skb, svc) < 0)
3144 goto nla_put_failure;
3145
3146 genlmsg_end(skb, hdr);
3147 return 0;
3148
3149nla_put_failure:
3150 genlmsg_cancel(skb, hdr);
3151 return -EMSGSIZE;
3152}
3153
3154static int ip_vs_genl_dump_services(struct sk_buff *skb,
3155 struct netlink_callback *cb)
3156{
3157 int idx = 0, i;
3158 int start = cb->args[0];
3159 struct ip_vs_service *svc;
3160 struct net *net = sock_net(skb->sk);
3161 struct netns_ipvs *ipvs = net_ipvs(net);
3162
3163 mutex_lock(&__ip_vs_mutex);
3164 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3165 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3166 if (++idx <= start || (svc->ipvs != ipvs))
3167 continue;
3168 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3169 idx--;
3170 goto nla_put_failure;
3171 }
3172 }
3173 }
3174
3175 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3176 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3177 if (++idx <= start || (svc->ipvs != ipvs))
3178 continue;
3179 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3180 idx--;
3181 goto nla_put_failure;
3182 }
3183 }
3184 }
3185
3186nla_put_failure:
3187 mutex_unlock(&__ip_vs_mutex);
3188 cb->args[0] = idx;
3189
3190 return skb->len;
3191}
3192
3193static bool ip_vs_is_af_valid(int af)
3194{
3195 if (af == AF_INET)
3196 return true;
3197#ifdef CONFIG_IP_VS_IPV6
3198 if (af == AF_INET6 && ipv6_mod_enabled())
3199 return true;
3200#endif
3201 return false;
3202}
3203
3204static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3205 struct ip_vs_service_user_kern *usvc,
3206 struct nlattr *nla, bool full_entry,
3207 struct ip_vs_service **ret_svc)
3208{
3209 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3210 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3211 struct ip_vs_service *svc;
3212
3213
3214 if (nla == NULL ||
3215 nla_parse_nested_deprecated(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy, NULL))
3216 return -EINVAL;
3217
3218 nla_af = attrs[IPVS_SVC_ATTR_AF];
3219 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3220 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3221 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3222 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3223
3224 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3225 return -EINVAL;
3226
3227 memset(usvc, 0, sizeof(*usvc));
3228
3229 usvc->af = nla_get_u16(nla_af);
3230 if (!ip_vs_is_af_valid(usvc->af))
3231 return -EAFNOSUPPORT;
3232
3233 if (nla_fwmark) {
3234 usvc->protocol = IPPROTO_TCP;
3235 usvc->fwmark = nla_get_u32(nla_fwmark);
3236 } else {
3237 usvc->protocol = nla_get_u16(nla_protocol);
3238 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3239 usvc->port = nla_get_be16(nla_port);
3240 usvc->fwmark = 0;
3241 }
3242
3243 rcu_read_lock();
3244 if (usvc->fwmark)
3245 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3246 else
3247 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3248 &usvc->addr, usvc->port);
3249 rcu_read_unlock();
3250 *ret_svc = svc;
3251
3252
3253 if (full_entry) {
3254 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3255 *nla_netmask;
3256 struct ip_vs_flags flags;
3257
3258 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3259 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3260 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3261 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3262 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3263
3264 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3265 return -EINVAL;
3266
3267 nla_memcpy(&flags, nla_flags, sizeof(flags));
3268
3269
3270 if (svc)
3271 usvc->flags = svc->flags;
3272
3273
3274 usvc->flags = (usvc->flags & ~flags.mask) |
3275 (flags.flags & flags.mask);
3276 usvc->sched_name = nla_data(nla_sched);
3277 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3278 usvc->timeout = nla_get_u32(nla_timeout);
3279 usvc->netmask = nla_get_be32(nla_netmask);
3280 }
3281
3282 return 0;
3283}
3284
3285static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3286 struct nlattr *nla)
3287{
3288 struct ip_vs_service_user_kern usvc;
3289 struct ip_vs_service *svc;
3290 int ret;
3291
3292 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, false, &svc);
3293 return ret ? ERR_PTR(ret) : svc;
3294}
3295
3296static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3297{
3298 struct nlattr *nl_dest;
3299 struct ip_vs_kstats kstats;
3300
3301 nl_dest = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DEST);
3302 if (!nl_dest)
3303 return -EMSGSIZE;
3304
3305 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3306 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3307 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3308 (atomic_read(&dest->conn_flags) &
3309 IP_VS_CONN_F_FWD_MASK)) ||
3310 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3311 atomic_read(&dest->weight)) ||
3312 nla_put_u8(skb, IPVS_DEST_ATTR_TUN_TYPE,
3313 dest->tun_type) ||
3314 nla_put_be16(skb, IPVS_DEST_ATTR_TUN_PORT,
3315 dest->tun_port) ||
3316 nla_put_u16(skb, IPVS_DEST_ATTR_TUN_FLAGS,
3317 dest->tun_flags) ||
3318 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3319 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3320 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3321 atomic_read(&dest->activeconns)) ||
3322 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3323 atomic_read(&dest->inactconns)) ||
3324 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3325 atomic_read(&dest->persistconns)) ||
3326 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3327 goto nla_put_failure;
3328 ip_vs_copy_stats(&kstats, &dest->stats);
3329 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3330 goto nla_put_failure;
3331 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3332 goto nla_put_failure;
3333
3334 nla_nest_end(skb, nl_dest);
3335
3336 return 0;
3337
3338nla_put_failure:
3339 nla_nest_cancel(skb, nl_dest);
3340 return -EMSGSIZE;
3341}
3342
3343static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3344 struct netlink_callback *cb)
3345{
3346 void *hdr;
3347
3348 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3349 &ip_vs_genl_family, NLM_F_MULTI,
3350 IPVS_CMD_NEW_DEST);
3351 if (!hdr)
3352 return -EMSGSIZE;
3353
3354 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3355 goto nla_put_failure;
3356
3357 genlmsg_end(skb, hdr);
3358 return 0;
3359
3360nla_put_failure:
3361 genlmsg_cancel(skb, hdr);
3362 return -EMSGSIZE;
3363}
3364
3365static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3366 struct netlink_callback *cb)
3367{
3368 int idx = 0;
3369 int start = cb->args[0];
3370 struct ip_vs_service *svc;
3371 struct ip_vs_dest *dest;
3372 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3373 struct net *net = sock_net(skb->sk);
3374 struct netns_ipvs *ipvs = net_ipvs(net);
3375
3376 mutex_lock(&__ip_vs_mutex);
3377
3378
3379 if (nlmsg_parse_deprecated(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy, cb->extack))
3380 goto out_err;
3381
3382
3383 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3384 if (IS_ERR_OR_NULL(svc))
3385 goto out_err;
3386
3387
3388 list_for_each_entry(dest, &svc->destinations, n_list) {
3389 if (++idx <= start)
3390 continue;
3391 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3392 idx--;
3393 goto nla_put_failure;
3394 }
3395 }
3396
3397nla_put_failure:
3398 cb->args[0] = idx;
3399
3400out_err:
3401 mutex_unlock(&__ip_vs_mutex);
3402
3403 return skb->len;
3404}
3405
3406static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3407 struct nlattr *nla, bool full_entry)
3408{
3409 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3410 struct nlattr *nla_addr, *nla_port;
3411 struct nlattr *nla_addr_family;
3412
3413
3414 if (nla == NULL ||
3415 nla_parse_nested_deprecated(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy, NULL))
3416 return -EINVAL;
3417
3418 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3419 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3420 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3421
3422 if (!(nla_addr && nla_port))
3423 return -EINVAL;
3424
3425 memset(udest, 0, sizeof(*udest));
3426
3427 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3428 udest->port = nla_get_be16(nla_port);
3429
3430 if (nla_addr_family)
3431 udest->af = nla_get_u16(nla_addr_family);
3432 else
3433 udest->af = 0;
3434
3435
3436 if (full_entry) {
3437 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3438 *nla_l_thresh, *nla_tun_type, *nla_tun_port,
3439 *nla_tun_flags;
3440
3441 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3442 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3443 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3444 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3445 nla_tun_type = attrs[IPVS_DEST_ATTR_TUN_TYPE];
3446 nla_tun_port = attrs[IPVS_DEST_ATTR_TUN_PORT];
3447 nla_tun_flags = attrs[IPVS_DEST_ATTR_TUN_FLAGS];
3448
3449 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3450 return -EINVAL;
3451
3452 udest->conn_flags = nla_get_u32(nla_fwd)
3453 & IP_VS_CONN_F_FWD_MASK;
3454 udest->weight = nla_get_u32(nla_weight);
3455 udest->u_threshold = nla_get_u32(nla_u_thresh);
3456 udest->l_threshold = nla_get_u32(nla_l_thresh);
3457
3458 if (nla_tun_type)
3459 udest->tun_type = nla_get_u8(nla_tun_type);
3460
3461 if (nla_tun_port)
3462 udest->tun_port = nla_get_be16(nla_tun_port);
3463
3464 if (nla_tun_flags)
3465 udest->tun_flags = nla_get_u16(nla_tun_flags);
3466 }
3467
3468 return 0;
3469}
3470
3471static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3472 struct ipvs_sync_daemon_cfg *c)
3473{
3474 struct nlattr *nl_daemon;
3475
3476 nl_daemon = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DAEMON);
3477 if (!nl_daemon)
3478 return -EMSGSIZE;
3479
3480 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3481 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3482 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3483 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3484 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3485 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3486 goto nla_put_failure;
3487#ifdef CONFIG_IP_VS_IPV6
3488 if (c->mcast_af == AF_INET6) {
3489 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3490 &c->mcast_group.in6))
3491 goto nla_put_failure;
3492 } else
3493#endif
3494 if (c->mcast_af == AF_INET &&
3495 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3496 c->mcast_group.ip))
3497 goto nla_put_failure;
3498 nla_nest_end(skb, nl_daemon);
3499
3500 return 0;
3501
3502nla_put_failure:
3503 nla_nest_cancel(skb, nl_daemon);
3504 return -EMSGSIZE;
3505}
3506
3507static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3508 struct ipvs_sync_daemon_cfg *c,
3509 struct netlink_callback *cb)
3510{
3511 void *hdr;
3512 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3513 &ip_vs_genl_family, NLM_F_MULTI,
3514 IPVS_CMD_NEW_DAEMON);
3515 if (!hdr)
3516 return -EMSGSIZE;
3517
3518 if (ip_vs_genl_fill_daemon(skb, state, c))
3519 goto nla_put_failure;
3520
3521 genlmsg_end(skb, hdr);
3522 return 0;
3523
3524nla_put_failure:
3525 genlmsg_cancel(skb, hdr);
3526 return -EMSGSIZE;
3527}
3528
3529static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3530 struct netlink_callback *cb)
3531{
3532 struct net *net = sock_net(skb->sk);
3533 struct netns_ipvs *ipvs = net_ipvs(net);
3534
3535 mutex_lock(&ipvs->sync_mutex);
3536 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3537 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3538 &ipvs->mcfg, cb) < 0)
3539 goto nla_put_failure;
3540
3541 cb->args[0] = 1;
3542 }
3543
3544 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3545 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3546 &ipvs->bcfg, cb) < 0)
3547 goto nla_put_failure;
3548
3549 cb->args[1] = 1;
3550 }
3551
3552nla_put_failure:
3553 mutex_unlock(&ipvs->sync_mutex);
3554
3555 return skb->len;
3556}
3557
3558static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3559{
3560 struct ipvs_sync_daemon_cfg c;
3561 struct nlattr *a;
3562 int ret;
3563
3564 memset(&c, 0, sizeof(c));
3565 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3566 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3567 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3568 return -EINVAL;
3569 strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3570 sizeof(c.mcast_ifn));
3571 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3572
3573 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3574 if (a)
3575 c.sync_maxlen = nla_get_u16(a);
3576
3577 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3578 if (a) {
3579 c.mcast_af = AF_INET;
3580 c.mcast_group.ip = nla_get_in_addr(a);
3581 if (!ipv4_is_multicast(c.mcast_group.ip))
3582 return -EINVAL;
3583 } else {
3584 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3585 if (a) {
3586#ifdef CONFIG_IP_VS_IPV6
3587 int addr_type;
3588
3589 c.mcast_af = AF_INET6;
3590 c.mcast_group.in6 = nla_get_in6_addr(a);
3591 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3592 if (!(addr_type & IPV6_ADDR_MULTICAST))
3593 return -EINVAL;
3594#else
3595 return -EAFNOSUPPORT;
3596#endif
3597 }
3598 }
3599
3600 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3601 if (a)
3602 c.mcast_port = nla_get_u16(a);
3603
3604 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3605 if (a)
3606 c.mcast_ttl = nla_get_u8(a);
3607
3608
3609
3610
3611 if (ipvs->mixed_address_family_dests > 0)
3612 return -EINVAL;
3613
3614 ret = start_sync_thread(ipvs, &c,
3615 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3616 return ret;
3617}
3618
3619static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3620{
3621 int ret;
3622
3623 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3624 return -EINVAL;
3625
3626 ret = stop_sync_thread(ipvs,
3627 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3628 return ret;
3629}
3630
3631static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3632{
3633 struct ip_vs_timeout_user t;
3634
3635 __ip_vs_get_timeouts(ipvs, &t);
3636
3637 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3638 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3639
3640 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3641 t.tcp_fin_timeout =
3642 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3643
3644 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3645 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3646
3647 return ip_vs_set_timeout(ipvs, &t);
3648}
3649
3650static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3651{
3652 int ret = -EINVAL, cmd;
3653 struct net *net = sock_net(skb->sk);
3654 struct netns_ipvs *ipvs = net_ipvs(net);
3655
3656 cmd = info->genlhdr->cmd;
3657
3658 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3659 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3660
3661 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3662 nla_parse_nested_deprecated(daemon_attrs, IPVS_DAEMON_ATTR_MAX, info->attrs[IPVS_CMD_ATTR_DAEMON], ip_vs_daemon_policy, info->extack))
3663 goto out;
3664
3665 if (cmd == IPVS_CMD_NEW_DAEMON)
3666 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3667 else
3668 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3669 }
3670
3671out:
3672 return ret;
3673}
3674
3675static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3676{
3677 bool need_full_svc = false, need_full_dest = false;
3678 struct ip_vs_service *svc = NULL;
3679 struct ip_vs_service_user_kern usvc;
3680 struct ip_vs_dest_user_kern udest;
3681 int ret = 0, cmd;
3682 struct net *net = sock_net(skb->sk);
3683 struct netns_ipvs *ipvs = net_ipvs(net);
3684
3685 cmd = info->genlhdr->cmd;
3686
3687 mutex_lock(&__ip_vs_mutex);
3688
3689 if (cmd == IPVS_CMD_FLUSH) {
3690 ret = ip_vs_flush(ipvs, false);
3691 goto out;
3692 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3693 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3694 goto out;
3695 } else if (cmd == IPVS_CMD_ZERO &&
3696 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3697 ret = ip_vs_zero_all(ipvs);
3698 goto out;
3699 }
3700
3701
3702
3703
3704 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3705 need_full_svc = true;
3706
3707 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3708 info->attrs[IPVS_CMD_ATTR_SERVICE],
3709 need_full_svc, &svc);
3710 if (ret)
3711 goto out;
3712
3713
3714 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3715 ret = -ESRCH;
3716 goto out;
3717 }
3718
3719
3720
3721
3722 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3723 cmd == IPVS_CMD_DEL_DEST) {
3724 if (cmd != IPVS_CMD_DEL_DEST)
3725 need_full_dest = true;
3726
3727 ret = ip_vs_genl_parse_dest(&udest,
3728 info->attrs[IPVS_CMD_ATTR_DEST],
3729 need_full_dest);
3730 if (ret)
3731 goto out;
3732
3733
3734
3735
3736
3737
3738
3739 if (udest.af == 0)
3740 udest.af = svc->af;
3741
3742 if (!ip_vs_is_af_valid(udest.af)) {
3743 ret = -EAFNOSUPPORT;
3744 goto out;
3745 }
3746
3747 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3748
3749
3750
3751 if (ipvs->sync_state) {
3752 ret = -EINVAL;
3753 goto out;
3754 }
3755
3756
3757 switch (udest.conn_flags) {
3758 case IP_VS_CONN_F_TUNNEL:
3759
3760 break;
3761 default:
3762 ret = -EINVAL;
3763 goto out;
3764 }
3765 }
3766 }
3767
3768 switch (cmd) {
3769 case IPVS_CMD_NEW_SERVICE:
3770 if (svc == NULL)
3771 ret = ip_vs_add_service(ipvs, &usvc, &svc);
3772 else
3773 ret = -EEXIST;
3774 break;
3775 case IPVS_CMD_SET_SERVICE:
3776 ret = ip_vs_edit_service(svc, &usvc);
3777 break;
3778 case IPVS_CMD_DEL_SERVICE:
3779 ret = ip_vs_del_service(svc);
3780
3781 break;
3782 case IPVS_CMD_NEW_DEST:
3783 ret = ip_vs_add_dest(svc, &udest);
3784 break;
3785 case IPVS_CMD_SET_DEST:
3786 ret = ip_vs_edit_dest(svc, &udest);
3787 break;
3788 case IPVS_CMD_DEL_DEST:
3789 ret = ip_vs_del_dest(svc, &udest);
3790 break;
3791 case IPVS_CMD_ZERO:
3792 ret = ip_vs_zero_service(svc);
3793 break;
3794 default:
3795 ret = -EINVAL;
3796 }
3797
3798out:
3799 mutex_unlock(&__ip_vs_mutex);
3800
3801 return ret;
3802}
3803
3804static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3805{
3806 struct sk_buff *msg;
3807 void *reply;
3808 int ret, cmd, reply_cmd;
3809 struct net *net = sock_net(skb->sk);
3810 struct netns_ipvs *ipvs = net_ipvs(net);
3811
3812 cmd = info->genlhdr->cmd;
3813
3814 if (cmd == IPVS_CMD_GET_SERVICE)
3815 reply_cmd = IPVS_CMD_NEW_SERVICE;
3816 else if (cmd == IPVS_CMD_GET_INFO)
3817 reply_cmd = IPVS_CMD_SET_INFO;
3818 else if (cmd == IPVS_CMD_GET_CONFIG)
3819 reply_cmd = IPVS_CMD_SET_CONFIG;
3820 else {
3821 pr_err("unknown Generic Netlink command\n");
3822 return -EINVAL;
3823 }
3824
3825 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3826 if (!msg)
3827 return -ENOMEM;
3828
3829 mutex_lock(&__ip_vs_mutex);
3830
3831 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3832 if (reply == NULL)
3833 goto nla_put_failure;
3834
3835 switch (cmd) {
3836 case IPVS_CMD_GET_SERVICE:
3837 {
3838 struct ip_vs_service *svc;
3839
3840 svc = ip_vs_genl_find_service(ipvs,
3841 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3842 if (IS_ERR(svc)) {
3843 ret = PTR_ERR(svc);
3844 goto out_err;
3845 } else if (svc) {
3846 ret = ip_vs_genl_fill_service(msg, svc);
3847 if (ret)
3848 goto nla_put_failure;
3849 } else {
3850 ret = -ESRCH;
3851 goto out_err;
3852 }
3853
3854 break;
3855 }
3856
3857 case IPVS_CMD_GET_CONFIG:
3858 {
3859 struct ip_vs_timeout_user t;
3860
3861 __ip_vs_get_timeouts(ipvs, &t);
3862#ifdef CONFIG_IP_VS_PROTO_TCP
3863 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3864 t.tcp_timeout) ||
3865 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3866 t.tcp_fin_timeout))
3867 goto nla_put_failure;
3868#endif
3869#ifdef CONFIG_IP_VS_PROTO_UDP
3870 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3871 goto nla_put_failure;
3872#endif
3873
3874 break;
3875 }
3876
3877 case IPVS_CMD_GET_INFO:
3878 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3879 IP_VS_VERSION_CODE) ||
3880 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3881 ip_vs_conn_tab_size))
3882 goto nla_put_failure;
3883 break;
3884 }
3885
3886 genlmsg_end(msg, reply);
3887 ret = genlmsg_reply(msg, info);
3888 goto out;
3889
3890nla_put_failure:
3891 pr_err("not enough space in Netlink message\n");
3892 ret = -EMSGSIZE;
3893
3894out_err:
3895 nlmsg_free(msg);
3896out:
3897 mutex_unlock(&__ip_vs_mutex);
3898
3899 return ret;
3900}
3901
3902
3903static const struct genl_small_ops ip_vs_genl_ops[] = {
3904 {
3905 .cmd = IPVS_CMD_NEW_SERVICE,
3906 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3907 .flags = GENL_ADMIN_PERM,
3908 .doit = ip_vs_genl_set_cmd,
3909 },
3910 {
3911 .cmd = IPVS_CMD_SET_SERVICE,
3912 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3913 .flags = GENL_ADMIN_PERM,
3914 .doit = ip_vs_genl_set_cmd,
3915 },
3916 {
3917 .cmd = IPVS_CMD_DEL_SERVICE,
3918 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3919 .flags = GENL_ADMIN_PERM,
3920 .doit = ip_vs_genl_set_cmd,
3921 },
3922 {
3923 .cmd = IPVS_CMD_GET_SERVICE,
3924 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3925 .flags = GENL_ADMIN_PERM,
3926 .doit = ip_vs_genl_get_cmd,
3927 .dumpit = ip_vs_genl_dump_services,
3928 },
3929 {
3930 .cmd = IPVS_CMD_NEW_DEST,
3931 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3932 .flags = GENL_ADMIN_PERM,
3933 .doit = ip_vs_genl_set_cmd,
3934 },
3935 {
3936 .cmd = IPVS_CMD_SET_DEST,
3937 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3938 .flags = GENL_ADMIN_PERM,
3939 .doit = ip_vs_genl_set_cmd,
3940 },
3941 {
3942 .cmd = IPVS_CMD_DEL_DEST,
3943 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3944 .flags = GENL_ADMIN_PERM,
3945 .doit = ip_vs_genl_set_cmd,
3946 },
3947 {
3948 .cmd = IPVS_CMD_GET_DEST,
3949 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3950 .flags = GENL_ADMIN_PERM,
3951 .dumpit = ip_vs_genl_dump_dests,
3952 },
3953 {
3954 .cmd = IPVS_CMD_NEW_DAEMON,
3955 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3956 .flags = GENL_ADMIN_PERM,
3957 .doit = ip_vs_genl_set_daemon,
3958 },
3959 {
3960 .cmd = IPVS_CMD_DEL_DAEMON,
3961 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3962 .flags = GENL_ADMIN_PERM,
3963 .doit = ip_vs_genl_set_daemon,
3964 },
3965 {
3966 .cmd = IPVS_CMD_GET_DAEMON,
3967 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3968 .flags = GENL_ADMIN_PERM,
3969 .dumpit = ip_vs_genl_dump_daemons,
3970 },
3971 {
3972 .cmd = IPVS_CMD_SET_CONFIG,
3973 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3974 .flags = GENL_ADMIN_PERM,
3975 .doit = ip_vs_genl_set_cmd,
3976 },
3977 {
3978 .cmd = IPVS_CMD_GET_CONFIG,
3979 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3980 .flags = GENL_ADMIN_PERM,
3981 .doit = ip_vs_genl_get_cmd,
3982 },
3983 {
3984 .cmd = IPVS_CMD_GET_INFO,
3985 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3986 .flags = GENL_ADMIN_PERM,
3987 .doit = ip_vs_genl_get_cmd,
3988 },
3989 {
3990 .cmd = IPVS_CMD_ZERO,
3991 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3992 .flags = GENL_ADMIN_PERM,
3993 .doit = ip_vs_genl_set_cmd,
3994 },
3995 {
3996 .cmd = IPVS_CMD_FLUSH,
3997 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3998 .flags = GENL_ADMIN_PERM,
3999 .doit = ip_vs_genl_set_cmd,
4000 },
4001};
4002
4003static struct genl_family ip_vs_genl_family __ro_after_init = {
4004 .hdrsize = 0,
4005 .name = IPVS_GENL_NAME,
4006 .version = IPVS_GENL_VERSION,
4007 .maxattr = IPVS_CMD_ATTR_MAX,
4008 .policy = ip_vs_cmd_policy,
4009 .netnsok = true,
4010 .module = THIS_MODULE,
4011 .small_ops = ip_vs_genl_ops,
4012 .n_small_ops = ARRAY_SIZE(ip_vs_genl_ops),
4013};
4014
4015static int __init ip_vs_genl_register(void)
4016{
4017 return genl_register_family(&ip_vs_genl_family);
4018}
4019
4020static void ip_vs_genl_unregister(void)
4021{
4022 genl_unregister_family(&ip_vs_genl_family);
4023}
4024
4025
4026
4027
4028
4029
4030#ifdef CONFIG_SYSCTL
4031static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
4032{
4033 struct net *net = ipvs->net;
4034 int idx;
4035 struct ctl_table *tbl;
4036
4037 atomic_set(&ipvs->dropentry, 0);
4038 spin_lock_init(&ipvs->dropentry_lock);
4039 spin_lock_init(&ipvs->droppacket_lock);
4040 spin_lock_init(&ipvs->securetcp_lock);
4041
4042 if (!net_eq(net, &init_net)) {
4043 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
4044 if (tbl == NULL)
4045 return -ENOMEM;
4046
4047
4048 if (net->user_ns != &init_user_ns)
4049 tbl[0].procname = NULL;
4050 } else
4051 tbl = vs_vars;
4052
4053 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
4054 if (tbl[idx].proc_handler == proc_do_defense_mode)
4055 tbl[idx].extra2 = ipvs;
4056 }
4057 idx = 0;
4058 ipvs->sysctl_amemthresh = 1024;
4059 tbl[idx++].data = &ipvs->sysctl_amemthresh;
4060 ipvs->sysctl_am_droprate = 10;
4061 tbl[idx++].data = &ipvs->sysctl_am_droprate;
4062 tbl[idx++].data = &ipvs->sysctl_drop_entry;
4063 tbl[idx++].data = &ipvs->sysctl_drop_packet;
4064#ifdef CONFIG_IP_VS_NFCT
4065 tbl[idx++].data = &ipvs->sysctl_conntrack;
4066#endif
4067 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
4068 ipvs->sysctl_snat_reroute = 1;
4069 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
4070 ipvs->sysctl_sync_ver = 1;
4071 tbl[idx++].data = &ipvs->sysctl_sync_ver;
4072 ipvs->sysctl_sync_ports = 1;
4073 tbl[idx++].data = &ipvs->sysctl_sync_ports;
4074 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
4075 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
4076 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
4077 ipvs->sysctl_sync_sock_size = 0;
4078 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
4079 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
4080 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
4081 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
4082 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
4083 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
4084 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
4085 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
4086 tbl[idx].data = &ipvs->sysctl_sync_threshold;
4087 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
4088 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
4089 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
4090 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
4091 tbl[idx++].data = &ipvs->sysctl_sync_retries;
4092 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
4093 ipvs->sysctl_pmtu_disc = 1;
4094 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
4095 tbl[idx++].data = &ipvs->sysctl_backup_only;
4096 ipvs->sysctl_conn_reuse_mode = 1;
4097 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
4098 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
4099 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
4100 ipvs->sysctl_run_estimation = 1;
4101 tbl[idx++].data = &ipvs->sysctl_run_estimation;
4102#ifdef CONFIG_IP_VS_DEBUG
4103
4104 if (!net_eq(net, &init_net))
4105 tbl[idx++].mode = 0444;
4106#endif
4107
4108 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
4109 if (ipvs->sysctl_hdr == NULL) {
4110 if (!net_eq(net, &init_net))
4111 kfree(tbl);
4112 return -ENOMEM;
4113 }
4114 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
4115 ipvs->sysctl_tbl = tbl;
4116
4117 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
4118 queue_delayed_work(system_long_wq, &ipvs->defense_work,
4119 DEFENSE_TIMER_PERIOD);
4120
4121
4122 INIT_DELAYED_WORK(&ipvs->expire_nodest_conn_work,
4123 expire_nodest_conn_handler);
4124
4125 return 0;
4126}
4127
4128static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
4129{
4130 struct net *net = ipvs->net;
4131
4132 cancel_delayed_work_sync(&ipvs->expire_nodest_conn_work);
4133 cancel_delayed_work_sync(&ipvs->defense_work);
4134 cancel_work_sync(&ipvs->defense_work.work);
4135 unregister_net_sysctl_table(ipvs->sysctl_hdr);
4136 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
4137
4138 if (!net_eq(net, &init_net))
4139 kfree(ipvs->sysctl_tbl);
4140}
4141
4142#else
4143
4144static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
4145static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
4146
4147#endif
4148
4149static struct notifier_block ip_vs_dst_notifier = {
4150 .notifier_call = ip_vs_dst_event,
4151#ifdef CONFIG_IP_VS_IPV6
4152 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
4153#endif
4154};
4155
4156int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
4157{
4158 int i, idx;
4159
4160
4161 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
4162 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
4163
4164 INIT_LIST_HEAD(&ipvs->dest_trash);
4165 spin_lock_init(&ipvs->dest_trash_lock);
4166 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
4167 atomic_set(&ipvs->ftpsvc_counter, 0);
4168 atomic_set(&ipvs->nullsvc_counter, 0);
4169 atomic_set(&ipvs->conn_out_counter, 0);
4170
4171
4172 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
4173 if (!ipvs->tot_stats.cpustats)
4174 return -ENOMEM;
4175
4176 for_each_possible_cpu(i) {
4177 struct ip_vs_cpu_stats *ipvs_tot_stats;
4178 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4179 u64_stats_init(&ipvs_tot_stats->syncp);
4180 }
4181
4182 spin_lock_init(&ipvs->tot_stats.lock);
4183
4184#ifdef CONFIG_PROC_FS
4185 if (!proc_create_net("ip_vs", 0, ipvs->net->proc_net,
4186 &ip_vs_info_seq_ops, sizeof(struct ip_vs_iter)))
4187 goto err_vs;
4188 if (!proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4189 ip_vs_stats_show, NULL))
4190 goto err_stats;
4191 if (!proc_create_net_single("ip_vs_stats_percpu", 0,
4192 ipvs->net->proc_net,
4193 ip_vs_stats_percpu_show, NULL))
4194 goto err_percpu;
4195#endif
4196
4197 if (ip_vs_control_net_init_sysctl(ipvs))
4198 goto err;
4199
4200 return 0;
4201
4202err:
4203#ifdef CONFIG_PROC_FS
4204 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4205
4206err_percpu:
4207 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4208
4209err_stats:
4210 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4211
4212err_vs:
4213#endif
4214 free_percpu(ipvs->tot_stats.cpustats);
4215 return -ENOMEM;
4216}
4217
4218void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4219{
4220 ip_vs_trash_cleanup(ipvs);
4221 ip_vs_control_net_cleanup_sysctl(ipvs);
4222#ifdef CONFIG_PROC_FS
4223 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4224 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4225 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4226#endif
4227 free_percpu(ipvs->tot_stats.cpustats);
4228}
4229
4230int __init ip_vs_register_nl_ioctl(void)
4231{
4232 int ret;
4233
4234 ret = nf_register_sockopt(&ip_vs_sockopts);
4235 if (ret) {
4236 pr_err("cannot register sockopt.\n");
4237 goto err_sock;
4238 }
4239
4240 ret = ip_vs_genl_register();
4241 if (ret) {
4242 pr_err("cannot register Generic Netlink interface.\n");
4243 goto err_genl;
4244 }
4245 return 0;
4246
4247err_genl:
4248 nf_unregister_sockopt(&ip_vs_sockopts);
4249err_sock:
4250 return ret;
4251}
4252
4253void ip_vs_unregister_nl_ioctl(void)
4254{
4255 ip_vs_genl_unregister();
4256 nf_unregister_sockopt(&ip_vs_sockopts);
4257}
4258
4259int __init ip_vs_control_init(void)
4260{
4261 int idx;
4262 int ret;
4263
4264 EnterFunction(2);
4265
4266
4267 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4268 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4269 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4270 }
4271
4272 smp_wmb();
4273
4274 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4275 if (ret < 0)
4276 return ret;
4277
4278 LeaveFunction(2);
4279 return 0;
4280}
4281
4282
4283void ip_vs_control_cleanup(void)
4284{
4285 EnterFunction(2);
4286 unregister_netdevice_notifier(&ip_vs_dst_notifier);
4287 LeaveFunction(2);
4288}
4289