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