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