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/vmalloc.h>
11 #include <linux/percpu.h>
16 #include <net/ip_fib.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #if IS_ENABLED(CONFIG_IPV6)
22 #include <net/addrconf.h>
23 #include <net/nexthop.h>
26 /* Maximum number of labels to look ahead at when selecting a path of
29 #define MAX_MP_SELECT_LABELS 4
31 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
34 static int label_limit = (1 << 20) - 1;
36 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
37 struct nlmsghdr *nlh, struct net *net, u32 portid,
38 unsigned int nlm_flags);
40 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
42 struct mpls_route *rt = NULL;
44 if (index < net->mpls.platform_labels) {
45 struct mpls_route __rcu **platform_label =
46 rcu_dereference(net->mpls.platform_label);
47 rt = rcu_dereference(platform_label[index]);
52 bool mpls_output_possible(const struct net_device *dev)
54 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
56 EXPORT_SYMBOL_GPL(mpls_output_possible);
58 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
60 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
61 int nh_index = nh - rt->rt_nh;
63 return nh0_via + rt->rt_max_alen * nh_index;
66 static const u8 *mpls_nh_via(const struct mpls_route *rt,
67 const struct mpls_nh *nh)
69 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
72 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
74 /* The size of the layer 2.5 labels to be added for this route */
75 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
78 unsigned int mpls_dev_mtu(const struct net_device *dev)
80 /* The amount of data the layer 2 frame can hold */
83 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
85 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
90 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
95 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
97 void mpls_stats_inc_outucastpkts(struct net_device *dev,
98 const struct sk_buff *skb)
100 struct mpls_dev *mdev;
102 if (skb->protocol == htons(ETH_P_MPLS_UC)) {
103 mdev = mpls_dev_get(dev);
105 MPLS_INC_STATS_LEN(mdev, skb->len,
108 } else if (skb->protocol == htons(ETH_P_IP)) {
109 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
110 #if IS_ENABLED(CONFIG_IPV6)
111 } else if (skb->protocol == htons(ETH_P_IPV6)) {
112 struct inet6_dev *in6dev = __in6_dev_get(dev);
115 IP6_UPD_PO_STATS(dev_net(dev), in6dev,
116 IPSTATS_MIB_OUT, skb->len);
120 EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
122 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
124 struct mpls_entry_decoded dec;
125 unsigned int mpls_hdr_len = 0;
126 struct mpls_shim_hdr *hdr;
127 bool eli_seen = false;
131 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
133 mpls_hdr_len += sizeof(*hdr);
134 if (!pskb_may_pull(skb, mpls_hdr_len))
137 /* Read and decode the current label */
138 hdr = mpls_hdr(skb) + label_index;
139 dec = mpls_entry_decode(hdr);
141 /* RFC6790 - reserved labels MUST NOT be used as keys
142 * for the load-balancing function
144 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
145 hash = jhash_1word(dec.label, hash);
147 /* The entropy label follows the entropy label
148 * indicator, so this means that the entropy
149 * label was just added to the hash - no need to
150 * go any deeper either in the label stack or in the
155 } else if (dec.label == MPLS_LABEL_ENTROPY) {
162 /* found bottom label; does skb have room for a header? */
163 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
164 const struct iphdr *v4hdr;
166 v4hdr = (const struct iphdr *)(hdr + 1);
167 if (v4hdr->version == 4) {
168 hash = jhash_3words(ntohl(v4hdr->saddr),
170 v4hdr->protocol, hash);
171 } else if (v4hdr->version == 6 &&
172 pskb_may_pull(skb, mpls_hdr_len +
173 sizeof(struct ipv6hdr))) {
174 const struct ipv6hdr *v6hdr;
176 v6hdr = (const struct ipv6hdr *)(hdr + 1);
177 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
178 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
179 hash = jhash_1word(v6hdr->nexthdr, hash);
189 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
192 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
197 /* No need to look further into packet if there's only
206 hash = mpls_multipath_hash(rt, skb);
207 nh_index = hash % alive;
208 if (alive == rt->rt_nhn)
211 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
216 } endfor_nexthops(rt);
219 return &rt->rt_nh[nh_index];
222 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
223 struct mpls_entry_decoded dec)
225 enum mpls_payload_type payload_type;
226 bool success = false;
228 /* The IPv4 code below accesses through the IPv4 header
229 * checksum, which is 12 bytes into the packet.
230 * The IPv6 code below accesses through the IPv6 hop limit
231 * which is 8 bytes into the packet.
233 * For all supported cases there should always be at least 12
234 * bytes of packet data present. The IPv4 header is 20 bytes
235 * without options and the IPv6 header is always 40 bytes
238 if (!pskb_may_pull(skb, 12))
241 payload_type = rt->rt_payload_type;
242 if (payload_type == MPT_UNSPEC)
243 payload_type = ip_hdr(skb)->version;
245 switch (payload_type) {
247 struct iphdr *hdr4 = ip_hdr(skb);
248 skb->protocol = htons(ETH_P_IP);
249 csum_replace2(&hdr4->check,
250 htons(hdr4->ttl << 8),
251 htons(dec.ttl << 8));
257 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
258 skb->protocol = htons(ETH_P_IPV6);
259 hdr6->hop_limit = dec.ttl;
270 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
271 struct packet_type *pt, struct net_device *orig_dev)
273 struct net *net = dev_net(dev);
274 struct mpls_shim_hdr *hdr;
275 struct mpls_route *rt;
277 struct mpls_entry_decoded dec;
278 struct net_device *out_dev;
279 struct mpls_dev *out_mdev;
280 struct mpls_dev *mdev;
282 unsigned int new_header_size;
286 /* Careful this entire function runs inside of an rcu critical section */
288 mdev = mpls_dev_get(dev);
292 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
295 if (!mdev->input_enabled) {
296 MPLS_INC_STATS(mdev, rx_dropped);
300 if (skb->pkt_type != PACKET_HOST)
303 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
306 if (!pskb_may_pull(skb, sizeof(*hdr)))
309 /* Read and decode the label */
311 dec = mpls_entry_decode(hdr);
313 rt = mpls_route_input_rcu(net, dec.label);
315 MPLS_INC_STATS(mdev, rx_noroute);
319 nh = mpls_select_multipath(rt, skb);
324 skb_pull(skb, sizeof(*hdr));
325 skb_reset_network_header(skb);
329 if (skb_warn_if_lro(skb))
332 skb_forward_csum(skb);
334 /* Verify ttl is valid */
339 /* Find the output device */
340 out_dev = rcu_dereference(nh->nh_dev);
341 if (!mpls_output_possible(out_dev))
344 /* Verify the destination can hold the packet */
345 new_header_size = mpls_nh_header_size(nh);
346 mtu = mpls_dev_mtu(out_dev);
347 if (mpls_pkt_too_big(skb, mtu - new_header_size))
350 hh_len = LL_RESERVED_SPACE(out_dev);
351 if (!out_dev->header_ops)
354 /* Ensure there is enough space for the headers in the skb */
355 if (skb_cow(skb, hh_len + new_header_size))
359 skb->protocol = htons(ETH_P_MPLS_UC);
361 if (unlikely(!new_header_size && dec.bos)) {
362 /* Penultimate hop popping */
363 if (!mpls_egress(rt, skb, dec))
368 skb_push(skb, new_header_size);
369 skb_reset_network_header(skb);
370 /* Push the new labels */
373 for (i = nh->nh_labels - 1; i >= 0; i--) {
374 hdr[i] = mpls_entry_encode(nh->nh_label[i],
380 mpls_stats_inc_outucastpkts(out_dev, skb);
382 /* If via wasn't specified then send out using device address */
383 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
384 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
385 out_dev->dev_addr, skb);
387 err = neigh_xmit(nh->nh_via_table, out_dev,
388 mpls_nh_via(rt, nh), skb);
390 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
395 out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
397 MPLS_INC_STATS(out_mdev, tx_errors);
400 MPLS_INC_STATS(mdev, rx_errors);
406 static struct packet_type mpls_packet_type __read_mostly = {
407 .type = cpu_to_be16(ETH_P_MPLS_UC),
408 .func = mpls_forward,
411 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
412 [RTA_DST] = { .type = NLA_U32 },
413 [RTA_OIF] = { .type = NLA_U32 },
416 struct mpls_route_config {
421 u8 rc_via[MAX_VIA_ALEN];
424 u32 rc_output_label[MAX_NEW_LABELS];
426 enum mpls_payload_type rc_payload_type;
427 struct nl_info rc_nlinfo;
428 struct rtnexthop *rc_mp;
432 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
434 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
435 struct mpls_route *rt;
437 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
439 num_nh * max_alen_aligned,
443 rt->rt_nhn_alive = num_nh;
444 rt->rt_max_alen = max_alen_aligned;
450 static void mpls_rt_free(struct mpls_route *rt)
453 kfree_rcu(rt, rt_rcu);
456 static void mpls_notify_route(struct net *net, unsigned index,
457 struct mpls_route *old, struct mpls_route *new,
458 const struct nl_info *info)
460 struct nlmsghdr *nlh = info ? info->nlh : NULL;
461 unsigned portid = info ? info->portid : 0;
462 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
463 struct mpls_route *rt = new ? new : old;
464 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
465 /* Ignore reserved labels for now */
466 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
467 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
470 static void mpls_route_update(struct net *net, unsigned index,
471 struct mpls_route *new,
472 const struct nl_info *info)
474 struct mpls_route __rcu **platform_label;
475 struct mpls_route *rt;
479 platform_label = rtnl_dereference(net->mpls.platform_label);
480 rt = rtnl_dereference(platform_label[index]);
481 rcu_assign_pointer(platform_label[index], new);
483 mpls_notify_route(net, index, rt, new, info);
485 /* If we removed a route free it now */
489 static unsigned find_free_label(struct net *net)
491 struct mpls_route __rcu **platform_label;
492 size_t platform_labels;
495 platform_label = rtnl_dereference(net->mpls.platform_label);
496 platform_labels = net->mpls.platform_labels;
497 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
499 if (!rtnl_dereference(platform_label[index]))
502 return LABEL_NOT_SPECIFIED;
505 #if IS_ENABLED(CONFIG_INET)
506 static struct net_device *inet_fib_lookup_dev(struct net *net,
509 struct net_device *dev;
511 struct in_addr daddr;
513 memcpy(&daddr, addr, sizeof(struct in_addr));
514 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
526 static struct net_device *inet_fib_lookup_dev(struct net *net,
529 return ERR_PTR(-EAFNOSUPPORT);
533 #if IS_ENABLED(CONFIG_IPV6)
534 static struct net_device *inet6_fib_lookup_dev(struct net *net,
537 struct net_device *dev;
538 struct dst_entry *dst;
543 return ERR_PTR(-EAFNOSUPPORT);
545 memset(&fl6, 0, sizeof(fl6));
546 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
547 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
558 static struct net_device *inet6_fib_lookup_dev(struct net *net,
561 return ERR_PTR(-EAFNOSUPPORT);
565 static struct net_device *find_outdev(struct net *net,
566 struct mpls_route *rt,
567 struct mpls_nh *nh, int oif)
569 struct net_device *dev = NULL;
572 switch (nh->nh_via_table) {
573 case NEIGH_ARP_TABLE:
574 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
577 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
579 case NEIGH_LINK_TABLE:
583 dev = dev_get_by_index(net, oif);
587 return ERR_PTR(-ENODEV);
592 /* The caller is holding rtnl anyways, so release the dev reference */
598 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
599 struct mpls_nh *nh, int oif)
601 struct net_device *dev = NULL;
604 dev = find_outdev(net, rt, nh, oif);
611 /* Ensure this is a supported device */
613 if (!mpls_dev_get(dev))
616 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
617 (dev->addr_len != nh->nh_via_alen))
620 RCU_INIT_POINTER(nh->nh_dev, dev);
622 if (!(dev->flags & IFF_UP)) {
623 nh->nh_flags |= RTNH_F_DEAD;
627 flags = dev_get_flags(dev);
628 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
629 nh->nh_flags |= RTNH_F_LINKDOWN;
638 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
639 struct mpls_route *rt)
641 struct net *net = cfg->rc_nlinfo.nl_net;
642 struct mpls_nh *nh = rt->rt_nh;
650 /* Ensure only a supported number of labels are present */
651 if (cfg->rc_output_labels > MAX_NEW_LABELS)
654 nh->nh_labels = cfg->rc_output_labels;
655 for (i = 0; i < nh->nh_labels; i++)
656 nh->nh_label[i] = cfg->rc_output_label[i];
658 nh->nh_via_table = cfg->rc_via_table;
659 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
660 nh->nh_via_alen = cfg->rc_via_alen;
662 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
666 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
675 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
676 struct mpls_nh *nh, int oif, struct nlattr *via,
677 struct nlattr *newdst)
685 err = nla_get_labels(newdst, MAX_NEW_LABELS,
686 &nh->nh_labels, nh->nh_label);
692 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
693 __mpls_nh_via(rt, nh));
697 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
700 err = mpls_nh_assign_dev(net, rt, nh, oif);
710 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
711 u8 cfg_via_alen, u8 *max_via_alen)
717 *max_via_alen = cfg_via_alen;
723 while (rtnh_ok(rtnh, remaining)) {
724 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
727 attrlen = rtnh_attrlen(rtnh);
728 nla = nla_find(attrs, attrlen, RTA_VIA);
729 if (nla && nla_len(nla) >=
730 offsetof(struct rtvia, rtvia_addr)) {
731 int via_alen = nla_len(nla) -
732 offsetof(struct rtvia, rtvia_addr);
734 if (via_alen <= MAX_VIA_ALEN)
735 *max_via_alen = max_t(u16, *max_via_alen,
740 rtnh = rtnh_next(rtnh, &remaining);
743 /* leftover implies invalid nexthop configuration, discard it */
744 return remaining > 0 ? 0 : nhs;
747 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
748 struct mpls_route *rt)
750 struct rtnexthop *rtnh = cfg->rc_mp;
751 struct nlattr *nla_via, *nla_newdst;
752 int remaining = cfg->rc_mp_len;
756 change_nexthops(rt) {
763 if (!rtnh_ok(rtnh, remaining))
766 /* neither weighted multipath nor any flags
769 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
772 attrlen = rtnh_attrlen(rtnh);
774 struct nlattr *attrs = rtnh_attrs(rtnh);
776 nla_via = nla_find(attrs, attrlen, RTA_VIA);
777 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
780 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
781 rtnh->rtnh_ifindex, nla_via, nla_newdst);
785 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
788 rtnh = rtnh_next(rtnh, &remaining);
790 } endfor_nexthops(rt);
800 static int mpls_route_add(struct mpls_route_config *cfg)
802 struct mpls_route __rcu **platform_label;
803 struct net *net = cfg->rc_nlinfo.nl_net;
804 struct mpls_route *rt, *old;
810 index = cfg->rc_label;
812 /* If a label was not specified during insert pick one */
813 if ((index == LABEL_NOT_SPECIFIED) &&
814 (cfg->rc_nlflags & NLM_F_CREATE)) {
815 index = find_free_label(net);
818 /* Reserved labels may not be set */
819 if (index < MPLS_LABEL_FIRST_UNRESERVED)
822 /* The full 20 bit range may not be supported. */
823 if (index >= net->mpls.platform_labels)
826 /* Append makes no sense with mpls */
828 if (cfg->rc_nlflags & NLM_F_APPEND)
832 platform_label = rtnl_dereference(net->mpls.platform_label);
833 old = rtnl_dereference(platform_label[index]);
834 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
838 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
842 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
846 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
847 cfg->rc_via_alen, &max_via_alen);
852 rt = mpls_rt_alloc(nhs, max_via_alen);
856 rt->rt_protocol = cfg->rc_protocol;
857 rt->rt_payload_type = cfg->rc_payload_type;
860 err = mpls_nh_build_multi(cfg, rt);
862 err = mpls_nh_build_from_cfg(cfg, rt);
866 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
876 static int mpls_route_del(struct mpls_route_config *cfg)
878 struct net *net = cfg->rc_nlinfo.nl_net;
882 index = cfg->rc_label;
884 /* Reserved labels may not be removed */
885 if (index < MPLS_LABEL_FIRST_UNRESERVED)
888 /* The full 20 bit range may not be supported */
889 if (index >= net->mpls.platform_labels)
892 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
899 static void mpls_get_stats(struct mpls_dev *mdev,
900 struct mpls_link_stats *stats)
902 struct mpls_pcpu_stats *p;
905 memset(stats, 0, sizeof(*stats));
907 for_each_possible_cpu(i) {
908 struct mpls_link_stats local;
911 p = per_cpu_ptr(mdev->stats, i);
913 start = u64_stats_fetch_begin(&p->syncp);
915 } while (u64_stats_fetch_retry(&p->syncp, start));
917 stats->rx_packets += local.rx_packets;
918 stats->rx_bytes += local.rx_bytes;
919 stats->tx_packets += local.tx_packets;
920 stats->tx_bytes += local.tx_bytes;
921 stats->rx_errors += local.rx_errors;
922 stats->tx_errors += local.tx_errors;
923 stats->rx_dropped += local.rx_dropped;
924 stats->tx_dropped += local.tx_dropped;
925 stats->rx_noroute += local.rx_noroute;
929 static int mpls_fill_stats_af(struct sk_buff *skb,
930 const struct net_device *dev)
932 struct mpls_link_stats *stats;
933 struct mpls_dev *mdev;
936 mdev = mpls_dev_get(dev);
940 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
941 sizeof(struct mpls_link_stats),
946 stats = nla_data(nla);
947 mpls_get_stats(mdev, stats);
952 static size_t mpls_get_stats_af_size(const struct net_device *dev)
954 struct mpls_dev *mdev;
956 mdev = mpls_dev_get(dev);
960 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
963 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
964 (&((struct mpls_dev *)0)->field)
966 static const struct ctl_table mpls_dev_table[] = {
969 .maxlen = sizeof(int),
971 .proc_handler = proc_dointvec,
972 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
977 static int mpls_dev_sysctl_register(struct net_device *dev,
978 struct mpls_dev *mdev)
980 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
981 struct ctl_table *table;
984 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
988 /* Table data contains only offsets relative to the base of
989 * the mdev at this point, so make them absolute.
991 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
992 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
994 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
996 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
1008 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
1010 struct ctl_table *table;
1012 table = mdev->sysctl->ctl_table_arg;
1013 unregister_net_sysctl_table(mdev->sysctl);
1017 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1019 struct mpls_dev *mdev;
1025 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1027 return ERR_PTR(err);
1029 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1033 for_each_possible_cpu(i) {
1034 struct mpls_pcpu_stats *mpls_stats;
1036 mpls_stats = per_cpu_ptr(mdev->stats, i);
1037 u64_stats_init(&mpls_stats->syncp);
1040 err = mpls_dev_sysctl_register(dev, mdev);
1044 rcu_assign_pointer(dev->mpls_ptr, mdev);
1049 free_percpu(mdev->stats);
1051 return ERR_PTR(err);
1054 static void mpls_dev_destroy_rcu(struct rcu_head *head)
1056 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1058 free_percpu(mdev->stats);
1062 static void mpls_ifdown(struct net_device *dev, int event)
1064 struct mpls_route __rcu **platform_label;
1065 struct net *net = dev_net(dev);
1068 platform_label = rtnl_dereference(net->mpls.platform_label);
1069 for (index = 0; index < net->mpls.platform_labels; index++) {
1070 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1075 change_nexthops(rt) {
1076 if (rtnl_dereference(nh->nh_dev) != dev)
1080 case NETDEV_UNREGISTER:
1081 nh->nh_flags |= RTNH_F_DEAD;
1084 nh->nh_flags |= RTNH_F_LINKDOWN;
1085 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
1088 if (event == NETDEV_UNREGISTER)
1089 RCU_INIT_POINTER(nh->nh_dev, NULL);
1090 } endfor_nexthops(rt);
1094 static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
1096 struct mpls_route __rcu **platform_label;
1097 struct net *net = dev_net(dev);
1101 platform_label = rtnl_dereference(net->mpls.platform_label);
1102 for (index = 0; index < net->mpls.platform_labels; index++) {
1103 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1109 change_nexthops(rt) {
1110 struct net_device *nh_dev =
1111 rtnl_dereference(nh->nh_dev);
1113 if (!(nh->nh_flags & nh_flags)) {
1120 nh->nh_flags &= ~nh_flags;
1121 } endfor_nexthops(rt);
1123 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1127 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1130 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1131 struct mpls_dev *mdev;
1134 if (event == NETDEV_REGISTER) {
1135 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
1136 if (dev->type == ARPHRD_ETHER ||
1137 dev->type == ARPHRD_LOOPBACK ||
1138 dev->type == ARPHRD_IPGRE ||
1139 dev->type == ARPHRD_SIT ||
1140 dev->type == ARPHRD_TUNNEL) {
1141 mdev = mpls_add_dev(dev);
1143 return notifier_from_errno(PTR_ERR(mdev));
1148 mdev = mpls_dev_get(dev);
1154 mpls_ifdown(dev, event);
1157 flags = dev_get_flags(dev);
1158 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1159 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1161 mpls_ifup(dev, RTNH_F_DEAD);
1164 flags = dev_get_flags(dev);
1165 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1166 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1168 mpls_ifdown(dev, event);
1170 case NETDEV_UNREGISTER:
1171 mpls_ifdown(dev, event);
1172 mdev = mpls_dev_get(dev);
1174 mpls_dev_sysctl_unregister(mdev);
1175 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1176 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
1179 case NETDEV_CHANGENAME:
1180 mdev = mpls_dev_get(dev);
1184 mpls_dev_sysctl_unregister(mdev);
1185 err = mpls_dev_sysctl_register(dev, mdev);
1187 return notifier_from_errno(err);
1194 static struct notifier_block mpls_dev_notifier = {
1195 .notifier_call = mpls_dev_notify,
1198 static int nla_put_via(struct sk_buff *skb,
1199 u8 table, const void *addr, int alen)
1201 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1202 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1206 int family = AF_UNSPEC;
1208 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1212 if (table <= NEIGH_NR_TABLES)
1213 family = table_to_family[table];
1215 via = nla_data(nla);
1216 via->rtvia_family = family;
1217 memcpy(via->rtvia_addr, addr, alen);
1221 int nla_put_labels(struct sk_buff *skb, int attrtype,
1222 u8 labels, const u32 label[])
1225 struct mpls_shim_hdr *nla_label;
1228 nla = nla_reserve(skb, attrtype, labels*4);
1232 nla_label = nla_data(nla);
1234 for (i = labels - 1; i >= 0; i--) {
1235 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1241 EXPORT_SYMBOL_GPL(nla_put_labels);
1243 int nla_get_labels(const struct nlattr *nla,
1244 u32 max_labels, u8 *labels, u32 label[])
1246 unsigned len = nla_len(nla);
1247 unsigned nla_labels;
1248 struct mpls_shim_hdr *nla_label;
1252 /* len needs to be an even multiple of 4 (the label size) */
1256 /* Limit the number of new labels allowed */
1258 if (nla_labels > max_labels)
1261 nla_label = nla_data(nla);
1263 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1264 struct mpls_entry_decoded dec;
1265 dec = mpls_entry_decode(nla_label + i);
1267 /* Ensure the bottom of stack flag is properly set
1268 * and ttl and tc are both clear.
1270 if ((dec.bos != bos) || dec.ttl || dec.tc)
1273 switch (dec.label) {
1274 case MPLS_LABEL_IMPLNULL:
1275 /* RFC3032: This is a label that an LSR may
1276 * assign and distribute, but which never
1277 * actually appears in the encapsulation.
1282 label[i] = dec.label;
1284 *labels = nla_labels;
1287 EXPORT_SYMBOL_GPL(nla_get_labels);
1289 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1290 u8 *via_table, u8 via_addr[])
1292 struct rtvia *via = nla_data(nla);
1296 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1298 alen = nla_len(nla) -
1299 offsetof(struct rtvia, rtvia_addr);
1300 if (alen > MAX_VIA_ALEN)
1303 /* Validate the address family */
1304 switch (via->rtvia_family) {
1306 *via_table = NEIGH_LINK_TABLE;
1309 *via_table = NEIGH_ARP_TABLE;
1314 *via_table = NEIGH_ND_TABLE;
1319 /* Unsupported address family */
1323 memcpy(via_addr, via->rtvia_addr, alen);
1331 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1332 struct mpls_route_config *cfg)
1335 struct nlattr *tb[RTA_MAX+1];
1339 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1344 rtm = nlmsg_data(nlh);
1345 memset(cfg, 0, sizeof(*cfg));
1347 if (rtm->rtm_family != AF_MPLS)
1349 if (rtm->rtm_dst_len != 20)
1351 if (rtm->rtm_src_len != 0)
1353 if (rtm->rtm_tos != 0)
1355 if (rtm->rtm_table != RT_TABLE_MAIN)
1357 /* Any value is acceptable for rtm_protocol */
1359 /* As mpls uses destination specific addresses
1360 * (or source specific address in the case of multicast)
1361 * all addresses have universal scope.
1363 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1365 if (rtm->rtm_type != RTN_UNICAST)
1367 if (rtm->rtm_flags != 0)
1370 cfg->rc_label = LABEL_NOT_SPECIFIED;
1371 cfg->rc_protocol = rtm->rtm_protocol;
1372 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
1373 cfg->rc_nlflags = nlh->nlmsg_flags;
1374 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1375 cfg->rc_nlinfo.nlh = nlh;
1376 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1378 for (index = 0; index <= RTA_MAX; index++) {
1379 struct nlattr *nla = tb[index];
1385 cfg->rc_ifindex = nla_get_u32(nla);
1388 if (nla_get_labels(nla, MAX_NEW_LABELS,
1389 &cfg->rc_output_labels,
1390 cfg->rc_output_label))
1396 if (nla_get_labels(nla, 1, &label_count,
1400 /* Reserved labels may not be set */
1401 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1408 if (nla_get_via(nla, &cfg->rc_via_alen,
1409 &cfg->rc_via_table, cfg->rc_via))
1415 cfg->rc_mp = nla_data(nla);
1416 cfg->rc_mp_len = nla_len(nla);
1420 /* Unsupported attribute */
1430 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1432 struct mpls_route_config cfg;
1435 err = rtm_to_route_config(skb, nlh, &cfg);
1439 return mpls_route_del(&cfg);
1443 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1445 struct mpls_route_config cfg;
1448 err = rtm_to_route_config(skb, nlh, &cfg);
1452 return mpls_route_add(&cfg);
1455 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1456 u32 label, struct mpls_route *rt, int flags)
1458 struct net_device *dev;
1459 struct nlmsghdr *nlh;
1462 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1466 rtm = nlmsg_data(nlh);
1467 rtm->rtm_family = AF_MPLS;
1468 rtm->rtm_dst_len = 20;
1469 rtm->rtm_src_len = 0;
1471 rtm->rtm_table = RT_TABLE_MAIN;
1472 rtm->rtm_protocol = rt->rt_protocol;
1473 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1474 rtm->rtm_type = RTN_UNICAST;
1477 if (nla_put_labels(skb, RTA_DST, 1, &label))
1478 goto nla_put_failure;
1479 if (rt->rt_nhn == 1) {
1480 const struct mpls_nh *nh = rt->rt_nh;
1482 if (nh->nh_labels &&
1483 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1485 goto nla_put_failure;
1486 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1487 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1489 goto nla_put_failure;
1490 dev = rtnl_dereference(nh->nh_dev);
1491 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1492 goto nla_put_failure;
1493 if (nh->nh_flags & RTNH_F_LINKDOWN)
1494 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1495 if (nh->nh_flags & RTNH_F_DEAD)
1496 rtm->rtm_flags |= RTNH_F_DEAD;
1498 struct rtnexthop *rtnh;
1503 mp = nla_nest_start(skb, RTA_MULTIPATH);
1505 goto nla_put_failure;
1508 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1510 goto nla_put_failure;
1512 dev = rtnl_dereference(nh->nh_dev);
1514 rtnh->rtnh_ifindex = dev->ifindex;
1515 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1516 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1519 if (nh->nh_flags & RTNH_F_DEAD) {
1520 rtnh->rtnh_flags |= RTNH_F_DEAD;
1524 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1527 goto nla_put_failure;
1528 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1529 nla_put_via(skb, nh->nh_via_table,
1530 mpls_nh_via(rt, nh),
1532 goto nla_put_failure;
1534 /* length of rtnetlink header + attributes */
1535 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1536 } endfor_nexthops(rt);
1538 if (linkdown == rt->rt_nhn)
1539 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1540 if (dead == rt->rt_nhn)
1541 rtm->rtm_flags |= RTNH_F_DEAD;
1543 nla_nest_end(skb, mp);
1546 nlmsg_end(skb, nlh);
1550 nlmsg_cancel(skb, nlh);
1554 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1556 struct net *net = sock_net(skb->sk);
1557 struct mpls_route __rcu **platform_label;
1558 size_t platform_labels;
1563 index = cb->args[0];
1564 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1565 index = MPLS_LABEL_FIRST_UNRESERVED;
1567 platform_label = rtnl_dereference(net->mpls.platform_label);
1568 platform_labels = net->mpls.platform_labels;
1569 for (; index < platform_labels; index++) {
1570 struct mpls_route *rt;
1571 rt = rtnl_dereference(platform_label[index]);
1575 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1576 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1577 index, rt, NLM_F_MULTI) < 0)
1580 cb->args[0] = index;
1585 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1588 NLMSG_ALIGN(sizeof(struct rtmsg))
1589 + nla_total_size(4); /* RTA_DST */
1591 if (rt->rt_nhn == 1) {
1592 struct mpls_nh *nh = rt->rt_nh;
1595 payload += nla_total_size(4); /* RTA_OIF */
1596 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1597 payload += nla_total_size(2 + nh->nh_via_alen);
1598 if (nh->nh_labels) /* RTA_NEWDST */
1599 payload += nla_total_size(nh->nh_labels * 4);
1601 /* each nexthop is packed in an attribute */
1605 nhsize += nla_total_size(sizeof(struct rtnexthop));
1607 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1608 nhsize += nla_total_size(2 + nh->nh_via_alen);
1610 nhsize += nla_total_size(nh->nh_labels * 4);
1611 } endfor_nexthops(rt);
1612 /* nested attribute */
1613 payload += nla_total_size(nhsize);
1619 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1620 struct nlmsghdr *nlh, struct net *net, u32 portid,
1621 unsigned int nlm_flags)
1623 struct sk_buff *skb;
1624 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1627 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1631 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1633 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1634 WARN_ON(err == -EMSGSIZE);
1638 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1643 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1646 static int resize_platform_label_table(struct net *net, size_t limit)
1648 size_t size = sizeof(struct mpls_route *) * limit;
1651 struct mpls_route __rcu **labels = NULL, **old;
1652 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1656 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1658 labels = vzalloc(size);
1664 /* In case the predefined labels need to be populated */
1665 if (limit > MPLS_LABEL_IPV4NULL) {
1666 struct net_device *lo = net->loopback_dev;
1667 rt0 = mpls_rt_alloc(1, lo->addr_len);
1670 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1671 rt0->rt_protocol = RTPROT_KERNEL;
1672 rt0->rt_payload_type = MPT_IPV4;
1673 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1674 rt0->rt_nh->nh_via_alen = lo->addr_len;
1675 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1678 if (limit > MPLS_LABEL_IPV6NULL) {
1679 struct net_device *lo = net->loopback_dev;
1680 rt2 = mpls_rt_alloc(1, lo->addr_len);
1683 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1684 rt2->rt_protocol = RTPROT_KERNEL;
1685 rt2->rt_payload_type = MPT_IPV6;
1686 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1687 rt2->rt_nh->nh_via_alen = lo->addr_len;
1688 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1693 /* Remember the original table */
1694 old = rtnl_dereference(net->mpls.platform_label);
1695 old_limit = net->mpls.platform_labels;
1697 /* Free any labels beyond the new table */
1698 for (index = limit; index < old_limit; index++)
1699 mpls_route_update(net, index, NULL, NULL);
1701 /* Copy over the old labels */
1703 if (old_limit < limit)
1704 cp_size = old_limit * sizeof(struct mpls_route *);
1706 memcpy(labels, old, cp_size);
1708 /* If needed set the predefined labels */
1709 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1710 (limit > MPLS_LABEL_IPV6NULL)) {
1711 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1715 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1716 (limit > MPLS_LABEL_IPV4NULL)) {
1717 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1721 /* Update the global pointers */
1722 net->mpls.platform_labels = limit;
1723 rcu_assign_pointer(net->mpls.platform_label, labels);
1744 static int mpls_platform_labels(struct ctl_table *table, int write,
1745 void __user *buffer, size_t *lenp, loff_t *ppos)
1747 struct net *net = table->data;
1748 int platform_labels = net->mpls.platform_labels;
1750 struct ctl_table tmp = {
1751 .procname = table->procname,
1752 .data = &platform_labels,
1753 .maxlen = sizeof(int),
1754 .mode = table->mode,
1756 .extra2 = &label_limit,
1759 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1761 if (write && ret == 0)
1762 ret = resize_platform_label_table(net, platform_labels);
1767 static const struct ctl_table mpls_table[] = {
1769 .procname = "platform_labels",
1771 .maxlen = sizeof(int),
1773 .proc_handler = mpls_platform_labels,
1778 static int mpls_net_init(struct net *net)
1780 struct ctl_table *table;
1782 net->mpls.platform_labels = 0;
1783 net->mpls.platform_label = NULL;
1785 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1789 table[0].data = net;
1790 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1791 if (net->mpls.ctl == NULL) {
1799 static void mpls_net_exit(struct net *net)
1801 struct mpls_route __rcu **platform_label;
1802 size_t platform_labels;
1803 struct ctl_table *table;
1806 table = net->mpls.ctl->ctl_table_arg;
1807 unregister_net_sysctl_table(net->mpls.ctl);
1810 /* An rcu grace period has passed since there was a device in
1811 * the network namespace (and thus the last in flight packet)
1812 * left this network namespace. This is because
1813 * unregister_netdevice_many and netdev_run_todo has completed
1814 * for each network device that was in this network namespace.
1816 * As such no additional rcu synchronization is necessary when
1817 * freeing the platform_label table.
1820 platform_label = rtnl_dereference(net->mpls.platform_label);
1821 platform_labels = net->mpls.platform_labels;
1822 for (index = 0; index < platform_labels; index++) {
1823 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1824 RCU_INIT_POINTER(platform_label[index], NULL);
1829 kvfree(platform_label);
1832 static struct pernet_operations mpls_net_ops = {
1833 .init = mpls_net_init,
1834 .exit = mpls_net_exit,
1837 static struct rtnl_af_ops mpls_af_ops __read_mostly = {
1839 .fill_stats_af = mpls_fill_stats_af,
1840 .get_stats_af_size = mpls_get_stats_af_size,
1843 static int __init mpls_init(void)
1847 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1849 err = register_pernet_subsys(&mpls_net_ops);
1853 err = register_netdevice_notifier(&mpls_dev_notifier);
1855 goto out_unregister_pernet;
1857 dev_add_pack(&mpls_packet_type);
1859 rtnl_af_register(&mpls_af_ops);
1861 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1862 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1863 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1868 out_unregister_pernet:
1869 unregister_pernet_subsys(&mpls_net_ops);
1872 module_init(mpls_init);
1874 static void __exit mpls_exit(void)
1876 rtnl_unregister_all(PF_MPLS);
1877 rtnl_af_unregister(&mpls_af_ops);
1878 dev_remove_pack(&mpls_packet_type);
1879 unregister_netdevice_notifier(&mpls_dev_notifier);
1880 unregister_pernet_subsys(&mpls_net_ops);
1882 module_exit(mpls_exit);
1884 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1885 MODULE_LICENSE("GPL v2");
1886 MODULE_ALIAS_NETPROTO(PF_MPLS);