2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/workqueue.h>
32 #include <linux/swap.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
46 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
48 #include <net/route.h>
50 #include <net/genetlink.h>
52 #include <linux/uaccess.h>
54 #include <net/ip_vs.h>
56 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
57 static DEFINE_MUTEX(__ip_vs_mutex
);
59 /* sysctl variables */
61 #ifdef CONFIG_IP_VS_DEBUG
62 static int sysctl_ip_vs_debug_level
= 0;
64 int ip_vs_get_debug_level(void)
66 return sysctl_ip_vs_debug_level
;
72 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
);
75 #ifdef CONFIG_IP_VS_IPV6
76 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
77 static bool __ip_vs_addr_is_local_v6(struct net
*net
,
78 const struct in6_addr
*addr
)
83 struct dst_entry
*dst
= ip6_route_output(net
, NULL
, &fl6
);
86 is_local
= !dst
->error
&& dst
->dev
&& (dst
->dev
->flags
& IFF_LOOPBACK
);
95 * update_defense_level is called from keventd and from sysctl,
96 * so it needs to protect itself from softirqs
98 static void update_defense_level(struct netns_ipvs
*ipvs
)
105 /* we only count free and buffered memory (in pages) */
107 availmem
= i
.freeram
+ i
.bufferram
;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem
= (availmem
< ipvs
->sysctl_amemthresh
);
118 spin_lock(&ipvs
->dropentry_lock
);
119 switch (ipvs
->sysctl_drop_entry
) {
121 atomic_set(&ipvs
->dropentry
, 0);
125 atomic_set(&ipvs
->dropentry
, 1);
126 ipvs
->sysctl_drop_entry
= 2;
128 atomic_set(&ipvs
->dropentry
, 0);
133 atomic_set(&ipvs
->dropentry
, 1);
135 atomic_set(&ipvs
->dropentry
, 0);
136 ipvs
->sysctl_drop_entry
= 1;
140 atomic_set(&ipvs
->dropentry
, 1);
143 spin_unlock(&ipvs
->dropentry_lock
);
146 spin_lock(&ipvs
->droppacket_lock
);
147 switch (ipvs
->sysctl_drop_packet
) {
153 ipvs
->drop_rate
= ipvs
->drop_counter
154 = ipvs
->sysctl_amemthresh
/
155 (ipvs
->sysctl_amemthresh
-availmem
);
156 ipvs
->sysctl_drop_packet
= 2;
163 ipvs
->drop_rate
= ipvs
->drop_counter
164 = ipvs
->sysctl_amemthresh
/
165 (ipvs
->sysctl_amemthresh
-availmem
);
168 ipvs
->sysctl_drop_packet
= 1;
172 ipvs
->drop_rate
= ipvs
->sysctl_am_droprate
;
175 spin_unlock(&ipvs
->droppacket_lock
);
178 spin_lock(&ipvs
->securetcp_lock
);
179 switch (ipvs
->sysctl_secure_tcp
) {
181 if (ipvs
->old_secure_tcp
>= 2)
186 if (ipvs
->old_secure_tcp
< 2)
188 ipvs
->sysctl_secure_tcp
= 2;
190 if (ipvs
->old_secure_tcp
>= 2)
196 if (ipvs
->old_secure_tcp
< 2)
199 if (ipvs
->old_secure_tcp
>= 2)
201 ipvs
->sysctl_secure_tcp
= 1;
205 if (ipvs
->old_secure_tcp
< 2)
209 ipvs
->old_secure_tcp
= ipvs
->sysctl_secure_tcp
;
211 ip_vs_protocol_timeout_change(ipvs
,
212 ipvs
->sysctl_secure_tcp
> 1);
213 spin_unlock(&ipvs
->securetcp_lock
);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct
*work
)
226 struct netns_ipvs
*ipvs
=
227 container_of(work
, struct netns_ipvs
, defense_work
.work
);
229 update_defense_level(ipvs
);
230 if (atomic_read(&ipvs
->dropentry
))
231 ip_vs_random_dropentry(ipvs
);
232 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE
);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE
);
250 * Hash table: for virtual service lookups
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct netns_ipvs
*ipvs
, int af
, unsigned int proto
,
267 const union nf_inet_addr
*addr
, __be16 port
)
269 register unsigned int porth
= ntohs(port
);
270 __be32 addr_fold
= addr
->ip
;
273 #ifdef CONFIG_IP_VS_IPV6
275 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
276 addr
->ip6
[2]^addr
->ip6
[3];
278 ahash
= ntohl(addr_fold
);
279 ahash
^= ((size_t) ipvs
>> 8);
281 return (proto
^ ahash
^ (porth
>> IP_VS_SVC_TAB_BITS
) ^ porth
) &
286 * Returns hash value of fwmark for virtual service lookup
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct netns_ipvs
*ipvs
, __u32 fwmark
)
290 return (((size_t)ipvs
>>8) ^ fwmark
) & IP_VS_SVC_TAB_MASK
;
294 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
295 * or in the ip_vs_svc_fwm_table by fwmark.
296 * Should be called with locked tables.
298 static int ip_vs_svc_hash(struct ip_vs_service
*svc
)
302 if (svc
->flags
& IP_VS_SVC_F_HASHED
) {
303 pr_err("%s(): request for already hashed, called from %pS\n",
304 __func__
, __builtin_return_address(0));
308 if (svc
->fwmark
== 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash
= ip_vs_svc_hashkey(svc
->ipvs
, svc
->af
, svc
->protocol
,
313 &svc
->addr
, svc
->port
);
314 hlist_add_head_rcu(&svc
->s_list
, &ip_vs_svc_table
[hash
]);
317 * Hash it by fwmark in svc_fwm_table
319 hash
= ip_vs_svc_fwm_hashkey(svc
->ipvs
, svc
->fwmark
);
320 hlist_add_head_rcu(&svc
->f_list
, &ip_vs_svc_fwm_table
[hash
]);
323 svc
->flags
|= IP_VS_SVC_F_HASHED
;
324 /* increase its refcnt because it is referenced by the svc table */
325 atomic_inc(&svc
->refcnt
);
331 * Unhashes a service from svc_table / svc_fwm_table.
332 * Should be called with locked tables.
334 static int ip_vs_svc_unhash(struct ip_vs_service
*svc
)
336 if (!(svc
->flags
& IP_VS_SVC_F_HASHED
)) {
337 pr_err("%s(): request for unhash flagged, called from %pS\n",
338 __func__
, __builtin_return_address(0));
342 if (svc
->fwmark
== 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc
->s_list
);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc
->f_list
);
350 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
351 atomic_dec(&svc
->refcnt
);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service
*
360 __ip_vs_service_find(struct netns_ipvs
*ipvs
, int af
, __u16 protocol
,
361 const union nf_inet_addr
*vaddr
, __be16 vport
)
364 struct ip_vs_service
*svc
;
366 /* Check for "full" addressed entries */
367 hash
= ip_vs_svc_hashkey(ipvs
, af
, protocol
, vaddr
, vport
);
369 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[hash
], s_list
) {
371 && ip_vs_addr_equal(af
, &svc
->addr
, vaddr
)
372 && (svc
->port
== vport
)
373 && (svc
->protocol
== protocol
)
374 && (svc
->ipvs
== ipvs
)) {
385 * Get service by {fwmark} in the service table.
387 static inline struct ip_vs_service
*
388 __ip_vs_svc_fwm_find(struct netns_ipvs
*ipvs
, int af
, __u32 fwmark
)
391 struct ip_vs_service
*svc
;
393 /* Check for fwmark addressed entries */
394 hash
= ip_vs_svc_fwm_hashkey(ipvs
, fwmark
);
396 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[hash
], f_list
) {
397 if (svc
->fwmark
== fwmark
&& svc
->af
== af
398 && (svc
->ipvs
== ipvs
)) {
407 /* Find service, called under RCU lock */
408 struct ip_vs_service
*
409 ip_vs_service_find(struct netns_ipvs
*ipvs
, int af
, __u32 fwmark
, __u16 protocol
,
410 const union nf_inet_addr
*vaddr
, __be16 vport
)
412 struct ip_vs_service
*svc
;
415 * Check the table hashed by fwmark first
418 svc
= __ip_vs_svc_fwm_find(ipvs
, af
, fwmark
);
424 * Check the table hashed by <protocol,addr,port>
425 * for "full" addressed entries
427 svc
= __ip_vs_service_find(ipvs
, af
, protocol
, vaddr
, vport
);
429 if (!svc
&& protocol
== IPPROTO_TCP
&&
430 atomic_read(&ipvs
->ftpsvc_counter
) &&
431 (vport
== FTPDATA
|| ntohs(vport
) >= inet_prot_sock(ipvs
->net
))) {
433 * Check if ftp service entry exists, the packet
434 * might belong to FTP data connections.
436 svc
= __ip_vs_service_find(ipvs
, af
, protocol
, vaddr
, FTPPORT
);
440 && atomic_read(&ipvs
->nullsvc_counter
)) {
442 * Check if the catch-all port (port zero) exists
444 svc
= __ip_vs_service_find(ipvs
, af
, protocol
, vaddr
, 0);
448 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
449 fwmark
, ip_vs_proto_name(protocol
),
450 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
451 svc
? "hit" : "not hit");
458 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
460 atomic_inc(&svc
->refcnt
);
461 rcu_assign_pointer(dest
->svc
, svc
);
464 static void ip_vs_service_free(struct ip_vs_service
*svc
)
466 free_percpu(svc
->stats
.cpustats
);
470 static void ip_vs_service_rcu_free(struct rcu_head
*head
)
472 struct ip_vs_service
*svc
;
474 svc
= container_of(head
, struct ip_vs_service
, rcu_head
);
475 ip_vs_service_free(svc
);
478 static void __ip_vs_svc_put(struct ip_vs_service
*svc
, bool do_delay
)
480 if (atomic_dec_and_test(&svc
->refcnt
)) {
481 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
483 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
486 call_rcu(&svc
->rcu_head
, ip_vs_service_rcu_free
);
488 ip_vs_service_free(svc
);
494 * Returns hash value for real service
496 static inline unsigned int ip_vs_rs_hashkey(int af
,
497 const union nf_inet_addr
*addr
,
500 register unsigned int porth
= ntohs(port
);
501 __be32 addr_fold
= addr
->ip
;
503 #ifdef CONFIG_IP_VS_IPV6
505 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
506 addr
->ip6
[2]^addr
->ip6
[3];
509 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
513 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
514 static void ip_vs_rs_hash(struct netns_ipvs
*ipvs
, struct ip_vs_dest
*dest
)
518 if (dest
->in_rs_table
)
522 * Hash by proto,addr,port,
523 * which are the parameters of the real service.
525 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
527 hlist_add_head_rcu(&dest
->d_list
, &ipvs
->rs_table
[hash
]);
528 dest
->in_rs_table
= 1;
531 /* Unhash ip_vs_dest from rs_table. */
532 static void ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
535 * Remove it from the rs_table table.
537 if (dest
->in_rs_table
) {
538 hlist_del_rcu(&dest
->d_list
);
539 dest
->in_rs_table
= 0;
543 /* Check if real service by <proto,addr,port> is present */
544 bool ip_vs_has_real_service(struct netns_ipvs
*ipvs
, int af
, __u16 protocol
,
545 const union nf_inet_addr
*daddr
, __be16 dport
)
548 struct ip_vs_dest
*dest
;
550 /* Check for "full" addressed entries */
551 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
553 hlist_for_each_entry_rcu(dest
, &ipvs
->rs_table
[hash
], d_list
) {
554 if (dest
->port
== dport
&&
556 ip_vs_addr_equal(af
, &dest
->addr
, daddr
) &&
557 (dest
->protocol
== protocol
|| dest
->vfwmark
)) {
566 /* Find real service record by <proto,addr,port>.
567 * In case of multiple records with the same <proto,addr,port>, only
568 * the first found record is returned.
570 * To be called under RCU lock.
572 struct ip_vs_dest
*ip_vs_find_real_service(struct netns_ipvs
*ipvs
, int af
,
574 const union nf_inet_addr
*daddr
,
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
)) {
596 /* Lookup destination by {addr,port} in the given service
597 * Called under RCU lock.
599 static struct ip_vs_dest
*
600 ip_vs_lookup_dest(struct ip_vs_service
*svc
, int dest_af
,
601 const union nf_inet_addr
*daddr
, __be16 dport
)
603 struct ip_vs_dest
*dest
;
606 * Find the destination for the given service
608 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
609 if ((dest
->af
== dest_af
) &&
610 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
611 (dest
->port
== dport
)) {
621 * Find destination by {daddr,dport,vaddr,protocol}
622 * Created to be used in ip_vs_process_message() in
623 * the backup synchronization daemon. It finds the
624 * destination to be bound to the received connection
626 * Called under RCU lock, no refcnt is returned.
628 struct ip_vs_dest
*ip_vs_find_dest(struct netns_ipvs
*ipvs
, int svc_af
, int dest_af
,
629 const union nf_inet_addr
*daddr
,
631 const union nf_inet_addr
*vaddr
,
632 __be16 vport
, __u16 protocol
, __u32 fwmark
,
635 struct ip_vs_dest
*dest
;
636 struct ip_vs_service
*svc
;
639 svc
= ip_vs_service_find(ipvs
, svc_af
, fwmark
, protocol
, vaddr
, vport
);
642 if (fwmark
&& (flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
)
644 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
);
646 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
^ dport
);
650 void ip_vs_dest_dst_rcu_free(struct rcu_head
*head
)
652 struct ip_vs_dest_dst
*dest_dst
= container_of(head
,
653 struct ip_vs_dest_dst
,
656 dst_release(dest_dst
->dst_cache
);
660 /* Release dest_dst and dst_cache for dest in user context */
661 static void __ip_vs_dst_cache_reset(struct ip_vs_dest
*dest
)
663 struct ip_vs_dest_dst
*old
;
665 old
= rcu_dereference_protected(dest
->dest_dst
, 1);
667 RCU_INIT_POINTER(dest
->dest_dst
, NULL
);
668 call_rcu(&old
->rcu_head
, ip_vs_dest_dst_rcu_free
);
673 * Lookup dest by {svc,addr,port} in the destination trash.
674 * The destination trash is used to hold the destinations that are removed
675 * from the service table but are still referenced by some conn entries.
676 * The reason to add the destination trash is when the dest is temporary
677 * down (either by administrator or by monitor program), the dest can be
678 * picked back from the trash, the remaining connections to the dest can
679 * continue, and the counting information of the dest is also useful for
682 static struct ip_vs_dest
*
683 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, int dest_af
,
684 const union nf_inet_addr
*daddr
, __be16 dport
)
686 struct ip_vs_dest
*dest
;
687 struct netns_ipvs
*ipvs
= svc
->ipvs
;
690 * Find the destination in trash
692 spin_lock_bh(&ipvs
->dest_trash_lock
);
693 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
694 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
697 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
699 refcount_read(&dest
->refcnt
));
700 if (dest
->af
== dest_af
&&
701 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
702 dest
->port
== dport
&&
703 dest
->vfwmark
== svc
->fwmark
&&
704 dest
->protocol
== svc
->protocol
&&
706 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
707 dest
->vport
== svc
->port
))) {
709 list_del(&dest
->t_list
);
717 spin_unlock_bh(&ipvs
->dest_trash_lock
);
722 static void ip_vs_dest_free(struct ip_vs_dest
*dest
)
724 struct ip_vs_service
*svc
= rcu_dereference_protected(dest
->svc
, 1);
726 __ip_vs_dst_cache_reset(dest
);
727 __ip_vs_svc_put(svc
, false);
728 free_percpu(dest
->stats
.cpustats
);
729 ip_vs_dest_put_and_free(dest
);
733 * Clean up all the destinations in the trash
734 * Called by the ip_vs_control_cleanup()
736 * When the ip_vs_control_clearup is activated by ipvs module exit,
737 * the service tables must have been flushed and all the connections
738 * are expired, and the refcnt of each destination in the trash must
739 * be 1, so we simply release them here.
741 static void ip_vs_trash_cleanup(struct netns_ipvs
*ipvs
)
743 struct ip_vs_dest
*dest
, *nxt
;
745 del_timer_sync(&ipvs
->dest_trash_timer
);
746 /* No need to use dest_trash_lock */
747 list_for_each_entry_safe(dest
, nxt
, &ipvs
->dest_trash
, t_list
) {
748 list_del(&dest
->t_list
);
749 ip_vs_dest_free(dest
);
754 ip_vs_copy_stats(struct ip_vs_kstats
*dst
, struct ip_vs_stats
*src
)
756 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
758 spin_lock_bh(&src
->lock
);
760 IP_VS_SHOW_STATS_COUNTER(conns
);
761 IP_VS_SHOW_STATS_COUNTER(inpkts
);
762 IP_VS_SHOW_STATS_COUNTER(outpkts
);
763 IP_VS_SHOW_STATS_COUNTER(inbytes
);
764 IP_VS_SHOW_STATS_COUNTER(outbytes
);
766 ip_vs_read_estimator(dst
, src
);
768 spin_unlock_bh(&src
->lock
);
772 ip_vs_export_stats_user(struct ip_vs_stats_user
*dst
, struct ip_vs_kstats
*src
)
774 dst
->conns
= (u32
)src
->conns
;
775 dst
->inpkts
= (u32
)src
->inpkts
;
776 dst
->outpkts
= (u32
)src
->outpkts
;
777 dst
->inbytes
= src
->inbytes
;
778 dst
->outbytes
= src
->outbytes
;
779 dst
->cps
= (u32
)src
->cps
;
780 dst
->inpps
= (u32
)src
->inpps
;
781 dst
->outpps
= (u32
)src
->outpps
;
782 dst
->inbps
= (u32
)src
->inbps
;
783 dst
->outbps
= (u32
)src
->outbps
;
787 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
789 spin_lock_bh(&stats
->lock
);
791 /* get current counters as zero point, rates are zeroed */
793 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
795 IP_VS_ZERO_STATS_COUNTER(conns
);
796 IP_VS_ZERO_STATS_COUNTER(inpkts
);
797 IP_VS_ZERO_STATS_COUNTER(outpkts
);
798 IP_VS_ZERO_STATS_COUNTER(inbytes
);
799 IP_VS_ZERO_STATS_COUNTER(outbytes
);
801 ip_vs_zero_estimator(stats
);
803 spin_unlock_bh(&stats
->lock
);
807 * Update a destination in the given service
810 __ip_vs_update_dest(struct ip_vs_service
*svc
, struct ip_vs_dest
*dest
,
811 struct ip_vs_dest_user_kern
*udest
, int add
)
813 struct netns_ipvs
*ipvs
= svc
->ipvs
;
814 struct ip_vs_service
*old_svc
;
815 struct ip_vs_scheduler
*sched
;
818 /* We cannot modify an address and change the address family */
819 BUG_ON(!add
&& udest
->af
!= dest
->af
);
821 if (add
&& udest
->af
!= svc
->af
)
822 ipvs
->mixed_address_family_dests
++;
824 /* set the weight and the flags */
825 atomic_set(&dest
->weight
, udest
->weight
);
826 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
827 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
829 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
830 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
831 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
834 * Put the real service in rs_table if not present.
835 * For now only for NAT!
837 ip_vs_rs_hash(ipvs
, dest
);
839 atomic_set(&dest
->conn_flags
, conn_flags
);
841 /* bind the service */
842 old_svc
= rcu_dereference_protected(dest
->svc
, 1);
844 __ip_vs_bind_svc(dest
, svc
);
846 if (old_svc
!= svc
) {
847 ip_vs_zero_stats(&dest
->stats
);
848 __ip_vs_bind_svc(dest
, svc
);
849 __ip_vs_svc_put(old_svc
, true);
853 /* set the dest status flags */
854 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
856 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
857 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
858 dest
->u_threshold
= udest
->u_threshold
;
859 dest
->l_threshold
= udest
->l_threshold
;
861 dest
->af
= udest
->af
;
863 spin_lock_bh(&dest
->dst_lock
);
864 __ip_vs_dst_cache_reset(dest
);
865 spin_unlock_bh(&dest
->dst_lock
);
868 ip_vs_start_estimator(svc
->ipvs
, &dest
->stats
);
869 list_add_rcu(&dest
->n_list
, &svc
->destinations
);
871 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
872 if (sched
&& sched
->add_dest
)
873 sched
->add_dest(svc
, dest
);
875 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
876 if (sched
&& sched
->upd_dest
)
877 sched
->upd_dest(svc
, dest
);
883 * Create a destination for the given service
886 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
887 struct ip_vs_dest
**dest_p
)
889 struct ip_vs_dest
*dest
;
890 unsigned int atype
, i
;
894 #ifdef CONFIG_IP_VS_IPV6
895 if (udest
->af
== AF_INET6
) {
898 atype
= ipv6_addr_type(&udest
->addr
.in6
);
899 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
900 atype
& IPV6_ADDR_LINKLOCAL
) &&
901 !__ip_vs_addr_is_local_v6(svc
->ipvs
->net
, &udest
->addr
.in6
))
904 ret
= nf_defrag_ipv6_enable(svc
->ipvs
->net
);
910 atype
= inet_addr_type(svc
->ipvs
->net
, udest
->addr
.ip
);
911 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
915 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
919 dest
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
920 if (!dest
->stats
.cpustats
)
923 for_each_possible_cpu(i
) {
924 struct ip_vs_cpu_stats
*ip_vs_dest_stats
;
925 ip_vs_dest_stats
= per_cpu_ptr(dest
->stats
.cpustats
, i
);
926 u64_stats_init(&ip_vs_dest_stats
->syncp
);
929 dest
->af
= udest
->af
;
930 dest
->protocol
= svc
->protocol
;
931 dest
->vaddr
= svc
->addr
;
932 dest
->vport
= svc
->port
;
933 dest
->vfwmark
= svc
->fwmark
;
934 ip_vs_addr_copy(udest
->af
, &dest
->addr
, &udest
->addr
);
935 dest
->port
= udest
->port
;
937 atomic_set(&dest
->activeconns
, 0);
938 atomic_set(&dest
->inactconns
, 0);
939 atomic_set(&dest
->persistconns
, 0);
940 refcount_set(&dest
->refcnt
, 1);
942 INIT_HLIST_NODE(&dest
->d_list
);
943 spin_lock_init(&dest
->dst_lock
);
944 spin_lock_init(&dest
->stats
.lock
);
945 __ip_vs_update_dest(svc
, dest
, udest
, 1);
959 * Add a destination into an existing service
962 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
964 struct ip_vs_dest
*dest
;
965 union nf_inet_addr daddr
;
966 __be16 dport
= udest
->port
;
971 if (udest
->weight
< 0) {
972 pr_err("%s(): server weight less than zero\n", __func__
);
976 if (udest
->l_threshold
> udest
->u_threshold
) {
977 pr_err("%s(): lower threshold is higher than upper threshold\n",
982 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
984 /* We use function that requires RCU lock */
986 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
990 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
995 * Check if the dest already exists in the trash and
996 * is from the same service
998 dest
= ip_vs_trash_get_dest(svc
, udest
->af
, &daddr
, dport
);
1001 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
1002 "dest->refcnt=%d, service %u/%s:%u\n",
1003 IP_VS_DBG_ADDR(udest
->af
, &daddr
), ntohs(dport
),
1004 refcount_read(&dest
->refcnt
),
1006 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
1007 ntohs(dest
->vport
));
1009 __ip_vs_update_dest(svc
, dest
, udest
, 1);
1013 * Allocate and initialize the dest structure
1015 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
1024 * Edit a destination in the given service
1027 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1029 struct ip_vs_dest
*dest
;
1030 union nf_inet_addr daddr
;
1031 __be16 dport
= udest
->port
;
1035 if (udest
->weight
< 0) {
1036 pr_err("%s(): server weight less than zero\n", __func__
);
1040 if (udest
->l_threshold
> udest
->u_threshold
) {
1041 pr_err("%s(): lower threshold is higher than upper threshold\n",
1046 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
1048 /* We use function that requires RCU lock */
1050 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
1054 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1058 __ip_vs_update_dest(svc
, dest
, udest
, 0);
1065 * Delete a destination (must be already unlinked from the service)
1067 static void __ip_vs_del_dest(struct netns_ipvs
*ipvs
, struct ip_vs_dest
*dest
,
1070 ip_vs_stop_estimator(ipvs
, &dest
->stats
);
1073 * Remove it from the d-linked list with the real services.
1075 ip_vs_rs_unhash(dest
);
1077 spin_lock_bh(&ipvs
->dest_trash_lock
);
1078 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1079 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
), ntohs(dest
->port
),
1080 refcount_read(&dest
->refcnt
));
1081 if (list_empty(&ipvs
->dest_trash
) && !cleanup
)
1082 mod_timer(&ipvs
->dest_trash_timer
,
1083 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1084 /* dest lives in trash with reference */
1085 list_add(&dest
->t_list
, &ipvs
->dest_trash
);
1086 dest
->idle_start
= 0;
1087 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1092 * Unlink a destination from the given service
1094 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1095 struct ip_vs_dest
*dest
,
1098 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1101 * Remove it from the d-linked destination list.
1103 list_del_rcu(&dest
->n_list
);
1106 if (dest
->af
!= svc
->af
)
1107 svc
->ipvs
->mixed_address_family_dests
--;
1110 struct ip_vs_scheduler
*sched
;
1112 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1113 if (sched
&& sched
->del_dest
)
1114 sched
->del_dest(svc
, dest
);
1120 * Delete a destination server in the given service
1123 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1125 struct ip_vs_dest
*dest
;
1126 __be16 dport
= udest
->port
;
1130 /* We use function that requires RCU lock */
1132 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &udest
->addr
, dport
);
1136 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1141 * Unlink dest from the service
1143 __ip_vs_unlink_dest(svc
, dest
, 1);
1146 * Delete the destination
1148 __ip_vs_del_dest(svc
->ipvs
, dest
, false);
1155 static void ip_vs_dest_trash_expire(struct timer_list
*t
)
1157 struct netns_ipvs
*ipvs
= from_timer(ipvs
, t
, dest_trash_timer
);
1158 struct ip_vs_dest
*dest
, *next
;
1159 unsigned long now
= jiffies
;
1161 spin_lock(&ipvs
->dest_trash_lock
);
1162 list_for_each_entry_safe(dest
, next
, &ipvs
->dest_trash
, t_list
) {
1163 if (refcount_read(&dest
->refcnt
) > 1)
1165 if (dest
->idle_start
) {
1166 if (time_before(now
, dest
->idle_start
+
1167 IP_VS_DEST_TRASH_PERIOD
))
1170 dest
->idle_start
= max(1UL, now
);
1173 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1175 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1177 list_del(&dest
->t_list
);
1178 ip_vs_dest_free(dest
);
1180 if (!list_empty(&ipvs
->dest_trash
))
1181 mod_timer(&ipvs
->dest_trash_timer
,
1182 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1183 spin_unlock(&ipvs
->dest_trash_lock
);
1187 * Add a service into the service hash table
1190 ip_vs_add_service(struct netns_ipvs
*ipvs
, struct ip_vs_service_user_kern
*u
,
1191 struct ip_vs_service
**svc_p
)
1194 struct ip_vs_scheduler
*sched
= NULL
;
1195 struct ip_vs_pe
*pe
= NULL
;
1196 struct ip_vs_service
*svc
= NULL
;
1198 /* increase the module use count */
1199 if (!ip_vs_use_count_inc())
1200 return -ENOPROTOOPT
;
1202 /* Lookup the scheduler by 'u->sched_name' */
1203 if (strcmp(u
->sched_name
, "none")) {
1204 sched
= ip_vs_scheduler_get(u
->sched_name
);
1206 pr_info("Scheduler module ip_vs_%s not found\n",
1213 if (u
->pe_name
&& *u
->pe_name
) {
1214 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1216 pr_info("persistence engine module ip_vs_pe_%s "
1217 "not found\n", u
->pe_name
);
1223 #ifdef CONFIG_IP_VS_IPV6
1224 if (u
->af
== AF_INET6
) {
1225 __u32 plen
= (__force __u32
) u
->netmask
;
1227 if (plen
< 1 || plen
> 128) {
1232 ret
= nf_defrag_ipv6_enable(ipvs
->net
);
1238 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1240 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1244 svc
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
1245 if (!svc
->stats
.cpustats
) {
1250 for_each_possible_cpu(i
) {
1251 struct ip_vs_cpu_stats
*ip_vs_stats
;
1252 ip_vs_stats
= per_cpu_ptr(svc
->stats
.cpustats
, i
);
1253 u64_stats_init(&ip_vs_stats
->syncp
);
1257 /* I'm the first user of the service */
1258 atomic_set(&svc
->refcnt
, 0);
1261 svc
->protocol
= u
->protocol
;
1262 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1263 svc
->port
= u
->port
;
1264 svc
->fwmark
= u
->fwmark
;
1265 svc
->flags
= u
->flags
;
1266 svc
->timeout
= u
->timeout
* HZ
;
1267 svc
->netmask
= u
->netmask
;
1270 INIT_LIST_HEAD(&svc
->destinations
);
1271 spin_lock_init(&svc
->sched_lock
);
1272 spin_lock_init(&svc
->stats
.lock
);
1274 /* Bind the scheduler */
1276 ret
= ip_vs_bind_scheduler(svc
, sched
);
1282 /* Bind the ct retriever */
1283 RCU_INIT_POINTER(svc
->pe
, pe
);
1286 /* Update the virtual service counters */
1287 if (svc
->port
== FTPPORT
)
1288 atomic_inc(&ipvs
->ftpsvc_counter
);
1289 else if (svc
->port
== 0)
1290 atomic_inc(&ipvs
->nullsvc_counter
);
1291 if (svc
->pe
&& svc
->pe
->conn_out
)
1292 atomic_inc(&ipvs
->conn_out_counter
);
1294 ip_vs_start_estimator(ipvs
, &svc
->stats
);
1296 /* Count only IPv4 services for old get/setsockopt interface */
1297 if (svc
->af
== AF_INET
)
1298 ipvs
->num_services
++;
1300 /* Hash the service into the service table */
1301 ip_vs_svc_hash(svc
);
1304 /* Now there is a service - full throttle */
1311 ip_vs_unbind_scheduler(svc
, sched
);
1312 ip_vs_service_free(svc
);
1314 ip_vs_scheduler_put(sched
);
1317 /* decrease the module use count */
1318 ip_vs_use_count_dec();
1325 * Edit a service and bind it with a new scheduler
1328 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1330 struct ip_vs_scheduler
*sched
= NULL
, *old_sched
;
1331 struct ip_vs_pe
*pe
= NULL
, *old_pe
= NULL
;
1333 bool new_pe_conn_out
, old_pe_conn_out
;
1336 * Lookup the scheduler, by 'u->sched_name'
1338 if (strcmp(u
->sched_name
, "none")) {
1339 sched
= ip_vs_scheduler_get(u
->sched_name
);
1341 pr_info("Scheduler module ip_vs_%s not found\n",
1348 if (u
->pe_name
&& *u
->pe_name
) {
1349 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1351 pr_info("persistence engine module ip_vs_pe_%s "
1352 "not found\n", u
->pe_name
);
1359 #ifdef CONFIG_IP_VS_IPV6
1360 if (u
->af
== AF_INET6
) {
1361 __u32 plen
= (__force __u32
) u
->netmask
;
1363 if (plen
< 1 || plen
> 128) {
1370 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1371 if (sched
!= old_sched
) {
1373 ip_vs_unbind_scheduler(svc
, old_sched
);
1374 RCU_INIT_POINTER(svc
->scheduler
, NULL
);
1375 /* Wait all svc->sched_data users */
1378 /* Bind the new scheduler */
1380 ret
= ip_vs_bind_scheduler(svc
, sched
);
1382 ip_vs_scheduler_put(sched
);
1389 * Set the flags and timeout value
1391 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1392 svc
->timeout
= u
->timeout
* HZ
;
1393 svc
->netmask
= u
->netmask
;
1395 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1397 rcu_assign_pointer(svc
->pe
, pe
);
1398 /* check for optional methods in new pe */
1399 new_pe_conn_out
= (pe
&& pe
->conn_out
) ? true : false;
1400 old_pe_conn_out
= (old_pe
&& old_pe
->conn_out
) ? true : false;
1401 if (new_pe_conn_out
&& !old_pe_conn_out
)
1402 atomic_inc(&svc
->ipvs
->conn_out_counter
);
1403 if (old_pe_conn_out
&& !new_pe_conn_out
)
1404 atomic_dec(&svc
->ipvs
->conn_out_counter
);
1408 ip_vs_scheduler_put(old_sched
);
1409 ip_vs_pe_put(old_pe
);
1414 * Delete a service from the service list
1415 * - The service must be unlinked, unlocked and not referenced!
1416 * - We are called under _bh lock
1418 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
)
1420 struct ip_vs_dest
*dest
, *nxt
;
1421 struct ip_vs_scheduler
*old_sched
;
1422 struct ip_vs_pe
*old_pe
;
1423 struct netns_ipvs
*ipvs
= svc
->ipvs
;
1425 /* Count only IPv4 services for old get/setsockopt interface */
1426 if (svc
->af
== AF_INET
)
1427 ipvs
->num_services
--;
1429 ip_vs_stop_estimator(svc
->ipvs
, &svc
->stats
);
1431 /* Unbind scheduler */
1432 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1433 ip_vs_unbind_scheduler(svc
, old_sched
);
1434 ip_vs_scheduler_put(old_sched
);
1436 /* Unbind persistence engine, keep svc->pe */
1437 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1438 if (old_pe
&& old_pe
->conn_out
)
1439 atomic_dec(&ipvs
->conn_out_counter
);
1440 ip_vs_pe_put(old_pe
);
1443 * Unlink the whole destination list
1445 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1446 __ip_vs_unlink_dest(svc
, dest
, 0);
1447 __ip_vs_del_dest(svc
->ipvs
, dest
, cleanup
);
1451 * Update the virtual service counters
1453 if (svc
->port
== FTPPORT
)
1454 atomic_dec(&ipvs
->ftpsvc_counter
);
1455 else if (svc
->port
== 0)
1456 atomic_dec(&ipvs
->nullsvc_counter
);
1459 * Free the service if nobody refers to it
1461 __ip_vs_svc_put(svc
, true);
1463 /* decrease the module use count */
1464 ip_vs_use_count_dec();
1468 * Unlink a service from list and try to delete it if its refcnt reached 0
1470 static void ip_vs_unlink_service(struct ip_vs_service
*svc
, bool cleanup
)
1472 /* Hold svc to avoid double release from dest_trash */
1473 atomic_inc(&svc
->refcnt
);
1475 * Unhash it from the service table
1477 ip_vs_svc_unhash(svc
);
1479 __ip_vs_del_service(svc
, cleanup
);
1483 * Delete a service from the service list
1485 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1489 ip_vs_unlink_service(svc
, false);
1496 * Flush all the virtual services
1498 static int ip_vs_flush(struct netns_ipvs
*ipvs
, bool cleanup
)
1501 struct ip_vs_service
*svc
;
1502 struct hlist_node
*n
;
1505 * Flush the service table hashed by <netns,protocol,addr,port>
1507 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1508 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_table
[idx
],
1510 if (svc
->ipvs
== ipvs
)
1511 ip_vs_unlink_service(svc
, cleanup
);
1516 * Flush the service table hashed by fwmark
1518 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1519 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_fwm_table
[idx
],
1521 if (svc
->ipvs
== ipvs
)
1522 ip_vs_unlink_service(svc
, cleanup
);
1530 * Delete service by {netns} in the service table.
1531 * Called by __ip_vs_cleanup()
1533 void ip_vs_service_net_cleanup(struct netns_ipvs
*ipvs
)
1536 /* Check for "full" addressed entries */
1537 mutex_lock(&__ip_vs_mutex
);
1538 ip_vs_flush(ipvs
, true);
1539 mutex_unlock(&__ip_vs_mutex
);
1543 /* Put all references for device (dst_cache) */
1545 ip_vs_forget_dev(struct ip_vs_dest
*dest
, struct net_device
*dev
)
1547 struct ip_vs_dest_dst
*dest_dst
;
1549 spin_lock_bh(&dest
->dst_lock
);
1550 dest_dst
= rcu_dereference_protected(dest
->dest_dst
, 1);
1551 if (dest_dst
&& dest_dst
->dst_cache
->dev
== dev
) {
1552 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1554 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1556 refcount_read(&dest
->refcnt
));
1557 __ip_vs_dst_cache_reset(dest
);
1559 spin_unlock_bh(&dest
->dst_lock
);
1562 /* Netdev event receiver
1563 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1565 static int ip_vs_dst_event(struct notifier_block
*this, unsigned long event
,
1568 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1569 struct net
*net
= dev_net(dev
);
1570 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1571 struct ip_vs_service
*svc
;
1572 struct ip_vs_dest
*dest
;
1575 if (event
!= NETDEV_DOWN
|| !ipvs
)
1577 IP_VS_DBG(3, "%s() dev=%s\n", __func__
, dev
->name
);
1579 mutex_lock(&__ip_vs_mutex
);
1580 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1581 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1582 if (svc
->ipvs
== ipvs
) {
1583 list_for_each_entry(dest
, &svc
->destinations
,
1585 ip_vs_forget_dev(dest
, dev
);
1590 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1591 if (svc
->ipvs
== ipvs
) {
1592 list_for_each_entry(dest
, &svc
->destinations
,
1594 ip_vs_forget_dev(dest
, dev
);
1601 spin_lock_bh(&ipvs
->dest_trash_lock
);
1602 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
1603 ip_vs_forget_dev(dest
, dev
);
1605 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1606 mutex_unlock(&__ip_vs_mutex
);
1612 * Zero counters in a service or all services
1614 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1616 struct ip_vs_dest
*dest
;
1618 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1619 ip_vs_zero_stats(&dest
->stats
);
1621 ip_vs_zero_stats(&svc
->stats
);
1625 static int ip_vs_zero_all(struct netns_ipvs
*ipvs
)
1628 struct ip_vs_service
*svc
;
1630 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1631 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1632 if (svc
->ipvs
== ipvs
)
1633 ip_vs_zero_service(svc
);
1637 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1638 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1639 if (svc
->ipvs
== ipvs
)
1640 ip_vs_zero_service(svc
);
1644 ip_vs_zero_stats(&ipvs
->tot_stats
);
1648 #ifdef CONFIG_SYSCTL
1651 static int three
= 3;
1654 proc_do_defense_mode(struct ctl_table
*table
, int write
,
1655 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1657 struct netns_ipvs
*ipvs
= table
->extra2
;
1658 int *valp
= table
->data
;
1662 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1663 if (write
&& (*valp
!= val
)) {
1664 if ((*valp
< 0) || (*valp
> 3)) {
1665 /* Restore the correct value */
1668 update_defense_level(ipvs
);
1675 proc_do_sync_threshold(struct ctl_table
*table
, int write
,
1676 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1678 int *valp
= table
->data
;
1682 /* backup the value first */
1683 memcpy(val
, valp
, sizeof(val
));
1685 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1686 if (write
&& (valp
[0] < 0 || valp
[1] < 0 ||
1687 (valp
[0] >= valp
[1] && valp
[1]))) {
1688 /* Restore the correct value */
1689 memcpy(valp
, val
, sizeof(val
));
1695 proc_do_sync_mode(struct ctl_table
*table
, int write
,
1696 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1698 int *valp
= table
->data
;
1702 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1703 if (write
&& (*valp
!= val
)) {
1704 if ((*valp
< 0) || (*valp
> 1)) {
1705 /* Restore the correct value */
1713 proc_do_sync_ports(struct ctl_table
*table
, int write
,
1714 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1716 int *valp
= table
->data
;
1720 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1721 if (write
&& (*valp
!= val
)) {
1722 if (*valp
< 1 || !is_power_of_2(*valp
)) {
1723 /* Restore the correct value */
1731 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1732 * Do not change order or insert new entries without
1733 * align with netns init in ip_vs_control_net_init()
1736 static struct ctl_table vs_vars
[] = {
1738 .procname
= "amemthresh",
1739 .maxlen
= sizeof(int),
1741 .proc_handler
= proc_dointvec
,
1744 .procname
= "am_droprate",
1745 .maxlen
= sizeof(int),
1747 .proc_handler
= proc_dointvec
,
1750 .procname
= "drop_entry",
1751 .maxlen
= sizeof(int),
1753 .proc_handler
= proc_do_defense_mode
,
1756 .procname
= "drop_packet",
1757 .maxlen
= sizeof(int),
1759 .proc_handler
= proc_do_defense_mode
,
1761 #ifdef CONFIG_IP_VS_NFCT
1763 .procname
= "conntrack",
1764 .maxlen
= sizeof(int),
1766 .proc_handler
= &proc_dointvec
,
1770 .procname
= "secure_tcp",
1771 .maxlen
= sizeof(int),
1773 .proc_handler
= proc_do_defense_mode
,
1776 .procname
= "snat_reroute",
1777 .maxlen
= sizeof(int),
1779 .proc_handler
= &proc_dointvec
,
1782 .procname
= "sync_version",
1783 .maxlen
= sizeof(int),
1785 .proc_handler
= proc_do_sync_mode
,
1788 .procname
= "sync_ports",
1789 .maxlen
= sizeof(int),
1791 .proc_handler
= proc_do_sync_ports
,
1794 .procname
= "sync_persist_mode",
1795 .maxlen
= sizeof(int),
1797 .proc_handler
= proc_dointvec
,
1800 .procname
= "sync_qlen_max",
1801 .maxlen
= sizeof(unsigned long),
1803 .proc_handler
= proc_doulongvec_minmax
,
1806 .procname
= "sync_sock_size",
1807 .maxlen
= sizeof(int),
1809 .proc_handler
= proc_dointvec
,
1812 .procname
= "cache_bypass",
1813 .maxlen
= sizeof(int),
1815 .proc_handler
= proc_dointvec
,
1818 .procname
= "expire_nodest_conn",
1819 .maxlen
= sizeof(int),
1821 .proc_handler
= proc_dointvec
,
1824 .procname
= "sloppy_tcp",
1825 .maxlen
= sizeof(int),
1827 .proc_handler
= proc_dointvec
,
1830 .procname
= "sloppy_sctp",
1831 .maxlen
= sizeof(int),
1833 .proc_handler
= proc_dointvec
,
1836 .procname
= "expire_quiescent_template",
1837 .maxlen
= sizeof(int),
1839 .proc_handler
= proc_dointvec
,
1842 .procname
= "sync_threshold",
1844 sizeof(((struct netns_ipvs
*)0)->sysctl_sync_threshold
),
1846 .proc_handler
= proc_do_sync_threshold
,
1849 .procname
= "sync_refresh_period",
1850 .maxlen
= sizeof(int),
1852 .proc_handler
= proc_dointvec_jiffies
,
1855 .procname
= "sync_retries",
1856 .maxlen
= sizeof(int),
1858 .proc_handler
= proc_dointvec_minmax
,
1863 .procname
= "nat_icmp_send",
1864 .maxlen
= sizeof(int),
1866 .proc_handler
= proc_dointvec
,
1869 .procname
= "pmtu_disc",
1870 .maxlen
= sizeof(int),
1872 .proc_handler
= proc_dointvec
,
1875 .procname
= "backup_only",
1876 .maxlen
= sizeof(int),
1878 .proc_handler
= proc_dointvec
,
1881 .procname
= "conn_reuse_mode",
1882 .maxlen
= sizeof(int),
1884 .proc_handler
= proc_dointvec
,
1887 .procname
= "schedule_icmp",
1888 .maxlen
= sizeof(int),
1890 .proc_handler
= proc_dointvec
,
1893 .procname
= "ignore_tunneled",
1894 .maxlen
= sizeof(int),
1896 .proc_handler
= proc_dointvec
,
1898 #ifdef CONFIG_IP_VS_DEBUG
1900 .procname
= "debug_level",
1901 .data
= &sysctl_ip_vs_debug_level
,
1902 .maxlen
= sizeof(int),
1904 .proc_handler
= proc_dointvec
,
1912 #ifdef CONFIG_PROC_FS
1915 struct seq_net_private p
; /* Do not move this, netns depends upon it*/
1916 struct hlist_head
*table
;
1921 * Write the contents of the VS rule table to a PROCfs file.
1922 * (It is kept just for backward compatibility)
1924 static inline const char *ip_vs_fwd_name(unsigned int flags
)
1926 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1927 case IP_VS_CONN_F_LOCALNODE
:
1929 case IP_VS_CONN_F_TUNNEL
:
1931 case IP_VS_CONN_F_DROUTE
:
1939 /* Get the Nth entry in the two lists */
1940 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1942 struct net
*net
= seq_file_net(seq
);
1943 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1944 struct ip_vs_iter
*iter
= seq
->private;
1946 struct ip_vs_service
*svc
;
1948 /* look in hash by protocol */
1949 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1950 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1951 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1952 iter
->table
= ip_vs_svc_table
;
1959 /* keep looking in fwmark */
1960 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1961 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[idx
],
1963 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1964 iter
->table
= ip_vs_svc_fwm_table
;
1974 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1978 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1982 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1984 struct hlist_node
*e
;
1985 struct ip_vs_iter
*iter
;
1986 struct ip_vs_service
*svc
;
1989 if (v
== SEQ_START_TOKEN
)
1990 return ip_vs_info_array(seq
,0);
1993 iter
= seq
->private;
1995 if (iter
->table
== ip_vs_svc_table
) {
1996 /* next service in table hashed by protocol */
1997 e
= rcu_dereference(hlist_next_rcu(&svc
->s_list
));
1999 return hlist_entry(e
, struct ip_vs_service
, s_list
);
2001 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
2002 hlist_for_each_entry_rcu(svc
,
2003 &ip_vs_svc_table
[iter
->bucket
],
2009 iter
->table
= ip_vs_svc_fwm_table
;
2014 /* next service in hashed by fwmark */
2015 e
= rcu_dereference(hlist_next_rcu(&svc
->f_list
));
2017 return hlist_entry(e
, struct ip_vs_service
, f_list
);
2020 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
2021 hlist_for_each_entry_rcu(svc
,
2022 &ip_vs_svc_fwm_table
[iter
->bucket
],
2030 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
2037 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
2039 if (v
== SEQ_START_TOKEN
) {
2041 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2042 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2044 "Prot LocalAddress:Port Scheduler Flags\n");
2046 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2048 struct net
*net
= seq_file_net(seq
);
2049 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2050 const struct ip_vs_service
*svc
= v
;
2051 const struct ip_vs_iter
*iter
= seq
->private;
2052 const struct ip_vs_dest
*dest
;
2053 struct ip_vs_scheduler
*sched
= rcu_dereference(svc
->scheduler
);
2054 char *sched_name
= sched
? sched
->name
: "none";
2056 if (svc
->ipvs
!= ipvs
)
2058 if (iter
->table
== ip_vs_svc_table
) {
2059 #ifdef CONFIG_IP_VS_IPV6
2060 if (svc
->af
== AF_INET6
)
2061 seq_printf(seq
, "%s [%pI6]:%04X %s ",
2062 ip_vs_proto_name(svc
->protocol
),
2068 seq_printf(seq
, "%s %08X:%04X %s %s ",
2069 ip_vs_proto_name(svc
->protocol
),
2070 ntohl(svc
->addr
.ip
),
2073 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2075 seq_printf(seq
, "FWM %08X %s %s",
2076 svc
->fwmark
, sched_name
,
2077 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2080 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
2081 seq_printf(seq
, "persistent %d %08X\n",
2083 ntohl(svc
->netmask
));
2085 seq_putc(seq
, '\n');
2087 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
2088 #ifdef CONFIG_IP_VS_IPV6
2089 if (dest
->af
== AF_INET6
)
2092 " %-7s %-6d %-10d %-10d\n",
2095 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2096 atomic_read(&dest
->weight
),
2097 atomic_read(&dest
->activeconns
),
2098 atomic_read(&dest
->inactconns
));
2103 "%-7s %-6d %-10d %-10d\n",
2104 ntohl(dest
->addr
.ip
),
2106 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2107 atomic_read(&dest
->weight
),
2108 atomic_read(&dest
->activeconns
),
2109 atomic_read(&dest
->inactconns
));
2116 static const struct seq_operations ip_vs_info_seq_ops
= {
2117 .start
= ip_vs_info_seq_start
,
2118 .next
= ip_vs_info_seq_next
,
2119 .stop
= ip_vs_info_seq_stop
,
2120 .show
= ip_vs_info_seq_show
,
2123 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
2125 return seq_open_net(inode
, file
, &ip_vs_info_seq_ops
,
2126 sizeof(struct ip_vs_iter
));
2129 static const struct file_operations ip_vs_info_fops
= {
2130 .owner
= THIS_MODULE
,
2131 .open
= ip_vs_info_open
,
2133 .llseek
= seq_lseek
,
2134 .release
= seq_release_net
,
2137 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
2139 struct net
*net
= seq_file_single_net(seq
);
2140 struct ip_vs_kstats show
;
2142 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2144 " Total Incoming Outgoing Incoming Outgoing\n");
2146 " Conns Packets Packets Bytes Bytes\n");
2148 ip_vs_copy_stats(&show
, &net_ipvs(net
)->tot_stats
);
2149 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n\n",
2150 (unsigned long long)show
.conns
,
2151 (unsigned long long)show
.inpkts
,
2152 (unsigned long long)show
.outpkts
,
2153 (unsigned long long)show
.inbytes
,
2154 (unsigned long long)show
.outbytes
);
2156 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2158 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2159 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n",
2160 (unsigned long long)show
.cps
,
2161 (unsigned long long)show
.inpps
,
2162 (unsigned long long)show
.outpps
,
2163 (unsigned long long)show
.inbps
,
2164 (unsigned long long)show
.outbps
);
2169 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
2171 return single_open_net(inode
, file
, ip_vs_stats_show
);
2174 static const struct file_operations ip_vs_stats_fops
= {
2175 .owner
= THIS_MODULE
,
2176 .open
= ip_vs_stats_seq_open
,
2178 .llseek
= seq_lseek
,
2179 .release
= single_release_net
,
2182 static int ip_vs_stats_percpu_show(struct seq_file
*seq
, void *v
)
2184 struct net
*net
= seq_file_single_net(seq
);
2185 struct ip_vs_stats
*tot_stats
= &net_ipvs(net
)->tot_stats
;
2186 struct ip_vs_cpu_stats __percpu
*cpustats
= tot_stats
->cpustats
;
2187 struct ip_vs_kstats kstats
;
2190 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2192 " Total Incoming Outgoing Incoming Outgoing\n");
2194 "CPU Conns Packets Packets Bytes Bytes\n");
2196 for_each_possible_cpu(i
) {
2197 struct ip_vs_cpu_stats
*u
= per_cpu_ptr(cpustats
, i
);
2199 u64 conns
, inpkts
, outpkts
, inbytes
, outbytes
;
2202 start
= u64_stats_fetch_begin_irq(&u
->syncp
);
2203 conns
= u
->cnt
.conns
;
2204 inpkts
= u
->cnt
.inpkts
;
2205 outpkts
= u
->cnt
.outpkts
;
2206 inbytes
= u
->cnt
.inbytes
;
2207 outbytes
= u
->cnt
.outbytes
;
2208 } while (u64_stats_fetch_retry_irq(&u
->syncp
, start
));
2210 seq_printf(seq
, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2211 i
, (u64
)conns
, (u64
)inpkts
,
2212 (u64
)outpkts
, (u64
)inbytes
,
2216 ip_vs_copy_stats(&kstats
, tot_stats
);
2218 seq_printf(seq
, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2219 (unsigned long long)kstats
.conns
,
2220 (unsigned long long)kstats
.inpkts
,
2221 (unsigned long long)kstats
.outpkts
,
2222 (unsigned long long)kstats
.inbytes
,
2223 (unsigned long long)kstats
.outbytes
);
2225 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2227 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2228 seq_printf(seq
, " %8LX %8LX %8LX %16LX %16LX\n",
2238 static int ip_vs_stats_percpu_seq_open(struct inode
*inode
, struct file
*file
)
2240 return single_open_net(inode
, file
, ip_vs_stats_percpu_show
);
2243 static const struct file_operations ip_vs_stats_percpu_fops
= {
2244 .owner
= THIS_MODULE
,
2245 .open
= ip_vs_stats_percpu_seq_open
,
2247 .llseek
= seq_lseek
,
2248 .release
= single_release_net
,
2253 * Set timeout values for tcp tcpfin udp in the timeout_table.
2255 static int ip_vs_set_timeout(struct netns_ipvs
*ipvs
, struct ip_vs_timeout_user
*u
)
2257 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2258 struct ip_vs_proto_data
*pd
;
2261 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2266 #ifdef CONFIG_IP_VS_PROTO_TCP
2267 if (u
->tcp_timeout
< 0 || u
->tcp_timeout
> (INT_MAX
/ HZ
) ||
2268 u
->tcp_fin_timeout
< 0 || u
->tcp_fin_timeout
> (INT_MAX
/ HZ
)) {
2273 #ifdef CONFIG_IP_VS_PROTO_UDP
2274 if (u
->udp_timeout
< 0 || u
->udp_timeout
> (INT_MAX
/ HZ
))
2278 #ifdef CONFIG_IP_VS_PROTO_TCP
2279 if (u
->tcp_timeout
) {
2280 pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_TCP
);
2281 pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
2282 = u
->tcp_timeout
* HZ
;
2285 if (u
->tcp_fin_timeout
) {
2286 pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_TCP
);
2287 pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
2288 = u
->tcp_fin_timeout
* HZ
;
2292 #ifdef CONFIG_IP_VS_PROTO_UDP
2293 if (u
->udp_timeout
) {
2294 pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_UDP
);
2295 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
]
2296 = u
->udp_timeout
* HZ
;
2302 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2304 struct ip_vs_svcdest_user
{
2305 struct ip_vs_service_user s
;
2306 struct ip_vs_dest_user d
;
2309 static const unsigned char set_arglen
[CMDID(IP_VS_SO_SET_MAX
) + 1] = {
2310 [CMDID(IP_VS_SO_SET_ADD
)] = sizeof(struct ip_vs_service_user
),
2311 [CMDID(IP_VS_SO_SET_EDIT
)] = sizeof(struct ip_vs_service_user
),
2312 [CMDID(IP_VS_SO_SET_DEL
)] = sizeof(struct ip_vs_service_user
),
2313 [CMDID(IP_VS_SO_SET_ADDDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2314 [CMDID(IP_VS_SO_SET_DELDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2315 [CMDID(IP_VS_SO_SET_EDITDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2316 [CMDID(IP_VS_SO_SET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2317 [CMDID(IP_VS_SO_SET_STARTDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2318 [CMDID(IP_VS_SO_SET_STOPDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2319 [CMDID(IP_VS_SO_SET_ZERO
)] = sizeof(struct ip_vs_service_user
),
2322 union ip_vs_set_arglen
{
2323 struct ip_vs_service_user field_IP_VS_SO_SET_ADD
;
2324 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT
;
2325 struct ip_vs_service_user field_IP_VS_SO_SET_DEL
;
2326 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST
;
2327 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST
;
2328 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST
;
2329 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT
;
2330 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON
;
2331 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON
;
2332 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO
;
2335 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2337 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2338 struct ip_vs_service_user
*usvc_compat
)
2340 memset(usvc
, 0, sizeof(*usvc
));
2343 usvc
->protocol
= usvc_compat
->protocol
;
2344 usvc
->addr
.ip
= usvc_compat
->addr
;
2345 usvc
->port
= usvc_compat
->port
;
2346 usvc
->fwmark
= usvc_compat
->fwmark
;
2348 /* Deep copy of sched_name is not needed here */
2349 usvc
->sched_name
= usvc_compat
->sched_name
;
2351 usvc
->flags
= usvc_compat
->flags
;
2352 usvc
->timeout
= usvc_compat
->timeout
;
2353 usvc
->netmask
= usvc_compat
->netmask
;
2356 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2357 struct ip_vs_dest_user
*udest_compat
)
2359 memset(udest
, 0, sizeof(*udest
));
2361 udest
->addr
.ip
= udest_compat
->addr
;
2362 udest
->port
= udest_compat
->port
;
2363 udest
->conn_flags
= udest_compat
->conn_flags
;
2364 udest
->weight
= udest_compat
->weight
;
2365 udest
->u_threshold
= udest_compat
->u_threshold
;
2366 udest
->l_threshold
= udest_compat
->l_threshold
;
2367 udest
->af
= AF_INET
;
2371 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2373 struct net
*net
= sock_net(sk
);
2375 unsigned char arg
[MAX_SET_ARGLEN
];
2376 struct ip_vs_service_user
*usvc_compat
;
2377 struct ip_vs_service_user_kern usvc
;
2378 struct ip_vs_service
*svc
;
2379 struct ip_vs_dest_user
*udest_compat
;
2380 struct ip_vs_dest_user_kern udest
;
2381 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2383 BUILD_BUG_ON(sizeof(arg
) > 255);
2384 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2387 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2389 if (len
!= set_arglen
[CMDID(cmd
)]) {
2390 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2391 len
, set_arglen
[CMDID(cmd
)]);
2395 if (copy_from_user(arg
, user
, len
) != 0)
2398 /* Handle daemons since they have another lock */
2399 if (cmd
== IP_VS_SO_SET_STARTDAEMON
||
2400 cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2401 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2403 if (cmd
== IP_VS_SO_SET_STARTDAEMON
) {
2404 struct ipvs_sync_daemon_cfg cfg
;
2406 memset(&cfg
, 0, sizeof(cfg
));
2408 if (strscpy(cfg
.mcast_ifn
, dm
->mcast_ifn
,
2409 sizeof(cfg
.mcast_ifn
)) <= 0)
2411 cfg
.syncid
= dm
->syncid
;
2412 ret
= start_sync_thread(ipvs
, &cfg
, dm
->state
);
2414 ret
= stop_sync_thread(ipvs
, dm
->state
);
2419 mutex_lock(&__ip_vs_mutex
);
2420 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2421 /* Flush the virtual service */
2422 ret
= ip_vs_flush(ipvs
, false);
2424 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2425 /* Set timeout values for (tcp tcpfin udp) */
2426 ret
= ip_vs_set_timeout(ipvs
, (struct ip_vs_timeout_user
*)arg
);
2430 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2431 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2433 /* We only use the new structs internally, so copy userspace compat
2434 * structs to extended internal versions */
2435 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2436 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2438 if (cmd
== IP_VS_SO_SET_ZERO
) {
2439 /* if no service address is set, zero counters in all */
2440 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2441 ret
= ip_vs_zero_all(ipvs
);
2446 if ((cmd
== IP_VS_SO_SET_ADD
|| cmd
== IP_VS_SO_SET_EDIT
) &&
2447 strnlen(usvc
.sched_name
, IP_VS_SCHEDNAME_MAXLEN
) ==
2448 IP_VS_SCHEDNAME_MAXLEN
) {
2453 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2454 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2455 usvc
.protocol
!= IPPROTO_SCTP
) {
2456 pr_err("set_ctl: invalid protocol: %d %pI4:%d\n",
2457 usvc
.protocol
, &usvc
.addr
.ip
,
2463 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2465 if (usvc
.fwmark
== 0)
2466 svc
= __ip_vs_service_find(ipvs
, usvc
.af
, usvc
.protocol
,
2467 &usvc
.addr
, usvc
.port
);
2469 svc
= __ip_vs_svc_fwm_find(ipvs
, usvc
.af
, usvc
.fwmark
);
2472 if (cmd
!= IP_VS_SO_SET_ADD
2473 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2479 case IP_VS_SO_SET_ADD
:
2483 ret
= ip_vs_add_service(ipvs
, &usvc
, &svc
);
2485 case IP_VS_SO_SET_EDIT
:
2486 ret
= ip_vs_edit_service(svc
, &usvc
);
2488 case IP_VS_SO_SET_DEL
:
2489 ret
= ip_vs_del_service(svc
);
2493 case IP_VS_SO_SET_ZERO
:
2494 ret
= ip_vs_zero_service(svc
);
2496 case IP_VS_SO_SET_ADDDEST
:
2497 ret
= ip_vs_add_dest(svc
, &udest
);
2499 case IP_VS_SO_SET_EDITDEST
:
2500 ret
= ip_vs_edit_dest(svc
, &udest
);
2502 case IP_VS_SO_SET_DELDEST
:
2503 ret
= ip_vs_del_dest(svc
, &udest
);
2510 mutex_unlock(&__ip_vs_mutex
);
2516 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2518 struct ip_vs_scheduler
*sched
;
2519 struct ip_vs_kstats kstats
;
2522 sched
= rcu_dereference_protected(src
->scheduler
, 1);
2523 sched_name
= sched
? sched
->name
: "none";
2524 dst
->protocol
= src
->protocol
;
2525 dst
->addr
= src
->addr
.ip
;
2526 dst
->port
= src
->port
;
2527 dst
->fwmark
= src
->fwmark
;
2528 strlcpy(dst
->sched_name
, sched_name
, sizeof(dst
->sched_name
));
2529 dst
->flags
= src
->flags
;
2530 dst
->timeout
= src
->timeout
/ HZ
;
2531 dst
->netmask
= src
->netmask
;
2532 dst
->num_dests
= src
->num_dests
;
2533 ip_vs_copy_stats(&kstats
, &src
->stats
);
2534 ip_vs_export_stats_user(&dst
->stats
, &kstats
);
2538 __ip_vs_get_service_entries(struct netns_ipvs
*ipvs
,
2539 const struct ip_vs_get_services
*get
,
2540 struct ip_vs_get_services __user
*uptr
)
2543 struct ip_vs_service
*svc
;
2544 struct ip_vs_service_entry entry
;
2547 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2548 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2549 /* Only expose IPv4 entries to old interface */
2550 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2553 if (count
>= get
->num_services
)
2555 memset(&entry
, 0, sizeof(entry
));
2556 ip_vs_copy_service(&entry
, svc
);
2557 if (copy_to_user(&uptr
->entrytable
[count
],
2558 &entry
, sizeof(entry
))) {
2566 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2567 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
2568 /* Only expose IPv4 entries to old interface */
2569 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2572 if (count
>= get
->num_services
)
2574 memset(&entry
, 0, sizeof(entry
));
2575 ip_vs_copy_service(&entry
, svc
);
2576 if (copy_to_user(&uptr
->entrytable
[count
],
2577 &entry
, sizeof(entry
))) {
2589 __ip_vs_get_dest_entries(struct netns_ipvs
*ipvs
, const struct ip_vs_get_dests
*get
,
2590 struct ip_vs_get_dests __user
*uptr
)
2592 struct ip_vs_service
*svc
;
2593 union nf_inet_addr addr
= { .ip
= get
->addr
};
2598 svc
= __ip_vs_svc_fwm_find(ipvs
, AF_INET
, get
->fwmark
);
2600 svc
= __ip_vs_service_find(ipvs
, AF_INET
, get
->protocol
, &addr
,
2606 struct ip_vs_dest
*dest
;
2607 struct ip_vs_dest_entry entry
;
2608 struct ip_vs_kstats kstats
;
2610 memset(&entry
, 0, sizeof(entry
));
2611 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2612 if (count
>= get
->num_dests
)
2615 /* Cannot expose heterogeneous members via sockopt
2618 if (dest
->af
!= svc
->af
)
2621 entry
.addr
= dest
->addr
.ip
;
2622 entry
.port
= dest
->port
;
2623 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2624 entry
.weight
= atomic_read(&dest
->weight
);
2625 entry
.u_threshold
= dest
->u_threshold
;
2626 entry
.l_threshold
= dest
->l_threshold
;
2627 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2628 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2629 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2630 ip_vs_copy_stats(&kstats
, &dest
->stats
);
2631 ip_vs_export_stats_user(&entry
.stats
, &kstats
);
2632 if (copy_to_user(&uptr
->entrytable
[count
],
2633 &entry
, sizeof(entry
))) {
2645 __ip_vs_get_timeouts(struct netns_ipvs
*ipvs
, struct ip_vs_timeout_user
*u
)
2647 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2648 struct ip_vs_proto_data
*pd
;
2651 memset(u
, 0, sizeof (*u
));
2653 #ifdef CONFIG_IP_VS_PROTO_TCP
2654 pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_TCP
);
2655 u
->tcp_timeout
= pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2656 u
->tcp_fin_timeout
= pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2658 #ifdef CONFIG_IP_VS_PROTO_UDP
2659 pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_UDP
);
2661 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2665 static const unsigned char get_arglen
[CMDID(IP_VS_SO_GET_MAX
) + 1] = {
2666 [CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2667 [CMDID(IP_VS_SO_GET_INFO
)] = sizeof(struct ip_vs_getinfo
),
2668 [CMDID(IP_VS_SO_GET_SERVICES
)] = sizeof(struct ip_vs_get_services
),
2669 [CMDID(IP_VS_SO_GET_SERVICE
)] = sizeof(struct ip_vs_service_entry
),
2670 [CMDID(IP_VS_SO_GET_DESTS
)] = sizeof(struct ip_vs_get_dests
),
2671 [CMDID(IP_VS_SO_GET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2672 [CMDID(IP_VS_SO_GET_DAEMON
)] = 2 * sizeof(struct ip_vs_daemon_user
),
2675 union ip_vs_get_arglen
{
2676 char field_IP_VS_SO_GET_VERSION
[64];
2677 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO
;
2678 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES
;
2679 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE
;
2680 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS
;
2681 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT
;
2682 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON
[2];
2685 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2688 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2690 unsigned char arg
[MAX_GET_ARGLEN
];
2692 unsigned int copylen
;
2693 struct net
*net
= sock_net(sk
);
2694 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2697 BUILD_BUG_ON(sizeof(arg
) > 255);
2698 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2701 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2704 copylen
= get_arglen
[CMDID(cmd
)];
2705 if (*len
< (int) copylen
) {
2706 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len
, copylen
);
2710 if (copy_from_user(arg
, user
, copylen
) != 0)
2713 * Handle daemons first since it has its own locking
2715 if (cmd
== IP_VS_SO_GET_DAEMON
) {
2716 struct ip_vs_daemon_user d
[2];
2718 memset(&d
, 0, sizeof(d
));
2719 mutex_lock(&ipvs
->sync_mutex
);
2720 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
) {
2721 d
[0].state
= IP_VS_STATE_MASTER
;
2722 strlcpy(d
[0].mcast_ifn
, ipvs
->mcfg
.mcast_ifn
,
2723 sizeof(d
[0].mcast_ifn
));
2724 d
[0].syncid
= ipvs
->mcfg
.syncid
;
2726 if (ipvs
->sync_state
& IP_VS_STATE_BACKUP
) {
2727 d
[1].state
= IP_VS_STATE_BACKUP
;
2728 strlcpy(d
[1].mcast_ifn
, ipvs
->bcfg
.mcast_ifn
,
2729 sizeof(d
[1].mcast_ifn
));
2730 d
[1].syncid
= ipvs
->bcfg
.syncid
;
2732 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2734 mutex_unlock(&ipvs
->sync_mutex
);
2738 mutex_lock(&__ip_vs_mutex
);
2740 case IP_VS_SO_GET_VERSION
:
2744 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2745 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2746 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2750 *len
= strlen(buf
)+1;
2754 case IP_VS_SO_GET_INFO
:
2756 struct ip_vs_getinfo info
;
2757 info
.version
= IP_VS_VERSION_CODE
;
2758 info
.size
= ip_vs_conn_tab_size
;
2759 info
.num_services
= ipvs
->num_services
;
2760 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2765 case IP_VS_SO_GET_SERVICES
:
2767 struct ip_vs_get_services
*get
;
2770 get
= (struct ip_vs_get_services
*)arg
;
2771 size
= sizeof(*get
) +
2772 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2774 pr_err("length: %u != %u\n", *len
, size
);
2778 ret
= __ip_vs_get_service_entries(ipvs
, get
, user
);
2782 case IP_VS_SO_GET_SERVICE
:
2784 struct ip_vs_service_entry
*entry
;
2785 struct ip_vs_service
*svc
;
2786 union nf_inet_addr addr
;
2788 entry
= (struct ip_vs_service_entry
*)arg
;
2789 addr
.ip
= entry
->addr
;
2792 svc
= __ip_vs_svc_fwm_find(ipvs
, AF_INET
, entry
->fwmark
);
2794 svc
= __ip_vs_service_find(ipvs
, AF_INET
,
2795 entry
->protocol
, &addr
,
2799 ip_vs_copy_service(entry
, svc
);
2800 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2807 case IP_VS_SO_GET_DESTS
:
2809 struct ip_vs_get_dests
*get
;
2812 get
= (struct ip_vs_get_dests
*)arg
;
2813 size
= sizeof(*get
) +
2814 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2816 pr_err("length: %u != %u\n", *len
, size
);
2820 ret
= __ip_vs_get_dest_entries(ipvs
, get
, user
);
2824 case IP_VS_SO_GET_TIMEOUT
:
2826 struct ip_vs_timeout_user t
;
2828 __ip_vs_get_timeouts(ipvs
, &t
);
2829 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2839 mutex_unlock(&__ip_vs_mutex
);
2844 static struct nf_sockopt_ops ip_vs_sockopts
= {
2846 .set_optmin
= IP_VS_BASE_CTL
,
2847 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2848 .set
= do_ip_vs_set_ctl
,
2849 .get_optmin
= IP_VS_BASE_CTL
,
2850 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2851 .get
= do_ip_vs_get_ctl
,
2852 .owner
= THIS_MODULE
,
2856 * Generic Netlink interface
2859 /* IPVS genetlink family */
2860 static struct genl_family ip_vs_genl_family
;
2862 /* Policy used for first-level command attributes */
2863 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2864 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2865 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2866 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2867 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2868 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2869 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2872 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2873 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2874 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2875 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2876 .len
= IP_VS_IFNAME_MAXLEN
- 1 },
2877 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2878 [IPVS_DAEMON_ATTR_SYNC_MAXLEN
] = { .type
= NLA_U16
},
2879 [IPVS_DAEMON_ATTR_MCAST_GROUP
] = { .type
= NLA_U32
},
2880 [IPVS_DAEMON_ATTR_MCAST_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2881 [IPVS_DAEMON_ATTR_MCAST_PORT
] = { .type
= NLA_U16
},
2882 [IPVS_DAEMON_ATTR_MCAST_TTL
] = { .type
= NLA_U8
},
2885 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2886 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2887 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2888 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2889 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2890 .len
= sizeof(union nf_inet_addr
) },
2891 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2892 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2893 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2894 .len
= IP_VS_SCHEDNAME_MAXLEN
- 1 },
2895 [IPVS_SVC_ATTR_PE_NAME
] = { .type
= NLA_NUL_STRING
,
2896 .len
= IP_VS_PENAME_MAXLEN
},
2897 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2898 .len
= sizeof(struct ip_vs_flags
) },
2899 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2900 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2901 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2904 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2905 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2906 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2907 .len
= sizeof(union nf_inet_addr
) },
2908 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2909 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2910 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2911 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2912 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2913 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2914 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2915 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2916 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2917 [IPVS_DEST_ATTR_ADDR_FAMILY
] = { .type
= NLA_U16
},
2920 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2921 struct ip_vs_kstats
*kstats
)
2923 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2928 if (nla_put_u32(skb
, IPVS_STATS_ATTR_CONNS
, (u32
)kstats
->conns
) ||
2929 nla_put_u32(skb
, IPVS_STATS_ATTR_INPKTS
, (u32
)kstats
->inpkts
) ||
2930 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPKTS
, (u32
)kstats
->outpkts
) ||
2931 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
,
2932 IPVS_STATS_ATTR_PAD
) ||
2933 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
,
2934 IPVS_STATS_ATTR_PAD
) ||
2935 nla_put_u32(skb
, IPVS_STATS_ATTR_CPS
, (u32
)kstats
->cps
) ||
2936 nla_put_u32(skb
, IPVS_STATS_ATTR_INPPS
, (u32
)kstats
->inpps
) ||
2937 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPPS
, (u32
)kstats
->outpps
) ||
2938 nla_put_u32(skb
, IPVS_STATS_ATTR_INBPS
, (u32
)kstats
->inbps
) ||
2939 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTBPS
, (u32
)kstats
->outbps
))
2940 goto nla_put_failure
;
2941 nla_nest_end(skb
, nl_stats
);
2946 nla_nest_cancel(skb
, nl_stats
);
2950 static int ip_vs_genl_fill_stats64(struct sk_buff
*skb
, int container_type
,
2951 struct ip_vs_kstats
*kstats
)
2953 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2958 if (nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_CONNS
, kstats
->conns
,
2959 IPVS_STATS_ATTR_PAD
) ||
2960 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_INPKTS
, kstats
->inpkts
,
2961 IPVS_STATS_ATTR_PAD
) ||
2962 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_OUTPKTS
, kstats
->outpkts
,
2963 IPVS_STATS_ATTR_PAD
) ||
2964 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
,
2965 IPVS_STATS_ATTR_PAD
) ||
2966 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
,
2967 IPVS_STATS_ATTR_PAD
) ||
2968 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_CPS
, kstats
->cps
,
2969 IPVS_STATS_ATTR_PAD
) ||
2970 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_INPPS
, kstats
->inpps
,
2971 IPVS_STATS_ATTR_PAD
) ||
2972 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_OUTPPS
, kstats
->outpps
,
2973 IPVS_STATS_ATTR_PAD
) ||
2974 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_INBPS
, kstats
->inbps
,
2975 IPVS_STATS_ATTR_PAD
) ||
2976 nla_put_u64_64bit(skb
, IPVS_STATS_ATTR_OUTBPS
, kstats
->outbps
,
2977 IPVS_STATS_ATTR_PAD
))
2978 goto nla_put_failure
;
2979 nla_nest_end(skb
, nl_stats
);
2984 nla_nest_cancel(skb
, nl_stats
);
2988 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2989 struct ip_vs_service
*svc
)
2991 struct ip_vs_scheduler
*sched
;
2992 struct ip_vs_pe
*pe
;
2993 struct nlattr
*nl_service
;
2994 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2996 struct ip_vs_kstats kstats
;
2999 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
3003 if (nla_put_u16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
))
3004 goto nla_put_failure
;
3006 if (nla_put_u32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
))
3007 goto nla_put_failure
;
3009 if (nla_put_u16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
) ||
3010 nla_put(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
) ||
3011 nla_put_be16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
))
3012 goto nla_put_failure
;
3015 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
3016 sched_name
= sched
? sched
->name
: "none";
3017 pe
= rcu_dereference_protected(svc
->pe
, 1);
3018 if (nla_put_string(skb
, IPVS_SVC_ATTR_SCHED_NAME
, sched_name
) ||
3019 (pe
&& nla_put_string(skb
, IPVS_SVC_ATTR_PE_NAME
, pe
->name
)) ||
3020 nla_put(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
) ||
3021 nla_put_u32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
) ||
3022 nla_put_be32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
))
3023 goto nla_put_failure
;
3024 ip_vs_copy_stats(&kstats
, &svc
->stats
);
3025 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &kstats
))
3026 goto nla_put_failure
;
3027 if (ip_vs_genl_fill_stats64(skb
, IPVS_SVC_ATTR_STATS64
, &kstats
))
3028 goto nla_put_failure
;
3030 nla_nest_end(skb
, nl_service
);
3035 nla_nest_cancel(skb
, nl_service
);
3039 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
3040 struct ip_vs_service
*svc
,
3041 struct netlink_callback
*cb
)
3045 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3046 &ip_vs_genl_family
, NLM_F_MULTI
,
3047 IPVS_CMD_NEW_SERVICE
);
3051 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
3052 goto nla_put_failure
;
3054 genlmsg_end(skb
, hdr
);
3058 genlmsg_cancel(skb
, hdr
);
3062 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
3063 struct netlink_callback
*cb
)
3066 int start
= cb
->args
[0];
3067 struct ip_vs_service
*svc
;
3068 struct net
*net
= sock_net(skb
->sk
);
3069 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3071 mutex_lock(&__ip_vs_mutex
);
3072 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3073 hlist_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
3074 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3076 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3078 goto nla_put_failure
;
3083 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3084 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
3085 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3087 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3089 goto nla_put_failure
;
3095 mutex_unlock(&__ip_vs_mutex
);
3101 static bool ip_vs_is_af_valid(int af
)
3105 #ifdef CONFIG_IP_VS_IPV6
3106 if (af
== AF_INET6
&& ipv6_mod_enabled())
3112 static int ip_vs_genl_parse_service(struct netns_ipvs
*ipvs
,
3113 struct ip_vs_service_user_kern
*usvc
,
3114 struct nlattr
*nla
, int full_entry
,
3115 struct ip_vs_service
**ret_svc
)
3117 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
3118 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
3119 struct ip_vs_service
*svc
;
3121 /* Parse mandatory identifying service fields first */
3123 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
,
3124 ip_vs_svc_policy
, NULL
))
3127 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
3128 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
3129 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
3130 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
3131 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
3133 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
3136 memset(usvc
, 0, sizeof(*usvc
));
3138 usvc
->af
= nla_get_u16(nla_af
);
3139 if (!ip_vs_is_af_valid(usvc
->af
))
3140 return -EAFNOSUPPORT
;
3143 usvc
->protocol
= IPPROTO_TCP
;
3144 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
3146 usvc
->protocol
= nla_get_u16(nla_protocol
);
3147 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
3148 usvc
->port
= nla_get_be16(nla_port
);
3154 svc
= __ip_vs_svc_fwm_find(ipvs
, usvc
->af
, usvc
->fwmark
);
3156 svc
= __ip_vs_service_find(ipvs
, usvc
->af
, usvc
->protocol
,
3157 &usvc
->addr
, usvc
->port
);
3161 /* If a full entry was requested, check for the additional fields */
3163 struct nlattr
*nla_sched
, *nla_flags
, *nla_pe
, *nla_timeout
,
3165 struct ip_vs_flags flags
;
3167 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
3168 nla_pe
= attrs
[IPVS_SVC_ATTR_PE_NAME
];
3169 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
3170 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
3171 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
3173 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
3176 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
3178 /* prefill flags from service if it already exists */
3180 usvc
->flags
= svc
->flags
;
3182 /* set new flags from userland */
3183 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
3184 (flags
.flags
& flags
.mask
);
3185 usvc
->sched_name
= nla_data(nla_sched
);
3186 usvc
->pe_name
= nla_pe
? nla_data(nla_pe
) : NULL
;
3187 usvc
->timeout
= nla_get_u32(nla_timeout
);
3188 usvc
->netmask
= nla_get_be32(nla_netmask
);
3194 static struct ip_vs_service
*ip_vs_genl_find_service(struct netns_ipvs
*ipvs
,
3197 struct ip_vs_service_user_kern usvc
;
3198 struct ip_vs_service
*svc
;
3201 ret
= ip_vs_genl_parse_service(ipvs
, &usvc
, nla
, 0, &svc
);
3202 return ret
? ERR_PTR(ret
) : svc
;
3205 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
3207 struct nlattr
*nl_dest
;
3208 struct ip_vs_kstats kstats
;
3210 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
3214 if (nla_put(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
) ||
3215 nla_put_be16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
) ||
3216 nla_put_u32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
3217 (atomic_read(&dest
->conn_flags
) &
3218 IP_VS_CONN_F_FWD_MASK
)) ||
3219 nla_put_u32(skb
, IPVS_DEST_ATTR_WEIGHT
,
3220 atomic_read(&dest
->weight
)) ||
3221 nla_put_u32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
) ||
3222 nla_put_u32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
) ||
3223 nla_put_u32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
3224 atomic_read(&dest
->activeconns
)) ||
3225 nla_put_u32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
3226 atomic_read(&dest
->inactconns
)) ||
3227 nla_put_u32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
3228 atomic_read(&dest
->persistconns
)) ||
3229 nla_put_u16(skb
, IPVS_DEST_ATTR_ADDR_FAMILY
, dest
->af
))
3230 goto nla_put_failure
;
3231 ip_vs_copy_stats(&kstats
, &dest
->stats
);
3232 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &kstats
))
3233 goto nla_put_failure
;
3234 if (ip_vs_genl_fill_stats64(skb
, IPVS_DEST_ATTR_STATS64
, &kstats
))
3235 goto nla_put_failure
;
3237 nla_nest_end(skb
, nl_dest
);
3242 nla_nest_cancel(skb
, nl_dest
);
3246 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
3247 struct netlink_callback
*cb
)
3251 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3252 &ip_vs_genl_family
, NLM_F_MULTI
,
3257 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
3258 goto nla_put_failure
;
3260 genlmsg_end(skb
, hdr
);
3264 genlmsg_cancel(skb
, hdr
);
3268 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
3269 struct netlink_callback
*cb
)
3272 int start
= cb
->args
[0];
3273 struct ip_vs_service
*svc
;
3274 struct ip_vs_dest
*dest
;
3275 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
3276 struct net
*net
= sock_net(skb
->sk
);
3277 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3279 mutex_lock(&__ip_vs_mutex
);
3281 /* Try to find the service for which to dump destinations */
3282 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
, IPVS_CMD_ATTR_MAX
,
3283 ip_vs_cmd_policy
, NULL
))
3287 svc
= ip_vs_genl_find_service(ipvs
, attrs
[IPVS_CMD_ATTR_SERVICE
]);
3288 if (IS_ERR_OR_NULL(svc
))
3291 /* Dump the destinations */
3292 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
3295 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
3297 goto nla_put_failure
;
3305 mutex_unlock(&__ip_vs_mutex
);
3310 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
3311 struct nlattr
*nla
, int full_entry
)
3313 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
3314 struct nlattr
*nla_addr
, *nla_port
;
3315 struct nlattr
*nla_addr_family
;
3317 /* Parse mandatory identifying destination fields first */
3319 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
,
3320 ip_vs_dest_policy
, NULL
))
3323 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
3324 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
3325 nla_addr_family
= attrs
[IPVS_DEST_ATTR_ADDR_FAMILY
];
3327 if (!(nla_addr
&& nla_port
))
3330 memset(udest
, 0, sizeof(*udest
));
3332 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
3333 udest
->port
= nla_get_be16(nla_port
);
3335 if (nla_addr_family
)
3336 udest
->af
= nla_get_u16(nla_addr_family
);
3340 /* If a full entry was requested, check for the additional fields */
3342 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
3345 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
3346 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
3347 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
3348 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
3350 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
3353 udest
->conn_flags
= nla_get_u32(nla_fwd
)
3354 & IP_VS_CONN_F_FWD_MASK
;
3355 udest
->weight
= nla_get_u32(nla_weight
);
3356 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
3357 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
3363 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __u32 state
,
3364 struct ipvs_sync_daemon_cfg
*c
)
3366 struct nlattr
*nl_daemon
;
3368 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
3372 if (nla_put_u32(skb
, IPVS_DAEMON_ATTR_STATE
, state
) ||
3373 nla_put_string(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, c
->mcast_ifn
) ||
3374 nla_put_u32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, c
->syncid
) ||
3375 nla_put_u16(skb
, IPVS_DAEMON_ATTR_SYNC_MAXLEN
, c
->sync_maxlen
) ||
3376 nla_put_u16(skb
, IPVS_DAEMON_ATTR_MCAST_PORT
, c
->mcast_port
) ||
3377 nla_put_u8(skb
, IPVS_DAEMON_ATTR_MCAST_TTL
, c
->mcast_ttl
))
3378 goto nla_put_failure
;
3379 #ifdef CONFIG_IP_VS_IPV6
3380 if (c
->mcast_af
== AF_INET6
) {
3381 if (nla_put_in6_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP6
,
3382 &c
->mcast_group
.in6
))
3383 goto nla_put_failure
;
3386 if (c
->mcast_af
== AF_INET
&&
3387 nla_put_in_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP
,
3389 goto nla_put_failure
;
3390 nla_nest_end(skb
, nl_daemon
);
3395 nla_nest_cancel(skb
, nl_daemon
);
3399 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __u32 state
,
3400 struct ipvs_sync_daemon_cfg
*c
,
3401 struct netlink_callback
*cb
)
3404 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3405 &ip_vs_genl_family
, NLM_F_MULTI
,
3406 IPVS_CMD_NEW_DAEMON
);
3410 if (ip_vs_genl_fill_daemon(skb
, state
, c
))
3411 goto nla_put_failure
;
3413 genlmsg_end(skb
, hdr
);
3417 genlmsg_cancel(skb
, hdr
);
3421 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
3422 struct netlink_callback
*cb
)
3424 struct net
*net
= sock_net(skb
->sk
);
3425 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3427 mutex_lock(&ipvs
->sync_mutex
);
3428 if ((ipvs
->sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
3429 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
3430 &ipvs
->mcfg
, cb
) < 0)
3431 goto nla_put_failure
;
3436 if ((ipvs
->sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
3437 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
3438 &ipvs
->bcfg
, cb
) < 0)
3439 goto nla_put_failure
;
3445 mutex_unlock(&ipvs
->sync_mutex
);
3450 static int ip_vs_genl_new_daemon(struct netns_ipvs
*ipvs
, struct nlattr
**attrs
)
3452 struct ipvs_sync_daemon_cfg c
;
3456 memset(&c
, 0, sizeof(c
));
3457 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
3458 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
3459 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3461 strlcpy(c
.mcast_ifn
, nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3462 sizeof(c
.mcast_ifn
));
3463 c
.syncid
= nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]);
3465 a
= attrs
[IPVS_DAEMON_ATTR_SYNC_MAXLEN
];
3467 c
.sync_maxlen
= nla_get_u16(a
);
3469 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP
];
3471 c
.mcast_af
= AF_INET
;
3472 c
.mcast_group
.ip
= nla_get_in_addr(a
);
3473 if (!ipv4_is_multicast(c
.mcast_group
.ip
))
3476 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP6
];
3478 #ifdef CONFIG_IP_VS_IPV6
3481 c
.mcast_af
= AF_INET6
;
3482 c
.mcast_group
.in6
= nla_get_in6_addr(a
);
3483 addr_type
= ipv6_addr_type(&c
.mcast_group
.in6
);
3484 if (!(addr_type
& IPV6_ADDR_MULTICAST
))
3487 return -EAFNOSUPPORT
;
3492 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_PORT
];
3494 c
.mcast_port
= nla_get_u16(a
);
3496 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_TTL
];
3498 c
.mcast_ttl
= nla_get_u8(a
);
3500 /* The synchronization protocol is incompatible with mixed family
3503 if (ipvs
->mixed_address_family_dests
> 0)
3506 ret
= start_sync_thread(ipvs
, &c
,
3507 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3511 static int ip_vs_genl_del_daemon(struct netns_ipvs
*ipvs
, struct nlattr
**attrs
)
3515 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3518 ret
= stop_sync_thread(ipvs
,
3519 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3523 static int ip_vs_genl_set_config(struct netns_ipvs
*ipvs
, struct nlattr
**attrs
)
3525 struct ip_vs_timeout_user t
;
3527 __ip_vs_get_timeouts(ipvs
, &t
);
3529 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3530 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3532 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3534 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3536 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3537 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3539 return ip_vs_set_timeout(ipvs
, &t
);
3542 static int ip_vs_genl_set_daemon(struct sk_buff
*skb
, struct genl_info
*info
)
3544 int ret
= -EINVAL
, cmd
;
3545 struct net
*net
= sock_net(skb
->sk
);
3546 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3548 cmd
= info
->genlhdr
->cmd
;
3550 if (cmd
== IPVS_CMD_NEW_DAEMON
|| cmd
== IPVS_CMD_DEL_DAEMON
) {
3551 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3553 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3554 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3555 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3556 ip_vs_daemon_policy
, info
->extack
))
3559 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3560 ret
= ip_vs_genl_new_daemon(ipvs
, daemon_attrs
);
3562 ret
= ip_vs_genl_del_daemon(ipvs
, daemon_attrs
);
3569 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3571 struct ip_vs_service
*svc
= NULL
;
3572 struct ip_vs_service_user_kern usvc
;
3573 struct ip_vs_dest_user_kern udest
;
3575 int need_full_svc
= 0, need_full_dest
= 0;
3576 struct net
*net
= sock_net(skb
->sk
);
3577 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3579 cmd
= info
->genlhdr
->cmd
;
3581 mutex_lock(&__ip_vs_mutex
);
3583 if (cmd
== IPVS_CMD_FLUSH
) {
3584 ret
= ip_vs_flush(ipvs
, false);
3586 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3587 ret
= ip_vs_genl_set_config(ipvs
, info
->attrs
);
3589 } else if (cmd
== IPVS_CMD_ZERO
&&
3590 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3591 ret
= ip_vs_zero_all(ipvs
);
3595 /* All following commands require a service argument, so check if we
3596 * received a valid one. We need a full service specification when
3597 * adding / editing a service. Only identifying members otherwise. */
3598 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3601 ret
= ip_vs_genl_parse_service(ipvs
, &usvc
,
3602 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3603 need_full_svc
, &svc
);
3607 /* Unless we're adding a new service, the service must already exist */
3608 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3613 /* Destination commands require a valid destination argument. For
3614 * adding / editing a destination, we need a full destination
3616 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3617 cmd
== IPVS_CMD_DEL_DEST
) {
3618 if (cmd
!= IPVS_CMD_DEL_DEST
)
3621 ret
= ip_vs_genl_parse_dest(&udest
,
3622 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3627 /* Old protocols did not allow the user to specify address
3628 * family, so we set it to zero instead. We also didn't
3629 * allow heterogeneous pools in the old code, so it's safe
3630 * to assume that this will have the same address family as
3636 if (!ip_vs_is_af_valid(udest
.af
)) {
3637 ret
= -EAFNOSUPPORT
;
3641 if (udest
.af
!= svc
->af
&& cmd
!= IPVS_CMD_DEL_DEST
) {
3642 /* The synchronization protocol is incompatible
3643 * with mixed family services
3645 if (ipvs
->sync_state
) {
3650 /* Which connection types do we support? */
3651 switch (udest
.conn_flags
) {
3652 case IP_VS_CONN_F_TUNNEL
:
3653 /* We are able to forward this */
3663 case IPVS_CMD_NEW_SERVICE
:
3665 ret
= ip_vs_add_service(ipvs
, &usvc
, &svc
);
3669 case IPVS_CMD_SET_SERVICE
:
3670 ret
= ip_vs_edit_service(svc
, &usvc
);
3672 case IPVS_CMD_DEL_SERVICE
:
3673 ret
= ip_vs_del_service(svc
);
3674 /* do not use svc, it can be freed */
3676 case IPVS_CMD_NEW_DEST
:
3677 ret
= ip_vs_add_dest(svc
, &udest
);
3679 case IPVS_CMD_SET_DEST
:
3680 ret
= ip_vs_edit_dest(svc
, &udest
);
3682 case IPVS_CMD_DEL_DEST
:
3683 ret
= ip_vs_del_dest(svc
, &udest
);
3686 ret
= ip_vs_zero_service(svc
);
3693 mutex_unlock(&__ip_vs_mutex
);
3698 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3700 struct sk_buff
*msg
;
3702 int ret
, cmd
, reply_cmd
;
3703 struct net
*net
= sock_net(skb
->sk
);
3704 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3706 cmd
= info
->genlhdr
->cmd
;
3708 if (cmd
== IPVS_CMD_GET_SERVICE
)
3709 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3710 else if (cmd
== IPVS_CMD_GET_INFO
)
3711 reply_cmd
= IPVS_CMD_SET_INFO
;
3712 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3713 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3715 pr_err("unknown Generic Netlink command\n");
3719 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3723 mutex_lock(&__ip_vs_mutex
);
3725 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3727 goto nla_put_failure
;
3730 case IPVS_CMD_GET_SERVICE
:
3732 struct ip_vs_service
*svc
;
3734 svc
= ip_vs_genl_find_service(ipvs
,
3735 info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3740 ret
= ip_vs_genl_fill_service(msg
, svc
);
3742 goto nla_put_failure
;
3751 case IPVS_CMD_GET_CONFIG
:
3753 struct ip_vs_timeout_user t
;
3755 __ip_vs_get_timeouts(ipvs
, &t
);
3756 #ifdef CONFIG_IP_VS_PROTO_TCP
3757 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
,
3759 nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3761 goto nla_put_failure
;
3763 #ifdef CONFIG_IP_VS_PROTO_UDP
3764 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
))
3765 goto nla_put_failure
;
3771 case IPVS_CMD_GET_INFO
:
3772 if (nla_put_u32(msg
, IPVS_INFO_ATTR_VERSION
,
3773 IP_VS_VERSION_CODE
) ||
3774 nla_put_u32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3775 ip_vs_conn_tab_size
))
3776 goto nla_put_failure
;
3780 genlmsg_end(msg
, reply
);
3781 ret
= genlmsg_reply(msg
, info
);
3785 pr_err("not enough space in Netlink message\n");
3791 mutex_unlock(&__ip_vs_mutex
);
3797 static const struct genl_ops ip_vs_genl_ops
[] = {
3799 .cmd
= IPVS_CMD_NEW_SERVICE
,
3800 .flags
= GENL_ADMIN_PERM
,
3801 .policy
= ip_vs_cmd_policy
,
3802 .doit
= ip_vs_genl_set_cmd
,
3805 .cmd
= IPVS_CMD_SET_SERVICE
,
3806 .flags
= GENL_ADMIN_PERM
,
3807 .policy
= ip_vs_cmd_policy
,
3808 .doit
= ip_vs_genl_set_cmd
,
3811 .cmd
= IPVS_CMD_DEL_SERVICE
,
3812 .flags
= GENL_ADMIN_PERM
,
3813 .policy
= ip_vs_cmd_policy
,
3814 .doit
= ip_vs_genl_set_cmd
,
3817 .cmd
= IPVS_CMD_GET_SERVICE
,
3818 .flags
= GENL_ADMIN_PERM
,
3819 .doit
= ip_vs_genl_get_cmd
,
3820 .dumpit
= ip_vs_genl_dump_services
,
3821 .policy
= ip_vs_cmd_policy
,
3824 .cmd
= IPVS_CMD_NEW_DEST
,
3825 .flags
= GENL_ADMIN_PERM
,
3826 .policy
= ip_vs_cmd_policy
,
3827 .doit
= ip_vs_genl_set_cmd
,
3830 .cmd
= IPVS_CMD_SET_DEST
,
3831 .flags
= GENL_ADMIN_PERM
,
3832 .policy
= ip_vs_cmd_policy
,
3833 .doit
= ip_vs_genl_set_cmd
,
3836 .cmd
= IPVS_CMD_DEL_DEST
,
3837 .flags
= GENL_ADMIN_PERM
,
3838 .policy
= ip_vs_cmd_policy
,
3839 .doit
= ip_vs_genl_set_cmd
,
3842 .cmd
= IPVS_CMD_GET_DEST
,
3843 .flags
= GENL_ADMIN_PERM
,
3844 .policy
= ip_vs_cmd_policy
,
3845 .dumpit
= ip_vs_genl_dump_dests
,
3848 .cmd
= IPVS_CMD_NEW_DAEMON
,
3849 .flags
= GENL_ADMIN_PERM
,
3850 .policy
= ip_vs_cmd_policy
,
3851 .doit
= ip_vs_genl_set_daemon
,
3854 .cmd
= IPVS_CMD_DEL_DAEMON
,
3855 .flags
= GENL_ADMIN_PERM
,
3856 .policy
= ip_vs_cmd_policy
,
3857 .doit
= ip_vs_genl_set_daemon
,
3860 .cmd
= IPVS_CMD_GET_DAEMON
,
3861 .flags
= GENL_ADMIN_PERM
,
3862 .dumpit
= ip_vs_genl_dump_daemons
,
3865 .cmd
= IPVS_CMD_SET_CONFIG
,
3866 .flags
= GENL_ADMIN_PERM
,
3867 .policy
= ip_vs_cmd_policy
,
3868 .doit
= ip_vs_genl_set_cmd
,
3871 .cmd
= IPVS_CMD_GET_CONFIG
,
3872 .flags
= GENL_ADMIN_PERM
,
3873 .doit
= ip_vs_genl_get_cmd
,
3876 .cmd
= IPVS_CMD_GET_INFO
,
3877 .flags
= GENL_ADMIN_PERM
,
3878 .doit
= ip_vs_genl_get_cmd
,
3881 .cmd
= IPVS_CMD_ZERO
,
3882 .flags
= GENL_ADMIN_PERM
,
3883 .policy
= ip_vs_cmd_policy
,
3884 .doit
= ip_vs_genl_set_cmd
,
3887 .cmd
= IPVS_CMD_FLUSH
,
3888 .flags
= GENL_ADMIN_PERM
,
3889 .doit
= ip_vs_genl_set_cmd
,
3893 static struct genl_family ip_vs_genl_family __ro_after_init
= {
3895 .name
= IPVS_GENL_NAME
,
3896 .version
= IPVS_GENL_VERSION
,
3897 .maxattr
= IPVS_CMD_ATTR_MAX
,
3898 .netnsok
= true, /* Make ipvsadm to work on netns */
3899 .module
= THIS_MODULE
,
3900 .ops
= ip_vs_genl_ops
,
3901 .n_ops
= ARRAY_SIZE(ip_vs_genl_ops
),
3904 static int __init
ip_vs_genl_register(void)
3906 return genl_register_family(&ip_vs_genl_family
);
3909 static void ip_vs_genl_unregister(void)
3911 genl_unregister_family(&ip_vs_genl_family
);
3914 /* End of Generic Netlink interface definitions */
3917 * per netns intit/exit func.
3919 #ifdef CONFIG_SYSCTL
3920 static int __net_init
ip_vs_control_net_init_sysctl(struct netns_ipvs
*ipvs
)
3922 struct net
*net
= ipvs
->net
;
3924 struct ctl_table
*tbl
;
3926 atomic_set(&ipvs
->dropentry
, 0);
3927 spin_lock_init(&ipvs
->dropentry_lock
);
3928 spin_lock_init(&ipvs
->droppacket_lock
);
3929 spin_lock_init(&ipvs
->securetcp_lock
);
3931 if (!net_eq(net
, &init_net
)) {
3932 tbl
= kmemdup(vs_vars
, sizeof(vs_vars
), GFP_KERNEL
);
3936 /* Don't export sysctls to unprivileged users */
3937 if (net
->user_ns
!= &init_user_ns
)
3938 tbl
[0].procname
= NULL
;
3941 /* Initialize sysctl defaults */
3942 for (idx
= 0; idx
< ARRAY_SIZE(vs_vars
); idx
++) {
3943 if (tbl
[idx
].proc_handler
== proc_do_defense_mode
)
3944 tbl
[idx
].extra2
= ipvs
;
3947 ipvs
->sysctl_amemthresh
= 1024;
3948 tbl
[idx
++].data
= &ipvs
->sysctl_amemthresh
;
3949 ipvs
->sysctl_am_droprate
= 10;
3950 tbl
[idx
++].data
= &ipvs
->sysctl_am_droprate
;
3951 tbl
[idx
++].data
= &ipvs
->sysctl_drop_entry
;
3952 tbl
[idx
++].data
= &ipvs
->sysctl_drop_packet
;
3953 #ifdef CONFIG_IP_VS_NFCT
3954 tbl
[idx
++].data
= &ipvs
->sysctl_conntrack
;
3956 tbl
[idx
++].data
= &ipvs
->sysctl_secure_tcp
;
3957 ipvs
->sysctl_snat_reroute
= 1;
3958 tbl
[idx
++].data
= &ipvs
->sysctl_snat_reroute
;
3959 ipvs
->sysctl_sync_ver
= 1;
3960 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ver
;
3961 ipvs
->sysctl_sync_ports
= 1;
3962 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ports
;
3963 tbl
[idx
++].data
= &ipvs
->sysctl_sync_persist_mode
;
3964 ipvs
->sysctl_sync_qlen_max
= nr_free_buffer_pages() / 32;
3965 tbl
[idx
++].data
= &ipvs
->sysctl_sync_qlen_max
;
3966 ipvs
->sysctl_sync_sock_size
= 0;
3967 tbl
[idx
++].data
= &ipvs
->sysctl_sync_sock_size
;
3968 tbl
[idx
++].data
= &ipvs
->sysctl_cache_bypass
;
3969 tbl
[idx
++].data
= &ipvs
->sysctl_expire_nodest_conn
;
3970 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_tcp
;
3971 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_sctp
;
3972 tbl
[idx
++].data
= &ipvs
->sysctl_expire_quiescent_template
;
3973 ipvs
->sysctl_sync_threshold
[0] = DEFAULT_SYNC_THRESHOLD
;
3974 ipvs
->sysctl_sync_threshold
[1] = DEFAULT_SYNC_PERIOD
;
3975 tbl
[idx
].data
= &ipvs
->sysctl_sync_threshold
;
3976 tbl
[idx
++].maxlen
= sizeof(ipvs
->sysctl_sync_threshold
);
3977 ipvs
->sysctl_sync_refresh_period
= DEFAULT_SYNC_REFRESH_PERIOD
;
3978 tbl
[idx
++].data
= &ipvs
->sysctl_sync_refresh_period
;
3979 ipvs
->sysctl_sync_retries
= clamp_t(int, DEFAULT_SYNC_RETRIES
, 0, 3);
3980 tbl
[idx
++].data
= &ipvs
->sysctl_sync_retries
;
3981 tbl
[idx
++].data
= &ipvs
->sysctl_nat_icmp_send
;
3982 ipvs
->sysctl_pmtu_disc
= 1;
3983 tbl
[idx
++].data
= &ipvs
->sysctl_pmtu_disc
;
3984 tbl
[idx
++].data
= &ipvs
->sysctl_backup_only
;
3985 ipvs
->sysctl_conn_reuse_mode
= 1;
3986 tbl
[idx
++].data
= &ipvs
->sysctl_conn_reuse_mode
;
3987 tbl
[idx
++].data
= &ipvs
->sysctl_schedule_icmp
;
3988 tbl
[idx
++].data
= &ipvs
->sysctl_ignore_tunneled
;
3990 ipvs
->sysctl_hdr
= register_net_sysctl(net
, "net/ipv4/vs", tbl
);
3991 if (ipvs
->sysctl_hdr
== NULL
) {
3992 if (!net_eq(net
, &init_net
))
3996 ip_vs_start_estimator(ipvs
, &ipvs
->tot_stats
);
3997 ipvs
->sysctl_tbl
= tbl
;
3998 /* Schedule defense work */
3999 INIT_DELAYED_WORK(&ipvs
->defense_work
, defense_work_handler
);
4000 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
4005 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct netns_ipvs
*ipvs
)
4007 struct net
*net
= ipvs
->net
;
4009 cancel_delayed_work_sync(&ipvs
->defense_work
);
4010 cancel_work_sync(&ipvs
->defense_work
.work
);
4011 unregister_net_sysctl_table(ipvs
->sysctl_hdr
);
4012 ip_vs_stop_estimator(ipvs
, &ipvs
->tot_stats
);
4014 if (!net_eq(net
, &init_net
))
4015 kfree(ipvs
->sysctl_tbl
);
4020 static int __net_init
ip_vs_control_net_init_sysctl(struct netns_ipvs
*ipvs
) { return 0; }
4021 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct netns_ipvs
*ipvs
) { }
4025 static struct notifier_block ip_vs_dst_notifier
= {
4026 .notifier_call
= ip_vs_dst_event
,
4027 #ifdef CONFIG_IP_VS_IPV6
4028 .priority
= ADDRCONF_NOTIFY_PRIORITY
+ 5,
4032 int __net_init
ip_vs_control_net_init(struct netns_ipvs
*ipvs
)
4036 /* Initialize rs_table */
4037 for (idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++)
4038 INIT_HLIST_HEAD(&ipvs
->rs_table
[idx
]);
4040 INIT_LIST_HEAD(&ipvs
->dest_trash
);
4041 spin_lock_init(&ipvs
->dest_trash_lock
);
4042 timer_setup(&ipvs
->dest_trash_timer
, ip_vs_dest_trash_expire
, 0);
4043 atomic_set(&ipvs
->ftpsvc_counter
, 0);
4044 atomic_set(&ipvs
->nullsvc_counter
, 0);
4045 atomic_set(&ipvs
->conn_out_counter
, 0);
4048 ipvs
->tot_stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
4049 if (!ipvs
->tot_stats
.cpustats
)
4052 for_each_possible_cpu(i
) {
4053 struct ip_vs_cpu_stats
*ipvs_tot_stats
;
4054 ipvs_tot_stats
= per_cpu_ptr(ipvs
->tot_stats
.cpustats
, i
);
4055 u64_stats_init(&ipvs_tot_stats
->syncp
);
4058 spin_lock_init(&ipvs
->tot_stats
.lock
);
4060 proc_create("ip_vs", 0, ipvs
->net
->proc_net
, &ip_vs_info_fops
);
4061 proc_create("ip_vs_stats", 0, ipvs
->net
->proc_net
, &ip_vs_stats_fops
);
4062 proc_create("ip_vs_stats_percpu", 0, ipvs
->net
->proc_net
,
4063 &ip_vs_stats_percpu_fops
);
4065 if (ip_vs_control_net_init_sysctl(ipvs
))
4071 free_percpu(ipvs
->tot_stats
.cpustats
);
4075 void __net_exit
ip_vs_control_net_cleanup(struct netns_ipvs
*ipvs
)
4077 ip_vs_trash_cleanup(ipvs
);
4078 ip_vs_control_net_cleanup_sysctl(ipvs
);
4079 remove_proc_entry("ip_vs_stats_percpu", ipvs
->net
->proc_net
);
4080 remove_proc_entry("ip_vs_stats", ipvs
->net
->proc_net
);
4081 remove_proc_entry("ip_vs", ipvs
->net
->proc_net
);
4082 free_percpu(ipvs
->tot_stats
.cpustats
);
4085 int __init
ip_vs_register_nl_ioctl(void)
4089 ret
= nf_register_sockopt(&ip_vs_sockopts
);
4091 pr_err("cannot register sockopt.\n");
4095 ret
= ip_vs_genl_register();
4097 pr_err("cannot register Generic Netlink interface.\n");
4103 nf_unregister_sockopt(&ip_vs_sockopts
);
4108 void ip_vs_unregister_nl_ioctl(void)
4110 ip_vs_genl_unregister();
4111 nf_unregister_sockopt(&ip_vs_sockopts
);
4114 int __init
ip_vs_control_init(void)
4121 /* Initialize svc_table, ip_vs_svc_fwm_table */
4122 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
4123 INIT_HLIST_HEAD(&ip_vs_svc_table
[idx
]);
4124 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
4127 smp_wmb(); /* Do we really need it now ? */
4129 ret
= register_netdevice_notifier(&ip_vs_dst_notifier
);
4138 void ip_vs_control_cleanup(void)
4141 unregister_netdevice_notifier(&ip_vs_dst_notifier
);