addrlabel.c 14.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9
/*
 * IPv6 Address Label subsystem
 * for the IPv6 "Default" Source Address Selection
 *
 * Copyright (C)2007 USAGI/WIDE Project
 */
/*
 * Author:
10
 *	YOSHIFUJI Hideaki @ USAGI/WIDE Project <yoshfuji@linux-ipv6.org>
11 12 13 14 15 16
 */

#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/rcupdate.h>
#include <linux/in6.h>
17
#include <linux/slab.h>
18 19 20 21 22 23 24 25
#include <net/addrconf.h>
#include <linux/if_addrlabel.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#if 0
#define ADDRLABEL(x...) printk(x)
#else
26
#define ADDRLABEL(x...) do { ; } while (0)
27 28 29 30 31
#endif

/*
 * Policy Table
 */
32
struct ip6addrlbl_entry {
33 34 35 36 37 38 39 40 41 42
	struct in6_addr prefix;
	int prefixlen;
	int ifindex;
	int addrtype;
	u32 label;
	struct hlist_node list;
	struct rcu_head rcu;
};

/*
43
 * Default policy table (RFC6724 + extensions)
44 45 46 47 48 49 50 51 52 53
 *
 * prefix		addr_type	label
 * -------------------------------------------------------------------------
 * ::1/128		LOOPBACK	0
 * ::/0			N/A		1
 * 2002::/16		N/A		2
 * ::/96		COMPATv4	3
 * ::ffff:0:0/96	V4MAPPED	4
 * fc00::/7		N/A		5		ULA (RFC 4193)
 * 2001::/32		N/A		6		Teredo (RFC 4380)
54
 * 2001:10::/28		N/A		7		ORCHID (RFC 4843)
55 56 57
 * fec0::/10		N/A		11		Site-local
 *							(deprecated by RFC3879)
 * 3ffe::/16		N/A		12		6bone
58 59
 *
 * Note: 0xffffffff is used if we do not have any policies.
60
 * Note: Labels for ULA and 6to4 are different from labels listed in RFC6724.
61 62 63 64
 */

#define IPV6_ADDR_LABEL_DEFAULT	0xffffffffUL

65
static const __net_initconst struct ip6addrlbl_init_table
66 67 68 69 70 71 72 73
{
	const struct in6_addr *prefix;
	int prefixlen;
	u32 label;
} ip6addrlbl_init_table[] = {
	{	/* ::/0 */
		.prefix = &in6addr_any,
		.label = 1,
74 75
	}, {	/* fc00::/7 */
		.prefix = &(struct in6_addr){ { { 0xfc } } } ,
76 77
		.prefixlen = 7,
		.label = 5,
78 79
	}, {	/* fec0::/10 */
		.prefix = &(struct in6_addr){ { { 0xfe, 0xc0 } } },
80 81
		.prefixlen = 10,
		.label = 11,
82 83
	}, {	/* 2002::/16 */
		.prefix = &(struct in6_addr){ { { 0x20, 0x02 } } },
84 85
		.prefixlen = 16,
		.label = 2,
86 87
	}, {	/* 3ffe::/16 */
		.prefix = &(struct in6_addr){ { { 0x3f, 0xfe } } },
88 89
		.prefixlen = 16,
		.label = 12,
90 91
	}, {	/* 2001::/32 */
		.prefix = &(struct in6_addr){ { { 0x20, 0x01 } } },
92 93
		.prefixlen = 32,
		.label = 6,
94 95
	}, {	/* 2001:10::/28 */
		.prefix = &(struct in6_addr){ { { 0x20, 0x01, 0x00, 0x10 } } },
96 97
		.prefixlen = 28,
		.label = 7,
98 99
	}, {	/* ::ffff:0:0 */
		.prefix = &(struct in6_addr){ { { [10] = 0xff, [11] = 0xff } } },
100 101
		.prefixlen = 96,
		.label = 4,
102
	}, {	/* ::/96 */
103 104 105
		.prefix = &in6addr_any,
		.prefixlen = 96,
		.label = 3,
106
	}, {	/* ::1/128 */
107 108 109 110 111 112 113
		.prefix = &in6addr_loopback,
		.prefixlen = 128,
		.label = 0,
	}
};

