af_mpls.c 63.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
E
Eric W. Biederman 已提交
2 3 4
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
5
#include <linux/sysctl.h>
E
Eric W. Biederman 已提交
6 7 8 9 10
#include <linux/net.h>
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/ipv6.h>
#include <linux/mpls.h>
D
David Ahern 已提交
11
#include <linux/netconf.h>
12
#include <linux/nospec.h>
13
#include <linux/vmalloc.h>
R
Robert Shearman 已提交
14
#include <linux/percpu.h>
E
Eric W. Biederman 已提交
15 16 17 18 19 20
#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>
21
#include <net/ip_tunnels.h>
E
Eric W. Biederman 已提交
22
#include <net/netns/generic.h>
23 24 25
#if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h>
#endif
26
#include <net/ipv6_stubs.h>
27
#include <net/rtnh.h>
E
Eric W. Biederman 已提交
28 29
#include "internal.h"

30 31 32
/* max memory we will use for mpls_route */
#define MAX_MPLS_ROUTE_MEM	4096

33 34 35 36 37
/* Maximum number of labels to look ahead at when selecting a path of
 * a multipath route
 */
#define MAX_MP_SELECT_LABELS 4

38 39
#define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)

40
static int label_limit = (1 << 20) - 1;
41
static int ttl_max = 255;
42

43
#if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
44
static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
	return sizeof(struct mpls_shim_hdr);
}

static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
	.encap_hlen	= ipgre_mpls_encap_hlen,
};

static int ipgre_tunnel_encap_add_mpls_ops(void)
{
	return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
}

static void ipgre_tunnel_encap_del_mpls_ops(void)
{
	ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
}
#else
static int ipgre_tunnel_encap_add_mpls_ops(void)
{
	return 0;
}

static void ipgre_tunnel_encap_del_mpls_ops(void)
{
}
#endif

73 74 75 76
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 已提交
77 78 79 80 81 82 83 84 85 86 87 88
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;
}

89
bool mpls_output_possible(const struct net_device *dev)
E
Eric W. Biederman 已提交
90 91 92
{
	return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
}
93
EXPORT_SYMBOL_GPL(mpls_output_possible);
E
Eric W. Biederman 已提交
94

95 96
static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
{
97
	return (u8 *)nh + rt->rt_via_offset;
98 99 100 101 102 103 104 105
}

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 已提交
106
static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
E
Eric W. Biederman 已提交
107 108
{
	/* The size of the layer 2.5 labels to be added for this route */
R
Roopa Prabhu 已提交
109
	return nh->nh_labels * sizeof(struct mpls_shim_hdr);
E
Eric W. Biederman 已提交
110 111
}

112
unsigned int mpls_dev_mtu(const struct net_device *dev)
E
Eric W. Biederman 已提交
113 114 115 116
{
	/* The amount of data the layer 2 frame can hold */
	return dev->mtu;
}
117
EXPORT_SYMBOL_GPL(mpls_dev_mtu);
E
Eric W. Biederman 已提交
118

119
bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
E
Eric W. Biederman 已提交
120 121 122 123
{
	if (skb->len <= mtu)
		return false;

124
	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
E
Eric W. Biederman 已提交
125 126 127 128
		return false;

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

R
Robert Shearman 已提交
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
void mpls_stats_inc_outucastpkts(struct net_device *dev,
				 const struct sk_buff *skb)
{
	struct mpls_dev *mdev;

	if (skb->protocol == htons(ETH_P_MPLS_UC)) {
		mdev = mpls_dev_get(dev);
		if (mdev)
			MPLS_INC_STATS_LEN(mdev, skb->len,
					   tx_packets,
					   tx_bytes);
	} else if (skb->protocol == htons(ETH_P_IP)) {
		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
#if IS_ENABLED(CONFIG_IPV6)
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		struct inet6_dev *in6dev = __in6_dev_get(dev);

		if (in6dev)
			IP6_UPD_PO_STATS(dev_net(dev), in6dev,
					 IPSTATS_MIB_OUT, skb->len);
#endif
	}
}
EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);

156
static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
R
Roopa Prabhu 已提交
157
{
158
	struct mpls_entry_decoded dec;
159
	unsigned int mpls_hdr_len = 0;
160 161 162 163 164
	struct mpls_shim_hdr *hdr;
	bool eli_seen = false;
	int label_index;
	u32 hash = 0;

165
	for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
166
	     label_index++) {
167 168
		mpls_hdr_len += sizeof(*hdr);
		if (!pskb_may_pull(skb, mpls_hdr_len))
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
			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;
		}

193 194 195 196 197
		if (!dec.bos)
			continue;

		/* found bottom label; does skb have room for a header? */
		if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
198 199
			const struct iphdr *v4hdr;

200
			v4hdr = (const struct iphdr *)(hdr + 1);
201 202 203 204 205
			if (v4hdr->version == 4) {
				hash = jhash_3words(ntohl(v4hdr->saddr),
						    ntohl(v4hdr->daddr),
						    v4hdr->protocol, hash);
			} else if (v4hdr->version == 6 &&
206 207
				   pskb_may_pull(skb, mpls_hdr_len +
						 sizeof(struct ipv6hdr))) {
208 209
				const struct ipv6hdr *v6hdr;

210
				v6hdr = (const struct ipv6hdr *)(hdr + 1);
211 212 213 214 215
				hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
				hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
				hash = jhash_1word(v6hdr->nexthdr, hash);
			}
		}
216 217

		break;
218 219
	}

R
Roopa Prabhu 已提交
220 221 222
	return hash;
}

223 224 225 226 227
static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
{
	return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
}

228 229 230 231 232
/* number of alive nexthops (rt->rt_nhn_alive) and the flags for
 * a next hop (nh->nh_flags) are modified by netdev event handlers.
 * Since those fields can change at any moment, use READ_ONCE to
 * access both.
 */
R
Roopa Prabhu 已提交
233
static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
234
					     struct sk_buff *skb)
R
Roopa Prabhu 已提交
235 236 237 238
{
	u32 hash = 0;
	int nh_index = 0;
	int n = 0;
239
	u8 alive;
R
Roopa Prabhu 已提交
240 241 242 243 244

	/* No need to look further into packet if there's only
	 * one path
	 */
	if (rt->rt_nhn == 1)
245
		return rt->rt_nh;
R
Roopa Prabhu 已提交
246

247 248
	alive = READ_ONCE(rt->rt_nhn_alive);
	if (alive == 0)
R
Roopa Prabhu 已提交
249 250
		return NULL;

251
	hash = mpls_multipath_hash(rt, skb);
R
Roopa Prabhu 已提交
252 253 254 255
	nh_index = hash % alive;
	if (alive == rt->rt_nhn)
		goto out;
	for_nexthops(rt) {
256 257 258
		unsigned int nh_flags = READ_ONCE(nh->nh_flags);

		if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
R
Roopa Prabhu 已提交
259 260 261 262 263 264
			continue;
		if (n == nh_index)
			return nh;
		n++;
	} endfor_nexthops(rt);

265
out:
266
	return mpls_get_nexthop(rt, nh_index);
R
Roopa Prabhu 已提交
267 268
}

269 270
static bool mpls_egress(struct net *net, struct mpls_route *rt,
			struct sk_buff *skb, struct mpls_entry_decoded dec)
E
Eric W. Biederman 已提交
271
{
272 273
	enum mpls_payload_type payload_type;
	bool success = false;
E
Eric W. Biederman 已提交
274

275 276 277 278 279 280 281 282 283 284 285 286 287
	/* 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;

288 289 290 291 292 293 294
	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);
295
		u8 new_ttl;
E
Eric W. Biederman 已提交
296
		skb->protocol = htons(ETH_P_IP);
297 298 299 300 301 302 303 304 305 306 307 308

		/* If propagating TTL, take the decremented TTL from
		 * the incoming MPLS header, otherwise decrement the
		 * TTL, but only if not 0 to avoid underflow.
		 */
		if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
		    (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
		     net->mpls.ip_ttl_propagate))
			new_ttl = dec.ttl;
		else
			new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;

E
Eric W. Biederman 已提交
309 310
		csum_replace2(&hdr4->check,
			      htons(hdr4->ttl << 8),
311 312
			      htons(new_ttl << 8));
		hdr4->ttl = new_ttl;
313 314
		success = true;
		break;
E
Eric W. Biederman 已提交
315
	}
316
	case MPT_IPV6: {
E
Eric W. Biederman 已提交
317 318
		struct ipv6hdr *hdr6 = ipv6_hdr(skb);
		skb->protocol = htons(ETH_P_IPV6);
319 320 321 322 323 324 325 326 327 328 329

		/* If propagating TTL, take the decremented TTL from
		 * the incoming MPLS header, otherwise decrement the
		 * hop limit, but only if not 0 to avoid underflow.
		 */
		if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
		    (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
		     net->mpls.ip_ttl_propagate))
			hdr6->hop_limit = dec.ttl;
		else if (hdr6->hop_limit)
			hdr6->hop_limit = hdr6->hop_limit - 1;
330 331
		success = true;
		break;
E
Eric W. Biederman 已提交
332
	}
333
	case MPT_UNSPEC:
334
		/* Should have decided which protocol it is by now */
335 336 337
		break;
	}

