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;
91 #ifdef CONFIG_IP_VS_NFCT
92 int sysctl_ip_vs_conntrack
;
94 int sysctl_ip_vs_snat_reroute
= 1;
97 #ifdef CONFIG_IP_VS_DEBUG
98 static int sysctl_ip_vs_debug_level
= 0;
100 int ip_vs_get_debug_level(void)
102 return sysctl_ip_vs_debug_level
;
106 #ifdef CONFIG_IP_VS_IPV6
107 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
108 static int __ip_vs_addr_is_local_v6(const struct in6_addr
*addr
)
116 .saddr
= { .s6_addr32
= {0, 0, 0, 0} }, } },
119 rt
= (struct rt6_info
*)ip6_route_output(&init_net
, NULL
, &fl
);
120 if (rt
&& rt
->rt6i_dev
&& (rt
->rt6i_dev
->flags
& IFF_LOOPBACK
))
127 * update_defense_level is called from keventd and from sysctl,
128 * so it needs to protect itself from softirqs
130 static void update_defense_level(void)
133 static int old_secure_tcp
= 0;
138 /* we only count free and buffered memory (in pages) */
140 availmem
= i
.freeram
+ i
.bufferram
;
141 /* however in linux 2.5 the i.bufferram is total page cache size,
143 /* si_swapinfo(&i); */
144 /* availmem = availmem - (i.totalswap - i.freeswap); */
146 nomem
= (availmem
< sysctl_ip_vs_amemthresh
);
151 spin_lock(&__ip_vs_dropentry_lock
);
152 switch (sysctl_ip_vs_drop_entry
) {
154 atomic_set(&ip_vs_dropentry
, 0);
158 atomic_set(&ip_vs_dropentry
, 1);
159 sysctl_ip_vs_drop_entry
= 2;
161 atomic_set(&ip_vs_dropentry
, 0);
166 atomic_set(&ip_vs_dropentry
, 1);
168 atomic_set(&ip_vs_dropentry
, 0);
169 sysctl_ip_vs_drop_entry
= 1;
173 atomic_set(&ip_vs_dropentry
, 1);
176 spin_unlock(&__ip_vs_dropentry_lock
);
179 spin_lock(&__ip_vs_droppacket_lock
);
180 switch (sysctl_ip_vs_drop_packet
) {
186 ip_vs_drop_rate
= ip_vs_drop_counter
187 = sysctl_ip_vs_amemthresh
/
188 (sysctl_ip_vs_amemthresh
-availmem
);
189 sysctl_ip_vs_drop_packet
= 2;
196 ip_vs_drop_rate
= ip_vs_drop_counter
197 = sysctl_ip_vs_amemthresh
/
198 (sysctl_ip_vs_amemthresh
-availmem
);
201 sysctl_ip_vs_drop_packet
= 1;
205 ip_vs_drop_rate
= sysctl_ip_vs_am_droprate
;
208 spin_unlock(&__ip_vs_droppacket_lock
);
211 spin_lock(&ip_vs_securetcp_lock
);
212 switch (sysctl_ip_vs_secure_tcp
) {
214 if (old_secure_tcp
>= 2)
219 if (old_secure_tcp
< 2)
221 sysctl_ip_vs_secure_tcp
= 2;
223 if (old_secure_tcp
>= 2)
229 if (old_secure_tcp
< 2)
232 if (old_secure_tcp
>= 2)
234 sysctl_ip_vs_secure_tcp
= 1;
238 if (old_secure_tcp
< 2)
242 old_secure_tcp
= sysctl_ip_vs_secure_tcp
;
244 ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp
>1);
245 spin_unlock(&ip_vs_securetcp_lock
);
252 * Timer for checking the defense
254 #define DEFENSE_TIMER_PERIOD 1*HZ
255 static void defense_work_handler(struct work_struct
*work
);
256 static DECLARE_DELAYED_WORK(defense_work
, defense_work_handler
);
258 static void defense_work_handler(struct work_struct
*work
)
260 update_defense_level();
261 if (atomic_read(&ip_vs_dropentry
))
262 ip_vs_random_dropentry();
264 schedule_delayed_work(&defense_work
, DEFENSE_TIMER_PERIOD
);
268 ip_vs_use_count_inc(void)
270 return try_module_get(THIS_MODULE
);
274 ip_vs_use_count_dec(void)
276 module_put(THIS_MODULE
);
281 * Hash table: for virtual service lookups
283 #define IP_VS_SVC_TAB_BITS 8
284 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
285 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
287 /* the service table hashed by <protocol, addr, port> */
288 static struct list_head ip_vs_svc_table
[IP_VS_SVC_TAB_SIZE
];
289 /* the service table hashed by fwmark */
290 static struct list_head ip_vs_svc_fwm_table
[IP_VS_SVC_TAB_SIZE
];
293 * Hash table: for real service lookups
295 #define IP_VS_RTAB_BITS 4
296 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
297 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
299 static struct list_head ip_vs_rtable
[IP_VS_RTAB_SIZE
];
302 * Trash for destinations
304 static LIST_HEAD(ip_vs_dest_trash
);
307 * FTP & NULL virtual service counters
309 static atomic_t ip_vs_ftpsvc_counter
= ATOMIC_INIT(0);
310 static atomic_t ip_vs_nullsvc_counter
= ATOMIC_INIT(0);
314 * Returns hash value for virtual service
316 static __inline__
unsigned
317 ip_vs_svc_hashkey(int af
, unsigned proto
, const union nf_inet_addr
*addr
,
320 register unsigned porth
= ntohs(port
);
321 __be32 addr_fold
= addr
->ip
;
323 #ifdef CONFIG_IP_VS_IPV6
325 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
326 addr
->ip6
[2]^addr
->ip6
[3];
329 return (proto
^ntohl(addr_fold
)^(porth
>>IP_VS_SVC_TAB_BITS
)^porth
)
330 & IP_VS_SVC_TAB_MASK
;
334 * Returns hash value of fwmark for virtual service lookup
336 static __inline__
unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark
)
338 return fwmark
& IP_VS_SVC_TAB_MASK
;
342 * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
343 * or in the ip_vs_svc_fwm_table by fwmark.
344 * Should be called with locked tables.
346 static int ip_vs_svc_hash(struct ip_vs_service
*svc
)
350 if (svc
->flags
& IP_VS_SVC_F_HASHED
) {
351 pr_err("%s(): request for already hashed, called from %pF\n",
352 __func__
, __builtin_return_address(0));
356 if (svc
->fwmark
== 0) {
358 * Hash it by <protocol,addr,port> in ip_vs_svc_table
360 hash
= ip_vs_svc_hashkey(svc
->af
, svc
->protocol
, &svc
->addr
,
362 list_add(&svc
->s_list
, &ip_vs_svc_table
[hash
]);
365 * Hash it by fwmark in ip_vs_svc_fwm_table
367 hash
= ip_vs_svc_fwm_hashkey(svc
->fwmark
);
368 list_add(&svc
->f_list
, &ip_vs_svc_fwm_table
[hash
]);
371 svc
->flags
|= IP_VS_SVC_F_HASHED
;
372 /* increase its refcnt because it is referenced by the svc table */
373 atomic_inc(&svc
->refcnt
);
379 * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
380 * Should be called with locked tables.
382 static int ip_vs_svc_unhash(struct ip_vs_service
*svc
)
384 if (!(svc
->flags
& IP_VS_SVC_F_HASHED
)) {
385 pr_err("%s(): request for unhash flagged, called from %pF\n",
386 __func__
, __builtin_return_address(0));
390 if (svc
->fwmark
== 0) {
391 /* Remove it from the ip_vs_svc_table table */
392 list_del(&svc
->s_list
);
394 /* Remove it from the ip_vs_svc_fwm_table table */
395 list_del(&svc
->f_list
);
398 svc
->flags
&= ~IP_VS_SVC_F_HASHED
;
399 atomic_dec(&svc
->refcnt
);
405 * Get service by {proto,addr,port} in the service table.
407 static inline struct ip_vs_service
*
408 __ip_vs_service_find(int af
, __u16 protocol
, const union nf_inet_addr
*vaddr
,
412 struct ip_vs_service
*svc
;
414 /* Check for "full" addressed entries */
415 hash
= ip_vs_svc_hashkey(af
, protocol
, vaddr
, vport
);
417 list_for_each_entry(svc
, &ip_vs_svc_table
[hash
], s_list
){
419 && ip_vs_addr_equal(af
, &svc
->addr
, vaddr
)
420 && (svc
->port
== vport
)
421 && (svc
->protocol
== protocol
)) {
432 * Get service by {fwmark} in the service table.
434 static inline struct ip_vs_service
*
435 __ip_vs_svc_fwm_find(int af
, __u32 fwmark
)
438 struct ip_vs_service
*svc
;
440 /* Check for fwmark addressed entries */
441 hash
= ip_vs_svc_fwm_hashkey(fwmark
);
443 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[hash
], f_list
) {
444 if (svc
->fwmark
== fwmark
&& svc
->af
== af
) {
453 struct ip_vs_service
*
454 ip_vs_service_get(int af
, __u32 fwmark
, __u16 protocol
,
455 const union nf_inet_addr
*vaddr
, __be16 vport
)
457 struct ip_vs_service
*svc
;
459 read_lock(&__ip_vs_svc_lock
);
462 * Check the table hashed by fwmark first
464 if (fwmark
&& (svc
= __ip_vs_svc_fwm_find(af
, fwmark
)))
468 * Check the table hashed by <protocol,addr,port>
469 * for "full" addressed entries
471 svc
= __ip_vs_service_find(af
, protocol
, vaddr
, vport
);
474 && protocol
== IPPROTO_TCP
475 && atomic_read(&ip_vs_ftpsvc_counter
)
476 && (vport
== FTPDATA
|| ntohs(vport
) >= PROT_SOCK
)) {
478 * Check if ftp service entry exists, the packet
479 * might belong to FTP data connections.
481 svc
= __ip_vs_service_find(af
, protocol
, vaddr
, FTPPORT
);
485 && atomic_read(&ip_vs_nullsvc_counter
)) {
487 * Check if the catch-all port (port zero) exists
489 svc
= __ip_vs_service_find(af
, protocol
, vaddr
, 0);
494 atomic_inc(&svc
->usecnt
);
495 read_unlock(&__ip_vs_svc_lock
);
497 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
498 fwmark
, ip_vs_proto_name(protocol
),
499 IP_VS_DBG_ADDR(af
, vaddr
), ntohs(vport
),
500 svc
? "hit" : "not hit");
507 __ip_vs_bind_svc(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
509 atomic_inc(&svc
->refcnt
);
514 __ip_vs_unbind_svc(struct ip_vs_dest
*dest
)
516 struct ip_vs_service
*svc
= dest
->svc
;
519 if (atomic_dec_and_test(&svc
->refcnt
)) {
520 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n",
522 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
523 ntohs(svc
->port
), atomic_read(&svc
->usecnt
));
530 * Returns hash value for real service
532 static inline unsigned ip_vs_rs_hashkey(int af
,
533 const union nf_inet_addr
*addr
,
536 register unsigned porth
= ntohs(port
);
537 __be32 addr_fold
= addr
->ip
;
539 #ifdef CONFIG_IP_VS_IPV6
541 addr_fold
= addr
->ip6
[0]^addr
->ip6
[1]^
542 addr
->ip6
[2]^addr
->ip6
[3];
545 return (ntohl(addr_fold
)^(porth
>>IP_VS_RTAB_BITS
)^porth
)
550 * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
551 * should be called with locked tables.
553 static int ip_vs_rs_hash(struct ip_vs_dest
*dest
)
557 if (!list_empty(&dest
->d_list
)) {
562 * Hash by proto,addr,port,
563 * which are the parameters of the real service.
565 hash
= ip_vs_rs_hashkey(dest
->af
, &dest
->addr
, dest
->port
);
567 list_add(&dest
->d_list
, &ip_vs_rtable
[hash
]);
573 * UNhashes ip_vs_dest from ip_vs_rtable.
574 * should be called with locked tables.
576 static int ip_vs_rs_unhash(struct ip_vs_dest
*dest
)
579 * Remove it from the ip_vs_rtable table.
581 if (!list_empty(&dest
->d_list
)) {
582 list_del(&dest
->d_list
);
583 INIT_LIST_HEAD(&dest
->d_list
);
590 * Lookup real service by <proto,addr,port> in the real service table.
593 ip_vs_lookup_real_service(int af
, __u16 protocol
,
594 const union nf_inet_addr
*daddr
,
598 struct ip_vs_dest
*dest
;
601 * Check for "full" addressed entries
602 * Return the first found entry
604 hash
= ip_vs_rs_hashkey(af
, daddr
, dport
);
606 read_lock(&__ip_vs_rs_lock
);
607 list_for_each_entry(dest
, &ip_vs_rtable
[hash
], d_list
) {
609 && ip_vs_addr_equal(af
, &dest
->addr
, daddr
)
610 && (dest
->port
== dport
)
611 && ((dest
->protocol
== protocol
) ||
614 read_unlock(&__ip_vs_rs_lock
);
618 read_unlock(&__ip_vs_rs_lock
);
624 * Lookup destination by {addr,port} in the given service
626 static struct ip_vs_dest
*
627 ip_vs_lookup_dest(struct ip_vs_service
*svc
, const union nf_inet_addr
*daddr
,
630 struct ip_vs_dest
*dest
;
633 * Find the destination for the given service
635 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
636 if ((dest
->af
== svc
->af
)
637 && ip_vs_addr_equal(svc
->af
, &dest
->addr
, daddr
)
638 && (dest
->port
== dport
)) {
648 * Find destination by {daddr,dport,vaddr,protocol}
649 * Cretaed to be used in ip_vs_process_message() in
650 * the backup synchronization daemon. It finds the
651 * destination to be bound to the received connection
654 * ip_vs_lookup_real_service() looked promissing, but
655 * seems not working as expected.
657 struct ip_vs_dest
*ip_vs_find_dest(int af
, const union nf_inet_addr
*daddr
,
659 const union nf_inet_addr
*vaddr
,
660 __be16 vport
, __u16 protocol
)
662 struct ip_vs_dest
*dest
;
663 struct ip_vs_service
*svc
;
665 svc
= ip_vs_service_get(af
, 0, protocol
, vaddr
, vport
);
668 dest
= ip_vs_lookup_dest(svc
, daddr
, dport
);
670 atomic_inc(&dest
->refcnt
);
671 ip_vs_service_put(svc
);
676 * Lookup dest by {svc,addr,port} in the destination trash.
677 * The destination trash is used to hold the destinations that are removed
678 * from the service table but are still referenced by some conn entries.
679 * The reason to add the destination trash is when the dest is temporary
680 * down (either by administrator or by monitor program), the dest can be
681 * picked back from the trash, the remaining connections to the dest can
682 * continue, and the counting information of the dest is also useful for
685 static struct ip_vs_dest
*
686 ip_vs_trash_get_dest(struct ip_vs_service
*svc
, const union nf_inet_addr
*daddr
,
689 struct ip_vs_dest
*dest
, *nxt
;
692 * Find the destination in trash
694 list_for_each_entry_safe(dest
, nxt
, &ip_vs_dest_trash
, n_list
) {
695 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
698 IP_VS_DBG_ADDR(svc
->af
, &dest
->addr
),
700 atomic_read(&dest
->refcnt
));
701 if (dest
->af
== svc
->af
&&
702 ip_vs_addr_equal(svc
->af
, &dest
->addr
, daddr
) &&
703 dest
->port
== dport
&&
704 dest
->vfwmark
== svc
->fwmark
&&
705 dest
->protocol
== svc
->protocol
&&
707 (ip_vs_addr_equal(svc
->af
, &dest
->vaddr
, &svc
->addr
) &&
708 dest
->vport
== svc
->port
))) {
714 * Try to purge the destination from trash if not referenced
716 if (atomic_read(&dest
->refcnt
) == 1) {
717 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
720 IP_VS_DBG_ADDR(svc
->af
, &dest
->addr
),
722 list_del(&dest
->n_list
);
723 ip_vs_dst_reset(dest
);
724 __ip_vs_unbind_svc(dest
);
734 * Clean up all the destinations in the trash
735 * Called by the ip_vs_control_cleanup()
737 * When the ip_vs_control_clearup is activated by ipvs module exit,
738 * the service tables must have been flushed and all the connections
739 * are expired, and the refcnt of each destination in the trash must
740 * be 1, so we simply release them here.
742 static void ip_vs_trash_cleanup(void)
744 struct ip_vs_dest
*dest
, *nxt
;
746 list_for_each_entry_safe(dest
, nxt
, &ip_vs_dest_trash
, n_list
) {
747 list_del(&dest
->n_list
);
748 ip_vs_dst_reset(dest
);
749 __ip_vs_unbind_svc(dest
);
756 ip_vs_zero_stats(struct ip_vs_stats
*stats
)
758 spin_lock_bh(&stats
->lock
);
760 memset(&stats
->ustats
, 0, sizeof(stats
->ustats
));
761 ip_vs_zero_estimator(stats
);
763 spin_unlock_bh(&stats
->lock
);
767 * Update a destination in the given service
770 __ip_vs_update_dest(struct ip_vs_service
*svc
, struct ip_vs_dest
*dest
,
771 struct ip_vs_dest_user_kern
*udest
, int add
)
775 /* set the weight and the flags */
776 atomic_set(&dest
->weight
, udest
->weight
);
777 conn_flags
= udest
->conn_flags
& IP_VS_CONN_F_DEST_MASK
;
778 conn_flags
|= IP_VS_CONN_F_INACTIVE
;
780 /* check if local node and update the flags */
781 #ifdef CONFIG_IP_VS_IPV6
782 if (svc
->af
== AF_INET6
) {
783 if (__ip_vs_addr_is_local_v6(&udest
->addr
.in6
)) {
784 conn_flags
= (conn_flags
& ~IP_VS_CONN_F_FWD_MASK
)
785 | IP_VS_CONN_F_LOCALNODE
;
789 if (inet_addr_type(&init_net
, udest
->addr
.ip
) == RTN_LOCAL
) {
790 conn_flags
= (conn_flags
& ~IP_VS_CONN_F_FWD_MASK
)
791 | IP_VS_CONN_F_LOCALNODE
;
794 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
795 if ((conn_flags
& IP_VS_CONN_F_FWD_MASK
) != IP_VS_CONN_F_MASQ
) {
796 conn_flags
|= IP_VS_CONN_F_NOOUTPUT
;
799 * Put the real service in ip_vs_rtable if not present.
800 * For now only for NAT!
802 write_lock_bh(&__ip_vs_rs_lock
);
804 write_unlock_bh(&__ip_vs_rs_lock
);
806 atomic_set(&dest
->conn_flags
, conn_flags
);
808 /* bind the service */
810 __ip_vs_bind_svc(dest
, svc
);
812 if (dest
->svc
!= svc
) {
813 __ip_vs_unbind_svc(dest
);
814 ip_vs_zero_stats(&dest
->stats
);
815 __ip_vs_bind_svc(dest
, svc
);
819 /* set the dest status flags */
820 dest
->flags
|= IP_VS_DEST_F_AVAILABLE
;
822 if (udest
->u_threshold
== 0 || udest
->u_threshold
> dest
->u_threshold
)
823 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
824 dest
->u_threshold
= udest
->u_threshold
;
825 dest
->l_threshold
= udest
->l_threshold
;
828 ip_vs_new_estimator(&dest
->stats
);
830 write_lock_bh(&__ip_vs_svc_lock
);
832 /* Wait until all other svc users go away */
833 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
836 list_add(&dest
->n_list
, &svc
->destinations
);
840 /* call the update_service, because server weight may be changed */
841 if (svc
->scheduler
->update_service
)
842 svc
->scheduler
->update_service(svc
);
844 write_unlock_bh(&__ip_vs_svc_lock
);
849 * Create a destination for the given service
852 ip_vs_new_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
,
853 struct ip_vs_dest
**dest_p
)
855 struct ip_vs_dest
*dest
;
860 #ifdef CONFIG_IP_VS_IPV6
861 if (svc
->af
== AF_INET6
) {
862 atype
= ipv6_addr_type(&udest
->addr
.in6
);
863 if ((!(atype
& IPV6_ADDR_UNICAST
) ||
864 atype
& IPV6_ADDR_LINKLOCAL
) &&
865 !__ip_vs_addr_is_local_v6(&udest
->addr
.in6
))
870 atype
= inet_addr_type(&init_net
, udest
->addr
.ip
);
871 if (atype
!= RTN_LOCAL
&& atype
!= RTN_UNICAST
)
875 dest
= kzalloc(sizeof(struct ip_vs_dest
), GFP_KERNEL
);
877 pr_err("%s(): no memory.\n", __func__
);
882 dest
->protocol
= svc
->protocol
;
883 dest
->vaddr
= svc
->addr
;
884 dest
->vport
= svc
->port
;
885 dest
->vfwmark
= svc
->fwmark
;
886 ip_vs_addr_copy(svc
->af
, &dest
->addr
, &udest
->addr
);
887 dest
->port
= udest
->port
;
889 atomic_set(&dest
->activeconns
, 0);
890 atomic_set(&dest
->inactconns
, 0);
891 atomic_set(&dest
->persistconns
, 0);
892 atomic_set(&dest
->refcnt
, 1);
894 INIT_LIST_HEAD(&dest
->d_list
);
895 spin_lock_init(&dest
->dst_lock
);
896 spin_lock_init(&dest
->stats
.lock
);
897 __ip_vs_update_dest(svc
, dest
, udest
, 1);
907 * Add a destination into an existing service
910 ip_vs_add_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
912 struct ip_vs_dest
*dest
;
913 union nf_inet_addr daddr
;
914 __be16 dport
= udest
->port
;
919 if (udest
->weight
< 0) {
920 pr_err("%s(): server weight less than zero\n", __func__
);
924 if (udest
->l_threshold
> udest
->u_threshold
) {
925 pr_err("%s(): lower threshold is higher than upper threshold\n",
930 ip_vs_addr_copy(svc
->af
, &daddr
, &udest
->addr
);
933 * Check if the dest already exists in the list
935 dest
= ip_vs_lookup_dest(svc
, &daddr
, dport
);
938 IP_VS_DBG(1, "%s(): dest already exists\n", __func__
);
943 * Check if the dest already exists in the trash and
944 * is from the same service
946 dest
= ip_vs_trash_get_dest(svc
, &daddr
, dport
);
949 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
950 "dest->refcnt=%d, service %u/%s:%u\n",
951 IP_VS_DBG_ADDR(svc
->af
, &daddr
), ntohs(dport
),
952 atomic_read(&dest
->refcnt
),
954 IP_VS_DBG_ADDR(svc
->af
, &dest
->vaddr
),
958 * Get the destination from the trash
960 list_del(&dest
->n_list
);
962 __ip_vs_update_dest(svc
, dest
, udest
, 1);
966 * Allocate and initialize the dest structure
968 ret
= ip_vs_new_dest(svc
, udest
, &dest
);
977 * Edit a destination in the given service
980 ip_vs_edit_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
982 struct ip_vs_dest
*dest
;
983 union nf_inet_addr daddr
;
984 __be16 dport
= udest
->port
;
988 if (udest
->weight
< 0) {
989 pr_err("%s(): server weight less than zero\n", __func__
);
993 if (udest
->l_threshold
> udest
->u_threshold
) {
994 pr_err("%s(): lower threshold is higher than upper threshold\n",
999 ip_vs_addr_copy(svc
->af
, &daddr
, &udest
->addr
);
1002 * Lookup the destination list
1004 dest
= ip_vs_lookup_dest(svc
, &daddr
, dport
);
1007 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__
);
1011 __ip_vs_update_dest(svc
, dest
, udest
, 0);
1019 * Delete a destination (must be already unlinked from the service)
1021 static void __ip_vs_del_dest(struct ip_vs_dest
*dest
)
1023 ip_vs_kill_estimator(&dest
->stats
);
1026 * Remove it from the d-linked list with the real services.
1028 write_lock_bh(&__ip_vs_rs_lock
);
1029 ip_vs_rs_unhash(dest
);
1030 write_unlock_bh(&__ip_vs_rs_lock
);
1033 * Decrease the refcnt of the dest, and free the dest
1034 * if nobody refers to it (refcnt=0). Otherwise, throw
1035 * the destination into the trash.
1037 if (atomic_dec_and_test(&dest
->refcnt
)) {
1038 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u\n",
1040 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1042 ip_vs_dst_reset(dest
);
1043 /* simply decrease svc->refcnt here, let the caller check
1044 and release the service if nobody refers to it.
1045 Only user context can release destination and service,
1046 and only one user context can update virtual service at a
1047 time, so the operation here is OK */
1048 atomic_dec(&dest
->svc
->refcnt
);
1051 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1052 "dest->refcnt=%d\n",
1053 IP_VS_DBG_ADDR(dest
->af
, &dest
->addr
),
1055 atomic_read(&dest
->refcnt
));
1056 list_add(&dest
->n_list
, &ip_vs_dest_trash
);
1057 atomic_inc(&dest
->refcnt
);
1063 * Unlink a destination from the given service
1065 static void __ip_vs_unlink_dest(struct ip_vs_service
*svc
,
1066 struct ip_vs_dest
*dest
,
1069 dest
->flags
&= ~IP_VS_DEST_F_AVAILABLE
;
1072 * Remove it from the d-linked destination list.
1074 list_del(&dest
->n_list
);
1078 * Call the update_service function of its scheduler
1080 if (svcupd
&& svc
->scheduler
->update_service
)
1081 svc
->scheduler
->update_service(svc
);
1086 * Delete a destination server in the given service
1089 ip_vs_del_dest(struct ip_vs_service
*svc
, struct ip_vs_dest_user_kern
*udest
)
1091 struct ip_vs_dest
*dest
;
1092 __be16 dport
= udest
->port
;
1096 dest
= ip_vs_lookup_dest(svc
, &udest
->addr
, dport
);
1099 IP_VS_DBG(1, "%s(): destination not found!\n", __func__
);
1103 write_lock_bh(&__ip_vs_svc_lock
);
1106 * Wait until all other svc users go away.
1108 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
1111 * Unlink dest from the service
1113 __ip_vs_unlink_dest(svc
, dest
, 1);
1115 write_unlock_bh(&__ip_vs_svc_lock
);
1118 * Delete the destination
1120 __ip_vs_del_dest(dest
);
1129 * Add a service into the service hash table
1132 ip_vs_add_service(struct ip_vs_service_user_kern
*u
,
1133 struct ip_vs_service
**svc_p
)
1136 struct ip_vs_scheduler
*sched
= NULL
;
1137 struct ip_vs_service
*svc
= NULL
;
1139 /* increase the module use count */
1140 ip_vs_use_count_inc();
1142 /* Lookup the scheduler by 'u->sched_name' */
1143 sched
= ip_vs_scheduler_get(u
->sched_name
);
1144 if (sched
== NULL
) {
1145 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1150 #ifdef CONFIG_IP_VS_IPV6
1151 if (u
->af
== AF_INET6
&& (u
->netmask
< 1 || u
->netmask
> 128)) {
1157 svc
= kzalloc(sizeof(struct ip_vs_service
), GFP_KERNEL
);
1159 IP_VS_DBG(1, "%s(): no memory\n", __func__
);
1164 /* I'm the first user of the service */
1165 atomic_set(&svc
->usecnt
, 0);
1166 atomic_set(&svc
->refcnt
, 0);
1169 svc
->protocol
= u
->protocol
;
1170 ip_vs_addr_copy(svc
->af
, &svc
->addr
, &u
->addr
);
1171 svc
->port
= u
->port
;
1172 svc
->fwmark
= u
->fwmark
;
1173 svc
->flags
= u
->flags
;
1174 svc
->timeout
= u
->timeout
* HZ
;
1175 svc
->netmask
= u
->netmask
;
1177 INIT_LIST_HEAD(&svc
->destinations
);
1178 rwlock_init(&svc
->sched_lock
);
1179 spin_lock_init(&svc
->stats
.lock
);
1181 /* Bind the scheduler */
1182 ret
= ip_vs_bind_scheduler(svc
, sched
);
1187 /* Update the virtual service counters */
1188 if (svc
->port
== FTPPORT
)
1189 atomic_inc(&ip_vs_ftpsvc_counter
);
1190 else if (svc
->port
== 0)
1191 atomic_inc(&ip_vs_nullsvc_counter
);
1193 ip_vs_new_estimator(&svc
->stats
);
1195 /* Count only IPv4 services for old get/setsockopt interface */
1196 if (svc
->af
== AF_INET
)
1197 ip_vs_num_services
++;
1199 /* Hash the service into the service table */
1200 write_lock_bh(&__ip_vs_svc_lock
);
1201 ip_vs_svc_hash(svc
);
1202 write_unlock_bh(&__ip_vs_svc_lock
);
1210 ip_vs_unbind_scheduler(svc
);
1213 ip_vs_app_inc_put(svc
->inc
);
1218 ip_vs_scheduler_put(sched
);
1220 /* decrease the module use count */
1221 ip_vs_use_count_dec();
1228 * Edit a service and bind it with a new scheduler
1231 ip_vs_edit_service(struct ip_vs_service
*svc
, struct ip_vs_service_user_kern
*u
)
1233 struct ip_vs_scheduler
*sched
, *old_sched
;
1237 * Lookup the scheduler, by 'u->sched_name'
1239 sched
= ip_vs_scheduler_get(u
->sched_name
);
1240 if (sched
== NULL
) {
1241 pr_info("Scheduler module ip_vs_%s not found\n", u
->sched_name
);
1246 #ifdef CONFIG_IP_VS_IPV6
1247 if (u
->af
== AF_INET6
&& (u
->netmask
< 1 || u
->netmask
> 128)) {
1253 write_lock_bh(&__ip_vs_svc_lock
);
1256 * Wait until all other svc users go away.
1258 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
1261 * Set the flags and timeout value
1263 svc
->flags
= u
->flags
| IP_VS_SVC_F_HASHED
;
1264 svc
->timeout
= u
->timeout
* HZ
;
1265 svc
->netmask
= u
->netmask
;
1267 old_sched
= svc
->scheduler
;
1268 if (sched
!= old_sched
) {
1270 * Unbind the old scheduler
1272 if ((ret
= ip_vs_unbind_scheduler(svc
))) {
1278 * Bind the new scheduler
1280 if ((ret
= ip_vs_bind_scheduler(svc
, sched
))) {
1282 * If ip_vs_bind_scheduler fails, restore the old
1284 * The main reason of failure is out of memory.
1286 * The question is if the old scheduler can be
1287 * restored all the time. TODO: if it cannot be
1288 * restored some time, we must delete the service,
1289 * otherwise the system may crash.
1291 ip_vs_bind_scheduler(svc
, old_sched
);
1298 write_unlock_bh(&__ip_vs_svc_lock
);
1299 #ifdef CONFIG_IP_VS_IPV6
1302 ip_vs_scheduler_put(old_sched
);
1308 * Delete a service from the service list
1309 * - The service must be unlinked, unlocked and not referenced!
1310 * - We are called under _bh lock
1312 static void __ip_vs_del_service(struct ip_vs_service
*svc
)
1314 struct ip_vs_dest
*dest
, *nxt
;
1315 struct ip_vs_scheduler
*old_sched
;
1317 /* Count only IPv4 services for old get/setsockopt interface */
1318 if (svc
->af
== AF_INET
)
1319 ip_vs_num_services
--;
1321 ip_vs_kill_estimator(&svc
->stats
);
1323 /* Unbind scheduler */
1324 old_sched
= svc
->scheduler
;
1325 ip_vs_unbind_scheduler(svc
);
1326 ip_vs_scheduler_put(old_sched
);
1328 /* Unbind app inc */
1330 ip_vs_app_inc_put(svc
->inc
);
1335 * Unlink the whole destination list
1337 list_for_each_entry_safe(dest
, nxt
, &svc
->destinations
, n_list
) {
1338 __ip_vs_unlink_dest(svc
, dest
, 0);
1339 __ip_vs_del_dest(dest
);
1343 * Update the virtual service counters
1345 if (svc
->port
== FTPPORT
)
1346 atomic_dec(&ip_vs_ftpsvc_counter
);
1347 else if (svc
->port
== 0)
1348 atomic_dec(&ip_vs_nullsvc_counter
);
1351 * Free the service if nobody refers to it
1353 if (atomic_read(&svc
->refcnt
) == 0) {
1354 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n",
1356 IP_VS_DBG_ADDR(svc
->af
, &svc
->addr
),
1357 ntohs(svc
->port
), atomic_read(&svc
->usecnt
));
1361 /* decrease the module use count */
1362 ip_vs_use_count_dec();
1366 * Unlink a service from list and try to delete it if its refcnt reached 0
1368 static void ip_vs_unlink_service(struct ip_vs_service
*svc
)
1371 * Unhash it from the service table
1373 write_lock_bh(&__ip_vs_svc_lock
);
1375 ip_vs_svc_unhash(svc
);
1378 * Wait until all the svc users go away.
1380 IP_VS_WAIT_WHILE(atomic_read(&svc
->usecnt
) > 0);
1382 __ip_vs_del_service(svc
);
1384 write_unlock_bh(&__ip_vs_svc_lock
);
1388 * Delete a service from the service list
1390 static int ip_vs_del_service(struct ip_vs_service
*svc
)
1394 ip_vs_unlink_service(svc
);
1401 * Flush all the virtual services
1403 static int ip_vs_flush(void)
1406 struct ip_vs_service
*svc
, *nxt
;
1409 * Flush the service table hashed by <protocol,addr,port>
1411 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1412 list_for_each_entry_safe(svc
, nxt
, &ip_vs_svc_table
[idx
], s_list
) {
1413 ip_vs_unlink_service(svc
);
1418 * Flush the service table hashed by fwmark
1420 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1421 list_for_each_entry_safe(svc
, nxt
,
1422 &ip_vs_svc_fwm_table
[idx
], f_list
) {
1423 ip_vs_unlink_service(svc
);
1432 * Zero counters in a service or all services
1434 static int ip_vs_zero_service(struct ip_vs_service
*svc
)
1436 struct ip_vs_dest
*dest
;
1438 write_lock_bh(&__ip_vs_svc_lock
);
1439 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1440 ip_vs_zero_stats(&dest
->stats
);
1442 ip_vs_zero_stats(&svc
->stats
);
1443 write_unlock_bh(&__ip_vs_svc_lock
);
1447 static int ip_vs_zero_all(void)
1450 struct ip_vs_service
*svc
;
1452 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1453 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1454 ip_vs_zero_service(svc
);
1458 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1459 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1460 ip_vs_zero_service(svc
);
1464 ip_vs_zero_stats(&ip_vs_stats
);
1470 proc_do_defense_mode(ctl_table
*table
, int write
,
1471 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1473 int *valp
= table
->data
;
1477 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1478 if (write
&& (*valp
!= val
)) {
1479 if ((*valp
< 0) || (*valp
> 3)) {
1480 /* Restore the correct value */
1483 update_defense_level();
1491 proc_do_sync_threshold(ctl_table
*table
, int write
,
1492 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1494 int *valp
= table
->data
;
1498 /* backup the value first */
1499 memcpy(val
, valp
, sizeof(val
));
1501 rc
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1502 if (write
&& (valp
[0] < 0 || valp
[1] < 0 || valp
[0] >= valp
[1])) {
1503 /* Restore the correct value */
1504 memcpy(valp
, val
, sizeof(val
));
1511 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1514 static struct ctl_table vs_vars
[] = {
1516 .procname
= "amemthresh",
1517 .data
= &sysctl_ip_vs_amemthresh
,
1518 .maxlen
= sizeof(int),
1520 .proc_handler
= proc_dointvec
,
1522 #ifdef CONFIG_IP_VS_DEBUG
1524 .procname
= "debug_level",
1525 .data
= &sysctl_ip_vs_debug_level
,
1526 .maxlen
= sizeof(int),
1528 .proc_handler
= proc_dointvec
,
1532 .procname
= "am_droprate",
1533 .data
= &sysctl_ip_vs_am_droprate
,
1534 .maxlen
= sizeof(int),
1536 .proc_handler
= proc_dointvec
,
1539 .procname
= "drop_entry",
1540 .data
= &sysctl_ip_vs_drop_entry
,
1541 .maxlen
= sizeof(int),
1543 .proc_handler
= proc_do_defense_mode
,
1546 .procname
= "drop_packet",
1547 .data
= &sysctl_ip_vs_drop_packet
,
1548 .maxlen
= sizeof(int),
1550 .proc_handler
= proc_do_defense_mode
,
1552 #ifdef CONFIG_IP_VS_NFCT
1554 .procname
= "conntrack",
1555 .data
= &sysctl_ip_vs_conntrack
,
1556 .maxlen
= sizeof(int),
1558 .proc_handler
= &proc_dointvec
,
1562 .procname
= "secure_tcp",
1563 .data
= &sysctl_ip_vs_secure_tcp
,
1564 .maxlen
= sizeof(int),
1566 .proc_handler
= proc_do_defense_mode
,
1569 .procname
= "snat_reroute",
1570 .data
= &sysctl_ip_vs_snat_reroute
,
1571 .maxlen
= sizeof(int),
1573 .proc_handler
= &proc_dointvec
,
1577 .procname
= "timeout_established",
1578 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_ESTABLISHED
],
1579 .maxlen
= sizeof(int),
1581 .proc_handler
= proc_dointvec_jiffies
,
1584 .procname
= "timeout_synsent",
1585 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYN_SENT
],
1586 .maxlen
= sizeof(int),
1588 .proc_handler
= proc_dointvec_jiffies
,
1591 .procname
= "timeout_synrecv",
1592 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYN_RECV
],
1593 .maxlen
= sizeof(int),
1595 .proc_handler
= proc_dointvec_jiffies
,
1598 .procname
= "timeout_finwait",
1599 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_FIN_WAIT
],
1600 .maxlen
= sizeof(int),
1602 .proc_handler
= proc_dointvec_jiffies
,
1605 .procname
= "timeout_timewait",
1606 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_TIME_WAIT
],
1607 .maxlen
= sizeof(int),
1609 .proc_handler
= proc_dointvec_jiffies
,
1612 .procname
= "timeout_close",
1613 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_CLOSE
],
1614 .maxlen
= sizeof(int),
1616 .proc_handler
= proc_dointvec_jiffies
,
1619 .procname
= "timeout_closewait",
1620 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_CLOSE_WAIT
],
1621 .maxlen
= sizeof(int),
1623 .proc_handler
= proc_dointvec_jiffies
,
1626 .procname
= "timeout_lastack",
1627 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_LAST_ACK
],
1628 .maxlen
= sizeof(int),
1630 .proc_handler
= proc_dointvec_jiffies
,
1633 .procname
= "timeout_listen",
1634 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_LISTEN
],
1635 .maxlen
= sizeof(int),
1637 .proc_handler
= proc_dointvec_jiffies
,
1640 .procname
= "timeout_synack",
1641 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_SYNACK
],
1642 .maxlen
= sizeof(int),
1644 .proc_handler
= proc_dointvec_jiffies
,
1647 .procname
= "timeout_udp",
1648 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_UDP
],
1649 .maxlen
= sizeof(int),
1651 .proc_handler
= proc_dointvec_jiffies
,
1654 .procname
= "timeout_icmp",
1655 .data
= &vs_timeout_table_dos
.timeout
[IP_VS_S_ICMP
],
1656 .maxlen
= sizeof(int),
1658 .proc_handler
= proc_dointvec_jiffies
,
1662 .procname
= "cache_bypass",
1663 .data
= &sysctl_ip_vs_cache_bypass
,
1664 .maxlen
= sizeof(int),
1666 .proc_handler
= proc_dointvec
,
1669 .procname
= "expire_nodest_conn",
1670 .data
= &sysctl_ip_vs_expire_nodest_conn
,
1671 .maxlen
= sizeof(int),
1673 .proc_handler
= proc_dointvec
,
1676 .procname
= "expire_quiescent_template",
1677 .data
= &sysctl_ip_vs_expire_quiescent_template
,
1678 .maxlen
= sizeof(int),
1680 .proc_handler
= proc_dointvec
,
1683 .procname
= "sync_threshold",
1684 .data
= &sysctl_ip_vs_sync_threshold
,
1685 .maxlen
= sizeof(sysctl_ip_vs_sync_threshold
),
1687 .proc_handler
= proc_do_sync_threshold
,
1690 .procname
= "nat_icmp_send",
1691 .data
= &sysctl_ip_vs_nat_icmp_send
,
1692 .maxlen
= sizeof(int),
1694 .proc_handler
= proc_dointvec
,
1699 const struct ctl_path net_vs_ctl_path
[] = {
1700 { .procname
= "net", },
1701 { .procname
= "ipv4", },
1702 { .procname
= "vs", },
1705 EXPORT_SYMBOL_GPL(net_vs_ctl_path
);
1707 static struct ctl_table_header
* sysctl_header
;
1709 #ifdef CONFIG_PROC_FS
1712 struct list_head
*table
;
1717 * Write the contents of the VS rule table to a PROCfs file.
1718 * (It is kept just for backward compatibility)
1720 static inline const char *ip_vs_fwd_name(unsigned flags
)
1722 switch (flags
& IP_VS_CONN_F_FWD_MASK
) {
1723 case IP_VS_CONN_F_LOCALNODE
:
1725 case IP_VS_CONN_F_TUNNEL
:
1727 case IP_VS_CONN_F_DROUTE
:
1735 /* Get the Nth entry in the two lists */
1736 static struct ip_vs_service
*ip_vs_info_array(struct seq_file
*seq
, loff_t pos
)
1738 struct ip_vs_iter
*iter
= seq
->private;
1740 struct ip_vs_service
*svc
;
1742 /* look in hash by protocol */
1743 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1744 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
1746 iter
->table
= ip_vs_svc_table
;
1753 /* keep looking in fwmark */
1754 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
1755 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_list
) {
1757 iter
->table
= ip_vs_svc_fwm_table
;
1767 static void *ip_vs_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1768 __acquires(__ip_vs_svc_lock
)
1771 read_lock_bh(&__ip_vs_svc_lock
);
1772 return *pos
? ip_vs_info_array(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1776 static void *ip_vs_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1778 struct list_head
*e
;
1779 struct ip_vs_iter
*iter
;
1780 struct ip_vs_service
*svc
;
1783 if (v
== SEQ_START_TOKEN
)
1784 return ip_vs_info_array(seq
,0);
1787 iter
= seq
->private;
1789 if (iter
->table
== ip_vs_svc_table
) {
1790 /* next service in table hashed by protocol */
1791 if ((e
= svc
->s_list
.next
) != &ip_vs_svc_table
[iter
->bucket
])
1792 return list_entry(e
, struct ip_vs_service
, s_list
);
1795 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1796 list_for_each_entry(svc
,&ip_vs_svc_table
[iter
->bucket
],
1802 iter
->table
= ip_vs_svc_fwm_table
;
1807 /* next service in hashed by fwmark */
1808 if ((e
= svc
->f_list
.next
) != &ip_vs_svc_fwm_table
[iter
->bucket
])
1809 return list_entry(e
, struct ip_vs_service
, f_list
);
1812 while (++iter
->bucket
< IP_VS_SVC_TAB_SIZE
) {
1813 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[iter
->bucket
],
1821 static void ip_vs_info_seq_stop(struct seq_file
*seq
, void *v
)
1822 __releases(__ip_vs_svc_lock
)
1824 read_unlock_bh(&__ip_vs_svc_lock
);
1828 static int ip_vs_info_seq_show(struct seq_file
*seq
, void *v
)
1830 if (v
== SEQ_START_TOKEN
) {
1832 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1833 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
1835 "Prot LocalAddress:Port Scheduler Flags\n");
1837 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1839 const struct ip_vs_service
*svc
= v
;
1840 const struct ip_vs_iter
*iter
= seq
->private;
1841 const struct ip_vs_dest
*dest
;
1843 if (iter
->table
== ip_vs_svc_table
) {
1844 #ifdef CONFIG_IP_VS_IPV6
1845 if (svc
->af
== AF_INET6
)
1846 seq_printf(seq
, "%s [%pI6]:%04X %s ",
1847 ip_vs_proto_name(svc
->protocol
),
1850 svc
->scheduler
->name
);
1853 seq_printf(seq
, "%s %08X:%04X %s %s ",
1854 ip_vs_proto_name(svc
->protocol
),
1855 ntohl(svc
->addr
.ip
),
1857 svc
->scheduler
->name
,
1858 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
1860 seq_printf(seq
, "FWM %08X %s %s",
1861 svc
->fwmark
, svc
->scheduler
->name
,
1862 (svc
->flags
& IP_VS_SVC_F_ONEPACKET
)?"ops ":"");
1865 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
1866 seq_printf(seq
, "persistent %d %08X\n",
1868 ntohl(svc
->netmask
));
1870 seq_putc(seq
, '\n');
1872 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
1873 #ifdef CONFIG_IP_VS_IPV6
1874 if (dest
->af
== AF_INET6
)
1877 " %-7s %-6d %-10d %-10d\n",
1880 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
1881 atomic_read(&dest
->weight
),
1882 atomic_read(&dest
->activeconns
),
1883 atomic_read(&dest
->inactconns
));
1888 "%-7s %-6d %-10d %-10d\n",
1889 ntohl(dest
->addr
.ip
),
1891 ip_vs_fwd_name(atomic_read(&dest
->conn_flags
)),
1892 atomic_read(&dest
->weight
),
1893 atomic_read(&dest
->activeconns
),
1894 atomic_read(&dest
->inactconns
));
1901 static const struct seq_operations ip_vs_info_seq_ops
= {
1902 .start
= ip_vs_info_seq_start
,
1903 .next
= ip_vs_info_seq_next
,
1904 .stop
= ip_vs_info_seq_stop
,
1905 .show
= ip_vs_info_seq_show
,
1908 static int ip_vs_info_open(struct inode
*inode
, struct file
*file
)
1910 return seq_open_private(file
, &ip_vs_info_seq_ops
,
1911 sizeof(struct ip_vs_iter
));
1914 static const struct file_operations ip_vs_info_fops
= {
1915 .owner
= THIS_MODULE
,
1916 .open
= ip_vs_info_open
,
1918 .llseek
= seq_lseek
,
1919 .release
= seq_release_private
,
1924 struct ip_vs_stats ip_vs_stats
= {
1925 .lock
= __SPIN_LOCK_UNLOCKED(ip_vs_stats
.lock
),
1928 #ifdef CONFIG_PROC_FS
1929 static int ip_vs_stats_show(struct seq_file
*seq
, void *v
)
1932 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1934 " Total Incoming Outgoing Incoming Outgoing\n");
1936 " Conns Packets Packets Bytes Bytes\n");
1938 spin_lock_bh(&ip_vs_stats
.lock
);
1939 seq_printf(seq
, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats
.ustats
.conns
,
1940 ip_vs_stats
.ustats
.inpkts
, ip_vs_stats
.ustats
.outpkts
,
1941 (unsigned long long) ip_vs_stats
.ustats
.inbytes
,
1942 (unsigned long long) ip_vs_stats
.ustats
.outbytes
);
1944 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1946 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1947 seq_printf(seq
,"%8X %8X %8X %16X %16X\n",
1948 ip_vs_stats
.ustats
.cps
,
1949 ip_vs_stats
.ustats
.inpps
,
1950 ip_vs_stats
.ustats
.outpps
,
1951 ip_vs_stats
.ustats
.inbps
,
1952 ip_vs_stats
.ustats
.outbps
);
1953 spin_unlock_bh(&ip_vs_stats
.lock
);
1958 static int ip_vs_stats_seq_open(struct inode
*inode
, struct file
*file
)
1960 return single_open(file
, ip_vs_stats_show
, NULL
);
1963 static const struct file_operations ip_vs_stats_fops
= {
1964 .owner
= THIS_MODULE
,
1965 .open
= ip_vs_stats_seq_open
,
1967 .llseek
= seq_lseek
,
1968 .release
= single_release
,
1974 * Set timeout values for tcp tcpfin udp in the timeout_table.
1976 static int ip_vs_set_timeout(struct ip_vs_timeout_user
*u
)
1978 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1983 #ifdef CONFIG_IP_VS_PROTO_TCP
1984 if (u
->tcp_timeout
) {
1985 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_ESTABLISHED
]
1986 = u
->tcp_timeout
* HZ
;
1989 if (u
->tcp_fin_timeout
) {
1990 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_FIN_WAIT
]
1991 = u
->tcp_fin_timeout
* HZ
;
1995 #ifdef CONFIG_IP_VS_PROTO_UDP
1996 if (u
->udp_timeout
) {
1997 ip_vs_protocol_udp
.timeout_table
[IP_VS_UDP_S_NORMAL
]
1998 = u
->udp_timeout
* HZ
;
2005 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2006 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2007 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2008 sizeof(struct ip_vs_dest_user))
2009 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2010 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2011 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2013 static const unsigned char set_arglen
[SET_CMDID(IP_VS_SO_SET_MAX
)+1] = {
2014 [SET_CMDID(IP_VS_SO_SET_ADD
)] = SERVICE_ARG_LEN
,
2015 [SET_CMDID(IP_VS_SO_SET_EDIT
)] = SERVICE_ARG_LEN
,
2016 [SET_CMDID(IP_VS_SO_SET_DEL
)] = SERVICE_ARG_LEN
,
2017 [SET_CMDID(IP_VS_SO_SET_FLUSH
)] = 0,
2018 [SET_CMDID(IP_VS_SO_SET_ADDDEST
)] = SVCDEST_ARG_LEN
,
2019 [SET_CMDID(IP_VS_SO_SET_DELDEST
)] = SVCDEST_ARG_LEN
,
2020 [SET_CMDID(IP_VS_SO_SET_EDITDEST
)] = SVCDEST_ARG_LEN
,
2021 [SET_CMDID(IP_VS_SO_SET_TIMEOUT
)] = TIMEOUT_ARG_LEN
,
2022 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON
)] = DAEMON_ARG_LEN
,
2023 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON
)] = DAEMON_ARG_LEN
,
2024 [SET_CMDID(IP_VS_SO_SET_ZERO
)] = SERVICE_ARG_LEN
,
2027 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern
*usvc
,
2028 struct ip_vs_service_user
*usvc_compat
)
2031 usvc
->protocol
= usvc_compat
->protocol
;
2032 usvc
->addr
.ip
= usvc_compat
->addr
;
2033 usvc
->port
= usvc_compat
->port
;
2034 usvc
->fwmark
= usvc_compat
->fwmark
;
2036 /* Deep copy of sched_name is not needed here */
2037 usvc
->sched_name
= usvc_compat
->sched_name
;
2039 usvc
->flags
= usvc_compat
->flags
;
2040 usvc
->timeout
= usvc_compat
->timeout
;
2041 usvc
->netmask
= usvc_compat
->netmask
;
2044 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern
*udest
,
2045 struct ip_vs_dest_user
*udest_compat
)
2047 udest
->addr
.ip
= udest_compat
->addr
;
2048 udest
->port
= udest_compat
->port
;
2049 udest
->conn_flags
= udest_compat
->conn_flags
;
2050 udest
->weight
= udest_compat
->weight
;
2051 udest
->u_threshold
= udest_compat
->u_threshold
;
2052 udest
->l_threshold
= udest_compat
->l_threshold
;
2056 do_ip_vs_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
2059 unsigned char arg
[MAX_ARG_LEN
];
2060 struct ip_vs_service_user
*usvc_compat
;
2061 struct ip_vs_service_user_kern usvc
;
2062 struct ip_vs_service
*svc
;
2063 struct ip_vs_dest_user
*udest_compat
;
2064 struct ip_vs_dest_user_kern udest
;
2066 if (!capable(CAP_NET_ADMIN
))
2069 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_SET_MAX
)
2071 if (len
< 0 || len
> MAX_ARG_LEN
)
2073 if (len
!= set_arglen
[SET_CMDID(cmd
)]) {
2074 pr_err("set_ctl: len %u != %u\n",
2075 len
, set_arglen
[SET_CMDID(cmd
)]);
2079 if (copy_from_user(arg
, user
, len
) != 0)
2082 /* increase the module use count */
2083 ip_vs_use_count_inc();
2085 if (mutex_lock_interruptible(&__ip_vs_mutex
)) {
2090 if (cmd
== IP_VS_SO_SET_FLUSH
) {
2091 /* Flush the virtual service */
2092 ret
= ip_vs_flush();
2094 } else if (cmd
== IP_VS_SO_SET_TIMEOUT
) {
2095 /* Set timeout values for (tcp tcpfin udp) */
2096 ret
= ip_vs_set_timeout((struct ip_vs_timeout_user
*)arg
);
2098 } else if (cmd
== IP_VS_SO_SET_STARTDAEMON
) {
2099 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2100 ret
= start_sync_thread(dm
->state
, dm
->mcast_ifn
, dm
->syncid
);
2102 } else if (cmd
== IP_VS_SO_SET_STOPDAEMON
) {
2103 struct ip_vs_daemon_user
*dm
= (struct ip_vs_daemon_user
*)arg
;
2104 ret
= stop_sync_thread(dm
->state
);
2108 usvc_compat
= (struct ip_vs_service_user
*)arg
;
2109 udest_compat
= (struct ip_vs_dest_user
*)(usvc_compat
+ 1);
2111 /* We only use the new structs internally, so copy userspace compat
2112 * structs to extended internal versions */
2113 ip_vs_copy_usvc_compat(&usvc
, usvc_compat
);
2114 ip_vs_copy_udest_compat(&udest
, udest_compat
);
2116 if (cmd
== IP_VS_SO_SET_ZERO
) {
2117 /* if no service address is set, zero counters in all */
2118 if (!usvc
.fwmark
&& !usvc
.addr
.ip
&& !usvc
.port
) {
2119 ret
= ip_vs_zero_all();
2124 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2125 if (usvc
.protocol
!= IPPROTO_TCP
&& usvc
.protocol
!= IPPROTO_UDP
&&
2126 usvc
.protocol
!= IPPROTO_SCTP
) {
2127 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2128 usvc
.protocol
, &usvc
.addr
.ip
,
2129 ntohs(usvc
.port
), usvc
.sched_name
);
2134 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2135 if (usvc
.fwmark
== 0)
2136 svc
= __ip_vs_service_find(usvc
.af
, usvc
.protocol
,
2137 &usvc
.addr
, usvc
.port
);
2139 svc
= __ip_vs_svc_fwm_find(usvc
.af
, usvc
.fwmark
);
2141 if (cmd
!= IP_VS_SO_SET_ADD
2142 && (svc
== NULL
|| svc
->protocol
!= usvc
.protocol
)) {
2148 case IP_VS_SO_SET_ADD
:
2152 ret
= ip_vs_add_service(&usvc
, &svc
);
2154 case IP_VS_SO_SET_EDIT
:
2155 ret
= ip_vs_edit_service(svc
, &usvc
);
2157 case IP_VS_SO_SET_DEL
:
2158 ret
= ip_vs_del_service(svc
);
2162 case IP_VS_SO_SET_ZERO
:
2163 ret
= ip_vs_zero_service(svc
);
2165 case IP_VS_SO_SET_ADDDEST
:
2166 ret
= ip_vs_add_dest(svc
, &udest
);
2168 case IP_VS_SO_SET_EDITDEST
:
2169 ret
= ip_vs_edit_dest(svc
, &udest
);
2171 case IP_VS_SO_SET_DELDEST
:
2172 ret
= ip_vs_del_dest(svc
, &udest
);
2179 mutex_unlock(&__ip_vs_mutex
);
2181 /* decrease the module use count */
2182 ip_vs_use_count_dec();
2189 ip_vs_copy_stats(struct ip_vs_stats_user
*dst
, struct ip_vs_stats
*src
)
2191 spin_lock_bh(&src
->lock
);
2192 memcpy(dst
, &src
->ustats
, sizeof(*dst
));
2193 spin_unlock_bh(&src
->lock
);
2197 ip_vs_copy_service(struct ip_vs_service_entry
*dst
, struct ip_vs_service
*src
)
2199 dst
->protocol
= src
->protocol
;
2200 dst
->addr
= src
->addr
.ip
;
2201 dst
->port
= src
->port
;
2202 dst
->fwmark
= src
->fwmark
;
2203 strlcpy(dst
->sched_name
, src
->scheduler
->name
, sizeof(dst
->sched_name
));
2204 dst
->flags
= src
->flags
;
2205 dst
->timeout
= src
->timeout
/ HZ
;
2206 dst
->netmask
= src
->netmask
;
2207 dst
->num_dests
= src
->num_dests
;
2208 ip_vs_copy_stats(&dst
->stats
, &src
->stats
);
2212 __ip_vs_get_service_entries(const struct ip_vs_get_services
*get
,
2213 struct ip_vs_get_services __user
*uptr
)
2216 struct ip_vs_service
*svc
;
2217 struct ip_vs_service_entry entry
;
2220 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2221 list_for_each_entry(svc
, &ip_vs_svc_table
[idx
], s_list
) {
2222 /* Only expose IPv4 entries to old interface */
2223 if (svc
->af
!= AF_INET
)
2226 if (count
>= get
->num_services
)
2228 memset(&entry
, 0, sizeof(entry
));
2229 ip_vs_copy_service(&entry
, svc
);
2230 if (copy_to_user(&uptr
->entrytable
[count
],
2231 &entry
, sizeof(entry
))) {
2239 for (idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
2240 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[idx
], f_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
))) {
2262 __ip_vs_get_dest_entries(const struct ip_vs_get_dests
*get
,
2263 struct ip_vs_get_dests __user
*uptr
)
2265 struct ip_vs_service
*svc
;
2266 union nf_inet_addr addr
= { .ip
= get
->addr
};
2270 svc
= __ip_vs_svc_fwm_find(AF_INET
, get
->fwmark
);
2272 svc
= __ip_vs_service_find(AF_INET
, get
->protocol
, &addr
,
2277 struct ip_vs_dest
*dest
;
2278 struct ip_vs_dest_entry entry
;
2280 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2281 if (count
>= get
->num_dests
)
2284 entry
.addr
= dest
->addr
.ip
;
2285 entry
.port
= dest
->port
;
2286 entry
.conn_flags
= atomic_read(&dest
->conn_flags
);
2287 entry
.weight
= atomic_read(&dest
->weight
);
2288 entry
.u_threshold
= dest
->u_threshold
;
2289 entry
.l_threshold
= dest
->l_threshold
;
2290 entry
.activeconns
= atomic_read(&dest
->activeconns
);
2291 entry
.inactconns
= atomic_read(&dest
->inactconns
);
2292 entry
.persistconns
= atomic_read(&dest
->persistconns
);
2293 ip_vs_copy_stats(&entry
.stats
, &dest
->stats
);
2294 if (copy_to_user(&uptr
->entrytable
[count
],
2295 &entry
, sizeof(entry
))) {
2307 __ip_vs_get_timeouts(struct ip_vs_timeout_user
*u
)
2309 #ifdef CONFIG_IP_VS_PROTO_TCP
2311 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_ESTABLISHED
] / HZ
;
2312 u
->tcp_fin_timeout
=
2313 ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_FIN_WAIT
] / HZ
;
2315 #ifdef CONFIG_IP_VS_PROTO_UDP
2317 ip_vs_protocol_udp
.timeout_table
[IP_VS_UDP_S_NORMAL
] / HZ
;
2322 #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2323 #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2324 #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2325 #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2326 #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2327 #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2328 #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2330 static const unsigned char get_arglen
[GET_CMDID(IP_VS_SO_GET_MAX
)+1] = {
2331 [GET_CMDID(IP_VS_SO_GET_VERSION
)] = 64,
2332 [GET_CMDID(IP_VS_SO_GET_INFO
)] = GET_INFO_ARG_LEN
,
2333 [GET_CMDID(IP_VS_SO_GET_SERVICES
)] = GET_SERVICES_ARG_LEN
,
2334 [GET_CMDID(IP_VS_SO_GET_SERVICE
)] = GET_SERVICE_ARG_LEN
,
2335 [GET_CMDID(IP_VS_SO_GET_DESTS
)] = GET_DESTS_ARG_LEN
,
2336 [GET_CMDID(IP_VS_SO_GET_TIMEOUT
)] = GET_TIMEOUT_ARG_LEN
,
2337 [GET_CMDID(IP_VS_SO_GET_DAEMON
)] = GET_DAEMON_ARG_LEN
,
2341 do_ip_vs_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
2343 unsigned char arg
[128];
2345 unsigned int copylen
;
2347 if (!capable(CAP_NET_ADMIN
))
2350 if (cmd
< IP_VS_BASE_CTL
|| cmd
> IP_VS_SO_GET_MAX
)
2353 if (*len
< get_arglen
[GET_CMDID(cmd
)]) {
2354 pr_err("get_ctl: len %u < %u\n",
2355 *len
, get_arglen
[GET_CMDID(cmd
)]);
2359 copylen
= get_arglen
[GET_CMDID(cmd
)];
2363 if (copy_from_user(arg
, user
, copylen
) != 0)
2366 if (mutex_lock_interruptible(&__ip_vs_mutex
))
2367 return -ERESTARTSYS
;
2370 case IP_VS_SO_GET_VERSION
:
2374 sprintf(buf
, "IP Virtual Server version %d.%d.%d (size=%d)",
2375 NVERSION(IP_VS_VERSION_CODE
), ip_vs_conn_tab_size
);
2376 if (copy_to_user(user
, buf
, strlen(buf
)+1) != 0) {
2380 *len
= strlen(buf
)+1;
2384 case IP_VS_SO_GET_INFO
:
2386 struct ip_vs_getinfo info
;
2387 info
.version
= IP_VS_VERSION_CODE
;
2388 info
.size
= ip_vs_conn_tab_size
;
2389 info
.num_services
= ip_vs_num_services
;
2390 if (copy_to_user(user
, &info
, sizeof(info
)) != 0)
2395 case IP_VS_SO_GET_SERVICES
:
2397 struct ip_vs_get_services
*get
;
2400 get
= (struct ip_vs_get_services
*)arg
;
2401 size
= sizeof(*get
) +
2402 sizeof(struct ip_vs_service_entry
) * get
->num_services
;
2404 pr_err("length: %u != %u\n", *len
, size
);
2408 ret
= __ip_vs_get_service_entries(get
, user
);
2412 case IP_VS_SO_GET_SERVICE
:
2414 struct ip_vs_service_entry
*entry
;
2415 struct ip_vs_service
*svc
;
2416 union nf_inet_addr addr
;
2418 entry
= (struct ip_vs_service_entry
*)arg
;
2419 addr
.ip
= entry
->addr
;
2421 svc
= __ip_vs_svc_fwm_find(AF_INET
, entry
->fwmark
);
2423 svc
= __ip_vs_service_find(AF_INET
, entry
->protocol
,
2424 &addr
, entry
->port
);
2426 ip_vs_copy_service(entry
, svc
);
2427 if (copy_to_user(user
, entry
, sizeof(*entry
)) != 0)
2434 case IP_VS_SO_GET_DESTS
:
2436 struct ip_vs_get_dests
*get
;
2439 get
= (struct ip_vs_get_dests
*)arg
;
2440 size
= sizeof(*get
) +
2441 sizeof(struct ip_vs_dest_entry
) * get
->num_dests
;
2443 pr_err("length: %u != %u\n", *len
, size
);
2447 ret
= __ip_vs_get_dest_entries(get
, user
);
2451 case IP_VS_SO_GET_TIMEOUT
:
2453 struct ip_vs_timeout_user t
;
2455 __ip_vs_get_timeouts(&t
);
2456 if (copy_to_user(user
, &t
, sizeof(t
)) != 0)
2461 case IP_VS_SO_GET_DAEMON
:
2463 struct ip_vs_daemon_user d
[2];
2465 memset(&d
, 0, sizeof(d
));
2466 if (ip_vs_sync_state
& IP_VS_STATE_MASTER
) {
2467 d
[0].state
= IP_VS_STATE_MASTER
;
2468 strlcpy(d
[0].mcast_ifn
, ip_vs_master_mcast_ifn
, sizeof(d
[0].mcast_ifn
));
2469 d
[0].syncid
= ip_vs_master_syncid
;
2471 if (ip_vs_sync_state
& IP_VS_STATE_BACKUP
) {
2472 d
[1].state
= IP_VS_STATE_BACKUP
;
2473 strlcpy(d
[1].mcast_ifn
, ip_vs_backup_mcast_ifn
, sizeof(d
[1].mcast_ifn
));
2474 d
[1].syncid
= ip_vs_backup_syncid
;
2476 if (copy_to_user(user
, &d
, sizeof(d
)) != 0)
2486 mutex_unlock(&__ip_vs_mutex
);
2491 static struct nf_sockopt_ops ip_vs_sockopts
= {
2493 .set_optmin
= IP_VS_BASE_CTL
,
2494 .set_optmax
= IP_VS_SO_SET_MAX
+1,
2495 .set
= do_ip_vs_set_ctl
,
2496 .get_optmin
= IP_VS_BASE_CTL
,
2497 .get_optmax
= IP_VS_SO_GET_MAX
+1,
2498 .get
= do_ip_vs_get_ctl
,
2499 .owner
= THIS_MODULE
,
2503 * Generic Netlink interface
2506 /* IPVS genetlink family */
2507 static struct genl_family ip_vs_genl_family
= {
2508 .id
= GENL_ID_GENERATE
,
2510 .name
= IPVS_GENL_NAME
,
2511 .version
= IPVS_GENL_VERSION
,
2512 .maxattr
= IPVS_CMD_MAX
,
2515 /* Policy used for first-level command attributes */
2516 static const struct nla_policy ip_vs_cmd_policy
[IPVS_CMD_ATTR_MAX
+ 1] = {
2517 [IPVS_CMD_ATTR_SERVICE
] = { .type
= NLA_NESTED
},
2518 [IPVS_CMD_ATTR_DEST
] = { .type
= NLA_NESTED
},
2519 [IPVS_CMD_ATTR_DAEMON
] = { .type
= NLA_NESTED
},
2520 [IPVS_CMD_ATTR_TIMEOUT_TCP
] = { .type
= NLA_U32
},
2521 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
] = { .type
= NLA_U32
},
2522 [IPVS_CMD_ATTR_TIMEOUT_UDP
] = { .type
= NLA_U32
},
2525 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2526 static const struct nla_policy ip_vs_daemon_policy
[IPVS_DAEMON_ATTR_MAX
+ 1] = {
2527 [IPVS_DAEMON_ATTR_STATE
] = { .type
= NLA_U32
},
2528 [IPVS_DAEMON_ATTR_MCAST_IFN
] = { .type
= NLA_NUL_STRING
,
2529 .len
= IP_VS_IFNAME_MAXLEN
},
2530 [IPVS_DAEMON_ATTR_SYNC_ID
] = { .type
= NLA_U32
},
2533 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2534 static const struct nla_policy ip_vs_svc_policy
[IPVS_SVC_ATTR_MAX
+ 1] = {
2535 [IPVS_SVC_ATTR_AF
] = { .type
= NLA_U16
},
2536 [IPVS_SVC_ATTR_PROTOCOL
] = { .type
= NLA_U16
},
2537 [IPVS_SVC_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2538 .len
= sizeof(union nf_inet_addr
) },
2539 [IPVS_SVC_ATTR_PORT
] = { .type
= NLA_U16
},
2540 [IPVS_SVC_ATTR_FWMARK
] = { .type
= NLA_U32
},
2541 [IPVS_SVC_ATTR_SCHED_NAME
] = { .type
= NLA_NUL_STRING
,
2542 .len
= IP_VS_SCHEDNAME_MAXLEN
},
2543 [IPVS_SVC_ATTR_FLAGS
] = { .type
= NLA_BINARY
,
2544 .len
= sizeof(struct ip_vs_flags
) },
2545 [IPVS_SVC_ATTR_TIMEOUT
] = { .type
= NLA_U32
},
2546 [IPVS_SVC_ATTR_NETMASK
] = { .type
= NLA_U32
},
2547 [IPVS_SVC_ATTR_STATS
] = { .type
= NLA_NESTED
},
2550 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2551 static const struct nla_policy ip_vs_dest_policy
[IPVS_DEST_ATTR_MAX
+ 1] = {
2552 [IPVS_DEST_ATTR_ADDR
] = { .type
= NLA_BINARY
,
2553 .len
= sizeof(union nf_inet_addr
) },
2554 [IPVS_DEST_ATTR_PORT
] = { .type
= NLA_U16
},
2555 [IPVS_DEST_ATTR_FWD_METHOD
] = { .type
= NLA_U32
},
2556 [IPVS_DEST_ATTR_WEIGHT
] = { .type
= NLA_U32
},
2557 [IPVS_DEST_ATTR_U_THRESH
] = { .type
= NLA_U32
},
2558 [IPVS_DEST_ATTR_L_THRESH
] = { .type
= NLA_U32
},
2559 [IPVS_DEST_ATTR_ACTIVE_CONNS
] = { .type
= NLA_U32
},
2560 [IPVS_DEST_ATTR_INACT_CONNS
] = { .type
= NLA_U32
},
2561 [IPVS_DEST_ATTR_PERSIST_CONNS
] = { .type
= NLA_U32
},
2562 [IPVS_DEST_ATTR_STATS
] = { .type
= NLA_NESTED
},
2565 static int ip_vs_genl_fill_stats(struct sk_buff
*skb
, int container_type
,
2566 struct ip_vs_stats
*stats
)
2568 struct nlattr
*nl_stats
= nla_nest_start(skb
, container_type
);
2572 spin_lock_bh(&stats
->lock
);
2574 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_CONNS
, stats
->ustats
.conns
);
2575 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INPKTS
, stats
->ustats
.inpkts
);
2576 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTPKTS
, stats
->ustats
.outpkts
);
2577 NLA_PUT_U64(skb
, IPVS_STATS_ATTR_INBYTES
, stats
->ustats
.inbytes
);
2578 NLA_PUT_U64(skb
, IPVS_STATS_ATTR_OUTBYTES
, stats
->ustats
.outbytes
);
2579 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_CPS
, stats
->ustats
.cps
);
2580 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INPPS
, stats
->ustats
.inpps
);
2581 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTPPS
, stats
->ustats
.outpps
);
2582 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_INBPS
, stats
->ustats
.inbps
);
2583 NLA_PUT_U32(skb
, IPVS_STATS_ATTR_OUTBPS
, stats
->ustats
.outbps
);
2585 spin_unlock_bh(&stats
->lock
);
2587 nla_nest_end(skb
, nl_stats
);
2592 spin_unlock_bh(&stats
->lock
);
2593 nla_nest_cancel(skb
, nl_stats
);
2597 static int ip_vs_genl_fill_service(struct sk_buff
*skb
,
2598 struct ip_vs_service
*svc
)
2600 struct nlattr
*nl_service
;
2601 struct ip_vs_flags flags
= { .flags
= svc
->flags
,
2604 nl_service
= nla_nest_start(skb
, IPVS_CMD_ATTR_SERVICE
);
2608 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_AF
, svc
->af
);
2611 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_FWMARK
, svc
->fwmark
);
2613 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_PROTOCOL
, svc
->protocol
);
2614 NLA_PUT(skb
, IPVS_SVC_ATTR_ADDR
, sizeof(svc
->addr
), &svc
->addr
);
2615 NLA_PUT_U16(skb
, IPVS_SVC_ATTR_PORT
, svc
->port
);
2618 NLA_PUT_STRING(skb
, IPVS_SVC_ATTR_SCHED_NAME
, svc
->scheduler
->name
);
2619 NLA_PUT(skb
, IPVS_SVC_ATTR_FLAGS
, sizeof(flags
), &flags
);
2620 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_TIMEOUT
, svc
->timeout
/ HZ
);
2621 NLA_PUT_U32(skb
, IPVS_SVC_ATTR_NETMASK
, svc
->netmask
);
2623 if (ip_vs_genl_fill_stats(skb
, IPVS_SVC_ATTR_STATS
, &svc
->stats
))
2624 goto nla_put_failure
;
2626 nla_nest_end(skb
, nl_service
);
2631 nla_nest_cancel(skb
, nl_service
);
2635 static int ip_vs_genl_dump_service(struct sk_buff
*skb
,
2636 struct ip_vs_service
*svc
,
2637 struct netlink_callback
*cb
)
2641 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2642 &ip_vs_genl_family
, NLM_F_MULTI
,
2643 IPVS_CMD_NEW_SERVICE
);
2647 if (ip_vs_genl_fill_service(skb
, svc
) < 0)
2648 goto nla_put_failure
;
2650 return genlmsg_end(skb
, hdr
);
2653 genlmsg_cancel(skb
, hdr
);
2657 static int ip_vs_genl_dump_services(struct sk_buff
*skb
,
2658 struct netlink_callback
*cb
)
2661 int start
= cb
->args
[0];
2662 struct ip_vs_service
*svc
;
2664 mutex_lock(&__ip_vs_mutex
);
2665 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2666 list_for_each_entry(svc
, &ip_vs_svc_table
[i
], s_list
) {
2669 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2671 goto nla_put_failure
;
2676 for (i
= 0; i
< IP_VS_SVC_TAB_SIZE
; i
++) {
2677 list_for_each_entry(svc
, &ip_vs_svc_fwm_table
[i
], f_list
) {
2680 if (ip_vs_genl_dump_service(skb
, svc
, cb
) < 0) {
2682 goto nla_put_failure
;
2688 mutex_unlock(&__ip_vs_mutex
);
2694 static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern
*usvc
,
2695 struct nlattr
*nla
, int full_entry
,
2696 struct ip_vs_service
**ret_svc
)
2698 struct nlattr
*attrs
[IPVS_SVC_ATTR_MAX
+ 1];
2699 struct nlattr
*nla_af
, *nla_port
, *nla_fwmark
, *nla_protocol
, *nla_addr
;
2700 struct ip_vs_service
*svc
;
2702 /* Parse mandatory identifying service fields first */
2704 nla_parse_nested(attrs
, IPVS_SVC_ATTR_MAX
, nla
, ip_vs_svc_policy
))
2707 nla_af
= attrs
[IPVS_SVC_ATTR_AF
];
2708 nla_protocol
= attrs
[IPVS_SVC_ATTR_PROTOCOL
];
2709 nla_addr
= attrs
[IPVS_SVC_ATTR_ADDR
];
2710 nla_port
= attrs
[IPVS_SVC_ATTR_PORT
];
2711 nla_fwmark
= attrs
[IPVS_SVC_ATTR_FWMARK
];
2713 if (!(nla_af
&& (nla_fwmark
|| (nla_port
&& nla_protocol
&& nla_addr
))))
2716 memset(usvc
, 0, sizeof(*usvc
));
2718 usvc
->af
= nla_get_u16(nla_af
);
2719 #ifdef CONFIG_IP_VS_IPV6
2720 if (usvc
->af
!= AF_INET
&& usvc
->af
!= AF_INET6
)
2722 if (usvc
->af
!= AF_INET
)
2724 return -EAFNOSUPPORT
;
2727 usvc
->protocol
= IPPROTO_TCP
;
2728 usvc
->fwmark
= nla_get_u32(nla_fwmark
);
2730 usvc
->protocol
= nla_get_u16(nla_protocol
);
2731 nla_memcpy(&usvc
->addr
, nla_addr
, sizeof(usvc
->addr
));
2732 usvc
->port
= nla_get_u16(nla_port
);
2737 svc
= __ip_vs_svc_fwm_find(usvc
->af
, usvc
->fwmark
);
2739 svc
= __ip_vs_service_find(usvc
->af
, usvc
->protocol
,
2740 &usvc
->addr
, usvc
->port
);
2743 /* If a full entry was requested, check for the additional fields */
2745 struct nlattr
*nla_sched
, *nla_flags
, *nla_timeout
,
2747 struct ip_vs_flags flags
;
2749 nla_sched
= attrs
[IPVS_SVC_ATTR_SCHED_NAME
];
2750 nla_flags
= attrs
[IPVS_SVC_ATTR_FLAGS
];
2751 nla_timeout
= attrs
[IPVS_SVC_ATTR_TIMEOUT
];
2752 nla_netmask
= attrs
[IPVS_SVC_ATTR_NETMASK
];
2754 if (!(nla_sched
&& nla_flags
&& nla_timeout
&& nla_netmask
))
2757 nla_memcpy(&flags
, nla_flags
, sizeof(flags
));
2759 /* prefill flags from service if it already exists */
2761 usvc
->flags
= svc
->flags
;
2763 /* set new flags from userland */
2764 usvc
->flags
= (usvc
->flags
& ~flags
.mask
) |
2765 (flags
.flags
& flags
.mask
);
2766 usvc
->sched_name
= nla_data(nla_sched
);
2767 usvc
->timeout
= nla_get_u32(nla_timeout
);
2768 usvc
->netmask
= nla_get_u32(nla_netmask
);
2774 static struct ip_vs_service
*ip_vs_genl_find_service(struct nlattr
*nla
)
2776 struct ip_vs_service_user_kern usvc
;
2777 struct ip_vs_service
*svc
;
2780 ret
= ip_vs_genl_parse_service(&usvc
, nla
, 0, &svc
);
2781 return ret
? ERR_PTR(ret
) : svc
;
2784 static int ip_vs_genl_fill_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
)
2786 struct nlattr
*nl_dest
;
2788 nl_dest
= nla_nest_start(skb
, IPVS_CMD_ATTR_DEST
);
2792 NLA_PUT(skb
, IPVS_DEST_ATTR_ADDR
, sizeof(dest
->addr
), &dest
->addr
);
2793 NLA_PUT_U16(skb
, IPVS_DEST_ATTR_PORT
, dest
->port
);
2795 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_FWD_METHOD
,
2796 atomic_read(&dest
->conn_flags
) & IP_VS_CONN_F_FWD_MASK
);
2797 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_WEIGHT
, atomic_read(&dest
->weight
));
2798 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_U_THRESH
, dest
->u_threshold
);
2799 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_L_THRESH
, dest
->l_threshold
);
2800 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_ACTIVE_CONNS
,
2801 atomic_read(&dest
->activeconns
));
2802 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_INACT_CONNS
,
2803 atomic_read(&dest
->inactconns
));
2804 NLA_PUT_U32(skb
, IPVS_DEST_ATTR_PERSIST_CONNS
,
2805 atomic_read(&dest
->persistconns
));
2807 if (ip_vs_genl_fill_stats(skb
, IPVS_DEST_ATTR_STATS
, &dest
->stats
))
2808 goto nla_put_failure
;
2810 nla_nest_end(skb
, nl_dest
);
2815 nla_nest_cancel(skb
, nl_dest
);
2819 static int ip_vs_genl_dump_dest(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
2820 struct netlink_callback
*cb
)
2824 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2825 &ip_vs_genl_family
, NLM_F_MULTI
,
2830 if (ip_vs_genl_fill_dest(skb
, dest
) < 0)
2831 goto nla_put_failure
;
2833 return genlmsg_end(skb
, hdr
);
2836 genlmsg_cancel(skb
, hdr
);
2840 static int ip_vs_genl_dump_dests(struct sk_buff
*skb
,
2841 struct netlink_callback
*cb
)
2844 int start
= cb
->args
[0];
2845 struct ip_vs_service
*svc
;
2846 struct ip_vs_dest
*dest
;
2847 struct nlattr
*attrs
[IPVS_CMD_ATTR_MAX
+ 1];
2849 mutex_lock(&__ip_vs_mutex
);
2851 /* Try to find the service for which to dump destinations */
2852 if (nlmsg_parse(cb
->nlh
, GENL_HDRLEN
, attrs
,
2853 IPVS_CMD_ATTR_MAX
, ip_vs_cmd_policy
))
2856 svc
= ip_vs_genl_find_service(attrs
[IPVS_CMD_ATTR_SERVICE
]);
2857 if (IS_ERR(svc
) || svc
== NULL
)
2860 /* Dump the destinations */
2861 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
2864 if (ip_vs_genl_dump_dest(skb
, dest
, cb
) < 0) {
2866 goto nla_put_failure
;
2874 mutex_unlock(&__ip_vs_mutex
);
2879 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern
*udest
,
2880 struct nlattr
*nla
, int full_entry
)
2882 struct nlattr
*attrs
[IPVS_DEST_ATTR_MAX
+ 1];
2883 struct nlattr
*nla_addr
, *nla_port
;
2885 /* Parse mandatory identifying destination fields first */
2887 nla_parse_nested(attrs
, IPVS_DEST_ATTR_MAX
, nla
, ip_vs_dest_policy
))
2890 nla_addr
= attrs
[IPVS_DEST_ATTR_ADDR
];
2891 nla_port
= attrs
[IPVS_DEST_ATTR_PORT
];
2893 if (!(nla_addr
&& nla_port
))
2896 memset(udest
, 0, sizeof(*udest
));
2898 nla_memcpy(&udest
->addr
, nla_addr
, sizeof(udest
->addr
));
2899 udest
->port
= nla_get_u16(nla_port
);
2901 /* If a full entry was requested, check for the additional fields */
2903 struct nlattr
*nla_fwd
, *nla_weight
, *nla_u_thresh
,
2906 nla_fwd
= attrs
[IPVS_DEST_ATTR_FWD_METHOD
];
2907 nla_weight
= attrs
[IPVS_DEST_ATTR_WEIGHT
];
2908 nla_u_thresh
= attrs
[IPVS_DEST_ATTR_U_THRESH
];
2909 nla_l_thresh
= attrs
[IPVS_DEST_ATTR_L_THRESH
];
2911 if (!(nla_fwd
&& nla_weight
&& nla_u_thresh
&& nla_l_thresh
))
2914 udest
->conn_flags
= nla_get_u32(nla_fwd
)
2915 & IP_VS_CONN_F_FWD_MASK
;
2916 udest
->weight
= nla_get_u32(nla_weight
);
2917 udest
->u_threshold
= nla_get_u32(nla_u_thresh
);
2918 udest
->l_threshold
= nla_get_u32(nla_l_thresh
);
2924 static int ip_vs_genl_fill_daemon(struct sk_buff
*skb
, __be32 state
,
2925 const char *mcast_ifn
, __be32 syncid
)
2927 struct nlattr
*nl_daemon
;
2929 nl_daemon
= nla_nest_start(skb
, IPVS_CMD_ATTR_DAEMON
);
2933 NLA_PUT_U32(skb
, IPVS_DAEMON_ATTR_STATE
, state
);
2934 NLA_PUT_STRING(skb
, IPVS_DAEMON_ATTR_MCAST_IFN
, mcast_ifn
);
2935 NLA_PUT_U32(skb
, IPVS_DAEMON_ATTR_SYNC_ID
, syncid
);
2937 nla_nest_end(skb
, nl_daemon
);
2942 nla_nest_cancel(skb
, nl_daemon
);
2946 static int ip_vs_genl_dump_daemon(struct sk_buff
*skb
, __be32 state
,
2947 const char *mcast_ifn
, __be32 syncid
,
2948 struct netlink_callback
*cb
)
2951 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
2952 &ip_vs_genl_family
, NLM_F_MULTI
,
2953 IPVS_CMD_NEW_DAEMON
);
2957 if (ip_vs_genl_fill_daemon(skb
, state
, mcast_ifn
, syncid
))
2958 goto nla_put_failure
;
2960 return genlmsg_end(skb
, hdr
);
2963 genlmsg_cancel(skb
, hdr
);
2967 static int ip_vs_genl_dump_daemons(struct sk_buff
*skb
,
2968 struct netlink_callback
*cb
)
2970 mutex_lock(&__ip_vs_mutex
);
2971 if ((ip_vs_sync_state
& IP_VS_STATE_MASTER
) && !cb
->args
[0]) {
2972 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_MASTER
,
2973 ip_vs_master_mcast_ifn
,
2974 ip_vs_master_syncid
, cb
) < 0)
2975 goto nla_put_failure
;
2980 if ((ip_vs_sync_state
& IP_VS_STATE_BACKUP
) && !cb
->args
[1]) {
2981 if (ip_vs_genl_dump_daemon(skb
, IP_VS_STATE_BACKUP
,
2982 ip_vs_backup_mcast_ifn
,
2983 ip_vs_backup_syncid
, cb
) < 0)
2984 goto nla_put_failure
;
2990 mutex_unlock(&__ip_vs_mutex
);
2995 static int ip_vs_genl_new_daemon(struct nlattr
**attrs
)
2997 if (!(attrs
[IPVS_DAEMON_ATTR_STATE
] &&
2998 attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
] &&
2999 attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]))
3002 return start_sync_thread(nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]),
3003 nla_data(attrs
[IPVS_DAEMON_ATTR_MCAST_IFN
]),
3004 nla_get_u32(attrs
[IPVS_DAEMON_ATTR_SYNC_ID
]));
3007 static int ip_vs_genl_del_daemon(struct nlattr
**attrs
)
3009 if (!attrs
[IPVS_DAEMON_ATTR_STATE
])
3012 return stop_sync_thread(nla_get_u32(attrs
[IPVS_DAEMON_ATTR_STATE
]));
3015 static int ip_vs_genl_set_config(struct nlattr
**attrs
)
3017 struct ip_vs_timeout_user t
;
3019 __ip_vs_get_timeouts(&t
);
3021 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
])
3022 t
.tcp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP
]);
3024 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
])
3026 nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
]);
3028 if (attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
])
3029 t
.udp_timeout
= nla_get_u32(attrs
[IPVS_CMD_ATTR_TIMEOUT_UDP
]);
3031 return ip_vs_set_timeout(&t
);
3034 static int ip_vs_genl_set_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3036 struct ip_vs_service
*svc
= NULL
;
3037 struct ip_vs_service_user_kern usvc
;
3038 struct ip_vs_dest_user_kern udest
;
3040 int need_full_svc
= 0, need_full_dest
= 0;
3042 cmd
= info
->genlhdr
->cmd
;
3044 mutex_lock(&__ip_vs_mutex
);
3046 if (cmd
== IPVS_CMD_FLUSH
) {
3047 ret
= ip_vs_flush();
3049 } else if (cmd
== IPVS_CMD_SET_CONFIG
) {
3050 ret
= ip_vs_genl_set_config(info
->attrs
);
3052 } else if (cmd
== IPVS_CMD_NEW_DAEMON
||
3053 cmd
== IPVS_CMD_DEL_DAEMON
) {
3055 struct nlattr
*daemon_attrs
[IPVS_DAEMON_ATTR_MAX
+ 1];
3057 if (!info
->attrs
[IPVS_CMD_ATTR_DAEMON
] ||
3058 nla_parse_nested(daemon_attrs
, IPVS_DAEMON_ATTR_MAX
,
3059 info
->attrs
[IPVS_CMD_ATTR_DAEMON
],
3060 ip_vs_daemon_policy
)) {
3065 if (cmd
== IPVS_CMD_NEW_DAEMON
)
3066 ret
= ip_vs_genl_new_daemon(daemon_attrs
);
3068 ret
= ip_vs_genl_del_daemon(daemon_attrs
);
3070 } else if (cmd
== IPVS_CMD_ZERO
&&
3071 !info
->attrs
[IPVS_CMD_ATTR_SERVICE
]) {
3072 ret
= ip_vs_zero_all();
3076 /* All following commands require a service argument, so check if we
3077 * received a valid one. We need a full service specification when
3078 * adding / editing a service. Only identifying members otherwise. */
3079 if (cmd
== IPVS_CMD_NEW_SERVICE
|| cmd
== IPVS_CMD_SET_SERVICE
)
3082 ret
= ip_vs_genl_parse_service(&usvc
,
3083 info
->attrs
[IPVS_CMD_ATTR_SERVICE
],
3084 need_full_svc
, &svc
);
3088 /* Unless we're adding a new service, the service must already exist */
3089 if ((cmd
!= IPVS_CMD_NEW_SERVICE
) && (svc
== NULL
)) {
3094 /* Destination commands require a valid destination argument. For
3095 * adding / editing a destination, we need a full destination
3097 if (cmd
== IPVS_CMD_NEW_DEST
|| cmd
== IPVS_CMD_SET_DEST
||
3098 cmd
== IPVS_CMD_DEL_DEST
) {
3099 if (cmd
!= IPVS_CMD_DEL_DEST
)
3102 ret
= ip_vs_genl_parse_dest(&udest
,
3103 info
->attrs
[IPVS_CMD_ATTR_DEST
],
3110 case IPVS_CMD_NEW_SERVICE
:
3112 ret
= ip_vs_add_service(&usvc
, &svc
);
3116 case IPVS_CMD_SET_SERVICE
:
3117 ret
= ip_vs_edit_service(svc
, &usvc
);
3119 case IPVS_CMD_DEL_SERVICE
:
3120 ret
= ip_vs_del_service(svc
);
3121 /* do not use svc, it can be freed */
3123 case IPVS_CMD_NEW_DEST
:
3124 ret
= ip_vs_add_dest(svc
, &udest
);
3126 case IPVS_CMD_SET_DEST
:
3127 ret
= ip_vs_edit_dest(svc
, &udest
);
3129 case IPVS_CMD_DEL_DEST
:
3130 ret
= ip_vs_del_dest(svc
, &udest
);
3133 ret
= ip_vs_zero_service(svc
);
3140 mutex_unlock(&__ip_vs_mutex
);
3145 static int ip_vs_genl_get_cmd(struct sk_buff
*skb
, struct genl_info
*info
)
3147 struct sk_buff
*msg
;
3149 int ret
, cmd
, reply_cmd
;
3151 cmd
= info
->genlhdr
->cmd
;
3153 if (cmd
== IPVS_CMD_GET_SERVICE
)
3154 reply_cmd
= IPVS_CMD_NEW_SERVICE
;
3155 else if (cmd
== IPVS_CMD_GET_INFO
)
3156 reply_cmd
= IPVS_CMD_SET_INFO
;
3157 else if (cmd
== IPVS_CMD_GET_CONFIG
)
3158 reply_cmd
= IPVS_CMD_SET_CONFIG
;
3160 pr_err("unknown Generic Netlink command\n");
3164 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3168 mutex_lock(&__ip_vs_mutex
);
3170 reply
= genlmsg_put_reply(msg
, info
, &ip_vs_genl_family
, 0, reply_cmd
);
3172 goto nla_put_failure
;
3175 case IPVS_CMD_GET_SERVICE
:
3177 struct ip_vs_service
*svc
;
3179 svc
= ip_vs_genl_find_service(info
->attrs
[IPVS_CMD_ATTR_SERVICE
]);
3184 ret
= ip_vs_genl_fill_service(msg
, svc
);
3186 goto nla_put_failure
;
3195 case IPVS_CMD_GET_CONFIG
:
3197 struct ip_vs_timeout_user t
;
3199 __ip_vs_get_timeouts(&t
);
3200 #ifdef CONFIG_IP_VS_PROTO_TCP
3201 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP
, t
.tcp_timeout
);
3202 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN
,
3205 #ifdef CONFIG_IP_VS_PROTO_UDP
3206 NLA_PUT_U32(msg
, IPVS_CMD_ATTR_TIMEOUT_UDP
, t
.udp_timeout
);
3212 case IPVS_CMD_GET_INFO
:
3213 NLA_PUT_U32(msg
, IPVS_INFO_ATTR_VERSION
, IP_VS_VERSION_CODE
);
3214 NLA_PUT_U32(msg
, IPVS_INFO_ATTR_CONN_TAB_SIZE
,
3215 ip_vs_conn_tab_size
);
3219 genlmsg_end(msg
, reply
);
3220 ret
= genlmsg_reply(msg
, info
);
3224 pr_err("not enough space in Netlink message\n");
3230 mutex_unlock(&__ip_vs_mutex
);
3236 static struct genl_ops ip_vs_genl_ops
[] __read_mostly
= {
3238 .cmd
= IPVS_CMD_NEW_SERVICE
,
3239 .flags
= GENL_ADMIN_PERM
,
3240 .policy
= ip_vs_cmd_policy
,
3241 .doit
= ip_vs_genl_set_cmd
,
3244 .cmd
= IPVS_CMD_SET_SERVICE
,
3245 .flags
= GENL_ADMIN_PERM
,
3246 .policy
= ip_vs_cmd_policy
,
3247 .doit
= ip_vs_genl_set_cmd
,
3250 .cmd
= IPVS_CMD_DEL_SERVICE
,
3251 .flags
= GENL_ADMIN_PERM
,
3252 .policy
= ip_vs_cmd_policy
,
3253 .doit
= ip_vs_genl_set_cmd
,
3256 .cmd
= IPVS_CMD_GET_SERVICE
,
3257 .flags
= GENL_ADMIN_PERM
,
3258 .doit
= ip_vs_genl_get_cmd
,
3259 .dumpit
= ip_vs_genl_dump_services
,
3260 .policy
= ip_vs_cmd_policy
,
3263 .cmd
= IPVS_CMD_NEW_DEST
,
3264 .flags
= GENL_ADMIN_PERM
,
3265 .policy
= ip_vs_cmd_policy
,
3266 .doit
= ip_vs_genl_set_cmd
,
3269 .cmd
= IPVS_CMD_SET_DEST
,
3270 .flags
= GENL_ADMIN_PERM
,
3271 .policy
= ip_vs_cmd_policy
,
3272 .doit
= ip_vs_genl_set_cmd
,
3275 .cmd
= IPVS_CMD_DEL_DEST
,
3276 .flags
= GENL_ADMIN_PERM
,
3277 .policy
= ip_vs_cmd_policy
,
3278 .doit
= ip_vs_genl_set_cmd
,
3281 .cmd
= IPVS_CMD_GET_DEST
,
3282 .flags
= GENL_ADMIN_PERM
,
3283 .policy
= ip_vs_cmd_policy
,
3284 .dumpit
= ip_vs_genl_dump_dests
,
3287 .cmd
= IPVS_CMD_NEW_DAEMON
,
3288 .flags
= GENL_ADMIN_PERM
,
3289 .policy
= ip_vs_cmd_policy
,
3290 .doit
= ip_vs_genl_set_cmd
,
3293 .cmd
= IPVS_CMD_DEL_DAEMON
,
3294 .flags
= GENL_ADMIN_PERM
,
3295 .policy
= ip_vs_cmd_policy
,
3296 .doit
= ip_vs_genl_set_cmd
,
3299 .cmd
= IPVS_CMD_GET_DAEMON
,
3300 .flags
= GENL_ADMIN_PERM
,
3301 .dumpit
= ip_vs_genl_dump_daemons
,
3304 .cmd
= IPVS_CMD_SET_CONFIG
,
3305 .flags
= GENL_ADMIN_PERM
,
3306 .policy
= ip_vs_cmd_policy
,
3307 .doit
= ip_vs_genl_set_cmd
,
3310 .cmd
= IPVS_CMD_GET_CONFIG
,
3311 .flags
= GENL_ADMIN_PERM
,
3312 .doit
= ip_vs_genl_get_cmd
,
3315 .cmd
= IPVS_CMD_GET_INFO
,
3316 .flags
= GENL_ADMIN_PERM
,
3317 .doit
= ip_vs_genl_get_cmd
,
3320 .cmd
= IPVS_CMD_ZERO
,
3321 .flags
= GENL_ADMIN_PERM
,
3322 .policy
= ip_vs_cmd_policy
,
3323 .doit
= ip_vs_genl_set_cmd
,
3326 .cmd
= IPVS_CMD_FLUSH
,
3327 .flags
= GENL_ADMIN_PERM
,
3328 .doit
= ip_vs_genl_set_cmd
,
3332 static int __init
ip_vs_genl_register(void)
3334 return genl_register_family_with_ops(&ip_vs_genl_family
,
3335 ip_vs_genl_ops
, ARRAY_SIZE(ip_vs_genl_ops
));
3338 static void ip_vs_genl_unregister(void)
3340 genl_unregister_family(&ip_vs_genl_family
);
3343 /* End of Generic Netlink interface definitions */
3346 int __init
ip_vs_control_init(void)
3353 ret
= nf_register_sockopt(&ip_vs_sockopts
);
3355 pr_err("cannot register sockopt.\n");
3359 ret
= ip_vs_genl_register();
3361 pr_err("cannot register Generic Netlink interface.\n");
3362 nf_unregister_sockopt(&ip_vs_sockopts
);
3366 proc_net_fops_create(&init_net
, "ip_vs", 0, &ip_vs_info_fops
);
3367 proc_net_fops_create(&init_net
, "ip_vs_stats",0, &ip_vs_stats_fops
);
3369 sysctl_header
= register_sysctl_paths(net_vs_ctl_path
, vs_vars
);
3371 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3372 for(idx
= 0; idx
< IP_VS_SVC_TAB_SIZE
; idx
++) {
3373 INIT_LIST_HEAD(&ip_vs_svc_table
[idx
]);
3374 INIT_LIST_HEAD(&ip_vs_svc_fwm_table
[idx
]);
3376 for(idx
= 0; idx
< IP_VS_RTAB_SIZE
; idx
++) {
3377 INIT_LIST_HEAD(&ip_vs_rtable
[idx
]);
3380 ip_vs_new_estimator(&ip_vs_stats
);
3382 /* Hook the defense timer */
3383 schedule_delayed_work(&defense_work
, DEFENSE_TIMER_PERIOD
);
3390 void ip_vs_control_cleanup(void)
3393 ip_vs_trash_cleanup();
3394 cancel_rearming_delayed_work(&defense_work
);
3395 cancel_work_sync(&defense_work
.work
);
3396 ip_vs_kill_estimator(&ip_vs_stats
);
3397 unregister_sysctl_table(sysctl_header
);
3398 proc_net_remove(&init_net
, "ip_vs_stats");
3399 proc_net_remove(&init_net
, "ip_vs");
3400 ip_vs_genl_unregister();
3401 nf_unregister_sockopt(&ip_vs_sockopts
);