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