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