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