ila_xlat.c 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <linux/jhash.h>
#include <linux/netfilter.h>
#include <linux/rcupdate.h>
#include <linux/rhashtable.h>
#include <linux/vmalloc.h>
#include <net/genetlink.h>
#include <net/ila.h>
#include <net/netns/generic.h>
#include <uapi/linux/genetlink.h>
#include "ila.h"

struct ila_xlat_params {
	struct ila_params ip;
	int ifindex;
};

struct ila_map {
18
	struct ila_xlat_params xp;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	struct rhash_head node;
	struct ila_map __rcu *next;
	struct rcu_head rcu;
};

static unsigned int ila_net_id;

struct ila_net {
	struct rhashtable rhash_table;
	spinlock_t *locks; /* Bucket locks for entry manipulation */
	unsigned int locks_mask;
	bool hooks_registered;
};

#define	LOCKS_PER_CPU 10

static int alloc_ila_locks(struct ila_net *ilan)
{
	unsigned int i, size;
	unsigned int nr_pcpus = num_possible_cpus();

	nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
	size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);

	if (sizeof(spinlock_t) != 0) {
44
		ilan->locks = kvmalloc(size * sizeof(spinlock_t), GFP_KERNEL);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
		if (!ilan->locks)
			return -ENOMEM;
		for (i = 0; i < size; i++)
			spin_lock_init(&ilan->locks[i]);
	}
	ilan->locks_mask = size - 1;

	return 0;
}

static u32 hashrnd __read_mostly;
static __always_inline void __ila_hash_secret_init(void)
{
	net_get_random_once(&hashrnd, sizeof(hashrnd));
}

T
Tom Herbert 已提交
61
static inline u32 ila_locator_hash(struct ila_locator loc)
62
{
T
Tom Herbert 已提交
63
	u32 *v = (u32 *)loc.v32;
64

65
	__ila_hash_secret_init();
66 67 68
	return jhash_2words(v[0], v[1], hashrnd);
}

69
static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
T
Tom Herbert 已提交
70
				       struct ila_locator loc)
71
{
T
Tom Herbert 已提交
72
	return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
73 74
}

75
static inline int ila_cmp_wildcards(struct ila_map *ila,
T
Tom Herbert 已提交
76
				    struct ila_addr *iaddr, int ifindex)
77
{
T
Tom Herbert 已提交
78
	return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
79 80
}

81 82
static inline int ila_cmp_params(struct ila_map *ila,
				 struct ila_xlat_params *xp)
83
{
T
Tom Herbert 已提交
84
	return (ila->xp.ifindex != xp->ifindex);
85 86 87 88 89 90 91
}

static int ila_cmpfn(struct rhashtable_compare_arg *arg,
		     const void *obj)
{
	const struct ila_map *ila = obj;

T
Tom Herbert 已提交
92
	return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
93 94 95 96 97 98
}

static inline int ila_order(struct ila_map *ila)
{
	int score = 0;

99
	if (ila->xp.ifindex)
100 101 102 103 104 105 106 107
		score += 1 << 1;

	return score;
}

static const struct rhashtable_params rht_params = {
	.nelem_hint = 1024,
	.head_offset = offsetof(struct ila_map, node),
T
Tom Herbert 已提交
108
	.key_offset = offsetof(struct ila_map, xp.ip.locator_match),
109 110 111 112 113 114 115
	.key_len = sizeof(u64), /* identifier */
	.max_size = 1048576,
	.min_size = 256,
	.automatic_shrinking = true,
	.obj_cmpfn = ila_cmpfn,
};

116
static struct genl_family ila_nl_family;
117

S
stephen hemminger 已提交
118
static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
119 120 121
	[ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
	[ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
	[ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
122
	[ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
123 124 125
};

static int parse_nl_config(struct genl_info *info,
126
			   struct ila_xlat_params *xp)
127
{
128
	memset(xp, 0, sizeof(*xp));
129 130

	if (info->attrs[ILA_ATTR_LOCATOR])
131
		xp->ip.locator.v64 = (__force __be64)nla_get_u64(
132 133 134
			info->attrs[ILA_ATTR_LOCATOR]);

	if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
135
		xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
136 137
			info->attrs[ILA_ATTR_LOCATOR_MATCH]);

138 139 140
	if (info->attrs[ILA_ATTR_CSUM_MODE])
		xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);

141
	if (info->attrs[ILA_ATTR_IFINDEX])
142
		xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
143 144 145 146 147

	return 0;
}

/* Must be called with rcu readlock */
148
static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
149 150 151 152 153
						   int ifindex,
						   struct ila_net *ilan)
{
	struct ila_map *ila;

T
Tom Herbert 已提交
154
	ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
155
				     rht_params);
