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