E
Eric W. Biederman 已提交
338 339 340 341 342 343 344 345 346
	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 已提交
347
	struct mpls_nh *nh;
E
Eric W. Biederman 已提交
348 349
	struct mpls_entry_decoded dec;
	struct net_device *out_dev;
R
Robert Shearman 已提交
350
	struct mpls_dev *out_mdev;
R
Robert Shearman 已提交
351
	struct mpls_dev *mdev;
E
Eric W. Biederman 已提交
352 353 354 355 356 357 358
	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 已提交
359
	mdev = mpls_dev_get(dev);
R
Robert Shearman 已提交
360
	if (!mdev)
R
Robert Shearman 已提交
361 362
		goto drop;

R
Robert Shearman 已提交
363 364 365 366 367
	MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
			   rx_bytes);

	if (!mdev->input_enabled) {
		MPLS_INC_STATS(mdev, rx_dropped);
E
Eric W. Biederman 已提交
368
		goto drop;
R
Robert Shearman 已提交
369 370 371 372
	}

	if (skb->pkt_type != PACKET_HOST)
		goto err;
E
Eric W. Biederman 已提交
373 374

	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
R
Robert Shearman 已提交
375
		goto err;
E
Eric W. Biederman 已提交
376 377

	if (!pskb_may_pull(skb, sizeof(*hdr)))
R
Robert Shearman 已提交
378
		goto err;
E
Eric W. Biederman 已提交
379 380 381 382 383 384

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

	rt = mpls_route_input_rcu(net, dec.label);
R
Robert Shearman 已提交
385 386
	if (!rt) {
		MPLS_INC_STATS(mdev, rx_noroute);
E
Eric W. Biederman 已提交
387
		goto drop;
R
Robert Shearman 已提交
388
	}
E
Eric W. Biederman 已提交
389

390
	nh = mpls_select_multipath(rt, skb);
R
Roopa Prabhu 已提交
391
	if (!nh)
R
Robert Shearman 已提交
392
		goto err;
E
Eric W. Biederman 已提交
393

394 395 396 397 398 399
	/* Pop the label */
	skb_pull(skb, sizeof(*hdr));
	skb_reset_network_header(skb);

	skb_orphan(skb);

E
Eric W. Biederman 已提交
400
	if (skb_warn_if_lro(skb))
R
Robert Shearman 已提交
401
		goto err;
E
Eric W. Biederman 已提交
402 403 404 405

	skb_forward_csum(skb);

	/* Verify ttl is valid */
406
	if (dec.ttl <= 1)
R
Robert Shearman 已提交
407
		goto err;
E
Eric W. Biederman 已提交
408 409
	dec.ttl -= 1;

R
Robert Shearman 已提交
410 411 412 413 414
	/* Find the output device */
	out_dev = rcu_dereference(nh->nh_dev);
	if (!mpls_output_possible(out_dev))
		goto tx_err;

E
Eric W. Biederman 已提交
415
	/* Verify the destination can hold the packet */
R
Roopa Prabhu 已提交
416
	new_header_size = mpls_nh_header_size(nh);
E
Eric W. Biederman 已提交
417 418
	mtu = mpls_dev_mtu(out_dev);
	if (mpls_pkt_too_big(skb, mtu - new_header_size))
R
Robert Shearman 已提交
419
		goto tx_err;
E
Eric W. Biederman 已提交
420 421 422 423 424 425 426

	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))
R
Robert Shearman 已提交
427
		goto tx_err;
E
Eric W. Biederman 已提交
428 429 430 431 432 433

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

	if (unlikely(!new_header_size && dec.bos)) {
		/* Penultimate hop popping */
434
		if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
R
Robert Shearman 已提交
435
			goto err;
E
Eric W. Biederman 已提交
436 437 438 439 440 441 442 443
	} 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 已提交
444 445 446
		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 已提交
447 448 449 450
			bos = false;
		}
	}

R
Robert Shearman 已提交
451 452
	mpls_stats_inc_outucastpkts(out_dev, skb);

453 454 455 456 457 458 459
	/* 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 已提交
460 461 462 463 464
	if (err)
		net_dbg_ratelimited("%s: packet transmission failed: %d\n",
				    __func__, err);
	return 0;

R
Robert Shearman 已提交
465 466 467 468 469 470 471
tx_err:
	out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
	if (out_mdev)
		MPLS_INC_STATS(out_mdev, tx_errors);
	goto drop;
err:
	MPLS_INC_STATS(mdev, rx_errors);
E
Eric W. Biederman 已提交
472 473 474 475 476 477 478 479 480 481
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,
};

482
static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
483 484
	[RTA_DST]		= { .type = NLA_U32 },
	[RTA_OIF]		= { .type = NLA_U32 },
485
	[RTA_TTL_PROPAGATE]	= { .type = NLA_U8 },
486 487
};

488
struct mpls_route_config {
489 490
	u32			rc_protocol;
	u32			rc_ifindex;
R
Roopa Prabhu 已提交
491 492
	u8			rc_via_table;
	u8			rc_via_alen;
493 494
	u8			rc_via[MAX_VIA_ALEN];
	u32			rc_label;
495
	u8			rc_ttl_propagate;
R
Roopa Prabhu 已提交
496
	u8			rc_output_labels;
497 498 499 500
	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 已提交
501 502
	struct rtnexthop	*rc_mp;
	int			rc_mp_len;
503 504
};

505 506 507 508
/* all nexthops within a route have the same size based on max
 * number of labels and max via length for a hop
 */
static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
E
Eric W. Biederman 已提交
509
{
510
	u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
E
Eric W. Biederman 已提交
511
	struct mpls_route *rt;
512
	size_t size;
E
Eric W. Biederman 已提交
513

514 515 516 517 518 519 520 521 522 523 524 525
	size = sizeof(*rt) + num_nh * nh_size;
	if (size > MAX_MPLS_ROUTE_MEM)
		return ERR_PTR(-EINVAL);

	rt = kzalloc(size, GFP_KERNEL);
	if (!rt)
		return ERR_PTR(-ENOMEM);

	rt->rt_nhn = num_nh;
	rt->rt_nhn_alive = num_nh;
	rt->rt_nh_size = nh_size;
	rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
R
Roopa Prabhu 已提交
526

E
Eric W. Biederman 已提交
527 528 529 530 531 532 533 534 535
	return rt;
}

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

536 537 538 539 540 541 542 543 544 545
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 */
546
	if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
547 548 549
		rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
}

E
Eric W. Biederman 已提交
550
static void mpls_route_update(struct net *net, unsigned index,
R
Roopa Prabhu 已提交
551
			      struct mpls_route *new,
E
Eric W. Biederman 已提交
552 553
			      const struct nl_info *info)
{
554
	struct mpls_route __rcu **platform_label;
R
Roopa Prabhu 已提交
555
	struct mpls_route *rt;
E
Eric W. Biederman 已提交
556 557 558

	ASSERT_RTNL();

559 560
	platform_label = rtnl_dereference(net->mpls.platform_label);
	rt = rtnl_dereference(platform_label[index]);
R
Roopa Prabhu 已提交
561
	rcu_assign_pointer(platform_label[index], new);
E
Eric W. Biederman 已提交
562

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

E
Eric W. Biederman 已提交
565
	/* If we removed a route free it now */
R
Roopa Prabhu 已提交
566
	mpls_rt_free(rt);
E
Eric W. Biederman 已提交
567 568
}

569 570
static unsigned find_free_label(struct net *net)
{
571 572
	struct mpls_route __rcu **platform_label;
	size_t platform_labels;
573
	unsigned index;
574 575 576

	platform_label = rtnl_dereference(net->mpls.platform_label);
	platform_labels = net->mpls.platform_labels;
577 578
	for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
	     index++) {
579
		if (!rtnl_dereference(platform_label[index]))
580 581 582 583 584
			return index;
	}
	return LABEL_NOT_SPECIFIED;
}

585
#if IS_ENABLED(CONFIG_INET)
586 587
static struct net_device *inet_fib_lookup_dev(struct net *net,
					      const void *addr)
R
Roopa Prabhu 已提交
588
{
589
	struct net_device *dev;
R
Roopa Prabhu 已提交
590 591 592 593 594 595
	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))
596
		return ERR_CAST(rt);
R
Roopa Prabhu 已提交
597 598 599 600 601 602 603

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

	ip_rt_put(rt);

	return dev;
604 605
}
#else
606 607
static struct net_device *inet_fib_lookup_dev(struct net *net,
					      const void *addr)
608 609
{
	return ERR_PTR(-EAFNOSUPPORT);
R
Roopa Prabhu 已提交
610
}
611
#endif
R
Roopa Prabhu 已提交
612

613
#if IS_ENABLED(CONFIG_IPV6)
614 615
static struct net_device *inet6_fib_lookup_dev(struct net *net,
					       const void *addr)
R
Roopa Prabhu 已提交
616
{
617
	struct net_device *dev;
R
Roopa Prabhu 已提交
618 619
	struct dst_entry *dst;
	struct flowi6 fl6;
620 621 622

	if (!ipv6_stub)
		return ERR_PTR(-EAFNOSUPPORT);
R
Roopa Prabhu 已提交
623 624 625

	memset(&fl6, 0, sizeof(fl6));
	memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
626 627 628
	dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
	if (IS_ERR(dst))
		return ERR_CAST(dst);
R
Roopa Prabhu 已提交
629 630 631 632 633 634 635

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

	return dev;
}
636
#else
637 638
static struct net_device *inet6_fib_lookup_dev(struct net *net,
					       const void *addr)
