1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/skbuff.h>
4 #include <linux/socket.h>
5 #include <linux/sysctl.h>
6 #include <linux/net.h>
7 #include <linux/module.h>
8 #include <linux/if_arp.h>
9 #include <linux/ipv6.h>
10 #include <linux/mpls.h>
11 #include <linux/netconf.h>
12 #include <linux/nospec.h>
13 #include <linux/vmalloc.h>
14 #include <linux/percpu.h>
15 #include <net/gso.h>
16 #include <net/ip.h>
17 #include <net/dst.h>
18 #include <net/sock.h>
19 #include <net/arp.h>
20 #include <net/ip_fib.h>
21 #include <net/netevent.h>
22 #include <net/ip_tunnels.h>
23 #include <net/netns/generic.h>
24 #if IS_ENABLED(CONFIG_IPV6)
25 #include <net/ipv6.h>
26 #endif
27 #include <net/rtnh.h>
28 #include "internal.h"
29
30 /* max memory we will use for mpls_route */
31 #define MAX_MPLS_ROUTE_MEM 4096
32
33 /* Maximum number of labels to look ahead at when selecting a path of
34 * a multipath route
35 */
36 #define MAX_MP_SELECT_LABELS 4
37
38 #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
39
40 static int label_limit = (1 << 20) - 1;
41 static int ttl_max = 255;
42
43 #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
ipgre_mpls_encap_hlen(struct ip_tunnel_encap * e)44 static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
45 {
46 return sizeof(struct mpls_shim_hdr);
47 }
48
49 static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
50 .encap_hlen = ipgre_mpls_encap_hlen,
51 };
52
ipgre_tunnel_encap_add_mpls_ops(void)53 static int ipgre_tunnel_encap_add_mpls_ops(void)
54 {
55 return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
56 }
57
ipgre_tunnel_encap_del_mpls_ops(void)58 static void ipgre_tunnel_encap_del_mpls_ops(void)
59 {
60 ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
61 }
62 #else
ipgre_tunnel_encap_add_mpls_ops(void)63 static int ipgre_tunnel_encap_add_mpls_ops(void)
64 {
65 return 0;
66 }
67
ipgre_tunnel_encap_del_mpls_ops(void)68 static void ipgre_tunnel_encap_del_mpls_ops(void)
69 {
70 }
71 #endif
72
73 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
74 struct nlmsghdr *nlh, struct net *net, u32 portid,
75 unsigned int nlm_flags);
76
mpls_route_input(struct net * net,unsigned int index)77 static struct mpls_route *mpls_route_input(struct net *net, unsigned int index)
78 {
79 struct mpls_route __rcu **platform_label;
80
81 platform_label = mpls_dereference(net, net->mpls.platform_label);
82 return mpls_dereference(net, platform_label[index]);
83 }
84
mpls_platform_label_rcu(struct net * net,size_t * platform_labels)85 static struct mpls_route __rcu **mpls_platform_label_rcu(struct net *net, size_t *platform_labels)
86 {
87 struct mpls_route __rcu **platform_label;
88 unsigned int sequence;
89
90 do {
91 sequence = read_seqcount_begin(&net->mpls.platform_label_seq);
92 platform_label = rcu_dereference(net->mpls.platform_label);
93 *platform_labels = net->mpls.platform_labels;
94 } while (read_seqcount_retry(&net->mpls.platform_label_seq, sequence));
95
96 return platform_label;
97 }
98
mpls_route_input_rcu(struct net * net,unsigned int index)99 static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned int index)
100 {
101 struct mpls_route __rcu **platform_label;
102 size_t platform_labels;
103
104 platform_label = mpls_platform_label_rcu(net, &platform_labels);
105
106 if (index >= platform_labels)
107 return NULL;
108
109 return rcu_dereference(platform_label[index]);
110 }
111
mpls_output_possible(const struct net_device * dev)112 bool mpls_output_possible(const struct net_device *dev)
113 {
114 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
115 }
116 EXPORT_SYMBOL_GPL(mpls_output_possible);
117
__mpls_nh_via(struct mpls_route * rt,struct mpls_nh * nh)118 static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
119 {
120 return (u8 *)nh + rt->rt_via_offset;
121 }
122
mpls_nh_via(const struct mpls_route * rt,const struct mpls_nh * nh)123 static const u8 *mpls_nh_via(const struct mpls_route *rt,
124 const struct mpls_nh *nh)
125 {
126 return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
127 }
128
mpls_nh_header_size(const struct mpls_nh * nh)129 static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
130 {
131 /* The size of the layer 2.5 labels to be added for this route */
132 return nh->nh_labels * sizeof(struct mpls_shim_hdr);
133 }
134
mpls_dev_mtu(const struct net_device * dev)135 unsigned int mpls_dev_mtu(const struct net_device *dev)
136 {
137 /* The amount of data the layer 2 frame can hold */
138 return dev->mtu;
139 }
140 EXPORT_SYMBOL_GPL(mpls_dev_mtu);
141
mpls_pkt_too_big(const struct sk_buff * skb,unsigned int mtu)142 bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
143 {
144 if (skb->len <= mtu)
145 return false;
146
147 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
148 return false;
149
150 return true;
151 }
152 EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
153
mpls_stats_inc_outucastpkts(struct net * net,struct net_device * dev,const struct sk_buff * skb)154 void mpls_stats_inc_outucastpkts(struct net *net,
155 struct net_device *dev,
156 const struct sk_buff *skb)
157 {
158 struct mpls_dev *mdev;
159
160 if (skb->protocol == htons(ETH_P_MPLS_UC)) {
161 mdev = mpls_dev_rcu(dev);
162 if (mdev)
163 MPLS_INC_STATS_LEN(mdev, skb->len,
164 tx_packets,
165 tx_bytes);
166 } else if (skb->protocol == htons(ETH_P_IP)) {
167 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
168 #if IS_ENABLED(CONFIG_IPV6)
169 } else if (skb->protocol == htons(ETH_P_IPV6)) {
170 struct inet6_dev *in6dev = in6_dev_rcu(dev);
171
172 if (in6dev)
173 IP6_UPD_PO_STATS(net, in6dev,
174 IPSTATS_MIB_OUT, skb->len);
175 #endif
176 }
177 }
178 EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
179
mpls_multipath_hash(struct mpls_route * rt,struct sk_buff * skb)180 static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
181 {
182 struct mpls_entry_decoded dec;
183 unsigned int mpls_hdr_len = 0;
184 struct mpls_shim_hdr *hdr;
185 bool eli_seen = false;
186 int label_index;
187 u32 hash = 0;
188
189 for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
190 label_index++) {
191 mpls_hdr_len += sizeof(*hdr);
192 if (!pskb_may_pull(skb, mpls_hdr_len))
193 break;
194
195 /* Read and decode the current label */
196 hdr = mpls_hdr(skb) + label_index;
197 dec = mpls_entry_decode(hdr);
198
199 /* RFC6790 - reserved labels MUST NOT be used as keys
200 * for the load-balancing function
201 */
202 if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
203 hash = jhash_1word(dec.label, hash);
204
205 /* The entropy label follows the entropy label
206 * indicator, so this means that the entropy
207 * label was just added to the hash - no need to
208 * go any deeper either in the label stack or in the
209 * payload
210 */
211 if (eli_seen)
212 break;
213 } else if (dec.label == MPLS_LABEL_ENTROPY) {
214 eli_seen = true;
215 }
216
217 if (!dec.bos)
218 continue;
219
220 /* found bottom label; does skb have room for a header? */
221 if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
222 const struct iphdr *v4hdr;
223
224 v4hdr = (const struct iphdr *)(hdr + 1);
225 if (v4hdr->version == 4) {
226 hash = jhash_3words(ntohl(v4hdr->saddr),
227 ntohl(v4hdr->daddr),
228 v4hdr->protocol, hash);
229 } else if (v4hdr->version == 6 &&
230 pskb_may_pull(skb, mpls_hdr_len +
231 sizeof(struct ipv6hdr))) {
232 const struct ipv6hdr *v6hdr;
233
234 v6hdr = (const struct ipv6hdr *)(hdr + 1);
235 hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
236 hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
237 hash = jhash_1word(v6hdr->nexthdr, hash);
238 }
239 }
240
241 break;
242 }
243
244 return hash;
245 }
246
mpls_get_nexthop(struct mpls_route * rt,u8 index)247 static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
248 {
249 return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
250 }
251
252 /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
253 * a next hop (nh->nh_flags) are modified by netdev event handlers.
254 * Since those fields can change at any moment, use READ_ONCE to
255 * access both.
256 */
mpls_select_multipath(struct mpls_route * rt,struct sk_buff * skb)257 static const struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
258 struct sk_buff *skb)
259 {
260 u32 hash = 0;
261 int nh_index = 0;
262 int n = 0;
263 u8 alive;
264
265 /* No need to look further into packet if there's only
266 * one path
267 */
268 if (rt->rt_nhn == 1)
269 return rt->rt_nh;
270
271 alive = READ_ONCE(rt->rt_nhn_alive);
272 if (alive == 0)
273 return NULL;
274
275 hash = mpls_multipath_hash(rt, skb);
276 nh_index = hash % alive;
277 if (alive == rt->rt_nhn)
278 goto out;
279 for_nexthops(rt) {
280 unsigned int nh_flags = READ_ONCE(nh->nh_flags);
281
282 if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
283 continue;
284 if (n == nh_index)
285 return nh;
286 n++;
287 } endfor_nexthops(rt);
288
289 out:
290 return mpls_get_nexthop(rt, nh_index);
291 }
292
mpls_egress(struct net * net,struct mpls_route * rt,struct sk_buff * skb,struct mpls_entry_decoded dec)293 static bool mpls_egress(struct net *net, struct mpls_route *rt,
294 struct sk_buff *skb, struct mpls_entry_decoded dec)
295 {
296 enum mpls_payload_type payload_type;
297 bool success = false;
298
299 /* The IPv4 code below accesses through the IPv4 header
300 * checksum, which is 12 bytes into the packet.
301 * The IPv6 code below accesses through the IPv6 hop limit
302 * which is 8 bytes into the packet.
303 *
304 * For all supported cases there should always be at least 12
305 * bytes of packet data present. The IPv4 header is 20 bytes
306 * without options and the IPv6 header is always 40 bytes
307 * long.
308 */
309 if (!pskb_may_pull(skb, 12))
310 return false;
311
312 payload_type = rt->rt_payload_type;
313 if (payload_type == MPT_UNSPEC)
314 payload_type = ip_hdr(skb)->version;
315
316 switch (payload_type) {
317 case MPT_IPV4: {
318 struct iphdr *hdr4 = ip_hdr(skb);
319 u8 new_ttl;
320 skb->protocol = htons(ETH_P_IP);
321
322 /* If propagating TTL, take the decremented TTL from
323 * the incoming MPLS header, otherwise decrement the
324 * TTL, but only if not 0 to avoid underflow.
325 */
326 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
327 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
328 net->mpls.ip_ttl_propagate))
329 new_ttl = dec.ttl;
330 else
331 new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
332
333 csum_replace2(&hdr4->check,
334 htons(hdr4->ttl << 8),
335 htons(new_ttl << 8));
336 hdr4->ttl = new_ttl;
337 success = true;
338 break;
339 }
340 case MPT_IPV6: {
341 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
342 skb->protocol = htons(ETH_P_IPV6);
343
344 /* If propagating TTL, take the decremented TTL from
345 * the incoming MPLS header, otherwise decrement the
346 * hop limit, but only if not 0 to avoid underflow.
347 */
348 if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
349 (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
350 net->mpls.ip_ttl_propagate))
351 hdr6->hop_limit = dec.ttl;
352 else if (hdr6->hop_limit)
353 hdr6->hop_limit = hdr6->hop_limit - 1;
354 success = true;
355 break;
356 }
357 case MPT_UNSPEC:
358 /* Should have decided which protocol it is by now */
359 break;
360 }
361
362 return success;
363 }
364
mpls_forward(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)365 static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
366 struct packet_type *pt, struct net_device *orig_dev)
367 {
368 struct net *net = dev_net_rcu(dev);
369 struct mpls_shim_hdr *hdr;
370 const struct mpls_nh *nh;
371 struct mpls_route *rt;
372 struct mpls_entry_decoded dec;
373 struct net_device *out_dev;
374 struct mpls_dev *out_mdev;
375 struct mpls_dev *mdev;
376 unsigned int hh_len;
377 unsigned int new_header_size;
378 unsigned int mtu;
379 int err;
380
381 /* Careful this entire function runs inside of an rcu critical section */
382
383 mdev = mpls_dev_rcu(dev);
384 if (!mdev)
385 goto drop;
386
387 MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
388 rx_bytes);
389
390 if (!mdev->input_enabled) {
391 MPLS_INC_STATS(mdev, rx_dropped);
392 goto drop;
393 }
394
395 if (skb->pkt_type != PACKET_HOST)
396 goto err;
397
398 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
399 goto err;
400
401 if (!pskb_may_pull(skb, sizeof(*hdr)))
402 goto err;
403
404 skb_dst_drop(skb);
405
406 /* Read and decode the label */
407 hdr = mpls_hdr(skb);
408 dec = mpls_entry_decode(hdr);
409
410 rt = mpls_route_input_rcu(net, dec.label);
411 if (!rt) {
412 MPLS_INC_STATS(mdev, rx_noroute);
413 goto drop;
414 }
415
416 nh = mpls_select_multipath(rt, skb);
417 if (!nh)
418 goto err;
419
420 /* Pop the label */
421 skb_pull(skb, sizeof(*hdr));
422 skb_reset_network_header(skb);
423
424 skb_orphan(skb);
425
426 if (skb_warn_if_lro(skb))
427 goto err;
428
429 skb_forward_csum(skb);
430
431 /* Verify ttl is valid */
432 if (dec.ttl <= 1)
433 goto err;
434
435 /* Find the output device */
436 out_dev = nh->nh_dev;
437 if (!mpls_output_possible(out_dev))
438 goto tx_err;
439
440 /* Verify the destination can hold the packet */
441 new_header_size = mpls_nh_header_size(nh);
442 mtu = mpls_dev_mtu(out_dev);
443 if (mpls_pkt_too_big(skb, mtu - new_header_size))
444 goto tx_err;
445
446 hh_len = LL_RESERVED_SPACE(out_dev);
447 if (!out_dev->header_ops)
448 hh_len = 0;
449
450 /* Ensure there is enough space for the headers in the skb */
451 if (skb_cow(skb, hh_len + new_header_size))
452 goto tx_err;
453
454 skb->dev = out_dev;
455 skb->protocol = htons(ETH_P_MPLS_UC);
456
457 dec.ttl -= 1;
458 if (unlikely(!new_header_size && dec.bos)) {
459 /* Penultimate hop popping */
460 if (!mpls_egress(net, rt, skb, dec))
461 goto err;
462 } else {
463 bool bos;
464 int i;
465 skb_push(skb, new_header_size);
466 skb_reset_network_header(skb);
467 /* Push the new labels */
468 hdr = mpls_hdr(skb);
469 bos = dec.bos;
470 for (i = nh->nh_labels - 1; i >= 0; i--) {
471 hdr[i] = mpls_entry_encode(nh->nh_label[i],
472 dec.ttl, 0, bos);
473 bos = false;
474 }
475 }
476
477 mpls_stats_inc_outucastpkts(net, out_dev, skb);
478
479 /* If via wasn't specified then send out using device address */
480 if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
481 err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
482 out_dev->dev_addr, skb);
483 else
484 err = neigh_xmit(nh->nh_via_table, out_dev,
485 mpls_nh_via(rt, nh), skb);
486 if (err)
487 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
488 __func__, err);
489 return 0;
490
491 tx_err:
492 out_mdev = out_dev ? mpls_dev_rcu(out_dev) : NULL;
493 if (out_mdev)
494 MPLS_INC_STATS(out_mdev, tx_errors);
495 goto drop;
496 err:
497 MPLS_INC_STATS(mdev, rx_errors);
498 drop:
499 kfree_skb(skb);
500 return NET_RX_DROP;
501 }
502
503 static struct packet_type mpls_packet_type __read_mostly = {
504 .type = cpu_to_be16(ETH_P_MPLS_UC),
505 .func = mpls_forward,
506 };
507
508 static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
509 [RTA_DST] = { .type = NLA_U32 },
510 [RTA_OIF] = { .type = NLA_U32 },
511 [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
512 };
513
514 struct mpls_route_config {
515 u32 rc_protocol;
516 u32 rc_ifindex;
517 u8 rc_via_table;
518 u8 rc_via_alen;
519 u8 rc_via[MAX_VIA_ALEN];
520 u32 rc_label;
521 u8 rc_ttl_propagate;
522 u8 rc_output_labels;
523 u32 rc_output_label[MAX_NEW_LABELS];
524 u32 rc_nlflags;
525 enum mpls_payload_type rc_payload_type;
526 struct nl_info rc_nlinfo;
527 struct rtnexthop *rc_mp;
528 int rc_mp_len;
529 };
530
531 /* all nexthops within a route have the same size based on max
532 * number of labels and max via length for a hop
533 */
mpls_rt_alloc(u8 num_nh,u8 max_alen,u8 max_labels)534 static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
535 {
536 u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
537 struct mpls_route *rt;
538 size_t size;
539
540 size = sizeof(*rt) + num_nh * nh_size;
541 if (size > MAX_MPLS_ROUTE_MEM)
542 return ERR_PTR(-EINVAL);
543
544 rt = kzalloc(size, GFP_KERNEL);
545 if (!rt)
546 return ERR_PTR(-ENOMEM);
547
548 rt->rt_nhn = num_nh;
549 rt->rt_nhn_alive = num_nh;
550 rt->rt_nh_size = nh_size;
551 rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
552
553 return rt;
554 }
555
mpls_rt_free_rcu(struct rcu_head * head)556 static void mpls_rt_free_rcu(struct rcu_head *head)
557 {
558 struct mpls_route *rt;
559
560 rt = container_of(head, struct mpls_route, rt_rcu);
561
562 change_nexthops(rt) {
563 netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
564 } endfor_nexthops(rt);
565
566 kfree(rt);
567 }
568
mpls_rt_free(struct mpls_route * rt)569 static void mpls_rt_free(struct mpls_route *rt)
570 {
571 if (rt)
572 call_rcu(&rt->rt_rcu, mpls_rt_free_rcu);
573 }
574
mpls_notify_route(struct net * net,unsigned index,struct mpls_route * old,struct mpls_route * new,const struct nl_info * info)575 static void mpls_notify_route(struct net *net, unsigned index,
576 struct mpls_route *old, struct mpls_route *new,
577 const struct nl_info *info)
578 {
579 struct nlmsghdr *nlh = info ? info->nlh : NULL;
580 unsigned portid = info ? info->portid : 0;
581 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
582 struct mpls_route *rt = new ? new : old;
583 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
584 /* Ignore reserved labels for now */
585 if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
586 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
587 }
588
mpls_route_update(struct net * net,unsigned index,struct mpls_route * new,const struct nl_info * info)589 static void mpls_route_update(struct net *net, unsigned index,
590 struct mpls_route *new,
591 const struct nl_info *info)
592 {
593 struct mpls_route __rcu **platform_label;
594 struct mpls_route *rt;
595
596 platform_label = mpls_dereference(net, net->mpls.platform_label);
597 rt = mpls_dereference(net, platform_label[index]);
598 rcu_assign_pointer(platform_label[index], new);
599
600 mpls_notify_route(net, index, rt, new, info);
601
602 /* If we removed a route free it now */
603 mpls_rt_free(rt);
604 }
605
find_free_label(struct net * net)606 static unsigned int find_free_label(struct net *net)
607 {
608 unsigned int index;
609
610 for (index = MPLS_LABEL_FIRST_UNRESERVED;
611 index < net->mpls.platform_labels;
612 index++) {
613 if (!mpls_route_input(net, index))
614 return index;
615 }
616
617 return LABEL_NOT_SPECIFIED;
618 }
619
620 #if IS_ENABLED(CONFIG_INET)
inet_fib_lookup_dev(struct net * net,struct mpls_nh * nh,const void * addr)621 static struct net_device *inet_fib_lookup_dev(struct net *net,
622 struct mpls_nh *nh,
623 const void *addr)
624 {
625 struct net_device *dev;
626 struct rtable *rt;
627 struct in_addr daddr;
628
629 memcpy(&daddr, addr, sizeof(struct in_addr));
630 rt = ip_route_output(net, daddr.s_addr, 0, 0, 0, RT_SCOPE_UNIVERSE);
631 if (IS_ERR(rt))
632 return ERR_CAST(rt);
633
634 dev = rt->dst.dev;
635 netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
636 ip_rt_put(rt);
637
638 return dev;
639 }
640 #else
inet_fib_lookup_dev(struct net * net,struct mpls_nh * nh,const void * addr)641 static struct net_device *inet_fib_lookup_dev(struct net *net,
642 struct mpls_nh *nh,
643 const void *addr)
644 {
645 return ERR_PTR(-EAFNOSUPPORT);
646 }
647 #endif
648
649 #if IS_ENABLED(CONFIG_IPV6)
inet6_fib_lookup_dev(struct net * net,struct mpls_nh * nh,const void * addr)650 static struct net_device *inet6_fib_lookup_dev(struct net *net,
651 struct mpls_nh *nh,
652 const void *addr)
653 {
654 struct net_device *dev;
655 struct dst_entry *dst;
656 struct flowi6 fl6;
657
658 memset(&fl6, 0, sizeof(fl6));
659 memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
660 dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL);
661 if (IS_ERR(dst))
662 return ERR_CAST(dst);
663
664 dev = dst->dev;
665 netdev_hold(dev, &nh->nh_dev_tracker, GFP_KERNEL);
666 dst_release(dst);
667
668 return dev;
669 }
670 #else
inet6_fib_lookup_dev(struct net * net,struct mpls_nh * nh,const void * addr)671 static struct net_device *inet6_fib_lookup_dev(struct net *net,
672 struct mpls_nh *nh,
673 const void *addr)
674 {
675 return ERR_PTR(-EAFNOSUPPORT);
676 }
677 #endif
678
find_outdev(struct net * net,struct mpls_route * rt,struct mpls_nh * nh,int oif)679 static struct net_device *find_outdev(struct net *net,
680 struct mpls_route *rt,
681 struct mpls_nh *nh, int oif)
682 {
683 struct net_device *dev = NULL;
684
685 if (!oif) {
686 switch (nh->nh_via_table) {
687 case NEIGH_ARP_TABLE:
688 dev = inet_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
689 break;
690 case NEIGH_ND_TABLE:
691 dev = inet6_fib_lookup_dev(net, nh, mpls_nh_via(rt, nh));
692 break;
693 case NEIGH_LINK_TABLE:
694 break;
695 }
696 } else {
697 dev = netdev_get_by_index(net, oif,
698 &nh->nh_dev_tracker, GFP_KERNEL);
699 }
700
701 if (!dev)
702 return ERR_PTR(-ENODEV);
703
704 if (IS_ERR(dev))
705 return dev;
706
707 nh->nh_dev = dev;
708
709 return dev;
710 }
711
mpls_nh_assign_dev(struct net * net,struct mpls_route * rt,struct mpls_nh * nh,int oif)712 static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
713 struct mpls_nh *nh, int oif)
714 {
715 struct net_device *dev = NULL;
716 int err = -ENODEV;
717
718 dev = find_outdev(net, rt, nh, oif);
719 if (IS_ERR(dev)) {
720 err = PTR_ERR(dev);
721 goto errout;
722 }
723
724 /* Ensure this is a supported device */
725 err = -EINVAL;
726 if (!mpls_dev_get(net, dev))
727 goto errout_put;
728
729 if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
730 (dev->addr_len != nh->nh_via_alen))
731 goto errout_put;
732
733 if (!(dev->flags & IFF_UP)) {
734 nh->nh_flags |= RTNH_F_DEAD;
735 } else {
736 unsigned int flags;
737
738 flags = netif_get_flags(dev);
739 if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
740 nh->nh_flags |= RTNH_F_LINKDOWN;
741 }
742
743 return 0;
744
745 errout_put:
746 netdev_put(nh->nh_dev, &nh->nh_dev_tracker);
747 nh->nh_dev = NULL;
748 errout:
749 return err;
750 }
751
nla_get_via(const struct nlattr * nla,u8 * via_alen,u8 * via_table,u8 via_addr[],struct netlink_ext_ack * extack)752 static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
753 u8 via_addr[], struct netlink_ext_ack *extack)
754 {
755 struct rtvia *via = nla_data(nla);
756 int err = -EINVAL;
757 int alen;
758
759 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
760 NL_SET_ERR_MSG_ATTR(extack, nla,
761 "Invalid attribute length for RTA_VIA");
762 goto errout;
763 }
764 alen = nla_len(nla) -
765 offsetof(struct rtvia, rtvia_addr);
766 if (alen > MAX_VIA_ALEN) {
767 NL_SET_ERR_MSG_ATTR(extack, nla,
768 "Invalid address length for RTA_VIA");
769 goto errout;
770 }
771
772 /* Validate the address family */
773 switch (via->rtvia_family) {
774 case AF_PACKET:
775 *via_table = NEIGH_LINK_TABLE;
776 break;
777 case AF_INET:
778 *via_table = NEIGH_ARP_TABLE;
779 if (alen != 4)
780 goto errout;
781 break;
782 case AF_INET6:
783 *via_table = NEIGH_ND_TABLE;
784 if (alen != 16)
785 goto errout;
786 break;
787 default:
788 /* Unsupported address family */
789 goto errout;
790 }
791
792 memcpy(via_addr, via->rtvia_addr, alen);
793 *via_alen = alen;
794 err = 0;
795
796 errout:
797 return err;
798 }
799
mpls_nh_build_from_cfg(struct mpls_route_config * cfg,struct mpls_route * rt)800 static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
801 struct mpls_route *rt)
802 {
803 struct net *net = cfg->rc_nlinfo.nl_net;
804 struct mpls_nh *nh = rt->rt_nh;
805 int err;
806 int i;
807
808 if (!nh)
809 return -ENOMEM;
810
811 nh->nh_labels = cfg->rc_output_labels;
812 for (i = 0; i < nh->nh_labels; i++)
813 nh->nh_label[i] = cfg->rc_output_label[i];
814
815 nh->nh_via_table = cfg->rc_via_table;
816 memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
817 nh->nh_via_alen = cfg->rc_via_alen;
818
819 err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
820 if (err)
821 goto errout;
822
823 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
824 rt->rt_nhn_alive--;
825
826 return 0;
827
828 errout:
829 return err;
830 }
831
mpls_nh_build(struct net * net,struct mpls_route * rt,struct mpls_nh * nh,int oif,struct nlattr * via,struct nlattr * newdst,u8 max_labels,struct netlink_ext_ack * extack)832 static int mpls_nh_build(struct net *net, struct mpls_route *rt,
833 struct mpls_nh *nh, int oif, struct nlattr *via,
834 struct nlattr *newdst, u8 max_labels,
835 struct netlink_ext_ack *extack)
836 {
837 int err = -ENOMEM;
838
839 if (!nh)
840 goto errout;
841
842 if (newdst) {
843 err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
844 nh->nh_label, extack);
845 if (err)
846 goto errout;
847 }
848
849 if (via) {
850 err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
851 __mpls_nh_via(rt, nh), extack);
852 if (err)
853 goto errout;
854 } else {
855 nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
856 }
857
858 err = mpls_nh_assign_dev(net, rt, nh, oif);
859 if (err)
860 goto errout;
861
862 return 0;
863
864 errout:
865 return err;
866 }
867
mpls_count_nexthops(struct rtnexthop * rtnh,int len,u8 cfg_via_alen,u8 * max_via_alen,u8 * max_labels)868 static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
869 u8 cfg_via_alen, u8 *max_via_alen,
870 u8 *max_labels)
871 {
872 int remaining = len;
873 u8 nhs = 0;
874
875 *max_via_alen = 0;
876 *max_labels = 0;
877
878 while (rtnh_ok(rtnh, remaining)) {
879 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
880 int attrlen;
881 u8 n_labels = 0;
882
883 attrlen = rtnh_attrlen(rtnh);
884 nla = nla_find(attrs, attrlen, RTA_VIA);
885 if (nla && nla_len(nla) >=
886 offsetof(struct rtvia, rtvia_addr)) {
887 int via_alen = nla_len(nla) -
888 offsetof(struct rtvia, rtvia_addr);
889
890 if (via_alen <= MAX_VIA_ALEN)
891 *max_via_alen = max_t(u16, *max_via_alen,
892 via_alen);
893 }
894
895 nla = nla_find(attrs, attrlen, RTA_NEWDST);
896 if (nla &&
897 nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
898 NULL, NULL) != 0)
899 return 0;
900
901 *max_labels = max_t(u8, *max_labels, n_labels);
902
903 /* number of nexthops is tracked by a u8.
904 * Check for overflow.
905 */
906 if (nhs == 255)
907 return 0;
908 nhs++;
909
910 rtnh = rtnh_next(rtnh, &remaining);
911 }
912
913 /* leftover implies invalid nexthop configuration, discard it */
914 return remaining > 0 ? 0 : nhs;
915 }
916
mpls_nh_build_multi(struct mpls_route_config * cfg,struct mpls_route * rt,u8 max_labels,struct netlink_ext_ack * extack)917 static int mpls_nh_build_multi(struct mpls_route_config *cfg,
918 struct mpls_route *rt, u8 max_labels,
919 struct netlink_ext_ack *extack)
920 {
921 struct rtnexthop *rtnh = cfg->rc_mp;
922 struct nlattr *nla_via, *nla_newdst;
923 int remaining = cfg->rc_mp_len;
924 int err = 0;
925
926 rt->rt_nhn = 0;
927
928 change_nexthops(rt) {
929 int attrlen;
930
931 nla_via = NULL;
932 nla_newdst = NULL;
933
934 err = -EINVAL;
935 if (!rtnh_ok(rtnh, remaining))
936 goto errout;
937
938 /* neither weighted multipath nor any flags
939 * are supported
940 */
941 if (rtnh->rtnh_hops || rtnh->rtnh_flags)
942 goto errout;
943
944 attrlen = rtnh_attrlen(rtnh);
945 if (attrlen > 0) {
946 struct nlattr *attrs = rtnh_attrs(rtnh);
947
948 nla_via = nla_find(attrs, attrlen, RTA_VIA);
949 nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
950 }
951
952 err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
953 rtnh->rtnh_ifindex, nla_via, nla_newdst,
954 max_labels, extack);
955 if (err)
956 goto errout;
957
958 if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
959 rt->rt_nhn_alive--;
960
961 rtnh = rtnh_next(rtnh, &remaining);
962 rt->rt_nhn++;
963 } endfor_nexthops(rt);
964
965 return 0;
966
967 errout:
968 return err;
969 }
970
mpls_label_ok(struct net * net,unsigned int * index,struct netlink_ext_ack * extack)971 static bool mpls_label_ok(struct net *net, unsigned int *index,
972 struct netlink_ext_ack *extack)
973 {
974 /* Reserved labels may not be set */
975 if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
976 NL_SET_ERR_MSG(extack,
977 "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
978 return false;
979 }
980
981 /* The full 20 bit range may not be supported. */
982 if (*index >= net->mpls.platform_labels) {
983 NL_SET_ERR_MSG(extack,
984 "Label >= configured maximum in platform_labels");
985 return false;
986 }
987
988 *index = array_index_nospec(*index, net->mpls.platform_labels);
989
990 return true;
991 }
992
mpls_route_add(struct mpls_route_config * cfg,struct netlink_ext_ack * extack)993 static int mpls_route_add(struct mpls_route_config *cfg,
994 struct netlink_ext_ack *extack)
995 {
996 struct net *net = cfg->rc_nlinfo.nl_net;
997 struct mpls_route *rt, *old;
998 int err = -EINVAL;
999 u8 max_via_alen;
1000 unsigned index;
1001 u8 max_labels;
1002 u8 nhs;
1003
1004 index = cfg->rc_label;
1005
1006 /* If a label was not specified during insert pick one */
1007 if ((index == LABEL_NOT_SPECIFIED) &&
1008 (cfg->rc_nlflags & NLM_F_CREATE)) {
1009 index = find_free_label(net);
1010 }
1011
1012 if (!mpls_label_ok(net, &index, extack))
1013 goto errout;
1014
1015 /* Append makes no sense with mpls */
1016 err = -EOPNOTSUPP;
1017 if (cfg->rc_nlflags & NLM_F_APPEND) {
1018 NL_SET_ERR_MSG(extack, "MPLS does not support route append");
1019 goto errout;
1020 }
1021
1022 err = -EEXIST;
1023 old = mpls_route_input(net, index);
1024 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
1025 goto errout;
1026
1027 err = -EEXIST;
1028 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
1029 goto errout;
1030
1031 err = -ENOENT;
1032 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
1033 goto errout;
1034
1035 err = -EINVAL;
1036 if (cfg->rc_mp) {
1037 nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
1038 cfg->rc_via_alen, &max_via_alen,
1039 &max_labels);
1040 } else {
1041 max_via_alen = cfg->rc_via_alen;
1042 max_labels = cfg->rc_output_labels;
1043 nhs = 1;
1044 }
1045
1046 if (nhs == 0) {
1047 NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
1048 goto errout;
1049 }
1050
1051 rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
1052 if (IS_ERR(rt)) {
1053 err = PTR_ERR(rt);
1054 goto errout;
1055 }
1056
1057 rt->rt_protocol = cfg->rc_protocol;
1058 rt->rt_payload_type = cfg->rc_payload_type;
1059 rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
1060
1061 if (cfg->rc_mp)
1062 err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
1063 else
1064 err = mpls_nh_build_from_cfg(cfg, rt);
1065 if (err)
1066 goto freert;
1067
1068 mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
1069
1070 return 0;
1071
1072 freert:
1073 mpls_rt_free(rt);
1074 errout:
1075 return err;
1076 }
1077
mpls_route_del(struct mpls_route_config * cfg,struct netlink_ext_ack * extack)1078 static int mpls_route_del(struct mpls_route_config *cfg,
1079 struct netlink_ext_ack *extack)
1080 {
1081 struct net *net = cfg->rc_nlinfo.nl_net;
1082 unsigned index;
1083 int err = -EINVAL;
1084
1085 index = cfg->rc_label;
1086
1087 if (!mpls_label_ok(net, &index, extack))
1088 goto errout;
1089
1090 mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
1091
1092 err = 0;
1093 errout:
1094 return err;
1095 }
1096
mpls_get_stats(struct mpls_dev * mdev,struct mpls_link_stats * stats)1097 static void mpls_get_stats(struct mpls_dev *mdev,
1098 struct mpls_link_stats *stats)
1099 {
1100 struct mpls_pcpu_stats *p;
1101 int i;
1102
1103 memset(stats, 0, sizeof(*stats));
1104
1105 for_each_possible_cpu(i) {
1106 struct mpls_link_stats local;
1107 unsigned int start;
1108
1109 p = per_cpu_ptr(mdev->stats, i);
1110 do {
1111 start = u64_stats_fetch_begin(&p->syncp);
1112 local = p->stats;
1113 } while (u64_stats_fetch_retry(&p->syncp, start));
1114
1115 stats->rx_packets += local.rx_packets;
1116 stats->rx_bytes += local.rx_bytes;
1117 stats->tx_packets += local.tx_packets;
1118 stats->tx_bytes += local.tx_bytes;
1119 stats->rx_errors += local.rx_errors;
1120 stats->tx_errors += local.tx_errors;
1121 stats->rx_dropped += local.rx_dropped;
1122 stats->tx_dropped += local.tx_dropped;
1123 stats->rx_noroute += local.rx_noroute;
1124 }
1125 }
1126
mpls_fill_stats_af(struct sk_buff * skb,const struct net_device * dev)1127 static int mpls_fill_stats_af(struct sk_buff *skb,
1128 const struct net_device *dev)
1129 {
1130 struct mpls_link_stats *stats;
1131 struct mpls_dev *mdev;
1132 struct nlattr *nla;
1133
1134 mdev = mpls_dev_rcu(dev);
1135 if (!mdev)
1136 return -ENODATA;
1137
1138 nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
1139 sizeof(struct mpls_link_stats),
1140 MPLS_STATS_UNSPEC);
1141 if (!nla)
1142 return -EMSGSIZE;
1143
1144 stats = nla_data(nla);
1145 mpls_get_stats(mdev, stats);
1146
1147 return 0;
1148 }
1149
mpls_get_stats_af_size(const struct net_device * dev)1150 static size_t mpls_get_stats_af_size(const struct net_device *dev)
1151 {
1152 struct mpls_dev *mdev;
1153
1154 mdev = mpls_dev_rcu(dev);
1155 if (!mdev)
1156 return 0;
1157
1158 return nla_total_size_64bit(sizeof(struct mpls_link_stats));
1159 }
1160
mpls_netconf_fill_devconf(struct sk_buff * skb,struct mpls_dev * mdev,u32 portid,u32 seq,int event,unsigned int flags,int type)1161 static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
1162 u32 portid, u32 seq, int event,
1163 unsigned int flags, int type)
1164 {
1165 struct nlmsghdr *nlh;
1166 struct netconfmsg *ncm;
1167 bool all = false;
1168
1169 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1170 flags);
1171 if (!nlh)
1172 return -EMSGSIZE;
1173
1174 if (type == NETCONFA_ALL)
1175 all = true;
1176
1177 ncm = nlmsg_data(nlh);
1178 ncm->ncm_family = AF_MPLS;
1179
1180 if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
1181 goto nla_put_failure;
1182
1183 if ((all || type == NETCONFA_INPUT) &&
1184 nla_put_s32(skb, NETCONFA_INPUT,
1185 READ_ONCE(mdev->input_enabled)) < 0)
1186 goto nla_put_failure;
1187
1188 nlmsg_end(skb, nlh);
1189 return 0;
1190
1191 nla_put_failure:
1192 nlmsg_cancel(skb, nlh);
1193 return -EMSGSIZE;
1194 }
1195
mpls_netconf_msgsize_devconf(int type)1196 static int mpls_netconf_msgsize_devconf(int type)
1197 {
1198 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1199 + nla_total_size(4); /* NETCONFA_IFINDEX */
1200 bool all = false;
1201
1202 if (type == NETCONFA_ALL)
1203 all = true;
1204
1205 if (all || type == NETCONFA_INPUT)
1206 size += nla_total_size(4);
1207
1208 return size;
1209 }
1210
mpls_netconf_notify_devconf(struct net * net,int event,int type,struct mpls_dev * mdev)1211 static void mpls_netconf_notify_devconf(struct net *net, int event,
1212 int type, struct mpls_dev *mdev)
1213 {
1214 struct sk_buff *skb;
1215 int err = -ENOBUFS;
1216
1217 skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
1218 if (!skb)
1219 goto errout;
1220
1221 err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
1222 if (err < 0) {
1223 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1224 WARN_ON(err == -EMSGSIZE);
1225 kfree_skb(skb);
1226 goto errout;
1227 }
1228
1229 rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
1230 return;
1231 errout:
1232 rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
1233 }
1234
1235 static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
1236 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
1237 };
1238
mpls_netconf_valid_get_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)1239 static int mpls_netconf_valid_get_req(struct sk_buff *skb,
1240 const struct nlmsghdr *nlh,
1241 struct nlattr **tb,
1242 struct netlink_ext_ack *extack)
1243 {
1244 int i, err;
1245
1246 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
1247 NL_SET_ERR_MSG_MOD(extack,
1248 "Invalid header for netconf get request");
1249 return -EINVAL;
1250 }
1251
1252 if (!netlink_strict_get_check(skb))
1253 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
1254 tb, NETCONFA_MAX,
1255 devconf_mpls_policy, extack);
1256
1257 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
1258 tb, NETCONFA_MAX,
1259 devconf_mpls_policy, extack);
1260 if (err)
1261 return err;
1262
1263 for (i = 0; i <= NETCONFA_MAX; i++) {
1264 if (!tb[i])
1265 continue;
1266
1267 switch (i) {
1268 case NETCONFA_IFINDEX:
1269 break;
1270 default:
1271 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
1272 return -EINVAL;
1273 }
1274 }
1275
1276 return 0;
1277 }
1278
mpls_netconf_get_devconf(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)1279 static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
1280 struct nlmsghdr *nlh,
1281 struct netlink_ext_ack *extack)
1282 {
1283 struct net *net = sock_net(in_skb->sk);
1284 struct nlattr *tb[NETCONFA_MAX + 1];
1285 struct net_device *dev;
1286 struct mpls_dev *mdev;
1287 struct sk_buff *skb;
1288 int ifindex;
1289 int err;
1290
1291 err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
1292 if (err < 0)
1293 goto errout;
1294
1295 if (!tb[NETCONFA_IFINDEX]) {
1296 err = -EINVAL;
1297 goto errout;
1298 }
1299
1300 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
1301
1302 skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
1303 if (!skb) {
1304 err = -ENOBUFS;
1305 goto errout;
1306 }
1307
1308 rcu_read_lock();
1309
1310 dev = dev_get_by_index_rcu(net, ifindex);
1311 if (!dev) {
1312 err = -EINVAL;
1313 goto errout_unlock;
1314 }
1315
1316 mdev = mpls_dev_rcu(dev);
1317 if (!mdev) {
1318 err = -EINVAL;
1319 goto errout_unlock;
1320 }
1321
1322 err = mpls_netconf_fill_devconf(skb, mdev,
1323 NETLINK_CB(in_skb).portid,
1324 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
1325 NETCONFA_ALL);
1326 if (err < 0) {
1327 /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
1328 WARN_ON(err == -EMSGSIZE);
1329 goto errout_unlock;
1330 }
1331
1332 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1333
1334 rcu_read_unlock();
1335 errout:
1336 return err;
1337
1338 errout_unlock:
1339 rcu_read_unlock();
1340 kfree_skb(skb);
1341 goto errout;
1342 }
1343
mpls_netconf_dump_devconf(struct sk_buff * skb,struct netlink_callback * cb)1344 static int mpls_netconf_dump_devconf(struct sk_buff *skb,
1345 struct netlink_callback *cb)
1346 {
1347 const struct nlmsghdr *nlh = cb->nlh;
1348 struct net *net = sock_net(skb->sk);
1349 struct {
1350 unsigned long ifindex;
1351 } *ctx = (void *)cb->ctx;
1352 struct net_device *dev;
1353 struct mpls_dev *mdev;
1354 int err = 0;
1355
1356 if (cb->strict_check) {
1357 struct netlink_ext_ack *extack = cb->extack;
1358 struct netconfmsg *ncm;
1359
1360 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
1361 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
1362 return -EINVAL;
1363 }
1364
1365 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
1366 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
1367 return -EINVAL;
1368 }
1369 }
1370
1371 rcu_read_lock();
1372 for_each_netdev_dump(net, dev, ctx->ifindex) {
1373 mdev = mpls_dev_rcu(dev);
1374 if (!mdev)
1375 continue;
1376 err = mpls_netconf_fill_devconf(skb, mdev,
1377 NETLINK_CB(cb->skb).portid,
1378 nlh->nlmsg_seq,
1379 RTM_NEWNETCONF,
1380 NLM_F_MULTI,
1381 NETCONFA_ALL);
1382 if (err < 0)
1383 break;
1384 }
1385 rcu_read_unlock();
1386
1387 return err;
1388 }
1389
1390 #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
1391 (&((struct mpls_dev *)0)->field)
1392
mpls_conf_proc(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)1393 static int mpls_conf_proc(const struct ctl_table *ctl, int write,
1394 void *buffer, size_t *lenp, loff_t *ppos)
1395 {
1396 int oval = *(int *)ctl->data;
1397 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1398
1399 if (write) {
1400 struct mpls_dev *mdev = ctl->extra1;
1401 int i = (int *)ctl->data - (int *)mdev;
1402 struct net *net = ctl->extra2;
1403 int val = *(int *)ctl->data;
1404
1405 if (i == offsetof(struct mpls_dev, input_enabled) &&
1406 val != oval) {
1407 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
1408 NETCONFA_INPUT, mdev);
1409 }
1410 }
1411
1412 return ret;
1413 }
1414
1415 static const struct ctl_table mpls_dev_table[] = {
1416 {
1417 .procname = "input",
1418 .maxlen = sizeof(int),
1419 .mode = 0644,
1420 .proc_handler = mpls_conf_proc,
1421 .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
1422 },
1423 };
1424
mpls_dev_sysctl_register(struct net_device * dev,struct mpls_dev * mdev)1425 static int mpls_dev_sysctl_register(struct net_device *dev,
1426 struct mpls_dev *mdev)
1427 {
1428 char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
1429 size_t table_size = ARRAY_SIZE(mpls_dev_table);
1430 struct net *net = dev_net(dev);
1431 struct ctl_table *table;
1432 int i;
1433
1434 table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
1435 if (!table)
1436 goto out;
1437
1438 /* Table data contains only offsets relative to the base of
1439 * the mdev at this point, so make them absolute.
1440 */
1441 for (i = 0; i < table_size; i++) {
1442 table[i].data = (char *)mdev + (uintptr_t)table[i].data;
1443 table[i].extra1 = mdev;
1444 table[i].extra2 = net;
1445 }
1446
1447 snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
1448
1449 mdev->sysctl = register_net_sysctl_sz(net, path, table, table_size);
1450 if (!mdev->sysctl)
1451 goto free;
1452
1453 mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
1454 return 0;
1455
1456 free:
1457 kfree(table);
1458 out:
1459 mdev->sysctl = NULL;
1460 return -ENOBUFS;
1461 }
1462
mpls_dev_sysctl_unregister(struct net_device * dev,struct mpls_dev * mdev)1463 static void mpls_dev_sysctl_unregister(struct net_device *dev,
1464 struct mpls_dev *mdev)
1465 {
1466 struct net *net = dev_net(dev);
1467 const struct ctl_table *table;
1468
1469 if (!mdev->sysctl)
1470 return;
1471
1472 table = mdev->sysctl->ctl_table_arg;
1473 unregister_net_sysctl_table(mdev->sysctl);
1474 kfree(table);
1475
1476 mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
1477 }
1478
mpls_add_dev(struct net_device * dev)1479 static struct mpls_dev *mpls_add_dev(struct net_device *dev)
1480 {
1481 struct mpls_dev *mdev;
1482 int err = -ENOMEM;
1483 int i;
1484
1485 mdev = kzalloc_obj(*mdev);
1486 if (!mdev)
1487 return ERR_PTR(err);
1488
1489 mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
1490 if (!mdev->stats)
1491 goto free;
1492
1493 for_each_possible_cpu(i) {
1494 struct mpls_pcpu_stats *mpls_stats;
1495
1496 mpls_stats = per_cpu_ptr(mdev->stats, i);
1497 u64_stats_init(&mpls_stats->syncp);
1498 }
1499
1500 mdev->dev = dev;
1501
1502 err = mpls_dev_sysctl_register(dev, mdev);
1503 if (err)
1504 goto free;
1505
1506 rcu_assign_pointer(dev->mpls_ptr, mdev);
1507
1508 return mdev;
1509
1510 free:
1511 free_percpu(mdev->stats);
1512 kfree(mdev);
1513 return ERR_PTR(err);
1514 }
1515
mpls_dev_destroy_rcu(struct rcu_head * head)1516 static void mpls_dev_destroy_rcu(struct rcu_head *head)
1517 {
1518 struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
1519
1520 free_percpu(mdev->stats);
1521 kfree(mdev);
1522 }
1523
mpls_ifdown(struct net_device * dev,int event)1524 static int mpls_ifdown(struct net_device *dev, int event)
1525 {
1526 struct net *net = dev_net(dev);
1527 unsigned int index;
1528
1529 for (index = 0; index < net->mpls.platform_labels; index++) {
1530 struct mpls_route *rt;
1531 bool nh_del = false;
1532 u8 alive = 0;
1533
1534 rt = mpls_route_input(net, index);
1535 if (!rt)
1536 continue;
1537
1538 if (event == NETDEV_UNREGISTER) {
1539 u8 deleted = 0;
1540
1541 for_nexthops(rt) {
1542 if (!nh->nh_dev || nh->nh_dev == dev)
1543 deleted++;
1544 if (nh->nh_dev == dev)
1545 nh_del = true;
1546 } endfor_nexthops(rt);
1547
1548 /* if there are no more nexthops, delete the route */
1549 if (deleted == rt->rt_nhn) {
1550 mpls_route_update(net, index, NULL, NULL);
1551 continue;
1552 }
1553
1554 if (nh_del) {
1555 size_t size = sizeof(*rt) + rt->rt_nhn *
1556 rt->rt_nh_size;
1557 struct mpls_route *orig = rt;
1558
1559 rt = kmemdup(orig, size, GFP_KERNEL);
1560 if (!rt)
1561 return -ENOMEM;
1562 }
1563 }
1564
1565 change_nexthops(rt) {
1566 unsigned int nh_flags = nh->nh_flags;
1567
1568 if (nh->nh_dev != dev) {
1569 if (nh_del)
1570 netdev_hold(nh->nh_dev, &nh->nh_dev_tracker,
1571 GFP_KERNEL);
1572 goto next;
1573 }
1574
1575 switch (event) {
1576 case NETDEV_DOWN:
1577 case NETDEV_UNREGISTER:
1578 nh_flags |= RTNH_F_DEAD;
1579 fallthrough;
1580 case NETDEV_CHANGE:
1581 nh_flags |= RTNH_F_LINKDOWN;
1582 break;
1583 }
1584 if (event == NETDEV_UNREGISTER)
1585 nh->nh_dev = NULL;
1586
1587 if (nh->nh_flags != nh_flags)
1588 WRITE_ONCE(nh->nh_flags, nh_flags);
1589 next:
1590 if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
1591 alive++;
1592 } endfor_nexthops(rt);
1593
1594 WRITE_ONCE(rt->rt_nhn_alive, alive);
1595
1596 if (nh_del)
1597 mpls_route_update(net, index, rt, NULL);
1598 }
1599
1600 return 0;
1601 }
1602
mpls_ifup(struct net_device * dev,unsigned int flags)1603 static void mpls_ifup(struct net_device *dev, unsigned int flags)
1604 {
1605 struct net *net = dev_net(dev);
1606 unsigned int index;
1607 u8 alive;
1608
1609 for (index = 0; index < net->mpls.platform_labels; index++) {
1610 struct mpls_route *rt;
1611
1612 rt = mpls_route_input(net, index);
1613 if (!rt)
1614 continue;
1615
1616 alive = 0;
1617 change_nexthops(rt) {
1618 unsigned int nh_flags = nh->nh_flags;
1619
1620 if (!(nh_flags & flags)) {
1621 alive++;
1622 continue;
1623 }
1624 if (nh->nh_dev != dev)
1625 continue;
1626 alive++;
1627 nh_flags &= ~flags;
1628 WRITE_ONCE(nh->nh_flags, nh_flags);
1629 } endfor_nexthops(rt);
1630
1631 WRITE_ONCE(rt->rt_nhn_alive, alive);
1632 }
1633 }
1634
mpls_dev_notify(struct notifier_block * this,unsigned long event,void * ptr)1635 static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
1636 void *ptr)
1637 {
1638 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1639 struct net *net = dev_net(dev);
1640 struct mpls_dev *mdev;
1641 unsigned int flags;
1642 int err;
1643
1644 mutex_lock(&net->mpls.platform_mutex);
1645
1646 if (event == NETDEV_REGISTER) {
1647 mdev = mpls_add_dev(dev);
1648 if (IS_ERR(mdev)) {
1649 err = PTR_ERR(mdev);
1650 goto err;
1651 }
1652
1653 goto out;
1654 }
1655
1656 mdev = mpls_dev_get(net, dev);
1657 if (!mdev)
1658 goto out;
1659
1660 switch (event) {
1661
1662 case NETDEV_DOWN:
1663 err = mpls_ifdown(dev, event);
1664 if (err)
1665 goto err;
1666 break;
1667 case NETDEV_UP:
1668 flags = netif_get_flags(dev);
1669 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1670 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1671 else
1672 mpls_ifup(dev, RTNH_F_DEAD);
1673 break;
1674 case NETDEV_CHANGE:
1675 flags = netif_get_flags(dev);
1676 if (flags & (IFF_RUNNING | IFF_LOWER_UP)) {
1677 mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
1678 } else {
1679 err = mpls_ifdown(dev, event);
1680 if (err)
1681 goto err;
1682 }
1683 break;
1684 case NETDEV_UNREGISTER:
1685 err = mpls_ifdown(dev, event);
1686 if (err)
1687 goto err;
1688
1689 mdev = mpls_dev_get(net, dev);
1690 if (mdev) {
1691 mpls_dev_sysctl_unregister(dev, mdev);
1692 RCU_INIT_POINTER(dev->mpls_ptr, NULL);
1693 call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
1694 }
1695 break;
1696 case NETDEV_CHANGENAME:
1697 mdev = mpls_dev_get(net, dev);
1698 if (mdev) {
1699 mpls_dev_sysctl_unregister(dev, mdev);
1700 err = mpls_dev_sysctl_register(dev, mdev);
1701 if (err)
1702 goto err;
1703 }
1704 break;
1705 }
1706
1707 out:
1708 mutex_unlock(&net->mpls.platform_mutex);
1709 return NOTIFY_OK;
1710
1711 err:
1712 mutex_unlock(&net->mpls.platform_mutex);
1713 return notifier_from_errno(err);
1714 }
1715
1716 static struct notifier_block mpls_dev_notifier = {
1717 .notifier_call = mpls_dev_notify,
1718 };
1719
nla_put_via(struct sk_buff * skb,u8 table,const void * addr,int alen)1720 static int nla_put_via(struct sk_buff *skb,
1721 u8 table, const void *addr, int alen)
1722 {
1723 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
1724 AF_INET, AF_INET6, AF_PACKET,
1725 };
1726 struct nlattr *nla;
1727 struct rtvia *via;
1728 int family = AF_UNSPEC;
1729
1730 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1731 if (!nla)
1732 return -EMSGSIZE;
1733
1734 if (table <= NEIGH_NR_TABLES)
1735 family = table_to_family[table];
1736
1737 via = nla_data(nla);
1738 via->rtvia_family = family;
1739 memcpy(via->rtvia_addr, addr, alen);
1740 return 0;
1741 }
1742
nla_put_labels(struct sk_buff * skb,int attrtype,u8 labels,const u32 label[])1743 int nla_put_labels(struct sk_buff *skb, int attrtype,
1744 u8 labels, const u32 label[])
1745 {
1746 struct nlattr *nla;
1747 struct mpls_shim_hdr *nla_label;
1748 bool bos;
1749 int i;
1750 nla = nla_reserve(skb, attrtype, labels*4);
1751 if (!nla)
1752 return -EMSGSIZE;
1753
1754 nla_label = nla_data(nla);
1755 bos = true;
1756 for (i = labels - 1; i >= 0; i--) {
1757 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
1758 bos = false;
1759 }
1760
1761 return 0;
1762 }
1763 EXPORT_SYMBOL_GPL(nla_put_labels);
1764
nla_get_labels(const struct nlattr * nla,u8 max_labels,u8 * labels,u32 label[],struct netlink_ext_ack * extack)1765 int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
1766 u32 label[], struct netlink_ext_ack *extack)
1767 {
1768 unsigned len = nla_len(nla);
1769 struct mpls_shim_hdr *nla_label;
1770 u8 nla_labels;
1771 bool bos;
1772 int i;
1773
1774 /* len needs to be an even multiple of 4 (the label size). Number
1775 * of labels is a u8 so check for overflow.
1776 */
1777 if (len & 3 || len / 4 > 255) {
1778 NL_SET_ERR_MSG_ATTR(extack, nla,
1779 "Invalid length for labels attribute");
1780 return -EINVAL;
1781 }
1782
1783 /* Limit the number of new labels allowed */
1784 nla_labels = len/4;
1785 if (nla_labels > max_labels) {
1786 NL_SET_ERR_MSG(extack, "Too many labels");
1787 return -EINVAL;
1788 }
1789
1790 /* when label == NULL, caller wants number of labels */
1791 if (!label)
1792 goto out;
1793
1794 nla_label = nla_data(nla);
1795 bos = true;
1796 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
1797 struct mpls_entry_decoded dec;
1798 dec = mpls_entry_decode(nla_label + i);
1799
1800 /* Ensure the bottom of stack flag is properly set
1801 * and ttl and tc are both clear.
1802 */
1803 if (dec.ttl) {
1804 NL_SET_ERR_MSG_ATTR(extack, nla,
1805 "TTL in label must be 0");
1806 return -EINVAL;
1807 }
1808
1809 if (dec.tc) {
1810 NL_SET_ERR_MSG_ATTR(extack, nla,
1811 "Traffic class in label must be 0");
1812 return -EINVAL;
1813 }
1814
1815 if (dec.bos != bos) {
1816 NL_SET_BAD_ATTR(extack, nla);
1817 if (bos) {
1818 NL_SET_ERR_MSG(extack,
1819 "BOS bit must be set in first label");
1820 } else {
1821 NL_SET_ERR_MSG(extack,
1822 "BOS bit can only be set in first label");
1823 }
1824 return -EINVAL;
1825 }
1826
1827 switch (dec.label) {
1828 case MPLS_LABEL_IMPLNULL:
1829 /* RFC3032: This is a label that an LSR may
1830 * assign and distribute, but which never
1831 * actually appears in the encapsulation.
1832 */
1833 NL_SET_ERR_MSG_ATTR(extack, nla,
1834 "Implicit NULL Label (3) can not be used in encapsulation");
1835 return -EINVAL;
1836 }
1837
1838 label[i] = dec.label;
1839 }
1840 out:
1841 *labels = nla_labels;
1842 return 0;
1843 }
1844 EXPORT_SYMBOL_GPL(nla_get_labels);
1845
rtm_to_route_config(struct sk_buff * skb,struct nlmsghdr * nlh,struct mpls_route_config * cfg,struct netlink_ext_ack * extack)1846 static int rtm_to_route_config(struct sk_buff *skb,
1847 struct nlmsghdr *nlh,
1848 struct mpls_route_config *cfg,
1849 struct netlink_ext_ack *extack)
1850 {
1851 struct rtmsg *rtm;
1852 struct nlattr *tb[RTA_MAX+1];
1853 int index;
1854 int err;
1855
1856 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
1857 rtm_mpls_policy, extack);
1858 if (err < 0)
1859 goto errout;
1860
1861 err = -EINVAL;
1862 rtm = nlmsg_data(nlh);
1863
1864 if (rtm->rtm_family != AF_MPLS) {
1865 NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
1866 goto errout;
1867 }
1868 if (rtm->rtm_dst_len != 20) {
1869 NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
1870 goto errout;
1871 }
1872 if (rtm->rtm_src_len != 0) {
1873 NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
1874 goto errout;
1875 }
1876 if (rtm->rtm_tos != 0) {
1877 NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
1878 goto errout;
1879 }
1880 if (rtm->rtm_table != RT_TABLE_MAIN) {
1881 NL_SET_ERR_MSG(extack,
1882 "MPLS only supports the main route table");
1883 goto errout;
1884 }
1885 /* Any value is acceptable for rtm_protocol */
1886
1887 /* As mpls uses destination specific addresses
1888 * (or source specific address in the case of multicast)
1889 * all addresses have universal scope.
1890 */
1891 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
1892 NL_SET_ERR_MSG(extack,
1893 "Invalid route scope - MPLS only supports UNIVERSE");
1894 goto errout;
1895 }
1896 if (rtm->rtm_type != RTN_UNICAST) {
1897 NL_SET_ERR_MSG(extack,
1898 "Invalid route type - MPLS only supports UNICAST");
1899 goto errout;
1900 }
1901 if (rtm->rtm_flags != 0) {
1902 NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
1903 goto errout;
1904 }
1905
1906 cfg->rc_label = LABEL_NOT_SPECIFIED;
1907 cfg->rc_protocol = rtm->rtm_protocol;
1908 cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
1909 cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
1910 cfg->rc_nlflags = nlh->nlmsg_flags;
1911 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
1912 cfg->rc_nlinfo.nlh = nlh;
1913 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
1914
1915 for (index = 0; index <= RTA_MAX; index++) {
1916 struct nlattr *nla = tb[index];
1917 if (!nla)
1918 continue;
1919
1920 switch (index) {
1921 case RTA_OIF:
1922 cfg->rc_ifindex = nla_get_u32(nla);
1923 break;
1924 case RTA_NEWDST:
1925 if (nla_get_labels(nla, MAX_NEW_LABELS,
1926 &cfg->rc_output_labels,
1927 cfg->rc_output_label, extack))
1928 goto errout;
1929 break;
1930 case RTA_DST:
1931 {
1932 u8 label_count;
1933 if (nla_get_labels(nla, 1, &label_count,
1934 &cfg->rc_label, extack))
1935 goto errout;
1936
1937 if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
1938 &cfg->rc_label, extack))
1939 goto errout;
1940 break;
1941 }
1942 case RTA_GATEWAY:
1943 NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
1944 goto errout;
1945 case RTA_VIA:
1946 {
1947 if (nla_get_via(nla, &cfg->rc_via_alen,
1948 &cfg->rc_via_table, cfg->rc_via,
1949 extack))
1950 goto errout;
1951 break;
1952 }
1953 case RTA_MULTIPATH:
1954 {
1955 cfg->rc_mp = nla_data(nla);
1956 cfg->rc_mp_len = nla_len(nla);
1957 break;
1958 }
1959 case RTA_TTL_PROPAGATE:
1960 {
1961 u8 ttl_propagate = nla_get_u8(nla);
1962
1963 if (ttl_propagate > 1) {
1964 NL_SET_ERR_MSG_ATTR(extack, nla,
1965 "RTA_TTL_PROPAGATE can only be 0 or 1");
1966 goto errout;
1967 }
1968 cfg->rc_ttl_propagate = ttl_propagate ?
1969 MPLS_TTL_PROP_ENABLED :
1970 MPLS_TTL_PROP_DISABLED;
1971 break;
1972 }
1973 default:
1974 NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
1975 /* Unsupported attribute */
1976 goto errout;
1977 }
1978 }
1979
1980 err = 0;
1981 errout:
1982 return err;
1983 }
1984
mpls_rtm_delroute(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)1985 static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1986 struct netlink_ext_ack *extack)
1987 {
1988 struct net *net = sock_net(skb->sk);
1989 struct mpls_route_config *cfg;
1990 int err;
1991
1992 cfg = kzalloc_obj(*cfg);
1993 if (!cfg)
1994 return -ENOMEM;
1995
1996 err = rtm_to_route_config(skb, nlh, cfg, extack);
1997 if (err < 0)
1998 goto out;
1999
2000 mutex_lock(&net->mpls.platform_mutex);
2001 err = mpls_route_del(cfg, extack);
2002 mutex_unlock(&net->mpls.platform_mutex);
2003 out:
2004 kfree(cfg);
2005
2006 return err;
2007 }
2008
2009
mpls_rtm_newroute(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)2010 static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
2011 struct netlink_ext_ack *extack)
2012 {
2013 struct net *net = sock_net(skb->sk);
2014 struct mpls_route_config *cfg;
2015 int err;
2016
2017 cfg = kzalloc_obj(*cfg);
2018 if (!cfg)
2019 return -ENOMEM;
2020
2021 err = rtm_to_route_config(skb, nlh, cfg, extack);
2022 if (err < 0)
2023 goto out;
2024
2025 mutex_lock(&net->mpls.platform_mutex);
2026 err = mpls_route_add(cfg, extack);
2027 mutex_unlock(&net->mpls.platform_mutex);
2028 out:
2029 kfree(cfg);
2030
2031 return err;
2032 }
2033
mpls_dump_route(struct sk_buff * skb,u32 portid,u32 seq,int event,u32 label,struct mpls_route * rt,int flags)2034 static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
2035 u32 label, struct mpls_route *rt, int flags)
2036 {
2037 struct net_device *dev;
2038 struct nlmsghdr *nlh;
2039 struct rtmsg *rtm;
2040
2041 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
2042 if (nlh == NULL)
2043 return -EMSGSIZE;
2044
2045 rtm = nlmsg_data(nlh);
2046 rtm->rtm_family = AF_MPLS;
2047 rtm->rtm_dst_len = 20;
2048 rtm->rtm_src_len = 0;
2049 rtm->rtm_tos = 0;
2050 rtm->rtm_table = RT_TABLE_MAIN;
2051 rtm->rtm_protocol = rt->rt_protocol;
2052 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2053 rtm->rtm_type = RTN_UNICAST;
2054 rtm->rtm_flags = 0;
2055
2056 if (nla_put_labels(skb, RTA_DST, 1, &label))
2057 goto nla_put_failure;
2058
2059 if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
2060 bool ttl_propagate =
2061 rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
2062
2063 if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
2064 ttl_propagate))
2065 goto nla_put_failure;
2066 }
2067 if (rt->rt_nhn == 1) {
2068 const struct mpls_nh *nh = rt->rt_nh;
2069
2070 if (nh->nh_labels &&
2071 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
2072 nh->nh_label))
2073 goto nla_put_failure;
2074 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2075 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
2076 nh->nh_via_alen))
2077 goto nla_put_failure;
2078 dev = nh->nh_dev;
2079 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
2080 goto nla_put_failure;
2081 if (nh->nh_flags & RTNH_F_LINKDOWN)
2082 rtm->rtm_flags |= RTNH_F_LINKDOWN;
2083 if (nh->nh_flags & RTNH_F_DEAD)
2084 rtm->rtm_flags |= RTNH_F_DEAD;
2085 } else {
2086 struct rtnexthop *rtnh;
2087 struct nlattr *mp;
2088 u8 linkdown = 0;
2089 u8 dead = 0;
2090
2091 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
2092 if (!mp)
2093 goto nla_put_failure;
2094
2095 for_nexthops(rt) {
2096 dev = nh->nh_dev;
2097 if (!dev)
2098 continue;
2099
2100 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
2101 if (!rtnh)
2102 goto nla_put_failure;
2103
2104 rtnh->rtnh_ifindex = dev->ifindex;
2105 if (nh->nh_flags & RTNH_F_LINKDOWN) {
2106 rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
2107 linkdown++;
2108 }
2109 if (nh->nh_flags & RTNH_F_DEAD) {
2110 rtnh->rtnh_flags |= RTNH_F_DEAD;
2111 dead++;
2112 }
2113
2114 if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
2115 nh->nh_labels,
2116 nh->nh_label))
2117 goto nla_put_failure;
2118 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2119 nla_put_via(skb, nh->nh_via_table,
2120 mpls_nh_via(rt, nh),
2121 nh->nh_via_alen))
2122 goto nla_put_failure;
2123
2124 /* length of rtnetlink header + attributes */
2125 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
2126 } endfor_nexthops(rt);
2127
2128 if (linkdown == rt->rt_nhn)
2129 rtm->rtm_flags |= RTNH_F_LINKDOWN;
2130 if (dead == rt->rt_nhn)
2131 rtm->rtm_flags |= RTNH_F_DEAD;
2132
2133 nla_nest_end(skb, mp);
2134 }
2135
2136 nlmsg_end(skb, nlh);
2137 return 0;
2138
2139 nla_put_failure:
2140 nlmsg_cancel(skb, nlh);
2141 return -EMSGSIZE;
2142 }
2143
2144 #if IS_ENABLED(CONFIG_INET)
mpls_valid_fib_dump_req(struct net * net,const struct nlmsghdr * nlh,struct fib_dump_filter * filter,struct netlink_callback * cb)2145 static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
2146 struct fib_dump_filter *filter,
2147 struct netlink_callback *cb)
2148 {
2149 return ip_valid_fib_dump_req(net, nlh, filter, cb);
2150 }
2151 #else
mpls_valid_fib_dump_req(struct net * net,const struct nlmsghdr * nlh,struct fib_dump_filter * filter,struct netlink_callback * cb)2152 static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
2153 struct fib_dump_filter *filter,
2154 struct netlink_callback *cb)
2155 {
2156 struct netlink_ext_ack *extack = cb->extack;
2157 struct nlattr *tb[RTA_MAX + 1];
2158 struct rtmsg *rtm;
2159 int err, i;
2160
2161 rtm = nlmsg_payload(nlh, sizeof(*rtm));
2162 if (!rtm) {
2163 NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
2164 return -EINVAL;
2165 }
2166
2167 if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
2168 rtm->rtm_table || rtm->rtm_scope || rtm->rtm_type ||
2169 rtm->rtm_flags) {
2170 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
2171 return -EINVAL;
2172 }
2173
2174 if (rtm->rtm_protocol) {
2175 filter->protocol = rtm->rtm_protocol;
2176 filter->filter_set = 1;
2177 cb->answer_flags = NLM_F_DUMP_FILTERED;
2178 }
2179
2180 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2181 rtm_mpls_policy, extack);
2182 if (err < 0)
2183 return err;
2184
2185 for (i = 0; i <= RTA_MAX; ++i) {
2186 int ifindex;
2187
2188 if (i == RTA_OIF) {
2189 ifindex = nla_get_u32(tb[i]);
2190 filter->dev = dev_get_by_index_rcu(net, ifindex);
2191 if (!filter->dev)
2192 return -ENODEV;
2193 filter->filter_set = 1;
2194 } else if (tb[i]) {
2195 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
2196 return -EINVAL;
2197 }
2198 }
2199
2200 return 0;
2201 }
2202 #endif
2203
mpls_rt_uses_dev(struct mpls_route * rt,const struct net_device * dev)2204 static bool mpls_rt_uses_dev(struct mpls_route *rt,
2205 const struct net_device *dev)
2206 {
2207 if (rt->rt_nhn == 1) {
2208 struct mpls_nh *nh = rt->rt_nh;
2209
2210 if (nh->nh_dev == dev)
2211 return true;
2212 } else {
2213 for_nexthops(rt) {
2214 if (nh->nh_dev == dev)
2215 return true;
2216 } endfor_nexthops(rt);
2217 }
2218
2219 return false;
2220 }
2221
mpls_dump_routes(struct sk_buff * skb,struct netlink_callback * cb)2222 static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
2223 {
2224 const struct nlmsghdr *nlh = cb->nlh;
2225 struct net *net = sock_net(skb->sk);
2226 struct mpls_route __rcu **platform_label;
2227 struct fib_dump_filter filter = {
2228 .rtnl_held = false,
2229 };
2230 unsigned int flags = NLM_F_MULTI;
2231 size_t platform_labels;
2232 unsigned int index;
2233 int err;
2234
2235 rcu_read_lock();
2236
2237 if (cb->strict_check) {
2238 err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
2239 if (err < 0)
2240 goto err;
2241
2242 /* for MPLS, there is only 1 table with fixed type and flags.
2243 * If either are set in the filter then return nothing.
2244 */
2245 if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
2246 (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
2247 filter.flags)
2248 goto unlock;
2249 }
2250
2251 index = cb->args[0];
2252 if (index < MPLS_LABEL_FIRST_UNRESERVED)
2253 index = MPLS_LABEL_FIRST_UNRESERVED;
2254
2255 platform_label = mpls_platform_label_rcu(net, &platform_labels);
2256
2257 if (filter.filter_set)
2258 flags |= NLM_F_DUMP_FILTERED;
2259
2260 for (; index < platform_labels; index++) {
2261 struct mpls_route *rt;
2262
2263 rt = rcu_dereference(platform_label[index]);
2264 if (!rt)
2265 continue;
2266
2267 if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
2268 (filter.protocol && rt->rt_protocol != filter.protocol))
2269 continue;
2270
2271 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
2272 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2273 index, rt, flags) < 0)
2274 break;
2275 }
2276 cb->args[0] = index;
2277
2278 unlock:
2279 rcu_read_unlock();
2280 return skb->len;
2281
2282 err:
2283 rcu_read_unlock();
2284 return err;
2285 }
2286
lfib_nlmsg_size(struct mpls_route * rt)2287 static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
2288 {
2289 size_t payload =
2290 NLMSG_ALIGN(sizeof(struct rtmsg))
2291 + nla_total_size(4) /* RTA_DST */
2292 + nla_total_size(1); /* RTA_TTL_PROPAGATE */
2293
2294 if (rt->rt_nhn == 1) {
2295 struct mpls_nh *nh = rt->rt_nh;
2296
2297 if (nh->nh_dev)
2298 payload += nla_total_size(4); /* RTA_OIF */
2299 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
2300 payload += nla_total_size(2 + nh->nh_via_alen);
2301 if (nh->nh_labels) /* RTA_NEWDST */
2302 payload += nla_total_size(nh->nh_labels * 4);
2303 } else {
2304 /* each nexthop is packed in an attribute */
2305 size_t nhsize = 0;
2306
2307 for_nexthops(rt) {
2308 if (!nh->nh_dev)
2309 continue;
2310 nhsize += nla_total_size(sizeof(struct rtnexthop));
2311 /* RTA_VIA */
2312 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
2313 nhsize += nla_total_size(2 + nh->nh_via_alen);
2314 if (nh->nh_labels)
2315 nhsize += nla_total_size(nh->nh_labels * 4);
2316 } endfor_nexthops(rt);
2317 /* nested attribute */
2318 payload += nla_total_size(nhsize);
2319 }
2320
2321 return payload;
2322 }
2323
rtmsg_lfib(int event,u32 label,struct mpls_route * rt,struct nlmsghdr * nlh,struct net * net,u32 portid,unsigned int nlm_flags)2324 static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
2325 struct nlmsghdr *nlh, struct net *net, u32 portid,
2326 unsigned int nlm_flags)
2327 {
2328 struct sk_buff *skb;
2329 u32 seq = nlh ? nlh->nlmsg_seq : 0;
2330 int err = -ENOBUFS;
2331
2332 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
2333 if (skb == NULL)
2334 goto errout;
2335
2336 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
2337 if (err < 0) {
2338 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2339 WARN_ON(err == -EMSGSIZE);
2340 kfree_skb(skb);
2341 goto errout;
2342 }
2343 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
2344
2345 return;
2346 errout:
2347 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
2348 }
2349
mpls_valid_getroute_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)2350 static int mpls_valid_getroute_req(struct sk_buff *skb,
2351 const struct nlmsghdr *nlh,
2352 struct nlattr **tb,
2353 struct netlink_ext_ack *extack)
2354 {
2355 struct rtmsg *rtm;
2356 int i, err;
2357
2358 rtm = nlmsg_payload(nlh, sizeof(*rtm));
2359 if (!rtm) {
2360 NL_SET_ERR_MSG_MOD(extack,
2361 "Invalid header for get route request");
2362 return -EINVAL;
2363 }
2364
2365 if (!netlink_strict_get_check(skb))
2366 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2367 rtm_mpls_policy, extack);
2368
2369 if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
2370 rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
2371 rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
2372 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
2373 return -EINVAL;
2374 }
2375 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
2376 NL_SET_ERR_MSG_MOD(extack,
2377 "Invalid flags for get route request");
2378 return -EINVAL;
2379 }
2380
2381 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2382 rtm_mpls_policy, extack);
2383 if (err)
2384 return err;
2385
2386 if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
2387 NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
2388 return -EINVAL;
2389 }
2390
2391 for (i = 0; i <= RTA_MAX; i++) {
2392 if (!tb[i])
2393 continue;
2394
2395 switch (i) {
2396 case RTA_DST:
2397 case RTA_NEWDST:
2398 break;
2399 default:
2400 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
2401 return -EINVAL;
2402 }
2403 }
2404
2405 return 0;
2406 }
2407
mpls_getroute(struct sk_buff * in_skb,struct nlmsghdr * in_nlh,struct netlink_ext_ack * extack)2408 static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
2409 struct netlink_ext_ack *extack)
2410 {
2411 struct net *net = sock_net(in_skb->sk);
2412 u32 portid = NETLINK_CB(in_skb).portid;
2413 u32 in_label = LABEL_NOT_SPECIFIED;
2414 struct nlattr *tb[RTA_MAX + 1];
2415 struct mpls_route *rt = NULL;
2416 u32 labels[MAX_NEW_LABELS];
2417 struct mpls_shim_hdr *hdr;
2418 unsigned int hdr_size = 0;
2419 const struct mpls_nh *nh;
2420 struct net_device *dev;
2421 struct rtmsg *rtm, *r;
2422 struct nlmsghdr *nlh;
2423 struct sk_buff *skb;
2424 u8 n_labels;
2425 int err;
2426
2427 mutex_lock(&net->mpls.platform_mutex);
2428
2429 err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
2430 if (err < 0)
2431 goto errout;
2432
2433 rtm = nlmsg_data(in_nlh);
2434
2435 if (tb[RTA_DST]) {
2436 u8 label_count;
2437
2438 if (nla_get_labels(tb[RTA_DST], 1, &label_count,
2439 &in_label, extack)) {
2440 err = -EINVAL;
2441 goto errout;
2442 }
2443
2444 if (!mpls_label_ok(net, &in_label, extack)) {
2445 err = -EINVAL;
2446 goto errout;
2447 }
2448 }
2449
2450 if (in_label < net->mpls.platform_labels)
2451 rt = mpls_route_input(net, in_label);
2452 if (!rt) {
2453 err = -ENETUNREACH;
2454 goto errout;
2455 }
2456
2457 if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
2458 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
2459 if (!skb) {
2460 err = -ENOBUFS;
2461 goto errout;
2462 }
2463
2464 err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
2465 RTM_NEWROUTE, in_label, rt, 0);
2466 if (err < 0) {
2467 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
2468 WARN_ON(err == -EMSGSIZE);
2469 goto errout_free;
2470 }
2471
2472 err = rtnl_unicast(skb, net, portid);
2473 goto errout;
2474 }
2475
2476 if (tb[RTA_NEWDST]) {
2477 if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
2478 labels, extack) != 0) {
2479 err = -EINVAL;
2480 goto errout;
2481 }
2482
2483 hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
2484 }
2485
2486 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2487 if (!skb) {
2488 err = -ENOBUFS;
2489 goto errout;
2490 }
2491
2492 skb->protocol = htons(ETH_P_MPLS_UC);
2493
2494 if (hdr_size) {
2495 bool bos;
2496 int i;
2497
2498 if (skb_cow(skb, hdr_size)) {
2499 err = -ENOBUFS;
2500 goto errout_free;
2501 }
2502
2503 skb_reserve(skb, hdr_size);
2504 skb_push(skb, hdr_size);
2505 skb_reset_network_header(skb);
2506
2507 /* Push new labels */
2508 hdr = mpls_hdr(skb);
2509 bos = true;
2510 for (i = n_labels - 1; i >= 0; i--) {
2511 hdr[i] = mpls_entry_encode(labels[i],
2512 1, 0, bos);
2513 bos = false;
2514 }
2515 }
2516
2517 nh = mpls_select_multipath(rt, skb);
2518 if (!nh) {
2519 err = -ENETUNREACH;
2520 goto errout_free;
2521 }
2522
2523 if (hdr_size) {
2524 skb_pull(skb, hdr_size);
2525 skb_reset_network_header(skb);
2526 }
2527
2528 nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
2529 RTM_NEWROUTE, sizeof(*r), 0);
2530 if (!nlh) {
2531 err = -EMSGSIZE;
2532 goto errout_free;
2533 }
2534
2535 r = nlmsg_data(nlh);
2536 r->rtm_family = AF_MPLS;
2537 r->rtm_dst_len = 20;
2538 r->rtm_src_len = 0;
2539 r->rtm_table = RT_TABLE_MAIN;
2540 r->rtm_type = RTN_UNICAST;
2541 r->rtm_scope = RT_SCOPE_UNIVERSE;
2542 r->rtm_protocol = rt->rt_protocol;
2543 r->rtm_flags = 0;
2544
2545 if (nla_put_labels(skb, RTA_DST, 1, &in_label))
2546 goto nla_put_failure;
2547
2548 if (nh->nh_labels &&
2549 nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
2550 nh->nh_label))
2551 goto nla_put_failure;
2552
2553 if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
2554 nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
2555 nh->nh_via_alen))
2556 goto nla_put_failure;
2557 dev = nh->nh_dev;
2558 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
2559 goto nla_put_failure;
2560
2561 nlmsg_end(skb, nlh);
2562
2563 err = rtnl_unicast(skb, net, portid);
2564 errout:
2565 mutex_unlock(&net->mpls.platform_mutex);
2566 return err;
2567
2568 nla_put_failure:
2569 nlmsg_cancel(skb, nlh);
2570 err = -EMSGSIZE;
2571 errout_free:
2572 mutex_unlock(&net->mpls.platform_mutex);
2573 kfree_skb(skb);
2574 return err;
2575 }
2576
resize_platform_label_table(struct net * net,size_t limit)2577 static int resize_platform_label_table(struct net *net, size_t limit)
2578 {
2579 size_t size = sizeof(struct mpls_route *) * limit;
2580 size_t old_limit;
2581 size_t cp_size;
2582 struct mpls_route __rcu **labels = NULL, **old;
2583 struct mpls_route *rt0 = NULL, *rt2 = NULL;
2584 unsigned index;
2585
2586 if (size) {
2587 labels = kvzalloc(size, GFP_KERNEL);
2588 if (!labels)
2589 goto nolabels;
2590 }
2591
2592 /* In case the predefined labels need to be populated */
2593 if (limit > MPLS_LABEL_IPV4NULL) {
2594 struct net_device *lo = net->loopback_dev;
2595
2596 rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
2597 if (IS_ERR(rt0))
2598 goto nort0;
2599
2600 rt0->rt_nh->nh_dev = lo;
2601 netdev_hold(lo, &rt0->rt_nh->nh_dev_tracker, GFP_KERNEL);
2602 rt0->rt_protocol = RTPROT_KERNEL;
2603 rt0->rt_payload_type = MPT_IPV4;
2604 rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
2605 rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2606 rt0->rt_nh->nh_via_alen = lo->addr_len;
2607 memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
2608 lo->addr_len);
2609 }
2610 if (limit > MPLS_LABEL_IPV6NULL) {
2611 struct net_device *lo = net->loopback_dev;
2612
2613 rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
2614 if (IS_ERR(rt2))
2615 goto nort2;
2616
2617 rt2->rt_nh->nh_dev = lo;
2618 netdev_hold(lo, &rt2->rt_nh->nh_dev_tracker, GFP_KERNEL);
2619 rt2->rt_protocol = RTPROT_KERNEL;
2620 rt2->rt_payload_type = MPT_IPV6;
2621 rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
2622 rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
2623 rt2->rt_nh->nh_via_alen = lo->addr_len;
2624 memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
2625 lo->addr_len);
2626 }
2627
2628 mutex_lock(&net->mpls.platform_mutex);
2629
2630 /* Remember the original table */
2631 old = mpls_dereference(net, net->mpls.platform_label);
2632 old_limit = net->mpls.platform_labels;
2633
2634 /* Free any labels beyond the new table */
2635 for (index = limit; index < old_limit; index++)
2636 mpls_route_update(net, index, NULL, NULL);
2637
2638 /* Copy over the old labels */
2639 cp_size = size;
2640 if (old_limit < limit)
2641 cp_size = old_limit * sizeof(struct mpls_route *);
2642
2643 memcpy(labels, old, cp_size);
2644
2645 /* If needed set the predefined labels */
2646 if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
2647 (limit > MPLS_LABEL_IPV6NULL)) {
2648 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
2649 rt2 = NULL;
2650 }
2651
2652 if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
2653 (limit > MPLS_LABEL_IPV4NULL)) {
2654 RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
2655 rt0 = NULL;
2656 }
2657
2658 /* Update the global pointers */
2659 local_bh_disable();
2660 write_seqcount_begin(&net->mpls.platform_label_seq);
2661 net->mpls.platform_labels = limit;
2662 rcu_assign_pointer(net->mpls.platform_label, labels);
2663 write_seqcount_end(&net->mpls.platform_label_seq);
2664 local_bh_enable();
2665
2666 mutex_unlock(&net->mpls.platform_mutex);
2667
2668 mpls_rt_free(rt2);
2669 mpls_rt_free(rt0);
2670
2671 if (old) {
2672 synchronize_rcu();
2673 kvfree(old);
2674 }
2675 return 0;
2676
2677 nort2:
2678 mpls_rt_free(rt0);
2679 nort0:
2680 kvfree(labels);
2681 nolabels:
2682 return -ENOMEM;
2683 }
2684
mpls_platform_labels(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2685 static int mpls_platform_labels(const struct ctl_table *table, int write,
2686 void *buffer, size_t *lenp, loff_t *ppos)
2687 {
2688 struct net *net = table->data;
2689 int platform_labels = net->mpls.platform_labels;
2690 int ret;
2691 struct ctl_table tmp = {
2692 .procname = table->procname,
2693 .data = &platform_labels,
2694 .maxlen = sizeof(int),
2695 .mode = table->mode,
2696 .extra1 = SYSCTL_ZERO,
2697 .extra2 = &label_limit,
2698 };
2699
2700 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2701
2702 if (write && ret == 0)
2703 ret = resize_platform_label_table(net, platform_labels);
2704
2705 return ret;
2706 }
2707
2708 #define MPLS_NS_SYSCTL_OFFSET(field) \
2709 (&((struct net *)0)->field)
2710
2711 static const struct ctl_table mpls_table[] = {
2712 {
2713 .procname = "platform_labels",
2714 .data = NULL,
2715 .maxlen = sizeof(int),
2716 .mode = 0644,
2717 .proc_handler = mpls_platform_labels,
2718 },
2719 {
2720 .procname = "ip_ttl_propagate",
2721 .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
2722 .maxlen = sizeof(int),
2723 .mode = 0644,
2724 .proc_handler = proc_dointvec_minmax,
2725 .extra1 = SYSCTL_ZERO,
2726 .extra2 = SYSCTL_ONE,
2727 },
2728 {
2729 .procname = "default_ttl",
2730 .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
2731 .maxlen = sizeof(int),
2732 .mode = 0644,
2733 .proc_handler = proc_dointvec_minmax,
2734 .extra1 = SYSCTL_ONE,
2735 .extra2 = &ttl_max,
2736 },
2737 };
2738
mpls_net_init(struct net * net)2739 static __net_init int mpls_net_init(struct net *net)
2740 {
2741 size_t table_size = ARRAY_SIZE(mpls_table);
2742 struct ctl_table *table;
2743 int i;
2744
2745 mutex_init(&net->mpls.platform_mutex);
2746 seqcount_mutex_init(&net->mpls.platform_label_seq, &net->mpls.platform_mutex);
2747
2748 net->mpls.platform_labels = 0;
2749 net->mpls.platform_label = NULL;
2750 net->mpls.ip_ttl_propagate = 1;
2751 net->mpls.default_ttl = 255;
2752
2753 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
2754 if (table == NULL)
2755 return -ENOMEM;
2756
2757 /* Table data contains only offsets relative to the base of
2758 * the mdev at this point, so make them absolute.
2759 */
2760 for (i = 0; i < table_size; i++)
2761 table[i].data = (char *)net + (uintptr_t)table[i].data;
2762
2763 net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
2764 table_size);
2765 if (net->mpls.ctl == NULL) {
2766 kfree(table);
2767 return -ENOMEM;
2768 }
2769
2770 return 0;
2771 }
2772
mpls_net_exit(struct net * net)2773 static __net_exit void mpls_net_exit(struct net *net)
2774 {
2775 struct mpls_route __rcu **platform_label;
2776 size_t platform_labels;
2777 const struct ctl_table *table;
2778 unsigned int index;
2779
2780 table = net->mpls.ctl->ctl_table_arg;
2781 unregister_net_sysctl_table(net->mpls.ctl);
2782 kfree(table);
2783
2784 /* An rcu grace period has passed since there was a device in
2785 * the network namespace (and thus the last in flight packet)
2786 * left this network namespace. This is because
2787 * unregister_netdevice_many and netdev_run_todo has completed
2788 * for each network device that was in this network namespace.
2789 *
2790 * As such no additional rcu synchronization is necessary when
2791 * freeing the platform_label table.
2792 */
2793 mutex_lock(&net->mpls.platform_mutex);
2794
2795 platform_label = mpls_dereference(net, net->mpls.platform_label);
2796 platform_labels = net->mpls.platform_labels;
2797
2798 for (index = 0; index < platform_labels; index++) {
2799 struct mpls_route *rt;
2800
2801 rt = mpls_dereference(net, platform_label[index]);
2802 mpls_notify_route(net, index, rt, NULL, NULL);
2803 mpls_rt_free(rt);
2804 }
2805
2806 mutex_unlock(&net->mpls.platform_mutex);
2807
2808 kvfree(platform_label);
2809 }
2810
2811 static struct pernet_operations mpls_net_ops = {
2812 .init = mpls_net_init,
2813 .exit = mpls_net_exit,
2814 };
2815
2816 static struct rtnl_af_ops mpls_af_ops __read_mostly = {
2817 .family = AF_MPLS,
2818 .fill_stats_af = mpls_fill_stats_af,
2819 .get_stats_af_size = mpls_get_stats_af_size,
2820 };
2821
2822 static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = {
2823 {THIS_MODULE, PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL,
2824 RTNL_FLAG_DOIT_UNLOCKED},
2825 {THIS_MODULE, PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL,
2826 RTNL_FLAG_DOIT_UNLOCKED},
2827 {THIS_MODULE, PF_MPLS, RTM_GETROUTE, mpls_getroute, mpls_dump_routes,
2828 RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
2829 {THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
2830 mpls_netconf_get_devconf, mpls_netconf_dump_devconf,
2831 RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
2832 };
2833
mpls_init(void)2834 static int __init mpls_init(void)
2835 {
2836 int err;
2837
2838 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
2839
2840 err = register_pernet_subsys(&mpls_net_ops);
2841 if (err)
2842 goto out;
2843
2844 err = register_netdevice_notifier(&mpls_dev_notifier);
2845 if (err)
2846 goto out_unregister_pernet;
2847
2848 dev_add_pack(&mpls_packet_type);
2849
2850 err = rtnl_af_register(&mpls_af_ops);
2851 if (err)
2852 goto out_unregister_dev_type;
2853
2854 err = rtnl_register_many(mpls_rtnl_msg_handlers);
2855 if (err)
2856 goto out_unregister_rtnl_af;
2857
2858 err = ipgre_tunnel_encap_add_mpls_ops();
2859 if (err) {
2860 pr_err("Can't add mpls over gre tunnel ops\n");
2861 goto out_unregister_rtnl;
2862 }
2863
2864 err = 0;
2865 out:
2866 return err;
2867
2868 out_unregister_rtnl:
2869 rtnl_unregister_many(mpls_rtnl_msg_handlers);
2870 out_unregister_rtnl_af:
2871 rtnl_af_unregister(&mpls_af_ops);
2872 out_unregister_dev_type:
2873 dev_remove_pack(&mpls_packet_type);
2874 unregister_netdevice_notifier(&mpls_dev_notifier);
2875 out_unregister_pernet:
2876 unregister_pernet_subsys(&mpls_net_ops);
2877 goto out;
2878 }
2879 module_init(mpls_init);
2880
mpls_exit(void)2881 static void __exit mpls_exit(void)
2882 {
2883 rtnl_unregister_all(PF_MPLS);
2884 rtnl_af_unregister(&mpls_af_ops);
2885 dev_remove_pack(&mpls_packet_type);
2886 unregister_netdevice_notifier(&mpls_dev_notifier);
2887 unregister_pernet_subsys(&mpls_net_ops);
2888 ipgre_tunnel_encap_del_mpls_ops();
2889 }
2890 module_exit(mpls_exit);
2891
2892 MODULE_DESCRIPTION("MultiProtocol Label Switching");
2893 MODULE_LICENSE("GPL v2");
2894 MODULE_ALIAS_NETPROTO(PF_MPLS);
2895