af_mpls.c 39.3 KB
Newer Older
E
Eric W. Biederman 已提交
1 2 3
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
4
#include <linux/sysctl.h>
E
Eric W. Biederman 已提交
5 6 7 8 9
#include <linux/net.h>
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/ipv6.h>
#include <linux/mpls.h>
10
#include <linux/vmalloc.h>
E
Eric W. Biederman 已提交
11 12 13 14 15 16 17
#include <net/ip.h>
#include <net/dst.h>
#include <net/sock.h>
#include <net/arp.h>
#include <net/ip_fib.h>
#include <net/netevent.h>
#include <net/netns/generic.h>
18 19 20 21
#if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h>
#include <net/addrconf.h>
#endif
R
Roopa Prabhu 已提交
22
#include <net/nexthop.h>
E
Eric W. Biederman 已提交
23 24
#include "internal.h"

25 26 27 28 29
/* Maximum number of labels to look ahead at when selecting a path of
 * a multipath route
 */
#define MAX_MP_SELECT_LABELS 4

30 31
#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)

32 33 34
static int zero = 0;
static int label_limit = (1 << 20) - 1;

35 36 37 38
static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
		       struct nlmsghdr *nlh, struct net *net, u32 portid,
		       unsigned int nlm_flags);

E
Eric W. Biederman 已提交
39 40 41 42 43 44 45 46 47 48 49 50
static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
{
	struct mpls_route *rt = NULL;

	if (index < net->mpls.platform_labels) {
		struct mpls_route __rcu **platform_label =
			rcu_dereference(net->mpls.platform_label);
		rt = rcu_dereference(platform_label[index]);
	}
	return rt;
}

R
Robert Shearman 已提交
51 52 53 54 55
static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
{
	return rcu_dereference_rtnl(dev->mpls_ptr);
}

56
bool mpls_output_possible(const struct net_device *dev)
E
Eric W. Biederman 已提交
57 58 59
{
	return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
}
60
EXPORT_SYMBOL_GPL(mpls_output_possible);
E
Eric W. Biederman 已提交
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75
static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
{
	u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
	int nh_index = nh - rt->rt_nh;

	return nh0_via + rt->rt_max_alen * nh_index;
}

static const u8 *mpls_nh_via(const struct mpls_route *rt,
			     const struct mpls_nh *nh)
{
	return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
}

R
Roopa Prabhu 已提交
76
static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
E
Eric W. Biederman 已提交
77 78
{
	/* The size of the layer 2.5 labels to be added for this route */
R
Roopa Prabhu 已提交
79
	return nh->nh_labels * sizeof(struct mpls_shim_hdr);
E
Eric W. Biederman 已提交
80 81
}

82
unsigned int mpls_dev_mtu(const struct net_device *dev)
E
Eric W. Biederman 已提交
83 84 85 86
{
	/* The amount of data the layer 2 frame can hold */
	return dev->mtu;
}
87
EXPORT_SYMBOL_GPL(mpls_dev_mtu);
E
Eric W. Biederman 已提交
88

89
bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
E
Eric W. Biederman 已提交
90 91 92 93
{
	if (skb->len <= mtu)
		return false;

94
	if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
E
Eric W. Biederman 已提交
95 96 97 98
		return false;

	return true;
}
99
EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
E
Eric W. Biederman 已提交
100

R
Roopa Prabhu 已提交
101 102
static u32 mpls_multipath_hash(struct mpls_route *rt,
			       struct sk_buff *skb, bool bos)
R
Roopa Prabhu 已提交
103
{
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	struct mpls_entry_decoded dec;
	struct mpls_shim_hdr *hdr;
	bool eli_seen = false;
	int label_index;
	u32 hash = 0;

	for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
	     label_index++) {
		if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
			break;

		/* Read and decode the current label */
		hdr = mpls_hdr(skb) + label_index;
		dec = mpls_entry_decode(hdr);

		/* RFC6790 - reserved labels MUST NOT be used as keys
		 * for the load-balancing function
		 */
		if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
			hash = jhash_1word(dec.label, hash);

			/* The entropy label follows the entropy label
			 * indicator, so this means that the entropy
			 * label was just added to the hash - no need to
			 * go any deeper either in the label stack or in the
			 * payload
			 */
			if (eli_seen)
				break;
		} else if (dec.label == MPLS_LABEL_ENTROPY) {
			eli_seen = true;
		}

		bos = dec.bos;
		if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
					 sizeof(struct iphdr))) {
			const struct iphdr *v4hdr;

			v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
						       label_index);
			if (v4hdr->version == 4) {
				hash = jhash_3words(ntohl(v4hdr->saddr),
						    ntohl(v4hdr->daddr),
						    v4hdr->protocol, hash);
			} else if (v4hdr->version == 6 &&
				pskb_may_pull(skb, sizeof(*hdr) * label_index +
					      sizeof(struct ipv6hdr))) {
				const struct ipv6hdr *v6hdr;

				v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
								label_index);

				hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
				hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
				hash = jhash_1word(v6hdr->nexthdr, hash);
			}
		}
	}

R
Roopa Prabhu 已提交
163 164 165 166 167 168 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
	return hash;
}

static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
					     struct sk_buff *skb, bool bos)
{
	int alive = ACCESS_ONCE(rt->rt_nhn_alive);
	u32 hash = 0;
	int nh_index = 0;
	int n = 0;

	/* No need to look further into packet if there's only
	 * one path
	 */
	if (rt->rt_nhn == 1)
		goto out;

	if (alive <= 0)
		return NULL;

	hash = mpls_multipath_hash(rt, skb, bos);
	nh_index = hash % alive;
	if (alive == rt->rt_nhn)
		goto out;
	for_nexthops(rt) {
		if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
			continue;
		if (n == nh_index)
			return nh;
		n++;
	} endfor_nexthops(rt);

195 196
out:
	return &rt->rt_nh[nh_index];
R
Roopa Prabhu 已提交
197 198
}

E
Eric W. Biederman 已提交
199 200 201
static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
			struct mpls_entry_decoded dec)
{
202 203
	enum mpls_payload_type payload_type;
	bool success = false;
E
Eric W. Biederman 已提交
204

205 206 207 208 209 210 211 212 213 214 215 216 217
	/* The IPv4 code below accesses through the IPv4 header
	 * checksum, which is 12 bytes into the packet.
	 * The IPv6 code below accesses through the IPv6 hop limit
	 * which is 8 bytes into the packet.
	 *
	 * For all supported cases there should always be at least 12
	 * bytes of packet data present.  The IPv4 header is 20 bytes
	 * without options and the IPv6 header is always 40 bytes
	 * long.
	 */
	if (!pskb_may_pull(skb, 12))
		return false;

218 219 220 221 222 223 224
	payload_type = rt->rt_payload_type;
	if (payload_type == MPT_UNSPEC)
		payload_type = ip_hdr(skb)->version;

