ipvs: fix sparse warnings for ip_vs_conn listing
[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)
1940{
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)
1996{
ceec4c38 1997 rcu_read_unlock();
1da177e4
LT
1998}
1999
2000
2001static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
2002{
2003 if (v == SEQ_START_TOKEN) {
2004 seq_printf(seq,
2005 "IP Virtual Server version %d.%d.%d (size=%d)\n",
6f7edb48 2006 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1da177e4
LT
2007 seq_puts(seq,
2008 "Prot LocalAddress:Port Scheduler Flags\n");
2009 seq_puts(seq,
2010 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
2011 } else {
2012 const struct ip_vs_service *svc = v;
2013 const struct ip_vs_iter *iter = seq->private;
2014 const struct ip_vs_dest *dest;
ceec4c38 2015 struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
1da177e4 2016
667a5f18
VB
2017 if (iter->table == ip_vs_svc_table) {
2018#ifdef CONFIG_IP_VS_IPV6
2019 if (svc->af == AF_INET6)
5b095d98 2020 seq_printf(seq, "%s [%pI6]:%04X %s ",
667a5f18 2021 ip_vs_proto_name(svc->protocol),
38ff4fa4 2022 &svc->addr.in6,
667a5f18 2023 ntohs(svc->port),
ceec4c38 2024 sched->name);
667a5f18
VB
2025 else
2026#endif
26ec037f 2027 seq_printf(seq, "%s %08X:%04X %s %s ",
667a5f18
VB
2028 ip_vs_proto_name(svc->protocol),
2029 ntohl(svc->addr.ip),
2030 ntohs(svc->port),
ceec4c38 2031 sched->name,
26ec037f 2032 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
667a5f18 2033 } else {
26ec037f 2034 seq_printf(seq, "FWM %08X %s %s",
ceec4c38 2035 svc->fwmark, sched->name,
26ec037f 2036 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
667a5f18 2037 }
1da177e4
LT
2038
2039 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
2040 seq_printf(seq, "persistent %d %08X\n",
2041 svc->timeout,
2042 ntohl(svc->netmask));
2043 else
2044 seq_putc(seq, '\n');
2045
413c2d04 2046 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
667a5f18
VB
2047#ifdef CONFIG_IP_VS_IPV6
2048 if (dest->af == AF_INET6)
2049 seq_printf(seq,
5b095d98 2050 " -> [%pI6]:%04X"
667a5f18 2051 " %-7s %-6d %-10d %-10d\n",
38ff4fa4 2052 &dest->addr.in6,
667a5f18
VB
2053 ntohs(dest->port),
2054 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2055 atomic_read(&dest->weight),
2056 atomic_read(&dest->activeconns),
2057 atomic_read(&dest->inactconns));
2058 else
2059#endif
2060 seq_printf(seq,
2061 " -> %08X:%04X "
2062 "%-7s %-6d %-10d %-10d\n",
2063 ntohl(dest->addr.ip),
2064 ntohs(dest->port),
2065 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
2066 atomic_read(&dest->weight),
2067 atomic_read(&dest->activeconns),
2068 atomic_read(&dest->inactconns));
2069
1da177e4
LT
2070 }
2071 }
2072 return 0;
2073}
2074
56b3d975 2075static const struct seq_operations ip_vs_info_seq_ops = {
1da177e4
LT
2076 .start = ip_vs_info_seq_start,
2077 .next = ip_vs_info_seq_next,
2078 .stop = ip_vs_info_seq_stop,
2079 .show = ip_vs_info_seq_show,
2080};
2081
2082static int ip_vs_info_open(struct inode *inode, struct file *file)
2083{
fc723250 2084 return seq_open_net(inode, file, &ip_vs_info_seq_ops,
cf7732e4 2085 sizeof(struct ip_vs_iter));
1da177e4
LT
2086}
2087
9a32144e 2088static const struct file_operations ip_vs_info_fops = {
1da177e4
LT
2089 .owner = THIS_MODULE,
2090 .open = ip_vs_info_open,
2091 .read = seq_read,
2092 .llseek = seq_lseek,
0f08190f 2093 .release = seq_release_net,
1da177e4
LT
2094};
2095
1da177e4
LT
2096static int ip_vs_stats_show(struct seq_file *seq, void *v)
2097{
b17fc996 2098 struct net *net = seq_file_single_net(seq);
55a3d4e1 2099 struct ip_vs_stats_user show;
1da177e4
LT
2100
2101/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2102 seq_puts(seq,
2103 " Total Incoming Outgoing Incoming Outgoing\n");
2104 seq_printf(seq,
2105 " Conns Packets Packets Bytes Bytes\n");
2106
55a3d4e1
JA
2107 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2108 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2109 show.inpkts, show.outpkts,
2110 (unsigned long long) show.inbytes,
2111 (unsigned long long) show.outbytes);
1da177e4
LT
2112
2113/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2114 seq_puts(seq,
2115 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
55a3d4e1
JA
2116 seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2117 show.cps, show.inpps, show.outpps,
2118 show.inbps, show.outbps);
1da177e4
LT
2119
2120 return 0;
2121}
2122
2123static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
2124{
fc723250 2125 return single_open_net(inode, file, ip_vs_stats_show);
1da177e4
LT
2126}
2127
9a32144e 2128static const struct file_operations ip_vs_stats_fops = {
1da177e4
LT
2129 .owner = THIS_MODULE,
2130 .open = ip_vs_stats_seq_open,
2131 .read = seq_read,
2132 .llseek = seq_lseek,
0f08190f 2133 .release = single_release_net,
1da177e4
LT
2134};
2135
b17fc996
HS
2136static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2137{
2138 struct net *net = seq_file_single_net(seq);
2a0751af
JA
2139 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2140 struct ip_vs_cpu_stats *cpustats = tot_stats->cpustats;
ea9f22cc 2141 struct ip_vs_stats_user rates;
b17fc996
HS
2142 int i;
2143
2144/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2145 seq_puts(seq,
2146 " Total Incoming Outgoing Incoming Outgoing\n");
2147 seq_printf(seq,
2148 "CPU Conns Packets Packets Bytes Bytes\n");
2149
2150 for_each_possible_cpu(i) {
2a0751af
JA
2151 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2152 unsigned int start;
2153 __u64 inbytes, outbytes;
2154
2155 do {
2156 start = u64_stats_fetch_begin_bh(&u->syncp);
2157 inbytes = u->ustats.inbytes;
2158 outbytes = u->ustats.outbytes;
2159 } while (u64_stats_fetch_retry_bh(&u->syncp, start));
2160
b17fc996 2161 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
2a0751af
JA
2162 i, u->ustats.conns, u->ustats.inpkts,
2163 u->ustats.outpkts, (__u64)inbytes,
2164 (__u64)outbytes);
b17fc996
HS
2165 }
2166
2167 spin_lock_bh(&tot_stats->lock);
ea9f22cc 2168
b17fc996
HS
2169 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2170 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2171 tot_stats->ustats.outpkts,
2172 (unsigned long long) tot_stats->ustats.inbytes,
2173 (unsigned long long) tot_stats->ustats.outbytes);
2174
ea9f22cc
JA
2175 ip_vs_read_estimator(&rates, tot_stats);
2176
2177 spin_unlock_bh(&tot_stats->lock);
2178
b17fc996
HS
2179/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2180 seq_puts(seq,
2181 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2182 seq_printf(seq, " %8X %8X %8X %16X %16X\n",
ea9f22cc
JA
2183 rates.cps,
2184 rates.inpps,
2185 rates.outpps,
2186 rates.inbps,
2187 rates.outbps);
b17fc996
HS
2188
2189 return 0;
2190}
2191
2192static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
2193{
2194 return single_open_net(inode, file, ip_vs_stats_percpu_show);
2195}
2196
2197static const struct file_operations ip_vs_stats_percpu_fops = {
2198 .owner = THIS_MODULE,
2199 .open = ip_vs_stats_percpu_seq_open,
2200 .read = seq_read,
2201 .llseek = seq_lseek,
0f08190f 2202 .release = single_release_net,
b17fc996 2203};
1da177e4
LT
2204#endif
2205
2206/*
2207 * Set timeout values for tcp tcpfin udp in the timeout_table.
2208 */
9330419d 2209static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
1da177e4 2210{
091bb34c 2211#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
9330419d 2212 struct ip_vs_proto_data *pd;
091bb34c 2213#endif
9330419d 2214
1da177e4
LT
2215 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
2216 u->tcp_timeout,
2217 u->tcp_fin_timeout,
2218 u->udp_timeout);
2219
2220#ifdef CONFIG_IP_VS_PROTO_TCP
2221 if (u->tcp_timeout) {
9330419d
HS
2222 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2223 pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
1da177e4
LT
2224 = u->tcp_timeout * HZ;
2225 }
2226
2227 if (u->tcp_fin_timeout) {
9330419d
HS
2228 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2229 pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
1da177e4
LT
2230 = u->tcp_fin_timeout * HZ;
2231 }
2232#endif
2233
2234#ifdef CONFIG_IP_VS_PROTO_UDP
2235 if (u->udp_timeout) {
9330419d
HS
2236 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
2237 pd->timeout_table[IP_VS_UDP_S_NORMAL]
1da177e4
LT
2238 = u->udp_timeout * HZ;
2239 }
2240#endif
2241 return 0;
2242}
2243
2244
2245#define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2246#define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2247#define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2248 sizeof(struct ip_vs_dest_user))
2249#define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2250#define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2251#define MAX_ARG_LEN SVCDEST_ARG_LEN
2252
9b5b5cff 2253static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
1da177e4
LT
2254 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2255 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2256 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2257 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2258 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2259 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2260 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2261 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2262 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2263 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2264 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2265};
2266
c860c6b1
JV
2267static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2268 struct ip_vs_service_user *usvc_compat)
2269{
0d1e71b0
SH
2270 memset(usvc, 0, sizeof(*usvc));
2271
c860c6b1
JV
2272 usvc->af = AF_INET;
2273 usvc->protocol = usvc_compat->protocol;
2274 usvc->addr.ip = usvc_compat->addr;
2275 usvc->port = usvc_compat->port;
2276 usvc->fwmark = usvc_compat->fwmark;
2277
2278 /* Deep copy of sched_name is not needed here */
2279 usvc->sched_name = usvc_compat->sched_name;
2280
2281 usvc->flags = usvc_compat->flags;
2282 usvc->timeout = usvc_compat->timeout;
2283 usvc->netmask = usvc_compat->netmask;
2284}
2285
2286static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2287 struct ip_vs_dest_user *udest_compat)
2288{
0d1e71b0
SH
2289 memset(udest, 0, sizeof(*udest));
2290
c860c6b1
JV
2291 udest->addr.ip = udest_compat->addr;
2292 udest->port = udest_compat->port;
2293 udest->conn_flags = udest_compat->conn_flags;
2294 udest->weight = udest_compat->weight;
2295 udest->u_threshold = udest_compat->u_threshold;
2296 udest->l_threshold = udest_compat->l_threshold;
2297}
2298
1da177e4
LT
2299static int
2300do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2301{
fc723250 2302 struct net *net = sock_net(sk);
1da177e4
LT
2303 int ret;
2304 unsigned char arg[MAX_ARG_LEN];
c860c6b1
JV
2305 struct ip_vs_service_user *usvc_compat;
2306 struct ip_vs_service_user_kern usvc;
1da177e4 2307 struct ip_vs_service *svc;
c860c6b1
JV
2308 struct ip_vs_dest_user *udest_compat;
2309 struct ip_vs_dest_user_kern udest;
ae1d48b2 2310 struct netns_ipvs *ipvs = net_ipvs(net);
1da177e4 2311
df008c91 2312 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
2313 return -EPERM;
2314
04bcef2a
AV
2315 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2316 return -EINVAL;
2317 if (len < 0 || len > MAX_ARG_LEN)
2318 return -EINVAL;
1da177e4 2319 if (len != set_arglen[SET_CMDID(cmd)]) {
1e3e238e
HE
2320 pr_err("set_ctl: len %u != %u\n",
2321 len, set_arglen[SET_CMDID(cmd)]);
1da177e4
LT
2322 return -EINVAL;
2323 }
2324
2325 if (copy_from_user(arg, user, len) != 0)
2326 return -EFAULT;
2327
2328 /* increase the module use count */
2329 ip_vs_use_count_inc();
2330
ae1d48b2
HS
2331 /* Handle daemons since they have another lock */
2332 if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2333 cmd == IP_VS_SO_SET_STOPDAEMON) {
2334 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2335
2336 if (mutex_lock_interruptible(&ipvs->sync_mutex)) {
2337 ret = -ERESTARTSYS;
2338 goto out_dec;
2339 }
2340 if (cmd == IP_VS_SO_SET_STARTDAEMON)
2341 ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2342 dm->syncid);
2343 else
2344 ret = stop_sync_thread(net, dm->state);
2345 mutex_unlock(&ipvs->sync_mutex);
2346 goto out_dec;
2347 }
2348
14cc3e2b 2349 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
1da177e4
LT
2350 ret = -ERESTARTSYS;
2351 goto out_dec;
2352 }
2353
2354 if (cmd == IP_VS_SO_SET_FLUSH) {
2355 /* Flush the virtual service */
578bc3ef 2356 ret = ip_vs_flush(net, false);
1da177e4
LT
2357 goto out_unlock;
2358 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2359 /* Set timeout values for (tcp tcpfin udp) */
9330419d 2360 ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
1da177e4 2361 goto out_unlock;
1da177e4
LT
2362 }
2363
c860c6b1
JV
2364 usvc_compat = (struct ip_vs_service_user *)arg;
2365 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2366
2367 /* We only use the new structs internally, so copy userspace compat
2368 * structs to extended internal versions */
2369 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2370 ip_vs_copy_udest_compat(&udest, udest_compat);
1da177e4
LT
2371
2372 if (cmd == IP_VS_SO_SET_ZERO) {
2373 /* if no service address is set, zero counters in all */
c860c6b1 2374 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
fc723250 2375 ret = ip_vs_zero_all(net);
1da177e4
LT
2376 goto out_unlock;
2377 }
2378 }
2379
2906f66a
VMR
2380 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2381 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2382 usvc.protocol != IPPROTO_SCTP) {
1e3e238e
HE
2383 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2384 usvc.protocol, &usvc.addr.ip,
2385 ntohs(usvc.port), usvc.sched_name);
1da177e4
LT
2386 ret = -EFAULT;
2387 goto out_unlock;
2388 }
2389
2390 /* Lookup the exact service by <protocol, addr, port> or fwmark */
ceec4c38 2391 rcu_read_lock();
c860c6b1 2392 if (usvc.fwmark == 0)
fc723250 2393 svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
26c15cfd 2394 &usvc.addr, usvc.port);
1da177e4 2395 else
fc723250 2396 svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
ceec4c38 2397 rcu_read_unlock();
1da177e4
LT
2398
2399 if (cmd != IP_VS_SO_SET_ADD
c860c6b1 2400 && (svc == NULL || svc->protocol != usvc.protocol)) {
1da177e4 2401 ret = -ESRCH;
26c15cfd 2402 goto out_unlock;
1da177e4
LT
2403 }
2404
2405 switch (cmd) {
2406 case IP_VS_SO_SET_ADD:
2407 if (svc != NULL)
2408 ret = -EEXIST;
2409 else
fc723250 2410 ret = ip_vs_add_service(net, &usvc, &svc);
1da177e4
LT
2411 break;
2412 case IP_VS_SO_SET_EDIT:
c860c6b1 2413 ret = ip_vs_edit_service(svc, &usvc);
1da177e4
LT
2414 break;
2415 case IP_VS_SO_SET_DEL:
2416 ret = ip_vs_del_service(svc);
2417 if (!ret)
2418 goto out_unlock;
2419 break;
2420 case IP_VS_SO_SET_ZERO:
2421 ret = ip_vs_zero_service(svc);
2422 break;
2423 case IP_VS_SO_SET_ADDDEST:
c860c6b1 2424 ret = ip_vs_add_dest(svc, &udest);
1da177e4
LT
2425 break;
2426 case IP_VS_SO_SET_EDITDEST:
c860c6b1 2427 ret = ip_vs_edit_dest(svc, &udest);
1da177e4
LT
2428 break;
2429 case IP_VS_SO_SET_DELDEST:
c860c6b1 2430 ret = ip_vs_del_dest(svc, &udest);
1da177e4
LT
2431 break;
2432 default:
2433 ret = -EINVAL;
2434 }
2435
1da177e4 2436 out_unlock:
14cc3e2b 2437 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2438 out_dec:
2439 /* decrease the module use count */
2440 ip_vs_use_count_dec();
2441
2442 return ret;
2443}
2444
2445
1da177e4
LT
2446static void
2447ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2448{
ceec4c38
JA
2449 struct ip_vs_scheduler *sched;
2450
2451 sched = rcu_dereference_protected(src->scheduler, 1);
1da177e4 2452 dst->protocol = src->protocol;
e7ade46a 2453 dst->addr = src->addr.ip;
1da177e4
LT
2454 dst->port = src->port;
2455 dst->fwmark = src->fwmark;
ceec4c38 2456 strlcpy(dst->sched_name, sched->name, sizeof(dst->sched_name));
1da177e4
LT
2457 dst->flags = src->flags;
2458 dst->timeout = src->timeout / HZ;
2459 dst->netmask = src->netmask;
2460 dst->num_dests = src->num_dests;
2461 ip_vs_copy_stats(&dst->stats, &src->stats);
2462}
2463
2464static inline int
fc723250
HS
2465__ip_vs_get_service_entries(struct net *net,
2466 const struct ip_vs_get_services *get,
1da177e4
LT
2467 struct ip_vs_get_services __user *uptr)
2468{
2469 int idx, count=0;
2470 struct ip_vs_service *svc;
2471 struct ip_vs_service_entry entry;
2472 int ret = 0;
2473
2474 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
ceec4c38 2475 hlist_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
f94fd041 2476 /* Only expose IPv4 entries to old interface */
fc723250 2477 if (svc->af != AF_INET || !net_eq(svc->net, net))
f94fd041
JV
2478 continue;
2479
1da177e4
LT
2480 if (count >= get->num_services)
2481 goto out;
4da62fc7 2482 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2483 ip_vs_copy_service(&entry, svc);
2484 if (copy_to_user(&uptr->entrytable[count],
2485 &entry, sizeof(entry))) {
2486 ret = -EFAULT;
2487 goto out;
2488 }
2489 count++;
2490 }
2491 }
2492
2493 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
ceec4c38 2494 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
f94fd041 2495 /* Only expose IPv4 entries to old interface */
fc723250 2496 if (svc->af != AF_INET || !net_eq(svc->net, net))
f94fd041
JV
2497 continue;
2498
1da177e4
LT
2499 if (count >= get->num_services)
2500 goto out;
4da62fc7 2501 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2502 ip_vs_copy_service(&entry, svc);
2503 if (copy_to_user(&uptr->entrytable[count],
2504 &entry, sizeof(entry))) {
2505 ret = -EFAULT;
2506 goto out;
2507 }
2508 count++;
2509 }
2510 }
552ad65a 2511out:
1da177e4
LT
2512 return ret;
2513}
2514
2515static inline int
fc723250 2516__ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
1da177e4
LT
2517 struct ip_vs_get_dests __user *uptr)
2518{
2519 struct ip_vs_service *svc;
b18610de 2520 union nf_inet_addr addr = { .ip = get->addr };
1da177e4
LT
2521 int ret = 0;
2522
ceec4c38 2523 rcu_read_lock();
1da177e4 2524 if (get->fwmark)
fc723250 2525 svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
1da177e4 2526 else
fc723250 2527 svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
26c15cfd 2528 get->port);
ceec4c38 2529 rcu_read_unlock();
b18610de 2530
1da177e4
LT
2531 if (svc) {
2532 int count = 0;
2533 struct ip_vs_dest *dest;
2534 struct ip_vs_dest_entry entry;
2535
2536 list_for_each_entry(dest, &svc->destinations, n_list) {
2537 if (count >= get->num_dests)
2538 break;
2539
e7ade46a 2540 entry.addr = dest->addr.ip;
1da177e4
LT
2541 entry.port = dest->port;
2542 entry.conn_flags = atomic_read(&dest->conn_flags);
2543 entry.weight = atomic_read(&dest->weight);
2544 entry.u_threshold = dest->u_threshold;
2545 entry.l_threshold = dest->l_threshold;
2546 entry.activeconns = atomic_read(&dest->activeconns);
2547 entry.inactconns = atomic_read(&dest->inactconns);
2548 entry.persistconns = atomic_read(&dest->persistconns);
2549 ip_vs_copy_stats(&entry.stats, &dest->stats);
2550 if (copy_to_user(&uptr->entrytable[count],
2551 &entry, sizeof(entry))) {
2552 ret = -EFAULT;
2553 break;
2554 }
2555 count++;
2556 }
1da177e4
LT
2557 } else
2558 ret = -ESRCH;
2559 return ret;
2560}
2561
2562static inline void
9330419d 2563__ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
1da177e4 2564{
091bb34c 2565#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
9330419d 2566 struct ip_vs_proto_data *pd;
091bb34c 2567#endif
9330419d 2568
b61a602e
AB
2569 memset(u, 0, sizeof (*u));
2570
1da177e4 2571#ifdef CONFIG_IP_VS_PROTO_TCP
9330419d
HS
2572 pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
2573 u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2574 u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
1da177e4
LT
2575#endif
2576#ifdef CONFIG_IP_VS_PROTO_UDP
9330419d 2577 pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
1da177e4 2578 u->udp_timeout =
9330419d 2579 pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
1da177e4
LT
2580#endif
2581}
2582
2583
2584#define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2585#define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2586#define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2587#define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2588#define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2589#define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2590#define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2591
9b5b5cff 2592static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
1da177e4
LT
2593 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2594 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2595 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2596 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2597 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2598 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2599 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2600};
2601
2602static int
2603do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2604{
2605 unsigned char arg[128];
2606 int ret = 0;
04bcef2a 2607 unsigned int copylen;
fc723250 2608 struct net *net = sock_net(sk);
f131315f 2609 struct netns_ipvs *ipvs = net_ipvs(net);
1da177e4 2610
fc723250 2611 BUG_ON(!net);
df008c91 2612 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
2613 return -EPERM;
2614
04bcef2a
AV
2615 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2616 return -EINVAL;
2617
1da177e4 2618 if (*len < get_arglen[GET_CMDID(cmd)]) {
1e3e238e
HE
2619 pr_err("get_ctl: len %u < %u\n",
2620 *len, get_arglen[GET_CMDID(cmd)]);
1da177e4
LT
2621 return -EINVAL;
2622 }
2623
04bcef2a
AV
2624 copylen = get_arglen[GET_CMDID(cmd)];
2625 if (copylen > 128)
2626 return -EINVAL;
2627
2628 if (copy_from_user(arg, user, copylen) != 0)
1da177e4 2629 return -EFAULT;
ae1d48b2
HS
2630 /*
2631 * Handle daemons first since it has its own locking
2632 */
2633 if (cmd == IP_VS_SO_GET_DAEMON) {
2634 struct ip_vs_daemon_user d[2];
2635
2636 memset(&d, 0, sizeof(d));
2637 if (mutex_lock_interruptible(&ipvs->sync_mutex))
2638 return -ERESTARTSYS;
2639
2640 if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2641 d[0].state = IP_VS_STATE_MASTER;
2642 strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2643 sizeof(d[0].mcast_ifn));
2644 d[0].syncid = ipvs->master_syncid;
2645 }
2646 if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2647 d[1].state = IP_VS_STATE_BACKUP;
2648 strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2649 sizeof(d[1].mcast_ifn));
2650 d[1].syncid = ipvs->backup_syncid;
2651 }
2652 if (copy_to_user(user, &d, sizeof(d)) != 0)
2653 ret = -EFAULT;
2654 mutex_unlock(&ipvs->sync_mutex);
2655 return ret;
2656 }
1da177e4 2657
14cc3e2b 2658 if (mutex_lock_interruptible(&__ip_vs_mutex))
1da177e4
LT
2659 return -ERESTARTSYS;
2660
2661 switch (cmd) {
2662 case IP_VS_SO_GET_VERSION:
2663 {
2664 char buf[64];
2665
2666 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
6f7edb48 2667 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
1da177e4
LT
2668 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2669 ret = -EFAULT;
2670 goto out;
2671 }
2672 *len = strlen(buf)+1;
2673 }
2674 break;
2675
2676 case IP_VS_SO_GET_INFO:
2677 {
2678 struct ip_vs_getinfo info;
2679 info.version = IP_VS_VERSION_CODE;
6f7edb48 2680 info.size = ip_vs_conn_tab_size;
a0840e2e 2681 info.num_services = ipvs->num_services;
1da177e4
LT
2682 if (copy_to_user(user, &info, sizeof(info)) != 0)
2683 ret = -EFAULT;
2684 }
2685 break;
2686
2687 case IP_VS_SO_GET_SERVICES:
2688 {
2689 struct ip_vs_get_services *get;
2690 int size;
2691
2692 get = (struct ip_vs_get_services *)arg;
2693 size = sizeof(*get) +
2694 sizeof(struct ip_vs_service_entry) * get->num_services;
2695 if (*len != size) {
1e3e238e 2696 pr_err("length: %u != %u\n", *len, size);
1da177e4
LT
2697 ret = -EINVAL;
2698 goto out;
2699 }
fc723250 2700 ret = __ip_vs_get_service_entries(net, get, user);
1da177e4
LT
2701 }
2702 break;
2703
2704 case IP_VS_SO_GET_SERVICE:
2705 {
2706 struct ip_vs_service_entry *entry;
2707 struct ip_vs_service *svc;
b18610de 2708 union nf_inet_addr addr;
1da177e4
LT
2709
2710 entry = (struct ip_vs_service_entry *)arg;
b18610de 2711 addr.ip = entry->addr;
ceec4c38 2712 rcu_read_lock();
1da177e4 2713 if (entry->fwmark)
fc723250 2714 svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
1da177e4 2715 else
fc723250
HS
2716 svc = __ip_vs_service_find(net, AF_INET,
2717 entry->protocol, &addr,
2718 entry->port);
ceec4c38 2719 rcu_read_unlock();
1da177e4
LT
2720 if (svc) {
2721 ip_vs_copy_service(entry, svc);
2722 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2723 ret = -EFAULT;
1da177e4
LT
2724 } else
2725 ret = -ESRCH;
2726 }
2727 break;
2728
2729 case IP_VS_SO_GET_DESTS:
2730 {
2731 struct ip_vs_get_dests *get;
2732 int size;
2733
2734 get = (struct ip_vs_get_dests *)arg;
2735 size = sizeof(*get) +
2736 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2737 if (*len != size) {
1e3e238e 2738 pr_err("length: %u != %u\n", *len, size);
1da177e4
LT
2739 ret = -EINVAL;
2740 goto out;
2741 }
fc723250 2742 ret = __ip_vs_get_dest_entries(net, get, user);
1da177e4
LT
2743 }
2744 break;
2745
2746 case IP_VS_SO_GET_TIMEOUT:
2747 {
2748 struct ip_vs_timeout_user t;
2749
9330419d 2750 __ip_vs_get_timeouts(net, &t);
1da177e4
LT
2751 if (copy_to_user(user, &t, sizeof(t)) != 0)
2752 ret = -EFAULT;
2753 }
2754 break;
2755
1da177e4
LT
2756 default:
2757 ret = -EINVAL;
2758 }
2759
552ad65a 2760out:
14cc3e2b 2761 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2762 return ret;
2763}
2764
2765
2766static struct nf_sockopt_ops ip_vs_sockopts = {
2767 .pf = PF_INET,
2768 .set_optmin = IP_VS_BASE_CTL,
2769 .set_optmax = IP_VS_SO_SET_MAX+1,
2770 .set = do_ip_vs_set_ctl,
2771 .get_optmin = IP_VS_BASE_CTL,
2772 .get_optmax = IP_VS_SO_GET_MAX+1,
2773 .get = do_ip_vs_get_ctl,
16fcec35 2774 .owner = THIS_MODULE,
1da177e4
LT
2775};
2776
9a812198
JV
2777/*
2778 * Generic Netlink interface
2779 */
2780
2781/* IPVS genetlink family */
2782static struct genl_family ip_vs_genl_family = {
2783 .id = GENL_ID_GENERATE,
2784 .hdrsize = 0,
2785 .name = IPVS_GENL_NAME,
2786 .version = IPVS_GENL_VERSION,
2787 .maxattr = IPVS_CMD_MAX,
c6d2d445 2788 .netnsok = true, /* Make ipvsadm to work on netns */
9a812198
JV
2789};
2790
2791/* Policy used for first-level command attributes */
2792static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2793 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2794 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2795 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2796 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2797 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2798 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2799};
2800
2801/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2802static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2803 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2804 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2805 .len = IP_VS_IFNAME_MAXLEN },
2806 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2807};
2808
2809/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2810static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2811 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2812 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2813 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2814 .len = sizeof(union nf_inet_addr) },
2815 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2816 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2817 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2818 .len = IP_VS_SCHEDNAME_MAXLEN },
0d1e71b0
SH
2819 [IPVS_SVC_ATTR_PE_NAME] = { .type = NLA_NUL_STRING,
2820 .len = IP_VS_PENAME_MAXLEN },
9a812198
JV
2821 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2822 .len = sizeof(struct ip_vs_flags) },
2823 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2824 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2825 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2826};
2827
2828/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2829static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2830 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2831 .len = sizeof(union nf_inet_addr) },
2832 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2833 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2834 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2835 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2836 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2837 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2838 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2839 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2840 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2841};
2842
2843static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2844 struct ip_vs_stats *stats)
2845{
55a3d4e1 2846 struct ip_vs_stats_user ustats;
9a812198
JV
2847 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2848 if (!nl_stats)
2849 return -EMSGSIZE;
2850
55a3d4e1 2851 ip_vs_copy_stats(&ustats, stats);
9a812198 2852
969e8e25
DM
2853 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) ||
2854 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) ||
2855 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) ||
2856 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) ||
2857 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) ||
2858 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) ||
2859 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) ||
2860 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) ||
2861 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) ||
2862 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps))
2863 goto nla_put_failure;
9a812198
JV
2864 nla_nest_end(skb, nl_stats);
2865
2866 return 0;
2867
2868nla_put_failure:
9a812198
JV
2869 nla_nest_cancel(skb, nl_stats);
2870 return -EMSGSIZE;
2871}
2872
2873static int ip_vs_genl_fill_service(struct sk_buff *skb,
2874 struct ip_vs_service *svc)
2875{
ceec4c38 2876 struct ip_vs_scheduler *sched;
9a812198
JV
2877 struct nlattr *nl_service;
2878 struct ip_vs_flags flags = { .flags = svc->flags,
2879 .mask = ~0 };
2880
2881 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2882 if (!nl_service)
2883 return -EMSGSIZE;
2884
969e8e25
DM
2885 if (nla_put_u16(skb, IPVS_SVC_ATTR_AF, svc->af))
2886 goto nla_put_failure;
9a812198 2887 if (svc->fwmark) {
969e8e25
DM
2888 if (nla_put_u32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark))
2889 goto nla_put_failure;
9a812198 2890 } else {
969e8e25
DM
2891 if (nla_put_u16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol) ||
2892 nla_put(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr) ||
2893 nla_put_u16(skb, IPVS_SVC_ATTR_PORT, svc->port))
2894 goto nla_put_failure;
9a812198
JV
2895 }
2896
ceec4c38
JA
2897 sched = rcu_dereference_protected(svc->scheduler, 1);
2898 if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched->name) ||
969e8e25
DM
2899 (svc->pe &&
2900 nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, svc->pe->name)) ||
2901 nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
2902 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2903 nla_put_u32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2904 goto nla_put_failure;
9a812198
JV
2905 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2906 goto nla_put_failure;
2907
2908 nla_nest_end(skb, nl_service);
2909
2910 return 0;
2911
2912nla_put_failure:
2913 nla_nest_cancel(skb, nl_service);
2914 return -EMSGSIZE;
2915}
2916
2917static int ip_vs_genl_dump_service(struct sk_buff *skb,
2918 struct ip_vs_service *svc,
2919 struct netlink_callback *cb)
2920{
2921 void *hdr;
2922
15e47304 2923 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
9a812198
JV
2924 &ip_vs_genl_family, NLM_F_MULTI,
2925 IPVS_CMD_NEW_SERVICE);
2926 if (!hdr)
2927 return -EMSGSIZE;
2928
2929 if (ip_vs_genl_fill_service(skb, svc) < 0)
2930 goto nla_put_failure;
2931
2932 return genlmsg_end(skb, hdr);
2933
2934nla_put_failure:
2935 genlmsg_cancel(skb, hdr);
2936 return -EMSGSIZE;
2937}
2938
2939static int ip_vs_genl_dump_services(struct sk_buff *skb,
2940 struct netlink_callback *cb)
2941{
2942 int idx = 0, i;
2943 int start = cb->args[0];
2944 struct ip_vs_service *svc;
fc723250 2945 struct net *net = skb_sknet(skb);
9a812198
JV
2946
2947 mutex_lock(&__ip_vs_mutex);
2948 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
ceec4c38 2949 hlist_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
fc723250 2950 if (++idx <= start || !net_eq(svc->net, net))
9a812198
JV
2951 continue;
2952 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2953 idx--;
2954 goto nla_put_failure;
2955 }
2956 }
2957 }
2958
2959 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
ceec4c38 2960 hlist_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
fc723250 2961 if (++idx <= start || !net_eq(svc->net, net))
9a812198
JV
2962 continue;
2963 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2964 idx--;
2965 goto nla_put_failure;
2966 }
2967 }
2968 }
2969
2970nla_put_failure:
2971 mutex_unlock(&__ip_vs_mutex);
2972 cb->args[0] = idx;
2973
2974 return skb->len;
2975}
2976
fc723250
HS
2977static int ip_vs_genl_parse_service(struct net *net,
2978 struct ip_vs_service_user_kern *usvc,
26c15cfd
JA
2979 struct nlattr *nla, int full_entry,
2980 struct ip_vs_service **ret_svc)
9a812198
JV
2981{
2982 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2983 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
26c15cfd 2984 struct ip_vs_service *svc;
9a812198
JV
2985
2986 /* Parse mandatory identifying service fields first */
2987 if (nla == NULL ||
2988 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2989 return -EINVAL;
2990
2991 nla_af = attrs[IPVS_SVC_ATTR_AF];
2992 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2993 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2994 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2995 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2996
2997 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2998 return -EINVAL;
2999
258c8893
SH
3000 memset(usvc, 0, sizeof(*usvc));
3001
c860c6b1 3002 usvc->af = nla_get_u16(nla_af);
f94fd041
JV
3003#ifdef CONFIG_IP_VS_IPV6
3004 if (usvc->af != AF_INET && usvc->af != AF_INET6)
3005#else
3006 if (usvc->af != AF_INET)
3007#endif
9a812198
JV
3008 return -EAFNOSUPPORT;
3009
3010 if (nla_fwmark) {
3011 usvc->protocol = IPPROTO_TCP;
3012 usvc->fwmark = nla_get_u32(nla_fwmark);
3013 } else {
3014 usvc->protocol = nla_get_u16(nla_protocol);
3015 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
3016 usvc->port = nla_get_u16(nla_port);
3017 usvc->fwmark = 0;
3018 }
3019
ceec4c38 3020 rcu_read_lock();
26c15cfd 3021 if (usvc->fwmark)
fc723250 3022 svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
26c15cfd 3023 else
fc723250 3024 svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
26c15cfd 3025 &usvc->addr, usvc->port);
ceec4c38 3026 rcu_read_unlock();
26c15cfd
JA
3027 *ret_svc = svc;
3028
9a812198
JV
3029 /* If a full entry was requested, check for the additional fields */
3030 if (full_entry) {
0d1e71b0 3031 struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
9a812198
JV
3032 *nla_netmask;
3033 struct ip_vs_flags flags;
9a812198
JV
3034
3035 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
0d1e71b0 3036 nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
9a812198
JV
3037 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
3038 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
3039 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
3040
3041 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
3042 return -EINVAL;
3043
3044 nla_memcpy(&flags, nla_flags, sizeof(flags));
3045
3046 /* prefill flags from service if it already exists */
26c15cfd 3047 if (svc)
9a812198 3048 usvc->flags = svc->flags;
9a812198
JV
3049
3050 /* set new flags from userland */
3051 usvc->flags = (usvc->flags & ~flags.mask) |
3052 (flags.flags & flags.mask);
c860c6b1 3053 usvc->sched_name = nla_data(nla_sched);
0d1e71b0 3054 usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
9a812198
JV
3055 usvc->timeout = nla_get_u32(nla_timeout);
3056 usvc->netmask = nla_get_u32(nla_netmask);
3057 }
3058
3059 return 0;
3060}
3061
fc723250
HS
3062static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3063 struct nlattr *nla)
9a812198 3064{
c860c6b1 3065 struct ip_vs_service_user_kern usvc;
26c15cfd 3066 struct ip_vs_service *svc;
9a812198
JV
3067 int ret;
3068
fc723250 3069 ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
26c15cfd 3070 return ret ? ERR_PTR(ret) : svc;
9a812198
JV
3071}
3072
3073static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3074{
3075 struct nlattr *nl_dest;
3076
3077 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3078 if (!nl_dest)
3079 return -EMSGSIZE;
3080
969e8e25
DM
3081 if (nla_put(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr) ||
3082 nla_put_u16(skb, IPVS_DEST_ATTR_PORT, dest->port) ||
3083 nla_put_u32(skb, IPVS_DEST_ATTR_FWD_METHOD,
3084 (atomic_read(&dest->conn_flags) &
3085 IP_VS_CONN_F_FWD_MASK)) ||
3086 nla_put_u32(skb, IPVS_DEST_ATTR_WEIGHT,
3087 atomic_read(&dest->weight)) ||
3088 nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
3089 nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
3090 nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
3091 atomic_read(&dest->activeconns)) ||
3092 nla_put_u32(skb, IPVS_DEST_ATTR_INACT_CONNS,
3093 atomic_read(&dest->inactconns)) ||
3094 nla_put_u32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
3095 atomic_read(&dest->persistconns)))
3096 goto nla_put_failure;
9a812198
JV
3097 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
3098 goto nla_put_failure;
3099
3100 nla_nest_end(skb, nl_dest);
3101
3102 return 0;
3103
3104nla_put_failure:
3105 nla_nest_cancel(skb, nl_dest);
3106 return -EMSGSIZE;
3107}
3108
3109static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
3110 struct netlink_callback *cb)
3111{
3112 void *hdr;
3113
15e47304 3114 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
9a812198
JV
3115 &ip_vs_genl_family, NLM_F_MULTI,
3116 IPVS_CMD_NEW_DEST);
3117 if (!hdr)
3118 return -EMSGSIZE;
3119
3120 if (ip_vs_genl_fill_dest(skb, dest) < 0)
3121 goto nla_put_failure;
3122
3123 return genlmsg_end(skb, hdr);
3124
3125nla_put_failure:
3126 genlmsg_cancel(skb, hdr);
3127 return -EMSGSIZE;
3128}
3129
3130static int ip_vs_genl_dump_dests(struct sk_buff *skb,
3131 struct netlink_callback *cb)
3132{
3133 int idx = 0;
3134 int start = cb->args[0];
3135 struct ip_vs_service *svc;
3136 struct ip_vs_dest *dest;
3137 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
a0840e2e 3138 struct net *net = skb_sknet(skb);
9a812198
JV
3139
3140 mutex_lock(&__ip_vs_mutex);
3141
3142 /* Try to find the service for which to dump destinations */
3143 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
3144 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
3145 goto out_err;
3146
a0840e2e 3147
fc723250 3148 svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
9a812198
JV
3149 if (IS_ERR(svc) || svc == NULL)
3150 goto out_err;
3151
3152 /* Dump the destinations */
3153 list_for_each_entry(dest, &svc->destinations, n_list) {
3154 if (++idx <= start)
3155 continue;
3156 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
3157 idx--;
3158 goto nla_put_failure;
3159 }
3160 }
3161
3162nla_put_failure:
3163 cb->args[0] = idx;
9a812198
JV
3164
3165out_err:
3166 mutex_unlock(&__ip_vs_mutex);
3167
3168 return skb->len;
3169}
3170
c860c6b1 3171static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
9a812198
JV
3172 struct nlattr *nla, int full_entry)
3173{
3174 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
3175 struct nlattr *nla_addr, *nla_port;
3176
3177 /* Parse mandatory identifying destination fields first */
3178 if (nla == NULL ||
3179 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
3180 return -EINVAL;
3181
3182 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
3183 nla_port = attrs[IPVS_DEST_ATTR_PORT];
3184
3185 if (!(nla_addr && nla_port))
3186 return -EINVAL;
3187
258c8893
SH
3188 memset(udest, 0, sizeof(*udest));
3189
9a812198
JV
3190 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
3191 udest->port = nla_get_u16(nla_port);
3192
3193 /* If a full entry was requested, check for the additional fields */
3194 if (full_entry) {
3195 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3196 *nla_l_thresh;
3197
3198 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
3199 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
3200 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
3201 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
3202
3203 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
3204 return -EINVAL;
3205
3206 udest->conn_flags = nla_get_u32(nla_fwd)
3207 & IP_VS_CONN_F_FWD_MASK;
3208 udest->weight = nla_get_u32(nla_weight);
3209 udest->u_threshold = nla_get_u32(nla_u_thresh);
3210 udest->l_threshold = nla_get_u32(nla_l_thresh);
3211 }
3212
3213 return 0;
3214}
3215
3216static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
3217 const char *mcast_ifn, __be32 syncid)
3218{
3219 struct nlattr *nl_daemon;
3220
3221 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
3222 if (!nl_daemon)
3223 return -EMSGSIZE;
3224
969e8e25
DM
3225 if (nla_put_u32(skb, IPVS_DAEMON_ATTR_STATE, state) ||
3226 nla_put_string(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn) ||
3227 nla_put_u32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid))
3228 goto nla_put_failure;
9a812198
JV
3229 nla_nest_end(skb, nl_daemon);
3230
3231 return 0;
3232
3233nla_put_failure:
3234 nla_nest_cancel(skb, nl_daemon);
3235 return -EMSGSIZE;
3236}
3237
3238static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
3239 const char *mcast_ifn, __be32 syncid,
3240 struct netlink_callback *cb)
3241{
3242 void *hdr;
15e47304 3243 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
9a812198
JV
3244 &ip_vs_genl_family, NLM_F_MULTI,
3245 IPVS_CMD_NEW_DAEMON);
3246 if (!hdr)
3247 return -EMSGSIZE;
3248
3249 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
3250 goto nla_put_failure;
3251
3252 return genlmsg_end(skb, hdr);
3253
3254nla_put_failure:
3255 genlmsg_cancel(skb, hdr);
3256 return -EMSGSIZE;
3257}
3258
3259static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
3260 struct netlink_callback *cb)
3261{
a09d1977 3262 struct net *net = skb_sknet(skb);
f131315f
HS
3263 struct netns_ipvs *ipvs = net_ipvs(net);
3264
ae1d48b2 3265 mutex_lock(&ipvs->sync_mutex);
f131315f 3266 if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
9a812198 3267 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
f131315f
HS
3268 ipvs->master_mcast_ifn,
3269 ipvs->master_syncid, cb) < 0)
9a812198
JV
3270 goto nla_put_failure;
3271
3272 cb->args[0] = 1;
3273 }
3274
f131315f 3275 if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
9a812198 3276 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
f131315f
HS
3277 ipvs->backup_mcast_ifn,
3278 ipvs->backup_syncid, cb) < 0)
9a812198
JV
3279 goto nla_put_failure;
3280
3281 cb->args[1] = 1;
3282 }
3283
3284nla_put_failure:
ae1d48b2 3285 mutex_unlock(&ipvs->sync_mutex);
9a812198
JV
3286
3287 return skb->len;
3288}
3289
f131315f 3290static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
9a812198
JV
3291{
3292 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3293 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3294 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3295 return -EINVAL;
3296
f131315f
HS
3297 return start_sync_thread(net,
3298 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
9a812198
JV
3299 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3300 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3301}
3302
f131315f 3303static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
9a812198
JV
3304{
3305 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3306 return -EINVAL;
3307
f131315f
HS
3308 return stop_sync_thread(net,
3309 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
9a812198
JV
3310}
3311
9330419d 3312static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
9a812198
JV
3313{
3314 struct ip_vs_timeout_user t;
3315
9330419d 3316 __ip_vs_get_timeouts(net, &t);
9a812198
JV
3317
3318 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3319 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3320
3321 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3322 t.tcp_fin_timeout =
3323 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3324
3325 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3326 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3327
9330419d 3328 return ip_vs_set_timeout(net, &t);
9a812198
JV
3329}
3330
ae1d48b2 3331static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
9a812198 3332{
9a812198 3333 int ret = 0, cmd;
fc723250 3334 struct net *net;
a0840e2e 3335 struct netns_ipvs *ipvs;
9a812198 3336
fc723250 3337 net = skb_sknet(skb);
a0840e2e 3338 ipvs = net_ipvs(net);
9a812198
JV
3339 cmd = info->genlhdr->cmd;
3340
ae1d48b2 3341 if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
9a812198
JV
3342 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3343
ae1d48b2 3344 mutex_lock(&ipvs->sync_mutex);
9a812198
JV
3345 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3346 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3347 info->attrs[IPVS_CMD_ATTR_DAEMON],
3348 ip_vs_daemon_policy)) {
3349 ret = -EINVAL;
3350 goto out;
3351 }
3352
3353 if (cmd == IPVS_CMD_NEW_DAEMON)
f131315f 3354 ret = ip_vs_genl_new_daemon(net, daemon_attrs);
9a812198 3355 else
f131315f 3356 ret = ip_vs_genl_del_daemon(net, daemon_attrs);
ae1d48b2
HS
3357out:
3358 mutex_unlock(&ipvs->sync_mutex);
3359 }
3360 return ret;
3361}
3362
3363static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3364{
3365 struct ip_vs_service *svc = NULL;
3366 struct ip_vs_service_user_kern usvc;
3367 struct ip_vs_dest_user_kern udest;
3368 int ret = 0, cmd;
3369 int need_full_svc = 0, need_full_dest = 0;
3370 struct net *net;
ae1d48b2
HS
3371
3372 net = skb_sknet(skb);
ae1d48b2
HS
3373 cmd = info->genlhdr->cmd;
3374
3375 mutex_lock(&__ip_vs_mutex);
3376
3377 if (cmd == IPVS_CMD_FLUSH) {
578bc3ef 3378 ret = ip_vs_flush(net, false);
ae1d48b2
HS
3379 goto out;
3380 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3381 ret = ip_vs_genl_set_config(net, info->attrs);
9a812198
JV
3382 goto out;
3383 } else if (cmd == IPVS_CMD_ZERO &&
3384 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
fc723250 3385 ret = ip_vs_zero_all(net);
9a812198
JV
3386 goto out;
3387 }
3388
3389 /* All following commands require a service argument, so check if we
3390 * received a valid one. We need a full service specification when
3391 * adding / editing a service. Only identifying members otherwise. */
3392 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3393 need_full_svc = 1;
3394
fc723250 3395 ret = ip_vs_genl_parse_service(net, &usvc,
9a812198 3396 info->attrs[IPVS_CMD_ATTR_SERVICE],
26c15cfd 3397 need_full_svc, &svc);
9a812198
JV
3398 if (ret)
3399 goto out;
3400
9a812198
JV
3401 /* Unless we're adding a new service, the service must already exist */
3402 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3403 ret = -ESRCH;
3404 goto out;
3405 }
3406
3407 /* Destination commands require a valid destination argument. For
3408 * adding / editing a destination, we need a full destination
3409 * specification. */
3410 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3411 cmd == IPVS_CMD_DEL_DEST) {
3412 if (cmd != IPVS_CMD_DEL_DEST)
3413 need_full_dest = 1;
3414
3415 ret = ip_vs_genl_parse_dest(&udest,
3416 info->attrs[IPVS_CMD_ATTR_DEST],
3417 need_full_dest);
3418 if (ret)
3419 goto out;
3420 }
3421
3422 switch (cmd) {
3423 case IPVS_CMD_NEW_SERVICE:
3424 if (svc == NULL)
fc723250 3425 ret = ip_vs_add_service(net, &usvc, &svc);
9a812198
JV
3426 else
3427 ret = -EEXIST;
3428 break;
3429 case IPVS_CMD_SET_SERVICE:
3430 ret = ip_vs_edit_service(svc, &usvc);
3431 break;
3432 case IPVS_CMD_DEL_SERVICE:
3433 ret = ip_vs_del_service(svc);
26c15cfd 3434 /* do not use svc, it can be freed */
9a812198
JV
3435 break;
3436 case IPVS_CMD_NEW_DEST:
3437 ret = ip_vs_add_dest(svc, &udest);
3438 break;
3439 case IPVS_CMD_SET_DEST:
3440 ret = ip_vs_edit_dest(svc, &udest);
3441 break;
3442 case IPVS_CMD_DEL_DEST:
3443 ret = ip_vs_del_dest(svc, &udest);
3444 break;
3445 case IPVS_CMD_ZERO:
3446 ret = ip_vs_zero_service(svc);
3447 break;
3448 default:
3449 ret = -EINVAL;
3450 }
3451
3452out:
9a812198
JV
3453 mutex_unlock(&__ip_vs_mutex);
3454
3455 return ret;
3456}
3457
3458static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3459{
3460 struct sk_buff *msg;
3461 void *reply;
3462 int ret, cmd, reply_cmd;
fc723250 3463 struct net *net;
9a812198 3464
fc723250 3465 net = skb_sknet(skb);
9a812198
JV
3466 cmd = info->genlhdr->cmd;
3467
3468 if (cmd == IPVS_CMD_GET_SERVICE)
3469 reply_cmd = IPVS_CMD_NEW_SERVICE;
3470 else if (cmd == IPVS_CMD_GET_INFO)
3471 reply_cmd = IPVS_CMD_SET_INFO;
3472 else if (cmd == IPVS_CMD_GET_CONFIG)
3473 reply_cmd = IPVS_CMD_SET_CONFIG;
3474 else {
1e3e238e 3475 pr_err("unknown Generic Netlink command\n");
9a812198
JV
3476 return -EINVAL;
3477 }
3478
3479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3480 if (!msg)
3481 return -ENOMEM;
3482
3483 mutex_lock(&__ip_vs_mutex);
3484
3485 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3486 if (reply == NULL)
3487 goto nla_put_failure;
3488
3489 switch (cmd) {
3490 case IPVS_CMD_GET_SERVICE:
3491 {
3492 struct ip_vs_service *svc;
3493
fc723250
HS
3494 svc = ip_vs_genl_find_service(net,
3495 info->attrs[IPVS_CMD_ATTR_SERVICE]);
9a812198
JV
3496 if (IS_ERR(svc)) {
3497 ret = PTR_ERR(svc);
3498 goto out_err;
3499 } else if (svc) {
3500 ret = ip_vs_genl_fill_service(msg, svc);
9a812198
JV
3501 if (ret)
3502 goto nla_put_failure;
3503 } else {
3504 ret = -ESRCH;
3505 goto out_err;
3506 }
3507
3508 break;
3509 }
3510
3511 case IPVS_CMD_GET_CONFIG:
3512 {
3513 struct ip_vs_timeout_user t;
3514
9330419d 3515 __ip_vs_get_timeouts(net, &t);
9a812198 3516#ifdef CONFIG_IP_VS_PROTO_TCP
969e8e25
DM
3517 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP,
3518 t.tcp_timeout) ||
3519 nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3520 t.tcp_fin_timeout))
3521 goto nla_put_failure;
9a812198
JV
3522#endif
3523#ifdef CONFIG_IP_VS_PROTO_UDP
969e8e25
DM
3524 if (nla_put_u32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout))
3525 goto nla_put_failure;
9a812198
JV
3526#endif
3527
3528 break;
3529 }
3530
3531 case IPVS_CMD_GET_INFO:
969e8e25
DM
3532 if (nla_put_u32(msg, IPVS_INFO_ATTR_VERSION,
3533 IP_VS_VERSION_CODE) ||
3534 nla_put_u32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3535 ip_vs_conn_tab_size))
3536 goto nla_put_failure;
9a812198
JV
3537 break;
3538 }
3539
3540 genlmsg_end(msg, reply);
134e6375 3541 ret = genlmsg_reply(msg, info);
9a812198
JV
3542 goto out;
3543
3544nla_put_failure:
1e3e238e 3545 pr_err("not enough space in Netlink message\n");
9a812198
JV
3546 ret = -EMSGSIZE;
3547
3548out_err:
3549 nlmsg_free(msg);
3550out:
3551 mutex_unlock(&__ip_vs_mutex);
3552
3553 return ret;
3554}
3555
3556
3557static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3558 {
3559 .cmd = IPVS_CMD_NEW_SERVICE,
3560 .flags = GENL_ADMIN_PERM,
3561 .policy = ip_vs_cmd_policy,
3562 .doit = ip_vs_genl_set_cmd,
3563 },
3564 {
3565 .cmd = IPVS_CMD_SET_SERVICE,
3566 .flags = GENL_ADMIN_PERM,
3567 .policy = ip_vs_cmd_policy,
3568 .doit = ip_vs_genl_set_cmd,
3569 },
3570 {
3571 .cmd = IPVS_CMD_DEL_SERVICE,
3572 .flags = GENL_ADMIN_PERM,
3573 .policy = ip_vs_cmd_policy,
3574 .doit = ip_vs_genl_set_cmd,
3575 },
3576 {
3577 .cmd = IPVS_CMD_GET_SERVICE,
3578 .flags = GENL_ADMIN_PERM,
3579 .doit = ip_vs_genl_get_cmd,
3580 .dumpit = ip_vs_genl_dump_services,
3581 .policy = ip_vs_cmd_policy,
3582 },
3583 {
3584 .cmd = IPVS_CMD_NEW_DEST,
3585 .flags = GENL_ADMIN_PERM,
3586 .policy = ip_vs_cmd_policy,
3587 .doit = ip_vs_genl_set_cmd,
3588 },
3589 {
3590 .cmd = IPVS_CMD_SET_DEST,
3591 .flags = GENL_ADMIN_PERM,
3592 .policy = ip_vs_cmd_policy,
3593 .doit = ip_vs_genl_set_cmd,
3594 },
3595 {
3596 .cmd = IPVS_CMD_DEL_DEST,
3597 .flags = GENL_ADMIN_PERM,
3598 .policy = ip_vs_cmd_policy,
3599 .doit = ip_vs_genl_set_cmd,
3600 },
3601 {
3602 .cmd = IPVS_CMD_GET_DEST,
3603 .flags = GENL_ADMIN_PERM,
3604 .policy = ip_vs_cmd_policy,
3605 .dumpit = ip_vs_genl_dump_dests,
3606 },
3607 {
3608 .cmd = IPVS_CMD_NEW_DAEMON,
3609 .flags = GENL_ADMIN_PERM,
3610 .policy = ip_vs_cmd_policy,
ae1d48b2 3611 .doit = ip_vs_genl_set_daemon,
9a812198
JV
3612 },
3613 {
3614 .cmd = IPVS_CMD_DEL_DAEMON,
3615 .flags = GENL_ADMIN_PERM,
3616 .policy = ip_vs_cmd_policy,
ae1d48b2 3617 .doit = ip_vs_genl_set_daemon,
9a812198
JV
3618 },
3619 {
3620 .cmd = IPVS_CMD_GET_DAEMON,
3621 .flags = GENL_ADMIN_PERM,
3622 .dumpit = ip_vs_genl_dump_daemons,
3623 },
3624 {
3625 .cmd = IPVS_CMD_SET_CONFIG,
3626 .flags = GENL_ADMIN_PERM,
3627 .policy = ip_vs_cmd_policy,
3628 .doit = ip_vs_genl_set_cmd,
3629 },
3630 {
3631 .cmd = IPVS_CMD_GET_CONFIG,
3632 .flags = GENL_ADMIN_PERM,
3633 .doit = ip_vs_genl_get_cmd,
3634 },
3635 {
3636 .cmd = IPVS_CMD_GET_INFO,
3637 .flags = GENL_ADMIN_PERM,
3638 .doit = ip_vs_genl_get_cmd,
3639 },
3640 {
3641 .cmd = IPVS_CMD_ZERO,
3642 .flags = GENL_ADMIN_PERM,
3643 .policy = ip_vs_cmd_policy,
3644 .doit = ip_vs_genl_set_cmd,
3645 },
3646 {
3647 .cmd = IPVS_CMD_FLUSH,
3648 .flags = GENL_ADMIN_PERM,
3649 .doit = ip_vs_genl_set_cmd,
3650 },
3651};
3652
3653static int __init ip_vs_genl_register(void)
3654{
8f698d54
MM
3655 return genl_register_family_with_ops(&ip_vs_genl_family,
3656 ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops));
9a812198
JV
3657}
3658
3659static void ip_vs_genl_unregister(void)
3660{
3661 genl_unregister_family(&ip_vs_genl_family);
3662}
3663
3664/* End of Generic Netlink interface definitions */
3665
61b1ab45
HS
3666/*
3667 * per netns intit/exit func.
3668 */
14e40546 3669#ifdef CONFIG_SYSCTL
2b2d2808 3670static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
61b1ab45 3671{
fc723250
HS
3672 int idx;
3673 struct netns_ipvs *ipvs = net_ipvs(net);
a0840e2e 3674 struct ctl_table *tbl;
fc723250 3675
a0840e2e
HS
3676 atomic_set(&ipvs->dropentry, 0);
3677 spin_lock_init(&ipvs->dropentry_lock);
3678 spin_lock_init(&ipvs->droppacket_lock);
3679 spin_lock_init(&ipvs->securetcp_lock);
a0840e2e
HS
3680
3681 if (!net_eq(net, &init_net)) {
3682 tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
3683 if (tbl == NULL)
14e40546 3684 return -ENOMEM;
464dc801
EB
3685
3686 /* Don't export sysctls to unprivileged users */
3687 if (net->user_ns != &init_user_ns)
3688 tbl[0].procname = NULL;
a0840e2e
HS
3689 } else
3690 tbl = vs_vars;
3691 /* Initialize sysctl defaults */
3692 idx = 0;
3693 ipvs->sysctl_amemthresh = 1024;
3694 tbl[idx++].data = &ipvs->sysctl_amemthresh;
3695 ipvs->sysctl_am_droprate = 10;
3696 tbl[idx++].data = &ipvs->sysctl_am_droprate;
3697 tbl[idx++].data = &ipvs->sysctl_drop_entry;
3698 tbl[idx++].data = &ipvs->sysctl_drop_packet;
3699#ifdef CONFIG_IP_VS_NFCT
3700 tbl[idx++].data = &ipvs->sysctl_conntrack;
3701#endif
3702 tbl[idx++].data = &ipvs->sysctl_secure_tcp;
3703 ipvs->sysctl_snat_reroute = 1;
3704 tbl[idx++].data = &ipvs->sysctl_snat_reroute;
3705 ipvs->sysctl_sync_ver = 1;
3706 tbl[idx++].data = &ipvs->sysctl_sync_ver;
f73181c8
PNA
3707 ipvs->sysctl_sync_ports = 1;
3708 tbl[idx++].data = &ipvs->sysctl_sync_ports;
1c003b15
PNA
3709 ipvs->sysctl_sync_qlen_max = nr_free_buffer_pages() / 32;
3710 tbl[idx++].data = &ipvs->sysctl_sync_qlen_max;
3711 ipvs->sysctl_sync_sock_size = 0;
3712 tbl[idx++].data = &ipvs->sysctl_sync_sock_size;
a0840e2e
HS
3713 tbl[idx++].data = &ipvs->sysctl_cache_bypass;
3714 tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
3715 tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
59e0350e
SH
3716 ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
3717 ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
a0840e2e
HS
3718 tbl[idx].data = &ipvs->sysctl_sync_threshold;
3719 tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
749c42b6
JA
3720 ipvs->sysctl_sync_refresh_period = DEFAULT_SYNC_REFRESH_PERIOD;
3721 tbl[idx++].data = &ipvs->sysctl_sync_refresh_period;
3722 ipvs->sysctl_sync_retries = clamp_t(int, DEFAULT_SYNC_RETRIES, 0, 3);
3723 tbl[idx++].data = &ipvs->sysctl_sync_retries;
a0840e2e 3724 tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;
3654e611
JA
3725 ipvs->sysctl_pmtu_disc = 1;
3726 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
0c12582f 3727 tbl[idx++].data = &ipvs->sysctl_backup_only;
a0840e2e
HS
3728
3729
ec8f23ce 3730 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
0443929f
SH
3731 if (ipvs->sysctl_hdr == NULL) {
3732 if (!net_eq(net, &init_net))
3733 kfree(tbl);
14e40546 3734 return -ENOMEM;
0443929f 3735 }
6ef757f9 3736 ip_vs_start_estimator(net, &ipvs->tot_stats);
a0840e2e 3737 ipvs->sysctl_tbl = tbl;
f6340ee0
HS
3738 /* Schedule defense work */
3739 INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
3740 schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
61b1ab45 3741
61b1ab45 3742 return 0;
61b1ab45
HS
3743}
3744
2b2d2808 3745static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net)
61b1ab45 3746{
b17fc996
HS
3747 struct netns_ipvs *ipvs = net_ipvs(net);
3748
f2431e6e
HS
3749 cancel_delayed_work_sync(&ipvs->defense_work);
3750 cancel_work_sync(&ipvs->defense_work.work);
a0840e2e 3751 unregister_net_sysctl_table(ipvs->sysctl_hdr);
14e40546
SH
3752}
3753
3754#else
3755
2b2d2808
CG
3756static int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
3757static void __net_exit ip_vs_control_net_cleanup_sysctl(struct net *net) { }
14e40546 3758
0443929f 3759#endif
14e40546 3760
7a4f0761
HS
3761static struct notifier_block ip_vs_dst_notifier = {
3762 .notifier_call = ip_vs_dst_event,
3763};
3764
503cf15a 3765int __net_init ip_vs_control_net_init(struct net *net)
14e40546
SH
3766{
3767 int idx;
3768 struct netns_ipvs *ipvs = net_ipvs(net);
3769
14e40546
SH
3770 /* Initialize rs_table */
3771 for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
276472ea 3772 INIT_HLIST_HEAD(&ipvs->rs_table[idx]);
14e40546
SH
3773
3774 INIT_LIST_HEAD(&ipvs->dest_trash);
578bc3ef
JA
3775 spin_lock_init(&ipvs->dest_trash_lock);
3776 setup_timer(&ipvs->dest_trash_timer, ip_vs_dest_trash_expire,
3777 (unsigned long) net);
14e40546
SH
3778 atomic_set(&ipvs->ftpsvc_counter, 0);
3779 atomic_set(&ipvs->nullsvc_counter, 0);
3780
3781 /* procfs stats */
3782 ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
0a9ee813 3783 if (!ipvs->tot_stats.cpustats)
14e40546 3784 return -ENOMEM;
0a9ee813 3785
14e40546
SH
3786 spin_lock_init(&ipvs->tot_stats.lock);
3787
d4beaa66
G
3788 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3789 proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3790 proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3791 &ip_vs_stats_percpu_fops);
14e40546 3792
503cf15a 3793 if (ip_vs_control_net_init_sysctl(net))
14e40546
SH
3794 goto err;
3795
3796 return 0;
3797
3798err:
2a0751af 3799 free_percpu(ipvs->tot_stats.cpustats);
61b1ab45
HS
3800 return -ENOMEM;
3801}
3802
503cf15a 3803void __net_exit ip_vs_control_net_cleanup(struct net *net)
61b1ab45 3804{
b17fc996
HS
3805 struct netns_ipvs *ipvs = net_ipvs(net);
3806
578bc3ef
JA
3807 /* Some dest can be in grace period even before cleanup, we have to
3808 * defer ip_vs_trash_cleanup until ip_vs_dest_wait_readers is called.
3809 */
3810 rcu_barrier();
f2431e6e 3811 ip_vs_trash_cleanup(net);
6ef757f9 3812 ip_vs_stop_estimator(net, &ipvs->tot_stats);
503cf15a 3813 ip_vs_control_net_cleanup_sysctl(net);
ece31ffd
G
3814 remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3815 remove_proc_entry("ip_vs_stats", net->proc_net);
3816 remove_proc_entry("ip_vs", net->proc_net);
2a0751af 3817 free_percpu(ipvs->tot_stats.cpustats);
61b1ab45
HS
3818}
3819
8537de8a 3820int __init ip_vs_register_nl_ioctl(void)
1da177e4 3821{
fc723250 3822 int ret;
1da177e4 3823
1da177e4
LT
3824 ret = nf_register_sockopt(&ip_vs_sockopts);
3825 if (ret) {
1e3e238e 3826 pr_err("cannot register sockopt.\n");
7a4f0761 3827 goto err_sock;
1da177e4
LT
3828 }
3829
9a812198
JV
3830 ret = ip_vs_genl_register();
3831 if (ret) {
1e3e238e 3832 pr_err("cannot register Generic Netlink interface.\n");
7a4f0761 3833 goto err_genl;
9a812198 3834 }
1da177e4 3835 return 0;
fc723250 3836
7a4f0761
HS
3837err_genl:
3838 nf_unregister_sockopt(&ip_vs_sockopts);
3839err_sock:
fc723250 3840 return ret;
1da177e4
LT
3841}
3842
8537de8a
HS
3843void ip_vs_unregister_nl_ioctl(void)
3844{
3845 ip_vs_genl_unregister();
3846 nf_unregister_sockopt(&ip_vs_sockopts);
3847}
3848
3849int __init ip_vs_control_init(void)
3850{
3851 int idx;
3852 int ret;
3853
3854 EnterFunction(2);
3855
276472ea 3856 /* Initialize svc_table, ip_vs_svc_fwm_table */
8537de8a 3857 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
ceec4c38
JA
3858 INIT_HLIST_HEAD(&ip_vs_svc_table[idx]);
3859 INIT_HLIST_HEAD(&ip_vs_svc_fwm_table[idx]);
8537de8a
HS
3860 }
3861
3862 smp_wmb(); /* Do we really need it now ? */
3863
3864 ret = register_netdevice_notifier(&ip_vs_dst_notifier);
3865 if (ret < 0)
3866 return ret;
3867
3868 LeaveFunction(2);
3869 return 0;
3870}
3871
1da177e4
LT
3872
3873void ip_vs_control_cleanup(void)
3874{
3875 EnterFunction(2);
7676e345 3876 unregister_netdevice_notifier(&ip_vs_dst_notifier);
1da177e4
LT
3877 LeaveFunction(2);
3878}
This page took 1.010184 seconds and 5 git commands to generate.