156
	while (ila) {
T
Tom Herbert 已提交
157
		if (!ila_cmp_wildcards(ila, iaddr, ifindex))
158 159 160 161 162 163 164 165
			return ila;
		ila = rcu_access_pointer(ila->next);
	}

	return NULL;
}

/* Must be called with rcu readlock */
166
static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
167 168 169 170
						   struct ila_net *ilan)
{
	struct ila_map *ila;

T
Tom Herbert 已提交
171 172
	ila = rhashtable_lookup_fast(&ilan->rhash_table,
				     &xp->ip.locator_match,
173 174
				     rht_params);
	while (ila) {
175
		if (!ila_cmp_params(ila, xp))
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
			return ila;
		ila = rcu_access_pointer(ila->next);
	}

	return NULL;
}

static inline void ila_release(struct ila_map *ila)
{
	kfree_rcu(ila, rcu);
}

static void ila_free_cb(void *ptr, void *arg)
{
	struct ila_map *ila = (struct ila_map *)ptr, *next;

	/* Assume rcu_readlock held */
	while (ila) {
		next = rcu_access_pointer(ila->next);
		ila_release(ila);
		ila = next;
	}
}

200
static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral);
201 202 203 204 205 206

static unsigned int
ila_nf_input(void *priv,
	     struct sk_buff *skb,
	     const struct nf_hook_state *state)
{
207
	ila_xlat_addr(skb, false);
208 209 210
	return NF_ACCEPT;
}

211
static const struct nf_hook_ops ila_nf_hook_ops[] = {
212 213 214 215 216 217 218 219
	{
		.hook = ila_nf_input,
		.pf = NFPROTO_IPV6,
		.hooknum = NF_INET_PRE_ROUTING,
		.priority = -1,
	},
};

220
static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
221 222 223
{
	struct ila_net *ilan = net_generic(net, ila_net_id);
	struct ila_map *ila, *head;
T
Tom Herbert 已提交
224
	spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	int err = 0, order;

	if (!ilan->hooks_registered) {
		/* We defer registering net hooks in the namespace until the
		 * first mapping is added.
		 */
		err = nf_register_net_hooks(net, ila_nf_hook_ops,
					    ARRAY_SIZE(ila_nf_hook_ops));
		if (err)
			return err;

		ilan->hooks_registered = true;
	}

	ila = kzalloc(sizeof(*ila), GFP_KERNEL);
	if (!ila)
		return -ENOMEM;

243
	ila_init_saved_csum(&xp->ip);
244

245
	ila->xp = *xp;
246 247 248 249 250

	order = ila_order(ila);

	spin_lock(lock);

T
Tom Herbert 已提交
251 252
	head = rhashtable_lookup_fast(&ilan->rhash_table,
				      &xp->ip.locator_match,
253 254 255 256 257 258 259 260 261
				      rht_params);
	if (!head) {
		/* New entry for the rhash_table */
		err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
						    &ila->node, rht_params);
	} else {
		struct ila_map *tila = head, *prev = NULL;

		do {
262
			if (!ila_cmp_params(tila, xp)) {
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 297 298
				err = -EEXIST;
				goto out;
			}

			if (order > ila_order(tila))
				break;

			prev = tila;
			tila = rcu_dereference_protected(tila->next,
				lockdep_is_held(lock));
		} while (tila);

		if (prev) {
			/* Insert in sub list of head */
			RCU_INIT_POINTER(ila->next, tila);
			rcu_assign_pointer(prev->next, ila);
		} else {
			/* Make this ila new head */
			RCU_INIT_POINTER(ila->next, head);
			err = rhashtable_replace_fast(&ilan->rhash_table,
						      &head->node,
						      &ila->node, rht_params);
			if (err)
				goto out;
		}
	}

out:
	spin_unlock(lock);

	if (err)
		kfree(ila);

	return err;
}