639 640 641 642
{
	return ERR_PTR(-EAFNOSUPPORT);
}
#endif
R
Roopa Prabhu 已提交
643 644

static struct net_device *find_outdev(struct net *net,
645
				      struct mpls_route *rt,
R
Roopa Prabhu 已提交
646
				      struct mpls_nh *nh, int oif)
R
Roopa Prabhu 已提交
647 648 649
{
	struct net_device *dev = NULL;

R
Roopa Prabhu 已提交
650 651
	if (!oif) {
		switch (nh->nh_via_table) {
R
Roopa Prabhu 已提交
652
		case NEIGH_ARP_TABLE:
653
			dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
R
Roopa Prabhu 已提交
654 655
			break;
		case NEIGH_ND_TABLE:
656
			dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
R
Roopa Prabhu 已提交
657 658 659 660 661
			break;
		case NEIGH_LINK_TABLE:
			break;
		}
	} else {
R
Roopa Prabhu 已提交
662
		dev = dev_get_by_index(net, oif);
R
Roopa Prabhu 已提交
663 664
	}

665 666 667
	if (!dev)
		return ERR_PTR(-ENODEV);

668 669 670
	if (IS_ERR(dev))
		return dev;

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

R
Roopa Prabhu 已提交
674 675 676
	return dev;
}

677 678
static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
			      struct mpls_nh *nh, int oif)
R
Roopa Prabhu 已提交
679 680 681 682
{
	struct net_device *dev = NULL;
	int err = -ENODEV;

683
	dev = find_outdev(net, rt, nh, oif);
R
Roopa Prabhu 已提交
684 685 686 687 688 689 690 691 692 693 694
	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;

695 696 697 698
	if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
	    (dev->addr_len != nh->nh_via_alen))
		goto errout;

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

R
Roopa Prabhu 已提交
701 702 703 704 705 706 707 708 709 710
	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 已提交
711 712 713 714 715 716
	return 0;

errout:
	return err;
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
		       u8 via_addr[], struct netlink_ext_ack *extack)
{
	struct rtvia *via = nla_data(nla);
	int err = -EINVAL;
	int alen;

	if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
		NL_SET_ERR_MSG_ATTR(extack, nla,
				    "Invalid attribute length for RTA_VIA");
		goto errout;
	}
	alen = nla_len(nla) -
			offsetof(struct rtvia, rtvia_addr);
	if (alen > MAX_VIA_ALEN) {
		NL_SET_ERR_MSG_ATTR(extack, nla,
				    "Invalid address length for RTA_VIA");
		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;
}

R
Roopa Prabhu 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
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;

	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;
781
	memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
R
Roopa Prabhu 已提交
782 783
	nh->nh_via_alen = cfg->rc_via_alen;

784
	err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
R
Roopa Prabhu 已提交
785 786 787
	if (err)
		goto errout;

R
Roopa Prabhu 已提交
788 789 790
	if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
		rt->rt_nhn_alive--;

R
Roopa Prabhu 已提交
791 792 793 794 795 796
	return 0;

errout:
	return err;
}

797
static int mpls_nh_build(struct net *net, struct mpls_route *rt,
R
Roopa Prabhu 已提交
798
			 struct mpls_nh *nh, int oif, struct nlattr *via,
799 800
			 struct nlattr *newdst, u8 max_labels,
			 struct netlink_ext_ack *extack)
R
Roopa Prabhu 已提交
801 802 803 804 805 806 807
{
	int err = -ENOMEM;

	if (!nh)
		goto errout;

	if (newdst) {
808
		err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
809
				     nh->nh_label, extack);
R
Roopa Prabhu 已提交
810 811 812 813
		if (err)
			goto errout;
	}

814 815
	if (via) {
		err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
816
				  __mpls_nh_via(rt, nh), extack);
817 818 819 820 821
		if (err)
			goto errout;
	} else {
		nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
	}
R
Roopa Prabhu 已提交
822

823
	err = mpls_nh_assign_dev(net, rt, nh, oif);
R
Roopa Prabhu 已提交
824 825 826 827 828 829 830 831 832
	if (err)
		goto errout;

	return 0;

errout:
	return err;
}

833
static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
834 835
			      u8 cfg_via_alen, u8 *max_via_alen,
			      u8 *max_labels)
R
Roopa Prabhu 已提交
836 837
{
	int remaining = len;
838
	u8 nhs = 0;
R
Roopa Prabhu 已提交
839

840
	*max_via_alen = 0;
841
	*max_labels = 0;
842

R
Roopa Prabhu 已提交
843
	while (rtnh_ok(rtnh, remaining)) {
844 845
		struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
		int attrlen;
846
		u8 n_labels = 0;
847 848 849 850 851 852 853 854 855 856 857 858 859

		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);
		}

860 861
		nla = nla_find(attrs, attrlen, RTA_NEWDST);
		if (nla &&
862 863
		    nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
				   NULL, NULL) != 0)
864 865 866 867
			return 0;

		*max_labels = max_t(u8, *max_labels, n_labels);

868 869 870 871 872
		/* number of nexthops is tracked by a u8.
		 * Check for overflow.
		 */
		if (nhs == 255)
			return 0;
R
Roopa Prabhu 已提交
873
		nhs++;
874

R
Roopa Prabhu 已提交
875 876 877 878 879 880 881 882
		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,
883 884
			       struct mpls_route *rt, u8 max_labels,
			       struct netlink_ext_ack *extack)
R
Roopa Prabhu 已提交
885 886 887 888 889
{
	struct rtnexthop *rtnh = cfg->rc_mp;
	struct nlattr *nla_via, *nla_newdst;
	int remaining = cfg->rc_mp_len;
	int err = 0;
890
	u8 nhs = 0;
R
Roopa Prabhu 已提交
891 892 893 894 895 896 897 898 899 900 901

	change_nexthops(rt) {
		int attrlen;

		nla_via = NULL;
		nla_newdst = NULL;

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

902 903 904 905 906 907
		/* neither weighted multipath nor any flags
		 * are supported
		 */
		if (rtnh->rtnh_hops || rtnh->rtnh_flags)
			goto errout;

R
Roopa Prabhu 已提交
908 909 910 911 912 913 914 915
		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);
		}

916
		err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
917
				    rtnh->rtnh_ifindex, nla_via, nla_newdst,
918
				    max_labels, extack);
R
Roopa Prabhu 已提交
919 920 921
		if (err)
			goto errout;

R
Roopa Prabhu 已提交
922 923 924
		if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
			rt->rt_nhn_alive--;

R
Roopa Prabhu 已提交
925 926 927 928 929 930 931 932 933 934 935 936
		rtnh = rtnh_next(rtnh, &remaining);
		nhs++;
	} endfor_nexthops(rt);

	rt->rt_nhn = nhs;

	return 0;

errout:
	return err;
}

937
static bool mpls_label_ok(struct net *net, unsigned int *index,
938
			  struct netlink_ext_ack *extack)
939
{
940 941
	bool is_ok = true;

942
	/* Reserved labels may not be set */
943
	if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
944 945
		NL_SET_ERR_MSG(extack,
			       "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
946
		is_ok = false;
947
	}
948 949

	/* The full 20 bit range may not be supported. */
950
	if (is_ok && *index >= net->mpls.platform_labels) {
951 952
		NL_SET_ERR_MSG(extack,
			       "Label >= configured maximum in platform_labels");
953
		is_ok = false;
954
	}
955

956 957
	*index = array_index_nospec(*index, net->mpls.platform_labels);
	return is_ok;
958 959
}

960 961
static int mpls_route_add(struct mpls_route_config *cfg,
			  struct netlink_ext_ack *extack)
962
{
963
	struct mpls_route __rcu **platform_label;
964 965 966
	struct net *net = cfg->rc_nlinfo.nl_net;
	struct mpls_route *rt, *old;
	int err = -EINVAL;
967
	u8 max_via_alen;
R
Roopa Prabhu 已提交
968
	unsigned index;
969
	u8 max_labels;
970
	u8 nhs;
971 972 973 974 975 976 977 978 979

	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);
	}

980
	if (!mpls_label_ok(net, &index, extack))
981 982 983
		goto errout;

	/* Append makes no sense with mpls */
984
	err = -EOPNOTSUPP;
985 986
	if (cfg->rc_nlflags & NLM_F_APPEND) {
		NL_SET_ERR_MSG(extack, "MPLS does not support route append");
987
		goto errout;
988
	}
989 990

	err = -EEXIST;
991 992
	platform_label = rtnl_dereference(net->mpls.platform_label);
	old = rtnl_dereference(platform_label[index]);
993 994 995 996 997 998 999 1000 1001 1002 1003
	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;

1004
	err = -EINVAL;
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	if (cfg->rc_mp) {
		nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
					  cfg->rc_via_alen, &max_via_alen,
					  &max_labels);
	} else {
		max_via_alen = cfg->rc_via_alen;
		max_labels = cfg->rc_output_labels;
		nhs = 1;
	}

