ip_vs_ctl.c 91.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * IPVS         An implementation of the IP virtual server support for the
 *              LINUX operating system.  IPVS is now implemented as a module
 *              over the NetFilter framework. IPVS can be used to build a
 *              high-performance and highly available server based on a
 *              cluster of servers.
 *
 * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
 *              Peter Kese <peter.kese@ijs.si>
 *              Julian Anastasov <ja@ssi.bg>
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *
 * Changes:
 *
 */

H
Hannes Eder 已提交
21 22 23
#define KMSG_COMPONENT "IPVS"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
24 25 26
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
27
#include <linux/capability.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33
#include <linux/fs.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/workqueue.h>
#include <linux/swap.h>
#include <linux/seq_file.h>
34
#include <linux/slab.h>
L
Linus Torvalds 已提交
35 36 37

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
38
#include <linux/mutex.h>
L
Linus Torvalds 已提交
39

40
#include <net/net_namespace.h>
41
#include <linux/nsproxy.h>
L
Linus Torvalds 已提交
42
#include <net/ip.h>
43 44 45 46
#ifdef CONFIG_IP_VS_IPV6
#include <net/ipv6.h>
#include <net/ip6_route.h>
#endif
47
#include <net/route.h>
L
Linus Torvalds 已提交
48
#include <net/sock.h>
49
#include <net/genetlink.h>
L
Linus Torvalds 已提交
50 51 52 53 54 55

#include <asm/uaccess.h>

#include <net/ip_vs.h>

/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
56
static DEFINE_MUTEX(__ip_vs_mutex);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

/* lock for service table */
static DEFINE_RWLOCK(__ip_vs_svc_lock);

/* sysctl variables */

#ifdef CONFIG_IP_VS_DEBUG
static int sysctl_ip_vs_debug_level = 0;

int ip_vs_get_debug_level(void)
{
	return sysctl_ip_vs_debug_level;
}
#endif

72 73 74 75 76

/*  Protos */
static void __ip_vs_del_service(struct ip_vs_service *svc);


77 78
#ifdef CONFIG_IP_VS_IPV6
/* Taken from rt6_fill_node() in net/ipv6/route.c, is there a better way? */
79 80
static int __ip_vs_addr_is_local_v6(struct net *net,
				    const struct in6_addr *addr)
81 82
{
	struct rt6_info *rt;
83 84
	struct flowi6 fl6 = {
		.daddr = *addr,
85 86
	};

87
	rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
88
	if (rt && rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
89
		return 1;
90 91 92 93

	return 0;
}
#endif
94 95

#ifdef CONFIG_SYSCTL
L
Linus Torvalds 已提交
96
/*
97 98
 *	update_defense_level is called from keventd and from sysctl,
 *	so it needs to protect itself from softirqs
L
Linus Torvalds 已提交
99
 */
100
static void update_defense_level(struct netns_ipvs *ipvs)
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
	struct sysinfo i;
	static int old_secure_tcp = 0;
	int availmem;
	int nomem;
	int to_change = -1;

	/* we only count free and buffered memory (in pages) */
	si_meminfo(&i);
	availmem = i.freeram + i.bufferram;
	/* however in linux 2.5 the i.bufferram is total page cache size,
	   we need adjust it */
	/* si_swapinfo(&i); */
	/* availmem = availmem - (i.totalswap - i.freeswap); */

116
	nomem = (availmem < ipvs->sysctl_amemthresh);
L
Linus Torvalds 已提交
117

118 119
	local_bh_disable();

L
Linus Torvalds 已提交
120
	/* drop_entry */
121 122
	spin_lock(&ipvs->dropentry_lock);
	switch (ipvs->sysctl_drop_entry) {
L
Linus Torvalds 已提交
123
	case 0:
124
		atomic_set(&ipvs->dropentry, 0);
L
Linus Torvalds 已提交
125 126 127
		break;
	case 1:
		if (nomem) {
128 129
			atomic_set(&ipvs->dropentry, 1);
			ipvs->sysctl_drop_entry = 2;
L
Linus Torvalds 已提交
130
		} else {
131
			atomic_set(&ipvs->dropentry, 0);
L
Linus Torvalds 已提交
132 133 134 135
		}
		break;
	case 2:
		if (nomem) {
136
			atomic_set(&ipvs->dropentry, 1);
L
Linus Torvalds 已提交
137
		} else {
138 139
			atomic_set(&ipvs->dropentry, 0);
			ipvs->sysctl_drop_entry = 1;
L
Linus Torvalds 已提交
140 141 142
		};
		break;
	case 3:
143
		atomic_set(&ipvs->dropentry, 1);
L
Linus Torvalds 已提交
144 145
		break;
	}
146
	spin_unlock(&ipvs->dropentry_lock);
L
Linus Torvalds 已提交
147 148

	/* drop_packet */
149 150
	spin_lock(&ipvs->droppacket_lock);
	switch (ipvs->sysctl_drop_packet) {
L
Linus Torvalds 已提交
151
	case 0:
152
		ipvs->drop_rate = 0;
L
Linus Torvalds 已提交
153 154 155
		break;
	case 1:
		if (nomem) {
156 157 158 159
			ipvs->drop_rate = ipvs->drop_counter
				= ipvs->sysctl_amemthresh /
				(ipvs->sysctl_amemthresh-availmem);
			ipvs->sysctl_drop_packet = 2;
L
Linus Torvalds 已提交
160
		} else {
161
			ipvs->drop_rate = 0;
L
Linus Torvalds 已提交
162 163 164 165
		}
		break;
	case 2:
		if (nomem) {
166 167 168
			ipvs->drop_rate = ipvs->drop_counter
				= ipvs->sysctl_amemthresh /
				(ipvs->sysctl_amemthresh-availmem);
L
Linus Torvalds 已提交
169
		} else {
170 171
			ipvs->drop_rate = 0;
			ipvs->sysctl_drop_packet = 1;
L
Linus Torvalds 已提交
172 173 174
		}
		break;
	case 3:
175
		ipvs->drop_rate = ipvs->sysctl_am_droprate;
L
Linus Torvalds 已提交
176 177
		break;
	}
178
	spin_unlock(&ipvs->droppacket_lock);
L
Linus Torvalds 已提交
179 180

	/* secure_tcp */
181 182
	spin_lock(&ipvs->securetcp_lock);
	switch (ipvs->sysctl_secure_tcp) {
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190
	case 0:
		if (old_secure_tcp >= 2)
			to_change = 0;
		break;
	case 1:
		if (nomem) {
			if (old_secure_tcp < 2)
				to_change = 1;
191
			ipvs->sysctl_secure_tcp = 2;
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203
		} else {
			if (old_secure_tcp >= 2)
				to_change = 0;
		}
		break;
	case 2:
		if (nomem) {
			if (old_secure_tcp < 2)
				to_change = 1;
		} else {
			if (old_secure_tcp >= 2)
				to_change = 0;
204
			ipvs->sysctl_secure_tcp = 1;
L
Linus Torvalds 已提交
205 206 207 208 209 210 211
		}
		break;
	case 3:
		if (old_secure_tcp < 2)
			to_change = 1;
		break;
	}
212
	old_secure_tcp = ipvs->sysctl_secure_tcp;
L
Linus Torvalds 已提交
213
	if (to_change >= 0)
214
		ip_vs_protocol_timeout_change(ipvs,
215 216
					      ipvs->sysctl_secure_tcp > 1);
	spin_unlock(&ipvs->securetcp_lock);
217 218

	local_bh_enable();
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226
}


/*
 *	Timer for checking the defense
 */
#define DEFENSE_TIMER_PERIOD	1*HZ

D
David Howells 已提交
227
static void defense_work_handler(struct work_struct *work)
L
Linus Torvalds 已提交
228
{
229 230
	struct netns_ipvs *ipvs =
		container_of(work, struct netns_ipvs, defense_work.work);
231 232

	update_defense_level(ipvs);
233
	if (atomic_read(&ipvs->dropentry))
234 235
		ip_vs_random_dropentry(ipvs->net);
	schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
L
Linus Torvalds 已提交
236
}
237
#endif
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

int
ip_vs_use_count_inc(void)
{
	return try_module_get(THIS_MODULE);
}

void
ip_vs_use_count_dec(void)
{
	module_put(THIS_MODULE);
}


/*
 *	Hash table: for virtual service lookups
 */
#define IP_VS_SVC_TAB_BITS 8
#define IP_VS_SVC_TAB_SIZE (1 << IP_VS_SVC_TAB_BITS)
#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)

/* the service table hashed by <protocol, addr, port> */
static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
/* the service table hashed by fwmark */
static struct list_head ip_vs_svc_fwm_table[IP_VS_SVC_TAB_SIZE];


/*
 *	Returns hash value for virtual service
 */
268 269 270
static inline unsigned
ip_vs_svc_hashkey(struct net *net, int af, unsigned proto,
		  const union nf_inet_addr *addr, __be16 port)
L
Linus Torvalds 已提交
271 272
{
	register unsigned porth = ntohs(port);
273
	__be32 addr_fold = addr->ip;
L
Linus Torvalds 已提交
274

275 276 277 278 279
#ifdef CONFIG_IP_VS_IPV6
	if (af == AF_INET6)
		addr_fold = addr->ip6[0]^addr->ip6[1]^
			    addr->ip6[2]^addr->ip6[3];
#endif
280
	addr_fold ^= ((size_t)net>>8);
281 282

	return (proto^ntohl(addr_fold)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
L
Linus Torvalds 已提交
283 284 285 286 287 288
		& IP_VS_SVC_TAB_MASK;
}

/*
 *	Returns hash value of fwmark for virtual service lookup
 */
289
static inline unsigned ip_vs_svc_fwm_hashkey(struct net *net, __u32 fwmark)
L
Linus Torvalds 已提交
290
{
291
	return (((size_t)net>>8) ^ fwmark) & IP_VS_SVC_TAB_MASK;
L
Linus Torvalds 已提交
292 293 294
}

/*
295
 *	Hashes a service in the ip_vs_svc_table by <netns,proto,addr,port>
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303
 *	or in the ip_vs_svc_fwm_table by fwmark.
 *	Should be called with locked tables.
 */
static int ip_vs_svc_hash(struct ip_vs_service *svc)
{
	unsigned hash;

	if (svc->flags & IP_VS_SVC_F_HASHED) {
304 305
		pr_err("%s(): request for already hashed, called from %pF\n",
		       __func__, __builtin_return_address(0));
L
Linus Torvalds 已提交
306 307 308 309 310
		return 0;
	}

	if (svc->fwmark == 0) {
		/*
311
		 *  Hash it by <netns,protocol,addr,port> in ip_vs_svc_table
L
Linus Torvalds 已提交
312
		 */
313 314
		hash = ip_vs_svc_hashkey(svc->net, svc->af, svc->protocol,
					 &svc->addr, svc->port);
L
Linus Torvalds 已提交
315 316 317
		list_add(&svc->s_list, &ip_vs_svc_table[hash]);
	} else {
		/*
318
		 *  Hash it by fwmark in svc_fwm_table
L
Linus Torvalds 已提交
319
		 */
320
		hash = ip_vs_svc_fwm_hashkey(svc->net, svc->fwmark);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331
		list_add(&svc->f_list, &ip_vs_svc_fwm_table[hash]);
	}

	svc->flags |= IP_VS_SVC_F_HASHED;
	/* increase its refcnt because it is referenced by the svc table */
	atomic_inc(&svc->refcnt);
	return 1;
}


/*
332
 *	Unhashes a service from svc_table / svc_fwm_table.
L
Linus Torvalds 已提交
333 334 335 336 337
 *	Should be called with locked tables.
 */
static int ip_vs_svc_unhash(struct ip_vs_service *svc)
{
	if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
338 339
		pr_err("%s(): request for unhash flagged, called from %pF\n",
		       __func__, __builtin_return_address(0));
L
Linus Torvalds 已提交
340 341 342 343
		return 0;
	}

	if (svc->fwmark == 0) {
344
		/* Remove it from the svc_table table */
L
Linus Torvalds 已提交
345 346
		list_del(&svc->s_list);
	} else {
347
		/* Remove it from the svc_fwm_table table */
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357
		list_del(&svc->f_list);
	}

	svc->flags &= ~IP_VS_SVC_F_HASHED;
	atomic_dec(&svc->refcnt);
	return 1;
}


/*
358
 *	Get service by {netns, proto,addr,port} in the service table.
L
Linus Torvalds 已提交
359
 */
360
static inline struct ip_vs_service *
361 362
__ip_vs_service_find(struct net *net, int af, __u16 protocol,
		     const union nf_inet_addr *vaddr, __be16 vport)
L
Linus Torvalds 已提交
363 364 365 366 367
{
	unsigned hash;
	struct ip_vs_service *svc;

	/* Check for "full" addressed entries */
368
	hash = ip_vs_svc_hashkey(net, af, protocol, vaddr, vport);
L
Linus Torvalds 已提交
369 370

	list_for_each_entry(svc, &ip_vs_svc_table[hash], s_list){
371 372
		if ((svc->af == af)
		    && ip_vs_addr_equal(af, &svc->addr, vaddr)
L
Linus Torvalds 已提交
373
		    && (svc->port == vport)
374 375
		    && (svc->protocol == protocol)
		    && net_eq(svc->net, net)) {
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387
			/* HIT */
			return svc;
		}
	}

	return NULL;
}


/*
 *	Get service by {fwmark} in the service table.
 */
388
static inline struct ip_vs_service *
389
__ip_vs_svc_fwm_find(struct net *net, int af, __u32 fwmark)
L
Linus Torvalds 已提交
390 391 392 393 394
{
	unsigned hash;
	struct ip_vs_service *svc;

	/* Check for fwmark addressed entries */
395
	hash = ip_vs_svc_fwm_hashkey(net, fwmark);
L
Linus Torvalds 已提交
396 397

	list_for_each_entry(svc, &ip_vs_svc_fwm_table[hash], f_list) {
398 399
		if (svc->fwmark == fwmark && svc->af == af
		    && net_eq(svc->net, net)) {
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408
			/* HIT */
			return svc;
		}
	}

	return NULL;
}

struct ip_vs_service *
409
ip_vs_service_get(struct net *net, int af, __u32 fwmark, __u16 protocol,
410
		  const union nf_inet_addr *vaddr, __be16 vport)
L
Linus Torvalds 已提交
411 412
{
	struct ip_vs_service *svc;
413
	struct netns_ipvs *ipvs = net_ipvs(net);
414

L
Linus Torvalds 已提交
415 416 417 418 419
	read_lock(&__ip_vs_svc_lock);

	/*
	 *	Check the table hashed by fwmark first
	 */
420 421 422 423 424
	if (fwmark) {
		svc = __ip_vs_svc_fwm_find(net, af, fwmark);
		if (svc)
			goto out;
	}
L
Linus Torvalds 已提交
425 426 427 428 429

	/*
	 *	Check the table hashed by <protocol,addr,port>
	 *	for "full" addressed entries
	 */
430
	svc = __ip_vs_service_find(net, af, protocol, vaddr, vport);
L
Linus Torvalds 已提交
431 432 433

	if (svc == NULL
	    && protocol == IPPROTO_TCP
434
	    && atomic_read(&ipvs->ftpsvc_counter)
L
Linus Torvalds 已提交
435 436 437 438 439
	    && (vport == FTPDATA || ntohs(vport) >= PROT_SOCK)) {
		/*
		 * Check if ftp service entry exists, the packet
		 * might belong to FTP data connections.
		 */
440
		svc = __ip_vs_service_find(net, af, protocol, vaddr, FTPPORT);
L
Linus Torvalds 已提交
441 442 443
	}

	if (svc == NULL
444
	    && atomic_read(&ipvs->nullsvc_counter)) {
L
Linus Torvalds 已提交
445 446 447
		/*
		 * Check if the catch-all port (port zero) exists
		 */
448
		svc = __ip_vs_service_find(net, af, protocol, vaddr, 0);
L
Linus Torvalds 已提交
449 450 451
	}

  out:
452 453
	if (svc)
		atomic_inc(&svc->usecnt);
L
Linus Torvalds 已提交
454 455
	read_unlock(&__ip_vs_svc_lock);

456 457 458 459
	IP_VS_DBG_BUF(9, "lookup service: fwm %u %s %s:%u %s\n",
		      fwmark, ip_vs_proto_name(protocol),
		      IP_VS_DBG_ADDR(af, vaddr), ntohs(vport),
		      svc ? "hit" : "not hit");
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469 470 471

	return svc;
}


static inline void
__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
{
	atomic_inc(&svc->refcnt);
	dest->svc = svc;
}

