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
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
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.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.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>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
47 #include <net/route.h>
49 #include <net/genetlink.h>
51 #include <linux/uaccess.h>
53 #include <net/ip_vs.h>
55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56 static DEFINE_MUTEX(__ip_vs_mutex);
58 /* sysctl variables */
60 #ifdef CONFIG_IP_VS_DEBUG
61 static int sysctl_ip_vs_debug_level = 0;
63 int ip_vs_get_debug_level(void)
65 return sysctl_ip_vs_debug_level;
71 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
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)
82 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
85 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
94 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
97 static void update_defense_level(struct netns_ipvs *ipvs)
100 static int old_secure_tcp = 0;
105 /* we only count free and buffered memory (in pages) */
107 availmem = i.freeram + i.bufferram;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem = (availmem < ipvs->sysctl_amemthresh);
118 spin_lock(&ipvs->dropentry_lock);
119 switch (ipvs->sysctl_drop_entry) {
121 atomic_set(&ipvs->dropentry, 0);
125 atomic_set(&ipvs->dropentry, 1);
126 ipvs->sysctl_drop_entry = 2;
128 atomic_set(&ipvs->dropentry, 0);
133 atomic_set(&ipvs->dropentry, 1);
135 atomic_set(&ipvs->dropentry, 0);
136 ipvs->sysctl_drop_entry = 1;
140 atomic_set(&ipvs->dropentry, 1);
143 spin_unlock(&ipvs->dropentry_lock);
146 spin_lock(&ipvs->droppacket_lock);
147 switch (ipvs->sysctl_drop_packet) {
153 ipvs->drop_rate = ipvs->drop_counter
154 = ipvs->sysctl_amemthresh /
155 (ipvs->sysctl_amemthresh-availmem);
156 ipvs->sysctl_drop_packet = 2;
163 ipvs->drop_rate = ipvs->drop_counter
164 = ipvs->sysctl_amemthresh /
165 (ipvs->sysctl_amemthresh-availmem);
168 ipvs->sysctl_drop_packet = 1;
172 ipvs->drop_rate = ipvs->sysctl_am_droprate;
175 spin_unlock(&ipvs->droppacket_lock);
178 spin_lock(&ipvs->securetcp_lock);
179 switch (ipvs->sysctl_secure_tcp) {
181 if (old_secure_tcp >= 2)
186 if (old_secure_tcp < 2)
188 ipvs->sysctl_secure_tcp = 2;
190 if (old_secure_tcp >= 2)
196 if (old_secure_tcp < 2)
199 if (old_secure_tcp >= 2)
201 ipvs->sysctl_secure_tcp = 1;
205 if (old_secure_tcp < 2)
209 old_secure_tcp = ipvs->sysctl_secure_tcp;
211 ip_vs_protocol_timeout_change(ipvs,
212 ipvs->sysctl_secure_tcp > 1);
213 spin_unlock(&ipvs->securetcp_lock);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct *work)
226 struct netns_ipvs *ipvs =
227 container_of(work, struct netns_ipvs, defense_work.work);
229 update_defense_level(ipvs);
230 if (atomic_read(&ipvs->dropentry))
231 ip_vs_random_dropentry(ipvs);
232 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE);
250 * Hash table: for virtual service lookups
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)
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];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
267 const union nf_inet_addr *addr, __be16 port)
269 register unsigned int porth = ntohs(port);
270 __be32 addr_fold = addr->ip;
273 #ifdef CONFIG_IP_VS_IPV6
275 addr_fold = addr->ip6[0]^addr->ip6[1]^
276 addr->ip6[2]^addr->ip6[3];
278 ahash = ntohl(addr_fold);
279 ahash ^= ((size_t) ipvs >> 8);
281 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
286 * Returns hash value of fwmark for virtual service lookup
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
290 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
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.
298 static int ip_vs_svc_hash(struct ip_vs_service *svc)
302 if (svc->flags & IP_VS_SVC_F_HASHED) {
303 pr_err("%s(): request for already hashed, called from %pS\n",
304 __func__, __builtin_return_address(0));
308 if (svc->fwmark == 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
313 &svc->addr, svc->port);
314 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
317 * Hash it by fwmark in svc_fwm_table
319 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
320 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
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);
331 * Unhashes a service from svc_table / svc_fwm_table.
332 * Should be called with locked tables.
334 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
336 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
337 pr_err("%s(): request for unhash flagged, called from %pS\n",
338 __func__, __builtin_return_address(0));
342 if (svc->fwmark == 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc->s_list);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc->f_list);
350 svc->flags &= ~IP_VS_SVC_F_HASHED;
351 atomic_dec(&svc->refcnt);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service *
360 __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
361 const union nf_inet_addr *vaddr, __be16 vport)
364 struct ip_vs_service *svc;
366 /* Check for "full" addressed entries */
367 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
369 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
371 && ip_vs_addr_equal(af, &svc->addr, vaddr)
372 && (svc->port == vport)
373 && (svc->protocol == protocol)
374 && (svc->ipvs == ipvs)) {
385 * Get service by {fwmark} in the service table.
387 static inline struct ip_vs_service *
388 __ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
391 struct ip_vs_service *svc;
393 /* Check for fwmark addressed entries */
394 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
396 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
397 if (svc->fwmark == fwmark && svc->af == af
398 && (svc->ipvs == ipvs)) {
407 /* Find service, called under RCU lock */
408 struct ip_vs_service *
409 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
410 const union nf_inet_addr *vaddr, __be16 vport)
412 struct ip_vs_service *svc;
415 * Check the table hashed by fwmark first
418 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
424 * Check the table hashed by <protocol,addr,port>
425 * for "full" addressed entries
427 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
429 if (!svc && protocol == IPPROTO_TCP &&
430 atomic_read(&ipvs->ftpsvc_counter) &&
431 (vport == FTPDATA || ntohs(vport) >= inet_prot_sock(ipvs->net))) {
433 * Check if ftp service entry exists, the packet
434 * might belong to FTP data connections.
436 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
440 && atomic_read(&ipvs->nullsvc_counter)) {
442 * Check if the catch-all port (port zero) exists
444 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
448 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
449 fwmark, ip_vs_proto_name(protocol),
450 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
451 svc ? "hit" : "not hit");
458 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
460 atomic_inc(&svc->refcnt);
461 rcu_assign_pointer(dest->svc, svc);
464 static void ip_vs_service_free(struct ip_vs_service *svc)
466 free_percpu(svc->stats.cpustats);
470 static void ip_vs_service_rcu_free(struct rcu_head *head)
472 struct ip_vs_service *svc;
474 svc = container_of(head, struct ip_vs_service, rcu_head);
475 ip_vs_service_free(svc);
478 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
480 if (atomic_dec_and_test(&svc->refcnt)) {
481 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
483 IP_VS_DBG_ADDR(svc->af, &svc->addr),
486 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
488 ip_vs_service_free(svc);
494 * Returns hash value for real service
496 static inline unsigned int ip_vs_rs_hashkey(int af,
497 const union nf_inet_addr *addr,
500 register unsigned int porth = ntohs(port);
501 __be32 addr_fold = addr->ip;
503 #ifdef CONFIG_IP_VS_IPV6
505 addr_fold = addr->ip6[0]^addr->ip6[1]^
506 addr->ip6[2]^addr->ip6[3];
509 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
513 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
514 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
518 if (dest->in_rs_table)
522 * Hash by proto,addr,port,
523 * which are the parameters of the real service.
525 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
527 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
528 dest->in_rs_table = 1;
531 /* Unhash ip_vs_dest from rs_table. */
532 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
535 * Remove it from the rs_table table.
537 if (dest->in_rs_table) {
538 hlist_del_rcu(&dest->d_list);
539 dest->in_rs_table = 0;
543 /* Check if real service by <proto,addr,port> is present */
544 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
545 const union nf_inet_addr *daddr, __be16 dport)
548 struct ip_vs_dest *dest;
550 /* Check for "full" addressed entries */
551 hash = ip_vs_rs_hashkey(af, daddr, dport);
553 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
554 if (dest->port == dport &&
556 ip_vs_addr_equal(af, &dest->addr, daddr) &&
557 (dest->protocol == protocol || dest->vfwmark)) {
566 /* Find real service record by <proto,addr,port>.
567 * In case of multiple records with the same <proto,addr,port>, only
568 * the first found record is returned.
570 * To be called under RCU lock.
572 struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
574 const union nf_inet_addr *daddr,
578 struct ip_vs_dest *dest;
580 /* Check for "full" addressed entries */
581 hash = ip_vs_rs_hashkey(af, daddr, dport);
583 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
584 if (dest->port == dport &&
586 ip_vs_addr_equal(af, &dest->addr, daddr) &&
587 (dest->protocol == protocol || dest->vfwmark)) {
596 /* Lookup destination by {addr,port} in the given service
597 * Called under RCU lock.
599 static struct ip_vs_dest *
600 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
601 const union nf_inet_addr *daddr, __be16 dport)
603 struct ip_vs_dest *dest;
606 * Find the destination for the given service
608 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
609 if ((dest->af == dest_af) &&
610 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
611 (dest->port == dport)) {
621 * Find destination by {daddr,dport,vaddr,protocol}
622 * Created to be used in ip_vs_process_message() in
623 * the backup synchronization daemon. It finds the
624 * destination to be bound to the received connection
626 * Called under RCU lock, no refcnt is returned.
628 struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
629 const union nf_inet_addr *daddr,
631 const union nf_inet_addr *vaddr,
632 __be16 vport, __u16 protocol, __u32 fwmark,
635 struct ip_vs_dest *dest;
636 struct ip_vs_service *svc;
639 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
642 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
644 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
646 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
650 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
652 struct ip_vs_dest_dst *dest_dst = container_of(head,
653 struct ip_vs_dest_dst,
656 dst_release(dest_dst->dst_cache);
660 /* Release dest_dst and dst_cache for dest in user context */
661 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
663 struct ip_vs_dest_dst *old;
665 old = rcu_dereference_protected(dest->dest_dst, 1);
667 RCU_INIT_POINTER(dest->dest_dst, NULL);
668 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
673 * Lookup dest by {svc,addr,port} in the destination trash.
674 * The destination trash is used to hold the destinations that are removed
675 * from the service table but are still referenced by some conn entries.
676 * The reason to add the destination trash is when the dest is temporary
677 * down (either by administrator or by monitor program), the dest can be
678 * picked back from the trash, the remaining connections to the dest can
679 * continue, and the counting information of the dest is also useful for
682 static struct ip_vs_dest *
683 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
684 const union nf_inet_addr *daddr, __be16 dport)
686 struct ip_vs_dest *dest;
687 struct netns_ipvs *ipvs = svc->ipvs;
690 * Find the destination in trash
692 spin_lock_bh(&ipvs->dest_trash_lock);
693 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
694 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
697 IP_VS_DBG_ADDR(dest->af, &dest->addr),
699 refcount_read(&dest->refcnt));
700 if (dest->af == dest_af &&
701 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
702 dest->port == dport &&
703 dest->vfwmark == svc->fwmark &&
704 dest->protocol == svc->protocol &&
706 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
707 dest->vport == svc->port))) {
709 list_del(&dest->t_list);
717 spin_unlock_bh(&ipvs->dest_trash_lock);
722 static void ip_vs_dest_free(struct ip_vs_dest *dest)
724 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
726 __ip_vs_dst_cache_reset(dest);
727 __ip_vs_svc_put(svc, false);
728 free_percpu(dest->stats.cpustats);
729 ip_vs_dest_put_and_free(dest);
733 * Clean up all the destinations in the trash
734 * Called by the ip_vs_control_cleanup()
736 * When the ip_vs_control_clearup is activated by ipvs module exit,
737 * the service tables must have been flushed and all the connections
738 * are expired, and the refcnt of each destination in the trash must
739 * be 1, so we simply release them here.
741 static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
743 struct ip_vs_dest *dest, *nxt;
745 del_timer_sync(&ipvs->dest_trash_timer);
746 /* No need to use dest_trash_lock */
747 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
748 list_del(&dest->t_list);
749 ip_vs_dest_free(dest);
754 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
756 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
758 spin_lock_bh(&src->lock);
760 IP_VS_SHOW_STATS_COUNTER(conns);
761 IP_VS_SHOW_STATS_COUNTER(inpkts);
762 IP_VS_SHOW_STATS_COUNTER(outpkts);
763 IP_VS_SHOW_STATS_COUNTER(inbytes);
764 IP_VS_SHOW_STATS_COUNTER(outbytes);
766 ip_vs_read_estimator(dst, src);
768 spin_unlock_bh(&src->lock);
772 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
774 dst->conns = (u32)src->conns;
775 dst->inpkts = (u32)src->inpkts;
776 dst->outpkts = (u32)src->outpkts;
777 dst->inbytes = src->inbytes;
778 dst->outbytes = src->outbytes;
779 dst->cps = (u32)src->cps;
780 dst->inpps = (u32)src->inpps;
781 dst->outpps = (u32)src->outpps;
782 dst->inbps = (u32)src->inbps;
783 dst->outbps = (u32)src->outbps;
787 ip_vs_zero_stats(struct ip_vs_stats *stats)
789 spin_lock_bh(&stats->lock);
791 /* get current counters as zero point, rates are zeroed */
793 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
795 IP_VS_ZERO_STATS_COUNTER(conns);
796 IP_VS_ZERO_STATS_COUNTER(inpkts);
797 IP_VS_ZERO_STATS_COUNTER(outpkts);
798 IP_VS_ZERO_STATS_COUNTER(inbytes);
799 IP_VS_ZERO_STATS_COUNTER(outbytes);
801 ip_vs_zero_estimator(stats);
803 spin_unlock_bh(&stats->lock);
807 * Update a destination in the given service
810 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
811 struct ip_vs_dest_user_kern *udest, int add)
813 struct netns_ipvs *ipvs = svc->ipvs;
814 struct ip_vs_service *old_svc;
815 struct ip_vs_scheduler *sched;
818 /* We cannot modify an address and change the address family */
819 BUG_ON(!add && udest->af != dest->af);
821 if (add && udest->af != svc->af)
822 ipvs->mixed_address_family_dests++;
824 /* set the weight and the flags */
825 atomic_set(&dest->weight, udest->weight);
826 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
827 conn_flags |= IP_VS_CONN_F_INACTIVE;
829 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
830 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
831 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
834 * Put the real service in rs_table if not present.
835 * For now only for NAT!
837 ip_vs_rs_hash(ipvs, dest);
839 atomic_set(&dest->conn_flags, conn_flags);
841 /* bind the service */
842 old_svc = rcu_dereference_protected(dest->svc, 1);
844 __ip_vs_bind_svc(dest, svc);
846 if (old_svc != svc) {
847 ip_vs_zero_stats(&dest->stats);
848 __ip_vs_bind_svc(dest, svc);
849 __ip_vs_svc_put(old_svc, true);
853 /* set the dest status flags */
854 dest->flags |= IP_VS_DEST_F_AVAILABLE;
856 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
857 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
858 dest->u_threshold = udest->u_threshold;
859 dest->l_threshold = udest->l_threshold;
861 dest->af = udest->af;
863 spin_lock_bh(&dest->dst_lock);
864 __ip_vs_dst_cache_reset(dest);
865 spin_unlock_bh(&dest->dst_lock);
868 ip_vs_start_estimator(svc->ipvs, &dest->stats);
869 list_add_rcu(&dest->n_list, &svc->destinations);
871 sched = rcu_dereference_protected(svc->scheduler, 1);
872 if (sched && sched->add_dest)
873 sched->add_dest(svc, dest);
875 sched = rcu_dereference_protected(svc->scheduler, 1);
876 if (sched && sched->upd_dest)
877 sched->upd_dest(svc, dest);
883 * Create a destination for the given service
886 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
887 struct ip_vs_dest **dest_p)
889 struct ip_vs_dest *dest;
890 unsigned int atype, i;
894 #ifdef CONFIG_IP_VS_IPV6
895 if (udest->af == AF_INET6) {
896 atype = ipv6_addr_type(&udest->addr.in6);
897 if ((!(atype & IPV6_ADDR_UNICAST) ||
898 atype & IPV6_ADDR_LINKLOCAL) &&
899 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
904 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
905 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
909 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
913 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
914 if (!dest->stats.cpustats)
917 for_each_possible_cpu(i) {
918 struct ip_vs_cpu_stats *ip_vs_dest_stats;
919 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
920 u64_stats_init(&ip_vs_dest_stats->syncp);
923 dest->af = udest->af;
924 dest->protocol = svc->protocol;
925 dest->vaddr = svc->addr;
926 dest->vport = svc->port;
927 dest->vfwmark = svc->fwmark;
928 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
929 dest->port = udest->port;
931 atomic_set(&dest->activeconns, 0);
932 atomic_set(&dest->inactconns, 0);
933 atomic_set(&dest->persistconns, 0);
934 refcount_set(&dest->refcnt, 1);
936 INIT_HLIST_NODE(&dest->d_list);
937 spin_lock_init(&dest->dst_lock);
938 spin_lock_init(&dest->stats.lock);
939 __ip_vs_update_dest(svc, dest, udest, 1);
953 * Add a destination into an existing service
956 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
958 struct ip_vs_dest *dest;
959 union nf_inet_addr daddr;
960 __be16 dport = udest->port;
965 if (udest->weight < 0) {
966 pr_err("%s(): server weight less than zero\n", __func__);
970 if (udest->l_threshold > udest->u_threshold) {
971 pr_err("%s(): lower threshold is higher than upper threshold\n",
976 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
978 /* We use function that requires RCU lock */
980 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
984 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
989 * Check if the dest already exists in the trash and
990 * is from the same service
992 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
995 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
996 "dest->refcnt=%d, service %u/%s:%u\n",
997 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
998 refcount_read(&dest->refcnt),
1000 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1001 ntohs(dest->vport));
1003 __ip_vs_update_dest(svc, dest, udest, 1);
1007 * Allocate and initialize the dest structure
1009 ret = ip_vs_new_dest(svc, udest, &dest);
1018 * Edit a destination in the given service
1021 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1023 struct ip_vs_dest *dest;
1024 union nf_inet_addr daddr;
1025 __be16 dport = udest->port;
1029 if (udest->weight < 0) {
1030 pr_err("%s(): server weight less than zero\n", __func__);
1034 if (udest->l_threshold > udest->u_threshold) {
1035 pr_err("%s(): lower threshold is higher than upper threshold\n",
1040 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1042 /* We use function that requires RCU lock */
1044 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1048 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1052 __ip_vs_update_dest(svc, dest, udest, 0);
1059 * Delete a destination (must be already unlinked from the service)
1061 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1064 ip_vs_stop_estimator(ipvs, &dest->stats);
1067 * Remove it from the d-linked list with the real services.
1069 ip_vs_rs_unhash(dest);
1071 spin_lock_bh(&ipvs->dest_trash_lock);
1072 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1073 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1074 refcount_read(&dest->refcnt));
1075 if (list_empty(&ipvs->dest_trash) && !cleanup)
1076 mod_timer(&ipvs->dest_trash_timer,
1077 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1078 /* dest lives in trash with reference */
1079 list_add(&dest->t_list, &ipvs->dest_trash);
1080 dest->idle_start = 0;
1081 spin_unlock_bh(&ipvs->dest_trash_lock);
1086 * Unlink a destination from the given service
1088 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1089 struct ip_vs_dest *dest,
1092 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1095 * Remove it from the d-linked destination list.
1097 list_del_rcu(&dest->n_list);
1100 if (dest->af != svc->af)
1101 svc->ipvs->mixed_address_family_dests--;
1104 struct ip_vs_scheduler *sched;
1106 sched = rcu_dereference_protected(svc->scheduler, 1);
1107 if (sched && sched->del_dest)
1108 sched->del_dest(svc, dest);
1114 * Delete a destination server in the given service
1117 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1119 struct ip_vs_dest *dest;
1120 __be16 dport = udest->port;
1124 /* We use function that requires RCU lock */
1126 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1130 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1135 * Unlink dest from the service
1137 __ip_vs_unlink_dest(svc, dest, 1);
1140 * Delete the destination
1142 __ip_vs_del_dest(svc->ipvs, dest, false);
1149 static void ip_vs_dest_trash_expire(struct timer_list *t)
1151 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1152 struct ip_vs_dest *dest, *next;
1153 unsigned long now = jiffies;
1155 spin_lock(&ipvs->dest_trash_lock);
1156 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1157 if (refcount_read(&dest->refcnt) > 1)
1159 if (dest->idle_start) {
1160 if (time_before(now, dest->idle_start +
1161 IP_VS_DEST_TRASH_PERIOD))
1164 dest->idle_start = max(1UL, now);
1167 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1169 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1171 list_del(&dest->t_list);
1172 ip_vs_dest_free(dest);
1174 if (!list_empty(&ipvs->dest_trash))
1175 mod_timer(&ipvs->dest_trash_timer,
1176 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1177 spin_unlock(&ipvs->dest_trash_lock);
1181 * Add a service into the service hash table
1184 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1185 struct ip_vs_service **svc_p)
1188 struct ip_vs_scheduler *sched = NULL;
1189 struct ip_vs_pe *pe = NULL;
1190 struct ip_vs_service *svc = NULL;
1192 /* increase the module use count */
1193 ip_vs_use_count_inc();
1195 /* Lookup the scheduler by 'u->sched_name' */
1196 if (strcmp(u->sched_name, "none")) {
1197 sched = ip_vs_scheduler_get(u->sched_name);
1199 pr_info("Scheduler module ip_vs_%s not found\n",
1206 if (u->pe_name && *u->pe_name) {
1207 pe = ip_vs_pe_getbyname(u->pe_name);
1209 pr_info("persistence engine module ip_vs_pe_%s "
1210 "not found\n", u->pe_name);
1216 #ifdef CONFIG_IP_VS_IPV6
1217 if (u->af == AF_INET6) {
1218 __u32 plen = (__force __u32) u->netmask;
1220 if (plen < 1 || plen > 128) {
1227 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1229 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1233 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1234 if (!svc->stats.cpustats) {
1239 for_each_possible_cpu(i) {
1240 struct ip_vs_cpu_stats *ip_vs_stats;
1241 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1242 u64_stats_init(&ip_vs_stats->syncp);
1246 /* I'm the first user of the service */
1247 atomic_set(&svc->refcnt, 0);
1250 svc->protocol = u->protocol;
1251 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1252 svc->port = u->port;
1253 svc->fwmark = u->fwmark;
1254 svc->flags = u->flags;
1255 svc->timeout = u->timeout * HZ;
1256 svc->netmask = u->netmask;
1259 INIT_LIST_HEAD(&svc->destinations);
1260 spin_lock_init(&svc->sched_lock);
1261 spin_lock_init(&svc->stats.lock);
1263 /* Bind the scheduler */
1265 ret = ip_vs_bind_scheduler(svc, sched);
1271 /* Bind the ct retriever */
1272 RCU_INIT_POINTER(svc->pe, pe);
1275 /* Update the virtual service counters */
1276 if (svc->port == FTPPORT)
1277 atomic_inc(&ipvs->ftpsvc_counter);
1278 else if (svc->port == 0)
1279 atomic_inc(&ipvs->nullsvc_counter);
1280 if (svc->pe && svc->pe->conn_out)
1281 atomic_inc(&ipvs->conn_out_counter);
1283 ip_vs_start_estimator(ipvs, &svc->stats);
1285 /* Count only IPv4 services for old get/setsockopt interface */
1286 if (svc->af == AF_INET)
1287 ipvs->num_services++;
1289 /* Hash the service into the service table */
1290 ip_vs_svc_hash(svc);
1293 /* Now there is a service - full throttle */
1300 ip_vs_unbind_scheduler(svc, sched);
1301 ip_vs_service_free(svc);
1303 ip_vs_scheduler_put(sched);
1306 /* decrease the module use count */
1307 ip_vs_use_count_dec();
1314 * Edit a service and bind it with a new scheduler
1317 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1319 struct ip_vs_scheduler *sched = NULL, *old_sched;
1320 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1322 bool new_pe_conn_out, old_pe_conn_out;
1325 * Lookup the scheduler, by 'u->sched_name'
1327 if (strcmp(u->sched_name, "none")) {
1328 sched = ip_vs_scheduler_get(u->sched_name);
1330 pr_info("Scheduler module ip_vs_%s not found\n",
1337 if (u->pe_name && *u->pe_name) {
1338 pe = ip_vs_pe_getbyname(u->pe_name);
1340 pr_info("persistence engine module ip_vs_pe_%s "
1341 "not found\n", u->pe_name);
1348 #ifdef CONFIG_IP_VS_IPV6
1349 if (u->af == AF_INET6) {
1350 __u32 plen = (__force __u32) u->netmask;
1352 if (plen < 1 || plen > 128) {
1359 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1360 if (sched != old_sched) {
1362 ip_vs_unbind_scheduler(svc, old_sched);
1363 RCU_INIT_POINTER(svc->scheduler, NULL);
1364 /* Wait all svc->sched_data users */
1367 /* Bind the new scheduler */
1369 ret = ip_vs_bind_scheduler(svc, sched);
1371 ip_vs_scheduler_put(sched);
1378 * Set the flags and timeout value
1380 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1381 svc->timeout = u->timeout * HZ;
1382 svc->netmask = u->netmask;
1384 old_pe = rcu_dereference_protected(svc->pe, 1);
1386 rcu_assign_pointer(svc->pe, pe);
1387 /* check for optional methods in new pe */
1388 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1389 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1390 if (new_pe_conn_out && !old_pe_conn_out)
1391 atomic_inc(&svc->ipvs->conn_out_counter);
1392 if (old_pe_conn_out && !new_pe_conn_out)
1393 atomic_dec(&svc->ipvs->conn_out_counter);
1397 ip_vs_scheduler_put(old_sched);
1398 ip_vs_pe_put(old_pe);
1403 * Delete a service from the service list
1404 * - The service must be unlinked, unlocked and not referenced!
1405 * - We are called under _bh lock
1407 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1409 struct ip_vs_dest *dest, *nxt;
1410 struct ip_vs_scheduler *old_sched;
1411 struct ip_vs_pe *old_pe;
1412 struct netns_ipvs *ipvs = svc->ipvs;
1414 /* Count only IPv4 services for old get/setsockopt interface */
1415 if (svc->af == AF_INET)
1416 ipvs->num_services--;
1418 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1420 /* Unbind scheduler */
1421 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1422 ip_vs_unbind_scheduler(svc, old_sched);
1423 ip_vs_scheduler_put(old_sched);
1425 /* Unbind persistence engine, keep svc->pe */
1426 old_pe = rcu_dereference_protected(svc->pe, 1);
1427 if (old_pe && old_pe->conn_out)
1428 atomic_dec(&ipvs->conn_out_counter);
1429 ip_vs_pe_put(old_pe);
1432 * Unlink the whole destination list
1434 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1435 __ip_vs_unlink_dest(svc, dest, 0);
1436 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1440 * Update the virtual service counters
1442 if (svc->port == FTPPORT)
1443 atomic_dec(&ipvs->ftpsvc_counter);
1444 else if (svc->port == 0)
1445 atomic_dec(&ipvs->nullsvc_counter);
1448 * Free the service if nobody refers to it
1450 __ip_vs_svc_put(svc, true);
1452 /* decrease the module use count */
1453 ip_vs_use_count_dec();
1457 * Unlink a service from list and try to delete it if its refcnt reached 0
1459 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1461 /* Hold svc to avoid double release from dest_trash */
1462 atomic_inc(&svc->refcnt);
1464 * Unhash it from the service table
1466 ip_vs_svc_unhash(svc);
1468 __ip_vs_del_service(svc, cleanup);
1472 * Delete a service from the service list
1474 static int ip_vs_del_service(struct ip_vs_service *svc)
1478 ip_vs_unlink_service(svc, false);
1485 * Flush all the virtual services
1487 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1490 struct ip_vs_service *svc;
1491 struct hlist_node *n;
1494 * Flush the service table hashed by <netns,protocol,addr,port>
1496 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1497 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1499 if (svc->ipvs == ipvs)
1500 ip_vs_unlink_service(svc, cleanup);
1505 * Flush the service table hashed by fwmark
1507 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1508 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1510 if (svc->ipvs == ipvs)
1511 ip_vs_unlink_service(svc, cleanup);
1519 * Delete service by {netns} in the service table.
1520 * Called by __ip_vs_cleanup()
1522 void ip_vs_service_net_cleanup(struct netns_ipvs *ipvs)
1525 /* Check for "full" addressed entries */
1526 mutex_lock(&__ip_vs_mutex);
1527 ip_vs_flush(ipvs, true);
1528 mutex_unlock(&__ip_vs_mutex);
1532 /* Put all references for device (dst_cache) */
1534 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1536 struct ip_vs_dest_dst *dest_dst;
1538 spin_lock_bh(&dest->dst_lock);
1539 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1540 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1541 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1543 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1545 refcount_read(&dest->refcnt));
1546 __ip_vs_dst_cache_reset(dest);
1548 spin_unlock_bh(&dest->dst_lock);
1551 /* Netdev event receiver
1552 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1554 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1557 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1558 struct net *net = dev_net(dev);
1559 struct netns_ipvs *ipvs = net_ipvs(net);
1560 struct ip_vs_service *svc;
1561 struct ip_vs_dest *dest;
1564 if (event != NETDEV_DOWN || !ipvs)
1566 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1568 mutex_lock(&__ip_vs_mutex);
1569 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1570 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1571 if (svc->ipvs == ipvs) {
1572 list_for_each_entry(dest, &svc->destinations,
1574 ip_vs_forget_dev(dest, dev);
1579 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1580 if (svc->ipvs == ipvs) {
1581 list_for_each_entry(dest, &svc->destinations,
1583 ip_vs_forget_dev(dest, dev);
1590 spin_lock_bh(&ipvs->dest_trash_lock);
1591 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1592 ip_vs_forget_dev(dest, dev);
1594 spin_unlock_bh(&ipvs->dest_trash_lock);
1595 mutex_unlock(&__ip_vs_mutex);
1601 * Zero counters in a service or all services
1603 static int ip_vs_zero_service(struct ip_vs_service *svc)
1605 struct ip_vs_dest *dest;
1607 list_for_each_entry(dest, &svc->destinations, n_list) {
1608 ip_vs_zero_stats(&dest->stats);
1610 ip_vs_zero_stats(&svc->stats);
1614 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1617 struct ip_vs_service *svc;
1619 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1620 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1621 if (svc->ipvs == ipvs)
1622 ip_vs_zero_service(svc);
1626 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1627 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1628 if (svc->ipvs == ipvs)
1629 ip_vs_zero_service(svc);
1633 ip_vs_zero_stats(&ipvs->tot_stats);
1637 #ifdef CONFIG_SYSCTL
1640 static int three = 3;
1643 proc_do_defense_mode(struct ctl_table *table, int write,
1644 void __user *buffer, size_t *lenp, loff_t *ppos)
1646 struct netns_ipvs *ipvs = table->extra2;
1647 int *valp = table->data;
1651 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1652 if (write && (*valp != val)) {
1653 if ((*valp < 0) || (*valp > 3)) {
1654 /* Restore the correct value */
1657 update_defense_level(ipvs);
1664 proc_do_sync_threshold(struct ctl_table *table, int write,
1665 void __user *buffer, size_t *lenp, loff_t *ppos)
1667 int *valp = table->data;
1671 /* backup the value first */
1672 memcpy(val, valp, sizeof(val));
1674 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1675 if (write && (valp[0] < 0 || valp[1] < 0 ||
1676 (valp[0] >= valp[1] && valp[1]))) {
1677 /* Restore the correct value */
1678 memcpy(valp, val, sizeof(val));
1684 proc_do_sync_mode(struct ctl_table *table, int write,
1685 void __user *buffer, size_t *lenp, loff_t *ppos)
1687 int *valp = table->data;
1691 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1692 if (write && (*valp != val)) {
1693 if ((*valp < 0) || (*valp > 1)) {
1694 /* Restore the correct value */
1702 proc_do_sync_ports(struct ctl_table *table, int write,
1703 void __user *buffer, size_t *lenp, loff_t *ppos)
1705 int *valp = table->data;
1709 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1710 if (write && (*valp != val)) {
1711 if (*valp < 1 || !is_power_of_2(*valp)) {
1712 /* Restore the correct value */
1720 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1721 * Do not change order or insert new entries without
1722 * align with netns init in ip_vs_control_net_init()
1725 static struct ctl_table vs_vars[] = {
1727 .procname = "amemthresh",
1728 .maxlen = sizeof(int),
1730 .proc_handler = proc_dointvec,
1733 .procname = "am_droprate",
1734 .maxlen = sizeof(int),
1736 .proc_handler = proc_dointvec,
1739 .procname = "drop_entry",
1740 .maxlen = sizeof(int),
1742 .proc_handler = proc_do_defense_mode,
1745 .procname = "drop_packet",
1746 .maxlen = sizeof(int),
1748 .proc_handler = proc_do_defense_mode,
1750 #ifdef CONFIG_IP_VS_NFCT
1752 .procname = "conntrack",
1753 .maxlen = sizeof(int),
1755 .proc_handler = &proc_dointvec,
1759 .procname = "secure_tcp",
1760 .maxlen = sizeof(int),
1762 .proc_handler = proc_do_defense_mode,
1765 .procname = "snat_reroute",
1766 .maxlen = sizeof(int),
1768 .proc_handler = &proc_dointvec,
1771 .procname = "sync_version",
1772 .maxlen = sizeof(int),
1774 .proc_handler = proc_do_sync_mode,
1777 .procname = "sync_ports",
1778 .maxlen = sizeof(int),
1780 .proc_handler = proc_do_sync_ports,
1783 .procname = "sync_persist_mode",
1784 .maxlen = sizeof(int),
1786 .proc_handler = proc_dointvec,
1789 .procname = "sync_qlen_max",
1790 .maxlen = sizeof(unsigned long),
1792 .proc_handler = proc_doulongvec_minmax,
1795 .procname = "sync_sock_size",
1796 .maxlen = sizeof(int),
1798 .proc_handler = proc_dointvec,
1801 .procname = "cache_bypass",
1802 .maxlen = sizeof(int),
1804 .proc_handler = proc_dointvec,
1807 .procname = "expire_nodest_conn",
1808 .maxlen = sizeof(int),
1810 .proc_handler = proc_dointvec,
1813 .procname = "sloppy_tcp",
1814 .maxlen = sizeof(int),
1816 .proc_handler = proc_dointvec,
1819 .procname = "sloppy_sctp",
1820 .maxlen = sizeof(int),
1822 .proc_handler = proc_dointvec,
1825 .procname = "expire_quiescent_template",
1826 .maxlen = sizeof(int),
1828 .proc_handler = proc_dointvec,
1831 .procname = "sync_threshold",
1833 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1835 .proc_handler = proc_do_sync_threshold,
1838 .procname = "sync_refresh_period",
1839 .maxlen = sizeof(int),
1841 .proc_handler = proc_dointvec_jiffies,
1844 .procname = "sync_retries",
1845 .maxlen = sizeof(int),
1847 .proc_handler = proc_dointvec_minmax,
1852 .procname = "nat_icmp_send",
1853 .maxlen = sizeof(int),
1855 .proc_handler = proc_dointvec,
1858 .procname = "pmtu_disc",
1859 .maxlen = sizeof(int),
1861 .proc_handler = proc_dointvec,
1864 .procname = "backup_only",
1865 .maxlen = sizeof(int),
1867 .proc_handler = proc_dointvec,
1870 .procname = "conn_reuse_mode",
1871 .maxlen = sizeof(int),
1873 .proc_handler = proc_dointvec,
1876 .procname = "schedule_icmp",
1877 .maxlen = sizeof(int),
1879 .proc_handler = proc_dointvec,
1882 .procname = "ignore_tunneled",
1883 .maxlen = sizeof(int),
1885 .proc_handler = proc_dointvec,
1887 #ifdef CONFIG_IP_VS_DEBUG
1889 .procname = "debug_level",
1890 .data = &sysctl_ip_vs_debug_level,
1891 .maxlen = sizeof(int),
1893 .proc_handler = proc_dointvec,
1901 #ifdef CONFIG_PROC_FS
1904 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1905 struct hlist_head *table;
1910 * Write the contents of the VS rule table to a PROCfs file.
1911 * (It is kept just for backward compatibility)
1913 static inline const char *ip_vs_fwd_name(unsigned int flags)
1915 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1916 case IP_VS_CONN_F_LOCALNODE:
1918 case IP_VS_CONN_F_TUNNEL:
1920 case IP_VS_CONN_F_DROUTE:
1928 /* Get the Nth entry in the two lists */
1929 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1931 struct net *net = seq_file_net(seq);
1932 struct netns_ipvs *ipvs = net_ipvs(net);
1933 struct ip_vs_iter *iter = seq->private;
1935 struct ip_vs_service *svc;
1937 /* look in hash by protocol */
1938 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1939 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1940 if ((svc->ipvs == ipvs) && pos-- == 0) {
1941 iter->table = ip_vs_svc_table;
1948 /* keep looking in fwmark */
1949 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1950 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1952 if ((svc->ipvs == ipvs) && pos-- == 0) {
1953 iter->table = ip_vs_svc_fwm_table;
1963 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1967 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1971 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1973 struct hlist_node *e;
1974 struct ip_vs_iter *iter;
1975 struct ip_vs_service *svc;
1978 if (v == SEQ_START_TOKEN)
1979 return ip_vs_info_array(seq,0);
1982 iter = seq->private;
1984 if (iter->table == ip_vs_svc_table) {
1985 /* next service in table hashed by protocol */
1986 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1988 return hlist_entry(e, struct ip_vs_service, s_list);
1990 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1991 hlist_for_each_entry_rcu(svc,
1992 &ip_vs_svc_table[iter->bucket],
1998 iter->table = ip_vs_svc_fwm_table;
2003 /* next service in hashed by fwmark */
2004 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2006 return hlist_entry(e, struct ip_vs_service, f_list);
2009 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2010 hlist_for_each_entry_rcu(svc,
2011 &ip_vs_svc_fwm_table[iter->bucket],
2019 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2026 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2028 if (v == SEQ_START_TOKEN) {
2030 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2031 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2033 "Prot LocalAddress:Port Scheduler Flags\n");
2035 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2037 struct net *net = seq_file_net(seq);
2038 struct netns_ipvs *ipvs = net_ipvs(net);
2039 const struct ip_vs_service *svc = v;
2040 const struct ip_vs_iter *iter = seq->private;
2041 const struct ip_vs_dest *dest;
2042 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2043 char *sched_name = sched ? sched->name : "none";
2045 if (svc->ipvs != ipvs)
2047 if (iter->table == ip_vs_svc_table) {
2048 #ifdef CONFIG_IP_VS_IPV6
2049 if (svc->af == AF_INET6)
2050 seq_printf(seq, "%s [%pI6]:%04X %s ",
2051 ip_vs_proto_name(svc->protocol),
2057 seq_printf(seq, "%s %08X:%04X %s %s ",
2058 ip_vs_proto_name(svc->protocol),
2059 ntohl(svc->addr.ip),
2062 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2064 seq_printf(seq, "FWM %08X %s %s",
2065 svc->fwmark, sched_name,
2066 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2069 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2070 seq_printf(seq, "persistent %d %08X\n",
2072 ntohl(svc->netmask));
2074 seq_putc(seq, '\n');
2076 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2077 #ifdef CONFIG_IP_VS_IPV6
2078 if (dest->af == AF_INET6)
2081 " %-7s %-6d %-10d %-10d\n",
2084 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2085 atomic_read(&dest->weight),
2086 atomic_read(&dest->activeconns),
2087 atomic_read(&dest->inactconns));
2092 "%-7s %-6d %-10d %-10d\n",
2093 ntohl(dest->addr.ip),
2095 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2096 atomic_read(&dest->weight),
2097 atomic_read(&dest->activeconns),
2098 atomic_read(&dest->inactconns));
2105 static const struct seq_operations ip_vs_info_seq_ops = {
2106 .start = ip_vs_info_seq_start,
2107 .next = ip_vs_info_seq_next,
2108 .stop = ip_vs_info_seq_stop,
2109 .show = ip_vs_info_seq_show,
2112 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2114 struct net *net = seq_file_single_net(seq);
2115 struct ip_vs_kstats show;
2117 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2119 " Total Incoming Outgoing Incoming Outgoing\n");
2121 " Conns Packets Packets Bytes Bytes\n");
2123 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2124 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2125 (unsigned long long)show.conns,
2126 (unsigned long long)show.inpkts,
2127 (unsigned long long)show.outpkts,
2128 (unsigned long long)show.inbytes,
2129 (unsigned long long)show.outbytes);
2131 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2133 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2134 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2135 (unsigned long long)show.cps,
2136 (unsigned long long)show.inpps,
2137 (unsigned long long)show.outpps,
2138 (unsigned long long)show.inbps,
2139 (unsigned long long)show.outbps);
2144 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2146 struct net *net = seq_file_single_net(seq);
2147 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2148 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2149 struct ip_vs_kstats kstats;
2152 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2154 " Total Incoming Outgoing Incoming Outgoing\n");
2156 "CPU Conns Packets Packets Bytes Bytes\n");
2158 for_each_possible_cpu(i) {
2159 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2161 u64 conns, inpkts, outpkts, inbytes, outbytes;
2164 start = u64_stats_fetch_begin_irq(&u->syncp);
2165 conns = u->cnt.conns;
2166 inpkts = u->cnt.inpkts;
2167 outpkts = u->cnt.outpkts;
2168 inbytes = u->cnt.inbytes;
2169 outbytes = u->cnt.outbytes;
2170 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2172 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2173 i, (u64)conns, (u64)inpkts,
2174 (u64)outpkts, (u64)inbytes,
2178 ip_vs_copy_stats(&kstats, tot_stats);
2180 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2181 (unsigned long long)kstats.conns,
2182 (unsigned long long)kstats.inpkts,
2183 (unsigned long long)kstats.outpkts,
2184 (unsigned long long)kstats.inbytes,
2185 (unsigned long long)kstats.outbytes);
2187 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2189 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2190 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2202 * Set timeout values for tcp tcpfin udp in the timeout_table.
2204 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2206 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2207 struct ip_vs_proto_data *pd;
2210 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2215 #ifdef CONFIG_IP_VS_PROTO_TCP
2216 if (u->tcp_timeout) {
2217 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2218 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2219 = u->tcp_timeout * HZ;
2222 if (u->tcp_fin_timeout) {
2223 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2224 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2225 = u->tcp_fin_timeout * HZ;
2229 #ifdef CONFIG_IP_VS_PROTO_UDP
2230 if (u->udp_timeout) {
2231 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2232 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2233 = u->udp_timeout * HZ;
2239 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2241 struct ip_vs_svcdest_user {
2242 struct ip_vs_service_user s;
2243 struct ip_vs_dest_user d;
2246 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2247 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2248 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2249 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2250 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2251 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2252 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2253 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2254 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2255 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2256 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2259 union ip_vs_set_arglen {
2260 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2261 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2262 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2263 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2264 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2265 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2266 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2267 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2268 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2269 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2272 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2274 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2275 struct ip_vs_service_user *usvc_compat)
2277 memset(usvc, 0, sizeof(*usvc));
2280 usvc->protocol = usvc_compat->protocol;
2281 usvc->addr.ip = usvc_compat->addr;
2282 usvc->port = usvc_compat->port;
2283 usvc->fwmark = usvc_compat->fwmark;
2285 /* Deep copy of sched_name is not needed here */
2286 usvc->sched_name = usvc_compat->sched_name;
2288 usvc->flags = usvc_compat->flags;
2289 usvc->timeout = usvc_compat->timeout;
2290 usvc->netmask = usvc_compat->netmask;
2293 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2294 struct ip_vs_dest_user *udest_compat)
2296 memset(udest, 0, sizeof(*udest));
2298 udest->addr.ip = udest_compat->addr;
2299 udest->port = udest_compat->port;
2300 udest->conn_flags = udest_compat->conn_flags;
2301 udest->weight = udest_compat->weight;
2302 udest->u_threshold = udest_compat->u_threshold;
2303 udest->l_threshold = udest_compat->l_threshold;
2304 udest->af = AF_INET;
2308 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2310 struct net *net = sock_net(sk);
2312 unsigned char arg[MAX_SET_ARGLEN];
2313 struct ip_vs_service_user *usvc_compat;
2314 struct ip_vs_service_user_kern usvc;
2315 struct ip_vs_service *svc;
2316 struct ip_vs_dest_user *udest_compat;
2317 struct ip_vs_dest_user_kern udest;
2318 struct netns_ipvs *ipvs = net_ipvs(net);
2320 BUILD_BUG_ON(sizeof(arg) > 255);
2321 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2324 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2326 if (len != set_arglen[CMDID(cmd)]) {
2327 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2328 len, set_arglen[CMDID(cmd)]);
2332 if (copy_from_user(arg, user, len) != 0)
2335 /* increase the module use count */
2336 ip_vs_use_count_inc();
2338 /* Handle daemons since they have another lock */
2339 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2340 cmd == IP_VS_SO_SET_STOPDAEMON) {
2341 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2343 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2344 struct ipvs_sync_daemon_cfg cfg;
2346 memset(&cfg, 0, sizeof(cfg));
2348 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2349 sizeof(cfg.mcast_ifn)) <= 0)
2351 cfg.syncid = dm->syncid;
2352 ret = start_sync_thread(ipvs, &cfg, dm->state);
2354 mutex_lock(&ipvs->sync_mutex);
2355 ret = stop_sync_thread(ipvs, dm->state);
2356 mutex_unlock(&ipvs->sync_mutex);
2361 mutex_lock(&__ip_vs_mutex);
2362 if (cmd == IP_VS_SO_SET_FLUSH) {
2363 /* Flush the virtual service */
2364 ret = ip_vs_flush(ipvs, false);
2366 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2367 /* Set timeout values for (tcp tcpfin udp) */
2368 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2372 usvc_compat = (struct ip_vs_service_user *)arg;
2373 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2375 /* We only use the new structs internally, so copy userspace compat
2376 * structs to extended internal versions */
2377 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2378 ip_vs_copy_udest_compat(&udest, udest_compat);
2380 if (cmd == IP_VS_SO_SET_ZERO) {
2381 /* if no service address is set, zero counters in all */
2382 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2383 ret = ip_vs_zero_all(ipvs);
2388 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2389 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2390 IP_VS_SCHEDNAME_MAXLEN) {
2395 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2396 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2397 usvc.protocol != IPPROTO_SCTP) {
2398 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2399 usvc.protocol, &usvc.addr.ip,
2405 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2407 if (usvc.fwmark == 0)
2408 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2409 &usvc.addr, usvc.port);
2411 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2414 if (cmd != IP_VS_SO_SET_ADD
2415 && (svc == NULL || svc->protocol != usvc.protocol)) {
2421 case IP_VS_SO_SET_ADD:
2425 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2427 case IP_VS_SO_SET_EDIT:
2428 ret = ip_vs_edit_service(svc, &usvc);
2430 case IP_VS_SO_SET_DEL:
2431 ret = ip_vs_del_service(svc);
2435 case IP_VS_SO_SET_ZERO:
2436 ret = ip_vs_zero_service(svc);
2438 case IP_VS_SO_SET_ADDDEST:
2439 ret = ip_vs_add_dest(svc, &udest);
2441 case IP_VS_SO_SET_EDITDEST:
2442 ret = ip_vs_edit_dest(svc, &udest);
2444 case IP_VS_SO_SET_DELDEST:
2445 ret = ip_vs_del_dest(svc, &udest);
2452 mutex_unlock(&__ip_vs_mutex);
2454 /* decrease the module use count */
2455 ip_vs_use_count_dec();
2462 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2464 struct ip_vs_scheduler *sched;
2465 struct ip_vs_kstats kstats;
2468 sched = rcu_dereference_protected(src->scheduler, 1);
2469 sched_name = sched ? sched->name : "none";
2470 dst->protocol = src->protocol;
2471 dst->addr = src->addr.ip;
2472 dst->port = src->port;
2473 dst->fwmark = src->fwmark;
2474 strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2475 dst->flags = src->flags;
2476 dst->timeout = src->timeout / HZ;
2477 dst->netmask = src->netmask;
2478 dst->num_dests = src->num_dests;
2479 ip_vs_copy_stats(&kstats, &src->stats);
2480 ip_vs_export_stats_user(&dst->stats, &kstats);
2484 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2485 const struct ip_vs_get_services *get,
2486 struct ip_vs_get_services __user *uptr)
2489 struct ip_vs_service *svc;
2490 struct ip_vs_service_entry entry;
2493 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2494 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2495 /* Only expose IPv4 entries to old interface */
2496 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2499 if (count >= get->num_services)
2501 memset(&entry, 0, sizeof(entry));
2502 ip_vs_copy_service(&entry, svc);
2503 if (copy_to_user(&uptr->entrytable[count],
2504 &entry, sizeof(entry))) {
2512 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2513 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2514 /* Only expose IPv4 entries to old interface */
2515 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2518 if (count >= get->num_services)
2520 memset(&entry, 0, sizeof(entry));
2521 ip_vs_copy_service(&entry, svc);
2522 if (copy_to_user(&uptr->entrytable[count],
2523 &entry, sizeof(entry))) {
2535 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2536 struct ip_vs_get_dests __user *uptr)
2538 struct ip_vs_service *svc;
2539 union nf_inet_addr addr = { .ip = get->addr };
2544 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2546 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2552 struct ip_vs_dest *dest;
2553 struct ip_vs_dest_entry entry;
2554 struct ip_vs_kstats kstats;
2556 memset(&entry, 0, sizeof(entry));
2557 list_for_each_entry(dest, &svc->destinations, n_list) {
2558 if (count >= get->num_dests)
2561 /* Cannot expose heterogeneous members via sockopt
2564 if (dest->af != svc->af)
2567 entry.addr = dest->addr.ip;
2568 entry.port = dest->port;
2569 entry.conn_flags = atomic_read(&dest->conn_flags);
2570 entry.weight = atomic_read(&dest->weight);
2571 entry.u_threshold = dest->u_threshold;
2572 entry.l_threshold = dest->l_threshold;
2573 entry.activeconns = atomic_read(&dest->activeconns);
2574 entry.inactconns = atomic_read(&dest->inactconns);
2575 entry.persistconns = atomic_read(&dest->persistconns);
2576 ip_vs_copy_stats(&kstats, &dest->stats);
2577 ip_vs_export_stats_user(&entry.stats, &kstats);
2578 if (copy_to_user(&uptr->entrytable[count],
2579 &entry, sizeof(entry))) {
2591 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2593 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2594 struct ip_vs_proto_data *pd;
2597 memset(u, 0, sizeof (*u));
2599 #ifdef CONFIG_IP_VS_PROTO_TCP
2600 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2601 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2602 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2604 #ifdef CONFIG_IP_VS_PROTO_UDP
2605 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2607 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2611 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2612 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2613 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2614 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2615 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2616 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2617 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2618 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2621 union ip_vs_get_arglen {
2622 char field_IP_VS_SO_GET_VERSION[64];
2623 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2624 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2625 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2626 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2627 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2628 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2631 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2634 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2636 unsigned char arg[MAX_GET_ARGLEN];
2638 unsigned int copylen;
2639 struct net *net = sock_net(sk);
2640 struct netns_ipvs *ipvs = net_ipvs(net);
2643 BUILD_BUG_ON(sizeof(arg) > 255);
2644 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2647 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2650 copylen = get_arglen[CMDID(cmd)];
2651 if (*len < (int) copylen) {
2652 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2656 if (copy_from_user(arg, user, copylen) != 0)
2659 * Handle daemons first since it has its own locking
2661 if (cmd == IP_VS_SO_GET_DAEMON) {
2662 struct ip_vs_daemon_user d[2];
2664 memset(&d, 0, sizeof(d));
2665 mutex_lock(&ipvs->sync_mutex);
2666 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2667 d[0].state = IP_VS_STATE_MASTER;
2668 strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2669 sizeof(d[0].mcast_ifn));
2670 d[0].syncid = ipvs->mcfg.syncid;
2672 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2673 d[1].state = IP_VS_STATE_BACKUP;
2674 strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2675 sizeof(d[1].mcast_ifn));
2676 d[1].syncid = ipvs->bcfg.syncid;
2678 if (copy_to_user(user, &d, sizeof(d)) != 0)
2680 mutex_unlock(&ipvs->sync_mutex);
2684 mutex_lock(&__ip_vs_mutex);
2686 case IP_VS_SO_GET_VERSION:
2690 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2691 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2692 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2696 *len = strlen(buf)+1;
2700 case IP_VS_SO_GET_INFO:
2702 struct ip_vs_getinfo info;
2703 info.version = IP_VS_VERSION_CODE;
2704 info.size = ip_vs_conn_tab_size;
2705 info.num_services = ipvs->num_services;
2706 if (copy_to_user(user, &info, sizeof(info)) != 0)
2711 case IP_VS_SO_GET_SERVICES:
2713 struct ip_vs_get_services *get;
2716 get = (struct ip_vs_get_services *)arg;
2717 size = sizeof(*get) +
2718 sizeof(struct ip_vs_service_entry) * get->num_services;
2720 pr_err("length: %u != %u\n", *len, size);
2724 ret = __ip_vs_get_service_entries(ipvs, get, user);
2728 case IP_VS_SO_GET_SERVICE:
2730 struct ip_vs_service_entry *entry;
2731 struct ip_vs_service *svc;
2732 union nf_inet_addr addr;
2734 entry = (struct ip_vs_service_entry *)arg;
2735 addr.ip = entry->addr;
2738 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2740 svc = __ip_vs_service_find(ipvs, AF_INET,
2741 entry->protocol, &addr,
2745 ip_vs_copy_service(entry, svc);
2746 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2753 case IP_VS_SO_GET_DESTS:
2755 struct ip_vs_get_dests *get;
2758 get = (struct ip_vs_get_dests *)arg;
2759 size = sizeof(*get) +
2760 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2762 pr_err("length: %u != %u\n", *len, size);
2766 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2770 case IP_VS_SO_GET_TIMEOUT:
2772 struct ip_vs_timeout_user t;
2774 __ip_vs_get_timeouts(ipvs, &t);
2775 if (copy_to_user(user, &t, sizeof(t)) != 0)
2785 mutex_unlock(&__ip_vs_mutex);
2790 static struct nf_sockopt_ops ip_vs_sockopts = {
2792 .set_optmin = IP_VS_BASE_CTL,
2793 .set_optmax = IP_VS_SO_SET_MAX+1,
2794 .set = do_ip_vs_set_ctl,
2795 .get_optmin = IP_VS_BASE_CTL,
2796 .get_optmax = IP_VS_SO_GET_MAX+1,
2797 .get = do_ip_vs_get_ctl,
2798 .owner = THIS_MODULE,
2802 * Generic Netlink interface
2805 /* IPVS genetlink family */
2806 static struct genl_family ip_vs_genl_family;
2808 /* Policy used for first-level command attributes */
2809 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2810 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2811 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2812 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2813 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2814 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2815 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2818 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2819 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2820 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2821 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2822 .len = IP_VS_IFNAME_MAXLEN - 1 },
2823 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2824 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2825 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2826 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2827 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2828 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2831 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2832 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2833 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2834 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2835 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2836 .len = sizeof(union nf_inet_addr) },
2837 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2838 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2839 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2840 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
2841 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2842 .len = IP_VS_PENAME_MAXLEN },
2843 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2844 .len = sizeof(struct ip_vs_flags) },
2845 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2846 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2847 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2850 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2851 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2852 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2853 .len = sizeof(union nf_inet_addr) },
2854 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2855 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2856 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2857 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2858 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2859 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2860 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2861 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2862 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2863 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2866 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2867 struct ip_vs_kstats *kstats)
2869 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2874 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2875 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2876 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2877 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2878 IPVS_STATS_ATTR_PAD) ||
2879 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2880 IPVS_STATS_ATTR_PAD) ||
2881 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2882 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2883 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2884 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2885 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2886 goto nla_put_failure;
2887 nla_nest_end(skb, nl_stats);
2892 nla_nest_cancel(skb, nl_stats);
2896 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2897 struct ip_vs_kstats *kstats)
2899 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2904 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
2905 IPVS_STATS_ATTR_PAD) ||
2906 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
2907 IPVS_STATS_ATTR_PAD) ||
2908 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
2909 IPVS_STATS_ATTR_PAD) ||
2910 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2911 IPVS_STATS_ATTR_PAD) ||
2912 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2913 IPVS_STATS_ATTR_PAD) ||
2914 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
2915 IPVS_STATS_ATTR_PAD) ||
2916 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
2917 IPVS_STATS_ATTR_PAD) ||
2918 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
2919 IPVS_STATS_ATTR_PAD) ||
2920 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
2921 IPVS_STATS_ATTR_PAD) ||
2922 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
2923 IPVS_STATS_ATTR_PAD))
2924 goto nla_put_failure;
2925 nla_nest_end(skb, nl_stats);
2930 nla_nest_cancel(skb, nl_stats);
2934 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2935 struct ip_vs_service *svc)
2937 struct ip_vs_scheduler *sched;
2938 struct ip_vs_pe *pe;
2939 struct nlattr *nl_service;
2940 struct ip_vs_flags flags = { .flags = svc->flags,
2942 struct ip_vs_kstats kstats;
2945 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2949 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2950 goto nla_put_failure;
2952 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2953 goto nla_put_failure;
2955 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2956 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2957 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2958 goto nla_put_failure;
2961 sched = rcu_dereference_protected(svc->scheduler, 1);
2962 sched_name = sched ? sched->name : "none";
2963 pe = rcu_dereference_protected(svc->pe, 1);
2964 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
2965 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2966 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2967 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2968 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2969 goto nla_put_failure;
2970 ip_vs_copy_stats(&kstats, &svc->stats);
2971 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
2972 goto nla_put_failure;
2973 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
2974 goto nla_put_failure;
2976 nla_nest_end(skb, nl_service);
2981 nla_nest_cancel(skb, nl_service);
2985 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2986 struct ip_vs_service *svc,
2987 struct netlink_callback *cb)
2991 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2992 &ip_vs_genl_family, NLM_F_MULTI,
2993 IPVS_CMD_NEW_SERVICE);
2997 if (ip_vs_genl_fill_service(skb, svc) < 0)
2998 goto nla_put_failure;
3000 genlmsg_end(skb, hdr);
3004 genlmsg_cancel(skb, hdr);
3008 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3009 struct netlink_callback *cb)
3012 int start = cb->args[0];
3013 struct ip_vs_service *svc;
3014 struct net *net = sock_net(skb->sk);
3015 struct netns_ipvs *ipvs = net_ipvs(net);
3017 mutex_lock(&__ip_vs_mutex);
3018 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3019 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3020 if (++idx <= start || (svc->ipvs != ipvs))
3022 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3024 goto nla_put_failure;
3029 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3030 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3031 if (++idx <= start || (svc->ipvs != ipvs))
3033 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3035 goto nla_put_failure;
3041 mutex_unlock(&__ip_vs_mutex);
3047 static bool ip_vs_is_af_valid(int af)
3051 #ifdef CONFIG_IP_VS_IPV6
3052 if (af == AF_INET6 && ipv6_mod_enabled())
3058 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3059 struct ip_vs_service_user_kern *usvc,
3060 struct nlattr *nla, int full_entry,
3061 struct ip_vs_service **ret_svc)
3063 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3064 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3065 struct ip_vs_service *svc;
3067 /* Parse mandatory identifying service fields first */
3069 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla,
3070 ip_vs_svc_policy, NULL))
3073 nla_af = attrs[IPVS_SVC_ATTR_AF];
3074 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3075 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3076 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3077 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3079 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3082 memset(usvc, 0, sizeof(*usvc));
3084 usvc->af = nla_get_u16(nla_af);
3085 if (!ip_vs_is_af_valid(usvc->af))
3086 return -EAFNOSUPPORT;
3089 usvc->protocol = IPPROTO_TCP;
3090 usvc->fwmark = nla_get_u32(nla_fwmark);
3092 usvc->protocol = nla_get_u16(nla_protocol);
3093 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3094 usvc->port = nla_get_be16(nla_port);
3100 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3102 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3103 &usvc->addr, usvc->port);
3107 /* If a full entry was requested, check for the additional fields */
3109 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3111 struct ip_vs_flags flags;
3113 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3114 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3115 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3116 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3117 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3119 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3122 nla_memcpy(&flags, nla_flags, sizeof(flags));
3124 /* prefill flags from service if it already exists */
3126 usvc->flags = svc->flags;
3128 /* set new flags from userland */
3129 usvc->flags = (usvc->flags & ~flags.mask) |
3130 (flags.flags & flags.mask);
3131 usvc->sched_name = nla_data(nla_sched);
3132 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3133 usvc->timeout = nla_get_u32(nla_timeout);
3134 usvc->netmask = nla_get_be32(nla_netmask);
3140 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3143 struct ip_vs_service_user_kern usvc;
3144 struct ip_vs_service *svc;
3147 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, 0, &svc);
3148 return ret ? ERR_PTR(ret) : svc;
3151 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3153 struct nlattr *nl_dest;
3154 struct ip_vs_kstats kstats;
3156 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3160 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3161 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3162 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3163 (atomic_read(&dest->conn_flags) &
3164 IP_VS_CONN_F_FWD_MASK)) ||
3165 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3166 atomic_read(&dest->weight)) ||
3167 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3168 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3169 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3170 atomic_read(&dest->activeconns)) ||
3171 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3172 atomic_read(&dest->inactconns)) ||
3173 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3174 atomic_read(&dest->persistconns)) ||
3175 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3176 goto nla_put_failure;
3177 ip_vs_copy_stats(&kstats, &dest->stats);
3178 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3179 goto nla_put_failure;
3180 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3181 goto nla_put_failure;
3183 nla_nest_end(skb, nl_dest);
3188 nla_nest_cancel(skb, nl_dest);
3192 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3193 struct netlink_callback *cb)
3197 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3198 &ip_vs_genl_family, NLM_F_MULTI,
3203 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3204 goto nla_put_failure;
3206 genlmsg_end(skb, hdr);
3210 genlmsg_cancel(skb, hdr);
3214 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3215 struct netlink_callback *cb)
3218 int start = cb->args[0];
3219 struct ip_vs_service *svc;
3220 struct ip_vs_dest *dest;
3221 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3222 struct net *net = sock_net(skb->sk);
3223 struct netns_ipvs *ipvs = net_ipvs(net);
3225 mutex_lock(&__ip_vs_mutex);
3227 /* Try to find the service for which to dump destinations */
3228 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX,
3229 ip_vs_cmd_policy, NULL))
3233 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3234 if (IS_ERR_OR_NULL(svc))
3237 /* Dump the destinations */
3238 list_for_each_entry(dest, &svc->destinations, n_list) {
3241 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3243 goto nla_put_failure;
3251 mutex_unlock(&__ip_vs_mutex);
3256 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3257 struct nlattr *nla, int full_entry)
3259 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3260 struct nlattr *nla_addr, *nla_port;
3261 struct nlattr *nla_addr_family;
3263 /* Parse mandatory identifying destination fields first */
3265 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla,
3266 ip_vs_dest_policy, NULL))
3269 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3270 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3271 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3273 if (!(nla_addr && nla_port))
3276 memset(udest, 0, sizeof(*udest));
3278 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3279 udest->port = nla_get_be16(nla_port);
3281 if (nla_addr_family)
3282 udest->af = nla_get_u16(nla_addr_family);
3286 /* If a full entry was requested, check for the additional fields */
3288 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3291 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3292 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3293 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3294 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3296 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3299 udest->conn_flags = nla_get_u32(nla_fwd)
3300 & IP_VS_CONN_F_FWD_MASK;
3301 udest->weight = nla_get_u32(nla_weight);
3302 udest->u_threshold = nla_get_u32(nla_u_thresh);
3303 udest->l_threshold = nla_get_u32(nla_l_thresh);
3309 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3310 struct ipvs_sync_daemon_cfg *c)
3312 struct nlattr *nl_daemon;
3314 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3318 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3319 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3320 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3321 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3322 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3323 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3324 goto nla_put_failure;
3325 #ifdef CONFIG_IP_VS_IPV6
3326 if (c->mcast_af == AF_INET6) {
3327 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3328 &c->mcast_group.in6))
3329 goto nla_put_failure;
3332 if (c->mcast_af == AF_INET &&
3333 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3335 goto nla_put_failure;
3336 nla_nest_end(skb, nl_daemon);
3341 nla_nest_cancel(skb, nl_daemon);
3345 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3346 struct ipvs_sync_daemon_cfg *c,
3347 struct netlink_callback *cb)
3350 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3351 &ip_vs_genl_family, NLM_F_MULTI,
3352 IPVS_CMD_NEW_DAEMON);
3356 if (ip_vs_genl_fill_daemon(skb, state, c))
3357 goto nla_put_failure;
3359 genlmsg_end(skb, hdr);
3363 genlmsg_cancel(skb, hdr);
3367 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3368 struct netlink_callback *cb)
3370 struct net *net = sock_net(skb->sk);
3371 struct netns_ipvs *ipvs = net_ipvs(net);
3373 mutex_lock(&ipvs->sync_mutex);
3374 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3375 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3376 &ipvs->mcfg, cb) < 0)
3377 goto nla_put_failure;
3382 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3383 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3384 &ipvs->bcfg, cb) < 0)
3385 goto nla_put_failure;
3391 mutex_unlock(&ipvs->sync_mutex);
3396 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3398 struct ipvs_sync_daemon_cfg c;
3402 memset(&c, 0, sizeof(c));
3403 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3404 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3405 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3407 strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3408 sizeof(c.mcast_ifn));
3409 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3411 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3413 c.sync_maxlen = nla_get_u16(a);
3415 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3417 c.mcast_af = AF_INET;
3418 c.mcast_group.ip = nla_get_in_addr(a);
3419 if (!ipv4_is_multicast(c.mcast_group.ip))
3422 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3424 #ifdef CONFIG_IP_VS_IPV6
3427 c.mcast_af = AF_INET6;
3428 c.mcast_group.in6 = nla_get_in6_addr(a);
3429 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3430 if (!(addr_type & IPV6_ADDR_MULTICAST))
3433 return -EAFNOSUPPORT;
3438 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3440 c.mcast_port = nla_get_u16(a);
3442 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3444 c.mcast_ttl = nla_get_u8(a);
3446 /* The synchronization protocol is incompatible with mixed family
3449 if (ipvs->mixed_address_family_dests > 0)
3452 ret = start_sync_thread(ipvs, &c,
3453 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3457 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3461 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3464 mutex_lock(&ipvs->sync_mutex);
3465 ret = stop_sync_thread(ipvs,
3466 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3467 mutex_unlock(&ipvs->sync_mutex);
3471 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3473 struct ip_vs_timeout_user t;
3475 __ip_vs_get_timeouts(ipvs, &t);
3477 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3478 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3480 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3482 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3484 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3485 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3487 return ip_vs_set_timeout(ipvs, &t);
3490 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3492 int ret = -EINVAL, cmd;
3493 struct net *net = sock_net(skb->sk);
3494 struct netns_ipvs *ipvs = net_ipvs(net);
3496 cmd = info->genlhdr->cmd;
3498 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3499 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3501 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3502 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3503 info->attrs[IPVS_CMD_ATTR_DAEMON],
3504 ip_vs_daemon_policy, info->extack))
3507 if (cmd == IPVS_CMD_NEW_DAEMON)
3508 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3510 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3517 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3519 struct ip_vs_service *svc = NULL;
3520 struct ip_vs_service_user_kern usvc;
3521 struct ip_vs_dest_user_kern udest;
3523 int need_full_svc = 0, need_full_dest = 0;
3524 struct net *net = sock_net(skb->sk);
3525 struct netns_ipvs *ipvs = net_ipvs(net);
3527 cmd = info->genlhdr->cmd;
3529 mutex_lock(&__ip_vs_mutex);
3531 if (cmd == IPVS_CMD_FLUSH) {
3532 ret = ip_vs_flush(ipvs, false);
3534 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3535 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3537 } else if (cmd == IPVS_CMD_ZERO &&
3538 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3539 ret = ip_vs_zero_all(ipvs);
3543 /* All following commands require a service argument, so check if we
3544 * received a valid one. We need a full service specification when
3545 * adding / editing a service. Only identifying members otherwise. */
3546 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3549 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3550 info->attrs[IPVS_CMD_ATTR_SERVICE],
3551 need_full_svc, &svc);
3555 /* Unless we're adding a new service, the service must already exist */
3556 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3561 /* Destination commands require a valid destination argument. For
3562 * adding / editing a destination, we need a full destination
3564 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3565 cmd == IPVS_CMD_DEL_DEST) {
3566 if (cmd != IPVS_CMD_DEL_DEST)
3569 ret = ip_vs_genl_parse_dest(&udest,
3570 info->attrs[IPVS_CMD_ATTR_DEST],
3575 /* Old protocols did not allow the user to specify address
3576 * family, so we set it to zero instead. We also didn't
3577 * allow heterogeneous pools in the old code, so it's safe
3578 * to assume that this will have the same address family as
3584 if (!ip_vs_is_af_valid(udest.af)) {
3585 ret = -EAFNOSUPPORT;
3589 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3590 /* The synchronization protocol is incompatible
3591 * with mixed family services
3593 if (ipvs->sync_state) {
3598 /* Which connection types do we support? */
3599 switch (udest.conn_flags) {
3600 case IP_VS_CONN_F_TUNNEL:
3601 /* We are able to forward this */
3611 case IPVS_CMD_NEW_SERVICE:
3613 ret = ip_vs_add_service(ipvs, &usvc, &svc);
3617 case IPVS_CMD_SET_SERVICE:
3618 ret = ip_vs_edit_service(svc, &usvc);
3620 case IPVS_CMD_DEL_SERVICE:
3621 ret = ip_vs_del_service(svc);
3622 /* do not use svc, it can be freed */
3624 case IPVS_CMD_NEW_DEST:
3625 ret = ip_vs_add_dest(svc, &udest);
3627 case IPVS_CMD_SET_DEST:
3628 ret = ip_vs_edit_dest(svc, &udest);
3630 case IPVS_CMD_DEL_DEST:
3631 ret = ip_vs_del_dest(svc, &udest);
3634 ret = ip_vs_zero_service(svc);
3641 mutex_unlock(&__ip_vs_mutex);
3646 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3648 struct sk_buff *msg;
3650 int ret, cmd, reply_cmd;
3651 struct net *net = sock_net(skb->sk);
3652 struct netns_ipvs *ipvs = net_ipvs(net);
3654 cmd = info->genlhdr->cmd;
3656 if (cmd == IPVS_CMD_GET_SERVICE)
3657 reply_cmd = IPVS_CMD_NEW_SERVICE;
3658 else if (cmd == IPVS_CMD_GET_INFO)
3659 reply_cmd = IPVS_CMD_SET_INFO;
3660 else if (cmd == IPVS_CMD_GET_CONFIG)
3661 reply_cmd = IPVS_CMD_SET_CONFIG;
3663 pr_err("unknown Generic Netlink command\n");
3667 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3671 mutex_lock(&__ip_vs_mutex);
3673 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3675 goto nla_put_failure;
3678 case IPVS_CMD_GET_SERVICE:
3680 struct ip_vs_service *svc;
3682 svc = ip_vs_genl_find_service(ipvs,
3683 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3688 ret = ip_vs_genl_fill_service(msg, svc);
3690 goto nla_put_failure;
3699 case IPVS_CMD_GET_CONFIG:
3701 struct ip_vs_timeout_user t;
3703 __ip_vs_get_timeouts(ipvs, &t);
3704 #ifdef CONFIG_IP_VS_PROTO_TCP
3705 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3707 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3709 goto nla_put_failure;
3711 #ifdef CONFIG_IP_VS_PROTO_UDP
3712 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3713 goto nla_put_failure;
3719 case IPVS_CMD_GET_INFO:
3720 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3721 IP_VS_VERSION_CODE) ||
3722 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3723 ip_vs_conn_tab_size))
3724 goto nla_put_failure;
3728 genlmsg_end(msg, reply);
3729 ret = genlmsg_reply(msg, info);
3733 pr_err("not enough space in Netlink message\n");
3739 mutex_unlock(&__ip_vs_mutex);
3745 static const struct genl_ops ip_vs_genl_ops[] = {
3747 .cmd = IPVS_CMD_NEW_SERVICE,
3748 .flags = GENL_ADMIN_PERM,
3749 .policy = ip_vs_cmd_policy,
3750 .doit = ip_vs_genl_set_cmd,
3753 .cmd = IPVS_CMD_SET_SERVICE,
3754 .flags = GENL_ADMIN_PERM,
3755 .policy = ip_vs_cmd_policy,
3756 .doit = ip_vs_genl_set_cmd,
3759 .cmd = IPVS_CMD_DEL_SERVICE,
3760 .flags = GENL_ADMIN_PERM,
3761 .policy = ip_vs_cmd_policy,
3762 .doit = ip_vs_genl_set_cmd,
3765 .cmd = IPVS_CMD_GET_SERVICE,
3766 .flags = GENL_ADMIN_PERM,
3767 .doit = ip_vs_genl_get_cmd,
3768 .dumpit = ip_vs_genl_dump_services,
3769 .policy = ip_vs_cmd_policy,
3772 .cmd = IPVS_CMD_NEW_DEST,
3773 .flags = GENL_ADMIN_PERM,
3774 .policy = ip_vs_cmd_policy,
3775 .doit = ip_vs_genl_set_cmd,
3778 .cmd = IPVS_CMD_SET_DEST,
3779 .flags = GENL_ADMIN_PERM,
3780 .policy = ip_vs_cmd_policy,
3781 .doit = ip_vs_genl_set_cmd,
3784 .cmd = IPVS_CMD_DEL_DEST,
3785 .flags = GENL_ADMIN_PERM,
3786 .policy = ip_vs_cmd_policy,
3787 .doit = ip_vs_genl_set_cmd,
3790 .cmd = IPVS_CMD_GET_DEST,
3791 .flags = GENL_ADMIN_PERM,
3792 .policy = ip_vs_cmd_policy,
3793 .dumpit = ip_vs_genl_dump_dests,
3796 .cmd = IPVS_CMD_NEW_DAEMON,
3797 .flags = GENL_ADMIN_PERM,
3798 .policy = ip_vs_cmd_policy,
3799 .doit = ip_vs_genl_set_daemon,
3802 .cmd = IPVS_CMD_DEL_DAEMON,
3803 .flags = GENL_ADMIN_PERM,
3804 .policy = ip_vs_cmd_policy,
3805 .doit = ip_vs_genl_set_daemon,
3808 .cmd = IPVS_CMD_GET_DAEMON,
3809 .flags = GENL_ADMIN_PERM,
3810 .dumpit = ip_vs_genl_dump_daemons,
3813 .cmd = IPVS_CMD_SET_CONFIG,
3814 .flags = GENL_ADMIN_PERM,
3815 .policy = ip_vs_cmd_policy,
3816 .doit = ip_vs_genl_set_cmd,
3819 .cmd = IPVS_CMD_GET_CONFIG,
3820 .flags = GENL_ADMIN_PERM,
3821 .doit = ip_vs_genl_get_cmd,
3824 .cmd = IPVS_CMD_GET_INFO,
3825 .flags = GENL_ADMIN_PERM,
3826 .doit = ip_vs_genl_get_cmd,
3829 .cmd = IPVS_CMD_ZERO,
3830 .flags = GENL_ADMIN_PERM,
3831 .policy = ip_vs_cmd_policy,
3832 .doit = ip_vs_genl_set_cmd,
3835 .cmd = IPVS_CMD_FLUSH,
3836 .flags = GENL_ADMIN_PERM,
3837 .doit = ip_vs_genl_set_cmd,
3841 static struct genl_family ip_vs_genl_family __ro_after_init = {
3843 .name = IPVS_GENL_NAME,
3844 .version = IPVS_GENL_VERSION,
3845 .maxattr = IPVS_CMD_ATTR_MAX,
3846 .netnsok = true, /* Make ipvsadm to work on netns */
3847 .module = THIS_MODULE,
3848 .ops = ip_vs_genl_ops,
3849 .n_ops = ARRAY_SIZE(ip_vs_genl_ops),
3852 static int __init ip_vs_genl_register(void)
3854 return genl_register_family(&ip_vs_genl_family);
3857 static void ip_vs_genl_unregister(void)
3859 genl_unregister_family(&ip_vs_genl_family);
3862 /* End of Generic Netlink interface definitions */
3865 * per netns intit/exit func.
3867 #ifdef CONFIG_SYSCTL
3868 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
3870 struct net *net = ipvs->net;
3872 struct ctl_table *tbl;
3874 atomic_set(&ipvs->dropentry, 0);
3875 spin_lock_init(&ipvs->dropentry_lock);
3876 spin_lock_init(&ipvs->droppacket_lock);
3877 spin_lock_init(&ipvs->securetcp_lock);
3879 if (!net_eq(net, &init_net)) {
3880 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3884 /* Don't export sysctls to unprivileged users */
3885 if (net->user_ns != &init_user_ns)
3886 tbl[0].procname = NULL;
3889 /* Initialize sysctl defaults */
3890 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
3891 if (tbl[idx].proc_handler == proc_do_defense_mode)
3892 tbl[idx].extra2 = ipvs;
3895 ipvs->sysctl_amemthresh = 1024;
3896 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3897 ipvs->sysctl_am_droprate = 10;
3898 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3899 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3900 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3901 #ifdef CONFIG_IP_VS_NFCT
3902 tbl[idx++].data = &ipvs->sysctl_conntrack;
3904 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3905 ipvs->sysctl_snat_reroute = 1;
3906 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3907 ipvs->sysctl_sync_ver = 1;
3908 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3909 ipvs->sysctl_sync_ports = 1;
3910 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3911 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3912 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3913 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3914 ipvs->sysctl_sync_sock_size = 0;
3915 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3916 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3917 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3918 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3919 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3920 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3921 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3922 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3923 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3924 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3925 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3926 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3927 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3928 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3929 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3930 ipvs->sysctl_pmtu_disc = 1;
3931 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3932 tbl[idx++].data = &ipvs->sysctl_backup_only;
3933 ipvs->sysctl_conn_reuse_mode = 1;
3934 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
3935 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
3936 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
3938 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3939 if (ipvs->sysctl_hdr == NULL) {
3940 if (!net_eq(net, &init_net))
3944 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
3945 ipvs->sysctl_tbl = tbl;
3946 /* Schedule defense work */
3947 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3948 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3953 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
3955 struct net *net = ipvs->net;
3957 cancel_delayed_work_sync(&ipvs->defense_work);
3958 cancel_work_sync(&ipvs->defense_work.work);
3959 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3960 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
3962 if (!net_eq(net, &init_net))
3963 kfree(ipvs->sysctl_tbl);
3968 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
3969 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
3973 static struct notifier_block ip_vs_dst_notifier = {
3974 .notifier_call = ip_vs_dst_event,
3977 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
3981 /* Initialize rs_table */
3982 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3983 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3985 INIT_LIST_HEAD(&ipvs->dest_trash);
3986 spin_lock_init(&ipvs->dest_trash_lock);
3987 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
3988 atomic_set(&ipvs->ftpsvc_counter, 0);
3989 atomic_set(&ipvs->nullsvc_counter, 0);
3990 atomic_set(&ipvs->conn_out_counter, 0);
3993 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3994 if (!ipvs->tot_stats.cpustats)
3997 for_each_possible_cpu(i) {
3998 struct ip_vs_cpu_stats *ipvs_tot_stats;
3999 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4000 u64_stats_init(&ipvs_tot_stats->syncp);
4003 spin_lock_init(&ipvs->tot_stats.lock);
4005 proc_create_net("ip_vs", 0, ipvs->net->proc_net, &ip_vs_info_seq_ops,
4006 sizeof(struct ip_vs_iter));
4007 proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4008 ip_vs_stats_show, NULL);
4009 proc_create_net_single("ip_vs_stats_percpu", 0, ipvs->net->proc_net,
4010 ip_vs_stats_percpu_show, NULL);
4012 if (ip_vs_control_net_init_sysctl(ipvs))
4018 free_percpu(ipvs->tot_stats.cpustats);
4022 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4024 ip_vs_trash_cleanup(ipvs);
4025 ip_vs_control_net_cleanup_sysctl(ipvs);
4026 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4027 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4028 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4029 free_percpu(ipvs->tot_stats.cpustats);
4032 int __init ip_vs_register_nl_ioctl(void)
4036 ret = nf_register_sockopt(&ip_vs_sockopts);
4038 pr_err("cannot register sockopt.\n");
4042 ret = ip_vs_genl_register();
4044 pr_err("cannot register Generic Netlink interface.\n");
4050 nf_unregister_sockopt(&ip_vs_sockopts);
4055 void ip_vs_unregister_nl_ioctl(void)
4057 ip_vs_genl_unregister();
4058 nf_unregister_sockopt(&ip_vs_sockopts);
4061 int __init ip_vs_control_init(void)
4068 /* Initialize svc_table, ip_vs_svc_fwm_table */
4069 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4070 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4071 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4074 smp_wmb(); /* Do we really need it now ? */
4076 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4085 void ip_vs_control_cleanup(void)
4088 unregister_netdevice_notifier(&ip_vs_dst_notifier);