1015 1016
	if (nhs == 0) {
		NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
1017
		goto errout;
1018
	}
R
Roopa Prabhu 已提交
1019

1020
	err = -ENOMEM;
1021
	rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
1022 1023
	if (IS_ERR(rt)) {
		err = PTR_ERR(rt);
1024
		goto errout;
1025
	}
1026 1027

	rt->rt_protocol = cfg->rc_protocol;
1028
	rt->rt_payload_type = cfg->rc_payload_type;
1029
	rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
1030

R
Roopa Prabhu 已提交
1031
	if (cfg->rc_mp)
1032
		err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
R
Roopa Prabhu 已提交
1033 1034 1035 1036 1037 1038
	else
		err = mpls_nh_build_from_cfg(cfg, rt);
	if (err)
		goto freert;

	mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
1039 1040 1041

	return 0;

R
Roopa Prabhu 已提交
1042 1043
freert:
	mpls_rt_free(rt);
1044 1045 1046 1047
errout:
	return err;
}

1048 1049
static int mpls_route_del(struct mpls_route_config *cfg,
			  struct netlink_ext_ack *extack)
1050 1051 1052 1053 1054 1055 1056
{
	struct net *net = cfg->rc_nlinfo.nl_net;
	unsigned index;
	int err = -EINVAL;

	index = cfg->rc_label;

1057
	if (!mpls_label_ok(net, &index, extack))
1058 1059
		goto errout;

R
Roopa Prabhu 已提交
1060
	mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
1061 1062 1063 1064 1065 1066

	err = 0;
errout:
	return err;
}

R
Robert Shearman 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
static void mpls_get_stats(struct mpls_dev *mdev,
			   struct mpls_link_stats *stats)
{
	struct mpls_pcpu_stats *p;
	int i;

	memset(stats, 0, sizeof(*stats));

	for_each_possible_cpu(i) {
		struct mpls_link_stats local;
		unsigned int start;

		p = per_cpu_ptr(mdev->stats, i);
		do {
			start = u64_stats_fetch_begin(&p->syncp);
			local = p->stats;
		} while (u64_stats_fetch_retry(&p->syncp, start));

		stats->rx_packets	+= local.rx_packets;
		stats->rx_bytes		+= local.rx_bytes;
		stats->tx_packets	+= local.tx_packets;
		stats->tx_bytes		+= local.tx_bytes;
		stats->rx_errors	+= local.rx_errors;
		stats->tx_errors	+= local.tx_errors;
		stats->rx_dropped	+= local.rx_dropped;
		stats->tx_dropped	+= local.tx_dropped;
		stats->rx_noroute	+= local.rx_noroute;
	}
}

static int mpls_fill_stats_af(struct sk_buff *skb,
			      const struct net_device *dev)
{
	struct mpls_link_stats *stats;
	struct mpls_dev *mdev;
	struct nlattr *nla;

	mdev = mpls_dev_get(dev);
	if (!mdev)
		return -ENODATA;

	nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
				sizeof(struct mpls_link_stats),
				MPLS_STATS_UNSPEC);
	if (!nla)
		return -EMSGSIZE;

	stats = nla_data(nla);
	mpls_get_stats(mdev, stats);

	return 0;
}

static size_t mpls_get_stats_af_size(const struct net_device *dev)
{
	struct mpls_dev *mdev;

	mdev = mpls_dev_get(dev);
	if (!mdev)
		return 0;

	return nla_total_size_64bit(sizeof(struct mpls_link_stats));
}

D
David Ahern 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
				     u32 portid, u32 seq, int event,
				     unsigned int flags, int type)
{
	struct nlmsghdr  *nlh;
	struct netconfmsg *ncm;
	bool all = false;

	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
			flags);
	if (!nlh)
		return -EMSGSIZE;

	if (type == NETCONFA_ALL)
		all = true;

	ncm = nlmsg_data(nlh);
	ncm->ncm_family = AF_MPLS;

	if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
		goto nla_put_failure;

	if ((all || type == NETCONFA_INPUT) &&
	    nla_put_s32(skb, NETCONFA_INPUT,
			mdev->input_enabled) < 0)
		goto nla_put_failure;

	nlmsg_end(skb, nlh);
	return 0;

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

static int mpls_netconf_msgsize_devconf(int type)
{
	int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
			+ nla_total_size(4); /* NETCONFA_IFINDEX */
	bool all = false;

	if (type == NETCONFA_ALL)
		all = true;

	if (all || type == NETCONFA_INPUT)
		size += nla_total_size(4);

	return size;
}

1181 1182
static void mpls_netconf_notify_devconf(struct net *net, int event,
					int type, struct mpls_dev *mdev)
D
David Ahern 已提交
1183 1184 1185 1186 1187 1188 1189 1190
{
	struct sk_buff *skb;
	int err = -ENOBUFS;

	skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
	if (!skb)
		goto errout;

1191
	err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
D
David Ahern 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	if (err < 0) {
		/* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}

	rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
	return;
errout:
	if (err < 0)
		rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
}

static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
};

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
static int mpls_netconf_valid_get_req(struct sk_buff *skb,
				      const struct nlmsghdr *nlh,
				      struct nlattr **tb,
				      struct netlink_ext_ack *extack)
{
	int i, err;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
		NL_SET_ERR_MSG_MOD(extack,
				   "Invalid header for netconf get request");
		return -EINVAL;
	}

	if (!netlink_strict_get_check(skb))
1224 1225 1226
		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
					      tb, NETCONFA_MAX,
					      devconf_mpls_policy, extack);
1227

1228 1229 1230
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
					    tb, NETCONFA_MAX,
					    devconf_mpls_policy, extack);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
	if (err)
		return err;

	for (i = 0; i <= NETCONFA_MAX; i++) {
		if (!tb[i])
			continue;

		switch (i) {
		case NETCONFA_IFINDEX:
			break;
		default:
			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
			return -EINVAL;
		}
	}

	return 0;
}

D
David Ahern 已提交
1250
static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1251 1252
				    struct nlmsghdr *nlh,
				    struct netlink_ext_ack *extack)
D
David Ahern 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261
{
	struct net *net = sock_net(in_skb->sk);
	struct nlattr *tb[NETCONFA_MAX + 1];
	struct net_device *dev;
	struct mpls_dev *mdev;
	struct sk_buff *skb;
	int ifindex;
	int err;

1262
	err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
D
David Ahern 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	if (err < 0)
		goto errout;

	err = -EINVAL;
	if (!tb[NETCONFA_IFINDEX])
		goto errout;

	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
	dev = __dev_get_by_index(net, ifindex);
	if (!dev)
		goto errout;

	mdev = mpls_dev_get(dev);
	if (!mdev)
		goto errout;

	err = -ENOBUFS;
	skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
	if (!skb)
		goto errout;

	err = mpls_netconf_fill_devconf(skb, mdev,
					NETLINK_CB(in_skb).portid,
					nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
					NETCONFA_ALL);
	if (err < 0) {
		/* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
errout:
	return err;
}

static int mpls_netconf_dump_devconf(struct sk_buff *skb,
				     struct netlink_callback *cb)
{
1302
	const struct nlmsghdr *nlh = cb->nlh;
D
David Ahern 已提交
1303 1304 1305 1306 1307 1308 1309
	struct net *net = sock_net(skb->sk);
	struct hlist_head *head;
	struct net_device *dev;
	struct mpls_dev *mdev;
	int idx, s_idx;
	int h, s_h;

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
	if (cb->strict_check) {
		struct netlink_ext_ack *extack = cb->extack;
		struct netconfmsg *ncm;

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

		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
			NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
			return -EINVAL;
		}
	}

D
David Ahern 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	s_h = cb->args[0];
	s_idx = idx = cb->args[1];

	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
		idx = 0;
		head = &net->dev_index_head[h];
		rcu_read_lock();
		cb->seq = net->dev_base_seq;
		hlist_for_each_entry_rcu(dev, head, index_hlist) {
			if (idx < s_idx)
				goto cont;
			mdev = mpls_dev_get(dev);
			if (!mdev)
				goto cont;
			if (mpls_netconf_fill_devconf(skb, mdev,
						      NETLINK_CB(cb->skb).portid,
1341
						      nlh->nlmsg_seq,
D
David Ahern 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
						      RTM_NEWNETCONF,
						      NLM_F_MULTI,
						      NETCONFA_ALL) < 0) {
				rcu_read_unlock();
				goto done;
			}
			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
cont:
			idx++;
		}
		rcu_read_unlock();
	}
done:
	cb->args[0] = h;
	cb->args[1] = idx;

	return skb->len;
}

1361 1362 1363
#define MPLS_PERDEV_SYSCTL_OFFSET(field)	\
	(&((struct mpls_dev *)0)->field)

D
David Ahern 已提交
1364
static int mpls_conf_proc(struct ctl_table *ctl, int write,
1365
			  void *buffer, size_t *lenp, loff_t *ppos)
D
David Ahern 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
{
	int oval = *(int *)ctl->data;
	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);

	if (write) {
		struct mpls_dev *mdev = ctl->extra1;
		int i = (int *)ctl->data - (int *)mdev;
		struct net *net = ctl->extra2;
		int val = *(int *)ctl->data;

		if (i == offsetof(struct mpls_dev, input_enabled) &&
		    val != oval) {
1378 1379
			mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
						    NETCONFA_INPUT, mdev);
D
David Ahern 已提交
1380 1381 1382 1383 1384 1385
		}
	}

	return ret;
}

1386 1387 1388 1389 1390
static const struct ctl_table mpls_dev_table[] = {
	{
		.procname	= "input",
		.maxlen		= sizeof(int),
		.mode		= 0644,
D
David Ahern 已提交
1391
		.proc_handler	= mpls_conf_proc,
1392 1393 1394 1395 1396 1397 1398 1399 1400
		.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];
D
David Ahern 已提交
1401
	struct net *net = dev_net(dev);
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	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.
	 */
D
David Ahern 已提交
1412
	for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
1413
		table[i].data = (char *)mdev + (uintptr_t)table[i].data;
D
David Ahern 已提交
1414 1415 1416
		table[i].extra1 = mdev;
		table[i].extra2 = net;
	}
1417 1418 1419

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

1420
	mdev->sysctl = register_net_sysctl(net, path, table);
1421 1422 1423
	if (!mdev->sysctl)
		goto free;

1424
	mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
1425 1426 1427 1428 1429 1430 1431 1432
	return 0;

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

1433 1434
static void mpls_dev_sysctl_unregister(struct net_device *dev,
				       struct mpls_dev *mdev)
1435
{
1436
	struct net *net = dev_net(dev);
1437 1438 1439 1440 1441
	struct ctl_table *table;

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

	mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
1444 1445
}

R
Robert Shearman 已提交
1446 1447 1448 1449
static struct mpls_dev *mpls_add_dev(struct net_device *dev)
{
	struct mpls_dev *mdev;
	int err = -ENOMEM;
R
Robert Shearman 已提交
1450
	int i;
R
Robert Shearman 已提交
1451 1452 1453 1454 1455 1456 1457

	ASSERT_RTNL();

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

R
Robert Shearman 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
	mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
	if (!mdev->stats)
		goto free;

	for_each_possible_cpu(i) {
		struct mpls_pcpu_stats *mpls_stats;

		mpls_stats = per_cpu_ptr(mdev->stats, i);
		u64_stats_init(&mpls_stats->syncp);
	}

1469 1470
	mdev->dev = dev;

1471 1472 1473 1474
	err = mpls_dev_sysctl_register(dev, mdev);
	if (err)
		goto free;

R
Robert Shearman 已提交
1475 1476 1477
	rcu_assign_pointer(dev->mpls_ptr, mdev);

	return mdev;
1478 1479

free:
R
Robert Shearman 已提交
1480
	free_percpu(mdev->stats);
1481 1482
	kfree(mdev);
	return ERR_PTR(err);
R
Robert Shearman 已提交
1483 1484
}

R
Robert Shearman 已提交
1485 1486 1487 1488 1489 1490 1491 1492
static void mpls_dev_destroy_rcu(struct rcu_head *head)
{
	struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);

	free_percpu(mdev->stats);
	kfree(mdev);
}

