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