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