]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/ipvs/ip_vs_ctl.c
bonding, net: Move last_rx update into bonding recv logic
[net-next-2.6.git] / net / netfilter / ipvs / ip_vs_ctl.c
CommitLineData
1da177e4
LT
1/*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the NetFilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
1da177e4
LT
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * Changes:
18 *
19 */
20
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 1170#ifdef CONFIG_IP_VS_IPV6
48148938
JV
1171 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1172 ret = -EINVAL;
1173 goto out_err;
f94fd041
JV
1174 }
1175#endif
1176
0da974f4 1177 svc = kzalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
1da177e4
LT
1178 if (svc == NULL) {
1179 IP_VS_DBG(1, "ip_vs_add_service: kmalloc failed.\n");
1180 ret = -ENOMEM;
1181 goto out_err;
1182 }
1da177e4
LT
1183
1184 /* I'm the first user of the service */
1185 atomic_set(&svc->usecnt, 1);
1186 atomic_set(&svc->refcnt, 0);
1187
c860c6b1 1188 svc->af = u->af;
1da177e4 1189 svc->protocol = u->protocol;
c860c6b1 1190 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
1da177e4
LT
1191 svc->port = u->port;
1192 svc->fwmark = u->fwmark;
1193 svc->flags = u->flags;
1194 svc->timeout = u->timeout * HZ;
1195 svc->netmask = u->netmask;
1196
1197 INIT_LIST_HEAD(&svc->destinations);
1198 rwlock_init(&svc->sched_lock);
1199 spin_lock_init(&svc->stats.lock);
1200
1201 /* Bind the scheduler */
1202 ret = ip_vs_bind_scheduler(svc, sched);
1203 if (ret)
1204 goto out_err;
1205 sched = NULL;
1206
1207 /* Update the virtual service counters */
1208 if (svc->port == FTPPORT)
1209 atomic_inc(&ip_vs_ftpsvc_counter);
1210 else if (svc->port == 0)
1211 atomic_inc(&ip_vs_nullsvc_counter);
1212
1213 ip_vs_new_estimator(&svc->stats);
f94fd041
JV
1214
1215 /* Count only IPv4 services for old get/setsockopt interface */
1216 if (svc->af == AF_INET)
1217 ip_vs_num_services++;
1da177e4
LT
1218
1219 /* Hash the service into the service table */
1220 write_lock_bh(&__ip_vs_svc_lock);
1221 ip_vs_svc_hash(svc);
1222 write_unlock_bh(&__ip_vs_svc_lock);
1223
1224 *svc_p = svc;
1225 return 0;
1226
1227 out_err:
1228 if (svc != NULL) {
1229 if (svc->scheduler)
1230 ip_vs_unbind_scheduler(svc);
1231 if (svc->inc) {
1232 local_bh_disable();
1233 ip_vs_app_inc_put(svc->inc);
1234 local_bh_enable();
1235 }
1236 kfree(svc);
1237 }
1238 ip_vs_scheduler_put(sched);
1239
1240 out_mod_dec:
1241 /* decrease the module use count */
1242 ip_vs_use_count_dec();
1243
1244 return ret;
1245}
1246
1247
1248/*
1249 * Edit a service and bind it with a new scheduler
1250 */
1251static int
c860c6b1 1252ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
1da177e4
LT
1253{
1254 struct ip_vs_scheduler *sched, *old_sched;
1255 int ret = 0;
1256
1257 /*
1258 * Lookup the scheduler, by 'u->sched_name'
1259 */
1260 sched = ip_vs_scheduler_get(u->sched_name);
1261 if (sched == NULL) {
1262 IP_VS_INFO("Scheduler module ip_vs_%s not found\n",
1263 u->sched_name);
1264 return -ENOENT;
1265 }
1266 old_sched = sched;
1267
f94fd041 1268#ifdef CONFIG_IP_VS_IPV6
48148938
JV
1269 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1270 ret = -EINVAL;
1271 goto out;
f94fd041
JV
1272 }
1273#endif
1274
1da177e4
LT
1275 write_lock_bh(&__ip_vs_svc_lock);
1276
1277 /*
1278 * Wait until all other svc users go away.
1279 */
1280 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1281
1282 /*
1283 * Set the flags and timeout value
1284 */
1285 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1286 svc->timeout = u->timeout * HZ;
1287 svc->netmask = u->netmask;
1288
1289 old_sched = svc->scheduler;
1290 if (sched != old_sched) {
1291 /*
1292 * Unbind the old scheduler
1293 */
1294 if ((ret = ip_vs_unbind_scheduler(svc))) {
1295 old_sched = sched;
9e691ed6 1296 goto out_unlock;
1da177e4
LT
1297 }
1298
1299 /*
1300 * Bind the new scheduler
1301 */
1302 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1303 /*
1304 * If ip_vs_bind_scheduler fails, restore the old
1305 * scheduler.
1306 * The main reason of failure is out of memory.
1307 *
1308 * The question is if the old scheduler can be
1309 * restored all the time. TODO: if it cannot be
1310 * restored some time, we must delete the service,
1311 * otherwise the system may crash.
1312 */
1313 ip_vs_bind_scheduler(svc, old_sched);
1314 old_sched = sched;
9e691ed6 1315 goto out_unlock;
1da177e4
LT
1316 }
1317 }
1318
9e691ed6 1319 out_unlock:
1da177e4 1320 write_unlock_bh(&__ip_vs_svc_lock);
8d5803bf 1321#ifdef CONFIG_IP_VS_IPV6
9e691ed6 1322 out:
8d5803bf 1323#endif
1da177e4
LT
1324
1325 if (old_sched)
1326 ip_vs_scheduler_put(old_sched);
1327
1328 return ret;
1329}
1330
1331
1332/*
1333 * Delete a service from the service list
1334 * - The service must be unlinked, unlocked and not referenced!
1335 * - We are called under _bh lock
1336 */
1337static void __ip_vs_del_service(struct ip_vs_service *svc)
1338{
1339 struct ip_vs_dest *dest, *nxt;
1340 struct ip_vs_scheduler *old_sched;
1341
f94fd041
JV
1342 /* Count only IPv4 services for old get/setsockopt interface */
1343 if (svc->af == AF_INET)
1344 ip_vs_num_services--;
1345
1da177e4
LT
1346 ip_vs_kill_estimator(&svc->stats);
1347
1348 /* Unbind scheduler */
1349 old_sched = svc->scheduler;
1350 ip_vs_unbind_scheduler(svc);
1351 if (old_sched)
1352 ip_vs_scheduler_put(old_sched);
1353
1354 /* Unbind app inc */
1355 if (svc->inc) {
1356 ip_vs_app_inc_put(svc->inc);
1357 svc->inc = NULL;
1358 }
1359
1360 /*
1361 * Unlink the whole destination list
1362 */
1363 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1364 __ip_vs_unlink_dest(svc, dest, 0);
1365 __ip_vs_del_dest(dest);
1366 }
1367
1368 /*
1369 * Update the virtual service counters
1370 */
1371 if (svc->port == FTPPORT)
1372 atomic_dec(&ip_vs_ftpsvc_counter);
1373 else if (svc->port == 0)
1374 atomic_dec(&ip_vs_nullsvc_counter);
1375
1376 /*
1377 * Free the service if nobody refers to it
1378 */
1379 if (atomic_read(&svc->refcnt) == 0)
1380 kfree(svc);
1381
1382 /* decrease the module use count */
1383 ip_vs_use_count_dec();
1384}
1385
1386/*
1387 * Delete a service from the service list
1388 */
1389static int ip_vs_del_service(struct ip_vs_service *svc)
1390{
1391 if (svc == NULL)
1392 return -EEXIST;
1393
1394 /*
1395 * Unhash it from the service table
1396 */
1397 write_lock_bh(&__ip_vs_svc_lock);
1398
1399 ip_vs_svc_unhash(svc);
1400
1401 /*
1402 * Wait until all the svc users go away.
1403 */
1404 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 1);
1405
1406 __ip_vs_del_service(svc);
1407
1408 write_unlock_bh(&__ip_vs_svc_lock);
1409
1410 return 0;
1411}
1412
1413
1414/*
1415 * Flush all the virtual services
1416 */
1417static int ip_vs_flush(void)
1418{
1419 int idx;
1420 struct ip_vs_service *svc, *nxt;
1421
1422 /*
1423 * Flush the service table hashed by <protocol,addr,port>
1424 */
1425 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1426 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
1427 write_lock_bh(&__ip_vs_svc_lock);
1428 ip_vs_svc_unhash(svc);
1429 /*
1430 * Wait until all the svc users go away.
1431 */
1432 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1433 __ip_vs_del_service(svc);
1434 write_unlock_bh(&__ip_vs_svc_lock);
1435 }
1436 }
1437
1438 /*
1439 * Flush the service table hashed by fwmark
1440 */
1441 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1442 list_for_each_entry_safe(svc, nxt,
1443 &ip_vs_svc_fwm_table[idx], f_list) {
1444 write_lock_bh(&__ip_vs_svc_lock);
1445 ip_vs_svc_unhash(svc);
1446 /*
1447 * Wait until all the svc users go away.
1448 */
1449 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
1450 __ip_vs_del_service(svc);
1451 write_unlock_bh(&__ip_vs_svc_lock);
1452 }
1453 }
1454
1455 return 0;
1456}
1457
1458
1459/*
1460 * Zero counters in a service or all services
1461 */
1462static int ip_vs_zero_service(struct ip_vs_service *svc)
1463{
1464 struct ip_vs_dest *dest;
1465
1466 write_lock_bh(&__ip_vs_svc_lock);
1467 list_for_each_entry(dest, &svc->destinations, n_list) {
1468 ip_vs_zero_stats(&dest->stats);
1469 }
1470 ip_vs_zero_stats(&svc->stats);
1471 write_unlock_bh(&__ip_vs_svc_lock);
1472 return 0;
1473}
1474
1475static int ip_vs_zero_all(void)
1476{
1477 int idx;
1478 struct ip_vs_service *svc;
1479
1480 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1481 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1482 ip_vs_zero_service(svc);
1483 }
1484 }
1485
1486 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1487 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1488 ip_vs_zero_service(svc);
1489 }
1490 }
1491
1492 ip_vs_zero_stats(&ip_vs_stats);
1493 return 0;
1494}
1495
1496
1497static int
1498proc_do_defense_mode(ctl_table *table, int write, struct file * filp,
1499 void __user *buffer, size_t *lenp, loff_t *ppos)
1500{
1501 int *valp = table->data;
1502 int val = *valp;
1503 int rc;
1504
1505 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1506 if (write && (*valp != val)) {
1507 if ((*valp < 0) || (*valp > 3)) {
1508 /* Restore the correct value */
1509 *valp = val;
1510 } else {
1da177e4 1511 update_defense_level();
1da177e4
LT
1512 }
1513 }
1514 return rc;
1515}
1516
1517
1518static int
1519proc_do_sync_threshold(ctl_table *table, int write, struct file *filp,
1520 void __user *buffer, size_t *lenp, loff_t *ppos)
1521{
1522 int *valp = table->data;
1523 int val[2];
1524 int rc;
1525
1526 /* backup the value first */
1527 memcpy(val, valp, sizeof(val));
1528
1529 rc = proc_dointvec(table, write, filp, buffer, lenp, ppos);
1530 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1531 /* Restore the correct value */
1532 memcpy(valp, val, sizeof(val));
1533 }
1534 return rc;
1535}
1536
1537
1538/*
1539 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1540 */
1541
1542static struct ctl_table vs_vars[] = {
1543 {
1da177e4
LT
1544 .procname = "amemthresh",
1545 .data = &sysctl_ip_vs_amemthresh,
1546 .maxlen = sizeof(int),
1547 .mode = 0644,
1548 .proc_handler = &proc_dointvec,
1549 },
1550#ifdef CONFIG_IP_VS_DEBUG
1551 {
1da177e4
LT
1552 .procname = "debug_level",
1553 .data = &sysctl_ip_vs_debug_level,
1554 .maxlen = sizeof(int),
1555 .mode = 0644,
1556 .proc_handler = &proc_dointvec,
1557 },
1558#endif
1559 {
1da177e4
LT
1560 .procname = "am_droprate",
1561 .data = &sysctl_ip_vs_am_droprate,
1562 .maxlen = sizeof(int),
1563 .mode = 0644,
1564 .proc_handler = &proc_dointvec,
1565 },
1566 {
1da177e4
LT
1567 .procname = "drop_entry",
1568 .data = &sysctl_ip_vs_drop_entry,
1569 .maxlen = sizeof(int),
1570 .mode = 0644,
1571 .proc_handler = &proc_do_defense_mode,
1572 },
1573 {
1da177e4
LT
1574 .procname = "drop_packet",
1575 .data = &sysctl_ip_vs_drop_packet,
1576 .maxlen = sizeof(int),
1577 .mode = 0644,
1578 .proc_handler = &proc_do_defense_mode,
1579 },
1580 {
1da177e4
LT
1581 .procname = "secure_tcp",
1582 .data = &sysctl_ip_vs_secure_tcp,
1583 .maxlen = sizeof(int),
1584 .mode = 0644,
1585 .proc_handler = &proc_do_defense_mode,
1586 },
1587#if 0
1588 {
1da177e4
LT
1589 .procname = "timeout_established",
1590 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1591 .maxlen = sizeof(int),
1592 .mode = 0644,
1593 .proc_handler = &proc_dointvec_jiffies,
1594 },
1595 {
1da177e4
LT
1596 .procname = "timeout_synsent",
1597 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1598 .maxlen = sizeof(int),
1599 .mode = 0644,
1600 .proc_handler = &proc_dointvec_jiffies,
1601 },
1602 {
1da177e4
LT
1603 .procname = "timeout_synrecv",
1604 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1605 .maxlen = sizeof(int),
1606 .mode = 0644,
1607 .proc_handler = &proc_dointvec_jiffies,
1608 },
1609 {
1da177e4
LT
1610 .procname = "timeout_finwait",
1611 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1612 .maxlen = sizeof(int),
1613 .mode = 0644,
1614 .proc_handler = &proc_dointvec_jiffies,
1615 },
1616 {
1da177e4
LT
1617 .procname = "timeout_timewait",
1618 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1619 .maxlen = sizeof(int),
1620 .mode = 0644,
1621 .proc_handler = &proc_dointvec_jiffies,
1622 },
1623 {
1da177e4
LT
1624 .procname = "timeout_close",
1625 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1626 .maxlen = sizeof(int),
1627 .mode = 0644,
1628 .proc_handler = &proc_dointvec_jiffies,
1629 },
1630 {
1da177e4
LT
1631 .procname = "timeout_closewait",
1632 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1633 .maxlen = sizeof(int),
1634 .mode = 0644,
1635 .proc_handler = &proc_dointvec_jiffies,
1636 },
1637 {
1da177e4
LT
1638 .procname = "timeout_lastack",
1639 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1640 .maxlen = sizeof(int),
1641 .mode = 0644,
1642 .proc_handler = &proc_dointvec_jiffies,
1643 },
1644 {
1da177e4
LT
1645 .procname = "timeout_listen",
1646 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1647 .maxlen = sizeof(int),
1648 .mode = 0644,
1649 .proc_handler = &proc_dointvec_jiffies,
1650 },
1651 {
1da177e4
LT
1652 .procname = "timeout_synack",
1653 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1654 .maxlen = sizeof(int),
1655 .mode = 0644,
1656 .proc_handler = &proc_dointvec_jiffies,
1657 },
1658 {
1da177e4
LT
1659 .procname = "timeout_udp",
1660 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1661 .maxlen = sizeof(int),
1662 .mode = 0644,
1663 .proc_handler = &proc_dointvec_jiffies,
1664 },
1665 {
1da177e4
LT
1666 .procname = "timeout_icmp",
1667 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1668 .maxlen = sizeof(int),
1669 .mode = 0644,
1670 .proc_handler = &proc_dointvec_jiffies,
1671 },
1672#endif
1673 {
1da177e4
LT
1674 .procname = "cache_bypass",
1675 .data = &sysctl_ip_vs_cache_bypass,
1676 .maxlen = sizeof(int),
1677 .mode = 0644,
1678 .proc_handler = &proc_dointvec,
1679 },
1680 {
1da177e4
LT
1681 .procname = "expire_nodest_conn",
1682 .data = &sysctl_ip_vs_expire_nodest_conn,
1683 .maxlen = sizeof(int),
1684 .mode = 0644,
1685 .proc_handler = &proc_dointvec,
1686 },
1687 {
1da177e4
LT
1688 .procname = "expire_quiescent_template",
1689 .data = &sysctl_ip_vs_expire_quiescent_template,
1690 .maxlen = sizeof(int),
1691 .mode = 0644,
1692 .proc_handler = &proc_dointvec,
1693 },
1694 {
1da177e4
LT
1695 .procname = "sync_threshold",
1696 .data = &sysctl_ip_vs_sync_threshold,
1697 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1698 .mode = 0644,
1699 .proc_handler = &proc_do_sync_threshold,
1700 },
1701 {
1da177e4
LT
1702 .procname = "nat_icmp_send",
1703 .data = &sysctl_ip_vs_nat_icmp_send,
1704 .maxlen = sizeof(int),
1705 .mode = 0644,
1706 .proc_handler = &proc_dointvec,
1707 },
1708 { .ctl_name = 0 }
1709};
1710
5587da55 1711const struct ctl_path net_vs_ctl_path[] = {
90754f8e
PE
1712 { .procname = "net", .ctl_name = CTL_NET, },
1713 { .procname = "ipv4", .ctl_name = NET_IPV4, },
1714 { .procname = "vs", },
1715 { }
1da177e4 1716};
90754f8e 1717EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1da177e4
LT
1718
1719static struct ctl_table_header * sysctl_header;
1720
1721#ifdef CONFIG_PROC_FS
1722
1723struct ip_vs_iter {
1724 struct list_head *table;
1725 int bucket;
1726};
1727
1728/*
1729 * Write the contents of the VS rule table to a PROCfs file.
1730 * (It is kept just for backward compatibility)
1731 */
1732static inline const char *ip_vs_fwd_name(unsigned flags)
1733{
1734 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1735 case IP_VS_CONN_F_LOCALNODE:
1736 return "Local";
1737 case IP_VS_CONN_F_TUNNEL:
1738 return "Tunnel";
1739 case IP_VS_CONN_F_DROUTE:
1740 return "Route";
1741 default:
1742 return "Masq";
1743 }
1744}
1745
1746
1747/* Get the Nth entry in the two lists */
1748static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1749{
1750 struct ip_vs_iter *iter = seq->private;
1751 int idx;
1752 struct ip_vs_service *svc;
1753
1754 /* look in hash by protocol */
1755 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1756 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1757 if (pos-- == 0){
1758 iter->table = ip_vs_svc_table;
1759 iter->bucket = idx;
1760 return svc;
1761 }
1762 }
1763 }
1764
1765 /* keep looking in fwmark */
1766 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1767 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1768 if (pos-- == 0) {
1769 iter->table = ip_vs_svc_fwm_table;
1770 iter->bucket = idx;
1771 return svc;
1772 }
1773 }
1774 }
1775
1776 return NULL;
1777}
1778
1779static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
563e94f0 1780__acquires(__ip_vs_svc_lock)
1da177e4
LT
1781{
1782
1783 read_lock_bh(&__ip_vs_svc_lock);
1784 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1785}
1786
1787
1788static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1789{
1790 struct list_head *e;
1791 struct ip_vs_iter *iter;
1792 struct ip_vs_service *svc;
1793
1794 ++*pos;
1795 if (v == SEQ_START_TOKEN)
1796 return ip_vs_info_array(seq,0);
1797
1798 svc = v;
1799 iter = seq->private;
1800
1801 if (iter->table == ip_vs_svc_table) {
1802 /* next service in table hashed by protocol */
1803 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1804 return list_entry(e, struct ip_vs_service, s_list);
1805
1806
1807 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1808 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1809 s_list) {
1810 return svc;
1811 }
1812 }
1813
1814 iter->table = ip_vs_svc_fwm_table;
1815 iter->bucket = -1;
1816 goto scan_fwmark;
1817 }
1818
1819 /* next service in hashed by fwmark */
1820 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1821 return list_entry(e, struct ip_vs_service, f_list);
1822
1823 scan_fwmark:
1824 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1825 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1826 f_list)
1827 return svc;
1828 }
1829
1830 return NULL;
1831}
1832
1833static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
563e94f0 1834__releases(__ip_vs_svc_lock)
1da177e4
LT
1835{
1836 read_unlock_bh(&__ip_vs_svc_lock);
1837}
1838
1839
1840static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1841{
1842 if (v == SEQ_START_TOKEN) {
1843 seq_printf(seq,
1844 "IP Virtual Server version %d.%d.%d (size=%d)\n",
1845 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
1846 seq_puts(seq,
1847 "Prot LocalAddress:Port Scheduler Flags\n");
1848 seq_puts(seq,
1849 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1850 } else {
1851 const struct ip_vs_service *svc = v;
1852 const struct ip_vs_iter *iter = seq->private;
1853 const struct ip_vs_dest *dest;
1854
667a5f18
VB
1855 if (iter->table == ip_vs_svc_table) {
1856#ifdef CONFIG_IP_VS_IPV6
1857 if (svc->af == AF_INET6)
5b095d98 1858 seq_printf(seq, "%s [%pI6]:%04X %s ",
667a5f18 1859 ip_vs_proto_name(svc->protocol),
38ff4fa4 1860 &svc->addr.in6,
667a5f18
VB
1861 ntohs(svc->port),
1862 svc->scheduler->name);
1863 else
1864#endif
1865 seq_printf(seq, "%s %08X:%04X %s ",
1866 ip_vs_proto_name(svc->protocol),
1867 ntohl(svc->addr.ip),
1868 ntohs(svc->port),
1869 svc->scheduler->name);
1870 } else {
1da177e4
LT
1871 seq_printf(seq, "FWM %08X %s ",
1872 svc->fwmark, svc->scheduler->name);
667a5f18 1873 }
1da177e4
LT
1874
1875 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1876 seq_printf(seq, "persistent %d %08X\n",
1877 svc->timeout,
1878 ntohl(svc->netmask));
1879 else
1880 seq_putc(seq, '\n');
1881
1882 list_for_each_entry(dest, &svc->destinations, n_list) {
667a5f18
VB
1883#ifdef CONFIG_IP_VS_IPV6
1884 if (dest->af == AF_INET6)
1885 seq_printf(seq,
5b095d98 1886 " -> [%pI6]:%04X"
667a5f18 1887 " %-7s %-6d %-10d %-10d\n",
38ff4fa4 1888 &dest->addr.in6,
667a5f18
VB
1889 ntohs(dest->port),
1890 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1891 atomic_read(&dest->weight),
1892 atomic_read(&dest->activeconns),
1893 atomic_read(&dest->inactconns));
1894 else
1895#endif
1896 seq_printf(seq,
1897 " -> %08X:%04X "
1898 "%-7s %-6d %-10d %-10d\n",
1899 ntohl(dest->addr.ip),
1900 ntohs(dest->port),
1901 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1902 atomic_read(&dest->weight),
1903 atomic_read(&dest->activeconns),
1904 atomic_read(&dest->inactconns));
1905
1da177e4
LT
1906 }
1907 }
1908 return 0;
1909}
1910
56b3d975 1911static const struct seq_operations ip_vs_info_seq_ops = {
1da177e4
LT
1912 .start = ip_vs_info_seq_start,
1913 .next = ip_vs_info_seq_next,
1914 .stop = ip_vs_info_seq_stop,
1915 .show = ip_vs_info_seq_show,
1916};
1917
1918static int ip_vs_info_open(struct inode *inode, struct file *file)
1919{
cf7732e4
PE
1920 return seq_open_private(file, &ip_vs_info_seq_ops,
1921 sizeof(struct ip_vs_iter));
1da177e4
LT
1922}
1923
9a32144e 1924static const struct file_operations ip_vs_info_fops = {
1da177e4
LT
1925 .owner = THIS_MODULE,
1926 .open = ip_vs_info_open,
1927 .read = seq_read,
1928 .llseek = seq_lseek,
1929 .release = seq_release_private,
1930};
1931
1932#endif
1933
519e49e8
SW
1934struct ip_vs_stats ip_vs_stats = {
1935 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1936};
1da177e4
LT
1937
1938#ifdef CONFIG_PROC_FS
1939static int ip_vs_stats_show(struct seq_file *seq, void *v)
1940{
1941
1942/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1943 seq_puts(seq,
1944 " Total Incoming Outgoing Incoming Outgoing\n");
1945 seq_printf(seq,
1946 " Conns Packets Packets Bytes Bytes\n");
1947
1948 spin_lock_bh(&ip_vs_stats.lock);
e9c0ce23
SW
1949 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1950 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1951 (unsigned long long) ip_vs_stats.ustats.inbytes,
1952 (unsigned long long) ip_vs_stats.ustats.outbytes);
1da177e4
LT
1953
1954/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1955 seq_puts(seq,
1956 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1957 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
e9c0ce23
SW
1958 ip_vs_stats.ustats.cps,
1959 ip_vs_stats.ustats.inpps,
1960 ip_vs_stats.ustats.outpps,
1961 ip_vs_stats.ustats.inbps,
1962 ip_vs_stats.ustats.outbps);
1da177e4
LT
1963 spin_unlock_bh(&ip_vs_stats.lock);
1964
1965 return 0;
1966}
1967
1968static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1969{
1970 return single_open(file, ip_vs_stats_show, NULL);
1971}
1972
9a32144e 1973static const struct file_operations ip_vs_stats_fops = {
1da177e4
LT
1974 .owner = THIS_MODULE,
1975 .open = ip_vs_stats_seq_open,
1976 .read = seq_read,
1977 .llseek = seq_lseek,
1978 .release = single_release,
1979};
1980
1981#endif
1982
1983/*
1984 * Set timeout values for tcp tcpfin udp in the timeout_table.
1985 */
1986static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1987{
1988 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1989 u->tcp_timeout,
1990 u->tcp_fin_timeout,
1991 u->udp_timeout);
1992
1993#ifdef CONFIG_IP_VS_PROTO_TCP
1994 if (u->tcp_timeout) {
1995 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
1996 = u->tcp_timeout * HZ;
1997 }
1998
1999 if (u->tcp_fin_timeout) {
2000 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
2001 = u->tcp_fin_timeout * HZ;
2002 }
2003#endif
2004
2005#ifdef CONFIG_IP_VS_PROTO_UDP
2006 if (u->udp_timeout) {
2007 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
2008 = u->udp_timeout * HZ;
2009 }
2010#endif
2011 return 0;
2012}
2013
2014
2015#define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2016#define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2017#define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2018 sizeof(struct ip_vs_dest_user))
2019#define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2020#define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2021#define MAX_ARG_LEN SVCDEST_ARG_LEN
2022
9b5b5cff 2023static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
1da177e4
LT
2024 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2025 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2026 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2027 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2028 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2029 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2030 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2031 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2032 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2033 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2034 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2035};
2036
c860c6b1
JV
2037static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2038 struct ip_vs_service_user *usvc_compat)
2039{
2040 usvc->af = AF_INET;
2041 usvc->protocol = usvc_compat->protocol;
2042 usvc->addr.ip = usvc_compat->addr;
2043 usvc->port = usvc_compat->port;
2044 usvc->fwmark = usvc_compat->fwmark;
2045
2046 /* Deep copy of sched_name is not needed here */
2047 usvc->sched_name = usvc_compat->sched_name;
2048
2049 usvc->flags = usvc_compat->flags;
2050 usvc->timeout = usvc_compat->timeout;
2051 usvc->netmask = usvc_compat->netmask;
2052}
2053
2054static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2055 struct ip_vs_dest_user *udest_compat)
2056{
2057 udest->addr.ip = udest_compat->addr;
2058 udest->port = udest_compat->port;
2059 udest->conn_flags = udest_compat->conn_flags;
2060 udest->weight = udest_compat->weight;
2061 udest->u_threshold = udest_compat->u_threshold;
2062 udest->l_threshold = udest_compat->l_threshold;
2063}
2064
1da177e4
LT
2065static int
2066do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2067{
2068 int ret;
2069 unsigned char arg[MAX_ARG_LEN];
c860c6b1
JV
2070 struct ip_vs_service_user *usvc_compat;
2071 struct ip_vs_service_user_kern usvc;
1da177e4 2072 struct ip_vs_service *svc;
c860c6b1
JV
2073 struct ip_vs_dest_user *udest_compat;
2074 struct ip_vs_dest_user_kern udest;
1da177e4
LT
2075
2076 if (!capable(CAP_NET_ADMIN))
2077 return -EPERM;
2078
2079 if (len != set_arglen[SET_CMDID(cmd)]) {
2080 IP_VS_ERR("set_ctl: len %u != %u\n",
2081 len, set_arglen[SET_CMDID(cmd)]);
2082 return -EINVAL;
2083 }
2084
2085 if (copy_from_user(arg, user, len) != 0)
2086 return -EFAULT;
2087
2088 /* increase the module use count */
2089 ip_vs_use_count_inc();
2090
14cc3e2b 2091 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
1da177e4
LT
2092 ret = -ERESTARTSYS;
2093 goto out_dec;
2094 }
2095
2096 if (cmd == IP_VS_SO_SET_FLUSH) {
2097 /* Flush the virtual service */
2098 ret = ip_vs_flush();
2099 goto out_unlock;
2100 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2101 /* Set timeout values for (tcp tcpfin udp) */
2102 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2103 goto out_unlock;
2104 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2105 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2106 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2107 goto out_unlock;
2108 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2109 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2110 ret = stop_sync_thread(dm->state);
2111 goto out_unlock;
2112 }
2113
c860c6b1
JV
2114 usvc_compat = (struct ip_vs_service_user *)arg;
2115 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2116
2117 /* We only use the new structs internally, so copy userspace compat
2118 * structs to extended internal versions */
2119 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2120 ip_vs_copy_udest_compat(&udest, udest_compat);
1da177e4
LT
2121
2122 if (cmd == IP_VS_SO_SET_ZERO) {
2123 /* if no service address is set, zero counters in all */
c860c6b1 2124 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
1da177e4
LT
2125 ret = ip_vs_zero_all();
2126 goto out_unlock;
2127 }
2128 }
2129
2130 /* Check for valid protocol: TCP or UDP, even for fwmark!=0 */
c860c6b1 2131 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP) {
14d5e834
HH
2132 IP_VS_ERR("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2133 usvc.protocol, &usvc.addr.ip,
c860c6b1 2134 ntohs(usvc.port), usvc.sched_name);
1da177e4
LT
2135 ret = -EFAULT;
2136 goto out_unlock;
2137 }
2138
2139 /* Lookup the exact service by <protocol, addr, port> or fwmark */
c860c6b1 2140 if (usvc.fwmark == 0)
b18610de
JV
2141 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
2142 &usvc.addr, usvc.port);
1da177e4 2143 else
b18610de 2144 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
1da177e4
LT
2145
2146 if (cmd != IP_VS_SO_SET_ADD
c860c6b1 2147 && (svc == NULL || svc->protocol != usvc.protocol)) {
1da177e4
LT
2148 ret = -ESRCH;
2149 goto out_unlock;
2150 }
2151
2152 switch (cmd) {
2153 case IP_VS_SO_SET_ADD:
2154 if (svc != NULL)
2155 ret = -EEXIST;
2156 else
c860c6b1 2157 ret = ip_vs_add_service(&usvc, &svc);
1da177e4
LT
2158 break;
2159 case IP_VS_SO_SET_EDIT:
c860c6b1 2160 ret = ip_vs_edit_service(svc, &usvc);
1da177e4
LT
2161 break;
2162 case IP_VS_SO_SET_DEL:
2163 ret = ip_vs_del_service(svc);
2164 if (!ret)
2165 goto out_unlock;
2166 break;
2167 case IP_VS_SO_SET_ZERO:
2168 ret = ip_vs_zero_service(svc);
2169 break;
2170 case IP_VS_SO_SET_ADDDEST:
c860c6b1 2171 ret = ip_vs_add_dest(svc, &udest);
1da177e4
LT
2172 break;
2173 case IP_VS_SO_SET_EDITDEST:
c860c6b1 2174 ret = ip_vs_edit_dest(svc, &udest);
1da177e4
LT
2175 break;
2176 case IP_VS_SO_SET_DELDEST:
c860c6b1 2177 ret = ip_vs_del_dest(svc, &udest);
1da177e4
LT
2178 break;
2179 default:
2180 ret = -EINVAL;
2181 }
2182
2183 if (svc)
2184 ip_vs_service_put(svc);
2185
2186 out_unlock:
14cc3e2b 2187 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2188 out_dec:
2189 /* decrease the module use count */
2190 ip_vs_use_count_dec();
2191
2192 return ret;
2193}
2194
2195
2196static void
2197ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2198{
2199 spin_lock_bh(&src->lock);
e9c0ce23 2200 memcpy(dst, &src->ustats, sizeof(*dst));
1da177e4
LT
2201 spin_unlock_bh(&src->lock);
2202}
2203
2204static void
2205ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2206{
2207 dst->protocol = src->protocol;
e7ade46a 2208 dst->addr = src->addr.ip;
1da177e4
LT
2209 dst->port = src->port;
2210 dst->fwmark = src->fwmark;
4da62fc7 2211 strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
1da177e4
LT
2212 dst->flags = src->flags;
2213 dst->timeout = src->timeout / HZ;
2214 dst->netmask = src->netmask;
2215 dst->num_dests = src->num_dests;
2216 ip_vs_copy_stats(&dst->stats, &src->stats);
2217}
2218
2219static inline int
2220__ip_vs_get_service_entries(const struct ip_vs_get_services *get,
2221 struct ip_vs_get_services __user *uptr)
2222{
2223 int idx, count=0;
2224 struct ip_vs_service *svc;
2225 struct ip_vs_service_entry entry;
2226 int ret = 0;
2227
2228 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2229 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
f94fd041
JV
2230 /* Only expose IPv4 entries to old interface */
2231 if (svc->af != AF_INET)
2232 continue;
2233
1da177e4
LT
2234 if (count >= get->num_services)
2235 goto out;
4da62fc7 2236 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2237 ip_vs_copy_service(&entry, svc);
2238 if (copy_to_user(&uptr->entrytable[count],
2239 &entry, sizeof(entry))) {
2240 ret = -EFAULT;
2241 goto out;
2242 }
2243 count++;
2244 }
2245 }
2246
2247 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2248 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
f94fd041
JV
2249 /* Only expose IPv4 entries to old interface */
2250 if (svc->af != AF_INET)
2251 continue;
2252
1da177e4
LT
2253 if (count >= get->num_services)
2254 goto out;
4da62fc7 2255 memset(&entry, 0, sizeof(entry));
1da177e4
LT
2256 ip_vs_copy_service(&entry, svc);
2257 if (copy_to_user(&uptr->entrytable[count],
2258 &entry, sizeof(entry))) {
2259 ret = -EFAULT;
2260 goto out;
2261 }
2262 count++;
2263 }
2264 }
2265 out:
2266 return ret;
2267}
2268
2269static inline int
2270__ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
2271 struct ip_vs_get_dests __user *uptr)
2272{
2273 struct ip_vs_service *svc;
b18610de 2274 union nf_inet_addr addr = { .ip = get->addr };
1da177e4
LT
2275 int ret = 0;
2276
2277 if (get->fwmark)
b18610de 2278 svc = __ip_vs_svc_fwm_get(AF_INET, get->fwmark);
1da177e4 2279 else
b18610de
JV
2280 svc = __ip_vs_service_get(AF_INET, get->protocol, &addr,
2281 get->port);
2282
1da177e4
LT
2283 if (svc) {
2284 int count = 0;
2285 struct ip_vs_dest *dest;
2286 struct ip_vs_dest_entry entry;
2287
2288 list_for_each_entry(dest, &svc->destinations, n_list) {
2289 if (count >= get->num_dests)
2290 break;
2291
e7ade46a 2292 entry.addr = dest->addr.ip;
1da177e4
LT
2293 entry.port = dest->port;
2294 entry.conn_flags = atomic_read(&dest->conn_flags);
2295 entry.weight = atomic_read(&dest->weight);
2296 entry.u_threshold = dest->u_threshold;
2297 entry.l_threshold = dest->l_threshold;
2298 entry.activeconns = atomic_read(&dest->activeconns);
2299 entry.inactconns = atomic_read(&dest->inactconns);
2300 entry.persistconns = atomic_read(&dest->persistconns);
2301 ip_vs_copy_stats(&entry.stats, &dest->stats);
2302 if (copy_to_user(&uptr->entrytable[count],
2303 &entry, sizeof(entry))) {
2304 ret = -EFAULT;
2305 break;
2306 }
2307 count++;
2308 }
2309 ip_vs_service_put(svc);
2310 } else
2311 ret = -ESRCH;
2312 return ret;
2313}
2314
2315static inline void
2316__ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
2317{
2318#ifdef CONFIG_IP_VS_PROTO_TCP
2319 u->tcp_timeout =
2320 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2321 u->tcp_fin_timeout =
2322 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2323#endif
2324#ifdef CONFIG_IP_VS_PROTO_UDP
2325 u->udp_timeout =
2326 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2327#endif
2328}
2329
2330
2331#define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2332#define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2333#define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2334#define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2335#define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2336#define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2337#define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2338
9b5b5cff 2339static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
1da177e4
LT
2340 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2341 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2342 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2343 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2344 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2345 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2346 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2347};
2348
2349static int
2350do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2351{
2352 unsigned char arg[128];
2353 int ret = 0;
2354
2355 if (!capable(CAP_NET_ADMIN))
2356 return -EPERM;
2357
2358 if (*len < get_arglen[GET_CMDID(cmd)]) {
2359 IP_VS_ERR("get_ctl: len %u < %u\n",
2360 *len, get_arglen[GET_CMDID(cmd)]);
2361 return -EINVAL;
2362 }
2363
2364 if (copy_from_user(arg, user, get_arglen[GET_CMDID(cmd)]) != 0)
2365 return -EFAULT;
2366
14cc3e2b 2367 if (mutex_lock_interruptible(&__ip_vs_mutex))
1da177e4
LT
2368 return -ERESTARTSYS;
2369
2370 switch (cmd) {
2371 case IP_VS_SO_GET_VERSION:
2372 {
2373 char buf[64];
2374
2375 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2376 NVERSION(IP_VS_VERSION_CODE), IP_VS_CONN_TAB_SIZE);
2377 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2378 ret = -EFAULT;
2379 goto out;
2380 }
2381 *len = strlen(buf)+1;
2382 }
2383 break;
2384
2385 case IP_VS_SO_GET_INFO:
2386 {
2387 struct ip_vs_getinfo info;
2388 info.version = IP_VS_VERSION_CODE;
2389 info.size = IP_VS_CONN_TAB_SIZE;
2390 info.num_services = ip_vs_num_services;
2391 if (copy_to_user(user, &info, sizeof(info)) != 0)
2392 ret = -EFAULT;
2393 }
2394 break;
2395
2396 case IP_VS_SO_GET_SERVICES:
2397 {
2398 struct ip_vs_get_services *get;
2399 int size;
2400
2401 get = (struct ip_vs_get_services *)arg;
2402 size = sizeof(*get) +
2403 sizeof(struct ip_vs_service_entry) * get->num_services;
2404 if (*len != size) {
2405 IP_VS_ERR("length: %u != %u\n", *len, size);
2406 ret = -EINVAL;
2407 goto out;
2408 }
2409 ret = __ip_vs_get_service_entries(get, user);
2410 }
2411 break;
2412
2413 case IP_VS_SO_GET_SERVICE:
2414 {
2415 struct ip_vs_service_entry *entry;
2416 struct ip_vs_service *svc;
b18610de 2417 union nf_inet_addr addr;
1da177e4
LT
2418
2419 entry = (struct ip_vs_service_entry *)arg;
b18610de 2420 addr.ip = entry->addr;
1da177e4 2421 if (entry->fwmark)
b18610de 2422 svc = __ip_vs_svc_fwm_get(AF_INET, entry->fwmark);
1da177e4 2423 else
b18610de
JV
2424 svc = __ip_vs_service_get(AF_INET, entry->protocol,
2425 &addr, entry->port);
1da177e4
LT
2426 if (svc) {
2427 ip_vs_copy_service(entry, svc);
2428 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2429 ret = -EFAULT;
2430 ip_vs_service_put(svc);
2431 } else
2432 ret = -ESRCH;
2433 }
2434 break;
2435
2436 case IP_VS_SO_GET_DESTS:
2437 {
2438 struct ip_vs_get_dests *get;
2439 int size;
2440
2441 get = (struct ip_vs_get_dests *)arg;
2442 size = sizeof(*get) +
2443 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2444 if (*len != size) {
2445 IP_VS_ERR("length: %u != %u\n", *len, size);
2446 ret = -EINVAL;
2447 goto out;
2448 }
2449 ret = __ip_vs_get_dest_entries(get, user);
2450 }
2451 break;
2452
2453 case IP_VS_SO_GET_TIMEOUT:
2454 {
2455 struct ip_vs_timeout_user t;
2456
2457 __ip_vs_get_timeouts(&t);
2458 if (copy_to_user(user, &t, sizeof(t)) != 0)
2459 ret = -EFAULT;
2460 }
2461 break;
2462
2463 case IP_VS_SO_GET_DAEMON:
2464 {
2465 struct ip_vs_daemon_user d[2];
2466
2467 memset(&d, 0, sizeof(d));
2468 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2469 d[0].state = IP_VS_STATE_MASTER;
4da62fc7 2470 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
1da177e4
LT
2471 d[0].syncid = ip_vs_master_syncid;
2472 }
2473 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2474 d[1].state = IP_VS_STATE_BACKUP;
4da62fc7 2475 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
1da177e4
LT
2476 d[1].syncid = ip_vs_backup_syncid;
2477 }
2478 if (copy_to_user(user, &d, sizeof(d)) != 0)
2479 ret = -EFAULT;
2480 }
2481 break;
2482
2483 default:
2484 ret = -EINVAL;
2485 }
2486
2487 out:
14cc3e2b 2488 mutex_unlock(&__ip_vs_mutex);
1da177e4
LT
2489 return ret;
2490}
2491
2492
2493static struct nf_sockopt_ops ip_vs_sockopts = {
2494 .pf = PF_INET,
2495 .set_optmin = IP_VS_BASE_CTL,
2496 .set_optmax = IP_VS_SO_SET_MAX+1,
2497 .set = do_ip_vs_set_ctl,
2498 .get_optmin = IP_VS_BASE_CTL,
2499 .get_optmax = IP_VS_SO_GET_MAX+1,
2500 .get = do_ip_vs_get_ctl,
16fcec35 2501 .owner = THIS_MODULE,
1da177e4
LT
2502};
2503
9a812198
JV
2504/*
2505 * Generic Netlink interface
2506 */
2507
2508/* IPVS genetlink family */
2509static struct genl_family ip_vs_genl_family = {
2510 .id = GENL_ID_GENERATE,
2511 .hdrsize = 0,
2512 .name = IPVS_GENL_NAME,
2513 .version = IPVS_GENL_VERSION,
2514 .maxattr = IPVS_CMD_MAX,
2515};
2516
2517/* Policy used for first-level command attributes */
2518static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2519 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2520 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2521 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2522 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2523 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2524 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2525};
2526
2527/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2528static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2529 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2530 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2531 .len = IP_VS_IFNAME_MAXLEN },
2532 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2533};
2534
2535/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2536static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2537 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2538 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2539 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2540 .len = sizeof(union nf_inet_addr) },
2541 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2542 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2543 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2544 .len = IP_VS_SCHEDNAME_MAXLEN },
2545 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2546 .len = sizeof(struct ip_vs_flags) },
2547 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2548 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2549 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2550};
2551
2552/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2553static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2554 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2555 .len = sizeof(union nf_inet_addr) },
2556 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2557 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2558 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2559 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2560 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2561 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2562 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2563 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2564 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2565};
2566
2567static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2568 struct ip_vs_stats *stats)
2569{
2570 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2571 if (!nl_stats)
2572 return -EMSGSIZE;
2573
2574 spin_lock_bh(&stats->lock);
2575
e9c0ce23
SW
2576 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2577 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2578 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2579 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2580 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2581 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2582 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2583 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2584 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2585 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
9a812198
JV
2586
2587 spin_unlock_bh(&stats->lock);
2588
2589 nla_nest_end(skb, nl_stats);
2590
2591 return 0;
2592
2593nla_put_failure:
2594 spin_unlock_bh(&stats->lock);
2595 nla_nest_cancel(skb, nl_stats);
2596 return -EMSGSIZE;
2597}
2598
2599static int ip_vs_genl_fill_service(struct sk_buff *skb,
2600 struct ip_vs_service *svc)
2601{
2602 struct nlattr *nl_service;
2603 struct ip_vs_flags flags = { .flags = svc->flags,
2604 .mask = ~0 };
2605
2606 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2607 if (!nl_service)
2608 return -EMSGSIZE;
2609
f94fd041 2610 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
9a812198
JV
2611
2612 if (svc->fwmark) {
2613 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2614 } else {
2615 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2616 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2617 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2618 }
2619
2620 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2621 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2622 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2623 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2624
2625 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2626 goto nla_put_failure;
2627
2628 nla_nest_end(skb, nl_service);
2629
2630 return 0;
2631
2632nla_put_failure:
2633 nla_nest_cancel(skb, nl_service);
2634 return -EMSGSIZE;
2635}
2636
2637static int ip_vs_genl_dump_service(struct sk_buff *skb,
2638 struct ip_vs_service *svc,
2639 struct netlink_callback *cb)
2640{
2641 void *hdr;
2642
2643 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2644 &ip_vs_genl_family, NLM_F_MULTI,
2645 IPVS_CMD_NEW_SERVICE);
2646 if (!hdr)
2647 return -EMSGSIZE;
2648
2649 if (ip_vs_genl_fill_service(skb, svc) < 0)
2650 goto nla_put_failure;
2651
2652 return genlmsg_end(skb, hdr);
2653
2654nla_put_failure:
2655 genlmsg_cancel(skb, hdr);
2656 return -EMSGSIZE;
2657}
2658
2659static int ip_vs_genl_dump_services(struct sk_buff *skb,
2660 struct netlink_callback *cb)
2661{
2662 int idx = 0, i;
2663 int start = cb->args[0];
2664 struct ip_vs_service *svc;
2665
2666 mutex_lock(&__ip_vs_mutex);
2667 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2668 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2669 if (++idx <= start)
2670 continue;
2671 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2672 idx--;
2673 goto nla_put_failure;
2674 }
2675 }
2676 }
2677
2678 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2679 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2680 if (++idx <= start)
2681 continue;
2682 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2683 idx--;
2684 goto nla_put_failure;
2685 }
2686 }
2687 }
2688
2689nla_put_failure:
2690 mutex_unlock(&__ip_vs_mutex);
2691 cb->args[0] = idx;
2692
2693 return skb->len;
2694}
2695
c860c6b1 2696static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
9a812198
JV
2697 struct nlattr *nla, int full_entry)
2698{
2699 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2700 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2701
2702 /* Parse mandatory identifying service fields first */
2703 if (nla == NULL ||
2704 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2705 return -EINVAL;
2706
2707 nla_af = attrs[IPVS_SVC_ATTR_AF];
2708 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2709 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2710 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2711 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2712
2713 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2714 return -EINVAL;
2715
c860c6b1 2716 usvc->af = nla_get_u16(nla_af);
f94fd041
JV
2717#ifdef CONFIG_IP_VS_IPV6
2718 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2719#else
2720 if (usvc->af != AF_INET)
2721#endif
9a812198
JV
2722 return -EAFNOSUPPORT;
2723
2724 if (nla_fwmark) {
2725 usvc->protocol = IPPROTO_TCP;
2726 usvc->fwmark = nla_get_u32(nla_fwmark);
2727 } else {
2728 usvc->protocol = nla_get_u16(nla_protocol);
2729 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2730 usvc->port = nla_get_u16(nla_port);
2731 usvc->fwmark = 0;
2732 }
2733
2734 /* If a full entry was requested, check for the additional fields */
2735 if (full_entry) {
2736 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2737 *nla_netmask;
2738 struct ip_vs_flags flags;
2739 struct ip_vs_service *svc;
2740
2741 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2742 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2743 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2744 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2745
2746 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2747 return -EINVAL;
2748
2749 nla_memcpy(&flags, nla_flags, sizeof(flags));
2750
2751 /* prefill flags from service if it already exists */
2752 if (usvc->fwmark)
b18610de 2753 svc = __ip_vs_svc_fwm_get(usvc->af, usvc->fwmark);
9a812198 2754 else
b18610de
JV
2755 svc = __ip_vs_service_get(usvc->af, usvc->protocol,
2756 &usvc->addr, usvc->port);
9a812198
JV
2757 if (svc) {
2758 usvc->flags = svc->flags;
2759 ip_vs_service_put(svc);
2760 } else
2761 usvc->flags = 0;
2762
2763 /* set new flags from userland */
2764 usvc->flags = (usvc->flags & ~flags.mask) |
2765 (flags.flags & flags.mask);
c860c6b1 2766 usvc->sched_name = nla_data(nla_sched);
9a812198
JV
2767 usvc->timeout = nla_get_u32(nla_timeout);
2768 usvc->netmask = nla_get_u32(nla_netmask);
2769 }
2770
2771 return 0;
2772}
2773
2774static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2775{
c860c6b1 2776 struct ip_vs_service_user_kern usvc;
9a812198
JV
2777 int ret;
2778
2779 ret = ip_vs_genl_parse_service(&usvc, nla, 0);
2780 if (ret)
2781 return ERR_PTR(ret);
2782
2783 if (usvc.fwmark)
b18610de 2784 return __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
9a812198 2785 else
b18610de
JV
2786 return __ip_vs_service_get(usvc.af, usvc.protocol,
2787 &usvc.addr, usvc.port);
9a812198
JV
2788}
2789
2790static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2791{
2792 struct nlattr *nl_dest;
2793
2794 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2795 if (!nl_dest)
2796 return -EMSGSIZE;
2797
2798 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2799 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2800
2801 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2802 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2803 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2804 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2805 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2806 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2807 atomic_read(&dest->activeconns));
2808 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2809 atomic_read(&dest->inactconns));
2810 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2811 atomic_read(&dest->persistconns));
2812
2813 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2814 goto nla_put_failure;
2815
2816 nla_nest_end(skb, nl_dest);
2817
2818 return 0;
2819
2820nla_put_failure:
2821 nla_nest_cancel(skb, nl_dest);
2822 return -EMSGSIZE;
2823}
2824
2825static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2826 struct netlink_callback *cb)
2827{
2828 void *hdr;
2829
2830 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2831 &ip_vs_genl_family, NLM_F_MULTI,
2832 IPVS_CMD_NEW_DEST);
2833 if (!hdr)
2834 return -EMSGSIZE;
2835
2836 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2837 goto nla_put_failure;
2838
2839 return genlmsg_end(skb, hdr);
2840
2841nla_put_failure:
2842 genlmsg_cancel(skb, hdr);
2843 return -EMSGSIZE;
2844}
2845
2846static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2847 struct netlink_callback *cb)
2848{
2849 int idx = 0;
2850 int start = cb->args[0];
2851 struct ip_vs_service *svc;
2852 struct ip_vs_dest *dest;
2853 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2854
2855 mutex_lock(&__ip_vs_mutex);
2856
2857 /* Try to find the service for which to dump destinations */
2858 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2859 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2860 goto out_err;
2861
2862 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2863 if (IS_ERR(svc) || svc == NULL)
2864 goto out_err;
2865
2866 /* Dump the destinations */
2867 list_for_each_entry(dest, &svc->destinations, n_list) {
2868 if (++idx <= start)
2869 continue;
2870 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2871 idx--;
2872 goto nla_put_failure;
2873 }
2874 }
2875
2876nla_put_failure:
2877 cb->args[0] = idx;
2878 ip_vs_service_put(svc);
2879
2880out_err:
2881 mutex_unlock(&__ip_vs_mutex);
2882
2883 return skb->len;
2884}
2885
c860c6b1 2886static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
9a812198
JV
2887 struct nlattr *nla, int full_entry)
2888{
2889 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2890 struct nlattr *nla_addr, *nla_port;
2891
2892 /* Parse mandatory identifying destination fields first */
2893 if (nla == NULL ||
2894 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2895 return -EINVAL;
2896
2897 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2898 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2899
2900 if (!(nla_addr && nla_port))
2901 return -EINVAL;
2902
2903 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2904 udest->port = nla_get_u16(nla_port);
2905
2906 /* If a full entry was requested, check for the additional fields */
2907 if (full_entry) {
2908 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2909 *nla_l_thresh;
2910
2911 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2912 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2913 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2914 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2915
2916 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2917 return -EINVAL;
2918
2919 udest->conn_flags = nla_get_u32(nla_fwd)
2920 & IP_VS_CONN_F_FWD_MASK;
2921 udest->weight = nla_get_u32(nla_weight);
2922 udest->u_threshold = nla_get_u32(nla_u_thresh);
2923 udest->l_threshold = nla_get_u32(nla_l_thresh);
2924 }
2925
2926 return 0;
2927}
2928
2929static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2930 const char *mcast_ifn, __be32 syncid)
2931{
2932 struct nlattr *nl_daemon;
2933
2934 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2935 if (!nl_daemon)
2936 return -EMSGSIZE;
2937
2938 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2939 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2940 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2941
2942 nla_nest_end(skb, nl_daemon);
2943
2944 return 0;
2945
2946nla_put_failure:
2947 nla_nest_cancel(skb, nl_daemon);
2948 return -EMSGSIZE;
2949}
2950
2951static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2952 const char *mcast_ifn, __be32 syncid,
2953 struct netlink_callback *cb)
2954{
2955 void *hdr;
2956 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2957 &ip_vs_genl_family, NLM_F_MULTI,
2958 IPVS_CMD_NEW_DAEMON);
2959 if (!hdr)
2960 return -EMSGSIZE;
2961
2962 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2963 goto nla_put_failure;
2964
2965 return genlmsg_end(skb, hdr);
2966
2967nla_put_failure:
2968 genlmsg_cancel(skb, hdr);
2969 return -EMSGSIZE;
2970}
2971
2972static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2973 struct netlink_callback *cb)
2974{
2975 mutex_lock(&__ip_vs_mutex);
2976 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2977 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
2978 ip_vs_master_mcast_ifn,
2979 ip_vs_master_syncid, cb) < 0)
2980 goto nla_put_failure;
2981
2982 cb->args[0] = 1;
2983 }
2984
2985 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
2986 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
2987 ip_vs_backup_mcast_ifn,
2988 ip_vs_backup_syncid, cb) < 0)
2989 goto nla_put_failure;
2990
2991 cb->args[1] = 1;
2992 }
2993
2994nla_put_failure:
2995 mutex_unlock(&__ip_vs_mutex);
2996
2997 return skb->len;
2998}
2999
3000static int ip_vs_genl_new_daemon(struct nlattr **attrs)
3001{
3002 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
3003 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
3004 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
3005 return -EINVAL;
3006
3007 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3008 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3009 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3010}
3011
3012static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3013{
3014 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3015 return -EINVAL;
3016
3017 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3018}
3019
3020static int ip_vs_genl_set_config(struct nlattr **attrs)
3021{
3022 struct ip_vs_timeout_user t;
3023
3024 __ip_vs_get_timeouts(&t);
3025
3026 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3027 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3028
3029 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3030 t.tcp_fin_timeout =
3031 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3032
3033 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3034 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3035
3036 return ip_vs_set_timeout(&t);
3037}
3038
3039static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3040{
3041 struct ip_vs_service *svc = NULL;
c860c6b1
JV
3042 struct ip_vs_service_user_kern usvc;
3043 struct ip_vs_dest_user_kern udest;
9a812198
JV
3044 int ret = 0, cmd;
3045 int need_full_svc = 0, need_full_dest = 0;
3046
3047 cmd = info->genlhdr->cmd;
3048
3049 mutex_lock(&__ip_vs_mutex);
3050
3051 if (cmd == IPVS_CMD_FLUSH) {
3052 ret = ip_vs_flush();
3053 goto out;
3054 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3055 ret = ip_vs_genl_set_config(info->attrs);
3056 goto out;
3057 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3058 cmd == IPVS_CMD_DEL_DAEMON) {
3059
3060 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3061
3062 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3063 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3064 info->attrs[IPVS_CMD_ATTR_DAEMON],
3065 ip_vs_daemon_policy)) {
3066 ret = -EINVAL;
3067 goto out;
3068 }
3069
3070 if (cmd == IPVS_CMD_NEW_DAEMON)
3071 ret = ip_vs_genl_new_daemon(daemon_attrs);
3072 else
3073 ret = ip_vs_genl_del_daemon(daemon_attrs);
3074 goto out;
3075 } else if (cmd == IPVS_CMD_ZERO &&
3076 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3077 ret = ip_vs_zero_all();
3078 goto out;
3079 }
3080
3081 /* All following commands require a service argument, so check if we
3082 * received a valid one. We need a full service specification when
3083 * adding / editing a service. Only identifying members otherwise. */
3084 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3085 need_full_svc = 1;
3086
3087 ret = ip_vs_genl_parse_service(&usvc,
3088 info->attrs[IPVS_CMD_ATTR_SERVICE],
3089 need_full_svc);
3090 if (ret)
3091 goto out;
3092
3093 /* Lookup the exact service by <protocol, addr, port> or fwmark */
3094 if (usvc.fwmark == 0)
b18610de
JV
3095 svc = __ip_vs_service_get(usvc.af, usvc.protocol,
3096 &usvc.addr, usvc.port);
9a812198 3097 else
b18610de 3098 svc = __ip_vs_svc_fwm_get(usvc.af, usvc.fwmark);
9a812198
JV
3099
3100 /* Unless we're adding a new service, the service must already exist */
3101 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3102 ret = -ESRCH;
3103 goto out;
3104 }
3105
3106 /* Destination commands require a valid destination argument. For
3107 * adding / editing a destination, we need a full destination
3108 * specification. */
3109 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3110 cmd == IPVS_CMD_DEL_DEST) {
3111 if (cmd != IPVS_CMD_DEL_DEST)
3112 need_full_dest = 1;
3113
3114 ret = ip_vs_genl_parse_dest(&udest,
3115 info->attrs[IPVS_CMD_ATTR_DEST],
3116 need_full_dest);
3117 if (ret)
3118 goto out;
3119 }
3120
3121 switch (cmd) {
3122 case IPVS_CMD_NEW_SERVICE:
3123 if (svc == NULL)
3124 ret = ip_vs_add_service(&usvc, &svc);
3125 else
3126 ret = -EEXIST;
3127 break;
3128 case IPVS_CMD_SET_SERVICE:
3129 ret = ip_vs_edit_service(svc, &usvc);
3130 break;
3131 case IPVS_CMD_DEL_SERVICE:
3132 ret = ip_vs_del_service(svc);
3133 break;
3134 case IPVS_CMD_NEW_DEST:
3135 ret = ip_vs_add_dest(svc, &udest);
3136 break;
3137 case IPVS_CMD_SET_DEST:
3138 ret = ip_vs_edit_dest(svc, &udest);
3139 break;
3140 case IPVS_CMD_DEL_DEST:
3141 ret = ip_vs_del_dest(svc, &udest);
3142 break;
3143 case IPVS_CMD_ZERO:
3144 ret = ip_vs_zero_service(svc);
3145 break;
3146 default:
3147 ret = -EINVAL;
3148 }
3149
3150out:
3151 if (svc)
3152 ip_vs_service_put(svc);
3153 mutex_unlock(&__ip_vs_mutex);
3154
3155 return ret;
3156}
3157
3158static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3159{
3160 struct sk_buff *msg;
3161 void *reply;
3162 int ret, cmd, reply_cmd;
3163
3164 cmd = info->genlhdr->cmd;
3165
3166 if (cmd == IPVS_CMD_GET_SERVICE)
3167 reply_cmd = IPVS_CMD_NEW_SERVICE;
3168 else if (cmd == IPVS_CMD_GET_INFO)
3169 reply_cmd = IPVS_CMD_SET_INFO;
3170 else if (cmd == IPVS_CMD_GET_CONFIG)
3171 reply_cmd = IPVS_CMD_SET_CONFIG;
3172 else {
3173 IP_VS_ERR("unknown Generic Netlink command\n");
3174 return -EINVAL;
3175 }
3176
3177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3178 if (!msg)
3179 return -ENOMEM;
3180
3181 mutex_lock(&__ip_vs_mutex);
3182
3183 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3184 if (reply == NULL)
3185 goto nla_put_failure;
3186
3187 switch (cmd) {
3188 case IPVS_CMD_GET_SERVICE:
3189 {
3190 struct ip_vs_service *svc;
3191
3192 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3193 if (IS_ERR(svc)) {
3194 ret = PTR_ERR(svc);
3195 goto out_err;
3196 } else if (svc) {
3197 ret = ip_vs_genl_fill_service(msg, svc);
3198 ip_vs_service_put(svc);
3199 if (ret)
3200 goto nla_put_failure;
3201 } else {
3202 ret = -ESRCH;
3203 goto out_err;
3204 }
3205
3206 break;
3207 }
3208
3209 case IPVS_CMD_GET_CONFIG:
3210 {
3211 struct ip_vs_timeout_user t;
3212
3213 __ip_vs_get_timeouts(&t);
3214#ifdef CONFIG_IP_VS_PROTO_TCP
3215 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3216 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3217 t.tcp_fin_timeout);
3218#endif
3219#ifdef CONFIG_IP_VS_PROTO_UDP
3220 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3221#endif
3222
3223 break;
3224 }
3225
3226 case IPVS_CMD_GET_INFO:
3227 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3228 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3229 IP_VS_CONN_TAB_SIZE);
3230 break;
3231 }
3232
3233 genlmsg_end(msg, reply);
3234 ret = genlmsg_unicast(msg, info->snd_pid);
3235 goto out;
3236
3237nla_put_failure:
3238 IP_VS_ERR("not enough space in Netlink message\n");
3239 ret = -EMSGSIZE;
3240
3241out_err:
3242 nlmsg_free(msg);
3243out:
3244 mutex_unlock(&__ip_vs_mutex);
3245
3246 return ret;
3247}
3248
3249
3250static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3251 {
3252 .cmd = IPVS_CMD_NEW_SERVICE,
3253 .flags = GENL_ADMIN_PERM,
3254 .policy = ip_vs_cmd_policy,
3255 .doit = ip_vs_genl_set_cmd,
3256 },
3257 {
3258 .cmd = IPVS_CMD_SET_SERVICE,
3259 .flags = GENL_ADMIN_PERM,
3260 .policy = ip_vs_cmd_policy,
3261 .doit = ip_vs_genl_set_cmd,
3262 },
3263 {
3264 .cmd = IPVS_CMD_DEL_SERVICE,
3265 .flags = GENL_ADMIN_PERM,
3266 .policy = ip_vs_cmd_policy,
3267 .doit = ip_vs_genl_set_cmd,
3268 },
3269 {
3270 .cmd = IPVS_CMD_GET_SERVICE,
3271 .flags = GENL_ADMIN_PERM,
3272 .doit = ip_vs_genl_get_cmd,
3273 .dumpit = ip_vs_genl_dump_services,
3274 .policy = ip_vs_cmd_policy,
3275 },
3276 {
3277 .cmd = IPVS_CMD_NEW_DEST,
3278 .flags = GENL_ADMIN_PERM,
3279 .policy = ip_vs_cmd_policy,
3280 .doit = ip_vs_genl_set_cmd,
3281 },
3282 {
3283 .cmd = IPVS_CMD_SET_DEST,
3284 .flags = GENL_ADMIN_PERM,
3285 .policy = ip_vs_cmd_policy,
3286 .doit = ip_vs_genl_set_cmd,
3287 },
3288 {
3289 .cmd = IPVS_CMD_DEL_DEST,
3290 .flags = GENL_ADMIN_PERM,
3291 .policy = ip_vs_cmd_policy,
3292 .doit = ip_vs_genl_set_cmd,
3293 },
3294 {
3295 .cmd = IPVS_CMD_GET_DEST,
3296 .flags = GENL_ADMIN_PERM,
3297 .policy = ip_vs_cmd_policy,
3298 .dumpit = ip_vs_genl_dump_dests,
3299 },
3300 {
3301 .cmd = IPVS_CMD_NEW_DAEMON,
3302 .flags = GENL_ADMIN_PERM,
3303 .policy = ip_vs_cmd_policy,
3304 .doit = ip_vs_genl_set_cmd,
3305 },
3306 {
3307 .cmd = IPVS_CMD_DEL_DAEMON,
3308 .flags = GENL_ADMIN_PERM,
3309 .policy = ip_vs_cmd_policy,
3310 .doit = ip_vs_genl_set_cmd,
3311 },
3312 {
3313 .cmd = IPVS_CMD_GET_DAEMON,
3314 .flags = GENL_ADMIN_PERM,
3315 .dumpit = ip_vs_genl_dump_daemons,
3316 },
3317 {
3318 .cmd = IPVS_CMD_SET_CONFIG,
3319 .flags = GENL_ADMIN_PERM,
3320 .policy = ip_vs_cmd_policy,
3321 .doit = ip_vs_genl_set_cmd,
3322 },
3323 {
3324 .cmd = IPVS_CMD_GET_CONFIG,
3325 .flags = GENL_ADMIN_PERM,
3326 .doit = ip_vs_genl_get_cmd,
3327 },
3328 {
3329 .cmd = IPVS_CMD_GET_INFO,
3330 .flags = GENL_ADMIN_PERM,
3331 .doit = ip_vs_genl_get_cmd,
3332 },
3333 {
3334 .cmd = IPVS_CMD_ZERO,
3335 .flags = GENL_ADMIN_PERM,
3336 .policy = ip_vs_cmd_policy,
3337 .doit = ip_vs_genl_set_cmd,
3338 },
3339 {
3340 .cmd = IPVS_CMD_FLUSH,
3341 .flags = GENL_ADMIN_PERM,
3342 .doit = ip_vs_genl_set_cmd,
3343 },
3344};
3345
3346static int __init ip_vs_genl_register(void)
3347{
3348 int ret, i;
3349
3350 ret = genl_register_family(&ip_vs_genl_family);
3351 if (ret)
3352 return ret;
3353
3354 for (i = 0; i < ARRAY_SIZE(ip_vs_genl_ops); i++) {
3355 ret = genl_register_ops(&ip_vs_genl_family, &ip_vs_genl_ops[i]);
3356 if (ret)
3357 goto err_out;
3358 }
3359 return 0;
3360
3361err_out:
3362 genl_unregister_family(&ip_vs_genl_family);
3363 return ret;
3364}
3365
3366static void ip_vs_genl_unregister(void)
3367{
3368 genl_unregister_family(&ip_vs_genl_family);
3369}
3370
3371/* End of Generic Netlink interface definitions */
3372
1da177e4 3373
048cf48b 3374int __init ip_vs_control_init(void)
1da177e4
LT
3375{
3376 int ret;
3377 int idx;
3378
3379 EnterFunction(2);
3380
3381 ret = nf_register_sockopt(&ip_vs_sockopts);
3382 if (ret) {
3383 IP_VS_ERR("cannot register sockopt.\n");
3384 return ret;
3385 }
3386
9a812198
JV
3387 ret = ip_vs_genl_register();
3388 if (ret) {
3389 IP_VS_ERR("cannot register Generic Netlink interface.\n");
3390 nf_unregister_sockopt(&ip_vs_sockopts);
3391 return ret;
3392 }
3393
457c4cbc
EB
3394 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3395 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
1da177e4 3396
90754f8e 3397 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
1da177e4
LT
3398
3399 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3400 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3401 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3402 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3403 }
3404 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3405 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3406 }
3407
1da177e4
LT
3408 ip_vs_new_estimator(&ip_vs_stats);
3409
3410 /* Hook the defense timer */
3411 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3412
3413 LeaveFunction(2);
3414 return 0;
3415}
3416
3417
3418void ip_vs_control_cleanup(void)
3419{
3420 EnterFunction(2);
3421 ip_vs_trash_cleanup();
3422 cancel_rearming_delayed_work(&defense_work);
28e53bdd 3423 cancel_work_sync(&defense_work.work);
1da177e4
LT
3424 ip_vs_kill_estimator(&ip_vs_stats);
3425 unregister_sysctl_table(sysctl_header);
457c4cbc
EB
3426 proc_net_remove(&init_net, "ip_vs_stats");
3427 proc_net_remove(&init_net, "ip_vs");
9a812198 3428 ip_vs_genl_unregister();
1da177e4
LT
3429 nf_unregister_sockopt(&ip_vs_sockopts);
3430 LeaveFunction(2);
3431}