blob: 23e51d13b0ff34a25218ba2016667a86d59ed6ce [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#include <linux/types.h>
2#include <linux/skbuff.h>
3#include <linux/socket.h>
Eric W. Biederman7720c012015-03-03 19:11:20 -06004#include <linux/sysctl.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06005#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>
10#include <net/ip.h>
11#include <net/dst.h>
12#include <net/sock.h>
13#include <net/arp.h>
14#include <net/ip_fib.h>
15#include <net/netevent.h>
16#include <net/netns/generic.h>
17#include "internal.h"
18
Eric W. Biedermana2519922015-03-03 19:12:40 -060019#define LABEL_NOT_SPECIFIED (1<<20)
Eric W. Biederman01891972015-03-03 19:10:47 -060020#define MAX_NEW_LABELS 2
21
22/* This maximum ha length copied from the definition of struct neighbour */
23#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
24
25struct 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 */
30 u8 rt_labels:2,
31 rt_via_alen:6;
32 unsigned short rt_via_family;
33 u8 rt_via[0];
34};
35
Eric W. Biederman7720c012015-03-03 19:11:20 -060036static int zero = 0;
37static int label_limit = (1 << 20) - 1;
38
Eric W. Biederman8de147d2015-03-03 19:14:31 -060039static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
40 struct nlmsghdr *nlh, struct net *net, u32 portid,
41 unsigned int nlm_flags);
42
Eric W. Biederman01891972015-03-03 19:10:47 -060043static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
44{
45 struct mpls_route *rt = NULL;
46
47 if (index < net->mpls.platform_labels) {
48 struct mpls_route __rcu **platform_label =
49 rcu_dereference(net->mpls.platform_label);
50 rt = rcu_dereference(platform_label[index]);
51 }
52 return rt;
53}
54
55static bool mpls_output_possible(const struct net_device *dev)
56{
57 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
58}
59
60static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
61{
62 /* The size of the layer 2.5 labels to be added for this route */
63 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
64}
65
66static unsigned int mpls_dev_mtu(const struct net_device *dev)
67{
68 /* The amount of data the layer 2 frame can hold */
69 return dev->mtu;
70}
71
72static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
73{
74 if (skb->len <= mtu)
75 return false;
76
77 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
78 return false;
79
80 return true;
81}
82
83static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
84 struct mpls_entry_decoded dec)
85{
86 /* RFC4385 and RFC5586 encode other packets in mpls such that
87 * they don't conflict with the ip version number, making
88 * decoding by examining the ip version correct in everything
89 * except for the strangest cases.
90 *
91 * The strange cases if we choose to support them will require
92 * manual configuration.
93 */
94 struct iphdr *hdr4 = ip_hdr(skb);
95 bool success = true;
96
97 if (hdr4->version == 4) {
98 skb->protocol = htons(ETH_P_IP);
99 csum_replace2(&hdr4->check,
100 htons(hdr4->ttl << 8),
101 htons(dec.ttl << 8));
102 hdr4->ttl = dec.ttl;
103 }
104 else if (hdr4->version == 6) {
105 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
106 skb->protocol = htons(ETH_P_IPV6);
107 hdr6->hop_limit = dec.ttl;
108 }
109 else
110 /* version 0 and version 1 are used by pseudo wires */
111 success = false;
112 return success;
113}
114
115static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
116 struct packet_type *pt, struct net_device *orig_dev)
117{
118 struct net *net = dev_net(dev);
119 struct mpls_shim_hdr *hdr;
120 struct mpls_route *rt;
121 struct mpls_entry_decoded dec;
122 struct net_device *out_dev;
123 unsigned int hh_len;
124 unsigned int new_header_size;
125 unsigned int mtu;
126 int err;
127
128 /* Careful this entire function runs inside of an rcu critical section */
129
130 if (skb->pkt_type != PACKET_HOST)
131 goto drop;
132
133 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
134 goto drop;
135
136 if (!pskb_may_pull(skb, sizeof(*hdr)))
137 goto drop;
138
139 /* Read and decode the label */
140 hdr = mpls_hdr(skb);
141 dec = mpls_entry_decode(hdr);
142
143 /* Pop the label */
144 skb_pull(skb, sizeof(*hdr));
145 skb_reset_network_header(skb);
146
147 skb_orphan(skb);
148
149 rt = mpls_route_input_rcu(net, dec.label);
150 if (!rt)
151 goto drop;
152
153 /* Find the output device */
154 out_dev = rt->rt_dev;
155 if (!mpls_output_possible(out_dev))
156 goto drop;
157
158 if (skb_warn_if_lro(skb))
159 goto drop;
160
161 skb_forward_csum(skb);
162
163 /* Verify ttl is valid */
164 if (dec.ttl <= 2)
165 goto drop;
166 dec.ttl -= 1;
167
168 /* Verify the destination can hold the packet */
169 new_header_size = mpls_rt_header_size(rt);
170 mtu = mpls_dev_mtu(out_dev);
171 if (mpls_pkt_too_big(skb, mtu - new_header_size))
172 goto drop;
173
174 hh_len = LL_RESERVED_SPACE(out_dev);
175 if (!out_dev->header_ops)
176 hh_len = 0;
177
178 /* Ensure there is enough space for the headers in the skb */
179 if (skb_cow(skb, hh_len + new_header_size))
180 goto drop;
181
182 skb->dev = out_dev;
183 skb->protocol = htons(ETH_P_MPLS_UC);
184
185 if (unlikely(!new_header_size && dec.bos)) {
186 /* Penultimate hop popping */
187 if (!mpls_egress(rt, skb, dec))
188 goto drop;
189 } else {
190 bool bos;
191 int i;
192 skb_push(skb, new_header_size);
193 skb_reset_network_header(skb);
194 /* Push the new labels */
195 hdr = mpls_hdr(skb);
196 bos = dec.bos;
197 for (i = rt->rt_labels - 1; i >= 0; i--) {
198 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
199 bos = false;
200 }
201 }
202
203 err = neigh_xmit(rt->rt_via_family, out_dev, rt->rt_via, skb);
204 if (err)
205 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
206 __func__, err);
207 return 0;
208
209drop:
210 kfree_skb(skb);
211 return NET_RX_DROP;
212}
213
214static struct packet_type mpls_packet_type __read_mostly = {
215 .type = cpu_to_be16(ETH_P_MPLS_UC),
216 .func = mpls_forward,
217};
218
Wu Fengguangf0126532015-03-05 05:33:54 +0800219static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600220 [RTA_DST] = { .type = NLA_U32 },
221 [RTA_OIF] = { .type = NLA_U32 },
222};
223
Eric W. Biedermana2519922015-03-03 19:12:40 -0600224struct mpls_route_config {
225 u32 rc_protocol;
226 u32 rc_ifindex;
227 u16 rc_via_family;
228 u16 rc_via_alen;
229 u8 rc_via[MAX_VIA_ALEN];
230 u32 rc_label;
231 u32 rc_output_labels;
232 u32 rc_output_label[MAX_NEW_LABELS];
233 u32 rc_nlflags;
234 struct nl_info rc_nlinfo;
235};
236
Eric W. Biederman01891972015-03-03 19:10:47 -0600237static struct mpls_route *mpls_rt_alloc(size_t alen)
238{
239 struct mpls_route *rt;
240
241 rt = kzalloc(GFP_KERNEL, sizeof(*rt) + alen);
242 if (rt)
243 rt->rt_via_alen = alen;
244 return rt;
245}
246
247static void mpls_rt_free(struct mpls_route *rt)
248{
249 if (rt)
250 kfree_rcu(rt, rt_rcu);
251}
252
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600253static void mpls_notify_route(struct net *net, unsigned index,
254 struct mpls_route *old, struct mpls_route *new,
255 const struct nl_info *info)
256{
257 struct nlmsghdr *nlh = info ? info->nlh : NULL;
258 unsigned portid = info ? info->portid : 0;
259 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
260 struct mpls_route *rt = new ? new : old;
261 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
262 /* Ignore reserved labels for now */
263 if (rt && (index >= 16))
264 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
265}
266
Eric W. Biederman01891972015-03-03 19:10:47 -0600267static void mpls_route_update(struct net *net, unsigned index,
268 struct net_device *dev, struct mpls_route *new,
269 const struct nl_info *info)
270{
271 struct mpls_route *rt, *old = NULL;
272
273 ASSERT_RTNL();
274
275 rt = net->mpls.platform_label[index];
276 if (!dev || (rt && (rt->rt_dev == dev))) {
277 rcu_assign_pointer(net->mpls.platform_label[index], new);
278 old = rt;
279 }
280
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600281 mpls_notify_route(net, index, old, new, info);
282
Eric W. Biederman01891972015-03-03 19:10:47 -0600283 /* If we removed a route free it now */
284 mpls_rt_free(old);
285}
286
Eric W. Biedermana2519922015-03-03 19:12:40 -0600287static unsigned find_free_label(struct net *net)
288{
289 unsigned index;
290 for (index = 16; index < net->mpls.platform_labels; index++) {
291 if (!net->mpls.platform_label[index])
292 return index;
293 }
294 return LABEL_NOT_SPECIFIED;
295}
296
297static int mpls_route_add(struct mpls_route_config *cfg)
298{
299 struct net *net = cfg->rc_nlinfo.nl_net;
300 struct net_device *dev = NULL;
301 struct mpls_route *rt, *old;
302 unsigned index;
303 int i;
304 int err = -EINVAL;
305
306 index = cfg->rc_label;
307
308 /* If a label was not specified during insert pick one */
309 if ((index == LABEL_NOT_SPECIFIED) &&
310 (cfg->rc_nlflags & NLM_F_CREATE)) {
311 index = find_free_label(net);
312 }
313
314 /* The first 16 labels are reserved, and may not be set */
315 if (index < 16)
316 goto errout;
317
318 /* The full 20 bit range may not be supported. */
319 if (index >= net->mpls.platform_labels)
320 goto errout;
321
322 /* Ensure only a supported number of labels are present */
323 if (cfg->rc_output_labels > MAX_NEW_LABELS)
324 goto errout;
325
326 err = -ENODEV;
327 dev = dev_get_by_index(net, cfg->rc_ifindex);
328 if (!dev)
329 goto errout;
330
331 /* For now just support ethernet devices */
332 err = -EINVAL;
333 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
334 goto errout;
335
336 err = -EINVAL;
337 if ((cfg->rc_via_family == AF_PACKET) &&
338 (dev->addr_len != cfg->rc_via_alen))
339 goto errout;
340
341 /* Append makes no sense with mpls */
342 err = -EINVAL;
343 if (cfg->rc_nlflags & NLM_F_APPEND)
344 goto errout;
345
346 err = -EEXIST;
347 old = net->mpls.platform_label[index];
348 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
349 goto errout;
350
351 err = -EEXIST;
352 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
353 goto errout;
354
355 err = -ENOENT;
356 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
357 goto errout;
358
359 err = -ENOMEM;
360 rt = mpls_rt_alloc(cfg->rc_via_alen);
361 if (!rt)
362 goto errout;
363
364 rt->rt_labels = cfg->rc_output_labels;
365 for (i = 0; i < rt->rt_labels; i++)
366 rt->rt_label[i] = cfg->rc_output_label[i];
367 rt->rt_protocol = cfg->rc_protocol;
368 rt->rt_dev = dev;
369 rt->rt_via_family = cfg->rc_via_family;
370 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
371
372 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
373
374 dev_put(dev);
375 return 0;
376
377errout:
378 if (dev)
379 dev_put(dev);
380 return err;
381}
382
383static int mpls_route_del(struct mpls_route_config *cfg)
384{
385 struct net *net = cfg->rc_nlinfo.nl_net;
386 unsigned index;
387 int err = -EINVAL;
388
389 index = cfg->rc_label;
390
391 /* The first 16 labels are reserved, and may not be removed */
392 if (index < 16)
393 goto errout;
394
395 /* The full 20 bit range may not be supported */
396 if (index >= net->mpls.platform_labels)
397 goto errout;
398
399 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
400
401 err = 0;
402errout:
403 return err;
404}
405
Eric W. Biederman01891972015-03-03 19:10:47 -0600406static void mpls_ifdown(struct net_device *dev)
407{
408 struct net *net = dev_net(dev);
409 unsigned index;
410
411 for (index = 0; index < net->mpls.platform_labels; index++) {
412 struct mpls_route *rt = net->mpls.platform_label[index];
413 if (!rt)
414 continue;
415 if (rt->rt_dev != dev)
416 continue;
417 rt->rt_dev = NULL;
418 }
419}
420
421static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
422 void *ptr)
423{
424 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
425
426 switch(event) {
427 case NETDEV_UNREGISTER:
428 mpls_ifdown(dev);
429 break;
430 }
431 return NOTIFY_OK;
432}
433
434static struct notifier_block mpls_dev_notifier = {
435 .notifier_call = mpls_dev_notify,
436};
437
Eric W. Biederman03c05662015-03-03 19:13:56 -0600438static int nla_put_via(struct sk_buff *skb,
439 u16 family, const void *addr, int alen)
440{
441 struct nlattr *nla;
442 struct rtvia *via;
443
444 nla = nla_reserve(skb, RTA_VIA, alen + 2);
445 if (!nla)
446 return -EMSGSIZE;
447
448 via = nla_data(nla);
449 via->rtvia_family = family;
450 memcpy(via->rtvia_addr, addr, alen);
451 return 0;
452}
453
Eric W. Biederman966bae32015-03-03 19:13:19 -0600454int nla_put_labels(struct sk_buff *skb, int attrtype,
455 u8 labels, const u32 label[])
456{
457 struct nlattr *nla;
458 struct mpls_shim_hdr *nla_label;
459 bool bos;
460 int i;
461 nla = nla_reserve(skb, attrtype, labels*4);
462 if (!nla)
463 return -EMSGSIZE;
464
465 nla_label = nla_data(nla);
466 bos = true;
467 for (i = labels - 1; i >= 0; i--) {
468 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
469 bos = false;
470 }
471
472 return 0;
473}
474
475int nla_get_labels(const struct nlattr *nla,
476 u32 max_labels, u32 *labels, u32 label[])
477{
478 unsigned len = nla_len(nla);
479 unsigned nla_labels;
480 struct mpls_shim_hdr *nla_label;
481 bool bos;
482 int i;
483
484 /* len needs to be an even multiple of 4 (the label size) */
485 if (len & 3)
486 return -EINVAL;
487
488 /* Limit the number of new labels allowed */
489 nla_labels = len/4;
490 if (nla_labels > max_labels)
491 return -EINVAL;
492
493 nla_label = nla_data(nla);
494 bos = true;
495 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
496 struct mpls_entry_decoded dec;
497 dec = mpls_entry_decode(nla_label + i);
498
499 /* Ensure the bottom of stack flag is properly set
500 * and ttl and tc are both clear.
501 */
502 if ((dec.bos != bos) || dec.ttl || dec.tc)
503 return -EINVAL;
504
505 label[i] = dec.label;
506 }
507 *labels = nla_labels;
508 return 0;
509}
510
Eric W. Biederman03c05662015-03-03 19:13:56 -0600511static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
512 struct mpls_route_config *cfg)
513{
514 struct rtmsg *rtm;
515 struct nlattr *tb[RTA_MAX+1];
516 int index;
517 int err;
518
519 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
520 if (err < 0)
521 goto errout;
522
523 err = -EINVAL;
524 rtm = nlmsg_data(nlh);
525 memset(cfg, 0, sizeof(*cfg));
526
527 if (rtm->rtm_family != AF_MPLS)
528 goto errout;
529 if (rtm->rtm_dst_len != 20)
530 goto errout;
531 if (rtm->rtm_src_len != 0)
532 goto errout;
533 if (rtm->rtm_tos != 0)
534 goto errout;
535 if (rtm->rtm_table != RT_TABLE_MAIN)
536 goto errout;
537 /* Any value is acceptable for rtm_protocol */
538
539 /* As mpls uses destination specific addresses
540 * (or source specific address in the case of multicast)
541 * all addresses have universal scope.
542 */
543 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
544 goto errout;
545 if (rtm->rtm_type != RTN_UNICAST)
546 goto errout;
547 if (rtm->rtm_flags != 0)
548 goto errout;
549
550 cfg->rc_label = LABEL_NOT_SPECIFIED;
551 cfg->rc_protocol = rtm->rtm_protocol;
552 cfg->rc_nlflags = nlh->nlmsg_flags;
553 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
554 cfg->rc_nlinfo.nlh = nlh;
555 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
556
557 for (index = 0; index <= RTA_MAX; index++) {
558 struct nlattr *nla = tb[index];
559 if (!nla)
560 continue;
561
562 switch(index) {
563 case RTA_OIF:
564 cfg->rc_ifindex = nla_get_u32(nla);
565 break;
566 case RTA_NEWDST:
567 if (nla_get_labels(nla, MAX_NEW_LABELS,
568 &cfg->rc_output_labels,
569 cfg->rc_output_label))
570 goto errout;
571 break;
572 case RTA_DST:
573 {
574 u32 label_count;
575 if (nla_get_labels(nla, 1, &label_count,
576 &cfg->rc_label))
577 goto errout;
578
579 /* The first 16 labels are reserved, and may not be set */
580 if (cfg->rc_label < 16)
581 goto errout;
582
583 break;
584 }
585 case RTA_VIA:
586 {
587 struct rtvia *via = nla_data(nla);
588 cfg->rc_via_family = via->rtvia_family;
589 cfg->rc_via_alen = nla_len(nla) - 2;
590 if (cfg->rc_via_alen > MAX_VIA_ALEN)
591 goto errout;
592
593 /* Validate the address family */
594 switch(cfg->rc_via_family) {
595 case AF_PACKET:
596 break;
597 case AF_INET:
598 if (cfg->rc_via_alen != 4)
599 goto errout;
600 break;
601 case AF_INET6:
602 if (cfg->rc_via_alen != 16)
603 goto errout;
604 break;
605 default:
606 /* Unsupported address family */
607 goto errout;
608 }
609
610 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
611 break;
612 }
613 default:
614 /* Unsupported attribute */
615 goto errout;
616 }
617 }
618
619 err = 0;
620errout:
621 return err;
622}
623
624static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
625{
626 struct mpls_route_config cfg;
627 int err;
628
629 err = rtm_to_route_config(skb, nlh, &cfg);
630 if (err < 0)
631 return err;
632
633 return mpls_route_del(&cfg);
634}
635
636
637static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
638{
639 struct mpls_route_config cfg;
640 int err;
641
642 err = rtm_to_route_config(skb, nlh, &cfg);
643 if (err < 0)
644 return err;
645
646 return mpls_route_add(&cfg);
647}
648
649static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
650 u32 label, struct mpls_route *rt, int flags)
651{
652 struct nlmsghdr *nlh;
653 struct rtmsg *rtm;
654
655 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
656 if (nlh == NULL)
657 return -EMSGSIZE;
658
659 rtm = nlmsg_data(nlh);
660 rtm->rtm_family = AF_MPLS;
661 rtm->rtm_dst_len = 20;
662 rtm->rtm_src_len = 0;
663 rtm->rtm_tos = 0;
664 rtm->rtm_table = RT_TABLE_MAIN;
665 rtm->rtm_protocol = rt->rt_protocol;
666 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
667 rtm->rtm_type = RTN_UNICAST;
668 rtm->rtm_flags = 0;
669
670 if (rt->rt_labels &&
671 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
672 goto nla_put_failure;
673 if (nla_put_via(skb, rt->rt_via_family, rt->rt_via, rt->rt_via_alen))
674 goto nla_put_failure;
675 if (rt->rt_dev && nla_put_u32(skb, RTA_OIF, rt->rt_dev->ifindex))
676 goto nla_put_failure;
677 if (nla_put_labels(skb, RTA_DST, 1, &label))
678 goto nla_put_failure;
679
680 nlmsg_end(skb, nlh);
681 return 0;
682
683nla_put_failure:
684 nlmsg_cancel(skb, nlh);
685 return -EMSGSIZE;
686}
687
688static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
689{
690 struct net *net = sock_net(skb->sk);
691 unsigned int index;
692
693 ASSERT_RTNL();
694
695 index = cb->args[0];
696 if (index < 16)
697 index = 16;
698
699 for (; index < net->mpls.platform_labels; index++) {
700 struct mpls_route *rt;
701 rt = net->mpls.platform_label[index];
702 if (!rt)
703 continue;
704
705 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
706 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
707 index, rt, NLM_F_MULTI) < 0)
708 break;
709 }
710 cb->args[0] = index;
711
712 return skb->len;
713}
714
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600715static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
716{
717 size_t payload =
718 NLMSG_ALIGN(sizeof(struct rtmsg))
719 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
720 + nla_total_size(4); /* RTA_DST */
721 if (rt->rt_labels) /* RTA_NEWDST */
722 payload += nla_total_size(rt->rt_labels * 4);
723 if (rt->rt_dev) /* RTA_OIF */
724 payload += nla_total_size(4);
725 return payload;
726}
727
728static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
729 struct nlmsghdr *nlh, struct net *net, u32 portid,
730 unsigned int nlm_flags)
731{
732 struct sk_buff *skb;
733 u32 seq = nlh ? nlh->nlmsg_seq : 0;
734 int err = -ENOBUFS;
735
736 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
737 if (skb == NULL)
738 goto errout;
739
740 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
741 if (err < 0) {
742 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
743 WARN_ON(err == -EMSGSIZE);
744 kfree_skb(skb);
745 goto errout;
746 }
747 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
748
749 return;
750errout:
751 if (err < 0)
752 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
753}
754
Eric W. Biederman7720c012015-03-03 19:11:20 -0600755static int resize_platform_label_table(struct net *net, size_t limit)
756{
757 size_t size = sizeof(struct mpls_route *) * limit;
758 size_t old_limit;
759 size_t cp_size;
760 struct mpls_route __rcu **labels = NULL, **old;
761 struct mpls_route *rt0 = NULL, *rt2 = NULL;
762 unsigned index;
763
764 if (size) {
765 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
766 if (!labels)
767 labels = vzalloc(size);
768
769 if (!labels)
770 goto nolabels;
771 }
772
773 /* In case the predefined labels need to be populated */
774 if (limit > LABEL_IPV4_EXPLICIT_NULL) {
775 struct net_device *lo = net->loopback_dev;
776 rt0 = mpls_rt_alloc(lo->addr_len);
777 if (!rt0)
778 goto nort0;
779 rt0->rt_dev = lo;
780 rt0->rt_protocol = RTPROT_KERNEL;
781 rt0->rt_via_family = AF_PACKET;
782 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
783 }
784 if (limit > LABEL_IPV6_EXPLICIT_NULL) {
785 struct net_device *lo = net->loopback_dev;
786 rt2 = mpls_rt_alloc(lo->addr_len);
787 if (!rt2)
788 goto nort2;
789 rt2->rt_dev = lo;
790 rt2->rt_protocol = RTPROT_KERNEL;
791 rt2->rt_via_family = AF_PACKET;
792 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
793 }
794
795 rtnl_lock();
796 /* Remember the original table */
797 old = net->mpls.platform_label;
798 old_limit = net->mpls.platform_labels;
799
800 /* Free any labels beyond the new table */
801 for (index = limit; index < old_limit; index++)
802 mpls_route_update(net, index, NULL, NULL, NULL);
803
804 /* Copy over the old labels */
805 cp_size = size;
806 if (old_limit < limit)
807 cp_size = old_limit * sizeof(struct mpls_route *);
808
809 memcpy(labels, old, cp_size);
810
811 /* If needed set the predefined labels */
812 if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) &&
813 (limit > LABEL_IPV6_EXPLICIT_NULL)) {
814 labels[LABEL_IPV6_EXPLICIT_NULL] = rt2;
815 rt2 = NULL;
816 }
817
818 if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) &&
819 (limit > LABEL_IPV4_EXPLICIT_NULL)) {
820 labels[LABEL_IPV4_EXPLICIT_NULL] = rt0;
821 rt0 = NULL;
822 }
823
824 /* Update the global pointers */
825 net->mpls.platform_labels = limit;
826 net->mpls.platform_label = labels;
827
828 rtnl_unlock();
829
830 mpls_rt_free(rt2);
831 mpls_rt_free(rt0);
832
833 if (old) {
834 synchronize_rcu();
835 kvfree(old);
836 }
837 return 0;
838
839nort2:
840 mpls_rt_free(rt0);
841nort0:
842 kvfree(labels);
843nolabels:
844 return -ENOMEM;
845}
846
847static int mpls_platform_labels(struct ctl_table *table, int write,
848 void __user *buffer, size_t *lenp, loff_t *ppos)
849{
850 struct net *net = table->data;
851 int platform_labels = net->mpls.platform_labels;
852 int ret;
853 struct ctl_table tmp = {
854 .procname = table->procname,
855 .data = &platform_labels,
856 .maxlen = sizeof(int),
857 .mode = table->mode,
858 .extra1 = &zero,
859 .extra2 = &label_limit,
860 };
861
862 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
863
864 if (write && ret == 0)
865 ret = resize_platform_label_table(net, platform_labels);
866
867 return ret;
868}
869
870static struct ctl_table mpls_table[] = {
871 {
872 .procname = "platform_labels",
873 .data = NULL,
874 .maxlen = sizeof(int),
875 .mode = 0644,
876 .proc_handler = mpls_platform_labels,
877 },
878 { }
879};
880
Eric W. Biederman01891972015-03-03 19:10:47 -0600881static int mpls_net_init(struct net *net)
882{
Eric W. Biederman7720c012015-03-03 19:11:20 -0600883 struct ctl_table *table;
884
Eric W. Biederman01891972015-03-03 19:10:47 -0600885 net->mpls.platform_labels = 0;
886 net->mpls.platform_label = NULL;
887
Eric W. Biederman7720c012015-03-03 19:11:20 -0600888 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
889 if (table == NULL)
890 return -ENOMEM;
891
892 table[0].data = net;
893 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
894 if (net->mpls.ctl == NULL)
895 return -ENOMEM;
896
Eric W. Biederman01891972015-03-03 19:10:47 -0600897 return 0;
898}
899
900static void mpls_net_exit(struct net *net)
901{
Eric W. Biederman7720c012015-03-03 19:11:20 -0600902 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -0600903 unsigned int index;
904
Eric W. Biederman7720c012015-03-03 19:11:20 -0600905 table = net->mpls.ctl->ctl_table_arg;
906 unregister_net_sysctl_table(net->mpls.ctl);
907 kfree(table);
908
Eric W. Biederman01891972015-03-03 19:10:47 -0600909 /* An rcu grace period haselapsed since there was a device in
910 * the network namespace (and thus the last in fqlight packet)
911 * left this network namespace. This is because
912 * unregister_netdevice_many and netdev_run_todo has completed
913 * for each network device that was in this network namespace.
914 *
915 * As such no additional rcu synchronization is necessary when
916 * freeing the platform_label table.
917 */
918 rtnl_lock();
919 for (index = 0; index < net->mpls.platform_labels; index++) {
920 struct mpls_route *rt = net->mpls.platform_label[index];
921 rcu_assign_pointer(net->mpls.platform_label[index], NULL);
922 mpls_rt_free(rt);
923 }
924 rtnl_unlock();
925
926 kvfree(net->mpls.platform_label);
927}
928
929static struct pernet_operations mpls_net_ops = {
930 .init = mpls_net_init,
931 .exit = mpls_net_exit,
932};
933
934static int __init mpls_init(void)
935{
936 int err;
937
938 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
939
940 err = register_pernet_subsys(&mpls_net_ops);
941 if (err)
942 goto out;
943
944 err = register_netdevice_notifier(&mpls_dev_notifier);
945 if (err)
946 goto out_unregister_pernet;
947
948 dev_add_pack(&mpls_packet_type);
949
Eric W. Biederman03c05662015-03-03 19:13:56 -0600950 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
951 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
952 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600953 err = 0;
954out:
955 return err;
956
957out_unregister_pernet:
958 unregister_pernet_subsys(&mpls_net_ops);
959 goto out;
960}
961module_init(mpls_init);
962
963static void __exit mpls_exit(void)
964{
Eric W. Biederman03c05662015-03-03 19:13:56 -0600965 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -0600966 dev_remove_pack(&mpls_packet_type);
967 unregister_netdevice_notifier(&mpls_dev_notifier);
968 unregister_pernet_subsys(&mpls_net_ops);
969}
970module_exit(mpls_exit);
971
972MODULE_DESCRIPTION("MultiProtocol Label Switching");
973MODULE_LICENSE("GPL v2");
974MODULE_ALIAS_NETPROTO(PF_MPLS);