1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS An implementation of the IP virtual server support for the
4 * LINUX operating system. IPVS is now implemented as a module
5 * over the NetFilter framework. IPVS can be used to build a
6 * high-performance and highly available server based on a
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/capability.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/workqueue.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/mutex.h>
34 #include <net/net_namespace.h>
35 #include <linux/nsproxy.h>
37 #ifdef CONFIG_IP_VS_IPV6
39 #include <net/ip6_route.h>
40 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
42 #include <net/route.h>
44 #include <net/genetlink.h>
46 #include <linux/uaccess.h>
48 #include <net/ip_vs.h>
50 MODULE_ALIAS_GENL_FAMILY(IPVS_GENL_NAME);
52 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
53 static DEFINE_MUTEX(__ip_vs_mutex);
55 /* sysctl variables */
57 #ifdef CONFIG_IP_VS_DEBUG
58 static int sysctl_ip_vs_debug_level = 0;
60 int ip_vs_get_debug_level(void)
62 return sysctl_ip_vs_debug_level;
68 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup);
71 #ifdef CONFIG_IP_VS_IPV6
72 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
73 static bool __ip_vs_addr_is_local_v6(struct net *net,
74 const struct in6_addr *addr)
79 struct dst_entry *dst = ip6_route_output(net, NULL, &fl6);
82 is_local = !dst->error && dst->dev && (dst->dev->flags & IFF_LOOPBACK);
91 * update_defense_level is called from keventd and from sysctl,
92 * so it needs to protect itself from softirqs
94 static void update_defense_level(struct netns_ipvs *ipvs)
101 /* we only count free and buffered memory (in pages) */
103 availmem = i.freeram + i.bufferram;
104 /* however in linux 2.5 the i.bufferram is total page cache size,
106 /* si_swapinfo(&i); */
107 /* availmem = availmem - (i.totalswap - i.freeswap); */
109 nomem = (availmem < ipvs->sysctl_amemthresh);
114 spin_lock(&ipvs->dropentry_lock);
115 switch (ipvs->sysctl_drop_entry) {
117 atomic_set(&ipvs->dropentry, 0);
121 atomic_set(&ipvs->dropentry, 1);
122 ipvs->sysctl_drop_entry = 2;
124 atomic_set(&ipvs->dropentry, 0);
129 atomic_set(&ipvs->dropentry, 1);
131 atomic_set(&ipvs->dropentry, 0);
132 ipvs->sysctl_drop_entry = 1;
136 atomic_set(&ipvs->dropentry, 1);
139 spin_unlock(&ipvs->dropentry_lock);
142 spin_lock(&ipvs->droppacket_lock);
143 switch (ipvs->sysctl_drop_packet) {
149 ipvs->drop_rate = ipvs->drop_counter
150 = ipvs->sysctl_amemthresh /
151 (ipvs->sysctl_amemthresh-availmem);
152 ipvs->sysctl_drop_packet = 2;
159 ipvs->drop_rate = ipvs->drop_counter
160 = ipvs->sysctl_amemthresh /
161 (ipvs->sysctl_amemthresh-availmem);
164 ipvs->sysctl_drop_packet = 1;
168 ipvs->drop_rate = ipvs->sysctl_am_droprate;
171 spin_unlock(&ipvs->droppacket_lock);
174 spin_lock(&ipvs->securetcp_lock);
175 switch (ipvs->sysctl_secure_tcp) {
177 if (ipvs->old_secure_tcp >= 2)
182 if (ipvs->old_secure_tcp < 2)
184 ipvs->sysctl_secure_tcp = 2;
186 if (ipvs->old_secure_tcp >= 2)
192 if (ipvs->old_secure_tcp < 2)
195 if (ipvs->old_secure_tcp >= 2)
197 ipvs->sysctl_secure_tcp = 1;
201 if (ipvs->old_secure_tcp < 2)
205 ipvs->old_secure_tcp = ipvs->sysctl_secure_tcp;
207 ip_vs_protocol_timeout_change(ipvs,
208 ipvs->sysctl_secure_tcp > 1);
209 spin_unlock(&ipvs->securetcp_lock);
214 /* Handler for delayed work for expiring no
215 * destination connections
217 static void expire_nodest_conn_handler(struct work_struct *work)
219 struct netns_ipvs *ipvs;
221 ipvs = container_of(work, struct netns_ipvs,
222 expire_nodest_conn_work.work);
223 ip_vs_expire_nodest_conn_flush(ipvs);
227 * Timer for checking the defense
229 #define DEFENSE_TIMER_PERIOD 1*HZ
231 static void defense_work_handler(struct work_struct *work)
233 struct netns_ipvs *ipvs =
234 container_of(work, struct netns_ipvs, defense_work.work);
236 update_defense_level(ipvs);
237 if (atomic_read(&ipvs->dropentry))
238 ip_vs_random_dropentry(ipvs);
239 queue_delayed_work(system_long_wq, &ipvs->defense_work,
240 DEFENSE_TIMER_PERIOD);
245 ip_vs_use_count_inc(void)
247 return try_module_get(THIS_MODULE);
251 ip_vs_use_count_dec(void)
253 module_put(THIS_MODULE);
258 * Hash table: for virtual service lookups
260 #define IP_VS_SVC_TAB_BITS 8
261 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
262 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
264 /* the service table hashed by <protocol, addr, port> */
265 static struct hlist_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
266 /* the service table hashed by fwmark */
267 static struct hlist_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
271 * Returns hash value for virtual service
273 static inline unsigned int
274 ip_vs_svc_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
275 const union nf_inet_addr *addr, __be16 port)
277 unsigned int porth = ntohs(port);
278 __be32 addr_fold = addr->ip;
281 #ifdef CONFIG_IP_VS_IPV6
283 addr_fold = addr->ip6[0]^addr->ip6[1]^
284 addr->ip6[2]^addr->ip6[3];
286 ahash = ntohl(addr_fold);
287 ahash ^= ((size_t) ipvs >> 8);
289 return (proto ^ ahash ^ (porth >> IP_VS_SVC_TAB_BITS) ^ porth) &
294 * Returns hash value of fwmark for virtual service lookup
296 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs *ipvs, __u32 fwmark)
298 return (((size_t)ipvs>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
302 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
303 * or in the ip_vs_svc_fwm_table by fwmark.
304 * Should be called with locked tables.
306 static int ip_vs_svc_hash(struct ip_vs_service *svc)
310 if (svc->flags & IP_VS_SVC_F_HASHED) {
311 pr_err("%s(): request for already hashed, called from %pS\n",
312 __func__, __builtin_return_address(0));
316 if (svc->fwmark == 0) {
318 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
320 hash = ip_vs_svc_hashkey(svc->ipvs, svc->af, svc->protocol,
321 &svc->addr, svc->port);
322 hlist_add_head_rcu(&svc->s_list, &ip_vs_svc_table[hash]);
325 * Hash it by fwmark in svc_fwm_table
327 hash = ip_vs_svc_fwm_hashkey(svc->ipvs, svc->fwmark);
328 hlist_add_head_rcu(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
331 svc->flags |= IP_VS_SVC_F_HASHED;
332 /* increase its refcnt because it is referenced by the svc table */
333 atomic_inc(&svc->refcnt);
339 * Unhashes a service from svc_table / svc_fwm_table.
340 * Should be called with locked tables.
342 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
344 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
345 pr_err("%s(): request for unhash flagged, called from %pS\n",
346 __func__, __builtin_return_address(0));
350 if (svc->fwmark == 0) {
351 /* Remove it from the svc_table table */
352 hlist_del_rcu(&svc->s_list);
354 /* Remove it from the svc_fwm_table table */
355 hlist_del_rcu(&svc->f_list);
358 svc->flags &= ~IP_VS_SVC_F_HASHED;
359 atomic_dec(&svc->refcnt);
365 * Get service by {netns, proto,addr,port} in the service table.
367 static inline struct ip_vs_service *
368 __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,
369 const union nf_inet_addr *vaddr, __be16 vport)
372 struct ip_vs_service *svc;
374 /* Check for "full" addressed entries */
375 hash = ip_vs_svc_hashkey(ipvs, af, protocol, vaddr, vport);
377 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[hash], s_list) {
379 && ip_vs_addr_equal(af, &svc->addr, vaddr)
380 && (svc->port == vport)
381 && (svc->protocol == protocol)
382 && (svc->ipvs == ipvs)) {
393 * Get service by {fwmark} in the service table.
395 static inline struct ip_vs_service *
396 __ip_vs_svc_fwm_find(struct netns_ipvs *ipvs, int af, __u32 fwmark)
399 struct ip_vs_service *svc;
401 /* Check for fwmark addressed entries */
402 hash = ip_vs_svc_fwm_hashkey(ipvs, fwmark);
404 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[hash], f_list) {
405 if (svc->fwmark == fwmark && svc->af == af
406 && (svc->ipvs == ipvs)) {
415 /* Find service, called under RCU lock */
416 struct ip_vs_service *
417 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
418 const union nf_inet_addr *vaddr, __be16 vport)
420 struct ip_vs_service *svc;
423 * Check the table hashed by fwmark first
426 svc = __ip_vs_svc_fwm_find(ipvs, af, fwmark);
432 * Check the table hashed by <protocol,addr,port>
433 * for "full" addressed entries
435 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, vport);
437 if (!svc && protocol == IPPROTO_TCP &&
438 atomic_read(&ipvs->ftpsvc_counter) &&
439 (vport == FTPDATA || !inet_port_requires_bind_service(ipvs->net, ntohs(vport)))) {
441 * Check if ftp service entry exists, the packet
442 * might belong to FTP data connections.
444 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, FTPPORT);
448 && atomic_read(&ipvs->nullsvc_counter)) {
450 * Check if the catch-all port (port zero) exists
452 svc = __ip_vs_service_find(ipvs, af, protocol, vaddr, 0);
456 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
457 fwmark, ip_vs_proto_name(protocol),
458 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
459 svc ? "hit" : "not hit");
466 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
468 atomic_inc(&svc->refcnt);
469 rcu_assign_pointer(dest->svc, svc);
472 static void ip_vs_service_free(struct ip_vs_service *svc)
474 free_percpu(svc->stats.cpustats);
478 static void ip_vs_service_rcu_free(struct rcu_head *head)
480 struct ip_vs_service *svc;
482 svc = container_of(head, struct ip_vs_service, rcu_head);
483 ip_vs_service_free(svc);
486 static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
488 if (atomic_dec_and_test(&svc->refcnt)) {
489 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
491 IP_VS_DBG_ADDR(svc->af, &svc->addr),
494 call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
496 ip_vs_service_free(svc);
502 * Returns hash value for real service
504 static inline unsigned int ip_vs_rs_hashkey(int af,
505 const union nf_inet_addr *addr,
508 unsigned int porth = ntohs(port);
509 __be32 addr_fold = addr->ip;
511 #ifdef CONFIG_IP_VS_IPV6
513 addr_fold = addr->ip6[0]^addr->ip6[1]^
514 addr->ip6[2]^addr->ip6[3];
517 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
521 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
522 static void ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
527 if (dest->in_rs_table)
530 switch (IP_VS_DFWD_METHOD(dest)) {
531 case IP_VS_CONN_F_MASQ:
534 case IP_VS_CONN_F_TUNNEL:
535 switch (dest->tun_type) {
536 case IP_VS_CONN_F_TUNNEL_TYPE_GUE:
537 port = dest->tun_port;
539 case IP_VS_CONN_F_TUNNEL_TYPE_IPIP:
540 case IP_VS_CONN_F_TUNNEL_TYPE_GRE:
552 * Hash by proto,addr,port,
553 * which are the parameters of the real service.
555 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, port);
557 hlist_add_head_rcu(&dest->d_list, &ipvs->rs_table[hash]);
558 dest->in_rs_table = 1;
561 /* Unhash ip_vs_dest from rs_table. */
562 static void ip_vs_rs_unhash(struct ip_vs_dest *dest)
565 * Remove it from the rs_table table.
567 if (dest->in_rs_table) {
568 hlist_del_rcu(&dest->d_list);
569 dest->in_rs_table = 0;
573 /* Check if real service by <proto,addr,port> is present */
574 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
575 const union nf_inet_addr *daddr, __be16 dport)
578 struct ip_vs_dest *dest;
580 /* Check for "full" addressed entries */
581 hash = ip_vs_rs_hashkey(af, daddr, dport);
583 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
584 if (dest->port == dport &&
586 ip_vs_addr_equal(af, &dest->addr, daddr) &&
587 (dest->protocol == protocol || dest->vfwmark) &&
588 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
597 /* Find real service record by <proto,addr,port>.
598 * In case of multiple records with the same <proto,addr,port>, only
599 * the first found record is returned.
601 * To be called under RCU lock.
603 struct ip_vs_dest *ip_vs_find_real_service(struct netns_ipvs *ipvs, int af,
605 const union nf_inet_addr *daddr,
609 struct ip_vs_dest *dest;
611 /* Check for "full" addressed entries */
612 hash = ip_vs_rs_hashkey(af, daddr, dport);
614 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
615 if (dest->port == dport &&
617 ip_vs_addr_equal(af, &dest->addr, daddr) &&
618 (dest->protocol == protocol || dest->vfwmark) &&
619 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_MASQ) {
628 /* Find real service record by <af,addr,tun_port>.
629 * In case of multiple records with the same <af,addr,tun_port>, only
630 * the first found record is returned.
632 * To be called under RCU lock.
634 struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
635 const union nf_inet_addr *daddr,
638 struct ip_vs_dest *dest;
641 /* Check for "full" addressed entries */
642 hash = ip_vs_rs_hashkey(af, daddr, tun_port);
644 hlist_for_each_entry_rcu(dest, &ipvs->rs_table[hash], d_list) {
645 if (dest->tun_port == tun_port &&
647 ip_vs_addr_equal(af, &dest->addr, daddr) &&
648 IP_VS_DFWD_METHOD(dest) == IP_VS_CONN_F_TUNNEL) {
657 /* Lookup destination by {addr,port} in the given service
658 * Called under RCU lock.
660 static struct ip_vs_dest *
661 ip_vs_lookup_dest(struct ip_vs_service *svc, int dest_af,
662 const union nf_inet_addr *daddr, __be16 dport)
664 struct ip_vs_dest *dest;
667 * Find the destination for the given service
669 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
670 if ((dest->af == dest_af) &&
671 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
672 (dest->port == dport)) {
682 * Find destination by {daddr,dport,vaddr,protocol}
683 * Created to be used in ip_vs_process_message() in
684 * the backup synchronization daemon. It finds the
685 * destination to be bound to the received connection
687 * Called under RCU lock, no refcnt is returned.
689 struct ip_vs_dest *ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
690 const union nf_inet_addr *daddr,
692 const union nf_inet_addr *vaddr,
693 __be16 vport, __u16 protocol, __u32 fwmark,
696 struct ip_vs_dest *dest;
697 struct ip_vs_service *svc;
700 svc = ip_vs_service_find(ipvs, svc_af, fwmark, protocol, vaddr, vport);
703 if (fwmark && (flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ)
705 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port);
707 dest = ip_vs_lookup_dest(svc, dest_af, daddr, port ^ dport);
711 void ip_vs_dest_dst_rcu_free(struct rcu_head *head)
713 struct ip_vs_dest_dst *dest_dst = container_of(head,
714 struct ip_vs_dest_dst,
717 dst_release(dest_dst->dst_cache);
721 /* Release dest_dst and dst_cache for dest in user context */
722 static void __ip_vs_dst_cache_reset(struct ip_vs_dest *dest)
724 struct ip_vs_dest_dst *old;
726 old = rcu_dereference_protected(dest->dest_dst, 1);
728 RCU_INIT_POINTER(dest->dest_dst, NULL);
729 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
734 * Lookup dest by {svc,addr,port} in the destination trash.
735 * The destination trash is used to hold the destinations that are removed
736 * from the service table but are still referenced by some conn entries.
737 * The reason to add the destination trash is when the dest is temporary
738 * down (either by administrator or by monitor program), the dest can be
739 * picked back from the trash, the remaining connections to the dest can
740 * continue, and the counting information of the dest is also useful for
743 static struct ip_vs_dest *
744 ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af,
745 const union nf_inet_addr *daddr, __be16 dport)
747 struct ip_vs_dest *dest;
748 struct netns_ipvs *ipvs = svc->ipvs;
751 * Find the destination in trash
753 spin_lock_bh(&ipvs->dest_trash_lock);
754 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
755 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
758 IP_VS_DBG_ADDR(dest->af, &dest->addr),
760 refcount_read(&dest->refcnt));
761 if (dest->af == dest_af &&
762 ip_vs_addr_equal(dest_af, &dest->addr, daddr) &&
763 dest->port == dport &&
764 dest->vfwmark == svc->fwmark &&
765 dest->protocol == svc->protocol &&
767 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
768 dest->vport == svc->port))) {
770 list_del(&dest->t_list);
778 spin_unlock_bh(&ipvs->dest_trash_lock);
783 static void ip_vs_dest_free(struct ip_vs_dest *dest)
785 struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
787 __ip_vs_dst_cache_reset(dest);
788 __ip_vs_svc_put(svc, false);
789 free_percpu(dest->stats.cpustats);
790 ip_vs_dest_put_and_free(dest);
794 * Clean up all the destinations in the trash
795 * Called by the ip_vs_control_cleanup()
797 * When the ip_vs_control_clearup is activated by ipvs module exit,
798 * the service tables must have been flushed and all the connections
799 * are expired, and the refcnt of each destination in the trash must
800 * be 1, so we simply release them here.
802 static void ip_vs_trash_cleanup(struct netns_ipvs *ipvs)
804 struct ip_vs_dest *dest, *nxt;
806 del_timer_sync(&ipvs->dest_trash_timer);
807 /* No need to use dest_trash_lock */
808 list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, t_list) {
809 list_del(&dest->t_list);
810 ip_vs_dest_free(dest);
815 ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
817 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
819 spin_lock_bh(&src->lock);
821 IP_VS_SHOW_STATS_COUNTER(conns);
822 IP_VS_SHOW_STATS_COUNTER(inpkts);
823 IP_VS_SHOW_STATS_COUNTER(outpkts);
824 IP_VS_SHOW_STATS_COUNTER(inbytes);
825 IP_VS_SHOW_STATS_COUNTER(outbytes);
827 ip_vs_read_estimator(dst, src);
829 spin_unlock_bh(&src->lock);
833 ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
835 dst->conns = (u32)src->conns;
836 dst->inpkts = (u32)src->inpkts;
837 dst->outpkts = (u32)src->outpkts;
838 dst->inbytes = src->inbytes;
839 dst->outbytes = src->outbytes;
840 dst->cps = (u32)src->cps;
841 dst->inpps = (u32)src->inpps;
842 dst->outpps = (u32)src->outpps;
843 dst->inbps = (u32)src->inbps;
844 dst->outbps = (u32)src->outbps;
848 ip_vs_zero_stats(struct ip_vs_stats *stats)
850 spin_lock_bh(&stats->lock);
852 /* get current counters as zero point, rates are zeroed */
854 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
856 IP_VS_ZERO_STATS_COUNTER(conns);
857 IP_VS_ZERO_STATS_COUNTER(inpkts);
858 IP_VS_ZERO_STATS_COUNTER(outpkts);
859 IP_VS_ZERO_STATS_COUNTER(inbytes);
860 IP_VS_ZERO_STATS_COUNTER(outbytes);
862 ip_vs_zero_estimator(stats);
864 spin_unlock_bh(&stats->lock);
868 * Update a destination in the given service
871 __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
872 struct ip_vs_dest_user_kern *udest, int add)
874 struct netns_ipvs *ipvs = svc->ipvs;
875 struct ip_vs_service *old_svc;
876 struct ip_vs_scheduler *sched;
879 /* We cannot modify an address and change the address family */
880 BUG_ON(!add && udest->af != dest->af);
882 if (add && udest->af != svc->af)
883 ipvs->mixed_address_family_dests++;
885 /* keep the last_weight with latest non-0 weight */
886 if (add || udest->weight != 0)
887 atomic_set(&dest->last_weight, udest->weight);
889 /* set the weight and the flags */
890 atomic_set(&dest->weight, udest->weight);
891 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
892 conn_flags |= IP_VS_CONN_F_INACTIVE;
894 /* Need to rehash? */
895 if ((udest->conn_flags & IP_VS_CONN_F_FWD_MASK) !=
896 IP_VS_DFWD_METHOD(dest) ||
897 udest->tun_type != dest->tun_type ||
898 udest->tun_port != dest->tun_port)
899 ip_vs_rs_unhash(dest);
901 /* set the tunnel info */
902 dest->tun_type = udest->tun_type;
903 dest->tun_port = udest->tun_port;
904 dest->tun_flags = udest->tun_flags;
906 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
907 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
908 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
910 /* FTP-NAT requires conntrack for mangling */
911 if (svc->port == FTPPORT)
912 ip_vs_register_conntrack(svc);
914 atomic_set(&dest->conn_flags, conn_flags);
915 /* Put the real service in rs_table if not present. */
916 ip_vs_rs_hash(ipvs, dest);
918 /* bind the service */
919 old_svc = rcu_dereference_protected(dest->svc, 1);
921 __ip_vs_bind_svc(dest, svc);
923 if (old_svc != svc) {
924 ip_vs_zero_stats(&dest->stats);
925 __ip_vs_bind_svc(dest, svc);
926 __ip_vs_svc_put(old_svc, true);
930 /* set the dest status flags */
931 dest->flags |= IP_VS_DEST_F_AVAILABLE;
933 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
934 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
935 dest->u_threshold = udest->u_threshold;
936 dest->l_threshold = udest->l_threshold;
938 dest->af = udest->af;
940 spin_lock_bh(&dest->dst_lock);
941 __ip_vs_dst_cache_reset(dest);
942 spin_unlock_bh(&dest->dst_lock);
945 ip_vs_start_estimator(svc->ipvs, &dest->stats);
946 list_add_rcu(&dest->n_list, &svc->destinations);
948 sched = rcu_dereference_protected(svc->scheduler, 1);
949 if (sched && sched->add_dest)
950 sched->add_dest(svc, dest);
952 sched = rcu_dereference_protected(svc->scheduler, 1);
953 if (sched && sched->upd_dest)
954 sched->upd_dest(svc, dest);
960 * Create a destination for the given service
963 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
965 struct ip_vs_dest *dest;
966 unsigned int atype, i;
970 #ifdef CONFIG_IP_VS_IPV6
971 if (udest->af == AF_INET6) {
974 atype = ipv6_addr_type(&udest->addr.in6);
975 if ((!(atype & IPV6_ADDR_UNICAST) ||
976 atype & IPV6_ADDR_LINKLOCAL) &&
977 !__ip_vs_addr_is_local_v6(svc->ipvs->net, &udest->addr.in6))
980 ret = nf_defrag_ipv6_enable(svc->ipvs->net);
986 atype = inet_addr_type(svc->ipvs->net, udest->addr.ip);
987 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
991 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
995 dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
996 if (!dest->stats.cpustats)
999 for_each_possible_cpu(i) {
1000 struct ip_vs_cpu_stats *ip_vs_dest_stats;
1001 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
1002 u64_stats_init(&ip_vs_dest_stats->syncp);
1005 dest->af = udest->af;
1006 dest->protocol = svc->protocol;
1007 dest->vaddr = svc->addr;
1008 dest->vport = svc->port;
1009 dest->vfwmark = svc->fwmark;
1010 ip_vs_addr_copy(udest->af, &dest->addr, &udest->addr);
1011 dest->port = udest->port;
1013 atomic_set(&dest->activeconns, 0);
1014 atomic_set(&dest->inactconns, 0);
1015 atomic_set(&dest->persistconns, 0);
1016 refcount_set(&dest->refcnt, 1);
1018 INIT_HLIST_NODE(&dest->d_list);
1019 spin_lock_init(&dest->dst_lock);
1020 spin_lock_init(&dest->stats.lock);
1021 __ip_vs_update_dest(svc, dest, udest, 1);
1033 * Add a destination into an existing service
1036 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1038 struct ip_vs_dest *dest;
1039 union nf_inet_addr daddr;
1040 __be16 dport = udest->port;
1045 if (udest->weight < 0) {
1046 pr_err("%s(): server weight less than zero\n", __func__);
1050 if (udest->l_threshold > udest->u_threshold) {
1051 pr_err("%s(): lower threshold is higher than upper threshold\n",
1056 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1057 if (udest->tun_port == 0) {
1058 pr_err("%s(): tunnel port is zero\n", __func__);
1063 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1065 /* We use function that requires RCU lock */
1067 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1071 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
1076 * Check if the dest already exists in the trash and
1077 * is from the same service
1079 dest = ip_vs_trash_get_dest(svc, udest->af, &daddr, dport);
1082 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1083 "dest->refcnt=%d, service %u/%s:%u\n",
1084 IP_VS_DBG_ADDR(udest->af, &daddr), ntohs(dport),
1085 refcount_read(&dest->refcnt),
1087 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
1088 ntohs(dest->vport));
1090 __ip_vs_update_dest(svc, dest, udest, 1);
1094 * Allocate and initialize the dest structure
1096 ret = ip_vs_new_dest(svc, udest);
1105 * Edit a destination in the given service
1108 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1110 struct ip_vs_dest *dest;
1111 union nf_inet_addr daddr;
1112 __be16 dport = udest->port;
1116 if (udest->weight < 0) {
1117 pr_err("%s(): server weight less than zero\n", __func__);
1121 if (udest->l_threshold > udest->u_threshold) {
1122 pr_err("%s(): lower threshold is higher than upper threshold\n",
1127 if (udest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1128 if (udest->tun_port == 0) {
1129 pr_err("%s(): tunnel port is zero\n", __func__);
1134 ip_vs_addr_copy(udest->af, &daddr, &udest->addr);
1136 /* We use function that requires RCU lock */
1138 dest = ip_vs_lookup_dest(svc, udest->af, &daddr, dport);
1142 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1146 __ip_vs_update_dest(svc, dest, udest, 0);
1153 * Delete a destination (must be already unlinked from the service)
1155 static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,
1158 ip_vs_stop_estimator(ipvs, &dest->stats);
1161 * Remove it from the d-linked list with the real services.
1163 ip_vs_rs_unhash(dest);
1165 spin_lock_bh(&ipvs->dest_trash_lock);
1166 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1167 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
1168 refcount_read(&dest->refcnt));
1169 if (list_empty(&ipvs->dest_trash) && !cleanup)
1170 mod_timer(&ipvs->dest_trash_timer,
1171 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1172 /* dest lives in trash with reference */
1173 list_add(&dest->t_list, &ipvs->dest_trash);
1174 dest->idle_start = 0;
1175 spin_unlock_bh(&ipvs->dest_trash_lock);
1177 /* Queue up delayed work to expire all no destination connections.
1178 * No-op when CONFIG_SYSCTL is disabled.
1181 ip_vs_enqueue_expire_nodest_conns(ipvs);
1186 * Unlink a destination from the given service
1188 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1189 struct ip_vs_dest *dest,
1192 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1195 * Remove it from the d-linked destination list.
1197 list_del_rcu(&dest->n_list);
1200 if (dest->af != svc->af)
1201 svc->ipvs->mixed_address_family_dests--;
1204 struct ip_vs_scheduler *sched;
1206 sched = rcu_dereference_protected(svc->scheduler, 1);
1207 if (sched && sched->del_dest)
1208 sched->del_dest(svc, dest);
1214 * Delete a destination server in the given service
1217 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1219 struct ip_vs_dest *dest;
1220 __be16 dport = udest->port;
1224 /* We use function that requires RCU lock */
1226 dest = ip_vs_lookup_dest(svc, udest->af, &udest->addr, dport);
1230 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1235 * Unlink dest from the service
1237 __ip_vs_unlink_dest(svc, dest, 1);
1240 * Delete the destination
1242 __ip_vs_del_dest(svc->ipvs, dest, false);
1249 static void ip_vs_dest_trash_expire(struct timer_list *t)
1251 struct netns_ipvs *ipvs = from_timer(ipvs, t, dest_trash_timer);
1252 struct ip_vs_dest *dest, *next;
1253 unsigned long now = jiffies;
1255 spin_lock(&ipvs->dest_trash_lock);
1256 list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1257 if (refcount_read(&dest->refcnt) > 1)
1259 if (dest->idle_start) {
1260 if (time_before(now, dest->idle_start +
1261 IP_VS_DEST_TRASH_PERIOD))
1264 dest->idle_start = max(1UL, now);
1267 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1269 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1271 list_del(&dest->t_list);
1272 ip_vs_dest_free(dest);
1274 if (!list_empty(&ipvs->dest_trash))
1275 mod_timer(&ipvs->dest_trash_timer,
1276 jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
1277 spin_unlock(&ipvs->dest_trash_lock);
1281 * Add a service into the service hash table
1284 ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
1285 struct ip_vs_service **svc_p)
1288 struct ip_vs_scheduler *sched = NULL;
1289 struct ip_vs_pe *pe = NULL;
1290 struct ip_vs_service *svc = NULL;
1293 /* increase the module use count */
1294 if (!ip_vs_use_count_inc())
1295 return -ENOPROTOOPT;
1297 /* Lookup the scheduler by 'u->sched_name' */
1298 if (strcmp(u->sched_name, "none")) {
1299 sched = ip_vs_scheduler_get(u->sched_name);
1301 pr_info("Scheduler module ip_vs_%s not found\n",
1308 if (u->pe_name && *u->pe_name) {
1309 pe = ip_vs_pe_getbyname(u->pe_name);
1311 pr_info("persistence engine module ip_vs_pe_%s "
1312 "not found\n", u->pe_name);
1318 #ifdef CONFIG_IP_VS_IPV6
1319 if (u->af == AF_INET6) {
1320 __u32 plen = (__force __u32) u->netmask;
1322 if (plen < 1 || plen > 128) {
1327 ret = nf_defrag_ipv6_enable(ipvs->net);
1333 if ((u->af == AF_INET && !ipvs->num_services) ||
1334 (u->af == AF_INET6 && !ipvs->num_services6)) {
1335 ret = ip_vs_register_hooks(ipvs, u->af);
1341 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
1343 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1347 svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
1348 if (!svc->stats.cpustats) {
1353 for_each_possible_cpu(i) {
1354 struct ip_vs_cpu_stats *ip_vs_stats;
1355 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1356 u64_stats_init(&ip_vs_stats->syncp);
1360 /* I'm the first user of the service */
1361 atomic_set(&svc->refcnt, 0);
1364 svc->protocol = u->protocol;
1365 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1366 svc->port = u->port;
1367 svc->fwmark = u->fwmark;
1368 svc->flags = u->flags & ~IP_VS_SVC_F_HASHED;
1369 svc->timeout = u->timeout * HZ;
1370 svc->netmask = u->netmask;
1373 INIT_LIST_HEAD(&svc->destinations);
1374 spin_lock_init(&svc->sched_lock);
1375 spin_lock_init(&svc->stats.lock);
1377 /* Bind the scheduler */
1379 ret = ip_vs_bind_scheduler(svc, sched);
1385 /* Bind the ct retriever */
1386 RCU_INIT_POINTER(svc->pe, pe);
1389 /* Update the virtual service counters */
1390 if (svc->port == FTPPORT)
1391 atomic_inc(&ipvs->ftpsvc_counter);
1392 else if (svc->port == 0)
1393 atomic_inc(&ipvs->nullsvc_counter);
1394 if (svc->pe && svc->pe->conn_out)
1395 atomic_inc(&ipvs->conn_out_counter);
1397 ip_vs_start_estimator(ipvs, &svc->stats);
1399 /* Count only IPv4 services for old get/setsockopt interface */
1400 if (svc->af == AF_INET)
1401 ipvs->num_services++;
1402 else if (svc->af == AF_INET6)
1403 ipvs->num_services6++;
1405 /* Hash the service into the service table */
1406 ip_vs_svc_hash(svc);
1409 /* Now there is a service - full throttle */
1416 ip_vs_unregister_hooks(ipvs, u->af);
1418 ip_vs_unbind_scheduler(svc, sched);
1419 ip_vs_service_free(svc);
1421 ip_vs_scheduler_put(sched);
1424 /* decrease the module use count */
1425 ip_vs_use_count_dec();
1432 * Edit a service and bind it with a new scheduler
1435 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1437 struct ip_vs_scheduler *sched = NULL, *old_sched;
1438 struct ip_vs_pe *pe = NULL, *old_pe = NULL;
1440 bool new_pe_conn_out, old_pe_conn_out;
1443 * Lookup the scheduler, by 'u->sched_name'
1445 if (strcmp(u->sched_name, "none")) {
1446 sched = ip_vs_scheduler_get(u->sched_name);
1448 pr_info("Scheduler module ip_vs_%s not found\n",
1455 if (u->pe_name && *u->pe_name) {
1456 pe = ip_vs_pe_getbyname(u->pe_name);
1458 pr_info("persistence engine module ip_vs_pe_%s "
1459 "not found\n", u->pe_name);
1466 #ifdef CONFIG_IP_VS_IPV6
1467 if (u->af == AF_INET6) {
1468 __u32 plen = (__force __u32) u->netmask;
1470 if (plen < 1 || plen > 128) {
1477 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1478 if (sched != old_sched) {
1480 ip_vs_unbind_scheduler(svc, old_sched);
1481 RCU_INIT_POINTER(svc->scheduler, NULL);
1482 /* Wait all svc->sched_data users */
1485 /* Bind the new scheduler */
1487 ret = ip_vs_bind_scheduler(svc, sched);
1489 ip_vs_scheduler_put(sched);
1496 * Set the flags and timeout value
1498 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1499 svc->timeout = u->timeout * HZ;
1500 svc->netmask = u->netmask;
1502 old_pe = rcu_dereference_protected(svc->pe, 1);
1504 rcu_assign_pointer(svc->pe, pe);
1505 /* check for optional methods in new pe */
1506 new_pe_conn_out = (pe && pe->conn_out) ? true : false;
1507 old_pe_conn_out = (old_pe && old_pe->conn_out) ? true : false;
1508 if (new_pe_conn_out && !old_pe_conn_out)
1509 atomic_inc(&svc->ipvs->conn_out_counter);
1510 if (old_pe_conn_out && !new_pe_conn_out)
1511 atomic_dec(&svc->ipvs->conn_out_counter);
1515 ip_vs_scheduler_put(old_sched);
1516 ip_vs_pe_put(old_pe);
1521 * Delete a service from the service list
1522 * - The service must be unlinked, unlocked and not referenced!
1523 * - We are called under _bh lock
1525 static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
1527 struct ip_vs_dest *dest, *nxt;
1528 struct ip_vs_scheduler *old_sched;
1529 struct ip_vs_pe *old_pe;
1530 struct netns_ipvs *ipvs = svc->ipvs;
1532 if (svc->af == AF_INET) {
1533 ipvs->num_services--;
1534 if (!ipvs->num_services)
1535 ip_vs_unregister_hooks(ipvs, svc->af);
1536 } else if (svc->af == AF_INET6) {
1537 ipvs->num_services6--;
1538 if (!ipvs->num_services6)
1539 ip_vs_unregister_hooks(ipvs, svc->af);
1542 ip_vs_stop_estimator(svc->ipvs, &svc->stats);
1544 /* Unbind scheduler */
1545 old_sched = rcu_dereference_protected(svc->scheduler, 1);
1546 ip_vs_unbind_scheduler(svc, old_sched);
1547 ip_vs_scheduler_put(old_sched);
1549 /* Unbind persistence engine, keep svc->pe */
1550 old_pe = rcu_dereference_protected(svc->pe, 1);
1551 if (old_pe && old_pe->conn_out)
1552 atomic_dec(&ipvs->conn_out_counter);
1553 ip_vs_pe_put(old_pe);
1556 * Unlink the whole destination list
1558 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1559 __ip_vs_unlink_dest(svc, dest, 0);
1560 __ip_vs_del_dest(svc->ipvs, dest, cleanup);
1564 * Update the virtual service counters
1566 if (svc->port == FTPPORT)
1567 atomic_dec(&ipvs->ftpsvc_counter);
1568 else if (svc->port == 0)
1569 atomic_dec(&ipvs->nullsvc_counter);
1572 * Free the service if nobody refers to it
1574 __ip_vs_svc_put(svc, true);
1576 /* decrease the module use count */
1577 ip_vs_use_count_dec();
1581 * Unlink a service from list and try to delete it if its refcnt reached 0
1583 static void ip_vs_unlink_service(struct ip_vs_service *svc, bool cleanup)
1585 ip_vs_unregister_conntrack(svc);
1586 /* Hold svc to avoid double release from dest_trash */
1587 atomic_inc(&svc->refcnt);
1589 * Unhash it from the service table
1591 ip_vs_svc_unhash(svc);
1593 __ip_vs_del_service(svc, cleanup);
1597 * Delete a service from the service list
1599 static int ip_vs_del_service(struct ip_vs_service *svc)
1603 ip_vs_unlink_service(svc, false);
1610 * Flush all the virtual services
1612 static int ip_vs_flush(struct netns_ipvs *ipvs, bool cleanup)
1615 struct ip_vs_service *svc;
1616 struct hlist_node *n;
1619 * Flush the service table hashed by <netns,protocol,addr,port>
1621 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1622 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_table[idx],
1624 if (svc->ipvs == ipvs)
1625 ip_vs_unlink_service(svc, cleanup);
1630 * Flush the service table hashed by fwmark
1632 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1633 hlist_for_each_entry_safe(svc, n, &ip_vs_svc_fwm_table[idx],
1635 if (svc->ipvs == ipvs)
1636 ip_vs_unlink_service(svc, cleanup);
1644 * Delete service by {netns} in the service table.
1645 * Called by __ip_vs_batch_cleanup()
1647 void ip_vs_service_nets_cleanup(struct list_head *net_list)
1649 struct netns_ipvs *ipvs;
1653 /* Check for "full" addressed entries */
1654 mutex_lock(&__ip_vs_mutex);
1655 list_for_each_entry(net, net_list, exit_list) {
1656 ipvs = net_ipvs(net);
1657 ip_vs_flush(ipvs, true);
1659 mutex_unlock(&__ip_vs_mutex);
1663 /* Put all references for device (dst_cache) */
1665 ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev)
1667 struct ip_vs_dest_dst *dest_dst;
1669 spin_lock_bh(&dest->dst_lock);
1670 dest_dst = rcu_dereference_protected(dest->dest_dst, 1);
1671 if (dest_dst && dest_dst->dst_cache->dev == dev) {
1672 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1674 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1676 refcount_read(&dest->refcnt));
1677 __ip_vs_dst_cache_reset(dest);
1679 spin_unlock_bh(&dest->dst_lock);
1682 /* Netdev event receiver
1683 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1685 static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
1688 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1689 struct net *net = dev_net(dev);
1690 struct netns_ipvs *ipvs = net_ipvs(net);
1691 struct ip_vs_service *svc;
1692 struct ip_vs_dest *dest;
1695 if (event != NETDEV_DOWN || !ipvs)
1697 IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
1699 mutex_lock(&__ip_vs_mutex);
1700 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1701 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1702 if (svc->ipvs == ipvs) {
1703 list_for_each_entry(dest, &svc->destinations,
1705 ip_vs_forget_dev(dest, dev);
1710 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1711 if (svc->ipvs == ipvs) {
1712 list_for_each_entry(dest, &svc->destinations,
1714 ip_vs_forget_dev(dest, dev);
1721 spin_lock_bh(&ipvs->dest_trash_lock);
1722 list_for_each_entry(dest, &ipvs->dest_trash, t_list) {
1723 ip_vs_forget_dev(dest, dev);
1725 spin_unlock_bh(&ipvs->dest_trash_lock);
1726 mutex_unlock(&__ip_vs_mutex);
1732 * Zero counters in a service or all services
1734 static int ip_vs_zero_service(struct ip_vs_service *svc)
1736 struct ip_vs_dest *dest;
1738 list_for_each_entry(dest, &svc->destinations, n_list) {
1739 ip_vs_zero_stats(&dest->stats);
1741 ip_vs_zero_stats(&svc->stats);
1745 static int ip_vs_zero_all(struct netns_ipvs *ipvs)
1748 struct ip_vs_service *svc;
1750 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1751 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1752 if (svc->ipvs == ipvs)
1753 ip_vs_zero_service(svc);
1757 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1758 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1759 if (svc->ipvs == ipvs)
1760 ip_vs_zero_service(svc);
1764 ip_vs_zero_stats(&ipvs->tot_stats);
1768 #ifdef CONFIG_SYSCTL
1771 proc_do_defense_mode(struct ctl_table *table, int write,
1772 void *buffer, size_t *lenp, loff_t *ppos)
1774 struct netns_ipvs *ipvs = table->extra2;
1775 int *valp = table->data;
1779 struct ctl_table tmp = {
1781 .maxlen = sizeof(int),
1782 .mode = table->mode,
1785 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1786 if (write && (*valp != val)) {
1787 if (val < 0 || val > 3) {
1791 update_defense_level(ipvs);
1798 proc_do_sync_threshold(struct ctl_table *table, int write,
1799 void *buffer, size_t *lenp, loff_t *ppos)
1801 struct netns_ipvs *ipvs = table->extra2;
1802 int *valp = table->data;
1805 struct ctl_table tmp = {
1807 .maxlen = table->maxlen,
1808 .mode = table->mode,
1811 mutex_lock(&ipvs->sync_mutex);
1812 memcpy(val, valp, sizeof(val));
1813 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1815 if (val[0] < 0 || val[1] < 0 ||
1816 (val[0] >= val[1] && val[1]))
1819 memcpy(valp, val, sizeof(val));
1821 mutex_unlock(&ipvs->sync_mutex);
1826 proc_do_sync_ports(struct ctl_table *table, int write,
1827 void *buffer, size_t *lenp, loff_t *ppos)
1829 int *valp = table->data;
1833 struct ctl_table tmp = {
1835 .maxlen = sizeof(int),
1836 .mode = table->mode,
1839 rc = proc_dointvec(&tmp, write, buffer, lenp, ppos);
1840 if (write && (*valp != val)) {
1841 if (val < 1 || !is_power_of_2(val))
1850 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1851 * Do not change order or insert new entries without
1852 * align with netns init in ip_vs_control_net_init()
1855 static struct ctl_table vs_vars[] = {
1857 .procname = "amemthresh",
1858 .maxlen = sizeof(int),
1860 .proc_handler = proc_dointvec,
1863 .procname = "am_droprate",
1864 .maxlen = sizeof(int),
1866 .proc_handler = proc_dointvec,
1869 .procname = "drop_entry",
1870 .maxlen = sizeof(int),
1872 .proc_handler = proc_do_defense_mode,
1875 .procname = "drop_packet",
1876 .maxlen = sizeof(int),
1878 .proc_handler = proc_do_defense_mode,
1880 #ifdef CONFIG_IP_VS_NFCT
1882 .procname = "conntrack",
1883 .maxlen = sizeof(int),
1885 .proc_handler = &proc_dointvec,
1889 .procname = "secure_tcp",
1890 .maxlen = sizeof(int),
1892 .proc_handler = proc_do_defense_mode,
1895 .procname = "snat_reroute",
1896 .maxlen = sizeof(int),
1898 .proc_handler = &proc_dointvec,
1901 .procname = "sync_version",
1902 .maxlen = sizeof(int),
1904 .proc_handler = proc_dointvec_minmax,
1905 .extra1 = SYSCTL_ZERO,
1906 .extra2 = SYSCTL_ONE,
1909 .procname = "sync_ports",
1910 .maxlen = sizeof(int),
1912 .proc_handler = proc_do_sync_ports,
1915 .procname = "sync_persist_mode",
1916 .maxlen = sizeof(int),
1918 .proc_handler = proc_dointvec,
1921 .procname = "sync_qlen_max",
1922 .maxlen = sizeof(unsigned long),
1924 .proc_handler = proc_doulongvec_minmax,
1927 .procname = "sync_sock_size",
1928 .maxlen = sizeof(int),
1930 .proc_handler = proc_dointvec,
1933 .procname = "cache_bypass",
1934 .maxlen = sizeof(int),
1936 .proc_handler = proc_dointvec,
1939 .procname = "expire_nodest_conn",
1940 .maxlen = sizeof(int),
1942 .proc_handler = proc_dointvec,
1945 .procname = "sloppy_tcp",
1946 .maxlen = sizeof(int),
1948 .proc_handler = proc_dointvec,
1951 .procname = "sloppy_sctp",
1952 .maxlen = sizeof(int),
1954 .proc_handler = proc_dointvec,
1957 .procname = "expire_quiescent_template",
1958 .maxlen = sizeof(int),
1960 .proc_handler = proc_dointvec,
1963 .procname = "sync_threshold",
1965 sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
1967 .proc_handler = proc_do_sync_threshold,
1970 .procname = "sync_refresh_period",
1971 .maxlen = sizeof(int),
1973 .proc_handler = proc_dointvec_jiffies,
1976 .procname = "sync_retries",
1977 .maxlen = sizeof(int),
1979 .proc_handler = proc_dointvec_minmax,
1980 .extra1 = SYSCTL_ZERO,
1981 .extra2 = SYSCTL_THREE,
1984 .procname = "nat_icmp_send",
1985 .maxlen = sizeof(int),
1987 .proc_handler = proc_dointvec,
1990 .procname = "pmtu_disc",
1991 .maxlen = sizeof(int),
1993 .proc_handler = proc_dointvec,
1996 .procname = "backup_only",
1997 .maxlen = sizeof(int),
1999 .proc_handler = proc_dointvec,
2002 .procname = "conn_reuse_mode",
2003 .maxlen = sizeof(int),
2005 .proc_handler = proc_dointvec,
2008 .procname = "schedule_icmp",
2009 .maxlen = sizeof(int),
2011 .proc_handler = proc_dointvec,
2014 .procname = "ignore_tunneled",
2015 .maxlen = sizeof(int),
2017 .proc_handler = proc_dointvec,
2020 .procname = "run_estimation",
2021 .maxlen = sizeof(int),
2023 .proc_handler = proc_dointvec,
2025 #ifdef CONFIG_IP_VS_DEBUG
2027 .procname = "debug_level",
2028 .data = &sysctl_ip_vs_debug_level,
2029 .maxlen = sizeof(int),
2031 .proc_handler = proc_dointvec,
2039 #ifdef CONFIG_PROC_FS
2042 struct seq_net_private p; /* Do not move this, netns depends upon it*/
2043 struct hlist_head *table;
2048 * Write the contents of the VS rule table to a PROCfs file.
2049 * (It is kept just for backward compatibility)
2051 static inline const char *ip_vs_fwd_name(unsigned int flags)
2053 switch (flags & IP_VS_CONN_F_FWD_MASK) {
2054 case IP_VS_CONN_F_LOCALNODE:
2056 case IP_VS_CONN_F_TUNNEL:
2058 case IP_VS_CONN_F_DROUTE:
2066 /* Get the Nth entry in the two lists */
2067 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
2069 struct net *net = seq_file_net(seq);
2070 struct netns_ipvs *ipvs = net_ipvs(net);
2071 struct ip_vs_iter *iter = seq->private;
2073 struct ip_vs_service *svc;
2075 /* look in hash by protocol */
2076 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2077 hlist_for_each_entry_rcu(svc, &ip_vs_svc_table[idx], s_list) {
2078 if ((svc->ipvs == ipvs) && pos-- == 0) {
2079 iter->table = ip_vs_svc_table;
2086 /* keep looking in fwmark */
2087 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2088 hlist_for_each_entry_rcu(svc, &ip_vs_svc_fwm_table[idx],
2090 if ((svc->ipvs == ipvs) && pos-- == 0) {
2091 iter->table = ip_vs_svc_fwm_table;
2101 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
2105 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
2109 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2111 struct hlist_node *e;
2112 struct ip_vs_iter *iter;
2113 struct ip_vs_service *svc;
2116 if (v == SEQ_START_TOKEN)
2117 return ip_vs_info_array(seq,0);
2120 iter = seq->private;
2122 if (iter->table == ip_vs_svc_table) {
2123 /* next service in table hashed by protocol */
2124 e = rcu_dereference(hlist_next_rcu(&svc->s_list));
2126 return hlist_entry(e, struct ip_vs_service, s_list);
2128 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2129 hlist_for_each_entry_rcu(svc,
2130 &ip_vs_svc_table[iter->bucket],
2136 iter->table = ip_vs_svc_fwm_table;
2141 /* next service in hashed by fwmark */
2142 e = rcu_dereference(hlist_next_rcu(&svc->f_list));
2144 return hlist_entry(e, struct ip_vs_service, f_list);
2147 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
2148 hlist_for_each_entry_rcu(svc,
2149 &ip_vs_svc_fwm_table[iter->bucket],
2157 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
2164 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2166 if (v == SEQ_START_TOKEN) {
2168 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2169 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2171 "Prot LocalAddress:Port Scheduler Flags\n");
2173 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2175 struct net *net = seq_file_net(seq);
2176 struct netns_ipvs *ipvs = net_ipvs(net);
2177 const struct ip_vs_service *svc = v;
2178 const struct ip_vs_iter *iter = seq->private;
2179 const struct ip_vs_dest *dest;
2180 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
2181 char *sched_name = sched ? sched->name : "none";
2183 if (svc->ipvs != ipvs)
2185 if (iter->table == ip_vs_svc_table) {
2186 #ifdef CONFIG_IP_VS_IPV6
2187 if (svc->af == AF_INET6)
2188 seq_printf(seq, "%s [%pI6]:%04X %s ",
2189 ip_vs_proto_name(svc->protocol),
2195 seq_printf(seq, "%s %08X:%04X %s %s ",
2196 ip_vs_proto_name(svc->protocol),
2197 ntohl(svc->addr.ip),
2200 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2202 seq_printf(seq, "FWM %08X %s %s",
2203 svc->fwmark, sched_name,
2204 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2207 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2208 seq_printf(seq, "persistent %d %08X\n",
2210 ntohl(svc->netmask));
2212 seq_putc(seq, '\n');
2214 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
2215 #ifdef CONFIG_IP_VS_IPV6
2216 if (dest->af == AF_INET6)
2219 " %-7s %-6d %-10d %-10d\n",
2222 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2223 atomic_read(&dest->weight),
2224 atomic_read(&dest->activeconns),
2225 atomic_read(&dest->inactconns));
2230 "%-7s %-6d %-10d %-10d\n",
2231 ntohl(dest->addr.ip),
2233 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2234 atomic_read(&dest->weight),
2235 atomic_read(&dest->activeconns),
2236 atomic_read(&dest->inactconns));
2243 static const struct seq_operations ip_vs_info_seq_ops = {
2244 .start = ip_vs_info_seq_start,
2245 .next = ip_vs_info_seq_next,
2246 .stop = ip_vs_info_seq_stop,
2247 .show = ip_vs_info_seq_show,
2250 static int ip_vs_stats_show(struct seq_file *seq, void *v)
2252 struct net *net = seq_file_single_net(seq);
2253 struct ip_vs_kstats show;
2255 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2257 " Total Incoming Outgoing Incoming Outgoing\n");
2259 " Conns Packets Packets Bytes Bytes\n");
2261 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2262 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2263 (unsigned long long)show.conns,
2264 (unsigned long long)show.inpkts,
2265 (unsigned long long)show.outpkts,
2266 (unsigned long long)show.inbytes,
2267 (unsigned long long)show.outbytes);
2269 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2271 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2272 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2273 (unsigned long long)show.cps,
2274 (unsigned long long)show.inpps,
2275 (unsigned long long)show.outpps,
2276 (unsigned long long)show.inbps,
2277 (unsigned long long)show.outbps);
2282 static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2284 struct net *net = seq_file_single_net(seq);
2285 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2286 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2287 struct ip_vs_kstats kstats;
2290 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2292 " Total Incoming Outgoing Incoming Outgoing\n");
2294 "CPU Conns Packets Packets Bytes Bytes\n");
2296 for_each_possible_cpu(i) {
2297 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2299 u64 conns, inpkts, outpkts, inbytes, outbytes;
2302 start = u64_stats_fetch_begin_irq(&u->syncp);
2303 conns = u64_stats_read(&u->cnt.conns);
2304 inpkts = u64_stats_read(&u->cnt.inpkts);
2305 outpkts = u64_stats_read(&u->cnt.outpkts);
2306 inbytes = u64_stats_read(&u->cnt.inbytes);
2307 outbytes = u64_stats_read(&u->cnt.outbytes);
2308 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2310 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2311 i, (u64)conns, (u64)inpkts,
2312 (u64)outpkts, (u64)inbytes,
2316 ip_vs_copy_stats(&kstats, tot_stats);
2318 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2319 (unsigned long long)kstats.conns,
2320 (unsigned long long)kstats.inpkts,
2321 (unsigned long long)kstats.outpkts,
2322 (unsigned long long)kstats.inbytes,
2323 (unsigned long long)kstats.outbytes);
2325 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2327 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2328 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2340 * Set timeout values for tcp tcpfin udp in the timeout_table.
2342 static int ip_vs_set_timeout(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2344 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2345 struct ip_vs_proto_data *pd;
2348 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2353 #ifdef CONFIG_IP_VS_PROTO_TCP
2354 if (u->tcp_timeout < 0 || u->tcp_timeout > (INT_MAX / HZ) ||
2355 u->tcp_fin_timeout < 0 || u->tcp_fin_timeout > (INT_MAX / HZ)) {
2360 #ifdef CONFIG_IP_VS_PROTO_UDP
2361 if (u->udp_timeout < 0 || u->udp_timeout > (INT_MAX / HZ))
2365 #ifdef CONFIG_IP_VS_PROTO_TCP
2366 if (u->tcp_timeout) {
2367 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2368 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
2369 = u->tcp_timeout * HZ;
2372 if (u->tcp_fin_timeout) {
2373 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2374 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
2375 = u->tcp_fin_timeout * HZ;
2379 #ifdef CONFIG_IP_VS_PROTO_UDP
2380 if (u->udp_timeout) {
2381 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2382 pd->timeout_table[IP_VS_UDP_S_NORMAL]
2383 = u->udp_timeout * HZ;
2389 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2391 struct ip_vs_svcdest_user {
2392 struct ip_vs_service_user s;
2393 struct ip_vs_dest_user d;
2396 static const unsigned char set_arglen[CMDID(IP_VS_SO_SET_MAX) + 1] = {
2397 [CMDID(IP_VS_SO_SET_ADD)] = sizeof(struct ip_vs_service_user),
2398 [CMDID(IP_VS_SO_SET_EDIT)] = sizeof(struct ip_vs_service_user),
2399 [CMDID(IP_VS_SO_SET_DEL)] = sizeof(struct ip_vs_service_user),
2400 [CMDID(IP_VS_SO_SET_ADDDEST)] = sizeof(struct ip_vs_svcdest_user),
2401 [CMDID(IP_VS_SO_SET_DELDEST)] = sizeof(struct ip_vs_svcdest_user),
2402 [CMDID(IP_VS_SO_SET_EDITDEST)] = sizeof(struct ip_vs_svcdest_user),
2403 [CMDID(IP_VS_SO_SET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2404 [CMDID(IP_VS_SO_SET_STARTDAEMON)] = sizeof(struct ip_vs_daemon_user),
2405 [CMDID(IP_VS_SO_SET_STOPDAEMON)] = sizeof(struct ip_vs_daemon_user),
2406 [CMDID(IP_VS_SO_SET_ZERO)] = sizeof(struct ip_vs_service_user),
2409 union ip_vs_set_arglen {
2410 struct ip_vs_service_user field_IP_VS_SO_SET_ADD;
2411 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT;
2412 struct ip_vs_service_user field_IP_VS_SO_SET_DEL;
2413 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST;
2414 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST;
2415 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST;
2416 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT;
2417 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON;
2418 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON;
2419 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO;
2422 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2424 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2425 struct ip_vs_service_user *usvc_compat)
2427 memset(usvc, 0, sizeof(*usvc));
2430 usvc->protocol = usvc_compat->protocol;
2431 usvc->addr.ip = usvc_compat->addr;
2432 usvc->port = usvc_compat->port;
2433 usvc->fwmark = usvc_compat->fwmark;
2435 /* Deep copy of sched_name is not needed here */
2436 usvc->sched_name = usvc_compat->sched_name;
2438 usvc->flags = usvc_compat->flags;
2439 usvc->timeout = usvc_compat->timeout;
2440 usvc->netmask = usvc_compat->netmask;
2443 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2444 struct ip_vs_dest_user *udest_compat)
2446 memset(udest, 0, sizeof(*udest));
2448 udest->addr.ip = udest_compat->addr;
2449 udest->port = udest_compat->port;
2450 udest->conn_flags = udest_compat->conn_flags;
2451 udest->weight = udest_compat->weight;
2452 udest->u_threshold = udest_compat->u_threshold;
2453 udest->l_threshold = udest_compat->l_threshold;
2454 udest->af = AF_INET;
2455 udest->tun_type = IP_VS_CONN_F_TUNNEL_TYPE_IPIP;
2459 do_ip_vs_set_ctl(struct sock *sk, int cmd, sockptr_t ptr, unsigned int len)
2461 struct net *net = sock_net(sk);
2463 unsigned char arg[MAX_SET_ARGLEN];
2464 struct ip_vs_service_user *usvc_compat;
2465 struct ip_vs_service_user_kern usvc;
2466 struct ip_vs_service *svc;
2467 struct ip_vs_dest_user *udest_compat;
2468 struct ip_vs_dest_user_kern udest;
2469 struct netns_ipvs *ipvs = net_ipvs(net);
2471 BUILD_BUG_ON(sizeof(arg) > 255);
2472 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2475 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2477 if (len != set_arglen[CMDID(cmd)]) {
2478 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2479 len, set_arglen[CMDID(cmd)]);
2483 if (copy_from_sockptr(arg, ptr, len) != 0)
2486 /* Handle daemons since they have another lock */
2487 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2488 cmd == IP_VS_SO_SET_STOPDAEMON) {
2489 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2491 if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2492 struct ipvs_sync_daemon_cfg cfg;
2494 memset(&cfg, 0, sizeof(cfg));
2496 if (strscpy(cfg.mcast_ifn, dm->mcast_ifn,
2497 sizeof(cfg.mcast_ifn)) <= 0)
2499 cfg.syncid = dm->syncid;
2500 ret = start_sync_thread(ipvs, &cfg, dm->state);
2502 ret = stop_sync_thread(ipvs, dm->state);
2507 mutex_lock(&__ip_vs_mutex);
2508 if (cmd == IP_VS_SO_SET_FLUSH) {
2509 /* Flush the virtual service */
2510 ret = ip_vs_flush(ipvs, false);
2512 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2513 /* Set timeout values for (tcp tcpfin udp) */
2514 ret = ip_vs_set_timeout(ipvs, (struct ip_vs_timeout_user *)arg);
2517 /* No more commands with len == 0 below */
2522 usvc_compat = (struct ip_vs_service_user *)arg;
2523 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2525 /* We only use the new structs internally, so copy userspace compat
2526 * structs to extended internal versions */
2527 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2528 ip_vs_copy_udest_compat(&udest, udest_compat);
2530 if (cmd == IP_VS_SO_SET_ZERO) {
2531 /* if no service address is set, zero counters in all */
2532 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2533 ret = ip_vs_zero_all(ipvs);
2538 if ((cmd == IP_VS_SO_SET_ADD || cmd == IP_VS_SO_SET_EDIT) &&
2539 strnlen(usvc.sched_name, IP_VS_SCHEDNAME_MAXLEN) ==
2540 IP_VS_SCHEDNAME_MAXLEN) {
2545 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2546 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2547 usvc.protocol != IPPROTO_SCTP) {
2548 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2549 usvc.protocol, &usvc.addr.ip,
2555 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2557 if (usvc.fwmark == 0)
2558 svc = __ip_vs_service_find(ipvs, usvc.af, usvc.protocol,
2559 &usvc.addr, usvc.port);
2561 svc = __ip_vs_svc_fwm_find(ipvs, usvc.af, usvc.fwmark);
2564 if (cmd != IP_VS_SO_SET_ADD
2565 && (svc == NULL || svc->protocol != usvc.protocol)) {
2571 case IP_VS_SO_SET_ADD:
2575 ret = ip_vs_add_service(ipvs, &usvc, &svc);
2577 case IP_VS_SO_SET_EDIT:
2578 ret = ip_vs_edit_service(svc, &usvc);
2580 case IP_VS_SO_SET_DEL:
2581 ret = ip_vs_del_service(svc);
2585 case IP_VS_SO_SET_ZERO:
2586 ret = ip_vs_zero_service(svc);
2588 case IP_VS_SO_SET_ADDDEST:
2589 ret = ip_vs_add_dest(svc, &udest);
2591 case IP_VS_SO_SET_EDITDEST:
2592 ret = ip_vs_edit_dest(svc, &udest);
2594 case IP_VS_SO_SET_DELDEST:
2595 ret = ip_vs_del_dest(svc, &udest);
2599 mutex_unlock(&__ip_vs_mutex);
2605 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2607 struct ip_vs_scheduler *sched;
2608 struct ip_vs_kstats kstats;
2611 sched = rcu_dereference_protected(src->scheduler, 1);
2612 sched_name = sched ? sched->name : "none";
2613 dst->protocol = src->protocol;
2614 dst->addr = src->addr.ip;
2615 dst->port = src->port;
2616 dst->fwmark = src->fwmark;
2617 strscpy(dst->sched_name, sched_name, sizeof(dst->sched_name));
2618 dst->flags = src->flags;
2619 dst->timeout = src->timeout / HZ;
2620 dst->netmask = src->netmask;
2621 dst->num_dests = src->num_dests;
2622 ip_vs_copy_stats(&kstats, &src->stats);
2623 ip_vs_export_stats_user(&dst->stats, &kstats);
2627 __ip_vs_get_service_entries(struct netns_ipvs *ipvs,
2628 const struct ip_vs_get_services *get,
2629 struct ip_vs_get_services __user *uptr)
2632 struct ip_vs_service *svc;
2633 struct ip_vs_service_entry entry;
2636 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2637 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2638 /* Only expose IPv4 entries to old interface */
2639 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2642 if (count >= get->num_services)
2644 memset(&entry, 0, sizeof(entry));
2645 ip_vs_copy_service(&entry, svc);
2646 if (copy_to_user(&uptr->entrytable[count],
2647 &entry, sizeof(entry))) {
2655 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2656 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2657 /* Only expose IPv4 entries to old interface */
2658 if (svc->af != AF_INET || (svc->ipvs != ipvs))
2661 if (count >= get->num_services)
2663 memset(&entry, 0, sizeof(entry));
2664 ip_vs_copy_service(&entry, svc);
2665 if (copy_to_user(&uptr->entrytable[count],
2666 &entry, sizeof(entry))) {
2678 __ip_vs_get_dest_entries(struct netns_ipvs *ipvs, const struct ip_vs_get_dests *get,
2679 struct ip_vs_get_dests __user *uptr)
2681 struct ip_vs_service *svc;
2682 union nf_inet_addr addr = { .ip = get->addr };
2687 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, get->fwmark);
2689 svc = __ip_vs_service_find(ipvs, AF_INET, get->protocol, &addr,
2695 struct ip_vs_dest *dest;
2696 struct ip_vs_dest_entry entry;
2697 struct ip_vs_kstats kstats;
2699 memset(&entry, 0, sizeof(entry));
2700 list_for_each_entry(dest, &svc->destinations, n_list) {
2701 if (count >= get->num_dests)
2704 /* Cannot expose heterogeneous members via sockopt
2707 if (dest->af != svc->af)
2710 entry.addr = dest->addr.ip;
2711 entry.port = dest->port;
2712 entry.conn_flags = atomic_read(&dest->conn_flags);
2713 entry.weight = atomic_read(&dest->weight);
2714 entry.u_threshold = dest->u_threshold;
2715 entry.l_threshold = dest->l_threshold;
2716 entry.activeconns = atomic_read(&dest->activeconns);
2717 entry.inactconns = atomic_read(&dest->inactconns);
2718 entry.persistconns = atomic_read(&dest->persistconns);
2719 ip_vs_copy_stats(&kstats, &dest->stats);
2720 ip_vs_export_stats_user(&entry.stats, &kstats);
2721 if (copy_to_user(&uptr->entrytable[count],
2722 &entry, sizeof(entry))) {
2734 __ip_vs_get_timeouts(struct netns_ipvs *ipvs, struct ip_vs_timeout_user *u)
2736 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2737 struct ip_vs_proto_data *pd;
2740 memset(u, 0, sizeof (*u));
2742 #ifdef CONFIG_IP_VS_PROTO_TCP
2743 pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP);
2744 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2745 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2747 #ifdef CONFIG_IP_VS_PROTO_UDP
2748 pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
2750 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2754 static const unsigned char get_arglen[CMDID(IP_VS_SO_GET_MAX) + 1] = {
2755 [CMDID(IP_VS_SO_GET_VERSION)] = 64,
2756 [CMDID(IP_VS_SO_GET_INFO)] = sizeof(struct ip_vs_getinfo),
2757 [CMDID(IP_VS_SO_GET_SERVICES)] = sizeof(struct ip_vs_get_services),
2758 [CMDID(IP_VS_SO_GET_SERVICE)] = sizeof(struct ip_vs_service_entry),
2759 [CMDID(IP_VS_SO_GET_DESTS)] = sizeof(struct ip_vs_get_dests),
2760 [CMDID(IP_VS_SO_GET_TIMEOUT)] = sizeof(struct ip_vs_timeout_user),
2761 [CMDID(IP_VS_SO_GET_DAEMON)] = 2 * sizeof(struct ip_vs_daemon_user),
2764 union ip_vs_get_arglen {
2765 char field_IP_VS_SO_GET_VERSION[64];
2766 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO;
2767 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES;
2768 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE;
2769 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS;
2770 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT;
2771 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON[2];
2774 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2777 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2779 unsigned char arg[MAX_GET_ARGLEN];
2781 unsigned int copylen;
2782 struct net *net = sock_net(sk);
2783 struct netns_ipvs *ipvs = net_ipvs(net);
2786 BUILD_BUG_ON(sizeof(arg) > 255);
2787 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2790 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2793 copylen = get_arglen[CMDID(cmd)];
2794 if (*len < (int) copylen) {
2795 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len, copylen);
2799 if (copy_from_user(arg, user, copylen) != 0)
2802 * Handle daemons first since it has its own locking
2804 if (cmd == IP_VS_SO_GET_DAEMON) {
2805 struct ip_vs_daemon_user d[2];
2807 memset(&d, 0, sizeof(d));
2808 mutex_lock(&ipvs->sync_mutex);
2809 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2810 d[0].state = IP_VS_STATE_MASTER;
2811 strscpy(d[0].mcast_ifn, ipvs->mcfg.mcast_ifn,
2812 sizeof(d[0].mcast_ifn));
2813 d[0].syncid = ipvs->mcfg.syncid;
2815 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2816 d[1].state = IP_VS_STATE_BACKUP;
2817 strscpy(d[1].mcast_ifn, ipvs->bcfg.mcast_ifn,
2818 sizeof(d[1].mcast_ifn));
2819 d[1].syncid = ipvs->bcfg.syncid;
2821 if (copy_to_user(user, &d, sizeof(d)) != 0)
2823 mutex_unlock(&ipvs->sync_mutex);
2827 mutex_lock(&__ip_vs_mutex);
2829 case IP_VS_SO_GET_VERSION:
2833 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2834 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2835 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2839 *len = strlen(buf)+1;
2843 case IP_VS_SO_GET_INFO:
2845 struct ip_vs_getinfo info;
2846 info.version = IP_VS_VERSION_CODE;
2847 info.size = ip_vs_conn_tab_size;
2848 info.num_services = ipvs->num_services;
2849 if (copy_to_user(user, &info, sizeof(info)) != 0)
2854 case IP_VS_SO_GET_SERVICES:
2856 struct ip_vs_get_services *get;
2859 get = (struct ip_vs_get_services *)arg;
2860 size = struct_size(get, entrytable, get->num_services);
2862 pr_err("length: %u != %u\n", *len, size);
2866 ret = __ip_vs_get_service_entries(ipvs, get, user);
2870 case IP_VS_SO_GET_SERVICE:
2872 struct ip_vs_service_entry *entry;
2873 struct ip_vs_service *svc;
2874 union nf_inet_addr addr;
2876 entry = (struct ip_vs_service_entry *)arg;
2877 addr.ip = entry->addr;
2880 svc = __ip_vs_svc_fwm_find(ipvs, AF_INET, entry->fwmark);
2882 svc = __ip_vs_service_find(ipvs, AF_INET,
2883 entry->protocol, &addr,
2887 ip_vs_copy_service(entry, svc);
2888 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2895 case IP_VS_SO_GET_DESTS:
2897 struct ip_vs_get_dests *get;
2900 get = (struct ip_vs_get_dests *)arg;
2901 size = struct_size(get, entrytable, get->num_dests);
2903 pr_err("length: %u != %u\n", *len, size);
2907 ret = __ip_vs_get_dest_entries(ipvs, get, user);
2911 case IP_VS_SO_GET_TIMEOUT:
2913 struct ip_vs_timeout_user t;
2915 __ip_vs_get_timeouts(ipvs, &t);
2916 if (copy_to_user(user, &t, sizeof(t)) != 0)
2926 mutex_unlock(&__ip_vs_mutex);
2931 static struct nf_sockopt_ops ip_vs_sockopts = {
2933 .set_optmin = IP_VS_BASE_CTL,
2934 .set_optmax = IP_VS_SO_SET_MAX+1,
2935 .set = do_ip_vs_set_ctl,
2936 .get_optmin = IP_VS_BASE_CTL,
2937 .get_optmax = IP_VS_SO_GET_MAX+1,
2938 .get = do_ip_vs_get_ctl,
2939 .owner = THIS_MODULE,
2943 * Generic Netlink interface
2946 /* IPVS genetlink family */
2947 static struct genl_family ip_vs_genl_family;
2949 /* Policy used for first-level command attributes */
2950 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2951 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2952 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2953 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2954 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2955 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2956 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2959 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2960 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2961 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2962 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2963 .len = IP_VS_IFNAME_MAXLEN - 1 },
2964 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2965 [IPVS_DAEMON_ATTR_SYNC_MAXLEN] = { .type = NLA_U16 },
2966 [IPVS_DAEMON_ATTR_MCAST_GROUP] = { .type = NLA_U32 },
2967 [IPVS_DAEMON_ATTR_MCAST_GROUP6] = { .len = sizeof(struct in6_addr) },
2968 [IPVS_DAEMON_ATTR_MCAST_PORT] = { .type = NLA_U16 },
2969 [IPVS_DAEMON_ATTR_MCAST_TTL] = { .type = NLA_U8 },
2972 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2973 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2974 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2975 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2976 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2977 .len = sizeof(union nf_inet_addr) },
2978 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2979 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2980 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2981 .len = IP_VS_SCHEDNAME_MAXLEN - 1 },
2982 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2983 .len = IP_VS_PENAME_MAXLEN },
2984 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2985 .len = sizeof(struct ip_vs_flags) },
2986 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2987 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2988 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2991 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2992 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2993 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2994 .len = sizeof(union nf_inet_addr) },
2995 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2996 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2997 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2998 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2999 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
3000 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
3001 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
3002 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
3003 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
3004 [IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
3005 [IPVS_DEST_ATTR_TUN_TYPE] = { .type = NLA_U8 },
3006 [IPVS_DEST_ATTR_TUN_PORT] = { .type = NLA_U16 },
3007 [IPVS_DEST_ATTR_TUN_FLAGS] = { .type = NLA_U16 },
3010 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
3011 struct ip_vs_kstats *kstats)
3013 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3018 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
3019 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
3020 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
3021 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3022 IPVS_STATS_ATTR_PAD) ||
3023 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3024 IPVS_STATS_ATTR_PAD) ||
3025 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
3026 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
3027 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
3028 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
3029 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
3030 goto nla_put_failure;
3031 nla_nest_end(skb, nl_stats);
3036 nla_nest_cancel(skb, nl_stats);
3040 static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
3041 struct ip_vs_kstats *kstats)
3043 struct nlattr *nl_stats = nla_nest_start_noflag(skb, container_type);
3048 if (nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CONNS, kstats->conns,
3049 IPVS_STATS_ATTR_PAD) ||
3050 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts,
3051 IPVS_STATS_ATTR_PAD) ||
3052 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts,
3053 IPVS_STATS_ATTR_PAD) ||
3054 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes,
3055 IPVS_STATS_ATTR_PAD) ||
3056 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes,
3057 IPVS_STATS_ATTR_PAD) ||
3058 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_CPS, kstats->cps,
3059 IPVS_STATS_ATTR_PAD) ||
3060 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps,
3061 IPVS_STATS_ATTR_PAD) ||
3062 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps,
3063 IPVS_STATS_ATTR_PAD) ||
3064 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps,
3065 IPVS_STATS_ATTR_PAD) ||
3066 nla_put_u64_64bit(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps,
3067 IPVS_STATS_ATTR_PAD))
3068 goto nla_put_failure;
3069 nla_nest_end(skb, nl_stats);
3074 nla_nest_cancel(skb, nl_stats);
3078 static int ip_vs_genl_fill_service(struct sk_buff *skb,
3079 struct ip_vs_service *svc)
3081 struct ip_vs_scheduler *sched;
3082 struct ip_vs_pe *pe;
3083 struct nlattr *nl_service;
3084 struct ip_vs_flags flags = { .flags = svc->flags,
3086 struct ip_vs_kstats kstats;
3089 nl_service = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_SERVICE);
3093 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
3094 goto nla_put_failure;
3096 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
3097 goto nla_put_failure;
3099 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
3100 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
3101 nla_put_be16(skb, IPVS_SVC_ATTR_PORT, svc->port))
3102 goto nla_put_failure;
3105 sched = rcu_dereference_protected(svc->scheduler, 1);
3106 sched_name = sched ? sched->name : "none";
3107 pe = rcu_dereference_protected(svc->pe, 1);
3108 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) ||
3109 (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) ||
3110 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
3111 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
3112 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
3113 goto nla_put_failure;
3114 ip_vs_copy_stats(&kstats, &svc->stats);
3115 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
3116 goto nla_put_failure;
3117 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
3118 goto nla_put_failure;
3120 nla_nest_end(skb, nl_service);
3125 nla_nest_cancel(skb, nl_service);
3129 static int ip_vs_genl_dump_service(struct sk_buff *skb,
3130 struct ip_vs_service *svc,
3131 struct netlink_callback *cb)
3135 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3136 &ip_vs_genl_family, NLM_F_MULTI,
3137 IPVS_CMD_NEW_SERVICE);
3141 if (ip_vs_genl_fill_service(skb, svc) < 0)
3142 goto nla_put_failure;
3144 genlmsg_end(skb, hdr);
3148 genlmsg_cancel(skb, hdr);
3152 static int ip_vs_genl_dump_services(struct sk_buff *skb,
3153 struct netlink_callback *cb)
3156 int start = cb->args[0];
3157 struct ip_vs_service *svc;
3158 struct net *net = sock_net(skb->sk);
3159 struct netns_ipvs *ipvs = net_ipvs(net);
3161 mutex_lock(&__ip_vs_mutex);
3162 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3163 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
3164 if (++idx <= start || (svc->ipvs != ipvs))
3166 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3168 goto nla_put_failure;
3173 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
3174 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
3175 if (++idx <= start || (svc->ipvs != ipvs))
3177 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
3179 goto nla_put_failure;
3185 mutex_unlock(&__ip_vs_mutex);
3191 static bool ip_vs_is_af_valid(int af)
3195 #ifdef CONFIG_IP_VS_IPV6
3196 if (af == AF_INET6 && ipv6_mod_enabled())
3202 static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs,
3203 struct ip_vs_service_user_kern *usvc,
3204 struct nlattr *nla, bool full_entry,
3205 struct ip_vs_service **ret_svc)
3207 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
3208 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
3209 struct ip_vs_service *svc;
3211 /* Parse mandatory identifying service fields first */
3213 nla_parse_nested_deprecated(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy, NULL))
3216 nla_af = attrs[IPVS_SVC_ATTR_AF];
3217 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
3218 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
3219 nla_port = attrs[IPVS_SVC_ATTR_PORT];
3220 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
3222 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
3225 memset(usvc, 0, sizeof(*usvc));
3227 usvc->af = nla_get_u16(nla_af);
3228 if (!ip_vs_is_af_valid(usvc->af))
3229 return -EAFNOSUPPORT;
3232 usvc->protocol = IPPROTO_TCP;
3233 usvc->fwmark = nla_get_u32(nla_fwmark);
3235 usvc->protocol = nla_get_u16(nla_protocol);
3236 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3237 usvc->port = nla_get_be16(nla_port);
3243 svc = __ip_vs_svc_fwm_find(ipvs, usvc->af, usvc->fwmark);
3245 svc = __ip_vs_service_find(ipvs, usvc->af, usvc->protocol,
3246 &usvc->addr, usvc->port);
3250 /* If a full entry was requested, check for the additional fields */
3252 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
3254 struct ip_vs_flags flags;
3256 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
3257 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
3258 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3259 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3260 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3262 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3265 nla_memcpy(&flags, nla_flags, sizeof(flags));
3267 /* prefill flags from service if it already exists */
3269 usvc->flags = svc->flags;
3271 /* set new flags from userland */
3272 usvc->flags = (usvc->flags & ~flags.mask) |
3273 (flags.flags & flags.mask);
3274 usvc->sched_name = nla_data(nla_sched);
3275 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
3276 usvc->timeout = nla_get_u32(nla_timeout);
3277 usvc->netmask = nla_get_be32(nla_netmask);
3283 static struct ip_vs_service *ip_vs_genl_find_service(struct netns_ipvs *ipvs,
3286 struct ip_vs_service_user_kern usvc;
3287 struct ip_vs_service *svc;
3290 ret = ip_vs_genl_parse_service(ipvs, &usvc, nla, false, &svc);
3291 return ret ? ERR_PTR(ret) : svc;
3294 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3296 struct nlattr *nl_dest;
3297 struct ip_vs_kstats kstats;
3299 nl_dest = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DEST);
3303 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3304 nla_put_be16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3305 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3306 (atomic_read(&dest->conn_flags) &
3307 IP_VS_CONN_F_FWD_MASK)) ||
3308 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3309 atomic_read(&dest->weight)) ||
3310 nla_put_u8(skb, IPVS_DEST_ATTR_TUN_TYPE,
3312 nla_put_be16(skb, IPVS_DEST_ATTR_TUN_PORT,
3314 nla_put_u16(skb, IPVS_DEST_ATTR_TUN_FLAGS,
3316 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3317 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3318 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3319 atomic_read(&dest->activeconns)) ||
3320 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3321 atomic_read(&dest->inactconns)) ||
3322 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3323 atomic_read(&dest->persistconns)) ||
3324 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3325 goto nla_put_failure;
3326 ip_vs_copy_stats(&kstats, &dest->stats);
3327 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3328 goto nla_put_failure;
3329 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3330 goto nla_put_failure;
3332 nla_nest_end(skb, nl_dest);
3337 nla_nest_cancel(skb, nl_dest);
3341 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3342 struct netlink_callback *cb)
3346 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3347 &ip_vs_genl_family, NLM_F_MULTI,
3352 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3353 goto nla_put_failure;
3355 genlmsg_end(skb, hdr);
3359 genlmsg_cancel(skb, hdr);
3363 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3364 struct netlink_callback *cb)
3367 int start = cb->args[0];
3368 struct ip_vs_service *svc;
3369 struct ip_vs_dest *dest;
3370 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3371 struct net *net = sock_net(skb->sk);
3372 struct netns_ipvs *ipvs = net_ipvs(net);
3374 mutex_lock(&__ip_vs_mutex);
3376 /* Try to find the service for which to dump destinations */
3377 if (nlmsg_parse_deprecated(cb->nlh, GENL_HDRLEN, attrs, IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy, cb->extack))
3381 svc = ip_vs_genl_find_service(ipvs, attrs[IPVS_CMD_ATTR_SERVICE]);
3382 if (IS_ERR_OR_NULL(svc))
3385 /* Dump the destinations */
3386 list_for_each_entry(dest, &svc->destinations, n_list) {
3389 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3391 goto nla_put_failure;
3399 mutex_unlock(&__ip_vs_mutex);
3404 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3405 struct nlattr *nla, bool full_entry)
3407 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3408 struct nlattr *nla_addr, *nla_port;
3409 struct nlattr *nla_addr_family;
3411 /* Parse mandatory identifying destination fields first */
3413 nla_parse_nested_deprecated(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy, NULL))
3416 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3417 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3418 nla_addr_family = attrs[IPVS_DEST_ATTR_ADDR_FAMILY];
3420 if (!(nla_addr && nla_port))
3423 memset(udest, 0, sizeof(*udest));
3425 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3426 udest->port = nla_get_be16(nla_port);
3428 if (nla_addr_family)
3429 udest->af = nla_get_u16(nla_addr_family);
3433 /* If a full entry was requested, check for the additional fields */
3435 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3436 *nla_l_thresh, *nla_tun_type, *nla_tun_port,
3439 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3440 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3441 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3442 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3443 nla_tun_type = attrs[IPVS_DEST_ATTR_TUN_TYPE];
3444 nla_tun_port = attrs[IPVS_DEST_ATTR_TUN_PORT];
3445 nla_tun_flags = attrs[IPVS_DEST_ATTR_TUN_FLAGS];
3447 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3450 udest->conn_flags = nla_get_u32(nla_fwd)
3451 & IP_VS_CONN_F_FWD_MASK;
3452 udest->weight = nla_get_u32(nla_weight);
3453 udest->u_threshold = nla_get_u32(nla_u_thresh);
3454 udest->l_threshold = nla_get_u32(nla_l_thresh);
3457 udest->tun_type = nla_get_u8(nla_tun_type);
3460 udest->tun_port = nla_get_be16(nla_tun_port);
3463 udest->tun_flags = nla_get_u16(nla_tun_flags);
3469 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __u32 state,
3470 struct ipvs_sync_daemon_cfg *c)
3472 struct nlattr *nl_daemon;
3474 nl_daemon = nla_nest_start_noflag(skb, IPVS_CMD_ATTR_DAEMON);
3478 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3479 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, c->mcast_ifn) ||
3480 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, c->syncid) ||
3481 nla_put_u16(skb, IPVS_DAEMON_ATTR_SYNC_MAXLEN, c->sync_maxlen) ||
3482 nla_put_u16(skb, IPVS_DAEMON_ATTR_MCAST_PORT, c->mcast_port) ||
3483 nla_put_u8(skb, IPVS_DAEMON_ATTR_MCAST_TTL, c->mcast_ttl))
3484 goto nla_put_failure;
3485 #ifdef CONFIG_IP_VS_IPV6
3486 if (c->mcast_af == AF_INET6) {
3487 if (nla_put_in6_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP6,
3488 &c->mcast_group.in6))
3489 goto nla_put_failure;
3492 if (c->mcast_af == AF_INET &&
3493 nla_put_in_addr(skb, IPVS_DAEMON_ATTR_MCAST_GROUP,
3495 goto nla_put_failure;
3496 nla_nest_end(skb, nl_daemon);
3501 nla_nest_cancel(skb, nl_daemon);
3505 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __u32 state,
3506 struct ipvs_sync_daemon_cfg *c,
3507 struct netlink_callback *cb)
3510 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3511 &ip_vs_genl_family, NLM_F_MULTI,
3512 IPVS_CMD_NEW_DAEMON);
3516 if (ip_vs_genl_fill_daemon(skb, state, c))
3517 goto nla_put_failure;
3519 genlmsg_end(skb, hdr);
3523 genlmsg_cancel(skb, hdr);
3527 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3528 struct netlink_callback *cb)
3530 struct net *net = sock_net(skb->sk);
3531 struct netns_ipvs *ipvs = net_ipvs(net);
3533 mutex_lock(&ipvs->sync_mutex);
3534 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3535 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3536 &ipvs->mcfg, cb) < 0)
3537 goto nla_put_failure;
3542 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3543 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3544 &ipvs->bcfg, cb) < 0)
3545 goto nla_put_failure;
3551 mutex_unlock(&ipvs->sync_mutex);
3556 static int ip_vs_genl_new_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3558 struct ipvs_sync_daemon_cfg c;
3562 memset(&c, 0, sizeof(c));
3563 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3564 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3565 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3567 strscpy(c.mcast_ifn, nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3568 sizeof(c.mcast_ifn));
3569 c.syncid = nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]);
3571 a = attrs[IPVS_DAEMON_ATTR_SYNC_MAXLEN];
3573 c.sync_maxlen = nla_get_u16(a);
3575 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP];
3577 c.mcast_af = AF_INET;
3578 c.mcast_group.ip = nla_get_in_addr(a);
3579 if (!ipv4_is_multicast(c.mcast_group.ip))
3582 a = attrs[IPVS_DAEMON_ATTR_MCAST_GROUP6];
3584 #ifdef CONFIG_IP_VS_IPV6
3587 c.mcast_af = AF_INET6;
3588 c.mcast_group.in6 = nla_get_in6_addr(a);
3589 addr_type = ipv6_addr_type(&c.mcast_group.in6);
3590 if (!(addr_type & IPV6_ADDR_MULTICAST))
3593 return -EAFNOSUPPORT;
3598 a = attrs[IPVS_DAEMON_ATTR_MCAST_PORT];
3600 c.mcast_port = nla_get_u16(a);
3602 a = attrs[IPVS_DAEMON_ATTR_MCAST_TTL];
3604 c.mcast_ttl = nla_get_u8(a);
3606 /* The synchronization protocol is incompatible with mixed family
3609 if (ipvs->mixed_address_family_dests > 0)
3612 ret = start_sync_thread(ipvs, &c,
3613 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3617 static int ip_vs_genl_del_daemon(struct netns_ipvs *ipvs, struct nlattr **attrs)
3621 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3624 ret = stop_sync_thread(ipvs,
3625 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3629 static int ip_vs_genl_set_config(struct netns_ipvs *ipvs, struct nlattr **attrs)
3631 struct ip_vs_timeout_user t;
3633 __ip_vs_get_timeouts(ipvs, &t);
3635 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3636 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3638 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3640 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3642 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3643 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3645 return ip_vs_set_timeout(ipvs, &t);
3648 static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
3650 int ret = -EINVAL, cmd;
3651 struct net *net = sock_net(skb->sk);
3652 struct netns_ipvs *ipvs = net_ipvs(net);
3654 cmd = info->genlhdr->cmd;
3656 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
3657 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3659 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3660 nla_parse_nested_deprecated(daemon_attrs, IPVS_DAEMON_ATTR_MAX, info->attrs[IPVS_CMD_ATTR_DAEMON], ip_vs_daemon_policy, info->extack))
3663 if (cmd == IPVS_CMD_NEW_DAEMON)
3664 ret = ip_vs_genl_new_daemon(ipvs, daemon_attrs);
3666 ret = ip_vs_genl_del_daemon(ipvs, daemon_attrs);
3673 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3675 bool need_full_svc = false, need_full_dest = false;
3676 struct ip_vs_service *svc = NULL;
3677 struct ip_vs_service_user_kern usvc;
3678 struct ip_vs_dest_user_kern udest;
3680 struct net *net = sock_net(skb->sk);
3681 struct netns_ipvs *ipvs = net_ipvs(net);
3683 cmd = info->genlhdr->cmd;
3685 mutex_lock(&__ip_vs_mutex);
3687 if (cmd == IPVS_CMD_FLUSH) {
3688 ret = ip_vs_flush(ipvs, false);
3690 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3691 ret = ip_vs_genl_set_config(ipvs, info->attrs);
3693 } else if (cmd == IPVS_CMD_ZERO &&
3694 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3695 ret = ip_vs_zero_all(ipvs);
3699 /* All following commands require a service argument, so check if we
3700 * received a valid one. We need a full service specification when
3701 * adding / editing a service. Only identifying members otherwise. */
3702 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3703 need_full_svc = true;
3705 ret = ip_vs_genl_parse_service(ipvs, &usvc,
3706 info->attrs[IPVS_CMD_ATTR_SERVICE],
3707 need_full_svc, &svc);
3711 /* Unless we're adding a new service, the service must already exist */
3712 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3717 /* Destination commands require a valid destination argument. For
3718 * adding / editing a destination, we need a full destination
3720 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3721 cmd == IPVS_CMD_DEL_DEST) {
3722 if (cmd != IPVS_CMD_DEL_DEST)
3723 need_full_dest = true;
3725 ret = ip_vs_genl_parse_dest(&udest,
3726 info->attrs[IPVS_CMD_ATTR_DEST],
3731 /* Old protocols did not allow the user to specify address
3732 * family, so we set it to zero instead. We also didn't
3733 * allow heterogeneous pools in the old code, so it's safe
3734 * to assume that this will have the same address family as
3740 if (!ip_vs_is_af_valid(udest.af)) {
3741 ret = -EAFNOSUPPORT;
3745 if (udest.af != svc->af && cmd != IPVS_CMD_DEL_DEST) {
3746 /* The synchronization protocol is incompatible
3747 * with mixed family services
3749 if (ipvs->sync_state) {
3754 /* Which connection types do we support? */
3755 switch (udest.conn_flags) {
3756 case IP_VS_CONN_F_TUNNEL:
3757 /* We are able to forward this */
3767 case IPVS_CMD_NEW_SERVICE:
3769 ret = ip_vs_add_service(ipvs, &usvc, &svc);
3773 case IPVS_CMD_SET_SERVICE:
3774 ret = ip_vs_edit_service(svc, &usvc);
3776 case IPVS_CMD_DEL_SERVICE:
3777 ret = ip_vs_del_service(svc);
3778 /* do not use svc, it can be freed */
3780 case IPVS_CMD_NEW_DEST:
3781 ret = ip_vs_add_dest(svc, &udest);
3783 case IPVS_CMD_SET_DEST:
3784 ret = ip_vs_edit_dest(svc, &udest);
3786 case IPVS_CMD_DEL_DEST:
3787 ret = ip_vs_del_dest(svc, &udest);
3790 ret = ip_vs_zero_service(svc);
3797 mutex_unlock(&__ip_vs_mutex);
3802 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3804 struct sk_buff *msg;
3806 int ret, cmd, reply_cmd;
3807 struct net *net = sock_net(skb->sk);
3808 struct netns_ipvs *ipvs = net_ipvs(net);
3810 cmd = info->genlhdr->cmd;
3812 if (cmd == IPVS_CMD_GET_SERVICE)
3813 reply_cmd = IPVS_CMD_NEW_SERVICE;
3814 else if (cmd == IPVS_CMD_GET_INFO)
3815 reply_cmd = IPVS_CMD_SET_INFO;
3816 else if (cmd == IPVS_CMD_GET_CONFIG)
3817 reply_cmd = IPVS_CMD_SET_CONFIG;
3819 pr_err("unknown Generic Netlink command\n");
3823 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3827 mutex_lock(&__ip_vs_mutex);
3829 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3831 goto nla_put_failure;
3834 case IPVS_CMD_GET_SERVICE:
3836 struct ip_vs_service *svc;
3838 svc = ip_vs_genl_find_service(ipvs,
3839 info->attrs[IPVS_CMD_ATTR_SERVICE]);
3844 ret = ip_vs_genl_fill_service(msg, svc);
3846 goto nla_put_failure;
3855 case IPVS_CMD_GET_CONFIG:
3857 struct ip_vs_timeout_user t;
3859 __ip_vs_get_timeouts(ipvs, &t);
3860 #ifdef CONFIG_IP_VS_PROTO_TCP
3861 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3863 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3865 goto nla_put_failure;
3867 #ifdef CONFIG_IP_VS_PROTO_UDP
3868 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3869 goto nla_put_failure;
3875 case IPVS_CMD_GET_INFO:
3876 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3877 IP_VS_VERSION_CODE) ||
3878 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3879 ip_vs_conn_tab_size))
3880 goto nla_put_failure;
3884 genlmsg_end(msg, reply);
3885 ret = genlmsg_reply(msg, info);
3889 pr_err("not enough space in Netlink message\n");
3895 mutex_unlock(&__ip_vs_mutex);
3901 static const struct genl_small_ops ip_vs_genl_ops[] = {
3903 .cmd = IPVS_CMD_NEW_SERVICE,
3904 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3905 .flags = GENL_ADMIN_PERM,
3906 .doit = ip_vs_genl_set_cmd,
3909 .cmd = IPVS_CMD_SET_SERVICE,
3910 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3911 .flags = GENL_ADMIN_PERM,
3912 .doit = ip_vs_genl_set_cmd,
3915 .cmd = IPVS_CMD_DEL_SERVICE,
3916 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3917 .flags = GENL_ADMIN_PERM,
3918 .doit = ip_vs_genl_set_cmd,
3921 .cmd = IPVS_CMD_GET_SERVICE,
3922 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3923 .flags = GENL_ADMIN_PERM,
3924 .doit = ip_vs_genl_get_cmd,
3925 .dumpit = ip_vs_genl_dump_services,
3928 .cmd = IPVS_CMD_NEW_DEST,
3929 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3930 .flags = GENL_ADMIN_PERM,
3931 .doit = ip_vs_genl_set_cmd,
3934 .cmd = IPVS_CMD_SET_DEST,
3935 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3936 .flags = GENL_ADMIN_PERM,
3937 .doit = ip_vs_genl_set_cmd,
3940 .cmd = IPVS_CMD_DEL_DEST,
3941 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3942 .flags = GENL_ADMIN_PERM,
3943 .doit = ip_vs_genl_set_cmd,
3946 .cmd = IPVS_CMD_GET_DEST,
3947 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3948 .flags = GENL_ADMIN_PERM,
3949 .dumpit = ip_vs_genl_dump_dests,
3952 .cmd = IPVS_CMD_NEW_DAEMON,
3953 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3954 .flags = GENL_ADMIN_PERM,
3955 .doit = ip_vs_genl_set_daemon,
3958 .cmd = IPVS_CMD_DEL_DAEMON,
3959 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3960 .flags = GENL_ADMIN_PERM,
3961 .doit = ip_vs_genl_set_daemon,
3964 .cmd = IPVS_CMD_GET_DAEMON,
3965 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3966 .flags = GENL_ADMIN_PERM,
3967 .dumpit = ip_vs_genl_dump_daemons,
3970 .cmd = IPVS_CMD_SET_CONFIG,
3971 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3972 .flags = GENL_ADMIN_PERM,
3973 .doit = ip_vs_genl_set_cmd,
3976 .cmd = IPVS_CMD_GET_CONFIG,
3977 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3978 .flags = GENL_ADMIN_PERM,
3979 .doit = ip_vs_genl_get_cmd,
3982 .cmd = IPVS_CMD_GET_INFO,
3983 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3984 .flags = GENL_ADMIN_PERM,
3985 .doit = ip_vs_genl_get_cmd,
3988 .cmd = IPVS_CMD_ZERO,
3989 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3990 .flags = GENL_ADMIN_PERM,
3991 .doit = ip_vs_genl_set_cmd,
3994 .cmd = IPVS_CMD_FLUSH,
3995 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3996 .flags = GENL_ADMIN_PERM,
3997 .doit = ip_vs_genl_set_cmd,
4001 static struct genl_family ip_vs_genl_family __ro_after_init = {
4003 .name = IPVS_GENL_NAME,
4004 .version = IPVS_GENL_VERSION,
4005 .maxattr = IPVS_CMD_ATTR_MAX,
4006 .policy = ip_vs_cmd_policy,
4007 .netnsok = true, /* Make ipvsadm to work on netns */
4008 .module = THIS_MODULE,
4009 .small_ops = ip_vs_genl_ops,
4010 .n_small_ops = ARRAY_SIZE(ip_vs_genl_ops),
4011 .resv_start_op = IPVS_CMD_FLUSH + 1,
4014 static int __init ip_vs_genl_register(void)
4016 return genl_register_family(&ip_vs_genl_family);
4019 static void ip_vs_genl_unregister(void)
4021 genl_unregister_family(&ip_vs_genl_family);
4024 /* End of Generic Netlink interface definitions */
4027 * per netns intit/exit func.
4029 #ifdef CONFIG_SYSCTL
4030 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs)
4032 struct net *net = ipvs->net;
4034 struct ctl_table *tbl;
4036 atomic_set(&ipvs->dropentry, 0);
4037 spin_lock_init(&ipvs->dropentry_lock);
4038 spin_lock_init(&ipvs->droppacket_lock);
4039 spin_lock_init(&ipvs->securetcp_lock);
4041 if (!net_eq(net, &init_net)) {
4042 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
4046 /* Don't export sysctls to unprivileged users */
4047 if (net->user_ns != &init_user_ns)
4048 tbl[0].procname = NULL;
4051 /* Initialize sysctl defaults */
4052 for (idx = 0; idx < ARRAY_SIZE(vs_vars); idx++) {
4053 if (tbl[idx].proc_handler == proc_do_defense_mode)
4054 tbl[idx].extra2 = ipvs;
4057 ipvs->sysctl_amemthresh = 1024;
4058 tbl[idx++].data = &ipvs->sysctl_amemthresh;
4059 ipvs->sysctl_am_droprate = 10;
4060 tbl[idx++].data = &ipvs->sysctl_am_droprate;
4061 tbl[idx++].data = &ipvs->sysctl_drop_entry;
4062 tbl[idx++].data = &ipvs->sysctl_drop_packet;
4063 #ifdef CONFIG_IP_VS_NFCT
4064 tbl[idx++].data = &ipvs->sysctl_conntrack;
4066 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
4067 ipvs->sysctl_snat_reroute = 1;
4068 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
4069 ipvs->sysctl_sync_ver = 1;
4070 tbl[idx++].data = &ipvs->sysctl_sync_ver;
4071 ipvs->sysctl_sync_ports = 1;
4072 tbl[idx++].data = &ipvs->sysctl_sync_ports;
4073 tbl[idx++].data = &ipvs->sysctl_sync_persist_mode;
4074 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
4075 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
4076 ipvs->sysctl_sync_sock_size = 0;
4077 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
4078 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
4079 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
4080 tbl[idx++].data = &ipvs->sysctl_sloppy_tcp;
4081 tbl[idx++].data = &ipvs->sysctl_sloppy_sctp;
4082 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
4083 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
4084 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
4085 tbl[idx].data = &ipvs->sysctl_sync_threshold;
4086 tbl[idx].extra2 = ipvs;
4087 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
4088 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
4089 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
4090 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
4091 tbl[idx++].data = &ipvs->sysctl_sync_retries;
4092 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
4093 ipvs->sysctl_pmtu_disc = 1;
4094 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
4095 tbl[idx++].data = &ipvs->sysctl_backup_only;
4096 ipvs->sysctl_conn_reuse_mode = 1;
4097 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
4098 tbl[idx++].data = &ipvs->sysctl_schedule_icmp;
4099 tbl[idx++].data = &ipvs->sysctl_ignore_tunneled;
4100 ipvs->sysctl_run_estimation = 1;
4101 tbl[idx++].data = &ipvs->sysctl_run_estimation;
4102 #ifdef CONFIG_IP_VS_DEBUG
4103 /* Global sysctls must be ro in non-init netns */
4104 if (!net_eq(net, &init_net))
4105 tbl[idx++].mode = 0444;
4108 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
4109 if (ipvs->sysctl_hdr == NULL) {
4110 if (!net_eq(net, &init_net))
4114 ip_vs_start_estimator(ipvs, &ipvs->tot_stats);
4115 ipvs->sysctl_tbl = tbl;
4116 /* Schedule defense work */
4117 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
4118 queue_delayed_work(system_long_wq, &ipvs->defense_work,
4119 DEFENSE_TIMER_PERIOD);
4121 /* Init delayed work for expiring no dest conn */
4122 INIT_DELAYED_WORK(&ipvs->expire_nodest_conn_work,
4123 expire_nodest_conn_handler);
4128 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs)
4130 struct net *net = ipvs->net;
4132 cancel_delayed_work_sync(&ipvs->expire_nodest_conn_work);
4133 cancel_delayed_work_sync(&ipvs->defense_work);
4134 cancel_work_sync(&ipvs->defense_work.work);
4135 unregister_net_sysctl_table(ipvs->sysctl_hdr);
4136 ip_vs_stop_estimator(ipvs, &ipvs->tot_stats);
4138 if (!net_eq(net, &init_net))
4139 kfree(ipvs->sysctl_tbl);
4144 static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) { return 0; }
4145 static void __net_exit ip_vs_control_net_cleanup_sysctl(struct netns_ipvs *ipvs) { }
4149 static struct notifier_block ip_vs_dst_notifier = {
4150 .notifier_call = ip_vs_dst_event,
4151 #ifdef CONFIG_IP_VS_IPV6
4152 .priority = ADDRCONF_NOTIFY_PRIORITY + 5,
4156 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
4160 /* Initialize rs_table */
4161 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
4162 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
4164 INIT_LIST_HEAD(&ipvs->dest_trash);
4165 spin_lock_init(&ipvs->dest_trash_lock);
4166 timer_setup(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire, 0);
4167 atomic_set(&ipvs->ftpsvc_counter, 0);
4168 atomic_set(&ipvs->nullsvc_counter, 0);
4169 atomic_set(&ipvs->conn_out_counter, 0);
4172 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
4173 if (!ipvs->tot_stats.cpustats)
4176 for_each_possible_cpu(i) {
4177 struct ip_vs_cpu_stats *ipvs_tot_stats;
4178 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
4179 u64_stats_init(&ipvs_tot_stats->syncp);
4182 spin_lock_init(&ipvs->tot_stats.lock);
4184 #ifdef CONFIG_PROC_FS
4185 if (!proc_create_net("ip_vs", 0, ipvs->net->proc_net,
4186 &ip_vs_info_seq_ops, sizeof(struct ip_vs_iter)))
4188 if (!proc_create_net_single("ip_vs_stats", 0, ipvs->net->proc_net,
4189 ip_vs_stats_show, NULL))
4191 if (!proc_create_net_single("ip_vs_stats_percpu", 0,
4192 ipvs->net->proc_net,
4193 ip_vs_stats_percpu_show, NULL))
4197 if (ip_vs_control_net_init_sysctl(ipvs))
4203 #ifdef CONFIG_PROC_FS
4204 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4207 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4210 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4214 free_percpu(ipvs->tot_stats.cpustats);
4218 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
4220 ip_vs_trash_cleanup(ipvs);
4221 ip_vs_control_net_cleanup_sysctl(ipvs);
4222 #ifdef CONFIG_PROC_FS
4223 remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
4224 remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
4225 remove_proc_entry("ip_vs", ipvs->net->proc_net);
4227 free_percpu(ipvs->tot_stats.cpustats);
4230 int __init ip_vs_register_nl_ioctl(void)
4234 ret = nf_register_sockopt(&ip_vs_sockopts);
4236 pr_err("cannot register sockopt.\n");
4240 ret = ip_vs_genl_register();
4242 pr_err("cannot register Generic Netlink interface.\n");
4248 nf_unregister_sockopt(&ip_vs_sockopts);
4253 void ip_vs_unregister_nl_ioctl(void)
4255 ip_vs_genl_unregister();
4256 nf_unregister_sockopt(&ip_vs_sockopts);
4259 int __init ip_vs_control_init(void)
4266 /* Initialize svc_table, ip_vs_svc_fwm_table */
4267 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
4268 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
4269 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
4272 smp_wmb(); /* Do we really need it now ? */
4274 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
4283 void ip_vs_control_cleanup(void)
4286 unregister_netdevice_notifier(&ip_vs_dst_notifier);