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