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 >= 16))
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 = 16; index < platform_labels; index++) {
331 if (!rtnl_dereference(platform_label[index]))
334 return LABEL_NOT_SPECIFIED;
337 #if IS_ENABLED(CONFIG_INET)
338 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
340 struct net_device *dev = NULL;
342 struct in_addr daddr;
344 memcpy(&daddr, addr, sizeof(struct in_addr));
345 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
356 return ERR_PTR(-ENODEV);
359 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
361 return ERR_PTR(-EAFNOSUPPORT);
365 #if IS_ENABLED(CONFIG_IPV6)
366 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
368 struct net_device *dev = NULL;
369 struct dst_entry *dst;
374 return ERR_PTR(-EAFNOSUPPORT);
376 memset(&fl6, 0, sizeof(fl6));
377 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
378 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
392 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
394 return ERR_PTR(-EAFNOSUPPORT);
398 static struct net_device *find_outdev(struct net *net,
399 struct mpls_route_config *cfg)
401 struct net_device *dev = NULL;
403 if (!cfg->rc_ifindex) {
404 switch (cfg->rc_via_table) {
405 case NEIGH_ARP_TABLE:
406 dev = inet_fib_lookup_dev(net, cfg->rc_via);
409 dev = inet6_fib_lookup_dev(net, cfg->rc_via);
411 case NEIGH_LINK_TABLE:
415 dev = dev_get_by_index(net, cfg->rc_ifindex);
421 static int mpls_route_add(struct mpls_route_config *cfg)
423 struct mpls_route __rcu **platform_label;
424 struct net *net = cfg->rc_nlinfo.nl_net;
425 struct net_device *dev = NULL;
426 struct mpls_route *rt, *old;
431 index = cfg->rc_label;
433 /* If a label was not specified during insert pick one */
434 if ((index == LABEL_NOT_SPECIFIED) &&
435 (cfg->rc_nlflags & NLM_F_CREATE)) {
436 index = find_free_label(net);
439 /* The first 16 labels are reserved, and may not be set */
443 /* The full 20 bit range may not be supported. */
444 if (index >= net->mpls.platform_labels)
447 /* Ensure only a supported number of labels are present */
448 if (cfg->rc_output_labels > MAX_NEW_LABELS)
451 dev = find_outdev(net, cfg);
458 /* Ensure this is a supported device */
460 if (!mpls_dev_get(dev))
464 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
465 (dev->addr_len != cfg->rc_via_alen))
468 /* Append makes no sense with mpls */
470 if (cfg->rc_nlflags & NLM_F_APPEND)
474 platform_label = rtnl_dereference(net->mpls.platform_label);
475 old = rtnl_dereference(platform_label[index]);
476 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
480 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
484 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
488 rt = mpls_rt_alloc(cfg->rc_via_alen);
492 rt->rt_labels = cfg->rc_output_labels;
493 for (i = 0; i < rt->rt_labels; i++)
494 rt->rt_label[i] = cfg->rc_output_label[i];
495 rt->rt_protocol = cfg->rc_protocol;
496 RCU_INIT_POINTER(rt->rt_dev, dev);
497 rt->rt_via_table = cfg->rc_via_table;
498 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
500 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
511 static int mpls_route_del(struct mpls_route_config *cfg)
513 struct net *net = cfg->rc_nlinfo.nl_net;
517 index = cfg->rc_label;
519 /* The first 16 labels are reserved, and may not be removed */
523 /* The full 20 bit range may not be supported */
524 if (index >= net->mpls.platform_labels)
527 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
534 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
535 (&((struct mpls_dev *)0)->field)
537 static const struct ctl_table mpls_dev_table[] = {
540 .maxlen = sizeof(int),
542 .proc_handler = proc_dointvec,
543 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
548 static int mpls_dev_sysctl_register(struct net_device *dev,
549 struct mpls_dev *mdev)
551 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
552 struct ctl_table *table;
555 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
559 /* Table data contains only offsets relative to the base of
560 * the mdev at this point, so make them absolute.
562 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
563 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
565 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
567 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
579 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
581 struct ctl_table *table;
583 table = mdev->sysctl->ctl_table_arg;
584 unregister_net_sysctl_table(mdev->sysctl);
588 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
590 struct mpls_dev *mdev;
595 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
599 err = mpls_dev_sysctl_register(dev, mdev);
603 rcu_assign_pointer(dev->mpls_ptr, mdev);
612 static void mpls_ifdown(struct net_device *dev)
614 struct mpls_route __rcu **platform_label;
615 struct net *net = dev_net(dev);
616 struct mpls_dev *mdev;
619 platform_label = rtnl_dereference(net->mpls.platform_label);
620 for (index = 0; index < net->mpls.platform_labels; index++) {
621 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
624 if (rtnl_dereference(rt->rt_dev) != dev)
629 mdev = mpls_dev_get(dev);
633 mpls_dev_sysctl_unregister(mdev);
635 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
637 kfree_rcu(mdev, rcu);
640 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
643 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
644 struct mpls_dev *mdev;
647 case NETDEV_REGISTER:
648 /* For now just support ethernet devices */
649 if ((dev->type == ARPHRD_ETHER) ||
650 (dev->type == ARPHRD_LOOPBACK)) {
651 mdev = mpls_add_dev(dev);
653 return notifier_from_errno(PTR_ERR(mdev));
657 case NETDEV_UNREGISTER:
660 case NETDEV_CHANGENAME:
661 mdev = mpls_dev_get(dev);
665 mpls_dev_sysctl_unregister(mdev);
666 err = mpls_dev_sysctl_register(dev, mdev);
668 return notifier_from_errno(err);
675 static struct notifier_block mpls_dev_notifier = {
676 .notifier_call = mpls_dev_notify,
679 static int nla_put_via(struct sk_buff *skb,
680 u8 table, const void *addr, int alen)
682 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
683 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
687 int family = AF_UNSPEC;
689 nla = nla_reserve(skb, RTA_VIA, alen + 2);
693 if (table <= NEIGH_NR_TABLES)
694 family = table_to_family[table];
697 via->rtvia_family = family;
698 memcpy(via->rtvia_addr, addr, alen);
702 int nla_put_labels(struct sk_buff *skb, int attrtype,
703 u8 labels, const u32 label[])
706 struct mpls_shim_hdr *nla_label;
709 nla = nla_reserve(skb, attrtype, labels*4);
713 nla_label = nla_data(nla);
715 for (i = labels - 1; i >= 0; i--) {
716 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
722 EXPORT_SYMBOL_GPL(nla_put_labels);
724 int nla_get_labels(const struct nlattr *nla,
725 u32 max_labels, u32 *labels, u32 label[])
727 unsigned len = nla_len(nla);
729 struct mpls_shim_hdr *nla_label;
733 /* len needs to be an even multiple of 4 (the label size) */
737 /* Limit the number of new labels allowed */
739 if (nla_labels > max_labels)
742 nla_label = nla_data(nla);
744 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
745 struct mpls_entry_decoded dec;
746 dec = mpls_entry_decode(nla_label + i);
748 /* Ensure the bottom of stack flag is properly set
749 * and ttl and tc are both clear.
751 if ((dec.bos != bos) || dec.ttl || dec.tc)
755 case MPLS_LABEL_IMPLNULL:
756 /* RFC3032: This is a label that an LSR may
757 * assign and distribute, but which never
758 * actually appears in the encapsulation.
763 label[i] = dec.label;
765 *labels = nla_labels;
768 EXPORT_SYMBOL_GPL(nla_get_labels);
770 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
771 struct mpls_route_config *cfg)
774 struct nlattr *tb[RTA_MAX+1];
778 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
783 rtm = nlmsg_data(nlh);
784 memset(cfg, 0, sizeof(*cfg));
786 if (rtm->rtm_family != AF_MPLS)
788 if (rtm->rtm_dst_len != 20)
790 if (rtm->rtm_src_len != 0)
792 if (rtm->rtm_tos != 0)
794 if (rtm->rtm_table != RT_TABLE_MAIN)
796 /* Any value is acceptable for rtm_protocol */
798 /* As mpls uses destination specific addresses
799 * (or source specific address in the case of multicast)
800 * all addresses have universal scope.
802 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
804 if (rtm->rtm_type != RTN_UNICAST)
806 if (rtm->rtm_flags != 0)
809 cfg->rc_label = LABEL_NOT_SPECIFIED;
810 cfg->rc_protocol = rtm->rtm_protocol;
811 cfg->rc_nlflags = nlh->nlmsg_flags;
812 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
813 cfg->rc_nlinfo.nlh = nlh;
814 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
816 for (index = 0; index <= RTA_MAX; index++) {
817 struct nlattr *nla = tb[index];
823 cfg->rc_ifindex = nla_get_u32(nla);
826 if (nla_get_labels(nla, MAX_NEW_LABELS,
827 &cfg->rc_output_labels,
828 cfg->rc_output_label))
834 if (nla_get_labels(nla, 1, &label_count,
838 /* The first 16 labels are reserved, and may not be set */
839 if (cfg->rc_label < 16)
846 struct rtvia *via = nla_data(nla);
847 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
849 cfg->rc_via_alen = nla_len(nla) -
850 offsetof(struct rtvia, rtvia_addr);
851 if (cfg->rc_via_alen > MAX_VIA_ALEN)
854 /* Validate the address family */
855 switch(via->rtvia_family) {
857 cfg->rc_via_table = NEIGH_LINK_TABLE;
860 cfg->rc_via_table = NEIGH_ARP_TABLE;
861 if (cfg->rc_via_alen != 4)
865 cfg->rc_via_table = NEIGH_ND_TABLE;
866 if (cfg->rc_via_alen != 16)
870 /* Unsupported address family */
874 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
878 /* Unsupported attribute */
888 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
890 struct mpls_route_config cfg;
893 err = rtm_to_route_config(skb, nlh, &cfg);
897 return mpls_route_del(&cfg);
901 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
903 struct mpls_route_config cfg;
906 err = rtm_to_route_config(skb, nlh, &cfg);
910 return mpls_route_add(&cfg);
913 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
914 u32 label, struct mpls_route *rt, int flags)
916 struct net_device *dev;
917 struct nlmsghdr *nlh;
920 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
924 rtm = nlmsg_data(nlh);
925 rtm->rtm_family = AF_MPLS;
926 rtm->rtm_dst_len = 20;
927 rtm->rtm_src_len = 0;
929 rtm->rtm_table = RT_TABLE_MAIN;
930 rtm->rtm_protocol = rt->rt_protocol;
931 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
932 rtm->rtm_type = RTN_UNICAST;
936 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
937 goto nla_put_failure;
938 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
939 goto nla_put_failure;
940 dev = rtnl_dereference(rt->rt_dev);
941 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
942 goto nla_put_failure;
943 if (nla_put_labels(skb, RTA_DST, 1, &label))
944 goto nla_put_failure;
950 nlmsg_cancel(skb, nlh);
954 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
956 struct net *net = sock_net(skb->sk);
957 struct mpls_route __rcu **platform_label;
958 size_t platform_labels;
967 platform_label = rtnl_dereference(net->mpls.platform_label);
968 platform_labels = net->mpls.platform_labels;
969 for (; index < platform_labels; index++) {
970 struct mpls_route *rt;
971 rt = rtnl_dereference(platform_label[index]);
975 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
976 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
977 index, rt, NLM_F_MULTI) < 0)
985 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
988 NLMSG_ALIGN(sizeof(struct rtmsg))
989 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
990 + nla_total_size(4); /* RTA_DST */
991 if (rt->rt_labels) /* RTA_NEWDST */
992 payload += nla_total_size(rt->rt_labels * 4);
993 if (rt->rt_dev) /* RTA_OIF */
994 payload += nla_total_size(4);
998 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
999 struct nlmsghdr *nlh, struct net *net, u32 portid,
1000 unsigned int nlm_flags)
1002 struct sk_buff *skb;
1003 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1006 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1010 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1012 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1013 WARN_ON(err == -EMSGSIZE);
1017 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1022 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1025 static int resize_platform_label_table(struct net *net, size_t limit)
1027 size_t size = sizeof(struct mpls_route *) * limit;
1030 struct mpls_route __rcu **labels = NULL, **old;
1031 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1035 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1037 labels = vzalloc(size);
1043 /* In case the predefined labels need to be populated */
1044 if (limit > MPLS_LABEL_IPV4NULL) {
1045 struct net_device *lo = net->loopback_dev;
1046 rt0 = mpls_rt_alloc(lo->addr_len);
1049 RCU_INIT_POINTER(rt0->rt_dev, lo);
1050 rt0->rt_protocol = RTPROT_KERNEL;
1051 rt0->rt_via_table = NEIGH_LINK_TABLE;
1052 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1054 if (limit > MPLS_LABEL_IPV6NULL) {
1055 struct net_device *lo = net->loopback_dev;
1056 rt2 = mpls_rt_alloc(lo->addr_len);
1059 RCU_INIT_POINTER(rt2->rt_dev, lo);
1060 rt2->rt_protocol = RTPROT_KERNEL;
1061 rt2->rt_via_table = NEIGH_LINK_TABLE;
1062 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1066 /* Remember the original table */
1067 old = rtnl_dereference(net->mpls.platform_label);
1068 old_limit = net->mpls.platform_labels;
1070 /* Free any labels beyond the new table */
1071 for (index = limit; index < old_limit; index++)
1072 mpls_route_update(net, index, NULL, NULL, NULL);
1074 /* Copy over the old labels */
1076 if (old_limit < limit)
1077 cp_size = old_limit * sizeof(struct mpls_route *);
1079 memcpy(labels, old, cp_size);
1081 /* If needed set the predefined labels */
1082 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1083 (limit > MPLS_LABEL_IPV6NULL)) {
1084 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1088 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1089 (limit > MPLS_LABEL_IPV4NULL)) {
1090 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1094 /* Update the global pointers */
1095 net->mpls.platform_labels = limit;
1096 rcu_assign_pointer(net->mpls.platform_label, labels);
1117 static int mpls_platform_labels(struct ctl_table *table, int write,
1118 void __user *buffer, size_t *lenp, loff_t *ppos)
1120 struct net *net = table->data;
1121 int platform_labels = net->mpls.platform_labels;
1123 struct ctl_table tmp = {
1124 .procname = table->procname,
1125 .data = &platform_labels,
1126 .maxlen = sizeof(int),
1127 .mode = table->mode,
1129 .extra2 = &label_limit,
1132 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1134 if (write && ret == 0)
1135 ret = resize_platform_label_table(net, platform_labels);
1140 static const struct ctl_table mpls_table[] = {
1142 .procname = "platform_labels",
1144 .maxlen = sizeof(int),
1146 .proc_handler = mpls_platform_labels,
1151 static int mpls_net_init(struct net *net)
1153 struct ctl_table *table;
1155 net->mpls.platform_labels = 0;
1156 net->mpls.platform_label = NULL;
1158 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1162 table[0].data = net;
1163 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1164 if (net->mpls.ctl == NULL)
1170 static void mpls_net_exit(struct net *net)
1172 struct mpls_route __rcu **platform_label;
1173 size_t platform_labels;
1174 struct ctl_table *table;
1177 table = net->mpls.ctl->ctl_table_arg;
1178 unregister_net_sysctl_table(net->mpls.ctl);
1181 /* An rcu grace period has passed since there was a device in
1182 * the network namespace (and thus the last in flight packet)
1183 * left this network namespace. This is because
1184 * unregister_netdevice_many and netdev_run_todo has completed
1185 * for each network device that was in this network namespace.
1187 * As such no additional rcu synchronization is necessary when
1188 * freeing the platform_label table.
1191 platform_label = rtnl_dereference(net->mpls.platform_label);
1192 platform_labels = net->mpls.platform_labels;
1193 for (index = 0; index < platform_labels; index++) {
1194 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1195 RCU_INIT_POINTER(platform_label[index], NULL);
1200 kvfree(platform_label);
1203 static struct pernet_operations mpls_net_ops = {
1204 .init = mpls_net_init,
1205 .exit = mpls_net_exit,
1208 static int __init mpls_init(void)
1212 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1214 err = register_pernet_subsys(&mpls_net_ops);
1218 err = register_netdevice_notifier(&mpls_dev_notifier);
1220 goto out_unregister_pernet;
1222 dev_add_pack(&mpls_packet_type);
1224 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1225 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1226 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1231 out_unregister_pernet:
1232 unregister_pernet_subsys(&mpls_net_ops);
1235 module_init(mpls_init);
1237 static void __exit mpls_exit(void)
1239 rtnl_unregister_all(PF_MPLS);
1240 dev_remove_pack(&mpls_packet_type);
1241 unregister_netdevice_notifier(&mpls_dev_notifier);
1242 unregister_pernet_subsys(&mpls_net_ops);
1244 module_exit(mpls_exit);
1246 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1247 MODULE_LICENSE("GPL v2");
1248 MODULE_ALIAS_NETPROTO(PF_MPLS);