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