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