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