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