1 // SPDX-License-Identifier: GPL-2.0 2 /* Generic nexthop implementation 3 * 4 * Copyright (c) 2017-19 Cumulus Networks 5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> 6 */ 7 8 #include <linux/nexthop.h> 9 #include <linux/rtnetlink.h> 10 #include <linux/slab.h> 11 #include <linux/vmalloc.h> 12 #include <net/arp.h> 13 #include <net/ip6_route.h> 14 #include <net/lwtunnel.h> 15 #include <net/ndisc.h> 16 #include <net/nexthop.h> 17 #include <net/route.h> 18 #include <net/sock.h> 19 20 #define NH_RES_DEFAULT_IDLE_TIMER (120 * HZ) 21 #define NH_RES_DEFAULT_UNBALANCED_TIMER 0 /* No forced rebalancing. */ 22 23 static void remove_nexthop(struct net *net, struct nexthop *nh, 24 struct nl_info *nlinfo); 25 26 #define NH_DEV_HASHBITS 8 27 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS) 28 29 #define NHA_OP_FLAGS_DUMP_ALL (NHA_OP_FLAG_DUMP_STATS | \ 30 NHA_OP_FLAG_DUMP_HW_STATS) 31 32 static const struct nla_policy rtm_nh_policy_new[] = { 33 [NHA_ID] = { .type = NLA_U32 }, 34 [NHA_GROUP] = { .type = NLA_BINARY }, 35 [NHA_GROUP_TYPE] = { .type = NLA_U16 }, 36 [NHA_BLACKHOLE] = { .type = NLA_FLAG }, 37 [NHA_OIF] = { .type = NLA_U32 }, 38 [NHA_GATEWAY] = { .type = NLA_BINARY }, 39 [NHA_ENCAP_TYPE] = { .type = NLA_U16 }, 40 [NHA_ENCAP] = { .type = NLA_NESTED }, 41 [NHA_FDB] = { .type = NLA_FLAG }, 42 [NHA_RES_GROUP] = { .type = NLA_NESTED }, 43 [NHA_HW_STATS_ENABLE] = NLA_POLICY_MAX(NLA_U32, true), 44 }; 45 46 static const struct nla_policy rtm_nh_policy_get[] = { 47 [NHA_ID] = { .type = NLA_U32 }, 48 [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32, 49 NHA_OP_FLAGS_DUMP_ALL), 50 }; 51 52 static const struct nla_policy rtm_nh_policy_del[] = { 53 [NHA_ID] = { .type = NLA_U32 }, 54 }; 55 56 static const struct nla_policy rtm_nh_policy_dump[] = { 57 [NHA_OIF] = { .type = NLA_U32 }, 58 [NHA_GROUPS] = { .type = NLA_FLAG }, 59 [NHA_MASTER] = { .type = NLA_U32 }, 60 [NHA_FDB] = { .type = NLA_FLAG }, 61 [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32, 62 NHA_OP_FLAGS_DUMP_ALL), 63 }; 64 65 static const struct nla_policy rtm_nh_res_policy_new[] = { 66 [NHA_RES_GROUP_BUCKETS] = { .type = NLA_U16 }, 67 [NHA_RES_GROUP_IDLE_TIMER] = { .type = NLA_U32 }, 68 [NHA_RES_GROUP_UNBALANCED_TIMER] = { .type = NLA_U32 }, 69 }; 70 71 static const struct nla_policy rtm_nh_policy_dump_bucket[] = { 72 [NHA_ID] = { .type = NLA_U32 }, 73 [NHA_OIF] = { .type = NLA_U32 }, 74 [NHA_MASTER] = { .type = NLA_U32 }, 75 [NHA_RES_BUCKET] = { .type = NLA_NESTED }, 76 }; 77 78 static const struct nla_policy rtm_nh_res_bucket_policy_dump[] = { 79 [NHA_RES_BUCKET_NH_ID] = { .type = NLA_U32 }, 80 }; 81 82 static const struct nla_policy rtm_nh_policy_get_bucket[] = { 83 [NHA_ID] = { .type = NLA_U32 }, 84 [NHA_RES_BUCKET] = { .type = NLA_NESTED }, 85 }; 86 87 static const struct nla_policy rtm_nh_res_bucket_policy_get[] = { 88 [NHA_RES_BUCKET_INDEX] = { .type = NLA_U16 }, 89 }; 90 91 static bool nexthop_notifiers_is_empty(struct net *net) 92 { 93 return !net->nexthop.notifier_chain.head; 94 } 95 96 static void 97 __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info, 98 const struct nh_info *nhi) 99 { 100 nh_info->dev = nhi->fib_nhc.nhc_dev; 101 nh_info->gw_family = nhi->fib_nhc.nhc_gw_family; 102 if (nh_info->gw_family == AF_INET) 103 nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4; 104 else if (nh_info->gw_family == AF_INET6) 105 nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6; 106 107 nh_info->id = nhi->nh_parent->id; 108 nh_info->is_reject = nhi->reject_nh; 109 nh_info->is_fdb = nhi->fdb_nh; 110 nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate; 111 } 112 113 static int nh_notifier_single_info_init(struct nh_notifier_info *info, 114 const struct nexthop *nh) 115 { 116 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 117 118 info->type = NH_NOTIFIER_INFO_TYPE_SINGLE; 119 info->nh = kzalloc_obj(*info->nh); 120 if (!info->nh) 121 return -ENOMEM; 122 123 __nh_notifier_single_info_init(info->nh, nhi); 124 125 return 0; 126 } 127 128 static void nh_notifier_single_info_fini(struct nh_notifier_info *info) 129 { 130 kfree(info->nh); 131 } 132 133 static int nh_notifier_mpath_info_init(struct nh_notifier_info *info, 134 struct nh_group *nhg) 135 { 136 u16 num_nh = nhg->num_nh; 137 int i; 138 139 info->type = NH_NOTIFIER_INFO_TYPE_GRP; 140 info->nh_grp = kzalloc_flex(*info->nh_grp, nh_entries, num_nh); 141 if (!info->nh_grp) 142 return -ENOMEM; 143 144 info->nh_grp->num_nh = num_nh; 145 info->nh_grp->is_fdb = nhg->fdb_nh; 146 info->nh_grp->hw_stats = nhg->hw_stats; 147 148 for (i = 0; i < num_nh; i++) { 149 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 150 struct nh_info *nhi; 151 152 nhi = rtnl_dereference(nhge->nh->nh_info); 153 info->nh_grp->nh_entries[i].weight = nhge->weight; 154 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh, 155 nhi); 156 } 157 158 return 0; 159 } 160 161 static int nh_notifier_res_table_info_init(struct nh_notifier_info *info, 162 struct nh_group *nhg) 163 { 164 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table); 165 u16 num_nh_buckets = res_table->num_nh_buckets; 166 unsigned long size; 167 u16 i; 168 169 info->type = NH_NOTIFIER_INFO_TYPE_RES_TABLE; 170 size = struct_size(info->nh_res_table, nhs, num_nh_buckets); 171 info->nh_res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | 172 __GFP_NOWARN); 173 if (!info->nh_res_table) 174 return -ENOMEM; 175 176 info->nh_res_table->num_nh_buckets = num_nh_buckets; 177 info->nh_res_table->hw_stats = nhg->hw_stats; 178 179 for (i = 0; i < num_nh_buckets; i++) { 180 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 181 struct nh_grp_entry *nhge; 182 struct nh_info *nhi; 183 184 nhge = rtnl_dereference(bucket->nh_entry); 185 nhi = rtnl_dereference(nhge->nh->nh_info); 186 __nh_notifier_single_info_init(&info->nh_res_table->nhs[i], 187 nhi); 188 } 189 190 return 0; 191 } 192 193 static int nh_notifier_grp_info_init(struct nh_notifier_info *info, 194 const struct nexthop *nh) 195 { 196 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 197 198 if (nhg->hash_threshold) 199 return nh_notifier_mpath_info_init(info, nhg); 200 else if (nhg->resilient) 201 return nh_notifier_res_table_info_init(info, nhg); 202 return -EINVAL; 203 } 204 205 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info, 206 const struct nexthop *nh) 207 { 208 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 209 210 if (nhg->hash_threshold) 211 kfree(info->nh_grp); 212 else if (nhg->resilient) 213 vfree(info->nh_res_table); 214 } 215 216 static int nh_notifier_info_init(struct nh_notifier_info *info, 217 const struct nexthop *nh) 218 { 219 info->id = nh->id; 220 221 if (nh->is_group) 222 return nh_notifier_grp_info_init(info, nh); 223 else 224 return nh_notifier_single_info_init(info, nh); 225 } 226 227 static void nh_notifier_info_fini(struct nh_notifier_info *info, 228 const struct nexthop *nh) 229 { 230 if (nh->is_group) 231 nh_notifier_grp_info_fini(info, nh); 232 else 233 nh_notifier_single_info_fini(info); 234 } 235 236 static int call_nexthop_notifiers(struct net *net, 237 enum nexthop_event_type event_type, 238 struct nexthop *nh, 239 struct netlink_ext_ack *extack) 240 { 241 struct nh_notifier_info info = { 242 .net = net, 243 .extack = extack, 244 }; 245 int err; 246 247 ASSERT_RTNL(); 248 249 if (nexthop_notifiers_is_empty(net)) 250 return 0; 251 252 err = nh_notifier_info_init(&info, nh); 253 if (err) { 254 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 255 return err; 256 } 257 258 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 259 event_type, &info); 260 nh_notifier_info_fini(&info, nh); 261 262 return notifier_to_errno(err); 263 } 264 265 static int 266 nh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info *info, 267 bool force, unsigned int *p_idle_timer_ms) 268 { 269 struct nh_res_table *res_table; 270 struct nh_group *nhg; 271 struct nexthop *nh; 272 int err = 0; 273 274 /* When 'force' is false, nexthop bucket replacement is performed 275 * because the bucket was deemed to be idle. In this case, capable 276 * listeners can choose to perform an atomic replacement: The bucket is 277 * only replaced if it is inactive. However, if the idle timer interval 278 * is smaller than the interval in which a listener is querying 279 * buckets' activity from the device, then atomic replacement should 280 * not be tried. Pass the idle timer value to listeners, so that they 281 * could determine which type of replacement to perform. 282 */ 283 if (force) { 284 *p_idle_timer_ms = 0; 285 return 0; 286 } 287 288 rcu_read_lock(); 289 290 nh = nexthop_find_by_id(info->net, info->id); 291 if (!nh) { 292 err = -EINVAL; 293 goto out; 294 } 295 296 nhg = rcu_dereference(nh->nh_grp); 297 res_table = rcu_dereference(nhg->res_table); 298 *p_idle_timer_ms = jiffies_to_msecs(res_table->idle_timer); 299 300 out: 301 rcu_read_unlock(); 302 303 return err; 304 } 305 306 static int nh_notifier_res_bucket_info_init(struct nh_notifier_info *info, 307 u16 bucket_index, bool force, 308 struct nh_info *oldi, 309 struct nh_info *newi) 310 { 311 unsigned int idle_timer_ms; 312 int err; 313 314 err = nh_notifier_res_bucket_idle_timer_get(info, force, 315 &idle_timer_ms); 316 if (err) 317 return err; 318 319 info->type = NH_NOTIFIER_INFO_TYPE_RES_BUCKET; 320 info->nh_res_bucket = kzalloc_obj(*info->nh_res_bucket); 321 if (!info->nh_res_bucket) 322 return -ENOMEM; 323 324 info->nh_res_bucket->bucket_index = bucket_index; 325 info->nh_res_bucket->idle_timer_ms = idle_timer_ms; 326 info->nh_res_bucket->force = force; 327 __nh_notifier_single_info_init(&info->nh_res_bucket->old_nh, oldi); 328 __nh_notifier_single_info_init(&info->nh_res_bucket->new_nh, newi); 329 return 0; 330 } 331 332 static void nh_notifier_res_bucket_info_fini(struct nh_notifier_info *info) 333 { 334 kfree(info->nh_res_bucket); 335 } 336 337 static int __call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id, 338 u16 bucket_index, bool force, 339 struct nh_info *oldi, 340 struct nh_info *newi, 341 struct netlink_ext_ack *extack) 342 { 343 struct nh_notifier_info info = { 344 .net = net, 345 .extack = extack, 346 .id = nhg_id, 347 }; 348 int err; 349 350 if (nexthop_notifiers_is_empty(net)) 351 return 0; 352 353 err = nh_notifier_res_bucket_info_init(&info, bucket_index, force, 354 oldi, newi); 355 if (err) 356 return err; 357 358 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 359 NEXTHOP_EVENT_BUCKET_REPLACE, &info); 360 nh_notifier_res_bucket_info_fini(&info); 361 362 return notifier_to_errno(err); 363 } 364 365 /* There are three users of RES_TABLE, and NHs etc. referenced from there: 366 * 367 * 1) a collection of callbacks for NH maintenance. This operates under 368 * RTNL, 369 * 2) the delayed work that gradually balances the resilient table, 370 * 3) and nexthop_select_path(), operating under RCU. 371 * 372 * Both the delayed work and the RTNL block are writers, and need to 373 * maintain mutual exclusion. Since there are only two and well-known 374 * writers for each table, the RTNL code can make sure it has exclusive 375 * access thus: 376 * 377 * - Have the DW operate without locking; 378 * - synchronously cancel the DW; 379 * - do the writing; 380 * - if the write was not actually a delete, call upkeep, which schedules 381 * DW again if necessary. 382 * 383 * The functions that are always called from the RTNL context use 384 * rtnl_dereference(). The functions that can also be called from the DW do 385 * a raw dereference and rely on the above mutual exclusion scheme. 386 */ 387 #define nh_res_dereference(p) (rcu_dereference_raw(p)) 388 389 static int call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id, 390 u16 bucket_index, bool force, 391 struct nexthop *old_nh, 392 struct nexthop *new_nh, 393 struct netlink_ext_ack *extack) 394 { 395 struct nh_info *oldi = nh_res_dereference(old_nh->nh_info); 396 struct nh_info *newi = nh_res_dereference(new_nh->nh_info); 397 398 return __call_nexthop_res_bucket_notifiers(net, nhg_id, bucket_index, 399 force, oldi, newi, extack); 400 } 401 402 static int call_nexthop_res_table_notifiers(struct net *net, struct nexthop *nh, 403 struct netlink_ext_ack *extack) 404 { 405 struct nh_notifier_info info = { 406 .net = net, 407 .extack = extack, 408 .id = nh->id, 409 }; 410 struct nh_group *nhg; 411 int err; 412 413 ASSERT_RTNL(); 414 415 if (nexthop_notifiers_is_empty(net)) 416 return 0; 417 418 /* At this point, the nexthop buckets are still not populated. Only 419 * emit a notification with the logical nexthops, so that a listener 420 * could potentially veto it in case of unsupported configuration. 421 */ 422 nhg = rtnl_dereference(nh->nh_grp); 423 err = nh_notifier_mpath_info_init(&info, nhg); 424 if (err) { 425 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 426 return err; 427 } 428 429 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 430 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE, 431 &info); 432 kfree(info.nh_grp); 433 434 return notifier_to_errno(err); 435 } 436 437 static int call_nexthop_notifier(struct notifier_block *nb, struct net *net, 438 enum nexthop_event_type event_type, 439 struct nexthop *nh, 440 struct netlink_ext_ack *extack) 441 { 442 struct nh_notifier_info info = { 443 .net = net, 444 .extack = extack, 445 }; 446 int err; 447 448 err = nh_notifier_info_init(&info, nh); 449 if (err) 450 return err; 451 452 err = nb->notifier_call(nb, event_type, &info); 453 nh_notifier_info_fini(&info, nh); 454 455 return notifier_to_errno(err); 456 } 457 458 static unsigned int nh_dev_hashfn(unsigned int val) 459 { 460 unsigned int mask = NH_DEV_HASHSIZE - 1; 461 462 return (val ^ 463 (val >> NH_DEV_HASHBITS) ^ 464 (val >> (NH_DEV_HASHBITS * 2))) & mask; 465 } 466 467 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi) 468 { 469 struct net_device *dev = nhi->fib_nhc.nhc_dev; 470 struct hlist_head *head; 471 unsigned int hash; 472 473 WARN_ON(!dev); 474 475 hash = nh_dev_hashfn(dev->ifindex); 476 head = &net->nexthop.devhash[hash]; 477 hlist_add_head(&nhi->dev_hash, head); 478 } 479 480 static void nexthop_free_group(struct nexthop *nh) 481 { 482 struct nh_group *nhg; 483 int i; 484 485 nhg = rcu_dereference_raw(nh->nh_grp); 486 for (i = 0; i < nhg->num_nh; ++i) { 487 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 488 489 WARN_ON(!list_empty(&nhge->nh_list)); 490 free_percpu(nhge->stats); 491 nexthop_put(nhge->nh); 492 } 493 494 WARN_ON(nhg->spare == nhg); 495 496 if (nhg->resilient) 497 vfree(rcu_dereference_raw(nhg->res_table)); 498 499 kfree(nhg->spare); 500 kfree(nhg); 501 } 502 503 static void nexthop_free_single(struct nexthop *nh) 504 { 505 struct nh_info *nhi; 506 507 nhi = rcu_dereference_raw(nh->nh_info); 508 switch (nhi->family) { 509 case AF_INET: 510 fib_nh_release(nh->net, &nhi->fib_nh); 511 break; 512 case AF_INET6: 513 fib6_nh_release(&nhi->fib6_nh); 514 break; 515 } 516 kfree(nhi); 517 } 518 519 void nexthop_free_rcu(struct rcu_head *head) 520 { 521 struct nexthop *nh = container_of(head, struct nexthop, rcu); 522 523 if (nh->is_group) 524 nexthop_free_group(nh); 525 else 526 nexthop_free_single(nh); 527 528 kfree(nh); 529 } 530 EXPORT_SYMBOL_GPL(nexthop_free_rcu); 531 532 static struct nexthop *nexthop_alloc(void) 533 { 534 struct nexthop *nh; 535 536 nh = kzalloc_obj(struct nexthop); 537 if (nh) { 538 INIT_LIST_HEAD(&nh->fi_list); 539 INIT_LIST_HEAD(&nh->f6i_list); 540 INIT_LIST_HEAD(&nh->grp_list); 541 INIT_LIST_HEAD(&nh->fdb_list); 542 spin_lock_init(&nh->lock); 543 } 544 return nh; 545 } 546 547 static struct nh_group *nexthop_grp_alloc(u16 num_nh) 548 { 549 struct nh_group *nhg; 550 551 nhg = kzalloc_flex(*nhg, nh_entries, num_nh); 552 if (nhg) 553 nhg->num_nh = num_nh; 554 555 return nhg; 556 } 557 558 static void nh_res_table_upkeep_dw(struct work_struct *work); 559 560 static struct nh_res_table * 561 nexthop_res_table_alloc(struct net *net, u32 nhg_id, struct nh_config *cfg) 562 { 563 const u16 num_nh_buckets = cfg->nh_grp_res_num_buckets; 564 struct nh_res_table *res_table; 565 unsigned long size; 566 567 size = struct_size(res_table, nh_buckets, num_nh_buckets); 568 res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN); 569 if (!res_table) 570 return NULL; 571 572 res_table->net = net; 573 res_table->nhg_id = nhg_id; 574 INIT_DELAYED_WORK(&res_table->upkeep_dw, &nh_res_table_upkeep_dw); 575 INIT_LIST_HEAD(&res_table->uw_nh_entries); 576 res_table->idle_timer = cfg->nh_grp_res_idle_timer; 577 res_table->unbalanced_timer = cfg->nh_grp_res_unbalanced_timer; 578 res_table->num_nh_buckets = num_nh_buckets; 579 return res_table; 580 } 581 582 static void nh_base_seq_inc(struct net *net) 583 { 584 while (++net->nexthop.seq == 0) 585 ; 586 } 587 588 /* no reference taken; rcu lock or rtnl must be held */ 589 struct nexthop *nexthop_find_by_id(struct net *net, u32 id) 590 { 591 struct rb_node **pp, *parent = NULL, *next; 592 593 pp = &net->nexthop.rb_root.rb_node; 594 while (1) { 595 struct nexthop *nh; 596 597 next = rcu_dereference_raw(*pp); 598 if (!next) 599 break; 600 parent = next; 601 602 nh = rb_entry(parent, struct nexthop, rb_node); 603 if (id < nh->id) 604 pp = &next->rb_left; 605 else if (id > nh->id) 606 pp = &next->rb_right; 607 else 608 return nh; 609 } 610 return NULL; 611 } 612 EXPORT_SYMBOL_GPL(nexthop_find_by_id); 613 614 /* used for auto id allocation; called with rtnl held */ 615 static u32 nh_find_unused_id(struct net *net) 616 { 617 u32 id_start = net->nexthop.last_id_allocated; 618 619 while (1) { 620 net->nexthop.last_id_allocated++; 621 if (net->nexthop.last_id_allocated == id_start) 622 break; 623 624 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated)) 625 return net->nexthop.last_id_allocated; 626 } 627 return 0; 628 } 629 630 static void nh_res_time_set_deadline(unsigned long next_time, 631 unsigned long *deadline) 632 { 633 if (time_before(next_time, *deadline)) 634 *deadline = next_time; 635 } 636 637 static clock_t nh_res_table_unbalanced_time(struct nh_res_table *res_table) 638 { 639 if (list_empty(&res_table->uw_nh_entries)) 640 return 0; 641 return jiffies_delta_to_clock_t(jiffies - res_table->unbalanced_since); 642 } 643 644 static int nla_put_nh_group_res(struct sk_buff *skb, struct nh_group *nhg) 645 { 646 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table); 647 struct nlattr *nest; 648 649 nest = nla_nest_start(skb, NHA_RES_GROUP); 650 if (!nest) 651 return -EMSGSIZE; 652 653 if (nla_put_u16(skb, NHA_RES_GROUP_BUCKETS, 654 res_table->num_nh_buckets) || 655 nla_put_u32(skb, NHA_RES_GROUP_IDLE_TIMER, 656 jiffies_to_clock_t(res_table->idle_timer)) || 657 nla_put_u32(skb, NHA_RES_GROUP_UNBALANCED_TIMER, 658 jiffies_to_clock_t(res_table->unbalanced_timer)) || 659 nla_put_u64_64bit(skb, NHA_RES_GROUP_UNBALANCED_TIME, 660 nh_res_table_unbalanced_time(res_table), 661 NHA_RES_GROUP_PAD)) 662 goto nla_put_failure; 663 664 nla_nest_end(skb, nest); 665 return 0; 666 667 nla_put_failure: 668 nla_nest_cancel(skb, nest); 669 return -EMSGSIZE; 670 } 671 672 static void nh_grp_entry_stats_inc(struct nh_grp_entry *nhge) 673 { 674 struct nh_grp_entry_stats *cpu_stats; 675 676 cpu_stats = get_cpu_ptr(nhge->stats); 677 u64_stats_update_begin(&cpu_stats->syncp); 678 u64_stats_inc(&cpu_stats->packets); 679 u64_stats_update_end(&cpu_stats->syncp); 680 put_cpu_ptr(cpu_stats); 681 } 682 683 static void nh_grp_entry_stats_read(struct nh_grp_entry *nhge, 684 u64 *ret_packets) 685 { 686 int i; 687 688 *ret_packets = 0; 689 690 for_each_possible_cpu(i) { 691 struct nh_grp_entry_stats *cpu_stats; 692 unsigned int start; 693 u64 packets; 694 695 cpu_stats = per_cpu_ptr(nhge->stats, i); 696 do { 697 start = u64_stats_fetch_begin(&cpu_stats->syncp); 698 packets = u64_stats_read(&cpu_stats->packets); 699 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start)); 700 701 *ret_packets += packets; 702 } 703 } 704 705 static int nh_notifier_grp_hw_stats_init(struct nh_notifier_info *info, 706 const struct nexthop *nh) 707 { 708 struct nh_group *nhg; 709 int i; 710 711 ASSERT_RTNL(); 712 nhg = rtnl_dereference(nh->nh_grp); 713 714 info->id = nh->id; 715 info->type = NH_NOTIFIER_INFO_TYPE_GRP_HW_STATS; 716 info->nh_grp_hw_stats = kzalloc_flex(*info->nh_grp_hw_stats, stats, 717 nhg->num_nh); 718 if (!info->nh_grp_hw_stats) 719 return -ENOMEM; 720 721 info->nh_grp_hw_stats->num_nh = nhg->num_nh; 722 for (i = 0; i < nhg->num_nh; i++) { 723 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 724 725 info->nh_grp_hw_stats->stats[i].id = nhge->nh->id; 726 } 727 728 return 0; 729 } 730 731 static void nh_notifier_grp_hw_stats_fini(struct nh_notifier_info *info) 732 { 733 kfree(info->nh_grp_hw_stats); 734 } 735 736 void nh_grp_hw_stats_report_delta(struct nh_notifier_grp_hw_stats_info *info, 737 unsigned int nh_idx, 738 u64 delta_packets) 739 { 740 info->hw_stats_used = true; 741 info->stats[nh_idx].packets += delta_packets; 742 } 743 EXPORT_SYMBOL(nh_grp_hw_stats_report_delta); 744 745 static void nh_grp_hw_stats_apply_update(struct nexthop *nh, 746 struct nh_notifier_info *info) 747 { 748 struct nh_group *nhg; 749 int i; 750 751 ASSERT_RTNL(); 752 nhg = rtnl_dereference(nh->nh_grp); 753 754 for (i = 0; i < nhg->num_nh; i++) { 755 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 756 757 nhge->packets_hw += info->nh_grp_hw_stats->stats[i].packets; 758 } 759 } 760 761 static int nh_grp_hw_stats_update(struct nexthop *nh, bool *hw_stats_used) 762 { 763 struct nh_notifier_info info = { 764 .net = nh->net, 765 }; 766 struct net *net = nh->net; 767 int err; 768 769 if (nexthop_notifiers_is_empty(net)) { 770 *hw_stats_used = false; 771 return 0; 772 } 773 774 err = nh_notifier_grp_hw_stats_init(&info, nh); 775 if (err) 776 return err; 777 778 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 779 NEXTHOP_EVENT_HW_STATS_REPORT_DELTA, 780 &info); 781 782 /* Cache whatever we got, even if there was an error, otherwise the 783 * successful stats retrievals would get lost. 784 */ 785 nh_grp_hw_stats_apply_update(nh, &info); 786 *hw_stats_used = info.nh_grp_hw_stats->hw_stats_used; 787 788 nh_notifier_grp_hw_stats_fini(&info); 789 return notifier_to_errno(err); 790 } 791 792 static int nla_put_nh_group_stats_entry(struct sk_buff *skb, 793 struct nh_grp_entry *nhge, 794 u32 op_flags) 795 { 796 struct nlattr *nest; 797 u64 packets; 798 799 nh_grp_entry_stats_read(nhge, &packets); 800 801 nest = nla_nest_start(skb, NHA_GROUP_STATS_ENTRY); 802 if (!nest) 803 return -EMSGSIZE; 804 805 if (nla_put_u32(skb, NHA_GROUP_STATS_ENTRY_ID, nhge->nh->id) || 806 nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS, 807 packets + nhge->packets_hw)) 808 goto nla_put_failure; 809 810 if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS && 811 nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS_HW, 812 nhge->packets_hw)) 813 goto nla_put_failure; 814 815 nla_nest_end(skb, nest); 816 return 0; 817 818 nla_put_failure: 819 nla_nest_cancel(skb, nest); 820 return -EMSGSIZE; 821 } 822 823 static int nla_put_nh_group_stats(struct sk_buff *skb, struct nexthop *nh, 824 u32 op_flags) 825 { 826 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 827 struct nlattr *nest; 828 bool hw_stats_used; 829 int err; 830 int i; 831 832 if (nla_put_u32(skb, NHA_HW_STATS_ENABLE, nhg->hw_stats)) 833 goto err_out; 834 835 if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS && 836 nhg->hw_stats) { 837 err = nh_grp_hw_stats_update(nh, &hw_stats_used); 838 if (err) 839 goto out; 840 841 if (nla_put_u32(skb, NHA_HW_STATS_USED, hw_stats_used)) 842 goto err_out; 843 } 844 845 nest = nla_nest_start(skb, NHA_GROUP_STATS); 846 if (!nest) 847 goto err_out; 848 849 for (i = 0; i < nhg->num_nh; i++) 850 if (nla_put_nh_group_stats_entry(skb, &nhg->nh_entries[i], 851 op_flags)) 852 goto cancel_out; 853 854 nla_nest_end(skb, nest); 855 return 0; 856 857 cancel_out: 858 nla_nest_cancel(skb, nest); 859 err_out: 860 err = -EMSGSIZE; 861 out: 862 return err; 863 } 864 865 static int nla_put_nh_group(struct sk_buff *skb, struct nexthop *nh, 866 u32 op_flags, u32 *resp_op_flags) 867 { 868 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 869 struct nexthop_grp *p; 870 size_t len = nhg->num_nh * sizeof(*p); 871 struct nlattr *nla; 872 u16 group_type = 0; 873 u16 weight; 874 int i; 875 876 *resp_op_flags |= NHA_OP_FLAG_RESP_GRP_RESVD_0; 877 878 if (nhg->hash_threshold) 879 group_type = NEXTHOP_GRP_TYPE_MPATH; 880 else if (nhg->resilient) 881 group_type = NEXTHOP_GRP_TYPE_RES; 882 883 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type)) 884 goto nla_put_failure; 885 886 nla = nla_reserve(skb, NHA_GROUP, len); 887 if (!nla) 888 goto nla_put_failure; 889 890 p = nla_data(nla); 891 for (i = 0; i < nhg->num_nh; ++i) { 892 weight = nhg->nh_entries[i].weight - 1; 893 894 *p++ = (struct nexthop_grp) { 895 .id = nhg->nh_entries[i].nh->id, 896 .weight = weight, 897 .weight_high = weight >> 8, 898 }; 899 } 900 901 if (nhg->resilient && nla_put_nh_group_res(skb, nhg)) 902 goto nla_put_failure; 903 904 if (op_flags & NHA_OP_FLAG_DUMP_STATS && 905 nla_put_nh_group_stats(skb, nh, op_flags)) 906 goto nla_put_failure; 907 908 return 0; 909 910 nla_put_failure: 911 return -EMSGSIZE; 912 } 913 914 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh, 915 int event, u32 portid, u32 seq, unsigned int nlflags, 916 u32 op_flags) 917 { 918 struct fib6_nh *fib6_nh; 919 struct fib_nh *fib_nh; 920 struct nlmsghdr *nlh; 921 struct nh_info *nhi; 922 struct nhmsg *nhm; 923 924 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 925 if (!nlh) 926 return -EMSGSIZE; 927 928 nhm = nlmsg_data(nlh); 929 nhm->nh_family = AF_UNSPEC; 930 nhm->nh_flags = nh->nh_flags; 931 nhm->nh_protocol = nh->protocol; 932 nhm->nh_scope = 0; 933 nhm->resvd = 0; 934 935 if (nla_put_u32(skb, NHA_ID, nh->id)) 936 goto nla_put_failure; 937 938 if (nh->is_group) { 939 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 940 u32 resp_op_flags = 0; 941 942 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB)) 943 goto nla_put_failure; 944 if (nla_put_nh_group(skb, nh, op_flags, &resp_op_flags) || 945 nla_put_u32(skb, NHA_OP_FLAGS, resp_op_flags)) 946 goto nla_put_failure; 947 goto out; 948 } 949 950 nhi = rtnl_dereference(nh->nh_info); 951 nhm->nh_family = nhi->family; 952 if (nhi->reject_nh) { 953 if (nla_put_flag(skb, NHA_BLACKHOLE)) 954 goto nla_put_failure; 955 goto out; 956 } else if (nhi->fdb_nh) { 957 if (nla_put_flag(skb, NHA_FDB)) 958 goto nla_put_failure; 959 } else { 960 const struct net_device *dev; 961 962 dev = nhi->fib_nhc.nhc_dev; 963 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex)) 964 goto nla_put_failure; 965 } 966 967 nhm->nh_scope = nhi->fib_nhc.nhc_scope; 968 switch (nhi->family) { 969 case AF_INET: 970 fib_nh = &nhi->fib_nh; 971 if (fib_nh->fib_nh_gw_family && 972 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4)) 973 goto nla_put_failure; 974 break; 975 976 case AF_INET6: 977 fib6_nh = &nhi->fib6_nh; 978 if (fib6_nh->fib_nh_gw_family && 979 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6)) 980 goto nla_put_failure; 981 break; 982 } 983 984 if (lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate, 985 NHA_ENCAP, NHA_ENCAP_TYPE) < 0) 986 goto nla_put_failure; 987 988 out: 989 nlmsg_end(skb, nlh); 990 return 0; 991 992 nla_put_failure: 993 nlmsg_cancel(skb, nlh); 994 return -EMSGSIZE; 995 } 996 997 static size_t nh_nlmsg_size_grp_res(struct nh_group *nhg) 998 { 999 return nla_total_size(0) + /* NHA_RES_GROUP */ 1000 nla_total_size(2) + /* NHA_RES_GROUP_BUCKETS */ 1001 nla_total_size(4) + /* NHA_RES_GROUP_IDLE_TIMER */ 1002 nla_total_size(4) + /* NHA_RES_GROUP_UNBALANCED_TIMER */ 1003 nla_total_size_64bit(8);/* NHA_RES_GROUP_UNBALANCED_TIME */ 1004 } 1005 1006 static size_t nh_nlmsg_size_grp(struct nexthop *nh, u32 op_flags) 1007 { 1008 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 1009 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh; 1010 size_t tot = nla_total_size(sz) + 1011 nla_total_size(2) + /* NHA_GROUP_TYPE */ 1012 nla_total_size(0); /* NHA_FDB */ 1013 1014 if (nhg->resilient) 1015 tot += nh_nlmsg_size_grp_res(nhg); 1016 1017 if (op_flags & NHA_OP_FLAG_DUMP_STATS) { 1018 tot += nla_total_size(0) + /* NHA_GROUP_STATS */ 1019 nla_total_size(4); /* NHA_HW_STATS_ENABLE */ 1020 tot += nhg->num_nh * 1021 (nla_total_size(0) + /* NHA_GROUP_STATS_ENTRY */ 1022 nla_total_size(4) + /* NHA_GROUP_STATS_ENTRY_ID */ 1023 nla_total_size_64bit(8)); /* NHA_GROUP_STATS_ENTRY_PACKETS */ 1024 1025 if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS) { 1026 tot += nhg->num_nh * 1027 nla_total_size_64bit(8); /* NHA_GROUP_STATS_ENTRY_PACKETS_HW */ 1028 tot += nla_total_size(4); /* NHA_HW_STATS_USED */ 1029 } 1030 } 1031 1032 return tot; 1033 } 1034 1035 static size_t nh_nlmsg_size_single(struct nexthop *nh) 1036 { 1037 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 1038 size_t sz; 1039 1040 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE 1041 * are mutually exclusive 1042 */ 1043 sz = nla_total_size(4); /* NHA_OIF */ 1044 1045 switch (nhi->family) { 1046 case AF_INET: 1047 if (nhi->fib_nh.fib_nh_gw_family) 1048 sz += nla_total_size(4); /* NHA_GATEWAY */ 1049 break; 1050 1051 case AF_INET6: 1052 /* NHA_GATEWAY */ 1053 if (nhi->fib6_nh.fib_nh_gw_family) 1054 sz += nla_total_size(sizeof(const struct in6_addr)); 1055 break; 1056 } 1057 1058 if (nhi->fib_nhc.nhc_lwtstate) { 1059 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate); 1060 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */ 1061 } 1062 1063 return sz; 1064 } 1065 1066 static size_t nh_nlmsg_size(struct nexthop *nh, u32 op_flags) 1067 { 1068 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg)); 1069 1070 sz += nla_total_size(4); /* NHA_ID */ 1071 1072 if (nh->is_group) 1073 sz += nh_nlmsg_size_grp(nh, op_flags) + 1074 nla_total_size(4) + /* NHA_OP_FLAGS */ 1075 0; 1076 else 1077 sz += nh_nlmsg_size_single(nh); 1078 1079 return sz; 1080 } 1081 1082 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info) 1083 { 1084 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0; 1085 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 1086 struct sk_buff *skb; 1087 int err = -ENOBUFS; 1088 1089 skb = nlmsg_new(nh_nlmsg_size(nh, 0), gfp_any()); 1090 if (!skb) 1091 goto errout; 1092 1093 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags, 0); 1094 if (err < 0) { 1095 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */ 1096 WARN_ON(err == -EMSGSIZE); 1097 kfree_skb(skb); 1098 goto errout; 1099 } 1100 1101 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP, 1102 info->nlh, gfp_any()); 1103 return; 1104 errout: 1105 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err); 1106 } 1107 1108 static unsigned long nh_res_bucket_used_time(const struct nh_res_bucket *bucket) 1109 { 1110 return (unsigned long)atomic_long_read(&bucket->used_time); 1111 } 1112 1113 static unsigned long 1114 nh_res_bucket_idle_point(const struct nh_res_table *res_table, 1115 const struct nh_res_bucket *bucket, 1116 unsigned long now) 1117 { 1118 unsigned long time = nh_res_bucket_used_time(bucket); 1119 1120 /* Bucket was not used since it was migrated. The idle time is now. */ 1121 if (time == bucket->migrated_time) 1122 return now; 1123 1124 return time + res_table->idle_timer; 1125 } 1126 1127 static unsigned long 1128 nh_res_table_unb_point(const struct nh_res_table *res_table) 1129 { 1130 return res_table->unbalanced_since + res_table->unbalanced_timer; 1131 } 1132 1133 static void nh_res_bucket_set_idle(const struct nh_res_table *res_table, 1134 struct nh_res_bucket *bucket) 1135 { 1136 unsigned long now = jiffies; 1137 1138 atomic_long_set(&bucket->used_time, (long)now); 1139 bucket->migrated_time = now; 1140 } 1141 1142 static void nh_res_bucket_set_busy(struct nh_res_bucket *bucket) 1143 { 1144 atomic_long_set(&bucket->used_time, (long)jiffies); 1145 } 1146 1147 static clock_t nh_res_bucket_idle_time(const struct nh_res_bucket *bucket) 1148 { 1149 unsigned long used_time = nh_res_bucket_used_time(bucket); 1150 1151 return jiffies_delta_to_clock_t(jiffies - used_time); 1152 } 1153 1154 static int nh_fill_res_bucket(struct sk_buff *skb, struct nexthop *nh, 1155 struct nh_res_bucket *bucket, u16 bucket_index, 1156 int event, u32 portid, u32 seq, 1157 unsigned int nlflags, 1158 struct netlink_ext_ack *extack) 1159 { 1160 struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry); 1161 struct nlmsghdr *nlh; 1162 struct nlattr *nest; 1163 struct nhmsg *nhm; 1164 1165 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 1166 if (!nlh) 1167 return -EMSGSIZE; 1168 1169 nhm = nlmsg_data(nlh); 1170 nhm->nh_family = AF_UNSPEC; 1171 nhm->nh_flags = bucket->nh_flags; 1172 nhm->nh_protocol = nh->protocol; 1173 nhm->nh_scope = 0; 1174 nhm->resvd = 0; 1175 1176 if (nla_put_u32(skb, NHA_ID, nh->id)) 1177 goto nla_put_failure; 1178 1179 nest = nla_nest_start(skb, NHA_RES_BUCKET); 1180 if (!nest) 1181 goto nla_put_failure; 1182 1183 if (nla_put_u16(skb, NHA_RES_BUCKET_INDEX, bucket_index) || 1184 nla_put_u32(skb, NHA_RES_BUCKET_NH_ID, nhge->nh->id) || 1185 nla_put_u64_64bit(skb, NHA_RES_BUCKET_IDLE_TIME, 1186 nh_res_bucket_idle_time(bucket), 1187 NHA_RES_BUCKET_PAD)) 1188 goto nla_put_failure_nest; 1189 1190 nla_nest_end(skb, nest); 1191 nlmsg_end(skb, nlh); 1192 return 0; 1193 1194 nla_put_failure_nest: 1195 nla_nest_cancel(skb, nest); 1196 nla_put_failure: 1197 nlmsg_cancel(skb, nlh); 1198 return -EMSGSIZE; 1199 } 1200 1201 static void nexthop_bucket_notify(struct nh_res_table *res_table, 1202 u16 bucket_index) 1203 { 1204 struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index]; 1205 struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry); 1206 struct nexthop *nh = nhge->nh_parent; 1207 struct sk_buff *skb; 1208 int err = -ENOBUFS; 1209 1210 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1211 if (!skb) 1212 goto errout; 1213 1214 err = nh_fill_res_bucket(skb, nh, bucket, bucket_index, 1215 RTM_NEWNEXTHOPBUCKET, 0, 0, NLM_F_REPLACE, 1216 NULL); 1217 if (err < 0) { 1218 kfree_skb(skb); 1219 goto errout; 1220 } 1221 1222 rtnl_notify(skb, nh->net, 0, RTNLGRP_NEXTHOP, NULL, GFP_KERNEL); 1223 return; 1224 errout: 1225 rtnl_set_sk_err(nh->net, RTNLGRP_NEXTHOP, err); 1226 } 1227 1228 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths, 1229 bool *is_fdb, struct netlink_ext_ack *extack) 1230 { 1231 if (nh->is_group) { 1232 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 1233 1234 /* Nesting groups within groups is not supported. */ 1235 if (nhg->hash_threshold) { 1236 NL_SET_ERR_MSG(extack, 1237 "Hash-threshold group can not be a nexthop within a group"); 1238 return false; 1239 } 1240 if (nhg->resilient) { 1241 NL_SET_ERR_MSG(extack, 1242 "Resilient group can not be a nexthop within a group"); 1243 return false; 1244 } 1245 *is_fdb = nhg->fdb_nh; 1246 } else { 1247 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 1248 1249 if (nhi->reject_nh && npaths > 1) { 1250 NL_SET_ERR_MSG(extack, 1251 "Blackhole nexthop can not be used in a group with more than 1 path"); 1252 return false; 1253 } 1254 *is_fdb = nhi->fdb_nh; 1255 } 1256 1257 return true; 1258 } 1259 1260 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family, 1261 struct netlink_ext_ack *extack) 1262 { 1263 struct nh_info *nhi; 1264 1265 nhi = rtnl_dereference(nh->nh_info); 1266 1267 if (!nhi->fdb_nh) { 1268 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops"); 1269 return -EINVAL; 1270 } 1271 1272 if (*nh_family == AF_UNSPEC) { 1273 *nh_family = nhi->family; 1274 } else if (*nh_family != nhi->family) { 1275 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops"); 1276 return -EINVAL; 1277 } 1278 1279 return 0; 1280 } 1281 1282 static int nh_check_attr_group(struct net *net, 1283 struct nlattr *tb[], size_t tb_size, 1284 u16 nh_grp_type, struct netlink_ext_ack *extack) 1285 { 1286 unsigned int len = nla_len(tb[NHA_GROUP]); 1287 struct nexthop_grp *nhg; 1288 unsigned int i, j; 1289 1290 if (!len || len & (sizeof(struct nexthop_grp) - 1)) { 1291 NL_SET_ERR_MSG(extack, 1292 "Invalid length for nexthop group attribute"); 1293 return -EINVAL; 1294 } 1295 1296 /* convert len to number of nexthop ids */ 1297 len /= sizeof(*nhg); 1298 1299 nhg = nla_data(tb[NHA_GROUP]); 1300 for (i = 0; i < len; ++i) { 1301 if (nhg[i].resvd2) { 1302 NL_SET_ERR_MSG(extack, "Reserved field in nexthop_grp must be 0"); 1303 return -EINVAL; 1304 } 1305 if (nexthop_grp_weight(&nhg[i]) == 0) { 1306 /* 0xffff got passed in, representing weight of 0x10000, 1307 * which is too heavy. 1308 */ 1309 NL_SET_ERR_MSG(extack, "Invalid value for weight"); 1310 return -EINVAL; 1311 } 1312 for (j = i + 1; j < len; ++j) { 1313 if (nhg[i].id == nhg[j].id) { 1314 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group"); 1315 return -EINVAL; 1316 } 1317 } 1318 } 1319 1320 nhg = nla_data(tb[NHA_GROUP]); 1321 for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) { 1322 if (!tb[i]) 1323 continue; 1324 switch (i) { 1325 case NHA_HW_STATS_ENABLE: 1326 case NHA_FDB: 1327 continue; 1328 case NHA_RES_GROUP: 1329 if (nh_grp_type == NEXTHOP_GRP_TYPE_RES) 1330 continue; 1331 break; 1332 } 1333 NL_SET_ERR_MSG(extack, 1334 "No other attributes can be set in nexthop groups"); 1335 return -EINVAL; 1336 } 1337 1338 return 0; 1339 } 1340 1341 static int nh_check_attr_group_rtnl(struct net *net, struct nlattr *tb[], 1342 struct netlink_ext_ack *extack) 1343 { 1344 u8 nh_family = AF_UNSPEC; 1345 struct nexthop_grp *nhg; 1346 unsigned int len; 1347 unsigned int i; 1348 u8 nhg_fdb; 1349 1350 len = nla_len(tb[NHA_GROUP]) / sizeof(*nhg); 1351 nhg = nla_data(tb[NHA_GROUP]); 1352 nhg_fdb = !!tb[NHA_FDB]; 1353 1354 for (i = 0; i < len; i++) { 1355 struct nexthop *nh; 1356 bool is_fdb_nh; 1357 1358 nh = nexthop_find_by_id(net, nhg[i].id); 1359 if (!nh) { 1360 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 1361 return -EINVAL; 1362 } 1363 if (!valid_group_nh(nh, len, &is_fdb_nh, extack)) 1364 return -EINVAL; 1365 1366 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack)) 1367 return -EINVAL; 1368 1369 if (!nhg_fdb && is_fdb_nh) { 1370 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops"); 1371 return -EINVAL; 1372 } 1373 } 1374 1375 return 0; 1376 } 1377 1378 static bool ipv6_good_nh(const struct fib6_nh *nh) 1379 { 1380 int state = NUD_REACHABLE; 1381 struct neighbour *n; 1382 1383 rcu_read_lock(); 1384 1385 n = __ipv6_neigh_lookup_noref(nh->fib_nh_dev, &nh->fib_nh_gw6); 1386 if (n) 1387 state = READ_ONCE(n->nud_state); 1388 1389 rcu_read_unlock(); 1390 1391 return !!(state & NUD_VALID); 1392 } 1393 1394 static bool ipv4_good_nh(const struct fib_nh *nh) 1395 { 1396 int state = NUD_REACHABLE; 1397 struct neighbour *n; 1398 1399 rcu_read_lock(); 1400 1401 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, 1402 (__force u32)nh->fib_nh_gw4); 1403 if (n) 1404 state = READ_ONCE(n->nud_state); 1405 1406 rcu_read_unlock(); 1407 1408 return !!(state & NUD_VALID); 1409 } 1410 1411 static bool nexthop_is_good_nh(const struct nexthop *nh) 1412 { 1413 struct nh_info *nhi = rcu_dereference(nh->nh_info); 1414 1415 switch (nhi->family) { 1416 case AF_INET: 1417 return ipv4_good_nh(&nhi->fib_nh); 1418 case AF_INET6: 1419 return IS_ENABLED(CONFIG_IPV6) && ipv6_good_nh(&nhi->fib6_nh); 1420 } 1421 1422 return false; 1423 } 1424 1425 static struct nexthop *nexthop_select_path_fdb(struct nh_group *nhg, int hash) 1426 { 1427 int i; 1428 1429 for (i = 0; i < nhg->num_nh; i++) { 1430 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 1431 1432 if (hash > atomic_read(&nhge->hthr.upper_bound)) 1433 continue; 1434 1435 nh_grp_entry_stats_inc(nhge); 1436 return nhge->nh; 1437 } 1438 1439 WARN_ON_ONCE(1); 1440 return NULL; 1441 } 1442 1443 static struct nexthop *nexthop_select_path_hthr(struct nh_group *nhg, int hash) 1444 { 1445 struct nh_grp_entry *nhge0 = NULL; 1446 int i; 1447 1448 if (nhg->fdb_nh) 1449 return nexthop_select_path_fdb(nhg, hash); 1450 1451 for (i = 0; i < nhg->num_nh; ++i) { 1452 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 1453 1454 /* nexthops always check if it is good and does 1455 * not rely on a sysctl for this behavior 1456 */ 1457 if (!nexthop_is_good_nh(nhge->nh)) 1458 continue; 1459 1460 if (!nhge0) 1461 nhge0 = nhge; 1462 1463 if (hash > atomic_read(&nhge->hthr.upper_bound)) 1464 continue; 1465 1466 nh_grp_entry_stats_inc(nhge); 1467 return nhge->nh; 1468 } 1469 1470 if (!nhge0) 1471 nhge0 = &nhg->nh_entries[0]; 1472 nh_grp_entry_stats_inc(nhge0); 1473 return nhge0->nh; 1474 } 1475 1476 static struct nexthop *nexthop_select_path_res(struct nh_group *nhg, int hash) 1477 { 1478 struct nh_res_table *res_table = rcu_dereference(nhg->res_table); 1479 u16 bucket_index = hash % res_table->num_nh_buckets; 1480 struct nh_res_bucket *bucket; 1481 struct nh_grp_entry *nhge; 1482 1483 /* nexthop_select_path() is expected to return a non-NULL value, so 1484 * skip protocol validation and just hand out whatever there is. 1485 */ 1486 bucket = &res_table->nh_buckets[bucket_index]; 1487 nh_res_bucket_set_busy(bucket); 1488 nhge = rcu_dereference(bucket->nh_entry); 1489 nh_grp_entry_stats_inc(nhge); 1490 return nhge->nh; 1491 } 1492 1493 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash) 1494 { 1495 struct nh_group *nhg; 1496 1497 if (!nh->is_group) 1498 return nh; 1499 1500 nhg = rcu_dereference(nh->nh_grp); 1501 if (nhg->hash_threshold) 1502 return nexthop_select_path_hthr(nhg, hash); 1503 else if (nhg->resilient) 1504 return nexthop_select_path_res(nhg, hash); 1505 1506 /* Unreachable. */ 1507 return NULL; 1508 } 1509 EXPORT_SYMBOL_GPL(nexthop_select_path); 1510 1511 int nexthop_for_each_fib6_nh(struct nexthop *nh, 1512 int (*cb)(struct fib6_nh *nh, void *arg), 1513 void *arg) 1514 { 1515 struct nh_info *nhi; 1516 int err; 1517 1518 if (nh->is_group) { 1519 struct nh_group *nhg; 1520 int i; 1521 1522 nhg = rcu_dereference_rtnl(nh->nh_grp); 1523 for (i = 0; i < nhg->num_nh; i++) { 1524 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 1525 1526 nhi = rcu_dereference_rtnl(nhge->nh->nh_info); 1527 err = cb(&nhi->fib6_nh, arg); 1528 if (err) 1529 return err; 1530 } 1531 } else { 1532 nhi = rcu_dereference_rtnl(nh->nh_info); 1533 err = cb(&nhi->fib6_nh, arg); 1534 if (err) 1535 return err; 1536 } 1537 1538 return 0; 1539 } 1540 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh); 1541 1542 static int check_src_addr(const struct in6_addr *saddr, 1543 struct netlink_ext_ack *extack) 1544 { 1545 if (!ipv6_addr_any(saddr)) { 1546 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects"); 1547 return -EINVAL; 1548 } 1549 return 0; 1550 } 1551 1552 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, 1553 struct netlink_ext_ack *extack) 1554 { 1555 struct nh_info *nhi; 1556 bool is_fdb_nh; 1557 1558 /* fib6_src is unique to a fib6_info and limits the ability to cache 1559 * routes in fib6_nh within a nexthop that is potentially shared 1560 * across multiple fib entries. If the config wants to use source 1561 * routing it can not use nexthop objects. mlxsw also does not allow 1562 * fib6_src on routes. 1563 */ 1564 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0) 1565 return -EINVAL; 1566 1567 if (nh->is_group) { 1568 struct nh_group *nhg; 1569 1570 nhg = rcu_dereference_rtnl(nh->nh_grp); 1571 if (nhg->has_v4) 1572 goto no_v4_nh; 1573 is_fdb_nh = nhg->fdb_nh; 1574 } else { 1575 nhi = rcu_dereference_rtnl(nh->nh_info); 1576 if (nhi->family == AF_INET) 1577 goto no_v4_nh; 1578 is_fdb_nh = nhi->fdb_nh; 1579 } 1580 1581 if (is_fdb_nh) { 1582 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 1583 return -EINVAL; 1584 } 1585 1586 return 0; 1587 no_v4_nh: 1588 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop"); 1589 return -EINVAL; 1590 } 1591 EXPORT_SYMBOL_GPL(fib6_check_nexthop); 1592 1593 /* if existing nexthop has ipv6 routes linked to it, need 1594 * to verify this new spec works with ipv6 1595 */ 1596 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new, 1597 struct netlink_ext_ack *extack) 1598 { 1599 struct fib6_info *f6i; 1600 1601 if (list_empty(&old->f6i_list)) 1602 return 0; 1603 1604 list_for_each_entry(f6i, &old->f6i_list, nh_list) { 1605 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0) 1606 return -EINVAL; 1607 } 1608 1609 return fib6_check_nexthop(new, NULL, extack); 1610 } 1611 1612 static int nexthop_check_scope(struct nh_info *nhi, u8 scope, 1613 struct netlink_ext_ack *extack) 1614 { 1615 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) { 1616 NL_SET_ERR_MSG(extack, 1617 "Route with host scope can not have a gateway"); 1618 return -EINVAL; 1619 } 1620 1621 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) { 1622 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop"); 1623 return -EINVAL; 1624 } 1625 1626 return 0; 1627 } 1628 1629 /* Invoked by fib add code to verify nexthop by id is ok with 1630 * config for prefix; parts of fib_check_nh not done when nexthop 1631 * object is used. 1632 */ 1633 int fib_check_nexthop(struct nexthop *nh, u8 scope, 1634 struct netlink_ext_ack *extack) 1635 { 1636 struct nh_info *nhi; 1637 int err = 0; 1638 1639 if (nh->is_group) { 1640 struct nh_group *nhg; 1641 1642 nhg = rtnl_dereference(nh->nh_grp); 1643 if (nhg->fdb_nh) { 1644 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 1645 err = -EINVAL; 1646 goto out; 1647 } 1648 1649 if (scope == RT_SCOPE_HOST) { 1650 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops"); 1651 err = -EINVAL; 1652 goto out; 1653 } 1654 1655 /* all nexthops in a group have the same scope */ 1656 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info); 1657 err = nexthop_check_scope(nhi, scope, extack); 1658 } else { 1659 nhi = rtnl_dereference(nh->nh_info); 1660 if (nhi->fdb_nh) { 1661 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 1662 err = -EINVAL; 1663 goto out; 1664 } 1665 err = nexthop_check_scope(nhi, scope, extack); 1666 } 1667 1668 out: 1669 return err; 1670 } 1671 1672 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new, 1673 struct netlink_ext_ack *extack) 1674 { 1675 struct fib_info *fi; 1676 1677 list_for_each_entry(fi, &old->fi_list, nh_list) { 1678 int err; 1679 1680 err = fib_check_nexthop(new, fi->fib_scope, extack); 1681 if (err) 1682 return err; 1683 } 1684 return 0; 1685 } 1686 1687 static bool nh_res_nhge_is_balanced(const struct nh_grp_entry *nhge) 1688 { 1689 return nhge->res.count_buckets == nhge->res.wants_buckets; 1690 } 1691 1692 static bool nh_res_nhge_is_ow(const struct nh_grp_entry *nhge) 1693 { 1694 return nhge->res.count_buckets > nhge->res.wants_buckets; 1695 } 1696 1697 static bool nh_res_nhge_is_uw(const struct nh_grp_entry *nhge) 1698 { 1699 return nhge->res.count_buckets < nhge->res.wants_buckets; 1700 } 1701 1702 static bool nh_res_table_is_balanced(const struct nh_res_table *res_table) 1703 { 1704 return list_empty(&res_table->uw_nh_entries); 1705 } 1706 1707 static void nh_res_bucket_unset_nh(struct nh_res_bucket *bucket) 1708 { 1709 struct nh_grp_entry *nhge; 1710 1711 if (bucket->occupied) { 1712 nhge = nh_res_dereference(bucket->nh_entry); 1713 nhge->res.count_buckets--; 1714 bucket->occupied = false; 1715 } 1716 } 1717 1718 static void nh_res_bucket_set_nh(struct nh_res_bucket *bucket, 1719 struct nh_grp_entry *nhge) 1720 { 1721 nh_res_bucket_unset_nh(bucket); 1722 1723 bucket->occupied = true; 1724 rcu_assign_pointer(bucket->nh_entry, nhge); 1725 nhge->res.count_buckets++; 1726 } 1727 1728 static bool nh_res_bucket_should_migrate(struct nh_res_table *res_table, 1729 struct nh_res_bucket *bucket, 1730 unsigned long *deadline, bool *force) 1731 { 1732 unsigned long now = jiffies; 1733 struct nh_grp_entry *nhge; 1734 unsigned long idle_point; 1735 1736 if (!bucket->occupied) { 1737 /* The bucket is not occupied, its NHGE pointer is either 1738 * NULL or obsolete. We _have to_ migrate: set force. 1739 */ 1740 *force = true; 1741 return true; 1742 } 1743 1744 nhge = nh_res_dereference(bucket->nh_entry); 1745 1746 /* If the bucket is populated by an underweight or balanced 1747 * nexthop, do not migrate. 1748 */ 1749 if (!nh_res_nhge_is_ow(nhge)) 1750 return false; 1751 1752 /* At this point we know that the bucket is populated with an 1753 * overweight nexthop. It needs to be migrated to a new nexthop if 1754 * the idle timer of unbalanced timer expired. 1755 */ 1756 1757 idle_point = nh_res_bucket_idle_point(res_table, bucket, now); 1758 if (time_after_eq(now, idle_point)) { 1759 /* The bucket is idle. We _can_ migrate: unset force. */ 1760 *force = false; 1761 return true; 1762 } 1763 1764 /* Unbalanced timer of 0 means "never force". */ 1765 if (res_table->unbalanced_timer) { 1766 unsigned long unb_point; 1767 1768 unb_point = nh_res_table_unb_point(res_table); 1769 if (time_after(now, unb_point)) { 1770 /* The bucket is not idle, but the unbalanced timer 1771 * expired. We _can_ migrate, but set force anyway, 1772 * so that drivers know to ignore activity reports 1773 * from the HW. 1774 */ 1775 *force = true; 1776 return true; 1777 } 1778 1779 nh_res_time_set_deadline(unb_point, deadline); 1780 } 1781 1782 nh_res_time_set_deadline(idle_point, deadline); 1783 return false; 1784 } 1785 1786 static bool nh_res_bucket_migrate(struct nh_res_table *res_table, 1787 u16 bucket_index, bool notify, 1788 bool notify_nl, bool force) 1789 { 1790 struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index]; 1791 struct nh_grp_entry *new_nhge; 1792 struct netlink_ext_ack extack; 1793 int err; 1794 1795 new_nhge = list_first_entry_or_null(&res_table->uw_nh_entries, 1796 struct nh_grp_entry, 1797 res.uw_nh_entry); 1798 if (WARN_ON_ONCE(!new_nhge)) 1799 /* If this function is called, "bucket" is either not 1800 * occupied, or it belongs to a next hop that is 1801 * overweight. In either case, there ought to be a 1802 * corresponding underweight next hop. 1803 */ 1804 return false; 1805 1806 if (notify) { 1807 struct nh_grp_entry *old_nhge; 1808 1809 old_nhge = nh_res_dereference(bucket->nh_entry); 1810 err = call_nexthop_res_bucket_notifiers(res_table->net, 1811 res_table->nhg_id, 1812 bucket_index, force, 1813 old_nhge->nh, 1814 new_nhge->nh, &extack); 1815 if (err) { 1816 pr_err_ratelimited("%s\n", extack._msg); 1817 if (!force) 1818 return false; 1819 /* It is not possible to veto a forced replacement, so 1820 * just clear the hardware flags from the nexthop 1821 * bucket to indicate to user space that this bucket is 1822 * not correctly populated in hardware. 1823 */ 1824 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 1825 } 1826 } 1827 1828 nh_res_bucket_set_nh(bucket, new_nhge); 1829 nh_res_bucket_set_idle(res_table, bucket); 1830 1831 if (notify_nl) 1832 nexthop_bucket_notify(res_table, bucket_index); 1833 1834 if (nh_res_nhge_is_balanced(new_nhge)) 1835 list_del(&new_nhge->res.uw_nh_entry); 1836 return true; 1837 } 1838 1839 #define NH_RES_UPKEEP_DW_MINIMUM_INTERVAL (HZ / 2) 1840 1841 static void nh_res_table_upkeep(struct nh_res_table *res_table, 1842 bool notify, bool notify_nl) 1843 { 1844 unsigned long now = jiffies; 1845 unsigned long deadline; 1846 u16 i; 1847 1848 /* Deadline is the next time that upkeep should be run. It is the 1849 * earliest time at which one of the buckets might be migrated. 1850 * Start at the most pessimistic estimate: either unbalanced_timer 1851 * from now, or if there is none, idle_timer from now. For each 1852 * encountered time point, call nh_res_time_set_deadline() to 1853 * refine the estimate. 1854 */ 1855 if (res_table->unbalanced_timer) 1856 deadline = now + res_table->unbalanced_timer; 1857 else 1858 deadline = now + res_table->idle_timer; 1859 1860 for (i = 0; i < res_table->num_nh_buckets; i++) { 1861 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 1862 bool force; 1863 1864 if (nh_res_bucket_should_migrate(res_table, bucket, 1865 &deadline, &force)) { 1866 if (!nh_res_bucket_migrate(res_table, i, notify, 1867 notify_nl, force)) { 1868 unsigned long idle_point; 1869 1870 /* A driver can override the migration 1871 * decision if the HW reports that the 1872 * bucket is actually not idle. Therefore 1873 * remark the bucket as busy again and 1874 * update the deadline. 1875 */ 1876 nh_res_bucket_set_busy(bucket); 1877 idle_point = nh_res_bucket_idle_point(res_table, 1878 bucket, 1879 now); 1880 nh_res_time_set_deadline(idle_point, &deadline); 1881 } 1882 } 1883 } 1884 1885 /* If the group is still unbalanced, schedule the next upkeep to 1886 * either the deadline computed above, or the minimum deadline, 1887 * whichever comes later. 1888 */ 1889 if (!nh_res_table_is_balanced(res_table)) { 1890 unsigned long now = jiffies; 1891 unsigned long min_deadline; 1892 1893 min_deadline = now + NH_RES_UPKEEP_DW_MINIMUM_INTERVAL; 1894 if (time_before(deadline, min_deadline)) 1895 deadline = min_deadline; 1896 1897 queue_delayed_work(system_power_efficient_wq, 1898 &res_table->upkeep_dw, deadline - now); 1899 } 1900 } 1901 1902 static void nh_res_table_upkeep_dw(struct work_struct *work) 1903 { 1904 struct delayed_work *dw = to_delayed_work(work); 1905 struct nh_res_table *res_table; 1906 1907 res_table = container_of(dw, struct nh_res_table, upkeep_dw); 1908 nh_res_table_upkeep(res_table, true, true); 1909 } 1910 1911 static void nh_res_table_cancel_upkeep(struct nh_res_table *res_table) 1912 { 1913 cancel_delayed_work_sync(&res_table->upkeep_dw); 1914 } 1915 1916 static void nh_res_group_rebalance(struct nh_group *nhg, 1917 struct nh_res_table *res_table) 1918 { 1919 u16 prev_upper_bound = 0; 1920 u32 total = 0; 1921 u32 w = 0; 1922 int i; 1923 1924 INIT_LIST_HEAD(&res_table->uw_nh_entries); 1925 1926 for (i = 0; i < nhg->num_nh; ++i) 1927 total += nhg->nh_entries[i].weight; 1928 1929 for (i = 0; i < nhg->num_nh; ++i) { 1930 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 1931 u16 upper_bound; 1932 u64 btw; 1933 1934 w += nhge->weight; 1935 btw = ((u64)res_table->num_nh_buckets) * w; 1936 upper_bound = DIV_ROUND_CLOSEST_ULL(btw, total); 1937 nhge->res.wants_buckets = upper_bound - prev_upper_bound; 1938 prev_upper_bound = upper_bound; 1939 1940 if (nh_res_nhge_is_uw(nhge)) { 1941 if (list_empty(&res_table->uw_nh_entries)) 1942 res_table->unbalanced_since = jiffies; 1943 list_add(&nhge->res.uw_nh_entry, 1944 &res_table->uw_nh_entries); 1945 } 1946 } 1947 } 1948 1949 /* Migrate buckets in res_table so that they reference NHGE's from NHG with 1950 * the right NH ID. Set those buckets that do not have a corresponding NHGE 1951 * entry in NHG as not occupied. 1952 */ 1953 static void nh_res_table_migrate_buckets(struct nh_res_table *res_table, 1954 struct nh_group *nhg) 1955 { 1956 u16 i; 1957 1958 for (i = 0; i < res_table->num_nh_buckets; i++) { 1959 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 1960 u32 id = rtnl_dereference(bucket->nh_entry)->nh->id; 1961 bool found = false; 1962 int j; 1963 1964 for (j = 0; j < nhg->num_nh; j++) { 1965 struct nh_grp_entry *nhge = &nhg->nh_entries[j]; 1966 1967 if (nhge->nh->id == id) { 1968 nh_res_bucket_set_nh(bucket, nhge); 1969 found = true; 1970 break; 1971 } 1972 } 1973 1974 if (!found) 1975 nh_res_bucket_unset_nh(bucket); 1976 } 1977 } 1978 1979 static void replace_nexthop_grp_res(struct nh_group *oldg, 1980 struct nh_group *newg) 1981 { 1982 /* For NH group replacement, the new NHG might only have a stub 1983 * hash table with 0 buckets, because the number of buckets was not 1984 * specified. For NH removal, oldg and newg both reference the same 1985 * res_table. So in any case, in the following, we want to work 1986 * with oldg->res_table. 1987 */ 1988 struct nh_res_table *old_res_table = rtnl_dereference(oldg->res_table); 1989 unsigned long prev_unbalanced_since = old_res_table->unbalanced_since; 1990 bool prev_has_uw = !list_empty(&old_res_table->uw_nh_entries); 1991 1992 nh_res_table_cancel_upkeep(old_res_table); 1993 nh_res_table_migrate_buckets(old_res_table, newg); 1994 nh_res_group_rebalance(newg, old_res_table); 1995 if (prev_has_uw && !list_empty(&old_res_table->uw_nh_entries)) 1996 old_res_table->unbalanced_since = prev_unbalanced_since; 1997 nh_res_table_upkeep(old_res_table, true, false); 1998 } 1999 2000 static void nh_hthr_group_rebalance(struct nh_group *nhg) 2001 { 2002 u32 total = 0; 2003 u32 w = 0; 2004 int i; 2005 2006 for (i = 0; i < nhg->num_nh; ++i) 2007 total += nhg->nh_entries[i].weight; 2008 2009 for (i = 0; i < nhg->num_nh; ++i) { 2010 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 2011 u32 upper_bound; 2012 2013 w += nhge->weight; 2014 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1; 2015 atomic_set(&nhge->hthr.upper_bound, upper_bound); 2016 } 2017 } 2018 2019 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, 2020 struct nl_info *nlinfo, 2021 struct list_head *deferred_free) 2022 { 2023 struct nh_grp_entry *nhges, *new_nhges; 2024 struct nexthop *nhp = nhge->nh_parent; 2025 struct netlink_ext_ack extack; 2026 struct nexthop *nh = nhge->nh; 2027 struct nh_group *nhg, *newg; 2028 int i, j, err; 2029 2030 WARN_ON(!nh); 2031 2032 nhg = rtnl_dereference(nhp->nh_grp); 2033 newg = nhg->spare; 2034 2035 /* last entry, keep it visible and remove the parent */ 2036 if (nhg->num_nh == 1) { 2037 remove_nexthop(net, nhp, nlinfo); 2038 return; 2039 } 2040 2041 newg->has_v4 = false; 2042 newg->is_multipath = nhg->is_multipath; 2043 newg->hash_threshold = nhg->hash_threshold; 2044 newg->resilient = nhg->resilient; 2045 newg->fdb_nh = nhg->fdb_nh; 2046 newg->num_nh = nhg->num_nh; 2047 2048 /* copy old entries to new except the one getting removed */ 2049 nhges = nhg->nh_entries; 2050 new_nhges = newg->nh_entries; 2051 for (i = 0, j = 0; i < nhg->num_nh; ++i) { 2052 struct nh_info *nhi; 2053 2054 /* current nexthop getting removed */ 2055 if (nhg->nh_entries[i].nh == nh) { 2056 newg->num_nh--; 2057 continue; 2058 } 2059 2060 nhi = rtnl_dereference(nhges[i].nh->nh_info); 2061 if (nhi->family == AF_INET) 2062 newg->has_v4 = true; 2063 2064 list_del(&nhges[i].nh_list); 2065 new_nhges[j].stats = nhges[i].stats; 2066 new_nhges[j].nh_parent = nhges[i].nh_parent; 2067 new_nhges[j].nh = nhges[i].nh; 2068 new_nhges[j].weight = nhges[i].weight; 2069 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); 2070 j++; 2071 } 2072 2073 if (newg->hash_threshold) 2074 nh_hthr_group_rebalance(newg); 2075 else if (newg->resilient) 2076 replace_nexthop_grp_res(nhg, newg); 2077 2078 rcu_assign_pointer(nhp->nh_grp, newg); 2079 2080 list_del(&nhge->nh_list); 2081 nexthop_put(nhge->nh); 2082 list_add(&nhge->nh_list, deferred_free); 2083 2084 /* Removal of a NH from a resilient group is notified through 2085 * bucket notifications. 2086 */ 2087 if (newg->hash_threshold) { 2088 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, 2089 &extack); 2090 if (err) 2091 pr_err("%s\n", extack._msg); 2092 } 2093 2094 if (nlinfo) 2095 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); 2096 } 2097 2098 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh, 2099 struct nl_info *nlinfo) 2100 { 2101 struct nh_grp_entry *nhge, *tmp; 2102 LIST_HEAD(deferred_free); 2103 2104 /* If there is nothing to do, let's avoid the costly call to 2105 * synchronize_net() 2106 */ 2107 if (list_empty(&nh->grp_list)) 2108 return; 2109 2110 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) 2111 remove_nh_grp_entry(net, nhge, nlinfo, &deferred_free); 2112 2113 /* make sure all see the newly published array before releasing rtnl */ 2114 synchronize_net(); 2115 2116 /* Now safe to free percpu stats — all RCU readers have finished */ 2117 list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) { 2118 list_del(&nhge->nh_list); 2119 free_percpu(nhge->stats); 2120 } 2121 } 2122 2123 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) 2124 { 2125 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 2126 struct nh_res_table *res_table; 2127 int i, num_nh = nhg->num_nh; 2128 2129 for (i = 0; i < num_nh; ++i) { 2130 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 2131 2132 if (WARN_ON(!nhge->nh)) 2133 continue; 2134 2135 list_del_init(&nhge->nh_list); 2136 } 2137 2138 if (nhg->resilient) { 2139 res_table = rtnl_dereference(nhg->res_table); 2140 nh_res_table_cancel_upkeep(res_table); 2141 } 2142 } 2143 2144 /* not called for nexthop replace */ 2145 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh) 2146 { 2147 struct fib6_info *f6i; 2148 bool do_flush = false; 2149 struct fib_info *fi; 2150 2151 list_for_each_entry(fi, &nh->fi_list, nh_list) { 2152 fi->fib_flags |= RTNH_F_DEAD; 2153 do_flush = true; 2154 } 2155 if (do_flush) 2156 fib_flush(net); 2157 2158 spin_lock_bh(&nh->lock); 2159 2160 nh->dead = true; 2161 2162 while (!list_empty(&nh->f6i_list)) { 2163 f6i = list_first_entry(&nh->f6i_list, typeof(*f6i), nh_list); 2164 2165 /* __ip6_del_rt does a release, so do a hold here */ 2166 fib6_info_hold(f6i); 2167 2168 spin_unlock_bh(&nh->lock); 2169 ip6_del_rt(net, f6i, 2170 !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)); 2171 2172 spin_lock_bh(&nh->lock); 2173 } 2174 2175 spin_unlock_bh(&nh->lock); 2176 } 2177 2178 static void __remove_nexthop(struct net *net, struct nexthop *nh, 2179 struct nl_info *nlinfo) 2180 { 2181 __remove_nexthop_fib(net, nh); 2182 2183 if (nh->is_group) { 2184 remove_nexthop_group(nh, nlinfo); 2185 } else { 2186 struct nh_info *nhi; 2187 2188 nhi = rtnl_dereference(nh->nh_info); 2189 if (nhi->fib_nhc.nhc_dev) 2190 hlist_del(&nhi->dev_hash); 2191 2192 remove_nexthop_from_groups(net, nh, nlinfo); 2193 } 2194 } 2195 2196 static void remove_nexthop(struct net *net, struct nexthop *nh, 2197 struct nl_info *nlinfo) 2198 { 2199 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL); 2200 2201 /* remove from the tree */ 2202 rb_erase(&nh->rb_node, &net->nexthop.rb_root); 2203 2204 if (nlinfo) 2205 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); 2206 2207 __remove_nexthop(net, nh, nlinfo); 2208 nh_base_seq_inc(net); 2209 2210 nexthop_put(nh); 2211 } 2212 2213 /* if any FIB entries reference this nexthop, any dst entries 2214 * need to be regenerated 2215 */ 2216 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh, 2217 struct nexthop *replaced_nh) 2218 { 2219 struct fib6_info *f6i; 2220 struct nh_group *nhg; 2221 int i; 2222 2223 if (!list_empty(&nh->fi_list)) 2224 rt_cache_flush(net); 2225 2226 list_for_each_entry(f6i, &nh->f6i_list, nh_list) { 2227 spin_lock_bh(&f6i->fib6_table->tb6_lock); 2228 fib6_update_sernum_upto_root(net, f6i); 2229 spin_unlock_bh(&f6i->fib6_table->tb6_lock); 2230 } 2231 2232 /* if an IPv6 group was replaced, we have to release all old 2233 * dsts to make sure all refcounts are released 2234 */ 2235 if (!replaced_nh->is_group) 2236 return; 2237 2238 nhg = rtnl_dereference(replaced_nh->nh_grp); 2239 for (i = 0; i < nhg->num_nh; i++) { 2240 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 2241 struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info); 2242 2243 if (nhi->family == AF_INET6) 2244 fib6_nh_release_dsts(&nhi->fib6_nh); 2245 } 2246 } 2247 2248 static int replace_nexthop_grp(struct net *net, struct nexthop *old, 2249 struct nexthop *new, const struct nh_config *cfg, 2250 struct netlink_ext_ack *extack) 2251 { 2252 struct nh_res_table *tmp_table = NULL; 2253 struct nh_res_table *new_res_table; 2254 struct nh_res_table *old_res_table; 2255 struct nh_group *oldg, *newg; 2256 int i, err; 2257 2258 if (!new->is_group) { 2259 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); 2260 return -EINVAL; 2261 } 2262 2263 oldg = rtnl_dereference(old->nh_grp); 2264 newg = rtnl_dereference(new->nh_grp); 2265 2266 if (newg->hash_threshold != oldg->hash_threshold) { 2267 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type."); 2268 return -EINVAL; 2269 } 2270 2271 if (newg->hash_threshold) { 2272 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, 2273 extack); 2274 if (err) 2275 return err; 2276 } else if (newg->resilient) { 2277 new_res_table = rtnl_dereference(newg->res_table); 2278 old_res_table = rtnl_dereference(oldg->res_table); 2279 2280 /* Accept if num_nh_buckets was not given, but if it was 2281 * given, demand that the value be correct. 2282 */ 2283 if (cfg->nh_grp_res_has_num_buckets && 2284 cfg->nh_grp_res_num_buckets != 2285 old_res_table->num_nh_buckets) { 2286 NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group."); 2287 return -EINVAL; 2288 } 2289 2290 /* Emit a pre-replace notification so that listeners could veto 2291 * a potentially unsupported configuration. Otherwise, 2292 * individual bucket replacement notifications would need to be 2293 * vetoed, which is something that should only happen if the 2294 * bucket is currently active. 2295 */ 2296 err = call_nexthop_res_table_notifiers(net, new, extack); 2297 if (err) 2298 return err; 2299 2300 if (cfg->nh_grp_res_has_idle_timer) 2301 old_res_table->idle_timer = cfg->nh_grp_res_idle_timer; 2302 if (cfg->nh_grp_res_has_unbalanced_timer) 2303 old_res_table->unbalanced_timer = 2304 cfg->nh_grp_res_unbalanced_timer; 2305 2306 replace_nexthop_grp_res(oldg, newg); 2307 2308 tmp_table = new_res_table; 2309 rcu_assign_pointer(newg->res_table, old_res_table); 2310 rcu_assign_pointer(newg->spare->res_table, old_res_table); 2311 } 2312 2313 /* update parents - used by nexthop code for cleanup */ 2314 for (i = 0; i < newg->num_nh; i++) 2315 newg->nh_entries[i].nh_parent = old; 2316 2317 rcu_assign_pointer(old->nh_grp, newg); 2318 2319 /* Make sure concurrent readers are not using 'oldg' anymore. */ 2320 synchronize_net(); 2321 2322 if (newg->resilient) { 2323 rcu_assign_pointer(oldg->res_table, tmp_table); 2324 rcu_assign_pointer(oldg->spare->res_table, tmp_table); 2325 } 2326 2327 for (i = 0; i < oldg->num_nh; i++) 2328 oldg->nh_entries[i].nh_parent = new; 2329 2330 rcu_assign_pointer(new->nh_grp, oldg); 2331 2332 return 0; 2333 } 2334 2335 static void nh_group_v4_update(struct nh_group *nhg) 2336 { 2337 struct nh_grp_entry *nhges; 2338 bool has_v4 = false; 2339 int i; 2340 2341 nhges = nhg->nh_entries; 2342 for (i = 0; i < nhg->num_nh; i++) { 2343 struct nh_info *nhi; 2344 2345 nhi = rtnl_dereference(nhges[i].nh->nh_info); 2346 if (nhi->family == AF_INET) 2347 has_v4 = true; 2348 } 2349 nhg->has_v4 = has_v4; 2350 } 2351 2352 static int replace_nexthop_single_notify_res(struct net *net, 2353 struct nh_res_table *res_table, 2354 struct nexthop *old, 2355 struct nh_info *oldi, 2356 struct nh_info *newi, 2357 struct netlink_ext_ack *extack) 2358 { 2359 u32 nhg_id = res_table->nhg_id; 2360 int err; 2361 u16 i; 2362 2363 for (i = 0; i < res_table->num_nh_buckets; i++) { 2364 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 2365 struct nh_grp_entry *nhge; 2366 2367 nhge = rtnl_dereference(bucket->nh_entry); 2368 if (nhge->nh == old) { 2369 err = __call_nexthop_res_bucket_notifiers(net, nhg_id, 2370 i, true, 2371 oldi, newi, 2372 extack); 2373 if (err) 2374 goto err_notify; 2375 } 2376 } 2377 2378 return 0; 2379 2380 err_notify: 2381 while (i-- > 0) { 2382 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 2383 struct nh_grp_entry *nhge; 2384 2385 nhge = rtnl_dereference(bucket->nh_entry); 2386 if (nhge->nh == old) 2387 __call_nexthop_res_bucket_notifiers(net, nhg_id, i, 2388 true, newi, oldi, 2389 extack); 2390 } 2391 return err; 2392 } 2393 2394 static int replace_nexthop_single_notify(struct net *net, 2395 struct nexthop *group_nh, 2396 struct nexthop *old, 2397 struct nh_info *oldi, 2398 struct nh_info *newi, 2399 struct netlink_ext_ack *extack) 2400 { 2401 struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp); 2402 struct nh_res_table *res_table; 2403 2404 if (nhg->hash_threshold) { 2405 return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, 2406 group_nh, extack); 2407 } else if (nhg->resilient) { 2408 res_table = rtnl_dereference(nhg->res_table); 2409 return replace_nexthop_single_notify_res(net, res_table, 2410 old, oldi, newi, 2411 extack); 2412 } 2413 2414 return -EINVAL; 2415 } 2416 2417 static int replace_nexthop_single(struct net *net, struct nexthop *old, 2418 struct nexthop *new, 2419 struct netlink_ext_ack *extack) 2420 { 2421 u8 old_protocol, old_nh_flags; 2422 struct nh_info *oldi, *newi; 2423 struct nh_grp_entry *nhge; 2424 int err; 2425 2426 if (new->is_group) { 2427 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); 2428 return -EINVAL; 2429 } 2430 2431 if (!list_empty(&old->grp_list) && 2432 rtnl_dereference(new->nh_info)->fdb_nh != 2433 rtnl_dereference(old->nh_info)->fdb_nh) { 2434 NL_SET_ERR_MSG(extack, "Cannot change nexthop FDB status while in a group"); 2435 return -EINVAL; 2436 } 2437 2438 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack); 2439 if (err) 2440 return err; 2441 2442 /* Hardware flags were set on 'old' as 'new' is not in the red-black 2443 * tree. Therefore, inherit the flags from 'old' to 'new'. 2444 */ 2445 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP); 2446 2447 oldi = rtnl_dereference(old->nh_info); 2448 newi = rtnl_dereference(new->nh_info); 2449 2450 newi->nh_parent = old; 2451 oldi->nh_parent = new; 2452 2453 old_protocol = old->protocol; 2454 old_nh_flags = old->nh_flags; 2455 2456 old->protocol = new->protocol; 2457 old->nh_flags = new->nh_flags; 2458 2459 rcu_assign_pointer(old->nh_info, newi); 2460 rcu_assign_pointer(new->nh_info, oldi); 2461 2462 /* Send a replace notification for all the groups using the nexthop. */ 2463 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2464 struct nexthop *nhp = nhge->nh_parent; 2465 2466 err = replace_nexthop_single_notify(net, nhp, old, oldi, newi, 2467 extack); 2468 if (err) 2469 goto err_notify; 2470 } 2471 2472 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially 2473 * update IPv4 indication in all the groups using the nexthop. 2474 */ 2475 if (oldi->family == AF_INET && newi->family == AF_INET6) { 2476 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2477 struct nexthop *nhp = nhge->nh_parent; 2478 struct nh_group *nhg; 2479 2480 nhg = rtnl_dereference(nhp->nh_grp); 2481 nh_group_v4_update(nhg); 2482 } 2483 } 2484 2485 return 0; 2486 2487 err_notify: 2488 rcu_assign_pointer(new->nh_info, newi); 2489 rcu_assign_pointer(old->nh_info, oldi); 2490 old->nh_flags = old_nh_flags; 2491 old->protocol = old_protocol; 2492 oldi->nh_parent = old; 2493 newi->nh_parent = new; 2494 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) { 2495 struct nexthop *nhp = nhge->nh_parent; 2496 2497 replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL); 2498 } 2499 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack); 2500 return err; 2501 } 2502 2503 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh, 2504 struct nl_info *info) 2505 { 2506 struct fib6_info *f6i; 2507 2508 if (!list_empty(&nh->fi_list)) { 2509 struct fib_info *fi; 2510 2511 /* expectation is a few fib_info per nexthop and then 2512 * a lot of routes per fib_info. So mark the fib_info 2513 * and then walk the fib tables once 2514 */ 2515 list_for_each_entry(fi, &nh->fi_list, nh_list) 2516 fi->nh_updated = true; 2517 2518 fib_info_notify_update(net, info); 2519 2520 list_for_each_entry(fi, &nh->fi_list, nh_list) 2521 fi->nh_updated = false; 2522 } 2523 2524 list_for_each_entry(f6i, &nh->f6i_list, nh_list) 2525 fib6_rt_update(net, f6i, info); 2526 } 2527 2528 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries 2529 * linked to this nexthop and for all groups that the nexthop 2530 * is a member of 2531 */ 2532 static void nexthop_replace_notify(struct net *net, struct nexthop *nh, 2533 struct nl_info *info) 2534 { 2535 struct nh_grp_entry *nhge; 2536 2537 __nexthop_replace_notify(net, nh, info); 2538 2539 list_for_each_entry(nhge, &nh->grp_list, nh_list) 2540 __nexthop_replace_notify(net, nhge->nh_parent, info); 2541 } 2542 2543 static int replace_nexthop(struct net *net, struct nexthop *old, 2544 struct nexthop *new, const struct nh_config *cfg, 2545 struct netlink_ext_ack *extack) 2546 { 2547 bool new_is_reject = false; 2548 struct nh_grp_entry *nhge; 2549 int err; 2550 2551 /* check that existing FIB entries are ok with the 2552 * new nexthop definition 2553 */ 2554 err = fib_check_nh_list(old, new, extack); 2555 if (err) 2556 return err; 2557 2558 err = fib6_check_nh_list(old, new, extack); 2559 if (err) 2560 return err; 2561 2562 if (!new->is_group) { 2563 struct nh_info *nhi = rtnl_dereference(new->nh_info); 2564 2565 new_is_reject = nhi->reject_nh; 2566 } 2567 2568 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2569 /* if new nexthop is a blackhole, any groups using this 2570 * nexthop cannot have more than 1 path 2571 */ 2572 if (new_is_reject && 2573 nexthop_num_path(nhge->nh_parent) > 1) { 2574 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); 2575 return -EINVAL; 2576 } 2577 2578 err = fib_check_nh_list(nhge->nh_parent, new, extack); 2579 if (err) 2580 return err; 2581 2582 err = fib6_check_nh_list(nhge->nh_parent, new, extack); 2583 if (err) 2584 return err; 2585 } 2586 2587 if (old->is_group) 2588 err = replace_nexthop_grp(net, old, new, cfg, extack); 2589 else 2590 err = replace_nexthop_single(net, old, new, extack); 2591 2592 if (!err) { 2593 nh_rt_cache_flush(net, old, new); 2594 2595 __remove_nexthop(net, new, NULL); 2596 nexthop_put(new); 2597 } 2598 2599 return err; 2600 } 2601 2602 /* called with rtnl_lock held */ 2603 static int insert_nexthop(struct net *net, struct nexthop *new_nh, 2604 struct nh_config *cfg, struct netlink_ext_ack *extack) 2605 { 2606 struct rb_node **pp, *parent = NULL, *next; 2607 struct rb_root *root = &net->nexthop.rb_root; 2608 bool replace = !!(cfg->nlflags & NLM_F_REPLACE); 2609 bool create = !!(cfg->nlflags & NLM_F_CREATE); 2610 u32 new_id = new_nh->id; 2611 int replace_notify = 0; 2612 int rc = -EEXIST; 2613 2614 pp = &root->rb_node; 2615 while (1) { 2616 struct nexthop *nh; 2617 2618 next = *pp; 2619 if (!next) 2620 break; 2621 2622 parent = next; 2623 2624 nh = rb_entry(parent, struct nexthop, rb_node); 2625 if (new_id < nh->id) { 2626 pp = &next->rb_left; 2627 } else if (new_id > nh->id) { 2628 pp = &next->rb_right; 2629 } else if (replace) { 2630 rc = replace_nexthop(net, nh, new_nh, cfg, extack); 2631 if (!rc) { 2632 new_nh = nh; /* send notification with old nh */ 2633 replace_notify = 1; 2634 } 2635 goto out; 2636 } else { 2637 /* id already exists and not a replace */ 2638 goto out; 2639 } 2640 } 2641 2642 if (replace && !create) { 2643 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); 2644 rc = -ENOENT; 2645 goto out; 2646 } 2647 2648 if (new_nh->is_group) { 2649 struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp); 2650 struct nh_res_table *res_table; 2651 2652 if (nhg->resilient) { 2653 res_table = rtnl_dereference(nhg->res_table); 2654 2655 /* Not passing the number of buckets is OK when 2656 * replacing, but not when creating a new group. 2657 */ 2658 if (!cfg->nh_grp_res_has_num_buckets) { 2659 NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion"); 2660 rc = -EINVAL; 2661 goto out; 2662 } 2663 2664 nh_res_group_rebalance(nhg, res_table); 2665 2666 /* Do not send bucket notifications, we do full 2667 * notification below. 2668 */ 2669 nh_res_table_upkeep(res_table, false, false); 2670 } 2671 } 2672 2673 rb_link_node_rcu(&new_nh->rb_node, parent, pp); 2674 rb_insert_color(&new_nh->rb_node, root); 2675 2676 /* The initial insertion is a full notification for hash-threshold as 2677 * well as resilient groups. 2678 */ 2679 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack); 2680 if (rc) 2681 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root); 2682 2683 out: 2684 if (!rc) { 2685 nh_base_seq_inc(net); 2686 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 2687 if (replace_notify && 2688 READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)) 2689 nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 2690 } 2691 2692 return rc; 2693 } 2694 2695 /* rtnl */ 2696 /* remove all nexthops tied to a device being deleted */ 2697 static void nexthop_flush_dev(struct net_device *dev, unsigned long event) 2698 { 2699 unsigned int hash = nh_dev_hashfn(dev->ifindex); 2700 struct net *net = dev_net(dev); 2701 struct hlist_head *head = &net->nexthop.devhash[hash]; 2702 struct hlist_node *n; 2703 struct nh_info *nhi; 2704 2705 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 2706 if (nhi->fib_nhc.nhc_dev != dev) 2707 continue; 2708 2709 if (nhi->reject_nh && 2710 (event == NETDEV_DOWN || event == NETDEV_CHANGE)) 2711 continue; 2712 2713 remove_nexthop(net, nhi->nh_parent, NULL); 2714 } 2715 } 2716 2717 /* rtnl; called when net namespace is deleted */ 2718 static void flush_all_nexthops(struct net *net) 2719 { 2720 struct rb_root *root = &net->nexthop.rb_root; 2721 struct rb_node *node; 2722 struct nexthop *nh; 2723 2724 while ((node = rb_first(root))) { 2725 nh = rb_entry(node, struct nexthop, rb_node); 2726 remove_nexthop(net, nh, NULL); 2727 cond_resched(); 2728 } 2729 } 2730 2731 static struct nexthop *nexthop_create_group(struct net *net, 2732 struct nh_config *cfg) 2733 { 2734 struct nlattr *grps_attr = cfg->nh_grp; 2735 struct nexthop_grp *entry = nla_data(grps_attr); 2736 u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 2737 struct nh_group *nhg; 2738 struct nexthop *nh; 2739 int err; 2740 int i; 2741 2742 nh = nexthop_alloc(); 2743 if (!nh) 2744 return ERR_PTR(-ENOMEM); 2745 2746 nh->is_group = 1; 2747 2748 nhg = nexthop_grp_alloc(num_nh); 2749 if (!nhg) { 2750 kfree(nh); 2751 return ERR_PTR(-ENOMEM); 2752 } 2753 2754 /* spare group used for removals */ 2755 nhg->spare = nexthop_grp_alloc(num_nh); 2756 if (!nhg->spare) { 2757 kfree(nhg); 2758 kfree(nh); 2759 return ERR_PTR(-ENOMEM); 2760 } 2761 nhg->spare->spare = nhg; 2762 2763 for (i = 0; i < nhg->num_nh; ++i) { 2764 struct nexthop *nhe; 2765 struct nh_info *nhi; 2766 2767 nhe = nexthop_find_by_id(net, entry[i].id); 2768 if (!nexthop_get(nhe)) { 2769 err = -ENOENT; 2770 goto out_no_nh; 2771 } 2772 2773 nhi = rtnl_dereference(nhe->nh_info); 2774 if (nhi->family == AF_INET) 2775 nhg->has_v4 = true; 2776 2777 nhg->nh_entries[i].stats = 2778 netdev_alloc_pcpu_stats(struct nh_grp_entry_stats); 2779 if (!nhg->nh_entries[i].stats) { 2780 err = -ENOMEM; 2781 nexthop_put(nhe); 2782 goto out_no_nh; 2783 } 2784 nhg->nh_entries[i].nh = nhe; 2785 nhg->nh_entries[i].weight = nexthop_grp_weight(&entry[i]); 2786 2787 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 2788 nhg->nh_entries[i].nh_parent = nh; 2789 } 2790 2791 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 2792 nhg->hash_threshold = 1; 2793 nhg->is_multipath = true; 2794 } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) { 2795 struct nh_res_table *res_table; 2796 2797 res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg); 2798 if (!res_table) { 2799 err = -ENOMEM; 2800 goto out_no_nh; 2801 } 2802 2803 rcu_assign_pointer(nhg->spare->res_table, res_table); 2804 rcu_assign_pointer(nhg->res_table, res_table); 2805 nhg->resilient = true; 2806 nhg->is_multipath = true; 2807 } 2808 2809 WARN_ON_ONCE(nhg->hash_threshold + nhg->resilient != 1); 2810 2811 if (nhg->hash_threshold) 2812 nh_hthr_group_rebalance(nhg); 2813 2814 if (cfg->nh_fdb) 2815 nhg->fdb_nh = 1; 2816 2817 if (cfg->nh_hw_stats) 2818 nhg->hw_stats = true; 2819 2820 rcu_assign_pointer(nh->nh_grp, nhg); 2821 2822 return nh; 2823 2824 out_no_nh: 2825 for (i--; i >= 0; --i) { 2826 list_del(&nhg->nh_entries[i].nh_list); 2827 free_percpu(nhg->nh_entries[i].stats); 2828 nexthop_put(nhg->nh_entries[i].nh); 2829 } 2830 2831 kfree(nhg->spare); 2832 kfree(nhg); 2833 kfree(nh); 2834 2835 return ERR_PTR(err); 2836 } 2837 2838 static int nh_create_ipv4(struct net *net, struct nexthop *nh, 2839 struct nh_info *nhi, struct nh_config *cfg, 2840 struct netlink_ext_ack *extack) 2841 { 2842 struct fib_nh *fib_nh = &nhi->fib_nh; 2843 struct fib_config fib_cfg = { 2844 .fc_oif = cfg->nh_ifindex, 2845 .fc_gw4 = cfg->gw.ipv4, 2846 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 2847 .fc_flags = cfg->nh_flags, 2848 .fc_nlinfo = cfg->nlinfo, 2849 .fc_encap = cfg->nh_encap, 2850 .fc_encap_type = cfg->nh_encap_type, 2851 }; 2852 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 2853 int err; 2854 2855 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 2856 if (err) { 2857 fib_nh_release(net, fib_nh); 2858 goto out; 2859 } 2860 2861 if (nhi->fdb_nh) 2862 goto out; 2863 2864 /* sets nh_dev if successful */ 2865 err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 2866 if (!err) { 2867 nh->nh_flags = fib_nh->fib_nh_flags; 2868 fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 2869 !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1); 2870 } else { 2871 fib_nh_release(net, fib_nh); 2872 } 2873 out: 2874 return err; 2875 } 2876 2877 static int nh_create_ipv6(struct net *net, struct nexthop *nh, 2878 struct nh_info *nhi, struct nh_config *cfg, 2879 struct netlink_ext_ack *extack) 2880 { 2881 struct fib6_nh *fib6_nh = &nhi->fib6_nh; 2882 struct fib6_config fib6_cfg = { 2883 .fc_table = l3mdev_fib_table(cfg->dev), 2884 .fc_ifindex = cfg->nh_ifindex, 2885 .fc_gateway = cfg->gw.ipv6, 2886 .fc_flags = cfg->nh_flags, 2887 .fc_nlinfo = cfg->nlinfo, 2888 .fc_encap = cfg->nh_encap, 2889 .fc_encap_type = cfg->nh_encap_type, 2890 .fc_is_fdb = cfg->nh_fdb, 2891 }; 2892 int err; 2893 2894 if (!ipv6_addr_any(&cfg->gw.ipv6)) 2895 fib6_cfg.fc_flags |= RTF_GATEWAY; 2896 2897 /* sets nh_dev if successful */ 2898 err = fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, extack); 2899 if (err) { 2900 /* IPv6 is not enabled, don't call fib6_nh_release */ 2901 if (err == -EAFNOSUPPORT) 2902 goto out; 2903 fib6_nh_release(fib6_nh); 2904 } else { 2905 nh->nh_flags = fib6_nh->fib_nh_flags; 2906 } 2907 out: 2908 return err; 2909 } 2910 2911 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 2912 struct netlink_ext_ack *extack) 2913 { 2914 struct nh_info *nhi; 2915 struct nexthop *nh; 2916 int err = 0; 2917 2918 nh = nexthop_alloc(); 2919 if (!nh) 2920 return ERR_PTR(-ENOMEM); 2921 2922 nhi = kzalloc_obj(*nhi); 2923 if (!nhi) { 2924 kfree(nh); 2925 return ERR_PTR(-ENOMEM); 2926 } 2927 2928 nh->nh_flags = cfg->nh_flags; 2929 nh->net = net; 2930 2931 nhi->nh_parent = nh; 2932 nhi->family = cfg->nh_family; 2933 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 2934 2935 if (cfg->nh_fdb) 2936 nhi->fdb_nh = 1; 2937 2938 if (cfg->nh_blackhole) { 2939 nhi->reject_nh = 1; 2940 cfg->nh_ifindex = net->loopback_dev->ifindex; 2941 } 2942 2943 switch (cfg->nh_family) { 2944 case AF_INET: 2945 err = nh_create_ipv4(net, nh, nhi, cfg, extack); 2946 break; 2947 case AF_INET6: 2948 err = nh_create_ipv6(net, nh, nhi, cfg, extack); 2949 break; 2950 } 2951 2952 if (err) { 2953 kfree(nhi); 2954 kfree(nh); 2955 return ERR_PTR(err); 2956 } 2957 2958 /* add the entry to the device based hash */ 2959 if (!nhi->fdb_nh) 2960 nexthop_devhash_add(net, nhi); 2961 2962 rcu_assign_pointer(nh->nh_info, nhi); 2963 2964 return nh; 2965 } 2966 2967 /* called with rtnl lock held */ 2968 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 2969 struct netlink_ext_ack *extack) 2970 { 2971 struct nexthop *nh; 2972 int err; 2973 2974 if (!cfg->nh_id) { 2975 cfg->nh_id = nh_find_unused_id(net); 2976 if (!cfg->nh_id) { 2977 NL_SET_ERR_MSG(extack, "No unused id"); 2978 return ERR_PTR(-EINVAL); 2979 } 2980 } 2981 2982 if (cfg->nh_grp) 2983 nh = nexthop_create_group(net, cfg); 2984 else 2985 nh = nexthop_create(net, cfg, extack); 2986 2987 if (IS_ERR(nh)) 2988 return nh; 2989 2990 refcount_set(&nh->refcnt, 1); 2991 nh->id = cfg->nh_id; 2992 nh->protocol = cfg->nh_protocol; 2993 nh->net = net; 2994 2995 err = insert_nexthop(net, nh, cfg, extack); 2996 if (err) { 2997 __remove_nexthop(net, nh, NULL); 2998 nexthop_put(nh); 2999 nh = ERR_PTR(err); 3000 } 3001 3002 return nh; 3003 } 3004 3005 static int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback, 3006 unsigned long *timer_p, bool *has_p, 3007 struct netlink_ext_ack *extack) 3008 { 3009 unsigned long timer; 3010 u32 value; 3011 3012 if (!attr) { 3013 *timer_p = fallback; 3014 *has_p = false; 3015 return 0; 3016 } 3017 3018 value = nla_get_u32(attr); 3019 timer = clock_t_to_jiffies(value); 3020 if (timer == ~0UL) { 3021 NL_SET_ERR_MSG(extack, "Timer value too large"); 3022 return -EINVAL; 3023 } 3024 3025 *timer_p = timer; 3026 *has_p = true; 3027 return 0; 3028 } 3029 3030 static int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg, 3031 struct netlink_ext_ack *extack) 3032 { 3033 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {}; 3034 int err; 3035 3036 if (res) { 3037 err = nla_parse_nested(tb, 3038 ARRAY_SIZE(rtm_nh_res_policy_new) - 1, 3039 res, rtm_nh_res_policy_new, extack); 3040 if (err < 0) 3041 return err; 3042 } 3043 3044 if (tb[NHA_RES_GROUP_BUCKETS]) { 3045 cfg->nh_grp_res_num_buckets = 3046 nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]); 3047 cfg->nh_grp_res_has_num_buckets = true; 3048 if (!cfg->nh_grp_res_num_buckets) { 3049 NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0"); 3050 return -EINVAL; 3051 } 3052 } 3053 3054 err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER], 3055 NH_RES_DEFAULT_IDLE_TIMER, 3056 &cfg->nh_grp_res_idle_timer, 3057 &cfg->nh_grp_res_has_idle_timer, 3058 extack); 3059 if (err) 3060 return err; 3061 3062 return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER], 3063 NH_RES_DEFAULT_UNBALANCED_TIMER, 3064 &cfg->nh_grp_res_unbalanced_timer, 3065 &cfg->nh_grp_res_has_unbalanced_timer, 3066 extack); 3067 } 3068 3069 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 3070 struct nlmsghdr *nlh, struct nlattr **tb, 3071 struct nh_config *cfg, 3072 struct netlink_ext_ack *extack) 3073 { 3074 struct nhmsg *nhm = nlmsg_data(nlh); 3075 int err; 3076 3077 err = -EINVAL; 3078 if (nhm->resvd || nhm->nh_scope) { 3079 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 3080 goto out; 3081 } 3082 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 3083 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 3084 goto out; 3085 } 3086 3087 switch (nhm->nh_family) { 3088 case AF_INET: 3089 case AF_INET6: 3090 break; 3091 case AF_UNSPEC: 3092 if (tb[NHA_GROUP]) 3093 break; 3094 fallthrough; 3095 default: 3096 NL_SET_ERR_MSG(extack, "Invalid address family"); 3097 goto out; 3098 } 3099 3100 memset(cfg, 0, sizeof(*cfg)); 3101 cfg->nlflags = nlh->nlmsg_flags; 3102 cfg->nlinfo.portid = NETLINK_CB(skb).portid; 3103 cfg->nlinfo.nlh = nlh; 3104 cfg->nlinfo.nl_net = net; 3105 3106 cfg->nh_family = nhm->nh_family; 3107 cfg->nh_protocol = nhm->nh_protocol; 3108 cfg->nh_flags = nhm->nh_flags; 3109 3110 if (tb[NHA_ID]) 3111 cfg->nh_id = nla_get_u32(tb[NHA_ID]); 3112 3113 if (tb[NHA_FDB]) { 3114 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 3115 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 3116 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 3117 goto out; 3118 } 3119 if (nhm->nh_flags) { 3120 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 3121 goto out; 3122 } 3123 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 3124 } 3125 3126 if (tb[NHA_GROUP]) { 3127 if (nhm->nh_family != AF_UNSPEC) { 3128 NL_SET_ERR_MSG(extack, "Invalid family for group"); 3129 goto out; 3130 } 3131 cfg->nh_grp = tb[NHA_GROUP]; 3132 3133 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 3134 if (tb[NHA_GROUP_TYPE]) 3135 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 3136 3137 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 3138 NL_SET_ERR_MSG(extack, "Invalid group type"); 3139 goto out; 3140 } 3141 3142 err = nh_check_attr_group(net, tb, ARRAY_SIZE(rtm_nh_policy_new), 3143 cfg->nh_grp_type, extack); 3144 if (err) 3145 goto out; 3146 3147 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) 3148 err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP], 3149 cfg, extack); 3150 3151 if (tb[NHA_HW_STATS_ENABLE]) 3152 cfg->nh_hw_stats = nla_get_u32(tb[NHA_HW_STATS_ENABLE]); 3153 3154 /* no other attributes should be set */ 3155 goto out; 3156 } 3157 3158 if (tb[NHA_BLACKHOLE]) { 3159 if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 3160 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 3161 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 3162 goto out; 3163 } 3164 3165 cfg->nh_blackhole = 1; 3166 err = 0; 3167 goto out; 3168 } 3169 3170 if (!cfg->nh_fdb && !tb[NHA_OIF]) { 3171 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 3172 goto out; 3173 } 3174 3175 err = -EINVAL; 3176 if (tb[NHA_GATEWAY]) { 3177 struct nlattr *gwa = tb[NHA_GATEWAY]; 3178 3179 switch (cfg->nh_family) { 3180 case AF_INET: 3181 if (nla_len(gwa) != sizeof(u32)) { 3182 NL_SET_ERR_MSG(extack, "Invalid gateway"); 3183 goto out; 3184 } 3185 cfg->gw.ipv4 = nla_get_be32(gwa); 3186 break; 3187 case AF_INET6: 3188 if (nla_len(gwa) != sizeof(struct in6_addr)) { 3189 NL_SET_ERR_MSG(extack, "Invalid gateway"); 3190 goto out; 3191 } 3192 cfg->gw.ipv6 = nla_get_in6_addr(gwa); 3193 break; 3194 default: 3195 NL_SET_ERR_MSG(extack, 3196 "Unknown address family for gateway"); 3197 goto out; 3198 } 3199 } else { 3200 /* device only nexthop (no gateway) */ 3201 if (cfg->nh_flags & RTNH_F_ONLINK) { 3202 NL_SET_ERR_MSG(extack, 3203 "ONLINK flag can not be set for nexthop without a gateway"); 3204 goto out; 3205 } 3206 } 3207 3208 if (tb[NHA_ENCAP]) { 3209 cfg->nh_encap = tb[NHA_ENCAP]; 3210 3211 if (!tb[NHA_ENCAP_TYPE]) { 3212 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 3213 goto out; 3214 } 3215 3216 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 3217 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 3218 if (err < 0) 3219 goto out; 3220 3221 } else if (tb[NHA_ENCAP_TYPE]) { 3222 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 3223 goto out; 3224 } 3225 3226 if (tb[NHA_HW_STATS_ENABLE]) { 3227 NL_SET_ERR_MSG(extack, "Cannot enable nexthop hardware statistics for non-group nexthops"); 3228 goto out; 3229 } 3230 3231 err = 0; 3232 out: 3233 return err; 3234 } 3235 3236 static int rtm_to_nh_config_rtnl(struct net *net, struct nlattr **tb, 3237 struct nh_config *cfg, 3238 struct netlink_ext_ack *extack) 3239 { 3240 if (tb[NHA_GROUP]) 3241 return nh_check_attr_group_rtnl(net, tb, extack); 3242 3243 if (tb[NHA_OIF]) { 3244 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 3245 if (cfg->nh_ifindex) 3246 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 3247 3248 if (!cfg->dev) { 3249 NL_SET_ERR_MSG(extack, "Invalid device index"); 3250 return -EINVAL; 3251 } 3252 3253 if (!(cfg->dev->flags & IFF_UP)) { 3254 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 3255 return -ENETDOWN; 3256 } 3257 3258 if (!netif_carrier_ok(cfg->dev)) { 3259 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 3260 return -ENETDOWN; 3261 } 3262 } 3263 3264 return 0; 3265 } 3266 3267 /* rtnl */ 3268 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 3269 struct netlink_ext_ack *extack) 3270 { 3271 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)]; 3272 struct net *net = sock_net(skb->sk); 3273 struct nh_config cfg; 3274 struct nexthop *nh; 3275 int err; 3276 3277 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3278 ARRAY_SIZE(rtm_nh_policy_new) - 1, 3279 rtm_nh_policy_new, extack); 3280 if (err < 0) 3281 goto out; 3282 3283 err = rtm_to_nh_config(net, skb, nlh, tb, &cfg, extack); 3284 if (err) 3285 goto out; 3286 3287 if (cfg.nlflags & NLM_F_REPLACE && !cfg.nh_id) { 3288 NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 3289 err = -EINVAL; 3290 goto out; 3291 } 3292 3293 rtnl_net_lock(net); 3294 3295 err = rtm_to_nh_config_rtnl(net, tb, &cfg, extack); 3296 if (err) 3297 goto unlock; 3298 3299 nh = nexthop_add(net, &cfg, extack); 3300 if (IS_ERR(nh)) 3301 err = PTR_ERR(nh); 3302 3303 unlock: 3304 rtnl_net_unlock(net); 3305 out: 3306 return err; 3307 } 3308 3309 static int nh_valid_get_del_req(const struct nlmsghdr *nlh, 3310 struct nlattr **tb, u32 *id, u32 *op_flags, 3311 struct netlink_ext_ack *extack) 3312 { 3313 struct nhmsg *nhm = nlmsg_data(nlh); 3314 3315 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 3316 NL_SET_ERR_MSG(extack, "Invalid values in header"); 3317 return -EINVAL; 3318 } 3319 3320 if (!tb[NHA_ID]) { 3321 NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 3322 return -EINVAL; 3323 } 3324 3325 *id = nla_get_u32(tb[NHA_ID]); 3326 if (!(*id)) { 3327 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 3328 return -EINVAL; 3329 } 3330 3331 if (op_flags) 3332 *op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0); 3333 3334 return 0; 3335 } 3336 3337 /* rtnl */ 3338 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 3339 struct netlink_ext_ack *extack) 3340 { 3341 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_del)]; 3342 struct net *net = sock_net(skb->sk); 3343 struct nl_info nlinfo = { 3344 .nlh = nlh, 3345 .nl_net = net, 3346 .portid = NETLINK_CB(skb).portid, 3347 }; 3348 struct nexthop *nh; 3349 int err; 3350 u32 id; 3351 3352 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3353 ARRAY_SIZE(rtm_nh_policy_del) - 1, rtm_nh_policy_del, 3354 extack); 3355 if (err < 0) 3356 return err; 3357 3358 err = nh_valid_get_del_req(nlh, tb, &id, NULL, extack); 3359 if (err) 3360 return err; 3361 3362 rtnl_net_lock(net); 3363 3364 nh = nexthop_find_by_id(net, id); 3365 if (nh) 3366 remove_nexthop(net, nh, &nlinfo); 3367 else 3368 err = -ENOENT; 3369 3370 rtnl_net_unlock(net); 3371 3372 return err; 3373 } 3374 3375 /* rtnl */ 3376 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3377 struct netlink_ext_ack *extack) 3378 { 3379 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)]; 3380 struct net *net = sock_net(in_skb->sk); 3381 struct sk_buff *skb = NULL; 3382 struct nexthop *nh; 3383 u32 op_flags; 3384 int err; 3385 u32 id; 3386 3387 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3388 ARRAY_SIZE(rtm_nh_policy_get) - 1, rtm_nh_policy_get, 3389 extack); 3390 if (err < 0) 3391 return err; 3392 3393 err = nh_valid_get_del_req(nlh, tb, &id, &op_flags, extack); 3394 if (err) 3395 return err; 3396 3397 err = -ENOENT; 3398 nh = nexthop_find_by_id(net, id); 3399 if (!nh) 3400 goto out; 3401 3402 err = -ENOBUFS; 3403 skb = nlmsg_new(nh_nlmsg_size(nh, op_flags), GFP_KERNEL); 3404 if (!skb) 3405 goto out; 3406 3407 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 3408 nlh->nlmsg_seq, 0, op_flags); 3409 if (err < 0) { 3410 WARN_ON(err == -EMSGSIZE); 3411 goto errout_free; 3412 } 3413 3414 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3415 out: 3416 return err; 3417 errout_free: 3418 kfree_skb(skb); 3419 goto out; 3420 } 3421 3422 struct nh_dump_filter { 3423 u32 nh_id; 3424 int dev_idx; 3425 int master_idx; 3426 bool group_filter; 3427 bool fdb_filter; 3428 u32 res_bucket_nh_id; 3429 u32 op_flags; 3430 }; 3431 3432 static bool nh_dump_filtered(struct nexthop *nh, 3433 struct nh_dump_filter *filter, u8 family) 3434 { 3435 const struct net_device *dev; 3436 const struct nh_info *nhi; 3437 3438 if (filter->group_filter && !nh->is_group) 3439 return true; 3440 3441 if (!filter->dev_idx && !filter->master_idx && !family) 3442 return false; 3443 3444 if (nh->is_group) 3445 return true; 3446 3447 nhi = rtnl_dereference(nh->nh_info); 3448 if (family && nhi->family != family) 3449 return true; 3450 3451 dev = nhi->fib_nhc.nhc_dev; 3452 if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx)) 3453 return true; 3454 3455 if (filter->master_idx) { 3456 struct net_device *master; 3457 3458 if (!dev) 3459 return true; 3460 3461 master = netdev_master_upper_dev_get((struct net_device *)dev); 3462 if (!master || master->ifindex != filter->master_idx) 3463 return true; 3464 } 3465 3466 return false; 3467 } 3468 3469 static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb, 3470 struct nh_dump_filter *filter, 3471 struct netlink_ext_ack *extack) 3472 { 3473 struct nhmsg *nhm; 3474 u32 idx; 3475 3476 if (tb[NHA_OIF]) { 3477 idx = nla_get_u32(tb[NHA_OIF]); 3478 if (idx > INT_MAX) { 3479 NL_SET_ERR_MSG(extack, "Invalid device index"); 3480 return -EINVAL; 3481 } 3482 filter->dev_idx = idx; 3483 } 3484 if (tb[NHA_MASTER]) { 3485 idx = nla_get_u32(tb[NHA_MASTER]); 3486 if (idx > INT_MAX) { 3487 NL_SET_ERR_MSG(extack, "Invalid master device index"); 3488 return -EINVAL; 3489 } 3490 filter->master_idx = idx; 3491 } 3492 filter->group_filter = nla_get_flag(tb[NHA_GROUPS]); 3493 filter->fdb_filter = nla_get_flag(tb[NHA_FDB]); 3494 3495 nhm = nlmsg_data(nlh); 3496 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 3497 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 3498 return -EINVAL; 3499 } 3500 3501 return 0; 3502 } 3503 3504 static int nh_valid_dump_req(const struct nlmsghdr *nlh, 3505 struct nh_dump_filter *filter, 3506 struct netlink_callback *cb) 3507 { 3508 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)]; 3509 int err; 3510 3511 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3512 ARRAY_SIZE(rtm_nh_policy_dump) - 1, 3513 rtm_nh_policy_dump, cb->extack); 3514 if (err < 0) 3515 return err; 3516 3517 filter->op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0); 3518 3519 return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 3520 } 3521 3522 struct rtm_dump_nh_ctx { 3523 u32 idx; 3524 }; 3525 3526 static struct rtm_dump_nh_ctx * 3527 rtm_dump_nh_ctx(struct netlink_callback *cb) 3528 { 3529 struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx; 3530 3531 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 3532 return ctx; 3533 } 3534 3535 static int rtm_dump_walk_nexthops(struct sk_buff *skb, 3536 struct netlink_callback *cb, 3537 struct rb_root *root, 3538 struct rtm_dump_nh_ctx *ctx, 3539 int (*nh_cb)(struct sk_buff *skb, 3540 struct netlink_callback *cb, 3541 struct nexthop *nh, void *data), 3542 void *data) 3543 { 3544 struct rb_node *node; 3545 int s_idx; 3546 int err; 3547 3548 s_idx = ctx->idx; 3549 3550 /* If this is not the first invocation, ctx->idx will contain the id of 3551 * the last nexthop we processed. Instead of starting from the very 3552 * first element of the red/black tree again and linearly skipping the 3553 * (potentially large) set of nodes with an id smaller than s_idx, walk 3554 * the tree and find the left-most node whose id is >= s_idx. This 3555 * provides an efficient O(log n) starting point for the dump 3556 * continuation. 3557 */ 3558 if (s_idx != 0) { 3559 struct rb_node *tmp = root->rb_node; 3560 3561 node = NULL; 3562 while (tmp) { 3563 struct nexthop *nh; 3564 3565 nh = rb_entry(tmp, struct nexthop, rb_node); 3566 if (nh->id < s_idx) { 3567 tmp = tmp->rb_right; 3568 } else { 3569 /* Track current candidate and keep looking on 3570 * the left side to find the left-most 3571 * (smallest id) that is still >= s_idx. 3572 */ 3573 node = tmp; 3574 tmp = tmp->rb_left; 3575 } 3576 } 3577 } else { 3578 node = rb_first(root); 3579 } 3580 3581 for (; node; node = rb_next(node)) { 3582 struct nexthop *nh; 3583 3584 nh = rb_entry(node, struct nexthop, rb_node); 3585 3586 ctx->idx = nh->id; 3587 err = nh_cb(skb, cb, nh, data); 3588 if (err) 3589 return err; 3590 } 3591 3592 return 0; 3593 } 3594 3595 static int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb, 3596 struct nexthop *nh, void *data) 3597 { 3598 struct nhmsg *nhm = nlmsg_data(cb->nlh); 3599 struct nh_dump_filter *filter = data; 3600 3601 if (nh_dump_filtered(nh, filter, nhm->nh_family)) 3602 return 0; 3603 3604 return nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 3605 NETLINK_CB(cb->skb).portid, 3606 cb->nlh->nlmsg_seq, NLM_F_MULTI, filter->op_flags); 3607 } 3608 3609 /* rtnl */ 3610 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 3611 { 3612 struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb); 3613 struct net *net = sock_net(skb->sk); 3614 struct rb_root *root = &net->nexthop.rb_root; 3615 struct nh_dump_filter filter = {}; 3616 int err; 3617 3618 err = nh_valid_dump_req(cb->nlh, &filter, cb); 3619 if (err < 0) 3620 return err; 3621 3622 err = rtm_dump_walk_nexthops(skb, cb, root, ctx, 3623 &rtm_dump_nexthop_cb, &filter); 3624 3625 cb->seq = net->nexthop.seq; 3626 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 3627 return err; 3628 } 3629 3630 static struct nexthop * 3631 nexthop_find_group_resilient(struct net *net, u32 id, 3632 struct netlink_ext_ack *extack) 3633 { 3634 struct nh_group *nhg; 3635 struct nexthop *nh; 3636 3637 nh = nexthop_find_by_id(net, id); 3638 if (!nh) 3639 return ERR_PTR(-ENOENT); 3640 3641 if (!nh->is_group) { 3642 NL_SET_ERR_MSG(extack, "Not a nexthop group"); 3643 return ERR_PTR(-EINVAL); 3644 } 3645 3646 nhg = rtnl_dereference(nh->nh_grp); 3647 if (!nhg->resilient) { 3648 NL_SET_ERR_MSG(extack, "Nexthop group not of type resilient"); 3649 return ERR_PTR(-EINVAL); 3650 } 3651 3652 return nh; 3653 } 3654 3655 static int nh_valid_dump_nhid(struct nlattr *attr, u32 *nh_id_p, 3656 struct netlink_ext_ack *extack) 3657 { 3658 u32 idx; 3659 3660 if (attr) { 3661 idx = nla_get_u32(attr); 3662 if (!idx) { 3663 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 3664 return -EINVAL; 3665 } 3666 *nh_id_p = idx; 3667 } else { 3668 *nh_id_p = 0; 3669 } 3670 3671 return 0; 3672 } 3673 3674 static int nh_valid_dump_bucket_req(const struct nlmsghdr *nlh, 3675 struct nh_dump_filter *filter, 3676 struct netlink_callback *cb) 3677 { 3678 struct nlattr *res_tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_dump)]; 3679 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump_bucket)]; 3680 int err; 3681 3682 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3683 ARRAY_SIZE(rtm_nh_policy_dump_bucket) - 1, 3684 rtm_nh_policy_dump_bucket, NULL); 3685 if (err < 0) 3686 return err; 3687 3688 err = nh_valid_dump_nhid(tb[NHA_ID], &filter->nh_id, cb->extack); 3689 if (err) 3690 return err; 3691 3692 if (tb[NHA_RES_BUCKET]) { 3693 size_t max = ARRAY_SIZE(rtm_nh_res_bucket_policy_dump) - 1; 3694 3695 err = nla_parse_nested(res_tb, max, 3696 tb[NHA_RES_BUCKET], 3697 rtm_nh_res_bucket_policy_dump, 3698 cb->extack); 3699 if (err < 0) 3700 return err; 3701 3702 err = nh_valid_dump_nhid(res_tb[NHA_RES_BUCKET_NH_ID], 3703 &filter->res_bucket_nh_id, 3704 cb->extack); 3705 if (err) 3706 return err; 3707 } 3708 3709 return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 3710 } 3711 3712 struct rtm_dump_res_bucket_ctx { 3713 struct rtm_dump_nh_ctx nh; 3714 u16 bucket_index; 3715 }; 3716 3717 static struct rtm_dump_res_bucket_ctx * 3718 rtm_dump_res_bucket_ctx(struct netlink_callback *cb) 3719 { 3720 struct rtm_dump_res_bucket_ctx *ctx = (void *)cb->ctx; 3721 3722 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 3723 return ctx; 3724 } 3725 3726 struct rtm_dump_nexthop_bucket_data { 3727 struct rtm_dump_res_bucket_ctx *ctx; 3728 struct nh_dump_filter filter; 3729 }; 3730 3731 static int rtm_dump_nexthop_bucket_nh(struct sk_buff *skb, 3732 struct netlink_callback *cb, 3733 struct nexthop *nh, 3734 struct rtm_dump_nexthop_bucket_data *dd) 3735 { 3736 u32 portid = NETLINK_CB(cb->skb).portid; 3737 struct nhmsg *nhm = nlmsg_data(cb->nlh); 3738 struct nh_res_table *res_table; 3739 struct nh_group *nhg; 3740 u16 bucket_index; 3741 int err; 3742 3743 nhg = rtnl_dereference(nh->nh_grp); 3744 res_table = rtnl_dereference(nhg->res_table); 3745 for (bucket_index = dd->ctx->bucket_index; 3746 bucket_index < res_table->num_nh_buckets; 3747 bucket_index++) { 3748 struct nh_res_bucket *bucket; 3749 struct nh_grp_entry *nhge; 3750 3751 bucket = &res_table->nh_buckets[bucket_index]; 3752 nhge = rtnl_dereference(bucket->nh_entry); 3753 if (nh_dump_filtered(nhge->nh, &dd->filter, nhm->nh_family)) 3754 continue; 3755 3756 if (dd->filter.res_bucket_nh_id && 3757 dd->filter.res_bucket_nh_id != nhge->nh->id) 3758 continue; 3759 3760 dd->ctx->bucket_index = bucket_index; 3761 err = nh_fill_res_bucket(skb, nh, bucket, bucket_index, 3762 RTM_NEWNEXTHOPBUCKET, portid, 3763 cb->nlh->nlmsg_seq, NLM_F_MULTI, 3764 cb->extack); 3765 if (err) 3766 return err; 3767 } 3768 3769 dd->ctx->bucket_index = 0; 3770 3771 return 0; 3772 } 3773 3774 static int rtm_dump_nexthop_bucket_cb(struct sk_buff *skb, 3775 struct netlink_callback *cb, 3776 struct nexthop *nh, void *data) 3777 { 3778 struct rtm_dump_nexthop_bucket_data *dd = data; 3779 struct nh_group *nhg; 3780 3781 if (!nh->is_group) 3782 return 0; 3783 3784 nhg = rtnl_dereference(nh->nh_grp); 3785 if (!nhg->resilient) 3786 return 0; 3787 3788 return rtm_dump_nexthop_bucket_nh(skb, cb, nh, dd); 3789 } 3790 3791 /* rtnl */ 3792 static int rtm_dump_nexthop_bucket(struct sk_buff *skb, 3793 struct netlink_callback *cb) 3794 { 3795 struct rtm_dump_res_bucket_ctx *ctx = rtm_dump_res_bucket_ctx(cb); 3796 struct rtm_dump_nexthop_bucket_data dd = { .ctx = ctx }; 3797 struct net *net = sock_net(skb->sk); 3798 struct nexthop *nh; 3799 int err; 3800 3801 err = nh_valid_dump_bucket_req(cb->nlh, &dd.filter, cb); 3802 if (err) 3803 return err; 3804 3805 if (dd.filter.nh_id) { 3806 nh = nexthop_find_group_resilient(net, dd.filter.nh_id, 3807 cb->extack); 3808 if (IS_ERR(nh)) 3809 return PTR_ERR(nh); 3810 err = rtm_dump_nexthop_bucket_nh(skb, cb, nh, &dd); 3811 } else { 3812 struct rb_root *root = &net->nexthop.rb_root; 3813 3814 err = rtm_dump_walk_nexthops(skb, cb, root, &ctx->nh, 3815 &rtm_dump_nexthop_bucket_cb, &dd); 3816 } 3817 3818 cb->seq = net->nexthop.seq; 3819 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 3820 return err; 3821 } 3822 3823 static int nh_valid_get_bucket_req_res_bucket(struct nlattr *res, 3824 u16 *bucket_index, 3825 struct netlink_ext_ack *extack) 3826 { 3827 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_get)]; 3828 int err; 3829 3830 err = nla_parse_nested(tb, ARRAY_SIZE(rtm_nh_res_bucket_policy_get) - 1, 3831 res, rtm_nh_res_bucket_policy_get, extack); 3832 if (err < 0) 3833 return err; 3834 3835 if (!tb[NHA_RES_BUCKET_INDEX]) { 3836 NL_SET_ERR_MSG(extack, "Bucket index is missing"); 3837 return -EINVAL; 3838 } 3839 3840 *bucket_index = nla_get_u16(tb[NHA_RES_BUCKET_INDEX]); 3841 return 0; 3842 } 3843 3844 static int nh_valid_get_bucket_req(const struct nlmsghdr *nlh, 3845 u32 *id, u16 *bucket_index, 3846 struct netlink_ext_ack *extack) 3847 { 3848 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get_bucket)]; 3849 int err; 3850 3851 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3852 ARRAY_SIZE(rtm_nh_policy_get_bucket) - 1, 3853 rtm_nh_policy_get_bucket, extack); 3854 if (err < 0) 3855 return err; 3856 3857 err = nh_valid_get_del_req(nlh, tb, id, NULL, extack); 3858 if (err) 3859 return err; 3860 3861 if (!tb[NHA_RES_BUCKET]) { 3862 NL_SET_ERR_MSG(extack, "Bucket information is missing"); 3863 return -EINVAL; 3864 } 3865 3866 err = nh_valid_get_bucket_req_res_bucket(tb[NHA_RES_BUCKET], 3867 bucket_index, extack); 3868 if (err) 3869 return err; 3870 3871 return 0; 3872 } 3873 3874 /* rtnl */ 3875 static int rtm_get_nexthop_bucket(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3876 struct netlink_ext_ack *extack) 3877 { 3878 struct net *net = sock_net(in_skb->sk); 3879 struct nh_res_table *res_table; 3880 struct sk_buff *skb = NULL; 3881 struct nh_group *nhg; 3882 struct nexthop *nh; 3883 u16 bucket_index; 3884 int err; 3885 u32 id; 3886 3887 err = nh_valid_get_bucket_req(nlh, &id, &bucket_index, extack); 3888 if (err) 3889 return err; 3890 3891 nh = nexthop_find_group_resilient(net, id, extack); 3892 if (IS_ERR(nh)) 3893 return PTR_ERR(nh); 3894 3895 nhg = rtnl_dereference(nh->nh_grp); 3896 res_table = rtnl_dereference(nhg->res_table); 3897 if (bucket_index >= res_table->num_nh_buckets) { 3898 NL_SET_ERR_MSG(extack, "Bucket index out of bounds"); 3899 return -ENOENT; 3900 } 3901 3902 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 3903 if (!skb) 3904 return -ENOBUFS; 3905 3906 err = nh_fill_res_bucket(skb, nh, &res_table->nh_buckets[bucket_index], 3907 bucket_index, RTM_NEWNEXTHOPBUCKET, 3908 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 3909 0, extack); 3910 if (err < 0) { 3911 WARN_ON(err == -EMSGSIZE); 3912 goto errout_free; 3913 } 3914 3915 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3916 3917 errout_free: 3918 kfree_skb(skb); 3919 return err; 3920 } 3921 3922 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 3923 { 3924 unsigned int hash = nh_dev_hashfn(dev->ifindex); 3925 struct net *net = dev_net(dev); 3926 struct hlist_head *head = &net->nexthop.devhash[hash]; 3927 struct hlist_node *n; 3928 struct nh_info *nhi; 3929 3930 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 3931 if (nhi->fib_nhc.nhc_dev == dev) { 3932 if (nhi->family == AF_INET) 3933 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 3934 orig_mtu); 3935 } 3936 } 3937 } 3938 3939 /* rtnl */ 3940 static int nh_netdev_event(struct notifier_block *this, 3941 unsigned long event, void *ptr) 3942 { 3943 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3944 struct netdev_notifier_info_ext *info_ext; 3945 3946 switch (event) { 3947 case NETDEV_DOWN: 3948 case NETDEV_UNREGISTER: 3949 nexthop_flush_dev(dev, event); 3950 break; 3951 case NETDEV_CHANGE: 3952 if (!(netif_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 3953 nexthop_flush_dev(dev, event); 3954 break; 3955 case NETDEV_CHANGEMTU: 3956 info_ext = ptr; 3957 nexthop_sync_mtu(dev, info_ext->ext.mtu); 3958 rt_cache_flush(dev_net(dev)); 3959 break; 3960 } 3961 return NOTIFY_DONE; 3962 } 3963 3964 static struct notifier_block nh_netdev_notifier = { 3965 .notifier_call = nh_netdev_event, 3966 }; 3967 3968 static int nexthops_dump(struct net *net, struct notifier_block *nb, 3969 enum nexthop_event_type event_type, 3970 struct netlink_ext_ack *extack) 3971 { 3972 struct rb_root *root = &net->nexthop.rb_root; 3973 struct rb_node *node; 3974 int err = 0; 3975 3976 for (node = rb_first(root); node; node = rb_next(node)) { 3977 struct nexthop *nh; 3978 3979 nh = rb_entry(node, struct nexthop, rb_node); 3980 err = call_nexthop_notifier(nb, net, event_type, nh, extack); 3981 if (err) 3982 break; 3983 } 3984 3985 return err; 3986 } 3987 3988 int register_nexthop_notifier(struct net *net, struct notifier_block *nb, 3989 struct netlink_ext_ack *extack) 3990 { 3991 int err; 3992 3993 rtnl_lock(); 3994 err = nexthops_dump(net, nb, NEXTHOP_EVENT_REPLACE, extack); 3995 if (err) 3996 goto unlock; 3997 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain, 3998 nb); 3999 unlock: 4000 rtnl_unlock(); 4001 return err; 4002 } 4003 EXPORT_SYMBOL(register_nexthop_notifier); 4004 4005 int __unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 4006 { 4007 int err; 4008 4009 err = blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 4010 nb); 4011 if (!err) 4012 nexthops_dump(net, nb, NEXTHOP_EVENT_DEL, NULL); 4013 return err; 4014 } 4015 EXPORT_SYMBOL(__unregister_nexthop_notifier); 4016 4017 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 4018 { 4019 int err; 4020 4021 rtnl_lock(); 4022 err = __unregister_nexthop_notifier(net, nb); 4023 rtnl_unlock(); 4024 return err; 4025 } 4026 EXPORT_SYMBOL(unregister_nexthop_notifier); 4027 4028 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 4029 { 4030 struct nexthop *nexthop; 4031 4032 rcu_read_lock(); 4033 4034 nexthop = nexthop_find_by_id(net, id); 4035 if (!nexthop) 4036 goto out; 4037 4038 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 4039 if (offload) 4040 nexthop->nh_flags |= RTNH_F_OFFLOAD; 4041 if (trap) 4042 nexthop->nh_flags |= RTNH_F_TRAP; 4043 4044 out: 4045 rcu_read_unlock(); 4046 } 4047 EXPORT_SYMBOL(nexthop_set_hw_flags); 4048 4049 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index, 4050 bool offload, bool trap) 4051 { 4052 struct nh_res_table *res_table; 4053 struct nh_res_bucket *bucket; 4054 struct nexthop *nexthop; 4055 struct nh_group *nhg; 4056 4057 rcu_read_lock(); 4058 4059 nexthop = nexthop_find_by_id(net, id); 4060 if (!nexthop || !nexthop->is_group) 4061 goto out; 4062 4063 nhg = rcu_dereference(nexthop->nh_grp); 4064 if (!nhg->resilient) 4065 goto out; 4066 4067 if (bucket_index >= nhg->res_table->num_nh_buckets) 4068 goto out; 4069 4070 res_table = rcu_dereference(nhg->res_table); 4071 bucket = &res_table->nh_buckets[bucket_index]; 4072 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 4073 if (offload) 4074 bucket->nh_flags |= RTNH_F_OFFLOAD; 4075 if (trap) 4076 bucket->nh_flags |= RTNH_F_TRAP; 4077 4078 out: 4079 rcu_read_unlock(); 4080 } 4081 EXPORT_SYMBOL(nexthop_bucket_set_hw_flags); 4082 4083 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets, 4084 unsigned long *activity) 4085 { 4086 struct nh_res_table *res_table; 4087 struct nexthop *nexthop; 4088 struct nh_group *nhg; 4089 u16 i; 4090 4091 rcu_read_lock(); 4092 4093 nexthop = nexthop_find_by_id(net, id); 4094 if (!nexthop || !nexthop->is_group) 4095 goto out; 4096 4097 nhg = rcu_dereference(nexthop->nh_grp); 4098 if (!nhg->resilient) 4099 goto out; 4100 4101 /* Instead of silently ignoring some buckets, demand that the sizes 4102 * be the same. 4103 */ 4104 res_table = rcu_dereference(nhg->res_table); 4105 if (num_buckets != res_table->num_nh_buckets) 4106 goto out; 4107 4108 for (i = 0; i < num_buckets; i++) { 4109 if (test_bit(i, activity)) 4110 nh_res_bucket_set_busy(&res_table->nh_buckets[i]); 4111 } 4112 4113 out: 4114 rcu_read_unlock(); 4115 } 4116 EXPORT_SYMBOL(nexthop_res_grp_activity_update); 4117 4118 static void __net_exit nexthop_net_exit_rtnl(struct net *net, 4119 struct list_head *dev_to_kill) 4120 { 4121 ASSERT_RTNL_NET(net); 4122 flush_all_nexthops(net); 4123 } 4124 4125 static void __net_exit nexthop_net_exit(struct net *net) 4126 { 4127 kfree(net->nexthop.devhash); 4128 net->nexthop.devhash = NULL; 4129 } 4130 4131 static int __net_init nexthop_net_init(struct net *net) 4132 { 4133 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 4134 4135 net->nexthop.rb_root = RB_ROOT; 4136 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 4137 if (!net->nexthop.devhash) 4138 return -ENOMEM; 4139 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 4140 4141 return 0; 4142 } 4143 4144 static struct pernet_operations nexthop_net_ops = { 4145 .init = nexthop_net_init, 4146 .exit = nexthop_net_exit, 4147 .exit_rtnl = nexthop_net_exit_rtnl, 4148 }; 4149 4150 static const struct rtnl_msg_handler nexthop_rtnl_msg_handlers[] __initconst = { 4151 {.msgtype = RTM_NEWNEXTHOP, .doit = rtm_new_nexthop, 4152 .flags = RTNL_FLAG_DOIT_PERNET}, 4153 {.msgtype = RTM_DELNEXTHOP, .doit = rtm_del_nexthop, 4154 .flags = RTNL_FLAG_DOIT_PERNET}, 4155 {.msgtype = RTM_GETNEXTHOP, .doit = rtm_get_nexthop, 4156 .dumpit = rtm_dump_nexthop}, 4157 {.msgtype = RTM_GETNEXTHOPBUCKET, .doit = rtm_get_nexthop_bucket, 4158 .dumpit = rtm_dump_nexthop_bucket}, 4159 {.protocol = PF_INET, .msgtype = RTM_NEWNEXTHOP, 4160 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET}, 4161 {.protocol = PF_INET, .msgtype = RTM_GETNEXTHOP, 4162 .dumpit = rtm_dump_nexthop}, 4163 {.protocol = PF_INET6, .msgtype = RTM_NEWNEXTHOP, 4164 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET}, 4165 {.protocol = PF_INET6, .msgtype = RTM_GETNEXTHOP, 4166 .dumpit = rtm_dump_nexthop}, 4167 }; 4168 4169 static int __init nexthop_init(void) 4170 { 4171 register_pernet_subsys(&nexthop_net_ops); 4172 4173 register_netdevice_notifier(&nh_netdev_notifier); 4174 4175 rtnl_register_many(nexthop_rtnl_msg_handlers); 4176 4177 return 0; 4178 } 4179 subsys_initcall(nexthop_init); 4180