	switch (payload_type) {
	case MPT_IPV4: {
		struct iphdr *hdr4 = ip_hdr(skb);
E
Eric W. Biederman 已提交
225 226 227 228 229
		skb->protocol = htons(ETH_P_IP);
		csum_replace2(&hdr4->check,
			      htons(hdr4->ttl << 8),
			      htons(dec.ttl << 8));
		hdr4->ttl = dec.ttl;
230 231
		success = true;
		break;
E
Eric W. Biederman 已提交
232
	}
233
	case MPT_IPV6: {
E
Eric W. Biederman 已提交
234 235 236
		struct ipv6hdr *hdr6 = ipv6_hdr(skb);
		skb->protocol = htons(ETH_P_IPV6);
		hdr6->hop_limit = dec.ttl;
237 238
		success = true;
		break;
E
Eric W. Biederman 已提交
239
	}
240 241 242 243
	case MPT_UNSPEC:
		break;
	}

E
Eric W. Biederman 已提交
244 245 246 247 248 249 250 251 252
	return success;
}

static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
			struct packet_type *pt, struct net_device *orig_dev)
{
	struct net *net = dev_net(dev);
	struct mpls_shim_hdr *hdr;
	struct mpls_route *rt;
R
Roopa Prabhu 已提交
253
	struct mpls_nh *nh;
E
Eric W. Biederman 已提交
254 255
	struct mpls_entry_decoded dec;
	struct net_device *out_dev;
R
Robert Shearman 已提交
256
	struct mpls_dev *mdev;
E
Eric W. Biederman 已提交
257 258 259 260 261 262 263
	unsigned int hh_len;
	unsigned int new_header_size;
	unsigned int mtu;
	int err;

	/* Careful this entire function runs inside of an rcu critical section */

R
Robert Shearman 已提交
264
	mdev = mpls_dev_get(dev);
265
	if (!mdev || !mdev->input_enabled)
R
Robert Shearman 已提交
266 267
		goto drop;

E
Eric W. Biederman 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	if (skb->pkt_type != PACKET_HOST)
		goto drop;

	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
		goto drop;

	if (!pskb_may_pull(skb, sizeof(*hdr)))
		goto drop;

	/* Read and decode the label */
	hdr = mpls_hdr(skb);
	dec = mpls_entry_decode(hdr);

	/* Pop the label */
	skb_pull(skb, sizeof(*hdr));
	skb_reset_network_header(skb);

	skb_orphan(skb);

	rt = mpls_route_input_rcu(net, dec.label);
	if (!rt)
		goto drop;

291
	nh = mpls_select_multipath(rt, skb, dec.bos);
R
Roopa Prabhu 已提交
292 293 294
	if (!nh)
		goto drop;

E
Eric W. Biederman 已提交
295
	/* Find the output device */
R
Roopa Prabhu 已提交
296
	out_dev = rcu_dereference(nh->nh_dev);
E
Eric W. Biederman 已提交
297 298 299 300 301 302 303 304 305
	if (!mpls_output_possible(out_dev))
		goto drop;

	if (skb_warn_if_lro(skb))
		goto drop;

	skb_forward_csum(skb);

	/* Verify ttl is valid */
306
	if (dec.ttl <= 1)
E
Eric W. Biederman 已提交
307 308 309 310
		goto drop;
	dec.ttl -= 1;

	/* Verify the destination can hold the packet */
R
Roopa Prabhu 已提交
311
	new_header_size = mpls_nh_header_size(nh);
E
Eric W. Biederman 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	mtu = mpls_dev_mtu(out_dev);
	if (mpls_pkt_too_big(skb, mtu - new_header_size))
		goto drop;

	hh_len = LL_RESERVED_SPACE(out_dev);
	if (!out_dev->header_ops)
		hh_len = 0;

	/* Ensure there is enough space for the headers in the skb */
	if (skb_cow(skb, hh_len + new_header_size))
		goto drop;

	skb->dev = out_dev;
	skb->protocol = htons(ETH_P_MPLS_UC);

	if (unlikely(!new_header_size && dec.bos)) {
		/* Penultimate hop popping */
		if (!mpls_egress(rt, skb, dec))
			goto drop;
	} else {
		bool bos;
		int i;
		skb_push(skb, new_header_size);
		skb_reset_network_header(skb);
		/* Push the new labels */
		hdr = mpls_hdr(skb);
		bos = dec.bos;
R
Roopa Prabhu 已提交
339 340 341
		for (i = nh->nh_labels - 1; i >= 0; i--) {
			hdr[i] = mpls_entry_encode(nh->nh_label[i],
						   dec.ttl, 0, bos);
E
Eric W. Biederman 已提交
342 343 344 345
			bos = false;
		}
	}

346 347 348 349 350 351 352
	/* If via wasn't specified then send out using device address */
	if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
		err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
				 out_dev->dev_addr, skb);
	else
		err = neigh_xmit(nh->nh_via_table, out_dev,
				 mpls_nh_via(rt, nh), skb);
E
Eric W. Biederman 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	if (err)
		net_dbg_ratelimited("%s: packet transmission failed: %d\n",
				    __func__, err);
	return 0;

drop:
	kfree_skb(skb);
	return NET_RX_DROP;
}

static struct packet_type mpls_packet_type __read_mostly = {
	.type = cpu_to_be16(ETH_P_MPLS_UC),
	.func = mpls_forward,
};

368
static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
369 370 371 372
	[RTA_DST]		= { .type = NLA_U32 },
	[RTA_OIF]		= { .type = NLA_U32 },
};

373
struct mpls_route_config {
374 375
	u32			rc_protocol;
	u32			rc_ifindex;
R
Roopa Prabhu 已提交
376 377
	u8			rc_via_table;
	u8			rc_via_alen;
378 379
	u8			rc_via[MAX_VIA_ALEN];
	u32			rc_label;
R
Roopa Prabhu 已提交
380
	u8			rc_output_labels;
381 382 383 384
	u32			rc_output_label[MAX_NEW_LABELS];
	u32			rc_nlflags;
	enum mpls_payload_type	rc_payload_type;
	struct nl_info		rc_nlinfo;
R
Roopa Prabhu 已提交
385 386
	struct rtnexthop	*rc_mp;
	int			rc_mp_len;
387 388
};

389
static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
E
Eric W. Biederman 已提交
390
{
391
	u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
E
Eric W. Biederman 已提交
392 393
	struct mpls_route *rt;

394 395 396
	rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
			   VIA_ALEN_ALIGN) +
		     num_nh * max_alen_aligned,
R
Roopa Prabhu 已提交
397
		     GFP_KERNEL);
398
	if (rt) {
R
Roopa Prabhu 已提交
399
		rt->rt_nhn = num_nh;
R
Roopa Prabhu 已提交
400
		rt->rt_nhn_alive = num_nh;
401 402
		rt->rt_max_alen = max_alen_aligned;
	}
R
Roopa Prabhu 已提交
403

E
Eric W. Biederman 已提交
404 405 406 407 408 409 410 411 412
	return rt;
}

static void mpls_rt_free(struct mpls_route *rt)
{
	if (rt)
		kfree_rcu(rt, rt_rcu);
}

413 414 415 416 417 418 419 420 421 422
static void mpls_notify_route(struct net *net, unsigned index,
			      struct mpls_route *old, struct mpls_route *new,
			      const struct nl_info *info)
{
	struct nlmsghdr *nlh = info ? info->nlh : NULL;
	unsigned portid = info ? info->portid : 0;
	int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
	struct mpls_route *rt = new ? new : old;
	unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
	/* Ignore reserved labels for now */
423
	if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
424 425 426
		rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
}

