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);
846 ip_vs_start_estimator(svc->net, &dest->stats);
847 list_add_rcu(&dest->n_list, &svc->destinations);
849 sched = rcu_dereference_protected(svc->scheduler, 1);
850 if (sched && sched->add_dest)
851 sched->add_dest(svc, dest);
853 sched = rcu_dereference_protected(svc->scheduler, 1);
854 if (sched && sched->upd_dest)
855 sched->upd_dest(svc, dest);
861 * Create a destination for the given service
864 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
865 struct ip_vs_dest **dest_p)
867 struct ip_vs_dest *dest;
868 unsigned int atype, i;
872 #ifdef CONFIG_IP_VS_IPV6
873 if (udest->af == AF_INET6) {
874 atype = ipv6_addr_type(&udest->addr.in6);
875 if ((!(atype & IPV6_ADDR_UNICAST) ||
876 atype & IPV6_ADDR_LINKLOCAL) &&
877 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
882 atype = inet_addr_type(svc->net, udest->addr.ip);
883 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
887 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
891 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
892 if (!dest->stats.cpustats)
895 for_each_possible_cpu(i) {
896 struct ip_vs_cpu_stats *ip_vs_dest_stats;
897 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
898 u64_stats_init(&ip_vs_dest_stats->syncp);
901 dest->af = udest->af;
902 dest->protocol = svc->protocol;
903 dest->vaddr = svc->addr;
904 dest->vport = svc->port;
905 dest->vfwmark = svc->fwmark;
906 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
907 dest->port = udest->port;
909 atomic_set(&dest->activeconns, 0);
910 atomic_set(&dest->inactconns, 0);
911 atomic_set(&dest->persistconns, 0);
912 atomic_set(&dest->refcnt, 1);
914 INIT_HLIST_NODE(&dest->d_list);
915 spin_lock_init(&dest->dst_lock);
916 spin_lock_init(&dest->stats.lock);
917 __ip_vs_update_dest(svc, dest, udest, 1);
931 * Add a destination into an existing service
934 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
936 struct ip_vs_dest *dest;
937 union nf_inet_addr daddr;
938 __be16 dport = udest->port;
943 if (udest->weight < 0) {
944 pr_err("%s(): server weight less than zero\n", __func__);
948 if (udest->l_threshold > udest->u_threshold) {
949 pr_err("%s(): lower threshold is higher than upper threshold\n",
954 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
956 /* We use function that requires RCU lock */
958 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
962 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
967 * Check if the dest already exists in the trash and
968 * is from the same service
970 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
973 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
974 "dest->refcnt=%d, service %u/%s:%u\n",
975 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
976 atomic_read(&dest->refcnt),
978 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
981 __ip_vs_update_dest(svc, dest, udest, 1);
985 * Allocate and initialize the dest structure
987 ret = ip_vs_new_dest(svc, udest, &dest);
996 * Edit a destination in the given service
999 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1001 struct ip_vs_dest *dest;
1002 union nf_inet_addr daddr;
1003 __be16 dport = udest->port;
1007 if (udest->weight < 0) {
1008 pr_err("%s(): server weight less than zero\n", __func__);
1012 if (udest->l_threshold > udest->u_threshold) {
1013 pr_err("%s(): lower threshold is higher than upper threshold\n",
1018 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1020 /* We use function that requires RCU lock */
1022 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1026 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1030 __ip_vs_update_dest(svc, dest, udest, 0);
1037 * Delete a destination (must be already unlinked from the service)
1039 static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1042 struct netns_ipvs *ipvs = net_ipvs(net);
1044 ip_vs_stop_estimator(net, &dest->stats);
1047 * Remove it from the d-linked list with the real services.
1049 ip_vs_rs_unhash(dest);
1051 spin_lock_bh(&ipvs->dest_trash_lock);
1052 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1053 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1054 atomic_read(&dest->refcnt));
1055 if (list_empty(&ipvs->dest_trash) && !cleanup)
1056 mod_timer(&ipvs->dest_trash_timer,
1057 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1058 /* dest lives in trash without reference */
1059 list_add(&dest->t_list, &ipvs->dest_trash);
1060 dest->idle_start = 0;
1061 spin_unlock_bh(&ipvs->dest_trash_lock);
1062 ip_vs_dest_put(dest);
1067 * Unlink a destination from the given service
1069 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1070 struct ip_vs_dest *dest,
1073 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1076 * Remove it from the d-linked destination list.
1078 list_del_rcu(&dest->n_list);
1081 if (dest->af != svc->af)
1082 net_ipvs(svc->net)->mixed_address_family_dests--;
1085 struct ip_vs_scheduler *sched;
1087 sched = rcu_dereference_protected(svc->scheduler, 1);
1088 if (sched && sched->del_dest)
1089 sched->del_dest(svc, dest);
1095 * Delete a destination server in the given service
1098 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1100 struct ip_vs_dest *dest;
1101 __be16 dport = udest->port;
1105 /* We use function that requires RCU lock */
1107 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1111 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1116 * Unlink dest from the service
1118 __ip_vs_unlink_dest(svc, dest, 1);
1121 * Delete the destination
1123 __ip_vs_del_dest(svc->net, dest, false);
1130 static void ip_vs_dest_trash_expire(unsigned long data)
1132 struct net *net = (struct net *) data;
1133 struct netns_ipvs *ipvs = net_ipvs(net);
1134 struct ip_vs_dest *dest, *next;
1135 unsigned long now = jiffies;
1137 spin_lock(&ipvs->dest_trash_lock);
1138 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1139 if (atomic_read(&dest->refcnt) > 0)
1141 if (dest->idle_start) {
1142 if (time_before(now, dest->idle_start +
1143 IP_VS_DEST_TRASH_PERIOD))
1146 dest->idle_start = max(1UL, now);
1149 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1151 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1153 list_del(&dest->t_list);
1154 ip_vs_dest_free(dest);
1156 if (!list_empty(&ipvs->dest_trash))
1157 mod_timer(&ipvs->dest_trash_timer,
1158 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1159 spin_unlock(&ipvs->dest_trash_lock);
1163 * Add a service into the service hash table
1166 ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1167 struct ip_vs_service **svc_p)
1170 struct ip_vs_scheduler *sched = NULL;
1171 struct ip_vs_pe *pe = NULL;
1172 struct ip_vs_service *svc = NULL;
1173 struct netns_ipvs *ipvs = net_ipvs(net);
1175 /* increase the module use count */
1176 ip_vs_use_count_inc();
1178 /* Lookup the scheduler by 'u->sched_name' */
1179 if (strcmp(u->sched_name, "none")) {
1180 sched = ip_vs_scheduler_get(u->sched_name);
1182 pr_info("Scheduler module ip_vs_%s not found\n",
1189 if (u->pe_name && *u->pe_name) {
1190 pe = ip_vs_pe_getbyname(u->pe_name);
1192 pr_info("persistence engine module ip_vs_pe_%s "
1193 "not found\n", u->pe_name);
1199 #ifdef CONFIG_IP_VS_IPV6
1200 if (u->af == AF_INET6) {
1201 __u32 plen = (__force __u32) u->netmask;
1203 if (plen < 1 || plen > 128) {
1210 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1212 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1216 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1217 if (!svc->stats.cpustats) {
1222 for_each_possible_cpu(i) {
1223 struct ip_vs_cpu_stats *ip_vs_stats;
1224 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1225 u64_stats_init(&ip_vs_stats->syncp);
1229 /* I'm the first user of the service */
1230 atomic_set(&svc->refcnt, 0);
1233 svc->protocol = u->protocol;
1234 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1235 svc->port = u->port;
1236 svc->fwmark = u->fwmark;
1237 svc->flags = u->flags;
1238 svc->timeout = u->timeout * HZ;
1239 svc->netmask = u->netmask;
1242 INIT_LIST_HEAD(&svc->destinations);
1243 spin_lock_init(&svc->sched_lock);
1244 spin_lock_init(&svc->stats.lock);
1246 /* Bind the scheduler */
1248 ret = ip_vs_bind_scheduler(svc, sched);
1254 /* Bind the ct retriever */
1255 RCU_INIT_POINTER(svc->pe, pe);
1258 /* Update the virtual service counters */
1259 if (svc->port == FTPPORT)
1260 atomic_inc(&ipvs->ftpsvc_counter);
1261 else if (svc->port == 0)
1262 atomic_inc(&ipvs->nullsvc_counter);
1264 ip_vs_start_estimator(net, &svc->stats);
1266 /* Count only IPv4 services for old get/setsockopt interface */
1267 if (svc->af == AF_INET)
1268 ipvs->num_services++;
1270 /* Hash the service into the service table */
1271 ip_vs_svc_hash(svc);
1274 /* Now there is a service - full throttle */
1281 ip_vs_unbind_scheduler(svc, sched);
1282 ip_vs_service_free(svc);
1284 ip_vs_scheduler_put(sched);
1287 /* decrease the module use count */
1288 ip_vs_use_count_dec();
1295 * Edit a service and bind it with a new scheduler
1298 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1300 struct ip_vs_scheduler *sched = NULL, *old_sched;
1301 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1305 * Lookup the scheduler, by 'u->sched_name'
1307 if (strcmp(u->sched_name, "none")) {
1308 sched = ip_vs_scheduler_get(u->sched_name);
1310 pr_info("Scheduler module ip_vs_%s not found\n",
1317 if (u->pe_name && *u->pe_name) {
1318 pe = ip_vs_pe_getbyname(u->pe_name);
1320 pr_info("persistence engine module ip_vs_pe_%s "
1321 "not found\n", u->pe_name);
1328 #ifdef CONFIG_IP_VS_IPV6
1329 if (u->af == AF_INET6) {
1330 __u32 plen = (__force __u32) u->netmask;
1332 if (plen < 1 || plen > 128) {
1339 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1340 if (sched != old_sched) {
1342 ip_vs_unbind_scheduler(svc, old_sched);
1343 RCU_INIT_POINTER(svc->scheduler, NULL);
1344 /* Wait all svc->sched_data users */
1347 /* Bind the new scheduler */
1349 ret = ip_vs_bind_scheduler(svc, sched);
1351 ip_vs_scheduler_put(sched);
1358 * Set the flags and timeout value
1360 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1361 svc->timeout = u->timeout * HZ;
1362 svc->netmask = u->netmask;
1364 old_pe = rcu_dereference_protected(svc->pe, 1);
1366 rcu_assign_pointer(svc->pe, pe);
1369 ip_vs_scheduler_put(old_sched);
1370 ip_vs_pe_put(old_pe);
1375 * Delete a service from the service list
1376 * - The service must be unlinked, unlocked and not referenced!
1377 * - We are called under _bh lock
1379 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1381 struct ip_vs_dest *dest, *nxt;
1382 struct ip_vs_scheduler *old_sched;
1383 struct ip_vs_pe *old_pe;
1384 struct netns_ipvs *ipvs = net_ipvs(svc->net);
1386 pr_info("%s: enter\n", __func__);
1388 /* Count only IPv4 services for old get/setsockopt interface */
1389 if (svc->af == AF_INET)
1390 ipvs->num_services--;
1392 ip_vs_stop_estimator(svc->net, &svc->stats);
1394 /* Unbind scheduler */
1395 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1396 ip_vs_unbind_scheduler(svc, old_sched);
1397 ip_vs_scheduler_put(old_sched);
1399 /* Unbind persistence engine, keep svc->pe */
1400 old_pe = rcu_dereference_protected(svc->pe, 1);
1401 ip_vs_pe_put(old_pe);
1404 * Unlink the whole destination list
1406 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1407 __ip_vs_unlink_dest(svc, dest, 0);
1408 __ip_vs_del_dest(svc->net, dest, cleanup);
1412 * Update the virtual service counters
1414 if (svc->port == FTPPORT)
1415 atomic_dec(&ipvs->ftpsvc_counter);
1416 else if (svc->port == 0)
1417 atomic_dec(&ipvs->nullsvc_counter);
1420 * Free the service if nobody refers to it
1422 __ip_vs_svc_put(svc, true);
1424 /* decrease the module use count */
1425 ip_vs_use_count_dec();
1429 * Unlink a service from list and try to delete it if its refcnt reached 0
1431 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1433 /* Hold svc to avoid double release from dest_trash */
1434 atomic_inc(&svc->refcnt);
1436 * Unhash it from the service table
1438 ip_vs_svc_unhash(svc);
1440 __ip_vs_del_service(svc, cleanup);
1444 * Delete a service from the service list
1446 static int ip_vs_del_service(struct ip_vs_service *svc)
1450 ip_vs_unlink_service(svc, false);
1457 * Flush all the virtual services
1459 static int ip_vs_flush(struct net *net, bool cleanup)
1462 struct ip_vs_service *svc;
1463 struct hlist_node *n;
1466 * Flush the service table hashed by <netns,protocol,addr,port>
1468 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1469 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1471 if (net_eq(svc->net, net))
1472 ip_vs_unlink_service(svc, cleanup);
1477 * Flush the service table hashed by fwmark
1479 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1480 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1482 if (net_eq(svc->net, net))
1483 ip_vs_unlink_service(svc, cleanup);
1491 * Delete service by {netns} in the service table.
1492 * Called by __ip_vs_cleanup()
1494 void ip_vs_service_net_cleanup(struct net *net)
1497 /* Check for "full" addressed entries */
1498 mutex_lock(&__ip_vs_mutex);
1499 ip_vs_flush(net, true);
1500 mutex_unlock(&__ip_vs_mutex);
1504 /* Put all references for device (dst_cache) */
1506 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1508 struct ip_vs_dest_dst *dest_dst;
1510 spin_lock_bh(&dest->dst_lock);
1511 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1512 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1513 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1515 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1517 atomic_read(&dest->refcnt));
1518 __ip_vs_dst_cache_reset(dest);
1520 spin_unlock_bh(&dest->dst_lock);
1523 /* Netdev event receiver
1524 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1526 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1529 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1530 struct net *net = dev_net(dev);
1531 struct netns_ipvs *ipvs = net_ipvs(net);
1532 struct ip_vs_service *svc;
1533 struct ip_vs_dest *dest;
1536 if (event != NETDEV_DOWN || !ipvs)
1538 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1540 mutex_lock(&__ip_vs_mutex);
1541 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1542 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1543 if (net_eq(svc->net, net)) {
1544 list_for_each_entry(dest, &svc->destinations,
1546 ip_vs_forget_dev(dest, dev);
1551 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1552 if (net_eq(svc->net, net)) {
1553 list_for_each_entry(dest, &svc->destinations,
1555 ip_vs_forget_dev(dest, dev);
1562 spin_lock_bh(&ipvs->dest_trash_lock);
1563 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1564 ip_vs_forget_dev(dest, dev);
1566 spin_unlock_bh(&ipvs->dest_trash_lock);
1567 mutex_unlock(&__ip_vs_mutex);
1573 * Zero counters in a service or all services
1575 static int ip_vs_zero_service(struct ip_vs_service *svc)
1577 struct ip_vs_dest *dest;
1579 list_for_each_entry(dest, &svc->destinations, n_list) {
1580 ip_vs_zero_stats(&dest->stats);
1582 ip_vs_zero_stats(&svc->stats);
1586 static int ip_vs_zero_all(struct net *net)
1589 struct ip_vs_service *svc;
1591 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1592 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1593 if (net_eq(svc->net, net))
1594 ip_vs_zero_service(svc);
1598 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1599 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1600 if (net_eq(svc->net, net))
1601 ip_vs_zero_service(svc);
1605 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
1609 #ifdef CONFIG_SYSCTL
1612 static int three = 3;
1615 proc_do_defense_mode(struct ctl_table *table, int write,
1616 void __user *buffer, size_t *lenp, loff_t *ppos)
1618 struct net *net = current->nsproxy->net_ns;
1619 int *valp = table->data;
1623 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1624 if (write && (*valp != val)) {
1625 if ((*valp < 0) || (*valp > 3)) {
1626 /* Restore the correct value */
1629 update_defense_level(net_ipvs(net));
1636 proc_do_sync_threshold(struct ctl_table *table, int write,
1637 void __user *buffer, size_t *lenp, loff_t *ppos)
1639 int *valp = table->data;
1643 /* backup the value first */
1644 memcpy(val, valp, sizeof(val));
1646 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1647 if (write && (valp[0] < 0 || valp[1] < 0 ||
1648 (valp[0] >= valp[1] && valp[1]))) {
1649 /* Restore the correct value */
1650 memcpy(valp, val, sizeof(val));
1656 proc_do_sync_mode(struct ctl_table *table, int write,
1657 void __user *buffer, size_t *lenp, loff_t *ppos)
1659 int *valp = table->data;
1663 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1664 if (write && (*valp != val)) {
1665 if ((*valp < 0) || (*valp > 1)) {
1666 /* Restore the correct value */
1674 proc_do_sync_ports(struct ctl_table *table, int write,
1675 void __user *buffer, size_t *lenp, loff_t *ppos)
1677 int *valp = table->data;
1681 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1682 if (write && (*valp != val)) {
1683 if (*valp < 1 || !is_power_of_2(*valp)) {
1684 /* Restore the correct value */
1692 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1693 * Do not change order or insert new entries without
1694 * align with netns init in ip_vs_control_net_init()
1697 static struct ctl_table vs_vars[] = {
1699 .procname = "amemthresh",
1700 .maxlen = sizeof(int),
1702 .proc_handler = proc_dointvec,
1705 .procname = "am_droprate",
1706 .maxlen = sizeof(int),
1708 .proc_handler = proc_dointvec,
1711 .procname = "drop_entry",
1712 .maxlen = sizeof(int),
1714 .proc_handler = proc_do_defense_mode,
1717 .procname = "drop_packet",
1718 .maxlen = sizeof(int),
1720 .proc_handler = proc_do_defense_mode,
1722 #ifdef CONFIG_IP_VS_NFCT
1724 .procname = "conntrack",
1725 .maxlen = sizeof(int),
1727 .proc_handler = &proc_dointvec,
1731 .procname = "secure_tcp",
1732 .maxlen = sizeof(int),
1734 .proc_handler = proc_do_defense_mode,
1737 .procname = "snat_reroute",
1738 .maxlen = sizeof(int),
1740 .proc_handler = &proc_dointvec,
1743 .procname = "sync_version",
1744 .maxlen = sizeof(int),
1746 .proc_handler = &proc_do_sync_mode,
1749 .procname = "sync_ports",
1750 .maxlen = sizeof(int),
1752 .proc_handler = &proc_do_sync_ports,
1755 .procname = "sync_persist_mode",
1756 .maxlen = sizeof(int),
1758 .proc_handler = proc_dointvec,
1761 .procname = "sync_qlen_max",
1762 .maxlen = sizeof(unsigned long),
1764 .proc_handler = proc_doulongvec_minmax,
1767 .procname = "sync_sock_size",
1768 .maxlen = sizeof(int),
1770 .proc_handler = proc_dointvec,
1773 .procname = "cache_bypass",
1774 .maxlen = sizeof(int),
1776 .proc_handler = proc_dointvec,
1779 .procname = "expire_nodest_conn",
1780 .maxlen = sizeof(int),
1782 .proc_handler = proc_dointvec,
1785 .procname = "sloppy_tcp",
1786 .maxlen = sizeof(int),
1788 .proc_handler = proc_dointvec,
1791 .procname = "sloppy_sctp",
1792 .maxlen = sizeof(int),
1794 .proc_handler = proc_dointvec,
1797 .procname = "expire_quiescent_template",
1798 .maxlen = sizeof(int),
1800 .proc_handler = proc_dointvec,
1803 .procname = "sync_threshold",
1805 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1807 .proc_handler = proc_do_sync_threshold,
1810 .procname = "sync_refresh_period",
1811 .maxlen = sizeof(int),
1813 .proc_handler = proc_dointvec_jiffies,
1816 .procname = "sync_retries",
1817 .maxlen = sizeof(int),
1819 .proc_handler = proc_dointvec_minmax,
1824 .procname = "nat_icmp_send",
1825 .maxlen = sizeof(int),
1827 .proc_handler = proc_dointvec,
1830 .procname = "pmtu_disc",
1831 .maxlen = sizeof(int),
1833 .proc_handler = proc_dointvec,
1836 .procname = "backup_only",
1837 .maxlen = sizeof(int),
1839 .proc_handler = proc_dointvec,
1842 .procname = "conn_reuse_mode",
1843 .maxlen = sizeof(int),
1845 .proc_handler = proc_dointvec,
1847 #ifdef CONFIG_IP_VS_DEBUG
1849 .procname = "debug_level",
1850 .data = &sysctl_ip_vs_debug_level,
1851 .maxlen = sizeof(int),
1853 .proc_handler = proc_dointvec,
1861 #ifdef CONFIG_PROC_FS
1864 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1865 struct hlist_head *table;
1870 * Write the contents of the VS rule table to a PROCfs file.
1871 * (It is kept just for backward compatibility)
1873 static inline const char *ip_vs_fwd_name(unsigned int flags)
1875 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1876 case IP_VS_CONN_F_LOCALNODE:
1878 case IP_VS_CONN_F_TUNNEL:
1880 case IP_VS_CONN_F_DROUTE:
1888 /* Get the Nth entry in the two lists */
1889 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1891 struct net *net = seq_file_net(seq);
1892 struct ip_vs_iter *iter = seq->private;
1894 struct ip_vs_service *svc;
1896 /* look in hash by protocol */
1897 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1898 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1899 if (net_eq(svc->net, net) && pos-- == 0) {
1900 iter->table = ip_vs_svc_table;
1907 /* keep looking in fwmark */
1908 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1909 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1911 if (net_eq(svc->net, net) && pos-- == 0) {
1912 iter->table = ip_vs_svc_fwm_table;
1922 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1926 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1930 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1932 struct hlist_node *e;
1933 struct ip_vs_iter *iter;
1934 struct ip_vs_service *svc;
1937 if (v == SEQ_START_TOKEN)
1938 return ip_vs_info_array(seq,0);
1941 iter = seq->private;
1943 if (iter->table == ip_vs_svc_table) {
1944 /* next service in table hashed by protocol */
1945 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1947 return hlist_entry(e, struct ip_vs_service, s_list);
1949 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1950 hlist_for_each_entry_rcu(svc,
1951 &ip_vs_svc_table[iter->bucket],
1957 iter->table = ip_vs_svc_fwm_table;
1962 /* next service in hashed by fwmark */
1963 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
1965 return hlist_entry(e, struct ip_vs_service, f_list);
1968 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1969 hlist_for_each_entry_rcu(svc,
1970 &ip_vs_svc_fwm_table[iter->bucket],
1978 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1985 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1987 if (v == SEQ_START_TOKEN) {
1989 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1990 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1992 "Prot LocalAddress:Port Scheduler Flags\n");
1994 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1996 const struct ip_vs_service *svc = v;
1997 const struct ip_vs_iter *iter = seq->private;
1998 const struct ip_vs_dest *dest;
1999 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2000 char *sched_name = sched ? sched->name : "none";
2002 if (iter->table == ip_vs_svc_table) {
2003 #ifdef CONFIG_IP_VS_IPV6
2004 if (svc->af == AF_INET6)
2005 seq_printf(seq, "%s [%pI6]:%04X %s ",
2006 ip_vs_proto_name(svc->protocol),
2012 seq_printf(seq, "%s %08X:%04X %s %s ",
2013 ip_vs_proto_name(svc->protocol),
2014 ntohl(svc->addr.ip),
2017 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2019 seq_printf(seq, "FWM %08X %s %s",
2020 svc->fwmark, sched_name,
2021 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2024 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2025 seq_printf(seq, "persistent %d %08X\n",
2027 ntohl(svc->netmask));
2029 seq_putc(seq, '\n');
2031 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2032 #ifdef CONFIG_IP_VS_IPV6
2033 if (dest->af == AF_INET6)
2036 " %-7s %-6d %-10d %-10d\n",
2039 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2040 atomic_read(&dest->weight),
2041 atomic_read(&dest->activeconns),
2042 atomic_read(&dest->inactconns));
2047 "%-7s %-6d %-10d %-10d\n",
2048 ntohl(dest->addr.ip),
2050 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2051 atomic_read(&dest->weight),
2052 atomic_read(&dest->activeconns),
2053 atomic_read(&dest->inactconns));
2060 static const struct seq_operations ip_vs_info_seq_ops = {
2061 .start = ip_vs_info_seq_start,
2062 .next = ip_vs_info_seq_next,
2063 .stop = ip_vs_info_seq_stop,
2064 .show = ip_vs_info_seq_show,
2067 static int ip_vs_info_open(struct inode *inode, struct file *file)
2069 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2070 sizeof(struct ip_vs_iter));
2073 static const struct file_operations ip_vs_info_fops = {
2074 .owner = THIS_MODULE,
2075 .open = ip_vs_info_open,
2077 .llseek = seq_lseek,
2078 .release = seq_release_net,
2081 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2083 struct net *net = seq_file_single_net(seq);
2084 struct ip_vs_kstats show;
2086 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2088 " Total Incoming Outgoing Incoming Outgoing\n");
2090 " Conns Packets Packets Bytes Bytes\n");
2092 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2093 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2094 (unsigned long long)show.conns,
2095 (unsigned long long)show.inpkts,
2096 (unsigned long long)show.outpkts,
2097 (unsigned long long)show.inbytes,
2098 (unsigned long long)show.outbytes);
2100 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2102 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2103 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2104 (unsigned long long)show.cps,
2105 (unsigned long long)show.inpps,
2106 (unsigned long long)show.outpps,
2107 (unsigned long long)show.inbps,
2108 (unsigned long long)show.outbps);
2113 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2115 return single_open_net(inode, file, ip_vs_stats_show);
2118 static const struct file_operations ip_vs_stats_fops = {
2119 .owner = THIS_MODULE,
2120 .open = ip_vs_stats_seq_open,
2122 .llseek = seq_lseek,
2123 .release = single_release_net,
2126 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2128 struct net *net = seq_file_single_net(seq);
2129 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2130 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2131 struct ip_vs_kstats kstats;
2134 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2136 " Total Incoming Outgoing Incoming Outgoing\n");
2138 "CPU Conns Packets Packets Bytes Bytes\n");
2140 for_each_possible_cpu(i) {
2141 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2143 u64 conns, inpkts, outpkts, inbytes, outbytes;
2146 start = u64_stats_fetch_begin_irq(&u->syncp);
2147 conns = u->cnt.conns;
2148 inpkts = u->cnt.inpkts;
2149 outpkts = u->cnt.outpkts;
2150 inbytes = u->cnt.inbytes;
2151 outbytes = u->cnt.outbytes;
2152 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2154 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2155 i, (u64)conns, (u64)inpkts,
2156 (u64)outpkts, (u64)inbytes,
2160 ip_vs_copy_stats(&kstats, tot_stats);
2162 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2163 (unsigned long long)kstats.conns,
2164 (unsigned long long)kstats.inpkts,
2165 (unsigned long long)kstats.outpkts,
2166 (unsigned long long)kstats.inbytes,
2167 (unsigned long long)kstats.outbytes);
2169 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2171 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2172 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2182 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2184 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2187 static const struct file_operations ip_vs_stats_percpu_fops = {
2188 .owner = THIS_MODULE,
2189 .open = ip_vs_stats_percpu_seq_open,
2191 .llseek = seq_lseek,
2192 .release = single_release_net,
2197 * Set timeout values for tcp tcpfin udp in the timeout_table.
2199 static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
2201 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2202 struct ip_vs_proto_data *pd;
2205 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2210 #ifdef CONFIG_IP_VS_PROTO_TCP
2211 if (u->tcp_timeout) {
2212 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2213 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2214 = u->tcp_timeout * HZ;
2217 if (u->tcp_fin_timeout) {
2218 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2219 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2220 = u->tcp_fin_timeout * HZ;
2224 #ifdef CONFIG_IP_VS_PROTO_UDP
2225 if (u->udp_timeout) {
2226 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2227 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2228 = u->udp_timeout * HZ;
2234 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2236 struct ip_vs_svcdest_user {
2237 struct ip_vs_service_user s;
2238 struct ip_vs_dest_user d;
2241 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2242 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2243 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2244 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2245 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2246 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2247 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2248 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2249 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2250 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2251 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2254 union ip_vs_set_arglen {
2255 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2256 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2257 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2258 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2259 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2260 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2261 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2262 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2263 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2264 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2267 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2269 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2270 struct ip_vs_service_user *usvc_compat)
2272 memset(usvc, 0, sizeof(*usvc));
2275 usvc->protocol = usvc_compat->protocol;
2276 usvc->addr.ip = usvc_compat->addr;
2277 usvc->port = usvc_compat->port;
2278 usvc->fwmark = usvc_compat->fwmark;
2280 /* Deep copy of sched_name is not needed here */
2281 usvc->sched_name = usvc_compat->sched_name;
2283 usvc->flags = usvc_compat->flags;
2284 usvc->timeout = usvc_compat->timeout;
2285 usvc->netmask = usvc_compat->netmask;
2288 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2289 struct ip_vs_dest_user *udest_compat)
2291 memset(udest, 0, sizeof(*udest));
2293 udest->addr.ip = udest_compat->addr;
2294 udest->port = udest_compat->port;
2295 udest->conn_flags = udest_compat->conn_flags;
2296 udest->weight = udest_compat->weight;
2297 udest->u_threshold = udest_compat->u_threshold;
2298 udest->l_threshold = udest_compat->l_threshold;
2299 udest->af = AF_INET;
2303 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2305 struct net *net = sock_net(sk);
2307 unsigned char arg[MAX_SET_ARGLEN];
2308 struct ip_vs_service_user *usvc_compat;
2309 struct ip_vs_service_user_kern usvc;
2310 struct ip_vs_service *svc;
2311 struct ip_vs_dest_user *udest_compat;
2312 struct ip_vs_dest_user_kern udest;
2313 struct netns_ipvs *ipvs = net_ipvs(net);
2315 BUILD_BUG_ON(sizeof(arg) > 255);
2316 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2319 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2321 if (len != set_arglen[CMDID(cmd)]) {
2322 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2323 len, set_arglen[CMDID(cmd)]);
2327 if (copy_from_user(arg, user, len) != 0)
2330 /* increase the module use count */
2331 ip_vs_use_count_inc();
2333 /* Handle daemons since they have another lock */
2334 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2335 cmd == IP_VS_SO_SET_STOPDAEMON) {
2336 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2338 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2339 struct ipvs_sync_daemon_cfg cfg;
2341 memset(&cfg, 0, sizeof(cfg));
2342 strlcpy(cfg.mcast_ifn, dm->mcast_ifn,
2343 sizeof(cfg.mcast_ifn));
2344 cfg.syncid = dm->syncid;
2346 mutex_lock(&ipvs->sync_mutex);
2347 ret = start_sync_thread(net, &cfg, dm->state);
2348 mutex_unlock(&ipvs->sync_mutex);
2351 mutex_lock(&ipvs->sync_mutex);
2352 ret = stop_sync_thread(net, dm->state);
2353 mutex_unlock(&ipvs->sync_mutex);
2358 mutex_lock(&__ip_vs_mutex);
2359 if (cmd == IP_VS_SO_SET_FLUSH) {
2360 /* Flush the virtual service */
2361 ret = ip_vs_flush(net, false);
2363 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2364 /* Set timeout values for (tcp tcpfin udp) */
2365 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
2369 usvc_compat = (struct ip_vs_service_user *)arg;
2370 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2372 /* We only use the new structs internally, so copy userspace compat
2373 * structs to extended internal versions */
2374 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2375 ip_vs_copy_udest_compat(&udest, udest_compat);
2377 if (cmd == IP_VS_SO_SET_ZERO) {
2378 /* if no service address is set, zero counters in all */
2379 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2380 ret = ip_vs_zero_all(net);
2385 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2386 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2387 usvc.protocol != IPPROTO_SCTP) {
2388 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2389 usvc.protocol, &usvc.addr.ip,
2390 ntohs(usvc.port), usvc.sched_name);
2395 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2397 if (usvc.fwmark == 0)
2398 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2399 &usvc.addr, usvc.port);
2401 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
2404 if (cmd != IP_VS_SO_SET_ADD
2405 && (svc == NULL || svc->protocol != usvc.protocol)) {
2411 case IP_VS_SO_SET_ADD:
2415 ret = ip_vs_add_service(net, &usvc, &svc);
2417 case IP_VS_SO_SET_EDIT:
2418 ret = ip_vs_edit_service(svc, &usvc);
2420 case IP_VS_SO_SET_DEL:
2421 ret = ip_vs_del_service(svc);
2425 case IP_VS_SO_SET_ZERO:
2426 ret = ip_vs_zero_service(svc);
2428 case IP_VS_SO_SET_ADDDEST:
2429 ret = ip_vs_add_dest(svc, &udest);
2431 case IP_VS_SO_SET_EDITDEST:
2432 ret = ip_vs_edit_dest(svc, &udest);
2434 case IP_VS_SO_SET_DELDEST:
2435 ret = ip_vs_del_dest(svc, &udest);
2442 mutex_unlock(&__ip_vs_mutex);
2444 /* decrease the module use count */
2445 ip_vs_use_count_dec();
2452 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2454 struct ip_vs_scheduler *sched;
2455 struct ip_vs_kstats kstats;
2458 sched = rcu_dereference_protected(src->scheduler, 1);
2459 sched_name = sched ? sched->name : "none";
2460 dst->protocol = src->protocol;
2461 dst->addr = src->addr.ip;
2462 dst->port = src->port;
2463 dst->fwmark = src->fwmark;
2464 strlcpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2465 dst->flags = src->flags;
2466 dst->timeout = src->timeout / HZ;
2467 dst->netmask = src->netmask;
2468 dst->num_dests = src->num_dests;
2469 ip_vs_copy_stats(&kstats, &src->stats);
2470 ip_vs_export_stats_user(&dst->stats, &kstats);
2474 __ip_vs_get_service_entries(struct net *net,
2475 const struct ip_vs_get_services *get,
2476 struct ip_vs_get_services __user *uptr)
2479 struct ip_vs_service *svc;
2480 struct ip_vs_service_entry entry;
2483 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2484 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2485 /* Only expose IPv4 entries to old interface */
2486 if (svc->af != AF_INET || !net_eq(svc->net, net))
2489 if (count >= get->num_services)
2491 memset(&entry, 0, sizeof(entry));
2492 ip_vs_copy_service(&entry, svc);
2493 if (copy_to_user(&uptr->entrytable[count],
2494 &entry, sizeof(entry))) {
2502 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2503 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2504 /* Only expose IPv4 entries to old interface */
2505 if (svc->af != AF_INET || !net_eq(svc->net, net))
2508 if (count >= get->num_services)
2510 memset(&entry, 0, sizeof(entry));
2511 ip_vs_copy_service(&entry, svc);
2512 if (copy_to_user(&uptr->entrytable[count],
2513 &entry, sizeof(entry))) {
2525 __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2526 struct ip_vs_get_dests __user *uptr)
2528 struct ip_vs_service *svc;
2529 union nf_inet_addr addr = { .ip = get->addr };
2534 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
2536 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2542 struct ip_vs_dest *dest;
2543 struct ip_vs_dest_entry entry;
2544 struct ip_vs_kstats kstats;
2546 memset(&entry, 0, sizeof(entry));
2547 list_for_each_entry(dest, &svc->destinations, n_list) {
2548 if (count >= get->num_dests)
2551 /* Cannot expose heterogeneous members via sockopt
2554 if (dest->af != svc->af)
2557 entry.addr = dest->addr.ip;
2558 entry.port = dest->port;
2559 entry.conn_flags = atomic_read(&dest->conn_flags);
2560 entry.weight = atomic_read(&dest->weight);
2561 entry.u_threshold = dest->u_threshold;
2562 entry.l_threshold = dest->l_threshold;
2563 entry.activeconns = atomic_read(&dest->activeconns);
2564 entry.inactconns = atomic_read(&dest->inactconns);
2565 entry.persistconns = atomic_read(&dest->persistconns);
2566 ip_vs_copy_stats(&kstats, &dest->stats);
2567 ip_vs_export_stats_user(&entry.stats, &kstats);
2568 if (copy_to_user(&uptr->entrytable[count],
2569 &entry, sizeof(entry))) {
2581 __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
2583 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2584 struct ip_vs_proto_data *pd;
2587 memset(u, 0, sizeof (*u));
2589 #ifdef CONFIG_IP_VS_PROTO_TCP
2590 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2591 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2592 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2594 #ifdef CONFIG_IP_VS_PROTO_UDP
2595 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2597 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2601 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2602 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2603 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2604 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2605 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2606 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2607 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2608 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2611 union ip_vs_get_arglen {
2612 char field_IP_VS_SO_GET_VERSION[64];
2613 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2614 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2615 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2616 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2617 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2618 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2621 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2624 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2626 unsigned char arg[MAX_GET_ARGLEN];
2628 unsigned int copylen;
2629 struct net *net = sock_net(sk);
2630 struct netns_ipvs *ipvs = net_ipvs(net);
2633 BUILD_BUG_ON(sizeof(arg) > 255);
2634 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2637 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2640 copylen = get_arglen[CMDID(cmd)];
2641 if (*len < (int) copylen) {
2642 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2646 if (copy_from_user(arg, user, copylen) != 0)
2649 * Handle daemons first since it has its own locking
2651 if (cmd == IP_VS_SO_GET_DAEMON) {
2652 struct ip_vs_daemon_user d[2];
2654 memset(&d, 0, sizeof(d));
2655 mutex_lock(&ipvs->sync_mutex);
2656 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2657 d[0].state = IP_VS_STATE_MASTER;
2658 strlcpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2659 sizeof(d[0].mcast_ifn));
2660 d[0].syncid = ipvs->mcfg.syncid;
2662 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2663 d[1].state = IP_VS_STATE_BACKUP;
2664 strlcpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2665 sizeof(d[1].mcast_ifn));
2666 d[1].syncid = ipvs->bcfg.syncid;
2668 if (copy_to_user(user, &d, sizeof(d)) != 0)
2670 mutex_unlock(&ipvs->sync_mutex);
2674 mutex_lock(&__ip_vs_mutex);
2676 case IP_VS_SO_GET_VERSION:
2680 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2681 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2682 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2686 *len = strlen(buf)+1;
2690 case IP_VS_SO_GET_INFO:
2692 struct ip_vs_getinfo info;
2693 info.version = IP_VS_VERSION_CODE;
2694 info.size = ip_vs_conn_tab_size;
2695 info.num_services = ipvs->num_services;
2696 if (copy_to_user(user, &info, sizeof(info)) != 0)
2701 case IP_VS_SO_GET_SERVICES:
2703 struct ip_vs_get_services *get;
2706 get = (struct ip_vs_get_services *)arg;
2707 size = sizeof(*get) +
2708 sizeof(struct ip_vs_service_entry) * get->num_services;
2710 pr_err("length: %u != %u\n", *len, size);
2714 ret = __ip_vs_get_service_entries(net, get, user);
2718 case IP_VS_SO_GET_SERVICE:
2720 struct ip_vs_service_entry *entry;
2721 struct ip_vs_service *svc;
2722 union nf_inet_addr addr;
2724 entry = (struct ip_vs_service_entry *)arg;
2725 addr.ip = entry->addr;
2728 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
2730 svc = __ip_vs_service_find(net, AF_INET,
2731 entry->protocol, &addr,
2735 ip_vs_copy_service(entry, svc);
2736 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2743 case IP_VS_SO_GET_DESTS:
2745 struct ip_vs_get_dests *get;
2748 get = (struct ip_vs_get_dests *)arg;
2749 size = sizeof(*get) +
2750 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2752 pr_err("length: %u != %u\n", *len, size);
2756 ret = __ip_vs_get_dest_entries(net, get, user);
2760 case IP_VS_SO_GET_TIMEOUT:
2762 struct ip_vs_timeout_user t;
2764 __ip_vs_get_timeouts(net, &t);
2765 if (copy_to_user(user, &t, sizeof(t)) != 0)
2775 mutex_unlock(&__ip_vs_mutex);
2780 static struct nf_sockopt_ops ip_vs_sockopts = {
2782 .set_optmin = IP_VS_BASE_CTL,
2783 .set_optmax = IP_VS_SO_SET_MAX+1,
2784 .set = do_ip_vs_set_ctl,
2785 .get_optmin = IP_VS_BASE_CTL,
2786 .get_optmax = IP_VS_SO_GET_MAX+1,
2787 .get = do_ip_vs_get_ctl,
2788 .owner = THIS_MODULE,
2792 * Generic Netlink interface
2795 /* IPVS genetlink family */
2796 static struct genl_family ip_vs_genl_family = {
2797 .id = GENL_ID_GENERATE,
2799 .name = IPVS_GENL_NAME,
2800 .version = IPVS_GENL_VERSION,
2801 .maxattr = IPVS_CMD_MAX,
2802 .netnsok = true, /* Make ipvsadm to work on netns */
2805 /* Policy used for first-level command attributes */
2806 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2807 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2808 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2809 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2810 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2811 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2812 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2815 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2816 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2817 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2818 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2819 .len = IP_VS_IFNAME_MAXLEN },
2820 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2821 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2822 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2823 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2824 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2825 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2828 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2829 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2830 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2831 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2832 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2833 .len = sizeof(union nf_inet_addr) },
2834 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2835 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2836 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2837 .len = IP_VS_SCHEDNAME_MAXLEN },
2838 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2839 .len = IP_VS_PENAME_MAXLEN },
2840 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2841 .len = sizeof(struct ip_vs_flags) },
2842 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2843 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2844 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2847 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2848 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2849 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2850 .len = sizeof(union nf_inet_addr) },
2851 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2852 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2853 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2854 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2855 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2856 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2857 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2858 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2859 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2860 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2863 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2864 struct ip_vs_kstats *kstats)
2866 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2871 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2872 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2873 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2874 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2875 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2876 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2877 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2878 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2879 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2880 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2881 goto nla_put_failure;
2882 nla_nest_end(skb, nl_stats);
2887 nla_nest_cancel(skb, nl_stats);
2891 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2892 struct ip_vs_kstats *kstats)
2894 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2899 if (nla_put_u64(skb, IPVS_STATS_ATTR_CONNS, kstats->conns) ||
2900 nla_put_u64(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts) ||
2901 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts) ||
2902 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2903 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2904 nla_put_u64(skb, IPVS_STATS_ATTR_CPS, kstats->cps) ||
2905 nla_put_u64(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps) ||
2906 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps) ||
2907 nla_put_u64(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps) ||
2908 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps))
2909 goto nla_put_failure;
2910 nla_nest_end(skb, nl_stats);
2915 nla_nest_cancel(skb, nl_stats);
2919 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2920 struct ip_vs_service *svc)
2922 struct ip_vs_scheduler *sched;
2923 struct ip_vs_pe *pe;
2924 struct nlattr *nl_service;
2925 struct ip_vs_flags flags = { .flags = svc->flags,
2927 struct ip_vs_kstats kstats;
2930 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2934 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2935 goto nla_put_failure;
2937 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2938 goto nla_put_failure;
2940 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2941 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2942 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2943 goto nla_put_failure;
2946 sched = rcu_dereference_protected(svc->scheduler, 1);
2947 sched_name = sched ? sched->name : "none";
2948 pe = rcu_dereference_protected(svc->pe, 1);
2949 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
2950 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2951 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2952 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2953 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2954 goto nla_put_failure;
2955 ip_vs_copy_stats(&kstats, &svc->stats);
2956 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
2957 goto nla_put_failure;
2958 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
2959 goto nla_put_failure;
2961 nla_nest_end(skb, nl_service);
2966 nla_nest_cancel(skb, nl_service);
2970 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2971 struct ip_vs_service *svc,
2972 struct netlink_callback *cb)
2976 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2977 &ip_vs_genl_family, NLM_F_MULTI,
2978 IPVS_CMD_NEW_SERVICE);
2982 if (ip_vs_genl_fill_service(skb, svc) < 0)
2983 goto nla_put_failure;
2985 genlmsg_end(skb, hdr);
2989 genlmsg_cancel(skb, hdr);
2993 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2994 struct netlink_callback *cb)
2997 int start = cb->args[0];
2998 struct ip_vs_service *svc;
2999 struct net *net = skb_sknet(skb);
3001 mutex_lock(&__ip_vs_mutex);
3002 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3003 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3004 if (++idx <= start || !net_eq(svc->net, net))
3006 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3008 goto nla_put_failure;
3013 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3014 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3015 if (++idx <= start || !net_eq(svc->net, net))
3017 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3019 goto nla_put_failure;
3025 mutex_unlock(&__ip_vs_mutex);
3031 static int ip_vs_genl_parse_service(struct net *net,
3032 struct ip_vs_service_user_kern *usvc,
3033 struct nlattr *nla, int full_entry,
3034 struct ip_vs_service **ret_svc)
3036 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3037 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3038 struct ip_vs_service *svc;
3040 /* Parse mandatory identifying service fields first */
3042 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
3045 nla_af = attrs[IPVS_SVC_ATTR_AF];
3046 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3047 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3048 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3049 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3051 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3054 memset(usvc, 0, sizeof(*usvc));
3056 usvc->af = nla_get_u16(nla_af);
3057 #ifdef CONFIG_IP_VS_IPV6
3058 if (usvc->af != AF_INET && usvc->af != AF_INET6)
3060 if (usvc->af != AF_INET)
3062 return -EAFNOSUPPORT;
3065 usvc->protocol = IPPROTO_TCP;
3066 usvc->fwmark = nla_get_u32(nla_fwmark);
3068 usvc->protocol = nla_get_u16(nla_protocol);
3069 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3070 usvc->port = nla_get_be16(nla_port);
3076 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
3078 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
3079 &usvc->addr, usvc->port);
3083 /* If a full entry was requested, check for the additional fields */
3085 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3087 struct ip_vs_flags flags;
3089 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3090 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3091 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3092 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3093 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3095 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3098 nla_memcpy(&flags, nla_flags, sizeof(flags));
3100 /* prefill flags from service if it already exists */
3102 usvc->flags = svc->flags;
3104 /* set new flags from userland */
3105 usvc->flags = (usvc->flags & ~flags.mask) |
3106 (flags.flags & flags.mask);
3107 usvc->sched_name = nla_data(nla_sched);
3108 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3109 usvc->timeout = nla_get_u32(nla_timeout);
3110 usvc->netmask = nla_get_be32(nla_netmask);
3116 static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3119 struct ip_vs_service_user_kern usvc;
3120 struct ip_vs_service *svc;
3123 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3124 return ret ? ERR_PTR(ret) : svc;
3127 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3129 struct nlattr *nl_dest;
3130 struct ip_vs_kstats kstats;
3132 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3136 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3137 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3138 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3139 (atomic_read(&dest->conn_flags) &
3140 IP_VS_CONN_F_FWD_MASK)) ||
3141 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3142 atomic_read(&dest->weight)) ||
3143 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3144 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3145 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3146 atomic_read(&dest->activeconns)) ||
3147 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3148 atomic_read(&dest->inactconns)) ||
3149 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3150 atomic_read(&dest->persistconns)) ||
3151 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3152 goto nla_put_failure;
3153 ip_vs_copy_stats(&kstats, &dest->stats);
3154 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3155 goto nla_put_failure;
3156 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3157 goto nla_put_failure;
3159 nla_nest_end(skb, nl_dest);
3164 nla_nest_cancel(skb, nl_dest);
3168 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3169 struct netlink_callback *cb)
3173 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3174 &ip_vs_genl_family, NLM_F_MULTI,
3179 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3180 goto nla_put_failure;
3182 genlmsg_end(skb, hdr);
3186 genlmsg_cancel(skb, hdr);
3190 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3191 struct netlink_callback *cb)
3194 int start = cb->args[0];
3195 struct ip_vs_service *svc;
3196 struct ip_vs_dest *dest;
3197 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3198 struct net *net = skb_sknet(skb);
3200 mutex_lock(&__ip_vs_mutex);
3202 /* Try to find the service for which to dump destinations */
3203 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3204 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3208 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3209 if (IS_ERR(svc) || svc == NULL)
3212 /* Dump the destinations */
3213 list_for_each_entry(dest, &svc->destinations, n_list) {
3216 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3218 goto nla_put_failure;
3226 mutex_unlock(&__ip_vs_mutex);
3231 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3232 struct nlattr *nla, int full_entry)
3234 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3235 struct nlattr *nla_addr, *nla_port;
3236 struct nlattr *nla_addr_family;
3238 /* Parse mandatory identifying destination fields first */
3240 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3243 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3244 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3245 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3247 if (!(nla_addr && nla_port))
3250 memset(udest, 0, sizeof(*udest));
3252 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3253 udest->port = nla_get_be16(nla_port);
3255 if (nla_addr_family)
3256 udest->af = nla_get_u16(nla_addr_family);
3260 /* If a full entry was requested, check for the additional fields */
3262 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3265 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3266 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3267 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3268 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3270 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3273 udest->conn_flags = nla_get_u32(nla_fwd)
3274 & IP_VS_CONN_F_FWD_MASK;
3275 udest->weight = nla_get_u32(nla_weight);
3276 udest->u_threshold = nla_get_u32(nla_u_thresh);
3277 udest->l_threshold = nla_get_u32(nla_l_thresh);
3283 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3284 struct ipvs_sync_daemon_cfg *c)
3286 struct nlattr *nl_daemon;
3288 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3292 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3293 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3294 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3295 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3296 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3297 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3298 goto nla_put_failure;
3299 #ifdef CONFIG_IP_VS_IPV6
3300 if (c->mcast_af == AF_INET6) {
3301 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3302 &c->mcast_group.in6))
3303 goto nla_put_failure;
3306 if (c->mcast_af == AF_INET &&
3307 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3309 goto nla_put_failure;
3310 nla_nest_end(skb, nl_daemon);
3315 nla_nest_cancel(skb, nl_daemon);
3319 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3320 struct ipvs_sync_daemon_cfg *c,
3321 struct netlink_callback *cb)
3324 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3325 &ip_vs_genl_family, NLM_F_MULTI,
3326 IPVS_CMD_NEW_DAEMON);
3330 if (ip_vs_genl_fill_daemon(skb, state, c))
3331 goto nla_put_failure;
3333 genlmsg_end(skb, hdr);
3337 genlmsg_cancel(skb, hdr);
3341 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3342 struct netlink_callback *cb)
3344 struct net *net = skb_sknet(skb);
3345 struct netns_ipvs *ipvs = net_ipvs(net);
3347 mutex_lock(&ipvs->sync_mutex);
3348 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3349 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3350 &ipvs->mcfg, cb) < 0)
3351 goto nla_put_failure;
3356 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3357 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3358 &ipvs->bcfg, cb) < 0)
3359 goto nla_put_failure;
3365 mutex_unlock(&ipvs->sync_mutex);
3370 static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3372 struct netns_ipvs *ipvs = net_ipvs(net);
3373 struct ipvs_sync_daemon_cfg c;
3377 memset(&c, 0, sizeof(c));
3378 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3379 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3380 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3382 strlcpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3383 sizeof(c.mcast_ifn));
3384 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3386 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3388 c.sync_maxlen = nla_get_u16(a);
3390 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3392 c.mcast_af = AF_INET;
3393 c.mcast_group.ip = nla_get_in_addr(a);
3394 if (!ipv4_is_multicast(c.mcast_group.ip))
3397 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3399 #ifdef CONFIG_IP_VS_IPV6
3402 c.mcast_af = AF_INET6;
3403 c.mcast_group.in6 = nla_get_in6_addr(a);
3404 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3405 if (!(addr_type & IPV6_ADDR_MULTICAST))
3408 return -EAFNOSUPPORT;
3413 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3415 c.mcast_port = nla_get_u16(a);
3417 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3419 c.mcast_ttl = nla_get_u8(a);
3421 /* The synchronization protocol is incompatible with mixed family
3424 if (ipvs->mixed_address_family_dests > 0)
3428 mutex_lock(&ipvs->sync_mutex);
3429 ret = start_sync_thread(net, &c,
3430 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3431 mutex_unlock(&ipvs->sync_mutex);
3436 static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3438 struct netns_ipvs *ipvs = net_ipvs(net);
3441 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3444 mutex_lock(&ipvs->sync_mutex);
3445 ret = stop_sync_thread(net,
3446 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3447 mutex_unlock(&ipvs->sync_mutex);
3451 static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3453 struct ip_vs_timeout_user t;
3455 __ip_vs_get_timeouts(net, &t);
3457 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3458 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3460 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3462 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3464 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3465 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3467 return ip_vs_set_timeout(net, &t);
3470 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3472 int ret = -EINVAL, cmd;
3474 struct netns_ipvs *ipvs;
3476 net = skb_sknet(skb);
3477 ipvs = net_ipvs(net);
3478 cmd = info->genlhdr->cmd;
3480 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3481 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3483 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3484 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3485 info->attrs[IPVS_CMD_ATTR_DAEMON],
3486 ip_vs_daemon_policy))
3489 if (cmd == IPVS_CMD_NEW_DAEMON)
3490 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3492 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3499 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3501 struct ip_vs_service *svc = NULL;
3502 struct ip_vs_service_user_kern usvc;
3503 struct ip_vs_dest_user_kern udest;
3505 int need_full_svc = 0, need_full_dest = 0;
3508 net = skb_sknet(skb);
3509 cmd = info->genlhdr->cmd;
3511 mutex_lock(&__ip_vs_mutex);
3513 if (cmd == IPVS_CMD_FLUSH) {
3514 ret = ip_vs_flush(net, false);
3516 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3517 ret = ip_vs_genl_set_config(net, info->attrs);
3519 } else if (cmd == IPVS_CMD_ZERO &&
3520 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3521 ret = ip_vs_zero_all(net);
3525 /* All following commands require a service argument, so check if we
3526 * received a valid one. We need a full service specification when
3527 * adding / editing a service. Only identifying members otherwise. */
3528 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3531 ret = ip_vs_genl_parse_service(net, &usvc,
3532 info->attrs[IPVS_CMD_ATTR_SERVICE],
3533 need_full_svc, &svc);
3537 /* Unless we're adding a new service, the service must already exist */
3538 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3543 /* Destination commands require a valid destination argument. For
3544 * adding / editing a destination, we need a full destination
3546 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3547 cmd == IPVS_CMD_DEL_DEST) {
3548 if (cmd != IPVS_CMD_DEL_DEST)
3551 ret = ip_vs_genl_parse_dest(&udest,
3552 info->attrs[IPVS_CMD_ATTR_DEST],
3557 /* Old protocols did not allow the user to specify address
3558 * family, so we set it to zero instead. We also didn't
3559 * allow heterogeneous pools in the old code, so it's safe
3560 * to assume that this will have the same address family as
3566 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3567 /* The synchronization protocol is incompatible
3568 * with mixed family services
3570 if (net_ipvs(net)->sync_state) {
3575 /* Which connection types do we support? */
3576 switch (udest.conn_flags) {
3577 case IP_VS_CONN_F_TUNNEL:
3578 /* We are able to forward this */
3588 case IPVS_CMD_NEW_SERVICE:
3590 ret = ip_vs_add_service(net, &usvc, &svc);
3594 case IPVS_CMD_SET_SERVICE:
3595 ret = ip_vs_edit_service(svc, &usvc);
3597 case IPVS_CMD_DEL_SERVICE:
3598 ret = ip_vs_del_service(svc);
3599 /* do not use svc, it can be freed */
3601 case IPVS_CMD_NEW_DEST:
3602 ret = ip_vs_add_dest(svc, &udest);
3604 case IPVS_CMD_SET_DEST:
3605 ret = ip_vs_edit_dest(svc, &udest);
3607 case IPVS_CMD_DEL_DEST:
3608 ret = ip_vs_del_dest(svc, &udest);
3611 ret = ip_vs_zero_service(svc);
3618 mutex_unlock(&__ip_vs_mutex);
3623 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3625 struct sk_buff *msg;
3627 int ret, cmd, reply_cmd;
3630 net = skb_sknet(skb);
3631 cmd = info->genlhdr->cmd;
3633 if (cmd == IPVS_CMD_GET_SERVICE)
3634 reply_cmd = IPVS_CMD_NEW_SERVICE;
3635 else if (cmd == IPVS_CMD_GET_INFO)
3636 reply_cmd = IPVS_CMD_SET_INFO;
3637 else if (cmd == IPVS_CMD_GET_CONFIG)
3638 reply_cmd = IPVS_CMD_SET_CONFIG;
3640 pr_err("unknown Generic Netlink command\n");
3644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3648 mutex_lock(&__ip_vs_mutex);
3650 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3652 goto nla_put_failure;
3655 case IPVS_CMD_GET_SERVICE:
3657 struct ip_vs_service *svc;
3659 svc = ip_vs_genl_find_service(net,
3660 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3665 ret = ip_vs_genl_fill_service(msg, svc);
3667 goto nla_put_failure;
3676 case IPVS_CMD_GET_CONFIG:
3678 struct ip_vs_timeout_user t;
3680 __ip_vs_get_timeouts(net, &t);
3681 #ifdef CONFIG_IP_VS_PROTO_TCP
3682 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3684 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3686 goto nla_put_failure;
3688 #ifdef CONFIG_IP_VS_PROTO_UDP
3689 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3690 goto nla_put_failure;
3696 case IPVS_CMD_GET_INFO:
3697 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3698 IP_VS_VERSION_CODE) ||
3699 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3700 ip_vs_conn_tab_size))
3701 goto nla_put_failure;
3705 genlmsg_end(msg, reply);
3706 ret = genlmsg_reply(msg, info);
3710 pr_err("not enough space in Netlink message\n");
3716 mutex_unlock(&__ip_vs_mutex);
3722 static const struct genl_ops ip_vs_genl_ops[] = {
3724 .cmd = IPVS_CMD_NEW_SERVICE,
3725 .flags = GENL_ADMIN_PERM,
3726 .policy = ip_vs_cmd_policy,
3727 .doit = ip_vs_genl_set_cmd,
3730 .cmd = IPVS_CMD_SET_SERVICE,
3731 .flags = GENL_ADMIN_PERM,
3732 .policy = ip_vs_cmd_policy,
3733 .doit = ip_vs_genl_set_cmd,
3736 .cmd = IPVS_CMD_DEL_SERVICE,
3737 .flags = GENL_ADMIN_PERM,
3738 .policy = ip_vs_cmd_policy,
3739 .doit = ip_vs_genl_set_cmd,
3742 .cmd = IPVS_CMD_GET_SERVICE,
3743 .flags = GENL_ADMIN_PERM,
3744 .doit = ip_vs_genl_get_cmd,
3745 .dumpit = ip_vs_genl_dump_services,
3746 .policy = ip_vs_cmd_policy,
3749 .cmd = IPVS_CMD_NEW_DEST,
3750 .flags = GENL_ADMIN_PERM,
3751 .policy = ip_vs_cmd_policy,
3752 .doit = ip_vs_genl_set_cmd,
3755 .cmd = IPVS_CMD_SET_DEST,
3756 .flags = GENL_ADMIN_PERM,
3757 .policy = ip_vs_cmd_policy,
3758 .doit = ip_vs_genl_set_cmd,
3761 .cmd = IPVS_CMD_DEL_DEST,
3762 .flags = GENL_ADMIN_PERM,
3763 .policy = ip_vs_cmd_policy,
3764 .doit = ip_vs_genl_set_cmd,
3767 .cmd = IPVS_CMD_GET_DEST,
3768 .flags = GENL_ADMIN_PERM,
3769 .policy = ip_vs_cmd_policy,
3770 .dumpit = ip_vs_genl_dump_dests,
3773 .cmd = IPVS_CMD_NEW_DAEMON,
3774 .flags = GENL_ADMIN_PERM,
3775 .policy = ip_vs_cmd_policy,
3776 .doit = ip_vs_genl_set_daemon,
3779 .cmd = IPVS_CMD_DEL_DAEMON,
3780 .flags = GENL_ADMIN_PERM,
3781 .policy = ip_vs_cmd_policy,
3782 .doit = ip_vs_genl_set_daemon,
3785 .cmd = IPVS_CMD_GET_DAEMON,
3786 .flags = GENL_ADMIN_PERM,
3787 .dumpit = ip_vs_genl_dump_daemons,
3790 .cmd = IPVS_CMD_SET_CONFIG,
3791 .flags = GENL_ADMIN_PERM,
3792 .policy = ip_vs_cmd_policy,
3793 .doit = ip_vs_genl_set_cmd,
3796 .cmd = IPVS_CMD_GET_CONFIG,
3797 .flags = GENL_ADMIN_PERM,
3798 .doit = ip_vs_genl_get_cmd,
3801 .cmd = IPVS_CMD_GET_INFO,
3802 .flags = GENL_ADMIN_PERM,
3803 .doit = ip_vs_genl_get_cmd,
3806 .cmd = IPVS_CMD_ZERO,
3807 .flags = GENL_ADMIN_PERM,
3808 .policy = ip_vs_cmd_policy,
3809 .doit = ip_vs_genl_set_cmd,
3812 .cmd = IPVS_CMD_FLUSH,
3813 .flags = GENL_ADMIN_PERM,
3814 .doit = ip_vs_genl_set_cmd,
3818 static int __init ip_vs_genl_register(void)
3820 return genl_register_family_with_ops(&ip_vs_genl_family,
3824 static void ip_vs_genl_unregister(void)
3826 genl_unregister_family(&ip_vs_genl_family);
3829 /* End of Generic Netlink interface definitions */
3832 * per netns intit/exit func.
3834 #ifdef CONFIG_SYSCTL
3835 static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3838 struct netns_ipvs *ipvs = net_ipvs(net);
3839 struct ctl_table *tbl;
3841 atomic_set(&ipvs->dropentry, 0);
3842 spin_lock_init(&ipvs->dropentry_lock);
3843 spin_lock_init(&ipvs->droppacket_lock);
3844 spin_lock_init(&ipvs->securetcp_lock);
3846 if (!net_eq(net, &init_net)) {
3847 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3851 /* Don't export sysctls to unprivileged users */
3852 if (net->user_ns != &init_user_ns)
3853 tbl[0].procname = NULL;
3856 /* Initialize sysctl defaults */
3858 ipvs->sysctl_amemthresh = 1024;
3859 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3860 ipvs->sysctl_am_droprate = 10;
3861 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3862 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3863 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3864 #ifdef CONFIG_IP_VS_NFCT
3865 tbl[idx++].data = &ipvs->sysctl_conntrack;
3867 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3868 ipvs->sysctl_snat_reroute = 1;
3869 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3870 ipvs->sysctl_sync_ver = 1;
3871 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3872 ipvs->sysctl_sync_ports = 1;
3873 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3874 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3875 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3876 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3877 ipvs->sysctl_sync_sock_size = 0;
3878 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3879 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3880 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3881 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3882 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3883 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3884 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3885 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3886 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3887 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3888 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3889 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3890 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3891 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3892 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3893 ipvs->sysctl_pmtu_disc = 1;
3894 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3895 tbl[idx++].data = &ipvs->sysctl_backup_only;
3896 ipvs->sysctl_conn_reuse_mode = 1;
3897 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
3900 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3901 if (ipvs->sysctl_hdr == NULL) {
3902 if (!net_eq(net, &init_net))
3906 ip_vs_start_estimator(net, &ipvs->tot_stats);
3907 ipvs->sysctl_tbl = tbl;
3908 /* Schedule defense work */
3909 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3910 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3915 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
3917 struct netns_ipvs *ipvs = net_ipvs(net);
3919 cancel_delayed_work_sync(&ipvs->defense_work);
3920 cancel_work_sync(&ipvs->defense_work.work);
3921 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3922 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3924 if (!net_eq(net, &init_net))
3925 kfree(ipvs->sysctl_tbl);
3930 static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3931 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3935 static struct notifier_block ip_vs_dst_notifier = {
3936 .notifier_call = ip_vs_dst_event,
3939 int __net_init ip_vs_control_net_init(struct net *net)
3942 struct netns_ipvs *ipvs = net_ipvs(net);
3944 /* Initialize rs_table */
3945 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3946 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3948 INIT_LIST_HEAD(&ipvs->dest_trash);
3949 spin_lock_init(&ipvs->dest_trash_lock);
3950 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3951 (unsigned long) net);
3952 atomic_set(&ipvs->ftpsvc_counter, 0);
3953 atomic_set(&ipvs->nullsvc_counter, 0);
3956 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3957 if (!ipvs->tot_stats.cpustats)
3960 for_each_possible_cpu(i) {
3961 struct ip_vs_cpu_stats *ipvs_tot_stats;
3962 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3963 u64_stats_init(&ipvs_tot_stats->syncp);
3966 spin_lock_init(&ipvs->tot_stats.lock);
3968 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3969 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3970 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3971 &ip_vs_stats_percpu_fops);
3973 if (ip_vs_control_net_init_sysctl(net))
3979 free_percpu(ipvs->tot_stats.cpustats);
3983 void __net_exit ip_vs_control_net_cleanup(struct net *net)
3985 struct netns_ipvs *ipvs = net_ipvs(net);
3987 ip_vs_trash_cleanup(net);
3988 ip_vs_control_net_cleanup_sysctl(net);
3989 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3990 remove_proc_entry("ip_vs_stats", net->proc_net);
3991 remove_proc_entry("ip_vs", net->proc_net);
3992 free_percpu(ipvs->tot_stats.cpustats);
3995 int __init ip_vs_register_nl_ioctl(void)
3999 ret = nf_register_sockopt(&ip_vs_sockopts);
4001 pr_err("cannot register sockopt.\n");
4005 ret = ip_vs_genl_register();
4007 pr_err("cannot register Generic Netlink interface.\n");
4013 nf_unregister_sockopt(&ip_vs_sockopts);
4018 void ip_vs_unregister_nl_ioctl(void)
4020 ip_vs_genl_unregister();
4021 nf_unregister_sockopt(&ip_vs_sockopts);
4024 int __init ip_vs_control_init(void)
4031 /* Initialize svc_table, ip_vs_svc_fwm_table */
4032 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4033 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4034 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4037 smp_wmb(); /* Do we really need it now ? */
4039 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4048 void ip_vs_control_cleanup(void)
4051 unregister_netdevice_notifier(&ip_vs_dst_notifier);