blob: 04306989d05491ead0f83e1ab927b4f006302ff3 [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
Roopa Prabhu3dcb6152015-08-04 06:36:24 -0700414 if (!dev)
415 return ERR_PTR(-ENODEV);
416
Roopa Prabhu01faef22015-07-21 09:16:24 -0700417 return dev;
418}
419
Eric W. Biedermana2519922015-03-03 19:12:40 -0600420static int mpls_route_add(struct mpls_route_config *cfg)
421{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600422 struct mpls_route __rcu **platform_label;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600423 struct net *net = cfg->rc_nlinfo.nl_net;
424 struct net_device *dev = NULL;
425 struct mpls_route *rt, *old;
426 unsigned index;
427 int i;
428 int err = -EINVAL;
429
430 index = cfg->rc_label;
431
432 /* If a label was not specified during insert pick one */
433 if ((index == LABEL_NOT_SPECIFIED) &&
434 (cfg->rc_nlflags & NLM_F_CREATE)) {
435 index = find_free_label(net);
436 }
437
Robert Shearmana6affd22015-08-03 17:50:04 +0100438 /* Reserved labels may not be set */
439 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600440 goto errout;
441
442 /* The full 20 bit range may not be supported. */
443 if (index >= net->mpls.platform_labels)
444 goto errout;
445
446 /* Ensure only a supported number of labels are present */
447 if (cfg->rc_output_labels > MAX_NEW_LABELS)
448 goto errout;
449
Roopa Prabhu01faef22015-07-21 09:16:24 -0700450 dev = find_outdev(net, cfg);
Roopa Prabhubf215632015-07-30 13:34:54 -0700451 if (IS_ERR(dev)) {
452 err = PTR_ERR(dev);
453 dev = NULL;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600454 goto errout;
Roopa Prabhubf215632015-07-30 13:34:54 -0700455 }
Eric W. Biedermana2519922015-03-03 19:12:40 -0600456
Robert Shearman03c57742015-04-22 11:14:37 +0100457 /* Ensure this is a supported device */
Eric W. Biedermana2519922015-03-03 19:12:40 -0600458 err = -EINVAL;
Robert Shearman03c57742015-04-22 11:14:37 +0100459 if (!mpls_dev_get(dev))
Eric W. Biedermana2519922015-03-03 19:12:40 -0600460 goto errout;
461
462 err = -EINVAL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600463 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
Eric W. Biedermana2519922015-03-03 19:12:40 -0600464 (dev->addr_len != cfg->rc_via_alen))
465 goto errout;
466
467 /* Append makes no sense with mpls */
Eric W. Biederman0f7bbd52015-03-07 16:22:40 -0600468 err = -EOPNOTSUPP;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600469 if (cfg->rc_nlflags & NLM_F_APPEND)
470 goto errout;
471
472 err = -EEXIST;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600473 platform_label = rtnl_dereference(net->mpls.platform_label);
474 old = rtnl_dereference(platform_label[index]);
Eric W. Biedermana2519922015-03-03 19:12:40 -0600475 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
476 goto errout;
477
478 err = -EEXIST;
479 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
480 goto errout;
481
482 err = -ENOENT;
483 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
484 goto errout;
485
486 err = -ENOMEM;
487 rt = mpls_rt_alloc(cfg->rc_via_alen);
488 if (!rt)
489 goto errout;
490
491 rt->rt_labels = cfg->rc_output_labels;
492 for (i = 0; i < rt->rt_labels; i++)
493 rt->rt_label[i] = cfg->rc_output_label[i];
494 rt->rt_protocol = cfg->rc_protocol;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600495 RCU_INIT_POINTER(rt->rt_dev, dev);
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600496 rt->rt_via_table = cfg->rc_via_table;
Eric W. Biedermana2519922015-03-03 19:12:40 -0600497 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
498
499 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
500
501 dev_put(dev);
502 return 0;
503
504errout:
505 if (dev)
506 dev_put(dev);
507 return err;
508}
509
510static int mpls_route_del(struct mpls_route_config *cfg)
511{
512 struct net *net = cfg->rc_nlinfo.nl_net;
513 unsigned index;
514 int err = -EINVAL;
515
516 index = cfg->rc_label;
517
Robert Shearmana6affd22015-08-03 17:50:04 +0100518 /* Reserved labels may not be removed */
519 if (index < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biedermana2519922015-03-03 19:12:40 -0600520 goto errout;
521
522 /* The full 20 bit range may not be supported */
523 if (index >= net->mpls.platform_labels)
524 goto errout;
525
526 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
527
528 err = 0;
529errout:
530 return err;
531}
532
Robert Shearman37bde792015-04-22 11:14:38 +0100533#define MPLS_PERDEV_SYSCTL_OFFSET(field) \
534 (&((struct mpls_dev *)0)->field)
535
536static const struct ctl_table mpls_dev_table[] = {
537 {
538 .procname = "input",
539 .maxlen = sizeof(int),
540 .mode = 0644,
541 .proc_handler = proc_dointvec,
542 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
543 },
544 { }
545};
546
547static int mpls_dev_sysctl_register(struct net_device *dev,
548 struct mpls_dev *mdev)
549{
550 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
551 struct ctl_table *table;
552 int i;
553
554 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
555 if (!table)
556 goto out;
557
558 /* Table data contains only offsets relative to the base of
559 * the mdev at this point, so make them absolute.
560 */
561 for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
562 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
563
564 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
565
566 mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
567 if (!mdev->sysctl)
568 goto free;
569
570 return 0;
571
572free:
573 kfree(table);
574out:
575 return -ENOBUFS;
576}
577
578static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
579{
580 struct ctl_table *table;
581
582 table = mdev->sysctl->ctl_table_arg;
583 unregister_net_sysctl_table(mdev->sysctl);
584 kfree(table);
585}
586
Robert Shearman03c57742015-04-22 11:14:37 +0100587static struct mpls_dev *mpls_add_dev(struct net_device *dev)
588{
589 struct mpls_dev *mdev;
590 int err = -ENOMEM;
591
592 ASSERT_RTNL();
593
594 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
595 if (!mdev)
596 return ERR_PTR(err);
597
Robert Shearman37bde792015-04-22 11:14:38 +0100598 err = mpls_dev_sysctl_register(dev, mdev);
599 if (err)
600 goto free;
601
Robert Shearman03c57742015-04-22 11:14:37 +0100602 rcu_assign_pointer(dev->mpls_ptr, mdev);
603
604 return mdev;
Robert Shearman37bde792015-04-22 11:14:38 +0100605
606free:
607 kfree(mdev);
608 return ERR_PTR(err);
Robert Shearman03c57742015-04-22 11:14:37 +0100609}
610
Eric W. Biederman01891972015-03-03 19:10:47 -0600611static void mpls_ifdown(struct net_device *dev)
612{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600613 struct mpls_route __rcu **platform_label;
Eric W. Biederman01891972015-03-03 19:10:47 -0600614 struct net *net = dev_net(dev);
Robert Shearman03c57742015-04-22 11:14:37 +0100615 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600616 unsigned index;
617
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600618 platform_label = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -0600619 for (index = 0; index < net->mpls.platform_labels; index++) {
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600620 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman01891972015-03-03 19:10:47 -0600621 if (!rt)
622 continue;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600623 if (rtnl_dereference(rt->rt_dev) != dev)
Eric W. Biederman01891972015-03-03 19:10:47 -0600624 continue;
625 rt->rt_dev = NULL;
626 }
Robert Shearman03c57742015-04-22 11:14:37 +0100627
628 mdev = mpls_dev_get(dev);
629 if (!mdev)
630 return;
631
Robert Shearman37bde792015-04-22 11:14:38 +0100632 mpls_dev_sysctl_unregister(mdev);
633
Robert Shearman03c57742015-04-22 11:14:37 +0100634 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
635
Robert Shearman25cc8f02015-06-05 18:54:45 +0100636 kfree_rcu(mdev, rcu);
Eric W. Biederman01891972015-03-03 19:10:47 -0600637}
638
639static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
640 void *ptr)
641{
642 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Robert Shearman03c57742015-04-22 11:14:37 +0100643 struct mpls_dev *mdev;
Eric W. Biederman01891972015-03-03 19:10:47 -0600644
645 switch(event) {
Robert Shearman03c57742015-04-22 11:14:37 +0100646 case NETDEV_REGISTER:
647 /* For now just support ethernet devices */
648 if ((dev->type == ARPHRD_ETHER) ||
649 (dev->type == ARPHRD_LOOPBACK)) {
650 mdev = mpls_add_dev(dev);
651 if (IS_ERR(mdev))
652 return notifier_from_errno(PTR_ERR(mdev));
653 }
654 break;
655
Eric W. Biederman01891972015-03-03 19:10:47 -0600656 case NETDEV_UNREGISTER:
657 mpls_ifdown(dev);
658 break;
Robert Shearman0fae3bf2015-06-11 19:58:26 +0100659 case NETDEV_CHANGENAME:
660 mdev = mpls_dev_get(dev);
661 if (mdev) {
662 int err;
663
664 mpls_dev_sysctl_unregister(mdev);
665 err = mpls_dev_sysctl_register(dev, mdev);
666 if (err)
667 return notifier_from_errno(err);
668 }
669 break;
Eric W. Biederman01891972015-03-03 19:10:47 -0600670 }
671 return NOTIFY_OK;
672}
673
674static struct notifier_block mpls_dev_notifier = {
675 .notifier_call = mpls_dev_notify,
676};
677
Eric W. Biederman03c05662015-03-03 19:13:56 -0600678static int nla_put_via(struct sk_buff *skb,
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600679 u8 table, const void *addr, int alen)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600680{
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600681 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
682 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
683 };
Eric W. Biederman03c05662015-03-03 19:13:56 -0600684 struct nlattr *nla;
685 struct rtvia *via;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600686 int family = AF_UNSPEC;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600687
688 nla = nla_reserve(skb, RTA_VIA, alen + 2);
689 if (!nla)
690 return -EMSGSIZE;
691
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600692 if (table <= NEIGH_NR_TABLES)
693 family = table_to_family[table];
694
Eric W. Biederman03c05662015-03-03 19:13:56 -0600695 via = nla_data(nla);
696 via->rtvia_family = family;
697 memcpy(via->rtvia_addr, addr, alen);
698 return 0;
699}
700
Eric W. Biederman966bae32015-03-03 19:13:19 -0600701int nla_put_labels(struct sk_buff *skb, int attrtype,
702 u8 labels, const u32 label[])
703{
704 struct nlattr *nla;
705 struct mpls_shim_hdr *nla_label;
706 bool bos;
707 int i;
708 nla = nla_reserve(skb, attrtype, labels*4);
709 if (!nla)
710 return -EMSGSIZE;
711
712 nla_label = nla_data(nla);
713 bos = true;
714 for (i = labels - 1; i >= 0; i--) {
715 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
716 bos = false;
717 }
718
719 return 0;
720}
Roopa Prabhuface0182015-07-21 10:43:52 +0200721EXPORT_SYMBOL_GPL(nla_put_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600722
723int nla_get_labels(const struct nlattr *nla,
724 u32 max_labels, u32 *labels, u32 label[])
725{
726 unsigned len = nla_len(nla);
727 unsigned nla_labels;
728 struct mpls_shim_hdr *nla_label;
729 bool bos;
730 int i;
731
732 /* len needs to be an even multiple of 4 (the label size) */
733 if (len & 3)
734 return -EINVAL;
735
736 /* Limit the number of new labels allowed */
737 nla_labels = len/4;
738 if (nla_labels > max_labels)
739 return -EINVAL;
740
741 nla_label = nla_data(nla);
742 bos = true;
743 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
744 struct mpls_entry_decoded dec;
745 dec = mpls_entry_decode(nla_label + i);
746
747 /* Ensure the bottom of stack flag is properly set
748 * and ttl and tc are both clear.
749 */
750 if ((dec.bos != bos) || dec.ttl || dec.tc)
751 return -EINVAL;
752
Robert Shearman5a9ab012015-04-22 11:14:39 +0100753 switch (dec.label) {
Tom Herbert78f5b892015-05-07 08:08:51 -0700754 case MPLS_LABEL_IMPLNULL:
Robert Shearman5a9ab012015-04-22 11:14:39 +0100755 /* RFC3032: This is a label that an LSR may
756 * assign and distribute, but which never
757 * actually appears in the encapsulation.
758 */
759 return -EINVAL;
760 }
761
Eric W. Biederman966bae32015-03-03 19:13:19 -0600762 label[i] = dec.label;
763 }
764 *labels = nla_labels;
765 return 0;
766}
Roopa Prabhuface0182015-07-21 10:43:52 +0200767EXPORT_SYMBOL_GPL(nla_get_labels);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600768
Eric W. Biederman03c05662015-03-03 19:13:56 -0600769static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
770 struct mpls_route_config *cfg)
771{
772 struct rtmsg *rtm;
773 struct nlattr *tb[RTA_MAX+1];
774 int index;
775 int err;
776
777 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
778 if (err < 0)
779 goto errout;
780
781 err = -EINVAL;
782 rtm = nlmsg_data(nlh);
783 memset(cfg, 0, sizeof(*cfg));
784
785 if (rtm->rtm_family != AF_MPLS)
786 goto errout;
787 if (rtm->rtm_dst_len != 20)
788 goto errout;
789 if (rtm->rtm_src_len != 0)
790 goto errout;
791 if (rtm->rtm_tos != 0)
792 goto errout;
793 if (rtm->rtm_table != RT_TABLE_MAIN)
794 goto errout;
795 /* Any value is acceptable for rtm_protocol */
796
797 /* As mpls uses destination specific addresses
798 * (or source specific address in the case of multicast)
799 * all addresses have universal scope.
800 */
801 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
802 goto errout;
803 if (rtm->rtm_type != RTN_UNICAST)
804 goto errout;
805 if (rtm->rtm_flags != 0)
806 goto errout;
807
808 cfg->rc_label = LABEL_NOT_SPECIFIED;
809 cfg->rc_protocol = rtm->rtm_protocol;
810 cfg->rc_nlflags = nlh->nlmsg_flags;
811 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
812 cfg->rc_nlinfo.nlh = nlh;
813 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
814
815 for (index = 0; index <= RTA_MAX; index++) {
816 struct nlattr *nla = tb[index];
817 if (!nla)
818 continue;
819
820 switch(index) {
821 case RTA_OIF:
822 cfg->rc_ifindex = nla_get_u32(nla);
823 break;
824 case RTA_NEWDST:
825 if (nla_get_labels(nla, MAX_NEW_LABELS,
826 &cfg->rc_output_labels,
827 cfg->rc_output_label))
828 goto errout;
829 break;
830 case RTA_DST:
831 {
832 u32 label_count;
833 if (nla_get_labels(nla, 1, &label_count,
834 &cfg->rc_label))
835 goto errout;
836
Robert Shearmana6affd22015-08-03 17:50:04 +0100837 /* Reserved labels may not be set */
838 if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
Eric W. Biederman03c05662015-03-03 19:13:56 -0600839 goto errout;
840
841 break;
842 }
843 case RTA_VIA:
844 {
845 struct rtvia *via = nla_data(nla);
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000846 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
847 goto errout;
Robert Shearmanf8d54af2015-03-06 10:47:00 +0000848 cfg->rc_via_alen = nla_len(nla) -
849 offsetof(struct rtvia, rtvia_addr);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600850 if (cfg->rc_via_alen > MAX_VIA_ALEN)
851 goto errout;
852
853 /* Validate the address family */
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600854 switch(via->rtvia_family) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600855 case AF_PACKET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600856 cfg->rc_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600857 break;
858 case AF_INET:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600859 cfg->rc_via_table = NEIGH_ARP_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600860 if (cfg->rc_via_alen != 4)
861 goto errout;
862 break;
863 case AF_INET6:
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600864 cfg->rc_via_table = NEIGH_ND_TABLE;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600865 if (cfg->rc_via_alen != 16)
866 goto errout;
867 break;
868 default:
869 /* Unsupported address family */
870 goto errout;
871 }
872
873 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
874 break;
875 }
876 default:
877 /* Unsupported attribute */
878 goto errout;
879 }
880 }
881
882 err = 0;
883errout:
884 return err;
885}
886
887static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
888{
889 struct mpls_route_config cfg;
890 int err;
891
892 err = rtm_to_route_config(skb, nlh, &cfg);
893 if (err < 0)
894 return err;
895
896 return mpls_route_del(&cfg);
897}
898
899
900static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
901{
902 struct mpls_route_config cfg;
903 int err;
904
905 err = rtm_to_route_config(skb, nlh, &cfg);
906 if (err < 0)
907 return err;
908
909 return mpls_route_add(&cfg);
910}
911
912static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
913 u32 label, struct mpls_route *rt, int flags)
914{
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600915 struct net_device *dev;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600916 struct nlmsghdr *nlh;
917 struct rtmsg *rtm;
918
919 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
920 if (nlh == NULL)
921 return -EMSGSIZE;
922
923 rtm = nlmsg_data(nlh);
924 rtm->rtm_family = AF_MPLS;
925 rtm->rtm_dst_len = 20;
926 rtm->rtm_src_len = 0;
927 rtm->rtm_tos = 0;
928 rtm->rtm_table = RT_TABLE_MAIN;
929 rtm->rtm_protocol = rt->rt_protocol;
930 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
931 rtm->rtm_type = RTN_UNICAST;
932 rtm->rtm_flags = 0;
933
934 if (rt->rt_labels &&
935 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
936 goto nla_put_failure;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -0600937 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600938 goto nla_put_failure;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600939 dev = rtnl_dereference(rt->rt_dev);
940 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
Eric W. Biederman03c05662015-03-03 19:13:56 -0600941 goto nla_put_failure;
942 if (nla_put_labels(skb, RTA_DST, 1, &label))
943 goto nla_put_failure;
944
945 nlmsg_end(skb, nlh);
946 return 0;
947
948nla_put_failure:
949 nlmsg_cancel(skb, nlh);
950 return -EMSGSIZE;
951}
952
953static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
954{
955 struct net *net = sock_net(skb->sk);
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600956 struct mpls_route __rcu **platform_label;
957 size_t platform_labels;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600958 unsigned int index;
959
960 ASSERT_RTNL();
961
962 index = cb->args[0];
Robert Shearmana6affd22015-08-03 17:50:04 +0100963 if (index < MPLS_LABEL_FIRST_UNRESERVED)
964 index = MPLS_LABEL_FIRST_UNRESERVED;
Eric W. Biederman03c05662015-03-03 19:13:56 -0600965
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600966 platform_label = rtnl_dereference(net->mpls.platform_label);
967 platform_labels = net->mpls.platform_labels;
968 for (; index < platform_labels; index++) {
Eric W. Biederman03c05662015-03-03 19:13:56 -0600969 struct mpls_route *rt;
Eric W. Biederman19d0c342015-03-07 16:21:56 -0600970 rt = rtnl_dereference(platform_label[index]);
Eric W. Biederman03c05662015-03-03 19:13:56 -0600971 if (!rt)
972 continue;
973
974 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
975 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
976 index, rt, NLM_F_MULTI) < 0)
977 break;
978 }
979 cb->args[0] = index;
980
981 return skb->len;
982}
983
Eric W. Biederman8de147d2015-03-03 19:14:31 -0600984static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
985{
986 size_t payload =
987 NLMSG_ALIGN(sizeof(struct rtmsg))
988 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
989 + nla_total_size(4); /* RTA_DST */
990 if (rt->rt_labels) /* RTA_NEWDST */
991 payload += nla_total_size(rt->rt_labels * 4);
992 if (rt->rt_dev) /* RTA_OIF */
993 payload += nla_total_size(4);
994 return payload;
995}
996
997static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
998 struct nlmsghdr *nlh, struct net *net, u32 portid,
999 unsigned int nlm_flags)
1000{
1001 struct sk_buff *skb;
1002 u32 seq = nlh ? nlh->nlmsg_seq : 0;
1003 int err = -ENOBUFS;
1004
1005 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
1006 if (skb == NULL)
1007 goto errout;
1008
1009 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
1010 if (err < 0) {
1011 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
1012 WARN_ON(err == -EMSGSIZE);
1013 kfree_skb(skb);
1014 goto errout;
1015 }
1016 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
1017
1018 return;
1019errout:
1020 if (err < 0)
1021 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
1022}
1023
Eric W. Biederman7720c012015-03-03 19:11:20 -06001024static int resize_platform_label_table(struct net *net, size_t limit)
1025{
1026 size_t size = sizeof(struct mpls_route *) * limit;
1027 size_t old_limit;
1028 size_t cp_size;
1029 struct mpls_route __rcu **labels = NULL, **old;
1030 struct mpls_route *rt0 = NULL, *rt2 = NULL;
1031 unsigned index;
1032
1033 if (size) {
1034 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1035 if (!labels)
1036 labels = vzalloc(size);
1037
1038 if (!labels)
1039 goto nolabels;
1040 }
1041
1042 /* In case the predefined labels need to be populated */
Tom Herbert78f5b892015-05-07 08:08:51 -07001043 if (limit > MPLS_LABEL_IPV4NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001044 struct net_device *lo = net->loopback_dev;
1045 rt0 = mpls_rt_alloc(lo->addr_len);
1046 if (!rt0)
1047 goto nort0;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001048 RCU_INIT_POINTER(rt0->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001049 rt0->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001050 rt0->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001051 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
1052 }
Tom Herbert78f5b892015-05-07 08:08:51 -07001053 if (limit > MPLS_LABEL_IPV6NULL) {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001054 struct net_device *lo = net->loopback_dev;
1055 rt2 = mpls_rt_alloc(lo->addr_len);
1056 if (!rt2)
1057 goto nort2;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001058 RCU_INIT_POINTER(rt2->rt_dev, lo);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001059 rt2->rt_protocol = RTPROT_KERNEL;
Eric W. Biedermanb79bda32015-03-07 16:25:56 -06001060 rt2->rt_via_table = NEIGH_LINK_TABLE;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001061 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
1062 }
1063
1064 rtnl_lock();
1065 /* Remember the original table */
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001066 old = rtnl_dereference(net->mpls.platform_label);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001067 old_limit = net->mpls.platform_labels;
1068
1069 /* Free any labels beyond the new table */
1070 for (index = limit; index < old_limit; index++)
1071 mpls_route_update(net, index, NULL, NULL, NULL);
1072
1073 /* Copy over the old labels */
1074 cp_size = size;
1075 if (old_limit < limit)
1076 cp_size = old_limit * sizeof(struct mpls_route *);
1077
1078 memcpy(labels, old, cp_size);
1079
1080 /* If needed set the predefined labels */
Tom Herbert78f5b892015-05-07 08:08:51 -07001081 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
1082 (limit > MPLS_LABEL_IPV6NULL)) {
1083 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001084 rt2 = NULL;
1085 }
1086
Tom Herbert78f5b892015-05-07 08:08:51 -07001087 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
1088 (limit > MPLS_LABEL_IPV4NULL)) {
1089 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001090 rt0 = NULL;
1091 }
1092
1093 /* Update the global pointers */
1094 net->mpls.platform_labels = limit;
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001095 rcu_assign_pointer(net->mpls.platform_label, labels);
Eric W. Biederman7720c012015-03-03 19:11:20 -06001096
1097 rtnl_unlock();
1098
1099 mpls_rt_free(rt2);
1100 mpls_rt_free(rt0);
1101
1102 if (old) {
1103 synchronize_rcu();
1104 kvfree(old);
1105 }
1106 return 0;
1107
1108nort2:
1109 mpls_rt_free(rt0);
1110nort0:
1111 kvfree(labels);
1112nolabels:
1113 return -ENOMEM;
1114}
1115
1116static int mpls_platform_labels(struct ctl_table *table, int write,
1117 void __user *buffer, size_t *lenp, loff_t *ppos)
1118{
1119 struct net *net = table->data;
1120 int platform_labels = net->mpls.platform_labels;
1121 int ret;
1122 struct ctl_table tmp = {
1123 .procname = table->procname,
1124 .data = &platform_labels,
1125 .maxlen = sizeof(int),
1126 .mode = table->mode,
1127 .extra1 = &zero,
1128 .extra2 = &label_limit,
1129 };
1130
1131 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1132
1133 if (write && ret == 0)
1134 ret = resize_platform_label_table(net, platform_labels);
1135
1136 return ret;
1137}
1138
Robert Shearman37bde792015-04-22 11:14:38 +01001139static const struct ctl_table mpls_table[] = {
Eric W. Biederman7720c012015-03-03 19:11:20 -06001140 {
1141 .procname = "platform_labels",
1142 .data = NULL,
1143 .maxlen = sizeof(int),
1144 .mode = 0644,
1145 .proc_handler = mpls_platform_labels,
1146 },
1147 { }
1148};
1149
Eric W. Biederman01891972015-03-03 19:10:47 -06001150static int mpls_net_init(struct net *net)
1151{
Eric W. Biederman7720c012015-03-03 19:11:20 -06001152 struct ctl_table *table;
1153
Eric W. Biederman01891972015-03-03 19:10:47 -06001154 net->mpls.platform_labels = 0;
1155 net->mpls.platform_label = NULL;
1156
Eric W. Biederman7720c012015-03-03 19:11:20 -06001157 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
1158 if (table == NULL)
1159 return -ENOMEM;
1160
1161 table[0].data = net;
1162 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
1163 if (net->mpls.ctl == NULL)
1164 return -ENOMEM;
1165
Eric W. Biederman01891972015-03-03 19:10:47 -06001166 return 0;
1167}
1168
1169static void mpls_net_exit(struct net *net)
1170{
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001171 struct mpls_route __rcu **platform_label;
1172 size_t platform_labels;
Eric W. Biederman7720c012015-03-03 19:11:20 -06001173 struct ctl_table *table;
Eric W. Biederman01891972015-03-03 19:10:47 -06001174 unsigned int index;
1175
Eric W. Biederman7720c012015-03-03 19:11:20 -06001176 table = net->mpls.ctl->ctl_table_arg;
1177 unregister_net_sysctl_table(net->mpls.ctl);
1178 kfree(table);
1179
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001180 /* An rcu grace period has passed since there was a device in
1181 * the network namespace (and thus the last in flight packet)
Eric W. Biederman01891972015-03-03 19:10:47 -06001182 * left this network namespace. This is because
1183 * unregister_netdevice_many and netdev_run_todo has completed
1184 * for each network device that was in this network namespace.
1185 *
1186 * As such no additional rcu synchronization is necessary when
1187 * freeing the platform_label table.
1188 */
1189 rtnl_lock();
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001190 platform_label = rtnl_dereference(net->mpls.platform_label);
1191 platform_labels = net->mpls.platform_labels;
1192 for (index = 0; index < platform_labels; index++) {
1193 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
1194 RCU_INIT_POINTER(platform_label[index], NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001195 mpls_rt_free(rt);
1196 }
1197 rtnl_unlock();
1198
Eric W. Biederman19d0c342015-03-07 16:21:56 -06001199 kvfree(platform_label);
Eric W. Biederman01891972015-03-03 19:10:47 -06001200}
1201
1202static struct pernet_operations mpls_net_ops = {
1203 .init = mpls_net_init,
1204 .exit = mpls_net_exit,
1205};
1206
1207static int __init mpls_init(void)
1208{
1209 int err;
1210
1211 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
1212
1213 err = register_pernet_subsys(&mpls_net_ops);
1214 if (err)
1215 goto out;
1216
1217 err = register_netdevice_notifier(&mpls_dev_notifier);
1218 if (err)
1219 goto out_unregister_pernet;
1220
1221 dev_add_pack(&mpls_packet_type);
1222
Eric W. Biederman03c05662015-03-03 19:13:56 -06001223 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1224 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1225 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
Eric W. Biederman01891972015-03-03 19:10:47 -06001226 err = 0;
1227out:
1228 return err;
1229
1230out_unregister_pernet:
1231 unregister_pernet_subsys(&mpls_net_ops);
1232 goto out;
1233}
1234module_init(mpls_init);
1235
1236static void __exit mpls_exit(void)
1237{
Eric W. Biederman03c05662015-03-03 19:13:56 -06001238 rtnl_unregister_all(PF_MPLS);
Eric W. Biederman01891972015-03-03 19:10:47 -06001239 dev_remove_pack(&mpls_packet_type);
1240 unregister_netdevice_notifier(&mpls_dev_notifier);
1241 unregister_pernet_subsys(&mpls_net_ops);
1242}
1243module_exit(mpls_exit);
1244
1245MODULE_DESCRIPTION("MultiProtocol Label Switching");
1246MODULE_LICENSE("GPL v2");
1247MODULE_ALIAS_NETPROTO(PF_MPLS);