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> | |
18 | #include "internal.h" | |
19 | ||
a2519929 | 20 | #define LABEL_NOT_SPECIFIED (1<<20) |
0189197f EB |
21 | #define MAX_NEW_LABELS 2 |
22 | ||
23 | /* This maximum ha length copied from the definition of struct neighbour */ | |
24 | #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))) | |
25 | ||
26 | struct mpls_route { /* next hop label forwarding entry */ | |
19d0c341 | 27 | struct net_device __rcu *rt_dev; |
0189197f EB |
28 | struct rcu_head rt_rcu; |
29 | u32 rt_label[MAX_NEW_LABELS]; | |
30 | u8 rt_protocol; /* routing protocol that set this entry */ | |
b79bda3d EB |
31 | u8 rt_labels; |
32 | u8 rt_via_alen; | |
33 | u8 rt_via_table; | |
0189197f EB |
34 | u8 rt_via[0]; |
35 | }; | |
36 | ||
7720c01f EB |
37 | static int zero = 0; |
38 | static int label_limit = (1 << 20) - 1; | |
39 | ||
8de147dc EB |
40 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, |
41 | struct nlmsghdr *nlh, struct net *net, u32 portid, | |
42 | unsigned int nlm_flags); | |
43 | ||
0189197f EB |
44 | static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index) |
45 | { | |
46 | struct mpls_route *rt = NULL; | |
47 | ||
48 | if (index < net->mpls.platform_labels) { | |
49 | struct mpls_route __rcu **platform_label = | |
50 | rcu_dereference(net->mpls.platform_label); | |
51 | rt = rcu_dereference(platform_label[index]); | |
52 | } | |
53 | return rt; | |
54 | } | |
55 | ||
03c57747 RS |
56 | static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev) |
57 | { | |
58 | return rcu_dereference_rtnl(dev->mpls_ptr); | |
59 | } | |
60 | ||
0189197f EB |
61 | static bool mpls_output_possible(const struct net_device *dev) |
62 | { | |
63 | return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev); | |
64 | } | |
65 | ||
66 | static unsigned int mpls_rt_header_size(const struct mpls_route *rt) | |
67 | { | |
68 | /* The size of the layer 2.5 labels to be added for this route */ | |
69 | return rt->rt_labels * sizeof(struct mpls_shim_hdr); | |
70 | } | |
71 | ||
72 | static unsigned int mpls_dev_mtu(const struct net_device *dev) | |
73 | { | |
74 | /* The amount of data the layer 2 frame can hold */ | |
75 | return dev->mtu; | |
76 | } | |
77 | ||
78 | static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) | |
79 | { | |
80 | if (skb->len <= mtu) | |
81 | return false; | |
82 | ||
83 | if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu) | |
84 | return false; | |
85 | ||
86 | return true; | |
87 | } | |
88 | ||
89 | static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb, | |
90 | struct mpls_entry_decoded dec) | |
91 | { | |
92 | /* RFC4385 and RFC5586 encode other packets in mpls such that | |
93 | * they don't conflict with the ip version number, making | |
94 | * decoding by examining the ip version correct in everything | |
95 | * except for the strangest cases. | |
96 | * | |
97 | * The strange cases if we choose to support them will require | |
98 | * manual configuration. | |
99 | */ | |
76fecd82 | 100 | struct iphdr *hdr4; |
0189197f EB |
101 | bool success = true; |
102 | ||
76fecd82 EB |
103 | /* The IPv4 code below accesses through the IPv4 header |
104 | * checksum, which is 12 bytes into the packet. | |
105 | * The IPv6 code below accesses through the IPv6 hop limit | |
106 | * which is 8 bytes into the packet. | |
107 | * | |
108 | * For all supported cases there should always be at least 12 | |
109 | * bytes of packet data present. The IPv4 header is 20 bytes | |
110 | * without options and the IPv6 header is always 40 bytes | |
111 | * long. | |
112 | */ | |
113 | if (!pskb_may_pull(skb, 12)) | |
114 | return false; | |
115 | ||
116 | /* Use ip_hdr to find the ip protocol version */ | |
117 | hdr4 = ip_hdr(skb); | |
0189197f EB |
118 | if (hdr4->version == 4) { |
119 | skb->protocol = htons(ETH_P_IP); | |
120 | csum_replace2(&hdr4->check, | |
121 | htons(hdr4->ttl << 8), | |
122 | htons(dec.ttl << 8)); | |
123 | hdr4->ttl = dec.ttl; | |
124 | } | |
125 | else if (hdr4->version == 6) { | |
126 | struct ipv6hdr *hdr6 = ipv6_hdr(skb); | |
127 | skb->protocol = htons(ETH_P_IPV6); | |
128 | hdr6->hop_limit = dec.ttl; | |
129 | } | |
130 | else | |
131 | /* version 0 and version 1 are used by pseudo wires */ | |
132 | success = false; | |
133 | return success; | |
134 | } | |
135 | ||
136 | static int mpls_forward(struct sk_buff *skb, struct net_device *dev, | |
137 | struct packet_type *pt, struct net_device *orig_dev) | |
138 | { | |
139 | struct net *net = dev_net(dev); | |
140 | struct mpls_shim_hdr *hdr; | |
141 | struct mpls_route *rt; | |
142 | struct mpls_entry_decoded dec; | |
143 | struct net_device *out_dev; | |
03c57747 | 144 | struct mpls_dev *mdev; |
0189197f EB |
145 | unsigned int hh_len; |
146 | unsigned int new_header_size; | |
147 | unsigned int mtu; | |
148 | int err; | |
149 | ||
150 | /* Careful this entire function runs inside of an rcu critical section */ | |
151 | ||
03c57747 RS |
152 | mdev = mpls_dev_get(dev); |
153 | if (!mdev) | |
154 | goto drop; | |
155 | ||
0189197f EB |
156 | if (skb->pkt_type != PACKET_HOST) |
157 | goto drop; | |
158 | ||
159 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) | |
160 | goto drop; | |
161 | ||
162 | if (!pskb_may_pull(skb, sizeof(*hdr))) | |
163 | goto drop; | |
164 | ||
165 | /* Read and decode the label */ | |
166 | hdr = mpls_hdr(skb); | |
167 | dec = mpls_entry_decode(hdr); | |
168 | ||
169 | /* Pop the label */ | |
170 | skb_pull(skb, sizeof(*hdr)); | |
171 | skb_reset_network_header(skb); | |
172 | ||
173 | skb_orphan(skb); | |
174 | ||
175 | rt = mpls_route_input_rcu(net, dec.label); | |
176 | if (!rt) | |
177 | goto drop; | |
178 | ||
179 | /* Find the output device */ | |
19d0c341 | 180 | out_dev = rcu_dereference(rt->rt_dev); |
0189197f EB |
181 | if (!mpls_output_possible(out_dev)) |
182 | goto drop; | |
183 | ||
184 | if (skb_warn_if_lro(skb)) | |
185 | goto drop; | |
186 | ||
187 | skb_forward_csum(skb); | |
188 | ||
189 | /* Verify ttl is valid */ | |
aa7da937 | 190 | if (dec.ttl <= 1) |
0189197f EB |
191 | goto drop; |
192 | dec.ttl -= 1; | |
193 | ||
194 | /* Verify the destination can hold the packet */ | |
195 | new_header_size = mpls_rt_header_size(rt); | |
196 | mtu = mpls_dev_mtu(out_dev); | |
197 | if (mpls_pkt_too_big(skb, mtu - new_header_size)) | |
198 | goto drop; | |
199 | ||
200 | hh_len = LL_RESERVED_SPACE(out_dev); | |
201 | if (!out_dev->header_ops) | |
202 | hh_len = 0; | |
203 | ||
204 | /* Ensure there is enough space for the headers in the skb */ | |
205 | if (skb_cow(skb, hh_len + new_header_size)) | |
206 | goto drop; | |
207 | ||
208 | skb->dev = out_dev; | |
209 | skb->protocol = htons(ETH_P_MPLS_UC); | |
210 | ||
211 | if (unlikely(!new_header_size && dec.bos)) { | |
212 | /* Penultimate hop popping */ | |
213 | if (!mpls_egress(rt, skb, dec)) | |
214 | goto drop; | |
215 | } else { | |
216 | bool bos; | |
217 | int i; | |
218 | skb_push(skb, new_header_size); | |
219 | skb_reset_network_header(skb); | |
220 | /* Push the new labels */ | |
221 | hdr = mpls_hdr(skb); | |
222 | bos = dec.bos; | |
223 | for (i = rt->rt_labels - 1; i >= 0; i--) { | |
224 | hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos); | |
225 | bos = false; | |
226 | } | |
227 | } | |
228 | ||
b79bda3d | 229 | err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb); |
0189197f EB |
230 | if (err) |
231 | net_dbg_ratelimited("%s: packet transmission failed: %d\n", | |
232 | __func__, err); | |
233 | return 0; | |
234 | ||
235 | drop: | |
236 | kfree_skb(skb); | |
237 | return NET_RX_DROP; | |
238 | } | |
239 | ||
240 | static struct packet_type mpls_packet_type __read_mostly = { | |
241 | .type = cpu_to_be16(ETH_P_MPLS_UC), | |
242 | .func = mpls_forward, | |
243 | }; | |
244 | ||
f0126539 | 245 | static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = { |
03c05665 EB |
246 | [RTA_DST] = { .type = NLA_U32 }, |
247 | [RTA_OIF] = { .type = NLA_U32 }, | |
248 | }; | |
249 | ||
a2519929 EB |
250 | struct mpls_route_config { |
251 | u32 rc_protocol; | |
252 | u32 rc_ifindex; | |
b79bda3d | 253 | u16 rc_via_table; |
a2519929 EB |
254 | u16 rc_via_alen; |
255 | u8 rc_via[MAX_VIA_ALEN]; | |
256 | u32 rc_label; | |
257 | u32 rc_output_labels; | |
258 | u32 rc_output_label[MAX_NEW_LABELS]; | |
259 | u32 rc_nlflags; | |
260 | struct nl_info rc_nlinfo; | |
261 | }; | |
262 | ||
0189197f EB |
263 | static struct mpls_route *mpls_rt_alloc(size_t alen) |
264 | { | |
265 | struct mpls_route *rt; | |
266 | ||
d865616e | 267 | rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL); |
0189197f EB |
268 | if (rt) |
269 | rt->rt_via_alen = alen; | |
270 | return rt; | |
271 | } | |
272 | ||
273 | static void mpls_rt_free(struct mpls_route *rt) | |
274 | { | |
275 | if (rt) | |
276 | kfree_rcu(rt, rt_rcu); | |
277 | } | |
278 | ||
8de147dc EB |
279 | static void mpls_notify_route(struct net *net, unsigned index, |
280 | struct mpls_route *old, struct mpls_route *new, | |
281 | const struct nl_info *info) | |
282 | { | |
283 | struct nlmsghdr *nlh = info ? info->nlh : NULL; | |
284 | unsigned portid = info ? info->portid : 0; | |
285 | int event = new ? RTM_NEWROUTE : RTM_DELROUTE; | |
286 | struct mpls_route *rt = new ? new : old; | |
287 | unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0; | |
288 | /* Ignore reserved labels for now */ | |
289 | if (rt && (index >= 16)) | |
290 | rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags); | |
291 | } | |
292 | ||
0189197f EB |
293 | static void mpls_route_update(struct net *net, unsigned index, |
294 | struct net_device *dev, struct mpls_route *new, | |
295 | const struct nl_info *info) | |
296 | { | |
19d0c341 | 297 | struct mpls_route __rcu **platform_label; |
0189197f EB |
298 | struct mpls_route *rt, *old = NULL; |
299 | ||
300 | ASSERT_RTNL(); | |
301 | ||
19d0c341 EB |
302 | platform_label = rtnl_dereference(net->mpls.platform_label); |
303 | rt = rtnl_dereference(platform_label[index]); | |
304 | if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) { | |
305 | rcu_assign_pointer(platform_label[index], new); | |
0189197f EB |
306 | old = rt; |
307 | } | |
308 | ||
8de147dc EB |
309 | mpls_notify_route(net, index, old, new, info); |
310 | ||
0189197f EB |
311 | /* If we removed a route free it now */ |
312 | mpls_rt_free(old); | |
313 | } | |
314 | ||
a2519929 EB |
315 | static unsigned find_free_label(struct net *net) |
316 | { | |
19d0c341 EB |
317 | struct mpls_route __rcu **platform_label; |
318 | size_t platform_labels; | |
a2519929 | 319 | unsigned index; |
19d0c341 EB |
320 | |
321 | platform_label = rtnl_dereference(net->mpls.platform_label); | |
322 | platform_labels = net->mpls.platform_labels; | |
323 | for (index = 16; index < platform_labels; index++) { | |
324 | if (!rtnl_dereference(platform_label[index])) | |
a2519929 EB |
325 | return index; |
326 | } | |
327 | return LABEL_NOT_SPECIFIED; | |
328 | } | |
329 | ||
330 | static int mpls_route_add(struct mpls_route_config *cfg) | |
331 | { | |
19d0c341 | 332 | struct mpls_route __rcu **platform_label; |
a2519929 EB |
333 | struct net *net = cfg->rc_nlinfo.nl_net; |
334 | struct net_device *dev = NULL; | |
335 | struct mpls_route *rt, *old; | |
336 | unsigned index; | |
337 | int i; | |
338 | int err = -EINVAL; | |
339 | ||
340 | index = cfg->rc_label; | |
341 | ||
342 | /* If a label was not specified during insert pick one */ | |
343 | if ((index == LABEL_NOT_SPECIFIED) && | |
344 | (cfg->rc_nlflags & NLM_F_CREATE)) { | |
345 | index = find_free_label(net); | |
346 | } | |
347 | ||
348 | /* The first 16 labels are reserved, and may not be set */ | |
349 | if (index < 16) | |
350 | goto errout; | |
351 | ||
352 | /* The full 20 bit range may not be supported. */ | |
353 | if (index >= net->mpls.platform_labels) | |
354 | goto errout; | |
355 | ||
356 | /* Ensure only a supported number of labels are present */ | |
357 | if (cfg->rc_output_labels > MAX_NEW_LABELS) | |
358 | goto errout; | |
359 | ||
360 | err = -ENODEV; | |
361 | dev = dev_get_by_index(net, cfg->rc_ifindex); | |
362 | if (!dev) | |
363 | goto errout; | |
364 | ||
03c57747 | 365 | /* Ensure this is a supported device */ |
a2519929 | 366 | err = -EINVAL; |
03c57747 | 367 | if (!mpls_dev_get(dev)) |
a2519929 EB |
368 | goto errout; |
369 | ||
370 | err = -EINVAL; | |
b79bda3d | 371 | if ((cfg->rc_via_table == NEIGH_LINK_TABLE) && |
a2519929 EB |
372 | (dev->addr_len != cfg->rc_via_alen)) |
373 | goto errout; | |
374 | ||
375 | /* Append makes no sense with mpls */ | |
0f7bbd58 | 376 | err = -EOPNOTSUPP; |
a2519929 EB |
377 | if (cfg->rc_nlflags & NLM_F_APPEND) |
378 | goto errout; | |
379 | ||
380 | err = -EEXIST; | |
19d0c341 EB |
381 | platform_label = rtnl_dereference(net->mpls.platform_label); |
382 | old = rtnl_dereference(platform_label[index]); | |
a2519929 EB |
383 | if ((cfg->rc_nlflags & NLM_F_EXCL) && old) |
384 | goto errout; | |
385 | ||
386 | err = -EEXIST; | |
387 | if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old) | |
388 | goto errout; | |
389 | ||
390 | err = -ENOENT; | |
391 | if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old) | |
392 | goto errout; | |
393 | ||
394 | err = -ENOMEM; | |
395 | rt = mpls_rt_alloc(cfg->rc_via_alen); | |
396 | if (!rt) | |
397 | goto errout; | |
398 | ||
399 | rt->rt_labels = cfg->rc_output_labels; | |
400 | for (i = 0; i < rt->rt_labels; i++) | |
401 | rt->rt_label[i] = cfg->rc_output_label[i]; | |
402 | rt->rt_protocol = cfg->rc_protocol; | |
19d0c341 | 403 | RCU_INIT_POINTER(rt->rt_dev, dev); |
b79bda3d | 404 | rt->rt_via_table = cfg->rc_via_table; |
a2519929 EB |
405 | memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen); |
406 | ||
407 | mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo); | |
408 | ||
409 | dev_put(dev); | |
410 | return 0; | |
411 | ||
412 | errout: | |
413 | if (dev) | |
414 | dev_put(dev); | |
415 | return err; | |
416 | } | |
417 | ||
418 | static int mpls_route_del(struct mpls_route_config *cfg) | |
419 | { | |
420 | struct net *net = cfg->rc_nlinfo.nl_net; | |
421 | unsigned index; | |
422 | int err = -EINVAL; | |
423 | ||
424 | index = cfg->rc_label; | |
425 | ||
426 | /* The first 16 labels are reserved, and may not be removed */ | |
427 | if (index < 16) | |
428 | goto errout; | |
429 | ||
430 | /* The full 20 bit range may not be supported */ | |
431 | if (index >= net->mpls.platform_labels) | |
432 | goto errout; | |
433 | ||
434 | mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo); | |
435 | ||
436 | err = 0; | |
437 | errout: | |
438 | return err; | |
439 | } | |
440 | ||
03c57747 RS |
441 | static struct mpls_dev *mpls_add_dev(struct net_device *dev) |
442 | { | |
443 | struct mpls_dev *mdev; | |
444 | int err = -ENOMEM; | |
445 | ||
446 | ASSERT_RTNL(); | |
447 | ||
448 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
449 | if (!mdev) | |
450 | return ERR_PTR(err); | |
451 | ||
452 | rcu_assign_pointer(dev->mpls_ptr, mdev); | |
453 | ||
454 | return mdev; | |
455 | } | |
456 | ||
0189197f EB |
457 | static void mpls_ifdown(struct net_device *dev) |
458 | { | |
19d0c341 | 459 | struct mpls_route __rcu **platform_label; |
0189197f | 460 | struct net *net = dev_net(dev); |
03c57747 | 461 | struct mpls_dev *mdev; |
0189197f EB |
462 | unsigned index; |
463 | ||
19d0c341 | 464 | platform_label = rtnl_dereference(net->mpls.platform_label); |
0189197f | 465 | for (index = 0; index < net->mpls.platform_labels; index++) { |
19d0c341 | 466 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); |
0189197f EB |
467 | if (!rt) |
468 | continue; | |
19d0c341 | 469 | if (rtnl_dereference(rt->rt_dev) != dev) |
0189197f EB |
470 | continue; |
471 | rt->rt_dev = NULL; | |
472 | } | |
03c57747 RS |
473 | |
474 | mdev = mpls_dev_get(dev); | |
475 | if (!mdev) | |
476 | return; | |
477 | ||
478 | RCU_INIT_POINTER(dev->mpls_ptr, NULL); | |
479 | ||
480 | kfree(mdev); | |
0189197f EB |
481 | } |
482 | ||
483 | static int mpls_dev_notify(struct notifier_block *this, unsigned long event, | |
484 | void *ptr) | |
485 | { | |
486 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
03c57747 | 487 | struct mpls_dev *mdev; |
0189197f EB |
488 | |
489 | switch(event) { | |
03c57747 RS |
490 | case NETDEV_REGISTER: |
491 | /* For now just support ethernet devices */ | |
492 | if ((dev->type == ARPHRD_ETHER) || | |
493 | (dev->type == ARPHRD_LOOPBACK)) { | |
494 | mdev = mpls_add_dev(dev); | |
495 | if (IS_ERR(mdev)) | |
496 | return notifier_from_errno(PTR_ERR(mdev)); | |
497 | } | |
498 | break; | |
499 | ||
0189197f EB |
500 | case NETDEV_UNREGISTER: |
501 | mpls_ifdown(dev); | |
502 | break; | |
503 | } | |
504 | return NOTIFY_OK; | |
505 | } | |
506 | ||
507 | static struct notifier_block mpls_dev_notifier = { | |
508 | .notifier_call = mpls_dev_notify, | |
509 | }; | |
510 | ||
03c05665 | 511 | static int nla_put_via(struct sk_buff *skb, |
b79bda3d | 512 | u8 table, const void *addr, int alen) |
03c05665 | 513 | { |
b79bda3d EB |
514 | static const int table_to_family[NEIGH_NR_TABLES + 1] = { |
515 | AF_INET, AF_INET6, AF_DECnet, AF_PACKET, | |
516 | }; | |
03c05665 EB |
517 | struct nlattr *nla; |
518 | struct rtvia *via; | |
b79bda3d | 519 | int family = AF_UNSPEC; |
03c05665 EB |
520 | |
521 | nla = nla_reserve(skb, RTA_VIA, alen + 2); | |
522 | if (!nla) | |
523 | return -EMSGSIZE; | |
524 | ||
b79bda3d EB |
525 | if (table <= NEIGH_NR_TABLES) |
526 | family = table_to_family[table]; | |
527 | ||
03c05665 EB |
528 | via = nla_data(nla); |
529 | via->rtvia_family = family; | |
530 | memcpy(via->rtvia_addr, addr, alen); | |
531 | return 0; | |
532 | } | |
533 | ||
966bae33 EB |
534 | int nla_put_labels(struct sk_buff *skb, int attrtype, |
535 | u8 labels, const u32 label[]) | |
536 | { | |
537 | struct nlattr *nla; | |
538 | struct mpls_shim_hdr *nla_label; | |
539 | bool bos; | |
540 | int i; | |
541 | nla = nla_reserve(skb, attrtype, labels*4); | |
542 | if (!nla) | |
543 | return -EMSGSIZE; | |
544 | ||
545 | nla_label = nla_data(nla); | |
546 | bos = true; | |
547 | for (i = labels - 1; i >= 0; i--) { | |
548 | nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos); | |
549 | bos = false; | |
550 | } | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | int nla_get_labels(const struct nlattr *nla, | |
556 | u32 max_labels, u32 *labels, u32 label[]) | |
557 | { | |
558 | unsigned len = nla_len(nla); | |
559 | unsigned nla_labels; | |
560 | struct mpls_shim_hdr *nla_label; | |
561 | bool bos; | |
562 | int i; | |
563 | ||
564 | /* len needs to be an even multiple of 4 (the label size) */ | |
565 | if (len & 3) | |
566 | return -EINVAL; | |
567 | ||
568 | /* Limit the number of new labels allowed */ | |
569 | nla_labels = len/4; | |
570 | if (nla_labels > max_labels) | |
571 | return -EINVAL; | |
572 | ||
573 | nla_label = nla_data(nla); | |
574 | bos = true; | |
575 | for (i = nla_labels - 1; i >= 0; i--, bos = false) { | |
576 | struct mpls_entry_decoded dec; | |
577 | dec = mpls_entry_decode(nla_label + i); | |
578 | ||
579 | /* Ensure the bottom of stack flag is properly set | |
580 | * and ttl and tc are both clear. | |
581 | */ | |
582 | if ((dec.bos != bos) || dec.ttl || dec.tc) | |
583 | return -EINVAL; | |
584 | ||
585 | label[i] = dec.label; | |
586 | } | |
587 | *labels = nla_labels; | |
588 | return 0; | |
589 | } | |
590 | ||
03c05665 EB |
591 | static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
592 | struct mpls_route_config *cfg) | |
593 | { | |
594 | struct rtmsg *rtm; | |
595 | struct nlattr *tb[RTA_MAX+1]; | |
596 | int index; | |
597 | int err; | |
598 | ||
599 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy); | |
600 | if (err < 0) | |
601 | goto errout; | |
602 | ||
603 | err = -EINVAL; | |
604 | rtm = nlmsg_data(nlh); | |
605 | memset(cfg, 0, sizeof(*cfg)); | |
606 | ||
607 | if (rtm->rtm_family != AF_MPLS) | |
608 | goto errout; | |
609 | if (rtm->rtm_dst_len != 20) | |
610 | goto errout; | |
611 | if (rtm->rtm_src_len != 0) | |
612 | goto errout; | |
613 | if (rtm->rtm_tos != 0) | |
614 | goto errout; | |
615 | if (rtm->rtm_table != RT_TABLE_MAIN) | |
616 | goto errout; | |
617 | /* Any value is acceptable for rtm_protocol */ | |
618 | ||
619 | /* As mpls uses destination specific addresses | |
620 | * (or source specific address in the case of multicast) | |
621 | * all addresses have universal scope. | |
622 | */ | |
623 | if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) | |
624 | goto errout; | |
625 | if (rtm->rtm_type != RTN_UNICAST) | |
626 | goto errout; | |
627 | if (rtm->rtm_flags != 0) | |
628 | goto errout; | |
629 | ||
630 | cfg->rc_label = LABEL_NOT_SPECIFIED; | |
631 | cfg->rc_protocol = rtm->rtm_protocol; | |
632 | cfg->rc_nlflags = nlh->nlmsg_flags; | |
633 | cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid; | |
634 | cfg->rc_nlinfo.nlh = nlh; | |
635 | cfg->rc_nlinfo.nl_net = sock_net(skb->sk); | |
636 | ||
637 | for (index = 0; index <= RTA_MAX; index++) { | |
638 | struct nlattr *nla = tb[index]; | |
639 | if (!nla) | |
640 | continue; | |
641 | ||
642 | switch(index) { | |
643 | case RTA_OIF: | |
644 | cfg->rc_ifindex = nla_get_u32(nla); | |
645 | break; | |
646 | case RTA_NEWDST: | |
647 | if (nla_get_labels(nla, MAX_NEW_LABELS, | |
648 | &cfg->rc_output_labels, | |
649 | cfg->rc_output_label)) | |
650 | goto errout; | |
651 | break; | |
652 | case RTA_DST: | |
653 | { | |
654 | u32 label_count; | |
655 | if (nla_get_labels(nla, 1, &label_count, | |
656 | &cfg->rc_label)) | |
657 | goto errout; | |
658 | ||
659 | /* The first 16 labels are reserved, and may not be set */ | |
660 | if (cfg->rc_label < 16) | |
661 | goto errout; | |
662 | ||
663 | break; | |
664 | } | |
665 | case RTA_VIA: | |
666 | { | |
667 | struct rtvia *via = nla_data(nla); | |
f8d54afc RS |
668 | if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) |
669 | goto errout; | |
f8d54afc RS |
670 | cfg->rc_via_alen = nla_len(nla) - |
671 | offsetof(struct rtvia, rtvia_addr); | |
03c05665 EB |
672 | if (cfg->rc_via_alen > MAX_VIA_ALEN) |
673 | goto errout; | |
674 | ||
675 | /* Validate the address family */ | |
b79bda3d | 676 | switch(via->rtvia_family) { |
03c05665 | 677 | case AF_PACKET: |
b79bda3d | 678 | cfg->rc_via_table = NEIGH_LINK_TABLE; |
03c05665 EB |
679 | break; |
680 | case AF_INET: | |
b79bda3d | 681 | cfg->rc_via_table = NEIGH_ARP_TABLE; |
03c05665 EB |
682 | if (cfg->rc_via_alen != 4) |
683 | goto errout; | |
684 | break; | |
685 | case AF_INET6: | |
b79bda3d | 686 | cfg->rc_via_table = NEIGH_ND_TABLE; |
03c05665 EB |
687 | if (cfg->rc_via_alen != 16) |
688 | goto errout; | |
689 | break; | |
690 | default: | |
691 | /* Unsupported address family */ | |
692 | goto errout; | |
693 | } | |
694 | ||
695 | memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen); | |
696 | break; | |
697 | } | |
698 | default: | |
699 | /* Unsupported attribute */ | |
700 | goto errout; | |
701 | } | |
702 | } | |
703 | ||
704 | err = 0; | |
705 | errout: | |
706 | return err; | |
707 | } | |
708 | ||
709 | static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
710 | { | |
711 | struct mpls_route_config cfg; | |
712 | int err; | |
713 | ||
714 | err = rtm_to_route_config(skb, nlh, &cfg); | |
715 | if (err < 0) | |
716 | return err; | |
717 | ||
718 | return mpls_route_del(&cfg); | |
719 | } | |
720 | ||
721 | ||
722 | static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) | |
723 | { | |
724 | struct mpls_route_config cfg; | |
725 | int err; | |
726 | ||
727 | err = rtm_to_route_config(skb, nlh, &cfg); | |
728 | if (err < 0) | |
729 | return err; | |
730 | ||
731 | return mpls_route_add(&cfg); | |
732 | } | |
733 | ||
734 | static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event, | |
735 | u32 label, struct mpls_route *rt, int flags) | |
736 | { | |
19d0c341 | 737 | struct net_device *dev; |
03c05665 EB |
738 | struct nlmsghdr *nlh; |
739 | struct rtmsg *rtm; | |
740 | ||
741 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); | |
742 | if (nlh == NULL) | |
743 | return -EMSGSIZE; | |
744 | ||
745 | rtm = nlmsg_data(nlh); | |
746 | rtm->rtm_family = AF_MPLS; | |
747 | rtm->rtm_dst_len = 20; | |
748 | rtm->rtm_src_len = 0; | |
749 | rtm->rtm_tos = 0; | |
750 | rtm->rtm_table = RT_TABLE_MAIN; | |
751 | rtm->rtm_protocol = rt->rt_protocol; | |
752 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
753 | rtm->rtm_type = RTN_UNICAST; | |
754 | rtm->rtm_flags = 0; | |
755 | ||
756 | if (rt->rt_labels && | |
757 | nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label)) | |
758 | goto nla_put_failure; | |
b79bda3d | 759 | if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen)) |
03c05665 | 760 | goto nla_put_failure; |
19d0c341 EB |
761 | dev = rtnl_dereference(rt->rt_dev); |
762 | if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex)) | |
03c05665 EB |
763 | goto nla_put_failure; |
764 | if (nla_put_labels(skb, RTA_DST, 1, &label)) | |
765 | goto nla_put_failure; | |
766 | ||
767 | nlmsg_end(skb, nlh); | |
768 | return 0; | |
769 | ||
770 | nla_put_failure: | |
771 | nlmsg_cancel(skb, nlh); | |
772 | return -EMSGSIZE; | |
773 | } | |
774 | ||
775 | static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb) | |
776 | { | |
777 | struct net *net = sock_net(skb->sk); | |
19d0c341 EB |
778 | struct mpls_route __rcu **platform_label; |
779 | size_t platform_labels; | |
03c05665 EB |
780 | unsigned int index; |
781 | ||
782 | ASSERT_RTNL(); | |
783 | ||
784 | index = cb->args[0]; | |
785 | if (index < 16) | |
786 | index = 16; | |
787 | ||
19d0c341 EB |
788 | platform_label = rtnl_dereference(net->mpls.platform_label); |
789 | platform_labels = net->mpls.platform_labels; | |
790 | for (; index < platform_labels; index++) { | |
03c05665 | 791 | struct mpls_route *rt; |
19d0c341 | 792 | rt = rtnl_dereference(platform_label[index]); |
03c05665 EB |
793 | if (!rt) |
794 | continue; | |
795 | ||
796 | if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid, | |
797 | cb->nlh->nlmsg_seq, RTM_NEWROUTE, | |
798 | index, rt, NLM_F_MULTI) < 0) | |
799 | break; | |
800 | } | |
801 | cb->args[0] = index; | |
802 | ||
803 | return skb->len; | |
804 | } | |
805 | ||
8de147dc EB |
806 | static inline size_t lfib_nlmsg_size(struct mpls_route *rt) |
807 | { | |
808 | size_t payload = | |
809 | NLMSG_ALIGN(sizeof(struct rtmsg)) | |
810 | + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */ | |
811 | + nla_total_size(4); /* RTA_DST */ | |
812 | if (rt->rt_labels) /* RTA_NEWDST */ | |
813 | payload += nla_total_size(rt->rt_labels * 4); | |
814 | if (rt->rt_dev) /* RTA_OIF */ | |
815 | payload += nla_total_size(4); | |
816 | return payload; | |
817 | } | |
818 | ||
819 | static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, | |
820 | struct nlmsghdr *nlh, struct net *net, u32 portid, | |
821 | unsigned int nlm_flags) | |
822 | { | |
823 | struct sk_buff *skb; | |
824 | u32 seq = nlh ? nlh->nlmsg_seq : 0; | |
825 | int err = -ENOBUFS; | |
826 | ||
827 | skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL); | |
828 | if (skb == NULL) | |
829 | goto errout; | |
830 | ||
831 | err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags); | |
832 | if (err < 0) { | |
833 | /* -EMSGSIZE implies BUG in lfib_nlmsg_size */ | |
834 | WARN_ON(err == -EMSGSIZE); | |
835 | kfree_skb(skb); | |
836 | goto errout; | |
837 | } | |
838 | rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL); | |
839 | ||
840 | return; | |
841 | errout: | |
842 | if (err < 0) | |
843 | rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err); | |
844 | } | |
845 | ||
7720c01f EB |
846 | static int resize_platform_label_table(struct net *net, size_t limit) |
847 | { | |
848 | size_t size = sizeof(struct mpls_route *) * limit; | |
849 | size_t old_limit; | |
850 | size_t cp_size; | |
851 | struct mpls_route __rcu **labels = NULL, **old; | |
852 | struct mpls_route *rt0 = NULL, *rt2 = NULL; | |
853 | unsigned index; | |
854 | ||
855 | if (size) { | |
856 | labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); | |
857 | if (!labels) | |
858 | labels = vzalloc(size); | |
859 | ||
860 | if (!labels) | |
861 | goto nolabels; | |
862 | } | |
863 | ||
864 | /* In case the predefined labels need to be populated */ | |
865 | if (limit > LABEL_IPV4_EXPLICIT_NULL) { | |
866 | struct net_device *lo = net->loopback_dev; | |
867 | rt0 = mpls_rt_alloc(lo->addr_len); | |
868 | if (!rt0) | |
869 | goto nort0; | |
19d0c341 | 870 | RCU_INIT_POINTER(rt0->rt_dev, lo); |
7720c01f | 871 | rt0->rt_protocol = RTPROT_KERNEL; |
b79bda3d | 872 | rt0->rt_via_table = NEIGH_LINK_TABLE; |
7720c01f EB |
873 | memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len); |
874 | } | |
875 | if (limit > LABEL_IPV6_EXPLICIT_NULL) { | |
876 | struct net_device *lo = net->loopback_dev; | |
877 | rt2 = mpls_rt_alloc(lo->addr_len); | |
878 | if (!rt2) | |
879 | goto nort2; | |
19d0c341 | 880 | RCU_INIT_POINTER(rt2->rt_dev, lo); |
7720c01f | 881 | rt2->rt_protocol = RTPROT_KERNEL; |
b79bda3d | 882 | rt2->rt_via_table = NEIGH_LINK_TABLE; |
7720c01f EB |
883 | memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len); |
884 | } | |
885 | ||
886 | rtnl_lock(); | |
887 | /* Remember the original table */ | |
19d0c341 | 888 | old = rtnl_dereference(net->mpls.platform_label); |
7720c01f EB |
889 | old_limit = net->mpls.platform_labels; |
890 | ||
891 | /* Free any labels beyond the new table */ | |
892 | for (index = limit; index < old_limit; index++) | |
893 | mpls_route_update(net, index, NULL, NULL, NULL); | |
894 | ||
895 | /* Copy over the old labels */ | |
896 | cp_size = size; | |
897 | if (old_limit < limit) | |
898 | cp_size = old_limit * sizeof(struct mpls_route *); | |
899 | ||
900 | memcpy(labels, old, cp_size); | |
901 | ||
902 | /* If needed set the predefined labels */ | |
903 | if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) && | |
904 | (limit > LABEL_IPV6_EXPLICIT_NULL)) { | |
19d0c341 | 905 | RCU_INIT_POINTER(labels[LABEL_IPV6_EXPLICIT_NULL], rt2); |
7720c01f EB |
906 | rt2 = NULL; |
907 | } | |
908 | ||
909 | if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) && | |
910 | (limit > LABEL_IPV4_EXPLICIT_NULL)) { | |
19d0c341 | 911 | RCU_INIT_POINTER(labels[LABEL_IPV4_EXPLICIT_NULL], rt0); |
7720c01f EB |
912 | rt0 = NULL; |
913 | } | |
914 | ||
915 | /* Update the global pointers */ | |
916 | net->mpls.platform_labels = limit; | |
19d0c341 | 917 | rcu_assign_pointer(net->mpls.platform_label, labels); |
7720c01f EB |
918 | |
919 | rtnl_unlock(); | |
920 | ||
921 | mpls_rt_free(rt2); | |
922 | mpls_rt_free(rt0); | |
923 | ||
924 | if (old) { | |
925 | synchronize_rcu(); | |
926 | kvfree(old); | |
927 | } | |
928 | return 0; | |
929 | ||
930 | nort2: | |
931 | mpls_rt_free(rt0); | |
932 | nort0: | |
933 | kvfree(labels); | |
934 | nolabels: | |
935 | return -ENOMEM; | |
936 | } | |
937 | ||
938 | static int mpls_platform_labels(struct ctl_table *table, int write, | |
939 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
940 | { | |
941 | struct net *net = table->data; | |
942 | int platform_labels = net->mpls.platform_labels; | |
943 | int ret; | |
944 | struct ctl_table tmp = { | |
945 | .procname = table->procname, | |
946 | .data = &platform_labels, | |
947 | .maxlen = sizeof(int), | |
948 | .mode = table->mode, | |
949 | .extra1 = &zero, | |
950 | .extra2 = &label_limit, | |
951 | }; | |
952 | ||
953 | ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); | |
954 | ||
955 | if (write && ret == 0) | |
956 | ret = resize_platform_label_table(net, platform_labels); | |
957 | ||
958 | return ret; | |
959 | } | |
960 | ||
961 | static struct ctl_table mpls_table[] = { | |
962 | { | |
963 | .procname = "platform_labels", | |
964 | .data = NULL, | |
965 | .maxlen = sizeof(int), | |
966 | .mode = 0644, | |
967 | .proc_handler = mpls_platform_labels, | |
968 | }, | |
969 | { } | |
970 | }; | |
971 | ||
0189197f EB |
972 | static int mpls_net_init(struct net *net) |
973 | { | |
7720c01f EB |
974 | struct ctl_table *table; |
975 | ||
0189197f EB |
976 | net->mpls.platform_labels = 0; |
977 | net->mpls.platform_label = NULL; | |
978 | ||
7720c01f EB |
979 | table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL); |
980 | if (table == NULL) | |
981 | return -ENOMEM; | |
982 | ||
983 | table[0].data = net; | |
984 | net->mpls.ctl = register_net_sysctl(net, "net/mpls", table); | |
985 | if (net->mpls.ctl == NULL) | |
986 | return -ENOMEM; | |
987 | ||
0189197f EB |
988 | return 0; |
989 | } | |
990 | ||
991 | static void mpls_net_exit(struct net *net) | |
992 | { | |
19d0c341 EB |
993 | struct mpls_route __rcu **platform_label; |
994 | size_t platform_labels; | |
7720c01f | 995 | struct ctl_table *table; |
0189197f EB |
996 | unsigned int index; |
997 | ||
7720c01f EB |
998 | table = net->mpls.ctl->ctl_table_arg; |
999 | unregister_net_sysctl_table(net->mpls.ctl); | |
1000 | kfree(table); | |
1001 | ||
19d0c341 EB |
1002 | /* An rcu grace period has passed since there was a device in |
1003 | * the network namespace (and thus the last in flight packet) | |
0189197f EB |
1004 | * left this network namespace. This is because |
1005 | * unregister_netdevice_many and netdev_run_todo has completed | |
1006 | * for each network device that was in this network namespace. | |
1007 | * | |
1008 | * As such no additional rcu synchronization is necessary when | |
1009 | * freeing the platform_label table. | |
1010 | */ | |
1011 | rtnl_lock(); | |
19d0c341 EB |
1012 | platform_label = rtnl_dereference(net->mpls.platform_label); |
1013 | platform_labels = net->mpls.platform_labels; | |
1014 | for (index = 0; index < platform_labels; index++) { | |
1015 | struct mpls_route *rt = rtnl_dereference(platform_label[index]); | |
1016 | RCU_INIT_POINTER(platform_label[index], NULL); | |
0189197f EB |
1017 | mpls_rt_free(rt); |
1018 | } | |
1019 | rtnl_unlock(); | |
1020 | ||
19d0c341 | 1021 | kvfree(platform_label); |
0189197f EB |
1022 | } |
1023 | ||
1024 | static struct pernet_operations mpls_net_ops = { | |
1025 | .init = mpls_net_init, | |
1026 | .exit = mpls_net_exit, | |
1027 | }; | |
1028 | ||
1029 | static int __init mpls_init(void) | |
1030 | { | |
1031 | int err; | |
1032 | ||
1033 | BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4); | |
1034 | ||
1035 | err = register_pernet_subsys(&mpls_net_ops); | |
1036 | if (err) | |
1037 | goto out; | |
1038 | ||
1039 | err = register_netdevice_notifier(&mpls_dev_notifier); | |
1040 | if (err) | |
1041 | goto out_unregister_pernet; | |
1042 | ||
1043 | dev_add_pack(&mpls_packet_type); | |
1044 | ||
03c05665 EB |
1045 | rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL); |
1046 | rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL); | |
1047 | rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL); | |
0189197f EB |
1048 | err = 0; |
1049 | out: | |
1050 | return err; | |
1051 | ||
1052 | out_unregister_pernet: | |
1053 | unregister_pernet_subsys(&mpls_net_ops); | |
1054 | goto out; | |
1055 | } | |
1056 | module_init(mpls_init); | |
1057 | ||
1058 | static void __exit mpls_exit(void) | |
1059 | { | |
03c05665 | 1060 | rtnl_unregister_all(PF_MPLS); |
0189197f EB |
1061 | dev_remove_pack(&mpls_packet_type); |
1062 | unregister_netdevice_notifier(&mpls_dev_notifier); | |
1063 | unregister_pernet_subsys(&mpls_net_ops); | |
1064 | } | |
1065 | module_exit(mpls_exit); | |
1066 | ||
1067 | MODULE_DESCRIPTION("MultiProtocol Label Switching"); | |
1068 | MODULE_LICENSE("GPL v2"); | |
1069 | MODULE_ALIAS_NETPROTO(PF_MPLS); |