299
static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
300 301 302
{
	struct ila_net *ilan = net_generic(net, ila_net_id);
	struct ila_map *ila, *head, *prev;
T
Tom Herbert 已提交
303
	spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
304 305 306 307 308
	int err = -ENOENT;

	spin_lock(lock);

	head = rhashtable_lookup_fast(&ilan->rhash_table,
T
Tom Herbert 已提交
309
				      &xp->ip.locator_match, rht_params);
310 311 312 313 314
	ila = head;

	prev = NULL;

	while (ila) {
315
		if (ila_cmp_params(ila, xp)) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
			prev = ila;
			ila = rcu_dereference_protected(ila->next,
							lockdep_is_held(lock));
			continue;
		}

		err = 0;

		if (prev) {
			/* Not head, just delete from list */
			rcu_assign_pointer(prev->next, ila->next);
		} else {
			/* It is the head. If there is something in the
			 * sublist we need to make a new head.
			 */
			head = rcu_dereference_protected(ila->next,
							 lockdep_is_held(lock));
			if (head) {
				/* Put first entry in the sublist into the
				 * table
				 */
				err = rhashtable_replace_fast(
					&ilan->rhash_table, &ila->node,
					&head->node, rht_params);
				if (err)
					goto out;
			} else {
				/* Entry no longer used */
				err = rhashtable_remove_fast(&ilan->rhash_table,
							     &ila->node,
							     rht_params);
			}
		}

		ila_release(ila);

		break;
	}

out:
	spin_unlock(lock);

	return err;
}

static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
{
	struct net *net = genl_info_net(info);
	struct ila_xlat_params p;
	int err;

	err = parse_nl_config(info, &p);
	if (err)
		return err;

	return ila_add_mapping(net, &p);
}

static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
{
	struct net *net = genl_info_net(info);
377
	struct ila_xlat_params xp;
378 379
	int err;

380
	err = parse_nl_config(info, &xp);
381 382 383
	if (err)
		return err;

384
	ila_del_mapping(net, &xp);
385 386 387 388 389 390

	return 0;
}

static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
{
T
Tom Herbert 已提交
391
	if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
392
			      (__force u64)ila->xp.ip.locator.v64,
N
Nicolas Dichtel 已提交
393 394
			      ILA_ATTR_PAD) ||
	    nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
395
			      (__force u64)ila->xp.ip.locator_match.v64,
N
Nicolas Dichtel 已提交
396
			      ILA_ATTR_PAD) ||
397 398
	    nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
	    nla_put_u32(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode))
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		return -1;

	return 0;
}

static int ila_dump_info(struct ila_map *ila,
			 u32 portid, u32 seq, u32 flags,
			 struct sk_buff *skb, u8 cmd)
{
	void *hdr;

	hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
	if (!hdr)
		return -ENOMEM;

	if (ila_fill_info(ila, skb) < 0)
		goto nla_put_failure;

	genlmsg_end(skb, hdr);
	return 0;

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

static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
{
	struct net *net = genl_info_net(info);
	struct ila_net *ilan = net_generic(net, ila_net_id);
	struct sk_buff *msg;
430
	struct ila_xlat_params xp;
431 432 433
	struct ila_map *ila;
	int ret;

434
	ret = parse_nl_config(info, &xp);
435 436 437 438 439 440 441 442 443
	if (ret)
		return ret;

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

	rcu_read_lock();

444
	ila = ila_lookup_by_params(&xp, ilan);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	if (ila) {
		ret = ila_dump_info(ila,
				    info->snd_portid,
				    info->snd_seq, 0, msg,
				    info->genlhdr->cmd);
	}

	rcu_read_unlock();

	if (ret < 0)
		goto out_free;

	return genlmsg_reply(msg, info);

out_free:
	nlmsg_free(msg);
	return ret;
}

struct ila_dump_iter {
	struct rhashtable_iter rhiter;
};

static int ila_nl_dump_start(struct netlink_callback *cb)
{
	struct net *net = sock_net(cb->skb->sk);
	struct ila_net *ilan = net_generic(net, ila_net_id);
472 473 474 475 476 477 478 479 480
	struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];

	if (!iter) {
		iter = kmalloc(sizeof(*iter), GFP_KERNEL);
		if (!iter)
			return -ENOMEM;

		cb->args[0] = (long)iter;
	}
481

482 483
	return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
				    GFP_KERNEL);
484 485 486 487
}

static int ila_nl_dump_done(struct netlink_callback *cb)
{
488
	struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
489 490 491

	rhashtable_walk_exit(&iter->rhiter);

492 493
	kfree(iter);

494 495 496 497 498
	return 0;
}

