blob: 49f1b0e41bdf6d495fc0a015b02eeca4224a0404 [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>
Roopa Prabhu01faef22015-07-21 09:16:24 -070018#include <net/ip6_route.h>
Eric W. Biederman01891972015-03-03 19:10:47 -060019#include "internal.h"
20
Eric W. Biedermana2519922015-03-03 19:12:40 -060021#define LABEL_NOT_SPECIFIED (1<<20)
Eric W. Biederman01891972015-03-03 19:10:47 -060022#define MAX_NEW_LABELS 2
23
24/* This maximum ha length copied from the definition of struct neighbour */
25#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
26
27struct mpls_route { /* next hop label forwarding entry */
Eric W. Biederman19d0c342015-03-07 16:21:56 -060028 struct net_device __rcu *rt_dev;
Eric W. Biederman01891972015-03-03 19:10:47 -060029 struct rcu_head rt_rcu;
30 u32 rt_label[MAX_NEW_LABELS];
31 u8 rt_protocol; /* routing protocol that set this entry */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -060032 u8 rt_labels;
33 u8 rt_via_alen;
34 u8 rt_via_table;
Eric W. Biederman01891972015-03-03 19:10:47 -060035 u8 rt_via[0];
36};
37
Eric W. Biederman7720c012015-03-03 19:11:20 -060038static int zero = 0;
39static int label_limit = (1 << 20) - 1;
40
Eric W. Biederman8de147d2015-03-03 19:14:31 -060041static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
42 struct nlmsghdr *nlh, struct net *net, u32 portid,
43 unsigned int nlm_flags);
44
Eric W. Biederman01891972015-03-03 19:10:47 -060045static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
46{
47 struct mpls_route *rt = NULL;
48
49 if (index < net->mpls.platform_labels) {
50 struct mpls_route __rcu **platform_label =
51 rcu_dereference(net->mpls.platform_label);
52 rt = rcu_dereference(platform_label[index]);
53 }
54 return rt;
55}
56
Robert Shearman03c57742015-04-22 11:14:37 +010057static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
58{
59 return rcu_dereference_rtnl(dev->mpls_ptr);
60}
61
Roopa Prabhuface0182015-07-21 10:43:52 +020062bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060063{
64 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
65}
Roopa Prabhuface0182015-07-21 10:43:52 +020066EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060067
68static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
69{
70 /* The size of the layer 2.5 labels to be added for this route */
71 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
72}
73
Roopa Prabhuface0182015-07-21 10:43:52 +020074unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060075{
76 /* The amount of data the layer 2 frame can hold */
77 return dev->mtu;
78}
Roopa Prabhuface0182015-07-21 10:43:52 +020079EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060080
Roopa Prabhuface0182015-07-21 10:43:52 +020081bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060082{
83 if (skb->len <= mtu)
84 return false;
85
86 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
87 return false;
88
89 return true;
90}
Roopa Prabhuface0182015-07-21 10:43:52 +020091EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060092
93static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
94 struct mpls_entry_decoded dec)
95{
96 /* RFC4385 and RFC5586 encode other packets in mpls such that
97 * they don't conflict with the ip version number, making
98 * decoding by examining the ip version correct in everything
99 * except for the strangest cases.
100 *
101 * The strange cases if we choose to support them will require
102 * manual configuration.
103 */
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500104 struct iphdr *hdr4;
Eric W. Biederman01891972015-03-03 19:10:47 -0600105 bool success = true;
106
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500107 /* The IPv4 code below accesses through the IPv4 header
108 * checksum, which is 12 bytes into the packet.
109 * The IPv6 code below accesses through the IPv6 hop limit
110 * which is 8 bytes into the packet.
111 *
112 * For all supported cases there should always be at least 12
113 * bytes of packet data present. The IPv4 header is 20 bytes
114 * without options and the IPv6 header is always 40 bytes
115 * long.
116 */
117 if (!pskb_may_pull(skb, 12))
118 return false;
119
120 /* Use ip_hdr to find the ip protocol version */
121 hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600122 if (hdr4->version == 4) {
123 skb->protocol = htons(ETH_P_IP);
124 csum_replace2(&hdr4->check,
125 htons(hdr4->ttl << 8),
126 htons(dec.ttl << 8));
127 hdr4->ttl = dec.ttl;
128 }
129 else if (hdr4->version == 6) {
130 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
131 skb->protocol = htons(ETH_P_IPV6);
132 hdr6->hop_limit = dec.ttl;
133 }
134 else
135 /* version 0 and version 1 are used by pseudo wires */
136 success = false;
137 return success;
138}
139
140static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
141 struct packet_type *pt, struct net_device *orig_dev)
142{
143 struct net *net = dev_net(dev);
144 struct mpls_shim_hdr *hdr;
145 struct mpls_route *rt;
146 struct mpls_entry_decoded dec;
147 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100148 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600149 unsigned int hh_len;
150 unsigned int new_header_size;
151 unsigned int mtu;
152 int err;
153
154 /* Careful this entire function runs inside of an rcu critical section */
155
Robert Shearman03c57742015-04-22 11:14:37 +0100156 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100157 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100158 goto drop;
159
Eric W. Biederman01891972015-03-03 19:10:47 -0600160 if (skb->pkt_type != PACKET_HOST)
161 goto drop;
162
163 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
164 goto drop;
165
166 if (!pskb_may_pull(skb, sizeof(*hdr)))
167 goto drop;
168
169 /* Read and decode the label */
170 hdr = mpls_hdr(skb);
171 dec = mpls_entry_decode(hdr);
172
173 /* Pop the label */
174 skb_pull(skb, sizeof(*hdr));
175 skb_reset_network_header(skb);
176
177 skb_orphan(skb);
178
179 rt = mpls_route_input_rcu(net, dec.label);
180 if (!rt)
181 goto drop;
182
183 /* Find the output device */
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600184 out_dev = rcu_dereference(rt->rt_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600185 if (!mpls_output_possible(out_dev))
186 goto drop;
187
188 if (skb_warn_if_lro(skb))
189 goto drop;
190
191 skb_forward_csum(skb);
192
193 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600194 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600195 goto drop;
196 dec.ttl -= 1;
197
198 /* Verify the destination can hold the packet */
199 new_header_size = mpls_rt_header_size(rt);
200 mtu = mpls_dev_mtu(out_dev);
201 if (mpls_pkt_too_big(skb, mtu - new_header_size))
202 goto drop;
203
204 hh_len = LL_RESERVED_SPACE(out_dev);
205 if (!out_dev->header_ops)
206 hh_len = 0;
207
208 /* Ensure there is enough space for the headers in the skb */
209 if (skb_cow(skb, hh_len + new_header_size))
210 goto drop;
211
212 skb->dev = out_dev;
213 skb->protocol = htons(ETH_P_MPLS_UC);
214
215 if (unlikely(!new_header_size && dec.bos)) {
216 /* Penultimate hop popping */
217 if (!mpls_egress(rt, skb, dec))
218 goto drop;
219 } else {
220 bool bos;
221 int i;
222 skb_push(skb, new_header_size);
223 skb_reset_network_header(skb);
224 /* Push the new labels */
225 hdr = mpls_hdr(skb);
226 bos = dec.bos;
227 for (i = rt->rt_labels - 1; i >= 0; i--) {
228 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
229 bos = false;
230 }
231 }
232
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600233 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600234 if (err)
235 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
236 __func__, err);
237 return 0;
238
239drop:
240 kfree_skb(skb);
241 return NET_RX_DROP;
242}
243
244static struct packet_type mpls_packet_type __read_mostly = {
245 .type = cpu_to_be16(ETH_P_MPLS_UC),
246 .func = mpls_forward,
247};
248
Wu Fengguangf0126532015-03-05 05:33:54 +0800249static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600250 [RTA_DST] = { .type = NLA_U32 },
251 [RTA_OIF] = { .type = NLA_U32 },
252};
253
Eric W. Biedermana2519922015-03-03 19:12:40 -0600254struct mpls_route_config {
255 u32 rc_protocol;
256 u32 rc_ifindex;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600257 u16 rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600258 u16 rc_via_alen;
259 u8 rc_via[MAX_VIA_ALEN];
260 u32 rc_label;
261 u32 rc_output_labels;
262 u32 rc_output_label[MAX_NEW_LABELS];
263 u32 rc_nlflags;
264 struct nl_info rc_nlinfo;
265};
266
Eric W. Biederman01891972015-03-03 19:10:47 -0600267static struct mpls_route *mpls_rt_alloc(size_t alen)
268{
269 struct mpls_route *rt;
270
Eric W. Biedermand8656162015-03-07 16:19:41 -0600271 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600272 if (rt)
273 rt->rt_via_alen = alen;
274 return rt;
275}
276
277static void mpls_rt_free(struct mpls_route *rt)
278{
279 if (rt)
280 kfree_rcu(rt, rt_rcu);
281}
282
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600283static void mpls_notify_route(struct net *net, unsigned index,
284 struct mpls_route *old, struct mpls_route *new,
285 const struct nl_info *info)
286{
287 struct nlmsghdr *nlh = info ? info->nlh : NULL;
288 unsigned portid = info ? info->portid : 0;
289 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
290 struct mpls_route *rt = new ? new : old;
291 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
292 /* Ignore reserved labels for now */
293 if (rt && (index >= 16))
294 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
295}
296
Eric W. Biederman01891972015-03-03 19:10:47 -0600297static void mpls_route_update(struct net *net, unsigned index,
298 struct net_device *dev, struct mpls_route *new,
299 const struct nl_info *info)
300{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600301 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600302 struct mpls_route *rt, *old = NULL;
303
304 ASSERT_RTNL();
305
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600306 platform_label = rtnl_dereference(net->mpls.platform_label);
307 rt = rtnl_dereference(platform_label[index]);
308 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
309 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600310 old = rt;
311 }
312
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600313 mpls_notify_route(net, index, old, new, info);
314
Eric W. Biederman01891972015-03-03 19:10:47 -0600315 /* If we removed a route free it now */
316 mpls_rt_free(old);
317}
318
Eric W. Biedermana2519922015-03-03 19:12:40 -0600319static unsigned find_free_label(struct net *net)
320{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600321 struct mpls_route __rcu **platform_label;
322 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600323 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600324
325 platform_label = rtnl_dereference(net->mpls.platform_label);
326 platform_labels = net->mpls.platform_labels;
327 for (index = 16; index < platform_labels; index++) {
328 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600329 return index;
330 }
331 return LABEL_NOT_SPECIFIED;
332}
333
Roopa Prabhu01faef22015-07-21 09:16:24 -0700334static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
335{
336 struct net_device *dev = NULL;
337 struct rtable *rt;
338 struct in_addr daddr;
339
340 memcpy(&daddr, addr, sizeof(struct in_addr));
341 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
342 if (IS_ERR(rt))
343 goto errout;
344
345 dev = rt->dst.dev;
346 dev_hold(dev);
347
348 ip_rt_put(rt);
349
350errout:
351 return dev;
352}
353
354static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
355{
356 struct net_device *dev = NULL;
357 struct dst_entry *dst;
358 struct flowi6 fl6;
359
360 memset(&fl6, 0, sizeof(fl6));
361 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
362 dst = ip6_route_output(net, NULL, &fl6);
363 if (dst->error)
364 goto errout;
365
366 dev = dst->dev;
367 dev_hold(dev);
368
369errout:
370 dst_release(dst);
371
372 return dev;
373}
374
375static struct net_device *find_outdev(struct net *net,
376 struct mpls_route_config *cfg)
377{
378 struct net_device *dev = NULL;
379
380 if (!cfg->rc_ifindex) {
381 switch (cfg->rc_via_table) {
382 case NEIGH_ARP_TABLE:
383 dev = inet_fib_lookup_dev(net, cfg->rc_via);
384 break;
385 case NEIGH_ND_TABLE:
386 dev = inet6_fib_lookup_dev(net, cfg->rc_via);
387 break;
388 case NEIGH_LINK_TABLE:
389 break;
390 }
391 } else {
392 dev = dev_get_by_index(net, cfg->rc_ifindex);
393 }
394
395 return dev;
396}
397
Eric W. Biedermana2519922015-03-03 19:12:40 -0600398static int mpls_route_add(struct mpls_route_config *cfg)
399{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600400 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600401 struct net *net = cfg->rc_nlinfo.nl_net;
402 struct net_device *dev = NULL;
403 struct mpls_route *rt, *old;
404 unsigned index;
405 int i;
406 int err = -EINVAL;
407
408 index = cfg->rc_label;
409
410 /* If a label was not specified during insert pick one */
411 if ((index == LABEL_NOT_SPECIFIED) &&
412 (cfg->rc_nlflags & NLM_F_CREATE)) {
413 index = find_free_label(net);
414 }
415
416 /* The first 16 labels are reserved, and may not be set */
417 if (index < 16)
418 goto errout;
419
420 /* The full 20 bit range may not be supported. */
421 if (index >= net->mpls.platform_labels)
422 goto errout;
423
424 /* Ensure only a supported number of labels are present */
425 if (cfg->rc_output_labels > MAX_NEW_LABELS)
426 goto errout;
427
428 err = -ENODEV;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700429 dev = find_outdev(net, cfg);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600430 if (!dev)
431 goto errout;
432
Robert Shearman03c57742015-04-22 11:14:37 +0100433 /* Ensure this is a supported device */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600434 err = -EINVAL;
Robert Shearman03c57742015-04-22 11:14:37 +0100435 if (!mpls_dev_get(dev))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600436 goto errout;
437
438 err = -EINVAL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600439 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
Eric W. Biedermana2519922015-03-03 19:12:40 -0600440 (dev->addr_len != cfg->rc_via_alen))
441 goto errout;
442
443 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600444 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600445 if (cfg->rc_nlflags & NLM_F_APPEND)
446 goto errout;
447
448 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600449 platform_label = rtnl_dereference(net->mpls.platform_label);
450 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600451 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
452 goto errout;
453
454 err = -EEXIST;
455 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
456 goto errout;
457
458 err = -ENOENT;
459 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
460 goto errout;
461
462 err = -ENOMEM;
463 rt = mpls_rt_alloc(cfg->rc_via_alen);
464 if (!rt)
465 goto errout;
466
467 rt->rt_labels = cfg->rc_output_labels;
468 for (i = 0; i < rt->rt_labels; i++)
469 rt->rt_label[i] = cfg->rc_output_label[i];
470 rt->rt_protocol = cfg->rc_protocol;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600471 RCU_INIT_POINTER(rt->rt_dev, dev);
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600472 rt->rt_via_table = cfg->rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600473 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
474
475 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
476
477 dev_put(dev);
478 return 0;
479
480errout:
481 if (dev)
482 dev_put(dev);
483 return err;
484}
485
486static int mpls_route_del(struct mpls_route_config *cfg)
487{
488 struct net *net = cfg->rc_nlinfo.nl_net;
489 unsigned index;
490 int err = -EINVAL;
491
492 index = cfg->rc_label;
493
494 /* The first 16 labels are reserved, and may not be removed */
495 if (index < 16)
496 goto errout;
497
498 /* The full 20 bit range may not be supported */
499 if (index >= net->mpls.platform_labels)
500 goto errout;
501
502 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
503
504 err = 0;
505errout:
506 return err;
507}
508
Robert Shearman37bde792015-04-22 11:14:38 +0100509#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
510 (&((struct mpls_dev *)0)->field)
511
512static const struct ctl_table mpls_dev_table[] = {
513 {
514 .procname = "input",
515 .maxlen = sizeof(int),
516 .mode = 0644,
517 .proc_handler = proc_dointvec,
518 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
519 },
520 { }
521};
522
523static int mpls_dev_sysctl_register(struct net_device *dev,
524 struct mpls_dev *mdev)
525{
526 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
527 struct ctl_table *table;
528 int i;
529
530 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
531 if (!table)
532 goto out;
533
534 /* Table data contains only offsets relative to the base of
535 * the mdev at this point, so make them absolute.
536 */
537 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
538 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
539
540 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
541
542 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
543 if (!mdev->sysctl)
544 goto free;
545
546 return 0;
547
548free:
549 kfree(table);
550out:
551 return -ENOBUFS;
552}
553
554static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
555{
556 struct ctl_table *table;
557
558 table = mdev->sysctl->ctl_table_arg;
559 unregister_net_sysctl_table(mdev->sysctl);
560 kfree(table);
561}
562
Robert Shearman03c57742015-04-22 11:14:37 +0100563static struct mpls_dev *mpls_add_dev(struct net_device *dev)
564{
565 struct mpls_dev *mdev;
566 int err = -ENOMEM;
567
568 ASSERT_RTNL();
569
570 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
571 if (!mdev)
572 return ERR_PTR(err);
573
Robert Shearman37bde792015-04-22 11:14:38 +0100574 err = mpls_dev_sysctl_register(dev, mdev);
575 if (err)
576 goto free;
577
Robert Shearman03c57742015-04-22 11:14:37 +0100578 rcu_assign_pointer(dev->mpls_ptr, mdev);
579
580 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100581
582free:
583 kfree(mdev);
584 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100585}
586
Eric W. Biederman01891972015-03-03 19:10:47 -0600587static void mpls_ifdown(struct net_device *dev)
588{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600589 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600590 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100591 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600592 unsigned index;
593
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600594 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600595 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600596 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600597 if (!rt)
598 continue;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600599 if (rtnl_dereference(rt->rt_dev) != dev)
Eric W. Biederman01891972015-03-03 19:10:47 -0600600 continue;
601 rt->rt_dev = NULL;
602 }
Robert Shearman03c57742015-04-22 11:14:37 +0100603
604 mdev = mpls_dev_get(dev);
605 if (!mdev)
606 return;
607
Robert Shearman37bde792015-04-22 11:14:38 +0100608 mpls_dev_sysctl_unregister(mdev);
609
Robert Shearman03c57742015-04-22 11:14:37 +0100610 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
611
Robert Shearman25cc8f02015-06-05 18:54:45 +0100612 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600613}
614
615static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
616 void *ptr)
617{
618 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100619 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600620
621 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100622 case NETDEV_REGISTER:
623 /* For now just support ethernet devices */
624 if ((dev->type == ARPHRD_ETHER) ||
625 (dev->type == ARPHRD_LOOPBACK)) {
626 mdev = mpls_add_dev(dev);
627 if (IS_ERR(mdev))
628 return notifier_from_errno(PTR_ERR(mdev));
629 }
630 break;
631
Eric W. Biederman01891972015-03-03 19:10:47 -0600632 case NETDEV_UNREGISTER:
633 mpls_ifdown(dev);
634 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100635 case NETDEV_CHANGENAME:
636 mdev = mpls_dev_get(dev);
637 if (mdev) {
638 int err;
639
640 mpls_dev_sysctl_unregister(mdev);
641 err = mpls_dev_sysctl_register(dev, mdev);
642 if (err)
643 return notifier_from_errno(err);
644 }
645 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600646 }
647 return NOTIFY_OK;
648}
649
650static struct notifier_block mpls_dev_notifier = {
651 .notifier_call = mpls_dev_notify,
652};
653
Eric W. Biederman03c05662015-03-03 19:13:56 -0600654static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600655 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600656{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600657 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
658 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
659 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600660 struct nlattr *nla;
661 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600662 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600663
664 nla = nla_reserve(skb, RTA_VIA, alen + 2);
665 if (!nla)
666 return -EMSGSIZE;
667
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600668 if (table <= NEIGH_NR_TABLES)
669 family = table_to_family[table];
670
Eric W. Biederman03c05662015-03-03 19:13:56 -0600671 via = nla_data(nla);
672 via->rtvia_family = family;
673 memcpy(via->rtvia_addr, addr, alen);
674 return 0;
675}
676
Eric W. Biederman966bae32015-03-03 19:13:19 -0600677int nla_put_labels(struct sk_buff *skb, int attrtype,
678 u8 labels, const u32 label[])
679{
680 struct nlattr *nla;
681 struct mpls_shim_hdr *nla_label;
682 bool bos;
683 int i;
684 nla = nla_reserve(skb, attrtype, labels*4);
685 if (!nla)
686 return -EMSGSIZE;
687
688 nla_label = nla_data(nla);
689 bos = true;
690 for (i = labels - 1; i >= 0; i--) {
691 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
692 bos = false;
693 }
694
695 return 0;
696}
Roopa Prabhuface0182015-07-21 10:43:52 +0200697EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600698
699int nla_get_labels(const struct nlattr *nla,
700 u32 max_labels, u32 *labels, u32 label[])
701{
702 unsigned len = nla_len(nla);
703 unsigned nla_labels;
704 struct mpls_shim_hdr *nla_label;
705 bool bos;
706 int i;
707
708 /* len needs to be an even multiple of 4 (the label size) */
709 if (len & 3)
710 return -EINVAL;
711
712 /* Limit the number of new labels allowed */
713 nla_labels = len/4;
714 if (nla_labels > max_labels)
715 return -EINVAL;
716
717 nla_label = nla_data(nla);
718 bos = true;
719 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
720 struct mpls_entry_decoded dec;
721 dec = mpls_entry_decode(nla_label + i);
722
723 /* Ensure the bottom of stack flag is properly set
724 * and ttl and tc are both clear.
725 */
726 if ((dec.bos != bos) || dec.ttl || dec.tc)
727 return -EINVAL;
728
Robert Shearman5a9ab012015-04-22 11:14:39 +0100729 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -0700730 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +0100731 /* RFC3032: This is a label that an LSR may
732 * assign and distribute, but which never
733 * actually appears in the encapsulation.
734 */
735 return -EINVAL;
736 }
737
Eric W. Biederman966bae32015-03-03 19:13:19 -0600738 label[i] = dec.label;
739 }
740 *labels = nla_labels;
741 return 0;
742}
Roopa Prabhuface0182015-07-21 10:43:52 +0200743EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600744
Eric W. Biederman03c05662015-03-03 19:13:56 -0600745static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
746 struct mpls_route_config *cfg)
747{
748 struct rtmsg *rtm;
749 struct nlattr *tb[RTA_MAX+1];
750 int index;
751 int err;
752
753 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
754 if (err < 0)
755 goto errout;
756
757 err = -EINVAL;
758 rtm = nlmsg_data(nlh);
759 memset(cfg, 0, sizeof(*cfg));
760
761 if (rtm->rtm_family != AF_MPLS)
762 goto errout;
763 if (rtm->rtm_dst_len != 20)
764 goto errout;
765 if (rtm->rtm_src_len != 0)
766 goto errout;
767 if (rtm->rtm_tos != 0)
768 goto errout;
769 if (rtm->rtm_table != RT_TABLE_MAIN)
770 goto errout;
771 /* Any value is acceptable for rtm_protocol */
772
773 /* As mpls uses destination specific addresses
774 * (or source specific address in the case of multicast)
775 * all addresses have universal scope.
776 */
777 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
778 goto errout;
779 if (rtm->rtm_type != RTN_UNICAST)
780 goto errout;
781 if (rtm->rtm_flags != 0)
782 goto errout;
783
784 cfg->rc_label = LABEL_NOT_SPECIFIED;
785 cfg->rc_protocol = rtm->rtm_protocol;
786 cfg->rc_nlflags = nlh->nlmsg_flags;
787 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
788 cfg->rc_nlinfo.nlh = nlh;
789 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
790
791 for (index = 0; index <= RTA_MAX; index++) {
792 struct nlattr *nla = tb[index];
793 if (!nla)
794 continue;
795
796 switch(index) {
797 case RTA_OIF:
798 cfg->rc_ifindex = nla_get_u32(nla);
799 break;
800 case RTA_NEWDST:
801 if (nla_get_labels(nla, MAX_NEW_LABELS,
802 &cfg->rc_output_labels,
803 cfg->rc_output_label))
804 goto errout;
805 break;
806 case RTA_DST:
807 {
808 u32 label_count;
809 if (nla_get_labels(nla, 1, &label_count,
810 &cfg->rc_label))
811 goto errout;
812
813 /* The first 16 labels are reserved, and may not be set */
814 if (cfg->rc_label < 16)
815 goto errout;
816
817 break;
818 }
819 case RTA_VIA:
820 {
821 struct rtvia *via = nla_data(nla);
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000822 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
823 goto errout;
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000824 cfg->rc_via_alen = nla_len(nla) -
825 offsetof(struct rtvia, rtvia_addr);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600826 if (cfg->rc_via_alen > MAX_VIA_ALEN)
827 goto errout;
828
829 /* Validate the address family */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600830 switch(via->rtvia_family) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600831 case AF_PACKET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600832 cfg->rc_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600833 break;
834 case AF_INET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600835 cfg->rc_via_table = NEIGH_ARP_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600836 if (cfg->rc_via_alen != 4)
837 goto errout;
838 break;
839 case AF_INET6:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600840 cfg->rc_via_table = NEIGH_ND_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600841 if (cfg->rc_via_alen != 16)
842 goto errout;
843 break;
844 default:
845 /* Unsupported address family */
846 goto errout;
847 }
848
849 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
850 break;
851 }
852 default:
853 /* Unsupported attribute */
854 goto errout;
855 }
856 }
857
858 err = 0;
859errout:
860 return err;
861}
862
863static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
864{
865 struct mpls_route_config cfg;
866 int err;
867
868 err = rtm_to_route_config(skb, nlh, &cfg);
869 if (err < 0)
870 return err;
871
872 return mpls_route_del(&cfg);
873}
874
875
876static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
877{
878 struct mpls_route_config cfg;
879 int err;
880
881 err = rtm_to_route_config(skb, nlh, &cfg);
882 if (err < 0)
883 return err;
884
885 return mpls_route_add(&cfg);
886}
887
888static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
889 u32 label, struct mpls_route *rt, int flags)
890{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600891 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600892 struct nlmsghdr *nlh;
893 struct rtmsg *rtm;
894
895 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
896 if (nlh == NULL)
897 return -EMSGSIZE;
898
899 rtm = nlmsg_data(nlh);
900 rtm->rtm_family = AF_MPLS;
901 rtm->rtm_dst_len = 20;
902 rtm->rtm_src_len = 0;
903 rtm->rtm_tos = 0;
904 rtm->rtm_table = RT_TABLE_MAIN;
905 rtm->rtm_protocol = rt->rt_protocol;
906 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
907 rtm->rtm_type = RTN_UNICAST;
908 rtm->rtm_flags = 0;
909
910 if (rt->rt_labels &&
911 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
912 goto nla_put_failure;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600913 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600914 goto nla_put_failure;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600915 dev = rtnl_dereference(rt->rt_dev);
916 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600917 goto nla_put_failure;
918 if (nla_put_labels(skb, RTA_DST, 1, &label))
919 goto nla_put_failure;
920
921 nlmsg_end(skb, nlh);
922 return 0;
923
924nla_put_failure:
925 nlmsg_cancel(skb, nlh);
926 return -EMSGSIZE;
927}
928
929static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
930{
931 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600932 struct mpls_route __rcu **platform_label;
933 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600934 unsigned int index;
935
936 ASSERT_RTNL();
937
938 index = cb->args[0];
939 if (index < 16)
940 index = 16;
941
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600942 platform_label = rtnl_dereference(net->mpls.platform_label);
943 platform_labels = net->mpls.platform_labels;
944 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600945 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600946 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600947 if (!rt)
948 continue;
949
950 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
951 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
952 index, rt, NLM_F_MULTI) < 0)
953 break;
954 }
955 cb->args[0] = index;
956
957 return skb->len;
958}
959
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600960static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
961{
962 size_t payload =
963 NLMSG_ALIGN(sizeof(struct rtmsg))
964 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
965 + nla_total_size(4); /* RTA_DST */
966 if (rt->rt_labels) /* RTA_NEWDST */
967 payload += nla_total_size(rt->rt_labels * 4);
968 if (rt->rt_dev) /* RTA_OIF */
969 payload += nla_total_size(4);
970 return payload;
971}
972
973static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
974 struct nlmsghdr *nlh, struct net *net, u32 portid,
975 unsigned int nlm_flags)
976{
977 struct sk_buff *skb;
978 u32 seq = nlh ? nlh->nlmsg_seq : 0;
979 int err = -ENOBUFS;
980
981 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
982 if (skb == NULL)
983 goto errout;
984
985 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
986 if (err < 0) {
987 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
988 WARN_ON(err == -EMSGSIZE);
989 kfree_skb(skb);
990 goto errout;
991 }
992 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
993
994 return;
995errout:
996 if (err < 0)
997 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
998}
999
Eric W. Biederman7720c012015-03-03 19:11:20 -06001000static int resize_platform_label_table(struct net *net, size_t limit)
1001{
1002 size_t size = sizeof(struct mpls_route *) * limit;
1003 size_t old_limit;
1004 size_t cp_size;
1005 struct mpls_route __rcu **labels = NULL, **old;
1006 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1007 unsigned index;
1008
1009 if (size) {
1010 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1011 if (!labels)
1012 labels = vzalloc(size);
1013
1014 if (!labels)
1015 goto nolabels;
1016 }
1017
1018 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001019 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001020 struct net_device *lo = net->loopback_dev;
1021 rt0 = mpls_rt_alloc(lo->addr_len);
1022 if (!rt0)
1023 goto nort0;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001024 RCU_INIT_POINTER(rt0->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001025 rt0->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001026 rt0->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001027 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1028 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001029 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001030 struct net_device *lo = net->loopback_dev;
1031 rt2 = mpls_rt_alloc(lo->addr_len);
1032 if (!rt2)
1033 goto nort2;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001034 RCU_INIT_POINTER(rt2->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001035 rt2->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001036 rt2->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001037 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1038 }
1039
1040 rtnl_lock();
1041 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001042 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001043 old_limit = net->mpls.platform_labels;
1044
1045 /* Free any labels beyond the new table */
1046 for (index = limit; index < old_limit; index++)
1047 mpls_route_update(net, index, NULL, NULL, NULL);
1048
1049 /* Copy over the old labels */
1050 cp_size = size;
1051 if (old_limit < limit)
1052 cp_size = old_limit * sizeof(struct mpls_route *);
1053
1054 memcpy(labels, old, cp_size);
1055
1056 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001057 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1058 (limit > MPLS_LABEL_IPV6NULL)) {
1059 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001060 rt2 = NULL;
1061 }
1062
Tom Herbert78f5b892015-05-07 08:08:51 -07001063 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1064 (limit > MPLS_LABEL_IPV4NULL)) {
1065 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001066 rt0 = NULL;
1067 }
1068
1069 /* Update the global pointers */
1070 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001071 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001072
1073 rtnl_unlock();
1074
1075 mpls_rt_free(rt2);
1076 mpls_rt_free(rt0);
1077
1078 if (old) {
1079 synchronize_rcu();
1080 kvfree(old);
1081 }
1082 return 0;
1083
1084nort2:
1085 mpls_rt_free(rt0);
1086nort0:
1087 kvfree(labels);
1088nolabels:
1089 return -ENOMEM;
1090}
1091
1092static int mpls_platform_labels(struct ctl_table *table, int write,
1093 void __user *buffer, size_t *lenp, loff_t *ppos)
1094{
1095 struct net *net = table->data;
1096 int platform_labels = net->mpls.platform_labels;
1097 int ret;
1098 struct ctl_table tmp = {
1099 .procname = table->procname,
1100 .data = &platform_labels,
1101 .maxlen = sizeof(int),
1102 .mode = table->mode,
1103 .extra1 = &zero,
1104 .extra2 = &label_limit,
1105 };
1106
1107 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1108
1109 if (write && ret == 0)
1110 ret = resize_platform_label_table(net, platform_labels);
1111
1112 return ret;
1113}
1114
Robert Shearman37bde792015-04-22 11:14:38 +01001115static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001116 {
1117 .procname = "platform_labels",
1118 .data = NULL,
1119 .maxlen = sizeof(int),
1120 .mode = 0644,
1121 .proc_handler = mpls_platform_labels,
1122 },
1123 { }
1124};
1125
Eric W. Biederman01891972015-03-03 19:10:47 -06001126static int mpls_net_init(struct net *net)
1127{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001128 struct ctl_table *table;
1129
Eric W. Biederman01891972015-03-03 19:10:47 -06001130 net->mpls.platform_labels = 0;
1131 net->mpls.platform_label = NULL;
1132
Eric W. Biederman7720c012015-03-03 19:11:20 -06001133 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1134 if (table == NULL)
1135 return -ENOMEM;
1136
1137 table[0].data = net;
1138 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1139 if (net->mpls.ctl == NULL)
1140 return -ENOMEM;
1141
Eric W. Biederman01891972015-03-03 19:10:47 -06001142 return 0;
1143}
1144
1145static void mpls_net_exit(struct net *net)
1146{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001147 struct mpls_route __rcu **platform_label;
1148 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001149 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001150 unsigned int index;
1151
Eric W. Biederman7720c012015-03-03 19:11:20 -06001152 table = net->mpls.ctl->ctl_table_arg;
1153 unregister_net_sysctl_table(net->mpls.ctl);
1154 kfree(table);
1155
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001156 /* An rcu grace period has passed since there was a device in
1157 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001158 * left this network namespace. This is because
1159 * unregister_netdevice_many and netdev_run_todo has completed
1160 * for each network device that was in this network namespace.
1161 *
1162 * As such no additional rcu synchronization is necessary when
1163 * freeing the platform_label table.
1164 */
1165 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001166 platform_label = rtnl_dereference(net->mpls.platform_label);
1167 platform_labels = net->mpls.platform_labels;
1168 for (index = 0; index < platform_labels; index++) {
1169 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1170 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001171 mpls_rt_free(rt);
1172 }
1173 rtnl_unlock();
1174
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001175 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001176}
1177
1178static struct pernet_operations mpls_net_ops = {
1179 .init = mpls_net_init,
1180 .exit = mpls_net_exit,
1181};
1182
1183static int __init mpls_init(void)
1184{
1185 int err;
1186
1187 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1188
1189 err = register_pernet_subsys(&mpls_net_ops);
1190 if (err)
1191 goto out;
1192
1193 err = register_netdevice_notifier(&mpls_dev_notifier);
1194 if (err)
1195 goto out_unregister_pernet;
1196
1197 dev_add_pack(&mpls_packet_type);
1198
Eric W. Biederman03c05662015-03-03 19:13:56 -06001199 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1200 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1201 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001202 err = 0;
1203out:
1204 return err;
1205
1206out_unregister_pernet:
1207 unregister_pernet_subsys(&mpls_net_ops);
1208 goto out;
1209}
1210module_init(mpls_init);
1211
1212static void __exit mpls_exit(void)
1213{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001214 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001215 dev_remove_pack(&mpls_packet_type);
1216 unregister_netdevice_notifier(&mpls_dev_notifier);
1217 unregister_pernet_subsys(&mpls_net_ops);
1218}
1219module_exit(mpls_exit);
1220
1221MODULE_DESCRIPTION("MultiProtocol Label Switching");
1222MODULE_LICENSE("GPL v2");
1223MODULE_ALIAS_NETPROTO(PF_MPLS);