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>
14 #include <net/ip_fib.h>
15 #include <net/netevent.h>
16 #include <net/netns/generic.h>
19 #define LABEL_NOT_SPECIFIED (1<<20)
20 #define MAX_NEW_LABELS 2
22 /* This maximum ha length copied from the definition of struct neighbour */
23 #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
25 struct mpls_route { /* next hop label forwarding entry */
26 struct net_device *rt_dev;
27 struct rcu_head rt_rcu;
28 u32 rt_label[MAX_NEW_LABELS];
29 u8 rt_protocol; /* routing protocol that set this entry */
32 unsigned short rt_via_family;
37 static int label_limit = (1 << 20) - 1;
39 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
41 struct mpls_route *rt = NULL;
43 if (index < net->mpls.platform_labels) {
44 struct mpls_route __rcu **platform_label =
45 rcu_dereference(net->mpls.platform_label);
46 rt = rcu_dereference(platform_label[index]);
51 static bool mpls_output_possible(const struct net_device *dev)
53 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
56 static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
58 /* The size of the layer 2.5 labels to be added for this route */
59 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
62 static unsigned int mpls_dev_mtu(const struct net_device *dev)
64 /* The amount of data the layer 2 frame can hold */
68 static 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)
79 static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
80 struct mpls_entry_decoded dec)
82 /* RFC4385 and RFC5586 encode other packets in mpls such that
83 * they don't conflict with the ip version number, making
84 * decoding by examining the ip version correct in everything
85 * except for the strangest cases.
87 * The strange cases if we choose to support them will require
88 * manual configuration.
90 struct iphdr *hdr4 = ip_hdr(skb);
93 if (hdr4->version == 4) {
94 skb->protocol = htons(ETH_P_IP);
95 csum_replace2(&hdr4->check,
96 htons(hdr4->ttl << 8),
100 else if (hdr4->version == 6) {
101 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
102 skb->protocol = htons(ETH_P_IPV6);
103 hdr6->hop_limit = dec.ttl;
106 /* version 0 and version 1 are used by pseudo wires */
111 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
112 struct packet_type *pt, struct net_device *orig_dev)
114 struct net *net = dev_net(dev);
115 struct mpls_shim_hdr *hdr;
116 struct mpls_route *rt;
117 struct mpls_entry_decoded dec;
118 struct net_device *out_dev;
120 unsigned int new_header_size;
124 /* Careful this entire function runs inside of an rcu critical section */
126 if (skb->pkt_type != PACKET_HOST)
129 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
132 if (!pskb_may_pull(skb, sizeof(*hdr)))
135 /* Read and decode the label */
137 dec = mpls_entry_decode(hdr);
140 skb_pull(skb, sizeof(*hdr));
141 skb_reset_network_header(skb);
145 rt = mpls_route_input_rcu(net, dec.label);
149 /* Find the output device */
150 out_dev = rt->rt_dev;
151 if (!mpls_output_possible(out_dev))
154 if (skb_warn_if_lro(skb))
157 skb_forward_csum(skb);
159 /* Verify ttl is valid */
164 /* Verify the destination can hold the packet */
165 new_header_size = mpls_rt_header_size(rt);
166 mtu = mpls_dev_mtu(out_dev);
167 if (mpls_pkt_too_big(skb, mtu - new_header_size))
170 hh_len = LL_RESERVED_SPACE(out_dev);
171 if (!out_dev->header_ops)
174 /* Ensure there is enough space for the headers in the skb */
175 if (skb_cow(skb, hh_len + new_header_size))
179 skb->protocol = htons(ETH_P_MPLS_UC);
181 if (unlikely(!new_header_size && dec.bos)) {
182 /* Penultimate hop popping */
183 if (!mpls_egress(rt, skb, dec))
188 skb_push(skb, new_header_size);
189 skb_reset_network_header(skb);
190 /* Push the new labels */
193 for (i = rt->rt_labels - 1; i >= 0; i--) {
194 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
199 err = neigh_xmit(rt->rt_via_family, out_dev, rt->rt_via, skb);
201 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
210 static struct packet_type mpls_packet_type __read_mostly = {
211 .type = cpu_to_be16(ETH_P_MPLS_UC),
212 .func = mpls_forward,
215 struct mpls_route_config {
220 u8 rc_via[MAX_VIA_ALEN];
222 u32 rc_output_labels;
223 u32 rc_output_label[MAX_NEW_LABELS];
225 struct nl_info rc_nlinfo;
228 static struct mpls_route *mpls_rt_alloc(size_t alen)
230 struct mpls_route *rt;
232 rt = kzalloc(GFP_KERNEL, sizeof(*rt) + alen);
234 rt->rt_via_alen = alen;
238 static void mpls_rt_free(struct mpls_route *rt)
241 kfree_rcu(rt, rt_rcu);
244 static void mpls_route_update(struct net *net, unsigned index,
245 struct net_device *dev, struct mpls_route *new,
246 const struct nl_info *info)
248 struct mpls_route *rt, *old = NULL;
252 rt = net->mpls.platform_label[index];
253 if (!dev || (rt && (rt->rt_dev == dev))) {
254 rcu_assign_pointer(net->mpls.platform_label[index], new);
258 /* If we removed a route free it now */
262 static unsigned find_free_label(struct net *net)
265 for (index = 16; index < net->mpls.platform_labels; index++) {
266 if (!net->mpls.platform_label[index])
269 return LABEL_NOT_SPECIFIED;
272 static int mpls_route_add(struct mpls_route_config *cfg)
274 struct net *net = cfg->rc_nlinfo.nl_net;
275 struct net_device *dev = NULL;
276 struct mpls_route *rt, *old;
281 index = cfg->rc_label;
283 /* If a label was not specified during insert pick one */
284 if ((index == LABEL_NOT_SPECIFIED) &&
285 (cfg->rc_nlflags & NLM_F_CREATE)) {
286 index = find_free_label(net);
289 /* The first 16 labels are reserved, and may not be set */
293 /* The full 20 bit range may not be supported. */
294 if (index >= net->mpls.platform_labels)
297 /* Ensure only a supported number of labels are present */
298 if (cfg->rc_output_labels > MAX_NEW_LABELS)
302 dev = dev_get_by_index(net, cfg->rc_ifindex);
306 /* For now just support ethernet devices */
308 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
312 if ((cfg->rc_via_family == AF_PACKET) &&
313 (dev->addr_len != cfg->rc_via_alen))
316 /* Append makes no sense with mpls */
318 if (cfg->rc_nlflags & NLM_F_APPEND)
322 old = net->mpls.platform_label[index];
323 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
327 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
331 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
335 rt = mpls_rt_alloc(cfg->rc_via_alen);
339 rt->rt_labels = cfg->rc_output_labels;
340 for (i = 0; i < rt->rt_labels; i++)
341 rt->rt_label[i] = cfg->rc_output_label[i];
342 rt->rt_protocol = cfg->rc_protocol;
344 rt->rt_via_family = cfg->rc_via_family;
345 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
347 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
358 static int mpls_route_del(struct mpls_route_config *cfg)
360 struct net *net = cfg->rc_nlinfo.nl_net;
364 index = cfg->rc_label;
366 /* The first 16 labels are reserved, and may not be removed */
370 /* The full 20 bit range may not be supported */
371 if (index >= net->mpls.platform_labels)
374 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
381 static void mpls_ifdown(struct net_device *dev)
383 struct net *net = dev_net(dev);
386 for (index = 0; index < net->mpls.platform_labels; index++) {
387 struct mpls_route *rt = net->mpls.platform_label[index];
390 if (rt->rt_dev != dev)
396 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
399 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
402 case NETDEV_UNREGISTER:
409 static struct notifier_block mpls_dev_notifier = {
410 .notifier_call = mpls_dev_notify,
413 int nla_put_labels(struct sk_buff *skb, int attrtype,
414 u8 labels, const u32 label[])
417 struct mpls_shim_hdr *nla_label;
420 nla = nla_reserve(skb, attrtype, labels*4);
424 nla_label = nla_data(nla);
426 for (i = labels - 1; i >= 0; i--) {
427 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
434 int nla_get_labels(const struct nlattr *nla,
435 u32 max_labels, u32 *labels, u32 label[])
437 unsigned len = nla_len(nla);
439 struct mpls_shim_hdr *nla_label;
443 /* len needs to be an even multiple of 4 (the label size) */
447 /* Limit the number of new labels allowed */
449 if (nla_labels > max_labels)
452 nla_label = nla_data(nla);
454 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
455 struct mpls_entry_decoded dec;
456 dec = mpls_entry_decode(nla_label + i);
458 /* Ensure the bottom of stack flag is properly set
459 * and ttl and tc are both clear.
461 if ((dec.bos != bos) || dec.ttl || dec.tc)
464 label[i] = dec.label;
466 *labels = nla_labels;
470 static int resize_platform_label_table(struct net *net, size_t limit)
472 size_t size = sizeof(struct mpls_route *) * limit;
475 struct mpls_route __rcu **labels = NULL, **old;
476 struct mpls_route *rt0 = NULL, *rt2 = NULL;
480 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
482 labels = vzalloc(size);
488 /* In case the predefined labels need to be populated */
489 if (limit > LABEL_IPV4_EXPLICIT_NULL) {
490 struct net_device *lo = net->loopback_dev;
491 rt0 = mpls_rt_alloc(lo->addr_len);
495 rt0->rt_protocol = RTPROT_KERNEL;
496 rt0->rt_via_family = AF_PACKET;
497 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
499 if (limit > LABEL_IPV6_EXPLICIT_NULL) {
500 struct net_device *lo = net->loopback_dev;
501 rt2 = mpls_rt_alloc(lo->addr_len);
505 rt2->rt_protocol = RTPROT_KERNEL;
506 rt2->rt_via_family = AF_PACKET;
507 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
511 /* Remember the original table */
512 old = net->mpls.platform_label;
513 old_limit = net->mpls.platform_labels;
515 /* Free any labels beyond the new table */
516 for (index = limit; index < old_limit; index++)
517 mpls_route_update(net, index, NULL, NULL, NULL);
519 /* Copy over the old labels */
521 if (old_limit < limit)
522 cp_size = old_limit * sizeof(struct mpls_route *);
524 memcpy(labels, old, cp_size);
526 /* If needed set the predefined labels */
527 if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) &&
528 (limit > LABEL_IPV6_EXPLICIT_NULL)) {
529 labels[LABEL_IPV6_EXPLICIT_NULL] = rt2;
533 if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) &&
534 (limit > LABEL_IPV4_EXPLICIT_NULL)) {
535 labels[LABEL_IPV4_EXPLICIT_NULL] = rt0;
539 /* Update the global pointers */
540 net->mpls.platform_labels = limit;
541 net->mpls.platform_label = labels;
562 static int mpls_platform_labels(struct ctl_table *table, int write,
563 void __user *buffer, size_t *lenp, loff_t *ppos)
565 struct net *net = table->data;
566 int platform_labels = net->mpls.platform_labels;
568 struct ctl_table tmp = {
569 .procname = table->procname,
570 .data = &platform_labels,
571 .maxlen = sizeof(int),
574 .extra2 = &label_limit,
577 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
579 if (write && ret == 0)
580 ret = resize_platform_label_table(net, platform_labels);
585 static struct ctl_table mpls_table[] = {
587 .procname = "platform_labels",
589 .maxlen = sizeof(int),
591 .proc_handler = mpls_platform_labels,
596 static int mpls_net_init(struct net *net)
598 struct ctl_table *table;
600 net->mpls.platform_labels = 0;
601 net->mpls.platform_label = NULL;
603 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
608 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
609 if (net->mpls.ctl == NULL)
615 static void mpls_net_exit(struct net *net)
617 struct ctl_table *table;
620 table = net->mpls.ctl->ctl_table_arg;
621 unregister_net_sysctl_table(net->mpls.ctl);
624 /* An rcu grace period haselapsed since there was a device in
625 * the network namespace (and thus the last in fqlight packet)
626 * left this network namespace. This is because
627 * unregister_netdevice_many and netdev_run_todo has completed
628 * for each network device that was in this network namespace.
630 * As such no additional rcu synchronization is necessary when
631 * freeing the platform_label table.
634 for (index = 0; index < net->mpls.platform_labels; index++) {
635 struct mpls_route *rt = net->mpls.platform_label[index];
636 rcu_assign_pointer(net->mpls.platform_label[index], NULL);
641 kvfree(net->mpls.platform_label);
644 static struct pernet_operations mpls_net_ops = {
645 .init = mpls_net_init,
646 .exit = mpls_net_exit,
649 static int __init mpls_init(void)
653 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
655 err = register_pernet_subsys(&mpls_net_ops);
659 err = register_netdevice_notifier(&mpls_dev_notifier);
661 goto out_unregister_pernet;
663 dev_add_pack(&mpls_packet_type);
669 out_unregister_pernet:
670 unregister_pernet_subsys(&mpls_net_ops);
673 module_init(mpls_init);
675 static void __exit mpls_exit(void)
677 dev_remove_pack(&mpls_packet_type);
678 unregister_netdevice_notifier(&mpls_dev_notifier);
679 unregister_pernet_subsys(&mpls_net_ops);
681 module_exit(mpls_exit);
683 MODULE_DESCRIPTION("MultiProtocol Label Switching");
684 MODULE_LICENSE("GPL v2");
685 MODULE_ALIAS_NETPROTO(PF_MPLS);