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/nospec.h>
11 #include <linux/vmalloc.h>
16 #include <net/ip_fib.h>
17 #include <net/netevent.h>
18 #include <net/netns/generic.h>
19 #if IS_ENABLED(CONFIG_IPV6)
21 #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 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
54 return rcu_dereference_rtnl(dev->mpls_ptr);
57 bool mpls_output_possible(const struct net_device *dev)
59 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
61 EXPORT_SYMBOL_GPL(mpls_output_possible);
63 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
65 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
66 int nh_index = nh - rt->rt_nh;
68 return nh0_via + rt->rt_max_alen * nh_index;
71 static const u8 *mpls_nh_via(const struct mpls_route *rt,
72 const struct mpls_nh *nh)
74 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
77 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
79 /* The size of the layer 2.5 labels to be added for this route */
80 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
83 unsigned int mpls_dev_mtu(const struct net_device *dev)
85 /* The amount of data the layer 2 frame can hold */
88 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
90 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
95 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
100 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
102 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
104 struct mpls_entry_decoded dec;
105 unsigned int mpls_hdr_len = 0;
106 struct mpls_shim_hdr *hdr;
107 bool eli_seen = false;
111 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
113 mpls_hdr_len += sizeof(*hdr);
114 if (!pskb_may_pull(skb, mpls_hdr_len))
117 /* Read and decode the current label */
118 hdr = mpls_hdr(skb) + label_index;
119 dec = mpls_entry_decode(hdr);
121 /* RFC6790 - reserved labels MUST NOT be used as keys
122 * for the load-balancing function
124 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
125 hash = jhash_1word(dec.label, hash);
127 /* The entropy label follows the entropy label
128 * indicator, so this means that the entropy
129 * label was just added to the hash - no need to
130 * go any deeper either in the label stack or in the
135 } else if (dec.label == MPLS_LABEL_ENTROPY) {
142 /* found bottom label; does skb have room for a header? */
143 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
144 const struct iphdr *v4hdr;
146 v4hdr = (const struct iphdr *)(hdr + 1);
147 if (v4hdr->version == 4) {
148 hash = jhash_3words(ntohl(v4hdr->saddr),
150 v4hdr->protocol, hash);
151 } else if (v4hdr->version == 6 &&
152 pskb_may_pull(skb, mpls_hdr_len +
153 sizeof(struct ipv6hdr))) {
154 const struct ipv6hdr *v6hdr;
156 v6hdr = (const struct ipv6hdr *)(hdr + 1);
157 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
158 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
159 hash = jhash_1word(v6hdr->nexthdr, hash);
169 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
172 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
177 /* No need to look further into packet if there's only
186 hash = mpls_multipath_hash(rt, skb);
187 nh_index = hash % alive;
188 if (alive == rt->rt_nhn)
191 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
196 } endfor_nexthops(rt);
199 return &rt->rt_nh[nh_index];
202 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
203 struct mpls_entry_decoded dec)
205 enum mpls_payload_type payload_type;
206 bool success = false;
208 /* The IPv4 code below accesses through the IPv4 header
209 * checksum, which is 12 bytes into the packet.
210 * The IPv6 code below accesses through the IPv6 hop limit
211 * which is 8 bytes into the packet.
213 * For all supported cases there should always be at least 12
214 * bytes of packet data present. The IPv4 header is 20 bytes
215 * without options and the IPv6 header is always 40 bytes
218 if (!pskb_may_pull(skb, 12))
221 payload_type = rt->rt_payload_type;
222 if (payload_type == MPT_UNSPEC)
223 payload_type = ip_hdr(skb)->version;
225 switch (payload_type) {
227 struct iphdr *hdr4 = ip_hdr(skb);
228 skb->protocol = htons(ETH_P_IP);
229 csum_replace2(&hdr4->check,
230 htons(hdr4->ttl << 8),
231 htons(dec.ttl << 8));
237 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
238 skb->protocol = htons(ETH_P_IPV6);
239 hdr6->hop_limit = dec.ttl;
250 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
251 struct packet_type *pt, struct net_device *orig_dev)
253 struct net *net = dev_net(dev);
254 struct mpls_shim_hdr *hdr;
255 struct mpls_route *rt;
257 struct mpls_entry_decoded dec;
258 struct net_device *out_dev;
259 struct mpls_dev *mdev;
261 unsigned int new_header_size;
265 /* Careful this entire function runs inside of an rcu critical section */
267 mdev = mpls_dev_get(dev);
268 if (!mdev || !mdev->input_enabled)
271 if (skb->pkt_type != PACKET_HOST)
274 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
277 if (!pskb_may_pull(skb, sizeof(*hdr)))
280 /* Read and decode the label */
282 dec = mpls_entry_decode(hdr);
284 rt = mpls_route_input_rcu(net, dec.label);
288 nh = mpls_select_multipath(rt, skb);
292 /* Find the output device */
293 out_dev = rcu_dereference(nh->nh_dev);
294 if (!mpls_output_possible(out_dev))
298 skb_pull(skb, sizeof(*hdr));
299 skb_reset_network_header(skb);
303 if (skb_warn_if_lro(skb))
306 skb_forward_csum(skb);
308 /* Verify ttl is valid */
313 /* Verify the destination can hold the packet */
314 new_header_size = mpls_nh_header_size(nh);
315 mtu = mpls_dev_mtu(out_dev);
316 if (mpls_pkt_too_big(skb, mtu - new_header_size))
319 hh_len = LL_RESERVED_SPACE(out_dev);
320 if (!out_dev->header_ops)
323 /* Ensure there is enough space for the headers in the skb */
324 if (skb_cow(skb, hh_len + new_header_size))
328 skb->protocol = htons(ETH_P_MPLS_UC);
330 if (unlikely(!new_header_size && dec.bos)) {
331 /* Penultimate hop popping */
332 if (!mpls_egress(rt, skb, dec))
337 skb_push(skb, new_header_size);
338 skb_reset_network_header(skb);
339 /* Push the new labels */
342 for (i = nh->nh_labels - 1; i >= 0; i--) {
343 hdr[i] = mpls_entry_encode(nh->nh_label[i],
349 /* If via wasn't specified then send out using device address */
350 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
351 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
352 out_dev->dev_addr, skb);
354 err = neigh_xmit(nh->nh_via_table, out_dev,
355 mpls_nh_via(rt, nh), skb);
357 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
366 static struct packet_type mpls_packet_type __read_mostly = {
367 .type = cpu_to_be16(ETH_P_MPLS_UC),
368 .func = mpls_forward,
371 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
372 [RTA_DST] = { .type = NLA_U32 },
373 [RTA_OIF] = { .type = NLA_U32 },
376 struct mpls_route_config {
381 u8 rc_via[MAX_VIA_ALEN];
384 u32 rc_output_label[MAX_NEW_LABELS];
386 enum mpls_payload_type rc_payload_type;
387 struct nl_info rc_nlinfo;
388 struct rtnexthop *rc_mp;
392 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
394 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
395 struct mpls_route *rt;
397 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
399 num_nh * max_alen_aligned,
403 rt->rt_nhn_alive = num_nh;
404 rt->rt_max_alen = max_alen_aligned;
410 static void mpls_rt_free(struct mpls_route *rt)
413 kfree_rcu(rt, rt_rcu);
416 static void mpls_notify_route(struct net *net, unsigned index,
417 struct mpls_route *old, struct mpls_route *new,
418 const struct nl_info *info)
420 struct nlmsghdr *nlh = info ? info->nlh : NULL;
421 unsigned portid = info ? info->portid : 0;
422 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
423 struct mpls_route *rt = new ? new : old;
424 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
425 /* Ignore reserved labels for now */
426 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
427 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
430 static void mpls_route_update(struct net *net, unsigned index,
431 struct mpls_route *new,
432 const struct nl_info *info)
434 struct mpls_route __rcu **platform_label;
435 struct mpls_route *rt;
439 platform_label = rtnl_dereference(net->mpls.platform_label);
440 rt = rtnl_dereference(platform_label[index]);
441 rcu_assign_pointer(platform_label[index], new);
443 mpls_notify_route(net, index, rt, new, info);
445 /* If we removed a route free it now */
449 static unsigned find_free_label(struct net *net)
451 struct mpls_route __rcu **platform_label;
452 size_t platform_labels;
455 platform_label = rtnl_dereference(net->mpls.platform_label);
456 platform_labels = net->mpls.platform_labels;
457 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
459 if (!rtnl_dereference(platform_label[index]))
462 return LABEL_NOT_SPECIFIED;
465 #if IS_ENABLED(CONFIG_INET)
466 static struct net_device *inet_fib_lookup_dev(struct net *net,
469 struct net_device *dev;
471 struct in_addr daddr;
473 memcpy(&daddr, addr, sizeof(struct in_addr));
474 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
486 static struct net_device *inet_fib_lookup_dev(struct net *net,
489 return ERR_PTR(-EAFNOSUPPORT);
493 #if IS_ENABLED(CONFIG_IPV6)
494 static struct net_device *inet6_fib_lookup_dev(struct net *net,
497 struct net_device *dev;
498 struct dst_entry *dst;
502 return ERR_PTR(-EAFNOSUPPORT);
504 memset(&fl6, 0, sizeof(fl6));
505 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
506 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
508 return ERR_CAST(dst);
517 static struct net_device *inet6_fib_lookup_dev(struct net *net,
520 return ERR_PTR(-EAFNOSUPPORT);
524 static struct net_device *find_outdev(struct net *net,
525 struct mpls_route *rt,
526 struct mpls_nh *nh, int oif)
528 struct net_device *dev = NULL;
531 switch (nh->nh_via_table) {
532 case NEIGH_ARP_TABLE:
533 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
536 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
538 case NEIGH_LINK_TABLE:
542 dev = dev_get_by_index(net, oif);
546 return ERR_PTR(-ENODEV);
551 /* The caller is holding rtnl anyways, so release the dev reference */
557 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
558 struct mpls_nh *nh, int oif)
560 struct net_device *dev = NULL;
563 dev = find_outdev(net, rt, nh, oif);
570 /* Ensure this is a supported device */
572 if (!mpls_dev_get(dev))
575 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
576 (dev->addr_len != nh->nh_via_alen))
579 RCU_INIT_POINTER(nh->nh_dev, dev);
581 if (!(dev->flags & IFF_UP)) {
582 nh->nh_flags |= RTNH_F_DEAD;
586 flags = dev_get_flags(dev);
587 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
588 nh->nh_flags |= RTNH_F_LINKDOWN;
597 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
598 struct mpls_route *rt)
600 struct net *net = cfg->rc_nlinfo.nl_net;
601 struct mpls_nh *nh = rt->rt_nh;
609 /* Ensure only a supported number of labels are present */
610 if (cfg->rc_output_labels > MAX_NEW_LABELS)
613 nh->nh_labels = cfg->rc_output_labels;
614 for (i = 0; i < nh->nh_labels; i++)
615 nh->nh_label[i] = cfg->rc_output_label[i];
617 nh->nh_via_table = cfg->rc_via_table;
618 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
619 nh->nh_via_alen = cfg->rc_via_alen;
621 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
625 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
634 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
635 struct mpls_nh *nh, int oif, struct nlattr *via,
636 struct nlattr *newdst)
644 err = nla_get_labels(newdst, MAX_NEW_LABELS,
645 &nh->nh_labels, nh->nh_label);
651 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
652 __mpls_nh_via(rt, nh));
656 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
659 err = mpls_nh_assign_dev(net, rt, nh, oif);
669 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
670 u8 cfg_via_alen, u8 *max_via_alen)
676 *max_via_alen = cfg_via_alen;
682 while (rtnh_ok(rtnh, remaining)) {
683 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
686 attrlen = rtnh_attrlen(rtnh);
687 nla = nla_find(attrs, attrlen, RTA_VIA);
688 if (nla && nla_len(nla) >=
689 offsetof(struct rtvia, rtvia_addr)) {
690 int via_alen = nla_len(nla) -
691 offsetof(struct rtvia, rtvia_addr);
693 if (via_alen <= MAX_VIA_ALEN)
694 *max_via_alen = max_t(u16, *max_via_alen,
699 rtnh = rtnh_next(rtnh, &remaining);
702 /* leftover implies invalid nexthop configuration, discard it */
703 return remaining > 0 ? 0 : nhs;
706 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
707 struct mpls_route *rt)
709 struct rtnexthop *rtnh = cfg->rc_mp;
710 struct nlattr *nla_via, *nla_newdst;
711 int remaining = cfg->rc_mp_len;
715 change_nexthops(rt) {
722 if (!rtnh_ok(rtnh, remaining))
725 /* neither weighted multipath nor any flags
728 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
731 attrlen = rtnh_attrlen(rtnh);
733 struct nlattr *attrs = rtnh_attrs(rtnh);
735 nla_via = nla_find(attrs, attrlen, RTA_VIA);
736 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
739 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
740 rtnh->rtnh_ifindex, nla_via, nla_newdst);
744 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
747 rtnh = rtnh_next(rtnh, &remaining);
749 } endfor_nexthops(rt);
759 static bool mpls_label_ok(struct net *net, unsigned int *index)
763 /* Reserved labels may not be set */
764 if (*index < MPLS_LABEL_FIRST_UNRESERVED)
767 /* The full 20 bit range may not be supported. */
768 if (is_ok && *index >= net->mpls.platform_labels)
771 *index = array_index_nospec(*index, net->mpls.platform_labels);
775 static int mpls_route_add(struct mpls_route_config *cfg)
777 struct mpls_route __rcu **platform_label;
778 struct net *net = cfg->rc_nlinfo.nl_net;
779 struct mpls_route *rt, *old;
785 index = cfg->rc_label;
787 /* If a label was not specified during insert pick one */
788 if ((index == LABEL_NOT_SPECIFIED) &&
789 (cfg->rc_nlflags & NLM_F_CREATE)) {
790 index = find_free_label(net);
793 if (!mpls_label_ok(net, &index))
796 /* Append makes no sense with mpls */
798 if (cfg->rc_nlflags & NLM_F_APPEND)
802 platform_label = rtnl_dereference(net->mpls.platform_label);
803 old = rtnl_dereference(platform_label[index]);
804 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
808 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
812 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
816 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
817 cfg->rc_via_alen, &max_via_alen);
822 rt = mpls_rt_alloc(nhs, max_via_alen);
826 rt->rt_protocol = cfg->rc_protocol;
827 rt->rt_payload_type = cfg->rc_payload_type;
830 err = mpls_nh_build_multi(cfg, rt);
832 err = mpls_nh_build_from_cfg(cfg, rt);
836 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
846 static int mpls_route_del(struct mpls_route_config *cfg)
848 struct net *net = cfg->rc_nlinfo.nl_net;
852 index = cfg->rc_label;
854 if (!mpls_label_ok(net, &index))
857 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
864 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
865 (&((struct mpls_dev *)0)->field)
867 static const struct ctl_table mpls_dev_table[] = {
870 .maxlen = sizeof(int),
872 .proc_handler = proc_dointvec,
873 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
878 static int mpls_dev_sysctl_register(struct net_device *dev,
879 struct mpls_dev *mdev)
881 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
882 struct ctl_table *table;
885 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
889 /* Table data contains only offsets relative to the base of
890 * the mdev at this point, so make them absolute.
892 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
893 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
895 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
897 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
909 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
911 struct ctl_table *table;
913 table = mdev->sysctl->ctl_table_arg;
914 unregister_net_sysctl_table(mdev->sysctl);
918 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
920 struct mpls_dev *mdev;
925 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
929 err = mpls_dev_sysctl_register(dev, mdev);
933 rcu_assign_pointer(dev->mpls_ptr, mdev);
942 static void mpls_ifdown(struct net_device *dev, int event)
944 struct mpls_route __rcu **platform_label;
945 struct net *net = dev_net(dev);
946 unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
950 platform_label = rtnl_dereference(net->mpls.platform_label);
951 for (index = 0; index < net->mpls.platform_labels; index++) {
952 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
958 change_nexthops(rt) {
959 if (rtnl_dereference(nh->nh_dev) != dev)
964 case NETDEV_UNREGISTER:
965 nh->nh_flags |= RTNH_F_DEAD;
968 nh->nh_flags |= RTNH_F_LINKDOWN;
971 if (event == NETDEV_UNREGISTER)
972 RCU_INIT_POINTER(nh->nh_dev, NULL);
974 if (!(nh->nh_flags & nh_flags))
976 } endfor_nexthops(rt);
978 WRITE_ONCE(rt->rt_nhn_alive, alive);
982 static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
984 struct mpls_route __rcu **platform_label;
985 struct net *net = dev_net(dev);
989 platform_label = rtnl_dereference(net->mpls.platform_label);
990 for (index = 0; index < net->mpls.platform_labels; index++) {
991 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
997 change_nexthops(rt) {
998 struct net_device *nh_dev =
999 rtnl_dereference(nh->nh_dev);
1001 if (!(nh->nh_flags & nh_flags)) {
1008 nh->nh_flags &= ~nh_flags;
1009 } endfor_nexthops(rt);
1011 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
1015 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1018 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1019 struct mpls_dev *mdev;
1022 if (event == NETDEV_REGISTER) {
1023 /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
1024 if (dev->type == ARPHRD_ETHER ||
1025 dev->type == ARPHRD_LOOPBACK ||
1026 dev->type == ARPHRD_IPGRE ||
1027 dev->type == ARPHRD_SIT ||
1028 dev->type == ARPHRD_TUNNEL) {
1029 mdev = mpls_add_dev(dev);
1031 return notifier_from_errno(PTR_ERR(mdev));
1036 mdev = mpls_dev_get(dev);
1042 mpls_ifdown(dev, event);
1045 flags = dev_get_flags(dev);
1046 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1047 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1049 mpls_ifup(dev, RTNH_F_DEAD);
1052 flags = dev_get_flags(dev);
1053 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1054 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1056 mpls_ifdown(dev, event);
1058 case NETDEV_UNREGISTER:
1059 mpls_ifdown(dev, event);
1060 mdev = mpls_dev_get(dev);
1062 mpls_dev_sysctl_unregister(mdev);
1063 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1064 kfree_rcu(mdev, rcu);
1067 case NETDEV_CHANGENAME:
1068 mdev = mpls_dev_get(dev);
1072 mpls_dev_sysctl_unregister(mdev);
1073 err = mpls_dev_sysctl_register(dev, mdev);
1075 return notifier_from_errno(err);
1082 static struct notifier_block mpls_dev_notifier = {
1083 .notifier_call = mpls_dev_notify,
1086 static int nla_put_via(struct sk_buff *skb,
1087 u8 table, const void *addr, int alen)
1089 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1090 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1094 int family = AF_UNSPEC;
1096 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1100 if (table <= NEIGH_NR_TABLES)
1101 family = table_to_family[table];
1103 via = nla_data(nla);
1104 via->rtvia_family = family;
1105 memcpy(via->rtvia_addr, addr, alen);
1109 int nla_put_labels(struct sk_buff *skb, int attrtype,
1110 u8 labels, const u32 label[])
1113 struct mpls_shim_hdr *nla_label;
1116 nla = nla_reserve(skb, attrtype, labels*4);
1120 nla_label = nla_data(nla);
1122 for (i = labels - 1; i >= 0; i--) {
1123 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1129 EXPORT_SYMBOL_GPL(nla_put_labels);
1131 int nla_get_labels(const struct nlattr *nla,
1132 u32 max_labels, u8 *labels, u32 label[])
1134 unsigned len = nla_len(nla);
1135 unsigned nla_labels;
1136 struct mpls_shim_hdr *nla_label;
1140 /* len needs to be an even multiple of 4 (the label size) */
1144 /* Limit the number of new labels allowed */
1146 if (nla_labels > max_labels)
1149 nla_label = nla_data(nla);
1151 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1152 struct mpls_entry_decoded dec;
1153 dec = mpls_entry_decode(nla_label + i);
1155 /* Ensure the bottom of stack flag is properly set
1156 * and ttl and tc are both clear.
1158 if ((dec.bos != bos) || dec.ttl || dec.tc)
1161 switch (dec.label) {
1162 case MPLS_LABEL_IMPLNULL:
1163 /* RFC3032: This is a label that an LSR may
1164 * assign and distribute, but which never
1165 * actually appears in the encapsulation.
1170 label[i] = dec.label;
1172 *labels = nla_labels;
1175 EXPORT_SYMBOL_GPL(nla_get_labels);
1177 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1178 u8 *via_table, u8 via_addr[])
1180 struct rtvia *via = nla_data(nla);
1184 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1186 alen = nla_len(nla) -
1187 offsetof(struct rtvia, rtvia_addr);
1188 if (alen > MAX_VIA_ALEN)
1191 /* Validate the address family */
1192 switch (via->rtvia_family) {
1194 *via_table = NEIGH_LINK_TABLE;
1197 *via_table = NEIGH_ARP_TABLE;
1202 *via_table = NEIGH_ND_TABLE;
1207 /* Unsupported address family */
1211 memcpy(via_addr, via->rtvia_addr, alen);
1219 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1220 struct mpls_route_config *cfg)
1223 struct nlattr *tb[RTA_MAX+1];
1227 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1232 rtm = nlmsg_data(nlh);
1233 memset(cfg, 0, sizeof(*cfg));
1235 if (rtm->rtm_family != AF_MPLS)
1237 if (rtm->rtm_dst_len != 20)
1239 if (rtm->rtm_src_len != 0)
1241 if (rtm->rtm_tos != 0)
1243 if (rtm->rtm_table != RT_TABLE_MAIN)
1245 /* Any value is acceptable for rtm_protocol */
1247 /* As mpls uses destination specific addresses
1248 * (or source specific address in the case of multicast)
1249 * all addresses have universal scope.
1251 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1253 if (rtm->rtm_type != RTN_UNICAST)
1255 if (rtm->rtm_flags != 0)
1258 cfg->rc_label = LABEL_NOT_SPECIFIED;
1259 cfg->rc_protocol = rtm->rtm_protocol;
1260 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
1261 cfg->rc_nlflags = nlh->nlmsg_flags;
1262 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1263 cfg->rc_nlinfo.nlh = nlh;
1264 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1266 for (index = 0; index <= RTA_MAX; index++) {
1267 struct nlattr *nla = tb[index];
1273 cfg->rc_ifindex = nla_get_u32(nla);
1276 if (nla_get_labels(nla, MAX_NEW_LABELS,
1277 &cfg->rc_output_labels,
1278 cfg->rc_output_label))
1284 if (nla_get_labels(nla, 1, &label_count,
1288 if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1295 if (nla_get_via(nla, &cfg->rc_via_alen,
1296 &cfg->rc_via_table, cfg->rc_via))
1302 cfg->rc_mp = nla_data(nla);
1303 cfg->rc_mp_len = nla_len(nla);
1307 /* Unsupported attribute */
1317 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1319 struct mpls_route_config cfg;
1322 err = rtm_to_route_config(skb, nlh, &cfg);
1326 return mpls_route_del(&cfg);
1330 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1332 struct mpls_route_config cfg;
1335 err = rtm_to_route_config(skb, nlh, &cfg);
1339 return mpls_route_add(&cfg);
1342 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1343 u32 label, struct mpls_route *rt, int flags)
1345 struct net_device *dev;
1346 struct nlmsghdr *nlh;
1349 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1353 rtm = nlmsg_data(nlh);
1354 rtm->rtm_family = AF_MPLS;
1355 rtm->rtm_dst_len = 20;
1356 rtm->rtm_src_len = 0;
1358 rtm->rtm_table = RT_TABLE_MAIN;
1359 rtm->rtm_protocol = rt->rt_protocol;
1360 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1361 rtm->rtm_type = RTN_UNICAST;
1364 if (nla_put_labels(skb, RTA_DST, 1, &label))
1365 goto nla_put_failure;
1366 if (rt->rt_nhn == 1) {
1367 const struct mpls_nh *nh = rt->rt_nh;
1369 if (nh->nh_labels &&
1370 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1372 goto nla_put_failure;
1373 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1374 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1376 goto nla_put_failure;
1377 dev = rtnl_dereference(nh->nh_dev);
1378 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1379 goto nla_put_failure;
1380 if (nh->nh_flags & RTNH_F_LINKDOWN)
1381 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1382 if (nh->nh_flags & RTNH_F_DEAD)
1383 rtm->rtm_flags |= RTNH_F_DEAD;
1385 struct rtnexthop *rtnh;
1390 mp = nla_nest_start(skb, RTA_MULTIPATH);
1392 goto nla_put_failure;
1395 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1397 goto nla_put_failure;
1399 dev = rtnl_dereference(nh->nh_dev);
1401 rtnh->rtnh_ifindex = dev->ifindex;
1402 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1403 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1406 if (nh->nh_flags & RTNH_F_DEAD) {
1407 rtnh->rtnh_flags |= RTNH_F_DEAD;
1411 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1414 goto nla_put_failure;
1415 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1416 nla_put_via(skb, nh->nh_via_table,
1417 mpls_nh_via(rt, nh),
1419 goto nla_put_failure;
1421 /* length of rtnetlink header + attributes */
1422 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1423 } endfor_nexthops(rt);
1425 if (linkdown == rt->rt_nhn)
1426 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1427 if (dead == rt->rt_nhn)
1428 rtm->rtm_flags |= RTNH_F_DEAD;
1430 nla_nest_end(skb, mp);
1433 nlmsg_end(skb, nlh);
1437 nlmsg_cancel(skb, nlh);
1441 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1443 struct net *net = sock_net(skb->sk);
1444 struct mpls_route __rcu **platform_label;
1445 size_t platform_labels;
1450 index = cb->args[0];
1451 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1452 index = MPLS_LABEL_FIRST_UNRESERVED;
1454 platform_label = rtnl_dereference(net->mpls.platform_label);
1455 platform_labels = net->mpls.platform_labels;
1456 for (; index < platform_labels; index++) {
1457 struct mpls_route *rt;
1458 rt = rtnl_dereference(platform_label[index]);
1462 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1463 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1464 index, rt, NLM_F_MULTI) < 0)
1467 cb->args[0] = index;
1472 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1475 NLMSG_ALIGN(sizeof(struct rtmsg))
1476 + nla_total_size(4); /* RTA_DST */
1478 if (rt->rt_nhn == 1) {
1479 struct mpls_nh *nh = rt->rt_nh;
1482 payload += nla_total_size(4); /* RTA_OIF */
1483 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1484 payload += nla_total_size(2 + nh->nh_via_alen);
1485 if (nh->nh_labels) /* RTA_NEWDST */
1486 payload += nla_total_size(nh->nh_labels * 4);
1488 /* each nexthop is packed in an attribute */
1492 nhsize += nla_total_size(sizeof(struct rtnexthop));
1494 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1495 nhsize += nla_total_size(2 + nh->nh_via_alen);
1497 nhsize += nla_total_size(nh->nh_labels * 4);
1498 } endfor_nexthops(rt);
1499 /* nested attribute */
1500 payload += nla_total_size(nhsize);
1506 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1507 struct nlmsghdr *nlh, struct net *net, u32 portid,
1508 unsigned int nlm_flags)
1510 struct sk_buff *skb;
1511 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1514 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1518 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1520 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1521 WARN_ON(err == -EMSGSIZE);
1525 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1530 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1533 static int resize_platform_label_table(struct net *net, size_t limit)
1535 size_t size = sizeof(struct mpls_route *) * limit;
1538 struct mpls_route __rcu **labels = NULL, **old;
1539 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1543 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1545 labels = vzalloc(size);
1551 /* In case the predefined labels need to be populated */
1552 if (limit > MPLS_LABEL_IPV4NULL) {
1553 struct net_device *lo = net->loopback_dev;
1554 rt0 = mpls_rt_alloc(1, lo->addr_len);
1557 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1558 rt0->rt_protocol = RTPROT_KERNEL;
1559 rt0->rt_payload_type = MPT_IPV4;
1560 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1561 rt0->rt_nh->nh_via_alen = lo->addr_len;
1562 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1565 if (limit > MPLS_LABEL_IPV6NULL) {
1566 struct net_device *lo = net->loopback_dev;
1567 rt2 = mpls_rt_alloc(1, lo->addr_len);
1570 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1571 rt2->rt_protocol = RTPROT_KERNEL;
1572 rt2->rt_payload_type = MPT_IPV6;
1573 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1574 rt2->rt_nh->nh_via_alen = lo->addr_len;
1575 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1580 /* Remember the original table */
1581 old = rtnl_dereference(net->mpls.platform_label);
1582 old_limit = net->mpls.platform_labels;
1584 /* Free any labels beyond the new table */
1585 for (index = limit; index < old_limit; index++)
1586 mpls_route_update(net, index, NULL, NULL);
1588 /* Copy over the old labels */
1590 if (old_limit < limit)
1591 cp_size = old_limit * sizeof(struct mpls_route *);
1593 memcpy(labels, old, cp_size);
1595 /* If needed set the predefined labels */
1596 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1597 (limit > MPLS_LABEL_IPV6NULL)) {
1598 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1602 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1603 (limit > MPLS_LABEL_IPV4NULL)) {
1604 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1608 /* Update the global pointers */
1609 net->mpls.platform_labels = limit;
1610 rcu_assign_pointer(net->mpls.platform_label, labels);
1631 static int mpls_platform_labels(struct ctl_table *table, int write,
1632 void __user *buffer, size_t *lenp, loff_t *ppos)
1634 struct net *net = table->data;
1635 int platform_labels = net->mpls.platform_labels;
1637 struct ctl_table tmp = {
1638 .procname = table->procname,
1639 .data = &platform_labels,
1640 .maxlen = sizeof(int),
1641 .mode = table->mode,
1643 .extra2 = &label_limit,
1646 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1648 if (write && ret == 0)
1649 ret = resize_platform_label_table(net, platform_labels);
1654 static const struct ctl_table mpls_table[] = {
1656 .procname = "platform_labels",
1658 .maxlen = sizeof(int),
1660 .proc_handler = mpls_platform_labels,
1665 static int mpls_net_init(struct net *net)
1667 struct ctl_table *table;
1669 net->mpls.platform_labels = 0;
1670 net->mpls.platform_label = NULL;
1672 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1676 table[0].data = net;
1677 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1678 if (net->mpls.ctl == NULL) {
1686 static void mpls_net_exit(struct net *net)
1688 struct mpls_route __rcu **platform_label;
1689 size_t platform_labels;
1690 struct ctl_table *table;
1693 table = net->mpls.ctl->ctl_table_arg;
1694 unregister_net_sysctl_table(net->mpls.ctl);
1697 /* An rcu grace period has passed since there was a device in
1698 * the network namespace (and thus the last in flight packet)
1699 * left this network namespace. This is because
1700 * unregister_netdevice_many and netdev_run_todo has completed
1701 * for each network device that was in this network namespace.
1703 * As such no additional rcu synchronization is necessary when
1704 * freeing the platform_label table.
1707 platform_label = rtnl_dereference(net->mpls.platform_label);
1708 platform_labels = net->mpls.platform_labels;
1709 for (index = 0; index < platform_labels; index++) {
1710 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1711 RCU_INIT_POINTER(platform_label[index], NULL);
1712 mpls_notify_route(net, index, rt, NULL, NULL);
1717 kvfree(platform_label);
1720 static struct pernet_operations mpls_net_ops = {
1721 .init = mpls_net_init,
1722 .exit = mpls_net_exit,
1725 static int __init mpls_init(void)
1729 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1731 err = register_pernet_subsys(&mpls_net_ops);
1735 err = register_netdevice_notifier(&mpls_dev_notifier);
1737 goto out_unregister_pernet;
1739 dev_add_pack(&mpls_packet_type);
1741 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1742 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1743 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1748 out_unregister_pernet:
1749 unregister_pernet_subsys(&mpls_net_ops);
1752 module_init(mpls_init);
1754 static void __exit mpls_exit(void)
1756 rtnl_unregister_all(PF_MPLS);
1757 dev_remove_pack(&mpls_packet_type);
1758 unregister_netdevice_notifier(&mpls_dev_notifier);
1759 unregister_pernet_subsys(&mpls_net_ops);
1761 module_exit(mpls_exit);
1763 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1764 MODULE_LICENSE("GPL v2");
1765 MODULE_ALIAS_NETPROTO(PF_MPLS);