1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS An implementation of the IP virtual server support for the
4 * LINUX operating system. IPVS is now implemented as a module
5 * over the NetFilter framework. IPVS can be used to build a
6 * high-performance and highly available server based on a
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/capability.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/workqueue.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/mutex.h>
34 #include <net/net_namespace.h>
35 #include <linux/nsproxy.h>
37 #ifdef CONFIG_IP_VS_IPV6
39 #include <net/ip6_route.h>
40 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
42 #include <net/route.h>
44 #include <net/genetlink.h>
46 #include <linux/uaccess.h>
48 #include <net/ip_vs.h>
50 MODULE_ALIAS_GENL_FAMILY(IPVS_GENL_NAME);
52 DEFINE_MUTEX(__ip_vs_mutex); /* Serialize configuration with sockopt/netlink */
54 /* sysctl variables */
56 #ifdef CONFIG_IP_VS_DEBUG
57 static int sysctl_ip_vs_debug_level = 0;
59 int ip_vs_get_debug_level(void)
61 return sysctl_ip_vs_debug_level;
67 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
70 #ifdef CONFIG_IP_VS_IPV6
71 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
72 static bool __ip_vs_addr_is_local_v6(struct net *net,
73 const struct in6_addr *addr)
78 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
81 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
90 * update_defense_level is called from keventd and from sysctl,
91 * so it needs to protect itself from softirqs
93 static void update_defense_level(struct netns_ipvs *ipvs)
100 /* we only count free and buffered memory (in pages) */
102 availmem = i.freeram + i.bufferram;
103 /* however in linux 2.5 the i.bufferram is total page cache size,
105 /* si_swapinfo(&i); */
106 /* availmem = availmem - (i.totalswap - i.freeswap); */
108 nomem = (availmem < ipvs->sysctl_amemthresh);
113 spin_lock(&ipvs->dropentry_lock);
114 switch (ipvs->sysctl_drop_entry) {
116 atomic_set(&ipvs->dropentry, 0);
120 atomic_set(&ipvs->dropentry, 1);
121 ipvs->sysctl_drop_entry = 2;
123 atomic_set(&ipvs->dropentry, 0);
128 atomic_set(&ipvs->dropentry, 1);
130 atomic_set(&ipvs->dropentry, 0);
131 ipvs->sysctl_drop_entry = 1;
135 atomic_set(&ipvs->dropentry, 1);
138 spin_unlock(&ipvs->dropentry_lock);
141 spin_lock(&ipvs->droppacket_lock);
142 switch (ipvs->sysctl_drop_packet) {
148 ipvs->drop_rate = ipvs->drop_counter
149 = ipvs->sysctl_amemthresh /
150 (ipvs->sysctl_amemthresh-availmem);
151 ipvs->sysctl_drop_packet = 2;
158 ipvs->drop_rate = ipvs->drop_counter
159 = ipvs->sysctl_amemthresh /
160 (ipvs->sysctl_amemthresh-availmem);
163 ipvs->sysctl_drop_packet = 1;
167 ipvs->drop_rate = ipvs->sysctl_am_droprate;
170 spin_unlock(&ipvs->droppacket_lock);
173 spin_lock(&ipvs->securetcp_lock);
174 switch (ipvs->sysctl_secure_tcp) {
176 if (ipvs->old_secure_tcp >= 2)
181 if (ipvs->old_secure_tcp < 2)
183 ipvs->sysctl_secure_tcp = 2;
185 if (ipvs->old_secure_tcp >= 2)
191 if (ipvs->old_secure_tcp < 2)
194 if (ipvs->old_secure_tcp >= 2)
196 ipvs->sysctl_secure_tcp = 1;
200 if (ipvs->old_secure_tcp < 2)
204 ipvs->old_secure_tcp = ipvs->sysctl_secure_tcp;
206 ip_vs_protocol_timeout_change(ipvs,
207 ipvs->sysctl_secure_tcp > 1);
208 spin_unlock(&ipvs->securetcp_lock);
213 /* Handler for delayed work for expiring no
214 * destination connections
216 static void expire_nodest_conn_handler(struct work_struct *work)
218 struct netns_ipvs *ipvs;
220 ipvs = container_of(work, struct netns_ipvs,
221 expire_nodest_conn_work.work);
222 ip_vs_expire_nodest_conn_flush(ipvs);
226 * Timer for checking the defense
228 #define DEFENSE_TIMER_PERIOD 1*HZ
230 static void defense_work_handler(struct work_struct *work)
232 struct netns_ipvs *ipvs =
233 container_of(work, struct netns_ipvs, defense_work.work);
235 update_defense_level(ipvs);
236 if (atomic_read(&ipvs->dropentry))
237 ip_vs_random_dropentry(ipvs);
238 queue_delayed_work(system_long_wq, &ipvs->defense_work,
239 DEFENSE_TIMER_PERIOD);
243 static void est_reload_work_handler(struct work_struct *work)
245 struct netns_ipvs *ipvs =
246 container_of(work, struct netns_ipvs, est_reload_work.work);
247 int genid_done = atomic_read(&ipvs->est_genid_done);
248 unsigned long delay = HZ / 10; /* repeat startups after failure */
253 mutex_lock(&ipvs->est_mutex);
254 genid = atomic_read(&ipvs->est_genid);
255 for (id = 0; id < ipvs->est_kt_count; id++) {
256 struct ip_vs_est_kt_data *kd = ipvs->est_kt_arr[id];
258 /* netns clean up started, abort delayed work */
263 /* New config ? Stop kthread tasks */
264 if (genid != genid_done)
265 ip_vs_est_kthread_stop(kd);
266 if (!kd->task && !ip_vs_est_stopped(ipvs)) {
267 /* Do not start kthreads above 0 in calc phase */
268 if ((!id || !ipvs->est_calc_phase) &&
269 ip_vs_est_kthread_start(ipvs, kd) < 0)
274 atomic_set(&ipvs->est_genid_done, genid);
277 queue_delayed_work(system_long_wq, &ipvs->est_reload_work,
281 mutex_unlock(&ipvs->est_mutex);
285 ip_vs_use_count_inc(void)
287 return try_module_get(THIS_MODULE);
291 ip_vs_use_count_dec(void)
293 module_put(THIS_MODULE);
298 * Hash table: for virtual service lookups
300 #define IP_VS_SVC_TAB_BITS 8
301 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
302 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
304 /* the service table hashed by <protocol, addr, port> */
305 static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
306 /* the service table hashed by fwmark */
307 static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
311 * Returns hash value for virtual service
313 static inline unsigned int
314 ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
315 const union nf_inet_addr *addr, __be16 port)
317 unsigned int porth = ntohs(port);
318 __be32 addr_fold = addr->ip;
321 #ifdef CONFIG_IP_VS_IPV6
323 addr_fold = addr->ip6[0]^addr->ip6[1]^
324 addr->ip6[2]^addr->ip6[3];
326 ahash = ntohl(addr_fold);
327 ahash ^= ((size_t) ipvs >> 8);
329 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
334 * Returns hash value of fwmark for virtual service lookup
336 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
338 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
342 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
343 * or in the ip_vs_svc_fwm_table by fwmark.
344 * Should be called with locked tables.
346 static int ip_vs_svc_hash(struct ip_vs_service *svc)
350 if (svc->flags & IP_VS_SVC_F_HASHED) {
351 pr_err("%s(): request for already hashed, called from %pS\n",
352 __func__, __builtin_return_address(0));
356 if (svc->fwmark == 0) {
358 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
360 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
361 &svc->addr, svc->port);
362 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
365 * Hash it by fwmark in svc_fwm_table
367 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
368 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
371 svc->flags |= IP_VS_SVC_F_HASHED;
372 /* increase its refcnt because it is referenced by the svc table */
373 atomic_inc(&svc->refcnt);
379 * Unhashes a service from svc_table / svc_fwm_table.
380 * Should be called with locked tables.
382 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
384 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
385 pr_err("%s(): request for unhash flagged, called from %pS\n",
386 __func__, __builtin_return_address(0));
390 if (svc->fwmark == 0) {
391 /* Remove it from the svc_table table */
392 hlist_del_rcu(&svc->s_list);
394 /* Remove it from the svc_fwm_table table */
395 hlist_del_rcu(&svc->f_list);
398 svc->flags &= ~IP_VS_SVC_F_HASHED;
399 atomic_dec(&svc->refcnt);
405 * Get service by {netns, proto,addr,port} in the service table.
407 static inline struct ip_vs_service *
408 __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
409 const union nf_inet_addr *vaddr, __be16 vport)
412 struct ip_vs_service *svc;
414 /* Check for "full" addressed entries */
415 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
417 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
419 && ip_vs_addr_equal(af, &svc->addr, vaddr)
420 && (svc->port == vport)
421 && (svc->protocol == protocol)
422 && (svc->ipvs == ipvs)) {
433 * Get service by {fwmark} in the service table.
435 static inline struct ip_vs_service *
436 __ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
439 struct ip_vs_service *svc;
441 /* Check for fwmark addressed entries */
442 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
444 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
445 if (svc->fwmark == fwmark && svc->af == af
446 && (svc->ipvs == ipvs)) {
455 /* Find service, called under RCU lock */
456 struct ip_vs_service *
457 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
458 const union nf_inet_addr *vaddr, __be16 vport)
460 struct ip_vs_service *svc;
463 * Check the table hashed by fwmark first
466 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
472 * Check the table hashed by <protocol,addr,port>
473 * for "full" addressed entries
475 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
477 if (!svc && protocol == IPPROTO_TCP &&
478 atomic_read(&ipvs->ftpsvc_counter) &&
479 (vport == FTPDATA || !inet_port_requires_bind_service(ipvs->net, ntohs(vport)))) {
481 * Check if ftp service entry exists, the packet
482 * might belong to FTP data connections.
484 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
488 && atomic_read(&ipvs->nullsvc_counter)) {
490 * Check if the catch-all port (port zero) exists
492 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
496 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
497 fwmark, ip_vs_proto_name(protocol),
498 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
499 svc ? "hit" : "not hit");
506 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
508 atomic_inc(&svc->refcnt);
509 rcu_assign_pointer(dest->svc, svc);
512 static void ip_vs_service_free(struct ip_vs_service *svc)
514 ip_vs_stats_release(&svc->stats);
518 static void ip_vs_service_rcu_free(struct rcu_head *head)
520 struct ip_vs_service *svc;
522 svc = container_of(head, struct ip_vs_service, rcu_head);
523 ip_vs_service_free(svc);
526 static void __ip_vs_svc_put(struct ip_vs_service *svc)
528 if (atomic_dec_and_test(&svc->refcnt)) {
529 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
531 IP_VS_DBG_ADDR(svc->af, &svc->addr),
533 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
539 * Returns hash value for real service
541 static inline unsigned int ip_vs_rs_hashkey(int af,
542 const union nf_inet_addr *addr,
545 unsigned int porth = ntohs(port);
546 __be32 addr_fold = addr->ip;
548 #ifdef CONFIG_IP_VS_IPV6
550 addr_fold = addr->ip6[0]^addr->ip6[1]^
551 addr->ip6[2]^addr->ip6[3];
554 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
558 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
559 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
564 if (dest->in_rs_table)
567 switch (IP_VS_DFWD_METHOD(dest)) {
568 case IP_VS_CONN_F_MASQ:
571 case IP_VS_CONN_F_TUNNEL:
572 switch (dest->tun_type) {
573 case IP_VS_CONN_F_TUNNEL_TYPE_GUE:
574 port = dest->tun_port;
576 case IP_VS_CONN_F_TUNNEL_TYPE_IPIP:
577 case IP_VS_CONN_F_TUNNEL_TYPE_GRE:
589 * Hash by proto,addr,port,
590 * which are the parameters of the real service.
592 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, port);
594 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
595 dest->in_rs_table = 1;
598 /* Unhash ip_vs_dest from rs_table. */
599 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
602 * Remove it from the rs_table table.
604 if (dest->in_rs_table) {
605 hlist_del_rcu(&dest->d_list);
606 dest->in_rs_table = 0;
610 /* Check if real service by <proto,addr,port> is present */
611 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
612 const union nf_inet_addr *daddr, __be16 dport)
615 struct ip_vs_dest *dest;
617 /* Check for "full" addressed entries */
618 hash = ip_vs_rs_hashkey(af, daddr, dport);
620 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
621 if (dest->port == dport &&
623 ip_vs_addr_equal(af, &dest->addr, daddr) &&
624 (dest->protocol == protocol || dest->vfwmark) &&
625 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
634 /* Find real service record by <proto,addr,port>.
635 * In case of multiple records with the same <proto,addr,port>, only
636 * the first found record is returned.
638 * To be called under RCU lock.
640 struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
642 const union nf_inet_addr *daddr,
646 struct ip_vs_dest *dest;
648 /* Check for "full" addressed entries */
649 hash = ip_vs_rs_hashkey(af, daddr, dport);
651 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
652 if (dest->port == dport &&
654 ip_vs_addr_equal(af, &dest->addr, daddr) &&
655 (dest->protocol == protocol || dest->vfwmark) &&
656 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
665 /* Find real service record by <af,addr,tun_port>.
666 * In case of multiple records with the same <af,addr,tun_port>, only
667 * the first found record is returned.
669 * To be called under RCU lock.
671 struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
672 const union nf_inet_addr *daddr,
675 struct ip_vs_dest *dest;
678 /* Check for "full" addressed entries */
679 hash = ip_vs_rs_hashkey(af, daddr, tun_port);
681 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
682 if (dest->tun_port == tun_port &&
684 ip_vs_addr_equal(af, &dest->addr, daddr) &&
685 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_TUNNEL) {
694 /* Lookup destination by {addr,port} in the given service
695 * Called under RCU lock.
697 static struct ip_vs_dest *
698 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
699 const union nf_inet_addr *daddr, __be16 dport)
701 struct ip_vs_dest *dest;
704 * Find the destination for the given service
706 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
707 if ((dest->af == dest_af) &&
708 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
709 (dest->port == dport)) {
719 * Find destination by {daddr,dport,vaddr,protocol}
720 * Created to be used in ip_vs_process_message() in
721 * the backup synchronization daemon. It finds the
722 * destination to be bound to the received connection
724 * Called under RCU lock, no refcnt is returned.
726 struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
727 const union nf_inet_addr *daddr,
729 const union nf_inet_addr *vaddr,
730 __be16 vport, __u16 protocol, __u32 fwmark,
733 struct ip_vs_dest *dest;
734 struct ip_vs_service *svc;
737 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
740 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
742 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
744 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
748 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
750 struct ip_vs_dest_dst *dest_dst = container_of(head,
751 struct ip_vs_dest_dst,
754 dst_release(dest_dst->dst_cache);
758 /* Release dest_dst and dst_cache for dest in user context */
759 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
761 struct ip_vs_dest_dst *old;
763 old = rcu_dereference_protected(dest->dest_dst, 1);
765 RCU_INIT_POINTER(dest->dest_dst, NULL);
766 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
771 * Lookup dest by {svc,addr,port} in the destination trash.
772 * The destination trash is used to hold the destinations that are removed
773 * from the service table but are still referenced by some conn entries.
774 * The reason to add the destination trash is when the dest is temporary
775 * down (either by administrator or by monitor program), the dest can be
776 * picked back from the trash, the remaining connections to the dest can
777 * continue, and the counting information of the dest is also useful for
780 static struct ip_vs_dest *
781 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
782 const union nf_inet_addr *daddr, __be16 dport)
784 struct ip_vs_dest *dest;
785 struct netns_ipvs *ipvs = svc->ipvs;
788 * Find the destination in trash
790 spin_lock_bh(&ipvs->dest_trash_lock);
791 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
792 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
795 IP_VS_DBG_ADDR(dest->af, &dest->addr),
797 refcount_read(&dest->refcnt));
798 if (dest->af == dest_af &&
799 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
800 dest->port == dport &&
801 dest->vfwmark == svc->fwmark &&
802 dest->protocol == svc->protocol &&
804 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
805 dest->vport == svc->port))) {
807 list_del(&dest->t_list);
815 spin_unlock_bh(&ipvs->dest_trash_lock);
820 static void ip_vs_dest_rcu_free(struct rcu_head *head)
822 struct ip_vs_dest *dest;
824 dest = container_of(head, struct ip_vs_dest, rcu_head);
825 ip_vs_stats_release(&dest->stats);
826 ip_vs_dest_put_and_free(dest);
829 static void ip_vs_dest_free(struct ip_vs_dest *dest)
831 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
833 __ip_vs_dst_cache_reset(dest);
834 __ip_vs_svc_put(svc);
835 call_rcu(&dest->rcu_head, ip_vs_dest_rcu_free);
839 * Clean up all the destinations in the trash
840 * Called by the ip_vs_control_cleanup()
842 * When the ip_vs_control_clearup is activated by ipvs module exit,
843 * the service tables must have been flushed and all the connections
844 * are expired, and the refcnt of each destination in the trash must
845 * be 1, so we simply release them here.
847 static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
849 struct ip_vs_dest *dest, *nxt;
851 del_timer_sync(&ipvs->dest_trash_timer);
852 /* No need to use dest_trash_lock */
853 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
854 list_del(&dest->t_list);
855 ip_vs_dest_free(dest);
859 static void ip_vs_stats_rcu_free(struct rcu_head *head)
861 struct ip_vs_stats_rcu *rs = container_of(head,
862 struct ip_vs_stats_rcu,
865 ip_vs_stats_release(&rs->s);
870 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
872 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
874 spin_lock(&src->lock);
876 IP_VS_SHOW_STATS_COUNTER(conns);
877 IP_VS_SHOW_STATS_COUNTER(inpkts);
878 IP_VS_SHOW_STATS_COUNTER(outpkts);
879 IP_VS_SHOW_STATS_COUNTER(inbytes);
880 IP_VS_SHOW_STATS_COUNTER(outbytes);
882 ip_vs_read_estimator(dst, src);
884 spin_unlock(&src->lock);
888 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
890 dst->conns = (u32)src->conns;
891 dst->inpkts = (u32)src->inpkts;
892 dst->outpkts = (u32)src->outpkts;
893 dst->inbytes = src->inbytes;
894 dst->outbytes = src->outbytes;
895 dst->cps = (u32)src->cps;
896 dst->inpps = (u32)src->inpps;
897 dst->outpps = (u32)src->outpps;
898 dst->inbps = (u32)src->inbps;
899 dst->outbps = (u32)src->outbps;
903 ip_vs_zero_stats(struct ip_vs_stats *stats)
905 spin_lock(&stats->lock);
907 /* get current counters as zero point, rates are zeroed */
909 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
911 IP_VS_ZERO_STATS_COUNTER(conns);
912 IP_VS_ZERO_STATS_COUNTER(inpkts);
913 IP_VS_ZERO_STATS_COUNTER(outpkts);
914 IP_VS_ZERO_STATS_COUNTER(inbytes);
915 IP_VS_ZERO_STATS_COUNTER(outbytes);
917 ip_vs_zero_estimator(stats);
919 spin_unlock(&stats->lock);
922 /* Allocate fields after kzalloc */
923 int ip_vs_stats_init_alloc(struct ip_vs_stats *s)
927 spin_lock_init(&s->lock);
928 s->cpustats = alloc_percpu(struct ip_vs_cpu_stats);
932 for_each_possible_cpu(i) {
933 struct ip_vs_cpu_stats *cs = per_cpu_ptr(s->cpustats, i);
935 u64_stats_init(&cs->syncp);
940 struct ip_vs_stats *ip_vs_stats_alloc(void)
942 struct ip_vs_stats *s = kzalloc(sizeof(*s), GFP_KERNEL);
944 if (s && ip_vs_stats_init_alloc(s) >= 0)
950 void ip_vs_stats_release(struct ip_vs_stats *stats)
952 free_percpu(stats->cpustats);
955 void ip_vs_stats_free(struct ip_vs_stats *stats)
958 ip_vs_stats_release(stats);
964 * Update a destination in the given service
967 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
968 struct ip_vs_dest_user_kern *udest, int add)
970 struct netns_ipvs *ipvs = svc->ipvs;
971 struct ip_vs_service *old_svc;
972 struct ip_vs_scheduler *sched;
975 /* We cannot modify an address and change the address family */
976 BUG_ON(!add && udest->af != dest->af);
978 if (add && udest->af != svc->af)
979 ipvs->mixed_address_family_dests++;
981 /* keep the last_weight with latest non-0 weight */
982 if (add || udest->weight != 0)
983 atomic_set(&dest->last_weight, udest->weight);
985 /* set the weight and the flags */
986 atomic_set(&dest->weight, udest->weight);
987 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
988 conn_flags |= IP_VS_CONN_F_INACTIVE;
990 /* Need to rehash? */
991 if ((udest->conn_flags & IP_VS_CONN_F_FWD_MASK) !=
992 IP_VS_DFWD_METHOD(dest) ||
993 udest->tun_type != dest->tun_type ||
994 udest->tun_port != dest->tun_port)
995 ip_vs_rs_unhash(dest);
997 /* set the tunnel info */
998 dest->tun_type = udest->tun_type;
999 dest->tun_port = udest->tun_port;
1000 dest->tun_flags = udest->tun_flags;
1002 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
1003 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
1004 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
1006 /* FTP-NAT requires conntrack for mangling */
1007 if (svc->port == FTPPORT)
1008 ip_vs_register_conntrack(svc);
1010 atomic_set(&dest->conn_flags, conn_flags);
1011 /* Put the real service in rs_table if not present. */
1012 ip_vs_rs_hash(ipvs, dest);
1014 /* bind the service */
1015 old_svc = rcu_dereference_protected(dest->svc, 1);
1017 __ip_vs_bind_svc(dest, svc);
1019 if (old_svc != svc) {
1020 ip_vs_zero_stats(&dest->stats);
1021 __ip_vs_bind_svc(dest, svc);
1022 __ip_vs_svc_put(old_svc);
1026 /* set the dest status flags */
1027 dest->flags |= IP_VS_DEST_F_AVAILABLE;
1029 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
1030 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
1031 dest->u_threshold = udest->u_threshold;
1032 dest->l_threshold = udest->l_threshold;
1034 dest->af = udest->af;
1036 spin_lock_bh(&dest->dst_lock);
1037 __ip_vs_dst_cache_reset(dest);
1038 spin_unlock_bh(&dest->dst_lock);
1041 list_add_rcu(&dest->n_list, &svc->destinations);
1043 sched = rcu_dereference_protected(svc->scheduler, 1);
1044 if (sched && sched->add_dest)
1045 sched->add_dest(svc, dest);
1047 sched = rcu_dereference_protected(svc->scheduler, 1);
1048 if (sched && sched->upd_dest)
1049 sched->upd_dest(svc, dest);
1055 * Create a destination for the given service
1058 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1060 struct ip_vs_dest *dest;
1064 #ifdef CONFIG_IP_VS_IPV6
1065 if (udest->af == AF_INET6) {
1066 atype = ipv6_addr_type(&udest->addr.in6);
1067 if ((!(atype & IPV6_ADDR_UNICAST) ||
1068 atype & IPV6_ADDR_LINKLOCAL) &&
1069 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
1072 ret = nf_defrag_ipv6_enable(svc->ipvs->net);
1078 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
1079 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
1083 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
1087 ret = ip_vs_stats_init_alloc(&dest->stats);
1091 ret = ip_vs_start_estimator(svc->ipvs, &dest->stats);
1095 dest->af = udest->af;
1096 dest->protocol = svc->protocol;
1097 dest->vaddr = svc->addr;
1098 dest->vport = svc->port;
1099 dest->vfwmark = svc->fwmark;
1100 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
1101 dest->port = udest->port;
1103 atomic_set(&dest->activeconns, 0);
1104 atomic_set(&dest->inactconns, 0);
1105 atomic_set(&dest->persistconns, 0);
1106 refcount_set(&dest->refcnt, 1);
1108 INIT_HLIST_NODE(&dest->d_list);
1109 spin_lock_init(&dest->dst_lock);
1110 __ip_vs_update_dest(svc, dest, udest, 1);
1115 ip_vs_stats_release(&dest->stats);
1124 * Add a destination into an existing service
1127 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1129 struct ip_vs_dest *dest;
1130 union nf_inet_addr daddr;
1131 __be16 dport = udest->port;
1134 if (udest->weight < 0) {
1135 pr_err("%s(): server weight less than zero\n", __func__);
1139 if (udest->l_threshold > udest->u_threshold) {
1140 pr_err("%s(): lower threshold is higher than upper threshold\n",
1145 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1146 if (udest->tun_port == 0) {
1147 pr_err("%s(): tunnel port is zero\n", __func__);
1152 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1154 /* We use function that requires RCU lock */
1156 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1160 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
1165 * Check if the dest already exists in the trash and
1166 * is from the same service
1168 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
1171 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1172 "dest->refcnt=%d, service %u/%s:%u\n",
1173 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
1174 refcount_read(&dest->refcnt),
1176 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1177 ntohs(dest->vport));
1179 ret = ip_vs_start_estimator(svc->ipvs, &dest->stats);
1182 __ip_vs_update_dest(svc, dest, udest, 1);
1185 * Allocate and initialize the dest structure
1187 ret = ip_vs_new_dest(svc, udest);
1195 * Edit a destination in the given service
1198 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1200 struct ip_vs_dest *dest;
1201 union nf_inet_addr daddr;
1202 __be16 dport = udest->port;
1204 if (udest->weight < 0) {
1205 pr_err("%s(): server weight less than zero\n", __func__);
1209 if (udest->l_threshold > udest->u_threshold) {
1210 pr_err("%s(): lower threshold is higher than upper threshold\n",
1215 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1216 if (udest->tun_port == 0) {
1217 pr_err("%s(): tunnel port is zero\n", __func__);
1222 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1224 /* We use function that requires RCU lock */
1226 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1230 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1234 __ip_vs_update_dest(svc, dest, udest, 0);
1240 * Delete a destination (must be already unlinked from the service)
1242 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1245 ip_vs_stop_estimator(ipvs, &dest->stats);
1248 * Remove it from the d-linked list with the real services.
1250 ip_vs_rs_unhash(dest);
1252 spin_lock_bh(&ipvs->dest_trash_lock);
1253 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1254 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1255 refcount_read(&dest->refcnt));
1256 if (list_empty(&ipvs->dest_trash) && !cleanup)
1257 mod_timer(&ipvs->dest_trash_timer,
1258 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1259 /* dest lives in trash with reference */
1260 list_add(&dest->t_list, &ipvs->dest_trash);
1261 dest->idle_start = 0;
1262 spin_unlock_bh(&ipvs->dest_trash_lock);
1264 /* Queue up delayed work to expire all no destination connections.
1265 * No-op when CONFIG_SYSCTL is disabled.
1268 ip_vs_enqueue_expire_nodest_conns(ipvs);
1273 * Unlink a destination from the given service
1275 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1276 struct ip_vs_dest *dest,
1279 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1282 * Remove it from the d-linked destination list.
1284 list_del_rcu(&dest->n_list);
1287 if (dest->af != svc->af)
1288 svc->ipvs->mixed_address_family_dests--;
1291 struct ip_vs_scheduler *sched;
1293 sched = rcu_dereference_protected(svc->scheduler, 1);
1294 if (sched && sched->del_dest)
1295 sched->del_dest(svc, dest);
1301 * Delete a destination server in the given service
1304 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1306 struct ip_vs_dest *dest;
1307 __be16 dport = udest->port;
1309 /* We use function that requires RCU lock */
1311 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1315 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1320 * Unlink dest from the service
1322 __ip_vs_unlink_dest(svc, dest, 1);
1325 * Delete the destination
1327 __ip_vs_del_dest(svc->ipvs, dest, false);
1332 static void ip_vs_dest_trash_expire(struct timer_list *t)
1334 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1335 struct ip_vs_dest *dest, *next;
1336 unsigned long now = jiffies;
1338 spin_lock(&ipvs->dest_trash_lock);
1339 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1340 if (refcount_read(&dest->refcnt) > 1)
1342 if (dest->idle_start) {
1343 if (time_before(now, dest->idle_start +
1344 IP_VS_DEST_TRASH_PERIOD))
1347 dest->idle_start = max(1UL, now);
1350 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1352 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1354 list_del(&dest->t_list);
1355 ip_vs_dest_free(dest);
1357 if (!list_empty(&ipvs->dest_trash))
1358 mod_timer(&ipvs->dest_trash_timer,
1359 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1360 spin_unlock(&ipvs->dest_trash_lock);
1364 * Add a service into the service hash table
1367 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1368 struct ip_vs_service **svc_p)
1371 struct ip_vs_scheduler *sched = NULL;
1372 struct ip_vs_pe *pe = NULL;
1373 struct ip_vs_service *svc = NULL;
1376 /* increase the module use count */
1377 if (!ip_vs_use_count_inc())
1378 return -ENOPROTOOPT;
1380 /* Lookup the scheduler by 'u->sched_name' */
1381 if (strcmp(u->sched_name, "none")) {
1382 sched = ip_vs_scheduler_get(u->sched_name);
1384 pr_info("Scheduler module ip_vs_%s not found\n",
1391 if (u->pe_name && *u->pe_name) {
1392 pe = ip_vs_pe_getbyname(u->pe_name);
1394 pr_info("persistence engine module ip_vs_pe_%s "
1395 "not found\n", u->pe_name);
1401 #ifdef CONFIG_IP_VS_IPV6
1402 if (u->af == AF_INET6) {
1403 __u32 plen = (__force __u32) u->netmask;
1405 if (plen < 1 || plen > 128) {
1410 ret = nf_defrag_ipv6_enable(ipvs->net);
1416 if ((u->af == AF_INET && !ipvs->num_services) ||
1417 (u->af == AF_INET6 && !ipvs->num_services6)) {
1418 ret = ip_vs_register_hooks(ipvs, u->af);
1424 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1426 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1430 ret = ip_vs_stats_init_alloc(&svc->stats);
1434 /* I'm the first user of the service */
1435 atomic_set(&svc->refcnt, 0);
1438 svc->protocol = u->protocol;
1439 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1440 svc->port = u->port;
1441 svc->fwmark = u->fwmark;
1442 svc->flags = u->flags & ~IP_VS_SVC_F_HASHED;
1443 svc->timeout = u->timeout * HZ;
1444 svc->netmask = u->netmask;
1447 INIT_LIST_HEAD(&svc->destinations);
1448 spin_lock_init(&svc->sched_lock);
1450 /* Bind the scheduler */
1452 ret = ip_vs_bind_scheduler(svc, sched);
1458 ret = ip_vs_start_estimator(ipvs, &svc->stats);
1462 /* Bind the ct retriever */
1463 RCU_INIT_POINTER(svc->pe, pe);
1466 /* Update the virtual service counters */
1467 if (svc->port == FTPPORT)
1468 atomic_inc(&ipvs->ftpsvc_counter);
1469 else if (svc->port == 0)
1470 atomic_inc(&ipvs->nullsvc_counter);
1471 if (svc->pe && svc->pe->conn_out)
1472 atomic_inc(&ipvs->conn_out_counter);
1474 /* Count only IPv4 services for old get/setsockopt interface */
1475 if (svc->af == AF_INET)
1476 ipvs->num_services++;
1477 else if (svc->af == AF_INET6)
1478 ipvs->num_services6++;
1480 /* Hash the service into the service table */
1481 ip_vs_svc_hash(svc);
1485 if (!ipvs->enable) {
1486 /* Now there is a service - full throttle */
1489 /* Start estimation for first time */
1490 ip_vs_est_reload_start(ipvs);
1498 ip_vs_unregister_hooks(ipvs, u->af);
1500 ip_vs_unbind_scheduler(svc, sched);
1501 ip_vs_service_free(svc);
1503 ip_vs_scheduler_put(sched);
1506 /* decrease the module use count */
1507 ip_vs_use_count_dec();
1514 * Edit a service and bind it with a new scheduler
1517 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1519 struct ip_vs_scheduler *sched = NULL, *old_sched;
1520 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1522 bool new_pe_conn_out, old_pe_conn_out;
1525 * Lookup the scheduler, by 'u->sched_name'
1527 if (strcmp(u->sched_name, "none")) {
1528 sched = ip_vs_scheduler_get(u->sched_name);
1530 pr_info("Scheduler module ip_vs_%s not found\n",
1537 if (u->pe_name && *u->pe_name) {
1538 pe = ip_vs_pe_getbyname(u->pe_name);
1540 pr_info("persistence engine module ip_vs_pe_%s "
1541 "not found\n", u->pe_name);
1548 #ifdef CONFIG_IP_VS_IPV6
1549 if (u->af == AF_INET6) {
1550 __u32 plen = (__force __u32) u->netmask;
1552 if (plen < 1 || plen > 128) {
1559 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1560 if (sched != old_sched) {
1562 ip_vs_unbind_scheduler(svc, old_sched);
1563 RCU_INIT_POINTER(svc->scheduler, NULL);
1564 /* Wait all svc->sched_data users */
1567 /* Bind the new scheduler */
1569 ret = ip_vs_bind_scheduler(svc, sched);
1571 ip_vs_scheduler_put(sched);
1578 * Set the flags and timeout value
1580 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1581 svc->timeout = u->timeout * HZ;
1582 svc->netmask = u->netmask;
1584 old_pe = rcu_dereference_protected(svc->pe, 1);
1586 rcu_assign_pointer(svc->pe, pe);
1587 /* check for optional methods in new pe */
1588 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1589 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1590 if (new_pe_conn_out && !old_pe_conn_out)
1591 atomic_inc(&svc->ipvs->conn_out_counter);
1592 if (old_pe_conn_out && !new_pe_conn_out)
1593 atomic_dec(&svc->ipvs->conn_out_counter);
1597 ip_vs_scheduler_put(old_sched);
1598 ip_vs_pe_put(old_pe);
1603 * Delete a service from the service list
1604 * - The service must be unlinked, unlocked and not referenced!
1605 * - We are called under _bh lock
1607 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1609 struct ip_vs_dest *dest, *nxt;
1610 struct ip_vs_scheduler *old_sched;
1611 struct ip_vs_pe *old_pe;
1612 struct netns_ipvs *ipvs = svc->ipvs;
1614 if (svc->af == AF_INET) {
1615 ipvs->num_services--;
1616 if (!ipvs->num_services)
1617 ip_vs_unregister_hooks(ipvs, svc->af);
1618 } else if (svc->af == AF_INET6) {
1619 ipvs->num_services6--;
1620 if (!ipvs->num_services6)
1621 ip_vs_unregister_hooks(ipvs, svc->af);
1624 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1626 /* Unbind scheduler */
1627 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1628 ip_vs_unbind_scheduler(svc, old_sched);
1629 ip_vs_scheduler_put(old_sched);
1631 /* Unbind persistence engine, keep svc->pe */
1632 old_pe = rcu_dereference_protected(svc->pe, 1);
1633 if (old_pe && old_pe->conn_out)
1634 atomic_dec(&ipvs->conn_out_counter);
1635 ip_vs_pe_put(old_pe);
1638 * Unlink the whole destination list
1640 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1641 __ip_vs_unlink_dest(svc, dest, 0);
1642 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1646 * Update the virtual service counters
1648 if (svc->port == FTPPORT)
1649 atomic_dec(&ipvs->ftpsvc_counter);
1650 else if (svc->port == 0)
1651 atomic_dec(&ipvs->nullsvc_counter);
1654 * Free the service if nobody refers to it
1656 __ip_vs_svc_put(svc);
1658 /* decrease the module use count */
1659 ip_vs_use_count_dec();
1663 * Unlink a service from list and try to delete it if its refcnt reached 0
1665 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1667 ip_vs_unregister_conntrack(svc);
1668 /* Hold svc to avoid double release from dest_trash */
1669 atomic_inc(&svc->refcnt);
1671 * Unhash it from the service table
1673 ip_vs_svc_unhash(svc);
1675 __ip_vs_del_service(svc, cleanup);
1679 * Delete a service from the service list
1681 static int ip_vs_del_service(struct ip_vs_service *svc)
1685 ip_vs_unlink_service(svc, false);
1692 * Flush all the virtual services
1694 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1697 struct ip_vs_service *svc;
1698 struct hlist_node *n;
1701 * Flush the service table hashed by <netns,protocol,addr,port>
1703 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1704 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1706 if (svc->ipvs == ipvs)
1707 ip_vs_unlink_service(svc, cleanup);
1712 * Flush the service table hashed by fwmark
1714 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1715 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1717 if (svc->ipvs == ipvs)
1718 ip_vs_unlink_service(svc, cleanup);
1726 * Delete service by {netns} in the service table.
1727 * Called by __ip_vs_batch_cleanup()
1729 void ip_vs_service_nets_cleanup(struct list_head *net_list)
1731 struct netns_ipvs *ipvs;
1734 /* Check for "full" addressed entries */
1735 mutex_lock(&__ip_vs_mutex);
1736 list_for_each_entry(net, net_list, exit_list) {
1737 ipvs = net_ipvs(net);
1738 ip_vs_flush(ipvs, true);
1740 mutex_unlock(&__ip_vs_mutex);
1743 /* Put all references for device (dst_cache) */
1745 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1747 struct ip_vs_dest_dst *dest_dst;
1749 spin_lock_bh(&dest->dst_lock);
1750 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1751 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1752 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1754 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1756 refcount_read(&dest->refcnt));
1757 __ip_vs_dst_cache_reset(dest);
1759 spin_unlock_bh(&dest->dst_lock);
1762 /* Netdev event receiver
1763 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1765 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1768 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1769 struct net *net = dev_net(dev);
1770 struct netns_ipvs *ipvs = net_ipvs(net);
1771 struct ip_vs_service *svc;
1772 struct ip_vs_dest *dest;
1775 if (event != NETDEV_DOWN || !ipvs)
1777 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1778 mutex_lock(&__ip_vs_mutex);
1779 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1780 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1781 if (svc->ipvs == ipvs) {
1782 list_for_each_entry(dest, &svc->destinations,
1784 ip_vs_forget_dev(dest, dev);
1789 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1790 if (svc->ipvs == ipvs) {
1791 list_for_each_entry(dest, &svc->destinations,
1793 ip_vs_forget_dev(dest, dev);
1800 spin_lock_bh(&ipvs->dest_trash_lock);
1801 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1802 ip_vs_forget_dev(dest, dev);
1804 spin_unlock_bh(&ipvs->dest_trash_lock);
1805 mutex_unlock(&__ip_vs_mutex);
1810 * Zero counters in a service or all services
1812 static int ip_vs_zero_service(struct ip_vs_service *svc)
1814 struct ip_vs_dest *dest;
1816 list_for_each_entry(dest, &svc->destinations, n_list) {
1817 ip_vs_zero_stats(&dest->stats);
1819 ip_vs_zero_stats(&svc->stats);
1823 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1826 struct ip_vs_service *svc;
1828 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1829 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1830 if (svc->ipvs == ipvs)
1831 ip_vs_zero_service(svc);
1835 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1836 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1837 if (svc->ipvs == ipvs)
1838 ip_vs_zero_service(svc);
1842 ip_vs_zero_stats(&ipvs->tot_stats->s);
1846 #ifdef CONFIG_SYSCTL
1849 proc_do_defense_mode(struct ctl_table *table, int write,
1850 void *buffer, size_t *lenp, loff_t *ppos)
1852 struct netns_ipvs *ipvs = table->extra2;
1853 int *valp = table->data;
1857 struct ctl_table tmp = {
1859 .maxlen = sizeof(int),
1860 .mode = table->mode,
1863 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1864 if (write && (*valp != val)) {
1865 if (val < 0 || val > 3) {
1869 update_defense_level(ipvs);
1876 proc_do_sync_threshold(struct ctl_table *table, int write,
1877 void *buffer, size_t *lenp, loff_t *ppos)
1879 struct netns_ipvs *ipvs = table->extra2;
1880 int *valp = table->data;
1883 struct ctl_table tmp = {
1885 .maxlen = table->maxlen,
1886 .mode = table->mode,
1889 mutex_lock(&ipvs->sync_mutex);
1890 memcpy(val, valp, sizeof(val));
1891 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1893 if (val[0] < 0 || val[1] < 0 ||
1894 (val[0] >= val[1] && val[1]))
1897 memcpy(valp, val, sizeof(val));
1899 mutex_unlock(&ipvs->sync_mutex);
1904 proc_do_sync_ports(struct ctl_table *table, int write,
1905 void *buffer, size_t *lenp, loff_t *ppos)
1907 int *valp = table->data;
1911 struct ctl_table tmp = {
1913 .maxlen = sizeof(int),
1914 .mode = table->mode,
1917 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1918 if (write && (*valp != val)) {
1919 if (val < 1 || !is_power_of_2(val))
1927 static int ipvs_proc_est_cpumask_set(struct ctl_table *table, void *buffer)
1929 struct netns_ipvs *ipvs = table->extra2;
1930 cpumask_var_t *valp = table->data;
1931 cpumask_var_t newmask;
1934 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL))
1937 ret = cpulist_parse(buffer, newmask);
1941 mutex_lock(&ipvs->est_mutex);
1943 if (!ipvs->est_cpulist_valid) {
1944 if (!zalloc_cpumask_var(valp, GFP_KERNEL)) {
1948 ipvs->est_cpulist_valid = 1;
1950 cpumask_and(newmask, newmask, ¤t->cpus_mask);
1951 cpumask_copy(*valp, newmask);
1952 /* est_max_threads may depend on cpulist size */
1953 ipvs->est_max_threads = ip_vs_est_max_threads(ipvs);
1954 ipvs->est_calc_phase = 1;
1955 ip_vs_est_reload_start(ipvs);
1958 mutex_unlock(&ipvs->est_mutex);
1961 free_cpumask_var(newmask);
1965 static int ipvs_proc_est_cpumask_get(struct ctl_table *table, void *buffer,
1968 struct netns_ipvs *ipvs = table->extra2;
1969 cpumask_var_t *valp = table->data;
1970 struct cpumask *mask;
1973 mutex_lock(&ipvs->est_mutex);
1975 if (ipvs->est_cpulist_valid)
1978 mask = (struct cpumask *)housekeeping_cpumask(HK_TYPE_KTHREAD);
1979 ret = scnprintf(buffer, size, "%*pbl\n", cpumask_pr_args(mask));
1981 mutex_unlock(&ipvs->est_mutex);
1986 static int ipvs_proc_est_cpulist(struct ctl_table *table, int write,
1987 void *buffer, size_t *lenp, loff_t *ppos)
1991 /* Ignore both read and write(append) if *ppos not 0 */
1992 if (*ppos || !*lenp) {
1997 /* proc_sys_call_handler() appends terminator */
1998 ret = ipvs_proc_est_cpumask_set(table, buffer);
2002 /* proc_sys_call_handler() allocates 1 byte for terminator */
2003 ret = ipvs_proc_est_cpumask_get(table, buffer, *lenp + 1);
2013 static int ipvs_proc_est_nice(struct ctl_table *table, int write,
2014 void *buffer, size_t *lenp, loff_t *ppos)
2016 struct netns_ipvs *ipvs = table->extra2;
2017 int *valp = table->data;
2021 struct ctl_table tmp_table = {
2023 .maxlen = sizeof(int),
2024 .mode = table->mode,
2027 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
2028 if (write && ret >= 0) {
2029 if (val < MIN_NICE || val > MAX_NICE) {
2032 mutex_lock(&ipvs->est_mutex);
2035 ip_vs_est_reload_start(ipvs);
2037 mutex_unlock(&ipvs->est_mutex);
2043 static int ipvs_proc_run_estimation(struct ctl_table *table, int write,
2044 void *buffer, size_t *lenp, loff_t *ppos)
2046 struct netns_ipvs *ipvs = table->extra2;
2047 int *valp = table->data;
2051 struct ctl_table tmp_table = {
2053 .maxlen = sizeof(int),
2054 .mode = table->mode,
2057 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
2058 if (write && ret >= 0) {
2059 mutex_lock(&ipvs->est_mutex);
2062 ip_vs_est_reload_start(ipvs);
2064 mutex_unlock(&ipvs->est_mutex);
2070 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
2071 * Do not change order or insert new entries without
2072 * align with netns init in ip_vs_control_net_init()
2075 static struct ctl_table vs_vars[] = {
2077 .procname = "amemthresh",
2078 .maxlen = sizeof(int),
2080 .proc_handler = proc_dointvec,
2083 .procname = "am_droprate",
2084 .maxlen = sizeof(int),
2086 .proc_handler = proc_dointvec,
2089 .procname = "drop_entry",
2090 .maxlen = sizeof(int),
2092 .proc_handler = proc_do_defense_mode,
2095 .procname = "drop_packet",
2096 .maxlen = sizeof(int),
2098 .proc_handler = proc_do_defense_mode,
2100 #ifdef CONFIG_IP_VS_NFCT
2102 .procname = "conntrack",
2103 .maxlen = sizeof(int),
2105 .proc_handler = &proc_dointvec,
2109 .procname = "secure_tcp",
2110 .maxlen = sizeof(int),
2112 .proc_handler = proc_do_defense_mode,
2115 .procname = "snat_reroute",
2116 .maxlen = sizeof(int),
2118 .proc_handler = &proc_dointvec,
2121 .procname = "sync_version",
2122 .maxlen = sizeof(int),
2124 .proc_handler = proc_dointvec_minmax,
2125 .extra1 = SYSCTL_ZERO,
2126 .extra2 = SYSCTL_ONE,
2129 .procname = "sync_ports",
2130 .maxlen = sizeof(int),
2132 .proc_handler = proc_do_sync_ports,
2135 .procname = "sync_persist_mode",
2136 .maxlen = sizeof(int),
2138 .proc_handler = proc_dointvec,
2141 .procname = "sync_qlen_max",
2142 .maxlen = sizeof(unsigned long),
2144 .proc_handler = proc_doulongvec_minmax,
2147 .procname = "sync_sock_size",
2148 .maxlen = sizeof(int),
2150 .proc_handler = proc_dointvec,
2153 .procname = "cache_bypass",
2154 .maxlen = sizeof(int),
2156 .proc_handler = proc_dointvec,
2159 .procname = "expire_nodest_conn",
2160 .maxlen = sizeof(int),
2162 .proc_handler = proc_dointvec,
2165 .procname = "sloppy_tcp",
2166 .maxlen = sizeof(int),
2168 .proc_handler = proc_dointvec,
2171 .procname = "sloppy_sctp",
2172 .maxlen = sizeof(int),
2174 .proc_handler = proc_dointvec,
2177 .procname = "expire_quiescent_template",
2178 .maxlen = sizeof(int),
2180 .proc_handler = proc_dointvec,
2183 .procname = "sync_threshold",
2185 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
2187 .proc_handler = proc_do_sync_threshold,
2190 .procname = "sync_refresh_period",
2191 .maxlen = sizeof(int),
2193 .proc_handler = proc_dointvec_jiffies,
2196 .procname = "sync_retries",
2197 .maxlen = sizeof(int),
2199 .proc_handler = proc_dointvec_minmax,
2200 .extra1 = SYSCTL_ZERO,
2201 .extra2 = SYSCTL_THREE,
2204 .procname = "nat_icmp_send",
2205 .maxlen = sizeof(int),
2207 .proc_handler = proc_dointvec,
2210 .procname = "pmtu_disc",
2211 .maxlen = sizeof(int),
2213 .proc_handler = proc_dointvec,
2216 .procname = "backup_only",
2217 .maxlen = sizeof(int),
2219 .proc_handler = proc_dointvec,
2222 .procname = "conn_reuse_mode",
2223 .maxlen = sizeof(int),
2225 .proc_handler = proc_dointvec,
2228 .procname = "schedule_icmp",
2229 .maxlen = sizeof(int),
2231 .proc_handler = proc_dointvec,
2234 .procname = "ignore_tunneled",
2235 .maxlen = sizeof(int),
2237 .proc_handler = proc_dointvec,
2240 .procname = "run_estimation",
2241 .maxlen = sizeof(int),
2243 .proc_handler = ipvs_proc_run_estimation,
2246 .procname = "est_cpulist",
2247 .maxlen = NR_CPUS, /* unused */
2249 .proc_handler = ipvs_proc_est_cpulist,
2252 .procname = "est_nice",
2253 .maxlen = sizeof(int),
2255 .proc_handler = ipvs_proc_est_nice,
2257 #ifdef CONFIG_IP_VS_DEBUG
2259 .procname = "debug_level",
2260 .data = &sysctl_ip_vs_debug_level,
2261 .maxlen = sizeof(int),
2263 .proc_handler = proc_dointvec,
2271 #ifdef CONFIG_PROC_FS
2274 struct seq_net_private p; /* Do not move this, netns depends upon it*/
2275 struct hlist_head *table;
2280 * Write the contents of the VS rule table to a PROCfs file.
2281 * (It is kept just for backward compatibility)
2283 static inline const char *ip_vs_fwd_name(unsigned int flags)
2285 switch (flags & IP_VS_CONN_F_FWD_MASK) {
2286 case IP_VS_CONN_F_LOCALNODE:
2288 case IP_VS_CONN_F_TUNNEL:
2290 case IP_VS_CONN_F_DROUTE:
2298 /* Get the Nth entry in the two lists */
2299 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
2301 struct net *net = seq_file_net(seq);
2302 struct netns_ipvs *ipvs = net_ipvs(net);
2303 struct ip_vs_iter *iter = seq->private;
2305 struct ip_vs_service *svc;
2307 /* look in hash by protocol */
2308 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2309 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
2310 if ((svc->ipvs == ipvs) && pos-- == 0) {
2311 iter->table = ip_vs_svc_table;
2318 /* keep looking in fwmark */
2319 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2320 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
2322 if ((svc->ipvs == ipvs) && pos-- == 0) {
2323 iter->table = ip_vs_svc_fwm_table;
2333 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
2337 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
2341 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2343 struct hlist_node *e;
2344 struct ip_vs_iter *iter;
2345 struct ip_vs_service *svc;
2348 if (v == SEQ_START_TOKEN)
2349 return ip_vs_info_array(seq,0);
2352 iter = seq->private;
2354 if (iter->table == ip_vs_svc_table) {
2355 /* next service in table hashed by protocol */
2356 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
2358 return hlist_entry(e, struct ip_vs_service, s_list);
2360 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2361 hlist_for_each_entry_rcu(svc,
2362 &ip_vs_svc_table[iter->bucket],
2368 iter->table = ip_vs_svc_fwm_table;
2373 /* next service in hashed by fwmark */
2374 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2376 return hlist_entry(e, struct ip_vs_service, f_list);
2379 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2380 hlist_for_each_entry_rcu(svc,
2381 &ip_vs_svc_fwm_table[iter->bucket],
2389 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2396 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2398 if (v == SEQ_START_TOKEN) {
2400 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2401 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2403 "Prot LocalAddress:Port Scheduler Flags\n");
2405 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2407 struct net *net = seq_file_net(seq);
2408 struct netns_ipvs *ipvs = net_ipvs(net);
2409 const struct ip_vs_service *svc = v;
2410 const struct ip_vs_iter *iter = seq->private;
2411 const struct ip_vs_dest *dest;
2412 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2413 char *sched_name = sched ? sched->name : "none";
2415 if (svc->ipvs != ipvs)
2417 if (iter->table == ip_vs_svc_table) {
2418 #ifdef CONFIG_IP_VS_IPV6
2419 if (svc->af == AF_INET6)
2420 seq_printf(seq, "%s [%pI6]:%04X %s ",
2421 ip_vs_proto_name(svc->protocol),
2427 seq_printf(seq, "%s %08X:%04X %s %s ",
2428 ip_vs_proto_name(svc->protocol),
2429 ntohl(svc->addr.ip),
2432 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2434 seq_printf(seq, "FWM %08X %s %s",
2435 svc->fwmark, sched_name,
2436 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2439 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2440 seq_printf(seq, "persistent %d %08X\n",
2442 ntohl(svc->netmask));
2444 seq_putc(seq, '\n');
2446 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2447 #ifdef CONFIG_IP_VS_IPV6
2448 if (dest->af == AF_INET6)
2451 " %-7s %-6d %-10d %-10d\n",
2454 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2455 atomic_read(&dest->weight),
2456 atomic_read(&dest->activeconns),
2457 atomic_read(&dest->inactconns));
2462 "%-7s %-6d %-10d %-10d\n",
2463 ntohl(dest->addr.ip),
2465 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2466 atomic_read(&dest->weight),
2467 atomic_read(&dest->activeconns),
2468 atomic_read(&dest->inactconns));
2475 static const struct seq_operations ip_vs_info_seq_ops = {
2476 .start = ip_vs_info_seq_start,
2477 .next = ip_vs_info_seq_next,
2478 .stop = ip_vs_info_seq_stop,
2479 .show = ip_vs_info_seq_show,
2482 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2484 struct net *net = seq_file_single_net(seq);
2485 struct ip_vs_kstats show;
2487 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2489 " Total Incoming Outgoing Incoming Outgoing\n");
2491 " Conns Packets Packets Bytes Bytes\n");
2493 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats->s);
2494 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2495 (unsigned long long)show.conns,
2496 (unsigned long long)show.inpkts,
2497 (unsigned long long)show.outpkts,
2498 (unsigned long long)show.inbytes,
2499 (unsigned long long)show.outbytes);
2501 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2503 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2504 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2505 (unsigned long long)show.cps,
2506 (unsigned long long)show.inpps,
2507 (unsigned long long)show.outpps,
2508 (unsigned long long)show.inbps,
2509 (unsigned long long)show.outbps);
2514 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2516 struct net *net = seq_file_single_net(seq);
2517 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats->s;
2518 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2519 struct ip_vs_kstats kstats;
2522 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2524 " Total Incoming Outgoing Incoming Outgoing\n");
2526 "CPU Conns Packets Packets Bytes Bytes\n");
2528 for_each_possible_cpu(i) {
2529 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2531 u64 conns, inpkts, outpkts, inbytes, outbytes;
2534 start = u64_stats_fetch_begin(&u->syncp);
2535 conns = u64_stats_read(&u->cnt.conns);
2536 inpkts = u64_stats_read(&u->cnt.inpkts);
2537 outpkts = u64_stats_read(&u->cnt.outpkts);
2538 inbytes = u64_stats_read(&u->cnt.inbytes);
2539 outbytes = u64_stats_read(&u->cnt.outbytes);
2540 } while (u64_stats_fetch_retry(&u->syncp, start));
2542 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2543 i, (u64)conns, (u64)inpkts,
2544 (u64)outpkts, (u64)inbytes,
2548 ip_vs_copy_stats(&kstats, tot_stats);
2550 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2551 (unsigned long long)kstats.conns,
2552 (unsigned long long)kstats.inpkts,
2553 (unsigned long long)kstats.outpkts,
2554 (unsigned long long)kstats.inbytes,
2555 (unsigned long long)kstats.outbytes);
2557 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2559 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2560 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2572 * Set timeout values for tcp tcpfin udp in the timeout_table.
2574 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2576 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2577 struct ip_vs_proto_data *pd;
2580 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2585 #ifdef CONFIG_IP_VS_PROTO_TCP
2586 if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2587 u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2592 #ifdef CONFIG_IP_VS_PROTO_UDP
2593 if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2597 #ifdef CONFIG_IP_VS_PROTO_TCP
2598 if (u->tcp_timeout) {
2599 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2600 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2601 = u->tcp_timeout * HZ;
2604 if (u->tcp_fin_timeout) {
2605 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2606 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2607 = u->tcp_fin_timeout * HZ;
2611 #ifdef CONFIG_IP_VS_PROTO_UDP
2612 if (u->udp_timeout) {
2613 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2614 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2615 = u->udp_timeout * HZ;
2621 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2623 struct ip_vs_svcdest_user {
2624 struct ip_vs_service_user s;
2625 struct ip_vs_dest_user d;
2628 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2629 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2630 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2631 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2632 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2633 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2634 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2635 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2636 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2637 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2638 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2641 union ip_vs_set_arglen {
2642 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2643 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2644 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2645 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2646 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2647 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2648 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2649 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2650 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2651 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2654 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2656 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2657 struct ip_vs_service_user *usvc_compat)
2659 memset(usvc, 0, sizeof(*usvc));
2662 usvc->protocol = usvc_compat->protocol;
2663 usvc->addr.ip = usvc_compat->addr;
2664 usvc->port = usvc_compat->port;
2665 usvc->fwmark = usvc_compat->fwmark;
2667 /* Deep copy of sched_name is not needed here */
2668 usvc->sched_name = usvc_compat->sched_name;
2670 usvc->flags = usvc_compat->flags;
2671 usvc->timeout = usvc_compat->timeout;
2672 usvc->netmask = usvc_compat->netmask;
2675 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2676 struct ip_vs_dest_user *udest_compat)
2678 memset(udest, 0, sizeof(*udest));
2680 udest->addr.ip = udest_compat->addr;
2681 udest->port = udest_compat->port;
2682 udest->conn_flags = udest_compat->conn_flags;
2683 udest->weight = udest_compat->weight;
2684 udest->u_threshold = udest_compat->u_threshold;
2685 udest->l_threshold = udest_compat->l_threshold;
2686 udest->af = AF_INET;
2687 udest->tun_type = IP_VS_CONN_F_TUNNEL_TYPE_IPIP;
2691 do_ip_vs_set_ctl(struct sock *sk, int cmd, sockptr_t ptr, unsigned int len)
2693 struct net *net = sock_net(sk);
2695 unsigned char arg[MAX_SET_ARGLEN];
2696 struct ip_vs_service_user *usvc_compat;
2697 struct ip_vs_service_user_kern usvc;
2698 struct ip_vs_service *svc;
2699 struct ip_vs_dest_user *udest_compat;
2700 struct ip_vs_dest_user_kern udest;
2701 struct netns_ipvs *ipvs = net_ipvs(net);
2703 BUILD_BUG_ON(sizeof(arg) > 255);
2704 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2707 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2709 if (len != set_arglen[CMDID(cmd)]) {
2710 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2711 len, set_arglen[CMDID(cmd)]);
2715 if (copy_from_sockptr(arg, ptr, len) != 0)
2718 /* Handle daemons since they have another lock */
2719 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2720 cmd == IP_VS_SO_SET_STOPDAEMON) {
2721 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2723 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2724 struct ipvs_sync_daemon_cfg cfg;
2726 memset(&cfg, 0, sizeof(cfg));
2728 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2729 sizeof(cfg.mcast_ifn)) <= 0)
2731 cfg.syncid = dm->syncid;
2732 ret = start_sync_thread(ipvs, &cfg, dm->state);
2734 ret = stop_sync_thread(ipvs, dm->state);
2739 mutex_lock(&__ip_vs_mutex);
2740 if (cmd == IP_VS_SO_SET_FLUSH) {
2741 /* Flush the virtual service */
2742 ret = ip_vs_flush(ipvs, false);
2744 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2745 /* Set timeout values for (tcp tcpfin udp) */
2746 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2749 /* No more commands with len == 0 below */
2754 usvc_compat = (struct ip_vs_service_user *)arg;
2755 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2757 /* We only use the new structs internally, so copy userspace compat
2758 * structs to extended internal versions */
2759 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2760 ip_vs_copy_udest_compat(&udest, udest_compat);
2762 if (cmd == IP_VS_SO_SET_ZERO) {
2763 /* if no service address is set, zero counters in all */
2764 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2765 ret = ip_vs_zero_all(ipvs);
2770 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2771 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2772 IP_VS_SCHEDNAME_MAXLEN) {
2777 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2778 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2779 usvc.protocol != IPPROTO_SCTP) {
2780 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2781 usvc.protocol, &usvc.addr.ip,
2787 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2789 if (usvc.fwmark == 0)
2790 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2791 &usvc.addr, usvc.port);
2793 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2796 if (cmd != IP_VS_SO_SET_ADD
2797 && (svc == NULL || svc->protocol != usvc.protocol)) {
2803 case IP_VS_SO_SET_ADD:
2807 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2809 case IP_VS_SO_SET_EDIT:
2810 ret = ip_vs_edit_service(svc, &usvc);
2812 case IP_VS_SO_SET_DEL:
2813 ret = ip_vs_del_service(svc);
2817 case IP_VS_SO_SET_ZERO:
2818 ret = ip_vs_zero_service(svc);
2820 case IP_VS_SO_SET_ADDDEST:
2821 ret = ip_vs_add_dest(svc, &udest);
2823 case IP_VS_SO_SET_EDITDEST:
2824 ret = ip_vs_edit_dest(svc, &udest);
2826 case IP_VS_SO_SET_DELDEST:
2827 ret = ip_vs_del_dest(svc, &udest);
2836 mutex_unlock(&__ip_vs_mutex);
2842 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2844 struct ip_vs_scheduler *sched;
2845 struct ip_vs_kstats kstats;
2848 sched = rcu_dereference_protected(src->scheduler, 1);
2849 sched_name = sched ? sched->name : "none";
2850 dst->protocol = src->protocol;
2851 dst->addr = src->addr.ip;
2852 dst->port = src->port;
2853 dst->fwmark = src->fwmark;
2854 strscpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2855 dst->flags = src->flags;
2856 dst->timeout = src->timeout / HZ;
2857 dst->netmask = src->netmask;
2858 dst->num_dests = src->num_dests;
2859 ip_vs_copy_stats(&kstats, &src->stats);
2860 ip_vs_export_stats_user(&dst->stats, &kstats);
2864 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2865 const struct ip_vs_get_services *get,
2866 struct ip_vs_get_services __user *uptr)
2869 struct ip_vs_service *svc;
2870 struct ip_vs_service_entry entry;
2873 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2874 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2875 /* Only expose IPv4 entries to old interface */
2876 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2879 if (count >= get->num_services)
2881 memset(&entry, 0, sizeof(entry));
2882 ip_vs_copy_service(&entry, svc);
2883 if (copy_to_user(&uptr->entrytable[count],
2884 &entry, sizeof(entry))) {
2892 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2893 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2894 /* Only expose IPv4 entries to old interface */
2895 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2898 if (count >= get->num_services)
2900 memset(&entry, 0, sizeof(entry));
2901 ip_vs_copy_service(&entry, svc);
2902 if (copy_to_user(&uptr->entrytable[count],
2903 &entry, sizeof(entry))) {
2915 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2916 struct ip_vs_get_dests __user *uptr)
2918 struct ip_vs_service *svc;
2919 union nf_inet_addr addr = { .ip = get->addr };
2924 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2926 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2932 struct ip_vs_dest *dest;
2933 struct ip_vs_dest_entry entry;
2934 struct ip_vs_kstats kstats;
2936 memset(&entry, 0, sizeof(entry));
2937 list_for_each_entry(dest, &svc->destinations, n_list) {
2938 if (count >= get->num_dests)
2941 /* Cannot expose heterogeneous members via sockopt
2944 if (dest->af != svc->af)
2947 entry.addr = dest->addr.ip;
2948 entry.port = dest->port;
2949 entry.conn_flags = atomic_read(&dest->conn_flags);
2950 entry.weight = atomic_read(&dest->weight);
2951 entry.u_threshold = dest->u_threshold;
2952 entry.l_threshold = dest->l_threshold;
2953 entry.activeconns = atomic_read(&dest->activeconns);
2954 entry.inactconns = atomic_read(&dest->inactconns);
2955 entry.persistconns = atomic_read(&dest->persistconns);
2956 ip_vs_copy_stats(&kstats, &dest->stats);
2957 ip_vs_export_stats_user(&entry.stats, &kstats);
2958 if (copy_to_user(&uptr->entrytable[count],
2959 &entry, sizeof(entry))) {
2971 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2973 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2974 struct ip_vs_proto_data *pd;
2977 memset(u, 0, sizeof (*u));
2979 #ifdef CONFIG_IP_VS_PROTO_TCP
2980 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2981 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2982 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2984 #ifdef CONFIG_IP_VS_PROTO_UDP
2985 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2987 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2991 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2992 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2993 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2994 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2995 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2996 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2997 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2998 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
3001 union ip_vs_get_arglen {
3002 char field_IP_VS_SO_GET_VERSION[64];
3003 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
3004 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
3005 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
3006 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
3007 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
3008 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
3011 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
3014 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
3016 unsigned char arg[MAX_GET_ARGLEN];
3018 unsigned int copylen;
3019 struct net *net = sock_net(sk);
3020 struct netns_ipvs *ipvs = net_ipvs(net);
3023 BUILD_BUG_ON(sizeof(arg) > 255);
3024 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
3027 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
3030 copylen = get_arglen[CMDID(cmd)];
3031 if (*len < (int) copylen) {
3032 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
3036 if (copy_from_user(arg, user, copylen) != 0)
3039 * Handle daemons first since it has its own locking
3041 if (cmd == IP_VS_SO_GET_DAEMON) {
3042 struct ip_vs_daemon_user d[2];
3044 memset(&d, 0, sizeof(d));
3045 mutex_lock(&ipvs->sync_mutex);
3046 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
3047 d[0].state = IP_VS_STATE_MASTER;
3048 strscpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
3049 sizeof(d[0].mcast_ifn));
3050 d[0].syncid = ipvs->mcfg.syncid;
3052 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
3053 d[1].state = IP_VS_STATE_BACKUP;
3054 strscpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
3055 sizeof(d[1].mcast_ifn));
3056 d[1].syncid = ipvs->bcfg.syncid;
3058 if (copy_to_user(user, &d, sizeof(d)) != 0)
3060 mutex_unlock(&ipvs->sync_mutex);
3064 mutex_lock(&__ip_vs_mutex);
3066 case IP_VS_SO_GET_VERSION:
3070 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
3071 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
3072 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
3076 *len = strlen(buf)+1;
3080 case IP_VS_SO_GET_INFO:
3082 struct ip_vs_getinfo info;
3083 info.version = IP_VS_VERSION_CODE;
3084 info.size = ip_vs_conn_tab_size;
3085 info.num_services = ipvs->num_services;
3086 if (copy_to_user(user, &info, sizeof(info)) != 0)
3091 case IP_VS_SO_GET_SERVICES:
3093 struct ip_vs_get_services *get;
3096 get = (struct ip_vs_get_services *)arg;
3097 size = struct_size(get, entrytable, get->num_services);
3099 pr_err("length: %u != %u\n", *len, size);
3103 ret = __ip_vs_get_service_entries(ipvs, get, user);
3107 case IP_VS_SO_GET_SERVICE:
3109 struct ip_vs_service_entry *entry;
3110 struct ip_vs_service *svc;
3111 union nf_inet_addr addr;
3113 entry = (struct ip_vs_service_entry *)arg;
3114 addr.ip = entry->addr;
3117 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
3119 svc = __ip_vs_service_find(ipvs, AF_INET,
3120 entry->protocol, &addr,
3124 ip_vs_copy_service(entry, svc);
3125 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
3132 case IP_VS_SO_GET_DESTS:
3134 struct ip_vs_get_dests *get;
3137 get = (struct ip_vs_get_dests *)arg;
3138 size = struct_size(get, entrytable, get->num_dests);
3140 pr_err("length: %u != %u\n", *len, size);
3144 ret = __ip_vs_get_dest_entries(ipvs, get, user);
3148 case IP_VS_SO_GET_TIMEOUT:
3150 struct ip_vs_timeout_user t;
3152 __ip_vs_get_timeouts(ipvs, &t);
3153 if (copy_to_user(user, &t, sizeof(t)) != 0)
3163 mutex_unlock(&__ip_vs_mutex);
3168 static struct nf_sockopt_ops ip_vs_sockopts = {
3170 .set_optmin = IP_VS_BASE_CTL,
3171 .set_optmax = IP_VS_SO_SET_MAX+1,
3172 .set = do_ip_vs_set_ctl,
3173 .get_optmin = IP_VS_BASE_CTL,
3174 .get_optmax = IP_VS_SO_GET_MAX+1,
3175 .get = do_ip_vs_get_ctl,
3176 .owner = THIS_MODULE,
3180 * Generic Netlink interface
3183 /* IPVS genetlink family */
3184 static struct genl_family ip_vs_genl_family;
3186 /* Policy used for first-level command attributes */
3187 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
3188 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
3189 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
3190 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
3191 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
3192 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
3193 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
3196 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
3197 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
3198 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
3199 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
3200 .len = IP_VS_IFNAME_MAXLEN - 1 },
3201 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
3202 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
3203 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
3204 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
3205 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
3206 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
3209 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
3210 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
3211 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
3212 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
3213 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
3214 .len = sizeof(union nf_inet_addr) },
3215 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
3216 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
3217 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
3218 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
3219 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
3220 .len = IP_VS_PENAME_MAXLEN },
3221 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
3222 .len = sizeof(struct ip_vs_flags) },
3223 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
3224 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
3225 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
3228 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
3229 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
3230 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
3231 .len = sizeof(union nf_inet_addr) },
3232 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
3233 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
3234 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
3235 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
3236 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
3237 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
3238 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
3239 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
3240 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
3241 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
3242 [IPVS_DEST_ATTR_TUN_TYPE] = { .type = NLA_U8 },
3243 [IPVS_DEST_ATTR_TUN_PORT] = { .type = NLA_U16 },
3244 [IPVS_DEST_ATTR_TUN_FLAGS] = { .type = NLA_U16 },
3247 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
3248 struct ip_vs_kstats *kstats)
3250 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3255 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
3256 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
3257 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
3258 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3259 IPVS_STATS_ATTR_PAD) ||
3260 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3261 IPVS_STATS_ATTR_PAD) ||
3262 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
3263 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
3264 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
3265 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
3266 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
3267 goto nla_put_failure;
3268 nla_nest_end(skb, nl_stats);
3273 nla_nest_cancel(skb, nl_stats);
3277 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
3278 struct ip_vs_kstats *kstats)
3280 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3285 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
3286 IPVS_STATS_ATTR_PAD) ||
3287 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
3288 IPVS_STATS_ATTR_PAD) ||
3289 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
3290 IPVS_STATS_ATTR_PAD) ||
3291 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3292 IPVS_STATS_ATTR_PAD) ||
3293 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3294 IPVS_STATS_ATTR_PAD) ||
3295 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
3296 IPVS_STATS_ATTR_PAD) ||
3297 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
3298 IPVS_STATS_ATTR_PAD) ||
3299 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
3300 IPVS_STATS_ATTR_PAD) ||
3301 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
3302 IPVS_STATS_ATTR_PAD) ||
3303 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
3304 IPVS_STATS_ATTR_PAD))
3305 goto nla_put_failure;
3306 nla_nest_end(skb, nl_stats);
3311 nla_nest_cancel(skb, nl_stats);
3315 static int ip_vs_genl_fill_service(struct sk_buff *skb,
3316 struct ip_vs_service *svc)
3318 struct ip_vs_scheduler *sched;
3319 struct ip_vs_pe *pe;
3320 struct nlattr *nl_service;
3321 struct ip_vs_flags flags = { .flags = svc->flags,
3323 struct ip_vs_kstats kstats;
3326 nl_service = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_SERVICE);
3330 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
3331 goto nla_put_failure;
3333 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
3334 goto nla_put_failure;
3336 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
3337 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
3338 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
3339 goto nla_put_failure;
3342 sched = rcu_dereference_protected(svc->scheduler, 1);
3343 sched_name = sched ? sched->name : "none";
3344 pe = rcu_dereference_protected(svc->pe, 1);
3345 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
3346 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
3347 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3348 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
3349 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
3350 goto nla_put_failure;
3351 ip_vs_copy_stats(&kstats, &svc->stats);
3352 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3353 goto nla_put_failure;
3354 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
3355 goto nla_put_failure;
3357 nla_nest_end(skb, nl_service);
3362 nla_nest_cancel(skb, nl_service);
3366 static int ip_vs_genl_dump_service(struct sk_buff *skb,
3367 struct ip_vs_service *svc,
3368 struct netlink_callback *cb)
3372 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3373 &ip_vs_genl_family, NLM_F_MULTI,
3374 IPVS_CMD_NEW_SERVICE);
3378 if (ip_vs_genl_fill_service(skb, svc) < 0)
3379 goto nla_put_failure;
3381 genlmsg_end(skb, hdr);
3385 genlmsg_cancel(skb, hdr);
3389 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3390 struct netlink_callback *cb)
3393 int start = cb->args[0];
3394 struct ip_vs_service *svc;
3395 struct net *net = sock_net(skb->sk);
3396 struct netns_ipvs *ipvs = net_ipvs(net);
3398 mutex_lock(&__ip_vs_mutex);
3399 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3400 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3401 if (++idx <= start || (svc->ipvs != ipvs))
3403 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3405 goto nla_put_failure;
3410 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3411 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3412 if (++idx <= start || (svc->ipvs != ipvs))
3414 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3416 goto nla_put_failure;
3422 mutex_unlock(&__ip_vs_mutex);
3428 static bool ip_vs_is_af_valid(int af)
3432 #ifdef CONFIG_IP_VS_IPV6
3433 if (af == AF_INET6 && ipv6_mod_enabled())
3439 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3440 struct ip_vs_service_user_kern *usvc,
3441 struct nlattr *nla, bool full_entry,
3442 struct ip_vs_service **ret_svc)
3444 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3445 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3446 struct ip_vs_service *svc;
3448 /* Parse mandatory identifying service fields first */
3450 nla_parse_nested_deprecated(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy, NULL))
3453 nla_af = attrs[IPVS_SVC_ATTR_AF];
3454 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3455 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3456 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3457 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3459 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3462 memset(usvc, 0, sizeof(*usvc));
3464 usvc->af = nla_get_u16(nla_af);
3465 if (!ip_vs_is_af_valid(usvc->af))
3466 return -EAFNOSUPPORT;
3469 usvc->protocol = IPPROTO_TCP;
3470 usvc->fwmark = nla_get_u32(nla_fwmark);
3472 usvc->protocol = nla_get_u16(nla_protocol);
3473 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3474 usvc->port = nla_get_be16(nla_port);
3480 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3482 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3483 &usvc->addr, usvc->port);
3487 /* If a full entry was requested, check for the additional fields */
3489 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3491 struct ip_vs_flags flags;
3493 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3494 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3495 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3496 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3497 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3499 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3502 nla_memcpy(&flags, nla_flags, sizeof(flags));
3504 /* prefill flags from service if it already exists */
3506 usvc->flags = svc->flags;
3508 /* set new flags from userland */
3509 usvc->flags = (usvc->flags & ~flags.mask) |
3510 (flags.flags & flags.mask);
3511 usvc->sched_name = nla_data(nla_sched);
3512 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3513 usvc->timeout = nla_get_u32(nla_timeout);
3514 usvc->netmask = nla_get_be32(nla_netmask);
3520 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3523 struct ip_vs_service_user_kern usvc;
3524 struct ip_vs_service *svc;
3527 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, false, &svc);
3528 return ret ? ERR_PTR(ret) : svc;
3531 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3533 struct nlattr *nl_dest;
3534 struct ip_vs_kstats kstats;
3536 nl_dest = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DEST);
3540 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3541 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3542 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3543 (atomic_read(&dest->conn_flags) &
3544 IP_VS_CONN_F_FWD_MASK)) ||
3545 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3546 atomic_read(&dest->weight)) ||
3547 nla_put_u8(skb, IPVS_DEST_ATTR_TUN_TYPE,
3549 nla_put_be16(skb, IPVS_DEST_ATTR_TUN_PORT,
3551 nla_put_u16(skb, IPVS_DEST_ATTR_TUN_FLAGS,
3553 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3554 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3555 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3556 atomic_read(&dest->activeconns)) ||
3557 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3558 atomic_read(&dest->inactconns)) ||
3559 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3560 atomic_read(&dest->persistconns)) ||
3561 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3562 goto nla_put_failure;
3563 ip_vs_copy_stats(&kstats, &dest->stats);
3564 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3565 goto nla_put_failure;
3566 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3567 goto nla_put_failure;
3569 nla_nest_end(skb, nl_dest);
3574 nla_nest_cancel(skb, nl_dest);
3578 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3579 struct netlink_callback *cb)
3583 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3584 &ip_vs_genl_family, NLM_F_MULTI,
3589 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3590 goto nla_put_failure;
3592 genlmsg_end(skb, hdr);
3596 genlmsg_cancel(skb, hdr);
3600 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3601 struct netlink_callback *cb)
3604 int start = cb->args[0];
3605 struct ip_vs_service *svc;
3606 struct ip_vs_dest *dest;
3607 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3608 struct net *net = sock_net(skb->sk);
3609 struct netns_ipvs *ipvs = net_ipvs(net);
3611 mutex_lock(&__ip_vs_mutex);
3613 /* Try to find the service for which to dump destinations */
3614 if (nlmsg_parse_deprecated(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy, cb->extack))
3618 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3619 if (IS_ERR_OR_NULL(svc))
3622 /* Dump the destinations */
3623 list_for_each_entry(dest, &svc->destinations, n_list) {
3626 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3628 goto nla_put_failure;
3636 mutex_unlock(&__ip_vs_mutex);
3641 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3642 struct nlattr *nla, bool full_entry)
3644 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3645 struct nlattr *nla_addr, *nla_port;
3646 struct nlattr *nla_addr_family;
3648 /* Parse mandatory identifying destination fields first */
3650 nla_parse_nested_deprecated(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy, NULL))
3653 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3654 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3655 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3657 if (!(nla_addr && nla_port))
3660 memset(udest, 0, sizeof(*udest));
3662 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3663 udest->port = nla_get_be16(nla_port);
3665 if (nla_addr_family)
3666 udest->af = nla_get_u16(nla_addr_family);
3670 /* If a full entry was requested, check for the additional fields */
3672 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3673 *nla_l_thresh, *nla_tun_type, *nla_tun_port,
3676 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3677 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3678 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3679 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3680 nla_tun_type = attrs[IPVS_DEST_ATTR_TUN_TYPE];
3681 nla_tun_port = attrs[IPVS_DEST_ATTR_TUN_PORT];
3682 nla_tun_flags = attrs[IPVS_DEST_ATTR_TUN_FLAGS];
3684 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3687 udest->conn_flags = nla_get_u32(nla_fwd)
3688 & IP_VS_CONN_F_FWD_MASK;
3689 udest->weight = nla_get_u32(nla_weight);
3690 udest->u_threshold = nla_get_u32(nla_u_thresh);
3691 udest->l_threshold = nla_get_u32(nla_l_thresh);
3694 udest->tun_type = nla_get_u8(nla_tun_type);
3697 udest->tun_port = nla_get_be16(nla_tun_port);
3700 udest->tun_flags = nla_get_u16(nla_tun_flags);
3706 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3707 struct ipvs_sync_daemon_cfg *c)
3709 struct nlattr *nl_daemon;
3711 nl_daemon = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DAEMON);
3715 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3716 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3717 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3718 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3719 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3720 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3721 goto nla_put_failure;
3722 #ifdef CONFIG_IP_VS_IPV6
3723 if (c->mcast_af == AF_INET6) {
3724 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3725 &c->mcast_group.in6))
3726 goto nla_put_failure;
3729 if (c->mcast_af == AF_INET &&
3730 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3732 goto nla_put_failure;
3733 nla_nest_end(skb, nl_daemon);
3738 nla_nest_cancel(skb, nl_daemon);
3742 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3743 struct ipvs_sync_daemon_cfg *c,
3744 struct netlink_callback *cb)
3747 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3748 &ip_vs_genl_family, NLM_F_MULTI,
3749 IPVS_CMD_NEW_DAEMON);
3753 if (ip_vs_genl_fill_daemon(skb, state, c))
3754 goto nla_put_failure;
3756 genlmsg_end(skb, hdr);
3760 genlmsg_cancel(skb, hdr);
3764 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3765 struct netlink_callback *cb)
3767 struct net *net = sock_net(skb->sk);
3768 struct netns_ipvs *ipvs = net_ipvs(net);
3770 mutex_lock(&ipvs->sync_mutex);
3771 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3772 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3773 &ipvs->mcfg, cb) < 0)
3774 goto nla_put_failure;
3779 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3780 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3781 &ipvs->bcfg, cb) < 0)
3782 goto nla_put_failure;
3788 mutex_unlock(&ipvs->sync_mutex);
3793 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3795 struct ipvs_sync_daemon_cfg c;
3799 memset(&c, 0, sizeof(c));
3800 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3801 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3802 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3804 strscpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3805 sizeof(c.mcast_ifn));
3806 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3808 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3810 c.sync_maxlen = nla_get_u16(a);
3812 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3814 c.mcast_af = AF_INET;
3815 c.mcast_group.ip = nla_get_in_addr(a);
3816 if (!ipv4_is_multicast(c.mcast_group.ip))
3819 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3821 #ifdef CONFIG_IP_VS_IPV6
3824 c.mcast_af = AF_INET6;
3825 c.mcast_group.in6 = nla_get_in6_addr(a);
3826 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3827 if (!(addr_type & IPV6_ADDR_MULTICAST))
3830 return -EAFNOSUPPORT;
3835 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3837 c.mcast_port = nla_get_u16(a);
3839 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3841 c.mcast_ttl = nla_get_u8(a);
3843 /* The synchronization protocol is incompatible with mixed family
3846 if (ipvs->mixed_address_family_dests > 0)
3849 ret = start_sync_thread(ipvs, &c,
3850 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3854 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3858 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3861 ret = stop_sync_thread(ipvs,
3862 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3866 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3868 struct ip_vs_timeout_user t;
3870 __ip_vs_get_timeouts(ipvs, &t);
3872 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3873 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3875 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3877 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3879 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3880 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3882 return ip_vs_set_timeout(ipvs, &t);
3885 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3887 int ret = -EINVAL, cmd;
3888 struct net *net = sock_net(skb->sk);
3889 struct netns_ipvs *ipvs = net_ipvs(net);
3891 cmd = info->genlhdr->cmd;
3893 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3894 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3896 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3897 nla_parse_nested_deprecated(daemon_attrs, IPVS_DAEMON_ATTR_MAX, info->attrs[IPVS_CMD_ATTR_DAEMON], ip_vs_daemon_policy, info->extack))
3900 if (cmd == IPVS_CMD_NEW_DAEMON)
3901 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3903 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3910 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3912 bool need_full_svc = false, need_full_dest = false;
3913 struct ip_vs_service *svc = NULL;
3914 struct ip_vs_service_user_kern usvc;
3915 struct ip_vs_dest_user_kern udest;
3917 struct net *net = sock_net(skb->sk);
3918 struct netns_ipvs *ipvs = net_ipvs(net);
3920 cmd = info->genlhdr->cmd;
3922 mutex_lock(&__ip_vs_mutex);
3924 if (cmd == IPVS_CMD_FLUSH) {
3925 ret = ip_vs_flush(ipvs, false);
3927 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3928 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3930 } else if (cmd == IPVS_CMD_ZERO &&
3931 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3932 ret = ip_vs_zero_all(ipvs);
3936 /* All following commands require a service argument, so check if we
3937 * received a valid one. We need a full service specification when
3938 * adding / editing a service. Only identifying members otherwise. */
3939 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3940 need_full_svc = true;
3942 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3943 info->attrs[IPVS_CMD_ATTR_SERVICE],
3944 need_full_svc, &svc);
3948 /* Unless we're adding a new service, the service must already exist */
3949 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3954 /* Destination commands require a valid destination argument. For
3955 * adding / editing a destination, we need a full destination
3957 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3958 cmd == IPVS_CMD_DEL_DEST) {
3959 if (cmd != IPVS_CMD_DEL_DEST)
3960 need_full_dest = true;
3962 ret = ip_vs_genl_parse_dest(&udest,
3963 info->attrs[IPVS_CMD_ATTR_DEST],
3968 /* Old protocols did not allow the user to specify address
3969 * family, so we set it to zero instead. We also didn't
3970 * allow heterogeneous pools in the old code, so it's safe
3971 * to assume that this will have the same address family as
3977 if (!ip_vs_is_af_valid(udest.af)) {
3978 ret = -EAFNOSUPPORT;
3982 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3983 /* The synchronization protocol is incompatible
3984 * with mixed family services
3986 if (ipvs->sync_state) {
3991 /* Which connection types do we support? */
3992 switch (udest.conn_flags) {
3993 case IP_VS_CONN_F_TUNNEL:
3994 /* We are able to forward this */
4004 case IPVS_CMD_NEW_SERVICE:
4006 ret = ip_vs_add_service(ipvs, &usvc, &svc);
4010 case IPVS_CMD_SET_SERVICE:
4011 ret = ip_vs_edit_service(svc, &usvc);
4013 case IPVS_CMD_DEL_SERVICE:
4014 ret = ip_vs_del_service(svc);
4015 /* do not use svc, it can be freed */
4017 case IPVS_CMD_NEW_DEST:
4018 ret = ip_vs_add_dest(svc, &udest);
4020 case IPVS_CMD_SET_DEST:
4021 ret = ip_vs_edit_dest(svc, &udest);
4023 case IPVS_CMD_DEL_DEST:
4024 ret = ip_vs_del_dest(svc, &udest);
4027 ret = ip_vs_zero_service(svc);
4034 mutex_unlock(&__ip_vs_mutex);
4039 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
4041 struct sk_buff *msg;
4043 int ret, cmd, reply_cmd;
4044 struct net *net = sock_net(skb->sk);
4045 struct netns_ipvs *ipvs = net_ipvs(net);
4047 cmd = info->genlhdr->cmd;
4049 if (cmd == IPVS_CMD_GET_SERVICE)
4050 reply_cmd = IPVS_CMD_NEW_SERVICE;
4051 else if (cmd == IPVS_CMD_GET_INFO)
4052 reply_cmd = IPVS_CMD_SET_INFO;
4053 else if (cmd == IPVS_CMD_GET_CONFIG)
4054 reply_cmd = IPVS_CMD_SET_CONFIG;
4056 pr_err("unknown Generic Netlink command\n");
4060 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4064 mutex_lock(&__ip_vs_mutex);
4066 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
4068 goto nla_put_failure;
4071 case IPVS_CMD_GET_SERVICE:
4073 struct ip_vs_service *svc;
4075 svc = ip_vs_genl_find_service(ipvs,
4076 info->attrs[IPVS_CMD_ATTR_SERVICE]);
4081 ret = ip_vs_genl_fill_service(msg, svc);
4083 goto nla_put_failure;
4092 case IPVS_CMD_GET_CONFIG:
4094 struct ip_vs_timeout_user t;
4096 __ip_vs_get_timeouts(ipvs, &t);
4097 #ifdef CONFIG_IP_VS_PROTO_TCP
4098 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
4100 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
4102 goto nla_put_failure;
4104 #ifdef CONFIG_IP_VS_PROTO_UDP
4105 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
4106 goto nla_put_failure;
4112 case IPVS_CMD_GET_INFO:
4113 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
4114 IP_VS_VERSION_CODE) ||
4115 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
4116 ip_vs_conn_tab_size))
4117 goto nla_put_failure;
4121 genlmsg_end(msg, reply);
4122 ret = genlmsg_reply(msg, info);
4126 pr_err("not enough space in Netlink message\n");
4132 mutex_unlock(&__ip_vs_mutex);
4138 static const struct genl_small_ops ip_vs_genl_ops[] = {
4140 .cmd = IPVS_CMD_NEW_SERVICE,
4141 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4142 .flags = GENL_ADMIN_PERM,
4143 .doit = ip_vs_genl_set_cmd,
4146 .cmd = IPVS_CMD_SET_SERVICE,
4147 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4148 .flags = GENL_ADMIN_PERM,
4149 .doit = ip_vs_genl_set_cmd,
4152 .cmd = IPVS_CMD_DEL_SERVICE,
4153 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4154 .flags = GENL_ADMIN_PERM,
4155 .doit = ip_vs_genl_set_cmd,
4158 .cmd = IPVS_CMD_GET_SERVICE,
4159 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4160 .flags = GENL_ADMIN_PERM,
4161 .doit = ip_vs_genl_get_cmd,
4162 .dumpit = ip_vs_genl_dump_services,
4165 .cmd = IPVS_CMD_NEW_DEST,
4166 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4167 .flags = GENL_ADMIN_PERM,
4168 .doit = ip_vs_genl_set_cmd,
4171 .cmd = IPVS_CMD_SET_DEST,
4172 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4173 .flags = GENL_ADMIN_PERM,
4174 .doit = ip_vs_genl_set_cmd,
4177 .cmd = IPVS_CMD_DEL_DEST,
4178 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4179 .flags = GENL_ADMIN_PERM,
4180 .doit = ip_vs_genl_set_cmd,
4183 .cmd = IPVS_CMD_GET_DEST,
4184 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4185 .flags = GENL_ADMIN_PERM,
4186 .dumpit = ip_vs_genl_dump_dests,
4189 .cmd = IPVS_CMD_NEW_DAEMON,
4190 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4191 .flags = GENL_ADMIN_PERM,
4192 .doit = ip_vs_genl_set_daemon,
4195 .cmd = IPVS_CMD_DEL_DAEMON,
4196 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4197 .flags = GENL_ADMIN_PERM,
4198 .doit = ip_vs_genl_set_daemon,
4201 .cmd = IPVS_CMD_GET_DAEMON,
4202 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4203 .flags = GENL_ADMIN_PERM,
4204 .dumpit = ip_vs_genl_dump_daemons,
4207 .cmd = IPVS_CMD_SET_CONFIG,
4208 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4209 .flags = GENL_ADMIN_PERM,
4210 .doit = ip_vs_genl_set_cmd,
4213 .cmd = IPVS_CMD_GET_CONFIG,
4214 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4215 .flags = GENL_ADMIN_PERM,
4216 .doit = ip_vs_genl_get_cmd,
4219 .cmd = IPVS_CMD_GET_INFO,
4220 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4221 .flags = GENL_ADMIN_PERM,
4222 .doit = ip_vs_genl_get_cmd,
4225 .cmd = IPVS_CMD_ZERO,
4226 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4227 .flags = GENL_ADMIN_PERM,
4228 .doit = ip_vs_genl_set_cmd,
4231 .cmd = IPVS_CMD_FLUSH,
4232 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4233 .flags = GENL_ADMIN_PERM,
4234 .doit = ip_vs_genl_set_cmd,
4238 static struct genl_family ip_vs_genl_family __ro_after_init = {
4240 .name = IPVS_GENL_NAME,
4241 .version = IPVS_GENL_VERSION,
4242 .maxattr = IPVS_CMD_ATTR_MAX,
4243 .policy = ip_vs_cmd_policy,
4244 .netnsok = true, /* Make ipvsadm to work on netns */
4245 .module = THIS_MODULE,
4246 .small_ops = ip_vs_genl_ops,
4247 .n_small_ops = ARRAY_SIZE(ip_vs_genl_ops),
4248 .resv_start_op = IPVS_CMD_FLUSH + 1,
4251 static int __init ip_vs_genl_register(void)
4253 return genl_register_family(&ip_vs_genl_family);
4256 static void ip_vs_genl_unregister(void)
4258 genl_unregister_family(&ip_vs_genl_family);
4261 /* End of Generic Netlink interface definitions */
4264 * per netns intit/exit func.
4266 #ifdef CONFIG_SYSCTL
4267 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
4269 struct net *net = ipvs->net;
4270 struct ctl_table *tbl;
4272 size_t ctl_table_size = ARRAY_SIZE(vs_vars);
4274 atomic_set(&ipvs->dropentry, 0);
4275 spin_lock_init(&ipvs->dropentry_lock);
4276 spin_lock_init(&ipvs->droppacket_lock);
4277 spin_lock_init(&ipvs->securetcp_lock);
4278 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
4279 INIT_DELAYED_WORK(&ipvs->expire_nodest_conn_work,
4280 expire_nodest_conn_handler);
4281 ipvs->est_stopped = 0;
4283 if (!net_eq(net, &init_net)) {
4284 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
4288 /* Don't export sysctls to unprivileged users */
4289 if (net->user_ns != &init_user_ns) {
4290 tbl[0].procname = NULL;
4295 /* Initialize sysctl defaults */
4296 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
4297 if (tbl[idx].proc_handler == proc_do_defense_mode)
4298 tbl[idx].extra2 = ipvs;
4301 ipvs->sysctl_amemthresh = 1024;
4302 tbl[idx++].data = &ipvs->sysctl_amemthresh;
4303 ipvs->sysctl_am_droprate = 10;
4304 tbl[idx++].data = &ipvs->sysctl_am_droprate;
4305 tbl[idx++].data = &ipvs->sysctl_drop_entry;
4306 tbl[idx++].data = &ipvs->sysctl_drop_packet;
4307 #ifdef CONFIG_IP_VS_NFCT
4308 tbl[idx++].data = &ipvs->sysctl_conntrack;
4310 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
4311 ipvs->sysctl_snat_reroute = 1;
4312 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
4313 ipvs->sysctl_sync_ver = 1;
4314 tbl[idx++].data = &ipvs->sysctl_sync_ver;
4315 ipvs->sysctl_sync_ports = 1;
4316 tbl[idx++].data = &ipvs->sysctl_sync_ports;
4317 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
4318 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
4319 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
4320 ipvs->sysctl_sync_sock_size = 0;
4321 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
4322 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
4323 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
4324 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
4325 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
4326 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
4327 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
4328 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
4329 tbl[idx].data = &ipvs->sysctl_sync_threshold;
4330 tbl[idx].extra2 = ipvs;
4331 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
4332 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
4333 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
4334 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
4335 tbl[idx++].data = &ipvs->sysctl_sync_retries;
4336 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
4337 ipvs->sysctl_pmtu_disc = 1;
4338 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
4339 tbl[idx++].data = &ipvs->sysctl_backup_only;
4340 ipvs->sysctl_conn_reuse_mode = 1;
4341 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
4342 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
4343 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
4344 ipvs->sysctl_run_estimation = 1;
4345 tbl[idx].extra2 = ipvs;
4346 tbl[idx++].data = &ipvs->sysctl_run_estimation;
4348 ipvs->est_cpulist_valid = 0;
4349 tbl[idx].extra2 = ipvs;
4350 tbl[idx++].data = &ipvs->sysctl_est_cpulist;
4352 ipvs->sysctl_est_nice = IPVS_EST_NICE;
4353 tbl[idx].extra2 = ipvs;
4354 tbl[idx++].data = &ipvs->sysctl_est_nice;
4356 #ifdef CONFIG_IP_VS_DEBUG
4357 /* Global sysctls must be ro in non-init netns */
4358 if (!net_eq(net, &init_net))
4359 tbl[idx++].mode = 0444;
4363 ipvs->sysctl_hdr = register_net_sysctl_sz(net, "net/ipv4/vs", tbl,
4365 if (!ipvs->sysctl_hdr)
4367 ipvs->sysctl_tbl = tbl;
4369 ret = ip_vs_start_estimator(ipvs, &ipvs->tot_stats->s);
4373 /* Schedule defense work */
4374 queue_delayed_work(system_long_wq, &ipvs->defense_work,
4375 DEFENSE_TIMER_PERIOD);
4380 unregister_net_sysctl_table(ipvs->sysctl_hdr);
4381 if (!net_eq(net, &init_net))
4386 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
4388 struct net *net = ipvs->net;
4390 cancel_delayed_work_sync(&ipvs->expire_nodest_conn_work);
4391 cancel_delayed_work_sync(&ipvs->defense_work);
4392 cancel_work_sync(&ipvs->defense_work.work);
4393 unregister_net_sysctl_table(ipvs->sysctl_hdr);
4394 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats->s);
4396 if (ipvs->est_cpulist_valid)
4397 free_cpumask_var(ipvs->sysctl_est_cpulist);
4399 if (!net_eq(net, &init_net))
4400 kfree(ipvs->sysctl_tbl);
4405 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
4406 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
4410 static struct notifier_block ip_vs_dst_notifier = {
4411 .notifier_call = ip_vs_dst_event,
4412 #ifdef CONFIG_IP_VS_IPV6
4413 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
4417 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
4422 /* Initialize rs_table */
4423 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
4424 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
4426 INIT_LIST_HEAD(&ipvs->dest_trash);
4427 spin_lock_init(&ipvs->dest_trash_lock);
4428 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
4429 atomic_set(&ipvs->ftpsvc_counter, 0);
4430 atomic_set(&ipvs->nullsvc_counter, 0);
4431 atomic_set(&ipvs->conn_out_counter, 0);
4433 INIT_DELAYED_WORK(&ipvs->est_reload_work, est_reload_work_handler);
4436 ipvs->tot_stats = kzalloc(sizeof(*ipvs->tot_stats), GFP_KERNEL);
4437 if (!ipvs->tot_stats)
4439 if (ip_vs_stats_init_alloc(&ipvs->tot_stats->s) < 0)
4442 #ifdef CONFIG_PROC_FS
4443 if (!proc_create_net("ip_vs", 0, ipvs->net->proc_net,
4444 &ip_vs_info_seq_ops, sizeof(struct ip_vs_iter)))
4446 if (!proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4447 ip_vs_stats_show, NULL))
4449 if (!proc_create_net_single("ip_vs_stats_percpu", 0,
4450 ipvs->net->proc_net,
4451 ip_vs_stats_percpu_show, NULL))
4455 ret = ip_vs_control_net_init_sysctl(ipvs);
4462 #ifdef CONFIG_PROC_FS
4463 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4466 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4469 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4473 ip_vs_stats_release(&ipvs->tot_stats->s);
4476 kfree(ipvs->tot_stats);
4482 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4484 ip_vs_trash_cleanup(ipvs);
4485 ip_vs_control_net_cleanup_sysctl(ipvs);
4486 cancel_delayed_work_sync(&ipvs->est_reload_work);
4487 #ifdef CONFIG_PROC_FS
4488 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4489 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4490 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4492 call_rcu(&ipvs->tot_stats->rcu_head, ip_vs_stats_rcu_free);
4495 int __init ip_vs_register_nl_ioctl(void)
4499 ret = nf_register_sockopt(&ip_vs_sockopts);
4501 pr_err("cannot register sockopt.\n");
4505 ret = ip_vs_genl_register();
4507 pr_err("cannot register Generic Netlink interface.\n");
4513 nf_unregister_sockopt(&ip_vs_sockopts);
4518 void ip_vs_unregister_nl_ioctl(void)
4520 ip_vs_genl_unregister();
4521 nf_unregister_sockopt(&ip_vs_sockopts);
4524 int __init ip_vs_control_init(void)
4529 /* Initialize svc_table, ip_vs_svc_fwm_table */
4530 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4531 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4532 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4535 smp_wmb(); /* Do we really need it now ? */
4537 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4545 void ip_vs_control_cleanup(void)
4547 unregister_netdevice_notifier(&ip_vs_dst_notifier);
4548 /* relying on common rcu_barrier() in ip_vs_cleanup() */