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 unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
62 /* The size of the layer 2.5 labels to be added for this route */
63 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
66 unsigned int mpls_dev_mtu(const struct net_device *dev)
68 /* The amount of data the layer 2 frame can hold */
71 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
73 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
78 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
83 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
85 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
86 struct sk_buff *skb, bool bos)
88 struct mpls_entry_decoded dec;
89 struct mpls_shim_hdr *hdr;
90 bool eli_seen = false;
95 /* No need to look further into packet if there's only
101 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
103 if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
106 /* Read and decode the current label */
107 hdr = mpls_hdr(skb) + label_index;
108 dec = mpls_entry_decode(hdr);
110 /* RFC6790 - reserved labels MUST NOT be used as keys
111 * for the load-balancing function
113 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
114 hash = jhash_1word(dec.label, hash);
116 /* The entropy label follows the entropy label
117 * indicator, so this means that the entropy
118 * label was just added to the hash - no need to
119 * go any deeper either in the label stack or in the
124 } else if (dec.label == MPLS_LABEL_ENTROPY) {
129 if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
130 sizeof(struct iphdr))) {
131 const struct iphdr *v4hdr;
133 v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
135 if (v4hdr->version == 4) {
136 hash = jhash_3words(ntohl(v4hdr->saddr),
138 v4hdr->protocol, hash);
139 } else if (v4hdr->version == 6 &&
140 pskb_may_pull(skb, sizeof(*hdr) * label_index +
141 sizeof(struct ipv6hdr))) {
142 const struct ipv6hdr *v6hdr;
144 v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
147 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
148 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
149 hash = jhash_1word(v6hdr->nexthdr, hash);
154 nh_index = hash % rt->rt_nhn;
156 return &rt->rt_nh[nh_index];
159 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
160 struct mpls_entry_decoded dec)
162 enum mpls_payload_type payload_type;
163 bool success = false;
165 /* The IPv4 code below accesses through the IPv4 header
166 * checksum, which is 12 bytes into the packet.
167 * The IPv6 code below accesses through the IPv6 hop limit
168 * which is 8 bytes into the packet.
170 * For all supported cases there should always be at least 12
171 * bytes of packet data present. The IPv4 header is 20 bytes
172 * without options and the IPv6 header is always 40 bytes
175 if (!pskb_may_pull(skb, 12))
178 payload_type = rt->rt_payload_type;
179 if (payload_type == MPT_UNSPEC)
180 payload_type = ip_hdr(skb)->version;
182 switch (payload_type) {
184 struct iphdr *hdr4 = ip_hdr(skb);
185 skb->protocol = htons(ETH_P_IP);
186 csum_replace2(&hdr4->check,
187 htons(hdr4->ttl << 8),
188 htons(dec.ttl << 8));
194 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
195 skb->protocol = htons(ETH_P_IPV6);
196 hdr6->hop_limit = dec.ttl;
207 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
208 struct packet_type *pt, struct net_device *orig_dev)
210 struct net *net = dev_net(dev);
211 struct mpls_shim_hdr *hdr;
212 struct mpls_route *rt;
214 struct mpls_entry_decoded dec;
215 struct net_device *out_dev;
216 struct mpls_dev *mdev;
218 unsigned int new_header_size;
222 /* Careful this entire function runs inside of an rcu critical section */
224 mdev = mpls_dev_get(dev);
225 if (!mdev || !mdev->input_enabled)
228 if (skb->pkt_type != PACKET_HOST)
231 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
234 if (!pskb_may_pull(skb, sizeof(*hdr)))
237 /* Read and decode the label */
239 dec = mpls_entry_decode(hdr);
242 skb_pull(skb, sizeof(*hdr));
243 skb_reset_network_header(skb);
247 rt = mpls_route_input_rcu(net, dec.label);
251 nh = mpls_select_multipath(rt, skb, dec.bos);
255 /* Find the output device */
256 out_dev = rcu_dereference(nh->nh_dev);
257 if (!mpls_output_possible(out_dev))
260 if (skb_warn_if_lro(skb))
263 skb_forward_csum(skb);
265 /* Verify ttl is valid */
270 /* Verify the destination can hold the packet */
271 new_header_size = mpls_nh_header_size(nh);
272 mtu = mpls_dev_mtu(out_dev);
273 if (mpls_pkt_too_big(skb, mtu - new_header_size))
276 hh_len = LL_RESERVED_SPACE(out_dev);
277 if (!out_dev->header_ops)
280 /* Ensure there is enough space for the headers in the skb */
281 if (skb_cow(skb, hh_len + new_header_size))
285 skb->protocol = htons(ETH_P_MPLS_UC);
287 if (unlikely(!new_header_size && dec.bos)) {
288 /* Penultimate hop popping */
289 if (!mpls_egress(rt, skb, dec))
294 skb_push(skb, new_header_size);
295 skb_reset_network_header(skb);
296 /* Push the new labels */
299 for (i = nh->nh_labels - 1; i >= 0; i--) {
300 hdr[i] = mpls_entry_encode(nh->nh_label[i],
306 err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
308 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
317 static struct packet_type mpls_packet_type __read_mostly = {
318 .type = cpu_to_be16(ETH_P_MPLS_UC),
319 .func = mpls_forward,
322 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
323 [RTA_DST] = { .type = NLA_U32 },
324 [RTA_OIF] = { .type = NLA_U32 },
327 struct mpls_route_config {
332 u8 rc_via[MAX_VIA_ALEN];
335 u32 rc_output_label[MAX_NEW_LABELS];
337 enum mpls_payload_type rc_payload_type;
338 struct nl_info rc_nlinfo;
339 struct rtnexthop *rc_mp;
343 static struct mpls_route *mpls_rt_alloc(int num_nh)
345 struct mpls_route *rt;
347 rt = kzalloc(sizeof(*rt) + (num_nh * sizeof(struct mpls_nh)),
355 static void mpls_rt_free(struct mpls_route *rt)
358 kfree_rcu(rt, rt_rcu);
361 static void mpls_notify_route(struct net *net, unsigned index,
362 struct mpls_route *old, struct mpls_route *new,
363 const struct nl_info *info)
365 struct nlmsghdr *nlh = info ? info->nlh : NULL;
366 unsigned portid = info ? info->portid : 0;
367 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
368 struct mpls_route *rt = new ? new : old;
369 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
370 /* Ignore reserved labels for now */
371 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
372 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
375 static void mpls_route_update(struct net *net, unsigned index,
376 struct mpls_route *new,
377 const struct nl_info *info)
379 struct mpls_route __rcu **platform_label;
380 struct mpls_route *rt;
384 platform_label = rtnl_dereference(net->mpls.platform_label);
385 rt = rtnl_dereference(platform_label[index]);
386 rcu_assign_pointer(platform_label[index], new);
388 mpls_notify_route(net, index, rt, new, info);
390 /* If we removed a route free it now */
394 static unsigned find_free_label(struct net *net)
396 struct mpls_route __rcu **platform_label;
397 size_t platform_labels;
400 platform_label = rtnl_dereference(net->mpls.platform_label);
401 platform_labels = net->mpls.platform_labels;
402 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
404 if (!rtnl_dereference(platform_label[index]))
407 return LABEL_NOT_SPECIFIED;
410 #if IS_ENABLED(CONFIG_INET)
411 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
413 struct net_device *dev;
415 struct in_addr daddr;
417 memcpy(&daddr, addr, sizeof(struct in_addr));
418 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
430 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
432 return ERR_PTR(-EAFNOSUPPORT);
436 #if IS_ENABLED(CONFIG_IPV6)
437 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
439 struct net_device *dev;
440 struct dst_entry *dst;
445 return ERR_PTR(-EAFNOSUPPORT);
447 memset(&fl6, 0, sizeof(fl6));
448 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
449 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
460 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
462 return ERR_PTR(-EAFNOSUPPORT);
466 static struct net_device *find_outdev(struct net *net,
467 struct mpls_nh *nh, int oif)
469 struct net_device *dev = NULL;
472 switch (nh->nh_via_table) {
473 case NEIGH_ARP_TABLE:
474 dev = inet_fib_lookup_dev(net, nh->nh_via);
477 dev = inet6_fib_lookup_dev(net, nh->nh_via);
479 case NEIGH_LINK_TABLE:
483 dev = dev_get_by_index(net, oif);
487 return ERR_PTR(-ENODEV);
489 /* The caller is holding rtnl anyways, so release the dev reference */
495 static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
497 struct net_device *dev = NULL;
500 dev = find_outdev(net, nh, oif);
507 /* Ensure this is a supported device */
509 if (!mpls_dev_get(dev))
512 RCU_INIT_POINTER(nh->nh_dev, dev);
520 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
521 struct mpls_route *rt)
523 struct net *net = cfg->rc_nlinfo.nl_net;
524 struct mpls_nh *nh = rt->rt_nh;
532 /* Ensure only a supported number of labels are present */
533 if (cfg->rc_output_labels > MAX_NEW_LABELS)
536 nh->nh_labels = cfg->rc_output_labels;
537 for (i = 0; i < nh->nh_labels; i++)
538 nh->nh_label[i] = cfg->rc_output_label[i];
540 nh->nh_via_table = cfg->rc_via_table;
541 memcpy(nh->nh_via, cfg->rc_via, cfg->rc_via_alen);
542 nh->nh_via_alen = cfg->rc_via_alen;
544 err = mpls_nh_assign_dev(net, nh, cfg->rc_ifindex);
554 static int mpls_nh_build(struct net *net, struct mpls_nh *nh,
555 int oif, struct nlattr *via, struct nlattr *newdst)
563 err = nla_get_labels(newdst, MAX_NEW_LABELS,
564 &nh->nh_labels, nh->nh_label);
569 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
574 err = mpls_nh_assign_dev(net, nh, oif);
584 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len)
589 while (rtnh_ok(rtnh, remaining)) {
591 rtnh = rtnh_next(rtnh, &remaining);
594 /* leftover implies invalid nexthop configuration, discard it */
595 return remaining > 0 ? 0 : nhs;
598 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
599 struct mpls_route *rt)
601 struct rtnexthop *rtnh = cfg->rc_mp;
602 struct nlattr *nla_via, *nla_newdst;
603 int remaining = cfg->rc_mp_len;
607 change_nexthops(rt) {
614 if (!rtnh_ok(rtnh, remaining))
617 /* neither weighted multipath nor any flags
620 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
623 attrlen = rtnh_attrlen(rtnh);
625 struct nlattr *attrs = rtnh_attrs(rtnh);
627 nla_via = nla_find(attrs, attrlen, RTA_VIA);
628 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
634 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, nh,
635 rtnh->rtnh_ifindex, nla_via,
640 rtnh = rtnh_next(rtnh, &remaining);
642 } endfor_nexthops(rt);
652 static int mpls_route_add(struct mpls_route_config *cfg)
654 struct mpls_route __rcu **platform_label;
655 struct net *net = cfg->rc_nlinfo.nl_net;
656 struct mpls_route *rt, *old;
659 int nhs = 1; /* default to one nexthop */
661 index = cfg->rc_label;
663 /* If a label was not specified during insert pick one */
664 if ((index == LABEL_NOT_SPECIFIED) &&
665 (cfg->rc_nlflags & NLM_F_CREATE)) {
666 index = find_free_label(net);
669 /* Reserved labels may not be set */
670 if (index < MPLS_LABEL_FIRST_UNRESERVED)
673 /* The full 20 bit range may not be supported. */
674 if (index >= net->mpls.platform_labels)
677 /* Append makes no sense with mpls */
679 if (cfg->rc_nlflags & NLM_F_APPEND)
683 platform_label = rtnl_dereference(net->mpls.platform_label);
684 old = rtnl_dereference(platform_label[index]);
685 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
689 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
693 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
698 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len);
704 rt = mpls_rt_alloc(nhs);
708 rt->rt_protocol = cfg->rc_protocol;
709 rt->rt_payload_type = cfg->rc_payload_type;
712 err = mpls_nh_build_multi(cfg, rt);
714 err = mpls_nh_build_from_cfg(cfg, rt);
718 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
728 static int mpls_route_del(struct mpls_route_config *cfg)
730 struct net *net = cfg->rc_nlinfo.nl_net;
734 index = cfg->rc_label;
736 /* Reserved labels may not be removed */
737 if (index < MPLS_LABEL_FIRST_UNRESERVED)
740 /* The full 20 bit range may not be supported */
741 if (index >= net->mpls.platform_labels)
744 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
751 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
752 (&((struct mpls_dev *)0)->field)
754 static const struct ctl_table mpls_dev_table[] = {
757 .maxlen = sizeof(int),
759 .proc_handler = proc_dointvec,
760 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
765 static int mpls_dev_sysctl_register(struct net_device *dev,
766 struct mpls_dev *mdev)
768 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
769 struct ctl_table *table;
772 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
776 /* Table data contains only offsets relative to the base of
777 * the mdev at this point, so make them absolute.
779 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
780 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
782 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
784 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
796 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
798 struct ctl_table *table;
800 table = mdev->sysctl->ctl_table_arg;
801 unregister_net_sysctl_table(mdev->sysctl);
805 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
807 struct mpls_dev *mdev;
812 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
816 err = mpls_dev_sysctl_register(dev, mdev);
820 rcu_assign_pointer(dev->mpls_ptr, mdev);
829 static void mpls_ifdown(struct net_device *dev)
831 struct mpls_route __rcu **platform_label;
832 struct net *net = dev_net(dev);
833 struct mpls_dev *mdev;
836 platform_label = rtnl_dereference(net->mpls.platform_label);
837 for (index = 0; index < net->mpls.platform_labels; index++) {
838 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
842 if (rtnl_dereference(nh->nh_dev) != dev)
845 } endfor_nexthops(rt);
848 mdev = mpls_dev_get(dev);
852 mpls_dev_sysctl_unregister(mdev);
854 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
856 kfree_rcu(mdev, rcu);
859 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
862 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
863 struct mpls_dev *mdev;
866 case NETDEV_REGISTER:
867 /* For now just support ethernet devices */
868 if ((dev->type == ARPHRD_ETHER) ||
869 (dev->type == ARPHRD_LOOPBACK)) {
870 mdev = mpls_add_dev(dev);
872 return notifier_from_errno(PTR_ERR(mdev));
876 case NETDEV_UNREGISTER:
879 case NETDEV_CHANGENAME:
880 mdev = mpls_dev_get(dev);
884 mpls_dev_sysctl_unregister(mdev);
885 err = mpls_dev_sysctl_register(dev, mdev);
887 return notifier_from_errno(err);
894 static struct notifier_block mpls_dev_notifier = {
895 .notifier_call = mpls_dev_notify,
898 static int nla_put_via(struct sk_buff *skb,
899 u8 table, const void *addr, int alen)
901 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
902 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
906 int family = AF_UNSPEC;
908 nla = nla_reserve(skb, RTA_VIA, alen + 2);
912 if (table <= NEIGH_NR_TABLES)
913 family = table_to_family[table];
916 via->rtvia_family = family;
917 memcpy(via->rtvia_addr, addr, alen);
921 int nla_put_labels(struct sk_buff *skb, int attrtype,
922 u8 labels, const u32 label[])
925 struct mpls_shim_hdr *nla_label;
928 nla = nla_reserve(skb, attrtype, labels*4);
932 nla_label = nla_data(nla);
934 for (i = labels - 1; i >= 0; i--) {
935 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
941 EXPORT_SYMBOL_GPL(nla_put_labels);
943 int nla_get_labels(const struct nlattr *nla,
944 u32 max_labels, u8 *labels, u32 label[])
946 unsigned len = nla_len(nla);
948 struct mpls_shim_hdr *nla_label;
952 /* len needs to be an even multiple of 4 (the label size) */
956 /* Limit the number of new labels allowed */
958 if (nla_labels > max_labels)
961 nla_label = nla_data(nla);
963 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
964 struct mpls_entry_decoded dec;
965 dec = mpls_entry_decode(nla_label + i);
967 /* Ensure the bottom of stack flag is properly set
968 * and ttl and tc are both clear.
970 if ((dec.bos != bos) || dec.ttl || dec.tc)
974 case MPLS_LABEL_IMPLNULL:
975 /* RFC3032: This is a label that an LSR may
976 * assign and distribute, but which never
977 * actually appears in the encapsulation.
982 label[i] = dec.label;
984 *labels = nla_labels;
987 EXPORT_SYMBOL_GPL(nla_get_labels);
989 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
990 u8 *via_table, u8 via_addr[])
992 struct rtvia *via = nla_data(nla);
996 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
998 alen = nla_len(nla) -
999 offsetof(struct rtvia, rtvia_addr);
1000 if (alen > MAX_VIA_ALEN)
1003 /* Validate the address family */
1004 switch (via->rtvia_family) {
1006 *via_table = NEIGH_LINK_TABLE;
1009 *via_table = NEIGH_ARP_TABLE;
1014 *via_table = NEIGH_ND_TABLE;
1019 /* Unsupported address family */
1023 memcpy(via_addr, via->rtvia_addr, alen);
1031 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
1032 struct mpls_route_config *cfg)
1035 struct nlattr *tb[RTA_MAX+1];
1039 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
1044 rtm = nlmsg_data(nlh);
1045 memset(cfg, 0, sizeof(*cfg));
1047 if (rtm->rtm_family != AF_MPLS)
1049 if (rtm->rtm_dst_len != 20)
1051 if (rtm->rtm_src_len != 0)
1053 if (rtm->rtm_tos != 0)
1055 if (rtm->rtm_table != RT_TABLE_MAIN)
1057 /* Any value is acceptable for rtm_protocol */
1059 /* As mpls uses destination specific addresses
1060 * (or source specific address in the case of multicast)
1061 * all addresses have universal scope.
1063 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
1065 if (rtm->rtm_type != RTN_UNICAST)
1067 if (rtm->rtm_flags != 0)
1070 cfg->rc_label = LABEL_NOT_SPECIFIED;
1071 cfg->rc_protocol = rtm->rtm_protocol;
1072 cfg->rc_nlflags = nlh->nlmsg_flags;
1073 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1074 cfg->rc_nlinfo.nlh = nlh;
1075 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1077 for (index = 0; index <= RTA_MAX; index++) {
1078 struct nlattr *nla = tb[index];
1084 cfg->rc_ifindex = nla_get_u32(nla);
1087 if (nla_get_labels(nla, MAX_NEW_LABELS,
1088 &cfg->rc_output_labels,
1089 cfg->rc_output_label))
1095 if (nla_get_labels(nla, 1, &label_count,
1099 /* Reserved labels may not be set */
1100 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1107 if (nla_get_via(nla, &cfg->rc_via_alen,
1108 &cfg->rc_via_table, cfg->rc_via))
1114 cfg->rc_mp = nla_data(nla);
1115 cfg->rc_mp_len = nla_len(nla);
1119 /* Unsupported attribute */
1129 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1131 struct mpls_route_config cfg;
1134 err = rtm_to_route_config(skb, nlh, &cfg);
1138 return mpls_route_del(&cfg);
1142 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1144 struct mpls_route_config cfg;
1147 err = rtm_to_route_config(skb, nlh, &cfg);
1151 return mpls_route_add(&cfg);
1154 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1155 u32 label, struct mpls_route *rt, int flags)
1157 struct net_device *dev;
1158 struct nlmsghdr *nlh;
1161 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1165 rtm = nlmsg_data(nlh);
1166 rtm->rtm_family = AF_MPLS;
1167 rtm->rtm_dst_len = 20;
1168 rtm->rtm_src_len = 0;
1170 rtm->rtm_table = RT_TABLE_MAIN;
1171 rtm->rtm_protocol = rt->rt_protocol;
1172 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1173 rtm->rtm_type = RTN_UNICAST;
1176 if (nla_put_labels(skb, RTA_DST, 1, &label))
1177 goto nla_put_failure;
1178 if (rt->rt_nhn == 1) {
1179 struct mpls_nh *nh = rt->rt_nh;
1181 if (nh->nh_labels &&
1182 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1184 goto nla_put_failure;
1185 if (nla_put_via(skb, nh->nh_via_table, nh->nh_via,
1187 goto nla_put_failure;
1188 dev = rtnl_dereference(nh->nh_dev);
1189 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1190 goto nla_put_failure;
1192 struct rtnexthop *rtnh;
1195 mp = nla_nest_start(skb, RTA_MULTIPATH);
1197 goto nla_put_failure;
1200 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1202 goto nla_put_failure;
1204 dev = rtnl_dereference(nh->nh_dev);
1206 rtnh->rtnh_ifindex = dev->ifindex;
1207 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1210 goto nla_put_failure;
1211 if (nla_put_via(skb, nh->nh_via_table,
1214 goto nla_put_failure;
1216 /* length of rtnetlink header + attributes */
1217 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1218 } endfor_nexthops(rt);
1220 nla_nest_end(skb, mp);
1223 nlmsg_end(skb, nlh);
1227 nlmsg_cancel(skb, nlh);
1231 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1233 struct net *net = sock_net(skb->sk);
1234 struct mpls_route __rcu **platform_label;
1235 size_t platform_labels;
1240 index = cb->args[0];
1241 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1242 index = MPLS_LABEL_FIRST_UNRESERVED;
1244 platform_label = rtnl_dereference(net->mpls.platform_label);
1245 platform_labels = net->mpls.platform_labels;
1246 for (; index < platform_labels; index++) {
1247 struct mpls_route *rt;
1248 rt = rtnl_dereference(platform_label[index]);
1252 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1253 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1254 index, rt, NLM_F_MULTI) < 0)
1257 cb->args[0] = index;
1262 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1265 NLMSG_ALIGN(sizeof(struct rtmsg))
1266 + nla_total_size(4); /* RTA_DST */
1268 if (rt->rt_nhn == 1) {
1269 struct mpls_nh *nh = rt->rt_nh;
1272 payload += nla_total_size(4); /* RTA_OIF */
1273 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1274 if (nh->nh_labels) /* RTA_NEWDST */
1275 payload += nla_total_size(nh->nh_labels * 4);
1277 /* each nexthop is packed in an attribute */
1281 nhsize += nla_total_size(sizeof(struct rtnexthop));
1282 nhsize += nla_total_size(2 + nh->nh_via_alen);
1284 nhsize += nla_total_size(nh->nh_labels * 4);
1285 } endfor_nexthops(rt);
1286 /* nested attribute */
1287 payload += nla_total_size(nhsize);
1293 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1294 struct nlmsghdr *nlh, struct net *net, u32 portid,
1295 unsigned int nlm_flags)
1297 struct sk_buff *skb;
1298 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1301 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1305 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1307 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1308 WARN_ON(err == -EMSGSIZE);
1312 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1317 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1320 static int resize_platform_label_table(struct net *net, size_t limit)
1322 size_t size = sizeof(struct mpls_route *) * limit;
1325 struct mpls_route __rcu **labels = NULL, **old;
1326 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1330 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1332 labels = vzalloc(size);
1338 /* In case the predefined labels need to be populated */
1339 if (limit > MPLS_LABEL_IPV4NULL) {
1340 struct net_device *lo = net->loopback_dev;
1341 rt0 = mpls_rt_alloc(1);
1344 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1345 rt0->rt_protocol = RTPROT_KERNEL;
1346 rt0->rt_payload_type = MPT_IPV4;
1347 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1348 memcpy(rt0->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
1350 if (limit > MPLS_LABEL_IPV6NULL) {
1351 struct net_device *lo = net->loopback_dev;
1352 rt2 = mpls_rt_alloc(1);
1355 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1356 rt2->rt_protocol = RTPROT_KERNEL;
1357 rt2->rt_payload_type = MPT_IPV6;
1358 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1359 memcpy(rt2->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
1363 /* Remember the original table */
1364 old = rtnl_dereference(net->mpls.platform_label);
1365 old_limit = net->mpls.platform_labels;
1367 /* Free any labels beyond the new table */
1368 for (index = limit; index < old_limit; index++)
1369 mpls_route_update(net, index, NULL, NULL);
1371 /* Copy over the old labels */
1373 if (old_limit < limit)
1374 cp_size = old_limit * sizeof(struct mpls_route *);
1376 memcpy(labels, old, cp_size);
1378 /* If needed set the predefined labels */
1379 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1380 (limit > MPLS_LABEL_IPV6NULL)) {
1381 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1385 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1386 (limit > MPLS_LABEL_IPV4NULL)) {
1387 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1391 /* Update the global pointers */
1392 net->mpls.platform_labels = limit;
1393 rcu_assign_pointer(net->mpls.platform_label, labels);
1414 static int mpls_platform_labels(struct ctl_table *table, int write,
1415 void __user *buffer, size_t *lenp, loff_t *ppos)
1417 struct net *net = table->data;
1418 int platform_labels = net->mpls.platform_labels;
1420 struct ctl_table tmp = {
1421 .procname = table->procname,
1422 .data = &platform_labels,
1423 .maxlen = sizeof(int),
1424 .mode = table->mode,
1426 .extra2 = &label_limit,
1429 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1431 if (write && ret == 0)
1432 ret = resize_platform_label_table(net, platform_labels);
1437 static const struct ctl_table mpls_table[] = {
1439 .procname = "platform_labels",
1441 .maxlen = sizeof(int),
1443 .proc_handler = mpls_platform_labels,
1448 static int mpls_net_init(struct net *net)
1450 struct ctl_table *table;
1452 net->mpls.platform_labels = 0;
1453 net->mpls.platform_label = NULL;
1455 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1459 table[0].data = net;
1460 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1461 if (net->mpls.ctl == NULL) {
1469 static void mpls_net_exit(struct net *net)
1471 struct mpls_route __rcu **platform_label;
1472 size_t platform_labels;
1473 struct ctl_table *table;
1476 table = net->mpls.ctl->ctl_table_arg;
1477 unregister_net_sysctl_table(net->mpls.ctl);
1480 /* An rcu grace period has passed since there was a device in
1481 * the network namespace (and thus the last in flight packet)
1482 * left this network namespace. This is because
1483 * unregister_netdevice_many and netdev_run_todo has completed
1484 * for each network device that was in this network namespace.
1486 * As such no additional rcu synchronization is necessary when
1487 * freeing the platform_label table.
1490 platform_label = rtnl_dereference(net->mpls.platform_label);
1491 platform_labels = net->mpls.platform_labels;
1492 for (index = 0; index < platform_labels; index++) {
1493 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1494 RCU_INIT_POINTER(platform_label[index], NULL);
1499 kvfree(platform_label);
1502 static struct pernet_operations mpls_net_ops = {
1503 .init = mpls_net_init,
1504 .exit = mpls_net_exit,
1507 static int __init mpls_init(void)
1511 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1513 err = register_pernet_subsys(&mpls_net_ops);
1517 err = register_netdevice_notifier(&mpls_dev_notifier);
1519 goto out_unregister_pernet;
1521 dev_add_pack(&mpls_packet_type);
1523 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1524 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1525 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1530 out_unregister_pernet:
1531 unregister_pernet_subsys(&mpls_net_ops);
1534 module_init(mpls_init);
1536 static void __exit mpls_exit(void)
1538 rtnl_unregister_all(PF_MPLS);
1539 dev_remove_pack(&mpls_packet_type);
1540 unregister_netdevice_notifier(&mpls_dev_notifier);
1541 unregister_pernet_subsys(&mpls_net_ops);
1543 module_exit(mpls_exit);
1545 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1546 MODULE_LICENSE("GPL v2");
1547 MODULE_ALIAS_NETPROTO(PF_MPLS);