2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/capability.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/workqueue.h>
32 #include <linux/swap.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
36 #include <linux/netfilter.h>
37 #include <linux/netfilter_ipv4.h>
38 #include <linux/mutex.h>
40 #include <net/net_namespace.h>
41 #include <linux/nsproxy.h>
43 #ifdef CONFIG_IP_VS_IPV6
45 #include <net/ip6_route.h>
47 #include <net/route.h>
49 #include <net/genetlink.h>
51 #include <asm/uaccess.h>
53 #include <net/ip_vs.h>
55 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56 static DEFINE_MUTEX(__ip_vs_mutex
);
58 /* sysctl variables */
60 #ifdef CONFIG_IP_VS_DEBUG
61 static int sysctl_ip_vs_debug_level
= 0;
63 int ip_vs_get_debug_level(void)
65 return sysctl_ip_vs_debug_level
;
71 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
);
74 #ifdef CONFIG_IP_VS_IPV6
75 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
76 static bool __ip_vs_addr_is_local_v6(struct net
*net
,
77 const struct in6_addr
*addr
)
82 struct dst_entry
*dst
= ip6_route_output(net
, NULL
, &fl6
);
85 is_local
= !dst
->error
&& dst
->dev
&& (dst
->dev
->flags
& IFF_LOOPBACK
);
94 * update_defense_level is called from keventd and from sysctl,
95 * so it needs to protect itself from softirqs
97 static void update_defense_level(struct netns_ipvs
*ipvs
)
100 static int old_secure_tcp
= 0;
105 /* we only count free and buffered memory (in pages) */
107 availmem
= i
.freeram
+ i
.bufferram
;
108 /* however in linux 2.5 the i.bufferram is total page cache size,
110 /* si_swapinfo(&i); */
111 /* availmem = availmem - (i.totalswap - i.freeswap); */
113 nomem
= (availmem
< ipvs
->sysctl_amemthresh
);
118 spin_lock(&ipvs
->dropentry_lock
);
119 switch (ipvs
->sysctl_drop_entry
) {
121 atomic_set(&ipvs
->dropentry
, 0);
125 atomic_set(&ipvs
->dropentry
, 1);
126 ipvs
->sysctl_drop_entry
= 2;
128 atomic_set(&ipvs
->dropentry
, 0);
133 atomic_set(&ipvs
->dropentry
, 1);
135 atomic_set(&ipvs
->dropentry
, 0);
136 ipvs
->sysctl_drop_entry
= 1;
140 atomic_set(&ipvs
->dropentry
, 1);
143 spin_unlock(&ipvs
->dropentry_lock
);
146 spin_lock(&ipvs
->droppacket_lock
);
147 switch (ipvs
->sysctl_drop_packet
) {
153 ipvs
->drop_rate
= ipvs
->drop_counter
154 = ipvs
->sysctl_amemthresh
/
155 (ipvs
->sysctl_amemthresh
-availmem
);
156 ipvs
->sysctl_drop_packet
= 2;
163 ipvs
->drop_rate
= ipvs
->drop_counter
164 = ipvs
->sysctl_amemthresh
/
165 (ipvs
->sysctl_amemthresh
-availmem
);
168 ipvs
->sysctl_drop_packet
= 1;
172 ipvs
->drop_rate
= ipvs
->sysctl_am_droprate
;
175 spin_unlock(&ipvs
->droppacket_lock
);
178 spin_lock(&ipvs
->securetcp_lock
);
179 switch (ipvs
->sysctl_secure_tcp
) {
181 if (old_secure_tcp
>= 2)
186 if (old_secure_tcp
< 2)
188 ipvs
->sysctl_secure_tcp
= 2;
190 if (old_secure_tcp
>= 2)
196 if (old_secure_tcp
< 2)
199 if (old_secure_tcp
>= 2)
201 ipvs
->sysctl_secure_tcp
= 1;
205 if (old_secure_tcp
< 2)
209 old_secure_tcp
= ipvs
->sysctl_secure_tcp
;
211 ip_vs_protocol_timeout_change(ipvs
,
212 ipvs
->sysctl_secure_tcp
> 1);
213 spin_unlock(&ipvs
->securetcp_lock
);
220 * Timer for checking the defense
222 #define DEFENSE_TIMER_PERIOD 1*HZ
224 static void defense_work_handler(struct work_struct
*work
)
226 struct netns_ipvs
*ipvs
=
227 container_of(work
, struct netns_ipvs
, defense_work
.work
);
229 update_defense_level(ipvs
);
230 if (atomic_read(&ipvs
->dropentry
))
231 ip_vs_random_dropentry(ipvs
->net
);
232 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
237 ip_vs_use_count_inc(void)
239 return try_module_get(THIS_MODULE
);
243 ip_vs_use_count_dec(void)
245 module_put(THIS_MODULE
);
250 * Hash table: for virtual service lookups
252 #define IP_VS_SVC_TAB_BITS 8
253 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
254 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
256 /* the service table hashed by <protocol, addr, port> */
257 static struct hlist_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
258 /* the service table hashed by fwmark */
259 static struct hlist_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
263 * Returns hash value for virtual service
265 static inline unsigned int
266 ip_vs_svc_hashkey(struct net
*net
, int af
, unsigned int proto
,
267 const union nf_inet_addr
*addr
, __be16 port
)
269 register unsigned int porth
= ntohs(port
);
270 __be32 addr_fold
= addr
->ip
;
273 #ifdef CONFIG_IP_VS_IPV6
275 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
276 addr
->ip6
[2]^addr
->ip6
[3];
278 ahash
= ntohl(addr_fold
);
279 ahash
^= ((size_t) net
>> 8);
281 return (proto
^ ahash
^ (porth
>> IP_VS_SVC_TAB_BITS
) ^ porth
) &
286 * Returns hash value of fwmark for virtual service lookup
288 static inline unsigned int ip_vs_svc_fwm_hashkey(struct net
*net
, __u32 fwmark
)
290 return (((size_t)net
>>8) ^ fwmark
) & IP_VS_SVC_TAB_MASK
;
294 * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
295 * or in the ip_vs_svc_fwm_table by fwmark.
296 * Should be called with locked tables.
298 static int ip_vs_svc_hash(struct ip_vs_service
*svc
)
302 if (svc
->flags
& IP_VS_SVC_F_HASHED
) {
303 pr_err("%s(): request for already hashed, called from %pF\n",
304 __func__
, __builtin_return_address(0));
308 if (svc
->fwmark
== 0) {
310 * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
312 hash
= ip_vs_svc_hashkey(svc
->net
, svc
->af
, svc
->protocol
,
313 &svc
->addr
, svc
->port
);
314 hlist_add_head_rcu(&svc
->s_list
, &ip_vs_svc_table
[hash
]);
317 * Hash it by fwmark in svc_fwm_table
319 hash
= ip_vs_svc_fwm_hashkey(svc
->net
, svc
->fwmark
);
320 hlist_add_head_rcu(&svc
->f_list
, &ip_vs_svc_fwm_table
[hash
]);
323 svc
->flags
|= IP_VS_SVC_F_HASHED
;
324 /* increase its refcnt because it is referenced by the svc table */
325 atomic_inc(&svc
->refcnt
);
331 * Unhashes a service from svc_table / svc_fwm_table.
332 * Should be called with locked tables.
334 static int ip_vs_svc_unhash(struct ip_vs_service
*svc
)
336 if (!(svc
->flags
& IP_VS_SVC_F_HASHED
)) {
337 pr_err("%s(): request for unhash flagged, called from %pF\n",
338 __func__
, __builtin_return_address(0));
342 if (svc
->fwmark
== 0) {
343 /* Remove it from the svc_table table */
344 hlist_del_rcu(&svc
->s_list
);
346 /* Remove it from the svc_fwm_table table */
347 hlist_del_rcu(&svc
->f_list
);
350 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
351 atomic_dec(&svc
->refcnt
);
357 * Get service by {netns, proto,addr,port} in the service table.
359 static inline struct ip_vs_service
*
360 __ip_vs_service_find(struct net
*net
, int af
, __u16 protocol
,
361 const union nf_inet_addr
*vaddr
, __be16 vport
)
364 struct ip_vs_service
*svc
;
366 /* Check for "full" addressed entries */
367 hash
= ip_vs_svc_hashkey(net
, af
, protocol
, vaddr
, vport
);
369 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[hash
], s_list
) {
371 && ip_vs_addr_equal(af
, &svc
->addr
, vaddr
)
372 && (svc
->port
== vport
)
373 && (svc
->protocol
== protocol
)
374 && net_eq(svc
->net
, net
)) {
385 * Get service by {fwmark} in the service table.
387 static inline struct ip_vs_service
*
388 __ip_vs_svc_fwm_find(struct net
*net
, int af
, __u32 fwmark
)
391 struct ip_vs_service
*svc
;
393 /* Check for fwmark addressed entries */
394 hash
= ip_vs_svc_fwm_hashkey(net
, fwmark
);
396 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[hash
], f_list
) {
397 if (svc
->fwmark
== fwmark
&& svc
->af
== af
398 && net_eq(svc
->net
, net
)) {
407 /* Find service, called under RCU lock */
408 struct ip_vs_service
*
409 ip_vs_service_find(struct net
*net
, int af
, __u32 fwmark
, __u16 protocol
,
410 const union nf_inet_addr
*vaddr
, __be16 vport
)
412 struct ip_vs_service
*svc
;
413 struct netns_ipvs
*ipvs
= net_ipvs(net
);
416 * Check the table hashed by fwmark first
419 svc
= __ip_vs_svc_fwm_find(net
, af
, fwmark
);
425 * Check the table hashed by <protocol,addr,port>
426 * for "full" addressed entries
428 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, vport
);
431 && protocol
== IPPROTO_TCP
432 && atomic_read(&ipvs
->ftpsvc_counter
)
433 && (vport
== FTPDATA
|| ntohs(vport
) >= PROT_SOCK
)) {
435 * Check if ftp service entry exists, the packet
436 * might belong to FTP data connections.
438 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, FTPPORT
);
442 && atomic_read(&ipvs
->nullsvc_counter
)) {
444 * Check if the catch-all port (port zero) exists
446 svc
= __ip_vs_service_find(net
, af
, protocol
, vaddr
, 0);
450 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
451 fwmark
, ip_vs_proto_name(protocol
),
452 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
453 svc
? "hit" : "not hit");
460 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
462 atomic_inc(&svc
->refcnt
);
463 rcu_assign_pointer(dest
->svc
, svc
);
466 static void ip_vs_service_free(struct ip_vs_service
*svc
)
468 free_percpu(svc
->stats
.cpustats
);
472 static void ip_vs_service_rcu_free(struct rcu_head
*head
)
474 struct ip_vs_service
*svc
;
476 svc
= container_of(head
, struct ip_vs_service
, rcu_head
);
477 ip_vs_service_free(svc
);
480 static void __ip_vs_svc_put(struct ip_vs_service
*svc
, bool do_delay
)
482 if (atomic_dec_and_test(&svc
->refcnt
)) {
483 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
485 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
488 call_rcu(&svc
->rcu_head
, ip_vs_service_rcu_free
);
490 ip_vs_service_free(svc
);
496 * Returns hash value for real service
498 static inline unsigned int ip_vs_rs_hashkey(int af
,
499 const union nf_inet_addr
*addr
,
502 register unsigned int porth
= ntohs(port
);
503 __be32 addr_fold
= addr
->ip
;
505 #ifdef CONFIG_IP_VS_IPV6
507 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
508 addr
->ip6
[2]^addr
->ip6
[3];
511 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
515 /* Hash ip_vs_dest in rs_table by <proto,addr,port>. */
516 static void ip_vs_rs_hash(struct netns_ipvs
*ipvs
, struct ip_vs_dest
*dest
)
520 if (dest
->in_rs_table
)
524 * Hash by proto,addr,port,
525 * which are the parameters of the real service.
527 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
529 hlist_add_head_rcu(&dest
->d_list
, &ipvs
->rs_table
[hash
]);
530 dest
->in_rs_table
= 1;
533 /* Unhash ip_vs_dest from rs_table. */
534 static void ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
537 * Remove it from the rs_table table.
539 if (dest
->in_rs_table
) {
540 hlist_del_rcu(&dest
->d_list
);
541 dest
->in_rs_table
= 0;
545 /* Check if real service by <proto,addr,port> is present */
546 bool ip_vs_has_real_service(struct net
*net
, int af
, __u16 protocol
,
547 const union nf_inet_addr
*daddr
, __be16 dport
)
549 struct netns_ipvs
*ipvs
= net_ipvs(net
);
551 struct ip_vs_dest
*dest
;
553 /* Check for "full" addressed entries */
554 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
557 hlist_for_each_entry_rcu(dest
, &ipvs
->rs_table
[hash
], d_list
) {
558 if (dest
->port
== dport
&&
560 ip_vs_addr_equal(af
, &dest
->addr
, daddr
) &&
561 (dest
->protocol
== protocol
|| dest
->vfwmark
)) {
572 /* Lookup destination by {addr,port} in the given service
573 * Called under RCU lock.
575 static struct ip_vs_dest
*
576 ip_vs_lookup_dest(struct ip_vs_service
*svc
, int dest_af
,
577 const union nf_inet_addr
*daddr
, __be16 dport
)
579 struct ip_vs_dest
*dest
;
582 * Find the destination for the given service
584 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
585 if ((dest
->af
== dest_af
) &&
586 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
587 (dest
->port
== dport
)) {
597 * Find destination by {daddr,dport,vaddr,protocol}
598 * Created to be used in ip_vs_process_message() in
599 * the backup synchronization daemon. It finds the
600 * destination to be bound to the received connection
602 * Called under RCU lock, no refcnt is returned.
604 struct ip_vs_dest
*ip_vs_find_dest(struct net
*net
, int svc_af
, int dest_af
,
605 const union nf_inet_addr
*daddr
,
607 const union nf_inet_addr
*vaddr
,
608 __be16 vport
, __u16 protocol
, __u32 fwmark
,
611 struct ip_vs_dest
*dest
;
612 struct ip_vs_service
*svc
;
615 svc
= ip_vs_service_find(net
, svc_af
, fwmark
, protocol
, vaddr
, vport
);
618 if (fwmark
&& (flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
)
620 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
);
622 dest
= ip_vs_lookup_dest(svc
, dest_af
, daddr
, port
^ dport
);
626 void ip_vs_dest_dst_rcu_free(struct rcu_head
*head
)
628 struct ip_vs_dest_dst
*dest_dst
= container_of(head
,
629 struct ip_vs_dest_dst
,
632 dst_release(dest_dst
->dst_cache
);
636 /* Release dest_dst and dst_cache for dest in user context */
637 static void __ip_vs_dst_cache_reset(struct ip_vs_dest
*dest
)
639 struct ip_vs_dest_dst
*old
;
641 old
= rcu_dereference_protected(dest
->dest_dst
, 1);
643 RCU_INIT_POINTER(dest
->dest_dst
, NULL
);
644 call_rcu(&old
->rcu_head
, ip_vs_dest_dst_rcu_free
);
649 * Lookup dest by {svc,addr,port} in the destination trash.
650 * The destination trash is used to hold the destinations that are removed
651 * from the service table but are still referenced by some conn entries.
652 * The reason to add the destination trash is when the dest is temporary
653 * down (either by administrator or by monitor program), the dest can be
654 * picked back from the trash, the remaining connections to the dest can
655 * continue, and the counting information of the dest is also useful for
658 static struct ip_vs_dest
*
659 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, int dest_af
,
660 const union nf_inet_addr
*daddr
, __be16 dport
)
662 struct ip_vs_dest
*dest
;
663 struct netns_ipvs
*ipvs
= net_ipvs(svc
->net
);
666 * Find the destination in trash
668 spin_lock_bh(&ipvs
->dest_trash_lock
);
669 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
670 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
673 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
675 atomic_read(&dest
->refcnt
));
676 if (dest
->af
== dest_af
&&
677 ip_vs_addr_equal(dest_af
, &dest
->addr
, daddr
) &&
678 dest
->port
== dport
&&
679 dest
->vfwmark
== svc
->fwmark
&&
680 dest
->protocol
== svc
->protocol
&&
682 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
683 dest
->vport
== svc
->port
))) {
685 list_del(&dest
->t_list
);
686 ip_vs_dest_hold(dest
);
694 spin_unlock_bh(&ipvs
->dest_trash_lock
);
699 static void ip_vs_dest_free(struct ip_vs_dest
*dest
)
701 struct ip_vs_service
*svc
= rcu_dereference_protected(dest
->svc
, 1);
703 __ip_vs_dst_cache_reset(dest
);
704 __ip_vs_svc_put(svc
, false);
705 free_percpu(dest
->stats
.cpustats
);
706 ip_vs_dest_put_and_free(dest
);
710 * Clean up all the destinations in the trash
711 * Called by the ip_vs_control_cleanup()
713 * When the ip_vs_control_clearup is activated by ipvs module exit,
714 * the service tables must have been flushed and all the connections
715 * are expired, and the refcnt of each destination in the trash must
716 * be 0, so we simply release them here.
718 static void ip_vs_trash_cleanup(struct net
*net
)
720 struct ip_vs_dest
*dest
, *nxt
;
721 struct netns_ipvs
*ipvs
= net_ipvs(net
);
723 del_timer_sync(&ipvs
->dest_trash_timer
);
724 /* No need to use dest_trash_lock */
725 list_for_each_entry_safe(dest
, nxt
, &ipvs
->dest_trash
, t_list
) {
726 list_del(&dest
->t_list
);
727 ip_vs_dest_free(dest
);
732 ip_vs_copy_stats(struct ip_vs_kstats
*dst
, struct ip_vs_stats
*src
)
734 #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
736 spin_lock_bh(&src
->lock
);
738 IP_VS_SHOW_STATS_COUNTER(conns
);
739 IP_VS_SHOW_STATS_COUNTER(inpkts
);
740 IP_VS_SHOW_STATS_COUNTER(outpkts
);
741 IP_VS_SHOW_STATS_COUNTER(inbytes
);
742 IP_VS_SHOW_STATS_COUNTER(outbytes
);
744 ip_vs_read_estimator(dst
, src
);
746 spin_unlock_bh(&src
->lock
);
750 ip_vs_export_stats_user(struct ip_vs_stats_user
*dst
, struct ip_vs_kstats
*src
)
752 dst
->conns
= (u32
)src
->conns
;
753 dst
->inpkts
= (u32
)src
->inpkts
;
754 dst
->outpkts
= (u32
)src
->outpkts
;
755 dst
->inbytes
= src
->inbytes
;
756 dst
->outbytes
= src
->outbytes
;
757 dst
->cps
= (u32
)src
->cps
;
758 dst
->inpps
= (u32
)src
->inpps
;
759 dst
->outpps
= (u32
)src
->outpps
;
760 dst
->inbps
= (u32
)src
->inbps
;
761 dst
->outbps
= (u32
)src
->outbps
;
765 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
767 spin_lock_bh(&stats
->lock
);
769 /* get current counters as zero point, rates are zeroed */
771 #define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
773 IP_VS_ZERO_STATS_COUNTER(conns
);
774 IP_VS_ZERO_STATS_COUNTER(inpkts
);
775 IP_VS_ZERO_STATS_COUNTER(outpkts
);
776 IP_VS_ZERO_STATS_COUNTER(inbytes
);
777 IP_VS_ZERO_STATS_COUNTER(outbytes
);
779 ip_vs_zero_estimator(stats
);
781 spin_unlock_bh(&stats
->lock
);
785 * Update a destination in the given service
788 __ip_vs_update_dest(struct ip_vs_service
*svc
, struct ip_vs_dest
*dest
,
789 struct ip_vs_dest_user_kern
*udest
, int add
)
791 struct netns_ipvs
*ipvs
= net_ipvs(svc
->net
);
792 struct ip_vs_service
*old_svc
;
793 struct ip_vs_scheduler
*sched
;
796 /* We cannot modify an address and change the address family */
797 BUG_ON(!add
&& udest
->af
!= dest
->af
);
799 if (add
&& udest
->af
!= svc
->af
)
800 ipvs
->mixed_address_family_dests
++;
802 /* set the weight and the flags */
803 atomic_set(&dest
->weight
, udest
->weight
);
804 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
805 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
807 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
808 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
809 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
812 * Put the real service in rs_table if not present.
813 * For now only for NAT!
815 ip_vs_rs_hash(ipvs
, dest
);
817 atomic_set(&dest
->conn_flags
, conn_flags
);
819 /* bind the service */
820 old_svc
= rcu_dereference_protected(dest
->svc
, 1);
822 __ip_vs_bind_svc(dest
, svc
);
824 if (old_svc
!= svc
) {
825 ip_vs_zero_stats(&dest
->stats
);
826 __ip_vs_bind_svc(dest
, svc
);
827 __ip_vs_svc_put(old_svc
, true);
831 /* set the dest status flags */
832 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
834 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
835 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
836 dest
->u_threshold
= udest
->u_threshold
;
837 dest
->l_threshold
= udest
->l_threshold
;
839 dest
->af
= udest
->af
;
841 spin_lock_bh(&dest
->dst_lock
);
842 __ip_vs_dst_cache_reset(dest
);
843 spin_unlock_bh(&dest
->dst_lock
);
845 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
847 ip_vs_start_estimator(svc
->net
, &dest
->stats
);
848 list_add_rcu(&dest
->n_list
, &svc
->destinations
);
851 sched
->add_dest(svc
, dest
);
854 sched
->upd_dest(svc
, dest
);
860 * Create a destination for the given service
863 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
864 struct ip_vs_dest
**dest_p
)
866 struct ip_vs_dest
*dest
;
867 unsigned int atype
, i
;
871 #ifdef CONFIG_IP_VS_IPV6
872 if (udest
->af
== AF_INET6
) {
873 atype
= ipv6_addr_type(&udest
->addr
.in6
);
874 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
875 atype
& IPV6_ADDR_LINKLOCAL
) &&
876 !__ip_vs_addr_is_local_v6(svc
->net
, &udest
->addr
.in6
))
881 atype
= inet_addr_type(svc
->net
, udest
->addr
.ip
);
882 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
886 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
890 dest
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
891 if (!dest
->stats
.cpustats
)
894 for_each_possible_cpu(i
) {
895 struct ip_vs_cpu_stats
*ip_vs_dest_stats
;
896 ip_vs_dest_stats
= per_cpu_ptr(dest
->stats
.cpustats
, i
);
897 u64_stats_init(&ip_vs_dest_stats
->syncp
);
900 dest
->af
= udest
->af
;
901 dest
->protocol
= svc
->protocol
;
902 dest
->vaddr
= svc
->addr
;
903 dest
->vport
= svc
->port
;
904 dest
->vfwmark
= svc
->fwmark
;
905 ip_vs_addr_copy(udest
->af
, &dest
->addr
, &udest
->addr
);
906 dest
->port
= udest
->port
;
908 atomic_set(&dest
->activeconns
, 0);
909 atomic_set(&dest
->inactconns
, 0);
910 atomic_set(&dest
->persistconns
, 0);
911 atomic_set(&dest
->refcnt
, 1);
913 INIT_HLIST_NODE(&dest
->d_list
);
914 spin_lock_init(&dest
->dst_lock
);
915 spin_lock_init(&dest
->stats
.lock
);
916 __ip_vs_update_dest(svc
, dest
, udest
, 1);
930 * Add a destination into an existing service
933 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
935 struct ip_vs_dest
*dest
;
936 union nf_inet_addr daddr
;
937 __be16 dport
= udest
->port
;
942 if (udest
->weight
< 0) {
943 pr_err("%s(): server weight less than zero\n", __func__
);
947 if (udest
->l_threshold
> udest
->u_threshold
) {
948 pr_err("%s(): lower threshold is higher than upper threshold\n",
953 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
955 /* We use function that requires RCU lock */
957 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
961 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
966 * Check if the dest already exists in the trash and
967 * is from the same service
969 dest
= ip_vs_trash_get_dest(svc
, udest
->af
, &daddr
, dport
);
972 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
973 "dest->refcnt=%d, service %u/%s:%u\n",
974 IP_VS_DBG_ADDR(udest
->af
, &daddr
), ntohs(dport
),
975 atomic_read(&dest
->refcnt
),
977 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
980 __ip_vs_update_dest(svc
, dest
, udest
, 1);
984 * Allocate and initialize the dest structure
986 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
995 * Edit a destination in the given service
998 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1000 struct ip_vs_dest
*dest
;
1001 union nf_inet_addr daddr
;
1002 __be16 dport
= udest
->port
;
1006 if (udest
->weight
< 0) {
1007 pr_err("%s(): server weight less than zero\n", __func__
);
1011 if (udest
->l_threshold
> udest
->u_threshold
) {
1012 pr_err("%s(): lower threshold is higher than upper threshold\n",
1017 ip_vs_addr_copy(udest
->af
, &daddr
, &udest
->addr
);
1019 /* We use function that requires RCU lock */
1021 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &daddr
, dport
);
1025 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1029 __ip_vs_update_dest(svc
, dest
, udest
, 0);
1036 * Delete a destination (must be already unlinked from the service)
1038 static void __ip_vs_del_dest(struct net
*net
, struct ip_vs_dest
*dest
,
1041 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1043 ip_vs_stop_estimator(net
, &dest
->stats
);
1046 * Remove it from the d-linked list with the real services.
1048 ip_vs_rs_unhash(dest
);
1050 spin_lock_bh(&ipvs
->dest_trash_lock
);
1051 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
1052 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
), ntohs(dest
->port
),
1053 atomic_read(&dest
->refcnt
));
1054 if (list_empty(&ipvs
->dest_trash
) && !cleanup
)
1055 mod_timer(&ipvs
->dest_trash_timer
,
1056 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1057 /* dest lives in trash without reference */
1058 list_add(&dest
->t_list
, &ipvs
->dest_trash
);
1059 dest
->idle_start
= 0;
1060 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1061 ip_vs_dest_put(dest
);
1066 * Unlink a destination from the given service
1068 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1069 struct ip_vs_dest
*dest
,
1072 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1075 * Remove it from the d-linked destination list.
1077 list_del_rcu(&dest
->n_list
);
1080 if (dest
->af
!= svc
->af
)
1081 net_ipvs(svc
->net
)->mixed_address_family_dests
--;
1084 struct ip_vs_scheduler
*sched
;
1086 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1087 if (sched
->del_dest
)
1088 sched
->del_dest(svc
, dest
);
1094 * Delete a destination server in the given service
1097 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1099 struct ip_vs_dest
*dest
;
1100 __be16 dport
= udest
->port
;
1104 /* We use function that requires RCU lock */
1106 dest
= ip_vs_lookup_dest(svc
, udest
->af
, &udest
->addr
, dport
);
1110 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1115 * Unlink dest from the service
1117 __ip_vs_unlink_dest(svc
, dest
, 1);
1120 * Delete the destination
1122 __ip_vs_del_dest(svc
->net
, dest
, false);
1129 static void ip_vs_dest_trash_expire(unsigned long data
)
1131 struct net
*net
= (struct net
*) data
;
1132 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1133 struct ip_vs_dest
*dest
, *next
;
1134 unsigned long now
= jiffies
;
1136 spin_lock(&ipvs
->dest_trash_lock
);
1137 list_for_each_entry_safe(dest
, next
, &ipvs
->dest_trash
, t_list
) {
1138 if (atomic_read(&dest
->refcnt
) > 0)
1140 if (dest
->idle_start
) {
1141 if (time_before(now
, dest
->idle_start
+
1142 IP_VS_DEST_TRASH_PERIOD
))
1145 dest
->idle_start
= max(1UL, now
);
1148 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
1150 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1152 list_del(&dest
->t_list
);
1153 ip_vs_dest_free(dest
);
1155 if (!list_empty(&ipvs
->dest_trash
))
1156 mod_timer(&ipvs
->dest_trash_timer
,
1157 jiffies
+ (IP_VS_DEST_TRASH_PERIOD
>> 1));
1158 spin_unlock(&ipvs
->dest_trash_lock
);
1162 * Add a service into the service hash table
1165 ip_vs_add_service(struct net
*net
, struct ip_vs_service_user_kern
*u
,
1166 struct ip_vs_service
**svc_p
)
1169 struct ip_vs_scheduler
*sched
= NULL
;
1170 struct ip_vs_pe
*pe
= NULL
;
1171 struct ip_vs_service
*svc
= NULL
;
1172 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1174 /* increase the module use count */
1175 ip_vs_use_count_inc();
1177 /* Lookup the scheduler by 'u->sched_name' */
1178 sched
= ip_vs_scheduler_get(u
->sched_name
);
1179 if (sched
== NULL
) {
1180 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1185 if (u
->pe_name
&& *u
->pe_name
) {
1186 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1188 pr_info("persistence engine module ip_vs_pe_%s "
1189 "not found\n", u
->pe_name
);
1195 #ifdef CONFIG_IP_VS_IPV6
1196 if (u
->af
== AF_INET6
) {
1197 __u32 plen
= (__force __u32
) u
->netmask
;
1199 if (plen
< 1 || plen
> 128) {
1206 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1208 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1212 svc
->stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
1213 if (!svc
->stats
.cpustats
) {
1218 for_each_possible_cpu(i
) {
1219 struct ip_vs_cpu_stats
*ip_vs_stats
;
1220 ip_vs_stats
= per_cpu_ptr(svc
->stats
.cpustats
, i
);
1221 u64_stats_init(&ip_vs_stats
->syncp
);
1225 /* I'm the first user of the service */
1226 atomic_set(&svc
->refcnt
, 0);
1229 svc
->protocol
= u
->protocol
;
1230 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1231 svc
->port
= u
->port
;
1232 svc
->fwmark
= u
->fwmark
;
1233 svc
->flags
= u
->flags
;
1234 svc
->timeout
= u
->timeout
* HZ
;
1235 svc
->netmask
= u
->netmask
;
1238 INIT_LIST_HEAD(&svc
->destinations
);
1239 spin_lock_init(&svc
->sched_lock
);
1240 spin_lock_init(&svc
->stats
.lock
);
1242 /* Bind the scheduler */
1243 ret
= ip_vs_bind_scheduler(svc
, sched
);
1248 /* Bind the ct retriever */
1249 RCU_INIT_POINTER(svc
->pe
, pe
);
1252 /* Update the virtual service counters */
1253 if (svc
->port
== FTPPORT
)
1254 atomic_inc(&ipvs
->ftpsvc_counter
);
1255 else if (svc
->port
== 0)
1256 atomic_inc(&ipvs
->nullsvc_counter
);
1258 ip_vs_start_estimator(net
, &svc
->stats
);
1260 /* Count only IPv4 services for old get/setsockopt interface */
1261 if (svc
->af
== AF_INET
)
1262 ipvs
->num_services
++;
1264 /* Hash the service into the service table */
1265 ip_vs_svc_hash(svc
);
1268 /* Now there is a service - full throttle */
1275 ip_vs_unbind_scheduler(svc
, sched
);
1276 ip_vs_service_free(svc
);
1278 ip_vs_scheduler_put(sched
);
1281 /* decrease the module use count */
1282 ip_vs_use_count_dec();
1289 * Edit a service and bind it with a new scheduler
1292 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1294 struct ip_vs_scheduler
*sched
, *old_sched
;
1295 struct ip_vs_pe
*pe
= NULL
, *old_pe
= NULL
;
1299 * Lookup the scheduler, by 'u->sched_name'
1301 sched
= ip_vs_scheduler_get(u
->sched_name
);
1302 if (sched
== NULL
) {
1303 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1308 if (u
->pe_name
&& *u
->pe_name
) {
1309 pe
= ip_vs_pe_getbyname(u
->pe_name
);
1311 pr_info("persistence engine module ip_vs_pe_%s "
1312 "not found\n", u
->pe_name
);
1319 #ifdef CONFIG_IP_VS_IPV6
1320 if (u
->af
== AF_INET6
) {
1321 __u32 plen
= (__force __u32
) u
->netmask
;
1323 if (plen
< 1 || plen
> 128) {
1330 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1331 if (sched
!= old_sched
) {
1332 /* Bind the new scheduler */
1333 ret
= ip_vs_bind_scheduler(svc
, sched
);
1338 /* Unbind the old scheduler on success */
1339 ip_vs_unbind_scheduler(svc
, old_sched
);
1343 * Set the flags and timeout value
1345 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1346 svc
->timeout
= u
->timeout
* HZ
;
1347 svc
->netmask
= u
->netmask
;
1349 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1351 rcu_assign_pointer(svc
->pe
, pe
);
1354 ip_vs_scheduler_put(old_sched
);
1355 ip_vs_pe_put(old_pe
);
1360 * Delete a service from the service list
1361 * - The service must be unlinked, unlocked and not referenced!
1362 * - We are called under _bh lock
1364 static void __ip_vs_del_service(struct ip_vs_service
*svc
, bool cleanup
)
1366 struct ip_vs_dest
*dest
, *nxt
;
1367 struct ip_vs_scheduler
*old_sched
;
1368 struct ip_vs_pe
*old_pe
;
1369 struct netns_ipvs
*ipvs
= net_ipvs(svc
->net
);
1371 pr_info("%s: enter\n", __func__
);
1373 /* Count only IPv4 services for old get/setsockopt interface */
1374 if (svc
->af
== AF_INET
)
1375 ipvs
->num_services
--;
1377 ip_vs_stop_estimator(svc
->net
, &svc
->stats
);
1379 /* Unbind scheduler */
1380 old_sched
= rcu_dereference_protected(svc
->scheduler
, 1);
1381 ip_vs_unbind_scheduler(svc
, old_sched
);
1382 ip_vs_scheduler_put(old_sched
);
1384 /* Unbind persistence engine, keep svc->pe */
1385 old_pe
= rcu_dereference_protected(svc
->pe
, 1);
1386 ip_vs_pe_put(old_pe
);
1389 * Unlink the whole destination list
1391 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1392 __ip_vs_unlink_dest(svc
, dest
, 0);
1393 __ip_vs_del_dest(svc
->net
, dest
, cleanup
);
1397 * Update the virtual service counters
1399 if (svc
->port
== FTPPORT
)
1400 atomic_dec(&ipvs
->ftpsvc_counter
);
1401 else if (svc
->port
== 0)
1402 atomic_dec(&ipvs
->nullsvc_counter
);
1405 * Free the service if nobody refers to it
1407 __ip_vs_svc_put(svc
, true);
1409 /* decrease the module use count */
1410 ip_vs_use_count_dec();
1414 * Unlink a service from list and try to delete it if its refcnt reached 0
1416 static void ip_vs_unlink_service(struct ip_vs_service
*svc
, bool cleanup
)
1418 /* Hold svc to avoid double release from dest_trash */
1419 atomic_inc(&svc
->refcnt
);
1421 * Unhash it from the service table
1423 ip_vs_svc_unhash(svc
);
1425 __ip_vs_del_service(svc
, cleanup
);
1429 * Delete a service from the service list
1431 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1435 ip_vs_unlink_service(svc
, false);
1442 * Flush all the virtual services
1444 static int ip_vs_flush(struct net
*net
, bool cleanup
)
1447 struct ip_vs_service
*svc
;
1448 struct hlist_node
*n
;
1451 * Flush the service table hashed by <netns,protocol,addr,port>
1453 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1454 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_table
[idx
],
1456 if (net_eq(svc
->net
, net
))
1457 ip_vs_unlink_service(svc
, cleanup
);
1462 * Flush the service table hashed by fwmark
1464 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1465 hlist_for_each_entry_safe(svc
, n
, &ip_vs_svc_fwm_table
[idx
],
1467 if (net_eq(svc
->net
, net
))
1468 ip_vs_unlink_service(svc
, cleanup
);
1476 * Delete service by {netns} in the service table.
1477 * Called by __ip_vs_cleanup()
1479 void ip_vs_service_net_cleanup(struct net
*net
)
1482 /* Check for "full" addressed entries */
1483 mutex_lock(&__ip_vs_mutex
);
1484 ip_vs_flush(net
, true);
1485 mutex_unlock(&__ip_vs_mutex
);
1489 /* Put all references for device (dst_cache) */
1491 ip_vs_forget_dev(struct ip_vs_dest
*dest
, struct net_device
*dev
)
1493 struct ip_vs_dest_dst
*dest_dst
;
1495 spin_lock_bh(&dest
->dst_lock
);
1496 dest_dst
= rcu_dereference_protected(dest
->dest_dst
, 1);
1497 if (dest_dst
&& dest_dst
->dst_cache
->dev
== dev
) {
1498 IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
1500 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1502 atomic_read(&dest
->refcnt
));
1503 __ip_vs_dst_cache_reset(dest
);
1505 spin_unlock_bh(&dest
->dst_lock
);
1508 /* Netdev event receiver
1509 * Currently only NETDEV_DOWN is handled to release refs to cached dsts
1511 static int ip_vs_dst_event(struct notifier_block
*this, unsigned long event
,
1514 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1515 struct net
*net
= dev_net(dev
);
1516 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1517 struct ip_vs_service
*svc
;
1518 struct ip_vs_dest
*dest
;
1521 if (event
!= NETDEV_DOWN
|| !ipvs
)
1523 IP_VS_DBG(3, "%s() dev=%s\n", __func__
, dev
->name
);
1525 mutex_lock(&__ip_vs_mutex
);
1526 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1527 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1528 if (net_eq(svc
->net
, net
)) {
1529 list_for_each_entry(dest
, &svc
->destinations
,
1531 ip_vs_forget_dev(dest
, dev
);
1536 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1537 if (net_eq(svc
->net
, net
)) {
1538 list_for_each_entry(dest
, &svc
->destinations
,
1540 ip_vs_forget_dev(dest
, dev
);
1547 spin_lock_bh(&ipvs
->dest_trash_lock
);
1548 list_for_each_entry(dest
, &ipvs
->dest_trash
, t_list
) {
1549 ip_vs_forget_dev(dest
, dev
);
1551 spin_unlock_bh(&ipvs
->dest_trash_lock
);
1552 mutex_unlock(&__ip_vs_mutex
);
1558 * Zero counters in a service or all services
1560 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1562 struct ip_vs_dest
*dest
;
1564 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1565 ip_vs_zero_stats(&dest
->stats
);
1567 ip_vs_zero_stats(&svc
->stats
);
1571 static int ip_vs_zero_all(struct net
*net
)
1574 struct ip_vs_service
*svc
;
1576 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1577 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1578 if (net_eq(svc
->net
, net
))
1579 ip_vs_zero_service(svc
);
1583 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1584 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1585 if (net_eq(svc
->net
, net
))
1586 ip_vs_zero_service(svc
);
1590 ip_vs_zero_stats(&net_ipvs(net
)->tot_stats
);
1594 #ifdef CONFIG_SYSCTL
1597 static int three
= 3;
1600 proc_do_defense_mode(struct ctl_table
*table
, int write
,
1601 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1603 struct net
*net
= current
->nsproxy
->net_ns
;
1604 int *valp
= table
->data
;
1608 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1609 if (write
&& (*valp
!= val
)) {
1610 if ((*valp
< 0) || (*valp
> 3)) {
1611 /* Restore the correct value */
1614 update_defense_level(net_ipvs(net
));
1621 proc_do_sync_threshold(struct ctl_table
*table
, int write
,
1622 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1624 int *valp
= table
->data
;
1628 /* backup the value first */
1629 memcpy(val
, valp
, sizeof(val
));
1631 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1632 if (write
&& (valp
[0] < 0 || valp
[1] < 0 ||
1633 (valp
[0] >= valp
[1] && valp
[1]))) {
1634 /* Restore the correct value */
1635 memcpy(valp
, val
, sizeof(val
));
1641 proc_do_sync_mode(struct ctl_table
*table
, int write
,
1642 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1644 int *valp
= table
->data
;
1648 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1649 if (write
&& (*valp
!= val
)) {
1650 if ((*valp
< 0) || (*valp
> 1)) {
1651 /* Restore the correct value */
1659 proc_do_sync_ports(struct ctl_table
*table
, int write
,
1660 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1662 int *valp
= table
->data
;
1666 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1667 if (write
&& (*valp
!= val
)) {
1668 if (*valp
< 1 || !is_power_of_2(*valp
)) {
1669 /* Restore the correct value */
1677 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1678 * Do not change order or insert new entries without
1679 * align with netns init in ip_vs_control_net_init()
1682 static struct ctl_table vs_vars
[] = {
1684 .procname
= "amemthresh",
1685 .maxlen
= sizeof(int),
1687 .proc_handler
= proc_dointvec
,
1690 .procname
= "am_droprate",
1691 .maxlen
= sizeof(int),
1693 .proc_handler
= proc_dointvec
,
1696 .procname
= "drop_entry",
1697 .maxlen
= sizeof(int),
1699 .proc_handler
= proc_do_defense_mode
,
1702 .procname
= "drop_packet",
1703 .maxlen
= sizeof(int),
1705 .proc_handler
= proc_do_defense_mode
,
1707 #ifdef CONFIG_IP_VS_NFCT
1709 .procname
= "conntrack",
1710 .maxlen
= sizeof(int),
1712 .proc_handler
= &proc_dointvec
,
1716 .procname
= "secure_tcp",
1717 .maxlen
= sizeof(int),
1719 .proc_handler
= proc_do_defense_mode
,
1722 .procname
= "snat_reroute",
1723 .maxlen
= sizeof(int),
1725 .proc_handler
= &proc_dointvec
,
1728 .procname
= "sync_version",
1729 .maxlen
= sizeof(int),
1731 .proc_handler
= &proc_do_sync_mode
,
1734 .procname
= "sync_ports",
1735 .maxlen
= sizeof(int),
1737 .proc_handler
= &proc_do_sync_ports
,
1740 .procname
= "sync_persist_mode",
1741 .maxlen
= sizeof(int),
1743 .proc_handler
= proc_dointvec
,
1746 .procname
= "sync_qlen_max",
1747 .maxlen
= sizeof(unsigned long),
1749 .proc_handler
= proc_doulongvec_minmax
,
1752 .procname
= "sync_sock_size",
1753 .maxlen
= sizeof(int),
1755 .proc_handler
= proc_dointvec
,
1758 .procname
= "cache_bypass",
1759 .maxlen
= sizeof(int),
1761 .proc_handler
= proc_dointvec
,
1764 .procname
= "expire_nodest_conn",
1765 .maxlen
= sizeof(int),
1767 .proc_handler
= proc_dointvec
,
1770 .procname
= "sloppy_tcp",
1771 .maxlen
= sizeof(int),
1773 .proc_handler
= proc_dointvec
,
1776 .procname
= "sloppy_sctp",
1777 .maxlen
= sizeof(int),
1779 .proc_handler
= proc_dointvec
,
1782 .procname
= "expire_quiescent_template",
1783 .maxlen
= sizeof(int),
1785 .proc_handler
= proc_dointvec
,
1788 .procname
= "sync_threshold",
1790 sizeof(((struct netns_ipvs
*)0)->sysctl_sync_threshold
),
1792 .proc_handler
= proc_do_sync_threshold
,
1795 .procname
= "sync_refresh_period",
1796 .maxlen
= sizeof(int),
1798 .proc_handler
= proc_dointvec_jiffies
,
1801 .procname
= "sync_retries",
1802 .maxlen
= sizeof(int),
1804 .proc_handler
= proc_dointvec_minmax
,
1809 .procname
= "nat_icmp_send",
1810 .maxlen
= sizeof(int),
1812 .proc_handler
= proc_dointvec
,
1815 .procname
= "pmtu_disc",
1816 .maxlen
= sizeof(int),
1818 .proc_handler
= proc_dointvec
,
1821 .procname
= "backup_only",
1822 .maxlen
= sizeof(int),
1824 .proc_handler
= proc_dointvec
,
1827 .procname
= "conn_reuse_mode",
1828 .maxlen
= sizeof(int),
1830 .proc_handler
= proc_dointvec
,
1832 #ifdef CONFIG_IP_VS_DEBUG
1834 .procname
= "debug_level",
1835 .data
= &sysctl_ip_vs_debug_level
,
1836 .maxlen
= sizeof(int),
1838 .proc_handler
= proc_dointvec
,
1846 #ifdef CONFIG_PROC_FS
1849 struct seq_net_private p
; /* Do not move this, netns depends upon it*/
1850 struct hlist_head
*table
;
1855 * Write the contents of the VS rule table to a PROCfs file.
1856 * (It is kept just for backward compatibility)
1858 static inline const char *ip_vs_fwd_name(unsigned int flags
)
1860 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1861 case IP_VS_CONN_F_LOCALNODE
:
1863 case IP_VS_CONN_F_TUNNEL
:
1865 case IP_VS_CONN_F_DROUTE
:
1873 /* Get the Nth entry in the two lists */
1874 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1876 struct net
*net
= seq_file_net(seq
);
1877 struct ip_vs_iter
*iter
= seq
->private;
1879 struct ip_vs_service
*svc
;
1881 /* look in hash by protocol */
1882 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1883 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1884 if (net_eq(svc
->net
, net
) && pos
-- == 0) {
1885 iter
->table
= ip_vs_svc_table
;
1892 /* keep looking in fwmark */
1893 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1894 hlist_for_each_entry_rcu(svc
, &ip_vs_svc_fwm_table
[idx
],
1896 if (net_eq(svc
->net
, net
) && pos
-- == 0) {
1897 iter
->table
= ip_vs_svc_fwm_table
;
1907 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1911 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1915 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1917 struct hlist_node
*e
;
1918 struct ip_vs_iter
*iter
;
1919 struct ip_vs_service
*svc
;
1922 if (v
== SEQ_START_TOKEN
)
1923 return ip_vs_info_array(seq
,0);
1926 iter
= seq
->private;
1928 if (iter
->table
== ip_vs_svc_table
) {
1929 /* next service in table hashed by protocol */
1930 e
= rcu_dereference(hlist_next_rcu(&svc
->s_list
));
1932 return hlist_entry(e
, struct ip_vs_service
, s_list
);
1934 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1935 hlist_for_each_entry_rcu(svc
,
1936 &ip_vs_svc_table
[iter
->bucket
],
1942 iter
->table
= ip_vs_svc_fwm_table
;
1947 /* next service in hashed by fwmark */
1948 e
= rcu_dereference(hlist_next_rcu(&svc
->f_list
));
1950 return hlist_entry(e
, struct ip_vs_service
, f_list
);
1953 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1954 hlist_for_each_entry_rcu(svc
,
1955 &ip_vs_svc_fwm_table
[iter
->bucket
],
1963 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
1970 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
1972 if (v
== SEQ_START_TOKEN
) {
1974 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1975 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
1977 "Prot LocalAddress:Port Scheduler Flags\n");
1979 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1981 const struct ip_vs_service
*svc
= v
;
1982 const struct ip_vs_iter
*iter
= seq
->private;
1983 const struct ip_vs_dest
*dest
;
1984 struct ip_vs_scheduler
*sched
= rcu_dereference(svc
->scheduler
);
1986 if (iter
->table
== ip_vs_svc_table
) {
1987 #ifdef CONFIG_IP_VS_IPV6
1988 if (svc
->af
== AF_INET6
)
1989 seq_printf(seq
, "%s [%pI6]:%04X %s ",
1990 ip_vs_proto_name(svc
->protocol
),
1996 seq_printf(seq
, "%s %08X:%04X %s %s ",
1997 ip_vs_proto_name(svc
->protocol
),
1998 ntohl(svc
->addr
.ip
),
2001 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2003 seq_printf(seq
, "FWM %08X %s %s",
2004 svc
->fwmark
, sched
->name
,
2005 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
2008 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
2009 seq_printf(seq
, "persistent %d %08X\n",
2011 ntohl(svc
->netmask
));
2013 seq_putc(seq
, '\n');
2015 list_for_each_entry_rcu(dest
, &svc
->destinations
, n_list
) {
2016 #ifdef CONFIG_IP_VS_IPV6
2017 if (dest
->af
== AF_INET6
)
2020 " %-7s %-6d %-10d %-10d\n",
2023 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2024 atomic_read(&dest
->weight
),
2025 atomic_read(&dest
->activeconns
),
2026 atomic_read(&dest
->inactconns
));
2031 "%-7s %-6d %-10d %-10d\n",
2032 ntohl(dest
->addr
.ip
),
2034 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
2035 atomic_read(&dest
->weight
),
2036 atomic_read(&dest
->activeconns
),
2037 atomic_read(&dest
->inactconns
));
2044 static const struct seq_operations ip_vs_info_seq_ops
= {
2045 .start
= ip_vs_info_seq_start
,
2046 .next
= ip_vs_info_seq_next
,
2047 .stop
= ip_vs_info_seq_stop
,
2048 .show
= ip_vs_info_seq_show
,
2051 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
2053 return seq_open_net(inode
, file
, &ip_vs_info_seq_ops
,
2054 sizeof(struct ip_vs_iter
));
2057 static const struct file_operations ip_vs_info_fops
= {
2058 .owner
= THIS_MODULE
,
2059 .open
= ip_vs_info_open
,
2061 .llseek
= seq_lseek
,
2062 .release
= seq_release_net
,
2065 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
2067 struct net
*net
= seq_file_single_net(seq
);
2068 struct ip_vs_kstats show
;
2070 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2072 " Total Incoming Outgoing Incoming Outgoing\n");
2074 " Conns Packets Packets Bytes Bytes\n");
2076 ip_vs_copy_stats(&show
, &net_ipvs(net
)->tot_stats
);
2077 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n\n",
2078 (unsigned long long)show
.conns
,
2079 (unsigned long long)show
.inpkts
,
2080 (unsigned long long)show
.outpkts
,
2081 (unsigned long long)show
.inbytes
,
2082 (unsigned long long)show
.outbytes
);
2084 /* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2086 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2087 seq_printf(seq
, "%8LX %8LX %8LX %16LX %16LX\n",
2088 (unsigned long long)show
.cps
,
2089 (unsigned long long)show
.inpps
,
2090 (unsigned long long)show
.outpps
,
2091 (unsigned long long)show
.inbps
,
2092 (unsigned long long)show
.outbps
);
2097 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
2099 return single_open_net(inode
, file
, ip_vs_stats_show
);
2102 static const struct file_operations ip_vs_stats_fops
= {
2103 .owner
= THIS_MODULE
,
2104 .open
= ip_vs_stats_seq_open
,
2106 .llseek
= seq_lseek
,
2107 .release
= single_release_net
,
2110 static int ip_vs_stats_percpu_show(struct seq_file
*seq
, void *v
)
2112 struct net
*net
= seq_file_single_net(seq
);
2113 struct ip_vs_stats
*tot_stats
= &net_ipvs(net
)->tot_stats
;
2114 struct ip_vs_cpu_stats __percpu
*cpustats
= tot_stats
->cpustats
;
2115 struct ip_vs_kstats kstats
;
2118 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2120 " Total Incoming Outgoing Incoming Outgoing\n");
2122 "CPU Conns Packets Packets Bytes Bytes\n");
2124 for_each_possible_cpu(i
) {
2125 struct ip_vs_cpu_stats
*u
= per_cpu_ptr(cpustats
, i
);
2127 u64 conns
, inpkts
, outpkts
, inbytes
, outbytes
;
2130 start
= u64_stats_fetch_begin_irq(&u
->syncp
);
2131 conns
= u
->cnt
.conns
;
2132 inpkts
= u
->cnt
.inpkts
;
2133 outpkts
= u
->cnt
.outpkts
;
2134 inbytes
= u
->cnt
.inbytes
;
2135 outbytes
= u
->cnt
.outbytes
;
2136 } while (u64_stats_fetch_retry_irq(&u
->syncp
, start
));
2138 seq_printf(seq
, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2139 i
, (u64
)conns
, (u64
)inpkts
,
2140 (u64
)outpkts
, (u64
)inbytes
,
2144 ip_vs_copy_stats(&kstats
, tot_stats
);
2146 seq_printf(seq
, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2147 (unsigned long long)kstats
.conns
,
2148 (unsigned long long)kstats
.inpkts
,
2149 (unsigned long long)kstats
.outpkts
,
2150 (unsigned long long)kstats
.inbytes
,
2151 (unsigned long long)kstats
.outbytes
);
2153 /* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2155 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2156 seq_printf(seq
, " %8LX %8LX %8LX %16LX %16LX\n",
2166 static int ip_vs_stats_percpu_seq_open(struct inode
*inode
, struct file
*file
)
2168 return single_open_net(inode
, file
, ip_vs_stats_percpu_show
);
2171 static const struct file_operations ip_vs_stats_percpu_fops
= {
2172 .owner
= THIS_MODULE
,
2173 .open
= ip_vs_stats_percpu_seq_open
,
2175 .llseek
= seq_lseek
,
2176 .release
= single_release_net
,
2181 * Set timeout values for tcp tcpfin udp in the timeout_table.
2183 static int ip_vs_set_timeout(struct net
*net
, struct ip_vs_timeout_user
*u
)
2185 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2186 struct ip_vs_proto_data
*pd
;
2189 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2194 #ifdef CONFIG_IP_VS_PROTO_TCP
2195 if (u
->tcp_timeout
) {
2196 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2197 pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
2198 = u
->tcp_timeout
* HZ
;
2201 if (u
->tcp_fin_timeout
) {
2202 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2203 pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
2204 = u
->tcp_fin_timeout
* HZ
;
2208 #ifdef CONFIG_IP_VS_PROTO_UDP
2209 if (u
->udp_timeout
) {
2210 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2211 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
]
2212 = u
->udp_timeout
* HZ
;
2218 #define CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2220 struct ip_vs_svcdest_user
{
2221 struct ip_vs_service_user s
;
2222 struct ip_vs_dest_user d
;
2225 static const unsigned char set_arglen
[CMDID(IP_VS_SO_SET_MAX
) + 1] = {
2226 [CMDID(IP_VS_SO_SET_ADD
)] = sizeof(struct ip_vs_service_user
),
2227 [CMDID(IP_VS_SO_SET_EDIT
)] = sizeof(struct ip_vs_service_user
),
2228 [CMDID(IP_VS_SO_SET_DEL
)] = sizeof(struct ip_vs_service_user
),
2229 [CMDID(IP_VS_SO_SET_ADDDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2230 [CMDID(IP_VS_SO_SET_DELDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2231 [CMDID(IP_VS_SO_SET_EDITDEST
)] = sizeof(struct ip_vs_svcdest_user
),
2232 [CMDID(IP_VS_SO_SET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2233 [CMDID(IP_VS_SO_SET_STARTDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2234 [CMDID(IP_VS_SO_SET_STOPDAEMON
)] = sizeof(struct ip_vs_daemon_user
),
2235 [CMDID(IP_VS_SO_SET_ZERO
)] = sizeof(struct ip_vs_service_user
),
2238 union ip_vs_set_arglen
{
2239 struct ip_vs_service_user field_IP_VS_SO_SET_ADD
;
2240 struct ip_vs_service_user field_IP_VS_SO_SET_EDIT
;
2241 struct ip_vs_service_user field_IP_VS_SO_SET_DEL
;
2242 struct ip_vs_svcdest_user field_IP_VS_SO_SET_ADDDEST
;
2243 struct ip_vs_svcdest_user field_IP_VS_SO_SET_DELDEST
;
2244 struct ip_vs_svcdest_user field_IP_VS_SO_SET_EDITDEST
;
2245 struct ip_vs_timeout_user field_IP_VS_SO_SET_TIMEOUT
;
2246 struct ip_vs_daemon_user field_IP_VS_SO_SET_STARTDAEMON
;
2247 struct ip_vs_daemon_user field_IP_VS_SO_SET_STOPDAEMON
;
2248 struct ip_vs_service_user field_IP_VS_SO_SET_ZERO
;
2251 #define MAX_SET_ARGLEN sizeof(union ip_vs_set_arglen)
2253 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2254 struct ip_vs_service_user
*usvc_compat
)
2256 memset(usvc
, 0, sizeof(*usvc
));
2259 usvc
->protocol
= usvc_compat
->protocol
;
2260 usvc
->addr
.ip
= usvc_compat
->addr
;
2261 usvc
->port
= usvc_compat
->port
;
2262 usvc
->fwmark
= usvc_compat
->fwmark
;
2264 /* Deep copy of sched_name is not needed here */
2265 usvc
->sched_name
= usvc_compat
->sched_name
;
2267 usvc
->flags
= usvc_compat
->flags
;
2268 usvc
->timeout
= usvc_compat
->timeout
;
2269 usvc
->netmask
= usvc_compat
->netmask
;
2272 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2273 struct ip_vs_dest_user
*udest_compat
)
2275 memset(udest
, 0, sizeof(*udest
));
2277 udest
->addr
.ip
= udest_compat
->addr
;
2278 udest
->port
= udest_compat
->port
;
2279 udest
->conn_flags
= udest_compat
->conn_flags
;
2280 udest
->weight
= udest_compat
->weight
;
2281 udest
->u_threshold
= udest_compat
->u_threshold
;
2282 udest
->l_threshold
= udest_compat
->l_threshold
;
2283 udest
->af
= AF_INET
;
2287 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2289 struct net
*net
= sock_net(sk
);
2291 unsigned char arg
[MAX_SET_ARGLEN
];
2292 struct ip_vs_service_user
*usvc_compat
;
2293 struct ip_vs_service_user_kern usvc
;
2294 struct ip_vs_service
*svc
;
2295 struct ip_vs_dest_user
*udest_compat
;
2296 struct ip_vs_dest_user_kern udest
;
2297 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2299 BUILD_BUG_ON(sizeof(arg
) > 255);
2300 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2303 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2305 if (len
!= set_arglen
[CMDID(cmd
)]) {
2306 IP_VS_DBG(1, "set_ctl: len %u != %u\n",
2307 len
, set_arglen
[CMDID(cmd
)]);
2311 if (copy_from_user(arg
, user
, len
) != 0)
2314 /* increase the module use count */
2315 ip_vs_use_count_inc();
2317 /* Handle daemons since they have another lock */
2318 if (cmd
== IP_VS_SO_SET_STARTDAEMON
||
2319 cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2320 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2322 mutex_lock(&ipvs
->sync_mutex
);
2323 if (cmd
== IP_VS_SO_SET_STARTDAEMON
)
2324 ret
= start_sync_thread(net
, dm
->state
, dm
->mcast_ifn
,
2327 ret
= stop_sync_thread(net
, dm
->state
);
2328 mutex_unlock(&ipvs
->sync_mutex
);
2332 mutex_lock(&__ip_vs_mutex
);
2333 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2334 /* Flush the virtual service */
2335 ret
= ip_vs_flush(net
, false);
2337 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2338 /* Set timeout values for (tcp tcpfin udp) */
2339 ret
= ip_vs_set_timeout(net
, (struct ip_vs_timeout_user
*)arg
);
2343 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2344 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2346 /* We only use the new structs internally, so copy userspace compat
2347 * structs to extended internal versions */
2348 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2349 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2351 if (cmd
== IP_VS_SO_SET_ZERO
) {
2352 /* if no service address is set, zero counters in all */
2353 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2354 ret
= ip_vs_zero_all(net
);
2359 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2360 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2361 usvc
.protocol
!= IPPROTO_SCTP
) {
2362 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2363 usvc
.protocol
, &usvc
.addr
.ip
,
2364 ntohs(usvc
.port
), usvc
.sched_name
);
2369 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2371 if (usvc
.fwmark
== 0)
2372 svc
= __ip_vs_service_find(net
, usvc
.af
, usvc
.protocol
,
2373 &usvc
.addr
, usvc
.port
);
2375 svc
= __ip_vs_svc_fwm_find(net
, usvc
.af
, usvc
.fwmark
);
2378 if (cmd
!= IP_VS_SO_SET_ADD
2379 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2385 case IP_VS_SO_SET_ADD
:
2389 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
2391 case IP_VS_SO_SET_EDIT
:
2392 ret
= ip_vs_edit_service(svc
, &usvc
);
2394 case IP_VS_SO_SET_DEL
:
2395 ret
= ip_vs_del_service(svc
);
2399 case IP_VS_SO_SET_ZERO
:
2400 ret
= ip_vs_zero_service(svc
);
2402 case IP_VS_SO_SET_ADDDEST
:
2403 ret
= ip_vs_add_dest(svc
, &udest
);
2405 case IP_VS_SO_SET_EDITDEST
:
2406 ret
= ip_vs_edit_dest(svc
, &udest
);
2408 case IP_VS_SO_SET_DELDEST
:
2409 ret
= ip_vs_del_dest(svc
, &udest
);
2416 mutex_unlock(&__ip_vs_mutex
);
2418 /* decrease the module use count */
2419 ip_vs_use_count_dec();
2426 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2428 struct ip_vs_scheduler
*sched
;
2429 struct ip_vs_kstats kstats
;
2431 sched
= rcu_dereference_protected(src
->scheduler
, 1);
2432 dst
->protocol
= src
->protocol
;
2433 dst
->addr
= src
->addr
.ip
;
2434 dst
->port
= src
->port
;
2435 dst
->fwmark
= src
->fwmark
;
2436 strlcpy(dst
->sched_name
, sched
->name
, sizeof(dst
->sched_name
));
2437 dst
->flags
= src
->flags
;
2438 dst
->timeout
= src
->timeout
/ HZ
;
2439 dst
->netmask
= src
->netmask
;
2440 dst
->num_dests
= src
->num_dests
;
2441 ip_vs_copy_stats(&kstats
, &src
->stats
);
2442 ip_vs_export_stats_user(&dst
->stats
, &kstats
);
2446 __ip_vs_get_service_entries(struct net
*net
,
2447 const struct ip_vs_get_services
*get
,
2448 struct ip_vs_get_services __user
*uptr
)
2451 struct ip_vs_service
*svc
;
2452 struct ip_vs_service_entry entry
;
2455 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2456 hlist_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2457 /* Only expose IPv4 entries to old interface */
2458 if (svc
->af
!= AF_INET
|| !net_eq(svc
->net
, net
))
2461 if (count
>= get
->num_services
)
2463 memset(&entry
, 0, sizeof(entry
));
2464 ip_vs_copy_service(&entry
, svc
);
2465 if (copy_to_user(&uptr
->entrytable
[count
],
2466 &entry
, sizeof(entry
))) {
2474 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2475 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
2476 /* Only expose IPv4 entries to old interface */
2477 if (svc
->af
!= AF_INET
|| !net_eq(svc
->net
, net
))
2480 if (count
>= get
->num_services
)
2482 memset(&entry
, 0, sizeof(entry
));
2483 ip_vs_copy_service(&entry
, svc
);
2484 if (copy_to_user(&uptr
->entrytable
[count
],
2485 &entry
, sizeof(entry
))) {
2497 __ip_vs_get_dest_entries(struct net
*net
, const struct ip_vs_get_dests
*get
,
2498 struct ip_vs_get_dests __user
*uptr
)
2500 struct ip_vs_service
*svc
;
2501 union nf_inet_addr addr
= { .ip
= get
->addr
};
2506 svc
= __ip_vs_svc_fwm_find(net
, AF_INET
, get
->fwmark
);
2508 svc
= __ip_vs_service_find(net
, AF_INET
, get
->protocol
, &addr
,
2514 struct ip_vs_dest
*dest
;
2515 struct ip_vs_dest_entry entry
;
2516 struct ip_vs_kstats kstats
;
2518 memset(&entry
, 0, sizeof(entry
));
2519 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2520 if (count
>= get
->num_dests
)
2523 /* Cannot expose heterogeneous members via sockopt
2526 if (dest
->af
!= svc
->af
)
2529 entry
.addr
= dest
->addr
.ip
;
2530 entry
.port
= dest
->port
;
2531 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2532 entry
.weight
= atomic_read(&dest
->weight
);
2533 entry
.u_threshold
= dest
->u_threshold
;
2534 entry
.l_threshold
= dest
->l_threshold
;
2535 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2536 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2537 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2538 ip_vs_copy_stats(&kstats
, &dest
->stats
);
2539 ip_vs_export_stats_user(&entry
.stats
, &kstats
);
2540 if (copy_to_user(&uptr
->entrytable
[count
],
2541 &entry
, sizeof(entry
))) {
2553 __ip_vs_get_timeouts(struct net
*net
, struct ip_vs_timeout_user
*u
)
2555 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2556 struct ip_vs_proto_data
*pd
;
2559 memset(u
, 0, sizeof (*u
));
2561 #ifdef CONFIG_IP_VS_PROTO_TCP
2562 pd
= ip_vs_proto_data_get(net
, IPPROTO_TCP
);
2563 u
->tcp_timeout
= pd
->timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2564 u
->tcp_fin_timeout
= pd
->timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2566 #ifdef CONFIG_IP_VS_PROTO_UDP
2567 pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
2569 pd
->timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2573 static const unsigned char get_arglen
[CMDID(IP_VS_SO_GET_MAX
) + 1] = {
2574 [CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2575 [CMDID(IP_VS_SO_GET_INFO
)] = sizeof(struct ip_vs_getinfo
),
2576 [CMDID(IP_VS_SO_GET_SERVICES
)] = sizeof(struct ip_vs_get_services
),
2577 [CMDID(IP_VS_SO_GET_SERVICE
)] = sizeof(struct ip_vs_service_entry
),
2578 [CMDID(IP_VS_SO_GET_DESTS
)] = sizeof(struct ip_vs_get_dests
),
2579 [CMDID(IP_VS_SO_GET_TIMEOUT
)] = sizeof(struct ip_vs_timeout_user
),
2580 [CMDID(IP_VS_SO_GET_DAEMON
)] = 2 * sizeof(struct ip_vs_daemon_user
),
2583 union ip_vs_get_arglen
{
2584 char field_IP_VS_SO_GET_VERSION
[64];
2585 struct ip_vs_getinfo field_IP_VS_SO_GET_INFO
;
2586 struct ip_vs_get_services field_IP_VS_SO_GET_SERVICES
;
2587 struct ip_vs_service_entry field_IP_VS_SO_GET_SERVICE
;
2588 struct ip_vs_get_dests field_IP_VS_SO_GET_DESTS
;
2589 struct ip_vs_timeout_user field_IP_VS_SO_GET_TIMEOUT
;
2590 struct ip_vs_daemon_user field_IP_VS_SO_GET_DAEMON
[2];
2593 #define MAX_GET_ARGLEN sizeof(union ip_vs_get_arglen)
2596 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2598 unsigned char arg
[MAX_GET_ARGLEN
];
2600 unsigned int copylen
;
2601 struct net
*net
= sock_net(sk
);
2602 struct netns_ipvs
*ipvs
= net_ipvs(net
);
2605 BUILD_BUG_ON(sizeof(arg
) > 255);
2606 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
2609 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2612 copylen
= get_arglen
[CMDID(cmd
)];
2613 if (*len
< (int) copylen
) {
2614 IP_VS_DBG(1, "get_ctl: len %d < %u\n", *len
, copylen
);
2618 if (copy_from_user(arg
, user
, copylen
) != 0)
2621 * Handle daemons first since it has its own locking
2623 if (cmd
== IP_VS_SO_GET_DAEMON
) {
2624 struct ip_vs_daemon_user d
[2];
2626 memset(&d
, 0, sizeof(d
));
2627 mutex_lock(&ipvs
->sync_mutex
);
2628 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
) {
2629 d
[0].state
= IP_VS_STATE_MASTER
;
2630 strlcpy(d
[0].mcast_ifn
, ipvs
->master_mcast_ifn
,
2631 sizeof(d
[0].mcast_ifn
));
2632 d
[0].syncid
= ipvs
->master_syncid
;
2634 if (ipvs
->sync_state
& IP_VS_STATE_BACKUP
) {
2635 d
[1].state
= IP_VS_STATE_BACKUP
;
2636 strlcpy(d
[1].mcast_ifn
, ipvs
->backup_mcast_ifn
,
2637 sizeof(d
[1].mcast_ifn
));
2638 d
[1].syncid
= ipvs
->backup_syncid
;
2640 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2642 mutex_unlock(&ipvs
->sync_mutex
);
2646 mutex_lock(&__ip_vs_mutex
);
2648 case IP_VS_SO_GET_VERSION
:
2652 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2653 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2654 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2658 *len
= strlen(buf
)+1;
2662 case IP_VS_SO_GET_INFO
:
2664 struct ip_vs_getinfo info
;
2665 info
.version
= IP_VS_VERSION_CODE
;
2666 info
.size
= ip_vs_conn_tab_size
;
2667 info
.num_services
= ipvs
->num_services
;
2668 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2673 case IP_VS_SO_GET_SERVICES
:
2675 struct ip_vs_get_services
*get
;
2678 get
= (struct ip_vs_get_services
*)arg
;
2679 size
= sizeof(*get
) +
2680 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2682 pr_err("length: %u != %u\n", *len
, size
);
2686 ret
= __ip_vs_get_service_entries(net
, get
, user
);
2690 case IP_VS_SO_GET_SERVICE
:
2692 struct ip_vs_service_entry
*entry
;
2693 struct ip_vs_service
*svc
;
2694 union nf_inet_addr addr
;
2696 entry
= (struct ip_vs_service_entry
*)arg
;
2697 addr
.ip
= entry
->addr
;
2700 svc
= __ip_vs_svc_fwm_find(net
, AF_INET
, entry
->fwmark
);
2702 svc
= __ip_vs_service_find(net
, AF_INET
,
2703 entry
->protocol
, &addr
,
2707 ip_vs_copy_service(entry
, svc
);
2708 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2715 case IP_VS_SO_GET_DESTS
:
2717 struct ip_vs_get_dests
*get
;
2720 get
= (struct ip_vs_get_dests
*)arg
;
2721 size
= sizeof(*get
) +
2722 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2724 pr_err("length: %u != %u\n", *len
, size
);
2728 ret
= __ip_vs_get_dest_entries(net
, get
, user
);
2732 case IP_VS_SO_GET_TIMEOUT
:
2734 struct ip_vs_timeout_user t
;
2736 __ip_vs_get_timeouts(net
, &t
);
2737 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2747 mutex_unlock(&__ip_vs_mutex
);
2752 static struct nf_sockopt_ops ip_vs_sockopts
= {
2754 .set_optmin
= IP_VS_BASE_CTL
,
2755 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2756 .set
= do_ip_vs_set_ctl
,
2757 .get_optmin
= IP_VS_BASE_CTL
,
2758 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2759 .get
= do_ip_vs_get_ctl
,
2760 .owner
= THIS_MODULE
,
2764 * Generic Netlink interface
2767 /* IPVS genetlink family */
2768 static struct genl_family ip_vs_genl_family
= {
2769 .id
= GENL_ID_GENERATE
,
2771 .name
= IPVS_GENL_NAME
,
2772 .version
= IPVS_GENL_VERSION
,
2773 .maxattr
= IPVS_CMD_MAX
,
2774 .netnsok
= true, /* Make ipvsadm to work on netns */
2777 /* Policy used for first-level command attributes */
2778 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2779 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2780 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2781 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2782 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2783 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2784 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2787 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2788 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2789 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2790 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2791 .len
= IP_VS_IFNAME_MAXLEN
},
2792 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2795 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2796 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2797 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2798 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2799 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2800 .len
= sizeof(union nf_inet_addr
) },
2801 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2802 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2803 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2804 .len
= IP_VS_SCHEDNAME_MAXLEN
},
2805 [IPVS_SVC_ATTR_PE_NAME
] = { .type
= NLA_NUL_STRING
,
2806 .len
= IP_VS_PENAME_MAXLEN
},
2807 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2808 .len
= sizeof(struct ip_vs_flags
) },
2809 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2810 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2811 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2814 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2815 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2816 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2817 .len
= sizeof(union nf_inet_addr
) },
2818 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2819 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2820 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2821 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2822 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2823 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2824 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2825 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2826 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2827 [IPVS_DEST_ATTR_ADDR_FAMILY
] = { .type
= NLA_U16
},
2830 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2831 struct ip_vs_kstats
*kstats
)
2833 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2838 if (nla_put_u32(skb
, IPVS_STATS_ATTR_CONNS
, (u32
)kstats
->conns
) ||
2839 nla_put_u32(skb
, IPVS_STATS_ATTR_INPKTS
, (u32
)kstats
->inpkts
) ||
2840 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPKTS
, (u32
)kstats
->outpkts
) ||
2841 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2842 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2843 nla_put_u32(skb
, IPVS_STATS_ATTR_CPS
, (u32
)kstats
->cps
) ||
2844 nla_put_u32(skb
, IPVS_STATS_ATTR_INPPS
, (u32
)kstats
->inpps
) ||
2845 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTPPS
, (u32
)kstats
->outpps
) ||
2846 nla_put_u32(skb
, IPVS_STATS_ATTR_INBPS
, (u32
)kstats
->inbps
) ||
2847 nla_put_u32(skb
, IPVS_STATS_ATTR_OUTBPS
, (u32
)kstats
->outbps
))
2848 goto nla_put_failure
;
2849 nla_nest_end(skb
, nl_stats
);
2854 nla_nest_cancel(skb
, nl_stats
);
2858 static int ip_vs_genl_fill_stats64(struct sk_buff
*skb
, int container_type
,
2859 struct ip_vs_kstats
*kstats
)
2861 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2866 if (nla_put_u64(skb
, IPVS_STATS_ATTR_CONNS
, kstats
->conns
) ||
2867 nla_put_u64(skb
, IPVS_STATS_ATTR_INPKTS
, kstats
->inpkts
) ||
2868 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPKTS
, kstats
->outpkts
) ||
2869 nla_put_u64(skb
, IPVS_STATS_ATTR_INBYTES
, kstats
->inbytes
) ||
2870 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBYTES
, kstats
->outbytes
) ||
2871 nla_put_u64(skb
, IPVS_STATS_ATTR_CPS
, kstats
->cps
) ||
2872 nla_put_u64(skb
, IPVS_STATS_ATTR_INPPS
, kstats
->inpps
) ||
2873 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTPPS
, kstats
->outpps
) ||
2874 nla_put_u64(skb
, IPVS_STATS_ATTR_INBPS
, kstats
->inbps
) ||
2875 nla_put_u64(skb
, IPVS_STATS_ATTR_OUTBPS
, kstats
->outbps
))
2876 goto nla_put_failure
;
2877 nla_nest_end(skb
, nl_stats
);
2882 nla_nest_cancel(skb
, nl_stats
);
2886 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2887 struct ip_vs_service
*svc
)
2889 struct ip_vs_scheduler
*sched
;
2890 struct ip_vs_pe
*pe
;
2891 struct nlattr
*nl_service
;
2892 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2894 struct ip_vs_kstats kstats
;
2896 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
2900 if (nla_put_u16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
))
2901 goto nla_put_failure
;
2903 if (nla_put_u32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
))
2904 goto nla_put_failure
;
2906 if (nla_put_u16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
) ||
2907 nla_put(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
) ||
2908 nla_put_be16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
))
2909 goto nla_put_failure
;
2912 sched
= rcu_dereference_protected(svc
->scheduler
, 1);
2913 pe
= rcu_dereference_protected(svc
->pe
, 1);
2914 if (nla_put_string(skb
, IPVS_SVC_ATTR_SCHED_NAME
, sched
->name
) ||
2915 (pe
&& nla_put_string(skb
, IPVS_SVC_ATTR_PE_NAME
, pe
->name
)) ||
2916 nla_put(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
) ||
2917 nla_put_u32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
) ||
2918 nla_put_be32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
))
2919 goto nla_put_failure
;
2920 ip_vs_copy_stats(&kstats
, &svc
->stats
);
2921 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &kstats
))
2922 goto nla_put_failure
;
2923 if (ip_vs_genl_fill_stats64(skb
, IPVS_SVC_ATTR_STATS64
, &kstats
))
2924 goto nla_put_failure
;
2926 nla_nest_end(skb
, nl_service
);
2931 nla_nest_cancel(skb
, nl_service
);
2935 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
2936 struct ip_vs_service
*svc
,
2937 struct netlink_callback
*cb
)
2941 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
2942 &ip_vs_genl_family
, NLM_F_MULTI
,
2943 IPVS_CMD_NEW_SERVICE
);
2947 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
2948 goto nla_put_failure
;
2950 genlmsg_end(skb
, hdr
);
2954 genlmsg_cancel(skb
, hdr
);
2958 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
2959 struct netlink_callback
*cb
)
2962 int start
= cb
->args
[0];
2963 struct ip_vs_service
*svc
;
2964 struct net
*net
= skb_sknet(skb
);
2966 mutex_lock(&__ip_vs_mutex
);
2967 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2968 hlist_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
2969 if (++idx
<= start
|| !net_eq(svc
->net
, net
))
2971 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2973 goto nla_put_failure
;
2978 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2979 hlist_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
2980 if (++idx
<= start
|| !net_eq(svc
->net
, net
))
2982 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2984 goto nla_put_failure
;
2990 mutex_unlock(&__ip_vs_mutex
);
2996 static int ip_vs_genl_parse_service(struct net
*net
,
2997 struct ip_vs_service_user_kern
*usvc
,
2998 struct nlattr
*nla
, int full_entry
,
2999 struct ip_vs_service
**ret_svc
)
3001 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
3002 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
3003 struct ip_vs_service
*svc
;
3005 /* Parse mandatory identifying service fields first */
3007 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
, ip_vs_svc_policy
))
3010 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
3011 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
3012 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
3013 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
3014 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
3016 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
3019 memset(usvc
, 0, sizeof(*usvc
));
3021 usvc
->af
= nla_get_u16(nla_af
);
3022 #ifdef CONFIG_IP_VS_IPV6
3023 if (usvc
->af
!= AF_INET
&& usvc
->af
!= AF_INET6
)
3025 if (usvc
->af
!= AF_INET
)
3027 return -EAFNOSUPPORT
;
3030 usvc
->protocol
= IPPROTO_TCP
;
3031 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
3033 usvc
->protocol
= nla_get_u16(nla_protocol
);
3034 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
3035 usvc
->port
= nla_get_be16(nla_port
);
3041 svc
= __ip_vs_svc_fwm_find(net
, usvc
->af
, usvc
->fwmark
);
3043 svc
= __ip_vs_service_find(net
, usvc
->af
, usvc
->protocol
,
3044 &usvc
->addr
, usvc
->port
);
3048 /* If a full entry was requested, check for the additional fields */
3050 struct nlattr
*nla_sched
, *nla_flags
, *nla_pe
, *nla_timeout
,
3052 struct ip_vs_flags flags
;
3054 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
3055 nla_pe
= attrs
[IPVS_SVC_ATTR_PE_NAME
];
3056 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
3057 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
3058 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
3060 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
3063 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
3065 /* prefill flags from service if it already exists */
3067 usvc
->flags
= svc
->flags
;
3069 /* set new flags from userland */
3070 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
3071 (flags
.flags
& flags
.mask
);
3072 usvc
->sched_name
= nla_data(nla_sched
);
3073 usvc
->pe_name
= nla_pe
? nla_data(nla_pe
) : NULL
;
3074 usvc
->timeout
= nla_get_u32(nla_timeout
);
3075 usvc
->netmask
= nla_get_be32(nla_netmask
);
3081 static struct ip_vs_service
*ip_vs_genl_find_service(struct net
*net
,
3084 struct ip_vs_service_user_kern usvc
;
3085 struct ip_vs_service
*svc
;
3088 ret
= ip_vs_genl_parse_service(net
, &usvc
, nla
, 0, &svc
);
3089 return ret
? ERR_PTR(ret
) : svc
;
3092 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
3094 struct nlattr
*nl_dest
;
3095 struct ip_vs_kstats kstats
;
3097 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
3101 if (nla_put(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
) ||
3102 nla_put_be16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
) ||
3103 nla_put_u32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
3104 (atomic_read(&dest
->conn_flags
) &
3105 IP_VS_CONN_F_FWD_MASK
)) ||
3106 nla_put_u32(skb
, IPVS_DEST_ATTR_WEIGHT
,
3107 atomic_read(&dest
->weight
)) ||
3108 nla_put_u32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
) ||
3109 nla_put_u32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
) ||
3110 nla_put_u32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
3111 atomic_read(&dest
->activeconns
)) ||
3112 nla_put_u32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
3113 atomic_read(&dest
->inactconns
)) ||
3114 nla_put_u32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
3115 atomic_read(&dest
->persistconns
)) ||
3116 nla_put_u16(skb
, IPVS_DEST_ATTR_ADDR_FAMILY
, dest
->af
))
3117 goto nla_put_failure
;
3118 ip_vs_copy_stats(&kstats
, &dest
->stats
);
3119 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &kstats
))
3120 goto nla_put_failure
;
3121 if (ip_vs_genl_fill_stats64(skb
, IPVS_DEST_ATTR_STATS64
, &kstats
))
3122 goto nla_put_failure
;
3124 nla_nest_end(skb
, nl_dest
);
3129 nla_nest_cancel(skb
, nl_dest
);
3133 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
3134 struct netlink_callback
*cb
)
3138 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3139 &ip_vs_genl_family
, NLM_F_MULTI
,
3144 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
3145 goto nla_put_failure
;
3147 genlmsg_end(skb
, hdr
);
3151 genlmsg_cancel(skb
, hdr
);
3155 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
3156 struct netlink_callback
*cb
)
3159 int start
= cb
->args
[0];
3160 struct ip_vs_service
*svc
;
3161 struct ip_vs_dest
*dest
;
3162 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
3163 struct net
*net
= skb_sknet(skb
);
3165 mutex_lock(&__ip_vs_mutex
);
3167 /* Try to find the service for which to dump destinations */
3168 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
,
3169 IPVS_CMD_ATTR_MAX
, ip_vs_cmd_policy
))
3173 svc
= ip_vs_genl_find_service(net
, attrs
[IPVS_CMD_ATTR_SERVICE
]);
3174 if (IS_ERR(svc
) || svc
== NULL
)
3177 /* Dump the destinations */
3178 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
3181 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
3183 goto nla_put_failure
;
3191 mutex_unlock(&__ip_vs_mutex
);
3196 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
3197 struct nlattr
*nla
, int full_entry
)
3199 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
3200 struct nlattr
*nla_addr
, *nla_port
;
3201 struct nlattr
*nla_addr_family
;
3203 /* Parse mandatory identifying destination fields first */
3205 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
, ip_vs_dest_policy
))
3208 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
3209 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
3210 nla_addr_family
= attrs
[IPVS_DEST_ATTR_ADDR_FAMILY
];
3212 if (!(nla_addr
&& nla_port
))
3215 memset(udest
, 0, sizeof(*udest
));
3217 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
3218 udest
->port
= nla_get_be16(nla_port
);
3220 if (nla_addr_family
)
3221 udest
->af
= nla_get_u16(nla_addr_family
);
3225 /* If a full entry was requested, check for the additional fields */
3227 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
3230 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
3231 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
3232 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
3233 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
3235 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
3238 udest
->conn_flags
= nla_get_u32(nla_fwd
)
3239 & IP_VS_CONN_F_FWD_MASK
;
3240 udest
->weight
= nla_get_u32(nla_weight
);
3241 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
3242 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
3248 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __u32 state
,
3249 const char *mcast_ifn
, __u32 syncid
)
3251 struct nlattr
*nl_daemon
;
3253 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
3257 if (nla_put_u32(skb
, IPVS_DAEMON_ATTR_STATE
, state
) ||
3258 nla_put_string(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, mcast_ifn
) ||
3259 nla_put_u32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, syncid
))
3260 goto nla_put_failure
;
3261 nla_nest_end(skb
, nl_daemon
);
3266 nla_nest_cancel(skb
, nl_daemon
);
3270 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __u32 state
,
3271 const char *mcast_ifn
, __u32 syncid
,
3272 struct netlink_callback
*cb
)
3275 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
3276 &ip_vs_genl_family
, NLM_F_MULTI
,
3277 IPVS_CMD_NEW_DAEMON
);
3281 if (ip_vs_genl_fill_daemon(skb
, state
, mcast_ifn
, syncid
))
3282 goto nla_put_failure
;
3284 genlmsg_end(skb
, hdr
);
3288 genlmsg_cancel(skb
, hdr
);
3292 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
3293 struct netlink_callback
*cb
)
3295 struct net
*net
= skb_sknet(skb
);
3296 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3298 mutex_lock(&ipvs
->sync_mutex
);
3299 if ((ipvs
->sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
3300 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
3301 ipvs
->master_mcast_ifn
,
3302 ipvs
->master_syncid
, cb
) < 0)
3303 goto nla_put_failure
;
3308 if ((ipvs
->sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
3309 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
3310 ipvs
->backup_mcast_ifn
,
3311 ipvs
->backup_syncid
, cb
) < 0)
3312 goto nla_put_failure
;
3318 mutex_unlock(&ipvs
->sync_mutex
);
3323 static int ip_vs_genl_new_daemon(struct net
*net
, struct nlattr
**attrs
)
3325 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
3326 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
3327 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3330 /* The synchronization protocol is incompatible with mixed family
3333 if (net_ipvs(net
)->mixed_address_family_dests
> 0)
3336 return start_sync_thread(net
,
3337 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]),
3338 nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3339 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]));
3342 static int ip_vs_genl_del_daemon(struct net
*net
, struct nlattr
**attrs
)
3344 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3347 return stop_sync_thread(net
,
3348 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3351 static int ip_vs_genl_set_config(struct net
*net
, struct nlattr
**attrs
)
3353 struct ip_vs_timeout_user t
;
3355 __ip_vs_get_timeouts(net
, &t
);
3357 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3358 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3360 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3362 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3364 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3365 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3367 return ip_vs_set_timeout(net
, &t
);
3370 static int ip_vs_genl_set_daemon(struct sk_buff
*skb
, struct genl_info
*info
)
3374 struct netns_ipvs
*ipvs
;
3376 net
= skb_sknet(skb
);
3377 ipvs
= net_ipvs(net
);
3378 cmd
= info
->genlhdr
->cmd
;
3380 if (cmd
== IPVS_CMD_NEW_DAEMON
|| cmd
== IPVS_CMD_DEL_DAEMON
) {
3381 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3383 mutex_lock(&ipvs
->sync_mutex
);
3384 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3385 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3386 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3387 ip_vs_daemon_policy
)) {
3392 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3393 ret
= ip_vs_genl_new_daemon(net
, daemon_attrs
);
3395 ret
= ip_vs_genl_del_daemon(net
, daemon_attrs
);
3397 mutex_unlock(&ipvs
->sync_mutex
);
3402 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3404 struct ip_vs_service
*svc
= NULL
;
3405 struct ip_vs_service_user_kern usvc
;
3406 struct ip_vs_dest_user_kern udest
;
3408 int need_full_svc
= 0, need_full_dest
= 0;
3411 net
= skb_sknet(skb
);
3412 cmd
= info
->genlhdr
->cmd
;
3414 mutex_lock(&__ip_vs_mutex
);
3416 if (cmd
== IPVS_CMD_FLUSH
) {
3417 ret
= ip_vs_flush(net
, false);
3419 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3420 ret
= ip_vs_genl_set_config(net
, info
->attrs
);
3422 } else if (cmd
== IPVS_CMD_ZERO
&&
3423 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3424 ret
= ip_vs_zero_all(net
);
3428 /* All following commands require a service argument, so check if we
3429 * received a valid one. We need a full service specification when
3430 * adding / editing a service. Only identifying members otherwise. */
3431 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3434 ret
= ip_vs_genl_parse_service(net
, &usvc
,
3435 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3436 need_full_svc
, &svc
);
3440 /* Unless we're adding a new service, the service must already exist */
3441 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3446 /* Destination commands require a valid destination argument. For
3447 * adding / editing a destination, we need a full destination
3449 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3450 cmd
== IPVS_CMD_DEL_DEST
) {
3451 if (cmd
!= IPVS_CMD_DEL_DEST
)
3454 ret
= ip_vs_genl_parse_dest(&udest
,
3455 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3460 /* Old protocols did not allow the user to specify address
3461 * family, so we set it to zero instead. We also didn't
3462 * allow heterogeneous pools in the old code, so it's safe
3463 * to assume that this will have the same address family as
3469 if (udest
.af
!= svc
->af
&& cmd
!= IPVS_CMD_DEL_DEST
) {
3470 /* The synchronization protocol is incompatible
3471 * with mixed family services
3473 if (net_ipvs(net
)->sync_state
) {
3478 /* Which connection types do we support? */
3479 switch (udest
.conn_flags
) {
3480 case IP_VS_CONN_F_TUNNEL
:
3481 /* We are able to forward this */
3491 case IPVS_CMD_NEW_SERVICE
:
3493 ret
= ip_vs_add_service(net
, &usvc
, &svc
);
3497 case IPVS_CMD_SET_SERVICE
:
3498 ret
= ip_vs_edit_service(svc
, &usvc
);
3500 case IPVS_CMD_DEL_SERVICE
:
3501 ret
= ip_vs_del_service(svc
);
3502 /* do not use svc, it can be freed */
3504 case IPVS_CMD_NEW_DEST
:
3505 ret
= ip_vs_add_dest(svc
, &udest
);
3507 case IPVS_CMD_SET_DEST
:
3508 ret
= ip_vs_edit_dest(svc
, &udest
);
3510 case IPVS_CMD_DEL_DEST
:
3511 ret
= ip_vs_del_dest(svc
, &udest
);
3514 ret
= ip_vs_zero_service(svc
);
3521 mutex_unlock(&__ip_vs_mutex
);
3526 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3528 struct sk_buff
*msg
;
3530 int ret
, cmd
, reply_cmd
;
3533 net
= skb_sknet(skb
);
3534 cmd
= info
->genlhdr
->cmd
;
3536 if (cmd
== IPVS_CMD_GET_SERVICE
)
3537 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3538 else if (cmd
== IPVS_CMD_GET_INFO
)
3539 reply_cmd
= IPVS_CMD_SET_INFO
;
3540 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3541 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3543 pr_err("unknown Generic Netlink command\n");
3547 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3551 mutex_lock(&__ip_vs_mutex
);
3553 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3555 goto nla_put_failure
;
3558 case IPVS_CMD_GET_SERVICE
:
3560 struct ip_vs_service
*svc
;
3562 svc
= ip_vs_genl_find_service(net
,
3563 info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3568 ret
= ip_vs_genl_fill_service(msg
, svc
);
3570 goto nla_put_failure
;
3579 case IPVS_CMD_GET_CONFIG
:
3581 struct ip_vs_timeout_user t
;
3583 __ip_vs_get_timeouts(net
, &t
);
3584 #ifdef CONFIG_IP_VS_PROTO_TCP
3585 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
,
3587 nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3589 goto nla_put_failure
;
3591 #ifdef CONFIG_IP_VS_PROTO_UDP
3592 if (nla_put_u32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
))
3593 goto nla_put_failure
;
3599 case IPVS_CMD_GET_INFO
:
3600 if (nla_put_u32(msg
, IPVS_INFO_ATTR_VERSION
,
3601 IP_VS_VERSION_CODE
) ||
3602 nla_put_u32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3603 ip_vs_conn_tab_size
))
3604 goto nla_put_failure
;
3608 genlmsg_end(msg
, reply
);
3609 ret
= genlmsg_reply(msg
, info
);
3613 pr_err("not enough space in Netlink message\n");
3619 mutex_unlock(&__ip_vs_mutex
);
3625 static const struct genl_ops ip_vs_genl_ops
[] = {
3627 .cmd
= IPVS_CMD_NEW_SERVICE
,
3628 .flags
= GENL_ADMIN_PERM
,
3629 .policy
= ip_vs_cmd_policy
,
3630 .doit
= ip_vs_genl_set_cmd
,
3633 .cmd
= IPVS_CMD_SET_SERVICE
,
3634 .flags
= GENL_ADMIN_PERM
,
3635 .policy
= ip_vs_cmd_policy
,
3636 .doit
= ip_vs_genl_set_cmd
,
3639 .cmd
= IPVS_CMD_DEL_SERVICE
,
3640 .flags
= GENL_ADMIN_PERM
,
3641 .policy
= ip_vs_cmd_policy
,
3642 .doit
= ip_vs_genl_set_cmd
,
3645 .cmd
= IPVS_CMD_GET_SERVICE
,
3646 .flags
= GENL_ADMIN_PERM
,
3647 .doit
= ip_vs_genl_get_cmd
,
3648 .dumpit
= ip_vs_genl_dump_services
,
3649 .policy
= ip_vs_cmd_policy
,
3652 .cmd
= IPVS_CMD_NEW_DEST
,
3653 .flags
= GENL_ADMIN_PERM
,
3654 .policy
= ip_vs_cmd_policy
,
3655 .doit
= ip_vs_genl_set_cmd
,
3658 .cmd
= IPVS_CMD_SET_DEST
,
3659 .flags
= GENL_ADMIN_PERM
,
3660 .policy
= ip_vs_cmd_policy
,
3661 .doit
= ip_vs_genl_set_cmd
,
3664 .cmd
= IPVS_CMD_DEL_DEST
,
3665 .flags
= GENL_ADMIN_PERM
,
3666 .policy
= ip_vs_cmd_policy
,
3667 .doit
= ip_vs_genl_set_cmd
,
3670 .cmd
= IPVS_CMD_GET_DEST
,
3671 .flags
= GENL_ADMIN_PERM
,
3672 .policy
= ip_vs_cmd_policy
,
3673 .dumpit
= ip_vs_genl_dump_dests
,
3676 .cmd
= IPVS_CMD_NEW_DAEMON
,
3677 .flags
= GENL_ADMIN_PERM
,
3678 .policy
= ip_vs_cmd_policy
,
3679 .doit
= ip_vs_genl_set_daemon
,
3682 .cmd
= IPVS_CMD_DEL_DAEMON
,
3683 .flags
= GENL_ADMIN_PERM
,
3684 .policy
= ip_vs_cmd_policy
,
3685 .doit
= ip_vs_genl_set_daemon
,
3688 .cmd
= IPVS_CMD_GET_DAEMON
,
3689 .flags
= GENL_ADMIN_PERM
,
3690 .dumpit
= ip_vs_genl_dump_daemons
,
3693 .cmd
= IPVS_CMD_SET_CONFIG
,
3694 .flags
= GENL_ADMIN_PERM
,
3695 .policy
= ip_vs_cmd_policy
,
3696 .doit
= ip_vs_genl_set_cmd
,
3699 .cmd
= IPVS_CMD_GET_CONFIG
,
3700 .flags
= GENL_ADMIN_PERM
,
3701 .doit
= ip_vs_genl_get_cmd
,
3704 .cmd
= IPVS_CMD_GET_INFO
,
3705 .flags
= GENL_ADMIN_PERM
,
3706 .doit
= ip_vs_genl_get_cmd
,
3709 .cmd
= IPVS_CMD_ZERO
,
3710 .flags
= GENL_ADMIN_PERM
,
3711 .policy
= ip_vs_cmd_policy
,
3712 .doit
= ip_vs_genl_set_cmd
,
3715 .cmd
= IPVS_CMD_FLUSH
,
3716 .flags
= GENL_ADMIN_PERM
,
3717 .doit
= ip_vs_genl_set_cmd
,
3721 static int __init
ip_vs_genl_register(void)
3723 return genl_register_family_with_ops(&ip_vs_genl_family
,
3727 static void ip_vs_genl_unregister(void)
3729 genl_unregister_family(&ip_vs_genl_family
);
3732 /* End of Generic Netlink interface definitions */
3735 * per netns intit/exit func.
3737 #ifdef CONFIG_SYSCTL
3738 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
)
3741 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3742 struct ctl_table
*tbl
;
3744 atomic_set(&ipvs
->dropentry
, 0);
3745 spin_lock_init(&ipvs
->dropentry_lock
);
3746 spin_lock_init(&ipvs
->droppacket_lock
);
3747 spin_lock_init(&ipvs
->securetcp_lock
);
3749 if (!net_eq(net
, &init_net
)) {
3750 tbl
= kmemdup(vs_vars
, sizeof(vs_vars
), GFP_KERNEL
);
3754 /* Don't export sysctls to unprivileged users */
3755 if (net
->user_ns
!= &init_user_ns
)
3756 tbl
[0].procname
= NULL
;
3759 /* Initialize sysctl defaults */
3761 ipvs
->sysctl_amemthresh
= 1024;
3762 tbl
[idx
++].data
= &ipvs
->sysctl_amemthresh
;
3763 ipvs
->sysctl_am_droprate
= 10;
3764 tbl
[idx
++].data
= &ipvs
->sysctl_am_droprate
;
3765 tbl
[idx
++].data
= &ipvs
->sysctl_drop_entry
;
3766 tbl
[idx
++].data
= &ipvs
->sysctl_drop_packet
;
3767 #ifdef CONFIG_IP_VS_NFCT
3768 tbl
[idx
++].data
= &ipvs
->sysctl_conntrack
;
3770 tbl
[idx
++].data
= &ipvs
->sysctl_secure_tcp
;
3771 ipvs
->sysctl_snat_reroute
= 1;
3772 tbl
[idx
++].data
= &ipvs
->sysctl_snat_reroute
;
3773 ipvs
->sysctl_sync_ver
= 1;
3774 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ver
;
3775 ipvs
->sysctl_sync_ports
= 1;
3776 tbl
[idx
++].data
= &ipvs
->sysctl_sync_ports
;
3777 tbl
[idx
++].data
= &ipvs
->sysctl_sync_persist_mode
;
3778 ipvs
->sysctl_sync_qlen_max
= nr_free_buffer_pages() / 32;
3779 tbl
[idx
++].data
= &ipvs
->sysctl_sync_qlen_max
;
3780 ipvs
->sysctl_sync_sock_size
= 0;
3781 tbl
[idx
++].data
= &ipvs
->sysctl_sync_sock_size
;
3782 tbl
[idx
++].data
= &ipvs
->sysctl_cache_bypass
;
3783 tbl
[idx
++].data
= &ipvs
->sysctl_expire_nodest_conn
;
3784 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_tcp
;
3785 tbl
[idx
++].data
= &ipvs
->sysctl_sloppy_sctp
;
3786 tbl
[idx
++].data
= &ipvs
->sysctl_expire_quiescent_template
;
3787 ipvs
->sysctl_sync_threshold
[0] = DEFAULT_SYNC_THRESHOLD
;
3788 ipvs
->sysctl_sync_threshold
[1] = DEFAULT_SYNC_PERIOD
;
3789 tbl
[idx
].data
= &ipvs
->sysctl_sync_threshold
;
3790 tbl
[idx
++].maxlen
= sizeof(ipvs
->sysctl_sync_threshold
);
3791 ipvs
->sysctl_sync_refresh_period
= DEFAULT_SYNC_REFRESH_PERIOD
;
3792 tbl
[idx
++].data
= &ipvs
->sysctl_sync_refresh_period
;
3793 ipvs
->sysctl_sync_retries
= clamp_t(int, DEFAULT_SYNC_RETRIES
, 0, 3);
3794 tbl
[idx
++].data
= &ipvs
->sysctl_sync_retries
;
3795 tbl
[idx
++].data
= &ipvs
->sysctl_nat_icmp_send
;
3796 ipvs
->sysctl_pmtu_disc
= 1;
3797 tbl
[idx
++].data
= &ipvs
->sysctl_pmtu_disc
;
3798 tbl
[idx
++].data
= &ipvs
->sysctl_backup_only
;
3799 ipvs
->sysctl_conn_reuse_mode
= 1;
3800 tbl
[idx
++].data
= &ipvs
->sysctl_conn_reuse_mode
;
3803 ipvs
->sysctl_hdr
= register_net_sysctl(net
, "net/ipv4/vs", tbl
);
3804 if (ipvs
->sysctl_hdr
== NULL
) {
3805 if (!net_eq(net
, &init_net
))
3809 ip_vs_start_estimator(net
, &ipvs
->tot_stats
);
3810 ipvs
->sysctl_tbl
= tbl
;
3811 /* Schedule defense work */
3812 INIT_DELAYED_WORK(&ipvs
->defense_work
, defense_work_handler
);
3813 schedule_delayed_work(&ipvs
->defense_work
, DEFENSE_TIMER_PERIOD
);
3818 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
)
3820 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3822 cancel_delayed_work_sync(&ipvs
->defense_work
);
3823 cancel_work_sync(&ipvs
->defense_work
.work
);
3824 unregister_net_sysctl_table(ipvs
->sysctl_hdr
);
3825 ip_vs_stop_estimator(net
, &ipvs
->tot_stats
);
3827 if (!net_eq(net
, &init_net
))
3828 kfree(ipvs
->sysctl_tbl
);
3833 static int __net_init
ip_vs_control_net_init_sysctl(struct net
*net
) { return 0; }
3834 static void __net_exit
ip_vs_control_net_cleanup_sysctl(struct net
*net
) { }
3838 static struct notifier_block ip_vs_dst_notifier
= {
3839 .notifier_call
= ip_vs_dst_event
,
3842 int __net_init
ip_vs_control_net_init(struct net
*net
)
3845 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3847 /* Initialize rs_table */
3848 for (idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++)
3849 INIT_HLIST_HEAD(&ipvs
->rs_table
[idx
]);
3851 INIT_LIST_HEAD(&ipvs
->dest_trash
);
3852 spin_lock_init(&ipvs
->dest_trash_lock
);
3853 setup_timer(&ipvs
->dest_trash_timer
, ip_vs_dest_trash_expire
,
3854 (unsigned long) net
);
3855 atomic_set(&ipvs
->ftpsvc_counter
, 0);
3856 atomic_set(&ipvs
->nullsvc_counter
, 0);
3859 ipvs
->tot_stats
.cpustats
= alloc_percpu(struct ip_vs_cpu_stats
);
3860 if (!ipvs
->tot_stats
.cpustats
)
3863 for_each_possible_cpu(i
) {
3864 struct ip_vs_cpu_stats
*ipvs_tot_stats
;
3865 ipvs_tot_stats
= per_cpu_ptr(ipvs
->tot_stats
.cpustats
, i
);
3866 u64_stats_init(&ipvs_tot_stats
->syncp
);
3869 spin_lock_init(&ipvs
->tot_stats
.lock
);
3871 proc_create("ip_vs", 0, net
->proc_net
, &ip_vs_info_fops
);
3872 proc_create("ip_vs_stats", 0, net
->proc_net
, &ip_vs_stats_fops
);
3873 proc_create("ip_vs_stats_percpu", 0, net
->proc_net
,
3874 &ip_vs_stats_percpu_fops
);
3876 if (ip_vs_control_net_init_sysctl(net
))
3882 free_percpu(ipvs
->tot_stats
.cpustats
);
3886 void __net_exit
ip_vs_control_net_cleanup(struct net
*net
)
3888 struct netns_ipvs
*ipvs
= net_ipvs(net
);
3890 ip_vs_trash_cleanup(net
);
3891 ip_vs_control_net_cleanup_sysctl(net
);
3892 remove_proc_entry("ip_vs_stats_percpu", net
->proc_net
);
3893 remove_proc_entry("ip_vs_stats", net
->proc_net
);
3894 remove_proc_entry("ip_vs", net
->proc_net
);
3895 free_percpu(ipvs
->tot_stats
.cpustats
);
3898 int __init
ip_vs_register_nl_ioctl(void)
3902 ret
= nf_register_sockopt(&ip_vs_sockopts
);
3904 pr_err("cannot register sockopt.\n");
3908 ret
= ip_vs_genl_register();
3910 pr_err("cannot register Generic Netlink interface.\n");
3916 nf_unregister_sockopt(&ip_vs_sockopts
);
3921 void ip_vs_unregister_nl_ioctl(void)
3923 ip_vs_genl_unregister();
3924 nf_unregister_sockopt(&ip_vs_sockopts
);
3927 int __init
ip_vs_control_init(void)
3934 /* Initialize svc_table, ip_vs_svc_fwm_table */
3935 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
3936 INIT_HLIST_HEAD(&ip_vs_svc_table
[idx
]);
3937 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
3940 smp_wmb(); /* Do we really need it now ? */
3942 ret
= register_netdevice_notifier(&ip_vs_dst_notifier
);
3951 void ip_vs_control_cleanup(void)
3954 unregister_netdevice_notifier(&ip_vs_dst_notifier
);