blob: 9fdd94cba83e4949f51c3c96479f0937f6ec0333 [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>
Stephen Rothwell4b5edb22015-03-05 13:37:05 +110010#include <linux/vmalloc.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060011#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
Eric W. Biedermana2519922015-03-03 19:12:40 -060020#define LABEL_NOT_SPECIFIED (1<<20)
Eric W. Biederman01891972015-03-03 19:10:47 -060021#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 */
Eric W. Biederman19d0c342015-03-07 16:21:56 -060027 struct net_device __rcu *rt_dev;
Eric W. Biederman01891972015-03-03 19:10:47 -060028 struct rcu_head rt_rcu;
29 u32 rt_label[MAX_NEW_LABELS];
30 u8 rt_protocol; /* routing protocol that set this entry */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -060031 u8 rt_labels;
32 u8 rt_via_alen;
33 u8 rt_via_table;
Eric W. Biederman01891972015-03-03 19:10:47 -060034 u8 rt_via[0];
35};
36
Eric W. Biederman7720c012015-03-03 19:11:20 -060037static int zero = 0;
38static int label_limit = (1 << 20) - 1;
39
Eric W. Biederman8de147d2015-03-03 19:14:31 -060040static 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
Eric W. Biederman01891972015-03-03 19:10:47 -060044static 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
Robert Shearman03c57742015-04-22 11:14:37 +010056static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
57{
58 return rcu_dereference_rtnl(dev->mpls_ptr);
59}
60
Eric W. Biederman01891972015-03-03 19:10:47 -060061static bool mpls_output_possible(const struct net_device *dev)
62{
63 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
64}
65
66static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
67{
68 /* The size of the layer 2.5 labels to be added for this route */
69 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
70}
71
72static unsigned int mpls_dev_mtu(const struct net_device *dev)
73{
74 /* The amount of data the layer 2 frame can hold */
75 return dev->mtu;
76}
77
78static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
79{
80 if (skb->len <= mtu)
81 return false;
82
83 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
84 return false;
85
86 return true;
87}
88
89static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
90 struct mpls_entry_decoded dec)
91{
92 /* RFC4385 and RFC5586 encode other packets in mpls such that
93 * they don't conflict with the ip version number, making
94 * decoding by examining the ip version correct in everything
95 * except for the strangest cases.
96 *
97 * The strange cases if we choose to support them will require
98 * manual configuration.
99 */
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500100 struct iphdr *hdr4;
Eric W. Biederman01891972015-03-03 19:10:47 -0600101 bool success = true;
102
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500103 /* The IPv4 code below accesses through the IPv4 header
104 * checksum, which is 12 bytes into the packet.
105 * The IPv6 code below accesses through the IPv6 hop limit
106 * which is 8 bytes into the packet.
107 *
108 * For all supported cases there should always be at least 12
109 * bytes of packet data present. The IPv4 header is 20 bytes
110 * without options and the IPv6 header is always 40 bytes
111 * long.
112 */
113 if (!pskb_may_pull(skb, 12))
114 return false;
115
116 /* Use ip_hdr to find the ip protocol version */
117 hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600118 if (hdr4->version == 4) {
119 skb->protocol = htons(ETH_P_IP);
120 csum_replace2(&hdr4->check,
121 htons(hdr4->ttl << 8),
122 htons(dec.ttl << 8));
123 hdr4->ttl = dec.ttl;
124 }
125 else if (hdr4->version == 6) {
126 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
127 skb->protocol = htons(ETH_P_IPV6);
128 hdr6->hop_limit = dec.ttl;
129 }
130 else
131 /* version 0 and version 1 are used by pseudo wires */
132 success = false;
133 return success;
134}
135
136static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
137 struct packet_type *pt, struct net_device *orig_dev)
138{
139 struct net *net = dev_net(dev);
140 struct mpls_shim_hdr *hdr;
141 struct mpls_route *rt;
142 struct mpls_entry_decoded dec;
143 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100144 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600145 unsigned int hh_len;
146 unsigned int new_header_size;
147 unsigned int mtu;
148 int err;
149
150 /* Careful this entire function runs inside of an rcu critical section */
151
Robert Shearman03c57742015-04-22 11:14:37 +0100152 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100153 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100154 goto drop;
155
Eric W. Biederman01891972015-03-03 19:10:47 -0600156 if (skb->pkt_type != PACKET_HOST)
157 goto drop;
158
159 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
160 goto drop;
161
162 if (!pskb_may_pull(skb, sizeof(*hdr)))
163 goto drop;
164
165 /* Read and decode the label */
166 hdr = mpls_hdr(skb);
167 dec = mpls_entry_decode(hdr);
168
169 /* Pop the label */
170 skb_pull(skb, sizeof(*hdr));
171 skb_reset_network_header(skb);
172
173 skb_orphan(skb);
174
175 rt = mpls_route_input_rcu(net, dec.label);
176 if (!rt)
177 goto drop;
178
179 /* Find the output device */
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600180 out_dev = rcu_dereference(rt->rt_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600181 if (!mpls_output_possible(out_dev))
182 goto drop;
183
184 if (skb_warn_if_lro(skb))
185 goto drop;
186
187 skb_forward_csum(skb);
188
189 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600190 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600191 goto drop;
192 dec.ttl -= 1;
193
194 /* Verify the destination can hold the packet */
195 new_header_size = mpls_rt_header_size(rt);
196 mtu = mpls_dev_mtu(out_dev);
197 if (mpls_pkt_too_big(skb, mtu - new_header_size))
198 goto drop;
199
200 hh_len = LL_RESERVED_SPACE(out_dev);
201 if (!out_dev->header_ops)
202 hh_len = 0;
203
204 /* Ensure there is enough space for the headers in the skb */
205 if (skb_cow(skb, hh_len + new_header_size))
206 goto drop;
207
208 skb->dev = out_dev;
209 skb->protocol = htons(ETH_P_MPLS_UC);
210
211 if (unlikely(!new_header_size && dec.bos)) {
212 /* Penultimate hop popping */
213 if (!mpls_egress(rt, skb, dec))
214 goto drop;
215 } else {
216 bool bos;
217 int i;
218 skb_push(skb, new_header_size);
219 skb_reset_network_header(skb);
220 /* Push the new labels */
221 hdr = mpls_hdr(skb);
222 bos = dec.bos;
223 for (i = rt->rt_labels - 1; i >= 0; i--) {
224 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
225 bos = false;
226 }
227 }
228
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600229 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600230 if (err)
231 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
232 __func__, err);
233 return 0;
234
235drop:
236 kfree_skb(skb);
237 return NET_RX_DROP;
238}
239
240static struct packet_type mpls_packet_type __read_mostly = {
241 .type = cpu_to_be16(ETH_P_MPLS_UC),
242 .func = mpls_forward,
243};
244
Wu Fengguangf0126532015-03-05 05:33:54 +0800245static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600246 [RTA_DST] = { .type = NLA_U32 },
247 [RTA_OIF] = { .type = NLA_U32 },
248};
249
Eric W. Biedermana2519922015-03-03 19:12:40 -0600250struct mpls_route_config {
251 u32 rc_protocol;
252 u32 rc_ifindex;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600253 u16 rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600254 u16 rc_via_alen;
255 u8 rc_via[MAX_VIA_ALEN];
256 u32 rc_label;
257 u32 rc_output_labels;
258 u32 rc_output_label[MAX_NEW_LABELS];
259 u32 rc_nlflags;
260 struct nl_info rc_nlinfo;
261};
262
Eric W. Biederman01891972015-03-03 19:10:47 -0600263static struct mpls_route *mpls_rt_alloc(size_t alen)
264{
265 struct mpls_route *rt;
266
Eric W. Biedermand8656162015-03-07 16:19:41 -0600267 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600268 if (rt)
269 rt->rt_via_alen = alen;
270 return rt;
271}
272
273static void mpls_rt_free(struct mpls_route *rt)
274{
275 if (rt)
276 kfree_rcu(rt, rt_rcu);
277}
278
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600279static void mpls_notify_route(struct net *net, unsigned index,
280 struct mpls_route *old, struct mpls_route *new,
281 const struct nl_info *info)
282{
283 struct nlmsghdr *nlh = info ? info->nlh : NULL;
284 unsigned portid = info ? info->portid : 0;
285 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
286 struct mpls_route *rt = new ? new : old;
287 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
288 /* Ignore reserved labels for now */
289 if (rt && (index >= 16))
290 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
291}
292
Eric W. Biederman01891972015-03-03 19:10:47 -0600293static void mpls_route_update(struct net *net, unsigned index,
294 struct net_device *dev, struct mpls_route *new,
295 const struct nl_info *info)
296{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600297 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600298 struct mpls_route *rt, *old = NULL;
299
300 ASSERT_RTNL();
301
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600302 platform_label = rtnl_dereference(net->mpls.platform_label);
303 rt = rtnl_dereference(platform_label[index]);
304 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
305 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600306 old = rt;
307 }
308
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600309 mpls_notify_route(net, index, old, new, info);
310
Eric W. Biederman01891972015-03-03 19:10:47 -0600311 /* If we removed a route free it now */
312 mpls_rt_free(old);
313}
314
Eric W. Biedermana2519922015-03-03 19:12:40 -0600315static unsigned find_free_label(struct net *net)
316{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600317 struct mpls_route __rcu **platform_label;
318 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600319 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600320
321 platform_label = rtnl_dereference(net->mpls.platform_label);
322 platform_labels = net->mpls.platform_labels;
323 for (index = 16; index < platform_labels; index++) {
324 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600325 return index;
326 }
327 return LABEL_NOT_SPECIFIED;
328}
329
330static int mpls_route_add(struct mpls_route_config *cfg)
331{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600332 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600333 struct net *net = cfg->rc_nlinfo.nl_net;
334 struct net_device *dev = NULL;
335 struct mpls_route *rt, *old;
336 unsigned index;
337 int i;
338 int err = -EINVAL;
339
340 index = cfg->rc_label;
341
342 /* If a label was not specified during insert pick one */
343 if ((index == LABEL_NOT_SPECIFIED) &&
344 (cfg->rc_nlflags & NLM_F_CREATE)) {
345 index = find_free_label(net);
346 }
347
348 /* The first 16 labels are reserved, and may not be set */
349 if (index < 16)
350 goto errout;
351
352 /* The full 20 bit range may not be supported. */
353 if (index >= net->mpls.platform_labels)
354 goto errout;
355
356 /* Ensure only a supported number of labels are present */
357 if (cfg->rc_output_labels > MAX_NEW_LABELS)
358 goto errout;
359
360 err = -ENODEV;
361 dev = dev_get_by_index(net, cfg->rc_ifindex);
362 if (!dev)
363 goto errout;
364
Robert Shearman03c57742015-04-22 11:14:37 +0100365 /* Ensure this is a supported device */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600366 err = -EINVAL;
Robert Shearman03c57742015-04-22 11:14:37 +0100367 if (!mpls_dev_get(dev))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600368 goto errout;
369
370 err = -EINVAL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600371 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
Eric W. Biedermana2519922015-03-03 19:12:40 -0600372 (dev->addr_len != cfg->rc_via_alen))
373 goto errout;
374
375 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600376 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600377 if (cfg->rc_nlflags & NLM_F_APPEND)
378 goto errout;
379
380 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600381 platform_label = rtnl_dereference(net->mpls.platform_label);
382 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600383 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
384 goto errout;
385
386 err = -EEXIST;
387 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
388 goto errout;
389
390 err = -ENOENT;
391 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
392 goto errout;
393
394 err = -ENOMEM;
395 rt = mpls_rt_alloc(cfg->rc_via_alen);
396 if (!rt)
397 goto errout;
398
399 rt->rt_labels = cfg->rc_output_labels;
400 for (i = 0; i < rt->rt_labels; i++)
401 rt->rt_label[i] = cfg->rc_output_label[i];
402 rt->rt_protocol = cfg->rc_protocol;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600403 RCU_INIT_POINTER(rt->rt_dev, dev);
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600404 rt->rt_via_table = cfg->rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600405 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
406
407 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
408
409 dev_put(dev);
410 return 0;
411
412errout:
413 if (dev)
414 dev_put(dev);
415 return err;
416}
417
418static int mpls_route_del(struct mpls_route_config *cfg)
419{
420 struct net *net = cfg->rc_nlinfo.nl_net;
421 unsigned index;
422 int err = -EINVAL;
423
424 index = cfg->rc_label;
425
426 /* The first 16 labels are reserved, and may not be removed */
427 if (index < 16)
428 goto errout;
429
430 /* The full 20 bit range may not be supported */
431 if (index >= net->mpls.platform_labels)
432 goto errout;
433
434 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
435
436 err = 0;
437errout:
438 return err;
439}
440
Robert Shearman37bde792015-04-22 11:14:38 +0100441#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
442 (&((struct mpls_dev *)0)->field)
443
444static const struct ctl_table mpls_dev_table[] = {
445 {
446 .procname = "input",
447 .maxlen = sizeof(int),
448 .mode = 0644,
449 .proc_handler = proc_dointvec,
450 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
451 },
452 { }
453};
454
455static int mpls_dev_sysctl_register(struct net_device *dev,
456 struct mpls_dev *mdev)
457{
458 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
459 struct ctl_table *table;
460 int i;
461
462 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
463 if (!table)
464 goto out;
465
466 /* Table data contains only offsets relative to the base of
467 * the mdev at this point, so make them absolute.
468 */
469 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
470 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
471
472 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
473
474 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
475 if (!mdev->sysctl)
476 goto free;
477
478 return 0;
479
480free:
481 kfree(table);
482out:
483 return -ENOBUFS;
484}
485
486static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
487{
488 struct ctl_table *table;
489
490 table = mdev->sysctl->ctl_table_arg;
491 unregister_net_sysctl_table(mdev->sysctl);
492 kfree(table);
493}
494
Robert Shearman03c57742015-04-22 11:14:37 +0100495static struct mpls_dev *mpls_add_dev(struct net_device *dev)
496{
497 struct mpls_dev *mdev;
498 int err = -ENOMEM;
499
500 ASSERT_RTNL();
501
502 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
503 if (!mdev)
504 return ERR_PTR(err);
505
Robert Shearman37bde792015-04-22 11:14:38 +0100506 err = mpls_dev_sysctl_register(dev, mdev);
507 if (err)
508 goto free;
509
Robert Shearman03c57742015-04-22 11:14:37 +0100510 rcu_assign_pointer(dev->mpls_ptr, mdev);
511
512 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100513
514free:
515 kfree(mdev);
516 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100517}
518
Eric W. Biederman01891972015-03-03 19:10:47 -0600519static void mpls_ifdown(struct net_device *dev)
520{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600521 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600522 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100523 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600524 unsigned index;
525
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600526 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600527 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600528 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600529 if (!rt)
530 continue;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600531 if (rtnl_dereference(rt->rt_dev) != dev)
Eric W. Biederman01891972015-03-03 19:10:47 -0600532 continue;
533 rt->rt_dev = NULL;
534 }
Robert Shearman03c57742015-04-22 11:14:37 +0100535
536 mdev = mpls_dev_get(dev);
537 if (!mdev)
538 return;
539
Robert Shearman37bde792015-04-22 11:14:38 +0100540 mpls_dev_sysctl_unregister(mdev);
541
Robert Shearman03c57742015-04-22 11:14:37 +0100542 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
543
544 kfree(mdev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600545}
546
547static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
548 void *ptr)
549{
550 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100551 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600552
553 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100554 case NETDEV_REGISTER:
555 /* For now just support ethernet devices */
556 if ((dev->type == ARPHRD_ETHER) ||
557 (dev->type == ARPHRD_LOOPBACK)) {
558 mdev = mpls_add_dev(dev);
559 if (IS_ERR(mdev))
560 return notifier_from_errno(PTR_ERR(mdev));
561 }
562 break;
563
Eric W. Biederman01891972015-03-03 19:10:47 -0600564 case NETDEV_UNREGISTER:
565 mpls_ifdown(dev);
566 break;
567 }
568 return NOTIFY_OK;
569}
570
571static struct notifier_block mpls_dev_notifier = {
572 .notifier_call = mpls_dev_notify,
573};
574
Eric W. Biederman03c05662015-03-03 19:13:56 -0600575static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600576 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600577{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600578 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
579 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
580 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600581 struct nlattr *nla;
582 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600583 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600584
585 nla = nla_reserve(skb, RTA_VIA, alen + 2);
586 if (!nla)
587 return -EMSGSIZE;
588
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600589 if (table <= NEIGH_NR_TABLES)
590 family = table_to_family[table];
591
Eric W. Biederman03c05662015-03-03 19:13:56 -0600592 via = nla_data(nla);
593 via->rtvia_family = family;
594 memcpy(via->rtvia_addr, addr, alen);
595 return 0;
596}
597
Eric W. Biederman966bae32015-03-03 19:13:19 -0600598int nla_put_labels(struct sk_buff *skb, int attrtype,
599 u8 labels, const u32 label[])
600{
601 struct nlattr *nla;
602 struct mpls_shim_hdr *nla_label;
603 bool bos;
604 int i;
605 nla = nla_reserve(skb, attrtype, labels*4);
606 if (!nla)
607 return -EMSGSIZE;
608
609 nla_label = nla_data(nla);
610 bos = true;
611 for (i = labels - 1; i >= 0; i--) {
612 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
613 bos = false;
614 }
615
616 return 0;
617}
618
619int nla_get_labels(const struct nlattr *nla,
620 u32 max_labels, u32 *labels, u32 label[])
621{
622 unsigned len = nla_len(nla);
623 unsigned nla_labels;
624 struct mpls_shim_hdr *nla_label;
625 bool bos;
626 int i;
627
628 /* len needs to be an even multiple of 4 (the label size) */
629 if (len & 3)
630 return -EINVAL;
631
632 /* Limit the number of new labels allowed */
633 nla_labels = len/4;
634 if (nla_labels > max_labels)
635 return -EINVAL;
636
637 nla_label = nla_data(nla);
638 bos = true;
639 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
640 struct mpls_entry_decoded dec;
641 dec = mpls_entry_decode(nla_label + i);
642
643 /* Ensure the bottom of stack flag is properly set
644 * and ttl and tc are both clear.
645 */
646 if ((dec.bos != bos) || dec.ttl || dec.tc)
647 return -EINVAL;
648
649 label[i] = dec.label;
650 }
651 *labels = nla_labels;
652 return 0;
653}
654
Eric W. Biederman03c05662015-03-03 19:13:56 -0600655static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
656 struct mpls_route_config *cfg)
657{
658 struct rtmsg *rtm;
659 struct nlattr *tb[RTA_MAX+1];
660 int index;
661 int err;
662
663 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
664 if (err < 0)
665 goto errout;
666
667 err = -EINVAL;
668 rtm = nlmsg_data(nlh);
669 memset(cfg, 0, sizeof(*cfg));
670
671 if (rtm->rtm_family != AF_MPLS)
672 goto errout;
673 if (rtm->rtm_dst_len != 20)
674 goto errout;
675 if (rtm->rtm_src_len != 0)
676 goto errout;
677 if (rtm->rtm_tos != 0)
678 goto errout;
679 if (rtm->rtm_table != RT_TABLE_MAIN)
680 goto errout;
681 /* Any value is acceptable for rtm_protocol */
682
683 /* As mpls uses destination specific addresses
684 * (or source specific address in the case of multicast)
685 * all addresses have universal scope.
686 */
687 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
688 goto errout;
689 if (rtm->rtm_type != RTN_UNICAST)
690 goto errout;
691 if (rtm->rtm_flags != 0)
692 goto errout;
693
694 cfg->rc_label = LABEL_NOT_SPECIFIED;
695 cfg->rc_protocol = rtm->rtm_protocol;
696 cfg->rc_nlflags = nlh->nlmsg_flags;
697 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
698 cfg->rc_nlinfo.nlh = nlh;
699 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
700
701 for (index = 0; index <= RTA_MAX; index++) {
702 struct nlattr *nla = tb[index];
703 if (!nla)
704 continue;
705
706 switch(index) {
707 case RTA_OIF:
708 cfg->rc_ifindex = nla_get_u32(nla);
709 break;
710 case RTA_NEWDST:
711 if (nla_get_labels(nla, MAX_NEW_LABELS,
712 &cfg->rc_output_labels,
713 cfg->rc_output_label))
714 goto errout;
715 break;
716 case RTA_DST:
717 {
718 u32 label_count;
719 if (nla_get_labels(nla, 1, &label_count,
720 &cfg->rc_label))
721 goto errout;
722
723 /* The first 16 labels are reserved, and may not be set */
724 if (cfg->rc_label < 16)
725 goto errout;
726
727 break;
728 }
729 case RTA_VIA:
730 {
731 struct rtvia *via = nla_data(nla);
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000732 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
733 goto errout;
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000734 cfg->rc_via_alen = nla_len(nla) -
735 offsetof(struct rtvia, rtvia_addr);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600736 if (cfg->rc_via_alen > MAX_VIA_ALEN)
737 goto errout;
738
739 /* Validate the address family */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600740 switch(via->rtvia_family) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600741 case AF_PACKET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600742 cfg->rc_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600743 break;
744 case AF_INET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600745 cfg->rc_via_table = NEIGH_ARP_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600746 if (cfg->rc_via_alen != 4)
747 goto errout;
748 break;
749 case AF_INET6:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600750 cfg->rc_via_table = NEIGH_ND_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600751 if (cfg->rc_via_alen != 16)
752 goto errout;
753 break;
754 default:
755 /* Unsupported address family */
756 goto errout;
757 }
758
759 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
760 break;
761 }
762 default:
763 /* Unsupported attribute */
764 goto errout;
765 }
766 }
767
768 err = 0;
769errout:
770 return err;
771}
772
773static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
774{
775 struct mpls_route_config cfg;
776 int err;
777
778 err = rtm_to_route_config(skb, nlh, &cfg);
779 if (err < 0)
780 return err;
781
782 return mpls_route_del(&cfg);
783}
784
785
786static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
787{
788 struct mpls_route_config cfg;
789 int err;
790
791 err = rtm_to_route_config(skb, nlh, &cfg);
792 if (err < 0)
793 return err;
794
795 return mpls_route_add(&cfg);
796}
797
798static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
799 u32 label, struct mpls_route *rt, int flags)
800{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600801 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600802 struct nlmsghdr *nlh;
803 struct rtmsg *rtm;
804
805 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
806 if (nlh == NULL)
807 return -EMSGSIZE;
808
809 rtm = nlmsg_data(nlh);
810 rtm->rtm_family = AF_MPLS;
811 rtm->rtm_dst_len = 20;
812 rtm->rtm_src_len = 0;
813 rtm->rtm_tos = 0;
814 rtm->rtm_table = RT_TABLE_MAIN;
815 rtm->rtm_protocol = rt->rt_protocol;
816 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
817 rtm->rtm_type = RTN_UNICAST;
818 rtm->rtm_flags = 0;
819
820 if (rt->rt_labels &&
821 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
822 goto nla_put_failure;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600823 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600824 goto nla_put_failure;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600825 dev = rtnl_dereference(rt->rt_dev);
826 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600827 goto nla_put_failure;
828 if (nla_put_labels(skb, RTA_DST, 1, &label))
829 goto nla_put_failure;
830
831 nlmsg_end(skb, nlh);
832 return 0;
833
834nla_put_failure:
835 nlmsg_cancel(skb, nlh);
836 return -EMSGSIZE;
837}
838
839static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
840{
841 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600842 struct mpls_route __rcu **platform_label;
843 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600844 unsigned int index;
845
846 ASSERT_RTNL();
847
848 index = cb->args[0];
849 if (index < 16)
850 index = 16;
851
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600852 platform_label = rtnl_dereference(net->mpls.platform_label);
853 platform_labels = net->mpls.platform_labels;
854 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600855 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600856 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600857 if (!rt)
858 continue;
859
860 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
861 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
862 index, rt, NLM_F_MULTI) < 0)
863 break;
864 }
865 cb->args[0] = index;
866
867 return skb->len;
868}
869
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600870static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
871{
872 size_t payload =
873 NLMSG_ALIGN(sizeof(struct rtmsg))
874 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
875 + nla_total_size(4); /* RTA_DST */
876 if (rt->rt_labels) /* RTA_NEWDST */
877 payload += nla_total_size(rt->rt_labels * 4);
878 if (rt->rt_dev) /* RTA_OIF */
879 payload += nla_total_size(4);
880 return payload;
881}
882
883static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
884 struct nlmsghdr *nlh, struct net *net, u32 portid,
885 unsigned int nlm_flags)
886{
887 struct sk_buff *skb;
888 u32 seq = nlh ? nlh->nlmsg_seq : 0;
889 int err = -ENOBUFS;
890
891 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
892 if (skb == NULL)
893 goto errout;
894
895 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
896 if (err < 0) {
897 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
898 WARN_ON(err == -EMSGSIZE);
899 kfree_skb(skb);
900 goto errout;
901 }
902 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
903
904 return;
905errout:
906 if (err < 0)
907 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
908}
909
Eric W. Biederman7720c012015-03-03 19:11:20 -0600910static int resize_platform_label_table(struct net *net, size_t limit)
911{
912 size_t size = sizeof(struct mpls_route *) * limit;
913 size_t old_limit;
914 size_t cp_size;
915 struct mpls_route __rcu **labels = NULL, **old;
916 struct mpls_route *rt0 = NULL, *rt2 = NULL;
917 unsigned index;
918
919 if (size) {
920 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
921 if (!labels)
922 labels = vzalloc(size);
923
924 if (!labels)
925 goto nolabels;
926 }
927
928 /* In case the predefined labels need to be populated */
929 if (limit > LABEL_IPV4_EXPLICIT_NULL) {
930 struct net_device *lo = net->loopback_dev;
931 rt0 = mpls_rt_alloc(lo->addr_len);
932 if (!rt0)
933 goto nort0;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600934 RCU_INIT_POINTER(rt0->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600935 rt0->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600936 rt0->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -0600937 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
938 }
939 if (limit > LABEL_IPV6_EXPLICIT_NULL) {
940 struct net_device *lo = net->loopback_dev;
941 rt2 = mpls_rt_alloc(lo->addr_len);
942 if (!rt2)
943 goto nort2;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600944 RCU_INIT_POINTER(rt2->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600945 rt2->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600946 rt2->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -0600947 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
948 }
949
950 rtnl_lock();
951 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600952 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600953 old_limit = net->mpls.platform_labels;
954
955 /* Free any labels beyond the new table */
956 for (index = limit; index < old_limit; index++)
957 mpls_route_update(net, index, NULL, NULL, NULL);
958
959 /* Copy over the old labels */
960 cp_size = size;
961 if (old_limit < limit)
962 cp_size = old_limit * sizeof(struct mpls_route *);
963
964 memcpy(labels, old, cp_size);
965
966 /* If needed set the predefined labels */
967 if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) &&
968 (limit > LABEL_IPV6_EXPLICIT_NULL)) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600969 RCU_INIT_POINTER(labels[LABEL_IPV6_EXPLICIT_NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600970 rt2 = NULL;
971 }
972
973 if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) &&
974 (limit > LABEL_IPV4_EXPLICIT_NULL)) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600975 RCU_INIT_POINTER(labels[LABEL_IPV4_EXPLICIT_NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600976 rt0 = NULL;
977 }
978
979 /* Update the global pointers */
980 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600981 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -0600982
983 rtnl_unlock();
984
985 mpls_rt_free(rt2);
986 mpls_rt_free(rt0);
987
988 if (old) {
989 synchronize_rcu();
990 kvfree(old);
991 }
992 return 0;
993
994nort2:
995 mpls_rt_free(rt0);
996nort0:
997 kvfree(labels);
998nolabels:
999 return -ENOMEM;
1000}
1001
1002static int mpls_platform_labels(struct ctl_table *table, int write,
1003 void __user *buffer, size_t *lenp, loff_t *ppos)
1004{
1005 struct net *net = table->data;
1006 int platform_labels = net->mpls.platform_labels;
1007 int ret;
1008 struct ctl_table tmp = {
1009 .procname = table->procname,
1010 .data = &platform_labels,
1011 .maxlen = sizeof(int),
1012 .mode = table->mode,
1013 .extra1 = &zero,
1014 .extra2 = &label_limit,
1015 };
1016
1017 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1018
1019 if (write && ret == 0)
1020 ret = resize_platform_label_table(net, platform_labels);
1021
1022 return ret;
1023}
1024
Robert Shearman37bde792015-04-22 11:14:38 +01001025static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001026 {
1027 .procname = "platform_labels",
1028 .data = NULL,
1029 .maxlen = sizeof(int),
1030 .mode = 0644,
1031 .proc_handler = mpls_platform_labels,
1032 },
1033 { }
1034};
1035
Eric W. Biederman01891972015-03-03 19:10:47 -06001036static int mpls_net_init(struct net *net)
1037{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001038 struct ctl_table *table;
1039
Eric W. Biederman01891972015-03-03 19:10:47 -06001040 net->mpls.platform_labels = 0;
1041 net->mpls.platform_label = NULL;
1042
Eric W. Biederman7720c012015-03-03 19:11:20 -06001043 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1044 if (table == NULL)
1045 return -ENOMEM;
1046
1047 table[0].data = net;
1048 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1049 if (net->mpls.ctl == NULL)
1050 return -ENOMEM;
1051
Eric W. Biederman01891972015-03-03 19:10:47 -06001052 return 0;
1053}
1054
1055static void mpls_net_exit(struct net *net)
1056{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001057 struct mpls_route __rcu **platform_label;
1058 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001059 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001060 unsigned int index;
1061
Eric W. Biederman7720c012015-03-03 19:11:20 -06001062 table = net->mpls.ctl->ctl_table_arg;
1063 unregister_net_sysctl_table(net->mpls.ctl);
1064 kfree(table);
1065
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001066 /* An rcu grace period has passed since there was a device in
1067 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001068 * left this network namespace. This is because
1069 * unregister_netdevice_many and netdev_run_todo has completed
1070 * for each network device that was in this network namespace.
1071 *
1072 * As such no additional rcu synchronization is necessary when
1073 * freeing the platform_label table.
1074 */
1075 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001076 platform_label = rtnl_dereference(net->mpls.platform_label);
1077 platform_labels = net->mpls.platform_labels;
1078 for (index = 0; index < platform_labels; index++) {
1079 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1080 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001081 mpls_rt_free(rt);
1082 }
1083 rtnl_unlock();
1084
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001085 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001086}
1087
1088static struct pernet_operations mpls_net_ops = {
1089 .init = mpls_net_init,
1090 .exit = mpls_net_exit,
1091};
1092
1093static int __init mpls_init(void)
1094{
1095 int err;
1096
1097 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1098
1099 err = register_pernet_subsys(&mpls_net_ops);
1100 if (err)
1101 goto out;
1102
1103 err = register_netdevice_notifier(&mpls_dev_notifier);
1104 if (err)
1105 goto out_unregister_pernet;
1106
1107 dev_add_pack(&mpls_packet_type);
1108
Eric W. Biederman03c05662015-03-03 19:13:56 -06001109 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1110 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1111 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001112 err = 0;
1113out:
1114 return err;
1115
1116out_unregister_pernet:
1117 unregister_pernet_subsys(&mpls_net_ops);
1118 goto out;
1119}
1120module_init(mpls_init);
1121
1122static void __exit mpls_exit(void)
1123{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001124 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001125 dev_remove_pack(&mpls_packet_type);
1126 unregister_netdevice_notifier(&mpls_dev_notifier);
1127 unregister_pernet_subsys(&mpls_net_ops);
1128}
1129module_exit(mpls_exit);
1130
1131MODULE_DESCRIPTION("MultiProtocol Label Switching");
1132MODULE_LICENSE("GPL v2");
1133MODULE_ALIAS_NETPROTO(PF_MPLS);