/* Find label */
114
static bool __ip6addrlbl_match(const struct ip6addrlbl_entry *p,
115 116
			       const struct in6_addr *addr,
			       int addrtype, int ifindex)
117 118
{
	if (p->ifindex && p->ifindex != ifindex)
119
		return false;
120
	if (p->addrtype && p->addrtype != addrtype)
121
		return false;
122
	if (!ipv6_prefix_equal(addr, &p->prefix, p->prefixlen))
123 124
		return false;
	return true;
125 126
}

127 128
static struct ip6addrlbl_entry *__ipv6_addr_label(struct net *net,
						  const struct in6_addr *addr,
129 130 131
						  int type, int ifindex)
{
	struct ip6addrlbl_entry *p;
132 133 134

	hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
		if (__ip6addrlbl_match(p, addr, type, ifindex))
135 136 137 138 139
			return p;
	}
	return NULL;
}

140 141
u32 ipv6_addr_label(struct net *net,
		    const struct in6_addr *addr, int type, int ifindex)
142 143 144 145 146 147 148
{
	u32 label;
	struct ip6addrlbl_entry *p;

	type &= IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK;

	rcu_read_lock();
149
	p = __ipv6_addr_label(net, addr, type, ifindex);
150 151 152
	label = p ? p->label : IPV6_ADDR_LABEL_DEFAULT;
	rcu_read_unlock();

153
	ADDRLABEL(KERN_DEBUG "%s(addr=%pI6, type=%d, ifindex=%d) => %08x\n",
154
		  __func__, addr, type, ifindex, label);
155 156 157 158 159

	return label;
}

/* allocate one entry */
160
static struct ip6addrlbl_entry *ip6addrlbl_alloc(const struct in6_addr *prefix,
161 162
						 int prefixlen, int ifindex,
						 u32 label)
163 164 165 166
{
	struct ip6addrlbl_entry *newp;
	int addrtype;

167
	ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u)\n",
168
		  __func__, prefix, prefixlen, ifindex, (unsigned int)label);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

	addrtype = ipv6_addr_type(prefix) & (IPV6_ADDR_MAPPED | IPV6_ADDR_COMPATv4 | IPV6_ADDR_LOOPBACK);

	switch (addrtype) {
	case IPV6_ADDR_MAPPED:
		if (prefixlen > 96)
			return ERR_PTR(-EINVAL);
		if (prefixlen < 96)
			addrtype = 0;
		break;
	case IPV6_ADDR_COMPATv4:
		if (prefixlen != 96)
			addrtype = 0;
		break;
	case IPV6_ADDR_LOOPBACK:
		if (prefixlen != 128)
			addrtype = 0;
		break;
	}

	newp = kmalloc(sizeof(*newp), GFP_KERNEL);
	if (!newp)
		return ERR_PTR(-ENOMEM);

	ipv6_addr_prefix(&newp->prefix, prefix, prefixlen);
	newp->prefixlen = prefixlen;
	newp->ifindex = ifindex;
	newp->addrtype = addrtype;
	newp->label = label;
	INIT_HLIST_NODE(&newp->list);
	return newp;
}

/* add a label */
203 204
static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
			    int replace)
205
{
206
	struct ip6addrlbl_entry *last = NULL, *p = NULL;
207
	struct hlist_node *n;
208 209
	int ret = 0;

210 211
	ADDRLABEL(KERN_DEBUG "%s(newp=%p, replace=%d)\n", __func__, newp,
		  replace);
212

213
	hlist_for_each_entry_safe(p, n,	&net->ipv6.ip6addrlbl_table.head, list) {
214 215 216 217 218
		if (p->prefixlen == newp->prefixlen &&
		    p->ifindex == newp->ifindex &&
		    ipv6_addr_equal(&p->prefix, &newp->prefix)) {
			if (!replace) {
				ret = -EEXIST;
219 220
				goto out;
			}
221
			hlist_replace_rcu(&p->list, &newp->list);
222
			kfree_rcu(p, rcu);
223 224 225 226 227
			goto out;
		} else if ((p->prefixlen == newp->prefixlen && !p->ifindex) ||
			   (p->prefixlen < newp->prefixlen)) {
			hlist_add_before_rcu(&newp->list, &p->list);
			goto out;
228
		}
229
		last = p;
230
	}
231
	if (last)
232
		hlist_add_behind_rcu(&newp->list, &last->list);
233
	else
234
		hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
235 236
out:
	if (!ret)
237
		net->ipv6.ip6addrlbl_table.seq++;
238 239 240 241
	return ret;
}