E
Eric W. Biederman 已提交
427
static void mpls_route_update(struct net *net, unsigned index,
R
Roopa Prabhu 已提交
428
			      struct mpls_route *new,
E
Eric W. Biederman 已提交
429 430
			      const struct nl_info *info)
{
431
	struct mpls_route __rcu **platform_label;
R
Roopa Prabhu 已提交
432
	struct mpls_route *rt;
E
Eric W. Biederman 已提交
433 434 435

	ASSERT_RTNL();

436 437
	platform_label = rtnl_dereference(net->mpls.platform_label);
	rt = rtnl_dereference(platform_label[index]);
R
Roopa Prabhu 已提交
438
	rcu_assign_pointer(platform_label[index], new);
E
Eric W. Biederman 已提交
439

R
Roopa Prabhu 已提交
440
	mpls_notify_route(net, index, rt, new, info);
441

E
Eric W. Biederman 已提交
442
	/* If we removed a route free it now */
R
Roopa Prabhu 已提交
443
	mpls_rt_free(rt);
E
Eric W. Biederman 已提交
444 445
}

446 447
static unsigned find_free_label(struct net *net)
{
448 449
	struct mpls_route __rcu **platform_label;
	size_t platform_labels;
450
	unsigned index;
451 452 453

	platform_label = rtnl_dereference(net->mpls.platform_label);
	platform_labels = net->mpls.platform_labels;
454 455
	for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
	     index++) {
456
		if (!rtnl_dereference(platform_label[index]))
457 458 459 460 461
			return index;
	}
	return LABEL_NOT_SPECIFIED;
}

462
#if IS_ENABLED(CONFIG_INET)
463 464
static struct net_device *inet_fib_lookup_dev(struct net *net,
					      const void *addr)
R
Roopa Prabhu 已提交
465
{
466
	struct net_device *dev;
R
Roopa Prabhu 已提交
467 468 469 470 471 472
	struct rtable *rt;
	struct in_addr daddr;

	memcpy(&daddr, addr, sizeof(struct in_addr));
	rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
	if (IS_ERR(rt))
473
		return ERR_CAST(rt);
R
Roopa Prabhu 已提交
474 475 476 477 478 479 480

	dev = rt->dst.dev;
	dev_hold(dev);

	ip_rt_put(rt);

	return dev;
481 482
}
#else
483 484
static struct net_device *inet_fib_lookup_dev(struct net *net,
					      const void *addr)
485 486
{
	return ERR_PTR(-EAFNOSUPPORT);
R
Roopa Prabhu 已提交
487
}
488
#endif
R
Roopa Prabhu 已提交
489

490
#if IS_ENABLED(CONFIG_IPV6)
491 492
static struct net_device *inet6_fib_lookup_dev(struct net *net,
					       const void *addr)
R
Roopa Prabhu 已提交
493
{
494
	struct net_device *dev;
R
Roopa Prabhu 已提交
495 496
	struct dst_entry *dst;
	struct flowi6 fl6;
497 498 499 500
	int err;

	if (!ipv6_stub)
		return ERR_PTR(-EAFNOSUPPORT);
R
Roopa Prabhu 已提交
501 502 503

	memset(&fl6, 0, sizeof(fl6));
	memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
504 505
	err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
	if (err)
506
		return ERR_PTR(err);
R
Roopa Prabhu 已提交
507 508 509 510 511 512 513

	dev = dst->dev;
	dev_hold(dev);
	dst_release(dst);

	return dev;
}
514
#else
515 516
static struct net_device *inet6_fib_lookup_dev(struct net *net,
					       const void *addr)
517 518 519 520
{
	return ERR_PTR(-EAFNOSUPPORT);
}
#endif
R
Roopa Prabhu 已提交
521 522

static struct net_device *find_outdev(struct net *net,
523
				      struct mpls_route *rt,
R
Roopa Prabhu 已提交
524
				      struct mpls_nh *nh, int oif)
R
Roopa Prabhu 已提交
525 526 527
{
	struct net_device *dev = NULL;

R
Roopa Prabhu 已提交
528 529
	if (!oif) {
		switch (nh->nh_via_table) {
R
Roopa Prabhu 已提交
530
		case NEIGH_ARP_TABLE:
531
			dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
R
Roopa Prabhu 已提交
532 533
			break;
		case NEIGH_ND_TABLE:
534
			dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
R
Roopa Prabhu 已提交
535 536 537 538 539
			break;
		case NEIGH_LINK_TABLE:
			break;
		}
	} else {
R
Roopa Prabhu 已提交
540
		dev = dev_get_by_index(net, oif);
R
Roopa Prabhu 已提交
541 542
	}

543 544 545
	if (!dev)
		return ERR_PTR(-ENODEV);

546 547 548
	if (IS_ERR(dev))
		return dev;

R
Roopa Prabhu 已提交
549 550 551
	/* The caller is holding rtnl anyways, so release the dev reference */
	dev_put(dev);

R
Roopa Prabhu 已提交
552 553 554
	return dev;
}

555 556
static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
			      struct mpls_nh *nh, int oif)
R
Roopa Prabhu 已提交
557 558 559 560
{
	struct net_device *dev = NULL;
	int err = -ENODEV;

561
	dev = find_outdev(net, rt, nh, oif);
R
Roopa Prabhu 已提交
562 563 564 565 566 567 568 569 570 571 572
	if (IS_ERR(dev)) {
		err = PTR_ERR(dev);
		dev = NULL;
		goto errout;
	}

	/* Ensure this is a supported device */
	err = -EINVAL;
	if (!mpls_dev_get(dev))
		goto errout;

573 574 575 576
	if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
	    (dev->addr_len != nh->nh_via_alen))
		goto errout;

R
Roopa Prabhu 已提交
577 578
	RCU_INIT_POINTER(nh->nh_dev, dev);

R
Roopa Prabhu 已提交
579 580 581 582 583 584 585 586 587 588
	if (!(dev->flags & IFF_UP)) {
		nh->nh_flags |= RTNH_F_DEAD;
	} else {
		unsigned int flags;

		flags = dev_get_flags(dev);
		if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
			nh->nh_flags |= RTNH_F_LINKDOWN;
	}

R
Roopa Prabhu 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	return 0;

errout:
	return err;
}

static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
				  struct mpls_route *rt)
{
	struct net *net = cfg->rc_nlinfo.nl_net;
	struct mpls_nh *nh = rt->rt_nh;
	int err;
	int i;

	if (!nh)
		return -ENOMEM;

	err = -EINVAL;
	/* Ensure only a supported number of labels are present */
	if (cfg->rc_output_labels > MAX_NEW_LABELS)
		goto errout;

	nh->nh_labels = cfg->rc_output_labels;
	for (i = 0; i < nh->nh_labels; i++)
		nh->nh_label[i] = cfg->rc_output_label[i];

	nh->nh_via_table = cfg->rc_via_table;
616
	memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
R
Roopa Prabhu 已提交
617 618
	nh->nh_via_alen = cfg->rc_via_alen;

619
	err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
R
Roopa Prabhu 已提交
620 621 622
	if (err)
		goto errout;

R
Roopa Prabhu 已提交
623 624 625
	if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
		rt->rt_nhn_alive--;

R
Roopa Prabhu 已提交
626 627 628 629 630 631
	return 0;

errout:
	return err;
}

