blob: 5f20caf47a1d6cca18d2892e7e48618040c6abac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070021#define KMSG_COMPONENT "IPVS"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/netfilter.h>
37#include <linux/netfilter_ipv4.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020040#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/ip.h>
Vince Busam09571c72008-09-02 15:55:52 +020042#ifdef CONFIG_IP_VS_IPV6
43#include <net/ipv6.h>
44#include <net/ip6_route.h>
45#endif
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/sock.h>
Julius Volz9a812192008-08-14 14:08:44 +020048#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/uaccess.h>
51
52#include <net/ip_vs.h>
53
54/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080055static DEFINE_MUTEX(__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 */
Simon Horman4f728162010-08-26 02:54:30 +000064static DEFINE_SPINLOCK(ip_vs_securetcp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
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;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +020091#ifdef CONFIG_IP_VS_NFCT
92int sysctl_ip_vs_conntrack;
93#endif
Julian Anastasov8a803042010-09-21 17:38:57 +020094int sysctl_ip_vs_snat_reroute = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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
Vince Busam09571c72008-09-02 15:55:52 +0200106#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700127 * update_defense_level is called from keventd and from sysctl,
128 * so it needs to protect itself from softirqs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
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
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700148 local_bh_disable();
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* 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 */
Simon Horman4f728162010-08-26 02:54:30 +0000211 spin_lock(&ip_vs_securetcp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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);
Simon Horman4f728162010-08-26 02:54:30 +0000245 spin_unlock(&ip_vs_securetcp_lock);
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700246
247 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250
251/*
252 * Timer for checking the defense
253 */
254#define DEFENSE_TIMER_PERIOD 1*HZ
David Howellsc4028952006-11-22 14:57:56 +0000255static void defense_work_handler(struct work_struct *work);
256static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Howellsc4028952006-11-22 14:57:56 +0000258static void defense_work_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
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
Julius Volzb18610d2008-09-02 15:55:37 +0200317ip_vs_svc_hashkey(int af, unsigned proto, const union nf_inet_addr *addr,
318 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 register unsigned porth = ntohs(port);
Julius Volzb18610d2008-09-02 15:55:37 +0200321 __be32 addr_fold = addr->ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Julius Volzb18610d2008-09-02 15:55:37 +0200323#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 & 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) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000351 pr_err("%s(): request for already hashed, called from %pF\n",
352 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354 }
355
356 if (svc->fwmark == 0) {
357 /*
358 * Hash it by <protocol,addr,port> in ip_vs_svc_table
359 */
Julius Volzb18610d2008-09-02 15:55:37 +0200360 hash = ip_vs_svc_hashkey(svc->af, svc->protocol, &svc->addr,
Julius Volze7ade462008-09-02 15:55:33 +0200361 svc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 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)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000385 pr_err("%s(): request for unhash flagged, called from %pF\n",
386 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 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 */
Julius Volzb18610d2008-09-02 15:55:37 +0200407static inline struct ip_vs_service *
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200408__ip_vs_service_find(int af, __u16 protocol, const union nf_inet_addr *vaddr,
Julius Volzb18610d2008-09-02 15:55:37 +0200409 __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 unsigned hash;
412 struct ip_vs_service *svc;
413
414 /* Check for "full" addressed entries */
Julius Volzb18610d2008-09-02 15:55:37 +0200415 hash = ip_vs_svc_hashkey(af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
Julius Volzb18610d2008-09-02 15:55:37 +0200418 if ((svc->af == af)
419 && ip_vs_addr_equal(af, &svc->addr, vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 && (svc->port == vport)
421 && (svc->protocol == protocol)) {
422 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return svc;
424 }
425 }
426
427 return NULL;
428}
429
430
431/*
432 * Get service by {fwmark} in the service table.
433 */
Julius Volzb18610d2008-09-02 15:55:37 +0200434static inline struct ip_vs_service *
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200435__ip_vs_svc_fwm_find(int af, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
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) {
Julius Volzb18610d2008-09-02 15:55:37 +0200444 if (svc->fwmark == fwmark && svc->af == af) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return svc;
447 }
448 }
449
450 return NULL;
451}
452
453struct ip_vs_service *
Julius Volz3c2e0502008-09-02 15:55:38 +0200454ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
455 const union nf_inet_addr *vaddr, __be16 vport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct ip_vs_service *svc;
Julius Volz3c2e0502008-09-02 15:55:38 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 read_lock(&__ip_vs_svc_lock);
460
461 /*
462 * Check the table hashed by fwmark first
463 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200464 if (fwmark && (svc = __ip_vs_svc_fwm_find(af, fwmark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
466
467 /*
468 * Check the table hashed by <protocol,addr,port>
469 * for "full" addressed entries
470 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200471 svc = __ip_vs_service_find(af, protocol, vaddr, vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
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 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200481 svc = __ip_vs_service_find(af, protocol, vaddr, FTPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 if (svc == NULL
485 && atomic_read(&ip_vs_nullsvc_counter)) {
486 /*
487 * Check if the catch-all port (port zero) exists
488 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200489 svc = __ip_vs_service_find(af, protocol, vaddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 out:
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200493 if (svc)
494 atomic_inc(&svc->usecnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 read_unlock(&__ip_vs_svc_lock);
496
Julius Volz3c2e0502008-09-02 15:55:38 +0200497 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
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
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200513static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514__ip_vs_unbind_svc(struct ip_vs_dest *dest)
515{
516 struct ip_vs_service *svc = dest->svc;
517
518 dest->svc = NULL;
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200519 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 kfree(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528
529/*
530 * Returns hash value for real service
531 */
Julius Volz7937df12008-09-02 15:55:48 +0200532static inline unsigned ip_vs_rs_hashkey(int af,
533 const union nf_inet_addr *addr,
534 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 register unsigned porth = ntohs(port);
Julius Volz7937df12008-09-02 15:55:48 +0200537 __be32 addr_fold = addr->ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Julius Volz7937df12008-09-02 15:55:48 +0200539#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
544
545 return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 & 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 */
Julius Volz7937df12008-09-02 15:55:48 +0200565 hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 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 *
Julius Volz7937df12008-09-02 15:55:48 +0200593ip_vs_lookup_real_service(int af, __u16 protocol,
594 const union nf_inet_addr *daddr,
595 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 unsigned hash;
598 struct ip_vs_dest *dest;
599
600 /*
601 * Check for "full" addressed entries
602 * Return the first found entry
603 */
Julius Volz7937df12008-09-02 15:55:48 +0200604 hash = ip_vs_rs_hashkey(af, daddr, dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 read_lock(&__ip_vs_rs_lock);
607 list_for_each_entry(dest, &ip_vs_rtable[hash], d_list) {
Julius Volz7937df12008-09-02 15:55:48 +0200608 if ((dest->af == af)
609 && ip_vs_addr_equal(af, &dest->addr, daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 && (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 *
Julius Volz7937df12008-09-02 15:55:48 +0200627ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
628 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
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) {
Julius Volz7937df12008-09-02 15:55:48 +0200636 if ((dest->af == svc->af)
637 && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
638 && (dest->port == dport)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* HIT */
640 return dest;
641 }
642 }
643
644 return NULL;
645}
646
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800647/*
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 */
Julius Volz7937df12008-09-02 15:55:48 +0200657struct 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)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800661{
662 struct ip_vs_dest *dest;
663 struct ip_vs_service *svc;
664
Julius Volz7937df12008-09-02 15:55:48 +0200665 svc = ip_vs_service_get(af, 0, protocol, vaddr, vport);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800666 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
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 *
Julius Volz7937df12008-09-02 15:55:48 +0200686ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
687 __be16 dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
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) {
Julius Volz7937df12008-09-02 15:55:48 +0200695 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) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dest->port == dport &&
704 dest->vfwmark == svc->fwmark &&
705 dest->protocol == svc->protocol &&
706 (svc->fwmark ||
Julius Volz7937df12008-09-02 15:55:48 +0200707 (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 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) {
Julius Volz7937df12008-09-02 15:55:48 +0200717 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 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);
Simon Hormane93615d2008-08-11 17:19:14 +1000759
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200760 memset(&stats->ustats, 0, sizeof(stats->ustats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ip_vs_zero_estimator(stats);
Simon Hormane93615d2008-08-11 17:19:14 +1000762
Sven Wegener3a14a3132008-08-10 18:24:41 +0000763 spin_unlock_bh(&stats->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Update a destination in the given service
768 */
769static void
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200770__ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
771 struct ip_vs_dest_user_kern *udest, int add)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int conn_flags;
774
775 /* set the weight and the flags */
776 atomic_set(&dest->weight, udest->weight);
Julian Anastasov35757922010-09-17 14:18:16 +0200777 conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
778 conn_flags |= IP_VS_CONN_F_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* check if local node and update the flags */
Vince Busam09571c72008-09-02 15:55:52 +0200781#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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
Julian Anastasov35757922010-09-17 14:18:16 +0200795 if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 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;
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200826
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847
848/*
849 * Create a destination for the given service
850 */
851static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200852ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct ip_vs_dest **dest_p)
854{
855 struct ip_vs_dest *dest;
856 unsigned atype;
857
858 EnterFunction(2);
859
Vince Busam09571c72008-09-02 15:55:52 +0200860#ifdef CONFIG_IP_VS_IPV6
861 if (svc->af == AF_INET6) {
862 atype = ipv6_addr_type(&udest->addr.in6);
Sven Wegener3bfb92f2008-09-05 16:53:49 +0200863 if ((!(atype & IPV6_ADDR_UNICAST) ||
864 atype & IPV6_ADDR_LINKLOCAL) &&
Vince Busam09571c72008-09-02 15:55:52 +0200865 !__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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Simon Hormandee06e42010-08-26 02:54:31 +0000875 dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000877 pr_err("%s(): no memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -ENOMEM;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Julius Volzc860c6b2008-09-02 15:55:36 +0200881 dest->af = svc->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 dest->protocol = svc->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +0200883 dest->vaddr = svc->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dest->vport = svc->port;
885 dest->vfwmark = svc->fwmark;
Julius Volzc860c6b2008-09-02 15:55:36 +0200886 ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dest->port = udest->port;
888
889 atomic_set(&dest->activeconns, 0);
890 atomic_set(&dest->inactconns, 0);
891 atomic_set(&dest->persistconns, 0);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200892 atomic_set(&dest->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 INIT_LIST_HEAD(&dest->d_list);
895 spin_lock_init(&dest->dst_lock);
896 spin_lock_init(&dest->stats.lock);
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200897 __ip_vs_update_dest(svc, dest, udest, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
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
Julius Volzc860c6b2008-09-02 15:55:36 +0200910ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +0200913 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -0700914 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int ret;
916
917 EnterFunction(2);
918
919 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000920 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -ERANGE;
922 }
923
924 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000925 pr_err("%s(): lower threshold is higher than upper threshold\n",
926 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -ERANGE;
928 }
929
Julius Volzc860c6b2008-09-02 15:55:36 +0200930 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Check if the dest already exists in the list
934 */
Julius Volz7937df12008-09-02 15:55:48 +0200935 dest = ip_vs_lookup_dest(svc, &daddr, dport);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (dest != NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000938 IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return -EEXIST;
940 }
941
942 /*
943 * Check if the dest already exists in the trash and
944 * is from the same service
945 */
Julius Volz7937df12008-09-02 15:55:48 +0200946 dest = ip_vs_trash_get_dest(svc, &daddr, dport);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (dest != NULL) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200949 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * Get the destination from the trash
959 */
960 list_del(&dest->n_list);
961
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200962 __ip_vs_update_dest(svc, dest, udest, 1);
963 ret = 0;
964 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200966 * Allocate and initialize the dest structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200968 ret = ip_vs_new_dest(svc, udest, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 LeaveFunction(2);
971
Julian Anastasov26c15cf2010-09-21 18:12:30 +0200972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975
976/*
977 * Edit a destination in the given service
978 */
979static int
Julius Volzc860c6b2008-09-02 15:55:36 +0200980ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 struct ip_vs_dest *dest;
Julius Volzc860c6b2008-09-02 15:55:36 +0200983 union nf_inet_addr daddr;
Al Viro014d7302006-09-28 14:29:52 -0700984 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 EnterFunction(2);
987
988 if (udest->weight < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000989 pr_err("%s(): server weight less than zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -ERANGE;
991 }
992
993 if (udest->l_threshold > udest->u_threshold) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000994 pr_err("%s(): lower threshold is higher than upper threshold\n",
995 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -ERANGE;
997 }
998
Julius Volzc860c6b2008-09-02 15:55:36 +0200999 ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * Lookup the destination list
1003 */
Julius Volz7937df12008-09-02 15:55:48 +02001004 dest = ip_vs_lookup_dest(svc, &daddr, dport);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001007 IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -ENOENT;
1009 }
1010
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001011 __ip_vs_update_dest(svc, dest, udest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 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)) {
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001038 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 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 {
Julius Volzcfc78c52008-09-02 15:55:53 +02001051 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 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--;
Sven Wegener82dfb6f2008-08-11 19:36:06 +00001076
1077 /*
1078 * Call the update_service function of its scheduler
1079 */
1080 if (svcupd && svc->scheduler->update_service)
1081 svc->scheduler->update_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084
1085/*
1086 * Delete a destination server in the given service
1087 */
1088static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001089ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct ip_vs_dest *dest;
Al Viro014d7302006-09-28 14:29:52 -07001092 __be16 dport = udest->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 EnterFunction(2);
1095
Julius Volz7937df12008-09-02 15:55:48 +02001096 dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
Julius Volzc860c6b2008-09-02 15:55:36 +02001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (dest == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001099 IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOENT;
1101 }
1102
1103 write_lock_bh(&__ip_vs_svc_lock);
1104
1105 /*
1106 * Wait until all other svc users go away.
1107 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001108 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
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
Julius Volzc860c6b2008-09-02 15:55:36 +02001132ip_vs_add_service(struct ip_vs_service_user_kern *u,
1133 struct ip_vs_service **svc_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
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) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001145 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ret = -ENOENT;
Simon Horman6e08bfb2010-08-22 21:37:52 +09001147 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Julius Volzf94fd042008-09-02 15:55:55 +02001150#ifdef CONFIG_IP_VS_IPV6
Julius Volz48148932008-11-03 17:08:56 -08001151 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1152 ret = -EINVAL;
1153 goto out_err;
Julius Volzf94fd042008-09-02 15:55:55 +02001154 }
1155#endif
1156
Simon Hormandee06e42010-08-26 02:54:31 +00001157 svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (svc == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001159 IP_VS_DBG(1, "%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ret = -ENOMEM;
1161 goto out_err;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* I'm the first user of the service */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001165 atomic_set(&svc->usecnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 atomic_set(&svc->refcnt, 0);
1167
Julius Volzc860c6b2008-09-02 15:55:36 +02001168 svc->af = u->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 svc->protocol = u->protocol;
Julius Volzc860c6b2008-09-02 15:55:36 +02001170 ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 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);
Julius Volzf94fd042008-09-02 15:55:55 +02001194
1195 /* Count only IPv4 services for old get/setsockopt interface */
1196 if (svc->af == AF_INET)
1197 ip_vs_num_services++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
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
Simon Horman6e08bfb2010-08-22 21:37:52 +09001207 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (svc != NULL) {
Simon Horman2fabf352010-08-22 21:37:52 +09001209 ip_vs_unbind_scheduler(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (svc->inc) {
1211 local_bh_disable();
1212 ip_vs_app_inc_put(svc->inc);
1213 local_bh_enable();
1214 }
1215 kfree(svc);
1216 }
1217 ip_vs_scheduler_put(sched);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* decrease the module use count */
1220 ip_vs_use_count_dec();
1221
1222 return ret;
1223}
1224
1225
1226/*
1227 * Edit a service and bind it with a new scheduler
1228 */
1229static int
Julius Volzc860c6b2008-09-02 15:55:36 +02001230ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct ip_vs_scheduler *sched, *old_sched;
1233 int ret = 0;
1234
1235 /*
1236 * Lookup the scheduler, by 'u->sched_name'
1237 */
1238 sched = ip_vs_scheduler_get(u->sched_name);
1239 if (sched == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001240 pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -ENOENT;
1242 }
1243 old_sched = sched;
1244
Julius Volzf94fd042008-09-02 15:55:55 +02001245#ifdef CONFIG_IP_VS_IPV6
Julius Volz48148932008-11-03 17:08:56 -08001246 if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
1247 ret = -EINVAL;
1248 goto out;
Julius Volzf94fd042008-09-02 15:55:55 +02001249 }
1250#endif
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 write_lock_bh(&__ip_vs_svc_lock);
1253
1254 /*
1255 * Wait until all other svc users go away.
1256 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001257 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * Set the flags and timeout value
1261 */
1262 svc->flags = u->flags | IP_VS_SVC_F_HASHED;
1263 svc->timeout = u->timeout * HZ;
1264 svc->netmask = u->netmask;
1265
1266 old_sched = svc->scheduler;
1267 if (sched != old_sched) {
1268 /*
1269 * Unbind the old scheduler
1270 */
1271 if ((ret = ip_vs_unbind_scheduler(svc))) {
1272 old_sched = sched;
Simon Horman9e691ed2008-09-17 10:10:41 +10001273 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276 /*
1277 * Bind the new scheduler
1278 */
1279 if ((ret = ip_vs_bind_scheduler(svc, sched))) {
1280 /*
1281 * If ip_vs_bind_scheduler fails, restore the old
1282 * scheduler.
1283 * The main reason of failure is out of memory.
1284 *
1285 * The question is if the old scheduler can be
1286 * restored all the time. TODO: if it cannot be
1287 * restored some time, we must delete the service,
1288 * otherwise the system may crash.
1289 */
1290 ip_vs_bind_scheduler(svc, old_sched);
1291 old_sched = sched;
Simon Horman9e691ed2008-09-17 10:10:41 +10001292 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
1295
Simon Horman9e691ed2008-09-17 10:10:41 +10001296 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 write_unlock_bh(&__ip_vs_svc_lock);
Sven Wegener8d5803b2008-09-20 11:48:33 +02001298#ifdef CONFIG_IP_VS_IPV6
Simon Horman9e691ed2008-09-17 10:10:41 +10001299 out:
Sven Wegener8d5803b2008-09-20 11:48:33 +02001300#endif
Simon Horman6e08bfb2010-08-22 21:37:52 +09001301 ip_vs_scheduler_put(old_sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return ret;
1303}
1304
1305
1306/*
1307 * Delete a service from the service list
1308 * - The service must be unlinked, unlocked and not referenced!
1309 * - We are called under _bh lock
1310 */
1311static void __ip_vs_del_service(struct ip_vs_service *svc)
1312{
1313 struct ip_vs_dest *dest, *nxt;
1314 struct ip_vs_scheduler *old_sched;
1315
Julius Volzf94fd042008-09-02 15:55:55 +02001316 /* Count only IPv4 services for old get/setsockopt interface */
1317 if (svc->af == AF_INET)
1318 ip_vs_num_services--;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ip_vs_kill_estimator(&svc->stats);
1321
1322 /* Unbind scheduler */
1323 old_sched = svc->scheduler;
1324 ip_vs_unbind_scheduler(svc);
Simon Horman6e08bfb2010-08-22 21:37:52 +09001325 ip_vs_scheduler_put(old_sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Unbind app inc */
1328 if (svc->inc) {
1329 ip_vs_app_inc_put(svc->inc);
1330 svc->inc = NULL;
1331 }
1332
1333 /*
1334 * Unlink the whole destination list
1335 */
1336 list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
1337 __ip_vs_unlink_dest(svc, dest, 0);
1338 __ip_vs_del_dest(dest);
1339 }
1340
1341 /*
1342 * Update the virtual service counters
1343 */
1344 if (svc->port == FTPPORT)
1345 atomic_dec(&ip_vs_ftpsvc_counter);
1346 else if (svc->port == 0)
1347 atomic_dec(&ip_vs_nullsvc_counter);
1348
1349 /*
1350 * Free the service if nobody refers to it
1351 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001352 if (atomic_read(&svc->refcnt) == 0) {
1353 IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n",
1354 svc->fwmark,
1355 IP_VS_DBG_ADDR(svc->af, &svc->addr),
1356 ntohs(svc->port), atomic_read(&svc->usecnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 kfree(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /* decrease the module use count */
1361 ip_vs_use_count_dec();
1362}
1363
1364/*
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001365 * Unlink a service from list and try to delete it if its refcnt reached 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001367static void ip_vs_unlink_service(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
1370 * Unhash it from the service table
1371 */
1372 write_lock_bh(&__ip_vs_svc_lock);
1373
1374 ip_vs_svc_unhash(svc);
1375
1376 /*
1377 * Wait until all the svc users go away.
1378 */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001379 IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 __ip_vs_del_service(svc);
1382
1383 write_unlock_bh(&__ip_vs_svc_lock);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001384}
1385
1386/*
1387 * Delete a service from the service list
1388 */
1389static int ip_vs_del_service(struct ip_vs_service *svc)
1390{
1391 if (svc == NULL)
1392 return -EEXIST;
1393 ip_vs_unlink_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 return 0;
1396}
1397
1398
1399/*
1400 * Flush all the virtual services
1401 */
1402static int ip_vs_flush(void)
1403{
1404 int idx;
1405 struct ip_vs_service *svc, *nxt;
1406
1407 /*
1408 * Flush the service table hashed by <protocol,addr,port>
1409 */
1410 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1411 list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx], s_list) {
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001412 ip_vs_unlink_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 }
1415
1416 /*
1417 * Flush the service table hashed by fwmark
1418 */
1419 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1420 list_for_each_entry_safe(svc, nxt,
1421 &ip_vs_svc_fwm_table[idx], f_list) {
Julian Anastasov26c15cf2010-09-21 18:12:30 +02001422 ip_vs_unlink_service(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 }
1425
1426 return 0;
1427}
1428
1429
1430/*
1431 * Zero counters in a service or all services
1432 */
1433static int ip_vs_zero_service(struct ip_vs_service *svc)
1434{
1435 struct ip_vs_dest *dest;
1436
1437 write_lock_bh(&__ip_vs_svc_lock);
1438 list_for_each_entry(dest, &svc->destinations, n_list) {
1439 ip_vs_zero_stats(&dest->stats);
1440 }
1441 ip_vs_zero_stats(&svc->stats);
1442 write_unlock_bh(&__ip_vs_svc_lock);
1443 return 0;
1444}
1445
1446static int ip_vs_zero_all(void)
1447{
1448 int idx;
1449 struct ip_vs_service *svc;
1450
1451 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1452 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1453 ip_vs_zero_service(svc);
1454 }
1455 }
1456
1457 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1458 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1459 ip_vs_zero_service(svc);
1460 }
1461 }
1462
1463 ip_vs_zero_stats(&ip_vs_stats);
1464 return 0;
1465}
1466
1467
1468static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001469proc_do_defense_mode(ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 void __user *buffer, size_t *lenp, loff_t *ppos)
1471{
1472 int *valp = table->data;
1473 int val = *valp;
1474 int rc;
1475
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001476 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (write && (*valp != val)) {
1478 if ((*valp < 0) || (*valp > 3)) {
1479 /* Restore the correct value */
1480 *valp = val;
1481 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 update_defense_level();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 }
1485 return rc;
1486}
1487
1488
1489static int
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001490proc_do_sync_threshold(ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 void __user *buffer, size_t *lenp, loff_t *ppos)
1492{
1493 int *valp = table->data;
1494 int val[2];
1495 int rc;
1496
1497 /* backup the value first */
1498 memcpy(val, valp, sizeof(val));
1499
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001500 rc = proc_dointvec(table, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
1502 /* Restore the correct value */
1503 memcpy(valp, val, sizeof(val));
1504 }
1505 return rc;
1506}
1507
1508
1509/*
1510 * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1511 */
1512
1513static struct ctl_table vs_vars[] = {
1514 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 .procname = "amemthresh",
1516 .data = &sysctl_ip_vs_amemthresh,
1517 .maxlen = sizeof(int),
1518 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001519 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 },
1521#ifdef CONFIG_IP_VS_DEBUG
1522 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .procname = "debug_level",
1524 .data = &sysctl_ip_vs_debug_level,
1525 .maxlen = sizeof(int),
1526 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001527 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 },
1529#endif
1530 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .procname = "am_droprate",
1532 .data = &sysctl_ip_vs_am_droprate,
1533 .maxlen = sizeof(int),
1534 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001535 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 },
1537 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .procname = "drop_entry",
1539 .data = &sysctl_ip_vs_drop_entry,
1540 .maxlen = sizeof(int),
1541 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001542 .proc_handler = proc_do_defense_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 },
1544 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .procname = "drop_packet",
1546 .data = &sysctl_ip_vs_drop_packet,
1547 .maxlen = sizeof(int),
1548 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001549 .proc_handler = proc_do_defense_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 },
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001551#ifdef CONFIG_IP_VS_NFCT
1552 {
1553 .procname = "conntrack",
1554 .data = &sysctl_ip_vs_conntrack,
1555 .maxlen = sizeof(int),
1556 .mode = 0644,
1557 .proc_handler = &proc_dointvec,
1558 },
1559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .procname = "secure_tcp",
1562 .data = &sysctl_ip_vs_secure_tcp,
1563 .maxlen = sizeof(int),
1564 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001565 .proc_handler = proc_do_defense_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 },
Julian Anastasov8a803042010-09-21 17:38:57 +02001567 {
1568 .procname = "snat_reroute",
1569 .data = &sysctl_ip_vs_snat_reroute,
1570 .maxlen = sizeof(int),
1571 .mode = 0644,
1572 .proc_handler = &proc_dointvec,
1573 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574#if 0
1575 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 .procname = "timeout_established",
1577 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
1578 .maxlen = sizeof(int),
1579 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001580 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 },
1582 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 .procname = "timeout_synsent",
1584 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
1585 .maxlen = sizeof(int),
1586 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001587 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 },
1589 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 .procname = "timeout_synrecv",
1591 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
1592 .maxlen = sizeof(int),
1593 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001594 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 },
1596 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 .procname = "timeout_finwait",
1598 .data = &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
1599 .maxlen = sizeof(int),
1600 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001601 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 },
1603 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 .procname = "timeout_timewait",
1605 .data = &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
1606 .maxlen = sizeof(int),
1607 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001608 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 },
1610 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 .procname = "timeout_close",
1612 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
1613 .maxlen = sizeof(int),
1614 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001615 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 },
1617 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .procname = "timeout_closewait",
1619 .data = &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
1620 .maxlen = sizeof(int),
1621 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001622 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 },
1624 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .procname = "timeout_lastack",
1626 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
1627 .maxlen = sizeof(int),
1628 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001629 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 },
1631 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 .procname = "timeout_listen",
1633 .data = &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
1634 .maxlen = sizeof(int),
1635 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001636 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 },
1638 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 .procname = "timeout_synack",
1640 .data = &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
1641 .maxlen = sizeof(int),
1642 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001643 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 },
1645 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 .procname = "timeout_udp",
1647 .data = &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
1648 .maxlen = sizeof(int),
1649 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001650 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 },
1652 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 .procname = "timeout_icmp",
1654 .data = &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
1655 .maxlen = sizeof(int),
1656 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001657 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 },
1659#endif
1660 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .procname = "cache_bypass",
1662 .data = &sysctl_ip_vs_cache_bypass,
1663 .maxlen = sizeof(int),
1664 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001665 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 },
1667 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 .procname = "expire_nodest_conn",
1669 .data = &sysctl_ip_vs_expire_nodest_conn,
1670 .maxlen = sizeof(int),
1671 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001672 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 },
1674 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .procname = "expire_quiescent_template",
1676 .data = &sysctl_ip_vs_expire_quiescent_template,
1677 .maxlen = sizeof(int),
1678 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001679 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 },
1681 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .procname = "sync_threshold",
1683 .data = &sysctl_ip_vs_sync_threshold,
1684 .maxlen = sizeof(sysctl_ip_vs_sync_threshold),
1685 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001686 .proc_handler = proc_do_sync_threshold,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 },
1688 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 .procname = "nat_icmp_send",
1690 .data = &sysctl_ip_vs_nat_icmp_send,
1691 .maxlen = sizeof(int),
1692 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08001693 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001695 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696};
1697
Sven Wegener5587da52008-08-10 18:24:40 +00001698const struct ctl_path net_vs_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08001699 { .procname = "net", },
1700 { .procname = "ipv4", },
Pavel Emelyanov90754f82008-01-12 02:33:50 -08001701 { .procname = "vs", },
1702 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703};
Pavel Emelyanov90754f82008-01-12 02:33:50 -08001704EXPORT_SYMBOL_GPL(net_vs_ctl_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706static struct ctl_table_header * sysctl_header;
1707
1708#ifdef CONFIG_PROC_FS
1709
1710struct ip_vs_iter {
1711 struct list_head *table;
1712 int bucket;
1713};
1714
1715/*
1716 * Write the contents of the VS rule table to a PROCfs file.
1717 * (It is kept just for backward compatibility)
1718 */
1719static inline const char *ip_vs_fwd_name(unsigned flags)
1720{
1721 switch (flags & IP_VS_CONN_F_FWD_MASK) {
1722 case IP_VS_CONN_F_LOCALNODE:
1723 return "Local";
1724 case IP_VS_CONN_F_TUNNEL:
1725 return "Tunnel";
1726 case IP_VS_CONN_F_DROUTE:
1727 return "Route";
1728 default:
1729 return "Masq";
1730 }
1731}
1732
1733
1734/* Get the Nth entry in the two lists */
1735static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
1736{
1737 struct ip_vs_iter *iter = seq->private;
1738 int idx;
1739 struct ip_vs_service *svc;
1740
1741 /* look in hash by protocol */
1742 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1743 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1744 if (pos-- == 0){
1745 iter->table = ip_vs_svc_table;
1746 iter->bucket = idx;
1747 return svc;
1748 }
1749 }
1750 }
1751
1752 /* keep looking in fwmark */
1753 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1754 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1755 if (pos-- == 0) {
1756 iter->table = ip_vs_svc_fwm_table;
1757 iter->bucket = idx;
1758 return svc;
1759 }
1760 }
1761 }
1762
1763 return NULL;
1764}
1765
1766static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
Simon Horman563e94f2008-09-17 10:10:42 +10001767__acquires(__ip_vs_svc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769
1770 read_lock_bh(&__ip_vs_svc_lock);
1771 return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
1772}
1773
1774
1775static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1776{
1777 struct list_head *e;
1778 struct ip_vs_iter *iter;
1779 struct ip_vs_service *svc;
1780
1781 ++*pos;
1782 if (v == SEQ_START_TOKEN)
1783 return ip_vs_info_array(seq,0);
1784
1785 svc = v;
1786 iter = seq->private;
1787
1788 if (iter->table == ip_vs_svc_table) {
1789 /* next service in table hashed by protocol */
1790 if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
1791 return list_entry(e, struct ip_vs_service, s_list);
1792
1793
1794 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1795 list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
1796 s_list) {
1797 return svc;
1798 }
1799 }
1800
1801 iter->table = ip_vs_svc_fwm_table;
1802 iter->bucket = -1;
1803 goto scan_fwmark;
1804 }
1805
1806 /* next service in hashed by fwmark */
1807 if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
1808 return list_entry(e, struct ip_vs_service, f_list);
1809
1810 scan_fwmark:
1811 while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
1812 list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
1813 f_list)
1814 return svc;
1815 }
1816
1817 return NULL;
1818}
1819
1820static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
Simon Horman563e94f2008-09-17 10:10:42 +10001821__releases(__ip_vs_svc_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 read_unlock_bh(&__ip_vs_svc_lock);
1824}
1825
1826
1827static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
1828{
1829 if (v == SEQ_START_TOKEN) {
1830 seq_printf(seq,
1831 "IP Virtual Server version %d.%d.%d (size=%d)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001832 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 seq_puts(seq,
1834 "Prot LocalAddress:Port Scheduler Flags\n");
1835 seq_puts(seq,
1836 " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
1837 } else {
1838 const struct ip_vs_service *svc = v;
1839 const struct ip_vs_iter *iter = seq->private;
1840 const struct ip_vs_dest *dest;
1841
Vince Busam667a5f12008-09-02 15:55:49 +02001842 if (iter->table == ip_vs_svc_table) {
1843#ifdef CONFIG_IP_VS_IPV6
1844 if (svc->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001845 seq_printf(seq, "%s [%pI6]:%04X %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02001846 ip_vs_proto_name(svc->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001847 &svc->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02001848 ntohs(svc->port),
1849 svc->scheduler->name);
1850 else
1851#endif
Nick Chalk26ec0372010-06-22 08:07:01 +02001852 seq_printf(seq, "%s %08X:%04X %s %s ",
Vince Busam667a5f12008-09-02 15:55:49 +02001853 ip_vs_proto_name(svc->protocol),
1854 ntohl(svc->addr.ip),
1855 ntohs(svc->port),
Nick Chalk26ec0372010-06-22 08:07:01 +02001856 svc->scheduler->name,
1857 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02001858 } else {
Nick Chalk26ec0372010-06-22 08:07:01 +02001859 seq_printf(seq, "FWM %08X %s %s",
1860 svc->fwmark, svc->scheduler->name,
1861 (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
Vince Busam667a5f12008-09-02 15:55:49 +02001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
1865 seq_printf(seq, "persistent %d %08X\n",
1866 svc->timeout,
1867 ntohl(svc->netmask));
1868 else
1869 seq_putc(seq, '\n');
1870
1871 list_for_each_entry(dest, &svc->destinations, n_list) {
Vince Busam667a5f12008-09-02 15:55:49 +02001872#ifdef CONFIG_IP_VS_IPV6
1873 if (dest->af == AF_INET6)
1874 seq_printf(seq,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001875 " -> [%pI6]:%04X"
Vince Busam667a5f12008-09-02 15:55:49 +02001876 " %-7s %-6d %-10d %-10d\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001877 &dest->addr.in6,
Vince Busam667a5f12008-09-02 15:55:49 +02001878 ntohs(dest->port),
1879 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1880 atomic_read(&dest->weight),
1881 atomic_read(&dest->activeconns),
1882 atomic_read(&dest->inactconns));
1883 else
1884#endif
1885 seq_printf(seq,
1886 " -> %08X:%04X "
1887 "%-7s %-6d %-10d %-10d\n",
1888 ntohl(dest->addr.ip),
1889 ntohs(dest->port),
1890 ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
1891 atomic_read(&dest->weight),
1892 atomic_read(&dest->activeconns),
1893 atomic_read(&dest->inactconns));
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 }
1897 return 0;
1898}
1899
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001900static const struct seq_operations ip_vs_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .start = ip_vs_info_seq_start,
1902 .next = ip_vs_info_seq_next,
1903 .stop = ip_vs_info_seq_stop,
1904 .show = ip_vs_info_seq_show,
1905};
1906
1907static int ip_vs_info_open(struct inode *inode, struct file *file)
1908{
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07001909 return seq_open_private(file, &ip_vs_info_seq_ops,
1910 sizeof(struct ip_vs_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Arjan van de Ven9a321442007-02-12 00:55:35 -08001913static const struct file_operations ip_vs_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .owner = THIS_MODULE,
1915 .open = ip_vs_info_open,
1916 .read = seq_read,
1917 .llseek = seq_lseek,
1918 .release = seq_release_private,
1919};
1920
1921#endif
1922
Sven Wegener519e49e2008-08-10 18:24:41 +00001923struct ip_vs_stats ip_vs_stats = {
1924 .lock = __SPIN_LOCK_UNLOCKED(ip_vs_stats.lock),
1925};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927#ifdef CONFIG_PROC_FS
1928static int ip_vs_stats_show(struct seq_file *seq, void *v)
1929{
1930
1931/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1932 seq_puts(seq,
1933 " Total Incoming Outgoing Incoming Outgoing\n");
1934 seq_printf(seq,
1935 " Conns Packets Packets Bytes Bytes\n");
1936
1937 spin_lock_bh(&ip_vs_stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +02001938 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", ip_vs_stats.ustats.conns,
1939 ip_vs_stats.ustats.inpkts, ip_vs_stats.ustats.outpkts,
1940 (unsigned long long) ip_vs_stats.ustats.inbytes,
1941 (unsigned long long) ip_vs_stats.ustats.outbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
1944 seq_puts(seq,
1945 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1946 seq_printf(seq,"%8X %8X %8X %16X %16X\n",
Sven Wegenere9c0ce22008-09-08 13:39:04 +02001947 ip_vs_stats.ustats.cps,
1948 ip_vs_stats.ustats.inpps,
1949 ip_vs_stats.ustats.outpps,
1950 ip_vs_stats.ustats.inbps,
1951 ip_vs_stats.ustats.outbps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 spin_unlock_bh(&ip_vs_stats.lock);
1953
1954 return 0;
1955}
1956
1957static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
1958{
1959 return single_open(file, ip_vs_stats_show, NULL);
1960}
1961
Arjan van de Ven9a321442007-02-12 00:55:35 -08001962static const struct file_operations ip_vs_stats_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 .owner = THIS_MODULE,
1964 .open = ip_vs_stats_seq_open,
1965 .read = seq_read,
1966 .llseek = seq_lseek,
1967 .release = single_release,
1968};
1969
1970#endif
1971
1972/*
1973 * Set timeout values for tcp tcpfin udp in the timeout_table.
1974 */
1975static int ip_vs_set_timeout(struct ip_vs_timeout_user *u)
1976{
1977 IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
1978 u->tcp_timeout,
1979 u->tcp_fin_timeout,
1980 u->udp_timeout);
1981
1982#ifdef CONFIG_IP_VS_PROTO_TCP
1983 if (u->tcp_timeout) {
1984 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED]
1985 = u->tcp_timeout * HZ;
1986 }
1987
1988 if (u->tcp_fin_timeout) {
1989 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT]
1990 = u->tcp_fin_timeout * HZ;
1991 }
1992#endif
1993
1994#ifdef CONFIG_IP_VS_PROTO_UDP
1995 if (u->udp_timeout) {
1996 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL]
1997 = u->udp_timeout * HZ;
1998 }
1999#endif
2000 return 0;
2001}
2002
2003
2004#define SET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2005#define SERVICE_ARG_LEN (sizeof(struct ip_vs_service_user))
2006#define SVCDEST_ARG_LEN (sizeof(struct ip_vs_service_user) + \
2007 sizeof(struct ip_vs_dest_user))
2008#define TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2009#define DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user))
2010#define MAX_ARG_LEN SVCDEST_ARG_LEN
2011
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08002012static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 [SET_CMDID(IP_VS_SO_SET_ADD)] = SERVICE_ARG_LEN,
2014 [SET_CMDID(IP_VS_SO_SET_EDIT)] = SERVICE_ARG_LEN,
2015 [SET_CMDID(IP_VS_SO_SET_DEL)] = SERVICE_ARG_LEN,
2016 [SET_CMDID(IP_VS_SO_SET_FLUSH)] = 0,
2017 [SET_CMDID(IP_VS_SO_SET_ADDDEST)] = SVCDEST_ARG_LEN,
2018 [SET_CMDID(IP_VS_SO_SET_DELDEST)] = SVCDEST_ARG_LEN,
2019 [SET_CMDID(IP_VS_SO_SET_EDITDEST)] = SVCDEST_ARG_LEN,
2020 [SET_CMDID(IP_VS_SO_SET_TIMEOUT)] = TIMEOUT_ARG_LEN,
2021 [SET_CMDID(IP_VS_SO_SET_STARTDAEMON)] = DAEMON_ARG_LEN,
2022 [SET_CMDID(IP_VS_SO_SET_STOPDAEMON)] = DAEMON_ARG_LEN,
2023 [SET_CMDID(IP_VS_SO_SET_ZERO)] = SERVICE_ARG_LEN,
2024};
2025
Julius Volzc860c6b2008-09-02 15:55:36 +02002026static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
2027 struct ip_vs_service_user *usvc_compat)
2028{
2029 usvc->af = AF_INET;
2030 usvc->protocol = usvc_compat->protocol;
2031 usvc->addr.ip = usvc_compat->addr;
2032 usvc->port = usvc_compat->port;
2033 usvc->fwmark = usvc_compat->fwmark;
2034
2035 /* Deep copy of sched_name is not needed here */
2036 usvc->sched_name = usvc_compat->sched_name;
2037
2038 usvc->flags = usvc_compat->flags;
2039 usvc->timeout = usvc_compat->timeout;
2040 usvc->netmask = usvc_compat->netmask;
2041}
2042
2043static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
2044 struct ip_vs_dest_user *udest_compat)
2045{
2046 udest->addr.ip = udest_compat->addr;
2047 udest->port = udest_compat->port;
2048 udest->conn_flags = udest_compat->conn_flags;
2049 udest->weight = udest_compat->weight;
2050 udest->u_threshold = udest_compat->u_threshold;
2051 udest->l_threshold = udest_compat->l_threshold;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static int
2055do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
2056{
2057 int ret;
2058 unsigned char arg[MAX_ARG_LEN];
Julius Volzc860c6b2008-09-02 15:55:36 +02002059 struct ip_vs_service_user *usvc_compat;
2060 struct ip_vs_service_user_kern usvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct ip_vs_service *svc;
Julius Volzc860c6b2008-09-02 15:55:36 +02002062 struct ip_vs_dest_user *udest_compat;
2063 struct ip_vs_dest_user_kern udest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (!capable(CAP_NET_ADMIN))
2066 return -EPERM;
2067
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002068 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
2069 return -EINVAL;
2070 if (len < 0 || len > MAX_ARG_LEN)
2071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (len != set_arglen[SET_CMDID(cmd)]) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002073 pr_err("set_ctl: len %u != %u\n",
2074 len, set_arglen[SET_CMDID(cmd)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return -EINVAL;
2076 }
2077
2078 if (copy_from_user(arg, user, len) != 0)
2079 return -EFAULT;
2080
2081 /* increase the module use count */
2082 ip_vs_use_count_inc();
2083
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002084 if (mutex_lock_interruptible(&__ip_vs_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 ret = -ERESTARTSYS;
2086 goto out_dec;
2087 }
2088
2089 if (cmd == IP_VS_SO_SET_FLUSH) {
2090 /* Flush the virtual service */
2091 ret = ip_vs_flush();
2092 goto out_unlock;
2093 } else if (cmd == IP_VS_SO_SET_TIMEOUT) {
2094 /* Set timeout values for (tcp tcpfin udp) */
2095 ret = ip_vs_set_timeout((struct ip_vs_timeout_user *)arg);
2096 goto out_unlock;
2097 } else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2098 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2099 ret = start_sync_thread(dm->state, dm->mcast_ifn, dm->syncid);
2100 goto out_unlock;
2101 } else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2102 struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2103 ret = stop_sync_thread(dm->state);
2104 goto out_unlock;
2105 }
2106
Julius Volzc860c6b2008-09-02 15:55:36 +02002107 usvc_compat = (struct ip_vs_service_user *)arg;
2108 udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);
2109
2110 /* We only use the new structs internally, so copy userspace compat
2111 * structs to extended internal versions */
2112 ip_vs_copy_usvc_compat(&usvc, usvc_compat);
2113 ip_vs_copy_udest_compat(&udest, udest_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 if (cmd == IP_VS_SO_SET_ZERO) {
2116 /* if no service address is set, zero counters in all */
Julius Volzc860c6b2008-09-02 15:55:36 +02002117 if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ret = ip_vs_zero_all();
2119 goto out_unlock;
2120 }
2121 }
2122
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01002123 /* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
2124 if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
2125 usvc.protocol != IPPROTO_SCTP) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002126 pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
2127 usvc.protocol, &usvc.addr.ip,
2128 ntohs(usvc.port), usvc.sched_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 ret = -EFAULT;
2130 goto out_unlock;
2131 }
2132
2133 /* Lookup the exact service by <protocol, addr, port> or fwmark */
Julius Volzc860c6b2008-09-02 15:55:36 +02002134 if (usvc.fwmark == 0)
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002135 svc = __ip_vs_service_find(usvc.af, usvc.protocol,
2136 &usvc.addr, usvc.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 else
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002138 svc = __ip_vs_svc_fwm_find(usvc.af, usvc.fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (cmd != IP_VS_SO_SET_ADD
Julius Volzc860c6b2008-09-02 15:55:36 +02002141 && (svc == NULL || svc->protocol != usvc.protocol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ret = -ESRCH;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002143 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
2146 switch (cmd) {
2147 case IP_VS_SO_SET_ADD:
2148 if (svc != NULL)
2149 ret = -EEXIST;
2150 else
Julius Volzc860c6b2008-09-02 15:55:36 +02002151 ret = ip_vs_add_service(&usvc, &svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 break;
2153 case IP_VS_SO_SET_EDIT:
Julius Volzc860c6b2008-09-02 15:55:36 +02002154 ret = ip_vs_edit_service(svc, &usvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156 case IP_VS_SO_SET_DEL:
2157 ret = ip_vs_del_service(svc);
2158 if (!ret)
2159 goto out_unlock;
2160 break;
2161 case IP_VS_SO_SET_ZERO:
2162 ret = ip_vs_zero_service(svc);
2163 break;
2164 case IP_VS_SO_SET_ADDDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002165 ret = ip_vs_add_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 break;
2167 case IP_VS_SO_SET_EDITDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002168 ret = ip_vs_edit_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 break;
2170 case IP_VS_SO_SET_DELDEST:
Julius Volzc860c6b2008-09-02 15:55:36 +02002171 ret = ip_vs_del_dest(svc, &udest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 break;
2173 default:
2174 ret = -EINVAL;
2175 }
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 out_unlock:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002178 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 out_dec:
2180 /* decrease the module use count */
2181 ip_vs_use_count_dec();
2182
2183 return ret;
2184}
2185
2186
2187static void
2188ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2189{
2190 spin_lock_bh(&src->lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +02002191 memcpy(dst, &src->ustats, sizeof(*dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 spin_unlock_bh(&src->lock);
2193}
2194
2195static void
2196ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2197{
2198 dst->protocol = src->protocol;
Julius Volze7ade462008-09-02 15:55:33 +02002199 dst->addr = src->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 dst->port = src->port;
2201 dst->fwmark = src->fwmark;
pageexec4da62fc2005-06-26 16:00:19 -07002202 strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 dst->flags = src->flags;
2204 dst->timeout = src->timeout / HZ;
2205 dst->netmask = src->netmask;
2206 dst->num_dests = src->num_dests;
2207 ip_vs_copy_stats(&dst->stats, &src->stats);
2208}
2209
2210static inline int
2211__ip_vs_get_service_entries(const struct ip_vs_get_services *get,
2212 struct ip_vs_get_services __user *uptr)
2213{
2214 int idx, count=0;
2215 struct ip_vs_service *svc;
2216 struct ip_vs_service_entry entry;
2217 int ret = 0;
2218
2219 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2220 list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002221 /* Only expose IPv4 entries to old interface */
2222 if (svc->af != AF_INET)
2223 continue;
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (count >= get->num_services)
2226 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002227 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 ip_vs_copy_service(&entry, svc);
2229 if (copy_to_user(&uptr->entrytable[count],
2230 &entry, sizeof(entry))) {
2231 ret = -EFAULT;
2232 goto out;
2233 }
2234 count++;
2235 }
2236 }
2237
2238 for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
2239 list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
Julius Volzf94fd042008-09-02 15:55:55 +02002240 /* Only expose IPv4 entries to old interface */
2241 if (svc->af != AF_INET)
2242 continue;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (count >= get->num_services)
2245 goto out;
pageexec4da62fc2005-06-26 16:00:19 -07002246 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 ip_vs_copy_service(&entry, svc);
2248 if (copy_to_user(&uptr->entrytable[count],
2249 &entry, sizeof(entry))) {
2250 ret = -EFAULT;
2251 goto out;
2252 }
2253 count++;
2254 }
2255 }
2256 out:
2257 return ret;
2258}
2259
2260static inline int
2261__ip_vs_get_dest_entries(const struct ip_vs_get_dests *get,
2262 struct ip_vs_get_dests __user *uptr)
2263{
2264 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002265 union nf_inet_addr addr = { .ip = get->addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 int ret = 0;
2267
2268 if (get->fwmark)
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002269 svc = __ip_vs_svc_fwm_find(AF_INET, get->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 else
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002271 svc = __ip_vs_service_find(AF_INET, get->protocol, &addr,
2272 get->port);
Julius Volzb18610d2008-09-02 15:55:37 +02002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (svc) {
2275 int count = 0;
2276 struct ip_vs_dest *dest;
2277 struct ip_vs_dest_entry entry;
2278
2279 list_for_each_entry(dest, &svc->destinations, n_list) {
2280 if (count >= get->num_dests)
2281 break;
2282
Julius Volze7ade462008-09-02 15:55:33 +02002283 entry.addr = dest->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 entry.port = dest->port;
2285 entry.conn_flags = atomic_read(&dest->conn_flags);
2286 entry.weight = atomic_read(&dest->weight);
2287 entry.u_threshold = dest->u_threshold;
2288 entry.l_threshold = dest->l_threshold;
2289 entry.activeconns = atomic_read(&dest->activeconns);
2290 entry.inactconns = atomic_read(&dest->inactconns);
2291 entry.persistconns = atomic_read(&dest->persistconns);
2292 ip_vs_copy_stats(&entry.stats, &dest->stats);
2293 if (copy_to_user(&uptr->entrytable[count],
2294 &entry, sizeof(entry))) {
2295 ret = -EFAULT;
2296 break;
2297 }
2298 count++;
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } else
2301 ret = -ESRCH;
2302 return ret;
2303}
2304
2305static inline void
2306__ip_vs_get_timeouts(struct ip_vs_timeout_user *u)
2307{
2308#ifdef CONFIG_IP_VS_PROTO_TCP
2309 u->tcp_timeout =
2310 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
2311 u->tcp_fin_timeout =
2312 ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
2313#endif
2314#ifdef CONFIG_IP_VS_PROTO_UDP
2315 u->udp_timeout =
2316 ip_vs_protocol_udp.timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
2317#endif
2318}
2319
2320
2321#define GET_CMDID(cmd) (cmd - IP_VS_BASE_CTL)
2322#define GET_INFO_ARG_LEN (sizeof(struct ip_vs_getinfo))
2323#define GET_SERVICES_ARG_LEN (sizeof(struct ip_vs_get_services))
2324#define GET_SERVICE_ARG_LEN (sizeof(struct ip_vs_service_entry))
2325#define GET_DESTS_ARG_LEN (sizeof(struct ip_vs_get_dests))
2326#define GET_TIMEOUT_ARG_LEN (sizeof(struct ip_vs_timeout_user))
2327#define GET_DAEMON_ARG_LEN (sizeof(struct ip_vs_daemon_user) * 2)
2328
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08002329static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 [GET_CMDID(IP_VS_SO_GET_VERSION)] = 64,
2331 [GET_CMDID(IP_VS_SO_GET_INFO)] = GET_INFO_ARG_LEN,
2332 [GET_CMDID(IP_VS_SO_GET_SERVICES)] = GET_SERVICES_ARG_LEN,
2333 [GET_CMDID(IP_VS_SO_GET_SERVICE)] = GET_SERVICE_ARG_LEN,
2334 [GET_CMDID(IP_VS_SO_GET_DESTS)] = GET_DESTS_ARG_LEN,
2335 [GET_CMDID(IP_VS_SO_GET_TIMEOUT)] = GET_TIMEOUT_ARG_LEN,
2336 [GET_CMDID(IP_VS_SO_GET_DAEMON)] = GET_DAEMON_ARG_LEN,
2337};
2338
2339static int
2340do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2341{
2342 unsigned char arg[128];
2343 int ret = 0;
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002344 unsigned int copylen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 if (!capable(CAP_NET_ADMIN))
2347 return -EPERM;
2348
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002349 if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
2350 return -EINVAL;
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (*len < get_arglen[GET_CMDID(cmd)]) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002353 pr_err("get_ctl: len %u < %u\n",
2354 *len, get_arglen[GET_CMDID(cmd)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return -EINVAL;
2356 }
2357
Arjan van de Ven04bcef22010-01-04 16:37:12 +01002358 copylen = get_arglen[GET_CMDID(cmd)];
2359 if (copylen > 128)
2360 return -EINVAL;
2361
2362 if (copy_from_user(arg, user, copylen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return -EFAULT;
2364
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002365 if (mutex_lock_interruptible(&__ip_vs_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -ERESTARTSYS;
2367
2368 switch (cmd) {
2369 case IP_VS_SO_GET_VERSION:
2370 {
2371 char buf[64];
2372
2373 sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002374 NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
2376 ret = -EFAULT;
2377 goto out;
2378 }
2379 *len = strlen(buf)+1;
2380 }
2381 break;
2382
2383 case IP_VS_SO_GET_INFO:
2384 {
2385 struct ip_vs_getinfo info;
2386 info.version = IP_VS_VERSION_CODE;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01002387 info.size = ip_vs_conn_tab_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 info.num_services = ip_vs_num_services;
2389 if (copy_to_user(user, &info, sizeof(info)) != 0)
2390 ret = -EFAULT;
2391 }
2392 break;
2393
2394 case IP_VS_SO_GET_SERVICES:
2395 {
2396 struct ip_vs_get_services *get;
2397 int size;
2398
2399 get = (struct ip_vs_get_services *)arg;
2400 size = sizeof(*get) +
2401 sizeof(struct ip_vs_service_entry) * get->num_services;
2402 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002403 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 ret = -EINVAL;
2405 goto out;
2406 }
2407 ret = __ip_vs_get_service_entries(get, user);
2408 }
2409 break;
2410
2411 case IP_VS_SO_GET_SERVICE:
2412 {
2413 struct ip_vs_service_entry *entry;
2414 struct ip_vs_service *svc;
Julius Volzb18610d2008-09-02 15:55:37 +02002415 union nf_inet_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 entry = (struct ip_vs_service_entry *)arg;
Julius Volzb18610d2008-09-02 15:55:37 +02002418 addr.ip = entry->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (entry->fwmark)
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002420 svc = __ip_vs_svc_fwm_find(AF_INET, entry->fwmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 else
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002422 svc = __ip_vs_service_find(AF_INET, entry->protocol,
2423 &addr, entry->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (svc) {
2425 ip_vs_copy_service(entry, svc);
2426 if (copy_to_user(user, entry, sizeof(*entry)) != 0)
2427 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 } else
2429 ret = -ESRCH;
2430 }
2431 break;
2432
2433 case IP_VS_SO_GET_DESTS:
2434 {
2435 struct ip_vs_get_dests *get;
2436 int size;
2437
2438 get = (struct ip_vs_get_dests *)arg;
2439 size = sizeof(*get) +
2440 sizeof(struct ip_vs_dest_entry) * get->num_dests;
2441 if (*len != size) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002442 pr_err("length: %u != %u\n", *len, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 ret = -EINVAL;
2444 goto out;
2445 }
2446 ret = __ip_vs_get_dest_entries(get, user);
2447 }
2448 break;
2449
2450 case IP_VS_SO_GET_TIMEOUT:
2451 {
2452 struct ip_vs_timeout_user t;
2453
2454 __ip_vs_get_timeouts(&t);
2455 if (copy_to_user(user, &t, sizeof(t)) != 0)
2456 ret = -EFAULT;
2457 }
2458 break;
2459
2460 case IP_VS_SO_GET_DAEMON:
2461 {
2462 struct ip_vs_daemon_user d[2];
2463
2464 memset(&d, 0, sizeof(d));
2465 if (ip_vs_sync_state & IP_VS_STATE_MASTER) {
2466 d[0].state = IP_VS_STATE_MASTER;
pageexec4da62fc2005-06-26 16:00:19 -07002467 strlcpy(d[0].mcast_ifn, ip_vs_master_mcast_ifn, sizeof(d[0].mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 d[0].syncid = ip_vs_master_syncid;
2469 }
2470 if (ip_vs_sync_state & IP_VS_STATE_BACKUP) {
2471 d[1].state = IP_VS_STATE_BACKUP;
pageexec4da62fc2005-06-26 16:00:19 -07002472 strlcpy(d[1].mcast_ifn, ip_vs_backup_mcast_ifn, sizeof(d[1].mcast_ifn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 d[1].syncid = ip_vs_backup_syncid;
2474 }
2475 if (copy_to_user(user, &d, sizeof(d)) != 0)
2476 ret = -EFAULT;
2477 }
2478 break;
2479
2480 default:
2481 ret = -EINVAL;
2482 }
2483
2484 out:
Ingo Molnar14cc3e22006-03-26 01:37:14 -08002485 mutex_unlock(&__ip_vs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return ret;
2487}
2488
2489
2490static struct nf_sockopt_ops ip_vs_sockopts = {
2491 .pf = PF_INET,
2492 .set_optmin = IP_VS_BASE_CTL,
2493 .set_optmax = IP_VS_SO_SET_MAX+1,
2494 .set = do_ip_vs_set_ctl,
2495 .get_optmin = IP_VS_BASE_CTL,
2496 .get_optmax = IP_VS_SO_GET_MAX+1,
2497 .get = do_ip_vs_get_ctl,
Neil Horman16fcec32007-09-11 11:28:26 +02002498 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499};
2500
Julius Volz9a812192008-08-14 14:08:44 +02002501/*
2502 * Generic Netlink interface
2503 */
2504
2505/* IPVS genetlink family */
2506static struct genl_family ip_vs_genl_family = {
2507 .id = GENL_ID_GENERATE,
2508 .hdrsize = 0,
2509 .name = IPVS_GENL_NAME,
2510 .version = IPVS_GENL_VERSION,
2511 .maxattr = IPVS_CMD_MAX,
2512};
2513
2514/* Policy used for first-level command attributes */
2515static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
2516 [IPVS_CMD_ATTR_SERVICE] = { .type = NLA_NESTED },
2517 [IPVS_CMD_ATTR_DEST] = { .type = NLA_NESTED },
2518 [IPVS_CMD_ATTR_DAEMON] = { .type = NLA_NESTED },
2519 [IPVS_CMD_ATTR_TIMEOUT_TCP] = { .type = NLA_U32 },
2520 [IPVS_CMD_ATTR_TIMEOUT_TCP_FIN] = { .type = NLA_U32 },
2521 [IPVS_CMD_ATTR_TIMEOUT_UDP] = { .type = NLA_U32 },
2522};
2523
2524/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
2525static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
2526 [IPVS_DAEMON_ATTR_STATE] = { .type = NLA_U32 },
2527 [IPVS_DAEMON_ATTR_MCAST_IFN] = { .type = NLA_NUL_STRING,
2528 .len = IP_VS_IFNAME_MAXLEN },
2529 [IPVS_DAEMON_ATTR_SYNC_ID] = { .type = NLA_U32 },
2530};
2531
2532/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
2533static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
2534 [IPVS_SVC_ATTR_AF] = { .type = NLA_U16 },
2535 [IPVS_SVC_ATTR_PROTOCOL] = { .type = NLA_U16 },
2536 [IPVS_SVC_ATTR_ADDR] = { .type = NLA_BINARY,
2537 .len = sizeof(union nf_inet_addr) },
2538 [IPVS_SVC_ATTR_PORT] = { .type = NLA_U16 },
2539 [IPVS_SVC_ATTR_FWMARK] = { .type = NLA_U32 },
2540 [IPVS_SVC_ATTR_SCHED_NAME] = { .type = NLA_NUL_STRING,
2541 .len = IP_VS_SCHEDNAME_MAXLEN },
2542 [IPVS_SVC_ATTR_FLAGS] = { .type = NLA_BINARY,
2543 .len = sizeof(struct ip_vs_flags) },
2544 [IPVS_SVC_ATTR_TIMEOUT] = { .type = NLA_U32 },
2545 [IPVS_SVC_ATTR_NETMASK] = { .type = NLA_U32 },
2546 [IPVS_SVC_ATTR_STATS] = { .type = NLA_NESTED },
2547};
2548
2549/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
2550static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2551 [IPVS_DEST_ATTR_ADDR] = { .type = NLA_BINARY,
2552 .len = sizeof(union nf_inet_addr) },
2553 [IPVS_DEST_ATTR_PORT] = { .type = NLA_U16 },
2554 [IPVS_DEST_ATTR_FWD_METHOD] = { .type = NLA_U32 },
2555 [IPVS_DEST_ATTR_WEIGHT] = { .type = NLA_U32 },
2556 [IPVS_DEST_ATTR_U_THRESH] = { .type = NLA_U32 },
2557 [IPVS_DEST_ATTR_L_THRESH] = { .type = NLA_U32 },
2558 [IPVS_DEST_ATTR_ACTIVE_CONNS] = { .type = NLA_U32 },
2559 [IPVS_DEST_ATTR_INACT_CONNS] = { .type = NLA_U32 },
2560 [IPVS_DEST_ATTR_PERSIST_CONNS] = { .type = NLA_U32 },
2561 [IPVS_DEST_ATTR_STATS] = { .type = NLA_NESTED },
2562};
2563
2564static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2565 struct ip_vs_stats *stats)
2566{
2567 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2568 if (!nl_stats)
2569 return -EMSGSIZE;
2570
2571 spin_lock_bh(&stats->lock);
2572
Sven Wegenere9c0ce22008-09-08 13:39:04 +02002573 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2574 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2575 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2576 NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2577 NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2578 NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2579 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2580 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2581 NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2582 NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
Julius Volz9a812192008-08-14 14:08:44 +02002583
2584 spin_unlock_bh(&stats->lock);
2585
2586 nla_nest_end(skb, nl_stats);
2587
2588 return 0;
2589
2590nla_put_failure:
2591 spin_unlock_bh(&stats->lock);
2592 nla_nest_cancel(skb, nl_stats);
2593 return -EMSGSIZE;
2594}
2595
2596static int ip_vs_genl_fill_service(struct sk_buff *skb,
2597 struct ip_vs_service *svc)
2598{
2599 struct nlattr *nl_service;
2600 struct ip_vs_flags flags = { .flags = svc->flags,
2601 .mask = ~0 };
2602
2603 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2604 if (!nl_service)
2605 return -EMSGSIZE;
2606
Julius Volzf94fd042008-09-02 15:55:55 +02002607 NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
Julius Volz9a812192008-08-14 14:08:44 +02002608
2609 if (svc->fwmark) {
2610 NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
2611 } else {
2612 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
2613 NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
2614 NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
2615 }
2616
2617 NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2618 NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
2619 NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
2620 NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);
2621
2622 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
2623 goto nla_put_failure;
2624
2625 nla_nest_end(skb, nl_service);
2626
2627 return 0;
2628
2629nla_put_failure:
2630 nla_nest_cancel(skb, nl_service);
2631 return -EMSGSIZE;
2632}
2633
2634static int ip_vs_genl_dump_service(struct sk_buff *skb,
2635 struct ip_vs_service *svc,
2636 struct netlink_callback *cb)
2637{
2638 void *hdr;
2639
2640 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2641 &ip_vs_genl_family, NLM_F_MULTI,
2642 IPVS_CMD_NEW_SERVICE);
2643 if (!hdr)
2644 return -EMSGSIZE;
2645
2646 if (ip_vs_genl_fill_service(skb, svc) < 0)
2647 goto nla_put_failure;
2648
2649 return genlmsg_end(skb, hdr);
2650
2651nla_put_failure:
2652 genlmsg_cancel(skb, hdr);
2653 return -EMSGSIZE;
2654}
2655
2656static int ip_vs_genl_dump_services(struct sk_buff *skb,
2657 struct netlink_callback *cb)
2658{
2659 int idx = 0, i;
2660 int start = cb->args[0];
2661 struct ip_vs_service *svc;
2662
2663 mutex_lock(&__ip_vs_mutex);
2664 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2665 list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2666 if (++idx <= start)
2667 continue;
2668 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2669 idx--;
2670 goto nla_put_failure;
2671 }
2672 }
2673 }
2674
2675 for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
2676 list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2677 if (++idx <= start)
2678 continue;
2679 if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
2680 idx--;
2681 goto nla_put_failure;
2682 }
2683 }
2684 }
2685
2686nla_put_failure:
2687 mutex_unlock(&__ip_vs_mutex);
2688 cb->args[0] = idx;
2689
2690 return skb->len;
2691}
2692
Julius Volzc860c6b2008-09-02 15:55:36 +02002693static int ip_vs_genl_parse_service(struct ip_vs_service_user_kern *usvc,
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002694 struct nlattr *nla, int full_entry,
2695 struct ip_vs_service **ret_svc)
Julius Volz9a812192008-08-14 14:08:44 +02002696{
2697 struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
2698 struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002699 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02002700
2701 /* Parse mandatory identifying service fields first */
2702 if (nla == NULL ||
2703 nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
2704 return -EINVAL;
2705
2706 nla_af = attrs[IPVS_SVC_ATTR_AF];
2707 nla_protocol = attrs[IPVS_SVC_ATTR_PROTOCOL];
2708 nla_addr = attrs[IPVS_SVC_ATTR_ADDR];
2709 nla_port = attrs[IPVS_SVC_ATTR_PORT];
2710 nla_fwmark = attrs[IPVS_SVC_ATTR_FWMARK];
2711
2712 if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
2713 return -EINVAL;
2714
Simon Horman258c8892009-12-15 17:01:25 +01002715 memset(usvc, 0, sizeof(*usvc));
2716
Julius Volzc860c6b2008-09-02 15:55:36 +02002717 usvc->af = nla_get_u16(nla_af);
Julius Volzf94fd042008-09-02 15:55:55 +02002718#ifdef CONFIG_IP_VS_IPV6
2719 if (usvc->af != AF_INET && usvc->af != AF_INET6)
2720#else
2721 if (usvc->af != AF_INET)
2722#endif
Julius Volz9a812192008-08-14 14:08:44 +02002723 return -EAFNOSUPPORT;
2724
2725 if (nla_fwmark) {
2726 usvc->protocol = IPPROTO_TCP;
2727 usvc->fwmark = nla_get_u32(nla_fwmark);
2728 } else {
2729 usvc->protocol = nla_get_u16(nla_protocol);
2730 nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
2731 usvc->port = nla_get_u16(nla_port);
2732 usvc->fwmark = 0;
2733 }
2734
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002735 if (usvc->fwmark)
2736 svc = __ip_vs_svc_fwm_find(usvc->af, usvc->fwmark);
2737 else
2738 svc = __ip_vs_service_find(usvc->af, usvc->protocol,
2739 &usvc->addr, usvc->port);
2740 *ret_svc = svc;
2741
Julius Volz9a812192008-08-14 14:08:44 +02002742 /* If a full entry was requested, check for the additional fields */
2743 if (full_entry) {
2744 struct nlattr *nla_sched, *nla_flags, *nla_timeout,
2745 *nla_netmask;
2746 struct ip_vs_flags flags;
Julius Volz9a812192008-08-14 14:08:44 +02002747
2748 nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2749 nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
2750 nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
2751 nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];
2752
2753 if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
2754 return -EINVAL;
2755
2756 nla_memcpy(&flags, nla_flags, sizeof(flags));
2757
2758 /* prefill flags from service if it already exists */
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002759 if (svc)
Julius Volz9a812192008-08-14 14:08:44 +02002760 usvc->flags = svc->flags;
Julius Volz9a812192008-08-14 14:08:44 +02002761
2762 /* set new flags from userland */
2763 usvc->flags = (usvc->flags & ~flags.mask) |
2764 (flags.flags & flags.mask);
Julius Volzc860c6b2008-09-02 15:55:36 +02002765 usvc->sched_name = nla_data(nla_sched);
Julius Volz9a812192008-08-14 14:08:44 +02002766 usvc->timeout = nla_get_u32(nla_timeout);
2767 usvc->netmask = nla_get_u32(nla_netmask);
2768 }
2769
2770 return 0;
2771}
2772
2773static struct ip_vs_service *ip_vs_genl_find_service(struct nlattr *nla)
2774{
Julius Volzc860c6b2008-09-02 15:55:36 +02002775 struct ip_vs_service_user_kern usvc;
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002776 struct ip_vs_service *svc;
Julius Volz9a812192008-08-14 14:08:44 +02002777 int ret;
2778
Julian Anastasov26c15cf2010-09-21 18:12:30 +02002779 ret = ip_vs_genl_parse_service(&usvc, nla, 0, &svc);
2780 return ret ? ERR_PTR(ret) : svc;
Julius Volz9a812192008-08-14 14:08:44 +02002781}
2782
2783static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
2784{
2785 struct nlattr *nl_dest;
2786
2787 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
2788 if (!nl_dest)
2789 return -EMSGSIZE;
2790
2791 NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
2792 NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);
2793
2794 NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
2795 atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
2796 NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
2797 NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
2798 NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
2799 NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
2800 atomic_read(&dest->activeconns));
2801 NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
2802 atomic_read(&dest->inactconns));
2803 NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
2804 atomic_read(&dest->persistconns));
2805
2806 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
2807 goto nla_put_failure;
2808
2809 nla_nest_end(skb, nl_dest);
2810
2811 return 0;
2812
2813nla_put_failure:
2814 nla_nest_cancel(skb, nl_dest);
2815 return -EMSGSIZE;
2816}
2817
2818static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
2819 struct netlink_callback *cb)
2820{
2821 void *hdr;
2822
2823 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2824 &ip_vs_genl_family, NLM_F_MULTI,
2825 IPVS_CMD_NEW_DEST);
2826 if (!hdr)
2827 return -EMSGSIZE;
2828
2829 if (ip_vs_genl_fill_dest(skb, dest) < 0)
2830 goto nla_put_failure;
2831
2832 return genlmsg_end(skb, hdr);
2833
2834nla_put_failure:
2835 genlmsg_cancel(skb, hdr);
2836 return -EMSGSIZE;
2837}
2838
2839static int ip_vs_genl_dump_dests(struct sk_buff *skb,
2840 struct netlink_callback *cb)
2841{
2842 int idx = 0;
2843 int start = cb->args[0];
2844 struct ip_vs_service *svc;
2845 struct ip_vs_dest *dest;
2846 struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
2847
2848 mutex_lock(&__ip_vs_mutex);
2849
2850 /* Try to find the service for which to dump destinations */
2851 if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
2852 IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
2853 goto out_err;
2854
2855 svc = ip_vs_genl_find_service(attrs[IPVS_CMD_ATTR_SERVICE]);
2856 if (IS_ERR(svc) || svc == NULL)
2857 goto out_err;
2858
2859 /* Dump the destinations */
2860 list_for_each_entry(dest, &svc->destinations, n_list) {
2861 if (++idx <= start)
2862 continue;
2863 if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
2864 idx--;
2865 goto nla_put_failure;
2866 }
2867 }
2868
2869nla_put_failure:
2870 cb->args[0] = idx;
Julius Volz9a812192008-08-14 14:08:44 +02002871
2872out_err:
2873 mutex_unlock(&__ip_vs_mutex);
2874
2875 return skb->len;
2876}
2877
Julius Volzc860c6b2008-09-02 15:55:36 +02002878static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
Julius Volz9a812192008-08-14 14:08:44 +02002879 struct nlattr *nla, int full_entry)
2880{
2881 struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
2882 struct nlattr *nla_addr, *nla_port;
2883
2884 /* Parse mandatory identifying destination fields first */
2885 if (nla == NULL ||
2886 nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
2887 return -EINVAL;
2888
2889 nla_addr = attrs[IPVS_DEST_ATTR_ADDR];
2890 nla_port = attrs[IPVS_DEST_ATTR_PORT];
2891
2892 if (!(nla_addr && nla_port))
2893 return -EINVAL;
2894
Simon Horman258c8892009-12-15 17:01:25 +01002895 memset(udest, 0, sizeof(*udest));
2896
Julius Volz9a812192008-08-14 14:08:44 +02002897 nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
2898 udest->port = nla_get_u16(nla_port);
2899
2900 /* If a full entry was requested, check for the additional fields */
2901 if (full_entry) {
2902 struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
2903 *nla_l_thresh;
2904
2905 nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
2906 nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
2907 nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
2908 nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
2909
2910 if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
2911 return -EINVAL;
2912
2913 udest->conn_flags = nla_get_u32(nla_fwd)
2914 & IP_VS_CONN_F_FWD_MASK;
2915 udest->weight = nla_get_u32(nla_weight);
2916 udest->u_threshold = nla_get_u32(nla_u_thresh);
2917 udest->l_threshold = nla_get_u32(nla_l_thresh);
2918 }
2919
2920 return 0;
2921}
2922
2923static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
2924 const char *mcast_ifn, __be32 syncid)
2925{
2926 struct nlattr *nl_daemon;
2927
2928 nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
2929 if (!nl_daemon)
2930 return -EMSGSIZE;
2931
2932 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
2933 NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
2934 NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);
2935
2936 nla_nest_end(skb, nl_daemon);
2937
2938 return 0;
2939
2940nla_put_failure:
2941 nla_nest_cancel(skb, nl_daemon);
2942 return -EMSGSIZE;
2943}
2944
2945static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
2946 const char *mcast_ifn, __be32 syncid,
2947 struct netlink_callback *cb)
2948{
2949 void *hdr;
2950 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
2951 &ip_vs_genl_family, NLM_F_MULTI,
2952 IPVS_CMD_NEW_DAEMON);
2953 if (!hdr)
2954 return -EMSGSIZE;
2955
2956 if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
2957 goto nla_put_failure;
2958
2959 return genlmsg_end(skb, hdr);
2960
2961nla_put_failure:
2962 genlmsg_cancel(skb, hdr);
2963 return -EMSGSIZE;
2964}
2965
2966static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
2967 struct netlink_callback *cb)
2968{
2969 mutex_lock(&__ip_vs_mutex);
2970 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
2971 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
2972 ip_vs_master_mcast_ifn,
2973 ip_vs_master_syncid, cb) < 0)
2974 goto nla_put_failure;
2975
2976 cb->args[0] = 1;
2977 }
2978
2979 if ((ip_vs_sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
2980 if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
2981 ip_vs_backup_mcast_ifn,
2982 ip_vs_backup_syncid, cb) < 0)
2983 goto nla_put_failure;
2984
2985 cb->args[1] = 1;
2986 }
2987
2988nla_put_failure:
2989 mutex_unlock(&__ip_vs_mutex);
2990
2991 return skb->len;
2992}
2993
2994static int ip_vs_genl_new_daemon(struct nlattr **attrs)
2995{
2996 if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
2997 attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
2998 attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
2999 return -EINVAL;
3000
3001 return start_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3002 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
3003 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
3004}
3005
3006static int ip_vs_genl_del_daemon(struct nlattr **attrs)
3007{
3008 if (!attrs[IPVS_DAEMON_ATTR_STATE])
3009 return -EINVAL;
3010
3011 return stop_sync_thread(nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3012}
3013
3014static int ip_vs_genl_set_config(struct nlattr **attrs)
3015{
3016 struct ip_vs_timeout_user t;
3017
3018 __ip_vs_get_timeouts(&t);
3019
3020 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
3021 t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);
3022
3023 if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
3024 t.tcp_fin_timeout =
3025 nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);
3026
3027 if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
3028 t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);
3029
3030 return ip_vs_set_timeout(&t);
3031}
3032
3033static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3034{
3035 struct ip_vs_service *svc = NULL;
Julius Volzc860c6b2008-09-02 15:55:36 +02003036 struct ip_vs_service_user_kern usvc;
3037 struct ip_vs_dest_user_kern udest;
Julius Volz9a812192008-08-14 14:08:44 +02003038 int ret = 0, cmd;
3039 int need_full_svc = 0, need_full_dest = 0;
3040
3041 cmd = info->genlhdr->cmd;
3042
3043 mutex_lock(&__ip_vs_mutex);
3044
3045 if (cmd == IPVS_CMD_FLUSH) {
3046 ret = ip_vs_flush();
3047 goto out;
3048 } else if (cmd == IPVS_CMD_SET_CONFIG) {
3049 ret = ip_vs_genl_set_config(info->attrs);
3050 goto out;
3051 } else if (cmd == IPVS_CMD_NEW_DAEMON ||
3052 cmd == IPVS_CMD_DEL_DAEMON) {
3053
3054 struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
3055
3056 if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
3057 nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
3058 info->attrs[IPVS_CMD_ATTR_DAEMON],
3059 ip_vs_daemon_policy)) {
3060 ret = -EINVAL;
3061 goto out;
3062 }
3063
3064 if (cmd == IPVS_CMD_NEW_DAEMON)
3065 ret = ip_vs_genl_new_daemon(daemon_attrs);
3066 else
3067 ret = ip_vs_genl_del_daemon(daemon_attrs);
3068 goto out;
3069 } else if (cmd == IPVS_CMD_ZERO &&
3070 !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3071 ret = ip_vs_zero_all();
3072 goto out;
3073 }
3074
3075 /* All following commands require a service argument, so check if we
3076 * received a valid one. We need a full service specification when
3077 * adding / editing a service. Only identifying members otherwise. */
3078 if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
3079 need_full_svc = 1;
3080
3081 ret = ip_vs_genl_parse_service(&usvc,
3082 info->attrs[IPVS_CMD_ATTR_SERVICE],
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003083 need_full_svc, &svc);
Julius Volz9a812192008-08-14 14:08:44 +02003084 if (ret)
3085 goto out;
3086
Julius Volz9a812192008-08-14 14:08:44 +02003087 /* Unless we're adding a new service, the service must already exist */
3088 if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
3089 ret = -ESRCH;
3090 goto out;
3091 }
3092
3093 /* Destination commands require a valid destination argument. For
3094 * adding / editing a destination, we need a full destination
3095 * specification. */
3096 if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
3097 cmd == IPVS_CMD_DEL_DEST) {
3098 if (cmd != IPVS_CMD_DEL_DEST)
3099 need_full_dest = 1;
3100
3101 ret = ip_vs_genl_parse_dest(&udest,
3102 info->attrs[IPVS_CMD_ATTR_DEST],
3103 need_full_dest);
3104 if (ret)
3105 goto out;
3106 }
3107
3108 switch (cmd) {
3109 case IPVS_CMD_NEW_SERVICE:
3110 if (svc == NULL)
3111 ret = ip_vs_add_service(&usvc, &svc);
3112 else
3113 ret = -EEXIST;
3114 break;
3115 case IPVS_CMD_SET_SERVICE:
3116 ret = ip_vs_edit_service(svc, &usvc);
3117 break;
3118 case IPVS_CMD_DEL_SERVICE:
3119 ret = ip_vs_del_service(svc);
Julian Anastasov26c15cf2010-09-21 18:12:30 +02003120 /* do not use svc, it can be freed */
Julius Volz9a812192008-08-14 14:08:44 +02003121 break;
3122 case IPVS_CMD_NEW_DEST:
3123 ret = ip_vs_add_dest(svc, &udest);
3124 break;
3125 case IPVS_CMD_SET_DEST:
3126 ret = ip_vs_edit_dest(svc, &udest);
3127 break;
3128 case IPVS_CMD_DEL_DEST:
3129 ret = ip_vs_del_dest(svc, &udest);
3130 break;
3131 case IPVS_CMD_ZERO:
3132 ret = ip_vs_zero_service(svc);
3133 break;
3134 default:
3135 ret = -EINVAL;
3136 }
3137
3138out:
Julius Volz9a812192008-08-14 14:08:44 +02003139 mutex_unlock(&__ip_vs_mutex);
3140
3141 return ret;
3142}
3143
3144static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
3145{
3146 struct sk_buff *msg;
3147 void *reply;
3148 int ret, cmd, reply_cmd;
3149
3150 cmd = info->genlhdr->cmd;
3151
3152 if (cmd == IPVS_CMD_GET_SERVICE)
3153 reply_cmd = IPVS_CMD_NEW_SERVICE;
3154 else if (cmd == IPVS_CMD_GET_INFO)
3155 reply_cmd = IPVS_CMD_SET_INFO;
3156 else if (cmd == IPVS_CMD_GET_CONFIG)
3157 reply_cmd = IPVS_CMD_SET_CONFIG;
3158 else {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003159 pr_err("unknown Generic Netlink command\n");
Julius Volz9a812192008-08-14 14:08:44 +02003160 return -EINVAL;
3161 }
3162
3163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3164 if (!msg)
3165 return -ENOMEM;
3166
3167 mutex_lock(&__ip_vs_mutex);
3168
3169 reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
3170 if (reply == NULL)
3171 goto nla_put_failure;
3172
3173 switch (cmd) {
3174 case IPVS_CMD_GET_SERVICE:
3175 {
3176 struct ip_vs_service *svc;
3177
3178 svc = ip_vs_genl_find_service(info->attrs[IPVS_CMD_ATTR_SERVICE]);
3179 if (IS_ERR(svc)) {
3180 ret = PTR_ERR(svc);
3181 goto out_err;
3182 } else if (svc) {
3183 ret = ip_vs_genl_fill_service(msg, svc);
Julius Volz9a812192008-08-14 14:08:44 +02003184 if (ret)
3185 goto nla_put_failure;
3186 } else {
3187 ret = -ESRCH;
3188 goto out_err;
3189 }
3190
3191 break;
3192 }
3193
3194 case IPVS_CMD_GET_CONFIG:
3195 {
3196 struct ip_vs_timeout_user t;
3197
3198 __ip_vs_get_timeouts(&t);
3199#ifdef CONFIG_IP_VS_PROTO_TCP
3200 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
3201 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
3202 t.tcp_fin_timeout);
3203#endif
3204#ifdef CONFIG_IP_VS_PROTO_UDP
3205 NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
3206#endif
3207
3208 break;
3209 }
3210
3211 case IPVS_CMD_GET_INFO:
3212 NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
3213 NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01003214 ip_vs_conn_tab_size);
Julius Volz9a812192008-08-14 14:08:44 +02003215 break;
3216 }
3217
3218 genlmsg_end(msg, reply);
Johannes Berg134e6372009-07-10 09:51:34 +00003219 ret = genlmsg_reply(msg, info);
Julius Volz9a812192008-08-14 14:08:44 +02003220 goto out;
3221
3222nla_put_failure:
Hannes Eder1e3e2382009-08-02 11:05:41 +00003223 pr_err("not enough space in Netlink message\n");
Julius Volz9a812192008-08-14 14:08:44 +02003224 ret = -EMSGSIZE;
3225
3226out_err:
3227 nlmsg_free(msg);
3228out:
3229 mutex_unlock(&__ip_vs_mutex);
3230
3231 return ret;
3232}
3233
3234
3235static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3236 {
3237 .cmd = IPVS_CMD_NEW_SERVICE,
3238 .flags = GENL_ADMIN_PERM,
3239 .policy = ip_vs_cmd_policy,
3240 .doit = ip_vs_genl_set_cmd,
3241 },
3242 {
3243 .cmd = IPVS_CMD_SET_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_DEL_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_GET_SERVICE,
3256 .flags = GENL_ADMIN_PERM,
3257 .doit = ip_vs_genl_get_cmd,
3258 .dumpit = ip_vs_genl_dump_services,
3259 .policy = ip_vs_cmd_policy,
3260 },
3261 {
3262 .cmd = IPVS_CMD_NEW_DEST,
3263 .flags = GENL_ADMIN_PERM,
3264 .policy = ip_vs_cmd_policy,
3265 .doit = ip_vs_genl_set_cmd,
3266 },
3267 {
3268 .cmd = IPVS_CMD_SET_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_DEL_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_GET_DEST,
3281 .flags = GENL_ADMIN_PERM,
3282 .policy = ip_vs_cmd_policy,
3283 .dumpit = ip_vs_genl_dump_dests,
3284 },
3285 {
3286 .cmd = IPVS_CMD_NEW_DAEMON,
3287 .flags = GENL_ADMIN_PERM,
3288 .policy = ip_vs_cmd_policy,
3289 .doit = ip_vs_genl_set_cmd,
3290 },
3291 {
3292 .cmd = IPVS_CMD_DEL_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_GET_DAEMON,
3299 .flags = GENL_ADMIN_PERM,
3300 .dumpit = ip_vs_genl_dump_daemons,
3301 },
3302 {
3303 .cmd = IPVS_CMD_SET_CONFIG,
3304 .flags = GENL_ADMIN_PERM,
3305 .policy = ip_vs_cmd_policy,
3306 .doit = ip_vs_genl_set_cmd,
3307 },
3308 {
3309 .cmd = IPVS_CMD_GET_CONFIG,
3310 .flags = GENL_ADMIN_PERM,
3311 .doit = ip_vs_genl_get_cmd,
3312 },
3313 {
3314 .cmd = IPVS_CMD_GET_INFO,
3315 .flags = GENL_ADMIN_PERM,
3316 .doit = ip_vs_genl_get_cmd,
3317 },
3318 {
3319 .cmd = IPVS_CMD_ZERO,
3320 .flags = GENL_ADMIN_PERM,
3321 .policy = ip_vs_cmd_policy,
3322 .doit = ip_vs_genl_set_cmd,
3323 },
3324 {
3325 .cmd = IPVS_CMD_FLUSH,
3326 .flags = GENL_ADMIN_PERM,
3327 .doit = ip_vs_genl_set_cmd,
3328 },
3329};
3330
3331static int __init ip_vs_genl_register(void)
3332{
Michał Mirosław8f698d52009-05-21 10:34:05 +00003333 return genl_register_family_with_ops(&ip_vs_genl_family,
3334 ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops));
Julius Volz9a812192008-08-14 14:08:44 +02003335}
3336
3337static void ip_vs_genl_unregister(void)
3338{
3339 genl_unregister_family(&ip_vs_genl_family);
3340}
3341
3342/* End of Generic Netlink interface definitions */
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Sven Wegener048cf482008-08-10 18:24:35 +00003345int __init ip_vs_control_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 int ret;
3348 int idx;
3349
3350 EnterFunction(2);
3351
3352 ret = nf_register_sockopt(&ip_vs_sockopts);
3353 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003354 pr_err("cannot register sockopt.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return ret;
3356 }
3357
Julius Volz9a812192008-08-14 14:08:44 +02003358 ret = ip_vs_genl_register();
3359 if (ret) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00003360 pr_err("cannot register Generic Netlink interface.\n");
Julius Volz9a812192008-08-14 14:08:44 +02003361 nf_unregister_sockopt(&ip_vs_sockopts);
3362 return ret;
3363 }
3364
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003365 proc_net_fops_create(&init_net, "ip_vs", 0, &ip_vs_info_fops);
3366 proc_net_fops_create(&init_net, "ip_vs_stats",0, &ip_vs_stats_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Pavel Emelyanov90754f82008-01-12 02:33:50 -08003368 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 /* Initialize ip_vs_svc_table, ip_vs_svc_fwm_table, ip_vs_rtable */
3371 for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
3372 INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
3373 INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
3374 }
3375 for(idx = 0; idx < IP_VS_RTAB_SIZE; idx++) {
3376 INIT_LIST_HEAD(&ip_vs_rtable[idx]);
3377 }
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ip_vs_new_estimator(&ip_vs_stats);
3380
3381 /* Hook the defense timer */
3382 schedule_delayed_work(&defense_work, DEFENSE_TIMER_PERIOD);
3383
3384 LeaveFunction(2);
3385 return 0;
3386}
3387
3388
3389void ip_vs_control_cleanup(void)
3390{
3391 EnterFunction(2);
3392 ip_vs_trash_cleanup();
3393 cancel_rearming_delayed_work(&defense_work);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07003394 cancel_work_sync(&defense_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 ip_vs_kill_estimator(&ip_vs_stats);
3396 unregister_sysctl_table(sysctl_header);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003397 proc_net_remove(&init_net, "ip_vs_stats");
3398 proc_net_remove(&init_net, "ip_vs");
Julius Volz9a812192008-08-14 14:08:44 +02003399 ip_vs_genl_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 nf_unregister_sockopt(&ip_vs_sockopts);
3401 LeaveFunction(2);
3402}