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>
35 #include <linux/netfilter.h>
36 #include <linux/netfilter_ipv4.h>
37 #include <linux/mutex.h>
39 #include <net/net_namespace.h>
41 #ifdef CONFIG_IP_VS_IPV6
43 #include <net/ip6_route.h>
45 #include <net/route.h>
47 #include <net/genetlink.h>
49 #include <asm/uaccess.h>
51 #include <net/ip_vs.h>
53 /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
54 static DEFINE_MUTEX(__ip_vs_mutex);
56 /* lock for service table */
57 static DEFINE_RWLOCK(__ip_vs_svc_lock);
59 /* lock for table with the real services */
60 static DEFINE_RWLOCK(__ip_vs_rs_lock);
62 /* lock for state and timeout tables */
63 static DEFINE_RWLOCK(__ip_vs_securetcp_lock);
65 /* lock for drop entry handling */
66 static DEFINE_SPINLOCK(__ip_vs_dropentry_lock);
68 /* lock for drop packet handling */
69 static DEFINE_SPINLOCK(__ip_vs_droppacket_lock);
71 /* 1/rate drop and drop-entry variables */
72 int ip_vs_drop_rate = 0;
73 int ip_vs_drop_counter = 0;
74 static atomic_t ip_vs_dropentry = ATOMIC_INIT(0);
76 /* number of virtual services */
77 static int ip_vs_num_services = 0;
79 /* sysctl variables */
80 static int sysctl_ip_vs_drop_entry = 0;
81 static int sysctl_ip_vs_drop_packet = 0;
82 static int sysctl_ip_vs_secure_tcp = 0;
83 static int sysctl_ip_vs_amemthresh = 1024;
84 static int sysctl_ip_vs_am_droprate = 10;
85 int sysctl_ip_vs_cache_bypass = 0;
86 int sysctl_ip_vs_expire_nodest_conn = 0;
87 int sysctl_ip_vs_expire_quiescent_template = 0;
88 int sysctl_ip_vs_sync_threshold[2] = { 3, 50 };
89 int sysctl_ip_vs_nat_icmp_send = 0;
92 #ifdef CONFIG_IP_VS_DEBUG
93 static int sysctl_ip_vs_debug_level = 0;
95 int ip_vs_get_debug_level(void)
97 return sysctl_ip_vs_debug_level;
101 #ifdef CONFIG_IP_VS_IPV6
102 /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
103 static int __ip_vs_addr_is_local_v6(const struct in6_addr *addr)
111 .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
114 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
115 if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
122 * update_defense_level is called from keventd and from sysctl,
123 * so it needs to protect itself from softirqs
125 static void update_defense_level(void)
128 static int old_secure_tcp = 0;
133 /* we only count free and buffered memory (in pages) */
135 availmem = i.freeram + i.bufferram;
136 /* however in linux 2.5 the i.bufferram is total page cache size,
138 /* si_swapinfo(&i); */
139 /* availmem = availmem - (i.totalswap - i.freeswap); */
141 nomem = (availmem < sysctl_ip_vs_amemthresh);
146 spin_lock(&__ip_vs_dropentry_lock);
147 switch (sysctl_ip_vs_drop_entry) {
149 atomic_set(&ip_vs_dropentry, 0);
153 atomic_set(&ip_vs_dropentry, 1);
154 sysctl_ip_vs_drop_entry = 2;
156 atomic_set(&ip_vs_dropentry, 0);
161 atomic_set(&ip_vs_dropentry, 1);
163 atomic_set(&ip_vs_dropentry, 0);
164 sysctl_ip_vs_drop_entry = 1;
168 atomic_set(&ip_vs_dropentry, 1);
171 spin_unlock(&__ip_vs_dropentry_lock);
174 spin_lock(&__ip_vs_droppacket_lock);
175 switch (sysctl_ip_vs_drop_packet) {
181 ip_vs_drop_rate = ip_vs_drop_counter
182 = sysctl_ip_vs_amemthresh /
183 (sysctl_ip_vs_amemthresh-availmem);
184 sysctl_ip_vs_drop_packet = 2;
191 ip_vs_drop_rate = ip_vs_drop_counter
192 = sysctl_ip_vs_amemthresh /
193 (sysctl_ip_vs_amemthresh-availmem);
196 sysctl_ip_vs_drop_packet = 1;
200 ip_vs_drop_rate = sysctl_ip_vs_am_droprate;
203 spin_unlock(&__ip_vs_droppacket_lock);
206 write_lock(&__ip_vs_securetcp_lock);
207 switch (sysctl_ip_vs_secure_tcp) {
209 if (old_secure_tcp >= 2)
214 if (old_secure_tcp < 2)
216 sysctl_ip_vs_secure_tcp = 2;
218 if (old_secure_tcp >= 2)
224 if (old_secure_tcp < 2)
227 if (old_secure_tcp >= 2)
229 sysctl_ip_vs_secure_tcp = 1;
233 if (old_secure_tcp < 2)
237 old_secure_tcp = sysctl_ip_vs_secure_tcp;
239 ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp>1);
240 write_unlock(&__ip_vs_securetcp_lock);
247 * Timer for checking the defense
249 #define DEFENSE_TIMER_PERIOD 1*HZ
250 static void defense_work_handler(struct work_struct *work);
251 static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
253 static void defense_work_handler(struct work_struct *work)
255 update_defense_level();
256 if (atomic_read(&ip_vs_dropentry))
257 ip_vs_random_dropentry();
259 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
263 ip_vs_use_count_inc(void)
265 return try_module_get(THIS_MODULE);
269 ip_vs_use_count_dec(void)
271 module_put(THIS_MODULE);
276 * Hash table: for virtual service lookups
278 #define IP_VS_SVC_TAB_BITS 8
279 #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
280 #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
282 /* the service table hashed by <protocol, addr, port> */
283 static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
284 /* the service table hashed by fwmark */
285 static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
288 * Hash table: for real service lookups
290 #define IP_VS_RTAB_BITS 4
291 #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
292 #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
294 static struct list_head ip_vs_rtable[IP_VS_RTAB_SIZE];
297 * Trash for destinations
299 static LIST_HEAD(ip_vs_dest_trash);
302 * FTP & NULL virtual service counters
304 static atomic_t ip_vs_ftpsvc_counter = ATOMIC_INIT(0);
305 static atomic_t ip_vs_nullsvc_counter = ATOMIC_INIT(0);
309 * Returns hash value for virtual service
311 static __inline__ unsigned
312 ip_vs_svc_hashkey(int af, unsigned proto, const union nf_inet_addr *addr,
315 register unsigned porth = ntohs(port);
316 __be32 addr_fold = addr->ip;
318 #ifdef CONFIG_IP_VS_IPV6
320 addr_fold = addr->ip6[0]^addr->ip6[1]^
321 addr->ip6[2]^addr->ip6[3];
324 return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
325 & IP_VS_SVC_TAB_MASK;
329 * Returns hash value of fwmark for virtual service lookup
331 static __inline__ unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark)
333 return fwmark & IP_VS_SVC_TAB_MASK;
337 * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
338 * or in the ip_vs_svc_fwm_table by fwmark.
339 * Should be called with locked tables.
341 static int ip_vs_svc_hash(struct ip_vs_service *svc)
345 if (svc->flags & IP_VS_SVC_F_HASHED) {
346 pr_err("%s(): request for already hashed, called from %pF\n",
347 __func__, __builtin_return_address(0));
351 if (svc->fwmark == 0) {
353 * Hash it by <protocol,addr,port> in ip_vs_svc_table
355 hash = ip_vs_svc_hashkey(svc->af, svc->protocol, &svc->addr,
357 list_add(&svc->s_list, &ip_vs_svc_table[hash]);
360 * Hash it by fwmark in ip_vs_svc_fwm_table
362 hash = ip_vs_svc_fwm_hashkey(svc->fwmark);
363 list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
366 svc->flags |= IP_VS_SVC_F_HASHED;
367 /* increase its refcnt because it is referenced by the svc table */
368 atomic_inc(&svc->refcnt);
374 * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
375 * Should be called with locked tables.
377 static int ip_vs_svc_unhash(struct ip_vs_service *svc)
379 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
380 pr_err("%s(): request for unhash flagged, called from %pF\n",
381 __func__, __builtin_return_address(0));
385 if (svc->fwmark == 0) {
386 /* Remove it from the ip_vs_svc_table table */
387 list_del(&svc->s_list);
389 /* Remove it from the ip_vs_svc_fwm_table table */
390 list_del(&svc->f_list);
393 svc->flags &= ~IP_VS_SVC_F_HASHED;
394 atomic_dec(&svc->refcnt);
400 * Get service by {proto,addr,port} in the service table.
402 static inline struct ip_vs_service *
403 __ip_vs_service_get(int af, __u16 protocol, const union nf_inet_addr *vaddr,
407 struct ip_vs_service *svc;
409 /* Check for "full" addressed entries */
410 hash = ip_vs_svc_hashkey(af, protocol, vaddr, vport);
412 list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
414 && ip_vs_addr_equal(af, &svc->addr, vaddr)
415 && (svc->port == vport)
416 && (svc->protocol == protocol)) {
418 atomic_inc(&svc->usecnt);
428 * Get service by {fwmark} in the service table.
430 static inline struct ip_vs_service *
431 __ip_vs_svc_fwm_get(int af, __u32 fwmark)
434 struct ip_vs_service *svc;
436 /* Check for fwmark addressed entries */
437 hash = ip_vs_svc_fwm_hashkey(fwmark);
439 list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) {
440 if (svc->fwmark == fwmark && svc->af == af) {
442 atomic_inc(&svc->usecnt);
450 struct ip_vs_service *
451 ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
452 const union nf_inet_addr *vaddr, __be16 vport)
454 struct ip_vs_service *svc;
456 read_lock(&__ip_vs_svc_lock);
459 * Check the table hashed by fwmark first
461 if (fwmark && (svc = __ip_vs_svc_fwm_get(af, fwmark)))
465 * Check the table hashed by <protocol,addr,port>
466 * for "full" addressed entries
468 svc = __ip_vs_service_get(af, protocol, vaddr, vport);
471 && protocol == IPPROTO_TCP
472 && atomic_read(&ip_vs_ftpsvc_counter)
473 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
475 * Check if ftp service entry exists, the packet
476 * might belong to FTP data connections.
478 svc = __ip_vs_service_get(af, protocol, vaddr, FTPPORT);
482 && atomic_read(&ip_vs_nullsvc_counter)) {
484 * Check if the catch-all port (port zero) exists
486 svc = __ip_vs_service_get(af, protocol, vaddr, 0);
490 read_unlock(&__ip_vs_svc_lock);
492 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
493 fwmark, ip_vs_proto_name(protocol),
494 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
495 svc ? "hit" : "not hit");
502 __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
504 atomic_inc(&svc->refcnt);
509 __ip_vs_unbind_svc(struct ip_vs_dest *dest)
511 struct ip_vs_service *svc = dest->svc;
514 if (atomic_dec_and_test(&svc->refcnt))
520 * Returns hash value for real service
522 static inline unsigned ip_vs_rs_hashkey(int af,
523 const union nf_inet_addr *addr,
526 register unsigned porth = ntohs(port);
527 __be32 addr_fold = addr->ip;
529 #ifdef CONFIG_IP_VS_IPV6
531 addr_fold = addr->ip6[0]^addr->ip6[1]^
532 addr->ip6[2]^addr->ip6[3];
535 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
540 * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
541 * should be called with locked tables.
543 static int ip_vs_rs_hash(struct ip_vs_dest *dest)
547 if (!list_empty(&dest->d_list)) {
552 * Hash by proto,addr,port,
553 * which are the parameters of the real service.
555 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
557 list_add(&dest->d_list, &ip_vs_rtable[hash]);
563 * UNhashes ip_vs_dest from ip_vs_rtable.
564 * should be called with locked tables.
566 static int ip_vs_rs_unhash(struct ip_vs_dest *dest)
569 * Remove it from the ip_vs_rtable table.
571 if (!list_empty(&dest->d_list)) {
572 list_del(&dest->d_list);
573 INIT_LIST_HEAD(&dest->d_list);
580 * Lookup real service by <proto,addr,port> in the real service table.
583 ip_vs_lookup_real_service(int af, __u16 protocol,
584 const union nf_inet_addr *daddr,
588 struct ip_vs_dest *dest;
591 * Check for "full" addressed entries
592 * Return the first found entry
594 hash = ip_vs_rs_hashkey(af, daddr, dport);
596 read_lock(&__ip_vs_rs_lock);
597 list_for_each_entry(dest, &ip_vs_rtable[hash], d_list) {
599 && ip_vs_addr_equal(af, &dest->addr, daddr)
600 && (dest->port == dport)
601 && ((dest->protocol == protocol) ||
604 read_unlock(&__ip_vs_rs_lock);
608 read_unlock(&__ip_vs_rs_lock);
614 * Lookup destination by {addr,port} in the given service
616 static struct ip_vs_dest *
617 ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
620 struct ip_vs_dest *dest;
623 * Find the destination for the given service
625 list_for_each_entry(dest, &svc->destinations, n_list) {
626 if ((dest->af == svc->af)
627 && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
628 && (dest->port == dport)) {
638 * Find destination by {daddr,dport,vaddr,protocol}
639 * Cretaed to be used in ip_vs_process_message() in
640 * the backup synchronization daemon. It finds the
641 * destination to be bound to the received connection
644 * ip_vs_lookup_real_service() looked promissing, but
645 * seems not working as expected.
647 struct ip_vs_dest *ip_vs_find_dest(int af, const union nf_inet_addr *daddr,
649 const union nf_inet_addr *vaddr,
650 __be16 vport, __u16 protocol)
652 struct ip_vs_dest *dest;
653 struct ip_vs_service *svc;
655 svc = ip_vs_service_get(af, 0, protocol, vaddr, vport);
658 dest = ip_vs_lookup_dest(svc, daddr, dport);
660 atomic_inc(&dest->refcnt);
661 ip_vs_service_put(svc);
666 * Lookup dest by {svc,addr,port} in the destination trash.
667 * The destination trash is used to hold the destinations that are removed
668 * from the service table but are still referenced by some conn entries.
669 * The reason to add the destination trash is when the dest is temporary
670 * down (either by administrator or by monitor program), the dest can be
671 * picked back from the trash, the remaining connections to the dest can
672 * continue, and the counting information of the dest is also useful for
675 static struct ip_vs_dest *
676 ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
679 struct ip_vs_dest *dest, *nxt;
682 * Find the destination in trash
684 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
685 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
688 IP_VS_DBG_ADDR(svc->af, &dest->addr),
690 atomic_read(&dest->refcnt));
691 if (dest->af == svc->af &&
692 ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
693 dest->port == dport &&
694 dest->vfwmark == svc->fwmark &&
695 dest->protocol == svc->protocol &&
697 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
698 dest->vport == svc->port))) {
704 * Try to purge the destination from trash if not referenced
706 if (atomic_read(&dest->refcnt) == 1) {
707 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
710 IP_VS_DBG_ADDR(svc->af, &dest->addr),
712 list_del(&dest->n_list);
713 ip_vs_dst_reset(dest);
714 __ip_vs_unbind_svc(dest);
724 * Clean up all the destinations in the trash
725 * Called by the ip_vs_control_cleanup()
727 * When the ip_vs_control_clearup is activated by ipvs module exit,
728 * the service tables must have been flushed and all the connections
729 * are expired, and the refcnt of each destination in the trash must
730 * be 1, so we simply release them here.
732 static void ip_vs_trash_cleanup(void)
734 struct ip_vs_dest *dest, *nxt;
736 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
737 list_del(&dest->n_list);
738 ip_vs_dst_reset(dest);
739 __ip_vs_unbind_svc(dest);
746 ip_vs_zero_stats(struct ip_vs_stats *stats)
748 spin_lock_bh(&stats->lock);
750 memset(&stats->ustats, 0, sizeof(stats->ustats));
751 ip_vs_zero_estimator(stats);
753 spin_unlock_bh(&stats->lock);
757 * Update a destination in the given service
760 __ip_vs_update_dest(struct ip_vs_service *svc,
761 struct ip_vs_dest *dest, struct ip_vs_dest_user_kern *udest)
765 /* set the weight and the flags */
766 atomic_set(&dest->weight, udest->weight);
767 conn_flags = udest->conn_flags | IP_VS_CONN_F_INACTIVE;
769 /* check if local node and update the flags */
770 #ifdef CONFIG_IP_VS_IPV6
771 if (svc->af == AF_INET6) {
772 if (__ip_vs_addr_is_local_v6(&udest->addr.in6)) {
773 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
774 | IP_VS_CONN_F_LOCALNODE;
778 if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
779 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
780 | IP_VS_CONN_F_LOCALNODE;
783 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
784 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
785 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
788 * Put the real service in ip_vs_rtable if not present.
789 * For now only for NAT!
791 write_lock_bh(&__ip_vs_rs_lock);
793 write_unlock_bh(&__ip_vs_rs_lock);
795 atomic_set(&dest->conn_flags, conn_flags);
797 /* bind the service */
799 __ip_vs_bind_svc(dest, svc);
801 if (dest->svc != svc) {
802 __ip_vs_unbind_svc(dest);
803 ip_vs_zero_stats(&dest->stats);
804 __ip_vs_bind_svc(dest, svc);
808 /* set the dest status flags */
809 dest->flags |= IP_VS_DEST_F_AVAILABLE;
811 if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
812 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
813 dest->u_threshold = udest->u_threshold;
814 dest->l_threshold = udest->l_threshold;
819 * Create a destination for the given service
822 ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
823 struct ip_vs_dest **dest_p)
825 struct ip_vs_dest *dest;
830 #ifdef CONFIG_IP_VS_IPV6
831 if (svc->af == AF_INET6) {
832 atype = ipv6_addr_type(&udest->addr.in6);
833 if ((!(atype & IPV6_ADDR_UNICAST) ||
834 atype & IPV6_ADDR_LINKLOCAL) &&
835 !__ip_vs_addr_is_local_v6(&udest->addr.in6))
840 atype = inet_addr_type(&init_net, udest->addr.ip);
841 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
845 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
847 pr_err("%s(): no memory.\n", __func__);
852 dest->protocol = svc->protocol;
853 dest->vaddr = svc->addr;
854 dest->vport = svc->port;
855 dest->vfwmark = svc->fwmark;
856 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
857 dest->port = udest->port;
859 atomic_set(&dest->activeconns, 0);
860 atomic_set(&dest->inactconns, 0);
861 atomic_set(&dest->persistconns, 0);
862 atomic_set(&dest->refcnt, 0);
864 INIT_LIST_HEAD(&dest->d_list);
865 spin_lock_init(&dest->dst_lock);
866 spin_lock_init(&dest->stats.lock);
867 __ip_vs_update_dest(svc, dest, udest);
868 ip_vs_new_estimator(&dest->stats);
878 * Add a destination into an existing service
881 ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
883 struct ip_vs_dest *dest;
884 union nf_inet_addr daddr;
885 __be16 dport = udest->port;
890 if (udest->weight < 0) {
891 pr_err("%s(): server weight less than zero\n", __func__);
895 if (udest->l_threshold > udest->u_threshold) {
896 pr_err("%s(): lower threshold is higher than upper threshold\n",
901 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
904 * Check if the dest already exists in the list
906 dest = ip_vs_lookup_dest(svc, &daddr, dport);
909 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
914 * Check if the dest already exists in the trash and
915 * is from the same service
917 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
920 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
921 "dest->refcnt=%d, service %u/%s:%u\n",
922 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
923 atomic_read(&dest->refcnt),
925 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
928 __ip_vs_update_dest(svc, dest, udest);
931 * Get the destination from the trash
933 list_del(&dest->n_list);
935 ip_vs_new_estimator(&dest->stats);
937 write_lock_bh(&__ip_vs_svc_lock);
940 * Wait until all other svc users go away.
942 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
944 list_add(&dest->n_list, &svc->destinations);
947 /* call the update_service function of its scheduler */
948 if (svc->scheduler->update_service)
949 svc->scheduler->update_service(svc);
951 write_unlock_bh(&__ip_vs_svc_lock);
956 * Allocate and initialize the dest structure
958 ret = ip_vs_new_dest(svc, udest, &dest);
964 * Add the dest entry into the list
966 atomic_inc(&dest->refcnt);
968 write_lock_bh(&__ip_vs_svc_lock);
971 * Wait until all other svc users go away.
973 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
975 list_add(&dest->n_list, &svc->destinations);
978 /* call the update_service function of its scheduler */
979 if (svc->scheduler->update_service)
980 svc->scheduler->update_service(svc);
982 write_unlock_bh(&__ip_vs_svc_lock);
991 * Edit a destination in the given service
994 ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
996 struct ip_vs_dest *dest;
997 union nf_inet_addr daddr;
998 __be16 dport = udest->port;
1002 if (udest->weight < 0) {
1003 pr_err("%s(): server weight less than zero\n", __func__);
1007 if (udest->l_threshold > udest->u_threshold) {
1008 pr_err("%s(): lower threshold is higher than upper threshold\n",
1013 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
1016 * Lookup the destination list
1018 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1021 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
1025 __ip_vs_update_dest(svc, dest, udest);
1027 write_lock_bh(&__ip_vs_svc_lock);
1029 /* Wait until all other svc users go away */
1030 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1032 /* call the update_service, because server weight may be changed */
1033 if (svc->scheduler->update_service)
1034 svc->scheduler->update_service(svc);
1036 write_unlock_bh(&__ip_vs_svc_lock);
1045 * Delete a destination (must be already unlinked from the service)
1047 static void __ip_vs_del_dest(struct ip_vs_dest *dest)
1049 ip_vs_kill_estimator(&dest->stats);
1052 * Remove it from the d-linked list with the real services.
1054 write_lock_bh(&__ip_vs_rs_lock);
1055 ip_vs_rs_unhash(dest);
1056 write_unlock_bh(&__ip_vs_rs_lock);
1059 * Decrease the refcnt of the dest, and free the dest
1060 * if nobody refers to it (refcnt=0). Otherwise, throw
1061 * the destination into the trash.
1063 if (atomic_dec_and_test(&dest->refcnt)) {
1064 ip_vs_dst_reset(dest);
1065 /* simply decrease svc->refcnt here, let the caller check
1066 and release the service if nobody refers to it.
1067 Only user context can release destination and service,
1068 and only one user context can update virtual service at a
1069 time, so the operation here is OK */
1070 atomic_dec(&dest->svc->refcnt);
1073 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1074 "dest->refcnt=%d\n",
1075 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1077 atomic_read(&dest->refcnt));
1078 list_add(&dest->n_list, &ip_vs_dest_trash);
1079 atomic_inc(&dest->refcnt);
1085 * Unlink a destination from the given service
1087 static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1088 struct ip_vs_dest *dest,
1091 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1094 * Remove it from the d-linked destination list.
1096 list_del(&dest->n_list);
1100 * Call the update_service function of its scheduler
1102 if (svcupd && svc->scheduler->update_service)
1103 svc->scheduler->update_service(svc);
1108 * Delete a destination server in the given service
1111 ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1113 struct ip_vs_dest *dest;
1114 __be16 dport = udest->port;
1118 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
1121 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
1125 write_lock_bh(&__ip_vs_svc_lock);
1128 * Wait until all other svc users go away.
1130 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1133 * Unlink dest from the service
1135 __ip_vs_unlink_dest(svc, dest, 1);
1137 write_unlock_bh(&__ip_vs_svc_lock);
1140 * Delete the destination
1142 __ip_vs_del_dest(dest);
1151 * Add a service into the service hash table
1154 ip_vs_add_service(struct ip_vs_service_user_kern *u,
1155 struct ip_vs_service **svc_p)
1158 struct ip_vs_scheduler *sched = NULL;
1159 struct ip_vs_service *svc = NULL;
1161 /* increase the module use count */
1162 ip_vs_use_count_inc();
1164 /* Lookup the scheduler by 'u->sched_name' */
1165 sched = ip_vs_scheduler_get(u->sched_name);
1166 if (sched == NULL) {
1167 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1172 #ifdef CONFIG_IP_VS_IPV6
1173 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1179 svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
1181 IP_VS_DBG(1, "%s(): no memory\n", __func__);
1186 /* I'm the first user of the service */
1187 atomic_set(&svc->usecnt, 1);
1188 atomic_set(&svc->refcnt, 0);
1191 svc->protocol = u->protocol;
1192 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1193 svc->port = u->port;
1194 svc->fwmark = u->fwmark;
1195 svc->flags = u->flags;
1196 svc->timeout = u->timeout * HZ;
1197 svc->netmask = u->netmask;
1199 INIT_LIST_HEAD(&svc->destinations);
1200 rwlock_init(&svc->sched_lock);
1201 spin_lock_init(&svc->stats.lock);
1203 /* Bind the scheduler */
1204 ret = ip_vs_bind_scheduler(svc, sched);
1209 /* Update the virtual service counters */
1210 if (svc->port == FTPPORT)
1211 atomic_inc(&ip_vs_ftpsvc_counter);
1212 else if (svc->port == 0)
1213 atomic_inc(&ip_vs_nullsvc_counter);
1215 ip_vs_new_estimator(&svc->stats);
1217 /* Count only IPv4 services for old get/setsockopt interface */
1218 if (svc->af == AF_INET)
1219 ip_vs_num_services++;
1221 /* Hash the service into the service table */
1222 write_lock_bh(&__ip_vs_svc_lock);
1223 ip_vs_svc_hash(svc);
1224 write_unlock_bh(&__ip_vs_svc_lock);
1232 ip_vs_unbind_scheduler(svc);
1235 ip_vs_app_inc_put(svc->inc);
1240 ip_vs_scheduler_put(sched);
1243 /* decrease the module use count */
1244 ip_vs_use_count_dec();
1251 * Edit a service and bind it with a new scheduler
1254 ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1256 struct ip_vs_scheduler *sched, *old_sched;
1260 * Lookup the scheduler, by 'u->sched_name'
1262 sched = ip_vs_scheduler_get(u->sched_name);
1263 if (sched == NULL) {
1264 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
1269 #ifdef CONFIG_IP_VS_IPV6
1270 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1276 write_lock_bh(&__ip_vs_svc_lock);
1279 * Wait until all other svc users go away.
1281 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1284 * Set the flags and timeout value
1286 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1287 svc->timeout = u->timeout * HZ;
1288 svc->netmask = u->netmask;
1290 old_sched = svc->scheduler;
1291 if (sched != old_sched) {
1293 * Unbind the old scheduler
1295 if ((ret = ip_vs_unbind_scheduler(svc))) {
1301 * Bind the new scheduler
1303 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1305 * If ip_vs_bind_scheduler fails, restore the old
1307 * The main reason of failure is out of memory.
1309 * The question is if the old scheduler can be
1310 * restored all the time. TODO: if it cannot be
1311 * restored some time, we must delete the service,
1312 * otherwise the system may crash.
1314 ip_vs_bind_scheduler(svc, old_sched);
1321 write_unlock_bh(&__ip_vs_svc_lock);
1322 #ifdef CONFIG_IP_VS_IPV6
1327 ip_vs_scheduler_put(old_sched);
1334 * Delete a service from the service list
1335 * - The service must be unlinked, unlocked and not referenced!
1336 * - We are called under _bh lock
1338 static void __ip_vs_del_service(struct ip_vs_service *svc)
1340 struct ip_vs_dest *dest, *nxt;
1341 struct ip_vs_scheduler *old_sched;
1343 /* Count only IPv4 services for old get/setsockopt interface */
1344 if (svc->af == AF_INET)
1345 ip_vs_num_services--;
1347 ip_vs_kill_estimator(&svc->stats);
1349 /* Unbind scheduler */
1350 old_sched = svc->scheduler;
1351 ip_vs_unbind_scheduler(svc);
1353 ip_vs_scheduler_put(old_sched);
1355 /* Unbind app inc */
1357 ip_vs_app_inc_put(svc->inc);
1362 * Unlink the whole destination list
1364 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1365 __ip_vs_unlink_dest(svc, dest, 0);
1366 __ip_vs_del_dest(dest);
1370 * Update the virtual service counters
1372 if (svc->port == FTPPORT)
1373 atomic_dec(&ip_vs_ftpsvc_counter);
1374 else if (svc->port == 0)
1375 atomic_dec(&ip_vs_nullsvc_counter);
1378 * Free the service if nobody refers to it
1380 if (atomic_read(&svc->refcnt) == 0)
1383 /* decrease the module use count */
1384 ip_vs_use_count_dec();
1388 * Delete a service from the service list
1390 static int ip_vs_del_service(struct ip_vs_service *svc)
1396 * Unhash it from the service table
1398 write_lock_bh(&__ip_vs_svc_lock);
1400 ip_vs_svc_unhash(svc);
1403 * Wait until all the svc users go away.
1405 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1407 __ip_vs_del_service(svc);
1409 write_unlock_bh(&__ip_vs_svc_lock);
1416 * Flush all the virtual services
1418 static int ip_vs_flush(void)
1421 struct ip_vs_service *svc, *nxt;
1424 * Flush the service table hashed by <protocol,addr,port>
1426 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1427 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
1428 write_lock_bh(&__ip_vs_svc_lock);
1429 ip_vs_svc_unhash(svc);
1431 * Wait until all the svc users go away.
1433 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1434 __ip_vs_del_service(svc);
1435 write_unlock_bh(&__ip_vs_svc_lock);
1440 * Flush the service table hashed by fwmark
1442 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1443 list_for_each_entry_safe(svc, nxt,
1444 &ip_vs_svc_fwm_table[idx], f_list) {
1445 write_lock_bh(&__ip_vs_svc_lock);
1446 ip_vs_svc_unhash(svc);
1448 * Wait until all the svc users go away.
1450 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1451 __ip_vs_del_service(svc);
1452 write_unlock_bh(&__ip_vs_svc_lock);
1461 * Zero counters in a service or all services
1463 static int ip_vs_zero_service(struct ip_vs_service *svc)
1465 struct ip_vs_dest *dest;
1467 write_lock_bh(&__ip_vs_svc_lock);
1468 list_for_each_entry(dest, &svc->destinations, n_list) {
1469 ip_vs_zero_stats(&dest->stats);
1471 ip_vs_zero_stats(&svc->stats);
1472 write_unlock_bh(&__ip_vs_svc_lock);
1476 static int ip_vs_zero_all(void)
1479 struct ip_vs_service *svc;
1481 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1482 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1483 ip_vs_zero_service(svc);
1487 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1488 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1489 ip_vs_zero_service(svc);
1493 ip_vs_zero_stats(&ip_vs_stats);
1499 proc_do_defense_mode(ctl_table *table, int write,
1500 void __user *buffer, size_t *lenp, loff_t *ppos)
1502 int *valp = table->data;
1506 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1507 if (write && (*valp != val)) {
1508 if ((*valp < 0) || (*valp > 3)) {
1509 /* Restore the correct value */
1512 update_defense_level();
1520 proc_do_sync_threshold(ctl_table *table, int write,
1521 void __user *buffer, size_t *lenp, loff_t *ppos)
1523 int *valp = table->data;
1527 /* backup the value first */
1528 memcpy(val, valp, sizeof(val));
1530 rc = proc_dointvec(table, write, buffer, lenp, ppos);
1531 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1532 /* Restore the correct value */
1533 memcpy(valp, val, sizeof(val));
1540 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1543 static struct ctl_table vs_vars[] = {
1545 .procname = "amemthresh",
1546 .data = &sysctl_ip_vs_amemthresh,
1547 .maxlen = sizeof(int),
1549 .proc_handler = proc_dointvec,
1551 #ifdef CONFIG_IP_VS_DEBUG
1553 .procname = "debug_level",
1554 .data = &sysctl_ip_vs_debug_level,
1555 .maxlen = sizeof(int),
1557 .proc_handler = proc_dointvec,
1561 .procname = "am_droprate",
1562 .data = &sysctl_ip_vs_am_droprate,
1563 .maxlen = sizeof(int),
1565 .proc_handler = proc_dointvec,
1568 .procname = "drop_entry",
1569 .data = &sysctl_ip_vs_drop_entry,
1570 .maxlen = sizeof(int),
1572 .proc_handler = proc_do_defense_mode,
1575 .procname = "drop_packet",
1576 .data = &sysctl_ip_vs_drop_packet,
1577 .maxlen = sizeof(int),
1579 .proc_handler = proc_do_defense_mode,
1582 .procname = "secure_tcp",
1583 .data = &sysctl_ip_vs_secure_tcp,
1584 .maxlen = sizeof(int),
1586 .proc_handler = proc_do_defense_mode,
1590 .procname = "timeout_established",
1591 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1592 .maxlen = sizeof(int),
1594 .proc_handler = proc_dointvec_jiffies,
1597 .procname = "timeout_synsent",
1598 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1599 .maxlen = sizeof(int),
1601 .proc_handler = proc_dointvec_jiffies,
1604 .procname = "timeout_synrecv",
1605 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1606 .maxlen = sizeof(int),
1608 .proc_handler = proc_dointvec_jiffies,
1611 .procname = "timeout_finwait",
1612 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1613 .maxlen = sizeof(int),
1615 .proc_handler = proc_dointvec_jiffies,
1618 .procname = "timeout_timewait",
1619 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1620 .maxlen = sizeof(int),
1622 .proc_handler = proc_dointvec_jiffies,
1625 .procname = "timeout_close",
1626 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1627 .maxlen = sizeof(int),
1629 .proc_handler = proc_dointvec_jiffies,
1632 .procname = "timeout_closewait",
1633 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1634 .maxlen = sizeof(int),
1636 .proc_handler = proc_dointvec_jiffies,
1639 .procname = "timeout_lastack",
1640 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1641 .maxlen = sizeof(int),
1643 .proc_handler = proc_dointvec_jiffies,
1646 .procname = "timeout_listen",
1647 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1648 .maxlen = sizeof(int),
1650 .proc_handler = proc_dointvec_jiffies,
1653 .procname = "timeout_synack",
1654 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1655 .maxlen = sizeof(int),
1657 .proc_handler = proc_dointvec_jiffies,
1660 .procname = "timeout_udp",
1661 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1662 .maxlen = sizeof(int),
1664 .proc_handler = proc_dointvec_jiffies,
1667 .procname = "timeout_icmp",
1668 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1669 .maxlen = sizeof(int),
1671 .proc_handler = proc_dointvec_jiffies,
1675 .procname = "cache_bypass",
1676 .data = &sysctl_ip_vs_cache_bypass,
1677 .maxlen = sizeof(int),
1679 .proc_handler = proc_dointvec,
1682 .procname = "expire_nodest_conn",
1683 .data = &sysctl_ip_vs_expire_nodest_conn,
1684 .maxlen = sizeof(int),
1686 .proc_handler = proc_dointvec,
1689 .procname = "expire_quiescent_template",
1690 .data = &sysctl_ip_vs_expire_quiescent_template,
1691 .maxlen = sizeof(int),
1693 .proc_handler = proc_dointvec,
1696 .procname = "sync_threshold",
1697 .data = &sysctl_ip_vs_sync_threshold,
1698 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1700 .proc_handler = proc_do_sync_threshold,
1703 .procname = "nat_icmp_send",
1704 .data = &sysctl_ip_vs_nat_icmp_send,
1705 .maxlen = sizeof(int),
1707 .proc_handler = proc_dointvec,
1712 const struct ctl_path net_vs_ctl_path[] = {
1713 { .procname = "net", },
1714 { .procname = "ipv4", },
1715 { .procname = "vs", },
1718 EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1720 static struct ctl_table_header * sysctl_header;
1722 #ifdef CONFIG_PROC_FS
1725 struct list_head *table;
1730 * Write the contents of the VS rule table to a PROCfs file.
1731 * (It is kept just for backward compatibility)
1733 static inline const char *ip_vs_fwd_name(unsigned flags)
1735 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1736 case IP_VS_CONN_F_LOCALNODE:
1738 case IP_VS_CONN_F_TUNNEL:
1740 case IP_VS_CONN_F_DROUTE:
1748 /* Get the Nth entry in the two lists */
1749 static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1751 struct ip_vs_iter *iter = seq->private;
1753 struct ip_vs_service *svc;
1755 /* look in hash by protocol */
1756 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1757 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1759 iter->table = ip_vs_svc_table;
1766 /* keep looking in fwmark */
1767 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1768 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1770 iter->table = ip_vs_svc_fwm_table;
1780 static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1781 __acquires(__ip_vs_svc_lock)
1784 read_lock_bh(&__ip_vs_svc_lock);
1785 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1789 static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1791 struct list_head *e;
1792 struct ip_vs_iter *iter;
1793 struct ip_vs_service *svc;
1796 if (v == SEQ_START_TOKEN)
1797 return ip_vs_info_array(seq,0);
1800 iter = seq->private;
1802 if (iter->table == ip_vs_svc_table) {
1803 /* next service in table hashed by protocol */
1804 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1805 return list_entry(e, struct ip_vs_service, s_list);
1808 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1809 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1815 iter->table = ip_vs_svc_fwm_table;
1820 /* next service in hashed by fwmark */
1821 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1822 return list_entry(e, struct ip_vs_service, f_list);
1825 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1826 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1834 static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1835 __releases(__ip_vs_svc_lock)
1837 read_unlock_bh(&__ip_vs_svc_lock);
1841 static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1843 if (v == SEQ_START_TOKEN) {
1845 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1846 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
1848 "Prot LocalAddress:Port Scheduler Flags\n");
1850 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1852 const struct ip_vs_service *svc = v;
1853 const struct ip_vs_iter *iter = seq->private;
1854 const struct ip_vs_dest *dest;
1856 if (iter->table == ip_vs_svc_table) {
1857 #ifdef CONFIG_IP_VS_IPV6
1858 if (svc->af == AF_INET6)
1859 seq_printf(seq, "%s [%pI6]:%04X %s ",
1860 ip_vs_proto_name(svc->protocol),
1863 svc->scheduler->name);
1866 seq_printf(seq, "%s %08X:%04X %s ",
1867 ip_vs_proto_name(svc->protocol),
1868 ntohl(svc->addr.ip),
1870 svc->scheduler->name);
1872 seq_printf(seq, "FWM %08X %s ",
1873 svc->fwmark, svc->scheduler->name);
1876 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1877 seq_printf(seq, "persistent %d %08X\n",
1879 ntohl(svc->netmask));
1881 seq_putc(seq, '\n');
1883 list_for_each_entry(dest, &svc->destinations, n_list) {
1884 #ifdef CONFIG_IP_VS_IPV6
1885 if (dest->af == AF_INET6)
1888 " %-7s %-6d %-10d %-10d\n",
1891 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1892 atomic_read(&dest->weight),
1893 atomic_read(&dest->activeconns),
1894 atomic_read(&dest->inactconns));
1899 "%-7s %-6d %-10d %-10d\n",
1900 ntohl(dest->addr.ip),
1902 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1903 atomic_read(&dest->weight),
1904 atomic_read(&dest->activeconns),
1905 atomic_read(&dest->inactconns));
1912 static const struct seq_operations ip_vs_info_seq_ops = {
1913 .start = ip_vs_info_seq_start,
1914 .next = ip_vs_info_seq_next,
1915 .stop = ip_vs_info_seq_stop,
1916 .show = ip_vs_info_seq_show,
1919 static int ip_vs_info_open(struct inode *inode, struct file *file)
1921 return seq_open_private(file, &ip_vs_info_seq_ops,
1922 sizeof(struct ip_vs_iter));
1925 static const struct file_operations ip_vs_info_fops = {
1926 .owner = THIS_MODULE,
1927 .open = ip_vs_info_open,
1929 .llseek = seq_lseek,
1930 .release = seq_release_private,
1935 struct ip_vs_stats ip_vs_stats = {
1936 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1939 #ifdef CONFIG_PROC_FS
1940 static int ip_vs_stats_show(struct seq_file *seq, void *v)
1943 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1945 " Total Incoming Outgoing Incoming Outgoing\n");
1947 " Conns Packets Packets Bytes Bytes\n");
1949 spin_lock_bh(&ip_vs_stats.lock);
1950 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1951 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1952 (unsigned long long) ip_vs_stats.ustats.inbytes,
1953 (unsigned long long) ip_vs_stats.ustats.outbytes);
1955 /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1957 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1958 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
1959 ip_vs_stats.ustats.cps,
1960 ip_vs_stats.ustats.inpps,
1961 ip_vs_stats.ustats.outpps,
1962 ip_vs_stats.ustats.inbps,
1963 ip_vs_stats.ustats.outbps);
1964 spin_unlock_bh(&ip_vs_stats.lock);
1969 static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1971 return single_open(file, ip_vs_stats_show, NULL);
1974 static const struct file_operations ip_vs_stats_fops = {
1975 .owner = THIS_MODULE,
1976 .open = ip_vs_stats_seq_open,
1978 .llseek = seq_lseek,
1979 .release = single_release,
1985 * Set timeout values for tcp tcpfin udp in the timeout_table.
1987 static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1989 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1994 #ifdef CONFIG_IP_VS_PROTO_TCP
1995 if (u->tcp_timeout) {
1996 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
1997 = u->tcp_timeout * HZ;
2000 if (u->tcp_fin_timeout) {
2001 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
2002 = u->tcp_fin_timeout * HZ;
2006 #ifdef CONFIG_IP_VS_PROTO_UDP
2007 if (u->udp_timeout) {
2008 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
2009 = u->udp_timeout * HZ;
2016 #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2017 #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2018 #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2019 sizeof(struct ip_vs_dest_user))
2020 #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2021 #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2022 #define MAX_ARG_LEN SVCDEST_ARG_LEN
2024 static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
2025 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2026 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2027 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2028 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2029 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2030 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2031 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2032 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2033 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2034 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2035 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2038 static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2039 struct ip_vs_service_user *usvc_compat)
2042 usvc->protocol = usvc_compat->protocol;
2043 usvc->addr.ip = usvc_compat->addr;
2044 usvc->port = usvc_compat->port;
2045 usvc->fwmark = usvc_compat->fwmark;
2047 /* Deep copy of sched_name is not needed here */
2048 usvc->sched_name = usvc_compat->sched_name;
2050 usvc->flags = usvc_compat->flags;
2051 usvc->timeout = usvc_compat->timeout;
2052 usvc->netmask = usvc_compat->netmask;
2055 static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2056 struct ip_vs_dest_user *udest_compat)
2058 udest->addr.ip = udest_compat->addr;
2059 udest->port = udest_compat->port;
2060 udest->conn_flags = udest_compat->conn_flags;
2061 udest->weight = udest_compat->weight;
2062 udest->u_threshold = udest_compat->u_threshold;
2063 udest->l_threshold = udest_compat->l_threshold;
2067 do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2070 unsigned char arg[MAX_ARG_LEN];
2071 struct ip_vs_service_user *usvc_compat;
2072 struct ip_vs_service_user_kern usvc;
2073 struct ip_vs_service *svc;
2074 struct ip_vs_dest_user *udest_compat;
2075 struct ip_vs_dest_user_kern udest;
2077 if (!capable(CAP_NET_ADMIN))
2080 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2082 if (len < 0 || len > MAX_ARG_LEN)
2084 if (len != set_arglen[SET_CMDID(cmd)]) {
2085 pr_err("set_ctl: len %u != %u\n",
2086 len, set_arglen[SET_CMDID(cmd)]);
2090 if (copy_from_user(arg, user, len) != 0)
2093 /* increase the module use count */
2094 ip_vs_use_count_inc();
2096 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
2101 if (cmd == IP_VS_SO_SET_FLUSH) {
2102 /* Flush the virtual service */
2103 ret = ip_vs_flush();
2105 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2106 /* Set timeout values for (tcp tcpfin udp) */
2107 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2109 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2110 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2111 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2113 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2114 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2115 ret = stop_sync_thread(dm->state);
2119 usvc_compat = (struct ip_vs_service_user *)arg;
2120 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2122 /* We only use the new structs internally, so copy userspace compat
2123 * structs to extended internal versions */
2124 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2125 ip_vs_copy_udest_compat(&udest, udest_compat);
2127 if (cmd == IP_VS_SO_SET_ZERO) {
2128 /* if no service address is set, zero counters in all */
2129 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2130 ret = ip_vs_zero_all();
2135 /* Check for valid protocol: TCP or UDP, even for fwmark!=0 */
2136 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP) {
2137 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2138 usvc.protocol, &usvc.addr.ip,
2139 ntohs(usvc.port), usvc.sched_name);
2144 /* Lookup the exact service by <protocol, addr, port> or fwmark */
2145 if (usvc.fwmark == 0)
2146 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
2147 &usvc.addr, usvc.port);
2149 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2151 if (cmd != IP_VS_SO_SET_ADD
2152 && (svc == NULL || svc->protocol != usvc.protocol)) {
2158 case IP_VS_SO_SET_ADD:
2162 ret = ip_vs_add_service(&usvc, &svc);
2164 case IP_VS_SO_SET_EDIT:
2165 ret = ip_vs_edit_service(svc, &usvc);
2167 case IP_VS_SO_SET_DEL:
2168 ret = ip_vs_del_service(svc);
2172 case IP_VS_SO_SET_ZERO:
2173 ret = ip_vs_zero_service(svc);
2175 case IP_VS_SO_SET_ADDDEST:
2176 ret = ip_vs_add_dest(svc, &udest);
2178 case IP_VS_SO_SET_EDITDEST:
2179 ret = ip_vs_edit_dest(svc, &udest);
2181 case IP_VS_SO_SET_DELDEST:
2182 ret = ip_vs_del_dest(svc, &udest);
2189 ip_vs_service_put(svc);
2192 mutex_unlock(&__ip_vs_mutex);
2194 /* decrease the module use count */
2195 ip_vs_use_count_dec();
2202 ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2204 spin_lock_bh(&src->lock);
2205 memcpy(dst, &src->ustats, sizeof(*dst));
2206 spin_unlock_bh(&src->lock);
2210 ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2212 dst->protocol = src->protocol;
2213 dst->addr = src->addr.ip;
2214 dst->port = src->port;
2215 dst->fwmark = src->fwmark;
2216 strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
2217 dst->flags = src->flags;
2218 dst->timeout = src->timeout / HZ;
2219 dst->netmask = src->netmask;
2220 dst->num_dests = src->num_dests;
2221 ip_vs_copy_stats(&dst->stats, &src->stats);
2225 __ip_vs_get_service_entries(const struct ip_vs_get_services *get,
2226 struct ip_vs_get_services __user *uptr)
2229 struct ip_vs_service *svc;
2230 struct ip_vs_service_entry entry;
2233 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2234 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2235 /* Only expose IPv4 entries to old interface */
2236 if (svc->af != AF_INET)
2239 if (count >= get->num_services)
2241 memset(&entry, 0, sizeof(entry));
2242 ip_vs_copy_service(&entry, svc);
2243 if (copy_to_user(&uptr->entrytable[count],
2244 &entry, sizeof(entry))) {
2252 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2253 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2254 /* Only expose IPv4 entries to old interface */
2255 if (svc->af != AF_INET)
2258 if (count >= get->num_services)
2260 memset(&entry, 0, sizeof(entry));
2261 ip_vs_copy_service(&entry, svc);
2262 if (copy_to_user(&uptr->entrytable[count],
2263 &entry, sizeof(entry))) {
2275 __ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
2276 struct ip_vs_get_dests __user *uptr)
2278 struct ip_vs_service *svc;
2279 union nf_inet_addr addr = { .ip = get->addr };
2283 svc = __ip_vs_svc_fwm_get(AF_INET, get->fwmark);
2285 svc = __ip_vs_service_get(AF_INET, get->protocol, &addr,
2290 struct ip_vs_dest *dest;
2291 struct ip_vs_dest_entry entry;
2293 list_for_each_entry(dest, &svc->destinations, n_list) {
2294 if (count >= get->num_dests)
2297 entry.addr = dest->addr.ip;
2298 entry.port = dest->port;
2299 entry.conn_flags = atomic_read(&dest->conn_flags);
2300 entry.weight = atomic_read(&dest->weight);
2301 entry.u_threshold = dest->u_threshold;
2302 entry.l_threshold = dest->l_threshold;
2303 entry.activeconns = atomic_read(&dest->activeconns);
2304 entry.inactconns = atomic_read(&dest->inactconns);
2305 entry.persistconns = atomic_read(&dest->persistconns);
2306 ip_vs_copy_stats(&entry.stats, &dest->stats);
2307 if (copy_to_user(&uptr->entrytable[count],
2308 &entry, sizeof(entry))) {
2314 ip_vs_service_put(svc);
2321 __ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
2323 #ifdef CONFIG_IP_VS_PROTO_TCP
2325 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2326 u->tcp_fin_timeout =
2327 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2329 #ifdef CONFIG_IP_VS_PROTO_UDP
2331 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2336 #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2337 #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2338 #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2339 #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2340 #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2341 #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2342 #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2344 static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
2345 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2346 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2347 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2348 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2349 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2350 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2351 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2355 do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2357 unsigned char arg[128];
2359 unsigned int copylen;
2361 if (!capable(CAP_NET_ADMIN))
2364 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2367 if (*len < get_arglen[GET_CMDID(cmd)]) {
2368 pr_err("get_ctl: len %u < %u\n",
2369 *len, get_arglen[GET_CMDID(cmd)]);
2373 copylen = get_arglen[GET_CMDID(cmd)];
2377 if (copy_from_user(arg, user, copylen) != 0)
2380 if (mutex_lock_interruptible(&__ip_vs_mutex))
2381 return -ERESTARTSYS;
2384 case IP_VS_SO_GET_VERSION:
2388 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2389 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
2390 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2394 *len = strlen(buf)+1;
2398 case IP_VS_SO_GET_INFO:
2400 struct ip_vs_getinfo info;
2401 info.version = IP_VS_VERSION_CODE;
2402 info.size = IP_VS_CONN_TAB_SIZE;
2403 info.num_services = ip_vs_num_services;
2404 if (copy_to_user(user, &info, sizeof(info)) != 0)
2409 case IP_VS_SO_GET_SERVICES:
2411 struct ip_vs_get_services *get;
2414 get = (struct ip_vs_get_services *)arg;
2415 size = sizeof(*get) +
2416 sizeof(struct ip_vs_service_entry) * get->num_services;
2418 pr_err("length: %u != %u\n", *len, size);
2422 ret = __ip_vs_get_service_entries(get, user);
2426 case IP_VS_SO_GET_SERVICE:
2428 struct ip_vs_service_entry *entry;
2429 struct ip_vs_service *svc;
2430 union nf_inet_addr addr;
2432 entry = (struct ip_vs_service_entry *)arg;
2433 addr.ip = entry->addr;
2435 svc = __ip_vs_svc_fwm_get(AF_INET, entry->fwmark);
2437 svc = __ip_vs_service_get(AF_INET, entry->protocol,
2438 &addr, entry->port);
2440 ip_vs_copy_service(entry, svc);
2441 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2443 ip_vs_service_put(svc);
2449 case IP_VS_SO_GET_DESTS:
2451 struct ip_vs_get_dests *get;
2454 get = (struct ip_vs_get_dests *)arg;
2455 size = sizeof(*get) +
2456 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2458 pr_err("length: %u != %u\n", *len, size);
2462 ret = __ip_vs_get_dest_entries(get, user);
2466 case IP_VS_SO_GET_TIMEOUT:
2468 struct ip_vs_timeout_user t;
2470 __ip_vs_get_timeouts(&t);
2471 if (copy_to_user(user, &t, sizeof(t)) != 0)
2476 case IP_VS_SO_GET_DAEMON:
2478 struct ip_vs_daemon_user d[2];
2480 memset(&d, 0, sizeof(d));
2481 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2482 d[0].state = IP_VS_STATE_MASTER;
2483 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
2484 d[0].syncid = ip_vs_master_syncid;
2486 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2487 d[1].state = IP_VS_STATE_BACKUP;
2488 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
2489 d[1].syncid = ip_vs_backup_syncid;
2491 if (copy_to_user(user, &d, sizeof(d)) != 0)
2501 mutex_unlock(&__ip_vs_mutex);
2506 static struct nf_sockopt_ops ip_vs_sockopts = {
2508 .set_optmin = IP_VS_BASE_CTL,
2509 .set_optmax = IP_VS_SO_SET_MAX+1,
2510 .set = do_ip_vs_set_ctl,
2511 .get_optmin = IP_VS_BASE_CTL,
2512 .get_optmax = IP_VS_SO_GET_MAX+1,
2513 .get = do_ip_vs_get_ctl,
2514 .owner = THIS_MODULE,
2518 * Generic Netlink interface
2521 /* IPVS genetlink family */
2522 static struct genl_family ip_vs_genl_family = {
2523 .id = GENL_ID_GENERATE,
2525 .name = IPVS_GENL_NAME,
2526 .version = IPVS_GENL_VERSION,
2527 .maxattr = IPVS_CMD_MAX,
2530 /* Policy used for first-level command attributes */
2531 static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2532 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2533 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2534 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2535 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2536 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2537 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2540 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2541 static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2542 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2543 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2544 .len = IP_VS_IFNAME_MAXLEN },
2545 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2548 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2549 static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2550 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2551 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2552 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2553 .len = sizeof(union nf_inet_addr) },
2554 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2555 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2556 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2557 .len = IP_VS_SCHEDNAME_MAXLEN },
2558 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2559 .len = sizeof(struct ip_vs_flags) },
2560 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2561 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2562 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2565 /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2566 static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2567 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2568 .len = sizeof(union nf_inet_addr) },
2569 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2570 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2571 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2572 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2573 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2574 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2575 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2576 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2577 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2580 static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2581 struct ip_vs_stats *stats)
2583 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2587 spin_lock_bh(&stats->lock);
2589 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2590 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2591 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2592 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2593 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2594 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2595 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2596 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2597 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2598 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
2600 spin_unlock_bh(&stats->lock);
2602 nla_nest_end(skb, nl_stats);
2607 spin_unlock_bh(&stats->lock);
2608 nla_nest_cancel(skb, nl_stats);
2612 static int ip_vs_genl_fill_service(struct sk_buff *skb,
2613 struct ip_vs_service *svc)
2615 struct nlattr *nl_service;
2616 struct ip_vs_flags flags = { .flags = svc->flags,
2619 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2623 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
2626 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2628 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2629 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2630 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2633 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2634 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2635 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2636 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2638 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2639 goto nla_put_failure;
2641 nla_nest_end(skb, nl_service);
2646 nla_nest_cancel(skb, nl_service);
2650 static int ip_vs_genl_dump_service(struct sk_buff *skb,
2651 struct ip_vs_service *svc,
2652 struct netlink_callback *cb)
2656 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2657 &ip_vs_genl_family, NLM_F_MULTI,
2658 IPVS_CMD_NEW_SERVICE);
2662 if (ip_vs_genl_fill_service(skb, svc) < 0)
2663 goto nla_put_failure;
2665 return genlmsg_end(skb, hdr);
2668 genlmsg_cancel(skb, hdr);
2672 static int ip_vs_genl_dump_services(struct sk_buff *skb,
2673 struct netlink_callback *cb)
2676 int start = cb->args[0];
2677 struct ip_vs_service *svc;
2679 mutex_lock(&__ip_vs_mutex);
2680 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2681 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2684 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2686 goto nla_put_failure;
2691 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2692 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2695 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2697 goto nla_put_failure;
2703 mutex_unlock(&__ip_vs_mutex);
2709 static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
2710 struct nlattr *nla, int full_entry)
2712 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2713 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2715 /* Parse mandatory identifying service fields first */
2717 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2720 nla_af = attrs[IPVS_SVC_ATTR_AF];
2721 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2722 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2723 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2724 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2726 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2729 memset(usvc, 0, sizeof(*usvc));
2731 usvc->af = nla_get_u16(nla_af);
2732 #ifdef CONFIG_IP_VS_IPV6
2733 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2735 if (usvc->af != AF_INET)
2737 return -EAFNOSUPPORT;
2740 usvc->protocol = IPPROTO_TCP;
2741 usvc->fwmark = nla_get_u32(nla_fwmark);
2743 usvc->protocol = nla_get_u16(nla_protocol);
2744 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2745 usvc->port = nla_get_u16(nla_port);
2749 /* If a full entry was requested, check for the additional fields */
2751 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2753 struct ip_vs_flags flags;
2754 struct ip_vs_service *svc;
2756 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2757 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2758 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2759 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2761 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2764 nla_memcpy(&flags, nla_flags, sizeof(flags));
2766 /* prefill flags from service if it already exists */
2768 svc = __ip_vs_svc_fwm_get(usvc->af, usvc->fwmark);
2770 svc = __ip_vs_service_get(usvc->af, usvc->protocol,
2771 &usvc->addr, usvc->port);
2773 usvc->flags = svc->flags;
2774 ip_vs_service_put(svc);
2778 /* set new flags from userland */
2779 usvc->flags = (usvc->flags & ~flags.mask) |
2780 (flags.flags & flags.mask);
2781 usvc->sched_name = nla_data(nla_sched);
2782 usvc->timeout = nla_get_u32(nla_timeout);
2783 usvc->netmask = nla_get_u32(nla_netmask);
2789 static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2791 struct ip_vs_service_user_kern usvc;
2794 ret = ip_vs_genl_parse_service(&usvc, nla, 0);
2796 return ERR_PTR(ret);
2799 return __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
2801 return __ip_vs_service_get(usvc.af, usvc.protocol,
2802 &usvc.addr, usvc.port);
2805 static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2807 struct nlattr *nl_dest;
2809 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2813 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2814 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2816 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2817 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2818 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2819 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2820 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2821 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2822 atomic_read(&dest->activeconns));
2823 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2824 atomic_read(&dest->inactconns));
2825 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2826 atomic_read(&dest->persistconns));
2828 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2829 goto nla_put_failure;
2831 nla_nest_end(skb, nl_dest);
2836 nla_nest_cancel(skb, nl_dest);
2840 static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2841 struct netlink_callback *cb)
2845 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2846 &ip_vs_genl_family, NLM_F_MULTI,
2851 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2852 goto nla_put_failure;
2854 return genlmsg_end(skb, hdr);
2857 genlmsg_cancel(skb, hdr);
2861 static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2862 struct netlink_callback *cb)
2865 int start = cb->args[0];
2866 struct ip_vs_service *svc;
2867 struct ip_vs_dest *dest;
2868 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2870 mutex_lock(&__ip_vs_mutex);
2872 /* Try to find the service for which to dump destinations */
2873 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2874 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2877 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2878 if (IS_ERR(svc) || svc == NULL)
2881 /* Dump the destinations */
2882 list_for_each_entry(dest, &svc->destinations, n_list) {
2885 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2887 goto nla_put_failure;
2893 ip_vs_service_put(svc);
2896 mutex_unlock(&__ip_vs_mutex);
2901 static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
2902 struct nlattr *nla, int full_entry)
2904 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2905 struct nlattr *nla_addr, *nla_port;
2907 /* Parse mandatory identifying destination fields first */
2909 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2912 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2913 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2915 if (!(nla_addr && nla_port))
2918 memset(udest, 0, sizeof(*udest));
2920 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2921 udest->port = nla_get_u16(nla_port);
2923 /* If a full entry was requested, check for the additional fields */
2925 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2928 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2929 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2930 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2931 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2933 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2936 udest->conn_flags = nla_get_u32(nla_fwd)
2937 & IP_VS_CONN_F_FWD_MASK;
2938 udest->weight = nla_get_u32(nla_weight);
2939 udest->u_threshold = nla_get_u32(nla_u_thresh);
2940 udest->l_threshold = nla_get_u32(nla_l_thresh);
2946 static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2947 const char *mcast_ifn, __be32 syncid)
2949 struct nlattr *nl_daemon;
2951 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2955 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2956 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2957 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2959 nla_nest_end(skb, nl_daemon);
2964 nla_nest_cancel(skb, nl_daemon);
2968 static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2969 const char *mcast_ifn, __be32 syncid,
2970 struct netlink_callback *cb)
2973 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2974 &ip_vs_genl_family, NLM_F_MULTI,
2975 IPVS_CMD_NEW_DAEMON);
2979 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2980 goto nla_put_failure;
2982 return genlmsg_end(skb, hdr);
2985 genlmsg_cancel(skb, hdr);
2989 static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2990 struct netlink_callback *cb)
2992 mutex_lock(&__ip_vs_mutex);
2993 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2994 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
2995 ip_vs_master_mcast_ifn,
2996 ip_vs_master_syncid, cb) < 0)
2997 goto nla_put_failure;
3002 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3003 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3004 ip_vs_backup_mcast_ifn,
3005 ip_vs_backup_syncid, cb) < 0)
3006 goto nla_put_failure;
3012 mutex_unlock(&__ip_vs_mutex);
3017 static int ip_vs_genl_new_daemon(struct nlattr **attrs)
3019 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3020 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3021 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3024 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3025 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3026 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3029 static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3031 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3034 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3037 static int ip_vs_genl_set_config(struct nlattr **attrs)
3039 struct ip_vs_timeout_user t;
3041 __ip_vs_get_timeouts(&t);
3043 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3044 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3046 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3048 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3050 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3051 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3053 return ip_vs_set_timeout(&t);
3056 static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3058 struct ip_vs_service *svc = NULL;
3059 struct ip_vs_service_user_kern usvc;
3060 struct ip_vs_dest_user_kern udest;
3062 int need_full_svc = 0, need_full_dest = 0;
3064 cmd = info->genlhdr->cmd;
3066 mutex_lock(&__ip_vs_mutex);
3068 if (cmd == IPVS_CMD_FLUSH) {
3069 ret = ip_vs_flush();
3071 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3072 ret = ip_vs_genl_set_config(info->attrs);
3074 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3075 cmd == IPVS_CMD_DEL_DAEMON) {
3077 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3079 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3080 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3081 info->attrs[IPVS_CMD_ATTR_DAEMON],
3082 ip_vs_daemon_policy)) {
3087 if (cmd == IPVS_CMD_NEW_DAEMON)
3088 ret = ip_vs_genl_new_daemon(daemon_attrs);
3090 ret = ip_vs_genl_del_daemon(daemon_attrs);
3092 } else if (cmd == IPVS_CMD_ZERO &&
3093 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3094 ret = ip_vs_zero_all();
3098 /* All following commands require a service argument, so check if we
3099 * received a valid one. We need a full service specification when
3100 * adding / editing a service. Only identifying members otherwise. */
3101 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3104 ret = ip_vs_genl_parse_service(&usvc,
3105 info->attrs[IPVS_CMD_ATTR_SERVICE],
3110 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3111 if (usvc.fwmark == 0)
3112 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
3113 &usvc.addr, usvc.port);
3115 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
3117 /* Unless we're adding a new service, the service must already exist */
3118 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3123 /* Destination commands require a valid destination argument. For
3124 * adding / editing a destination, we need a full destination
3126 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3127 cmd == IPVS_CMD_DEL_DEST) {
3128 if (cmd != IPVS_CMD_DEL_DEST)
3131 ret = ip_vs_genl_parse_dest(&udest,
3132 info->attrs[IPVS_CMD_ATTR_DEST],
3139 case IPVS_CMD_NEW_SERVICE:
3141 ret = ip_vs_add_service(&usvc, &svc);
3145 case IPVS_CMD_SET_SERVICE:
3146 ret = ip_vs_edit_service(svc, &usvc);
3148 case IPVS_CMD_DEL_SERVICE:
3149 ret = ip_vs_del_service(svc);
3151 case IPVS_CMD_NEW_DEST:
3152 ret = ip_vs_add_dest(svc, &udest);
3154 case IPVS_CMD_SET_DEST:
3155 ret = ip_vs_edit_dest(svc, &udest);
3157 case IPVS_CMD_DEL_DEST:
3158 ret = ip_vs_del_dest(svc, &udest);
3161 ret = ip_vs_zero_service(svc);
3169 ip_vs_service_put(svc);
3170 mutex_unlock(&__ip_vs_mutex);
3175 static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3177 struct sk_buff *msg;
3179 int ret, cmd, reply_cmd;
3181 cmd = info->genlhdr->cmd;
3183 if (cmd == IPVS_CMD_GET_SERVICE)
3184 reply_cmd = IPVS_CMD_NEW_SERVICE;
3185 else if (cmd == IPVS_CMD_GET_INFO)
3186 reply_cmd = IPVS_CMD_SET_INFO;
3187 else if (cmd == IPVS_CMD_GET_CONFIG)
3188 reply_cmd = IPVS_CMD_SET_CONFIG;
3190 pr_err("unknown Generic Netlink command\n");
3194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3198 mutex_lock(&__ip_vs_mutex);
3200 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3202 goto nla_put_failure;
3205 case IPVS_CMD_GET_SERVICE:
3207 struct ip_vs_service *svc;
3209 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3214 ret = ip_vs_genl_fill_service(msg, svc);
3215 ip_vs_service_put(svc);
3217 goto nla_put_failure;
3226 case IPVS_CMD_GET_CONFIG:
3228 struct ip_vs_timeout_user t;
3230 __ip_vs_get_timeouts(&t);
3231 #ifdef CONFIG_IP_VS_PROTO_TCP
3232 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3233 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3236 #ifdef CONFIG_IP_VS_PROTO_UDP
3237 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3243 case IPVS_CMD_GET_INFO:
3244 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3245 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3246 IP_VS_CONN_TAB_SIZE);
3250 genlmsg_end(msg, reply);
3251 ret = genlmsg_reply(msg, info);
3255 pr_err("not enough space in Netlink message\n");
3261 mutex_unlock(&__ip_vs_mutex);
3267 static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3269 .cmd = IPVS_CMD_NEW_SERVICE,
3270 .flags = GENL_ADMIN_PERM,
3271 .policy = ip_vs_cmd_policy,
3272 .doit = ip_vs_genl_set_cmd,
3275 .cmd = IPVS_CMD_SET_SERVICE,
3276 .flags = GENL_ADMIN_PERM,
3277 .policy = ip_vs_cmd_policy,
3278 .doit = ip_vs_genl_set_cmd,
3281 .cmd = IPVS_CMD_DEL_SERVICE,
3282 .flags = GENL_ADMIN_PERM,
3283 .policy = ip_vs_cmd_policy,
3284 .doit = ip_vs_genl_set_cmd,
3287 .cmd = IPVS_CMD_GET_SERVICE,
3288 .flags = GENL_ADMIN_PERM,
3289 .doit = ip_vs_genl_get_cmd,
3290 .dumpit = ip_vs_genl_dump_services,
3291 .policy = ip_vs_cmd_policy,
3294 .cmd = IPVS_CMD_NEW_DEST,
3295 .flags = GENL_ADMIN_PERM,
3296 .policy = ip_vs_cmd_policy,
3297 .doit = ip_vs_genl_set_cmd,
3300 .cmd = IPVS_CMD_SET_DEST,
3301 .flags = GENL_ADMIN_PERM,
3302 .policy = ip_vs_cmd_policy,
3303 .doit = ip_vs_genl_set_cmd,
3306 .cmd = IPVS_CMD_DEL_DEST,
3307 .flags = GENL_ADMIN_PERM,
3308 .policy = ip_vs_cmd_policy,
3309 .doit = ip_vs_genl_set_cmd,
3312 .cmd = IPVS_CMD_GET_DEST,
3313 .flags = GENL_ADMIN_PERM,
3314 .policy = ip_vs_cmd_policy,
3315 .dumpit = ip_vs_genl_dump_dests,
3318 .cmd = IPVS_CMD_NEW_DAEMON,
3319 .flags = GENL_ADMIN_PERM,
3320 .policy = ip_vs_cmd_policy,
3321 .doit = ip_vs_genl_set_cmd,
3324 .cmd = IPVS_CMD_DEL_DAEMON,
3325 .flags = GENL_ADMIN_PERM,
3326 .policy = ip_vs_cmd_policy,
3327 .doit = ip_vs_genl_set_cmd,
3330 .cmd = IPVS_CMD_GET_DAEMON,
3331 .flags = GENL_ADMIN_PERM,
3332 .dumpit = ip_vs_genl_dump_daemons,
3335 .cmd = IPVS_CMD_SET_CONFIG,
3336 .flags = GENL_ADMIN_PERM,
3337 .policy = ip_vs_cmd_policy,
3338 .doit = ip_vs_genl_set_cmd,
3341 .cmd = IPVS_CMD_GET_CONFIG,
3342 .flags = GENL_ADMIN_PERM,
3343 .doit = ip_vs_genl_get_cmd,
3346 .cmd = IPVS_CMD_GET_INFO,
3347 .flags = GENL_ADMIN_PERM,
3348 .doit = ip_vs_genl_get_cmd,
3351 .cmd = IPVS_CMD_ZERO,
3352 .flags = GENL_ADMIN_PERM,
3353 .policy = ip_vs_cmd_policy,
3354 .doit = ip_vs_genl_set_cmd,
3357 .cmd = IPVS_CMD_FLUSH,
3358 .flags = GENL_ADMIN_PERM,
3359 .doit = ip_vs_genl_set_cmd,
3363 static int __init ip_vs_genl_register(void)
3365 return genl_register_family_with_ops(&ip_vs_genl_family,
3366 ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops));
3369 static void ip_vs_genl_unregister(void)
3371 genl_unregister_family(&ip_vs_genl_family);
3374 /* End of Generic Netlink interface definitions */
3377 int __init ip_vs_control_init(void)
3384 ret = nf_register_sockopt(&ip_vs_sockopts);
3386 pr_err("cannot register sockopt.\n");
3390 ret = ip_vs_genl_register();
3392 pr_err("cannot register Generic Netlink interface.\n");
3393 nf_unregister_sockopt(&ip_vs_sockopts);
3397 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3398 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
3400 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
3402 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3403 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3404 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3405 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3407 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3408 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3411 ip_vs_new_estimator(&ip_vs_stats);
3413 /* Hook the defense timer */
3414 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3421 void ip_vs_control_cleanup(void)
3424 ip_vs_trash_cleanup();
3425 cancel_rearming_delayed_work(&defense_work);
3426 cancel_work_sync(&defense_work.work);
3427 ip_vs_kill_estimator(&ip_vs_stats);
3428 unregister_sysctl_table(sysctl_header);
3429 proc_net_remove(&init_net, "ip_vs_stats");
3430 proc_net_remove(&init_net, "ip_vs");
3431 ip_vs_genl_unregister();
3432 nf_unregister_sockopt(&ip_vs_sockopts);