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, const union nf_inet_addr *daddr,
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 == svc->af)
587 && ip_vs_addr_equal(svc->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 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, 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, daddr, port);
623 dest = ip_vs_lookup_dest(svc, 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, const union nf_inet_addr *daddr,
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(svc->af, &dest->addr),
676 atomic_read(&dest->refcnt));
677 if (dest->af == svc->af &&
678 ip_vs_addr_equal(svc->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 /* set the weight and the flags */
783 atomic_set(&dest->weight, udest->weight);
784 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
785 conn_flags |= IP_VS_CONN_F_INACTIVE;
787 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
788 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
789 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
792 * Put the real service in rs_table if not present.
793 * For now only for NAT!
795 ip_vs_rs_hash(ipvs, dest);
797 atomic_set(&dest->conn_flags, conn_flags);
799 /* bind the service */
800 old_svc = rcu_dereference_protected(dest->svc, 1);
802 __ip_vs_bind_svc(dest, svc);
804 if (old_svc != svc) {
805 ip_vs_zero_stats(&dest->stats);
806 __ip_vs_bind_svc(dest, svc);
807 __ip_vs_svc_put(old_svc, true);
811 /* set the dest status flags */
812 dest->flags |= IP_VS_DEST_F_AVAILABLE;
814 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
815 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
816 dest->u_threshold = udest->u_threshold;
817 dest->l_threshold = udest->l_threshold;
819 spin_lock_bh(&dest->dst_lock);
820 __ip_vs_dst_cache_reset(dest);
821 spin_unlock_bh(&dest->dst_lock);
823 sched = rcu_dereference_protected(svc->scheduler, 1);
825 ip_vs_start_estimator(svc->net, &dest->stats);
826 list_add_rcu(&dest->n_list, &svc->destinations);
829 sched->add_dest(svc, dest);
832 sched->upd_dest(svc, dest);
838 * Create a destination for the given service
841 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
842 struct ip_vs_dest **dest_p)
844 struct ip_vs_dest *dest;
845 unsigned int atype, i;
849 #ifdef CONFIG_IP_VS_IPV6
850 if (svc->af == AF_INET6) {
851 atype = ipv6_addr_type(&udest->addr.in6);
852 if ((!(atype & IPV6_ADDR_UNICAST) ||
853 atype & IPV6_ADDR_LINKLOCAL) &&
854 !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
859 atype = inet_addr_type(svc->net, udest->addr.ip);
860 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
864 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
868 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
869 if (!dest->stats.cpustats)
872 for_each_possible_cpu(i) {
873 struct ip_vs_cpu_stats *ip_vs_dest_stats;
874 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
875 u64_stats_init(&ip_vs_dest_stats->syncp);
879 dest->protocol = svc->protocol;
880 dest->vaddr = svc->addr;
881 dest->vport = svc->port;
882 dest->vfwmark = svc->fwmark;
883 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
884 dest->port = udest->port;
886 atomic_set(&dest->activeconns, 0);
887 atomic_set(&dest->inactconns, 0);
888 atomic_set(&dest->persistconns, 0);
889 atomic_set(&dest->refcnt, 1);
891 INIT_HLIST_NODE(&dest->d_list);
892 spin_lock_init(&dest->dst_lock);
893 spin_lock_init(&dest->stats.lock);
894 __ip_vs_update_dest(svc, dest, udest, 1);
908 * Add a destination into an existing service
911 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
913 struct ip_vs_dest *dest;
914 union nf_inet_addr daddr;
915 __be16 dport = udest->port;
920 if (udest->weight < 0) {
921 pr_err("%s(): server weight less than zero\n", __func__);
925 if (udest->l_threshold > udest->u_threshold) {
926 pr_err("%s(): lower threshold is higher than upper threshold\n",
931 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
933 /* We use function that requires RCU lock */
935 dest = ip_vs_lookup_dest(svc, &daddr, dport);
939 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
944 * Check if the dest already exists in the trash and
945 * is from the same service
947 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
950 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
951 "dest->refcnt=%d, service %u/%s:%u\n",
952 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
953 atomic_read(&dest->refcnt),
955 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
958 __ip_vs_update_dest(svc, dest, udest, 1);
962 * Allocate and initialize the dest structure
964 ret = ip_vs_new_dest(svc, udest, &dest);
973 * Edit a destination in the given service
976 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
978 struct ip_vs_dest *dest;
979 union nf_inet_addr daddr;
980 __be16 dport = udest->port;
984 if (udest->weight < 0) {
985 pr_err("%s(): server weight less than zero\n", __func__);
989 if (udest->l_threshold > udest->u_threshold) {
990 pr_err("%s(): lower threshold is higher than upper threshold\n",
995 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
997 /* We use function that requires RCU lock */
999 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1003 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1007 __ip_vs_update_dest(svc, dest, udest, 0);
1014 * Delete a destination (must be already unlinked from the service)
1016 static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
1019 struct netns_ipvs *ipvs = net_ipvs(net);
1021 ip_vs_stop_estimator(net, &dest->stats);
1024 * Remove it from the d-linked list with the real services.
1026 ip_vs_rs_unhash(dest);
1028 spin_lock_bh(&ipvs->dest_trash_lock);
1029 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1030 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1031 atomic_read(&dest->refcnt));
1032 if (list_empty(&ipvs->dest_trash) && !cleanup)
1033 mod_timer(&ipvs->dest_trash_timer,
1034 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1035 /* dest lives in trash without reference */
1036 list_add(&dest->t_list, &ipvs->dest_trash);
1037 dest->idle_start = 0;
1038 spin_unlock_bh(&ipvs->dest_trash_lock);
1039 ip_vs_dest_put(dest);
1044 * Unlink a destination from the given service
1046 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1047 struct ip_vs_dest *dest,
1050 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1053 * Remove it from the d-linked destination list.
1055 list_del_rcu(&dest->n_list);
1059 struct ip_vs_scheduler *sched;
1061 sched = rcu_dereference_protected(svc->scheduler, 1);
1062 if (sched->del_dest)
1063 sched->del_dest(svc, dest);
1069 * Delete a destination server in the given service
1072 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1074 struct ip_vs_dest *dest;
1075 __be16 dport = udest->port;
1079 /* We use function that requires RCU lock */
1081 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
1085 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1090 * Unlink dest from the service
1092 __ip_vs_unlink_dest(svc, dest, 1);
1095 * Delete the destination
1097 __ip_vs_del_dest(svc->net, dest, false);
1104 static void ip_vs_dest_trash_expire(unsigned long data)
1106 struct net *net = (struct net *) data;
1107 struct netns_ipvs *ipvs = net_ipvs(net);
1108 struct ip_vs_dest *dest, *next;
1109 unsigned long now = jiffies;
1111 spin_lock(&ipvs->dest_trash_lock);
1112 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1113 if (atomic_read(&dest->refcnt) > 0)
1115 if (dest->idle_start) {
1116 if (time_before(now, dest->idle_start +
1117 IP_VS_DEST_TRASH_PERIOD))
1120 dest->idle_start = max(1UL, now);
1123 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1125 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1127 list_del(&dest->t_list);
1128 ip_vs_dest_free(dest);
1130 if (!list_empty(&ipvs->dest_trash))
1131 mod_timer(&ipvs->dest_trash_timer,
1132 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1133 spin_unlock(&ipvs->dest_trash_lock);
1137 * Add a service into the service hash table
1140 ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1141 struct ip_vs_service **svc_p)
1144 struct ip_vs_scheduler *sched = NULL;
1145 struct ip_vs_pe *pe = NULL;
1146 struct ip_vs_service *svc = NULL;
1147 struct netns_ipvs *ipvs = net_ipvs(net);
1149 /* increase the module use count */
1150 ip_vs_use_count_inc();
1152 /* Lookup the scheduler by 'u->sched_name' */
1153 sched = ip_vs_scheduler_get(u->sched_name);
1154 if (sched == NULL) {
1155 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1160 if (u->pe_name && *u->pe_name) {
1161 pe = ip_vs_pe_getbyname(u->pe_name);
1163 pr_info("persistence engine module ip_vs_pe_%s "
1164 "not found\n", u->pe_name);
1170 #ifdef CONFIG_IP_VS_IPV6
1171 if (u->af == AF_INET6) {
1172 __u32 plen = (__force __u32) u->netmask;
1174 if (plen < 1 || plen > 128) {
1181 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1183 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1187 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1188 if (!svc->stats.cpustats) {
1193 for_each_possible_cpu(i) {
1194 struct ip_vs_cpu_stats *ip_vs_stats;
1195 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1196 u64_stats_init(&ip_vs_stats->syncp);
1200 /* I'm the first user of the service */
1201 atomic_set(&svc->refcnt, 0);
1204 svc->protocol = u->protocol;
1205 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1206 svc->port = u->port;
1207 svc->fwmark = u->fwmark;
1208 svc->flags = u->flags;
1209 svc->timeout = u->timeout * HZ;
1210 svc->netmask = u->netmask;
1213 INIT_LIST_HEAD(&svc->destinations);
1214 spin_lock_init(&svc->sched_lock);
1215 spin_lock_init(&svc->stats.lock);
1217 /* Bind the scheduler */
1218 ret = ip_vs_bind_scheduler(svc, sched);
1223 /* Bind the ct retriever */
1224 RCU_INIT_POINTER(svc->pe, pe);
1227 /* Update the virtual service counters */
1228 if (svc->port == FTPPORT)
1229 atomic_inc(&ipvs->ftpsvc_counter);
1230 else if (svc->port == 0)
1231 atomic_inc(&ipvs->nullsvc_counter);
1233 ip_vs_start_estimator(net, &svc->stats);
1235 /* Count only IPv4 services for old get/setsockopt interface */
1236 if (svc->af == AF_INET)
1237 ipvs->num_services++;
1239 /* Hash the service into the service table */
1240 ip_vs_svc_hash(svc);
1243 /* Now there is a service - full throttle */
1250 ip_vs_unbind_scheduler(svc, sched);
1251 ip_vs_service_free(svc);
1253 ip_vs_scheduler_put(sched);
1256 /* decrease the module use count */
1257 ip_vs_use_count_dec();
1264 * Edit a service and bind it with a new scheduler
1267 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1269 struct ip_vs_scheduler *sched, *old_sched;
1270 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1274 * Lookup the scheduler, by 'u->sched_name'
1276 sched = ip_vs_scheduler_get(u->sched_name);
1277 if (sched == NULL) {
1278 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1283 if (u->pe_name && *u->pe_name) {
1284 pe = ip_vs_pe_getbyname(u->pe_name);
1286 pr_info("persistence engine module ip_vs_pe_%s "
1287 "not found\n", u->pe_name);
1294 #ifdef CONFIG_IP_VS_IPV6
1295 if (u->af == AF_INET6) {
1296 __u32 plen = (__force __u32) u->netmask;
1298 if (plen < 1 || plen > 128) {
1305 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1306 if (sched != old_sched) {
1307 /* Bind the new scheduler */
1308 ret = ip_vs_bind_scheduler(svc, sched);
1313 /* Unbind the old scheduler on success */
1314 ip_vs_unbind_scheduler(svc, old_sched);
1318 * Set the flags and timeout value
1320 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1321 svc->timeout = u->timeout * HZ;
1322 svc->netmask = u->netmask;
1324 old_pe = rcu_dereference_protected(svc->pe, 1);
1326 rcu_assign_pointer(svc->pe, pe);
1329 ip_vs_scheduler_put(old_sched);
1330 ip_vs_pe_put(old_pe);
1335 * Delete a service from the service list
1336 * - The service must be unlinked, unlocked and not referenced!
1337 * - We are called under _bh lock
1339 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1341 struct ip_vs_dest *dest, *nxt;
1342 struct ip_vs_scheduler *old_sched;
1343 struct ip_vs_pe *old_pe;
1344 struct netns_ipvs *ipvs = net_ipvs(svc->net);
1346 pr_info("%s: enter\n", __func__);
1348 /* Count only IPv4 services for old get/setsockopt interface */
1349 if (svc->af == AF_INET)
1350 ipvs->num_services--;
1352 ip_vs_stop_estimator(svc->net, &svc->stats);
1354 /* Unbind scheduler */
1355 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1356 ip_vs_unbind_scheduler(svc, old_sched);
1357 ip_vs_scheduler_put(old_sched);
1359 /* Unbind persistence engine, keep svc->pe */
1360 old_pe = rcu_dereference_protected(svc->pe, 1);
1361 ip_vs_pe_put(old_pe);
1364 * Unlink the whole destination list
1366 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1367 __ip_vs_unlink_dest(svc, dest, 0);
1368 __ip_vs_del_dest(svc->net, dest, cleanup);
1372 * Update the virtual service counters
1374 if (svc->port == FTPPORT)
1375 atomic_dec(&ipvs->ftpsvc_counter);
1376 else if (svc->port == 0)
1377 atomic_dec(&ipvs->nullsvc_counter);
1380 * Free the service if nobody refers to it
1382 __ip_vs_svc_put(svc, true);
1384 /* decrease the module use count */
1385 ip_vs_use_count_dec();
1389 * Unlink a service from list and try to delete it if its refcnt reached 0
1391 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1393 /* Hold svc to avoid double release from dest_trash */
1394 atomic_inc(&svc->refcnt);
1396 * Unhash it from the service table
1398 ip_vs_svc_unhash(svc);
1400 __ip_vs_del_service(svc, cleanup);
1404 * Delete a service from the service list
1406 static int ip_vs_del_service(struct ip_vs_service *svc)
1410 ip_vs_unlink_service(svc, false);
1417 * Flush all the virtual services
1419 static int ip_vs_flush(struct net *net, bool cleanup)
1422 struct ip_vs_service *svc;
1423 struct hlist_node *n;
1426 * Flush the service table hashed by <netns,protocol,addr,port>
1428 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1429 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1431 if (net_eq(svc->net, net))
1432 ip_vs_unlink_service(svc, cleanup);
1437 * Flush the service table hashed by fwmark
1439 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1440 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1442 if (net_eq(svc->net, net))
1443 ip_vs_unlink_service(svc, cleanup);
1451 * Delete service by {netns} in the service table.
1452 * Called by __ip_vs_cleanup()
1454 void ip_vs_service_net_cleanup(struct net *net)
1457 /* Check for "full" addressed entries */
1458 mutex_lock(&__ip_vs_mutex);
1459 ip_vs_flush(net, true);
1460 mutex_unlock(&__ip_vs_mutex);
1464 /* Put all references for device (dst_cache) */
1466 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1468 struct ip_vs_dest_dst *dest_dst;
1470 spin_lock_bh(&dest->dst_lock);
1471 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1472 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1473 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1475 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1477 atomic_read(&dest->refcnt));
1478 __ip_vs_dst_cache_reset(dest);
1480 spin_unlock_bh(&dest->dst_lock);
1483 /* Netdev event receiver
1484 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1486 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1489 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1490 struct net *net = dev_net(dev);
1491 struct netns_ipvs *ipvs = net_ipvs(net);
1492 struct ip_vs_service *svc;
1493 struct ip_vs_dest *dest;
1496 if (event != NETDEV_DOWN || !ipvs)
1498 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1500 mutex_lock(&__ip_vs_mutex);
1501 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1502 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1503 if (net_eq(svc->net, net)) {
1504 list_for_each_entry(dest, &svc->destinations,
1506 ip_vs_forget_dev(dest, dev);
1511 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1512 if (net_eq(svc->net, net)) {
1513 list_for_each_entry(dest, &svc->destinations,
1515 ip_vs_forget_dev(dest, dev);
1522 spin_lock_bh(&ipvs->dest_trash_lock);
1523 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1524 ip_vs_forget_dev(dest, dev);
1526 spin_unlock_bh(&ipvs->dest_trash_lock);
1527 mutex_unlock(&__ip_vs_mutex);
1533 * Zero counters in a service or all services
1535 static int ip_vs_zero_service(struct ip_vs_service *svc)
1537 struct ip_vs_dest *dest;
1539 list_for_each_entry(dest, &svc->destinations, n_list) {
1540 ip_vs_zero_stats(&dest->stats);
1542 ip_vs_zero_stats(&svc->stats);
1546 static int ip_vs_zero_all(struct net *net)
1549 struct ip_vs_service *svc;
1551 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1552 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1553 if (net_eq(svc->net, net))
1554 ip_vs_zero_service(svc);
1558 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1559 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1560 if (net_eq(svc->net, net))
1561 ip_vs_zero_service(svc);
1565 ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
1569 #ifdef CONFIG_SYSCTL
1572 static int three = 3;
1575 proc_do_defense_mode(struct ctl_table *table, int write,
1576 void __user *buffer, size_t *lenp, loff_t *ppos)
1578 struct net *net = current->nsproxy->net_ns;
1579 int *valp = table->data;
1583 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1584 if (write && (*valp != val)) {
1585 if ((*valp < 0) || (*valp > 3)) {
1586 /* Restore the correct value */
1589 update_defense_level(net_ipvs(net));
1596 proc_do_sync_threshold(struct ctl_table *table, int write,
1597 void __user *buffer, size_t *lenp, loff_t *ppos)
1599 int *valp = table->data;
1603 /* backup the value first */
1604 memcpy(val, valp, sizeof(val));
1606 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1607 if (write && (valp[0] < 0 || valp[1] < 0 ||
1608 (valp[0] >= valp[1] && valp[1]))) {
1609 /* Restore the correct value */
1610 memcpy(valp, val, sizeof(val));
1616 proc_do_sync_mode(struct ctl_table *table, int write,
1617 void __user *buffer, size_t *lenp, loff_t *ppos)
1619 int *valp = table->data;
1623 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1624 if (write && (*valp != val)) {
1625 if ((*valp < 0) || (*valp > 1)) {
1626 /* Restore the correct value */
1634 proc_do_sync_ports(struct ctl_table *table, int write,
1635 void __user *buffer, size_t *lenp, loff_t *ppos)
1637 int *valp = table->data;
1641 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1642 if (write && (*valp != val)) {
1643 if (*valp < 1 || !is_power_of_2(*valp)) {
1644 /* Restore the correct value */
1652 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1653 * Do not change order or insert new entries without
1654 * align with netns init in ip_vs_control_net_init()
1657 static struct ctl_table vs_vars[] = {
1659 .procname = "amemthresh",
1660 .maxlen = sizeof(int),
1662 .proc_handler = proc_dointvec,
1665 .procname = "am_droprate",
1666 .maxlen = sizeof(int),
1668 .proc_handler = proc_dointvec,
1671 .procname = "drop_entry",
1672 .maxlen = sizeof(int),
1674 .proc_handler = proc_do_defense_mode,
1677 .procname = "drop_packet",
1678 .maxlen = sizeof(int),
1680 .proc_handler = proc_do_defense_mode,
1682 #ifdef CONFIG_IP_VS_NFCT
1684 .procname = "conntrack",
1685 .maxlen = sizeof(int),
1687 .proc_handler = &proc_dointvec,
1691 .procname = "secure_tcp",
1692 .maxlen = sizeof(int),
1694 .proc_handler = proc_do_defense_mode,
1697 .procname = "snat_reroute",
1698 .maxlen = sizeof(int),
1700 .proc_handler = &proc_dointvec,
1703 .procname = "sync_version",
1704 .maxlen = sizeof(int),
1706 .proc_handler = &proc_do_sync_mode,
1709 .procname = "sync_ports",
1710 .maxlen = sizeof(int),
1712 .proc_handler = &proc_do_sync_ports,
1715 .procname = "sync_persist_mode",
1716 .maxlen = sizeof(int),
1718 .proc_handler = proc_dointvec,
1721 .procname = "sync_qlen_max",
1722 .maxlen = sizeof(unsigned long),
1724 .proc_handler = proc_doulongvec_minmax,
1727 .procname = "sync_sock_size",
1728 .maxlen = sizeof(int),
1730 .proc_handler = proc_dointvec,
1733 .procname = "cache_bypass",
1734 .maxlen = sizeof(int),
1736 .proc_handler = proc_dointvec,
1739 .procname = "expire_nodest_conn",
1740 .maxlen = sizeof(int),
1742 .proc_handler = proc_dointvec,
1745 .procname = "sloppy_tcp",
1746 .maxlen = sizeof(int),
1748 .proc_handler = proc_dointvec,
1751 .procname = "sloppy_sctp",
1752 .maxlen = sizeof(int),
1754 .proc_handler = proc_dointvec,
1757 .procname = "expire_quiescent_template",
1758 .maxlen = sizeof(int),
1760 .proc_handler = proc_dointvec,
1763 .procname = "sync_threshold",
1765 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1767 .proc_handler = proc_do_sync_threshold,
1770 .procname = "sync_refresh_period",
1771 .maxlen = sizeof(int),
1773 .proc_handler = proc_dointvec_jiffies,
1776 .procname = "sync_retries",
1777 .maxlen = sizeof(int),
1779 .proc_handler = proc_dointvec_minmax,
1784 .procname = "nat_icmp_send",
1785 .maxlen = sizeof(int),
1787 .proc_handler = proc_dointvec,
1790 .procname = "pmtu_disc",
1791 .maxlen = sizeof(int),
1793 .proc_handler = proc_dointvec,
1796 .procname = "backup_only",
1797 .maxlen = sizeof(int),
1799 .proc_handler = proc_dointvec,
1801 #ifdef CONFIG_IP_VS_DEBUG
1803 .procname = "debug_level",
1804 .data = &sysctl_ip_vs_debug_level,
1805 .maxlen = sizeof(int),
1807 .proc_handler = proc_dointvec,
1812 .procname = "timeout_established",
1813 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1814 .maxlen = sizeof(int),
1816 .proc_handler = proc_dointvec_jiffies,
1819 .procname = "timeout_synsent",
1820 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1821 .maxlen = sizeof(int),
1823 .proc_handler = proc_dointvec_jiffies,
1826 .procname = "timeout_synrecv",
1827 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1828 .maxlen = sizeof(int),
1830 .proc_handler = proc_dointvec_jiffies,
1833 .procname = "timeout_finwait",
1834 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1835 .maxlen = sizeof(int),
1837 .proc_handler = proc_dointvec_jiffies,
1840 .procname = "timeout_timewait",
1841 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1842 .maxlen = sizeof(int),
1844 .proc_handler = proc_dointvec_jiffies,
1847 .procname = "timeout_close",
1848 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1849 .maxlen = sizeof(int),
1851 .proc_handler = proc_dointvec_jiffies,
1854 .procname = "timeout_closewait",
1855 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1856 .maxlen = sizeof(int),
1858 .proc_handler = proc_dointvec_jiffies,
1861 .procname = "timeout_lastack",
1862 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1863 .maxlen = sizeof(int),
1865 .proc_handler = proc_dointvec_jiffies,
1868 .procname = "timeout_listen",
1869 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1870 .maxlen = sizeof(int),
1872 .proc_handler = proc_dointvec_jiffies,
1875 .procname = "timeout_synack",
1876 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1877 .maxlen = sizeof(int),
1879 .proc_handler = proc_dointvec_jiffies,
1882 .procname = "timeout_udp",
1883 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1884 .maxlen = sizeof(int),
1886 .proc_handler = proc_dointvec_jiffies,
1889 .procname = "timeout_icmp",
1890 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1891 .maxlen = sizeof(int),
1893 .proc_handler = proc_dointvec_jiffies,
1901 #ifdef CONFIG_PROC_FS
1904 struct seq_net_private p; /* Do not move this, netns depends upon it*/
1905 struct hlist_head *table;
1910 * Write the contents of the VS rule table to a PROCfs file.
1911 * (It is kept just for backward compatibility)
1913 static inline const char *ip_vs_fwd_name(unsigned int flags)
1915 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1916 case IP_VS_CONN_F_LOCALNODE:
1918 case IP_VS_CONN_F_TUNNEL:
1920 case IP_VS_CONN_F_DROUTE:
1928 /* Get the Nth entry in the two lists */
1929 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1931 struct net *net = seq_file_net(seq);
1932 struct ip_vs_iter *iter = seq->private;
1934 struct ip_vs_service *svc;
1936 /* look in hash by protocol */
1937 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1938 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
1939 if (net_eq(svc->net, net) && pos-- == 0) {
1940 iter->table = ip_vs_svc_table;
1947 /* keep looking in fwmark */
1948 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1949 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
1951 if (net_eq(svc->net, net) && pos-- == 0) {
1952 iter->table = ip_vs_svc_fwm_table;
1962 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1966 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1970 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1972 struct hlist_node *e;
1973 struct ip_vs_iter *iter;
1974 struct ip_vs_service *svc;
1977 if (v == SEQ_START_TOKEN)
1978 return ip_vs_info_array(seq,0);
1981 iter = seq->private;
1983 if (iter->table == ip_vs_svc_table) {
1984 /* next service in table hashed by protocol */
1985 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
1987 return hlist_entry(e, struct ip_vs_service, s_list);
1989 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1990 hlist_for_each_entry_rcu(svc,
1991 &ip_vs_svc_table[iter->bucket],
1997 iter->table = ip_vs_svc_fwm_table;
2002 /* next service in hashed by fwmark */
2003 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2005 return hlist_entry(e, struct ip_vs_service, f_list);
2008 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2009 hlist_for_each_entry_rcu(svc,
2010 &ip_vs_svc_fwm_table[iter->bucket],
2018 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2025 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2027 if (v == SEQ_START_TOKEN) {
2029 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2030 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2032 "Prot LocalAddress:Port Scheduler Flags\n");
2034 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2036 const struct ip_vs_service *svc = v;
2037 const struct ip_vs_iter *iter = seq->private;
2038 const struct ip_vs_dest *dest;
2039 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2041 if (iter->table == ip_vs_svc_table) {
2042 #ifdef CONFIG_IP_VS_IPV6
2043 if (svc->af == AF_INET6)
2044 seq_printf(seq, "%s [%pI6]:%04X %s ",
2045 ip_vs_proto_name(svc->protocol),
2051 seq_printf(seq, "%s %08X:%04X %s %s ",
2052 ip_vs_proto_name(svc->protocol),
2053 ntohl(svc->addr.ip),
2056 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2058 seq_printf(seq, "FWM %08X %s %s",
2059 svc->fwmark, sched->name,
2060 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2063 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2064 seq_printf(seq, "persistent %d %08X\n",
2066 ntohl(svc->netmask));
2068 seq_putc(seq, '\n');
2070 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2071 #ifdef CONFIG_IP_VS_IPV6
2072 if (dest->af == AF_INET6)
2075 " %-7s %-6d %-10d %-10d\n",
2078 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2079 atomic_read(&dest->weight),
2080 atomic_read(&dest->activeconns),
2081 atomic_read(&dest->inactconns));
2086 "%-7s %-6d %-10d %-10d\n",
2087 ntohl(dest->addr.ip),
2089 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2090 atomic_read(&dest->weight),
2091 atomic_read(&dest->activeconns),
2092 atomic_read(&dest->inactconns));
2099 static const struct seq_operations ip_vs_info_seq_ops = {
2100 .start = ip_vs_info_seq_start,
2101 .next = ip_vs_info_seq_next,
2102 .stop = ip_vs_info_seq_stop,
2103 .show = ip_vs_info_seq_show,
2106 static int ip_vs_info_open(struct inode *inode, struct file *file)
2108 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2109 sizeof(struct ip_vs_iter));
2112 static const struct file_operations ip_vs_info_fops = {
2113 .owner = THIS_MODULE,
2114 .open = ip_vs_info_open,
2116 .llseek = seq_lseek,
2117 .release = seq_release_net,
2120 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2122 struct net *net = seq_file_single_net(seq);
2123 struct ip_vs_stats_user show;
2125 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2127 " Total Incoming Outgoing Incoming Outgoing\n");
2129 " Conns Packets Packets Bytes Bytes\n");
2131 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2132 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2133 show.inpkts, show.outpkts,
2134 (unsigned long long) show.inbytes,
2135 (unsigned long long) show.outbytes);
2137 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2139 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2140 seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2141 show.cps, show.inpps, show.outpps,
2142 show.inbps, show.outbps);
2147 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2149 return single_open_net(inode, file, ip_vs_stats_show);
2152 static const struct file_operations ip_vs_stats_fops = {
2153 .owner = THIS_MODULE,
2154 .open = ip_vs_stats_seq_open,
2156 .llseek = seq_lseek,
2157 .release = single_release_net,
2160 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2162 struct net *net = seq_file_single_net(seq);
2163 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2164 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2165 struct ip_vs_stats_user rates;
2168 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2170 " Total Incoming Outgoing Incoming Outgoing\n");
2172 "CPU Conns Packets Packets Bytes Bytes\n");
2174 for_each_possible_cpu(i) {
2175 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2177 __u64 inbytes, outbytes;
2180 start = u64_stats_fetch_begin_irq(&u->syncp);
2181 inbytes = u->ustats.inbytes;
2182 outbytes = u->ustats.outbytes;
2183 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2185 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
2186 i, u->ustats.conns, u->ustats.inpkts,
2187 u->ustats.outpkts, (__u64)inbytes,
2191 spin_lock_bh(&tot_stats->lock);
2193 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2194 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2195 tot_stats->ustats.outpkts,
2196 (unsigned long long) tot_stats->ustats.inbytes,
2197 (unsigned long long) tot_stats->ustats.outbytes);
2199 ip_vs_read_estimator(&rates, tot_stats);
2201 spin_unlock_bh(&tot_stats->lock);
2203 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2205 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2206 seq_printf(seq, " %8X %8X %8X %16X %16X\n",
2216 static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2218 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2221 static const struct file_operations ip_vs_stats_percpu_fops = {
2222 .owner = THIS_MODULE,
2223 .open = ip_vs_stats_percpu_seq_open,
2225 .llseek = seq_lseek,
2226 .release = single_release_net,
2231 * Set timeout values for tcp tcpfin udp in the timeout_table.
2233 static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
2235 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2236 struct ip_vs_proto_data *pd;
2239 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2244 #ifdef CONFIG_IP_VS_PROTO_TCP
2245 if (u->tcp_timeout) {
2246 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2247 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2248 = u->tcp_timeout * HZ;
2251 if (u->tcp_fin_timeout) {
2252 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2253 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2254 = u->tcp_fin_timeout * HZ;
2258 #ifdef CONFIG_IP_VS_PROTO_UDP
2259 if (u->udp_timeout) {
2260 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2261 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2262 = u->udp_timeout * HZ;
2269 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2270 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2271 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2272 sizeof(struct ip_vs_dest_user))
2273 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2274 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2275 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2277 static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
2278 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2279 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2280 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2281 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2282 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2283 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2284 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2285 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2286 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2287 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2288 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2291 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2292 struct ip_vs_service_user *usvc_compat)
2294 memset(usvc, 0, sizeof(*usvc));
2297 usvc->protocol = usvc_compat->protocol;
2298 usvc->addr.ip = usvc_compat->addr;
2299 usvc->port = usvc_compat->port;
2300 usvc->fwmark = usvc_compat->fwmark;
2302 /* Deep copy of sched_name is not needed here */
2303 usvc->sched_name = usvc_compat->sched_name;
2305 usvc->flags = usvc_compat->flags;
2306 usvc->timeout = usvc_compat->timeout;
2307 usvc->netmask = usvc_compat->netmask;
2310 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2311 struct ip_vs_dest_user *udest_compat)
2313 memset(udest, 0, sizeof(*udest));
2315 udest->addr.ip = udest_compat->addr;
2316 udest->port = udest_compat->port;
2317 udest->conn_flags = udest_compat->conn_flags;
2318 udest->weight = udest_compat->weight;
2319 udest->u_threshold = udest_compat->u_threshold;
2320 udest->l_threshold = udest_compat->l_threshold;
2324 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2326 struct net *net = sock_net(sk);
2328 unsigned char arg[MAX_ARG_LEN];
2329 struct ip_vs_service_user *usvc_compat;
2330 struct ip_vs_service_user_kern usvc;
2331 struct ip_vs_service *svc;
2332 struct ip_vs_dest_user *udest_compat;
2333 struct ip_vs_dest_user_kern udest;
2334 struct netns_ipvs *ipvs = net_ipvs(net);
2336 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2339 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2341 if (len < 0 || len > MAX_ARG_LEN)
2343 if (len != set_arglen[SET_CMDID(cmd)]) {
2344 pr_err("set_ctl: len %u != %u\n",
2345 len, set_arglen[SET_CMDID(cmd)]);
2349 if (copy_from_user(arg, user, len) != 0)
2352 /* increase the module use count */
2353 ip_vs_use_count_inc();
2355 /* Handle daemons since they have another lock */
2356 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2357 cmd == IP_VS_SO_SET_STOPDAEMON) {
2358 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2360 if (mutex_lock_interruptible(&ipvs->sync_mutex)) {
2364 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2365 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2368 ret = stop_sync_thread(net, dm->state);
2369 mutex_unlock(&ipvs->sync_mutex);
2373 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
2378 if (cmd == IP_VS_SO_SET_FLUSH) {
2379 /* Flush the virtual service */
2380 ret = ip_vs_flush(net, false);
2382 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2383 /* Set timeout values for (tcp tcpfin udp) */
2384 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
2388 usvc_compat = (struct ip_vs_service_user *)arg;
2389 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2391 /* We only use the new structs internally, so copy userspace compat
2392 * structs to extended internal versions */
2393 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2394 ip_vs_copy_udest_compat(&udest, udest_compat);
2396 if (cmd == IP_VS_SO_SET_ZERO) {
2397 /* if no service address is set, zero counters in all */
2398 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2399 ret = ip_vs_zero_all(net);
2404 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2405 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2406 usvc.protocol != IPPROTO_SCTP) {
2407 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2408 usvc.protocol, &usvc.addr.ip,
2409 ntohs(usvc.port), usvc.sched_name);
2414 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2416 if (usvc.fwmark == 0)
2417 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2418 &usvc.addr, usvc.port);
2420 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
2423 if (cmd != IP_VS_SO_SET_ADD
2424 && (svc == NULL || svc->protocol != usvc.protocol)) {
2430 case IP_VS_SO_SET_ADD:
2434 ret = ip_vs_add_service(net, &usvc, &svc);
2436 case IP_VS_SO_SET_EDIT:
2437 ret = ip_vs_edit_service(svc, &usvc);
2439 case IP_VS_SO_SET_DEL:
2440 ret = ip_vs_del_service(svc);
2444 case IP_VS_SO_SET_ZERO:
2445 ret = ip_vs_zero_service(svc);
2447 case IP_VS_SO_SET_ADDDEST:
2448 ret = ip_vs_add_dest(svc, &udest);
2450 case IP_VS_SO_SET_EDITDEST:
2451 ret = ip_vs_edit_dest(svc, &udest);
2453 case IP_VS_SO_SET_DELDEST:
2454 ret = ip_vs_del_dest(svc, &udest);
2461 mutex_unlock(&__ip_vs_mutex);
2463 /* decrease the module use count */
2464 ip_vs_use_count_dec();
2471 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2473 struct ip_vs_scheduler *sched;
2475 sched = rcu_dereference_protected(src->scheduler, 1);
2476 dst->protocol = src->protocol;
2477 dst->addr = src->addr.ip;
2478 dst->port = src->port;
2479 dst->fwmark = src->fwmark;
2480 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
2481 dst->flags = src->flags;
2482 dst->timeout = src->timeout / HZ;
2483 dst->netmask = src->netmask;
2484 dst->num_dests = src->num_dests;
2485 ip_vs_copy_stats(&dst->stats, &src->stats);
2489 __ip_vs_get_service_entries(struct net *net,
2490 const struct ip_vs_get_services *get,
2491 struct ip_vs_get_services __user *uptr)
2494 struct ip_vs_service *svc;
2495 struct ip_vs_service_entry entry;
2498 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2499 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2500 /* Only expose IPv4 entries to old interface */
2501 if (svc->af != AF_INET || !net_eq(svc->net, net))
2504 if (count >= get->num_services)
2506 memset(&entry, 0, sizeof(entry));
2507 ip_vs_copy_service(&entry, svc);
2508 if (copy_to_user(&uptr->entrytable[count],
2509 &entry, sizeof(entry))) {
2517 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2518 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2519 /* Only expose IPv4 entries to old interface */
2520 if (svc->af != AF_INET || !net_eq(svc->net, net))
2523 if (count >= get->num_services)
2525 memset(&entry, 0, sizeof(entry));
2526 ip_vs_copy_service(&entry, svc);
2527 if (copy_to_user(&uptr->entrytable[count],
2528 &entry, sizeof(entry))) {
2540 __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2541 struct ip_vs_get_dests __user *uptr)
2543 struct ip_vs_service *svc;
2544 union nf_inet_addr addr = { .ip = get->addr };
2549 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
2551 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2557 struct ip_vs_dest *dest;
2558 struct ip_vs_dest_entry entry;
2560 memset(&entry, 0, sizeof(entry));
2561 list_for_each_entry(dest, &svc->destinations, n_list) {
2562 if (count >= get->num_dests)
2565 entry.addr = dest->addr.ip;
2566 entry.port = dest->port;
2567 entry.conn_flags = atomic_read(&dest->conn_flags);
2568 entry.weight = atomic_read(&dest->weight);
2569 entry.u_threshold = dest->u_threshold;
2570 entry.l_threshold = dest->l_threshold;
2571 entry.activeconns = atomic_read(&dest->activeconns);
2572 entry.inactconns = atomic_read(&dest->inactconns);
2573 entry.persistconns = atomic_read(&dest->persistconns);
2574 ip_vs_copy_stats(&entry.stats, &dest->stats);
2575 if (copy_to_user(&uptr->entrytable[count],
2576 &entry, sizeof(entry))) {
2588 __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
2590 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2591 struct ip_vs_proto_data *pd;
2594 memset(u, 0, sizeof (*u));
2596 #ifdef CONFIG_IP_VS_PROTO_TCP
2597 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2598 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2599 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2601 #ifdef CONFIG_IP_VS_PROTO_UDP
2602 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2604 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2609 #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2610 #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2611 #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2612 #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2613 #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2614 #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2615 #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2617 static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
2618 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2619 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2620 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2621 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2622 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2623 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2624 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2628 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2630 unsigned char arg[128];
2632 unsigned int copylen;
2633 struct net *net = sock_net(sk);
2634 struct netns_ipvs *ipvs = net_ipvs(net);
2637 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2640 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2643 if (*len < get_arglen[GET_CMDID(cmd)]) {
2644 pr_err("get_ctl: len %u < %u\n",
2645 *len, get_arglen[GET_CMDID(cmd)]);
2649 copylen = get_arglen[GET_CMDID(cmd)];
2653 if (copy_from_user(arg, user, copylen) != 0)
2656 * Handle daemons first since it has its own locking
2658 if (cmd == IP_VS_SO_GET_DAEMON) {
2659 struct ip_vs_daemon_user d[2];
2661 memset(&d, 0, sizeof(d));
2662 if (mutex_lock_interruptible(&ipvs->sync_mutex))
2663 return -ERESTARTSYS;
2665 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2666 d[0].state = IP_VS_STATE_MASTER;
2667 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2668 sizeof(d[0].mcast_ifn));
2669 d[0].syncid = ipvs->master_syncid;
2671 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2672 d[1].state = IP_VS_STATE_BACKUP;
2673 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2674 sizeof(d[1].mcast_ifn));
2675 d[1].syncid = ipvs->backup_syncid;
2677 if (copy_to_user(user, &d, sizeof(d)) != 0)
2679 mutex_unlock(&ipvs->sync_mutex);
2683 if (mutex_lock_interruptible(&__ip_vs_mutex))
2684 return -ERESTARTSYS;
2687 case IP_VS_SO_GET_VERSION:
2691 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2692 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2693 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2697 *len = strlen(buf)+1;
2701 case IP_VS_SO_GET_INFO:
2703 struct ip_vs_getinfo info;
2704 info.version = IP_VS_VERSION_CODE;
2705 info.size = ip_vs_conn_tab_size;
2706 info.num_services = ipvs->num_services;
2707 if (copy_to_user(user, &info, sizeof(info)) != 0)
2712 case IP_VS_SO_GET_SERVICES:
2714 struct ip_vs_get_services *get;
2717 get = (struct ip_vs_get_services *)arg;
2718 size = sizeof(*get) +
2719 sizeof(struct ip_vs_service_entry) * get->num_services;
2721 pr_err("length: %u != %u\n", *len, size);
2725 ret = __ip_vs_get_service_entries(net, get, user);
2729 case IP_VS_SO_GET_SERVICE:
2731 struct ip_vs_service_entry *entry;
2732 struct ip_vs_service *svc;
2733 union nf_inet_addr addr;
2735 entry = (struct ip_vs_service_entry *)arg;
2736 addr.ip = entry->addr;
2739 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
2741 svc = __ip_vs_service_find(net, AF_INET,
2742 entry->protocol, &addr,
2746 ip_vs_copy_service(entry, svc);
2747 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2754 case IP_VS_SO_GET_DESTS:
2756 struct ip_vs_get_dests *get;
2759 get = (struct ip_vs_get_dests *)arg;
2760 size = sizeof(*get) +
2761 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2763 pr_err("length: %u != %u\n", *len, size);
2767 ret = __ip_vs_get_dest_entries(net, get, user);
2771 case IP_VS_SO_GET_TIMEOUT:
2773 struct ip_vs_timeout_user t;
2775 __ip_vs_get_timeouts(net, &t);
2776 if (copy_to_user(user, &t, sizeof(t)) != 0)
2786 mutex_unlock(&__ip_vs_mutex);
2791 static struct nf_sockopt_ops ip_vs_sockopts = {
2793 .set_optmin = IP_VS_BASE_CTL,
2794 .set_optmax = IP_VS_SO_SET_MAX+1,
2795 .set = do_ip_vs_set_ctl,
2796 .get_optmin = IP_VS_BASE_CTL,
2797 .get_optmax = IP_VS_SO_GET_MAX+1,
2798 .get = do_ip_vs_get_ctl,
2799 .owner = THIS_MODULE,
2803 * Generic Netlink interface
2806 /* IPVS genetlink family */
2807 static struct genl_family ip_vs_genl_family = {
2808 .id = GENL_ID_GENERATE,
2810 .name = IPVS_GENL_NAME,
2811 .version = IPVS_GENL_VERSION,
2812 .maxattr = IPVS_CMD_MAX,
2813 .netnsok = true, /* Make ipvsadm to work on netns */
2816 /* Policy used for first-level command attributes */
2817 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2818 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2819 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2820 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2821 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2822 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2823 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2826 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2827 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2828 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2829 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2830 .len = IP_VS_IFNAME_MAXLEN },
2831 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2834 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2835 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2836 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2837 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2838 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2839 .len = sizeof(union nf_inet_addr) },
2840 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2841 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2842 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2843 .len = IP_VS_SCHEDNAME_MAXLEN },
2844 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2845 .len = IP_VS_PENAME_MAXLEN },
2846 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2847 .len = sizeof(struct ip_vs_flags) },
2848 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2849 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2850 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2853 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2854 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2855 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2856 .len = sizeof(union nf_inet_addr) },
2857 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2858 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2859 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2860 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2861 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2862 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2863 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2864 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2865 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2868 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2869 struct ip_vs_stats *stats)
2871 struct ip_vs_stats_user ustats;
2872 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2876 ip_vs_copy_stats(&ustats, stats);
2878 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) ||
2879 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) ||
2880 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) ||
2881 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) ||
2882 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) ||
2883 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) ||
2884 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) ||
2885 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) ||
2886 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) ||
2887 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps))
2888 goto nla_put_failure;
2889 nla_nest_end(skb, nl_stats);
2894 nla_nest_cancel(skb, nl_stats);
2898 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2899 struct ip_vs_service *svc)
2901 struct ip_vs_scheduler *sched;
2902 struct ip_vs_pe *pe;
2903 struct nlattr *nl_service;
2904 struct ip_vs_flags flags = { .flags = svc->flags,
2907 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2911 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2912 goto nla_put_failure;
2914 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2915 goto nla_put_failure;
2917 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2918 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2919 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2920 goto nla_put_failure;
2923 sched = rcu_dereference_protected(svc->scheduler, 1);
2924 pe = rcu_dereference_protected(svc->pe, 1);
2925 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
2926 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
2927 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2928 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2929 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2930 goto nla_put_failure;
2931 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2932 goto nla_put_failure;
2934 nla_nest_end(skb, nl_service);
2939 nla_nest_cancel(skb, nl_service);
2943 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2944 struct ip_vs_service *svc,
2945 struct netlink_callback *cb)
2949 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2950 &ip_vs_genl_family, NLM_F_MULTI,
2951 IPVS_CMD_NEW_SERVICE);
2955 if (ip_vs_genl_fill_service(skb, svc) < 0)
2956 goto nla_put_failure;
2958 return genlmsg_end(skb, hdr);
2961 genlmsg_cancel(skb, hdr);
2965 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2966 struct netlink_callback *cb)
2969 int start = cb->args[0];
2970 struct ip_vs_service *svc;
2971 struct net *net = skb_sknet(skb);
2973 mutex_lock(&__ip_vs_mutex);
2974 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2975 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2976 if (++idx <= start || !net_eq(svc->net, net))
2978 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2980 goto nla_put_failure;
2985 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2986 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2987 if (++idx <= start || !net_eq(svc->net, net))
2989 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2991 goto nla_put_failure;
2997 mutex_unlock(&__ip_vs_mutex);
3003 static int ip_vs_genl_parse_service(struct net *net,
3004 struct ip_vs_service_user_kern *usvc,
3005 struct nlattr *nla, int full_entry,
3006 struct ip_vs_service **ret_svc)
3008 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3009 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3010 struct ip_vs_service *svc;
3012 /* Parse mandatory identifying service fields first */
3014 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
3017 nla_af = attrs[IPVS_SVC_ATTR_AF];
3018 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3019 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3020 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3021 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3023 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3026 memset(usvc, 0, sizeof(*usvc));
3028 usvc->af = nla_get_u16(nla_af);
3029 #ifdef CONFIG_IP_VS_IPV6
3030 if (usvc->af != AF_INET && usvc->af != AF_INET6)
3032 if (usvc->af != AF_INET)
3034 return -EAFNOSUPPORT;
3037 usvc->protocol = IPPROTO_TCP;
3038 usvc->fwmark = nla_get_u32(nla_fwmark);
3040 usvc->protocol = nla_get_u16(nla_protocol);
3041 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3042 usvc->port = nla_get_be16(nla_port);
3048 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
3050 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
3051 &usvc->addr, usvc->port);
3055 /* If a full entry was requested, check for the additional fields */
3057 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3059 struct ip_vs_flags flags;
3061 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3062 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3063 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3064 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3065 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3067 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3070 nla_memcpy(&flags, nla_flags, sizeof(flags));
3072 /* prefill flags from service if it already exists */
3074 usvc->flags = svc->flags;
3076 /* set new flags from userland */
3077 usvc->flags = (usvc->flags & ~flags.mask) |
3078 (flags.flags & flags.mask);
3079 usvc->sched_name = nla_data(nla_sched);
3080 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3081 usvc->timeout = nla_get_u32(nla_timeout);
3082 usvc->netmask = nla_get_be32(nla_netmask);
3088 static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3091 struct ip_vs_service_user_kern usvc;
3092 struct ip_vs_service *svc;
3095 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3096 return ret ? ERR_PTR(ret) : svc;
3099 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3101 struct nlattr *nl_dest;
3103 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3107 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3108 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3109 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3110 (atomic_read(&dest->conn_flags) &
3111 IP_VS_CONN_F_FWD_MASK)) ||
3112 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3113 atomic_read(&dest->weight)) ||
3114 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3115 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3116 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3117 atomic_read(&dest->activeconns)) ||
3118 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3119 atomic_read(&dest->inactconns)) ||
3120 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3121 atomic_read(&dest->persistconns)))
3122 goto nla_put_failure;
3123 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
3124 goto nla_put_failure;
3126 nla_nest_end(skb, nl_dest);
3131 nla_nest_cancel(skb, nl_dest);
3135 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3136 struct netlink_callback *cb)
3140 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3141 &ip_vs_genl_family, NLM_F_MULTI,
3146 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3147 goto nla_put_failure;
3149 return genlmsg_end(skb, hdr);
3152 genlmsg_cancel(skb, hdr);
3156 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3157 struct netlink_callback *cb)
3160 int start = cb->args[0];
3161 struct ip_vs_service *svc;
3162 struct ip_vs_dest *dest;
3163 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3164 struct net *net = skb_sknet(skb);
3166 mutex_lock(&__ip_vs_mutex);
3168 /* Try to find the service for which to dump destinations */
3169 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3170 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3174 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3175 if (IS_ERR(svc) || svc == NULL)
3178 /* Dump the destinations */
3179 list_for_each_entry(dest, &svc->destinations, n_list) {
3182 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3184 goto nla_put_failure;
3192 mutex_unlock(&__ip_vs_mutex);
3197 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3198 struct nlattr *nla, int full_entry)
3200 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3201 struct nlattr *nla_addr, *nla_port;
3203 /* Parse mandatory identifying destination fields first */
3205 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3208 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3209 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3211 if (!(nla_addr && nla_port))
3214 memset(udest, 0, sizeof(*udest));
3216 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3217 udest->port = nla_get_be16(nla_port);
3219 /* If a full entry was requested, check for the additional fields */
3221 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3224 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3225 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3226 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3227 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3229 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3232 udest->conn_flags = nla_get_u32(nla_fwd)
3233 & IP_VS_CONN_F_FWD_MASK;
3234 udest->weight = nla_get_u32(nla_weight);
3235 udest->u_threshold = nla_get_u32(nla_u_thresh);
3236 udest->l_threshold = nla_get_u32(nla_l_thresh);
3242 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3243 const char *mcast_ifn, __u32 syncid)
3245 struct nlattr *nl_daemon;
3247 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3251 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3252 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3253 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3254 goto nla_put_failure;
3255 nla_nest_end(skb, nl_daemon);
3260 nla_nest_cancel(skb, nl_daemon);
3264 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3265 const char *mcast_ifn, __u32 syncid,
3266 struct netlink_callback *cb)
3269 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3270 &ip_vs_genl_family, NLM_F_MULTI,
3271 IPVS_CMD_NEW_DAEMON);
3275 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3276 goto nla_put_failure;
3278 return genlmsg_end(skb, hdr);
3281 genlmsg_cancel(skb, hdr);
3285 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3286 struct netlink_callback *cb)
3288 struct net *net = skb_sknet(skb);
3289 struct netns_ipvs *ipvs = net_ipvs(net);
3291 mutex_lock(&ipvs->sync_mutex);
3292 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3293 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3294 ipvs->master_mcast_ifn,
3295 ipvs->master_syncid, cb) < 0)
3296 goto nla_put_failure;
3301 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3302 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3303 ipvs->backup_mcast_ifn,
3304 ipvs->backup_syncid, cb) < 0)
3305 goto nla_put_failure;
3311 mutex_unlock(&ipvs->sync_mutex);
3316 static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3318 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3319 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3320 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3323 return start_sync_thread(net,
3324 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3325 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3326 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3329 static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3331 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3334 return stop_sync_thread(net,
3335 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3338 static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3340 struct ip_vs_timeout_user t;
3342 __ip_vs_get_timeouts(net, &t);
3344 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3345 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3347 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3349 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3351 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3352 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3354 return ip_vs_set_timeout(net, &t);
3357 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3361 struct netns_ipvs *ipvs;
3363 net = skb_sknet(skb);
3364 ipvs = net_ipvs(net);
3365 cmd = info->genlhdr->cmd;
3367 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3368 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3370 mutex_lock(&ipvs->sync_mutex);
3371 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3372 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3373 info->attrs[IPVS_CMD_ATTR_DAEMON],
3374 ip_vs_daemon_policy)) {
3379 if (cmd == IPVS_CMD_NEW_DAEMON)
3380 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3382 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3384 mutex_unlock(&ipvs->sync_mutex);
3389 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3391 struct ip_vs_service *svc = NULL;
3392 struct ip_vs_service_user_kern usvc;
3393 struct ip_vs_dest_user_kern udest;
3395 int need_full_svc = 0, need_full_dest = 0;
3398 net = skb_sknet(skb);
3399 cmd = info->genlhdr->cmd;
3401 mutex_lock(&__ip_vs_mutex);
3403 if (cmd == IPVS_CMD_FLUSH) {
3404 ret = ip_vs_flush(net, false);
3406 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3407 ret = ip_vs_genl_set_config(net, info->attrs);
3409 } else if (cmd == IPVS_CMD_ZERO &&
3410 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3411 ret = ip_vs_zero_all(net);
3415 /* All following commands require a service argument, so check if we
3416 * received a valid one. We need a full service specification when
3417 * adding / editing a service. Only identifying members otherwise. */
3418 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3421 ret = ip_vs_genl_parse_service(net, &usvc,
3422 info->attrs[IPVS_CMD_ATTR_SERVICE],
3423 need_full_svc, &svc);
3427 /* Unless we're adding a new service, the service must already exist */
3428 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3433 /* Destination commands require a valid destination argument. For
3434 * adding / editing a destination, we need a full destination
3436 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3437 cmd == IPVS_CMD_DEL_DEST) {
3438 if (cmd != IPVS_CMD_DEL_DEST)
3441 ret = ip_vs_genl_parse_dest(&udest,
3442 info->attrs[IPVS_CMD_ATTR_DEST],
3449 case IPVS_CMD_NEW_SERVICE:
3451 ret = ip_vs_add_service(net, &usvc, &svc);
3455 case IPVS_CMD_SET_SERVICE:
3456 ret = ip_vs_edit_service(svc, &usvc);
3458 case IPVS_CMD_DEL_SERVICE:
3459 ret = ip_vs_del_service(svc);
3460 /* do not use svc, it can be freed */
3462 case IPVS_CMD_NEW_DEST:
3463 ret = ip_vs_add_dest(svc, &udest);
3465 case IPVS_CMD_SET_DEST:
3466 ret = ip_vs_edit_dest(svc, &udest);
3468 case IPVS_CMD_DEL_DEST:
3469 ret = ip_vs_del_dest(svc, &udest);
3472 ret = ip_vs_zero_service(svc);
3479 mutex_unlock(&__ip_vs_mutex);
3484 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3486 struct sk_buff *msg;
3488 int ret, cmd, reply_cmd;
3491 net = skb_sknet(skb);
3492 cmd = info->genlhdr->cmd;
3494 if (cmd == IPVS_CMD_GET_SERVICE)
3495 reply_cmd = IPVS_CMD_NEW_SERVICE;
3496 else if (cmd == IPVS_CMD_GET_INFO)
3497 reply_cmd = IPVS_CMD_SET_INFO;
3498 else if (cmd == IPVS_CMD_GET_CONFIG)
3499 reply_cmd = IPVS_CMD_SET_CONFIG;
3501 pr_err("unknown Generic Netlink command\n");
3505 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3509 mutex_lock(&__ip_vs_mutex);
3511 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3513 goto nla_put_failure;
3516 case IPVS_CMD_GET_SERVICE:
3518 struct ip_vs_service *svc;
3520 svc = ip_vs_genl_find_service(net,
3521 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3526 ret = ip_vs_genl_fill_service(msg, svc);
3528 goto nla_put_failure;
3537 case IPVS_CMD_GET_CONFIG:
3539 struct ip_vs_timeout_user t;
3541 __ip_vs_get_timeouts(net, &t);
3542 #ifdef CONFIG_IP_VS_PROTO_TCP
3543 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3545 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3547 goto nla_put_failure;
3549 #ifdef CONFIG_IP_VS_PROTO_UDP
3550 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3551 goto nla_put_failure;
3557 case IPVS_CMD_GET_INFO:
3558 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3559 IP_VS_VERSION_CODE) ||
3560 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3561 ip_vs_conn_tab_size))
3562 goto nla_put_failure;
3566 genlmsg_end(msg, reply);
3567 ret = genlmsg_reply(msg, info);
3571 pr_err("not enough space in Netlink message\n");
3577 mutex_unlock(&__ip_vs_mutex);
3583 static const struct genl_ops ip_vs_genl_ops[] = {
3585 .cmd = IPVS_CMD_NEW_SERVICE,
3586 .flags = GENL_ADMIN_PERM,
3587 .policy = ip_vs_cmd_policy,
3588 .doit = ip_vs_genl_set_cmd,
3591 .cmd = IPVS_CMD_SET_SERVICE,
3592 .flags = GENL_ADMIN_PERM,
3593 .policy = ip_vs_cmd_policy,
3594 .doit = ip_vs_genl_set_cmd,
3597 .cmd = IPVS_CMD_DEL_SERVICE,
3598 .flags = GENL_ADMIN_PERM,
3599 .policy = ip_vs_cmd_policy,
3600 .doit = ip_vs_genl_set_cmd,
3603 .cmd = IPVS_CMD_GET_SERVICE,
3604 .flags = GENL_ADMIN_PERM,
3605 .doit = ip_vs_genl_get_cmd,
3606 .dumpit = ip_vs_genl_dump_services,
3607 .policy = ip_vs_cmd_policy,
3610 .cmd = IPVS_CMD_NEW_DEST,
3611 .flags = GENL_ADMIN_PERM,
3612 .policy = ip_vs_cmd_policy,
3613 .doit = ip_vs_genl_set_cmd,
3616 .cmd = IPVS_CMD_SET_DEST,
3617 .flags = GENL_ADMIN_PERM,
3618 .policy = ip_vs_cmd_policy,
3619 .doit = ip_vs_genl_set_cmd,
3622 .cmd = IPVS_CMD_DEL_DEST,
3623 .flags = GENL_ADMIN_PERM,
3624 .policy = ip_vs_cmd_policy,
3625 .doit = ip_vs_genl_set_cmd,
3628 .cmd = IPVS_CMD_GET_DEST,
3629 .flags = GENL_ADMIN_PERM,
3630 .policy = ip_vs_cmd_policy,
3631 .dumpit = ip_vs_genl_dump_dests,
3634 .cmd = IPVS_CMD_NEW_DAEMON,
3635 .flags = GENL_ADMIN_PERM,
3636 .policy = ip_vs_cmd_policy,
3637 .doit = ip_vs_genl_set_daemon,
3640 .cmd = IPVS_CMD_DEL_DAEMON,
3641 .flags = GENL_ADMIN_PERM,
3642 .policy = ip_vs_cmd_policy,
3643 .doit = ip_vs_genl_set_daemon,
3646 .cmd = IPVS_CMD_GET_DAEMON,
3647 .flags = GENL_ADMIN_PERM,
3648 .dumpit = ip_vs_genl_dump_daemons,
3651 .cmd = IPVS_CMD_SET_CONFIG,
3652 .flags = GENL_ADMIN_PERM,
3653 .policy = ip_vs_cmd_policy,
3654 .doit = ip_vs_genl_set_cmd,
3657 .cmd = IPVS_CMD_GET_CONFIG,
3658 .flags = GENL_ADMIN_PERM,
3659 .doit = ip_vs_genl_get_cmd,
3662 .cmd = IPVS_CMD_GET_INFO,
3663 .flags = GENL_ADMIN_PERM,
3664 .doit = ip_vs_genl_get_cmd,
3667 .cmd = IPVS_CMD_ZERO,
3668 .flags = GENL_ADMIN_PERM,
3669 .policy = ip_vs_cmd_policy,
3670 .doit = ip_vs_genl_set_cmd,
3673 .cmd = IPVS_CMD_FLUSH,
3674 .flags = GENL_ADMIN_PERM,
3675 .doit = ip_vs_genl_set_cmd,
3679 static int __init ip_vs_genl_register(void)
3681 return genl_register_family_with_ops(&ip_vs_genl_family,
3685 static void ip_vs_genl_unregister(void)
3687 genl_unregister_family(&ip_vs_genl_family);
3690 /* End of Generic Netlink interface definitions */
3693 * per netns intit/exit func.
3695 #ifdef CONFIG_SYSCTL
3696 static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3699 struct netns_ipvs *ipvs = net_ipvs(net);
3700 struct ctl_table *tbl;
3702 atomic_set(&ipvs->dropentry, 0);
3703 spin_lock_init(&ipvs->dropentry_lock);
3704 spin_lock_init(&ipvs->droppacket_lock);
3705 spin_lock_init(&ipvs->securetcp_lock);
3707 if (!net_eq(net, &init_net)) {
3708 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3712 /* Don't export sysctls to unprivileged users */
3713 if (net->user_ns != &init_user_ns)
3714 tbl[0].procname = NULL;
3717 /* Initialize sysctl defaults */
3719 ipvs->sysctl_amemthresh = 1024;
3720 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3721 ipvs->sysctl_am_droprate = 10;
3722 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3723 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3724 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3725 #ifdef CONFIG_IP_VS_NFCT
3726 tbl[idx++].data = &ipvs->sysctl_conntrack;
3728 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3729 ipvs->sysctl_snat_reroute = 1;
3730 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3731 ipvs->sysctl_sync_ver = 1;
3732 tbl[idx++].data = &ipvs->sysctl_sync_ver;
3733 ipvs->sysctl_sync_ports = 1;
3734 tbl[idx++].data = &ipvs->sysctl_sync_ports;
3735 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
3736 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3737 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3738 ipvs->sysctl_sync_sock_size = 0;
3739 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
3740 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3741 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3742 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
3743 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
3744 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3745 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3746 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3747 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3748 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
3749 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3750 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3751 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3752 tbl[idx++].data = &ipvs->sysctl_sync_retries;
3753 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3754 ipvs->sysctl_pmtu_disc = 1;
3755 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3756 tbl[idx++].data = &ipvs->sysctl_backup_only;
3759 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
3760 if (ipvs->sysctl_hdr == NULL) {
3761 if (!net_eq(net, &init_net))
3765 ip_vs_start_estimator(net, &ipvs->tot_stats);
3766 ipvs->sysctl_tbl = tbl;
3767 /* Schedule defense work */
3768 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3769 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3774 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
3776 struct netns_ipvs *ipvs = net_ipvs(net);
3778 cancel_delayed_work_sync(&ipvs->defense_work);
3779 cancel_work_sync(&ipvs->defense_work.work);
3780 unregister_net_sysctl_table(ipvs->sysctl_hdr);
3781 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3786 static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3787 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3791 static struct notifier_block ip_vs_dst_notifier = {
3792 .notifier_call = ip_vs_dst_event,
3795 int __net_init ip_vs_control_net_init(struct net *net)
3798 struct netns_ipvs *ipvs = net_ipvs(net);
3800 /* Initialize rs_table */
3801 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
3802 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
3804 INIT_LIST_HEAD(&ipvs->dest_trash);
3805 spin_lock_init(&ipvs->dest_trash_lock);
3806 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3807 (unsigned long) net);
3808 atomic_set(&ipvs->ftpsvc_counter, 0);
3809 atomic_set(&ipvs->nullsvc_counter, 0);
3812 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3813 if (!ipvs->tot_stats.cpustats)
3816 for_each_possible_cpu(i) {
3817 struct ip_vs_cpu_stats *ipvs_tot_stats;
3818 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3819 u64_stats_init(&ipvs_tot_stats->syncp);
3822 spin_lock_init(&ipvs->tot_stats.lock);
3824 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3825 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3826 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3827 &ip_vs_stats_percpu_fops);
3829 if (ip_vs_control_net_init_sysctl(net))
3835 free_percpu(ipvs->tot_stats.cpustats);
3839 void __net_exit ip_vs_control_net_cleanup(struct net *net)
3841 struct netns_ipvs *ipvs = net_ipvs(net);
3843 ip_vs_trash_cleanup(net);
3844 ip_vs_control_net_cleanup_sysctl(net);
3845 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3846 remove_proc_entry("ip_vs_stats", net->proc_net);
3847 remove_proc_entry("ip_vs", net->proc_net);
3848 free_percpu(ipvs->tot_stats.cpustats);
3851 int __init ip_vs_register_nl_ioctl(void)
3855 ret = nf_register_sockopt(&ip_vs_sockopts);
3857 pr_err("cannot register sockopt.\n");
3861 ret = ip_vs_genl_register();
3863 pr_err("cannot register Generic Netlink interface.\n");
3869 nf_unregister_sockopt(&ip_vs_sockopts);
3874 void ip_vs_unregister_nl_ioctl(void)
3876 ip_vs_genl_unregister();
3877 nf_unregister_sockopt(&ip_vs_sockopts);
3880 int __init ip_vs_control_init(void)
3887 /* Initialize svc_table, ip_vs_svc_fwm_table */
3888 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3889 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3890 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3893 smp_wmb(); /* Do we really need it now ? */
3895 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3904 void ip_vs_control_cleanup(void)
3907 unregister_netdevice_notifier(&ip_vs_dst_notifier);