/* add a label */
242 243
static int ip6addrlbl_add(struct net *net,
			  const struct in6_addr *prefix, int prefixlen,
244
			  int ifindex, u32 label, int replace)
245 246 247 248
{
	struct ip6addrlbl_entry *newp;
	int ret = 0;

249
	ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d, label=%u, replace=%d)\n",
250 251
		  __func__, prefix, prefixlen, ifindex, (unsigned int)label,
		  replace);
252

253
	newp = ip6addrlbl_alloc(prefix, prefixlen, ifindex, label);
254 255
	if (IS_ERR(newp))
		return PTR_ERR(newp);
256 257 258
	spin_lock(&net->ipv6.ip6addrlbl_table.lock);
	ret = __ip6addrlbl_add(net, newp, replace);
	spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
259
	if (ret)
260
		kfree(newp);
261 262 263 264
	return ret;
}

/* remove a label */
265 266
static int __ip6addrlbl_del(struct net *net,
			    const struct in6_addr *prefix, int prefixlen,
267
			    int ifindex)
268 269
{
	struct ip6addrlbl_entry *p = NULL;
270
	struct hlist_node *n;
271 272
	int ret = -ESRCH;

273
	ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
274
		  __func__, prefix, prefixlen, ifindex);
275

276
	hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) {
277 278 279 280
		if (p->prefixlen == prefixlen &&
		    p->ifindex == ifindex &&
		    ipv6_addr_equal(&p->prefix, prefix)) {
			hlist_del_rcu(&p->list);
281
			kfree_rcu(p, rcu);
282 283 284 285 286 287 288
			ret = 0;
			break;
		}
	}
	return ret;
}

289 290
static int ip6addrlbl_del(struct net *net,
			  const struct in6_addr *prefix, int prefixlen,
291
			  int ifindex)
292 293 294 295
{
	struct in6_addr prefix_buf;
	int ret;

296
	ADDRLABEL(KERN_DEBUG "%s(prefix=%pI6, prefixlen=%d, ifindex=%d)\n",
297
		  __func__, prefix, prefixlen, ifindex);
298 299

	ipv6_addr_prefix(&prefix_buf, prefix, prefixlen);
300
	spin_lock(&net->ipv6.ip6addrlbl_table.lock);
301
	ret = __ip6addrlbl_del(net, &prefix_buf, prefixlen, ifindex);
302
	spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
303 304 305 306
	return ret;
}

/* add default label */
307
static int __net_init ip6addrlbl_net_init(struct net *net)
308 309 310 311
{
	int err = 0;
	int i;

312
	ADDRLABEL(KERN_DEBUG "%s\n", __func__);
313

314 315 316
	spin_lock_init(&net->ipv6.ip6addrlbl_table.lock);
	INIT_HLIST_HEAD(&net->ipv6.ip6addrlbl_table.head);

317
	for (i = 0; i < ARRAY_SIZE(ip6addrlbl_init_table); i++) {
318 319
		int ret = ip6addrlbl_add(net,
					 ip6addrlbl_init_table[i].prefix,
320 321 322 323 324 325 326 327 328 329
					 ip6addrlbl_init_table[i].prefixlen,
					 0,
					 ip6addrlbl_init_table[i].label, 0);
		/* XXX: should we free all rules when we catch an error? */
		if (ret && (!err || err != -ENOMEM))
			err = ret;
	}
	return err;
}

330 331 332
static void __net_exit ip6addrlbl_net_exit(struct net *net)
{
	struct ip6addrlbl_entry *p = NULL;
333
	struct hlist_node *n;
334 335

	/* Remove all labels belonging to the exiting net */
336 337 338
	spin_lock(&net->ipv6.ip6addrlbl_table.lock);
	hlist_for_each_entry_safe(p, n, &net->ipv6.ip6addrlbl_table.head, list) {
		hlist_del_rcu(&p->list);
339
		kfree_rcu(p, rcu);
340
	}
341
	spin_unlock(&net->ipv6.ip6addrlbl_table.lock);
342 343 344 345 346 347 348
}

