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