632
static int mpls_nh_build(struct net *net, struct mpls_route *rt,
R
Roopa Prabhu 已提交
633 634
			 struct mpls_nh *nh, int oif, struct nlattr *via,
			 struct nlattr *newdst)
R
Roopa Prabhu 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647
{
	int err = -ENOMEM;

	if (!nh)
		goto errout;

	if (newdst) {
		err = nla_get_labels(newdst, MAX_NEW_LABELS,
				     &nh->nh_labels, nh->nh_label);
		if (err)
			goto errout;
	}

648 649 650 651 652 653 654 655
	if (via) {
		err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
				  __mpls_nh_via(rt, nh));
		if (err)
			goto errout;
	} else {
		nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
	}
R
Roopa Prabhu 已提交
656

657
	err = mpls_nh_assign_dev(net, rt, nh, oif);
R
Roopa Prabhu 已提交
658 659 660 661 662 663 664 665 666
	if (err)
		goto errout;

	return 0;

errout:
	return err;
}

667 668
static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
			       u8 cfg_via_alen, u8 *max_via_alen)
R
Roopa Prabhu 已提交
669 670 671 672
{
	int nhs = 0;
	int remaining = len;

673 674 675 676 677 678 679
	if (!rtnh) {
		*max_via_alen = cfg_via_alen;
		return 1;
	}

	*max_via_alen = 0;

R
Roopa Prabhu 已提交
680
	while (rtnh_ok(rtnh, remaining)) {
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
		struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
		int attrlen;

		attrlen = rtnh_attrlen(rtnh);
		nla = nla_find(attrs, attrlen, RTA_VIA);
		if (nla && nla_len(nla) >=
		    offsetof(struct rtvia, rtvia_addr)) {
			int via_alen = nla_len(nla) -
				offsetof(struct rtvia, rtvia_addr);

			if (via_alen <= MAX_VIA_ALEN)
				*max_via_alen = max_t(u16, *max_via_alen,
						      via_alen);
		}

R
Roopa Prabhu 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		nhs++;
		rtnh = rtnh_next(rtnh, &remaining);
	}

	/* leftover implies invalid nexthop configuration, discard it */
	return remaining > 0 ? 0 : nhs;
}

static int mpls_nh_build_multi(struct mpls_route_config *cfg,
			       struct mpls_route *rt)
{
	struct rtnexthop *rtnh = cfg->rc_mp;
	struct nlattr *nla_via, *nla_newdst;
	int remaining = cfg->rc_mp_len;
	int nhs = 0;
	int err = 0;

	change_nexthops(rt) {
		int attrlen;

		nla_via = NULL;
		nla_newdst = NULL;

		err = -EINVAL;
		if (!rtnh_ok(rtnh, remaining))
			goto errout;

723 724 725 726 727 728
		/* neither weighted multipath nor any flags
		 * are supported
		 */
		if (rtnh->rtnh_hops || rtnh->rtnh_flags)
			goto errout;

R
Roopa Prabhu 已提交
729 730 731 732 733 734 735 736
		attrlen = rtnh_attrlen(rtnh);
		if (attrlen > 0) {
			struct nlattr *attrs = rtnh_attrs(rtnh);

			nla_via = nla_find(attrs, attrlen, RTA_VIA);
			nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
		}

737
		err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
R
Roopa Prabhu 已提交
738
				    rtnh->rtnh_ifindex, nla_via, nla_newdst);
R
Roopa Prabhu 已提交
739 740 741
		if (err)
			goto errout;

R
Roopa Prabhu 已提交
742 743 744
		if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
			rt->rt_nhn_alive--;

R
Roopa Prabhu 已提交
745 746 747 748 749 750 751 752 753 754 755 756
		rtnh = rtnh_next(rtnh, &remaining);
		nhs++;
	} endfor_nexthops(rt);

	rt->rt_nhn = nhs;

	return 0;

errout:
	return err;
}

757 758
static int mpls_route_add(struct mpls_route_config *cfg)
{
759
	struct mpls_route __rcu **platform_label;
760 761 762
	struct net *net = cfg->rc_nlinfo.nl_net;
	struct mpls_route *rt, *old;
	int err = -EINVAL;
763
	u8 max_via_alen;
R
Roopa Prabhu 已提交
764
	unsigned index;
765
	int nhs;
766 767 768 769 770 771 772 773 774

	index = cfg->rc_label;

	/* If a label was not specified during insert pick one */
	if ((index == LABEL_NOT_SPECIFIED) &&
	    (cfg->rc_nlflags & NLM_F_CREATE)) {
		index = find_free_label(net);
	}

775 776
	/* Reserved labels may not be set */
	if (index < MPLS_LABEL_FIRST_UNRESERVED)
777 778 779 780 781 782 783
		goto errout;

	/* The full 20 bit range may not be supported. */
	if (index >= net->mpls.platform_labels)
		goto errout;

	/* Append makes no sense with mpls */
784
	err = -EOPNOTSUPP;
785 786 787 788
	if (cfg->rc_nlflags & NLM_F_APPEND)
		goto errout;

	err = -EEXIST;
789 790
	platform_label = rtnl_dereference(net->mpls.platform_label);
	old = rtnl_dereference(platform_label[index]);
791 792 793 794 795 796 797 798 799 800 801
	if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
		goto errout;

	err = -EEXIST;
	if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
		goto errout;

	err = -ENOENT;
	if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
		goto errout;

802 803 804 805 806
	err = -EINVAL;
	nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
				  cfg->rc_via_alen, &max_via_alen);
	if (nhs == 0)
		goto errout;
R
Roopa Prabhu 已提交
807

808
	err = -ENOMEM;
809
	rt = mpls_rt_alloc(nhs, max_via_alen);
810 811 812 813
	if (!rt)
		goto errout;

	rt->rt_protocol = cfg->rc_protocol;
814
	rt->rt_payload_type = cfg->rc_payload_type;
815

R
Roopa Prabhu 已提交
816 817 818 819 820 821 822 823
	if (cfg->rc_mp)
		err = mpls_nh_build_multi(cfg, rt);
	else
		err = mpls_nh_build_from_cfg(cfg, rt);
	if (err)
		goto freert;

	mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
824 825 826

	return 0;

R
Roopa Prabhu 已提交
827 828
freert:
	mpls_rt_free(rt);
829 830 831 832 833 834 835 836 837 838 839 840
errout:
	return err;
}

static int mpls_route_del(struct mpls_route_config *cfg)
{
	struct net *net = cfg->rc_nlinfo.nl_net;
	unsigned index;
	int err = -EINVAL;

	index = cfg->rc_label;

841 842
	/* Reserved labels may not be removed */
	if (index < MPLS_LABEL_FIRST_UNRESERVED)
843 844 845 846 847 848
		goto errout;

	/* The full 20 bit range may not be supported */
	if (index >= net->mpls.platform_labels)
		goto errout;

R
Roopa Prabhu 已提交
849
	mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
850 851 852 853 854 855

	err = 0;
errout:
	return err;
}

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
#define MPLS_PERDEV_SYSCTL_OFFSET(field)	\
	(&((struct mpls_dev *)0)->field)

