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