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>
15 #include <net/ip_fib.h>
16 #include <net/netevent.h>
17 #include <net/netns/generic.h>
18 #if IS_ENABLED(CONFIG_IPV6)
20 #include <net/addrconf.h>
22 #include <net/nexthop.h>
25 /* Maximum number of labels to look ahead at when selecting a path of
28 #define MAX_MP_SELECT_LABELS 4
31 static int label_limit = (1 << 20) - 1;
33 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
34 struct nlmsghdr *nlh, struct net *net, u32 portid,
35 unsigned int nlm_flags);
37 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
39 struct mpls_route *rt = NULL;
41 if (index < net->mpls.platform_labels) {
42 struct mpls_route __rcu **platform_label =
43 rcu_dereference(net->mpls.platform_label);
44 rt = rcu_dereference(platform_label[index]);
49 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
51 return rcu_dereference_rtnl(dev->mpls_ptr);
54 bool mpls_output_possible(const struct net_device *dev)
56 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
58 EXPORT_SYMBOL_GPL(mpls_output_possible);
60 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
62 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
63 int nh_index = nh - rt->rt_nh;
65 return nh0_via + rt->rt_max_alen * nh_index;
68 static const u8 *mpls_nh_via(const struct mpls_route *rt,
69 const struct mpls_nh *nh)
71 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
74 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
76 /* The size of the layer 2.5 labels to be added for this route */
77 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
80 unsigned int mpls_dev_mtu(const struct net_device *dev)
82 /* The amount of data the layer 2 frame can hold */
85 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
87 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
92 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
97 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
99 static u32 mpls_multipath_hash(struct mpls_route *rt,
100 struct sk_buff *skb, bool bos)
102 struct mpls_entry_decoded dec;
103 struct mpls_shim_hdr *hdr;
104 bool eli_seen = false;
108 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
110 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
113 /* Read and decode the current label */
114 hdr = mpls_hdr(skb) + label_index;
115 dec = mpls_entry_decode(hdr);
117 /* RFC6790 - reserved labels MUST NOT be used as keys
118 * for the load-balancing function
120 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
121 hash = jhash_1word(dec.label, hash);
123 /* The entropy label follows the entropy label
124 * indicator, so this means that the entropy
125 * label was just added to the hash - no need to
126 * go any deeper either in the label stack or in the
131 } else if (dec.label == MPLS_LABEL_ENTROPY) {
136 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
137 sizeof(struct iphdr))) {
138 const struct iphdr *v4hdr;
140 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
142 if (v4hdr->version == 4) {
143 hash = jhash_3words(ntohl(v4hdr->saddr),
145 v4hdr->protocol, hash);
146 } else if (v4hdr->version == 6 &&
147 pskb_may_pull(skb, sizeof(*hdr) * label_index +
148 sizeof(struct ipv6hdr))) {
149 const struct ipv6hdr *v6hdr;
151 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
154 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
155 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
156 hash = jhash_1word(v6hdr->nexthdr, hash);
164 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
165 struct sk_buff *skb, bool bos)
167 int alive = ACCESS_ONCE(rt->rt_nhn_alive);
172 /* No need to look further into packet if there's only
181 hash = mpls_multipath_hash(rt, skb, bos);
182 nh_index = hash % alive;
183 if (alive == rt->rt_nhn)
186 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
191 } endfor_nexthops(rt);
194 return &rt->rt_nh[nh_index];
197 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
198 struct mpls_entry_decoded dec)
200 enum mpls_payload_type payload_type;
201 bool success = false;
203 /* The IPv4 code below accesses through the IPv4 header
204 * checksum, which is 12 bytes into the packet.
205 * The IPv6 code below accesses through the IPv6 hop limit
206 * which is 8 bytes into the packet.
208 * For all supported cases there should always be at least 12
209 * bytes of packet data present. The IPv4 header is 20 bytes
210 * without options and the IPv6 header is always 40 bytes
213 if (!pskb_may_pull(skb, 12))
216 payload_type = rt->rt_payload_type;
217 if (payload_type == MPT_UNSPEC)
218 payload_type = ip_hdr(skb)->version;
220 switch (payload_type) {
222 struct iphdr *hdr4 = ip_hdr(skb);
223 skb->protocol = htons(ETH_P_IP);
224 csum_replace2(&hdr4->check,
225 htons(hdr4->ttl << 8),
226 htons(dec.ttl << 8));
232 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
233 skb->protocol = htons(ETH_P_IPV6);
234 hdr6->hop_limit = dec.ttl;
245 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
246 struct packet_type *pt, struct net_device *orig_dev)
248 struct net *net = dev_net(dev);
249 struct mpls_shim_hdr *hdr;
250 struct mpls_route *rt;
252 struct mpls_entry_decoded dec;
253 struct net_device *out_dev;
254 struct mpls_dev *mdev;
256 unsigned int new_header_size;
260 /* Careful this entire function runs inside of an rcu critical section */
262 mdev = mpls_dev_get(dev);
263 if (!mdev || !mdev->input_enabled)
266 if (skb->pkt_type != PACKET_HOST)
269 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
272 if (!pskb_may_pull(skb, sizeof(*hdr)))
275 /* Read and decode the label */
277 dec = mpls_entry_decode(hdr);
280 skb_pull(skb, sizeof(*hdr));
281 skb_reset_network_header(skb);
285 rt = mpls_route_input_rcu(net, dec.label);
289 nh = mpls_select_multipath(rt, skb, dec.bos);
293 /* Find the output device */
294 out_dev = rcu_dereference(nh->nh_dev);
295 if (!mpls_output_possible(out_dev))
298 if (skb_warn_if_lro(skb))
301 skb_forward_csum(skb);
303 /* Verify ttl is valid */
308 /* Verify the destination can hold the packet */
309 new_header_size = mpls_nh_header_size(nh);
310 mtu = mpls_dev_mtu(out_dev);
311 if (mpls_pkt_too_big(skb, mtu - new_header_size))
314 hh_len = LL_RESERVED_SPACE(out_dev);
315 if (!out_dev->header_ops)
318 /* Ensure there is enough space for the headers in the skb */
319 if (skb_cow(skb, hh_len + new_header_size))
323 skb->protocol = htons(ETH_P_MPLS_UC);
325 if (unlikely(!new_header_size && dec.bos)) {
326 /* Penultimate hop popping */
327 if (!mpls_egress(rt, skb, dec))
332 skb_push(skb, new_header_size);
333 skb_reset_network_header(skb);
334 /* Push the new labels */
337 for (i = nh->nh_labels - 1; i >= 0; i--) {
338 hdr[i] = mpls_entry_encode(nh->nh_label[i],
344 err = neigh_xmit(nh->nh_via_table, out_dev, mpls_nh_via(rt, nh), skb);
346 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
355 static struct packet_type mpls_packet_type __read_mostly = {
356 .type = cpu_to_be16(ETH_P_MPLS_UC),
357 .func = mpls_forward,
360 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
361 [RTA_DST] = { .type = NLA_U32 },
362 [RTA_OIF] = { .type = NLA_U32 },
365 struct mpls_route_config {
370 u8 rc_via[MAX_VIA_ALEN];
373 u32 rc_output_label[MAX_NEW_LABELS];
375 enum mpls_payload_type rc_payload_type;
376 struct nl_info rc_nlinfo;
377 struct rtnexthop *rc_mp;
381 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
383 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
384 struct mpls_route *rt;
386 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
388 num_nh * max_alen_aligned,
392 rt->rt_nhn_alive = num_nh;
393 rt->rt_max_alen = max_alen_aligned;
399 static void mpls_rt_free(struct mpls_route *rt)
402 kfree_rcu(rt, rt_rcu);
405 static void mpls_notify_route(struct net *net, unsigned index,
406 struct mpls_route *old, struct mpls_route *new,
407 const struct nl_info *info)
409 struct nlmsghdr *nlh = info ? info->nlh : NULL;
410 unsigned portid = info ? info->portid : 0;
411 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
412 struct mpls_route *rt = new ? new : old;
413 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
414 /* Ignore reserved labels for now */
415 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
416 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
419 static void mpls_route_update(struct net *net, unsigned index,
420 struct mpls_route *new,
421 const struct nl_info *info)
423 struct mpls_route __rcu **platform_label;
424 struct mpls_route *rt;
428 platform_label = rtnl_dereference(net->mpls.platform_label);
429 rt = rtnl_dereference(platform_label[index]);
430 rcu_assign_pointer(platform_label[index], new);
432 mpls_notify_route(net, index, rt, new, info);
434 /* If we removed a route free it now */
438 static unsigned find_free_label(struct net *net)
440 struct mpls_route __rcu **platform_label;
441 size_t platform_labels;
444 platform_label = rtnl_dereference(net->mpls.platform_label);
445 platform_labels = net->mpls.platform_labels;
446 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
448 if (!rtnl_dereference(platform_label[index]))
451 return LABEL_NOT_SPECIFIED;
454 #if IS_ENABLED(CONFIG_INET)
455 static struct net_device *inet_fib_lookup_dev(struct net *net,
458 struct net_device *dev;
460 struct in_addr daddr;
462 memcpy(&daddr, addr, sizeof(struct in_addr));
463 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
475 static struct net_device *inet_fib_lookup_dev(struct net *net,
478 return ERR_PTR(-EAFNOSUPPORT);
482 #if IS_ENABLED(CONFIG_IPV6)
483 static struct net_device *inet6_fib_lookup_dev(struct net *net,
486 struct net_device *dev;
487 struct dst_entry *dst;
492 return ERR_PTR(-EAFNOSUPPORT);
494 memset(&fl6, 0, sizeof(fl6));
495 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
496 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
507 static struct net_device *inet6_fib_lookup_dev(struct net *net,
510 return ERR_PTR(-EAFNOSUPPORT);
514 static struct net_device *find_outdev(struct net *net,
515 struct mpls_route *rt,
516 struct mpls_nh *nh, int oif)
518 struct net_device *dev = NULL;
521 switch (nh->nh_via_table) {
522 case NEIGH_ARP_TABLE:
523 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
526 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
528 case NEIGH_LINK_TABLE:
532 dev = dev_get_by_index(net, oif);
536 return ERR_PTR(-ENODEV);
538 /* The caller is holding rtnl anyways, so release the dev reference */
544 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
545 struct mpls_nh *nh, int oif)
547 struct net_device *dev = NULL;
550 dev = find_outdev(net, rt, nh, oif);
557 /* Ensure this is a supported device */
559 if (!mpls_dev_get(dev))
562 RCU_INIT_POINTER(nh->nh_dev, dev);
564 if (!(dev->flags & IFF_UP)) {
565 nh->nh_flags |= RTNH_F_DEAD;
569 flags = dev_get_flags(dev);
570 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
571 nh->nh_flags |= RTNH_F_LINKDOWN;
580 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
581 struct mpls_route *rt)
583 struct net *net = cfg->rc_nlinfo.nl_net;
584 struct mpls_nh *nh = rt->rt_nh;
592 /* Ensure only a supported number of labels are present */
593 if (cfg->rc_output_labels > MAX_NEW_LABELS)
596 nh->nh_labels = cfg->rc_output_labels;
597 for (i = 0; i < nh->nh_labels; i++)
598 nh->nh_label[i] = cfg->rc_output_label[i];
600 nh->nh_via_table = cfg->rc_via_table;
601 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
602 nh->nh_via_alen = cfg->rc_via_alen;
604 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
608 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
617 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
618 struct mpls_nh *nh, int oif, struct nlattr *via,
619 struct nlattr *newdst)
627 err = nla_get_labels(newdst, MAX_NEW_LABELS,
628 &nh->nh_labels, nh->nh_label);
633 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
634 __mpls_nh_via(rt, nh));
638 err = mpls_nh_assign_dev(net, rt, nh, oif);
648 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
649 u8 cfg_via_alen, u8 *max_via_alen)
655 *max_via_alen = cfg_via_alen;
661 while (rtnh_ok(rtnh, remaining)) {
662 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
665 attrlen = rtnh_attrlen(rtnh);
666 nla = nla_find(attrs, attrlen, RTA_VIA);
667 if (nla && nla_len(nla) >=
668 offsetof(struct rtvia, rtvia_addr)) {
669 int via_alen = nla_len(nla) -
670 offsetof(struct rtvia, rtvia_addr);
672 if (via_alen <= MAX_VIA_ALEN)
673 *max_via_alen = max_t(u16, *max_via_alen,
678 rtnh = rtnh_next(rtnh, &remaining);
681 /* leftover implies invalid nexthop configuration, discard it */
682 return remaining > 0 ? 0 : nhs;
685 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
686 struct mpls_route *rt)
688 struct rtnexthop *rtnh = cfg->rc_mp;
689 struct nlattr *nla_via, *nla_newdst;
690 int remaining = cfg->rc_mp_len;
694 change_nexthops(rt) {
701 if (!rtnh_ok(rtnh, remaining))
704 /* neither weighted multipath nor any flags
707 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
710 attrlen = rtnh_attrlen(rtnh);
712 struct nlattr *attrs = rtnh_attrs(rtnh);
714 nla_via = nla_find(attrs, attrlen, RTA_VIA);
715 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
721 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
722 rtnh->rtnh_ifindex, nla_via, nla_newdst);
726 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
729 rtnh = rtnh_next(rtnh, &remaining);
731 } endfor_nexthops(rt);
741 static int mpls_route_add(struct mpls_route_config *cfg)
743 struct mpls_route __rcu **platform_label;
744 struct net *net = cfg->rc_nlinfo.nl_net;
745 struct mpls_route *rt, *old;
751 index = cfg->rc_label;
753 /* If a label was not specified during insert pick one */
754 if ((index == LABEL_NOT_SPECIFIED) &&
755 (cfg->rc_nlflags & NLM_F_CREATE)) {
756 index = find_free_label(net);
759 /* Reserved labels may not be set */
760 if (index < MPLS_LABEL_FIRST_UNRESERVED)
763 /* The full 20 bit range may not be supported. */
764 if (index >= net->mpls.platform_labels)
767 /* Append makes no sense with mpls */
769 if (cfg->rc_nlflags & NLM_F_APPEND)
773 platform_label = rtnl_dereference(net->mpls.platform_label);
774 old = rtnl_dereference(platform_label[index]);
775 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
779 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
783 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
787 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
788 cfg->rc_via_alen, &max_via_alen);
793 rt = mpls_rt_alloc(nhs, max_via_alen);
797 rt->rt_protocol = cfg->rc_protocol;
798 rt->rt_payload_type = cfg->rc_payload_type;
801 err = mpls_nh_build_multi(cfg, rt);
803 err = mpls_nh_build_from_cfg(cfg, rt);
807 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
817 static int mpls_route_del(struct mpls_route_config *cfg)
819 struct net *net = cfg->rc_nlinfo.nl_net;
823 index = cfg->rc_label;
825 /* Reserved labels may not be removed */
826 if (index < MPLS_LABEL_FIRST_UNRESERVED)
829 /* The full 20 bit range may not be supported */
830 if (index >= net->mpls.platform_labels)
833 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
840 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
841 (&((struct mpls_dev *)0)->field)
843 static const struct ctl_table mpls_dev_table[] = {
846 .maxlen = sizeof(int),
848 .proc_handler = proc_dointvec,
849 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
854 static int mpls_dev_sysctl_register(struct net_device *dev,
855 struct mpls_dev *mdev)
857 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
858 struct ctl_table *table;
861 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
865 /* Table data contains only offsets relative to the base of
866 * the mdev at this point, so make them absolute.
868 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
869 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
871 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
873 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
885 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
887 struct ctl_table *table;
889 table = mdev->sysctl->ctl_table_arg;
890 unregister_net_sysctl_table(mdev->sysctl);
894 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
896 struct mpls_dev *mdev;
901 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
905 err = mpls_dev_sysctl_register(dev, mdev);
909 rcu_assign_pointer(dev->mpls_ptr, mdev);
918 static void mpls_ifdown(struct net_device *dev, int event)
920 struct mpls_route __rcu **platform_label;
921 struct net *net = dev_net(dev);
924 platform_label = rtnl_dereference(net->mpls.platform_label);
925 for (index = 0; index < net->mpls.platform_labels; index++) {
926 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
931 change_nexthops(rt) {
932 if (rtnl_dereference(nh->nh_dev) != dev)
936 case NETDEV_UNREGISTER:
937 nh->nh_flags |= RTNH_F_DEAD;
940 nh->nh_flags |= RTNH_F_LINKDOWN;
941 ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
944 if (event == NETDEV_UNREGISTER)
945 RCU_INIT_POINTER(nh->nh_dev, NULL);
946 } endfor_nexthops(rt);
953 static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
955 struct mpls_route __rcu **platform_label;
956 struct net *net = dev_net(dev);
960 platform_label = rtnl_dereference(net->mpls.platform_label);
961 for (index = 0; index < net->mpls.platform_labels; index++) {
962 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
968 change_nexthops(rt) {
969 struct net_device *nh_dev =
970 rtnl_dereference(nh->nh_dev);
972 if (!(nh->nh_flags & nh_flags)) {
979 nh->nh_flags &= ~nh_flags;
980 } endfor_nexthops(rt);
982 ACCESS_ONCE(rt->rt_nhn_alive) = alive;
988 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
991 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
992 struct mpls_dev *mdev;
995 if (event == NETDEV_REGISTER) {
996 /* For now just support ethernet devices */
997 if ((dev->type == ARPHRD_ETHER) ||
998 (dev->type == ARPHRD_LOOPBACK)) {
999 mdev = mpls_add_dev(dev);
1001 return notifier_from_errno(PTR_ERR(mdev));
1006 mdev = mpls_dev_get(dev);
1012 mpls_ifdown(dev, event);
1015 flags = dev_get_flags(dev);
1016 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1017 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1019 mpls_ifup(dev, RTNH_F_DEAD);
1022 flags = dev_get_flags(dev);
1023 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1024 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1026 mpls_ifdown(dev, event);
1028 case NETDEV_UNREGISTER:
1029 mpls_ifdown(dev, event);
1030 mdev = mpls_dev_get(dev);
1032 mpls_dev_sysctl_unregister(mdev);
1033 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1034 kfree_rcu(mdev, rcu);
1037 case NETDEV_CHANGENAME:
1038 mdev = mpls_dev_get(dev);
1042 mpls_dev_sysctl_unregister(mdev);
1043 err = mpls_dev_sysctl_register(dev, mdev);
1045 return notifier_from_errno(err);
1052 static struct notifier_block mpls_dev_notifier = {
1053 .notifier_call = mpls_dev_notify,
1056 static int nla_put_via(struct sk_buff *skb,
1057 u8 table, const void *addr, int alen)
1059 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1060 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
1064 int family = AF_UNSPEC;
1066 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1070 if (table <= NEIGH_NR_TABLES)
1071 family = table_to_family[table];
1073 via = nla_data(nla);
1074 via->rtvia_family = family;
1075 memcpy(via->rtvia_addr, addr, alen);
1079 int nla_put_labels(struct sk_buff *skb, int attrtype,
1080 u8 labels, const u32 label[])
1083 struct mpls_shim_hdr *nla_label;
1086 nla = nla_reserve(skb, attrtype, labels*4);
1090 nla_label = nla_data(nla);
1092 for (i = labels - 1; i >= 0; i--) {
1093 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1099 EXPORT_SYMBOL_GPL(nla_put_labels);
1101 int nla_get_labels(const struct nlattr *nla,
1102 u32 max_labels, u8 *labels, u32 label[])
1104 unsigned len = nla_len(nla);
1105 unsigned nla_labels;
1106 struct mpls_shim_hdr *nla_label;
1110 /* len needs to be an even multiple of 4 (the label size) */
1114 /* Limit the number of new labels allowed */
1116 if (nla_labels > max_labels)
1119 nla_label = nla_data(nla);
1121 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1122 struct mpls_entry_decoded dec;
1123 dec = mpls_entry_decode(nla_label + i);
1125 /* Ensure the bottom of stack flag is properly set
1126 * and ttl and tc are both clear.
1128 if ((dec.bos != bos) || dec.ttl || dec.tc)
1131 switch (dec.label) {
1132 case MPLS_LABEL_IMPLNULL:
1133 /* RFC3032: This is a label that an LSR may
1134 * assign and distribute, but which never
1135 * actually appears in the encapsulation.
1140 label[i] = dec.label;
1142 *labels = nla_labels;
1145 EXPORT_SYMBOL_GPL(nla_get_labels);
1147 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1148 u8 *via_table, u8 via_addr[])
1150 struct rtvia *via = nla_data(nla);
1154 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1156 alen = nla_len(nla) -
1157 offsetof(struct rtvia, rtvia_addr);
1158 if (alen > MAX_VIA_ALEN)
1161 /* Validate the address family */
1162 switch (via->rtvia_family) {
1164 *via_table = NEIGH_LINK_TABLE;
1167 *via_table = NEIGH_ARP_TABLE;
1172 *via_table = NEIGH_ND_TABLE;
1177 /* Unsupported address family */
1181 memcpy(via_addr, via->rtvia_addr, alen);
1189 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1190 struct mpls_route_config *cfg)
1193 struct nlattr *tb[RTA_MAX+1];
1197 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1202 rtm = nlmsg_data(nlh);
1203 memset(cfg, 0, sizeof(*cfg));
1205 if (rtm->rtm_family != AF_MPLS)
1207 if (rtm->rtm_dst_len != 20)
1209 if (rtm->rtm_src_len != 0)
1211 if (rtm->rtm_tos != 0)
1213 if (rtm->rtm_table != RT_TABLE_MAIN)
1215 /* Any value is acceptable for rtm_protocol */
1217 /* As mpls uses destination specific addresses
1218 * (or source specific address in the case of multicast)
1219 * all addresses have universal scope.
1221 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1223 if (rtm->rtm_type != RTN_UNICAST)
1225 if (rtm->rtm_flags != 0)
1228 cfg->rc_label = LABEL_NOT_SPECIFIED;
1229 cfg->rc_protocol = rtm->rtm_protocol;
1230 cfg->rc_nlflags = nlh->nlmsg_flags;
1231 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1232 cfg->rc_nlinfo.nlh = nlh;
1233 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1235 for (index = 0; index <= RTA_MAX; index++) {
1236 struct nlattr *nla = tb[index];
1242 cfg->rc_ifindex = nla_get_u32(nla);
1245 if (nla_get_labels(nla, MAX_NEW_LABELS,
1246 &cfg->rc_output_labels,
1247 cfg->rc_output_label))
1253 if (nla_get_labels(nla, 1, &label_count,
1257 /* Reserved labels may not be set */
1258 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1265 if (nla_get_via(nla, &cfg->rc_via_alen,
1266 &cfg->rc_via_table, cfg->rc_via))
1272 cfg->rc_mp = nla_data(nla);
1273 cfg->rc_mp_len = nla_len(nla);
1277 /* Unsupported attribute */
1287 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1289 struct mpls_route_config cfg;
1292 err = rtm_to_route_config(skb, nlh, &cfg);
1296 return mpls_route_del(&cfg);
1300 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1302 struct mpls_route_config cfg;
1305 err = rtm_to_route_config(skb, nlh, &cfg);
1309 return mpls_route_add(&cfg);
1312 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1313 u32 label, struct mpls_route *rt, int flags)
1315 struct net_device *dev;
1316 struct nlmsghdr *nlh;
1319 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1323 rtm = nlmsg_data(nlh);
1324 rtm->rtm_family = AF_MPLS;
1325 rtm->rtm_dst_len = 20;
1326 rtm->rtm_src_len = 0;
1328 rtm->rtm_table = RT_TABLE_MAIN;
1329 rtm->rtm_protocol = rt->rt_protocol;
1330 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1331 rtm->rtm_type = RTN_UNICAST;
1334 if (nla_put_labels(skb, RTA_DST, 1, &label))
1335 goto nla_put_failure;
1336 if (rt->rt_nhn == 1) {
1337 const struct mpls_nh *nh = rt->rt_nh;
1339 if (nh->nh_labels &&
1340 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1342 goto nla_put_failure;
1343 if (nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1345 goto nla_put_failure;
1346 dev = rtnl_dereference(nh->nh_dev);
1347 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1348 goto nla_put_failure;
1349 if (nh->nh_flags & RTNH_F_LINKDOWN)
1350 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1351 if (nh->nh_flags & RTNH_F_DEAD)
1352 rtm->rtm_flags |= RTNH_F_DEAD;
1354 struct rtnexthop *rtnh;
1359 mp = nla_nest_start(skb, RTA_MULTIPATH);
1361 goto nla_put_failure;
1364 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1366 goto nla_put_failure;
1368 dev = rtnl_dereference(nh->nh_dev);
1370 rtnh->rtnh_ifindex = dev->ifindex;
1371 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1372 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
1375 if (nh->nh_flags & RTNH_F_DEAD) {
1376 rtnh->rtnh_flags |= RTNH_F_DEAD;
1380 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1383 goto nla_put_failure;
1384 if (nla_put_via(skb, nh->nh_via_table,
1385 mpls_nh_via(rt, nh),
1387 goto nla_put_failure;
1389 /* length of rtnetlink header + attributes */
1390 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1391 } endfor_nexthops(rt);
1393 if (linkdown == rt->rt_nhn)
1394 rtm->rtm_flags |= RTNH_F_LINKDOWN;
1395 if (dead == rt->rt_nhn)
1396 rtm->rtm_flags |= RTNH_F_DEAD;
1398 nla_nest_end(skb, mp);
1401 nlmsg_end(skb, nlh);
1405 nlmsg_cancel(skb, nlh);
1409 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1411 struct net *net = sock_net(skb->sk);
1412 struct mpls_route __rcu **platform_label;
1413 size_t platform_labels;
1418 index = cb->args[0];
1419 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1420 index = MPLS_LABEL_FIRST_UNRESERVED;
1422 platform_label = rtnl_dereference(net->mpls.platform_label);
1423 platform_labels = net->mpls.platform_labels;
1424 for (; index < platform_labels; index++) {
1425 struct mpls_route *rt;
1426 rt = rtnl_dereference(platform_label[index]);
1430 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1431 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1432 index, rt, NLM_F_MULTI) < 0)
1435 cb->args[0] = index;
1440 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1443 NLMSG_ALIGN(sizeof(struct rtmsg))
1444 + nla_total_size(4); /* RTA_DST */
1446 if (rt->rt_nhn == 1) {
1447 struct mpls_nh *nh = rt->rt_nh;
1450 payload += nla_total_size(4); /* RTA_OIF */
1451 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1452 if (nh->nh_labels) /* RTA_NEWDST */
1453 payload += nla_total_size(nh->nh_labels * 4);
1455 /* each nexthop is packed in an attribute */
1459 nhsize += nla_total_size(sizeof(struct rtnexthop));
1460 nhsize += nla_total_size(2 + nh->nh_via_alen);
1462 nhsize += nla_total_size(nh->nh_labels * 4);
1463 } endfor_nexthops(rt);
1464 /* nested attribute */
1465 payload += nla_total_size(nhsize);
1471 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1472 struct nlmsghdr *nlh, struct net *net, u32 portid,
1473 unsigned int nlm_flags)
1475 struct sk_buff *skb;
1476 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1479 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1483 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1485 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1486 WARN_ON(err == -EMSGSIZE);
1490 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1495 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1498 static int resize_platform_label_table(struct net *net, size_t limit)
1500 size_t size = sizeof(struct mpls_route *) * limit;
1503 struct mpls_route __rcu **labels = NULL, **old;
1504 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1508 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1510 labels = vzalloc(size);
1516 /* In case the predefined labels need to be populated */
1517 if (limit > MPLS_LABEL_IPV4NULL) {
1518 struct net_device *lo = net->loopback_dev;
1519 rt0 = mpls_rt_alloc(1, lo->addr_len);
1522 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1523 rt0->rt_protocol = RTPROT_KERNEL;
1524 rt0->rt_payload_type = MPT_IPV4;
1525 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1526 rt0->rt_nh->nh_via_alen = lo->addr_len;
1527 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1530 if (limit > MPLS_LABEL_IPV6NULL) {
1531 struct net_device *lo = net->loopback_dev;
1532 rt2 = mpls_rt_alloc(1, lo->addr_len);
1535 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1536 rt2->rt_protocol = RTPROT_KERNEL;
1537 rt2->rt_payload_type = MPT_IPV6;
1538 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1539 rt2->rt_nh->nh_via_alen = lo->addr_len;
1540 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1545 /* Remember the original table */
1546 old = rtnl_dereference(net->mpls.platform_label);
1547 old_limit = net->mpls.platform_labels;
1549 /* Free any labels beyond the new table */
1550 for (index = limit; index < old_limit; index++)
1551 mpls_route_update(net, index, NULL, NULL);
1553 /* Copy over the old labels */
1555 if (old_limit < limit)
1556 cp_size = old_limit * sizeof(struct mpls_route *);
1558 memcpy(labels, old, cp_size);
1560 /* If needed set the predefined labels */
1561 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1562 (limit > MPLS_LABEL_IPV6NULL)) {
1563 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1567 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1568 (limit > MPLS_LABEL_IPV4NULL)) {
1569 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1573 /* Update the global pointers */
1574 net->mpls.platform_labels = limit;
1575 rcu_assign_pointer(net->mpls.platform_label, labels);
1596 static int mpls_platform_labels(struct ctl_table *table, int write,
1597 void __user *buffer, size_t *lenp, loff_t *ppos)
1599 struct net *net = table->data;
1600 int platform_labels = net->mpls.platform_labels;
1602 struct ctl_table tmp = {
1603 .procname = table->procname,
1604 .data = &platform_labels,
1605 .maxlen = sizeof(int),
1606 .mode = table->mode,
1608 .extra2 = &label_limit,
1611 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1613 if (write && ret == 0)
1614 ret = resize_platform_label_table(net, platform_labels);
1619 static const struct ctl_table mpls_table[] = {
1621 .procname = "platform_labels",
1623 .maxlen = sizeof(int),
1625 .proc_handler = mpls_platform_labels,
1630 static int mpls_net_init(struct net *net)
1632 struct ctl_table *table;
1634 net->mpls.platform_labels = 0;
1635 net->mpls.platform_label = NULL;
1637 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1641 table[0].data = net;
1642 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1643 if (net->mpls.ctl == NULL) {
1651 static void mpls_net_exit(struct net *net)
1653 struct mpls_route __rcu **platform_label;
1654 size_t platform_labels;
1655 struct ctl_table *table;
1658 table = net->mpls.ctl->ctl_table_arg;
1659 unregister_net_sysctl_table(net->mpls.ctl);
1662 /* An rcu grace period has passed since there was a device in
1663 * the network namespace (and thus the last in flight packet)
1664 * left this network namespace. This is because
1665 * unregister_netdevice_many and netdev_run_todo has completed
1666 * for each network device that was in this network namespace.
1668 * As such no additional rcu synchronization is necessary when
1669 * freeing the platform_label table.
1672 platform_label = rtnl_dereference(net->mpls.platform_label);
1673 platform_labels = net->mpls.platform_labels;
1674 for (index = 0; index < platform_labels; index++) {
1675 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1676 RCU_INIT_POINTER(platform_label[index], NULL);
1681 kvfree(platform_label);
1684 static struct pernet_operations mpls_net_ops = {
1685 .init = mpls_net_init,
1686 .exit = mpls_net_exit,
1689 static int __init mpls_init(void)
1693 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1695 err = register_pernet_subsys(&mpls_net_ops);
1699 err = register_netdevice_notifier(&mpls_dev_notifier);
1701 goto out_unregister_pernet;
1703 dev_add_pack(&mpls_packet_type);
1705 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1706 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1707 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1712 out_unregister_pernet:
1713 unregister_pernet_subsys(&mpls_net_ops);
1716 module_init(mpls_init);
1718 static void __exit mpls_exit(void)
1720 rtnl_unregister_all(PF_MPLS);
1721 dev_remove_pack(&mpls_packet_type);
1722 unregister_netdevice_notifier(&mpls_dev_notifier);
1723 unregister_pernet_subsys(&mpls_net_ops);
1725 module_exit(mpls_exit);
1727 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1728 MODULE_LICENSE("GPL v2");
1729 MODULE_ALIAS_NETPROTO(PF_MPLS);