]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/mpls/af_mpls.c
ipv6: change ipv6_stub_impl.ipv6_dst_lookup to take net argument
[mirror_ubuntu-hirsute-kernel.git] / net / mpls / af_mpls.c
CommitLineData
0189197f
EB
1#include <linux/types.h>
2#include <linux/skbuff.h>
3#include <linux/socket.h>
7720c01f 4#include <linux/sysctl.h>
0189197f
EB
5#include <linux/net.h>
6#include <linux/module.h>
7#include <linux/if_arp.h>
8#include <linux/ipv6.h>
9#include <linux/mpls.h>
4b5edb2f 10#include <linux/vmalloc.h>
0189197f
EB
11#include <net/ip.h>
12#include <net/dst.h>
13#include <net/sock.h>
14#include <net/arp.h>
15#include <net/ip_fib.h>
16#include <net/netevent.h>
17#include <net/netns/generic.h>
01faef2c 18#include <net/ip6_route.h>
0189197f
EB
19#include "internal.h"
20
a2519929 21#define LABEL_NOT_SPECIFIED (1<<20)
0189197f
EB
22#define MAX_NEW_LABELS 2
23
24/* This maximum ha length copied from the definition of struct neighbour */
25#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
26
27struct mpls_route { /* next hop label forwarding entry */
19d0c341 28 struct net_device __rcu *rt_dev;
0189197f
EB
29 struct rcu_head rt_rcu;
30 u32 rt_label[MAX_NEW_LABELS];
31 u8 rt_protocol; /* routing protocol that set this entry */
b79bda3d
EB
32 u8 rt_labels;
33 u8 rt_via_alen;
34 u8 rt_via_table;
0189197f
EB
35 u8 rt_via[0];
36};
37
7720c01f
EB
38static int zero = 0;
39static int label_limit = (1 << 20) - 1;
40
8de147dc
EB
41static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
42 struct nlmsghdr *nlh, struct net *net, u32 portid,
43 unsigned int nlm_flags);
44
0189197f
EB
45static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
46{
47 struct mpls_route *rt = NULL;
48
49 if (index < net->mpls.platform_labels) {
50 struct mpls_route __rcu **platform_label =
51 rcu_dereference(net->mpls.platform_label);
52 rt = rcu_dereference(platform_label[index]);
53 }
54 return rt;
55}
56
03c57747
RS
57static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
58{
59 return rcu_dereference_rtnl(dev->mpls_ptr);
60}
61
face0188 62bool mpls_output_possible(const struct net_device *dev)
0189197f
EB
63{
64 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
65}
face0188 66EXPORT_SYMBOL_GPL(mpls_output_possible);
0189197f
EB
67
68static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
69{
70 /* The size of the layer 2.5 labels to be added for this route */
71 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
72}
73
face0188 74unsigned int mpls_dev_mtu(const struct net_device *dev)
0189197f
EB
75{
76 /* The amount of data the layer 2 frame can hold */
77 return dev->mtu;
78}
face0188 79EXPORT_SYMBOL_GPL(mpls_dev_mtu);
0189197f 80
face0188 81bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
0189197f
EB
82{
83 if (skb->len <= mtu)
84 return false;
85
86 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
87 return false;
88
89 return true;
90}
face0188 91EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
0189197f
EB
92
93static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
94 struct mpls_entry_decoded dec)
95{
96 /* RFC4385 and RFC5586 encode other packets in mpls such that
97 * they don't conflict with the ip version number, making
98 * decoding by examining the ip version correct in everything
99 * except for the strangest cases.
100 *
101 * The strange cases if we choose to support them will require
102 * manual configuration.
103 */
76fecd82 104 struct iphdr *hdr4;
0189197f
EB
105 bool success = true;
106
76fecd82
EB
107 /* The IPv4 code below accesses through the IPv4 header
108 * checksum, which is 12 bytes into the packet.
109 * The IPv6 code below accesses through the IPv6 hop limit
110 * which is 8 bytes into the packet.
111 *
112 * For all supported cases there should always be at least 12
113 * bytes of packet data present. The IPv4 header is 20 bytes
114 * without options and the IPv6 header is always 40 bytes
115 * long.
116 */
117 if (!pskb_may_pull(skb, 12))
118 return false;
119
120 /* Use ip_hdr to find the ip protocol version */
121 hdr4 = ip_hdr(skb);
0189197f
EB
122 if (hdr4->version == 4) {
123 skb->protocol = htons(ETH_P_IP);
124 csum_replace2(&hdr4->check,
125 htons(hdr4->ttl << 8),
126 htons(dec.ttl << 8));
127 hdr4->ttl = dec.ttl;
128 }
129 else if (hdr4->version == 6) {
130 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
131 skb->protocol = htons(ETH_P_IPV6);
132 hdr6->hop_limit = dec.ttl;
133 }
134 else
135 /* version 0 and version 1 are used by pseudo wires */
136 success = false;
137 return success;
138}
139
140static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
141 struct packet_type *pt, struct net_device *orig_dev)
142{
143 struct net *net = dev_net(dev);
144 struct mpls_shim_hdr *hdr;
145 struct mpls_route *rt;
146 struct mpls_entry_decoded dec;
147 struct net_device *out_dev;
03c57747 148 struct mpls_dev *mdev;
0189197f
EB
149 unsigned int hh_len;
150 unsigned int new_header_size;
151 unsigned int mtu;
152 int err;
153
154 /* Careful this entire function runs inside of an rcu critical section */
155
03c57747 156 mdev = mpls_dev_get(dev);
37bde799 157 if (!mdev || !mdev->input_enabled)
03c57747
RS
158 goto drop;
159
0189197f
EB
160 if (skb->pkt_type != PACKET_HOST)
161 goto drop;
162
163 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
164 goto drop;
165
166 if (!pskb_may_pull(skb, sizeof(*hdr)))
167 goto drop;
168
169 /* Read and decode the label */
170 hdr = mpls_hdr(skb);
171 dec = mpls_entry_decode(hdr);
172
173 /* Pop the label */
174 skb_pull(skb, sizeof(*hdr));
175 skb_reset_network_header(skb);
176
177 skb_orphan(skb);
178
179 rt = mpls_route_input_rcu(net, dec.label);
180 if (!rt)
181 goto drop;
182
183 /* Find the output device */
19d0c341 184 out_dev = rcu_dereference(rt->rt_dev);
0189197f
EB
185 if (!mpls_output_possible(out_dev))
186 goto drop;
187
188 if (skb_warn_if_lro(skb))
189 goto drop;
190
191 skb_forward_csum(skb);
192
193 /* Verify ttl is valid */
aa7da937 194 if (dec.ttl <= 1)
0189197f
EB
195 goto drop;
196 dec.ttl -= 1;
197
198 /* Verify the destination can hold the packet */
199 new_header_size = mpls_rt_header_size(rt);
200 mtu = mpls_dev_mtu(out_dev);
201 if (mpls_pkt_too_big(skb, mtu - new_header_size))
202 goto drop;
203
204 hh_len = LL_RESERVED_SPACE(out_dev);
205 if (!out_dev->header_ops)
206 hh_len = 0;
207
208 /* Ensure there is enough space for the headers in the skb */
209 if (skb_cow(skb, hh_len + new_header_size))
210 goto drop;
211
212 skb->dev = out_dev;
213 skb->protocol = htons(ETH_P_MPLS_UC);
214
215 if (unlikely(!new_header_size && dec.bos)) {
216 /* Penultimate hop popping */
217 if (!mpls_egress(rt, skb, dec))
218 goto drop;
219 } else {
220 bool bos;
221 int i;
222 skb_push(skb, new_header_size);
223 skb_reset_network_header(skb);
224 /* Push the new labels */
225 hdr = mpls_hdr(skb);
226 bos = dec.bos;
227 for (i = rt->rt_labels - 1; i >= 0; i--) {
228 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
229 bos = false;
230 }
231 }
232
b79bda3d 233 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
0189197f
EB
234 if (err)
235 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
236 __func__, err);
237 return 0;
238
239drop:
240 kfree_skb(skb);
241 return NET_RX_DROP;
242}
243
244static struct packet_type mpls_packet_type __read_mostly = {
245 .type = cpu_to_be16(ETH_P_MPLS_UC),
246 .func = mpls_forward,
247};
248
f0126539 249static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
03c05665
EB
250 [RTA_DST] = { .type = NLA_U32 },
251 [RTA_OIF] = { .type = NLA_U32 },
252};
253
a2519929
EB
254struct mpls_route_config {
255 u32 rc_protocol;
256 u32 rc_ifindex;
b79bda3d 257 u16 rc_via_table;
a2519929
EB
258 u16 rc_via_alen;
259 u8 rc_via[MAX_VIA_ALEN];
260 u32 rc_label;
261 u32 rc_output_labels;
262 u32 rc_output_label[MAX_NEW_LABELS];
263 u32 rc_nlflags;
264 struct nl_info rc_nlinfo;
265};
266
0189197f
EB
267static struct mpls_route *mpls_rt_alloc(size_t alen)
268{
269 struct mpls_route *rt;
270
d865616e 271 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
0189197f
EB
272 if (rt)
273 rt->rt_via_alen = alen;
274 return rt;
275}
276
277static void mpls_rt_free(struct mpls_route *rt)
278{
279 if (rt)
280 kfree_rcu(rt, rt_rcu);
281}
282
8de147dc
EB
283static void mpls_notify_route(struct net *net, unsigned index,
284 struct mpls_route *old, struct mpls_route *new,
285 const struct nl_info *info)
286{
287 struct nlmsghdr *nlh = info ? info->nlh : NULL;
288 unsigned portid = info ? info->portid : 0;
289 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
290 struct mpls_route *rt = new ? new : old;
291 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
292 /* Ignore reserved labels for now */
293 if (rt && (index >= 16))
294 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
295}
296
0189197f
EB
297static void mpls_route_update(struct net *net, unsigned index,
298 struct net_device *dev, struct mpls_route *new,
299 const struct nl_info *info)
300{
19d0c341 301 struct mpls_route __rcu **platform_label;
0189197f
EB
302 struct mpls_route *rt, *old = NULL;
303
304 ASSERT_RTNL();
305
19d0c341
EB
306 platform_label = rtnl_dereference(net->mpls.platform_label);
307 rt = rtnl_dereference(platform_label[index]);
308 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
309 rcu_assign_pointer(platform_label[index], new);
0189197f
EB
310 old = rt;
311 }
312
8de147dc
EB
313 mpls_notify_route(net, index, old, new, info);
314
0189197f
EB
315 /* If we removed a route free it now */
316 mpls_rt_free(old);
317}
318
a2519929
EB
319static unsigned find_free_label(struct net *net)
320{
19d0c341
EB
321 struct mpls_route __rcu **platform_label;
322 size_t platform_labels;
a2519929 323 unsigned index;
19d0c341
EB
324
325 platform_label = rtnl_dereference(net->mpls.platform_label);
326 platform_labels = net->mpls.platform_labels;
327 for (index = 16; index < platform_labels; index++) {
328 if (!rtnl_dereference(platform_label[index]))
a2519929
EB
329 return index;
330 }
331 return LABEL_NOT_SPECIFIED;
332}
333
01faef2c
RP
334static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
335{
336 struct net_device *dev = NULL;
337 struct rtable *rt;
338 struct in_addr daddr;
339
340 memcpy(&daddr, addr, sizeof(struct in_addr));
341 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
342 if (IS_ERR(rt))
343 goto errout;
344
345 dev = rt->dst.dev;
346 dev_hold(dev);
347
348 ip_rt_put(rt);
349
350errout:
351 return dev;
352}
353
354static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
355{
356 struct net_device *dev = NULL;
357 struct dst_entry *dst;
358 struct flowi6 fl6;
359
360 memset(&fl6, 0, sizeof(fl6));
361 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
362 dst = ip6_route_output(net, NULL, &fl6);
363 if (dst->error)
364 goto errout;
365
366 dev = dst->dev;
367 dev_hold(dev);
368
369errout:
370 dst_release(dst);
371
372 return dev;
373}
374
375static struct net_device *find_outdev(struct net *net,
376 struct mpls_route_config *cfg)
377{
378 struct net_device *dev = NULL;
379
380 if (!cfg->rc_ifindex) {
381 switch (cfg->rc_via_table) {
382 case NEIGH_ARP_TABLE:
383 dev = inet_fib_lookup_dev(net, cfg->rc_via);
384 break;
385 case NEIGH_ND_TABLE:
386 dev = inet6_fib_lookup_dev(net, cfg->rc_via);
387 break;
388 case NEIGH_LINK_TABLE:
389 break;
390 }
391 } else {
392 dev = dev_get_by_index(net, cfg->rc_ifindex);
393 }
394
395 return dev;
396}
397
a2519929
EB
398static int mpls_route_add(struct mpls_route_config *cfg)
399{
19d0c341 400 struct mpls_route __rcu **platform_label;
a2519929
EB
401 struct net *net = cfg->rc_nlinfo.nl_net;
402 struct net_device *dev = NULL;
403 struct mpls_route *rt, *old;
404 unsigned index;
405 int i;
406 int err = -EINVAL;
407
408 index = cfg->rc_label;
409
410 /* If a label was not specified during insert pick one */
411 if ((index == LABEL_NOT_SPECIFIED) &&
412 (cfg->rc_nlflags & NLM_F_CREATE)) {
413 index = find_free_label(net);
414 }
415
416 /* The first 16 labels are reserved, and may not be set */
417 if (index < 16)
418 goto errout;
419
420 /* The full 20 bit range may not be supported. */
421 if (index >= net->mpls.platform_labels)
422 goto errout;
423
424 /* Ensure only a supported number of labels are present */
425 if (cfg->rc_output_labels > MAX_NEW_LABELS)
426 goto errout;
427
428 err = -ENODEV;
01faef2c 429 dev = find_outdev(net, cfg);
a2519929
EB
430 if (!dev)
431 goto errout;
432
03c57747 433 /* Ensure this is a supported device */
a2519929 434 err = -EINVAL;
03c57747 435 if (!mpls_dev_get(dev))
a2519929
EB
436 goto errout;
437
438 err = -EINVAL;
b79bda3d 439 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
a2519929
EB
440 (dev->addr_len != cfg->rc_via_alen))
441 goto errout;
442
443 /* Append makes no sense with mpls */
0f7bbd58 444 err = -EOPNOTSUPP;
a2519929
EB
445 if (cfg->rc_nlflags & NLM_F_APPEND)
446 goto errout;
447
448 err = -EEXIST;
19d0c341
EB
449 platform_label = rtnl_dereference(net->mpls.platform_label);
450 old = rtnl_dereference(platform_label[index]);
a2519929
EB
451 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
452 goto errout;
453
454 err = -EEXIST;
455 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
456 goto errout;
457
458 err = -ENOENT;
459 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
460 goto errout;
461
462 err = -ENOMEM;
463 rt = mpls_rt_alloc(cfg->rc_via_alen);
464 if (!rt)
465 goto errout;
466
467 rt->rt_labels = cfg->rc_output_labels;
468 for (i = 0; i < rt->rt_labels; i++)
469 rt->rt_label[i] = cfg->rc_output_label[i];
470 rt->rt_protocol = cfg->rc_protocol;
19d0c341 471 RCU_INIT_POINTER(rt->rt_dev, dev);
b79bda3d 472 rt->rt_via_table = cfg->rc_via_table;
a2519929
EB
473 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
474
475 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
476
477 dev_put(dev);
478 return 0;
479
480errout:
481 if (dev)
482 dev_put(dev);
483 return err;
484}
485
486static int mpls_route_del(struct mpls_route_config *cfg)
487{
488 struct net *net = cfg->rc_nlinfo.nl_net;
489 unsigned index;
490 int err = -EINVAL;
491
492 index = cfg->rc_label;
493
494 /* The first 16 labels are reserved, and may not be removed */
495 if (index < 16)
496 goto errout;
497
498 /* The full 20 bit range may not be supported */
499 if (index >= net->mpls.platform_labels)
500 goto errout;
501
502 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
503
504 err = 0;
505errout:
506 return err;
507}
508
37bde799
RS
509#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
510 (&((struct mpls_dev *)0)->field)
511
512static const struct ctl_table mpls_dev_table[] = {
513 {
514 .procname = "input",
515 .maxlen = sizeof(int),
516 .mode = 0644,
517 .proc_handler = proc_dointvec,
518 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
519 },
520 { }
521};
522
523static int mpls_dev_sysctl_register(struct net_device *dev,
524 struct mpls_dev *mdev)
525{
526 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
527 struct ctl_table *table;
528 int i;
529
530 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
531 if (!table)
532 goto out;
533
534 /* Table data contains only offsets relative to the base of
535 * the mdev at this point, so make them absolute.
536 */
537 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
538 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
539
540 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
541
542 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
543 if (!mdev->sysctl)
544 goto free;
545
546 return 0;
547
548free:
549 kfree(table);
550out:
551 return -ENOBUFS;
552}
553
554static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
555{
556 struct ctl_table *table;
557
558 table = mdev->sysctl->ctl_table_arg;
559 unregister_net_sysctl_table(mdev->sysctl);
560 kfree(table);
561}
562
03c57747
RS
563static struct mpls_dev *mpls_add_dev(struct net_device *dev)
564{
565 struct mpls_dev *mdev;
566 int err = -ENOMEM;
567
568 ASSERT_RTNL();
569
570 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
571 if (!mdev)
572 return ERR_PTR(err);
573
37bde799
RS
574 err = mpls_dev_sysctl_register(dev, mdev);
575 if (err)
576 goto free;
577
03c57747
RS
578 rcu_assign_pointer(dev->mpls_ptr, mdev);
579
580 return mdev;
37bde799
RS
581
582free:
583 kfree(mdev);
584 return ERR_PTR(err);
03c57747
RS
585}
586
0189197f
EB
587static void mpls_ifdown(struct net_device *dev)
588{
19d0c341 589 struct mpls_route __rcu **platform_label;
0189197f 590 struct net *net = dev_net(dev);
03c57747 591 struct mpls_dev *mdev;
0189197f
EB
592 unsigned index;
593
19d0c341 594 platform_label = rtnl_dereference(net->mpls.platform_label);
0189197f 595 for (index = 0; index < net->mpls.platform_labels; index++) {
19d0c341 596 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
0189197f
EB
597 if (!rt)
598 continue;
19d0c341 599 if (rtnl_dereference(rt->rt_dev) != dev)
0189197f
EB
600 continue;
601 rt->rt_dev = NULL;
602 }
03c57747
RS
603
604 mdev = mpls_dev_get(dev);
605 if (!mdev)
606 return;
607
37bde799
RS
608 mpls_dev_sysctl_unregister(mdev);
609
03c57747
RS
610 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
611
25cc8f07 612 kfree_rcu(mdev, rcu);
0189197f
EB
613}
614
615static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
616 void *ptr)
617{
618 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
03c57747 619 struct mpls_dev *mdev;
0189197f
EB
620
621 switch(event) {
03c57747
RS
622 case NETDEV_REGISTER:
623 /* For now just support ethernet devices */
624 if ((dev->type == ARPHRD_ETHER) ||
625 (dev->type == ARPHRD_LOOPBACK)) {
626 mdev = mpls_add_dev(dev);
627 if (IS_ERR(mdev))
628 return notifier_from_errno(PTR_ERR(mdev));
629 }
630 break;
631
0189197f
EB
632 case NETDEV_UNREGISTER:
633 mpls_ifdown(dev);
634 break;
0fae3bf0
RS
635 case NETDEV_CHANGENAME:
636 mdev = mpls_dev_get(dev);
637 if (mdev) {
638 int err;
639
640 mpls_dev_sysctl_unregister(mdev);
641 err = mpls_dev_sysctl_register(dev, mdev);
642 if (err)
643 return notifier_from_errno(err);
644 }
645 break;
0189197f
EB
646 }
647 return NOTIFY_OK;
648}
649
650static struct notifier_block mpls_dev_notifier = {
651 .notifier_call = mpls_dev_notify,
652};
653
03c05665 654static int nla_put_via(struct sk_buff *skb,
b79bda3d 655 u8 table, const void *addr, int alen)
03c05665 656{
b79bda3d
EB
657 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
658 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
659 };
03c05665
EB
660 struct nlattr *nla;
661 struct rtvia *via;
b79bda3d 662 int family = AF_UNSPEC;
03c05665
EB
663
664 nla = nla_reserve(skb, RTA_VIA, alen + 2);
665 if (!nla)
666 return -EMSGSIZE;
667
b79bda3d
EB
668 if (table <= NEIGH_NR_TABLES)
669 family = table_to_family[table];
670
03c05665
EB
671 via = nla_data(nla);
672 via->rtvia_family = family;
673 memcpy(via->rtvia_addr, addr, alen);
674 return 0;
675}
676
966bae33
EB
677int nla_put_labels(struct sk_buff *skb, int attrtype,
678 u8 labels, const u32 label[])
679{
680 struct nlattr *nla;
681 struct mpls_shim_hdr *nla_label;
682 bool bos;
683 int i;
684 nla = nla_reserve(skb, attrtype, labels*4);
685 if (!nla)
686 return -EMSGSIZE;
687
688 nla_label = nla_data(nla);
689 bos = true;
690 for (i = labels - 1; i >= 0; i--) {
691 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
692 bos = false;
693 }
694
695 return 0;
696}
face0188 697EXPORT_SYMBOL_GPL(nla_put_labels);
966bae33
EB
698
699int nla_get_labels(const struct nlattr *nla,
700 u32 max_labels, u32 *labels, u32 label[])
701{
702 unsigned len = nla_len(nla);
703 unsigned nla_labels;
704 struct mpls_shim_hdr *nla_label;
705 bool bos;
706 int i;
707
708 /* len needs to be an even multiple of 4 (the label size) */
709 if (len & 3)
710 return -EINVAL;
711
712 /* Limit the number of new labels allowed */
713 nla_labels = len/4;
714 if (nla_labels > max_labels)
715 return -EINVAL;
716
717 nla_label = nla_data(nla);
718 bos = true;
719 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
720 struct mpls_entry_decoded dec;
721 dec = mpls_entry_decode(nla_label + i);
722
723 /* Ensure the bottom of stack flag is properly set
724 * and ttl and tc are both clear.
725 */
726 if ((dec.bos != bos) || dec.ttl || dec.tc)
727 return -EINVAL;
728
5a9ab017 729 switch (dec.label) {
78f5b899 730 case MPLS_LABEL_IMPLNULL:
5a9ab017
RS
731 /* RFC3032: This is a label that an LSR may
732 * assign and distribute, but which never
733 * actually appears in the encapsulation.
734 */
735 return -EINVAL;
736 }
737
966bae33
EB
738 label[i] = dec.label;
739 }
740 *labels = nla_labels;
741 return 0;
742}
face0188 743EXPORT_SYMBOL_GPL(nla_get_labels);
966bae33 744
03c05665
EB
745static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
746 struct mpls_route_config *cfg)
747{
748 struct rtmsg *rtm;
749 struct nlattr *tb[RTA_MAX+1];
750 int index;
751 int err;
752
753 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
754 if (err < 0)
755 goto errout;
756
757 err = -EINVAL;
758 rtm = nlmsg_data(nlh);
759 memset(cfg, 0, sizeof(*cfg));
760
761 if (rtm->rtm_family != AF_MPLS)
762 goto errout;
763 if (rtm->rtm_dst_len != 20)
764 goto errout;
765 if (rtm->rtm_src_len != 0)
766 goto errout;
767 if (rtm->rtm_tos != 0)
768 goto errout;
769 if (rtm->rtm_table != RT_TABLE_MAIN)
770 goto errout;
771 /* Any value is acceptable for rtm_protocol */
772
773 /* As mpls uses destination specific addresses
774 * (or source specific address in the case of multicast)
775 * all addresses have universal scope.
776 */
777 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
778 goto errout;
779 if (rtm->rtm_type != RTN_UNICAST)
780 goto errout;
781 if (rtm->rtm_flags != 0)
782 goto errout;
783
784 cfg->rc_label = LABEL_NOT_SPECIFIED;
785 cfg->rc_protocol = rtm->rtm_protocol;
786 cfg->rc_nlflags = nlh->nlmsg_flags;
787 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
788 cfg->rc_nlinfo.nlh = nlh;
789 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
790
791 for (index = 0; index <= RTA_MAX; index++) {
792 struct nlattr *nla = tb[index];
793 if (!nla)
794 continue;
795
796 switch(index) {
797 case RTA_OIF:
798 cfg->rc_ifindex = nla_get_u32(nla);
799 break;
800 case RTA_NEWDST:
801 if (nla_get_labels(nla, MAX_NEW_LABELS,
802 &cfg->rc_output_labels,
803 cfg->rc_output_label))
804 goto errout;
805 break;
806 case RTA_DST:
807 {
808 u32 label_count;
809 if (nla_get_labels(nla, 1, &label_count,
810 &cfg->rc_label))
811 goto errout;
812
813 /* The first 16 labels are reserved, and may not be set */
814 if (cfg->rc_label < 16)
815 goto errout;
816
817 break;
818 }
819 case RTA_VIA:
820 {
821 struct rtvia *via = nla_data(nla);
f8d54afc
RS
822 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
823 goto errout;
f8d54afc
RS
824 cfg->rc_via_alen = nla_len(nla) -
825 offsetof(struct rtvia, rtvia_addr);
03c05665
EB
826 if (cfg->rc_via_alen > MAX_VIA_ALEN)
827 goto errout;
828
829 /* Validate the address family */
b79bda3d 830 switch(via->rtvia_family) {
03c05665 831 case AF_PACKET:
b79bda3d 832 cfg->rc_via_table = NEIGH_LINK_TABLE;
03c05665
EB
833 break;
834 case AF_INET:
b79bda3d 835 cfg->rc_via_table = NEIGH_ARP_TABLE;
03c05665
EB
836 if (cfg->rc_via_alen != 4)
837 goto errout;
838 break;
839 case AF_INET6:
b79bda3d 840 cfg->rc_via_table = NEIGH_ND_TABLE;
03c05665
EB
841 if (cfg->rc_via_alen != 16)
842 goto errout;
843 break;
844 default:
845 /* Unsupported address family */
846 goto errout;
847 }
848
849 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
850 break;
851 }
852 default:
853 /* Unsupported attribute */
854 goto errout;
855 }
856 }
857
858 err = 0;
859errout:
860 return err;
861}
862
863static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
864{
865 struct mpls_route_config cfg;
866 int err;
867
868 err = rtm_to_route_config(skb, nlh, &cfg);
869 if (err < 0)
870 return err;
871
872 return mpls_route_del(&cfg);
873}
874
875
876static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
877{
878 struct mpls_route_config cfg;
879 int err;
880
881 err = rtm_to_route_config(skb, nlh, &cfg);
882 if (err < 0)
883 return err;
884
885 return mpls_route_add(&cfg);
886}
887
888static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
889 u32 label, struct mpls_route *rt, int flags)
890{
19d0c341 891 struct net_device *dev;
03c05665
EB
892 struct nlmsghdr *nlh;
893 struct rtmsg *rtm;
894
895 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
896 if (nlh == NULL)
897 return -EMSGSIZE;
898
899 rtm = nlmsg_data(nlh);
900 rtm->rtm_family = AF_MPLS;
901 rtm->rtm_dst_len = 20;
902 rtm->rtm_src_len = 0;
903 rtm->rtm_tos = 0;
904 rtm->rtm_table = RT_TABLE_MAIN;
905 rtm->rtm_protocol = rt->rt_protocol;
906 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
907 rtm->rtm_type = RTN_UNICAST;
908 rtm->rtm_flags = 0;
909
910 if (rt->rt_labels &&
911 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
912 goto nla_put_failure;
b79bda3d 913 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
03c05665 914 goto nla_put_failure;
19d0c341
EB
915 dev = rtnl_dereference(rt->rt_dev);
916 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
03c05665
EB
917 goto nla_put_failure;
918 if (nla_put_labels(skb, RTA_DST, 1, &label))
919 goto nla_put_failure;
920
921 nlmsg_end(skb, nlh);
922 return 0;
923
924nla_put_failure:
925 nlmsg_cancel(skb, nlh);
926 return -EMSGSIZE;
927}
928
929static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
930{
931 struct net *net = sock_net(skb->sk);
19d0c341
EB
932 struct mpls_route __rcu **platform_label;
933 size_t platform_labels;
03c05665
EB
934 unsigned int index;
935
936 ASSERT_RTNL();
937
938 index = cb->args[0];
939 if (index < 16)
940 index = 16;
941
19d0c341
EB
942 platform_label = rtnl_dereference(net->mpls.platform_label);
943 platform_labels = net->mpls.platform_labels;
944 for (; index < platform_labels; index++) {
03c05665 945 struct mpls_route *rt;
19d0c341 946 rt = rtnl_dereference(platform_label[index]);
03c05665
EB
947 if (!rt)
948 continue;
949
950 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
951 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
952 index, rt, NLM_F_MULTI) < 0)
953 break;
954 }
955 cb->args[0] = index;
956
957 return skb->len;
958}
959
8de147dc
EB
960static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
961{
962 size_t payload =
963 NLMSG_ALIGN(sizeof(struct rtmsg))
964 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
965 + nla_total_size(4); /* RTA_DST */
966 if (rt->rt_labels) /* RTA_NEWDST */
967 payload += nla_total_size(rt->rt_labels * 4);
968 if (rt->rt_dev) /* RTA_OIF */
969 payload += nla_total_size(4);
970 return payload;
971}
972
973static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
974 struct nlmsghdr *nlh, struct net *net, u32 portid,
975 unsigned int nlm_flags)
976{
977 struct sk_buff *skb;
978 u32 seq = nlh ? nlh->nlmsg_seq : 0;
979 int err = -ENOBUFS;
980
981 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
982 if (skb == NULL)
983 goto errout;
984
985 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
986 if (err < 0) {
987 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
988 WARN_ON(err == -EMSGSIZE);
989 kfree_skb(skb);
990 goto errout;
991 }
992 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
993
994 return;
995errout:
996 if (err < 0)
997 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
998}
999
7720c01f
EB
1000static int resize_platform_label_table(struct net *net, size_t limit)
1001{
1002 size_t size = sizeof(struct mpls_route *) * limit;
1003 size_t old_limit;
1004 size_t cp_size;
1005 struct mpls_route __rcu **labels = NULL, **old;
1006 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1007 unsigned index;
1008
1009 if (size) {
1010 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1011 if (!labels)
1012 labels = vzalloc(size);
1013
1014 if (!labels)
1015 goto nolabels;
1016 }
1017
1018 /* In case the predefined labels need to be populated */
78f5b899 1019 if (limit > MPLS_LABEL_IPV4NULL) {
7720c01f
EB
1020 struct net_device *lo = net->loopback_dev;
1021 rt0 = mpls_rt_alloc(lo->addr_len);
1022 if (!rt0)
1023 goto nort0;
19d0c341 1024 RCU_INIT_POINTER(rt0->rt_dev, lo);
7720c01f 1025 rt0->rt_protocol = RTPROT_KERNEL;
b79bda3d 1026 rt0->rt_via_table = NEIGH_LINK_TABLE;
7720c01f
EB
1027 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1028 }
78f5b899 1029 if (limit > MPLS_LABEL_IPV6NULL) {
7720c01f
EB
1030 struct net_device *lo = net->loopback_dev;
1031 rt2 = mpls_rt_alloc(lo->addr_len);
1032 if (!rt2)
1033 goto nort2;
19d0c341 1034 RCU_INIT_POINTER(rt2->rt_dev, lo);
7720c01f 1035 rt2->rt_protocol = RTPROT_KERNEL;
b79bda3d 1036 rt2->rt_via_table = NEIGH_LINK_TABLE;
7720c01f
EB
1037 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1038 }
1039
1040 rtnl_lock();
1041 /* Remember the original table */
19d0c341 1042 old = rtnl_dereference(net->mpls.platform_label);
7720c01f
EB
1043 old_limit = net->mpls.platform_labels;
1044
1045 /* Free any labels beyond the new table */
1046 for (index = limit; index < old_limit; index++)
1047 mpls_route_update(net, index, NULL, NULL, NULL);
1048
1049 /* Copy over the old labels */
1050 cp_size = size;
1051 if (old_limit < limit)
1052 cp_size = old_limit * sizeof(struct mpls_route *);
1053
1054 memcpy(labels, old, cp_size);
1055
1056 /* If needed set the predefined labels */
78f5b899
TH
1057 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1058 (limit > MPLS_LABEL_IPV6NULL)) {
1059 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
7720c01f
EB
1060 rt2 = NULL;
1061 }
1062
78f5b899
TH
1063 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1064 (limit > MPLS_LABEL_IPV4NULL)) {
1065 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
7720c01f
EB
1066 rt0 = NULL;
1067 }
1068
1069 /* Update the global pointers */
1070 net->mpls.platform_labels = limit;
19d0c341 1071 rcu_assign_pointer(net->mpls.platform_label, labels);
7720c01f
EB
1072
1073 rtnl_unlock();
1074
1075 mpls_rt_free(rt2);
1076 mpls_rt_free(rt0);
1077
1078 if (old) {
1079 synchronize_rcu();
1080 kvfree(old);
1081 }
1082 return 0;
1083
1084nort2:
1085 mpls_rt_free(rt0);
1086nort0:
1087 kvfree(labels);
1088nolabels:
1089 return -ENOMEM;
1090}
1091
1092static int mpls_platform_labels(struct ctl_table *table, int write,
1093 void __user *buffer, size_t *lenp, loff_t *ppos)
1094{
1095 struct net *net = table->data;
1096 int platform_labels = net->mpls.platform_labels;
1097 int ret;
1098 struct ctl_table tmp = {
1099 .procname = table->procname,
1100 .data = &platform_labels,
1101 .maxlen = sizeof(int),
1102 .mode = table->mode,
1103 .extra1 = &zero,
1104 .extra2 = &label_limit,
1105 };
1106
1107 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1108
1109 if (write && ret == 0)
1110 ret = resize_platform_label_table(net, platform_labels);
1111
1112 return ret;
1113}
1114
37bde799 1115static const struct ctl_table mpls_table[] = {
7720c01f
EB
1116 {
1117 .procname = "platform_labels",
1118 .data = NULL,
1119 .maxlen = sizeof(int),
1120 .mode = 0644,
1121 .proc_handler = mpls_platform_labels,
1122 },
1123 { }
1124};
1125
0189197f
EB
1126static int mpls_net_init(struct net *net)
1127{
7720c01f
EB
1128 struct ctl_table *table;
1129
0189197f
EB
1130 net->mpls.platform_labels = 0;
1131 net->mpls.platform_label = NULL;
1132
7720c01f
EB
1133 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1134 if (table == NULL)
1135 return -ENOMEM;
1136
1137 table[0].data = net;
1138 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1139 if (net->mpls.ctl == NULL)
1140 return -ENOMEM;
1141
0189197f
EB
1142 return 0;
1143}
1144
1145static void mpls_net_exit(struct net *net)
1146{
19d0c341
EB
1147 struct mpls_route __rcu **platform_label;
1148 size_t platform_labels;
7720c01f 1149 struct ctl_table *table;
0189197f
EB
1150 unsigned int index;
1151
7720c01f
EB
1152 table = net->mpls.ctl->ctl_table_arg;
1153 unregister_net_sysctl_table(net->mpls.ctl);
1154 kfree(table);
1155
19d0c341
EB
1156 /* An rcu grace period has passed since there was a device in
1157 * the network namespace (and thus the last in flight packet)
0189197f
EB
1158 * left this network namespace. This is because
1159 * unregister_netdevice_many and netdev_run_todo has completed
1160 * for each network device that was in this network namespace.
1161 *
1162 * As such no additional rcu synchronization is necessary when
1163 * freeing the platform_label table.
1164 */
1165 rtnl_lock();
19d0c341
EB
1166 platform_label = rtnl_dereference(net->mpls.platform_label);
1167 platform_labels = net->mpls.platform_labels;
1168 for (index = 0; index < platform_labels; index++) {
1169 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1170 RCU_INIT_POINTER(platform_label[index], NULL);
0189197f
EB
1171 mpls_rt_free(rt);
1172 }
1173 rtnl_unlock();
1174
19d0c341 1175 kvfree(platform_label);
0189197f
EB
1176}
1177
1178static struct pernet_operations mpls_net_ops = {
1179 .init = mpls_net_init,
1180 .exit = mpls_net_exit,
1181};
1182
1183static int __init mpls_init(void)
1184{
1185 int err;
1186
1187 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1188
1189 err = register_pernet_subsys(&mpls_net_ops);
1190 if (err)
1191 goto out;
1192
1193 err = register_netdevice_notifier(&mpls_dev_notifier);
1194 if (err)
1195 goto out_unregister_pernet;
1196
1197 dev_add_pack(&mpls_packet_type);
1198
03c05665
EB
1199 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1200 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1201 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
0189197f
EB
1202 err = 0;
1203out:
1204 return err;
1205
1206out_unregister_pernet:
1207 unregister_pernet_subsys(&mpls_net_ops);
1208 goto out;
1209}
1210module_init(mpls_init);
1211
1212static void __exit mpls_exit(void)
1213{
03c05665 1214 rtnl_unregister_all(PF_MPLS);
0189197f
EB
1215 dev_remove_pack(&mpls_packet_type);
1216 unregister_netdevice_notifier(&mpls_dev_notifier);
1217 unregister_pernet_subsys(&mpls_net_ops);
1218}
1219module_exit(mpls_exit);
1220
1221MODULE_DESCRIPTION("MultiProtocol Label Switching");
1222MODULE_LICENSE("GPL v2");
1223MODULE_ALIAS_NETPROTO(PF_MPLS);