472
static void
L
Linus Torvalds 已提交
473 474 475 476 477
__ip_vs_unbind_svc(struct ip_vs_dest *dest)
{
	struct ip_vs_service *svc = dest->svc;

	dest->svc = NULL;
478 479 480 481 482
	if (atomic_dec_and_test(&svc->refcnt)) {
		IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n",
			      svc->fwmark,
			      IP_VS_DBG_ADDR(svc->af, &svc->addr),
			      ntohs(svc->port), atomic_read(&svc->usecnt));
483
		free_percpu(svc->stats.cpustats);
L
Linus Torvalds 已提交
484
		kfree(svc);
485
	}
L
Linus Torvalds 已提交
486 487 488 489 490 491
}


/*
 *	Returns hash value for real service
 */
492 493 494
static inline unsigned ip_vs_rs_hashkey(int af,
					    const union nf_inet_addr *addr,
					    __be16 port)
L
Linus Torvalds 已提交
495 496
{
	register unsigned porth = ntohs(port);
497 498 499 500 501 502 503
	__be32 addr_fold = addr->ip;

#ifdef CONFIG_IP_VS_IPV6
	if (af == AF_INET6)
		addr_fold = addr->ip6[0]^addr->ip6[1]^
			    addr->ip6[2]^addr->ip6[3];
#endif
L
Linus Torvalds 已提交
504

505
	return (ntohl(addr_fold)^(porth>>IP_VS_RTAB_BITS)^porth)
L
Linus Torvalds 已提交
506 507 508 509
		& IP_VS_RTAB_MASK;
}

/*
510
 *	Hashes ip_vs_dest in rs_table by <proto,addr,port>.
L
Linus Torvalds 已提交
511 512
 *	should be called with locked tables.
 */
513
static int ip_vs_rs_hash(struct netns_ipvs *ipvs, struct ip_vs_dest *dest)
L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523 524
{
	unsigned hash;

	if (!list_empty(&dest->d_list)) {
		return 0;
	}

	/*
	 *	Hash by proto,addr,port,
	 *	which are the parameters of the real service.
	 */
525 526
	hash = ip_vs_rs_hashkey(dest->af, &dest->addr, dest->port);

527
	list_add(&dest->d_list, &ipvs->rs_table[hash]);
L
Linus Torvalds 已提交
528 529 530 531 532

	return 1;
}

/*
533
 *	UNhashes ip_vs_dest from rs_table.
L
Linus Torvalds 已提交
534 535 536 537 538
 *	should be called with locked tables.
 */
static int ip_vs_rs_unhash(struct ip_vs_dest *dest)
{
	/*
539
	 * Remove it from the rs_table table.
L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552
	 */
	if (!list_empty(&dest->d_list)) {
		list_del(&dest->d_list);
		INIT_LIST_HEAD(&dest->d_list);
	}

	return 1;
}

/*
 *	Lookup real service by <proto,addr,port> in the real service table.
 */
struct ip_vs_dest *
553
ip_vs_lookup_real_service(struct net *net, int af, __u16 protocol,
554 555
			  const union nf_inet_addr *daddr,
			  __be16 dport)
L
Linus Torvalds 已提交
556
{
557
	struct netns_ipvs *ipvs = net_ipvs(net);
L
Linus Torvalds 已提交
558 559 560 561 562 563 564
	unsigned hash;
	struct ip_vs_dest *dest;

	/*
	 *	Check for "full" addressed entries
	 *	Return the first found entry
	 */
565
	hash = ip_vs_rs_hashkey(af, daddr, dport);
L
Linus Torvalds 已提交
566

567
	read_lock(&ipvs->rs_lock);
568
	list_for_each_entry(dest, &ipvs->rs_table[hash], d_list) {
569 570
		if ((dest->af == af)
		    && ip_vs_addr_equal(af, &dest->addr, daddr)
L
Linus Torvalds 已提交
571 572 573 574
		    && (dest->port == dport)
		    && ((dest->protocol == protocol) ||
			dest->vfwmark)) {
			/* HIT */
575
			read_unlock(&ipvs->rs_lock);
L
Linus Torvalds 已提交
576 577 578
			return dest;
		}
	}
579
	read_unlock(&ipvs->rs_lock);
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587

	return NULL;
}

/*
 *	Lookup destination by {addr,port} in the given service
 */
static struct ip_vs_dest *
588 589
ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
		  __be16 dport)
L
Linus Torvalds 已提交
590 591 592 593 594 595 596
{
	struct ip_vs_dest *dest;

	/*
	 * Find the destination for the given service
	 */
	list_for_each_entry(dest, &svc->destinations, n_list) {
597 598 599
		if ((dest->af == svc->af)
		    && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
		    && (dest->port == dport)) {
L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607
			/* HIT */
			return dest;
		}
	}

	return NULL;
}

608 609 610 611 612 613 614 615 616 617
/*
 * Find destination by {daddr,dport,vaddr,protocol}
 * Cretaed to be used in ip_vs_process_message() in
 * the backup synchronization daemon. It finds the
 * destination to be bound to the received connection
 * on the backup.
 *
 * ip_vs_lookup_real_service() looked promissing, but
 * seems not working as expected.
 */
618 619
struct ip_vs_dest *ip_vs_find_dest(struct net  *net, int af,
				   const union nf_inet_addr *daddr,
620 621
				   __be16 dport,
				   const union nf_inet_addr *vaddr,
622
				   __be16 vport, __u16 protocol, __u32 fwmark)
623 624 625 626
{
	struct ip_vs_dest *dest;
	struct ip_vs_service *svc;

627
	svc = ip_vs_service_get(net, af, fwmark, protocol, vaddr, vport);
628 629 630 631 632 633 634 635
	if (!svc)
		return NULL;
	dest = ip_vs_lookup_dest(svc, daddr, dport);
	if (dest)
		atomic_inc(&dest->refcnt);
	ip_vs_service_put(svc);
	return dest;
}
L
Linus Torvalds 已提交
636 637 638 639 640 641 642 643 644 645 646 647

/*
 *  Lookup dest by {svc,addr,port} in the destination trash.
 *  The destination trash is used to hold the destinations that are removed
 *  from the service table but are still referenced by some conn entries.
 *  The reason to add the destination trash is when the dest is temporary
 *  down (either by administrator or by monitor program), the dest can be
 *  picked back from the trash, the remaining connections to the dest can
 *  continue, and the counting information of the dest is also useful for
 *  scheduling.
 */
static struct ip_vs_dest *
648 649
ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
		     __be16 dport)
L
Linus Torvalds 已提交
650 651
{
	struct ip_vs_dest *dest, *nxt;
H
Hans Schillstrom 已提交
652
	struct netns_ipvs *ipvs = net_ipvs(svc->net);
L
Linus Torvalds 已提交
653 654 655 656

	/*
	 * Find the destination in trash
	 */
H
Hans Schillstrom 已提交
657
	list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, n_list) {
658 659 660 661 662 663 664 665
		IP_VS_DBG_BUF(3, "Destination %u/%s:%u still in trash, "
			      "dest->refcnt=%d\n",
			      dest->vfwmark,
			      IP_VS_DBG_ADDR(svc->af, &dest->addr),
			      ntohs(dest->port),
			      atomic_read(&dest->refcnt));
		if (dest->af == svc->af &&
		    ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
L
Linus Torvalds 已提交
666 667 668 669
		    dest->port == dport &&
		    dest->vfwmark == svc->fwmark &&
		    dest->protocol == svc->protocol &&
		    (svc->fwmark ||
670
		     (ip_vs_addr_equal(svc->af, &dest->vaddr, &svc->addr) &&
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679
		      dest->vport == svc->port))) {
			/* HIT */
			return dest;
		}

		/*
		 * Try to purge the destination from trash if not referenced
		 */
		if (atomic_read(&dest->refcnt) == 1) {
680 681 682 683 684
			IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u "
				      "from trash\n",
				      dest->vfwmark,
				      IP_VS_DBG_ADDR(svc->af, &dest->addr),
				      ntohs(dest->port));
L
Linus Torvalds 已提交
685 686 687
			list_del(&dest->n_list);
			ip_vs_dst_reset(dest);
			__ip_vs_unbind_svc(dest);
688
			free_percpu(dest->stats.cpustats);
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
			kfree(dest);
		}
	}

	return NULL;
}


/*
 *  Clean up all the destinations in the trash
 *  Called by the ip_vs_control_cleanup()
 *
 *  When the ip_vs_control_clearup is activated by ipvs module exit,
 *  the service tables must have been flushed and all the connections
 *  are expired, and the refcnt of each destination in the trash must
 *  be 1, so we simply release them here.
 */
H
Hans Schillstrom 已提交
706
static void ip_vs_trash_cleanup(struct net *net)
L
Linus Torvalds 已提交
707 708
{
	struct ip_vs_dest *dest, *nxt;
H
Hans Schillstrom 已提交
709
	struct netns_ipvs *ipvs = net_ipvs(net);
L
Linus Torvalds 已提交
710

H
Hans Schillstrom 已提交
711
	list_for_each_entry_safe(dest, nxt, &ipvs->dest_trash, n_list) {
L
Linus Torvalds 已提交
712 713 714
		list_del(&dest->n_list);
		ip_vs_dst_reset(dest);
		__ip_vs_unbind_svc(dest);
715
		free_percpu(dest->stats.cpustats);
L
Linus Torvalds 已提交
716 717 718 719
		kfree(dest);
	}
}

720 721 722 723 724 725 726 727 728 729 730 731 732
static void
ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
{
#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c

	spin_lock_bh(&src->lock);

	IP_VS_SHOW_STATS_COUNTER(conns);
	IP_VS_SHOW_STATS_COUNTER(inpkts);
	IP_VS_SHOW_STATS_COUNTER(outpkts);
	IP_VS_SHOW_STATS_COUNTER(inbytes);
	IP_VS_SHOW_STATS_COUNTER(outbytes);

J
Julian Anastasov 已提交
733
	ip_vs_read_estimator(dst, src);
734 735 736

	spin_unlock_bh(&src->lock);
}
L
Linus Torvalds 已提交
737 738 739 740 741

static void
ip_vs_zero_stats(struct ip_vs_stats *stats)
{
	spin_lock_bh(&stats->lock);
742

743 744 745 746 747 748 749 750 751 752
	/* get current counters as zero point, rates are zeroed */

#define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c

	IP_VS_ZERO_STATS_COUNTER(conns);
	IP_VS_ZERO_STATS_COUNTER(inpkts);
	IP_VS_ZERO_STATS_COUNTER(outpkts);
	IP_VS_ZERO_STATS_COUNTER(inbytes);
	IP_VS_ZERO_STATS_COUNTER(outbytes);

L
Linus Torvalds 已提交
753
	ip_vs_zero_estimator(stats);
754

755
	spin_unlock_bh(&stats->lock);
L
Linus Torvalds 已提交
756 757 758 759 760 761
}

/*
 *	Update a destination in the given service
 */
static void
762 763
__ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
		    struct ip_vs_dest_user_kern *udest, int add)
L
Linus Torvalds 已提交
764
{
765
	struct netns_ipvs *ipvs = net_ipvs(svc->net);
L
Linus Torvalds 已提交
766 767 768 769
	int conn_flags;

	/* set the weight and the flags */
	atomic_set(&dest->weight, udest->weight);
770 771
	conn_flags = udest->conn_flags & IP_VS_CONN_F_DEST_MASK;
	conn_flags |= IP_VS_CONN_F_INACTIVE;
L
Linus Torvalds 已提交
772 773

	/* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
774
	if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
L
Linus Torvalds 已提交
775 776 777
		conn_flags |= IP_VS_CONN_F_NOOUTPUT;
	} else {
		/*
778
		 *    Put the real service in rs_table if not present.
L
Linus Torvalds 已提交
779 780
		 *    For now only for NAT!
		 */
781
		write_lock_bh(&ipvs->rs_lock);
782
		ip_vs_rs_hash(ipvs, dest);
783
		write_unlock_bh(&ipvs->rs_lock);
L
Linus Torvalds 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	}
	atomic_set(&dest->conn_flags, conn_flags);

	/* bind the service */
	if (!dest->svc) {
		__ip_vs_bind_svc(dest, svc);
	} else {
		if (dest->svc != svc) {
			__ip_vs_unbind_svc(dest);
			ip_vs_zero_stats(&dest->stats);
			__ip_vs_bind_svc(dest, svc);
		}
	}

	/* set the dest status flags */
	dest->flags |= IP_VS_DEST_F_AVAILABLE;

	if (udest->u_threshold == 0 || udest->u_threshold > dest->u_threshold)
		dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
	dest->u_threshold = udest->u_threshold;
	dest->l_threshold = udest->l_threshold;
805

806
	spin_lock_bh(&dest->dst_lock);
807
	ip_vs_dst_reset(dest);
808
	spin_unlock_bh(&dest->dst_lock);
809

810
	if (add)
811
		ip_vs_start_estimator(svc->net, &dest->stats);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

	write_lock_bh(&__ip_vs_svc_lock);

	/* Wait until all other svc users go away */
	IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);

	if (add) {
		list_add(&dest->n_list, &svc->destinations);
		svc->num_dests++;
	}

	/* call the update_service, because server weight may be changed */
	if (svc->scheduler->update_service)
		svc->scheduler->update_service(svc);

	write_unlock_bh(&__ip_vs_svc_lock);
L
Linus Torvalds 已提交
828 829 830 831 832 833 834
}


/*
 *	Create a destination for the given service
 */
static int
835
ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
L
Linus Torvalds 已提交
836 837 838 839 840 841 842
	       struct ip_vs_dest **dest_p)
{
	struct ip_vs_dest *dest;
	unsigned atype;

