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