static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
499
	struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	struct rhashtable_iter *rhiter = &iter->rhiter;
	struct ila_map *ila;
	int ret;

	ret = rhashtable_walk_start(rhiter);
	if (ret && ret != -EAGAIN)
		goto done;

	for (;;) {
		ila = rhashtable_walk_next(rhiter);

		if (IS_ERR(ila)) {
			if (PTR_ERR(ila) == -EAGAIN)
				continue;
			ret = PTR_ERR(ila);
			goto done;
		} else if (!ila) {
			break;
		}

		while (ila) {
			ret =  ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
					     cb->nlh->nlmsg_seq, NLM_F_MULTI,
					     skb, ILA_CMD_GET);
			if (ret)
				goto done;

			ila = rcu_access_pointer(ila->next);
		}
	}

	ret = skb->len;

done:
	rhashtable_walk_stop(rhiter);
	return ret;
}

static const struct genl_ops ila_nl_ops[] = {
	{
		.cmd = ILA_CMD_ADD,
		.doit = ila_nl_cmd_add_mapping,
		.policy = ila_nl_policy,
		.flags = GENL_ADMIN_PERM,
	},
	{
		.cmd = ILA_CMD_DEL,
		.doit = ila_nl_cmd_del_mapping,
		.policy = ila_nl_policy,
		.flags = GENL_ADMIN_PERM,
	},
	{
		.cmd = ILA_CMD_GET,
		.doit = ila_nl_cmd_get_mapping,
		.start = ila_nl_dump_start,
		.dumpit = ila_nl_dump,
		.done = ila_nl_dump_done,
		.policy = ila_nl_policy,
	},
};

561
static struct genl_family ila_nl_family __ro_after_init = {
562 563 564 565 566 567 568 569 570 571 572
	.hdrsize	= 0,
	.name		= ILA_GENL_NAME,
	.version	= ILA_GENL_VERSION,
	.maxattr	= ILA_ATTR_MAX,
	.netnsok	= true,
	.parallel_ops	= true,
	.module		= THIS_MODULE,
	.ops		= ila_nl_ops,
	.n_ops		= ARRAY_SIZE(ila_nl_ops),
};

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
#define ILA_HASH_TABLE_SIZE 1024

static __net_init int ila_init_net(struct net *net)
{
	int err;
	struct ila_net *ilan = net_generic(net, ila_net_id);

	err = alloc_ila_locks(ilan);
	if (err)
		return err;

	rhashtable_init(&ilan->rhash_table, &rht_params);

	return 0;
}

static __net_exit void ila_exit_net(struct net *net)
{
	struct ila_net *ilan = net_generic(net, ila_net_id);

	rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);

	kvfree(ilan->locks);

	if (ilan->hooks_registered)
		nf_unregister_net_hooks(net, ila_nf_hook_ops,
					ARRAY_SIZE(ila_nf_hook_ops));
}

static struct pernet_operations ila_net_ops = {
	.init = ila_init_net,
	.exit = ila_exit_net,
	.id   = &ila_net_id,
	.size = sizeof(struct ila_net),
};

609
static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral)
610 611 612 613 614
{
	struct ila_map *ila;
	struct ipv6hdr *ip6h = ipv6_hdr(skb);
	struct net *net = dev_net(skb->dev);
	struct ila_net *ilan = net_generic(net, ila_net_id);
615
	struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
616 617 618

	/* Assumes skb contains a valid IPv6 header that is pulled */

T
Tom Herbert 已提交
619 620 621 622
	if (!ila_addr_is_ila(iaddr)) {
		/* Type indicates this is not an ILA address */
		return 0;
	}
623 624 625

	rcu_read_lock();

T
Tom Herbert 已提交
626
	ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
627
	if (ila)
628
		ila_update_ipv6_locator(skb, &ila->xp.ip, set_csum_neutral);
629 630 631 632 633 634

	rcu_read_unlock();

	return 0;
}

635
int __init ila_xlat_init(void)
636 637 638 639 640 641 642
{
	int ret;

	ret = register_pernet_device(&ila_net_ops);
	if (ret)
		goto exit;

643
	ret = genl_register_family(&ila_nl_family);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	if (ret < 0)
		goto unregister;

	return 0;

unregister:
	unregister_pernet_device(&ila_net_ops);
exit:
	return ret;
}

void ila_xlat_fini(void)
{
	genl_unregister_family(&ila_nl_family);
	unregister_pernet_device(&ila_net_ops);
}