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