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