static const struct ctl_table mpls_dev_table[] = {
	{
		.procname	= "input",
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec,
		.data		= MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
	},
	{ }
};

static int mpls_dev_sysctl_register(struct net_device *dev,
				    struct mpls_dev *mdev)
{
	char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
	struct ctl_table *table;
	int i;

	table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
	if (!table)
		goto out;

	/* Table data contains only offsets relative to the base of
	 * the mdev at this point, so make them absolute.
	 */
	for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
		table[i].data = (char *)mdev + (uintptr_t)table[i].data;

	snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);

	mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
	if (!mdev->sysctl)
		goto free;

	return 0;

free:
	kfree(table);
out:
	return -ENOBUFS;
}

static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
{
	struct ctl_table *table;

	table = mdev->sysctl->ctl_table_arg;
	unregister_net_sysctl_table(mdev->sysctl);
	kfree(table);
}

R
Robert Shearman 已提交
910 911 912 913 914 915 916 917 918 919 920
static struct mpls_dev *mpls_add_dev(struct net_device *dev)
{
	struct mpls_dev *mdev;
	int err = -ENOMEM;

	ASSERT_RTNL();

	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
	if (!mdev)
		return ERR_PTR(err);

921 922 923 924
	err = mpls_dev_sysctl_register(dev, mdev);
	if (err)
		goto free;

R
Robert Shearman 已提交
925 926 927
	rcu_assign_pointer(dev->mpls_ptr, mdev);

	return mdev;
928 929 930 931

free:
	kfree(mdev);
	return ERR_PTR(err);
R
Robert Shearman 已提交
932 933
}

R
Roopa Prabhu 已提交
934
static void mpls_ifdown(struct net_device *dev, int event)
E
Eric W. Biederman 已提交
935
{
936
	struct mpls_route __rcu **platform_label;
E
Eric W. Biederman 已提交
937 938 939
	struct net *net = dev_net(dev);
	unsigned index;

940
	platform_label = rtnl_dereference(net->mpls.platform_label);
E
Eric W. Biederman 已提交
941
	for (index = 0; index < net->mpls.platform_labels; index++) {
942
		struct mpls_route *rt = rtnl_dereference(platform_label[index]);
R
Roopa Prabhu 已提交
943

E
Eric W. Biederman 已提交
944 945
		if (!rt)
			continue;
R
Roopa Prabhu 已提交
946 947

		change_nexthops(rt) {
R
Roopa Prabhu 已提交
948 949
			if (rtnl_dereference(nh->nh_dev) != dev)
				continue;
R
Roopa Prabhu 已提交
950 951 952 953 954 955 956 957 958 959 960 961
			switch (event) {
			case NETDEV_DOWN:
			case NETDEV_UNREGISTER:
				nh->nh_flags |= RTNH_F_DEAD;
				/* fall through */
			case NETDEV_CHANGE:
				nh->nh_flags |= RTNH_F_LINKDOWN;
				ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
				break;
			}
			if (event == NETDEV_UNREGISTER)
				RCU_INIT_POINTER(nh->nh_dev, NULL);
R
Roopa Prabhu 已提交
962
		} endfor_nexthops(rt);
E
Eric W. Biederman 已提交
963
	}
R
Roopa Prabhu 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}

static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
{
	struct mpls_route __rcu **platform_label;
	struct net *net = dev_net(dev);
	unsigned index;
	int alive;

	platform_label = rtnl_dereference(net->mpls.platform_label);
	for (index = 0; index < net->mpls.platform_labels; index++) {
		struct mpls_route *rt = rtnl_dereference(platform_label[index]);

		if (!rt)
			continue;
979

R
Roopa Prabhu 已提交
980 981 982 983
		alive = 0;
		change_nexthops(rt) {
			struct net_device *nh_dev =
				rtnl_dereference(nh->nh_dev);
R
Robert Shearman 已提交
984

R
Roopa Prabhu 已提交
985 986 987 988 989 990 991 992 993 994 995 996
			if (!(nh->nh_flags & nh_flags)) {
				alive++;
				continue;
			}
			if (nh_dev != dev)
				continue;
			alive++;
			nh->nh_flags &= ~nh_flags;
		} endfor_nexthops(rt);

		ACCESS_ONCE(rt->rt_nhn_alive) = alive;
	}
E
Eric W. Biederman 已提交
997 998 999 1000 1001 1002
}

static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
			   void *ptr)
{
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
R
Robert Shearman 已提交
1003
	struct mpls_dev *mdev;
R
Roopa Prabhu 已提交
1004
	unsigned int flags;
E
Eric W. Biederman 已提交
1005

R
Roopa Prabhu 已提交
1006
	if (event == NETDEV_REGISTER) {
1007
		/* For now just support Ethernet, IPGRE, SIT and IPIP devices */
1008 1009
		if (dev->type == ARPHRD_ETHER ||
		    dev->type == ARPHRD_LOOPBACK ||
1010 1011 1012
		    dev->type == ARPHRD_IPGRE ||
		    dev->type == ARPHRD_SIT ||
		    dev->type == ARPHRD_TUNNEL) {
R
Robert Shearman 已提交
1013 1014 1015 1016
			mdev = mpls_add_dev(dev);
			if (IS_ERR(mdev))
				return notifier_from_errno(PTR_ERR(mdev));
		}
R
Roopa Prabhu 已提交
1017 1018
		return NOTIFY_OK;
	}
R
Robert Shearman 已提交
1019

R
Roopa Prabhu 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	mdev = mpls_dev_get(dev);
	if (!mdev)
		return NOTIFY_OK;

	switch (event) {
	case NETDEV_DOWN:
		mpls_ifdown(dev, event);
		break;
	case NETDEV_UP:
		flags = dev_get_flags(dev);
		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
			mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
		else
			mpls_ifup(dev, RTNH_F_DEAD);
		break;
	case NETDEV_CHANGE:
		flags = dev_get_flags(dev);
		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
			mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
		else
			mpls_ifdown(dev, event);
		break;
E
Eric W. Biederman 已提交
1042
	case NETDEV_UNREGISTER:
R
Roopa Prabhu 已提交
1043 1044 1045 1046 1047 1048 1049
		mpls_ifdown(dev, event);
		mdev = mpls_dev_get(dev);
		if (mdev) {
			mpls_dev_sysctl_unregister(mdev);
			RCU_INIT_POINTER(dev->mpls_ptr, NULL);
			kfree_rcu(mdev, rcu);
		}
E
Eric W. Biederman 已提交
1050
		break;
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	case NETDEV_CHANGENAME:
		mdev = mpls_dev_get(dev);
		if (mdev) {
			int err;

			mpls_dev_sysctl_unregister(mdev);
			err = mpls_dev_sysctl_register(dev, mdev);
			if (err)
				return notifier_from_errno(err);
		}
		break;
E
Eric W. Biederman 已提交
1062 1063 1064 1065 1066 1067 1068 1069
	}
	return NOTIFY_OK;
}

static struct notifier_block mpls_dev_notifier = {
	.notifier_call = mpls_dev_notify,
};

