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