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_RWLOCK(__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 write_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 write_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_INACTIVE;
770 /* check if local node and update the flags */
771 #ifdef CONFIG_IP_VS_IPV6
772 if (svc->af == AF_INET6) {
773 if (__ip_vs_addr_is_local_v6(&udest->addr.in6)) {
774 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
775 | IP_VS_CONN_F_LOCALNODE;
779 if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
780 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
781 | IP_VS_CONN_F_LOCALNODE;
784 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
785 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
786 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
789 * Put the real service in ip_vs_rtable if not present.
790 * For now only for NAT!
792 write_lock_bh(&__ip_vs_rs_lock);
794 write_unlock_bh(&__ip_vs_rs_lock);
796 atomic_set(&dest->conn_flags, conn_flags);
798 /* bind the service */
800 __ip_vs_bind_svc(dest, svc);
802 if (dest->svc != svc) {
803 __ip_vs_unbind_svc(dest);
804 ip_vs_zero_stats(&dest->stats);
805 __ip_vs_bind_svc(dest, svc);
809 /* set the dest status flags */
810 dest->flags |= IP_VS_DEST_F_AVAILABLE;
812 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
813 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
814 dest->u_threshold = udest->u_threshold;
815 dest->l_threshold = udest->l_threshold;
820 * Create a destination for the given service
823 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
824 struct ip_vs_dest **dest_p)
826 struct ip_vs_dest *dest;
831 #ifdef CONFIG_IP_VS_IPV6
832 if (svc->af == AF_INET6) {
833 atype = ipv6_addr_type(&udest->addr.in6);
834 if ((!(atype & IPV6_ADDR_UNICAST) ||
835 atype & IPV6_ADDR_LINKLOCAL) &&
836 !__ip_vs_addr_is_local_v6(&udest->addr.in6))
841 atype = inet_addr_type(&init_net, udest->addr.ip);
842 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
846 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
848 pr_err("%s(): no memory.\n", __func__);
853 dest->protocol = svc->protocol;
854 dest->vaddr = svc->addr;
855 dest->vport = svc->port;
856 dest->vfwmark = svc->fwmark;
857 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
858 dest->port = udest->port;
860 atomic_set(&dest->activeconns, 0);
861 atomic_set(&dest->inactconns, 0);
862 atomic_set(&dest->persistconns, 0);
863 atomic_set(&dest->refcnt, 0);
865 INIT_LIST_HEAD(&dest->d_list);
866 spin_lock_init(&dest->dst_lock);
867 spin_lock_init(&dest->stats.lock);
868 __ip_vs_update_dest(svc, dest, udest);
869 ip_vs_new_estimator(&dest->stats);
879 * Add a destination into an existing service
882 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
884 struct ip_vs_dest *dest;
885 union nf_inet_addr daddr;
886 __be16 dport = udest->port;
891 if (udest->weight < 0) {
892 pr_err("%s(): server weight less than zero\n", __func__);
896 if (udest->l_threshold > udest->u_threshold) {
897 pr_err("%s(): lower threshold is higher than upper threshold\n",
902 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
905 * Check if the dest already exists in the list
907 dest = ip_vs_lookup_dest(svc, &daddr, dport);
910 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
915 * Check if the dest already exists in the trash and
916 * is from the same service
918 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
921 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
922 "dest->refcnt=%d, service %u/%s:%u\n",
923 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
924 atomic_read(&dest->refcnt),
926 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
929 __ip_vs_update_dest(svc, dest, udest);
932 * Get the destination from the trash
934 list_del(&dest->n_list);
936 ip_vs_new_estimator(&dest->stats);
938 write_lock_bh(&__ip_vs_svc_lock);
941 * Wait until all other svc users go away.
943 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
945 list_add(&dest->n_list, &svc->destinations);
948 /* call the update_service function of its scheduler */
949 if (svc->scheduler->update_service)
950 svc->scheduler->update_service(svc);
952 write_unlock_bh(&__ip_vs_svc_lock);
957 * Allocate and initialize the dest structure
959 ret = ip_vs_new_dest(svc, udest, &dest);
965 * Add the dest entry into the list
967 atomic_inc(&dest->refcnt);
969 write_lock_bh(&__ip_vs_svc_lock);
972 * Wait until all other svc users go away.
974 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
976 list_add(&dest->n_list, &svc->destinations);
979 /* call the update_service function of its scheduler */
980 if (svc->scheduler->update_service)
981 svc->scheduler->update_service(svc);
983 write_unlock_bh(&__ip_vs_svc_lock);
992 * Edit a destination in the given service
995 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
997 struct ip_vs_dest *dest;
998 union nf_inet_addr daddr;
999 __be16 dport = udest->port;
1003 if (udest->weight < 0) {
1004 pr_err("%s(): server weight less than zero\n", __func__);
1008 if (udest->l_threshold > udest->u_threshold) {
1009 pr_err("%s(): lower threshold is higher than upper threshold\n",
1014 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
1017 * Lookup the destination list
1019 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1022 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1026 __ip_vs_update_dest(svc, dest, udest);
1028 write_lock_bh(&__ip_vs_svc_lock);
1030 /* Wait until all other svc users go away */
1031 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1033 /* call the update_service, because server weight may be changed */
1034 if (svc->scheduler->update_service)
1035 svc->scheduler->update_service(svc);
1037 write_unlock_bh(&__ip_vs_svc_lock);
1046 * Delete a destination (must be already unlinked from the service)
1048 static void __ip_vs_del_dest(struct ip_vs_dest *dest)
1050 ip_vs_kill_estimator(&dest->stats);
1053 * Remove it from the d-linked list with the real services.
1055 write_lock_bh(&__ip_vs_rs_lock);
1056 ip_vs_rs_unhash(dest);
1057 write_unlock_bh(&__ip_vs_rs_lock);
1060 * Decrease the refcnt of the dest, and free the dest
1061 * if nobody refers to it (refcnt=0). Otherwise, throw
1062 * the destination into the trash.
1064 if (atomic_dec_and_test(&dest->refcnt)) {
1065 ip_vs_dst_reset(dest);
1066 /* simply decrease svc->refcnt here, let the caller check
1067 and release the service if nobody refers to it.
1068 Only user context can release destination and service,
1069 and only one user context can update virtual service at a
1070 time, so the operation here is OK */
1071 atomic_dec(&dest->svc->refcnt);
1074 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1075 "dest->refcnt=%d\n",
1076 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1078 atomic_read(&dest->refcnt));
1079 list_add(&dest->n_list, &ip_vs_dest_trash);
1080 atomic_inc(&dest->refcnt);
1086 * Unlink a destination from the given service
1088 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1089 struct ip_vs_dest *dest,
1092 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1095 * Remove it from the d-linked destination list.
1097 list_del(&dest->n_list);
1101 * Call the update_service function of its scheduler
1103 if (svcupd && svc->scheduler->update_service)
1104 svc->scheduler->update_service(svc);
1109 * Delete a destination server in the given service
1112 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1114 struct ip_vs_dest *dest;
1115 __be16 dport = udest->port;
1119 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
1122 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1126 write_lock_bh(&__ip_vs_svc_lock);
1129 * Wait until all other svc users go away.
1131 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1134 * Unlink dest from the service
1136 __ip_vs_unlink_dest(svc, dest, 1);
1138 write_unlock_bh(&__ip_vs_svc_lock);
1141 * Delete the destination
1143 __ip_vs_del_dest(dest);
1152 * Add a service into the service hash table
1155 ip_vs_add_service(struct ip_vs_service_user_kern *u,
1156 struct ip_vs_service **svc_p)
1159 struct ip_vs_scheduler *sched = NULL;
1160 struct ip_vs_service *svc = NULL;
1162 /* increase the module use count */
1163 ip_vs_use_count_inc();
1165 /* Lookup the scheduler by 'u->sched_name' */
1166 sched = ip_vs_scheduler_get(u->sched_name);
1167 if (sched == NULL) {
1168 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1173 #ifdef CONFIG_IP_VS_IPV6
1174 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1180 svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
1182 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1187 /* I'm the first user of the service */
1188 atomic_set(&svc->usecnt, 1);
1189 atomic_set(&svc->refcnt, 0);
1192 svc->protocol = u->protocol;
1193 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1194 svc->port = u->port;
1195 svc->fwmark = u->fwmark;
1196 svc->flags = u->flags;
1197 svc->timeout = u->timeout * HZ;
1198 svc->netmask = u->netmask;
1200 INIT_LIST_HEAD(&svc->destinations);
1201 rwlock_init(&svc->sched_lock);
1202 spin_lock_init(&svc->stats.lock);
1204 /* Bind the scheduler */
1205 ret = ip_vs_bind_scheduler(svc, sched);
1210 /* Update the virtual service counters */
1211 if (svc->port == FTPPORT)
1212 atomic_inc(&ip_vs_ftpsvc_counter);
1213 else if (svc->port == 0)
1214 atomic_inc(&ip_vs_nullsvc_counter);
1216 ip_vs_new_estimator(&svc->stats);
1218 /* Count only IPv4 services for old get/setsockopt interface */
1219 if (svc->af == AF_INET)
1220 ip_vs_num_services++;
1222 /* Hash the service into the service table */
1223 write_lock_bh(&__ip_vs_svc_lock);
1224 ip_vs_svc_hash(svc);
1225 write_unlock_bh(&__ip_vs_svc_lock);
1233 ip_vs_unbind_scheduler(svc);
1236 ip_vs_app_inc_put(svc->inc);
1241 ip_vs_scheduler_put(sched);
1244 /* decrease the module use count */
1245 ip_vs_use_count_dec();
1252 * Edit a service and bind it with a new scheduler
1255 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1257 struct ip_vs_scheduler *sched, *old_sched;
1261 * Lookup the scheduler, by 'u->sched_name'
1263 sched = ip_vs_scheduler_get(u->sched_name);
1264 if (sched == NULL) {
1265 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1270 #ifdef CONFIG_IP_VS_IPV6
1271 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1277 write_lock_bh(&__ip_vs_svc_lock);
1280 * Wait until all other svc users go away.
1282 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1285 * Set the flags and timeout value
1287 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1288 svc->timeout = u->timeout * HZ;
1289 svc->netmask = u->netmask;
1291 old_sched = svc->scheduler;
1292 if (sched != old_sched) {
1294 * Unbind the old scheduler
1296 if ((ret = ip_vs_unbind_scheduler(svc))) {
1302 * Bind the new scheduler
1304 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1306 * If ip_vs_bind_scheduler fails, restore the old
1308 * The main reason of failure is out of memory.
1310 * The question is if the old scheduler can be
1311 * restored all the time. TODO: if it cannot be
1312 * restored some time, we must delete the service,
1313 * otherwise the system may crash.
1315 ip_vs_bind_scheduler(svc, old_sched);
1322 write_unlock_bh(&__ip_vs_svc_lock);
1323 #ifdef CONFIG_IP_VS_IPV6
1328 ip_vs_scheduler_put(old_sched);
1335 * Delete a service from the service list
1336 * - The service must be unlinked, unlocked and not referenced!
1337 * - We are called under _bh lock
1339 static void __ip_vs_del_service(struct ip_vs_service *svc)
1341 struct ip_vs_dest *dest, *nxt;
1342 struct ip_vs_scheduler *old_sched;
1344 /* Count only IPv4 services for old get/setsockopt interface */
1345 if (svc->af == AF_INET)
1346 ip_vs_num_services--;
1348 ip_vs_kill_estimator(&svc->stats);
1350 /* Unbind scheduler */
1351 old_sched = svc->scheduler;
1352 ip_vs_unbind_scheduler(svc);
1354 ip_vs_scheduler_put(old_sched);
1356 /* Unbind app inc */
1358 ip_vs_app_inc_put(svc->inc);
1363 * Unlink the whole destination list
1365 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1366 __ip_vs_unlink_dest(svc, dest, 0);
1367 __ip_vs_del_dest(dest);
1371 * Update the virtual service counters
1373 if (svc->port == FTPPORT)
1374 atomic_dec(&ip_vs_ftpsvc_counter);
1375 else if (svc->port == 0)
1376 atomic_dec(&ip_vs_nullsvc_counter);
1379 * Free the service if nobody refers to it
1381 if (atomic_read(&svc->refcnt) == 0)
1384 /* decrease the module use count */
1385 ip_vs_use_count_dec();
1389 * Delete a service from the service list
1391 static int ip_vs_del_service(struct ip_vs_service *svc)
1397 * Unhash it from the service table
1399 write_lock_bh(&__ip_vs_svc_lock);
1401 ip_vs_svc_unhash(svc);
1404 * Wait until all the svc users go away.
1406 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1408 __ip_vs_del_service(svc);
1410 write_unlock_bh(&__ip_vs_svc_lock);
1417 * Flush all the virtual services
1419 static int ip_vs_flush(void)
1422 struct ip_vs_service *svc, *nxt;
1425 * Flush the service table hashed by <protocol,addr,port>
1427 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1428 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
1429 write_lock_bh(&__ip_vs_svc_lock);
1430 ip_vs_svc_unhash(svc);
1432 * Wait until all the svc users go away.
1434 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1435 __ip_vs_del_service(svc);
1436 write_unlock_bh(&__ip_vs_svc_lock);
1441 * Flush the service table hashed by fwmark
1443 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1444 list_for_each_entry_safe(svc, nxt,
1445 &ip_vs_svc_fwm_table[idx], f_list) {
1446 write_lock_bh(&__ip_vs_svc_lock);
1447 ip_vs_svc_unhash(svc);
1449 * Wait until all the svc users go away.
1451 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1452 __ip_vs_del_service(svc);
1453 write_unlock_bh(&__ip_vs_svc_lock);
1462 * Zero counters in a service or all services
1464 static int ip_vs_zero_service(struct ip_vs_service *svc)
1466 struct ip_vs_dest *dest;
1468 write_lock_bh(&__ip_vs_svc_lock);
1469 list_for_each_entry(dest, &svc->destinations, n_list) {
1470 ip_vs_zero_stats(&dest->stats);
1472 ip_vs_zero_stats(&svc->stats);
1473 write_unlock_bh(&__ip_vs_svc_lock);
1477 static int ip_vs_zero_all(void)
1480 struct ip_vs_service *svc;
1482 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1483 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1484 ip_vs_zero_service(svc);
1488 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1489 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1490 ip_vs_zero_service(svc);
1494 ip_vs_zero_stats(&ip_vs_stats);
1500 proc_do_defense_mode(ctl_table *table, int write,
1501 void __user *buffer, size_t *lenp, loff_t *ppos)
1503 int *valp = table->data;
1507 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1508 if (write && (*valp != val)) {
1509 if ((*valp < 0) || (*valp > 3)) {
1510 /* Restore the correct value */
1513 update_defense_level();
1521 proc_do_sync_threshold(ctl_table *table, int write,
1522 void __user *buffer, size_t *lenp, loff_t *ppos)
1524 int *valp = table->data;
1528 /* backup the value first */
1529 memcpy(val, valp, sizeof(val));
1531 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1532 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1533 /* Restore the correct value */
1534 memcpy(valp, val, sizeof(val));
1541 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1544 static struct ctl_table vs_vars[] = {
1546 .procname = "amemthresh",
1547 .data = &sysctl_ip_vs_amemthresh,
1548 .maxlen = sizeof(int),
1550 .proc_handler = proc_dointvec,
1552 #ifdef CONFIG_IP_VS_DEBUG
1554 .procname = "debug_level",
1555 .data = &sysctl_ip_vs_debug_level,
1556 .maxlen = sizeof(int),
1558 .proc_handler = proc_dointvec,
1562 .procname = "am_droprate",
1563 .data = &sysctl_ip_vs_am_droprate,
1564 .maxlen = sizeof(int),
1566 .proc_handler = proc_dointvec,
1569 .procname = "drop_entry",
1570 .data = &sysctl_ip_vs_drop_entry,
1571 .maxlen = sizeof(int),
1573 .proc_handler = proc_do_defense_mode,
1576 .procname = "drop_packet",
1577 .data = &sysctl_ip_vs_drop_packet,
1578 .maxlen = sizeof(int),
1580 .proc_handler = proc_do_defense_mode,
1583 .procname = "secure_tcp",
1584 .data = &sysctl_ip_vs_secure_tcp,
1585 .maxlen = sizeof(int),
1587 .proc_handler = proc_do_defense_mode,
1591 .procname = "timeout_established",
1592 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1593 .maxlen = sizeof(int),
1595 .proc_handler = proc_dointvec_jiffies,
1598 .procname = "timeout_synsent",
1599 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1600 .maxlen = sizeof(int),
1602 .proc_handler = proc_dointvec_jiffies,
1605 .procname = "timeout_synrecv",
1606 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1607 .maxlen = sizeof(int),
1609 .proc_handler = proc_dointvec_jiffies,
1612 .procname = "timeout_finwait",
1613 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1614 .maxlen = sizeof(int),
1616 .proc_handler = proc_dointvec_jiffies,
1619 .procname = "timeout_timewait",
1620 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1621 .maxlen = sizeof(int),
1623 .proc_handler = proc_dointvec_jiffies,
1626 .procname = "timeout_close",
1627 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1628 .maxlen = sizeof(int),
1630 .proc_handler = proc_dointvec_jiffies,
1633 .procname = "timeout_closewait",
1634 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1635 .maxlen = sizeof(int),
1637 .proc_handler = proc_dointvec_jiffies,
1640 .procname = "timeout_lastack",
1641 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1642 .maxlen = sizeof(int),
1644 .proc_handler = proc_dointvec_jiffies,
1647 .procname = "timeout_listen",
1648 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1649 .maxlen = sizeof(int),
1651 .proc_handler = proc_dointvec_jiffies,
1654 .procname = "timeout_synack",
1655 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1656 .maxlen = sizeof(int),
1658 .proc_handler = proc_dointvec_jiffies,
1661 .procname = "timeout_udp",
1662 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1663 .maxlen = sizeof(int),
1665 .proc_handler = proc_dointvec_jiffies,
1668 .procname = "timeout_icmp",
1669 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1670 .maxlen = sizeof(int),
1672 .proc_handler = proc_dointvec_jiffies,
1676 .procname = "cache_bypass",
1677 .data = &sysctl_ip_vs_cache_bypass,
1678 .maxlen = sizeof(int),
1680 .proc_handler = proc_dointvec,
1683 .procname = "expire_nodest_conn",
1684 .data = &sysctl_ip_vs_expire_nodest_conn,
1685 .maxlen = sizeof(int),
1687 .proc_handler = proc_dointvec,
1690 .procname = "expire_quiescent_template",
1691 .data = &sysctl_ip_vs_expire_quiescent_template,
1692 .maxlen = sizeof(int),
1694 .proc_handler = proc_dointvec,
1697 .procname = "sync_threshold",
1698 .data = &sysctl_ip_vs_sync_threshold,
1699 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1701 .proc_handler = proc_do_sync_threshold,
1704 .procname = "nat_icmp_send",
1705 .data = &sysctl_ip_vs_nat_icmp_send,
1706 .maxlen = sizeof(int),
1708 .proc_handler = proc_dointvec,
1713 const struct ctl_path net_vs_ctl_path[] = {
1714 { .procname = "net", },
1715 { .procname = "ipv4", },
1716 { .procname = "vs", },
1719 EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1721 static struct ctl_table_header * sysctl_header;
1723 #ifdef CONFIG_PROC_FS
1726 struct list_head *table;
1731 * Write the contents of the VS rule table to a PROCfs file.
1732 * (It is kept just for backward compatibility)
1734 static inline const char *ip_vs_fwd_name(unsigned flags)
1736 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1737 case IP_VS_CONN_F_LOCALNODE:
1739 case IP_VS_CONN_F_TUNNEL:
1741 case IP_VS_CONN_F_DROUTE:
1749 /* Get the Nth entry in the two lists */
1750 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1752 struct ip_vs_iter *iter = seq->private;
1754 struct ip_vs_service *svc;
1756 /* look in hash by protocol */
1757 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1758 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1760 iter->table = ip_vs_svc_table;
1767 /* keep looking in fwmark */
1768 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1769 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1771 iter->table = ip_vs_svc_fwm_table;
1781 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1782 __acquires(__ip_vs_svc_lock)
1785 read_lock_bh(&__ip_vs_svc_lock);
1786 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1790 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1792 struct list_head *e;
1793 struct ip_vs_iter *iter;
1794 struct ip_vs_service *svc;
1797 if (v == SEQ_START_TOKEN)
1798 return ip_vs_info_array(seq,0);
1801 iter = seq->private;
1803 if (iter->table == ip_vs_svc_table) {
1804 /* next service in table hashed by protocol */
1805 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1806 return list_entry(e, struct ip_vs_service, s_list);
1809 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1810 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1816 iter->table = ip_vs_svc_fwm_table;
1821 /* next service in hashed by fwmark */
1822 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1823 return list_entry(e, struct ip_vs_service, f_list);
1826 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1827 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1835 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1836 __releases(__ip_vs_svc_lock)
1838 read_unlock_bh(&__ip_vs_svc_lock);
1842 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1844 if (v == SEQ_START_TOKEN) {
1846 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1847 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1849 "Prot LocalAddress:Port Scheduler Flags\n");
1851 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1853 const struct ip_vs_service *svc = v;
1854 const struct ip_vs_iter *iter = seq->private;
1855 const struct ip_vs_dest *dest;
1857 if (iter->table == ip_vs_svc_table) {
1858 #ifdef CONFIG_IP_VS_IPV6
1859 if (svc->af == AF_INET6)
1860 seq_printf(seq, "%s [%pI6]:%04X %s ",
1861 ip_vs_proto_name(svc->protocol),
1864 svc->scheduler->name);
1867 seq_printf(seq, "%s %08X:%04X %s %s ",
1868 ip_vs_proto_name(svc->protocol),
1869 ntohl(svc->addr.ip),
1871 svc->scheduler->name,
1872 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1874 seq_printf(seq, "FWM %08X %s %s",
1875 svc->fwmark, svc->scheduler->name,
1876 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
1879 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1880 seq_printf(seq, "persistent %d %08X\n",
1882 ntohl(svc->netmask));
1884 seq_putc(seq, '\n');
1886 list_for_each_entry(dest, &svc->destinations, n_list) {
1887 #ifdef CONFIG_IP_VS_IPV6
1888 if (dest->af == AF_INET6)
1891 " %-7s %-6d %-10d %-10d\n",
1894 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1895 atomic_read(&dest->weight),
1896 atomic_read(&dest->activeconns),
1897 atomic_read(&dest->inactconns));
1902 "%-7s %-6d %-10d %-10d\n",
1903 ntohl(dest->addr.ip),
1905 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1906 atomic_read(&dest->weight),
1907 atomic_read(&dest->activeconns),
1908 atomic_read(&dest->inactconns));
1915 static const struct seq_operations ip_vs_info_seq_ops = {
1916 .start = ip_vs_info_seq_start,
1917 .next = ip_vs_info_seq_next,
1918 .stop = ip_vs_info_seq_stop,
1919 .show = ip_vs_info_seq_show,
1922 static int ip_vs_info_open(struct inode *inode, struct file *file)
1924 return seq_open_private(file, &ip_vs_info_seq_ops,
1925 sizeof(struct ip_vs_iter));
1928 static const struct file_operations ip_vs_info_fops = {
1929 .owner = THIS_MODULE,
1930 .open = ip_vs_info_open,
1932 .llseek = seq_lseek,
1933 .release = seq_release_private,
1938 struct ip_vs_stats ip_vs_stats = {
1939 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1942 #ifdef CONFIG_PROC_FS
1943 static int ip_vs_stats_show(struct seq_file *seq, void *v)
1946 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1948 " Total Incoming Outgoing Incoming Outgoing\n");
1950 " Conns Packets Packets Bytes Bytes\n");
1952 spin_lock_bh(&ip_vs_stats.lock);
1953 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1954 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1955 (unsigned long long) ip_vs_stats.ustats.inbytes,
1956 (unsigned long long) ip_vs_stats.ustats.outbytes);
1958 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1960 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1961 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
1962 ip_vs_stats.ustats.cps,
1963 ip_vs_stats.ustats.inpps,
1964 ip_vs_stats.ustats.outpps,
1965 ip_vs_stats.ustats.inbps,
1966 ip_vs_stats.ustats.outbps);
1967 spin_unlock_bh(&ip_vs_stats.lock);
1972 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1974 return single_open(file, ip_vs_stats_show, NULL);
1977 static const struct file_operations ip_vs_stats_fops = {
1978 .owner = THIS_MODULE,
1979 .open = ip_vs_stats_seq_open,
1981 .llseek = seq_lseek,
1982 .release = single_release,
1988 * Set timeout values for tcp tcpfin udp in the timeout_table.
1990 static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1992 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1997 #ifdef CONFIG_IP_VS_PROTO_TCP
1998 if (u->tcp_timeout) {
1999 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
2000 = u->tcp_timeout * HZ;
2003 if (u->tcp_fin_timeout) {
2004 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
2005 = u->tcp_fin_timeout * HZ;
2009 #ifdef CONFIG_IP_VS_PROTO_UDP
2010 if (u->udp_timeout) {
2011 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
2012 = u->udp_timeout * HZ;
2019 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2020 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2021 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2022 sizeof(struct ip_vs_dest_user))
2023 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2024 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2025 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2027 static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
2028 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2029 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2030 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2031 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2032 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2033 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2034 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2035 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2036 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2037 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2038 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2041 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2042 struct ip_vs_service_user *usvc_compat)
2045 usvc->protocol = usvc_compat->protocol;
2046 usvc->addr.ip = usvc_compat->addr;
2047 usvc->port = usvc_compat->port;
2048 usvc->fwmark = usvc_compat->fwmark;
2050 /* Deep copy of sched_name is not needed here */
2051 usvc->sched_name = usvc_compat->sched_name;
2053 usvc->flags = usvc_compat->flags;
2054 usvc->timeout = usvc_compat->timeout;
2055 usvc->netmask = usvc_compat->netmask;
2058 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2059 struct ip_vs_dest_user *udest_compat)
2061 udest->addr.ip = udest_compat->addr;
2062 udest->port = udest_compat->port;
2063 udest->conn_flags = udest_compat->conn_flags;
2064 udest->weight = udest_compat->weight;
2065 udest->u_threshold = udest_compat->u_threshold;
2066 udest->l_threshold = udest_compat->l_threshold;
2070 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2073 unsigned char arg[MAX_ARG_LEN];
2074 struct ip_vs_service_user *usvc_compat;
2075 struct ip_vs_service_user_kern usvc;
2076 struct ip_vs_service *svc;
2077 struct ip_vs_dest_user *udest_compat;
2078 struct ip_vs_dest_user_kern udest;
2080 if (!capable(CAP_NET_ADMIN))
2083 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2085 if (len < 0 || len > MAX_ARG_LEN)
2087 if (len != set_arglen[SET_CMDID(cmd)]) {
2088 pr_err("set_ctl: len %u != %u\n",
2089 len, set_arglen[SET_CMDID(cmd)]);
2093 if (copy_from_user(arg, user, len) != 0)
2096 /* increase the module use count */
2097 ip_vs_use_count_inc();
2099 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
2104 if (cmd == IP_VS_SO_SET_FLUSH) {
2105 /* Flush the virtual service */
2106 ret = ip_vs_flush();
2108 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2109 /* Set timeout values for (tcp tcpfin udp) */
2110 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2112 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2113 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2114 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2116 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2117 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2118 ret = stop_sync_thread(dm->state);
2122 usvc_compat = (struct ip_vs_service_user *)arg;
2123 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2125 /* We only use the new structs internally, so copy userspace compat
2126 * structs to extended internal versions */
2127 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2128 ip_vs_copy_udest_compat(&udest, udest_compat);
2130 if (cmd == IP_VS_SO_SET_ZERO) {
2131 /* if no service address is set, zero counters in all */
2132 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2133 ret = ip_vs_zero_all();
2138 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2139 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2140 usvc.protocol != IPPROTO_SCTP) {
2141 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2142 usvc.protocol, &usvc.addr.ip,
2143 ntohs(usvc.port), usvc.sched_name);
2148 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2149 if (usvc.fwmark == 0)
2150 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
2151 &usvc.addr, usvc.port);
2153 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2155 if (cmd != IP_VS_SO_SET_ADD
2156 && (svc == NULL || svc->protocol != usvc.protocol)) {
2158 goto out_drop_service;
2162 case IP_VS_SO_SET_ADD:
2166 ret = ip_vs_add_service(&usvc, &svc);
2168 case IP_VS_SO_SET_EDIT:
2169 ret = ip_vs_edit_service(svc, &usvc);
2171 case IP_VS_SO_SET_DEL:
2172 ret = ip_vs_del_service(svc);
2176 case IP_VS_SO_SET_ZERO:
2177 ret = ip_vs_zero_service(svc);
2179 case IP_VS_SO_SET_ADDDEST:
2180 ret = ip_vs_add_dest(svc, &udest);
2182 case IP_VS_SO_SET_EDITDEST:
2183 ret = ip_vs_edit_dest(svc, &udest);
2185 case IP_VS_SO_SET_DELDEST:
2186 ret = ip_vs_del_dest(svc, &udest);
2194 ip_vs_service_put(svc);
2197 mutex_unlock(&__ip_vs_mutex);
2199 /* decrease the module use count */
2200 ip_vs_use_count_dec();
2207 ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2209 spin_lock_bh(&src->lock);
2210 memcpy(dst, &src->ustats, sizeof(*dst));
2211 spin_unlock_bh(&src->lock);
2215 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2217 dst->protocol = src->protocol;
2218 dst->addr = src->addr.ip;
2219 dst->port = src->port;
2220 dst->fwmark = src->fwmark;
2221 strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
2222 dst->flags = src->flags;
2223 dst->timeout = src->timeout / HZ;
2224 dst->netmask = src->netmask;
2225 dst->num_dests = src->num_dests;
2226 ip_vs_copy_stats(&dst->stats, &src->stats);
2230 __ip_vs_get_service_entries(const struct ip_vs_get_services *get,
2231 struct ip_vs_get_services __user *uptr)
2234 struct ip_vs_service *svc;
2235 struct ip_vs_service_entry entry;
2238 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2239 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2240 /* Only expose IPv4 entries to old interface */
2241 if (svc->af != AF_INET)
2244 if (count >= get->num_services)
2246 memset(&entry, 0, sizeof(entry));
2247 ip_vs_copy_service(&entry, svc);
2248 if (copy_to_user(&uptr->entrytable[count],
2249 &entry, sizeof(entry))) {
2257 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2258 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2259 /* Only expose IPv4 entries to old interface */
2260 if (svc->af != AF_INET)
2263 if (count >= get->num_services)
2265 memset(&entry, 0, sizeof(entry));
2266 ip_vs_copy_service(&entry, svc);
2267 if (copy_to_user(&uptr->entrytable[count],
2268 &entry, sizeof(entry))) {
2280 __ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
2281 struct ip_vs_get_dests __user *uptr)
2283 struct ip_vs_service *svc;
2284 union nf_inet_addr addr = { .ip = get->addr };
2288 svc = __ip_vs_svc_fwm_get(AF_INET, get->fwmark);
2290 svc = __ip_vs_service_get(AF_INET, get->protocol, &addr,
2295 struct ip_vs_dest *dest;
2296 struct ip_vs_dest_entry entry;
2298 list_for_each_entry(dest, &svc->destinations, n_list) {
2299 if (count >= get->num_dests)
2302 entry.addr = dest->addr.ip;
2303 entry.port = dest->port;
2304 entry.conn_flags = atomic_read(&dest->conn_flags);
2305 entry.weight = atomic_read(&dest->weight);
2306 entry.u_threshold = dest->u_threshold;
2307 entry.l_threshold = dest->l_threshold;
2308 entry.activeconns = atomic_read(&dest->activeconns);
2309 entry.inactconns = atomic_read(&dest->inactconns);
2310 entry.persistconns = atomic_read(&dest->persistconns);
2311 ip_vs_copy_stats(&entry.stats, &dest->stats);
2312 if (copy_to_user(&uptr->entrytable[count],
2313 &entry, sizeof(entry))) {
2319 ip_vs_service_put(svc);
2326 __ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
2328 #ifdef CONFIG_IP_VS_PROTO_TCP
2330 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2331 u->tcp_fin_timeout =
2332 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2334 #ifdef CONFIG_IP_VS_PROTO_UDP
2336 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2341 #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2342 #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2343 #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2344 #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2345 #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2346 #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2347 #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2349 static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
2350 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2351 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2352 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2353 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2354 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2355 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2356 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2360 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2362 unsigned char arg[128];
2364 unsigned int copylen;
2366 if (!capable(CAP_NET_ADMIN))
2369 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2372 if (*len < get_arglen[GET_CMDID(cmd)]) {
2373 pr_err("get_ctl: len %u < %u\n",
2374 *len, get_arglen[GET_CMDID(cmd)]);
2378 copylen = get_arglen[GET_CMDID(cmd)];
2382 if (copy_from_user(arg, user, copylen) != 0)
2385 if (mutex_lock_interruptible(&__ip_vs_mutex))
2386 return -ERESTARTSYS;
2389 case IP_VS_SO_GET_VERSION:
2393 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2394 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
2395 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2399 *len = strlen(buf)+1;
2403 case IP_VS_SO_GET_INFO:
2405 struct ip_vs_getinfo info;
2406 info.version = IP_VS_VERSION_CODE;
2407 info.size = ip_vs_conn_tab_size;
2408 info.num_services = ip_vs_num_services;
2409 if (copy_to_user(user, &info, sizeof(info)) != 0)
2414 case IP_VS_SO_GET_SERVICES:
2416 struct ip_vs_get_services *get;
2419 get = (struct ip_vs_get_services *)arg;
2420 size = sizeof(*get) +
2421 sizeof(struct ip_vs_service_entry) * get->num_services;
2423 pr_err("length: %u != %u\n", *len, size);
2427 ret = __ip_vs_get_service_entries(get, user);
2431 case IP_VS_SO_GET_SERVICE:
2433 struct ip_vs_service_entry *entry;
2434 struct ip_vs_service *svc;
2435 union nf_inet_addr addr;
2437 entry = (struct ip_vs_service_entry *)arg;
2438 addr.ip = entry->addr;
2440 svc = __ip_vs_svc_fwm_get(AF_INET, entry->fwmark);
2442 svc = __ip_vs_service_get(AF_INET, entry->protocol,
2443 &addr, entry->port);
2445 ip_vs_copy_service(entry, svc);
2446 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2448 ip_vs_service_put(svc);
2454 case IP_VS_SO_GET_DESTS:
2456 struct ip_vs_get_dests *get;
2459 get = (struct ip_vs_get_dests *)arg;
2460 size = sizeof(*get) +
2461 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2463 pr_err("length: %u != %u\n", *len, size);
2467 ret = __ip_vs_get_dest_entries(get, user);
2471 case IP_VS_SO_GET_TIMEOUT:
2473 struct ip_vs_timeout_user t;
2475 __ip_vs_get_timeouts(&t);
2476 if (copy_to_user(user, &t, sizeof(t)) != 0)
2481 case IP_VS_SO_GET_DAEMON:
2483 struct ip_vs_daemon_user d[2];
2485 memset(&d, 0, sizeof(d));
2486 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2487 d[0].state = IP_VS_STATE_MASTER;
2488 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
2489 d[0].syncid = ip_vs_master_syncid;
2491 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2492 d[1].state = IP_VS_STATE_BACKUP;
2493 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
2494 d[1].syncid = ip_vs_backup_syncid;
2496 if (copy_to_user(user, &d, sizeof(d)) != 0)
2506 mutex_unlock(&__ip_vs_mutex);
2511 static struct nf_sockopt_ops ip_vs_sockopts = {
2513 .set_optmin = IP_VS_BASE_CTL,
2514 .set_optmax = IP_VS_SO_SET_MAX+1,
2515 .set = do_ip_vs_set_ctl,
2516 .get_optmin = IP_VS_BASE_CTL,
2517 .get_optmax = IP_VS_SO_GET_MAX+1,
2518 .get = do_ip_vs_get_ctl,
2519 .owner = THIS_MODULE,
2523 * Generic Netlink interface
2526 /* IPVS genetlink family */
2527 static struct genl_family ip_vs_genl_family = {
2528 .id = GENL_ID_GENERATE,
2530 .name = IPVS_GENL_NAME,
2531 .version = IPVS_GENL_VERSION,
2532 .maxattr = IPVS_CMD_MAX,
2535 /* Policy used for first-level command attributes */
2536 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2537 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2538 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2539 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2540 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2541 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2542 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2545 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2546 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2547 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2548 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2549 .len = IP_VS_IFNAME_MAXLEN },
2550 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2553 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2554 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2555 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2556 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2557 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2558 .len = sizeof(union nf_inet_addr) },
2559 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2560 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2561 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2562 .len = IP_VS_SCHEDNAME_MAXLEN },
2563 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2564 .len = sizeof(struct ip_vs_flags) },
2565 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2566 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2567 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2570 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2571 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2572 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2573 .len = sizeof(union nf_inet_addr) },
2574 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2575 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2576 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2577 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2578 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2579 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2580 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2581 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2582 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2585 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2586 struct ip_vs_stats *stats)
2588 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2592 spin_lock_bh(&stats->lock);
2594 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2595 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2596 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2597 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2598 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2599 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2600 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2601 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2602 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2603 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
2605 spin_unlock_bh(&stats->lock);
2607 nla_nest_end(skb, nl_stats);
2612 spin_unlock_bh(&stats->lock);
2613 nla_nest_cancel(skb, nl_stats);
2617 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2618 struct ip_vs_service *svc)
2620 struct nlattr *nl_service;
2621 struct ip_vs_flags flags = { .flags = svc->flags,
2624 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2628 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
2631 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2633 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2634 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2635 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2638 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2639 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2640 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2641 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2643 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2644 goto nla_put_failure;
2646 nla_nest_end(skb, nl_service);
2651 nla_nest_cancel(skb, nl_service);
2655 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2656 struct ip_vs_service *svc,
2657 struct netlink_callback *cb)
2661 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2662 &ip_vs_genl_family, NLM_F_MULTI,
2663 IPVS_CMD_NEW_SERVICE);
2667 if (ip_vs_genl_fill_service(skb, svc) < 0)
2668 goto nla_put_failure;
2670 return genlmsg_end(skb, hdr);
2673 genlmsg_cancel(skb, hdr);
2677 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2678 struct netlink_callback *cb)
2681 int start = cb->args[0];
2682 struct ip_vs_service *svc;
2684 mutex_lock(&__ip_vs_mutex);
2685 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2686 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2689 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2691 goto nla_put_failure;
2696 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2697 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2700 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2702 goto nla_put_failure;
2708 mutex_unlock(&__ip_vs_mutex);
2714 static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
2715 struct nlattr *nla, int full_entry)
2717 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2718 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2720 /* Parse mandatory identifying service fields first */
2722 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2725 nla_af = attrs[IPVS_SVC_ATTR_AF];
2726 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2727 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2728 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2729 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2731 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2734 memset(usvc, 0, sizeof(*usvc));
2736 usvc->af = nla_get_u16(nla_af);
2737 #ifdef CONFIG_IP_VS_IPV6
2738 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2740 if (usvc->af != AF_INET)
2742 return -EAFNOSUPPORT;
2745 usvc->protocol = IPPROTO_TCP;
2746 usvc->fwmark = nla_get_u32(nla_fwmark);
2748 usvc->protocol = nla_get_u16(nla_protocol);
2749 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2750 usvc->port = nla_get_u16(nla_port);
2754 /* If a full entry was requested, check for the additional fields */
2756 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2758 struct ip_vs_flags flags;
2759 struct ip_vs_service *svc;
2761 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2762 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2763 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2764 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2766 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2769 nla_memcpy(&flags, nla_flags, sizeof(flags));
2771 /* prefill flags from service if it already exists */
2773 svc = __ip_vs_svc_fwm_get(usvc->af, usvc->fwmark);
2775 svc = __ip_vs_service_get(usvc->af, usvc->protocol,
2776 &usvc->addr, usvc->port);
2778 usvc->flags = svc->flags;
2779 ip_vs_service_put(svc);
2783 /* set new flags from userland */
2784 usvc->flags = (usvc->flags & ~flags.mask) |
2785 (flags.flags & flags.mask);
2786 usvc->sched_name = nla_data(nla_sched);
2787 usvc->timeout = nla_get_u32(nla_timeout);
2788 usvc->netmask = nla_get_u32(nla_netmask);
2794 static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2796 struct ip_vs_service_user_kern usvc;
2799 ret = ip_vs_genl_parse_service(&usvc, nla, 0);
2801 return ERR_PTR(ret);
2804 return __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2806 return __ip_vs_service_get(usvc.af, usvc.protocol,
2807 &usvc.addr, usvc.port);
2810 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2812 struct nlattr *nl_dest;
2814 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2818 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2819 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2821 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2822 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2823 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2824 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2825 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2826 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2827 atomic_read(&dest->activeconns));
2828 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2829 atomic_read(&dest->inactconns));
2830 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2831 atomic_read(&dest->persistconns));
2833 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2834 goto nla_put_failure;
2836 nla_nest_end(skb, nl_dest);
2841 nla_nest_cancel(skb, nl_dest);
2845 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2846 struct netlink_callback *cb)
2850 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2851 &ip_vs_genl_family, NLM_F_MULTI,
2856 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2857 goto nla_put_failure;
2859 return genlmsg_end(skb, hdr);
2862 genlmsg_cancel(skb, hdr);
2866 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2867 struct netlink_callback *cb)
2870 int start = cb->args[0];
2871 struct ip_vs_service *svc;
2872 struct ip_vs_dest *dest;
2873 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2875 mutex_lock(&__ip_vs_mutex);
2877 /* Try to find the service for which to dump destinations */
2878 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2879 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2882 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2883 if (IS_ERR(svc) || svc == NULL)
2886 /* Dump the destinations */
2887 list_for_each_entry(dest, &svc->destinations, n_list) {
2890 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2892 goto nla_put_failure;
2898 ip_vs_service_put(svc);
2901 mutex_unlock(&__ip_vs_mutex);
2906 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
2907 struct nlattr *nla, int full_entry)
2909 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2910 struct nlattr *nla_addr, *nla_port;
2912 /* Parse mandatory identifying destination fields first */
2914 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2917 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2918 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2920 if (!(nla_addr && nla_port))
2923 memset(udest, 0, sizeof(*udest));
2925 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2926 udest->port = nla_get_u16(nla_port);
2928 /* If a full entry was requested, check for the additional fields */
2930 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2933 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2934 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2935 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2936 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2938 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2941 udest->conn_flags = nla_get_u32(nla_fwd)
2942 & IP_VS_CONN_F_FWD_MASK;
2943 udest->weight = nla_get_u32(nla_weight);
2944 udest->u_threshold = nla_get_u32(nla_u_thresh);
2945 udest->l_threshold = nla_get_u32(nla_l_thresh);
2951 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2952 const char *mcast_ifn, __be32 syncid)
2954 struct nlattr *nl_daemon;
2956 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2960 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2961 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2962 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2964 nla_nest_end(skb, nl_daemon);
2969 nla_nest_cancel(skb, nl_daemon);
2973 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2974 const char *mcast_ifn, __be32 syncid,
2975 struct netlink_callback *cb)
2978 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2979 &ip_vs_genl_family, NLM_F_MULTI,
2980 IPVS_CMD_NEW_DAEMON);
2984 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2985 goto nla_put_failure;
2987 return genlmsg_end(skb, hdr);
2990 genlmsg_cancel(skb, hdr);
2994 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2995 struct netlink_callback *cb)
2997 mutex_lock(&__ip_vs_mutex);
2998 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2999 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3000 ip_vs_master_mcast_ifn,
3001 ip_vs_master_syncid, cb) < 0)
3002 goto nla_put_failure;
3007 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3008 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3009 ip_vs_backup_mcast_ifn,
3010 ip_vs_backup_syncid, cb) < 0)
3011 goto nla_put_failure;
3017 mutex_unlock(&__ip_vs_mutex);
3022 static int ip_vs_genl_new_daemon(struct nlattr **attrs)
3024 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3025 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3026 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3029 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3030 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3031 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3034 static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3036 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3039 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3042 static int ip_vs_genl_set_config(struct nlattr **attrs)
3044 struct ip_vs_timeout_user t;
3046 __ip_vs_get_timeouts(&t);
3048 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3049 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3051 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3053 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3055 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3056 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3058 return ip_vs_set_timeout(&t);
3061 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3063 struct ip_vs_service *svc = NULL;
3064 struct ip_vs_service_user_kern usvc;
3065 struct ip_vs_dest_user_kern udest;
3067 int need_full_svc = 0, need_full_dest = 0;
3069 cmd = info->genlhdr->cmd;
3071 mutex_lock(&__ip_vs_mutex);
3073 if (cmd == IPVS_CMD_FLUSH) {
3074 ret = ip_vs_flush();
3076 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3077 ret = ip_vs_genl_set_config(info->attrs);
3079 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3080 cmd == IPVS_CMD_DEL_DAEMON) {
3082 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3084 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3085 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3086 info->attrs[IPVS_CMD_ATTR_DAEMON],
3087 ip_vs_daemon_policy)) {
3092 if (cmd == IPVS_CMD_NEW_DAEMON)
3093 ret = ip_vs_genl_new_daemon(daemon_attrs);
3095 ret = ip_vs_genl_del_daemon(daemon_attrs);
3097 } else if (cmd == IPVS_CMD_ZERO &&
3098 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3099 ret = ip_vs_zero_all();
3103 /* All following commands require a service argument, so check if we
3104 * received a valid one. We need a full service specification when
3105 * adding / editing a service. Only identifying members otherwise. */
3106 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3109 ret = ip_vs_genl_parse_service(&usvc,
3110 info->attrs[IPVS_CMD_ATTR_SERVICE],
3115 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3116 if (usvc.fwmark == 0)
3117 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
3118 &usvc.addr, usvc.port);
3120 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
3122 /* Unless we're adding a new service, the service must already exist */
3123 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3128 /* Destination commands require a valid destination argument. For
3129 * adding / editing a destination, we need a full destination
3131 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3132 cmd == IPVS_CMD_DEL_DEST) {
3133 if (cmd != IPVS_CMD_DEL_DEST)
3136 ret = ip_vs_genl_parse_dest(&udest,
3137 info->attrs[IPVS_CMD_ATTR_DEST],
3144 case IPVS_CMD_NEW_SERVICE:
3146 ret = ip_vs_add_service(&usvc, &svc);
3150 case IPVS_CMD_SET_SERVICE:
3151 ret = ip_vs_edit_service(svc, &usvc);
3153 case IPVS_CMD_DEL_SERVICE:
3154 ret = ip_vs_del_service(svc);
3156 case IPVS_CMD_NEW_DEST:
3157 ret = ip_vs_add_dest(svc, &udest);
3159 case IPVS_CMD_SET_DEST:
3160 ret = ip_vs_edit_dest(svc, &udest);
3162 case IPVS_CMD_DEL_DEST:
3163 ret = ip_vs_del_dest(svc, &udest);
3166 ret = ip_vs_zero_service(svc);
3174 ip_vs_service_put(svc);
3175 mutex_unlock(&__ip_vs_mutex);
3180 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3182 struct sk_buff *msg;
3184 int ret, cmd, reply_cmd;
3186 cmd = info->genlhdr->cmd;
3188 if (cmd == IPVS_CMD_GET_SERVICE)
3189 reply_cmd = IPVS_CMD_NEW_SERVICE;
3190 else if (cmd == IPVS_CMD_GET_INFO)
3191 reply_cmd = IPVS_CMD_SET_INFO;
3192 else if (cmd == IPVS_CMD_GET_CONFIG)
3193 reply_cmd = IPVS_CMD_SET_CONFIG;
3195 pr_err("unknown Generic Netlink command\n");
3199 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3203 mutex_lock(&__ip_vs_mutex);
3205 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3207 goto nla_put_failure;
3210 case IPVS_CMD_GET_SERVICE:
3212 struct ip_vs_service *svc;
3214 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3219 ret = ip_vs_genl_fill_service(msg, svc);
3220 ip_vs_service_put(svc);
3222 goto nla_put_failure;
3231 case IPVS_CMD_GET_CONFIG:
3233 struct ip_vs_timeout_user t;
3235 __ip_vs_get_timeouts(&t);
3236 #ifdef CONFIG_IP_VS_PROTO_TCP
3237 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3238 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3241 #ifdef CONFIG_IP_VS_PROTO_UDP
3242 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3248 case IPVS_CMD_GET_INFO:
3249 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3250 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3251 ip_vs_conn_tab_size);
3255 genlmsg_end(msg, reply);
3256 ret = genlmsg_reply(msg, info);
3260 pr_err("not enough space in Netlink message\n");
3266 mutex_unlock(&__ip_vs_mutex);
3272 static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3274 .cmd = IPVS_CMD_NEW_SERVICE,
3275 .flags = GENL_ADMIN_PERM,
3276 .policy = ip_vs_cmd_policy,
3277 .doit = ip_vs_genl_set_cmd,
3280 .cmd = IPVS_CMD_SET_SERVICE,
3281 .flags = GENL_ADMIN_PERM,
3282 .policy = ip_vs_cmd_policy,
3283 .doit = ip_vs_genl_set_cmd,
3286 .cmd = IPVS_CMD_DEL_SERVICE,
3287 .flags = GENL_ADMIN_PERM,
3288 .policy = ip_vs_cmd_policy,
3289 .doit = ip_vs_genl_set_cmd,
3292 .cmd = IPVS_CMD_GET_SERVICE,
3293 .flags = GENL_ADMIN_PERM,
3294 .doit = ip_vs_genl_get_cmd,
3295 .dumpit = ip_vs_genl_dump_services,
3296 .policy = ip_vs_cmd_policy,
3299 .cmd = IPVS_CMD_NEW_DEST,
3300 .flags = GENL_ADMIN_PERM,
3301 .policy = ip_vs_cmd_policy,
3302 .doit = ip_vs_genl_set_cmd,
3305 .cmd = IPVS_CMD_SET_DEST,
3306 .flags = GENL_ADMIN_PERM,
3307 .policy = ip_vs_cmd_policy,
3308 .doit = ip_vs_genl_set_cmd,
3311 .cmd = IPVS_CMD_DEL_DEST,
3312 .flags = GENL_ADMIN_PERM,
3313 .policy = ip_vs_cmd_policy,
3314 .doit = ip_vs_genl_set_cmd,
3317 .cmd = IPVS_CMD_GET_DEST,
3318 .flags = GENL_ADMIN_PERM,
3319 .policy = ip_vs_cmd_policy,
3320 .dumpit = ip_vs_genl_dump_dests,
3323 .cmd = IPVS_CMD_NEW_DAEMON,
3324 .flags = GENL_ADMIN_PERM,
3325 .policy = ip_vs_cmd_policy,
3326 .doit = ip_vs_genl_set_cmd,
3329 .cmd = IPVS_CMD_DEL_DAEMON,
3330 .flags = GENL_ADMIN_PERM,
3331 .policy = ip_vs_cmd_policy,
3332 .doit = ip_vs_genl_set_cmd,
3335 .cmd = IPVS_CMD_GET_DAEMON,
3336 .flags = GENL_ADMIN_PERM,
3337 .dumpit = ip_vs_genl_dump_daemons,
3340 .cmd = IPVS_CMD_SET_CONFIG,
3341 .flags = GENL_ADMIN_PERM,
3342 .policy = ip_vs_cmd_policy,
3343 .doit = ip_vs_genl_set_cmd,
3346 .cmd = IPVS_CMD_GET_CONFIG,
3347 .flags = GENL_ADMIN_PERM,
3348 .doit = ip_vs_genl_get_cmd,
3351 .cmd = IPVS_CMD_GET_INFO,
3352 .flags = GENL_ADMIN_PERM,
3353 .doit = ip_vs_genl_get_cmd,
3356 .cmd = IPVS_CMD_ZERO,
3357 .flags = GENL_ADMIN_PERM,
3358 .policy = ip_vs_cmd_policy,
3359 .doit = ip_vs_genl_set_cmd,
3362 .cmd = IPVS_CMD_FLUSH,
3363 .flags = GENL_ADMIN_PERM,
3364 .doit = ip_vs_genl_set_cmd,
3368 static int __init ip_vs_genl_register(void)
3370 return genl_register_family_with_ops(&ip_vs_genl_family,
3371 ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops));
3374 static void ip_vs_genl_unregister(void)
3376 genl_unregister_family(&ip_vs_genl_family);
3379 /* End of Generic Netlink interface definitions */
3382 int __init ip_vs_control_init(void)
3389 ret = nf_register_sockopt(&ip_vs_sockopts);
3391 pr_err("cannot register sockopt.\n");
3395 ret = ip_vs_genl_register();
3397 pr_err("cannot register Generic Netlink interface.\n");
3398 nf_unregister_sockopt(&ip_vs_sockopts);
3402 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3403 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
3405 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
3407 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3408 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3409 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3410 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3412 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3413 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3416 ip_vs_new_estimator(&ip_vs_stats);
3418 /* Hook the defense timer */
3419 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3426 void ip_vs_control_cleanup(void)
3429 ip_vs_trash_cleanup();
3430 cancel_rearming_delayed_work(&defense_work);
3431 cancel_work_sync(&defense_work.work);
3432 ip_vs_kill_estimator(&ip_vs_stats);
3433 unregister_sysctl_table(sysctl_header);
3434 proc_net_remove(&init_net, "ip_vs_stats");
3435 proc_net_remove(&init_net, "ip_vs");
3436 ip_vs_genl_unregister();
3437 nf_unregister_sockopt(&ip_vs_sockopts);