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