1070
static int nla_put_via(struct sk_buff *skb,
1071
		       u8 table, const void *addr, int alen)
1072
{
1073 1074 1075
	static const int table_to_family[NEIGH_NR_TABLES + 1] = {
		AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
	};
1076 1077
	struct nlattr *nla;
	struct rtvia *via;
1078
	int family = AF_UNSPEC;
1079 1080 1081 1082 1083

	nla = nla_reserve(skb, RTA_VIA, alen + 2);
	if (!nla)
		return -EMSGSIZE;

1084 1085 1086
	if (table <= NEIGH_NR_TABLES)
		family = table_to_family[table];

1087 1088 1089 1090 1091 1092
	via = nla_data(nla);
	via->rtvia_family = family;
	memcpy(via->rtvia_addr, addr, alen);
	return 0;
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
int nla_put_labels(struct sk_buff *skb, int attrtype,
		   u8 labels, const u32 label[])
{
	struct nlattr *nla;
	struct mpls_shim_hdr *nla_label;
	bool bos;
	int i;
	nla = nla_reserve(skb, attrtype, labels*4);
	if (!nla)
		return -EMSGSIZE;

	nla_label = nla_data(nla);
	bos = true;
	for (i = labels - 1; i >= 0; i--) {
		nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
		bos = false;
	}

	return 0;
}
1113
EXPORT_SYMBOL_GPL(nla_put_labels);
1114 1115

int nla_get_labels(const struct nlattr *nla,
R
Roopa Prabhu 已提交
1116
		   u32 max_labels, u8 *labels, u32 label[])
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
{
	unsigned len = nla_len(nla);
	unsigned nla_labels;
	struct mpls_shim_hdr *nla_label;
	bool bos;
	int i;

	/* len needs to be an even multiple of 4 (the label size) */
	if (len & 3)
		return -EINVAL;

	/* Limit the number of new labels allowed */
	nla_labels = len/4;
	if (nla_labels > max_labels)
		return -EINVAL;

	nla_label = nla_data(nla);
	bos = true;
	for (i = nla_labels - 1; i >= 0; i--, bos = false) {
		struct mpls_entry_decoded dec;
		dec = mpls_entry_decode(nla_label + i);

		/* Ensure the bottom of stack flag is properly set
		 * and ttl and tc are both clear.
		 */
		if ((dec.bos != bos) || dec.ttl || dec.tc)
			return -EINVAL;

1145
		switch (dec.label) {
1146
		case MPLS_LABEL_IMPLNULL:
1147 1148 1149 1150 1151 1152 1153
			/* RFC3032: This is a label that an LSR may
			 * assign and distribute, but which never
			 * actually appears in the encapsulation.
			 */
			return -EINVAL;
		}

1154 1155 1156 1157 1158
		label[i] = dec.label;
	}
	*labels = nla_labels;
	return 0;
}
1159
EXPORT_SYMBOL_GPL(nla_get_labels);
1160

R
Roopa Prabhu 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
int nla_get_via(const struct nlattr *nla, u8 *via_alen,
		u8 *via_table, u8 via_addr[])
{
	struct rtvia *via = nla_data(nla);
	int err = -EINVAL;
	int alen;

	if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
		goto errout;
	alen = nla_len(nla) -
			offsetof(struct rtvia, rtvia_addr);
	if (alen > MAX_VIA_ALEN)
		goto errout;

	/* Validate the address family */
	switch (via->rtvia_family) {
	case AF_PACKET:
		*via_table = NEIGH_LINK_TABLE;
		break;
	case AF_INET:
		*via_table = NEIGH_ARP_TABLE;
		if (alen != 4)
			goto errout;
		break;
	case AF_INET6:
		*via_table = NEIGH_ND_TABLE;
		if (alen != 16)
			goto errout;
		break;
	default:
		/* Unsupported address family */
		goto errout;
	}

	memcpy(via_addr, via->rtvia_addr, alen);
	*via_alen = alen;
	err = 0;

errout:
	return err;
}

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
static int rtm_to_route_config(struct sk_buff *skb,  struct nlmsghdr *nlh,
			       struct mpls_route_config *cfg)
{
	struct rtmsg *rtm;
	struct nlattr *tb[RTA_MAX+1];
	int index;
	int err;

	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
	if (err < 0)
		goto errout;

	err = -EINVAL;
	rtm = nlmsg_data(nlh);
	memset(cfg, 0, sizeof(*cfg));

	if (rtm->rtm_family != AF_MPLS)
		goto errout;
	if (rtm->rtm_dst_len != 20)
		goto errout;
	if (rtm->rtm_src_len != 0)
		goto errout;
	if (rtm->rtm_tos != 0)
		goto errout;
	if (rtm->rtm_table != RT_TABLE_MAIN)
		goto errout;
	/* Any value is acceptable for rtm_protocol */

	/* As mpls uses destination specific addresses
	 * (or source specific address in the case of multicast)
	 * all addresses have universal scope.
	 */
	if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
		goto errout;
	if (rtm->rtm_type != RTN_UNICAST)
		goto errout;
	if (rtm->rtm_flags != 0)
		goto errout;

	cfg->rc_label		= LABEL_NOT_SPECIFIED;
	cfg->rc_protocol	= rtm->rtm_protocol;
1244
	cfg->rc_via_table	= MPLS_NEIGH_TABLE_UNSPEC;
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	cfg->rc_nlflags		= nlh->nlmsg_flags;
	cfg->rc_nlinfo.portid	= NETLINK_CB(skb).portid;
	cfg->rc_nlinfo.nlh	= nlh;
	cfg->rc_nlinfo.nl_net	= sock_net(skb->sk);

	for (index = 0; index <= RTA_MAX; index++) {
		struct nlattr *nla = tb[index];
		if (!nla)
			continue;

		switch(index) {
		case RTA_OIF:
			cfg->rc_ifindex = nla_get_u32(nla);
			break;
		case RTA_NEWDST:
			if (nla_get_labels(nla, MAX_NEW_LABELS,
					   &cfg->rc_output_labels,
					   cfg->rc_output_label))
				goto errout;
			break;
		case RTA_DST:
		{
R
Roopa Prabhu 已提交
1267
			u8 label_count;
1268 1269 1270 1271
			if (nla_get_labels(nla, 1, &label_count,
					   &cfg->rc_label))
				goto errout;

1272 1273
			/* Reserved labels may not be set */
			if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1274 1275 1276 1277 1278 1279
				goto errout;

			break;
		}
		case RTA_VIA:
		{
R
Roopa Prabhu 已提交
1280 1281
			if (nla_get_via(nla, &cfg->rc_via_alen,
					&cfg->rc_via_table, cfg->rc_via))
1282
				goto errout;
R
Roopa Prabhu 已提交
1283 1284 1285 1286 1287 1288
			break;
		}
		case RTA_MULTIPATH:
		{
			cfg->rc_mp = nla_data(nla);
			cfg->rc_mp_len = nla_len(nla);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
			break;
		}
		default:
			/* Unsupported attribute */
			goto errout;
		}
	}

	err = 0;
errout:
	return err;
}

static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
{
	struct mpls_route_config cfg;
	int err;

	err = rtm_to_route_config(skb, nlh, &cfg);
	if (err < 0)
		return err;

	return mpls_route_del(&cfg);
}


static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
{
	struct mpls_route_config cfg;
	int err;

	err = rtm_to_route_config(skb, nlh, &cfg);
	if (err < 0)
		return err;

	return mpls_route_add(&cfg);
}

static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
			   u32 label, struct mpls_route *rt, int flags)
{
1330
	struct net_device *dev;
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	struct nlmsghdr *nlh;
	struct rtmsg *rtm;

	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
	if (nlh == NULL)
		return -EMSGSIZE;

	rtm = nlmsg_data(nlh);
	rtm->rtm_family = AF_MPLS;
	rtm->rtm_dst_len = 20;
	rtm->rtm_src_len = 0;
	rtm->rtm_tos = 0;
	rtm->rtm_table = RT_TABLE_MAIN;
	rtm->rtm_protocol = rt->rt_protocol;
	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
	rtm->rtm_type = RTN_UNICAST;
	rtm->rtm_flags = 0;

	if (nla_put_labels(skb, RTA_DST, 1, &label))
		goto nla_put_failure;
R
Roopa Prabhu 已提交
1351
	if (rt->rt_nhn == 1) {
1352
		const struct mpls_nh *nh = rt->rt_nh;
R
Roopa Prabhu 已提交
1353 1354 1355 1356 1357

		if (nh->nh_labels &&
		    nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
				   nh->nh_label))
			goto nla_put_failure;
1358
		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1359
		    nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
R
Roopa Prabhu 已提交
1360 1361 1362 1363 1364
				nh->nh_via_alen))
			goto nla_put_failure;
		dev = rtnl_dereference(nh->nh_dev);
		if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
			goto nla_put_failure;