static struct pernet_operations ipv6_addr_label_ops = {
	.init = ip6addrlbl_net_init,
	.exit = ip6addrlbl_net_exit,
};

349 350
int __init ipv6_addr_label_init(void)
{
351
	return register_pernet_subsys(&ipv6_addr_label_ops);
352 353
}

354 355 356 357 358
void ipv6_addr_label_cleanup(void)
{
	unregister_pernet_subsys(&ipv6_addr_label_ops);
}

359 360 361 362 363
static const struct nla_policy ifal_policy[IFAL_MAX+1] = {
	[IFAL_ADDRESS]		= { .len = sizeof(struct in6_addr), },
	[IFAL_LABEL]		= { .len = sizeof(u32), },
};

364 365 366 367 368 369 370 371 372 373 374 375
static bool addrlbl_ifindex_exists(struct net *net, int ifindex)
{

	struct net_device *dev;

	rcu_read_lock();
	dev = dev_get_by_index_rcu(net, ifindex);
	rcu_read_unlock();

	return dev != NULL;
}

376 377
static int ip6addrlbl_newdel(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
378
{
379
	struct net *net = sock_net(skb->sk);
380 381 382 383 384 385
	struct ifaddrlblmsg *ifal;
	struct nlattr *tb[IFAL_MAX+1];
	struct in6_addr *pfx;
	u32 label;
	int err = 0;

386 387
	err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy,
			  extack);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	if (err < 0)
		return err;

	ifal = nlmsg_data(nlh);

	if (ifal->ifal_family != AF_INET6 ||
	    ifal->ifal_prefixlen > 128)
		return -EINVAL;

	if (!tb[IFAL_ADDRESS])
		return -EINVAL;
	pfx = nla_data(tb[IFAL_ADDRESS]);

	if (!tb[IFAL_LABEL])
		return -EINVAL;
	label = nla_get_u32(tb[IFAL_LABEL]);
	if (label == IPV6_ADDR_LABEL_DEFAULT)
		return -EINVAL;

407
	switch (nlh->nlmsg_type) {
408
	case RTM_NEWADDRLABEL:
409
		if (ifal->ifal_index &&
410
		    !addrlbl_ifindex_exists(net, ifal->ifal_index))
411 412
			return -EINVAL;

413
		err = ip6addrlbl_add(net, pfx, ifal->ifal_prefixlen,
414 415 416 417
				     ifal->ifal_index, label,
				     nlh->nlmsg_flags & NLM_F_REPLACE);
		break;
	case RTM_DELADDRLABEL:
418
		err = ip6addrlbl_del(net, pfx, ifal->ifal_prefixlen,
419 420 421 422 423 424 425 426
				     ifal->ifal_index);
		break;
	default:
		err = -EOPNOTSUPP;
	}
	return err;
}

427 428
static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
			      int prefixlen, int ifindex, u32 lseq)
429 430 431 432 433 434 435 436 437 438 439 440
{
	struct ifaddrlblmsg *ifal = nlmsg_data(nlh);
	ifal->ifal_family = AF_INET6;
	ifal->ifal_prefixlen = prefixlen;
	ifal->ifal_flags = 0;
	ifal->ifal_index = ifindex;
	ifal->ifal_seq = lseq;
};

static int ip6addrlbl_fill(struct sk_buff *skb,
			   struct ip6addrlbl_entry *p,
			   u32 lseq,
441
			   u32 portid, u32 seq, int event,
442 443
			   unsigned int flags)
{
444
	struct nlmsghdr *nlh = nlmsg_put(skb, portid, seq, event,
445 446 447 448 449 450
					 sizeof(struct ifaddrlblmsg), flags);
	if (!nlh)
		return -EMSGSIZE;

	ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);

451
	if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 ||
452 453 454 455 456
	    nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
		nlmsg_cancel(skb, nlh);
		return -EMSGSIZE;
	}

