Commit | Line | Data |
---|---|---|
0189197f EB |
1 | #include <linux/types.h> |
2 | #include <linux/skbuff.h> | |
3 | #include <linux/socket.h> | |
7720c01f | 4 | #include <linux/sysctl.h> |
0189197f EB |
5 | #include <linux/net.h> |
6 | #include <linux/module.h> | |
7 | #include <linux/if_arp.h> | |
8 | #include <linux/ipv6.h> | |
9 | #include <linux/mpls.h> | |
4b5edb2f | 10 | #include <linux/vmalloc.h> |
0189197f EB |
11 | #include <net/ip.h> |
12 | #include <net/dst.h> | |
13 | #include <net/sock.h> | |
14 | #include <net/arp.h> | |
15 | #include <net/ip_fib.h> | |
16 | #include <net/netevent.h> | |
17 | #include <net/netns/generic.h> | |
bf21563a RP |
18 | #if IS_ENABLED(CONFIG_IPV6) |
19 | #include <net/ipv6.h> | |
20 | #include <net/addrconf.h> | |
21 | #endif | |
0189197f EB |
22 | #include "internal.h" |
23 | ||
a2519929 | 24 | #define LABEL_NOT_SPECIFIED (1<<20) |
0189197f EB |
25 | #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 | ||
30 | struct mpls_route { /* next hop label forwarding entry */ | |
19d0c341 | 31 | struct net_device __rcu *rt_dev; |
0189197f EB |
32 | struct rcu_head rt_rcu; |
33 | u32 rt_label[MAX_NEW_LABELS]; | |
34 | u8 rt_protocol; /* routing protocol that set this entry */ | |
b79bda3d EB |
35 | u8 rt_labels; |
36 | u8 rt_via_alen; | |
37 | u8 rt_via_table; | |
0189197f EB |
38 | u8 rt_via[0]; |
39 | }; | |
40 | ||
7720c01f EB |
41 | static int zero = 0; |
42 | static int label_limit = (1 << 20) - 1; | |
43 | ||
8de147dc EB |
44 | static 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 | ||
0189197f EB |
48 | static 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 | ||
03c57747 RS |
60 | static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev) |
61 | { | |
62 | return rcu_dereference_rtnl(dev->mpls_ptr); | |
63 | } | |
64 | ||
face0188 | 65 | bool mpls_output_possible(const struct net_device *dev) |
0189197f EB |
66 | { |
67 | return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev); | |
68 | } | |
face0188 | 69 | EXPORT_SYMBOL_GPL(mpls_output_possible); |
0189197f EB |
70 | |
71 | static 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 | ||
face0188 | 77 | unsigned int mpls_dev_mtu(const struct net_device *dev) |
0189197f EB |
78 | { |
79 | /* The amount of data the layer 2 frame can hold */ | |
80 | return dev->mtu; | |
81 | } | |
face0188 | 82 | EXPORT_SYMBOL_GPL(mpls_dev_mtu); |
0189197f | 83 | |
face0188 | 84 | bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) |
0189197f EB |
85 | { |
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 | } | |
face0188 | 94 | EXPORT_SYMBOL_GPL(mpls_pkt_too_big); |
0189197f EB |
95 | |
96 | static 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 | */ | |
76fecd82 | 107 | struct iphdr *hdr4; |
0189197f EB |
108 | bool success = true; |
109 | ||
76fecd82 EB |
110 | /* 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); | |
0189197f EB |
125 | 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 | ||
143 | static 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; | |
03c57747 | 151 | struct mpls_dev *mdev; |
0189197f EB |
152 | 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 | ||
03c57747 | 159 | mdev = mpls_dev_get(dev); |
37bde799 | 160 | if (!mdev || !mdev->input_enabled) |
03c57747 RS |
161 | goto drop; |
162 | ||
0189197f EB |
163 | 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 */ | |
19d0c341 | 187 | out_dev = rcu_dereference(rt->rt_dev); |
0189197f EB |
188 | 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 */ | |
aa7da937 | 197 | if (dec.ttl <= 1) |
0189197f EB |
198 | 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 | ||
b79bda3d | 236 | err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb); |
0189197f EB |
237 | if (err) |
238 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", | |
239 | __func__, err); | |
240 | return 0; | |
241 | ||
242 | drop: | |
243 | kfree_skb(skb); | |
244 | return NET_RX_DROP; | |
245 | } | |
246 | ||
247 | static struct packet_type mpls_packet_type __read_mostly = { | |
248 | .type = cpu_to_be16(ETH_P_MPLS_UC), | |
249 | .func = mpls_forward, | |
250 | }; | |
251 | ||
f0126539 | 252 | static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = { |
03c05665 EB |
253 | [RTA_DST] = { .type = NLA_U32 }, |
254 | [RTA_OIF] = { .type = NLA_U32 }, | |
255 | }; | |
256 | ||
a2519929 EB |
257 | struct mpls_route_config { |
258 | u32 rc_protocol; | |
259 | u32 rc_ifindex; | |
b79bda3d | 260 | u16 rc_via_table; |
a2519929 EB |
261 | 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 | ||
0189197f EB |
270 | static struct mpls_route *mpls_rt_alloc(size_t alen) |
271 | { | |
272 | struct mpls_route *rt; | |
273 | ||
d865616e | 274 | rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL); |
0189197f EB |
275 | if (rt) |
276 | rt->rt_via_alen = alen; | |
277 | return rt; | |
278 | } | |
279 | ||
280 | static void mpls_rt_free(struct mpls_route *rt) | |
281 | { | |
282 | if (rt) | |
283 | kfree_rcu(rt, rt_rcu); | |
284 | } | |
285 | ||
8de147dc EB |
286 | static 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 */ | |
a6affd24 | 296 | if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED)) |
8de147dc EB |
297 | rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags); |
298 | } | |
299 | ||
0189197f EB |
300 | static 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 | { | |
19d0c341 | 304 | struct mpls_route __rcu **platform_label; |
0189197f EB |
305 | struct mpls_route *rt, *old = NULL; |
306 | ||
307 | ASSERT_RTNL(); | |
308 | ||
19d0c341 EB |
309 | 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); | |
0189197f EB |
313 | old = rt; |
314 | } | |
315 | ||
8de147dc EB |
316 | mpls_notify_route(net, index, old, new, info); |
317 | ||
0189197f EB |
318 | /* If we removed a route free it now */ |
319 | mpls_rt_free(old); | |
320 | } | |
321 | ||
a2519929 EB |
322 | static unsigned find_free_label(struct net *net) |
323 | { | |
19d0c341 EB |
324 | struct mpls_route __rcu **platform_label; |
325 | size_t platform_labels; | |
a2519929 | 326 | unsigned index; |
19d0c341 EB |
327 | |
328 | platform_label = rtnl_dereference(net->mpls.platform_label); | |
329 | platform_labels = net->mpls.platform_labels; | |
a6affd24 RS |
330 | for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels; |
331 | index++) { | |
19d0c341 | 332 | if (!rtnl_dereference(platform_label[index])) |
a2519929 EB |
333 | return index; |
334 | } | |
335 | return LABEL_NOT_SPECIFIED; | |
336 | } | |
337 | ||
bf21563a | 338 | #if IS_ENABLED(CONFIG_INET) |
01faef2c RP |
339 | static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr) |
340 | { | |
341 | struct net_device *dev = NULL; | |
342 | 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)) | |
348 | goto errout; | |
349 | ||
350 | dev = rt->dst.dev; | |
351 | dev_hold(dev); | |
352 | ||
353 | ip_rt_put(rt); | |
354 | ||
01faef2c | 355 | return dev; |
bf21563a RP |
356 | errout: |
357 | return ERR_PTR(-ENODEV); | |
358 | } | |
359 | #else | |
360 | static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr) | |
361 | { | |
362 | return ERR_PTR(-EAFNOSUPPORT); | |
01faef2c | 363 | } |
bf21563a | 364 | #endif |
01faef2c | 365 | |
bf21563a | 366 | #if IS_ENABLED(CONFIG_IPV6) |
01faef2c RP |
367 | static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr) |
368 | { | |
369 | struct net_device *dev = NULL; | |
370 | struct dst_entry *dst; | |
371 | struct flowi6 fl6; | |
bf21563a RP |
372 | int err; |
373 | ||
374 | if (!ipv6_stub) | |
375 | return ERR_PTR(-EAFNOSUPPORT); | |
01faef2c RP |
376 | |
377 | memset(&fl6, 0, sizeof(fl6)); | |
378 | memcpy(&fl6.daddr, addr, sizeof(struct in6_addr)); | |
bf21563a RP |
379 | err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6); |
380 | if (err) | |
01faef2c RP |
381 | goto errout; |
382 | ||
383 | dev = dst->dev; | |
384 | dev_hold(dev); | |
01faef2c RP |
385 | dst_release(dst); |
386 | ||
387 | return dev; | |
bf21563a RP |
388 | |
389 | errout: | |
390 | return ERR_PTR(err); | |
01faef2c | 391 | } |
bf21563a RP |
392 | #else |
393 | static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr) | |
394 | { | |
395 | return ERR_PTR(-EAFNOSUPPORT); | |
396 | } | |
397 | #endif | |
01faef2c RP |
398 | |
399 | static struct net_device *find_outdev(struct net *net, | |
400 | struct mpls_route_config *cfg) | |
401 | { | |
402 | struct net_device *dev = NULL; | |
403 | ||
404 | if (!cfg->rc_ifindex) { | |
405 | switch (cfg->rc_via_table) { | |
406 | case NEIGH_ARP_TABLE: | |
407 | dev = inet_fib_lookup_dev(net, cfg->rc_via); | |
408 | break; | |
409 | case NEIGH_ND_TABLE: | |
410 | dev = inet6_fib_lookup_dev(net, cfg->rc_via); | |
411 | break; | |
412 | case NEIGH_LINK_TABLE: | |
413 | break; | |
414 | } | |
415 | } else { | |
416 | dev = dev_get_by_index(net, cfg->rc_ifindex); | |
417 | } | |
418 | ||
419 | return dev; | |
420 | } | |
421 | ||
a2519929 EB |
422 | static int mpls_route_add(struct mpls_route_config *cfg) |
423 | { | |
19d0c341 | 424 | struct mpls_route __rcu **platform_label; |
a2519929 EB |
425 | struct net *net = cfg->rc_nlinfo.nl_net; |
426 | struct net_device *dev = NULL; | |
427 | struct mpls_route *rt, *old; | |
428 | unsigned index; | |
429 | int i; | |
430 | int err = -EINVAL; | |
431 | ||
432 | index = cfg->rc_label; | |
433 | ||
434 | /* If a label was not specified during insert pick one */ | |
435 | if ((index == LABEL_NOT_SPECIFIED) && | |
436 | (cfg->rc_nlflags & NLM_F_CREATE)) { | |
437 | index = find_free_label(net); | |
438 | } | |
439 | ||
a6affd24 RS |
440 | /* Reserved labels may not be set */ |
441 | if (index < MPLS_LABEL_FIRST_UNRESERVED) | |
a2519929 EB |
442 | goto errout; |
443 | ||
444 | /* The full 20 bit range may not be supported. */ | |
445 | if (index >= net->mpls.platform_labels) | |
446 | goto errout; | |
447 | ||
448 | /* Ensure only a supported number of labels are present */ | |
449 | if (cfg->rc_output_labels > MAX_NEW_LABELS) | |
450 | goto errout; | |
451 | ||
01faef2c | 452 | dev = find_outdev(net, cfg); |
bf21563a RP |
453 | if (IS_ERR(dev)) { |
454 | err = PTR_ERR(dev); | |
455 | dev = NULL; | |
a2519929 | 456 | goto errout; |
bf21563a | 457 | } |
a2519929 | 458 | |
03c57747 | 459 | /* Ensure this is a supported device */ |
a2519929 | 460 | err = -EINVAL; |
03c57747 | 461 | if (!mpls_dev_get(dev)) |
a2519929 EB |
462 | goto errout; |
463 | ||
464 | err = -EINVAL; | |
b79bda3d | 465 | if ((cfg->rc_via_table == NEIGH_LINK_TABLE) && |
a2519929 EB |
466 | (dev->addr_len != cfg->rc_via_alen)) |
467 | goto errout; | |
468 | ||
469 | /* Append makes no sense with mpls */ | |
0f7bbd58 | 470 | err = -EOPNOTSUPP; |
a2519929 EB |
471 | if (cfg->rc_nlflags & NLM_F_APPEND) |
472 | goto errout; | |
473 | ||
474 | err = -EEXIST; | |
19d0c341 EB |
475 | platform_label = rtnl_dereference(net->mpls.platform_label); |
476 | old = rtnl_dereference(platform_label[index]); | |
a2519929 EB |
477 | if ((cfg->rc_nlflags & NLM_F_EXCL) && old) |
478 | goto errout; | |
479 | ||
480 | err = -EEXIST; | |
481 | if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old) | |
482 | goto errout; | |
483 | ||
484 | err = -ENOENT; | |
485 | if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old) | |
486 | goto errout; | |
487 | ||
488 | err = -ENOMEM; | |
489 | rt = mpls_rt_alloc(cfg->rc_via_alen); | |
490 | if (!rt) | |
491 | goto errout; | |
492 | ||
493 | rt->rt_labels = cfg->rc_output_labels; | |
494 | for (i = 0; i < rt->rt_labels; i++) | |
495 | rt->rt_label[i] = cfg->rc_output_label[i]; | |
496 | rt->rt_protocol = cfg->rc_protocol; | |
19d0c341 | 497 | RCU_INIT_POINTER(rt->rt_dev, dev); |
b79bda3d | 498 | rt->rt_via_table = cfg->rc_via_table; |
a2519929 EB |
499 | memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen); |
500 | ||
501 | mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo); | |
502 | ||
503 | dev_put(dev); | |
504 | return 0; | |
505 | ||
506 | errout: | |
507 | if (dev) | |
508 | dev_put(dev); | |
509 | return err; | |
510 | } | |
511 | ||
512 | static int mpls_route_del(struct mpls_route_config *cfg) | |
513 | { | |
514 | struct net *net = cfg->rc_nlinfo.nl_net; | |
515 | unsigned index; | |
516 | int err = -EINVAL; | |
517 | ||
518 | index = cfg->rc_label; | |
519 | ||
a6affd24 RS |
520 | /* Reserved labels may not be removed */ |
521 | if (index < MPLS_LABEL_FIRST_UNRESERVED) | |
a2519929 EB |
522 | goto errout; |
523 | ||
524 | /* The full 20 bit range may not be supported */ | |
525 | if (index >= net->mpls.platform_labels) | |
526 | goto errout; | |
527 | ||
528 | mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo); | |
529 | ||
530 | err = 0; | |
531 | errout: | |
532 | return err; | |
533 | } | |
534 | ||
37bde799 RS |
535 | #define MPLS_PERDEV_SYSCTL_OFFSET(field) \ |
536 | (&((struct mpls_dev *)0)->field) | |
537 | ||
538 | static const struct ctl_table mpls_dev_table[] = { | |
539 | { | |
540 | .procname = "input", | |
541 | .maxlen = sizeof(int), | |
542 | .mode = 0644, | |
543 | .proc_handler = proc_dointvec, | |
544 | .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled), | |
545 | }, | |
546 | { } | |
547 | }; | |
548 | ||
549 | static int mpls_dev_sysctl_register(struct net_device *dev, | |
550 | struct mpls_dev *mdev) | |
551 | { | |
552 | char path[sizeof("net/mpls/conf/") + IFNAMSIZ]; | |
553 | struct ctl_table *table; | |
554 | int i; | |
555 | ||
556 | table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL); | |
557 | if (!table) | |
558 | goto out; | |
559 | ||
560 | /* Table data contains only offsets relative to the base of | |
561 | * the mdev at this point, so make them absolute. | |
562 | */ | |
563 | for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) | |
564 | table[i].data = (char *)mdev + (uintptr_t)table[i].data; | |
565 | ||
566 | snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name); | |
567 | ||
568 | mdev->sysctl = register_net_sysctl(dev_net(dev), path, table); | |
569 | if (!mdev->sysctl) | |
570 | goto free; | |
571 | ||
572 | return 0; | |
573 | ||
574 | free: | |
575 | kfree(table); | |
576 | out: | |
577 | return -ENOBUFS; | |
578 | } | |
579 | ||
580 | static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev) | |
581 | { | |
582 | struct ctl_table *table; | |
583 | ||
584 | table = mdev->sysctl->ctl_table_arg; | |
585 | unregister_net_sysctl_table(mdev->sysctl); | |
586 | kfree(table); | |
587 | } | |
588 | ||
03c57747 RS |
589 | static struct mpls_dev *mpls_add_dev(struct net_device *dev) |
590 | { | |
591 | struct mpls_dev *mdev; | |
592 | int err = -ENOMEM; | |
593 | ||
594 | ASSERT_RTNL(); | |
595 | ||
596 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
597 | if (!mdev) | |
598 | return ERR_PTR(err); | |
599 | ||
37bde799 RS |
600 | err = mpls_dev_sysctl_register(dev, mdev); |
601 | if (err) | |
602 | goto free; | |
603 | ||
03c57747 RS |
604 | rcu_assign_pointer(dev->mpls_ptr, mdev); |
605 | ||
606 | return mdev; | |
37bde799 RS |
607 | |
608 | free: | |
609 | kfree(mdev); | |
610 | return ERR_PTR(err); | |
03c57747 RS |
611 | } |
612 | ||
0189197f EB |
613 | static void mpls_ifdown(struct net_device *dev) |
614 | { | |
19d0c341 | 615 | struct mpls_route __rcu **platform_label; |
0189197f | 616 | struct net *net = dev_net(dev); |
03c57747 | 617 | struct mpls_dev *mdev; |
0189197f EB |
618 | unsigned index; |
619 | ||
19d0c341 | 620 | platform_label = rtnl_dereference(net->mpls.platform_label); |
0189197f | 621 | for (index = 0; index < net->mpls.platform_labels; index++) { |
19d0c341 | 622 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
0189197f EB |
623 | if (!rt) |
624 | continue; | |
19d0c341 | 625 | if (rtnl_dereference(rt->rt_dev) != dev) |
0189197f EB |
626 | continue; |
627 | rt->rt_dev = NULL; | |
628 | } | |
03c57747 RS |
629 | |
630 | mdev = mpls_dev_get(dev); | |
631 | if (!mdev) | |
632 | return; | |
633 | ||
37bde799 RS |
634 | mpls_dev_sysctl_unregister(mdev); |
635 | ||
03c57747 RS |
636 | RCU_INIT_POINTER(dev->mpls_ptr, NULL); |
637 | ||
25cc8f07 | 638 | kfree_rcu(mdev, rcu); |
0189197f EB |
639 | } |
640 | ||
641 | static int mpls_dev_notify(struct notifier_block *this, unsigned long event, | |
642 | void *ptr) | |
643 | { | |
644 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
03c57747 | 645 | struct mpls_dev *mdev; |
0189197f EB |
646 | |
647 | switch(event) { | |
03c57747 RS |
648 | case NETDEV_REGISTER: |
649 | /* For now just support ethernet devices */ | |
650 | if ((dev->type == ARPHRD_ETHER) || | |
651 | (dev->type == ARPHRD_LOOPBACK)) { | |
652 | mdev = mpls_add_dev(dev); | |
653 | if (IS_ERR(mdev)) | |
654 | return notifier_from_errno(PTR_ERR(mdev)); | |
655 | } | |
656 | break; | |
657 | ||
0189197f EB |
658 | case NETDEV_UNREGISTER: |
659 | mpls_ifdown(dev); | |
660 | break; | |
0fae3bf0 RS |
661 | case NETDEV_CHANGENAME: |
662 | mdev = mpls_dev_get(dev); | |
663 | if (mdev) { | |
664 | int err; | |
665 | ||
666 | mpls_dev_sysctl_unregister(mdev); | |
667 | err = mpls_dev_sysctl_register(dev, mdev); | |
668 | if (err) | |
669 | return notifier_from_errno(err); | |
670 | } | |
671 | break; | |
0189197f EB |
672 | } |
673 | return NOTIFY_OK; | |
674 | } | |
675 | ||
676 | static struct notifier_block mpls_dev_notifier = { | |
677 | .notifier_call = mpls_dev_notify, | |
678 | }; | |
679 | ||
03c05665 | 680 | static int nla_put_via(struct sk_buff *skb, |
b79bda3d | 681 | u8 table, const void *addr, int alen) |
03c05665 | 682 | { |
b79bda3d EB |
683 | static const int table_to_family[NEIGH_NR_TABLES + 1] = { |
684 | AF_INET, AF_INET6, AF_DECnet, AF_PACKET, | |
685 | }; | |
03c05665 EB |
686 | struct nlattr *nla; |
687 | struct rtvia *via; | |
b79bda3d | 688 | int family = AF_UNSPEC; |
03c05665 EB |
689 | |
690 | nla = nla_reserve(skb, RTA_VIA, alen + 2); | |
691 | if (!nla) | |
692 | return -EMSGSIZE; | |
693 | ||
b79bda3d EB |
694 | if (table <= NEIGH_NR_TABLES) |
695 | family = table_to_family[table]; | |
696 | ||
03c05665 EB |
697 | via = nla_data(nla); |
698 | via->rtvia_family = family; | |
699 | memcpy(via->rtvia_addr, addr, alen); | |
700 | return 0; | |
701 | } | |
702 | ||
966bae33 EB |
703 | int nla_put_labels(struct sk_buff *skb, int attrtype, |
704 | u8 labels, const u32 label[]) | |
705 | { | |
706 | struct nlattr *nla; | |
707 | struct mpls_shim_hdr *nla_label; | |
708 | bool bos; | |
709 | int i; | |
710 | nla = nla_reserve(skb, attrtype, labels*4); | |
711 | if (!nla) | |
712 | return -EMSGSIZE; | |
713 | ||
714 | nla_label = nla_data(nla); | |
715 | bos = true; | |
716 | for (i = labels - 1; i >= 0; i--) { | |
717 | nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos); | |
718 | bos = false; | |
719 | } | |
720 | ||
721 | return 0; | |
722 | } | |
face0188 | 723 | EXPORT_SYMBOL_GPL(nla_put_labels); |
966bae33 EB |
724 | |
725 | int nla_get_labels(const struct nlattr *nla, | |
726 | u32 max_labels, u32 *labels, u32 label[]) | |
727 | { | |
728 | unsigned len = nla_len(nla); | |
729 | unsigned nla_labels; | |
730 | struct mpls_shim_hdr *nla_label; | |
731 | bool bos; | |
732 | int i; | |
733 | ||
734 | /* len needs to be an even multiple of 4 (the label size) */ | |
735 | if (len & 3) | |
736 | return -EINVAL; | |
737 | ||
738 | /* Limit the number of new labels allowed */ | |
739 | nla_labels = len/4; | |
740 | if (nla_labels > max_labels) | |
741 | return -EINVAL; | |
742 | ||
743 | nla_label = nla_data(nla); | |
744 | bos = true; | |
745 | for (i = nla_labels - 1; i >= 0; i--, bos = false) { | |
746 | struct mpls_entry_decoded dec; | |
747 | dec = mpls_entry_decode(nla_label + i); | |
748 | ||
749 | /* Ensure the bottom of stack flag is properly set | |
750 | * and ttl and tc are both clear. | |
751 | */ | |
752 | if ((dec.bos != bos) || dec.ttl || dec.tc) | |
753 | return -EINVAL; | |
754 | ||
5a9ab017 | 755 | switch (dec.label) { |
78f5b899 | 756 | case MPLS_LABEL_IMPLNULL: |
5a9ab017 RS |
757 | /* RFC3032: This is a label that an LSR may |
758 | * assign and distribute, but which never | |
759 | * actually appears in the encapsulation. | |
760 | */ | |
761 | return -EINVAL; | |
762 | } | |
763 | ||
966bae33 EB |
764 | label[i] = dec.label; |
765 | } | |
766 | *labels = nla_labels; | |
767 | return 0; | |
768 | } | |
face0188 | 769 | EXPORT_SYMBOL_GPL(nla_get_labels); |
966bae33 | 770 | |
03c05665 EB |
771 | static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
772 | struct mpls_route_config *cfg) | |
773 | { | |
774 | struct rtmsg *rtm; | |
775 | struct nlattr *tb[RTA_MAX+1]; | |
776 | int index; | |
777 | int err; | |
778 | ||
779 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy); | |
780 | if (err < 0) | |
781 | goto errout; | |
782 | ||
783 | err = -EINVAL; | |
784 | rtm = nlmsg_data(nlh); | |
785 | memset(cfg, 0, sizeof(*cfg)); | |
786 | ||
787 | if (rtm->rtm_family != AF_MPLS) | |
788 | goto errout; | |
789 | if (rtm->rtm_dst_len != 20) | |
790 | goto errout; | |
791 | if (rtm->rtm_src_len != 0) | |
792 | goto errout; | |
793 | if (rtm->rtm_tos != 0) | |
794 | goto errout; | |
795 | if (rtm->rtm_table != RT_TABLE_MAIN) | |
796 | goto errout; | |
797 | /* Any value is acceptable for rtm_protocol */ | |
798 | ||
799 | /* As mpls uses destination specific addresses | |
800 | * (or source specific address in the case of multicast) | |
801 | * all addresses have universal scope. | |
802 | */ | |
803 | if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) | |
804 | goto errout; | |
805 | if (rtm->rtm_type != RTN_UNICAST) | |
806 | goto errout; | |
807 | if (rtm->rtm_flags != 0) | |
808 | goto errout; | |
809 | ||
810 | cfg->rc_label = LABEL_NOT_SPECIFIED; | |
811 | cfg->rc_protocol = rtm->rtm_protocol; | |
812 | cfg->rc_nlflags = nlh->nlmsg_flags; | |
813 | cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid; | |
814 | cfg->rc_nlinfo.nlh = nlh; | |
815 | cfg->rc_nlinfo.nl_net = sock_net(skb->sk); | |
816 | ||
817 | for (index = 0; index <= RTA_MAX; index++) { | |
818 | struct nlattr *nla = tb[index]; | |
819 | if (!nla) | |
820 | continue; | |
821 | ||
822 | switch(index) { | |
823 | case RTA_OIF: | |
824 | cfg->rc_ifindex = nla_get_u32(nla); | |
825 | break; | |
826 | case RTA_NEWDST: | |
827 | if (nla_get_labels(nla, MAX_NEW_LABELS, | |
828 | &cfg->rc_output_labels, | |
829 | cfg->rc_output_label)) | |
830 | goto errout; | |
831 | break; | |
832 | case RTA_DST: | |
833 | { | |
834 | u32 label_count; | |
835 | if (nla_get_labels(nla, 1, &label_count, | |
836 | &cfg->rc_label)) | |
837 | goto errout; | |
838 | ||
a6affd24 RS |
839 | /* Reserved labels may not be set */ |
840 | if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED) | |
03c05665 EB |
841 | goto errout; |
842 | ||
843 | break; | |
844 | } | |
845 | case RTA_VIA: | |
846 | { | |
847 | struct rtvia *via = nla_data(nla); | |
f8d54afc RS |
848 | if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) |
849 | goto errout; | |
f8d54afc RS |
850 | cfg->rc_via_alen = nla_len(nla) - |
851 | offsetof(struct rtvia, rtvia_addr); | |
03c05665 EB |
852 | if (cfg->rc_via_alen > MAX_VIA_ALEN) |
853 | goto errout; | |
854 | ||
855 | /* Validate the address family */ | |
b79bda3d | 856 | switch(via->rtvia_family) { |
03c05665 | 857 | case AF_PACKET: |
b79bda3d | 858 | cfg->rc_via_table = NEIGH_LINK_TABLE; |
03c05665 EB |
859 | break; |
860 | case AF_INET: | |
b79bda3d | 861 | cfg->rc_via_table = NEIGH_ARP_TABLE; |
03c05665 EB |
862 | if (cfg->rc_via_alen != 4) |
863 | goto errout; | |
864 | break; | |
865 | case AF_INET6: | |
b79bda3d | 866 | cfg->rc_via_table = NEIGH_ND_TABLE; |
03c05665 EB |
867 | if (cfg->rc_via_alen != 16) |
868 | goto errout; | |
869 | break; | |
870 | default: | |
871 | /* Unsupported address family */ | |
872 | goto errout; | |
873 | } | |
874 | ||
875 | memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen); | |
876 | break; | |
877 | } | |
878 | default: | |
879 | /* Unsupported attribute */ | |
880 | goto errout; | |
881 | } | |
882 | } | |
883 | ||
884 | err = 0; | |
885 | errout: | |
886 | return err; | |
887 | } | |
888 | ||
889 | static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
890 | { | |
891 | struct mpls_route_config cfg; | |
892 | int err; | |
893 | ||
894 | err = rtm_to_route_config(skb, nlh, &cfg); | |
895 | if (err < 0) | |
896 | return err; | |
897 | ||
898 | return mpls_route_del(&cfg); | |
899 | } | |
900 | ||
901 | ||
902 | static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
903 | { | |
904 | struct mpls_route_config cfg; | |
905 | int err; | |
906 | ||
907 | err = rtm_to_route_config(skb, nlh, &cfg); | |
908 | if (err < 0) | |
909 | return err; | |
910 | ||
911 | return mpls_route_add(&cfg); | |
912 | } | |
913 | ||
914 | static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event, | |
915 | u32 label, struct mpls_route *rt, int flags) | |
916 | { | |
19d0c341 | 917 | struct net_device *dev; |
03c05665 EB |
918 | struct nlmsghdr *nlh; |
919 | struct rtmsg *rtm; | |
920 | ||
921 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); | |
922 | if (nlh == NULL) | |
923 | return -EMSGSIZE; | |
924 | ||
925 | rtm = nlmsg_data(nlh); | |
926 | rtm->rtm_family = AF_MPLS; | |
927 | rtm->rtm_dst_len = 20; | |
928 | rtm->rtm_src_len = 0; | |
929 | rtm->rtm_tos = 0; | |
930 | rtm->rtm_table = RT_TABLE_MAIN; | |
931 | rtm->rtm_protocol = rt->rt_protocol; | |
932 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
933 | rtm->rtm_type = RTN_UNICAST; | |
934 | rtm->rtm_flags = 0; | |
935 | ||
936 | if (rt->rt_labels && | |
937 | nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label)) | |
938 | goto nla_put_failure; | |
b79bda3d | 939 | if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen)) |
03c05665 | 940 | goto nla_put_failure; |
19d0c341 EB |
941 | dev = rtnl_dereference(rt->rt_dev); |
942 | if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex)) | |
03c05665 EB |
943 | goto nla_put_failure; |
944 | if (nla_put_labels(skb, RTA_DST, 1, &label)) | |
945 | goto nla_put_failure; | |
946 | ||
947 | nlmsg_end(skb, nlh); | |
948 | return 0; | |
949 | ||
950 | nla_put_failure: | |
951 | nlmsg_cancel(skb, nlh); | |
952 | return -EMSGSIZE; | |
953 | } | |
954 | ||
955 | static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb) | |
956 | { | |
957 | struct net *net = sock_net(skb->sk); | |
19d0c341 EB |
958 | struct mpls_route __rcu **platform_label; |
959 | size_t platform_labels; | |
03c05665 EB |
960 | unsigned int index; |
961 | ||
962 | ASSERT_RTNL(); | |
963 | ||
964 | index = cb->args[0]; | |
a6affd24 RS |
965 | if (index < MPLS_LABEL_FIRST_UNRESERVED) |
966 | index = MPLS_LABEL_FIRST_UNRESERVED; | |
03c05665 | 967 | |
19d0c341 EB |
968 | platform_label = rtnl_dereference(net->mpls.platform_label); |
969 | platform_labels = net->mpls.platform_labels; | |
970 | for (; index < platform_labels; index++) { | |
03c05665 | 971 | struct mpls_route *rt; |
19d0c341 | 972 | rt = rtnl_dereference(platform_label[index]); |
03c05665 EB |
973 | if (!rt) |
974 | continue; | |
975 | ||
976 | if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid, | |
977 | cb->nlh->nlmsg_seq, RTM_NEWROUTE, | |
978 | index, rt, NLM_F_MULTI) < 0) | |
979 | break; | |
980 | } | |
981 | cb->args[0] = index; | |
982 | ||
983 | return skb->len; | |
984 | } | |
985 | ||
8de147dc EB |
986 | static inline size_t lfib_nlmsg_size(struct mpls_route *rt) |
987 | { | |
988 | size_t payload = | |
989 | NLMSG_ALIGN(sizeof(struct rtmsg)) | |
990 | + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */ | |
991 | + nla_total_size(4); /* RTA_DST */ | |
992 | if (rt->rt_labels) /* RTA_NEWDST */ | |
993 | payload += nla_total_size(rt->rt_labels * 4); | |
994 | if (rt->rt_dev) /* RTA_OIF */ | |
995 | payload += nla_total_size(4); | |
996 | return payload; | |
997 | } | |
998 | ||
999 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, | |
1000 | struct nlmsghdr *nlh, struct net *net, u32 portid, | |
1001 | unsigned int nlm_flags) | |
1002 | { | |
1003 | struct sk_buff *skb; | |
1004 | u32 seq = nlh ? nlh->nlmsg_seq : 0; | |
1005 | int err = -ENOBUFS; | |
1006 | ||
1007 | skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL); | |
1008 | if (skb == NULL) | |
1009 | goto errout; | |
1010 | ||
1011 | err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags); | |
1012 | if (err < 0) { | |
1013 | /* -EMSGSIZE implies BUG in lfib_nlmsg_size */ | |
1014 | WARN_ON(err == -EMSGSIZE); | |
1015 | kfree_skb(skb); | |
1016 | goto errout; | |
1017 | } | |
1018 | rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL); | |
1019 | ||
1020 | return; | |
1021 | errout: | |
1022 | if (err < 0) | |
1023 | rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err); | |
1024 | } | |
1025 | ||
7720c01f EB |
1026 | static int resize_platform_label_table(struct net *net, size_t limit) |
1027 | { | |
1028 | size_t size = sizeof(struct mpls_route *) * limit; | |
1029 | size_t old_limit; | |
1030 | size_t cp_size; | |
1031 | struct mpls_route __rcu **labels = NULL, **old; | |
1032 | struct mpls_route *rt0 = NULL, *rt2 = NULL; | |
1033 | unsigned index; | |
1034 | ||
1035 | if (size) { | |
1036 | labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); | |
1037 | if (!labels) | |
1038 | labels = vzalloc(size); | |
1039 | ||
1040 | if (!labels) | |
1041 | goto nolabels; | |
1042 | } | |
1043 | ||
1044 | /* In case the predefined labels need to be populated */ | |
78f5b899 | 1045 | if (limit > MPLS_LABEL_IPV4NULL) { |
7720c01f EB |
1046 | struct net_device *lo = net->loopback_dev; |
1047 | rt0 = mpls_rt_alloc(lo->addr_len); | |
1048 | if (!rt0) | |
1049 | goto nort0; | |
19d0c341 | 1050 | RCU_INIT_POINTER(rt0->rt_dev, lo); |
7720c01f | 1051 | rt0->rt_protocol = RTPROT_KERNEL; |
b79bda3d | 1052 | rt0->rt_via_table = NEIGH_LINK_TABLE; |
7720c01f EB |
1053 | memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len); |
1054 | } | |
78f5b899 | 1055 | if (limit > MPLS_LABEL_IPV6NULL) { |
7720c01f EB |
1056 | struct net_device *lo = net->loopback_dev; |
1057 | rt2 = mpls_rt_alloc(lo->addr_len); | |
1058 | if (!rt2) | |
1059 | goto nort2; | |
19d0c341 | 1060 | RCU_INIT_POINTER(rt2->rt_dev, lo); |
7720c01f | 1061 | rt2->rt_protocol = RTPROT_KERNEL; |
b79bda3d | 1062 | rt2->rt_via_table = NEIGH_LINK_TABLE; |
7720c01f EB |
1063 | memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len); |
1064 | } | |
1065 | ||
1066 | rtnl_lock(); | |
1067 | /* Remember the original table */ | |
19d0c341 | 1068 | old = rtnl_dereference(net->mpls.platform_label); |
7720c01f EB |
1069 | old_limit = net->mpls.platform_labels; |
1070 | ||
1071 | /* Free any labels beyond the new table */ | |
1072 | for (index = limit; index < old_limit; index++) | |
1073 | mpls_route_update(net, index, NULL, NULL, NULL); | |
1074 | ||
1075 | /* Copy over the old labels */ | |
1076 | cp_size = size; | |
1077 | if (old_limit < limit) | |
1078 | cp_size = old_limit * sizeof(struct mpls_route *); | |
1079 | ||
1080 | memcpy(labels, old, cp_size); | |
1081 | ||
1082 | /* If needed set the predefined labels */ | |
78f5b899 TH |
1083 | if ((old_limit <= MPLS_LABEL_IPV6NULL) && |
1084 | (limit > MPLS_LABEL_IPV6NULL)) { | |
1085 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2); | |
7720c01f EB |
1086 | rt2 = NULL; |
1087 | } | |
1088 | ||
78f5b899 TH |
1089 | if ((old_limit <= MPLS_LABEL_IPV4NULL) && |
1090 | (limit > MPLS_LABEL_IPV4NULL)) { | |
1091 | RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0); | |
7720c01f EB |
1092 | rt0 = NULL; |
1093 | } | |
1094 | ||
1095 | /* Update the global pointers */ | |
1096 | net->mpls.platform_labels = limit; | |
19d0c341 | 1097 | rcu_assign_pointer(net->mpls.platform_label, labels); |
7720c01f EB |
1098 | |
1099 | rtnl_unlock(); | |
1100 | ||
1101 | mpls_rt_free(rt2); | |
1102 | mpls_rt_free(rt0); | |
1103 | ||
1104 | if (old) { | |
1105 | synchronize_rcu(); | |
1106 | kvfree(old); | |
1107 | } | |
1108 | return 0; | |
1109 | ||
1110 | nort2: | |
1111 | mpls_rt_free(rt0); | |
1112 | nort0: | |
1113 | kvfree(labels); | |
1114 | nolabels: | |
1115 | return -ENOMEM; | |
1116 | } | |
1117 | ||
1118 | static int mpls_platform_labels(struct ctl_table *table, int write, | |
1119 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
1120 | { | |
1121 | struct net *net = table->data; | |
1122 | int platform_labels = net->mpls.platform_labels; | |
1123 | int ret; | |
1124 | struct ctl_table tmp = { | |
1125 | .procname = table->procname, | |
1126 | .data = &platform_labels, | |
1127 | .maxlen = sizeof(int), | |
1128 | .mode = table->mode, | |
1129 | .extra1 = &zero, | |
1130 | .extra2 = &label_limit, | |
1131 | }; | |
1132 | ||
1133 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); | |
1134 | ||
1135 | if (write && ret == 0) | |
1136 | ret = resize_platform_label_table(net, platform_labels); | |
1137 | ||
1138 | return ret; | |
1139 | } | |
1140 | ||
37bde799 | 1141 | static const struct ctl_table mpls_table[] = { |
7720c01f EB |
1142 | { |
1143 | .procname = "platform_labels", | |
1144 | .data = NULL, | |
1145 | .maxlen = sizeof(int), | |
1146 | .mode = 0644, | |
1147 | .proc_handler = mpls_platform_labels, | |
1148 | }, | |
1149 | { } | |
1150 | }; | |
1151 | ||
0189197f EB |
1152 | static int mpls_net_init(struct net *net) |
1153 | { | |
7720c01f EB |
1154 | struct ctl_table *table; |
1155 | ||
0189197f EB |
1156 | net->mpls.platform_labels = 0; |
1157 | net->mpls.platform_label = NULL; | |
1158 | ||
7720c01f EB |
1159 | table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL); |
1160 | if (table == NULL) | |
1161 | return -ENOMEM; | |
1162 | ||
1163 | table[0].data = net; | |
1164 | net->mpls.ctl = register_net_sysctl(net, "net/mpls", table); | |
1165 | if (net->mpls.ctl == NULL) | |
1166 | return -ENOMEM; | |
1167 | ||
0189197f EB |
1168 | return 0; |
1169 | } | |
1170 | ||
1171 | static void mpls_net_exit(struct net *net) | |
1172 | { | |
19d0c341 EB |
1173 | struct mpls_route __rcu **platform_label; |
1174 | size_t platform_labels; | |
7720c01f | 1175 | struct ctl_table *table; |
0189197f EB |
1176 | unsigned int index; |
1177 | ||
7720c01f EB |
1178 | table = net->mpls.ctl->ctl_table_arg; |
1179 | unregister_net_sysctl_table(net->mpls.ctl); | |
1180 | kfree(table); | |
1181 | ||
19d0c341 EB |
1182 | /* An rcu grace period has passed since there was a device in |
1183 | * the network namespace (and thus the last in flight packet) | |
0189197f EB |
1184 | * left this network namespace. This is because |
1185 | * unregister_netdevice_many and netdev_run_todo has completed | |
1186 | * for each network device that was in this network namespace. | |
1187 | * | |
1188 | * As such no additional rcu synchronization is necessary when | |
1189 | * freeing the platform_label table. | |
1190 | */ | |
1191 | rtnl_lock(); | |
19d0c341 EB |
1192 | platform_label = rtnl_dereference(net->mpls.platform_label); |
1193 | platform_labels = net->mpls.platform_labels; | |
1194 | for (index = 0; index < platform_labels; index++) { | |
1195 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); | |
1196 | RCU_INIT_POINTER(platform_label[index], NULL); | |
0189197f EB |
1197 | mpls_rt_free(rt); |
1198 | } | |
1199 | rtnl_unlock(); | |
1200 | ||
19d0c341 | 1201 | kvfree(platform_label); |
0189197f EB |
1202 | } |
1203 | ||
1204 | static struct pernet_operations mpls_net_ops = { | |
1205 | .init = mpls_net_init, | |
1206 | .exit = mpls_net_exit, | |
1207 | }; | |
1208 | ||
1209 | static int __init mpls_init(void) | |
1210 | { | |
1211 | int err; | |
1212 | ||
1213 | BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4); | |
1214 | ||
1215 | err = register_pernet_subsys(&mpls_net_ops); | |
1216 | if (err) | |
1217 | goto out; | |
1218 | ||
1219 | err = register_netdevice_notifier(&mpls_dev_notifier); | |
1220 | if (err) | |
1221 | goto out_unregister_pernet; | |
1222 | ||
1223 | dev_add_pack(&mpls_packet_type); | |
1224 | ||
03c05665 EB |
1225 | rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL); |
1226 | rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL); | |
1227 | rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL); | |
0189197f EB |
1228 | err = 0; |
1229 | out: | |
1230 | return err; | |
1231 | ||
1232 | out_unregister_pernet: | |
1233 | unregister_pernet_subsys(&mpls_net_ops); | |
1234 | goto out; | |
1235 | } | |
1236 | module_init(mpls_init); | |
1237 | ||
1238 | static void __exit mpls_exit(void) | |
1239 | { | |
03c05665 | 1240 | rtnl_unregister_all(PF_MPLS); |
0189197f EB |
1241 | dev_remove_pack(&mpls_packet_type); |
1242 | unregister_netdevice_notifier(&mpls_dev_notifier); | |
1243 | unregister_pernet_subsys(&mpls_net_ops); | |
1244 | } | |
1245 | module_exit(mpls_exit); | |
1246 | ||
1247 | MODULE_DESCRIPTION("MultiProtocol Label Switching"); | |
1248 | MODULE_LICENSE("GPL v2"); | |
1249 | MODULE_ALIAS_NETPROTO(PF_MPLS); |