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