457 458
	nlmsg_end(skb, nlh);
	return 0;
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static int ip6addrlbl_valid_dump_req(const struct nlmsghdr *nlh,
				     struct netlink_ext_ack *extack)
{
	struct ifaddrlblmsg *ifal;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address label dump request");
		return -EINVAL;
	}

	ifal = nlmsg_data(nlh);
	if (ifal->__ifal_reserved || ifal->ifal_prefixlen ||
	    ifal->ifal_flags || ifal->ifal_index || ifal->ifal_seq) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address label dump request");
		return -EINVAL;
	}

	if (nlmsg_attrlen(nlh, sizeof(*ifal))) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid data after header for address label dump requewst");
		return -EINVAL;
	}

	return 0;
}

486 487
static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
488
	const struct nlmsghdr *nlh = cb->nlh;
489
	struct net *net = sock_net(skb->sk);
490 491 492 493
	struct ip6addrlbl_entry *p;
	int idx = 0, s_idx = cb->args[0];
	int err;

494 495 496 497 498 499
	if (cb->strict_check) {
		err = ip6addrlbl_valid_dump_req(nlh, cb->extack);
		if (err < 0)
			return err;
	}

500
	rcu_read_lock();
501 502
	hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
		if (idx >= s_idx) {
503
			err = ip6addrlbl_fill(skb, p,
504
					      net->ipv6.ip6addrlbl_table.seq,
505
					      NETLINK_CB(cb->skb).portid,
506
					      nlh->nlmsg_seq,
507 508
					      RTM_NEWADDRLABEL,
					      NLM_F_MULTI);
509
			if (err < 0)
510 511 512 513 514 515 516 517 518 519 520
				break;
		}
		idx++;
	}
	rcu_read_unlock();
	cb->args[0] = idx;
	return skb->len;
}

static inline int ip6addrlbl_msgsize(void)
{
521
	return NLMSG_ALIGN(sizeof(struct ifaddrlblmsg))
522
		+ nla_total_size(16)	/* IFAL_ADDRESS */
523
		+ nla_total_size(4);	/* IFAL_LABEL */
524 525
}

526 527
static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
528
{
529
	struct net *net = sock_net(in_skb->sk);
530 531 532 533 534 535 536 537
	struct ifaddrlblmsg *ifal;
	struct nlattr *tb[IFAL_MAX+1];
	struct in6_addr *addr;
	u32 lseq;
	int err = 0;
	struct ip6addrlbl_entry *p;
	struct sk_buff *skb;

538 539
	err = nlmsg_parse(nlh, sizeof(*ifal), tb, IFAL_MAX, ifal_policy,
			  extack);
540 541 542 543 544 545 546 547 548 549
	if (err < 0)
		return err;

	ifal = nlmsg_data(nlh);

	if (ifal->ifal_family != AF_INET6 ||
	    ifal->ifal_prefixlen != 128)
		return -EINVAL;

	if (ifal->ifal_index &&
550
	    !addrlbl_ifindex_exists(net, ifal->ifal_index))
551 552 553 554 555 556
		return -EINVAL;

	if (!tb[IFAL_ADDRESS])
		return -EINVAL;
	addr = nla_data(tb[IFAL_ADDRESS]);

557
	skb = nlmsg_new(ip6addrlbl_msgsize(), GFP_KERNEL);
558
	if (!skb)
559 560
		return -ENOBUFS;

561
	err = -ESRCH;
562

563 564 565 566 567 568 569 570 571
	rcu_read_lock();
	p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
	lseq = net->ipv6.ip6addrlbl_table.seq;
	if (p)
		err = ip6addrlbl_fill(skb, p, lseq,
				      NETLINK_CB(in_skb).portid,
				      nlh->nlmsg_seq,
				      RTM_NEWADDRLABEL, 0);
	rcu_read_unlock();
572 573 574 575

	if (err < 0) {
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
576 577
	} else {
		err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
578 579 580 581
	}
	return err;
}

582
int __init ipv6_addr_label_rtnl_register(void)
583
{
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	int ret;

	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDRLABEL,
				   ip6addrlbl_newdel,
				   NULL, RTNL_FLAG_DOIT_UNLOCKED);
	if (ret < 0)
		return ret;
	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDRLABEL,
				   ip6addrlbl_newdel,
				   NULL, RTNL_FLAG_DOIT_UNLOCKED);
	if (ret < 0)
		return ret;
	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDRLABEL,
				   ip6addrlbl_get,
				   ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED);
	return ret;
600
}