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