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