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