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>
26 static int label_limit = (1 << 20) - 1;
28 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
29 struct nlmsghdr *nlh, struct net *net, u32 portid,
30 unsigned int nlm_flags);
32 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
34 struct mpls_route *rt = NULL;
36 if (index < net->mpls.platform_labels) {
37 struct mpls_route __rcu **platform_label =
38 rcu_dereference(net->mpls.platform_label);
39 rt = rcu_dereference(platform_label[index]);
44 static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
46 return rcu_dereference_rtnl(dev->mpls_ptr);
49 bool mpls_output_possible(const struct net_device *dev)
51 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
53 EXPORT_SYMBOL_GPL(mpls_output_possible);
55 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
57 /* The size of the layer 2.5 labels to be added for this route */
58 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
61 unsigned int mpls_dev_mtu(const struct net_device *dev)
63 /* The amount of data the layer 2 frame can hold */
66 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
68 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
73 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
78 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
80 static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt)
82 /* assume single nexthop for now */
86 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
87 struct mpls_entry_decoded dec)
89 enum mpls_payload_type payload_type;
92 /* The IPv4 code below accesses through the IPv4 header
93 * checksum, which is 12 bytes into the packet.
94 * The IPv6 code below accesses through the IPv6 hop limit
95 * which is 8 bytes into the packet.
97 * For all supported cases there should always be at least 12
98 * bytes of packet data present. The IPv4 header is 20 bytes
99 * without options and the IPv6 header is always 40 bytes
102 if (!pskb_may_pull(skb, 12))
105 payload_type = rt->rt_payload_type;
106 if (payload_type == MPT_UNSPEC)
107 payload_type = ip_hdr(skb)->version;
109 switch (payload_type) {
111 struct iphdr *hdr4 = ip_hdr(skb);
112 skb->protocol = htons(ETH_P_IP);
113 csum_replace2(&hdr4->check,
114 htons(hdr4->ttl << 8),
115 htons(dec.ttl << 8));
121 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
122 skb->protocol = htons(ETH_P_IPV6);
123 hdr6->hop_limit = dec.ttl;
134 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
135 struct packet_type *pt, struct net_device *orig_dev)
137 struct net *net = dev_net(dev);
138 struct mpls_shim_hdr *hdr;
139 struct mpls_route *rt;
141 struct mpls_entry_decoded dec;
142 struct net_device *out_dev;
143 struct mpls_dev *mdev;
145 unsigned int new_header_size;
149 /* Careful this entire function runs inside of an rcu critical section */
151 mdev = mpls_dev_get(dev);
152 if (!mdev || !mdev->input_enabled)
155 if (skb->pkt_type != PACKET_HOST)
158 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
161 if (!pskb_may_pull(skb, sizeof(*hdr)))
164 /* Read and decode the label */
166 dec = mpls_entry_decode(hdr);
169 skb_pull(skb, sizeof(*hdr));
170 skb_reset_network_header(skb);
174 rt = mpls_route_input_rcu(net, dec.label);
178 nh = mpls_select_multipath(rt);
182 /* Find the output device */
183 out_dev = rcu_dereference(nh->nh_dev);
184 if (!mpls_output_possible(out_dev))
187 if (skb_warn_if_lro(skb))
190 skb_forward_csum(skb);
192 /* Verify ttl is valid */
197 /* Verify the destination can hold the packet */
198 new_header_size = mpls_nh_header_size(nh);
199 mtu = mpls_dev_mtu(out_dev);
200 if (mpls_pkt_too_big(skb, mtu - new_header_size))
203 hh_len = LL_RESERVED_SPACE(out_dev);
204 if (!out_dev->header_ops)
207 /* Ensure there is enough space for the headers in the skb */
208 if (skb_cow(skb, hh_len + new_header_size))
212 skb->protocol = htons(ETH_P_MPLS_UC);
214 if (unlikely(!new_header_size && dec.bos)) {
215 /* Penultimate hop popping */
216 if (!mpls_egress(rt, skb, dec))
221 skb_push(skb, new_header_size);
222 skb_reset_network_header(skb);
223 /* Push the new labels */
226 for (i = nh->nh_labels - 1; i >= 0; i--) {
227 hdr[i] = mpls_entry_encode(nh->nh_label[i],
233 err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
235 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
244 static struct packet_type mpls_packet_type __read_mostly = {
245 .type = cpu_to_be16(ETH_P_MPLS_UC),
246 .func = mpls_forward,
249 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
250 [RTA_DST] = { .type = NLA_U32 },
251 [RTA_OIF] = { .type = NLA_U32 },
254 struct mpls_route_config {
259 u8 rc_via[MAX_VIA_ALEN];
262 u32 rc_output_label[MAX_NEW_LABELS];
264 enum mpls_payload_type rc_payload_type;
265 struct nl_info rc_nlinfo;
266 struct rtnexthop *rc_mp;
270 static struct mpls_route *mpls_rt_alloc(int num_nh)
272 struct mpls_route *rt;
274 rt = kzalloc(sizeof(*rt) + (num_nh * sizeof(struct mpls_nh)),
282 static void mpls_rt_free(struct mpls_route *rt)
285 kfree_rcu(rt, rt_rcu);
288 static void mpls_notify_route(struct net *net, unsigned index,
289 struct mpls_route *old, struct mpls_route *new,
290 const struct nl_info *info)
292 struct nlmsghdr *nlh = info ? info->nlh : NULL;
293 unsigned portid = info ? info->portid : 0;
294 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
295 struct mpls_route *rt = new ? new : old;
296 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
297 /* Ignore reserved labels for now */
298 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
299 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
302 static void mpls_route_update(struct net *net, unsigned index,
303 struct mpls_route *new,
304 const struct nl_info *info)
306 struct mpls_route __rcu **platform_label;
307 struct mpls_route *rt;
311 platform_label = rtnl_dereference(net->mpls.platform_label);
312 rt = rtnl_dereference(platform_label[index]);
313 rcu_assign_pointer(platform_label[index], new);
315 mpls_notify_route(net, index, rt, new, info);
317 /* If we removed a route free it now */
321 static unsigned find_free_label(struct net *net)
323 struct mpls_route __rcu **platform_label;
324 size_t platform_labels;
327 platform_label = rtnl_dereference(net->mpls.platform_label);
328 platform_labels = net->mpls.platform_labels;
329 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
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;
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);
357 static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
359 return ERR_PTR(-EAFNOSUPPORT);
363 #if IS_ENABLED(CONFIG_IPV6)
364 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
366 struct net_device *dev;
367 struct dst_entry *dst;
372 return ERR_PTR(-EAFNOSUPPORT);
374 memset(&fl6, 0, sizeof(fl6));
375 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
376 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
387 static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
389 return ERR_PTR(-EAFNOSUPPORT);
393 static struct net_device *find_outdev(struct net *net,
394 struct mpls_nh *nh, int oif)
396 struct net_device *dev = NULL;
399 switch (nh->nh_via_table) {
400 case NEIGH_ARP_TABLE:
401 dev = inet_fib_lookup_dev(net, nh->nh_via);
404 dev = inet6_fib_lookup_dev(net, nh->nh_via);
406 case NEIGH_LINK_TABLE:
410 dev = dev_get_by_index(net, oif);
414 return ERR_PTR(-ENODEV);
416 /* The caller is holding rtnl anyways, so release the dev reference */
422 static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
424 struct net_device *dev = NULL;
427 dev = find_outdev(net, nh, oif);
434 /* Ensure this is a supported device */
436 if (!mpls_dev_get(dev))
439 RCU_INIT_POINTER(nh->nh_dev, dev);
447 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
448 struct mpls_route *rt)
450 struct net *net = cfg->rc_nlinfo.nl_net;
451 struct mpls_nh *nh = rt->rt_nh;
459 /* Ensure only a supported number of labels are present */
460 if (cfg->rc_output_labels > MAX_NEW_LABELS)
463 nh->nh_labels = cfg->rc_output_labels;
464 for (i = 0; i < nh->nh_labels; i++)
465 nh->nh_label[i] = cfg->rc_output_label[i];
467 nh->nh_via_table = cfg->rc_via_table;
468 memcpy(nh->nh_via, cfg->rc_via, cfg->rc_via_alen);
469 nh->nh_via_alen = cfg->rc_via_alen;
471 err = mpls_nh_assign_dev(net, nh, cfg->rc_ifindex);
481 static int mpls_nh_build(struct net *net, struct mpls_nh *nh,
482 int oif, struct nlattr *via, struct nlattr *newdst)
490 err = nla_get_labels(newdst, MAX_NEW_LABELS,
491 &nh->nh_labels, nh->nh_label);
496 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
501 err = mpls_nh_assign_dev(net, nh, oif);
511 static int mpls_count_nexthops(struct rtnexthop *rtnh, int len)
516 while (rtnh_ok(rtnh, remaining)) {
518 rtnh = rtnh_next(rtnh, &remaining);
521 /* leftover implies invalid nexthop configuration, discard it */
522 return remaining > 0 ? 0 : nhs;
525 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
526 struct mpls_route *rt)
528 struct rtnexthop *rtnh = cfg->rc_mp;
529 struct nlattr *nla_via, *nla_newdst;
530 int remaining = cfg->rc_mp_len;
534 change_nexthops(rt) {
541 if (!rtnh_ok(rtnh, remaining))
544 attrlen = rtnh_attrlen(rtnh);
546 struct nlattr *attrs = rtnh_attrs(rtnh);
548 nla_via = nla_find(attrs, attrlen, RTA_VIA);
549 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
555 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, nh,
556 rtnh->rtnh_ifindex, nla_via,
561 rtnh = rtnh_next(rtnh, &remaining);
563 } endfor_nexthops(rt);
573 static int mpls_route_add(struct mpls_route_config *cfg)
575 struct mpls_route __rcu **platform_label;
576 struct net *net = cfg->rc_nlinfo.nl_net;
577 struct mpls_route *rt, *old;
580 int nhs = 1; /* default to one nexthop */
582 index = cfg->rc_label;
584 /* If a label was not specified during insert pick one */
585 if ((index == LABEL_NOT_SPECIFIED) &&
586 (cfg->rc_nlflags & NLM_F_CREATE)) {
587 index = find_free_label(net);
590 /* Reserved labels may not be set */
591 if (index < MPLS_LABEL_FIRST_UNRESERVED)
594 /* The full 20 bit range may not be supported. */
595 if (index >= net->mpls.platform_labels)
598 /* Append makes no sense with mpls */
600 if (cfg->rc_nlflags & NLM_F_APPEND)
604 platform_label = rtnl_dereference(net->mpls.platform_label);
605 old = rtnl_dereference(platform_label[index]);
606 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
610 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
614 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
619 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len);
625 rt = mpls_rt_alloc(nhs);
629 rt->rt_protocol = cfg->rc_protocol;
630 rt->rt_payload_type = cfg->rc_payload_type;
633 err = mpls_nh_build_multi(cfg, rt);
635 err = mpls_nh_build_from_cfg(cfg, rt);
639 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
649 static int mpls_route_del(struct mpls_route_config *cfg)
651 struct net *net = cfg->rc_nlinfo.nl_net;
655 index = cfg->rc_label;
657 /* Reserved labels may not be removed */
658 if (index < MPLS_LABEL_FIRST_UNRESERVED)
661 /* The full 20 bit range may not be supported */
662 if (index >= net->mpls.platform_labels)
665 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
672 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
673 (&((struct mpls_dev *)0)->field)
675 static const struct ctl_table mpls_dev_table[] = {
678 .maxlen = sizeof(int),
680 .proc_handler = proc_dointvec,
681 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
686 static int mpls_dev_sysctl_register(struct net_device *dev,
687 struct mpls_dev *mdev)
689 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
690 struct ctl_table *table;
693 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
697 /* Table data contains only offsets relative to the base of
698 * the mdev at this point, so make them absolute.
700 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
701 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
703 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
705 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
717 static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
719 struct ctl_table *table;
721 table = mdev->sysctl->ctl_table_arg;
722 unregister_net_sysctl_table(mdev->sysctl);
726 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
728 struct mpls_dev *mdev;
733 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
737 err = mpls_dev_sysctl_register(dev, mdev);
741 rcu_assign_pointer(dev->mpls_ptr, mdev);
750 static void mpls_ifdown(struct net_device *dev)
752 struct mpls_route __rcu **platform_label;
753 struct net *net = dev_net(dev);
754 struct mpls_dev *mdev;
757 platform_label = rtnl_dereference(net->mpls.platform_label);
758 for (index = 0; index < net->mpls.platform_labels; index++) {
759 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
763 if (rtnl_dereference(nh->nh_dev) != dev)
766 } endfor_nexthops(rt);
769 mdev = mpls_dev_get(dev);
773 mpls_dev_sysctl_unregister(mdev);
775 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
777 kfree_rcu(mdev, rcu);
780 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
783 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
784 struct mpls_dev *mdev;
787 case NETDEV_REGISTER:
788 /* For now just support ethernet devices */
789 if ((dev->type == ARPHRD_ETHER) ||
790 (dev->type == ARPHRD_LOOPBACK)) {
791 mdev = mpls_add_dev(dev);
793 return notifier_from_errno(PTR_ERR(mdev));
797 case NETDEV_UNREGISTER:
800 case NETDEV_CHANGENAME:
801 mdev = mpls_dev_get(dev);
805 mpls_dev_sysctl_unregister(mdev);
806 err = mpls_dev_sysctl_register(dev, mdev);
808 return notifier_from_errno(err);
815 static struct notifier_block mpls_dev_notifier = {
816 .notifier_call = mpls_dev_notify,
819 static int nla_put_via(struct sk_buff *skb,
820 u8 table, const void *addr, int alen)
822 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
823 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
827 int family = AF_UNSPEC;
829 nla = nla_reserve(skb, RTA_VIA, alen + 2);
833 if (table <= NEIGH_NR_TABLES)
834 family = table_to_family[table];
837 via->rtvia_family = family;
838 memcpy(via->rtvia_addr, addr, alen);
842 int nla_put_labels(struct sk_buff *skb, int attrtype,
843 u8 labels, const u32 label[])
846 struct mpls_shim_hdr *nla_label;
849 nla = nla_reserve(skb, attrtype, labels*4);
853 nla_label = nla_data(nla);
855 for (i = labels - 1; i >= 0; i--) {
856 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
862 EXPORT_SYMBOL_GPL(nla_put_labels);
864 int nla_get_labels(const struct nlattr *nla,
865 u32 max_labels, u8 *labels, u32 label[])
867 unsigned len = nla_len(nla);
869 struct mpls_shim_hdr *nla_label;
873 /* len needs to be an even multiple of 4 (the label size) */
877 /* Limit the number of new labels allowed */
879 if (nla_labels > max_labels)
882 nla_label = nla_data(nla);
884 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
885 struct mpls_entry_decoded dec;
886 dec = mpls_entry_decode(nla_label + i);
888 /* Ensure the bottom of stack flag is properly set
889 * and ttl and tc are both clear.
891 if ((dec.bos != bos) || dec.ttl || dec.tc)
895 case MPLS_LABEL_IMPLNULL:
896 /* RFC3032: This is a label that an LSR may
897 * assign and distribute, but which never
898 * actually appears in the encapsulation.
903 label[i] = dec.label;
905 *labels = nla_labels;
908 EXPORT_SYMBOL_GPL(nla_get_labels);
910 int nla_get_via(const struct nlattr *nla, u8 *via_alen,
911 u8 *via_table, u8 via_addr[])
913 struct rtvia *via = nla_data(nla);
917 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
919 alen = nla_len(nla) -
920 offsetof(struct rtvia, rtvia_addr);
921 if (alen > MAX_VIA_ALEN)
924 /* Validate the address family */
925 switch (via->rtvia_family) {
927 *via_table = NEIGH_LINK_TABLE;
930 *via_table = NEIGH_ARP_TABLE;
935 *via_table = NEIGH_ND_TABLE;
940 /* Unsupported address family */
944 memcpy(via_addr, via->rtvia_addr, alen);
952 static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
953 struct mpls_route_config *cfg)
956 struct nlattr *tb[RTA_MAX+1];
960 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
965 rtm = nlmsg_data(nlh);
966 memset(cfg, 0, sizeof(*cfg));
968 if (rtm->rtm_family != AF_MPLS)
970 if (rtm->rtm_dst_len != 20)
972 if (rtm->rtm_src_len != 0)
974 if (rtm->rtm_tos != 0)
976 if (rtm->rtm_table != RT_TABLE_MAIN)
978 /* Any value is acceptable for rtm_protocol */
980 /* As mpls uses destination specific addresses
981 * (or source specific address in the case of multicast)
982 * all addresses have universal scope.
984 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
986 if (rtm->rtm_type != RTN_UNICAST)
988 if (rtm->rtm_flags != 0)
991 cfg->rc_label = LABEL_NOT_SPECIFIED;
992 cfg->rc_protocol = rtm->rtm_protocol;
993 cfg->rc_nlflags = nlh->nlmsg_flags;
994 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
995 cfg->rc_nlinfo.nlh = nlh;
996 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
998 for (index = 0; index <= RTA_MAX; index++) {
999 struct nlattr *nla = tb[index];
1005 cfg->rc_ifindex = nla_get_u32(nla);
1008 if (nla_get_labels(nla, MAX_NEW_LABELS,
1009 &cfg->rc_output_labels,
1010 cfg->rc_output_label))
1016 if (nla_get_labels(nla, 1, &label_count,
1020 /* Reserved labels may not be set */
1021 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
1028 if (nla_get_via(nla, &cfg->rc_via_alen,
1029 &cfg->rc_via_table, cfg->rc_via))
1035 cfg->rc_mp = nla_data(nla);
1036 cfg->rc_mp_len = nla_len(nla);
1040 /* Unsupported attribute */
1050 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1052 struct mpls_route_config cfg;
1055 err = rtm_to_route_config(skb, nlh, &cfg);
1059 return mpls_route_del(&cfg);
1063 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
1065 struct mpls_route_config cfg;
1068 err = rtm_to_route_config(skb, nlh, &cfg);
1072 return mpls_route_add(&cfg);
1075 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
1076 u32 label, struct mpls_route *rt, int flags)
1078 struct net_device *dev;
1079 struct nlmsghdr *nlh;
1082 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1086 rtm = nlmsg_data(nlh);
1087 rtm->rtm_family = AF_MPLS;
1088 rtm->rtm_dst_len = 20;
1089 rtm->rtm_src_len = 0;
1091 rtm->rtm_table = RT_TABLE_MAIN;
1092 rtm->rtm_protocol = rt->rt_protocol;
1093 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1094 rtm->rtm_type = RTN_UNICAST;
1097 if (nla_put_labels(skb, RTA_DST, 1, &label))
1098 goto nla_put_failure;
1099 if (rt->rt_nhn == 1) {
1100 struct mpls_nh *nh = rt->rt_nh;
1102 if (nh->nh_labels &&
1103 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
1105 goto nla_put_failure;
1106 if (nla_put_via(skb, nh->nh_via_table, nh->nh_via,
1108 goto nla_put_failure;
1109 dev = rtnl_dereference(nh->nh_dev);
1110 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
1111 goto nla_put_failure;
1113 struct rtnexthop *rtnh;
1116 mp = nla_nest_start(skb, RTA_MULTIPATH);
1118 goto nla_put_failure;
1121 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1123 goto nla_put_failure;
1125 dev = rtnl_dereference(nh->nh_dev);
1127 rtnh->rtnh_ifindex = dev->ifindex;
1128 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
1131 goto nla_put_failure;
1132 if (nla_put_via(skb, nh->nh_via_table,
1135 goto nla_put_failure;
1137 /* length of rtnetlink header + attributes */
1138 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1139 } endfor_nexthops(rt);
1141 nla_nest_end(skb, mp);
1144 nlmsg_end(skb, nlh);
1148 nlmsg_cancel(skb, nlh);
1152 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
1154 struct net *net = sock_net(skb->sk);
1155 struct mpls_route __rcu **platform_label;
1156 size_t platform_labels;
1161 index = cb->args[0];
1162 if (index < MPLS_LABEL_FIRST_UNRESERVED)
1163 index = MPLS_LABEL_FIRST_UNRESERVED;
1165 platform_label = rtnl_dereference(net->mpls.platform_label);
1166 platform_labels = net->mpls.platform_labels;
1167 for (; index < platform_labels; index++) {
1168 struct mpls_route *rt;
1169 rt = rtnl_dereference(platform_label[index]);
1173 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
1174 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1175 index, rt, NLM_F_MULTI) < 0)
1178 cb->args[0] = index;
1183 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
1186 NLMSG_ALIGN(sizeof(struct rtmsg))
1187 + nla_total_size(4); /* RTA_DST */
1189 if (rt->rt_nhn == 1) {
1190 struct mpls_nh *nh = rt->rt_nh;
1193 payload += nla_total_size(4); /* RTA_OIF */
1194 payload += nla_total_size(2 + nh->nh_via_alen); /* RTA_VIA */
1195 if (nh->nh_labels) /* RTA_NEWDST */
1196 payload += nla_total_size(nh->nh_labels * 4);
1198 /* each nexthop is packed in an attribute */
1202 nhsize += nla_total_size(sizeof(struct rtnexthop));
1203 nhsize += nla_total_size(2 + nh->nh_via_alen);
1205 nhsize += nla_total_size(nh->nh_labels * 4);
1206 } endfor_nexthops(rt);
1207 /* nested attribute */
1208 payload += nla_total_size(nhsize);
1214 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
1215 struct nlmsghdr *nlh, struct net *net, u32 portid,
1216 unsigned int nlm_flags)
1218 struct sk_buff *skb;
1219 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1222 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1226 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1228 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1229 WARN_ON(err == -EMSGSIZE);
1233 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1238 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1241 static int resize_platform_label_table(struct net *net, size_t limit)
1243 size_t size = sizeof(struct mpls_route *) * limit;
1246 struct mpls_route __rcu **labels = NULL, **old;
1247 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1251 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1253 labels = vzalloc(size);
1259 /* In case the predefined labels need to be populated */
1260 if (limit > MPLS_LABEL_IPV4NULL) {
1261 struct net_device *lo = net->loopback_dev;
1262 rt0 = mpls_rt_alloc(1);
1265 RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
1266 rt0->rt_protocol = RTPROT_KERNEL;
1267 rt0->rt_payload_type = MPT_IPV4;
1268 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1269 memcpy(rt0->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
1271 if (limit > MPLS_LABEL_IPV6NULL) {
1272 struct net_device *lo = net->loopback_dev;
1273 rt2 = mpls_rt_alloc(1);
1276 RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
1277 rt2->rt_protocol = RTPROT_KERNEL;
1278 rt2->rt_payload_type = MPT_IPV6;
1279 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
1280 memcpy(rt2->rt_nh->nh_via, lo->dev_addr, lo->addr_len);
1284 /* Remember the original table */
1285 old = rtnl_dereference(net->mpls.platform_label);
1286 old_limit = net->mpls.platform_labels;
1288 /* Free any labels beyond the new table */
1289 for (index = limit; index < old_limit; index++)
1290 mpls_route_update(net, index, NULL, NULL);
1292 /* Copy over the old labels */
1294 if (old_limit < limit)
1295 cp_size = old_limit * sizeof(struct mpls_route *);
1297 memcpy(labels, old, cp_size);
1299 /* If needed set the predefined labels */
1300 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1301 (limit > MPLS_LABEL_IPV6NULL)) {
1302 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
1306 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1307 (limit > MPLS_LABEL_IPV4NULL)) {
1308 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
1312 /* Update the global pointers */
1313 net->mpls.platform_labels = limit;
1314 rcu_assign_pointer(net->mpls.platform_label, labels);
1335 static int mpls_platform_labels(struct ctl_table *table, int write,
1336 void __user *buffer, size_t *lenp, loff_t *ppos)
1338 struct net *net = table->data;
1339 int platform_labels = net->mpls.platform_labels;
1341 struct ctl_table tmp = {
1342 .procname = table->procname,
1343 .data = &platform_labels,
1344 .maxlen = sizeof(int),
1345 .mode = table->mode,
1347 .extra2 = &label_limit,
1350 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1352 if (write && ret == 0)
1353 ret = resize_platform_label_table(net, platform_labels);
1358 static const struct ctl_table mpls_table[] = {
1360 .procname = "platform_labels",
1362 .maxlen = sizeof(int),
1364 .proc_handler = mpls_platform_labels,
1369 static int mpls_net_init(struct net *net)
1371 struct ctl_table *table;
1373 net->mpls.platform_labels = 0;
1374 net->mpls.platform_label = NULL;
1376 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1380 table[0].data = net;
1381 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1382 if (net->mpls.ctl == NULL) {
1390 static void mpls_net_exit(struct net *net)
1392 struct mpls_route __rcu **platform_label;
1393 size_t platform_labels;
1394 struct ctl_table *table;
1397 table = net->mpls.ctl->ctl_table_arg;
1398 unregister_net_sysctl_table(net->mpls.ctl);
1401 /* An rcu grace period has passed since there was a device in
1402 * the network namespace (and thus the last in flight packet)
1403 * left this network namespace. This is because
1404 * unregister_netdevice_many and netdev_run_todo has completed
1405 * for each network device that was in this network namespace.
1407 * As such no additional rcu synchronization is necessary when
1408 * freeing the platform_label table.
1411 platform_label = rtnl_dereference(net->mpls.platform_label);
1412 platform_labels = net->mpls.platform_labels;
1413 for (index = 0; index < platform_labels; index++) {
1414 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1415 RCU_INIT_POINTER(platform_label[index], NULL);
1420 kvfree(platform_label);
1423 static struct pernet_operations mpls_net_ops = {
1424 .init = mpls_net_init,
1425 .exit = mpls_net_exit,
1428 static int __init mpls_init(void)
1432 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1434 err = register_pernet_subsys(&mpls_net_ops);
1438 err = register_netdevice_notifier(&mpls_dev_notifier);
1440 goto out_unregister_pernet;
1442 dev_add_pack(&mpls_packet_type);
1444 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1445 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1446 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1451 out_unregister_pernet:
1452 unregister_pernet_subsys(&mpls_net_ops);
1455 module_init(mpls_init);
1457 static void __exit mpls_exit(void)
1459 rtnl_unregister_all(PF_MPLS);
1460 dev_remove_pack(&mpls_packet_type);
1461 unregister_netdevice_notifier(&mpls_dev_notifier);
1462 unregister_pernet_subsys(&mpls_net_ops);
1464 module_exit(mpls_exit);
1466 MODULE_DESCRIPTION("MultiProtocol Label Switching");
1467 MODULE_LICENSE("GPL v2");
1468 MODULE_ALIAS_NETPROTO(PF_MPLS);