ip_vs_app.c 13.4 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
/*
 * ip_vs_app.c: Application module support for IPVS
 *
 * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
 *
 *              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.
 *
 * Most code here is taken from ip_masq_app.c in kernel 2.2. The difference
 * is that ip_vs_app module handles the reverse direction (incoming requests
 * and outgoing responses).
 *
 *		IP_MASQ_APP application masquerading module
 *
 * Author:	Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
 *
 */

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

L
Linus Torvalds 已提交
24 25 26 27 28
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/ip.h>
29
#include <linux/netfilter.h>
30
#include <linux/slab.h>
31
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
32
#include <net/protocol.h>
33
#include <net/tcp.h>
L
Linus Torvalds 已提交
34 35 36
#include <linux/stat.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
I
Ingo Molnar 已提交
37
#include <linux/mutex.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44

#include <net/ip_vs.h>

EXPORT_SYMBOL(register_ip_vs_app);
EXPORT_SYMBOL(unregister_ip_vs_app);
EXPORT_SYMBOL(register_ip_vs_app_inc);

45 46
static DEFINE_MUTEX(__ip_vs_app_mutex);

L
Linus Torvalds 已提交
47 48 49 50 51
/*
 *	Get an ip_vs_app object
 */
static inline int ip_vs_app_get(struct ip_vs_app *app)
{
52
	return try_module_get(app->module);
L
Linus Torvalds 已提交
53 54 55 56 57
}


static inline void ip_vs_app_put(struct ip_vs_app *app)
{
58
	module_put(app->module);
L
Linus Torvalds 已提交
59 60
}

J
Julian Anastasov 已提交
61 62 63 64 65 66 67 68 69 70 71 72
static void ip_vs_app_inc_destroy(struct ip_vs_app *inc)
{
	kfree(inc->timeout_table);
	kfree(inc);
}

static void ip_vs_app_inc_rcu_free(struct rcu_head *head)
{
	struct ip_vs_app *inc = container_of(head, struct ip_vs_app, rcu_head);

	ip_vs_app_inc_destroy(inc);
}
L
Linus Torvalds 已提交
73 74 75 76 77

/*
 *	Allocate/initialize app incarnation and register it in proto apps.
 */
static int
78
ip_vs_app_inc_new(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
79
		  __u16 port)
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90
{
	struct ip_vs_protocol *pp;
	struct ip_vs_app *inc;
	int ret;

	if (!(pp = ip_vs_proto_get(proto)))
		return -EPROTONOSUPPORT;

	if (!pp->unregister_app)
		return -EOPNOTSUPP;

91
	inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	if (!inc)
		return -ENOMEM;
	INIT_LIST_HEAD(&inc->p_list);
	INIT_LIST_HEAD(&inc->incs_list);
	inc->app = app;
	inc->port = htons(port);
	atomic_set(&inc->usecnt, 0);

	if (app->timeouts) {
		inc->timeout_table =
			ip_vs_create_timeout_table(app->timeouts,
						   app->timeouts_size);
		if (!inc->timeout_table) {
			ret = -ENOMEM;
			goto out;
		}
	}

110
	ret = pp->register_app(ipvs, inc);
L
Linus Torvalds 已提交
111 112 113 114
	if (ret)
		goto out;

	list_add(&inc->a_list, &app->incs_list);
115 116
	IP_VS_DBG(9, "%s App %s:%u registered\n",
		  pp->name, inc->name, ntohs(inc->port));
L
Linus Torvalds 已提交
117 118 119 120

	return 0;

  out:
J
Julian Anastasov 已提交
121
	ip_vs_app_inc_destroy(inc);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129
	return ret;
}


/*
 *	Release app incarnation
 */
static void
130
ip_vs_app_inc_release(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
L
Linus Torvalds 已提交
131 132 133 134 135 136 137
{
	struct ip_vs_protocol *pp;

	if (!(pp = ip_vs_proto_get(inc->protocol)))
		return;

	if (pp->unregister_app)
138
		pp->unregister_app(ipvs, inc);
L
Linus Torvalds 已提交
139 140

	IP_VS_DBG(9, "%s App %s:%u unregistered\n",
141
		  pp->name, inc->name, ntohs(inc->port));
L
Linus Torvalds 已提交
142 143 144

	list_del(&inc->a_list);

J
Julian Anastasov 已提交
145
	call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156
}


/*
 *	Get reference to app inc (only called from softirq)
 *
 */
int ip_vs_app_inc_get(struct ip_vs_app *inc)
{
	int result;

J
Julian Anastasov 已提交
157 158 159
	result = ip_vs_app_get(inc->app);
	if (result)
		atomic_inc(&inc->usecnt);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169
	return result;
}


/*
 *	Put the app inc (only called from timer or net softirq)
 */
void ip_vs_app_inc_put(struct ip_vs_app *inc)
{
	atomic_dec(&inc->usecnt);
J
Julian Anastasov 已提交
170
	ip_vs_app_put(inc->app);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177
}


/*
 *	Register an application incarnation in protocol applications
 */
int
178
register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
179
		       __u16 port)
