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_network_seglen(skb) <= mtu)
100 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
102 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
103 struct sk_buff *skb, bool bos)
105 struct mpls_entry_decoded dec;
106 struct mpls_shim_hdr *hdr;
107 bool eli_seen = false;
112 /* No need to look further into packet if there's only
118 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
120 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
123 /* Read and decode the current label */
124 hdr = mpls_hdr(skb) + label_index;
125 dec = mpls_entry_decode(hdr);
127 /* RFC6790 - reserved labels MUST NOT be used as keys
128 * for the load-balancing function
130 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
131 hash = jhash_1word(dec.label, hash);
133 /* The entropy label follows the entropy label
134 * indicator, so this means that the entropy
135 * label was just added to the hash - no need to
136 * go any deeper either in the label stack or in the
141 } else if (dec.label == MPLS_LABEL_ENTROPY) {
146 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
147 sizeof(struct iphdr))) {
148 const struct iphdr *v4hdr;
150 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
152 if (v4hdr->version == 4) {
153 hash = jhash_3words(ntohl(v4hdr->saddr),
155 v4hdr->protocol, hash);
156 } else if (v4hdr->version == 6 &&
157 pskb_may_pull(skb, sizeof(*hdr) * label_index +
158 sizeof(struct ipv6hdr))) {
159 const struct ipv6hdr *v6hdr;
161 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
164 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
165 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
166 hash = jhash_1word(v6hdr->nexthdr, hash);
171 nh_index = hash % rt->rt_nhn;
173 return &rt->rt_nh[nh_index];
176 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
177 struct mpls_entry_decoded dec)
179 enum mpls_payload_type payload_type;
180 bool success = false;
182 /* The IPv4 code below accesses through the IPv4 header
183 * checksum, which is 12 bytes into the packet.
184 * The IPv6 code below accesses through the IPv6 hop limit
185 * which is 8 bytes into the packet.
187 * For all supported cases there should always be at least 12
188 * bytes of packet data present. The IPv4 header is 20 bytes
189 * without options and the IPv6 header is always 40 bytes
192 if (!pskb_may_pull(skb, 12))
195 payload_type = rt->rt_payload_type;
196 if (payload_type == MPT_UNSPEC)
197 payload_type = ip_hdr(skb)->version;
199 switch (payload_type) {
201 struct iphdr *hdr4 = ip_hdr(skb);
202 skb->protocol = htons(ETH_P_IP);
203 csum_replace2(&hdr4->check,
204 htons(hdr4->ttl << 8),
205 htons(dec.ttl << 8));
211 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
212 skb->protocol = htons(ETH_P_IPV6);
213 hdr6->hop_limit = dec.ttl;
224 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
225 struct packet_type *pt, struct net_device *orig_dev)
227 struct net *net = dev_net(dev);
228 struct mpls_shim_hdr *hdr;
229 struct mpls_route *rt;
231 struct mpls_entry_decoded dec;
232 struct net_device *out_dev;
233 struct mpls_dev *mdev;
235 unsigned int new_header_size;
239 /* Careful this entire function runs inside of an rcu critical section */
241 mdev = mpls_dev_get(dev);
242 if (!mdev || !mdev->input_enabled)
245 if (skb->pkt_type != PACKET_HOST)
248 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
251 if (!pskb_may_pull(skb, sizeof(*hdr)))
254 /* Read and decode the label */
256 dec = mpls_entry_decode(hdr);
259 skb_pull(skb, sizeof(*hdr));
260 skb_reset_network_header(skb);
264 rt = mpls_route_input_rcu(net, dec.label);
268 nh = mpls_select_multipath(rt, skb, dec.bos);
272 /* Find the output device */
273 out_dev = rcu_dereference(nh->nh_dev);
274 if (!mpls_output_possible(out_dev))
277 if (skb_warn_if_lro(skb))
280 skb_forward_csum(skb);
282 /* Verify ttl is valid */
287 /* Verify the destination can hold the packet */
288 new_header_size = mpls_nh_header_size(nh);
289 mtu = mpls_dev_mtu(out_dev);
290 if (mpls_pkt_too_big(skb, mtu - new_header_size))
293 hh_len = LL_RESERVED_SPACE(out_dev);
294 if (!out_dev->header_ops)
297 /* Ensure there is enough space for the headers in the skb */
298 if (skb_cow(skb, hh_len + new_header_size))
302 skb->protocol = htons(ETH_P_MPLS_UC);
304 if (unlikely(!new_header_size && dec.bos)) {
305 /* Penultimate hop popping */
306 if (!mpls_egress(rt, skb, dec))
311 skb_push(skb, new_header_size);
312 skb_reset_network_header(skb);
313 /* Push the new labels */
316 for (i = nh->nh_labels - 1; i >= 0; i--) {
317 hdr[i] = mpls_entry_encode(nh->nh_label[i],
323 /* If via wasn't specified then send out using device address */
324 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
325 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
326 out_dev->dev_addr, skb);
328 err = neigh_xmit(nh->nh_via_table, out_dev,
329 mpls_nh_via(rt, nh), skb);
331 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
340 static struct packet_type mpls_packet_type __read_mostly = {
341 .type = cpu_to_be16(ETH_P_MPLS_UC),
342 .func = mpls_forward,
345 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
346 [RTA_DST] = { .type = NLA_U32 },
347 [RTA_OIF] = { .type = NLA_U32 },
350 struct mpls_route_config {
355 u8 rc_via[MAX_VIA_ALEN];
358 u32 rc_output_label[MAX_NEW_LABELS];
360 enum mpls_payload_type rc_payload_type;
361 struct nl_info rc_nlinfo;
362 struct rtnexthop *rc_mp;
366 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
368 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
369 struct mpls_route *rt;
371 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
373 num_nh * max_alen_aligned,
377 rt->rt_max_alen = max_alen_aligned;
383 static void mpls_rt_free(struct mpls_route *rt)
386 kfree_rcu(rt, rt_rcu);
389 static void mpls_notify_route(struct net *net, unsigned index,
390 struct mpls_route *old, struct mpls_route *new,
391 const struct nl_info *info)
393 struct nlmsghdr *nlh = info ? info->nlh : NULL;
394 unsigned portid = info ? info->portid : 0;
395 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
396 struct mpls_route *rt = new ? new : old;
397 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
398 /* Ignore reserved labels for now */
399 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
400 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
403 static void mpls_route_update(struct net *net, unsigned index,
404 struct mpls_route *new,
405 const struct nl_info *info)
407 struct mpls_route __rcu **platform_label;
408 struct mpls_route *rt;
412 platform_label = rtnl_dereference(net->mpls.platform_label);
413 rt = rtnl_dereference(platform_label[index]);
414 rcu_assign_pointer(platform_label[index], new);
416 mpls_notify_route(net, index, rt, new, info);
418 /* If we removed a route free it now */
422 static unsigned find_free_label(struct net *net)
424 struct mpls_route __rcu **platform_label;
425 size_t platform_labels;
428 platform_label = rtnl_dereference(net->mpls.platform_label);
429 platform_labels = net->mpls.platform_labels;
430 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
432 if (!rtnl_dereference(platform_label[index]))
435 return LABEL_NOT_SPECIFIED;
438 #if IS_ENABLED(CONFIG_INET)
439 static struct net_device *inet_fib_lookup_dev(struct net *net,
442 struct net_device *dev;
444 struct in_addr daddr;
446 memcpy(&daddr, addr, sizeof(struct in_addr));
447 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
459 static struct net_device *inet_fib_lookup_dev(struct net *net,
462 return ERR_PTR(-EAFNOSUPPORT);
466 #if IS_ENABLED(CONFIG_IPV6)
467 static struct net_device *inet6_fib_lookup_dev(struct net *net,
470 struct net_device *dev;
471 struct dst_entry *dst;
475 return ERR_PTR(-EAFNOSUPPORT);
477 memset(&fl6, 0, sizeof(fl6));
478 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
479 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
481 return ERR_CAST(dst);
490 static struct net_device *inet6_fib_lookup_dev(struct net *net,
493 return ERR_PTR(-EAFNOSUPPORT);
497 static struct net_device *find_outdev(struct net *net,
498 struct mpls_route *rt,
499 struct mpls_nh *nh, int oif)
501 struct net_device *dev = NULL;
504 switch (nh->nh_via_table) {
505 case NEIGH_ARP_TABLE:
506 dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
509 dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
511 case NEIGH_LINK_TABLE:
515 dev = dev_get_by_index(net, oif);
519 return ERR_PTR(-ENODEV);
524 /* The caller is holding rtnl anyways, so release the dev reference */
530 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
531 struct mpls_nh *nh, int oif)
533 struct net_device *dev = NULL;
536 dev = find_outdev(net, rt, nh, oif);
543 /* Ensure this is a supported device */
545 if (!mpls_dev_get(dev))
548 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
549 (dev->addr_len != nh->nh_via_alen))
552 RCU_INIT_POINTER(nh->nh_dev, dev);
560 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
561 struct mpls_route *rt)
563 struct net *net = cfg->rc_nlinfo.nl_net;
564 struct mpls_nh *nh = rt->rt_nh;
572 /* Ensure only a supported number of labels are present */
573 if (cfg->rc_output_labels > MAX_NEW_LABELS)
576 nh->nh_labels = cfg->rc_output_labels;
577 for (i = 0; i < nh->nh_labels; i++)
578 nh->nh_label[i] = cfg->rc_output_label[i];
580 nh->nh_via_table = cfg->rc_via_table;
581 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
582 nh->nh_via_alen = cfg->rc_via_alen;
584 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
594 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
595 struct mpls_nh *nh, int oif,
596 struct nlattr *via, struct nlattr *newdst)
604 err = nla_get_labels(newdst, MAX_NEW_LABELS,
605 &nh->nh_labels, nh->nh_label);
611 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
612 __mpls_nh_via(rt, nh));
616 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
619 err = mpls_nh_assign_dev(net, rt, nh, oif);
629 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
630 u8 cfg_via_alen, u8 *max_via_alen)
636 *max_via_alen = cfg_via_alen;
642 while (rtnh_ok(rtnh, remaining)) {
643 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
646 attrlen = rtnh_attrlen(rtnh);
647 nla = nla_find(attrs, attrlen, RTA_VIA);
648 if (nla && nla_len(nla) >=
649 offsetof(struct rtvia, rtvia_addr)) {
650 int via_alen = nla_len(nla) -
651 offsetof(struct rtvia, rtvia_addr);
653 if (via_alen <= MAX_VIA_ALEN)
654 *max_via_alen = max_t(u16, *max_via_alen,
659 rtnh = rtnh_next(rtnh, &remaining);
662 /* leftover implies invalid nexthop configuration, discard it */
663 return remaining > 0 ? 0 : nhs;
666 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
667 struct mpls_route *rt)
669 struct rtnexthop *rtnh = cfg->rc_mp;
670 struct nlattr *nla_via, *nla_newdst;
671 int remaining = cfg->rc_mp_len;
675 change_nexthops(rt) {
682 if (!rtnh_ok(rtnh, remaining))
685 /* neither weighted multipath nor any flags
688 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
691 attrlen = rtnh_attrlen(rtnh);
693 struct nlattr *attrs = rtnh_attrs(rtnh);
695 nla_via = nla_find(attrs, attrlen, RTA_VIA);
696 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
699 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
700 rtnh->rtnh_ifindex, nla_via,
705 rtnh = rtnh_next(rtnh, &remaining);
707 } endfor_nexthops(rt);
717 static bool mpls_label_ok(struct net *net, unsigned int *index)
721 /* Reserved labels may not be set */
722 if (*index < MPLS_LABEL_FIRST_UNRESERVED)
725 /* The full 20 bit range may not be supported. */
726 if (is_ok && *index >= net->mpls.platform_labels)
729 *index = array_index_nospec(*index, net->mpls.platform_labels);
733 static int mpls_route_add(struct mpls_route_config *cfg)
735 struct mpls_route __rcu **platform_label;
736 struct net *net = cfg->rc_nlinfo.nl_net;
737 struct mpls_route *rt, *old;
743 index = cfg->rc_label;
745 /* If a label was not specified during insert pick one */
746 if ((index == LABEL_NOT_SPECIFIED) &&
747 (cfg->rc_nlflags & NLM_F_CREATE)) {
748 index = find_free_label(net);
751 if (!mpls_label_ok(net, &index))
754 /* Append makes no sense with mpls */
756 if (cfg->rc_nlflags & NLM_F_APPEND)
760 platform_label = rtnl_dereference(net->mpls.platform_label);
761 old = rtnl_dereference(platform_label[index]);
762 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
766 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
770 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
774 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
775 cfg->rc_via_alen, &max_via_alen);
780 rt = mpls_rt_alloc(nhs, max_via_alen);
784 rt->rt_protocol = cfg->rc_protocol;
785 rt->rt_payload_type = cfg->rc_payload_type;
788 err = mpls_nh_build_multi(cfg, rt);
790 err = mpls_nh_build_from_cfg(cfg, rt);
794 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
804 static int mpls_route_del(struct mpls_route_config *cfg)
806 struct net *net = cfg->rc_nlinfo.nl_net;
810 index = cfg->rc_label;
812 if (!mpls_label_ok(net, &index))
815 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
822 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
823 (&((struct mpls_dev *)0)->field)
825 static const struct ctl_table mpls_dev_table[] = {
828 .maxlen = sizeof(int),
830 .proc_handler = proc_dointvec,
831 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
836 static int mpls_dev_sysctl_register(struct net_device *dev,
837 struct mpls_dev *mdev)
839 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
840 struct ctl_table *table;
843 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
847 /* Table data contains only offsets relative to the base of
848 * the mdev at this point, so make them absolute.
850 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
851 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
853 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
855 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
867 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
869 struct ctl_table *table;
871 table = mdev->sysctl->ctl_table_arg;
872 unregister_net_sysctl_table(mdev->sysctl);
876 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
878 struct mpls_dev *mdev;
883 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
887 err = mpls_dev_sysctl_register(dev, mdev);
891 rcu_assign_pointer(dev->mpls_ptr, mdev);
900 static void mpls_ifdown(struct net_device *dev)
902 struct mpls_route __rcu **platform_label;
903 struct net *net = dev_net(dev);
904 struct mpls_dev *mdev;
907 platform_label = rtnl_dereference(net->mpls.platform_label);
908 for (index = 0; index < net->mpls.platform_labels; index++) {
909 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
913 if (rtnl_dereference(nh->nh_dev) != dev)
916 } endfor_nexthops(rt);
919 mdev = mpls_dev_get(dev);
923 mpls_dev_sysctl_unregister(mdev);
925 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
927 kfree_rcu(mdev, rcu);
930 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
933 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
934 struct mpls_dev *mdev;
937 case NETDEV_REGISTER:
938 /* For now just support ethernet devices */
939 if ((dev->type == ARPHRD_ETHER) ||
940 (dev->type == ARPHRD_LOOPBACK)) {
941 mdev = mpls_add_dev(dev);
943 return notifier_from_errno(PTR_ERR(mdev));
947 case NETDEV_UNREGISTER:
950 case NETDEV_CHANGENAME:
951 mdev = mpls_dev_get(dev);
955 mpls_dev_sysctl_unregister(mdev);
956 err = mpls_dev_sysctl_register(dev, mdev);
958 return notifier_from_errno(err);
965 static struct notifier_block mpls_dev_notifier = {
966 .notifier_call = mpls_dev_notify,
969 static int nla_put_via(struct sk_buff *skb,
970 u8 table, const void *addr, int alen)
972 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
973 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
977 int family = AF_UNSPEC;
979 nla = nla_reserve(skb, RTA_VIA, alen + 2);
983 if (table <= NEIGH_NR_TABLES)
984 family = table_to_family[table];
987 via->rtvia_family = family;
988 memcpy(via->rtvia_addr, addr, alen);
992 int nla_put_labels(struct sk_buff *skb, int attrtype,
993 u8 labels, const u32 label[])
996 struct mpls_shim_hdr *nla_label;
999 nla = nla_reserve(skb, attrtype, labels*4);
1003 nla_label = nla_data(nla);
1005 for (i = labels - 1; i >= 0; i--) {
1006 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1012 EXPORT_SYMBOL_GPL(nla_put_labels);
1014 int nla_get_labels(const struct nlattr *nla,
1015 u32 max_labels, u8 *labels, u32 label[])
1017 unsigned len = nla_len(nla);
1018 unsigned nla_labels;
1019 struct mpls_shim_hdr *nla_label;
1023 /* len needs to be an even multiple of 4 (the label size) */
1027 /* Limit the number of new labels allowed */
1029 if (nla_labels > max_labels)
1032 nla_label = nla_data(nla);
1034 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1035 struct mpls_entry_decoded dec;
1036 dec = mpls_entry_decode(nla_label + i);
1038 /* Ensure the bottom of stack flag is properly set
1039 * and ttl and tc are both clear.
1041 if ((dec.bos != bos) || dec.ttl || dec.tc)
1044 switch (dec.label) {
1045 case MPLS_LABEL_IMPLNULL:
1046 /* RFC3032: This is a label that an LSR may
1047 * assign and distribute, but which never
1048 * actually appears in the encapsulation.
1053 label[i] = dec.label;
1055 *labels = nla_labels;
1058 EXPORT_SYMBOL_GPL(nla_get_labels);
1060 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1061 u8 *via_table, u8 via_addr[])
1063 struct rtvia *via = nla_data(nla);
1067 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1069 alen = nla_len(nla) -
1070 offsetof(struct rtvia, rtvia_addr);
1071 if (alen > MAX_VIA_ALEN)
1074 /* Validate the address family */
1075 switch (via->rtvia_family) {
1077 *via_table = NEIGH_LINK_TABLE;
1080 *via_table = NEIGH_ARP_TABLE;
1085 *via_table = NEIGH_ND_TABLE;
1090 /* Unsupported address family */
1094 memcpy(via_addr, via->rtvia_addr, alen);
1102 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1103 struct mpls_route_config *cfg)
1106 struct nlattr *tb[RTA_MAX+1];
1110 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1115 rtm = nlmsg_data(nlh);
1116 memset(cfg, 0, sizeof(*cfg));
1118 if (rtm->rtm_family != AF_MPLS)
1120 if (rtm->rtm_dst_len != 20)
1122 if (rtm->rtm_src_len != 0)
1124 if (rtm->rtm_tos != 0)
1126 if (rtm->rtm_table != RT_TABLE_MAIN)
1128 /* Any value is acceptable for rtm_protocol */
1130 /* As mpls uses destination specific addresses
1131 * (or source specific address in the case of multicast)
1132 * all addresses have universal scope.
1134 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1136 if (rtm->rtm_type != RTN_UNICAST)
1138 if (rtm->rtm_flags != 0)
1141 cfg->rc_label = LABEL_NOT_SPECIFIED;
1142 cfg->rc_protocol = rtm->rtm_protocol;
1143 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
1144 cfg->rc_nlflags = nlh->nlmsg_flags;
1145 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1146 cfg->rc_nlinfo.nlh = nlh;
1147 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1149 for (index = 0; index <= RTA_MAX; index++) {
1150 struct nlattr *nla = tb[index];
1156 cfg->rc_ifindex = nla_get_u32(nla);
1159 if (nla_get_labels(nla, MAX_NEW_LABELS,
1160 &cfg->rc_output_labels,
1161 cfg->rc_output_label))
1167 if (nla_get_labels(nla, 1, &label_count,
1171 if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1178 if (nla_get_via(nla, &cfg->rc_via_alen,
1179 &cfg->rc_via_table, cfg->rc_via))
1185 cfg->rc_mp = nla_data(nla);
1186 cfg->rc_mp_len = nla_len(nla);
1190 /* Unsupported attribute */
1200 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1202 struct mpls_route_config cfg;
1205 err = rtm_to_route_config(skb, nlh, &cfg);
1209 return mpls_route_del(&cfg);
1213 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1215 struct mpls_route_config cfg;
1218 err = rtm_to_route_config(skb, nlh, &cfg);
1222 return mpls_route_add(&cfg);
1225 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1226 u32 label, struct mpls_route *rt, int flags)
1228 struct net_device *dev;
1229 struct nlmsghdr *nlh;
1232 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1236 rtm = nlmsg_data(nlh);
1237 rtm->rtm_family = AF_MPLS;
1238 rtm->rtm_dst_len = 20;
1239 rtm->rtm_src_len = 0;
1241 rtm->rtm_table = RT_TABLE_MAIN;
1242 rtm->rtm_protocol = rt->rt_protocol;
1243 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1244 rtm->rtm_type = RTN_UNICAST;
1247 if (nla_put_labels(skb, RTA_DST, 1, &label))
1248 goto nla_put_failure;
1249 if (rt->rt_nhn == 1) {
1250 const struct mpls_nh *nh = rt->rt_nh;
1252 if (nh->nh_labels &&
1253 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1255 goto nla_put_failure;
1256 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1257 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1259 goto nla_put_failure;
1260 dev = rtnl_dereference(nh->nh_dev);
1261 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1262 goto nla_put_failure;
1264 struct rtnexthop *rtnh;
1267 mp = nla_nest_start(skb, RTA_MULTIPATH);
1269 goto nla_put_failure;
1272 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1274 goto nla_put_failure;
1276 dev = rtnl_dereference(nh->nh_dev);
1278 rtnh->rtnh_ifindex = dev->ifindex;
1279 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1282 goto nla_put_failure;
1283 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1284 nla_put_via(skb, nh->nh_via_table,
1285 mpls_nh_via(rt, nh),
1287 goto nla_put_failure;
1289 /* length of rtnetlink header + attributes */
1290 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1291 } endfor_nexthops(rt);
1293 nla_nest_end(skb, mp);
1296 nlmsg_end(skb, nlh);
1300 nlmsg_cancel(skb, nlh);
1304 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1306 struct net *net = sock_net(skb->sk);
1307 struct mpls_route __rcu **platform_label;
1308 size_t platform_labels;
1313 index = cb->args[0];
1314 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1315 index = MPLS_LABEL_FIRST_UNRESERVED;
1317 platform_label = rtnl_dereference(net->mpls.platform_label);
1318 platform_labels = net->mpls.platform_labels;
1319 for (; index < platform_labels; index++) {
1320 struct mpls_route *rt;
1321 rt = rtnl_dereference(platform_label[index]);
1325 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1326 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1327 index, rt, NLM_F_MULTI) < 0)
1330 cb->args[0] = index;
1335 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1338 NLMSG_ALIGN(sizeof(struct rtmsg))
1339 + nla_total_size(4); /* RTA_DST */
1341 if (rt->rt_nhn == 1) {
1342 struct mpls_nh *nh = rt->rt_nh;
1345 payload += nla_total_size(4); /* RTA_OIF */
1346 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1347 payload += nla_total_size(2 + nh->nh_via_alen);
1348 if (nh->nh_labels) /* RTA_NEWDST */
1349 payload += nla_total_size(nh->nh_labels * 4);
1351 /* each nexthop is packed in an attribute */
1355 nhsize += nla_total_size(sizeof(struct rtnexthop));
1357 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1358 nhsize += nla_total_size(2 + nh->nh_via_alen);
1360 nhsize += nla_total_size(nh->nh_labels * 4);
1361 } endfor_nexthops(rt);
1362 /* nested attribute */
1363 payload += nla_total_size(nhsize);
1369 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1370 struct nlmsghdr *nlh, struct net *net, u32 portid,
1371 unsigned int nlm_flags)
1373 struct sk_buff *skb;
1374 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1377 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1381 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1383 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1384 WARN_ON(err == -EMSGSIZE);
1388 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1393 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1396 static int resize_platform_label_table(struct net *net, size_t limit)
1398 size_t size = sizeof(struct mpls_route *) * limit;
1401 struct mpls_route __rcu **labels = NULL, **old;
1402 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1406 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1408 labels = vzalloc(size);
1414 /* In case the predefined labels need to be populated */
1415 if (limit > MPLS_LABEL_IPV4NULL) {
1416 struct net_device *lo = net->loopback_dev;
1417 rt0 = mpls_rt_alloc(1, lo->addr_len);
1420 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1421 rt0->rt_protocol = RTPROT_KERNEL;
1422 rt0->rt_payload_type = MPT_IPV4;
1423 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1424 rt0->rt_nh->nh_via_alen = lo->addr_len;
1425 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1428 if (limit > MPLS_LABEL_IPV6NULL) {
1429 struct net_device *lo = net->loopback_dev;
1430 rt2 = mpls_rt_alloc(1, lo->addr_len);
1433 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1434 rt2->rt_protocol = RTPROT_KERNEL;
1435 rt2->rt_payload_type = MPT_IPV6;
1436 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1437 rt2->rt_nh->nh_via_alen = lo->addr_len;
1438 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1443 /* Remember the original table */
1444 old = rtnl_dereference(net->mpls.platform_label);
1445 old_limit = net->mpls.platform_labels;
1447 /* Free any labels beyond the new table */
1448 for (index = limit; index < old_limit; index++)
1449 mpls_route_update(net, index, NULL, NULL);
1451 /* Copy over the old labels */
1453 if (old_limit < limit)
1454 cp_size = old_limit * sizeof(struct mpls_route *);
1456 memcpy(labels, old, cp_size);
1458 /* If needed set the predefined labels */
1459 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1460 (limit > MPLS_LABEL_IPV6NULL)) {
1461 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1465 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1466 (limit > MPLS_LABEL_IPV4NULL)) {
1467 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1471 /* Update the global pointers */
1472 net->mpls.platform_labels = limit;
1473 rcu_assign_pointer(net->mpls.platform_label, labels);
1494 static int mpls_platform_labels(struct ctl_table *table, int write,
1495 void __user *buffer, size_t *lenp, loff_t *ppos)
1497 struct net *net = table->data;
1498 int platform_labels = net->mpls.platform_labels;
1500 struct ctl_table tmp = {
1501 .procname = table->procname,
1502 .data = &platform_labels,
1503 .maxlen = sizeof(int),
1504 .mode = table->mode,
1506 .extra2 = &label_limit,
1509 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1511 if (write && ret == 0)
1512 ret = resize_platform_label_table(net, platform_labels);
1517 static const struct ctl_table mpls_table[] = {
1519 .procname = "platform_labels",
1521 .maxlen = sizeof(int),
1523 .proc_handler = mpls_platform_labels,
1528 static int mpls_net_init(struct net *net)
1530 struct ctl_table *table;
1532 net->mpls.platform_labels = 0;
1533 net->mpls.platform_label = NULL;
1535 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1539 table[0].data = net;
1540 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1541 if (net->mpls.ctl == NULL) {
1549 static void mpls_net_exit(struct net *net)
1551 struct mpls_route __rcu **platform_label;
1552 size_t platform_labels;
1553 struct ctl_table *table;
1556 table = net->mpls.ctl->ctl_table_arg;
1557 unregister_net_sysctl_table(net->mpls.ctl);
1560 /* An rcu grace period has passed since there was a device in
1561 * the network namespace (and thus the last in flight packet)
1562 * left this network namespace. This is because
1563 * unregister_netdevice_many and netdev_run_todo has completed
1564 * for each network device that was in this network namespace.
1566 * As such no additional rcu synchronization is necessary when
1567 * freeing the platform_label table.
1570 platform_label = rtnl_dereference(net->mpls.platform_label);
1571 platform_labels = net->mpls.platform_labels;
1572 for (index = 0; index < platform_labels; index++) {
1573 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1574 RCU_INIT_POINTER(platform_label[index], NULL);
1575 mpls_notify_route(net, index, rt, NULL, NULL);
1580 kvfree(platform_label);
1583 static struct pernet_operations mpls_net_ops = {
1584 .init = mpls_net_init,
1585 .exit = mpls_net_exit,
1588 static int __init mpls_init(void)
1592 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1594 err = register_pernet_subsys(&mpls_net_ops);
1598 err = register_netdevice_notifier(&mpls_dev_notifier);
1600 goto out_unregister_pernet;
1602 dev_add_pack(&mpls_packet_type);
1604 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1605 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1606 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1611 out_unregister_pernet:
1612 unregister_pernet_subsys(&mpls_net_ops);
1615 module_init(mpls_init);
1617 static void __exit mpls_exit(void)
1619 rtnl_unregister_all(PF_MPLS);
1620 dev_remove_pack(&mpls_packet_type);
1621 unregister_netdevice_notifier(&mpls_dev_notifier);
1622 unregister_pernet_subsys(&mpls_net_ops);
1624 module_exit(mpls_exit);
1626 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1627 MODULE_LICENSE("GPL v2");
1628 MODULE_ALIAS_NETPROTO(PF_MPLS);