2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/workqueue.h>
32 #include <linux/swap.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
47 #include <net/route.h>
49 #include <net/genetlink.h>
51 #include <asm/uaccess.h>
53 #include <net/ip_vs.h>
55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56 static DEFINE_MUTEX(__ip_vs_mutex
);
58 /* sysctl variables */
60 #ifdef CONFIG_IP_VS_DEBUG
61 static int sysctl_ip_vs_debug_level
= 0;
63 int ip_vs_get_debug_level(void)
65 return sysctl_ip_vs_debug_level
;
71 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
);
74 #ifdef CONFIG_IP_VS_IPV6
75 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
76 static bool __ip_vs_addr_is_local_v6(struct net
*net
,
77 const struct in6_addr
*addr
)
82 struct dst_entry
*dst
= ip6_route_output(net
, NULL
, &fl6
);
85 is_local
= !dst
->error
&& dst
->dev
&& (dst
->dev
->flags
& IFF_LOOPBACK
);
94 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
97 static void update_defense_level(struct netns_ipvs
*ipvs
)
100 static int old_secure_tcp
= 0;
105 /* we only count free and buffered memory (in pages) */
107 availmem
= i
.freeram
+ i
.bufferram
;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem
= (availmem
< ipvs
->sysctl_amemthresh
);
118 spin_lock(&ipvs
->dropentry_lock
);
119 switch (ipvs
->sysctl_drop_entry
) {
121 atomic_set(&ipvs
->dropentry
, 0);
125 atomic_set(&ipvs
->dropentry
, 1);
126 ipvs
->sysctl_drop_entry
= 2;
128 atomic_set(&ipvs
->dropentry
, 0);
133 atomic_set(&ipvs
->dropentry
, 1);
135 atomic_set(&ipvs
->dropentry
, 0);
136 ipvs
->sysctl_drop_entry
= 1;
140 atomic_set(&ipvs
->dropentry
, 1);
143 spin_unlock(&ipvs
->dropentry_lock
);
146 spin_lock(&ipvs
->droppacket_lock
);
147 switch (ipvs
->sysctl_drop_packet
) {
153 ipvs
->drop_rate
= ipvs
->drop_counter
154 = ipvs
->sysctl_amemthresh
/
155 (ipvs
->sysctl_amemthresh
-availmem
);
156 ipvs
->sysctl_drop_packet
= 2;
163 ipvs
->drop_rate
= ipvs
->drop_counter
164 = ipvs
->sysctl_amemthresh
/
165 (ipvs
->sysctl_amemthresh
-availmem
);
168 ipvs
->sysctl_drop_packet
= 1;
172 ipvs
->drop_rate
= ipvs
->sysctl_am_droprate
;
175 spin_unlock(&ipvs
->droppacket_lock
);
178 spin_lock(&ipvs
->securetcp_lock
);
179 switch (ipvs
->sysctl_secure_tcp
) {
181 if (old_secure_tcp
>= 2)
186 if (old_secure_tcp
< 2)
188 ipvs
->sysctl_secure_tcp
= 2;
190 if (old_secure_tcp
>= 2)
196 if (old_secure_tcp
< 2)
199 if (old_secure_tcp
>= 2)
201 ipvs
->sysctl_secure_tcp
= 1;
205 if (old_secure_tcp
< 2)
209 old_secure_tcp
= ipvs
->sysctl_secure_tcp
;
211 ip_vs_protocol_timeout_change(ipvs
,
212 ipvs
->sysctl_secure_tcp
> 1);
213 spin_unlock(&ipvs
->securetcp_lock
);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct
*work
)
226 struct netns_ipvs
*ipvs
=
227 container_of(work
, struct netns_ipvs
, defense_work
.work
);
229 update_defense_level(ipvs
);
230 if (atomic_read(&ipvs
->dropentry
))
231 ip_vs_random_dropentry(ipvs
->net
);
232 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE
);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE
);
250 * Hash table: for virtual service lookups
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct 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 %pF\n",
304 __func__
, __builtin_return_address(0));
308 if (svc
->fwmark
== 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash
= ip_vs_svc_hashkey(svc
->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 %pF\n",
338 __func__
, __builtin_return_address(0));
342 if (svc
->fwmark
== 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc
->s_list
);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc
->f_list
);
350 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
351 atomic_dec(&svc
->refcnt
);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service
*
360 __ip_vs_service_find(struct 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
);
430 && protocol
== IPPROTO_TCP
431 && atomic_read(&ipvs
->ftpsvc_counter
)
432 && (vport
== FTPDATA
|| ntohs(vport
) >= PROT_SOCK
)) {
434 * Check if ftp service entry exists, the packet
435 * might belong to FTP data connections.
437 svc
= __ip_vs_service_find(ipvs
, af
, protocol
, vaddr
, FTPPORT
);
441 && atomic_read(&ipvs
->nullsvc_counter
)) {
443 * Check if the catch-all port (port zero) exists
445 svc
= __ip_vs_service_find(ipvs
, af
, protocol
, vaddr
, 0);
449 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
450 fwmark
, ip_vs_proto_name(protocol
),
451 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
452 svc
? "hit" : "not hit");
459 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
461 atomic_inc(&svc
->refcnt
);
462 rcu_assign_pointer(dest
->svc
, svc
);
465 static void ip_vs_service_free(struct ip_vs_service
*svc
)
467 free_percpu(svc
->stats
.cpustats
);
471 static void ip_vs_service_rcu_free(struct rcu_head
*head
)
473 struct ip_vs_service
*svc
;
475 svc
= container_of(head
, struct ip_vs_service
, rcu_head
);
476 ip_vs_service_free(svc
);
479 static void __ip_vs_svc_put(struct ip_vs_service
*svc
, bool do_delay
)
481 if (atomic_dec_and_test(&svc
->refcnt
)) {
482 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
484 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
487 call_rcu(&svc
->rcu_head
, ip_vs_service_rcu_free
);
489 ip_vs_service_free(svc
);
495 * Returns hash value for real service
497 static inline unsigned int ip_vs_rs_hashkey(int af
,
498 const union nf_inet_addr
*addr
,
501 register unsigned int porth
= ntohs(port
);
502 __be32 addr_fold
= addr
->ip
;
504 #ifdef CONFIG_IP_VS_IPV6
506 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
507 addr
->ip6
[2]^addr
->ip6
[3];
510 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
514 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
515 static void ip_vs_rs_hash(struct netns_ipvs
*ipvs
, struct ip_vs_dest
*dest
)
519 if (dest
->in_rs_table
)
523 * Hash by proto,addr,port,
524 * which are the parameters of the real service.
526 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
528 hlist_add_head_rcu(&dest
->d_list
, &ipvs
->rs_table
[hash
]);
529 dest
->in_rs_table
= 1;
532 /* Unhash ip_vs_dest from rs_table. */
533 static void ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
536 * Remove it from the rs_table table.
538 if (dest
->in_rs_table
) {
539 hlist_del_rcu(&dest
->d_list
);
540 dest
->in_rs_table
= 0;
544 /* Check if real service by <proto,addr,port> is present */
545 bool ip_vs_has_real_service(struct netns_ipvs
*ipvs
, int af
, __u16 protocol
,
546 const union nf_inet_addr
*daddr
, __be16 dport
)
549 struct ip_vs_dest
*dest
;
551 /* Check for "full" addressed entries */
552 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
555 hlist_for_each_entry_rcu(dest
, &ipvs
->rs_table
[hash
], d_list
) {
556 if (dest
->port
== dport
&&
558 ip_vs_addr_equal(af
, &dest
->addr
, daddr
) &&
559 (dest
->protocol
== protocol
|| dest
->vfwmark
)) {
570 /* Lookup destination by {addr,port} in the given service
571 * Called under RCU lock.
573 static struct ip_vs_dest
*
574 ip_vs_lookup_dest(struct ip_vs_service
*svc
, int dest_af
,
575 const union nf_inet_addr
*daddr
, __be16 dport
)
577 struct ip_vs_dest
*dest
;
580 * Find the destination for the given service
582 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
583 if ((dest
->af
== dest_af
) &&
584 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
585 (dest
->port
== dport
)) {
595 * Find destination by {daddr,dport,vaddr,protocol}
596 * Created to be used in ip_vs_process_message() in
597 * the backup synchronization daemon. It finds the
598 * destination to be bound to the received connection
600 * Called under RCU lock, no refcnt is returned.
602 struct ip_vs_dest
*ip_vs_find_dest(struct netns_ipvs
*ipvs
, int svc_af
, int dest_af
,
603 const union nf_inet_addr
*daddr
,
605 const union nf_inet_addr
*vaddr
,
606 __be16 vport
, __u16 protocol
, __u32 fwmark
,
609 struct ip_vs_dest
*dest
;
610 struct ip_vs_service
*svc
;
613 svc
= ip_vs_service_find(ipvs
, svc_af
, fwmark
, protocol
, vaddr
, vport
);
616 if (fwmark
&& (flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
)
618 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
);
620 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
^ dport
);
624 void ip_vs_dest_dst_rcu_free(struct rcu_head
*head
)
626 struct ip_vs_dest_dst
*dest_dst
= container_of(head
,
627 struct ip_vs_dest_dst
,
630 dst_release(dest_dst
->dst_cache
);
634 /* Release dest_dst and dst_cache for dest in user context */
635 static void __ip_vs_dst_cache_reset(struct ip_vs_dest
*dest
)
637 struct ip_vs_dest_dst
*old
;
639 old
= rcu_dereference_protected(dest
->dest_dst
, 1);
641 RCU_INIT_POINTER(dest
->dest_dst
, NULL
);
642 call_rcu(&old
->rcu_head
, ip_vs_dest_dst_rcu_free
);
647 * Lookup dest by {svc,addr,port} in the destination trash.
648 * The destination trash is used to hold the destinations that are removed
649 * from the service table but are still referenced by some conn entries.
650 * The reason to add the destination trash is when the dest is temporary
651 * down (either by administrator or by monitor program), the dest can be
652 * picked back from the trash, the remaining connections to the dest can
653 * continue, and the counting information of the dest is also useful for
656 static struct ip_vs_dest
*
657 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, int dest_af
,
658 const union nf_inet_addr
*daddr
, __be16 dport
)
660 struct ip_vs_dest
*dest
;
661 struct netns_ipvs
*ipvs
= svc
->ipvs
;
664 * Find the destination in trash
666 spin_lock_bh(&ipvs
->dest_trash_lock
);
667 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
668 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
671 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
673 atomic_read(&dest
->refcnt
));
674 if (dest
->af
== dest_af
&&
675 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
676 dest
->port
== dport
&&
677 dest
->vfwmark
== svc
->fwmark
&&
678 dest
->protocol
== svc
->protocol
&&
680 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
681 dest
->vport
== svc
->port
))) {
683 list_del(&dest
->t_list
);
684 ip_vs_dest_hold(dest
);
692 spin_unlock_bh(&ipvs
->dest_trash_lock
);
697 static void ip_vs_dest_free(struct ip_vs_dest
*dest
)
699 struct ip_vs_service
*svc
= rcu_dereference_protected(dest
->svc
, 1);
701 __ip_vs_dst_cache_reset(dest
);
702 __ip_vs_svc_put(svc
, false);
703 free_percpu(dest
->stats
.cpustats
);
704 ip_vs_dest_put_and_free(dest
);
708 * Clean up all the destinations in the trash
709 * Called by the ip_vs_control_cleanup()
711 * When the ip_vs_control_clearup is activated by ipvs module exit,
712 * the service tables must have been flushed and all the connections
713 * are expired, and the refcnt of each destination in the trash must
714 * be 0, so we simply release them here.
716 static void ip_vs_trash_cleanup(struct net
*net
)
718 struct ip_vs_dest
*dest
, *nxt
;
719 struct netns_ipvs
*ipvs
= net_ipvs(net
);
721 del_timer_sync(&ipvs
->dest_trash_timer
);
722 /* No need to use dest_trash_lock */
723 list_for_each_entry_safe(dest
, nxt
, &ipvs
->dest_trash
, t_list
) {
724 list_del(&dest
->t_list
);
725 ip_vs_dest_free(dest
);
730 ip_vs_copy_stats(struct ip_vs_kstats
*dst
, struct ip_vs_stats
*src
)
732 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
734 spin_lock_bh(&src
->lock
);
736 IP_VS_SHOW_STATS_COUNTER(conns
);
737 IP_VS_SHOW_STATS_COUNTER(inpkts
);
738 IP_VS_SHOW_STATS_COUNTER(outpkts
);
739 IP_VS_SHOW_STATS_COUNTER(inbytes
);
740 IP_VS_SHOW_STATS_COUNTER(outbytes
);
742 ip_vs_read_estimator(dst
, src
);
744 spin_unlock_bh(&src
->lock
);
748 ip_vs_export_stats_user(struct ip_vs_stats_user
*dst
, struct ip_vs_kstats
*src
)
750 dst
->conns
= (u32
)src
->conns
;
751 dst
->inpkts
= (u32
)src
->inpkts
;
752 dst
->outpkts
= (u32
)src
->outpkts
;
753 dst
->inbytes
= src
->inbytes
;
754 dst
->outbytes
= src
->outbytes
;
755 dst
->cps
= (u32
)src
->cps
;
756 dst
->inpps
= (u32
)src
->inpps
;
757 dst
->outpps
= (u32
)src
->outpps
;
758 dst
->inbps
= (u32
)src
->inbps
;
759 dst
->outbps
= (u32
)src
->outbps
;
763 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
765 spin_lock_bh(&stats
->lock
);
767 /* get current counters as zero point, rates are zeroed */
769 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
771 IP_VS_ZERO_STATS_COUNTER(conns
);
772 IP_VS_ZERO_STATS_COUNTER(inpkts
);
773 IP_VS_ZERO_STATS_COUNTER(outpkts
);
774 IP_VS_ZERO_STATS_COUNTER(inbytes
);
775 IP_VS_ZERO_STATS_COUNTER(outbytes
);
777 ip_vs_zero_estimator(stats
);
779 spin_unlock_bh(&stats
->lock
);
783 * Update a destination in the given service
786 __ip_vs_update_dest(struct ip_vs_service
*svc
, struct ip_vs_dest
*dest
,
787 struct ip_vs_dest_user_kern
*udest
, int add
)
789 struct netns_ipvs
*ipvs
= svc
->ipvs
;
790 struct ip_vs_service
*old_svc
;
791 struct ip_vs_scheduler
*sched
;
794 /* We cannot modify an address and change the address family */
795 BUG_ON(!add
&& udest
->af
!= dest
->af
);
797 if (add
&& udest
->af
!= svc
->af
)
798 ipvs
->mixed_address_family_dests
++;
800 /* set the weight and the flags */
801 atomic_set(&dest
->weight
, udest
->weight
);
802 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
803 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
805 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
806 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
807 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
810 * Put the real service in rs_table if not present.
811 * For now only for NAT!
813 ip_vs_rs_hash(ipvs
, dest
);
815 atomic_set(&dest
->conn_flags
, conn_flags
);
817 /* bind the service */
818 old_svc
= rcu_dereference_protected(dest
->svc
, 1);
820 __ip_vs_bind_svc(dest
, svc
);
822 if (old_svc
!= svc
) {
823 ip_vs_zero_stats(&dest
->stats
);
824 __ip_vs_bind_svc(dest
, svc
);
825 __ip_vs_svc_put(old_svc
, true);
829 /* set the dest status flags */
830 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
832 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
833 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
834 dest
->u_threshold
= udest
->u_threshold
;
835 dest
->l_threshold
= udest
->l_threshold
;
837 dest
->af
= udest
->af
;
839 spin_lock_bh(&dest
->dst_lock
);
840 __ip_vs_dst_cache_reset(dest
);
841 spin_unlock_bh(&dest
->dst_lock
);
844 ip_vs_start_estimator(svc
->ipvs
->net
, &dest
->stats
);
845 list_add_rcu(&dest
->n_list
, &svc
->destinations
);
847 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
848 if (sched
&& sched
->add_dest
)
849 sched
->add_dest(svc
, dest
);
851 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
852 if (sched
&& sched
->upd_dest
)
853 sched
->upd_dest(svc
, dest
);
859 * Create a destination for the given service
862 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
863 struct ip_vs_dest
**dest_p
)
865 struct ip_vs_dest
*dest
;
866 unsigned int atype
, i
;
870 #ifdef CONFIG_IP_VS_IPV6
871 if (udest
->af
== AF_INET6
) {
872 atype
= ipv6_addr_type(&udest
->addr
.in6
);
873 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
874 atype
& IPV6_ADDR_LINKLOCAL
) &&
875 !__ip_vs_addr_is_local_v6(svc
->ipvs
->net
, &udest
->addr
.in6
))
880 atype
= inet_addr_type(svc
->ipvs
->net
, udest
->addr
.ip
);
881 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
885 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
889 dest
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
890 if (!dest
->stats
.cpustats
)
893 for_each_possible_cpu(i
) {
894 struct ip_vs_cpu_stats
*ip_vs_dest_stats
;
895 ip_vs_dest_stats
= per_cpu_ptr(dest
->stats
.cpustats
, i
);
896 u64_stats_init(&ip_vs_dest_stats
->syncp
);
899 dest
->af
= udest
->af
;
900 dest
->protocol
= svc
->protocol
;
901 dest
->vaddr
= svc
->addr
;
902 dest
->vport
= svc
->port
;
903 dest
->vfwmark
= svc
->fwmark
;
904 ip_vs_addr_copy(udest
->af
, &dest
->addr
, &udest
->addr
);
905 dest
->port
= udest
->port
;
907 atomic_set(&dest
->activeconns
, 0);
908 atomic_set(&dest
->inactconns
, 0);
909 atomic_set(&dest
->persistconns
, 0);
910 atomic_set(&dest
->refcnt
, 1);
912 INIT_HLIST_NODE(&dest
->d_list
);
913 spin_lock_init(&dest
->dst_lock
);
914 spin_lock_init(&dest
->stats
.lock
);
915 __ip_vs_update_dest(svc
, dest
, udest
, 1);
929 * Add a destination into an existing service
932 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
934 struct ip_vs_dest
*dest
;
935 union nf_inet_addr daddr
;
936 __be16 dport
= udest
->port
;
941 if (udest
->weight
< 0) {
942 pr_err("%s(): server weight less than zero\n", __func__
);
946 if (udest
->l_threshold
> udest
->u_threshold
) {
947 pr_err("%s(): lower threshold is higher than upper threshold\n",
952 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
954 /* We use function that requires RCU lock */
956 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
960 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
965 * Check if the dest already exists in the trash and
966 * is from the same service
968 dest
= ip_vs_trash_get_dest(svc
, udest
->af
, &daddr
, dport
);
971 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
972 "dest->refcnt=%d, service %u/%s:%u\n",
973 IP_VS_DBG_ADDR(udest
->af
, &daddr
), ntohs(dport
),
974 atomic_read(&dest
->refcnt
),
976 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
979 __ip_vs_update_dest(svc
, dest
, udest
, 1);
983 * Allocate and initialize the dest structure
985 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
994 * Edit a destination in the given service
997 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
999 struct ip_vs_dest
*dest
;
1000 union nf_inet_addr daddr
;
1001 __be16 dport
= udest
->port
;
1005 if (udest
->weight
< 0) {
1006 pr_err("%s(): server weight less than zero\n", __func__
);
1010 if (udest
->l_threshold
> udest
->u_threshold
) {
1011 pr_err("%s(): lower threshold is higher than upper threshold\n",
1016 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
1018 /* We use function that requires RCU lock */
1020 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
1024 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1028 __ip_vs_update_dest(svc
, dest
, udest
, 0);
1035 * Delete a destination (must be already unlinked from the service)
1037 static void __ip_vs_del_dest(struct net
*net
, struct ip_vs_dest
*dest
,
1040 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1042 ip_vs_stop_estimator(net
, &dest
->stats
);
1045 * Remove it from the d-linked list with the real services.
1047 ip_vs_rs_unhash(dest
);
1049 spin_lock_bh(&ipvs
->dest_trash_lock
);
1050 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1051 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
), ntohs(dest
->port
),
1052 atomic_read(&dest
->refcnt
));
1053 if (list_empty(&ipvs
->dest_trash
) && !cleanup
)
1054 mod_timer(&ipvs
->dest_trash_timer
,
1055 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1056 /* dest lives in trash without reference */
1057 list_add(&dest
->t_list
, &ipvs
->dest_trash
);
1058 dest
->idle_start
= 0;
1059 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1060 ip_vs_dest_put(dest
);
1065 * Unlink a destination from the given service
1067 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1068 struct ip_vs_dest
*dest
,
1071 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1074 * Remove it from the d-linked destination list.
1076 list_del_rcu(&dest
->n_list
);
1079 if (dest
->af
!= svc
->af
)
1080 svc
->ipvs
->mixed_address_family_dests
--;
1083 struct ip_vs_scheduler
*sched
;
1085 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1086 if (sched
&& sched
->del_dest
)
1087 sched
->del_dest(svc
, dest
);
1093 * Delete a destination server in the given service
1096 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1098 struct ip_vs_dest
*dest
;
1099 __be16 dport
= udest
->port
;
1103 /* We use function that requires RCU lock */
1105 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &udest
->addr
, dport
);
1109 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1114 * Unlink dest from the service
1116 __ip_vs_unlink_dest(svc
, dest
, 1);
1119 * Delete the destination
1121 __ip_vs_del_dest(svc
->ipvs
->net
, dest
, false);
1128 static void ip_vs_dest_trash_expire(unsigned long data
)
1130 struct net
*net
= (struct net
*) data
;
1131 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1132 struct ip_vs_dest
*dest
, *next
;
1133 unsigned long now
= jiffies
;
1135 spin_lock(&ipvs
->dest_trash_lock
);
1136 list_for_each_entry_safe(dest
, next
, &ipvs
->dest_trash
, t_list
) {
1137 if (atomic_read(&dest
->refcnt
) > 0)
1139 if (dest
->idle_start
) {
1140 if (time_before(now
, dest
->idle_start
+
1141 IP_VS_DEST_TRASH_PERIOD
))
1144 dest
->idle_start
= max(1UL, now
);
1147 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1149 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1151 list_del(&dest
->t_list
);
1152 ip_vs_dest_free(dest
);
1154 if (!list_empty(&ipvs
->dest_trash
))
1155 mod_timer(&ipvs
->dest_trash_timer
,
1156 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1157 spin_unlock(&ipvs
->dest_trash_lock
);
1161 * Add a service into the service hash table
1164 ip_vs_add_service(struct net
*net
, struct ip_vs_service_user_kern
*u
,
1165 struct ip_vs_service
**svc_p
)
1168 struct ip_vs_scheduler
*sched
= NULL
;
1169 struct ip_vs_pe
*pe
= NULL
;
1170 struct ip_vs_service
*svc
= NULL
;
1171 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1173 /* increase the module use count */
1174 ip_vs_use_count_inc();
1176 /* Lookup the scheduler by 'u->sched_name' */
1177 if (strcmp(u
->sched_name
, "none")) {
1178 sched
= ip_vs_scheduler_get(u
->sched_name
);
1180 pr_info("Scheduler module ip_vs_%s not found\n",
1187 if (u
->pe_name
&& *u
->pe_name
) {
1188 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1190 pr_info("persistence engine module ip_vs_pe_%s "
1191 "not found\n", u
->pe_name
);
1197 #ifdef CONFIG_IP_VS_IPV6
1198 if (u
->af
== AF_INET6
) {
1199 __u32 plen
= (__force __u32
) u
->netmask
;
1201 if (plen
< 1 || plen
> 128) {
1208 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1210 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1214 svc
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
1215 if (!svc
->stats
.cpustats
) {
1220 for_each_possible_cpu(i
) {
1221 struct ip_vs_cpu_stats
*ip_vs_stats
;
1222 ip_vs_stats
= per_cpu_ptr(svc
->stats
.cpustats
, i
);
1223 u64_stats_init(&ip_vs_stats
->syncp
);
1227 /* I'm the first user of the service */
1228 atomic_set(&svc
->refcnt
, 0);
1231 svc
->protocol
= u
->protocol
;
1232 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1233 svc
->port
= u
->port
;
1234 svc
->fwmark
= u
->fwmark
;
1235 svc
->flags
= u
->flags
;
1236 svc
->timeout
= u
->timeout
* HZ
;
1237 svc
->netmask
= u
->netmask
;
1240 INIT_LIST_HEAD(&svc
->destinations
);
1241 spin_lock_init(&svc
->sched_lock
);
1242 spin_lock_init(&svc
->stats
.lock
);
1244 /* Bind the scheduler */
1246 ret
= ip_vs_bind_scheduler(svc
, sched
);
1252 /* Bind the ct retriever */
1253 RCU_INIT_POINTER(svc
->pe
, pe
);
1256 /* Update the virtual service counters */
1257 if (svc
->port
== FTPPORT
)
1258 atomic_inc(&ipvs
->ftpsvc_counter
);
1259 else if (svc
->port
== 0)
1260 atomic_inc(&ipvs
->nullsvc_counter
);
1262 ip_vs_start_estimator(net
, &svc
->stats
);
1264 /* Count only IPv4 services for old get/setsockopt interface */
1265 if (svc
->af
== AF_INET
)
1266 ipvs
->num_services
++;
1268 /* Hash the service into the service table */
1269 ip_vs_svc_hash(svc
);
1272 /* Now there is a service - full throttle */
1279 ip_vs_unbind_scheduler(svc
, sched
);
1280 ip_vs_service_free(svc
);
1282 ip_vs_scheduler_put(sched
);
1285 /* decrease the module use count */
1286 ip_vs_use_count_dec();
1293 * Edit a service and bind it with a new scheduler
1296 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1298 struct ip_vs_scheduler
*sched
= NULL
, *old_sched
;
1299 struct ip_vs_pe
*pe
= NULL
, *old_pe
= NULL
;
1303 * Lookup the scheduler, by 'u->sched_name'
1305 if (strcmp(u
->sched_name
, "none")) {
1306 sched
= ip_vs_scheduler_get(u
->sched_name
);
1308 pr_info("Scheduler module ip_vs_%s not found\n",
1315 if (u
->pe_name
&& *u
->pe_name
) {
1316 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1318 pr_info("persistence engine module ip_vs_pe_%s "
1319 "not found\n", u
->pe_name
);
1326 #ifdef CONFIG_IP_VS_IPV6
1327 if (u
->af
== AF_INET6
) {
1328 __u32 plen
= (__force __u32
) u
->netmask
;
1330 if (plen
< 1 || plen
> 128) {
1337 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1338 if (sched
!= old_sched
) {
1340 ip_vs_unbind_scheduler(svc
, old_sched
);
1341 RCU_INIT_POINTER(svc
->scheduler
, NULL
);
1342 /* Wait all svc->sched_data users */
1345 /* Bind the new scheduler */
1347 ret
= ip_vs_bind_scheduler(svc
, sched
);
1349 ip_vs_scheduler_put(sched
);
1356 * Set the flags and timeout value
1358 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1359 svc
->timeout
= u
->timeout
* HZ
;
1360 svc
->netmask
= u
->netmask
;
1362 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1364 rcu_assign_pointer(svc
->pe
, pe
);
1367 ip_vs_scheduler_put(old_sched
);
1368 ip_vs_pe_put(old_pe
);
1373 * Delete a service from the service list
1374 * - The service must be unlinked, unlocked and not referenced!
1375 * - We are called under _bh lock
1377 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
)
1379 struct ip_vs_dest
*dest
, *nxt
;
1380 struct ip_vs_scheduler
*old_sched
;
1381 struct ip_vs_pe
*old_pe
;
1382 struct netns_ipvs
*ipvs
= svc
->ipvs
;
1384 pr_info("%s: enter\n", __func__
);
1386 /* Count only IPv4 services for old get/setsockopt interface */
1387 if (svc
->af
== AF_INET
)
1388 ipvs
->num_services
--;
1390 ip_vs_stop_estimator(svc
->ipvs
->net
, &svc
->stats
);
1392 /* Unbind scheduler */
1393 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1394 ip_vs_unbind_scheduler(svc
, old_sched
);
1395 ip_vs_scheduler_put(old_sched
);
1397 /* Unbind persistence engine, keep svc->pe */
1398 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1399 ip_vs_pe_put(old_pe
);
1402 * Unlink the whole destination list
1404 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1405 __ip_vs_unlink_dest(svc
, dest
, 0);
1406 __ip_vs_del_dest(svc
->ipvs
->net
, dest
, cleanup
);
1410 * Update the virtual service counters
1412 if (svc
->port
== FTPPORT
)
1413 atomic_dec(&ipvs
->ftpsvc_counter
);
1414 else if (svc
->port
== 0)
1415 atomic_dec(&ipvs
->nullsvc_counter
);
1418 * Free the service if nobody refers to it
1420 __ip_vs_svc_put(svc
, true);
1422 /* decrease the module use count */
1423 ip_vs_use_count_dec();
1427 * Unlink a service from list and try to delete it if its refcnt reached 0
1429 static void ip_vs_unlink_service(struct ip_vs_service
*svc
, bool cleanup
)
1431 /* Hold svc to avoid double release from dest_trash */
1432 atomic_inc(&svc
->refcnt
);
1434 * Unhash it from the service table
1436 ip_vs_svc_unhash(svc
);
1438 __ip_vs_del_service(svc
, cleanup
);
1442 * Delete a service from the service list
1444 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1448 ip_vs_unlink_service(svc
, false);
1455 * Flush all the virtual services
1457 static int ip_vs_flush(struct net
*net
, bool cleanup
)
1459 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1461 struct ip_vs_service
*svc
;
1462 struct hlist_node
*n
;
1465 * Flush the service table hashed by <netns,protocol,addr,port>
1467 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1468 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_table
[idx
],
1470 if (svc
->ipvs
== ipvs
)
1471 ip_vs_unlink_service(svc
, cleanup
);
1476 * Flush the service table hashed by fwmark
1478 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1479 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_fwm_table
[idx
],
1481 if (svc
->ipvs
== ipvs
)
1482 ip_vs_unlink_service(svc
, cleanup
);
1490 * Delete service by {netns} in the service table.
1491 * Called by __ip_vs_cleanup()
1493 void ip_vs_service_net_cleanup(struct net
*net
)
1496 /* Check for "full" addressed entries */
1497 mutex_lock(&__ip_vs_mutex
);
1498 ip_vs_flush(net
, true);
1499 mutex_unlock(&__ip_vs_mutex
);
1503 /* Put all references for device (dst_cache) */
1505 ip_vs_forget_dev(struct ip_vs_dest
*dest
, struct net_device
*dev
)
1507 struct ip_vs_dest_dst
*dest_dst
;
1509 spin_lock_bh(&dest
->dst_lock
);
1510 dest_dst
= rcu_dereference_protected(dest
->dest_dst
, 1);
1511 if (dest_dst
&& dest_dst
->dst_cache
->dev
== dev
) {
1512 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1514 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1516 atomic_read(&dest
->refcnt
));
1517 __ip_vs_dst_cache_reset(dest
);
1519 spin_unlock_bh(&dest
->dst_lock
);
1522 /* Netdev event receiver
1523 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1525 static int ip_vs_dst_event(struct notifier_block
*this, unsigned long event
,
1528 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1529 struct net
*net
= dev_net(dev
);
1530 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1531 struct ip_vs_service
*svc
;
1532 struct ip_vs_dest
*dest
;
1535 if (event
!= NETDEV_DOWN
|| !ipvs
)
1537 IP_VS_DBG(3, "%s() dev=%s\n", __func__
, dev
->name
);
1539 mutex_lock(&__ip_vs_mutex
);
1540 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1541 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1542 if (svc
->ipvs
== ipvs
) {
1543 list_for_each_entry(dest
, &svc
->destinations
,
1545 ip_vs_forget_dev(dest
, dev
);
1550 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1551 if (svc
->ipvs
== ipvs
) {
1552 list_for_each_entry(dest
, &svc
->destinations
,
1554 ip_vs_forget_dev(dest
, dev
);
1561 spin_lock_bh(&ipvs
->dest_trash_lock
);
1562 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
1563 ip_vs_forget_dev(dest
, dev
);
1565 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1566 mutex_unlock(&__ip_vs_mutex
);
1572 * Zero counters in a service or all services
1574 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1576 struct ip_vs_dest
*dest
;
1578 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1579 ip_vs_zero_stats(&dest
->stats
);
1581 ip_vs_zero_stats(&svc
->stats
);
1585 static int ip_vs_zero_all(struct net
*net
)
1587 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1589 struct ip_vs_service
*svc
;
1591 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1592 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1593 if (svc
->ipvs
== ipvs
)
1594 ip_vs_zero_service(svc
);
1598 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1599 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1600 if (svc
->ipvs
== ipvs
)
1601 ip_vs_zero_service(svc
);
1605 ip_vs_zero_stats(&ipvs
->tot_stats
);
1609 #ifdef CONFIG_SYSCTL
1612 static int three
= 3;
1615 proc_do_defense_mode(struct ctl_table
*table
, int write
,
1616 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1618 struct netns_ipvs
*ipvs
= table
->extra2
;
1619 int *valp
= table
->data
;
1623 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1624 if (write
&& (*valp
!= val
)) {
1625 if ((*valp
< 0) || (*valp
> 3)) {
1626 /* Restore the correct value */
1629 update_defense_level(ipvs
);
1636 proc_do_sync_threshold(struct ctl_table
*table
, int write
,
1637 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1639 int *valp
= table
->data
;
1643 /* backup the value first */
1644 memcpy(val
, valp
, sizeof(val
));
1646 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1647 if (write
&& (valp
[0] < 0 || valp
[1] < 0 ||
1648 (valp
[0] >= valp
[1] && valp
[1]))) {
1649 /* Restore the correct value */
1650 memcpy(valp
, val
, sizeof(val
));
1656 proc_do_sync_mode(struct ctl_table
*table
, int write
,
1657 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1659 int *valp
= table
->data
;
1663 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1664 if (write
&& (*valp
!= val
)) {
1665 if ((*valp
< 0) || (*valp
> 1)) {
1666 /* Restore the correct value */
1674 proc_do_sync_ports(struct ctl_table
*table
, int write
,
1675 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1677 int *valp
= table
->data
;
1681 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1682 if (write
&& (*valp
!= val
)) {
1683 if (*valp
< 1 || !is_power_of_2(*valp
)) {
1684 /* Restore the correct value */
1692 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1693 * Do not change order or insert new entries without
1694 * align with netns init in ip_vs_control_net_init()
1697 static struct ctl_table vs_vars
[] = {
1699 .procname
= "amemthresh",
1700 .maxlen
= sizeof(int),
1702 .proc_handler
= proc_dointvec
,
1705 .procname
= "am_droprate",
1706 .maxlen
= sizeof(int),
1708 .proc_handler
= proc_dointvec
,
1711 .procname
= "drop_entry",
1712 .maxlen
= sizeof(int),
1714 .proc_handler
= proc_do_defense_mode
,
1717 .procname
= "drop_packet",
1718 .maxlen
= sizeof(int),
1720 .proc_handler
= proc_do_defense_mode
,
1722 #ifdef CONFIG_IP_VS_NFCT
1724 .procname
= "conntrack",
1725 .maxlen
= sizeof(int),
1727 .proc_handler
= &proc_dointvec
,
1731 .procname
= "secure_tcp",
1732 .maxlen
= sizeof(int),
1734 .proc_handler
= proc_do_defense_mode
,
1737 .procname
= "snat_reroute",
1738 .maxlen
= sizeof(int),
1740 .proc_handler
= &proc_dointvec
,
1743 .procname
= "sync_version",
1744 .maxlen
= sizeof(int),
1746 .proc_handler
= &proc_do_sync_mode
,
1749 .procname
= "sync_ports",
1750 .maxlen
= sizeof(int),
1752 .proc_handler
= &proc_do_sync_ports
,
1755 .procname
= "sync_persist_mode",
1756 .maxlen
= sizeof(int),
1758 .proc_handler
= proc_dointvec
,
1761 .procname
= "sync_qlen_max",
1762 .maxlen
= sizeof(unsigned long),
1764 .proc_handler
= proc_doulongvec_minmax
,
1767 .procname
= "sync_sock_size",
1768 .maxlen
= sizeof(int),
1770 .proc_handler
= proc_dointvec
,
1773 .procname
= "cache_bypass",
1774 .maxlen
= sizeof(int),
1776 .proc_handler
= proc_dointvec
,
1779 .procname
= "expire_nodest_conn",
1780 .maxlen
= sizeof(int),
1782 .proc_handler
= proc_dointvec
,
1785 .procname
= "sloppy_tcp",
1786 .maxlen
= sizeof(int),
1788 .proc_handler
= proc_dointvec
,
1791 .procname
= "sloppy_sctp",
1792 .maxlen
= sizeof(int),
1794 .proc_handler
= proc_dointvec
,
1797 .procname
= "expire_quiescent_template",
1798 .maxlen
= sizeof(int),
1800 .proc_handler
= proc_dointvec
,
1803 .procname
= "sync_threshold",
1805 sizeof(((struct netns_ipvs
*)0)->sysctl_sync_threshold
),
1807 .proc_handler
= proc_do_sync_threshold
,
1810 .procname
= "sync_refresh_period",
1811 .maxlen
= sizeof(int),
1813 .proc_handler
= proc_dointvec_jiffies
,
1816 .procname
= "sync_retries",
1817 .maxlen
= sizeof(int),
1819 .proc_handler
= proc_dointvec_minmax
,
1824 .procname
= "nat_icmp_send",
1825 .maxlen
= sizeof(int),
1827 .proc_handler
= proc_dointvec
,
1830 .procname
= "pmtu_disc",
1831 .maxlen
= sizeof(int),
1833 .proc_handler
= proc_dointvec
,
1836 .procname
= "backup_only",
1837 .maxlen
= sizeof(int),
1839 .proc_handler
= proc_dointvec
,
1842 .procname
= "conn_reuse_mode",
1843 .maxlen
= sizeof(int),
1845 .proc_handler
= proc_dointvec
,
1848 .procname
= "schedule_icmp",
1849 .maxlen
= sizeof(int),
1851 .proc_handler
= proc_dointvec
,
1854 .procname
= "ignore_tunneled",
1855 .maxlen
= sizeof(int),
1857 .proc_handler
= proc_dointvec
,
1859 #ifdef CONFIG_IP_VS_DEBUG
1861 .procname
= "debug_level",
1862 .data
= &sysctl_ip_vs_debug_level
,
1863 .maxlen
= sizeof(int),
1865 .proc_handler
= proc_dointvec
,
1873 #ifdef CONFIG_PROC_FS
1876 struct seq_net_private p
; /* Do not move this, netns depends upon it*/
1877 struct hlist_head
*table
;
1882 * Write the contents of the VS rule table to a PROCfs file.
1883 * (It is kept just for backward compatibility)
1885 static inline const char *ip_vs_fwd_name(unsigned int flags
)
1887 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1888 case IP_VS_CONN_F_LOCALNODE
:
1890 case IP_VS_CONN_F_TUNNEL
:
1892 case IP_VS_CONN_F_DROUTE
:
1900 /* Get the Nth entry in the two lists */
1901 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1903 struct net
*net
= seq_file_net(seq
);
1904 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1905 struct ip_vs_iter
*iter
= seq
->private;
1907 struct ip_vs_service
*svc
;
1909 /* look in hash by protocol */
1910 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1911 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1912 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1913 iter
->table
= ip_vs_svc_table
;
1920 /* keep looking in fwmark */
1921 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1922 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[idx
],
1924 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1925 iter
->table
= ip_vs_svc_fwm_table
;
1935 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1939 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1943 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1945 struct hlist_node
*e
;
1946 struct ip_vs_iter
*iter
;
1947 struct ip_vs_service
*svc
;
1950 if (v
== SEQ_START_TOKEN
)
1951 return ip_vs_info_array(seq
,0);
1954 iter
= seq
->private;
1956 if (iter
->table
== ip_vs_svc_table
) {
1957 /* next service in table hashed by protocol */
1958 e
= rcu_dereference(hlist_next_rcu(&svc
->s_list
));
1960 return hlist_entry(e
, struct ip_vs_service
, s_list
);
1962 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1963 hlist_for_each_entry_rcu(svc
,
1964 &ip_vs_svc_table
[iter
->bucket
],
1970 iter
->table
= ip_vs_svc_fwm_table
;
1975 /* next service in hashed by fwmark */
1976 e
= rcu_dereference(hlist_next_rcu(&svc
->f_list
));
1978 return hlist_entry(e
, struct ip_vs_service
, f_list
);
1981 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1982 hlist_for_each_entry_rcu(svc
,
1983 &ip_vs_svc_fwm_table
[iter
->bucket
],
1991 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
1998 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
2000 if (v
== SEQ_START_TOKEN
) {
2002 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2003 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2005 "Prot LocalAddress:Port Scheduler Flags\n");
2007 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2009 const struct ip_vs_service
*svc
= v
;
2010 const struct ip_vs_iter
*iter
= seq
->private;
2011 const struct ip_vs_dest
*dest
;
2012 struct ip_vs_scheduler
*sched
= rcu_dereference(svc
->scheduler
);
2013 char *sched_name
= sched
? sched
->name
: "none";
2015 if (iter
->table
== ip_vs_svc_table
) {
2016 #ifdef CONFIG_IP_VS_IPV6
2017 if (svc
->af
== AF_INET6
)
2018 seq_printf(seq
, "%s [%pI6]:%04X %s ",
2019 ip_vs_proto_name(svc
->protocol
),
2025 seq_printf(seq
, "%s %08X:%04X %s %s ",
2026 ip_vs_proto_name(svc
->protocol
),
2027 ntohl(svc
->addr
.ip
),
2030 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2032 seq_printf(seq
, "FWM %08X %s %s",
2033 svc
->fwmark
, sched_name
,
2034 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2037 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
2038 seq_printf(seq
, "persistent %d %08X\n",
2040 ntohl(svc
->netmask
));
2042 seq_putc(seq
, '\n');
2044 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
2045 #ifdef CONFIG_IP_VS_IPV6
2046 if (dest
->af
== AF_INET6
)
2049 " %-7s %-6d %-10d %-10d\n",
2052 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2053 atomic_read(&dest
->weight
),
2054 atomic_read(&dest
->activeconns
),
2055 atomic_read(&dest
->inactconns
));
2060 "%-7s %-6d %-10d %-10d\n",
2061 ntohl(dest
->addr
.ip
),
2063 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2064 atomic_read(&dest
->weight
),
2065 atomic_read(&dest
->activeconns
),
2066 atomic_read(&dest
->inactconns
));
2073 static const struct seq_operations ip_vs_info_seq_ops
= {
2074 .start
= ip_vs_info_seq_start
,
2075 .next
= ip_vs_info_seq_next
,
2076 .stop
= ip_vs_info_seq_stop
,
2077 .show
= ip_vs_info_seq_show
,
2080 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
2082 return seq_open_net(inode
, file
, &ip_vs_info_seq_ops
,
2083 sizeof(struct ip_vs_iter
));
2086 static const struct file_operations ip_vs_info_fops
= {
2087 .owner
= THIS_MODULE
,
2088 .open
= ip_vs_info_open
,
2090 .llseek
= seq_lseek
,
2091 .release
= seq_release_net
,
2094 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
2096 struct net
*net
= seq_file_single_net(seq
);
2097 struct ip_vs_kstats show
;
2099 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2101 " Total Incoming Outgoing Incoming Outgoing\n");
2103 " Conns Packets Packets Bytes Bytes\n");
2105 ip_vs_copy_stats(&show
, &net_ipvs(net
)->tot_stats
);
2106 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n\n",
2107 (unsigned long long)show
.conns
,
2108 (unsigned long long)show
.inpkts
,
2109 (unsigned long long)show
.outpkts
,
2110 (unsigned long long)show
.inbytes
,
2111 (unsigned long long)show
.outbytes
);
2113 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2115 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2116 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n",
2117 (unsigned long long)show
.cps
,
2118 (unsigned long long)show
.inpps
,
2119 (unsigned long long)show
.outpps
,
2120 (unsigned long long)show
.inbps
,
2121 (unsigned long long)show
.outbps
);
2126 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
2128 return single_open_net(inode
, file
, ip_vs_stats_show
);
2131 static const struct file_operations ip_vs_stats_fops
= {
2132 .owner
= THIS_MODULE
,
2133 .open
= ip_vs_stats_seq_open
,
2135 .llseek
= seq_lseek
,
2136 .release
= single_release_net
,
2139 static int ip_vs_stats_percpu_show(struct seq_file
*seq
, void *v
)
2141 struct net
*net
= seq_file_single_net(seq
);
2142 struct ip_vs_stats
*tot_stats
= &net_ipvs(net
)->tot_stats
;
2143 struct ip_vs_cpu_stats __percpu
*cpustats
= tot_stats
->cpustats
;
2144 struct ip_vs_kstats kstats
;
2147 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2149 " Total Incoming Outgoing Incoming Outgoing\n");
2151 "CPU Conns Packets Packets Bytes Bytes\n");
2153 for_each_possible_cpu(i
) {
2154 struct ip_vs_cpu_stats
*u
= per_cpu_ptr(cpustats
, i
);
2156 u64 conns
, inpkts
, outpkts
, inbytes
, outbytes
;
2159 start
= u64_stats_fetch_begin_irq(&u
->syncp
);
2160 conns
= u
->cnt
.conns
;
2161 inpkts
= u
->cnt
.inpkts
;
2162 outpkts
= u
->cnt
.outpkts
;
2163 inbytes
= u
->cnt
.inbytes
;
2164 outbytes
= u
->cnt
.outbytes
;
2165 } while (u64_stats_fetch_retry_irq(&u
->syncp
, start
));
2167 seq_printf(seq
, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2168 i
, (u64
)conns
, (u64
)inpkts
,
2169 (u64
)outpkts
, (u64
)inbytes
,
2173 ip_vs_copy_stats(&kstats
, tot_stats
);
2175 seq_printf(seq
, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2176 (unsigned long long)kstats
.conns
,
2177 (unsigned long long)kstats
.inpkts
,
2178 (unsigned long long)kstats
.outpkts
,
2179 (unsigned long long)kstats
.inbytes
,
2180 (unsigned long long)kstats
.outbytes
);
2182 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2184 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2185 seq_printf(seq
, " %8LX %8LX %8LX %16LX %16LX\n",
2195 static int ip_vs_stats_percpu_seq_open(struct inode
*inode
, struct file
*file
)
2197 return single_open_net(inode
, file
, ip_vs_stats_percpu_show
);
2200 static const struct file_operations ip_vs_stats_percpu_fops
= {
2201 .owner
= THIS_MODULE
,
2202 .open
= ip_vs_stats_percpu_seq_open
,
2204 .llseek
= seq_lseek
,
2205 .release
= single_release_net
,
2210 * Set timeout values for tcp tcpfin udp in the timeout_table.
2212 static int ip_vs_set_timeout(struct net
*net
, struct ip_vs_timeout_user
*u
)
2214 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2215 struct ip_vs_proto_data
*pd
;
2218 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2223 #ifdef CONFIG_IP_VS_PROTO_TCP
2224 if (u
->tcp_timeout
) {
2225 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2226 pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
2227 = u
->tcp_timeout
* HZ
;
2230 if (u
->tcp_fin_timeout
) {
2231 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2232 pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
2233 = u
->tcp_fin_timeout
* HZ
;
2237 #ifdef CONFIG_IP_VS_PROTO_UDP
2238 if (u
->udp_timeout
) {
2239 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2240 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
]
2241 = u
->udp_timeout
* HZ
;
2247 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2249 struct ip_vs_svcdest_user
{
2250 struct ip_vs_service_user s
;
2251 struct ip_vs_dest_user d
;
2254 static const unsigned char set_arglen
[CMDID(IP_VS_SO_SET_MAX
) + 1] = {
2255 [CMDID(IP_VS_SO_SET_ADD
)] = sizeof(struct ip_vs_service_user
),
2256 [CMDID(IP_VS_SO_SET_EDIT
)] = sizeof(struct ip_vs_service_user
),
2257 [CMDID(IP_VS_SO_SET_DEL
)] = sizeof(struct ip_vs_service_user
),
2258 [CMDID(IP_VS_SO_SET_ADDDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2259 [CMDID(IP_VS_SO_SET_DELDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2260 [CMDID(IP_VS_SO_SET_EDITDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2261 [CMDID(IP_VS_SO_SET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2262 [CMDID(IP_VS_SO_SET_STARTDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2263 [CMDID(IP_VS_SO_SET_STOPDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2264 [CMDID(IP_VS_SO_SET_ZERO
)] = sizeof(struct ip_vs_service_user
),
2267 union ip_vs_set_arglen
{
2268 struct ip_vs_service_user field_IP_VS_SO_SET_ADD
;
2269 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT
;
2270 struct ip_vs_service_user field_IP_VS_SO_SET_DEL
;
2271 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST
;
2272 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST
;
2273 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST
;
2274 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT
;
2275 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON
;
2276 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON
;
2277 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO
;
2280 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2282 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2283 struct ip_vs_service_user
*usvc_compat
)
2285 memset(usvc
, 0, sizeof(*usvc
));
2288 usvc
->protocol
= usvc_compat
->protocol
;
2289 usvc
->addr
.ip
= usvc_compat
->addr
;
2290 usvc
->port
= usvc_compat
->port
;
2291 usvc
->fwmark
= usvc_compat
->fwmark
;
2293 /* Deep copy of sched_name is not needed here */
2294 usvc
->sched_name
= usvc_compat
->sched_name
;
2296 usvc
->flags
= usvc_compat
->flags
;
2297 usvc
->timeout
= usvc_compat
->timeout
;
2298 usvc
->netmask
= usvc_compat
->netmask
;
2301 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2302 struct ip_vs_dest_user
*udest_compat
)
2304 memset(udest
, 0, sizeof(*udest
));
2306 udest
->addr
.ip
= udest_compat
->addr
;
2307 udest
->port
= udest_compat
->port
;
2308 udest
->conn_flags
= udest_compat
->conn_flags
;
2309 udest
->weight
= udest_compat
->weight
;
2310 udest
->u_threshold
= udest_compat
->u_threshold
;
2311 udest
->l_threshold
= udest_compat
->l_threshold
;
2312 udest
->af
= AF_INET
;
2316 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2318 struct net
*net
= sock_net(sk
);
2320 unsigned char arg
[MAX_SET_ARGLEN
];
2321 struct ip_vs_service_user
*usvc_compat
;
2322 struct ip_vs_service_user_kern usvc
;
2323 struct ip_vs_service
*svc
;
2324 struct ip_vs_dest_user
*udest_compat
;
2325 struct ip_vs_dest_user_kern udest
;
2326 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2328 BUILD_BUG_ON(sizeof(arg
) > 255);
2329 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2332 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2334 if (len
!= set_arglen
[CMDID(cmd
)]) {
2335 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2336 len
, set_arglen
[CMDID(cmd
)]);
2340 if (copy_from_user(arg
, user
, len
) != 0)
2343 /* increase the module use count */
2344 ip_vs_use_count_inc();
2346 /* Handle daemons since they have another lock */
2347 if (cmd
== IP_VS_SO_SET_STARTDAEMON
||
2348 cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2349 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2351 if (cmd
== IP_VS_SO_SET_STARTDAEMON
) {
2352 struct ipvs_sync_daemon_cfg cfg
;
2354 memset(&cfg
, 0, sizeof(cfg
));
2355 strlcpy(cfg
.mcast_ifn
, dm
->mcast_ifn
,
2356 sizeof(cfg
.mcast_ifn
));
2357 cfg
.syncid
= dm
->syncid
;
2359 mutex_lock(&ipvs
->sync_mutex
);
2360 ret
= start_sync_thread(net
, &cfg
, dm
->state
);
2361 mutex_unlock(&ipvs
->sync_mutex
);
2364 mutex_lock(&ipvs
->sync_mutex
);
2365 ret
= stop_sync_thread(net
, dm
->state
);
2366 mutex_unlock(&ipvs
->sync_mutex
);
2371 mutex_lock(&__ip_vs_mutex
);
2372 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2373 /* Flush the virtual service */
2374 ret
= ip_vs_flush(net
, false);
2376 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2377 /* Set timeout values for (tcp tcpfin udp) */
2378 ret
= ip_vs_set_timeout(net
, (struct ip_vs_timeout_user
*)arg
);
2382 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2383 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2385 /* We only use the new structs internally, so copy userspace compat
2386 * structs to extended internal versions */
2387 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2388 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2390 if (cmd
== IP_VS_SO_SET_ZERO
) {
2391 /* if no service address is set, zero counters in all */
2392 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2393 ret
= ip_vs_zero_all(net
);
2398 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2399 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2400 usvc
.protocol
!= IPPROTO_SCTP
) {
2401 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2402 usvc
.protocol
, &usvc
.addr
.ip
,
2403 ntohs(usvc
.port
), usvc
.sched_name
);
2408 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2410 if (usvc
.fwmark
== 0)
2411 svc
= __ip_vs_service_find(ipvs
, usvc
.af
, usvc
.protocol
,
2412 &usvc
.addr
, usvc
.port
);
2414 svc
= __ip_vs_svc_fwm_find(ipvs
, usvc
.af
, usvc
.fwmark
);
2417 if (cmd
!= IP_VS_SO_SET_ADD
2418 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2424 case IP_VS_SO_SET_ADD
:
2428 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
2430 case IP_VS_SO_SET_EDIT
:
2431 ret
= ip_vs_edit_service(svc
, &usvc
);
2433 case IP_VS_SO_SET_DEL
:
2434 ret
= ip_vs_del_service(svc
);
2438 case IP_VS_SO_SET_ZERO
:
2439 ret
= ip_vs_zero_service(svc
);
2441 case IP_VS_SO_SET_ADDDEST
:
2442 ret
= ip_vs_add_dest(svc
, &udest
);
2444 case IP_VS_SO_SET_EDITDEST
:
2445 ret
= ip_vs_edit_dest(svc
, &udest
);
2447 case IP_VS_SO_SET_DELDEST
:
2448 ret
= ip_vs_del_dest(svc
, &udest
);
2455 mutex_unlock(&__ip_vs_mutex
);
2457 /* decrease the module use count */
2458 ip_vs_use_count_dec();
2465 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2467 struct ip_vs_scheduler
*sched
;
2468 struct ip_vs_kstats kstats
;
2471 sched
= rcu_dereference_protected(src
->scheduler
, 1);
2472 sched_name
= sched
? sched
->name
: "none";
2473 dst
->protocol
= src
->protocol
;
2474 dst
->addr
= src
->addr
.ip
;
2475 dst
->port
= src
->port
;
2476 dst
->fwmark
= src
->fwmark
;
2477 strlcpy(dst
->sched_name
, sched_name
, sizeof(dst
->sched_name
));
2478 dst
->flags
= src
->flags
;
2479 dst
->timeout
= src
->timeout
/ HZ
;
2480 dst
->netmask
= src
->netmask
;
2481 dst
->num_dests
= src
->num_dests
;
2482 ip_vs_copy_stats(&kstats
, &src
->stats
);
2483 ip_vs_export_stats_user(&dst
->stats
, &kstats
);
2487 __ip_vs_get_service_entries(struct net
*net
,
2488 const struct ip_vs_get_services
*get
,
2489 struct ip_vs_get_services __user
*uptr
)
2491 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2493 struct ip_vs_service
*svc
;
2494 struct ip_vs_service_entry entry
;
2497 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2498 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2499 /* Only expose IPv4 entries to old interface */
2500 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2503 if (count
>= get
->num_services
)
2505 memset(&entry
, 0, sizeof(entry
));
2506 ip_vs_copy_service(&entry
, svc
);
2507 if (copy_to_user(&uptr
->entrytable
[count
],
2508 &entry
, sizeof(entry
))) {
2516 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2517 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
2518 /* Only expose IPv4 entries to old interface */
2519 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2522 if (count
>= get
->num_services
)
2524 memset(&entry
, 0, sizeof(entry
));
2525 ip_vs_copy_service(&entry
, svc
);
2526 if (copy_to_user(&uptr
->entrytable
[count
],
2527 &entry
, sizeof(entry
))) {
2539 __ip_vs_get_dest_entries(struct net
*net
, const struct ip_vs_get_dests
*get
,
2540 struct ip_vs_get_dests __user
*uptr
)
2542 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2543 struct ip_vs_service
*svc
;
2544 union nf_inet_addr addr
= { .ip
= get
->addr
};
2549 svc
= __ip_vs_svc_fwm_find(ipvs
, AF_INET
, get
->fwmark
);
2551 svc
= __ip_vs_service_find(ipvs
, AF_INET
, get
->protocol
, &addr
,
2557 struct ip_vs_dest
*dest
;
2558 struct ip_vs_dest_entry entry
;
2559 struct ip_vs_kstats kstats
;
2561 memset(&entry
, 0, sizeof(entry
));
2562 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2563 if (count
>= get
->num_dests
)
2566 /* Cannot expose heterogeneous members via sockopt
2569 if (dest
->af
!= svc
->af
)
2572 entry
.addr
= dest
->addr
.ip
;
2573 entry
.port
= dest
->port
;
2574 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2575 entry
.weight
= atomic_read(&dest
->weight
);
2576 entry
.u_threshold
= dest
->u_threshold
;
2577 entry
.l_threshold
= dest
->l_threshold
;
2578 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2579 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2580 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2581 ip_vs_copy_stats(&kstats
, &dest
->stats
);
2582 ip_vs_export_stats_user(&entry
.stats
, &kstats
);
2583 if (copy_to_user(&uptr
->entrytable
[count
],
2584 &entry
, sizeof(entry
))) {
2596 __ip_vs_get_timeouts(struct net
*net
, struct ip_vs_timeout_user
*u
)
2598 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2599 struct ip_vs_proto_data
*pd
;
2602 memset(u
, 0, sizeof (*u
));
2604 #ifdef CONFIG_IP_VS_PROTO_TCP
2605 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2606 u
->tcp_timeout
= pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2607 u
->tcp_fin_timeout
= pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2609 #ifdef CONFIG_IP_VS_PROTO_UDP
2610 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2612 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2616 static const unsigned char get_arglen
[CMDID(IP_VS_SO_GET_MAX
) + 1] = {
2617 [CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2618 [CMDID(IP_VS_SO_GET_INFO
)] = sizeof(struct ip_vs_getinfo
),
2619 [CMDID(IP_VS_SO_GET_SERVICES
)] = sizeof(struct ip_vs_get_services
),
2620 [CMDID(IP_VS_SO_GET_SERVICE
)] = sizeof(struct ip_vs_service_entry
),
2621 [CMDID(IP_VS_SO_GET_DESTS
)] = sizeof(struct ip_vs_get_dests
),
2622 [CMDID(IP_VS_SO_GET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2623 [CMDID(IP_VS_SO_GET_DAEMON
)] = 2 * sizeof(struct ip_vs_daemon_user
),
2626 union ip_vs_get_arglen
{
2627 char field_IP_VS_SO_GET_VERSION
[64];
2628 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO
;
2629 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES
;
2630 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE
;
2631 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS
;
2632 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT
;
2633 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON
[2];
2636 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2639 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2641 unsigned char arg
[MAX_GET_ARGLEN
];
2643 unsigned int copylen
;
2644 struct net
*net
= sock_net(sk
);
2645 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2648 BUILD_BUG_ON(sizeof(arg
) > 255);
2649 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2652 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2655 copylen
= get_arglen
[CMDID(cmd
)];
2656 if (*len
< (int) copylen
) {
2657 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len
, copylen
);
2661 if (copy_from_user(arg
, user
, copylen
) != 0)
2664 * Handle daemons first since it has its own locking
2666 if (cmd
== IP_VS_SO_GET_DAEMON
) {
2667 struct ip_vs_daemon_user d
[2];
2669 memset(&d
, 0, sizeof(d
));
2670 mutex_lock(&ipvs
->sync_mutex
);
2671 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
) {
2672 d
[0].state
= IP_VS_STATE_MASTER
;
2673 strlcpy(d
[0].mcast_ifn
, ipvs
->mcfg
.mcast_ifn
,
2674 sizeof(d
[0].mcast_ifn
));
2675 d
[0].syncid
= ipvs
->mcfg
.syncid
;
2677 if (ipvs
->sync_state
& IP_VS_STATE_BACKUP
) {
2678 d
[1].state
= IP_VS_STATE_BACKUP
;
2679 strlcpy(d
[1].mcast_ifn
, ipvs
->bcfg
.mcast_ifn
,
2680 sizeof(d
[1].mcast_ifn
));
2681 d
[1].syncid
= ipvs
->bcfg
.syncid
;
2683 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2685 mutex_unlock(&ipvs
->sync_mutex
);
2689 mutex_lock(&__ip_vs_mutex
);
2691 case IP_VS_SO_GET_VERSION
:
2695 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2696 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2697 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2701 *len
= strlen(buf
)+1;
2705 case IP_VS_SO_GET_INFO
:
2707 struct ip_vs_getinfo info
;
2708 info
.version
= IP_VS_VERSION_CODE
;
2709 info
.size
= ip_vs_conn_tab_size
;
2710 info
.num_services
= ipvs
->num_services
;
2711 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2716 case IP_VS_SO_GET_SERVICES
:
2718 struct ip_vs_get_services
*get
;
2721 get
= (struct ip_vs_get_services
*)arg
;
2722 size
= sizeof(*get
) +
2723 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2725 pr_err("length: %u != %u\n", *len
, size
);
2729 ret
= __ip_vs_get_service_entries(net
, get
, user
);
2733 case IP_VS_SO_GET_SERVICE
:
2735 struct ip_vs_service_entry
*entry
;
2736 struct ip_vs_service
*svc
;
2737 union nf_inet_addr addr
;
2739 entry
= (struct ip_vs_service_entry
*)arg
;
2740 addr
.ip
= entry
->addr
;
2743 svc
= __ip_vs_svc_fwm_find(ipvs
, AF_INET
, entry
->fwmark
);
2745 svc
= __ip_vs_service_find(ipvs
, AF_INET
,
2746 entry
->protocol
, &addr
,
2750 ip_vs_copy_service(entry
, svc
);
2751 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2758 case IP_VS_SO_GET_DESTS
:
2760 struct ip_vs_get_dests
*get
;
2763 get
= (struct ip_vs_get_dests
*)arg
;
2764 size
= sizeof(*get
) +
2765 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2767 pr_err("length: %u != %u\n", *len
, size
);
2771 ret
= __ip_vs_get_dest_entries(net
, get
, user
);
2775 case IP_VS_SO_GET_TIMEOUT
:
2777 struct ip_vs_timeout_user t
;
2779 __ip_vs_get_timeouts(net
, &t
);
2780 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2790 mutex_unlock(&__ip_vs_mutex
);
2795 static struct nf_sockopt_ops ip_vs_sockopts
= {
2797 .set_optmin
= IP_VS_BASE_CTL
,
2798 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2799 .set
= do_ip_vs_set_ctl
,
2800 .get_optmin
= IP_VS_BASE_CTL
,
2801 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2802 .get
= do_ip_vs_get_ctl
,
2803 .owner
= THIS_MODULE
,
2807 * Generic Netlink interface
2810 /* IPVS genetlink family */
2811 static struct genl_family ip_vs_genl_family
= {
2812 .id
= GENL_ID_GENERATE
,
2814 .name
= IPVS_GENL_NAME
,
2815 .version
= IPVS_GENL_VERSION
,
2816 .maxattr
= IPVS_CMD_MAX
,
2817 .netnsok
= true, /* Make ipvsadm to work on netns */
2820 /* Policy used for first-level command attributes */
2821 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2822 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2823 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2824 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2825 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2826 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2827 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2830 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2831 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2832 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2833 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2834 .len
= IP_VS_IFNAME_MAXLEN
},
2835 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2836 [IPVS_DAEMON_ATTR_SYNC_MAXLEN
] = { .type
= NLA_U16
},
2837 [IPVS_DAEMON_ATTR_MCAST_GROUP
] = { .type
= NLA_U32
},
2838 [IPVS_DAEMON_ATTR_MCAST_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2839 [IPVS_DAEMON_ATTR_MCAST_PORT
] = { .type
= NLA_U16
},
2840 [IPVS_DAEMON_ATTR_MCAST_TTL
] = { .type
= NLA_U8
},
2843 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2844 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2845 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2846 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2847 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2848 .len
= sizeof(union nf_inet_addr
) },
2849 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2850 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2851 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2852 .len
= IP_VS_SCHEDNAME_MAXLEN
},
2853 [IPVS_SVC_ATTR_PE_NAME
] = { .type
= NLA_NUL_STRING
,
2854 .len
= IP_VS_PENAME_MAXLEN
},
2855 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2856 .len
= sizeof(struct ip_vs_flags
) },
2857 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2858 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2859 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2862 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2863 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2864 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2865 .len
= sizeof(union nf_inet_addr
) },
2866 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2867 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2868 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2869 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2870 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2871 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2872 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2873 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2874 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2875 [IPVS_DEST_ATTR_ADDR_FAMILY
] = { .type
= NLA_U16
},
2878 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2879 struct ip_vs_kstats
*kstats
)
2881 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2886 if (nla_put_u32(skb
, IPVS_STATS_ATTR_CONNS
, (u32
)kstats
->conns
) ||
2887 nla_put_u32(skb
, IPVS_STATS_ATTR_INPKTS
, (u32
)kstats
->inpkts
) ||
2888 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPKTS
, (u32
)kstats
->outpkts
) ||
2889 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2890 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2891 nla_put_u32(skb
, IPVS_STATS_ATTR_CPS
, (u32
)kstats
->cps
) ||
2892 nla_put_u32(skb
, IPVS_STATS_ATTR_INPPS
, (u32
)kstats
->inpps
) ||
2893 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPPS
, (u32
)kstats
->outpps
) ||
2894 nla_put_u32(skb
, IPVS_STATS_ATTR_INBPS
, (u32
)kstats
->inbps
) ||
2895 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTBPS
, (u32
)kstats
->outbps
))
2896 goto nla_put_failure
;
2897 nla_nest_end(skb
, nl_stats
);
2902 nla_nest_cancel(skb
, nl_stats
);
2906 static int ip_vs_genl_fill_stats64(struct sk_buff
*skb
, int container_type
,
2907 struct ip_vs_kstats
*kstats
)
2909 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2914 if (nla_put_u64(skb
, IPVS_STATS_ATTR_CONNS
, kstats
->conns
) ||
2915 nla_put_u64(skb
, IPVS_STATS_ATTR_INPKTS
, kstats
->inpkts
) ||
2916 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPKTS
, kstats
->outpkts
) ||
2917 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2918 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2919 nla_put_u64(skb
, IPVS_STATS_ATTR_CPS
, kstats
->cps
) ||
2920 nla_put_u64(skb
, IPVS_STATS_ATTR_INPPS
, kstats
->inpps
) ||
2921 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPPS
, kstats
->outpps
) ||
2922 nla_put_u64(skb
, IPVS_STATS_ATTR_INBPS
, kstats
->inbps
) ||
2923 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBPS
, kstats
->outbps
))
2924 goto nla_put_failure
;
2925 nla_nest_end(skb
, nl_stats
);
2930 nla_nest_cancel(skb
, nl_stats
);
2934 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2935 struct ip_vs_service
*svc
)
2937 struct ip_vs_scheduler
*sched
;
2938 struct ip_vs_pe
*pe
;
2939 struct nlattr
*nl_service
;
2940 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2942 struct ip_vs_kstats kstats
;
2945 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
2949 if (nla_put_u16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
))
2950 goto nla_put_failure
;
2952 if (nla_put_u32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
))
2953 goto nla_put_failure
;
2955 if (nla_put_u16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
) ||
2956 nla_put(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
) ||
2957 nla_put_be16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
))
2958 goto nla_put_failure
;
2961 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
2962 sched_name
= sched
? sched
->name
: "none";
2963 pe
= rcu_dereference_protected(svc
->pe
, 1);
2964 if (nla_put_string(skb
, IPVS_SVC_ATTR_SCHED_NAME
, sched_name
) ||
2965 (pe
&& nla_put_string(skb
, IPVS_SVC_ATTR_PE_NAME
, pe
->name
)) ||
2966 nla_put(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
) ||
2967 nla_put_u32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
) ||
2968 nla_put_be32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
))
2969 goto nla_put_failure
;
2970 ip_vs_copy_stats(&kstats
, &svc
->stats
);
2971 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &kstats
))
2972 goto nla_put_failure
;
2973 if (ip_vs_genl_fill_stats64(skb
, IPVS_SVC_ATTR_STATS64
, &kstats
))
2974 goto nla_put_failure
;
2976 nla_nest_end(skb
, nl_service
);
2981 nla_nest_cancel(skb
, nl_service
);
2985 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
2986 struct ip_vs_service
*svc
,
2987 struct netlink_callback
*cb
)
2991 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
2992 &ip_vs_genl_family
, NLM_F_MULTI
,
2993 IPVS_CMD_NEW_SERVICE
);
2997 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
2998 goto nla_put_failure
;
3000 genlmsg_end(skb
, hdr
);
3004 genlmsg_cancel(skb
, hdr
);
3008 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
3009 struct netlink_callback
*cb
)
3012 int start
= cb
->args
[0];
3013 struct ip_vs_service
*svc
;
3014 struct netns_ipvs
*ipvs
= net_ipvs(skb_sknet(skb
));
3016 mutex_lock(&__ip_vs_mutex
);
3017 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3018 hlist_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
3019 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3021 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3023 goto nla_put_failure
;
3028 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3029 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
3030 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3032 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3034 goto nla_put_failure
;
3040 mutex_unlock(&__ip_vs_mutex
);
3046 static int ip_vs_genl_parse_service(struct net
*net
,
3047 struct ip_vs_service_user_kern
*usvc
,
3048 struct nlattr
*nla
, int full_entry
,
3049 struct ip_vs_service
**ret_svc
)
3051 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3052 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
3053 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
3054 struct ip_vs_service
*svc
;
3056 /* Parse mandatory identifying service fields first */
3058 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
, ip_vs_svc_policy
))
3061 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
3062 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
3063 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
3064 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
3065 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
3067 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
3070 memset(usvc
, 0, sizeof(*usvc
));
3072 usvc
->af
= nla_get_u16(nla_af
);
3073 #ifdef CONFIG_IP_VS_IPV6
3074 if (usvc
->af
!= AF_INET
&& usvc
->af
!= AF_INET6
)
3076 if (usvc
->af
!= AF_INET
)
3078 return -EAFNOSUPPORT
;
3081 usvc
->protocol
= IPPROTO_TCP
;
3082 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
3084 usvc
->protocol
= nla_get_u16(nla_protocol
);
3085 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
3086 usvc
->port
= nla_get_be16(nla_port
);
3092 svc
= __ip_vs_svc_fwm_find(ipvs
, usvc
->af
, usvc
->fwmark
);
3094 svc
= __ip_vs_service_find(ipvs
, usvc
->af
, usvc
->protocol
,
3095 &usvc
->addr
, usvc
->port
);
3099 /* If a full entry was requested, check for the additional fields */
3101 struct nlattr
*nla_sched
, *nla_flags
, *nla_pe
, *nla_timeout
,
3103 struct ip_vs_flags flags
;
3105 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
3106 nla_pe
= attrs
[IPVS_SVC_ATTR_PE_NAME
];
3107 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
3108 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
3109 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
3111 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
3114 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
3116 /* prefill flags from service if it already exists */
3118 usvc
->flags
= svc
->flags
;
3120 /* set new flags from userland */
3121 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
3122 (flags
.flags
& flags
.mask
);
3123 usvc
->sched_name
= nla_data(nla_sched
);
3124 usvc
->pe_name
= nla_pe
? nla_data(nla_pe
) : NULL
;
3125 usvc
->timeout
= nla_get_u32(nla_timeout
);
3126 usvc
->netmask
= nla_get_be32(nla_netmask
);
3132 static struct ip_vs_service
*ip_vs_genl_find_service(struct net
*net
,
3135 struct ip_vs_service_user_kern usvc
;
3136 struct ip_vs_service
*svc
;
3139 ret
= ip_vs_genl_parse_service(net
, &usvc
, nla
, 0, &svc
);
3140 return ret
? ERR_PTR(ret
) : svc
;
3143 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
3145 struct nlattr
*nl_dest
;
3146 struct ip_vs_kstats kstats
;
3148 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
3152 if (nla_put(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
) ||
3153 nla_put_be16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
) ||
3154 nla_put_u32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
3155 (atomic_read(&dest
->conn_flags
) &
3156 IP_VS_CONN_F_FWD_MASK
)) ||
3157 nla_put_u32(skb
, IPVS_DEST_ATTR_WEIGHT
,
3158 atomic_read(&dest
->weight
)) ||
3159 nla_put_u32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
) ||
3160 nla_put_u32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
) ||
3161 nla_put_u32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
3162 atomic_read(&dest
->activeconns
)) ||
3163 nla_put_u32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
3164 atomic_read(&dest
->inactconns
)) ||
3165 nla_put_u32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
3166 atomic_read(&dest
->persistconns
)) ||
3167 nla_put_u16(skb
, IPVS_DEST_ATTR_ADDR_FAMILY
, dest
->af
))
3168 goto nla_put_failure
;
3169 ip_vs_copy_stats(&kstats
, &dest
->stats
);
3170 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &kstats
))
3171 goto nla_put_failure
;
3172 if (ip_vs_genl_fill_stats64(skb
, IPVS_DEST_ATTR_STATS64
, &kstats
))
3173 goto nla_put_failure
;
3175 nla_nest_end(skb
, nl_dest
);
3180 nla_nest_cancel(skb
, nl_dest
);
3184 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
3185 struct netlink_callback
*cb
)
3189 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3190 &ip_vs_genl_family
, NLM_F_MULTI
,
3195 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
3196 goto nla_put_failure
;
3198 genlmsg_end(skb
, hdr
);
3202 genlmsg_cancel(skb
, hdr
);
3206 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
3207 struct netlink_callback
*cb
)
3210 int start
= cb
->args
[0];
3211 struct ip_vs_service
*svc
;
3212 struct ip_vs_dest
*dest
;
3213 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
3214 struct net
*net
= skb_sknet(skb
);
3216 mutex_lock(&__ip_vs_mutex
);
3218 /* Try to find the service for which to dump destinations */
3219 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
,
3220 IPVS_CMD_ATTR_MAX
, ip_vs_cmd_policy
))
3224 svc
= ip_vs_genl_find_service(net
, attrs
[IPVS_CMD_ATTR_SERVICE
]);
3225 if (IS_ERR(svc
) || svc
== NULL
)
3228 /* Dump the destinations */
3229 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
3232 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
3234 goto nla_put_failure
;
3242 mutex_unlock(&__ip_vs_mutex
);
3247 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
3248 struct nlattr
*nla
, int full_entry
)
3250 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
3251 struct nlattr
*nla_addr
, *nla_port
;
3252 struct nlattr
*nla_addr_family
;
3254 /* Parse mandatory identifying destination fields first */
3256 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
, ip_vs_dest_policy
))
3259 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
3260 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
3261 nla_addr_family
= attrs
[IPVS_DEST_ATTR_ADDR_FAMILY
];
3263 if (!(nla_addr
&& nla_port
))
3266 memset(udest
, 0, sizeof(*udest
));
3268 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
3269 udest
->port
= nla_get_be16(nla_port
);
3271 if (nla_addr_family
)
3272 udest
->af
= nla_get_u16(nla_addr_family
);
3276 /* If a full entry was requested, check for the additional fields */
3278 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
3281 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
3282 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
3283 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
3284 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
3286 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
3289 udest
->conn_flags
= nla_get_u32(nla_fwd
)
3290 & IP_VS_CONN_F_FWD_MASK
;
3291 udest
->weight
= nla_get_u32(nla_weight
);
3292 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
3293 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
3299 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __u32 state
,
3300 struct ipvs_sync_daemon_cfg
*c
)
3302 struct nlattr
*nl_daemon
;
3304 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
3308 if (nla_put_u32(skb
, IPVS_DAEMON_ATTR_STATE
, state
) ||
3309 nla_put_string(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, c
->mcast_ifn
) ||
3310 nla_put_u32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, c
->syncid
) ||
3311 nla_put_u16(skb
, IPVS_DAEMON_ATTR_SYNC_MAXLEN
, c
->sync_maxlen
) ||
3312 nla_put_u16(skb
, IPVS_DAEMON_ATTR_MCAST_PORT
, c
->mcast_port
) ||
3313 nla_put_u8(skb
, IPVS_DAEMON_ATTR_MCAST_TTL
, c
->mcast_ttl
))
3314 goto nla_put_failure
;
3315 #ifdef CONFIG_IP_VS_IPV6
3316 if (c
->mcast_af
== AF_INET6
) {
3317 if (nla_put_in6_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP6
,
3318 &c
->mcast_group
.in6
))
3319 goto nla_put_failure
;
3322 if (c
->mcast_af
== AF_INET
&&
3323 nla_put_in_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP
,
3325 goto nla_put_failure
;
3326 nla_nest_end(skb
, nl_daemon
);
3331 nla_nest_cancel(skb
, nl_daemon
);
3335 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __u32 state
,
3336 struct ipvs_sync_daemon_cfg
*c
,
3337 struct netlink_callback
*cb
)
3340 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3341 &ip_vs_genl_family
, NLM_F_MULTI
,
3342 IPVS_CMD_NEW_DAEMON
);
3346 if (ip_vs_genl_fill_daemon(skb
, state
, c
))
3347 goto nla_put_failure
;
3349 genlmsg_end(skb
, hdr
);
3353 genlmsg_cancel(skb
, hdr
);
3357 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
3358 struct netlink_callback
*cb
)
3360 struct net
*net
= skb_sknet(skb
);
3361 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3363 mutex_lock(&ipvs
->sync_mutex
);
3364 if ((ipvs
->sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
3365 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
3366 &ipvs
->mcfg
, cb
) < 0)
3367 goto nla_put_failure
;
3372 if ((ipvs
->sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
3373 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
3374 &ipvs
->bcfg
, cb
) < 0)
3375 goto nla_put_failure
;
3381 mutex_unlock(&ipvs
->sync_mutex
);
3386 static int ip_vs_genl_new_daemon(struct net
*net
, struct nlattr
**attrs
)
3388 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3389 struct ipvs_sync_daemon_cfg c
;
3393 memset(&c
, 0, sizeof(c
));
3394 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
3395 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
3396 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3398 strlcpy(c
.mcast_ifn
, nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3399 sizeof(c
.mcast_ifn
));
3400 c
.syncid
= nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]);
3402 a
= attrs
[IPVS_DAEMON_ATTR_SYNC_MAXLEN
];
3404 c
.sync_maxlen
= nla_get_u16(a
);
3406 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP
];
3408 c
.mcast_af
= AF_INET
;
3409 c
.mcast_group
.ip
= nla_get_in_addr(a
);
3410 if (!ipv4_is_multicast(c
.mcast_group
.ip
))
3413 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP6
];
3415 #ifdef CONFIG_IP_VS_IPV6
3418 c
.mcast_af
= AF_INET6
;
3419 c
.mcast_group
.in6
= nla_get_in6_addr(a
);
3420 addr_type
= ipv6_addr_type(&c
.mcast_group
.in6
);
3421 if (!(addr_type
& IPV6_ADDR_MULTICAST
))
3424 return -EAFNOSUPPORT
;
3429 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_PORT
];
3431 c
.mcast_port
= nla_get_u16(a
);
3433 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_TTL
];
3435 c
.mcast_ttl
= nla_get_u8(a
);
3437 /* The synchronization protocol is incompatible with mixed family
3440 if (ipvs
->mixed_address_family_dests
> 0)
3444 mutex_lock(&ipvs
->sync_mutex
);
3445 ret
= start_sync_thread(net
, &c
,
3446 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3447 mutex_unlock(&ipvs
->sync_mutex
);
3452 static int ip_vs_genl_del_daemon(struct net
*net
, struct nlattr
**attrs
)
3454 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3457 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3460 mutex_lock(&ipvs
->sync_mutex
);
3461 ret
= stop_sync_thread(net
,
3462 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3463 mutex_unlock(&ipvs
->sync_mutex
);
3467 static int ip_vs_genl_set_config(struct net
*net
, struct nlattr
**attrs
)
3469 struct ip_vs_timeout_user t
;
3471 __ip_vs_get_timeouts(net
, &t
);
3473 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3474 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3476 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3478 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3480 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3481 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3483 return ip_vs_set_timeout(net
, &t
);
3486 static int ip_vs_genl_set_daemon(struct sk_buff
*skb
, struct genl_info
*info
)
3488 int ret
= -EINVAL
, cmd
;
3490 struct netns_ipvs
*ipvs
;
3492 net
= skb_sknet(skb
);
3493 ipvs
= net_ipvs(net
);
3494 cmd
= info
->genlhdr
->cmd
;
3496 if (cmd
== IPVS_CMD_NEW_DAEMON
|| cmd
== IPVS_CMD_DEL_DAEMON
) {
3497 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3499 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3500 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3501 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3502 ip_vs_daemon_policy
))
3505 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3506 ret
= ip_vs_genl_new_daemon(net
, daemon_attrs
);
3508 ret
= ip_vs_genl_del_daemon(net
, daemon_attrs
);
3515 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3517 struct ip_vs_service
*svc
= NULL
;
3518 struct ip_vs_service_user_kern usvc
;
3519 struct ip_vs_dest_user_kern udest
;
3521 int need_full_svc
= 0, need_full_dest
= 0;
3524 net
= skb_sknet(skb
);
3525 cmd
= info
->genlhdr
->cmd
;
3527 mutex_lock(&__ip_vs_mutex
);
3529 if (cmd
== IPVS_CMD_FLUSH
) {
3530 ret
= ip_vs_flush(net
, false);
3532 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3533 ret
= ip_vs_genl_set_config(net
, info
->attrs
);
3535 } else if (cmd
== IPVS_CMD_ZERO
&&
3536 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3537 ret
= ip_vs_zero_all(net
);
3541 /* All following commands require a service argument, so check if we
3542 * received a valid one. We need a full service specification when
3543 * adding / editing a service. Only identifying members otherwise. */
3544 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3547 ret
= ip_vs_genl_parse_service(net
, &usvc
,
3548 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3549 need_full_svc
, &svc
);
3553 /* Unless we're adding a new service, the service must already exist */
3554 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3559 /* Destination commands require a valid destination argument. For
3560 * adding / editing a destination, we need a full destination
3562 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3563 cmd
== IPVS_CMD_DEL_DEST
) {
3564 if (cmd
!= IPVS_CMD_DEL_DEST
)
3567 ret
= ip_vs_genl_parse_dest(&udest
,
3568 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3573 /* Old protocols did not allow the user to specify address
3574 * family, so we set it to zero instead. We also didn't
3575 * allow heterogeneous pools in the old code, so it's safe
3576 * to assume that this will have the same address family as
3582 if (udest
.af
!= svc
->af
&& cmd
!= IPVS_CMD_DEL_DEST
) {
3583 /* The synchronization protocol is incompatible
3584 * with mixed family services
3586 if (net_ipvs(net
)->sync_state
) {
3591 /* Which connection types do we support? */
3592 switch (udest
.conn_flags
) {
3593 case IP_VS_CONN_F_TUNNEL
:
3594 /* We are able to forward this */
3604 case IPVS_CMD_NEW_SERVICE
:
3606 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
3610 case IPVS_CMD_SET_SERVICE
:
3611 ret
= ip_vs_edit_service(svc
, &usvc
);
3613 case IPVS_CMD_DEL_SERVICE
:
3614 ret
= ip_vs_del_service(svc
);
3615 /* do not use svc, it can be freed */
3617 case IPVS_CMD_NEW_DEST
:
3618 ret
= ip_vs_add_dest(svc
, &udest
);
3620 case IPVS_CMD_SET_DEST
:
3621 ret
= ip_vs_edit_dest(svc
, &udest
);
3623 case IPVS_CMD_DEL_DEST
:
3624 ret
= ip_vs_del_dest(svc
, &udest
);
3627 ret
= ip_vs_zero_service(svc
);
3634 mutex_unlock(&__ip_vs_mutex
);
3639 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3641 struct sk_buff
*msg
;
3643 int ret
, cmd
, reply_cmd
;
3646 net
= skb_sknet(skb
);
3647 cmd
= info
->genlhdr
->cmd
;
3649 if (cmd
== IPVS_CMD_GET_SERVICE
)
3650 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3651 else if (cmd
== IPVS_CMD_GET_INFO
)
3652 reply_cmd
= IPVS_CMD_SET_INFO
;
3653 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3654 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3656 pr_err("unknown Generic Netlink command\n");
3660 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3664 mutex_lock(&__ip_vs_mutex
);
3666 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3668 goto nla_put_failure
;
3671 case IPVS_CMD_GET_SERVICE
:
3673 struct ip_vs_service
*svc
;
3675 svc
= ip_vs_genl_find_service(net
,
3676 info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3681 ret
= ip_vs_genl_fill_service(msg
, svc
);
3683 goto nla_put_failure
;
3692 case IPVS_CMD_GET_CONFIG
:
3694 struct ip_vs_timeout_user t
;
3696 __ip_vs_get_timeouts(net
, &t
);
3697 #ifdef CONFIG_IP_VS_PROTO_TCP
3698 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
,
3700 nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3702 goto nla_put_failure
;
3704 #ifdef CONFIG_IP_VS_PROTO_UDP
3705 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
))
3706 goto nla_put_failure
;
3712 case IPVS_CMD_GET_INFO
:
3713 if (nla_put_u32(msg
, IPVS_INFO_ATTR_VERSION
,
3714 IP_VS_VERSION_CODE
) ||
3715 nla_put_u32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3716 ip_vs_conn_tab_size
))
3717 goto nla_put_failure
;
3721 genlmsg_end(msg
, reply
);
3722 ret
= genlmsg_reply(msg
, info
);
3726 pr_err("not enough space in Netlink message\n");
3732 mutex_unlock(&__ip_vs_mutex
);
3738 static const struct genl_ops ip_vs_genl_ops
[] = {
3740 .cmd
= IPVS_CMD_NEW_SERVICE
,
3741 .flags
= GENL_ADMIN_PERM
,
3742 .policy
= ip_vs_cmd_policy
,
3743 .doit
= ip_vs_genl_set_cmd
,
3746 .cmd
= IPVS_CMD_SET_SERVICE
,
3747 .flags
= GENL_ADMIN_PERM
,
3748 .policy
= ip_vs_cmd_policy
,
3749 .doit
= ip_vs_genl_set_cmd
,
3752 .cmd
= IPVS_CMD_DEL_SERVICE
,
3753 .flags
= GENL_ADMIN_PERM
,
3754 .policy
= ip_vs_cmd_policy
,
3755 .doit
= ip_vs_genl_set_cmd
,
3758 .cmd
= IPVS_CMD_GET_SERVICE
,
3759 .flags
= GENL_ADMIN_PERM
,
3760 .doit
= ip_vs_genl_get_cmd
,
3761 .dumpit
= ip_vs_genl_dump_services
,
3762 .policy
= ip_vs_cmd_policy
,
3765 .cmd
= IPVS_CMD_NEW_DEST
,
3766 .flags
= GENL_ADMIN_PERM
,
3767 .policy
= ip_vs_cmd_policy
,
3768 .doit
= ip_vs_genl_set_cmd
,
3771 .cmd
= IPVS_CMD_SET_DEST
,
3772 .flags
= GENL_ADMIN_PERM
,
3773 .policy
= ip_vs_cmd_policy
,
3774 .doit
= ip_vs_genl_set_cmd
,
3777 .cmd
= IPVS_CMD_DEL_DEST
,
3778 .flags
= GENL_ADMIN_PERM
,
3779 .policy
= ip_vs_cmd_policy
,
3780 .doit
= ip_vs_genl_set_cmd
,
3783 .cmd
= IPVS_CMD_GET_DEST
,
3784 .flags
= GENL_ADMIN_PERM
,
3785 .policy
= ip_vs_cmd_policy
,
3786 .dumpit
= ip_vs_genl_dump_dests
,
3789 .cmd
= IPVS_CMD_NEW_DAEMON
,
3790 .flags
= GENL_ADMIN_PERM
,
3791 .policy
= ip_vs_cmd_policy
,
3792 .doit
= ip_vs_genl_set_daemon
,
3795 .cmd
= IPVS_CMD_DEL_DAEMON
,
3796 .flags
= GENL_ADMIN_PERM
,
3797 .policy
= ip_vs_cmd_policy
,
3798 .doit
= ip_vs_genl_set_daemon
,
3801 .cmd
= IPVS_CMD_GET_DAEMON
,
3802 .flags
= GENL_ADMIN_PERM
,
3803 .dumpit
= ip_vs_genl_dump_daemons
,
3806 .cmd
= IPVS_CMD_SET_CONFIG
,
3807 .flags
= GENL_ADMIN_PERM
,
3808 .policy
= ip_vs_cmd_policy
,
3809 .doit
= ip_vs_genl_set_cmd
,
3812 .cmd
= IPVS_CMD_GET_CONFIG
,
3813 .flags
= GENL_ADMIN_PERM
,
3814 .doit
= ip_vs_genl_get_cmd
,
3817 .cmd
= IPVS_CMD_GET_INFO
,
3818 .flags
= GENL_ADMIN_PERM
,
3819 .doit
= ip_vs_genl_get_cmd
,
3822 .cmd
= IPVS_CMD_ZERO
,
3823 .flags
= GENL_ADMIN_PERM
,
3824 .policy
= ip_vs_cmd_policy
,
3825 .doit
= ip_vs_genl_set_cmd
,
3828 .cmd
= IPVS_CMD_FLUSH
,
3829 .flags
= GENL_ADMIN_PERM
,
3830 .doit
= ip_vs_genl_set_cmd
,
3834 static int __init
ip_vs_genl_register(void)
3836 return genl_register_family_with_ops(&ip_vs_genl_family
,
3840 static void ip_vs_genl_unregister(void)
3842 genl_unregister_family(&ip_vs_genl_family
);
3845 /* End of Generic Netlink interface definitions */
3848 * per netns intit/exit func.
3850 #ifdef CONFIG_SYSCTL
3851 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
)
3854 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3855 struct ctl_table
*tbl
;
3857 atomic_set(&ipvs
->dropentry
, 0);
3858 spin_lock_init(&ipvs
->dropentry_lock
);
3859 spin_lock_init(&ipvs
->droppacket_lock
);
3860 spin_lock_init(&ipvs
->securetcp_lock
);
3862 if (!net_eq(net
, &init_net
)) {
3863 tbl
= kmemdup(vs_vars
, sizeof(vs_vars
), GFP_KERNEL
);
3867 /* Don't export sysctls to unprivileged users */
3868 if (net
->user_ns
!= &init_user_ns
)
3869 tbl
[0].procname
= NULL
;
3872 /* Initialize sysctl defaults */
3873 for (idx
= 0; idx
< ARRAY_SIZE(vs_vars
); idx
++) {
3874 if (tbl
[idx
].proc_handler
== proc_do_defense_mode
)
3875 tbl
[idx
].extra2
= ipvs
;
3878 ipvs
->sysctl_amemthresh
= 1024;
3879 tbl
[idx
++].data
= &ipvs
->sysctl_amemthresh
;
3880 ipvs
->sysctl_am_droprate
= 10;
3881 tbl
[idx
++].data
= &ipvs
->sysctl_am_droprate
;
3882 tbl
[idx
++].data
= &ipvs
->sysctl_drop_entry
;
3883 tbl
[idx
++].data
= &ipvs
->sysctl_drop_packet
;
3884 #ifdef CONFIG_IP_VS_NFCT
3885 tbl
[idx
++].data
= &ipvs
->sysctl_conntrack
;
3887 tbl
[idx
++].data
= &ipvs
->sysctl_secure_tcp
;
3888 ipvs
->sysctl_snat_reroute
= 1;
3889 tbl
[idx
++].data
= &ipvs
->sysctl_snat_reroute
;
3890 ipvs
->sysctl_sync_ver
= 1;
3891 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ver
;
3892 ipvs
->sysctl_sync_ports
= 1;
3893 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ports
;
3894 tbl
[idx
++].data
= &ipvs
->sysctl_sync_persist_mode
;
3895 ipvs
->sysctl_sync_qlen_max
= nr_free_buffer_pages() / 32;
3896 tbl
[idx
++].data
= &ipvs
->sysctl_sync_qlen_max
;
3897 ipvs
->sysctl_sync_sock_size
= 0;
3898 tbl
[idx
++].data
= &ipvs
->sysctl_sync_sock_size
;
3899 tbl
[idx
++].data
= &ipvs
->sysctl_cache_bypass
;
3900 tbl
[idx
++].data
= &ipvs
->sysctl_expire_nodest_conn
;
3901 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_tcp
;
3902 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_sctp
;
3903 tbl
[idx
++].data
= &ipvs
->sysctl_expire_quiescent_template
;
3904 ipvs
->sysctl_sync_threshold
[0] = DEFAULT_SYNC_THRESHOLD
;
3905 ipvs
->sysctl_sync_threshold
[1] = DEFAULT_SYNC_PERIOD
;
3906 tbl
[idx
].data
= &ipvs
->sysctl_sync_threshold
;
3907 tbl
[idx
++].maxlen
= sizeof(ipvs
->sysctl_sync_threshold
);
3908 ipvs
->sysctl_sync_refresh_period
= DEFAULT_SYNC_REFRESH_PERIOD
;
3909 tbl
[idx
++].data
= &ipvs
->sysctl_sync_refresh_period
;
3910 ipvs
->sysctl_sync_retries
= clamp_t(int, DEFAULT_SYNC_RETRIES
, 0, 3);
3911 tbl
[idx
++].data
= &ipvs
->sysctl_sync_retries
;
3912 tbl
[idx
++].data
= &ipvs
->sysctl_nat_icmp_send
;
3913 ipvs
->sysctl_pmtu_disc
= 1;
3914 tbl
[idx
++].data
= &ipvs
->sysctl_pmtu_disc
;
3915 tbl
[idx
++].data
= &ipvs
->sysctl_backup_only
;
3916 ipvs
->sysctl_conn_reuse_mode
= 1;
3917 tbl
[idx
++].data
= &ipvs
->sysctl_conn_reuse_mode
;
3918 tbl
[idx
++].data
= &ipvs
->sysctl_schedule_icmp
;
3919 tbl
[idx
++].data
= &ipvs
->sysctl_ignore_tunneled
;
3921 ipvs
->sysctl_hdr
= register_net_sysctl(net
, "net/ipv4/vs", tbl
);
3922 if (ipvs
->sysctl_hdr
== NULL
) {
3923 if (!net_eq(net
, &init_net
))
3927 ip_vs_start_estimator(net
, &ipvs
->tot_stats
);
3928 ipvs
->sysctl_tbl
= tbl
;
3929 /* Schedule defense work */
3930 INIT_DELAYED_WORK(&ipvs
->defense_work
, defense_work_handler
);
3931 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
3936 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
)
3938 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3940 cancel_delayed_work_sync(&ipvs
->defense_work
);
3941 cancel_work_sync(&ipvs
->defense_work
.work
);
3942 unregister_net_sysctl_table(ipvs
->sysctl_hdr
);
3943 ip_vs_stop_estimator(net
, &ipvs
->tot_stats
);
3945 if (!net_eq(net
, &init_net
))
3946 kfree(ipvs
->sysctl_tbl
);
3951 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
) { return 0; }
3952 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
) { }
3956 static struct notifier_block ip_vs_dst_notifier
= {
3957 .notifier_call
= ip_vs_dst_event
,
3960 int __net_init
ip_vs_control_net_init(struct net
*net
)
3963 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3965 /* Initialize rs_table */
3966 for (idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++)
3967 INIT_HLIST_HEAD(&ipvs
->rs_table
[idx
]);
3969 INIT_LIST_HEAD(&ipvs
->dest_trash
);
3970 spin_lock_init(&ipvs
->dest_trash_lock
);
3971 setup_timer(&ipvs
->dest_trash_timer
, ip_vs_dest_trash_expire
,
3972 (unsigned long) net
);
3973 atomic_set(&ipvs
->ftpsvc_counter
, 0);
3974 atomic_set(&ipvs
->nullsvc_counter
, 0);
3977 ipvs
->tot_stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
3978 if (!ipvs
->tot_stats
.cpustats
)
3981 for_each_possible_cpu(i
) {
3982 struct ip_vs_cpu_stats
*ipvs_tot_stats
;
3983 ipvs_tot_stats
= per_cpu_ptr(ipvs
->tot_stats
.cpustats
, i
);
3984 u64_stats_init(&ipvs_tot_stats
->syncp
);
3987 spin_lock_init(&ipvs
->tot_stats
.lock
);
3989 proc_create("ip_vs", 0, net
->proc_net
, &ip_vs_info_fops
);
3990 proc_create("ip_vs_stats", 0, net
->proc_net
, &ip_vs_stats_fops
);
3991 proc_create("ip_vs_stats_percpu", 0, net
->proc_net
,
3992 &ip_vs_stats_percpu_fops
);
3994 if (ip_vs_control_net_init_sysctl(net
))
4000 free_percpu(ipvs
->tot_stats
.cpustats
);
4004 void __net_exit
ip_vs_control_net_cleanup(struct net
*net
)
4006 struct netns_ipvs
*ipvs
= net_ipvs(net
);
4008 ip_vs_trash_cleanup(net
);
4009 ip_vs_control_net_cleanup_sysctl(net
);
4010 remove_proc_entry("ip_vs_stats_percpu", net
->proc_net
);
4011 remove_proc_entry("ip_vs_stats", net
->proc_net
);
4012 remove_proc_entry("ip_vs", net
->proc_net
);
4013 free_percpu(ipvs
->tot_stats
.cpustats
);
4016 int __init
ip_vs_register_nl_ioctl(void)
4020 ret
= nf_register_sockopt(&ip_vs_sockopts
);
4022 pr_err("cannot register sockopt.\n");
4026 ret
= ip_vs_genl_register();
4028 pr_err("cannot register Generic Netlink interface.\n");
4034 nf_unregister_sockopt(&ip_vs_sockopts
);
4039 void ip_vs_unregister_nl_ioctl(void)
4041 ip_vs_genl_unregister();
4042 nf_unregister_sockopt(&ip_vs_sockopts
);
4045 int __init
ip_vs_control_init(void)
4052 /* Initialize svc_table, ip_vs_svc_fwm_table */
4053 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
4054 INIT_HLIST_HEAD(&ip_vs_svc_table
[idx
]);
4055 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
4058 smp_wmb(); /* Do we really need it now ? */
4060 ret
= register_netdevice_notifier(&ip_vs_dst_notifier
);
4069 void ip_vs_control_cleanup(void)
4072 unregister_netdevice_notifier(&ip_vs_dst_notifier
);