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