	EnterFunction(2);

843 844 845
#ifdef CONFIG_IP_VS_IPV6
	if (svc->af == AF_INET6) {
		atype = ipv6_addr_type(&udest->addr.in6);
846 847
		if ((!(atype & IPV6_ADDR_UNICAST) ||
			atype & IPV6_ADDR_LINKLOCAL) &&
848
			!__ip_vs_addr_is_local_v6(svc->net, &udest->addr.in6))
849 850 851 852
			return -EINVAL;
	} else
#endif
	{
853
		atype = inet_addr_type(svc->net, udest->addr.ip);
854 855 856
		if (atype != RTN_LOCAL && atype != RTN_UNICAST)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
857

858
	dest = kzalloc(sizeof(struct ip_vs_dest), GFP_KERNEL);
L
Linus Torvalds 已提交
859
	if (dest == NULL) {
860
		pr_err("%s(): no memory.\n", __func__);
L
Linus Torvalds 已提交
861 862
		return -ENOMEM;
	}
863 864 865 866 867
	dest->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
	if (!dest->stats.cpustats) {
		pr_err("%s() alloc_percpu failed\n", __func__);
		goto err_alloc;
	}
L
Linus Torvalds 已提交
868

869
	dest->af = svc->af;
L
Linus Torvalds 已提交
870
	dest->protocol = svc->protocol;
871
	dest->vaddr = svc->addr;
L
Linus Torvalds 已提交
872 873
	dest->vport = svc->port;
	dest->vfwmark = svc->fwmark;
874
	ip_vs_addr_copy(svc->af, &dest->addr, &udest->addr);
L
Linus Torvalds 已提交
875 876 877 878 879
	dest->port = udest->port;

	atomic_set(&dest->activeconns, 0);
	atomic_set(&dest->inactconns, 0);
	atomic_set(&dest->persistconns, 0);
880
	atomic_set(&dest->refcnt, 1);
L
Linus Torvalds 已提交
881 882 883 884

	INIT_LIST_HEAD(&dest->d_list);
	spin_lock_init(&dest->dst_lock);
	spin_lock_init(&dest->stats.lock);
885
	__ip_vs_update_dest(svc, dest, udest, 1);
L
Linus Torvalds 已提交
886 887 888 889 890

	*dest_p = dest;

	LeaveFunction(2);
	return 0;
891 892 893 894

err_alloc:
	kfree(dest);
	return -ENOMEM;
L
Linus Torvalds 已提交
895 896 897 898 899 900 901
}


/*
 *	Add a destination into an existing service
 */
static int
902
ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
L
Linus Torvalds 已提交
903 904
{
	struct ip_vs_dest *dest;
905
	union nf_inet_addr daddr;
A
Al Viro 已提交
906
	__be16 dport = udest->port;
L
Linus Torvalds 已提交
907 908 909 910 911
	int ret;

	EnterFunction(2);

	if (udest->weight < 0) {
912
		pr_err("%s(): server weight less than zero\n", __func__);
L
Linus Torvalds 已提交
913 914 915 916
		return -ERANGE;
	}

	if (udest->l_threshold > udest->u_threshold) {
917 918
		pr_err("%s(): lower threshold is higher than upper threshold\n",
			__func__);
L
Linus Torvalds 已提交
919 920 921
		return -ERANGE;
	}

922 923
	ip_vs_addr_copy(svc->af, &daddr, &udest->addr);

L
Linus Torvalds 已提交
924 925 926
	/*
	 * Check if the dest already exists in the list
	 */
927 928
	dest = ip_vs_lookup_dest(svc, &daddr, dport);

L
Linus Torvalds 已提交
929
	if (dest != NULL) {
930
		IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
L
Linus Torvalds 已提交
931 932 933 934 935 936 937
		return -EEXIST;
	}

	/*
	 * Check if the dest already exists in the trash and
	 * is from the same service
	 */
938 939
	dest = ip_vs_trash_get_dest(svc, &daddr, dport);

L
Linus Torvalds 已提交
940
	if (dest != NULL) {
941 942 943 944 945 946 947 948
		IP_VS_DBG_BUF(3, "Get destination %s:%u from trash, "
			      "dest->refcnt=%d, service %u/%s:%u\n",
			      IP_VS_DBG_ADDR(svc->af, &daddr), ntohs(dport),
			      atomic_read(&dest->refcnt),
			      dest->vfwmark,
			      IP_VS_DBG_ADDR(svc->af, &dest->vaddr),
			      ntohs(dest->vport));

L
Linus Torvalds 已提交
949 950 951 952 953
		/*
		 * Get the destination from the trash
		 */
		list_del(&dest->n_list);

954 955 956
		__ip_vs_update_dest(svc, dest, udest, 1);
		ret = 0;
	} else {
L
Linus Torvalds 已提交
957
		/*
958
		 * Allocate and initialize the dest structure
L
Linus Torvalds 已提交
959
		 */
960
		ret = ip_vs_new_dest(svc, udest, &dest);
L
Linus Torvalds 已提交
961 962 963
	}
	LeaveFunction(2);

964
	return ret;
L
Linus Torvalds 已提交
965 966 967 968 969 970 971
}


/*
 *	Edit a destination in the given service
 */
static int
972
ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
L
Linus Torvalds 已提交
973 974
{
	struct ip_vs_dest *dest;
975
	union nf_inet_addr daddr;
A
Al Viro 已提交
976
	__be16 dport = udest->port;
L
Linus Torvalds 已提交
977 978 979 980

	EnterFunction(2);

	if (udest->weight < 0) {
981
		pr_err("%s(): server weight less than zero\n", __func__);
L
Linus Torvalds 已提交
982 983 984 985
		return -ERANGE;
	}

	if (udest->l_threshold > udest->u_threshold) {
986 987
		pr_err("%s(): lower threshold is higher than upper threshold\n",
			__func__);
L
Linus Torvalds 已提交
988 989 990
		return -ERANGE;
	}

991 992
	ip_vs_addr_copy(svc->af, &daddr, &udest->addr);

L
Linus Torvalds 已提交
993 994 995
	/*
	 *  Lookup the destination list
	 */
996 997
	dest = ip_vs_lookup_dest(svc, &daddr, dport);

L
Linus Torvalds 已提交
998
	if (dest == NULL) {
999
		IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
L
Linus Torvalds 已提交
1000 1001 1002
		return -ENOENT;
	}

1003
	__ip_vs_update_dest(svc, dest, udest, 0);
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012
	LeaveFunction(2);

	return 0;
}


/*
 *	Delete a destination (must be already unlinked from the service)
 */
1013
static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest)
L
Linus Torvalds 已提交
1014
{
1015 1016
	struct netns_ipvs *ipvs = net_ipvs(net);

1017
	ip_vs_stop_estimator(net, &dest->stats);
L
Linus Torvalds 已提交
1018 1019 1020 1021

	/*
	 *  Remove it from the d-linked list with the real services.
	 */
1022
	write_lock_bh(&ipvs->rs_lock);
L
Linus Torvalds 已提交
1023
	ip_vs_rs_unhash(dest);
1024
	write_unlock_bh(&ipvs->rs_lock);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030 1031

	/*
	 *  Decrease the refcnt of the dest, and free the dest
	 *  if nobody refers to it (refcnt=0). Otherwise, throw
	 *  the destination into the trash.
	 */
	if (atomic_dec_and_test(&dest->refcnt)) {
1032 1033 1034 1035
		IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u\n",
			      dest->vfwmark,
			      IP_VS_DBG_ADDR(dest->af, &dest->addr),
			      ntohs(dest->port));
L
Linus Torvalds 已提交
1036 1037 1038 1039 1040 1041 1042
		ip_vs_dst_reset(dest);
		/* simply decrease svc->refcnt here, let the caller check
		   and release the service if nobody refers to it.
		   Only user context can release destination and service,
		   and only one user context can update virtual service at a
		   time, so the operation here is OK */
		atomic_dec(&dest->svc->refcnt);
1043
		free_percpu(dest->stats.cpustats);
L
Linus Torvalds 已提交
1044 1045
		kfree(dest);
	} else {
1046 1047 1048 1049 1050
		IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, "
			      "dest->refcnt=%d\n",
			      IP_VS_DBG_ADDR(dest->af, &dest->addr),
			      ntohs(dest->port),
			      atomic_read(&dest->refcnt));
H
Hans Schillstrom 已提交
1051
		list_add(&dest->n_list, &ipvs->dest_trash);
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		atomic_inc(&dest->refcnt);
	}
}


/*
 *	Unlink a destination from the given service
 */
static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
				struct ip_vs_dest *dest,
				int svcupd)
{
	dest->flags &= ~IP_VS_DEST_F_AVAILABLE;

	/*
	 *  Remove it from the d-linked destination list.
	 */
	list_del(&dest->n_list);
	svc->num_dests--;
1071 1072 1073 1074 1075 1076

	/*
	 *  Call the update_service function of its scheduler
	 */
	if (svcupd && svc->scheduler->update_service)
			svc->scheduler->update_service(svc);
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083
}


/*
 *	Delete a destination server in the given service
 */
static int
1084
ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
L
Linus Torvalds 已提交
1085 1086
{
	struct ip_vs_dest *dest;
A
Al Viro 已提交
1087
	__be16 dport = udest->port;
L
Linus Torvalds 已提交
1088 1089 1090

	EnterFunction(2);

1091
	dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
1092

L
Linus Torvalds 已提交
1093
	if (dest == NULL) {
1094
		IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099 1100 1101 1102
		return -ENOENT;
	}

	write_lock_bh(&__ip_vs_svc_lock);

	/*
	 *	Wait until all other svc users go away.
	 */
1103
	IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114

	/*
	 *	Unlink dest from the service
	 */
	__ip_vs_unlink_dest(svc, dest, 1);

	write_unlock_bh(&__ip_vs_svc_lock);

	/*
	 *	Delete the destination
	 */
1115
	__ip_vs_del_dest(svc->net, dest);
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

	LeaveFunction(2);

	return 0;
}


/*
 *	Add a service into the service hash table
 */
static int
1127
ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1128
		  struct ip_vs_service **svc_p)
L
Linus Torvalds 已提交
1129 1130 1131
{
	int ret = 0;
	struct ip_vs_scheduler *sched = NULL;
1132
	struct ip_vs_pe *pe = NULL;
L
Linus Torvalds 已提交
1133
	struct ip_vs_service *svc = NULL;
1134
	struct netns_ipvs *ipvs = net_ipvs(net);
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140 1141

	/* increase the module use count */
	ip_vs_use_count_inc();

	/* Lookup the scheduler by 'u->sched_name' */
	sched = ip_vs_scheduler_get(u->sched_name);
	if (sched == NULL) {
1142
		pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
L
Linus Torvalds 已提交
1143
		ret = -ENOENT;
1144
		goto out_err;
L
Linus Torvalds 已提交
1145 1146
	}

1147
	if (u->pe_name && *u->pe_name) {
1148
		pe = ip_vs_pe_getbyname(u->pe_name);
1149 1150 1151 1152 1153 1154 1155 1156
		if (pe == NULL) {
			pr_info("persistence engine module ip_vs_pe_%s "
				"not found\n", u->pe_name);
			ret = -ENOENT;
			goto out_err;
		}
	}

1157
#ifdef CONFIG_IP_VS_IPV6
1158 1159 1160
	if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
		ret = -EINVAL;
		goto out_err;
1161 1162 1163
	}
#endif

1164
	svc = kzalloc(sizeof(struct ip_vs_service), GFP_KERNEL);
L
Linus Torvalds 已提交
1165
	if (svc == NULL) {
1166
		IP_VS_DBG(1, "%s(): no memory\n", __func__);
L
Linus Torvalds 已提交
1167 1168 1169
		ret = -ENOMEM;
		goto out_err;
	}
1170 1171 1172 1173 1174
	svc->stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
	if (!svc->stats.cpustats) {
		pr_err("%s() alloc_percpu failed\n", __func__);
		goto out_err;
	}
L
Linus Torvalds 已提交
1175 1176

	/* I'm the first user of the service */
1177
	atomic_set(&svc->usecnt, 0);
L
Linus Torvalds 已提交
1178 1179
	atomic_set(&svc->refcnt, 0);

1180
	svc->af = u->af;
L
Linus Torvalds 已提交
1181
	svc->protocol = u->protocol;
1182
	ip_vs_addr_copy(svc->af, &svc->addr, &u->addr);
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187
	svc->port = u->port;
	svc->fwmark = u->fwmark;
	svc->flags = u->flags;
	svc->timeout = u->timeout * HZ;
	svc->netmask = u->netmask;
1188
	svc->net = net;
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

	INIT_LIST_HEAD(&svc->destinations);
	rwlock_init(&svc->sched_lock);
	spin_lock_init(&svc->stats.lock);

	/* Bind the scheduler */
	ret = ip_vs_bind_scheduler(svc, sched);
	if (ret)
		goto out_err;
	sched = NULL;

1200 1201 1202 1203
	/* Bind the ct retriever */
	ip_vs_bind_pe(svc, pe);
	pe = NULL;

L
Linus Torvalds 已提交
1204 1205
	/* Update the virtual service counters */
	if (svc->port == FTPPORT)
1206
		atomic_inc(&ipvs->ftpsvc_counter);
L
Linus Torvalds 已提交
1207
	else if (svc->port == 0)
1208
		atomic_inc(&ipvs->nullsvc_counter);
L
Linus Torvalds 已提交
1209

1210
	ip_vs_start_estimator(net, &svc->stats);
1211 1212 1213

	/* Count only IPv4 services for old get/setsockopt interface */
	if (svc->af == AF_INET)
1214
		ipvs->num_services++;
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221

	/* Hash the service into the service table */
	write_lock_bh(&__ip_vs_svc_lock);
	ip_vs_svc_hash(svc);
	write_unlock_bh(&__ip_vs_svc_lock);

	*svc_p = svc;
1222 1223
	/* Now there is a service - full throttle */
	ipvs->enable = 1;
L
Linus Torvalds 已提交
1224 1225
	return 0;

1226

1227
 out_err:
L
Linus Torvalds 已提交
1228
	if (svc != NULL) {
1229
		ip_vs_unbind_scheduler(svc);
L
Linus Torvalds 已提交
1230 1231 1232 1233 1234
		if (svc->inc) {
			local_bh_disable();
			ip_vs_app_inc_put(svc->inc);
			local_bh_enable();
		}
1235 1236
		if (svc->stats.cpustats)
			free_percpu(svc->stats.cpustats);
L
Linus Torvalds 已提交
1237 1238 1239
		kfree(svc);
	}
	ip_vs_scheduler_put(sched);
1240
	ip_vs_pe_put(pe);
L
Linus Torvalds 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

	/* decrease the module use count */
	ip_vs_use_count_dec();

	return ret;
}


/*
 *	Edit a service and bind it with a new scheduler
 */
static int
1253
ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
L
Linus Torvalds 已提交
1254 1255
{
	struct ip_vs_scheduler *sched, *old_sched;
1256
	struct ip_vs_pe *pe = NULL, *old_pe = NULL;
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261 1262 1263
	int ret = 0;

	/*
	 * Lookup the scheduler, by 'u->sched_name'
	 */
	sched = ip_vs_scheduler_get(u->sched_name);
	if (sched == NULL) {
1264
		pr_info("Scheduler module ip_vs_%s not found\n", u->sched_name);
L
Linus Torvalds 已提交
1265 1266 1267 1268
		return -ENOENT;
	}
	old_sched = sched;

1269
	if (u->pe_name && *u->pe_name) {
1270
		pe = ip_vs_pe_getbyname(u->pe_name);
1271 1272 1273 1274 1275 1276 1277 1278 1279
		if (pe == NULL) {
			pr_info("persistence engine module ip_vs_pe_%s "
				"not found\n", u->pe_name);
			ret = -ENOENT;
			goto out;
		}
		old_pe = pe;
	}

1280
#ifdef CONFIG_IP_VS_IPV6
1281 1282 1283
	if (u->af == AF_INET6 && (u->netmask < 1 || u->netmask > 128)) {
		ret = -EINVAL;
		goto out;
1284 1285 1286
	}
#endif

L
Linus Torvalds 已提交
1287 1288 1289 1290 1291
	write_lock_bh(&__ip_vs_svc_lock);

	/*
	 * Wait until all other svc users go away.
	 */
1292
	IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

	/*
	 * Set the flags and timeout value
	 */
	svc->flags = u->flags | IP_VS_SVC_F_HASHED;
	svc->timeout = u->timeout * HZ;
	svc->netmask = u->netmask;

	old_sched = svc->scheduler;
	if (sched != old_sched) {
		/*
		 * Unbind the old scheduler
		 */
		if ((ret = ip_vs_unbind_scheduler(svc))) {
			old_sched = sched;
1308
			goto out_unlock;
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		}

		/*
		 * Bind the new scheduler
		 */
		if ((ret = ip_vs_bind_scheduler(svc, sched))) {
			/*
			 * If ip_vs_bind_scheduler fails, restore the old
			 * scheduler.
			 * The main reason of failure is out of memory.
			 *
			 * The question is if the old scheduler can be
			 * restored all the time. TODO: if it cannot be
			 * restored some time, we must delete the service,
			 * otherwise the system may crash.
			 */
			ip_vs_bind_scheduler(svc, old_sched);
			old_sched = sched;
1327
			goto out_unlock;
L
Linus Torvalds 已提交
1328 1329 1330
		}
	}

1331 1332 1333 1334 1335 1336
	old_pe = svc->pe;
	if (pe != old_pe) {
		ip_vs_unbind_pe(svc);
		ip_vs_bind_pe(svc, pe);
	}

1337
  out_unlock:
L
Linus Torvalds 已提交
1338
	write_unlock_bh(&__ip_vs_svc_lock);
1339
  out:
1340
	ip_vs_scheduler_put(old_sched);
1341
	ip_vs_pe_put(old_pe);
L
Linus Torvalds 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
	return ret;
}


/*
 *	Delete a service from the service list
 *	- The service must be unlinked, unlocked and not referenced!
 *	- We are called under _bh lock
 */
static void __ip_vs_del_service(struct ip_vs_service *svc)
{
	struct ip_vs_dest *dest, *nxt;
	struct ip_vs_scheduler *old_sched;
1355
	struct ip_vs_pe *old_pe;
1356
	struct netns_ipvs *ipvs = net_ipvs(svc->net);
1357 1358

	pr_info("%s: enter\n", __func__);
L
Linus Torvalds 已提交
1359

1360 1361
	/* Count only IPv4 services for old get/setsockopt interface */
	if (svc->af == AF_INET)
1362
		ipvs->num_services--;
1363

1364
	ip_vs_stop_estimator(svc->net, &svc->stats);
L
Linus Torvalds 已提交
1365 1366 1367 1368

	/* Unbind scheduler */
	old_sched = svc->scheduler;
	ip_vs_unbind_scheduler(svc);
1369
	ip_vs_scheduler_put(old_sched);
L
Linus Torvalds 已提交
1370

1371 1372 1373 1374 1375
	/* Unbind persistence engine */
	old_pe = svc->pe;
	ip_vs_unbind_pe(svc);
	ip_vs_pe_put(old_pe);

L
Linus Torvalds 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	/* Unbind app inc */
	if (svc->inc) {
		ip_vs_app_inc_put(svc->inc);
		svc->inc = NULL;
	}

	/*
	 *    Unlink the whole destination list
	 */
	list_for_each_entry_safe(dest, nxt, &svc->destinations, n_list) {
		__ip_vs_unlink_dest(svc, dest, 0);
1387
		__ip_vs_del_dest(svc->net, dest);
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393
	}

	/*
	 *    Update the virtual service counters
	 */
	if (svc->port == FTPPORT)
1394
		atomic_dec(&ipvs->ftpsvc_counter);
L
Linus Torvalds 已提交
1395
	else if (svc->port == 0)
1396
		atomic_dec(&ipvs->nullsvc_counter);
L
Linus Torvalds 已提交
1397 1398 1399 1400

	/*
	 *    Free the service if nobody refers to it
	 */
1401 1402 1403 1404 1405
	if (atomic_read(&svc->refcnt) == 0) {
		IP_VS_DBG_BUF(3, "Removing service %u/%s:%u usecnt=%d\n",
			      svc->fwmark,
			      IP_VS_DBG_ADDR(svc->af, &svc->addr),
			      ntohs(svc->port), atomic_read(&svc->usecnt));
1406
		free_percpu(svc->stats.cpustats);
L
Linus Torvalds 已提交
1407
		kfree(svc);
1408
	}
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414

	/* decrease the module use count */
	ip_vs_use_count_dec();
}