L
Linus Torvalds 已提交
180 181 182
{
	int result;

183
	mutex_lock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
184

185
	result = ip_vs_app_inc_new(ipvs, app, proto, port);
L
Linus Torvalds 已提交
186

187
	mutex_unlock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
188 189 190 191 192

	return result;
}


193
/* Register application for netns */
194
struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
L
Linus Torvalds 已提交
195
{
196 197 198
	struct ip_vs_app *a;
	int err = 0;

199
	mutex_lock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	list_for_each_entry(a, &ipvs->app_list, a_list) {
		if (!strcmp(app->name, a->name)) {
			err = -EEXIST;
			goto out_unlock;
		}
	}
	a = kmemdup(app, sizeof(*app), GFP_KERNEL);
	if (!a) {
		err = -ENOMEM;
		goto out_unlock;
	}
	INIT_LIST_HEAD(&a->incs_list);
	list_add(&a->a_list, &ipvs->app_list);
	/* increase the module use count */
	ip_vs_use_count_inc();
L
Linus Torvalds 已提交
216

217
out_unlock:
218
	mutex_unlock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
219

220
	return err ? ERR_PTR(err) : a;
L
Linus Torvalds 已提交
221 222 223 224 225 226
}


/*
 *	ip_vs_app unregistration routine
 *	We are sure there are no app incarnations attached to services
J
Julian Anastasov 已提交
227
 *	Caller should use synchronize_rcu() or rcu_barrier()
L
Linus Torvalds 已提交
228
 */
229
void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app)
L
Linus Torvalds 已提交
230
{
231
	struct ip_vs_app *a, *anxt, *inc, *nxt;
L
Linus Torvalds 已提交
232

233
	mutex_lock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
234

235 236 237 238
	list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) {
		if (app && strcmp(app->name, a->name))
			continue;
		list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) {
239
			ip_vs_app_inc_release(ipvs, inc);
240
		}
L
Linus Torvalds 已提交
241

242 243
		list_del(&a->a_list);
		kfree(a);
L
Linus Torvalds 已提交
244

245 246 247
		/* decrease the module use count */
		ip_vs_use_count_dec();
	}
L
Linus Torvalds 已提交
248

249
	mutex_unlock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
250 251 252 253 254 255
}


/*
 *	Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
 */
256 257
int ip_vs_bind_app(struct ip_vs_conn *cp,
		   struct ip_vs_protocol *pp)
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
{
	return pp->app_conn_bind(cp);
}


/*
 *	Unbind cp from application incarnation (called by cp destructor)
 */
void ip_vs_unbind_app(struct ip_vs_conn *cp)
{
	struct ip_vs_app *inc = cp->app;

	if (!inc)
		return;

	if (inc->unbind_conn)
		inc->unbind_conn(inc, cp);
	if (inc->done_conn)
		inc->done_conn(inc, cp);
	ip_vs_app_inc_put(inc);
	cp->app = NULL;
}


/*
 *	Fixes th->seq based on ip_vs_seq info.
 */
static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
{
	__u32 seq = ntohl(th->seq);

	/*
	 *	Adjust seq with delta-offset for all packets after
	 *	the most recent resized pkt seq and with previous_delta offset
	 *	for all packets	before most recent resized pkt seq.
	 */
	if (vseq->delta || vseq->previous_delta) {
		if(after(seq, vseq->init_seq)) {
			th->seq = htonl(seq + vseq->delta);
297 298
			IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
				  __func__, vseq->delta);
L
Linus Torvalds 已提交
299 300
		} else {
			th->seq = htonl(seq + vseq->previous_delta);
301 302
			IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
				  __func__, vseq->previous_delta);
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		}
	}
}


/*
 *	Fixes th->ack_seq based on ip_vs_seq info.
 */
