Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 6 | * cluster of servers. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 9 | * Peter Kese <peter.kese@ijs.si> |
| 10 | * Julian Anastasov <ja@ssi.bg> |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * Changes: |
| 18 | * |
| 19 | */ |
| 20 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 21 | #define KMSG_COMPONENT "IPVS" |
| 22 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/fs.h> |
| 29 | #include <linux/sysctl.h> |
| 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/workqueue.h> |
| 32 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <linux/netfilter.h> |
| 37 | #include <linux/netfilter_ipv4.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 40 | #include <net/net_namespace.h> |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 41 | #include <linux/nsproxy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <net/ip.h> |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_IP_VS_IPV6 |
| 44 | #include <net/ipv6.h> |
| 45 | #include <net/ip6_route.h> |
| 46 | #endif |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 47 | #include <net/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <net/sock.h> |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 49 | #include <net/genetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include <asm/uaccess.h> |
| 52 | |
| 53 | #include <net/ip_vs.h> |
| 54 | |
| 55 | /* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 56 | static DEFINE_MUTEX(__ip_vs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* lock for service table */ |
| 59 | static DEFINE_RWLOCK(__ip_vs_svc_lock); |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* sysctl variables */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | #ifdef CONFIG_IP_VS_DEBUG |
| 64 | static int sysctl_ip_vs_debug_level = 0; |
| 65 | |
| 66 | int ip_vs_get_debug_level(void) |
| 67 | { |
| 68 | return sysctl_ip_vs_debug_level; |
| 69 | } |
| 70 | #endif |
| 71 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 72 | |
| 73 | /* Protos */ |
| 74 | static void __ip_vs_del_service(struct ip_vs_service *svc); |
| 75 | |
| 76 | |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 77 | #ifdef CONFIG_IP_VS_IPV6 |
| 78 | /* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */ |
Hans Schillstrom | 4a98480 | 2011-01-03 14:45:02 +0100 | [diff] [blame] | 79 | static int __ip_vs_addr_is_local_v6(struct net *net, |
| 80 | const struct in6_addr *addr) |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 81 | { |
| 82 | struct rt6_info *rt; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 83 | struct flowi6 fl6 = { |
| 84 | .daddr = *addr, |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 87 | rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 88 | if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK)) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 89 | return 1; |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | #endif |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 94 | |
| 95 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 97 | * update_defense_level is called from keventd and from sysctl, |
| 98 | * so it needs to protect itself from softirqs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 100 | static void update_defense_level(struct netns_ipvs *ipvs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | struct sysinfo i; |
| 103 | static int old_secure_tcp = 0; |
| 104 | int availmem; |
| 105 | int nomem; |
| 106 | int to_change = -1; |
| 107 | |
| 108 | /* we only count free and buffered memory (in pages) */ |
| 109 | si_meminfo(&i); |
| 110 | availmem = i.freeram + i.bufferram; |
| 111 | /* however in linux 2.5 the i.bufferram is total page cache size, |
| 112 | we need adjust it */ |
| 113 | /* si_swapinfo(&i); */ |
| 114 | /* availmem = availmem - (i.totalswap - i.freeswap); */ |
| 115 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 116 | nomem = (availmem < ipvs->sysctl_amemthresh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 118 | local_bh_disable(); |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* drop_entry */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 121 | spin_lock(&ipvs->dropentry_lock); |
| 122 | switch (ipvs->sysctl_drop_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | case 0: |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 124 | atomic_set(&ipvs->dropentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | break; |
| 126 | case 1: |
| 127 | if (nomem) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 128 | atomic_set(&ipvs->dropentry, 1); |
| 129 | ipvs->sysctl_drop_entry = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } else { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 131 | atomic_set(&ipvs->dropentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | case 2: |
| 135 | if (nomem) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 136 | atomic_set(&ipvs->dropentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } else { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 138 | atomic_set(&ipvs->dropentry, 0); |
| 139 | ipvs->sysctl_drop_entry = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | }; |
| 141 | break; |
| 142 | case 3: |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 143 | atomic_set(&ipvs->dropentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 146 | spin_unlock(&ipvs->dropentry_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* drop_packet */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 149 | spin_lock(&ipvs->droppacket_lock); |
| 150 | switch (ipvs->sysctl_drop_packet) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | case 0: |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 152 | ipvs->drop_rate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | break; |
| 154 | case 1: |
| 155 | if (nomem) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 156 | ipvs->drop_rate = ipvs->drop_counter |
| 157 | = ipvs->sysctl_amemthresh / |
| 158 | (ipvs->sysctl_amemthresh-availmem); |
| 159 | ipvs->sysctl_drop_packet = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } else { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 161 | ipvs->drop_rate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | case 2: |
| 165 | if (nomem) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 166 | ipvs->drop_rate = ipvs->drop_counter |
| 167 | = ipvs->sysctl_amemthresh / |
| 168 | (ipvs->sysctl_amemthresh-availmem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } else { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 170 | ipvs->drop_rate = 0; |
| 171 | ipvs->sysctl_drop_packet = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | break; |
| 174 | case 3: |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 175 | ipvs->drop_rate = ipvs->sysctl_am_droprate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | break; |
| 177 | } |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 178 | spin_unlock(&ipvs->droppacket_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | /* secure_tcp */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 181 | spin_lock(&ipvs->securetcp_lock); |
| 182 | switch (ipvs->sysctl_secure_tcp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | case 0: |
| 184 | if (old_secure_tcp >= 2) |
| 185 | to_change = 0; |
| 186 | break; |
| 187 | case 1: |
| 188 | if (nomem) { |
| 189 | if (old_secure_tcp < 2) |
| 190 | to_change = 1; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 191 | ipvs->sysctl_secure_tcp = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } else { |
| 193 | if (old_secure_tcp >= 2) |
| 194 | to_change = 0; |
| 195 | } |
| 196 | break; |
| 197 | case 2: |
| 198 | if (nomem) { |
| 199 | if (old_secure_tcp < 2) |
| 200 | to_change = 1; |
| 201 | } else { |
| 202 | if (old_secure_tcp >= 2) |
| 203 | to_change = 0; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 204 | ipvs->sysctl_secure_tcp = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | break; |
| 207 | case 3: |
| 208 | if (old_secure_tcp < 2) |
| 209 | to_change = 1; |
| 210 | break; |
| 211 | } |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 212 | old_secure_tcp = ipvs->sysctl_secure_tcp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (to_change >= 0) |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 214 | ip_vs_protocol_timeout_change(ipvs, |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 215 | ipvs->sysctl_secure_tcp > 1); |
| 216 | spin_unlock(&ipvs->securetcp_lock); |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 217 | |
| 218 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * Timer for checking the defense |
| 224 | */ |
| 225 | #define DEFENSE_TIMER_PERIOD 1*HZ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 227 | static void defense_work_handler(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Hans Schillstrom | f6340ee | 2011-01-03 14:44:59 +0100 | [diff] [blame] | 229 | struct netns_ipvs *ipvs = |
| 230 | container_of(work, struct netns_ipvs, defense_work.work); |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 231 | |
| 232 | update_defense_level(ipvs); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 233 | if (atomic_read(&ipvs->dropentry)) |
Hans Schillstrom | f6340ee | 2011-01-03 14:44:59 +0100 | [diff] [blame] | 234 | ip_vs_random_dropentry(ipvs->net); |
| 235 | schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 237 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | int |
| 240 | ip_vs_use_count_inc(void) |
| 241 | { |
| 242 | return try_module_get(THIS_MODULE); |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | ip_vs_use_count_dec(void) |
| 247 | { |
| 248 | module_put(THIS_MODULE); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* |
| 253 | * Hash table: for virtual service lookups |
| 254 | */ |
| 255 | #define IP_VS_SVC_TAB_BITS 8 |
| 256 | #define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS) |
| 257 | #define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1) |
| 258 | |
| 259 | /* the service table hashed by <protocol, addr, port> */ |
| 260 | static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE]; |
| 261 | /* the service table hashed by fwmark */ |
| 262 | static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE]; |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Returns hash value for virtual service |
| 267 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 268 | static inline unsigned |
| 269 | ip_vs_svc_hashkey(struct net *net, int af, unsigned proto, |
| 270 | const union nf_inet_addr *addr, __be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | register unsigned porth = ntohs(port); |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 273 | __be32 addr_fold = addr->ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 275 | #ifdef CONFIG_IP_VS_IPV6 |
| 276 | if (af == AF_INET6) |
| 277 | addr_fold = addr->ip6[0]^addr->ip6[1]^ |
| 278 | addr->ip6[2]^addr->ip6[3]; |
| 279 | #endif |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 280 | addr_fold ^= ((size_t)net>>8); |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 281 | |
| 282 | return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | & IP_VS_SVC_TAB_MASK; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Returns hash value of fwmark for virtual service lookup |
| 288 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 289 | static inline unsigned ip_vs_svc_fwm_hashkey(struct net *net, __u32 fwmark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 291 | return (((size_t)net>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 295 | * Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * or in the ip_vs_svc_fwm_table by fwmark. |
| 297 | * Should be called with locked tables. |
| 298 | */ |
| 299 | static int ip_vs_svc_hash(struct ip_vs_service *svc) |
| 300 | { |
| 301 | unsigned hash; |
| 302 | |
| 303 | if (svc->flags & IP_VS_SVC_F_HASHED) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 304 | pr_err("%s(): request for already hashed, called from %pF\n", |
| 305 | __func__, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | if (svc->fwmark == 0) { |
| 310 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 311 | * Hash it by <netns,protocol,addr,port> in ip_vs_svc_table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 313 | hash = ip_vs_svc_hashkey(svc->net, svc->af, svc->protocol, |
| 314 | &svc->addr, svc->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | list_add(&svc->s_list, &ip_vs_svc_table[hash]); |
| 316 | } else { |
| 317 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 318 | * Hash it by fwmark in svc_fwm_table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 320 | hash = ip_vs_svc_fwm_hashkey(svc->net, svc->fwmark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]); |
| 322 | } |
| 323 | |
| 324 | svc->flags |= IP_VS_SVC_F_HASHED; |
| 325 | /* increase its refcnt because it is referenced by the svc table */ |
| 326 | atomic_inc(&svc->refcnt); |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 332 | * Unhashes a service from svc_table / svc_fwm_table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * Should be called with locked tables. |
| 334 | */ |
| 335 | static int ip_vs_svc_unhash(struct ip_vs_service *svc) |
| 336 | { |
| 337 | if (!(svc->flags & IP_VS_SVC_F_HASHED)) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 338 | pr_err("%s(): request for unhash flagged, called from %pF\n", |
| 339 | __func__, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | if (svc->fwmark == 0) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 344 | /* Remove it from the svc_table table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | list_del(&svc->s_list); |
| 346 | } else { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 347 | /* Remove it from the svc_fwm_table table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | list_del(&svc->f_list); |
| 349 | } |
| 350 | |
| 351 | svc->flags &= ~IP_VS_SVC_F_HASHED; |
| 352 | atomic_dec(&svc->refcnt); |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 358 | * Get service by {netns, proto,addr,port} in the service table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | */ |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 360 | static inline struct ip_vs_service * |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 361 | __ip_vs_service_find(struct net *net, int af, __u16 protocol, |
| 362 | const union nf_inet_addr *vaddr, __be16 vport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | unsigned hash; |
| 365 | struct ip_vs_service *svc; |
| 366 | |
| 367 | /* Check for "full" addressed entries */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 368 | hash = ip_vs_svc_hashkey(net, af, protocol, vaddr, vport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){ |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 371 | if ((svc->af == af) |
| 372 | && ip_vs_addr_equal(af, &svc->addr, vaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | && (svc->port == vport) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 374 | && (svc->protocol == protocol) |
| 375 | && net_eq(svc->net, net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* HIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return svc; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * Get service by {fwmark} in the service table. |
| 387 | */ |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 388 | static inline struct ip_vs_service * |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 389 | __ip_vs_svc_fwm_find(struct net *net, int af, __u32 fwmark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | unsigned hash; |
| 392 | struct ip_vs_service *svc; |
| 393 | |
| 394 | /* Check for fwmark addressed entries */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 395 | hash = ip_vs_svc_fwm_hashkey(net, fwmark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 398 | if (svc->fwmark == fwmark && svc->af == af |
| 399 | && net_eq(svc->net, net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* HIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return svc; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | struct ip_vs_service * |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 409 | ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol, |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 410 | const union nf_inet_addr *vaddr, __be16 vport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | struct ip_vs_service *svc; |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 413 | struct netns_ipvs *ipvs = net_ipvs(net); |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | read_lock(&__ip_vs_svc_lock); |
| 416 | |
| 417 | /* |
| 418 | * Check the table hashed by fwmark first |
| 419 | */ |
Julian Anastasov | 097fc76 | 2011-03-04 12:26:17 +0200 | [diff] [blame] | 420 | if (fwmark) { |
| 421 | svc = __ip_vs_svc_fwm_find(net, af, fwmark); |
| 422 | if (svc) |
| 423 | goto out; |
| 424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * Check the table hashed by <protocol,addr,port> |
| 428 | * for "full" addressed entries |
| 429 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 430 | svc = __ip_vs_service_find(net, af, protocol, vaddr, vport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | if (svc == NULL |
| 433 | && protocol == IPPROTO_TCP |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 434 | && atomic_read(&ipvs->ftpsvc_counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) { |
| 436 | /* |
| 437 | * Check if ftp service entry exists, the packet |
| 438 | * might belong to FTP data connections. |
| 439 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 440 | svc = __ip_vs_service_find(net, af, protocol, vaddr, FTPPORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (svc == NULL |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 444 | && atomic_read(&ipvs->nullsvc_counter)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* |
| 446 | * Check if the catch-all port (port zero) exists |
| 447 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 448 | svc = __ip_vs_service_find(net, af, protocol, vaddr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | out: |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 452 | if (svc) |
| 453 | atomic_inc(&svc->usecnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | read_unlock(&__ip_vs_svc_lock); |
| 455 | |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 456 | IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n", |
| 457 | fwmark, ip_vs_proto_name(protocol), |
| 458 | IP_VS_DBG_ADDR(af, vaddr), ntohs(vport), |
| 459 | svc ? "hit" : "not hit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | return svc; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | static inline void |
| 466 | __ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc) |
| 467 | { |
| 468 | atomic_inc(&svc->refcnt); |
| 469 | dest->svc = svc; |
| 470 | } |
| 471 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 472 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | __ip_vs_unbind_svc(struct ip_vs_dest *dest) |
| 474 | { |
| 475 | struct ip_vs_service *svc = dest->svc; |
| 476 | |
| 477 | dest->svc = NULL; |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 478 | if (atomic_dec_and_test(&svc->refcnt)) { |
| 479 | IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n", |
| 480 | svc->fwmark, |
| 481 | IP_VS_DBG_ADDR(svc->af, &svc->addr), |
| 482 | ntohs(svc->port), atomic_read(&svc->usecnt)); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 483 | free_percpu(svc->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | kfree(svc); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | /* |
| 490 | * Returns hash value for real service |
| 491 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 492 | static inline unsigned ip_vs_rs_hashkey(int af, |
| 493 | const union nf_inet_addr *addr, |
| 494 | __be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | register unsigned porth = ntohs(port); |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 497 | __be32 addr_fold = addr->ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 499 | #ifdef CONFIG_IP_VS_IPV6 |
| 500 | if (af == AF_INET6) |
| 501 | addr_fold = addr->ip6[0]^addr->ip6[1]^ |
| 502 | addr->ip6[2]^addr->ip6[3]; |
| 503 | #endif |
| 504 | |
| 505 | return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | & IP_VS_RTAB_MASK; |
| 507 | } |
| 508 | |
| 509 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 510 | * Hashes ip_vs_dest in rs_table by <proto,addr,port>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * should be called with locked tables. |
| 512 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 513 | static int ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
| 515 | unsigned hash; |
| 516 | |
| 517 | if (!list_empty(&dest->d_list)) { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Hash by proto,addr,port, |
| 523 | * which are the parameters of the real service. |
| 524 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 525 | hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port); |
| 526 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 527 | list_add(&dest->d_list, &ipvs->rs_table[hash]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 533 | * UNhashes ip_vs_dest from rs_table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | * should be called with locked tables. |
| 535 | */ |
| 536 | static int ip_vs_rs_unhash(struct ip_vs_dest *dest) |
| 537 | { |
| 538 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 539 | * Remove it from the rs_table table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | */ |
| 541 | if (!list_empty(&dest->d_list)) { |
| 542 | list_del(&dest->d_list); |
| 543 | INIT_LIST_HEAD(&dest->d_list); |
| 544 | } |
| 545 | |
| 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Lookup real service by <proto,addr,port> in the real service table. |
| 551 | */ |
| 552 | struct ip_vs_dest * |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 553 | ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol, |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 554 | const union nf_inet_addr *daddr, |
| 555 | __be16 dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 557 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | unsigned hash; |
| 559 | struct ip_vs_dest *dest; |
| 560 | |
| 561 | /* |
| 562 | * Check for "full" addressed entries |
| 563 | * Return the first found entry |
| 564 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 565 | hash = ip_vs_rs_hashkey(af, daddr, dport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 567 | read_lock(&ipvs->rs_lock); |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 568 | list_for_each_entry(dest, &ipvs->rs_table[hash], d_list) { |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 569 | if ((dest->af == af) |
| 570 | && ip_vs_addr_equal(af, &dest->addr, daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | && (dest->port == dport) |
| 572 | && ((dest->protocol == protocol) || |
| 573 | dest->vfwmark)) { |
| 574 | /* HIT */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 575 | read_unlock(&ipvs->rs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return dest; |
| 577 | } |
| 578 | } |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 579 | read_unlock(&ipvs->rs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Lookup destination by {addr,port} in the given service |
| 586 | */ |
| 587 | static struct ip_vs_dest * |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 588 | ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr, |
| 589 | __be16 dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
| 591 | struct ip_vs_dest *dest; |
| 592 | |
| 593 | /* |
| 594 | * Find the destination for the given service |
| 595 | */ |
| 596 | list_for_each_entry(dest, &svc->destinations, n_list) { |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 597 | if ((dest->af == svc->af) |
| 598 | && ip_vs_addr_equal(svc->af, &dest->addr, daddr) |
| 599 | && (dest->port == dport)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | /* HIT */ |
| 601 | return dest; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return NULL; |
| 606 | } |
| 607 | |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 608 | /* |
| 609 | * Find destination by {daddr,dport,vaddr,protocol} |
| 610 | * Cretaed to be used in ip_vs_process_message() in |
| 611 | * the backup synchronization daemon. It finds the |
| 612 | * destination to be bound to the received connection |
| 613 | * on the backup. |
| 614 | * |
| 615 | * ip_vs_lookup_real_service() looked promissing, but |
| 616 | * seems not working as expected. |
| 617 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 618 | struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af, |
| 619 | const union nf_inet_addr *daddr, |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 620 | __be16 dport, |
| 621 | const union nf_inet_addr *vaddr, |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 622 | __be16 vport, __u16 protocol, __u32 fwmark) |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 623 | { |
| 624 | struct ip_vs_dest *dest; |
| 625 | struct ip_vs_service *svc; |
| 626 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 627 | svc = ip_vs_service_get(net, af, fwmark, protocol, vaddr, vport); |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 628 | if (!svc) |
| 629 | return NULL; |
| 630 | dest = ip_vs_lookup_dest(svc, daddr, dport); |
| 631 | if (dest) |
| 632 | atomic_inc(&dest->refcnt); |
| 633 | ip_vs_service_put(svc); |
| 634 | return dest; |
| 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * Lookup dest by {svc,addr,port} in the destination trash. |
| 639 | * The destination trash is used to hold the destinations that are removed |
| 640 | * from the service table but are still referenced by some conn entries. |
| 641 | * The reason to add the destination trash is when the dest is temporary |
| 642 | * down (either by administrator or by monitor program), the dest can be |
| 643 | * picked back from the trash, the remaining connections to the dest can |
| 644 | * continue, and the counting information of the dest is also useful for |
| 645 | * scheduling. |
| 646 | */ |
| 647 | static struct ip_vs_dest * |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 648 | ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr, |
| 649 | __be16 dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
| 651 | struct ip_vs_dest *dest, *nxt; |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 652 | struct netns_ipvs *ipvs = net_ipvs(svc->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * Find the destination in trash |
| 656 | */ |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 657 | list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, n_list) { |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 658 | IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, " |
| 659 | "dest->refcnt=%d\n", |
| 660 | dest->vfwmark, |
| 661 | IP_VS_DBG_ADDR(svc->af, &dest->addr), |
| 662 | ntohs(dest->port), |
| 663 | atomic_read(&dest->refcnt)); |
| 664 | if (dest->af == svc->af && |
| 665 | ip_vs_addr_equal(svc->af, &dest->addr, daddr) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | dest->port == dport && |
| 667 | dest->vfwmark == svc->fwmark && |
| 668 | dest->protocol == svc->protocol && |
| 669 | (svc->fwmark || |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 670 | (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | dest->vport == svc->port))) { |
| 672 | /* HIT */ |
| 673 | return dest; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Try to purge the destination from trash if not referenced |
| 678 | */ |
| 679 | if (atomic_read(&dest->refcnt) == 1) { |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 680 | IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u " |
| 681 | "from trash\n", |
| 682 | dest->vfwmark, |
| 683 | IP_VS_DBG_ADDR(svc->af, &dest->addr), |
| 684 | ntohs(dest->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | list_del(&dest->n_list); |
| 686 | ip_vs_dst_reset(dest); |
| 687 | __ip_vs_unbind_svc(dest); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 688 | free_percpu(dest->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | kfree(dest); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /* |
| 698 | * Clean up all the destinations in the trash |
| 699 | * Called by the ip_vs_control_cleanup() |
| 700 | * |
| 701 | * When the ip_vs_control_clearup is activated by ipvs module exit, |
| 702 | * the service tables must have been flushed and all the connections |
| 703 | * are expired, and the refcnt of each destination in the trash must |
| 704 | * be 1, so we simply release them here. |
| 705 | */ |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 706 | static void ip_vs_trash_cleanup(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
| 708 | struct ip_vs_dest *dest, *nxt; |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 709 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 711 | list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, n_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | list_del(&dest->n_list); |
| 713 | ip_vs_dst_reset(dest); |
| 714 | __ip_vs_unbind_svc(dest); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 715 | free_percpu(dest->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | kfree(dest); |
| 717 | } |
| 718 | } |
| 719 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 720 | static void |
| 721 | ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src) |
| 722 | { |
| 723 | #define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 724 | |
| 725 | spin_lock_bh(&src->lock); |
| 726 | |
| 727 | IP_VS_SHOW_STATS_COUNTER(conns); |
| 728 | IP_VS_SHOW_STATS_COUNTER(inpkts); |
| 729 | IP_VS_SHOW_STATS_COUNTER(outpkts); |
| 730 | IP_VS_SHOW_STATS_COUNTER(inbytes); |
| 731 | IP_VS_SHOW_STATS_COUNTER(outbytes); |
| 732 | |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 733 | ip_vs_read_estimator(dst, src); |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 734 | |
| 735 | spin_unlock_bh(&src->lock); |
| 736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | static void |
| 739 | ip_vs_zero_stats(struct ip_vs_stats *stats) |
| 740 | { |
| 741 | spin_lock_bh(&stats->lock); |
Simon Horman | e93615d | 2008-08-11 17:19:14 +1000 | [diff] [blame] | 742 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 743 | /* get current counters as zero point, rates are zeroed */ |
| 744 | |
| 745 | #define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 746 | |
| 747 | IP_VS_ZERO_STATS_COUNTER(conns); |
| 748 | IP_VS_ZERO_STATS_COUNTER(inpkts); |
| 749 | IP_VS_ZERO_STATS_COUNTER(outpkts); |
| 750 | IP_VS_ZERO_STATS_COUNTER(inbytes); |
| 751 | IP_VS_ZERO_STATS_COUNTER(outbytes); |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | ip_vs_zero_estimator(stats); |
Simon Horman | e93615d | 2008-08-11 17:19:14 +1000 | [diff] [blame] | 754 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 755 | spin_unlock_bh(&stats->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Update a destination in the given service |
| 760 | */ |
| 761 | static void |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 762 | __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest, |
| 763 | struct ip_vs_dest_user_kern *udest, int add) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 765 | struct netns_ipvs *ipvs = net_ipvs(svc->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | int conn_flags; |
| 767 | |
| 768 | /* set the weight and the flags */ |
| 769 | atomic_set(&dest->weight, udest->weight); |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 770 | conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK; |
| 771 | conn_flags |= IP_VS_CONN_F_INACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */ |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 774 | if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | conn_flags |= IP_VS_CONN_F_NOOUTPUT; |
| 776 | } else { |
| 777 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 778 | * Put the real service in rs_table if not present. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | * For now only for NAT! |
| 780 | */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 781 | write_lock_bh(&ipvs->rs_lock); |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 782 | ip_vs_rs_hash(ipvs, dest); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 783 | write_unlock_bh(&ipvs->rs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | atomic_set(&dest->conn_flags, conn_flags); |
| 786 | |
| 787 | /* bind the service */ |
| 788 | if (!dest->svc) { |
| 789 | __ip_vs_bind_svc(dest, svc); |
| 790 | } else { |
| 791 | if (dest->svc != svc) { |
| 792 | __ip_vs_unbind_svc(dest); |
| 793 | ip_vs_zero_stats(&dest->stats); |
| 794 | __ip_vs_bind_svc(dest, svc); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* set the dest status flags */ |
| 799 | dest->flags |= IP_VS_DEST_F_AVAILABLE; |
| 800 | |
| 801 | if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold) |
| 802 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 803 | dest->u_threshold = udest->u_threshold; |
| 804 | dest->l_threshold = udest->l_threshold; |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 805 | |
Julian Anastasov | ff75f40 | 2011-02-22 10:40:25 +0200 | [diff] [blame] | 806 | spin_lock_bh(&dest->dst_lock); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 807 | ip_vs_dst_reset(dest); |
Julian Anastasov | ff75f40 | 2011-02-22 10:40:25 +0200 | [diff] [blame] | 808 | spin_unlock_bh(&dest->dst_lock); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 809 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 810 | if (add) |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 811 | ip_vs_start_estimator(svc->net, &dest->stats); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 812 | |
| 813 | write_lock_bh(&__ip_vs_svc_lock); |
| 814 | |
| 815 | /* Wait until all other svc users go away */ |
| 816 | IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0); |
| 817 | |
| 818 | if (add) { |
| 819 | list_add(&dest->n_list, &svc->destinations); |
| 820 | svc->num_dests++; |
| 821 | } |
| 822 | |
| 823 | /* call the update_service, because server weight may be changed */ |
| 824 | if (svc->scheduler->update_service) |
| 825 | svc->scheduler->update_service(svc); |
| 826 | |
| 827 | write_unlock_bh(&__ip_vs_svc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | |
| 831 | /* |
| 832 | * Create a destination for the given service |
| 833 | */ |
| 834 | static int |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 835 | ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | struct ip_vs_dest **dest_p) |
| 837 | { |
| 838 | struct ip_vs_dest *dest; |
| 839 | unsigned atype; |
| 840 | |
| 841 | EnterFunction(2); |
| 842 | |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 843 | #ifdef CONFIG_IP_VS_IPV6 |
| 844 | if (svc->af == AF_INET6) { |
| 845 | atype = ipv6_addr_type(&udest->addr.in6); |
Sven Wegener | 3bfb92f | 2008-09-05 16:53:49 +0200 | [diff] [blame] | 846 | if ((!(atype & IPV6_ADDR_UNICAST) || |
| 847 | atype & IPV6_ADDR_LINKLOCAL) && |
Hans Schillstrom | 4a98480 | 2011-01-03 14:45:02 +0100 | [diff] [blame] | 848 | !__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6)) |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 849 | return -EINVAL; |
| 850 | } else |
| 851 | #endif |
| 852 | { |
Hans Schillstrom | 4a98480 | 2011-01-03 14:45:02 +0100 | [diff] [blame] | 853 | atype = inet_addr_type(svc->net, udest->addr.ip); |
Vince Busam | 09571c7 | 2008-09-02 15:55:52 +0200 | [diff] [blame] | 854 | if (atype != RTN_LOCAL && atype != RTN_UNICAST) |
| 855 | return -EINVAL; |
| 856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Simon Horman | dee06e4 | 2010-08-26 02:54:31 +0000 | [diff] [blame] | 858 | dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (dest == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 860 | pr_err("%s(): no memory.\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return -ENOMEM; |
| 862 | } |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 863 | dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats); |
| 864 | if (!dest->stats.cpustats) { |
| 865 | pr_err("%s() alloc_percpu failed\n", __func__); |
| 866 | goto err_alloc; |
| 867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 869 | dest->af = svc->af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | dest->protocol = svc->protocol; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 871 | dest->vaddr = svc->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | dest->vport = svc->port; |
| 873 | dest->vfwmark = svc->fwmark; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 874 | ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | dest->port = udest->port; |
| 876 | |
| 877 | atomic_set(&dest->activeconns, 0); |
| 878 | atomic_set(&dest->inactconns, 0); |
| 879 | atomic_set(&dest->persistconns, 0); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 880 | atomic_set(&dest->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | INIT_LIST_HEAD(&dest->d_list); |
| 883 | spin_lock_init(&dest->dst_lock); |
| 884 | spin_lock_init(&dest->stats.lock); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 885 | __ip_vs_update_dest(svc, dest, udest, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | *dest_p = dest; |
| 888 | |
| 889 | LeaveFunction(2); |
| 890 | return 0; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 891 | |
| 892 | err_alloc: |
| 893 | kfree(dest); |
| 894 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
| 898 | /* |
| 899 | * Add a destination into an existing service |
| 900 | */ |
| 901 | static int |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 902 | ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
| 904 | struct ip_vs_dest *dest; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 905 | union nf_inet_addr daddr; |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 906 | __be16 dport = udest->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | int ret; |
| 908 | |
| 909 | EnterFunction(2); |
| 910 | |
| 911 | if (udest->weight < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 912 | pr_err("%s(): server weight less than zero\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | return -ERANGE; |
| 914 | } |
| 915 | |
| 916 | if (udest->l_threshold > udest->u_threshold) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 917 | pr_err("%s(): lower threshold is higher than upper threshold\n", |
| 918 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return -ERANGE; |
| 920 | } |
| 921 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 922 | ip_vs_addr_copy(svc->af, &daddr, &udest->addr); |
| 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | /* |
| 925 | * Check if the dest already exists in the list |
| 926 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 927 | dest = ip_vs_lookup_dest(svc, &daddr, dport); |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | if (dest != NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 930 | IP_VS_DBG(1, "%s(): dest already exists\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return -EEXIST; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Check if the dest already exists in the trash and |
| 936 | * is from the same service |
| 937 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 938 | dest = ip_vs_trash_get_dest(svc, &daddr, dport); |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (dest != NULL) { |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 941 | IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, " |
| 942 | "dest->refcnt=%d, service %u/%s:%u\n", |
| 943 | IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport), |
| 944 | atomic_read(&dest->refcnt), |
| 945 | dest->vfwmark, |
| 946 | IP_VS_DBG_ADDR(svc->af, &dest->vaddr), |
| 947 | ntohs(dest->vport)); |
| 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Get the destination from the trash |
| 951 | */ |
| 952 | list_del(&dest->n_list); |
| 953 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 954 | __ip_vs_update_dest(svc, dest, udest, 1); |
| 955 | ret = 0; |
| 956 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | /* |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 958 | * Allocate and initialize the dest structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 960 | ret = ip_vs_new_dest(svc, udest, &dest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | LeaveFunction(2); |
| 963 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 964 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | |
| 968 | /* |
| 969 | * Edit a destination in the given service |
| 970 | */ |
| 971 | static int |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 972 | ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
| 974 | struct ip_vs_dest *dest; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 975 | union nf_inet_addr daddr; |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 976 | __be16 dport = udest->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | EnterFunction(2); |
| 979 | |
| 980 | if (udest->weight < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 981 | pr_err("%s(): server weight less than zero\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | return -ERANGE; |
| 983 | } |
| 984 | |
| 985 | if (udest->l_threshold > udest->u_threshold) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 986 | pr_err("%s(): lower threshold is higher than upper threshold\n", |
| 987 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | return -ERANGE; |
| 989 | } |
| 990 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 991 | ip_vs_addr_copy(svc->af, &daddr, &udest->addr); |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | /* |
| 994 | * Lookup the destination list |
| 995 | */ |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 996 | dest = ip_vs_lookup_dest(svc, &daddr, dport); |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | if (dest == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 999 | IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return -ENOENT; |
| 1001 | } |
| 1002 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1003 | __ip_vs_update_dest(svc, dest, udest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | LeaveFunction(2); |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | /* |
| 1011 | * Delete a destination (must be already unlinked from the service) |
| 1012 | */ |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 1013 | static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1015 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 1016 | |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 1017 | ip_vs_stop_estimator(net, &dest->stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | /* |
| 1020 | * Remove it from the d-linked list with the real services. |
| 1021 | */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1022 | write_lock_bh(&ipvs->rs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | ip_vs_rs_unhash(dest); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1024 | write_unlock_bh(&ipvs->rs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | * Decrease the refcnt of the dest, and free the dest |
| 1028 | * if nobody refers to it (refcnt=0). Otherwise, throw |
| 1029 | * the destination into the trash. |
| 1030 | */ |
| 1031 | if (atomic_dec_and_test(&dest->refcnt)) { |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1032 | IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u\n", |
| 1033 | dest->vfwmark, |
| 1034 | IP_VS_DBG_ADDR(dest->af, &dest->addr), |
| 1035 | ntohs(dest->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | ip_vs_dst_reset(dest); |
| 1037 | /* simply decrease svc->refcnt here, let the caller check |
| 1038 | and release the service if nobody refers to it. |
| 1039 | Only user context can release destination and service, |
| 1040 | and only one user context can update virtual service at a |
| 1041 | time, so the operation here is OK */ |
| 1042 | atomic_dec(&dest->svc->refcnt); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 1043 | free_percpu(dest->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | kfree(dest); |
| 1045 | } else { |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 1046 | IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, " |
| 1047 | "dest->refcnt=%d\n", |
| 1048 | IP_VS_DBG_ADDR(dest->af, &dest->addr), |
| 1049 | ntohs(dest->port), |
| 1050 | atomic_read(&dest->refcnt)); |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 1051 | list_add(&dest->n_list, &ipvs->dest_trash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | atomic_inc(&dest->refcnt); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | /* |
| 1058 | * Unlink a destination from the given service |
| 1059 | */ |
| 1060 | static void __ip_vs_unlink_dest(struct ip_vs_service *svc, |
| 1061 | struct ip_vs_dest *dest, |
| 1062 | int svcupd) |
| 1063 | { |
| 1064 | dest->flags &= ~IP_VS_DEST_F_AVAILABLE; |
| 1065 | |
| 1066 | /* |
| 1067 | * Remove it from the d-linked destination list. |
| 1068 | */ |
| 1069 | list_del(&dest->n_list); |
| 1070 | svc->num_dests--; |
Sven Wegener | 82dfb6f | 2008-08-11 19:36:06 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* |
| 1073 | * Call the update_service function of its scheduler |
| 1074 | */ |
| 1075 | if (svcupd && svc->scheduler->update_service) |
| 1076 | svc->scheduler->update_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | /* |
| 1081 | * Delete a destination server in the given service |
| 1082 | */ |
| 1083 | static int |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1084 | ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
| 1086 | struct ip_vs_dest *dest; |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 1087 | __be16 dport = udest->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | EnterFunction(2); |
| 1090 | |
Julius Volz | 7937df1 | 2008-09-02 15:55:48 +0200 | [diff] [blame] | 1091 | dest = ip_vs_lookup_dest(svc, &udest->addr, dport); |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (dest == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1094 | IP_VS_DBG(1, "%s(): destination not found!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | return -ENOENT; |
| 1096 | } |
| 1097 | |
| 1098 | write_lock_bh(&__ip_vs_svc_lock); |
| 1099 | |
| 1100 | /* |
| 1101 | * Wait until all other svc users go away. |
| 1102 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1103 | IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
| 1105 | /* |
| 1106 | * Unlink dest from the service |
| 1107 | */ |
| 1108 | __ip_vs_unlink_dest(svc, dest, 1); |
| 1109 | |
| 1110 | write_unlock_bh(&__ip_vs_svc_lock); |
| 1111 | |
| 1112 | /* |
| 1113 | * Delete the destination |
| 1114 | */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1115 | __ip_vs_del_dest(svc->net, dest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
| 1117 | LeaveFunction(2); |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | /* |
| 1124 | * Add a service into the service hash table |
| 1125 | */ |
| 1126 | static int |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1127 | ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u, |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1128 | struct ip_vs_service **svc_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | { |
| 1130 | int ret = 0; |
| 1131 | struct ip_vs_scheduler *sched = NULL; |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1132 | struct ip_vs_pe *pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | struct ip_vs_service *svc = NULL; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1134 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | /* increase the module use count */ |
| 1137 | ip_vs_use_count_inc(); |
| 1138 | |
| 1139 | /* Lookup the scheduler by 'u->sched_name' */ |
| 1140 | sched = ip_vs_scheduler_get(u->sched_name); |
| 1141 | if (sched == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1142 | pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | ret = -ENOENT; |
Simon Horman | 6e08bfb | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 1144 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1147 | if (u->pe_name && *u->pe_name) { |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 1148 | pe = ip_vs_pe_getbyname(u->pe_name); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1149 | if (pe == NULL) { |
| 1150 | pr_info("persistence engine module ip_vs_pe_%s " |
| 1151 | "not found\n", u->pe_name); |
| 1152 | ret = -ENOENT; |
| 1153 | goto out_err; |
| 1154 | } |
| 1155 | } |
| 1156 | |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1157 | #ifdef CONFIG_IP_VS_IPV6 |
Julius Volz | 4814893 | 2008-11-03 17:08:56 -0800 | [diff] [blame] | 1158 | if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) { |
| 1159 | ret = -EINVAL; |
| 1160 | goto out_err; |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1161 | } |
| 1162 | #endif |
| 1163 | |
Simon Horman | dee06e4 | 2010-08-26 02:54:31 +0000 | [diff] [blame] | 1164 | svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | if (svc == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1166 | IP_VS_DBG(1, "%s(): no memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | ret = -ENOMEM; |
| 1168 | goto out_err; |
| 1169 | } |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 1170 | svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats); |
| 1171 | if (!svc->stats.cpustats) { |
| 1172 | pr_err("%s() alloc_percpu failed\n", __func__); |
| 1173 | goto out_err; |
| 1174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | /* I'm the first user of the service */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1177 | atomic_set(&svc->usecnt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | atomic_set(&svc->refcnt, 0); |
| 1179 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1180 | svc->af = u->af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | svc->protocol = u->protocol; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1182 | ip_vs_addr_copy(svc->af, &svc->addr, &u->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | svc->port = u->port; |
| 1184 | svc->fwmark = u->fwmark; |
| 1185 | svc->flags = u->flags; |
| 1186 | svc->timeout = u->timeout * HZ; |
| 1187 | svc->netmask = u->netmask; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1188 | svc->net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | INIT_LIST_HEAD(&svc->destinations); |
| 1191 | rwlock_init(&svc->sched_lock); |
| 1192 | spin_lock_init(&svc->stats.lock); |
| 1193 | |
| 1194 | /* Bind the scheduler */ |
| 1195 | ret = ip_vs_bind_scheduler(svc, sched); |
| 1196 | if (ret) |
| 1197 | goto out_err; |
| 1198 | sched = NULL; |
| 1199 | |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1200 | /* Bind the ct retriever */ |
| 1201 | ip_vs_bind_pe(svc, pe); |
| 1202 | pe = NULL; |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | /* Update the virtual service counters */ |
| 1205 | if (svc->port == FTPPORT) |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 1206 | atomic_inc(&ipvs->ftpsvc_counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | else if (svc->port == 0) |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 1208 | atomic_inc(&ipvs->nullsvc_counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 1210 | ip_vs_start_estimator(net, &svc->stats); |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1211 | |
| 1212 | /* Count only IPv4 services for old get/setsockopt interface */ |
| 1213 | if (svc->af == AF_INET) |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1214 | ipvs->num_services++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
| 1216 | /* Hash the service into the service table */ |
| 1217 | write_lock_bh(&__ip_vs_svc_lock); |
| 1218 | ip_vs_svc_hash(svc); |
| 1219 | write_unlock_bh(&__ip_vs_svc_lock); |
| 1220 | |
| 1221 | *svc_p = svc; |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 1222 | /* Now there is a service - full throttle */ |
| 1223 | ipvs->enable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | return 0; |
| 1225 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 1226 | |
Simon Horman | 6e08bfb | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 1227 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (svc != NULL) { |
Simon Horman | 2fabf35 | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 1229 | ip_vs_unbind_scheduler(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | if (svc->inc) { |
| 1231 | local_bh_disable(); |
| 1232 | ip_vs_app_inc_put(svc->inc); |
| 1233 | local_bh_enable(); |
| 1234 | } |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 1235 | if (svc->stats.cpustats) |
| 1236 | free_percpu(svc->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | kfree(svc); |
| 1238 | } |
| 1239 | ip_vs_scheduler_put(sched); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1240 | ip_vs_pe_put(pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | /* decrease the module use count */ |
| 1243 | ip_vs_use_count_dec(); |
| 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | /* |
| 1250 | * Edit a service and bind it with a new scheduler |
| 1251 | */ |
| 1252 | static int |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 1253 | ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | { |
| 1255 | struct ip_vs_scheduler *sched, *old_sched; |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1256 | struct ip_vs_pe *pe = NULL, *old_pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | int ret = 0; |
| 1258 | |
| 1259 | /* |
| 1260 | * Lookup the scheduler, by 'u->sched_name' |
| 1261 | */ |
| 1262 | sched = ip_vs_scheduler_get(u->sched_name); |
| 1263 | if (sched == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1264 | pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | return -ENOENT; |
| 1266 | } |
| 1267 | old_sched = sched; |
| 1268 | |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1269 | if (u->pe_name && *u->pe_name) { |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 1270 | pe = ip_vs_pe_getbyname(u->pe_name); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1271 | if (pe == NULL) { |
| 1272 | pr_info("persistence engine module ip_vs_pe_%s " |
| 1273 | "not found\n", u->pe_name); |
| 1274 | ret = -ENOENT; |
| 1275 | goto out; |
| 1276 | } |
| 1277 | old_pe = pe; |
| 1278 | } |
| 1279 | |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1280 | #ifdef CONFIG_IP_VS_IPV6 |
Julius Volz | 4814893 | 2008-11-03 17:08:56 -0800 | [diff] [blame] | 1281 | if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) { |
| 1282 | ret = -EINVAL; |
| 1283 | goto out; |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1284 | } |
| 1285 | #endif |
| 1286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | write_lock_bh(&__ip_vs_svc_lock); |
| 1288 | |
| 1289 | /* |
| 1290 | * Wait until all other svc users go away. |
| 1291 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1292 | IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* |
| 1295 | * Set the flags and timeout value |
| 1296 | */ |
| 1297 | svc->flags = u->flags | IP_VS_SVC_F_HASHED; |
| 1298 | svc->timeout = u->timeout * HZ; |
| 1299 | svc->netmask = u->netmask; |
| 1300 | |
| 1301 | old_sched = svc->scheduler; |
| 1302 | if (sched != old_sched) { |
| 1303 | /* |
| 1304 | * Unbind the old scheduler |
| 1305 | */ |
| 1306 | if ((ret = ip_vs_unbind_scheduler(svc))) { |
| 1307 | old_sched = sched; |
Simon Horman | 9e691ed | 2008-09-17 10:10:41 +1000 | [diff] [blame] | 1308 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * Bind the new scheduler |
| 1313 | */ |
| 1314 | if ((ret = ip_vs_bind_scheduler(svc, sched))) { |
| 1315 | /* |
| 1316 | * If ip_vs_bind_scheduler fails, restore the old |
| 1317 | * scheduler. |
| 1318 | * The main reason of failure is out of memory. |
| 1319 | * |
| 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. |
| 1324 | */ |
| 1325 | ip_vs_bind_scheduler(svc, old_sched); |
| 1326 | old_sched = sched; |
Simon Horman | 9e691ed | 2008-09-17 10:10:41 +1000 | [diff] [blame] | 1327 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1331 | old_pe = svc->pe; |
| 1332 | if (pe != old_pe) { |
| 1333 | ip_vs_unbind_pe(svc); |
| 1334 | ip_vs_bind_pe(svc, pe); |
| 1335 | } |
| 1336 | |
Simon Horman | 9e691ed | 2008-09-17 10:10:41 +1000 | [diff] [blame] | 1337 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | write_unlock_bh(&__ip_vs_svc_lock); |
Simon Horman | 9e691ed | 2008-09-17 10:10:41 +1000 | [diff] [blame] | 1339 | out: |
Simon Horman | 6e08bfb | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 1340 | ip_vs_scheduler_put(old_sched); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1341 | ip_vs_pe_put(old_pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return ret; |
| 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | /* |
| 1347 | * Delete a service from the service list |
| 1348 | * - The service must be unlinked, unlocked and not referenced! |
| 1349 | * - We are called under _bh lock |
| 1350 | */ |
| 1351 | static void __ip_vs_del_service(struct ip_vs_service *svc) |
| 1352 | { |
| 1353 | struct ip_vs_dest *dest, *nxt; |
| 1354 | struct ip_vs_scheduler *old_sched; |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1355 | struct ip_vs_pe *old_pe; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1356 | struct netns_ipvs *ipvs = net_ipvs(svc->net); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1357 | |
| 1358 | pr_info("%s: enter\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1360 | /* Count only IPv4 services for old get/setsockopt interface */ |
| 1361 | if (svc->af == AF_INET) |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1362 | ipvs->num_services--; |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 1363 | |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 1364 | ip_vs_stop_estimator(svc->net, &svc->stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* Unbind scheduler */ |
| 1367 | old_sched = svc->scheduler; |
| 1368 | ip_vs_unbind_scheduler(svc); |
Simon Horman | 6e08bfb | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 1369 | ip_vs_scheduler_put(old_sched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 1371 | /* Unbind persistence engine */ |
| 1372 | old_pe = svc->pe; |
| 1373 | ip_vs_unbind_pe(svc); |
| 1374 | ip_vs_pe_put(old_pe); |
| 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | /* Unbind app inc */ |
| 1377 | if (svc->inc) { |
| 1378 | ip_vs_app_inc_put(svc->inc); |
| 1379 | svc->inc = NULL; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | * Unlink the whole destination list |
| 1384 | */ |
| 1385 | list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) { |
| 1386 | __ip_vs_unlink_dest(svc, dest, 0); |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 1387 | __ip_vs_del_dest(svc->net, dest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Update the virtual service counters |
| 1392 | */ |
| 1393 | if (svc->port == FTPPORT) |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 1394 | atomic_dec(&ipvs->ftpsvc_counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | else if (svc->port == 0) |
Hans Schillstrom | 763f8d0 | 2011-01-03 14:45:01 +0100 | [diff] [blame] | 1396 | atomic_dec(&ipvs->nullsvc_counter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
| 1398 | /* |
| 1399 | * Free the service if nobody refers to it |
| 1400 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1401 | if (atomic_read(&svc->refcnt) == 0) { |
| 1402 | IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n", |
| 1403 | svc->fwmark, |
| 1404 | IP_VS_DBG_ADDR(svc->af, &svc->addr), |
| 1405 | ntohs(svc->port), atomic_read(&svc->usecnt)); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 1406 | free_percpu(svc->stats.cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | kfree(svc); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
| 1410 | /* decrease the module use count */ |
| 1411 | ip_vs_use_count_dec(); |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1415 | * Unlink a service from list and try to delete it if its refcnt reached 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1417 | static void ip_vs_unlink_service(struct ip_vs_service *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * Unhash it from the service table |
| 1421 | */ |
| 1422 | write_lock_bh(&__ip_vs_svc_lock); |
| 1423 | |
| 1424 | ip_vs_svc_unhash(svc); |
| 1425 | |
| 1426 | /* |
| 1427 | * Wait until all the svc users go away. |
| 1428 | */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1429 | IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
| 1431 | __ip_vs_del_service(svc); |
| 1432 | |
| 1433 | write_unlock_bh(&__ip_vs_svc_lock); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * Delete a service from the service list |
| 1438 | */ |
| 1439 | static int ip_vs_del_service(struct ip_vs_service *svc) |
| 1440 | { |
| 1441 | if (svc == NULL) |
| 1442 | return -EEXIST; |
| 1443 | ip_vs_unlink_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /* |
| 1450 | * Flush all the virtual services |
| 1451 | */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1452 | static int ip_vs_flush(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
| 1454 | int idx; |
| 1455 | struct ip_vs_service *svc, *nxt; |
| 1456 | |
| 1457 | /* |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1458 | * Flush the service table hashed by <netns,protocol,addr,port> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | */ |
| 1460 | for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1461 | list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], |
| 1462 | s_list) { |
| 1463 | if (net_eq(svc->net, net)) |
| 1464 | ip_vs_unlink_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Flush the service table hashed by fwmark |
| 1470 | */ |
| 1471 | for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1472 | list_for_each_entry_safe(svc, nxt, |
| 1473 | &ip_vs_svc_fwm_table[idx], f_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1474 | if (net_eq(svc->net, net)) |
| 1475 | ip_vs_unlink_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 1482 | /* |
| 1483 | * Delete service by {netns} in the service table. |
| 1484 | * Called by __ip_vs_cleanup() |
| 1485 | */ |
| 1486 | void __ip_vs_service_cleanup(struct net *net) |
| 1487 | { |
| 1488 | EnterFunction(2); |
| 1489 | /* Check for "full" addressed entries */ |
| 1490 | mutex_lock(&__ip_vs_mutex); |
| 1491 | ip_vs_flush(net); |
| 1492 | mutex_unlock(&__ip_vs_mutex); |
| 1493 | LeaveFunction(2); |
| 1494 | } |
| 1495 | /* |
| 1496 | * Release dst hold by dst_cache |
| 1497 | */ |
| 1498 | static inline void |
| 1499 | __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev) |
| 1500 | { |
| 1501 | spin_lock_bh(&dest->dst_lock); |
| 1502 | if (dest->dst_cache && dest->dst_cache->dev == dev) { |
| 1503 | IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n", |
| 1504 | dev->name, |
| 1505 | IP_VS_DBG_ADDR(dest->af, &dest->addr), |
| 1506 | ntohs(dest->port), |
| 1507 | atomic_read(&dest->refcnt)); |
| 1508 | ip_vs_dst_reset(dest); |
| 1509 | } |
| 1510 | spin_unlock_bh(&dest->dst_lock); |
| 1511 | |
| 1512 | } |
| 1513 | /* |
| 1514 | * Netdev event receiver |
| 1515 | * Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to |
| 1516 | * a device that is "unregister" it must be released. |
| 1517 | */ |
| 1518 | static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, |
| 1519 | void *ptr) |
| 1520 | { |
| 1521 | struct net_device *dev = ptr; |
| 1522 | struct net *net = dev_net(dev); |
| 1523 | struct ip_vs_service *svc; |
| 1524 | struct ip_vs_dest *dest; |
| 1525 | unsigned int idx; |
| 1526 | |
| 1527 | if (event != NETDEV_UNREGISTER) |
| 1528 | return NOTIFY_DONE; |
| 1529 | IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name); |
| 1530 | EnterFunction(2); |
| 1531 | mutex_lock(&__ip_vs_mutex); |
| 1532 | for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1533 | list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) { |
| 1534 | if (net_eq(svc->net, net)) { |
| 1535 | list_for_each_entry(dest, &svc->destinations, |
| 1536 | n_list) { |
| 1537 | __ip_vs_dev_reset(dest, dev); |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) { |
| 1543 | if (net_eq(svc->net, net)) { |
| 1544 | list_for_each_entry(dest, &svc->destinations, |
| 1545 | n_list) { |
| 1546 | __ip_vs_dev_reset(dest, dev); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | list_for_each_entry(dest, &net_ipvs(net)->dest_trash, n_list) { |
| 1554 | __ip_vs_dev_reset(dest, dev); |
| 1555 | } |
| 1556 | mutex_unlock(&__ip_vs_mutex); |
| 1557 | LeaveFunction(2); |
| 1558 | return NOTIFY_DONE; |
| 1559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
| 1561 | /* |
| 1562 | * Zero counters in a service or all services |
| 1563 | */ |
| 1564 | static int ip_vs_zero_service(struct ip_vs_service *svc) |
| 1565 | { |
| 1566 | struct ip_vs_dest *dest; |
| 1567 | |
| 1568 | write_lock_bh(&__ip_vs_svc_lock); |
| 1569 | list_for_each_entry(dest, &svc->destinations, n_list) { |
| 1570 | ip_vs_zero_stats(&dest->stats); |
| 1571 | } |
| 1572 | ip_vs_zero_stats(&svc->stats); |
| 1573 | write_unlock_bh(&__ip_vs_svc_lock); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1577 | static int ip_vs_zero_all(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | { |
| 1579 | int idx; |
| 1580 | struct ip_vs_service *svc; |
| 1581 | |
| 1582 | for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1583 | list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1584 | if (net_eq(svc->net, net)) |
| 1585 | ip_vs_zero_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1590 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1591 | if (net_eq(svc->net, net)) |
| 1592 | ip_vs_zero_service(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 1596 | ip_vs_zero_stats(&net_ipvs(net)->tot_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | return 0; |
| 1598 | } |
| 1599 | |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 1600 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1602 | proc_do_defense_mode(ctl_table *table, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 1604 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1605 | struct net *net = current->nsproxy->net_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | int *valp = table->data; |
| 1607 | int val = *valp; |
| 1608 | int rc; |
| 1609 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1610 | rc = proc_dointvec(table, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | if (write && (*valp != val)) { |
| 1612 | if ((*valp < 0) || (*valp > 3)) { |
| 1613 | /* Restore the correct value */ |
| 1614 | *valp = val; |
| 1615 | } else { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1616 | update_defense_level(net_ipvs(net)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | return rc; |
| 1620 | } |
| 1621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | static int |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1623 | proc_do_sync_threshold(ctl_table *table, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 1625 | { |
| 1626 | int *valp = table->data; |
| 1627 | int val[2]; |
| 1628 | int rc; |
| 1629 | |
| 1630 | /* backup the value first */ |
| 1631 | memcpy(val, valp, sizeof(val)); |
| 1632 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1633 | rc = proc_dointvec(table, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) { |
| 1635 | /* Restore the correct value */ |
| 1636 | memcpy(valp, val, sizeof(val)); |
| 1637 | } |
| 1638 | return rc; |
| 1639 | } |
| 1640 | |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 1641 | static int |
| 1642 | proc_do_sync_mode(ctl_table *table, int write, |
| 1643 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 1644 | { |
| 1645 | int *valp = table->data; |
| 1646 | int val = *valp; |
| 1647 | int rc; |
| 1648 | |
| 1649 | rc = proc_dointvec(table, write, buffer, lenp, ppos); |
| 1650 | if (write && (*valp != val)) { |
| 1651 | if ((*valp < 0) || (*valp > 1)) { |
| 1652 | /* Restore the correct value */ |
| 1653 | *valp = val; |
| 1654 | } else { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1655 | struct net *net = current->nsproxy->net_ns; |
| 1656 | ip_vs_sync_switch_mode(net, val); |
Hans Schillstrom | b880c1f | 2010-11-19 14:25:14 +0100 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | return rc; |
| 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
| 1662 | /* |
| 1663 | * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/) |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1664 | * Do not change order or insert new entries without |
| 1665 | * align with netns init in __ip_vs_control_init() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | */ |
| 1667 | |
| 1668 | static struct ctl_table vs_vars[] = { |
| 1669 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | .procname = "amemthresh", |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1671 | .maxlen = sizeof(int), |
| 1672 | .mode = 0644, |
| 1673 | .proc_handler = proc_dointvec, |
| 1674 | }, |
| 1675 | { |
| 1676 | .procname = "am_droprate", |
| 1677 | .maxlen = sizeof(int), |
| 1678 | .mode = 0644, |
| 1679 | .proc_handler = proc_dointvec, |
| 1680 | }, |
| 1681 | { |
| 1682 | .procname = "drop_entry", |
| 1683 | .maxlen = sizeof(int), |
| 1684 | .mode = 0644, |
| 1685 | .proc_handler = proc_do_defense_mode, |
| 1686 | }, |
| 1687 | { |
| 1688 | .procname = "drop_packet", |
| 1689 | .maxlen = sizeof(int), |
| 1690 | .mode = 0644, |
| 1691 | .proc_handler = proc_do_defense_mode, |
| 1692 | }, |
| 1693 | #ifdef CONFIG_IP_VS_NFCT |
| 1694 | { |
| 1695 | .procname = "conntrack", |
| 1696 | .maxlen = sizeof(int), |
| 1697 | .mode = 0644, |
| 1698 | .proc_handler = &proc_dointvec, |
| 1699 | }, |
| 1700 | #endif |
| 1701 | { |
| 1702 | .procname = "secure_tcp", |
| 1703 | .maxlen = sizeof(int), |
| 1704 | .mode = 0644, |
| 1705 | .proc_handler = proc_do_defense_mode, |
| 1706 | }, |
| 1707 | { |
| 1708 | .procname = "snat_reroute", |
| 1709 | .maxlen = sizeof(int), |
| 1710 | .mode = 0644, |
| 1711 | .proc_handler = &proc_dointvec, |
| 1712 | }, |
| 1713 | { |
| 1714 | .procname = "sync_version", |
| 1715 | .maxlen = sizeof(int), |
| 1716 | .mode = 0644, |
| 1717 | .proc_handler = &proc_do_sync_mode, |
| 1718 | }, |
| 1719 | { |
| 1720 | .procname = "cache_bypass", |
| 1721 | .maxlen = sizeof(int), |
| 1722 | .mode = 0644, |
| 1723 | .proc_handler = proc_dointvec, |
| 1724 | }, |
| 1725 | { |
| 1726 | .procname = "expire_nodest_conn", |
| 1727 | .maxlen = sizeof(int), |
| 1728 | .mode = 0644, |
| 1729 | .proc_handler = proc_dointvec, |
| 1730 | }, |
| 1731 | { |
| 1732 | .procname = "expire_quiescent_template", |
| 1733 | .maxlen = sizeof(int), |
| 1734 | .mode = 0644, |
| 1735 | .proc_handler = proc_dointvec, |
| 1736 | }, |
| 1737 | { |
| 1738 | .procname = "sync_threshold", |
| 1739 | .maxlen = |
| 1740 | sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold), |
| 1741 | .mode = 0644, |
| 1742 | .proc_handler = proc_do_sync_threshold, |
| 1743 | }, |
| 1744 | { |
| 1745 | .procname = "nat_icmp_send", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | .maxlen = sizeof(int), |
| 1747 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1748 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | }, |
| 1750 | #ifdef CONFIG_IP_VS_DEBUG |
| 1751 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | .procname = "debug_level", |
| 1753 | .data = &sysctl_ip_vs_debug_level, |
| 1754 | .maxlen = sizeof(int), |
| 1755 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1756 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | }, |
| 1758 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | #if 0 |
| 1760 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | .procname = "timeout_established", |
| 1762 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED], |
| 1763 | .maxlen = sizeof(int), |
| 1764 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1765 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | }, |
| 1767 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | .procname = "timeout_synsent", |
| 1769 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT], |
| 1770 | .maxlen = sizeof(int), |
| 1771 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1772 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | }, |
| 1774 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | .procname = "timeout_synrecv", |
| 1776 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV], |
| 1777 | .maxlen = sizeof(int), |
| 1778 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1779 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | }, |
| 1781 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | .procname = "timeout_finwait", |
| 1783 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT], |
| 1784 | .maxlen = sizeof(int), |
| 1785 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1786 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | }, |
| 1788 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | .procname = "timeout_timewait", |
| 1790 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT], |
| 1791 | .maxlen = sizeof(int), |
| 1792 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1793 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | }, |
| 1795 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | .procname = "timeout_close", |
| 1797 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE], |
| 1798 | .maxlen = sizeof(int), |
| 1799 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1800 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | }, |
| 1802 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | .procname = "timeout_closewait", |
| 1804 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT], |
| 1805 | .maxlen = sizeof(int), |
| 1806 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1807 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | }, |
| 1809 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | .procname = "timeout_lastack", |
| 1811 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK], |
| 1812 | .maxlen = sizeof(int), |
| 1813 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1814 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | }, |
| 1816 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | .procname = "timeout_listen", |
| 1818 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN], |
| 1819 | .maxlen = sizeof(int), |
| 1820 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1821 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | }, |
| 1823 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | .procname = "timeout_synack", |
| 1825 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK], |
| 1826 | .maxlen = sizeof(int), |
| 1827 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1828 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | }, |
| 1830 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | .procname = "timeout_udp", |
| 1832 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP], |
| 1833 | .maxlen = sizeof(int), |
| 1834 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1835 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | }, |
| 1837 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | .procname = "timeout_icmp", |
| 1839 | .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP], |
| 1840 | .maxlen = sizeof(int), |
| 1841 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1842 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | }, |
| 1844 | #endif |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1845 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | }; |
| 1847 | |
Sven Wegener | 5587da5 | 2008-08-10 18:24:40 +0000 | [diff] [blame] | 1848 | const struct ctl_path net_vs_ctl_path[] = { |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1849 | { .procname = "net", }, |
| 1850 | { .procname = "ipv4", }, |
Pavel Emelyanov | 90754f8 | 2008-01-12 02:33:50 -0800 | [diff] [blame] | 1851 | { .procname = "vs", }, |
| 1852 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | }; |
Pavel Emelyanov | 90754f8 | 2008-01-12 02:33:50 -0800 | [diff] [blame] | 1854 | EXPORT_SYMBOL_GPL(net_vs_ctl_path); |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 1855 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | #ifdef CONFIG_PROC_FS |
| 1858 | |
| 1859 | struct ip_vs_iter { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1860 | struct seq_net_private p; /* Do not move this, netns depends upon it*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | struct list_head *table; |
| 1862 | int bucket; |
| 1863 | }; |
| 1864 | |
| 1865 | /* |
| 1866 | * Write the contents of the VS rule table to a PROCfs file. |
| 1867 | * (It is kept just for backward compatibility) |
| 1868 | */ |
| 1869 | static inline const char *ip_vs_fwd_name(unsigned flags) |
| 1870 | { |
| 1871 | switch (flags & IP_VS_CONN_F_FWD_MASK) { |
| 1872 | case IP_VS_CONN_F_LOCALNODE: |
| 1873 | return "Local"; |
| 1874 | case IP_VS_CONN_F_TUNNEL: |
| 1875 | return "Tunnel"; |
| 1876 | case IP_VS_CONN_F_DROUTE: |
| 1877 | return "Route"; |
| 1878 | default: |
| 1879 | return "Masq"; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | /* Get the Nth entry in the two lists */ |
| 1885 | static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos) |
| 1886 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1887 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | struct ip_vs_iter *iter = seq->private; |
| 1889 | int idx; |
| 1890 | struct ip_vs_service *svc; |
| 1891 | |
| 1892 | /* look in hash by protocol */ |
| 1893 | for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1894 | list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1895 | if (net_eq(svc->net, net) && pos-- == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | iter->table = ip_vs_svc_table; |
| 1897 | iter->bucket = idx; |
| 1898 | return svc; |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* keep looking in fwmark */ |
| 1904 | for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 1905 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1906 | if (net_eq(svc->net, net) && pos-- == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | iter->table = ip_vs_svc_fwm_table; |
| 1908 | iter->bucket = idx; |
| 1909 | return svc; |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | return NULL; |
| 1915 | } |
| 1916 | |
| 1917 | static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos) |
Simon Horman | 563e94f | 2008-09-17 10:10:42 +1000 | [diff] [blame] | 1918 | __acquires(__ip_vs_svc_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | { |
| 1920 | |
| 1921 | read_lock_bh(&__ip_vs_svc_lock); |
| 1922 | return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN; |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1927 | { |
| 1928 | struct list_head *e; |
| 1929 | struct ip_vs_iter *iter; |
| 1930 | struct ip_vs_service *svc; |
| 1931 | |
| 1932 | ++*pos; |
| 1933 | if (v == SEQ_START_TOKEN) |
| 1934 | return ip_vs_info_array(seq,0); |
| 1935 | |
| 1936 | svc = v; |
| 1937 | iter = seq->private; |
| 1938 | |
| 1939 | if (iter->table == ip_vs_svc_table) { |
| 1940 | /* next service in table hashed by protocol */ |
| 1941 | if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket]) |
| 1942 | return list_entry(e, struct ip_vs_service, s_list); |
| 1943 | |
| 1944 | |
| 1945 | while (++iter->bucket < IP_VS_SVC_TAB_SIZE) { |
| 1946 | list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket], |
| 1947 | s_list) { |
| 1948 | return svc; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | iter->table = ip_vs_svc_fwm_table; |
| 1953 | iter->bucket = -1; |
| 1954 | goto scan_fwmark; |
| 1955 | } |
| 1956 | |
| 1957 | /* next service in hashed by fwmark */ |
| 1958 | if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket]) |
| 1959 | return list_entry(e, struct ip_vs_service, f_list); |
| 1960 | |
| 1961 | scan_fwmark: |
| 1962 | while (++iter->bucket < IP_VS_SVC_TAB_SIZE) { |
| 1963 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket], |
| 1964 | f_list) |
| 1965 | return svc; |
| 1966 | } |
| 1967 | |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | |
| 1971 | static void ip_vs_info_seq_stop(struct seq_file *seq, void *v) |
Simon Horman | 563e94f | 2008-09-17 10:10:42 +1000 | [diff] [blame] | 1972 | __releases(__ip_vs_svc_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | { |
| 1974 | read_unlock_bh(&__ip_vs_svc_lock); |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | static int ip_vs_info_seq_show(struct seq_file *seq, void *v) |
| 1979 | { |
| 1980 | if (v == SEQ_START_TOKEN) { |
| 1981 | seq_printf(seq, |
| 1982 | "IP Virtual Server version %d.%d.%d (size=%d)\n", |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 1983 | NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | seq_puts(seq, |
| 1985 | "Prot LocalAddress:Port Scheduler Flags\n"); |
| 1986 | seq_puts(seq, |
| 1987 | " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n"); |
| 1988 | } else { |
| 1989 | const struct ip_vs_service *svc = v; |
| 1990 | const struct ip_vs_iter *iter = seq->private; |
| 1991 | const struct ip_vs_dest *dest; |
| 1992 | |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1993 | if (iter->table == ip_vs_svc_table) { |
| 1994 | #ifdef CONFIG_IP_VS_IPV6 |
| 1995 | if (svc->af == AF_INET6) |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1996 | seq_printf(seq, "%s [%pI6]:%04X %s ", |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1997 | ip_vs_proto_name(svc->protocol), |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 1998 | &svc->addr.in6, |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1999 | ntohs(svc->port), |
| 2000 | svc->scheduler->name); |
| 2001 | else |
| 2002 | #endif |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 2003 | seq_printf(seq, "%s %08X:%04X %s %s ", |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2004 | ip_vs_proto_name(svc->protocol), |
| 2005 | ntohl(svc->addr.ip), |
| 2006 | ntohs(svc->port), |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 2007 | svc->scheduler->name, |
| 2008 | (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":""); |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2009 | } else { |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 2010 | seq_printf(seq, "FWM %08X %s %s", |
| 2011 | svc->fwmark, svc->scheduler->name, |
| 2012 | (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":""); |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
| 2015 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) |
| 2016 | seq_printf(seq, "persistent %d %08X\n", |
| 2017 | svc->timeout, |
| 2018 | ntohl(svc->netmask)); |
| 2019 | else |
| 2020 | seq_putc(seq, '\n'); |
| 2021 | |
| 2022 | list_for_each_entry(dest, &svc->destinations, n_list) { |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2023 | #ifdef CONFIG_IP_VS_IPV6 |
| 2024 | if (dest->af == AF_INET6) |
| 2025 | seq_printf(seq, |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2026 | " -> [%pI6]:%04X" |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2027 | " %-7s %-6d %-10d %-10d\n", |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 2028 | &dest->addr.in6, |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 2029 | ntohs(dest->port), |
| 2030 | ip_vs_fwd_name(atomic_read(&dest->conn_flags)), |
| 2031 | atomic_read(&dest->weight), |
| 2032 | atomic_read(&dest->activeconns), |
| 2033 | atomic_read(&dest->inactconns)); |
| 2034 | else |
| 2035 | #endif |
| 2036 | seq_printf(seq, |
| 2037 | " -> %08X:%04X " |
| 2038 | "%-7s %-6d %-10d %-10d\n", |
| 2039 | ntohl(dest->addr.ip), |
| 2040 | ntohs(dest->port), |
| 2041 | ip_vs_fwd_name(atomic_read(&dest->conn_flags)), |
| 2042 | atomic_read(&dest->weight), |
| 2043 | atomic_read(&dest->activeconns), |
| 2044 | atomic_read(&dest->inactconns)); |
| 2045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2051 | static const struct seq_operations ip_vs_info_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | .start = ip_vs_info_seq_start, |
| 2053 | .next = ip_vs_info_seq_next, |
| 2054 | .stop = ip_vs_info_seq_stop, |
| 2055 | .show = ip_vs_info_seq_show, |
| 2056 | }; |
| 2057 | |
| 2058 | static int ip_vs_info_open(struct inode *inode, struct file *file) |
| 2059 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2060 | return seq_open_net(inode, file, &ip_vs_info_seq_ops, |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 2061 | sizeof(struct ip_vs_iter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2064 | static const struct file_operations ip_vs_info_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | .owner = THIS_MODULE, |
| 2066 | .open = ip_vs_info_open, |
| 2067 | .read = seq_read, |
| 2068 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 2069 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | }; |
| 2071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | static int ip_vs_stats_show(struct seq_file *seq, void *v) |
| 2073 | { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2074 | struct net *net = seq_file_single_net(seq); |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2075 | struct ip_vs_stats_user show; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | |
| 2077 | /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ |
| 2078 | seq_puts(seq, |
| 2079 | " Total Incoming Outgoing Incoming Outgoing\n"); |
| 2080 | seq_printf(seq, |
| 2081 | " Conns Packets Packets Bytes Bytes\n"); |
| 2082 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2083 | ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats); |
| 2084 | seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns, |
| 2085 | show.inpkts, show.outpkts, |
| 2086 | (unsigned long long) show.inbytes, |
| 2087 | (unsigned long long) show.outbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
| 2089 | /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ |
| 2090 | seq_puts(seq, |
| 2091 | " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n"); |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2092 | seq_printf(seq, "%8X %8X %8X %16X %16X\n", |
| 2093 | show.cps, show.inpps, show.outpps, |
| 2094 | show.inbps, show.outbps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | static int ip_vs_stats_seq_open(struct inode *inode, struct file *file) |
| 2100 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2101 | return single_open_net(inode, file, ip_vs_stats_show); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2104 | static const struct file_operations ip_vs_stats_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | .owner = THIS_MODULE, |
| 2106 | .open = ip_vs_stats_seq_open, |
| 2107 | .read = seq_read, |
| 2108 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 2109 | .release = single_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | }; |
| 2111 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2112 | static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v) |
| 2113 | { |
| 2114 | struct net *net = seq_file_single_net(seq); |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 2115 | struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats; |
| 2116 | struct ip_vs_cpu_stats *cpustats = tot_stats->cpustats; |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 2117 | struct ip_vs_stats_user rates; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2118 | int i; |
| 2119 | |
| 2120 | /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ |
| 2121 | seq_puts(seq, |
| 2122 | " Total Incoming Outgoing Incoming Outgoing\n"); |
| 2123 | seq_printf(seq, |
| 2124 | "CPU Conns Packets Packets Bytes Bytes\n"); |
| 2125 | |
| 2126 | for_each_possible_cpu(i) { |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 2127 | struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i); |
| 2128 | unsigned int start; |
| 2129 | __u64 inbytes, outbytes; |
| 2130 | |
| 2131 | do { |
| 2132 | start = u64_stats_fetch_begin_bh(&u->syncp); |
| 2133 | inbytes = u->ustats.inbytes; |
| 2134 | outbytes = u->ustats.outbytes; |
| 2135 | } while (u64_stats_fetch_retry_bh(&u->syncp, start)); |
| 2136 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2137 | seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n", |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 2138 | i, u->ustats.conns, u->ustats.inpkts, |
| 2139 | u->ustats.outpkts, (__u64)inbytes, |
| 2140 | (__u64)outbytes); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | spin_lock_bh(&tot_stats->lock); |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 2144 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2145 | seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n", |
| 2146 | tot_stats->ustats.conns, tot_stats->ustats.inpkts, |
| 2147 | tot_stats->ustats.outpkts, |
| 2148 | (unsigned long long) tot_stats->ustats.inbytes, |
| 2149 | (unsigned long long) tot_stats->ustats.outbytes); |
| 2150 | |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 2151 | ip_vs_read_estimator(&rates, tot_stats); |
| 2152 | |
| 2153 | spin_unlock_bh(&tot_stats->lock); |
| 2154 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2155 | /* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ |
| 2156 | seq_puts(seq, |
| 2157 | " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n"); |
| 2158 | seq_printf(seq, " %8X %8X %8X %16X %16X\n", |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 2159 | rates.cps, |
| 2160 | rates.inpps, |
| 2161 | rates.outpps, |
| 2162 | rates.inbps, |
| 2163 | rates.outbps); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
| 2168 | static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file) |
| 2169 | { |
| 2170 | return single_open_net(inode, file, ip_vs_stats_percpu_show); |
| 2171 | } |
| 2172 | |
| 2173 | static const struct file_operations ip_vs_stats_percpu_fops = { |
| 2174 | .owner = THIS_MODULE, |
| 2175 | .open = ip_vs_stats_percpu_seq_open, |
| 2176 | .read = seq_read, |
| 2177 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 2178 | .release = single_release_net, |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 2179 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | #endif |
| 2181 | |
| 2182 | /* |
| 2183 | * Set timeout values for tcp tcpfin udp in the timeout_table. |
| 2184 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2185 | static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
Changli Gao | 091bb34 | 2011-01-21 18:02:13 +0800 | [diff] [blame] | 2187 | #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP) |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2188 | struct ip_vs_proto_data *pd; |
Changli Gao | 091bb34 | 2011-01-21 18:02:13 +0800 | [diff] [blame] | 2189 | #endif |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n", |
| 2192 | u->tcp_timeout, |
| 2193 | u->tcp_fin_timeout, |
| 2194 | u->udp_timeout); |
| 2195 | |
| 2196 | #ifdef CONFIG_IP_VS_PROTO_TCP |
| 2197 | if (u->tcp_timeout) { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2198 | pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
| 2199 | pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | = u->tcp_timeout * HZ; |
| 2201 | } |
| 2202 | |
| 2203 | if (u->tcp_fin_timeout) { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2204 | pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
| 2205 | pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | = u->tcp_fin_timeout * HZ; |
| 2207 | } |
| 2208 | #endif |
| 2209 | |
| 2210 | #ifdef CONFIG_IP_VS_PROTO_UDP |
| 2211 | if (u->udp_timeout) { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2212 | pd = ip_vs_proto_data_get(net, IPPROTO_UDP); |
| 2213 | pd->timeout_table[IP_VS_UDP_S_NORMAL] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | = u->udp_timeout * HZ; |
| 2215 | } |
| 2216 | #endif |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | #define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL) |
| 2222 | #define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user)) |
| 2223 | #define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \ |
| 2224 | sizeof(struct ip_vs_dest_user)) |
| 2225 | #define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user)) |
| 2226 | #define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user)) |
| 2227 | #define MAX_ARG_LEN SVCDEST_ARG_LEN |
| 2228 | |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 2229 | static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN, |
| 2231 | [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN, |
| 2232 | [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN, |
| 2233 | [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0, |
| 2234 | [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN, |
| 2235 | [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN, |
| 2236 | [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN, |
| 2237 | [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN, |
| 2238 | [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN, |
| 2239 | [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN, |
| 2240 | [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN, |
| 2241 | }; |
| 2242 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2243 | static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc, |
| 2244 | struct ip_vs_service_user *usvc_compat) |
| 2245 | { |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2246 | memset(usvc, 0, sizeof(*usvc)); |
| 2247 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2248 | usvc->af = AF_INET; |
| 2249 | usvc->protocol = usvc_compat->protocol; |
| 2250 | usvc->addr.ip = usvc_compat->addr; |
| 2251 | usvc->port = usvc_compat->port; |
| 2252 | usvc->fwmark = usvc_compat->fwmark; |
| 2253 | |
| 2254 | /* Deep copy of sched_name is not needed here */ |
| 2255 | usvc->sched_name = usvc_compat->sched_name; |
| 2256 | |
| 2257 | usvc->flags = usvc_compat->flags; |
| 2258 | usvc->timeout = usvc_compat->timeout; |
| 2259 | usvc->netmask = usvc_compat->netmask; |
| 2260 | } |
| 2261 | |
| 2262 | static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest, |
| 2263 | struct ip_vs_dest_user *udest_compat) |
| 2264 | { |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2265 | memset(udest, 0, sizeof(*udest)); |
| 2266 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2267 | udest->addr.ip = udest_compat->addr; |
| 2268 | udest->port = udest_compat->port; |
| 2269 | udest->conn_flags = udest_compat->conn_flags; |
| 2270 | udest->weight = udest_compat->weight; |
| 2271 | udest->u_threshold = udest_compat->u_threshold; |
| 2272 | udest->l_threshold = udest_compat->l_threshold; |
| 2273 | } |
| 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | static int |
| 2276 | do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) |
| 2277 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2278 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | int ret; |
| 2280 | unsigned char arg[MAX_ARG_LEN]; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2281 | struct ip_vs_service_user *usvc_compat; |
| 2282 | struct ip_vs_service_user_kern usvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | struct ip_vs_service *svc; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2284 | struct ip_vs_dest_user *udest_compat; |
| 2285 | struct ip_vs_dest_user_kern udest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
| 2287 | if (!capable(CAP_NET_ADMIN)) |
| 2288 | return -EPERM; |
| 2289 | |
Arjan van de Ven | 04bcef2 | 2010-01-04 16:37:12 +0100 | [diff] [blame] | 2290 | if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX) |
| 2291 | return -EINVAL; |
| 2292 | if (len < 0 || len > MAX_ARG_LEN) |
| 2293 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | if (len != set_arglen[SET_CMDID(cmd)]) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 2295 | pr_err("set_ctl: len %u != %u\n", |
| 2296 | len, set_arglen[SET_CMDID(cmd)]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | return -EINVAL; |
| 2298 | } |
| 2299 | |
| 2300 | if (copy_from_user(arg, user, len) != 0) |
| 2301 | return -EFAULT; |
| 2302 | |
| 2303 | /* increase the module use count */ |
| 2304 | ip_vs_use_count_inc(); |
| 2305 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 2306 | if (mutex_lock_interruptible(&__ip_vs_mutex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | ret = -ERESTARTSYS; |
| 2308 | goto out_dec; |
| 2309 | } |
| 2310 | |
| 2311 | if (cmd == IP_VS_SO_SET_FLUSH) { |
| 2312 | /* Flush the virtual service */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2313 | ret = ip_vs_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | goto out_unlock; |
| 2315 | } else if (cmd == IP_VS_SO_SET_TIMEOUT) { |
| 2316 | /* Set timeout values for (tcp tcpfin udp) */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2317 | ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | goto out_unlock; |
| 2319 | } else if (cmd == IP_VS_SO_SET_STARTDAEMON) { |
| 2320 | struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2321 | ret = start_sync_thread(net, dm->state, dm->mcast_ifn, |
| 2322 | dm->syncid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | goto out_unlock; |
| 2324 | } else if (cmd == IP_VS_SO_SET_STOPDAEMON) { |
| 2325 | struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2326 | ret = stop_sync_thread(net, dm->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | goto out_unlock; |
| 2328 | } |
| 2329 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2330 | usvc_compat = (struct ip_vs_service_user *)arg; |
| 2331 | udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1); |
| 2332 | |
| 2333 | /* We only use the new structs internally, so copy userspace compat |
| 2334 | * structs to extended internal versions */ |
| 2335 | ip_vs_copy_usvc_compat(&usvc, usvc_compat); |
| 2336 | ip_vs_copy_udest_compat(&udest, udest_compat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | |
| 2338 | if (cmd == IP_VS_SO_SET_ZERO) { |
| 2339 | /* if no service address is set, zero counters in all */ |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2340 | if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2341 | ret = ip_vs_zero_all(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | goto out_unlock; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 2346 | /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */ |
| 2347 | if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP && |
| 2348 | usvc.protocol != IPPROTO_SCTP) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 2349 | pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n", |
| 2350 | usvc.protocol, &usvc.addr.ip, |
| 2351 | ntohs(usvc.port), usvc.sched_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | ret = -EFAULT; |
| 2353 | goto out_unlock; |
| 2354 | } |
| 2355 | |
| 2356 | /* Lookup the exact service by <protocol, addr, port> or fwmark */ |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2357 | if (usvc.fwmark == 0) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2358 | svc = __ip_vs_service_find(net, usvc.af, usvc.protocol, |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2359 | &usvc.addr, usvc.port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | else |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2361 | svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | |
| 2363 | if (cmd != IP_VS_SO_SET_ADD |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2364 | && (svc == NULL || svc->protocol != usvc.protocol)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | ret = -ESRCH; |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2366 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | switch (cmd) { |
| 2370 | case IP_VS_SO_SET_ADD: |
| 2371 | if (svc != NULL) |
| 2372 | ret = -EEXIST; |
| 2373 | else |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2374 | ret = ip_vs_add_service(net, &usvc, &svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | break; |
| 2376 | case IP_VS_SO_SET_EDIT: |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2377 | ret = ip_vs_edit_service(svc, &usvc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | break; |
| 2379 | case IP_VS_SO_SET_DEL: |
| 2380 | ret = ip_vs_del_service(svc); |
| 2381 | if (!ret) |
| 2382 | goto out_unlock; |
| 2383 | break; |
| 2384 | case IP_VS_SO_SET_ZERO: |
| 2385 | ret = ip_vs_zero_service(svc); |
| 2386 | break; |
| 2387 | case IP_VS_SO_SET_ADDDEST: |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2388 | ret = ip_vs_add_dest(svc, &udest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | break; |
| 2390 | case IP_VS_SO_SET_EDITDEST: |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2391 | ret = ip_vs_edit_dest(svc, &udest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | break; |
| 2393 | case IP_VS_SO_SET_DELDEST: |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2394 | ret = ip_vs_del_dest(svc, &udest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | break; |
| 2396 | default: |
| 2397 | ret = -EINVAL; |
| 2398 | } |
| 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | out_unlock: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 2401 | mutex_unlock(&__ip_vs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | out_dec: |
| 2403 | /* decrease the module use count */ |
| 2404 | ip_vs_use_count_dec(); |
| 2405 | |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src) |
| 2412 | { |
| 2413 | dst->protocol = src->protocol; |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 2414 | dst->addr = src->addr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | dst->port = src->port; |
| 2416 | dst->fwmark = src->fwmark; |
pageexec | 4da62fc | 2005-06-26 16:00:19 -0700 | [diff] [blame] | 2417 | strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | dst->flags = src->flags; |
| 2419 | dst->timeout = src->timeout / HZ; |
| 2420 | dst->netmask = src->netmask; |
| 2421 | dst->num_dests = src->num_dests; |
| 2422 | ip_vs_copy_stats(&dst->stats, &src->stats); |
| 2423 | } |
| 2424 | |
| 2425 | static inline int |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2426 | __ip_vs_get_service_entries(struct net *net, |
| 2427 | const struct ip_vs_get_services *get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | struct ip_vs_get_services __user *uptr) |
| 2429 | { |
| 2430 | int idx, count=0; |
| 2431 | struct ip_vs_service *svc; |
| 2432 | struct ip_vs_service_entry entry; |
| 2433 | int ret = 0; |
| 2434 | |
| 2435 | for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 2436 | list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) { |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2437 | /* Only expose IPv4 entries to old interface */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2438 | if (svc->af != AF_INET || !net_eq(svc->net, net)) |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2439 | continue; |
| 2440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | if (count >= get->num_services) |
| 2442 | goto out; |
pageexec | 4da62fc | 2005-06-26 16:00:19 -0700 | [diff] [blame] | 2443 | memset(&entry, 0, sizeof(entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | ip_vs_copy_service(&entry, svc); |
| 2445 | if (copy_to_user(&uptr->entrytable[count], |
| 2446 | &entry, sizeof(entry))) { |
| 2447 | ret = -EFAULT; |
| 2448 | goto out; |
| 2449 | } |
| 2450 | count++; |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 2455 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) { |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2456 | /* Only expose IPv4 entries to old interface */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2457 | if (svc->af != AF_INET || !net_eq(svc->net, net)) |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2458 | continue; |
| 2459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | if (count >= get->num_services) |
| 2461 | goto out; |
pageexec | 4da62fc | 2005-06-26 16:00:19 -0700 | [diff] [blame] | 2462 | memset(&entry, 0, sizeof(entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | ip_vs_copy_service(&entry, svc); |
| 2464 | if (copy_to_user(&uptr->entrytable[count], |
| 2465 | &entry, sizeof(entry))) { |
| 2466 | ret = -EFAULT; |
| 2467 | goto out; |
| 2468 | } |
| 2469 | count++; |
| 2470 | } |
| 2471 | } |
| 2472 | out: |
| 2473 | return ret; |
| 2474 | } |
| 2475 | |
| 2476 | static inline int |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2477 | __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | struct ip_vs_get_dests __user *uptr) |
| 2479 | { |
| 2480 | struct ip_vs_service *svc; |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 2481 | union nf_inet_addr addr = { .ip = get->addr }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | int ret = 0; |
| 2483 | |
| 2484 | if (get->fwmark) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2485 | svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | else |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2487 | svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr, |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2488 | get->port); |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 2489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | if (svc) { |
| 2491 | int count = 0; |
| 2492 | struct ip_vs_dest *dest; |
| 2493 | struct ip_vs_dest_entry entry; |
| 2494 | |
| 2495 | list_for_each_entry(dest, &svc->destinations, n_list) { |
| 2496 | if (count >= get->num_dests) |
| 2497 | break; |
| 2498 | |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 2499 | entry.addr = dest->addr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | entry.port = dest->port; |
| 2501 | entry.conn_flags = atomic_read(&dest->conn_flags); |
| 2502 | entry.weight = atomic_read(&dest->weight); |
| 2503 | entry.u_threshold = dest->u_threshold; |
| 2504 | entry.l_threshold = dest->l_threshold; |
| 2505 | entry.activeconns = atomic_read(&dest->activeconns); |
| 2506 | entry.inactconns = atomic_read(&dest->inactconns); |
| 2507 | entry.persistconns = atomic_read(&dest->persistconns); |
| 2508 | ip_vs_copy_stats(&entry.stats, &dest->stats); |
| 2509 | if (copy_to_user(&uptr->entrytable[count], |
| 2510 | &entry, sizeof(entry))) { |
| 2511 | ret = -EFAULT; |
| 2512 | break; |
| 2513 | } |
| 2514 | count++; |
| 2515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | } else |
| 2517 | ret = -ESRCH; |
| 2518 | return ret; |
| 2519 | } |
| 2520 | |
| 2521 | static inline void |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2522 | __ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | { |
Changli Gao | 091bb34 | 2011-01-21 18:02:13 +0800 | [diff] [blame] | 2524 | #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP) |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2525 | struct ip_vs_proto_data *pd; |
Changli Gao | 091bb34 | 2011-01-21 18:02:13 +0800 | [diff] [blame] | 2526 | #endif |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | #ifdef CONFIG_IP_VS_PROTO_TCP |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2529 | pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
| 2530 | u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ; |
| 2531 | u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | #endif |
| 2533 | #ifdef CONFIG_IP_VS_PROTO_UDP |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2534 | pd = ip_vs_proto_data_get(net, IPPROTO_UDP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | u->udp_timeout = |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2536 | pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | #endif |
| 2538 | } |
| 2539 | |
| 2540 | |
| 2541 | #define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL) |
| 2542 | #define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo)) |
| 2543 | #define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services)) |
| 2544 | #define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry)) |
| 2545 | #define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests)) |
| 2546 | #define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user)) |
| 2547 | #define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2) |
| 2548 | |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 2549 | static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64, |
| 2551 | [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN, |
| 2552 | [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN, |
| 2553 | [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN, |
| 2554 | [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN, |
| 2555 | [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN, |
| 2556 | [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN, |
| 2557 | }; |
| 2558 | |
| 2559 | static int |
| 2560 | do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) |
| 2561 | { |
| 2562 | unsigned char arg[128]; |
| 2563 | int ret = 0; |
Arjan van de Ven | 04bcef2 | 2010-01-04 16:37:12 +0100 | [diff] [blame] | 2564 | unsigned int copylen; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2565 | struct net *net = sock_net(sk); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2566 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2568 | BUG_ON(!net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | if (!capable(CAP_NET_ADMIN)) |
| 2570 | return -EPERM; |
| 2571 | |
Arjan van de Ven | 04bcef2 | 2010-01-04 16:37:12 +0100 | [diff] [blame] | 2572 | if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX) |
| 2573 | return -EINVAL; |
| 2574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | if (*len < get_arglen[GET_CMDID(cmd)]) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 2576 | pr_err("get_ctl: len %u < %u\n", |
| 2577 | *len, get_arglen[GET_CMDID(cmd)]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | return -EINVAL; |
| 2579 | } |
| 2580 | |
Arjan van de Ven | 04bcef2 | 2010-01-04 16:37:12 +0100 | [diff] [blame] | 2581 | copylen = get_arglen[GET_CMDID(cmd)]; |
| 2582 | if (copylen > 128) |
| 2583 | return -EINVAL; |
| 2584 | |
| 2585 | if (copy_from_user(arg, user, copylen) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | return -EFAULT; |
| 2587 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 2588 | if (mutex_lock_interruptible(&__ip_vs_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | return -ERESTARTSYS; |
| 2590 | |
| 2591 | switch (cmd) { |
| 2592 | case IP_VS_SO_GET_VERSION: |
| 2593 | { |
| 2594 | char buf[64]; |
| 2595 | |
| 2596 | sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)", |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 2597 | NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | if (copy_to_user(user, buf, strlen(buf)+1) != 0) { |
| 2599 | ret = -EFAULT; |
| 2600 | goto out; |
| 2601 | } |
| 2602 | *len = strlen(buf)+1; |
| 2603 | } |
| 2604 | break; |
| 2605 | |
| 2606 | case IP_VS_SO_GET_INFO: |
| 2607 | { |
| 2608 | struct ip_vs_getinfo info; |
| 2609 | info.version = IP_VS_VERSION_CODE; |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 2610 | info.size = ip_vs_conn_tab_size; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 2611 | info.num_services = ipvs->num_services; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | if (copy_to_user(user, &info, sizeof(info)) != 0) |
| 2613 | ret = -EFAULT; |
| 2614 | } |
| 2615 | break; |
| 2616 | |
| 2617 | case IP_VS_SO_GET_SERVICES: |
| 2618 | { |
| 2619 | struct ip_vs_get_services *get; |
| 2620 | int size; |
| 2621 | |
| 2622 | get = (struct ip_vs_get_services *)arg; |
| 2623 | size = sizeof(*get) + |
| 2624 | sizeof(struct ip_vs_service_entry) * get->num_services; |
| 2625 | if (*len != size) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 2626 | pr_err("length: %u != %u\n", *len, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | ret = -EINVAL; |
| 2628 | goto out; |
| 2629 | } |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2630 | ret = __ip_vs_get_service_entries(net, get, user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | } |
| 2632 | break; |
| 2633 | |
| 2634 | case IP_VS_SO_GET_SERVICE: |
| 2635 | { |
| 2636 | struct ip_vs_service_entry *entry; |
| 2637 | struct ip_vs_service *svc; |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 2638 | union nf_inet_addr addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | |
| 2640 | entry = (struct ip_vs_service_entry *)arg; |
Julius Volz | b18610d | 2008-09-02 15:55:37 +0200 | [diff] [blame] | 2641 | addr.ip = entry->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | if (entry->fwmark) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2643 | svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | else |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2645 | svc = __ip_vs_service_find(net, AF_INET, |
| 2646 | entry->protocol, &addr, |
| 2647 | entry->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | if (svc) { |
| 2649 | ip_vs_copy_service(entry, svc); |
| 2650 | if (copy_to_user(user, entry, sizeof(*entry)) != 0) |
| 2651 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | } else |
| 2653 | ret = -ESRCH; |
| 2654 | } |
| 2655 | break; |
| 2656 | |
| 2657 | case IP_VS_SO_GET_DESTS: |
| 2658 | { |
| 2659 | struct ip_vs_get_dests *get; |
| 2660 | int size; |
| 2661 | |
| 2662 | get = (struct ip_vs_get_dests *)arg; |
| 2663 | size = sizeof(*get) + |
| 2664 | sizeof(struct ip_vs_dest_entry) * get->num_dests; |
| 2665 | if (*len != size) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 2666 | pr_err("length: %u != %u\n", *len, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | ret = -EINVAL; |
| 2668 | goto out; |
| 2669 | } |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2670 | ret = __ip_vs_get_dest_entries(net, get, user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | } |
| 2672 | break; |
| 2673 | |
| 2674 | case IP_VS_SO_GET_TIMEOUT: |
| 2675 | { |
| 2676 | struct ip_vs_timeout_user t; |
| 2677 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 2678 | __ip_vs_get_timeouts(net, &t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | if (copy_to_user(user, &t, sizeof(t)) != 0) |
| 2680 | ret = -EFAULT; |
| 2681 | } |
| 2682 | break; |
| 2683 | |
| 2684 | case IP_VS_SO_GET_DAEMON: |
| 2685 | { |
| 2686 | struct ip_vs_daemon_user d[2]; |
| 2687 | |
| 2688 | memset(&d, 0, sizeof(d)); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2689 | if (ipvs->sync_state & IP_VS_STATE_MASTER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | d[0].state = IP_VS_STATE_MASTER; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2691 | strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn, |
| 2692 | sizeof(d[0].mcast_ifn)); |
| 2693 | d[0].syncid = ipvs->master_syncid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | } |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2695 | if (ipvs->sync_state & IP_VS_STATE_BACKUP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | d[1].state = IP_VS_STATE_BACKUP; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 2697 | strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn, |
| 2698 | sizeof(d[1].mcast_ifn)); |
| 2699 | d[1].syncid = ipvs->backup_syncid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | } |
| 2701 | if (copy_to_user(user, &d, sizeof(d)) != 0) |
| 2702 | ret = -EFAULT; |
| 2703 | } |
| 2704 | break; |
| 2705 | |
| 2706 | default: |
| 2707 | ret = -EINVAL; |
| 2708 | } |
| 2709 | |
| 2710 | out: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 2711 | mutex_unlock(&__ip_vs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | return ret; |
| 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | static struct nf_sockopt_ops ip_vs_sockopts = { |
| 2717 | .pf = PF_INET, |
| 2718 | .set_optmin = IP_VS_BASE_CTL, |
| 2719 | .set_optmax = IP_VS_SO_SET_MAX+1, |
| 2720 | .set = do_ip_vs_set_ctl, |
| 2721 | .get_optmin = IP_VS_BASE_CTL, |
| 2722 | .get_optmax = IP_VS_SO_GET_MAX+1, |
| 2723 | .get = do_ip_vs_get_ctl, |
Neil Horman | 16fcec3 | 2007-09-11 11:28:26 +0200 | [diff] [blame] | 2724 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | }; |
| 2726 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2727 | /* |
| 2728 | * Generic Netlink interface |
| 2729 | */ |
| 2730 | |
| 2731 | /* IPVS genetlink family */ |
| 2732 | static struct genl_family ip_vs_genl_family = { |
| 2733 | .id = GENL_ID_GENERATE, |
| 2734 | .hdrsize = 0, |
| 2735 | .name = IPVS_GENL_NAME, |
| 2736 | .version = IPVS_GENL_VERSION, |
| 2737 | .maxattr = IPVS_CMD_MAX, |
Hans Schillstrom | c6d2d44 | 2011-01-03 14:45:03 +0100 | [diff] [blame] | 2738 | .netnsok = true, /* Make ipvsadm to work on netns */ |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2739 | }; |
| 2740 | |
| 2741 | /* Policy used for first-level command attributes */ |
| 2742 | static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = { |
| 2743 | [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED }, |
| 2744 | [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED }, |
| 2745 | [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED }, |
| 2746 | [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 }, |
| 2747 | [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 }, |
| 2748 | [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 }, |
| 2749 | }; |
| 2750 | |
| 2751 | /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */ |
| 2752 | static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = { |
| 2753 | [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 }, |
| 2754 | [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING, |
| 2755 | .len = IP_VS_IFNAME_MAXLEN }, |
| 2756 | [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 }, |
| 2757 | }; |
| 2758 | |
| 2759 | /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */ |
| 2760 | static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = { |
| 2761 | [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 }, |
| 2762 | [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 }, |
| 2763 | [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY, |
| 2764 | .len = sizeof(union nf_inet_addr) }, |
| 2765 | [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 }, |
| 2766 | [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 }, |
| 2767 | [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING, |
| 2768 | .len = IP_VS_SCHEDNAME_MAXLEN }, |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2769 | [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING, |
| 2770 | .len = IP_VS_PENAME_MAXLEN }, |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2771 | [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY, |
| 2772 | .len = sizeof(struct ip_vs_flags) }, |
| 2773 | [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 2774 | [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 }, |
| 2775 | [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED }, |
| 2776 | }; |
| 2777 | |
| 2778 | /* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */ |
| 2779 | static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = { |
| 2780 | [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY, |
| 2781 | .len = sizeof(union nf_inet_addr) }, |
| 2782 | [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 }, |
| 2783 | [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 }, |
| 2784 | [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 }, |
| 2785 | [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 }, |
| 2786 | [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 }, |
| 2787 | [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 }, |
| 2788 | [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 }, |
| 2789 | [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 }, |
| 2790 | [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED }, |
| 2791 | }; |
| 2792 | |
| 2793 | static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type, |
| 2794 | struct ip_vs_stats *stats) |
| 2795 | { |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2796 | struct ip_vs_stats_user ustats; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2797 | struct nlattr *nl_stats = nla_nest_start(skb, container_type); |
| 2798 | if (!nl_stats) |
| 2799 | return -EMSGSIZE; |
| 2800 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2801 | ip_vs_copy_stats(&ustats, stats); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2802 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 2803 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns); |
| 2804 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts); |
| 2805 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts); |
| 2806 | NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes); |
| 2807 | NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes); |
| 2808 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, ustats.cps); |
| 2809 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps); |
| 2810 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps); |
| 2811 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps); |
| 2812 | NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2813 | |
| 2814 | nla_nest_end(skb, nl_stats); |
| 2815 | |
| 2816 | return 0; |
| 2817 | |
| 2818 | nla_put_failure: |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2819 | nla_nest_cancel(skb, nl_stats); |
| 2820 | return -EMSGSIZE; |
| 2821 | } |
| 2822 | |
| 2823 | static int ip_vs_genl_fill_service(struct sk_buff *skb, |
| 2824 | struct ip_vs_service *svc) |
| 2825 | { |
| 2826 | struct nlattr *nl_service; |
| 2827 | struct ip_vs_flags flags = { .flags = svc->flags, |
| 2828 | .mask = ~0 }; |
| 2829 | |
| 2830 | nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE); |
| 2831 | if (!nl_service) |
| 2832 | return -EMSGSIZE; |
| 2833 | |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2834 | NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2835 | |
| 2836 | if (svc->fwmark) { |
| 2837 | NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark); |
| 2838 | } else { |
| 2839 | NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol); |
| 2840 | NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr); |
| 2841 | NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port); |
| 2842 | } |
| 2843 | |
| 2844 | NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2845 | if (svc->pe) |
| 2846 | NLA_PUT_STRING(skb, IPVS_SVC_ATTR_PE_NAME, svc->pe->name); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2847 | NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags); |
| 2848 | NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ); |
| 2849 | NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask); |
| 2850 | |
| 2851 | if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats)) |
| 2852 | goto nla_put_failure; |
| 2853 | |
| 2854 | nla_nest_end(skb, nl_service); |
| 2855 | |
| 2856 | return 0; |
| 2857 | |
| 2858 | nla_put_failure: |
| 2859 | nla_nest_cancel(skb, nl_service); |
| 2860 | return -EMSGSIZE; |
| 2861 | } |
| 2862 | |
| 2863 | static int ip_vs_genl_dump_service(struct sk_buff *skb, |
| 2864 | struct ip_vs_service *svc, |
| 2865 | struct netlink_callback *cb) |
| 2866 | { |
| 2867 | void *hdr; |
| 2868 | |
| 2869 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
| 2870 | &ip_vs_genl_family, NLM_F_MULTI, |
| 2871 | IPVS_CMD_NEW_SERVICE); |
| 2872 | if (!hdr) |
| 2873 | return -EMSGSIZE; |
| 2874 | |
| 2875 | if (ip_vs_genl_fill_service(skb, svc) < 0) |
| 2876 | goto nla_put_failure; |
| 2877 | |
| 2878 | return genlmsg_end(skb, hdr); |
| 2879 | |
| 2880 | nla_put_failure: |
| 2881 | genlmsg_cancel(skb, hdr); |
| 2882 | return -EMSGSIZE; |
| 2883 | } |
| 2884 | |
| 2885 | static int ip_vs_genl_dump_services(struct sk_buff *skb, |
| 2886 | struct netlink_callback *cb) |
| 2887 | { |
| 2888 | int idx = 0, i; |
| 2889 | int start = cb->args[0]; |
| 2890 | struct ip_vs_service *svc; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2891 | struct net *net = skb_sknet(skb); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2892 | |
| 2893 | mutex_lock(&__ip_vs_mutex); |
| 2894 | for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) { |
| 2895 | list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2896 | if (++idx <= start || !net_eq(svc->net, net)) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2897 | continue; |
| 2898 | if (ip_vs_genl_dump_service(skb, svc, cb) < 0) { |
| 2899 | idx--; |
| 2900 | goto nla_put_failure; |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) { |
| 2906 | list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2907 | if (++idx <= start || !net_eq(svc->net, net)) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2908 | continue; |
| 2909 | if (ip_vs_genl_dump_service(skb, svc, cb) < 0) { |
| 2910 | idx--; |
| 2911 | goto nla_put_failure; |
| 2912 | } |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | nla_put_failure: |
| 2917 | mutex_unlock(&__ip_vs_mutex); |
| 2918 | cb->args[0] = idx; |
| 2919 | |
| 2920 | return skb->len; |
| 2921 | } |
| 2922 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2923 | static int ip_vs_genl_parse_service(struct net *net, |
| 2924 | struct ip_vs_service_user_kern *usvc, |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2925 | struct nlattr *nla, int full_entry, |
| 2926 | struct ip_vs_service **ret_svc) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2927 | { |
| 2928 | struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1]; |
| 2929 | struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr; |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2930 | struct ip_vs_service *svc; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2931 | |
| 2932 | /* Parse mandatory identifying service fields first */ |
| 2933 | if (nla == NULL || |
| 2934 | nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy)) |
| 2935 | return -EINVAL; |
| 2936 | |
| 2937 | nla_af = attrs[IPVS_SVC_ATTR_AF]; |
| 2938 | nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL]; |
| 2939 | nla_addr = attrs[IPVS_SVC_ATTR_ADDR]; |
| 2940 | nla_port = attrs[IPVS_SVC_ATTR_PORT]; |
| 2941 | nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK]; |
| 2942 | |
| 2943 | if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr)))) |
| 2944 | return -EINVAL; |
| 2945 | |
Simon Horman | 258c889 | 2009-12-15 17:01:25 +0100 | [diff] [blame] | 2946 | memset(usvc, 0, sizeof(*usvc)); |
| 2947 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2948 | usvc->af = nla_get_u16(nla_af); |
Julius Volz | f94fd04 | 2008-09-02 15:55:55 +0200 | [diff] [blame] | 2949 | #ifdef CONFIG_IP_VS_IPV6 |
| 2950 | if (usvc->af != AF_INET && usvc->af != AF_INET6) |
| 2951 | #else |
| 2952 | if (usvc->af != AF_INET) |
| 2953 | #endif |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2954 | return -EAFNOSUPPORT; |
| 2955 | |
| 2956 | if (nla_fwmark) { |
| 2957 | usvc->protocol = IPPROTO_TCP; |
| 2958 | usvc->fwmark = nla_get_u32(nla_fwmark); |
| 2959 | } else { |
| 2960 | usvc->protocol = nla_get_u16(nla_protocol); |
| 2961 | nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr)); |
| 2962 | usvc->port = nla_get_u16(nla_port); |
| 2963 | usvc->fwmark = 0; |
| 2964 | } |
| 2965 | |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2966 | if (usvc->fwmark) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2967 | svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2968 | else |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 2969 | svc = __ip_vs_service_find(net, usvc->af, usvc->protocol, |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2970 | &usvc->addr, usvc->port); |
| 2971 | *ret_svc = svc; |
| 2972 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2973 | /* If a full entry was requested, check for the additional fields */ |
| 2974 | if (full_entry) { |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2975 | struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout, |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2976 | *nla_netmask; |
| 2977 | struct ip_vs_flags flags; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2978 | |
| 2979 | nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME]; |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2980 | nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME]; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2981 | nla_flags = attrs[IPVS_SVC_ATTR_FLAGS]; |
| 2982 | nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT]; |
| 2983 | nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK]; |
| 2984 | |
| 2985 | if (!(nla_sched && nla_flags && nla_timeout && nla_netmask)) |
| 2986 | return -EINVAL; |
| 2987 | |
| 2988 | nla_memcpy(&flags, nla_flags, sizeof(flags)); |
| 2989 | |
| 2990 | /* prefill flags from service if it already exists */ |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 2991 | if (svc) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2992 | usvc->flags = svc->flags; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2993 | |
| 2994 | /* set new flags from userland */ |
| 2995 | usvc->flags = (usvc->flags & ~flags.mask) | |
| 2996 | (flags.flags & flags.mask); |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 2997 | usvc->sched_name = nla_data(nla_sched); |
Simon Horman | 0d1e71b | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 2998 | usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 2999 | usvc->timeout = nla_get_u32(nla_timeout); |
| 3000 | usvc->netmask = nla_get_u32(nla_netmask); |
| 3001 | } |
| 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3006 | static struct ip_vs_service *ip_vs_genl_find_service(struct net *net, |
| 3007 | struct nlattr *nla) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3008 | { |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 3009 | struct ip_vs_service_user_kern usvc; |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 3010 | struct ip_vs_service *svc; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3011 | int ret; |
| 3012 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3013 | ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 3014 | return ret ? ERR_PTR(ret) : svc; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest) |
| 3018 | { |
| 3019 | struct nlattr *nl_dest; |
| 3020 | |
| 3021 | nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST); |
| 3022 | if (!nl_dest) |
| 3023 | return -EMSGSIZE; |
| 3024 | |
| 3025 | NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr); |
| 3026 | NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port); |
| 3027 | |
| 3028 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD, |
| 3029 | atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK); |
| 3030 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight)); |
| 3031 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold); |
| 3032 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold); |
| 3033 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS, |
| 3034 | atomic_read(&dest->activeconns)); |
| 3035 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS, |
| 3036 | atomic_read(&dest->inactconns)); |
| 3037 | NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS, |
| 3038 | atomic_read(&dest->persistconns)); |
| 3039 | |
| 3040 | if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats)) |
| 3041 | goto nla_put_failure; |
| 3042 | |
| 3043 | nla_nest_end(skb, nl_dest); |
| 3044 | |
| 3045 | return 0; |
| 3046 | |
| 3047 | nla_put_failure: |
| 3048 | nla_nest_cancel(skb, nl_dest); |
| 3049 | return -EMSGSIZE; |
| 3050 | } |
| 3051 | |
| 3052 | static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest, |
| 3053 | struct netlink_callback *cb) |
| 3054 | { |
| 3055 | void *hdr; |
| 3056 | |
| 3057 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
| 3058 | &ip_vs_genl_family, NLM_F_MULTI, |
| 3059 | IPVS_CMD_NEW_DEST); |
| 3060 | if (!hdr) |
| 3061 | return -EMSGSIZE; |
| 3062 | |
| 3063 | if (ip_vs_genl_fill_dest(skb, dest) < 0) |
| 3064 | goto nla_put_failure; |
| 3065 | |
| 3066 | return genlmsg_end(skb, hdr); |
| 3067 | |
| 3068 | nla_put_failure: |
| 3069 | genlmsg_cancel(skb, hdr); |
| 3070 | return -EMSGSIZE; |
| 3071 | } |
| 3072 | |
| 3073 | static int ip_vs_genl_dump_dests(struct sk_buff *skb, |
| 3074 | struct netlink_callback *cb) |
| 3075 | { |
| 3076 | int idx = 0; |
| 3077 | int start = cb->args[0]; |
| 3078 | struct ip_vs_service *svc; |
| 3079 | struct ip_vs_dest *dest; |
| 3080 | struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1]; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3081 | struct net *net = skb_sknet(skb); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3082 | |
| 3083 | mutex_lock(&__ip_vs_mutex); |
| 3084 | |
| 3085 | /* Try to find the service for which to dump destinations */ |
| 3086 | if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, |
| 3087 | IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy)) |
| 3088 | goto out_err; |
| 3089 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3090 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3091 | svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3092 | if (IS_ERR(svc) || svc == NULL) |
| 3093 | goto out_err; |
| 3094 | |
| 3095 | /* Dump the destinations */ |
| 3096 | list_for_each_entry(dest, &svc->destinations, n_list) { |
| 3097 | if (++idx <= start) |
| 3098 | continue; |
| 3099 | if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) { |
| 3100 | idx--; |
| 3101 | goto nla_put_failure; |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | nla_put_failure: |
| 3106 | cb->args[0] = idx; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3107 | |
| 3108 | out_err: |
| 3109 | mutex_unlock(&__ip_vs_mutex); |
| 3110 | |
| 3111 | return skb->len; |
| 3112 | } |
| 3113 | |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 3114 | static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest, |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3115 | struct nlattr *nla, int full_entry) |
| 3116 | { |
| 3117 | struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1]; |
| 3118 | struct nlattr *nla_addr, *nla_port; |
| 3119 | |
| 3120 | /* Parse mandatory identifying destination fields first */ |
| 3121 | if (nla == NULL || |
| 3122 | nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy)) |
| 3123 | return -EINVAL; |
| 3124 | |
| 3125 | nla_addr = attrs[IPVS_DEST_ATTR_ADDR]; |
| 3126 | nla_port = attrs[IPVS_DEST_ATTR_PORT]; |
| 3127 | |
| 3128 | if (!(nla_addr && nla_port)) |
| 3129 | return -EINVAL; |
| 3130 | |
Simon Horman | 258c889 | 2009-12-15 17:01:25 +0100 | [diff] [blame] | 3131 | memset(udest, 0, sizeof(*udest)); |
| 3132 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3133 | nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr)); |
| 3134 | udest->port = nla_get_u16(nla_port); |
| 3135 | |
| 3136 | /* If a full entry was requested, check for the additional fields */ |
| 3137 | if (full_entry) { |
| 3138 | struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh, |
| 3139 | *nla_l_thresh; |
| 3140 | |
| 3141 | nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD]; |
| 3142 | nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT]; |
| 3143 | nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH]; |
| 3144 | nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH]; |
| 3145 | |
| 3146 | if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh)) |
| 3147 | return -EINVAL; |
| 3148 | |
| 3149 | udest->conn_flags = nla_get_u32(nla_fwd) |
| 3150 | & IP_VS_CONN_F_FWD_MASK; |
| 3151 | udest->weight = nla_get_u32(nla_weight); |
| 3152 | udest->u_threshold = nla_get_u32(nla_u_thresh); |
| 3153 | udest->l_threshold = nla_get_u32(nla_l_thresh); |
| 3154 | } |
| 3155 | |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state, |
| 3160 | const char *mcast_ifn, __be32 syncid) |
| 3161 | { |
| 3162 | struct nlattr *nl_daemon; |
| 3163 | |
| 3164 | nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON); |
| 3165 | if (!nl_daemon) |
| 3166 | return -EMSGSIZE; |
| 3167 | |
| 3168 | NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state); |
| 3169 | NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn); |
| 3170 | NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid); |
| 3171 | |
| 3172 | nla_nest_end(skb, nl_daemon); |
| 3173 | |
| 3174 | return 0; |
| 3175 | |
| 3176 | nla_put_failure: |
| 3177 | nla_nest_cancel(skb, nl_daemon); |
| 3178 | return -EMSGSIZE; |
| 3179 | } |
| 3180 | |
| 3181 | static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state, |
| 3182 | const char *mcast_ifn, __be32 syncid, |
| 3183 | struct netlink_callback *cb) |
| 3184 | { |
| 3185 | void *hdr; |
| 3186 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
| 3187 | &ip_vs_genl_family, NLM_F_MULTI, |
| 3188 | IPVS_CMD_NEW_DAEMON); |
| 3189 | if (!hdr) |
| 3190 | return -EMSGSIZE; |
| 3191 | |
| 3192 | if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid)) |
| 3193 | goto nla_put_failure; |
| 3194 | |
| 3195 | return genlmsg_end(skb, hdr); |
| 3196 | |
| 3197 | nla_put_failure: |
| 3198 | genlmsg_cancel(skb, hdr); |
| 3199 | return -EMSGSIZE; |
| 3200 | } |
| 3201 | |
| 3202 | static int ip_vs_genl_dump_daemons(struct sk_buff *skb, |
| 3203 | struct netlink_callback *cb) |
| 3204 | { |
Hans Schillstrom | a09d197 | 2011-04-04 15:25:18 +0200 | [diff] [blame] | 3205 | struct net *net = skb_sknet(skb); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3206 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 3207 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3208 | mutex_lock(&__ip_vs_mutex); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3209 | if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) { |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3210 | if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER, |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3211 | ipvs->master_mcast_ifn, |
| 3212 | ipvs->master_syncid, cb) < 0) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3213 | goto nla_put_failure; |
| 3214 | |
| 3215 | cb->args[0] = 1; |
| 3216 | } |
| 3217 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3218 | if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) { |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3219 | if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP, |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3220 | ipvs->backup_mcast_ifn, |
| 3221 | ipvs->backup_syncid, cb) < 0) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3222 | goto nla_put_failure; |
| 3223 | |
| 3224 | cb->args[1] = 1; |
| 3225 | } |
| 3226 | |
| 3227 | nla_put_failure: |
| 3228 | mutex_unlock(&__ip_vs_mutex); |
| 3229 | |
| 3230 | return skb->len; |
| 3231 | } |
| 3232 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3233 | static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3234 | { |
| 3235 | if (!(attrs[IPVS_DAEMON_ATTR_STATE] && |
| 3236 | attrs[IPVS_DAEMON_ATTR_MCAST_IFN] && |
| 3237 | attrs[IPVS_DAEMON_ATTR_SYNC_ID])) |
| 3238 | return -EINVAL; |
| 3239 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3240 | return start_sync_thread(net, |
| 3241 | nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]), |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3242 | nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]), |
| 3243 | nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID])); |
| 3244 | } |
| 3245 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3246 | static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3247 | { |
| 3248 | if (!attrs[IPVS_DAEMON_ATTR_STATE]) |
| 3249 | return -EINVAL; |
| 3250 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3251 | return stop_sync_thread(net, |
| 3252 | nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE])); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3253 | } |
| 3254 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 3255 | static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs) |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3256 | { |
| 3257 | struct ip_vs_timeout_user t; |
| 3258 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 3259 | __ip_vs_get_timeouts(net, &t); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3260 | |
| 3261 | if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]) |
| 3262 | t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]); |
| 3263 | |
| 3264 | if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]) |
| 3265 | t.tcp_fin_timeout = |
| 3266 | nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]); |
| 3267 | |
| 3268 | if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]) |
| 3269 | t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]); |
| 3270 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 3271 | return ip_vs_set_timeout(net, &t); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info) |
| 3275 | { |
| 3276 | struct ip_vs_service *svc = NULL; |
Julius Volz | c860c6b | 2008-09-02 15:55:36 +0200 | [diff] [blame] | 3277 | struct ip_vs_service_user_kern usvc; |
| 3278 | struct ip_vs_dest_user_kern udest; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3279 | int ret = 0, cmd; |
| 3280 | int need_full_svc = 0, need_full_dest = 0; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3281 | struct net *net; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3282 | struct netns_ipvs *ipvs; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3283 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3284 | net = skb_sknet(skb); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3285 | ipvs = net_ipvs(net); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3286 | cmd = info->genlhdr->cmd; |
| 3287 | |
| 3288 | mutex_lock(&__ip_vs_mutex); |
| 3289 | |
| 3290 | if (cmd == IPVS_CMD_FLUSH) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3291 | ret = ip_vs_flush(net); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3292 | goto out; |
| 3293 | } else if (cmd == IPVS_CMD_SET_CONFIG) { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 3294 | ret = ip_vs_genl_set_config(net, info->attrs); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3295 | goto out; |
| 3296 | } else if (cmd == IPVS_CMD_NEW_DAEMON || |
| 3297 | cmd == IPVS_CMD_DEL_DAEMON) { |
| 3298 | |
| 3299 | struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1]; |
| 3300 | |
| 3301 | if (!info->attrs[IPVS_CMD_ATTR_DAEMON] || |
| 3302 | nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX, |
| 3303 | info->attrs[IPVS_CMD_ATTR_DAEMON], |
| 3304 | ip_vs_daemon_policy)) { |
| 3305 | ret = -EINVAL; |
| 3306 | goto out; |
| 3307 | } |
| 3308 | |
| 3309 | if (cmd == IPVS_CMD_NEW_DAEMON) |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3310 | ret = ip_vs_genl_new_daemon(net, daemon_attrs); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3311 | else |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 3312 | ret = ip_vs_genl_del_daemon(net, daemon_attrs); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3313 | goto out; |
| 3314 | } else if (cmd == IPVS_CMD_ZERO && |
| 3315 | !info->attrs[IPVS_CMD_ATTR_SERVICE]) { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3316 | ret = ip_vs_zero_all(net); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3317 | goto out; |
| 3318 | } |
| 3319 | |
| 3320 | /* All following commands require a service argument, so check if we |
| 3321 | * received a valid one. We need a full service specification when |
| 3322 | * adding / editing a service. Only identifying members otherwise. */ |
| 3323 | if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE) |
| 3324 | need_full_svc = 1; |
| 3325 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3326 | ret = ip_vs_genl_parse_service(net, &usvc, |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3327 | info->attrs[IPVS_CMD_ATTR_SERVICE], |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 3328 | need_full_svc, &svc); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3329 | if (ret) |
| 3330 | goto out; |
| 3331 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3332 | /* Unless we're adding a new service, the service must already exist */ |
| 3333 | if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) { |
| 3334 | ret = -ESRCH; |
| 3335 | goto out; |
| 3336 | } |
| 3337 | |
| 3338 | /* Destination commands require a valid destination argument. For |
| 3339 | * adding / editing a destination, we need a full destination |
| 3340 | * specification. */ |
| 3341 | if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST || |
| 3342 | cmd == IPVS_CMD_DEL_DEST) { |
| 3343 | if (cmd != IPVS_CMD_DEL_DEST) |
| 3344 | need_full_dest = 1; |
| 3345 | |
| 3346 | ret = ip_vs_genl_parse_dest(&udest, |
| 3347 | info->attrs[IPVS_CMD_ATTR_DEST], |
| 3348 | need_full_dest); |
| 3349 | if (ret) |
| 3350 | goto out; |
| 3351 | } |
| 3352 | |
| 3353 | switch (cmd) { |
| 3354 | case IPVS_CMD_NEW_SERVICE: |
| 3355 | if (svc == NULL) |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3356 | ret = ip_vs_add_service(net, &usvc, &svc); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3357 | else |
| 3358 | ret = -EEXIST; |
| 3359 | break; |
| 3360 | case IPVS_CMD_SET_SERVICE: |
| 3361 | ret = ip_vs_edit_service(svc, &usvc); |
| 3362 | break; |
| 3363 | case IPVS_CMD_DEL_SERVICE: |
| 3364 | ret = ip_vs_del_service(svc); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 3365 | /* do not use svc, it can be freed */ |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3366 | break; |
| 3367 | case IPVS_CMD_NEW_DEST: |
| 3368 | ret = ip_vs_add_dest(svc, &udest); |
| 3369 | break; |
| 3370 | case IPVS_CMD_SET_DEST: |
| 3371 | ret = ip_vs_edit_dest(svc, &udest); |
| 3372 | break; |
| 3373 | case IPVS_CMD_DEL_DEST: |
| 3374 | ret = ip_vs_del_dest(svc, &udest); |
| 3375 | break; |
| 3376 | case IPVS_CMD_ZERO: |
| 3377 | ret = ip_vs_zero_service(svc); |
| 3378 | break; |
| 3379 | default: |
| 3380 | ret = -EINVAL; |
| 3381 | } |
| 3382 | |
| 3383 | out: |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3384 | mutex_unlock(&__ip_vs_mutex); |
| 3385 | |
| 3386 | return ret; |
| 3387 | } |
| 3388 | |
| 3389 | static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info) |
| 3390 | { |
| 3391 | struct sk_buff *msg; |
| 3392 | void *reply; |
| 3393 | int ret, cmd, reply_cmd; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3394 | struct net *net; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3395 | struct netns_ipvs *ipvs; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3396 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3397 | net = skb_sknet(skb); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3398 | ipvs = net_ipvs(net); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3399 | cmd = info->genlhdr->cmd; |
| 3400 | |
| 3401 | if (cmd == IPVS_CMD_GET_SERVICE) |
| 3402 | reply_cmd = IPVS_CMD_NEW_SERVICE; |
| 3403 | else if (cmd == IPVS_CMD_GET_INFO) |
| 3404 | reply_cmd = IPVS_CMD_SET_INFO; |
| 3405 | else if (cmd == IPVS_CMD_GET_CONFIG) |
| 3406 | reply_cmd = IPVS_CMD_SET_CONFIG; |
| 3407 | else { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 3408 | pr_err("unknown Generic Netlink command\n"); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3409 | return -EINVAL; |
| 3410 | } |
| 3411 | |
| 3412 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 3413 | if (!msg) |
| 3414 | return -ENOMEM; |
| 3415 | |
| 3416 | mutex_lock(&__ip_vs_mutex); |
| 3417 | |
| 3418 | reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd); |
| 3419 | if (reply == NULL) |
| 3420 | goto nla_put_failure; |
| 3421 | |
| 3422 | switch (cmd) { |
| 3423 | case IPVS_CMD_GET_SERVICE: |
| 3424 | { |
| 3425 | struct ip_vs_service *svc; |
| 3426 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3427 | svc = ip_vs_genl_find_service(net, |
| 3428 | info->attrs[IPVS_CMD_ATTR_SERVICE]); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3429 | if (IS_ERR(svc)) { |
| 3430 | ret = PTR_ERR(svc); |
| 3431 | goto out_err; |
| 3432 | } else if (svc) { |
| 3433 | ret = ip_vs_genl_fill_service(msg, svc); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3434 | if (ret) |
| 3435 | goto nla_put_failure; |
| 3436 | } else { |
| 3437 | ret = -ESRCH; |
| 3438 | goto out_err; |
| 3439 | } |
| 3440 | |
| 3441 | break; |
| 3442 | } |
| 3443 | |
| 3444 | case IPVS_CMD_GET_CONFIG: |
| 3445 | { |
| 3446 | struct ip_vs_timeout_user t; |
| 3447 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 3448 | __ip_vs_get_timeouts(net, &t); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3449 | #ifdef CONFIG_IP_VS_PROTO_TCP |
| 3450 | NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout); |
| 3451 | NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN, |
| 3452 | t.tcp_fin_timeout); |
| 3453 | #endif |
| 3454 | #ifdef CONFIG_IP_VS_PROTO_UDP |
| 3455 | NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout); |
| 3456 | #endif |
| 3457 | |
| 3458 | break; |
| 3459 | } |
| 3460 | |
| 3461 | case IPVS_CMD_GET_INFO: |
| 3462 | NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE); |
| 3463 | NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE, |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 3464 | ip_vs_conn_tab_size); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3465 | break; |
| 3466 | } |
| 3467 | |
| 3468 | genlmsg_end(msg, reply); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 3469 | ret = genlmsg_reply(msg, info); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3470 | goto out; |
| 3471 | |
| 3472 | nla_put_failure: |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 3473 | pr_err("not enough space in Netlink message\n"); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3474 | ret = -EMSGSIZE; |
| 3475 | |
| 3476 | out_err: |
| 3477 | nlmsg_free(msg); |
| 3478 | out: |
| 3479 | mutex_unlock(&__ip_vs_mutex); |
| 3480 | |
| 3481 | return ret; |
| 3482 | } |
| 3483 | |
| 3484 | |
| 3485 | static struct genl_ops ip_vs_genl_ops[] __read_mostly = { |
| 3486 | { |
| 3487 | .cmd = IPVS_CMD_NEW_SERVICE, |
| 3488 | .flags = GENL_ADMIN_PERM, |
| 3489 | .policy = ip_vs_cmd_policy, |
| 3490 | .doit = ip_vs_genl_set_cmd, |
| 3491 | }, |
| 3492 | { |
| 3493 | .cmd = IPVS_CMD_SET_SERVICE, |
| 3494 | .flags = GENL_ADMIN_PERM, |
| 3495 | .policy = ip_vs_cmd_policy, |
| 3496 | .doit = ip_vs_genl_set_cmd, |
| 3497 | }, |
| 3498 | { |
| 3499 | .cmd = IPVS_CMD_DEL_SERVICE, |
| 3500 | .flags = GENL_ADMIN_PERM, |
| 3501 | .policy = ip_vs_cmd_policy, |
| 3502 | .doit = ip_vs_genl_set_cmd, |
| 3503 | }, |
| 3504 | { |
| 3505 | .cmd = IPVS_CMD_GET_SERVICE, |
| 3506 | .flags = GENL_ADMIN_PERM, |
| 3507 | .doit = ip_vs_genl_get_cmd, |
| 3508 | .dumpit = ip_vs_genl_dump_services, |
| 3509 | .policy = ip_vs_cmd_policy, |
| 3510 | }, |
| 3511 | { |
| 3512 | .cmd = IPVS_CMD_NEW_DEST, |
| 3513 | .flags = GENL_ADMIN_PERM, |
| 3514 | .policy = ip_vs_cmd_policy, |
| 3515 | .doit = ip_vs_genl_set_cmd, |
| 3516 | }, |
| 3517 | { |
| 3518 | .cmd = IPVS_CMD_SET_DEST, |
| 3519 | .flags = GENL_ADMIN_PERM, |
| 3520 | .policy = ip_vs_cmd_policy, |
| 3521 | .doit = ip_vs_genl_set_cmd, |
| 3522 | }, |
| 3523 | { |
| 3524 | .cmd = IPVS_CMD_DEL_DEST, |
| 3525 | .flags = GENL_ADMIN_PERM, |
| 3526 | .policy = ip_vs_cmd_policy, |
| 3527 | .doit = ip_vs_genl_set_cmd, |
| 3528 | }, |
| 3529 | { |
| 3530 | .cmd = IPVS_CMD_GET_DEST, |
| 3531 | .flags = GENL_ADMIN_PERM, |
| 3532 | .policy = ip_vs_cmd_policy, |
| 3533 | .dumpit = ip_vs_genl_dump_dests, |
| 3534 | }, |
| 3535 | { |
| 3536 | .cmd = IPVS_CMD_NEW_DAEMON, |
| 3537 | .flags = GENL_ADMIN_PERM, |
| 3538 | .policy = ip_vs_cmd_policy, |
| 3539 | .doit = ip_vs_genl_set_cmd, |
| 3540 | }, |
| 3541 | { |
| 3542 | .cmd = IPVS_CMD_DEL_DAEMON, |
| 3543 | .flags = GENL_ADMIN_PERM, |
| 3544 | .policy = ip_vs_cmd_policy, |
| 3545 | .doit = ip_vs_genl_set_cmd, |
| 3546 | }, |
| 3547 | { |
| 3548 | .cmd = IPVS_CMD_GET_DAEMON, |
| 3549 | .flags = GENL_ADMIN_PERM, |
| 3550 | .dumpit = ip_vs_genl_dump_daemons, |
| 3551 | }, |
| 3552 | { |
| 3553 | .cmd = IPVS_CMD_SET_CONFIG, |
| 3554 | .flags = GENL_ADMIN_PERM, |
| 3555 | .policy = ip_vs_cmd_policy, |
| 3556 | .doit = ip_vs_genl_set_cmd, |
| 3557 | }, |
| 3558 | { |
| 3559 | .cmd = IPVS_CMD_GET_CONFIG, |
| 3560 | .flags = GENL_ADMIN_PERM, |
| 3561 | .doit = ip_vs_genl_get_cmd, |
| 3562 | }, |
| 3563 | { |
| 3564 | .cmd = IPVS_CMD_GET_INFO, |
| 3565 | .flags = GENL_ADMIN_PERM, |
| 3566 | .doit = ip_vs_genl_get_cmd, |
| 3567 | }, |
| 3568 | { |
| 3569 | .cmd = IPVS_CMD_ZERO, |
| 3570 | .flags = GENL_ADMIN_PERM, |
| 3571 | .policy = ip_vs_cmd_policy, |
| 3572 | .doit = ip_vs_genl_set_cmd, |
| 3573 | }, |
| 3574 | { |
| 3575 | .cmd = IPVS_CMD_FLUSH, |
| 3576 | .flags = GENL_ADMIN_PERM, |
| 3577 | .doit = ip_vs_genl_set_cmd, |
| 3578 | }, |
| 3579 | }; |
| 3580 | |
| 3581 | static int __init ip_vs_genl_register(void) |
| 3582 | { |
Michał Mirosław | 8f698d5 | 2009-05-21 10:34:05 +0000 | [diff] [blame] | 3583 | return genl_register_family_with_ops(&ip_vs_genl_family, |
| 3584 | ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops)); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | static void ip_vs_genl_unregister(void) |
| 3588 | { |
| 3589 | genl_unregister_family(&ip_vs_genl_family); |
| 3590 | } |
| 3591 | |
| 3592 | /* End of Generic Netlink interface definitions */ |
| 3593 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3594 | /* |
| 3595 | * per netns intit/exit func. |
| 3596 | */ |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3597 | #ifdef CONFIG_SYSCTL |
| 3598 | int __net_init __ip_vs_control_init_sysctl(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3599 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3600 | int idx; |
| 3601 | struct netns_ipvs *ipvs = net_ipvs(net); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3602 | struct ctl_table *tbl; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3603 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3604 | atomic_set(&ipvs->dropentry, 0); |
| 3605 | spin_lock_init(&ipvs->dropentry_lock); |
| 3606 | spin_lock_init(&ipvs->droppacket_lock); |
| 3607 | spin_lock_init(&ipvs->securetcp_lock); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3608 | |
| 3609 | if (!net_eq(net, &init_net)) { |
| 3610 | tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL); |
| 3611 | if (tbl == NULL) |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3612 | return -ENOMEM; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3613 | } else |
| 3614 | tbl = vs_vars; |
| 3615 | /* Initialize sysctl defaults */ |
| 3616 | idx = 0; |
| 3617 | ipvs->sysctl_amemthresh = 1024; |
| 3618 | tbl[idx++].data = &ipvs->sysctl_amemthresh; |
| 3619 | ipvs->sysctl_am_droprate = 10; |
| 3620 | tbl[idx++].data = &ipvs->sysctl_am_droprate; |
| 3621 | tbl[idx++].data = &ipvs->sysctl_drop_entry; |
| 3622 | tbl[idx++].data = &ipvs->sysctl_drop_packet; |
| 3623 | #ifdef CONFIG_IP_VS_NFCT |
| 3624 | tbl[idx++].data = &ipvs->sysctl_conntrack; |
| 3625 | #endif |
| 3626 | tbl[idx++].data = &ipvs->sysctl_secure_tcp; |
| 3627 | ipvs->sysctl_snat_reroute = 1; |
| 3628 | tbl[idx++].data = &ipvs->sysctl_snat_reroute; |
| 3629 | ipvs->sysctl_sync_ver = 1; |
| 3630 | tbl[idx++].data = &ipvs->sysctl_sync_ver; |
| 3631 | tbl[idx++].data = &ipvs->sysctl_cache_bypass; |
| 3632 | tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn; |
| 3633 | tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template; |
Simon Horman | 59e0350 | 2011-02-04 18:33:01 +0900 | [diff] [blame] | 3634 | ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD; |
| 3635 | ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3636 | tbl[idx].data = &ipvs->sysctl_sync_threshold; |
| 3637 | tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold); |
| 3638 | tbl[idx++].data = &ipvs->sysctl_nat_icmp_send; |
| 3639 | |
| 3640 | |
| 3641 | ipvs->sysctl_hdr = register_net_sysctl_table(net, net_vs_ctl_path, |
Hans Schillstrom | 0792470 | 2011-01-24 15:14:41 +0100 | [diff] [blame] | 3642 | tbl); |
Simon Horman | 0443929 | 2011-02-01 18:29:04 +0100 | [diff] [blame] | 3643 | if (ipvs->sysctl_hdr == NULL) { |
| 3644 | if (!net_eq(net, &init_net)) |
| 3645 | kfree(tbl); |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3646 | return -ENOMEM; |
Simon Horman | 0443929 | 2011-02-01 18:29:04 +0100 | [diff] [blame] | 3647 | } |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 3648 | ip_vs_start_estimator(net, &ipvs->tot_stats); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 3649 | ipvs->sysctl_tbl = tbl; |
Hans Schillstrom | f6340ee | 2011-01-03 14:44:59 +0100 | [diff] [blame] | 3650 | /* Schedule defense work */ |
| 3651 | INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler); |
| 3652 | schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD); |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3653 | |
| 3654 | return 0; |
| 3655 | } |
| 3656 | |
| 3657 | void __net_init __ip_vs_control_cleanup_sysctl(struct net *net) |
| 3658 | { |
| 3659 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 3660 | |
| 3661 | cancel_delayed_work_sync(&ipvs->defense_work); |
| 3662 | cancel_work_sync(&ipvs->defense_work.work); |
| 3663 | unregister_net_sysctl_table(ipvs->sysctl_hdr); |
| 3664 | } |
| 3665 | |
| 3666 | #else |
| 3667 | |
| 3668 | int __net_init __ip_vs_control_init_sysctl(struct net *net) { return 0; } |
| 3669 | void __net_init __ip_vs_control_cleanup_sysctl(struct net *net) { } |
| 3670 | |
| 3671 | #endif |
| 3672 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3673 | static struct notifier_block ip_vs_dst_notifier = { |
| 3674 | .notifier_call = ip_vs_dst_event, |
| 3675 | }; |
| 3676 | |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3677 | int __net_init __ip_vs_control_init(struct net *net) |
| 3678 | { |
| 3679 | int idx; |
| 3680 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 3681 | |
| 3682 | ipvs->rs_lock = __RW_LOCK_UNLOCKED(ipvs->rs_lock); |
| 3683 | |
| 3684 | /* Initialize rs_table */ |
| 3685 | for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++) |
| 3686 | INIT_LIST_HEAD(&ipvs->rs_table[idx]); |
| 3687 | |
| 3688 | INIT_LIST_HEAD(&ipvs->dest_trash); |
| 3689 | atomic_set(&ipvs->ftpsvc_counter, 0); |
| 3690 | atomic_set(&ipvs->nullsvc_counter, 0); |
| 3691 | |
| 3692 | /* procfs stats */ |
| 3693 | ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats); |
Eric Dumazet | f40f94f | 2011-03-21 10:15:40 +0000 | [diff] [blame] | 3694 | if (!ipvs->tot_stats.cpustats) { |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3695 | pr_err("%s(): alloc_percpu.\n", __func__); |
| 3696 | return -ENOMEM; |
| 3697 | } |
| 3698 | spin_lock_init(&ipvs->tot_stats.lock); |
| 3699 | |
| 3700 | proc_net_fops_create(net, "ip_vs", 0, &ip_vs_info_fops); |
| 3701 | proc_net_fops_create(net, "ip_vs_stats", 0, &ip_vs_stats_fops); |
| 3702 | proc_net_fops_create(net, "ip_vs_stats_percpu", 0, |
| 3703 | &ip_vs_stats_percpu_fops); |
| 3704 | |
| 3705 | if (__ip_vs_control_init_sysctl(net)) |
| 3706 | goto err; |
| 3707 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3708 | return 0; |
| 3709 | |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3710 | err: |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 3711 | free_percpu(ipvs->tot_stats.cpustats); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3712 | return -ENOMEM; |
| 3713 | } |
| 3714 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3715 | void __net_exit __ip_vs_control_cleanup(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3716 | { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 3717 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 3718 | |
Hans Schillstrom | f2431e6 | 2011-01-03 14:45:00 +0100 | [diff] [blame] | 3719 | ip_vs_trash_cleanup(net); |
Julian Anastasov | 6ef757f | 2011-03-14 01:44:28 +0200 | [diff] [blame] | 3720 | ip_vs_stop_estimator(net, &ipvs->tot_stats); |
Simon Horman | 14e4054 | 2011-02-04 18:33:02 +0900 | [diff] [blame] | 3721 | __ip_vs_control_cleanup_sysctl(net); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 3722 | proc_net_remove(net, "ip_vs_stats_percpu"); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3723 | proc_net_remove(net, "ip_vs_stats"); |
| 3724 | proc_net_remove(net, "ip_vs"); |
Julian Anastasov | 2a0751a | 2011-03-04 12:20:35 +0200 | [diff] [blame] | 3725 | free_percpu(ipvs->tot_stats.cpustats); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
Sven Wegener | 048cf48 | 2008-08-10 18:24:35 +0000 | [diff] [blame] | 3728 | int __init ip_vs_control_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | int idx; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3731 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | |
| 3733 | EnterFunction(2); |
| 3734 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3735 | /* Initialize svc_table, ip_vs_svc_fwm_table, rs_table */ |
Eduardo Blanco | d86bef7 | 2010-10-19 10:26:47 +0100 | [diff] [blame] | 3736 | for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) { |
| 3737 | INIT_LIST_HEAD(&ip_vs_svc_table[idx]); |
| 3738 | INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]); |
| 3739 | } |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3740 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3741 | smp_wmb(); /* Do we really need it now ? */ |
Eduardo Blanco | d86bef7 | 2010-10-19 10:26:47 +0100 | [diff] [blame] | 3742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | ret = nf_register_sockopt(&ip_vs_sockopts); |
| 3744 | if (ret) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 3745 | pr_err("cannot register sockopt.\n"); |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3746 | goto err_sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3749 | ret = ip_vs_genl_register(); |
| 3750 | if (ret) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 3751 | pr_err("cannot register Generic Netlink interface.\n"); |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3752 | goto err_genl; |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3753 | } |
| 3754 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3755 | ret = register_netdevice_notifier(&ip_vs_dst_notifier); |
| 3756 | if (ret < 0) |
| 3757 | goto err_notf; |
| 3758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | LeaveFunction(2); |
| 3760 | return 0; |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3761 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 3762 | err_notf: |
| 3763 | ip_vs_genl_unregister(); |
| 3764 | err_genl: |
| 3765 | nf_unregister_sockopt(&ip_vs_sockopts); |
| 3766 | err_sock: |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 3767 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | |
| 3771 | void ip_vs_control_cleanup(void) |
| 3772 | { |
| 3773 | EnterFunction(2); |
Simon Horman | 7676e34 | 2011-05-19 21:32:57 +0900 | [diff] [blame^] | 3774 | unregister_netdevice_notifier(&ip_vs_dst_notifier); |
Julius Volz | 9a81219 | 2008-08-14 14:08:44 +0200 | [diff] [blame] | 3775 | ip_vs_genl_unregister(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | nf_unregister_sockopt(&ip_vs_sockopts); |
| 3777 | LeaveFunction(2); |
| 3778 | } |