]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/ipvs/ip_vs_ctl.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/horms/lvs-2.6 into lvs-next-2.6
[net-next-2.6.git] / net / ipv4 / 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
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/types.h>
4fc268d2 24#include <linux/capability.h>
1da177e4
LT
25#include <linux/fs.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/swap.h>
1da177e4
LT
30#include <linux/seq_file.h>
31
32#include <linux/netfilter.h>
33#include <linux/netfilter_ipv4.h>
14cc3e2b 34#include <linux/mutex.h>
1da177e4 35
457c4cbc 36#include <net/net_namespace.h>
1da177e4 37#include <net/ip.h>
09571c7a
VB
38#ifdef CONFIG_IP_VS_IPV6
39#include <net/ipv6.h>
40#include <net/ip6_route.h>
41#endif
14c85021 42#include <net/route.h>
1da177e4 43#include <net/sock.h>
9a812198 44#include <net/genetlink.h>
1da177e4
LT
45
46#include <asm/uaccess.h>
47
48#include <net/ip_vs.h>
49
50/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
14cc3e2b 51static DEFINE_MUTEX(__ip_vs_mutex);
1da177e4
LT
52
53/* lock for service table */
54static DEFINE_RWLOCK(__ip_vs_svc_lock);
55
56/* lock for table with the real services */
57static DEFINE_RWLOCK(__ip_vs_rs_lock);
58
59/* lock for state and timeout tables */
60static DEFINE_RWLOCK(__ip_vs_securetcp_lock);
61
62/* lock for drop entry handling */
63static DEFINE_SPINLOCK(__ip_vs_dropentry_lock);
64
65/* lock for drop packet handling */
66static DEFINE_SPINLOCK(__ip_vs_droppacket_lock);
67
68/* 1/rate drop and drop-entry variables */
69int ip_vs_drop_rate = 0;
70int ip_vs_drop_counter = 0;
71static atomic_t ip_vs_dropentry = ATOMIC_INIT(0);
72
73/* number of virtual services */
74static int ip_vs_num_services = 0;
75
76/* sysctl variables */
77static int sysctl_ip_vs_drop_entry = 0;
78static int sysctl_ip_vs_drop_packet = 0;
79static int sysctl_ip_vs_secure_tcp = 0;
80static int sysctl_ip_vs_amemthresh = 1024;
81static int sysctl_ip_vs_am_droprate = 10;
82int sysctl_ip_vs_cache_bypass = 0;
83int sysctl_ip_vs_expire_nodest_conn = 0;
84int sysctl_ip_vs_expire_quiescent_template = 0;
85int sysctl_ip_vs_sync_threshold[2] = { 3, 50 };
86int sysctl_ip_vs_nat_icmp_send = 0;
87
88
89#ifdef CONFIG_IP_VS_DEBUG
90static int sysctl_ip_vs_debug_level = 0;
91
92int ip_vs_get_debug_level(void)
93{
94 return sysctl_ip_vs_debug_level;
95}
96#endif
97
09571c7a
VB
98#ifdef CONFIG_IP_VS_IPV6
99/* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
100static int __ip_vs_addr_is_local_v6(const struct in6_addr *addr)
101{
102 struct rt6_info *rt;
103 struct flowi fl = {
104 .oif = 0,
105 .nl_u = {
106 .ip6_u = {
107 .daddr = *addr,
108 .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
109 };
110
111 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
112 if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
113 return 1;
114
115 return 0;
116}
117#endif
1da177e4 118/*
af9debd4
JA
119 * update_defense_level is called from keventd and from sysctl,
120 * so it needs to protect itself from softirqs
1da177e4
LT
121 */
122static void update_defense_level(void)
123{
124 struct sysinfo i;
125 static int old_secure_tcp = 0;
126 int availmem;
127 int nomem;
128 int to_change = -1;
129
130 /* we only count free and buffered memory (in pages) */
131 si_meminfo(&i);
132 availmem = i.freeram + i.bufferram;
133 /* however in linux 2.5 the i.bufferram is total page cache size,
134 we need adjust it */
135 /* si_swapinfo(&i); */
136 /* availmem = availmem - (i.totalswap - i.freeswap); */
137
138 nomem = (availmem < sysctl_ip_vs_amemthresh);
139
af9debd4
JA
140 local_bh_disable();
141
1da177e4
LT
142 /* drop_entry */
143 spin_lock(&__ip_vs_dropentry_lock);
144 switch (sysctl_ip_vs_drop_entry) {
145 case 0:
146 atomic_set(&ip_vs_dropentry, 0);
147 break;
148 case 1:
149 if (nomem) {
150 atomic_set(&ip_vs_dropentry, 1);
151 sysctl_ip_vs_drop_entry = 2;
152 } else {
153 atomic_set(&ip_vs_dropentry, 0);
154 }
155 break;
156 case 2:
157 if (nomem) {
158 atomic_set(&ip_vs_dropentry, 1);
159 } else {
160 atomic_set(&ip_vs_dropentry, 0);
161 sysctl_ip_vs_drop_entry = 1;
162 };
163 break;
164 case 3:
165 atomic_set(&ip_vs_dropentry, 1);
166 break;
167 }
168 spin_unlock(&__ip_vs_dropentry_lock);
169
170 /* drop_packet */
171 spin_lock(&__ip_vs_droppacket_lock);
172 switch (sysctl_ip_vs_drop_packet) {
173 case 0:
174 ip_vs_drop_rate = 0;
175 break;
176 case 1:
177 if (nomem) {
178 ip_vs_drop_rate = ip_vs_drop_counter
179 = sysctl_ip_vs_amemthresh /
180 (sysctl_ip_vs_amemthresh-availmem);
181 sysctl_ip_vs_drop_packet = 2;
182 } else {
183 ip_vs_drop_rate = 0;
184 }
185 break;
186 case 2:
187 if (nomem) {
188 ip_vs_drop_rate = ip_vs_drop_counter
189 = sysctl_ip_vs_amemthresh /
190 (sysctl_ip_vs_amemthresh-availmem);
191 } else {
192 ip_vs_drop_rate = 0;
193 sysctl_ip_vs_drop_packet = 1;
194 }
195 break;
196 case 3:
197 ip_vs_drop_rate = sysctl_ip_vs_am_droprate;
198 break;
199 }
200 spin_unlock(&__ip_vs_droppacket_lock);
201
202 /* secure_tcp */
203 write_lock(&__ip_vs_securetcp_lock);
204 switch (sysctl_ip_vs_secure_tcp) {
205 case 0:
206 if (old_secure_tcp >= 2)
207 to_change = 0;
208 break;
209 case 1:
210 if (nomem) {
211 if (old_secure_tcp < 2)
212 to_change = 1;
213 sysctl_ip_vs_secure_tcp = 2;
214 } else {
215 if (old_secure_tcp >= 2)
216 to_change = 0;
217 }
218 break;
219 case 2:
220 if (nomem) {
221 if (old_secure_tcp < 2)
222 to_change = 1;
223 } else {
224 if (old_secure_tcp >= 2)
225 to_change = 0;
226 sysctl_ip_vs_secure_tcp = 1;
227 }
228 break;
229 case 3:
230 if (old_secure_tcp < 2)
231 to_change = 1;
232 break;
233 }
234 old_secure_tcp = sysctl_ip_vs_secure_tcp;
235 if (to_change >= 0)
236 ip_vs_protocol_timeout_change(sysctl_ip_vs_secure_tcp>1);
237 write_unlock(&__ip_vs_securetcp_lock);
af9debd4
JA
238
239 local_bh_enable();
1da177e4
LT
240}
241
242
243/*
244 * Timer for checking the defense
245 */
246#define DEFENSE_TIMER_PERIOD 1*HZ
c4028958
DH
247static void defense_work_handler(struct work_struct *work);
248static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
1da177e4 249
c4028958 250static void defense_work_handler(struct work_struct *work)
1da177e4
LT
251{
252 update_defense_level();
253 if (atomic_read(&ip_vs_dropentry))
254 ip_vs_random_dropentry();
255
256 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
257}
258
259int
260ip_vs_use_count_inc(void)
261{
262 return try_module_get(THIS_MODULE);
263}
264
265void
266ip_vs_use_count_dec(void)
267{
268 module_put(THIS_MODULE);
269}
270
271
272/*
273 * Hash table: for virtual service lookups
274 */
275#define IP_VS_SVC_TAB_BITS 8
276#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
277#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)
278
279/* the service table hashed by <protocol, addr, port> */
280static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
281/* the service table hashed by fwmark */
282static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];
283
284/*
285 * Hash table: for real service lookups
286 */
287#define IP_VS_RTAB_BITS 4
288#define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
289#define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
290
291static struct list_head ip_vs_rtable[IP_VS_RTAB_SIZE];
292
293/*
294 * Trash for destinations
295 */
296static LIST_HEAD(ip_vs_dest_trash);
297
298/*
299 * FTP & NULL virtual service counters
300 */
301static atomic_t ip_vs_ftpsvc_counter = ATOMIC_INIT(0);
302static atomic_t ip_vs_nullsvc_counter = ATOMIC_INIT(0);
303
304
305/*
306 * Returns hash value for virtual service
307 */
308static __inline__ unsigned
b18610de
JV
309ip_vs_svc_hashkey(int af, unsigned proto, const union nf_inet_addr *addr,
310 __be16 port)
1da177e4
LT
311{
312 register unsigned porth = ntohs(port);
b18610de 313 __be32 addr_fold = addr->ip;
1da177e4 314
b18610de
JV
315#ifdef CONFIG_IP_VS_IPV6
316 if (af == AF_INET6)
317 addr_fold = addr->ip6[0]^addr->ip6[1]^
318 addr->ip6[2]^addr->ip6[3];
319#endif
320
321 return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
1da177e4
LT
322 & IP_VS_SVC_TAB_MASK;
323}
324
325/*
326 * Returns hash value of fwmark for virtual service lookup
327 */
328static __inline__ unsigned ip_vs_svc_fwm_hashkey(__u32 fwmark)
329{
330 return fwmark & IP_VS_SVC_TAB_MASK;
331}
332
333/*
334 * Hashes a service in the ip_vs_svc_table by <proto,addr,port>
335 * or in the ip_vs_svc_fwm_table by fwmark.
336 * Should be called with locked tables.
337 */
338static int ip_vs_svc_hash(struct ip_vs_service *svc)
339{
340 unsigned hash;
341
342 if (svc->flags & IP_VS_SVC_F_HASHED) {
343 IP_VS_ERR("ip_vs_svc_hash(): request for already hashed, "
344 "called from %p\n", __builtin_return_address(0));
345 return 0;
346 }
347
348 if (svc->fwmark == 0) {
349 /*
350 * Hash it by <protocol,addr,port> in ip_vs_svc_table
351 */
b18610de 352 hash = ip_vs_svc_hashkey(svc->af, svc->protocol, &svc->addr,
e7ade46a 353 svc->port);
1da177e4
LT
354 list_add(&svc->s_list, &ip_vs_svc_table[hash]);
355 } else {
356 /*
357 * Hash it by fwmark in ip_vs_svc_fwm_table
358 */
359 hash = ip_vs_svc_fwm_hashkey(svc->fwmark);
360 list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
361 }
362
363 svc->flags |= IP_VS_SVC_F_HASHED;
364 /* increase its refcnt because it is referenced by the svc table */
365 atomic_inc(&svc->refcnt);
366 return 1;
367}
368
369
370/*
371 * Unhashes a service from ip_vs_svc_table/ip_vs_svc_fwm_table.
372 * Should be called with locked tables.
373 */
374static int ip_vs_svc_unhash(struct ip_vs_service *svc)
375{
376 if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
377 IP_VS_ERR("ip_vs_svc_unhash(): request for unhash flagged, "
378 "called from %p\n", __builtin_return_address(0));
379 return 0;
380 }
381
382 if (svc->fwmark == 0) {
383 /* Remove it from the ip_vs_svc_table table */
384 list_del(&svc->s_list);
385 } else {
386 /* Remove it from the ip_vs_svc_fwm_table table */
387 list_del(&svc->f_list);
388 }
389
390 svc->flags &= ~IP_VS_SVC_F_HASHED;
391 atomic_dec(&svc->refcnt);
392 return 1;
393}
394
395
396/*
397 * Get service by {proto,addr,port} in the service table.
398 */
b18610de
JV
399static inline struct ip_vs_service *
400__ip_vs_service_get(int af, __u16 protocol, const union nf_inet_addr *vaddr,
401 __be16 vport)
1da177e4
LT
402{
403 unsigned hash;
404 struct ip_vs_service *svc;
405
406 /* Check for "full" addressed entries */
b18610de 407 hash = ip_vs_svc_hashkey(af, protocol, vaddr, vport);
1da177e4
LT
408
409 list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
b18610de
JV
410 if ((svc->af == af)
411 && ip_vs_addr_equal(af, &svc->addr, vaddr)
1da177e4
LT
412 && (svc->port == vport)
413 && (svc->protocol == protocol)) {
414 /* HIT */
415 atomic_inc(&svc->usecnt);
416 return svc;
417 }
418 }
419
420 return NULL;
421}
422
423
424/*
425 * Get service by {fwmark} in the service table.
426 */
b18610de
JV
427static inline struct ip_vs_service *
428__ip_vs_svc_fwm_get(int af, __u32 fwmark)
1da177e4
LT
429{
430 unsigned hash;
431 struct ip_vs_service *svc;
432
433 /* Check for fwmark addressed entries */
434 hash = ip_vs_svc_fwm_hashkey(fwmark);
435
436 list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) {
b18610de 437 if (svc->fwmark == fwmark && svc->af == af) {
1da177e4
LT
438 /* HIT */
439 atomic_inc(&svc->usecnt);
440 return svc;
441 }
442 }
443
444 return NULL;
445}
446
447struct ip_vs_service *
3c2e0505
JV
448ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
449 const union nf_inet_addr *vaddr, __be16 vport)
1da177e4
LT
450{
451 struct ip_vs_service *svc;
3c2e0505 452
1da177e4
LT
453 read_lock(&__ip_vs_svc_lock);
454
455 /*
456 * Check the table hashed by fwmark first
457 */
3c2e0505 458 if (fwmark && (svc = __ip_vs_svc_fwm_get(af, fwmark)))
1da177e4
LT
459 goto out;
460
461 /*
462 * Check the table hashed by <protocol,addr,port>
463 * for "full" addressed entries
464 */
3c2e0505 465 svc = __ip_vs_service_get(af, protocol, vaddr, vport);
1da177e4
LT
466
467 if (svc == NULL
468 && protocol == IPPROTO_TCP
469 && atomic_read(&ip_vs_ftpsvc_counter)
470 && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
471 /*
472 * Check if ftp service entry exists, the packet
473 * might belong to FTP data connections.
474 */
3c2e0505 475 svc = __ip_vs_service_get(af, protocol, vaddr, FTPPORT);
1da177e4
LT
476 }
477
478 if (svc == NULL
479 && atomic_read(&ip_vs_nullsvc_counter)) {
480 /*
481 * Check if the catch-all port (port zero) exists
482 */
3c2e0505 483 svc = __ip_vs_service_get(af, protocol, vaddr, 0);
1da177e4
LT
484 }
485
486 out:
487 read_unlock(&__ip_vs_svc_lock);
488
3c2e0505
JV
489 IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
490 fwmark, ip_vs_proto_name(protocol),
491 IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
492 svc ? "hit" : "not hit");
1da177e4
LT
493
494 return svc;
495}
496
497
498static inline void
499__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
500{
501 atomic_inc(&svc->refcnt);
502 dest->svc = svc;
503}
504
505static inline void
506__ip_vs_unbind_svc(struct ip_vs_dest *dest)
507{
508 struct ip_vs_service *svc = dest->svc;
509
510 dest->svc = NULL;
511 if (atomic_dec_and_test(&svc->refcnt))
512 kfree(svc);
513}
514
515
516/*
517 * Returns hash value for real service
518 */
7937df15
JV
519static inline unsigned ip_vs_rs_hashkey(int af,
520 const union nf_inet_addr *addr,
521 __be16 port)
1da177e4
LT
522{
523 register unsigned porth = ntohs(port);
7937df15
JV
524 __be32 addr_fold = addr->ip;
525
526#ifdef CONFIG_IP_VS_IPV6
527 if (af == AF_INET6)
528 addr_fold = addr->ip6[0]^addr->ip6[1]^
529 addr->ip6[2]^addr->ip6[3];
530#endif
1da177e4 531
7937df15 532 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
1da177e4
LT
533 & IP_VS_RTAB_MASK;
534}
535
536/*
537 * Hashes ip_vs_dest in ip_vs_rtable by <proto,addr,port>.
538 * should be called with locked tables.
539 */
540static int ip_vs_rs_hash(struct ip_vs_dest *dest)
541{
542 unsigned hash;
543
544 if (!list_empty(&dest->d_list)) {
545 return 0;
546 }
547
548 /*
549 * Hash by proto,addr,port,
550 * which are the parameters of the real service.
551 */
7937df15
JV
552 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
553
1da177e4
LT
554 list_add(&dest->d_list, &ip_vs_rtable[hash]);
555
556 return 1;
557}
558
559/*
560 * UNhashes ip_vs_dest from ip_vs_rtable.
561 * should be called with locked tables.
562 */
563static int ip_vs_rs_unhash(struct ip_vs_dest *dest)
564{
565 /*
566 * Remove it from the ip_vs_rtable table.
567 */
568 if (!list_empty(&dest->d_list)) {
569 list_del(&dest->d_list);
570 INIT_LIST_HEAD(&dest->d_list);
571 }
572
573 return 1;
574}
575
576/*
577 * Lookup real service by <proto,addr,port> in the real service table.
578 */
579struct ip_vs_dest *
7937df15
JV
580ip_vs_lookup_real_service(int af, __u16 protocol,
581 const union nf_inet_addr *daddr,
582 __be16 dport)
1da177e4
LT
583{
584 unsigned hash;
585 struct ip_vs_dest *dest;
586
587 /*
588 * Check for "full" addressed entries
589 * Return the first found entry
590 */
7937df15 591 hash = ip_vs_rs_hashkey(af, daddr, dport);
1da177e4
LT
592
593 read_lock(&__ip_vs_rs_lock);
594 list_for_each_entry(dest, &ip_vs_rtable[hash], d_list) {
7937df15
JV
595 if ((dest->af == af)
596 && ip_vs_addr_equal(af, &dest->addr, daddr)
1da177e4
LT
597 && (dest->port == dport)
598 && ((dest->protocol == protocol) ||
599 dest->vfwmark)) {
600 /* HIT */
601 read_unlock(&__ip_vs_rs_lock);
602 return dest;
603 }
604 }
605 read_unlock(&__ip_vs_rs_lock);
606
607 return NULL;
608}
609
610/*
611 * Lookup destination by {addr,port} in the given service
612 */
613static struct ip_vs_dest *
7937df15
JV
614ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
615 __be16 dport)
1da177e4
LT
616{
617 struct ip_vs_dest *dest;
618
619 /*
620 * Find the destination for the given service
621 */
622 list_for_each_entry(dest, &svc->destinations, n_list) {
7937df15
JV
623 if ((dest->af == svc->af)
624 && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
625 && (dest->port == dport)) {
1da177e4
LT
626 /* HIT */
627 return dest;
628 }
629 }
630
631 return NULL;
632}
633
1e356f9c
RB
634/*
635 * Find destination by {daddr,dport,vaddr,protocol}
636 * Cretaed to be used in ip_vs_process_message() in
637 * the backup synchronization daemon. It finds the
638 * destination to be bound to the received connection
639 * on the backup.
640 *
641 * ip_vs_lookup_real_service() looked promissing, but
642 * seems not working as expected.
643 */
7937df15
JV
644struct ip_vs_dest *ip_vs_find_dest(int af, const union nf_inet_addr *daddr,
645 __be16 dport,
646 const union nf_inet_addr *vaddr,
647 __be16 vport, __u16 protocol)
1e356f9c
RB
648{
649 struct ip_vs_dest *dest;
650 struct ip_vs_service *svc;
651
7937df15 652 svc = ip_vs_service_get(af, 0, protocol, vaddr, vport);
1e356f9c
RB
653 if (!svc)
654 return NULL;
655 dest = ip_vs_lookup_dest(svc, daddr, dport);
656 if (dest)
657 atomic_inc(&dest->refcnt);
658 ip_vs_service_put(svc);
659 return dest;
660}
1da177e4
LT
661
662/*
663 * Lookup dest by {svc,addr,port} in the destination trash.
664 * The destination trash is used to hold the destinations that are removed
665 * from the service table but are still referenced by some conn entries.
666 * The reason to add the destination trash is when the dest is temporary
667 * down (either by administrator or by monitor program), the dest can be
668 * picked back from the trash, the remaining connections to the dest can
669 * continue, and the counting information of the dest is also useful for
670 * scheduling.
671 */
672static struct ip_vs_dest *
7937df15
JV
673ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
674 __be16 dport)
1da177e4
LT
675{
676 struct ip_vs_dest *dest, *nxt;
677
678 /*
679 * Find the destination in trash
680 */
681 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
7937df15
JV
682 IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
683 "dest->refcnt=%d\n",
684 dest->vfwmark,
685 IP_VS_DBG_ADDR(svc->af, &dest->addr),
686 ntohs(dest->port),
687 atomic_read(&dest->refcnt));
688 if (dest->af == svc->af &&
689 ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
1da177e4
LT
690 dest->port == dport &&
691 dest->vfwmark == svc->fwmark &&
692 dest->protocol == svc->protocol &&
693 (svc->fwmark ||
7937df15 694 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
1da177e4
LT
695 dest->vport == svc->port))) {
696 /* HIT */
697 return dest;
698 }
699
700 /*
701 * Try to purge the destination from trash if not referenced
702 */
703 if (atomic_read(&dest->refcnt) == 1) {
7937df15
JV
704 IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
705 "from trash\n",
706 dest->vfwmark,
707 IP_VS_DBG_ADDR(svc->af, &dest->addr),
708 ntohs(dest->port));
1da177e4
LT
709 list_del(&dest->n_list);
710 ip_vs_dst_reset(dest);
711 __ip_vs_unbind_svc(dest);
712 kfree(dest);
713 }
714 }
715
716 return NULL;
717}
718
719
720/*
721 * Clean up all the destinations in the trash
722 * Called by the ip_vs_control_cleanup()
723 *
724 * When the ip_vs_control_clearup is activated by ipvs module exit,
725 * the service tables must have been flushed and all the connections
726 * are expired, and the refcnt of each destination in the trash must
727 * be 1, so we simply release them here.
728 */
729static void ip_vs_trash_cleanup(void)
730{
731 struct ip_vs_dest *dest, *nxt;
732
733 list_for_each_entry_safe(dest, nxt, &ip_vs_dest_trash, n_list) {
734 list_del(&dest->n_list);
735 ip_vs_dst_reset(dest);
736 __ip_vs_unbind_svc(dest);
737 kfree(dest);
738 }
739}
740
741
742static void
743ip_vs_zero_stats(struct ip_vs_stats *stats)
744{
745 spin_lock_bh(&stats->lock);
e93615d0 746
e9c0ce23 747 memset(&stats->ustats, 0, sizeof(stats->ustats));
1da177e4 748 ip_vs_zero_estimator(stats);
e93615d0 749
3a14a313 750 spin_unlock_bh(&stats->lock);
1da177e4
LT
751}
752
753/*
754 * Update a destination in the given service
755 */
756static void
757__ip_vs_update_dest(struct ip_vs_service *svc,
c860c6b1 758 struct ip_vs_dest *dest, struct ip_vs_dest_user_kern *udest)
1da177e4
LT
759{
760 int conn_flags;
761
762 /* set the weight and the flags */
763 atomic_set(&dest->weight, udest->weight);
764 conn_flags = udest->conn_flags | IP_VS_CONN_F_INACTIVE;
765
766 /* check if local node and update the flags */
09571c7a
VB
767#ifdef CONFIG_IP_VS_IPV6
768 if (svc->af == AF_INET6) {
769 if (__ip_vs_addr_is_local_v6(&udest->addr.in6)) {
770 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
771 | IP_VS_CONN_F_LOCALNODE;
772 }
773 } else
774#endif
775 if (inet_addr_type(&init_net, udest->addr.ip) == RTN_LOCAL) {
776 conn_flags = (conn_flags & ~IP_VS_CONN_F_FWD_MASK)
777 | IP_VS_CONN_F_LOCALNODE;
778 }
1da177e4
LT
779
780 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
781 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != 0) {
782 conn_flags |= IP_VS_CONN_F_NOOUTPUT;
783 } else {
784 /*
785 * Put the real service in ip_vs_rtable if not present.
786 * For now only for NAT!
787 */
788 write_lock_bh(&__ip_vs_rs_lock);
789 ip_vs_rs_hash(dest);
790 write_unlock_bh(&__ip_vs_rs_lock);
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;
812}
813
814
815/*
816 * Create a destination for the given service
817 */
818static int
c860c6b1 819ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
1da177e4
LT
820 struct ip_vs_dest **dest_p)
821{
822 struct ip_vs_dest *dest;
823 unsigned atype;
824
825 EnterFunction(2);
826
09571c7a
VB
827#ifdef CONFIG_IP_VS_IPV6
828 if (svc->af == AF_INET6) {
829 atype = ipv6_addr_type(&udest->addr.in6);
3bfb92f4
SW
830 if ((!(atype & IPV6_ADDR_UNICAST) ||
831 atype & IPV6_ADDR_LINKLOCAL) &&
09571c7a
VB
832 !__ip_vs_addr_is_local_v6(&udest->addr.in6))
833 return -EINVAL;
834 } else
835#endif
836 {
837 atype = inet_addr_type(&init_net, udest->addr.ip);
838 if (atype != RTN_LOCAL && atype != RTN_UNICAST)
839 return -EINVAL;
840 }
1da177e4 841
0da974f4 842 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_ATOMIC);
1da177e4
LT
843 if (dest == NULL) {
844 IP_VS_ERR("ip_vs_new_dest: kmalloc failed.\n");
845 return -ENOMEM;
846 }
1da177e4 847
c860c6b1 848 dest->af = svc->af;
1da177e4 849 dest->protocol = svc->protocol;
c860c6b1 850 dest->vaddr = svc->addr;
1da177e4
LT
851 dest->vport = svc->port;
852 dest->vfwmark = svc->fwmark;
c860c6b1 853 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
1da177e4
LT
854 dest->port = udest->port;
855
856 atomic_set(&dest->activeconns, 0);
857 atomic_set(&dest->inactconns, 0);
858 atomic_set(&dest->persistconns, 0);
859 atomic_set(&dest->refcnt, 0);
860
861 INIT_LIST_HEAD(&dest->d_list);
862 spin_lock_init(&dest->dst_lock);
863 spin_lock_init(&dest->stats.lock);
864 __ip_vs_update_dest(svc, dest, udest);
865 ip_vs_new_estimator(&dest->stats);
866
867 *dest_p = dest;
868
869 LeaveFunction(2);
870 return 0;
871}
872
873
874/*
875 * Add a destination into an existing service
876 */
877static int
c860c6b1 878ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1da177e4
LT
879{
880 struct ip_vs_dest *dest;
c860c6b1 881 union nf_inet_addr daddr;
014d730d 882 __be16 dport = udest->port;
1da177e4
LT
883 int ret;
884
885 EnterFunction(2);
886
887 if (udest->weight < 0) {
888 IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
889 return -ERANGE;
890 }
891
892 if (udest->l_threshold > udest->u_threshold) {
893 IP_VS_ERR("ip_vs_add_dest(): lower threshold is higher than "
894 "upper threshold\n");
895 return -ERANGE;
896 }
897
c860c6b1
JV
898 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
899
1da177e4
LT
900 /*
901 * Check if the dest already exists in the list
902 */
7937df15
JV
903 dest = ip_vs_lookup_dest(svc, &daddr, dport);
904
1da177e4
LT
905 if (dest != NULL) {
906 IP_VS_DBG(1, "ip_vs_add_dest(): dest already exists\n");
907 return -EEXIST;
908 }
909
910 /*
911 * Check if the dest already exists in the trash and
912 * is from the same service
913 */
7937df15
JV
914 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
915
1da177e4 916 if (dest != NULL) {
cfc78c5a
JV
917 IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
918 "dest->refcnt=%d, service %u/%s:%u\n",
919 IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
920 atomic_read(&dest->refcnt),
921 dest->vfwmark,
922 IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
923 ntohs(dest->vport));
924
1da177e4
LT
925 __ip_vs_update_dest(svc, dest, udest);
926
927 /*
928 * Get the destination from the trash
929 */
930 list_del(&dest->n_list);
931
932 ip_vs_new_estimator(&dest->stats);
933
934 write_lock_bh(&__ip_vs_svc_lock);
935
936 /*
937 * Wait until all other svc users go away.
938 */
939 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
940
941 list_add(&dest->n_list, &svc->destinations);
942 svc->num_dests++;
943
944 /* call the update_service function of its scheduler */
82dfb6f3
SW
945 if (svc->scheduler->update_service)
946 svc->scheduler->update_service(svc);
1da177e4
LT
947
948 write_unlock_bh(&__ip_vs_svc_lock);
949 return 0;
950 }
951
952 /*
953 * Allocate and initialize the dest structure
954 */
955 ret = ip_vs_new_dest(svc, udest, &dest);
956 if (ret) {
957 return ret;
958 }
959
960 /*
961 * Add the dest entry into the list
962 */
963 atomic_inc(&dest->refcnt);
964
965 write_lock_bh(&__ip_vs_svc_lock);
966
967 /*
968 * Wait until all other svc users go away.
969 */
970 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
971
972 list_add(&dest->n_list, &svc->destinations);
973 svc->num_dests++;
974
975 /* call the update_service function of its scheduler */
82dfb6f3
SW
976 if (svc->scheduler->update_service)
977 svc->scheduler->update_service(svc);
1da177e4
LT
978
979 write_unlock_bh(&__ip_vs_svc_lock);
980
981 LeaveFunction(2);
982
983 return 0;
984}
985
986
987/*
988 * Edit a destination in the given service
989 */
990static int
c860c6b1 991ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1da177e4
LT
992{
993 struct ip_vs_dest *dest;
c860c6b1 994 union nf_inet_addr daddr;
014d730d 995 __be16 dport = udest->port;
1da177e4
LT
996
997 EnterFunction(2);
998
999 if (udest->weight < 0) {
1000 IP_VS_ERR("ip_vs_edit_dest(): server weight less than zero\n");
1001 return -ERANGE;
1002 }
1003
1004 if (udest->l_threshold > udest->u_threshold) {
1005 IP_VS_ERR("ip_vs_edit_dest(): lower threshold is higher than "
1006 "upper threshold\n");
1007 return -ERANGE;
1008 }
1009
c860c6b1
JV
1010 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
1011
1da177e4
LT
1012 /*
1013 * Lookup the destination list
1014 */
7937df15
JV
1015 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1016
1da177e4
LT
1017 if (dest == NULL) {
1018 IP_VS_DBG(1, "ip_vs_edit_dest(): dest doesn't exist\n");
1019 return -ENOENT;
1020 }
1021
1022 __ip_vs_update_dest(svc, dest, udest);
1023
1024 write_lock_bh(&__ip_vs_svc_lock);
1025
1026 /* Wait until all other svc users go away */
cae7ca3d 1027 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1da177e4
LT
1028
1029 /* call the update_service, because server weight may be changed */
82dfb6f3
SW
1030 if (svc->scheduler->update_service)
1031 svc->scheduler->update_service(svc);
1da177e4
LT
1032
1033 write_unlock_bh(&__ip_vs_svc_lock);
1034
1035 LeaveFunction(2);
1036
1037 return 0;
1038}
1039
1040
1041/*
1042 * Delete a destination (must be already unlinked from the service)
1043 */
1044static void __ip_vs_del_dest(struct ip_vs_dest *dest)
1045{
1046 ip_vs_kill_estimator(&dest->stats);
1047
1048 /*
1049 * Remove it from the d-linked list with the real services.
1050 */
1051 write_lock_bh(&__ip_vs_rs_lock);
1052 ip_vs_rs_unhash(dest);
1053 write_unlock_bh(&__ip_vs_rs_lock);
1054
1055 /*
1056 * Decrease the refcnt of the dest, and free the dest
1057 * if nobody refers to it (refcnt=0). Otherwise, throw
1058 * the destination into the trash.
1059 */
1060 if (atomic_dec_and_test(&dest->refcnt)) {
1061 ip_vs_dst_reset(dest);
1062 /* simply decrease svc->refcnt here, let the caller check
1063 and release the service if nobody refers to it.
1064 Only user context can release destination and service,
1065 and only one user context can update virtual service at a
1066 time, so the operation here is OK */
1067 atomic_dec(&dest->svc->refcnt);
1068 kfree(dest);
1069 } else {
cfc78c5a
JV
1070 IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
1071 "dest->refcnt=%d\n",
1072 IP_VS_DBG_ADDR(dest->af, &dest->addr),
1073 ntohs(dest->port),
1074 atomic_read(&dest->refcnt));
1da177e4
LT
1075 list_add(&dest->n_list, &ip_vs_dest_trash);
1076 atomic_inc(&dest->refcnt);
1077 }
1078}
1079
1080
1081/*
1082 * Unlink a destination from the given service
1083 */
1084static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
1085 struct ip_vs_dest *dest,
1086 int svcupd)
1087{
1088 dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
1089
1090 /*
1091 * Remove it from the d-linked destination list.
1092 */
1093 list_del(&dest->n_list);
1094 svc->num_dests--;
82dfb6f3
SW
1095
1096 /*
1097 * Call the update_service function of its scheduler
1098 */
1099 if (svcupd && svc->scheduler->update_service)
1100 svc->scheduler->update_service(svc);
1da177e4
LT
1101}
1102
1103
1104/*
1105 * Delete a destination server in the given service
1106 */
1107static int
c860c6b1 1108ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
1da177e4
LT
1109{
1110 struct ip_vs_dest *dest;
014d730d 1111 __be16 dport = udest->port;
1da177e4
LT
1112
1113 EnterFunction(2);
1114
7937df15 1115 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
c860c6b1 1116
1da177e4
LT
1117 if (dest == NULL) {
1118 IP_VS_DBG(1, "ip_vs_del_dest(): destination not found!\n");
1119 return -ENOENT;
1120 }
1121
1122 write_lock_bh(&__ip_vs_svc_lock);
1123
1124 /*
1125 * Wait until all other svc users go away.
1126 */
1127 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1128
1129 /*
1130 * Unlink dest from the service
1131 */
1132 __ip_vs_unlink_dest(svc, dest, 1);
1133
1134 write_unlock_bh(&__ip_vs_svc_lock);
1135
1136 /*
1137 * Delete the destination
1138 */
1139 __ip_vs_del_dest(dest);
1140
1141 LeaveFunction(2);
1142
1143 return 0;
1144}
1145
1146
1147/*
1148 * Add a service into the service hash table
1149 */
1150static int
c860c6b1
JV
1151ip_vs_add_service(struct ip_vs_service_user_kern *u,
1152 struct ip_vs_service **svc_p)
1da177e4
LT
1153{
1154 int ret = 0;
1155 struct ip_vs_scheduler *sched = NULL;
1156 struct ip_vs_service *svc = NULL;
1157
1158 /* increase the module use count */
1159 ip_vs_use_count_inc();
1160
1161 /* Lookup the scheduler by 'u->sched_name' */
1162 sched = ip_vs_scheduler_get(u->sched_name);
1163 if (sched == NULL) {
1164 IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
1165 u->sched_name);
1166 ret = -ENOENT;
1167 goto out_mod_dec;
1168 }
1169
f94fd041
JV
1170#ifdef CONFIG_IP_VS_IPV6
1171 if (u->af == AF_INET6) {
1172 if (!sched->supports_ipv6) {
1173 ret = -EAFNOSUPPORT;
1174 goto out_err;
1175 }
1176 if ((u->netmask < 1) || (u->netmask > 128)) {
1177 ret = -EINVAL;
1178 goto out_err;
1179 }
1180 }
1181#endif
1182
0da974f4 1183 svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
1da177e4
LT
1184 if (svc == NULL) {
1185 IP_VS_DBG(1, "ip_vs_add_service: kmalloc failed.\n");
1186 ret = -ENOMEM;
1187 goto out_err;
1188 }
1da177e4
LT
1189
1190 /* I'm the first user of the service */
1191 atomic_set(&svc->usecnt, 1);
1192 atomic_set(&svc->refcnt, 0);
1193
c860c6b1 1194 svc->af = u->af;
1da177e4 1195 svc->protocol = u->protocol;
c860c6b1 1196 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1da177e4
LT
1197 svc->port = u->port;
1198 svc->fwmark = u->fwmark;
1199 svc->flags = u->flags;
1200 svc->timeout = u->timeout * HZ;
1201 svc->netmask = u->netmask;
1202
1203 INIT_LIST_HEAD(&svc->destinations);
1204 rwlock_init(&svc->sched_lock);
1205 spin_lock_init(&svc->stats.lock);
1206
1207 /* Bind the scheduler */
1208 ret = ip_vs_bind_scheduler(svc, sched);
1209 if (ret)
1210 goto out_err;
1211 sched = NULL;
1212
1213 /* Update the virtual service counters */
1214 if (svc->port == FTPPORT)
1215 atomic_inc(&ip_vs_ftpsvc_counter);
1216 else if (svc->port == 0)
1217 atomic_inc(&ip_vs_nullsvc_counter);
1218
1219 ip_vs_new_estimator(&svc->stats);
f94fd041
JV
1220
1221 /* Count only IPv4 services for old get/setsockopt interface */
1222 if (svc->af == AF_INET)
1223 ip_vs_num_services++;
1da177e4
LT
1224
1225 /* Hash the service into the service table */
1226 write_lock_bh(&__ip_vs_svc_lock);
1227 ip_vs_svc_hash(svc);
1228 write_unlock_bh(&__ip_vs_svc_lock);
1229
1230 *svc_p = svc;
1231 return 0;
1232
1233 out_err:
1234 if (svc != NULL) {
1235 if (svc->scheduler)
1236 ip_vs_unbind_scheduler(svc);
1237 if (svc->inc) {
1238 local_bh_disable();
1239 ip_vs_app_inc_put(svc->inc);
1240 local_bh_enable();
1241 }
1242 kfree(svc);
1243 }
1244 ip_vs_scheduler_put(sched);
1245
1246 out_mod_dec:
1247 /* decrease the module use count */
1248 ip_vs_use_count_dec();
1249
1250 return ret;
1251}
1252
1253
1254/*
1255 * Edit a service and bind it with a new scheduler
1256 */
1257static int
c860c6b1 1258ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1da177e4
LT
1259{
1260 struct ip_vs_scheduler *sched, *old_sched;
1261 int ret = 0;
1262
1263 /*
1264 * Lookup the scheduler, by 'u->sched_name'
1265 */
1266 sched = ip_vs_scheduler_get(u->sched_name);
1267 if (sched == NULL) {
1268 IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
1269 u->sched_name);
1270 return -ENOENT;
1271 }
1272 old_sched = sched;
1273
f94fd041
JV
1274#ifdef CONFIG_IP_VS_IPV6
1275 if (u->af == AF_INET6) {
1276 if (!sched->supports_ipv6) {
a5ba4bf2 1277 ret = -EAFNOSUPPORT;
f94fd041
JV
1278 goto out;
1279 }
1280 if ((u->netmask < 1) || (u->netmask > 128)) {
a5ba4bf2 1281 ret = -EINVAL;
f94fd041
JV
1282 goto out;
1283 }
1284 }
1285#endif
1286
1da177e4
LT
1287 write_lock_bh(&__ip_vs_svc_lock);
1288
1289 /*
1290 * Wait until all other svc users go away.
1291 */
1292 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1293
1294 /*
1295 * Set the flags and timeout value
1296 */
1297 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1298 svc->timeout = u->timeout * HZ;
1299 svc->netmask = u->netmask;
1300
1301 old_sched = svc->scheduler;
1302 if (sched != old_sched) {
1303 /*
1304 * Unbind the old scheduler
1305 */
1306 if ((ret = ip_vs_unbind_scheduler(svc))) {
1307 old_sched = sched;
1308 goto out;
1309 }
1310
1311 /*
1312 * Bind the new scheduler
1313 */
1314 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1315 /*
1316 * If ip_vs_bind_scheduler fails, restore the old
1317 * scheduler.
1318 * The main reason of failure is out of memory.
1319 *
1320 * The question is if the old scheduler can be
1321 * restored all the time. TODO: if it cannot be
1322 * restored some time, we must delete the service,
1323 * otherwise the system may crash.
1324 */
1325 ip_vs_bind_scheduler(svc, old_sched);
1326 old_sched = sched;
1327 goto out;
1328 }
1329 }
1330
1331 out:
1332 write_unlock_bh(&__ip_vs_svc_lock);
1333
1334 if (old_sched)
1335 ip_vs_scheduler_put(old_sched);
1336
1337 return ret;
1338}
1339
1340
1341/*
1342 * Delete a service from the service list
1343 * - The service must be unlinked, unlocked and not referenced!
1344 * - We are called under _bh lock
1345 */
1346static void __ip_vs_del_service(struct ip_vs_service *svc)
1347{
1348 struct ip_vs_dest *dest, *nxt;
1349 struct ip_vs_scheduler *old_sched;
1350
f94fd041
JV
1351 /* Count only IPv4 services for old get/setsockopt interface */
1352 if (svc->af == AF_INET)
1353 ip_vs_num_services--;
1354
1da177e4
LT
1355 ip_vs_kill_estimator(&svc->stats);
1356
1357 /* Unbind scheduler */
1358 old_sched = svc->scheduler;
1359 ip_vs_unbind_scheduler(svc);
1360 if (old_sched)
1361 ip_vs_scheduler_put(old_sched);
1362
1363 /* Unbind app inc */
1364 if (svc->inc) {
1365 ip_vs_app_inc_put(svc->inc);
1366 svc->inc = NULL;
1367 }
1368
1369 /*
1370 * Unlink the whole destination list
1371 */
1372 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1373 __ip_vs_unlink_dest(svc, dest, 0);
1374 __ip_vs_del_dest(dest);
1375 }
1376
1377 /*
1378 * Update the virtual service counters
1379 */
1380 if (svc->port == FTPPORT)
1381 atomic_dec(&ip_vs_ftpsvc_counter);
1382 else if (svc->port == 0)
1383 atomic_dec(&ip_vs_nullsvc_counter);
1384
1385 /*
1386 * Free the service if nobody refers to it
1387 */
1388 if (atomic_read(&svc->refcnt) == 0)
1389 kfree(svc);
1390
1391 /* decrease the module use count */
1392 ip_vs_use_count_dec();
1393}
1394
1395/*
1396 * Delete a service from the service list
1397 */
1398static int ip_vs_del_service(struct ip_vs_service *svc)
1399{
1400 if (svc == NULL)
1401 return -EEXIST;
1402
1403 /*
1404 * Unhash it from the service table
1405 */
1406 write_lock_bh(&__ip_vs_svc_lock);
1407
1408 ip_vs_svc_unhash(svc);
1409
1410 /*
1411 * Wait until all the svc users go away.
1412 */
1413 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1414
1415 __ip_vs_del_service(svc);
1416
1417 write_unlock_bh(&__ip_vs_svc_lock);
1418
1419 return 0;
1420}
1421
1422
1423/*
1424 * Flush all the virtual services
1425 */
1426static int ip_vs_flush(void)
1427{
1428 int idx;
1429 struct ip_vs_service *svc, *nxt;
1430
1431 /*
1432 * Flush the service table hashed by <protocol,addr,port>
1433 */
1434 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1435 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
1436 write_lock_bh(&__ip_vs_svc_lock);
1437 ip_vs_svc_unhash(svc);
1438 /*
1439 * Wait until all the svc users go away.
1440 */
1441 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1442 __ip_vs_del_service(svc);
1443 write_unlock_bh(&__ip_vs_svc_lock);
1444 }
1445 }
1446
1447 /*
1448 * Flush the service table hashed by fwmark
1449 */
1450 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1451 list_for_each_entry_safe(svc, nxt,
1452 &ip_vs_svc_fwm_table[idx], f_list) {
1453 write_lock_bh(&__ip_vs_svc_lock);
1454 ip_vs_svc_unhash(svc);
1455 /*
1456 * Wait until all the svc users go away.
1457 */
1458 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1459 __ip_vs_del_service(svc);
1460 write_unlock_bh(&__ip_vs_svc_lock);
1461 }
1462 }
1463
1464 return 0;
1465}
1466
1467
1468/*
1469 * Zero counters in a service or all services
1470 */
1471static int ip_vs_zero_service(struct ip_vs_service *svc)
1472{
1473 struct ip_vs_dest *dest;
1474
1475 write_lock_bh(&__ip_vs_svc_lock);
1476 list_for_each_entry(dest, &svc->destinations, n_list) {
1477 ip_vs_zero_stats(&dest->stats);
1478 }
1479 ip_vs_zero_stats(&svc->stats);
1480 write_unlock_bh(&__ip_vs_svc_lock);
1481 return 0;
1482}
1483
1484static int ip_vs_zero_all(void)
1485{
1486 int idx;
1487 struct ip_vs_service *svc;
1488
1489 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1490 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1491 ip_vs_zero_service(svc);
1492 }
1493 }
1494
1495 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1496 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1497 ip_vs_zero_service(svc);
1498 }
1499 }
1500
1501 ip_vs_zero_stats(&ip_vs_stats);
1502 return 0;
1503}
1504
1505
1506static int
1507proc_do_defense_mode(ctl_table *table, int write, struct file * filp,
1508 void __user *buffer, size_t *lenp, loff_t *ppos)
1509{
1510 int *valp = table->data;
1511 int val = *valp;
1512 int rc;
1513
1514 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1515 if (write && (*valp != val)) {
1516 if ((*valp < 0) || (*valp > 3)) {
1517 /* Restore the correct value */
1518 *valp = val;
1519 } else {
1da177e4 1520 update_defense_level();
1da177e4
LT
1521 }
1522 }
1523 return rc;
1524}
1525
1526
1527static int
1528proc_do_sync_threshold(ctl_table *table, int write, struct file *filp,
1529 void __user *buffer, size_t *lenp, loff_t *ppos)
1530{
1531 int *valp = table->data;
1532 int val[2];
1533 int rc;
1534
1535 /* backup the value first */
1536 memcpy(val, valp, sizeof(val));
1537
1538 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1539 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1540 /* Restore the correct value */
1541 memcpy(valp, val, sizeof(val));
1542 }
1543 return rc;
1544}
1545
1546
1547/*
1548 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1549 */
1550
1551static struct ctl_table vs_vars[] = {
1552 {
1da177e4
LT
1553 .procname = "amemthresh",
1554 .data = &sysctl_ip_vs_amemthresh,
1555 .maxlen = sizeof(int),
1556 .mode = 0644,
1557 .proc_handler = &proc_dointvec,
1558 },
1559#ifdef CONFIG_IP_VS_DEBUG
1560 {
1da177e4
LT
1561 .procname = "debug_level",
1562 .data = &sysctl_ip_vs_debug_level,
1563 .maxlen = sizeof(int),
1564 .mode = 0644,
1565 .proc_handler = &proc_dointvec,
1566 },
1567#endif
1568 {
1da177e4
LT
1569 .procname = "am_droprate",
1570 .data = &sysctl_ip_vs_am_droprate,
1571 .maxlen = sizeof(int),
1572 .mode = 0644,
1573 .proc_handler = &proc_dointvec,
1574 },
1575 {
1da177e4
LT
1576 .procname = "drop_entry",
1577 .data = &sysctl_ip_vs_drop_entry,
1578 .maxlen = sizeof(int),
1579 .mode = 0644,
1580 .proc_handler = &proc_do_defense_mode,
1581 },
1582 {
1da177e4
LT
1583 .procname = "drop_packet",
1584 .data = &sysctl_ip_vs_drop_packet,
1585 .maxlen = sizeof(int),
1586 .mode = 0644,
1587 .proc_handler = &proc_do_defense_mode,
1588 },
1589 {
1da177e4
LT
1590 .procname = "secure_tcp",
1591 .data = &sysctl_ip_vs_secure_tcp,
1592 .maxlen = sizeof(int),
1593 .mode = 0644,
1594 .proc_handler = &proc_do_defense_mode,
1595 },
1596#if 0
1597 {
1da177e4
LT
1598 .procname = "timeout_established",
1599 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1600 .maxlen = sizeof(int),
1601 .mode = 0644,
1602 .proc_handler = &proc_dointvec_jiffies,
1603 },
1604 {
1da177e4
LT
1605 .procname = "timeout_synsent",
1606 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1607 .maxlen = sizeof(int),
1608 .mode = 0644,
1609 .proc_handler = &proc_dointvec_jiffies,
1610 },
1611 {
1da177e4
LT
1612 .procname = "timeout_synrecv",
1613 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1614 .maxlen = sizeof(int),
1615 .mode = 0644,
1616 .proc_handler = &proc_dointvec_jiffies,
1617 },
1618 {
1da177e4
LT
1619 .procname = "timeout_finwait",
1620 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1621 .maxlen = sizeof(int),
1622 .mode = 0644,
1623 .proc_handler = &proc_dointvec_jiffies,
1624 },
1625 {
1da177e4
LT
1626 .procname = "timeout_timewait",
1627 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1628 .maxlen = sizeof(int),
1629 .mode = 0644,
1630 .proc_handler = &proc_dointvec_jiffies,
1631 },
1632 {
1da177e4
LT
1633 .procname = "timeout_close",
1634 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1635 .maxlen = sizeof(int),
1636 .mode = 0644,
1637 .proc_handler = &proc_dointvec_jiffies,
1638 },
1639 {
1da177e4
LT
1640 .procname = "timeout_closewait",
1641 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1642 .maxlen = sizeof(int),
1643 .mode = 0644,
1644 .proc_handler = &proc_dointvec_jiffies,
1645 },
1646 {
1da177e4
LT
1647 .procname = "timeout_lastack",
1648 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1649 .maxlen = sizeof(int),
1650 .mode = 0644,
1651 .proc_handler = &proc_dointvec_jiffies,
1652 },
1653 {
1da177e4
LT
1654 .procname = "timeout_listen",
1655 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1656 .maxlen = sizeof(int),
1657 .mode = 0644,
1658 .proc_handler = &proc_dointvec_jiffies,
1659 },
1660 {
1da177e4
LT
1661 .procname = "timeout_synack",
1662 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1663 .maxlen = sizeof(int),
1664 .mode = 0644,
1665 .proc_handler = &proc_dointvec_jiffies,
1666 },
1667 {
1da177e4
LT
1668 .procname = "timeout_udp",
1669 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1670 .maxlen = sizeof(int),
1671 .mode = 0644,
1672 .proc_handler = &proc_dointvec_jiffies,
1673 },
1674 {
1da177e4
LT
1675 .procname = "timeout_icmp",
1676 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1677 .maxlen = sizeof(int),
1678 .mode = 0644,
1679 .proc_handler = &proc_dointvec_jiffies,
1680 },
1681#endif
1682 {
1da177e4
LT
1683 .procname = "cache_bypass",
1684 .data = &sysctl_ip_vs_cache_bypass,
1685 .maxlen = sizeof(int),
1686 .mode = 0644,
1687 .proc_handler = &proc_dointvec,
1688 },
1689 {
1da177e4
LT
1690 .procname = "expire_nodest_conn",
1691 .data = &sysctl_ip_vs_expire_nodest_conn,
1692 .maxlen = sizeof(int),
1693 .mode = 0644,
1694 .proc_handler = &proc_dointvec,
1695 },
1696 {
1da177e4
LT
1697 .procname = "expire_quiescent_template",
1698 .data = &sysctl_ip_vs_expire_quiescent_template,
1699 .maxlen = sizeof(int),
1700 .mode = 0644,
1701 .proc_handler = &proc_dointvec,
1702 },
1703 {
1da177e4
LT
1704 .procname = "sync_threshold",
1705 .data = &sysctl_ip_vs_sync_threshold,
1706 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1707 .mode = 0644,
1708 .proc_handler = &proc_do_sync_threshold,
1709 },
1710 {
1da177e4
LT
1711 .procname = "nat_icmp_send",
1712 .data = &sysctl_ip_vs_nat_icmp_send,
1713 .maxlen = sizeof(int),
1714 .mode = 0644,
1715 .proc_handler = &proc_dointvec,
1716 },
1717 { .ctl_name = 0 }
1718};
1719
5587da55 1720const struct ctl_path net_vs_ctl_path[] = {
90754f8e
PE
1721 { .procname = "net", .ctl_name = CTL_NET, },
1722 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1723 { .procname = "vs", },
1724 { }
1da177e4 1725};
90754f8e 1726EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1da177e4
LT
1727
1728static struct ctl_table_header * sysctl_header;
1729
1730#ifdef CONFIG_PROC_FS
1731
1732struct ip_vs_iter {
1733 struct list_head *table;
1734 int bucket;
1735};
1736
1737/*
1738 * Write the contents of the VS rule table to a PROCfs file.
1739 * (It is kept just for backward compatibility)
1740 */
1741static inline const char *ip_vs_fwd_name(unsigned flags)
1742{
1743 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1744 case IP_VS_CONN_F_LOCALNODE:
1745 return "Local";
1746 case IP_VS_CONN_F_TUNNEL:
1747 return "Tunnel";
1748 case IP_VS_CONN_F_DROUTE:
1749 return "Route";
1750 default:
1751 return "Masq";
1752 }
1753}
1754
1755
1756/* Get the Nth entry in the two lists */
1757static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1758{
1759 struct ip_vs_iter *iter = seq->private;
1760 int idx;
1761 struct ip_vs_service *svc;
1762
1763 /* look in hash by protocol */
1764 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1765 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1766 if (pos-- == 0){
1767 iter->table = ip_vs_svc_table;
1768 iter->bucket = idx;
1769 return svc;
1770 }
1771 }
1772 }
1773
1774 /* keep looking in fwmark */
1775 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1776 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1777 if (pos-- == 0) {
1778 iter->table = ip_vs_svc_fwm_table;
1779 iter->bucket = idx;
1780 return svc;
1781 }
1782 }
1783 }
1784
1785 return NULL;
1786}
1787
1788static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1789{
1790
1791 read_lock_bh(&__ip_vs_svc_lock);
1792 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1793}
1794
1795
1796static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1797{
1798 struct list_head *e;
1799 struct ip_vs_iter *iter;
1800 struct ip_vs_service *svc;
1801
1802 ++*pos;
1803 if (v == SEQ_START_TOKEN)
1804 return ip_vs_info_array(seq,0);
1805
1806 svc = v;
1807 iter = seq->private;
1808
1809 if (iter->table == ip_vs_svc_table) {
1810 /* next service in table hashed by protocol */
1811 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1812 return list_entry(e, struct ip_vs_service, s_list);
1813
1814
1815 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1816 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1817 s_list) {
1818 return svc;
1819 }
1820 }
1821
1822 iter->table = ip_vs_svc_fwm_table;
1823 iter->bucket = -1;
1824 goto scan_fwmark;
1825 }
1826
1827 /* next service in hashed by fwmark */
1828 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1829 return list_entry(e, struct ip_vs_service, f_list);
1830
1831 scan_fwmark:
1832 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1833 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1834 f_list)
1835 return svc;
1836 }
1837
1838 return NULL;
1839}
1840
1841static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1842{
1843 read_unlock_bh(&__ip_vs_svc_lock);
1844}
1845
1846
1847static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1848{
1849 if (v == SEQ_START_TOKEN) {
1850 seq_printf(seq,
1851 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1852 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
1853 seq_puts(seq,
1854 "Prot LocalAddress:Port Scheduler Flags\n");
1855 seq_puts(seq,
1856 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1857 } else {
1858 const struct ip_vs_service *svc = v;
1859 const struct ip_vs_iter *iter = seq->private;
1860 const struct ip_vs_dest *dest;
1861
667a5f18
VB
1862 if (iter->table == ip_vs_svc_table) {
1863#ifdef CONFIG_IP_VS_IPV6
1864 if (svc->af == AF_INET6)
1865 seq_printf(seq, "%s [" NIP6_FMT "]:%04X %s ",
1866 ip_vs_proto_name(svc->protocol),
1867 NIP6(svc->addr.in6),
1868 ntohs(svc->port),
1869 svc->scheduler->name);
1870 else
1871#endif
1872 seq_printf(seq, "%s %08X:%04X %s ",
1873 ip_vs_proto_name(svc->protocol),
1874 ntohl(svc->addr.ip),
1875 ntohs(svc->port),
1876 svc->scheduler->name);
1877 } else {
1da177e4
LT
1878 seq_printf(seq, "FWM %08X %s ",
1879 svc->fwmark, svc->scheduler->name);
667a5f18 1880 }
1da177e4
LT
1881
1882 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1883 seq_printf(seq, "persistent %d %08X\n",
1884 svc->timeout,
1885 ntohl(svc->netmask));
1886 else
1887 seq_putc(seq, '\n');
1888
1889 list_for_each_entry(dest, &svc->destinations, n_list) {
667a5f18
VB
1890#ifdef CONFIG_IP_VS_IPV6
1891 if (dest->af == AF_INET6)
1892 seq_printf(seq,
1893 " -> [" NIP6_FMT "]:%04X"
1894 " %-7s %-6d %-10d %-10d\n",
1895 NIP6(dest->addr.in6),
1896 ntohs(dest->port),
1897 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1898 atomic_read(&dest->weight),
1899 atomic_read(&dest->activeconns),
1900 atomic_read(&dest->inactconns));
1901 else
1902#endif
1903 seq_printf(seq,
1904 " -> %08X:%04X "
1905 "%-7s %-6d %-10d %-10d\n",
1906 ntohl(dest->addr.ip),
1907 ntohs(dest->port),
1908 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1909 atomic_read(&dest->weight),
1910 atomic_read(&dest->activeconns),
1911 atomic_read(&dest->inactconns));
1912
1da177e4
LT
1913 }
1914 }
1915 return 0;
1916}
1917
56b3d975 1918static const struct seq_operations ip_vs_info_seq_ops = {
1da177e4
LT
1919 .start = ip_vs_info_seq_start,
1920 .next = ip_vs_info_seq_next,
1921 .stop = ip_vs_info_seq_stop,
1922 .show = ip_vs_info_seq_show,
1923};
1924
1925static int ip_vs_info_open(struct inode *inode, struct file *file)
1926{
cf7732e4
PE
1927 return seq_open_private(file, &ip_vs_info_seq_ops,
1928 sizeof(struct ip_vs_iter));
1da177e4
LT
1929}
1930
9a32144e 1931static const struct file_operations ip_vs_info_fops = {
1da177e4
LT
1932 .owner = THIS_MODULE,
1933 .open = ip_vs_info_open,
1934 .read = seq_read,
1935 .llseek = seq_lseek,
1936 .release = seq_release_private,
1937};
1938
1939#endif
1940
519e49e8
SW
1941struct ip_vs_stats ip_vs_stats = {
1942 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1943};
1da177e4
LT
1944
1945#ifdef CONFIG_PROC_FS
1946static int ip_vs_stats_show(struct seq_file *seq, void *v)
1947{
1948
1949/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1950 seq_puts(seq,
1951 " Total Incoming Outgoing Incoming Outgoing\n");
1952 seq_printf(seq,
1953 " Conns Packets Packets Bytes Bytes\n");
1954
1955 spin_lock_bh(&ip_vs_stats.lock);
e9c0ce23
SW
1956 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1957 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1958 (unsigned long long) ip_vs_stats.ustats.inbytes,
1959 (unsigned long long) ip_vs_stats.ustats.outbytes);
1da177e4
LT
1960
1961/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1962 seq_puts(seq,
1963 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1964 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
e9c0ce23
SW
1965 ip_vs_stats.ustats.cps,
1966 ip_vs_stats.ustats.inpps,
1967 ip_vs_stats.ustats.outpps,
1968 ip_vs_stats.ustats.inbps,
1969 ip_vs_stats.ustats.outbps);
1da177e4
LT
1970 spin_unlock_bh(&ip_vs_stats.lock);
1971
1972 return 0;
1973}
1974
1975static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1976{
1977 return single_open(file, ip_vs_stats_show, NULL);
1978}
1979
9a32144e 1980static const struct file_operations ip_vs_stats_fops = {
1da177e4
LT
1981 .owner = THIS_MODULE,
1982 .open = ip_vs_stats_seq_open,
1983 .read = seq_read,
1984 .llseek = seq_lseek,
1985 .release = single_release,
1986};
1987
1988#endif
1989
1990/*
1991 * Set timeout values for tcp tcpfin udp in the timeout_table.
1992 */
1993static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1994{
1995 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1996 u->tcp_timeout,
1997 u->tcp_fin_timeout,
1998 u->udp_timeout);
1999
2000#ifdef CONFIG_IP_VS_PROTO_TCP
2001 if (u->tcp_timeout) {
2002 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
2003 = u->tcp_timeout * HZ;
2004 }
2005
2006 if (u->tcp_fin_timeout) {
2007 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
2008 = u->tcp_fin_timeout * HZ;
2009 }
2010#endif
2011
2012#ifdef CONFIG_IP_VS_PROTO_UDP
2013 if (u->udp_timeout) {
2014 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
2015 = u->udp_timeout * HZ;
2016 }
2017#endif
2018 return 0;
2019}
2020
2021
2022#define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2023#define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2024#define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2025 sizeof(struct ip_vs_dest_user))
2026#define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2027#define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2028#define MAX_ARG_LEN SVCDEST_ARG_LEN
2029
9b5b5cff 2030static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
1da177e4
LT
2031 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2032 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2033 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2034 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2035 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2036 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2037 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2038 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2039 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2040 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2041 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2042};
2043
c860c6b1
JV
2044static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2045 struct ip_vs_service_user *usvc_compat)
2046{
2047 usvc->af = AF_INET;
2048 usvc->protocol = usvc_compat->protocol;
2049 usvc->addr.ip = usvc_compat->addr;
2050 usvc->port = usvc_compat->port;
2051 usvc->fwmark = usvc_compat->fwmark;
2052
2053 /* Deep copy of sched_name is not needed here */
2054 usvc->sched_name = usvc_compat->sched_name;
2055
2056 usvc->flags = usvc_compat->flags;
2057 usvc->timeout = usvc_compat->timeout;
2058 usvc->netmask = usvc_compat->netmask;
2059}
2060
2061static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2062 struct ip_vs_dest_user *udest_compat)
2063{
2064 udest->addr.ip = udest_compat->addr;
2065 udest->port = udest_compat->port;
2066 udest->conn_flags = udest_compat->conn_flags;
2067 udest->weight = udest_compat->weight;
2068 udest->u_threshold = udest_compat->u_threshold;
2069 udest->l_threshold = udest_compat->l_threshold;
2070}
2071
1da177e4
LT
2072static int
2073do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2074{
2075 int ret;
2076 unsigned char arg[MAX_ARG_LEN];
c860c6b1
JV
2077 struct ip_vs_service_user *usvc_compat;
2078 struct ip_vs_service_user_kern usvc;
1da177e4 2079 struct ip_vs_service *svc;
c860c6b1
JV
2080 struct ip_vs_dest_user *udest_compat;
2081 struct ip_vs_dest_user_kern udest;
1da177e4
LT
2082
2083 if (!capable(CAP_NET_ADMIN))
2084 return -EPERM;
2085
2086 if (len != set_arglen[SET_CMDID(cmd)]) {
2087 IP_VS_ERR("set_ctl: len %u != %u\n",
2088 len, set_arglen[SET_CMDID(cmd)]);
2089 return -EINVAL;
2090 }
2091
2092 if (copy_from_user(arg, user, len) != 0)
2093 return -EFAULT;
2094
2095 /* increase the module use count */
2096 ip_vs_use_count_inc();
2097
14cc3e2b 2098 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
1da177e4
LT
2099 ret = -ERESTARTSYS;
2100 goto out_dec;
2101 }
2102
2103 if (cmd == IP_VS_SO_SET_FLUSH) {
2104 /* Flush the virtual service */
2105 ret = ip_vs_flush();
2106 goto out_unlock;
2107 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2108 /* Set timeout values for (tcp tcpfin udp) */
2109 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2110 goto out_unlock;
2111 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2112 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2113 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2114 goto out_unlock;
2115 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2116 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2117 ret = stop_sync_thread(dm->state);
2118 goto out_unlock;
2119 }
2120
c860c6b1
JV
2121 usvc_compat = (struct ip_vs_service_user *)arg;
2122 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2123
2124 /* We only use the new structs internally, so copy userspace compat
2125 * structs to extended internal versions */
2126 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2127 ip_vs_copy_udest_compat(&udest, udest_compat);
1da177e4
LT
2128
2129 if (cmd == IP_VS_SO_SET_ZERO) {
2130 /* if no service address is set, zero counters in all */
c860c6b1 2131 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
1da177e4
LT
2132 ret = ip_vs_zero_all();
2133 goto out_unlock;
2134 }
2135 }
2136
2137 /* Check for valid protocol: TCP or UDP, even for fwmark!=0 */
c860c6b1 2138 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP) {
1da177e4 2139 IP_VS_ERR("set_ctl: invalid protocol: %d %d.%d.%d.%d:%d %s\n",
c860c6b1
JV
2140 usvc.protocol, NIPQUAD(usvc.addr.ip),
2141 ntohs(usvc.port), usvc.sched_name);
1da177e4
LT
2142 ret = -EFAULT;
2143 goto out_unlock;
2144 }
2145
2146 /* Lookup the exact service by <protocol, addr, port> or fwmark */
c860c6b1 2147 if (usvc.fwmark == 0)
b18610de
JV
2148 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
2149 &usvc.addr, usvc.port);
1da177e4 2150 else
b18610de 2151 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
1da177e4
LT
2152
2153 if (cmd != IP_VS_SO_SET_ADD
c860c6b1 2154 && (svc == NULL || svc->protocol != usvc.protocol)) {
1da177e4
LT
2155 ret = -ESRCH;
2156 goto out_unlock;
2157 }
2158
2159 switch (cmd) {
2160 case IP_VS_SO_SET_ADD:
2161 if (svc != NULL)
2162 ret = -EEXIST;
2163 else
c860c6b1 2164 ret = ip_vs_add_service(&usvc, &svc);
1da177e4
LT
2165 break;
2166 case IP_VS_SO_SET_EDIT:
c860c6b1 2167 ret = ip_vs_edit_service(svc, &usvc);
1da177e4
LT
2168 break;
2169 case IP_VS_SO_SET_DEL:
2170 ret = ip_vs_del_service(svc);
2171 if (!ret)
2172 goto out_unlock;
2173 break;
2174 case IP_VS_SO_SET_ZERO:
2175 ret = ip_vs_zero_service(svc);
2176 break;
2177 case IP_VS_SO_SET_ADDDEST:
c860c6b1 2178 ret = ip_vs_add_dest(svc, &udest);
1da177e4
LT
2179 break;
2180 case IP_VS_SO_SET_EDITDEST:
c860c6b1 2181 ret = ip_vs_edit_dest(svc, &udest);
1da177e4
LT
2182 break;
2183 case IP_VS_SO_SET_DELDEST:
c860c6b1 2184 ret = ip_vs_del_dest(svc, &udest);
1da177e4
LT
2185 break;
2186 default:
2187 ret = -EINVAL;
2188 }
2189
2190 if (svc)
2191 ip_vs_service_put(svc);
2192
2193 out_unlock:
14cc3e2b 2194 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2195 out_dec:
2196 /* decrease the module use count */
2197 ip_vs_use_count_dec();
2198
2199 return ret;
2200}
2201
2202
2203static void
2204ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2205{
2206 spin_lock_bh(&src->lock);
e9c0ce23 2207 memcpy(dst, &src->ustats, sizeof(*dst));
1da177e4
LT
2208 spin_unlock_bh(&src->lock);
2209}
2210
2211static void
2212ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2213{
2214 dst->protocol = src->protocol;
e7ade46a 2215 dst->addr = src->addr.ip;
1da177e4
LT
2216 dst->port = src->port;
2217 dst->fwmark = src->fwmark;
4da62fc7 2218 strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
1da177e4
LT
2219 dst->flags = src->flags;
2220 dst->timeout = src->timeout / HZ;
2221 dst->netmask = src->netmask;
2222 dst->num_dests = src->num_dests;
2223 ip_vs_copy_stats(&dst->stats, &src->stats);
2224}
2225
2226static inline int
2227__ip_vs_get_service_entries(const struct ip_vs_get_services *get,
2228 struct ip_vs_get_services __user *uptr)
2229{
2230 int idx, count=0;
2231 struct ip_vs_service *svc;
2232 struct ip_vs_service_entry entry;
2233 int ret = 0;
2234
2235 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2236 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
f94fd041
JV
2237 /* Only expose IPv4 entries to old interface */
2238 if (svc->af != AF_INET)
2239 continue;
2240
1da177e4
LT
2241 if (count >= get->num_services)
2242 goto out;
4da62fc7 2243 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2244 ip_vs_copy_service(&entry, svc);
2245 if (copy_to_user(&uptr->entrytable[count],
2246 &entry, sizeof(entry))) {
2247 ret = -EFAULT;
2248 goto out;
2249 }
2250 count++;
2251 }
2252 }
2253
2254 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2255 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
f94fd041
JV
2256 /* Only expose IPv4 entries to old interface */
2257 if (svc->af != AF_INET)
2258 continue;
2259
1da177e4
LT
2260 if (count >= get->num_services)
2261 goto out;
4da62fc7 2262 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2263 ip_vs_copy_service(&entry, svc);
2264 if (copy_to_user(&uptr->entrytable[count],
2265 &entry, sizeof(entry))) {
2266 ret = -EFAULT;
2267 goto out;
2268 }
2269 count++;
2270 }
2271 }
2272 out:
2273 return ret;
2274}
2275
2276static inline int
2277__ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
2278 struct ip_vs_get_dests __user *uptr)
2279{
2280 struct ip_vs_service *svc;
b18610de 2281 union nf_inet_addr addr = { .ip = get->addr };
1da177e4
LT
2282 int ret = 0;
2283
2284 if (get->fwmark)
b18610de 2285 svc = __ip_vs_svc_fwm_get(AF_INET, get->fwmark);
1da177e4 2286 else
b18610de
JV
2287 svc = __ip_vs_service_get(AF_INET, get->protocol, &addr,
2288 get->port);
2289
1da177e4
LT
2290 if (svc) {
2291 int count = 0;
2292 struct ip_vs_dest *dest;
2293 struct ip_vs_dest_entry entry;
2294
2295 list_for_each_entry(dest, &svc->destinations, n_list) {
2296 if (count >= get->num_dests)
2297 break;
2298
e7ade46a 2299 entry.addr = dest->addr.ip;
1da177e4
LT
2300 entry.port = dest->port;
2301 entry.conn_flags = atomic_read(&dest->conn_flags);
2302 entry.weight = atomic_read(&dest->weight);
2303 entry.u_threshold = dest->u_threshold;
2304 entry.l_threshold = dest->l_threshold;
2305 entry.activeconns = atomic_read(&dest->activeconns);
2306 entry.inactconns = atomic_read(&dest->inactconns);
2307 entry.persistconns = atomic_read(&dest->persistconns);
2308 ip_vs_copy_stats(&entry.stats, &dest->stats);
2309 if (copy_to_user(&uptr->entrytable[count],
2310 &entry, sizeof(entry))) {
2311 ret = -EFAULT;
2312 break;
2313 }
2314 count++;
2315 }
2316 ip_vs_service_put(svc);
2317 } else
2318 ret = -ESRCH;
2319 return ret;
2320}
2321
2322static inline void
2323__ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
2324{
2325#ifdef CONFIG_IP_VS_PROTO_TCP
2326 u->tcp_timeout =
2327 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2328 u->tcp_fin_timeout =
2329 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2330#endif
2331#ifdef CONFIG_IP_VS_PROTO_UDP
2332 u->udp_timeout =
2333 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2334#endif
2335}
2336
2337
2338#define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2339#define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2340#define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2341#define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2342#define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2343#define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2344#define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2345
9b5b5cff 2346static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
1da177e4
LT
2347 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2348 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2349 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2350 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2351 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2352 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2353 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2354};
2355
2356static int
2357do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2358{
2359 unsigned char arg[128];
2360 int ret = 0;
2361
2362 if (!capable(CAP_NET_ADMIN))
2363 return -EPERM;
2364
2365 if (*len < get_arglen[GET_CMDID(cmd)]) {
2366 IP_VS_ERR("get_ctl: len %u < %u\n",
2367 *len, get_arglen[GET_CMDID(cmd)]);
2368 return -EINVAL;
2369 }
2370
2371 if (copy_from_user(arg, user, get_arglen[GET_CMDID(cmd)]) != 0)
2372 return -EFAULT;
2373
14cc3e2b 2374 if (mutex_lock_interruptible(&__ip_vs_mutex))
1da177e4
LT
2375 return -ERESTARTSYS;
2376
2377 switch (cmd) {
2378 case IP_VS_SO_GET_VERSION:
2379 {
2380 char buf[64];
2381
2382 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2383 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
2384 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2385 ret = -EFAULT;
2386 goto out;
2387 }
2388 *len = strlen(buf)+1;
2389 }
2390 break;
2391
2392 case IP_VS_SO_GET_INFO:
2393 {
2394 struct ip_vs_getinfo info;
2395 info.version = IP_VS_VERSION_CODE;
2396 info.size = IP_VS_CONN_TAB_SIZE;
2397 info.num_services = ip_vs_num_services;
2398 if (copy_to_user(user, &info, sizeof(info)) != 0)
2399 ret = -EFAULT;
2400 }
2401 break;
2402
2403 case IP_VS_SO_GET_SERVICES:
2404 {
2405 struct ip_vs_get_services *get;
2406 int size;
2407
2408 get = (struct ip_vs_get_services *)arg;
2409 size = sizeof(*get) +
2410 sizeof(struct ip_vs_service_entry) * get->num_services;
2411 if (*len != size) {
2412 IP_VS_ERR("length: %u != %u\n", *len, size);
2413 ret = -EINVAL;
2414 goto out;
2415 }
2416 ret = __ip_vs_get_service_entries(get, user);
2417 }
2418 break;
2419
2420 case IP_VS_SO_GET_SERVICE:
2421 {
2422 struct ip_vs_service_entry *entry;
2423 struct ip_vs_service *svc;
b18610de 2424 union nf_inet_addr addr;
1da177e4
LT
2425
2426 entry = (struct ip_vs_service_entry *)arg;
b18610de 2427 addr.ip = entry->addr;
1da177e4 2428 if (entry->fwmark)
b18610de 2429 svc = __ip_vs_svc_fwm_get(AF_INET, entry->fwmark);
1da177e4 2430 else
b18610de
JV
2431 svc = __ip_vs_service_get(AF_INET, entry->protocol,
2432 &addr, entry->port);
1da177e4
LT
2433 if (svc) {
2434 ip_vs_copy_service(entry, svc);
2435 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2436 ret = -EFAULT;
2437 ip_vs_service_put(svc);
2438 } else
2439 ret = -ESRCH;
2440 }
2441 break;
2442
2443 case IP_VS_SO_GET_DESTS:
2444 {
2445 struct ip_vs_get_dests *get;
2446 int size;
2447
2448 get = (struct ip_vs_get_dests *)arg;
2449 size = sizeof(*get) +
2450 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2451 if (*len != size) {
2452 IP_VS_ERR("length: %u != %u\n", *len, size);
2453 ret = -EINVAL;
2454 goto out;
2455 }
2456 ret = __ip_vs_get_dest_entries(get, user);
2457 }
2458 break;
2459
2460 case IP_VS_SO_GET_TIMEOUT:
2461 {
2462 struct ip_vs_timeout_user t;
2463
2464 __ip_vs_get_timeouts(&t);
2465 if (copy_to_user(user, &t, sizeof(t)) != 0)
2466 ret = -EFAULT;
2467 }
2468 break;
2469
2470 case IP_VS_SO_GET_DAEMON:
2471 {
2472 struct ip_vs_daemon_user d[2];
2473
2474 memset(&d, 0, sizeof(d));
2475 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2476 d[0].state = IP_VS_STATE_MASTER;
4da62fc7 2477 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
1da177e4
LT
2478 d[0].syncid = ip_vs_master_syncid;
2479 }
2480 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2481 d[1].state = IP_VS_STATE_BACKUP;
4da62fc7 2482 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
1da177e4
LT
2483 d[1].syncid = ip_vs_backup_syncid;
2484 }
2485 if (copy_to_user(user, &d, sizeof(d)) != 0)
2486 ret = -EFAULT;
2487 }
2488 break;
2489
2490 default:
2491 ret = -EINVAL;
2492 }
2493
2494 out:
14cc3e2b 2495 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2496 return ret;
2497}
2498
2499
2500static struct nf_sockopt_ops ip_vs_sockopts = {
2501 .pf = PF_INET,
2502 .set_optmin = IP_VS_BASE_CTL,
2503 .set_optmax = IP_VS_SO_SET_MAX+1,
2504 .set = do_ip_vs_set_ctl,
2505 .get_optmin = IP_VS_BASE_CTL,
2506 .get_optmax = IP_VS_SO_GET_MAX+1,
2507 .get = do_ip_vs_get_ctl,
16fcec35 2508 .owner = THIS_MODULE,
1da177e4
LT
2509};
2510
9a812198
JV
2511/*
2512 * Generic Netlink interface
2513 */
2514
2515/* IPVS genetlink family */
2516static struct genl_family ip_vs_genl_family = {
2517 .id = GENL_ID_GENERATE,
2518 .hdrsize = 0,
2519 .name = IPVS_GENL_NAME,
2520 .version = IPVS_GENL_VERSION,
2521 .maxattr = IPVS_CMD_MAX,
2522};
2523
2524/* Policy used for first-level command attributes */
2525static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2526 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2527 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2528 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2529 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2530 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2531 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2532};
2533
2534/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2535static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2536 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2537 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2538 .len = IP_VS_IFNAME_MAXLEN },
2539 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2540};
2541
2542/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2543static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2544 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2545 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2546 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2547 .len = sizeof(union nf_inet_addr) },
2548 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2549 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2550 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2551 .len = IP_VS_SCHEDNAME_MAXLEN },
2552 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2553 .len = sizeof(struct ip_vs_flags) },
2554 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2555 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2556 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2557};
2558
2559/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2560static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2561 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2562 .len = sizeof(union nf_inet_addr) },
2563 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2564 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2565 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2566 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2567 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2568 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2569 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2570 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2571 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2572};
2573
2574static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2575 struct ip_vs_stats *stats)
2576{
2577 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2578 if (!nl_stats)
2579 return -EMSGSIZE;
2580
2581 spin_lock_bh(&stats->lock);
2582
e9c0ce23
SW
2583 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2584 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2585 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2586 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2587 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2588 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2589 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2590 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2591 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2592 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
9a812198
JV
2593
2594 spin_unlock_bh(&stats->lock);
2595
2596 nla_nest_end(skb, nl_stats);
2597
2598 return 0;
2599
2600nla_put_failure:
2601 spin_unlock_bh(&stats->lock);
2602 nla_nest_cancel(skb, nl_stats);
2603 return -EMSGSIZE;
2604}
2605
2606static int ip_vs_genl_fill_service(struct sk_buff *skb,
2607 struct ip_vs_service *svc)
2608{
2609 struct nlattr *nl_service;
2610 struct ip_vs_flags flags = { .flags = svc->flags,
2611 .mask = ~0 };
2612
2613 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2614 if (!nl_service)
2615 return -EMSGSIZE;
2616
f94fd041 2617 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
9a812198
JV
2618
2619 if (svc->fwmark) {
2620 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2621 } else {
2622 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2623 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2624 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2625 }
2626
2627 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2628 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2629 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2630 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2631
2632 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2633 goto nla_put_failure;
2634
2635 nla_nest_end(skb, nl_service);
2636
2637 return 0;
2638
2639nla_put_failure:
2640 nla_nest_cancel(skb, nl_service);
2641 return -EMSGSIZE;
2642}
2643
2644static int ip_vs_genl_dump_service(struct sk_buff *skb,
2645 struct ip_vs_service *svc,
2646 struct netlink_callback *cb)
2647{
2648 void *hdr;
2649
2650 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2651 &ip_vs_genl_family, NLM_F_MULTI,
2652 IPVS_CMD_NEW_SERVICE);
2653 if (!hdr)
2654 return -EMSGSIZE;
2655
2656 if (ip_vs_genl_fill_service(skb, svc) < 0)
2657 goto nla_put_failure;
2658
2659 return genlmsg_end(skb, hdr);
2660
2661nla_put_failure:
2662 genlmsg_cancel(skb, hdr);
2663 return -EMSGSIZE;
2664}
2665
2666static int ip_vs_genl_dump_services(struct sk_buff *skb,
2667 struct netlink_callback *cb)
2668{
2669 int idx = 0, i;
2670 int start = cb->args[0];
2671 struct ip_vs_service *svc;
2672
2673 mutex_lock(&__ip_vs_mutex);
2674 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2675 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2676 if (++idx <= start)
2677 continue;
2678 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2679 idx--;
2680 goto nla_put_failure;
2681 }
2682 }
2683 }
2684
2685 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2686 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2687 if (++idx <= start)
2688 continue;
2689 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2690 idx--;
2691 goto nla_put_failure;
2692 }
2693 }
2694 }
2695
2696nla_put_failure:
2697 mutex_unlock(&__ip_vs_mutex);
2698 cb->args[0] = idx;
2699
2700 return skb->len;
2701}
2702
c860c6b1 2703static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
9a812198
JV
2704 struct nlattr *nla, int full_entry)
2705{
2706 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2707 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2708
2709 /* Parse mandatory identifying service fields first */
2710 if (nla == NULL ||
2711 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2712 return -EINVAL;
2713
2714 nla_af = attrs[IPVS_SVC_ATTR_AF];
2715 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2716 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2717 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2718 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2719
2720 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2721 return -EINVAL;
2722
c860c6b1 2723 usvc->af = nla_get_u16(nla_af);
f94fd041
JV
2724#ifdef CONFIG_IP_VS_IPV6
2725 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2726#else
2727 if (usvc->af != AF_INET)
2728#endif
9a812198
JV
2729 return -EAFNOSUPPORT;
2730
2731 if (nla_fwmark) {
2732 usvc->protocol = IPPROTO_TCP;
2733 usvc->fwmark = nla_get_u32(nla_fwmark);
2734 } else {
2735 usvc->protocol = nla_get_u16(nla_protocol);
2736 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2737 usvc->port = nla_get_u16(nla_port);
2738 usvc->fwmark = 0;
2739 }
2740
2741 /* If a full entry was requested, check for the additional fields */
2742 if (full_entry) {
2743 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2744 *nla_netmask;
2745 struct ip_vs_flags flags;
2746 struct ip_vs_service *svc;
2747
2748 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2749 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2750 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2751 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2752
2753 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2754 return -EINVAL;
2755
2756 nla_memcpy(&flags, nla_flags, sizeof(flags));
2757
2758 /* prefill flags from service if it already exists */
2759 if (usvc->fwmark)
b18610de 2760 svc = __ip_vs_svc_fwm_get(usvc->af, usvc->fwmark);
9a812198 2761 else
b18610de
JV
2762 svc = __ip_vs_service_get(usvc->af, usvc->protocol,
2763 &usvc->addr, usvc->port);
9a812198
JV
2764 if (svc) {
2765 usvc->flags = svc->flags;
2766 ip_vs_service_put(svc);
2767 } else
2768 usvc->flags = 0;
2769
2770 /* set new flags from userland */
2771 usvc->flags = (usvc->flags & ~flags.mask) |
2772 (flags.flags & flags.mask);
c860c6b1 2773 usvc->sched_name = nla_data(nla_sched);
9a812198
JV
2774 usvc->timeout = nla_get_u32(nla_timeout);
2775 usvc->netmask = nla_get_u32(nla_netmask);
2776 }
2777
2778 return 0;
2779}
2780
2781static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2782{
c860c6b1 2783 struct ip_vs_service_user_kern usvc;
9a812198
JV
2784 int ret;
2785
2786 ret = ip_vs_genl_parse_service(&usvc, nla, 0);
2787 if (ret)
2788 return ERR_PTR(ret);
2789
2790 if (usvc.fwmark)
b18610de 2791 return __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
9a812198 2792 else
b18610de
JV
2793 return __ip_vs_service_get(usvc.af, usvc.protocol,
2794 &usvc.addr, usvc.port);
9a812198
JV
2795}
2796
2797static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2798{
2799 struct nlattr *nl_dest;
2800
2801 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2802 if (!nl_dest)
2803 return -EMSGSIZE;
2804
2805 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2806 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2807
2808 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2809 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2810 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2811 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2812 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2813 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2814 atomic_read(&dest->activeconns));
2815 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2816 atomic_read(&dest->inactconns));
2817 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2818 atomic_read(&dest->persistconns));
2819
2820 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2821 goto nla_put_failure;
2822
2823 nla_nest_end(skb, nl_dest);
2824
2825 return 0;
2826
2827nla_put_failure:
2828 nla_nest_cancel(skb, nl_dest);
2829 return -EMSGSIZE;
2830}
2831
2832static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2833 struct netlink_callback *cb)
2834{
2835 void *hdr;
2836
2837 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2838 &ip_vs_genl_family, NLM_F_MULTI,
2839 IPVS_CMD_NEW_DEST);
2840 if (!hdr)
2841 return -EMSGSIZE;
2842
2843 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2844 goto nla_put_failure;
2845
2846 return genlmsg_end(skb, hdr);
2847
2848nla_put_failure:
2849 genlmsg_cancel(skb, hdr);
2850 return -EMSGSIZE;
2851}
2852
2853static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2854 struct netlink_callback *cb)
2855{
2856 int idx = 0;
2857 int start = cb->args[0];
2858 struct ip_vs_service *svc;
2859 struct ip_vs_dest *dest;
2860 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2861
2862 mutex_lock(&__ip_vs_mutex);
2863
2864 /* Try to find the service for which to dump destinations */
2865 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2866 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2867 goto out_err;
2868
2869 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2870 if (IS_ERR(svc) || svc == NULL)
2871 goto out_err;
2872
2873 /* Dump the destinations */
2874 list_for_each_entry(dest, &svc->destinations, n_list) {
2875 if (++idx <= start)
2876 continue;
2877 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2878 idx--;
2879 goto nla_put_failure;
2880 }
2881 }
2882
2883nla_put_failure:
2884 cb->args[0] = idx;
2885 ip_vs_service_put(svc);
2886
2887out_err:
2888 mutex_unlock(&__ip_vs_mutex);
2889
2890 return skb->len;
2891}
2892
c860c6b1 2893static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
9a812198
JV
2894 struct nlattr *nla, int full_entry)
2895{
2896 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2897 struct nlattr *nla_addr, *nla_port;
2898
2899 /* Parse mandatory identifying destination fields first */
2900 if (nla == NULL ||
2901 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2902 return -EINVAL;
2903
2904 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2905 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2906
2907 if (!(nla_addr && nla_port))
2908 return -EINVAL;
2909
2910 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2911 udest->port = nla_get_u16(nla_port);
2912
2913 /* If a full entry was requested, check for the additional fields */
2914 if (full_entry) {
2915 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2916 *nla_l_thresh;
2917
2918 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2919 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2920 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2921 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2922
2923 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2924 return -EINVAL;
2925
2926 udest->conn_flags = nla_get_u32(nla_fwd)
2927 & IP_VS_CONN_F_FWD_MASK;
2928 udest->weight = nla_get_u32(nla_weight);
2929 udest->u_threshold = nla_get_u32(nla_u_thresh);
2930 udest->l_threshold = nla_get_u32(nla_l_thresh);
2931 }
2932
2933 return 0;
2934}
2935
2936static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2937 const char *mcast_ifn, __be32 syncid)
2938{
2939 struct nlattr *nl_daemon;
2940
2941 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2942 if (!nl_daemon)
2943 return -EMSGSIZE;
2944
2945 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2946 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2947 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2948
2949 nla_nest_end(skb, nl_daemon);
2950
2951 return 0;
2952
2953nla_put_failure:
2954 nla_nest_cancel(skb, nl_daemon);
2955 return -EMSGSIZE;
2956}
2957
2958static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2959 const char *mcast_ifn, __be32 syncid,
2960 struct netlink_callback *cb)
2961{
2962 void *hdr;
2963 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2964 &ip_vs_genl_family, NLM_F_MULTI,
2965 IPVS_CMD_NEW_DAEMON);
2966 if (!hdr)
2967 return -EMSGSIZE;
2968
2969 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2970 goto nla_put_failure;
2971
2972 return genlmsg_end(skb, hdr);
2973
2974nla_put_failure:
2975 genlmsg_cancel(skb, hdr);
2976 return -EMSGSIZE;
2977}
2978
2979static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2980 struct netlink_callback *cb)
2981{
2982 mutex_lock(&__ip_vs_mutex);
2983 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2984 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
2985 ip_vs_master_mcast_ifn,
2986 ip_vs_master_syncid, cb) < 0)
2987 goto nla_put_failure;
2988
2989 cb->args[0] = 1;
2990 }
2991
2992 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
2993 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
2994 ip_vs_backup_mcast_ifn,
2995 ip_vs_backup_syncid, cb) < 0)
2996 goto nla_put_failure;
2997
2998 cb->args[1] = 1;
2999 }
3000
3001nla_put_failure:
3002 mutex_unlock(&__ip_vs_mutex);
3003
3004 return skb->len;
3005}
3006
3007static int ip_vs_genl_new_daemon(struct nlattr **attrs)
3008{
3009 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3010 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3011 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3012 return -EINVAL;
3013
3014 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3015 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3016 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3017}
3018
3019static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3020{
3021 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3022 return -EINVAL;
3023
3024 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3025}
3026
3027static int ip_vs_genl_set_config(struct nlattr **attrs)
3028{
3029 struct ip_vs_timeout_user t;
3030
3031 __ip_vs_get_timeouts(&t);
3032
3033 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3034 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3035
3036 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3037 t.tcp_fin_timeout =
3038 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3039
3040 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3041 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3042
3043 return ip_vs_set_timeout(&t);
3044}
3045
3046static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3047{
3048 struct ip_vs_service *svc = NULL;
c860c6b1
JV
3049 struct ip_vs_service_user_kern usvc;
3050 struct ip_vs_dest_user_kern udest;
9a812198
JV
3051 int ret = 0, cmd;
3052 int need_full_svc = 0, need_full_dest = 0;
3053
3054 cmd = info->genlhdr->cmd;
3055
3056 mutex_lock(&__ip_vs_mutex);
3057
3058 if (cmd == IPVS_CMD_FLUSH) {
3059 ret = ip_vs_flush();
3060 goto out;
3061 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3062 ret = ip_vs_genl_set_config(info->attrs);
3063 goto out;
3064 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3065 cmd == IPVS_CMD_DEL_DAEMON) {
3066
3067 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3068
3069 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3070 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3071 info->attrs[IPVS_CMD_ATTR_DAEMON],
3072 ip_vs_daemon_policy)) {
3073 ret = -EINVAL;
3074 goto out;
3075 }
3076
3077 if (cmd == IPVS_CMD_NEW_DAEMON)
3078 ret = ip_vs_genl_new_daemon(daemon_attrs);
3079 else
3080 ret = ip_vs_genl_del_daemon(daemon_attrs);
3081 goto out;
3082 } else if (cmd == IPVS_CMD_ZERO &&
3083 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3084 ret = ip_vs_zero_all();
3085 goto out;
3086 }
3087
3088 /* All following commands require a service argument, so check if we
3089 * received a valid one. We need a full service specification when
3090 * adding / editing a service. Only identifying members otherwise. */
3091 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3092 need_full_svc = 1;
3093
3094 ret = ip_vs_genl_parse_service(&usvc,
3095 info->attrs[IPVS_CMD_ATTR_SERVICE],
3096 need_full_svc);
3097 if (ret)
3098 goto out;
3099
3100 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3101 if (usvc.fwmark == 0)
b18610de
JV
3102 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
3103 &usvc.addr, usvc.port);
9a812198 3104 else
b18610de 3105 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
9a812198
JV
3106
3107 /* Unless we're adding a new service, the service must already exist */
3108 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3109 ret = -ESRCH;
3110 goto out;
3111 }
3112
3113 /* Destination commands require a valid destination argument. For
3114 * adding / editing a destination, we need a full destination
3115 * specification. */
3116 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3117 cmd == IPVS_CMD_DEL_DEST) {
3118 if (cmd != IPVS_CMD_DEL_DEST)
3119 need_full_dest = 1;
3120
3121 ret = ip_vs_genl_parse_dest(&udest,
3122 info->attrs[IPVS_CMD_ATTR_DEST],
3123 need_full_dest);
3124 if (ret)
3125 goto out;
3126 }
3127
3128 switch (cmd) {
3129 case IPVS_CMD_NEW_SERVICE:
3130 if (svc == NULL)
3131 ret = ip_vs_add_service(&usvc, &svc);
3132 else
3133 ret = -EEXIST;
3134 break;
3135 case IPVS_CMD_SET_SERVICE:
3136 ret = ip_vs_edit_service(svc, &usvc);
3137 break;
3138 case IPVS_CMD_DEL_SERVICE:
3139 ret = ip_vs_del_service(svc);
3140 break;
3141 case IPVS_CMD_NEW_DEST:
3142 ret = ip_vs_add_dest(svc, &udest);
3143 break;
3144 case IPVS_CMD_SET_DEST:
3145 ret = ip_vs_edit_dest(svc, &udest);
3146 break;
3147 case IPVS_CMD_DEL_DEST:
3148 ret = ip_vs_del_dest(svc, &udest);
3149 break;
3150 case IPVS_CMD_ZERO:
3151 ret = ip_vs_zero_service(svc);
3152 break;
3153 default:
3154 ret = -EINVAL;
3155 }
3156
3157out:
3158 if (svc)
3159 ip_vs_service_put(svc);
3160 mutex_unlock(&__ip_vs_mutex);
3161
3162 return ret;
3163}
3164
3165static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3166{
3167 struct sk_buff *msg;
3168 void *reply;
3169 int ret, cmd, reply_cmd;
3170
3171 cmd = info->genlhdr->cmd;
3172
3173 if (cmd == IPVS_CMD_GET_SERVICE)
3174 reply_cmd = IPVS_CMD_NEW_SERVICE;
3175 else if (cmd == IPVS_CMD_GET_INFO)
3176 reply_cmd = IPVS_CMD_SET_INFO;
3177 else if (cmd == IPVS_CMD_GET_CONFIG)
3178 reply_cmd = IPVS_CMD_SET_CONFIG;
3179 else {
3180 IP_VS_ERR("unknown Generic Netlink command\n");
3181 return -EINVAL;
3182 }
3183
3184 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3185 if (!msg)
3186 return -ENOMEM;
3187
3188 mutex_lock(&__ip_vs_mutex);
3189
3190 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3191 if (reply == NULL)
3192 goto nla_put_failure;
3193
3194 switch (cmd) {
3195 case IPVS_CMD_GET_SERVICE:
3196 {
3197 struct ip_vs_service *svc;
3198
3199 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3200 if (IS_ERR(svc)) {
3201 ret = PTR_ERR(svc);
3202 goto out_err;
3203 } else if (svc) {
3204 ret = ip_vs_genl_fill_service(msg, svc);
3205 ip_vs_service_put(svc);
3206 if (ret)
3207 goto nla_put_failure;
3208 } else {
3209 ret = -ESRCH;
3210 goto out_err;
3211 }
3212
3213 break;
3214 }
3215
3216 case IPVS_CMD_GET_CONFIG:
3217 {
3218 struct ip_vs_timeout_user t;
3219
3220 __ip_vs_get_timeouts(&t);
3221#ifdef CONFIG_IP_VS_PROTO_TCP
3222 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3223 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3224 t.tcp_fin_timeout);
3225#endif
3226#ifdef CONFIG_IP_VS_PROTO_UDP
3227 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3228#endif
3229
3230 break;
3231 }
3232
3233 case IPVS_CMD_GET_INFO:
3234 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3235 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3236 IP_VS_CONN_TAB_SIZE);
3237 break;
3238 }
3239
3240 genlmsg_end(msg, reply);
3241 ret = genlmsg_unicast(msg, info->snd_pid);
3242 goto out;
3243
3244nla_put_failure:
3245 IP_VS_ERR("not enough space in Netlink message\n");
3246 ret = -EMSGSIZE;
3247
3248out_err:
3249 nlmsg_free(msg);
3250out:
3251 mutex_unlock(&__ip_vs_mutex);
3252
3253 return ret;
3254}
3255
3256
3257static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3258 {
3259 .cmd = IPVS_CMD_NEW_SERVICE,
3260 .flags = GENL_ADMIN_PERM,
3261 .policy = ip_vs_cmd_policy,
3262 .doit = ip_vs_genl_set_cmd,
3263 },
3264 {
3265 .cmd = IPVS_CMD_SET_SERVICE,
3266 .flags = GENL_ADMIN_PERM,
3267 .policy = ip_vs_cmd_policy,
3268 .doit = ip_vs_genl_set_cmd,
3269 },
3270 {
3271 .cmd = IPVS_CMD_DEL_SERVICE,
3272 .flags = GENL_ADMIN_PERM,
3273 .policy = ip_vs_cmd_policy,
3274 .doit = ip_vs_genl_set_cmd,
3275 },
3276 {
3277 .cmd = IPVS_CMD_GET_SERVICE,
3278 .flags = GENL_ADMIN_PERM,
3279 .doit = ip_vs_genl_get_cmd,
3280 .dumpit = ip_vs_genl_dump_services,
3281 .policy = ip_vs_cmd_policy,
3282 },
3283 {
3284 .cmd = IPVS_CMD_NEW_DEST,
3285 .flags = GENL_ADMIN_PERM,
3286 .policy = ip_vs_cmd_policy,
3287 .doit = ip_vs_genl_set_cmd,
3288 },
3289 {
3290 .cmd = IPVS_CMD_SET_DEST,
3291 .flags = GENL_ADMIN_PERM,
3292 .policy = ip_vs_cmd_policy,
3293 .doit = ip_vs_genl_set_cmd,
3294 },
3295 {
3296 .cmd = IPVS_CMD_DEL_DEST,
3297 .flags = GENL_ADMIN_PERM,
3298 .policy = ip_vs_cmd_policy,
3299 .doit = ip_vs_genl_set_cmd,
3300 },
3301 {
3302 .cmd = IPVS_CMD_GET_DEST,
3303 .flags = GENL_ADMIN_PERM,
3304 .policy = ip_vs_cmd_policy,
3305 .dumpit = ip_vs_genl_dump_dests,
3306 },
3307 {
3308 .cmd = IPVS_CMD_NEW_DAEMON,
3309 .flags = GENL_ADMIN_PERM,
3310 .policy = ip_vs_cmd_policy,
3311 .doit = ip_vs_genl_set_cmd,
3312 },
3313 {
3314 .cmd = IPVS_CMD_DEL_DAEMON,
3315 .flags = GENL_ADMIN_PERM,
3316 .policy = ip_vs_cmd_policy,
3317 .doit = ip_vs_genl_set_cmd,
3318 },
3319 {
3320 .cmd = IPVS_CMD_GET_DAEMON,
3321 .flags = GENL_ADMIN_PERM,
3322 .dumpit = ip_vs_genl_dump_daemons,
3323 },
3324 {
3325 .cmd = IPVS_CMD_SET_CONFIG,
3326 .flags = GENL_ADMIN_PERM,
3327 .policy = ip_vs_cmd_policy,
3328 .doit = ip_vs_genl_set_cmd,
3329 },
3330 {
3331 .cmd = IPVS_CMD_GET_CONFIG,
3332 .flags = GENL_ADMIN_PERM,
3333 .doit = ip_vs_genl_get_cmd,
3334 },
3335 {
3336 .cmd = IPVS_CMD_GET_INFO,
3337 .flags = GENL_ADMIN_PERM,
3338 .doit = ip_vs_genl_get_cmd,
3339 },
3340 {
3341 .cmd = IPVS_CMD_ZERO,
3342 .flags = GENL_ADMIN_PERM,
3343 .policy = ip_vs_cmd_policy,
3344 .doit = ip_vs_genl_set_cmd,
3345 },
3346 {
3347 .cmd = IPVS_CMD_FLUSH,
3348 .flags = GENL_ADMIN_PERM,
3349 .doit = ip_vs_genl_set_cmd,
3350 },
3351};
3352
3353static int __init ip_vs_genl_register(void)
3354{
3355 int ret, i;
3356
3357 ret = genl_register_family(&ip_vs_genl_family);
3358 if (ret)
3359 return ret;
3360
3361 for (i = 0; i < ARRAY_SIZE(ip_vs_genl_ops); i++) {
3362 ret = genl_register_ops(&ip_vs_genl_family, &ip_vs_genl_ops[i]);
3363 if (ret)
3364 goto err_out;
3365 }
3366 return 0;
3367
3368err_out:
3369 genl_unregister_family(&ip_vs_genl_family);
3370 return ret;
3371}
3372
3373static void ip_vs_genl_unregister(void)
3374{
3375 genl_unregister_family(&ip_vs_genl_family);
3376}
3377
3378/* End of Generic Netlink interface definitions */
3379
1da177e4 3380
048cf48b 3381int __init ip_vs_control_init(void)
1da177e4
LT
3382{
3383 int ret;
3384 int idx;
3385
3386 EnterFunction(2);
3387
3388 ret = nf_register_sockopt(&ip_vs_sockopts);
3389 if (ret) {
3390 IP_VS_ERR("cannot register sockopt.\n");
3391 return ret;
3392 }
3393
9a812198
JV
3394 ret = ip_vs_genl_register();
3395 if (ret) {
3396 IP_VS_ERR("cannot register Generic Netlink interface.\n");
3397 nf_unregister_sockopt(&ip_vs_sockopts);
3398 return ret;
3399 }
3400
457c4cbc
EB
3401 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3402 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
1da177e4 3403
90754f8e 3404 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
1da177e4
LT
3405
3406 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3407 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3408 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3409 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3410 }
3411 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3412 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3413 }
3414
1da177e4
LT
3415 ip_vs_new_estimator(&ip_vs_stats);
3416
3417 /* Hook the defense timer */
3418 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3419
3420 LeaveFunction(2);
3421 return 0;
3422}
3423
3424
3425void ip_vs_control_cleanup(void)
3426{
3427 EnterFunction(2);
3428 ip_vs_trash_cleanup();
3429 cancel_rearming_delayed_work(&defense_work);
28e53bdd 3430 cancel_work_sync(&defense_work.work);
1da177e4
LT
3431 ip_vs_kill_estimator(&ip_vs_stats);
3432 unregister_sysctl_table(sysctl_header);
457c4cbc
EB
3433 proc_net_remove(&init_net, "ip_vs_stats");
3434 proc_net_remove(&init_net, "ip_vs");
9a812198 3435 ip_vs_genl_unregister();
1da177e4
LT
3436 nf_unregister_sockopt(&ip_vs_sockopts);
3437 LeaveFunction(2);
3438}