R
Roopa Prabhu 已提交
1493
static void mpls_ifdown(struct net_device *dev, int event)
E
Eric W. Biederman 已提交
1494
{
1495
	struct mpls_route __rcu **platform_label;
E
Eric W. Biederman 已提交
1496
	struct net *net = dev_net(dev);
1497
	u8 alive, deleted;
E
Eric W. Biederman 已提交
1498 1499
	unsigned index;

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

E
Eric W. Biederman 已提交
1504 1505
		if (!rt)
			continue;
R
Roopa Prabhu 已提交
1506

1507
		alive = 0;
1508
		deleted = 0;
R
Roopa Prabhu 已提交
1509
		change_nexthops(rt) {
1510 1511
			unsigned int nh_flags = nh->nh_flags;

R
Roopa Prabhu 已提交
1512
			if (rtnl_dereference(nh->nh_dev) != dev)
1513 1514
				goto next;

R
Roopa Prabhu 已提交
1515 1516 1517
			switch (event) {
			case NETDEV_DOWN:
			case NETDEV_UNREGISTER:
1518
				nh_flags |= RTNH_F_DEAD;
R
Roopa Prabhu 已提交
1519 1520
				/* fall through */
			case NETDEV_CHANGE:
1521
				nh_flags |= RTNH_F_LINKDOWN;
R
Roopa Prabhu 已提交
1522 1523 1524 1525
				break;
			}
			if (event == NETDEV_UNREGISTER)
				RCU_INIT_POINTER(nh->nh_dev, NULL);
1526 1527 1528

			if (nh->nh_flags != nh_flags)
				WRITE_ONCE(nh->nh_flags, nh_flags);
1529
next:
1530
			if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
1531
				alive++;
1532 1533
			if (!rtnl_dereference(nh->nh_dev))
				deleted++;
R
Roopa Prabhu 已提交
1534
		} endfor_nexthops(rt);
1535 1536

		WRITE_ONCE(rt->rt_nhn_alive, alive);
1537 1538 1539 1540

		/* if there are no more nexthops, delete the route */
		if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
			mpls_route_update(net, index, NULL, NULL);
E
Eric W. Biederman 已提交
1541
	}
R
Roopa Prabhu 已提交
1542 1543
}

1544
static void mpls_ifup(struct net_device *dev, unsigned int flags)
R
Roopa Prabhu 已提交
1545 1546 1547 1548
{
	struct mpls_route __rcu **platform_label;
	struct net *net = dev_net(dev);
	unsigned index;
1549
	u8 alive;
R
Roopa Prabhu 已提交
1550 1551 1552 1553 1554 1555 1556

	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;
1557

R
Roopa Prabhu 已提交
1558 1559
		alive = 0;
		change_nexthops(rt) {
1560
			unsigned int nh_flags = nh->nh_flags;
R
Roopa Prabhu 已提交
1561 1562
			struct net_device *nh_dev =
				rtnl_dereference(nh->nh_dev);
R
Robert Shearman 已提交
1563

1564
			if (!(nh_flags & flags)) {
R
Roopa Prabhu 已提交
1565 1566 1567 1568 1569 1570
				alive++;
				continue;
			}
			if (nh_dev != dev)
				continue;
			alive++;
1571
			nh_flags &= ~flags;
1572
			WRITE_ONCE(nh->nh_flags, nh_flags);
R
Roopa Prabhu 已提交
1573 1574
		} endfor_nexthops(rt);

1575
		WRITE_ONCE(rt->rt_nhn_alive, alive);
R
Roopa Prabhu 已提交
1576
	}
E
Eric W. Biederman 已提交
1577 1578 1579 1580 1581 1582
}

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 已提交
1583
	struct mpls_dev *mdev;
R
Roopa Prabhu 已提交
1584
	unsigned int flags;
E
Eric W. Biederman 已提交
1585

R
Roopa Prabhu 已提交
1586
	if (event == NETDEV_REGISTER) {
1587 1588 1589
		mdev = mpls_add_dev(dev);
		if (IS_ERR(mdev))
			return notifier_from_errno(PTR_ERR(mdev));
1590

R
Roopa Prabhu 已提交
1591 1592
		return NOTIFY_OK;
	}
R
Robert Shearman 已提交
1593

R
Roopa Prabhu 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
	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 已提交
1616
	case NETDEV_UNREGISTER:
R
Roopa Prabhu 已提交
1617 1618 1619
		mpls_ifdown(dev, event);
		mdev = mpls_dev_get(dev);
		if (mdev) {
1620
			mpls_dev_sysctl_unregister(dev, mdev);
R
Roopa Prabhu 已提交
1621
			RCU_INIT_POINTER(dev->mpls_ptr, NULL);
R
Robert Shearman 已提交
1622
			call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
R
Roopa Prabhu 已提交
1623
		}
E
Eric W. Biederman 已提交
1624
		break;
1625 1626 1627 1628 1629
	case NETDEV_CHANGENAME:
		mdev = mpls_dev_get(dev);
		if (mdev) {
			int err;

1630
			mpls_dev_sysctl_unregister(dev, mdev);
1631 1632 1633 1634 1635
			err = mpls_dev_sysctl_register(dev, mdev);
			if (err)
				return notifier_from_errno(err);
		}
		break;
E
Eric W. Biederman 已提交
1636 1637 1638 1639 1640 1641 1642 1643
	}
	return NOTIFY_OK;
}

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

1644
static int nla_put_via(struct sk_buff *skb,
1645
		       u8 table, const void *addr, int alen)
1646
{
1647 1648 1649
	static const int table_to_family[NEIGH_NR_TABLES + 1] = {
		AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
	};
1650 1651
	struct nlattr *nla;
	struct rtvia *via;
1652
	int family = AF_UNSPEC;
1653 1654 1655 1656 1657

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

1658 1659 1660
	if (table <= NEIGH_NR_TABLES)
		family = table_to_family[table];

1661 1662 1663 1664 1665 1666
	via = nla_data(nla);
	via->rtvia_family = family;
	memcpy(via->rtvia_addr, addr, alen);
	return 0;
}

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
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;
}
1687
EXPORT_SYMBOL_GPL(nla_put_labels);
1688

1689 1690
int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
		   u32 label[], struct netlink_ext_ack *extack)