/*
1415
 * Unlink a service from list and try to delete it if its refcnt reached 0
L
Linus Torvalds 已提交
1416
 */
1417
static void ip_vs_unlink_service(struct ip_vs_service *svc)
L
Linus Torvalds 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
{
	/*
	 * Unhash it from the service table
	 */
	write_lock_bh(&__ip_vs_svc_lock);

	ip_vs_svc_unhash(svc);

	/*
	 * Wait until all the svc users go away.
	 */
1429
	IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
L
Linus Torvalds 已提交
1430 1431 1432 1433

	__ip_vs_del_service(svc);

	write_unlock_bh(&__ip_vs_svc_lock);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
}

/*
 *	Delete a service from the service list
 */
static int ip_vs_del_service(struct ip_vs_service *svc)
{
	if (svc == NULL)
		return -EEXIST;
	ip_vs_unlink_service(svc);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451

	return 0;
}


/*
 *	Flush all the virtual services
 */
1452
static int ip_vs_flush(struct net *net)
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457
{
	int idx;
	struct ip_vs_service *svc, *nxt;

	/*
1458
	 * Flush the service table hashed by <netns,protocol,addr,port>
L
Linus Torvalds 已提交
1459 1460
	 */
	for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
1461 1462 1463 1464
		list_for_each_entry_safe(svc, nxt, &ip_vs_svc_table[idx],
					 s_list) {
			if (net_eq(svc->net, net))
				ip_vs_unlink_service(svc);
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473
		}
	}

	/*
	 * Flush the service table hashed by fwmark
	 */
	for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry_safe(svc, nxt,
					 &ip_vs_svc_fwm_table[idx], f_list) {
1474 1475
			if (net_eq(svc->net, net))
				ip_vs_unlink_service(svc);
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480 1481
		}
	}

	return 0;
}

1482 1483 1484 1485
/*
 *	Delete service by {netns} in the service table.
 *	Called by __ip_vs_cleanup()
 */
1486
void ip_vs_service_net_cleanup(struct net *net)
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
{
	EnterFunction(2);
	/* Check for "full" addressed entries */
	mutex_lock(&__ip_vs_mutex);
	ip_vs_flush(net);
	mutex_unlock(&__ip_vs_mutex);
	LeaveFunction(2);
}
/*
 * Release dst hold by dst_cache
 */
static inline void
__ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)
{
	spin_lock_bh(&dest->dst_lock);
	if (dest->dst_cache && dest->dst_cache->dev == dev) {
		IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n",
			      dev->name,
			      IP_VS_DBG_ADDR(dest->af, &dest->addr),
			      ntohs(dest->port),
			      atomic_read(&dest->refcnt));
		ip_vs_dst_reset(dest);
	}
	spin_unlock_bh(&dest->dst_lock);

}
/*
 * Netdev event receiver
 * Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to
 * a device that is "unregister" it must be released.
 */
static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,
			    void *ptr)
{
	struct net_device *dev = ptr;
	struct net *net = dev_net(dev);
	struct ip_vs_service *svc;
	struct ip_vs_dest *dest;
	unsigned int idx;

	if (event != NETDEV_UNREGISTER)
		return NOTIFY_DONE;
	IP_VS_DBG(3, "%s() dev=%s\n", __func__, dev->name);
	EnterFunction(2);
	mutex_lock(&__ip_vs_mutex);
	for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
			if (net_eq(svc->net, net)) {
				list_for_each_entry(dest, &svc->destinations,
						    n_list) {
					__ip_vs_dev_reset(dest, dev);
				}
			}
		}

		list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
			if (net_eq(svc->net, net)) {
				list_for_each_entry(dest, &svc->destinations,
						    n_list) {
					__ip_vs_dev_reset(dest, dev);
				}
			}

		}
	}

	list_for_each_entry(dest, &net_ipvs(net)->dest_trash, n_list) {
		__ip_vs_dev_reset(dest, dev);
	}
	mutex_unlock(&__ip_vs_mutex);
	LeaveFunction(2);
	return NOTIFY_DONE;
}
L
Linus Torvalds 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576

/*
 *	Zero counters in a service or all services
 */
static int ip_vs_zero_service(struct ip_vs_service *svc)
{
	struct ip_vs_dest *dest;

	write_lock_bh(&__ip_vs_svc_lock);
	list_for_each_entry(dest, &svc->destinations, n_list) {
		ip_vs_zero_stats(&dest->stats);
	}
	ip_vs_zero_stats(&svc->stats);
	write_unlock_bh(&__ip_vs_svc_lock);
	return 0;
}

1577
static int ip_vs_zero_all(struct net *net)
L
Linus Torvalds 已提交
1578 1579 1580 1581 1582 1583
{
	int idx;
	struct ip_vs_service *svc;

	for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1584 1585
			if (net_eq(svc->net, net))
				ip_vs_zero_service(svc);
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590
		}
	}

	for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1591 1592
			if (net_eq(svc->net, net))
				ip_vs_zero_service(svc);
L
Linus Torvalds 已提交
1593 1594 1595
		}
	}

J
Julian Anastasov 已提交
1596
	ip_vs_zero_stats(&net_ipvs(net)->tot_stats);
L
Linus Torvalds 已提交
1597 1598 1599
	return 0;
}

1600
#ifdef CONFIG_SYSCTL
L
Linus Torvalds 已提交
1601
static int
1602
proc_do_defense_mode(ctl_table *table, int write,
L
Linus Torvalds 已提交
1603 1604
		     void __user *buffer, size_t *lenp, loff_t *ppos)
{
1605
	struct net *net = current->nsproxy->net_ns;
L
Linus Torvalds 已提交
1606 1607 1608 1609
	int *valp = table->data;
	int val = *valp;
	int rc;

1610
	rc = proc_dointvec(table, write, buffer, lenp, ppos);
L
Linus Torvalds 已提交
1611 1612 1613 1614 1615
	if (write && (*valp != val)) {
		if ((*valp < 0) || (*valp > 3)) {
			/* Restore the correct value */
			*valp = val;
		} else {
1616
			update_defense_level(net_ipvs(net));
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622
		}
	}
	return rc;
}

static int
1623
proc_do_sync_threshold(ctl_table *table, int write,
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632
		       void __user *buffer, size_t *lenp, loff_t *ppos)
{
	int *valp = table->data;
	int val[2];
	int rc;

	/* backup the value first */
	memcpy(val, valp, sizeof(val));

1633
	rc = proc_dointvec(table, write, buffer, lenp, ppos);
L
Linus Torvalds 已提交
1634 1635 1636 1637 1638 1639 1640
	if (write && (valp[0] < 0 || valp[1] < 0 || valp[0] >= valp[1])) {
		/* Restore the correct value */
		memcpy(valp, val, sizeof(val));
	}
	return rc;
}

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
static int
proc_do_sync_mode(ctl_table *table, int write,
		     void __user *buffer, size_t *lenp, loff_t *ppos)
{
	int *valp = table->data;
	int val = *valp;
	int rc;

	rc = proc_dointvec(table, write, buffer, lenp, ppos);
	if (write && (*valp != val)) {
		if ((*valp < 0) || (*valp > 1)) {
			/* Restore the correct value */
			*valp = val;
		} else {
1655 1656
			struct net *net = current->nsproxy->net_ns;
			ip_vs_sync_switch_mode(net, val);
1657 1658 1659 1660
		}
	}
	return rc;
}
L
Linus Torvalds 已提交
1661 1662 1663

/*
 *	IPVS sysctl table (under the /proc/sys/net/ipv4/vs/)
1664
 *	Do not change order or insert new entries without
1665
 *	align with netns init in ip_vs_control_net_init()
L
Linus Torvalds 已提交
1666 1667 1668 1669 1670 1671 1672
 */

static struct ctl_table vs_vars[] = {
	{
		.procname	= "amemthresh",
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1673
		.proc_handler	= proc_dointvec,
L
Linus Torvalds 已提交
1674 1675 1676 1677 1678
	},
	{
		.procname	= "am_droprate",
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1679
		.proc_handler	= proc_dointvec,
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684
	},
	{
		.procname	= "drop_entry",
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1685
		.proc_handler	= proc_do_defense_mode,
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690
	},
	{
		.procname	= "drop_packet",
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1691
		.proc_handler	= proc_do_defense_mode,
L
Linus Torvalds 已提交
1692
	},
1693 1694 1695 1696 1697 1698 1699 1700
#ifdef CONFIG_IP_VS_NFCT
	{
		.procname	= "conntrack",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
#endif
L
Linus Torvalds 已提交
1701 1702 1703 1704
	{
		.procname	= "secure_tcp",
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1705
		.proc_handler	= proc_do_defense_mode,
L
Linus Torvalds 已提交
1706
	},
1707 1708 1709 1710 1711 1712
	{
		.procname	= "snat_reroute",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
1713 1714 1715 1716 1717 1718
	{
		.procname	= "sync_version",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_do_sync_mode,
	},
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	{
		.procname	= "cache_bypass",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
	{
		.procname	= "expire_nodest_conn",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
	{
		.procname	= "expire_quiescent_template",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
	{
		.procname	= "sync_threshold",
		.maxlen		=
			sizeof(((struct netns_ipvs *)0)->sysctl_sync_threshold),
		.mode		= 0644,
		.proc_handler	= proc_do_sync_threshold,
	},
	{
		.procname	= "nat_icmp_send",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
#ifdef CONFIG_IP_VS_DEBUG
	{
		.procname	= "debug_level",
		.data		= &sysctl_ip_vs_debug_level,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
	},
#endif
L
Linus Torvalds 已提交
1759 1760 1761 1762 1763 1764
#if 0
	{
		.procname	= "timeout_established",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_ESTABLISHED],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1765
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1766 1767 1768 1769 1770 1771
	},
	{
		.procname	= "timeout_synsent",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_SYN_SENT],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1772
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1773 1774 1775 1776 1777 1778
	},
	{
		.procname	= "timeout_synrecv",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_SYN_RECV],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1779
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1780 1781 1782 1783 1784 1785
	},
	{
		.procname	= "timeout_finwait",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_FIN_WAIT],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1786
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1787 1788 1789 1790 1791 1792
	},
	{
		.procname	= "timeout_timewait",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_TIME_WAIT],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1793
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1794 1795 1796 1797 1798 1799
	},
	{
		.procname	= "timeout_close",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1800
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1801 1802 1803 1804 1805 1806
	},
	{
		.procname	= "timeout_closewait",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_CLOSE_WAIT],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1807
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1808 1809 1810 1811 1812 1813
	},
	{
		.procname	= "timeout_lastack",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_LAST_ACK],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1814
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1815 1816 1817 1818 1819 1820
	},
	{
		.procname	= "timeout_listen",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_LISTEN],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1821
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827
	},
	{
		.procname	= "timeout_synack",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_SYNACK],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1828
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833 1834
	},
	{
		.procname	= "timeout_udp",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_UDP],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1835
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841
	},
	{
		.procname	= "timeout_icmp",
		.data	= &vs_timeout_table_dos.timeout[IP_VS_S_ICMP],
		.maxlen		= sizeof(int),
		.mode		= 0644,
A
Alexey Dobriyan 已提交
1842
		.proc_handler	= proc_dointvec_jiffies,
L
Linus Torvalds 已提交
1843 1844
	},
#endif
1845
	{ }
L
Linus Torvalds 已提交
1846 1847
};

S
Sven Wegener 已提交
1848
const struct ctl_path net_vs_ctl_path[] = {
1849 1850
	{ .procname = "net", },
	{ .procname = "ipv4", },
1851 1852
	{ .procname = "vs", },
	{ }
L
Linus Torvalds 已提交
1853
};
1854
EXPORT_SYMBOL_GPL(net_vs_ctl_path);
1855
#endif
L
Linus Torvalds 已提交
1856 1857 1858 1859

#ifdef CONFIG_PROC_FS

struct ip_vs_iter {
1860
	struct seq_net_private p;  /* Do not move this, netns depends upon it*/
L
Linus Torvalds 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
	struct list_head *table;
	int bucket;
};

/*
 *	Write the contents of the VS rule table to a PROCfs file.
 *	(It is kept just for backward compatibility)
 */
static inline const char *ip_vs_fwd_name(unsigned flags)
{
	switch (flags & IP_VS_CONN_F_FWD_MASK) {
	case IP_VS_CONN_F_LOCALNODE:
		return "Local";
	case IP_VS_CONN_F_TUNNEL:
		return "Tunnel";
	case IP_VS_CONN_F_DROUTE:
		return "Route";
	default:
		return "Masq";
	}
}


/* Get the Nth entry in the two lists */
static struct ip_vs_service *ip_vs_info_array(struct seq_file *seq, loff_t pos)
{
1887
	struct net *net = seq_file_net(seq);
L
Linus Torvalds 已提交
1888 1889 1890 1891 1892 1893 1894
	struct ip_vs_iter *iter = seq->private;
	int idx;
	struct ip_vs_service *svc;

	/* look in hash by protocol */
	for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
1895
			if (net_eq(svc->net, net) && pos-- == 0) {
L
Linus Torvalds 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
				iter->table = ip_vs_svc_table;
				iter->bucket = idx;
				return svc;
			}
		}
	}

	/* keep looking in fwmark */
	for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
1906
			if (net_eq(svc->net, net) && pos-- == 0) {
L
Linus Torvalds 已提交
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
				iter->table = ip_vs_svc_fwm_table;
				iter->bucket = idx;
				return svc;
			}
		}
	}

	return NULL;
}

static void *ip_vs_info_seq_start(struct seq_file *seq, loff_t *pos)
1918
__acquires(__ip_vs_svc_lock)
L
Linus Torvalds 已提交
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
{

	read_lock_bh(&__ip_vs_svc_lock);
	return *pos ? ip_vs_info_array(seq, *pos - 1) : SEQ_START_TOKEN;
}


static void *ip_vs_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct list_head *e;
	struct ip_vs_iter *iter;
	struct ip_vs_service *svc;

	++*pos;
	if (v == SEQ_START_TOKEN)
		return ip_vs_info_array(seq,0);

	svc = v;
	iter = seq->private;

	if (iter->table == ip_vs_svc_table) {
		/* next service in table hashed by protocol */
		if ((e = svc->s_list.next) != &ip_vs_svc_table[iter->bucket])
			return list_entry(e, struct ip_vs_service, s_list);


		while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
			list_for_each_entry(svc,&ip_vs_svc_table[iter->bucket],
					    s_list) {
				return svc;
			}
		}

		iter->table = ip_vs_svc_fwm_table;
		iter->bucket = -1;
		goto scan_fwmark;
	}

	/* next service in hashed by fwmark */
	if ((e = svc->f_list.next) != &ip_vs_svc_fwm_table[iter->bucket])
		return list_entry(e, struct ip_vs_service, f_list);

 scan_fwmark:
	while (++iter->bucket < IP_VS_SVC_TAB_SIZE) {
		list_for_each_entry(svc, &ip_vs_svc_fwm_table[iter->bucket],
				    f_list)
			return svc;
	}

	return NULL;
}

static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)
1972
__releases(__ip_vs_svc_lock)
L
Linus Torvalds 已提交
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
{
	read_unlock_bh(&__ip_vs_svc_lock);
}


