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_stats_user *dst, struct ip_vs_stats *src)
734 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.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_zero_stats(struct ip_vs_stats *stats)
752 spin_lock_bh(&stats->lock);
754 /* get current counters as zero point, rates are zeroed */
756 #define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c
758 IP_VS_ZERO_STATS_COUNTER(conns);
759 IP_VS_ZERO_STATS_COUNTER(inpkts);
760 IP_VS_ZERO_STATS_COUNTER(outpkts);
761 IP_VS_ZERO_STATS_COUNTER(inbytes);
762 IP_VS_ZERO_STATS_COUNTER(outbytes);
764 ip_vs_zero_estimator(stats);
766 spin_unlock_bh(&stats->lock);
770 * Update a destination in the given service
773 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
774 struct ip_vs_dest_user_kern *udest, int add)
776 struct netns_ipvs *ipvs = net_ipvs(svc->net);
777 struct ip_vs_service *old_svc;
778 struct ip_vs_scheduler *sched;
781 /* We cannot modify an address and change the address family */
782 BUG_ON(!add && udest->af != dest->af);
784 if (add && udest->af != svc->af)
785 ipvs->mixed_address_family_dests++;
787 /* set the weight and the flags */
788 atomic_set(&dest->weight, udest->weight);
789 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
790 conn_flags |= IP_VS_CONN_F_INACTIVE;
792 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
793 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
794 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
797 * Put the real service in rs_table if not present.
798 * For now only for NAT!
800 ip_vs_rs_hash(ipvs, dest);
802 atomic_set(&dest->conn_flags, conn_flags);
804 /* bind the service */
805 old_svc = rcu_dereference_protected(dest->svc, 1);
807 __ip_vs_bind_svc(dest, svc);
809 if (old_svc != svc) {
810 ip_vs_zero_stats(&dest->stats);
811 __ip_vs_bind_svc(dest, svc);
812 __ip_vs_svc_put(old_svc, true);
816 /* set the dest status flags */
817 dest->flags |= IP_VS_DEST_F_AVAILABLE;
819 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
820 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
821 dest->u_threshold = udest->u_threshold;
822 dest->l_threshold = udest->l_threshold;
824 dest->af = udest->af;
826 spin_lock_bh(&dest->dst_lock);
827 __ip_vs_dst_cache_reset(dest);
828 spin_unlock_bh(&dest->dst_lock);
830 sched = rcu_dereference_protected(svc->scheduler, 1);
832 ip_vs_start_estimator(svc->net, &dest->stats);
833 list_add_rcu(&dest->n_list, &svc->destinations);
836 sched->add_dest(svc, dest);
839 sched->upd_dest(svc, dest);
845 * Create a destination for the given service
848 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
849 struct ip_vs_dest **dest_p)
851 struct ip_vs_dest *dest;
852 unsigned int atype, i;
856 #ifdef CONFIG_IP_VS_IPV6
857 if (udest->af == AF_INET6) {
858 atype = ipv6_addr_type(&udest->addr.in6);
859 if ((!(atype & IPV6_ADDR_UNICAST) ||
860 atype & IPV6_ADDR_LINKLOCAL) &&
861 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
866 atype = inet_addr_type(svc->net, udest->addr.ip);
867 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
871 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
875 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
876 if (!dest->stats.cpustats)
879 for_each_possible_cpu(i) {
880 struct ip_vs_cpu_stats *ip_vs_dest_stats;
881 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
882 u64_stats_init(&ip_vs_dest_stats->syncp);
885 dest->af = udest->af;
886 dest->protocol = svc->protocol;
887 dest->vaddr = svc->addr;
888 dest->vport = svc->port;
889 dest->vfwmark = svc->fwmark;
890 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
891 dest->port = udest->port;
893 atomic_set(&dest->activeconns, 0);
894 atomic_set(&dest->inactconns, 0);
895 atomic_set(&dest->persistconns, 0);
896 atomic_set(&dest->refcnt, 1);
898 INIT_HLIST_NODE(&dest->d_list);
899 spin_lock_init(&dest->dst_lock);
900 spin_lock_init(&dest->stats.lock);
901 __ip_vs_update_dest(svc, dest, udest, 1);
915 * Add a destination into an existing service
918 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
920 struct ip_vs_dest *dest;
921 union nf_inet_addr daddr;
922 __be16 dport = udest->port;
927 if (udest->weight < 0) {
928 pr_err("%s(): server weight less than zero\n", __func__);
932 if (udest->l_threshold > udest->u_threshold) {
933 pr_err("%s(): lower threshold is higher than upper threshold\n",
938 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
940 /* We use function that requires RCU lock */
942 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
946 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
951 * Check if the dest already exists in the trash and
952 * is from the same service
954 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
957 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
958 "dest->refcnt=%d, service %u/%s:%u\n",
959 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
960 atomic_read(&dest->refcnt),
962 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
965 __ip_vs_update_dest(svc, dest, udest, 1);
969 * Allocate and initialize the dest structure
971 ret = ip_vs_new_dest(svc, udest, &dest);
980 * Edit a destination in the given service
983 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
985 struct ip_vs_dest *dest;
986 union nf_inet_addr daddr;
987 __be16 dport = udest->port;
991 if (udest->weight < 0) {
992 pr_err("%s(): server weight less than zero\n", __func__);
996 if (udest->l_threshold > udest->u_threshold) {
997 pr_err("%s(): lower threshold is higher than upper threshold\n",
1002 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1004 /* We use function that requires RCU lock */
1006 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1010 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1014 __ip_vs_update_dest(svc, dest, udest, 0);
1021 * Delete a destination (must be already unlinked from the service)
1023 static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1026 struct netns_ipvs *ipvs = net_ipvs(net);
1028 ip_vs_stop_estimator(net, &dest->stats);
1031 * Remove it from the d-linked list with the real services.
1033 ip_vs_rs_unhash(dest);
1035 spin_lock_bh(&ipvs->dest_trash_lock);
1036 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1037 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1038 atomic_read(&dest->refcnt));
1039 if (list_empty(&ipvs->dest_trash) && !cleanup)
1040 mod_timer(&ipvs->dest_trash_timer,
1041 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1042 /* dest lives in trash without reference */
1043 list_add(&dest->t_list, &ipvs->dest_trash);
1044 dest->idle_start = 0;
1045 spin_unlock_bh(&ipvs->dest_trash_lock);
1046 ip_vs_dest_put(dest);
1051 * Unlink a destination from the given service
1053 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1054 struct ip_vs_dest *dest,
1057 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1060 * Remove it from the d-linked destination list.
1062 list_del_rcu(&dest->n_list);
1065 if (dest->af != svc->af)
1066 net_ipvs(svc->net)->mixed_address_family_dests--;
1069 struct ip_vs_scheduler *sched;
1071 sched = rcu_dereference_protected(svc->scheduler, 1);
1072 if (sched->del_dest)
1073 sched->del_dest(svc, dest);
1079 * Delete a destination server in the given service
1082 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1084 struct ip_vs_dest *dest;
1085 __be16 dport = udest->port;
1089 /* We use function that requires RCU lock */
1091 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1095 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1100 * Unlink dest from the service
1102 __ip_vs_unlink_dest(svc, dest, 1);
1105 * Delete the destination
1107 __ip_vs_del_dest(svc->net, dest, false);
1114 static void ip_vs_dest_trash_expire(unsigned long data)
1116 struct net *net = (struct net *) data;
1117 struct netns_ipvs *ipvs = net_ipvs(net);
1118 struct ip_vs_dest *dest, *next;
1119 unsigned long now = jiffies;
1121 spin_lock(&ipvs->dest_trash_lock);
1122 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1123 if (atomic_read(&dest->refcnt) > 0)
1125 if (dest->idle_start) {
1126 if (time_before(now, dest->idle_start +
1127 IP_VS_DEST_TRASH_PERIOD))
1130 dest->idle_start = max(1UL, now);
1133 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1135 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1137 list_del(&dest->t_list);
1138 ip_vs_dest_free(dest);
1140 if (!list_empty(&ipvs->dest_trash))
1141 mod_timer(&ipvs->dest_trash_timer,
1142 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1143 spin_unlock(&ipvs->dest_trash_lock);
1147 * Add a service into the service hash table
1150 ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1151 struct ip_vs_service **svc_p)
1154 struct ip_vs_scheduler *sched = NULL;
1155 struct ip_vs_pe *pe = NULL;
1156 struct ip_vs_service *svc = NULL;
1157 struct netns_ipvs *ipvs = net_ipvs(net);
1159 /* increase the module use count */
1160 ip_vs_use_count_inc();
1162 /* Lookup the scheduler by 'u->sched_name' */
1163 sched = ip_vs_scheduler_get(u->sched_name);
1164 if (sched == NULL) {
1165 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1170 if (u->pe_name && *u->pe_name) {
1171 pe = ip_vs_pe_getbyname(u->pe_name);
1173 pr_info("persistence engine module ip_vs_pe_%s "
1174 "not found\n", u->pe_name);
1180 #ifdef CONFIG_IP_VS_IPV6
1181 if (u->af == AF_INET6) {
1182 __u32 plen = (__force __u32) u->netmask;
1184 if (plen < 1 || plen > 128) {
1191 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1193 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1197 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1198 if (!svc->stats.cpustats) {
1203 for_each_possible_cpu(i) {
1204 struct ip_vs_cpu_stats *ip_vs_stats;
1205 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1206 u64_stats_init(&ip_vs_stats->syncp);
1210 /* I'm the first user of the service */
1211 atomic_set(&svc->refcnt, 0);
1214 svc->protocol = u->protocol;
1215 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1216 svc->port = u->port;
1217 svc->fwmark = u->fwmark;
1218 svc->flags = u->flags;
1219 svc->timeout = u->timeout * HZ;
1220 svc->netmask = u->netmask;
1223 INIT_LIST_HEAD(&svc->destinations);
1224 spin_lock_init(&svc->sched_lock);
1225 spin_lock_init(&svc->stats.lock);
1227 /* Bind the scheduler */
1228 ret = ip_vs_bind_scheduler(svc, sched);
1233 /* Bind the ct retriever */
1234 RCU_INIT_POINTER(svc->pe, pe);
1237 /* Update the virtual service counters */
1238 if (svc->port == FTPPORT)
1239 atomic_inc(&ipvs->ftpsvc_counter);
1240 else if (svc->port == 0)
1241 atomic_inc(&ipvs->nullsvc_counter);
1243 ip_vs_start_estimator(net, &svc->stats);
1245 /* Count only IPv4 services for old get/setsockopt interface */
1246 if (svc->af == AF_INET)
1247 ipvs->num_services++;
1249 /* Hash the service into the service table */
1250 ip_vs_svc_hash(svc);
1253 /* Now there is a service - full throttle */
1260 ip_vs_unbind_scheduler(svc, sched);
1261 ip_vs_service_free(svc);
1263 ip_vs_scheduler_put(sched);
1266 /* decrease the module use count */
1267 ip_vs_use_count_dec();
1274 * Edit a service and bind it with a new scheduler
1277 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1279 struct ip_vs_scheduler *sched, *old_sched;
1280 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1284 * Lookup the scheduler, by 'u->sched_name'
1286 sched = ip_vs_scheduler_get(u->sched_name);
1287 if (sched == NULL) {
1288 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1293 if (u->pe_name && *u->pe_name) {
1294 pe = ip_vs_pe_getbyname(u->pe_name);
1296 pr_info("persistence engine module ip_vs_pe_%s "
1297 "not found\n", u->pe_name);
1304 #ifdef CONFIG_IP_VS_IPV6
1305 if (u->af == AF_INET6) {
1306 __u32 plen = (__force __u32) u->netmask;
1308 if (plen < 1 || plen > 128) {
1315 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1316 if (sched != old_sched) {
1317 /* Bind the new scheduler */
1318 ret = ip_vs_bind_scheduler(svc, sched);
1323 /* Unbind the old scheduler on success */
1324 ip_vs_unbind_scheduler(svc, old_sched);
1328 * Set the flags and timeout value
1330 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1331 svc->timeout = u->timeout * HZ;
1332 svc->netmask = u->netmask;
1334 old_pe = rcu_dereference_protected(svc->pe, 1);
1336 rcu_assign_pointer(svc->pe, pe);
1339 ip_vs_scheduler_put(old_sched);
1340 ip_vs_pe_put(old_pe);
1345 * Delete a service from the service list
1346 * - The service must be unlinked, unlocked and not referenced!
1347 * - We are called under _bh lock
1349 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1351 struct ip_vs_dest *dest, *nxt;
1352 struct ip_vs_scheduler *old_sched;
1353 struct ip_vs_pe *old_pe;
1354 struct netns_ipvs *ipvs = net_ipvs(svc->net);
1356 pr_info("%s: enter\n", __func__);
1358 /* Count only IPv4 services for old get/setsockopt interface */
1359 if (svc->af == AF_INET)
1360 ipvs->num_services--;
1362 ip_vs_stop_estimator(svc->net, &svc->stats);
1364 /* Unbind scheduler */
1365 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1366 ip_vs_unbind_scheduler(svc, old_sched);
1367 ip_vs_scheduler_put(old_sched);
1369 /* Unbind persistence engine, keep svc->pe */
1370 old_pe = rcu_dereference_protected(svc->pe, 1);
1371 ip_vs_pe_put(old_pe);
1374 * Unlink the whole destination list
1376 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1377 __ip_vs_unlink_dest(svc, dest, 0);
1378 __ip_vs_del_dest(svc->net, dest, cleanup);
1382 * Update the virtual service counters
1384 if (svc->port == FTPPORT)
1385 atomic_dec(&ipvs->ftpsvc_counter);
1386 else if (svc->port == 0)
1387 atomic_dec(&ipvs->nullsvc_counter);
1390 * Free the service if nobody refers to it
1392 __ip_vs_svc_put(svc, true);
1394 /* decrease the module use count */
1395 ip_vs_use_count_dec();
1399 * Unlink a service from list and try to delete it if its refcnt reached 0
1401 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1403 /* Hold svc to avoid double release from dest_trash */
1404 atomic_inc(&svc->refcnt);
1406 * Unhash it from the service table
1408 ip_vs_svc_unhash(svc);
1410 __ip_vs_del_service(svc, cleanup);
1414 * Delete a service from the service list
1416 static int ip_vs_del_service(struct ip_vs_service *svc)
1420 ip_vs_unlink_service(svc, false);
1427 * Flush all the virtual services
1429 static int ip_vs_flush(struct net *net, bool cleanup)
1432 struct ip_vs_service *svc;
1433 struct hlist_node *n;
1436 * Flush the service table hashed by <netns,protocol,addr,port>
1438 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1439 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1441 if (net_eq(svc->net, net))
1442 ip_vs_unlink_service(svc, cleanup);
1447 * Flush the service table hashed by fwmark
1449 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1450 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1452 if (net_eq(svc->net, net))
1453 ip_vs_unlink_service(svc, cleanup);
1461 * Delete service by {netns} in the service table.
1462 * Called by __ip_vs_cleanup()
1464 void ip_vs_service_net_cleanup(struct net *net)
1467 /* Check for "full" addressed entries */
1468 mutex_lock(&__ip_vs_mutex);
1469 ip_vs_flush(net, true);
1470 mutex_unlock(&__ip_vs_mutex);
1474 /* Put all references for device (dst_cache) */
1476 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1478 struct ip_vs_dest_dst *dest_dst;
1480 spin_lock_bh(&dest->dst_lock);
1481 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1482 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1483 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1485 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1487 atomic_read(&dest->refcnt));
1488 __ip_vs_dst_cache_reset(dest);
1490 spin_unlock_bh(&dest->dst_lock);
1493 /* Netdev event receiver
1494 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1496 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1499 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1500 struct net *net = dev_net(dev);
1501 struct netns_ipvs *ipvs = net_ipvs(net);
1502 struct ip_vs_service *svc;
1503 struct ip_vs_dest *dest;
1506 if (event != NETDEV_DOWN || !ipvs)
1508 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1510 mutex_lock(&__ip_vs_mutex);
1511 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1512 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1513 if (net_eq(svc->net, net)) {
1514 list_for_each_entry(dest, &svc->destinations,
1516 ip_vs_forget_dev(dest, dev);
1521 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1522 if (net_eq(svc->net, net)) {
1523 list_for_each_entry(dest, &svc->destinations,
1525 ip_vs_forget_dev(dest, dev);
1532 spin_lock_bh(&ipvs->dest_trash_lock);
1533 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1534 ip_vs_forget_dev(dest, dev);
1536 spin_unlock_bh(&ipvs->dest_trash_lock);
1537 mutex_unlock(&__ip_vs_mutex);
1543 * Zero counters in a service or all services
1545 static int ip_vs_zero_service(struct ip_vs_service *svc)
1547 struct ip_vs_dest *dest;
1549 list_for_each_entry(dest, &svc->destinations, n_list) {
1550 ip_vs_zero_stats(&dest->stats);
1552 ip_vs_zero_stats(&svc->stats);
1556 static int ip_vs_zero_all(struct net *net)
1559 struct ip_vs_service *svc;
1561 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1562 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1563 if (net_eq(svc->net, net))
1564 ip_vs_zero_service(svc);
1568 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1569 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1570 if (net_eq(svc->net, net))
1571 ip_vs_zero_service(svc);
1575 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
1579 #ifdef CONFIG_SYSCTL
1582 static int three = 3;
1585 proc_do_defense_mode(struct ctl_table *table, int write,
1586 void __user *buffer, size_t *lenp, loff_t *ppos)
1588 struct net *net = current->nsproxy->net_ns;
1589 int *valp = table->data;
1593 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1594 if (write && (*valp != val)) {
1595 if ((*valp < 0) || (*valp > 3)) {
1596 /* Restore the correct value */
1599 update_defense_level(net_ipvs(net));
1606 proc_do_sync_threshold(struct ctl_table *table, int write,
1607 void __user *buffer, size_t *lenp, loff_t *ppos)
1609 int *valp = table->data;
1613 /* backup the value first */
1614 memcpy(val, valp, sizeof(val));
1616 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1617 if (write && (valp[0] < 0 || valp[1] < 0 ||
1618 (valp[0] >= valp[1] && valp[1]))) {
1619 /* Restore the correct value */
1620 memcpy(valp, val, sizeof(val));
1626 proc_do_sync_mode(struct ctl_table *table, int write,
1627 void __user *buffer, size_t *lenp, loff_t *ppos)
1629 int *valp = table->data;
1633 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1634 if (write && (*valp != val)) {
1635 if ((*valp < 0) || (*valp > 1)) {
1636 /* Restore the correct value */
1644 proc_do_sync_ports(struct ctl_table *table, int write,
1645 void __user *buffer, size_t *lenp, loff_t *ppos)
1647 int *valp = table->data;
1651 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1652 if (write && (*valp != val)) {
1653 if (*valp < 1 || !is_power_of_2(*valp)) {
1654 /* Restore the correct value */
1662 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1663 * Do not change order or insert new entries without
1664 * align with netns init in ip_vs_control_net_init()
1667 static struct ctl_table vs_vars[] = {
1669 .procname = "amemthresh",
1670 .maxlen = sizeof(int),
1672 .proc_handler = proc_dointvec,
1675 .procname = "am_droprate",
1676 .maxlen = sizeof(int),
1678 .proc_handler = proc_dointvec,
1681 .procname = "drop_entry",
1682 .maxlen = sizeof(int),
1684 .proc_handler = proc_do_defense_mode,
1687 .procname = "drop_packet",
1688 .maxlen = sizeof(int),
1690 .proc_handler = proc_do_defense_mode,
1692 #ifdef CONFIG_IP_VS_NFCT
1694 .procname = "conntrack",
1695 .maxlen = sizeof(int),
1697 .proc_handler = &proc_dointvec,
1701 .procname = "secure_tcp",
1702 .maxlen = sizeof(int),
1704 .proc_handler = proc_do_defense_mode,
1707 .procname = "snat_reroute",
1708 .maxlen = sizeof(int),
1710 .proc_handler = &proc_dointvec,
1713 .procname = "sync_version",
1714 .maxlen = sizeof(int),
1716 .proc_handler = &proc_do_sync_mode,
1719 .procname = "sync_ports",
1720 .maxlen = sizeof(int),
1722 .proc_handler = &proc_do_sync_ports,
1725 .procname = "sync_persist_mode",
1726 .maxlen = sizeof(int),
1728 .proc_handler = proc_dointvec,
1731 .procname = "sync_qlen_max",
1732 .maxlen = sizeof(unsigned long),
1734 .proc_handler = proc_doulongvec_minmax,
1737 .procname = "sync_sock_size",
1738 .maxlen = sizeof(int),
1740 .proc_handler = proc_dointvec,
1743 .procname = "cache_bypass",
1744 .maxlen = sizeof(int),
1746 .proc_handler = proc_dointvec,
1749 .procname = "expire_nodest_conn",
1750 .maxlen = sizeof(int),
1752 .proc_handler = proc_dointvec,
1755 .procname = "sloppy_tcp",
1756 .maxlen = sizeof(int),
1758 .proc_handler = proc_dointvec,
1761 .procname = "sloppy_sctp",
1762 .maxlen = sizeof(int),
1764 .proc_handler = proc_dointvec,
1767 .procname = "expire_quiescent_template",
1768 .maxlen = sizeof(int),
1770 .proc_handler = proc_dointvec,
1773 .procname = "sync_threshold",
1775 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1777 .proc_handler = proc_do_sync_threshold,
1780 .procname = "sync_refresh_period",
1781 .maxlen = sizeof(int),
1783 .proc_handler = proc_dointvec_jiffies,
1786 .procname = "sync_retries",
1787 .maxlen = sizeof(int),
1789 .proc_handler = proc_dointvec_minmax,
1794 .procname = "nat_icmp_send",
1795 .maxlen = sizeof(int),
1797 .proc_handler = proc_dointvec,
1800 .procname = "pmtu_disc",
1801 .maxlen = sizeof(int),
1803 .proc_handler = proc_dointvec,
1806 .procname = "backup_only",
1807 .maxlen = sizeof(int),
1809 .proc_handler = proc_dointvec,
1811 #ifdef CONFIG_IP_VS_DEBUG
1813 .procname = "debug_level",
1814 .data = &sysctl_ip_vs_debug_level,
1815 .maxlen = sizeof(int),
1817 .proc_handler = proc_dointvec,
1825 #ifdef CONFIG_PROC_FS
1828 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1829 struct hlist_head *table;
1834 * Write the contents of the VS rule table to a PROCfs file.
1835 * (It is kept just for backward compatibility)
1837 static inline const char *ip_vs_fwd_name(unsigned int flags)
1839 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1840 case IP_VS_CONN_F_LOCALNODE:
1842 case IP_VS_CONN_F_TUNNEL:
1844 case IP_VS_CONN_F_DROUTE:
1852 /* Get the Nth entry in the two lists */
1853 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1855 struct net *net = seq_file_net(seq);
1856 struct ip_vs_iter *iter = seq->private;
1858 struct ip_vs_service *svc;
1860 /* look in hash by protocol */
1861 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1862 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1863 if (net_eq(svc->net, net) && pos-- == 0) {
1864 iter->table = ip_vs_svc_table;
1871 /* keep looking in fwmark */
1872 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1873 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1875 if (net_eq(svc->net, net) && pos-- == 0) {
1876 iter->table = ip_vs_svc_fwm_table;
1886 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1890 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1894 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1896 struct hlist_node *e;
1897 struct ip_vs_iter *iter;
1898 struct ip_vs_service *svc;
1901 if (v == SEQ_START_TOKEN)
1902 return ip_vs_info_array(seq,0);
1905 iter = seq->private;
1907 if (iter->table == ip_vs_svc_table) {
1908 /* next service in table hashed by protocol */
1909 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1911 return hlist_entry(e, struct ip_vs_service, s_list);
1913 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1914 hlist_for_each_entry_rcu(svc,
1915 &ip_vs_svc_table[iter->bucket],
1921 iter->table = ip_vs_svc_fwm_table;
1926 /* next service in hashed by fwmark */
1927 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
1929 return hlist_entry(e, struct ip_vs_service, f_list);
1932 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1933 hlist_for_each_entry_rcu(svc,
1934 &ip_vs_svc_fwm_table[iter->bucket],
1942 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1949 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1951 if (v == SEQ_START_TOKEN) {
1953 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1954 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1956 "Prot LocalAddress:Port Scheduler Flags\n");
1958 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1960 const struct ip_vs_service *svc = v;
1961 const struct ip_vs_iter *iter = seq->private;
1962 const struct ip_vs_dest *dest;
1963 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
1965 if (iter->table == ip_vs_svc_table) {
1966 #ifdef CONFIG_IP_VS_IPV6
1967 if (svc->af == AF_INET6)
1968 seq_printf(seq, "%s [%pI6]:%04X %s ",
1969 ip_vs_proto_name(svc->protocol),
1975 seq_printf(seq, "%s %08X:%04X %s %s ",
1976 ip_vs_proto_name(svc->protocol),
1977 ntohl(svc->addr.ip),
1980 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1982 seq_printf(seq, "FWM %08X %s %s",
1983 svc->fwmark, sched->name,
1984 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1987 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1988 seq_printf(seq, "persistent %d %08X\n",
1990 ntohl(svc->netmask));
1992 seq_putc(seq, '\n');
1994 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
1995 #ifdef CONFIG_IP_VS_IPV6
1996 if (dest->af == AF_INET6)
1999 " %-7s %-6d %-10d %-10d\n",
2002 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2003 atomic_read(&dest->weight),
2004 atomic_read(&dest->activeconns),
2005 atomic_read(&dest->inactconns));
2010 "%-7s %-6d %-10d %-10d\n",
2011 ntohl(dest->addr.ip),
2013 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2014 atomic_read(&dest->weight),
2015 atomic_read(&dest->activeconns),
2016 atomic_read(&dest->inactconns));
2023 static const struct seq_operations ip_vs_info_seq_ops = {
2024 .start = ip_vs_info_seq_start,
2025 .next = ip_vs_info_seq_next,
2026 .stop = ip_vs_info_seq_stop,
2027 .show = ip_vs_info_seq_show,
2030 static int ip_vs_info_open(struct inode *inode, struct file *file)
2032 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2033 sizeof(struct ip_vs_iter));
2036 static const struct file_operations ip_vs_info_fops = {
2037 .owner = THIS_MODULE,
2038 .open = ip_vs_info_open,
2040 .llseek = seq_lseek,
2041 .release = seq_release_net,
2044 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2046 struct net *net = seq_file_single_net(seq);
2047 struct ip_vs_stats_user show;
2049 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2051 " Total Incoming Outgoing Incoming Outgoing\n");
2053 " Conns Packets Packets Bytes Bytes\n");
2055 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2056 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2057 show.inpkts, show.outpkts,
2058 (unsigned long long) show.inbytes,
2059 (unsigned long long) show.outbytes);
2061 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2063 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2064 seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2065 show.cps, show.inpps, show.outpps,
2066 show.inbps, show.outbps);
2071 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2073 return single_open_net(inode, file, ip_vs_stats_show);
2076 static const struct file_operations ip_vs_stats_fops = {
2077 .owner = THIS_MODULE,
2078 .open = ip_vs_stats_seq_open,
2080 .llseek = seq_lseek,
2081 .release = single_release_net,
2084 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2086 struct net *net = seq_file_single_net(seq);
2087 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2088 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2089 struct ip_vs_stats_user rates;
2092 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2094 " Total Incoming Outgoing Incoming Outgoing\n");
2096 "CPU Conns Packets Packets Bytes Bytes\n");
2098 for_each_possible_cpu(i) {
2099 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2101 __u64 inbytes, outbytes;
2104 start = u64_stats_fetch_begin_irq(&u->syncp);
2105 inbytes = u->ustats.inbytes;
2106 outbytes = u->ustats.outbytes;
2107 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2109 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
2110 i, u->ustats.conns, u->ustats.inpkts,
2111 u->ustats.outpkts, (__u64)inbytes,
2115 spin_lock_bh(&tot_stats->lock);
2117 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2118 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2119 tot_stats->ustats.outpkts,
2120 (unsigned long long) tot_stats->ustats.inbytes,
2121 (unsigned long long) tot_stats->ustats.outbytes);
2123 ip_vs_read_estimator(&rates, tot_stats);
2125 spin_unlock_bh(&tot_stats->lock);
2127 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2129 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2130 seq_printf(seq, " %8X %8X %8X %16X %16X\n",
2140 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2142 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2145 static const struct file_operations ip_vs_stats_percpu_fops = {
2146 .owner = THIS_MODULE,
2147 .open = ip_vs_stats_percpu_seq_open,
2149 .llseek = seq_lseek,
2150 .release = single_release_net,
2155 * Set timeout values for tcp tcpfin udp in the timeout_table.
2157 static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
2159 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2160 struct ip_vs_proto_data *pd;
2163 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2168 #ifdef CONFIG_IP_VS_PROTO_TCP
2169 if (u->tcp_timeout) {
2170 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2171 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2172 = u->tcp_timeout * HZ;
2175 if (u->tcp_fin_timeout) {
2176 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2177 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2178 = u->tcp_fin_timeout * HZ;
2182 #ifdef CONFIG_IP_VS_PROTO_UDP
2183 if (u->udp_timeout) {
2184 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2185 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2186 = u->udp_timeout * HZ;
2192 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2194 struct ip_vs_svcdest_user {
2195 struct ip_vs_service_user s;
2196 struct ip_vs_dest_user d;
2199 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2200 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2201 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2202 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2203 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2204 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2205 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2206 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2207 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2208 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2209 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2212 union ip_vs_set_arglen {
2213 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2214 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2215 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2216 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2217 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2218 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2219 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2220 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2221 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2222 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2225 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2227 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2228 struct ip_vs_service_user *usvc_compat)
2230 memset(usvc, 0, sizeof(*usvc));
2233 usvc->protocol = usvc_compat->protocol;
2234 usvc->addr.ip = usvc_compat->addr;
2235 usvc->port = usvc_compat->port;
2236 usvc->fwmark = usvc_compat->fwmark;
2238 /* Deep copy of sched_name is not needed here */
2239 usvc->sched_name = usvc_compat->sched_name;
2241 usvc->flags = usvc_compat->flags;
2242 usvc->timeout = usvc_compat->timeout;
2243 usvc->netmask = usvc_compat->netmask;
2246 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2247 struct ip_vs_dest_user *udest_compat)
2249 memset(udest, 0, sizeof(*udest));
2251 udest->addr.ip = udest_compat->addr;
2252 udest->port = udest_compat->port;
2253 udest->conn_flags = udest_compat->conn_flags;
2254 udest->weight = udest_compat->weight;
2255 udest->u_threshold = udest_compat->u_threshold;
2256 udest->l_threshold = udest_compat->l_threshold;
2257 udest->af = AF_INET;
2261 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2263 struct net *net = sock_net(sk);
2265 unsigned char arg[MAX_SET_ARGLEN];
2266 struct ip_vs_service_user *usvc_compat;
2267 struct ip_vs_service_user_kern usvc;
2268 struct ip_vs_service *svc;
2269 struct ip_vs_dest_user *udest_compat;
2270 struct ip_vs_dest_user_kern udest;
2271 struct netns_ipvs *ipvs = net_ipvs(net);
2273 BUILD_BUG_ON(sizeof(arg) > 255);
2274 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2277 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2279 if (len != set_arglen[CMDID(cmd)]) {
2280 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2281 len, set_arglen[CMDID(cmd)]);
2285 if (copy_from_user(arg, user, len) != 0)
2288 /* increase the module use count */
2289 ip_vs_use_count_inc();
2291 /* Handle daemons since they have another lock */
2292 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2293 cmd == IP_VS_SO_SET_STOPDAEMON) {
2294 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2296 mutex_lock(&ipvs->sync_mutex);
2297 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2298 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2301 ret = stop_sync_thread(net, dm->state);
2302 mutex_unlock(&ipvs->sync_mutex);
2306 mutex_lock(&__ip_vs_mutex);
2307 if (cmd == IP_VS_SO_SET_FLUSH) {
2308 /* Flush the virtual service */
2309 ret = ip_vs_flush(net, false);
2311 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2312 /* Set timeout values for (tcp tcpfin udp) */
2313 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
2317 usvc_compat = (struct ip_vs_service_user *)arg;
2318 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2320 /* We only use the new structs internally, so copy userspace compat
2321 * structs to extended internal versions */
2322 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2323 ip_vs_copy_udest_compat(&udest, udest_compat);
2325 if (cmd == IP_VS_SO_SET_ZERO) {
2326 /* if no service address is set, zero counters in all */
2327 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2328 ret = ip_vs_zero_all(net);
2333 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2334 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2335 usvc.protocol != IPPROTO_SCTP) {
2336 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2337 usvc.protocol, &usvc.addr.ip,
2338 ntohs(usvc.port), usvc.sched_name);
2343 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2345 if (usvc.fwmark == 0)
2346 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2347 &usvc.addr, usvc.port);
2349 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
2352 if (cmd != IP_VS_SO_SET_ADD
2353 && (svc == NULL || svc->protocol != usvc.protocol)) {
2359 case IP_VS_SO_SET_ADD:
2363 ret = ip_vs_add_service(net, &usvc, &svc);
2365 case IP_VS_SO_SET_EDIT:
2366 ret = ip_vs_edit_service(svc, &usvc);
2368 case IP_VS_SO_SET_DEL:
2369 ret = ip_vs_del_service(svc);
2373 case IP_VS_SO_SET_ZERO:
2374 ret = ip_vs_zero_service(svc);
2376 case IP_VS_SO_SET_ADDDEST:
2377 ret = ip_vs_add_dest(svc, &udest);
2379 case IP_VS_SO_SET_EDITDEST:
2380 ret = ip_vs_edit_dest(svc, &udest);
2382 case IP_VS_SO_SET_DELDEST:
2383 ret = ip_vs_del_dest(svc, &udest);
2390 mutex_unlock(&__ip_vs_mutex);
2392 /* decrease the module use count */
2393 ip_vs_use_count_dec();
2400 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2402 struct ip_vs_scheduler *sched;
2404 sched = rcu_dereference_protected(src->scheduler, 1);
2405 dst->protocol = src->protocol;
2406 dst->addr = src->addr.ip;
2407 dst->port = src->port;
2408 dst->fwmark = src->fwmark;
2409 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
2410 dst->flags = src->flags;
2411 dst->timeout = src->timeout / HZ;
2412 dst->netmask = src->netmask;
2413 dst->num_dests = src->num_dests;
2414 ip_vs_copy_stats(&dst->stats, &src->stats);
2418 __ip_vs_get_service_entries(struct net *net,
2419 const struct ip_vs_get_services *get,
2420 struct ip_vs_get_services __user *uptr)
2423 struct ip_vs_service *svc;
2424 struct ip_vs_service_entry entry;
2427 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2428 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2429 /* Only expose IPv4 entries to old interface */
2430 if (svc->af != AF_INET || !net_eq(svc->net, net))
2433 if (count >= get->num_services)
2435 memset(&entry, 0, sizeof(entry));
2436 ip_vs_copy_service(&entry, svc);
2437 if (copy_to_user(&uptr->entrytable[count],
2438 &entry, sizeof(entry))) {
2446 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2447 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2448 /* Only expose IPv4 entries to old interface */
2449 if (svc->af != AF_INET || !net_eq(svc->net, net))
2452 if (count >= get->num_services)
2454 memset(&entry, 0, sizeof(entry));
2455 ip_vs_copy_service(&entry, svc);
2456 if (copy_to_user(&uptr->entrytable[count],
2457 &entry, sizeof(entry))) {
2469 __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2470 struct ip_vs_get_dests __user *uptr)
2472 struct ip_vs_service *svc;
2473 union nf_inet_addr addr = { .ip = get->addr };
2478 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
2480 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2486 struct ip_vs_dest *dest;
2487 struct ip_vs_dest_entry entry;
2489 memset(&entry, 0, sizeof(entry));
2490 list_for_each_entry(dest, &svc->destinations, n_list) {
2491 if (count >= get->num_dests)
2494 /* Cannot expose heterogeneous members via sockopt
2497 if (dest->af != svc->af)
2500 entry.addr = dest->addr.ip;
2501 entry.port = dest->port;
2502 entry.conn_flags = atomic_read(&dest->conn_flags);
2503 entry.weight = atomic_read(&dest->weight);
2504 entry.u_threshold = dest->u_threshold;
2505 entry.l_threshold = dest->l_threshold;
2506 entry.activeconns = atomic_read(&dest->activeconns);
2507 entry.inactconns = atomic_read(&dest->inactconns);
2508 entry.persistconns = atomic_read(&dest->persistconns);
2509 ip_vs_copy_stats(&entry.stats, &dest->stats);
2510 if (copy_to_user(&uptr->entrytable[count],
2511 &entry, sizeof(entry))) {
2523 __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
2525 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2526 struct ip_vs_proto_data *pd;
2529 memset(u, 0, sizeof (*u));
2531 #ifdef CONFIG_IP_VS_PROTO_TCP
2532 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2533 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2534 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2536 #ifdef CONFIG_IP_VS_PROTO_UDP
2537 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2539 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2543 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2544 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2545 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2546 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2547 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2548 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2549 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2550 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2553 union ip_vs_get_arglen {
2554 char field_IP_VS_SO_GET_VERSION[64];
2555 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2556 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2557 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2558 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2559 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2560 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2563 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2566 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2568 unsigned char arg[MAX_GET_ARGLEN];
2570 unsigned int copylen;
2571 struct net *net = sock_net(sk);
2572 struct netns_ipvs *ipvs = net_ipvs(net);
2575 BUILD_BUG_ON(sizeof(arg) > 255);
2576 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2579 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2582 copylen = get_arglen[CMDID(cmd)];
2583 if (*len < (int) copylen) {
2584 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2588 if (copy_from_user(arg, user, copylen) != 0)
2591 * Handle daemons first since it has its own locking
2593 if (cmd == IP_VS_SO_GET_DAEMON) {
2594 struct ip_vs_daemon_user d[2];
2596 memset(&d, 0, sizeof(d));
2597 mutex_lock(&ipvs->sync_mutex);
2598 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2599 d[0].state = IP_VS_STATE_MASTER;
2600 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2601 sizeof(d[0].mcast_ifn));
2602 d[0].syncid = ipvs->master_syncid;
2604 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2605 d[1].state = IP_VS_STATE_BACKUP;
2606 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2607 sizeof(d[1].mcast_ifn));
2608 d[1].syncid = ipvs->backup_syncid;
2610 if (copy_to_user(user, &d, sizeof(d)) != 0)
2612 mutex_unlock(&ipvs->sync_mutex);
2616 mutex_lock(&__ip_vs_mutex);
2618 case IP_VS_SO_GET_VERSION:
2622 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2623 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2624 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2628 *len = strlen(buf)+1;
2632 case IP_VS_SO_GET_INFO:
2634 struct ip_vs_getinfo info;
2635 info.version = IP_VS_VERSION_CODE;
2636 info.size = ip_vs_conn_tab_size;
2637 info.num_services = ipvs->num_services;
2638 if (copy_to_user(user, &info, sizeof(info)) != 0)
2643 case IP_VS_SO_GET_SERVICES:
2645 struct ip_vs_get_services *get;
2648 get = (struct ip_vs_get_services *)arg;
2649 size = sizeof(*get) +
2650 sizeof(struct ip_vs_service_entry) * get->num_services;
2652 pr_err("length: %u != %u\n", *len, size);
2656 ret = __ip_vs_get_service_entries(net, get, user);
2660 case IP_VS_SO_GET_SERVICE:
2662 struct ip_vs_service_entry *entry;
2663 struct ip_vs_service *svc;
2664 union nf_inet_addr addr;
2666 entry = (struct ip_vs_service_entry *)arg;
2667 addr.ip = entry->addr;
2670 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
2672 svc = __ip_vs_service_find(net, AF_INET,
2673 entry->protocol, &addr,
2677 ip_vs_copy_service(entry, svc);
2678 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2685 case IP_VS_SO_GET_DESTS:
2687 struct ip_vs_get_dests *get;
2690 get = (struct ip_vs_get_dests *)arg;
2691 size = sizeof(*get) +
2692 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2694 pr_err("length: %u != %u\n", *len, size);
2698 ret = __ip_vs_get_dest_entries(net, get, user);
2702 case IP_VS_SO_GET_TIMEOUT:
2704 struct ip_vs_timeout_user t;
2706 __ip_vs_get_timeouts(net, &t);
2707 if (copy_to_user(user, &t, sizeof(t)) != 0)
2717 mutex_unlock(&__ip_vs_mutex);
2722 static struct nf_sockopt_ops ip_vs_sockopts = {
2724 .set_optmin = IP_VS_BASE_CTL,
2725 .set_optmax = IP_VS_SO_SET_MAX+1,
2726 .set = do_ip_vs_set_ctl,
2727 .get_optmin = IP_VS_BASE_CTL,
2728 .get_optmax = IP_VS_SO_GET_MAX+1,
2729 .get = do_ip_vs_get_ctl,
2730 .owner = THIS_MODULE,
2734 * Generic Netlink interface
2737 /* IPVS genetlink family */
2738 static struct genl_family ip_vs_genl_family = {
2739 .id = GENL_ID_GENERATE,
2741 .name = IPVS_GENL_NAME,
2742 .version = IPVS_GENL_VERSION,
2743 .maxattr = IPVS_CMD_MAX,
2744 .netnsok = true, /* Make ipvsadm to work on netns */
2747 /* Policy used for first-level command attributes */
2748 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2749 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2750 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2751 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2752 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2753 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2754 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2757 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2758 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2759 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2760 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2761 .len = IP_VS_IFNAME_MAXLEN },
2762 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2765 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2766 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2767 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2768 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2769 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2770 .len = sizeof(union nf_inet_addr) },
2771 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2772 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2773 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2774 .len = IP_VS_SCHEDNAME_MAXLEN },
2775 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2776 .len = IP_VS_PENAME_MAXLEN },
2777 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2778 .len = sizeof(struct ip_vs_flags) },
2779 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2780 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2781 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2784 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2785 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2786 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2787 .len = sizeof(union nf_inet_addr) },
2788 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2789 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2790 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2791 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2792 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2793 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2794 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2795 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2796 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2797 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2800 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2801 struct ip_vs_stats *stats)
2803 struct ip_vs_stats_user ustats;
2804 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2808 ip_vs_copy_stats(&ustats, stats);
2810 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) ||
2811 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) ||
2812 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) ||
2813 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) ||
2814 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) ||
2815 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) ||
2816 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) ||
2817 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) ||
2818 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) ||
2819 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps))
2820 goto nla_put_failure;
2821 nla_nest_end(skb, nl_stats);
2826 nla_nest_cancel(skb, nl_stats);
2830 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2831 struct ip_vs_service *svc)
2833 struct ip_vs_scheduler *sched;
2834 struct ip_vs_pe *pe;
2835 struct nlattr *nl_service;
2836 struct ip_vs_flags flags = { .flags = svc->flags,
2839 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2843 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2844 goto nla_put_failure;
2846 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2847 goto nla_put_failure;
2849 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2850 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2851 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2852 goto nla_put_failure;
2855 sched = rcu_dereference_protected(svc->scheduler, 1);
2856 pe = rcu_dereference_protected(svc->pe, 1);
2857 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
2858 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2859 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2860 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2861 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2862 goto nla_put_failure;
2863 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2864 goto nla_put_failure;
2866 nla_nest_end(skb, nl_service);
2871 nla_nest_cancel(skb, nl_service);
2875 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2876 struct ip_vs_service *svc,
2877 struct netlink_callback *cb)
2881 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2882 &ip_vs_genl_family, NLM_F_MULTI,
2883 IPVS_CMD_NEW_SERVICE);
2887 if (ip_vs_genl_fill_service(skb, svc) < 0)
2888 goto nla_put_failure;
2890 return genlmsg_end(skb, hdr);
2893 genlmsg_cancel(skb, hdr);
2897 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2898 struct netlink_callback *cb)
2901 int start = cb->args[0];
2902 struct ip_vs_service *svc;
2903 struct net *net = skb_sknet(skb);
2905 mutex_lock(&__ip_vs_mutex);
2906 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2907 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2908 if (++idx <= start || !net_eq(svc->net, net))
2910 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2912 goto nla_put_failure;
2917 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2918 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2919 if (++idx <= start || !net_eq(svc->net, net))
2921 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2923 goto nla_put_failure;
2929 mutex_unlock(&__ip_vs_mutex);
2935 static int ip_vs_genl_parse_service(struct net *net,
2936 struct ip_vs_service_user_kern *usvc,
2937 struct nlattr *nla, int full_entry,
2938 struct ip_vs_service **ret_svc)
2940 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2941 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2942 struct ip_vs_service *svc;
2944 /* Parse mandatory identifying service fields first */
2946 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2949 nla_af = attrs[IPVS_SVC_ATTR_AF];
2950 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2951 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2952 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2953 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2955 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2958 memset(usvc, 0, sizeof(*usvc));
2960 usvc->af = nla_get_u16(nla_af);
2961 #ifdef CONFIG_IP_VS_IPV6
2962 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2964 if (usvc->af != AF_INET)
2966 return -EAFNOSUPPORT;
2969 usvc->protocol = IPPROTO_TCP;
2970 usvc->fwmark = nla_get_u32(nla_fwmark);
2972 usvc->protocol = nla_get_u16(nla_protocol);
2973 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2974 usvc->port = nla_get_be16(nla_port);
2980 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
2982 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
2983 &usvc->addr, usvc->port);
2987 /* If a full entry was requested, check for the additional fields */
2989 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
2991 struct ip_vs_flags flags;
2993 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2994 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
2995 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2996 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2997 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2999 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3002 nla_memcpy(&flags, nla_flags, sizeof(flags));
3004 /* prefill flags from service if it already exists */
3006 usvc->flags = svc->flags;
3008 /* set new flags from userland */
3009 usvc->flags = (usvc->flags & ~flags.mask) |
3010 (flags.flags & flags.mask);
3011 usvc->sched_name = nla_data(nla_sched);
3012 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3013 usvc->timeout = nla_get_u32(nla_timeout);
3014 usvc->netmask = nla_get_be32(nla_netmask);
3020 static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3023 struct ip_vs_service_user_kern usvc;
3024 struct ip_vs_service *svc;
3027 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3028 return ret ? ERR_PTR(ret) : svc;
3031 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3033 struct nlattr *nl_dest;
3035 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3039 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3040 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3041 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3042 (atomic_read(&dest->conn_flags) &
3043 IP_VS_CONN_F_FWD_MASK)) ||
3044 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3045 atomic_read(&dest->weight)) ||
3046 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3047 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3048 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3049 atomic_read(&dest->activeconns)) ||
3050 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3051 atomic_read(&dest->inactconns)) ||
3052 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3053 atomic_read(&dest->persistconns)) ||
3054 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3055 goto nla_put_failure;
3056 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
3057 goto nla_put_failure;
3059 nla_nest_end(skb, nl_dest);
3064 nla_nest_cancel(skb, nl_dest);
3068 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3069 struct netlink_callback *cb)
3073 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3074 &ip_vs_genl_family, NLM_F_MULTI,
3079 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3080 goto nla_put_failure;
3082 return genlmsg_end(skb, hdr);
3085 genlmsg_cancel(skb, hdr);
3089 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3090 struct netlink_callback *cb)
3093 int start = cb->args[0];
3094 struct ip_vs_service *svc;
3095 struct ip_vs_dest *dest;
3096 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3097 struct net *net = skb_sknet(skb);
3099 mutex_lock(&__ip_vs_mutex);
3101 /* Try to find the service for which to dump destinations */
3102 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3103 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3107 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3108 if (IS_ERR(svc) || svc == NULL)
3111 /* Dump the destinations */
3112 list_for_each_entry(dest, &svc->destinations, n_list) {
3115 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3117 goto nla_put_failure;
3125 mutex_unlock(&__ip_vs_mutex);
3130 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3131 struct nlattr *nla, int full_entry)
3133 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3134 struct nlattr *nla_addr, *nla_port;
3135 struct nlattr *nla_addr_family;
3137 /* Parse mandatory identifying destination fields first */
3139 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3142 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3143 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3144 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3146 if (!(nla_addr && nla_port))
3149 memset(udest, 0, sizeof(*udest));
3151 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3152 udest->port = nla_get_be16(nla_port);
3154 if (nla_addr_family)
3155 udest->af = nla_get_u16(nla_addr_family);
3159 /* If a full entry was requested, check for the additional fields */
3161 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3164 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3165 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3166 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3167 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3169 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3172 udest->conn_flags = nla_get_u32(nla_fwd)
3173 & IP_VS_CONN_F_FWD_MASK;
3174 udest->weight = nla_get_u32(nla_weight);
3175 udest->u_threshold = nla_get_u32(nla_u_thresh);
3176 udest->l_threshold = nla_get_u32(nla_l_thresh);
3182 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3183 const char *mcast_ifn, __u32 syncid)
3185 struct nlattr *nl_daemon;
3187 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3191 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3192 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3193 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3194 goto nla_put_failure;
3195 nla_nest_end(skb, nl_daemon);
3200 nla_nest_cancel(skb, nl_daemon);
3204 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3205 const char *mcast_ifn, __u32 syncid,
3206 struct netlink_callback *cb)
3209 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3210 &ip_vs_genl_family, NLM_F_MULTI,
3211 IPVS_CMD_NEW_DAEMON);
3215 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3216 goto nla_put_failure;
3218 return genlmsg_end(skb, hdr);
3221 genlmsg_cancel(skb, hdr);
3225 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3226 struct netlink_callback *cb)
3228 struct net *net = skb_sknet(skb);
3229 struct netns_ipvs *ipvs = net_ipvs(net);
3231 mutex_lock(&ipvs->sync_mutex);
3232 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3233 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3234 ipvs->master_mcast_ifn,
3235 ipvs->master_syncid, cb) < 0)
3236 goto nla_put_failure;
3241 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3242 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3243 ipvs->backup_mcast_ifn,
3244 ipvs->backup_syncid, cb) < 0)
3245 goto nla_put_failure;
3251 mutex_unlock(&ipvs->sync_mutex);
3256 static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3258 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3259 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3260 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3263 /* The synchronization protocol is incompatible with mixed family
3266 if (net_ipvs(net)->mixed_address_family_dests > 0)
3269 return start_sync_thread(net,
3270 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3271 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3272 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3275 static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3277 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3280 return stop_sync_thread(net,
3281 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3284 static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3286 struct ip_vs_timeout_user t;
3288 __ip_vs_get_timeouts(net, &t);
3290 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3291 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3293 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3295 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3297 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3298 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3300 return ip_vs_set_timeout(net, &t);
3303 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3307 struct netns_ipvs *ipvs;
3309 net = skb_sknet(skb);
3310 ipvs = net_ipvs(net);
3311 cmd = info->genlhdr->cmd;
3313 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3314 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3316 mutex_lock(&ipvs->sync_mutex);
3317 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3318 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3319 info->attrs[IPVS_CMD_ATTR_DAEMON],
3320 ip_vs_daemon_policy)) {
3325 if (cmd == IPVS_CMD_NEW_DAEMON)
3326 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3328 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3330 mutex_unlock(&ipvs->sync_mutex);
3335 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3337 struct ip_vs_service *svc = NULL;
3338 struct ip_vs_service_user_kern usvc;
3339 struct ip_vs_dest_user_kern udest;
3341 int need_full_svc = 0, need_full_dest = 0;
3344 net = skb_sknet(skb);
3345 cmd = info->genlhdr->cmd;
3347 mutex_lock(&__ip_vs_mutex);
3349 if (cmd == IPVS_CMD_FLUSH) {
3350 ret = ip_vs_flush(net, false);
3352 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3353 ret = ip_vs_genl_set_config(net, info->attrs);
3355 } else if (cmd == IPVS_CMD_ZERO &&
3356 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3357 ret = ip_vs_zero_all(net);
3361 /* All following commands require a service argument, so check if we
3362 * received a valid one. We need a full service specification when
3363 * adding / editing a service. Only identifying members otherwise. */
3364 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3367 ret = ip_vs_genl_parse_service(net, &usvc,
3368 info->attrs[IPVS_CMD_ATTR_SERVICE],
3369 need_full_svc, &svc);
3373 /* Unless we're adding a new service, the service must already exist */
3374 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3379 /* Destination commands require a valid destination argument. For
3380 * adding / editing a destination, we need a full destination
3382 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3383 cmd == IPVS_CMD_DEL_DEST) {
3384 if (cmd != IPVS_CMD_DEL_DEST)
3387 ret = ip_vs_genl_parse_dest(&udest,
3388 info->attrs[IPVS_CMD_ATTR_DEST],
3393 /* Old protocols did not allow the user to specify address
3394 * family, so we set it to zero instead. We also didn't
3395 * allow heterogeneous pools in the old code, so it's safe
3396 * to assume that this will have the same address family as
3402 if (udest.af != svc->af) {
3403 /* The synchronization protocol is incompatible
3404 * with mixed family services
3406 if (net_ipvs(net)->sync_state) {
3411 /* Which connection types do we support? */
3412 switch (udest.conn_flags) {
3413 case IP_VS_CONN_F_TUNNEL:
3414 /* We are able to forward this */
3424 case IPVS_CMD_NEW_SERVICE:
3426 ret = ip_vs_add_service(net, &usvc, &svc);
3430 case IPVS_CMD_SET_SERVICE:
3431 ret = ip_vs_edit_service(svc, &usvc);
3433 case IPVS_CMD_DEL_SERVICE:
3434 ret = ip_vs_del_service(svc);
3435 /* do not use svc, it can be freed */
3437 case IPVS_CMD_NEW_DEST:
3438 ret = ip_vs_add_dest(svc, &udest);
3440 case IPVS_CMD_SET_DEST:
3441 ret = ip_vs_edit_dest(svc, &udest);
3443 case IPVS_CMD_DEL_DEST:
3444 ret = ip_vs_del_dest(svc, &udest);
3447 ret = ip_vs_zero_service(svc);
3454 mutex_unlock(&__ip_vs_mutex);
3459 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3461 struct sk_buff *msg;
3463 int ret, cmd, reply_cmd;
3466 net = skb_sknet(skb);
3467 cmd = info->genlhdr->cmd;
3469 if (cmd == IPVS_CMD_GET_SERVICE)
3470 reply_cmd = IPVS_CMD_NEW_SERVICE;
3471 else if (cmd == IPVS_CMD_GET_INFO)
3472 reply_cmd = IPVS_CMD_SET_INFO;
3473 else if (cmd == IPVS_CMD_GET_CONFIG)
3474 reply_cmd = IPVS_CMD_SET_CONFIG;
3476 pr_err("unknown Generic Netlink command\n");
3480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3484 mutex_lock(&__ip_vs_mutex);
3486 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3488 goto nla_put_failure;
3491 case IPVS_CMD_GET_SERVICE:
3493 struct ip_vs_service *svc;
3495 svc = ip_vs_genl_find_service(net,
3496 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3501 ret = ip_vs_genl_fill_service(msg, svc);
3503 goto nla_put_failure;
3512 case IPVS_CMD_GET_CONFIG:
3514 struct ip_vs_timeout_user t;
3516 __ip_vs_get_timeouts(net, &t);
3517 #ifdef CONFIG_IP_VS_PROTO_TCP
3518 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3520 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3522 goto nla_put_failure;
3524 #ifdef CONFIG_IP_VS_PROTO_UDP
3525 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3526 goto nla_put_failure;
3532 case IPVS_CMD_GET_INFO:
3533 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3534 IP_VS_VERSION_CODE) ||
3535 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3536 ip_vs_conn_tab_size))
3537 goto nla_put_failure;
3541 genlmsg_end(msg, reply);
3542 ret = genlmsg_reply(msg, info);
3546 pr_err("not enough space in Netlink message\n");
3552 mutex_unlock(&__ip_vs_mutex);
3558 static const struct genl_ops ip_vs_genl_ops[] = {
3560 .cmd = IPVS_CMD_NEW_SERVICE,
3561 .flags = GENL_ADMIN_PERM,
3562 .policy = ip_vs_cmd_policy,
3563 .doit = ip_vs_genl_set_cmd,
3566 .cmd = IPVS_CMD_SET_SERVICE,
3567 .flags = GENL_ADMIN_PERM,
3568 .policy = ip_vs_cmd_policy,
3569 .doit = ip_vs_genl_set_cmd,
3572 .cmd = IPVS_CMD_DEL_SERVICE,
3573 .flags = GENL_ADMIN_PERM,
3574 .policy = ip_vs_cmd_policy,
3575 .doit = ip_vs_genl_set_cmd,
3578 .cmd = IPVS_CMD_GET_SERVICE,
3579 .flags = GENL_ADMIN_PERM,
3580 .doit = ip_vs_genl_get_cmd,
3581 .dumpit = ip_vs_genl_dump_services,
3582 .policy = ip_vs_cmd_policy,
3585 .cmd = IPVS_CMD_NEW_DEST,
3586 .flags = GENL_ADMIN_PERM,
3587 .policy = ip_vs_cmd_policy,
3588 .doit = ip_vs_genl_set_cmd,
3591 .cmd = IPVS_CMD_SET_DEST,
3592 .flags = GENL_ADMIN_PERM,
3593 .policy = ip_vs_cmd_policy,
3594 .doit = ip_vs_genl_set_cmd,
3597 .cmd = IPVS_CMD_DEL_DEST,
3598 .flags = GENL_ADMIN_PERM,
3599 .policy = ip_vs_cmd_policy,
3600 .doit = ip_vs_genl_set_cmd,
3603 .cmd = IPVS_CMD_GET_DEST,
3604 .flags = GENL_ADMIN_PERM,
3605 .policy = ip_vs_cmd_policy,
3606 .dumpit = ip_vs_genl_dump_dests,
3609 .cmd = IPVS_CMD_NEW_DAEMON,
3610 .flags = GENL_ADMIN_PERM,
3611 .policy = ip_vs_cmd_policy,
3612 .doit = ip_vs_genl_set_daemon,
3615 .cmd = IPVS_CMD_DEL_DAEMON,
3616 .flags = GENL_ADMIN_PERM,
3617 .policy = ip_vs_cmd_policy,
3618 .doit = ip_vs_genl_set_daemon,
3621 .cmd = IPVS_CMD_GET_DAEMON,
3622 .flags = GENL_ADMIN_PERM,
3623 .dumpit = ip_vs_genl_dump_daemons,
3626 .cmd = IPVS_CMD_SET_CONFIG,
3627 .flags = GENL_ADMIN_PERM,
3628 .policy = ip_vs_cmd_policy,
3629 .doit = ip_vs_genl_set_cmd,
3632 .cmd = IPVS_CMD_GET_CONFIG,
3633 .flags = GENL_ADMIN_PERM,
3634 .doit = ip_vs_genl_get_cmd,
3637 .cmd = IPVS_CMD_GET_INFO,
3638 .flags = GENL_ADMIN_PERM,
3639 .doit = ip_vs_genl_get_cmd,
3642 .cmd = IPVS_CMD_ZERO,
3643 .flags = GENL_ADMIN_PERM,
3644 .policy = ip_vs_cmd_policy,
3645 .doit = ip_vs_genl_set_cmd,
3648 .cmd = IPVS_CMD_FLUSH,
3649 .flags = GENL_ADMIN_PERM,
3650 .doit = ip_vs_genl_set_cmd,
3654 static int __init ip_vs_genl_register(void)
3656 return genl_register_family_with_ops(&ip_vs_genl_family,
3660 static void ip_vs_genl_unregister(void)
3662 genl_unregister_family(&ip_vs_genl_family);
3665 /* End of Generic Netlink interface definitions */
3668 * per netns intit/exit func.
3670 #ifdef CONFIG_SYSCTL
3671 static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3674 struct netns_ipvs *ipvs = net_ipvs(net);
3675 struct ctl_table *tbl;
3677 atomic_set(&ipvs->dropentry, 0);
3678 spin_lock_init(&ipvs->dropentry_lock);
3679 spin_lock_init(&ipvs->droppacket_lock);
3680 spin_lock_init(&ipvs->securetcp_lock);
3682 if (!net_eq(net, &init_net)) {
3683 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3687 /* Don't export sysctls to unprivileged users */
3688 if (net->user_ns != &init_user_ns)
3689 tbl[0].procname = NULL;
3692 /* Initialize sysctl defaults */
3694 ipvs->sysctl_amemthresh = 1024;
3695 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3696 ipvs->sysctl_am_droprate = 10;
3697 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3698 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3699 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3700 #ifdef CONFIG_IP_VS_NFCT
3701 tbl[idx++].data = &ipvs->sysctl_conntrack;
3703 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3704 ipvs->sysctl_snat_reroute = 1;
3705 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3706 ipvs->sysctl_sync_ver = 1;
3707 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3708 ipvs->sysctl_sync_ports = 1;
3709 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3710 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3711 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3712 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3713 ipvs->sysctl_sync_sock_size = 0;
3714 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3715 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3716 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3717 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3718 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3719 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3720 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3721 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3722 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3723 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3724 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3725 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3726 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3727 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3728 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3729 ipvs->sysctl_pmtu_disc = 1;
3730 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3731 tbl[idx++].data = &ipvs->sysctl_backup_only;
3734 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3735 if (ipvs->sysctl_hdr == NULL) {
3736 if (!net_eq(net, &init_net))
3740 ip_vs_start_estimator(net, &ipvs->tot_stats);
3741 ipvs->sysctl_tbl = tbl;
3742 /* Schedule defense work */
3743 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3744 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3749 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
3751 struct netns_ipvs *ipvs = net_ipvs(net);
3753 cancel_delayed_work_sync(&ipvs->defense_work);
3754 cancel_work_sync(&ipvs->defense_work.work);
3755 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3756 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3761 static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3762 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3766 static struct notifier_block ip_vs_dst_notifier = {
3767 .notifier_call = ip_vs_dst_event,
3770 int __net_init ip_vs_control_net_init(struct net *net)
3773 struct netns_ipvs *ipvs = net_ipvs(net);
3775 /* Initialize rs_table */
3776 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3777 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3779 INIT_LIST_HEAD(&ipvs->dest_trash);
3780 spin_lock_init(&ipvs->dest_trash_lock);
3781 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3782 (unsigned long) net);
3783 atomic_set(&ipvs->ftpsvc_counter, 0);
3784 atomic_set(&ipvs->nullsvc_counter, 0);
3787 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3788 if (!ipvs->tot_stats.cpustats)
3791 for_each_possible_cpu(i) {
3792 struct ip_vs_cpu_stats *ipvs_tot_stats;
3793 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3794 u64_stats_init(&ipvs_tot_stats->syncp);
3797 spin_lock_init(&ipvs->tot_stats.lock);
3799 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3800 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3801 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3802 &ip_vs_stats_percpu_fops);
3804 if (ip_vs_control_net_init_sysctl(net))
3810 free_percpu(ipvs->tot_stats.cpustats);
3814 void __net_exit ip_vs_control_net_cleanup(struct net *net)
3816 struct netns_ipvs *ipvs = net_ipvs(net);
3818 ip_vs_trash_cleanup(net);
3819 ip_vs_control_net_cleanup_sysctl(net);
3820 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3821 remove_proc_entry("ip_vs_stats", net->proc_net);
3822 remove_proc_entry("ip_vs", net->proc_net);
3823 free_percpu(ipvs->tot_stats.cpustats);
3826 int __init ip_vs_register_nl_ioctl(void)
3830 ret = nf_register_sockopt(&ip_vs_sockopts);
3832 pr_err("cannot register sockopt.\n");
3836 ret = ip_vs_genl_register();
3838 pr_err("cannot register Generic Netlink interface.\n");
3844 nf_unregister_sockopt(&ip_vs_sockopts);
3849 void ip_vs_unregister_nl_ioctl(void)
3851 ip_vs_genl_unregister();
3852 nf_unregister_sockopt(&ip_vs_sockopts);
3855 int __init ip_vs_control_init(void)
3862 /* Initialize svc_table, ip_vs_svc_fwm_table */
3863 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3864 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3865 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3868 smp_wmb(); /* Do we really need it now ? */
3870 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3879 void ip_vs_control_cleanup(void)
3882 unregister_netdevice_notifier(&ip_vs_dst_notifier);