1691 1692 1693
{
	unsigned len = nla_len(nla);
	struct mpls_shim_hdr *nla_label;
1694
	u8 nla_labels;
1695 1696 1697
	bool bos;
	int i;

1698 1699 1700
	/* len needs to be an even multiple of 4 (the label size). Number
	 * of labels is a u8 so check for overflow.
	 */
1701 1702 1703
	if (len & 3 || len / 4 > 255) {
		NL_SET_ERR_MSG_ATTR(extack, nla,
				    "Invalid length for labels attribute");
1704
		return -EINVAL;
1705
	}
1706 1707 1708

	/* Limit the number of new labels allowed */
	nla_labels = len/4;
1709 1710
	if (nla_labels > max_labels) {
		NL_SET_ERR_MSG(extack, "Too many labels");
1711
		return -EINVAL;
1712
	}
1713

1714 1715 1716 1717
	/* when label == NULL, caller wants number of labels */
	if (!label)
		goto out;

1718 1719 1720 1721 1722 1723 1724 1725 1726
	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.
		 */
1727 1728 1729 1730 1731 1732 1733 1734 1735
		if (dec.ttl) {
			NL_SET_ERR_MSG_ATTR(extack, nla,
					    "TTL in label must be 0");
			return -EINVAL;
		}

		if (dec.tc) {
			NL_SET_ERR_MSG_ATTR(extack, nla,
					    "Traffic class in label must be 0");
1736
			return -EINVAL;
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
		}

		if (dec.bos != bos) {
			NL_SET_BAD_ATTR(extack, nla);
			if (bos) {
				NL_SET_ERR_MSG(extack,
					       "BOS bit must be set in first label");
			} else {
				NL_SET_ERR_MSG(extack,
					       "BOS bit can only be set in first label");
			}
			return -EINVAL;
		}
1750

1751
		switch (dec.label) {
1752
		case MPLS_LABEL_IMPLNULL:
1753 1754 1755 1756
			/* RFC3032: This is a label that an LSR may
			 * assign and distribute, but which never
			 * actually appears in the encapsulation.
			 */
1757 1758
			NL_SET_ERR_MSG_ATTR(extack, nla,
					    "Implicit NULL Label (3) can not be used in encapsulation");
1759 1760 1761
			return -EINVAL;
		}

1762 1763
		label[i] = dec.label;
	}
1764
out:
1765 1766 1767
	*labels = nla_labels;
	return 0;
}
1768
EXPORT_SYMBOL_GPL(nla_get_labels);
1769

1770 1771 1772 1773
static int rtm_to_route_config(struct sk_buff *skb,
			       struct nlmsghdr *nlh,
			       struct mpls_route_config *cfg,
			       struct netlink_ext_ack *extack)
1774 1775 1776 1777 1778 1779
{
	struct rtmsg *rtm;
	struct nlattr *tb[RTA_MAX+1];
	int index;
	int err;

1780 1781
	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
				     rtm_mpls_policy, extack);
1782 1783 1784 1785 1786 1787
	if (err < 0)
		goto errout;

	err = -EINVAL;
	rtm = nlmsg_data(nlh);

1788 1789
	if (rtm->rtm_family != AF_MPLS) {
		NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
1790
		goto errout;
1791 1792 1793
	}
	if (rtm->rtm_dst_len != 20) {
		NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
1794
		goto errout;
1795 1796 1797
	}
	if (rtm->rtm_src_len != 0) {
		NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
1798
		goto errout;
1799 1800 1801
	}
	if (rtm->rtm_tos != 0) {
		NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
1802
		goto errout;
1803 1804 1805 1806
	}
	if (rtm->rtm_table != RT_TABLE_MAIN) {
		NL_SET_ERR_MSG(extack,
			       "MPLS only supports the main route table");
1807
		goto errout;
1808
	}
1809 1810 1811 1812 1813 1814
	/* 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.
	 */
1815 1816 1817
	if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
		NL_SET_ERR_MSG(extack,
			       "Invalid route scope  - MPLS only supports UNIVERSE");
1818
		goto errout;
1819 1820 1821 1822
	}
	if (rtm->rtm_type != RTN_UNICAST) {
		NL_SET_ERR_MSG(extack,
			       "Invalid route type - MPLS only supports UNICAST");
1823
		goto errout;
1824 1825 1826
	}
	if (rtm->rtm_flags != 0) {
		NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
1827
		goto errout;
1828
	}
1829 1830 1831

	cfg->rc_label		= LABEL_NOT_SPECIFIED;
	cfg->rc_protocol	= rtm->rtm_protocol;
1832
	cfg->rc_via_table	= MPLS_NEIGH_TABLE_UNSPEC;
1833
	cfg->rc_ttl_propagate	= MPLS_TTL_PROP_DEFAULT;
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	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;

1844
		switch (index) {
1845 1846 1847 1848 1849 1850
		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,
1851
					   cfg->rc_output_label, extack))
1852 1853 1854 1855
				goto errout;
			break;
		case RTA_DST:
		{
R
Roopa Prabhu 已提交
1856
			u8 label_count;
1857
			if (nla_get_labels(nla, 1, &label_count,
1858
					   &cfg->rc_label, extack))
1859 1860
				goto errout;

1861
			if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1862
					   &cfg->rc_label, extack))
1863 1864 1865
				goto errout;
			break;
		}
1866 1867 1868
		case RTA_GATEWAY:
			NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
			goto errout;
1869 1870
		case RTA_VIA:
		{
R
Roopa Prabhu 已提交
1871
			if (nla_get_via(nla, &cfg->rc_via_alen,
1872 1873
					&cfg->rc_via_table, cfg->rc_via,
					extack))
1874
				goto errout;
R
Roopa Prabhu 已提交
1875 1876 1877 1878 1879 1880
			break;
		}
		case RTA_MULTIPATH:
		{
			cfg->rc_mp = nla_data(nla);
			cfg->rc_mp_len = nla_len(nla);
1881 1882
			break;
		}
1883 1884 1885 1886
		case RTA_TTL_PROPAGATE:
		{
			u8 ttl_propagate = nla_get_u8(nla);

1887 1888 1889
			if (ttl_propagate > 1) {
				NL_SET_ERR_MSG_ATTR(extack, nla,
						    "RTA_TTL_PROPAGATE can only be 0 or 1");
1890
				goto errout;
1891
			}
1892 1893 1894 1895 1896
			cfg->rc_ttl_propagate = ttl_propagate ?
				MPLS_TTL_PROP_ENABLED :
				MPLS_TTL_PROP_DISABLED;
			break;
		}
1897
		default:
1898
			NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
			/* Unsupported attribute */
			goto errout;
		}
	}

	err = 0;
errout:
	return err;
}

1909 1910
static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
1911
{
1912
	struct mpls_route_config *cfg;
1913 1914
	int err;

1915 1916 1917 1918
	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
	if (!cfg)
		return -ENOMEM;

1919
	err = rtm_to_route_config(skb, nlh, cfg, extack);
1920
	if (err < 0)
1921
		goto out;
1922

1923
	err = mpls_route_del(cfg, extack);
1924 1925 1926 1927
out:
	kfree(cfg);

	return err;
1928 1929 1930
}


1931 1932
static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
1933
{
1934
	struct mpls_route_config *cfg;
1935 1936
	int err;

1937 1938 1939 1940
	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
	if (!cfg)
		return -ENOMEM;

1941
	err = rtm_to_route_config(skb, nlh, cfg, extack);
1942
	if (err < 0)
1943
		goto out;
1944

1945
	err = mpls_route_add(cfg, extack);
1946 1947 1948 1949
out:
	kfree(cfg);

	return err;
1950 1951 1952 1953 1954
}

static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
			   u32 label, struct mpls_route *rt, int flags)
{
1955
	struct net_device *dev;
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
	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;
1976 1977 1978 1979 1980 1981 1982 1983 1984

	if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
		bool ttl_propagate =
			rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;

		if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
			       ttl_propagate))
			goto nla_put_failure;
	}
R
Roopa Prabhu 已提交
1985
	if (rt->rt_nhn == 1) {
1986
		const struct mpls_nh *nh = rt->rt_nh;
R
Roopa Prabhu 已提交
1987 1988 1989 1990 1991

		if (nh->nh_labels &&
		    nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
				   nh->nh_label))
			goto nla_put_failure;
1992
		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1993
		    nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
R
Roopa Prabhu 已提交
1994 1995 1996 1997 1998
				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 已提交
1999 2000 2001 2002
		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 已提交
2003 2004 2005
	} else {
		struct rtnexthop *rtnh;
		struct nlattr *mp;
2006 2007
		u8 linkdown = 0;
		u8 dead = 0;
R
Roopa Prabhu 已提交
2008

2009
		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
R
Roopa Prabhu 已提交
2010 2011 2012 2013
		if (!mp)
			goto nla_put_failure;

		for_nexthops(rt) {
2014 2015 2016 2017
			dev = rtnl_dereference(nh->nh_dev);
			if (!dev)
				continue;

R
Roopa Prabhu 已提交
2018 2019 2020 2021
			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
			if (!rtnh)
				goto nla_put_failure;

2022
			rtnh->rtnh_ifindex = dev->ifindex;
R
Roopa Prabhu 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031
			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 已提交
2032 2033 2034 2035
			if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
							    nh->nh_labels,
							    nh->nh_label))
				goto nla_put_failure;
