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