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>
42 #ifdef CONFIG_IP_VS_IPV6
44 #include <net/ip6_route.h>
46 #include <net/route.h>
48 #include <net/genetlink.h>
50 #include <asm/uaccess.h>
52 #include <net/ip_vs.h>
54 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
55 static DEFINE_MUTEX(__ip_vs_mutex
);
57 /* lock for service table */
58 static DEFINE_RWLOCK(__ip_vs_svc_lock
);
60 /* lock for table with the real services */
61 static DEFINE_RWLOCK(__ip_vs_rs_lock
);
63 /* lock for state and timeout tables */
64 static DEFINE_SPINLOCK(ip_vs_securetcp_lock
);
66 /* lock for drop entry handling */
67 static DEFINE_SPINLOCK(__ip_vs_dropentry_lock
);
69 /* lock for drop packet handling */
70 static DEFINE_SPINLOCK(__ip_vs_droppacket_lock
);
72 /* 1/rate drop and drop-entry variables */
73 int ip_vs_drop_rate
= 0;
74 int ip_vs_drop_counter
= 0;
75 static atomic_t ip_vs_dropentry
= ATOMIC_INIT(0);
77 /* number of virtual services */
78 static int ip_vs_num_services
= 0;
80 /* sysctl variables */
81 static int sysctl_ip_vs_drop_entry
= 0;
82 static int sysctl_ip_vs_drop_packet
= 0;
83 static int sysctl_ip_vs_secure_tcp
= 0;
84 static int sysctl_ip_vs_amemthresh
= 1024;
85 static int sysctl_ip_vs_am_droprate
= 10;
86 int sysctl_ip_vs_cache_bypass
= 0;
87 int sysctl_ip_vs_expire_nodest_conn
= 0;
88 int sysctl_ip_vs_expire_quiescent_template
= 0;
89 int sysctl_ip_vs_sync_threshold
[2] = { 3, 50 };
90 int sysctl_ip_vs_nat_icmp_send
= 0;
93 #ifdef CONFIG_IP_VS_DEBUG
94 static int sysctl_ip_vs_debug_level
= 0;
96 int ip_vs_get_debug_level(void)
98 return sysctl_ip_vs_debug_level
;
102 #ifdef CONFIG_IP_VS_IPV6
103 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
104 static int __ip_vs_addr_is_local_v6(const struct in6_addr
*addr
)
112 .saddr
= { .s6_addr32
= {0, 0, 0, 0} }, } },
115 rt
= (struct rt6_info
*)ip6_route_output(&init_net
, NULL
, &fl
);
116 if (rt
&& rt
->rt6i_dev
&& (rt
->rt6i_dev
->flags
& IFF_LOOPBACK
))
123 * update_defense_level is called from keventd and from sysctl,
124 * so it needs to protect itself from softirqs
126 static void update_defense_level(void)
129 static int old_secure_tcp
= 0;
134 /* we only count free and buffered memory (in pages) */
136 availmem
= i
.freeram
+ i
.bufferram
;
137 /* however in linux 2.5 the i.bufferram is total page cache size,
139 /* si_swapinfo(&i); */
140 /* availmem = availmem - (i.totalswap - i.freeswap); */
142 nomem
= (availmem
< sysctl_ip_vs_amemthresh
);
147 spin_lock(&__ip_vs_dropentry_lock
);
148 switch (sysctl_ip_vs_drop_entry
) {
150 atomic_set(&ip_vs_dropentry
, 0);
154 atomic_set(&ip_vs_dropentry
, 1);
155 sysctl_ip_vs_drop_entry
= 2;
157 atomic_set(&ip_vs_dropentry
, 0);
162 atomic_set(&ip_vs_dropentry
, 1);
164 atomic_set(&ip_vs_dropentry
, 0);
165 sysctl_ip_vs_drop_entry
= 1;
169 atomic_set(&ip_vs_dropentry
, 1);
172 spin_unlock(&__ip_vs_dropentry_lock
);
175 spin_lock(&__ip_vs_droppacket_lock
);
176 switch (sysctl_ip_vs_drop_packet
) {
182 ip_vs_drop_rate
= ip_vs_drop_counter
183 = sysctl_ip_vs_amemthresh
/
184 (sysctl_ip_vs_amemthresh
-availmem
);
185 sysctl_ip_vs_drop_packet
= 2;
192 ip_vs_drop_rate
= ip_vs_drop_counter
193 = sysctl_ip_vs_amemthresh
/
194 (sysctl_ip_vs_amemthresh
-availmem
);
197 sysctl_ip_vs_drop_packet
= 1;
201 ip_vs_drop_rate
= sysctl_ip_vs_am_droprate
;
204 spin_unlock(&__ip_vs_droppacket_lock
);
207 spin_lock(&ip_vs_securetcp_lock
);
208 switch (sysctl_ip_vs_secure_tcp
) {
210 if (old_secure_tcp
>= 2)
215 if (old_secure_tcp
< 2)
217 sysctl_ip_vs_secure_tcp
= 2;
219 if (old_secure_tcp
>= 2)
225 if (old_secure_tcp
< 2)
228 if (old_secure_tcp
>= 2)
230 sysctl_ip_vs_secure_tcp
= 1;
234 if (old_secure_tcp
< 2)
238 old_secure_tcp
= sysctl_ip_vs_secure_tcp
;
240 ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp
>1);
241 spin_unlock(&ip_vs_securetcp_lock
);
248 * Timer for checking the defense
250 #define DEFENSE_TIMER_PERIOD 1*HZ
251 static void defense_work_handler(struct work_struct
*work
);
252 static DECLARE_DELAYED_WORK(defense_work
, defense_work_handler
);
254 static void defense_work_handler(struct work_struct
*work
)
256 update_defense_level();
257 if (atomic_read(&ip_vs_dropentry
))
258 ip_vs_random_dropentry();
260 schedule_delayed_work(&defense_work
, DEFENSE_TIMER_PERIOD
);
264 ip_vs_use_count_inc(void)
266 return try_module_get(THIS_MODULE
);
270 ip_vs_use_count_dec(void)
272 module_put(THIS_MODULE
);
277 * Hash table: for virtual service lookups
279 #define IP_VS_SVC_TAB_BITS 8
280 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
281 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
283 /* the service table hashed by <protocol, addr, port> */
284 static struct list_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
285 /* the service table hashed by fwmark */
286 static struct list_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
289 * Hash table: for real service lookups
291 #define IP_VS_RTAB_BITS 4
292 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
293 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
295 static struct list_head ip_vs_rtable
[IP_VS_RTAB_SIZE
];
298 * Trash for destinations
300 static LIST_HEAD(ip_vs_dest_trash
);
303 * FTP & NULL virtual service counters
305 static atomic_t ip_vs_ftpsvc_counter
= ATOMIC_INIT(0);
306 static atomic_t ip_vs_nullsvc_counter
= ATOMIC_INIT(0);
310 * Returns hash value for virtual service
312 static __inline__
unsigned
313 ip_vs_svc_hashkey(int af
, unsigned proto
, const union nf_inet_addr
*addr
,
316 register unsigned porth
= ntohs(port
);
317 __be32 addr_fold
= addr
->ip
;
319 #ifdef CONFIG_IP_VS_IPV6
321 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
322 addr
->ip6
[2]^addr
->ip6
[3];
325 return (proto
^ntohl(addr_fold
)^(porth
>>IP_VS_SVC_TAB_BITS
)^porth
)
326 & IP_VS_SVC_TAB_MASK
;
330 * Returns hash value of fwmark for virtual service lookup
332 static __inline__
unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark
)
334 return fwmark
& IP_VS_SVC_TAB_MASK
;
338 * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
339 * or in the ip_vs_svc_fwm_table by fwmark.
340 * Should be called with locked tables.
342 static int ip_vs_svc_hash(struct ip_vs_service
*svc
)
346 if (svc
->flags
& IP_VS_SVC_F_HASHED
) {
347 pr_err("%s(): request for already hashed, called from %pF\n",
348 __func__
, __builtin_return_address(0));
352 if (svc
->fwmark
== 0) {
354 * Hash it by <protocol,addr,port> in ip_vs_svc_table
356 hash
= ip_vs_svc_hashkey(svc
->af
, svc
->protocol
, &svc
->addr
,
358 list_add(&svc
->s_list
, &ip_vs_svc_table
[hash
]);
361 * Hash it by fwmark in ip_vs_svc_fwm_table
363 hash
= ip_vs_svc_fwm_hashkey(svc
->fwmark
);
364 list_add(&svc
->f_list
, &ip_vs_svc_fwm_table
[hash
]);
367 svc
->flags
|= IP_VS_SVC_F_HASHED
;
368 /* increase its refcnt because it is referenced by the svc table */
369 atomic_inc(&svc
->refcnt
);
375 * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
376 * Should be called with locked tables.
378 static int ip_vs_svc_unhash(struct ip_vs_service
*svc
)
380 if (!(svc
->flags
& IP_VS_SVC_F_HASHED
)) {
381 pr_err("%s(): request for unhash flagged, called from %pF\n",
382 __func__
, __builtin_return_address(0));
386 if (svc
->fwmark
== 0) {
387 /* Remove it from the ip_vs_svc_table table */
388 list_del(&svc
->s_list
);
390 /* Remove it from the ip_vs_svc_fwm_table table */
391 list_del(&svc
->f_list
);
394 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
395 atomic_dec(&svc
->refcnt
);
401 * Get service by {proto,addr,port} in the service table.
403 static inline struct ip_vs_service
*
404 __ip_vs_service_get(int af
, __u16 protocol
, const union nf_inet_addr
*vaddr
,
408 struct ip_vs_service
*svc
;
410 /* Check for "full" addressed entries */
411 hash
= ip_vs_svc_hashkey(af
, protocol
, vaddr
, vport
);
413 list_for_each_entry(svc
, &ip_vs_svc_table
[hash
], s_list
){
415 && ip_vs_addr_equal(af
, &svc
->addr
, vaddr
)
416 && (svc
->port
== vport
)
417 && (svc
->protocol
== protocol
)) {
419 atomic_inc(&svc
->usecnt
);
429 * Get service by {fwmark} in the service table.
431 static inline struct ip_vs_service
*
432 __ip_vs_svc_fwm_get(int af
, __u32 fwmark
)
435 struct ip_vs_service
*svc
;
437 /* Check for fwmark addressed entries */
438 hash
= ip_vs_svc_fwm_hashkey(fwmark
);
440 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[hash
], f_list
) {
441 if (svc
->fwmark
== fwmark
&& svc
->af
== af
) {
443 atomic_inc(&svc
->usecnt
);
451 struct ip_vs_service
*
452 ip_vs_service_get(int af
, __u32 fwmark
, __u16 protocol
,
453 const union nf_inet_addr
*vaddr
, __be16 vport
)
455 struct ip_vs_service
*svc
;
457 read_lock(&__ip_vs_svc_lock
);
460 * Check the table hashed by fwmark first
462 if (fwmark
&& (svc
= __ip_vs_svc_fwm_get(af
, fwmark
)))
466 * Check the table hashed by <protocol,addr,port>
467 * for "full" addressed entries
469 svc
= __ip_vs_service_get(af
, protocol
, vaddr
, vport
);
472 && protocol
== IPPROTO_TCP
473 && atomic_read(&ip_vs_ftpsvc_counter
)
474 && (vport
== FTPDATA
|| ntohs(vport
) >= PROT_SOCK
)) {
476 * Check if ftp service entry exists, the packet
477 * might belong to FTP data connections.
479 svc
= __ip_vs_service_get(af
, protocol
, vaddr
, FTPPORT
);
483 && atomic_read(&ip_vs_nullsvc_counter
)) {
485 * Check if the catch-all port (port zero) exists
487 svc
= __ip_vs_service_get(af
, protocol
, vaddr
, 0);
491 read_unlock(&__ip_vs_svc_lock
);
493 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
494 fwmark
, ip_vs_proto_name(protocol
),
495 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
496 svc
? "hit" : "not hit");
503 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
505 atomic_inc(&svc
->refcnt
);
510 __ip_vs_unbind_svc(struct ip_vs_dest
*dest
)
512 struct ip_vs_service
*svc
= dest
->svc
;
515 if (atomic_dec_and_test(&svc
->refcnt
))
521 * Returns hash value for real service
523 static inline unsigned ip_vs_rs_hashkey(int af
,
524 const union nf_inet_addr
*addr
,
527 register unsigned porth
= ntohs(port
);
528 __be32 addr_fold
= addr
->ip
;
530 #ifdef CONFIG_IP_VS_IPV6
532 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
533 addr
->ip6
[2]^addr
->ip6
[3];
536 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
541 * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
542 * should be called with locked tables.
544 static int ip_vs_rs_hash(struct ip_vs_dest
*dest
)
548 if (!list_empty(&dest
->d_list
)) {
553 * Hash by proto,addr,port,
554 * which are the parameters of the real service.
556 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
558 list_add(&dest
->d_list
, &ip_vs_rtable
[hash
]);
564 * UNhashes ip_vs_dest from ip_vs_rtable.
565 * should be called with locked tables.
567 static int ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
570 * Remove it from the ip_vs_rtable table.
572 if (!list_empty(&dest
->d_list
)) {
573 list_del(&dest
->d_list
);
574 INIT_LIST_HEAD(&dest
->d_list
);
581 * Lookup real service by <proto,addr,port> in the real service table.
584 ip_vs_lookup_real_service(int af
, __u16 protocol
,
585 const union nf_inet_addr
*daddr
,
589 struct ip_vs_dest
*dest
;
592 * Check for "full" addressed entries
593 * Return the first found entry
595 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
597 read_lock(&__ip_vs_rs_lock
);
598 list_for_each_entry(dest
, &ip_vs_rtable
[hash
], d_list
) {
600 && ip_vs_addr_equal(af
, &dest
->addr
, daddr
)
601 && (dest
->port
== dport
)
602 && ((dest
->protocol
== protocol
) ||
605 read_unlock(&__ip_vs_rs_lock
);
609 read_unlock(&__ip_vs_rs_lock
);
615 * Lookup destination by {addr,port} in the given service
617 static struct ip_vs_dest
*
618 ip_vs_lookup_dest(struct ip_vs_service
*svc
, const union nf_inet_addr
*daddr
,
621 struct ip_vs_dest
*dest
;
624 * Find the destination for the given service
626 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
627 if ((dest
->af
== svc
->af
)
628 && ip_vs_addr_equal(svc
->af
, &dest
->addr
, daddr
)
629 && (dest
->port
== dport
)) {
639 * Find destination by {daddr,dport,vaddr,protocol}
640 * Cretaed to be used in ip_vs_process_message() in
641 * the backup synchronization daemon. It finds the
642 * destination to be bound to the received connection
645 * ip_vs_lookup_real_service() looked promissing, but
646 * seems not working as expected.
648 struct ip_vs_dest
*ip_vs_find_dest(int af
, const union nf_inet_addr
*daddr
,
650 const union nf_inet_addr
*vaddr
,
651 __be16 vport
, __u16 protocol
)
653 struct ip_vs_dest
*dest
;
654 struct ip_vs_service
*svc
;
656 svc
= ip_vs_service_get(af
, 0, protocol
, vaddr
, vport
);
659 dest
= ip_vs_lookup_dest(svc
, daddr
, dport
);
661 atomic_inc(&dest
->refcnt
);
662 ip_vs_service_put(svc
);
667 * Lookup dest by {svc,addr,port} in the destination trash.
668 * The destination trash is used to hold the destinations that are removed
669 * from the service table but are still referenced by some conn entries.
670 * The reason to add the destination trash is when the dest is temporary
671 * down (either by administrator or by monitor program), the dest can be
672 * picked back from the trash, the remaining connections to the dest can
673 * continue, and the counting information of the dest is also useful for
676 static struct ip_vs_dest
*
677 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, const union nf_inet_addr
*daddr
,
680 struct ip_vs_dest
*dest
, *nxt
;
683 * Find the destination in trash
685 list_for_each_entry_safe(dest
, nxt
, &ip_vs_dest_trash
, n_list
) {
686 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
689 IP_VS_DBG_ADDR(svc
->af
, &dest
->addr
),
691 atomic_read(&dest
->refcnt
));
692 if (dest
->af
== svc
->af
&&
693 ip_vs_addr_equal(svc
->af
, &dest
->addr
, daddr
) &&
694 dest
->port
== dport
&&
695 dest
->vfwmark
== svc
->fwmark
&&
696 dest
->protocol
== svc
->protocol
&&
698 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
699 dest
->vport
== svc
->port
))) {
705 * Try to purge the destination from trash if not referenced
707 if (atomic_read(&dest
->refcnt
) == 1) {
708 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
711 IP_VS_DBG_ADDR(svc
->af
, &dest
->addr
),
713 list_del(&dest
->n_list
);
714 ip_vs_dst_reset(dest
);
715 __ip_vs_unbind_svc(dest
);
725 * Clean up all the destinations in the trash
726 * Called by the ip_vs_control_cleanup()
728 * When the ip_vs_control_clearup is activated by ipvs module exit,
729 * the service tables must have been flushed and all the connections
730 * are expired, and the refcnt of each destination in the trash must
731 * be 1, so we simply release them here.
733 static void ip_vs_trash_cleanup(void)
735 struct ip_vs_dest
*dest
, *nxt
;
737 list_for_each_entry_safe(dest
, nxt
, &ip_vs_dest_trash
, n_list
) {
738 list_del(&dest
->n_list
);
739 ip_vs_dst_reset(dest
);
740 __ip_vs_unbind_svc(dest
);
747 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
749 spin_lock_bh(&stats
->lock
);
751 memset(&stats
->ustats
, 0, sizeof(stats
->ustats
));
752 ip_vs_zero_estimator(stats
);
754 spin_unlock_bh(&stats
->lock
);
758 * Update a destination in the given service
761 __ip_vs_update_dest(struct ip_vs_service
*svc
,
762 struct ip_vs_dest
*dest
, struct ip_vs_dest_user_kern
*udest
)
766 /* set the weight and the flags */
767 atomic_set(&dest
->weight
, udest
->weight
);
768 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
769 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
771 /* check if local node and update the flags */
772 #ifdef CONFIG_IP_VS_IPV6
773 if (svc
->af
== AF_INET6
) {
774 if (__ip_vs_addr_is_local_v6(&udest
->addr
.in6
)) {
775 conn_flags
= (conn_flags
& ~IP_VS_CONN_F_FWD_MASK
)
776 | IP_VS_CONN_F_LOCALNODE
;
780 if (inet_addr_type(&init_net
, udest
->addr
.ip
) == RTN_LOCAL
) {
781 conn_flags
= (conn_flags
& ~IP_VS_CONN_F_FWD_MASK
)
782 | IP_VS_CONN_F_LOCALNODE
;
785 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
786 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
787 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
790 * Put the real service in ip_vs_rtable if not present.
791 * For now only for NAT!
793 write_lock_bh(&__ip_vs_rs_lock
);
795 write_unlock_bh(&__ip_vs_rs_lock
);
797 atomic_set(&dest
->conn_flags
, conn_flags
);
799 /* bind the service */
801 __ip_vs_bind_svc(dest
, svc
);
803 if (dest
->svc
!= svc
) {
804 __ip_vs_unbind_svc(dest
);
805 ip_vs_zero_stats(&dest
->stats
);
806 __ip_vs_bind_svc(dest
, svc
);
810 /* set the dest status flags */
811 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
813 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
814 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
815 dest
->u_threshold
= udest
->u_threshold
;
816 dest
->l_threshold
= udest
->l_threshold
;
821 * Create a destination for the given service
824 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
825 struct ip_vs_dest
**dest_p
)
827 struct ip_vs_dest
*dest
;
832 #ifdef CONFIG_IP_VS_IPV6
833 if (svc
->af
== AF_INET6
) {
834 atype
= ipv6_addr_type(&udest
->addr
.in6
);
835 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
836 atype
& IPV6_ADDR_LINKLOCAL
) &&
837 !__ip_vs_addr_is_local_v6(&udest
->addr
.in6
))
842 atype
= inet_addr_type(&init_net
, udest
->addr
.ip
);
843 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
847 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
849 pr_err("%s(): no memory.\n", __func__
);
854 dest
->protocol
= svc
->protocol
;
855 dest
->vaddr
= svc
->addr
;
856 dest
->vport
= svc
->port
;
857 dest
->vfwmark
= svc
->fwmark
;
858 ip_vs_addr_copy(svc
->af
, &dest
->addr
, &udest
->addr
);
859 dest
->port
= udest
->port
;
861 atomic_set(&dest
->activeconns
, 0);
862 atomic_set(&dest
->inactconns
, 0);
863 atomic_set(&dest
->persistconns
, 0);
864 atomic_set(&dest
->refcnt
, 0);
866 INIT_LIST_HEAD(&dest
->d_list
);
867 spin_lock_init(&dest
->dst_lock
);
868 spin_lock_init(&dest
->stats
.lock
);
869 __ip_vs_update_dest(svc
, dest
, udest
);
870 ip_vs_new_estimator(&dest
->stats
);
880 * Add a destination into an existing service
883 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
885 struct ip_vs_dest
*dest
;
886 union nf_inet_addr daddr
;
887 __be16 dport
= udest
->port
;
892 if (udest
->weight
< 0) {
893 pr_err("%s(): server weight less than zero\n", __func__
);
897 if (udest
->l_threshold
> udest
->u_threshold
) {
898 pr_err("%s(): lower threshold is higher than upper threshold\n",
903 ip_vs_addr_copy(svc
->af
, &daddr
, &udest
->addr
);
906 * Check if the dest already exists in the list
908 dest
= ip_vs_lookup_dest(svc
, &daddr
, dport
);
911 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
916 * Check if the dest already exists in the trash and
917 * is from the same service
919 dest
= ip_vs_trash_get_dest(svc
, &daddr
, dport
);
922 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
923 "dest->refcnt=%d, service %u/%s:%u\n",
924 IP_VS_DBG_ADDR(svc
->af
, &daddr
), ntohs(dport
),
925 atomic_read(&dest
->refcnt
),
927 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
930 __ip_vs_update_dest(svc
, dest
, udest
);
933 * Get the destination from the trash
935 list_del(&dest
->n_list
);
937 ip_vs_new_estimator(&dest
->stats
);
939 write_lock_bh(&__ip_vs_svc_lock
);
942 * Wait until all other svc users go away.
944 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
946 list_add(&dest
->n_list
, &svc
->destinations
);
949 /* call the update_service function of its scheduler */
950 if (svc
->scheduler
->update_service
)
951 svc
->scheduler
->update_service(svc
);
953 write_unlock_bh(&__ip_vs_svc_lock
);
958 * Allocate and initialize the dest structure
960 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
966 * Add the dest entry into the list
968 atomic_inc(&dest
->refcnt
);
970 write_lock_bh(&__ip_vs_svc_lock
);
973 * Wait until all other svc users go away.
975 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
977 list_add(&dest
->n_list
, &svc
->destinations
);
980 /* call the update_service function of its scheduler */
981 if (svc
->scheduler
->update_service
)
982 svc
->scheduler
->update_service(svc
);
984 write_unlock_bh(&__ip_vs_svc_lock
);
993 * Edit a destination in the given service
996 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
998 struct ip_vs_dest
*dest
;
999 union nf_inet_addr daddr
;
1000 __be16 dport
= udest
->port
;
1004 if (udest
->weight
< 0) {
1005 pr_err("%s(): server weight less than zero\n", __func__
);
1009 if (udest
->l_threshold
> udest
->u_threshold
) {
1010 pr_err("%s(): lower threshold is higher than upper threshold\n",
1015 ip_vs_addr_copy(svc
->af
, &daddr
, &udest
->addr
);
1018 * Lookup the destination list
1020 dest
= ip_vs_lookup_dest(svc
, &daddr
, dport
);
1023 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1027 __ip_vs_update_dest(svc
, dest
, udest
);
1029 write_lock_bh(&__ip_vs_svc_lock
);
1031 /* Wait until all other svc users go away */
1032 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
1034 /* call the update_service, because server weight may be changed */
1035 if (svc
->scheduler
->update_service
)
1036 svc
->scheduler
->update_service(svc
);
1038 write_unlock_bh(&__ip_vs_svc_lock
);
1047 * Delete a destination (must be already unlinked from the service)
1049 static void __ip_vs_del_dest(struct ip_vs_dest
*dest
)
1051 ip_vs_kill_estimator(&dest
->stats
);
1054 * Remove it from the d-linked list with the real services.
1056 write_lock_bh(&__ip_vs_rs_lock
);
1057 ip_vs_rs_unhash(dest
);
1058 write_unlock_bh(&__ip_vs_rs_lock
);
1061 * Decrease the refcnt of the dest, and free the dest
1062 * if nobody refers to it (refcnt=0). Otherwise, throw
1063 * the destination into the trash.
1065 if (atomic_dec_and_test(&dest
->refcnt
)) {
1066 ip_vs_dst_reset(dest
);
1067 /* simply decrease svc->refcnt here, let the caller check
1068 and release the service if nobody refers to it.
1069 Only user context can release destination and service,
1070 and only one user context can update virtual service at a
1071 time, so the operation here is OK */
1072 atomic_dec(&dest
->svc
->refcnt
);
1075 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1076 "dest->refcnt=%d\n",
1077 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1079 atomic_read(&dest
->refcnt
));
1080 list_add(&dest
->n_list
, &ip_vs_dest_trash
);
1081 atomic_inc(&dest
->refcnt
);
1087 * Unlink a destination from the given service
1089 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1090 struct ip_vs_dest
*dest
,
1093 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1096 * Remove it from the d-linked destination list.
1098 list_del(&dest
->n_list
);
1102 * Call the update_service function of its scheduler
1104 if (svcupd
&& svc
->scheduler
->update_service
)
1105 svc
->scheduler
->update_service(svc
);
1110 * Delete a destination server in the given service
1113 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1115 struct ip_vs_dest
*dest
;
1116 __be16 dport
= udest
->port
;
1120 dest
= ip_vs_lookup_dest(svc
, &udest
->addr
, dport
);
1123 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1127 write_lock_bh(&__ip_vs_svc_lock
);
1130 * Wait until all other svc users go away.
1132 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
1135 * Unlink dest from the service
1137 __ip_vs_unlink_dest(svc
, dest
, 1);
1139 write_unlock_bh(&__ip_vs_svc_lock
);
1142 * Delete the destination
1144 __ip_vs_del_dest(dest
);
1153 * Add a service into the service hash table
1156 ip_vs_add_service(struct ip_vs_service_user_kern
*u
,
1157 struct ip_vs_service
**svc_p
)
1160 struct ip_vs_scheduler
*sched
= NULL
;
1161 struct ip_vs_service
*svc
= NULL
;
1163 /* increase the module use count */
1164 ip_vs_use_count_inc();
1166 /* Lookup the scheduler by 'u->sched_name' */
1167 sched
= ip_vs_scheduler_get(u
->sched_name
);
1168 if (sched
== NULL
) {
1169 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1174 #ifdef CONFIG_IP_VS_IPV6
1175 if (u
->af
== AF_INET6
&& (u
->netmask
< 1 || u
->netmask
> 128)) {
1181 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1183 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1188 /* I'm the first user of the service */
1189 atomic_set(&svc
->usecnt
, 1);
1190 atomic_set(&svc
->refcnt
, 0);
1193 svc
->protocol
= u
->protocol
;
1194 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1195 svc
->port
= u
->port
;
1196 svc
->fwmark
= u
->fwmark
;
1197 svc
->flags
= u
->flags
;
1198 svc
->timeout
= u
->timeout
* HZ
;
1199 svc
->netmask
= u
->netmask
;
1201 INIT_LIST_HEAD(&svc
->destinations
);
1202 rwlock_init(&svc
->sched_lock
);
1203 spin_lock_init(&svc
->stats
.lock
);
1205 /* Bind the scheduler */
1206 ret
= ip_vs_bind_scheduler(svc
, sched
);
1211 /* Update the virtual service counters */
1212 if (svc
->port
== FTPPORT
)
1213 atomic_inc(&ip_vs_ftpsvc_counter
);
1214 else if (svc
->port
== 0)
1215 atomic_inc(&ip_vs_nullsvc_counter
);
1217 ip_vs_new_estimator(&svc
->stats
);
1219 /* Count only IPv4 services for old get/setsockopt interface */
1220 if (svc
->af
== AF_INET
)
1221 ip_vs_num_services
++;
1223 /* Hash the service into the service table */
1224 write_lock_bh(&__ip_vs_svc_lock
);
1225 ip_vs_svc_hash(svc
);
1226 write_unlock_bh(&__ip_vs_svc_lock
);
1234 ip_vs_unbind_scheduler(svc
);
1237 ip_vs_app_inc_put(svc
->inc
);
1242 ip_vs_scheduler_put(sched
);
1245 /* decrease the module use count */
1246 ip_vs_use_count_dec();
1253 * Edit a service and bind it with a new scheduler
1256 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1258 struct ip_vs_scheduler
*sched
, *old_sched
;
1262 * Lookup the scheduler, by 'u->sched_name'
1264 sched
= ip_vs_scheduler_get(u
->sched_name
);
1265 if (sched
== NULL
) {
1266 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1271 #ifdef CONFIG_IP_VS_IPV6
1272 if (u
->af
== AF_INET6
&& (u
->netmask
< 1 || u
->netmask
> 128)) {
1278 write_lock_bh(&__ip_vs_svc_lock
);
1281 * Wait until all other svc users go away.
1283 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
1286 * Set the flags and timeout value
1288 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1289 svc
->timeout
= u
->timeout
* HZ
;
1290 svc
->netmask
= u
->netmask
;
1292 old_sched
= svc
->scheduler
;
1293 if (sched
!= old_sched
) {
1295 * Unbind the old scheduler
1297 if ((ret
= ip_vs_unbind_scheduler(svc
))) {
1303 * Bind the new scheduler
1305 if ((ret
= ip_vs_bind_scheduler(svc
, sched
))) {
1307 * If ip_vs_bind_scheduler fails, restore the old
1309 * The main reason of failure is out of memory.
1311 * The question is if the old scheduler can be
1312 * restored all the time. TODO: if it cannot be
1313 * restored some time, we must delete the service,
1314 * otherwise the system may crash.
1316 ip_vs_bind_scheduler(svc
, old_sched
);
1323 write_unlock_bh(&__ip_vs_svc_lock
);
1324 #ifdef CONFIG_IP_VS_IPV6
1329 ip_vs_scheduler_put(old_sched
);
1336 * Delete a service from the service list
1337 * - The service must be unlinked, unlocked and not referenced!
1338 * - We are called under _bh lock
1340 static void __ip_vs_del_service(struct ip_vs_service
*svc
)
1342 struct ip_vs_dest
*dest
, *nxt
;
1343 struct ip_vs_scheduler
*old_sched
;
1345 /* Count only IPv4 services for old get/setsockopt interface */
1346 if (svc
->af
== AF_INET
)
1347 ip_vs_num_services
--;
1349 ip_vs_kill_estimator(&svc
->stats
);
1351 /* Unbind scheduler */
1352 old_sched
= svc
->scheduler
;
1353 ip_vs_unbind_scheduler(svc
);
1355 ip_vs_scheduler_put(old_sched
);
1357 /* Unbind app inc */
1359 ip_vs_app_inc_put(svc
->inc
);
1364 * Unlink the whole destination list
1366 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1367 __ip_vs_unlink_dest(svc
, dest
, 0);
1368 __ip_vs_del_dest(dest
);
1372 * Update the virtual service counters
1374 if (svc
->port
== FTPPORT
)
1375 atomic_dec(&ip_vs_ftpsvc_counter
);
1376 else if (svc
->port
== 0)
1377 atomic_dec(&ip_vs_nullsvc_counter
);
1380 * Free the service if nobody refers to it
1382 if (atomic_read(&svc
->refcnt
) == 0)
1385 /* decrease the module use count */
1386 ip_vs_use_count_dec();
1390 * Delete a service from the service list
1392 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1398 * Unhash it from the service table
1400 write_lock_bh(&__ip_vs_svc_lock
);
1402 ip_vs_svc_unhash(svc
);
1405 * Wait until all the svc users go away.
1407 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 1);
1409 __ip_vs_del_service(svc
);
1411 write_unlock_bh(&__ip_vs_svc_lock
);
1418 * Flush all the virtual services
1420 static int ip_vs_flush(void)
1423 struct ip_vs_service
*svc
, *nxt
;
1426 * Flush the service table hashed by <protocol,addr,port>
1428 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1429 list_for_each_entry_safe(svc
, nxt
, &ip_vs_svc_table
[idx
], s_list
) {
1430 write_lock_bh(&__ip_vs_svc_lock
);
1431 ip_vs_svc_unhash(svc
);
1433 * Wait until all the svc users go away.
1435 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
1436 __ip_vs_del_service(svc
);
1437 write_unlock_bh(&__ip_vs_svc_lock
);
1442 * Flush the service table hashed by fwmark
1444 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1445 list_for_each_entry_safe(svc
, nxt
,
1446 &ip_vs_svc_fwm_table
[idx
], f_list
) {
1447 write_lock_bh(&__ip_vs_svc_lock
);
1448 ip_vs_svc_unhash(svc
);
1450 * Wait until all the svc users go away.
1452 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
1453 __ip_vs_del_service(svc
);
1454 write_unlock_bh(&__ip_vs_svc_lock
);
1463 * Zero counters in a service or all services
1465 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1467 struct ip_vs_dest
*dest
;
1469 write_lock_bh(&__ip_vs_svc_lock
);
1470 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1471 ip_vs_zero_stats(&dest
->stats
);
1473 ip_vs_zero_stats(&svc
->stats
);
1474 write_unlock_bh(&__ip_vs_svc_lock
);
1478 static int ip_vs_zero_all(void)
1481 struct ip_vs_service
*svc
;
1483 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1484 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1485 ip_vs_zero_service(svc
);
1489 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1490 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1491 ip_vs_zero_service(svc
);
1495 ip_vs_zero_stats(&ip_vs_stats
);
1501 proc_do_defense_mode(ctl_table
*table
, int write
,
1502 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1504 int *valp
= table
->data
;
1508 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1509 if (write
&& (*valp
!= val
)) {
1510 if ((*valp
< 0) || (*valp
> 3)) {
1511 /* Restore the correct value */
1514 update_defense_level();
1522 proc_do_sync_threshold(ctl_table
*table
, int write
,
1523 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1525 int *valp
= table
->data
;
1529 /* backup the value first */
1530 memcpy(val
, valp
, sizeof(val
));
1532 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1533 if (write
&& (valp
[0] < 0 || valp
[1] < 0 || valp
[0] >= valp
[1])) {
1534 /* Restore the correct value */
1535 memcpy(valp
, val
, sizeof(val
));
1542 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1545 static struct ctl_table vs_vars
[] = {
1547 .procname
= "amemthresh",
1548 .data
= &sysctl_ip_vs_amemthresh
,
1549 .maxlen
= sizeof(int),
1551 .proc_handler
= proc_dointvec
,
1553 #ifdef CONFIG_IP_VS_DEBUG
1555 .procname
= "debug_level",
1556 .data
= &sysctl_ip_vs_debug_level
,
1557 .maxlen
= sizeof(int),
1559 .proc_handler
= proc_dointvec
,
1563 .procname
= "am_droprate",
1564 .data
= &sysctl_ip_vs_am_droprate
,
1565 .maxlen
= sizeof(int),
1567 .proc_handler
= proc_dointvec
,
1570 .procname
= "drop_entry",
1571 .data
= &sysctl_ip_vs_drop_entry
,
1572 .maxlen
= sizeof(int),
1574 .proc_handler
= proc_do_defense_mode
,
1577 .procname
= "drop_packet",
1578 .data
= &sysctl_ip_vs_drop_packet
,
1579 .maxlen
= sizeof(int),
1581 .proc_handler
= proc_do_defense_mode
,
1584 .procname
= "secure_tcp",
1585 .data
= &sysctl_ip_vs_secure_tcp
,
1586 .maxlen
= sizeof(int),
1588 .proc_handler
= proc_do_defense_mode
,
1592 .procname
= "timeout_established",
1593 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_ESTABLISHED
],
1594 .maxlen
= sizeof(int),
1596 .proc_handler
= proc_dointvec_jiffies
,
1599 .procname
= "timeout_synsent",
1600 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYN_SENT
],
1601 .maxlen
= sizeof(int),
1603 .proc_handler
= proc_dointvec_jiffies
,
1606 .procname
= "timeout_synrecv",
1607 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYN_RECV
],
1608 .maxlen
= sizeof(int),
1610 .proc_handler
= proc_dointvec_jiffies
,
1613 .procname
= "timeout_finwait",
1614 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_FIN_WAIT
],
1615 .maxlen
= sizeof(int),
1617 .proc_handler
= proc_dointvec_jiffies
,
1620 .procname
= "timeout_timewait",
1621 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_TIME_WAIT
],
1622 .maxlen
= sizeof(int),
1624 .proc_handler
= proc_dointvec_jiffies
,
1627 .procname
= "timeout_close",
1628 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_CLOSE
],
1629 .maxlen
= sizeof(int),
1631 .proc_handler
= proc_dointvec_jiffies
,
1634 .procname
= "timeout_closewait",
1635 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_CLOSE_WAIT
],
1636 .maxlen
= sizeof(int),
1638 .proc_handler
= proc_dointvec_jiffies
,
1641 .procname
= "timeout_lastack",
1642 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_LAST_ACK
],
1643 .maxlen
= sizeof(int),
1645 .proc_handler
= proc_dointvec_jiffies
,
1648 .procname
= "timeout_listen",
1649 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_LISTEN
],
1650 .maxlen
= sizeof(int),
1652 .proc_handler
= proc_dointvec_jiffies
,
1655 .procname
= "timeout_synack",
1656 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYNACK
],
1657 .maxlen
= sizeof(int),
1659 .proc_handler
= proc_dointvec_jiffies
,
1662 .procname
= "timeout_udp",
1663 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_UDP
],
1664 .maxlen
= sizeof(int),
1666 .proc_handler
= proc_dointvec_jiffies
,
1669 .procname
= "timeout_icmp",
1670 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_ICMP
],
1671 .maxlen
= sizeof(int),
1673 .proc_handler
= proc_dointvec_jiffies
,
1677 .procname
= "cache_bypass",
1678 .data
= &sysctl_ip_vs_cache_bypass
,
1679 .maxlen
= sizeof(int),
1681 .proc_handler
= proc_dointvec
,
1684 .procname
= "expire_nodest_conn",
1685 .data
= &sysctl_ip_vs_expire_nodest_conn
,
1686 .maxlen
= sizeof(int),
1688 .proc_handler
= proc_dointvec
,
1691 .procname
= "expire_quiescent_template",
1692 .data
= &sysctl_ip_vs_expire_quiescent_template
,
1693 .maxlen
= sizeof(int),
1695 .proc_handler
= proc_dointvec
,
1698 .procname
= "sync_threshold",
1699 .data
= &sysctl_ip_vs_sync_threshold
,
1700 .maxlen
= sizeof(sysctl_ip_vs_sync_threshold
),
1702 .proc_handler
= proc_do_sync_threshold
,
1705 .procname
= "nat_icmp_send",
1706 .data
= &sysctl_ip_vs_nat_icmp_send
,
1707 .maxlen
= sizeof(int),
1709 .proc_handler
= proc_dointvec
,
1714 const struct ctl_path net_vs_ctl_path
[] = {
1715 { .procname
= "net", },
1716 { .procname
= "ipv4", },
1717 { .procname
= "vs", },
1720 EXPORT_SYMBOL_GPL(net_vs_ctl_path
);
1722 static struct ctl_table_header
* sysctl_header
;
1724 #ifdef CONFIG_PROC_FS
1727 struct list_head
*table
;
1732 * Write the contents of the VS rule table to a PROCfs file.
1733 * (It is kept just for backward compatibility)
1735 static inline const char *ip_vs_fwd_name(unsigned flags
)
1737 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1738 case IP_VS_CONN_F_LOCALNODE
:
1740 case IP_VS_CONN_F_TUNNEL
:
1742 case IP_VS_CONN_F_DROUTE
:
1750 /* Get the Nth entry in the two lists */
1751 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1753 struct ip_vs_iter
*iter
= seq
->private;
1755 struct ip_vs_service
*svc
;
1757 /* look in hash by protocol */
1758 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1759 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1761 iter
->table
= ip_vs_svc_table
;
1768 /* keep looking in fwmark */
1769 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1770 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1772 iter
->table
= ip_vs_svc_fwm_table
;
1782 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1783 __acquires(__ip_vs_svc_lock
)
1786 read_lock_bh(&__ip_vs_svc_lock
);
1787 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1791 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1793 struct list_head
*e
;
1794 struct ip_vs_iter
*iter
;
1795 struct ip_vs_service
*svc
;
1798 if (v
== SEQ_START_TOKEN
)
1799 return ip_vs_info_array(seq
,0);
1802 iter
= seq
->private;
1804 if (iter
->table
== ip_vs_svc_table
) {
1805 /* next service in table hashed by protocol */
1806 if ((e
= svc
->s_list
.next
) != &ip_vs_svc_table
[iter
->bucket
])
1807 return list_entry(e
, struct ip_vs_service
, s_list
);
1810 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1811 list_for_each_entry(svc
,&ip_vs_svc_table
[iter
->bucket
],
1817 iter
->table
= ip_vs_svc_fwm_table
;
1822 /* next service in hashed by fwmark */
1823 if ((e
= svc
->f_list
.next
) != &ip_vs_svc_fwm_table
[iter
->bucket
])
1824 return list_entry(e
, struct ip_vs_service
, f_list
);
1827 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1828 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[iter
->bucket
],
1836 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
1837 __releases(__ip_vs_svc_lock
)
1839 read_unlock_bh(&__ip_vs_svc_lock
);
1843 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
1845 if (v
== SEQ_START_TOKEN
) {
1847 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1848 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
1850 "Prot LocalAddress:Port Scheduler Flags\n");
1852 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1854 const struct ip_vs_service
*svc
= v
;
1855 const struct ip_vs_iter
*iter
= seq
->private;
1856 const struct ip_vs_dest
*dest
;
1858 if (iter
->table
== ip_vs_svc_table
) {
1859 #ifdef CONFIG_IP_VS_IPV6
1860 if (svc
->af
== AF_INET6
)
1861 seq_printf(seq
, "%s [%pI6]:%04X %s ",
1862 ip_vs_proto_name(svc
->protocol
),
1865 svc
->scheduler
->name
);
1868 seq_printf(seq
, "%s %08X:%04X %s %s ",
1869 ip_vs_proto_name(svc
->protocol
),
1870 ntohl(svc
->addr
.ip
),
1872 svc
->scheduler
->name
,
1873 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
1875 seq_printf(seq
, "FWM %08X %s %s",
1876 svc
->fwmark
, svc
->scheduler
->name
,
1877 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
1880 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
1881 seq_printf(seq
, "persistent %d %08X\n",
1883 ntohl(svc
->netmask
));
1885 seq_putc(seq
, '\n');
1887 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1888 #ifdef CONFIG_IP_VS_IPV6
1889 if (dest
->af
== AF_INET6
)
1892 " %-7s %-6d %-10d %-10d\n",
1895 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
1896 atomic_read(&dest
->weight
),
1897 atomic_read(&dest
->activeconns
),
1898 atomic_read(&dest
->inactconns
));
1903 "%-7s %-6d %-10d %-10d\n",
1904 ntohl(dest
->addr
.ip
),
1906 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
1907 atomic_read(&dest
->weight
),
1908 atomic_read(&dest
->activeconns
),
1909 atomic_read(&dest
->inactconns
));
1916 static const struct seq_operations ip_vs_info_seq_ops
= {
1917 .start
= ip_vs_info_seq_start
,
1918 .next
= ip_vs_info_seq_next
,
1919 .stop
= ip_vs_info_seq_stop
,
1920 .show
= ip_vs_info_seq_show
,
1923 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
1925 return seq_open_private(file
, &ip_vs_info_seq_ops
,
1926 sizeof(struct ip_vs_iter
));
1929 static const struct file_operations ip_vs_info_fops
= {
1930 .owner
= THIS_MODULE
,
1931 .open
= ip_vs_info_open
,
1933 .llseek
= seq_lseek
,
1934 .release
= seq_release_private
,
1939 struct ip_vs_stats ip_vs_stats
= {
1940 .lock
= __SPIN_LOCK_UNLOCKED(ip_vs_stats
.lock
),
1943 #ifdef CONFIG_PROC_FS
1944 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
1947 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1949 " Total Incoming Outgoing Incoming Outgoing\n");
1951 " Conns Packets Packets Bytes Bytes\n");
1953 spin_lock_bh(&ip_vs_stats
.lock
);
1954 seq_printf(seq
, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats
.ustats
.conns
,
1955 ip_vs_stats
.ustats
.inpkts
, ip_vs_stats
.ustats
.outpkts
,
1956 (unsigned long long) ip_vs_stats
.ustats
.inbytes
,
1957 (unsigned long long) ip_vs_stats
.ustats
.outbytes
);
1959 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1961 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1962 seq_printf(seq
,"%8X %8X %8X %16X %16X\n",
1963 ip_vs_stats
.ustats
.cps
,
1964 ip_vs_stats
.ustats
.inpps
,
1965 ip_vs_stats
.ustats
.outpps
,
1966 ip_vs_stats
.ustats
.inbps
,
1967 ip_vs_stats
.ustats
.outbps
);
1968 spin_unlock_bh(&ip_vs_stats
.lock
);
1973 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
1975 return single_open(file
, ip_vs_stats_show
, NULL
);
1978 static const struct file_operations ip_vs_stats_fops
= {
1979 .owner
= THIS_MODULE
,
1980 .open
= ip_vs_stats_seq_open
,
1982 .llseek
= seq_lseek
,
1983 .release
= single_release
,
1989 * Set timeout values for tcp tcpfin udp in the timeout_table.
1991 static int ip_vs_set_timeout(struct ip_vs_timeout_user
*u
)
1993 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1998 #ifdef CONFIG_IP_VS_PROTO_TCP
1999 if (u
->tcp_timeout
) {
2000 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
2001 = u
->tcp_timeout
* HZ
;
2004 if (u
->tcp_fin_timeout
) {
2005 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
2006 = u
->tcp_fin_timeout
* HZ
;
2010 #ifdef CONFIG_IP_VS_PROTO_UDP
2011 if (u
->udp_timeout
) {
2012 ip_vs_protocol_udp
.timeout_table
[IP_VS_UDP_S_NORMAL
]
2013 = u
->udp_timeout
* HZ
;
2020 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2021 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2022 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2023 sizeof(struct ip_vs_dest_user))
2024 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2025 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2026 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2028 static const unsigned char set_arglen
[SET_CMDID(IP_VS_SO_SET_MAX
)+1] = {
2029 [SET_CMDID(IP_VS_SO_SET_ADD
)] = SERVICE_ARG_LEN
,
2030 [SET_CMDID(IP_VS_SO_SET_EDIT
)] = SERVICE_ARG_LEN
,
2031 [SET_CMDID(IP_VS_SO_SET_DEL
)] = SERVICE_ARG_LEN
,
2032 [SET_CMDID(IP_VS_SO_SET_FLUSH
)] = 0,
2033 [SET_CMDID(IP_VS_SO_SET_ADDDEST
)] = SVCDEST_ARG_LEN
,
2034 [SET_CMDID(IP_VS_SO_SET_DELDEST
)] = SVCDEST_ARG_LEN
,
2035 [SET_CMDID(IP_VS_SO_SET_EDITDEST
)] = SVCDEST_ARG_LEN
,
2036 [SET_CMDID(IP_VS_SO_SET_TIMEOUT
)] = TIMEOUT_ARG_LEN
,
2037 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON
)] = DAEMON_ARG_LEN
,
2038 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON
)] = DAEMON_ARG_LEN
,
2039 [SET_CMDID(IP_VS_SO_SET_ZERO
)] = SERVICE_ARG_LEN
,
2042 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2043 struct ip_vs_service_user
*usvc_compat
)
2046 usvc
->protocol
= usvc_compat
->protocol
;
2047 usvc
->addr
.ip
= usvc_compat
->addr
;
2048 usvc
->port
= usvc_compat
->port
;
2049 usvc
->fwmark
= usvc_compat
->fwmark
;
2051 /* Deep copy of sched_name is not needed here */
2052 usvc
->sched_name
= usvc_compat
->sched_name
;
2054 usvc
->flags
= usvc_compat
->flags
;
2055 usvc
->timeout
= usvc_compat
->timeout
;
2056 usvc
->netmask
= usvc_compat
->netmask
;
2059 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2060 struct ip_vs_dest_user
*udest_compat
)
2062 udest
->addr
.ip
= udest_compat
->addr
;
2063 udest
->port
= udest_compat
->port
;
2064 udest
->conn_flags
= udest_compat
->conn_flags
;
2065 udest
->weight
= udest_compat
->weight
;
2066 udest
->u_threshold
= udest_compat
->u_threshold
;
2067 udest
->l_threshold
= udest_compat
->l_threshold
;
2071 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2074 unsigned char arg
[MAX_ARG_LEN
];
2075 struct ip_vs_service_user
*usvc_compat
;
2076 struct ip_vs_service_user_kern usvc
;
2077 struct ip_vs_service
*svc
;
2078 struct ip_vs_dest_user
*udest_compat
;
2079 struct ip_vs_dest_user_kern udest
;
2081 if (!capable(CAP_NET_ADMIN
))
2084 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2086 if (len
< 0 || len
> MAX_ARG_LEN
)
2088 if (len
!= set_arglen
[SET_CMDID(cmd
)]) {
2089 pr_err("set_ctl: len %u != %u\n",
2090 len
, set_arglen
[SET_CMDID(cmd
)]);
2094 if (copy_from_user(arg
, user
, len
) != 0)
2097 /* increase the module use count */
2098 ip_vs_use_count_inc();
2100 if (mutex_lock_interruptible(&__ip_vs_mutex
)) {
2105 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2106 /* Flush the virtual service */
2107 ret
= ip_vs_flush();
2109 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2110 /* Set timeout values for (tcp tcpfin udp) */
2111 ret
= ip_vs_set_timeout((struct ip_vs_timeout_user
*)arg
);
2113 } else if (cmd
== IP_VS_SO_SET_STARTDAEMON
) {
2114 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2115 ret
= start_sync_thread(dm
->state
, dm
->mcast_ifn
, dm
->syncid
);
2117 } else if (cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2118 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2119 ret
= stop_sync_thread(dm
->state
);
2123 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2124 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2126 /* We only use the new structs internally, so copy userspace compat
2127 * structs to extended internal versions */
2128 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2129 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2131 if (cmd
== IP_VS_SO_SET_ZERO
) {
2132 /* if no service address is set, zero counters in all */
2133 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2134 ret
= ip_vs_zero_all();
2139 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2140 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2141 usvc
.protocol
!= IPPROTO_SCTP
) {
2142 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2143 usvc
.protocol
, &usvc
.addr
.ip
,
2144 ntohs(usvc
.port
), usvc
.sched_name
);
2149 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2150 if (usvc
.fwmark
== 0)
2151 svc
= __ip_vs_service_get(usvc
.af
, usvc
.protocol
,
2152 &usvc
.addr
, usvc
.port
);
2154 svc
= __ip_vs_svc_fwm_get(usvc
.af
, usvc
.fwmark
);
2156 if (cmd
!= IP_VS_SO_SET_ADD
2157 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2159 goto out_drop_service
;
2163 case IP_VS_SO_SET_ADD
:
2167 ret
= ip_vs_add_service(&usvc
, &svc
);
2169 case IP_VS_SO_SET_EDIT
:
2170 ret
= ip_vs_edit_service(svc
, &usvc
);
2172 case IP_VS_SO_SET_DEL
:
2173 ret
= ip_vs_del_service(svc
);
2177 case IP_VS_SO_SET_ZERO
:
2178 ret
= ip_vs_zero_service(svc
);
2180 case IP_VS_SO_SET_ADDDEST
:
2181 ret
= ip_vs_add_dest(svc
, &udest
);
2183 case IP_VS_SO_SET_EDITDEST
:
2184 ret
= ip_vs_edit_dest(svc
, &udest
);
2186 case IP_VS_SO_SET_DELDEST
:
2187 ret
= ip_vs_del_dest(svc
, &udest
);
2195 ip_vs_service_put(svc
);
2198 mutex_unlock(&__ip_vs_mutex
);
2200 /* decrease the module use count */
2201 ip_vs_use_count_dec();
2208 ip_vs_copy_stats(struct ip_vs_stats_user
*dst
, struct ip_vs_stats
*src
)
2210 spin_lock_bh(&src
->lock
);
2211 memcpy(dst
, &src
->ustats
, sizeof(*dst
));
2212 spin_unlock_bh(&src
->lock
);
2216 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2218 dst
->protocol
= src
->protocol
;
2219 dst
->addr
= src
->addr
.ip
;
2220 dst
->port
= src
->port
;
2221 dst
->fwmark
= src
->fwmark
;
2222 strlcpy(dst
->sched_name
, src
->scheduler
->name
, sizeof(dst
->sched_name
));
2223 dst
->flags
= src
->flags
;
2224 dst
->timeout
= src
->timeout
/ HZ
;
2225 dst
->netmask
= src
->netmask
;
2226 dst
->num_dests
= src
->num_dests
;
2227 ip_vs_copy_stats(&dst
->stats
, &src
->stats
);
2231 __ip_vs_get_service_entries(const struct ip_vs_get_services
*get
,
2232 struct ip_vs_get_services __user
*uptr
)
2235 struct ip_vs_service
*svc
;
2236 struct ip_vs_service_entry entry
;
2239 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2240 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2241 /* Only expose IPv4 entries to old interface */
2242 if (svc
->af
!= AF_INET
)
2245 if (count
>= get
->num_services
)
2247 memset(&entry
, 0, sizeof(entry
));
2248 ip_vs_copy_service(&entry
, svc
);
2249 if (copy_to_user(&uptr
->entrytable
[count
],
2250 &entry
, sizeof(entry
))) {
2258 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2259 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
2260 /* Only expose IPv4 entries to old interface */
2261 if (svc
->af
!= AF_INET
)
2264 if (count
>= get
->num_services
)
2266 memset(&entry
, 0, sizeof(entry
));
2267 ip_vs_copy_service(&entry
, svc
);
2268 if (copy_to_user(&uptr
->entrytable
[count
],
2269 &entry
, sizeof(entry
))) {
2281 __ip_vs_get_dest_entries(const struct ip_vs_get_dests
*get
,
2282 struct ip_vs_get_dests __user
*uptr
)
2284 struct ip_vs_service
*svc
;
2285 union nf_inet_addr addr
= { .ip
= get
->addr
};
2289 svc
= __ip_vs_svc_fwm_get(AF_INET
, get
->fwmark
);
2291 svc
= __ip_vs_service_get(AF_INET
, get
->protocol
, &addr
,
2296 struct ip_vs_dest
*dest
;
2297 struct ip_vs_dest_entry entry
;
2299 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2300 if (count
>= get
->num_dests
)
2303 entry
.addr
= dest
->addr
.ip
;
2304 entry
.port
= dest
->port
;
2305 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2306 entry
.weight
= atomic_read(&dest
->weight
);
2307 entry
.u_threshold
= dest
->u_threshold
;
2308 entry
.l_threshold
= dest
->l_threshold
;
2309 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2310 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2311 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2312 ip_vs_copy_stats(&entry
.stats
, &dest
->stats
);
2313 if (copy_to_user(&uptr
->entrytable
[count
],
2314 &entry
, sizeof(entry
))) {
2320 ip_vs_service_put(svc
);
2327 __ip_vs_get_timeouts(struct ip_vs_timeout_user
*u
)
2329 #ifdef CONFIG_IP_VS_PROTO_TCP
2331 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2332 u
->tcp_fin_timeout
=
2333 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2335 #ifdef CONFIG_IP_VS_PROTO_UDP
2337 ip_vs_protocol_udp
.timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2342 #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2343 #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2344 #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2345 #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2346 #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2347 #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2348 #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2350 static const unsigned char get_arglen
[GET_CMDID(IP_VS_SO_GET_MAX
)+1] = {
2351 [GET_CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2352 [GET_CMDID(IP_VS_SO_GET_INFO
)] = GET_INFO_ARG_LEN
,
2353 [GET_CMDID(IP_VS_SO_GET_SERVICES
)] = GET_SERVICES_ARG_LEN
,
2354 [GET_CMDID(IP_VS_SO_GET_SERVICE
)] = GET_SERVICE_ARG_LEN
,
2355 [GET_CMDID(IP_VS_SO_GET_DESTS
)] = GET_DESTS_ARG_LEN
,
2356 [GET_CMDID(IP_VS_SO_GET_TIMEOUT
)] = GET_TIMEOUT_ARG_LEN
,
2357 [GET_CMDID(IP_VS_SO_GET_DAEMON
)] = GET_DAEMON_ARG_LEN
,
2361 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2363 unsigned char arg
[128];
2365 unsigned int copylen
;
2367 if (!capable(CAP_NET_ADMIN
))
2370 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2373 if (*len
< get_arglen
[GET_CMDID(cmd
)]) {
2374 pr_err("get_ctl: len %u < %u\n",
2375 *len
, get_arglen
[GET_CMDID(cmd
)]);
2379 copylen
= get_arglen
[GET_CMDID(cmd
)];
2383 if (copy_from_user(arg
, user
, copylen
) != 0)
2386 if (mutex_lock_interruptible(&__ip_vs_mutex
))
2387 return -ERESTARTSYS
;
2390 case IP_VS_SO_GET_VERSION
:
2394 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2395 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2396 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2400 *len
= strlen(buf
)+1;
2404 case IP_VS_SO_GET_INFO
:
2406 struct ip_vs_getinfo info
;
2407 info
.version
= IP_VS_VERSION_CODE
;
2408 info
.size
= ip_vs_conn_tab_size
;
2409 info
.num_services
= ip_vs_num_services
;
2410 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2415 case IP_VS_SO_GET_SERVICES
:
2417 struct ip_vs_get_services
*get
;
2420 get
= (struct ip_vs_get_services
*)arg
;
2421 size
= sizeof(*get
) +
2422 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2424 pr_err("length: %u != %u\n", *len
, size
);
2428 ret
= __ip_vs_get_service_entries(get
, user
);
2432 case IP_VS_SO_GET_SERVICE
:
2434 struct ip_vs_service_entry
*entry
;
2435 struct ip_vs_service
*svc
;
2436 union nf_inet_addr addr
;
2438 entry
= (struct ip_vs_service_entry
*)arg
;
2439 addr
.ip
= entry
->addr
;
2441 svc
= __ip_vs_svc_fwm_get(AF_INET
, entry
->fwmark
);
2443 svc
= __ip_vs_service_get(AF_INET
, entry
->protocol
,
2444 &addr
, entry
->port
);
2446 ip_vs_copy_service(entry
, svc
);
2447 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2449 ip_vs_service_put(svc
);
2455 case IP_VS_SO_GET_DESTS
:
2457 struct ip_vs_get_dests
*get
;
2460 get
= (struct ip_vs_get_dests
*)arg
;
2461 size
= sizeof(*get
) +
2462 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2464 pr_err("length: %u != %u\n", *len
, size
);
2468 ret
= __ip_vs_get_dest_entries(get
, user
);
2472 case IP_VS_SO_GET_TIMEOUT
:
2474 struct ip_vs_timeout_user t
;
2476 __ip_vs_get_timeouts(&t
);
2477 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2482 case IP_VS_SO_GET_DAEMON
:
2484 struct ip_vs_daemon_user d
[2];
2486 memset(&d
, 0, sizeof(d
));
2487 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
) {
2488 d
[0].state
= IP_VS_STATE_MASTER
;
2489 strlcpy(d
[0].mcast_ifn
, ip_vs_master_mcast_ifn
, sizeof(d
[0].mcast_ifn
));
2490 d
[0].syncid
= ip_vs_master_syncid
;
2492 if (ip_vs_sync_state
& IP_VS_STATE_BACKUP
) {
2493 d
[1].state
= IP_VS_STATE_BACKUP
;
2494 strlcpy(d
[1].mcast_ifn
, ip_vs_backup_mcast_ifn
, sizeof(d
[1].mcast_ifn
));
2495 d
[1].syncid
= ip_vs_backup_syncid
;
2497 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2507 mutex_unlock(&__ip_vs_mutex
);
2512 static struct nf_sockopt_ops ip_vs_sockopts
= {
2514 .set_optmin
= IP_VS_BASE_CTL
,
2515 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2516 .set
= do_ip_vs_set_ctl
,
2517 .get_optmin
= IP_VS_BASE_CTL
,
2518 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2519 .get
= do_ip_vs_get_ctl
,
2520 .owner
= THIS_MODULE
,
2524 * Generic Netlink interface
2527 /* IPVS genetlink family */
2528 static struct genl_family ip_vs_genl_family
= {
2529 .id
= GENL_ID_GENERATE
,
2531 .name
= IPVS_GENL_NAME
,
2532 .version
= IPVS_GENL_VERSION
,
2533 .maxattr
= IPVS_CMD_MAX
,
2536 /* Policy used for first-level command attributes */
2537 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2538 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2539 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2540 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2541 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2542 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2543 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2546 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2547 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2548 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2549 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2550 .len
= IP_VS_IFNAME_MAXLEN
},
2551 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2554 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2555 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2556 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2557 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2558 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2559 .len
= sizeof(union nf_inet_addr
) },
2560 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2561 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2562 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2563 .len
= IP_VS_SCHEDNAME_MAXLEN
},
2564 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2565 .len
= sizeof(struct ip_vs_flags
) },
2566 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2567 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2568 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2571 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2572 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2573 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2574 .len
= sizeof(union nf_inet_addr
) },
2575 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2576 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2577 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2578 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2579 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2580 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2581 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2582 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2583 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2586 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2587 struct ip_vs_stats
*stats
)
2589 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2593 spin_lock_bh(&stats
->lock
);
2595 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_CONNS
, stats
->ustats
.conns
);
2596 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INPKTS
, stats
->ustats
.inpkts
);
2597 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTPKTS
, stats
->ustats
.outpkts
);
2598 NLA_PUT_U64(skb
, IPVS_STATS_ATTR_INBYTES
, stats
->ustats
.inbytes
);
2599 NLA_PUT_U64(skb
, IPVS_STATS_ATTR_OUTBYTES
, stats
->ustats
.outbytes
);
2600 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_CPS
, stats
->ustats
.cps
);
2601 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INPPS
, stats
->ustats
.inpps
);
2602 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTPPS
, stats
->ustats
.outpps
);
2603 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INBPS
, stats
->ustats
.inbps
);
2604 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTBPS
, stats
->ustats
.outbps
);
2606 spin_unlock_bh(&stats
->lock
);
2608 nla_nest_end(skb
, nl_stats
);
2613 spin_unlock_bh(&stats
->lock
);
2614 nla_nest_cancel(skb
, nl_stats
);
2618 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2619 struct ip_vs_service
*svc
)
2621 struct nlattr
*nl_service
;
2622 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2625 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
2629 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
);
2632 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
);
2634 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
);
2635 NLA_PUT(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
);
2636 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
);
2639 NLA_PUT_STRING(skb
, IPVS_SVC_ATTR_SCHED_NAME
, svc
->scheduler
->name
);
2640 NLA_PUT(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
);
2641 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
);
2642 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
);
2644 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &svc
->stats
))
2645 goto nla_put_failure
;
2647 nla_nest_end(skb
, nl_service
);
2652 nla_nest_cancel(skb
, nl_service
);
2656 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
2657 struct ip_vs_service
*svc
,
2658 struct netlink_callback
*cb
)
2662 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2663 &ip_vs_genl_family
, NLM_F_MULTI
,
2664 IPVS_CMD_NEW_SERVICE
);
2668 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
2669 goto nla_put_failure
;
2671 return genlmsg_end(skb
, hdr
);
2674 genlmsg_cancel(skb
, hdr
);
2678 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
2679 struct netlink_callback
*cb
)
2682 int start
= cb
->args
[0];
2683 struct ip_vs_service
*svc
;
2685 mutex_lock(&__ip_vs_mutex
);
2686 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2687 list_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
2690 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2692 goto nla_put_failure
;
2697 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2698 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
2701 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2703 goto nla_put_failure
;
2709 mutex_unlock(&__ip_vs_mutex
);
2715 static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern
*usvc
,
2716 struct nlattr
*nla
, int full_entry
)
2718 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
2719 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
2721 /* Parse mandatory identifying service fields first */
2723 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
, ip_vs_svc_policy
))
2726 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
2727 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
2728 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
2729 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
2730 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
2732 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
2735 memset(usvc
, 0, sizeof(*usvc
));
2737 usvc
->af
= nla_get_u16(nla_af
);
2738 #ifdef CONFIG_IP_VS_IPV6
2739 if (usvc
->af
!= AF_INET
&& usvc
->af
!= AF_INET6
)
2741 if (usvc
->af
!= AF_INET
)
2743 return -EAFNOSUPPORT
;
2746 usvc
->protocol
= IPPROTO_TCP
;
2747 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
2749 usvc
->protocol
= nla_get_u16(nla_protocol
);
2750 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
2751 usvc
->port
= nla_get_u16(nla_port
);
2755 /* If a full entry was requested, check for the additional fields */
2757 struct nlattr
*nla_sched
, *nla_flags
, *nla_timeout
,
2759 struct ip_vs_flags flags
;
2760 struct ip_vs_service
*svc
;
2762 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
2763 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
2764 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
2765 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
2767 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
2770 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
2772 /* prefill flags from service if it already exists */
2774 svc
= __ip_vs_svc_fwm_get(usvc
->af
, usvc
->fwmark
);
2776 svc
= __ip_vs_service_get(usvc
->af
, usvc
->protocol
,
2777 &usvc
->addr
, usvc
->port
);
2779 usvc
->flags
= svc
->flags
;
2780 ip_vs_service_put(svc
);
2784 /* set new flags from userland */
2785 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
2786 (flags
.flags
& flags
.mask
);
2787 usvc
->sched_name
= nla_data(nla_sched
);
2788 usvc
->timeout
= nla_get_u32(nla_timeout
);
2789 usvc
->netmask
= nla_get_u32(nla_netmask
);
2795 static struct ip_vs_service
*ip_vs_genl_find_service(struct nlattr
*nla
)
2797 struct ip_vs_service_user_kern usvc
;
2800 ret
= ip_vs_genl_parse_service(&usvc
, nla
, 0);
2802 return ERR_PTR(ret
);
2805 return __ip_vs_svc_fwm_get(usvc
.af
, usvc
.fwmark
);
2807 return __ip_vs_service_get(usvc
.af
, usvc
.protocol
,
2808 &usvc
.addr
, usvc
.port
);
2811 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
2813 struct nlattr
*nl_dest
;
2815 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
2819 NLA_PUT(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
);
2820 NLA_PUT_U16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
);
2822 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
2823 atomic_read(&dest
->conn_flags
) & IP_VS_CONN_F_FWD_MASK
);
2824 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_WEIGHT
, atomic_read(&dest
->weight
));
2825 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
);
2826 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
);
2827 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
2828 atomic_read(&dest
->activeconns
));
2829 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
2830 atomic_read(&dest
->inactconns
));
2831 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
2832 atomic_read(&dest
->persistconns
));
2834 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &dest
->stats
))
2835 goto nla_put_failure
;
2837 nla_nest_end(skb
, nl_dest
);
2842 nla_nest_cancel(skb
, nl_dest
);
2846 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
2847 struct netlink_callback
*cb
)
2851 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2852 &ip_vs_genl_family
, NLM_F_MULTI
,
2857 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
2858 goto nla_put_failure
;
2860 return genlmsg_end(skb
, hdr
);
2863 genlmsg_cancel(skb
, hdr
);
2867 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
2868 struct netlink_callback
*cb
)
2871 int start
= cb
->args
[0];
2872 struct ip_vs_service
*svc
;
2873 struct ip_vs_dest
*dest
;
2874 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
2876 mutex_lock(&__ip_vs_mutex
);
2878 /* Try to find the service for which to dump destinations */
2879 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
,
2880 IPVS_CMD_ATTR_MAX
, ip_vs_cmd_policy
))
2883 svc
= ip_vs_genl_find_service(attrs
[IPVS_CMD_ATTR_SERVICE
]);
2884 if (IS_ERR(svc
) || svc
== NULL
)
2887 /* Dump the destinations */
2888 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2891 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
2893 goto nla_put_failure
;
2899 ip_vs_service_put(svc
);
2902 mutex_unlock(&__ip_vs_mutex
);
2907 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
2908 struct nlattr
*nla
, int full_entry
)
2910 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
2911 struct nlattr
*nla_addr
, *nla_port
;
2913 /* Parse mandatory identifying destination fields first */
2915 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
, ip_vs_dest_policy
))
2918 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
2919 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
2921 if (!(nla_addr
&& nla_port
))
2924 memset(udest
, 0, sizeof(*udest
));
2926 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
2927 udest
->port
= nla_get_u16(nla_port
);
2929 /* If a full entry was requested, check for the additional fields */
2931 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
2934 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
2935 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
2936 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
2937 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
2939 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
2942 udest
->conn_flags
= nla_get_u32(nla_fwd
)
2943 & IP_VS_CONN_F_FWD_MASK
;
2944 udest
->weight
= nla_get_u32(nla_weight
);
2945 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
2946 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
2952 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __be32 state
,
2953 const char *mcast_ifn
, __be32 syncid
)
2955 struct nlattr
*nl_daemon
;
2957 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
2961 NLA_PUT_U32(skb
, IPVS_DAEMON_ATTR_STATE
, state
);
2962 NLA_PUT_STRING(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, mcast_ifn
);
2963 NLA_PUT_U32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, syncid
);
2965 nla_nest_end(skb
, nl_daemon
);
2970 nla_nest_cancel(skb
, nl_daemon
);
2974 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __be32 state
,
2975 const char *mcast_ifn
, __be32 syncid
,
2976 struct netlink_callback
*cb
)
2979 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2980 &ip_vs_genl_family
, NLM_F_MULTI
,
2981 IPVS_CMD_NEW_DAEMON
);
2985 if (ip_vs_genl_fill_daemon(skb
, state
, mcast_ifn
, syncid
))
2986 goto nla_put_failure
;
2988 return genlmsg_end(skb
, hdr
);
2991 genlmsg_cancel(skb
, hdr
);
2995 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
2996 struct netlink_callback
*cb
)
2998 mutex_lock(&__ip_vs_mutex
);
2999 if ((ip_vs_sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
3000 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
3001 ip_vs_master_mcast_ifn
,
3002 ip_vs_master_syncid
, cb
) < 0)
3003 goto nla_put_failure
;
3008 if ((ip_vs_sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
3009 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
3010 ip_vs_backup_mcast_ifn
,
3011 ip_vs_backup_syncid
, cb
) < 0)
3012 goto nla_put_failure
;
3018 mutex_unlock(&__ip_vs_mutex
);
3023 static int ip_vs_genl_new_daemon(struct nlattr
**attrs
)
3025 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
3026 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
3027 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3030 return start_sync_thread(nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]),
3031 nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3032 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]));
3035 static int ip_vs_genl_del_daemon(struct nlattr
**attrs
)
3037 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3040 return stop_sync_thread(nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3043 static int ip_vs_genl_set_config(struct nlattr
**attrs
)
3045 struct ip_vs_timeout_user t
;
3047 __ip_vs_get_timeouts(&t
);
3049 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3050 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3052 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3054 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3056 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3057 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3059 return ip_vs_set_timeout(&t
);
3062 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3064 struct ip_vs_service
*svc
= NULL
;
3065 struct ip_vs_service_user_kern usvc
;
3066 struct ip_vs_dest_user_kern udest
;
3068 int need_full_svc
= 0, need_full_dest
= 0;
3070 cmd
= info
->genlhdr
->cmd
;
3072 mutex_lock(&__ip_vs_mutex
);
3074 if (cmd
== IPVS_CMD_FLUSH
) {
3075 ret
= ip_vs_flush();
3077 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3078 ret
= ip_vs_genl_set_config(info
->attrs
);
3080 } else if (cmd
== IPVS_CMD_NEW_DAEMON
||
3081 cmd
== IPVS_CMD_DEL_DAEMON
) {
3083 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3085 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3086 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3087 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3088 ip_vs_daemon_policy
)) {
3093 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3094 ret
= ip_vs_genl_new_daemon(daemon_attrs
);
3096 ret
= ip_vs_genl_del_daemon(daemon_attrs
);
3098 } else if (cmd
== IPVS_CMD_ZERO
&&
3099 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3100 ret
= ip_vs_zero_all();
3104 /* All following commands require a service argument, so check if we
3105 * received a valid one. We need a full service specification when
3106 * adding / editing a service. Only identifying members otherwise. */
3107 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3110 ret
= ip_vs_genl_parse_service(&usvc
,
3111 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3116 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3117 if (usvc
.fwmark
== 0)
3118 svc
= __ip_vs_service_get(usvc
.af
, usvc
.protocol
,
3119 &usvc
.addr
, usvc
.port
);
3121 svc
= __ip_vs_svc_fwm_get(usvc
.af
, usvc
.fwmark
);
3123 /* Unless we're adding a new service, the service must already exist */
3124 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3129 /* Destination commands require a valid destination argument. For
3130 * adding / editing a destination, we need a full destination
3132 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3133 cmd
== IPVS_CMD_DEL_DEST
) {
3134 if (cmd
!= IPVS_CMD_DEL_DEST
)
3137 ret
= ip_vs_genl_parse_dest(&udest
,
3138 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3145 case IPVS_CMD_NEW_SERVICE
:
3147 ret
= ip_vs_add_service(&usvc
, &svc
);
3151 case IPVS_CMD_SET_SERVICE
:
3152 ret
= ip_vs_edit_service(svc
, &usvc
);
3154 case IPVS_CMD_DEL_SERVICE
:
3155 ret
= ip_vs_del_service(svc
);
3157 case IPVS_CMD_NEW_DEST
:
3158 ret
= ip_vs_add_dest(svc
, &udest
);
3160 case IPVS_CMD_SET_DEST
:
3161 ret
= ip_vs_edit_dest(svc
, &udest
);
3163 case IPVS_CMD_DEL_DEST
:
3164 ret
= ip_vs_del_dest(svc
, &udest
);
3167 ret
= ip_vs_zero_service(svc
);
3175 ip_vs_service_put(svc
);
3176 mutex_unlock(&__ip_vs_mutex
);
3181 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3183 struct sk_buff
*msg
;
3185 int ret
, cmd
, reply_cmd
;
3187 cmd
= info
->genlhdr
->cmd
;
3189 if (cmd
== IPVS_CMD_GET_SERVICE
)
3190 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3191 else if (cmd
== IPVS_CMD_GET_INFO
)
3192 reply_cmd
= IPVS_CMD_SET_INFO
;
3193 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3194 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3196 pr_err("unknown Generic Netlink command\n");
3200 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3204 mutex_lock(&__ip_vs_mutex
);
3206 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3208 goto nla_put_failure
;
3211 case IPVS_CMD_GET_SERVICE
:
3213 struct ip_vs_service
*svc
;
3215 svc
= ip_vs_genl_find_service(info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3220 ret
= ip_vs_genl_fill_service(msg
, svc
);
3221 ip_vs_service_put(svc
);
3223 goto nla_put_failure
;
3232 case IPVS_CMD_GET_CONFIG
:
3234 struct ip_vs_timeout_user t
;
3236 __ip_vs_get_timeouts(&t
);
3237 #ifdef CONFIG_IP_VS_PROTO_TCP
3238 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
, t
.tcp_timeout
);
3239 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3242 #ifdef CONFIG_IP_VS_PROTO_UDP
3243 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
);
3249 case IPVS_CMD_GET_INFO
:
3250 NLA_PUT_U32(msg
, IPVS_INFO_ATTR_VERSION
, IP_VS_VERSION_CODE
);
3251 NLA_PUT_U32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3252 ip_vs_conn_tab_size
);
3256 genlmsg_end(msg
, reply
);
3257 ret
= genlmsg_reply(msg
, info
);
3261 pr_err("not enough space in Netlink message\n");
3267 mutex_unlock(&__ip_vs_mutex
);
3273 static struct genl_ops ip_vs_genl_ops
[] __read_mostly
= {
3275 .cmd
= IPVS_CMD_NEW_SERVICE
,
3276 .flags
= GENL_ADMIN_PERM
,
3277 .policy
= ip_vs_cmd_policy
,
3278 .doit
= ip_vs_genl_set_cmd
,
3281 .cmd
= IPVS_CMD_SET_SERVICE
,
3282 .flags
= GENL_ADMIN_PERM
,
3283 .policy
= ip_vs_cmd_policy
,
3284 .doit
= ip_vs_genl_set_cmd
,
3287 .cmd
= IPVS_CMD_DEL_SERVICE
,
3288 .flags
= GENL_ADMIN_PERM
,
3289 .policy
= ip_vs_cmd_policy
,
3290 .doit
= ip_vs_genl_set_cmd
,
3293 .cmd
= IPVS_CMD_GET_SERVICE
,
3294 .flags
= GENL_ADMIN_PERM
,
3295 .doit
= ip_vs_genl_get_cmd
,
3296 .dumpit
= ip_vs_genl_dump_services
,
3297 .policy
= ip_vs_cmd_policy
,
3300 .cmd
= IPVS_CMD_NEW_DEST
,
3301 .flags
= GENL_ADMIN_PERM
,
3302 .policy
= ip_vs_cmd_policy
,
3303 .doit
= ip_vs_genl_set_cmd
,
3306 .cmd
= IPVS_CMD_SET_DEST
,
3307 .flags
= GENL_ADMIN_PERM
,
3308 .policy
= ip_vs_cmd_policy
,
3309 .doit
= ip_vs_genl_set_cmd
,
3312 .cmd
= IPVS_CMD_DEL_DEST
,
3313 .flags
= GENL_ADMIN_PERM
,
3314 .policy
= ip_vs_cmd_policy
,
3315 .doit
= ip_vs_genl_set_cmd
,
3318 .cmd
= IPVS_CMD_GET_DEST
,
3319 .flags
= GENL_ADMIN_PERM
,
3320 .policy
= ip_vs_cmd_policy
,
3321 .dumpit
= ip_vs_genl_dump_dests
,
3324 .cmd
= IPVS_CMD_NEW_DAEMON
,
3325 .flags
= GENL_ADMIN_PERM
,
3326 .policy
= ip_vs_cmd_policy
,
3327 .doit
= ip_vs_genl_set_cmd
,
3330 .cmd
= IPVS_CMD_DEL_DAEMON
,
3331 .flags
= GENL_ADMIN_PERM
,
3332 .policy
= ip_vs_cmd_policy
,
3333 .doit
= ip_vs_genl_set_cmd
,
3336 .cmd
= IPVS_CMD_GET_DAEMON
,
3337 .flags
= GENL_ADMIN_PERM
,
3338 .dumpit
= ip_vs_genl_dump_daemons
,
3341 .cmd
= IPVS_CMD_SET_CONFIG
,
3342 .flags
= GENL_ADMIN_PERM
,
3343 .policy
= ip_vs_cmd_policy
,
3344 .doit
= ip_vs_genl_set_cmd
,
3347 .cmd
= IPVS_CMD_GET_CONFIG
,
3348 .flags
= GENL_ADMIN_PERM
,
3349 .doit
= ip_vs_genl_get_cmd
,
3352 .cmd
= IPVS_CMD_GET_INFO
,
3353 .flags
= GENL_ADMIN_PERM
,
3354 .doit
= ip_vs_genl_get_cmd
,
3357 .cmd
= IPVS_CMD_ZERO
,
3358 .flags
= GENL_ADMIN_PERM
,
3359 .policy
= ip_vs_cmd_policy
,
3360 .doit
= ip_vs_genl_set_cmd
,
3363 .cmd
= IPVS_CMD_FLUSH
,
3364 .flags
= GENL_ADMIN_PERM
,
3365 .doit
= ip_vs_genl_set_cmd
,
3369 static int __init
ip_vs_genl_register(void)
3371 return genl_register_family_with_ops(&ip_vs_genl_family
,
3372 ip_vs_genl_ops
, ARRAY_SIZE(ip_vs_genl_ops
));
3375 static void ip_vs_genl_unregister(void)
3377 genl_unregister_family(&ip_vs_genl_family
);
3380 /* End of Generic Netlink interface definitions */
3383 int __init
ip_vs_control_init(void)
3390 ret
= nf_register_sockopt(&ip_vs_sockopts
);
3392 pr_err("cannot register sockopt.\n");
3396 ret
= ip_vs_genl_register();
3398 pr_err("cannot register Generic Netlink interface.\n");
3399 nf_unregister_sockopt(&ip_vs_sockopts
);
3403 proc_net_fops_create(&init_net
, "ip_vs", 0, &ip_vs_info_fops
);
3404 proc_net_fops_create(&init_net
, "ip_vs_stats",0, &ip_vs_stats_fops
);
3406 sysctl_header
= register_sysctl_paths(net_vs_ctl_path
, vs_vars
);
3408 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3409 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
3410 INIT_LIST_HEAD(&ip_vs_svc_table
[idx
]);
3411 INIT_LIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
3413 for(idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++) {
3414 INIT_LIST_HEAD(&ip_vs_rtable
[idx
]);
3417 ip_vs_new_estimator(&ip_vs_stats
);
3419 /* Hook the defense timer */
3420 schedule_delayed_work(&defense_work
, DEFENSE_TIMER_PERIOD
);
3427 void ip_vs_control_cleanup(void)
3430 ip_vs_trash_cleanup();
3431 cancel_rearming_delayed_work(&defense_work
);
3432 cancel_work_sync(&defense_work
.work
);
3433 ip_vs_kill_estimator(&ip_vs_stats
);
3434 unregister_sysctl_table(sysctl_header
);
3435 proc_net_remove(&init_net
, "ip_vs_stats");
3436 proc_net_remove(&init_net
, "ip_vs");
3437 ip_vs_genl_unregister();
3438 nf_unregister_sockopt(&ip_vs_sockopts
);