2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/workqueue.h>
32 #include <linux/swap.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
47 #include <net/route.h>
49 #include <net/genetlink.h>
51 #include <asm/uaccess.h>
53 #include <net/ip_vs.h>
55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56 static DEFINE_MUTEX(__ip_vs_mutex);
58 /* sysctl variables */
60 #ifdef CONFIG_IP_VS_DEBUG
61 static int sysctl_ip_vs_debug_level = 0;
63 int ip_vs_get_debug_level(void)
65 return sysctl_ip_vs_debug_level;
71 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
74 #ifdef CONFIG_IP_VS_IPV6
75 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
76 static bool __ip_vs_addr_is_local_v6(struct net *net,
77 const struct in6_addr *addr)
82 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
85 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
94 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
97 static void update_defense_level(struct netns_ipvs *ipvs)
100 static int old_secure_tcp = 0;
105 /* we only count free and buffered memory (in pages) */
107 availmem = i.freeram + i.bufferram;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem = (availmem < ipvs->sysctl_amemthresh);
118 spin_lock(&ipvs->dropentry_lock);
119 switch (ipvs->sysctl_drop_entry) {
121 atomic_set(&ipvs->dropentry, 0);
125 atomic_set(&ipvs->dropentry, 1);
126 ipvs->sysctl_drop_entry = 2;
128 atomic_set(&ipvs->dropentry, 0);
133 atomic_set(&ipvs->dropentry, 1);
135 atomic_set(&ipvs->dropentry, 0);
136 ipvs->sysctl_drop_entry = 1;
140 atomic_set(&ipvs->dropentry, 1);
143 spin_unlock(&ipvs->dropentry_lock);
146 spin_lock(&ipvs->droppacket_lock);
147 switch (ipvs->sysctl_drop_packet) {
153 ipvs->drop_rate = ipvs->drop_counter
154 = ipvs->sysctl_amemthresh /
155 (ipvs->sysctl_amemthresh-availmem);
156 ipvs->sysctl_drop_packet = 2;
163 ipvs->drop_rate = ipvs->drop_counter
164 = ipvs->sysctl_amemthresh /
165 (ipvs->sysctl_amemthresh-availmem);
168 ipvs->sysctl_drop_packet = 1;
172 ipvs->drop_rate = ipvs->sysctl_am_droprate;
175 spin_unlock(&ipvs->droppacket_lock);
178 spin_lock(&ipvs->securetcp_lock);
179 switch (ipvs->sysctl_secure_tcp) {
181 if (old_secure_tcp >= 2)
186 if (old_secure_tcp < 2)
188 ipvs->sysctl_secure_tcp = 2;
190 if (old_secure_tcp >= 2)
196 if (old_secure_tcp < 2)
199 if (old_secure_tcp >= 2)
201 ipvs->sysctl_secure_tcp = 1;
205 if (old_secure_tcp < 2)
209 old_secure_tcp = ipvs->sysctl_secure_tcp;
211 ip_vs_protocol_timeout_change(ipvs,
212 ipvs->sysctl_secure_tcp > 1);
213 spin_unlock(&ipvs->securetcp_lock);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct *work)
226 struct netns_ipvs *ipvs =
227 container_of(work, struct netns_ipvs, defense_work.work);
229 update_defense_level(ipvs);
230 if (atomic_read(&ipvs->dropentry))
231 ip_vs_random_dropentry(ipvs->net);
232 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE);
250 * Hash table: for virtual service lookups
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct net *net, int af, unsigned int proto,
267 const union nf_inet_addr *addr, __be16 port)
269 register unsigned int porth = ntohs(port);
270 __be32 addr_fold = addr->ip;
273 #ifdef CONFIG_IP_VS_IPV6
275 addr_fold = addr->ip6[0]^addr->ip6[1]^
276 addr->ip6[2]^addr->ip6[3];
278 ahash = ntohl(addr_fold);
279 ahash ^= ((size_t) net >> 8);
281 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
286 * Returns hash value of fwmark for virtual service lookup
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct net *net, __u32 fwmark)
290 return (((size_t)net>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
294 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
295 * or in the ip_vs_svc_fwm_table by fwmark.
296 * Should be called with locked tables.
298 static int ip_vs_svc_hash(struct ip_vs_service *svc)
302 if (svc->flags & IP_VS_SVC_F_HASHED) {
303 pr_err("%s(): request for already hashed, called from %pF\n",
304 __func__, __builtin_return_address(0));
308 if (svc->fwmark == 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash = ip_vs_svc_hashkey(svc->net, svc->af, svc->protocol,
313 &svc->addr, svc->port);
314 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
317 * Hash it by fwmark in svc_fwm_table
319 hash = ip_vs_svc_fwm_hashkey(svc->net, svc->fwmark);
320 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
323 svc->flags |= IP_VS_SVC_F_HASHED;
324 /* increase its refcnt because it is referenced by the svc table */
325 atomic_inc(&svc->refcnt);
331 * Unhashes a service from svc_table / svc_fwm_table.
332 * Should be called with locked tables.
334 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
336 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
337 pr_err("%s(): request for unhash flagged, called from %pF\n",
338 __func__, __builtin_return_address(0));
342 if (svc->fwmark == 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc->s_list);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc->f_list);
350 svc->flags &= ~IP_VS_SVC_F_HASHED;
351 atomic_dec(&svc->refcnt);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service *
360 __ip_vs_service_find(struct net *net, int af, __u16 protocol,
361 const union nf_inet_addr *vaddr, __be16 vport)
364 struct ip_vs_service *svc;
366 /* Check for "full" addressed entries */
367 hash = ip_vs_svc_hashkey(net, af, protocol, vaddr, vport);
369 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
371 && ip_vs_addr_equal(af, &svc->addr, vaddr)
372 && (svc->port == vport)
373 && (svc->protocol == protocol)
374 && net_eq(svc->net, net)) {
385 * Get service by {fwmark} in the service table.
387 static inline struct ip_vs_service *
388 __ip_vs_svc_fwm_find(struct net *net, int af, __u32 fwmark)
391 struct ip_vs_service *svc;
393 /* Check for fwmark addressed entries */
394 hash = ip_vs_svc_fwm_hashkey(net, fwmark);
396 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
397 if (svc->fwmark == fwmark && svc->af == af
398 && net_eq(svc->net, net)) {
407 /* Find service, called under RCU lock */
408 struct ip_vs_service *
409 ip_vs_service_find(struct net *net, int af, __u32 fwmark, __u16 protocol,
410 const union nf_inet_addr *vaddr, __be16 vport)
412 struct ip_vs_service *svc;
413 struct netns_ipvs *ipvs = net_ipvs(net);
416 * Check the table hashed by fwmark first
419 svc = __ip_vs_svc_fwm_find(net, af, fwmark);
425 * Check the table hashed by <protocol,addr,port>
426 * for "full" addressed entries
428 svc = __ip_vs_service_find(net, af, protocol, vaddr, vport);
431 && protocol == IPPROTO_TCP
432 && atomic_read(&ipvs->ftpsvc_counter)
433 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
435 * Check if ftp service entry exists, the packet
436 * might belong to FTP data connections.
438 svc = __ip_vs_service_find(net, af, protocol, vaddr, FTPPORT);
442 && atomic_read(&ipvs->nullsvc_counter)) {
444 * Check if the catch-all port (port zero) exists
446 svc = __ip_vs_service_find(net, af, protocol, vaddr, 0);
450 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
451 fwmark, ip_vs_proto_name(protocol),
452 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
453 svc ? "hit" : "not hit");
460 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
462 atomic_inc(&svc->refcnt);
463 rcu_assign_pointer(dest->svc, svc);
466 static void ip_vs_service_free(struct ip_vs_service *svc)
468 free_percpu(svc->stats.cpustats);
472 static void ip_vs_service_rcu_free(struct rcu_head *head)
474 struct ip_vs_service *svc;
476 svc = container_of(head, struct ip_vs_service, rcu_head);
477 ip_vs_service_free(svc);
480 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
482 if (atomic_dec_and_test(&svc->refcnt)) {
483 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
485 IP_VS_DBG_ADDR(svc->af, &svc->addr),
488 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
490 ip_vs_service_free(svc);
496 * Returns hash value for real service
498 static inline unsigned int ip_vs_rs_hashkey(int af,
499 const union nf_inet_addr *addr,
502 register unsigned int porth = ntohs(port);
503 __be32 addr_fold = addr->ip;
505 #ifdef CONFIG_IP_VS_IPV6
507 addr_fold = addr->ip6[0]^addr->ip6[1]^
508 addr->ip6[2]^addr->ip6[3];
511 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
515 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
516 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
520 if (dest->in_rs_table)
524 * Hash by proto,addr,port,
525 * which are the parameters of the real service.
527 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
529 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
530 dest->in_rs_table = 1;
533 /* Unhash ip_vs_dest from rs_table. */
534 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
537 * Remove it from the rs_table table.
539 if (dest->in_rs_table) {
540 hlist_del_rcu(&dest->d_list);
541 dest->in_rs_table = 0;
545 /* Check if real service by <proto,addr,port> is present */
546 bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
547 const union nf_inet_addr *daddr, __be16 dport)
549 struct netns_ipvs *ipvs = net_ipvs(net);
551 struct ip_vs_dest *dest;
553 /* Check for "full" addressed entries */
554 hash = ip_vs_rs_hashkey(af, daddr, dport);
557 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
558 if (dest->port == dport &&
560 ip_vs_addr_equal(af, &dest->addr, daddr) &&
561 (dest->protocol == protocol || dest->vfwmark)) {
572 /* Lookup destination by {addr,port} in the given service
573 * Called under RCU lock.
575 static struct ip_vs_dest *
576 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
577 const union nf_inet_addr *daddr, __be16 dport)
579 struct ip_vs_dest *dest;
582 * Find the destination for the given service
584 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
585 if ((dest->af == dest_af) &&
586 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
587 (dest->port == dport)) {
597 * Find destination by {daddr,dport,vaddr,protocol}
598 * Created to be used in ip_vs_process_message() in
599 * the backup synchronization daemon. It finds the
600 * destination to be bound to the received connection
602 * Called under RCU lock, no refcnt is returned.
604 struct ip_vs_dest *ip_vs_find_dest(struct net *net, int svc_af, int dest_af,
605 const union nf_inet_addr *daddr,
607 const union nf_inet_addr *vaddr,
608 __be16 vport, __u16 protocol, __u32 fwmark,
611 struct ip_vs_dest *dest;
612 struct ip_vs_service *svc;
615 svc = ip_vs_service_find(net, svc_af, fwmark, protocol, vaddr, vport);
618 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
620 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
622 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
626 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
628 struct ip_vs_dest_dst *dest_dst = container_of(head,
629 struct ip_vs_dest_dst,
632 dst_release(dest_dst->dst_cache);
636 /* Release dest_dst and dst_cache for dest in user context */
637 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
639 struct ip_vs_dest_dst *old;
641 old = rcu_dereference_protected(dest->dest_dst, 1);
643 RCU_INIT_POINTER(dest->dest_dst, NULL);
644 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
649 * Lookup dest by {svc,addr,port} in the destination trash.
650 * The destination trash is used to hold the destinations that are removed
651 * from the service table but are still referenced by some conn entries.
652 * The reason to add the destination trash is when the dest is temporary
653 * down (either by administrator or by monitor program), the dest can be
654 * picked back from the trash, the remaining connections to the dest can
655 * continue, and the counting information of the dest is also useful for
658 static struct ip_vs_dest *
659 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
660 const union nf_inet_addr *daddr, __be16 dport)
662 struct ip_vs_dest *dest;
663 struct netns_ipvs *ipvs = net_ipvs(svc->net);
666 * Find the destination in trash
668 spin_lock_bh(&ipvs->dest_trash_lock);
669 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
670 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
673 IP_VS_DBG_ADDR(dest->af, &dest->addr),
675 atomic_read(&dest->refcnt));
676 if (dest->af == dest_af &&
677 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
678 dest->port == dport &&
679 dest->vfwmark == svc->fwmark &&
680 dest->protocol == svc->protocol &&
682 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
683 dest->vport == svc->port))) {
685 list_del(&dest->t_list);
686 ip_vs_dest_hold(dest);
694 spin_unlock_bh(&ipvs->dest_trash_lock);
699 static void ip_vs_dest_free(struct ip_vs_dest *dest)
701 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
703 __ip_vs_dst_cache_reset(dest);
704 __ip_vs_svc_put(svc, false);
705 free_percpu(dest->stats.cpustats);
706 ip_vs_dest_put_and_free(dest);
710 * Clean up all the destinations in the trash
711 * Called by the ip_vs_control_cleanup()
713 * When the ip_vs_control_clearup is activated by ipvs module exit,
714 * the service tables must have been flushed and all the connections
715 * are expired, and the refcnt of each destination in the trash must
716 * be 0, so we simply release them here.
718 static void ip_vs_trash_cleanup(struct net *net)
720 struct ip_vs_dest *dest, *nxt;
721 struct netns_ipvs *ipvs = net_ipvs(net);
723 del_timer_sync(&ipvs->dest_trash_timer);
724 /* No need to use dest_trash_lock */
725 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
726 list_del(&dest->t_list);
727 ip_vs_dest_free(dest);
732 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
734 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
736 spin_lock_bh(&src->lock);
738 IP_VS_SHOW_STATS_COUNTER(conns);
739 IP_VS_SHOW_STATS_COUNTER(inpkts);
740 IP_VS_SHOW_STATS_COUNTER(outpkts);
741 IP_VS_SHOW_STATS_COUNTER(inbytes);
742 IP_VS_SHOW_STATS_COUNTER(outbytes);
744 ip_vs_read_estimator(dst, src);
746 spin_unlock_bh(&src->lock);
750 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
752 dst->conns = (u32)src->conns;
753 dst->inpkts = (u32)src->inpkts;
754 dst->outpkts = (u32)src->outpkts;
755 dst->inbytes = src->inbytes;
756 dst->outbytes = src->outbytes;
757 dst->cps = (u32)src->cps;
758 dst->inpps = (u32)src->inpps;
759 dst->outpps = (u32)src->outpps;
760 dst->inbps = (u32)src->inbps;
761 dst->outbps = (u32)src->outbps;
765 ip_vs_zero_stats(struct ip_vs_stats *stats)
767 spin_lock_bh(&stats->lock);
769 /* get current counters as zero point, rates are zeroed */
771 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
773 IP_VS_ZERO_STATS_COUNTER(conns);
774 IP_VS_ZERO_STATS_COUNTER(inpkts);
775 IP_VS_ZERO_STATS_COUNTER(outpkts);
776 IP_VS_ZERO_STATS_COUNTER(inbytes);
777 IP_VS_ZERO_STATS_COUNTER(outbytes);
779 ip_vs_zero_estimator(stats);
781 spin_unlock_bh(&stats->lock);
785 * Update a destination in the given service
788 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
789 struct ip_vs_dest_user_kern *udest, int add)
791 struct netns_ipvs *ipvs = net_ipvs(svc->net);
792 struct ip_vs_service *old_svc;
793 struct ip_vs_scheduler *sched;
796 /* We cannot modify an address and change the address family */
797 BUG_ON(!add && udest->af != dest->af);
799 if (add && udest->af != svc->af)
800 ipvs->mixed_address_family_dests++;
802 /* set the weight and the flags */
803 atomic_set(&dest->weight, udest->weight);
804 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
805 conn_flags |= IP_VS_CONN_F_INACTIVE;
807 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
808 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
809 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
812 * Put the real service in rs_table if not present.
813 * For now only for NAT!
815 ip_vs_rs_hash(ipvs, dest);
817 atomic_set(&dest->conn_flags, conn_flags);
819 /* bind the service */
820 old_svc = rcu_dereference_protected(dest->svc, 1);
822 __ip_vs_bind_svc(dest, svc);
824 if (old_svc != svc) {
825 ip_vs_zero_stats(&dest->stats);
826 __ip_vs_bind_svc(dest, svc);
827 __ip_vs_svc_put(old_svc, true);
831 /* set the dest status flags */
832 dest->flags |= IP_VS_DEST_F_AVAILABLE;
834 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
835 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
836 dest->u_threshold = udest->u_threshold;
837 dest->l_threshold = udest->l_threshold;
839 dest->af = udest->af;
841 spin_lock_bh(&dest->dst_lock);
842 __ip_vs_dst_cache_reset(dest);
843 spin_unlock_bh(&dest->dst_lock);
845 sched = rcu_dereference_protected(svc->scheduler, 1);
847 ip_vs_start_estimator(svc->net, &dest->stats);
848 list_add_rcu(&dest->n_list, &svc->destinations);
851 sched->add_dest(svc, dest);
854 sched->upd_dest(svc, dest);
860 * Create a destination for the given service
863 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
864 struct ip_vs_dest **dest_p)
866 struct ip_vs_dest *dest;
867 unsigned int atype, i;
871 #ifdef CONFIG_IP_VS_IPV6
872 if (udest->af == AF_INET6) {
873 atype = ipv6_addr_type(&udest->addr.in6);
874 if ((!(atype & IPV6_ADDR_UNICAST) ||
875 atype & IPV6_ADDR_LINKLOCAL) &&
876 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
881 atype = inet_addr_type(svc->net, udest->addr.ip);
882 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
886 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
890 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
891 if (!dest->stats.cpustats)
894 for_each_possible_cpu(i) {
895 struct ip_vs_cpu_stats *ip_vs_dest_stats;
896 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
897 u64_stats_init(&ip_vs_dest_stats->syncp);
900 dest->af = udest->af;
901 dest->protocol = svc->protocol;
902 dest->vaddr = svc->addr;
903 dest->vport = svc->port;
904 dest->vfwmark = svc->fwmark;
905 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
906 dest->port = udest->port;
908 atomic_set(&dest->activeconns, 0);
909 atomic_set(&dest->inactconns, 0);
910 atomic_set(&dest->persistconns, 0);
911 atomic_set(&dest->refcnt, 1);
913 INIT_HLIST_NODE(&dest->d_list);
914 spin_lock_init(&dest->dst_lock);
915 spin_lock_init(&dest->stats.lock);
916 __ip_vs_update_dest(svc, dest, udest, 1);
930 * Add a destination into an existing service
933 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
935 struct ip_vs_dest *dest;
936 union nf_inet_addr daddr;
937 __be16 dport = udest->port;
942 if (udest->weight < 0) {
943 pr_err("%s(): server weight less than zero\n", __func__);
947 if (udest->l_threshold > udest->u_threshold) {
948 pr_err("%s(): lower threshold is higher than upper threshold\n",
953 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
955 /* We use function that requires RCU lock */
957 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
961 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
966 * Check if the dest already exists in the trash and
967 * is from the same service
969 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
972 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
973 "dest->refcnt=%d, service %u/%s:%u\n",
974 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
975 atomic_read(&dest->refcnt),
977 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
980 __ip_vs_update_dest(svc, dest, udest, 1);
984 * Allocate and initialize the dest structure
986 ret = ip_vs_new_dest(svc, udest, &dest);
995 * Edit a destination in the given service
998 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1000 struct ip_vs_dest *dest;
1001 union nf_inet_addr daddr;
1002 __be16 dport = udest->port;
1006 if (udest->weight < 0) {
1007 pr_err("%s(): server weight less than zero\n", __func__);
1011 if (udest->l_threshold > udest->u_threshold) {
1012 pr_err("%s(): lower threshold is higher than upper threshold\n",
1017 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1019 /* We use function that requires RCU lock */
1021 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1025 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1029 __ip_vs_update_dest(svc, dest, udest, 0);
1036 * Delete a destination (must be already unlinked from the service)
1038 static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1041 struct netns_ipvs *ipvs = net_ipvs(net);
1043 ip_vs_stop_estimator(net, &dest->stats);
1046 * Remove it from the d-linked list with the real services.
1048 ip_vs_rs_unhash(dest);
1050 spin_lock_bh(&ipvs->dest_trash_lock);
1051 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1052 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1053 atomic_read(&dest->refcnt));
1054 if (list_empty(&ipvs->dest_trash) && !cleanup)
1055 mod_timer(&ipvs->dest_trash_timer,
1056 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1057 /* dest lives in trash without reference */
1058 list_add(&dest->t_list, &ipvs->dest_trash);
1059 dest->idle_start = 0;
1060 spin_unlock_bh(&ipvs->dest_trash_lock);
1061 ip_vs_dest_put(dest);
1066 * Unlink a destination from the given service
1068 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1069 struct ip_vs_dest *dest,
1072 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1075 * Remove it from the d-linked destination list.
1077 list_del_rcu(&dest->n_list);
1080 if (dest->af != svc->af)
1081 net_ipvs(svc->net)->mixed_address_family_dests--;
1084 struct ip_vs_scheduler *sched;
1086 sched = rcu_dereference_protected(svc->scheduler, 1);
1087 if (sched->del_dest)
1088 sched->del_dest(svc, dest);
1094 * Delete a destination server in the given service
1097 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1099 struct ip_vs_dest *dest;
1100 __be16 dport = udest->port;
1104 /* We use function that requires RCU lock */
1106 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1110 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1115 * Unlink dest from the service
1117 __ip_vs_unlink_dest(svc, dest, 1);
1120 * Delete the destination
1122 __ip_vs_del_dest(svc->net, dest, false);
1129 static void ip_vs_dest_trash_expire(unsigned long data)
1131 struct net *net = (struct net *) data;
1132 struct netns_ipvs *ipvs = net_ipvs(net);
1133 struct ip_vs_dest *dest, *next;
1134 unsigned long now = jiffies;
1136 spin_lock(&ipvs->dest_trash_lock);
1137 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1138 if (atomic_read(&dest->refcnt) > 0)
1140 if (dest->idle_start) {
1141 if (time_before(now, dest->idle_start +
1142 IP_VS_DEST_TRASH_PERIOD))
1145 dest->idle_start = max(1UL, now);
1148 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1150 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1152 list_del(&dest->t_list);
1153 ip_vs_dest_free(dest);
1155 if (!list_empty(&ipvs->dest_trash))
1156 mod_timer(&ipvs->dest_trash_timer,
1157 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1158 spin_unlock(&ipvs->dest_trash_lock);
1162 * Add a service into the service hash table
1165 ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1166 struct ip_vs_service **svc_p)
1169 struct ip_vs_scheduler *sched = NULL;
1170 struct ip_vs_pe *pe = NULL;
1171 struct ip_vs_service *svc = NULL;
1172 struct netns_ipvs *ipvs = net_ipvs(net);
1174 /* increase the module use count */
1175 ip_vs_use_count_inc();
1177 /* Lookup the scheduler by 'u->sched_name' */
1178 sched = ip_vs_scheduler_get(u->sched_name);
1179 if (sched == NULL) {
1180 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1185 if (u->pe_name && *u->pe_name) {
1186 pe = ip_vs_pe_getbyname(u->pe_name);
1188 pr_info("persistence engine module ip_vs_pe_%s "
1189 "not found\n", u->pe_name);
1195 #ifdef CONFIG_IP_VS_IPV6
1196 if (u->af == AF_INET6) {
1197 __u32 plen = (__force __u32) u->netmask;
1199 if (plen < 1 || plen > 128) {
1206 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1208 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1212 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1213 if (!svc->stats.cpustats) {
1218 for_each_possible_cpu(i) {
1219 struct ip_vs_cpu_stats *ip_vs_stats;
1220 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1221 u64_stats_init(&ip_vs_stats->syncp);
1225 /* I'm the first user of the service */
1226 atomic_set(&svc->refcnt, 0);
1229 svc->protocol = u->protocol;
1230 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1231 svc->port = u->port;
1232 svc->fwmark = u->fwmark;
1233 svc->flags = u->flags;
1234 svc->timeout = u->timeout * HZ;
1235 svc->netmask = u->netmask;
1238 INIT_LIST_HEAD(&svc->destinations);
1239 spin_lock_init(&svc->sched_lock);
1240 spin_lock_init(&svc->stats.lock);
1242 /* Bind the scheduler */
1243 ret = ip_vs_bind_scheduler(svc, sched);
1248 /* Bind the ct retriever */
1249 RCU_INIT_POINTER(svc->pe, pe);
1252 /* Update the virtual service counters */
1253 if (svc->port == FTPPORT)
1254 atomic_inc(&ipvs->ftpsvc_counter);
1255 else if (svc->port == 0)
1256 atomic_inc(&ipvs->nullsvc_counter);
1258 ip_vs_start_estimator(net, &svc->stats);
1260 /* Count only IPv4 services for old get/setsockopt interface */
1261 if (svc->af == AF_INET)
1262 ipvs->num_services++;
1264 /* Hash the service into the service table */
1265 ip_vs_svc_hash(svc);
1268 /* Now there is a service - full throttle */
1275 ip_vs_unbind_scheduler(svc, sched);
1276 ip_vs_service_free(svc);
1278 ip_vs_scheduler_put(sched);
1281 /* decrease the module use count */
1282 ip_vs_use_count_dec();
1289 * Edit a service and bind it with a new scheduler
1292 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1294 struct ip_vs_scheduler *sched, *old_sched;
1295 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1299 * Lookup the scheduler, by 'u->sched_name'
1301 sched = ip_vs_scheduler_get(u->sched_name);
1302 if (sched == NULL) {
1303 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1308 if (u->pe_name && *u->pe_name) {
1309 pe = ip_vs_pe_getbyname(u->pe_name);
1311 pr_info("persistence engine module ip_vs_pe_%s "
1312 "not found\n", u->pe_name);
1319 #ifdef CONFIG_IP_VS_IPV6
1320 if (u->af == AF_INET6) {
1321 __u32 plen = (__force __u32) u->netmask;
1323 if (plen < 1 || plen > 128) {
1330 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1331 if (sched != old_sched) {
1332 /* Bind the new scheduler */
1333 ret = ip_vs_bind_scheduler(svc, sched);
1338 /* Unbind the old scheduler on success */
1339 ip_vs_unbind_scheduler(svc, old_sched);
1343 * Set the flags and timeout value
1345 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1346 svc->timeout = u->timeout * HZ;
1347 svc->netmask = u->netmask;
1349 old_pe = rcu_dereference_protected(svc->pe, 1);
1351 rcu_assign_pointer(svc->pe, pe);
1354 ip_vs_scheduler_put(old_sched);
1355 ip_vs_pe_put(old_pe);
1360 * Delete a service from the service list
1361 * - The service must be unlinked, unlocked and not referenced!
1362 * - We are called under _bh lock
1364 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1366 struct ip_vs_dest *dest, *nxt;
1367 struct ip_vs_scheduler *old_sched;
1368 struct ip_vs_pe *old_pe;
1369 struct netns_ipvs *ipvs = net_ipvs(svc->net);
1371 pr_info("%s: enter\n", __func__);
1373 /* Count only IPv4 services for old get/setsockopt interface */
1374 if (svc->af == AF_INET)
1375 ipvs->num_services--;
1377 ip_vs_stop_estimator(svc->net, &svc->stats);
1379 /* Unbind scheduler */
1380 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1381 ip_vs_unbind_scheduler(svc, old_sched);
1382 ip_vs_scheduler_put(old_sched);
1384 /* Unbind persistence engine, keep svc->pe */
1385 old_pe = rcu_dereference_protected(svc->pe, 1);
1386 ip_vs_pe_put(old_pe);
1389 * Unlink the whole destination list
1391 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1392 __ip_vs_unlink_dest(svc, dest, 0);
1393 __ip_vs_del_dest(svc->net, dest, cleanup);
1397 * Update the virtual service counters
1399 if (svc->port == FTPPORT)
1400 atomic_dec(&ipvs->ftpsvc_counter);
1401 else if (svc->port == 0)
1402 atomic_dec(&ipvs->nullsvc_counter);
1405 * Free the service if nobody refers to it
1407 __ip_vs_svc_put(svc, true);
1409 /* decrease the module use count */
1410 ip_vs_use_count_dec();
1414 * Unlink a service from list and try to delete it if its refcnt reached 0
1416 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1418 /* Hold svc to avoid double release from dest_trash */
1419 atomic_inc(&svc->refcnt);
1421 * Unhash it from the service table
1423 ip_vs_svc_unhash(svc);
1425 __ip_vs_del_service(svc, cleanup);
1429 * Delete a service from the service list
1431 static int ip_vs_del_service(struct ip_vs_service *svc)
1435 ip_vs_unlink_service(svc, false);
1442 * Flush all the virtual services
1444 static int ip_vs_flush(struct net *net, bool cleanup)
1447 struct ip_vs_service *svc;
1448 struct hlist_node *n;
1451 * Flush the service table hashed by <netns,protocol,addr,port>
1453 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1454 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1456 if (net_eq(svc->net, net))
1457 ip_vs_unlink_service(svc, cleanup);
1462 * Flush the service table hashed by fwmark
1464 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1465 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1467 if (net_eq(svc->net, net))
1468 ip_vs_unlink_service(svc, cleanup);
1476 * Delete service by {netns} in the service table.
1477 * Called by __ip_vs_cleanup()
1479 void ip_vs_service_net_cleanup(struct net *net)
1482 /* Check for "full" addressed entries */
1483 mutex_lock(&__ip_vs_mutex);
1484 ip_vs_flush(net, true);
1485 mutex_unlock(&__ip_vs_mutex);
1489 /* Put all references for device (dst_cache) */
1491 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1493 struct ip_vs_dest_dst *dest_dst;
1495 spin_lock_bh(&dest->dst_lock);
1496 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1497 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1498 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1500 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1502 atomic_read(&dest->refcnt));
1503 __ip_vs_dst_cache_reset(dest);
1505 spin_unlock_bh(&dest->dst_lock);
1508 /* Netdev event receiver
1509 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1511 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1514 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1515 struct net *net = dev_net(dev);
1516 struct netns_ipvs *ipvs = net_ipvs(net);
1517 struct ip_vs_service *svc;
1518 struct ip_vs_dest *dest;
1521 if (event != NETDEV_DOWN || !ipvs)
1523 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1525 mutex_lock(&__ip_vs_mutex);
1526 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1527 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1528 if (net_eq(svc->net, net)) {
1529 list_for_each_entry(dest, &svc->destinations,
1531 ip_vs_forget_dev(dest, dev);
1536 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1537 if (net_eq(svc->net, net)) {
1538 list_for_each_entry(dest, &svc->destinations,
1540 ip_vs_forget_dev(dest, dev);
1547 spin_lock_bh(&ipvs->dest_trash_lock);
1548 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1549 ip_vs_forget_dev(dest, dev);
1551 spin_unlock_bh(&ipvs->dest_trash_lock);
1552 mutex_unlock(&__ip_vs_mutex);
1558 * Zero counters in a service or all services
1560 static int ip_vs_zero_service(struct ip_vs_service *svc)
1562 struct ip_vs_dest *dest;
1564 list_for_each_entry(dest, &svc->destinations, n_list) {
1565 ip_vs_zero_stats(&dest->stats);
1567 ip_vs_zero_stats(&svc->stats);
1571 static int ip_vs_zero_all(struct net *net)
1574 struct ip_vs_service *svc;
1576 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1577 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1578 if (net_eq(svc->net, net))
1579 ip_vs_zero_service(svc);
1583 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1584 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1585 if (net_eq(svc->net, net))
1586 ip_vs_zero_service(svc);
1590 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
1594 #ifdef CONFIG_SYSCTL
1597 static int three = 3;
1600 proc_do_defense_mode(struct ctl_table *table, int write,
1601 void __user *buffer, size_t *lenp, loff_t *ppos)
1603 struct net *net = current->nsproxy->net_ns;
1604 int *valp = table->data;
1608 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1609 if (write && (*valp != val)) {
1610 if ((*valp < 0) || (*valp > 3)) {
1611 /* Restore the correct value */
1614 update_defense_level(net_ipvs(net));
1621 proc_do_sync_threshold(struct ctl_table *table, int write,
1622 void __user *buffer, size_t *lenp, loff_t *ppos)
1624 int *valp = table->data;
1628 /* backup the value first */
1629 memcpy(val, valp, sizeof(val));
1631 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1632 if (write && (valp[0] < 0 || valp[1] < 0 ||
1633 (valp[0] >= valp[1] && valp[1]))) {
1634 /* Restore the correct value */
1635 memcpy(valp, val, sizeof(val));
1641 proc_do_sync_mode(struct ctl_table *table, int write,
1642 void __user *buffer, size_t *lenp, loff_t *ppos)
1644 int *valp = table->data;
1648 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1649 if (write && (*valp != val)) {
1650 if ((*valp < 0) || (*valp > 1)) {
1651 /* Restore the correct value */
1659 proc_do_sync_ports(struct ctl_table *table, int write,
1660 void __user *buffer, size_t *lenp, loff_t *ppos)
1662 int *valp = table->data;
1666 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1667 if (write && (*valp != val)) {
1668 if (*valp < 1 || !is_power_of_2(*valp)) {
1669 /* Restore the correct value */
1677 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1678 * Do not change order or insert new entries without
1679 * align with netns init in ip_vs_control_net_init()
1682 static struct ctl_table vs_vars[] = {
1684 .procname = "amemthresh",
1685 .maxlen = sizeof(int),
1687 .proc_handler = proc_dointvec,
1690 .procname = "am_droprate",
1691 .maxlen = sizeof(int),
1693 .proc_handler = proc_dointvec,
1696 .procname = "drop_entry",
1697 .maxlen = sizeof(int),
1699 .proc_handler = proc_do_defense_mode,
1702 .procname = "drop_packet",
1703 .maxlen = sizeof(int),
1705 .proc_handler = proc_do_defense_mode,
1707 #ifdef CONFIG_IP_VS_NFCT
1709 .procname = "conntrack",
1710 .maxlen = sizeof(int),
1712 .proc_handler = &proc_dointvec,
1716 .procname = "secure_tcp",
1717 .maxlen = sizeof(int),
1719 .proc_handler = proc_do_defense_mode,
1722 .procname = "snat_reroute",
1723 .maxlen = sizeof(int),
1725 .proc_handler = &proc_dointvec,
1728 .procname = "sync_version",
1729 .maxlen = sizeof(int),
1731 .proc_handler = &proc_do_sync_mode,
1734 .procname = "sync_ports",
1735 .maxlen = sizeof(int),
1737 .proc_handler = &proc_do_sync_ports,
1740 .procname = "sync_persist_mode",
1741 .maxlen = sizeof(int),
1743 .proc_handler = proc_dointvec,
1746 .procname = "sync_qlen_max",
1747 .maxlen = sizeof(unsigned long),
1749 .proc_handler = proc_doulongvec_minmax,
1752 .procname = "sync_sock_size",
1753 .maxlen = sizeof(int),
1755 .proc_handler = proc_dointvec,
1758 .procname = "cache_bypass",
1759 .maxlen = sizeof(int),
1761 .proc_handler = proc_dointvec,
1764 .procname = "expire_nodest_conn",
1765 .maxlen = sizeof(int),
1767 .proc_handler = proc_dointvec,
1770 .procname = "sloppy_tcp",
1771 .maxlen = sizeof(int),
1773 .proc_handler = proc_dointvec,
1776 .procname = "sloppy_sctp",
1777 .maxlen = sizeof(int),
1779 .proc_handler = proc_dointvec,
1782 .procname = "expire_quiescent_template",
1783 .maxlen = sizeof(int),
1785 .proc_handler = proc_dointvec,
1788 .procname = "sync_threshold",
1790 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1792 .proc_handler = proc_do_sync_threshold,
1795 .procname = "sync_refresh_period",
1796 .maxlen = sizeof(int),
1798 .proc_handler = proc_dointvec_jiffies,
1801 .procname = "sync_retries",
1802 .maxlen = sizeof(int),
1804 .proc_handler = proc_dointvec_minmax,
1809 .procname = "nat_icmp_send",
1810 .maxlen = sizeof(int),
1812 .proc_handler = proc_dointvec,
1815 .procname = "pmtu_disc",
1816 .maxlen = sizeof(int),
1818 .proc_handler = proc_dointvec,
1821 .procname = "backup_only",
1822 .maxlen = sizeof(int),
1824 .proc_handler = proc_dointvec,
1826 #ifdef CONFIG_IP_VS_DEBUG
1828 .procname = "debug_level",
1829 .data = &sysctl_ip_vs_debug_level,
1830 .maxlen = sizeof(int),
1832 .proc_handler = proc_dointvec,
1840 #ifdef CONFIG_PROC_FS
1843 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1844 struct hlist_head *table;
1849 * Write the contents of the VS rule table to a PROCfs file.
1850 * (It is kept just for backward compatibility)
1852 static inline const char *ip_vs_fwd_name(unsigned int flags)
1854 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1855 case IP_VS_CONN_F_LOCALNODE:
1857 case IP_VS_CONN_F_TUNNEL:
1859 case IP_VS_CONN_F_DROUTE:
1867 /* Get the Nth entry in the two lists */
1868 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1870 struct net *net = seq_file_net(seq);
1871 struct ip_vs_iter *iter = seq->private;
1873 struct ip_vs_service *svc;
1875 /* look in hash by protocol */
1876 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1877 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1878 if (net_eq(svc->net, net) && pos-- == 0) {
1879 iter->table = ip_vs_svc_table;
1886 /* keep looking in fwmark */
1887 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1888 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1890 if (net_eq(svc->net, net) && pos-- == 0) {
1891 iter->table = ip_vs_svc_fwm_table;
1901 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1905 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1909 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1911 struct hlist_node *e;
1912 struct ip_vs_iter *iter;
1913 struct ip_vs_service *svc;
1916 if (v == SEQ_START_TOKEN)
1917 return ip_vs_info_array(seq,0);
1920 iter = seq->private;
1922 if (iter->table == ip_vs_svc_table) {
1923 /* next service in table hashed by protocol */
1924 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1926 return hlist_entry(e, struct ip_vs_service, s_list);
1928 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1929 hlist_for_each_entry_rcu(svc,
1930 &ip_vs_svc_table[iter->bucket],
1936 iter->table = ip_vs_svc_fwm_table;
1941 /* next service in hashed by fwmark */
1942 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
1944 return hlist_entry(e, struct ip_vs_service, f_list);
1947 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1948 hlist_for_each_entry_rcu(svc,
1949 &ip_vs_svc_fwm_table[iter->bucket],
1957 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1964 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1966 if (v == SEQ_START_TOKEN) {
1968 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1969 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1971 "Prot LocalAddress:Port Scheduler Flags\n");
1973 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1975 const struct ip_vs_service *svc = v;
1976 const struct ip_vs_iter *iter = seq->private;
1977 const struct ip_vs_dest *dest;
1978 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
1980 if (iter->table == ip_vs_svc_table) {
1981 #ifdef CONFIG_IP_VS_IPV6
1982 if (svc->af == AF_INET6)
1983 seq_printf(seq, "%s [%pI6]:%04X %s ",
1984 ip_vs_proto_name(svc->protocol),
1990 seq_printf(seq, "%s %08X:%04X %s %s ",
1991 ip_vs_proto_name(svc->protocol),
1992 ntohl(svc->addr.ip),
1995 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1997 seq_printf(seq, "FWM %08X %s %s",
1998 svc->fwmark, sched->name,
1999 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2002 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2003 seq_printf(seq, "persistent %d %08X\n",
2005 ntohl(svc->netmask));
2007 seq_putc(seq, '\n');
2009 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2010 #ifdef CONFIG_IP_VS_IPV6
2011 if (dest->af == AF_INET6)
2014 " %-7s %-6d %-10d %-10d\n",
2017 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2018 atomic_read(&dest->weight),
2019 atomic_read(&dest->activeconns),
2020 atomic_read(&dest->inactconns));
2025 "%-7s %-6d %-10d %-10d\n",
2026 ntohl(dest->addr.ip),
2028 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2029 atomic_read(&dest->weight),
2030 atomic_read(&dest->activeconns),
2031 atomic_read(&dest->inactconns));
2038 static const struct seq_operations ip_vs_info_seq_ops = {
2039 .start = ip_vs_info_seq_start,
2040 .next = ip_vs_info_seq_next,
2041 .stop = ip_vs_info_seq_stop,
2042 .show = ip_vs_info_seq_show,
2045 static int ip_vs_info_open(struct inode *inode, struct file *file)
2047 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2048 sizeof(struct ip_vs_iter));
2051 static const struct file_operations ip_vs_info_fops = {
2052 .owner = THIS_MODULE,
2053 .open = ip_vs_info_open,
2055 .llseek = seq_lseek,
2056 .release = seq_release_net,
2059 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2061 struct net *net = seq_file_single_net(seq);
2062 struct ip_vs_kstats show;
2064 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2066 " Total Incoming Outgoing Incoming Outgoing\n");
2068 " Conns Packets Packets Bytes Bytes\n");
2070 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2071 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2072 (unsigned long long)show.conns,
2073 (unsigned long long)show.inpkts,
2074 (unsigned long long)show.outpkts,
2075 (unsigned long long)show.inbytes,
2076 (unsigned long long)show.outbytes);
2078 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2080 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2081 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2082 (unsigned long long)show.cps,
2083 (unsigned long long)show.inpps,
2084 (unsigned long long)show.outpps,
2085 (unsigned long long)show.inbps,
2086 (unsigned long long)show.outbps);
2091 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2093 return single_open_net(inode, file, ip_vs_stats_show);
2096 static const struct file_operations ip_vs_stats_fops = {
2097 .owner = THIS_MODULE,
2098 .open = ip_vs_stats_seq_open,
2100 .llseek = seq_lseek,
2101 .release = single_release_net,
2104 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2106 struct net *net = seq_file_single_net(seq);
2107 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2108 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2109 struct ip_vs_kstats kstats;
2112 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2114 " Total Incoming Outgoing Incoming Outgoing\n");
2116 "CPU Conns Packets Packets Bytes Bytes\n");
2118 for_each_possible_cpu(i) {
2119 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2121 u64 conns, inpkts, outpkts, inbytes, outbytes;
2124 start = u64_stats_fetch_begin_irq(&u->syncp);
2125 conns = u->cnt.conns;
2126 inpkts = u->cnt.inpkts;
2127 outpkts = u->cnt.outpkts;
2128 inbytes = u->cnt.inbytes;
2129 outbytes = u->cnt.outbytes;
2130 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2132 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2133 i, (u64)conns, (u64)inpkts,
2134 (u64)outpkts, (u64)inbytes,
2138 ip_vs_copy_stats(&kstats, tot_stats);
2140 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2141 (unsigned long long)kstats.conns,
2142 (unsigned long long)kstats.inpkts,
2143 (unsigned long long)kstats.outpkts,
2144 (unsigned long long)kstats.inbytes,
2145 (unsigned long long)kstats.outbytes);
2147 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2149 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2150 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2160 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2162 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2165 static const struct file_operations ip_vs_stats_percpu_fops = {
2166 .owner = THIS_MODULE,
2167 .open = ip_vs_stats_percpu_seq_open,
2169 .llseek = seq_lseek,
2170 .release = single_release_net,
2175 * Set timeout values for tcp tcpfin udp in the timeout_table.
2177 static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
2179 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2180 struct ip_vs_proto_data *pd;
2183 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2188 #ifdef CONFIG_IP_VS_PROTO_TCP
2189 if (u->tcp_timeout) {
2190 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2191 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2192 = u->tcp_timeout * HZ;
2195 if (u->tcp_fin_timeout) {
2196 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2197 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2198 = u->tcp_fin_timeout * HZ;
2202 #ifdef CONFIG_IP_VS_PROTO_UDP
2203 if (u->udp_timeout) {
2204 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2205 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2206 = u->udp_timeout * HZ;
2212 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2214 struct ip_vs_svcdest_user {
2215 struct ip_vs_service_user s;
2216 struct ip_vs_dest_user d;
2219 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2220 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2221 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2222 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2223 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2224 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2225 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2226 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2227 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2228 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2229 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2232 union ip_vs_set_arglen {
2233 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2234 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2235 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2236 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2237 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2238 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2239 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2240 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2241 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2242 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2245 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2247 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2248 struct ip_vs_service_user *usvc_compat)
2250 memset(usvc, 0, sizeof(*usvc));
2253 usvc->protocol = usvc_compat->protocol;
2254 usvc->addr.ip = usvc_compat->addr;
2255 usvc->port = usvc_compat->port;
2256 usvc->fwmark = usvc_compat->fwmark;
2258 /* Deep copy of sched_name is not needed here */
2259 usvc->sched_name = usvc_compat->sched_name;
2261 usvc->flags = usvc_compat->flags;
2262 usvc->timeout = usvc_compat->timeout;
2263 usvc->netmask = usvc_compat->netmask;
2266 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2267 struct ip_vs_dest_user *udest_compat)
2269 memset(udest, 0, sizeof(*udest));
2271 udest->addr.ip = udest_compat->addr;
2272 udest->port = udest_compat->port;
2273 udest->conn_flags = udest_compat->conn_flags;
2274 udest->weight = udest_compat->weight;
2275 udest->u_threshold = udest_compat->u_threshold;
2276 udest->l_threshold = udest_compat->l_threshold;
2277 udest->af = AF_INET;
2281 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2283 struct net *net = sock_net(sk);
2285 unsigned char arg[MAX_SET_ARGLEN];
2286 struct ip_vs_service_user *usvc_compat;
2287 struct ip_vs_service_user_kern usvc;
2288 struct ip_vs_service *svc;
2289 struct ip_vs_dest_user *udest_compat;
2290 struct ip_vs_dest_user_kern udest;
2291 struct netns_ipvs *ipvs = net_ipvs(net);
2293 BUILD_BUG_ON(sizeof(arg) > 255);
2294 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2297 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2299 if (len != set_arglen[CMDID(cmd)]) {
2300 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2301 len, set_arglen[CMDID(cmd)]);
2305 if (copy_from_user(arg, user, len) != 0)
2308 /* increase the module use count */
2309 ip_vs_use_count_inc();
2311 /* Handle daemons since they have another lock */
2312 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2313 cmd == IP_VS_SO_SET_STOPDAEMON) {
2314 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2316 mutex_lock(&ipvs->sync_mutex);
2317 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2318 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2321 ret = stop_sync_thread(net, dm->state);
2322 mutex_unlock(&ipvs->sync_mutex);
2326 mutex_lock(&__ip_vs_mutex);
2327 if (cmd == IP_VS_SO_SET_FLUSH) {
2328 /* Flush the virtual service */
2329 ret = ip_vs_flush(net, false);
2331 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2332 /* Set timeout values for (tcp tcpfin udp) */
2333 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
2337 usvc_compat = (struct ip_vs_service_user *)arg;
2338 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2340 /* We only use the new structs internally, so copy userspace compat
2341 * structs to extended internal versions */
2342 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2343 ip_vs_copy_udest_compat(&udest, udest_compat);
2345 if (cmd == IP_VS_SO_SET_ZERO) {
2346 /* if no service address is set, zero counters in all */
2347 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2348 ret = ip_vs_zero_all(net);
2353 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2354 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2355 usvc.protocol != IPPROTO_SCTP) {
2356 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2357 usvc.protocol, &usvc.addr.ip,
2358 ntohs(usvc.port), usvc.sched_name);
2363 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2365 if (usvc.fwmark == 0)
2366 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2367 &usvc.addr, usvc.port);
2369 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
2372 if (cmd != IP_VS_SO_SET_ADD
2373 && (svc == NULL || svc->protocol != usvc.protocol)) {
2379 case IP_VS_SO_SET_ADD:
2383 ret = ip_vs_add_service(net, &usvc, &svc);
2385 case IP_VS_SO_SET_EDIT:
2386 ret = ip_vs_edit_service(svc, &usvc);
2388 case IP_VS_SO_SET_DEL:
2389 ret = ip_vs_del_service(svc);
2393 case IP_VS_SO_SET_ZERO:
2394 ret = ip_vs_zero_service(svc);
2396 case IP_VS_SO_SET_ADDDEST:
2397 ret = ip_vs_add_dest(svc, &udest);
2399 case IP_VS_SO_SET_EDITDEST:
2400 ret = ip_vs_edit_dest(svc, &udest);
2402 case IP_VS_SO_SET_DELDEST:
2403 ret = ip_vs_del_dest(svc, &udest);
2410 mutex_unlock(&__ip_vs_mutex);
2412 /* decrease the module use count */
2413 ip_vs_use_count_dec();
2420 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2422 struct ip_vs_scheduler *sched;
2423 struct ip_vs_kstats kstats;
2425 sched = rcu_dereference_protected(src->scheduler, 1);
2426 dst->protocol = src->protocol;
2427 dst->addr = src->addr.ip;
2428 dst->port = src->port;
2429 dst->fwmark = src->fwmark;
2430 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
2431 dst->flags = src->flags;
2432 dst->timeout = src->timeout / HZ;
2433 dst->netmask = src->netmask;
2434 dst->num_dests = src->num_dests;
2435 ip_vs_copy_stats(&kstats, &src->stats);
2436 ip_vs_export_stats_user(&dst->stats, &kstats);
2440 __ip_vs_get_service_entries(struct net *net,
2441 const struct ip_vs_get_services *get,
2442 struct ip_vs_get_services __user *uptr)
2445 struct ip_vs_service *svc;
2446 struct ip_vs_service_entry entry;
2449 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2450 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2451 /* Only expose IPv4 entries to old interface */
2452 if (svc->af != AF_INET || !net_eq(svc->net, net))
2455 if (count >= get->num_services)
2457 memset(&entry, 0, sizeof(entry));
2458 ip_vs_copy_service(&entry, svc);
2459 if (copy_to_user(&uptr->entrytable[count],
2460 &entry, sizeof(entry))) {
2468 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2469 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2470 /* Only expose IPv4 entries to old interface */
2471 if (svc->af != AF_INET || !net_eq(svc->net, net))
2474 if (count >= get->num_services)
2476 memset(&entry, 0, sizeof(entry));
2477 ip_vs_copy_service(&entry, svc);
2478 if (copy_to_user(&uptr->entrytable[count],
2479 &entry, sizeof(entry))) {
2491 __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2492 struct ip_vs_get_dests __user *uptr)
2494 struct ip_vs_service *svc;
2495 union nf_inet_addr addr = { .ip = get->addr };
2500 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
2502 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2508 struct ip_vs_dest *dest;
2509 struct ip_vs_dest_entry entry;
2510 struct ip_vs_kstats kstats;
2512 memset(&entry, 0, sizeof(entry));
2513 list_for_each_entry(dest, &svc->destinations, n_list) {
2514 if (count >= get->num_dests)
2517 /* Cannot expose heterogeneous members via sockopt
2520 if (dest->af != svc->af)
2523 entry.addr = dest->addr.ip;
2524 entry.port = dest->port;
2525 entry.conn_flags = atomic_read(&dest->conn_flags);
2526 entry.weight = atomic_read(&dest->weight);
2527 entry.u_threshold = dest->u_threshold;
2528 entry.l_threshold = dest->l_threshold;
2529 entry.activeconns = atomic_read(&dest->activeconns);
2530 entry.inactconns = atomic_read(&dest->inactconns);
2531 entry.persistconns = atomic_read(&dest->persistconns);
2532 ip_vs_copy_stats(&kstats, &dest->stats);
2533 ip_vs_export_stats_user(&entry.stats, &kstats);
2534 if (copy_to_user(&uptr->entrytable[count],
2535 &entry, sizeof(entry))) {
2547 __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
2549 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2550 struct ip_vs_proto_data *pd;
2553 memset(u, 0, sizeof (*u));
2555 #ifdef CONFIG_IP_VS_PROTO_TCP
2556 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2557 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2558 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2560 #ifdef CONFIG_IP_VS_PROTO_UDP
2561 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2563 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2567 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2568 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2569 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2570 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2571 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2572 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2573 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2574 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2577 union ip_vs_get_arglen {
2578 char field_IP_VS_SO_GET_VERSION[64];
2579 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2580 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2581 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2582 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2583 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2584 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2587 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2590 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2592 unsigned char arg[MAX_GET_ARGLEN];
2594 unsigned int copylen;
2595 struct net *net = sock_net(sk);
2596 struct netns_ipvs *ipvs = net_ipvs(net);
2599 BUILD_BUG_ON(sizeof(arg) > 255);
2600 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2603 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2606 copylen = get_arglen[CMDID(cmd)];
2607 if (*len < (int) copylen) {
2608 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2612 if (copy_from_user(arg, user, copylen) != 0)
2615 * Handle daemons first since it has its own locking
2617 if (cmd == IP_VS_SO_GET_DAEMON) {
2618 struct ip_vs_daemon_user d[2];
2620 memset(&d, 0, sizeof(d));
2621 mutex_lock(&ipvs->sync_mutex);
2622 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2623 d[0].state = IP_VS_STATE_MASTER;
2624 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2625 sizeof(d[0].mcast_ifn));
2626 d[0].syncid = ipvs->master_syncid;
2628 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2629 d[1].state = IP_VS_STATE_BACKUP;
2630 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2631 sizeof(d[1].mcast_ifn));
2632 d[1].syncid = ipvs->backup_syncid;
2634 if (copy_to_user(user, &d, sizeof(d)) != 0)
2636 mutex_unlock(&ipvs->sync_mutex);
2640 mutex_lock(&__ip_vs_mutex);
2642 case IP_VS_SO_GET_VERSION:
2646 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2647 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2648 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2652 *len = strlen(buf)+1;
2656 case IP_VS_SO_GET_INFO:
2658 struct ip_vs_getinfo info;
2659 info.version = IP_VS_VERSION_CODE;
2660 info.size = ip_vs_conn_tab_size;
2661 info.num_services = ipvs->num_services;
2662 if (copy_to_user(user, &info, sizeof(info)) != 0)
2667 case IP_VS_SO_GET_SERVICES:
2669 struct ip_vs_get_services *get;
2672 get = (struct ip_vs_get_services *)arg;
2673 size = sizeof(*get) +
2674 sizeof(struct ip_vs_service_entry) * get->num_services;
2676 pr_err("length: %u != %u\n", *len, size);
2680 ret = __ip_vs_get_service_entries(net, get, user);
2684 case IP_VS_SO_GET_SERVICE:
2686 struct ip_vs_service_entry *entry;
2687 struct ip_vs_service *svc;
2688 union nf_inet_addr addr;
2690 entry = (struct ip_vs_service_entry *)arg;
2691 addr.ip = entry->addr;
2694 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
2696 svc = __ip_vs_service_find(net, AF_INET,
2697 entry->protocol, &addr,
2701 ip_vs_copy_service(entry, svc);
2702 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2709 case IP_VS_SO_GET_DESTS:
2711 struct ip_vs_get_dests *get;
2714 get = (struct ip_vs_get_dests *)arg;
2715 size = sizeof(*get) +
2716 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2718 pr_err("length: %u != %u\n", *len, size);
2722 ret = __ip_vs_get_dest_entries(net, get, user);
2726 case IP_VS_SO_GET_TIMEOUT:
2728 struct ip_vs_timeout_user t;
2730 __ip_vs_get_timeouts(net, &t);
2731 if (copy_to_user(user, &t, sizeof(t)) != 0)
2741 mutex_unlock(&__ip_vs_mutex);
2746 static struct nf_sockopt_ops ip_vs_sockopts = {
2748 .set_optmin = IP_VS_BASE_CTL,
2749 .set_optmax = IP_VS_SO_SET_MAX+1,
2750 .set = do_ip_vs_set_ctl,
2751 .get_optmin = IP_VS_BASE_CTL,
2752 .get_optmax = IP_VS_SO_GET_MAX+1,
2753 .get = do_ip_vs_get_ctl,
2754 .owner = THIS_MODULE,
2758 * Generic Netlink interface
2761 /* IPVS genetlink family */
2762 static struct genl_family ip_vs_genl_family = {
2763 .id = GENL_ID_GENERATE,
2765 .name = IPVS_GENL_NAME,
2766 .version = IPVS_GENL_VERSION,
2767 .maxattr = IPVS_CMD_MAX,
2768 .netnsok = true, /* Make ipvsadm to work on netns */
2771 /* Policy used for first-level command attributes */
2772 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2773 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2774 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2775 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2776 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2777 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2778 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2781 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2782 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2783 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2784 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2785 .len = IP_VS_IFNAME_MAXLEN },
2786 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2789 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2790 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2791 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2792 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2793 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2794 .len = sizeof(union nf_inet_addr) },
2795 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2796 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2797 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2798 .len = IP_VS_SCHEDNAME_MAXLEN },
2799 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2800 .len = IP_VS_PENAME_MAXLEN },
2801 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2802 .len = sizeof(struct ip_vs_flags) },
2803 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2804 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2805 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2808 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2809 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2810 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2811 .len = sizeof(union nf_inet_addr) },
2812 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2813 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2814 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2815 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2816 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2817 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2818 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2819 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2820 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2821 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2824 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2825 struct ip_vs_kstats *kstats)
2827 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2832 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2833 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2834 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2835 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2836 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2837 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2838 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2839 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2840 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2841 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2842 goto nla_put_failure;
2843 nla_nest_end(skb, nl_stats);
2848 nla_nest_cancel(skb, nl_stats);
2852 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2853 struct ip_vs_kstats *kstats)
2855 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2860 if (nla_put_u64(skb, IPVS_STATS_ATTR_CONNS, kstats->conns) ||
2861 nla_put_u64(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts) ||
2862 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts) ||
2863 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2864 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2865 nla_put_u64(skb, IPVS_STATS_ATTR_CPS, kstats->cps) ||
2866 nla_put_u64(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps) ||
2867 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps) ||
2868 nla_put_u64(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps) ||
2869 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps))
2870 goto nla_put_failure;
2871 nla_nest_end(skb, nl_stats);
2876 nla_nest_cancel(skb, nl_stats);
2880 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2881 struct ip_vs_service *svc)
2883 struct ip_vs_scheduler *sched;
2884 struct ip_vs_pe *pe;
2885 struct nlattr *nl_service;
2886 struct ip_vs_flags flags = { .flags = svc->flags,
2888 struct ip_vs_kstats kstats;
2890 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2894 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2895 goto nla_put_failure;
2897 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2898 goto nla_put_failure;
2900 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2901 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2902 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2903 goto nla_put_failure;
2906 sched = rcu_dereference_protected(svc->scheduler, 1);
2907 pe = rcu_dereference_protected(svc->pe, 1);
2908 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
2909 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2910 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2911 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2912 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2913 goto nla_put_failure;
2914 ip_vs_copy_stats(&kstats, &svc->stats);
2915 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
2916 goto nla_put_failure;
2917 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
2918 goto nla_put_failure;
2920 nla_nest_end(skb, nl_service);
2925 nla_nest_cancel(skb, nl_service);
2929 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2930 struct ip_vs_service *svc,
2931 struct netlink_callback *cb)
2935 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2936 &ip_vs_genl_family, NLM_F_MULTI,
2937 IPVS_CMD_NEW_SERVICE);
2941 if (ip_vs_genl_fill_service(skb, svc) < 0)
2942 goto nla_put_failure;
2944 genlmsg_end(skb, hdr);
2948 genlmsg_cancel(skb, hdr);
2952 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2953 struct netlink_callback *cb)
2956 int start = cb->args[0];
2957 struct ip_vs_service *svc;
2958 struct net *net = skb_sknet(skb);
2960 mutex_lock(&__ip_vs_mutex);
2961 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2962 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2963 if (++idx <= start || !net_eq(svc->net, net))
2965 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2967 goto nla_put_failure;
2972 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2973 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2974 if (++idx <= start || !net_eq(svc->net, net))
2976 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2978 goto nla_put_failure;
2984 mutex_unlock(&__ip_vs_mutex);
2990 static int ip_vs_genl_parse_service(struct net *net,
2991 struct ip_vs_service_user_kern *usvc,
2992 struct nlattr *nla, int full_entry,
2993 struct ip_vs_service **ret_svc)
2995 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2996 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2997 struct ip_vs_service *svc;
2999 /* Parse mandatory identifying service fields first */
3001 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
3004 nla_af = attrs[IPVS_SVC_ATTR_AF];
3005 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3006 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3007 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3008 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3010 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3013 memset(usvc, 0, sizeof(*usvc));
3015 usvc->af = nla_get_u16(nla_af);
3016 #ifdef CONFIG_IP_VS_IPV6
3017 if (usvc->af != AF_INET && usvc->af != AF_INET6)
3019 if (usvc->af != AF_INET)
3021 return -EAFNOSUPPORT;
3024 usvc->protocol = IPPROTO_TCP;
3025 usvc->fwmark = nla_get_u32(nla_fwmark);
3027 usvc->protocol = nla_get_u16(nla_protocol);
3028 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3029 usvc->port = nla_get_be16(nla_port);
3035 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
3037 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
3038 &usvc->addr, usvc->port);
3042 /* If a full entry was requested, check for the additional fields */
3044 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3046 struct ip_vs_flags flags;
3048 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3049 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3050 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3051 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3052 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3054 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3057 nla_memcpy(&flags, nla_flags, sizeof(flags));
3059 /* prefill flags from service if it already exists */
3061 usvc->flags = svc->flags;
3063 /* set new flags from userland */
3064 usvc->flags = (usvc->flags & ~flags.mask) |
3065 (flags.flags & flags.mask);
3066 usvc->sched_name = nla_data(nla_sched);
3067 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3068 usvc->timeout = nla_get_u32(nla_timeout);
3069 usvc->netmask = nla_get_be32(nla_netmask);
3075 static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3078 struct ip_vs_service_user_kern usvc;
3079 struct ip_vs_service *svc;
3082 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3083 return ret ? ERR_PTR(ret) : svc;
3086 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3088 struct nlattr *nl_dest;
3089 struct ip_vs_kstats kstats;
3091 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3095 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3096 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3097 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3098 (atomic_read(&dest->conn_flags) &
3099 IP_VS_CONN_F_FWD_MASK)) ||
3100 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3101 atomic_read(&dest->weight)) ||
3102 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3103 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3104 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3105 atomic_read(&dest->activeconns)) ||
3106 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3107 atomic_read(&dest->inactconns)) ||
3108 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3109 atomic_read(&dest->persistconns)) ||
3110 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3111 goto nla_put_failure;
3112 ip_vs_copy_stats(&kstats, &dest->stats);
3113 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3114 goto nla_put_failure;
3115 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3116 goto nla_put_failure;
3118 nla_nest_end(skb, nl_dest);
3123 nla_nest_cancel(skb, nl_dest);
3127 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3128 struct netlink_callback *cb)
3132 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3133 &ip_vs_genl_family, NLM_F_MULTI,
3138 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3139 goto nla_put_failure;
3141 genlmsg_end(skb, hdr);
3145 genlmsg_cancel(skb, hdr);
3149 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3150 struct netlink_callback *cb)
3153 int start = cb->args[0];
3154 struct ip_vs_service *svc;
3155 struct ip_vs_dest *dest;
3156 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3157 struct net *net = skb_sknet(skb);
3159 mutex_lock(&__ip_vs_mutex);
3161 /* Try to find the service for which to dump destinations */
3162 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3163 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3167 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3168 if (IS_ERR(svc) || svc == NULL)
3171 /* Dump the destinations */
3172 list_for_each_entry(dest, &svc->destinations, n_list) {
3175 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3177 goto nla_put_failure;
3185 mutex_unlock(&__ip_vs_mutex);
3190 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3191 struct nlattr *nla, int full_entry)
3193 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3194 struct nlattr *nla_addr, *nla_port;
3195 struct nlattr *nla_addr_family;
3197 /* Parse mandatory identifying destination fields first */
3199 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3202 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3203 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3204 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3206 if (!(nla_addr && nla_port))
3209 memset(udest, 0, sizeof(*udest));
3211 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3212 udest->port = nla_get_be16(nla_port);
3214 if (nla_addr_family)
3215 udest->af = nla_get_u16(nla_addr_family);
3219 /* If a full entry was requested, check for the additional fields */
3221 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3224 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3225 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3226 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3227 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3229 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3232 udest->conn_flags = nla_get_u32(nla_fwd)
3233 & IP_VS_CONN_F_FWD_MASK;
3234 udest->weight = nla_get_u32(nla_weight);
3235 udest->u_threshold = nla_get_u32(nla_u_thresh);
3236 udest->l_threshold = nla_get_u32(nla_l_thresh);
3242 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3243 const char *mcast_ifn, __u32 syncid)
3245 struct nlattr *nl_daemon;
3247 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3251 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3252 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3253 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3254 goto nla_put_failure;
3255 nla_nest_end(skb, nl_daemon);
3260 nla_nest_cancel(skb, nl_daemon);
3264 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3265 const char *mcast_ifn, __u32 syncid,
3266 struct netlink_callback *cb)
3269 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3270 &ip_vs_genl_family, NLM_F_MULTI,
3271 IPVS_CMD_NEW_DAEMON);
3275 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3276 goto nla_put_failure;
3278 genlmsg_end(skb, hdr);
3282 genlmsg_cancel(skb, hdr);
3286 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3287 struct netlink_callback *cb)
3289 struct net *net = skb_sknet(skb);
3290 struct netns_ipvs *ipvs = net_ipvs(net);
3292 mutex_lock(&ipvs->sync_mutex);
3293 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3294 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3295 ipvs->master_mcast_ifn,
3296 ipvs->master_syncid, cb) < 0)
3297 goto nla_put_failure;
3302 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3303 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3304 ipvs->backup_mcast_ifn,
3305 ipvs->backup_syncid, cb) < 0)
3306 goto nla_put_failure;
3312 mutex_unlock(&ipvs->sync_mutex);
3317 static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3319 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3320 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3321 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3324 /* The synchronization protocol is incompatible with mixed family
3327 if (net_ipvs(net)->mixed_address_family_dests > 0)
3330 return start_sync_thread(net,
3331 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3332 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3333 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3336 static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3338 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3341 return stop_sync_thread(net,
3342 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3345 static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3347 struct ip_vs_timeout_user t;
3349 __ip_vs_get_timeouts(net, &t);
3351 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3352 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3354 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3356 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3358 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3359 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3361 return ip_vs_set_timeout(net, &t);
3364 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3368 struct netns_ipvs *ipvs;
3370 net = skb_sknet(skb);
3371 ipvs = net_ipvs(net);
3372 cmd = info->genlhdr->cmd;
3374 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3375 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3377 mutex_lock(&ipvs->sync_mutex);
3378 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3379 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3380 info->attrs[IPVS_CMD_ATTR_DAEMON],
3381 ip_vs_daemon_policy)) {
3386 if (cmd == IPVS_CMD_NEW_DAEMON)
3387 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3389 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3391 mutex_unlock(&ipvs->sync_mutex);
3396 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3398 struct ip_vs_service *svc = NULL;
3399 struct ip_vs_service_user_kern usvc;
3400 struct ip_vs_dest_user_kern udest;
3402 int need_full_svc = 0, need_full_dest = 0;
3405 net = skb_sknet(skb);
3406 cmd = info->genlhdr->cmd;
3408 mutex_lock(&__ip_vs_mutex);
3410 if (cmd == IPVS_CMD_FLUSH) {
3411 ret = ip_vs_flush(net, false);
3413 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3414 ret = ip_vs_genl_set_config(net, info->attrs);
3416 } else if (cmd == IPVS_CMD_ZERO &&
3417 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3418 ret = ip_vs_zero_all(net);
3422 /* All following commands require a service argument, so check if we
3423 * received a valid one. We need a full service specification when
3424 * adding / editing a service. Only identifying members otherwise. */
3425 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3428 ret = ip_vs_genl_parse_service(net, &usvc,
3429 info->attrs[IPVS_CMD_ATTR_SERVICE],
3430 need_full_svc, &svc);
3434 /* Unless we're adding a new service, the service must already exist */
3435 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3440 /* Destination commands require a valid destination argument. For
3441 * adding / editing a destination, we need a full destination
3443 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3444 cmd == IPVS_CMD_DEL_DEST) {
3445 if (cmd != IPVS_CMD_DEL_DEST)
3448 ret = ip_vs_genl_parse_dest(&udest,
3449 info->attrs[IPVS_CMD_ATTR_DEST],
3454 /* Old protocols did not allow the user to specify address
3455 * family, so we set it to zero instead. We also didn't
3456 * allow heterogeneous pools in the old code, so it's safe
3457 * to assume that this will have the same address family as
3463 if (udest.af != svc->af) {
3464 /* The synchronization protocol is incompatible
3465 * with mixed family services
3467 if (net_ipvs(net)->sync_state) {
3472 /* Which connection types do we support? */
3473 switch (udest.conn_flags) {
3474 case IP_VS_CONN_F_TUNNEL:
3475 /* We are able to forward this */
3485 case IPVS_CMD_NEW_SERVICE:
3487 ret = ip_vs_add_service(net, &usvc, &svc);
3491 case IPVS_CMD_SET_SERVICE:
3492 ret = ip_vs_edit_service(svc, &usvc);
3494 case IPVS_CMD_DEL_SERVICE:
3495 ret = ip_vs_del_service(svc);
3496 /* do not use svc, it can be freed */
3498 case IPVS_CMD_NEW_DEST:
3499 ret = ip_vs_add_dest(svc, &udest);
3501 case IPVS_CMD_SET_DEST:
3502 ret = ip_vs_edit_dest(svc, &udest);
3504 case IPVS_CMD_DEL_DEST:
3505 ret = ip_vs_del_dest(svc, &udest);
3508 ret = ip_vs_zero_service(svc);
3515 mutex_unlock(&__ip_vs_mutex);
3520 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3522 struct sk_buff *msg;
3524 int ret, cmd, reply_cmd;
3527 net = skb_sknet(skb);
3528 cmd = info->genlhdr->cmd;
3530 if (cmd == IPVS_CMD_GET_SERVICE)
3531 reply_cmd = IPVS_CMD_NEW_SERVICE;
3532 else if (cmd == IPVS_CMD_GET_INFO)
3533 reply_cmd = IPVS_CMD_SET_INFO;
3534 else if (cmd == IPVS_CMD_GET_CONFIG)
3535 reply_cmd = IPVS_CMD_SET_CONFIG;
3537 pr_err("unknown Generic Netlink command\n");
3541 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3545 mutex_lock(&__ip_vs_mutex);
3547 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3549 goto nla_put_failure;
3552 case IPVS_CMD_GET_SERVICE:
3554 struct ip_vs_service *svc;
3556 svc = ip_vs_genl_find_service(net,
3557 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3562 ret = ip_vs_genl_fill_service(msg, svc);
3564 goto nla_put_failure;
3573 case IPVS_CMD_GET_CONFIG:
3575 struct ip_vs_timeout_user t;
3577 __ip_vs_get_timeouts(net, &t);
3578 #ifdef CONFIG_IP_VS_PROTO_TCP
3579 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3581 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3583 goto nla_put_failure;
3585 #ifdef CONFIG_IP_VS_PROTO_UDP
3586 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3587 goto nla_put_failure;
3593 case IPVS_CMD_GET_INFO:
3594 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3595 IP_VS_VERSION_CODE) ||
3596 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3597 ip_vs_conn_tab_size))
3598 goto nla_put_failure;
3602 genlmsg_end(msg, reply);
3603 ret = genlmsg_reply(msg, info);
3607 pr_err("not enough space in Netlink message\n");
3613 mutex_unlock(&__ip_vs_mutex);
3619 static const struct genl_ops ip_vs_genl_ops[] = {
3621 .cmd = IPVS_CMD_NEW_SERVICE,
3622 .flags = GENL_ADMIN_PERM,
3623 .policy = ip_vs_cmd_policy,
3624 .doit = ip_vs_genl_set_cmd,
3627 .cmd = IPVS_CMD_SET_SERVICE,
3628 .flags = GENL_ADMIN_PERM,
3629 .policy = ip_vs_cmd_policy,
3630 .doit = ip_vs_genl_set_cmd,
3633 .cmd = IPVS_CMD_DEL_SERVICE,
3634 .flags = GENL_ADMIN_PERM,
3635 .policy = ip_vs_cmd_policy,
3636 .doit = ip_vs_genl_set_cmd,
3639 .cmd = IPVS_CMD_GET_SERVICE,
3640 .flags = GENL_ADMIN_PERM,
3641 .doit = ip_vs_genl_get_cmd,
3642 .dumpit = ip_vs_genl_dump_services,
3643 .policy = ip_vs_cmd_policy,
3646 .cmd = IPVS_CMD_NEW_DEST,
3647 .flags = GENL_ADMIN_PERM,
3648 .policy = ip_vs_cmd_policy,
3649 .doit = ip_vs_genl_set_cmd,
3652 .cmd = IPVS_CMD_SET_DEST,
3653 .flags = GENL_ADMIN_PERM,
3654 .policy = ip_vs_cmd_policy,
3655 .doit = ip_vs_genl_set_cmd,
3658 .cmd = IPVS_CMD_DEL_DEST,
3659 .flags = GENL_ADMIN_PERM,
3660 .policy = ip_vs_cmd_policy,
3661 .doit = ip_vs_genl_set_cmd,
3664 .cmd = IPVS_CMD_GET_DEST,
3665 .flags = GENL_ADMIN_PERM,
3666 .policy = ip_vs_cmd_policy,
3667 .dumpit = ip_vs_genl_dump_dests,
3670 .cmd = IPVS_CMD_NEW_DAEMON,
3671 .flags = GENL_ADMIN_PERM,
3672 .policy = ip_vs_cmd_policy,
3673 .doit = ip_vs_genl_set_daemon,
3676 .cmd = IPVS_CMD_DEL_DAEMON,
3677 .flags = GENL_ADMIN_PERM,
3678 .policy = ip_vs_cmd_policy,
3679 .doit = ip_vs_genl_set_daemon,
3682 .cmd = IPVS_CMD_GET_DAEMON,
3683 .flags = GENL_ADMIN_PERM,
3684 .dumpit = ip_vs_genl_dump_daemons,
3687 .cmd = IPVS_CMD_SET_CONFIG,
3688 .flags = GENL_ADMIN_PERM,
3689 .policy = ip_vs_cmd_policy,
3690 .doit = ip_vs_genl_set_cmd,
3693 .cmd = IPVS_CMD_GET_CONFIG,
3694 .flags = GENL_ADMIN_PERM,
3695 .doit = ip_vs_genl_get_cmd,
3698 .cmd = IPVS_CMD_GET_INFO,
3699 .flags = GENL_ADMIN_PERM,
3700 .doit = ip_vs_genl_get_cmd,
3703 .cmd = IPVS_CMD_ZERO,
3704 .flags = GENL_ADMIN_PERM,
3705 .policy = ip_vs_cmd_policy,
3706 .doit = ip_vs_genl_set_cmd,
3709 .cmd = IPVS_CMD_FLUSH,
3710 .flags = GENL_ADMIN_PERM,
3711 .doit = ip_vs_genl_set_cmd,
3715 static int __init ip_vs_genl_register(void)
3717 return genl_register_family_with_ops(&ip_vs_genl_family,
3721 static void ip_vs_genl_unregister(void)
3723 genl_unregister_family(&ip_vs_genl_family);
3726 /* End of Generic Netlink interface definitions */
3729 * per netns intit/exit func.
3731 #ifdef CONFIG_SYSCTL
3732 static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3735 struct netns_ipvs *ipvs = net_ipvs(net);
3736 struct ctl_table *tbl;
3738 atomic_set(&ipvs->dropentry, 0);
3739 spin_lock_init(&ipvs->dropentry_lock);
3740 spin_lock_init(&ipvs->droppacket_lock);
3741 spin_lock_init(&ipvs->securetcp_lock);
3743 if (!net_eq(net, &init_net)) {
3744 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3748 /* Don't export sysctls to unprivileged users */
3749 if (net->user_ns != &init_user_ns)
3750 tbl[0].procname = NULL;
3753 /* Initialize sysctl defaults */
3755 ipvs->sysctl_amemthresh = 1024;
3756 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3757 ipvs->sysctl_am_droprate = 10;
3758 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3759 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3760 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3761 #ifdef CONFIG_IP_VS_NFCT
3762 tbl[idx++].data = &ipvs->sysctl_conntrack;
3764 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3765 ipvs->sysctl_snat_reroute = 1;
3766 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3767 ipvs->sysctl_sync_ver = 1;
3768 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3769 ipvs->sysctl_sync_ports = 1;
3770 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3771 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3772 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3773 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3774 ipvs->sysctl_sync_sock_size = 0;
3775 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3776 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3777 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3778 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3779 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3780 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3781 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3782 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3783 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3784 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3785 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3786 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3787 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3788 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3789 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3790 ipvs->sysctl_pmtu_disc = 1;
3791 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3792 tbl[idx++].data = &ipvs->sysctl_backup_only;
3795 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3796 if (ipvs->sysctl_hdr == NULL) {
3797 if (!net_eq(net, &init_net))
3801 ip_vs_start_estimator(net, &ipvs->tot_stats);
3802 ipvs->sysctl_tbl = tbl;
3803 /* Schedule defense work */
3804 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3805 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3810 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
3812 struct netns_ipvs *ipvs = net_ipvs(net);
3814 cancel_delayed_work_sync(&ipvs->defense_work);
3815 cancel_work_sync(&ipvs->defense_work.work);
3816 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3817 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3822 static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3823 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3827 static struct notifier_block ip_vs_dst_notifier = {
3828 .notifier_call = ip_vs_dst_event,
3831 int __net_init ip_vs_control_net_init(struct net *net)
3834 struct netns_ipvs *ipvs = net_ipvs(net);
3836 /* Initialize rs_table */
3837 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3838 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3840 INIT_LIST_HEAD(&ipvs->dest_trash);
3841 spin_lock_init(&ipvs->dest_trash_lock);
3842 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3843 (unsigned long) net);
3844 atomic_set(&ipvs->ftpsvc_counter, 0);
3845 atomic_set(&ipvs->nullsvc_counter, 0);
3848 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3849 if (!ipvs->tot_stats.cpustats)
3852 for_each_possible_cpu(i) {
3853 struct ip_vs_cpu_stats *ipvs_tot_stats;
3854 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3855 u64_stats_init(&ipvs_tot_stats->syncp);
3858 spin_lock_init(&ipvs->tot_stats.lock);
3860 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3861 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3862 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3863 &ip_vs_stats_percpu_fops);
3865 if (ip_vs_control_net_init_sysctl(net))
3871 free_percpu(ipvs->tot_stats.cpustats);
3875 void __net_exit ip_vs_control_net_cleanup(struct net *net)
3877 struct netns_ipvs *ipvs = net_ipvs(net);
3879 ip_vs_trash_cleanup(net);
3880 ip_vs_control_net_cleanup_sysctl(net);
3881 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3882 remove_proc_entry("ip_vs_stats", net->proc_net);
3883 remove_proc_entry("ip_vs", net->proc_net);
3884 free_percpu(ipvs->tot_stats.cpustats);
3887 int __init ip_vs_register_nl_ioctl(void)
3891 ret = nf_register_sockopt(&ip_vs_sockopts);
3893 pr_err("cannot register sockopt.\n");
3897 ret = ip_vs_genl_register();
3899 pr_err("cannot register Generic Netlink interface.\n");
3905 nf_unregister_sockopt(&ip_vs_sockopts);
3910 void ip_vs_unregister_nl_ioctl(void)
3912 ip_vs_genl_unregister();
3913 nf_unregister_sockopt(&ip_vs_sockopts);
3916 int __init ip_vs_control_init(void)
3923 /* Initialize svc_table, ip_vs_svc_fwm_table */
3924 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3925 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3926 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3929 smp_wmb(); /* Do we really need it now ? */
3931 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3940 void ip_vs_control_cleanup(void)
3943 unregister_netdevice_notifier(&ip_vs_dst_notifier);