static inline void
vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
{
	__u32 ack_seq = ntohl(th->ack_seq);

	/*
	 * Adjust ack_seq with delta-offset for
	 * the packets AFTER most recent resized pkt has caused a shift
	 * for packets before most recent resized pkt, use previous_delta
	 */
	if (vseq->delta || vseq->previous_delta) {
		/* since ack_seq is the number of octet that is expected
		   to receive next, so compare it with init_seq+delta */
		if(after(ack_seq, vseq->init_seq+vseq->delta)) {
			th->ack_seq = htonl(ack_seq - vseq->delta);
326 327
			IP_VS_DBG(9, "%s(): subtracted delta "
				  "(%d) from ack_seq\n", __func__, vseq->delta);
L
Linus Torvalds 已提交
328 329 330

		} else {
			th->ack_seq = htonl(ack_seq - vseq->previous_delta);
331
			IP_VS_DBG(9, "%s(): subtracted "
L
Linus Torvalds 已提交
332
				  "previous_delta (%d) from ack_seq\n",
333
				  __func__, vseq->previous_delta);
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343
		}
	}
}


/*
 *	Updates ip_vs_seq if pkt has been resized
 *	Assumes already checked proto==IPPROTO_TCP and diff!=0.
 */
static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
344
				 unsigned int flag, __u32 seq, int diff)
L
Linus Torvalds 已提交
345 346
{
	/* spinlock is to keep updating cp->flags atomic */
347
	spin_lock_bh(&cp->lock);
L
Linus Torvalds 已提交
348 349 350 351 352 353
	if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
		vseq->previous_delta = vseq->delta;
		vseq->delta += diff;
		vseq->init_seq = seq;
		cp->flags |= flag;
	}
354
	spin_unlock_bh(&cp->lock);
L
Linus Torvalds 已提交
355 356
}

357
static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
L
Linus Torvalds 已提交
358 359 360
				  struct ip_vs_app *app)
{
	int diff;
361
	const unsigned int tcp_offset = ip_hdrlen(skb);
L
Linus Torvalds 已提交
362 363 364
	struct tcphdr *th;
	__u32 seq;

365
	if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
L
Linus Torvalds 已提交
366 367
		return 0;

368
	th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
L
Linus Torvalds 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	/*
	 *	Remember seq number in case this pkt gets resized
	 */
	seq = ntohl(th->seq);

	/*
	 *	Fix seq stuff if flagged as so.
	 */
	if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
		vs_fix_seq(&cp->out_seq, th);
	if (cp->flags & IP_VS_CONN_F_IN_SEQ)
		vs_fix_ack_seq(&cp->in_seq, th);

	/*
	 *	Call private output hook function
	 */
	if (app->pkt_out == NULL)
		return 1;

389
	if (!app->pkt_out(app, cp, skb, &diff))
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		return 0;

	/*
	 *	Update ip_vs seq stuff if len has changed.
	 */
	if (diff != 0)
		vs_seq_update(cp, &cp->out_seq,
			      IP_VS_CONN_F_OUT_SEQ, seq, diff);

	return 1;
}

/*
 *	Output pkt hook. Will call bound ip_vs_app specific function
 *	called by ipvs packet handler, assumes previously checked cp!=NULL
 *	returns false if it can't handle packet (oom)
 */
407
int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
L
Linus Torvalds 已提交
408 409 410 411 412 413 414 415 416 417 418 419
{
	struct ip_vs_app *app;

	/*
	 *	check if application module is bound to
	 *	this ip_vs_conn.
	 */
	if ((app = cp->app) == NULL)
		return 1;

	/* TCP is complicated */
	if (cp->protocol == IPPROTO_TCP)
420
		return app_tcp_pkt_out(cp, skb, app);
L
Linus Torvalds 已提交
421 422 423 424 425 426 427

	/*
	 *	Call private output hook function
	 */
	if (app->pkt_out == NULL)
		return 1;

428
	return app->pkt_out(app, cp, skb, NULL);
L
Linus Torvalds 已提交
429 430 431
}


432
static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
L
Linus Torvalds 已提交
433 434 435
				 struct ip_vs_app *app)
{
	int diff;
436
	const unsigned int tcp_offset = ip_hdrlen(skb);
L
Linus Torvalds 已提交
437 438 439
	struct tcphdr *th;
	__u32 seq;

440
	if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
L
Linus Torvalds 已提交
441 442
		return 0;

443
	th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

	/*
	 *	Remember seq number in case this pkt gets resized
	 */
	seq = ntohl(th->seq);

	/*
	 *	Fix seq stuff if flagged as so.
	 */
	if (cp->flags & IP_VS_CONN_F_IN_SEQ)
		vs_fix_seq(&cp->in_seq, th);
	if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
		vs_fix_ack_seq(&cp->out_seq, th);

	/*
	 *	Call private input hook function
	 */
	if (app->pkt_in == NULL)
		return 1;

464
	if (!app->pkt_in(app, cp, skb, &diff))
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
		return 0;

	/*
	 *	Update ip_vs seq stuff if len has changed.
	 */
	if (diff != 0)
		vs_seq_update(cp, &cp->in_seq,
			      IP_VS_CONN_F_IN_SEQ, seq, diff);

