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 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/capability.h>
26 #include <linux/sysctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/workqueue.h>
29 #include <linux/swap.h>
30 #include <linux/seq_file.h>
32 #include <linux/netfilter.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/mutex.h>
36 #include <net/net_namespace.h>
38 #ifdef CONFIG_IP_VS_IPV6
40 #include <net/ip6_route.h>
42 #include <net/route.h>
44 #include <net/genetlink.h>
46 #include <asm/uaccess.h>
48 #include <net/ip_vs.h>
50 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
51 static DEFINE_MUTEX(__ip_vs_mutex);
53 /* lock for service table */
54 static DEFINE_RWLOCK(__ip_vs_svc_lock);
56 /* lock for table with the real services */
57 static DEFINE_RWLOCK(__ip_vs_rs_lock);
59 /* lock for state and timeout tables */
60 static DEFINE_RWLOCK(__ip_vs_securetcp_lock);
62 /* lock for drop entry handling */
63 static DEFINE_SPINLOCK(__ip_vs_dropentry_lock);
65 /* lock for drop packet handling */
66 static DEFINE_SPINLOCK(__ip_vs_droppacket_lock);
68 /* 1/rate drop and drop-entry variables */
69 int ip_vs_drop_rate = 0;
70 int ip_vs_drop_counter = 0;
71 static atomic_t ip_vs_dropentry = ATOMIC_INIT(0);
73 /* number of virtual services */
74 static int ip_vs_num_services = 0;
76 /* sysctl variables */
77 static int sysctl_ip_vs_drop_entry = 0;
78 static int sysctl_ip_vs_drop_packet = 0;
79 static int sysctl_ip_vs_secure_tcp = 0;
80 static int sysctl_ip_vs_amemthresh = 1024;
81 static int sysctl_ip_vs_am_droprate = 10;
82 int sysctl_ip_vs_cache_bypass = 0;
83 int sysctl_ip_vs_expire_nodest_conn = 0;
84 int sysctl_ip_vs_expire_quiescent_template = 0;
85 int sysctl_ip_vs_sync_threshold[2] = { 3, 50 };
86 int sysctl_ip_vs_nat_icmp_send = 0;
89 #ifdef CONFIG_IP_VS_DEBUG
90 static int sysctl_ip_vs_debug_level = 0;
92 int ip_vs_get_debug_level(void)
94 return sysctl_ip_vs_debug_level;
98 #ifdef CONFIG_IP_VS_IPV6
99 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
100 static int __ip_vs_addr_is_local_v6(const struct in6_addr *addr)
108 .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
111 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
112 if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
119 * update_defense_level is called from keventd and from sysctl,
120 * so it needs to protect itself from softirqs
122 static void update_defense_level(void)
125 static int old_secure_tcp = 0;
130 /* we only count free and buffered memory (in pages) */
132 availmem = i.freeram + i.bufferram;
133 /* however in linux 2.5 the i.bufferram is total page cache size,
135 /* si_swapinfo(&i); */
136 /* availmem = availmem - (i.totalswap - i.freeswap); */
138 nomem = (availmem < sysctl_ip_vs_amemthresh);
143 spin_lock(&__ip_vs_dropentry_lock);
144 switch (sysctl_ip_vs_drop_entry) {
146 atomic_set(&ip_vs_dropentry, 0);
150 atomic_set(&ip_vs_dropentry, 1);
151 sysctl_ip_vs_drop_entry = 2;
153 atomic_set(&ip_vs_dropentry, 0);
158 atomic_set(&ip_vs_dropentry, 1);
160 atomic_set(&ip_vs_dropentry, 0);
161 sysctl_ip_vs_drop_entry = 1;
165 atomic_set(&ip_vs_dropentry, 1);
168 spin_unlock(&__ip_vs_dropentry_lock);
171 spin_lock(&__ip_vs_droppacket_lock);
172 switch (sysctl_ip_vs_drop_packet) {
178 ip_vs_drop_rate = ip_vs_drop_counter
179 = sysctl_ip_vs_amemthresh /
180 (sysctl_ip_vs_amemthresh-availmem);
181 sysctl_ip_vs_drop_packet = 2;
188 ip_vs_drop_rate = ip_vs_drop_counter
189 = sysctl_ip_vs_amemthresh /
190 (sysctl_ip_vs_amemthresh-availmem);
193 sysctl_ip_vs_drop_packet = 1;
197 ip_vs_drop_rate = sysctl_ip_vs_am_droprate;
200 spin_unlock(&__ip_vs_droppacket_lock);
203 write_lock(&__ip_vs_securetcp_lock);
204 switch (sysctl_ip_vs_secure_tcp) {
206 if (old_secure_tcp >= 2)
211 if (old_secure_tcp < 2)
213 sysctl_ip_vs_secure_tcp = 2;
215 if (old_secure_tcp >= 2)
221 if (old_secure_tcp < 2)
224 if (old_secure_tcp >= 2)
226 sysctl_ip_vs_secure_tcp = 1;
230 if (old_secure_tcp < 2)
234 old_secure_tcp = sysctl_ip_vs_secure_tcp;
236 ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp>1);
237 write_unlock(&__ip_vs_securetcp_lock);
244 * Timer for checking the defense
246 #define DEFENSE_TIMER_PERIOD 1*HZ
247 static void defense_work_handler(struct work_struct *work);
248 static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
250 static void defense_work_handler(struct work_struct *work)
252 update_defense_level();
253 if (atomic_read(&ip_vs_dropentry))
254 ip_vs_random_dropentry();
256 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
260 ip_vs_use_count_inc(void)
262 return try_module_get(THIS_MODULE);
266 ip_vs_use_count_dec(void)
268 module_put(THIS_MODULE);
273 * Hash table: for virtual service lookups
275 #define IP_VS_SVC_TAB_BITS 8
276 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
277 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
279 /* the service table hashed by <protocol, addr, port> */
280 static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
281 /* the service table hashed by fwmark */
282 static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
285 * Hash table: for real service lookups
287 #define IP_VS_RTAB_BITS 4
288 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
289 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
291 static struct list_head ip_vs_rtable[IP_VS_RTAB_SIZE];
294 * Trash for destinations
296 static LIST_HEAD(ip_vs_dest_trash);
299 * FTP & NULL virtual service counters
301 static atomic_t ip_vs_ftpsvc_counter = ATOMIC_INIT(0);
302 static atomic_t ip_vs_nullsvc_counter = ATOMIC_INIT(0);
306 * Returns hash value for virtual service
308 static __inline__ unsigned
309 ip_vs_svc_hashkey(int af, unsigned proto, const union nf_inet_addr *addr,
312 register unsigned porth = ntohs(port);
313 __be32 addr_fold = addr->ip;
315 #ifdef CONFIG_IP_VS_IPV6
317 addr_fold = addr->ip6[0]^addr->ip6[1]^
318 addr->ip6[2]^addr->ip6[3];
321 return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
322 & IP_VS_SVC_TAB_MASK;
326 * Returns hash value of fwmark for virtual service lookup
328 static __inline__ unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark)
330 return fwmark & IP_VS_SVC_TAB_MASK;
334 * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
335 * or in the ip_vs_svc_fwm_table by fwmark.
336 * Should be called with locked tables.
338 static int ip_vs_svc_hash(struct ip_vs_service *svc)
342 if (svc->flags & IP_VS_SVC_F_HASHED) {
343 IP_VS_ERR("ip_vs_svc_hash(): request for already hashed, "
344 "called from %p\n", __builtin_return_address(0));
348 if (svc->fwmark == 0) {
350 * Hash it by <protocol,addr,port> in ip_vs_svc_table
352 hash = ip_vs_svc_hashkey(svc->af, svc->protocol, &svc->addr,
354 list_add(&svc->s_list, &ip_vs_svc_table[hash]);
357 * Hash it by fwmark in ip_vs_svc_fwm_table
359 hash = ip_vs_svc_fwm_hashkey(svc->fwmark);
360 list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
363 svc->flags |= IP_VS_SVC_F_HASHED;
364 /* increase its refcnt because it is referenced by the svc table */
365 atomic_inc(&svc->refcnt);
371 * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
372 * Should be called with locked tables.
374 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
376 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
377 IP_VS_ERR("ip_vs_svc_unhash(): request for unhash flagged, "
378 "called from %p\n", __builtin_return_address(0));
382 if (svc->fwmark == 0) {
383 /* Remove it from the ip_vs_svc_table table */
384 list_del(&svc->s_list);
386 /* Remove it from the ip_vs_svc_fwm_table table */
387 list_del(&svc->f_list);
390 svc->flags &= ~IP_VS_SVC_F_HASHED;
391 atomic_dec(&svc->refcnt);
397 * Get service by {proto,addr,port} in the service table.
399 static inline struct ip_vs_service *
400 __ip_vs_service_get(int af, __u16 protocol, const union nf_inet_addr *vaddr,
404 struct ip_vs_service *svc;
406 /* Check for "full" addressed entries */
407 hash = ip_vs_svc_hashkey(af, protocol, vaddr, vport);
409 list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
411 && ip_vs_addr_equal(af, &svc->addr, vaddr)
412 && (svc->port == vport)
413 && (svc->protocol == protocol)) {
415 atomic_inc(&svc->usecnt);
425 * Get service by {fwmark} in the service table.
427 static inline struct ip_vs_service *
428 __ip_vs_svc_fwm_get(int af, __u32 fwmark)
431 struct ip_vs_service *svc;
433 /* Check for fwmark addressed entries */
434 hash = ip_vs_svc_fwm_hashkey(fwmark);
436 list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) {
437 if (svc->fwmark == fwmark && svc->af == af) {
439 atomic_inc(&svc->usecnt);
447 struct ip_vs_service *
448 ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
449 const union nf_inet_addr *vaddr, __be16 vport)
451 struct ip_vs_service *svc;
453 read_lock(&__ip_vs_svc_lock);
456 * Check the table hashed by fwmark first
458 if (fwmark && (svc = __ip_vs_svc_fwm_get(af, fwmark)))
462 * Check the table hashed by <protocol,addr,port>
463 * for "full" addressed entries
465 svc = __ip_vs_service_get(af, protocol, vaddr, vport);
468 && protocol == IPPROTO_TCP
469 && atomic_read(&ip_vs_ftpsvc_counter)
470 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
472 * Check if ftp service entry exists, the packet
473 * might belong to FTP data connections.
475 svc = __ip_vs_service_get(af, protocol, vaddr, FTPPORT);
479 && atomic_read(&ip_vs_nullsvc_counter)) {
481 * Check if the catch-all port (port zero) exists
483 svc = __ip_vs_service_get(af, protocol, vaddr, 0);
487 read_unlock(&__ip_vs_svc_lock);
489 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
490 fwmark, ip_vs_proto_name(protocol),
491 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
492 svc ? "hit" : "not hit");
499 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
501 atomic_inc(&svc->refcnt);
506 __ip_vs_unbind_svc(struct ip_vs_dest *dest)
508 struct ip_vs_service *svc = dest->svc;
511 if (atomic_dec_and_test(&svc->refcnt))
517 * Returns hash value for real service
519 static inline unsigned ip_vs_rs_hashkey(int af,
520 const union nf_inet_addr *addr,
523 register unsigned porth = ntohs(port);
524 __be32 addr_fold = addr->ip;
526 #ifdef CONFIG_IP_VS_IPV6
528 addr_fold = addr->ip6[0]^addr->ip6[1]^
529 addr->ip6[2]^addr->ip6[3];
532 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
537 * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
538 * should be called with locked tables.
540 static int ip_vs_rs_hash(struct ip_vs_dest *dest)
544 if (!list_empty(&dest->d_list)) {
549 * Hash by proto,addr,port,
550 * which are the parameters of the real service.
552 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
554 list_add(&dest->d_list, &ip_vs_rtable[hash]);
560 * UNhashes ip_vs_dest from ip_vs_rtable.
561 * should be called with locked tables.
563 static int ip_vs_rs_unhash(struct ip_vs_dest *dest)
566 * Remove it from the ip_vs_rtable table.
568 if (!list_empty(&dest->d_list)) {
569 list_del(&dest->d_list);
570 INIT_LIST_HEAD(&dest->d_list);
577 * Lookup real service by <proto,addr,port> in the real service table.
580 ip_vs_lookup_real_service(int af, __u16 protocol,
581 const union nf_inet_addr *daddr,
585 struct ip_vs_dest *dest;
588 * Check for "full" addressed entries
589 * Return the first found entry
591 hash = ip_vs_rs_hashkey(af, daddr, dport);
593 read_lock(&__ip_vs_rs_lock);
594 list_for_each_entry(dest, &ip_vs_rtable[hash], d_list) {
596 && ip_vs_addr_equal(af, &dest->addr, daddr)
597 && (dest->port == dport)
598 && ((dest->protocol == protocol) ||
601 read_unlock(&__ip_vs_rs_lock);
605 read_unlock(&__ip_vs_rs_lock);
611 * Lookup destination by {addr,port} in the given service
613 static struct ip_vs_dest *
614 ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
617 struct ip_vs_dest *dest;
620 * Find the destination for the given service
622 list_for_each_entry(dest, &svc->destinations, n_list) {
623 if ((dest->af == svc->af)
624 && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
625 && (dest->port == dport)) {
635 * Find destination by {daddr,dport,vaddr,protocol}
636 * Cretaed to be used in ip_vs_process_message() in
637 * the backup synchronization daemon. It finds the
638 * destination to be bound to the received connection
641 * ip_vs_lookup_real_service() looked promissing, but
642 * seems not working as expected.
644 struct ip_vs_dest *ip_vs_find_dest(int af, const union nf_inet_addr *daddr,
646 const union nf_inet_addr *vaddr,
647 __be16 vport, __u16 protocol)
649 struct ip_vs_dest *dest;
650 struct ip_vs_service *svc;
652 svc = ip_vs_service_get(af, 0, protocol, vaddr, vport);
655 dest = ip_vs_lookup_dest(svc, daddr, dport);
657 atomic_inc(&dest->refcnt);
658 ip_vs_service_put(svc);
663 * Lookup dest by {svc,addr,port} in the destination trash.
664 * The destination trash is used to hold the destinations that are removed
665 * from the service table but are still referenced by some conn entries.
666 * The reason to add the destination trash is when the dest is temporary
667 * down (either by administrator or by monitor program), the dest can be
668 * picked back from the trash, the remaining connections to the dest can
669 * continue, and the counting information of the dest is also useful for
672 static struct ip_vs_dest *
673 ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
676 struct ip_vs_dest *dest, *nxt;
679 * Find the destination in trash
681 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
682 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
685 IP_VS_DBG_ADDR(svc->af, &dest->addr),
687 atomic_read(&dest->refcnt));
688 if (dest->af == svc->af &&
689 ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
690 dest->port == dport &&
691 dest->vfwmark == svc->fwmark &&
692 dest->protocol == svc->protocol &&
694 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
695 dest->vport == svc->port))) {
701 * Try to purge the destination from trash if not referenced
703 if (atomic_read(&dest->refcnt) == 1) {
704 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
707 IP_VS_DBG_ADDR(svc->af, &dest->addr),
709 list_del(&dest->n_list);
710 ip_vs_dst_reset(dest);
711 __ip_vs_unbind_svc(dest);
721 * Clean up all the destinations in the trash
722 * Called by the ip_vs_control_cleanup()
724 * When the ip_vs_control_clearup is activated by ipvs module exit,
725 * the service tables must have been flushed and all the connections
726 * are expired, and the refcnt of each destination in the trash must
727 * be 1, so we simply release them here.
729 static void ip_vs_trash_cleanup(void)
731 struct ip_vs_dest *dest, *nxt;
733 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
734 list_del(&dest->n_list);
735 ip_vs_dst_reset(dest);
736 __ip_vs_unbind_svc(dest);
743 ip_vs_zero_stats(struct ip_vs_stats *stats)
745 spin_lock_bh(&stats->lock);
747 memset(&stats->ustats, 0, sizeof(stats->ustats));
748 ip_vs_zero_estimator(stats);
750 spin_unlock_bh(&stats->lock);
754 * Update a destination in the given service
757 __ip_vs_update_dest(struct ip_vs_service *svc,
758 struct ip_vs_dest *dest, struct ip_vs_dest_user_kern *udest)
762 /* set the weight and the flags */
763 atomic_set(&dest->weight, udest->weight);
764 conn_flags = udest->conn_flags | IP_VS_CONN_F_INACTIVE;
766 /* check if local node and update the flags */
767 #ifdef CONFIG_IP_VS_IPV6
768 if (svc->af == AF_INET6) {
769 if (__ip_vs_addr_is_local_v6(&udest->addr.in6)) {
770 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
771 | IP_VS_CONN_F_LOCALNODE;
775 if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
776 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
777 | IP_VS_CONN_F_LOCALNODE;
780 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
781 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
782 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
785 * Put the real service in ip_vs_rtable if not present.
786 * For now only for NAT!
788 write_lock_bh(&__ip_vs_rs_lock);
790 write_unlock_bh(&__ip_vs_rs_lock);
792 atomic_set(&dest->conn_flags, conn_flags);
794 /* bind the service */
796 __ip_vs_bind_svc(dest, svc);
798 if (dest->svc != svc) {
799 __ip_vs_unbind_svc(dest);
800 ip_vs_zero_stats(&dest->stats);
801 __ip_vs_bind_svc(dest, svc);
805 /* set the dest status flags */
806 dest->flags |= IP_VS_DEST_F_AVAILABLE;
808 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
809 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
810 dest->u_threshold = udest->u_threshold;
811 dest->l_threshold = udest->l_threshold;
816 * Create a destination for the given service
819 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
820 struct ip_vs_dest **dest_p)
822 struct ip_vs_dest *dest;
827 #ifdef CONFIG_IP_VS_IPV6
828 if (svc->af == AF_INET6) {
829 atype = ipv6_addr_type(&udest->addr.in6);
830 if ((!(atype & IPV6_ADDR_UNICAST) ||
831 atype & IPV6_ADDR_LINKLOCAL) &&
832 !__ip_vs_addr_is_local_v6(&udest->addr.in6))
837 atype = inet_addr_type(&init_net, udest->addr.ip);
838 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
842 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
844 IP_VS_ERR("ip_vs_new_dest: kmalloc failed.\n");
849 dest->protocol = svc->protocol;
850 dest->vaddr = svc->addr;
851 dest->vport = svc->port;
852 dest->vfwmark = svc->fwmark;
853 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
854 dest->port = udest->port;
856 atomic_set(&dest->activeconns, 0);
857 atomic_set(&dest->inactconns, 0);
858 atomic_set(&dest->persistconns, 0);
859 atomic_set(&dest->refcnt, 0);
861 INIT_LIST_HEAD(&dest->d_list);
862 spin_lock_init(&dest->dst_lock);
863 spin_lock_init(&dest->stats.lock);
864 __ip_vs_update_dest(svc, dest, udest);
865 ip_vs_new_estimator(&dest->stats);
875 * Add a destination into an existing service
878 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
880 struct ip_vs_dest *dest;
881 union nf_inet_addr daddr;
882 __be16 dport = udest->port;
887 if (udest->weight < 0) {
888 IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
892 if (udest->l_threshold > udest->u_threshold) {
893 IP_VS_ERR("ip_vs_add_dest(): lower threshold is higher than "
894 "upper threshold\n");
898 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
901 * Check if the dest already exists in the list
903 dest = ip_vs_lookup_dest(svc, &daddr, dport);
906 IP_VS_DBG(1, "ip_vs_add_dest(): dest already exists\n");
911 * Check if the dest already exists in the trash and
912 * is from the same service
914 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
917 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
918 "dest->refcnt=%d, service %u/%s:%u\n",
919 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
920 atomic_read(&dest->refcnt),
922 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
925 __ip_vs_update_dest(svc, dest, udest);
928 * Get the destination from the trash
930 list_del(&dest->n_list);
932 ip_vs_new_estimator(&dest->stats);
934 write_lock_bh(&__ip_vs_svc_lock);
937 * Wait until all other svc users go away.
939 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
941 list_add(&dest->n_list, &svc->destinations);
944 /* call the update_service function of its scheduler */
945 if (svc->scheduler->update_service)
946 svc->scheduler->update_service(svc);
948 write_unlock_bh(&__ip_vs_svc_lock);
953 * Allocate and initialize the dest structure
955 ret = ip_vs_new_dest(svc, udest, &dest);
961 * Add the dest entry into the list
963 atomic_inc(&dest->refcnt);
965 write_lock_bh(&__ip_vs_svc_lock);
968 * Wait until all other svc users go away.
970 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
972 list_add(&dest->n_list, &svc->destinations);
975 /* call the update_service function of its scheduler */
976 if (svc->scheduler->update_service)
977 svc->scheduler->update_service(svc);
979 write_unlock_bh(&__ip_vs_svc_lock);
988 * Edit a destination in the given service
991 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
993 struct ip_vs_dest *dest;
994 union nf_inet_addr daddr;
995 __be16 dport = udest->port;
999 if (udest->weight < 0) {
1000 IP_VS_ERR("ip_vs_edit_dest(): server weight less than zero\n");
1004 if (udest->l_threshold > udest->u_threshold) {
1005 IP_VS_ERR("ip_vs_edit_dest(): lower threshold is higher than "
1006 "upper threshold\n");
1010 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
1013 * Lookup the destination list
1015 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1018 IP_VS_DBG(1, "ip_vs_edit_dest(): dest doesn't exist\n");
1022 __ip_vs_update_dest(svc, dest, udest);
1024 write_lock_bh(&__ip_vs_svc_lock);
1026 /* Wait until all other svc users go away */
1027 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1029 /* call the update_service, because server weight may be changed */
1030 if (svc->scheduler->update_service)
1031 svc->scheduler->update_service(svc);
1033 write_unlock_bh(&__ip_vs_svc_lock);
1042 * Delete a destination (must be already unlinked from the service)
1044 static void __ip_vs_del_dest(struct ip_vs_dest *dest)
1046 ip_vs_kill_estimator(&dest->stats);
1049 * Remove it from the d-linked list with the real services.
1051 write_lock_bh(&__ip_vs_rs_lock);
1052 ip_vs_rs_unhash(dest);
1053 write_unlock_bh(&__ip_vs_rs_lock);
1056 * Decrease the refcnt of the dest, and free the dest
1057 * if nobody refers to it (refcnt=0). Otherwise, throw
1058 * the destination into the trash.
1060 if (atomic_dec_and_test(&dest->refcnt)) {
1061 ip_vs_dst_reset(dest);
1062 /* simply decrease svc->refcnt here, let the caller check
1063 and release the service if nobody refers to it.
1064 Only user context can release destination and service,
1065 and only one user context can update virtual service at a
1066 time, so the operation here is OK */
1067 atomic_dec(&dest->svc->refcnt);
1070 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1071 "dest->refcnt=%d\n",
1072 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1074 atomic_read(&dest->refcnt));
1075 list_add(&dest->n_list, &ip_vs_dest_trash);
1076 atomic_inc(&dest->refcnt);
1082 * Unlink a destination from the given service
1084 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1085 struct ip_vs_dest *dest,
1088 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1091 * Remove it from the d-linked destination list.
1093 list_del(&dest->n_list);
1097 * Call the update_service function of its scheduler
1099 if (svcupd && svc->scheduler->update_service)
1100 svc->scheduler->update_service(svc);
1105 * Delete a destination server in the given service
1108 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1110 struct ip_vs_dest *dest;
1111 __be16 dport = udest->port;
1115 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
1118 IP_VS_DBG(1, "ip_vs_del_dest(): destination not found!\n");
1122 write_lock_bh(&__ip_vs_svc_lock);
1125 * Wait until all other svc users go away.
1127 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1130 * Unlink dest from the service
1132 __ip_vs_unlink_dest(svc, dest, 1);
1134 write_unlock_bh(&__ip_vs_svc_lock);
1137 * Delete the destination
1139 __ip_vs_del_dest(dest);
1148 * Add a service into the service hash table
1151 ip_vs_add_service(struct ip_vs_service_user_kern *u,
1152 struct ip_vs_service **svc_p)
1155 struct ip_vs_scheduler *sched = NULL;
1156 struct ip_vs_service *svc = NULL;
1158 /* increase the module use count */
1159 ip_vs_use_count_inc();
1161 /* Lookup the scheduler by 'u->sched_name' */
1162 sched = ip_vs_scheduler_get(u->sched_name);
1163 if (sched == NULL) {
1164 IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
1170 #ifdef CONFIG_IP_VS_IPV6
1171 if (u->af == AF_INET6) {
1172 if (!sched->supports_ipv6) {
1173 ret = -EAFNOSUPPORT;
1176 if ((u->netmask < 1) || (u->netmask > 128)) {
1183 svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
1185 IP_VS_DBG(1, "ip_vs_add_service: kmalloc failed.\n");
1190 /* I'm the first user of the service */
1191 atomic_set(&svc->usecnt, 1);
1192 atomic_set(&svc->refcnt, 0);
1195 svc->protocol = u->protocol;
1196 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1197 svc->port = u->port;
1198 svc->fwmark = u->fwmark;
1199 svc->flags = u->flags;
1200 svc->timeout = u->timeout * HZ;
1201 svc->netmask = u->netmask;
1203 INIT_LIST_HEAD(&svc->destinations);
1204 rwlock_init(&svc->sched_lock);
1205 spin_lock_init(&svc->stats.lock);
1207 /* Bind the scheduler */
1208 ret = ip_vs_bind_scheduler(svc, sched);
1213 /* Update the virtual service counters */
1214 if (svc->port == FTPPORT)
1215 atomic_inc(&ip_vs_ftpsvc_counter);
1216 else if (svc->port == 0)
1217 atomic_inc(&ip_vs_nullsvc_counter);
1219 ip_vs_new_estimator(&svc->stats);
1221 /* Count only IPv4 services for old get/setsockopt interface */
1222 if (svc->af == AF_INET)
1223 ip_vs_num_services++;
1225 /* Hash the service into the service table */
1226 write_lock_bh(&__ip_vs_svc_lock);
1227 ip_vs_svc_hash(svc);
1228 write_unlock_bh(&__ip_vs_svc_lock);
1236 ip_vs_unbind_scheduler(svc);
1239 ip_vs_app_inc_put(svc->inc);
1244 ip_vs_scheduler_put(sched);
1247 /* decrease the module use count */
1248 ip_vs_use_count_dec();
1255 * Edit a service and bind it with a new scheduler
1258 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1260 struct ip_vs_scheduler *sched, *old_sched;
1264 * Lookup the scheduler, by 'u->sched_name'
1266 sched = ip_vs_scheduler_get(u->sched_name);
1267 if (sched == NULL) {
1268 IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
1274 #ifdef CONFIG_IP_VS_IPV6
1275 if (u->af == AF_INET6) {
1276 if (!sched->supports_ipv6) {
1277 ret = -EAFNOSUPPORT;
1280 if ((u->netmask < 1) || (u->netmask > 128)) {
1287 write_lock_bh(&__ip_vs_svc_lock);
1290 * Wait until all other svc users go away.
1292 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1295 * Set the flags and timeout value
1297 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1298 svc->timeout = u->timeout * HZ;
1299 svc->netmask = u->netmask;
1301 old_sched = svc->scheduler;
1302 if (sched != old_sched) {
1304 * Unbind the old scheduler
1306 if ((ret = ip_vs_unbind_scheduler(svc))) {
1312 * Bind the new scheduler
1314 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1316 * If ip_vs_bind_scheduler fails, restore the old
1318 * The main reason of failure is out of memory.
1320 * The question is if the old scheduler can be
1321 * restored all the time. TODO: if it cannot be
1322 * restored some time, we must delete the service,
1323 * otherwise the system may crash.
1325 ip_vs_bind_scheduler(svc, old_sched);
1332 write_unlock_bh(&__ip_vs_svc_lock);
1336 ip_vs_scheduler_put(old_sched);
1343 * Delete a service from the service list
1344 * - The service must be unlinked, unlocked and not referenced!
1345 * - We are called under _bh lock
1347 static void __ip_vs_del_service(struct ip_vs_service *svc)
1349 struct ip_vs_dest *dest, *nxt;
1350 struct ip_vs_scheduler *old_sched;
1352 /* Count only IPv4 services for old get/setsockopt interface */
1353 if (svc->af == AF_INET)
1354 ip_vs_num_services--;
1356 ip_vs_kill_estimator(&svc->stats);
1358 /* Unbind scheduler */
1359 old_sched = svc->scheduler;
1360 ip_vs_unbind_scheduler(svc);
1362 ip_vs_scheduler_put(old_sched);
1364 /* Unbind app inc */
1366 ip_vs_app_inc_put(svc->inc);
1371 * Unlink the whole destination list
1373 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1374 __ip_vs_unlink_dest(svc, dest, 0);
1375 __ip_vs_del_dest(dest);
1379 * Update the virtual service counters
1381 if (svc->port == FTPPORT)
1382 atomic_dec(&ip_vs_ftpsvc_counter);
1383 else if (svc->port == 0)
1384 atomic_dec(&ip_vs_nullsvc_counter);
1387 * Free the service if nobody refers to it
1389 if (atomic_read(&svc->refcnt) == 0)
1392 /* decrease the module use count */
1393 ip_vs_use_count_dec();
1397 * Delete a service from the service list
1399 static int ip_vs_del_service(struct ip_vs_service *svc)
1405 * Unhash it from the service table
1407 write_lock_bh(&__ip_vs_svc_lock);
1409 ip_vs_svc_unhash(svc);
1412 * Wait until all the svc users go away.
1414 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1416 __ip_vs_del_service(svc);
1418 write_unlock_bh(&__ip_vs_svc_lock);
1425 * Flush all the virtual services
1427 static int ip_vs_flush(void)
1430 struct ip_vs_service *svc, *nxt;
1433 * Flush the service table hashed by <protocol,addr,port>
1435 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1436 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
1437 write_lock_bh(&__ip_vs_svc_lock);
1438 ip_vs_svc_unhash(svc);
1440 * Wait until all the svc users go away.
1442 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1443 __ip_vs_del_service(svc);
1444 write_unlock_bh(&__ip_vs_svc_lock);
1449 * Flush the service table hashed by fwmark
1451 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1452 list_for_each_entry_safe(svc, nxt,
1453 &ip_vs_svc_fwm_table[idx], f_list) {
1454 write_lock_bh(&__ip_vs_svc_lock);
1455 ip_vs_svc_unhash(svc);
1457 * Wait until all the svc users go away.
1459 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1460 __ip_vs_del_service(svc);
1461 write_unlock_bh(&__ip_vs_svc_lock);
1470 * Zero counters in a service or all services
1472 static int ip_vs_zero_service(struct ip_vs_service *svc)
1474 struct ip_vs_dest *dest;
1476 write_lock_bh(&__ip_vs_svc_lock);
1477 list_for_each_entry(dest, &svc->destinations, n_list) {
1478 ip_vs_zero_stats(&dest->stats);
1480 ip_vs_zero_stats(&svc->stats);
1481 write_unlock_bh(&__ip_vs_svc_lock);
1485 static int ip_vs_zero_all(void)
1488 struct ip_vs_service *svc;
1490 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1491 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1492 ip_vs_zero_service(svc);
1496 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1497 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1498 ip_vs_zero_service(svc);
1502 ip_vs_zero_stats(&ip_vs_stats);
1508 proc_do_defense_mode(ctl_table *table, int write, struct file * filp,
1509 void __user *buffer, size_t *lenp, loff_t *ppos)
1511 int *valp = table->data;
1515 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1516 if (write && (*valp != val)) {
1517 if ((*valp < 0) || (*valp > 3)) {
1518 /* Restore the correct value */
1521 update_defense_level();
1529 proc_do_sync_threshold(ctl_table *table, int write, struct file *filp,
1530 void __user *buffer, size_t *lenp, loff_t *ppos)
1532 int *valp = table->data;
1536 /* backup the value first */
1537 memcpy(val, valp, sizeof(val));
1539 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1540 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1541 /* Restore the correct value */
1542 memcpy(valp, val, sizeof(val));
1549 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1552 static struct ctl_table vs_vars[] = {
1554 .procname = "amemthresh",
1555 .data = &sysctl_ip_vs_amemthresh,
1556 .maxlen = sizeof(int),
1558 .proc_handler = &proc_dointvec,
1560 #ifdef CONFIG_IP_VS_DEBUG
1562 .procname = "debug_level",
1563 .data = &sysctl_ip_vs_debug_level,
1564 .maxlen = sizeof(int),
1566 .proc_handler = &proc_dointvec,
1570 .procname = "am_droprate",
1571 .data = &sysctl_ip_vs_am_droprate,
1572 .maxlen = sizeof(int),
1574 .proc_handler = &proc_dointvec,
1577 .procname = "drop_entry",
1578 .data = &sysctl_ip_vs_drop_entry,
1579 .maxlen = sizeof(int),
1581 .proc_handler = &proc_do_defense_mode,
1584 .procname = "drop_packet",
1585 .data = &sysctl_ip_vs_drop_packet,
1586 .maxlen = sizeof(int),
1588 .proc_handler = &proc_do_defense_mode,
1591 .procname = "secure_tcp",
1592 .data = &sysctl_ip_vs_secure_tcp,
1593 .maxlen = sizeof(int),
1595 .proc_handler = &proc_do_defense_mode,
1599 .procname = "timeout_established",
1600 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1601 .maxlen = sizeof(int),
1603 .proc_handler = &proc_dointvec_jiffies,
1606 .procname = "timeout_synsent",
1607 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1608 .maxlen = sizeof(int),
1610 .proc_handler = &proc_dointvec_jiffies,
1613 .procname = "timeout_synrecv",
1614 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1615 .maxlen = sizeof(int),
1617 .proc_handler = &proc_dointvec_jiffies,
1620 .procname = "timeout_finwait",
1621 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1622 .maxlen = sizeof(int),
1624 .proc_handler = &proc_dointvec_jiffies,
1627 .procname = "timeout_timewait",
1628 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1629 .maxlen = sizeof(int),
1631 .proc_handler = &proc_dointvec_jiffies,
1634 .procname = "timeout_close",
1635 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1636 .maxlen = sizeof(int),
1638 .proc_handler = &proc_dointvec_jiffies,
1641 .procname = "timeout_closewait",
1642 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1643 .maxlen = sizeof(int),
1645 .proc_handler = &proc_dointvec_jiffies,
1648 .procname = "timeout_lastack",
1649 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1650 .maxlen = sizeof(int),
1652 .proc_handler = &proc_dointvec_jiffies,
1655 .procname = "timeout_listen",
1656 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1657 .maxlen = sizeof(int),
1659 .proc_handler = &proc_dointvec_jiffies,
1662 .procname = "timeout_synack",
1663 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1664 .maxlen = sizeof(int),
1666 .proc_handler = &proc_dointvec_jiffies,
1669 .procname = "timeout_udp",
1670 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1671 .maxlen = sizeof(int),
1673 .proc_handler = &proc_dointvec_jiffies,
1676 .procname = "timeout_icmp",
1677 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1678 .maxlen = sizeof(int),
1680 .proc_handler = &proc_dointvec_jiffies,
1684 .procname = "cache_bypass",
1685 .data = &sysctl_ip_vs_cache_bypass,
1686 .maxlen = sizeof(int),
1688 .proc_handler = &proc_dointvec,
1691 .procname = "expire_nodest_conn",
1692 .data = &sysctl_ip_vs_expire_nodest_conn,
1693 .maxlen = sizeof(int),
1695 .proc_handler = &proc_dointvec,
1698 .procname = "expire_quiescent_template",
1699 .data = &sysctl_ip_vs_expire_quiescent_template,
1700 .maxlen = sizeof(int),
1702 .proc_handler = &proc_dointvec,
1705 .procname = "sync_threshold",
1706 .data = &sysctl_ip_vs_sync_threshold,
1707 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1709 .proc_handler = &proc_do_sync_threshold,
1712 .procname = "nat_icmp_send",
1713 .data = &sysctl_ip_vs_nat_icmp_send,
1714 .maxlen = sizeof(int),
1716 .proc_handler = &proc_dointvec,
1721 const struct ctl_path net_vs_ctl_path[] = {
1722 { .procname = "net", .ctl_name = CTL_NET, },
1723 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1724 { .procname = "vs", },
1727 EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1729 static struct ctl_table_header * sysctl_header;
1731 #ifdef CONFIG_PROC_FS
1734 struct list_head *table;
1739 * Write the contents of the VS rule table to a PROCfs file.
1740 * (It is kept just for backward compatibility)
1742 static inline const char *ip_vs_fwd_name(unsigned flags)
1744 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1745 case IP_VS_CONN_F_LOCALNODE:
1747 case IP_VS_CONN_F_TUNNEL:
1749 case IP_VS_CONN_F_DROUTE:
1757 /* Get the Nth entry in the two lists */
1758 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1760 struct ip_vs_iter *iter = seq->private;
1762 struct ip_vs_service *svc;
1764 /* look in hash by protocol */
1765 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1766 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1768 iter->table = ip_vs_svc_table;
1775 /* keep looking in fwmark */
1776 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1777 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1779 iter->table = ip_vs_svc_fwm_table;
1789 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1790 __acquires(__ip_vs_svc_lock)
1793 read_lock_bh(&__ip_vs_svc_lock);
1794 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1798 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1800 struct list_head *e;
1801 struct ip_vs_iter *iter;
1802 struct ip_vs_service *svc;
1805 if (v == SEQ_START_TOKEN)
1806 return ip_vs_info_array(seq,0);
1809 iter = seq->private;
1811 if (iter->table == ip_vs_svc_table) {
1812 /* next service in table hashed by protocol */
1813 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1814 return list_entry(e, struct ip_vs_service, s_list);
1817 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1818 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1824 iter->table = ip_vs_svc_fwm_table;
1829 /* next service in hashed by fwmark */
1830 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1831 return list_entry(e, struct ip_vs_service, f_list);
1834 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1835 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1843 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1844 __releases(__ip_vs_svc_lock)
1846 read_unlock_bh(&__ip_vs_svc_lock);
1850 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1852 if (v == SEQ_START_TOKEN) {
1854 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1855 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
1857 "Prot LocalAddress:Port Scheduler Flags\n");
1859 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1861 const struct ip_vs_service *svc = v;
1862 const struct ip_vs_iter *iter = seq->private;
1863 const struct ip_vs_dest *dest;
1865 if (iter->table == ip_vs_svc_table) {
1866 #ifdef CONFIG_IP_VS_IPV6
1867 if (svc->af == AF_INET6)
1868 seq_printf(seq, "%s [" NIP6_FMT "]:%04X %s ",
1869 ip_vs_proto_name(svc->protocol),
1870 NIP6(svc->addr.in6),
1872 svc->scheduler->name);
1875 seq_printf(seq, "%s %08X:%04X %s ",
1876 ip_vs_proto_name(svc->protocol),
1877 ntohl(svc->addr.ip),
1879 svc->scheduler->name);
1881 seq_printf(seq, "FWM %08X %s ",
1882 svc->fwmark, svc->scheduler->name);
1885 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1886 seq_printf(seq, "persistent %d %08X\n",
1888 ntohl(svc->netmask));
1890 seq_putc(seq, '\n');
1892 list_for_each_entry(dest, &svc->destinations, n_list) {
1893 #ifdef CONFIG_IP_VS_IPV6
1894 if (dest->af == AF_INET6)
1896 " -> [" NIP6_FMT "]:%04X"
1897 " %-7s %-6d %-10d %-10d\n",
1898 NIP6(dest->addr.in6),
1900 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1901 atomic_read(&dest->weight),
1902 atomic_read(&dest->activeconns),
1903 atomic_read(&dest->inactconns));
1908 "%-7s %-6d %-10d %-10d\n",
1909 ntohl(dest->addr.ip),
1911 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1912 atomic_read(&dest->weight),
1913 atomic_read(&dest->activeconns),
1914 atomic_read(&dest->inactconns));
1921 static const struct seq_operations ip_vs_info_seq_ops = {
1922 .start = ip_vs_info_seq_start,
1923 .next = ip_vs_info_seq_next,
1924 .stop = ip_vs_info_seq_stop,
1925 .show = ip_vs_info_seq_show,
1928 static int ip_vs_info_open(struct inode *inode, struct file *file)
1930 return seq_open_private(file, &ip_vs_info_seq_ops,
1931 sizeof(struct ip_vs_iter));
1934 static const struct file_operations ip_vs_info_fops = {
1935 .owner = THIS_MODULE,
1936 .open = ip_vs_info_open,
1938 .llseek = seq_lseek,
1939 .release = seq_release_private,
1944 struct ip_vs_stats ip_vs_stats = {
1945 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1948 #ifdef CONFIG_PROC_FS
1949 static int ip_vs_stats_show(struct seq_file *seq, void *v)
1952 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1954 " Total Incoming Outgoing Incoming Outgoing\n");
1956 " Conns Packets Packets Bytes Bytes\n");
1958 spin_lock_bh(&ip_vs_stats.lock);
1959 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1960 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1961 (unsigned long long) ip_vs_stats.ustats.inbytes,
1962 (unsigned long long) ip_vs_stats.ustats.outbytes);
1964 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1966 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1967 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
1968 ip_vs_stats.ustats.cps,
1969 ip_vs_stats.ustats.inpps,
1970 ip_vs_stats.ustats.outpps,
1971 ip_vs_stats.ustats.inbps,
1972 ip_vs_stats.ustats.outbps);
1973 spin_unlock_bh(&ip_vs_stats.lock);
1978 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1980 return single_open(file, ip_vs_stats_show, NULL);
1983 static const struct file_operations ip_vs_stats_fops = {
1984 .owner = THIS_MODULE,
1985 .open = ip_vs_stats_seq_open,
1987 .llseek = seq_lseek,
1988 .release = single_release,
1994 * Set timeout values for tcp tcpfin udp in the timeout_table.
1996 static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1998 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2003 #ifdef CONFIG_IP_VS_PROTO_TCP
2004 if (u->tcp_timeout) {
2005 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
2006 = u->tcp_timeout * HZ;
2009 if (u->tcp_fin_timeout) {
2010 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
2011 = u->tcp_fin_timeout * HZ;
2015 #ifdef CONFIG_IP_VS_PROTO_UDP
2016 if (u->udp_timeout) {
2017 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
2018 = u->udp_timeout * HZ;
2025 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2026 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2027 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2028 sizeof(struct ip_vs_dest_user))
2029 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2030 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2031 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2033 static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
2034 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2035 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2036 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2037 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2038 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2039 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2040 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2041 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2042 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2043 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2044 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2047 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2048 struct ip_vs_service_user *usvc_compat)
2051 usvc->protocol = usvc_compat->protocol;
2052 usvc->addr.ip = usvc_compat->addr;
2053 usvc->port = usvc_compat->port;
2054 usvc->fwmark = usvc_compat->fwmark;
2056 /* Deep copy of sched_name is not needed here */
2057 usvc->sched_name = usvc_compat->sched_name;
2059 usvc->flags = usvc_compat->flags;
2060 usvc->timeout = usvc_compat->timeout;
2061 usvc->netmask = usvc_compat->netmask;
2064 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2065 struct ip_vs_dest_user *udest_compat)
2067 udest->addr.ip = udest_compat->addr;
2068 udest->port = udest_compat->port;
2069 udest->conn_flags = udest_compat->conn_flags;
2070 udest->weight = udest_compat->weight;
2071 udest->u_threshold = udest_compat->u_threshold;
2072 udest->l_threshold = udest_compat->l_threshold;
2076 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2079 unsigned char arg[MAX_ARG_LEN];
2080 struct ip_vs_service_user *usvc_compat;
2081 struct ip_vs_service_user_kern usvc;
2082 struct ip_vs_service *svc;
2083 struct ip_vs_dest_user *udest_compat;
2084 struct ip_vs_dest_user_kern udest;
2086 if (!capable(CAP_NET_ADMIN))
2089 if (len != set_arglen[SET_CMDID(cmd)]) {
2090 IP_VS_ERR("set_ctl: len %u != %u\n",
2091 len, set_arglen[SET_CMDID(cmd)]);
2095 if (copy_from_user(arg, user, len) != 0)
2098 /* increase the module use count */
2099 ip_vs_use_count_inc();
2101 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
2106 if (cmd == IP_VS_SO_SET_FLUSH) {
2107 /* Flush the virtual service */
2108 ret = ip_vs_flush();
2110 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2111 /* Set timeout values for (tcp tcpfin udp) */
2112 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2114 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2115 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2116 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2118 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2119 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2120 ret = stop_sync_thread(dm->state);
2124 usvc_compat = (struct ip_vs_service_user *)arg;
2125 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2127 /* We only use the new structs internally, so copy userspace compat
2128 * structs to extended internal versions */
2129 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2130 ip_vs_copy_udest_compat(&udest, udest_compat);
2132 if (cmd == IP_VS_SO_SET_ZERO) {
2133 /* if no service address is set, zero counters in all */
2134 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2135 ret = ip_vs_zero_all();
2140 /* Check for valid protocol: TCP or UDP, even for fwmark!=0 */
2141 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP) {
2142 IP_VS_ERR("set_ctl: invalid protocol: %d %d.%d.%d.%d:%d %s\n",
2143 usvc.protocol, NIPQUAD(usvc.addr.ip),
2144 ntohs(usvc.port), usvc.sched_name);
2149 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2150 if (usvc.fwmark == 0)
2151 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
2152 &usvc.addr, usvc.port);
2154 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2156 if (cmd != IP_VS_SO_SET_ADD
2157 && (svc == NULL || svc->protocol != usvc.protocol)) {
2163 case IP_VS_SO_SET_ADD:
2167 ret = ip_vs_add_service(&usvc, &svc);
2169 case IP_VS_SO_SET_EDIT:
2170 ret = ip_vs_edit_service(svc, &usvc);
2172 case IP_VS_SO_SET_DEL:
2173 ret = ip_vs_del_service(svc);
2177 case IP_VS_SO_SET_ZERO:
2178 ret = ip_vs_zero_service(svc);
2180 case IP_VS_SO_SET_ADDDEST:
2181 ret = ip_vs_add_dest(svc, &udest);
2183 case IP_VS_SO_SET_EDITDEST:
2184 ret = ip_vs_edit_dest(svc, &udest);
2186 case IP_VS_SO_SET_DELDEST:
2187 ret = ip_vs_del_dest(svc, &udest);
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];
2365 if (!capable(CAP_NET_ADMIN))
2368 if (*len < get_arglen[GET_CMDID(cmd)]) {
2369 IP_VS_ERR("get_ctl: len %u < %u\n",
2370 *len, get_arglen[GET_CMDID(cmd)]);
2374 if (copy_from_user(arg, user, get_arglen[GET_CMDID(cmd)]) != 0)
2377 if (mutex_lock_interruptible(&__ip_vs_mutex))
2378 return -ERESTARTSYS;
2381 case IP_VS_SO_GET_VERSION:
2385 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2386 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
2387 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2391 *len = strlen(buf)+1;
2395 case IP_VS_SO_GET_INFO:
2397 struct ip_vs_getinfo info;
2398 info.version = IP_VS_VERSION_CODE;
2399 info.size = IP_VS_CONN_TAB_SIZE;
2400 info.num_services = ip_vs_num_services;
2401 if (copy_to_user(user, &info, sizeof(info)) != 0)
2406 case IP_VS_SO_GET_SERVICES:
2408 struct ip_vs_get_services *get;
2411 get = (struct ip_vs_get_services *)arg;
2412 size = sizeof(*get) +
2413 sizeof(struct ip_vs_service_entry) * get->num_services;
2415 IP_VS_ERR("length: %u != %u\n", *len, size);
2419 ret = __ip_vs_get_service_entries(get, user);
2423 case IP_VS_SO_GET_SERVICE:
2425 struct ip_vs_service_entry *entry;
2426 struct ip_vs_service *svc;
2427 union nf_inet_addr addr;
2429 entry = (struct ip_vs_service_entry *)arg;
2430 addr.ip = entry->addr;
2432 svc = __ip_vs_svc_fwm_get(AF_INET, entry->fwmark);
2434 svc = __ip_vs_service_get(AF_INET, entry->protocol,
2435 &addr, entry->port);
2437 ip_vs_copy_service(entry, svc);
2438 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2440 ip_vs_service_put(svc);
2446 case IP_VS_SO_GET_DESTS:
2448 struct ip_vs_get_dests *get;
2451 get = (struct ip_vs_get_dests *)arg;
2452 size = sizeof(*get) +
2453 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2455 IP_VS_ERR("length: %u != %u\n", *len, size);
2459 ret = __ip_vs_get_dest_entries(get, user);
2463 case IP_VS_SO_GET_TIMEOUT:
2465 struct ip_vs_timeout_user t;
2467 __ip_vs_get_timeouts(&t);
2468 if (copy_to_user(user, &t, sizeof(t)) != 0)
2473 case IP_VS_SO_GET_DAEMON:
2475 struct ip_vs_daemon_user d[2];
2477 memset(&d, 0, sizeof(d));
2478 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2479 d[0].state = IP_VS_STATE_MASTER;
2480 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
2481 d[0].syncid = ip_vs_master_syncid;
2483 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2484 d[1].state = IP_VS_STATE_BACKUP;
2485 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
2486 d[1].syncid = ip_vs_backup_syncid;
2488 if (copy_to_user(user, &d, sizeof(d)) != 0)
2498 mutex_unlock(&__ip_vs_mutex);
2503 static struct nf_sockopt_ops ip_vs_sockopts = {
2505 .set_optmin = IP_VS_BASE_CTL,
2506 .set_optmax = IP_VS_SO_SET_MAX+1,
2507 .set = do_ip_vs_set_ctl,
2508 .get_optmin = IP_VS_BASE_CTL,
2509 .get_optmax = IP_VS_SO_GET_MAX+1,
2510 .get = do_ip_vs_get_ctl,
2511 .owner = THIS_MODULE,
2515 * Generic Netlink interface
2518 /* IPVS genetlink family */
2519 static struct genl_family ip_vs_genl_family = {
2520 .id = GENL_ID_GENERATE,
2522 .name = IPVS_GENL_NAME,
2523 .version = IPVS_GENL_VERSION,
2524 .maxattr = IPVS_CMD_MAX,
2527 /* Policy used for first-level command attributes */
2528 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2529 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2530 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2531 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2532 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2533 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2534 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2537 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2538 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2539 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2540 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2541 .len = IP_VS_IFNAME_MAXLEN },
2542 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2545 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2546 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2547 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2548 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2549 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2550 .len = sizeof(union nf_inet_addr) },
2551 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2552 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2553 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2554 .len = IP_VS_SCHEDNAME_MAXLEN },
2555 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2556 .len = sizeof(struct ip_vs_flags) },
2557 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2558 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2559 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2562 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2563 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2564 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2565 .len = sizeof(union nf_inet_addr) },
2566 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2567 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2568 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2569 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2570 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2571 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2572 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2573 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2574 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2577 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2578 struct ip_vs_stats *stats)
2580 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2584 spin_lock_bh(&stats->lock);
2586 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2587 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2588 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2589 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2590 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2591 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2592 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2593 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2594 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2595 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
2597 spin_unlock_bh(&stats->lock);
2599 nla_nest_end(skb, nl_stats);
2604 spin_unlock_bh(&stats->lock);
2605 nla_nest_cancel(skb, nl_stats);
2609 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2610 struct ip_vs_service *svc)
2612 struct nlattr *nl_service;
2613 struct ip_vs_flags flags = { .flags = svc->flags,
2616 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2620 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
2623 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2625 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2626 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2627 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2630 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2631 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2632 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2633 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2635 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2636 goto nla_put_failure;
2638 nla_nest_end(skb, nl_service);
2643 nla_nest_cancel(skb, nl_service);
2647 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2648 struct ip_vs_service *svc,
2649 struct netlink_callback *cb)
2653 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2654 &ip_vs_genl_family, NLM_F_MULTI,
2655 IPVS_CMD_NEW_SERVICE);
2659 if (ip_vs_genl_fill_service(skb, svc) < 0)
2660 goto nla_put_failure;
2662 return genlmsg_end(skb, hdr);
2665 genlmsg_cancel(skb, hdr);
2669 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2670 struct netlink_callback *cb)
2673 int start = cb->args[0];
2674 struct ip_vs_service *svc;
2676 mutex_lock(&__ip_vs_mutex);
2677 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2678 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2681 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2683 goto nla_put_failure;
2688 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2689 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2692 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2694 goto nla_put_failure;
2700 mutex_unlock(&__ip_vs_mutex);
2706 static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
2707 struct nlattr *nla, int full_entry)
2709 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2710 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2712 /* Parse mandatory identifying service fields first */
2714 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2717 nla_af = attrs[IPVS_SVC_ATTR_AF];
2718 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2719 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2720 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2721 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2723 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2726 usvc->af = nla_get_u16(nla_af);
2727 #ifdef CONFIG_IP_VS_IPV6
2728 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2730 if (usvc->af != AF_INET)
2732 return -EAFNOSUPPORT;
2735 usvc->protocol = IPPROTO_TCP;
2736 usvc->fwmark = nla_get_u32(nla_fwmark);
2738 usvc->protocol = nla_get_u16(nla_protocol);
2739 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2740 usvc->port = nla_get_u16(nla_port);
2744 /* If a full entry was requested, check for the additional fields */
2746 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2748 struct ip_vs_flags flags;
2749 struct ip_vs_service *svc;
2751 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2752 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2753 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2754 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2756 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2759 nla_memcpy(&flags, nla_flags, sizeof(flags));
2761 /* prefill flags from service if it already exists */
2763 svc = __ip_vs_svc_fwm_get(usvc->af, usvc->fwmark);
2765 svc = __ip_vs_service_get(usvc->af, usvc->protocol,
2766 &usvc->addr, usvc->port);
2768 usvc->flags = svc->flags;
2769 ip_vs_service_put(svc);
2773 /* set new flags from userland */
2774 usvc->flags = (usvc->flags & ~flags.mask) |
2775 (flags.flags & flags.mask);
2776 usvc->sched_name = nla_data(nla_sched);
2777 usvc->timeout = nla_get_u32(nla_timeout);
2778 usvc->netmask = nla_get_u32(nla_netmask);
2784 static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2786 struct ip_vs_service_user_kern usvc;
2789 ret = ip_vs_genl_parse_service(&usvc, nla, 0);
2791 return ERR_PTR(ret);
2794 return __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2796 return __ip_vs_service_get(usvc.af, usvc.protocol,
2797 &usvc.addr, usvc.port);
2800 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2802 struct nlattr *nl_dest;
2804 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2808 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2809 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2811 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2812 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2813 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2814 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2815 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2816 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2817 atomic_read(&dest->activeconns));
2818 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2819 atomic_read(&dest->inactconns));
2820 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2821 atomic_read(&dest->persistconns));
2823 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2824 goto nla_put_failure;
2826 nla_nest_end(skb, nl_dest);
2831 nla_nest_cancel(skb, nl_dest);
2835 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2836 struct netlink_callback *cb)
2840 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2841 &ip_vs_genl_family, NLM_F_MULTI,
2846 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2847 goto nla_put_failure;
2849 return genlmsg_end(skb, hdr);
2852 genlmsg_cancel(skb, hdr);
2856 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2857 struct netlink_callback *cb)
2860 int start = cb->args[0];
2861 struct ip_vs_service *svc;
2862 struct ip_vs_dest *dest;
2863 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2865 mutex_lock(&__ip_vs_mutex);
2867 /* Try to find the service for which to dump destinations */
2868 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2869 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2872 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2873 if (IS_ERR(svc) || svc == NULL)
2876 /* Dump the destinations */
2877 list_for_each_entry(dest, &svc->destinations, n_list) {
2880 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2882 goto nla_put_failure;
2888 ip_vs_service_put(svc);
2891 mutex_unlock(&__ip_vs_mutex);
2896 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
2897 struct nlattr *nla, int full_entry)
2899 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2900 struct nlattr *nla_addr, *nla_port;
2902 /* Parse mandatory identifying destination fields first */
2904 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2907 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2908 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2910 if (!(nla_addr && nla_port))
2913 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2914 udest->port = nla_get_u16(nla_port);
2916 /* If a full entry was requested, check for the additional fields */
2918 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2921 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2922 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2923 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2924 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2926 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2929 udest->conn_flags = nla_get_u32(nla_fwd)
2930 & IP_VS_CONN_F_FWD_MASK;
2931 udest->weight = nla_get_u32(nla_weight);
2932 udest->u_threshold = nla_get_u32(nla_u_thresh);
2933 udest->l_threshold = nla_get_u32(nla_l_thresh);
2939 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2940 const char *mcast_ifn, __be32 syncid)
2942 struct nlattr *nl_daemon;
2944 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2948 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2949 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2950 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2952 nla_nest_end(skb, nl_daemon);
2957 nla_nest_cancel(skb, nl_daemon);
2961 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2962 const char *mcast_ifn, __be32 syncid,
2963 struct netlink_callback *cb)
2966 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2967 &ip_vs_genl_family, NLM_F_MULTI,
2968 IPVS_CMD_NEW_DAEMON);
2972 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2973 goto nla_put_failure;
2975 return genlmsg_end(skb, hdr);
2978 genlmsg_cancel(skb, hdr);
2982 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2983 struct netlink_callback *cb)
2985 mutex_lock(&__ip_vs_mutex);
2986 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2987 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
2988 ip_vs_master_mcast_ifn,
2989 ip_vs_master_syncid, cb) < 0)
2990 goto nla_put_failure;
2995 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
2996 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
2997 ip_vs_backup_mcast_ifn,
2998 ip_vs_backup_syncid, cb) < 0)
2999 goto nla_put_failure;
3005 mutex_unlock(&__ip_vs_mutex);
3010 static int ip_vs_genl_new_daemon(struct nlattr **attrs)
3012 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3013 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3014 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3017 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3018 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3019 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3022 static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3024 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3027 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3030 static int ip_vs_genl_set_config(struct nlattr **attrs)
3032 struct ip_vs_timeout_user t;
3034 __ip_vs_get_timeouts(&t);
3036 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3037 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3039 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3041 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3043 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3044 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3046 return ip_vs_set_timeout(&t);
3049 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3051 struct ip_vs_service *svc = NULL;
3052 struct ip_vs_service_user_kern usvc;
3053 struct ip_vs_dest_user_kern udest;
3055 int need_full_svc = 0, need_full_dest = 0;
3057 cmd = info->genlhdr->cmd;
3059 mutex_lock(&__ip_vs_mutex);
3061 if (cmd == IPVS_CMD_FLUSH) {
3062 ret = ip_vs_flush();
3064 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3065 ret = ip_vs_genl_set_config(info->attrs);
3067 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3068 cmd == IPVS_CMD_DEL_DAEMON) {
3070 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3072 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3073 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3074 info->attrs[IPVS_CMD_ATTR_DAEMON],
3075 ip_vs_daemon_policy)) {
3080 if (cmd == IPVS_CMD_NEW_DAEMON)
3081 ret = ip_vs_genl_new_daemon(daemon_attrs);
3083 ret = ip_vs_genl_del_daemon(daemon_attrs);
3085 } else if (cmd == IPVS_CMD_ZERO &&
3086 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3087 ret = ip_vs_zero_all();
3091 /* All following commands require a service argument, so check if we
3092 * received a valid one. We need a full service specification when
3093 * adding / editing a service. Only identifying members otherwise. */
3094 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3097 ret = ip_vs_genl_parse_service(&usvc,
3098 info->attrs[IPVS_CMD_ATTR_SERVICE],
3103 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3104 if (usvc.fwmark == 0)
3105 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
3106 &usvc.addr, usvc.port);
3108 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
3110 /* Unless we're adding a new service, the service must already exist */
3111 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3116 /* Destination commands require a valid destination argument. For
3117 * adding / editing a destination, we need a full destination
3119 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3120 cmd == IPVS_CMD_DEL_DEST) {
3121 if (cmd != IPVS_CMD_DEL_DEST)
3124 ret = ip_vs_genl_parse_dest(&udest,
3125 info->attrs[IPVS_CMD_ATTR_DEST],
3132 case IPVS_CMD_NEW_SERVICE:
3134 ret = ip_vs_add_service(&usvc, &svc);
3138 case IPVS_CMD_SET_SERVICE:
3139 ret = ip_vs_edit_service(svc, &usvc);
3141 case IPVS_CMD_DEL_SERVICE:
3142 ret = ip_vs_del_service(svc);
3144 case IPVS_CMD_NEW_DEST:
3145 ret = ip_vs_add_dest(svc, &udest);
3147 case IPVS_CMD_SET_DEST:
3148 ret = ip_vs_edit_dest(svc, &udest);
3150 case IPVS_CMD_DEL_DEST:
3151 ret = ip_vs_del_dest(svc, &udest);
3154 ret = ip_vs_zero_service(svc);
3162 ip_vs_service_put(svc);
3163 mutex_unlock(&__ip_vs_mutex);
3168 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3170 struct sk_buff *msg;
3172 int ret, cmd, reply_cmd;
3174 cmd = info->genlhdr->cmd;
3176 if (cmd == IPVS_CMD_GET_SERVICE)
3177 reply_cmd = IPVS_CMD_NEW_SERVICE;
3178 else if (cmd == IPVS_CMD_GET_INFO)
3179 reply_cmd = IPVS_CMD_SET_INFO;
3180 else if (cmd == IPVS_CMD_GET_CONFIG)
3181 reply_cmd = IPVS_CMD_SET_CONFIG;
3183 IP_VS_ERR("unknown Generic Netlink command\n");
3187 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3191 mutex_lock(&__ip_vs_mutex);
3193 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3195 goto nla_put_failure;
3198 case IPVS_CMD_GET_SERVICE:
3200 struct ip_vs_service *svc;
3202 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3207 ret = ip_vs_genl_fill_service(msg, svc);
3208 ip_vs_service_put(svc);
3210 goto nla_put_failure;
3219 case IPVS_CMD_GET_CONFIG:
3221 struct ip_vs_timeout_user t;
3223 __ip_vs_get_timeouts(&t);
3224 #ifdef CONFIG_IP_VS_PROTO_TCP
3225 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3226 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3229 #ifdef CONFIG_IP_VS_PROTO_UDP
3230 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3236 case IPVS_CMD_GET_INFO:
3237 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3238 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3239 IP_VS_CONN_TAB_SIZE);
3243 genlmsg_end(msg, reply);
3244 ret = genlmsg_unicast(msg, info->snd_pid);
3248 IP_VS_ERR("not enough space in Netlink message\n");
3254 mutex_unlock(&__ip_vs_mutex);
3260 static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3262 .cmd = IPVS_CMD_NEW_SERVICE,
3263 .flags = GENL_ADMIN_PERM,
3264 .policy = ip_vs_cmd_policy,
3265 .doit = ip_vs_genl_set_cmd,
3268 .cmd = IPVS_CMD_SET_SERVICE,
3269 .flags = GENL_ADMIN_PERM,
3270 .policy = ip_vs_cmd_policy,
3271 .doit = ip_vs_genl_set_cmd,
3274 .cmd = IPVS_CMD_DEL_SERVICE,
3275 .flags = GENL_ADMIN_PERM,
3276 .policy = ip_vs_cmd_policy,
3277 .doit = ip_vs_genl_set_cmd,
3280 .cmd = IPVS_CMD_GET_SERVICE,
3281 .flags = GENL_ADMIN_PERM,
3282 .doit = ip_vs_genl_get_cmd,
3283 .dumpit = ip_vs_genl_dump_services,
3284 .policy = ip_vs_cmd_policy,
3287 .cmd = IPVS_CMD_NEW_DEST,
3288 .flags = GENL_ADMIN_PERM,
3289 .policy = ip_vs_cmd_policy,
3290 .doit = ip_vs_genl_set_cmd,
3293 .cmd = IPVS_CMD_SET_DEST,
3294 .flags = GENL_ADMIN_PERM,
3295 .policy = ip_vs_cmd_policy,
3296 .doit = ip_vs_genl_set_cmd,
3299 .cmd = IPVS_CMD_DEL_DEST,
3300 .flags = GENL_ADMIN_PERM,
3301 .policy = ip_vs_cmd_policy,
3302 .doit = ip_vs_genl_set_cmd,
3305 .cmd = IPVS_CMD_GET_DEST,
3306 .flags = GENL_ADMIN_PERM,
3307 .policy = ip_vs_cmd_policy,
3308 .dumpit = ip_vs_genl_dump_dests,
3311 .cmd = IPVS_CMD_NEW_DAEMON,
3312 .flags = GENL_ADMIN_PERM,
3313 .policy = ip_vs_cmd_policy,
3314 .doit = ip_vs_genl_set_cmd,
3317 .cmd = IPVS_CMD_DEL_DAEMON,
3318 .flags = GENL_ADMIN_PERM,
3319 .policy = ip_vs_cmd_policy,
3320 .doit = ip_vs_genl_set_cmd,
3323 .cmd = IPVS_CMD_GET_DAEMON,
3324 .flags = GENL_ADMIN_PERM,
3325 .dumpit = ip_vs_genl_dump_daemons,
3328 .cmd = IPVS_CMD_SET_CONFIG,
3329 .flags = GENL_ADMIN_PERM,
3330 .policy = ip_vs_cmd_policy,
3331 .doit = ip_vs_genl_set_cmd,
3334 .cmd = IPVS_CMD_GET_CONFIG,
3335 .flags = GENL_ADMIN_PERM,
3336 .doit = ip_vs_genl_get_cmd,
3339 .cmd = IPVS_CMD_GET_INFO,
3340 .flags = GENL_ADMIN_PERM,
3341 .doit = ip_vs_genl_get_cmd,
3344 .cmd = IPVS_CMD_ZERO,
3345 .flags = GENL_ADMIN_PERM,
3346 .policy = ip_vs_cmd_policy,
3347 .doit = ip_vs_genl_set_cmd,
3350 .cmd = IPVS_CMD_FLUSH,
3351 .flags = GENL_ADMIN_PERM,
3352 .doit = ip_vs_genl_set_cmd,
3356 static int __init ip_vs_genl_register(void)
3360 ret = genl_register_family(&ip_vs_genl_family);
3364 for (i = 0; i < ARRAY_SIZE(ip_vs_genl_ops); i++) {
3365 ret = genl_register_ops(&ip_vs_genl_family, &ip_vs_genl_ops[i]);
3372 genl_unregister_family(&ip_vs_genl_family);
3376 static void ip_vs_genl_unregister(void)
3378 genl_unregister_family(&ip_vs_genl_family);
3381 /* End of Generic Netlink interface definitions */
3384 int __init ip_vs_control_init(void)
3391 ret = nf_register_sockopt(&ip_vs_sockopts);
3393 IP_VS_ERR("cannot register sockopt.\n");
3397 ret = ip_vs_genl_register();
3399 IP_VS_ERR("cannot register Generic Netlink interface.\n");
3400 nf_unregister_sockopt(&ip_vs_sockopts);
3404 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3405 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
3407 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
3409 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3410 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3411 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3412 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3414 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3415 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3418 ip_vs_new_estimator(&ip_vs_stats);
3420 /* Hook the defense timer */
3421 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3428 void ip_vs_control_cleanup(void)
3431 ip_vs_trash_cleanup();
3432 cancel_rearming_delayed_work(&defense_work);
3433 cancel_work_sync(&defense_work.work);
3434 ip_vs_kill_estimator(&ip_vs_stats);
3435 unregister_sysctl_table(sysctl_header);
3436 proc_net_remove(&init_net, "ip_vs_stats");
3437 proc_net_remove(&init_net, "ip_vs");
3438 ip_vs_genl_unregister();
3439 nf_unregister_sockopt(&ip_vs_sockopts);