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