2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/workqueue.h>
32 #include <linux/swap.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
47 #include <net/route.h>
49 #include <net/genetlink.h>
51 #include <asm/uaccess.h>
53 #include <net/ip_vs.h>
55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56 static DEFINE_MUTEX(__ip_vs_mutex
);
58 /* sysctl variables */
60 #ifdef CONFIG_IP_VS_DEBUG
61 static int sysctl_ip_vs_debug_level
= 0;
63 int ip_vs_get_debug_level(void)
65 return sysctl_ip_vs_debug_level
;
71 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
);
74 #ifdef CONFIG_IP_VS_IPV6
75 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
76 static bool __ip_vs_addr_is_local_v6(struct net
*net
,
77 const struct in6_addr
*addr
)
82 struct dst_entry
*dst
= ip6_route_output(net
, NULL
, &fl6
);
85 is_local
= !dst
->error
&& dst
->dev
&& (dst
->dev
->flags
& IFF_LOOPBACK
);
94 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
97 static void update_defense_level(struct netns_ipvs
*ipvs
)
100 static int old_secure_tcp
= 0;
105 /* we only count free and buffered memory (in pages) */
107 availmem
= i
.freeram
+ i
.bufferram
;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem
= (availmem
< ipvs
->sysctl_amemthresh
);
118 spin_lock(&ipvs
->dropentry_lock
);
119 switch (ipvs
->sysctl_drop_entry
) {
121 atomic_set(&ipvs
->dropentry
, 0);
125 atomic_set(&ipvs
->dropentry
, 1);
126 ipvs
->sysctl_drop_entry
= 2;
128 atomic_set(&ipvs
->dropentry
, 0);
133 atomic_set(&ipvs
->dropentry
, 1);
135 atomic_set(&ipvs
->dropentry
, 0);
136 ipvs
->sysctl_drop_entry
= 1;
140 atomic_set(&ipvs
->dropentry
, 1);
143 spin_unlock(&ipvs
->dropentry_lock
);
146 spin_lock(&ipvs
->droppacket_lock
);
147 switch (ipvs
->sysctl_drop_packet
) {
153 ipvs
->drop_rate
= ipvs
->drop_counter
154 = ipvs
->sysctl_amemthresh
/
155 (ipvs
->sysctl_amemthresh
-availmem
);
156 ipvs
->sysctl_drop_packet
= 2;
163 ipvs
->drop_rate
= ipvs
->drop_counter
164 = ipvs
->sysctl_amemthresh
/
165 (ipvs
->sysctl_amemthresh
-availmem
);
168 ipvs
->sysctl_drop_packet
= 1;
172 ipvs
->drop_rate
= ipvs
->sysctl_am_droprate
;
175 spin_unlock(&ipvs
->droppacket_lock
);
178 spin_lock(&ipvs
->securetcp_lock
);
179 switch (ipvs
->sysctl_secure_tcp
) {
181 if (old_secure_tcp
>= 2)
186 if (old_secure_tcp
< 2)
188 ipvs
->sysctl_secure_tcp
= 2;
190 if (old_secure_tcp
>= 2)
196 if (old_secure_tcp
< 2)
199 if (old_secure_tcp
>= 2)
201 ipvs
->sysctl_secure_tcp
= 1;
205 if (old_secure_tcp
< 2)
209 old_secure_tcp
= ipvs
->sysctl_secure_tcp
;
211 ip_vs_protocol_timeout_change(ipvs
,
212 ipvs
->sysctl_secure_tcp
> 1);
213 spin_unlock(&ipvs
->securetcp_lock
);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct
*work
)
226 struct netns_ipvs
*ipvs
=
227 container_of(work
, struct netns_ipvs
, defense_work
.work
);
229 update_defense_level(ipvs
);
230 if (atomic_read(&ipvs
->dropentry
))
231 ip_vs_random_dropentry(ipvs
->net
);
232 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE
);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE
);
250 * Hash table: for virtual service lookups
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct net
*net
, int af
, unsigned int proto
,
267 const union nf_inet_addr
*addr
, __be16 port
)
269 register unsigned int porth
= ntohs(port
);
270 __be32 addr_fold
= addr
->ip
;
273 #ifdef CONFIG_IP_VS_IPV6
275 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
276 addr
->ip6
[2]^addr
->ip6
[3];
278 ahash
= ntohl(addr_fold
);
279 ahash
^= ((size_t) net
>> 8);
281 return (proto
^ ahash
^ (porth
>> IP_VS_SVC_TAB_BITS
) ^ porth
) &
286 * Returns hash value of fwmark for virtual service lookup
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct net
*net
, __u32 fwmark
)
290 return (((size_t)net
>>8) ^ fwmark
) & IP_VS_SVC_TAB_MASK
;
294 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
295 * or in the ip_vs_svc_fwm_table by fwmark.
296 * Should be called with locked tables.
298 static int ip_vs_svc_hash(struct ip_vs_service
*svc
)
302 if (svc
->flags
& IP_VS_SVC_F_HASHED
) {
303 pr_err("%s(): request for already hashed, called from %pF\n",
304 __func__
, __builtin_return_address(0));
308 if (svc
->fwmark
== 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash
= ip_vs_svc_hashkey(svc
->ipvs
->net
, svc
->af
, svc
->protocol
,
313 &svc
->addr
, svc
->port
);
314 hlist_add_head_rcu(&svc
->s_list
, &ip_vs_svc_table
[hash
]);
317 * Hash it by fwmark in svc_fwm_table
319 hash
= ip_vs_svc_fwm_hashkey(svc
->ipvs
->net
, svc
->fwmark
);
320 hlist_add_head_rcu(&svc
->f_list
, &ip_vs_svc_fwm_table
[hash
]);
323 svc
->flags
|= IP_VS_SVC_F_HASHED
;
324 /* increase its refcnt because it is referenced by the svc table */
325 atomic_inc(&svc
->refcnt
);
331 * Unhashes a service from svc_table / svc_fwm_table.
332 * Should be called with locked tables.
334 static int ip_vs_svc_unhash(struct ip_vs_service
*svc
)
336 if (!(svc
->flags
& IP_VS_SVC_F_HASHED
)) {
337 pr_err("%s(): request for unhash flagged, called from %pF\n",
338 __func__
, __builtin_return_address(0));
342 if (svc
->fwmark
== 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc
->s_list
);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc
->f_list
);
350 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
351 atomic_dec(&svc
->refcnt
);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service
*
360 __ip_vs_service_find(struct net
*net
, int af
, __u16 protocol
,
361 const union nf_inet_addr
*vaddr
, __be16 vport
)
363 struct netns_ipvs
*ipvs
= net_ipvs(net
);
365 struct ip_vs_service
*svc
;
367 /* Check for "full" addressed entries */
368 hash
= ip_vs_svc_hashkey(net
, af
, protocol
, vaddr
, vport
);
370 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[hash
], s_list
) {
372 && ip_vs_addr_equal(af
, &svc
->addr
, vaddr
)
373 && (svc
->port
== vport
)
374 && (svc
->protocol
== protocol
)
375 && (svc
->ipvs
== ipvs
)) {
386 * Get service by {fwmark} in the service table.
388 static inline struct ip_vs_service
*
389 __ip_vs_svc_fwm_find(struct net
*net
, int af
, __u32 fwmark
)
391 struct netns_ipvs
*ipvs
= net_ipvs(net
);
393 struct ip_vs_service
*svc
;
395 /* Check for fwmark addressed entries */
396 hash
= ip_vs_svc_fwm_hashkey(net
, fwmark
);
398 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[hash
], f_list
) {
399 if (svc
->fwmark
== fwmark
&& svc
->af
== af
400 && (svc
->ipvs
== ipvs
)) {
409 /* Find service, called under RCU lock */
410 struct ip_vs_service
*
411 ip_vs_service_find(struct net
*net
, int af
, __u32 fwmark
, __u16 protocol
,
412 const union nf_inet_addr
*vaddr
, __be16 vport
)
414 struct ip_vs_service
*svc
;
415 struct netns_ipvs
*ipvs
= net_ipvs(net
);
418 * Check the table hashed by fwmark first
421 svc
= __ip_vs_svc_fwm_find(net
, af
, fwmark
);
427 * Check the table hashed by <protocol,addr,port>
428 * for "full" addressed entries
430 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, vport
);
433 && protocol
== IPPROTO_TCP
434 && atomic_read(&ipvs
->ftpsvc_counter
)
435 && (vport
== FTPDATA
|| ntohs(vport
) >= PROT_SOCK
)) {
437 * Check if ftp service entry exists, the packet
438 * might belong to FTP data connections.
440 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, FTPPORT
);
444 && atomic_read(&ipvs
->nullsvc_counter
)) {
446 * Check if the catch-all port (port zero) exists
448 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, 0);
452 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
453 fwmark
, ip_vs_proto_name(protocol
),
454 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
455 svc
? "hit" : "not hit");
462 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
464 atomic_inc(&svc
->refcnt
);
465 rcu_assign_pointer(dest
->svc
, svc
);
468 static void ip_vs_service_free(struct ip_vs_service
*svc
)
470 free_percpu(svc
->stats
.cpustats
);
474 static void ip_vs_service_rcu_free(struct rcu_head
*head
)
476 struct ip_vs_service
*svc
;
478 svc
= container_of(head
, struct ip_vs_service
, rcu_head
);
479 ip_vs_service_free(svc
);
482 static void __ip_vs_svc_put(struct ip_vs_service
*svc
, bool do_delay
)
484 if (atomic_dec_and_test(&svc
->refcnt
)) {
485 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
487 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
490 call_rcu(&svc
->rcu_head
, ip_vs_service_rcu_free
);
492 ip_vs_service_free(svc
);
498 * Returns hash value for real service
500 static inline unsigned int ip_vs_rs_hashkey(int af
,
501 const union nf_inet_addr
*addr
,
504 register unsigned int porth
= ntohs(port
);
505 __be32 addr_fold
= addr
->ip
;
507 #ifdef CONFIG_IP_VS_IPV6
509 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
510 addr
->ip6
[2]^addr
->ip6
[3];
513 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
517 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
518 static void ip_vs_rs_hash(struct netns_ipvs
*ipvs
, struct ip_vs_dest
*dest
)
522 if (dest
->in_rs_table
)
526 * Hash by proto,addr,port,
527 * which are the parameters of the real service.
529 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
531 hlist_add_head_rcu(&dest
->d_list
, &ipvs
->rs_table
[hash
]);
532 dest
->in_rs_table
= 1;
535 /* Unhash ip_vs_dest from rs_table. */
536 static void ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
539 * Remove it from the rs_table table.
541 if (dest
->in_rs_table
) {
542 hlist_del_rcu(&dest
->d_list
);
543 dest
->in_rs_table
= 0;
547 /* Check if real service by <proto,addr,port> is present */
548 bool ip_vs_has_real_service(struct net
*net
, int af
, __u16 protocol
,
549 const union nf_inet_addr
*daddr
, __be16 dport
)
551 struct netns_ipvs
*ipvs
= net_ipvs(net
);
553 struct ip_vs_dest
*dest
;
555 /* Check for "full" addressed entries */
556 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
559 hlist_for_each_entry_rcu(dest
, &ipvs
->rs_table
[hash
], d_list
) {
560 if (dest
->port
== dport
&&
562 ip_vs_addr_equal(af
, &dest
->addr
, daddr
) &&
563 (dest
->protocol
== protocol
|| dest
->vfwmark
)) {
574 /* Lookup destination by {addr,port} in the given service
575 * Called under RCU lock.
577 static struct ip_vs_dest
*
578 ip_vs_lookup_dest(struct ip_vs_service
*svc
, int dest_af
,
579 const union nf_inet_addr
*daddr
, __be16 dport
)
581 struct ip_vs_dest
*dest
;
584 * Find the destination for the given service
586 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
587 if ((dest
->af
== dest_af
) &&
588 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
589 (dest
->port
== dport
)) {
599 * Find destination by {daddr,dport,vaddr,protocol}
600 * Created to be used in ip_vs_process_message() in
601 * the backup synchronization daemon. It finds the
602 * destination to be bound to the received connection
604 * Called under RCU lock, no refcnt is returned.
606 struct ip_vs_dest
*ip_vs_find_dest(struct net
*net
, int svc_af
, int dest_af
,
607 const union nf_inet_addr
*daddr
,
609 const union nf_inet_addr
*vaddr
,
610 __be16 vport
, __u16 protocol
, __u32 fwmark
,
613 struct ip_vs_dest
*dest
;
614 struct ip_vs_service
*svc
;
617 svc
= ip_vs_service_find(net
, svc_af
, fwmark
, protocol
, vaddr
, vport
);
620 if (fwmark
&& (flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
)
622 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
);
624 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
^ dport
);
628 void ip_vs_dest_dst_rcu_free(struct rcu_head
*head
)
630 struct ip_vs_dest_dst
*dest_dst
= container_of(head
,
631 struct ip_vs_dest_dst
,
634 dst_release(dest_dst
->dst_cache
);
638 /* Release dest_dst and dst_cache for dest in user context */
639 static void __ip_vs_dst_cache_reset(struct ip_vs_dest
*dest
)
641 struct ip_vs_dest_dst
*old
;
643 old
= rcu_dereference_protected(dest
->dest_dst
, 1);
645 RCU_INIT_POINTER(dest
->dest_dst
, NULL
);
646 call_rcu(&old
->rcu_head
, ip_vs_dest_dst_rcu_free
);
651 * Lookup dest by {svc,addr,port} in the destination trash.
652 * The destination trash is used to hold the destinations that are removed
653 * from the service table but are still referenced by some conn entries.
654 * The reason to add the destination trash is when the dest is temporary
655 * down (either by administrator or by monitor program), the dest can be
656 * picked back from the trash, the remaining connections to the dest can
657 * continue, and the counting information of the dest is also useful for
660 static struct ip_vs_dest
*
661 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, int dest_af
,
662 const union nf_inet_addr
*daddr
, __be16 dport
)
664 struct ip_vs_dest
*dest
;
665 struct netns_ipvs
*ipvs
= svc
->ipvs
;
668 * Find the destination in trash
670 spin_lock_bh(&ipvs
->dest_trash_lock
);
671 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
672 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
675 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
677 atomic_read(&dest
->refcnt
));
678 if (dest
->af
== dest_af
&&
679 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
680 dest
->port
== dport
&&
681 dest
->vfwmark
== svc
->fwmark
&&
682 dest
->protocol
== svc
->protocol
&&
684 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
685 dest
->vport
== svc
->port
))) {
687 list_del(&dest
->t_list
);
688 ip_vs_dest_hold(dest
);
696 spin_unlock_bh(&ipvs
->dest_trash_lock
);
701 static void ip_vs_dest_free(struct ip_vs_dest
*dest
)
703 struct ip_vs_service
*svc
= rcu_dereference_protected(dest
->svc
, 1);
705 __ip_vs_dst_cache_reset(dest
);
706 __ip_vs_svc_put(svc
, false);
707 free_percpu(dest
->stats
.cpustats
);
708 ip_vs_dest_put_and_free(dest
);
712 * Clean up all the destinations in the trash
713 * Called by the ip_vs_control_cleanup()
715 * When the ip_vs_control_clearup is activated by ipvs module exit,
716 * the service tables must have been flushed and all the connections
717 * are expired, and the refcnt of each destination in the trash must
718 * be 0, so we simply release them here.
720 static void ip_vs_trash_cleanup(struct net
*net
)
722 struct ip_vs_dest
*dest
, *nxt
;
723 struct netns_ipvs
*ipvs
= net_ipvs(net
);
725 del_timer_sync(&ipvs
->dest_trash_timer
);
726 /* No need to use dest_trash_lock */
727 list_for_each_entry_safe(dest
, nxt
, &ipvs
->dest_trash
, t_list
) {
728 list_del(&dest
->t_list
);
729 ip_vs_dest_free(dest
);
734 ip_vs_copy_stats(struct ip_vs_kstats
*dst
, struct ip_vs_stats
*src
)
736 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
738 spin_lock_bh(&src
->lock
);
740 IP_VS_SHOW_STATS_COUNTER(conns
);
741 IP_VS_SHOW_STATS_COUNTER(inpkts
);
742 IP_VS_SHOW_STATS_COUNTER(outpkts
);
743 IP_VS_SHOW_STATS_COUNTER(inbytes
);
744 IP_VS_SHOW_STATS_COUNTER(outbytes
);
746 ip_vs_read_estimator(dst
, src
);
748 spin_unlock_bh(&src
->lock
);
752 ip_vs_export_stats_user(struct ip_vs_stats_user
*dst
, struct ip_vs_kstats
*src
)
754 dst
->conns
= (u32
)src
->conns
;
755 dst
->inpkts
= (u32
)src
->inpkts
;
756 dst
->outpkts
= (u32
)src
->outpkts
;
757 dst
->inbytes
= src
->inbytes
;
758 dst
->outbytes
= src
->outbytes
;
759 dst
->cps
= (u32
)src
->cps
;
760 dst
->inpps
= (u32
)src
->inpps
;
761 dst
->outpps
= (u32
)src
->outpps
;
762 dst
->inbps
= (u32
)src
->inbps
;
763 dst
->outbps
= (u32
)src
->outbps
;
767 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
769 spin_lock_bh(&stats
->lock
);
771 /* get current counters as zero point, rates are zeroed */
773 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
775 IP_VS_ZERO_STATS_COUNTER(conns
);
776 IP_VS_ZERO_STATS_COUNTER(inpkts
);
777 IP_VS_ZERO_STATS_COUNTER(outpkts
);
778 IP_VS_ZERO_STATS_COUNTER(inbytes
);
779 IP_VS_ZERO_STATS_COUNTER(outbytes
);
781 ip_vs_zero_estimator(stats
);
783 spin_unlock_bh(&stats
->lock
);
787 * Update a destination in the given service
790 __ip_vs_update_dest(struct ip_vs_service
*svc
, struct ip_vs_dest
*dest
,
791 struct ip_vs_dest_user_kern
*udest
, int add
)
793 struct netns_ipvs
*ipvs
= svc
->ipvs
;
794 struct ip_vs_service
*old_svc
;
795 struct ip_vs_scheduler
*sched
;
798 /* We cannot modify an address and change the address family */
799 BUG_ON(!add
&& udest
->af
!= dest
->af
);
801 if (add
&& udest
->af
!= svc
->af
)
802 ipvs
->mixed_address_family_dests
++;
804 /* set the weight and the flags */
805 atomic_set(&dest
->weight
, udest
->weight
);
806 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
807 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
809 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
810 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
811 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
814 * Put the real service in rs_table if not present.
815 * For now only for NAT!
817 ip_vs_rs_hash(ipvs
, dest
);
819 atomic_set(&dest
->conn_flags
, conn_flags
);
821 /* bind the service */
822 old_svc
= rcu_dereference_protected(dest
->svc
, 1);
824 __ip_vs_bind_svc(dest
, svc
);
826 if (old_svc
!= svc
) {
827 ip_vs_zero_stats(&dest
->stats
);
828 __ip_vs_bind_svc(dest
, svc
);
829 __ip_vs_svc_put(old_svc
, true);
833 /* set the dest status flags */
834 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
836 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
837 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
838 dest
->u_threshold
= udest
->u_threshold
;
839 dest
->l_threshold
= udest
->l_threshold
;
841 dest
->af
= udest
->af
;
843 spin_lock_bh(&dest
->dst_lock
);
844 __ip_vs_dst_cache_reset(dest
);
845 spin_unlock_bh(&dest
->dst_lock
);
848 ip_vs_start_estimator(svc
->ipvs
->net
, &dest
->stats
);
849 list_add_rcu(&dest
->n_list
, &svc
->destinations
);
851 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
852 if (sched
&& sched
->add_dest
)
853 sched
->add_dest(svc
, dest
);
855 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
856 if (sched
&& sched
->upd_dest
)
857 sched
->upd_dest(svc
, dest
);
863 * Create a destination for the given service
866 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
867 struct ip_vs_dest
**dest_p
)
869 struct ip_vs_dest
*dest
;
870 unsigned int atype
, i
;
874 #ifdef CONFIG_IP_VS_IPV6
875 if (udest
->af
== AF_INET6
) {
876 atype
= ipv6_addr_type(&udest
->addr
.in6
);
877 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
878 atype
& IPV6_ADDR_LINKLOCAL
) &&
879 !__ip_vs_addr_is_local_v6(svc
->ipvs
->net
, &udest
->addr
.in6
))
884 atype
= inet_addr_type(svc
->ipvs
->net
, udest
->addr
.ip
);
885 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
889 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
893 dest
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
894 if (!dest
->stats
.cpustats
)
897 for_each_possible_cpu(i
) {
898 struct ip_vs_cpu_stats
*ip_vs_dest_stats
;
899 ip_vs_dest_stats
= per_cpu_ptr(dest
->stats
.cpustats
, i
);
900 u64_stats_init(&ip_vs_dest_stats
->syncp
);
903 dest
->af
= udest
->af
;
904 dest
->protocol
= svc
->protocol
;
905 dest
->vaddr
= svc
->addr
;
906 dest
->vport
= svc
->port
;
907 dest
->vfwmark
= svc
->fwmark
;
908 ip_vs_addr_copy(udest
->af
, &dest
->addr
, &udest
->addr
);
909 dest
->port
= udest
->port
;
911 atomic_set(&dest
->activeconns
, 0);
912 atomic_set(&dest
->inactconns
, 0);
913 atomic_set(&dest
->persistconns
, 0);
914 atomic_set(&dest
->refcnt
, 1);
916 INIT_HLIST_NODE(&dest
->d_list
);
917 spin_lock_init(&dest
->dst_lock
);
918 spin_lock_init(&dest
->stats
.lock
);
919 __ip_vs_update_dest(svc
, dest
, udest
, 1);
933 * Add a destination into an existing service
936 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
938 struct ip_vs_dest
*dest
;
939 union nf_inet_addr daddr
;
940 __be16 dport
= udest
->port
;
945 if (udest
->weight
< 0) {
946 pr_err("%s(): server weight less than zero\n", __func__
);
950 if (udest
->l_threshold
> udest
->u_threshold
) {
951 pr_err("%s(): lower threshold is higher than upper threshold\n",
956 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
958 /* We use function that requires RCU lock */
960 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
964 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
969 * Check if the dest already exists in the trash and
970 * is from the same service
972 dest
= ip_vs_trash_get_dest(svc
, udest
->af
, &daddr
, dport
);
975 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
976 "dest->refcnt=%d, service %u/%s:%u\n",
977 IP_VS_DBG_ADDR(udest
->af
, &daddr
), ntohs(dport
),
978 atomic_read(&dest
->refcnt
),
980 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
983 __ip_vs_update_dest(svc
, dest
, udest
, 1);
987 * Allocate and initialize the dest structure
989 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
998 * Edit a destination in the given service
1001 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1003 struct ip_vs_dest
*dest
;
1004 union nf_inet_addr daddr
;
1005 __be16 dport
= udest
->port
;
1009 if (udest
->weight
< 0) {
1010 pr_err("%s(): server weight less than zero\n", __func__
);
1014 if (udest
->l_threshold
> udest
->u_threshold
) {
1015 pr_err("%s(): lower threshold is higher than upper threshold\n",
1020 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
1022 /* We use function that requires RCU lock */
1024 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
1028 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1032 __ip_vs_update_dest(svc
, dest
, udest
, 0);
1039 * Delete a destination (must be already unlinked from the service)
1041 static void __ip_vs_del_dest(struct net
*net
, struct ip_vs_dest
*dest
,
1044 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1046 ip_vs_stop_estimator(net
, &dest
->stats
);
1049 * Remove it from the d-linked list with the real services.
1051 ip_vs_rs_unhash(dest
);
1053 spin_lock_bh(&ipvs
->dest_trash_lock
);
1054 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1055 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
), ntohs(dest
->port
),
1056 atomic_read(&dest
->refcnt
));
1057 if (list_empty(&ipvs
->dest_trash
) && !cleanup
)
1058 mod_timer(&ipvs
->dest_trash_timer
,
1059 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1060 /* dest lives in trash without reference */
1061 list_add(&dest
->t_list
, &ipvs
->dest_trash
);
1062 dest
->idle_start
= 0;
1063 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1064 ip_vs_dest_put(dest
);
1069 * Unlink a destination from the given service
1071 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1072 struct ip_vs_dest
*dest
,
1075 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1078 * Remove it from the d-linked destination list.
1080 list_del_rcu(&dest
->n_list
);
1083 if (dest
->af
!= svc
->af
)
1084 svc
->ipvs
->mixed_address_family_dests
--;
1087 struct ip_vs_scheduler
*sched
;
1089 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1090 if (sched
&& sched
->del_dest
)
1091 sched
->del_dest(svc
, dest
);
1097 * Delete a destination server in the given service
1100 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1102 struct ip_vs_dest
*dest
;
1103 __be16 dport
= udest
->port
;
1107 /* We use function that requires RCU lock */
1109 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &udest
->addr
, dport
);
1113 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1118 * Unlink dest from the service
1120 __ip_vs_unlink_dest(svc
, dest
, 1);
1123 * Delete the destination
1125 __ip_vs_del_dest(svc
->ipvs
->net
, dest
, false);
1132 static void ip_vs_dest_trash_expire(unsigned long data
)
1134 struct net
*net
= (struct net
*) data
;
1135 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1136 struct ip_vs_dest
*dest
, *next
;
1137 unsigned long now
= jiffies
;
1139 spin_lock(&ipvs
->dest_trash_lock
);
1140 list_for_each_entry_safe(dest
, next
, &ipvs
->dest_trash
, t_list
) {
1141 if (atomic_read(&dest
->refcnt
) > 0)
1143 if (dest
->idle_start
) {
1144 if (time_before(now
, dest
->idle_start
+
1145 IP_VS_DEST_TRASH_PERIOD
))
1148 dest
->idle_start
= max(1UL, now
);
1151 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1153 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1155 list_del(&dest
->t_list
);
1156 ip_vs_dest_free(dest
);
1158 if (!list_empty(&ipvs
->dest_trash
))
1159 mod_timer(&ipvs
->dest_trash_timer
,
1160 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1161 spin_unlock(&ipvs
->dest_trash_lock
);
1165 * Add a service into the service hash table
1168 ip_vs_add_service(struct net
*net
, struct ip_vs_service_user_kern
*u
,
1169 struct ip_vs_service
**svc_p
)
1172 struct ip_vs_scheduler
*sched
= NULL
;
1173 struct ip_vs_pe
*pe
= NULL
;
1174 struct ip_vs_service
*svc
= NULL
;
1175 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1177 /* increase the module use count */
1178 ip_vs_use_count_inc();
1180 /* Lookup the scheduler by 'u->sched_name' */
1181 if (strcmp(u
->sched_name
, "none")) {
1182 sched
= ip_vs_scheduler_get(u
->sched_name
);
1184 pr_info("Scheduler module ip_vs_%s not found\n",
1191 if (u
->pe_name
&& *u
->pe_name
) {
1192 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1194 pr_info("persistence engine module ip_vs_pe_%s "
1195 "not found\n", u
->pe_name
);
1201 #ifdef CONFIG_IP_VS_IPV6
1202 if (u
->af
== AF_INET6
) {
1203 __u32 plen
= (__force __u32
) u
->netmask
;
1205 if (plen
< 1 || plen
> 128) {
1212 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1214 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1218 svc
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
1219 if (!svc
->stats
.cpustats
) {
1224 for_each_possible_cpu(i
) {
1225 struct ip_vs_cpu_stats
*ip_vs_stats
;
1226 ip_vs_stats
= per_cpu_ptr(svc
->stats
.cpustats
, i
);
1227 u64_stats_init(&ip_vs_stats
->syncp
);
1231 /* I'm the first user of the service */
1232 atomic_set(&svc
->refcnt
, 0);
1235 svc
->protocol
= u
->protocol
;
1236 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1237 svc
->port
= u
->port
;
1238 svc
->fwmark
= u
->fwmark
;
1239 svc
->flags
= u
->flags
;
1240 svc
->timeout
= u
->timeout
* HZ
;
1241 svc
->netmask
= u
->netmask
;
1244 INIT_LIST_HEAD(&svc
->destinations
);
1245 spin_lock_init(&svc
->sched_lock
);
1246 spin_lock_init(&svc
->stats
.lock
);
1248 /* Bind the scheduler */
1250 ret
= ip_vs_bind_scheduler(svc
, sched
);
1256 /* Bind the ct retriever */
1257 RCU_INIT_POINTER(svc
->pe
, pe
);
1260 /* Update the virtual service counters */
1261 if (svc
->port
== FTPPORT
)
1262 atomic_inc(&ipvs
->ftpsvc_counter
);
1263 else if (svc
->port
== 0)
1264 atomic_inc(&ipvs
->nullsvc_counter
);
1266 ip_vs_start_estimator(net
, &svc
->stats
);
1268 /* Count only IPv4 services for old get/setsockopt interface */
1269 if (svc
->af
== AF_INET
)
1270 ipvs
->num_services
++;
1272 /* Hash the service into the service table */
1273 ip_vs_svc_hash(svc
);
1276 /* Now there is a service - full throttle */
1283 ip_vs_unbind_scheduler(svc
, sched
);
1284 ip_vs_service_free(svc
);
1286 ip_vs_scheduler_put(sched
);
1289 /* decrease the module use count */
1290 ip_vs_use_count_dec();
1297 * Edit a service and bind it with a new scheduler
1300 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1302 struct ip_vs_scheduler
*sched
= NULL
, *old_sched
;
1303 struct ip_vs_pe
*pe
= NULL
, *old_pe
= NULL
;
1307 * Lookup the scheduler, by 'u->sched_name'
1309 if (strcmp(u
->sched_name
, "none")) {
1310 sched
= ip_vs_scheduler_get(u
->sched_name
);
1312 pr_info("Scheduler module ip_vs_%s not found\n",
1319 if (u
->pe_name
&& *u
->pe_name
) {
1320 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1322 pr_info("persistence engine module ip_vs_pe_%s "
1323 "not found\n", u
->pe_name
);
1330 #ifdef CONFIG_IP_VS_IPV6
1331 if (u
->af
== AF_INET6
) {
1332 __u32 plen
= (__force __u32
) u
->netmask
;
1334 if (plen
< 1 || plen
> 128) {
1341 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1342 if (sched
!= old_sched
) {
1344 ip_vs_unbind_scheduler(svc
, old_sched
);
1345 RCU_INIT_POINTER(svc
->scheduler
, NULL
);
1346 /* Wait all svc->sched_data users */
1349 /* Bind the new scheduler */
1351 ret
= ip_vs_bind_scheduler(svc
, sched
);
1353 ip_vs_scheduler_put(sched
);
1360 * Set the flags and timeout value
1362 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1363 svc
->timeout
= u
->timeout
* HZ
;
1364 svc
->netmask
= u
->netmask
;
1366 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1368 rcu_assign_pointer(svc
->pe
, pe
);
1371 ip_vs_scheduler_put(old_sched
);
1372 ip_vs_pe_put(old_pe
);
1377 * Delete a service from the service list
1378 * - The service must be unlinked, unlocked and not referenced!
1379 * - We are called under _bh lock
1381 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
)
1383 struct ip_vs_dest
*dest
, *nxt
;
1384 struct ip_vs_scheduler
*old_sched
;
1385 struct ip_vs_pe
*old_pe
;
1386 struct netns_ipvs
*ipvs
= svc
->ipvs
;
1388 pr_info("%s: enter\n", __func__
);
1390 /* Count only IPv4 services for old get/setsockopt interface */
1391 if (svc
->af
== AF_INET
)
1392 ipvs
->num_services
--;
1394 ip_vs_stop_estimator(svc
->ipvs
->net
, &svc
->stats
);
1396 /* Unbind scheduler */
1397 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1398 ip_vs_unbind_scheduler(svc
, old_sched
);
1399 ip_vs_scheduler_put(old_sched
);
1401 /* Unbind persistence engine, keep svc->pe */
1402 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1403 ip_vs_pe_put(old_pe
);
1406 * Unlink the whole destination list
1408 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1409 __ip_vs_unlink_dest(svc
, dest
, 0);
1410 __ip_vs_del_dest(svc
->ipvs
->net
, dest
, cleanup
);
1414 * Update the virtual service counters
1416 if (svc
->port
== FTPPORT
)
1417 atomic_dec(&ipvs
->ftpsvc_counter
);
1418 else if (svc
->port
== 0)
1419 atomic_dec(&ipvs
->nullsvc_counter
);
1422 * Free the service if nobody refers to it
1424 __ip_vs_svc_put(svc
, true);
1426 /* decrease the module use count */
1427 ip_vs_use_count_dec();
1431 * Unlink a service from list and try to delete it if its refcnt reached 0
1433 static void ip_vs_unlink_service(struct ip_vs_service
*svc
, bool cleanup
)
1435 /* Hold svc to avoid double release from dest_trash */
1436 atomic_inc(&svc
->refcnt
);
1438 * Unhash it from the service table
1440 ip_vs_svc_unhash(svc
);
1442 __ip_vs_del_service(svc
, cleanup
);
1446 * Delete a service from the service list
1448 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1452 ip_vs_unlink_service(svc
, false);
1459 * Flush all the virtual services
1461 static int ip_vs_flush(struct net
*net
, bool cleanup
)
1463 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1465 struct ip_vs_service
*svc
;
1466 struct hlist_node
*n
;
1469 * Flush the service table hashed by <netns,protocol,addr,port>
1471 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1472 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_table
[idx
],
1474 if (svc
->ipvs
== ipvs
)
1475 ip_vs_unlink_service(svc
, cleanup
);
1480 * Flush the service table hashed by fwmark
1482 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1483 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_fwm_table
[idx
],
1485 if (svc
->ipvs
== ipvs
)
1486 ip_vs_unlink_service(svc
, cleanup
);
1494 * Delete service by {netns} in the service table.
1495 * Called by __ip_vs_cleanup()
1497 void ip_vs_service_net_cleanup(struct net
*net
)
1500 /* Check for "full" addressed entries */
1501 mutex_lock(&__ip_vs_mutex
);
1502 ip_vs_flush(net
, true);
1503 mutex_unlock(&__ip_vs_mutex
);
1507 /* Put all references for device (dst_cache) */
1509 ip_vs_forget_dev(struct ip_vs_dest
*dest
, struct net_device
*dev
)
1511 struct ip_vs_dest_dst
*dest_dst
;
1513 spin_lock_bh(&dest
->dst_lock
);
1514 dest_dst
= rcu_dereference_protected(dest
->dest_dst
, 1);
1515 if (dest_dst
&& dest_dst
->dst_cache
->dev
== dev
) {
1516 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1518 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1520 atomic_read(&dest
->refcnt
));
1521 __ip_vs_dst_cache_reset(dest
);
1523 spin_unlock_bh(&dest
->dst_lock
);
1526 /* Netdev event receiver
1527 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1529 static int ip_vs_dst_event(struct notifier_block
*this, unsigned long event
,
1532 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1533 struct net
*net
= dev_net(dev
);
1534 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1535 struct ip_vs_service
*svc
;
1536 struct ip_vs_dest
*dest
;
1539 if (event
!= NETDEV_DOWN
|| !ipvs
)
1541 IP_VS_DBG(3, "%s() dev=%s\n", __func__
, dev
->name
);
1543 mutex_lock(&__ip_vs_mutex
);
1544 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1545 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1546 if (svc
->ipvs
== ipvs
) {
1547 list_for_each_entry(dest
, &svc
->destinations
,
1549 ip_vs_forget_dev(dest
, dev
);
1554 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1555 if (svc
->ipvs
== ipvs
) {
1556 list_for_each_entry(dest
, &svc
->destinations
,
1558 ip_vs_forget_dev(dest
, dev
);
1565 spin_lock_bh(&ipvs
->dest_trash_lock
);
1566 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
1567 ip_vs_forget_dev(dest
, dev
);
1569 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1570 mutex_unlock(&__ip_vs_mutex
);
1576 * Zero counters in a service or all services
1578 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1580 struct ip_vs_dest
*dest
;
1582 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1583 ip_vs_zero_stats(&dest
->stats
);
1585 ip_vs_zero_stats(&svc
->stats
);
1589 static int ip_vs_zero_all(struct net
*net
)
1591 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1593 struct ip_vs_service
*svc
;
1595 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1596 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1597 if (svc
->ipvs
== ipvs
)
1598 ip_vs_zero_service(svc
);
1602 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1603 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1604 if (svc
->ipvs
== ipvs
)
1605 ip_vs_zero_service(svc
);
1609 ip_vs_zero_stats(&ipvs
->tot_stats
);
1613 #ifdef CONFIG_SYSCTL
1616 static int three
= 3;
1619 proc_do_defense_mode(struct ctl_table
*table
, int write
,
1620 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1622 struct netns_ipvs
*ipvs
= table
->extra2
;
1623 int *valp
= table
->data
;
1627 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1628 if (write
&& (*valp
!= val
)) {
1629 if ((*valp
< 0) || (*valp
> 3)) {
1630 /* Restore the correct value */
1633 update_defense_level(ipvs
);
1640 proc_do_sync_threshold(struct ctl_table
*table
, int write
,
1641 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1643 int *valp
= table
->data
;
1647 /* backup the value first */
1648 memcpy(val
, valp
, sizeof(val
));
1650 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1651 if (write
&& (valp
[0] < 0 || valp
[1] < 0 ||
1652 (valp
[0] >= valp
[1] && valp
[1]))) {
1653 /* Restore the correct value */
1654 memcpy(valp
, val
, sizeof(val
));
1660 proc_do_sync_mode(struct ctl_table
*table
, int write
,
1661 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1663 int *valp
= table
->data
;
1667 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1668 if (write
&& (*valp
!= val
)) {
1669 if ((*valp
< 0) || (*valp
> 1)) {
1670 /* Restore the correct value */
1678 proc_do_sync_ports(struct ctl_table
*table
, int write
,
1679 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1681 int *valp
= table
->data
;
1685 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1686 if (write
&& (*valp
!= val
)) {
1687 if (*valp
< 1 || !is_power_of_2(*valp
)) {
1688 /* Restore the correct value */
1696 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1697 * Do not change order or insert new entries without
1698 * align with netns init in ip_vs_control_net_init()
1701 static struct ctl_table vs_vars
[] = {
1703 .procname
= "amemthresh",
1704 .maxlen
= sizeof(int),
1706 .proc_handler
= proc_dointvec
,
1709 .procname
= "am_droprate",
1710 .maxlen
= sizeof(int),
1712 .proc_handler
= proc_dointvec
,
1715 .procname
= "drop_entry",
1716 .maxlen
= sizeof(int),
1718 .proc_handler
= proc_do_defense_mode
,
1721 .procname
= "drop_packet",
1722 .maxlen
= sizeof(int),
1724 .proc_handler
= proc_do_defense_mode
,
1726 #ifdef CONFIG_IP_VS_NFCT
1728 .procname
= "conntrack",
1729 .maxlen
= sizeof(int),
1731 .proc_handler
= &proc_dointvec
,
1735 .procname
= "secure_tcp",
1736 .maxlen
= sizeof(int),
1738 .proc_handler
= proc_do_defense_mode
,
1741 .procname
= "snat_reroute",
1742 .maxlen
= sizeof(int),
1744 .proc_handler
= &proc_dointvec
,
1747 .procname
= "sync_version",
1748 .maxlen
= sizeof(int),
1750 .proc_handler
= &proc_do_sync_mode
,
1753 .procname
= "sync_ports",
1754 .maxlen
= sizeof(int),
1756 .proc_handler
= &proc_do_sync_ports
,
1759 .procname
= "sync_persist_mode",
1760 .maxlen
= sizeof(int),
1762 .proc_handler
= proc_dointvec
,
1765 .procname
= "sync_qlen_max",
1766 .maxlen
= sizeof(unsigned long),
1768 .proc_handler
= proc_doulongvec_minmax
,
1771 .procname
= "sync_sock_size",
1772 .maxlen
= sizeof(int),
1774 .proc_handler
= proc_dointvec
,
1777 .procname
= "cache_bypass",
1778 .maxlen
= sizeof(int),
1780 .proc_handler
= proc_dointvec
,
1783 .procname
= "expire_nodest_conn",
1784 .maxlen
= sizeof(int),
1786 .proc_handler
= proc_dointvec
,
1789 .procname
= "sloppy_tcp",
1790 .maxlen
= sizeof(int),
1792 .proc_handler
= proc_dointvec
,
1795 .procname
= "sloppy_sctp",
1796 .maxlen
= sizeof(int),
1798 .proc_handler
= proc_dointvec
,
1801 .procname
= "expire_quiescent_template",
1802 .maxlen
= sizeof(int),
1804 .proc_handler
= proc_dointvec
,
1807 .procname
= "sync_threshold",
1809 sizeof(((struct netns_ipvs
*)0)->sysctl_sync_threshold
),
1811 .proc_handler
= proc_do_sync_threshold
,
1814 .procname
= "sync_refresh_period",
1815 .maxlen
= sizeof(int),
1817 .proc_handler
= proc_dointvec_jiffies
,
1820 .procname
= "sync_retries",
1821 .maxlen
= sizeof(int),
1823 .proc_handler
= proc_dointvec_minmax
,
1828 .procname
= "nat_icmp_send",
1829 .maxlen
= sizeof(int),
1831 .proc_handler
= proc_dointvec
,
1834 .procname
= "pmtu_disc",
1835 .maxlen
= sizeof(int),
1837 .proc_handler
= proc_dointvec
,
1840 .procname
= "backup_only",
1841 .maxlen
= sizeof(int),
1843 .proc_handler
= proc_dointvec
,
1846 .procname
= "conn_reuse_mode",
1847 .maxlen
= sizeof(int),
1849 .proc_handler
= proc_dointvec
,
1852 .procname
= "schedule_icmp",
1853 .maxlen
= sizeof(int),
1855 .proc_handler
= proc_dointvec
,
1858 .procname
= "ignore_tunneled",
1859 .maxlen
= sizeof(int),
1861 .proc_handler
= proc_dointvec
,
1863 #ifdef CONFIG_IP_VS_DEBUG
1865 .procname
= "debug_level",
1866 .data
= &sysctl_ip_vs_debug_level
,
1867 .maxlen
= sizeof(int),
1869 .proc_handler
= proc_dointvec
,
1877 #ifdef CONFIG_PROC_FS
1880 struct seq_net_private p
; /* Do not move this, netns depends upon it*/
1881 struct hlist_head
*table
;
1886 * Write the contents of the VS rule table to a PROCfs file.
1887 * (It is kept just for backward compatibility)
1889 static inline const char *ip_vs_fwd_name(unsigned int flags
)
1891 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1892 case IP_VS_CONN_F_LOCALNODE
:
1894 case IP_VS_CONN_F_TUNNEL
:
1896 case IP_VS_CONN_F_DROUTE
:
1904 /* Get the Nth entry in the two lists */
1905 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1907 struct net
*net
= seq_file_net(seq
);
1908 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1909 struct ip_vs_iter
*iter
= seq
->private;
1911 struct ip_vs_service
*svc
;
1913 /* look in hash by protocol */
1914 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1915 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1916 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1917 iter
->table
= ip_vs_svc_table
;
1924 /* keep looking in fwmark */
1925 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1926 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[idx
],
1928 if ((svc
->ipvs
== ipvs
) && pos
-- == 0) {
1929 iter
->table
= ip_vs_svc_fwm_table
;
1939 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1943 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1947 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1949 struct hlist_node
*e
;
1950 struct ip_vs_iter
*iter
;
1951 struct ip_vs_service
*svc
;
1954 if (v
== SEQ_START_TOKEN
)
1955 return ip_vs_info_array(seq
,0);
1958 iter
= seq
->private;
1960 if (iter
->table
== ip_vs_svc_table
) {
1961 /* next service in table hashed by protocol */
1962 e
= rcu_dereference(hlist_next_rcu(&svc
->s_list
));
1964 return hlist_entry(e
, struct ip_vs_service
, s_list
);
1966 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1967 hlist_for_each_entry_rcu(svc
,
1968 &ip_vs_svc_table
[iter
->bucket
],
1974 iter
->table
= ip_vs_svc_fwm_table
;
1979 /* next service in hashed by fwmark */
1980 e
= rcu_dereference(hlist_next_rcu(&svc
->f_list
));
1982 return hlist_entry(e
, struct ip_vs_service
, f_list
);
1985 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1986 hlist_for_each_entry_rcu(svc
,
1987 &ip_vs_svc_fwm_table
[iter
->bucket
],
1995 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
2002 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
2004 if (v
== SEQ_START_TOKEN
) {
2006 "IP Virtual Server version %d.%d.%d (size=%d)\n",
2007 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2009 "Prot LocalAddress:Port Scheduler Flags\n");
2011 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2013 const struct ip_vs_service
*svc
= v
;
2014 const struct ip_vs_iter
*iter
= seq
->private;
2015 const struct ip_vs_dest
*dest
;
2016 struct ip_vs_scheduler
*sched
= rcu_dereference(svc
->scheduler
);
2017 char *sched_name
= sched
? sched
->name
: "none";
2019 if (iter
->table
== ip_vs_svc_table
) {
2020 #ifdef CONFIG_IP_VS_IPV6
2021 if (svc
->af
== AF_INET6
)
2022 seq_printf(seq
, "%s [%pI6]:%04X %s ",
2023 ip_vs_proto_name(svc
->protocol
),
2029 seq_printf(seq
, "%s %08X:%04X %s %s ",
2030 ip_vs_proto_name(svc
->protocol
),
2031 ntohl(svc
->addr
.ip
),
2034 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2036 seq_printf(seq
, "FWM %08X %s %s",
2037 svc
->fwmark
, sched_name
,
2038 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2041 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
2042 seq_printf(seq
, "persistent %d %08X\n",
2044 ntohl(svc
->netmask
));
2046 seq_putc(seq
, '\n');
2048 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
2049 #ifdef CONFIG_IP_VS_IPV6
2050 if (dest
->af
== AF_INET6
)
2053 " %-7s %-6d %-10d %-10d\n",
2056 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2057 atomic_read(&dest
->weight
),
2058 atomic_read(&dest
->activeconns
),
2059 atomic_read(&dest
->inactconns
));
2064 "%-7s %-6d %-10d %-10d\n",
2065 ntohl(dest
->addr
.ip
),
2067 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2068 atomic_read(&dest
->weight
),
2069 atomic_read(&dest
->activeconns
),
2070 atomic_read(&dest
->inactconns
));
2077 static const struct seq_operations ip_vs_info_seq_ops
= {
2078 .start
= ip_vs_info_seq_start
,
2079 .next
= ip_vs_info_seq_next
,
2080 .stop
= ip_vs_info_seq_stop
,
2081 .show
= ip_vs_info_seq_show
,
2084 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
2086 return seq_open_net(inode
, file
, &ip_vs_info_seq_ops
,
2087 sizeof(struct ip_vs_iter
));
2090 static const struct file_operations ip_vs_info_fops
= {
2091 .owner
= THIS_MODULE
,
2092 .open
= ip_vs_info_open
,
2094 .llseek
= seq_lseek
,
2095 .release
= seq_release_net
,
2098 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
2100 struct net
*net
= seq_file_single_net(seq
);
2101 struct ip_vs_kstats show
;
2103 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2105 " Total Incoming Outgoing Incoming Outgoing\n");
2107 " Conns Packets Packets Bytes Bytes\n");
2109 ip_vs_copy_stats(&show
, &net_ipvs(net
)->tot_stats
);
2110 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n\n",
2111 (unsigned long long)show
.conns
,
2112 (unsigned long long)show
.inpkts
,
2113 (unsigned long long)show
.outpkts
,
2114 (unsigned long long)show
.inbytes
,
2115 (unsigned long long)show
.outbytes
);
2117 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2119 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2120 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n",
2121 (unsigned long long)show
.cps
,
2122 (unsigned long long)show
.inpps
,
2123 (unsigned long long)show
.outpps
,
2124 (unsigned long long)show
.inbps
,
2125 (unsigned long long)show
.outbps
);
2130 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
2132 return single_open_net(inode
, file
, ip_vs_stats_show
);
2135 static const struct file_operations ip_vs_stats_fops
= {
2136 .owner
= THIS_MODULE
,
2137 .open
= ip_vs_stats_seq_open
,
2139 .llseek
= seq_lseek
,
2140 .release
= single_release_net
,
2143 static int ip_vs_stats_percpu_show(struct seq_file
*seq
, void *v
)
2145 struct net
*net
= seq_file_single_net(seq
);
2146 struct ip_vs_stats
*tot_stats
= &net_ipvs(net
)->tot_stats
;
2147 struct ip_vs_cpu_stats __percpu
*cpustats
= tot_stats
->cpustats
;
2148 struct ip_vs_kstats kstats
;
2151 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2153 " Total Incoming Outgoing Incoming Outgoing\n");
2155 "CPU Conns Packets Packets Bytes Bytes\n");
2157 for_each_possible_cpu(i
) {
2158 struct ip_vs_cpu_stats
*u
= per_cpu_ptr(cpustats
, i
);
2160 u64 conns
, inpkts
, outpkts
, inbytes
, outbytes
;
2163 start
= u64_stats_fetch_begin_irq(&u
->syncp
);
2164 conns
= u
->cnt
.conns
;
2165 inpkts
= u
->cnt
.inpkts
;
2166 outpkts
= u
->cnt
.outpkts
;
2167 inbytes
= u
->cnt
.inbytes
;
2168 outbytes
= u
->cnt
.outbytes
;
2169 } while (u64_stats_fetch_retry_irq(&u
->syncp
, start
));
2171 seq_printf(seq
, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2172 i
, (u64
)conns
, (u64
)inpkts
,
2173 (u64
)outpkts
, (u64
)inbytes
,
2177 ip_vs_copy_stats(&kstats
, tot_stats
);
2179 seq_printf(seq
, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2180 (unsigned long long)kstats
.conns
,
2181 (unsigned long long)kstats
.inpkts
,
2182 (unsigned long long)kstats
.outpkts
,
2183 (unsigned long long)kstats
.inbytes
,
2184 (unsigned long long)kstats
.outbytes
);
2186 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2188 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2189 seq_printf(seq
, " %8LX %8LX %8LX %16LX %16LX\n",
2199 static int ip_vs_stats_percpu_seq_open(struct inode
*inode
, struct file
*file
)
2201 return single_open_net(inode
, file
, ip_vs_stats_percpu_show
);
2204 static const struct file_operations ip_vs_stats_percpu_fops
= {
2205 .owner
= THIS_MODULE
,
2206 .open
= ip_vs_stats_percpu_seq_open
,
2208 .llseek
= seq_lseek
,
2209 .release
= single_release_net
,
2214 * Set timeout values for tcp tcpfin udp in the timeout_table.
2216 static int ip_vs_set_timeout(struct net
*net
, struct ip_vs_timeout_user
*u
)
2218 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2219 struct ip_vs_proto_data
*pd
;
2222 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2227 #ifdef CONFIG_IP_VS_PROTO_TCP
2228 if (u
->tcp_timeout
) {
2229 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2230 pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
2231 = u
->tcp_timeout
* HZ
;
2234 if (u
->tcp_fin_timeout
) {
2235 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2236 pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
2237 = u
->tcp_fin_timeout
* HZ
;
2241 #ifdef CONFIG_IP_VS_PROTO_UDP
2242 if (u
->udp_timeout
) {
2243 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2244 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
]
2245 = u
->udp_timeout
* HZ
;
2251 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2253 struct ip_vs_svcdest_user
{
2254 struct ip_vs_service_user s
;
2255 struct ip_vs_dest_user d
;
2258 static const unsigned char set_arglen
[CMDID(IP_VS_SO_SET_MAX
) + 1] = {
2259 [CMDID(IP_VS_SO_SET_ADD
)] = sizeof(struct ip_vs_service_user
),
2260 [CMDID(IP_VS_SO_SET_EDIT
)] = sizeof(struct ip_vs_service_user
),
2261 [CMDID(IP_VS_SO_SET_DEL
)] = sizeof(struct ip_vs_service_user
),
2262 [CMDID(IP_VS_SO_SET_ADDDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2263 [CMDID(IP_VS_SO_SET_DELDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2264 [CMDID(IP_VS_SO_SET_EDITDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2265 [CMDID(IP_VS_SO_SET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2266 [CMDID(IP_VS_SO_SET_STARTDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2267 [CMDID(IP_VS_SO_SET_STOPDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2268 [CMDID(IP_VS_SO_SET_ZERO
)] = sizeof(struct ip_vs_service_user
),
2271 union ip_vs_set_arglen
{
2272 struct ip_vs_service_user field_IP_VS_SO_SET_ADD
;
2273 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT
;
2274 struct ip_vs_service_user field_IP_VS_SO_SET_DEL
;
2275 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST
;
2276 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST
;
2277 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST
;
2278 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT
;
2279 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON
;
2280 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON
;
2281 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO
;
2284 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2286 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2287 struct ip_vs_service_user
*usvc_compat
)
2289 memset(usvc
, 0, sizeof(*usvc
));
2292 usvc
->protocol
= usvc_compat
->protocol
;
2293 usvc
->addr
.ip
= usvc_compat
->addr
;
2294 usvc
->port
= usvc_compat
->port
;
2295 usvc
->fwmark
= usvc_compat
->fwmark
;
2297 /* Deep copy of sched_name is not needed here */
2298 usvc
->sched_name
= usvc_compat
->sched_name
;
2300 usvc
->flags
= usvc_compat
->flags
;
2301 usvc
->timeout
= usvc_compat
->timeout
;
2302 usvc
->netmask
= usvc_compat
->netmask
;
2305 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2306 struct ip_vs_dest_user
*udest_compat
)
2308 memset(udest
, 0, sizeof(*udest
));
2310 udest
->addr
.ip
= udest_compat
->addr
;
2311 udest
->port
= udest_compat
->port
;
2312 udest
->conn_flags
= udest_compat
->conn_flags
;
2313 udest
->weight
= udest_compat
->weight
;
2314 udest
->u_threshold
= udest_compat
->u_threshold
;
2315 udest
->l_threshold
= udest_compat
->l_threshold
;
2316 udest
->af
= AF_INET
;
2320 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2322 struct net
*net
= sock_net(sk
);
2324 unsigned char arg
[MAX_SET_ARGLEN
];
2325 struct ip_vs_service_user
*usvc_compat
;
2326 struct ip_vs_service_user_kern usvc
;
2327 struct ip_vs_service
*svc
;
2328 struct ip_vs_dest_user
*udest_compat
;
2329 struct ip_vs_dest_user_kern udest
;
2330 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2332 BUILD_BUG_ON(sizeof(arg
) > 255);
2333 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2336 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2338 if (len
!= set_arglen
[CMDID(cmd
)]) {
2339 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2340 len
, set_arglen
[CMDID(cmd
)]);
2344 if (copy_from_user(arg
, user
, len
) != 0)
2347 /* increase the module use count */
2348 ip_vs_use_count_inc();
2350 /* Handle daemons since they have another lock */
2351 if (cmd
== IP_VS_SO_SET_STARTDAEMON
||
2352 cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2353 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2355 if (cmd
== IP_VS_SO_SET_STARTDAEMON
) {
2356 struct ipvs_sync_daemon_cfg cfg
;
2358 memset(&cfg
, 0, sizeof(cfg
));
2359 strlcpy(cfg
.mcast_ifn
, dm
->mcast_ifn
,
2360 sizeof(cfg
.mcast_ifn
));
2361 cfg
.syncid
= dm
->syncid
;
2363 mutex_lock(&ipvs
->sync_mutex
);
2364 ret
= start_sync_thread(net
, &cfg
, dm
->state
);
2365 mutex_unlock(&ipvs
->sync_mutex
);
2368 mutex_lock(&ipvs
->sync_mutex
);
2369 ret
= stop_sync_thread(net
, dm
->state
);
2370 mutex_unlock(&ipvs
->sync_mutex
);
2375 mutex_lock(&__ip_vs_mutex
);
2376 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2377 /* Flush the virtual service */
2378 ret
= ip_vs_flush(net
, false);
2380 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2381 /* Set timeout values for (tcp tcpfin udp) */
2382 ret
= ip_vs_set_timeout(net
, (struct ip_vs_timeout_user
*)arg
);
2386 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2387 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2389 /* We only use the new structs internally, so copy userspace compat
2390 * structs to extended internal versions */
2391 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2392 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2394 if (cmd
== IP_VS_SO_SET_ZERO
) {
2395 /* if no service address is set, zero counters in all */
2396 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2397 ret
= ip_vs_zero_all(net
);
2402 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2403 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2404 usvc
.protocol
!= IPPROTO_SCTP
) {
2405 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2406 usvc
.protocol
, &usvc
.addr
.ip
,
2407 ntohs(usvc
.port
), usvc
.sched_name
);
2412 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2414 if (usvc
.fwmark
== 0)
2415 svc
= __ip_vs_service_find(net
, usvc
.af
, usvc
.protocol
,
2416 &usvc
.addr
, usvc
.port
);
2418 svc
= __ip_vs_svc_fwm_find(net
, usvc
.af
, usvc
.fwmark
);
2421 if (cmd
!= IP_VS_SO_SET_ADD
2422 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2428 case IP_VS_SO_SET_ADD
:
2432 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
2434 case IP_VS_SO_SET_EDIT
:
2435 ret
= ip_vs_edit_service(svc
, &usvc
);
2437 case IP_VS_SO_SET_DEL
:
2438 ret
= ip_vs_del_service(svc
);
2442 case IP_VS_SO_SET_ZERO
:
2443 ret
= ip_vs_zero_service(svc
);
2445 case IP_VS_SO_SET_ADDDEST
:
2446 ret
= ip_vs_add_dest(svc
, &udest
);
2448 case IP_VS_SO_SET_EDITDEST
:
2449 ret
= ip_vs_edit_dest(svc
, &udest
);
2451 case IP_VS_SO_SET_DELDEST
:
2452 ret
= ip_vs_del_dest(svc
, &udest
);
2459 mutex_unlock(&__ip_vs_mutex
);
2461 /* decrease the module use count */
2462 ip_vs_use_count_dec();
2469 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2471 struct ip_vs_scheduler
*sched
;
2472 struct ip_vs_kstats kstats
;
2475 sched
= rcu_dereference_protected(src
->scheduler
, 1);
2476 sched_name
= sched
? sched
->name
: "none";
2477 dst
->protocol
= src
->protocol
;
2478 dst
->addr
= src
->addr
.ip
;
2479 dst
->port
= src
->port
;
2480 dst
->fwmark
= src
->fwmark
;
2481 strlcpy(dst
->sched_name
, sched_name
, sizeof(dst
->sched_name
));
2482 dst
->flags
= src
->flags
;
2483 dst
->timeout
= src
->timeout
/ HZ
;
2484 dst
->netmask
= src
->netmask
;
2485 dst
->num_dests
= src
->num_dests
;
2486 ip_vs_copy_stats(&kstats
, &src
->stats
);
2487 ip_vs_export_stats_user(&dst
->stats
, &kstats
);
2491 __ip_vs_get_service_entries(struct net
*net
,
2492 const struct ip_vs_get_services
*get
,
2493 struct ip_vs_get_services __user
*uptr
)
2495 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2497 struct ip_vs_service
*svc
;
2498 struct ip_vs_service_entry entry
;
2501 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2502 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2503 /* Only expose IPv4 entries to old interface */
2504 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2507 if (count
>= get
->num_services
)
2509 memset(&entry
, 0, sizeof(entry
));
2510 ip_vs_copy_service(&entry
, svc
);
2511 if (copy_to_user(&uptr
->entrytable
[count
],
2512 &entry
, sizeof(entry
))) {
2520 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2521 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
2522 /* Only expose IPv4 entries to old interface */
2523 if (svc
->af
!= AF_INET
|| (svc
->ipvs
!= ipvs
))
2526 if (count
>= get
->num_services
)
2528 memset(&entry
, 0, sizeof(entry
));
2529 ip_vs_copy_service(&entry
, svc
);
2530 if (copy_to_user(&uptr
->entrytable
[count
],
2531 &entry
, sizeof(entry
))) {
2543 __ip_vs_get_dest_entries(struct net
*net
, const struct ip_vs_get_dests
*get
,
2544 struct ip_vs_get_dests __user
*uptr
)
2546 struct ip_vs_service
*svc
;
2547 union nf_inet_addr addr
= { .ip
= get
->addr
};
2552 svc
= __ip_vs_svc_fwm_find(net
, AF_INET
, get
->fwmark
);
2554 svc
= __ip_vs_service_find(net
, AF_INET
, get
->protocol
, &addr
,
2560 struct ip_vs_dest
*dest
;
2561 struct ip_vs_dest_entry entry
;
2562 struct ip_vs_kstats kstats
;
2564 memset(&entry
, 0, sizeof(entry
));
2565 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2566 if (count
>= get
->num_dests
)
2569 /* Cannot expose heterogeneous members via sockopt
2572 if (dest
->af
!= svc
->af
)
2575 entry
.addr
= dest
->addr
.ip
;
2576 entry
.port
= dest
->port
;
2577 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2578 entry
.weight
= atomic_read(&dest
->weight
);
2579 entry
.u_threshold
= dest
->u_threshold
;
2580 entry
.l_threshold
= dest
->l_threshold
;
2581 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2582 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2583 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2584 ip_vs_copy_stats(&kstats
, &dest
->stats
);
2585 ip_vs_export_stats_user(&entry
.stats
, &kstats
);
2586 if (copy_to_user(&uptr
->entrytable
[count
],
2587 &entry
, sizeof(entry
))) {
2599 __ip_vs_get_timeouts(struct net
*net
, struct ip_vs_timeout_user
*u
)
2601 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2602 struct ip_vs_proto_data
*pd
;
2605 memset(u
, 0, sizeof (*u
));
2607 #ifdef CONFIG_IP_VS_PROTO_TCP
2608 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2609 u
->tcp_timeout
= pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2610 u
->tcp_fin_timeout
= pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2612 #ifdef CONFIG_IP_VS_PROTO_UDP
2613 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2615 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2619 static const unsigned char get_arglen
[CMDID(IP_VS_SO_GET_MAX
) + 1] = {
2620 [CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2621 [CMDID(IP_VS_SO_GET_INFO
)] = sizeof(struct ip_vs_getinfo
),
2622 [CMDID(IP_VS_SO_GET_SERVICES
)] = sizeof(struct ip_vs_get_services
),
2623 [CMDID(IP_VS_SO_GET_SERVICE
)] = sizeof(struct ip_vs_service_entry
),
2624 [CMDID(IP_VS_SO_GET_DESTS
)] = sizeof(struct ip_vs_get_dests
),
2625 [CMDID(IP_VS_SO_GET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2626 [CMDID(IP_VS_SO_GET_DAEMON
)] = 2 * sizeof(struct ip_vs_daemon_user
),
2629 union ip_vs_get_arglen
{
2630 char field_IP_VS_SO_GET_VERSION
[64];
2631 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO
;
2632 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES
;
2633 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE
;
2634 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS
;
2635 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT
;
2636 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON
[2];
2639 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2642 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2644 unsigned char arg
[MAX_GET_ARGLEN
];
2646 unsigned int copylen
;
2647 struct net
*net
= sock_net(sk
);
2648 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2651 BUILD_BUG_ON(sizeof(arg
) > 255);
2652 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2655 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2658 copylen
= get_arglen
[CMDID(cmd
)];
2659 if (*len
< (int) copylen
) {
2660 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len
, copylen
);
2664 if (copy_from_user(arg
, user
, copylen
) != 0)
2667 * Handle daemons first since it has its own locking
2669 if (cmd
== IP_VS_SO_GET_DAEMON
) {
2670 struct ip_vs_daemon_user d
[2];
2672 memset(&d
, 0, sizeof(d
));
2673 mutex_lock(&ipvs
->sync_mutex
);
2674 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
) {
2675 d
[0].state
= IP_VS_STATE_MASTER
;
2676 strlcpy(d
[0].mcast_ifn
, ipvs
->mcfg
.mcast_ifn
,
2677 sizeof(d
[0].mcast_ifn
));
2678 d
[0].syncid
= ipvs
->mcfg
.syncid
;
2680 if (ipvs
->sync_state
& IP_VS_STATE_BACKUP
) {
2681 d
[1].state
= IP_VS_STATE_BACKUP
;
2682 strlcpy(d
[1].mcast_ifn
, ipvs
->bcfg
.mcast_ifn
,
2683 sizeof(d
[1].mcast_ifn
));
2684 d
[1].syncid
= ipvs
->bcfg
.syncid
;
2686 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2688 mutex_unlock(&ipvs
->sync_mutex
);
2692 mutex_lock(&__ip_vs_mutex
);
2694 case IP_VS_SO_GET_VERSION
:
2698 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2699 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2700 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2704 *len
= strlen(buf
)+1;
2708 case IP_VS_SO_GET_INFO
:
2710 struct ip_vs_getinfo info
;
2711 info
.version
= IP_VS_VERSION_CODE
;
2712 info
.size
= ip_vs_conn_tab_size
;
2713 info
.num_services
= ipvs
->num_services
;
2714 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2719 case IP_VS_SO_GET_SERVICES
:
2721 struct ip_vs_get_services
*get
;
2724 get
= (struct ip_vs_get_services
*)arg
;
2725 size
= sizeof(*get
) +
2726 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2728 pr_err("length: %u != %u\n", *len
, size
);
2732 ret
= __ip_vs_get_service_entries(net
, get
, user
);
2736 case IP_VS_SO_GET_SERVICE
:
2738 struct ip_vs_service_entry
*entry
;
2739 struct ip_vs_service
*svc
;
2740 union nf_inet_addr addr
;
2742 entry
= (struct ip_vs_service_entry
*)arg
;
2743 addr
.ip
= entry
->addr
;
2746 svc
= __ip_vs_svc_fwm_find(net
, AF_INET
, entry
->fwmark
);
2748 svc
= __ip_vs_service_find(net
, AF_INET
,
2749 entry
->protocol
, &addr
,
2753 ip_vs_copy_service(entry
, svc
);
2754 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2761 case IP_VS_SO_GET_DESTS
:
2763 struct ip_vs_get_dests
*get
;
2766 get
= (struct ip_vs_get_dests
*)arg
;
2767 size
= sizeof(*get
) +
2768 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2770 pr_err("length: %u != %u\n", *len
, size
);
2774 ret
= __ip_vs_get_dest_entries(net
, get
, user
);
2778 case IP_VS_SO_GET_TIMEOUT
:
2780 struct ip_vs_timeout_user t
;
2782 __ip_vs_get_timeouts(net
, &t
);
2783 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2793 mutex_unlock(&__ip_vs_mutex
);
2798 static struct nf_sockopt_ops ip_vs_sockopts
= {
2800 .set_optmin
= IP_VS_BASE_CTL
,
2801 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2802 .set
= do_ip_vs_set_ctl
,
2803 .get_optmin
= IP_VS_BASE_CTL
,
2804 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2805 .get
= do_ip_vs_get_ctl
,
2806 .owner
= THIS_MODULE
,
2810 * Generic Netlink interface
2813 /* IPVS genetlink family */
2814 static struct genl_family ip_vs_genl_family
= {
2815 .id
= GENL_ID_GENERATE
,
2817 .name
= IPVS_GENL_NAME
,
2818 .version
= IPVS_GENL_VERSION
,
2819 .maxattr
= IPVS_CMD_MAX
,
2820 .netnsok
= true, /* Make ipvsadm to work on netns */
2823 /* Policy used for first-level command attributes */
2824 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2825 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2826 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2827 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2828 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2829 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2830 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2833 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2834 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2835 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2836 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2837 .len
= IP_VS_IFNAME_MAXLEN
},
2838 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2839 [IPVS_DAEMON_ATTR_SYNC_MAXLEN
] = { .type
= NLA_U16
},
2840 [IPVS_DAEMON_ATTR_MCAST_GROUP
] = { .type
= NLA_U32
},
2841 [IPVS_DAEMON_ATTR_MCAST_GROUP6
] = { .len
= sizeof(struct in6_addr
) },
2842 [IPVS_DAEMON_ATTR_MCAST_PORT
] = { .type
= NLA_U16
},
2843 [IPVS_DAEMON_ATTR_MCAST_TTL
] = { .type
= NLA_U8
},
2846 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2847 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2848 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2849 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2850 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2851 .len
= sizeof(union nf_inet_addr
) },
2852 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2853 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2854 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2855 .len
= IP_VS_SCHEDNAME_MAXLEN
},
2856 [IPVS_SVC_ATTR_PE_NAME
] = { .type
= NLA_NUL_STRING
,
2857 .len
= IP_VS_PENAME_MAXLEN
},
2858 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2859 .len
= sizeof(struct ip_vs_flags
) },
2860 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2861 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2862 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2865 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2866 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2867 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2868 .len
= sizeof(union nf_inet_addr
) },
2869 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2870 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2871 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2872 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2873 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2874 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2875 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2876 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2877 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2878 [IPVS_DEST_ATTR_ADDR_FAMILY
] = { .type
= NLA_U16
},
2881 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2882 struct ip_vs_kstats
*kstats
)
2884 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2889 if (nla_put_u32(skb
, IPVS_STATS_ATTR_CONNS
, (u32
)kstats
->conns
) ||
2890 nla_put_u32(skb
, IPVS_STATS_ATTR_INPKTS
, (u32
)kstats
->inpkts
) ||
2891 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPKTS
, (u32
)kstats
->outpkts
) ||
2892 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2893 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2894 nla_put_u32(skb
, IPVS_STATS_ATTR_CPS
, (u32
)kstats
->cps
) ||
2895 nla_put_u32(skb
, IPVS_STATS_ATTR_INPPS
, (u32
)kstats
->inpps
) ||
2896 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPPS
, (u32
)kstats
->outpps
) ||
2897 nla_put_u32(skb
, IPVS_STATS_ATTR_INBPS
, (u32
)kstats
->inbps
) ||
2898 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTBPS
, (u32
)kstats
->outbps
))
2899 goto nla_put_failure
;
2900 nla_nest_end(skb
, nl_stats
);
2905 nla_nest_cancel(skb
, nl_stats
);
2909 static int ip_vs_genl_fill_stats64(struct sk_buff
*skb
, int container_type
,
2910 struct ip_vs_kstats
*kstats
)
2912 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2917 if (nla_put_u64(skb
, IPVS_STATS_ATTR_CONNS
, kstats
->conns
) ||
2918 nla_put_u64(skb
, IPVS_STATS_ATTR_INPKTS
, kstats
->inpkts
) ||
2919 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPKTS
, kstats
->outpkts
) ||
2920 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2921 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2922 nla_put_u64(skb
, IPVS_STATS_ATTR_CPS
, kstats
->cps
) ||
2923 nla_put_u64(skb
, IPVS_STATS_ATTR_INPPS
, kstats
->inpps
) ||
2924 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPPS
, kstats
->outpps
) ||
2925 nla_put_u64(skb
, IPVS_STATS_ATTR_INBPS
, kstats
->inbps
) ||
2926 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBPS
, kstats
->outbps
))
2927 goto nla_put_failure
;
2928 nla_nest_end(skb
, nl_stats
);
2933 nla_nest_cancel(skb
, nl_stats
);
2937 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2938 struct ip_vs_service
*svc
)
2940 struct ip_vs_scheduler
*sched
;
2941 struct ip_vs_pe
*pe
;
2942 struct nlattr
*nl_service
;
2943 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2945 struct ip_vs_kstats kstats
;
2948 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
2952 if (nla_put_u16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
))
2953 goto nla_put_failure
;
2955 if (nla_put_u32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
))
2956 goto nla_put_failure
;
2958 if (nla_put_u16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
) ||
2959 nla_put(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
) ||
2960 nla_put_be16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
))
2961 goto nla_put_failure
;
2964 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
2965 sched_name
= sched
? sched
->name
: "none";
2966 pe
= rcu_dereference_protected(svc
->pe
, 1);
2967 if (nla_put_string(skb
, IPVS_SVC_ATTR_SCHED_NAME
, sched_name
) ||
2968 (pe
&& nla_put_string(skb
, IPVS_SVC_ATTR_PE_NAME
, pe
->name
)) ||
2969 nla_put(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
) ||
2970 nla_put_u32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
) ||
2971 nla_put_be32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
))
2972 goto nla_put_failure
;
2973 ip_vs_copy_stats(&kstats
, &svc
->stats
);
2974 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &kstats
))
2975 goto nla_put_failure
;
2976 if (ip_vs_genl_fill_stats64(skb
, IPVS_SVC_ATTR_STATS64
, &kstats
))
2977 goto nla_put_failure
;
2979 nla_nest_end(skb
, nl_service
);
2984 nla_nest_cancel(skb
, nl_service
);
2988 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
2989 struct ip_vs_service
*svc
,
2990 struct netlink_callback
*cb
)
2994 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
2995 &ip_vs_genl_family
, NLM_F_MULTI
,
2996 IPVS_CMD_NEW_SERVICE
);
3000 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
3001 goto nla_put_failure
;
3003 genlmsg_end(skb
, hdr
);
3007 genlmsg_cancel(skb
, hdr
);
3011 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
3012 struct netlink_callback
*cb
)
3015 int start
= cb
->args
[0];
3016 struct ip_vs_service
*svc
;
3017 struct netns_ipvs
*ipvs
= net_ipvs(skb_sknet(skb
));
3019 mutex_lock(&__ip_vs_mutex
);
3020 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3021 hlist_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
3022 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3024 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3026 goto nla_put_failure
;
3031 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
3032 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
3033 if (++idx
<= start
|| (svc
->ipvs
!= ipvs
))
3035 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
3037 goto nla_put_failure
;
3043 mutex_unlock(&__ip_vs_mutex
);
3049 static int ip_vs_genl_parse_service(struct net
*net
,
3050 struct ip_vs_service_user_kern
*usvc
,
3051 struct nlattr
*nla
, int full_entry
,
3052 struct ip_vs_service
**ret_svc
)
3054 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
3055 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
3056 struct ip_vs_service
*svc
;
3058 /* Parse mandatory identifying service fields first */
3060 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
, ip_vs_svc_policy
))
3063 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
3064 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
3065 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
3066 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
3067 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
3069 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
3072 memset(usvc
, 0, sizeof(*usvc
));
3074 usvc
->af
= nla_get_u16(nla_af
);
3075 #ifdef CONFIG_IP_VS_IPV6
3076 if (usvc
->af
!= AF_INET
&& usvc
->af
!= AF_INET6
)
3078 if (usvc
->af
!= AF_INET
)
3080 return -EAFNOSUPPORT
;
3083 usvc
->protocol
= IPPROTO_TCP
;
3084 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
3086 usvc
->protocol
= nla_get_u16(nla_protocol
);
3087 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
3088 usvc
->port
= nla_get_be16(nla_port
);
3094 svc
= __ip_vs_svc_fwm_find(net
, usvc
->af
, usvc
->fwmark
);
3096 svc
= __ip_vs_service_find(net
, usvc
->af
, usvc
->protocol
,
3097 &usvc
->addr
, usvc
->port
);
3101 /* If a full entry was requested, check for the additional fields */
3103 struct nlattr
*nla_sched
, *nla_flags
, *nla_pe
, *nla_timeout
,
3105 struct ip_vs_flags flags
;
3107 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
3108 nla_pe
= attrs
[IPVS_SVC_ATTR_PE_NAME
];
3109 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
3110 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
3111 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
3113 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
3116 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
3118 /* prefill flags from service if it already exists */
3120 usvc
->flags
= svc
->flags
;
3122 /* set new flags from userland */
3123 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
3124 (flags
.flags
& flags
.mask
);
3125 usvc
->sched_name
= nla_data(nla_sched
);
3126 usvc
->pe_name
= nla_pe
? nla_data(nla_pe
) : NULL
;
3127 usvc
->timeout
= nla_get_u32(nla_timeout
);
3128 usvc
->netmask
= nla_get_be32(nla_netmask
);
3134 static struct ip_vs_service
*ip_vs_genl_find_service(struct net
*net
,
3137 struct ip_vs_service_user_kern usvc
;
3138 struct ip_vs_service
*svc
;
3141 ret
= ip_vs_genl_parse_service(net
, &usvc
, nla
, 0, &svc
);
3142 return ret
? ERR_PTR(ret
) : svc
;
3145 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
3147 struct nlattr
*nl_dest
;
3148 struct ip_vs_kstats kstats
;
3150 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
3154 if (nla_put(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
) ||
3155 nla_put_be16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
) ||
3156 nla_put_u32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
3157 (atomic_read(&dest
->conn_flags
) &
3158 IP_VS_CONN_F_FWD_MASK
)) ||
3159 nla_put_u32(skb
, IPVS_DEST_ATTR_WEIGHT
,
3160 atomic_read(&dest
->weight
)) ||
3161 nla_put_u32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
) ||
3162 nla_put_u32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
) ||
3163 nla_put_u32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
3164 atomic_read(&dest
->activeconns
)) ||
3165 nla_put_u32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
3166 atomic_read(&dest
->inactconns
)) ||
3167 nla_put_u32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
3168 atomic_read(&dest
->persistconns
)) ||
3169 nla_put_u16(skb
, IPVS_DEST_ATTR_ADDR_FAMILY
, dest
->af
))
3170 goto nla_put_failure
;
3171 ip_vs_copy_stats(&kstats
, &dest
->stats
);
3172 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &kstats
))
3173 goto nla_put_failure
;
3174 if (ip_vs_genl_fill_stats64(skb
, IPVS_DEST_ATTR_STATS64
, &kstats
))
3175 goto nla_put_failure
;
3177 nla_nest_end(skb
, nl_dest
);
3182 nla_nest_cancel(skb
, nl_dest
);
3186 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
3187 struct netlink_callback
*cb
)
3191 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3192 &ip_vs_genl_family
, NLM_F_MULTI
,
3197 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
3198 goto nla_put_failure
;
3200 genlmsg_end(skb
, hdr
);
3204 genlmsg_cancel(skb
, hdr
);
3208 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
3209 struct netlink_callback
*cb
)
3212 int start
= cb
->args
[0];
3213 struct ip_vs_service
*svc
;
3214 struct ip_vs_dest
*dest
;
3215 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
3216 struct net
*net
= skb_sknet(skb
);
3218 mutex_lock(&__ip_vs_mutex
);
3220 /* Try to find the service for which to dump destinations */
3221 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
,
3222 IPVS_CMD_ATTR_MAX
, ip_vs_cmd_policy
))
3226 svc
= ip_vs_genl_find_service(net
, attrs
[IPVS_CMD_ATTR_SERVICE
]);
3227 if (IS_ERR(svc
) || svc
== NULL
)
3230 /* Dump the destinations */
3231 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
3234 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
3236 goto nla_put_failure
;
3244 mutex_unlock(&__ip_vs_mutex
);
3249 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
3250 struct nlattr
*nla
, int full_entry
)
3252 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
3253 struct nlattr
*nla_addr
, *nla_port
;
3254 struct nlattr
*nla_addr_family
;
3256 /* Parse mandatory identifying destination fields first */
3258 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
, ip_vs_dest_policy
))
3261 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
3262 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
3263 nla_addr_family
= attrs
[IPVS_DEST_ATTR_ADDR_FAMILY
];
3265 if (!(nla_addr
&& nla_port
))
3268 memset(udest
, 0, sizeof(*udest
));
3270 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
3271 udest
->port
= nla_get_be16(nla_port
);
3273 if (nla_addr_family
)
3274 udest
->af
= nla_get_u16(nla_addr_family
);
3278 /* If a full entry was requested, check for the additional fields */
3280 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
3283 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
3284 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
3285 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
3286 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
3288 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
3291 udest
->conn_flags
= nla_get_u32(nla_fwd
)
3292 & IP_VS_CONN_F_FWD_MASK
;
3293 udest
->weight
= nla_get_u32(nla_weight
);
3294 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
3295 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
3301 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __u32 state
,
3302 struct ipvs_sync_daemon_cfg
*c
)
3304 struct nlattr
*nl_daemon
;
3306 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
3310 if (nla_put_u32(skb
, IPVS_DAEMON_ATTR_STATE
, state
) ||
3311 nla_put_string(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, c
->mcast_ifn
) ||
3312 nla_put_u32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, c
->syncid
) ||
3313 nla_put_u16(skb
, IPVS_DAEMON_ATTR_SYNC_MAXLEN
, c
->sync_maxlen
) ||
3314 nla_put_u16(skb
, IPVS_DAEMON_ATTR_MCAST_PORT
, c
->mcast_port
) ||
3315 nla_put_u8(skb
, IPVS_DAEMON_ATTR_MCAST_TTL
, c
->mcast_ttl
))
3316 goto nla_put_failure
;
3317 #ifdef CONFIG_IP_VS_IPV6
3318 if (c
->mcast_af
== AF_INET6
) {
3319 if (nla_put_in6_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP6
,
3320 &c
->mcast_group
.in6
))
3321 goto nla_put_failure
;
3324 if (c
->mcast_af
== AF_INET
&&
3325 nla_put_in_addr(skb
, IPVS_DAEMON_ATTR_MCAST_GROUP
,
3327 goto nla_put_failure
;
3328 nla_nest_end(skb
, nl_daemon
);
3333 nla_nest_cancel(skb
, nl_daemon
);
3337 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __u32 state
,
3338 struct ipvs_sync_daemon_cfg
*c
,
3339 struct netlink_callback
*cb
)
3342 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3343 &ip_vs_genl_family
, NLM_F_MULTI
,
3344 IPVS_CMD_NEW_DAEMON
);
3348 if (ip_vs_genl_fill_daemon(skb
, state
, c
))
3349 goto nla_put_failure
;
3351 genlmsg_end(skb
, hdr
);
3355 genlmsg_cancel(skb
, hdr
);
3359 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
3360 struct netlink_callback
*cb
)
3362 struct net
*net
= skb_sknet(skb
);
3363 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3365 mutex_lock(&ipvs
->sync_mutex
);
3366 if ((ipvs
->sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
3367 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
3368 &ipvs
->mcfg
, cb
) < 0)
3369 goto nla_put_failure
;
3374 if ((ipvs
->sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
3375 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
3376 &ipvs
->bcfg
, cb
) < 0)
3377 goto nla_put_failure
;
3383 mutex_unlock(&ipvs
->sync_mutex
);
3388 static int ip_vs_genl_new_daemon(struct net
*net
, struct nlattr
**attrs
)
3390 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3391 struct ipvs_sync_daemon_cfg c
;
3395 memset(&c
, 0, sizeof(c
));
3396 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
3397 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
3398 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3400 strlcpy(c
.mcast_ifn
, nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3401 sizeof(c
.mcast_ifn
));
3402 c
.syncid
= nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]);
3404 a
= attrs
[IPVS_DAEMON_ATTR_SYNC_MAXLEN
];
3406 c
.sync_maxlen
= nla_get_u16(a
);
3408 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP
];
3410 c
.mcast_af
= AF_INET
;
3411 c
.mcast_group
.ip
= nla_get_in_addr(a
);
3412 if (!ipv4_is_multicast(c
.mcast_group
.ip
))
3415 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_GROUP6
];
3417 #ifdef CONFIG_IP_VS_IPV6
3420 c
.mcast_af
= AF_INET6
;
3421 c
.mcast_group
.in6
= nla_get_in6_addr(a
);
3422 addr_type
= ipv6_addr_type(&c
.mcast_group
.in6
);
3423 if (!(addr_type
& IPV6_ADDR_MULTICAST
))
3426 return -EAFNOSUPPORT
;
3431 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_PORT
];
3433 c
.mcast_port
= nla_get_u16(a
);
3435 a
= attrs
[IPVS_DAEMON_ATTR_MCAST_TTL
];
3437 c
.mcast_ttl
= nla_get_u8(a
);
3439 /* The synchronization protocol is incompatible with mixed family
3442 if (ipvs
->mixed_address_family_dests
> 0)
3446 mutex_lock(&ipvs
->sync_mutex
);
3447 ret
= start_sync_thread(net
, &c
,
3448 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3449 mutex_unlock(&ipvs
->sync_mutex
);
3454 static int ip_vs_genl_del_daemon(struct net
*net
, struct nlattr
**attrs
)
3456 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3459 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3462 mutex_lock(&ipvs
->sync_mutex
);
3463 ret
= stop_sync_thread(net
,
3464 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3465 mutex_unlock(&ipvs
->sync_mutex
);
3469 static int ip_vs_genl_set_config(struct net
*net
, struct nlattr
**attrs
)
3471 struct ip_vs_timeout_user t
;
3473 __ip_vs_get_timeouts(net
, &t
);
3475 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3476 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3478 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3480 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3482 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3483 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3485 return ip_vs_set_timeout(net
, &t
);
3488 static int ip_vs_genl_set_daemon(struct sk_buff
*skb
, struct genl_info
*info
)
3490 int ret
= -EINVAL
, cmd
;
3492 struct netns_ipvs
*ipvs
;
3494 net
= skb_sknet(skb
);
3495 ipvs
= net_ipvs(net
);
3496 cmd
= info
->genlhdr
->cmd
;
3498 if (cmd
== IPVS_CMD_NEW_DAEMON
|| cmd
== IPVS_CMD_DEL_DAEMON
) {
3499 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3501 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3502 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3503 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3504 ip_vs_daemon_policy
))
3507 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3508 ret
= ip_vs_genl_new_daemon(net
, daemon_attrs
);
3510 ret
= ip_vs_genl_del_daemon(net
, daemon_attrs
);
3517 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3519 struct ip_vs_service
*svc
= NULL
;
3520 struct ip_vs_service_user_kern usvc
;
3521 struct ip_vs_dest_user_kern udest
;
3523 int need_full_svc
= 0, need_full_dest
= 0;
3526 net
= skb_sknet(skb
);
3527 cmd
= info
->genlhdr
->cmd
;
3529 mutex_lock(&__ip_vs_mutex
);
3531 if (cmd
== IPVS_CMD_FLUSH
) {
3532 ret
= ip_vs_flush(net
, false);
3534 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3535 ret
= ip_vs_genl_set_config(net
, info
->attrs
);
3537 } else if (cmd
== IPVS_CMD_ZERO
&&
3538 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3539 ret
= ip_vs_zero_all(net
);
3543 /* All following commands require a service argument, so check if we
3544 * received a valid one. We need a full service specification when
3545 * adding / editing a service. Only identifying members otherwise. */
3546 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3549 ret
= ip_vs_genl_parse_service(net
, &usvc
,
3550 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3551 need_full_svc
, &svc
);
3555 /* Unless we're adding a new service, the service must already exist */
3556 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3561 /* Destination commands require a valid destination argument. For
3562 * adding / editing a destination, we need a full destination
3564 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3565 cmd
== IPVS_CMD_DEL_DEST
) {
3566 if (cmd
!= IPVS_CMD_DEL_DEST
)
3569 ret
= ip_vs_genl_parse_dest(&udest
,
3570 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3575 /* Old protocols did not allow the user to specify address
3576 * family, so we set it to zero instead. We also didn't
3577 * allow heterogeneous pools in the old code, so it's safe
3578 * to assume that this will have the same address family as
3584 if (udest
.af
!= svc
->af
&& cmd
!= IPVS_CMD_DEL_DEST
) {
3585 /* The synchronization protocol is incompatible
3586 * with mixed family services
3588 if (net_ipvs(net
)->sync_state
) {
3593 /* Which connection types do we support? */
3594 switch (udest
.conn_flags
) {
3595 case IP_VS_CONN_F_TUNNEL
:
3596 /* We are able to forward this */
3606 case IPVS_CMD_NEW_SERVICE
:
3608 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
3612 case IPVS_CMD_SET_SERVICE
:
3613 ret
= ip_vs_edit_service(svc
, &usvc
);
3615 case IPVS_CMD_DEL_SERVICE
:
3616 ret
= ip_vs_del_service(svc
);
3617 /* do not use svc, it can be freed */
3619 case IPVS_CMD_NEW_DEST
:
3620 ret
= ip_vs_add_dest(svc
, &udest
);
3622 case IPVS_CMD_SET_DEST
:
3623 ret
= ip_vs_edit_dest(svc
, &udest
);
3625 case IPVS_CMD_DEL_DEST
:
3626 ret
= ip_vs_del_dest(svc
, &udest
);
3629 ret
= ip_vs_zero_service(svc
);
3636 mutex_unlock(&__ip_vs_mutex
);
3641 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3643 struct sk_buff
*msg
;
3645 int ret
, cmd
, reply_cmd
;
3648 net
= skb_sknet(skb
);
3649 cmd
= info
->genlhdr
->cmd
;
3651 if (cmd
== IPVS_CMD_GET_SERVICE
)
3652 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3653 else if (cmd
== IPVS_CMD_GET_INFO
)
3654 reply_cmd
= IPVS_CMD_SET_INFO
;
3655 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3656 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3658 pr_err("unknown Generic Netlink command\n");
3662 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3666 mutex_lock(&__ip_vs_mutex
);
3668 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3670 goto nla_put_failure
;
3673 case IPVS_CMD_GET_SERVICE
:
3675 struct ip_vs_service
*svc
;
3677 svc
= ip_vs_genl_find_service(net
,
3678 info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3683 ret
= ip_vs_genl_fill_service(msg
, svc
);
3685 goto nla_put_failure
;
3694 case IPVS_CMD_GET_CONFIG
:
3696 struct ip_vs_timeout_user t
;
3698 __ip_vs_get_timeouts(net
, &t
);
3699 #ifdef CONFIG_IP_VS_PROTO_TCP
3700 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
,
3702 nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3704 goto nla_put_failure
;
3706 #ifdef CONFIG_IP_VS_PROTO_UDP
3707 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
))
3708 goto nla_put_failure
;
3714 case IPVS_CMD_GET_INFO
:
3715 if (nla_put_u32(msg
, IPVS_INFO_ATTR_VERSION
,
3716 IP_VS_VERSION_CODE
) ||
3717 nla_put_u32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3718 ip_vs_conn_tab_size
))
3719 goto nla_put_failure
;
3723 genlmsg_end(msg
, reply
);
3724 ret
= genlmsg_reply(msg
, info
);
3728 pr_err("not enough space in Netlink message\n");
3734 mutex_unlock(&__ip_vs_mutex
);
3740 static const struct genl_ops ip_vs_genl_ops
[] = {
3742 .cmd
= IPVS_CMD_NEW_SERVICE
,
3743 .flags
= GENL_ADMIN_PERM
,
3744 .policy
= ip_vs_cmd_policy
,
3745 .doit
= ip_vs_genl_set_cmd
,
3748 .cmd
= IPVS_CMD_SET_SERVICE
,
3749 .flags
= GENL_ADMIN_PERM
,
3750 .policy
= ip_vs_cmd_policy
,
3751 .doit
= ip_vs_genl_set_cmd
,
3754 .cmd
= IPVS_CMD_DEL_SERVICE
,
3755 .flags
= GENL_ADMIN_PERM
,
3756 .policy
= ip_vs_cmd_policy
,
3757 .doit
= ip_vs_genl_set_cmd
,
3760 .cmd
= IPVS_CMD_GET_SERVICE
,
3761 .flags
= GENL_ADMIN_PERM
,
3762 .doit
= ip_vs_genl_get_cmd
,
3763 .dumpit
= ip_vs_genl_dump_services
,
3764 .policy
= ip_vs_cmd_policy
,
3767 .cmd
= IPVS_CMD_NEW_DEST
,
3768 .flags
= GENL_ADMIN_PERM
,
3769 .policy
= ip_vs_cmd_policy
,
3770 .doit
= ip_vs_genl_set_cmd
,
3773 .cmd
= IPVS_CMD_SET_DEST
,
3774 .flags
= GENL_ADMIN_PERM
,
3775 .policy
= ip_vs_cmd_policy
,
3776 .doit
= ip_vs_genl_set_cmd
,
3779 .cmd
= IPVS_CMD_DEL_DEST
,
3780 .flags
= GENL_ADMIN_PERM
,
3781 .policy
= ip_vs_cmd_policy
,
3782 .doit
= ip_vs_genl_set_cmd
,
3785 .cmd
= IPVS_CMD_GET_DEST
,
3786 .flags
= GENL_ADMIN_PERM
,
3787 .policy
= ip_vs_cmd_policy
,
3788 .dumpit
= ip_vs_genl_dump_dests
,
3791 .cmd
= IPVS_CMD_NEW_DAEMON
,
3792 .flags
= GENL_ADMIN_PERM
,
3793 .policy
= ip_vs_cmd_policy
,
3794 .doit
= ip_vs_genl_set_daemon
,
3797 .cmd
= IPVS_CMD_DEL_DAEMON
,
3798 .flags
= GENL_ADMIN_PERM
,
3799 .policy
= ip_vs_cmd_policy
,
3800 .doit
= ip_vs_genl_set_daemon
,
3803 .cmd
= IPVS_CMD_GET_DAEMON
,
3804 .flags
= GENL_ADMIN_PERM
,
3805 .dumpit
= ip_vs_genl_dump_daemons
,
3808 .cmd
= IPVS_CMD_SET_CONFIG
,
3809 .flags
= GENL_ADMIN_PERM
,
3810 .policy
= ip_vs_cmd_policy
,
3811 .doit
= ip_vs_genl_set_cmd
,
3814 .cmd
= IPVS_CMD_GET_CONFIG
,
3815 .flags
= GENL_ADMIN_PERM
,
3816 .doit
= ip_vs_genl_get_cmd
,
3819 .cmd
= IPVS_CMD_GET_INFO
,
3820 .flags
= GENL_ADMIN_PERM
,
3821 .doit
= ip_vs_genl_get_cmd
,
3824 .cmd
= IPVS_CMD_ZERO
,
3825 .flags
= GENL_ADMIN_PERM
,
3826 .policy
= ip_vs_cmd_policy
,
3827 .doit
= ip_vs_genl_set_cmd
,
3830 .cmd
= IPVS_CMD_FLUSH
,
3831 .flags
= GENL_ADMIN_PERM
,
3832 .doit
= ip_vs_genl_set_cmd
,
3836 static int __init
ip_vs_genl_register(void)
3838 return genl_register_family_with_ops(&ip_vs_genl_family
,
3842 static void ip_vs_genl_unregister(void)
3844 genl_unregister_family(&ip_vs_genl_family
);
3847 /* End of Generic Netlink interface definitions */
3850 * per netns intit/exit func.
3852 #ifdef CONFIG_SYSCTL
3853 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
)
3856 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3857 struct ctl_table
*tbl
;
3859 atomic_set(&ipvs
->dropentry
, 0);
3860 spin_lock_init(&ipvs
->dropentry_lock
);
3861 spin_lock_init(&ipvs
->droppacket_lock
);
3862 spin_lock_init(&ipvs
->securetcp_lock
);
3864 if (!net_eq(net
, &init_net
)) {
3865 tbl
= kmemdup(vs_vars
, sizeof(vs_vars
), GFP_KERNEL
);
3869 /* Don't export sysctls to unprivileged users */
3870 if (net
->user_ns
!= &init_user_ns
)
3871 tbl
[0].procname
= NULL
;
3874 /* Initialize sysctl defaults */
3875 for (idx
= 0; idx
< ARRAY_SIZE(vs_vars
); idx
++) {
3876 if (tbl
[idx
].proc_handler
== proc_do_defense_mode
)
3877 tbl
[idx
].extra2
= ipvs
;
3880 ipvs
->sysctl_amemthresh
= 1024;
3881 tbl
[idx
++].data
= &ipvs
->sysctl_amemthresh
;
3882 ipvs
->sysctl_am_droprate
= 10;
3883 tbl
[idx
++].data
= &ipvs
->sysctl_am_droprate
;
3884 tbl
[idx
++].data
= &ipvs
->sysctl_drop_entry
;
3885 tbl
[idx
++].data
= &ipvs
->sysctl_drop_packet
;
3886 #ifdef CONFIG_IP_VS_NFCT
3887 tbl
[idx
++].data
= &ipvs
->sysctl_conntrack
;
3889 tbl
[idx
++].data
= &ipvs
->sysctl_secure_tcp
;
3890 ipvs
->sysctl_snat_reroute
= 1;
3891 tbl
[idx
++].data
= &ipvs
->sysctl_snat_reroute
;
3892 ipvs
->sysctl_sync_ver
= 1;
3893 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ver
;
3894 ipvs
->sysctl_sync_ports
= 1;
3895 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ports
;
3896 tbl
[idx
++].data
= &ipvs
->sysctl_sync_persist_mode
;
3897 ipvs
->sysctl_sync_qlen_max
= nr_free_buffer_pages() / 32;
3898 tbl
[idx
++].data
= &ipvs
->sysctl_sync_qlen_max
;
3899 ipvs
->sysctl_sync_sock_size
= 0;
3900 tbl
[idx
++].data
= &ipvs
->sysctl_sync_sock_size
;
3901 tbl
[idx
++].data
= &ipvs
->sysctl_cache_bypass
;
3902 tbl
[idx
++].data
= &ipvs
->sysctl_expire_nodest_conn
;
3903 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_tcp
;
3904 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_sctp
;
3905 tbl
[idx
++].data
= &ipvs
->sysctl_expire_quiescent_template
;
3906 ipvs
->sysctl_sync_threshold
[0] = DEFAULT_SYNC_THRESHOLD
;
3907 ipvs
->sysctl_sync_threshold
[1] = DEFAULT_SYNC_PERIOD
;
3908 tbl
[idx
].data
= &ipvs
->sysctl_sync_threshold
;
3909 tbl
[idx
++].maxlen
= sizeof(ipvs
->sysctl_sync_threshold
);
3910 ipvs
->sysctl_sync_refresh_period
= DEFAULT_SYNC_REFRESH_PERIOD
;
3911 tbl
[idx
++].data
= &ipvs
->sysctl_sync_refresh_period
;
3912 ipvs
->sysctl_sync_retries
= clamp_t(int, DEFAULT_SYNC_RETRIES
, 0, 3);
3913 tbl
[idx
++].data
= &ipvs
->sysctl_sync_retries
;
3914 tbl
[idx
++].data
= &ipvs
->sysctl_nat_icmp_send
;
3915 ipvs
->sysctl_pmtu_disc
= 1;
3916 tbl
[idx
++].data
= &ipvs
->sysctl_pmtu_disc
;
3917 tbl
[idx
++].data
= &ipvs
->sysctl_backup_only
;
3918 ipvs
->sysctl_conn_reuse_mode
= 1;
3919 tbl
[idx
++].data
= &ipvs
->sysctl_conn_reuse_mode
;
3920 tbl
[idx
++].data
= &ipvs
->sysctl_schedule_icmp
;
3921 tbl
[idx
++].data
= &ipvs
->sysctl_ignore_tunneled
;
3923 ipvs
->sysctl_hdr
= register_net_sysctl(net
, "net/ipv4/vs", tbl
);
3924 if (ipvs
->sysctl_hdr
== NULL
) {
3925 if (!net_eq(net
, &init_net
))
3929 ip_vs_start_estimator(net
, &ipvs
->tot_stats
);
3930 ipvs
->sysctl_tbl
= tbl
;
3931 /* Schedule defense work */
3932 INIT_DELAYED_WORK(&ipvs
->defense_work
, defense_work_handler
);
3933 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
3938 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
)
3940 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3942 cancel_delayed_work_sync(&ipvs
->defense_work
);
3943 cancel_work_sync(&ipvs
->defense_work
.work
);
3944 unregister_net_sysctl_table(ipvs
->sysctl_hdr
);
3945 ip_vs_stop_estimator(net
, &ipvs
->tot_stats
);
3947 if (!net_eq(net
, &init_net
))
3948 kfree(ipvs
->sysctl_tbl
);
3953 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
) { return 0; }
3954 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
) { }
3958 static struct notifier_block ip_vs_dst_notifier
= {
3959 .notifier_call
= ip_vs_dst_event
,
3962 int __net_init
ip_vs_control_net_init(struct net
*net
)
3965 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3967 /* Initialize rs_table */
3968 for (idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++)
3969 INIT_HLIST_HEAD(&ipvs
->rs_table
[idx
]);
3971 INIT_LIST_HEAD(&ipvs
->dest_trash
);
3972 spin_lock_init(&ipvs
->dest_trash_lock
);
3973 setup_timer(&ipvs
->dest_trash_timer
, ip_vs_dest_trash_expire
,
3974 (unsigned long) net
);
3975 atomic_set(&ipvs
->ftpsvc_counter
, 0);
3976 atomic_set(&ipvs
->nullsvc_counter
, 0);
3979 ipvs
->tot_stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
3980 if (!ipvs
->tot_stats
.cpustats
)
3983 for_each_possible_cpu(i
) {
3984 struct ip_vs_cpu_stats
*ipvs_tot_stats
;
3985 ipvs_tot_stats
= per_cpu_ptr(ipvs
->tot_stats
.cpustats
, i
);
3986 u64_stats_init(&ipvs_tot_stats
->syncp
);
3989 spin_lock_init(&ipvs
->tot_stats
.lock
);
3991 proc_create("ip_vs", 0, net
->proc_net
, &ip_vs_info_fops
);
3992 proc_create("ip_vs_stats", 0, net
->proc_net
, &ip_vs_stats_fops
);
3993 proc_create("ip_vs_stats_percpu", 0, net
->proc_net
,
3994 &ip_vs_stats_percpu_fops
);
3996 if (ip_vs_control_net_init_sysctl(net
))
4002 free_percpu(ipvs
->tot_stats
.cpustats
);
4006 void __net_exit
ip_vs_control_net_cleanup(struct net
*net
)
4008 struct netns_ipvs
*ipvs
= net_ipvs(net
);
4010 ip_vs_trash_cleanup(net
);
4011 ip_vs_control_net_cleanup_sysctl(net
);
4012 remove_proc_entry("ip_vs_stats_percpu", net
->proc_net
);
4013 remove_proc_entry("ip_vs_stats", net
->proc_net
);
4014 remove_proc_entry("ip_vs", net
->proc_net
);
4015 free_percpu(ipvs
->tot_stats
.cpustats
);
4018 int __init
ip_vs_register_nl_ioctl(void)
4022 ret
= nf_register_sockopt(&ip_vs_sockopts
);
4024 pr_err("cannot register sockopt.\n");
4028 ret
= ip_vs_genl_register();
4030 pr_err("cannot register Generic Netlink interface.\n");
4036 nf_unregister_sockopt(&ip_vs_sockopts
);
4041 void ip_vs_unregister_nl_ioctl(void)
4043 ip_vs_genl_unregister();
4044 nf_unregister_sockopt(&ip_vs_sockopts
);
4047 int __init
ip_vs_control_init(void)
4054 /* Initialize svc_table, ip_vs_svc_fwm_table */
4055 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
4056 INIT_HLIST_HEAD(&ip_vs_svc_table
[idx
]);
4057 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
4060 smp_wmb(); /* Do we really need it now ? */
4062 ret
= register_netdevice_notifier(&ip_vs_dst_notifier
);
4071 void ip_vs_control_cleanup(void)
4074 unregister_netdevice_notifier(&ip_vs_dst_notifier
);