GNU Linux-libre 4.9.337-gnu1
[releases.git] / net / mpls / af_mpls.c
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/nospec.h>
11 #include <linux/vmalloc.h>
12 #include <net/ip.h>
13 #include <net/dst.h>
14 #include <net/sock.h>
15 #include <net/arp.h>
16 #include <net/ip_fib.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #if IS_ENABLED(CONFIG_IPV6)
20 #include <net/ipv6.h>
21 #include <net/addrconf.h>
22 #endif
23 #include <net/nexthop.h>
24 #include "internal.h"
25
26 /* Maximum number of labels to look ahead at when selecting a path of
27  * a multipath route
28  */
29 #define MAX_MP_SELECT_LABELS 4
30
31 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
32
33 static int zero = 0;
34 static int label_limit = (1 << 20) - 1;
35
36 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
37                        struct nlmsghdr *nlh, struct net *net, u32 portid,
38                        unsigned int nlm_flags);
39
40 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
41 {
42         struct mpls_route *rt = NULL;
43
44         if (index < net->mpls.platform_labels) {
45                 struct mpls_route __rcu **platform_label =
46                         rcu_dereference(net->mpls.platform_label);
47                 rt = rcu_dereference(platform_label[index]);
48         }
49         return rt;
50 }
51
52 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
53 {
54         return rcu_dereference_rtnl(dev->mpls_ptr);
55 }
56
57 bool mpls_output_possible(const struct net_device *dev)
58 {
59         return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
60 }
61 EXPORT_SYMBOL_GPL(mpls_output_possible);
62
63 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
64 {
65         u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
66         int nh_index = nh - rt->rt_nh;
67
68         return nh0_via + rt->rt_max_alen * nh_index;
69 }
70
71 static const u8 *mpls_nh_via(const struct mpls_route *rt,
72                              const struct mpls_nh *nh)
73 {
74         return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
75 }
76
77 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
78 {
79         /* The size of the layer 2.5 labels to be added for this route */
80         return nh->nh_labels * sizeof(struct mpls_shim_hdr);
81 }
82
83 unsigned int mpls_dev_mtu(const struct net_device *dev)
84 {
85         /* The amount of data the layer 2 frame can hold */
86         return dev->mtu;
87 }
88 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
89
90 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
91 {
92         if (skb->len <= mtu)
93                 return false;
94
95         if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
96                 return false;
97
98         return true;
99 }
100 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
101
102 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
103 {
104         struct mpls_entry_decoded dec;
105         unsigned int mpls_hdr_len = 0;
106         struct mpls_shim_hdr *hdr;
107         bool eli_seen = false;
108         int label_index;
109         u32 hash = 0;
110
111         for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
112              label_index++) {
113                 mpls_hdr_len += sizeof(*hdr);
114                 if (!pskb_may_pull(skb, mpls_hdr_len))
115                         break;
116
117                 /* Read and decode the current label */
118                 hdr = mpls_hdr(skb) + label_index;
119                 dec = mpls_entry_decode(hdr);
120
121                 /* RFC6790 - reserved labels MUST NOT be used as keys
122                  * for the load-balancing function
123                  */
124                 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
125                         hash = jhash_1word(dec.label, hash);
126
127                         /* The entropy label follows the entropy label
128                          * indicator, so this means that the entropy
129                          * label was just added to the hash - no need to
130                          * go any deeper either in the label stack or in the
131                          * payload
132                          */
133                         if (eli_seen)
134                                 break;
135                 } else if (dec.label == MPLS_LABEL_ENTROPY) {
136                         eli_seen = true;
137                 }
138
139                 if (!dec.bos)
140                         continue;
141
142                 /* found bottom label; does skb have room for a header? */
143                 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
144                         const struct iphdr *v4hdr;
145
146                         v4hdr = (const struct iphdr *)(hdr + 1);
147                         if (v4hdr->version == 4) {
148                                 hash = jhash_3words(ntohl(v4hdr->saddr),
149                                                     ntohl(v4hdr->daddr),
150                                                     v4hdr->protocol, hash);
151                         } else if (v4hdr->version == 6 &&
152                                    pskb_may_pull(skb, mpls_hdr_len +
153                                                  sizeof(struct ipv6hdr))) {
154                                 const struct ipv6hdr *v6hdr;
155
156                                 v6hdr = (const struct ipv6hdr *)(hdr + 1);
157                                 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
158                                 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
159                                 hash = jhash_1word(v6hdr->nexthdr, hash);
160                         }
161                 }
162
163                 break;
164         }
165
166         return hash;
167 }
168
169 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
170                                              struct sk_buff *skb)
171 {
172         int alive = ACCESS_ONCE(rt->rt_nhn_alive);
173         u32 hash = 0;
174         int nh_index = 0;
175         int n = 0;
176
177         /* No need to look further into packet if there's only
178          * one path
179          */
180         if (rt->rt_nhn == 1)
181                 goto out;
182
183         if (alive <= 0)
184                 return NULL;
185
186         hash = mpls_multipath_hash(rt, skb);
187         nh_index = hash % alive;
188         if (alive == rt->rt_nhn)
189                 goto out;
190         for_nexthops(rt) {
191                 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
192                         continue;
193                 if (n == nh_index)
194                         return nh;
195                 n++;
196         } endfor_nexthops(rt);
197
198 out:
199         return &rt->rt_nh[nh_index];
200 }
201
202 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
203                         struct mpls_entry_decoded dec)
204 {
205         enum mpls_payload_type payload_type;
206         bool success = false;
207
208         /* The IPv4 code below accesses through the IPv4 header
209          * checksum, which is 12 bytes into the packet.
210          * The IPv6 code below accesses through the IPv6 hop limit
211          * which is 8 bytes into the packet.
212          *
213          * For all supported cases there should always be at least 12
214          * bytes of packet data present.  The IPv4 header is 20 bytes
215          * without options and the IPv6 header is always 40 bytes
216          * long.
217          */
218         if (!pskb_may_pull(skb, 12))
219                 return false;
220
221         payload_type = rt->rt_payload_type;
222         if (payload_type == MPT_UNSPEC)
223                 payload_type = ip_hdr(skb)->version;
224
225         switch (payload_type) {
226         case MPT_IPV4: {
227                 struct iphdr *hdr4 = ip_hdr(skb);
228                 skb->protocol = htons(ETH_P_IP);
229                 csum_replace2(&hdr4->check,
230                               htons(hdr4->ttl << 8),
231                               htons(dec.ttl << 8));
232                 hdr4->ttl = dec.ttl;
233                 success = true;
234                 break;
235         }
236         case MPT_IPV6: {
237                 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
238                 skb->protocol = htons(ETH_P_IPV6);
239                 hdr6->hop_limit = dec.ttl;
240                 success = true;
241                 break;
242         }
243         case MPT_UNSPEC:
244                 break;
245         }
246
247         return success;
248 }
249
250 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
251                         struct packet_type *pt, struct net_device *orig_dev)
252 {
253         struct net *net = dev_net(dev);
254         struct mpls_shim_hdr *hdr;
255         struct mpls_route *rt;
256         struct mpls_nh *nh;
257         struct mpls_entry_decoded dec;
258         struct net_device *out_dev;
259         struct mpls_dev *mdev;
260         unsigned int hh_len;
261         unsigned int new_header_size;
262         unsigned int mtu;
263         int err;
264
265         /* Careful this entire function runs inside of an rcu critical section */
266
267         mdev = mpls_dev_get(dev);
268         if (!mdev || !mdev->input_enabled)
269                 goto drop;
270
271         if (skb->pkt_type != PACKET_HOST)
272                 goto drop;
273
274         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
275                 goto drop;
276
277         if (!pskb_may_pull(skb, sizeof(*hdr)))
278                 goto drop;
279
280         /* Read and decode the label */
281         hdr = mpls_hdr(skb);
282         dec = mpls_entry_decode(hdr);
283
284         rt = mpls_route_input_rcu(net, dec.label);
285         if (!rt)
286                 goto drop;
287
288         nh = mpls_select_multipath(rt, skb);
289         if (!nh)
290                 goto drop;
291
292         /* Find the output device */
293         out_dev = rcu_dereference(nh->nh_dev);
294         if (!mpls_output_possible(out_dev))
295                 goto drop;
296
297         /* Pop the label */
298         skb_pull(skb, sizeof(*hdr));
299         skb_reset_network_header(skb);
300
301         skb_orphan(skb);
302
303         if (skb_warn_if_lro(skb))
304                 goto drop;
305
306         skb_forward_csum(skb);
307
308         /* Verify ttl is valid */
309         if (dec.ttl <= 1)
310                 goto drop;
311         dec.ttl -= 1;
312
313         /* Verify the destination can hold the packet */
314         new_header_size = mpls_nh_header_size(nh);
315         mtu = mpls_dev_mtu(out_dev);
316         if (mpls_pkt_too_big(skb, mtu - new_header_size))
317                 goto drop;
318
319         hh_len = LL_RESERVED_SPACE(out_dev);
320         if (!out_dev->header_ops)
321                 hh_len = 0;
322
323         /* Ensure there is enough space for the headers in the skb */
324         if (skb_cow(skb, hh_len + new_header_size))
325                 goto drop;
326
327         skb->dev = out_dev;
328         skb->protocol = htons(ETH_P_MPLS_UC);
329
330         if (unlikely(!new_header_size && dec.bos)) {
331                 /* Penultimate hop popping */
332                 if (!mpls_egress(rt, skb, dec))
333                         goto drop;
334         } else {
335                 bool bos;
336                 int i;
337                 skb_push(skb, new_header_size);
338                 skb_reset_network_header(skb);
339                 /* Push the new labels */
340                 hdr = mpls_hdr(skb);
341                 bos = dec.bos;
342                 for (i = nh->nh_labels - 1; i >= 0; i--) {
343                         hdr[i] = mpls_entry_encode(nh->nh_label[i],
344                                                    dec.ttl, 0, bos);
345                         bos = false;
346                 }
347         }
348
349         /* If via wasn't specified then send out using device address */
350         if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
351                 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
352                                  out_dev->dev_addr, skb);
353         else
354                 err = neigh_xmit(nh->nh_via_table, out_dev,
355                                  mpls_nh_via(rt, nh), skb);
356         if (err)
357                 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
358                                     __func__, err);
359         return 0;
360
361 drop:
362         kfree_skb(skb);
363         return NET_RX_DROP;
364 }
365
366 static struct packet_type mpls_packet_type __read_mostly = {
367         .type = cpu_to_be16(ETH_P_MPLS_UC),
368         .func = mpls_forward,
369 };
370
371 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
372         [RTA_DST]               = { .type = NLA_U32 },
373         [RTA_OIF]               = { .type = NLA_U32 },
374 };
375
376 struct mpls_route_config {
377         u32                     rc_protocol;
378         u32                     rc_ifindex;
379         u8                      rc_via_table;
380         u8                      rc_via_alen;
381         u8                      rc_via[MAX_VIA_ALEN];
382         u32                     rc_label;
383         u8                      rc_output_labels;
384         u32                     rc_output_label[MAX_NEW_LABELS];
385         u32                     rc_nlflags;
386         enum mpls_payload_type  rc_payload_type;
387         struct nl_info          rc_nlinfo;
388         struct rtnexthop        *rc_mp;
389         int                     rc_mp_len;
390 };
391
392 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
393 {
394         u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
395         struct mpls_route *rt;
396
397         rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
398                            VIA_ALEN_ALIGN) +
399                      num_nh * max_alen_aligned,
400                      GFP_KERNEL);
401         if (rt) {
402                 rt->rt_nhn = num_nh;
403                 rt->rt_nhn_alive = num_nh;
404                 rt->rt_max_alen = max_alen_aligned;
405         }
406
407         return rt;
408 }
409
410 static void mpls_rt_free(struct mpls_route *rt)
411 {
412         if (rt)
413                 kfree_rcu(rt, rt_rcu);
414 }
415
416 static void mpls_notify_route(struct net *net, unsigned index,
417                               struct mpls_route *old, struct mpls_route *new,
418                               const struct nl_info *info)
419 {
420         struct nlmsghdr *nlh = info ? info->nlh : NULL;
421         unsigned portid = info ? info->portid : 0;
422         int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
423         struct mpls_route *rt = new ? new : old;
424         unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
425         /* Ignore reserved labels for now */
426         if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
427                 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
428 }
429
430 static void mpls_route_update(struct net *net, unsigned index,
431                               struct mpls_route *new,
432                               const struct nl_info *info)
433 {
434         struct mpls_route __rcu **platform_label;
435         struct mpls_route *rt;
436
437         ASSERT_RTNL();
438
439         platform_label = rtnl_dereference(net->mpls.platform_label);
440         rt = rtnl_dereference(platform_label[index]);
441         rcu_assign_pointer(platform_label[index], new);
442
443         mpls_notify_route(net, index, rt, new, info);
444
445         /* If we removed a route free it now */
446         mpls_rt_free(rt);
447 }
448
449 static unsigned find_free_label(struct net *net)
450 {
451         struct mpls_route __rcu **platform_label;
452         size_t platform_labels;
453         unsigned index;
454
455         platform_label = rtnl_dereference(net->mpls.platform_label);
456         platform_labels = net->mpls.platform_labels;
457         for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
458              index++) {
459                 if (!rtnl_dereference(platform_label[index]))
460                         return index;
461         }
462         return LABEL_NOT_SPECIFIED;
463 }
464
465 #if IS_ENABLED(CONFIG_INET)
466 static struct net_device *inet_fib_lookup_dev(struct net *net,
467                                               const void *addr)
468 {
469         struct net_device *dev;
470         struct rtable *rt;
471         struct in_addr daddr;
472
473         memcpy(&daddr, addr, sizeof(struct in_addr));
474         rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
475         if (IS_ERR(rt))
476                 return ERR_CAST(rt);
477
478         dev = rt->dst.dev;
479         dev_hold(dev);
480
481         ip_rt_put(rt);
482
483         return dev;
484 }
485 #else
486 static struct net_device *inet_fib_lookup_dev(struct net *net,
487                                               const void *addr)
488 {
489         return ERR_PTR(-EAFNOSUPPORT);
490 }
491 #endif
492
493 #if IS_ENABLED(CONFIG_IPV6)
494 static struct net_device *inet6_fib_lookup_dev(struct net *net,
495                                                const void *addr)
496 {
497         struct net_device *dev;
498         struct dst_entry *dst;
499         struct flowi6 fl6;
500
501         if (!ipv6_stub)
502                 return ERR_PTR(-EAFNOSUPPORT);
503
504         memset(&fl6, 0, sizeof(fl6));
505         memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
506         dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
507         if (IS_ERR(dst))
508                 return ERR_CAST(dst);
509
510         dev = dst->dev;
511         dev_hold(dev);
512         dst_release(dst);
513
514         return dev;
515 }
516 #else
517 static struct net_device *inet6_fib_lookup_dev(struct net *net,
518                                                const void *addr)
519 {
520         return ERR_PTR(-EAFNOSUPPORT);
521 }
522 #endif
523
524 static struct net_device *find_outdev(struct net *net,
525                                       struct mpls_route *rt,
526                                       struct mpls_nh *nh, int oif)
527 {
528         struct net_device *dev = NULL;
529
530         if (!oif) {
531                 switch (nh->nh_via_table) {
532                 case NEIGH_ARP_TABLE:
533                         dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
534                         break;
535                 case NEIGH_ND_TABLE:
536                         dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
537                         break;
538                 case NEIGH_LINK_TABLE:
539                         break;
540                 }
541         } else {
542                 dev = dev_get_by_index(net, oif);
543         }
544
545         if (!dev)
546                 return ERR_PTR(-ENODEV);
547
548         if (IS_ERR(dev))
549                 return dev;
550
551         /* The caller is holding rtnl anyways, so release the dev reference */
552         dev_put(dev);
553
554         return dev;
555 }
556
557 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
558                               struct mpls_nh *nh, int oif)
559 {
560         struct net_device *dev = NULL;
561         int err = -ENODEV;
562
563         dev = find_outdev(net, rt, nh, oif);
564         if (IS_ERR(dev)) {
565                 err = PTR_ERR(dev);
566                 dev = NULL;
567                 goto errout;
568         }
569
570         /* Ensure this is a supported device */
571         err = -EINVAL;
572         if (!mpls_dev_get(dev))
573                 goto errout;
574
575         if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
576             (dev->addr_len != nh->nh_via_alen))
577                 goto errout;
578
579         RCU_INIT_POINTER(nh->nh_dev, dev);
580
581         if (!(dev->flags & IFF_UP)) {
582                 nh->nh_flags |= RTNH_F_DEAD;
583         } else {
584                 unsigned int flags;
585
586                 flags = dev_get_flags(dev);
587                 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
588                         nh->nh_flags |= RTNH_F_LINKDOWN;
589         }
590
591         return 0;
592
593 errout:
594         return err;
595 }
596
597 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
598                                   struct mpls_route *rt)
599 {
600         struct net *net = cfg->rc_nlinfo.nl_net;
601         struct mpls_nh *nh = rt->rt_nh;
602         int err;
603         int i;
604
605         if (!nh)
606                 return -ENOMEM;
607
608         err = -EINVAL;
609         /* Ensure only a supported number of labels are present */
610         if (cfg->rc_output_labels > MAX_NEW_LABELS)
611                 goto errout;
612
613         nh->nh_labels = cfg->rc_output_labels;
614         for (i = 0; i < nh->nh_labels; i++)
615                 nh->nh_label[i] = cfg->rc_output_label[i];
616
617         nh->nh_via_table = cfg->rc_via_table;
618         memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
619         nh->nh_via_alen = cfg->rc_via_alen;
620
621         err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
622         if (err)
623                 goto errout;
624
625         if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
626                 rt->rt_nhn_alive--;
627
628         return 0;
629
630 errout:
631         return err;
632 }
633
634 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
635                          struct mpls_nh *nh, int oif, struct nlattr *via,
636                          struct nlattr *newdst)
637 {
638         int err = -ENOMEM;
639
640         if (!nh)
641                 goto errout;
642
643         if (newdst) {
644                 err = nla_get_labels(newdst, MAX_NEW_LABELS,
645                                      &nh->nh_labels, nh->nh_label);
646                 if (err)
647                         goto errout;
648         }
649
650         if (via) {
651                 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
652                                   __mpls_nh_via(rt, nh));
653                 if (err)
654                         goto errout;
655         } else {
656                 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
657         }
658
659         err = mpls_nh_assign_dev(net, rt, nh, oif);
660         if (err)
661                 goto errout;
662
663         return 0;
664
665 errout:
666         return err;
667 }
668
669 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
670                                u8 cfg_via_alen, u8 *max_via_alen)
671 {
672         int nhs = 0;
673         int remaining = len;
674
675         if (!rtnh) {
676                 *max_via_alen = cfg_via_alen;
677                 return 1;
678         }
679
680         *max_via_alen = 0;
681
682         while (rtnh_ok(rtnh, remaining)) {
683                 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
684                 int attrlen;
685
686                 attrlen = rtnh_attrlen(rtnh);
687                 nla = nla_find(attrs, attrlen, RTA_VIA);
688                 if (nla && nla_len(nla) >=
689                     offsetof(struct rtvia, rtvia_addr)) {
690                         int via_alen = nla_len(nla) -
691                                 offsetof(struct rtvia, rtvia_addr);
692
693                         if (via_alen <= MAX_VIA_ALEN)
694                                 *max_via_alen = max_t(u16, *max_via_alen,
695                                                       via_alen);
696                 }
697
698                 nhs++;
699                 rtnh = rtnh_next(rtnh, &remaining);
700         }
701
702         /* leftover implies invalid nexthop configuration, discard it */
703         return remaining > 0 ? 0 : nhs;
704 }
705
706 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
707                                struct mpls_route *rt)
708 {
709         struct rtnexthop *rtnh = cfg->rc_mp;
710         struct nlattr *nla_via, *nla_newdst;
711         int remaining = cfg->rc_mp_len;
712         int nhs = 0;
713         int err = 0;
714
715         change_nexthops(rt) {
716                 int attrlen;
717
718                 nla_via = NULL;
719                 nla_newdst = NULL;
720
721                 err = -EINVAL;
722                 if (!rtnh_ok(rtnh, remaining))
723                         goto errout;
724
725                 /* neither weighted multipath nor any flags
726                  * are supported
727                  */
728                 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
729                         goto errout;
730
731                 attrlen = rtnh_attrlen(rtnh);
732                 if (attrlen > 0) {
733                         struct nlattr *attrs = rtnh_attrs(rtnh);
734
735                         nla_via = nla_find(attrs, attrlen, RTA_VIA);
736                         nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
737                 }
738
739                 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
740                                     rtnh->rtnh_ifindex, nla_via, nla_newdst);
741                 if (err)
742                         goto errout;
743
744                 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
745                         rt->rt_nhn_alive--;
746
747                 rtnh = rtnh_next(rtnh, &remaining);
748                 nhs++;
749         } endfor_nexthops(rt);
750
751         rt->rt_nhn = nhs;
752
753         return 0;
754
755 errout:
756         return err;
757 }
758
759 static bool mpls_label_ok(struct net *net, unsigned int *index)
760 {
761         bool is_ok = true;
762
763         /* Reserved labels may not be set */
764         if (*index < MPLS_LABEL_FIRST_UNRESERVED)
765                 is_ok = false;
766
767         /* The full 20 bit range may not be supported. */
768         if (is_ok && *index >= net->mpls.platform_labels)
769                 is_ok = false;
770
771         *index = array_index_nospec(*index, net->mpls.platform_labels);
772         return is_ok;
773 }
774
775 static int mpls_route_add(struct mpls_route_config *cfg)
776 {
777         struct mpls_route __rcu **platform_label;
778         struct net *net = cfg->rc_nlinfo.nl_net;
779         struct mpls_route *rt, *old;
780         int err = -EINVAL;
781         u8 max_via_alen;
782         unsigned index;
783         int nhs;
784
785         index = cfg->rc_label;
786
787         /* If a label was not specified during insert pick one */
788         if ((index == LABEL_NOT_SPECIFIED) &&
789             (cfg->rc_nlflags & NLM_F_CREATE)) {
790                 index = find_free_label(net);
791         }
792
793         if (!mpls_label_ok(net, &index))
794                 goto errout;
795
796         /* Append makes no sense with mpls */
797         err = -EOPNOTSUPP;
798         if (cfg->rc_nlflags & NLM_F_APPEND)
799                 goto errout;
800
801         err = -EEXIST;
802         platform_label = rtnl_dereference(net->mpls.platform_label);
803         old = rtnl_dereference(platform_label[index]);
804         if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
805                 goto errout;
806
807         err = -EEXIST;
808         if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
809                 goto errout;
810
811         err = -ENOENT;
812         if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
813                 goto errout;
814
815         err = -EINVAL;
816         nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
817                                   cfg->rc_via_alen, &max_via_alen);
818         if (nhs == 0)
819                 goto errout;
820
821         err = -ENOMEM;
822         rt = mpls_rt_alloc(nhs, max_via_alen);
823         if (!rt)
824                 goto errout;
825
826         rt->rt_protocol = cfg->rc_protocol;
827         rt->rt_payload_type = cfg->rc_payload_type;
828
829         if (cfg->rc_mp)
830                 err = mpls_nh_build_multi(cfg, rt);
831         else
832                 err = mpls_nh_build_from_cfg(cfg, rt);
833         if (err)
834                 goto freert;
835
836         mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
837
838         return 0;
839
840 freert:
841         mpls_rt_free(rt);
842 errout:
843         return err;
844 }
845
846 static int mpls_route_del(struct mpls_route_config *cfg)
847 {
848         struct net *net = cfg->rc_nlinfo.nl_net;
849         unsigned index;
850         int err = -EINVAL;
851
852         index = cfg->rc_label;
853
854         if (!mpls_label_ok(net, &index))
855                 goto errout;
856
857         mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
858
859         err = 0;
860 errout:
861         return err;
862 }
863
864 #define MPLS_PERDEV_SYSCTL_OFFSET(field)        \
865         (&((struct mpls_dev *)0)->field)
866
867 static const struct ctl_table mpls_dev_table[] = {
868         {
869                 .procname       = "input",
870                 .maxlen         = sizeof(int),
871                 .mode           = 0644,
872                 .proc_handler   = proc_dointvec,
873                 .data           = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
874         },
875         { }
876 };
877
878 static int mpls_dev_sysctl_register(struct net_device *dev,
879                                     struct mpls_dev *mdev)
880 {
881         char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
882         struct ctl_table *table;
883         int i;
884
885         table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
886         if (!table)
887                 goto out;
888
889         /* Table data contains only offsets relative to the base of
890          * the mdev at this point, so make them absolute.
891          */
892         for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
893                 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
894
895         snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
896
897         mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
898         if (!mdev->sysctl)
899                 goto free;
900
901         return 0;
902
903 free:
904         kfree(table);
905 out:
906         return -ENOBUFS;
907 }
908
909 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
910 {
911         struct ctl_table *table;
912
913         table = mdev->sysctl->ctl_table_arg;
914         unregister_net_sysctl_table(mdev->sysctl);
915         kfree(table);
916 }
917
918 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
919 {
920         struct mpls_dev *mdev;
921         int err = -ENOMEM;
922
923         ASSERT_RTNL();
924
925         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
926         if (!mdev)
927                 return ERR_PTR(err);
928
929         err = mpls_dev_sysctl_register(dev, mdev);
930         if (err)
931                 goto free;
932
933         rcu_assign_pointer(dev->mpls_ptr, mdev);
934
935         return mdev;
936
937 free:
938         kfree(mdev);
939         return ERR_PTR(err);
940 }
941
942 static void mpls_ifdown(struct net_device *dev, int event)
943 {
944         struct mpls_route __rcu **platform_label;
945         struct net *net = dev_net(dev);
946         unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
947         unsigned int alive;
948         unsigned index;
949
950         platform_label = rtnl_dereference(net->mpls.platform_label);
951         for (index = 0; index < net->mpls.platform_labels; index++) {
952                 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
953
954                 if (!rt)
955                         continue;
956
957                 alive = 0;
958                 change_nexthops(rt) {
959                         if (rtnl_dereference(nh->nh_dev) != dev)
960                                 goto next;
961
962                         switch (event) {
963                         case NETDEV_DOWN:
964                         case NETDEV_UNREGISTER:
965                                 nh->nh_flags |= RTNH_F_DEAD;
966                                 /* fall through */
967                         case NETDEV_CHANGE:
968                                 nh->nh_flags |= RTNH_F_LINKDOWN;
969                                 break;
970                         }
971                         if (event == NETDEV_UNREGISTER)
972                                 RCU_INIT_POINTER(nh->nh_dev, NULL);
973 next:
974                         if (!(nh->nh_flags & nh_flags))
975                                 alive++;
976                 } endfor_nexthops(rt);
977
978                 WRITE_ONCE(rt->rt_nhn_alive, alive);
979         }
980 }
981
982 static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
983 {
984         struct mpls_route __rcu **platform_label;
985         struct net *net = dev_net(dev);
986         unsigned index;
987         int alive;
988
989         platform_label = rtnl_dereference(net->mpls.platform_label);
990         for (index = 0; index < net->mpls.platform_labels; index++) {
991                 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
992
993                 if (!rt)
994                         continue;
995
996                 alive = 0;
997                 change_nexthops(rt) {
998                         struct net_device *nh_dev =
999                                 rtnl_dereference(nh->nh_dev);
1000
1001                         if (!(nh->nh_flags & nh_flags)) {
1002                                 alive++;
1003                                 continue;
1004                         }
1005                         if (nh_dev != dev)
1006                                 continue;
1007                         alive++;
1008                         nh->nh_flags &= ~nh_flags;
1009                 } endfor_nexthops(rt);
1010
1011                 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1012         }
1013 }
1014
1015 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1016                            void *ptr)
1017 {
1018         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1019         struct mpls_dev *mdev;
1020         unsigned int flags;
1021
1022         if (event == NETDEV_REGISTER) {
1023                 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
1024                 if (dev->type == ARPHRD_ETHER ||
1025                     dev->type == ARPHRD_LOOPBACK ||
1026                     dev->type == ARPHRD_IPGRE ||
1027                     dev->type == ARPHRD_SIT ||
1028                     dev->type == ARPHRD_TUNNEL) {
1029                         mdev = mpls_add_dev(dev);
1030                         if (IS_ERR(mdev))
1031                                 return notifier_from_errno(PTR_ERR(mdev));
1032                 }
1033                 return NOTIFY_OK;
1034         }
1035
1036         mdev = mpls_dev_get(dev);
1037         if (!mdev)
1038                 return NOTIFY_OK;
1039
1040         switch (event) {
1041         case NETDEV_DOWN:
1042                 mpls_ifdown(dev, event);
1043                 break;
1044         case NETDEV_UP:
1045                 flags = dev_get_flags(dev);
1046                 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1047                         mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1048                 else
1049                         mpls_ifup(dev, RTNH_F_DEAD);
1050                 break;
1051         case NETDEV_CHANGE:
1052                 flags = dev_get_flags(dev);
1053                 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1054                         mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1055                 else
1056                         mpls_ifdown(dev, event);
1057                 break;
1058         case NETDEV_UNREGISTER:
1059                 mpls_ifdown(dev, event);
1060                 mdev = mpls_dev_get(dev);
1061                 if (mdev) {
1062                         mpls_dev_sysctl_unregister(mdev);
1063                         RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1064                         kfree_rcu(mdev, rcu);
1065                 }
1066                 break;
1067         case NETDEV_CHANGENAME:
1068                 mdev = mpls_dev_get(dev);
1069                 if (mdev) {
1070                         int err;
1071
1072                         mpls_dev_sysctl_unregister(mdev);
1073                         err = mpls_dev_sysctl_register(dev, mdev);
1074                         if (err)
1075                                 return notifier_from_errno(err);
1076                 }
1077                 break;
1078         }
1079         return NOTIFY_OK;
1080 }
1081
1082 static struct notifier_block mpls_dev_notifier = {
1083         .notifier_call = mpls_dev_notify,
1084 };
1085
1086 static int nla_put_via(struct sk_buff *skb,
1087                        u8 table, const void *addr, int alen)
1088 {
1089         static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1090                 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1091         };
1092         struct nlattr *nla;
1093         struct rtvia *via;
1094         int family = AF_UNSPEC;
1095
1096         nla = nla_reserve(skb, RTA_VIA, alen + 2);
1097         if (!nla)
1098                 return -EMSGSIZE;
1099
1100         if (table <= NEIGH_NR_TABLES)
1101                 family = table_to_family[table];
1102
1103         via = nla_data(nla);
1104         via->rtvia_family = family;
1105         memcpy(via->rtvia_addr, addr, alen);
1106         return 0;
1107 }
1108
1109 int nla_put_labels(struct sk_buff *skb, int attrtype,
1110                    u8 labels, const u32 label[])
1111 {
1112         struct nlattr *nla;
1113         struct mpls_shim_hdr *nla_label;
1114         bool bos;
1115         int i;
1116         nla = nla_reserve(skb, attrtype, labels*4);
1117         if (!nla)
1118                 return -EMSGSIZE;
1119
1120         nla_label = nla_data(nla);
1121         bos = true;
1122         for (i = labels - 1; i >= 0; i--) {
1123                 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1124                 bos = false;
1125         }
1126
1127         return 0;
1128 }
1129 EXPORT_SYMBOL_GPL(nla_put_labels);
1130
1131 int nla_get_labels(const struct nlattr *nla,
1132                    u32 max_labels, u8 *labels, u32 label[])
1133 {
1134         unsigned len = nla_len(nla);
1135         unsigned nla_labels;
1136         struct mpls_shim_hdr *nla_label;
1137         bool bos;
1138         int i;
1139
1140         /* len needs to be an even multiple of 4 (the label size) */
1141         if (len & 3)
1142                 return -EINVAL;
1143
1144         /* Limit the number of new labels allowed */
1145         nla_labels = len/4;
1146         if (nla_labels > max_labels)
1147                 return -EINVAL;
1148
1149         nla_label = nla_data(nla);
1150         bos = true;
1151         for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1152                 struct mpls_entry_decoded dec;
1153                 dec = mpls_entry_decode(nla_label + i);
1154
1155                 /* Ensure the bottom of stack flag is properly set
1156                  * and ttl and tc are both clear.
1157                  */
1158                 if ((dec.bos != bos) || dec.ttl || dec.tc)
1159                         return -EINVAL;
1160
1161                 switch (dec.label) {
1162                 case MPLS_LABEL_IMPLNULL:
1163                         /* RFC3032: This is a label that an LSR may
1164                          * assign and distribute, but which never
1165                          * actually appears in the encapsulation.
1166                          */
1167                         return -EINVAL;
1168                 }
1169
1170                 label[i] = dec.label;
1171         }
1172         *labels = nla_labels;
1173         return 0;
1174 }
1175 EXPORT_SYMBOL_GPL(nla_get_labels);
1176
1177 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1178                 u8 *via_table, u8 via_addr[])
1179 {
1180         struct rtvia *via = nla_data(nla);
1181         int err = -EINVAL;
1182         int alen;
1183
1184         if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1185                 goto errout;
1186         alen = nla_len(nla) -
1187                         offsetof(struct rtvia, rtvia_addr);
1188         if (alen > MAX_VIA_ALEN)
1189                 goto errout;
1190
1191         /* Validate the address family */
1192         switch (via->rtvia_family) {
1193         case AF_PACKET:
1194                 *via_table = NEIGH_LINK_TABLE;
1195                 break;
1196         case AF_INET:
1197                 *via_table = NEIGH_ARP_TABLE;
1198                 if (alen != 4)
1199                         goto errout;
1200                 break;
1201         case AF_INET6:
1202                 *via_table = NEIGH_ND_TABLE;
1203                 if (alen != 16)
1204                         goto errout;
1205                 break;
1206         default:
1207                 /* Unsupported address family */
1208                 goto errout;
1209         }
1210
1211         memcpy(via_addr, via->rtvia_addr, alen);
1212         *via_alen = alen;
1213         err = 0;
1214
1215 errout:
1216         return err;
1217 }
1218
1219 static int rtm_to_route_config(struct sk_buff *skb,  struct nlmsghdr *nlh,
1220                                struct mpls_route_config *cfg)
1221 {
1222         struct rtmsg *rtm;
1223         struct nlattr *tb[RTA_MAX+1];
1224         int index;
1225         int err;
1226
1227         err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1228         if (err < 0)
1229                 goto errout;
1230
1231         err = -EINVAL;
1232         rtm = nlmsg_data(nlh);
1233         memset(cfg, 0, sizeof(*cfg));
1234
1235         if (rtm->rtm_family != AF_MPLS)
1236                 goto errout;
1237         if (rtm->rtm_dst_len != 20)
1238                 goto errout;
1239         if (rtm->rtm_src_len != 0)
1240                 goto errout;
1241         if (rtm->rtm_tos != 0)
1242                 goto errout;
1243         if (rtm->rtm_table != RT_TABLE_MAIN)
1244                 goto errout;
1245         /* Any value is acceptable for rtm_protocol */
1246
1247         /* As mpls uses destination specific addresses
1248          * (or source specific address in the case of multicast)
1249          * all addresses have universal scope.
1250          */
1251         if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1252                 goto errout;
1253         if (rtm->rtm_type != RTN_UNICAST)
1254                 goto errout;
1255         if (rtm->rtm_flags != 0)
1256                 goto errout;
1257
1258         cfg->rc_label           = LABEL_NOT_SPECIFIED;
1259         cfg->rc_protocol        = rtm->rtm_protocol;
1260         cfg->rc_via_table       = MPLS_NEIGH_TABLE_UNSPEC;
1261         cfg->rc_nlflags         = nlh->nlmsg_flags;
1262         cfg->rc_nlinfo.portid   = NETLINK_CB(skb).portid;
1263         cfg->rc_nlinfo.nlh      = nlh;
1264         cfg->rc_nlinfo.nl_net   = sock_net(skb->sk);
1265
1266         for (index = 0; index <= RTA_MAX; index++) {
1267                 struct nlattr *nla = tb[index];
1268                 if (!nla)
1269                         continue;
1270
1271                 switch (index) {
1272                 case RTA_OIF:
1273                         cfg->rc_ifindex = nla_get_u32(nla);
1274                         break;
1275                 case RTA_NEWDST:
1276                         if (nla_get_labels(nla, MAX_NEW_LABELS,
1277                                            &cfg->rc_output_labels,
1278                                            cfg->rc_output_label))
1279                                 goto errout;
1280                         break;
1281                 case RTA_DST:
1282                 {
1283                         u8 label_count;
1284                         if (nla_get_labels(nla, 1, &label_count,
1285                                            &cfg->rc_label))
1286                                 goto errout;
1287
1288                         if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1289                                            &cfg->rc_label))
1290                                 goto errout;
1291                         break;
1292                 }
1293                 case RTA_VIA:
1294                 {
1295                         if (nla_get_via(nla, &cfg->rc_via_alen,
1296                                         &cfg->rc_via_table, cfg->rc_via))
1297                                 goto errout;
1298                         break;
1299                 }
1300                 case RTA_MULTIPATH:
1301                 {
1302                         cfg->rc_mp = nla_data(nla);
1303                         cfg->rc_mp_len = nla_len(nla);
1304                         break;
1305                 }
1306                 default:
1307                         /* Unsupported attribute */
1308                         goto errout;
1309                 }
1310         }
1311
1312         err = 0;
1313 errout:
1314         return err;
1315 }
1316
1317 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1318 {
1319         struct mpls_route_config cfg;
1320         int err;
1321
1322         err = rtm_to_route_config(skb, nlh, &cfg);
1323         if (err < 0)
1324                 return err;
1325
1326         return mpls_route_del(&cfg);
1327 }
1328
1329
1330 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1331 {
1332         struct mpls_route_config cfg;
1333         int err;
1334
1335         err = rtm_to_route_config(skb, nlh, &cfg);
1336         if (err < 0)
1337                 return err;
1338
1339         return mpls_route_add(&cfg);
1340 }
1341
1342 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1343                            u32 label, struct mpls_route *rt, int flags)
1344 {
1345         struct net_device *dev;
1346         struct nlmsghdr *nlh;
1347         struct rtmsg *rtm;
1348
1349         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1350         if (nlh == NULL)
1351                 return -EMSGSIZE;
1352
1353         rtm = nlmsg_data(nlh);
1354         rtm->rtm_family = AF_MPLS;
1355         rtm->rtm_dst_len = 20;
1356         rtm->rtm_src_len = 0;
1357         rtm->rtm_tos = 0;
1358         rtm->rtm_table = RT_TABLE_MAIN;
1359         rtm->rtm_protocol = rt->rt_protocol;
1360         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1361         rtm->rtm_type = RTN_UNICAST;
1362         rtm->rtm_flags = 0;
1363
1364         if (nla_put_labels(skb, RTA_DST, 1, &label))
1365                 goto nla_put_failure;
1366         if (rt->rt_nhn == 1) {
1367                 const struct mpls_nh *nh = rt->rt_nh;
1368
1369                 if (nh->nh_labels &&
1370                     nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1371                                    nh->nh_label))
1372                         goto nla_put_failure;
1373                 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1374                     nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1375                                 nh->nh_via_alen))
1376                         goto nla_put_failure;
1377                 dev = rtnl_dereference(nh->nh_dev);
1378                 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1379                         goto nla_put_failure;
1380                 if (nh->nh_flags & RTNH_F_LINKDOWN)
1381                         rtm->rtm_flags |= RTNH_F_LINKDOWN;
1382                 if (nh->nh_flags & RTNH_F_DEAD)
1383                         rtm->rtm_flags |= RTNH_F_DEAD;
1384         } else {
1385                 struct rtnexthop *rtnh;
1386                 struct nlattr *mp;
1387                 int dead = 0;
1388                 int linkdown = 0;
1389
1390                 mp = nla_nest_start(skb, RTA_MULTIPATH);
1391                 if (!mp)
1392                         goto nla_put_failure;
1393
1394                 for_nexthops(rt) {
1395                         rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1396                         if (!rtnh)
1397                                 goto nla_put_failure;
1398
1399                         dev = rtnl_dereference(nh->nh_dev);
1400                         if (dev)
1401                                 rtnh->rtnh_ifindex = dev->ifindex;
1402                         if (nh->nh_flags & RTNH_F_LINKDOWN) {
1403                                 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1404                                 linkdown++;
1405                         }
1406                         if (nh->nh_flags & RTNH_F_DEAD) {
1407                                 rtnh->rtnh_flags |= RTNH_F_DEAD;
1408                                 dead++;
1409                         }
1410
1411                         if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1412                                                             nh->nh_labels,
1413                                                             nh->nh_label))
1414                                 goto nla_put_failure;
1415                         if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1416                             nla_put_via(skb, nh->nh_via_table,
1417                                         mpls_nh_via(rt, nh),
1418                                         nh->nh_via_alen))
1419                                 goto nla_put_failure;
1420
1421                         /* length of rtnetlink header + attributes */
1422                         rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1423                 } endfor_nexthops(rt);
1424
1425                 if (linkdown == rt->rt_nhn)
1426                         rtm->rtm_flags |= RTNH_F_LINKDOWN;
1427                 if (dead == rt->rt_nhn)
1428                         rtm->rtm_flags |= RTNH_F_DEAD;
1429
1430                 nla_nest_end(skb, mp);
1431         }
1432
1433         nlmsg_end(skb, nlh);
1434         return 0;
1435
1436 nla_put_failure:
1437         nlmsg_cancel(skb, nlh);
1438         return -EMSGSIZE;
1439 }
1440
1441 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1442 {
1443         struct net *net = sock_net(skb->sk);
1444         struct mpls_route __rcu **platform_label;
1445         size_t platform_labels;
1446         unsigned int index;
1447
1448         ASSERT_RTNL();
1449
1450         index = cb->args[0];
1451         if (index < MPLS_LABEL_FIRST_UNRESERVED)
1452                 index = MPLS_LABEL_FIRST_UNRESERVED;
1453
1454         platform_label = rtnl_dereference(net->mpls.platform_label);
1455         platform_labels = net->mpls.platform_labels;
1456         for (; index < platform_labels; index++) {
1457                 struct mpls_route *rt;
1458                 rt = rtnl_dereference(platform_label[index]);
1459                 if (!rt)
1460                         continue;
1461
1462                 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1463                                     cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1464                                     index, rt, NLM_F_MULTI) < 0)
1465                         break;
1466         }
1467         cb->args[0] = index;
1468
1469         return skb->len;
1470 }
1471
1472 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1473 {
1474         size_t payload =
1475                 NLMSG_ALIGN(sizeof(struct rtmsg))
1476                 + nla_total_size(4);                    /* RTA_DST */
1477
1478         if (rt->rt_nhn == 1) {
1479                 struct mpls_nh *nh = rt->rt_nh;
1480
1481                 if (nh->nh_dev)
1482                         payload += nla_total_size(4); /* RTA_OIF */
1483                 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1484                         payload += nla_total_size(2 + nh->nh_via_alen);
1485                 if (nh->nh_labels) /* RTA_NEWDST */
1486                         payload += nla_total_size(nh->nh_labels * 4);
1487         } else {
1488                 /* each nexthop is packed in an attribute */
1489                 size_t nhsize = 0;
1490
1491                 for_nexthops(rt) {
1492                         nhsize += nla_total_size(sizeof(struct rtnexthop));
1493                         /* RTA_VIA */
1494                         if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1495                                 nhsize += nla_total_size(2 + nh->nh_via_alen);
1496                         if (nh->nh_labels)
1497                                 nhsize += nla_total_size(nh->nh_labels * 4);
1498                 } endfor_nexthops(rt);
1499                 /* nested attribute */
1500                 payload += nla_total_size(nhsize);
1501         }
1502
1503         return payload;
1504 }
1505
1506 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1507                        struct nlmsghdr *nlh, struct net *net, u32 portid,
1508                        unsigned int nlm_flags)
1509 {
1510         struct sk_buff *skb;
1511         u32 seq = nlh ? nlh->nlmsg_seq : 0;
1512         int err = -ENOBUFS;
1513
1514         skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1515         if (skb == NULL)
1516                 goto errout;
1517
1518         err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1519         if (err < 0) {
1520                 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1521                 WARN_ON(err == -EMSGSIZE);
1522                 kfree_skb(skb);
1523                 goto errout;
1524         }
1525         rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1526
1527         return;
1528 errout:
1529         if (err < 0)
1530                 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1531 }
1532
1533 static int resize_platform_label_table(struct net *net, size_t limit)
1534 {
1535         size_t size = sizeof(struct mpls_route *) * limit;
1536         size_t old_limit;
1537         size_t cp_size;
1538         struct mpls_route __rcu **labels = NULL, **old;
1539         struct mpls_route *rt0 = NULL, *rt2 = NULL;
1540         unsigned index;
1541
1542         if (size) {
1543                 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1544                 if (!labels)
1545                         labels = vzalloc(size);
1546
1547                 if (!labels)
1548                         goto nolabels;
1549         }
1550
1551         /* In case the predefined labels need to be populated */
1552         if (limit > MPLS_LABEL_IPV4NULL) {
1553                 struct net_device *lo = net->loopback_dev;
1554                 rt0 = mpls_rt_alloc(1, lo->addr_len);
1555                 if (!rt0)
1556                         goto nort0;
1557                 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1558                 rt0->rt_protocol = RTPROT_KERNEL;
1559                 rt0->rt_payload_type = MPT_IPV4;
1560                 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1561                 rt0->rt_nh->nh_via_alen = lo->addr_len;
1562                 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1563                        lo->addr_len);
1564         }
1565         if (limit > MPLS_LABEL_IPV6NULL) {
1566                 struct net_device *lo = net->loopback_dev;
1567                 rt2 = mpls_rt_alloc(1, lo->addr_len);
1568                 if (!rt2)
1569                         goto nort2;
1570                 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1571                 rt2->rt_protocol = RTPROT_KERNEL;
1572                 rt2->rt_payload_type = MPT_IPV6;
1573                 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1574                 rt2->rt_nh->nh_via_alen = lo->addr_len;
1575                 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1576                        lo->addr_len);
1577         }
1578
1579         rtnl_lock();
1580         /* Remember the original table */
1581         old = rtnl_dereference(net->mpls.platform_label);
1582         old_limit = net->mpls.platform_labels;
1583
1584         /* Free any labels beyond the new table */
1585         for (index = limit; index < old_limit; index++)
1586                 mpls_route_update(net, index, NULL, NULL);
1587
1588         /* Copy over the old labels */
1589         cp_size = size;
1590         if (old_limit < limit)
1591                 cp_size = old_limit * sizeof(struct mpls_route *);
1592
1593         memcpy(labels, old, cp_size);
1594
1595         /* If needed set the predefined labels */
1596         if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1597             (limit > MPLS_LABEL_IPV6NULL)) {
1598                 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1599                 rt2 = NULL;
1600         }
1601
1602         if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1603             (limit > MPLS_LABEL_IPV4NULL)) {
1604                 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1605                 rt0 = NULL;
1606         }
1607
1608         /* Update the global pointers */
1609         net->mpls.platform_labels = limit;
1610         rcu_assign_pointer(net->mpls.platform_label, labels);
1611
1612         rtnl_unlock();
1613
1614         mpls_rt_free(rt2);
1615         mpls_rt_free(rt0);
1616
1617         if (old) {
1618                 synchronize_rcu();
1619                 kvfree(old);
1620         }
1621         return 0;
1622
1623 nort2:
1624         mpls_rt_free(rt0);
1625 nort0:
1626         kvfree(labels);
1627 nolabels:
1628         return -ENOMEM;
1629 }
1630
1631 static int mpls_platform_labels(struct ctl_table *table, int write,
1632                                 void __user *buffer, size_t *lenp, loff_t *ppos)
1633 {
1634         struct net *net = table->data;
1635         int platform_labels = net->mpls.platform_labels;
1636         int ret;
1637         struct ctl_table tmp = {
1638                 .procname       = table->procname,
1639                 .data           = &platform_labels,
1640                 .maxlen         = sizeof(int),
1641                 .mode           = table->mode,
1642                 .extra1         = &zero,
1643                 .extra2         = &label_limit,
1644         };
1645
1646         ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1647
1648         if (write && ret == 0)
1649                 ret = resize_platform_label_table(net, platform_labels);
1650
1651         return ret;
1652 }
1653
1654 static const struct ctl_table mpls_table[] = {
1655         {
1656                 .procname       = "platform_labels",
1657                 .data           = NULL,
1658                 .maxlen         = sizeof(int),
1659                 .mode           = 0644,
1660                 .proc_handler   = mpls_platform_labels,
1661         },
1662         { }
1663 };
1664
1665 static int mpls_net_init(struct net *net)
1666 {
1667         struct ctl_table *table;
1668
1669         net->mpls.platform_labels = 0;
1670         net->mpls.platform_label = NULL;
1671
1672         table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1673         if (table == NULL)
1674                 return -ENOMEM;
1675
1676         table[0].data = net;
1677         net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1678         if (net->mpls.ctl == NULL) {
1679                 kfree(table);
1680                 return -ENOMEM;
1681         }
1682
1683         return 0;
1684 }
1685
1686 static void mpls_net_exit(struct net *net)
1687 {
1688         struct mpls_route __rcu **platform_label;
1689         size_t platform_labels;
1690         struct ctl_table *table;
1691         unsigned int index;
1692
1693         table = net->mpls.ctl->ctl_table_arg;
1694         unregister_net_sysctl_table(net->mpls.ctl);
1695         kfree(table);
1696
1697         /* An rcu grace period has passed since there was a device in
1698          * the network namespace (and thus the last in flight packet)
1699          * left this network namespace.  This is because
1700          * unregister_netdevice_many and netdev_run_todo has completed
1701          * for each network device that was in this network namespace.
1702          *
1703          * As such no additional rcu synchronization is necessary when
1704          * freeing the platform_label table.
1705          */
1706         rtnl_lock();
1707         platform_label = rtnl_dereference(net->mpls.platform_label);
1708         platform_labels = net->mpls.platform_labels;
1709         for (index = 0; index < platform_labels; index++) {
1710                 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1711                 RCU_INIT_POINTER(platform_label[index], NULL);
1712                 mpls_notify_route(net, index, rt, NULL, NULL);
1713                 mpls_rt_free(rt);
1714         }
1715         rtnl_unlock();
1716
1717         kvfree(platform_label);
1718 }
1719
1720 static struct pernet_operations mpls_net_ops = {
1721         .init = mpls_net_init,
1722         .exit = mpls_net_exit,
1723 };
1724
1725 static int __init mpls_init(void)
1726 {
1727         int err;
1728
1729         BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1730
1731         err = register_pernet_subsys(&mpls_net_ops);
1732         if (err)
1733                 goto out;
1734
1735         err = register_netdevice_notifier(&mpls_dev_notifier);
1736         if (err)
1737                 goto out_unregister_pernet;
1738
1739         dev_add_pack(&mpls_packet_type);
1740
1741         rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1742         rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1743         rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1744         err = 0;
1745 out:
1746         return err;
1747
1748 out_unregister_pernet:
1749         unregister_pernet_subsys(&mpls_net_ops);
1750         goto out;
1751 }
1752 module_init(mpls_init);
1753
1754 static void __exit mpls_exit(void)
1755 {
1756         rtnl_unregister_all(PF_MPLS);
1757         dev_remove_pack(&mpls_packet_type);
1758         unregister_netdevice_notifier(&mpls_dev_notifier);
1759         unregister_pernet_subsys(&mpls_net_ops);
1760 }
1761 module_exit(mpls_exit);
1762
1763 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1764 MODULE_LICENSE("GPL v2");
1765 MODULE_ALIAS_NETPROTO(PF_MPLS);