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