xref: /linux/net/mpls/af_mpls.c (revision 21ef2d065ad3f0cfbf2ae51260bf962a9fa2c643)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/skbuff.h>
4 #include <linux/socket.h>
5 #include <linux/sysctl.h>
6 #include <linux/net.h>
7 #include <linux/module.h>
8 #include <linux/if_arp.h>
9 #include <linux/ipv6.h>
10 #include <linux/mpls.h>
11 #include <linux/netconf.h>
12 #include <linux/nospec.h>
13 #include <linux/vmalloc.h>
14 #include <linux/percpu.h>
15 #include <net/gso.h>
16 #include <net/ip.h>
17 #include <net/dst.h>
18 #include <net/sock.h>
19 #include <net/arp.h>
20 #include <net/ip_fib.h>
21 #include <net/netevent.h>
22 #include <net/ip_tunnels.h>
23 #include <net/netns/generic.h>
24 #if IS_ENABLED(CONFIG_IPV6)
25 #include <net/ipv6.h>
26 #endif
27 #include <net/rtnh.h>
28 #include "internal.h"
29 
30 /* max memory we will use for mpls_route */
31 #define MAX_MPLS_ROUTE_MEM	4096
32 
33 /* Maximum number of labels to look ahead at when selecting a path of
34  * a multipath route
35  */
36 #define MAX_MP_SELECT_LABELS 4
37 
38 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
39 
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 	return sizeof(struct mpls_shim_hdr);
47 }
48 
49 static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
50 	.encap_hlen	= ipgre_mpls_encap_hlen,
51 };
52 
53 static int ipgre_tunnel_encap_add_mpls_ops(void)
54 {
55 	return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
56 }
57 
58 static void ipgre_tunnel_encap_del_mpls_ops(void)
59 {
60 	ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
61 }
62 #else
63 static int ipgre_tunnel_encap_add_mpls_ops(void)
64 {
65 	return 0;
66 }
67 
68 static void ipgre_tunnel_encap_del_mpls_ops(void)
69 {
70 }
71 #endif
72 
73 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
74 		       struct nlmsghdr *nlh, struct net *net, u32 portid,
75 		       unsigned int nlm_flags);
76 
77 static struct mpls_route *mpls_route_input(struct net *net, unsigned int index)
78 {
79 	struct mpls_route __rcu **platform_label;
80 
81 	platform_label = mpls_dereference(net, net->mpls.platform_label);
82 	return mpls_dereference(net, platform_label[index]);
83 }
84 
85 static struct mpls_route __rcu **mpls_platform_label_rcu(struct net *net, size_t *platform_labels)
86 {
87 	struct mpls_route __rcu **platform_label;
88 	unsigned int sequence;
89 
90 	do {
91 		sequence = read_seqcount_begin(&net->mpls.platform_label_seq);
92 		platform_label = rcu_dereference(net->mpls.platform_label);
93 		*platform_labels = net->mpls.platform_labels;
94 	} while (read_seqcount_retry(&net->mpls.platform_label_seq, sequence));
95 
96 	return platform_label;
97 }
98 
99 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned int index)
100 {
101 	struct mpls_route __rcu **platform_label;
102 	size_t platform_labels;
103 
104 	platform_label = mpls_platform_label_rcu(net, &platform_labels);
105 
106 	if (index >= platform_labels)
107 		return NULL;
108 
109 	return rcu_dereference(platform_label[index]);
110 }
111 
112 bool mpls_output_possible(const struct net_device *dev)
113 {
114 	return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
115 }
116 EXPORT_SYMBOL_GPL(mpls_output_possible);
117 
118 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
119 {
120 	return (u8 *)nh + rt->rt_via_offset;
121 }
122 
123 static const u8 *mpls_nh_via(const struct mpls_route *rt,
124 			     const struct mpls_nh *nh)
125 {
126 	return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
127 }
128 
129 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
130 {
131 	/* The size of the layer 2.5 labels to be added for this route */
132 	return nh->nh_labels * sizeof(struct mpls_shim_hdr);
133 }
134 
135 unsigned int mpls_dev_mtu(const struct net_device *dev)
136 {
137 	/* The amount of data the layer 2 frame can hold */
138 	return dev->mtu;
139 }
140 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
141 
142 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
143 {
144 	if (skb->len <= mtu)
145 		return false;
146 
147 	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
148 		return false;
149 
150 	return true;
151 }
152 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
153 
154 void mpls_stats_inc_outucastpkts(struct net *net,
155 				 struct net_device *dev,
156 				 const struct sk_buff *skb)
157 {
158 	struct mpls_dev *mdev;
159 
160 	if (skb->protocol == htons(ETH_P_MPLS_UC)) {
161 		mdev = mpls_dev_rcu(dev);
162 		if (mdev)
163 			MPLS_INC_STATS_LEN(mdev, skb->len,
164 					   tx_packets,
165 					   tx_bytes);
166 	} else if (skb->protocol == htons(ETH_P_IP)) {
167 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
168 #if IS_ENABLED(CONFIG_IPV6)
169 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
170 		struct inet6_dev *in6dev = in6_dev_rcu(dev);
171 
172 		if (in6dev)
173 			IP6_UPD_PO_STATS(net, in6dev,
174 					 IPSTATS_MIB_OUT, skb->len);
175 #endif
176 	}
177 }
178 EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
179 
180 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
181 {
182 	struct mpls_entry_decoded dec;
183 	unsigned int mpls_hdr_len = 0;
184 	struct mpls_shim_hdr *hdr;
185 	bool eli_seen = false;
186 	int label_index;
187 	u32 hash = 0;
188 
189 	for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
190 	     label_index++) {
191 		mpls_hdr_len += sizeof(*hdr);
192 		if (!pskb_may_pull(skb, mpls_hdr_len))
193 			break;
194 
195 		/* Read and decode the current label */
196 		hdr = mpls_hdr(skb) + label_index;
197 		dec = mpls_entry_decode(hdr);
198 
199 		/* RFC6790 - reserved labels MUST NOT be used as keys
200 		 * for the load-balancing function
201 		 */
202 		if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
203 			hash = jhash_1word(dec.label, hash);
204 
205 			/* The entropy label follows the entropy label
206 			 * indicator, so this means that the entropy
207 			 * label was just added to the hash - no need to
208 			 * go any deeper either in the label stack or in the
209 			 * payload
210 			 */
211 			if (eli_seen)
212 				break;
213 		} else if (dec.label == MPLS_LABEL_ENTROPY) {
214 			eli_seen = true;
215 		}
216 
217 		if (!dec.bos)
218 			continue;
219 
220 		/* found bottom label; does skb have room for a header? */
221 		if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
222 			const struct iphdr *v4hdr;
223 
224 			v4hdr = (const struct iphdr *)(hdr + 1);
225 			if (v4hdr->version == 4) {
226 				hash = jhash_3words(ntohl(v4hdr->saddr),
227 						    ntohl(v4hdr->daddr),
228 						    v4hdr->protocol, hash);
229 			} else if (v4hdr->version == 6 &&
230 				   pskb_may_pull(skb, mpls_hdr_len +
231 						 sizeof(struct ipv6hdr))) {
232 				const struct ipv6hdr *v6hdr;
233 
234 				v6hdr = (const struct ipv6hdr *)(hdr + 1);
235 				hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
236 				hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
237 				hash = jhash_1word(v6hdr->nexthdr, hash);
238 			}
239 		}
240 
241 		break;
242 	}
243 
244 	return hash;
245 }
246 
247 static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
248 {
249 	return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
250 }
251 
252 /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
253  * a next hop (nh->nh_flags) are modified by netdev event handlers.
254  * Since those fields can change at any moment, use READ_ONCE to
255  * access both.
256  */
257 static const struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
258 						   struct sk_buff *skb)
259 {
260 	u32 hash = 0;
261 	int nh_index = 0;
262 	int n = 0;
263 	u8 alive;
264 
265 	/* No need to look further into packet if there's only
266 	 * one path
267 	 */
268 	if (rt->rt_nhn == 1)
269 		return rt->rt_nh;
270 
271 	alive = READ_ONCE(rt->rt_nhn_alive);
272 	if (alive == 0)
273 		return NULL;
274 
275 	hash = mpls_multipath_hash(rt, skb);
276 	nh_index = hash % alive;
277 	if (alive == rt->rt_nhn)
278 		goto out;
279 	for_nexthops(rt) {
280 		unsigned int nh_flags = READ_ONCE(nh->nh_flags);
281 
282 		if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
283 			continue;
284 		if (n == nh_index)
285 			return nh;
286 		n++;
287 	} endfor_nexthops(rt);
288 
289 out:
290 	return mpls_get_nexthop(rt, nh_index);
291 }
292 
293 static bool mpls_egress(struct net *net, struct mpls_route *rt,
294 			struct sk_buff *skb, struct mpls_entry_decoded dec)
295 {
296 	enum mpls_payload_type payload_type;
297 	bool success = false;
298 
299 	/* The IPv4 code below accesses through the IPv4 header
300 	 * checksum, which is 12 bytes into the packet.
301 	 * The IPv6 code below accesses through the IPv6 hop limit
302 	 * which is 8 bytes into the packet.
303 	 *
304 	 * For all supported cases there should always be at least 12
305 	 * bytes of packet data present.  The IPv4 header is 20 bytes
306 	 * without options and the IPv6 header is always 40 bytes
307 	 * long.
308 	 */
309 	if (!pskb_may_pull(skb, 12))
310 		return false;
311 
312 	payload_type = rt->rt_payload_type;
313 	if (payload_type == MPT_UNSPEC)
314 		payload_type = ip_hdr(skb)->version;
315 
316 	switch (payload_type) {
317 	case MPT_IPV4: {
318 		struct iphdr *hdr4 = ip_hdr(skb);
319 		u8 new_ttl;
320 		skb->protocol = htons(ETH_P_IP);
321 
322 		/* If propagating TTL, take the decremented TTL from
323 		 * the incoming MPLS header, otherwise decrement the
324 		 * TTL, but only if not 0 to avoid underflow.
325 		 */
326 		if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
327 		    (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
328 		     net->mpls.ip_ttl_propagate))
329 			new_ttl = dec.ttl;
330 		else
331 			new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
332 
333 		csum_replace2(&hdr4->check,
334 			      htons(hdr4->ttl << 8),
335 			      htons(new_ttl << 8));
336 		hdr4->ttl = new_ttl;
337 		success = true;
338 		break;
339 	}
340 	case MPT_IPV6: {
341 		struct ipv6hdr *hdr6 = ipv6_hdr(skb);
342 		skb->protocol = htons(ETH_P_IPV6);
343 
344 		/* If propagating TTL, take the decremented TTL from
345 		 * the incoming MPLS header, otherwise decrement the
346 		 * hop limit, but only if not 0 to avoid underflow.
347 		 */
348 		if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
349 		    (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
350 		     net->mpls.ip_ttl_propagate))
351 			hdr6->hop_limit = dec.ttl;
352 		else if (hdr6->hop_limit)
353 			hdr6->hop_limit = hdr6->hop_limit - 1;
354 		success = true;
355 		break;
356 	}
357 	case MPT_UNSPEC:
358 		/* Should have decided which protocol it is by now */
359 		break;
360 	}
361 
362 	return success;
363 }
364 
365 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
366 			struct packet_type *pt, struct net_device *orig_dev)
367 {
368 	struct net *net = dev_net_rcu(dev);
369 	struct mpls_shim_hdr *hdr;
370 	const struct mpls_nh *nh;
371 	struct mpls_route *rt;
372 	struct mpls_entry_decoded dec;
373 	struct net_device *out_dev;
374 	struct mpls_dev *out_mdev;
375 	struct mpls_dev *mdev;
376 	unsigned int hh_len;
377 	unsigned int new_header_size;
378 	unsigned int mtu;
379 	int err;
380 
381 	/* Careful this entire function runs inside of an rcu critical section */
382 
383 	mdev = mpls_dev_rcu(dev);
384 	if (!mdev)
385 		goto drop;
386 
387 	MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
388 			   rx_bytes);
389 
390 	if (!mdev->input_enabled) {
391 		MPLS_INC_STATS(mdev, rx_dropped);
392 		goto drop;
393 	}
394 
395 	if (skb->pkt_type != PACKET_HOST)
396 		goto err;
397 
398 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
399 		goto err;
400 
401 	if (!pskb_may_pull(skb, sizeof(*hdr)))
402 		goto err;
403 
404 	skb_dst_drop(skb);
405 
406 	/* Read and decode the label */
407 	hdr = mpls_hdr(skb);
408 	dec = mpls_entry_decode(hdr);
409 
410 	rt = mpls_route_input_rcu(net, dec.label);
411 	if (!rt) {
412 		MPLS_INC_STATS(mdev, rx_noroute);
413 		goto drop;
414 	}
415 
416 	nh = mpls_select_multipath(rt, skb);
417 	if (!nh)
418 		goto err;
419 
420 	/* Pop the label */
421 	skb_pull(skb, sizeof(*hdr));
422 	skb_reset_network_header(skb);
423 
424 	skb_orphan(skb);
425 
426 	if (skb_warn_if_lro(skb))
427 		goto err;
428 
429 	skb_forward_csum(skb);
430 
431 	/* Verify ttl is valid */
432 	if (dec.ttl <= 1)
433 		goto err;
434 
435 	/* Find the output device */
436 	out_dev = nh->nh_dev;
437 	if (!mpls_output_possible(out_dev))
438 		goto tx_err;
439 
440 	/* Verify the destination can hold the packet */
441 	new_header_size = mpls_nh_header_size(nh);
442 	mtu = mpls_dev_mtu(out_dev);
443 	if (mpls_pkt_too_big(skb, mtu - new_header_size))
444 		goto tx_err;
445 
446 	hh_len = LL_RESERVED_SPACE(out_dev);
447 	if (!out_dev->header_ops)
448 		hh_len = 0;
449 
450 	/* Ensure there is enough space for the headers in the skb */
451 	if (skb_cow(skb, hh_len + new_header_size))
452 		goto tx_err;
453 
454 	skb->dev = out_dev;
455 	skb->protocol = htons(ETH_P_MPLS_UC);
456 
457 	dec.ttl -= 1;
458 	if (unlikely(!new_header_size && dec.bos)) {
459 		/* Penultimate hop popping */
460 		if (!mpls_egress(net, rt, skb, dec))
461 			goto err;
462 	} else {
463 		bool bos;
464 		int i;
465 		skb_push(skb, new_header_size);
466 		skb_reset_network_header(skb);
467 		/* Push the new labels */
468 		hdr = mpls_hdr(skb);
469 		bos = dec.bos;
470 		for (i = nh->nh_labels - 1; i >= 0; i--) {
471 			hdr[i] = mpls_entry_encode(nh->nh_label[i],
472 						   dec.ttl, 0, bos);
473 			bos = false;
474 		}
475 	}
476 
477 	mpls_stats_inc_outucastpkts(net, out_dev, skb);
478 
479 	/* If via wasn't specified then send out using device address */
480 	if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
481 		err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
482 				 out_dev->dev_addr, skb);
483 	else
484 		err = neigh_xmit(nh->nh_via_table, out_dev,
485 				 mpls_nh_via(rt, nh), skb);
486 	if (err)
487 		net_dbg_ratelimited("%s: packet transmission failed: %d\n",
488 				    __func__, err);
489 	return 0;
490 
491 tx_err:
492 	out_mdev = out_dev ? mpls_dev_rcu(out_dev) : NULL;
493 	if (out_mdev)
494 		MPLS_INC_STATS(out_mdev, tx_errors);
495 	goto drop;
496 err:
497 	MPLS_INC_STATS(mdev, rx_errors);
498 drop:
499 	kfree_skb(skb);
500 	return NET_RX_DROP;
501 }
502 
503 static struct packet_type mpls_packet_type __read_mostly = {
504 	.type = cpu_to_be16(ETH_P_MPLS_UC),
505 	.func = mpls_forward,
506 };
507 
508 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
509 	[RTA_DST]		= { .type = NLA_U32 },
510 	[RTA_OIF]		= { .type = NLA_U32 },
511 	[RTA_TTL_PROPAGATE]	= { .type = NLA_U8 },
512 };
513 
514 struct mpls_route_config {
515 	u32			rc_protocol;
516 	u32			rc_ifindex;
517 	u8			rc_via_table;
518 	u8			rc_via_alen;
519 	u8			rc_via[MAX_VIA_ALEN];
520 	u32			rc_label;
521 	u8			rc_ttl_propagate;
522 	u8			rc_output_labels;
523 	u32			rc_output_label[MAX_NEW_LABELS];
524 	u32			rc_nlflags;
525 	enum mpls_payload_type	rc_payload_type;
526 	struct nl_info		rc_nlinfo;
527 	struct rtnexthop	*rc_mp;
528 	int			rc_mp_len;
529 };
530 
531 /* all nexthops within a route have the same size based on max
532  * number of labels and max via length for a hop
533  */
534 static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
535 {
536 	u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
537 	struct mpls_route *rt;
538 	size_t size;
539 
540 	size = sizeof(*rt) + num_nh * nh_size;
541 	if (size > MAX_MPLS_ROUTE_MEM)
542 		return ERR_PTR(-EINVAL);
543 
544 	rt = kzalloc(size, GFP_KERNEL);
545 	if (!rt)
546 		return ERR_PTR(-ENOMEM);
547 
548 	rt->rt_nhn = num_nh;
549 	rt->rt_nhn_alive = num_nh;
550 	rt->rt_nh_size = nh_size;
551 	rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
552 
553 	return rt;
554 }
555 
556 static void mpls_rt_free_rcu(struct rcu_head *head)
557 {
558 	struct mpls_route *rt;
559 
560 	rt = container_of(head, struct mpls_route, rt_rcu);
561 
562 	change_nexthops(rt) {
563 		netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
564 	} endfor_nexthops(rt);
565 
566 	kfree(rt);
567 }
568 
569 static void mpls_rt_free(struct mpls_route *rt)
570 {
571 	if (rt)
572 		call_rcu(&rt->rt_rcu, mpls_rt_free_rcu);
573 }
574 
575 static void mpls_notify_route(struct net *net, unsigned index,
576 			      struct mpls_route *old, struct mpls_route *new,
577 			      const struct nl_info *info)
578 {
579 	struct nlmsghdr *nlh = info ? info->nlh : NULL;
580 	unsigned portid = info ? info->portid : 0;
581 	int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
582 	struct mpls_route *rt = new ? new : old;
583 	unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
584 	/* Ignore reserved labels for now */
585 	if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
586 		rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
587 }
588 
589 static void mpls_route_update(struct net *net, unsigned index,
590 			      struct mpls_route *new,
591 			      const struct nl_info *info)
592 {
593 	struct mpls_route __rcu **platform_label;
594 	struct mpls_route *rt;
595 
596 	platform_label = mpls_dereference(net, net->mpls.platform_label);
597 	rt = mpls_dereference(net, platform_label[index]);
598 	rcu_assign_pointer(platform_label[index], new);
599 
600 	mpls_notify_route(net, index, rt, new, info);
601 
602 	/* If we removed a route free it now */
603 	mpls_rt_free(rt);
604 }
605 
606 static unsigned int find_free_label(struct net *net)
607 {
608 	unsigned int index;
609 
610 	for (index = MPLS_LABEL_FIRST_UNRESERVED;
611 	     index < net->mpls.platform_labels;
612 	     index++) {
613 		if (!mpls_route_input(net, index))
614 			return index;
615 	}
616 
617 	return LABEL_NOT_SPECIFIED;
618 }
619 
620 #if IS_ENABLED(CONFIG_INET)
621 static struct net_device *inet_fib_lookup_dev(struct net *net,
622 					      struct mpls_nh *nh,
623 					      const void *addr)
624 {
625 	struct net_device *dev;
626 	struct rtable *rt;
627 	struct in_addr daddr;
628 
629 	memcpy(&daddr, addr, sizeof(struct in_addr));
630 	rt = ip_route_output(net, daddr.s_addr, 0, 0, 0, RT_SCOPE_UNIVERSE);
631 	if (IS_ERR(rt))
632 		return ERR_CAST(rt);
633 
634 	dev = rt->dst.dev;
635 	netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
636 	ip_rt_put(rt);
637 
638 	return dev;
639 }
640 #else
641 static struct net_device *inet_fib_lookup_dev(struct net *net,
642 					      struct mpls_nh *nh,
643 					      const void *addr)
644 {
645 	return ERR_PTR(-EAFNOSUPPORT);
646 }
647 #endif
648 
649 #if IS_ENABLED(CONFIG_IPV6)
650 static struct net_device *inet6_fib_lookup_dev(struct net *net,
651 					       struct mpls_nh *nh,
652 					       const void *addr)
653 {
654 	struct net_device *dev;
655 	struct dst_entry *dst;
656 	struct flowi6 fl6;
657 
658 	memset(&fl6, 0, sizeof(fl6));
659 	memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
660 	dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL);
661 	if (IS_ERR(dst))
662 		return ERR_CAST(dst);
663 
664 	dev = dst->dev;
665 	netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
666 	dst_release(dst);
667 
668 	return dev;
669 }
670 #else
671 static struct net_device *inet6_fib_lookup_dev(struct net *net,
672 					       struct mpls_nh *nh,
673 					       const void *addr)
674 {
675 	return ERR_PTR(-EAFNOSUPPORT);
676 }
677 #endif
678 
679 static struct net_device *find_outdev(struct net *net,
680 				      struct mpls_route *rt,
681 				      struct mpls_nh *nh, int oif)
682 {
683 	struct net_device *dev = NULL;
684 
685 	if (!oif) {
686 		switch (nh->nh_via_table) {
687 		case NEIGH_ARP_TABLE:
688 			dev = inet_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
689 			break;
690 		case NEIGH_ND_TABLE:
691 			dev = inet6_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
692 			break;
693 		case NEIGH_LINK_TABLE:
694 			break;
695 		}
696 	} else {
697 		dev = netdev_get_by_index(net, oif,
698 					  &nh->nh_dev_tracker, GFP_KERNEL);
699 	}
700 
701 	if (!dev)
702 		return ERR_PTR(-ENODEV);
703 
704 	if (IS_ERR(dev))
705 		return dev;
706 
707 	nh->nh_dev = dev;
708 
709 	return dev;
710 }
711 
712 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
713 			      struct mpls_nh *nh, int oif)
714 {
715 	struct net_device *dev = NULL;
716 	int err = -ENODEV;
717 
718 	dev = find_outdev(net, rt, nh, oif);
719 	if (IS_ERR(dev)) {
720 		err = PTR_ERR(dev);
721 		goto errout;
722 	}
723 
724 	/* Ensure this is a supported device */
725 	err = -EINVAL;
726 	if (!mpls_dev_get(net, dev))
727 		goto errout_put;
728 
729 	if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
730 	    (dev->addr_len != nh->nh_via_alen))
731 		goto errout_put;
732 
733 	if (!(dev->flags & IFF_UP)) {
734 		nh->nh_flags |= RTNH_F_DEAD;
735 	} else {
736 		unsigned int flags;
737 
738 		flags = netif_get_flags(dev);
739 		if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
740 			nh->nh_flags |= RTNH_F_LINKDOWN;
741 	}
742 
743 	return 0;
744 
745 errout_put:
746 	netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
747 	nh->nh_dev = NULL;
748 errout:
749 	return err;
750 }
751 
752 static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
753 		       u8 via_addr[], struct netlink_ext_ack *extack)
754 {
755 	struct rtvia *via = nla_data(nla);
756 	int err = -EINVAL;
757 	int alen;
758 
759 	if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
760 		NL_SET_ERR_MSG_ATTR(extack, nla,
761 				    "Invalid attribute length for RTA_VIA");
762 		goto errout;
763 	}
764 	alen = nla_len(nla) -
765 			offsetof(struct rtvia, rtvia_addr);
766 	if (alen > MAX_VIA_ALEN) {
767 		NL_SET_ERR_MSG_ATTR(extack, nla,
768 				    "Invalid address length for RTA_VIA");
769 		goto errout;
770 	}
771 
772 	/* Validate the address family */
773 	switch (via->rtvia_family) {
774 	case AF_PACKET:
775 		*via_table = NEIGH_LINK_TABLE;
776 		break;
777 	case AF_INET:
778 		*via_table = NEIGH_ARP_TABLE;
779 		if (alen != 4)
780 			goto errout;
781 		break;
782 	case AF_INET6:
783 		*via_table = NEIGH_ND_TABLE;
784 		if (alen != 16)
785 			goto errout;
786 		break;
787 	default:
788 		/* Unsupported address family */
789 		goto errout;
790 	}
791 
792 	memcpy(via_addr, via->rtvia_addr, alen);
793 	*via_alen = alen;
794 	err = 0;
795 
796 errout:
797 	return err;
798 }
799 
800 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
801 				  struct mpls_route *rt)
802 {
803 	struct net *net = cfg->rc_nlinfo.nl_net;
804 	struct mpls_nh *nh = rt->rt_nh;
805 	int err;
806 	int i;
807 
808 	if (!nh)
809 		return -ENOMEM;
810 
811 	nh->nh_labels = cfg->rc_output_labels;
812 	for (i = 0; i < nh->nh_labels; i++)
813 		nh->nh_label[i] = cfg->rc_output_label[i];
814 
815 	nh->nh_via_table = cfg->rc_via_table;
816 	memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
817 	nh->nh_via_alen = cfg->rc_via_alen;
818 
819 	err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
820 	if (err)
821 		goto errout;
822 
823 	if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
824 		rt->rt_nhn_alive--;
825 
826 	return 0;
827 
828 errout:
829 	return err;
830 }
831 
832 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
833 			 struct mpls_nh *nh, int oif, struct nlattr *via,
834 			 struct nlattr *newdst, u8 max_labels,
835 			 struct netlink_ext_ack *extack)
836 {
837 	int err = -ENOMEM;
838 
839 	if (!nh)
840 		goto errout;
841 
842 	if (newdst) {
843 		err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
844 				     nh->nh_label, extack);
845 		if (err)
846 			goto errout;
847 	}
848 
849 	if (via) {
850 		err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
851 				  __mpls_nh_via(rt, nh), extack);
852 		if (err)
853 			goto errout;
854 	} else {
855 		nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
856 	}
857 
858 	err = mpls_nh_assign_dev(net, rt, nh, oif);
859 	if (err)
860 		goto errout;
861 
862 	return 0;
863 
864 errout:
865 	return err;
866 }
867 
868 static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
869 			      u8 cfg_via_alen, u8 *max_via_alen,
870 			      u8 *max_labels)
871 {
872 	int remaining = len;
873 	u8 nhs = 0;
874 
875 	*max_via_alen = 0;
876 	*max_labels = 0;
877 
878 	while (rtnh_ok(rtnh, remaining)) {
879 		struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
880 		int attrlen;
881 		u8 n_labels = 0;
882 
883 		attrlen = rtnh_attrlen(rtnh);
884 		nla = nla_find(attrs, attrlen, RTA_VIA);
885 		if (nla && nla_len(nla) >=
886 		    offsetof(struct rtvia, rtvia_addr)) {
887 			int via_alen = nla_len(nla) -
888 				offsetof(struct rtvia, rtvia_addr);
889 
890 			if (via_alen <= MAX_VIA_ALEN)
891 				*max_via_alen = max_t(u16, *max_via_alen,
892 						      via_alen);
893 		}
894 
895 		nla = nla_find(attrs, attrlen, RTA_NEWDST);
896 		if (nla &&
897 		    nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
898 				   NULL, NULL) != 0)
899 			return 0;
900 
901 		*max_labels = max_t(u8, *max_labels, n_labels);
902 
903 		/* number of nexthops is tracked by a u8.
904 		 * Check for overflow.
905 		 */
906 		if (nhs == 255)
907 			return 0;
908 		nhs++;
909 
910 		rtnh = rtnh_next(rtnh, &remaining);
911 	}
912 
913 	/* leftover implies invalid nexthop configuration, discard it */
914 	return remaining > 0 ? 0 : nhs;
915 }
916 
917 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
918 			       struct mpls_route *rt, u8 max_labels,
919 			       struct netlink_ext_ack *extack)
920 {
921 	struct rtnexthop *rtnh = cfg->rc_mp;
922 	struct nlattr *nla_via, *nla_newdst;
923 	int remaining = cfg->rc_mp_len;
924 	int err = 0;
925 	u8 nhs = 0;
926 
927 	change_nexthops(rt) {
928 		int attrlen;
929 
930 		nla_via = NULL;
931 		nla_newdst = NULL;
932 
933 		err = -EINVAL;
934 		if (!rtnh_ok(rtnh, remaining))
935 			goto errout;
936 
937 		/* neither weighted multipath nor any flags
938 		 * are supported
939 		 */
940 		if (rtnh->rtnh_hops || rtnh->rtnh_flags)
941 			goto errout;
942 
943 		attrlen = rtnh_attrlen(rtnh);
944 		if (attrlen > 0) {
945 			struct nlattr *attrs = rtnh_attrs(rtnh);
946 
947 			nla_via = nla_find(attrs, attrlen, RTA_VIA);
948 			nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
949 		}
950 
951 		err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
952 				    rtnh->rtnh_ifindex, nla_via, nla_newdst,
953 				    max_labels, extack);
954 		if (err)
955 			goto errout;
956 
957 		if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
958 			rt->rt_nhn_alive--;
959 
960 		rtnh = rtnh_next(rtnh, &remaining);
961 		nhs++;
962 	} endfor_nexthops(rt);
963 
964 	rt->rt_nhn = nhs;
965 
966 	return 0;
967 
968 errout:
969 	rt->rt_nhn = nhs;
970 	return err;
971 }
972 
973 static bool mpls_label_ok(struct net *net, unsigned int *index,
974 			  struct netlink_ext_ack *extack)
975 {
976 	/* Reserved labels may not be set */
977 	if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
978 		NL_SET_ERR_MSG(extack,
979 			       "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
980 		return false;
981 	}
982 
983 	/* The full 20 bit range may not be supported. */
984 	if (*index >= net->mpls.platform_labels) {
985 		NL_SET_ERR_MSG(extack,
986 			       "Label >= configured maximum in platform_labels");
987 		return false;
988 	}
989 
990 	*index = array_index_nospec(*index, net->mpls.platform_labels);
991 
992 	return true;
993 }
994 
995 static int mpls_route_add(struct mpls_route_config *cfg,
996 			  struct netlink_ext_ack *extack)
997 {
998 	struct net *net = cfg->rc_nlinfo.nl_net;
999 	struct mpls_route *rt, *old;
1000 	int err = -EINVAL;
1001 	u8 max_via_alen;
1002 	unsigned index;
1003 	u8 max_labels;
1004 	u8 nhs;
1005 
1006 	index = cfg->rc_label;
1007 
1008 	/* If a label was not specified during insert pick one */
1009 	if ((index == LABEL_NOT_SPECIFIED) &&
1010 	    (cfg->rc_nlflags & NLM_F_CREATE)) {
1011 		index = find_free_label(net);
1012 	}
1013 
1014 	if (!mpls_label_ok(net, &index, extack))
1015 		goto errout;
1016 
1017 	/* Append makes no sense with mpls */
1018 	err = -EOPNOTSUPP;
1019 	if (cfg->rc_nlflags & NLM_F_APPEND) {
1020 		NL_SET_ERR_MSG(extack, "MPLS does not support route append");
1021 		goto errout;
1022 	}
1023 
1024 	err = -EEXIST;
1025 	old = mpls_route_input(net, index);
1026 	if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
1027 		goto errout;
1028 
1029 	err = -EEXIST;
1030 	if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
1031 		goto errout;
1032 
1033 	err = -ENOENT;
1034 	if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
1035 		goto errout;
1036 
1037 	err = -EINVAL;
1038 	if (cfg->rc_mp) {
1039 		nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
1040 					  cfg->rc_via_alen, &max_via_alen,
1041 					  &max_labels);
1042 	} else {
1043 		max_via_alen = cfg->rc_via_alen;
1044 		max_labels = cfg->rc_output_labels;
1045 		nhs = 1;
1046 	}
1047 
1048 	if (nhs == 0) {
1049 		NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
1050 		goto errout;
1051 	}
1052 
1053 	rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
1054 	if (IS_ERR(rt)) {
1055 		err = PTR_ERR(rt);
1056 		goto errout;
1057 	}
1058 
1059 	rt->rt_protocol = cfg->rc_protocol;
1060 	rt->rt_payload_type = cfg->rc_payload_type;
1061 	rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
1062 
1063 	if (cfg->rc_mp)
1064 		err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
1065 	else
1066 		err = mpls_nh_build_from_cfg(cfg, rt);
1067 	if (err)
1068 		goto freert;
1069 
1070 	mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
1071 
1072 	return 0;
1073 
1074 freert:
1075 	mpls_rt_free(rt);
1076 errout:
1077 	return err;
1078 }
1079 
1080 static int mpls_route_del(struct mpls_route_config *cfg,
1081 			  struct netlink_ext_ack *extack)
1082 {
1083 	struct net *net = cfg->rc_nlinfo.nl_net;
1084 	unsigned index;
1085 	int err = -EINVAL;
1086 
1087 	index = cfg->rc_label;
1088 
1089 	if (!mpls_label_ok(net, &index, extack))
1090 		goto errout;
1091 
1092 	mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
1093 
1094 	err = 0;
1095 errout:
1096 	return err;
1097 }
1098 
1099 static void mpls_get_stats(struct mpls_dev *mdev,
1100 			   struct mpls_link_stats *stats)
1101 {
1102 	struct mpls_pcpu_stats *p;
1103 	int i;
1104 
1105 	memset(stats, 0, sizeof(*stats));
1106 
1107 	for_each_possible_cpu(i) {
1108 		struct mpls_link_stats local;
1109 		unsigned int start;
1110 
1111 		p = per_cpu_ptr(mdev->stats, i);
1112 		do {
1113 			start = u64_stats_fetch_begin(&p->syncp);
1114 			local = p->stats;
1115 		} while (u64_stats_fetch_retry(&p->syncp, start));
1116 
1117 		stats->rx_packets	+= local.rx_packets;
1118 		stats->rx_bytes		+= local.rx_bytes;
1119 		stats->tx_packets	+= local.tx_packets;
1120 		stats->tx_bytes		+= local.tx_bytes;
1121 		stats->rx_errors	+= local.rx_errors;
1122 		stats->tx_errors	+= local.tx_errors;
1123 		stats->rx_dropped	+= local.rx_dropped;
1124 		stats->tx_dropped	+= local.tx_dropped;
1125 		stats->rx_noroute	+= local.rx_noroute;
1126 	}
1127 }
1128 
1129 static int mpls_fill_stats_af(struct sk_buff *skb,
1130 			      const struct net_device *dev)
1131 {
1132 	struct mpls_link_stats *stats;
1133 	struct mpls_dev *mdev;
1134 	struct nlattr *nla;
1135 
1136 	mdev = mpls_dev_rcu(dev);
1137 	if (!mdev)
1138 		return -ENODATA;
1139 
1140 	nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
1141 				sizeof(struct mpls_link_stats),
1142 				MPLS_STATS_UNSPEC);
1143 	if (!nla)
1144 		return -EMSGSIZE;
1145 
1146 	stats = nla_data(nla);
1147 	mpls_get_stats(mdev, stats);
1148 
1149 	return 0;
1150 }
1151 
1152 static size_t mpls_get_stats_af_size(const struct net_device *dev)
1153 {
1154 	struct mpls_dev *mdev;
1155 
1156 	mdev = mpls_dev_rcu(dev);
1157 	if (!mdev)
1158 		return 0;
1159 
1160 	return nla_total_size_64bit(sizeof(struct mpls_link_stats));
1161 }
1162 
1163 static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
1164 				     u32 portid, u32 seq, int event,
1165 				     unsigned int flags, int type)
1166 {
1167 	struct nlmsghdr  *nlh;
1168 	struct netconfmsg *ncm;
1169 	bool all = false;
1170 
1171 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1172 			flags);
1173 	if (!nlh)
1174 		return -EMSGSIZE;
1175 
1176 	if (type == NETCONFA_ALL)
1177 		all = true;
1178 
1179 	ncm = nlmsg_data(nlh);
1180 	ncm->ncm_family = AF_MPLS;
1181 
1182 	if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1183 		goto nla_put_failure;
1184 
1185 	if ((all || type == NETCONFA_INPUT) &&
1186 	    nla_put_s32(skb, NETCONFA_INPUT,
1187 			READ_ONCE(mdev->input_enabled)) < 0)
1188 		goto nla_put_failure;
1189 
1190 	nlmsg_end(skb, nlh);
1191 	return 0;
1192 
1193 nla_put_failure:
1194 	nlmsg_cancel(skb, nlh);
1195 	return -EMSGSIZE;
1196 }
1197 
1198 static int mpls_netconf_msgsize_devconf(int type)
1199 {
1200 	int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1201 			+ nla_total_size(4); /* NETCONFA_IFINDEX */
1202 	bool all = false;
1203 
1204 	if (type == NETCONFA_ALL)
1205 		all = true;
1206 
1207 	if (all || type == NETCONFA_INPUT)
1208 		size += nla_total_size(4);
1209 
1210 	return size;
1211 }
1212 
1213 static void mpls_netconf_notify_devconf(struct net *net, int event,
1214 					int type, struct mpls_dev *mdev)
1215 {
1216 	struct sk_buff *skb;
1217 	int err = -ENOBUFS;
1218 
1219 	skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1220 	if (!skb)
1221 		goto errout;
1222 
1223 	err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
1224 	if (err < 0) {
1225 		/* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1226 		WARN_ON(err == -EMSGSIZE);
1227 		kfree_skb(skb);
1228 		goto errout;
1229 	}
1230 
1231 	rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1232 	return;
1233 errout:
1234 	rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1235 }
1236 
1237 static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1238 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
1239 };
1240 
1241 static int mpls_netconf_valid_get_req(struct sk_buff *skb,
1242 				      const struct nlmsghdr *nlh,
1243 				      struct nlattr **tb,
1244 				      struct netlink_ext_ack *extack)
1245 {
1246 	int i, err;
1247 
1248 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
1249 		NL_SET_ERR_MSG_MOD(extack,
1250 				   "Invalid header for netconf get request");
1251 		return -EINVAL;
1252 	}
1253 
1254 	if (!netlink_strict_get_check(skb))
1255 		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
1256 					      tb, NETCONFA_MAX,
1257 					      devconf_mpls_policy, extack);
1258 
1259 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
1260 					    tb, NETCONFA_MAX,
1261 					    devconf_mpls_policy, extack);
1262 	if (err)
1263 		return err;
1264 
1265 	for (i = 0; i <= NETCONFA_MAX; i++) {
1266 		if (!tb[i])
1267 			continue;
1268 
1269 		switch (i) {
1270 		case NETCONFA_IFINDEX:
1271 			break;
1272 		default:
1273 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
1274 			return -EINVAL;
1275 		}
1276 	}
1277 
1278 	return 0;
1279 }
1280 
1281 static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1282 				    struct nlmsghdr *nlh,
1283 				    struct netlink_ext_ack *extack)
1284 {
1285 	struct net *net = sock_net(in_skb->sk);
1286 	struct nlattr *tb[NETCONFA_MAX + 1];
1287 	struct net_device *dev;
1288 	struct mpls_dev *mdev;
1289 	struct sk_buff *skb;
1290 	int ifindex;
1291 	int err;
1292 
1293 	err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
1294 	if (err < 0)
1295 		goto errout;
1296 
1297 	if (!tb[NETCONFA_IFINDEX]) {
1298 		err = -EINVAL;
1299 		goto errout;
1300 	}
1301 
1302 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1303 
1304 	skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1305 	if (!skb) {
1306 		err = -ENOBUFS;
1307 		goto errout;
1308 	}
1309 
1310 	rcu_read_lock();
1311 
1312 	dev = dev_get_by_index_rcu(net, ifindex);
1313 	if (!dev) {
1314 		err = -EINVAL;
1315 		goto errout_unlock;
1316 	}
1317 
1318 	mdev = mpls_dev_rcu(dev);
1319 	if (!mdev) {
1320 		err = -EINVAL;
1321 		goto errout_unlock;
1322 	}
1323 
1324 	err = mpls_netconf_fill_devconf(skb, mdev,
1325 					NETLINK_CB(in_skb).portid,
1326 					nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1327 					NETCONFA_ALL);
1328 	if (err < 0) {
1329 		/* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1330 		WARN_ON(err == -EMSGSIZE);
1331 		goto errout_unlock;
1332 	}
1333 
1334 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1335 
1336 	rcu_read_unlock();
1337 errout:
1338 	return err;
1339 
1340 errout_unlock:
1341 	rcu_read_unlock();
1342 	kfree_skb(skb);
1343 	goto errout;
1344 }
1345 
1346 static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1347 				     struct netlink_callback *cb)
1348 {
1349 	const struct nlmsghdr *nlh = cb->nlh;
1350 	struct net *net = sock_net(skb->sk);
1351 	struct {
1352 		unsigned long ifindex;
1353 	} *ctx = (void *)cb->ctx;
1354 	struct net_device *dev;
1355 	struct mpls_dev *mdev;
1356 	int err = 0;
1357 
1358 	if (cb->strict_check) {
1359 		struct netlink_ext_ack *extack = cb->extack;
1360 		struct netconfmsg *ncm;
1361 
1362 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
1363 			NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
1364 			return -EINVAL;
1365 		}
1366 
1367 		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
1368 			NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
1369 			return -EINVAL;
1370 		}
1371 	}
1372 
1373 	rcu_read_lock();
1374 	for_each_netdev_dump(net, dev, ctx->ifindex) {
1375 		mdev = mpls_dev_rcu(dev);
1376 		if (!mdev)
1377 			continue;
1378 		err = mpls_netconf_fill_devconf(skb, mdev,
1379 						NETLINK_CB(cb->skb).portid,
1380 						nlh->nlmsg_seq,
1381 						RTM_NEWNETCONF,
1382 						NLM_F_MULTI,
1383 						NETCONFA_ALL);
1384 		if (err < 0)
1385 			break;
1386 	}
1387 	rcu_read_unlock();
1388 
1389 	return err;
1390 }
1391 
1392 #define MPLS_PERDEV_SYSCTL_OFFSET(field)	\
1393 	(&((struct mpls_dev *)0)->field)
1394 
1395 static int mpls_conf_proc(const struct ctl_table *ctl, int write,
1396 			  void *buffer, size_t *lenp, loff_t *ppos)
1397 {
1398 	int oval = *(int *)ctl->data;
1399 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1400 
1401 	if (write) {
1402 		struct mpls_dev *mdev = ctl->extra1;
1403 		int i = (int *)ctl->data - (int *)mdev;
1404 		struct net *net = ctl->extra2;
1405 		int val = *(int *)ctl->data;
1406 
1407 		if (i == offsetof(struct mpls_dev, input_enabled) &&
1408 		    val != oval) {
1409 			mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
1410 						    NETCONFA_INPUT, mdev);
1411 		}
1412 	}
1413 
1414 	return ret;
1415 }
1416 
1417 static const struct ctl_table mpls_dev_table[] = {
1418 	{
1419 		.procname	= "input",
1420 		.maxlen		= sizeof(int),
1421 		.mode		= 0644,
1422 		.proc_handler	= mpls_conf_proc,
1423 		.data		= MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1424 	},
1425 };
1426 
1427 static int mpls_dev_sysctl_register(struct net_device *dev,
1428 				    struct mpls_dev *mdev)
1429 {
1430 	char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
1431 	size_t table_size = ARRAY_SIZE(mpls_dev_table);
1432 	struct net *net = dev_net(dev);
1433 	struct ctl_table *table;
1434 	int i;
1435 
1436 	table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1437 	if (!table)
1438 		goto out;
1439 
1440 	/* Table data contains only offsets relative to the base of
1441 	 * the mdev at this point, so make them absolute.
1442 	 */
1443 	for (i = 0; i < table_size; i++) {
1444 		table[i].data = (char *)mdev + (uintptr_t)table[i].data;
1445 		table[i].extra1 = mdev;
1446 		table[i].extra2 = net;
1447 	}
1448 
1449 	snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1450 
1451 	mdev->sysctl = register_net_sysctl_sz(net, path, table, table_size);
1452 	if (!mdev->sysctl)
1453 		goto free;
1454 
1455 	mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
1456 	return 0;
1457 
1458 free:
1459 	kfree(table);
1460 out:
1461 	mdev->sysctl = NULL;
1462 	return -ENOBUFS;
1463 }
1464 
1465 static void mpls_dev_sysctl_unregister(struct net_device *dev,
1466 				       struct mpls_dev *mdev)
1467 {
1468 	struct net *net = dev_net(dev);
1469 	const struct ctl_table *table;
1470 
1471 	if (!mdev->sysctl)
1472 		return;
1473 
1474 	table = mdev->sysctl->ctl_table_arg;
1475 	unregister_net_sysctl_table(mdev->sysctl);
1476 	kfree(table);
1477 
1478 	mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
1479 }
1480 
1481 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1482 {
1483 	struct mpls_dev *mdev;
1484 	int err = -ENOMEM;
1485 	int i;
1486 
1487 	mdev = kzalloc_obj(*mdev);
1488 	if (!mdev)
1489 		return ERR_PTR(err);
1490 
1491 	mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1492 	if (!mdev->stats)
1493 		goto free;
1494 
1495 	for_each_possible_cpu(i) {
1496 		struct mpls_pcpu_stats *mpls_stats;
1497 
1498 		mpls_stats = per_cpu_ptr(mdev->stats, i);
1499 		u64_stats_init(&mpls_stats->syncp);
1500 	}
1501 
1502 	mdev->dev = dev;
1503 
1504 	err = mpls_dev_sysctl_register(dev, mdev);
1505 	if (err)
1506 		goto free;
1507 
1508 	rcu_assign_pointer(dev->mpls_ptr, mdev);
1509 
1510 	return mdev;
1511 
1512 free:
1513 	free_percpu(mdev->stats);
1514 	kfree(mdev);
1515 	return ERR_PTR(err);
1516 }
1517 
1518 static void mpls_dev_destroy_rcu(struct rcu_head *head)
1519 {
1520 	struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1521 
1522 	free_percpu(mdev->stats);
1523 	kfree(mdev);
1524 }
1525 
1526 static int mpls_ifdown(struct net_device *dev, int event)
1527 {
1528 	struct net *net = dev_net(dev);
1529 	unsigned int index;
1530 
1531 	for (index = 0; index < net->mpls.platform_labels; index++) {
1532 		struct mpls_route *rt;
1533 		bool nh_del = false;
1534 		u8 alive = 0;
1535 
1536 		rt = mpls_route_input(net, index);
1537 		if (!rt)
1538 			continue;
1539 
1540 		if (event == NETDEV_UNREGISTER) {
1541 			u8 deleted = 0;
1542 
1543 			for_nexthops(rt) {
1544 				if (!nh->nh_dev || nh->nh_dev == dev)
1545 					deleted++;
1546 				if (nh->nh_dev == dev)
1547 					nh_del = true;
1548 			} endfor_nexthops(rt);
1549 
1550 			/* if there are no more nexthops, delete the route */
1551 			if (deleted == rt->rt_nhn) {
1552 				mpls_route_update(net, index, NULL, NULL);
1553 				continue;
1554 			}
1555 
1556 			if (nh_del) {
1557 				size_t size = sizeof(*rt) + rt->rt_nhn *
1558 					rt->rt_nh_size;
1559 				struct mpls_route *orig = rt;
1560 
1561 				rt = kmemdup(orig, size, GFP_KERNEL);
1562 				if (!rt)
1563 					return -ENOMEM;
1564 			}
1565 		}
1566 
1567 		change_nexthops(rt) {
1568 			unsigned int nh_flags = nh->nh_flags;
1569 
1570 			if (nh->nh_dev != dev) {
1571 				if (nh_del)
1572 					netdev_hold(nh->nh_dev, &nh->nh_dev_tracker,
1573 						    GFP_KERNEL);
1574 				goto next;
1575 			}
1576 
1577 			switch (event) {
1578 			case NETDEV_DOWN:
1579 			case NETDEV_UNREGISTER:
1580 				nh_flags |= RTNH_F_DEAD;
1581 				fallthrough;
1582 			case NETDEV_CHANGE:
1583 				nh_flags |= RTNH_F_LINKDOWN;
1584 				break;
1585 			}
1586 			if (event == NETDEV_UNREGISTER)
1587 				nh->nh_dev = NULL;
1588 
1589 			if (nh->nh_flags != nh_flags)
1590 				WRITE_ONCE(nh->nh_flags, nh_flags);
1591 next:
1592 			if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
1593 				alive++;
1594 		} endfor_nexthops(rt);
1595 
1596 		WRITE_ONCE(rt->rt_nhn_alive, alive);
1597 
1598 		if (nh_del)
1599 			mpls_route_update(net, index, rt, NULL);
1600 	}
1601 
1602 	return 0;
1603 }
1604 
1605 static void mpls_ifup(struct net_device *dev, unsigned int flags)
1606 {
1607 	struct net *net = dev_net(dev);
1608 	unsigned int index;
1609 	u8 alive;
1610 
1611 	for (index = 0; index < net->mpls.platform_labels; index++) {
1612 		struct mpls_route *rt;
1613 
1614 		rt = mpls_route_input(net, index);
1615 		if (!rt)
1616 			continue;
1617 
1618 		alive = 0;
1619 		change_nexthops(rt) {
1620 			unsigned int nh_flags = nh->nh_flags;
1621 
1622 			if (!(nh_flags & flags)) {
1623 				alive++;
1624 				continue;
1625 			}
1626 			if (nh->nh_dev != dev)
1627 				continue;
1628 			alive++;
1629 			nh_flags &= ~flags;
1630 			WRITE_ONCE(nh->nh_flags, nh_flags);
1631 		} endfor_nexthops(rt);
1632 
1633 		WRITE_ONCE(rt->rt_nhn_alive, alive);
1634 	}
1635 }
1636 
1637 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1638 			   void *ptr)
1639 {
1640 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1641 	struct net *net = dev_net(dev);
1642 	struct mpls_dev *mdev;
1643 	unsigned int flags;
1644 	int err;
1645 
1646 	mutex_lock(&net->mpls.platform_mutex);
1647 
1648 	if (event == NETDEV_REGISTER) {
1649 		mdev = mpls_add_dev(dev);
1650 		if (IS_ERR(mdev)) {
1651 			err = PTR_ERR(mdev);
1652 			goto err;
1653 		}
1654 
1655 		goto out;
1656 	}
1657 
1658 	mdev = mpls_dev_get(net, dev);
1659 	if (!mdev)
1660 		goto out;
1661 
1662 	switch (event) {
1663 
1664 	case NETDEV_DOWN:
1665 		err = mpls_ifdown(dev, event);
1666 		if (err)
1667 			goto err;
1668 		break;
1669 	case NETDEV_UP:
1670 		flags = netif_get_flags(dev);
1671 		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1672 			mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1673 		else
1674 			mpls_ifup(dev, RTNH_F_DEAD);
1675 		break;
1676 	case NETDEV_CHANGE:
1677 		flags = netif_get_flags(dev);
1678 		if (flags & (IFF_RUNNING | IFF_LOWER_UP)) {
1679 			mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1680 		} else {
1681 			err = mpls_ifdown(dev, event);
1682 			if (err)
1683 				goto err;
1684 		}
1685 		break;
1686 	case NETDEV_UNREGISTER:
1687 		err = mpls_ifdown(dev, event);
1688 		if (err)
1689 			goto err;
1690 
1691 		mdev = mpls_dev_get(net, dev);
1692 		if (mdev) {
1693 			mpls_dev_sysctl_unregister(dev, mdev);
1694 			RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1695 			call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
1696 		}
1697 		break;
1698 	case NETDEV_CHANGENAME:
1699 		mdev = mpls_dev_get(net, dev);
1700 		if (mdev) {
1701 			mpls_dev_sysctl_unregister(dev, mdev);
1702 			err = mpls_dev_sysctl_register(dev, mdev);
1703 			if (err)
1704 				goto err;
1705 		}
1706 		break;
1707 	}
1708 
1709 out:
1710 	mutex_unlock(&net->mpls.platform_mutex);
1711 	return NOTIFY_OK;
1712 
1713 err:
1714 	mutex_unlock(&net->mpls.platform_mutex);
1715 	return notifier_from_errno(err);
1716 }
1717 
1718 static struct notifier_block mpls_dev_notifier = {
1719 	.notifier_call = mpls_dev_notify,
1720 };
1721 
1722 static int nla_put_via(struct sk_buff *skb,
1723 		       u8 table, const void *addr, int alen)
1724 {
1725 	static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1726 		AF_INET, AF_INET6, AF_PACKET,
1727 	};
1728 	struct nlattr *nla;
1729 	struct rtvia *via;
1730 	int family = AF_UNSPEC;
1731 
1732 	nla = nla_reserve(skb, RTA_VIA, alen + 2);
1733 	if (!nla)
1734 		return -EMSGSIZE;
1735 
1736 	if (table <= NEIGH_NR_TABLES)
1737 		family = table_to_family[table];
1738 
1739 	via = nla_data(nla);
1740 	via->rtvia_family = family;
1741 	memcpy(via->rtvia_addr, addr, alen);
1742 	return 0;
1743 }
1744 
1745 int nla_put_labels(struct sk_buff *skb, int attrtype,
1746 		   u8 labels, const u32 label[])
1747 {
1748 	struct nlattr *nla;
1749 	struct mpls_shim_hdr *nla_label;
1750 	bool bos;
1751 	int i;
1752 	nla = nla_reserve(skb, attrtype, labels*4);
1753 	if (!nla)
1754 		return -EMSGSIZE;
1755 
1756 	nla_label = nla_data(nla);
1757 	bos = true;
1758 	for (i = labels - 1; i >= 0; i--) {
1759 		nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1760 		bos = false;
1761 	}
1762 
1763 	return 0;
1764 }
1765 EXPORT_SYMBOL_GPL(nla_put_labels);
1766 
1767 int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
1768 		   u32 label[], struct netlink_ext_ack *extack)
1769 {
1770 	unsigned len = nla_len(nla);
1771 	struct mpls_shim_hdr *nla_label;
1772 	u8 nla_labels;
1773 	bool bos;
1774 	int i;
1775 
1776 	/* len needs to be an even multiple of 4 (the label size). Number
1777 	 * of labels is a u8 so check for overflow.
1778 	 */
1779 	if (len & 3 || len / 4 > 255) {
1780 		NL_SET_ERR_MSG_ATTR(extack, nla,
1781 				    "Invalid length for labels attribute");
1782 		return -EINVAL;
1783 	}
1784 
1785 	/* Limit the number of new labels allowed */
1786 	nla_labels = len/4;
1787 	if (nla_labels > max_labels) {
1788 		NL_SET_ERR_MSG(extack, "Too many labels");
1789 		return -EINVAL;
1790 	}
1791 
1792 	/* when label == NULL, caller wants number of labels */
1793 	if (!label)
1794 		goto out;
1795 
1796 	nla_label = nla_data(nla);
1797 	bos = true;
1798 	for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1799 		struct mpls_entry_decoded dec;
1800 		dec = mpls_entry_decode(nla_label + i);
1801 
1802 		/* Ensure the bottom of stack flag is properly set
1803 		 * and ttl and tc are both clear.
1804 		 */
1805 		if (dec.ttl) {
1806 			NL_SET_ERR_MSG_ATTR(extack, nla,
1807 					    "TTL in label must be 0");
1808 			return -EINVAL;
1809 		}
1810 
1811 		if (dec.tc) {
1812 			NL_SET_ERR_MSG_ATTR(extack, nla,
1813 					    "Traffic class in label must be 0");
1814 			return -EINVAL;
1815 		}
1816 
1817 		if (dec.bos != bos) {
1818 			NL_SET_BAD_ATTR(extack, nla);
1819 			if (bos) {
1820 				NL_SET_ERR_MSG(extack,
1821 					       "BOS bit must be set in first label");
1822 			} else {
1823 				NL_SET_ERR_MSG(extack,
1824 					       "BOS bit can only be set in first label");
1825 			}
1826 			return -EINVAL;
1827 		}
1828 
1829 		switch (dec.label) {
1830 		case MPLS_LABEL_IMPLNULL:
1831 			/* RFC3032: This is a label that an LSR may
1832 			 * assign and distribute, but which never
1833 			 * actually appears in the encapsulation.
1834 			 */
1835 			NL_SET_ERR_MSG_ATTR(extack, nla,
1836 					    "Implicit NULL Label (3) can not be used in encapsulation");
1837 			return -EINVAL;
1838 		}
1839 
1840 		label[i] = dec.label;
1841 	}
1842 out:
1843 	*labels = nla_labels;
1844 	return 0;
1845 }
1846 EXPORT_SYMBOL_GPL(nla_get_labels);
1847 
1848 static int rtm_to_route_config(struct sk_buff *skb,
1849 			       struct nlmsghdr *nlh,
1850 			       struct mpls_route_config *cfg,
1851 			       struct netlink_ext_ack *extack)
1852 {
1853 	struct rtmsg *rtm;
1854 	struct nlattr *tb[RTA_MAX+1];
1855 	int index;
1856 	int err;
1857 
1858 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
1859 				     rtm_mpls_policy, extack);
1860 	if (err < 0)
1861 		goto errout;
1862 
1863 	err = -EINVAL;
1864 	rtm = nlmsg_data(nlh);
1865 
1866 	if (rtm->rtm_family != AF_MPLS) {
1867 		NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
1868 		goto errout;
1869 	}
1870 	if (rtm->rtm_dst_len != 20) {
1871 		NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
1872 		goto errout;
1873 	}
1874 	if (rtm->rtm_src_len != 0) {
1875 		NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
1876 		goto errout;
1877 	}
1878 	if (rtm->rtm_tos != 0) {
1879 		NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
1880 		goto errout;
1881 	}
1882 	if (rtm->rtm_table != RT_TABLE_MAIN) {
1883 		NL_SET_ERR_MSG(extack,
1884 			       "MPLS only supports the main route table");
1885 		goto errout;
1886 	}
1887 	/* Any value is acceptable for rtm_protocol */
1888 
1889 	/* As mpls uses destination specific addresses
1890 	 * (or source specific address in the case of multicast)
1891 	 * all addresses have universal scope.
1892 	 */
1893 	if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
1894 		NL_SET_ERR_MSG(extack,
1895 			       "Invalid route scope  - MPLS only supports UNIVERSE");
1896 		goto errout;
1897 	}
1898 	if (rtm->rtm_type != RTN_UNICAST) {
1899 		NL_SET_ERR_MSG(extack,
1900 			       "Invalid route type - MPLS only supports UNICAST");
1901 		goto errout;
1902 	}
1903 	if (rtm->rtm_flags != 0) {
1904 		NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
1905 		goto errout;
1906 	}
1907 
1908 	cfg->rc_label		= LABEL_NOT_SPECIFIED;
1909 	cfg->rc_protocol	= rtm->rtm_protocol;
1910 	cfg->rc_via_table	= MPLS_NEIGH_TABLE_UNSPEC;
1911 	cfg->rc_ttl_propagate	= MPLS_TTL_PROP_DEFAULT;
1912 	cfg->rc_nlflags		= nlh->nlmsg_flags;
1913 	cfg->rc_nlinfo.portid	= NETLINK_CB(skb).portid;
1914 	cfg->rc_nlinfo.nlh	= nlh;
1915 	cfg->rc_nlinfo.nl_net	= sock_net(skb->sk);
1916 
1917 	for (index = 0; index <= RTA_MAX; index++) {
1918 		struct nlattr *nla = tb[index];
1919 		if (!nla)
1920 			continue;
1921 
1922 		switch (index) {
1923 		case RTA_OIF:
1924 			cfg->rc_ifindex = nla_get_u32(nla);
1925 			break;
1926 		case RTA_NEWDST:
1927 			if (nla_get_labels(nla, MAX_NEW_LABELS,
1928 					   &cfg->rc_output_labels,
1929 					   cfg->rc_output_label, extack))
1930 				goto errout;
1931 			break;
1932 		case RTA_DST:
1933 		{
1934 			u8 label_count;
1935 			if (nla_get_labels(nla, 1, &label_count,
1936 					   &cfg->rc_label, extack))
1937 				goto errout;
1938 
1939 			if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1940 					   &cfg->rc_label, extack))
1941 				goto errout;
1942 			break;
1943 		}
1944 		case RTA_GATEWAY:
1945 			NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
1946 			goto errout;
1947 		case RTA_VIA:
1948 		{
1949 			if (nla_get_via(nla, &cfg->rc_via_alen,
1950 					&cfg->rc_via_table, cfg->rc_via,
1951 					extack))
1952 				goto errout;
1953 			break;
1954 		}
1955 		case RTA_MULTIPATH:
1956 		{
1957 			cfg->rc_mp = nla_data(nla);
1958 			cfg->rc_mp_len = nla_len(nla);
1959 			break;
1960 		}
1961 		case RTA_TTL_PROPAGATE:
1962 		{
1963 			u8 ttl_propagate = nla_get_u8(nla);
1964 
1965 			if (ttl_propagate > 1) {
1966 				NL_SET_ERR_MSG_ATTR(extack, nla,
1967 						    "RTA_TTL_PROPAGATE can only be 0 or 1");
1968 				goto errout;
1969 			}
1970 			cfg->rc_ttl_propagate = ttl_propagate ?
1971 				MPLS_TTL_PROP_ENABLED :
1972 				MPLS_TTL_PROP_DISABLED;
1973 			break;
1974 		}
1975 		default:
1976 			NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
1977 			/* Unsupported attribute */
1978 			goto errout;
1979 		}
1980 	}
1981 
1982 	err = 0;
1983 errout:
1984 	return err;
1985 }
1986 
1987 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1988 			     struct netlink_ext_ack *extack)
1989 {
1990 	struct net *net = sock_net(skb->sk);
1991 	struct mpls_route_config *cfg;
1992 	int err;
1993 
1994 	cfg = kzalloc_obj(*cfg);
1995 	if (!cfg)
1996 		return -ENOMEM;
1997 
1998 	err = rtm_to_route_config(skb, nlh, cfg, extack);
1999 	if (err < 0)
2000 		goto out;
2001 
2002 	mutex_lock(&net->mpls.platform_mutex);
2003 	err = mpls_route_del(cfg, extack);
2004 	mutex_unlock(&net->mpls.platform_mutex);
2005 out:
2006 	kfree(cfg);
2007 
2008 	return err;
2009 }
2010 
2011 
2012 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
2013 			     struct netlink_ext_ack *extack)
2014 {
2015 	struct net *net = sock_net(skb->sk);
2016 	struct mpls_route_config *cfg;
2017 	int err;
2018 
2019 	cfg = kzalloc_obj(*cfg);
2020 	if (!cfg)
2021 		return -ENOMEM;
2022 
2023 	err = rtm_to_route_config(skb, nlh, cfg, extack);
2024 	if (err < 0)
2025 		goto out;
2026 
2027 	mutex_lock(&net->mpls.platform_mutex);
2028 	err = mpls_route_add(cfg, extack);
2029 	mutex_unlock(&net->mpls.platform_mutex);
2030 out:
2031 	kfree(cfg);
2032 
2033 	return err;
2034 }
2035 
2036 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
2037 			   u32 label, struct mpls_route *rt, int flags)
2038 {
2039 	struct net_device *dev;
2040 	struct nlmsghdr *nlh;
2041 	struct rtmsg *rtm;
2042 
2043 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
2044 	if (nlh == NULL)
2045 		return -EMSGSIZE;
2046 
2047 	rtm = nlmsg_data(nlh);
2048 	rtm->rtm_family = AF_MPLS;
2049 	rtm->rtm_dst_len = 20;
2050 	rtm->rtm_src_len = 0;
2051 	rtm->rtm_tos = 0;
2052 	rtm->rtm_table = RT_TABLE_MAIN;
2053 	rtm->rtm_protocol = rt->rt_protocol;
2054 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2055 	rtm->rtm_type = RTN_UNICAST;
2056 	rtm->rtm_flags = 0;
2057 
2058 	if (nla_put_labels(skb, RTA_DST, 1, &label))
2059 		goto nla_put_failure;
2060 
2061 	if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
2062 		bool ttl_propagate =
2063 			rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
2064 
2065 		if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
2066 			       ttl_propagate))
2067 			goto nla_put_failure;
2068 	}
2069 	if (rt->rt_nhn == 1) {
2070 		const struct mpls_nh *nh = rt->rt_nh;
2071 
2072 		if (nh->nh_labels &&
2073 		    nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
2074 				   nh->nh_label))
2075 			goto nla_put_failure;
2076 		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2077 		    nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
2078 				nh->nh_via_alen))
2079 			goto nla_put_failure;
2080 		dev = nh->nh_dev;
2081 		if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
2082 			goto nla_put_failure;
2083 		if (nh->nh_flags & RTNH_F_LINKDOWN)
2084 			rtm->rtm_flags |= RTNH_F_LINKDOWN;
2085 		if (nh->nh_flags & RTNH_F_DEAD)
2086 			rtm->rtm_flags |= RTNH_F_DEAD;
2087 	} else {
2088 		struct rtnexthop *rtnh;
2089 		struct nlattr *mp;
2090 		u8 linkdown = 0;
2091 		u8 dead = 0;
2092 
2093 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
2094 		if (!mp)
2095 			goto nla_put_failure;
2096 
2097 		for_nexthops(rt) {
2098 			dev = nh->nh_dev;
2099 			if (!dev)
2100 				continue;
2101 
2102 			rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
2103 			if (!rtnh)
2104 				goto nla_put_failure;
2105 
2106 			rtnh->rtnh_ifindex = dev->ifindex;
2107 			if (nh->nh_flags & RTNH_F_LINKDOWN) {
2108 				rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
2109 				linkdown++;
2110 			}
2111 			if (nh->nh_flags & RTNH_F_DEAD) {
2112 				rtnh->rtnh_flags |= RTNH_F_DEAD;
2113 				dead++;
2114 			}
2115 
2116 			if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
2117 							    nh->nh_labels,
2118 							    nh->nh_label))
2119 				goto nla_put_failure;
2120 			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2121 			    nla_put_via(skb, nh->nh_via_table,
2122 					mpls_nh_via(rt, nh),
2123 					nh->nh_via_alen))
2124 				goto nla_put_failure;
2125 
2126 			/* length of rtnetlink header + attributes */
2127 			rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
2128 		} endfor_nexthops(rt);
2129 
2130 		if (linkdown == rt->rt_nhn)
2131 			rtm->rtm_flags |= RTNH_F_LINKDOWN;
2132 		if (dead == rt->rt_nhn)
2133 			rtm->rtm_flags |= RTNH_F_DEAD;
2134 
2135 		nla_nest_end(skb, mp);
2136 	}
2137 
2138 	nlmsg_end(skb, nlh);
2139 	return 0;
2140 
2141 nla_put_failure:
2142 	nlmsg_cancel(skb, nlh);
2143 	return -EMSGSIZE;
2144 }
2145 
2146 #if IS_ENABLED(CONFIG_INET)
2147 static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
2148 				   struct fib_dump_filter *filter,
2149 				   struct netlink_callback *cb)
2150 {
2151 	return ip_valid_fib_dump_req(net, nlh, filter, cb);
2152 }
2153 #else
2154 static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
2155 				   struct fib_dump_filter *filter,
2156 				   struct netlink_callback *cb)
2157 {
2158 	struct netlink_ext_ack *extack = cb->extack;
2159 	struct nlattr *tb[RTA_MAX + 1];
2160 	struct rtmsg *rtm;
2161 	int err, i;
2162 
2163 	rtm = nlmsg_payload(nlh, sizeof(*rtm));
2164 	if (!rtm) {
2165 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
2166 		return -EINVAL;
2167 	}
2168 
2169 	if (rtm->rtm_dst_len || rtm->rtm_src_len  || rtm->rtm_tos   ||
2170 	    rtm->rtm_table   || rtm->rtm_scope    || rtm->rtm_type  ||
2171 	    rtm->rtm_flags) {
2172 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
2173 		return -EINVAL;
2174 	}
2175 
2176 	if (rtm->rtm_protocol) {
2177 		filter->protocol = rtm->rtm_protocol;
2178 		filter->filter_set = 1;
2179 		cb->answer_flags = NLM_F_DUMP_FILTERED;
2180 	}
2181 
2182 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2183 					    rtm_mpls_policy, extack);
2184 	if (err < 0)
2185 		return err;
2186 
2187 	for (i = 0; i <= RTA_MAX; ++i) {
2188 		int ifindex;
2189 
2190 		if (i == RTA_OIF) {
2191 			if (!tb[i])
2192 				continue;
2193 
2194 			ifindex = nla_get_u32(tb[i]);
2195 			filter->dev = dev_get_by_index_rcu(net, ifindex);
2196 			if (!filter->dev)
2197 				return -ENODEV;
2198 			filter->filter_set = 1;
2199 		} else if (tb[i]) {
2200 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
2201 			return -EINVAL;
2202 		}
2203 	}
2204 
2205 	return 0;
2206 }
2207 #endif
2208 
2209 static bool mpls_rt_uses_dev(struct mpls_route *rt,
2210 			     const struct net_device *dev)
2211 {
2212 	if (rt->rt_nhn == 1) {
2213 		struct mpls_nh *nh = rt->rt_nh;
2214 
2215 		if (nh->nh_dev == dev)
2216 			return true;
2217 	} else {
2218 		for_nexthops(rt) {
2219 			if (nh->nh_dev == dev)
2220 				return true;
2221 		} endfor_nexthops(rt);
2222 	}
2223 
2224 	return false;
2225 }
2226 
2227 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
2228 {
2229 	struct mpls_route __rcu **platform_label;
2230 	const struct nlmsghdr *nlh = cb->nlh;
2231 	struct net *net = sock_net(skb->sk);
2232 	struct fib_dump_filter filter = {};
2233 	unsigned int flags = NLM_F_MULTI;
2234 	size_t platform_labels;
2235 	unsigned int index;
2236 	int err;
2237 
2238 	rcu_read_lock();
2239 
2240 	if (cb->strict_check) {
2241 		err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
2242 		if (err < 0)
2243 			goto err;
2244 
2245 		/* for MPLS, there is only 1 table with fixed type and flags.
2246 		 * If either are set in the filter then return nothing.
2247 		 */
2248 		if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
2249 		    (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
2250 		     filter.flags)
2251 			goto unlock;
2252 	}
2253 
2254 	index = cb->args[0];
2255 	if (index < MPLS_LABEL_FIRST_UNRESERVED)
2256 		index = MPLS_LABEL_FIRST_UNRESERVED;
2257 
2258 	platform_label = mpls_platform_label_rcu(net, &platform_labels);
2259 
2260 	if (filter.filter_set)
2261 		flags |= NLM_F_DUMP_FILTERED;
2262 
2263 	for (; index < platform_labels; index++) {
2264 		struct mpls_route *rt;
2265 
2266 		rt = rcu_dereference(platform_label[index]);
2267 		if (!rt)
2268 			continue;
2269 
2270 		if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
2271 		    (filter.protocol && rt->rt_protocol != filter.protocol))
2272 			continue;
2273 
2274 		if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
2275 				    cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2276 				    index, rt, flags) < 0)
2277 			break;
2278 	}
2279 	cb->args[0] = index;
2280 
2281 unlock:
2282 	rcu_read_unlock();
2283 	return skb->len;
2284 
2285 err:
2286 	rcu_read_unlock();
2287 	return err;
2288 }
2289 
2290 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
2291 {
2292 	size_t payload =
2293 		NLMSG_ALIGN(sizeof(struct rtmsg))
2294 		+ nla_total_size(4)			/* RTA_DST */
2295 		+ nla_total_size(1);			/* RTA_TTL_PROPAGATE */
2296 
2297 	if (rt->rt_nhn == 1) {
2298 		struct mpls_nh *nh = rt->rt_nh;
2299 
2300 		if (nh->nh_dev)
2301 			payload += nla_total_size(4); /* RTA_OIF */
2302 		if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
2303 			payload += nla_total_size(2 + nh->nh_via_alen);
2304 		if (nh->nh_labels) /* RTA_NEWDST */
2305 			payload += nla_total_size(nh->nh_labels * 4);
2306 	} else {
2307 		/* each nexthop is packed in an attribute */
2308 		size_t nhsize = 0;
2309 
2310 		for_nexthops(rt) {
2311 			if (!nh->nh_dev)
2312 				continue;
2313 			nhsize += nla_total_size(sizeof(struct rtnexthop));
2314 			/* RTA_VIA */
2315 			if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
2316 				nhsize += nla_total_size(2 + nh->nh_via_alen);
2317 			if (nh->nh_labels)
2318 				nhsize += nla_total_size(nh->nh_labels * 4);
2319 		} endfor_nexthops(rt);
2320 		/* nested attribute */
2321 		payload += nla_total_size(nhsize);
2322 	}
2323 
2324 	return payload;
2325 }
2326 
2327 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
2328 		       struct nlmsghdr *nlh, struct net *net, u32 portid,
2329 		       unsigned int nlm_flags)
2330 {
2331 	struct sk_buff *skb;
2332 	u32 seq = nlh ? nlh->nlmsg_seq : 0;
2333 	int err = -ENOBUFS;
2334 
2335 	skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
2336 	if (skb == NULL)
2337 		goto errout;
2338 
2339 	err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
2340 	if (err < 0) {
2341 		/* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2342 		WARN_ON(err == -EMSGSIZE);
2343 		kfree_skb(skb);
2344 		goto errout;
2345 	}
2346 	rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
2347 
2348 	return;
2349 errout:
2350 	rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
2351 }
2352 
2353 static int mpls_valid_getroute_req(struct sk_buff *skb,
2354 				   const struct nlmsghdr *nlh,
2355 				   struct nlattr **tb,
2356 				   struct netlink_ext_ack *extack)
2357 {
2358 	struct rtmsg *rtm;
2359 	int i, err;
2360 
2361 	rtm = nlmsg_payload(nlh, sizeof(*rtm));
2362 	if (!rtm) {
2363 		NL_SET_ERR_MSG_MOD(extack,
2364 				   "Invalid header for get route request");
2365 		return -EINVAL;
2366 	}
2367 
2368 	if (!netlink_strict_get_check(skb))
2369 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2370 					      rtm_mpls_policy, extack);
2371 
2372 	if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
2373 	    rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
2374 	    rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
2375 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
2376 		return -EINVAL;
2377 	}
2378 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
2379 		NL_SET_ERR_MSG_MOD(extack,
2380 				   "Invalid flags for get route request");
2381 		return -EINVAL;
2382 	}
2383 
2384 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2385 					    rtm_mpls_policy, extack);
2386 	if (err)
2387 		return err;
2388 
2389 	if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
2390 		NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
2391 		return -EINVAL;
2392 	}
2393 
2394 	for (i = 0; i <= RTA_MAX; i++) {
2395 		if (!tb[i])
2396 			continue;
2397 
2398 		switch (i) {
2399 		case RTA_DST:
2400 		case RTA_NEWDST:
2401 			break;
2402 		default:
2403 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
2404 			return -EINVAL;
2405 		}
2406 	}
2407 
2408 	return 0;
2409 }
2410 
2411 static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
2412 			 struct netlink_ext_ack *extack)
2413 {
2414 	struct net *net = sock_net(in_skb->sk);
2415 	u32 portid = NETLINK_CB(in_skb).portid;
2416 	u32 in_label = LABEL_NOT_SPECIFIED;
2417 	struct nlattr *tb[RTA_MAX + 1];
2418 	struct mpls_route *rt = NULL;
2419 	u32 labels[MAX_NEW_LABELS];
2420 	struct mpls_shim_hdr *hdr;
2421 	unsigned int hdr_size = 0;
2422 	const struct mpls_nh *nh;
2423 	struct net_device *dev;
2424 	struct rtmsg *rtm, *r;
2425 	struct nlmsghdr *nlh;
2426 	struct sk_buff *skb;
2427 	u8 n_labels;
2428 	int err;
2429 
2430 	mutex_lock(&net->mpls.platform_mutex);
2431 
2432 	err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
2433 	if (err < 0)
2434 		goto errout;
2435 
2436 	rtm = nlmsg_data(in_nlh);
2437 
2438 	if (tb[RTA_DST]) {
2439 		u8 label_count;
2440 
2441 		if (nla_get_labels(tb[RTA_DST], 1, &label_count,
2442 				   &in_label, extack)) {
2443 			err = -EINVAL;
2444 			goto errout;
2445 		}
2446 
2447 		if (!mpls_label_ok(net, &in_label, extack)) {
2448 			err = -EINVAL;
2449 			goto errout;
2450 		}
2451 	}
2452 
2453 	if (in_label < net->mpls.platform_labels)
2454 		rt = mpls_route_input(net, in_label);
2455 	if (!rt) {
2456 		err = -ENETUNREACH;
2457 		goto errout;
2458 	}
2459 
2460 	if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
2461 		skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
2462 		if (!skb) {
2463 			err = -ENOBUFS;
2464 			goto errout;
2465 		}
2466 
2467 		err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
2468 				      RTM_NEWROUTE, in_label, rt, 0);
2469 		if (err < 0) {
2470 			/* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2471 			WARN_ON(err == -EMSGSIZE);
2472 			goto errout_free;
2473 		}
2474 
2475 		err = rtnl_unicast(skb, net, portid);
2476 		goto errout;
2477 	}
2478 
2479 	if (tb[RTA_NEWDST]) {
2480 		if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
2481 				   labels, extack) != 0) {
2482 			err = -EINVAL;
2483 			goto errout;
2484 		}
2485 
2486 		hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
2487 	}
2488 
2489 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2490 	if (!skb) {
2491 		err = -ENOBUFS;
2492 		goto errout;
2493 	}
2494 
2495 	skb->protocol = htons(ETH_P_MPLS_UC);
2496 
2497 	if (hdr_size) {
2498 		bool bos;
2499 		int i;
2500 
2501 		if (skb_cow(skb, hdr_size)) {
2502 			err = -ENOBUFS;
2503 			goto errout_free;
2504 		}
2505 
2506 		skb_reserve(skb, hdr_size);
2507 		skb_push(skb, hdr_size);
2508 		skb_reset_network_header(skb);
2509 
2510 		/* Push new labels */
2511 		hdr = mpls_hdr(skb);
2512 		bos = true;
2513 		for (i = n_labels - 1; i >= 0; i--) {
2514 			hdr[i] = mpls_entry_encode(labels[i],
2515 						   1, 0, bos);
2516 			bos = false;
2517 		}
2518 	}
2519 
2520 	nh = mpls_select_multipath(rt, skb);
2521 	if (!nh) {
2522 		err = -ENETUNREACH;
2523 		goto errout_free;
2524 	}
2525 
2526 	if (hdr_size) {
2527 		skb_pull(skb, hdr_size);
2528 		skb_reset_network_header(skb);
2529 	}
2530 
2531 	nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
2532 			RTM_NEWROUTE, sizeof(*r), 0);
2533 	if (!nlh) {
2534 		err = -EMSGSIZE;
2535 		goto errout_free;
2536 	}
2537 
2538 	r = nlmsg_data(nlh);
2539 	r->rtm_family	 = AF_MPLS;
2540 	r->rtm_dst_len	= 20;
2541 	r->rtm_src_len	= 0;
2542 	r->rtm_tos	= 0;
2543 	r->rtm_table	= RT_TABLE_MAIN;
2544 	r->rtm_type	= RTN_UNICAST;
2545 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
2546 	r->rtm_protocol = rt->rt_protocol;
2547 	r->rtm_flags	= 0;
2548 
2549 	if (nla_put_labels(skb, RTA_DST, 1, &in_label))
2550 		goto nla_put_failure;
2551 
2552 	if (nh->nh_labels &&
2553 	    nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
2554 			   nh->nh_label))
2555 		goto nla_put_failure;
2556 
2557 	if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2558 	    nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
2559 			nh->nh_via_alen))
2560 		goto nla_put_failure;
2561 	dev = nh->nh_dev;
2562 	if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
2563 		goto nla_put_failure;
2564 
2565 	nlmsg_end(skb, nlh);
2566 
2567 	err = rtnl_unicast(skb, net, portid);
2568 errout:
2569 	mutex_unlock(&net->mpls.platform_mutex);
2570 	return err;
2571 
2572 nla_put_failure:
2573 	nlmsg_cancel(skb, nlh);
2574 	err = -EMSGSIZE;
2575 errout_free:
2576 	mutex_unlock(&net->mpls.platform_mutex);
2577 	kfree_skb(skb);
2578 	return err;
2579 }
2580 
2581 static int resize_platform_label_table(struct net *net, size_t limit)
2582 {
2583 	size_t size = sizeof(struct mpls_route *) * limit;
2584 	size_t old_limit;
2585 	size_t cp_size;
2586 	struct mpls_route __rcu **labels = NULL, **old;
2587 	struct mpls_route *rt0 = NULL, *rt2 = NULL;
2588 	unsigned index;
2589 
2590 	if (size) {
2591 		labels = kvzalloc(size, GFP_KERNEL);
2592 		if (!labels)
2593 			goto nolabels;
2594 	}
2595 
2596 	/* In case the predefined labels need to be populated */
2597 	if (limit > MPLS_LABEL_IPV4NULL) {
2598 		struct net_device *lo = net->loopback_dev;
2599 
2600 		rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
2601 		if (IS_ERR(rt0))
2602 			goto nort0;
2603 
2604 		rt0->rt_nh->nh_dev = lo;
2605 		netdev_hold(lo, &rt0->rt_nh->nh_dev_tracker, GFP_KERNEL);
2606 		rt0->rt_protocol = RTPROT_KERNEL;
2607 		rt0->rt_payload_type = MPT_IPV4;
2608 		rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
2609 		rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2610 		rt0->rt_nh->nh_via_alen = lo->addr_len;
2611 		memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
2612 		       lo->addr_len);
2613 	}
2614 	if (limit > MPLS_LABEL_IPV6NULL) {
2615 		struct net_device *lo = net->loopback_dev;
2616 
2617 		rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
2618 		if (IS_ERR(rt2))
2619 			goto nort2;
2620 
2621 		rt2->rt_nh->nh_dev = lo;
2622 		netdev_hold(lo, &rt2->rt_nh->nh_dev_tracker, GFP_KERNEL);
2623 		rt2->rt_protocol = RTPROT_KERNEL;
2624 		rt2->rt_payload_type = MPT_IPV6;
2625 		rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
2626 		rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2627 		rt2->rt_nh->nh_via_alen = lo->addr_len;
2628 		memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
2629 		       lo->addr_len);
2630 	}
2631 
2632 	mutex_lock(&net->mpls.platform_mutex);
2633 
2634 	/* Remember the original table */
2635 	old = mpls_dereference(net, net->mpls.platform_label);
2636 	old_limit = net->mpls.platform_labels;
2637 
2638 	/* Free any labels beyond the new table */
2639 	for (index = limit; index < old_limit; index++)
2640 		mpls_route_update(net, index, NULL, NULL);
2641 
2642 	/* Copy over the old labels */
2643 	cp_size = size;
2644 	if (old_limit < limit)
2645 		cp_size = old_limit * sizeof(struct mpls_route *);
2646 
2647 	memcpy(labels, old, cp_size);
2648 
2649 	/* If needed set the predefined labels */
2650 	if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
2651 	    (limit > MPLS_LABEL_IPV6NULL)) {
2652 		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
2653 		rt2 = NULL;
2654 	}
2655 
2656 	if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
2657 	    (limit > MPLS_LABEL_IPV4NULL)) {
2658 		RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
2659 		rt0 = NULL;
2660 	}
2661 
2662 	/* Update the global pointers */
2663 	local_bh_disable();
2664 	write_seqcount_begin(&net->mpls.platform_label_seq);
2665 	net->mpls.platform_labels = limit;
2666 	rcu_assign_pointer(net->mpls.platform_label, labels);
2667 	write_seqcount_end(&net->mpls.platform_label_seq);
2668 	local_bh_enable();
2669 
2670 	mutex_unlock(&net->mpls.platform_mutex);
2671 
2672 	mpls_rt_free(rt2);
2673 	mpls_rt_free(rt0);
2674 
2675 	if (old) {
2676 		synchronize_rcu();
2677 		kvfree(old);
2678 	}
2679 	return 0;
2680 
2681 nort2:
2682 	mpls_rt_free(rt0);
2683 nort0:
2684 	kvfree(labels);
2685 nolabels:
2686 	return -ENOMEM;
2687 }
2688 
2689 static int mpls_platform_labels(const struct ctl_table *table, int write,
2690 				void *buffer, size_t *lenp, loff_t *ppos)
2691 {
2692 	struct net *net = table->data;
2693 	int platform_labels = net->mpls.platform_labels;
2694 	int ret;
2695 	struct ctl_table tmp = {
2696 		.procname	= table->procname,
2697 		.data		= &platform_labels,
2698 		.maxlen		= sizeof(int),
2699 		.mode		= table->mode,
2700 		.extra1		= SYSCTL_ZERO,
2701 		.extra2		= &label_limit,
2702 	};
2703 
2704 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2705 
2706 	if (write && ret == 0)
2707 		ret = resize_platform_label_table(net, platform_labels);
2708 
2709 	return ret;
2710 }
2711 
2712 #define MPLS_NS_SYSCTL_OFFSET(field)		\
2713 	(&((struct net *)0)->field)
2714 
2715 static const struct ctl_table mpls_table[] = {
2716 	{
2717 		.procname	= "platform_labels",
2718 		.data		= NULL,
2719 		.maxlen		= sizeof(int),
2720 		.mode		= 0644,
2721 		.proc_handler	= mpls_platform_labels,
2722 	},
2723 	{
2724 		.procname	= "ip_ttl_propagate",
2725 		.data		= MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2726 		.maxlen		= sizeof(int),
2727 		.mode		= 0644,
2728 		.proc_handler	= proc_dointvec_minmax,
2729 		.extra1		= SYSCTL_ZERO,
2730 		.extra2		= SYSCTL_ONE,
2731 	},
2732 	{
2733 		.procname	= "default_ttl",
2734 		.data		= MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2735 		.maxlen		= sizeof(int),
2736 		.mode		= 0644,
2737 		.proc_handler	= proc_dointvec_minmax,
2738 		.extra1		= SYSCTL_ONE,
2739 		.extra2		= &ttl_max,
2740 	},
2741 };
2742 
2743 static __net_init int mpls_net_init(struct net *net)
2744 {
2745 	size_t table_size = ARRAY_SIZE(mpls_table);
2746 	struct ctl_table *table;
2747 	int i;
2748 
2749 	mutex_init(&net->mpls.platform_mutex);
2750 	seqcount_mutex_init(&net->mpls.platform_label_seq, &net->mpls.platform_mutex);
2751 
2752 	net->mpls.platform_labels = 0;
2753 	net->mpls.platform_label = NULL;
2754 	net->mpls.ip_ttl_propagate = 1;
2755 	net->mpls.default_ttl = 255;
2756 
2757 	table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2758 	if (table == NULL)
2759 		return -ENOMEM;
2760 
2761 	/* Table data contains only offsets relative to the base of
2762 	 * the mdev at this point, so make them absolute.
2763 	 */
2764 	for (i = 0; i < table_size; i++)
2765 		table[i].data = (char *)net + (uintptr_t)table[i].data;
2766 
2767 	net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
2768 					       table_size);
2769 	if (net->mpls.ctl == NULL) {
2770 		kfree(table);
2771 		return -ENOMEM;
2772 	}
2773 
2774 	return 0;
2775 }
2776 
2777 static __net_exit void mpls_net_exit(struct net *net)
2778 {
2779 	struct mpls_route __rcu **platform_label;
2780 	size_t platform_labels;
2781 	const struct ctl_table *table;
2782 	unsigned int index;
2783 
2784 	table = net->mpls.ctl->ctl_table_arg;
2785 	unregister_net_sysctl_table(net->mpls.ctl);
2786 	kfree(table);
2787 
2788 	/* An rcu grace period has passed since there was a device in
2789 	 * the network namespace (and thus the last in flight packet)
2790 	 * left this network namespace.  This is because
2791 	 * unregister_netdevice_many and netdev_run_todo has completed
2792 	 * for each network device that was in this network namespace.
2793 	 *
2794 	 * As such no additional rcu synchronization is necessary when
2795 	 * freeing the platform_label table.
2796 	 */
2797 	mutex_lock(&net->mpls.platform_mutex);
2798 
2799 	platform_label = mpls_dereference(net, net->mpls.platform_label);
2800 	platform_labels = net->mpls.platform_labels;
2801 
2802 	for (index = 0; index < platform_labels; index++) {
2803 		struct mpls_route *rt;
2804 
2805 		rt = mpls_dereference(net, platform_label[index]);
2806 		mpls_notify_route(net, index, rt, NULL, NULL);
2807 		mpls_rt_free(rt);
2808 	}
2809 
2810 	mutex_unlock(&net->mpls.platform_mutex);
2811 
2812 	kvfree(platform_label);
2813 }
2814 
2815 static struct pernet_operations mpls_net_ops = {
2816 	.init = mpls_net_init,
2817 	.exit = mpls_net_exit,
2818 };
2819 
2820 static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2821 	.family		   = AF_MPLS,
2822 	.fill_stats_af	   = mpls_fill_stats_af,
2823 	.get_stats_af_size = mpls_get_stats_af_size,
2824 };
2825 
2826 static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = {
2827 	{THIS_MODULE, PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL,
2828 	 RTNL_FLAG_DOIT_UNLOCKED},
2829 	{THIS_MODULE, PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL,
2830 	 RTNL_FLAG_DOIT_UNLOCKED},
2831 	{THIS_MODULE, PF_MPLS, RTM_GETROUTE, mpls_getroute, mpls_dump_routes,
2832 	 RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
2833 	{THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
2834 	 mpls_netconf_get_devconf, mpls_netconf_dump_devconf,
2835 	 RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
2836 };
2837 
2838 static int __init mpls_init(void)
2839 {
2840 	int err;
2841 
2842 	BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2843 
2844 	err = register_pernet_subsys(&mpls_net_ops);
2845 	if (err)
2846 		goto out;
2847 
2848 	err = register_netdevice_notifier(&mpls_dev_notifier);
2849 	if (err)
2850 		goto out_unregister_pernet;
2851 
2852 	dev_add_pack(&mpls_packet_type);
2853 
2854 	err = rtnl_af_register(&mpls_af_ops);
2855 	if (err)
2856 		goto out_unregister_dev_type;
2857 
2858 	err = rtnl_register_many(mpls_rtnl_msg_handlers);
2859 	if (err)
2860 		goto out_unregister_rtnl_af;
2861 
2862 	err = ipgre_tunnel_encap_add_mpls_ops();
2863 	if (err) {
2864 		pr_err("Can't add mpls over gre tunnel ops\n");
2865 		goto out_unregister_rtnl;
2866 	}
2867 
2868 	err = 0;
2869 out:
2870 	return err;
2871 
2872 out_unregister_rtnl:
2873 	rtnl_unregister_many(mpls_rtnl_msg_handlers);
2874 out_unregister_rtnl_af:
2875 	rtnl_af_unregister(&mpls_af_ops);
2876 out_unregister_dev_type:
2877 	dev_remove_pack(&mpls_packet_type);
2878 	unregister_netdevice_notifier(&mpls_dev_notifier);
2879 out_unregister_pernet:
2880 	unregister_pernet_subsys(&mpls_net_ops);
2881 	goto out;
2882 }
2883 module_init(mpls_init);
2884 
2885 static void __exit mpls_exit(void)
2886 {
2887 	rtnl_unregister_all(PF_MPLS);
2888 	rtnl_af_unregister(&mpls_af_ops);
2889 	dev_remove_pack(&mpls_packet_type);
2890 	unregister_netdevice_notifier(&mpls_dev_notifier);
2891 	unregister_pernet_subsys(&mpls_net_ops);
2892 	ipgre_tunnel_encap_del_mpls_ops();
2893 }
2894 module_exit(mpls_exit);
2895 
2896 MODULE_DESCRIPTION("MultiProtocol Label Switching");
2897 MODULE_LICENSE("GPL v2");
2898 MODULE_ALIAS_NETPROTO(PF_MPLS);
2899