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