	return 1;
}

/*
 *	Input pkt hook. Will call bound ip_vs_app specific function
 *	called by ipvs packet handler, assumes previously checked cp!=NULL.
 *	returns false if can't handle packet (oom).
 */
482
int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494
{
	struct ip_vs_app *app;

	/*
	 *	check if application module is bound to
	 *	this ip_vs_conn.
	 */
	if ((app = cp->app) == NULL)
		return 1;

	/* TCP is complicated */
	if (cp->protocol == IPPROTO_TCP)
495
		return app_tcp_pkt_in(cp, skb, app);
L
Linus Torvalds 已提交
496 497 498 499 500 501 502

	/*
	 *	Call private input hook function
	 */
	if (app->pkt_in == NULL)
		return 1;

503
	return app->pkt_in(app, cp, skb, NULL);
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511
}


#ifdef CONFIG_PROC_FS
/*
 *	/proc/net/ip_vs_app entry function
 */

512
static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos)
L
Linus Torvalds 已提交
513 514 515
{
	struct ip_vs_app *app, *inc;

516
	list_for_each_entry(app, &ipvs->app_list, a_list) {
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527
		list_for_each_entry(inc, &app->incs_list, a_list) {
			if (pos-- == 0)
				return inc;
		}
	}
	return NULL;

}

static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
{
528 529 530
	struct net *net = seq_file_net(seq);
	struct netns_ipvs *ipvs = net_ipvs(net);

531
	mutex_lock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
532

533
	return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN;
L
Linus Torvalds 已提交
534 535 536 537 538 539
}

static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct ip_vs_app *inc, *app;
	struct list_head *e;
540 541
	struct net *net = seq_file_net(seq);
	struct netns_ipvs *ipvs = net_ipvs(net);
L
Linus Torvalds 已提交
542 543 544

	++*pos;
	if (v == SEQ_START_TOKEN)
545
		return ip_vs_app_idx(ipvs, 0);
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553

	inc = v;
	app = inc->app;

	if ((e = inc->a_list.next) != &app->incs_list)
		return list_entry(e, struct ip_vs_app, a_list);

	/* go on to next application */
554
	for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) {
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564
		app = list_entry(e, struct ip_vs_app, a_list);
		list_for_each_entry(inc, &app->incs_list, a_list) {
			return inc;
		}
	}
	return NULL;
}

static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
{
565
	mutex_unlock(&__ip_vs_app_mutex);
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
}

static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
{
	if (v == SEQ_START_TOKEN)
		seq_puts(seq, "prot port    usecnt name\n");
	else {
		const struct ip_vs_app *inc = v;

		seq_printf(seq, "%-3s  %-7u %-6d %-17s\n",
			   ip_vs_proto_name(inc->protocol),
			   ntohs(inc->port),
			   atomic_read(&inc->usecnt),
			   inc->name);
	}
	return 0;
}

584
static const struct seq_operations ip_vs_app_seq_ops = {
L
Linus Torvalds 已提交
585 586 587 588 589 590 591 592
	.start = ip_vs_app_seq_start,
	.next  = ip_vs_app_seq_next,
	.stop  = ip_vs_app_seq_stop,
	.show  = ip_vs_app_seq_show,
};

static int ip_vs_app_open(struct inode *inode, struct file *file)
{
593 594
	return seq_open_net(inode, file, &ip_vs_app_seq_ops,
			    sizeof(struct seq_net_private));
L
Linus Torvalds 已提交
595 596
}

597
static const struct file_operations ip_vs_app_fops = {
L
Linus Torvalds 已提交
598 599 600 601
	.owner	 = THIS_MODULE,
	.open	 = ip_vs_app_open,
	.read	 = seq_read,
	.llseek  = seq_lseek,
602
	.release = seq_release_net,
L
Linus Torvalds 已提交
603 604 605
};
#endif

606
int __net_init ip_vs_app_net_init(struct netns_ipvs *ipvs)
L
Linus Torvalds 已提交
607
{
608
	struct net *net = ipvs->net;
609 610

	INIT_LIST_HEAD(&ipvs->app_list);
611
	proc_create("ip_vs_app", 0, net->proc_net, &ip_vs_app_fops);
L
Linus Torvalds 已提交
612 613 614
	return 0;
}

615
void __net_exit ip_vs_app_net_cleanup(struct netns_ipvs *ipvs)
616
{
617
	struct net *net = ipvs->net;
618 619

	unregister_ip_vs_app(ipvs, NULL /* all */);
620
	remove_proc_entry("ip_vs_app", net->proc_net);
621
}