R
Roopa Prabhu 已提交
1365 1366 1367 1368
		if (nh->nh_flags & RTNH_F_LINKDOWN)
			rtm->rtm_flags |= RTNH_F_LINKDOWN;
		if (nh->nh_flags & RTNH_F_DEAD)
			rtm->rtm_flags |= RTNH_F_DEAD;
R
Roopa Prabhu 已提交
1369 1370 1371
	} else {
		struct rtnexthop *rtnh;
		struct nlattr *mp;
R
Roopa Prabhu 已提交
1372 1373
		int dead = 0;
		int linkdown = 0;
R
Roopa Prabhu 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386

		mp = nla_nest_start(skb, RTA_MULTIPATH);
		if (!mp)
			goto nla_put_failure;

		for_nexthops(rt) {
			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
			if (!rtnh)
				goto nla_put_failure;

			dev = rtnl_dereference(nh->nh_dev);
			if (dev)
				rtnh->rtnh_ifindex = dev->ifindex;
R
Roopa Prabhu 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395
			if (nh->nh_flags & RTNH_F_LINKDOWN) {
				rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
				linkdown++;
			}
			if (nh->nh_flags & RTNH_F_DEAD) {
				rtnh->rtnh_flags |= RTNH_F_DEAD;
				dead++;
			}

R
Roopa Prabhu 已提交
1396 1397 1398 1399
			if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
							    nh->nh_labels,
							    nh->nh_label))
				goto nla_put_failure;
1400 1401
			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
			    nla_put_via(skb, nh->nh_via_table,
1402
					mpls_nh_via(rt, nh),
R
Roopa Prabhu 已提交
1403 1404 1405 1406 1407 1408 1409
					nh->nh_via_alen))
				goto nla_put_failure;

			/* length of rtnetlink header + attributes */
			rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
		} endfor_nexthops(rt);

R
Roopa Prabhu 已提交
1410 1411 1412 1413 1414
		if (linkdown == rt->rt_nhn)
			rtm->rtm_flags |= RTNH_F_LINKDOWN;
		if (dead == rt->rt_nhn)
			rtm->rtm_flags |= RTNH_F_DEAD;

R
Roopa Prabhu 已提交
1415 1416
		nla_nest_end(skb, mp);
	}
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

	nlmsg_end(skb, nlh);
	return 0;

nla_put_failure:
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
}

static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct net *net = sock_net(skb->sk);
1429 1430
	struct mpls_route __rcu **platform_label;
	size_t platform_labels;
1431 1432 1433 1434 1435
	unsigned int index;

	ASSERT_RTNL();

	index = cb->args[0];
1436 1437
	if (index < MPLS_LABEL_FIRST_UNRESERVED)
		index = MPLS_LABEL_FIRST_UNRESERVED;
1438

1439 1440 1441
	platform_label = rtnl_dereference(net->mpls.platform_label);
	platform_labels = net->mpls.platform_labels;
	for (; index < platform_labels; index++) {
1442
		struct mpls_route *rt;
1443
		rt = rtnl_dereference(platform_label[index]);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
		if (!rt)
			continue;

		if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
				    cb->nlh->nlmsg_seq, RTM_NEWROUTE,
				    index, rt, NLM_F_MULTI) < 0)
			break;
	}
	cb->args[0] = index;

	return skb->len;
}

1457 1458 1459 1460 1461
static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
{
	size_t payload =
		NLMSG_ALIGN(sizeof(struct rtmsg))
		+ nla_total_size(4);			/* RTA_DST */
R
Roopa Prabhu 已提交
1462 1463 1464 1465 1466 1467

	if (rt->rt_nhn == 1) {
		struct mpls_nh *nh = rt->rt_nh;

		if (nh->nh_dev)
			payload += nla_total_size(4); /* RTA_OIF */
1468
		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1469
			payload += nla_total_size(2 + nh->nh_via_alen);
R
Roopa Prabhu 已提交
1470 1471 1472 1473 1474 1475 1476 1477
		if (nh->nh_labels) /* RTA_NEWDST */
			payload += nla_total_size(nh->nh_labels * 4);
	} else {
		/* each nexthop is packed in an attribute */
		size_t nhsize = 0;

		for_nexthops(rt) {
			nhsize += nla_total_size(sizeof(struct rtnexthop));
1478 1479 1480
			/* RTA_VIA */
			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
				nhsize += nla_total_size(2 + nh->nh_via_alen);
R
Roopa Prabhu 已提交
1481 1482 1483 1484 1485 1486 1487
			if (nh->nh_labels)
				nhsize += nla_total_size(nh->nh_labels * 4);
		} endfor_nexthops(rt);
		/* nested attribute */
		payload += nla_total_size(nhsize);
	}

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
	return payload;
}