2036 2037
			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
			    nla_put_via(skb, nh->nh_via_table,
2038
					mpls_nh_via(rt, nh),
R
Roopa Prabhu 已提交
2039 2040 2041 2042 2043 2044 2045
					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 已提交
2046 2047 2048 2049 2050
		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 已提交
2051 2052
		nla_nest_end(skb, mp);
	}
2053 2054 2055 2056 2057 2058 2059 2060 2061

	nlmsg_end(skb, nlh);
	return 0;

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

2062
#if IS_ENABLED(CONFIG_INET)
D
David Ahern 已提交
2063 2064
static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
				   struct fib_dump_filter *filter,
2065
				   struct netlink_callback *cb)
2066
{
2067
	return ip_valid_fib_dump_req(net, nlh, filter, cb);
2068 2069
}
#else
D
David Ahern 已提交
2070 2071
static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
				   struct fib_dump_filter *filter,
2072
				   struct netlink_callback *cb)
2073
{
2074
	struct netlink_ext_ack *extack = cb->extack;
2075
	struct nlattr *tb[RTA_MAX + 1];
2076
	struct rtmsg *rtm;
2077
	int err, i;
2078 2079 2080 2081 2082 2083 2084 2085

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

	rtm = nlmsg_data(nlh);
	if (rtm->rtm_dst_len || rtm->rtm_src_len  || rtm->rtm_tos   ||
2086 2087
	    rtm->rtm_table   || rtm->rtm_scope    || rtm->rtm_type  ||
	    rtm->rtm_flags) {
2088 2089 2090 2091
		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
		return -EINVAL;
	}

2092 2093 2094 2095 2096 2097
	if (rtm->rtm_protocol) {
		filter->protocol = rtm->rtm_protocol;
		filter->filter_set = 1;
		cb->answer_flags = NLM_F_DUMP_FILTERED;
	}

2098 2099
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
					    rtm_mpls_policy, extack);
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	if (err < 0)
		return err;

	for (i = 0; i <= RTA_MAX; ++i) {
		int ifindex;

		if (i == RTA_OIF) {
			ifindex = nla_get_u32(tb[i]);
			filter->dev = __dev_get_by_index(net, ifindex);
			if (!filter->dev)
				return -ENODEV;
			filter->filter_set = 1;
		} else if (tb[i]) {
			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
			return -EINVAL;
		}
2116 2117 2118 2119 2120 2121
	}

	return 0;
}
#endif

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
static bool mpls_rt_uses_dev(struct mpls_route *rt,
			     const struct net_device *dev)
{
	struct net_device *nh_dev;

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

		nh_dev = rtnl_dereference(nh->nh_dev);
		if (dev == nh_dev)
			return true;
	} else {
		for_nexthops(rt) {
			nh_dev = rtnl_dereference(nh->nh_dev);
			if (nh_dev == dev)
				return true;
		} endfor_nexthops(rt);
	}

	return false;
}

2144 2145
static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
{
2146
	const struct nlmsghdr *nlh = cb->nlh;
2147
	struct net *net = sock_net(skb->sk);
2148
	struct mpls_route __rcu **platform_label;
D
David Ahern 已提交
2149
	struct fib_dump_filter filter = {};
2150
	unsigned int flags = NLM_F_MULTI;
2151
	size_t platform_labels;
2152 2153 2154 2155
	unsigned int index;

	ASSERT_RTNL();

2156
	if (cb->strict_check) {
D
David Ahern 已提交
2157
		int err;
2158

2159
		err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
2160 2161
		if (err < 0)
			return err;
2162 2163 2164 2165 2166 2167 2168 2169

		/* for MPLS, there is only 1 table with fixed type and flags.
		 * If either are set in the filter then return nothing.
		 */
		if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
		    (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
		     filter.flags)
			return skb->len;
2170 2171
	}

2172
	index = cb->args[0];
2173 2174
	if (index < MPLS_LABEL_FIRST_UNRESERVED)
		index = MPLS_LABEL_FIRST_UNRESERVED;
2175

2176 2177
	platform_label = rtnl_dereference(net->mpls.platform_label);
	platform_labels = net->mpls.platform_labels;
2178 2179 2180 2181

	if (filter.filter_set)
		flags |= NLM_F_DUMP_FILTERED;

2182
	for (; index < platform_labels; index++) {
2183
		struct mpls_route *rt;
2184

2185
		rt = rtnl_dereference(platform_label[index]);
2186 2187 2188
		if (!rt)
			continue;

2189 2190 2191 2192
		if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
		    (filter.protocol && rt->rt_protocol != filter.protocol))
			continue;

2193 2194
		if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
				    cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2195
				    index, rt, flags) < 0)
2196 2197 2198 2199 2200 2201 2202
			break;
	}
	cb->args[0] = index;

	return skb->len;
}

2203 2204 2205 2206
static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
{
	size_t payload =
		NLMSG_ALIGN(sizeof(struct rtmsg))
2207 2208
		+ nla_total_size(4)			/* RTA_DST */
		+ nla_total_size(1);			/* RTA_TTL_PROPAGATE */
R
Roopa Prabhu 已提交
2209 2210 2211 2212 2213 2214

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

		if (nh->nh_dev)
			payload += nla_total_size(4); /* RTA_OIF */
2215
		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
2216
			payload += nla_total_size(2 + nh->nh_via_alen);
R
Roopa Prabhu 已提交
2217 2218 2219 2220 2221 2222 2223
		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) {
2224 2225
			if (!rtnl_dereference(nh->nh_dev))
				continue;
R
Roopa Prabhu 已提交
2226
			nhsize += nla_total_size(sizeof(struct rtnexthop));
2227 2228 2229
			/* RTA_VIA */
			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
				nhsize += nla_total_size(2 + nh->nh_via_alen);
R
Roopa Prabhu 已提交
2230 2231 2232 2233 2234 2235 2236
			if (nh->nh_labels)
				nhsize += nla_total_size(nh->nh_labels * 4);
		} endfor_nexthops(rt);
		/* nested attribute */
		payload += nla_total_size(nhsize);
	}

2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
	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);
}

2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
static int mpls_valid_getroute_req(struct sk_buff *skb,
				   const struct nlmsghdr *nlh,
				   struct nlattr **tb,
				   struct netlink_ext_ack *extack)
{
	struct rtmsg *rtm;
	int i, err;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
		NL_SET_ERR_MSG_MOD(extack,
				   "Invalid header for get route request");
		return -EINVAL;
	}

	if (!netlink_strict_get_check(skb))
2282 2283
		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
					      rtm_mpls_policy, extack);
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297

	rtm = nlmsg_data(nlh);
	if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
	    rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
	    rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
		return -EINVAL;
	}
	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
		NL_SET_ERR_MSG_MOD(extack,
				   "Invalid flags for get route request");
		return -EINVAL;
	}

2298 2299
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
					    rtm_mpls_policy, extack);
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
	if (err)
		return err;

	if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
		NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
		return -EINVAL;
	}

	for (i = 0; i <= RTA_MAX; i++) {
		if (!tb[i])
			continue;

		switch (i) {
		case RTA_DST:
		case RTA_NEWDST:
			break;
		default:
			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
			return -EINVAL;
		}
	}

	return 0;
}

R
Roopa Prabhu 已提交
2325 2326 2327 2328 2329
static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
			 struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(in_skb->sk);
	u32 portid = NETLINK_CB(in_skb).portid;
2330
	u32 in_label = LABEL_NOT_SPECIFIED;
R
Roopa Prabhu 已提交
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
	struct nlattr *tb[RTA_MAX + 1];
	u32 labels[MAX_NEW_LABELS];
	struct mpls_shim_hdr *hdr;
	unsigned int hdr_size = 0;
	struct net_device *dev;
	struct mpls_route *rt;
	struct rtmsg *rtm, *r;
	struct nlmsghdr *nlh;
	struct sk_buff *skb;
	struct mpls_nh *nh;
	u8 n_labels;
2342
	int err;
R
Roopa Prabhu 已提交
2343

2344
	err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
