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