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>
24 #define LABEL_NOT_SPECIFIED (1<<20)
25 #define MAX_NEW_LABELS 2
27 /* This maximum ha length copied from the definition of struct neighbour */
28 #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
30 struct mpls_route { /* next hop label forwarding entry */
31 struct net_device __rcu *rt_dev;
32 struct rcu_head rt_rcu;
33 u32 rt_label[MAX_NEW_LABELS];
34 u8 rt_protocol; /* routing protocol that set this entry */
42 static int label_limit = (1 << 20) - 1;
44 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
45 struct nlmsghdr *nlh, struct net *net, u32 portid,
46 unsigned int nlm_flags);
48 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
50 struct mpls_route *rt = NULL;
52 if (index < net->mpls.platform_labels) {
53 struct mpls_route __rcu **platform_label =
54 rcu_dereference(net->mpls.platform_label);
55 rt = rcu_dereference(platform_label[index]);
60 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
62 return rcu_dereference_rtnl(dev->mpls_ptr);
65 bool mpls_output_possible(const struct net_device *dev)
67 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
69 EXPORT_SYMBOL_GPL(mpls_output_possible);
71 static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
73 /* The size of the layer 2.5 labels to be added for this route */
74 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
77 unsigned int mpls_dev_mtu(const struct net_device *dev)
79 /* The amount of data the layer 2 frame can hold */
82 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
84 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
89 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
94 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
96 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
97 struct mpls_entry_decoded dec)
99 /* RFC4385 and RFC5586 encode other packets in mpls such that
100 * they don't conflict with the ip version number, making
101 * decoding by examining the ip version correct in everything
102 * except for the strangest cases.
104 * The strange cases if we choose to support them will require
105 * manual configuration.
110 /* The IPv4 code below accesses through the IPv4 header
111 * checksum, which is 12 bytes into the packet.
112 * The IPv6 code below accesses through the IPv6 hop limit
113 * which is 8 bytes into the packet.
115 * For all supported cases there should always be at least 12
116 * bytes of packet data present. The IPv4 header is 20 bytes
117 * without options and the IPv6 header is always 40 bytes
120 if (!pskb_may_pull(skb, 12))
123 /* Use ip_hdr to find the ip protocol version */
125 if (hdr4->version == 4) {
126 skb->protocol = htons(ETH_P_IP);
127 csum_replace2(&hdr4->check,
128 htons(hdr4->ttl << 8),
129 htons(dec.ttl << 8));
132 else if (hdr4->version == 6) {
133 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
134 skb->protocol = htons(ETH_P_IPV6);
135 hdr6->hop_limit = dec.ttl;
138 /* version 0 and version 1 are used by pseudo wires */
143 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
144 struct packet_type *pt, struct net_device *orig_dev)
146 struct net *net = dev_net(dev);
147 struct mpls_shim_hdr *hdr;
148 struct mpls_route *rt;
149 struct mpls_entry_decoded dec;
150 struct net_device *out_dev;
151 struct mpls_dev *mdev;
153 unsigned int new_header_size;
157 /* Careful this entire function runs inside of an rcu critical section */
159 mdev = mpls_dev_get(dev);
160 if (!mdev || !mdev->input_enabled)
163 if (skb->pkt_type != PACKET_HOST)
166 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
169 if (!pskb_may_pull(skb, sizeof(*hdr)))
172 /* Read and decode the label */
174 dec = mpls_entry_decode(hdr);
177 skb_pull(skb, sizeof(*hdr));
178 skb_reset_network_header(skb);
182 rt = mpls_route_input_rcu(net, dec.label);
186 /* Find the output device */
187 out_dev = rcu_dereference(rt->rt_dev);
188 if (!mpls_output_possible(out_dev))
191 if (skb_warn_if_lro(skb))
194 skb_forward_csum(skb);
196 /* Verify ttl is valid */
201 /* Verify the destination can hold the packet */
202 new_header_size = mpls_rt_header_size(rt);
203 mtu = mpls_dev_mtu(out_dev);
204 if (mpls_pkt_too_big(skb, mtu - new_header_size))
207 hh_len = LL_RESERVED_SPACE(out_dev);
208 if (!out_dev->header_ops)
211 /* Ensure there is enough space for the headers in the skb */
212 if (skb_cow(skb, hh_len + new_header_size))
216 skb->protocol = htons(ETH_P_MPLS_UC);
218 if (unlikely(!new_header_size && dec.bos)) {
219 /* Penultimate hop popping */
220 if (!mpls_egress(rt, skb, dec))
225 skb_push(skb, new_header_size);
226 skb_reset_network_header(skb);
227 /* Push the new labels */
230 for (i = rt->rt_labels - 1; i >= 0; i--) {
231 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
236 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
238 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
247 static struct packet_type mpls_packet_type __read_mostly = {
248 .type = cpu_to_be16(ETH_P_MPLS_UC),
249 .func = mpls_forward,
252 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
253 [RTA_DST] = { .type = NLA_U32 },
254 [RTA_OIF] = { .type = NLA_U32 },
257 struct mpls_route_config {
262 u8 rc_via[MAX_VIA_ALEN];
264 u32 rc_output_labels;
265 u32 rc_output_label[MAX_NEW_LABELS];
267 struct nl_info rc_nlinfo;
270 static struct mpls_route *mpls_rt_alloc(size_t alen)
272 struct mpls_route *rt;
274 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
276 rt->rt_via_alen = alen;
280 static void mpls_rt_free(struct mpls_route *rt)
283 kfree_rcu(rt, rt_rcu);
286 static void mpls_notify_route(struct net *net, unsigned index,
287 struct mpls_route *old, struct mpls_route *new,
288 const struct nl_info *info)
290 struct nlmsghdr *nlh = info ? info->nlh : NULL;
291 unsigned portid = info ? info->portid : 0;
292 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
293 struct mpls_route *rt = new ? new : old;
294 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
295 /* Ignore reserved labels for now */
296 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
297 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
300 static void mpls_route_update(struct net *net, unsigned index,
301 struct net_device *dev, struct mpls_route *new,
302 const struct nl_info *info)
304 struct mpls_route __rcu **platform_label;
305 struct mpls_route *rt, *old = NULL;
309 platform_label = rtnl_dereference(net->mpls.platform_label);
310 rt = rtnl_dereference(platform_label[index]);
311 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
312 rcu_assign_pointer(platform_label[index], new);
316 mpls_notify_route(net, index, old, new, info);
318 /* If we removed a route free it now */
322 static unsigned find_free_label(struct net *net)
324 struct mpls_route __rcu **platform_label;
325 size_t platform_labels;
328 platform_label = rtnl_dereference(net->mpls.platform_label);
329 platform_labels = net->mpls.platform_labels;
330 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
332 if (!rtnl_dereference(platform_label[index]))
335 return LABEL_NOT_SPECIFIED;
338 #if IS_ENABLED(CONFIG_INET)
339 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
341 struct net_device *dev = NULL;
343 struct in_addr daddr;
345 memcpy(&daddr, addr, sizeof(struct in_addr));
346 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
357 return ERR_PTR(-ENODEV);
360 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
362 return ERR_PTR(-EAFNOSUPPORT);
366 #if IS_ENABLED(CONFIG_IPV6)
367 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
369 struct net_device *dev = NULL;
370 struct dst_entry *dst;
375 return ERR_PTR(-EAFNOSUPPORT);
377 memset(&fl6, 0, sizeof(fl6));
378 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
379 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
393 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
395 return ERR_PTR(-EAFNOSUPPORT);
399 static struct net_device *find_outdev(struct net *net,
400 struct mpls_route_config *cfg)
402 struct net_device *dev = NULL;
404 if (!cfg->rc_ifindex) {
405 switch (cfg->rc_via_table) {
406 case NEIGH_ARP_TABLE:
407 dev = inet_fib_lookup_dev(net, cfg->rc_via);
410 dev = inet6_fib_lookup_dev(net, cfg->rc_via);
412 case NEIGH_LINK_TABLE:
416 dev = dev_get_by_index(net, cfg->rc_ifindex);
422 static int mpls_route_add(struct mpls_route_config *cfg)
424 struct mpls_route __rcu **platform_label;
425 struct net *net = cfg->rc_nlinfo.nl_net;
426 struct net_device *dev = NULL;
427 struct mpls_route *rt, *old;
432 index = cfg->rc_label;
434 /* If a label was not specified during insert pick one */
435 if ((index == LABEL_NOT_SPECIFIED) &&
436 (cfg->rc_nlflags & NLM_F_CREATE)) {
437 index = find_free_label(net);
440 /* Reserved labels may not be set */
441 if (index < MPLS_LABEL_FIRST_UNRESERVED)
444 /* The full 20 bit range may not be supported. */
445 if (index >= net->mpls.platform_labels)
448 /* Ensure only a supported number of labels are present */
449 if (cfg->rc_output_labels > MAX_NEW_LABELS)
452 dev = find_outdev(net, cfg);
459 /* Ensure this is a supported device */
461 if (!mpls_dev_get(dev))
465 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
466 (dev->addr_len != cfg->rc_via_alen))
469 /* Append makes no sense with mpls */
471 if (cfg->rc_nlflags & NLM_F_APPEND)
475 platform_label = rtnl_dereference(net->mpls.platform_label);
476 old = rtnl_dereference(platform_label[index]);
477 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
481 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
485 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
489 rt = mpls_rt_alloc(cfg->rc_via_alen);
493 rt->rt_labels = cfg->rc_output_labels;
494 for (i = 0; i < rt->rt_labels; i++)
495 rt->rt_label[i] = cfg->rc_output_label[i];
496 rt->rt_protocol = cfg->rc_protocol;
497 RCU_INIT_POINTER(rt->rt_dev, dev);
498 rt->rt_via_table = cfg->rc_via_table;
499 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
501 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
512 static int mpls_route_del(struct mpls_route_config *cfg)
514 struct net *net = cfg->rc_nlinfo.nl_net;
518 index = cfg->rc_label;
520 /* Reserved labels may not be removed */
521 if (index < MPLS_LABEL_FIRST_UNRESERVED)
524 /* The full 20 bit range may not be supported */
525 if (index >= net->mpls.platform_labels)
528 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
535 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
536 (&((struct mpls_dev *)0)->field)
538 static const struct ctl_table mpls_dev_table[] = {
541 .maxlen = sizeof(int),
543 .proc_handler = proc_dointvec,
544 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
549 static int mpls_dev_sysctl_register(struct net_device *dev,
550 struct mpls_dev *mdev)
552 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
553 struct ctl_table *table;
556 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
560 /* Table data contains only offsets relative to the base of
561 * the mdev at this point, so make them absolute.
563 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
564 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
566 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
568 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
580 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
582 struct ctl_table *table;
584 table = mdev->sysctl->ctl_table_arg;
585 unregister_net_sysctl_table(mdev->sysctl);
589 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
591 struct mpls_dev *mdev;
596 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
600 err = mpls_dev_sysctl_register(dev, mdev);
604 rcu_assign_pointer(dev->mpls_ptr, mdev);
613 static void mpls_ifdown(struct net_device *dev)
615 struct mpls_route __rcu **platform_label;
616 struct net *net = dev_net(dev);
617 struct mpls_dev *mdev;
620 platform_label = rtnl_dereference(net->mpls.platform_label);
621 for (index = 0; index < net->mpls.platform_labels; index++) {
622 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
625 if (rtnl_dereference(rt->rt_dev) != dev)
630 mdev = mpls_dev_get(dev);
634 mpls_dev_sysctl_unregister(mdev);
636 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
638 kfree_rcu(mdev, rcu);
641 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
644 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
645 struct mpls_dev *mdev;
648 case NETDEV_REGISTER:
649 /* For now just support ethernet devices */
650 if ((dev->type == ARPHRD_ETHER) ||
651 (dev->type == ARPHRD_LOOPBACK)) {
652 mdev = mpls_add_dev(dev);
654 return notifier_from_errno(PTR_ERR(mdev));
658 case NETDEV_UNREGISTER:
661 case NETDEV_CHANGENAME:
662 mdev = mpls_dev_get(dev);
666 mpls_dev_sysctl_unregister(mdev);
667 err = mpls_dev_sysctl_register(dev, mdev);
669 return notifier_from_errno(err);
676 static struct notifier_block mpls_dev_notifier = {
677 .notifier_call = mpls_dev_notify,
680 static int nla_put_via(struct sk_buff *skb,
681 u8 table, const void *addr, int alen)
683 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
684 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
688 int family = AF_UNSPEC;
690 nla = nla_reserve(skb, RTA_VIA, alen + 2);
694 if (table <= NEIGH_NR_TABLES)
695 family = table_to_family[table];
698 via->rtvia_family = family;
699 memcpy(via->rtvia_addr, addr, alen);
703 int nla_put_labels(struct sk_buff *skb, int attrtype,
704 u8 labels, const u32 label[])
707 struct mpls_shim_hdr *nla_label;
710 nla = nla_reserve(skb, attrtype, labels*4);
714 nla_label = nla_data(nla);
716 for (i = labels - 1; i >= 0; i--) {
717 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
723 EXPORT_SYMBOL_GPL(nla_put_labels);
725 int nla_get_labels(const struct nlattr *nla,
726 u32 max_labels, u32 *labels, u32 label[])
728 unsigned len = nla_len(nla);
730 struct mpls_shim_hdr *nla_label;
734 /* len needs to be an even multiple of 4 (the label size) */
738 /* Limit the number of new labels allowed */
740 if (nla_labels > max_labels)
743 nla_label = nla_data(nla);
745 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
746 struct mpls_entry_decoded dec;
747 dec = mpls_entry_decode(nla_label + i);
749 /* Ensure the bottom of stack flag is properly set
750 * and ttl and tc are both clear.
752 if ((dec.bos != bos) || dec.ttl || dec.tc)
756 case MPLS_LABEL_IMPLNULL:
757 /* RFC3032: This is a label that an LSR may
758 * assign and distribute, but which never
759 * actually appears in the encapsulation.
764 label[i] = dec.label;
766 *labels = nla_labels;
769 EXPORT_SYMBOL_GPL(nla_get_labels);
771 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
772 struct mpls_route_config *cfg)
775 struct nlattr *tb[RTA_MAX+1];
779 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
784 rtm = nlmsg_data(nlh);
785 memset(cfg, 0, sizeof(*cfg));
787 if (rtm->rtm_family != AF_MPLS)
789 if (rtm->rtm_dst_len != 20)
791 if (rtm->rtm_src_len != 0)
793 if (rtm->rtm_tos != 0)
795 if (rtm->rtm_table != RT_TABLE_MAIN)
797 /* Any value is acceptable for rtm_protocol */
799 /* As mpls uses destination specific addresses
800 * (or source specific address in the case of multicast)
801 * all addresses have universal scope.
803 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
805 if (rtm->rtm_type != RTN_UNICAST)
807 if (rtm->rtm_flags != 0)
810 cfg->rc_label = LABEL_NOT_SPECIFIED;
811 cfg->rc_protocol = rtm->rtm_protocol;
812 cfg->rc_nlflags = nlh->nlmsg_flags;
813 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
814 cfg->rc_nlinfo.nlh = nlh;
815 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
817 for (index = 0; index <= RTA_MAX; index++) {
818 struct nlattr *nla = tb[index];
824 cfg->rc_ifindex = nla_get_u32(nla);
827 if (nla_get_labels(nla, MAX_NEW_LABELS,
828 &cfg->rc_output_labels,
829 cfg->rc_output_label))
835 if (nla_get_labels(nla, 1, &label_count,
839 /* Reserved labels may not be set */
840 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
847 struct rtvia *via = nla_data(nla);
848 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
850 cfg->rc_via_alen = nla_len(nla) -
851 offsetof(struct rtvia, rtvia_addr);
852 if (cfg->rc_via_alen > MAX_VIA_ALEN)
855 /* Validate the address family */
856 switch(via->rtvia_family) {
858 cfg->rc_via_table = NEIGH_LINK_TABLE;
861 cfg->rc_via_table = NEIGH_ARP_TABLE;
862 if (cfg->rc_via_alen != 4)
866 cfg->rc_via_table = NEIGH_ND_TABLE;
867 if (cfg->rc_via_alen != 16)
871 /* Unsupported address family */
875 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
879 /* Unsupported attribute */
889 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
891 struct mpls_route_config cfg;
894 err = rtm_to_route_config(skb, nlh, &cfg);
898 return mpls_route_del(&cfg);
902 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
904 struct mpls_route_config cfg;
907 err = rtm_to_route_config(skb, nlh, &cfg);
911 return mpls_route_add(&cfg);
914 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
915 u32 label, struct mpls_route *rt, int flags)
917 struct net_device *dev;
918 struct nlmsghdr *nlh;
921 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
925 rtm = nlmsg_data(nlh);
926 rtm->rtm_family = AF_MPLS;
927 rtm->rtm_dst_len = 20;
928 rtm->rtm_src_len = 0;
930 rtm->rtm_table = RT_TABLE_MAIN;
931 rtm->rtm_protocol = rt->rt_protocol;
932 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
933 rtm->rtm_type = RTN_UNICAST;
937 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
938 goto nla_put_failure;
939 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
940 goto nla_put_failure;
941 dev = rtnl_dereference(rt->rt_dev);
942 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
943 goto nla_put_failure;
944 if (nla_put_labels(skb, RTA_DST, 1, &label))
945 goto nla_put_failure;
951 nlmsg_cancel(skb, nlh);
955 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
957 struct net *net = sock_net(skb->sk);
958 struct mpls_route __rcu **platform_label;
959 size_t platform_labels;
965 if (index < MPLS_LABEL_FIRST_UNRESERVED)
966 index = MPLS_LABEL_FIRST_UNRESERVED;
968 platform_label = rtnl_dereference(net->mpls.platform_label);
969 platform_labels = net->mpls.platform_labels;
970 for (; index < platform_labels; index++) {
971 struct mpls_route *rt;
972 rt = rtnl_dereference(platform_label[index]);
976 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
977 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
978 index, rt, NLM_F_MULTI) < 0)
986 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
989 NLMSG_ALIGN(sizeof(struct rtmsg))
990 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
991 + nla_total_size(4); /* RTA_DST */
992 if (rt->rt_labels) /* RTA_NEWDST */
993 payload += nla_total_size(rt->rt_labels * 4);
994 if (rt->rt_dev) /* RTA_OIF */
995 payload += nla_total_size(4);
999 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1000 struct nlmsghdr *nlh, struct net *net, u32 portid,
1001 unsigned int nlm_flags)
1003 struct sk_buff *skb;
1004 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1007 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1011 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1013 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1014 WARN_ON(err == -EMSGSIZE);
1018 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1023 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1026 static int resize_platform_label_table(struct net *net, size_t limit)
1028 size_t size = sizeof(struct mpls_route *) * limit;
1031 struct mpls_route __rcu **labels = NULL, **old;
1032 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1036 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1038 labels = vzalloc(size);
1044 /* In case the predefined labels need to be populated */
1045 if (limit > MPLS_LABEL_IPV4NULL) {
1046 struct net_device *lo = net->loopback_dev;
1047 rt0 = mpls_rt_alloc(lo->addr_len);
1050 RCU_INIT_POINTER(rt0->rt_dev, lo);
1051 rt0->rt_protocol = RTPROT_KERNEL;
1052 rt0->rt_via_table = NEIGH_LINK_TABLE;
1053 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1055 if (limit > MPLS_LABEL_IPV6NULL) {
1056 struct net_device *lo = net->loopback_dev;
1057 rt2 = mpls_rt_alloc(lo->addr_len);
1060 RCU_INIT_POINTER(rt2->rt_dev, lo);
1061 rt2->rt_protocol = RTPROT_KERNEL;
1062 rt2->rt_via_table = NEIGH_LINK_TABLE;
1063 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1067 /* Remember the original table */
1068 old = rtnl_dereference(net->mpls.platform_label);
1069 old_limit = net->mpls.platform_labels;
1071 /* Free any labels beyond the new table */
1072 for (index = limit; index < old_limit; index++)
1073 mpls_route_update(net, index, NULL, NULL, NULL);
1075 /* Copy over the old labels */
1077 if (old_limit < limit)
1078 cp_size = old_limit * sizeof(struct mpls_route *);
1080 memcpy(labels, old, cp_size);
1082 /* If needed set the predefined labels */
1083 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1084 (limit > MPLS_LABEL_IPV6NULL)) {
1085 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1089 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1090 (limit > MPLS_LABEL_IPV4NULL)) {
1091 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1095 /* Update the global pointers */
1096 net->mpls.platform_labels = limit;
1097 rcu_assign_pointer(net->mpls.platform_label, labels);
1118 static int mpls_platform_labels(struct ctl_table *table, int write,
1119 void __user *buffer, size_t *lenp, loff_t *ppos)
1121 struct net *net = table->data;
1122 int platform_labels = net->mpls.platform_labels;
1124 struct ctl_table tmp = {
1125 .procname = table->procname,
1126 .data = &platform_labels,
1127 .maxlen = sizeof(int),
1128 .mode = table->mode,
1130 .extra2 = &label_limit,
1133 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1135 if (write && ret == 0)
1136 ret = resize_platform_label_table(net, platform_labels);
1141 static const struct ctl_table mpls_table[] = {
1143 .procname = "platform_labels",
1145 .maxlen = sizeof(int),
1147 .proc_handler = mpls_platform_labels,
1152 static int mpls_net_init(struct net *net)
1154 struct ctl_table *table;
1156 net->mpls.platform_labels = 0;
1157 net->mpls.platform_label = NULL;
1159 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1163 table[0].data = net;
1164 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1165 if (net->mpls.ctl == NULL)
1171 static void mpls_net_exit(struct net *net)
1173 struct mpls_route __rcu **platform_label;
1174 size_t platform_labels;
1175 struct ctl_table *table;
1178 table = net->mpls.ctl->ctl_table_arg;
1179 unregister_net_sysctl_table(net->mpls.ctl);
1182 /* An rcu grace period has passed since there was a device in
1183 * the network namespace (and thus the last in flight packet)
1184 * left this network namespace. This is because
1185 * unregister_netdevice_many and netdev_run_todo has completed
1186 * for each network device that was in this network namespace.
1188 * As such no additional rcu synchronization is necessary when
1189 * freeing the platform_label table.
1192 platform_label = rtnl_dereference(net->mpls.platform_label);
1193 platform_labels = net->mpls.platform_labels;
1194 for (index = 0; index < platform_labels; index++) {
1195 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1196 RCU_INIT_POINTER(platform_label[index], NULL);
1201 kvfree(platform_label);
1204 static struct pernet_operations mpls_net_ops = {
1205 .init = mpls_net_init,
1206 .exit = mpls_net_exit,
1209 static int __init mpls_init(void)
1213 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1215 err = register_pernet_subsys(&mpls_net_ops);
1219 err = register_netdevice_notifier(&mpls_dev_notifier);
1221 goto out_unregister_pernet;
1223 dev_add_pack(&mpls_packet_type);
1225 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1226 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1227 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1232 out_unregister_pernet:
1233 unregister_pernet_subsys(&mpls_net_ops);
1236 module_init(mpls_init);
1238 static void __exit mpls_exit(void)
1240 rtnl_unregister_all(PF_MPLS);
1241 dev_remove_pack(&mpls_packet_type);
1242 unregister_netdevice_notifier(&mpls_dev_notifier);
1243 unregister_pernet_subsys(&mpls_net_ops);
1245 module_exit(mpls_exit);
1247 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1248 MODULE_LICENSE("GPL v2");
1249 MODULE_ALIAS_NETPROTO(PF_MPLS);