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