R
Roopa Prabhu 已提交
2345 2346 2347 2348 2349 2350 2351 2352 2353
	if (err < 0)
		goto errout;

	rtm = nlmsg_data(in_nlh);

	if (tb[RTA_DST]) {
		u8 label_count;

		if (nla_get_labels(tb[RTA_DST], 1, &label_count,
2354 2355
				   &in_label, extack)) {
			err = -EINVAL;
R
Roopa Prabhu 已提交
2356
			goto errout;
2357
		}
R
Roopa Prabhu 已提交
2358

2359
		if (!mpls_label_ok(net, &in_label, extack)) {
2360
			err = -EINVAL;
R
Roopa Prabhu 已提交
2361
			goto errout;
2362
		}
R
Roopa Prabhu 已提交
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
	}

	rt = mpls_route_input_rcu(net, in_label);
	if (!rt) {
		err = -ENETUNREACH;
		goto errout;
	}

	if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
		skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
		if (!skb) {
			err = -ENOBUFS;
			goto errout;
		}

		err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
				      RTM_NEWROUTE, in_label, rt, 0);
		if (err < 0) {
			/* -EMSGSIZE implies BUG in lfib_nlmsg_size */
			WARN_ON(err == -EMSGSIZE);
			goto errout_free;
		}

		return rtnl_unicast(skb, net, portid);
	}

	if (tb[RTA_NEWDST]) {
		if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
				   labels, extack) != 0) {
			err = -EINVAL;
			goto errout;
		}

		hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
	}

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb) {
		err = -ENOBUFS;
		goto errout;
	}

	skb->protocol = htons(ETH_P_MPLS_UC);

	if (hdr_size) {
		bool bos;
		int i;

		if (skb_cow(skb, hdr_size)) {
			err = -ENOBUFS;
			goto errout_free;
		}

		skb_reserve(skb, hdr_size);
		skb_push(skb, hdr_size);
		skb_reset_network_header(skb);

		/* Push new labels */
		hdr = mpls_hdr(skb);
		bos = true;
		for (i = n_labels - 1; i >= 0; i--) {
			hdr[i] = mpls_entry_encode(labels[i],
						   1, 0, bos);
			bos = false;
		}
	}

	nh = mpls_select_multipath(rt, skb);
	if (!nh) {
		err = -ENETUNREACH;
		goto errout_free;
	}

	if (hdr_size) {
		skb_pull(skb, hdr_size);
		skb_reset_network_header(skb);
	}

	nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
			RTM_NEWROUTE, sizeof(*r), 0);
	if (!nlh) {
		err = -EMSGSIZE;
		goto errout_free;
	}

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

	if (nla_put_labels(skb, RTA_DST, 1, &in_label))
		goto nla_put_failure;

	if (nh->nh_labels &&
	    nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
			   nh->nh_label))
		goto nla_put_failure;

	if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
	    nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
			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;

	nlmsg_end(skb, nlh);

	err = rtnl_unicast(skb, net, portid);
errout:
	return err;

nla_put_failure:
	nlmsg_cancel(skb, nlh);
	err = -EMSGSIZE;
errout_free:
	kfree_skb(skb);
	return err;
}

2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
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) {
2498
		labels = kvzalloc(size, GFP_KERNEL);
2499 2500 2501 2502 2503
		if (!labels)
			goto nolabels;
	}

	/* In case the predefined labels need to be populated */
2504
	if (limit > MPLS_LABEL_IPV4NULL) {
2505
		struct net_device *lo = net->loopback_dev;
2506
		rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
2507
		if (IS_ERR(rt0))
2508
			goto nort0;
R
Roopa Prabhu 已提交
2509
		RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
2510
		rt0->rt_protocol = RTPROT_KERNEL;
2511
		rt0->rt_payload_type = MPT_IPV4;
2512
		rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
R
Roopa Prabhu 已提交
2513
		rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2514
		rt0->rt_nh->nh_via_alen = lo->addr_len;
2515 2516
		memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
		       lo->addr_len);
2517
	}
2518
	if (limit > MPLS_LABEL_IPV6NULL) {
2519
		struct net_device *lo = net->loopback_dev;
2520
		rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
2521
		if (IS_ERR(rt2))
2522
			goto nort2;
R
Roopa Prabhu 已提交
2523
		RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
2524
		rt2->rt_protocol = RTPROT_KERNEL;
2525
		rt2->rt_payload_type = MPT_IPV6;
2526
		rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
R
Roopa Prabhu 已提交
2527
		rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2528
		rt2->rt_nh->nh_via_alen = lo->addr_len;
2529 2530
		memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
		       lo->addr_len);
2531 2532 2533 2534
	}

	rtnl_lock();
	/* Remember the original table */
2535
	old = rtnl_dereference(net->mpls.platform_label);
2536 2537 2538 2539
	old_limit = net->mpls.platform_labels;

	/* Free any labels beyond the new table */
	for (index = limit; index < old_limit; index++)
R
Roopa Prabhu 已提交
2540
		mpls_route_update(net, index, NULL, NULL);
2541 2542 2543 2544 2545 2546 2547 2548 2549

	/* 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 */
2550 2551 2552
	if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
	    (limit > MPLS_LABEL_IPV6NULL)) {
		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
2553 2554 2555
		rt2 = NULL;
	}

2556 2557 2558
	if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
	    (limit > MPLS_LABEL_IPV4NULL)) {
		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
2559 2560 2561 2562 2563
		rt0 = NULL;
	}

	/* Update the global pointers */
	net->mpls.platform_labels = limit;
2564
	rcu_assign_pointer(net->mpls.platform_label, labels);
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585

	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,
2586
				void *buffer, size_t *lenp, loff_t *ppos)
2587 2588 2589 2590 2591 2592 2593 2594 2595
{
	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,
2596
		.extra1		= SYSCTL_ZERO,
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
		.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;
}

2608 2609 2610
#define MPLS_NS_SYSCTL_OFFSET(field)		\
	(&((struct net *)0)->field)

2611
static const struct ctl_table mpls_table[] = {
2612 2613 2614 2615 2616 2617 2618
	{
		.procname	= "platform_labels",
		.data		= NULL,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= mpls_platform_labels,
	},
2619 2620 2621 2622 2623 2624
	{
		.procname	= "ip_ttl_propagate",
		.data		= MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec_minmax,
2625 2626
		.extra1		= SYSCTL_ZERO,
		.extra2		= SYSCTL_ONE,
2627
	},
2628 2629 2630 2631 2632 2633
	{
		.procname	= "default_ttl",
		.data		= MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= proc_dointvec_minmax,
2634
		.extra1		= SYSCTL_ONE,
2635 2636
		.extra2		= &ttl_max,
	},
2637 2638 2639
	{ }
};

E
Eric W. Biederman 已提交
2640 2641
static int mpls_net_init(struct net *net)
{
2642
	struct ctl_table *table;
2643
	int i;
2644

E
Eric W. Biederman 已提交
2645 2646
	net->mpls.platform_labels = 0;
	net->mpls.platform_label = NULL;
2647
	net->mpls.ip_ttl_propagate = 1;
2648
	net->mpls.default_ttl = 255;
E
Eric W. Biederman 已提交
2649

2650 2651 2652 2653
	table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
	if (table == NULL)
		return -ENOMEM;

2654 2655 2656 2657 2658 2659
	/* 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_table) - 1; i++)
		table[i].data = (char *)net + (uintptr_t)table[i].data;

2660
	net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
2661 2662
	if (net->mpls.ctl == NULL) {
		kfree(table);
2663
		return -ENOMEM;
2664
	}
2665

E
Eric W. Biederman 已提交
2666 2667 2668 2669 2670
	return 0;
}

static void mpls_net_exit(struct net *net)
{
2671 2672
	struct mpls_route __rcu **platform_label;
	size_t platform_labels;
2673
	struct ctl_table *table;
E
Eric W. Biederman 已提交
2674 2675
	unsigned int index;

2676 2677 2678 2679
	table = net->mpls.ctl->ctl_table_arg;
	unregister_net_sysctl_table(net->mpls.ctl);
	kfree(table);

2680 2681
	/* 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 已提交
2682 2683 2684 2685 2686 2687 2688 2689
	 * 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();
2690 2691 2692 2693 2694
	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);
2695
		mpls_notify_route(net, index, rt, NULL, NULL);
E
Eric W. Biederman 已提交
2696 2697 2698 2699
		mpls_rt_free(rt);
	}
	rtnl_unlock();

2700
	kvfree(platform_label);
E
Eric W. Biederman 已提交
2701 2702 2703 2704 2705 2706 2707
}

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

R
Robert Shearman 已提交
2708 2709 2710 2711 2712 2713
static struct rtnl_af_ops mpls_af_ops __read_mostly = {
	.family		   = AF_MPLS,
	.fill_stats_af	   = mpls_fill_stats_af,
	.get_stats_af_size = mpls_get_stats_af_size,
};

E
Eric W. Biederman 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
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);

R
Robert Shearman 已提交
2730 2731
	rtnl_af_register(&mpls_af_ops);

2732 2733 2734 2735 2736 2737 2738 2739 2740
	rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_NEWROUTE,
			     mpls_rtm_newroute, NULL, 0);
	rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_DELROUTE,
			     mpls_rtm_delroute, NULL, 0);
	rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETROUTE,
			     mpls_getroute, mpls_dump_routes, 0);
	rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
			     mpls_netconf_get_devconf,
			     mpls_netconf_dump_devconf, 0);
2741 2742 2743 2744
	err = ipgre_tunnel_encap_add_mpls_ops();
	if (err)
		pr_err("Can't add mpls over gre tunnel ops\n");

E
Eric W. Biederman 已提交
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
	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)
{
2757
	rtnl_unregister_all(PF_MPLS);
R
Robert Shearman 已提交
2758
	rtnl_af_unregister(&mpls_af_ops);
E
Eric W. Biederman 已提交
2759 2760 2761
	dev_remove_pack(&mpls_packet_type);
	unregister_netdevice_notifier(&mpls_dev_notifier);
	unregister_pernet_subsys(&mpls_net_ops);
2762
	ipgre_tunnel_encap_del_mpls_ops();
E
Eric W. Biederman 已提交
2763 2764 2765 2766 2767 2768
}
module_exit(mpls_exit);

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