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