Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.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/vmalloc.h>
11 #include <net/ip.h>
12 #include <net/dst.h>
13 #include <net/sock.h>
14 #include <net/arp.h>
15 #include <net/ip_fib.h>
16 #include <net/netevent.h>
17 #include <net/netns/generic.h>
18 #if IS_ENABLED(CONFIG_IPV6)
19 #include <net/ipv6.h>
20 #include <net/addrconf.h>
21 #endif
22 #include "internal.h"
23
24 #define LABEL_NOT_SPECIFIED (1<<20)
25 #define MAX_NEW_LABELS 2
26
27 /* This maximum ha length copied from the definition of struct neighbour */
28 #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
29
30 struct mpls_route { /* next hop label forwarding entry */
31         struct net_device __rcu *rt_dev;
32         struct rcu_head         rt_rcu;
33         u32                     rt_label[MAX_NEW_LABELS];
34         u8                      rt_protocol; /* routing protocol that set this entry */
35         u8                      rt_labels;
36         u8                      rt_via_alen;
37         u8                      rt_via_table;
38         u8                      rt_via[0];
39 };
40
41 static int zero = 0;
42 static int label_limit = (1 << 20) - 1;
43
44 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
45                        struct nlmsghdr *nlh, struct net *net, u32 portid,
46                        unsigned int nlm_flags);
47
48 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
49 {
50         struct mpls_route *rt = NULL;
51
52         if (index < net->mpls.platform_labels) {
53                 struct mpls_route __rcu **platform_label =
54                         rcu_dereference(net->mpls.platform_label);
55                 rt = rcu_dereference(platform_label[index]);
56         }
57         return rt;
58 }
59
60 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
61 {
62         return rcu_dereference_rtnl(dev->mpls_ptr);
63 }
64
65 bool mpls_output_possible(const struct net_device *dev)
66 {
67         return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
68 }
69 EXPORT_SYMBOL_GPL(mpls_output_possible);
70
71 static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
72 {
73         /* The size of the layer 2.5 labels to be added for this route */
74         return rt->rt_labels * sizeof(struct mpls_shim_hdr);
75 }
76
77 unsigned int mpls_dev_mtu(const struct net_device *dev)
78 {
79         /* The amount of data the layer 2 frame can hold */
80         return dev->mtu;
81 }
82 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
83
84 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
85 {
86         if (skb->len <= mtu)
87                 return false;
88
89         if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
90                 return false;
91
92         return true;
93 }
94 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
95
96 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
97                         struct mpls_entry_decoded dec)
98 {
99         /* RFC4385 and RFC5586 encode other packets in mpls such that
100          * they don't conflict with the ip version number, making
101          * decoding by examining the ip version correct in everything
102          * except for the strangest cases.
103          *
104          * The strange cases if we choose to support them will require
105          * manual configuration.
106          */
107         struct iphdr *hdr4;
108         bool success = true;
109
110         /* The IPv4 code below accesses through the IPv4 header
111          * checksum, which is 12 bytes into the packet.
112          * The IPv6 code below accesses through the IPv6 hop limit
113          * which is 8 bytes into the packet.
114          *
115          * For all supported cases there should always be at least 12
116          * bytes of packet data present.  The IPv4 header is 20 bytes
117          * without options and the IPv6 header is always 40 bytes
118          * long.
119          */
120         if (!pskb_may_pull(skb, 12))
121                 return false;
122
123         /* Use ip_hdr to find the ip protocol version */
124         hdr4 = ip_hdr(skb);
125         if (hdr4->version == 4) {
126                 skb->protocol = htons(ETH_P_IP);
127                 csum_replace2(&hdr4->check,
128                               htons(hdr4->ttl << 8),
129                               htons(dec.ttl << 8));
130                 hdr4->ttl = dec.ttl;
131         }
132         else if (hdr4->version == 6) {
133                 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
134                 skb->protocol = htons(ETH_P_IPV6);
135                 hdr6->hop_limit = dec.ttl;
136         }
137         else
138                 /* version 0 and version 1 are used by pseudo wires */
139                 success = false;
140         return success;
141 }
142
143 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
144                         struct packet_type *pt, struct net_device *orig_dev)
145 {
146         struct net *net = dev_net(dev);
147         struct mpls_shim_hdr *hdr;
148         struct mpls_route *rt;
149         struct mpls_entry_decoded dec;
150         struct net_device *out_dev;
151         struct mpls_dev *mdev;
152         unsigned int hh_len;
153         unsigned int new_header_size;
154         unsigned int mtu;
155         int err;
156
157         /* Careful this entire function runs inside of an rcu critical section */
158
159         mdev = mpls_dev_get(dev);
160         if (!mdev || !mdev->input_enabled)
161                 goto drop;
162
163         if (skb->pkt_type != PACKET_HOST)
164                 goto drop;
165
166         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
167                 goto drop;
168
169         if (!pskb_may_pull(skb, sizeof(*hdr)))
170                 goto drop;
171
172         /* Read and decode the label */
173         hdr = mpls_hdr(skb);
174         dec = mpls_entry_decode(hdr);
175
176         /* Pop the label */
177         skb_pull(skb, sizeof(*hdr));
178         skb_reset_network_header(skb);
179
180         skb_orphan(skb);
181
182         rt = mpls_route_input_rcu(net, dec.label);
183         if (!rt)
184                 goto drop;
185
186         /* Find the output device */
187         out_dev = rcu_dereference(rt->rt_dev);
188         if (!mpls_output_possible(out_dev))
189                 goto drop;
190
191         if (skb_warn_if_lro(skb))
192                 goto drop;
193
194         skb_forward_csum(skb);
195
196         /* Verify ttl is valid */
197         if (dec.ttl <= 1)
198                 goto drop;
199         dec.ttl -= 1;
200
201         /* Verify the destination can hold the packet */
202         new_header_size = mpls_rt_header_size(rt);
203         mtu = mpls_dev_mtu(out_dev);
204         if (mpls_pkt_too_big(skb, mtu - new_header_size))
205                 goto drop;
206
207         hh_len = LL_RESERVED_SPACE(out_dev);
208         if (!out_dev->header_ops)
209                 hh_len = 0;
210
211         /* Ensure there is enough space for the headers in the skb */
212         if (skb_cow(skb, hh_len + new_header_size))
213                 goto drop;
214
215         skb->dev = out_dev;
216         skb->protocol = htons(ETH_P_MPLS_UC);
217
218         if (unlikely(!new_header_size && dec.bos)) {
219                 /* Penultimate hop popping */
220                 if (!mpls_egress(rt, skb, dec))
221                         goto drop;
222         } else {
223                 bool bos;
224                 int i;
225                 skb_push(skb, new_header_size);
226                 skb_reset_network_header(skb);
227                 /* Push the new labels */
228                 hdr = mpls_hdr(skb);
229                 bos = dec.bos;
230                 for (i = rt->rt_labels - 1; i >= 0; i--) {
231                         hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
232                         bos = false;
233                 }
234         }
235
236         err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
237         if (err)
238                 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
239                                     __func__, err);
240         return 0;
241
242 drop:
243         kfree_skb(skb);
244         return NET_RX_DROP;
245 }
246
247 static struct packet_type mpls_packet_type __read_mostly = {
248         .type = cpu_to_be16(ETH_P_MPLS_UC),
249         .func = mpls_forward,
250 };
251
252 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
253         [RTA_DST]               = { .type = NLA_U32 },
254         [RTA_OIF]               = { .type = NLA_U32 },
255 };
256
257 struct mpls_route_config {
258         u32             rc_protocol;
259         u32             rc_ifindex;
260         u16             rc_via_table;
261         u16             rc_via_alen;
262         u8              rc_via[MAX_VIA_ALEN];
263         u32             rc_label;
264         u32             rc_output_labels;
265         u32             rc_output_label[MAX_NEW_LABELS];
266         u32             rc_nlflags;
267         struct nl_info  rc_nlinfo;
268 };
269
270 static struct mpls_route *mpls_rt_alloc(size_t alen)
271 {
272         struct mpls_route *rt;
273
274         rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
275         if (rt)
276                 rt->rt_via_alen = alen;
277         return rt;
278 }
279
280 static void mpls_rt_free(struct mpls_route *rt)
281 {
282         if (rt)
283                 kfree_rcu(rt, rt_rcu);
284 }
285
286 static void mpls_notify_route(struct net *net, unsigned index,
287                               struct mpls_route *old, struct mpls_route *new,
288                               const struct nl_info *info)
289 {
290         struct nlmsghdr *nlh = info ? info->nlh : NULL;
291         unsigned portid = info ? info->portid : 0;
292         int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
293         struct mpls_route *rt = new ? new : old;
294         unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
295         /* Ignore reserved labels for now */
296         if (rt && (index >= 16))
297                 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
298 }
299
300 static void mpls_route_update(struct net *net, unsigned index,
301                               struct net_device *dev, struct mpls_route *new,
302                               const struct nl_info *info)
303 {
304         struct mpls_route __rcu **platform_label;
305         struct mpls_route *rt, *old = NULL;
306
307         ASSERT_RTNL();
308
309         platform_label = rtnl_dereference(net->mpls.platform_label);
310         rt = rtnl_dereference(platform_label[index]);
311         if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
312                 rcu_assign_pointer(platform_label[index], new);
313                 old = rt;
314         }
315
316         mpls_notify_route(net, index, old, new, info);
317
318         /* If we removed a route free it now */
319         mpls_rt_free(old);
320 }
321
322 static unsigned find_free_label(struct net *net)
323 {
324         struct mpls_route __rcu **platform_label;
325         size_t platform_labels;
326         unsigned index;
327
328         platform_label = rtnl_dereference(net->mpls.platform_label);
329         platform_labels = net->mpls.platform_labels;
330         for (index = 16; index < platform_labels; index++) {
331                 if (!rtnl_dereference(platform_label[index]))
332                         return index;
333         }
334         return LABEL_NOT_SPECIFIED;
335 }
336
337 #if IS_ENABLED(CONFIG_INET)
338 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
339 {
340         struct net_device *dev = NULL;
341         struct rtable *rt;
342         struct in_addr daddr;
343
344         memcpy(&daddr, addr, sizeof(struct in_addr));
345         rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
346         if (IS_ERR(rt))
347                 goto errout;
348
349         dev = rt->dst.dev;
350         dev_hold(dev);
351
352         ip_rt_put(rt);
353
354         return dev;
355 errout:
356         return ERR_PTR(-ENODEV);
357 }
358 #else
359 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
360 {
361         return ERR_PTR(-EAFNOSUPPORT);
362 }
363 #endif
364
365 #if IS_ENABLED(CONFIG_IPV6)
366 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
367 {
368         struct net_device *dev = NULL;
369         struct dst_entry *dst;
370         struct flowi6 fl6;
371         int err;
372
373         if (!ipv6_stub)
374                 return ERR_PTR(-EAFNOSUPPORT);
375
376         memset(&fl6, 0, sizeof(fl6));
377         memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
378         err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
379         if (err)
380                 goto errout;
381
382         dev = dst->dev;
383         dev_hold(dev);
384         dst_release(dst);
385
386         return dev;
387
388 errout:
389         return ERR_PTR(err);
390 }
391 #else
392 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
393 {
394         return ERR_PTR(-EAFNOSUPPORT);
395 }
396 #endif
397
398 static struct net_device *find_outdev(struct net *net,
399                                       struct mpls_route_config *cfg)
400 {
401         struct net_device *dev = NULL;
402
403         if (!cfg->rc_ifindex) {
404                 switch (cfg->rc_via_table) {
405                 case NEIGH_ARP_TABLE:
406                         dev = inet_fib_lookup_dev(net, cfg->rc_via);
407                         break;
408                 case NEIGH_ND_TABLE:
409                         dev = inet6_fib_lookup_dev(net, cfg->rc_via);
410                         break;
411                 case NEIGH_LINK_TABLE:
412                         break;
413                 }
414         } else {
415                 dev = dev_get_by_index(net, cfg->rc_ifindex);
416         }
417
418         return dev;
419 }
420
421 static int mpls_route_add(struct mpls_route_config *cfg)
422 {
423         struct mpls_route __rcu **platform_label;
424         struct net *net = cfg->rc_nlinfo.nl_net;
425         struct net_device *dev = NULL;
426         struct mpls_route *rt, *old;
427         unsigned index;
428         int i;
429         int err = -EINVAL;
430
431         index = cfg->rc_label;
432
433         /* If a label was not specified during insert pick one */
434         if ((index == LABEL_NOT_SPECIFIED) &&
435             (cfg->rc_nlflags & NLM_F_CREATE)) {
436                 index = find_free_label(net);
437         }
438
439         /* The first 16 labels are reserved, and may not be set */
440         if (index < 16)
441                 goto errout;
442
443         /* The full 20 bit range may not be supported. */
444         if (index >= net->mpls.platform_labels)
445                 goto errout;
446
447         /* Ensure only a supported number of labels are present */
448         if (cfg->rc_output_labels > MAX_NEW_LABELS)
449                 goto errout;
450
451         dev = find_outdev(net, cfg);
452         if (IS_ERR(dev)) {
453                 err = PTR_ERR(dev);
454                 dev = NULL;
455                 goto errout;
456         }
457
458         /* Ensure this is a supported device */
459         err = -EINVAL;
460         if (!mpls_dev_get(dev))
461                 goto errout;
462
463         err = -EINVAL;
464         if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
465             (dev->addr_len != cfg->rc_via_alen))
466                 goto errout;
467
468         /* Append makes no sense with mpls */
469         err = -EOPNOTSUPP;
470         if (cfg->rc_nlflags & NLM_F_APPEND)
471                 goto errout;
472
473         err = -EEXIST;
474         platform_label = rtnl_dereference(net->mpls.platform_label);
475         old = rtnl_dereference(platform_label[index]);
476         if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
477                 goto errout;
478
479         err = -EEXIST;
480         if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
481                 goto errout;
482
483         err = -ENOENT;
484         if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
485                 goto errout;
486
487         err = -ENOMEM;
488         rt = mpls_rt_alloc(cfg->rc_via_alen);
489         if (!rt)
490                 goto errout;
491
492         rt->rt_labels = cfg->rc_output_labels;
493         for (i = 0; i < rt->rt_labels; i++)
494                 rt->rt_label[i] = cfg->rc_output_label[i];
495         rt->rt_protocol = cfg->rc_protocol;
496         RCU_INIT_POINTER(rt->rt_dev, dev);
497         rt->rt_via_table = cfg->rc_via_table;
498         memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
499
500         mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
501
502         dev_put(dev);
503         return 0;
504
505 errout:
506         if (dev)
507                 dev_put(dev);
508         return err;
509 }
510
511 static int mpls_route_del(struct mpls_route_config *cfg)
512 {
513         struct net *net = cfg->rc_nlinfo.nl_net;
514         unsigned index;
515         int err = -EINVAL;
516
517         index = cfg->rc_label;
518
519         /* The first 16 labels are reserved, and may not be removed */
520         if (index < 16)
521                 goto errout;
522
523         /* The full 20 bit range may not be supported */
524         if (index >= net->mpls.platform_labels)
525                 goto errout;
526
527         mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
528
529         err = 0;
530 errout:
531         return err;
532 }
533
534 #define MPLS_PERDEV_SYSCTL_OFFSET(field)        \
535         (&((struct mpls_dev *)0)->field)
536
537 static const struct ctl_table mpls_dev_table[] = {
538         {
539                 .procname       = "input",
540                 .maxlen         = sizeof(int),
541                 .mode           = 0644,
542                 .proc_handler   = proc_dointvec,
543                 .data           = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
544         },
545         { }
546 };
547
548 static int mpls_dev_sysctl_register(struct net_device *dev,
549                                     struct mpls_dev *mdev)
550 {
551         char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
552         struct ctl_table *table;
553         int i;
554
555         table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
556         if (!table)
557                 goto out;
558
559         /* Table data contains only offsets relative to the base of
560          * the mdev at this point, so make them absolute.
561          */
562         for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
563                 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
564
565         snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
566
567         mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
568         if (!mdev->sysctl)
569                 goto free;
570
571         return 0;
572
573 free:
574         kfree(table);
575 out:
576         return -ENOBUFS;
577 }
578
579 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
580 {
581         struct ctl_table *table;
582
583         table = mdev->sysctl->ctl_table_arg;
584         unregister_net_sysctl_table(mdev->sysctl);
585         kfree(table);
586 }
587
588 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
589 {
590         struct mpls_dev *mdev;
591         int err = -ENOMEM;
592
593         ASSERT_RTNL();
594
595         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
596         if (!mdev)
597                 return ERR_PTR(err);
598
599         err = mpls_dev_sysctl_register(dev, mdev);
600         if (err)
601                 goto free;
602
603         rcu_assign_pointer(dev->mpls_ptr, mdev);
604
605         return mdev;
606
607 free:
608         kfree(mdev);
609         return ERR_PTR(err);
610 }
611
612 static void mpls_ifdown(struct net_device *dev)
613 {
614         struct mpls_route __rcu **platform_label;
615         struct net *net = dev_net(dev);
616         struct mpls_dev *mdev;
617         unsigned index;
618
619         platform_label = rtnl_dereference(net->mpls.platform_label);
620         for (index = 0; index < net->mpls.platform_labels; index++) {
621                 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
622                 if (!rt)
623                         continue;
624                 if (rtnl_dereference(rt->rt_dev) != dev)
625                         continue;
626                 rt->rt_dev = NULL;
627         }
628
629         mdev = mpls_dev_get(dev);
630         if (!mdev)
631                 return;
632
633         mpls_dev_sysctl_unregister(mdev);
634
635         RCU_INIT_POINTER(dev->mpls_ptr, NULL);
636
637         kfree_rcu(mdev, rcu);
638 }
639
640 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
641                            void *ptr)
642 {
643         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
644         struct mpls_dev *mdev;
645
646         switch(event) {
647         case NETDEV_REGISTER:
648                 /* For now just support ethernet devices */
649                 if ((dev->type == ARPHRD_ETHER) ||
650                     (dev->type == ARPHRD_LOOPBACK)) {
651                         mdev = mpls_add_dev(dev);
652                         if (IS_ERR(mdev))
653                                 return notifier_from_errno(PTR_ERR(mdev));
654                 }
655                 break;
656
657         case NETDEV_UNREGISTER:
658                 mpls_ifdown(dev);
659                 break;
660         case NETDEV_CHANGENAME:
661                 mdev = mpls_dev_get(dev);
662                 if (mdev) {
663                         int err;
664
665                         mpls_dev_sysctl_unregister(mdev);
666                         err = mpls_dev_sysctl_register(dev, mdev);
667                         if (err)
668                                 return notifier_from_errno(err);
669                 }
670                 break;
671         }
672         return NOTIFY_OK;
673 }
674
675 static struct notifier_block mpls_dev_notifier = {
676         .notifier_call = mpls_dev_notify,
677 };
678
679 static int nla_put_via(struct sk_buff *skb,
680                        u8 table, const void *addr, int alen)
681 {
682         static const int table_to_family[NEIGH_NR_TABLES + 1] = {
683                 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
684         };
685         struct nlattr *nla;
686         struct rtvia *via;
687         int family = AF_UNSPEC;
688
689         nla = nla_reserve(skb, RTA_VIA, alen + 2);
690         if (!nla)
691                 return -EMSGSIZE;
692
693         if (table <= NEIGH_NR_TABLES)
694                 family = table_to_family[table];
695
696         via = nla_data(nla);
697         via->rtvia_family = family;
698         memcpy(via->rtvia_addr, addr, alen);
699         return 0;
700 }
701
702 int nla_put_labels(struct sk_buff *skb, int attrtype,
703                    u8 labels, const u32 label[])
704 {
705         struct nlattr *nla;
706         struct mpls_shim_hdr *nla_label;
707         bool bos;
708         int i;
709         nla = nla_reserve(skb, attrtype, labels*4);
710         if (!nla)
711                 return -EMSGSIZE;
712
713         nla_label = nla_data(nla);
714         bos = true;
715         for (i = labels - 1; i >= 0; i--) {
716                 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
717                 bos = false;
718         }
719
720         return 0;
721 }
722 EXPORT_SYMBOL_GPL(nla_put_labels);
723
724 int nla_get_labels(const struct nlattr *nla,
725                    u32 max_labels, u32 *labels, u32 label[])
726 {
727         unsigned len = nla_len(nla);
728         unsigned nla_labels;
729         struct mpls_shim_hdr *nla_label;
730         bool bos;
731         int i;
732
733         /* len needs to be an even multiple of 4 (the label size) */
734         if (len & 3)
735                 return -EINVAL;
736
737         /* Limit the number of new labels allowed */
738         nla_labels = len/4;
739         if (nla_labels > max_labels)
740                 return -EINVAL;
741
742         nla_label = nla_data(nla);
743         bos = true;
744         for (i = nla_labels - 1; i >= 0; i--, bos = false) {
745                 struct mpls_entry_decoded dec;
746                 dec = mpls_entry_decode(nla_label + i);
747
748                 /* Ensure the bottom of stack flag is properly set
749                  * and ttl and tc are both clear.
750                  */
751                 if ((dec.bos != bos) || dec.ttl || dec.tc)
752                         return -EINVAL;
753
754                 switch (dec.label) {
755                 case MPLS_LABEL_IMPLNULL:
756                         /* RFC3032: This is a label that an LSR may
757                          * assign and distribute, but which never
758                          * actually appears in the encapsulation.
759                          */
760                         return -EINVAL;
761                 }
762
763                 label[i] = dec.label;
764         }
765         *labels = nla_labels;
766         return 0;
767 }
768 EXPORT_SYMBOL_GPL(nla_get_labels);
769
770 static int rtm_to_route_config(struct sk_buff *skb,  struct nlmsghdr *nlh,
771                                struct mpls_route_config *cfg)
772 {
773         struct rtmsg *rtm;
774         struct nlattr *tb[RTA_MAX+1];
775         int index;
776         int err;
777
778         err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
779         if (err < 0)
780                 goto errout;
781
782         err = -EINVAL;
783         rtm = nlmsg_data(nlh);
784         memset(cfg, 0, sizeof(*cfg));
785
786         if (rtm->rtm_family != AF_MPLS)
787                 goto errout;
788         if (rtm->rtm_dst_len != 20)
789                 goto errout;
790         if (rtm->rtm_src_len != 0)
791                 goto errout;
792         if (rtm->rtm_tos != 0)
793                 goto errout;
794         if (rtm->rtm_table != RT_TABLE_MAIN)
795                 goto errout;
796         /* Any value is acceptable for rtm_protocol */
797
798         /* As mpls uses destination specific addresses
799          * (or source specific address in the case of multicast)
800          * all addresses have universal scope.
801          */
802         if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
803                 goto errout;
804         if (rtm->rtm_type != RTN_UNICAST)
805                 goto errout;
806         if (rtm->rtm_flags != 0)
807                 goto errout;
808
809         cfg->rc_label           = LABEL_NOT_SPECIFIED;
810         cfg->rc_protocol        = rtm->rtm_protocol;
811         cfg->rc_nlflags         = nlh->nlmsg_flags;
812         cfg->rc_nlinfo.portid   = NETLINK_CB(skb).portid;
813         cfg->rc_nlinfo.nlh      = nlh;
814         cfg->rc_nlinfo.nl_net   = sock_net(skb->sk);
815
816         for (index = 0; index <= RTA_MAX; index++) {
817                 struct nlattr *nla = tb[index];
818                 if (!nla)
819                         continue;
820
821                 switch(index) {
822                 case RTA_OIF:
823                         cfg->rc_ifindex = nla_get_u32(nla);
824                         break;
825                 case RTA_NEWDST:
826                         if (nla_get_labels(nla, MAX_NEW_LABELS,
827                                            &cfg->rc_output_labels,
828                                            cfg->rc_output_label))
829                                 goto errout;
830                         break;
831                 case RTA_DST:
832                 {
833                         u32 label_count;
834                         if (nla_get_labels(nla, 1, &label_count,
835                                            &cfg->rc_label))
836                                 goto errout;
837
838                         /* The first 16 labels are reserved, and may not be set */
839                         if (cfg->rc_label < 16)
840                                 goto errout;
841
842                         break;
843                 }
844                 case RTA_VIA:
845                 {
846                         struct rtvia *via = nla_data(nla);
847                         if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
848                                 goto errout;
849                         cfg->rc_via_alen   = nla_len(nla) -
850                                 offsetof(struct rtvia, rtvia_addr);
851                         if (cfg->rc_via_alen > MAX_VIA_ALEN)
852                                 goto errout;
853
854                         /* Validate the address family */
855                         switch(via->rtvia_family) {
856                         case AF_PACKET:
857                                 cfg->rc_via_table = NEIGH_LINK_TABLE;
858                                 break;
859                         case AF_INET:
860                                 cfg->rc_via_table = NEIGH_ARP_TABLE;
861                                 if (cfg->rc_via_alen != 4)
862                                         goto errout;
863                                 break;
864                         case AF_INET6:
865                                 cfg->rc_via_table = NEIGH_ND_TABLE;
866                                 if (cfg->rc_via_alen != 16)
867                                         goto errout;
868                                 break;
869                         default:
870                                 /* Unsupported address family */
871                                 goto errout;
872                         }
873
874                         memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
875                         break;
876                 }
877                 default:
878                         /* Unsupported attribute */
879                         goto errout;
880                 }
881         }
882
883         err = 0;
884 errout:
885         return err;
886 }
887
888 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
889 {
890         struct mpls_route_config cfg;
891         int err;
892
893         err = rtm_to_route_config(skb, nlh, &cfg);
894         if (err < 0)
895                 return err;
896
897         return mpls_route_del(&cfg);
898 }
899
900
901 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
902 {
903         struct mpls_route_config cfg;
904         int err;
905
906         err = rtm_to_route_config(skb, nlh, &cfg);
907         if (err < 0)
908                 return err;
909
910         return mpls_route_add(&cfg);
911 }
912
913 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
914                            u32 label, struct mpls_route *rt, int flags)
915 {
916         struct net_device *dev;
917         struct nlmsghdr *nlh;
918         struct rtmsg *rtm;
919
920         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
921         if (nlh == NULL)
922                 return -EMSGSIZE;
923
924         rtm = nlmsg_data(nlh);
925         rtm->rtm_family = AF_MPLS;
926         rtm->rtm_dst_len = 20;
927         rtm->rtm_src_len = 0;
928         rtm->rtm_tos = 0;
929         rtm->rtm_table = RT_TABLE_MAIN;
930         rtm->rtm_protocol = rt->rt_protocol;
931         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
932         rtm->rtm_type = RTN_UNICAST;
933         rtm->rtm_flags = 0;
934
935         if (rt->rt_labels &&
936             nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
937                 goto nla_put_failure;
938         if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
939                 goto nla_put_failure;
940         dev = rtnl_dereference(rt->rt_dev);
941         if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
942                 goto nla_put_failure;
943         if (nla_put_labels(skb, RTA_DST, 1, &label))
944                 goto nla_put_failure;
945
946         nlmsg_end(skb, nlh);
947         return 0;
948
949 nla_put_failure:
950         nlmsg_cancel(skb, nlh);
951         return -EMSGSIZE;
952 }
953
954 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
955 {
956         struct net *net = sock_net(skb->sk);
957         struct mpls_route __rcu **platform_label;
958         size_t platform_labels;
959         unsigned int index;
960
961         ASSERT_RTNL();
962
963         index = cb->args[0];
964         if (index < 16)
965                 index = 16;
966
967         platform_label = rtnl_dereference(net->mpls.platform_label);
968         platform_labels = net->mpls.platform_labels;
969         for (; index < platform_labels; index++) {
970                 struct mpls_route *rt;
971                 rt = rtnl_dereference(platform_label[index]);
972                 if (!rt)
973                         continue;
974
975                 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
976                                     cb->nlh->nlmsg_seq, RTM_NEWROUTE,
977                                     index, rt, NLM_F_MULTI) < 0)
978                         break;
979         }
980         cb->args[0] = index;
981
982         return skb->len;
983 }
984
985 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
986 {
987         size_t payload =
988                 NLMSG_ALIGN(sizeof(struct rtmsg))
989                 + nla_total_size(2 + rt->rt_via_alen)   /* RTA_VIA */
990                 + nla_total_size(4);                    /* RTA_DST */
991         if (rt->rt_labels)                              /* RTA_NEWDST */
992                 payload += nla_total_size(rt->rt_labels * 4);
993         if (rt->rt_dev)                                 /* RTA_OIF */
994                 payload += nla_total_size(4);
995         return payload;
996 }
997
998 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
999                        struct nlmsghdr *nlh, struct net *net, u32 portid,
1000                        unsigned int nlm_flags)
1001 {
1002         struct sk_buff *skb;
1003         u32 seq = nlh ? nlh->nlmsg_seq : 0;
1004         int err = -ENOBUFS;
1005
1006         skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1007         if (skb == NULL)
1008                 goto errout;
1009
1010         err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1011         if (err < 0) {
1012                 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1013                 WARN_ON(err == -EMSGSIZE);
1014                 kfree_skb(skb);
1015                 goto errout;
1016         }
1017         rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1018
1019         return;
1020 errout:
1021         if (err < 0)
1022                 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1023 }
1024
1025 static int resize_platform_label_table(struct net *net, size_t limit)
1026 {
1027         size_t size = sizeof(struct mpls_route *) * limit;
1028         size_t old_limit;
1029         size_t cp_size;
1030         struct mpls_route __rcu **labels = NULL, **old;
1031         struct mpls_route *rt0 = NULL, *rt2 = NULL;
1032         unsigned index;
1033
1034         if (size) {
1035                 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1036                 if (!labels)
1037                         labels = vzalloc(size);
1038
1039                 if (!labels)
1040                         goto nolabels;
1041         }
1042
1043         /* In case the predefined labels need to be populated */
1044         if (limit > MPLS_LABEL_IPV4NULL) {
1045                 struct net_device *lo = net->loopback_dev;
1046                 rt0 = mpls_rt_alloc(lo->addr_len);
1047                 if (!rt0)
1048                         goto nort0;
1049                 RCU_INIT_POINTER(rt0->rt_dev, lo);
1050                 rt0->rt_protocol = RTPROT_KERNEL;
1051                 rt0->rt_via_table = NEIGH_LINK_TABLE;
1052                 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1053         }
1054         if (limit > MPLS_LABEL_IPV6NULL) {
1055                 struct net_device *lo = net->loopback_dev;
1056                 rt2 = mpls_rt_alloc(lo->addr_len);
1057                 if (!rt2)
1058                         goto nort2;
1059                 RCU_INIT_POINTER(rt2->rt_dev, lo);
1060                 rt2->rt_protocol = RTPROT_KERNEL;
1061                 rt2->rt_via_table = NEIGH_LINK_TABLE;
1062                 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1063         }
1064
1065         rtnl_lock();
1066         /* Remember the original table */
1067         old = rtnl_dereference(net->mpls.platform_label);
1068         old_limit = net->mpls.platform_labels;
1069
1070         /* Free any labels beyond the new table */
1071         for (index = limit; index < old_limit; index++)
1072                 mpls_route_update(net, index, NULL, NULL, NULL);
1073
1074         /* Copy over the old labels */
1075         cp_size = size;
1076         if (old_limit < limit)
1077                 cp_size = old_limit * sizeof(struct mpls_route *);
1078
1079         memcpy(labels, old, cp_size);
1080
1081         /* If needed set the predefined labels */
1082         if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1083             (limit > MPLS_LABEL_IPV6NULL)) {
1084                 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1085                 rt2 = NULL;
1086         }
1087
1088         if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1089             (limit > MPLS_LABEL_IPV4NULL)) {
1090                 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1091                 rt0 = NULL;
1092         }
1093
1094         /* Update the global pointers */
1095         net->mpls.platform_labels = limit;
1096         rcu_assign_pointer(net->mpls.platform_label, labels);
1097
1098         rtnl_unlock();
1099
1100         mpls_rt_free(rt2);
1101         mpls_rt_free(rt0);
1102
1103         if (old) {
1104                 synchronize_rcu();
1105                 kvfree(old);
1106         }
1107         return 0;
1108
1109 nort2:
1110         mpls_rt_free(rt0);
1111 nort0:
1112         kvfree(labels);
1113 nolabels:
1114         return -ENOMEM;
1115 }
1116
1117 static int mpls_platform_labels(struct ctl_table *table, int write,
1118                                 void __user *buffer, size_t *lenp, loff_t *ppos)
1119 {
1120         struct net *net = table->data;
1121         int platform_labels = net->mpls.platform_labels;
1122         int ret;
1123         struct ctl_table tmp = {
1124                 .procname       = table->procname,
1125                 .data           = &platform_labels,
1126                 .maxlen         = sizeof(int),
1127                 .mode           = table->mode,
1128                 .extra1         = &zero,
1129                 .extra2         = &label_limit,
1130         };
1131
1132         ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1133
1134         if (write && ret == 0)
1135                 ret = resize_platform_label_table(net, platform_labels);
1136
1137         return ret;
1138 }
1139
1140 static const struct ctl_table mpls_table[] = {
1141         {
1142                 .procname       = "platform_labels",
1143                 .data           = NULL,
1144                 .maxlen         = sizeof(int),
1145                 .mode           = 0644,
1146                 .proc_handler   = mpls_platform_labels,
1147         },
1148         { }
1149 };
1150
1151 static int mpls_net_init(struct net *net)
1152 {
1153         struct ctl_table *table;
1154
1155         net->mpls.platform_labels = 0;
1156         net->mpls.platform_label = NULL;
1157
1158         table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1159         if (table == NULL)
1160                 return -ENOMEM;
1161
1162         table[0].data = net;
1163         net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1164         if (net->mpls.ctl == NULL)
1165                 return -ENOMEM;
1166
1167         return 0;
1168 }
1169
1170 static void mpls_net_exit(struct net *net)
1171 {
1172         struct mpls_route __rcu **platform_label;
1173         size_t platform_labels;
1174         struct ctl_table *table;
1175         unsigned int index;
1176
1177         table = net->mpls.ctl->ctl_table_arg;
1178         unregister_net_sysctl_table(net->mpls.ctl);
1179         kfree(table);
1180
1181         /* An rcu grace period has passed since there was a device in
1182          * the network namespace (and thus the last in flight packet)
1183          * left this network namespace.  This is because
1184          * unregister_netdevice_many and netdev_run_todo has completed
1185          * for each network device that was in this network namespace.
1186          *
1187          * As such no additional rcu synchronization is necessary when
1188          * freeing the platform_label table.
1189          */
1190         rtnl_lock();
1191         platform_label = rtnl_dereference(net->mpls.platform_label);
1192         platform_labels = net->mpls.platform_labels;
1193         for (index = 0; index < platform_labels; index++) {
1194                 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1195                 RCU_INIT_POINTER(platform_label[index], NULL);
1196                 mpls_rt_free(rt);
1197         }
1198         rtnl_unlock();
1199
1200         kvfree(platform_label);
1201 }
1202
1203 static struct pernet_operations mpls_net_ops = {
1204         .init = mpls_net_init,
1205         .exit = mpls_net_exit,
1206 };
1207
1208 static int __init mpls_init(void)
1209 {
1210         int err;
1211
1212         BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1213
1214         err = register_pernet_subsys(&mpls_net_ops);
1215         if (err)
1216                 goto out;
1217
1218         err = register_netdevice_notifier(&mpls_dev_notifier);
1219         if (err)
1220                 goto out_unregister_pernet;
1221
1222         dev_add_pack(&mpls_packet_type);
1223
1224         rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1225         rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1226         rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1227         err = 0;
1228 out:
1229         return err;
1230
1231 out_unregister_pernet:
1232         unregister_pernet_subsys(&mpls_net_ops);
1233         goto out;
1234 }
1235 module_init(mpls_init);
1236
1237 static void __exit mpls_exit(void)
1238 {
1239         rtnl_unregister_all(PF_MPLS);
1240         dev_remove_pack(&mpls_packet_type);
1241         unregister_netdevice_notifier(&mpls_dev_notifier);
1242         unregister_pernet_subsys(&mpls_net_ops);
1243 }
1244 module_exit(mpls_exit);
1245
1246 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1247 MODULE_LICENSE("GPL v2");
1248 MODULE_ALIAS_NETPROTO(PF_MPLS);