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