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