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 /* keep the last_weight with latest non-0 weight */
825 if (add || udest->weight != 0)
826 atomic_set(&dest->last_weight, udest->weight);
828 /* set the weight and the flags */
829 atomic_set(&dest->weight, udest->weight);
830 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
831 conn_flags |= IP_VS_CONN_F_INACTIVE;
833 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
834 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
835 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
838 * Put the real service in rs_table if not present.
839 * For now only for NAT!
841 ip_vs_rs_hash(ipvs, dest);
842 /* FTP-NAT requires conntrack for mangling */
843 if (svc->port == FTPPORT)
844 ip_vs_register_conntrack(svc);
846 atomic_set(&dest->conn_flags, conn_flags);
848 /* bind the service */
849 old_svc = rcu_dereference_protected(dest->svc, 1);
851 __ip_vs_bind_svc(dest, svc);
853 if (old_svc != svc) {
854 ip_vs_zero_stats(&dest->stats);
855 __ip_vs_bind_svc(dest, svc);
856 __ip_vs_svc_put(old_svc, true);
860 /* set the dest status flags */
861 dest->flags |= IP_VS_DEST_F_AVAILABLE;
863 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
864 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
865 dest->u_threshold = udest->u_threshold;
866 dest->l_threshold = udest->l_threshold;
868 dest->af = udest->af;
870 spin_lock_bh(&dest->dst_lock);
871 __ip_vs_dst_cache_reset(dest);
872 spin_unlock_bh(&dest->dst_lock);
875 ip_vs_start_estimator(svc->ipvs, &dest->stats);
876 list_add_rcu(&dest->n_list, &svc->destinations);
878 sched = rcu_dereference_protected(svc->scheduler, 1);
879 if (sched && sched->add_dest)
880 sched->add_dest(svc, dest);
882 sched = rcu_dereference_protected(svc->scheduler, 1);
883 if (sched && sched->upd_dest)
884 sched->upd_dest(svc, dest);
890 * Create a destination for the given service
893 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
894 struct ip_vs_dest **dest_p)
896 struct ip_vs_dest *dest;
897 unsigned int atype, i;
901 #ifdef CONFIG_IP_VS_IPV6
902 if (udest->af == AF_INET6) {
903 atype = ipv6_addr_type(&udest->addr.in6);
904 if ((!(atype & IPV6_ADDR_UNICAST) ||
905 atype & IPV6_ADDR_LINKLOCAL) &&
906 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
911 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
912 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
916 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
920 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
921 if (!dest->stats.cpustats)
924 for_each_possible_cpu(i) {
925 struct ip_vs_cpu_stats *ip_vs_dest_stats;
926 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
927 u64_stats_init(&ip_vs_dest_stats->syncp);
930 dest->af = udest->af;
931 dest->protocol = svc->protocol;
932 dest->vaddr = svc->addr;
933 dest->vport = svc->port;
934 dest->vfwmark = svc->fwmark;
935 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
936 dest->port = udest->port;
938 atomic_set(&dest->activeconns, 0);
939 atomic_set(&dest->inactconns, 0);
940 atomic_set(&dest->persistconns, 0);
941 refcount_set(&dest->refcnt, 1);
943 INIT_HLIST_NODE(&dest->d_list);
944 spin_lock_init(&dest->dst_lock);
945 spin_lock_init(&dest->stats.lock);
946 __ip_vs_update_dest(svc, dest, udest, 1);
960 * Add a destination into an existing service
963 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
965 struct ip_vs_dest *dest;
966 union nf_inet_addr daddr;
967 __be16 dport = udest->port;
972 if (udest->weight < 0) {
973 pr_err("%s(): server weight less than zero\n", __func__);
977 if (udest->l_threshold > udest->u_threshold) {
978 pr_err("%s(): lower threshold is higher than upper threshold\n",
983 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
985 /* We use function that requires RCU lock */
987 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
991 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
996 * Check if the dest already exists in the trash and
997 * is from the same service
999 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
1002 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1003 "dest->refcnt=%d, service %u/%s:%u\n",
1004 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
1005 refcount_read(&dest->refcnt),
1007 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1008 ntohs(dest->vport));
1010 __ip_vs_update_dest(svc, dest, udest, 1);
1014 * Allocate and initialize the dest structure
1016 ret = ip_vs_new_dest(svc, udest, &dest);
1025 * Edit a destination in the given service
1028 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1030 struct ip_vs_dest *dest;
1031 union nf_inet_addr daddr;
1032 __be16 dport = udest->port;
1036 if (udest->weight < 0) {
1037 pr_err("%s(): server weight less than zero\n", __func__);
1041 if (udest->l_threshold > udest->u_threshold) {
1042 pr_err("%s(): lower threshold is higher than upper threshold\n",
1047 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1049 /* We use function that requires RCU lock */
1051 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1055 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1059 __ip_vs_update_dest(svc, dest, udest, 0);
1066 * Delete a destination (must be already unlinked from the service)
1068 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1071 ip_vs_stop_estimator(ipvs, &dest->stats);
1074 * Remove it from the d-linked list with the real services.
1076 ip_vs_rs_unhash(dest);
1078 spin_lock_bh(&ipvs->dest_trash_lock);
1079 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1080 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1081 refcount_read(&dest->refcnt));
1082 if (list_empty(&ipvs->dest_trash) && !cleanup)
1083 mod_timer(&ipvs->dest_trash_timer,
1084 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1085 /* dest lives in trash with reference */
1086 list_add(&dest->t_list, &ipvs->dest_trash);
1087 dest->idle_start = 0;
1088 spin_unlock_bh(&ipvs->dest_trash_lock);
1093 * Unlink a destination from the given service
1095 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1096 struct ip_vs_dest *dest,
1099 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1102 * Remove it from the d-linked destination list.
1104 list_del_rcu(&dest->n_list);
1107 if (dest->af != svc->af)
1108 svc->ipvs->mixed_address_family_dests--;
1111 struct ip_vs_scheduler *sched;
1113 sched = rcu_dereference_protected(svc->scheduler, 1);
1114 if (sched && sched->del_dest)
1115 sched->del_dest(svc, dest);
1121 * Delete a destination server in the given service
1124 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1126 struct ip_vs_dest *dest;
1127 __be16 dport = udest->port;
1131 /* We use function that requires RCU lock */
1133 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1137 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1142 * Unlink dest from the service
1144 __ip_vs_unlink_dest(svc, dest, 1);
1147 * Delete the destination
1149 __ip_vs_del_dest(svc->ipvs, dest, false);
1156 static void ip_vs_dest_trash_expire(struct timer_list *t)
1158 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1159 struct ip_vs_dest *dest, *next;
1160 unsigned long now = jiffies;
1162 spin_lock(&ipvs->dest_trash_lock);
1163 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1164 if (refcount_read(&dest->refcnt) > 1)
1166 if (dest->idle_start) {
1167 if (time_before(now, dest->idle_start +
1168 IP_VS_DEST_TRASH_PERIOD))
1171 dest->idle_start = max(1UL, now);
1174 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1176 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1178 list_del(&dest->t_list);
1179 ip_vs_dest_free(dest);
1181 if (!list_empty(&ipvs->dest_trash))
1182 mod_timer(&ipvs->dest_trash_timer,
1183 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1184 spin_unlock(&ipvs->dest_trash_lock);
1188 * Add a service into the service hash table
1191 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1192 struct ip_vs_service **svc_p)
1195 struct ip_vs_scheduler *sched = NULL;
1196 struct ip_vs_pe *pe = NULL;
1197 struct ip_vs_service *svc = NULL;
1199 /* increase the module use count */
1200 ip_vs_use_count_inc();
1202 /* Lookup the scheduler by 'u->sched_name' */
1203 if (strcmp(u->sched_name, "none")) {
1204 sched = ip_vs_scheduler_get(u->sched_name);
1206 pr_info("Scheduler module ip_vs_%s not found\n",
1213 if (u->pe_name && *u->pe_name) {
1214 pe = ip_vs_pe_getbyname(u->pe_name);
1216 pr_info("persistence engine module ip_vs_pe_%s "
1217 "not found\n", u->pe_name);
1223 #ifdef CONFIG_IP_VS_IPV6
1224 if (u->af == AF_INET6) {
1225 __u32 plen = (__force __u32) u->netmask;
1227 if (plen < 1 || plen > 128) {
1234 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1236 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1240 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1241 if (!svc->stats.cpustats) {
1246 for_each_possible_cpu(i) {
1247 struct ip_vs_cpu_stats *ip_vs_stats;
1248 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1249 u64_stats_init(&ip_vs_stats->syncp);
1253 /* I'm the first user of the service */
1254 atomic_set(&svc->refcnt, 0);
1257 svc->protocol = u->protocol;
1258 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1259 svc->port = u->port;
1260 svc->fwmark = u->fwmark;
1261 svc->flags = u->flags;
1262 svc->timeout = u->timeout * HZ;
1263 svc->netmask = u->netmask;
1266 INIT_LIST_HEAD(&svc->destinations);
1267 spin_lock_init(&svc->sched_lock);
1268 spin_lock_init(&svc->stats.lock);
1270 /* Bind the scheduler */
1272 ret = ip_vs_bind_scheduler(svc, sched);
1278 /* Bind the ct retriever */
1279 RCU_INIT_POINTER(svc->pe, pe);
1282 /* Update the virtual service counters */
1283 if (svc->port == FTPPORT)
1284 atomic_inc(&ipvs->ftpsvc_counter);
1285 else if (svc->port == 0)
1286 atomic_inc(&ipvs->nullsvc_counter);
1287 if (svc->pe && svc->pe->conn_out)
1288 atomic_inc(&ipvs->conn_out_counter);
1290 ip_vs_start_estimator(ipvs, &svc->stats);
1292 /* Count only IPv4 services for old get/setsockopt interface */
1293 if (svc->af == AF_INET)
1294 ipvs->num_services++;
1296 /* Hash the service into the service table */
1297 ip_vs_svc_hash(svc);
1300 /* Now there is a service - full throttle */
1307 ip_vs_unbind_scheduler(svc, sched);
1308 ip_vs_service_free(svc);
1310 ip_vs_scheduler_put(sched);
1313 /* decrease the module use count */
1314 ip_vs_use_count_dec();
1321 * Edit a service and bind it with a new scheduler
1324 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1326 struct ip_vs_scheduler *sched = NULL, *old_sched;
1327 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1329 bool new_pe_conn_out, old_pe_conn_out;
1332 * Lookup the scheduler, by 'u->sched_name'
1334 if (strcmp(u->sched_name, "none")) {
1335 sched = ip_vs_scheduler_get(u->sched_name);
1337 pr_info("Scheduler module ip_vs_%s not found\n",
1344 if (u->pe_name && *u->pe_name) {
1345 pe = ip_vs_pe_getbyname(u->pe_name);
1347 pr_info("persistence engine module ip_vs_pe_%s "
1348 "not found\n", u->pe_name);
1355 #ifdef CONFIG_IP_VS_IPV6
1356 if (u->af == AF_INET6) {
1357 __u32 plen = (__force __u32) u->netmask;
1359 if (plen < 1 || plen > 128) {
1366 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1367 if (sched != old_sched) {
1369 ip_vs_unbind_scheduler(svc, old_sched);
1370 RCU_INIT_POINTER(svc->scheduler, NULL);
1371 /* Wait all svc->sched_data users */
1374 /* Bind the new scheduler */
1376 ret = ip_vs_bind_scheduler(svc, sched);
1378 ip_vs_scheduler_put(sched);
1385 * Set the flags and timeout value
1387 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1388 svc->timeout = u->timeout * HZ;
1389 svc->netmask = u->netmask;
1391 old_pe = rcu_dereference_protected(svc->pe, 1);
1393 rcu_assign_pointer(svc->pe, pe);
1394 /* check for optional methods in new pe */
1395 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1396 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1397 if (new_pe_conn_out && !old_pe_conn_out)
1398 atomic_inc(&svc->ipvs->conn_out_counter);
1399 if (old_pe_conn_out && !new_pe_conn_out)
1400 atomic_dec(&svc->ipvs->conn_out_counter);
1404 ip_vs_scheduler_put(old_sched);
1405 ip_vs_pe_put(old_pe);
1410 * Delete a service from the service list
1411 * - The service must be unlinked, unlocked and not referenced!
1412 * - We are called under _bh lock
1414 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1416 struct ip_vs_dest *dest, *nxt;
1417 struct ip_vs_scheduler *old_sched;
1418 struct ip_vs_pe *old_pe;
1419 struct netns_ipvs *ipvs = svc->ipvs;
1421 /* Count only IPv4 services for old get/setsockopt interface */
1422 if (svc->af == AF_INET)
1423 ipvs->num_services--;
1425 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1427 /* Unbind scheduler */
1428 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1429 ip_vs_unbind_scheduler(svc, old_sched);
1430 ip_vs_scheduler_put(old_sched);
1432 /* Unbind persistence engine, keep svc->pe */
1433 old_pe = rcu_dereference_protected(svc->pe, 1);
1434 if (old_pe && old_pe->conn_out)
1435 atomic_dec(&ipvs->conn_out_counter);
1436 ip_vs_pe_put(old_pe);
1439 * Unlink the whole destination list
1441 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1442 __ip_vs_unlink_dest(svc, dest, 0);
1443 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1447 * Update the virtual service counters
1449 if (svc->port == FTPPORT)
1450 atomic_dec(&ipvs->ftpsvc_counter);
1451 else if (svc->port == 0)
1452 atomic_dec(&ipvs->nullsvc_counter);
1455 * Free the service if nobody refers to it
1457 __ip_vs_svc_put(svc, true);
1459 /* decrease the module use count */
1460 ip_vs_use_count_dec();
1464 * Unlink a service from list and try to delete it if its refcnt reached 0
1466 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1468 ip_vs_unregister_conntrack(svc);
1469 /* Hold svc to avoid double release from dest_trash */
1470 atomic_inc(&svc->refcnt);
1472 * Unhash it from the service table
1474 ip_vs_svc_unhash(svc);
1476 __ip_vs_del_service(svc, cleanup);
1480 * Delete a service from the service list
1482 static int ip_vs_del_service(struct ip_vs_service *svc)
1486 ip_vs_unlink_service(svc, false);
1493 * Flush all the virtual services
1495 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1498 struct ip_vs_service *svc;
1499 struct hlist_node *n;
1502 * Flush the service table hashed by <netns,protocol,addr,port>
1504 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1505 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1507 if (svc->ipvs == ipvs)
1508 ip_vs_unlink_service(svc, cleanup);
1513 * Flush the service table hashed by fwmark
1515 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1516 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1518 if (svc->ipvs == ipvs)
1519 ip_vs_unlink_service(svc, cleanup);
1527 * Delete service by {netns} in the service table.
1528 * Called by __ip_vs_cleanup()
1530 void ip_vs_service_net_cleanup(struct netns_ipvs *ipvs)
1533 /* Check for "full" addressed entries */
1534 mutex_lock(&__ip_vs_mutex);
1535 ip_vs_flush(ipvs, true);
1536 mutex_unlock(&__ip_vs_mutex);
1540 /* Put all references for device (dst_cache) */
1542 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1544 struct ip_vs_dest_dst *dest_dst;
1546 spin_lock_bh(&dest->dst_lock);
1547 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1548 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1549 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1551 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1553 refcount_read(&dest->refcnt));
1554 __ip_vs_dst_cache_reset(dest);
1556 spin_unlock_bh(&dest->dst_lock);
1559 /* Netdev event receiver
1560 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1562 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1565 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1566 struct net *net = dev_net(dev);
1567 struct netns_ipvs *ipvs = net_ipvs(net);
1568 struct ip_vs_service *svc;
1569 struct ip_vs_dest *dest;
1572 if (event != NETDEV_DOWN || !ipvs)
1574 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1576 mutex_lock(&__ip_vs_mutex);
1577 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1578 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1579 if (svc->ipvs == ipvs) {
1580 list_for_each_entry(dest, &svc->destinations,
1582 ip_vs_forget_dev(dest, dev);
1587 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1588 if (svc->ipvs == ipvs) {
1589 list_for_each_entry(dest, &svc->destinations,
1591 ip_vs_forget_dev(dest, dev);
1598 spin_lock_bh(&ipvs->dest_trash_lock);
1599 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1600 ip_vs_forget_dev(dest, dev);
1602 spin_unlock_bh(&ipvs->dest_trash_lock);
1603 mutex_unlock(&__ip_vs_mutex);
1609 * Zero counters in a service or all services
1611 static int ip_vs_zero_service(struct ip_vs_service *svc)
1613 struct ip_vs_dest *dest;
1615 list_for_each_entry(dest, &svc->destinations, n_list) {
1616 ip_vs_zero_stats(&dest->stats);
1618 ip_vs_zero_stats(&svc->stats);
1622 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1625 struct ip_vs_service *svc;
1627 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1628 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1629 if (svc->ipvs == ipvs)
1630 ip_vs_zero_service(svc);
1634 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1635 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1636 if (svc->ipvs == ipvs)
1637 ip_vs_zero_service(svc);
1641 ip_vs_zero_stats(&ipvs->tot_stats);
1645 #ifdef CONFIG_SYSCTL
1648 static int three = 3;
1651 proc_do_defense_mode(struct ctl_table *table, int write,
1652 void __user *buffer, size_t *lenp, loff_t *ppos)
1654 struct netns_ipvs *ipvs = table->extra2;
1655 int *valp = table->data;
1659 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1660 if (write && (*valp != val)) {
1661 if ((*valp < 0) || (*valp > 3)) {
1662 /* Restore the correct value */
1665 update_defense_level(ipvs);
1672 proc_do_sync_threshold(struct ctl_table *table, int write,
1673 void __user *buffer, size_t *lenp, loff_t *ppos)
1675 int *valp = table->data;
1679 /* backup the value first */
1680 memcpy(val, valp, sizeof(val));
1682 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1683 if (write && (valp[0] < 0 || valp[1] < 0 ||
1684 (valp[0] >= valp[1] && valp[1]))) {
1685 /* Restore the correct value */
1686 memcpy(valp, val, sizeof(val));
1692 proc_do_sync_mode(struct ctl_table *table, int write,
1693 void __user *buffer, size_t *lenp, loff_t *ppos)
1695 int *valp = table->data;
1699 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1700 if (write && (*valp != val)) {
1701 if ((*valp < 0) || (*valp > 1)) {
1702 /* Restore the correct value */
1710 proc_do_sync_ports(struct ctl_table *table, int write,
1711 void __user *buffer, size_t *lenp, loff_t *ppos)
1713 int *valp = table->data;
1717 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1718 if (write && (*valp != val)) {
1719 if (*valp < 1 || !is_power_of_2(*valp)) {
1720 /* Restore the correct value */
1728 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1729 * Do not change order or insert new entries without
1730 * align with netns init in ip_vs_control_net_init()
1733 static struct ctl_table vs_vars[] = {
1735 .procname = "amemthresh",
1736 .maxlen = sizeof(int),
1738 .proc_handler = proc_dointvec,
1741 .procname = "am_droprate",
1742 .maxlen = sizeof(int),
1744 .proc_handler = proc_dointvec,
1747 .procname = "drop_entry",
1748 .maxlen = sizeof(int),
1750 .proc_handler = proc_do_defense_mode,
1753 .procname = "drop_packet",
1754 .maxlen = sizeof(int),
1756 .proc_handler = proc_do_defense_mode,
1758 #ifdef CONFIG_IP_VS_NFCT
1760 .procname = "conntrack",
1761 .maxlen = sizeof(int),
1763 .proc_handler = &proc_dointvec,
1767 .procname = "secure_tcp",
1768 .maxlen = sizeof(int),
1770 .proc_handler = proc_do_defense_mode,
1773 .procname = "snat_reroute",
1774 .maxlen = sizeof(int),
1776 .proc_handler = &proc_dointvec,
1779 .procname = "sync_version",
1780 .maxlen = sizeof(int),
1782 .proc_handler = proc_do_sync_mode,
1785 .procname = "sync_ports",
1786 .maxlen = sizeof(int),
1788 .proc_handler = proc_do_sync_ports,
1791 .procname = "sync_persist_mode",
1792 .maxlen = sizeof(int),
1794 .proc_handler = proc_dointvec,
1797 .procname = "sync_qlen_max",
1798 .maxlen = sizeof(unsigned long),
1800 .proc_handler = proc_doulongvec_minmax,
1803 .procname = "sync_sock_size",
1804 .maxlen = sizeof(int),
1806 .proc_handler = proc_dointvec,
1809 .procname = "cache_bypass",
1810 .maxlen = sizeof(int),
1812 .proc_handler = proc_dointvec,
1815 .procname = "expire_nodest_conn",
1816 .maxlen = sizeof(int),
1818 .proc_handler = proc_dointvec,
1821 .procname = "sloppy_tcp",
1822 .maxlen = sizeof(int),
1824 .proc_handler = proc_dointvec,
1827 .procname = "sloppy_sctp",
1828 .maxlen = sizeof(int),
1830 .proc_handler = proc_dointvec,
1833 .procname = "expire_quiescent_template",
1834 .maxlen = sizeof(int),
1836 .proc_handler = proc_dointvec,
1839 .procname = "sync_threshold",
1841 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1843 .proc_handler = proc_do_sync_threshold,
1846 .procname = "sync_refresh_period",
1847 .maxlen = sizeof(int),
1849 .proc_handler = proc_dointvec_jiffies,
1852 .procname = "sync_retries",
1853 .maxlen = sizeof(int),
1855 .proc_handler = proc_dointvec_minmax,
1860 .procname = "nat_icmp_send",
1861 .maxlen = sizeof(int),
1863 .proc_handler = proc_dointvec,
1866 .procname = "pmtu_disc",
1867 .maxlen = sizeof(int),
1869 .proc_handler = proc_dointvec,
1872 .procname = "backup_only",
1873 .maxlen = sizeof(int),
1875 .proc_handler = proc_dointvec,
1878 .procname = "conn_reuse_mode",
1879 .maxlen = sizeof(int),
1881 .proc_handler = proc_dointvec,
1884 .procname = "schedule_icmp",
1885 .maxlen = sizeof(int),
1887 .proc_handler = proc_dointvec,
1890 .procname = "ignore_tunneled",
1891 .maxlen = sizeof(int),
1893 .proc_handler = proc_dointvec,
1895 #ifdef CONFIG_IP_VS_DEBUG
1897 .procname = "debug_level",
1898 .data = &sysctl_ip_vs_debug_level,
1899 .maxlen = sizeof(int),
1901 .proc_handler = proc_dointvec,
1909 #ifdef CONFIG_PROC_FS
1912 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1913 struct hlist_head *table;
1918 * Write the contents of the VS rule table to a PROCfs file.
1919 * (It is kept just for backward compatibility)
1921 static inline const char *ip_vs_fwd_name(unsigned int flags)
1923 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1924 case IP_VS_CONN_F_LOCALNODE:
1926 case IP_VS_CONN_F_TUNNEL:
1928 case IP_VS_CONN_F_DROUTE:
1936 /* Get the Nth entry in the two lists */
1937 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1939 struct net *net = seq_file_net(seq);
1940 struct netns_ipvs *ipvs = net_ipvs(net);
1941 struct ip_vs_iter *iter = seq->private;
1943 struct ip_vs_service *svc;
1945 /* look in hash by protocol */
1946 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1947 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1948 if ((svc->ipvs == ipvs) && pos-- == 0) {
1949 iter->table = ip_vs_svc_table;
1956 /* keep looking in fwmark */
1957 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1958 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1960 if ((svc->ipvs == ipvs) && pos-- == 0) {
1961 iter->table = ip_vs_svc_fwm_table;
1971 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1975 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1979 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1981 struct hlist_node *e;
1982 struct ip_vs_iter *iter;
1983 struct ip_vs_service *svc;
1986 if (v == SEQ_START_TOKEN)
1987 return ip_vs_info_array(seq,0);
1990 iter = seq->private;
1992 if (iter->table == ip_vs_svc_table) {
1993 /* next service in table hashed by protocol */
1994 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1996 return hlist_entry(e, struct ip_vs_service, s_list);
1998 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1999 hlist_for_each_entry_rcu(svc,
2000 &ip_vs_svc_table[iter->bucket],
2006 iter->table = ip_vs_svc_fwm_table;
2011 /* next service in hashed by fwmark */
2012 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2014 return hlist_entry(e, struct ip_vs_service, f_list);
2017 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2018 hlist_for_each_entry_rcu(svc,
2019 &ip_vs_svc_fwm_table[iter->bucket],
2027 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2034 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2036 if (v == SEQ_START_TOKEN) {
2038 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2039 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2041 "Prot LocalAddress:Port Scheduler Flags\n");
2043 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2045 struct net *net = seq_file_net(seq);
2046 struct netns_ipvs *ipvs = net_ipvs(net);
2047 const struct ip_vs_service *svc = v;
2048 const struct ip_vs_iter *iter = seq->private;
2049 const struct ip_vs_dest *dest;
2050 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2051 char *sched_name = sched ? sched->name : "none";
2053 if (svc->ipvs != ipvs)
2055 if (iter->table == ip_vs_svc_table) {
2056 #ifdef CONFIG_IP_VS_IPV6
2057 if (svc->af == AF_INET6)
2058 seq_printf(seq, "%s [%pI6]:%04X %s ",
2059 ip_vs_proto_name(svc->protocol),
2065 seq_printf(seq, "%s %08X:%04X %s %s ",
2066 ip_vs_proto_name(svc->protocol),
2067 ntohl(svc->addr.ip),
2070 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2072 seq_printf(seq, "FWM %08X %s %s",
2073 svc->fwmark, sched_name,
2074 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2077 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2078 seq_printf(seq, "persistent %d %08X\n",
2080 ntohl(svc->netmask));
2082 seq_putc(seq, '\n');
2084 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2085 #ifdef CONFIG_IP_VS_IPV6
2086 if (dest->af == AF_INET6)
2089 " %-7s %-6d %-10d %-10d\n",
2092 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2093 atomic_read(&dest->weight),
2094 atomic_read(&dest->activeconns),
2095 atomic_read(&dest->inactconns));
2100 "%-7s %-6d %-10d %-10d\n",
2101 ntohl(dest->addr.ip),
2103 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2104 atomic_read(&dest->weight),
2105 atomic_read(&dest->activeconns),
2106 atomic_read(&dest->inactconns));
2113 static const struct seq_operations ip_vs_info_seq_ops = {
2114 .start = ip_vs_info_seq_start,
2115 .next = ip_vs_info_seq_next,
2116 .stop = ip_vs_info_seq_stop,
2117 .show = ip_vs_info_seq_show,
2120 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2122 struct net *net = seq_file_single_net(seq);
2123 struct ip_vs_kstats show;
2125 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2127 " Total Incoming Outgoing Incoming Outgoing\n");
2129 " Conns Packets Packets Bytes Bytes\n");
2131 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2132 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2133 (unsigned long long)show.conns,
2134 (unsigned long long)show.inpkts,
2135 (unsigned long long)show.outpkts,
2136 (unsigned long long)show.inbytes,
2137 (unsigned long long)show.outbytes);
2139 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2141 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2142 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2143 (unsigned long long)show.cps,
2144 (unsigned long long)show.inpps,
2145 (unsigned long long)show.outpps,
2146 (unsigned long long)show.inbps,
2147 (unsigned long long)show.outbps);
2152 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2154 struct net *net = seq_file_single_net(seq);
2155 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2156 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2157 struct ip_vs_kstats kstats;
2160 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2162 " Total Incoming Outgoing Incoming Outgoing\n");
2164 "CPU Conns Packets Packets Bytes Bytes\n");
2166 for_each_possible_cpu(i) {
2167 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2169 u64 conns, inpkts, outpkts, inbytes, outbytes;
2172 start = u64_stats_fetch_begin_irq(&u->syncp);
2173 conns = u->cnt.conns;
2174 inpkts = u->cnt.inpkts;
2175 outpkts = u->cnt.outpkts;
2176 inbytes = u->cnt.inbytes;
2177 outbytes = u->cnt.outbytes;
2178 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2180 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2181 i, (u64)conns, (u64)inpkts,
2182 (u64)outpkts, (u64)inbytes,
2186 ip_vs_copy_stats(&kstats, tot_stats);
2188 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2189 (unsigned long long)kstats.conns,
2190 (unsigned long long)kstats.inpkts,
2191 (unsigned long long)kstats.outpkts,
2192 (unsigned long long)kstats.inbytes,
2193 (unsigned long long)kstats.outbytes);
2195 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2197 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2198 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2210 * Set timeout values for tcp tcpfin udp in the timeout_table.
2212 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2214 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2215 struct ip_vs_proto_data *pd;
2218 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2223 #ifdef CONFIG_IP_VS_PROTO_TCP
2224 if (u->tcp_timeout) {
2225 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2226 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2227 = u->tcp_timeout * HZ;
2230 if (u->tcp_fin_timeout) {
2231 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2232 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2233 = u->tcp_fin_timeout * HZ;
2237 #ifdef CONFIG_IP_VS_PROTO_UDP
2238 if (u->udp_timeout) {
2239 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2240 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2241 = u->udp_timeout * HZ;
2247 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2249 struct ip_vs_svcdest_user {
2250 struct ip_vs_service_user s;
2251 struct ip_vs_dest_user d;
2254 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2255 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2256 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2257 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2258 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2259 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2260 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2261 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2262 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2263 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2264 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2267 union ip_vs_set_arglen {
2268 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2269 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2270 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2271 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2272 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2273 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2274 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2275 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2276 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2277 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2280 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2282 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2283 struct ip_vs_service_user *usvc_compat)
2285 memset(usvc, 0, sizeof(*usvc));
2288 usvc->protocol = usvc_compat->protocol;
2289 usvc->addr.ip = usvc_compat->addr;
2290 usvc->port = usvc_compat->port;
2291 usvc->fwmark = usvc_compat->fwmark;
2293 /* Deep copy of sched_name is not needed here */
2294 usvc->sched_name = usvc_compat->sched_name;
2296 usvc->flags = usvc_compat->flags;
2297 usvc->timeout = usvc_compat->timeout;
2298 usvc->netmask = usvc_compat->netmask;
2301 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2302 struct ip_vs_dest_user *udest_compat)
2304 memset(udest, 0, sizeof(*udest));
2306 udest->addr.ip = udest_compat->addr;
2307 udest->port = udest_compat->port;
2308 udest->conn_flags = udest_compat->conn_flags;
2309 udest->weight = udest_compat->weight;
2310 udest->u_threshold = udest_compat->u_threshold;
2311 udest->l_threshold = udest_compat->l_threshold;
2312 udest->af = AF_INET;
2316 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2318 struct net *net = sock_net(sk);
2320 unsigned char arg[MAX_SET_ARGLEN];
2321 struct ip_vs_service_user *usvc_compat;
2322 struct ip_vs_service_user_kern usvc;
2323 struct ip_vs_service *svc;
2324 struct ip_vs_dest_user *udest_compat;
2325 struct ip_vs_dest_user_kern udest;
2326 struct netns_ipvs *ipvs = net_ipvs(net);
2328 BUILD_BUG_ON(sizeof(arg) > 255);
2329 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2332 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2334 if (len != set_arglen[CMDID(cmd)]) {
2335 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2336 len, set_arglen[CMDID(cmd)]);
2340 if (copy_from_user(arg, user, len) != 0)
2343 /* increase the module use count */
2344 ip_vs_use_count_inc();
2346 /* Handle daemons since they have another lock */
2347 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2348 cmd == IP_VS_SO_SET_STOPDAEMON) {
2349 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2351 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2352 struct ipvs_sync_daemon_cfg cfg;
2354 memset(&cfg, 0, sizeof(cfg));
2356 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2357 sizeof(cfg.mcast_ifn)) <= 0)
2359 cfg.syncid = dm->syncid;
2360 ret = start_sync_thread(ipvs, &cfg, dm->state);
2362 mutex_lock(&ipvs->sync_mutex);
2363 ret = stop_sync_thread(ipvs, dm->state);
2364 mutex_unlock(&ipvs->sync_mutex);
2369 mutex_lock(&__ip_vs_mutex);
2370 if (cmd == IP_VS_SO_SET_FLUSH) {
2371 /* Flush the virtual service */
2372 ret = ip_vs_flush(ipvs, false);
2374 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2375 /* Set timeout values for (tcp tcpfin udp) */
2376 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2380 usvc_compat = (struct ip_vs_service_user *)arg;
2381 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2383 /* We only use the new structs internally, so copy userspace compat
2384 * structs to extended internal versions */
2385 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2386 ip_vs_copy_udest_compat(&udest, udest_compat);
2388 if (cmd == IP_VS_SO_SET_ZERO) {
2389 /* if no service address is set, zero counters in all */
2390 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2391 ret = ip_vs_zero_all(ipvs);
2396 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2397 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2398 IP_VS_SCHEDNAME_MAXLEN) {
2403 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2404 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2405 usvc.protocol != IPPROTO_SCTP) {
2406 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2407 usvc.protocol, &usvc.addr.ip,
2413 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2415 if (usvc.fwmark == 0)
2416 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2417 &usvc.addr, usvc.port);
2419 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2422 if (cmd != IP_VS_SO_SET_ADD
2423 && (svc == NULL || svc->protocol != usvc.protocol)) {
2429 case IP_VS_SO_SET_ADD:
2433 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2435 case IP_VS_SO_SET_EDIT:
2436 ret = ip_vs_edit_service(svc, &usvc);
2438 case IP_VS_SO_SET_DEL:
2439 ret = ip_vs_del_service(svc);
2443 case IP_VS_SO_SET_ZERO:
2444 ret = ip_vs_zero_service(svc);
2446 case IP_VS_SO_SET_ADDDEST:
2447 ret = ip_vs_add_dest(svc, &udest);
2449 case IP_VS_SO_SET_EDITDEST:
2450 ret = ip_vs_edit_dest(svc, &udest);
2452 case IP_VS_SO_SET_DELDEST:
2453 ret = ip_vs_del_dest(svc, &udest);
2460 mutex_unlock(&__ip_vs_mutex);
2462 /* decrease the module use count */
2463 ip_vs_use_count_dec();
2470 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2472 struct ip_vs_scheduler *sched;
2473 struct ip_vs_kstats kstats;
2476 sched = rcu_dereference_protected(src->scheduler, 1);
2477 sched_name = sched ? sched->name : "none";
2478 dst->protocol = src->protocol;
2479 dst->addr = src->addr.ip;
2480 dst->port = src->port;
2481 dst->fwmark = src->fwmark;
2482 strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2483 dst->flags = src->flags;
2484 dst->timeout = src->timeout / HZ;
2485 dst->netmask = src->netmask;
2486 dst->num_dests = src->num_dests;
2487 ip_vs_copy_stats(&kstats, &src->stats);
2488 ip_vs_export_stats_user(&dst->stats, &kstats);
2492 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2493 const struct ip_vs_get_services *get,
2494 struct ip_vs_get_services __user *uptr)
2497 struct ip_vs_service *svc;
2498 struct ip_vs_service_entry entry;
2501 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2502 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2503 /* Only expose IPv4 entries to old interface */
2504 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2507 if (count >= get->num_services)
2509 memset(&entry, 0, sizeof(entry));
2510 ip_vs_copy_service(&entry, svc);
2511 if (copy_to_user(&uptr->entrytable[count],
2512 &entry, sizeof(entry))) {
2520 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2521 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2522 /* Only expose IPv4 entries to old interface */
2523 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2526 if (count >= get->num_services)
2528 memset(&entry, 0, sizeof(entry));
2529 ip_vs_copy_service(&entry, svc);
2530 if (copy_to_user(&uptr->entrytable[count],
2531 &entry, sizeof(entry))) {
2543 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2544 struct ip_vs_get_dests __user *uptr)
2546 struct ip_vs_service *svc;
2547 union nf_inet_addr addr = { .ip = get->addr };
2552 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2554 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2560 struct ip_vs_dest *dest;
2561 struct ip_vs_dest_entry entry;
2562 struct ip_vs_kstats kstats;
2564 memset(&entry, 0, sizeof(entry));
2565 list_for_each_entry(dest, &svc->destinations, n_list) {
2566 if (count >= get->num_dests)
2569 /* Cannot expose heterogeneous members via sockopt
2572 if (dest->af != svc->af)
2575 entry.addr = dest->addr.ip;
2576 entry.port = dest->port;
2577 entry.conn_flags = atomic_read(&dest->conn_flags);
2578 entry.weight = atomic_read(&dest->weight);
2579 entry.u_threshold = dest->u_threshold;
2580 entry.l_threshold = dest->l_threshold;
2581 entry.activeconns = atomic_read(&dest->activeconns);
2582 entry.inactconns = atomic_read(&dest->inactconns);
2583 entry.persistconns = atomic_read(&dest->persistconns);
2584 ip_vs_copy_stats(&kstats, &dest->stats);
2585 ip_vs_export_stats_user(&entry.stats, &kstats);
2586 if (copy_to_user(&uptr->entrytable[count],
2587 &entry, sizeof(entry))) {
2599 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2601 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2602 struct ip_vs_proto_data *pd;
2605 memset(u, 0, sizeof (*u));
2607 #ifdef CONFIG_IP_VS_PROTO_TCP
2608 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2609 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2610 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2612 #ifdef CONFIG_IP_VS_PROTO_UDP
2613 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2615 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2619 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2620 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2621 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2622 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2623 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2624 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2625 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2626 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2629 union ip_vs_get_arglen {
2630 char field_IP_VS_SO_GET_VERSION[64];
2631 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2632 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2633 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2634 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2635 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2636 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2639 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2642 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2644 unsigned char arg[MAX_GET_ARGLEN];
2646 unsigned int copylen;
2647 struct net *net = sock_net(sk);
2648 struct netns_ipvs *ipvs = net_ipvs(net);
2651 BUILD_BUG_ON(sizeof(arg) > 255);
2652 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2655 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2658 copylen = get_arglen[CMDID(cmd)];
2659 if (*len < (int) copylen) {
2660 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2664 if (copy_from_user(arg, user, copylen) != 0)
2667 * Handle daemons first since it has its own locking
2669 if (cmd == IP_VS_SO_GET_DAEMON) {
2670 struct ip_vs_daemon_user d[2];
2672 memset(&d, 0, sizeof(d));
2673 mutex_lock(&ipvs->sync_mutex);
2674 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2675 d[0].state = IP_VS_STATE_MASTER;
2676 strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2677 sizeof(d[0].mcast_ifn));
2678 d[0].syncid = ipvs->mcfg.syncid;
2680 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2681 d[1].state = IP_VS_STATE_BACKUP;
2682 strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2683 sizeof(d[1].mcast_ifn));
2684 d[1].syncid = ipvs->bcfg.syncid;
2686 if (copy_to_user(user, &d, sizeof(d)) != 0)
2688 mutex_unlock(&ipvs->sync_mutex);
2692 mutex_lock(&__ip_vs_mutex);
2694 case IP_VS_SO_GET_VERSION:
2698 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2699 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2700 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2704 *len = strlen(buf)+1;
2708 case IP_VS_SO_GET_INFO:
2710 struct ip_vs_getinfo info;
2711 info.version = IP_VS_VERSION_CODE;
2712 info.size = ip_vs_conn_tab_size;
2713 info.num_services = ipvs->num_services;
2714 if (copy_to_user(user, &info, sizeof(info)) != 0)
2719 case IP_VS_SO_GET_SERVICES:
2721 struct ip_vs_get_services *get;
2724 get = (struct ip_vs_get_services *)arg;
2725 size = sizeof(*get) +
2726 sizeof(struct ip_vs_service_entry) * get->num_services;
2728 pr_err("length: %u != %u\n", *len, size);
2732 ret = __ip_vs_get_service_entries(ipvs, get, user);
2736 case IP_VS_SO_GET_SERVICE:
2738 struct ip_vs_service_entry *entry;
2739 struct ip_vs_service *svc;
2740 union nf_inet_addr addr;
2742 entry = (struct ip_vs_service_entry *)arg;
2743 addr.ip = entry->addr;
2746 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2748 svc = __ip_vs_service_find(ipvs, AF_INET,
2749 entry->protocol, &addr,
2753 ip_vs_copy_service(entry, svc);
2754 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2761 case IP_VS_SO_GET_DESTS:
2763 struct ip_vs_get_dests *get;
2766 get = (struct ip_vs_get_dests *)arg;
2767 size = sizeof(*get) +
2768 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2770 pr_err("length: %u != %u\n", *len, size);
2774 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2778 case IP_VS_SO_GET_TIMEOUT:
2780 struct ip_vs_timeout_user t;
2782 __ip_vs_get_timeouts(ipvs, &t);
2783 if (copy_to_user(user, &t, sizeof(t)) != 0)
2793 mutex_unlock(&__ip_vs_mutex);
2798 static struct nf_sockopt_ops ip_vs_sockopts = {
2800 .set_optmin = IP_VS_BASE_CTL,
2801 .set_optmax = IP_VS_SO_SET_MAX+1,
2802 .set = do_ip_vs_set_ctl,
2803 .get_optmin = IP_VS_BASE_CTL,
2804 .get_optmax = IP_VS_SO_GET_MAX+1,
2805 .get = do_ip_vs_get_ctl,
2806 .owner = THIS_MODULE,
2810 * Generic Netlink interface
2813 /* IPVS genetlink family */
2814 static struct genl_family ip_vs_genl_family;
2816 /* Policy used for first-level command attributes */
2817 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2818 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2819 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2820 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2821 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2822 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2823 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2826 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2827 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2828 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2829 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2830 .len = IP_VS_IFNAME_MAXLEN - 1 },
2831 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2832 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2833 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2834 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2835 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2836 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2839 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2840 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2841 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2842 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2843 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2844 .len = sizeof(union nf_inet_addr) },
2845 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2846 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2847 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2848 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
2849 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2850 .len = IP_VS_PENAME_MAXLEN },
2851 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2852 .len = sizeof(struct ip_vs_flags) },
2853 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2854 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2855 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2858 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2859 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2860 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2861 .len = sizeof(union nf_inet_addr) },
2862 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2863 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2864 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2865 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2866 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2867 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2868 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2869 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2870 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2871 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2874 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2875 struct ip_vs_kstats *kstats)
2877 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2882 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2883 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2884 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2885 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2886 IPVS_STATS_ATTR_PAD) ||
2887 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2888 IPVS_STATS_ATTR_PAD) ||
2889 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2890 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2891 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2892 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2893 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2894 goto nla_put_failure;
2895 nla_nest_end(skb, nl_stats);
2900 nla_nest_cancel(skb, nl_stats);
2904 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2905 struct ip_vs_kstats *kstats)
2907 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2912 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
2913 IPVS_STATS_ATTR_PAD) ||
2914 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
2915 IPVS_STATS_ATTR_PAD) ||
2916 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
2917 IPVS_STATS_ATTR_PAD) ||
2918 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
2919 IPVS_STATS_ATTR_PAD) ||
2920 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
2921 IPVS_STATS_ATTR_PAD) ||
2922 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
2923 IPVS_STATS_ATTR_PAD) ||
2924 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
2925 IPVS_STATS_ATTR_PAD) ||
2926 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
2927 IPVS_STATS_ATTR_PAD) ||
2928 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
2929 IPVS_STATS_ATTR_PAD) ||
2930 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
2931 IPVS_STATS_ATTR_PAD))
2932 goto nla_put_failure;
2933 nla_nest_end(skb, nl_stats);
2938 nla_nest_cancel(skb, nl_stats);
2942 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2943 struct ip_vs_service *svc)
2945 struct ip_vs_scheduler *sched;
2946 struct ip_vs_pe *pe;
2947 struct nlattr *nl_service;
2948 struct ip_vs_flags flags = { .flags = svc->flags,
2950 struct ip_vs_kstats kstats;
2953 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2957 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2958 goto nla_put_failure;
2960 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2961 goto nla_put_failure;
2963 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2964 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2965 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2966 goto nla_put_failure;
2969 sched = rcu_dereference_protected(svc->scheduler, 1);
2970 sched_name = sched ? sched->name : "none";
2971 pe = rcu_dereference_protected(svc->pe, 1);
2972 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
2973 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2974 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2975 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2976 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2977 goto nla_put_failure;
2978 ip_vs_copy_stats(&kstats, &svc->stats);
2979 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
2980 goto nla_put_failure;
2981 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
2982 goto nla_put_failure;
2984 nla_nest_end(skb, nl_service);
2989 nla_nest_cancel(skb, nl_service);
2993 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2994 struct ip_vs_service *svc,
2995 struct netlink_callback *cb)
2999 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3000 &ip_vs_genl_family, NLM_F_MULTI,
3001 IPVS_CMD_NEW_SERVICE);
3005 if (ip_vs_genl_fill_service(skb, svc) < 0)
3006 goto nla_put_failure;
3008 genlmsg_end(skb, hdr);
3012 genlmsg_cancel(skb, hdr);
3016 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3017 struct netlink_callback *cb)
3020 int start = cb->args[0];
3021 struct ip_vs_service *svc;
3022 struct net *net = sock_net(skb->sk);
3023 struct netns_ipvs *ipvs = net_ipvs(net);
3025 mutex_lock(&__ip_vs_mutex);
3026 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3027 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3028 if (++idx <= start || (svc->ipvs != ipvs))
3030 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3032 goto nla_put_failure;
3037 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3038 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3039 if (++idx <= start || (svc->ipvs != ipvs))
3041 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3043 goto nla_put_failure;
3049 mutex_unlock(&__ip_vs_mutex);
3055 static bool ip_vs_is_af_valid(int af)
3059 #ifdef CONFIG_IP_VS_IPV6
3060 if (af == AF_INET6 && ipv6_mod_enabled())
3066 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3067 struct ip_vs_service_user_kern *usvc,
3068 struct nlattr *nla, int full_entry,
3069 struct ip_vs_service **ret_svc)
3071 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3072 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3073 struct ip_vs_service *svc;
3075 /* Parse mandatory identifying service fields first */
3077 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla,
3078 ip_vs_svc_policy, NULL))
3081 nla_af = attrs[IPVS_SVC_ATTR_AF];
3082 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3083 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3084 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3085 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3087 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3090 memset(usvc, 0, sizeof(*usvc));
3092 usvc->af = nla_get_u16(nla_af);
3093 if (!ip_vs_is_af_valid(usvc->af))
3094 return -EAFNOSUPPORT;
3097 usvc->protocol = IPPROTO_TCP;
3098 usvc->fwmark = nla_get_u32(nla_fwmark);
3100 usvc->protocol = nla_get_u16(nla_protocol);
3101 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3102 usvc->port = nla_get_be16(nla_port);
3108 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3110 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3111 &usvc->addr, usvc->port);
3115 /* If a full entry was requested, check for the additional fields */
3117 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3119 struct ip_vs_flags flags;
3121 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3122 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3123 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3124 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3125 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3127 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3130 nla_memcpy(&flags, nla_flags, sizeof(flags));
3132 /* prefill flags from service if it already exists */
3134 usvc->flags = svc->flags;
3136 /* set new flags from userland */
3137 usvc->flags = (usvc->flags & ~flags.mask) |
3138 (flags.flags & flags.mask);
3139 usvc->sched_name = nla_data(nla_sched);
3140 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3141 usvc->timeout = nla_get_u32(nla_timeout);
3142 usvc->netmask = nla_get_be32(nla_netmask);
3148 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3151 struct ip_vs_service_user_kern usvc;
3152 struct ip_vs_service *svc;
3155 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, 0, &svc);
3156 return ret ? ERR_PTR(ret) : svc;
3159 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3161 struct nlattr *nl_dest;
3162 struct ip_vs_kstats kstats;
3164 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3168 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3169 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3170 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3171 (atomic_read(&dest->conn_flags) &
3172 IP_VS_CONN_F_FWD_MASK)) ||
3173 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3174 atomic_read(&dest->weight)) ||
3175 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3176 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3177 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3178 atomic_read(&dest->activeconns)) ||
3179 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3180 atomic_read(&dest->inactconns)) ||
3181 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3182 atomic_read(&dest->persistconns)) ||
3183 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3184 goto nla_put_failure;
3185 ip_vs_copy_stats(&kstats, &dest->stats);
3186 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3187 goto nla_put_failure;
3188 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3189 goto nla_put_failure;
3191 nla_nest_end(skb, nl_dest);
3196 nla_nest_cancel(skb, nl_dest);
3200 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3201 struct netlink_callback *cb)
3205 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3206 &ip_vs_genl_family, NLM_F_MULTI,
3211 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3212 goto nla_put_failure;
3214 genlmsg_end(skb, hdr);
3218 genlmsg_cancel(skb, hdr);
3222 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3223 struct netlink_callback *cb)
3226 int start = cb->args[0];
3227 struct ip_vs_service *svc;
3228 struct ip_vs_dest *dest;
3229 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3230 struct net *net = sock_net(skb->sk);
3231 struct netns_ipvs *ipvs = net_ipvs(net);
3233 mutex_lock(&__ip_vs_mutex);
3235 /* Try to find the service for which to dump destinations */
3236 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX,
3237 ip_vs_cmd_policy, cb->extack))
3241 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3242 if (IS_ERR_OR_NULL(svc))
3245 /* Dump the destinations */
3246 list_for_each_entry(dest, &svc->destinations, n_list) {
3249 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3251 goto nla_put_failure;
3259 mutex_unlock(&__ip_vs_mutex);
3264 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3265 struct nlattr *nla, int full_entry)
3267 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3268 struct nlattr *nla_addr, *nla_port;
3269 struct nlattr *nla_addr_family;
3271 /* Parse mandatory identifying destination fields first */
3273 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla,
3274 ip_vs_dest_policy, NULL))
3277 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3278 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3279 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3281 if (!(nla_addr && nla_port))
3284 memset(udest, 0, sizeof(*udest));
3286 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3287 udest->port = nla_get_be16(nla_port);
3289 if (nla_addr_family)
3290 udest->af = nla_get_u16(nla_addr_family);
3294 /* If a full entry was requested, check for the additional fields */
3296 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3299 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3300 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3301 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3302 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3304 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3307 udest->conn_flags = nla_get_u32(nla_fwd)
3308 & IP_VS_CONN_F_FWD_MASK;
3309 udest->weight = nla_get_u32(nla_weight);
3310 udest->u_threshold = nla_get_u32(nla_u_thresh);
3311 udest->l_threshold = nla_get_u32(nla_l_thresh);
3317 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3318 struct ipvs_sync_daemon_cfg *c)
3320 struct nlattr *nl_daemon;
3322 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3326 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3327 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3328 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3329 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3330 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3331 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3332 goto nla_put_failure;
3333 #ifdef CONFIG_IP_VS_IPV6
3334 if (c->mcast_af == AF_INET6) {
3335 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3336 &c->mcast_group.in6))
3337 goto nla_put_failure;
3340 if (c->mcast_af == AF_INET &&
3341 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3343 goto nla_put_failure;
3344 nla_nest_end(skb, nl_daemon);
3349 nla_nest_cancel(skb, nl_daemon);
3353 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3354 struct ipvs_sync_daemon_cfg *c,
3355 struct netlink_callback *cb)
3358 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3359 &ip_vs_genl_family, NLM_F_MULTI,
3360 IPVS_CMD_NEW_DAEMON);
3364 if (ip_vs_genl_fill_daemon(skb, state, c))
3365 goto nla_put_failure;
3367 genlmsg_end(skb, hdr);
3371 genlmsg_cancel(skb, hdr);
3375 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3376 struct netlink_callback *cb)
3378 struct net *net = sock_net(skb->sk);
3379 struct netns_ipvs *ipvs = net_ipvs(net);
3381 mutex_lock(&ipvs->sync_mutex);
3382 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3383 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3384 &ipvs->mcfg, cb) < 0)
3385 goto nla_put_failure;
3390 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3391 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3392 &ipvs->bcfg, cb) < 0)
3393 goto nla_put_failure;
3399 mutex_unlock(&ipvs->sync_mutex);
3404 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3406 struct ipvs_sync_daemon_cfg c;
3410 memset(&c, 0, sizeof(c));
3411 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3412 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3413 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3415 strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3416 sizeof(c.mcast_ifn));
3417 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3419 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3421 c.sync_maxlen = nla_get_u16(a);
3423 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3425 c.mcast_af = AF_INET;
3426 c.mcast_group.ip = nla_get_in_addr(a);
3427 if (!ipv4_is_multicast(c.mcast_group.ip))
3430 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3432 #ifdef CONFIG_IP_VS_IPV6
3435 c.mcast_af = AF_INET6;
3436 c.mcast_group.in6 = nla_get_in6_addr(a);
3437 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3438 if (!(addr_type & IPV6_ADDR_MULTICAST))
3441 return -EAFNOSUPPORT;
3446 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3448 c.mcast_port = nla_get_u16(a);
3450 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3452 c.mcast_ttl = nla_get_u8(a);
3454 /* The synchronization protocol is incompatible with mixed family
3457 if (ipvs->mixed_address_family_dests > 0)
3460 ret = start_sync_thread(ipvs, &c,
3461 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3465 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3469 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3472 mutex_lock(&ipvs->sync_mutex);
3473 ret = stop_sync_thread(ipvs,
3474 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3475 mutex_unlock(&ipvs->sync_mutex);
3479 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3481 struct ip_vs_timeout_user t;
3483 __ip_vs_get_timeouts(ipvs, &t);
3485 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3486 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3488 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3490 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3492 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3493 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3495 return ip_vs_set_timeout(ipvs, &t);
3498 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3500 int ret = -EINVAL, cmd;
3501 struct net *net = sock_net(skb->sk);
3502 struct netns_ipvs *ipvs = net_ipvs(net);
3504 cmd = info->genlhdr->cmd;
3506 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3507 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3509 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3510 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3511 info->attrs[IPVS_CMD_ATTR_DAEMON],
3512 ip_vs_daemon_policy, info->extack))
3515 if (cmd == IPVS_CMD_NEW_DAEMON)
3516 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3518 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3525 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3527 struct ip_vs_service *svc = NULL;
3528 struct ip_vs_service_user_kern usvc;
3529 struct ip_vs_dest_user_kern udest;
3531 int need_full_svc = 0, need_full_dest = 0;
3532 struct net *net = sock_net(skb->sk);
3533 struct netns_ipvs *ipvs = net_ipvs(net);
3535 cmd = info->genlhdr->cmd;
3537 mutex_lock(&__ip_vs_mutex);
3539 if (cmd == IPVS_CMD_FLUSH) {
3540 ret = ip_vs_flush(ipvs, false);
3542 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3543 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3545 } else if (cmd == IPVS_CMD_ZERO &&
3546 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3547 ret = ip_vs_zero_all(ipvs);
3551 /* All following commands require a service argument, so check if we
3552 * received a valid one. We need a full service specification when
3553 * adding / editing a service. Only identifying members otherwise. */
3554 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3557 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3558 info->attrs[IPVS_CMD_ATTR_SERVICE],
3559 need_full_svc, &svc);
3563 /* Unless we're adding a new service, the service must already exist */
3564 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3569 /* Destination commands require a valid destination argument. For
3570 * adding / editing a destination, we need a full destination
3572 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3573 cmd == IPVS_CMD_DEL_DEST) {
3574 if (cmd != IPVS_CMD_DEL_DEST)
3577 ret = ip_vs_genl_parse_dest(&udest,
3578 info->attrs[IPVS_CMD_ATTR_DEST],
3583 /* Old protocols did not allow the user to specify address
3584 * family, so we set it to zero instead. We also didn't
3585 * allow heterogeneous pools in the old code, so it's safe
3586 * to assume that this will have the same address family as
3592 if (!ip_vs_is_af_valid(udest.af)) {
3593 ret = -EAFNOSUPPORT;
3597 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3598 /* The synchronization protocol is incompatible
3599 * with mixed family services
3601 if (ipvs->sync_state) {
3606 /* Which connection types do we support? */
3607 switch (udest.conn_flags) {
3608 case IP_VS_CONN_F_TUNNEL:
3609 /* We are able to forward this */
3619 case IPVS_CMD_NEW_SERVICE:
3621 ret = ip_vs_add_service(ipvs, &usvc, &svc);
3625 case IPVS_CMD_SET_SERVICE:
3626 ret = ip_vs_edit_service(svc, &usvc);
3628 case IPVS_CMD_DEL_SERVICE:
3629 ret = ip_vs_del_service(svc);
3630 /* do not use svc, it can be freed */
3632 case IPVS_CMD_NEW_DEST:
3633 ret = ip_vs_add_dest(svc, &udest);
3635 case IPVS_CMD_SET_DEST:
3636 ret = ip_vs_edit_dest(svc, &udest);
3638 case IPVS_CMD_DEL_DEST:
3639 ret = ip_vs_del_dest(svc, &udest);
3642 ret = ip_vs_zero_service(svc);
3649 mutex_unlock(&__ip_vs_mutex);
3654 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3656 struct sk_buff *msg;
3658 int ret, cmd, reply_cmd;
3659 struct net *net = sock_net(skb->sk);
3660 struct netns_ipvs *ipvs = net_ipvs(net);
3662 cmd = info->genlhdr->cmd;
3664 if (cmd == IPVS_CMD_GET_SERVICE)
3665 reply_cmd = IPVS_CMD_NEW_SERVICE;
3666 else if (cmd == IPVS_CMD_GET_INFO)
3667 reply_cmd = IPVS_CMD_SET_INFO;
3668 else if (cmd == IPVS_CMD_GET_CONFIG)
3669 reply_cmd = IPVS_CMD_SET_CONFIG;
3671 pr_err("unknown Generic Netlink command\n");
3675 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3679 mutex_lock(&__ip_vs_mutex);
3681 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3683 goto nla_put_failure;
3686 case IPVS_CMD_GET_SERVICE:
3688 struct ip_vs_service *svc;
3690 svc = ip_vs_genl_find_service(ipvs,
3691 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3696 ret = ip_vs_genl_fill_service(msg, svc);
3698 goto nla_put_failure;
3707 case IPVS_CMD_GET_CONFIG:
3709 struct ip_vs_timeout_user t;
3711 __ip_vs_get_timeouts(ipvs, &t);
3712 #ifdef CONFIG_IP_VS_PROTO_TCP
3713 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3715 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3717 goto nla_put_failure;
3719 #ifdef CONFIG_IP_VS_PROTO_UDP
3720 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3721 goto nla_put_failure;
3727 case IPVS_CMD_GET_INFO:
3728 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3729 IP_VS_VERSION_CODE) ||
3730 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3731 ip_vs_conn_tab_size))
3732 goto nla_put_failure;
3736 genlmsg_end(msg, reply);
3737 ret = genlmsg_reply(msg, info);
3741 pr_err("not enough space in Netlink message\n");
3747 mutex_unlock(&__ip_vs_mutex);
3753 static const struct genl_ops ip_vs_genl_ops[] = {
3755 .cmd = IPVS_CMD_NEW_SERVICE,
3756 .flags = GENL_ADMIN_PERM,
3757 .policy = ip_vs_cmd_policy,
3758 .doit = ip_vs_genl_set_cmd,
3761 .cmd = IPVS_CMD_SET_SERVICE,
3762 .flags = GENL_ADMIN_PERM,
3763 .policy = ip_vs_cmd_policy,
3764 .doit = ip_vs_genl_set_cmd,
3767 .cmd = IPVS_CMD_DEL_SERVICE,
3768 .flags = GENL_ADMIN_PERM,
3769 .policy = ip_vs_cmd_policy,
3770 .doit = ip_vs_genl_set_cmd,
3773 .cmd = IPVS_CMD_GET_SERVICE,
3774 .flags = GENL_ADMIN_PERM,
3775 .doit = ip_vs_genl_get_cmd,
3776 .dumpit = ip_vs_genl_dump_services,
3777 .policy = ip_vs_cmd_policy,
3780 .cmd = IPVS_CMD_NEW_DEST,
3781 .flags = GENL_ADMIN_PERM,
3782 .policy = ip_vs_cmd_policy,
3783 .doit = ip_vs_genl_set_cmd,
3786 .cmd = IPVS_CMD_SET_DEST,
3787 .flags = GENL_ADMIN_PERM,
3788 .policy = ip_vs_cmd_policy,
3789 .doit = ip_vs_genl_set_cmd,
3792 .cmd = IPVS_CMD_DEL_DEST,
3793 .flags = GENL_ADMIN_PERM,
3794 .policy = ip_vs_cmd_policy,
3795 .doit = ip_vs_genl_set_cmd,
3798 .cmd = IPVS_CMD_GET_DEST,
3799 .flags = GENL_ADMIN_PERM,
3800 .policy = ip_vs_cmd_policy,
3801 .dumpit = ip_vs_genl_dump_dests,
3804 .cmd = IPVS_CMD_NEW_DAEMON,
3805 .flags = GENL_ADMIN_PERM,
3806 .policy = ip_vs_cmd_policy,
3807 .doit = ip_vs_genl_set_daemon,
3810 .cmd = IPVS_CMD_DEL_DAEMON,
3811 .flags = GENL_ADMIN_PERM,
3812 .policy = ip_vs_cmd_policy,
3813 .doit = ip_vs_genl_set_daemon,
3816 .cmd = IPVS_CMD_GET_DAEMON,
3817 .flags = GENL_ADMIN_PERM,
3818 .dumpit = ip_vs_genl_dump_daemons,
3821 .cmd = IPVS_CMD_SET_CONFIG,
3822 .flags = GENL_ADMIN_PERM,
3823 .policy = ip_vs_cmd_policy,
3824 .doit = ip_vs_genl_set_cmd,
3827 .cmd = IPVS_CMD_GET_CONFIG,
3828 .flags = GENL_ADMIN_PERM,
3829 .doit = ip_vs_genl_get_cmd,
3832 .cmd = IPVS_CMD_GET_INFO,
3833 .flags = GENL_ADMIN_PERM,
3834 .doit = ip_vs_genl_get_cmd,
3837 .cmd = IPVS_CMD_ZERO,
3838 .flags = GENL_ADMIN_PERM,
3839 .policy = ip_vs_cmd_policy,
3840 .doit = ip_vs_genl_set_cmd,
3843 .cmd = IPVS_CMD_FLUSH,
3844 .flags = GENL_ADMIN_PERM,
3845 .doit = ip_vs_genl_set_cmd,
3849 static struct genl_family ip_vs_genl_family __ro_after_init = {
3851 .name = IPVS_GENL_NAME,
3852 .version = IPVS_GENL_VERSION,
3853 .maxattr = IPVS_CMD_ATTR_MAX,
3854 .netnsok = true, /* Make ipvsadm to work on netns */
3855 .module = THIS_MODULE,
3856 .ops = ip_vs_genl_ops,
3857 .n_ops = ARRAY_SIZE(ip_vs_genl_ops),
3860 static int __init ip_vs_genl_register(void)
3862 return genl_register_family(&ip_vs_genl_family);
3865 static void ip_vs_genl_unregister(void)
3867 genl_unregister_family(&ip_vs_genl_family);
3870 /* End of Generic Netlink interface definitions */
3873 * per netns intit/exit func.
3875 #ifdef CONFIG_SYSCTL
3876 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
3878 struct net *net = ipvs->net;
3880 struct ctl_table *tbl;
3882 atomic_set(&ipvs->dropentry, 0);
3883 spin_lock_init(&ipvs->dropentry_lock);
3884 spin_lock_init(&ipvs->droppacket_lock);
3885 spin_lock_init(&ipvs->securetcp_lock);
3887 if (!net_eq(net, &init_net)) {
3888 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3892 /* Don't export sysctls to unprivileged users */
3893 if (net->user_ns != &init_user_ns)
3894 tbl[0].procname = NULL;
3897 /* Initialize sysctl defaults */
3898 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
3899 if (tbl[idx].proc_handler == proc_do_defense_mode)
3900 tbl[idx].extra2 = ipvs;
3903 ipvs->sysctl_amemthresh = 1024;
3904 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3905 ipvs->sysctl_am_droprate = 10;
3906 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3907 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3908 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3909 #ifdef CONFIG_IP_VS_NFCT
3910 tbl[idx++].data = &ipvs->sysctl_conntrack;
3912 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3913 ipvs->sysctl_snat_reroute = 1;
3914 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3915 ipvs->sysctl_sync_ver = 1;
3916 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3917 ipvs->sysctl_sync_ports = 1;
3918 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3919 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3920 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3921 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3922 ipvs->sysctl_sync_sock_size = 0;
3923 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3924 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3925 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3926 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3927 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3928 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3929 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3930 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3931 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3932 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3933 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3934 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3935 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3936 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3937 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3938 ipvs->sysctl_pmtu_disc = 1;
3939 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3940 tbl[idx++].data = &ipvs->sysctl_backup_only;
3941 ipvs->sysctl_conn_reuse_mode = 1;
3942 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
3943 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
3944 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
3946 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3947 if (ipvs->sysctl_hdr == NULL) {
3948 if (!net_eq(net, &init_net))
3952 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
3953 ipvs->sysctl_tbl = tbl;
3954 /* Schedule defense work */
3955 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3956 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3961 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
3963 struct net *net = ipvs->net;
3965 cancel_delayed_work_sync(&ipvs->defense_work);
3966 cancel_work_sync(&ipvs->defense_work.work);
3967 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3968 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
3970 if (!net_eq(net, &init_net))
3971 kfree(ipvs->sysctl_tbl);
3976 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
3977 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
3981 static struct notifier_block ip_vs_dst_notifier = {
3982 .notifier_call = ip_vs_dst_event,
3983 #ifdef CONFIG_IP_VS_IPV6
3984 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
3988 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
3992 /* Initialize rs_table */
3993 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3994 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3996 INIT_LIST_HEAD(&ipvs->dest_trash);
3997 spin_lock_init(&ipvs->dest_trash_lock);
3998 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
3999 atomic_set(&ipvs->ftpsvc_counter, 0);
4000 atomic_set(&ipvs->nullsvc_counter, 0);
4001 atomic_set(&ipvs->conn_out_counter, 0);
4004 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
4005 if (!ipvs->tot_stats.cpustats)
4008 for_each_possible_cpu(i) {
4009 struct ip_vs_cpu_stats *ipvs_tot_stats;
4010 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4011 u64_stats_init(&ipvs_tot_stats->syncp);
4014 spin_lock_init(&ipvs->tot_stats.lock);
4016 proc_create_net("ip_vs", 0, ipvs->net->proc_net, &ip_vs_info_seq_ops,
4017 sizeof(struct ip_vs_iter));
4018 proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4019 ip_vs_stats_show, NULL);
4020 proc_create_net_single("ip_vs_stats_percpu", 0, ipvs->net->proc_net,
4021 ip_vs_stats_percpu_show, NULL);
4023 if (ip_vs_control_net_init_sysctl(ipvs))
4029 free_percpu(ipvs->tot_stats.cpustats);
4033 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4035 ip_vs_trash_cleanup(ipvs);
4036 ip_vs_control_net_cleanup_sysctl(ipvs);
4037 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4038 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4039 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4040 free_percpu(ipvs->tot_stats.cpustats);
4043 int __init ip_vs_register_nl_ioctl(void)
4047 ret = nf_register_sockopt(&ip_vs_sockopts);
4049 pr_err("cannot register sockopt.\n");
4053 ret = ip_vs_genl_register();
4055 pr_err("cannot register Generic Netlink interface.\n");
4061 nf_unregister_sockopt(&ip_vs_sockopts);
4066 void ip_vs_unregister_nl_ioctl(void)
4068 ip_vs_genl_unregister();
4069 nf_unregister_sockopt(&ip_vs_sockopts);
4072 int __init ip_vs_control_init(void)
4079 /* Initialize svc_table, ip_vs_svc_fwm_table */
4080 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4081 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4082 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4085 smp_wmb(); /* Do we really need it now ? */
4087 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4096 void ip_vs_control_cleanup(void)
4099 unregister_netdevice_notifier(&ip_vs_dst_notifier);