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