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
30 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
33 static int label_limit = (1 << 20) - 1;
35 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
36 struct nlmsghdr *nlh, struct net *net, u32 portid,
37 unsigned int nlm_flags);
39 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
41 struct mpls_route *rt = NULL;
43 if (index < net->mpls.platform_labels) {
44 struct mpls_route __rcu **platform_label =
45 rcu_dereference(net->mpls.platform_label);
46 rt = rcu_dereference(platform_label[index]);
51 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
53 return rcu_dereference_rtnl(dev->mpls_ptr);
56 bool mpls_output_possible(const struct net_device *dev)
58 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
60 EXPORT_SYMBOL_GPL(mpls_output_possible);
62 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
64 u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
65 int nh_index = nh - rt->rt_nh;
67 return nh0_via + rt->rt_max_alen * nh_index;
70 static const u8 *mpls_nh_via(const struct mpls_route *rt,
71 const struct mpls_nh *nh)
73 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
76 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
78 /* The size of the layer 2.5 labels to be added for this route */
79 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
82 unsigned int mpls_dev_mtu(const struct net_device *dev)
84 /* The amount of data the layer 2 frame can hold */
87 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
89 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
94 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
99 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
101 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
102 struct sk_buff *skb, bool bos)
104 struct mpls_entry_decoded dec;
105 struct mpls_shim_hdr *hdr;
106 bool eli_seen = false;
111 /* No need to look further into packet if there's only
117 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
119 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
122 /* Read and decode the current label */
123 hdr = mpls_hdr(skb) + label_index;
124 dec = mpls_entry_decode(hdr);
126 /* RFC6790 - reserved labels MUST NOT be used as keys
127 * for the load-balancing function
129 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
130 hash = jhash_1word(dec.label, hash);
132 /* The entropy label follows the entropy label
133 * indicator, so this means that the entropy
134 * label was just added to the hash - no need to
135 * go any deeper either in the label stack or in the
140 } else if (dec.label == MPLS_LABEL_ENTROPY) {
145 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
146 sizeof(struct iphdr))) {
147 const struct iphdr *v4hdr;
149 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
151 if (v4hdr->version == 4) {
152 hash = jhash_3words(ntohl(v4hdr->saddr),
154 v4hdr->protocol, hash);
155 } else if (v4hdr->version == 6 &&
156 pskb_may_pull(skb, sizeof(*hdr) * label_index +
157 sizeof(struct ipv6hdr))) {
158 const struct ipv6hdr *v6hdr;
160 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
163 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
164 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
165 hash = jhash_1word(v6hdr->nexthdr, hash);
170 nh_index = hash % rt->rt_nhn;
172 return &rt->rt_nh[nh_index];
175 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
176 struct mpls_entry_decoded dec)
178 enum mpls_payload_type payload_type;
179 bool success = false;
181 /* The IPv4 code below accesses through the IPv4 header
182 * checksum, which is 12 bytes into the packet.
183 * The IPv6 code below accesses through the IPv6 hop limit
184 * which is 8 bytes into the packet.
186 * For all supported cases there should always be at least 12
187 * bytes of packet data present. The IPv4 header is 20 bytes
188 * without options and the IPv6 header is always 40 bytes
191 if (!pskb_may_pull(skb, 12))
194 payload_type = rt->rt_payload_type;
195 if (payload_type == MPT_UNSPEC)
196 payload_type = ip_hdr(skb)->version;
198 switch (payload_type) {
200 struct iphdr *hdr4 = ip_hdr(skb);
201 skb->protocol = htons(ETH_P_IP);
202 csum_replace2(&hdr4->check,
203 htons(hdr4->ttl << 8),
204 htons(dec.ttl << 8));
210 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
211 skb->protocol = htons(ETH_P_IPV6);
212 hdr6->hop_limit = dec.ttl;
223 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
224 struct packet_type *pt, struct net_device *orig_dev)
226 struct net *net = dev_net(dev);
227 struct mpls_shim_hdr *hdr;
228 struct mpls_route *rt;
230 struct mpls_entry_decoded dec;
231 struct net_device *out_dev;
232 struct mpls_dev *mdev;
234 unsigned int new_header_size;
238 /* Careful this entire function runs inside of an rcu critical section */
240 mdev = mpls_dev_get(dev);
241 if (!mdev || !mdev->input_enabled)
244 if (skb->pkt_type != PACKET_HOST)
247 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
250 if (!pskb_may_pull(skb, sizeof(*hdr)))
253 /* Read and decode the label */
255 dec = mpls_entry_decode(hdr);
258 skb_pull(skb, sizeof(*hdr));
259 skb_reset_network_header(skb);
263 rt = mpls_route_input_rcu(net, dec.label);
267 nh = mpls_select_multipath(rt, skb, dec.bos);
271 /* Find the output device */
272 out_dev = rcu_dereference(nh->nh_dev);
273 if (!mpls_output_possible(out_dev))
276 if (skb_warn_if_lro(skb))
279 skb_forward_csum(skb);
281 /* Verify ttl is valid */
286 /* Verify the destination can hold the packet */
287 new_header_size = mpls_nh_header_size(nh);
288 mtu = mpls_dev_mtu(out_dev);
289 if (mpls_pkt_too_big(skb, mtu - new_header_size))
292 hh_len = LL_RESERVED_SPACE(out_dev);
293 if (!out_dev->header_ops)
296 /* Ensure there is enough space for the headers in the skb */
297 if (skb_cow(skb, hh_len + new_header_size))
301 skb->protocol = htons(ETH_P_MPLS_UC);
303 if (unlikely(!new_header_size && dec.bos)) {
304 /* Penultimate hop popping */
305 if (!mpls_egress(rt, skb, dec))
310 skb_push(skb, new_header_size);
311 skb_reset_network_header(skb);
312 /* Push the new labels */
315 for (i = nh->nh_labels - 1; i >= 0; i--) {
316 hdr[i] = mpls_entry_encode(nh->nh_label[i],
322 /* If via wasn't specified then send out using device address */
323 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
324 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
325 out_dev->dev_addr, skb);
327 err = neigh_xmit(nh->nh_via_table, out_dev,
328 mpls_nh_via(rt, nh), skb);
330 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
339 static struct packet_type mpls_packet_type __read_mostly = {
340 .type = cpu_to_be16(ETH_P_MPLS_UC),
341 .func = mpls_forward,
344 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
345 [RTA_DST] = { .type = NLA_U32 },
346 [RTA_OIF] = { .type = NLA_U32 },
349 struct mpls_route_config {
354 u8 rc_via[MAX_VIA_ALEN];
357 u32 rc_output_label[MAX_NEW_LABELS];
359 enum mpls_payload_type rc_payload_type;
360 struct nl_info rc_nlinfo;
361 struct rtnexthop *rc_mp;
365 static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
367 u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
368 struct mpls_route *rt;
370 rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
372 num_nh * max_alen_aligned,
376 rt->rt_max_alen = max_alen_aligned;
382 static void mpls_rt_free(struct mpls_route *rt)
385 kfree_rcu(rt, rt_rcu);
388 static void mpls_notify_route(struct net *net, unsigned index,
389 struct mpls_route *old, struct mpls_route *new,
390 const struct nl_info *info)
392 struct nlmsghdr *nlh = info ? info->nlh : NULL;
393 unsigned portid = info ? info->portid : 0;
394 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
395 struct mpls_route *rt = new ? new : old;
396 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
397 /* Ignore reserved labels for now */
398 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
399 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
402 static void mpls_route_update(struct net *net, unsigned index,
403 struct mpls_route *new,
404 const struct nl_info *info)
406 struct mpls_route __rcu **platform_label;
407 struct mpls_route *rt;
411 platform_label = rtnl_dereference(net->mpls.platform_label);
412 rt = rtnl_dereference(platform_label[index]);
413 rcu_assign_pointer(platform_label[index], new);
415 mpls_notify_route(net, index, rt, new, info);
417 /* If we removed a route free it now */
421 static unsigned find_free_label(struct net *net)
423 struct mpls_route __rcu **platform_label;
424 size_t platform_labels;
427 platform_label = rtnl_dereference(net->mpls.platform_label);
428 platform_labels = net->mpls.platform_labels;
429 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
431 if (!rtnl_dereference(platform_label[index]))
434 return LABEL_NOT_SPECIFIED;
437 #if IS_ENABLED(CONFIG_INET)
438 static struct net_device *inet_fib_lookup_dev(struct net *net,
441 struct net_device *dev;
443 struct in_addr daddr;
445 memcpy(&daddr, addr, sizeof(struct in_addr));
446 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
458 static struct net_device *inet_fib_lookup_dev(struct net *net,
461 return ERR_PTR(-EAFNOSUPPORT);
465 #if IS_ENABLED(CONFIG_IPV6)
466 static struct net_device *inet6_fib_lookup_dev(struct net *net,
469 struct net_device *dev;
470 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 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
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);
521 /* The caller is holding rtnl anyways, so release the dev reference */
527 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
528 struct mpls_nh *nh, int oif)
530 struct net_device *dev = NULL;
533 dev = find_outdev(net, rt, nh, oif);
540 /* Ensure this is a supported device */
542 if (!mpls_dev_get(dev))
545 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
546 (dev->addr_len != nh->nh_via_alen))
549 RCU_INIT_POINTER(nh->nh_dev, dev);
557 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
558 struct mpls_route *rt)
560 struct net *net = cfg->rc_nlinfo.nl_net;
561 struct mpls_nh *nh = rt->rt_nh;
569 /* Ensure only a supported number of labels are present */
570 if (cfg->rc_output_labels > MAX_NEW_LABELS)
573 nh->nh_labels = cfg->rc_output_labels;
574 for (i = 0; i < nh->nh_labels; i++)
575 nh->nh_label[i] = cfg->rc_output_label[i];
577 nh->nh_via_table = cfg->rc_via_table;
578 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
579 nh->nh_via_alen = cfg->rc_via_alen;
581 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
591 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
592 struct mpls_nh *nh, int oif,
593 struct nlattr *via, struct nlattr *newdst)
601 err = nla_get_labels(newdst, MAX_NEW_LABELS,
602 &nh->nh_labels, nh->nh_label);
608 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
609 __mpls_nh_via(rt, nh));
613 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
616 err = mpls_nh_assign_dev(net, rt, nh, oif);
626 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
627 u8 cfg_via_alen, u8 *max_via_alen)
633 *max_via_alen = cfg_via_alen;
639 while (rtnh_ok(rtnh, remaining)) {
640 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
643 attrlen = rtnh_attrlen(rtnh);
644 nla = nla_find(attrs, attrlen, RTA_VIA);
645 if (nla && nla_len(nla) >=
646 offsetof(struct rtvia, rtvia_addr)) {
647 int via_alen = nla_len(nla) -
648 offsetof(struct rtvia, rtvia_addr);
650 if (via_alen <= MAX_VIA_ALEN)
651 *max_via_alen = max_t(u16, *max_via_alen,
656 rtnh = rtnh_next(rtnh, &remaining);
659 /* leftover implies invalid nexthop configuration, discard it */
660 return remaining > 0 ? 0 : nhs;
663 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
664 struct mpls_route *rt)
666 struct rtnexthop *rtnh = cfg->rc_mp;
667 struct nlattr *nla_via, *nla_newdst;
668 int remaining = cfg->rc_mp_len;
672 change_nexthops(rt) {
679 if (!rtnh_ok(rtnh, remaining))
682 /* neither weighted multipath nor any flags
685 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
688 attrlen = rtnh_attrlen(rtnh);
690 struct nlattr *attrs = rtnh_attrs(rtnh);
692 nla_via = nla_find(attrs, attrlen, RTA_VIA);
693 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
696 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
697 rtnh->rtnh_ifindex, nla_via,
702 rtnh = rtnh_next(rtnh, &remaining);
704 } endfor_nexthops(rt);
714 static int mpls_route_add(struct mpls_route_config *cfg)
716 struct mpls_route __rcu **platform_label;
717 struct net *net = cfg->rc_nlinfo.nl_net;
718 struct mpls_route *rt, *old;
724 index = cfg->rc_label;
726 /* If a label was not specified during insert pick one */
727 if ((index == LABEL_NOT_SPECIFIED) &&
728 (cfg->rc_nlflags & NLM_F_CREATE)) {
729 index = find_free_label(net);
732 /* Reserved labels may not be set */
733 if (index < MPLS_LABEL_FIRST_UNRESERVED)
736 /* The full 20 bit range may not be supported. */
737 if (index >= net->mpls.platform_labels)
740 /* Append makes no sense with mpls */
742 if (cfg->rc_nlflags & NLM_F_APPEND)
746 platform_label = rtnl_dereference(net->mpls.platform_label);
747 old = rtnl_dereference(platform_label[index]);
748 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
752 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
756 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
760 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
761 cfg->rc_via_alen, &max_via_alen);
766 rt = mpls_rt_alloc(nhs, max_via_alen);
770 rt->rt_protocol = cfg->rc_protocol;
771 rt->rt_payload_type = cfg->rc_payload_type;
774 err = mpls_nh_build_multi(cfg, rt);
776 err = mpls_nh_build_from_cfg(cfg, rt);
780 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
790 static int mpls_route_del(struct mpls_route_config *cfg)
792 struct net *net = cfg->rc_nlinfo.nl_net;
796 index = cfg->rc_label;
798 /* Reserved labels may not be removed */
799 if (index < MPLS_LABEL_FIRST_UNRESERVED)
802 /* The full 20 bit range may not be supported */
803 if (index >= net->mpls.platform_labels)
806 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
813 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
814 (&((struct mpls_dev *)0)->field)
816 static const struct ctl_table mpls_dev_table[] = {
819 .maxlen = sizeof(int),
821 .proc_handler = proc_dointvec,
822 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
827 static int mpls_dev_sysctl_register(struct net_device *dev,
828 struct mpls_dev *mdev)
830 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
831 struct ctl_table *table;
834 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
838 /* Table data contains only offsets relative to the base of
839 * the mdev at this point, so make them absolute.
841 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
842 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
844 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
846 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
858 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
860 struct ctl_table *table;
862 table = mdev->sysctl->ctl_table_arg;
863 unregister_net_sysctl_table(mdev->sysctl);
867 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
869 struct mpls_dev *mdev;
874 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
878 err = mpls_dev_sysctl_register(dev, mdev);
882 rcu_assign_pointer(dev->mpls_ptr, mdev);
891 static void mpls_ifdown(struct net_device *dev)
893 struct mpls_route __rcu **platform_label;
894 struct net *net = dev_net(dev);
895 struct mpls_dev *mdev;
898 platform_label = rtnl_dereference(net->mpls.platform_label);
899 for (index = 0; index < net->mpls.platform_labels; index++) {
900 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
904 if (rtnl_dereference(nh->nh_dev) != dev)
907 } endfor_nexthops(rt);
910 mdev = mpls_dev_get(dev);
914 mpls_dev_sysctl_unregister(mdev);
916 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
918 kfree_rcu(mdev, rcu);
921 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
924 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
925 struct mpls_dev *mdev;
928 case NETDEV_REGISTER:
929 /* For now just support ethernet devices */
930 if ((dev->type == ARPHRD_ETHER) ||
931 (dev->type == ARPHRD_LOOPBACK)) {
932 mdev = mpls_add_dev(dev);
934 return notifier_from_errno(PTR_ERR(mdev));
938 case NETDEV_UNREGISTER:
941 case NETDEV_CHANGENAME:
942 mdev = mpls_dev_get(dev);
946 mpls_dev_sysctl_unregister(mdev);
947 err = mpls_dev_sysctl_register(dev, mdev);
949 return notifier_from_errno(err);
956 static struct notifier_block mpls_dev_notifier = {
957 .notifier_call = mpls_dev_notify,
960 static int nla_put_via(struct sk_buff *skb,
961 u8 table, const void *addr, int alen)
963 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
964 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
968 int family = AF_UNSPEC;
970 nla = nla_reserve(skb, RTA_VIA, alen + 2);
974 if (table <= NEIGH_NR_TABLES)
975 family = table_to_family[table];
978 via->rtvia_family = family;
979 memcpy(via->rtvia_addr, addr, alen);
983 int nla_put_labels(struct sk_buff *skb, int attrtype,
984 u8 labels, const u32 label[])
987 struct mpls_shim_hdr *nla_label;
990 nla = nla_reserve(skb, attrtype, labels*4);
994 nla_label = nla_data(nla);
996 for (i = labels - 1; i >= 0; i--) {
997 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1003 EXPORT_SYMBOL_GPL(nla_put_labels);
1005 int nla_get_labels(const struct nlattr *nla,
1006 u32 max_labels, u8 *labels, u32 label[])
1008 unsigned len = nla_len(nla);
1009 unsigned nla_labels;
1010 struct mpls_shim_hdr *nla_label;
1014 /* len needs to be an even multiple of 4 (the label size) */
1018 /* Limit the number of new labels allowed */
1020 if (nla_labels > max_labels)
1023 nla_label = nla_data(nla);
1025 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1026 struct mpls_entry_decoded dec;
1027 dec = mpls_entry_decode(nla_label + i);
1029 /* Ensure the bottom of stack flag is properly set
1030 * and ttl and tc are both clear.
1032 if ((dec.bos != bos) || dec.ttl || dec.tc)
1035 switch (dec.label) {
1036 case MPLS_LABEL_IMPLNULL:
1037 /* RFC3032: This is a label that an LSR may
1038 * assign and distribute, but which never
1039 * actually appears in the encapsulation.
1044 label[i] = dec.label;
1046 *labels = nla_labels;
1049 EXPORT_SYMBOL_GPL(nla_get_labels);
1051 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
1052 u8 *via_table, u8 via_addr[])
1054 struct rtvia *via = nla_data(nla);
1058 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
1060 alen = nla_len(nla) -
1061 offsetof(struct rtvia, rtvia_addr);
1062 if (alen > MAX_VIA_ALEN)
1065 /* Validate the address family */
1066 switch (via->rtvia_family) {
1068 *via_table = NEIGH_LINK_TABLE;
1071 *via_table = NEIGH_ARP_TABLE;
1076 *via_table = NEIGH_ND_TABLE;
1081 /* Unsupported address family */
1085 memcpy(via_addr, via->rtvia_addr, alen);
1093 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1094 struct mpls_route_config *cfg)
1097 struct nlattr *tb[RTA_MAX+1];
1101 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1106 rtm = nlmsg_data(nlh);
1107 memset(cfg, 0, sizeof(*cfg));
1109 if (rtm->rtm_family != AF_MPLS)
1111 if (rtm->rtm_dst_len != 20)
1113 if (rtm->rtm_src_len != 0)
1115 if (rtm->rtm_tos != 0)
1117 if (rtm->rtm_table != RT_TABLE_MAIN)
1119 /* Any value is acceptable for rtm_protocol */
1121 /* As mpls uses destination specific addresses
1122 * (or source specific address in the case of multicast)
1123 * all addresses have universal scope.
1125 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1127 if (rtm->rtm_type != RTN_UNICAST)
1129 if (rtm->rtm_flags != 0)
1132 cfg->rc_label = LABEL_NOT_SPECIFIED;
1133 cfg->rc_protocol = rtm->rtm_protocol;
1134 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
1135 cfg->rc_nlflags = nlh->nlmsg_flags;
1136 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1137 cfg->rc_nlinfo.nlh = nlh;
1138 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1140 for (index = 0; index <= RTA_MAX; index++) {
1141 struct nlattr *nla = tb[index];
1147 cfg->rc_ifindex = nla_get_u32(nla);
1150 if (nla_get_labels(nla, MAX_NEW_LABELS,
1151 &cfg->rc_output_labels,
1152 cfg->rc_output_label))
1158 if (nla_get_labels(nla, 1, &label_count,
1162 /* Reserved labels may not be set */
1163 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1170 if (nla_get_via(nla, &cfg->rc_via_alen,
1171 &cfg->rc_via_table, cfg->rc_via))
1177 cfg->rc_mp = nla_data(nla);
1178 cfg->rc_mp_len = nla_len(nla);
1182 /* Unsupported attribute */
1192 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1194 struct mpls_route_config cfg;
1197 err = rtm_to_route_config(skb, nlh, &cfg);
1201 return mpls_route_del(&cfg);
1205 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1207 struct mpls_route_config cfg;
1210 err = rtm_to_route_config(skb, nlh, &cfg);
1214 return mpls_route_add(&cfg);
1217 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1218 u32 label, struct mpls_route *rt, int flags)
1220 struct net_device *dev;
1221 struct nlmsghdr *nlh;
1224 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1228 rtm = nlmsg_data(nlh);
1229 rtm->rtm_family = AF_MPLS;
1230 rtm->rtm_dst_len = 20;
1231 rtm->rtm_src_len = 0;
1233 rtm->rtm_table = RT_TABLE_MAIN;
1234 rtm->rtm_protocol = rt->rt_protocol;
1235 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1236 rtm->rtm_type = RTN_UNICAST;
1239 if (nla_put_labels(skb, RTA_DST, 1, &label))
1240 goto nla_put_failure;
1241 if (rt->rt_nhn == 1) {
1242 const struct mpls_nh *nh = rt->rt_nh;
1244 if (nh->nh_labels &&
1245 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1247 goto nla_put_failure;
1248 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1249 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
1251 goto nla_put_failure;
1252 dev = rtnl_dereference(nh->nh_dev);
1253 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1254 goto nla_put_failure;
1256 struct rtnexthop *rtnh;
1259 mp = nla_nest_start(skb, RTA_MULTIPATH);
1261 goto nla_put_failure;
1264 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1266 goto nla_put_failure;
1268 dev = rtnl_dereference(nh->nh_dev);
1270 rtnh->rtnh_ifindex = dev->ifindex;
1271 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1274 goto nla_put_failure;
1275 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
1276 nla_put_via(skb, nh->nh_via_table,
1277 mpls_nh_via(rt, nh),
1279 goto nla_put_failure;
1281 /* length of rtnetlink header + attributes */
1282 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1283 } endfor_nexthops(rt);
1285 nla_nest_end(skb, mp);
1288 nlmsg_end(skb, nlh);
1292 nlmsg_cancel(skb, nlh);
1296 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1298 struct net *net = sock_net(skb->sk);
1299 struct mpls_route __rcu **platform_label;
1300 size_t platform_labels;
1305 index = cb->args[0];
1306 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1307 index = MPLS_LABEL_FIRST_UNRESERVED;
1309 platform_label = rtnl_dereference(net->mpls.platform_label);
1310 platform_labels = net->mpls.platform_labels;
1311 for (; index < platform_labels; index++) {
1312 struct mpls_route *rt;
1313 rt = rtnl_dereference(platform_label[index]);
1317 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1318 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1319 index, rt, NLM_F_MULTI) < 0)
1322 cb->args[0] = index;
1327 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1330 NLMSG_ALIGN(sizeof(struct rtmsg))
1331 + nla_total_size(4); /* RTA_DST */
1333 if (rt->rt_nhn == 1) {
1334 struct mpls_nh *nh = rt->rt_nh;
1337 payload += nla_total_size(4); /* RTA_OIF */
1338 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
1339 payload += nla_total_size(2 + nh->nh_via_alen);
1340 if (nh->nh_labels) /* RTA_NEWDST */
1341 payload += nla_total_size(nh->nh_labels * 4);
1343 /* each nexthop is packed in an attribute */
1347 nhsize += nla_total_size(sizeof(struct rtnexthop));
1349 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
1350 nhsize += nla_total_size(2 + nh->nh_via_alen);
1352 nhsize += nla_total_size(nh->nh_labels * 4);
1353 } endfor_nexthops(rt);
1354 /* nested attribute */
1355 payload += nla_total_size(nhsize);
1361 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1362 struct nlmsghdr *nlh, struct net *net, u32 portid,
1363 unsigned int nlm_flags)
1365 struct sk_buff *skb;
1366 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1369 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1373 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1375 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1376 WARN_ON(err == -EMSGSIZE);
1380 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1385 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1388 static int resize_platform_label_table(struct net *net, size_t limit)
1390 size_t size = sizeof(struct mpls_route *) * limit;
1393 struct mpls_route __rcu **labels = NULL, **old;
1394 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1398 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1400 labels = vzalloc(size);
1406 /* In case the predefined labels need to be populated */
1407 if (limit > MPLS_LABEL_IPV4NULL) {
1408 struct net_device *lo = net->loopback_dev;
1409 rt0 = mpls_rt_alloc(1, lo->addr_len);
1412 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1413 rt0->rt_protocol = RTPROT_KERNEL;
1414 rt0->rt_payload_type = MPT_IPV4;
1415 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1416 rt0->rt_nh->nh_via_alen = lo->addr_len;
1417 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
1420 if (limit > MPLS_LABEL_IPV6NULL) {
1421 struct net_device *lo = net->loopback_dev;
1422 rt2 = mpls_rt_alloc(1, lo->addr_len);
1425 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1426 rt2->rt_protocol = RTPROT_KERNEL;
1427 rt2->rt_payload_type = MPT_IPV6;
1428 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1429 rt2->rt_nh->nh_via_alen = lo->addr_len;
1430 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
1435 /* Remember the original table */
1436 old = rtnl_dereference(net->mpls.platform_label);
1437 old_limit = net->mpls.platform_labels;
1439 /* Free any labels beyond the new table */
1440 for (index = limit; index < old_limit; index++)
1441 mpls_route_update(net, index, NULL, NULL);
1443 /* Copy over the old labels */
1445 if (old_limit < limit)
1446 cp_size = old_limit * sizeof(struct mpls_route *);
1448 memcpy(labels, old, cp_size);
1450 /* If needed set the predefined labels */
1451 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1452 (limit > MPLS_LABEL_IPV6NULL)) {
1453 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1457 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1458 (limit > MPLS_LABEL_IPV4NULL)) {
1459 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1463 /* Update the global pointers */
1464 net->mpls.platform_labels = limit;
1465 rcu_assign_pointer(net->mpls.platform_label, labels);
1486 static int mpls_platform_labels(struct ctl_table *table, int write,
1487 void __user *buffer, size_t *lenp, loff_t *ppos)
1489 struct net *net = table->data;
1490 int platform_labels = net->mpls.platform_labels;
1492 struct ctl_table tmp = {
1493 .procname = table->procname,
1494 .data = &platform_labels,
1495 .maxlen = sizeof(int),
1496 .mode = table->mode,
1498 .extra2 = &label_limit,
1501 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1503 if (write && ret == 0)
1504 ret = resize_platform_label_table(net, platform_labels);
1509 static const struct ctl_table mpls_table[] = {
1511 .procname = "platform_labels",
1513 .maxlen = sizeof(int),
1515 .proc_handler = mpls_platform_labels,
1520 static int mpls_net_init(struct net *net)
1522 struct ctl_table *table;
1524 net->mpls.platform_labels = 0;
1525 net->mpls.platform_label = NULL;
1527 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1531 table[0].data = net;
1532 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1533 if (net->mpls.ctl == NULL) {
1541 static void mpls_net_exit(struct net *net)
1543 struct mpls_route __rcu **platform_label;
1544 size_t platform_labels;
1545 struct ctl_table *table;
1548 table = net->mpls.ctl->ctl_table_arg;
1549 unregister_net_sysctl_table(net->mpls.ctl);
1552 /* An rcu grace period has passed since there was a device in
1553 * the network namespace (and thus the last in flight packet)
1554 * left this network namespace. This is because
1555 * unregister_netdevice_many and netdev_run_todo has completed
1556 * for each network device that was in this network namespace.
1558 * As such no additional rcu synchronization is necessary when
1559 * freeing the platform_label table.
1562 platform_label = rtnl_dereference(net->mpls.platform_label);
1563 platform_labels = net->mpls.platform_labels;
1564 for (index = 0; index < platform_labels; index++) {
1565 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1566 RCU_INIT_POINTER(platform_label[index], NULL);
1571 kvfree(platform_label);
1574 static struct pernet_operations mpls_net_ops = {
1575 .init = mpls_net_init,
1576 .exit = mpls_net_exit,
1579 static int __init mpls_init(void)
1583 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1585 err = register_pernet_subsys(&mpls_net_ops);
1589 err = register_netdevice_notifier(&mpls_dev_notifier);
1591 goto out_unregister_pernet;
1593 dev_add_pack(&mpls_packet_type);
1595 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1596 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1597 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1602 out_unregister_pernet:
1603 unregister_pernet_subsys(&mpls_net_ops);
1606 module_init(mpls_init);
1608 static void __exit mpls_exit(void)
1610 rtnl_unregister_all(PF_MPLS);
1611 dev_remove_pack(&mpls_packet_type);
1612 unregister_netdevice_notifier(&mpls_dev_notifier);
1613 unregister_pernet_subsys(&mpls_net_ops);
1615 module_exit(mpls_exit);
1617 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1618 MODULE_LICENSE("GPL v2");
1619 MODULE_ALIAS_NETPROTO(PF_MPLS);