static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
{
	if (v == SEQ_START_TOKEN) {
		seq_printf(seq,
			"IP Virtual Server version %d.%d.%d (size=%d)\n",
1983
			NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
L
Linus Torvalds 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992
		seq_puts(seq,
			 "Prot LocalAddress:Port Scheduler Flags\n");
		seq_puts(seq,
			 "  -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n");
	} else {
		const struct ip_vs_service *svc = v;
		const struct ip_vs_iter *iter = seq->private;
		const struct ip_vs_dest *dest;

1993 1994 1995
		if (iter->table == ip_vs_svc_table) {
#ifdef CONFIG_IP_VS_IPV6
			if (svc->af == AF_INET6)
H
Harvey Harrison 已提交
1996
				seq_printf(seq, "%s  [%pI6]:%04X %s ",
1997
					   ip_vs_proto_name(svc->protocol),
1998
					   &svc->addr.in6,
1999 2000 2001 2002
					   ntohs(svc->port),
					   svc->scheduler->name);
			else
#endif
N
Nick Chalk 已提交
2003
				seq_printf(seq, "%s  %08X:%04X %s %s ",
2004 2005 2006
					   ip_vs_proto_name(svc->protocol),
					   ntohl(svc->addr.ip),
					   ntohs(svc->port),
N
Nick Chalk 已提交
2007 2008
					   svc->scheduler->name,
					   (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2009
		} else {
N
Nick Chalk 已提交
2010 2011 2012
			seq_printf(seq, "FWM  %08X %s %s",
				   svc->fwmark, svc->scheduler->name,
				   (svc->flags & IP_VS_SVC_F_ONEPACKET)?"ops ":"");
2013
		}
L
Linus Torvalds 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022

		if (svc->flags & IP_VS_SVC_F_PERSISTENT)
			seq_printf(seq, "persistent %d %08X\n",
				svc->timeout,
				ntohl(svc->netmask));
		else
			seq_putc(seq, '\n');

		list_for_each_entry(dest, &svc->destinations, n_list) {
2023 2024 2025
#ifdef CONFIG_IP_VS_IPV6
			if (dest->af == AF_INET6)
				seq_printf(seq,
H
Harvey Harrison 已提交
2026
					   "  -> [%pI6]:%04X"
2027
					   "      %-7s %-6d %-10d %-10d\n",
2028
					   &dest->addr.in6,
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
					   ntohs(dest->port),
					   ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
					   atomic_read(&dest->weight),
					   atomic_read(&dest->activeconns),
					   atomic_read(&dest->inactconns));
			else
#endif
				seq_printf(seq,
					   "  -> %08X:%04X      "
					   "%-7s %-6d %-10d %-10d\n",
					   ntohl(dest->addr.ip),
					   ntohs(dest->port),
					   ip_vs_fwd_name(atomic_read(&dest->conn_flags)),
					   atomic_read(&dest->weight),
					   atomic_read(&dest->activeconns),
					   atomic_read(&dest->inactconns));

L
Linus Torvalds 已提交
2046 2047 2048 2049 2050
		}
	}
	return 0;
}

2051
static const struct seq_operations ip_vs_info_seq_ops = {
L
Linus Torvalds 已提交
2052 2053 2054 2055 2056 2057 2058 2059
	.start = ip_vs_info_seq_start,
	.next  = ip_vs_info_seq_next,
	.stop  = ip_vs_info_seq_stop,
	.show  = ip_vs_info_seq_show,
};

static int ip_vs_info_open(struct inode *inode, struct file *file)
{
2060
	return seq_open_net(inode, file, &ip_vs_info_seq_ops,
2061
			sizeof(struct ip_vs_iter));
L
Linus Torvalds 已提交
2062 2063
}

2064
static const struct file_operations ip_vs_info_fops = {
L
Linus Torvalds 已提交
2065 2066 2067 2068
	.owner	 = THIS_MODULE,
	.open    = ip_vs_info_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
2069
	.release = seq_release_net,
L
Linus Torvalds 已提交
2070 2071 2072 2073
};

static int ip_vs_stats_show(struct seq_file *seq, void *v)
{
2074
	struct net *net = seq_file_single_net(seq);
2075
	struct ip_vs_stats_user show;
L
Linus Torvalds 已提交
2076 2077 2078 2079 2080 2081 2082

/*               01234567 01234567 01234567 0123456701234567 0123456701234567 */
	seq_puts(seq,
		 "   Total Incoming Outgoing         Incoming         Outgoing\n");
	seq_printf(seq,
		   "   Conns  Packets  Packets            Bytes            Bytes\n");

2083 2084 2085 2086 2087
	ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
	seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
		   show.inpkts, show.outpkts,
		   (unsigned long long) show.inbytes,
		   (unsigned long long) show.outbytes);
L
Linus Torvalds 已提交
2088 2089 2090 2091

/*                 01234567 01234567 01234567 0123456701234567 0123456701234567 */
	seq_puts(seq,
		   " Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s\n");
2092 2093 2094
	seq_printf(seq, "%8X %8X %8X %16X %16X\n",
			show.cps, show.inpps, show.outpps,
			show.inbps, show.outbps);
L
Linus Torvalds 已提交
2095 2096 2097 2098 2099 2100

	return 0;
}

static int ip_vs_stats_seq_open(struct inode *inode, struct file *file)
{
2101
	return single_open_net(inode, file, ip_vs_stats_show);
L
Linus Torvalds 已提交
2102 2103
}

2104
static const struct file_operations ip_vs_stats_fops = {
L
Linus Torvalds 已提交
2105 2106 2107 2108
	.owner = THIS_MODULE,
	.open = ip_vs_stats_seq_open,
	.read = seq_read,
	.llseek = seq_lseek,
2109
	.release = single_release_net,
L
Linus Torvalds 已提交
2110 2111
};

2112 2113 2114
static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
{
	struct net *net = seq_file_single_net(seq);
J
Julian Anastasov 已提交
2115 2116
	struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
	struct ip_vs_cpu_stats *cpustats = tot_stats->cpustats;
J
Julian Anastasov 已提交
2117
	struct ip_vs_stats_user rates;
2118 2119 2120 2121 2122 2123 2124 2125 2126
	int i;

/*               01234567 01234567 01234567 0123456701234567 0123456701234567 */
	seq_puts(seq,
		 "       Total Incoming Outgoing         Incoming         Outgoing\n");
	seq_printf(seq,
		   "CPU    Conns  Packets  Packets            Bytes            Bytes\n");

	for_each_possible_cpu(i) {
J
Julian Anastasov 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
		struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
		unsigned int start;
		__u64 inbytes, outbytes;

		do {
			start = u64_stats_fetch_begin_bh(&u->syncp);
			inbytes = u->ustats.inbytes;
			outbytes = u->ustats.outbytes;
		} while (u64_stats_fetch_retry_bh(&u->syncp, start));

2137
		seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n",
J
Julian Anastasov 已提交
2138 2139 2140
			   i, u->ustats.conns, u->ustats.inpkts,
			   u->ustats.outpkts, (__u64)inbytes,
			   (__u64)outbytes);
2141 2142 2143
	}

	spin_lock_bh(&tot_stats->lock);
J
Julian Anastasov 已提交
2144

2145 2146 2147 2148 2149 2150
	seq_printf(seq, "  ~ %8X %8X %8X %16LX %16LX\n\n",
		   tot_stats->ustats.conns, tot_stats->ustats.inpkts,
		   tot_stats->ustats.outpkts,
		   (unsigned long long) tot_stats->ustats.inbytes,
		   (unsigned long long) tot_stats->ustats.outbytes);

J
Julian Anastasov 已提交
2151 2152 2153 2154
	ip_vs_read_estimator(&rates, tot_stats);

	spin_unlock_bh(&tot_stats->lock);

2155 2156 2157 2158
/*                 01234567 01234567 01234567 0123456701234567 0123456701234567 */
	seq_puts(seq,
		   "     Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s\n");
	seq_printf(seq, "    %8X %8X %8X %16X %16X\n",
J
Julian Anastasov 已提交
2159 2160 2161 2162 2163
			rates.cps,
			rates.inpps,
			rates.outpps,
			rates.inbps,
			rates.outbps);
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177

	return 0;
}

static int ip_vs_stats_percpu_seq_open(struct inode *inode, struct file *file)
{
	return single_open_net(inode, file, ip_vs_stats_percpu_show);
}

static const struct file_operations ip_vs_stats_percpu_fops = {
	.owner = THIS_MODULE,
	.open = ip_vs_stats_percpu_seq_open,
	.read = seq_read,
	.llseek = seq_lseek,
2178
	.release = single_release_net,
2179
};
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184
#endif

/*
 *	Set timeout values for tcp tcpfin udp in the timeout_table.
 */
2185
static int ip_vs_set_timeout(struct net *net, struct ip_vs_timeout_user *u)
L
Linus Torvalds 已提交
2186
{
2187
#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2188
	struct ip_vs_proto_data *pd;
2189
#endif
2190

L
Linus Torvalds 已提交
2191 2192 2193 2194 2195 2196 2197
	IP_VS_DBG(2, "Setting timeout tcp:%d tcpfin:%d udp:%d\n",
		  u->tcp_timeout,
		  u->tcp_fin_timeout,
		  u->udp_timeout);

#ifdef CONFIG_IP_VS_PROTO_TCP
	if (u->tcp_timeout) {
2198 2199
		pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
		pd->timeout_table[IP_VS_TCP_S_ESTABLISHED]
L
Linus Torvalds 已提交
2200 2201 2202 2203
			= u->tcp_timeout * HZ;
	}

	if (u->tcp_fin_timeout) {
2204 2205
		pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
		pd->timeout_table[IP_VS_TCP_S_FIN_WAIT]
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211
			= u->tcp_fin_timeout * HZ;
	}
#endif

#ifdef CONFIG_IP_VS_PROTO_UDP
	if (u->udp_timeout) {
2212 2213
		pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
		pd->timeout_table[IP_VS_UDP_S_NORMAL]
L
Linus Torvalds 已提交
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
			= u->udp_timeout * HZ;
	}
#endif
	return 0;
}


#define SET_CMDID(cmd)		(cmd - IP_VS_BASE_CTL)
#define SERVICE_ARG_LEN		(sizeof(struct ip_vs_service_user))
#define SVCDEST_ARG_LEN		(sizeof(struct ip_vs_service_user) +	\
				 sizeof(struct ip_vs_dest_user))
#define TIMEOUT_ARG_LEN		(sizeof(struct ip_vs_timeout_user))
#define DAEMON_ARG_LEN		(sizeof(struct ip_vs_daemon_user))
#define MAX_ARG_LEN		SVCDEST_ARG_LEN

2229
static const unsigned char set_arglen[SET_CMDID(IP_VS_SO_SET_MAX)+1] = {
L
Linus Torvalds 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	[SET_CMDID(IP_VS_SO_SET_ADD)]		= SERVICE_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_EDIT)]		= SERVICE_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_DEL)]		= SERVICE_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_FLUSH)]		= 0,
	[SET_CMDID(IP_VS_SO_SET_ADDDEST)]	= SVCDEST_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_DELDEST)]	= SVCDEST_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_EDITDEST)]	= SVCDEST_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_TIMEOUT)]	= TIMEOUT_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_STARTDAEMON)]	= DAEMON_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_STOPDAEMON)]	= DAEMON_ARG_LEN,
	[SET_CMDID(IP_VS_SO_SET_ZERO)]		= SERVICE_ARG_LEN,
};

2243 2244 2245
static void ip_vs_copy_usvc_compat(struct ip_vs_service_user_kern *usvc,
				  struct ip_vs_service_user *usvc_compat)
{
2246 2247
	memset(usvc, 0, sizeof(*usvc));

2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
	usvc->af		= AF_INET;
	usvc->protocol		= usvc_compat->protocol;
	usvc->addr.ip		= usvc_compat->addr;
	usvc->port		= usvc_compat->port;
	usvc->fwmark		= usvc_compat->fwmark;

	/* Deep copy of sched_name is not needed here */
	usvc->sched_name	= usvc_compat->sched_name;

	usvc->flags		= usvc_compat->flags;
	usvc->timeout		= usvc_compat->timeout;
	usvc->netmask		= usvc_compat->netmask;
}

static void ip_vs_copy_udest_compat(struct ip_vs_dest_user_kern *udest,
				   struct ip_vs_dest_user *udest_compat)
{
2265 2266
	memset(udest, 0, sizeof(*udest));

2267 2268 2269 2270 2271 2272 2273 2274
	udest->addr.ip		= udest_compat->addr;
	udest->port		= udest_compat->port;
	udest->conn_flags	= udest_compat->conn_flags;
	udest->weight		= udest_compat->weight;
	udest->u_threshold	= udest_compat->u_threshold;
	udest->l_threshold	= udest_compat->l_threshold;
}

L
Linus Torvalds 已提交
2275 2276 2277
static int
do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
{
2278
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
2279 2280
	int ret;
	unsigned char arg[MAX_ARG_LEN];
2281 2282
	struct ip_vs_service_user *usvc_compat;
	struct ip_vs_service_user_kern usvc;
L
Linus Torvalds 已提交
2283
	struct ip_vs_service *svc;
2284 2285
	struct ip_vs_dest_user *udest_compat;
	struct ip_vs_dest_user_kern udest;
L
Linus Torvalds 已提交
2286 2287 2288 2289

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

2290 2291 2292 2293
	if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_SET_MAX)
		return -EINVAL;
	if (len < 0 || len >  MAX_ARG_LEN)
		return -EINVAL;
L
Linus Torvalds 已提交
2294
	if (len != set_arglen[SET_CMDID(cmd)]) {
2295 2296
		pr_err("set_ctl: len %u != %u\n",
		       len, set_arglen[SET_CMDID(cmd)]);
L
Linus Torvalds 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305
		return -EINVAL;
	}

	if (copy_from_user(arg, user, len) != 0)
		return -EFAULT;

	/* increase the module use count */
	ip_vs_use_count_inc();

2306
	if (mutex_lock_interruptible(&__ip_vs_mutex)) {
L
Linus Torvalds 已提交
2307 2308 2309 2310 2311 2312
		ret = -ERESTARTSYS;
		goto out_dec;
	}

	if (cmd == IP_VS_SO_SET_FLUSH) {
		/* Flush the virtual service */
2313
		ret = ip_vs_flush(net);
L
Linus Torvalds 已提交
2314 2315 2316
		goto out_unlock;
	} else if (cmd == IP_VS_SO_SET_TIMEOUT) {
		/* Set timeout values for (tcp tcpfin udp) */
2317
		ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
L
Linus Torvalds 已提交
2318 2319 2320
		goto out_unlock;
	} else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
		struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2321 2322
		ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
					dm->syncid);
L
Linus Torvalds 已提交
2323 2324 2325
		goto out_unlock;
	} else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
		struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2326
		ret = stop_sync_thread(net, dm->state);
L
Linus Torvalds 已提交
2327 2328 2329
		goto out_unlock;
	}

2330 2331 2332 2333 2334 2335 2336
	usvc_compat = (struct ip_vs_service_user *)arg;
	udest_compat = (struct ip_vs_dest_user *)(usvc_compat + 1);

	/* We only use the new structs internally, so copy userspace compat
	 * structs to extended internal versions */
	ip_vs_copy_usvc_compat(&usvc, usvc_compat);
	ip_vs_copy_udest_compat(&udest, udest_compat);
L
Linus Torvalds 已提交
2337 2338 2339

	if (cmd == IP_VS_SO_SET_ZERO) {
		/* if no service address is set, zero counters in all */
2340
		if (!usvc.fwmark && !usvc.addr.ip && !usvc.port) {
2341
			ret = ip_vs_zero_all(net);
L
Linus Torvalds 已提交
2342 2343 2344 2345
			goto out_unlock;
		}
	}

2346 2347 2348
	/* Check for valid protocol: TCP or UDP or SCTP, even for fwmark!=0 */
	if (usvc.protocol != IPPROTO_TCP && usvc.protocol != IPPROTO_UDP &&
	    usvc.protocol != IPPROTO_SCTP) {
2349 2350 2351
		pr_err("set_ctl: invalid protocol: %d %pI4:%d %s\n",
		       usvc.protocol, &usvc.addr.ip,
		       ntohs(usvc.port), usvc.sched_name);
L
Linus Torvalds 已提交
2352 2353 2354 2355 2356
		ret = -EFAULT;
		goto out_unlock;
	}

	/* Lookup the exact service by <protocol, addr, port> or fwmark */
2357
	if (usvc.fwmark == 0)
2358
		svc = __ip_vs_service_find(net, usvc.af, usvc.protocol,
2359
					   &usvc.addr, usvc.port);
L
Linus Torvalds 已提交
2360
	else
2361
		svc = __ip_vs_svc_fwm_find(net, usvc.af, usvc.fwmark);
L
Linus Torvalds 已提交
2362 2363

	if (cmd != IP_VS_SO_SET_ADD
2364
	    && (svc == NULL || svc->protocol != usvc.protocol)) {
L
Linus Torvalds 已提交
2365
		ret = -ESRCH;
2366
		goto out_unlock;
L
Linus Torvalds 已提交
2367 2368 2369 2370 2371 2372 2373
	}

	switch (cmd) {
	case IP_VS_SO_SET_ADD:
		if (svc != NULL)
			ret = -EEXIST;
		else
2374
			ret = ip_vs_add_service(net, &usvc, &svc);
L
Linus Torvalds 已提交
2375 2376
		break;
	case IP_VS_SO_SET_EDIT:
2377
		ret = ip_vs_edit_service(svc, &usvc);
L
Linus Torvalds 已提交
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
		break;
	case IP_VS_SO_SET_DEL:
		ret = ip_vs_del_service(svc);
		if (!ret)
			goto out_unlock;
		break;
	case IP_VS_SO_SET_ZERO:
		ret = ip_vs_zero_service(svc);
		break;
	case IP_VS_SO_SET_ADDDEST:
2388
		ret = ip_vs_add_dest(svc, &udest);
L
Linus Torvalds 已提交
2389 2390
		break;
	case IP_VS_SO_SET_EDITDEST:
2391
		ret = ip_vs_edit_dest(svc, &udest);
L
Linus Torvalds 已提交
2392 2393
		break;
	case IP_VS_SO_SET_DELDEST:
2394
		ret = ip_vs_del_dest(svc, &udest);
L
Linus Torvalds 已提交
2395 2396 2397 2398 2399 2400
		break;
	default:
		ret = -EINVAL;
	}

  out_unlock:
2401
	mutex_unlock(&__ip_vs_mutex);
L
Linus Torvalds 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
  out_dec:
	/* decrease the module use count */
	ip_vs_use_count_dec();

	return ret;
}


static void
ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
{
	dst->protocol = src->protocol;
2414
	dst->addr = src->addr.ip;
L
Linus Torvalds 已提交
2415 2416
	dst->port = src->port;
	dst->fwmark = src->fwmark;
P
pageexec 已提交
2417
	strlcpy(dst->sched_name, src->scheduler->name, sizeof(dst->sched_name));
L
Linus Torvalds 已提交
2418 2419 2420 2421 2422 2423 2424 2425
	dst->flags = src->flags;
	dst->timeout = src->timeout / HZ;
	dst->netmask = src->netmask;
	dst->num_dests = src->num_dests;
	ip_vs_copy_stats(&dst->stats, &src->stats);
}

static inline int
2426 2427
__ip_vs_get_service_entries(struct net *net,
			    const struct ip_vs_get_services *get,
L
Linus Torvalds 已提交
2428 2429 2430 2431 2432 2433 2434 2435 2436
			    struct ip_vs_get_services __user *uptr)
{
	int idx, count=0;
	struct ip_vs_service *svc;
	struct ip_vs_service_entry entry;
	int ret = 0;

	for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_table[idx], s_list) {
2437
			/* Only expose IPv4 entries to old interface */
2438
			if (svc->af != AF_INET || !net_eq(svc->net, net))
2439 2440
				continue;

L
Linus Torvalds 已提交
2441 2442
			if (count >= get->num_services)
				goto out;
P
pageexec 已提交
2443
			memset(&entry, 0, sizeof(entry));
L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
			ip_vs_copy_service(&entry, svc);
			if (copy_to_user(&uptr->entrytable[count],
					 &entry, sizeof(entry))) {
				ret = -EFAULT;
				goto out;
			}
			count++;
		}
	}

	for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
		list_for_each_entry(svc, &ip_vs_svc_fwm_table[idx], f_list) {
2456
			/* Only expose IPv4 entries to old interface */
2457
			if (svc->af != AF_INET || !net_eq(svc->net, net))
2458 2459
				continue;

L
Linus Torvalds 已提交
2460 2461
			if (count >= get->num_services)
				goto out;
P
pageexec 已提交
2462
			memset(&entry, 0, sizeof(entry));
L
Linus Torvalds 已提交
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
			ip_vs_copy_service(&entry, svc);
			if (copy_to_user(&uptr->entrytable[count],
					 &entry, sizeof(entry))) {
				ret = -EFAULT;
				goto out;
			}
			count++;
		}
	}
  out:
	return ret;
}

static inline int
2477
__ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
L
Linus Torvalds 已提交
2478 2479 2480
			 struct ip_vs_get_dests __user *uptr)
{
	struct ip_vs_service *svc;
2481
	union nf_inet_addr addr = { .ip = get->addr };
L
Linus Torvalds 已提交
2482 2483 2484
	int ret = 0;

	if (get->fwmark)
2485
		svc = __ip_vs_svc_fwm_find(net, AF_INET, get->fwmark);
L
Linus Torvalds 已提交
2486
	else
2487
		svc = __ip_vs_service_find(net, AF_INET, get->protocol, &addr,
2488
					   get->port);
2489

L
Linus Torvalds 已提交
2490 2491 2492 2493 2494 2495 2496 2497 2498
	if (svc) {
		int count = 0;
		struct ip_vs_dest *dest;
		struct ip_vs_dest_entry entry;

		list_for_each_entry(dest, &svc->destinations, n_list) {
			if (count >= get->num_dests)
				break;

2499
			entry.addr = dest->addr.ip;
L
Linus Torvalds 已提交
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
			entry.port = dest->port;
			entry.conn_flags = atomic_read(&dest->conn_flags);
			entry.weight = atomic_read(&dest->weight);
			entry.u_threshold = dest->u_threshold;
			entry.l_threshold = dest->l_threshold;
			entry.activeconns = atomic_read(&dest->activeconns);
			entry.inactconns = atomic_read(&dest->inactconns);
			entry.persistconns = atomic_read(&dest->persistconns);
			ip_vs_copy_stats(&entry.stats, &dest->stats);
			if (copy_to_user(&uptr->entrytable[count],
					 &entry, sizeof(entry))) {
				ret = -EFAULT;
				break;
			}
			count++;
		}
	} else
		ret = -ESRCH;
	return ret;
}

static inline void
2522
__ip_vs_get_timeouts(struct net *net, struct ip_vs_timeout_user *u)
L
Linus Torvalds 已提交
2523
{
2524
#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP)
2525
	struct ip_vs_proto_data *pd;
2526
#endif
2527

L
Linus Torvalds 已提交
2528
#ifdef CONFIG_IP_VS_PROTO_TCP
2529 2530 2531
	pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
	u->tcp_timeout = pd->timeout_table[IP_VS_TCP_S_ESTABLISHED] / HZ;
	u->tcp_fin_timeout = pd->timeout_table[IP_VS_TCP_S_FIN_WAIT] / HZ;
L
Linus Torvalds 已提交
2532 2533
#endif
#ifdef CONFIG_IP_VS_PROTO_UDP
2534
	pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
L
Linus Torvalds 已提交
2535
	u->udp_timeout =
2536
			pd->timeout_table[IP_VS_UDP_S_NORMAL] / HZ;
L
Linus Torvalds 已提交
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
#endif
}


#define GET_CMDID(cmd)		(cmd - IP_VS_BASE_CTL)
#define GET_INFO_ARG_LEN	(sizeof(struct ip_vs_getinfo))
#define GET_SERVICES_ARG_LEN	(sizeof(struct ip_vs_get_services))
#define GET_SERVICE_ARG_LEN	(sizeof(struct ip_vs_service_entry))
#define GET_DESTS_ARG_LEN	(sizeof(struct ip_vs_get_dests))
#define GET_TIMEOUT_ARG_LEN	(sizeof(struct ip_vs_timeout_user))
#define GET_DAEMON_ARG_LEN	(sizeof(struct ip_vs_daemon_user) * 2)

2549
static const unsigned char get_arglen[GET_CMDID(IP_VS_SO_GET_MAX)+1] = {
L
Linus Torvalds 已提交
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
	[GET_CMDID(IP_VS_SO_GET_VERSION)]	= 64,
	[GET_CMDID(IP_VS_SO_GET_INFO)]		= GET_INFO_ARG_LEN,
	[GET_CMDID(IP_VS_SO_GET_SERVICES)]	= GET_SERVICES_ARG_LEN,
	[GET_CMDID(IP_VS_SO_GET_SERVICE)]	= GET_SERVICE_ARG_LEN,
	[GET_CMDID(IP_VS_SO_GET_DESTS)]		= GET_DESTS_ARG_LEN,
	[GET_CMDID(IP_VS_SO_GET_TIMEOUT)]	= GET_TIMEOUT_ARG_LEN,
	[GET_CMDID(IP_VS_SO_GET_DAEMON)]	= GET_DAEMON_ARG_LEN,
};

static int
do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
{
	unsigned char arg[128];
	int ret = 0;
2564
	unsigned int copylen;
2565
	struct net *net = sock_net(sk);
2566
	struct netns_ipvs *ipvs = net_ipvs(net);
L
Linus Torvalds 已提交
2567

2568
	BUG_ON(!net);
L
Linus Torvalds 已提交
2569 2570 2571
	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

2572 2573 2574
	if (cmd < IP_VS_BASE_CTL || cmd > IP_VS_SO_GET_MAX)
		return -EINVAL;

L
Linus Torvalds 已提交
2575
	if (*len < get_arglen[GET_CMDID(cmd)]) {
2576 2577
		pr_err("get_ctl: len %u < %u\n",
		       *len, get_arglen[GET_CMDID(cmd)]);
L
Linus Torvalds 已提交
2578 2579 2580
		return -EINVAL;
	}

2581 2582 2583 2584 2585
	copylen = get_arglen[GET_CMDID(cmd)];
	if (copylen > 128)
		return -EINVAL;

	if (copy_from_user(arg, user, copylen) != 0)
L
Linus Torvalds 已提交
2586 2587
		return -EFAULT;

2588
	if (mutex_lock_interruptible(&__ip_vs_mutex))
L
Linus Torvalds 已提交
2589 2590 2591 2592 2593 2594 2595 2596
		return -ERESTARTSYS;

	switch (cmd) {
	case IP_VS_SO_GET_VERSION:
	{
		char buf[64];

		sprintf(buf, "IP Virtual Server version %d.%d.%d (size=%d)",
2597
			NVERSION(IP_VS_VERSION_CODE), ip_vs_conn_tab_size);
L
Linus Torvalds 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
		if (copy_to_user(user, buf, strlen(buf)+1) != 0) {
			ret = -EFAULT;
			goto out;
		}
		*len = strlen(buf)+1;
	}
	break;

	case IP_VS_SO_GET_INFO:
	{
		struct ip_vs_getinfo info;
		info.version = IP_VS_VERSION_CODE;
2610
		info.size = ip_vs_conn_tab_size;
2611
		info.num_services = ipvs->num_services;
L
Linus Torvalds 已提交
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
		if (copy_to_user(user, &info, sizeof(info)) != 0)
			ret = -EFAULT;
	}
	break;

	case IP_VS_SO_GET_SERVICES:
	{
		struct ip_vs_get_services *get;
		int size;

		get = (struct ip_vs_get_services *)arg;
		size = sizeof(*get) +
			sizeof(struct ip_vs_service_entry) * get->num_services;
		if (*len != size) {
2626
			pr_err("length: %u != %u\n", *len, size);
L
Linus Torvalds 已提交
2627 2628 2629
			ret = -EINVAL;
			goto out;
		}
2630
		ret = __ip_vs_get_service_entries(net, get, user);
L
Linus Torvalds 已提交
2631 2632 2633 2634 2635 2636 2637
	}
	break;

	case IP_VS_SO_GET_SERVICE:
	{
		struct ip_vs_service_entry *entry;
		struct ip_vs_service *svc;
2638
		union nf_inet_addr addr;
L
Linus Torvalds 已提交
2639 2640

		entry = (struct ip_vs_service_entry *)arg;
2641
		addr.ip = entry->addr;
L
Linus Torvalds 已提交
2642
		if (entry->fwmark)
2643
			svc = __ip_vs_svc_fwm_find(net, AF_INET, entry->fwmark);
L
Linus Torvalds 已提交
2644
		else
2645 2646 2647
			svc = __ip_vs_service_find(net, AF_INET,
						   entry->protocol, &addr,
						   entry->port);
L
Linus Torvalds 已提交
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
		if (svc) {
			ip_vs_copy_service(entry, svc);
			if (copy_to_user(user, entry, sizeof(*entry)) != 0)
				ret = -EFAULT;
		} else
			ret = -ESRCH;
	}
	break;

	case IP_VS_SO_GET_DESTS:
	{
		struct ip_vs_get_dests *get;
		int size;

		get = (struct ip_vs_get_dests *)arg;
		size = sizeof(*get) +
			sizeof(struct ip_vs_dest_entry) * get->num_dests;
		if (*len != size) {
2666
			pr_err("length: %u != %u\n", *len, size);
L
Linus Torvalds 已提交
2667 2668 2669
			ret = -EINVAL;
			goto out;
		}
2670
		ret = __ip_vs_get_dest_entries(net, get, user);
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675 2676 2677
	}
	break;

	case IP_VS_SO_GET_TIMEOUT:
	{
		struct ip_vs_timeout_user t;

2678
		__ip_vs_get_timeouts(net, &t);
L
Linus Torvalds 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
		if (copy_to_user(user, &t, sizeof(t)) != 0)
			ret = -EFAULT;
	}
	break;

	case IP_VS_SO_GET_DAEMON:
	{
		struct ip_vs_daemon_user d[2];

		memset(&d, 0, sizeof(d));
2689
		if (ipvs->sync_state & IP_VS_STATE_MASTER) {
L
Linus Torvalds 已提交
2690
			d[0].state = IP_VS_STATE_MASTER;
2691 2692 2693
			strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
				sizeof(d[0].mcast_ifn));
			d[0].syncid = ipvs->master_syncid;
L
Linus Torvalds 已提交
2694
		}
2695
		if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
L
Linus Torvalds 已提交
2696
			d[1].state = IP_VS_STATE_BACKUP;
2697 2698 2699
			strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
				sizeof(d[1].mcast_ifn));
			d[1].syncid = ipvs->backup_syncid;
L
Linus Torvalds 已提交
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
		}
		if (copy_to_user(user, &d, sizeof(d)) != 0)
			ret = -EFAULT;
	}
	break;

	default:
		ret = -EINVAL;
	}

  out:
2711
	mutex_unlock(&__ip_vs_mutex);
L
Linus Torvalds 已提交
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
	return ret;
}


static struct nf_sockopt_ops ip_vs_sockopts = {
	.pf		= PF_INET,
	.set_optmin	= IP_VS_BASE_CTL,
	.set_optmax	= IP_VS_SO_SET_MAX+1,
	.set		= do_ip_vs_set_ctl,
	.get_optmin	= IP_VS_BASE_CTL,
	.get_optmax	= IP_VS_SO_GET_MAX+1,
	.get		= do_ip_vs_get_ctl,
2724
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
2725 2726
};

2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
/*
 * Generic Netlink interface
 */

/* IPVS genetlink family */
static struct genl_family ip_vs_genl_family = {
	.id		= GENL_ID_GENERATE,
	.hdrsize	= 0,
	.name		= IPVS_GENL_NAME,
	.version	= IPVS_GENL_VERSION,
	.maxattr	= IPVS_CMD_MAX,
2738
	.netnsok        = true,         /* Make ipvsadm to work on netns */
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
};

/* Policy used for first-level command attributes */
static const struct nla_policy ip_vs_cmd_policy[IPVS_CMD_ATTR_MAX + 1] = {
	[IPVS_CMD_ATTR_SERVICE]		= { .type = NLA_NESTED },
	[IPVS_CMD_ATTR_DEST]		= { .type = NLA_NESTED },
	[IPVS_CMD_ATTR_DAEMON]		= { .type = NLA_NESTED },
	[IPVS_CMD_ATTR_TIMEOUT_TCP]	= { .type = NLA_U32 },
	[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]	= { .type = NLA_U32 },
	[IPVS_CMD_ATTR_TIMEOUT_UDP]	= { .type = NLA_U32 },
};

/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DAEMON */
static const struct nla_policy ip_vs_daemon_policy[IPVS_DAEMON_ATTR_MAX + 1] = {
	[IPVS_DAEMON_ATTR_STATE]	= { .type = NLA_U32 },
	[IPVS_DAEMON_ATTR_MCAST_IFN]	= { .type = NLA_NUL_STRING,
					    .len = IP_VS_IFNAME_MAXLEN },
	[IPVS_DAEMON_ATTR_SYNC_ID]	= { .type = NLA_U32 },
};

/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_SERVICE */
static const struct nla_policy ip_vs_svc_policy[IPVS_SVC_ATTR_MAX + 1] = {
	[IPVS_SVC_ATTR_AF]		= { .type = NLA_U16 },
	[IPVS_SVC_ATTR_PROTOCOL]	= { .type = NLA_U16 },
	[IPVS_SVC_ATTR_ADDR]		= { .type = NLA_BINARY,
					    .len = sizeof(union nf_inet_addr) },
	[IPVS_SVC_ATTR_PORT]		= { .type = NLA_U16 },
	[IPVS_SVC_ATTR_FWMARK]		= { .type = NLA_U32 },
	[IPVS_SVC_ATTR_SCHED_NAME]	= { .type = NLA_NUL_STRING,
					    .len = IP_VS_SCHEDNAME_MAXLEN },
2769 2770
	[IPVS_SVC_ATTR_PE_NAME]		= { .type = NLA_NUL_STRING,
					    .len = IP_VS_PENAME_MAXLEN },
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
	[IPVS_SVC_ATTR_FLAGS]		= { .type = NLA_BINARY,
					    .len = sizeof(struct ip_vs_flags) },
	[IPVS_SVC_ATTR_TIMEOUT]		= { .type = NLA_U32 },
	[IPVS_SVC_ATTR_NETMASK]		= { .type = NLA_U32 },
	[IPVS_SVC_ATTR_STATS]		= { .type = NLA_NESTED },
};

