blob: d93c0301ad237bacd9520490d5739c32f2da5457 [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 Prabhubf215632015-07-30 13:34:54 -070018#if IS_ENABLED(CONFIG_IPV6)
19#include <net/ipv6.h>
20#include <net/addrconf.h>
21#endif
Eric W. Biederman01891972015-03-03 19:10:47 -060022#include "internal.h"
23
Eric W. Biedermana2519922015-03-03 19:12:40 -060024#define LABEL_NOT_SPECIFIED (1<<20)
Eric W. Biederman01891972015-03-03 19:10:47 -060025#define MAX_NEW_LABELS 2
26
27/* This maximum ha length copied from the definition of struct neighbour */
28#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
29
30struct mpls_route { /* next hop label forwarding entry */
Eric W. Biederman19d0c342015-03-07 16:21:56 -060031 struct net_device __rcu *rt_dev;
Eric W. Biederman01891972015-03-03 19:10:47 -060032 struct rcu_head rt_rcu;
33 u32 rt_label[MAX_NEW_LABELS];
34 u8 rt_protocol; /* routing protocol that set this entry */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -060035 u8 rt_labels;
36 u8 rt_via_alen;
37 u8 rt_via_table;
Eric W. Biederman01891972015-03-03 19:10:47 -060038 u8 rt_via[0];
39};
40
Eric W. Biederman7720c012015-03-03 19:11:20 -060041static int zero = 0;
42static int label_limit = (1 << 20) - 1;
43
Eric W. Biederman8de147d2015-03-03 19:14:31 -060044static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
45 struct nlmsghdr *nlh, struct net *net, u32 portid,
46 unsigned int nlm_flags);
47
Eric W. Biederman01891972015-03-03 19:10:47 -060048static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
49{
50 struct mpls_route *rt = NULL;
51
52 if (index < net->mpls.platform_labels) {
53 struct mpls_route __rcu **platform_label =
54 rcu_dereference(net->mpls.platform_label);
55 rt = rcu_dereference(platform_label[index]);
56 }
57 return rt;
58}
59
Robert Shearman03c57742015-04-22 11:14:37 +010060static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
61{
62 return rcu_dereference_rtnl(dev->mpls_ptr);
63}
64
Roopa Prabhuface0182015-07-21 10:43:52 +020065bool mpls_output_possible(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060066{
67 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
68}
Roopa Prabhuface0182015-07-21 10:43:52 +020069EXPORT_SYMBOL_GPL(mpls_output_possible);
Eric W. Biederman01891972015-03-03 19:10:47 -060070
71static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
72{
73 /* The size of the layer 2.5 labels to be added for this route */
74 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
75}
76
Roopa Prabhuface0182015-07-21 10:43:52 +020077unsigned int mpls_dev_mtu(const struct net_device *dev)
Eric W. Biederman01891972015-03-03 19:10:47 -060078{
79 /* The amount of data the layer 2 frame can hold */
80 return dev->mtu;
81}
Roopa Prabhuface0182015-07-21 10:43:52 +020082EXPORT_SYMBOL_GPL(mpls_dev_mtu);
Eric W. Biederman01891972015-03-03 19:10:47 -060083
Roopa Prabhuface0182015-07-21 10:43:52 +020084bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
Eric W. Biederman01891972015-03-03 19:10:47 -060085{
86 if (skb->len <= mtu)
87 return false;
88
89 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
90 return false;
91
92 return true;
93}
Roopa Prabhuface0182015-07-21 10:43:52 +020094EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
Eric W. Biederman01891972015-03-03 19:10:47 -060095
96static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
97 struct mpls_entry_decoded dec)
98{
99 /* RFC4385 and RFC5586 encode other packets in mpls such that
100 * they don't conflict with the ip version number, making
101 * decoding by examining the ip version correct in everything
102 * except for the strangest cases.
103 *
104 * The strange cases if we choose to support them will require
105 * manual configuration.
106 */
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500107 struct iphdr *hdr4;
Eric W. Biederman01891972015-03-03 19:10:47 -0600108 bool success = true;
109
Eric W. Biederman76fecd82015-03-12 18:22:59 -0500110 /* The IPv4 code below accesses through the IPv4 header
111 * checksum, which is 12 bytes into the packet.
112 * The IPv6 code below accesses through the IPv6 hop limit
113 * which is 8 bytes into the packet.
114 *
115 * For all supported cases there should always be at least 12
116 * bytes of packet data present. The IPv4 header is 20 bytes
117 * without options and the IPv6 header is always 40 bytes
118 * long.
119 */
120 if (!pskb_may_pull(skb, 12))
121 return false;
122
123 /* Use ip_hdr to find the ip protocol version */
124 hdr4 = ip_hdr(skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600125 if (hdr4->version == 4) {
126 skb->protocol = htons(ETH_P_IP);
127 csum_replace2(&hdr4->check,
128 htons(hdr4->ttl << 8),
129 htons(dec.ttl << 8));
130 hdr4->ttl = dec.ttl;
131 }
132 else if (hdr4->version == 6) {
133 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
134 skb->protocol = htons(ETH_P_IPV6);
135 hdr6->hop_limit = dec.ttl;
136 }
137 else
138 /* version 0 and version 1 are used by pseudo wires */
139 success = false;
140 return success;
141}
142
143static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
144 struct packet_type *pt, struct net_device *orig_dev)
145{
146 struct net *net = dev_net(dev);
147 struct mpls_shim_hdr *hdr;
148 struct mpls_route *rt;
149 struct mpls_entry_decoded dec;
150 struct net_device *out_dev;
Robert Shearman03c57742015-04-22 11:14:37 +0100151 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600152 unsigned int hh_len;
153 unsigned int new_header_size;
154 unsigned int mtu;
155 int err;
156
157 /* Careful this entire function runs inside of an rcu critical section */
158
Robert Shearman03c57742015-04-22 11:14:37 +0100159 mdev = mpls_dev_get(dev);
Robert Shearman37bde792015-04-22 11:14:38 +0100160 if (!mdev || !mdev->input_enabled)
Robert Shearman03c57742015-04-22 11:14:37 +0100161 goto drop;
162
Eric W. Biederman01891972015-03-03 19:10:47 -0600163 if (skb->pkt_type != PACKET_HOST)
164 goto drop;
165
166 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
167 goto drop;
168
169 if (!pskb_may_pull(skb, sizeof(*hdr)))
170 goto drop;
171
172 /* Read and decode the label */
173 hdr = mpls_hdr(skb);
174 dec = mpls_entry_decode(hdr);
175
176 /* Pop the label */
177 skb_pull(skb, sizeof(*hdr));
178 skb_reset_network_header(skb);
179
180 skb_orphan(skb);
181
182 rt = mpls_route_input_rcu(net, dec.label);
183 if (!rt)
184 goto drop;
185
186 /* Find the output device */
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600187 out_dev = rcu_dereference(rt->rt_dev);
Eric W. Biederman01891972015-03-03 19:10:47 -0600188 if (!mpls_output_possible(out_dev))
189 goto drop;
190
191 if (skb_warn_if_lro(skb))
192 goto drop;
193
194 skb_forward_csum(skb);
195
196 /* Verify ttl is valid */
Eric W. Biedermanaa7da932015-03-07 16:23:23 -0600197 if (dec.ttl <= 1)
Eric W. Biederman01891972015-03-03 19:10:47 -0600198 goto drop;
199 dec.ttl -= 1;
200
201 /* Verify the destination can hold the packet */
202 new_header_size = mpls_rt_header_size(rt);
203 mtu = mpls_dev_mtu(out_dev);
204 if (mpls_pkt_too_big(skb, mtu - new_header_size))
205 goto drop;
206
207 hh_len = LL_RESERVED_SPACE(out_dev);
208 if (!out_dev->header_ops)
209 hh_len = 0;
210
211 /* Ensure there is enough space for the headers in the skb */
212 if (skb_cow(skb, hh_len + new_header_size))
213 goto drop;
214
215 skb->dev = out_dev;
216 skb->protocol = htons(ETH_P_MPLS_UC);
217
218 if (unlikely(!new_header_size && dec.bos)) {
219 /* Penultimate hop popping */
220 if (!mpls_egress(rt, skb, dec))
221 goto drop;
222 } else {
223 bool bos;
224 int i;
225 skb_push(skb, new_header_size);
226 skb_reset_network_header(skb);
227 /* Push the new labels */
228 hdr = mpls_hdr(skb);
229 bos = dec.bos;
230 for (i = rt->rt_labels - 1; i >= 0; i--) {
231 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
232 bos = false;
233 }
234 }
235
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600236 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
Eric W. Biederman01891972015-03-03 19:10:47 -0600237 if (err)
238 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
239 __func__, err);
240 return 0;
241
242drop:
243 kfree_skb(skb);
244 return NET_RX_DROP;
245}
246
247static struct packet_type mpls_packet_type __read_mostly = {
248 .type = cpu_to_be16(ETH_P_MPLS_UC),
249 .func = mpls_forward,
250};
251
Wu Fengguangf0126532015-03-05 05:33:54 +0800252static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600253 [RTA_DST] = { .type = NLA_U32 },
254 [RTA_OIF] = { .type = NLA_U32 },
255};
256
Eric W. Biedermana2519922015-03-03 19:12:40 -0600257struct mpls_route_config {
258 u32 rc_protocol;
259 u32 rc_ifindex;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600260 u16 rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600261 u16 rc_via_alen;
262 u8 rc_via[MAX_VIA_ALEN];
263 u32 rc_label;
264 u32 rc_output_labels;
265 u32 rc_output_label[MAX_NEW_LABELS];
266 u32 rc_nlflags;
267 struct nl_info rc_nlinfo;
268};
269
Eric W. Biederman01891972015-03-03 19:10:47 -0600270static struct mpls_route *mpls_rt_alloc(size_t alen)
271{
272 struct mpls_route *rt;
273
Eric W. Biedermand8656162015-03-07 16:19:41 -0600274 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
Eric W. Biederman01891972015-03-03 19:10:47 -0600275 if (rt)
276 rt->rt_via_alen = alen;
277 return rt;
278}
279
280static void mpls_rt_free(struct mpls_route *rt)
281{
282 if (rt)
283 kfree_rcu(rt, rt_rcu);
284}
285
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600286static void mpls_notify_route(struct net *net, unsigned index,
287 struct mpls_route *old, struct mpls_route *new,
288 const struct nl_info *info)
289{
290 struct nlmsghdr *nlh = info ? info->nlh : NULL;
291 unsigned portid = info ? info->portid : 0;
292 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
293 struct mpls_route *rt = new ? new : old;
294 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
295 /* Ignore reserved labels for now */
Robert Shearmana6affd22015-08-03 17:50:04 +0100296 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600297 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
298}
299
Eric W. Biederman01891972015-03-03 19:10:47 -0600300static void mpls_route_update(struct net *net, unsigned index,
301 struct net_device *dev, struct mpls_route *new,
302 const struct nl_info *info)
303{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600304 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600305 struct mpls_route *rt, *old = NULL;
306
307 ASSERT_RTNL();
308
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600309 platform_label = rtnl_dereference(net->mpls.platform_label);
310 rt = rtnl_dereference(platform_label[index]);
311 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
312 rcu_assign_pointer(platform_label[index], new);
Eric W. Biederman01891972015-03-03 19:10:47 -0600313 old = rt;
314 }
315
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600316 mpls_notify_route(net, index, old, new, info);
317
Eric W. Biederman01891972015-03-03 19:10:47 -0600318 /* If we removed a route free it now */
319 mpls_rt_free(old);
320}
321
Eric W. Biedermana2519922015-03-03 19:12:40 -0600322static unsigned find_free_label(struct net *net)
323{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600324 struct mpls_route __rcu **platform_label;
325 size_t platform_labels;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600326 unsigned index;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600327
328 platform_label = rtnl_dereference(net->mpls.platform_label);
329 platform_labels = net->mpls.platform_labels;
Robert Shearmana6affd22015-08-03 17:50:04 +0100330 for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
331 index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600332 if (!rtnl_dereference(platform_label[index]))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600333 return index;
334 }
335 return LABEL_NOT_SPECIFIED;
336}
337
Roopa Prabhubf215632015-07-30 13:34:54 -0700338#if IS_ENABLED(CONFIG_INET)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700339static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
340{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300341 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700342 struct rtable *rt;
343 struct in_addr daddr;
344
345 memcpy(&daddr, addr, sizeof(struct in_addr));
346 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
347 if (IS_ERR(rt))
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300348 return ERR_CAST(rt);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700349
350 dev = rt->dst.dev;
351 dev_hold(dev);
352
353 ip_rt_put(rt);
354
Roopa Prabhu01faef22015-07-21 09:16:24 -0700355 return dev;
356}
Roopa Prabhubf215632015-07-30 13:34:54 -0700357#else
358static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
359{
360 return ERR_PTR(-EAFNOSUPPORT);
361}
362#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700363
Roopa Prabhubf215632015-07-30 13:34:54 -0700364#if IS_ENABLED(CONFIG_IPV6)
Roopa Prabhu01faef22015-07-21 09:16:24 -0700365static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
366{
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300367 struct net_device *dev;
Roopa Prabhu01faef22015-07-21 09:16:24 -0700368 struct dst_entry *dst;
369 struct flowi6 fl6;
Roopa Prabhubf215632015-07-30 13:34:54 -0700370 int err;
371
372 if (!ipv6_stub)
373 return ERR_PTR(-EAFNOSUPPORT);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700374
375 memset(&fl6, 0, sizeof(fl6));
376 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
Roopa Prabhubf215632015-07-30 13:34:54 -0700377 err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
378 if (err)
Dan Carpenter5a9348b2015-08-04 10:44:22 +0300379 return ERR_PTR(err);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700380
381 dev = dst->dev;
382 dev_hold(dev);
Roopa Prabhu01faef22015-07-21 09:16:24 -0700383 dst_release(dst);
384
385 return dev;
386}
Roopa Prabhubf215632015-07-30 13:34:54 -0700387#else
388static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
389{
390 return ERR_PTR(-EAFNOSUPPORT);
391}
392#endif
Roopa Prabhu01faef22015-07-21 09:16:24 -0700393
394static struct net_device *find_outdev(struct net *net,
395 struct mpls_route_config *cfg)
396{
397 struct net_device *dev = NULL;
398
399 if (!cfg->rc_ifindex) {
400 switch (cfg->rc_via_table) {
401 case NEIGH_ARP_TABLE:
402 dev = inet_fib_lookup_dev(net, cfg->rc_via);
403 break;
404 case NEIGH_ND_TABLE:
405 dev = inet6_fib_lookup_dev(net, cfg->rc_via);
406 break;
407 case NEIGH_LINK_TABLE:
408 break;
409 }
410 } else {
411 dev = dev_get_by_index(net, cfg->rc_ifindex);
412 }
413
414 return dev;
415}
416
Eric W. Biedermana2519922015-03-03 19:12:40 -0600417static int mpls_route_add(struct mpls_route_config *cfg)
418{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600419 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600420 struct net *net = cfg->rc_nlinfo.nl_net;
421 struct net_device *dev = NULL;
422 struct mpls_route *rt, *old;
423 unsigned index;
424 int i;
425 int err = -EINVAL;
426
427 index = cfg->rc_label;
428
429 /* If a label was not specified during insert pick one */
430 if ((index == LABEL_NOT_SPECIFIED) &&
431 (cfg->rc_nlflags & NLM_F_CREATE)) {
432 index = find_free_label(net);
433 }
434
Robert Shearmana6affd22015-08-03 17:50:04 +0100435 /* Reserved labels may not be set */
436 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600437 goto errout;
438
439 /* The full 20 bit range may not be supported. */
440 if (index >= net->mpls.platform_labels)
441 goto errout;
442
443 /* Ensure only a supported number of labels are present */
444 if (cfg->rc_output_labels > MAX_NEW_LABELS)
445 goto errout;
446
Roopa Prabhu01faef22015-07-21 09:16:24 -0700447 dev = find_outdev(net, cfg);
Roopa Prabhubf215632015-07-30 13:34:54 -0700448 if (IS_ERR(dev)) {
449 err = PTR_ERR(dev);
450 dev = NULL;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600451 goto errout;
Roopa Prabhubf215632015-07-30 13:34:54 -0700452 }
Eric W. Biedermana2519922015-03-03 19:12:40 -0600453
Robert Shearman03c57742015-04-22 11:14:37 +0100454 /* Ensure this is a supported device */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600455 err = -EINVAL;
Robert Shearman03c57742015-04-22 11:14:37 +0100456 if (!mpls_dev_get(dev))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600457 goto errout;
458
459 err = -EINVAL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600460 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
Eric W. Biedermana2519922015-03-03 19:12:40 -0600461 (dev->addr_len != cfg->rc_via_alen))
462 goto errout;
463
464 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600465 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600466 if (cfg->rc_nlflags & NLM_F_APPEND)
467 goto errout;
468
469 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600470 platform_label = rtnl_dereference(net->mpls.platform_label);
471 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600472 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
473 goto errout;
474
475 err = -EEXIST;
476 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
477 goto errout;
478
479 err = -ENOENT;
480 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
481 goto errout;
482
483 err = -ENOMEM;
484 rt = mpls_rt_alloc(cfg->rc_via_alen);
485 if (!rt)
486 goto errout;
487
488 rt->rt_labels = cfg->rc_output_labels;
489 for (i = 0; i < rt->rt_labels; i++)
490 rt->rt_label[i] = cfg->rc_output_label[i];
491 rt->rt_protocol = cfg->rc_protocol;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600492 RCU_INIT_POINTER(rt->rt_dev, dev);
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600493 rt->rt_via_table = cfg->rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600494 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
495
496 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
497
498 dev_put(dev);
499 return 0;
500
501errout:
502 if (dev)
503 dev_put(dev);
504 return err;
505}
506
507static int mpls_route_del(struct mpls_route_config *cfg)
508{
509 struct net *net = cfg->rc_nlinfo.nl_net;
510 unsigned index;
511 int err = -EINVAL;
512
513 index = cfg->rc_label;
514
Robert Shearmana6affd22015-08-03 17:50:04 +0100515 /* Reserved labels may not be removed */
516 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600517 goto errout;
518
519 /* The full 20 bit range may not be supported */
520 if (index >= net->mpls.platform_labels)
521 goto errout;
522
523 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
524
525 err = 0;
526errout:
527 return err;
528}
529
Robert Shearman37bde792015-04-22 11:14:38 +0100530#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
531 (&((struct mpls_dev *)0)->field)
532
533static const struct ctl_table mpls_dev_table[] = {
534 {
535 .procname = "input",
536 .maxlen = sizeof(int),
537 .mode = 0644,
538 .proc_handler = proc_dointvec,
539 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
540 },
541 { }
542};
543
544static int mpls_dev_sysctl_register(struct net_device *dev,
545 struct mpls_dev *mdev)
546{
547 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
548 struct ctl_table *table;
549 int i;
550
551 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
552 if (!table)
553 goto out;
554
555 /* Table data contains only offsets relative to the base of
556 * the mdev at this point, so make them absolute.
557 */
558 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
559 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
560
561 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
562
563 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
564 if (!mdev->sysctl)
565 goto free;
566
567 return 0;
568
569free:
570 kfree(table);
571out:
572 return -ENOBUFS;
573}
574
575static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
576{
577 struct ctl_table *table;
578
579 table = mdev->sysctl->ctl_table_arg;
580 unregister_net_sysctl_table(mdev->sysctl);
581 kfree(table);
582}
583
Robert Shearman03c57742015-04-22 11:14:37 +0100584static struct mpls_dev *mpls_add_dev(struct net_device *dev)
585{
586 struct mpls_dev *mdev;
587 int err = -ENOMEM;
588
589 ASSERT_RTNL();
590
591 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
592 if (!mdev)
593 return ERR_PTR(err);
594
Robert Shearman37bde792015-04-22 11:14:38 +0100595 err = mpls_dev_sysctl_register(dev, mdev);
596 if (err)
597 goto free;
598
Robert Shearman03c57742015-04-22 11:14:37 +0100599 rcu_assign_pointer(dev->mpls_ptr, mdev);
600
601 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100602
603free:
604 kfree(mdev);
605 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100606}
607
Eric W. Biederman01891972015-03-03 19:10:47 -0600608static void mpls_ifdown(struct net_device *dev)
609{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600610 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600611 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100612 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600613 unsigned index;
614
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600615 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600616 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600617 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600618 if (!rt)
619 continue;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600620 if (rtnl_dereference(rt->rt_dev) != dev)
Eric W. Biederman01891972015-03-03 19:10:47 -0600621 continue;
622 rt->rt_dev = NULL;
623 }
Robert Shearman03c57742015-04-22 11:14:37 +0100624
625 mdev = mpls_dev_get(dev);
626 if (!mdev)
627 return;
628
Robert Shearman37bde792015-04-22 11:14:38 +0100629 mpls_dev_sysctl_unregister(mdev);
630
Robert Shearman03c57742015-04-22 11:14:37 +0100631 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
632
Robert Shearman25cc8f02015-06-05 18:54:45 +0100633 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600634}
635
636static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
637 void *ptr)
638{
639 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100640 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600641
642 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100643 case NETDEV_REGISTER:
644 /* For now just support ethernet devices */
645 if ((dev->type == ARPHRD_ETHER) ||
646 (dev->type == ARPHRD_LOOPBACK)) {
647 mdev = mpls_add_dev(dev);
648 if (IS_ERR(mdev))
649 return notifier_from_errno(PTR_ERR(mdev));
650 }
651 break;
652
Eric W. Biederman01891972015-03-03 19:10:47 -0600653 case NETDEV_UNREGISTER:
654 mpls_ifdown(dev);
655 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100656 case NETDEV_CHANGENAME:
657 mdev = mpls_dev_get(dev);
658 if (mdev) {
659 int err;
660
661 mpls_dev_sysctl_unregister(mdev);
662 err = mpls_dev_sysctl_register(dev, mdev);
663 if (err)
664 return notifier_from_errno(err);
665 }
666 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600667 }
668 return NOTIFY_OK;
669}
670
671static struct notifier_block mpls_dev_notifier = {
672 .notifier_call = mpls_dev_notify,
673};
674
Eric W. Biederman03c05662015-03-03 19:13:56 -0600675static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600676 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600677{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600678 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
679 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
680 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600681 struct nlattr *nla;
682 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600683 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600684
685 nla = nla_reserve(skb, RTA_VIA, alen + 2);
686 if (!nla)
687 return -EMSGSIZE;
688
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600689 if (table <= NEIGH_NR_TABLES)
690 family = table_to_family[table];
691
Eric W. Biederman03c05662015-03-03 19:13:56 -0600692 via = nla_data(nla);
693 via->rtvia_family = family;
694 memcpy(via->rtvia_addr, addr, alen);
695 return 0;
696}
697
Eric W. Biederman966bae32015-03-03 19:13:19 -0600698int nla_put_labels(struct sk_buff *skb, int attrtype,
699 u8 labels, const u32 label[])
700{
701 struct nlattr *nla;
702 struct mpls_shim_hdr *nla_label;
703 bool bos;
704 int i;
705 nla = nla_reserve(skb, attrtype, labels*4);
706 if (!nla)
707 return -EMSGSIZE;
708
709 nla_label = nla_data(nla);
710 bos = true;
711 for (i = labels - 1; i >= 0; i--) {
712 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
713 bos = false;
714 }
715
716 return 0;
717}
Roopa Prabhuface0182015-07-21 10:43:52 +0200718EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600719
720int nla_get_labels(const struct nlattr *nla,
721 u32 max_labels, u32 *labels, u32 label[])
722{
723 unsigned len = nla_len(nla);
724 unsigned nla_labels;
725 struct mpls_shim_hdr *nla_label;
726 bool bos;
727 int i;
728
729 /* len needs to be an even multiple of 4 (the label size) */
730 if (len & 3)
731 return -EINVAL;
732
733 /* Limit the number of new labels allowed */
734 nla_labels = len/4;
735 if (nla_labels > max_labels)
736 return -EINVAL;
737
738 nla_label = nla_data(nla);
739 bos = true;
740 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
741 struct mpls_entry_decoded dec;
742 dec = mpls_entry_decode(nla_label + i);
743
744 /* Ensure the bottom of stack flag is properly set
745 * and ttl and tc are both clear.
746 */
747 if ((dec.bos != bos) || dec.ttl || dec.tc)
748 return -EINVAL;
749
Robert Shearman5a9ab012015-04-22 11:14:39 +0100750 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -0700751 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +0100752 /* RFC3032: This is a label that an LSR may
753 * assign and distribute, but which never
754 * actually appears in the encapsulation.
755 */
756 return -EINVAL;
757 }
758
Eric W. Biederman966bae32015-03-03 19:13:19 -0600759 label[i] = dec.label;
760 }
761 *labels = nla_labels;
762 return 0;
763}
Roopa Prabhuface0182015-07-21 10:43:52 +0200764EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600765
Eric W. Biederman03c05662015-03-03 19:13:56 -0600766static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
767 struct mpls_route_config *cfg)
768{
769 struct rtmsg *rtm;
770 struct nlattr *tb[RTA_MAX+1];
771 int index;
772 int err;
773
774 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
775 if (err < 0)
776 goto errout;
777
778 err = -EINVAL;
779 rtm = nlmsg_data(nlh);
780 memset(cfg, 0, sizeof(*cfg));
781
782 if (rtm->rtm_family != AF_MPLS)
783 goto errout;
784 if (rtm->rtm_dst_len != 20)
785 goto errout;
786 if (rtm->rtm_src_len != 0)
787 goto errout;
788 if (rtm->rtm_tos != 0)
789 goto errout;
790 if (rtm->rtm_table != RT_TABLE_MAIN)
791 goto errout;
792 /* Any value is acceptable for rtm_protocol */
793
794 /* As mpls uses destination specific addresses
795 * (or source specific address in the case of multicast)
796 * all addresses have universal scope.
797 */
798 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
799 goto errout;
800 if (rtm->rtm_type != RTN_UNICAST)
801 goto errout;
802 if (rtm->rtm_flags != 0)
803 goto errout;
804
805 cfg->rc_label = LABEL_NOT_SPECIFIED;
806 cfg->rc_protocol = rtm->rtm_protocol;
807 cfg->rc_nlflags = nlh->nlmsg_flags;
808 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
809 cfg->rc_nlinfo.nlh = nlh;
810 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
811
812 for (index = 0; index <= RTA_MAX; index++) {
813 struct nlattr *nla = tb[index];
814 if (!nla)
815 continue;
816
817 switch(index) {
818 case RTA_OIF:
819 cfg->rc_ifindex = nla_get_u32(nla);
820 break;
821 case RTA_NEWDST:
822 if (nla_get_labels(nla, MAX_NEW_LABELS,
823 &cfg->rc_output_labels,
824 cfg->rc_output_label))
825 goto errout;
826 break;
827 case RTA_DST:
828 {
829 u32 label_count;
830 if (nla_get_labels(nla, 1, &label_count,
831 &cfg->rc_label))
832 goto errout;
833
Robert Shearmana6affd22015-08-03 17:50:04 +0100834 /* Reserved labels may not be set */
835 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600836 goto errout;
837
838 break;
839 }
840 case RTA_VIA:
841 {
842 struct rtvia *via = nla_data(nla);
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000843 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
844 goto errout;
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000845 cfg->rc_via_alen = nla_len(nla) -
846 offsetof(struct rtvia, rtvia_addr);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600847 if (cfg->rc_via_alen > MAX_VIA_ALEN)
848 goto errout;
849
850 /* Validate the address family */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600851 switch(via->rtvia_family) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600852 case AF_PACKET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600853 cfg->rc_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600854 break;
855 case AF_INET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600856 cfg->rc_via_table = NEIGH_ARP_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600857 if (cfg->rc_via_alen != 4)
858 goto errout;
859 break;
860 case AF_INET6:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600861 cfg->rc_via_table = NEIGH_ND_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600862 if (cfg->rc_via_alen != 16)
863 goto errout;
864 break;
865 default:
866 /* Unsupported address family */
867 goto errout;
868 }
869
870 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
871 break;
872 }
873 default:
874 /* Unsupported attribute */
875 goto errout;
876 }
877 }
878
879 err = 0;
880errout:
881 return err;
882}
883
884static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
885{
886 struct mpls_route_config cfg;
887 int err;
888
889 err = rtm_to_route_config(skb, nlh, &cfg);
890 if (err < 0)
891 return err;
892
893 return mpls_route_del(&cfg);
894}
895
896
897static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
898{
899 struct mpls_route_config cfg;
900 int err;
901
902 err = rtm_to_route_config(skb, nlh, &cfg);
903 if (err < 0)
904 return err;
905
906 return mpls_route_add(&cfg);
907}
908
909static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
910 u32 label, struct mpls_route *rt, int flags)
911{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600912 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600913 struct nlmsghdr *nlh;
914 struct rtmsg *rtm;
915
916 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
917 if (nlh == NULL)
918 return -EMSGSIZE;
919
920 rtm = nlmsg_data(nlh);
921 rtm->rtm_family = AF_MPLS;
922 rtm->rtm_dst_len = 20;
923 rtm->rtm_src_len = 0;
924 rtm->rtm_tos = 0;
925 rtm->rtm_table = RT_TABLE_MAIN;
926 rtm->rtm_protocol = rt->rt_protocol;
927 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
928 rtm->rtm_type = RTN_UNICAST;
929 rtm->rtm_flags = 0;
930
931 if (rt->rt_labels &&
932 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
933 goto nla_put_failure;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600934 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600935 goto nla_put_failure;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600936 dev = rtnl_dereference(rt->rt_dev);
937 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600938 goto nla_put_failure;
939 if (nla_put_labels(skb, RTA_DST, 1, &label))
940 goto nla_put_failure;
941
942 nlmsg_end(skb, nlh);
943 return 0;
944
945nla_put_failure:
946 nlmsg_cancel(skb, nlh);
947 return -EMSGSIZE;
948}
949
950static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
951{
952 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600953 struct mpls_route __rcu **platform_label;
954 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600955 unsigned int index;
956
957 ASSERT_RTNL();
958
959 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +0100960 if (index < MPLS_LABEL_FIRST_UNRESERVED)
961 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600962
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600963 platform_label = rtnl_dereference(net->mpls.platform_label);
964 platform_labels = net->mpls.platform_labels;
965 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600966 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600967 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600968 if (!rt)
969 continue;
970
971 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
972 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
973 index, rt, NLM_F_MULTI) < 0)
974 break;
975 }
976 cb->args[0] = index;
977
978 return skb->len;
979}
980
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600981static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
982{
983 size_t payload =
984 NLMSG_ALIGN(sizeof(struct rtmsg))
985 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
986 + nla_total_size(4); /* RTA_DST */
987 if (rt->rt_labels) /* RTA_NEWDST */
988 payload += nla_total_size(rt->rt_labels * 4);
989 if (rt->rt_dev) /* RTA_OIF */
990 payload += nla_total_size(4);
991 return payload;
992}
993
994static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
995 struct nlmsghdr *nlh, struct net *net, u32 portid,
996 unsigned int nlm_flags)
997{
998 struct sk_buff *skb;
999 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1000 int err = -ENOBUFS;
1001
1002 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1003 if (skb == NULL)
1004 goto errout;
1005
1006 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1007 if (err < 0) {
1008 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1009 WARN_ON(err == -EMSGSIZE);
1010 kfree_skb(skb);
1011 goto errout;
1012 }
1013 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1014
1015 return;
1016errout:
1017 if (err < 0)
1018 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1019}
1020
Eric W. Biederman7720c012015-03-03 19:11:20 -06001021static int resize_platform_label_table(struct net *net, size_t limit)
1022{
1023 size_t size = sizeof(struct mpls_route *) * limit;
1024 size_t old_limit;
1025 size_t cp_size;
1026 struct mpls_route __rcu **labels = NULL, **old;
1027 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1028 unsigned index;
1029
1030 if (size) {
1031 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1032 if (!labels)
1033 labels = vzalloc(size);
1034
1035 if (!labels)
1036 goto nolabels;
1037 }
1038
1039 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001040 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001041 struct net_device *lo = net->loopback_dev;
1042 rt0 = mpls_rt_alloc(lo->addr_len);
1043 if (!rt0)
1044 goto nort0;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001045 RCU_INIT_POINTER(rt0->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001046 rt0->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001047 rt0->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001048 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1049 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001050 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001051 struct net_device *lo = net->loopback_dev;
1052 rt2 = mpls_rt_alloc(lo->addr_len);
1053 if (!rt2)
1054 goto nort2;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001055 RCU_INIT_POINTER(rt2->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001056 rt2->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001057 rt2->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001058 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1059 }
1060
1061 rtnl_lock();
1062 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001063 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001064 old_limit = net->mpls.platform_labels;
1065
1066 /* Free any labels beyond the new table */
1067 for (index = limit; index < old_limit; index++)
1068 mpls_route_update(net, index, NULL, NULL, NULL);
1069
1070 /* Copy over the old labels */
1071 cp_size = size;
1072 if (old_limit < limit)
1073 cp_size = old_limit * sizeof(struct mpls_route *);
1074
1075 memcpy(labels, old, cp_size);
1076
1077 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001078 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1079 (limit > MPLS_LABEL_IPV6NULL)) {
1080 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001081 rt2 = NULL;
1082 }
1083
Tom Herbert78f5b892015-05-07 08:08:51 -07001084 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1085 (limit > MPLS_LABEL_IPV4NULL)) {
1086 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001087 rt0 = NULL;
1088 }
1089
1090 /* Update the global pointers */
1091 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001092 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001093
1094 rtnl_unlock();
1095
1096 mpls_rt_free(rt2);
1097 mpls_rt_free(rt0);
1098
1099 if (old) {
1100 synchronize_rcu();
1101 kvfree(old);
1102 }
1103 return 0;
1104
1105nort2:
1106 mpls_rt_free(rt0);
1107nort0:
1108 kvfree(labels);
1109nolabels:
1110 return -ENOMEM;
1111}
1112
1113static int mpls_platform_labels(struct ctl_table *table, int write,
1114 void __user *buffer, size_t *lenp, loff_t *ppos)
1115{
1116 struct net *net = table->data;
1117 int platform_labels = net->mpls.platform_labels;
1118 int ret;
1119 struct ctl_table tmp = {
1120 .procname = table->procname,
1121 .data = &platform_labels,
1122 .maxlen = sizeof(int),
1123 .mode = table->mode,
1124 .extra1 = &zero,
1125 .extra2 = &label_limit,
1126 };
1127
1128 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1129
1130 if (write && ret == 0)
1131 ret = resize_platform_label_table(net, platform_labels);
1132
1133 return ret;
1134}
1135
Robert Shearman37bde792015-04-22 11:14:38 +01001136static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001137 {
1138 .procname = "platform_labels",
1139 .data = NULL,
1140 .maxlen = sizeof(int),
1141 .mode = 0644,
1142 .proc_handler = mpls_platform_labels,
1143 },
1144 { }
1145};
1146
Eric W. Biederman01891972015-03-03 19:10:47 -06001147static int mpls_net_init(struct net *net)
1148{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001149 struct ctl_table *table;
1150
Eric W. Biederman01891972015-03-03 19:10:47 -06001151 net->mpls.platform_labels = 0;
1152 net->mpls.platform_label = NULL;
1153
Eric W. Biederman7720c012015-03-03 19:11:20 -06001154 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1155 if (table == NULL)
1156 return -ENOMEM;
1157
1158 table[0].data = net;
1159 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1160 if (net->mpls.ctl == NULL)
1161 return -ENOMEM;
1162
Eric W. Biederman01891972015-03-03 19:10:47 -06001163 return 0;
1164}
1165
1166static void mpls_net_exit(struct net *net)
1167{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001168 struct mpls_route __rcu **platform_label;
1169 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001170 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001171 unsigned int index;
1172
Eric W. Biederman7720c012015-03-03 19:11:20 -06001173 table = net->mpls.ctl->ctl_table_arg;
1174 unregister_net_sysctl_table(net->mpls.ctl);
1175 kfree(table);
1176
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001177 /* An rcu grace period has passed since there was a device in
1178 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001179 * left this network namespace. This is because
1180 * unregister_netdevice_many and netdev_run_todo has completed
1181 * for each network device that was in this network namespace.
1182 *
1183 * As such no additional rcu synchronization is necessary when
1184 * freeing the platform_label table.
1185 */
1186 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001187 platform_label = rtnl_dereference(net->mpls.platform_label);
1188 platform_labels = net->mpls.platform_labels;
1189 for (index = 0; index < platform_labels; index++) {
1190 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1191 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001192 mpls_rt_free(rt);
1193 }
1194 rtnl_unlock();
1195
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001196 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001197}
1198
1199static struct pernet_operations mpls_net_ops = {
1200 .init = mpls_net_init,
1201 .exit = mpls_net_exit,
1202};
1203
1204static int __init mpls_init(void)
1205{
1206 int err;
1207
1208 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1209
1210 err = register_pernet_subsys(&mpls_net_ops);
1211 if (err)
1212 goto out;
1213
1214 err = register_netdevice_notifier(&mpls_dev_notifier);
1215 if (err)
1216 goto out_unregister_pernet;
1217
1218 dev_add_pack(&mpls_packet_type);
1219
Eric W. Biederman03c05662015-03-03 19:13:56 -06001220 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1221 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1222 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001223 err = 0;
1224out:
1225 return err;
1226
1227out_unregister_pernet:
1228 unregister_pernet_subsys(&mpls_net_ops);
1229 goto out;
1230}
1231module_init(mpls_init);
1232
1233static void __exit mpls_exit(void)
1234{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001235 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001236 dev_remove_pack(&mpls_packet_type);
1237 unregister_netdevice_notifier(&mpls_dev_notifier);
1238 unregister_pernet_subsys(&mpls_net_ops);
1239}
1240module_exit(mpls_exit);
1241
1242MODULE_DESCRIPTION("MultiProtocol Label Switching");
1243MODULE_LICENSE("GPL v2");
1244MODULE_ALIAS_NETPROTO(PF_MPLS);