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