/* Policy used for attributes in nested attribute IPVS_CMD_ATTR_DEST */
static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
	[IPVS_DEST_ATTR_ADDR]		= { .type = NLA_BINARY,
					    .len = sizeof(union nf_inet_addr) },
	[IPVS_DEST_ATTR_PORT]		= { .type = NLA_U16 },
	[IPVS_DEST_ATTR_FWD_METHOD]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_WEIGHT]		= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_U_THRESH]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_L_THRESH]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_ACTIVE_CONNS]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_INACT_CONNS]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_PERSIST_CONNS]	= { .type = NLA_U32 },
	[IPVS_DEST_ATTR_STATS]		= { .type = NLA_NESTED },
};

static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
				 struct ip_vs_stats *stats)
{
2796
	struct ip_vs_stats_user ustats;
2797 2798 2799 2800
	struct nlattr *nl_stats = nla_nest_start(skb, container_type);
	if (!nl_stats)
		return -EMSGSIZE;

2801
	ip_vs_copy_stats(&ustats, stats);
2802

2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts);
	NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes);
	NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, ustats.cps);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps);
	NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps);
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833

	nla_nest_end(skb, nl_stats);

	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nl_stats);
	return -EMSGSIZE;
}

static int ip_vs_genl_fill_service(struct sk_buff *skb,
				   struct ip_vs_service *svc)
{
	struct nlattr *nl_service;
	struct ip_vs_flags flags = { .flags = svc->flags,
				     .mask = ~0 };

	nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
	if (!nl_service)
		return -EMSGSIZE;

2834
	NLA_PUT_U16(skb, IPVS_SVC_ATTR_AF, svc->af);
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844

	if (svc->fwmark) {
		NLA_PUT_U32(skb, IPVS_SVC_ATTR_FWMARK, svc->fwmark);
	} else {
		NLA_PUT_U16(skb, IPVS_SVC_ATTR_PROTOCOL, svc->protocol);
		NLA_PUT(skb, IPVS_SVC_ATTR_ADDR, sizeof(svc->addr), &svc->addr);
		NLA_PUT_U16(skb, IPVS_SVC_ATTR_PORT, svc->port);
	}

	NLA_PUT_STRING(skb, IPVS_SVC_ATTR_SCHED_NAME, svc->scheduler->name);
2845 2846
	if (svc->pe)
		NLA_PUT_STRING(skb, IPVS_SVC_ATTR_PE_NAME, svc->pe->name);
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
	NLA_PUT(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags);
	NLA_PUT_U32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ);
	NLA_PUT_U32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask);

	if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats))
		goto nla_put_failure;

	nla_nest_end(skb, nl_service);

	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nl_service);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_service(struct sk_buff *skb,
				   struct ip_vs_service *svc,
				   struct netlink_callback *cb)
{
	void *hdr;

	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
			  &ip_vs_genl_family, NLM_F_MULTI,
			  IPVS_CMD_NEW_SERVICE);
	if (!hdr)
		return -EMSGSIZE;

	if (ip_vs_genl_fill_service(skb, svc) < 0)
		goto nla_put_failure;

	return genlmsg_end(skb, hdr);

nla_put_failure:
	genlmsg_cancel(skb, hdr);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_services(struct sk_buff *skb,
				    struct netlink_callback *cb)
{
	int idx = 0, i;
	int start = cb->args[0];
	struct ip_vs_service *svc;
2891
	struct net *net = skb_sknet(skb);
2892 2893 2894 2895

	mutex_lock(&__ip_vs_mutex);
	for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
		list_for_each_entry(svc, &ip_vs_svc_table[i], s_list) {
2896
			if (++idx <= start || !net_eq(svc->net, net))
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
				continue;
			if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
				idx--;
				goto nla_put_failure;
			}
		}
	}

	for (i = 0; i < IP_VS_SVC_TAB_SIZE; i++) {
		list_for_each_entry(svc, &ip_vs_svc_fwm_table[i], f_list) {
2907
			if (++idx <= start || !net_eq(svc->net, net))
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
				continue;
			if (ip_vs_genl_dump_service(skb, svc, cb) < 0) {
				idx--;
				goto nla_put_failure;
			}
		}
	}

nla_put_failure:
	mutex_unlock(&__ip_vs_mutex);
	cb->args[0] = idx;

	return skb->len;
}

2923 2924
static int ip_vs_genl_parse_service(struct net *net,
				    struct ip_vs_service_user_kern *usvc,
2925 2926
				    struct nlattr *nla, int full_entry,
				    struct ip_vs_service **ret_svc)
2927 2928 2929
{
	struct nlattr *attrs[IPVS_SVC_ATTR_MAX + 1];
	struct nlattr *nla_af, *nla_port, *nla_fwmark, *nla_protocol, *nla_addr;
2930
	struct ip_vs_service *svc;
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945

	/* Parse mandatory identifying service fields first */
	if (nla == NULL ||
	    nla_parse_nested(attrs, IPVS_SVC_ATTR_MAX, nla, ip_vs_svc_policy))
		return -EINVAL;

	nla_af		= attrs[IPVS_SVC_ATTR_AF];
	nla_protocol	= attrs[IPVS_SVC_ATTR_PROTOCOL];
	nla_addr	= attrs[IPVS_SVC_ATTR_ADDR];
	nla_port	= attrs[IPVS_SVC_ATTR_PORT];
	nla_fwmark	= attrs[IPVS_SVC_ATTR_FWMARK];

	if (!(nla_af && (nla_fwmark || (nla_port && nla_protocol && nla_addr))))
		return -EINVAL;

S
Simon Horman 已提交
2946 2947
	memset(usvc, 0, sizeof(*usvc));

2948
	usvc->af = nla_get_u16(nla_af);
2949 2950 2951 2952 2953
#ifdef CONFIG_IP_VS_IPV6
	if (usvc->af != AF_INET && usvc->af != AF_INET6)
#else
	if (usvc->af != AF_INET)
#endif
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
		return -EAFNOSUPPORT;

	if (nla_fwmark) {
		usvc->protocol = IPPROTO_TCP;
		usvc->fwmark = nla_get_u32(nla_fwmark);
	} else {
		usvc->protocol = nla_get_u16(nla_protocol);
		nla_memcpy(&usvc->addr, nla_addr, sizeof(usvc->addr));
		usvc->port = nla_get_u16(nla_port);
		usvc->fwmark = 0;
	}

2966
	if (usvc->fwmark)
2967
		svc = __ip_vs_svc_fwm_find(net, usvc->af, usvc->fwmark);
2968
	else
2969
		svc = __ip_vs_service_find(net, usvc->af, usvc->protocol,
2970 2971 2972
					   &usvc->addr, usvc->port);
	*ret_svc = svc;

2973 2974
	/* If a full entry was requested, check for the additional fields */
	if (full_entry) {
2975
		struct nlattr *nla_sched, *nla_flags, *nla_pe, *nla_timeout,
2976 2977 2978 2979
			      *nla_netmask;
		struct ip_vs_flags flags;

		nla_sched = attrs[IPVS_SVC_ATTR_SCHED_NAME];
2980
		nla_pe = attrs[IPVS_SVC_ATTR_PE_NAME];
2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
		nla_flags = attrs[IPVS_SVC_ATTR_FLAGS];
		nla_timeout = attrs[IPVS_SVC_ATTR_TIMEOUT];
		nla_netmask = attrs[IPVS_SVC_ATTR_NETMASK];

		if (!(nla_sched && nla_flags && nla_timeout && nla_netmask))
			return -EINVAL;

		nla_memcpy(&flags, nla_flags, sizeof(flags));

		/* prefill flags from service if it already exists */
2991
		if (svc)
2992 2993 2994 2995 2996
			usvc->flags = svc->flags;

		/* set new flags from userland */
		usvc->flags = (usvc->flags & ~flags.mask) |
			      (flags.flags & flags.mask);
2997
		usvc->sched_name = nla_data(nla_sched);
2998
		usvc->pe_name = nla_pe ? nla_data(nla_pe) : NULL;
2999 3000 3001 3002 3003 3004 3005
		usvc->timeout = nla_get_u32(nla_timeout);
		usvc->netmask = nla_get_u32(nla_netmask);
	}

	return 0;
}

3006 3007
static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
						     struct nlattr *nla)
3008
{
3009
	struct ip_vs_service_user_kern usvc;
3010
	struct ip_vs_service *svc;
3011 3012
	int ret;

3013
	ret = ip_vs_genl_parse_service(net, &usvc, nla, 0, &svc);
3014
	return ret ? ERR_PTR(ret) : svc;
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
}

static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
{
	struct nlattr *nl_dest;

	nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
	if (!nl_dest)
		return -EMSGSIZE;

	NLA_PUT(skb, IPVS_DEST_ATTR_ADDR, sizeof(dest->addr), &dest->addr);
	NLA_PUT_U16(skb, IPVS_DEST_ATTR_PORT, dest->port);

	NLA_PUT_U32(skb, IPVS_DEST_ATTR_FWD_METHOD,
		    atomic_read(&dest->conn_flags) & IP_VS_CONN_F_FWD_MASK);
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_WEIGHT, atomic_read(&dest->weight));
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold);
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold);
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
		    atomic_read(&dest->activeconns));
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_INACT_CONNS,
		    atomic_read(&dest->inactconns));
	NLA_PUT_U32(skb, IPVS_DEST_ATTR_PERSIST_CONNS,
		    atomic_read(&dest->persistconns));

	if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats))
		goto nla_put_failure;

	nla_nest_end(skb, nl_dest);

	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nl_dest);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_dest(struct sk_buff *skb, struct ip_vs_dest *dest,
				struct netlink_callback *cb)
{
	void *hdr;

	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
			  &ip_vs_genl_family, NLM_F_MULTI,
			  IPVS_CMD_NEW_DEST);
	if (!hdr)
		return -EMSGSIZE;

	if (ip_vs_genl_fill_dest(skb, dest) < 0)
		goto nla_put_failure;

	return genlmsg_end(skb, hdr);

nla_put_failure:
	genlmsg_cancel(skb, hdr);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_dests(struct sk_buff *skb,
				 struct netlink_callback *cb)
{
	int idx = 0;
	int start = cb->args[0];
	struct ip_vs_service *svc;
	struct ip_vs_dest *dest;
	struct nlattr *attrs[IPVS_CMD_ATTR_MAX + 1];
3081
	struct net *net = skb_sknet(skb);
3082 3083 3084 3085 3086 3087 3088 3089

	mutex_lock(&__ip_vs_mutex);

	/* Try to find the service for which to dump destinations */
	if (nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs,
			IPVS_CMD_ATTR_MAX, ip_vs_cmd_policy))
		goto out_err;

3090

3091
	svc = ip_vs_genl_find_service(net, attrs[IPVS_CMD_ATTR_SERVICE]);
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
	if (IS_ERR(svc) || svc == NULL)
		goto out_err;

	/* Dump the destinations */
	list_for_each_entry(dest, &svc->destinations, n_list) {
		if (++idx <= start)
			continue;
		if (ip_vs_genl_dump_dest(skb, dest, cb) < 0) {
			idx--;
			goto nla_put_failure;
		}
	}

nla_put_failure:
	cb->args[0] = idx;

out_err:
	mutex_unlock(&__ip_vs_mutex);

	return skb->len;
}

3114
static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
				 struct nlattr *nla, int full_entry)
{
	struct nlattr *attrs[IPVS_DEST_ATTR_MAX + 1];
	struct nlattr *nla_addr, *nla_port;

	/* Parse mandatory identifying destination fields first */
	if (nla == NULL ||
	    nla_parse_nested(attrs, IPVS_DEST_ATTR_MAX, nla, ip_vs_dest_policy))
		return -EINVAL;

	nla_addr	= attrs[IPVS_DEST_ATTR_ADDR];
	nla_port	= attrs[IPVS_DEST_ATTR_PORT];

	if (!(nla_addr && nla_port))
		return -EINVAL;

S
Simon Horman 已提交
3131 3132
	memset(udest, 0, sizeof(*udest));

3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
	nla_memcpy(&udest->addr, nla_addr, sizeof(udest->addr));
	udest->port = nla_get_u16(nla_port);

	/* If a full entry was requested, check for the additional fields */
	if (full_entry) {
		struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
			      *nla_l_thresh;

		nla_fwd		= attrs[IPVS_DEST_ATTR_FWD_METHOD];
		nla_weight	= attrs[IPVS_DEST_ATTR_WEIGHT];
		nla_u_thresh	= attrs[IPVS_DEST_ATTR_U_THRESH];
		nla_l_thresh	= attrs[IPVS_DEST_ATTR_L_THRESH];

		if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
			return -EINVAL;

		udest->conn_flags = nla_get_u32(nla_fwd)
				    & IP_VS_CONN_F_FWD_MASK;
		udest->weight = nla_get_u32(nla_weight);
		udest->u_threshold = nla_get_u32(nla_u_thresh);
		udest->l_threshold = nla_get_u32(nla_l_thresh);
	}

	return 0;
}

static int ip_vs_genl_fill_daemon(struct sk_buff *skb, __be32 state,
				  const char *mcast_ifn, __be32 syncid)
{
	struct nlattr *nl_daemon;

	nl_daemon = nla_nest_start(skb, IPVS_CMD_ATTR_DAEMON);
	if (!nl_daemon)
		return -EMSGSIZE;

	NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_STATE, state);
	NLA_PUT_STRING(skb, IPVS_DAEMON_ATTR_MCAST_IFN, mcast_ifn);
	NLA_PUT_U32(skb, IPVS_DAEMON_ATTR_SYNC_ID, syncid);

	nla_nest_end(skb, nl_daemon);

	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nl_daemon);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_daemon(struct sk_buff *skb, __be32 state,
				  const char *mcast_ifn, __be32 syncid,
				  struct netlink_callback *cb)
{
	void *hdr;
	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
			  &ip_vs_genl_family, NLM_F_MULTI,
			  IPVS_CMD_NEW_DAEMON);
	if (!hdr)
		return -EMSGSIZE;

	if (ip_vs_genl_fill_daemon(skb, state, mcast_ifn, syncid))
		goto nla_put_failure;

	return genlmsg_end(skb, hdr);

nla_put_failure:
	genlmsg_cancel(skb, hdr);
	return -EMSGSIZE;
}

static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
				   struct netlink_callback *cb)
{
3205
	struct net *net = skb_sknet(skb);
3206 3207
	struct netns_ipvs *ipvs = net_ipvs(net);

3208
	mutex_lock(&__ip_vs_mutex);
3209
	if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
3210
		if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
3211 3212
					   ipvs->master_mcast_ifn,
					   ipvs->master_syncid, cb) < 0)
3213 3214 3215 3216 3217
			goto nla_put_failure;

		cb->args[0] = 1;
	}

3218
	if ((ipvs->sync_state & IP_VS_STATE_BACKUP) && !cb->args[1]) {
3219
		if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_BACKUP,
3220 3221
					   ipvs->backup_mcast_ifn,
					   ipvs->backup_syncid, cb) < 0)
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
			goto nla_put_failure;

		cb->args[1] = 1;
	}

nla_put_failure:
	mutex_unlock(&__ip_vs_mutex);

	return skb->len;
}

3233
static int ip_vs_genl_new_daemon(struct net *net, struct nlattr **attrs)
3234 3235 3236 3237 3238 3239
{
	if (!(attrs[IPVS_DAEMON_ATTR_STATE] &&
	      attrs[IPVS_DAEMON_ATTR_MCAST_IFN] &&
	      attrs[IPVS_DAEMON_ATTR_SYNC_ID]))
		return -EINVAL;

3240 3241
	return start_sync_thread(net,
				 nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]),
3242 3243 3244 3245
				 nla_data(attrs[IPVS_DAEMON_ATTR_MCAST_IFN]),
				 nla_get_u32(attrs[IPVS_DAEMON_ATTR_SYNC_ID]));
}

3246
static int ip_vs_genl_del_daemon(struct net *net, struct nlattr **attrs)
3247 3248 3249 3250
{
	if (!attrs[IPVS_DAEMON_ATTR_STATE])
		return -EINVAL;

3251 3252
	return stop_sync_thread(net,
				nla_get_u32(attrs[IPVS_DAEMON_ATTR_STATE]));
3253 3254
}

3255
static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
3256 3257 3258
{
	struct ip_vs_timeout_user t;

3259
	__ip_vs_get_timeouts(net, &t);
3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270

	if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP])
		t.tcp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP]);

	if (attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN])
		t.tcp_fin_timeout =
			nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_TCP_FIN]);

	if (attrs[IPVS_CMD_ATTR_TIMEOUT_UDP])
		t.udp_timeout = nla_get_u32(attrs[IPVS_CMD_ATTR_TIMEOUT_UDP]);

3271
	return ip_vs_set_timeout(net, &t);
3272 3273 3274 3275 3276
}