static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
		       struct nlmsghdr *nlh, struct net *net, u32 portid,
		       unsigned int nlm_flags)
{
	struct sk_buff *skb;
	u32 seq = nlh ? nlh->nlmsg_seq : 0;
	int err = -ENOBUFS;

	skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
	if (skb == NULL)
		goto errout;

	err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
	if (err < 0) {
		/* -EMSGSIZE implies BUG in lfib_nlmsg_size */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
	rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);

	return;
errout:
	if (err < 0)
		rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static int resize_platform_label_table(struct net *net, size_t limit)
{
	size_t size = sizeof(struct mpls_route *) * limit;
	size_t old_limit;
	size_t cp_size;
	struct mpls_route __rcu **labels = NULL, **old;
	struct mpls_route *rt0 = NULL, *rt2 = NULL;
	unsigned index;

	if (size) {
		labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
		if (!labels)
			labels = vzalloc(size);

		if (!labels)
			goto nolabels;
	}

	/* In case the predefined labels need to be populated */
1537
	if (limit > MPLS_LABEL_IPV4NULL) {
1538
		struct net_device *lo = net->loopback_dev;
1539
		rt0 = mpls_rt_alloc(1, lo->addr_len);
1540 1541
		if (!rt0)
			goto nort0;
R
Roopa Prabhu 已提交
1542
		RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1543
		rt0->rt_protocol = RTPROT_KERNEL;
1544
		rt0->rt_payload_type = MPT_IPV4;
R
Roopa Prabhu 已提交
1545
		rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1546
		rt0->rt_nh->nh_via_alen = lo->addr_len;
1547 1548
		memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
		       lo->addr_len);
1549
	}
1550
	if (limit > MPLS_LABEL_IPV6NULL) {
1551
		struct net_device *lo = net->loopback_dev;
1552
		rt2 = mpls_rt_alloc(1, lo->addr_len);
1553 1554
		if (!rt2)
			goto nort2;
R
Roopa Prabhu 已提交
1555
		RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1556
		rt2->rt_protocol = RTPROT_KERNEL;
1557
		rt2->rt_payload_type = MPT_IPV6;
R
Roopa Prabhu 已提交
1558
		rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1559
		rt2->rt_nh->nh_via_alen = lo->addr_len;
1560 1561
		memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
		       lo->addr_len);
1562 1563 1564 1565
	}

	rtnl_lock();
	/* Remember the original table */
1566
	old = rtnl_dereference(net->mpls.platform_label);
1567 1568 1569 1570
	old_limit = net->mpls.platform_labels;

	/* Free any labels beyond the new table */
	for (index = limit; index < old_limit; index++)
R
Roopa Prabhu 已提交
1571
		mpls_route_update(net, index, NULL, NULL);
1572 1573 1574 1575 1576 1577 1578 1579 1580

	/* Copy over the old labels */
	cp_size = size;
	if (old_limit < limit)
		cp_size = old_limit * sizeof(struct mpls_route *);

	memcpy(labels, old, cp_size);

	/* If needed set the predefined labels */
1581 1582 1583
	if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
	    (limit > MPLS_LABEL_IPV6NULL)) {
		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1584 1585 1586
		rt2 = NULL;
	}

1587 1588 1589
	if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
	    (limit > MPLS_LABEL_IPV4NULL)) {
		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1590 1591 1592 1593 1594
		rt0 = NULL;
	}

	/* Update the global pointers */
	net->mpls.platform_labels = limit;
1595
	rcu_assign_pointer(net->mpls.platform_label, labels);
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

	rtnl_unlock();

	mpls_rt_free(rt2);
	mpls_rt_free(rt0);

	if (old) {
		synchronize_rcu();
		kvfree(old);
	}
	return 0;

nort2:
	mpls_rt_free(rt0);
nort0:
	kvfree(labels);
nolabels:
	return -ENOMEM;
}

static int mpls_platform_labels(struct ctl_table *table, int write,
				void __user *buffer, size_t *lenp, loff_t *ppos)
{
	struct net *net = table->data;
	int platform_labels = net->mpls.platform_labels;
	int ret;
	struct ctl_table tmp = {
		.procname	= table->procname,
		.data		= &platform_labels,
		.maxlen		= sizeof(int),
		.mode		= table->mode,
		.extra1		= &zero,
		.extra2		= &label_limit,
	};

	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);

	if (write && ret == 0)
		ret = resize_platform_label_table(net, platform_labels);

	return ret;
}

1639
static const struct ctl_table mpls_table[] = {
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	{
		.procname	= "platform_labels",
		.data		= NULL,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= mpls_platform_labels,
	},
	{ }
};

E
Eric W. Biederman 已提交
1650 1651
static int mpls_net_init(struct net *net)
{
1652 1653
	struct ctl_table *table;

E
Eric W. Biederman 已提交
1654 1655 1656
	net->mpls.platform_labels = 0;
	net->mpls.platform_label = NULL;

1657 1658 1659 1660 1661 1662
	table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
	if (table == NULL)
		return -ENOMEM;

	table[0].data = net;
	net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1663 1664
	if (net->mpls.ctl == NULL) {
		kfree(table);
1665
		return -ENOMEM;
1666
	}
1667

E
Eric W. Biederman 已提交
1668 1669 1670 1671 1672
	return 0;
}

static void mpls_net_exit(struct net *net)
{
1673 1674
	struct mpls_route __rcu **platform_label;
	size_t platform_labels;
1675
	struct ctl_table *table;
E
Eric W. Biederman 已提交
1676 1677
	unsigned int index;

1678 1679 1680 1681
	table = net->mpls.ctl->ctl_table_arg;
	unregister_net_sysctl_table(net->mpls.ctl);
	kfree(table);

1682 1683
	/* An rcu grace period has passed since there was a device in
	 * the network namespace (and thus the last in flight packet)
E
Eric W. Biederman 已提交
1684 1685 1686 1687 1688 1689 1690 1691
	 * left this network namespace.  This is because
	 * unregister_netdevice_many and netdev_run_todo has completed
	 * for each network device that was in this network namespace.
	 *
	 * As such no additional rcu synchronization is necessary when
	 * freeing the platform_label table.
	 */
	rtnl_lock();
1692 1693 1694 1695 1696
	platform_label = rtnl_dereference(net->mpls.platform_label);
	platform_labels = net->mpls.platform_labels;
	for (index = 0; index < platform_labels; index++) {
		struct mpls_route *rt = rtnl_dereference(platform_label[index]);
		RCU_INIT_POINTER(platform_label[index], NULL);
E
Eric W. Biederman 已提交
1697 1698 1699 1700
		mpls_rt_free(rt);
	}
	rtnl_unlock();

1701
	kvfree(platform_label);
E
Eric W. Biederman 已提交
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
}

static struct pernet_operations mpls_net_ops = {
	.init = mpls_net_init,
	.exit = mpls_net_exit,
};

static int __init mpls_init(void)
{
	int err;

	BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);

	err = register_pernet_subsys(&mpls_net_ops);
	if (err)
		goto out;

	err = register_netdevice_notifier(&mpls_dev_notifier);
	if (err)
		goto out_unregister_pernet;

	dev_add_pack(&mpls_packet_type);

1725 1726 1727
	rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
	rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
	rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
E
Eric W. Biederman 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
	err = 0;
out:
	return err;

out_unregister_pernet:
	unregister_pernet_subsys(&mpls_net_ops);
	goto out;
}
module_init(mpls_init);

static void __exit mpls_exit(void)
{
1740
	rtnl_unregister_all(PF_MPLS);
E
Eric W. Biederman 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749
	dev_remove_pack(&mpls_packet_type);
	unregister_netdevice_notifier(&mpls_dev_notifier);
	unregister_pernet_subsys(&mpls_net_ops);
}
module_exit(mpls_exit);

MODULE_DESCRIPTION("MultiProtocol Label Switching");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS_NETPROTO(PF_MPLS);