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