static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
{
	struct ip_vs_service *svc = NULL;
3277 3278
	struct ip_vs_service_user_kern usvc;
	struct ip_vs_dest_user_kern udest;
3279 3280
	int ret = 0, cmd;
	int need_full_svc = 0, need_full_dest = 0;
3281
	struct net *net;
3282
	struct netns_ipvs *ipvs;
3283

3284
	net = skb_sknet(skb);
3285
	ipvs = net_ipvs(net);
3286 3287 3288 3289 3290
	cmd = info->genlhdr->cmd;

	mutex_lock(&__ip_vs_mutex);

	if (cmd == IPVS_CMD_FLUSH) {
3291
		ret = ip_vs_flush(net);
3292 3293
		goto out;
	} else if (cmd == IPVS_CMD_SET_CONFIG) {
3294
		ret = ip_vs_genl_set_config(net, info->attrs);
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309
		goto out;
	} else if (cmd == IPVS_CMD_NEW_DAEMON ||
		   cmd == IPVS_CMD_DEL_DAEMON) {

		struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];

		if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
		    nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
				     info->attrs[IPVS_CMD_ATTR_DAEMON],
				     ip_vs_daemon_policy)) {
			ret = -EINVAL;
			goto out;
		}

		if (cmd == IPVS_CMD_NEW_DAEMON)
3310
			ret = ip_vs_genl_new_daemon(net, daemon_attrs);
3311
		else
3312
			ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3313 3314 3315
		goto out;
	} else if (cmd == IPVS_CMD_ZERO &&
		   !info->attrs[IPVS_CMD_ATTR_SERVICE]) {
3316
		ret = ip_vs_zero_all(net);
3317 3318 3319 3320 3321 3322 3323 3324 3325
		goto out;
	}

	/* All following commands require a service argument, so check if we
	 * received a valid one. We need a full service specification when
	 * adding / editing a service. Only identifying members otherwise. */
	if (cmd == IPVS_CMD_NEW_SERVICE || cmd == IPVS_CMD_SET_SERVICE)
		need_full_svc = 1;

3326
	ret = ip_vs_genl_parse_service(net, &usvc,
3327
				       info->attrs[IPVS_CMD_ATTR_SERVICE],
3328
				       need_full_svc, &svc);
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
	if (ret)
		goto out;

	/* Unless we're adding a new service, the service must already exist */
	if ((cmd != IPVS_CMD_NEW_SERVICE) && (svc == NULL)) {
		ret = -ESRCH;
		goto out;
	}

	/* Destination commands require a valid destination argument. For
	 * adding / editing a destination, we need a full destination
	 * specification. */
	if (cmd == IPVS_CMD_NEW_DEST || cmd == IPVS_CMD_SET_DEST ||
	    cmd == IPVS_CMD_DEL_DEST) {
		if (cmd != IPVS_CMD_DEL_DEST)
			need_full_dest = 1;

		ret = ip_vs_genl_parse_dest(&udest,
					    info->attrs[IPVS_CMD_ATTR_DEST],
					    need_full_dest);
		if (ret)
			goto out;
	}

	switch (cmd) {
	case IPVS_CMD_NEW_SERVICE:
		if (svc == NULL)
3356
			ret = ip_vs_add_service(net, &usvc, &svc);
3357 3358 3359 3360 3361 3362 3363 3364
		else
			ret = -EEXIST;
		break;
	case IPVS_CMD_SET_SERVICE:
		ret = ip_vs_edit_service(svc, &usvc);
		break;
	case IPVS_CMD_DEL_SERVICE:
		ret = ip_vs_del_service(svc);
3365
		/* do not use svc, it can be freed */
3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
		break;
	case IPVS_CMD_NEW_DEST:
		ret = ip_vs_add_dest(svc, &udest);
		break;
	case IPVS_CMD_SET_DEST:
		ret = ip_vs_edit_dest(svc, &udest);
		break;
	case IPVS_CMD_DEL_DEST:
		ret = ip_vs_del_dest(svc, &udest);
		break;
	case IPVS_CMD_ZERO:
		ret = ip_vs_zero_service(svc);
		break;
	default:
		ret = -EINVAL;
	}

out:
	mutex_unlock(&__ip_vs_mutex);

	return ret;
}

static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)
{
	struct sk_buff *msg;
	void *reply;
	int ret, cmd, reply_cmd;
3394
	struct net *net;
3395
	struct netns_ipvs *ipvs;
3396

3397
	net = skb_sknet(skb);
3398
	ipvs = net_ipvs(net);
3399 3400 3401 3402 3403 3404 3405 3406 3407
	cmd = info->genlhdr->cmd;

	if (cmd == IPVS_CMD_GET_SERVICE)
		reply_cmd = IPVS_CMD_NEW_SERVICE;
	else if (cmd == IPVS_CMD_GET_INFO)
		reply_cmd = IPVS_CMD_SET_INFO;
	else if (cmd == IPVS_CMD_GET_CONFIG)
		reply_cmd = IPVS_CMD_SET_CONFIG;
	else {
3408
		pr_err("unknown Generic Netlink command\n");
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
		return -EINVAL;
	}

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg)
		return -ENOMEM;

	mutex_lock(&__ip_vs_mutex);

	reply = genlmsg_put_reply(msg, info, &ip_vs_genl_family, 0, reply_cmd);
	if (reply == NULL)
		goto nla_put_failure;

	switch (cmd) {
	case IPVS_CMD_GET_SERVICE:
	{
		struct ip_vs_service *svc;

3427 3428
		svc = ip_vs_genl_find_service(net,
					      info->attrs[IPVS_CMD_ATTR_SERVICE]);
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
		if (IS_ERR(svc)) {
			ret = PTR_ERR(svc);
			goto out_err;
		} else if (svc) {
			ret = ip_vs_genl_fill_service(msg, svc);
			if (ret)
				goto nla_put_failure;
		} else {
			ret = -ESRCH;
			goto out_err;
		}

		break;
	}

	case IPVS_CMD_GET_CONFIG:
	{
		struct ip_vs_timeout_user t;

3448
		__ip_vs_get_timeouts(net, &t);
3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
#ifdef CONFIG_IP_VS_PROTO_TCP
		NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP, t.tcp_timeout);
		NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_TCP_FIN,
			    t.tcp_fin_timeout);
#endif
#ifdef CONFIG_IP_VS_PROTO_UDP
		NLA_PUT_U32(msg, IPVS_CMD_ATTR_TIMEOUT_UDP, t.udp_timeout);
#endif

		break;
	}

	case IPVS_CMD_GET_INFO:
		NLA_PUT_U32(msg, IPVS_INFO_ATTR_VERSION, IP_VS_VERSION_CODE);
		NLA_PUT_U32(msg, IPVS_INFO_ATTR_CONN_TAB_SIZE,
3464
			    ip_vs_conn_tab_size);
3465 3466 3467 3468
		break;
	}

	genlmsg_end(msg, reply);
J
Johannes Berg 已提交
3469
	ret = genlmsg_reply(msg, info);
3470 3471 3472
	goto out;

nla_put_failure:
3473
	pr_err("not enough space in Netlink message\n");
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582
	ret = -EMSGSIZE;

out_err:
	nlmsg_free(msg);
out:
	mutex_unlock(&__ip_vs_mutex);

	return ret;
}


static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
	{
		.cmd	= IPVS_CMD_NEW_SERVICE,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_SET_SERVICE,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_DEL_SERVICE,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_GET_SERVICE,
		.flags	= GENL_ADMIN_PERM,
		.doit	= ip_vs_genl_get_cmd,
		.dumpit	= ip_vs_genl_dump_services,
		.policy	= ip_vs_cmd_policy,
	},
	{
		.cmd	= IPVS_CMD_NEW_DEST,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_SET_DEST,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_DEL_DEST,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_GET_DEST,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.dumpit	= ip_vs_genl_dump_dests,
	},
	{
		.cmd	= IPVS_CMD_NEW_DAEMON,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_DEL_DAEMON,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_GET_DAEMON,
		.flags	= GENL_ADMIN_PERM,
		.dumpit	= ip_vs_genl_dump_daemons,
	},
	{
		.cmd	= IPVS_CMD_SET_CONFIG,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_GET_CONFIG,
		.flags	= GENL_ADMIN_PERM,
		.doit	= ip_vs_genl_get_cmd,
	},
	{
		.cmd	= IPVS_CMD_GET_INFO,
		.flags	= GENL_ADMIN_PERM,
		.doit	= ip_vs_genl_get_cmd,
	},
	{
		.cmd	= IPVS_CMD_ZERO,
		.flags	= GENL_ADMIN_PERM,
		.policy	= ip_vs_cmd_policy,
		.doit	= ip_vs_genl_set_cmd,
	},
	{
		.cmd	= IPVS_CMD_FLUSH,
		.flags	= GENL_ADMIN_PERM,
		.doit	= ip_vs_genl_set_cmd,
	},
};

static int __init ip_vs_genl_register(void)
{
3583 3584
	return genl_register_family_with_ops(&ip_vs_genl_family,
		ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops));
3585 3586 3587 3588 3589 3590 3591 3592 3593
}

static void ip_vs_genl_unregister(void)
{
	genl_unregister_family(&ip_vs_genl_family);
}

/* End of Generic Netlink interface definitions */

3594 3595 3596
/*
 * per netns intit/exit func.
 */
3597
#ifdef CONFIG_SYSCTL
3598
int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3599
{
3600 3601
	int idx;
	struct netns_ipvs *ipvs = net_ipvs(net);
3602
	struct ctl_table *tbl;
3603

3604 3605 3606 3607 3608 3609 3610 3611
	atomic_set(&ipvs->dropentry, 0);
	spin_lock_init(&ipvs->dropentry_lock);
	spin_lock_init(&ipvs->droppacket_lock);
	spin_lock_init(&ipvs->securetcp_lock);

	if (!net_eq(net, &init_net)) {
		tbl = kmemdup(vs_vars, sizeof(vs_vars), GFP_KERNEL);
		if (tbl == NULL)
3612
			return -ENOMEM;
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
	} else
		tbl = vs_vars;
	/* Initialize sysctl defaults */
	idx = 0;
	ipvs->sysctl_amemthresh = 1024;
	tbl[idx++].data = &ipvs->sysctl_amemthresh;
	ipvs->sysctl_am_droprate = 10;
	tbl[idx++].data = &ipvs->sysctl_am_droprate;
	tbl[idx++].data = &ipvs->sysctl_drop_entry;
	tbl[idx++].data = &ipvs->sysctl_drop_packet;
#ifdef CONFIG_IP_VS_NFCT
	tbl[idx++].data = &ipvs->sysctl_conntrack;
#endif
	tbl[idx++].data = &ipvs->sysctl_secure_tcp;
	ipvs->sysctl_snat_reroute = 1;
	tbl[idx++].data = &ipvs->sysctl_snat_reroute;
	ipvs->sysctl_sync_ver = 1;
	tbl[idx++].data = &ipvs->sysctl_sync_ver;
	tbl[idx++].data = &ipvs->sysctl_cache_bypass;
	tbl[idx++].data = &ipvs->sysctl_expire_nodest_conn;
	tbl[idx++].data = &ipvs->sysctl_expire_quiescent_template;
3634 3635
	ipvs->sysctl_sync_threshold[0] = DEFAULT_SYNC_THRESHOLD;
	ipvs->sysctl_sync_threshold[1] = DEFAULT_SYNC_PERIOD;
3636 3637 3638 3639 3640 3641
	tbl[idx].data = &ipvs->sysctl_sync_threshold;
	tbl[idx++].maxlen = sizeof(ipvs->sysctl_sync_threshold);
	tbl[idx++].data = &ipvs->sysctl_nat_icmp_send;


	ipvs->sysctl_hdr = register_net_sysctl_table(net, net_vs_ctl_path,
3642
						     tbl);
3643 3644 3645
	if (ipvs->sysctl_hdr == NULL) {
		if (!net_eq(net, &init_net))
			kfree(tbl);
3646
		return -ENOMEM;
3647
	}
3648
	ip_vs_start_estimator(net, &ipvs->tot_stats);
3649
	ipvs->sysctl_tbl = tbl;
3650 3651 3652
	/* Schedule defense work */
	INIT_DELAYED_WORK(&ipvs->defense_work, defense_work_handler);
	schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD);
3653 3654 3655 3656

	return 0;
}

3657
void __net_init ip_vs_control_net_cleanup_sysctl(struct net *net)
3658
{
3659 3660
	struct netns_ipvs *ipvs = net_ipvs(net);

H
Hans Schillstrom 已提交
3661 3662
	cancel_delayed_work_sync(&ipvs->defense_work);
	cancel_work_sync(&ipvs->defense_work.work);
3663
	unregister_net_sysctl_table(ipvs->sysctl_hdr);
3664 3665 3666 3667
}

#else

3668 3669
int __net_init ip_vs_control_net_init_sysctl(struct net *net) { return 0; }
void __net_init ip_vs_control_net_cleanup_sysctl(struct net *net) { }
3670

3671
#endif
3672

3673 3674 3675 3676
static struct notifier_block ip_vs_dst_notifier = {
	.notifier_call = ip_vs_dst_event,
};

3677
int __net_init ip_vs_control_net_init(struct net *net)
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693
{
	int idx;
	struct netns_ipvs *ipvs = net_ipvs(net);

	ipvs->rs_lock = __RW_LOCK_UNLOCKED(ipvs->rs_lock);

	/* Initialize rs_table */
	for (idx = 0; idx < IP_VS_RTAB_SIZE; idx++)
		INIT_LIST_HEAD(&ipvs->rs_table[idx]);

	INIT_LIST_HEAD(&ipvs->dest_trash);
	atomic_set(&ipvs->ftpsvc_counter, 0);
	atomic_set(&ipvs->nullsvc_counter, 0);

	/* procfs stats */
	ipvs->tot_stats.cpustats = alloc_percpu(struct ip_vs_cpu_stats);
3694
	if (!ipvs->tot_stats.cpustats) {
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704
		pr_err("%s(): alloc_percpu.\n", __func__);
		return -ENOMEM;
	}
	spin_lock_init(&ipvs->tot_stats.lock);

	proc_net_fops_create(net, "ip_vs", 0, &ip_vs_info_fops);
	proc_net_fops_create(net, "ip_vs_stats", 0, &ip_vs_stats_fops);
	proc_net_fops_create(net, "ip_vs_stats_percpu", 0,
			     &ip_vs_stats_percpu_fops);

3705
	if (ip_vs_control_net_init_sysctl(net))
3706 3707 3708 3709 3710
		goto err;

	return 0;

err:
J
Julian Anastasov 已提交
3711
	free_percpu(ipvs->tot_stats.cpustats);
3712 3713 3714
	return -ENOMEM;
}

3715
void __net_exit ip_vs_control_net_cleanup(struct net *net)
3716
{
3717 3718
	struct netns_ipvs *ipvs = net_ipvs(net);

H
Hans Schillstrom 已提交
3719
	ip_vs_trash_cleanup(net);
3720
	ip_vs_stop_estimator(net, &ipvs->tot_stats);
3721
	ip_vs_control_net_cleanup_sysctl(net);
3722
	proc_net_remove(net, "ip_vs_stats_percpu");
3723 3724
	proc_net_remove(net, "ip_vs_stats");
	proc_net_remove(net, "ip_vs");
J
Julian Anastasov 已提交
3725
	free_percpu(ipvs->tot_stats.cpustats);
3726 3727
}

3728
int __init ip_vs_control_init(void)
L
Linus Torvalds 已提交
3729 3730
{
	int idx;
3731
	int ret;
L
Linus Torvalds 已提交
3732 3733 3734

	EnterFunction(2);

3735
	/* Initialize svc_table, ip_vs_svc_fwm_table, rs_table */
3736 3737 3738 3739
	for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++)  {
		INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
		INIT_LIST_HEAD(&ip_vs_svc_fwm_table[idx]);
	}
3740 3741

	smp_wmb();	/* Do we really need it now ? */
3742

L
Linus Torvalds 已提交
3743 3744
	ret = nf_register_sockopt(&ip_vs_sockopts);
	if (ret) {
3745
		pr_err("cannot register sockopt.\n");
3746
		goto err_sock;
L
Linus Torvalds 已提交
3747 3748
	}

3749 3750
	ret = ip_vs_genl_register();
	if (ret) {
3751
		pr_err("cannot register Generic Netlink interface.\n");
3752
		goto err_genl;
3753 3754
	}

3755 3756 3757 3758
	ret = register_netdevice_notifier(&ip_vs_dst_notifier);
	if (ret < 0)
		goto err_notf;

L
Linus Torvalds 已提交
3759 3760
	LeaveFunction(2);
	return 0;
3761

3762 3763 3764 3765 3766
err_notf:
	ip_vs_genl_unregister();
err_genl:
	nf_unregister_sockopt(&ip_vs_sockopts);
err_sock:
3767
	return ret;
L
Linus Torvalds 已提交
3768 3769 3770 3771 3772 3773
}


void ip_vs_control_cleanup(void)
{
	EnterFunction(2);
3774
	ip_vs_genl_unregister();
L
Linus Torvalds 已提交
3775 3776 3777
	nf_unregister_sockopt(&ip_vs_sockopts);
	LeaveFunction(2);
}