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 if (svc->stats.cpustats)
469 free_percpu(svc->stats.cpustats);
473 static void ip_vs_service_rcu_free(struct rcu_head *head)
475 struct ip_vs_service *svc;
477 svc = container_of(head, struct ip_vs_service, rcu_head);
478 ip_vs_service_free(svc);
481 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
483 if (atomic_dec_and_test(&svc->refcnt)) {
484 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
486 IP_VS_DBG_ADDR(svc->af, &svc->addr),
489 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
491 ip_vs_service_free(svc);
497 * Returns hash value for real service
499 static inline unsigned int ip_vs_rs_hashkey(int af,
500 const union nf_inet_addr *addr,
503 register unsigned int porth = ntohs(port);
504 __be32 addr_fold = addr->ip;
506 #ifdef CONFIG_IP_VS_IPV6
508 addr_fold = addr->ip6[0]^addr->ip6[1]^
509 addr->ip6[2]^addr->ip6[3];
512 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
516 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
517 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
521 if (dest->in_rs_table)
525 * Hash by proto,addr,port,
526 * which are the parameters of the real service.
528 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
530 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
531 dest->in_rs_table = 1;
534 /* Unhash ip_vs_dest from rs_table. */
535 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
538 * Remove it from the rs_table table.
540 if (dest->in_rs_table) {
541 hlist_del_rcu(&dest->d_list);
542 dest->in_rs_table = 0;
546 /* Check if real service by <proto,addr,port> is present */
547 bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
548 const union nf_inet_addr *daddr, __be16 dport)
550 struct netns_ipvs *ipvs = net_ipvs(net);
552 struct ip_vs_dest *dest;
554 /* Check for "full" addressed entries */
555 hash = ip_vs_rs_hashkey(af, daddr, dport);
558 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
559 if (dest->port == dport &&
561 ip_vs_addr_equal(af, &dest->addr, daddr) &&
562 (dest->protocol == protocol || dest->vfwmark)) {
573 /* Lookup destination by {addr,port} in the given service
574 * Called under RCU lock.
576 static struct ip_vs_dest *
577 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
578 const union nf_inet_addr *daddr, __be16 dport)
580 struct ip_vs_dest *dest;
583 * Find the destination for the given service
585 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
586 if ((dest->af == dest_af) &&
587 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
588 (dest->port == dport)) {
598 * Find destination by {daddr,dport,vaddr,protocol}
599 * Created to be used in ip_vs_process_message() in
600 * the backup synchronization daemon. It finds the
601 * destination to be bound to the received connection
603 * Called under RCU lock, no refcnt is returned.
605 struct ip_vs_dest *ip_vs_find_dest(struct net *net, int svc_af, int dest_af,
606 const union nf_inet_addr *daddr,
608 const union nf_inet_addr *vaddr,
609 __be16 vport, __u16 protocol, __u32 fwmark,
612 struct ip_vs_dest *dest;
613 struct ip_vs_service *svc;
616 svc = ip_vs_service_find(net, svc_af, fwmark, protocol, vaddr, vport);
619 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
621 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
623 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
627 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
629 struct ip_vs_dest_dst *dest_dst = container_of(head,
630 struct ip_vs_dest_dst,
633 dst_release(dest_dst->dst_cache);
637 /* Release dest_dst and dst_cache for dest in user context */
638 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
640 struct ip_vs_dest_dst *old;
642 old = rcu_dereference_protected(dest->dest_dst, 1);
644 RCU_INIT_POINTER(dest->dest_dst, NULL);
645 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
650 * Lookup dest by {svc,addr,port} in the destination trash.
651 * The destination trash is used to hold the destinations that are removed
652 * from the service table but are still referenced by some conn entries.
653 * The reason to add the destination trash is when the dest is temporary
654 * down (either by administrator or by monitor program), the dest can be
655 * picked back from the trash, the remaining connections to the dest can
656 * continue, and the counting information of the dest is also useful for
659 static struct ip_vs_dest *
660 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
661 const union nf_inet_addr *daddr, __be16 dport)
663 struct ip_vs_dest *dest;
664 struct netns_ipvs *ipvs = net_ipvs(svc->net);
667 * Find the destination in trash
669 spin_lock_bh(&ipvs->dest_trash_lock);
670 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
671 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
674 IP_VS_DBG_ADDR(dest->af, &dest->addr),
676 atomic_read(&dest->refcnt));
677 if (dest->af == dest_af &&
678 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
679 dest->port == dport &&
680 dest->vfwmark == svc->fwmark &&
681 dest->protocol == svc->protocol &&
683 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
684 dest->vport == svc->port))) {
686 list_del(&dest->t_list);
687 ip_vs_dest_hold(dest);
695 spin_unlock_bh(&ipvs->dest_trash_lock);
700 static void ip_vs_dest_free(struct ip_vs_dest *dest)
702 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
704 __ip_vs_dst_cache_reset(dest);
705 __ip_vs_svc_put(svc, false);
706 free_percpu(dest->stats.cpustats);
707 ip_vs_dest_put_and_free(dest);
711 * Clean up all the destinations in the trash
712 * Called by the ip_vs_control_cleanup()
714 * When the ip_vs_control_clearup is activated by ipvs module exit,
715 * the service tables must have been flushed and all the connections
716 * are expired, and the refcnt of each destination in the trash must
717 * be 0, so we simply release them here.
719 static void ip_vs_trash_cleanup(struct net *net)
721 struct ip_vs_dest *dest, *nxt;
722 struct netns_ipvs *ipvs = net_ipvs(net);
724 del_timer_sync(&ipvs->dest_trash_timer);
725 /* No need to use dest_trash_lock */
726 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
727 list_del(&dest->t_list);
728 ip_vs_dest_free(dest);
733 ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
735 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c
737 spin_lock_bh(&src->lock);
739 IP_VS_SHOW_STATS_COUNTER(conns);
740 IP_VS_SHOW_STATS_COUNTER(inpkts);
741 IP_VS_SHOW_STATS_COUNTER(outpkts);
742 IP_VS_SHOW_STATS_COUNTER(inbytes);
743 IP_VS_SHOW_STATS_COUNTER(outbytes);
745 ip_vs_read_estimator(dst, src);
747 spin_unlock_bh(&src->lock);
751 ip_vs_zero_stats(struct ip_vs_stats *stats)
753 spin_lock_bh(&stats->lock);
755 /* get current counters as zero point, rates are zeroed */
757 #define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c
759 IP_VS_ZERO_STATS_COUNTER(conns);
760 IP_VS_ZERO_STATS_COUNTER(inpkts);
761 IP_VS_ZERO_STATS_COUNTER(outpkts);
762 IP_VS_ZERO_STATS_COUNTER(inbytes);
763 IP_VS_ZERO_STATS_COUNTER(outbytes);
765 ip_vs_zero_estimator(stats);
767 spin_unlock_bh(&stats->lock);
771 * Update a destination in the given service
774 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
775 struct ip_vs_dest_user_kern *udest, int add)
777 struct netns_ipvs *ipvs = net_ipvs(svc->net);
778 struct ip_vs_service *old_svc;
779 struct ip_vs_scheduler *sched;
782 /* We cannot modify an address and change the address family */
783 BUG_ON(!add && udest->af != dest->af);
785 if (add && udest->af != svc->af)
786 ipvs->mixed_address_family_dests++;
788 /* set the weight and the flags */
789 atomic_set(&dest->weight, udest->weight);
790 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
791 conn_flags |= IP_VS_CONN_F_INACTIVE;
793 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
794 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
795 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
798 * Put the real service in rs_table if not present.
799 * For now only for NAT!
801 ip_vs_rs_hash(ipvs, dest);
803 atomic_set(&dest->conn_flags, conn_flags);
805 /* bind the service */
806 old_svc = rcu_dereference_protected(dest->svc, 1);
808 __ip_vs_bind_svc(dest, svc);
810 if (old_svc != svc) {
811 ip_vs_zero_stats(&dest->stats);
812 __ip_vs_bind_svc(dest, svc);
813 __ip_vs_svc_put(old_svc, true);
817 /* set the dest status flags */
818 dest->flags |= IP_VS_DEST_F_AVAILABLE;
820 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
821 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
822 dest->u_threshold = udest->u_threshold;
823 dest->l_threshold = udest->l_threshold;
825 dest->af = udest->af;
827 spin_lock_bh(&dest->dst_lock);
828 __ip_vs_dst_cache_reset(dest);
829 spin_unlock_bh(&dest->dst_lock);
831 sched = rcu_dereference_protected(svc->scheduler, 1);
833 ip_vs_start_estimator(svc->net, &dest->stats);
834 list_add_rcu(&dest->n_list, &svc->destinations);
837 sched->add_dest(svc, dest);
840 sched->upd_dest(svc, dest);
846 * Create a destination for the given service
849 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
850 struct ip_vs_dest **dest_p)
852 struct ip_vs_dest *dest;
853 unsigned int atype, i;
857 #ifdef CONFIG_IP_VS_IPV6
858 if (udest->af == AF_INET6) {
859 atype = ipv6_addr_type(&udest->addr.in6);
860 if ((!(atype & IPV6_ADDR_UNICAST) ||
861 atype & IPV6_ADDR_LINKLOCAL) &&
862 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
867 atype = inet_addr_type(svc->net, udest->addr.ip);
868 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
872 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
876 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
877 if (!dest->stats.cpustats)
880 for_each_possible_cpu(i) {
881 struct ip_vs_cpu_stats *ip_vs_dest_stats;
882 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
883 u64_stats_init(&ip_vs_dest_stats->syncp);
886 dest->af = udest->af;
887 dest->protocol = svc->protocol;
888 dest->vaddr = svc->addr;
889 dest->vport = svc->port;
890 dest->vfwmark = svc->fwmark;
891 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
892 dest->port = udest->port;
894 atomic_set(&dest->activeconns, 0);
895 atomic_set(&dest->inactconns, 0);
896 atomic_set(&dest->persistconns, 0);
897 atomic_set(&dest->refcnt, 1);
899 INIT_HLIST_NODE(&dest->d_list);
900 spin_lock_init(&dest->dst_lock);
901 spin_lock_init(&dest->stats.lock);
902 __ip_vs_update_dest(svc, dest, udest, 1);
916 * Add a destination into an existing service
919 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
921 struct ip_vs_dest *dest;
922 union nf_inet_addr daddr;
923 __be16 dport = udest->port;
928 if (udest->weight < 0) {
929 pr_err("%s(): server weight less than zero\n", __func__);
933 if (udest->l_threshold > udest->u_threshold) {
934 pr_err("%s(): lower threshold is higher than upper threshold\n",
939 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
941 /* We use function that requires RCU lock */
943 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
947 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
952 * Check if the dest already exists in the trash and
953 * is from the same service
955 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
958 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
959 "dest->refcnt=%d, service %u/%s:%u\n",
960 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
961 atomic_read(&dest->refcnt),
963 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
966 __ip_vs_update_dest(svc, dest, udest, 1);
970 * Allocate and initialize the dest structure
972 ret = ip_vs_new_dest(svc, udest, &dest);
981 * Edit a destination in the given service
984 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
986 struct ip_vs_dest *dest;
987 union nf_inet_addr daddr;
988 __be16 dport = udest->port;
992 if (udest->weight < 0) {
993 pr_err("%s(): server weight less than zero\n", __func__);
997 if (udest->l_threshold > udest->u_threshold) {
998 pr_err("%s(): lower threshold is higher than upper threshold\n",
1003 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1005 /* We use function that requires RCU lock */
1007 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1011 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1015 __ip_vs_update_dest(svc, dest, udest, 0);
1022 * Delete a destination (must be already unlinked from the service)
1024 static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1027 struct netns_ipvs *ipvs = net_ipvs(net);
1029 ip_vs_stop_estimator(net, &dest->stats);
1032 * Remove it from the d-linked list with the real services.
1034 ip_vs_rs_unhash(dest);
1036 spin_lock_bh(&ipvs->dest_trash_lock);
1037 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1038 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1039 atomic_read(&dest->refcnt));
1040 if (list_empty(&ipvs->dest_trash) && !cleanup)
1041 mod_timer(&ipvs->dest_trash_timer,
1042 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1043 /* dest lives in trash without reference */
1044 list_add(&dest->t_list, &ipvs->dest_trash);
1045 dest->idle_start = 0;
1046 spin_unlock_bh(&ipvs->dest_trash_lock);
1047 ip_vs_dest_put(dest);
1052 * Unlink a destination from the given service
1054 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1055 struct ip_vs_dest *dest,
1058 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1061 * Remove it from the d-linked destination list.
1063 list_del_rcu(&dest->n_list);
1066 if (dest->af != svc->af)
1067 net_ipvs(svc->net)->mixed_address_family_dests--;
1070 struct ip_vs_scheduler *sched;
1072 sched = rcu_dereference_protected(svc->scheduler, 1);
1073 if (sched->del_dest)
1074 sched->del_dest(svc, dest);
1080 * Delete a destination server in the given service
1083 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1085 struct ip_vs_dest *dest;
1086 __be16 dport = udest->port;
1090 /* We use function that requires RCU lock */
1092 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1096 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1101 * Unlink dest from the service
1103 __ip_vs_unlink_dest(svc, dest, 1);
1106 * Delete the destination
1108 __ip_vs_del_dest(svc->net, dest, false);
1115 static void ip_vs_dest_trash_expire(unsigned long data)
1117 struct net *net = (struct net *) data;
1118 struct netns_ipvs *ipvs = net_ipvs(net);
1119 struct ip_vs_dest *dest, *next;
1120 unsigned long now = jiffies;
1122 spin_lock(&ipvs->dest_trash_lock);
1123 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1124 if (atomic_read(&dest->refcnt) > 0)
1126 if (dest->idle_start) {
1127 if (time_before(now, dest->idle_start +
1128 IP_VS_DEST_TRASH_PERIOD))
1131 dest->idle_start = max(1UL, now);
1134 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1136 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1138 list_del(&dest->t_list);
1139 ip_vs_dest_free(dest);
1141 if (!list_empty(&ipvs->dest_trash))
1142 mod_timer(&ipvs->dest_trash_timer,
1143 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1144 spin_unlock(&ipvs->dest_trash_lock);
1148 * Add a service into the service hash table
1151 ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1152 struct ip_vs_service **svc_p)
1155 struct ip_vs_scheduler *sched = NULL;
1156 struct ip_vs_pe *pe = NULL;
1157 struct ip_vs_service *svc = NULL;
1158 struct netns_ipvs *ipvs = net_ipvs(net);
1160 /* increase the module use count */
1161 ip_vs_use_count_inc();
1163 /* Lookup the scheduler by 'u->sched_name' */
1164 sched = ip_vs_scheduler_get(u->sched_name);
1165 if (sched == NULL) {
1166 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1171 if (u->pe_name && *u->pe_name) {
1172 pe = ip_vs_pe_getbyname(u->pe_name);
1174 pr_info("persistence engine module ip_vs_pe_%s "
1175 "not found\n", u->pe_name);
1181 #ifdef CONFIG_IP_VS_IPV6
1182 if (u->af == AF_INET6) {
1183 __u32 plen = (__force __u32) u->netmask;
1185 if (plen < 1 || plen > 128) {
1192 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1194 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1198 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1199 if (!svc->stats.cpustats) {
1204 for_each_possible_cpu(i) {
1205 struct ip_vs_cpu_stats *ip_vs_stats;
1206 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1207 u64_stats_init(&ip_vs_stats->syncp);
1211 /* I'm the first user of the service */
1212 atomic_set(&svc->refcnt, 0);
1215 svc->protocol = u->protocol;
1216 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1217 svc->port = u->port;
1218 svc->fwmark = u->fwmark;
1219 svc->flags = u->flags;
1220 svc->timeout = u->timeout * HZ;
1221 svc->netmask = u->netmask;
1224 INIT_LIST_HEAD(&svc->destinations);
1225 spin_lock_init(&svc->sched_lock);
1226 spin_lock_init(&svc->stats.lock);
1228 /* Bind the scheduler */
1229 ret = ip_vs_bind_scheduler(svc, sched);
1234 /* Bind the ct retriever */
1235 RCU_INIT_POINTER(svc->pe, pe);
1238 /* Update the virtual service counters */
1239 if (svc->port == FTPPORT)
1240 atomic_inc(&ipvs->ftpsvc_counter);
1241 else if (svc->port == 0)
1242 atomic_inc(&ipvs->nullsvc_counter);
1244 ip_vs_start_estimator(net, &svc->stats);
1246 /* Count only IPv4 services for old get/setsockopt interface */
1247 if (svc->af == AF_INET)
1248 ipvs->num_services++;
1250 /* Hash the service into the service table */
1251 ip_vs_svc_hash(svc);
1254 /* Now there is a service - full throttle */
1261 ip_vs_unbind_scheduler(svc, sched);
1262 ip_vs_service_free(svc);
1264 ip_vs_scheduler_put(sched);
1267 /* decrease the module use count */
1268 ip_vs_use_count_dec();
1275 * Edit a service and bind it with a new scheduler
1278 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1280 struct ip_vs_scheduler *sched, *old_sched;
1281 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1285 * Lookup the scheduler, by 'u->sched_name'
1287 sched = ip_vs_scheduler_get(u->sched_name);
1288 if (sched == NULL) {
1289 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1294 if (u->pe_name && *u->pe_name) {
1295 pe = ip_vs_pe_getbyname(u->pe_name);
1297 pr_info("persistence engine module ip_vs_pe_%s "
1298 "not found\n", u->pe_name);
1305 #ifdef CONFIG_IP_VS_IPV6
1306 if (u->af == AF_INET6) {
1307 __u32 plen = (__force __u32) u->netmask;
1309 if (plen < 1 || plen > 128) {
1316 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1317 if (sched != old_sched) {
1318 /* Bind the new scheduler */
1319 ret = ip_vs_bind_scheduler(svc, sched);
1324 /* Unbind the old scheduler on success */
1325 ip_vs_unbind_scheduler(svc, old_sched);
1329 * Set the flags and timeout value
1331 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1332 svc->timeout = u->timeout * HZ;
1333 svc->netmask = u->netmask;
1335 old_pe = rcu_dereference_protected(svc->pe, 1);
1337 rcu_assign_pointer(svc->pe, pe);
1340 ip_vs_scheduler_put(old_sched);
1341 ip_vs_pe_put(old_pe);
1346 * Delete a service from the service list
1347 * - The service must be unlinked, unlocked and not referenced!
1348 * - We are called under _bh lock
1350 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1352 struct ip_vs_dest *dest, *nxt;
1353 struct ip_vs_scheduler *old_sched;
1354 struct ip_vs_pe *old_pe;
1355 struct netns_ipvs *ipvs = net_ipvs(svc->net);
1357 pr_info("%s: enter\n", __func__);
1359 /* Count only IPv4 services for old get/setsockopt interface */
1360 if (svc->af == AF_INET)
1361 ipvs->num_services--;
1363 ip_vs_stop_estimator(svc->net, &svc->stats);
1365 /* Unbind scheduler */
1366 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1367 ip_vs_unbind_scheduler(svc, old_sched);
1368 ip_vs_scheduler_put(old_sched);
1370 /* Unbind persistence engine, keep svc->pe */
1371 old_pe = rcu_dereference_protected(svc->pe, 1);
1372 ip_vs_pe_put(old_pe);
1375 * Unlink the whole destination list
1377 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1378 __ip_vs_unlink_dest(svc, dest, 0);
1379 __ip_vs_del_dest(svc->net, dest, cleanup);
1383 * Update the virtual service counters
1385 if (svc->port == FTPPORT)
1386 atomic_dec(&ipvs->ftpsvc_counter);
1387 else if (svc->port == 0)
1388 atomic_dec(&ipvs->nullsvc_counter);
1391 * Free the service if nobody refers to it
1393 __ip_vs_svc_put(svc, true);
1395 /* decrease the module use count */
1396 ip_vs_use_count_dec();
1400 * Unlink a service from list and try to delete it if its refcnt reached 0
1402 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1404 /* Hold svc to avoid double release from dest_trash */
1405 atomic_inc(&svc->refcnt);
1407 * Unhash it from the service table
1409 ip_vs_svc_unhash(svc);
1411 __ip_vs_del_service(svc, cleanup);
1415 * Delete a service from the service list
1417 static int ip_vs_del_service(struct ip_vs_service *svc)
1421 ip_vs_unlink_service(svc, false);
1428 * Flush all the virtual services
1430 static int ip_vs_flush(struct net *net, bool cleanup)
1433 struct ip_vs_service *svc;
1434 struct hlist_node *n;
1437 * Flush the service table hashed by <netns,protocol,addr,port>
1439 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1440 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1442 if (net_eq(svc->net, net))
1443 ip_vs_unlink_service(svc, cleanup);
1448 * Flush the service table hashed by fwmark
1450 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1451 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1453 if (net_eq(svc->net, net))
1454 ip_vs_unlink_service(svc, cleanup);
1462 * Delete service by {netns} in the service table.
1463 * Called by __ip_vs_cleanup()
1465 void ip_vs_service_net_cleanup(struct net *net)
1468 /* Check for "full" addressed entries */
1469 mutex_lock(&__ip_vs_mutex);
1470 ip_vs_flush(net, true);
1471 mutex_unlock(&__ip_vs_mutex);
1475 /* Put all references for device (dst_cache) */
1477 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1479 struct ip_vs_dest_dst *dest_dst;
1481 spin_lock_bh(&dest->dst_lock);
1482 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1483 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1484 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1486 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1488 atomic_read(&dest->refcnt));
1489 __ip_vs_dst_cache_reset(dest);
1491 spin_unlock_bh(&dest->dst_lock);
1494 /* Netdev event receiver
1495 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1497 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1500 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1501 struct net *net = dev_net(dev);
1502 struct netns_ipvs *ipvs = net_ipvs(net);
1503 struct ip_vs_service *svc;
1504 struct ip_vs_dest *dest;
1507 if (event != NETDEV_DOWN || !ipvs)
1509 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1511 mutex_lock(&__ip_vs_mutex);
1512 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1513 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1514 if (net_eq(svc->net, net)) {
1515 list_for_each_entry(dest, &svc->destinations,
1517 ip_vs_forget_dev(dest, dev);
1522 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1523 if (net_eq(svc->net, net)) {
1524 list_for_each_entry(dest, &svc->destinations,
1526 ip_vs_forget_dev(dest, dev);
1533 spin_lock_bh(&ipvs->dest_trash_lock);
1534 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1535 ip_vs_forget_dev(dest, dev);
1537 spin_unlock_bh(&ipvs->dest_trash_lock);
1538 mutex_unlock(&__ip_vs_mutex);
1544 * Zero counters in a service or all services
1546 static int ip_vs_zero_service(struct ip_vs_service *svc)
1548 struct ip_vs_dest *dest;
1550 list_for_each_entry(dest, &svc->destinations, n_list) {
1551 ip_vs_zero_stats(&dest->stats);
1553 ip_vs_zero_stats(&svc->stats);
1557 static int ip_vs_zero_all(struct net *net)
1560 struct ip_vs_service *svc;
1562 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1563 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1564 if (net_eq(svc->net, net))
1565 ip_vs_zero_service(svc);
1569 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1570 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1571 if (net_eq(svc->net, net))
1572 ip_vs_zero_service(svc);
1576 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
1580 #ifdef CONFIG_SYSCTL
1583 static int three = 3;
1586 proc_do_defense_mode(struct ctl_table *table, int write,
1587 void __user *buffer, size_t *lenp, loff_t *ppos)
1589 struct net *net = current->nsproxy->net_ns;
1590 int *valp = table->data;
1594 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1595 if (write && (*valp != val)) {
1596 if ((*valp < 0) || (*valp > 3)) {
1597 /* Restore the correct value */
1600 update_defense_level(net_ipvs(net));
1607 proc_do_sync_threshold(struct ctl_table *table, int write,
1608 void __user *buffer, size_t *lenp, loff_t *ppos)
1610 int *valp = table->data;
1614 /* backup the value first */
1615 memcpy(val, valp, sizeof(val));
1617 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1618 if (write && (valp[0] < 0 || valp[1] < 0 ||
1619 (valp[0] >= valp[1] && valp[1]))) {
1620 /* Restore the correct value */
1621 memcpy(valp, val, sizeof(val));
1627 proc_do_sync_mode(struct ctl_table *table, int write,
1628 void __user *buffer, size_t *lenp, loff_t *ppos)
1630 int *valp = table->data;
1634 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1635 if (write && (*valp != val)) {
1636 if ((*valp < 0) || (*valp > 1)) {
1637 /* Restore the correct value */
1645 proc_do_sync_ports(struct ctl_table *table, int write,
1646 void __user *buffer, size_t *lenp, loff_t *ppos)
1648 int *valp = table->data;
1652 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1653 if (write && (*valp != val)) {
1654 if (*valp < 1 || !is_power_of_2(*valp)) {
1655 /* Restore the correct value */
1663 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1664 * Do not change order or insert new entries without
1665 * align with netns init in ip_vs_control_net_init()
1668 static struct ctl_table vs_vars[] = {
1670 .procname = "amemthresh",
1671 .maxlen = sizeof(int),
1673 .proc_handler = proc_dointvec,
1676 .procname = "am_droprate",
1677 .maxlen = sizeof(int),
1679 .proc_handler = proc_dointvec,
1682 .procname = "drop_entry",
1683 .maxlen = sizeof(int),
1685 .proc_handler = proc_do_defense_mode,
1688 .procname = "drop_packet",
1689 .maxlen = sizeof(int),
1691 .proc_handler = proc_do_defense_mode,
1693 #ifdef CONFIG_IP_VS_NFCT
1695 .procname = "conntrack",
1696 .maxlen = sizeof(int),
1698 .proc_handler = &proc_dointvec,
1702 .procname = "secure_tcp",
1703 .maxlen = sizeof(int),
1705 .proc_handler = proc_do_defense_mode,
1708 .procname = "snat_reroute",
1709 .maxlen = sizeof(int),
1711 .proc_handler = &proc_dointvec,
1714 .procname = "sync_version",
1715 .maxlen = sizeof(int),
1717 .proc_handler = &proc_do_sync_mode,
1720 .procname = "sync_ports",
1721 .maxlen = sizeof(int),
1723 .proc_handler = &proc_do_sync_ports,
1726 .procname = "sync_persist_mode",
1727 .maxlen = sizeof(int),
1729 .proc_handler = proc_dointvec,
1732 .procname = "sync_qlen_max",
1733 .maxlen = sizeof(unsigned long),
1735 .proc_handler = proc_doulongvec_minmax,
1738 .procname = "sync_sock_size",
1739 .maxlen = sizeof(int),
1741 .proc_handler = proc_dointvec,
1744 .procname = "cache_bypass",
1745 .maxlen = sizeof(int),
1747 .proc_handler = proc_dointvec,
1750 .procname = "expire_nodest_conn",
1751 .maxlen = sizeof(int),
1753 .proc_handler = proc_dointvec,
1756 .procname = "sloppy_tcp",
1757 .maxlen = sizeof(int),
1759 .proc_handler = proc_dointvec,
1762 .procname = "sloppy_sctp",
1763 .maxlen = sizeof(int),
1765 .proc_handler = proc_dointvec,
1768 .procname = "expire_quiescent_template",
1769 .maxlen = sizeof(int),
1771 .proc_handler = proc_dointvec,
1774 .procname = "sync_threshold",
1776 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1778 .proc_handler = proc_do_sync_threshold,
1781 .procname = "sync_refresh_period",
1782 .maxlen = sizeof(int),
1784 .proc_handler = proc_dointvec_jiffies,
1787 .procname = "sync_retries",
1788 .maxlen = sizeof(int),
1790 .proc_handler = proc_dointvec_minmax,
1795 .procname = "nat_icmp_send",
1796 .maxlen = sizeof(int),
1798 .proc_handler = proc_dointvec,
1801 .procname = "pmtu_disc",
1802 .maxlen = sizeof(int),
1804 .proc_handler = proc_dointvec,
1807 .procname = "backup_only",
1808 .maxlen = sizeof(int),
1810 .proc_handler = proc_dointvec,
1812 #ifdef CONFIG_IP_VS_DEBUG
1814 .procname = "debug_level",
1815 .data = &sysctl_ip_vs_debug_level,
1816 .maxlen = sizeof(int),
1818 .proc_handler = proc_dointvec,
1826 #ifdef CONFIG_PROC_FS
1829 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1830 struct hlist_head *table;
1835 * Write the contents of the VS rule table to a PROCfs file.
1836 * (It is kept just for backward compatibility)
1838 static inline const char *ip_vs_fwd_name(unsigned int flags)
1840 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1841 case IP_VS_CONN_F_LOCALNODE:
1843 case IP_VS_CONN_F_TUNNEL:
1845 case IP_VS_CONN_F_DROUTE:
1853 /* Get the Nth entry in the two lists */
1854 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1856 struct net *net = seq_file_net(seq);
1857 struct ip_vs_iter *iter = seq->private;
1859 struct ip_vs_service *svc;
1861 /* look in hash by protocol */
1862 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1863 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1864 if (net_eq(svc->net, net) && pos-- == 0) {
1865 iter->table = ip_vs_svc_table;
1872 /* keep looking in fwmark */
1873 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1874 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1876 if (net_eq(svc->net, net) && pos-- == 0) {
1877 iter->table = ip_vs_svc_fwm_table;
1887 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1891 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1895 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1897 struct hlist_node *e;
1898 struct ip_vs_iter *iter;
1899 struct ip_vs_service *svc;
1902 if (v == SEQ_START_TOKEN)
1903 return ip_vs_info_array(seq,0);
1906 iter = seq->private;
1908 if (iter->table == ip_vs_svc_table) {
1909 /* next service in table hashed by protocol */
1910 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1912 return hlist_entry(e, struct ip_vs_service, s_list);
1914 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1915 hlist_for_each_entry_rcu(svc,
1916 &ip_vs_svc_table[iter->bucket],
1922 iter->table = ip_vs_svc_fwm_table;
1927 /* next service in hashed by fwmark */
1928 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
1930 return hlist_entry(e, struct ip_vs_service, f_list);
1933 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1934 hlist_for_each_entry_rcu(svc,
1935 &ip_vs_svc_fwm_table[iter->bucket],
1943 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1950 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1952 if (v == SEQ_START_TOKEN) {
1954 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1955 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1957 "Prot LocalAddress:Port Scheduler Flags\n");
1959 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1961 const struct ip_vs_service *svc = v;
1962 const struct ip_vs_iter *iter = seq->private;
1963 const struct ip_vs_dest *dest;
1964 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
1966 if (iter->table == ip_vs_svc_table) {
1967 #ifdef CONFIG_IP_VS_IPV6
1968 if (svc->af == AF_INET6)
1969 seq_printf(seq, "%s [%pI6]:%04X %s ",
1970 ip_vs_proto_name(svc->protocol),
1976 seq_printf(seq, "%s %08X:%04X %s %s ",
1977 ip_vs_proto_name(svc->protocol),
1978 ntohl(svc->addr.ip),
1981 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1983 seq_printf(seq, "FWM %08X %s %s",
1984 svc->fwmark, sched->name,
1985 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1988 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1989 seq_printf(seq, "persistent %d %08X\n",
1991 ntohl(svc->netmask));
1993 seq_putc(seq, '\n');
1995 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
1996 #ifdef CONFIG_IP_VS_IPV6
1997 if (dest->af == AF_INET6)
2000 " %-7s %-6d %-10d %-10d\n",
2003 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2004 atomic_read(&dest->weight),
2005 atomic_read(&dest->activeconns),
2006 atomic_read(&dest->inactconns));
2011 "%-7s %-6d %-10d %-10d\n",
2012 ntohl(dest->addr.ip),
2014 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2015 atomic_read(&dest->weight),
2016 atomic_read(&dest->activeconns),
2017 atomic_read(&dest->inactconns));
2024 static const struct seq_operations ip_vs_info_seq_ops = {
2025 .start = ip_vs_info_seq_start,
2026 .next = ip_vs_info_seq_next,
2027 .stop = ip_vs_info_seq_stop,
2028 .show = ip_vs_info_seq_show,
2031 static int ip_vs_info_open(struct inode *inode, struct file *file)
2033 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2034 sizeof(struct ip_vs_iter));
2037 static const struct file_operations ip_vs_info_fops = {
2038 .owner = THIS_MODULE,
2039 .open = ip_vs_info_open,
2041 .llseek = seq_lseek,
2042 .release = seq_release_net,
2045 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2047 struct net *net = seq_file_single_net(seq);
2048 struct ip_vs_stats_user show;
2050 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2052 " Total Incoming Outgoing Incoming Outgoing\n");
2054 " Conns Packets Packets Bytes Bytes\n");
2056 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2057 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2058 show.inpkts, show.outpkts,
2059 (unsigned long long) show.inbytes,
2060 (unsigned long long) show.outbytes);
2062 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2064 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2065 seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2066 show.cps, show.inpps, show.outpps,
2067 show.inbps, show.outbps);
2072 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2074 return single_open_net(inode, file, ip_vs_stats_show);
2077 static const struct file_operations ip_vs_stats_fops = {
2078 .owner = THIS_MODULE,
2079 .open = ip_vs_stats_seq_open,
2081 .llseek = seq_lseek,
2082 .release = single_release_net,
2085 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2087 struct net *net = seq_file_single_net(seq);
2088 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2089 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2090 struct ip_vs_stats_user rates;
2093 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2095 " Total Incoming Outgoing Incoming Outgoing\n");
2097 "CPU Conns Packets Packets Bytes Bytes\n");
2099 for_each_possible_cpu(i) {
2100 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2102 __u64 inbytes, outbytes;
2105 start = u64_stats_fetch_begin_irq(&u->syncp);
2106 inbytes = u->ustats.inbytes;
2107 outbytes = u->ustats.outbytes;
2108 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2110 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
2111 i, u->ustats.conns, u->ustats.inpkts,
2112 u->ustats.outpkts, (__u64)inbytes,
2116 spin_lock_bh(&tot_stats->lock);
2118 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2119 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2120 tot_stats->ustats.outpkts,
2121 (unsigned long long) tot_stats->ustats.inbytes,
2122 (unsigned long long) tot_stats->ustats.outbytes);
2124 ip_vs_read_estimator(&rates, tot_stats);
2126 spin_unlock_bh(&tot_stats->lock);
2128 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2130 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2131 seq_printf(seq, " %8X %8X %8X %16X %16X\n",
2141 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2143 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2146 static const struct file_operations ip_vs_stats_percpu_fops = {
2147 .owner = THIS_MODULE,
2148 .open = ip_vs_stats_percpu_seq_open,
2150 .llseek = seq_lseek,
2151 .release = single_release_net,
2156 * Set timeout values for tcp tcpfin udp in the timeout_table.
2158 static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
2160 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2161 struct ip_vs_proto_data *pd;
2164 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2169 #ifdef CONFIG_IP_VS_PROTO_TCP
2170 if (u->tcp_timeout) {
2171 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2172 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2173 = u->tcp_timeout * HZ;
2176 if (u->tcp_fin_timeout) {
2177 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2178 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2179 = u->tcp_fin_timeout * HZ;
2183 #ifdef CONFIG_IP_VS_PROTO_UDP
2184 if (u->udp_timeout) {
2185 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2186 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2187 = u->udp_timeout * HZ;
2193 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2195 struct ip_vs_svcdest_user {
2196 struct ip_vs_service_user s;
2197 struct ip_vs_dest_user d;
2200 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2201 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2202 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2203 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2204 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2205 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2206 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2207 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2208 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2209 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2210 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2213 union ip_vs_set_arglen {
2214 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2215 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2216 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2217 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2218 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2219 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2220 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2221 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2222 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2223 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2226 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2228 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2229 struct ip_vs_service_user *usvc_compat)
2231 memset(usvc, 0, sizeof(*usvc));
2234 usvc->protocol = usvc_compat->protocol;
2235 usvc->addr.ip = usvc_compat->addr;
2236 usvc->port = usvc_compat->port;
2237 usvc->fwmark = usvc_compat->fwmark;
2239 /* Deep copy of sched_name is not needed here */
2240 usvc->sched_name = usvc_compat->sched_name;
2242 usvc->flags = usvc_compat->flags;
2243 usvc->timeout = usvc_compat->timeout;
2244 usvc->netmask = usvc_compat->netmask;
2247 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2248 struct ip_vs_dest_user *udest_compat)
2250 memset(udest, 0, sizeof(*udest));
2252 udest->addr.ip = udest_compat->addr;
2253 udest->port = udest_compat->port;
2254 udest->conn_flags = udest_compat->conn_flags;
2255 udest->weight = udest_compat->weight;
2256 udest->u_threshold = udest_compat->u_threshold;
2257 udest->l_threshold = udest_compat->l_threshold;
2258 udest->af = AF_INET;
2262 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2264 struct net *net = sock_net(sk);
2266 unsigned char arg[MAX_SET_ARGLEN];
2267 struct ip_vs_service_user *usvc_compat;
2268 struct ip_vs_service_user_kern usvc;
2269 struct ip_vs_service *svc;
2270 struct ip_vs_dest_user *udest_compat;
2271 struct ip_vs_dest_user_kern udest;
2272 struct netns_ipvs *ipvs = net_ipvs(net);
2274 BUILD_BUG_ON(sizeof(arg) > 255);
2275 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2278 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2280 if (len != set_arglen[CMDID(cmd)]) {
2281 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2282 len, set_arglen[CMDID(cmd)]);
2286 if (copy_from_user(arg, user, len) != 0)
2289 /* increase the module use count */
2290 ip_vs_use_count_inc();
2292 /* Handle daemons since they have another lock */
2293 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2294 cmd == IP_VS_SO_SET_STOPDAEMON) {
2295 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2297 mutex_lock(&ipvs->sync_mutex);
2298 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2299 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2302 ret = stop_sync_thread(net, dm->state);
2303 mutex_unlock(&ipvs->sync_mutex);
2307 mutex_lock(&__ip_vs_mutex);
2308 if (cmd == IP_VS_SO_SET_FLUSH) {
2309 /* Flush the virtual service */
2310 ret = ip_vs_flush(net, false);
2312 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2313 /* Set timeout values for (tcp tcpfin udp) */
2314 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
2318 usvc_compat = (struct ip_vs_service_user *)arg;
2319 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2321 /* We only use the new structs internally, so copy userspace compat
2322 * structs to extended internal versions */
2323 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2324 ip_vs_copy_udest_compat(&udest, udest_compat);
2326 if (cmd == IP_VS_SO_SET_ZERO) {
2327 /* if no service address is set, zero counters in all */
2328 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2329 ret = ip_vs_zero_all(net);
2334 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2335 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2336 usvc.protocol != IPPROTO_SCTP) {
2337 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2338 usvc.protocol, &usvc.addr.ip,
2339 ntohs(usvc.port), usvc.sched_name);
2344 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2346 if (usvc.fwmark == 0)
2347 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2348 &usvc.addr, usvc.port);
2350 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
2353 if (cmd != IP_VS_SO_SET_ADD
2354 && (svc == NULL || svc->protocol != usvc.protocol)) {
2360 case IP_VS_SO_SET_ADD:
2364 ret = ip_vs_add_service(net, &usvc, &svc);
2366 case IP_VS_SO_SET_EDIT:
2367 ret = ip_vs_edit_service(svc, &usvc);
2369 case IP_VS_SO_SET_DEL:
2370 ret = ip_vs_del_service(svc);
2374 case IP_VS_SO_SET_ZERO:
2375 ret = ip_vs_zero_service(svc);
2377 case IP_VS_SO_SET_ADDDEST:
2378 ret = ip_vs_add_dest(svc, &udest);
2380 case IP_VS_SO_SET_EDITDEST:
2381 ret = ip_vs_edit_dest(svc, &udest);
2383 case IP_VS_SO_SET_DELDEST:
2384 ret = ip_vs_del_dest(svc, &udest);
2391 mutex_unlock(&__ip_vs_mutex);
2393 /* decrease the module use count */
2394 ip_vs_use_count_dec();
2401 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2403 struct ip_vs_scheduler *sched;
2405 sched = rcu_dereference_protected(src->scheduler, 1);
2406 dst->protocol = src->protocol;
2407 dst->addr = src->addr.ip;
2408 dst->port = src->port;
2409 dst->fwmark = src->fwmark;
2410 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
2411 dst->flags = src->flags;
2412 dst->timeout = src->timeout / HZ;
2413 dst->netmask = src->netmask;
2414 dst->num_dests = src->num_dests;
2415 ip_vs_copy_stats(&dst->stats, &src->stats);
2419 __ip_vs_get_service_entries(struct net *net,
2420 const struct ip_vs_get_services *get,
2421 struct ip_vs_get_services __user *uptr)
2424 struct ip_vs_service *svc;
2425 struct ip_vs_service_entry entry;
2428 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2429 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2430 /* Only expose IPv4 entries to old interface */
2431 if (svc->af != AF_INET || !net_eq(svc->net, net))
2434 if (count >= get->num_services)
2436 memset(&entry, 0, sizeof(entry));
2437 ip_vs_copy_service(&entry, svc);
2438 if (copy_to_user(&uptr->entrytable[count],
2439 &entry, sizeof(entry))) {
2447 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2448 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2449 /* Only expose IPv4 entries to old interface */
2450 if (svc->af != AF_INET || !net_eq(svc->net, net))
2453 if (count >= get->num_services)
2455 memset(&entry, 0, sizeof(entry));
2456 ip_vs_copy_service(&entry, svc);
2457 if (copy_to_user(&uptr->entrytable[count],
2458 &entry, sizeof(entry))) {
2470 __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2471 struct ip_vs_get_dests __user *uptr)
2473 struct ip_vs_service *svc;
2474 union nf_inet_addr addr = { .ip = get->addr };
2479 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
2481 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2487 struct ip_vs_dest *dest;
2488 struct ip_vs_dest_entry entry;
2490 memset(&entry, 0, sizeof(entry));
2491 list_for_each_entry(dest, &svc->destinations, n_list) {
2492 if (count >= get->num_dests)
2495 /* Cannot expose heterogeneous members via sockopt
2498 if (dest->af != svc->af)
2501 entry.addr = dest->addr.ip;
2502 entry.port = dest->port;
2503 entry.conn_flags = atomic_read(&dest->conn_flags);
2504 entry.weight = atomic_read(&dest->weight);
2505 entry.u_threshold = dest->u_threshold;
2506 entry.l_threshold = dest->l_threshold;
2507 entry.activeconns = atomic_read(&dest->activeconns);
2508 entry.inactconns = atomic_read(&dest->inactconns);
2509 entry.persistconns = atomic_read(&dest->persistconns);
2510 ip_vs_copy_stats(&entry.stats, &dest->stats);
2511 if (copy_to_user(&uptr->entrytable[count],
2512 &entry, sizeof(entry))) {
2524 __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
2526 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2527 struct ip_vs_proto_data *pd;
2530 memset(u, 0, sizeof (*u));
2532 #ifdef CONFIG_IP_VS_PROTO_TCP
2533 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2534 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2535 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2537 #ifdef CONFIG_IP_VS_PROTO_UDP
2538 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2540 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2544 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2545 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2546 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2547 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2548 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2549 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2550 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2551 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2554 union ip_vs_get_arglen {
2555 char field_IP_VS_SO_GET_VERSION[64];
2556 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2557 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2558 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2559 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2560 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2561 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2564 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2567 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2569 unsigned char arg[MAX_GET_ARGLEN];
2571 unsigned int copylen;
2572 struct net *net = sock_net(sk);
2573 struct netns_ipvs *ipvs = net_ipvs(net);
2576 BUILD_BUG_ON(sizeof(arg) > 255);
2577 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2580 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2583 copylen = get_arglen[CMDID(cmd)];
2584 if (*len < (int) copylen) {
2585 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2589 if (copy_from_user(arg, user, copylen) != 0)
2592 * Handle daemons first since it has its own locking
2594 if (cmd == IP_VS_SO_GET_DAEMON) {
2595 struct ip_vs_daemon_user d[2];
2597 memset(&d, 0, sizeof(d));
2598 mutex_lock(&ipvs->sync_mutex);
2599 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2600 d[0].state = IP_VS_STATE_MASTER;
2601 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2602 sizeof(d[0].mcast_ifn));
2603 d[0].syncid = ipvs->master_syncid;
2605 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2606 d[1].state = IP_VS_STATE_BACKUP;
2607 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2608 sizeof(d[1].mcast_ifn));
2609 d[1].syncid = ipvs->backup_syncid;
2611 if (copy_to_user(user, &d, sizeof(d)) != 0)
2613 mutex_unlock(&ipvs->sync_mutex);
2617 mutex_lock(&__ip_vs_mutex);
2619 case IP_VS_SO_GET_VERSION:
2623 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2624 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2625 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2629 *len = strlen(buf)+1;
2633 case IP_VS_SO_GET_INFO:
2635 struct ip_vs_getinfo info;
2636 info.version = IP_VS_VERSION_CODE;
2637 info.size = ip_vs_conn_tab_size;
2638 info.num_services = ipvs->num_services;
2639 if (copy_to_user(user, &info, sizeof(info)) != 0)
2644 case IP_VS_SO_GET_SERVICES:
2646 struct ip_vs_get_services *get;
2649 get = (struct ip_vs_get_services *)arg;
2650 size = sizeof(*get) +
2651 sizeof(struct ip_vs_service_entry) * get->num_services;
2653 pr_err("length: %u != %u\n", *len, size);
2657 ret = __ip_vs_get_service_entries(net, get, user);
2661 case IP_VS_SO_GET_SERVICE:
2663 struct ip_vs_service_entry *entry;
2664 struct ip_vs_service *svc;
2665 union nf_inet_addr addr;
2667 entry = (struct ip_vs_service_entry *)arg;
2668 addr.ip = entry->addr;
2671 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
2673 svc = __ip_vs_service_find(net, AF_INET,
2674 entry->protocol, &addr,
2678 ip_vs_copy_service(entry, svc);
2679 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2686 case IP_VS_SO_GET_DESTS:
2688 struct ip_vs_get_dests *get;
2691 get = (struct ip_vs_get_dests *)arg;
2692 size = sizeof(*get) +
2693 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2695 pr_err("length: %u != %u\n", *len, size);
2699 ret = __ip_vs_get_dest_entries(net, get, user);
2703 case IP_VS_SO_GET_TIMEOUT:
2705 struct ip_vs_timeout_user t;
2707 __ip_vs_get_timeouts(net, &t);
2708 if (copy_to_user(user, &t, sizeof(t)) != 0)
2718 mutex_unlock(&__ip_vs_mutex);
2723 static struct nf_sockopt_ops ip_vs_sockopts = {
2725 .set_optmin = IP_VS_BASE_CTL,
2726 .set_optmax = IP_VS_SO_SET_MAX+1,
2727 .set = do_ip_vs_set_ctl,
2728 .get_optmin = IP_VS_BASE_CTL,
2729 .get_optmax = IP_VS_SO_GET_MAX+1,
2730 .get = do_ip_vs_get_ctl,
2731 .owner = THIS_MODULE,
2735 * Generic Netlink interface
2738 /* IPVS genetlink family */
2739 static struct genl_family ip_vs_genl_family = {
2740 .id = GENL_ID_GENERATE,
2742 .name = IPVS_GENL_NAME,
2743 .version = IPVS_GENL_VERSION,
2744 .maxattr = IPVS_CMD_MAX,
2745 .netnsok = true, /* Make ipvsadm to work on netns */
2748 /* Policy used for first-level command attributes */
2749 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2750 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2751 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2752 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2753 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2754 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2755 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2758 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2759 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2760 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2761 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2762 .len = IP_VS_IFNAME_MAXLEN },
2763 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2766 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2767 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2768 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2769 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2770 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2771 .len = sizeof(union nf_inet_addr) },
2772 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2773 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2774 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2775 .len = IP_VS_SCHEDNAME_MAXLEN },
2776 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2777 .len = IP_VS_PENAME_MAXLEN },
2778 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2779 .len = sizeof(struct ip_vs_flags) },
2780 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2781 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2782 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2785 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2786 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2787 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2788 .len = sizeof(union nf_inet_addr) },
2789 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2790 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2791 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2792 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2793 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2794 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2795 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2796 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2797 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2798 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
2801 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2802 struct ip_vs_stats *stats)
2804 struct ip_vs_stats_user ustats;
2805 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2809 ip_vs_copy_stats(&ustats, stats);
2811 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) ||
2812 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) ||
2813 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) ||
2814 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) ||
2815 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) ||
2816 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) ||
2817 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) ||
2818 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) ||
2819 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) ||
2820 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps))
2821 goto nla_put_failure;
2822 nla_nest_end(skb, nl_stats);
2827 nla_nest_cancel(skb, nl_stats);
2831 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2832 struct ip_vs_service *svc)
2834 struct ip_vs_scheduler *sched;
2835 struct ip_vs_pe *pe;
2836 struct nlattr *nl_service;
2837 struct ip_vs_flags flags = { .flags = svc->flags,
2840 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2844 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2845 goto nla_put_failure;
2847 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2848 goto nla_put_failure;
2850 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2851 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2852 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2853 goto nla_put_failure;
2856 sched = rcu_dereference_protected(svc->scheduler, 1);
2857 pe = rcu_dereference_protected(svc->pe, 1);
2858 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
2859 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2860 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2861 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2862 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2863 goto nla_put_failure;
2864 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2865 goto nla_put_failure;
2867 nla_nest_end(skb, nl_service);
2872 nla_nest_cancel(skb, nl_service);
2876 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2877 struct ip_vs_service *svc,
2878 struct netlink_callback *cb)
2882 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2883 &ip_vs_genl_family, NLM_F_MULTI,
2884 IPVS_CMD_NEW_SERVICE);
2888 if (ip_vs_genl_fill_service(skb, svc) < 0)
2889 goto nla_put_failure;
2891 return genlmsg_end(skb, hdr);
2894 genlmsg_cancel(skb, hdr);
2898 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2899 struct netlink_callback *cb)
2902 int start = cb->args[0];
2903 struct ip_vs_service *svc;
2904 struct net *net = skb_sknet(skb);
2906 mutex_lock(&__ip_vs_mutex);
2907 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2908 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2909 if (++idx <= start || !net_eq(svc->net, net))
2911 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2913 goto nla_put_failure;
2918 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2919 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2920 if (++idx <= start || !net_eq(svc->net, net))
2922 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2924 goto nla_put_failure;
2930 mutex_unlock(&__ip_vs_mutex);
2936 static int ip_vs_genl_parse_service(struct net *net,
2937 struct ip_vs_service_user_kern *usvc,
2938 struct nlattr *nla, int full_entry,
2939 struct ip_vs_service **ret_svc)
2941 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2942 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2943 struct ip_vs_service *svc;
2945 /* Parse mandatory identifying service fields first */
2947 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2950 nla_af = attrs[IPVS_SVC_ATTR_AF];
2951 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2952 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2953 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2954 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2956 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2959 memset(usvc, 0, sizeof(*usvc));
2961 usvc->af = nla_get_u16(nla_af);
2962 #ifdef CONFIG_IP_VS_IPV6
2963 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2965 if (usvc->af != AF_INET)
2967 return -EAFNOSUPPORT;
2970 usvc->protocol = IPPROTO_TCP;
2971 usvc->fwmark = nla_get_u32(nla_fwmark);
2973 usvc->protocol = nla_get_u16(nla_protocol);
2974 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2975 usvc->port = nla_get_be16(nla_port);
2981 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
2983 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
2984 &usvc->addr, usvc->port);
2988 /* If a full entry was requested, check for the additional fields */
2990 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
2992 struct ip_vs_flags flags;
2994 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2995 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
2996 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2997 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2998 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3000 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3003 nla_memcpy(&flags, nla_flags, sizeof(flags));
3005 /* prefill flags from service if it already exists */
3007 usvc->flags = svc->flags;
3009 /* set new flags from userland */
3010 usvc->flags = (usvc->flags & ~flags.mask) |
3011 (flags.flags & flags.mask);
3012 usvc->sched_name = nla_data(nla_sched);
3013 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3014 usvc->timeout = nla_get_u32(nla_timeout);
3015 usvc->netmask = nla_get_be32(nla_netmask);
3021 static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3024 struct ip_vs_service_user_kern usvc;
3025 struct ip_vs_service *svc;
3028 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3029 return ret ? ERR_PTR(ret) : svc;
3032 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3034 struct nlattr *nl_dest;
3036 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3040 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3041 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3042 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3043 (atomic_read(&dest->conn_flags) &
3044 IP_VS_CONN_F_FWD_MASK)) ||
3045 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3046 atomic_read(&dest->weight)) ||
3047 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3048 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3049 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3050 atomic_read(&dest->activeconns)) ||
3051 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3052 atomic_read(&dest->inactconns)) ||
3053 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3054 atomic_read(&dest->persistconns)) ||
3055 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3056 goto nla_put_failure;
3057 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
3058 goto nla_put_failure;
3060 nla_nest_end(skb, nl_dest);
3065 nla_nest_cancel(skb, nl_dest);
3069 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3070 struct netlink_callback *cb)
3074 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3075 &ip_vs_genl_family, NLM_F_MULTI,
3080 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3081 goto nla_put_failure;
3083 return genlmsg_end(skb, hdr);
3086 genlmsg_cancel(skb, hdr);
3090 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3091 struct netlink_callback *cb)
3094 int start = cb->args[0];
3095 struct ip_vs_service *svc;
3096 struct ip_vs_dest *dest;
3097 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3098 struct net *net = skb_sknet(skb);
3100 mutex_lock(&__ip_vs_mutex);
3102 /* Try to find the service for which to dump destinations */
3103 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3104 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3108 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3109 if (IS_ERR(svc) || svc == NULL)
3112 /* Dump the destinations */
3113 list_for_each_entry(dest, &svc->destinations, n_list) {
3116 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3118 goto nla_put_failure;
3126 mutex_unlock(&__ip_vs_mutex);
3131 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3132 struct nlattr *nla, int full_entry)
3134 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3135 struct nlattr *nla_addr, *nla_port;
3136 struct nlattr *nla_addr_family;
3138 /* Parse mandatory identifying destination fields first */
3140 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3143 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3144 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3145 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3147 if (!(nla_addr && nla_port))
3150 memset(udest, 0, sizeof(*udest));
3152 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3153 udest->port = nla_get_be16(nla_port);
3155 if (nla_addr_family)
3156 udest->af = nla_get_u16(nla_addr_family);
3160 /* If a full entry was requested, check for the additional fields */
3162 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3165 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3166 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3167 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3168 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3170 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3173 udest->conn_flags = nla_get_u32(nla_fwd)
3174 & IP_VS_CONN_F_FWD_MASK;
3175 udest->weight = nla_get_u32(nla_weight);
3176 udest->u_threshold = nla_get_u32(nla_u_thresh);
3177 udest->l_threshold = nla_get_u32(nla_l_thresh);
3183 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3184 const char *mcast_ifn, __u32 syncid)
3186 struct nlattr *nl_daemon;
3188 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3192 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3193 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3194 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3195 goto nla_put_failure;
3196 nla_nest_end(skb, nl_daemon);
3201 nla_nest_cancel(skb, nl_daemon);
3205 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3206 const char *mcast_ifn, __u32 syncid,
3207 struct netlink_callback *cb)
3210 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3211 &ip_vs_genl_family, NLM_F_MULTI,
3212 IPVS_CMD_NEW_DAEMON);
3216 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3217 goto nla_put_failure;
3219 return genlmsg_end(skb, hdr);
3222 genlmsg_cancel(skb, hdr);
3226 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3227 struct netlink_callback *cb)
3229 struct net *net = skb_sknet(skb);
3230 struct netns_ipvs *ipvs = net_ipvs(net);
3232 mutex_lock(&ipvs->sync_mutex);
3233 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3234 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3235 ipvs->master_mcast_ifn,
3236 ipvs->master_syncid, cb) < 0)
3237 goto nla_put_failure;
3242 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3243 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3244 ipvs->backup_mcast_ifn,
3245 ipvs->backup_syncid, cb) < 0)
3246 goto nla_put_failure;
3252 mutex_unlock(&ipvs->sync_mutex);
3257 static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3259 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3260 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3261 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3264 /* The synchronization protocol is incompatible with mixed family
3267 if (net_ipvs(net)->mixed_address_family_dests > 0)
3270 return start_sync_thread(net,
3271 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3272 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3273 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3276 static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3278 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3281 return stop_sync_thread(net,
3282 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3285 static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3287 struct ip_vs_timeout_user t;
3289 __ip_vs_get_timeouts(net, &t);
3291 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3292 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3294 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3296 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3298 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3299 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3301 return ip_vs_set_timeout(net, &t);
3304 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3308 struct netns_ipvs *ipvs;
3310 net = skb_sknet(skb);
3311 ipvs = net_ipvs(net);
3312 cmd = info->genlhdr->cmd;
3314 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3315 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3317 mutex_lock(&ipvs->sync_mutex);
3318 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3319 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3320 info->attrs[IPVS_CMD_ATTR_DAEMON],
3321 ip_vs_daemon_policy)) {
3326 if (cmd == IPVS_CMD_NEW_DAEMON)
3327 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3329 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3331 mutex_unlock(&ipvs->sync_mutex);
3336 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3338 struct ip_vs_service *svc = NULL;
3339 struct ip_vs_service_user_kern usvc;
3340 struct ip_vs_dest_user_kern udest;
3342 int need_full_svc = 0, need_full_dest = 0;
3345 net = skb_sknet(skb);
3346 cmd = info->genlhdr->cmd;
3348 mutex_lock(&__ip_vs_mutex);
3350 if (cmd == IPVS_CMD_FLUSH) {
3351 ret = ip_vs_flush(net, false);
3353 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3354 ret = ip_vs_genl_set_config(net, info->attrs);
3356 } else if (cmd == IPVS_CMD_ZERO &&
3357 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3358 ret = ip_vs_zero_all(net);
3362 /* All following commands require a service argument, so check if we
3363 * received a valid one. We need a full service specification when
3364 * adding / editing a service. Only identifying members otherwise. */
3365 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3368 ret = ip_vs_genl_parse_service(net, &usvc,
3369 info->attrs[IPVS_CMD_ATTR_SERVICE],
3370 need_full_svc, &svc);
3374 /* Unless we're adding a new service, the service must already exist */
3375 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3380 /* Destination commands require a valid destination argument. For
3381 * adding / editing a destination, we need a full destination
3383 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3384 cmd == IPVS_CMD_DEL_DEST) {
3385 if (cmd != IPVS_CMD_DEL_DEST)
3388 ret = ip_vs_genl_parse_dest(&udest,
3389 info->attrs[IPVS_CMD_ATTR_DEST],
3394 /* Old protocols did not allow the user to specify address
3395 * family, so we set it to zero instead. We also didn't
3396 * allow heterogeneous pools in the old code, so it's safe
3397 * to assume that this will have the same address family as
3403 if (udest.af != svc->af) {
3404 /* The synchronization protocol is incompatible
3405 * with mixed family services
3407 if (net_ipvs(net)->sync_state) {
3412 /* Which connection types do we support? */
3413 switch (udest.conn_flags) {
3414 case IP_VS_CONN_F_TUNNEL:
3415 /* We are able to forward this */
3425 case IPVS_CMD_NEW_SERVICE:
3427 ret = ip_vs_add_service(net, &usvc, &svc);
3431 case IPVS_CMD_SET_SERVICE:
3432 ret = ip_vs_edit_service(svc, &usvc);
3434 case IPVS_CMD_DEL_SERVICE:
3435 ret = ip_vs_del_service(svc);
3436 /* do not use svc, it can be freed */
3438 case IPVS_CMD_NEW_DEST:
3439 ret = ip_vs_add_dest(svc, &udest);
3441 case IPVS_CMD_SET_DEST:
3442 ret = ip_vs_edit_dest(svc, &udest);
3444 case IPVS_CMD_DEL_DEST:
3445 ret = ip_vs_del_dest(svc, &udest);
3448 ret = ip_vs_zero_service(svc);
3455 mutex_unlock(&__ip_vs_mutex);
3460 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3462 struct sk_buff *msg;
3464 int ret, cmd, reply_cmd;
3467 net = skb_sknet(skb);
3468 cmd = info->genlhdr->cmd;
3470 if (cmd == IPVS_CMD_GET_SERVICE)
3471 reply_cmd = IPVS_CMD_NEW_SERVICE;
3472 else if (cmd == IPVS_CMD_GET_INFO)
3473 reply_cmd = IPVS_CMD_SET_INFO;
3474 else if (cmd == IPVS_CMD_GET_CONFIG)
3475 reply_cmd = IPVS_CMD_SET_CONFIG;
3477 pr_err("unknown Generic Netlink command\n");
3481 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3485 mutex_lock(&__ip_vs_mutex);
3487 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3489 goto nla_put_failure;
3492 case IPVS_CMD_GET_SERVICE:
3494 struct ip_vs_service *svc;
3496 svc = ip_vs_genl_find_service(net,
3497 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3502 ret = ip_vs_genl_fill_service(msg, svc);
3504 goto nla_put_failure;
3513 case IPVS_CMD_GET_CONFIG:
3515 struct ip_vs_timeout_user t;
3517 __ip_vs_get_timeouts(net, &t);
3518 #ifdef CONFIG_IP_VS_PROTO_TCP
3519 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3521 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3523 goto nla_put_failure;
3525 #ifdef CONFIG_IP_VS_PROTO_UDP
3526 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3527 goto nla_put_failure;
3533 case IPVS_CMD_GET_INFO:
3534 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3535 IP_VS_VERSION_CODE) ||
3536 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3537 ip_vs_conn_tab_size))
3538 goto nla_put_failure;
3542 genlmsg_end(msg, reply);
3543 ret = genlmsg_reply(msg, info);
3547 pr_err("not enough space in Netlink message\n");
3553 mutex_unlock(&__ip_vs_mutex);
3559 static const struct genl_ops ip_vs_genl_ops[] = {
3561 .cmd = IPVS_CMD_NEW_SERVICE,
3562 .flags = GENL_ADMIN_PERM,
3563 .policy = ip_vs_cmd_policy,
3564 .doit = ip_vs_genl_set_cmd,
3567 .cmd = IPVS_CMD_SET_SERVICE,
3568 .flags = GENL_ADMIN_PERM,
3569 .policy = ip_vs_cmd_policy,
3570 .doit = ip_vs_genl_set_cmd,
3573 .cmd = IPVS_CMD_DEL_SERVICE,
3574 .flags = GENL_ADMIN_PERM,
3575 .policy = ip_vs_cmd_policy,
3576 .doit = ip_vs_genl_set_cmd,
3579 .cmd = IPVS_CMD_GET_SERVICE,
3580 .flags = GENL_ADMIN_PERM,
3581 .doit = ip_vs_genl_get_cmd,
3582 .dumpit = ip_vs_genl_dump_services,
3583 .policy = ip_vs_cmd_policy,
3586 .cmd = IPVS_CMD_NEW_DEST,
3587 .flags = GENL_ADMIN_PERM,
3588 .policy = ip_vs_cmd_policy,
3589 .doit = ip_vs_genl_set_cmd,
3592 .cmd = IPVS_CMD_SET_DEST,
3593 .flags = GENL_ADMIN_PERM,
3594 .policy = ip_vs_cmd_policy,
3595 .doit = ip_vs_genl_set_cmd,
3598 .cmd = IPVS_CMD_DEL_DEST,
3599 .flags = GENL_ADMIN_PERM,
3600 .policy = ip_vs_cmd_policy,
3601 .doit = ip_vs_genl_set_cmd,
3604 .cmd = IPVS_CMD_GET_DEST,
3605 .flags = GENL_ADMIN_PERM,
3606 .policy = ip_vs_cmd_policy,
3607 .dumpit = ip_vs_genl_dump_dests,
3610 .cmd = IPVS_CMD_NEW_DAEMON,
3611 .flags = GENL_ADMIN_PERM,
3612 .policy = ip_vs_cmd_policy,
3613 .doit = ip_vs_genl_set_daemon,
3616 .cmd = IPVS_CMD_DEL_DAEMON,
3617 .flags = GENL_ADMIN_PERM,
3618 .policy = ip_vs_cmd_policy,
3619 .doit = ip_vs_genl_set_daemon,
3622 .cmd = IPVS_CMD_GET_DAEMON,
3623 .flags = GENL_ADMIN_PERM,
3624 .dumpit = ip_vs_genl_dump_daemons,
3627 .cmd = IPVS_CMD_SET_CONFIG,
3628 .flags = GENL_ADMIN_PERM,
3629 .policy = ip_vs_cmd_policy,
3630 .doit = ip_vs_genl_set_cmd,
3633 .cmd = IPVS_CMD_GET_CONFIG,
3634 .flags = GENL_ADMIN_PERM,
3635 .doit = ip_vs_genl_get_cmd,
3638 .cmd = IPVS_CMD_GET_INFO,
3639 .flags = GENL_ADMIN_PERM,
3640 .doit = ip_vs_genl_get_cmd,
3643 .cmd = IPVS_CMD_ZERO,
3644 .flags = GENL_ADMIN_PERM,
3645 .policy = ip_vs_cmd_policy,
3646 .doit = ip_vs_genl_set_cmd,
3649 .cmd = IPVS_CMD_FLUSH,
3650 .flags = GENL_ADMIN_PERM,
3651 .doit = ip_vs_genl_set_cmd,
3655 static int __init ip_vs_genl_register(void)
3657 return genl_register_family_with_ops(&ip_vs_genl_family,
3661 static void ip_vs_genl_unregister(void)
3663 genl_unregister_family(&ip_vs_genl_family);
3666 /* End of Generic Netlink interface definitions */
3669 * per netns intit/exit func.
3671 #ifdef CONFIG_SYSCTL
3672 static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3675 struct netns_ipvs *ipvs = net_ipvs(net);
3676 struct ctl_table *tbl;
3678 atomic_set(&ipvs->dropentry, 0);
3679 spin_lock_init(&ipvs->dropentry_lock);
3680 spin_lock_init(&ipvs->droppacket_lock);
3681 spin_lock_init(&ipvs->securetcp_lock);
3683 if (!net_eq(net, &init_net)) {
3684 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3688 /* Don't export sysctls to unprivileged users */
3689 if (net->user_ns != &init_user_ns)
3690 tbl[0].procname = NULL;
3693 /* Initialize sysctl defaults */
3695 ipvs->sysctl_amemthresh = 1024;
3696 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3697 ipvs->sysctl_am_droprate = 10;
3698 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3699 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3700 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3701 #ifdef CONFIG_IP_VS_NFCT
3702 tbl[idx++].data = &ipvs->sysctl_conntrack;
3704 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3705 ipvs->sysctl_snat_reroute = 1;
3706 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3707 ipvs->sysctl_sync_ver = 1;
3708 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3709 ipvs->sysctl_sync_ports = 1;
3710 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3711 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3712 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3713 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3714 ipvs->sysctl_sync_sock_size = 0;
3715 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3716 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3717 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3718 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3719 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3720 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3721 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3722 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3723 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3724 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3725 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3726 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3727 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3728 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3729 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3730 ipvs->sysctl_pmtu_disc = 1;
3731 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3732 tbl[idx++].data = &ipvs->sysctl_backup_only;
3735 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3736 if (ipvs->sysctl_hdr == NULL) {
3737 if (!net_eq(net, &init_net))
3741 ip_vs_start_estimator(net, &ipvs->tot_stats);
3742 ipvs->sysctl_tbl = tbl;
3743 /* Schedule defense work */
3744 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3745 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3750 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
3752 struct netns_ipvs *ipvs = net_ipvs(net);
3754 cancel_delayed_work_sync(&ipvs->defense_work);
3755 cancel_work_sync(&ipvs->defense_work.work);
3756 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3757 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3762 static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3763 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3767 static struct notifier_block ip_vs_dst_notifier = {
3768 .notifier_call = ip_vs_dst_event,
3771 int __net_init ip_vs_control_net_init(struct net *net)
3774 struct netns_ipvs *ipvs = net_ipvs(net);
3776 /* Initialize rs_table */
3777 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3778 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3780 INIT_LIST_HEAD(&ipvs->dest_trash);
3781 spin_lock_init(&ipvs->dest_trash_lock);
3782 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3783 (unsigned long) net);
3784 atomic_set(&ipvs->ftpsvc_counter, 0);
3785 atomic_set(&ipvs->nullsvc_counter, 0);
3788 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3789 if (!ipvs->tot_stats.cpustats)
3792 for_each_possible_cpu(i) {
3793 struct ip_vs_cpu_stats *ipvs_tot_stats;
3794 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3795 u64_stats_init(&ipvs_tot_stats->syncp);
3798 spin_lock_init(&ipvs->tot_stats.lock);
3800 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3801 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3802 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3803 &ip_vs_stats_percpu_fops);
3805 if (ip_vs_control_net_init_sysctl(net))
3811 free_percpu(ipvs->tot_stats.cpustats);
3815 void __net_exit ip_vs_control_net_cleanup(struct net *net)
3817 struct netns_ipvs *ipvs = net_ipvs(net);
3819 ip_vs_trash_cleanup(net);
3820 ip_vs_control_net_cleanup_sysctl(net);
3821 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3822 remove_proc_entry("ip_vs_stats", net->proc_net);
3823 remove_proc_entry("ip_vs", net->proc_net);
3824 free_percpu(ipvs->tot_stats.cpustats);
3827 int __init ip_vs_register_nl_ioctl(void)
3831 ret = nf_register_sockopt(&ip_vs_sockopts);
3833 pr_err("cannot register sockopt.\n");
3837 ret = ip_vs_genl_register();
3839 pr_err("cannot register Generic Netlink interface.\n");
3845 nf_unregister_sockopt(&ip_vs_sockopts);
3850 void ip_vs_unregister_nl_ioctl(void)
3852 ip_vs_genl_unregister();
3853 nf_unregister_sockopt(&ip_vs_sockopts);
3856 int __init ip_vs_control_init(void)
3863 /* Initialize svc_table, ip_vs_svc_fwm_table */
3864 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3865 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3866 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3869 smp_wmb(); /* Do we really need it now ? */
3871 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3880 void ip_vs_control_cleanup(void)
3883 unregister_netdevice_notifier(&ip_vs_dst_notifier);