1 #include <linux/types.h>
2 #include <linux/skbuff.h>
3 #include <linux/socket.h>
4 #include <linux/sysctl.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>
17 #include <net/ip_fib.h>
18 #include <net/netevent.h>
19 #include <net/netns/generic.h>
20 #if IS_ENABLED(CONFIG_IPV6)
23 #include <net/addrconf.h>
24 #include <net/nexthop.h>
27 /* Maximum number of labels to look ahead at when selecting a path of
30 #define MAX_MP_SELECT_LABELS 4
32 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
36 static int label_limit = (1 << 20) - 1;
37 static int ttl_max = 255;
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);
43 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
45 struct mpls_route *rt = NULL;
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]);
55 bool mpls_output_possible(const struct net_device *dev)
57 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
59 EXPORT_SYMBOL_GPL(mpls_output_possible);
61 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
63 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
64 int nh_index = nh - rt->rt_nh;
66 return nh0_via + rt->rt_max_alen * nh_index;
69 static const u8 *mpls_nh_via(const struct mpls_route *rt,
70 const struct mpls_nh *nh)
72 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
75 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
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);
81 unsigned int mpls_dev_mtu(const struct net_device *dev)
83 /* The amount of data the layer 2 frame can hold */
86 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
88 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
93 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
98 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
100 void mpls_stats_inc_outucastpkts(struct net_device *dev,
101 const struct sk_buff *skb)
103 struct mpls_dev *mdev;
105 if (skb->protocol == htons(ETH_P_MPLS_UC)) {
106 mdev = mpls_dev_get(dev);
108 MPLS_INC_STATS_LEN(mdev, skb->len,
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);
118 IP6_UPD_PO_STATS(dev_net(dev), in6dev,
119 IPSTATS_MIB_OUT, skb->len);
123 EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
125 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
127 struct mpls_entry_decoded dec;
128 unsigned int mpls_hdr_len = 0;
129 struct mpls_shim_hdr *hdr;
130 bool eli_seen = false;
134 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
136 mpls_hdr_len += sizeof(*hdr);
137 if (!pskb_may_pull(skb, mpls_hdr_len))
140 /* Read and decode the current label */
141 hdr = mpls_hdr(skb) + label_index;
142 dec = mpls_entry_decode(hdr);
144 /* RFC6790 - reserved labels MUST NOT be used as keys
145 * for the load-balancing function
147 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
148 hash = jhash_1word(dec.label, hash);
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
158 } else if (dec.label == MPLS_LABEL_ENTROPY) {
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;
169 v4hdr = (const struct iphdr *)(hdr + 1);
170 if (v4hdr->version == 4) {
171 hash = jhash_3words(ntohl(v4hdr->saddr),
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;
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);
192 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
195 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
200 /* No need to look further into packet if there's only
209 hash = mpls_multipath_hash(rt, skb);
210 nh_index = hash % alive;
211 if (alive == rt->rt_nhn)
214 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
219 } endfor_nexthops(rt);
222 return &rt->rt_nh[nh_index];
225 static bool mpls_egress(struct net *net, struct mpls_route *rt,
226 struct sk_buff *skb, struct mpls_entry_decoded dec)
228 enum mpls_payload_type payload_type;
229 bool success = false;
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.
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
241 if (!pskb_may_pull(skb, 12))
244 payload_type = rt->rt_payload_type;
245 if (payload_type == MPT_UNSPEC)
246 payload_type = ip_hdr(skb)->version;
248 switch (payload_type) {
250 struct iphdr *hdr4 = ip_hdr(skb);
252 skb->protocol = htons(ETH_P_IP);
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.
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))
263 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
265 csum_replace2(&hdr4->check,
266 htons(hdr4->ttl << 8),
267 htons(new_ttl << 8));
273 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
274 skb->protocol = htons(ETH_P_IPV6);
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.
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;
290 /* Should have decided which protocol it is by now */
297 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
298 struct packet_type *pt, struct net_device *orig_dev)
300 struct net *net = dev_net(dev);
301 struct mpls_shim_hdr *hdr;
302 struct mpls_route *rt;
304 struct mpls_entry_decoded dec;
305 struct net_device *out_dev;
306 struct mpls_dev *out_mdev;
307 struct mpls_dev *mdev;
309 unsigned int new_header_size;
313 /* Careful this entire function runs inside of an rcu critical section */
315 mdev = mpls_dev_get(dev);
319 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
322 if (!mdev->input_enabled) {
323 MPLS_INC_STATS(mdev, rx_dropped);
327 if (skb->pkt_type != PACKET_HOST)
330 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
333 if (!pskb_may_pull(skb, sizeof(*hdr)))
336 /* Read and decode the label */
338 dec = mpls_entry_decode(hdr);
340 rt = mpls_route_input_rcu(net, dec.label);
342 MPLS_INC_STATS(mdev, rx_noroute);
346 nh = mpls_select_multipath(rt, skb);
351 skb_pull(skb, sizeof(*hdr));
352 skb_reset_network_header(skb);
356 if (skb_warn_if_lro(skb))
359 skb_forward_csum(skb);
361 /* Verify ttl is valid */
366 /* Find the output device */
367 out_dev = rcu_dereference(nh->nh_dev);
368 if (!mpls_output_possible(out_dev))
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))
377 hh_len = LL_RESERVED_SPACE(out_dev);
378 if (!out_dev->header_ops)
381 /* Ensure there is enough space for the headers in the skb */
382 if (skb_cow(skb, hh_len + new_header_size))
386 skb->protocol = htons(ETH_P_MPLS_UC);
388 if (unlikely(!new_header_size && dec.bos)) {
389 /* Penultimate hop popping */
390 if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
395 skb_push(skb, new_header_size);
396 skb_reset_network_header(skb);
397 /* Push the new labels */
400 for (i = nh->nh_labels - 1; i >= 0; i--) {
401 hdr[i] = mpls_entry_encode(nh->nh_label[i],
407 mpls_stats_inc_outucastpkts(out_dev, skb);
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);
414 err = neigh_xmit(nh->nh_via_table, out_dev,
415 mpls_nh_via(rt, nh), skb);
417 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
422 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
424 MPLS_INC_STATS(out_mdev, tx_errors);
427 MPLS_INC_STATS(mdev, rx_errors);
433 static struct packet_type mpls_packet_type __read_mostly = {
434 .type = cpu_to_be16(ETH_P_MPLS_UC),
435 .func = mpls_forward,
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 },
444 struct mpls_route_config {
449 u8 rc_via[MAX_VIA_ALEN];
453 u32 rc_output_label[MAX_NEW_LABELS];
455 enum mpls_payload_type rc_payload_type;
456 struct nl_info rc_nlinfo;
457 struct rtnexthop *rc_mp;
461 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
463 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
464 struct mpls_route *rt;
466 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
468 num_nh * max_alen_aligned,
472 rt->rt_nhn_alive = num_nh;
473 rt->rt_max_alen = max_alen_aligned;
479 static void mpls_rt_free(struct mpls_route *rt)
482 kfree_rcu(rt, rt_rcu);
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)
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);
499 static void mpls_route_update(struct net *net, unsigned index,
500 struct mpls_route *new,
501 const struct nl_info *info)
503 struct mpls_route __rcu **platform_label;
504 struct mpls_route *rt;
508 platform_label = rtnl_dereference(net->mpls.platform_label);
509 rt = rtnl_dereference(platform_label[index]);
510 rcu_assign_pointer(platform_label[index], new);
512 mpls_notify_route(net, index, rt, new, info);
514 /* If we removed a route free it now */
518 static unsigned find_free_label(struct net *net)
520 struct mpls_route __rcu **platform_label;
521 size_t platform_labels;
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;
528 if (!rtnl_dereference(platform_label[index]))
531 return LABEL_NOT_SPECIFIED;
534 #if IS_ENABLED(CONFIG_INET)
535 static struct net_device *inet_fib_lookup_dev(struct net *net,
538 struct net_device *dev;
540 struct in_addr daddr;
542 memcpy(&daddr, addr, sizeof(struct in_addr));
543 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
555 static struct net_device *inet_fib_lookup_dev(struct net *net,
558 return ERR_PTR(-EAFNOSUPPORT);
562 #if IS_ENABLED(CONFIG_IPV6)
563 static struct net_device *inet6_fib_lookup_dev(struct net *net,
566 struct net_device *dev;
567 struct dst_entry *dst;
572 return ERR_PTR(-EAFNOSUPPORT);
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);
587 static struct net_device *inet6_fib_lookup_dev(struct net *net,
590 return ERR_PTR(-EAFNOSUPPORT);
594 static struct net_device *find_outdev(struct net *net,
595 struct mpls_route *rt,
596 struct mpls_nh *nh, int oif)
598 struct net_device *dev = NULL;
601 switch (nh->nh_via_table) {
602 case NEIGH_ARP_TABLE:
603 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
606 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
608 case NEIGH_LINK_TABLE:
612 dev = dev_get_by_index(net, oif);
616 return ERR_PTR(-ENODEV);
621 /* The caller is holding rtnl anyways, so release the dev reference */
627 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
628 struct mpls_nh *nh, int oif)
630 struct net_device *dev = NULL;
633 dev = find_outdev(net, rt, nh, oif);
640 /* Ensure this is a supported device */
642 if (!mpls_dev_get(dev))
645 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
646 (dev->addr_len != nh->nh_via_alen))
649 RCU_INIT_POINTER(nh->nh_dev, dev);
651 if (!(dev->flags & IFF_UP)) {
652 nh->nh_flags |= RTNH_F_DEAD;
656 flags = dev_get_flags(dev);
657 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
658 nh->nh_flags |= RTNH_F_LINKDOWN;
667 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
668 struct mpls_route *rt)
670 struct net *net = cfg->rc_nlinfo.nl_net;
671 struct mpls_nh *nh = rt->rt_nh;
679 /* Ensure only a supported number of labels are present */
680 if (cfg->rc_output_labels > MAX_NEW_LABELS)
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];
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;
691 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
695 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
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)
714 err = nla_get_labels(newdst, MAX_NEW_LABELS,
715 &nh->nh_labels, nh->nh_label);
721 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
722 __mpls_nh_via(rt, nh));
726 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
729 err = mpls_nh_assign_dev(net, rt, nh, oif);
739 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
740 u8 cfg_via_alen, u8 *max_via_alen)
746 *max_via_alen = cfg_via_alen;
752 while (rtnh_ok(rtnh, remaining)) {
753 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
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);
763 if (via_alen <= MAX_VIA_ALEN)
764 *max_via_alen = max_t(u16, *max_via_alen,
769 rtnh = rtnh_next(rtnh, &remaining);
772 /* leftover implies invalid nexthop configuration, discard it */
773 return remaining > 0 ? 0 : nhs;
776 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
777 struct mpls_route *rt)
779 struct rtnexthop *rtnh = cfg->rc_mp;
780 struct nlattr *nla_via, *nla_newdst;
781 int remaining = cfg->rc_mp_len;
785 change_nexthops(rt) {
792 if (!rtnh_ok(rtnh, remaining))
795 /* neither weighted multipath nor any flags
798 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
801 attrlen = rtnh_attrlen(rtnh);
803 struct nlattr *attrs = rtnh_attrs(rtnh);
805 nla_via = nla_find(attrs, attrlen, RTA_VIA);
806 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
809 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
810 rtnh->rtnh_ifindex, nla_via, nla_newdst);
814 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
817 rtnh = rtnh_next(rtnh, &remaining);
819 } endfor_nexthops(rt);
829 static int mpls_route_add(struct mpls_route_config *cfg)
831 struct mpls_route __rcu **platform_label;
832 struct net *net = cfg->rc_nlinfo.nl_net;
833 struct mpls_route *rt, *old;
839 index = cfg->rc_label;
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);
847 /* Reserved labels may not be set */
848 if (index < MPLS_LABEL_FIRST_UNRESERVED)
851 /* The full 20 bit range may not be supported. */
852 if (index >= net->mpls.platform_labels)
855 /* Append makes no sense with mpls */
857 if (cfg->rc_nlflags & NLM_F_APPEND)
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)
867 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
871 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
875 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
876 cfg->rc_via_alen, &max_via_alen);
881 rt = mpls_rt_alloc(nhs, max_via_alen);
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;
890 err = mpls_nh_build_multi(cfg, rt);
892 err = mpls_nh_build_from_cfg(cfg, rt);
896 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
906 static int mpls_route_del(struct mpls_route_config *cfg)
908 struct net *net = cfg->rc_nlinfo.nl_net;
912 index = cfg->rc_label;
914 /* Reserved labels may not be removed */
915 if (index < MPLS_LABEL_FIRST_UNRESERVED)
918 /* The full 20 bit range may not be supported */
919 if (index >= net->mpls.platform_labels)
922 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
929 static void mpls_get_stats(struct mpls_dev *mdev,
930 struct mpls_link_stats *stats)
932 struct mpls_pcpu_stats *p;
935 memset(stats, 0, sizeof(*stats));
937 for_each_possible_cpu(i) {
938 struct mpls_link_stats local;
941 p = per_cpu_ptr(mdev->stats, i);
943 start = u64_stats_fetch_begin(&p->syncp);
945 } while (u64_stats_fetch_retry(&p->syncp, start));
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;
959 static int mpls_fill_stats_af(struct sk_buff *skb,
960 const struct net_device *dev)
962 struct mpls_link_stats *stats;
963 struct mpls_dev *mdev;
966 mdev = mpls_dev_get(dev);
970 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
971 sizeof(struct mpls_link_stats),
976 stats = nla_data(nla);
977 mpls_get_stats(mdev, stats);
982 static size_t mpls_get_stats_af_size(const struct net_device *dev)
984 struct mpls_dev *mdev;
986 mdev = mpls_dev_get(dev);
990 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
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)
997 struct nlmsghdr *nlh;
998 struct netconfmsg *ncm;
1001 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1006 if (type == NETCONFA_ALL)
1009 ncm = nlmsg_data(nlh);
1010 ncm->ncm_family = AF_MPLS;
1012 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1013 goto nla_put_failure;
1015 if ((all || type == NETCONFA_INPUT) &&
1016 nla_put_s32(skb, NETCONFA_INPUT,
1017 mdev->input_enabled) < 0)
1018 goto nla_put_failure;
1020 nlmsg_end(skb, nlh);
1024 nlmsg_cancel(skb, nlh);
1028 static int mpls_netconf_msgsize_devconf(int type)
1030 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1031 + nla_total_size(4); /* NETCONFA_IFINDEX */
1034 if (type == NETCONFA_ALL)
1037 if (all || type == NETCONFA_INPUT)
1038 size += nla_total_size(4);
1043 static void mpls_netconf_notify_devconf(struct net *net, int type,
1044 struct mpls_dev *mdev)
1046 struct sk_buff *skb;
1049 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1053 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, RTM_NEWNETCONF,
1056 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1057 WARN_ON(err == -EMSGSIZE);
1062 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1066 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1069 static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1070 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1073 static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1074 struct nlmsghdr *nlh)
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;
1085 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
1086 devconf_mpls_policy);
1091 if (!tb[NETCONFA_IFINDEX])
1094 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1095 dev = __dev_get_by_index(net, ifindex);
1099 mdev = mpls_dev_get(dev);
1104 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1108 err = mpls_netconf_fill_devconf(skb, mdev,
1109 NETLINK_CB(in_skb).portid,
1110 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1113 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1114 WARN_ON(err == -EMSGSIZE);
1118 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1123 static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1124 struct netlink_callback *cb)
1126 struct net *net = sock_net(skb->sk);
1127 struct hlist_head *head;
1128 struct net_device *dev;
1129 struct mpls_dev *mdev;
1134 s_idx = idx = cb->args[1];
1136 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1138 head = &net->dev_index_head[h];
1140 cb->seq = net->dev_base_seq;
1141 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1144 mdev = mpls_dev_get(dev);
1147 if (mpls_netconf_fill_devconf(skb, mdev,
1148 NETLINK_CB(cb->skb).portid,
1152 NETCONFA_ALL) < 0) {
1156 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1169 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1170 (&((struct mpls_dev *)0)->field)
1172 static int mpls_conf_proc(struct ctl_table *ctl, int write,
1173 void __user *buffer,
1174 size_t *lenp, loff_t *ppos)
1176 int oval = *(int *)ctl->data;
1177 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
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;
1185 if (i == offsetof(struct mpls_dev, input_enabled) &&
1187 mpls_netconf_notify_devconf(net,
1196 static const struct ctl_table mpls_dev_table[] = {
1198 .procname = "input",
1199 .maxlen = sizeof(int),
1201 .proc_handler = mpls_conf_proc,
1202 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1207 static int mpls_dev_sysctl_register(struct net_device *dev,
1208 struct mpls_dev *mdev)
1210 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
1211 struct net *net = dev_net(dev);
1212 struct ctl_table *table;
1215 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1219 /* Table data contains only offsets relative to the base of
1220 * the mdev at this point, so make them absolute.
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;
1228 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1230 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
1242 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
1244 struct ctl_table *table;
1246 table = mdev->sysctl->ctl_table_arg;
1247 unregister_net_sysctl_table(mdev->sysctl);
1251 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1253 struct mpls_dev *mdev;
1259 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1261 return ERR_PTR(err);
1263 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1267 for_each_possible_cpu(i) {
1268 struct mpls_pcpu_stats *mpls_stats;
1270 mpls_stats = per_cpu_ptr(mdev->stats, i);
1271 u64_stats_init(&mpls_stats->syncp);
1274 err = mpls_dev_sysctl_register(dev, mdev);
1279 rcu_assign_pointer(dev->mpls_ptr, mdev);
1284 free_percpu(mdev->stats);
1286 return ERR_PTR(err);
1289 static void mpls_dev_destroy_rcu(struct rcu_head *head)
1291 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1293 free_percpu(mdev->stats);
1297 static void mpls_ifdown(struct net_device *dev, int event)
1299 struct mpls_route __rcu **platform_label;
1300 struct net *net = dev_net(dev);
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]);
1310 change_nexthops(rt) {
1311 if (rtnl_dereference(nh->nh_dev) != dev)
1315 case NETDEV_UNREGISTER:
1316 nh->nh_flags |= RTNH_F_DEAD;
1319 nh->nh_flags |= RTNH_F_LINKDOWN;
1320 if (event != NETDEV_UNREGISTER)
1321 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
1324 if (event == NETDEV_UNREGISTER)
1325 RCU_INIT_POINTER(nh->nh_dev, NULL);
1326 } endfor_nexthops(rt);
1330 static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
1332 struct mpls_route __rcu **platform_label;
1333 struct net *net = dev_net(dev);
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]);
1345 change_nexthops(rt) {
1346 struct net_device *nh_dev =
1347 rtnl_dereference(nh->nh_dev);
1349 if (!(nh->nh_flags & nh_flags)) {
1356 nh->nh_flags &= ~nh_flags;
1357 } endfor_nexthops(rt);
1359 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1363 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1366 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1367 struct mpls_dev *mdev;
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);
1379 return notifier_from_errno(PTR_ERR(mdev));
1384 mdev = mpls_dev_get(dev);
1390 mpls_ifdown(dev, event);
1393 flags = dev_get_flags(dev);
1394 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1395 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1397 mpls_ifup(dev, RTNH_F_DEAD);
1400 flags = dev_get_flags(dev);
1401 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1402 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1404 mpls_ifdown(dev, event);
1406 case NETDEV_UNREGISTER:
1407 mpls_ifdown(dev, event);
1408 mdev = mpls_dev_get(dev);
1410 mpls_dev_sysctl_unregister(mdev);
1411 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1412 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
1415 case NETDEV_CHANGENAME:
1416 mdev = mpls_dev_get(dev);
1420 mpls_dev_sysctl_unregister(mdev);
1421 err = mpls_dev_sysctl_register(dev, mdev);
1423 return notifier_from_errno(err);
1430 static struct notifier_block mpls_dev_notifier = {
1431 .notifier_call = mpls_dev_notify,
1434 static int nla_put_via(struct sk_buff *skb,
1435 u8 table, const void *addr, int alen)
1437 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1438 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1442 int family = AF_UNSPEC;
1444 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1448 if (table <= NEIGH_NR_TABLES)
1449 family = table_to_family[table];
1451 via = nla_data(nla);
1452 via->rtvia_family = family;
1453 memcpy(via->rtvia_addr, addr, alen);
1457 int nla_put_labels(struct sk_buff *skb, int attrtype,
1458 u8 labels, const u32 label[])
1461 struct mpls_shim_hdr *nla_label;
1464 nla = nla_reserve(skb, attrtype, labels*4);
1468 nla_label = nla_data(nla);
1470 for (i = labels - 1; i >= 0; i--) {
1471 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1477 EXPORT_SYMBOL_GPL(nla_put_labels);
1479 int nla_get_labels(const struct nlattr *nla,
1480 u32 max_labels, u8 *labels, u32 label[])
1482 unsigned len = nla_len(nla);
1483 unsigned nla_labels;
1484 struct mpls_shim_hdr *nla_label;
1488 /* len needs to be an even multiple of 4 (the label size) */
1492 /* Limit the number of new labels allowed */
1494 if (nla_labels > max_labels)
1497 nla_label = nla_data(nla);
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);
1503 /* Ensure the bottom of stack flag is properly set
1504 * and ttl and tc are both clear.
1506 if ((dec.bos != bos) || dec.ttl || dec.tc)
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.
1518 label[i] = dec.label;
1520 *labels = nla_labels;
1523 EXPORT_SYMBOL_GPL(nla_get_labels);
1525 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1526 u8 *via_table, u8 via_addr[])
1528 struct rtvia *via = nla_data(nla);
1532 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1534 alen = nla_len(nla) -
1535 offsetof(struct rtvia, rtvia_addr);
1536 if (alen > MAX_VIA_ALEN)
1539 /* Validate the address family */
1540 switch (via->rtvia_family) {
1542 *via_table = NEIGH_LINK_TABLE;
1545 *via_table = NEIGH_ARP_TABLE;
1550 *via_table = NEIGH_ND_TABLE;
1555 /* Unsupported address family */
1559 memcpy(via_addr, via->rtvia_addr, alen);
1567 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1568 struct mpls_route_config *cfg)
1571 struct nlattr *tb[RTA_MAX+1];
1575 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1580 rtm = nlmsg_data(nlh);
1581 memset(cfg, 0, sizeof(*cfg));
1583 if (rtm->rtm_family != AF_MPLS)
1585 if (rtm->rtm_dst_len != 20)
1587 if (rtm->rtm_src_len != 0)
1589 if (rtm->rtm_tos != 0)
1591 if (rtm->rtm_table != RT_TABLE_MAIN)
1593 /* Any value is acceptable for rtm_protocol */
1595 /* As mpls uses destination specific addresses
1596 * (or source specific address in the case of multicast)
1597 * all addresses have universal scope.
1599 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1601 if (rtm->rtm_type != RTN_UNICAST)
1603 if (rtm->rtm_flags != 0)
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);
1615 for (index = 0; index <= RTA_MAX; index++) {
1616 struct nlattr *nla = tb[index];
1622 cfg->rc_ifindex = nla_get_u32(nla);
1625 if (nla_get_labels(nla, MAX_NEW_LABELS,
1626 &cfg->rc_output_labels,
1627 cfg->rc_output_label))
1633 if (nla_get_labels(nla, 1, &label_count,
1637 /* Reserved labels may not be set */
1638 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1645 if (nla_get_via(nla, &cfg->rc_via_alen,
1646 &cfg->rc_via_table, cfg->rc_via))
1652 cfg->rc_mp = nla_data(nla);
1653 cfg->rc_mp_len = nla_len(nla);
1656 case RTA_TTL_PROPAGATE:
1658 u8 ttl_propagate = nla_get_u8(nla);
1660 if (ttl_propagate > 1)
1662 cfg->rc_ttl_propagate = ttl_propagate ?
1663 MPLS_TTL_PROP_ENABLED :
1664 MPLS_TTL_PROP_DISABLED;
1668 /* Unsupported attribute */
1678 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1680 struct mpls_route_config cfg;
1683 err = rtm_to_route_config(skb, nlh, &cfg);
1687 return mpls_route_del(&cfg);
1691 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1693 struct mpls_route_config cfg;
1696 err = rtm_to_route_config(skb, nlh, &cfg);
1700 return mpls_route_add(&cfg);
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)
1706 struct net_device *dev;
1707 struct nlmsghdr *nlh;
1710 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1714 rtm = nlmsg_data(nlh);
1715 rtm->rtm_family = AF_MPLS;
1716 rtm->rtm_dst_len = 20;
1717 rtm->rtm_src_len = 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;
1725 if (nla_put_labels(skb, RTA_DST, 1, &label))
1726 goto nla_put_failure;
1728 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
1729 bool ttl_propagate =
1730 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
1732 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
1734 goto nla_put_failure;
1736 if (rt->rt_nhn == 1) {
1737 const struct mpls_nh *nh = rt->rt_nh;
1739 if (nh->nh_labels &&
1740 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
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),
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;
1755 struct rtnexthop *rtnh;
1760 mp = nla_nest_start(skb, RTA_MULTIPATH);
1762 goto nla_put_failure;
1765 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1767 goto nla_put_failure;
1769 dev = rtnl_dereference(nh->nh_dev);
1771 rtnh->rtnh_ifindex = dev->ifindex;
1772 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1773 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1776 if (nh->nh_flags & RTNH_F_DEAD) {
1777 rtnh->rtnh_flags |= RTNH_F_DEAD;
1781 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
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),
1789 goto nla_put_failure;
1791 /* length of rtnetlink header + attributes */
1792 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1793 } endfor_nexthops(rt);
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;
1800 nla_nest_end(skb, mp);
1803 nlmsg_end(skb, nlh);
1807 nlmsg_cancel(skb, nlh);
1811 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1813 struct net *net = sock_net(skb->sk);
1814 struct mpls_route __rcu **platform_label;
1815 size_t platform_labels;
1820 index = cb->args[0];
1821 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1822 index = MPLS_LABEL_FIRST_UNRESERVED;
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]);
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)
1837 cb->args[0] = index;
1842 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1845 NLMSG_ALIGN(sizeof(struct rtmsg))
1846 + nla_total_size(4) /* RTA_DST */
1847 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
1849 if (rt->rt_nhn == 1) {
1850 struct mpls_nh *nh = rt->rt_nh;
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);
1859 /* each nexthop is packed in an attribute */
1863 nhsize += nla_total_size(sizeof(struct rtnexthop));
1865 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1866 nhsize += nla_total_size(2 + nh->nh_via_alen);
1868 nhsize += nla_total_size(nh->nh_labels * 4);
1869 } endfor_nexthops(rt);
1870 /* nested attribute */
1871 payload += nla_total_size(nhsize);
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)
1881 struct sk_buff *skb;
1882 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1885 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1889 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1891 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1892 WARN_ON(err == -EMSGSIZE);
1896 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1901 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1904 static int resize_platform_label_table(struct net *net, size_t limit)
1906 size_t size = sizeof(struct mpls_route *) * limit;
1909 struct mpls_route __rcu **labels = NULL, **old;
1910 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1914 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1916 labels = vzalloc(size);
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);
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,
1937 if (limit > MPLS_LABEL_IPV6NULL) {
1938 struct net_device *lo = net->loopback_dev;
1939 rt2 = mpls_rt_alloc(1, lo->addr_len);
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,
1953 /* Remember the original table */
1954 old = rtnl_dereference(net->mpls.platform_label);
1955 old_limit = net->mpls.platform_labels;
1957 /* Free any labels beyond the new table */
1958 for (index = limit; index < old_limit; index++)
1959 mpls_route_update(net, index, NULL, NULL);
1961 /* Copy over the old labels */
1963 if (old_limit < limit)
1964 cp_size = old_limit * sizeof(struct mpls_route *);
1966 memcpy(labels, old, cp_size);
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);
1975 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1976 (limit > MPLS_LABEL_IPV4NULL)) {
1977 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1981 /* Update the global pointers */
1982 net->mpls.platform_labels = limit;
1983 rcu_assign_pointer(net->mpls.platform_label, labels);
2004 static int mpls_platform_labels(struct ctl_table *table, int write,
2005 void __user *buffer, size_t *lenp, loff_t *ppos)
2007 struct net *net = table->data;
2008 int platform_labels = net->mpls.platform_labels;
2010 struct ctl_table tmp = {
2011 .procname = table->procname,
2012 .data = &platform_labels,
2013 .maxlen = sizeof(int),
2014 .mode = table->mode,
2016 .extra2 = &label_limit,
2019 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2021 if (write && ret == 0)
2022 ret = resize_platform_label_table(net, platform_labels);
2027 #define MPLS_NS_SYSCTL_OFFSET(field) \
2028 (&((struct net *)0)->field)
2030 static const struct ctl_table mpls_table[] = {
2032 .procname = "platform_labels",
2034 .maxlen = sizeof(int),
2036 .proc_handler = mpls_platform_labels,
2039 .procname = "ip_ttl_propagate",
2040 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2041 .maxlen = sizeof(int),
2043 .proc_handler = proc_dointvec_minmax,
2048 .procname = "default_ttl",
2049 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2050 .maxlen = sizeof(int),
2052 .proc_handler = proc_dointvec_minmax,
2059 static int mpls_net_init(struct net *net)
2061 struct ctl_table *table;
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;
2069 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2073 /* Table data contains only offsets relative to the base of
2074 * the mdev at this point, so make them absolute.
2076 for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
2077 table[i].data = (char *)net + (uintptr_t)table[i].data;
2079 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
2080 if (net->mpls.ctl == NULL) {
2088 static void mpls_net_exit(struct net *net)
2090 struct mpls_route __rcu **platform_label;
2091 size_t platform_labels;
2092 struct ctl_table *table;
2095 table = net->mpls.ctl->ctl_table_arg;
2096 unregister_net_sysctl_table(net->mpls.ctl);
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.
2105 * As such no additional rcu synchronization is necessary when
2106 * freeing the platform_label table.
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);
2119 kvfree(platform_label);
2122 static struct pernet_operations mpls_net_ops = {
2123 .init = mpls_net_init,
2124 .exit = mpls_net_exit,
2127 static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2129 .fill_stats_af = mpls_fill_stats_af,
2130 .get_stats_af_size = mpls_get_stats_af_size,
2133 static int __init mpls_init(void)
2137 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2139 err = register_pernet_subsys(&mpls_net_ops);
2143 err = register_netdevice_notifier(&mpls_dev_notifier);
2145 goto out_unregister_pernet;
2147 dev_add_pack(&mpls_packet_type);
2149 rtnl_af_register(&mpls_af_ops);
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);
2160 out_unregister_pernet:
2161 unregister_pernet_subsys(&mpls_net_ops);
2164 module_init(mpls_init);
2166 static void __exit mpls_exit(void)
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);
2174 module_exit(mpls_exit);
2176 MODULE_DESCRIPTION("MultiProtocol Label Switching");
2177 MODULE_LICENSE("GPL v2");
2178 MODULE_ALIAS_NETPROTO(PF_MPLS);