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 bool __must_check 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 bool __must_check 2020 remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, 2021 struct nl_info *nlinfo, 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 return remove_nexthop(net, nhp, nlinfo); 2038 2039 newg->has_v4 = false; 2040 newg->is_multipath = nhg->is_multipath; 2041 newg->hash_threshold = nhg->hash_threshold; 2042 newg->resilient = nhg->resilient; 2043 newg->fdb_nh = nhg->fdb_nh; 2044 newg->num_nh = nhg->num_nh; 2045 2046 /* copy old entries to new except the one getting removed */ 2047 nhges = nhg->nh_entries; 2048 new_nhges = newg->nh_entries; 2049 for (i = 0, j = 0; i < nhg->num_nh; ++i) { 2050 struct nh_info *nhi; 2051 2052 /* current nexthop getting removed */ 2053 if (nhg->nh_entries[i].nh == nh) { 2054 newg->num_nh--; 2055 continue; 2056 } 2057 2058 nhi = rtnl_dereference(nhges[i].nh->nh_info); 2059 if (nhi->family == AF_INET) 2060 newg->has_v4 = true; 2061 2062 list_del(&nhges[i].nh_list); 2063 new_nhges[j].stats = nhges[i].stats; 2064 new_nhges[j].nh_parent = nhges[i].nh_parent; 2065 new_nhges[j].nh = nhges[i].nh; 2066 new_nhges[j].weight = nhges[i].weight; 2067 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); 2068 j++; 2069 } 2070 2071 if (newg->hash_threshold) 2072 nh_hthr_group_rebalance(newg); 2073 else if (newg->resilient) 2074 replace_nexthop_grp_res(nhg, newg); 2075 2076 rcu_assign_pointer(nhp->nh_grp, newg); 2077 2078 list_del(&nhge->nh_list); 2079 nexthop_put(nhge->nh); 2080 list_add(&nhge->nh_list, deferred_free); 2081 2082 /* Removal of a NH from a resilient group is notified through 2083 * bucket notifications. 2084 */ 2085 if (newg->hash_threshold) { 2086 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, 2087 &extack); 2088 if (err) 2089 pr_err("%s\n", extack._msg); 2090 } 2091 2092 if (nlinfo) 2093 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); 2094 2095 return false; 2096 } 2097 2098 static bool __must_check 2099 remove_nexthop_from_groups(struct net *net, struct nexthop *nh, 2100 struct nl_info *nlinfo) 2101 { 2102 struct nh_grp_entry *nhge, *tmp; 2103 LIST_HEAD(deferred_free); 2104 bool need_flush = false; 2105 2106 /* If there is nothing to do, let's avoid the costly call to 2107 * synchronize_net() 2108 */ 2109 if (list_empty(&nh->grp_list)) 2110 return false; 2111 2112 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) 2113 need_flush |= remove_nh_grp_entry(net, nhge, nlinfo, 2114 &deferred_free); 2115 2116 /* make sure all see the newly published array before releasing rtnl */ 2117 synchronize_net(); 2118 2119 /* Now safe to free percpu stats — all RCU readers have finished */ 2120 list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) { 2121 list_del(&nhge->nh_list); 2122 free_percpu(nhge->stats); 2123 } 2124 2125 return need_flush; 2126 } 2127 2128 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) 2129 { 2130 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 2131 struct nh_res_table *res_table; 2132 int i, num_nh = nhg->num_nh; 2133 2134 for (i = 0; i < num_nh; ++i) { 2135 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 2136 2137 if (WARN_ON(!nhge->nh)) 2138 continue; 2139 2140 list_del_init(&nhge->nh_list); 2141 } 2142 2143 if (nhg->resilient) { 2144 res_table = rtnl_dereference(nhg->res_table); 2145 nh_res_table_cancel_upkeep(res_table); 2146 } 2147 } 2148 2149 /* not called for nexthop replace */ 2150 static bool __must_check __remove_nexthop_fib(struct net *net, 2151 struct nexthop *nh) 2152 { 2153 bool need_flush = !list_empty(&nh->fi_list); 2154 struct fib6_info *f6i; 2155 struct fib_info *fi; 2156 2157 list_for_each_entry(fi, &nh->fi_list, nh_list) 2158 fi->fib_flags |= RTNH_F_DEAD; 2159 2160 spin_lock_bh(&nh->lock); 2161 2162 nh->dead = true; 2163 2164 while (!list_empty(&nh->f6i_list)) { 2165 f6i = list_first_entry(&nh->f6i_list, typeof(*f6i), nh_list); 2166 2167 /* __ip6_del_rt does a release, so do a hold here */ 2168 fib6_info_hold(f6i); 2169 2170 spin_unlock_bh(&nh->lock); 2171 ip6_del_rt(net, f6i, 2172 !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)); 2173 2174 spin_lock_bh(&nh->lock); 2175 } 2176 2177 spin_unlock_bh(&nh->lock); 2178 2179 return need_flush; 2180 } 2181 2182 static bool __must_check __remove_nexthop(struct net *net, struct nexthop *nh, 2183 struct nl_info *nlinfo) 2184 { 2185 bool need_flush = __remove_nexthop_fib(net, nh); 2186 2187 if (nh->is_group) { 2188 remove_nexthop_group(nh, nlinfo); 2189 } else { 2190 struct nh_info *nhi; 2191 2192 nhi = rtnl_dereference(nh->nh_info); 2193 if (nhi->fib_nhc.nhc_dev) 2194 hlist_del(&nhi->dev_hash); 2195 2196 need_flush |= remove_nexthop_from_groups(net, nh, nlinfo); 2197 } 2198 2199 return need_flush; 2200 } 2201 2202 static bool __must_check remove_nexthop(struct net *net, struct nexthop *nh, 2203 struct nl_info *nlinfo) 2204 { 2205 bool need_flush; 2206 2207 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL); 2208 2209 /* remove from the tree */ 2210 rb_erase(&nh->rb_node, &net->nexthop.rb_root); 2211 2212 if (nlinfo) 2213 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); 2214 2215 need_flush = __remove_nexthop(net, nh, nlinfo); 2216 nh_base_seq_inc(net); 2217 2218 nexthop_put(nh); 2219 2220 return need_flush; 2221 } 2222 2223 static void remove_one_nexthop(struct net *net, struct nexthop *nh, 2224 struct nl_info *nlinfo) 2225 { 2226 if (remove_nexthop(net, nh, nlinfo)) 2227 fib_flush(net); 2228 } 2229 2230 /* if any FIB entries reference this nexthop, any dst entries 2231 * need to be regenerated 2232 */ 2233 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh, 2234 struct nexthop *replaced_nh) 2235 { 2236 struct fib6_info *f6i; 2237 struct nh_group *nhg; 2238 int i; 2239 2240 if (!list_empty(&nh->fi_list)) 2241 rt_cache_flush(net); 2242 2243 list_for_each_entry(f6i, &nh->f6i_list, nh_list) { 2244 spin_lock_bh(&f6i->fib6_table->tb6_lock); 2245 fib6_update_sernum_upto_root(net, f6i); 2246 spin_unlock_bh(&f6i->fib6_table->tb6_lock); 2247 } 2248 2249 /* if an IPv6 group was replaced, we have to release all old 2250 * dsts to make sure all refcounts are released 2251 */ 2252 if (!replaced_nh->is_group) 2253 return; 2254 2255 nhg = rtnl_dereference(replaced_nh->nh_grp); 2256 for (i = 0; i < nhg->num_nh; i++) { 2257 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 2258 struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info); 2259 2260 if (nhi->family == AF_INET6) 2261 fib6_nh_release_dsts(&nhi->fib6_nh); 2262 } 2263 } 2264 2265 static int replace_nexthop_grp(struct net *net, struct nexthop *old, 2266 struct nexthop *new, const struct nh_config *cfg, 2267 struct netlink_ext_ack *extack) 2268 { 2269 struct nh_res_table *tmp_table = NULL; 2270 struct nh_res_table *new_res_table; 2271 struct nh_res_table *old_res_table; 2272 struct nh_group *oldg, *newg; 2273 int i, err; 2274 2275 if (!new->is_group) { 2276 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); 2277 return -EINVAL; 2278 } 2279 2280 oldg = rtnl_dereference(old->nh_grp); 2281 newg = rtnl_dereference(new->nh_grp); 2282 2283 if (newg->hash_threshold != oldg->hash_threshold) { 2284 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type."); 2285 return -EINVAL; 2286 } 2287 2288 if (newg->hash_threshold) { 2289 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, 2290 extack); 2291 if (err) 2292 return err; 2293 } else if (newg->resilient) { 2294 new_res_table = rtnl_dereference(newg->res_table); 2295 old_res_table = rtnl_dereference(oldg->res_table); 2296 2297 /* Accept if num_nh_buckets was not given, but if it was 2298 * given, demand that the value be correct. 2299 */ 2300 if (cfg->nh_grp_res_has_num_buckets && 2301 cfg->nh_grp_res_num_buckets != 2302 old_res_table->num_nh_buckets) { 2303 NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group."); 2304 return -EINVAL; 2305 } 2306 2307 /* Emit a pre-replace notification so that listeners could veto 2308 * a potentially unsupported configuration. Otherwise, 2309 * individual bucket replacement notifications would need to be 2310 * vetoed, which is something that should only happen if the 2311 * bucket is currently active. 2312 */ 2313 err = call_nexthop_res_table_notifiers(net, new, extack); 2314 if (err) 2315 return err; 2316 2317 if (cfg->nh_grp_res_has_idle_timer) 2318 old_res_table->idle_timer = cfg->nh_grp_res_idle_timer; 2319 if (cfg->nh_grp_res_has_unbalanced_timer) 2320 old_res_table->unbalanced_timer = 2321 cfg->nh_grp_res_unbalanced_timer; 2322 2323 replace_nexthop_grp_res(oldg, newg); 2324 2325 tmp_table = new_res_table; 2326 rcu_assign_pointer(newg->res_table, old_res_table); 2327 rcu_assign_pointer(newg->spare->res_table, old_res_table); 2328 } 2329 2330 /* update parents - used by nexthop code for cleanup */ 2331 for (i = 0; i < newg->num_nh; i++) 2332 newg->nh_entries[i].nh_parent = old; 2333 2334 rcu_assign_pointer(old->nh_grp, newg); 2335 2336 /* Make sure concurrent readers are not using 'oldg' anymore. */ 2337 synchronize_net(); 2338 2339 if (newg->resilient) { 2340 rcu_assign_pointer(oldg->res_table, tmp_table); 2341 rcu_assign_pointer(oldg->spare->res_table, tmp_table); 2342 } 2343 2344 for (i = 0; i < oldg->num_nh; i++) 2345 oldg->nh_entries[i].nh_parent = new; 2346 2347 rcu_assign_pointer(new->nh_grp, oldg); 2348 2349 return 0; 2350 } 2351 2352 static void nh_group_v4_update(struct nh_group *nhg) 2353 { 2354 struct nh_grp_entry *nhges; 2355 bool has_v4 = false; 2356 int i; 2357 2358 nhges = nhg->nh_entries; 2359 for (i = 0; i < nhg->num_nh; i++) { 2360 struct nh_info *nhi; 2361 2362 nhi = rtnl_dereference(nhges[i].nh->nh_info); 2363 if (nhi->family == AF_INET) 2364 has_v4 = true; 2365 } 2366 nhg->has_v4 = has_v4; 2367 } 2368 2369 static int replace_nexthop_single_notify_res(struct net *net, 2370 struct nh_res_table *res_table, 2371 struct nexthop *old, 2372 struct nh_info *oldi, 2373 struct nh_info *newi, 2374 struct netlink_ext_ack *extack) 2375 { 2376 u32 nhg_id = res_table->nhg_id; 2377 int err; 2378 u16 i; 2379 2380 for (i = 0; i < res_table->num_nh_buckets; i++) { 2381 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 2382 struct nh_grp_entry *nhge; 2383 2384 nhge = rtnl_dereference(bucket->nh_entry); 2385 if (nhge->nh == old) { 2386 err = __call_nexthop_res_bucket_notifiers(net, nhg_id, 2387 i, true, 2388 oldi, newi, 2389 extack); 2390 if (err) 2391 goto err_notify; 2392 } 2393 } 2394 2395 return 0; 2396 2397 err_notify: 2398 while (i-- > 0) { 2399 struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 2400 struct nh_grp_entry *nhge; 2401 2402 nhge = rtnl_dereference(bucket->nh_entry); 2403 if (nhge->nh == old) 2404 __call_nexthop_res_bucket_notifiers(net, nhg_id, i, 2405 true, newi, oldi, 2406 extack); 2407 } 2408 return err; 2409 } 2410 2411 static int replace_nexthop_single_notify(struct net *net, 2412 struct nexthop *group_nh, 2413 struct nexthop *old, 2414 struct nh_info *oldi, 2415 struct nh_info *newi, 2416 struct netlink_ext_ack *extack) 2417 { 2418 struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp); 2419 struct nh_res_table *res_table; 2420 2421 if (nhg->hash_threshold) { 2422 return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, 2423 group_nh, extack); 2424 } else if (nhg->resilient) { 2425 res_table = rtnl_dereference(nhg->res_table); 2426 return replace_nexthop_single_notify_res(net, res_table, 2427 old, oldi, newi, 2428 extack); 2429 } 2430 2431 return -EINVAL; 2432 } 2433 2434 static int replace_nexthop_single(struct net *net, struct nexthop *old, 2435 struct nexthop *new, 2436 struct netlink_ext_ack *extack) 2437 { 2438 u8 old_protocol, old_nh_flags; 2439 struct nh_info *oldi, *newi; 2440 struct nh_grp_entry *nhge; 2441 int err; 2442 2443 if (new->is_group) { 2444 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); 2445 return -EINVAL; 2446 } 2447 2448 if (!list_empty(&old->grp_list) && 2449 rtnl_dereference(new->nh_info)->fdb_nh != 2450 rtnl_dereference(old->nh_info)->fdb_nh) { 2451 NL_SET_ERR_MSG(extack, "Cannot change nexthop FDB status while in a group"); 2452 return -EINVAL; 2453 } 2454 2455 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack); 2456 if (err) 2457 return err; 2458 2459 /* Hardware flags were set on 'old' as 'new' is not in the red-black 2460 * tree. Therefore, inherit the flags from 'old' to 'new'. 2461 */ 2462 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP); 2463 2464 oldi = rtnl_dereference(old->nh_info); 2465 newi = rtnl_dereference(new->nh_info); 2466 2467 newi->nh_parent = old; 2468 oldi->nh_parent = new; 2469 2470 old_protocol = old->protocol; 2471 old_nh_flags = old->nh_flags; 2472 2473 old->protocol = new->protocol; 2474 old->nh_flags = new->nh_flags; 2475 2476 rcu_assign_pointer(old->nh_info, newi); 2477 rcu_assign_pointer(new->nh_info, oldi); 2478 2479 /* Send a replace notification for all the groups using the nexthop. */ 2480 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2481 struct nexthop *nhp = nhge->nh_parent; 2482 2483 err = replace_nexthop_single_notify(net, nhp, old, oldi, newi, 2484 extack); 2485 if (err) 2486 goto err_notify; 2487 } 2488 2489 /* When replacing a nexthop with one of a different family, potentially 2490 * update IPv4 indication in all the groups using the nexthop. 2491 */ 2492 if (oldi->family != newi->family) { 2493 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2494 struct nexthop *nhp = nhge->nh_parent; 2495 struct nh_group *nhg; 2496 2497 nhg = rtnl_dereference(nhp->nh_grp); 2498 nh_group_v4_update(nhg); 2499 } 2500 } 2501 2502 return 0; 2503 2504 err_notify: 2505 rcu_assign_pointer(new->nh_info, newi); 2506 rcu_assign_pointer(old->nh_info, oldi); 2507 old->nh_flags = old_nh_flags; 2508 old->protocol = old_protocol; 2509 oldi->nh_parent = old; 2510 newi->nh_parent = new; 2511 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) { 2512 struct nexthop *nhp = nhge->nh_parent; 2513 2514 replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL); 2515 } 2516 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack); 2517 return err; 2518 } 2519 2520 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh, 2521 struct nl_info *info) 2522 { 2523 struct fib6_info *f6i; 2524 2525 if (!list_empty(&nh->fi_list)) { 2526 struct fib_info *fi; 2527 2528 /* expectation is a few fib_info per nexthop and then 2529 * a lot of routes per fib_info. So mark the fib_info 2530 * and then walk the fib tables once 2531 */ 2532 list_for_each_entry(fi, &nh->fi_list, nh_list) 2533 fi->nh_updated = true; 2534 2535 fib_info_notify_update(net, info); 2536 2537 list_for_each_entry(fi, &nh->fi_list, nh_list) 2538 fi->nh_updated = false; 2539 } 2540 2541 list_for_each_entry(f6i, &nh->f6i_list, nh_list) 2542 fib6_rt_update(net, f6i, info); 2543 } 2544 2545 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries 2546 * linked to this nexthop and for all groups that the nexthop 2547 * is a member of 2548 */ 2549 static void nexthop_replace_notify(struct net *net, struct nexthop *nh, 2550 struct nl_info *info) 2551 { 2552 struct nh_grp_entry *nhge; 2553 2554 __nexthop_replace_notify(net, nh, info); 2555 2556 list_for_each_entry(nhge, &nh->grp_list, nh_list) 2557 __nexthop_replace_notify(net, nhge->nh_parent, info); 2558 } 2559 2560 static int replace_nexthop(struct net *net, struct nexthop *old, 2561 struct nexthop *new, const struct nh_config *cfg, 2562 struct netlink_ext_ack *extack) 2563 { 2564 bool new_is_reject = false; 2565 struct nh_grp_entry *nhge; 2566 int err; 2567 2568 /* check that existing FIB entries are ok with the 2569 * new nexthop definition 2570 */ 2571 err = fib_check_nh_list(old, new, extack); 2572 if (err) 2573 return err; 2574 2575 err = fib6_check_nh_list(old, new, extack); 2576 if (err) 2577 return err; 2578 2579 if (!new->is_group) { 2580 struct nh_info *nhi = rtnl_dereference(new->nh_info); 2581 2582 new_is_reject = nhi->reject_nh; 2583 } 2584 2585 list_for_each_entry(nhge, &old->grp_list, nh_list) { 2586 /* if new nexthop is a blackhole, any groups using this 2587 * nexthop cannot have more than 1 path 2588 */ 2589 if (new_is_reject && 2590 nexthop_num_path(nhge->nh_parent) > 1) { 2591 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); 2592 return -EINVAL; 2593 } 2594 2595 err = fib_check_nh_list(nhge->nh_parent, new, extack); 2596 if (err) 2597 return err; 2598 2599 err = fib6_check_nh_list(nhge->nh_parent, new, extack); 2600 if (err) 2601 return err; 2602 } 2603 2604 if (old->is_group) 2605 err = replace_nexthop_grp(net, old, new, cfg, extack); 2606 else 2607 err = replace_nexthop_single(net, old, new, extack); 2608 2609 if (!err) { 2610 nh_rt_cache_flush(net, old, new); 2611 2612 WARN_ON_ONCE(__remove_nexthop(net, new, NULL)); 2613 nexthop_put(new); 2614 } 2615 2616 return err; 2617 } 2618 2619 /* called with rtnl_lock held */ 2620 static int insert_nexthop(struct net *net, struct nexthop *new_nh, 2621 struct nh_config *cfg, struct netlink_ext_ack *extack) 2622 { 2623 struct rb_node **pp, *parent = NULL, *next; 2624 struct rb_root *root = &net->nexthop.rb_root; 2625 bool replace = !!(cfg->nlflags & NLM_F_REPLACE); 2626 bool create = !!(cfg->nlflags & NLM_F_CREATE); 2627 u32 new_id = new_nh->id; 2628 int replace_notify = 0; 2629 int rc = -EEXIST; 2630 2631 pp = &root->rb_node; 2632 while (1) { 2633 struct nexthop *nh; 2634 2635 next = *pp; 2636 if (!next) 2637 break; 2638 2639 parent = next; 2640 2641 nh = rb_entry(parent, struct nexthop, rb_node); 2642 if (new_id < nh->id) { 2643 pp = &next->rb_left; 2644 } else if (new_id > nh->id) { 2645 pp = &next->rb_right; 2646 } else if (replace) { 2647 rc = replace_nexthop(net, nh, new_nh, cfg, extack); 2648 if (!rc) { 2649 new_nh = nh; /* send notification with old nh */ 2650 replace_notify = 1; 2651 } 2652 goto out; 2653 } else { 2654 /* id already exists and not a replace */ 2655 goto out; 2656 } 2657 } 2658 2659 if (replace && !create) { 2660 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); 2661 rc = -ENOENT; 2662 goto out; 2663 } 2664 2665 if (new_nh->is_group) { 2666 struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp); 2667 struct nh_res_table *res_table; 2668 2669 if (nhg->resilient) { 2670 res_table = rtnl_dereference(nhg->res_table); 2671 2672 /* Not passing the number of buckets is OK when 2673 * replacing, but not when creating a new group. 2674 */ 2675 if (!cfg->nh_grp_res_has_num_buckets) { 2676 NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion"); 2677 rc = -EINVAL; 2678 goto out; 2679 } 2680 2681 nh_res_group_rebalance(nhg, res_table); 2682 2683 /* Do not send bucket notifications, we do full 2684 * notification below. 2685 */ 2686 nh_res_table_upkeep(res_table, false, false); 2687 } 2688 } 2689 2690 rb_link_node_rcu(&new_nh->rb_node, parent, pp); 2691 rb_insert_color(&new_nh->rb_node, root); 2692 2693 /* The initial insertion is a full notification for hash-threshold as 2694 * well as resilient groups. 2695 */ 2696 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack); 2697 if (rc) 2698 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root); 2699 2700 out: 2701 if (!rc) { 2702 nh_base_seq_inc(net); 2703 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 2704 if (replace_notify && 2705 READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)) 2706 nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 2707 } 2708 2709 return rc; 2710 } 2711 2712 /* rtnl */ 2713 /* remove all nexthops tied to a device being deleted */ 2714 static void nexthop_flush_dev(struct net_device *dev, unsigned long event) 2715 { 2716 unsigned int hash = nh_dev_hashfn(dev->ifindex); 2717 struct net *net = dev_net(dev); 2718 struct hlist_head *head = &net->nexthop.devhash[hash]; 2719 bool need_flush = false; 2720 struct hlist_node *n; 2721 struct nh_info *nhi; 2722 2723 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 2724 if (nhi->fib_nhc.nhc_dev != dev) 2725 continue; 2726 2727 if (nhi->reject_nh && 2728 (event == NETDEV_DOWN || event == NETDEV_CHANGE)) 2729 continue; 2730 2731 need_flush |= remove_nexthop(net, nhi->nh_parent, NULL); 2732 } 2733 2734 if (need_flush) 2735 fib_flush(net); 2736 } 2737 2738 /* rtnl; called when net namespace is deleted */ 2739 static void flush_all_nexthops(struct net *net) 2740 { 2741 struct rb_root *root = &net->nexthop.rb_root; 2742 bool need_flush = false; 2743 struct rb_node *node; 2744 struct nexthop *nh; 2745 2746 while ((node = rb_first(root))) { 2747 nh = rb_entry(node, struct nexthop, rb_node); 2748 need_flush |= remove_nexthop(net, nh, NULL); 2749 cond_resched(); 2750 } 2751 if (need_flush) 2752 fib_flush(net); 2753 } 2754 2755 static struct nexthop *nexthop_create_group(struct net *net, 2756 struct nh_config *cfg) 2757 { 2758 struct nlattr *grps_attr = cfg->nh_grp; 2759 struct nexthop_grp *entry = nla_data(grps_attr); 2760 u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 2761 struct nh_group *nhg; 2762 struct nexthop *nh; 2763 int err; 2764 int i; 2765 2766 nh = nexthop_alloc(); 2767 if (!nh) 2768 return ERR_PTR(-ENOMEM); 2769 2770 nh->is_group = 1; 2771 2772 nhg = nexthop_grp_alloc(num_nh); 2773 if (!nhg) { 2774 kfree(nh); 2775 return ERR_PTR(-ENOMEM); 2776 } 2777 2778 /* spare group used for removals */ 2779 nhg->spare = nexthop_grp_alloc(num_nh); 2780 if (!nhg->spare) { 2781 kfree(nhg); 2782 kfree(nh); 2783 return ERR_PTR(-ENOMEM); 2784 } 2785 nhg->spare->spare = nhg; 2786 2787 for (i = 0; i < nhg->num_nh; ++i) { 2788 struct nexthop *nhe; 2789 struct nh_info *nhi; 2790 2791 nhe = nexthop_find_by_id(net, entry[i].id); 2792 if (!nexthop_get(nhe)) { 2793 err = -ENOENT; 2794 goto out_no_nh; 2795 } 2796 2797 nhi = rtnl_dereference(nhe->nh_info); 2798 if (nhi->family == AF_INET) 2799 nhg->has_v4 = true; 2800 2801 nhg->nh_entries[i].stats = 2802 netdev_alloc_pcpu_stats(struct nh_grp_entry_stats); 2803 if (!nhg->nh_entries[i].stats) { 2804 err = -ENOMEM; 2805 nexthop_put(nhe); 2806 goto out_no_nh; 2807 } 2808 nhg->nh_entries[i].nh = nhe; 2809 nhg->nh_entries[i].weight = nexthop_grp_weight(&entry[i]); 2810 2811 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 2812 nhg->nh_entries[i].nh_parent = nh; 2813 } 2814 2815 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 2816 nhg->hash_threshold = 1; 2817 nhg->is_multipath = true; 2818 } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) { 2819 struct nh_res_table *res_table; 2820 2821 res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg); 2822 if (!res_table) { 2823 err = -ENOMEM; 2824 goto out_no_nh; 2825 } 2826 2827 rcu_assign_pointer(nhg->spare->res_table, res_table); 2828 rcu_assign_pointer(nhg->res_table, res_table); 2829 nhg->resilient = true; 2830 nhg->is_multipath = true; 2831 } 2832 2833 WARN_ON_ONCE(nhg->hash_threshold + nhg->resilient != 1); 2834 2835 if (nhg->hash_threshold) 2836 nh_hthr_group_rebalance(nhg); 2837 2838 if (cfg->nh_fdb) 2839 nhg->fdb_nh = 1; 2840 2841 if (cfg->nh_hw_stats) 2842 nhg->hw_stats = true; 2843 2844 rcu_assign_pointer(nh->nh_grp, nhg); 2845 2846 return nh; 2847 2848 out_no_nh: 2849 for (i--; i >= 0; --i) { 2850 list_del(&nhg->nh_entries[i].nh_list); 2851 free_percpu(nhg->nh_entries[i].stats); 2852 nexthop_put(nhg->nh_entries[i].nh); 2853 } 2854 2855 kfree(nhg->spare); 2856 kfree(nhg); 2857 kfree(nh); 2858 2859 return ERR_PTR(err); 2860 } 2861 2862 static int nh_create_ipv4(struct net *net, struct nexthop *nh, 2863 struct nh_info *nhi, struct nh_config *cfg, 2864 struct netlink_ext_ack *extack) 2865 { 2866 struct fib_nh *fib_nh = &nhi->fib_nh; 2867 struct fib_config fib_cfg = { 2868 .fc_oif = cfg->nh_ifindex, 2869 .fc_gw4 = cfg->gw.ipv4, 2870 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 2871 .fc_flags = cfg->nh_flags, 2872 .fc_nlinfo = cfg->nlinfo, 2873 .fc_encap = cfg->nh_encap, 2874 .fc_encap_type = cfg->nh_encap_type, 2875 }; 2876 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 2877 int err; 2878 2879 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 2880 if (err) { 2881 fib_nh_release(net, fib_nh); 2882 goto out; 2883 } 2884 2885 if (nhi->fdb_nh) 2886 goto out; 2887 2888 /* sets nh_dev if successful */ 2889 err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 2890 if (!err) { 2891 nh->nh_flags = fib_nh->fib_nh_flags; 2892 fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 2893 !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1); 2894 } else { 2895 fib_nh_release(net, fib_nh); 2896 } 2897 out: 2898 return err; 2899 } 2900 2901 static int nh_create_ipv6(struct net *net, struct nexthop *nh, 2902 struct nh_info *nhi, struct nh_config *cfg, 2903 struct netlink_ext_ack *extack) 2904 { 2905 struct fib6_nh *fib6_nh = &nhi->fib6_nh; 2906 struct fib6_config fib6_cfg = { 2907 .fc_table = l3mdev_fib_table(cfg->dev), 2908 .fc_ifindex = cfg->nh_ifindex, 2909 .fc_gateway = cfg->gw.ipv6, 2910 .fc_flags = cfg->nh_flags, 2911 .fc_nlinfo = cfg->nlinfo, 2912 .fc_encap = cfg->nh_encap, 2913 .fc_encap_type = cfg->nh_encap_type, 2914 .fc_is_fdb = cfg->nh_fdb, 2915 }; 2916 int err; 2917 2918 if (!ipv6_addr_any(&cfg->gw.ipv6)) 2919 fib6_cfg.fc_flags |= RTF_GATEWAY; 2920 2921 /* sets nh_dev if successful */ 2922 err = fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, extack); 2923 if (err) { 2924 /* IPv6 is not enabled, don't call fib6_nh_release */ 2925 if (err == -EAFNOSUPPORT) 2926 goto out; 2927 fib6_nh_release(fib6_nh); 2928 } else { 2929 nh->nh_flags = fib6_nh->fib_nh_flags; 2930 } 2931 out: 2932 return err; 2933 } 2934 2935 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 2936 struct netlink_ext_ack *extack) 2937 { 2938 struct nh_info *nhi; 2939 struct nexthop *nh; 2940 int err = 0; 2941 2942 nh = nexthop_alloc(); 2943 if (!nh) 2944 return ERR_PTR(-ENOMEM); 2945 2946 nhi = kzalloc_obj(*nhi); 2947 if (!nhi) { 2948 kfree(nh); 2949 return ERR_PTR(-ENOMEM); 2950 } 2951 2952 nh->nh_flags = cfg->nh_flags; 2953 nh->net = net; 2954 2955 nhi->nh_parent = nh; 2956 nhi->family = cfg->nh_family; 2957 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 2958 2959 if (cfg->nh_fdb) 2960 nhi->fdb_nh = 1; 2961 2962 if (cfg->nh_blackhole) { 2963 nhi->reject_nh = 1; 2964 cfg->nh_ifindex = net->loopback_dev->ifindex; 2965 } 2966 2967 switch (cfg->nh_family) { 2968 case AF_INET: 2969 err = nh_create_ipv4(net, nh, nhi, cfg, extack); 2970 break; 2971 case AF_INET6: 2972 err = nh_create_ipv6(net, nh, nhi, cfg, extack); 2973 break; 2974 } 2975 2976 if (err) { 2977 kfree(nhi); 2978 kfree(nh); 2979 return ERR_PTR(err); 2980 } 2981 2982 /* add the entry to the device based hash */ 2983 if (!nhi->fdb_nh) 2984 nexthop_devhash_add(net, nhi); 2985 2986 rcu_assign_pointer(nh->nh_info, nhi); 2987 2988 return nh; 2989 } 2990 2991 /* called with rtnl lock held */ 2992 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 2993 struct netlink_ext_ack *extack) 2994 { 2995 struct nexthop *nh; 2996 int err; 2997 2998 if (!cfg->nh_id) { 2999 cfg->nh_id = nh_find_unused_id(net); 3000 if (!cfg->nh_id) { 3001 NL_SET_ERR_MSG(extack, "No unused id"); 3002 return ERR_PTR(-EINVAL); 3003 } 3004 } 3005 3006 if (cfg->nh_grp) 3007 nh = nexthop_create_group(net, cfg); 3008 else 3009 nh = nexthop_create(net, cfg, extack); 3010 3011 if (IS_ERR(nh)) 3012 return nh; 3013 3014 refcount_set(&nh->refcnt, 1); 3015 nh->id = cfg->nh_id; 3016 nh->protocol = cfg->nh_protocol; 3017 nh->net = net; 3018 3019 err = insert_nexthop(net, nh, cfg, extack); 3020 if (err) { 3021 WARN_ON_ONCE(__remove_nexthop(net, nh, NULL)); 3022 nexthop_put(nh); 3023 nh = ERR_PTR(err); 3024 } 3025 3026 return nh; 3027 } 3028 3029 static int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback, 3030 unsigned long *timer_p, bool *has_p, 3031 struct netlink_ext_ack *extack) 3032 { 3033 unsigned long timer; 3034 u32 value; 3035 3036 if (!attr) { 3037 *timer_p = fallback; 3038 *has_p = false; 3039 return 0; 3040 } 3041 3042 value = nla_get_u32(attr); 3043 timer = clock_t_to_jiffies(value); 3044 if (timer == ~0UL) { 3045 NL_SET_ERR_MSG(extack, "Timer value too large"); 3046 return -EINVAL; 3047 } 3048 3049 *timer_p = timer; 3050 *has_p = true; 3051 return 0; 3052 } 3053 3054 static int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg, 3055 struct netlink_ext_ack *extack) 3056 { 3057 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {}; 3058 int err; 3059 3060 if (res) { 3061 err = nla_parse_nested(tb, 3062 ARRAY_SIZE(rtm_nh_res_policy_new) - 1, 3063 res, rtm_nh_res_policy_new, extack); 3064 if (err < 0) 3065 return err; 3066 } 3067 3068 if (tb[NHA_RES_GROUP_BUCKETS]) { 3069 cfg->nh_grp_res_num_buckets = 3070 nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]); 3071 cfg->nh_grp_res_has_num_buckets = true; 3072 if (!cfg->nh_grp_res_num_buckets) { 3073 NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0"); 3074 return -EINVAL; 3075 } 3076 } 3077 3078 err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER], 3079 NH_RES_DEFAULT_IDLE_TIMER, 3080 &cfg->nh_grp_res_idle_timer, 3081 &cfg->nh_grp_res_has_idle_timer, 3082 extack); 3083 if (err) 3084 return err; 3085 3086 return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER], 3087 NH_RES_DEFAULT_UNBALANCED_TIMER, 3088 &cfg->nh_grp_res_unbalanced_timer, 3089 &cfg->nh_grp_res_has_unbalanced_timer, 3090 extack); 3091 } 3092 3093 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 3094 struct nlmsghdr *nlh, struct nlattr **tb, 3095 struct nh_config *cfg, 3096 struct netlink_ext_ack *extack) 3097 { 3098 struct nhmsg *nhm = nlmsg_data(nlh); 3099 int err; 3100 3101 err = -EINVAL; 3102 if (nhm->resvd || nhm->nh_scope) { 3103 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 3104 goto out; 3105 } 3106 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 3107 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 3108 goto out; 3109 } 3110 3111 switch (nhm->nh_family) { 3112 case AF_INET: 3113 case AF_INET6: 3114 break; 3115 case AF_UNSPEC: 3116 if (tb[NHA_GROUP]) 3117 break; 3118 fallthrough; 3119 default: 3120 NL_SET_ERR_MSG(extack, "Invalid address family"); 3121 goto out; 3122 } 3123 3124 memset(cfg, 0, sizeof(*cfg)); 3125 cfg->nlflags = nlh->nlmsg_flags; 3126 cfg->nlinfo.portid = NETLINK_CB(skb).portid; 3127 cfg->nlinfo.nlh = nlh; 3128 cfg->nlinfo.nl_net = net; 3129 3130 cfg->nh_family = nhm->nh_family; 3131 cfg->nh_protocol = nhm->nh_protocol; 3132 cfg->nh_flags = nhm->nh_flags; 3133 3134 if (tb[NHA_ID]) 3135 cfg->nh_id = nla_get_u32(tb[NHA_ID]); 3136 3137 if (tb[NHA_FDB]) { 3138 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 3139 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 3140 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 3141 goto out; 3142 } 3143 if (nhm->nh_flags) { 3144 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 3145 goto out; 3146 } 3147 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 3148 } 3149 3150 if (tb[NHA_GROUP]) { 3151 if (nhm->nh_family != AF_UNSPEC) { 3152 NL_SET_ERR_MSG(extack, "Invalid family for group"); 3153 goto out; 3154 } 3155 cfg->nh_grp = tb[NHA_GROUP]; 3156 3157 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 3158 if (tb[NHA_GROUP_TYPE]) 3159 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 3160 3161 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 3162 NL_SET_ERR_MSG(extack, "Invalid group type"); 3163 goto out; 3164 } 3165 3166 err = nh_check_attr_group(net, tb, ARRAY_SIZE(rtm_nh_policy_new), 3167 cfg->nh_grp_type, extack); 3168 if (err) 3169 goto out; 3170 3171 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) 3172 err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP], 3173 cfg, extack); 3174 3175 if (tb[NHA_HW_STATS_ENABLE]) 3176 cfg->nh_hw_stats = nla_get_u32(tb[NHA_HW_STATS_ENABLE]); 3177 3178 /* no other attributes should be set */ 3179 goto out; 3180 } 3181 3182 if (tb[NHA_BLACKHOLE]) { 3183 if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 3184 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 3185 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 3186 goto out; 3187 } 3188 3189 cfg->nh_blackhole = 1; 3190 err = 0; 3191 goto out; 3192 } 3193 3194 if (!cfg->nh_fdb && !tb[NHA_OIF]) { 3195 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 3196 goto out; 3197 } 3198 3199 err = -EINVAL; 3200 if (tb[NHA_GATEWAY]) { 3201 struct nlattr *gwa = tb[NHA_GATEWAY]; 3202 3203 switch (cfg->nh_family) { 3204 case AF_INET: 3205 if (nla_len(gwa) != sizeof(u32)) { 3206 NL_SET_ERR_MSG(extack, "Invalid gateway"); 3207 goto out; 3208 } 3209 cfg->gw.ipv4 = nla_get_be32(gwa); 3210 break; 3211 case AF_INET6: 3212 if (nla_len(gwa) != sizeof(struct in6_addr)) { 3213 NL_SET_ERR_MSG(extack, "Invalid gateway"); 3214 goto out; 3215 } 3216 cfg->gw.ipv6 = nla_get_in6_addr(gwa); 3217 break; 3218 default: 3219 NL_SET_ERR_MSG(extack, 3220 "Unknown address family for gateway"); 3221 goto out; 3222 } 3223 } else { 3224 /* device only nexthop (no gateway) */ 3225 if (cfg->nh_flags & RTNH_F_ONLINK) { 3226 NL_SET_ERR_MSG(extack, 3227 "ONLINK flag can not be set for nexthop without a gateway"); 3228 goto out; 3229 } 3230 } 3231 3232 if (tb[NHA_ENCAP]) { 3233 cfg->nh_encap = tb[NHA_ENCAP]; 3234 3235 if (!tb[NHA_ENCAP_TYPE]) { 3236 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 3237 goto out; 3238 } 3239 3240 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 3241 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 3242 if (err < 0) 3243 goto out; 3244 3245 } else if (tb[NHA_ENCAP_TYPE]) { 3246 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 3247 goto out; 3248 } 3249 3250 if (tb[NHA_HW_STATS_ENABLE]) { 3251 NL_SET_ERR_MSG(extack, "Cannot enable nexthop hardware statistics for non-group nexthops"); 3252 goto out; 3253 } 3254 3255 err = 0; 3256 out: 3257 return err; 3258 } 3259 3260 static int rtm_to_nh_config_rtnl(struct net *net, struct nlattr **tb, 3261 struct nh_config *cfg, 3262 struct netlink_ext_ack *extack) 3263 { 3264 if (tb[NHA_GROUP]) 3265 return nh_check_attr_group_rtnl(net, tb, extack); 3266 3267 if (tb[NHA_OIF]) { 3268 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 3269 if (cfg->nh_ifindex) 3270 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 3271 3272 if (!cfg->dev) { 3273 NL_SET_ERR_MSG(extack, "Invalid device index"); 3274 return -EINVAL; 3275 } 3276 3277 if (!(cfg->dev->flags & IFF_UP)) { 3278 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 3279 return -ENETDOWN; 3280 } 3281 3282 if (!netif_carrier_ok(cfg->dev)) { 3283 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 3284 return -ENETDOWN; 3285 } 3286 } 3287 3288 return 0; 3289 } 3290 3291 /* rtnl */ 3292 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 3293 struct netlink_ext_ack *extack) 3294 { 3295 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)]; 3296 struct net *net = sock_net(skb->sk); 3297 struct nh_config cfg; 3298 struct nexthop *nh; 3299 int err; 3300 3301 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3302 ARRAY_SIZE(rtm_nh_policy_new) - 1, 3303 rtm_nh_policy_new, extack); 3304 if (err < 0) 3305 goto out; 3306 3307 err = rtm_to_nh_config(net, skb, nlh, tb, &cfg, extack); 3308 if (err) 3309 goto out; 3310 3311 if (cfg.nlflags & NLM_F_REPLACE && !cfg.nh_id) { 3312 NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 3313 err = -EINVAL; 3314 goto out; 3315 } 3316 3317 rtnl_net_lock(net); 3318 3319 err = rtm_to_nh_config_rtnl(net, tb, &cfg, extack); 3320 if (err) 3321 goto unlock; 3322 3323 nh = nexthop_add(net, &cfg, extack); 3324 if (IS_ERR(nh)) 3325 err = PTR_ERR(nh); 3326 3327 unlock: 3328 rtnl_net_unlock(net); 3329 out: 3330 return err; 3331 } 3332 3333 static int nh_valid_get_del_req(const struct nlmsghdr *nlh, 3334 struct nlattr **tb, u32 *id, u32 *op_flags, 3335 struct netlink_ext_ack *extack) 3336 { 3337 struct nhmsg *nhm = nlmsg_data(nlh); 3338 3339 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 3340 NL_SET_ERR_MSG(extack, "Invalid values in header"); 3341 return -EINVAL; 3342 } 3343 3344 if (!tb[NHA_ID]) { 3345 NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 3346 return -EINVAL; 3347 } 3348 3349 *id = nla_get_u32(tb[NHA_ID]); 3350 if (!(*id)) { 3351 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 3352 return -EINVAL; 3353 } 3354 3355 if (op_flags) 3356 *op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0); 3357 3358 return 0; 3359 } 3360 3361 /* rtnl */ 3362 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 3363 struct netlink_ext_ack *extack) 3364 { 3365 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_del)]; 3366 struct net *net = sock_net(skb->sk); 3367 struct nl_info nlinfo = { 3368 .nlh = nlh, 3369 .nl_net = net, 3370 .portid = NETLINK_CB(skb).portid, 3371 }; 3372 struct nexthop *nh; 3373 int err; 3374 u32 id; 3375 3376 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3377 ARRAY_SIZE(rtm_nh_policy_del) - 1, rtm_nh_policy_del, 3378 extack); 3379 if (err < 0) 3380 return err; 3381 3382 err = nh_valid_get_del_req(nlh, tb, &id, NULL, extack); 3383 if (err) 3384 return err; 3385 3386 rtnl_net_lock(net); 3387 3388 nh = nexthop_find_by_id(net, id); 3389 if (nh) 3390 remove_one_nexthop(net, nh, &nlinfo); 3391 else 3392 err = -ENOENT; 3393 3394 rtnl_net_unlock(net); 3395 3396 return err; 3397 } 3398 3399 /* rtnl */ 3400 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3401 struct netlink_ext_ack *extack) 3402 { 3403 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)]; 3404 struct net *net = sock_net(in_skb->sk); 3405 struct sk_buff *skb = NULL; 3406 struct nexthop *nh; 3407 u32 op_flags; 3408 int err; 3409 u32 id; 3410 3411 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3412 ARRAY_SIZE(rtm_nh_policy_get) - 1, rtm_nh_policy_get, 3413 extack); 3414 if (err < 0) 3415 return err; 3416 3417 err = nh_valid_get_del_req(nlh, tb, &id, &op_flags, extack); 3418 if (err) 3419 return err; 3420 3421 err = -ENOENT; 3422 nh = nexthop_find_by_id(net, id); 3423 if (!nh) 3424 goto out; 3425 3426 err = -ENOBUFS; 3427 skb = nlmsg_new(nh_nlmsg_size(nh, op_flags), GFP_KERNEL); 3428 if (!skb) 3429 goto out; 3430 3431 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 3432 nlh->nlmsg_seq, 0, op_flags); 3433 if (err < 0) { 3434 WARN_ON(err == -EMSGSIZE); 3435 goto errout_free; 3436 } 3437 3438 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3439 out: 3440 return err; 3441 errout_free: 3442 kfree_skb(skb); 3443 goto out; 3444 } 3445 3446 struct nh_dump_filter { 3447 u32 nh_id; 3448 int dev_idx; 3449 int master_idx; 3450 bool group_filter; 3451 bool fdb_filter; 3452 u32 res_bucket_nh_id; 3453 u32 op_flags; 3454 }; 3455 3456 static bool nh_dump_filtered(struct nexthop *nh, 3457 struct nh_dump_filter *filter, u8 family) 3458 { 3459 const struct net_device *dev; 3460 const struct nh_info *nhi; 3461 3462 if (filter->group_filter && !nh->is_group) 3463 return true; 3464 3465 if (!filter->dev_idx && !filter->master_idx && !family) 3466 return false; 3467 3468 if (nh->is_group) 3469 return true; 3470 3471 nhi = rtnl_dereference(nh->nh_info); 3472 if (family && nhi->family != family) 3473 return true; 3474 3475 dev = nhi->fib_nhc.nhc_dev; 3476 if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx)) 3477 return true; 3478 3479 if (filter->master_idx) { 3480 struct net_device *master; 3481 3482 if (!dev) 3483 return true; 3484 3485 master = netdev_master_upper_dev_get((struct net_device *)dev); 3486 if (!master || master->ifindex != filter->master_idx) 3487 return true; 3488 } 3489 3490 return false; 3491 } 3492 3493 static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb, 3494 struct nh_dump_filter *filter, 3495 struct netlink_ext_ack *extack) 3496 { 3497 struct nhmsg *nhm; 3498 u32 idx; 3499 3500 if (tb[NHA_OIF]) { 3501 idx = nla_get_u32(tb[NHA_OIF]); 3502 if (idx > INT_MAX) { 3503 NL_SET_ERR_MSG(extack, "Invalid device index"); 3504 return -EINVAL; 3505 } 3506 filter->dev_idx = idx; 3507 } 3508 if (tb[NHA_MASTER]) { 3509 idx = nla_get_u32(tb[NHA_MASTER]); 3510 if (idx > INT_MAX) { 3511 NL_SET_ERR_MSG(extack, "Invalid master device index"); 3512 return -EINVAL; 3513 } 3514 filter->master_idx = idx; 3515 } 3516 filter->group_filter = nla_get_flag(tb[NHA_GROUPS]); 3517 filter->fdb_filter = nla_get_flag(tb[NHA_FDB]); 3518 3519 nhm = nlmsg_data(nlh); 3520 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 3521 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 3522 return -EINVAL; 3523 } 3524 3525 return 0; 3526 } 3527 3528 static int nh_valid_dump_req(const struct nlmsghdr *nlh, 3529 struct nh_dump_filter *filter, 3530 struct netlink_callback *cb) 3531 { 3532 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)]; 3533 int err; 3534 3535 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3536 ARRAY_SIZE(rtm_nh_policy_dump) - 1, 3537 rtm_nh_policy_dump, cb->extack); 3538 if (err < 0) 3539 return err; 3540 3541 filter->op_flags = nla_get_u32_default(tb[NHA_OP_FLAGS], 0); 3542 3543 return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 3544 } 3545 3546 struct rtm_dump_nh_ctx { 3547 u32 idx; 3548 }; 3549 3550 static struct rtm_dump_nh_ctx * 3551 rtm_dump_nh_ctx(struct netlink_callback *cb) 3552 { 3553 struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx; 3554 3555 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 3556 return ctx; 3557 } 3558 3559 static int rtm_dump_walk_nexthops(struct sk_buff *skb, 3560 struct netlink_callback *cb, 3561 struct rb_root *root, 3562 struct rtm_dump_nh_ctx *ctx, 3563 int (*nh_cb)(struct sk_buff *skb, 3564 struct netlink_callback *cb, 3565 struct nexthop *nh, void *data), 3566 void *data) 3567 { 3568 struct rb_node *node; 3569 int s_idx; 3570 int err; 3571 3572 s_idx = ctx->idx; 3573 3574 /* If this is not the first invocation, ctx->idx will contain the id of 3575 * the last nexthop we processed. Instead of starting from the very 3576 * first element of the red/black tree again and linearly skipping the 3577 * (potentially large) set of nodes with an id smaller than s_idx, walk 3578 * the tree and find the left-most node whose id is >= s_idx. This 3579 * provides an efficient O(log n) starting point for the dump 3580 * continuation. 3581 */ 3582 if (s_idx != 0) { 3583 struct rb_node *tmp = root->rb_node; 3584 3585 node = NULL; 3586 while (tmp) { 3587 struct nexthop *nh; 3588 3589 nh = rb_entry(tmp, struct nexthop, rb_node); 3590 if (nh->id < s_idx) { 3591 tmp = tmp->rb_right; 3592 } else { 3593 /* Track current candidate and keep looking on 3594 * the left side to find the left-most 3595 * (smallest id) that is still >= s_idx. 3596 */ 3597 node = tmp; 3598 tmp = tmp->rb_left; 3599 } 3600 } 3601 } else { 3602 node = rb_first(root); 3603 } 3604 3605 for (; node; node = rb_next(node)) { 3606 struct nexthop *nh; 3607 3608 nh = rb_entry(node, struct nexthop, rb_node); 3609 3610 ctx->idx = nh->id; 3611 err = nh_cb(skb, cb, nh, data); 3612 if (err) 3613 return err; 3614 } 3615 3616 return 0; 3617 } 3618 3619 static int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb, 3620 struct nexthop *nh, void *data) 3621 { 3622 struct nhmsg *nhm = nlmsg_data(cb->nlh); 3623 struct nh_dump_filter *filter = data; 3624 3625 if (nh_dump_filtered(nh, filter, nhm->nh_family)) 3626 return 0; 3627 3628 return nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 3629 NETLINK_CB(cb->skb).portid, 3630 cb->nlh->nlmsg_seq, NLM_F_MULTI, filter->op_flags); 3631 } 3632 3633 /* rtnl */ 3634 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 3635 { 3636 struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb); 3637 struct net *net = sock_net(skb->sk); 3638 struct rb_root *root = &net->nexthop.rb_root; 3639 struct nh_dump_filter filter = {}; 3640 int err; 3641 3642 err = nh_valid_dump_req(cb->nlh, &filter, cb); 3643 if (err < 0) 3644 return err; 3645 3646 err = rtm_dump_walk_nexthops(skb, cb, root, ctx, 3647 &rtm_dump_nexthop_cb, &filter); 3648 3649 cb->seq = net->nexthop.seq; 3650 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 3651 return err; 3652 } 3653 3654 static struct nexthop * 3655 nexthop_find_group_resilient(struct net *net, u32 id, 3656 struct netlink_ext_ack *extack) 3657 { 3658 struct nh_group *nhg; 3659 struct nexthop *nh; 3660 3661 nh = nexthop_find_by_id(net, id); 3662 if (!nh) 3663 return ERR_PTR(-ENOENT); 3664 3665 if (!nh->is_group) { 3666 NL_SET_ERR_MSG(extack, "Not a nexthop group"); 3667 return ERR_PTR(-EINVAL); 3668 } 3669 3670 nhg = rtnl_dereference(nh->nh_grp); 3671 if (!nhg->resilient) { 3672 NL_SET_ERR_MSG(extack, "Nexthop group not of type resilient"); 3673 return ERR_PTR(-EINVAL); 3674 } 3675 3676 return nh; 3677 } 3678 3679 static int nh_valid_dump_nhid(struct nlattr *attr, u32 *nh_id_p, 3680 struct netlink_ext_ack *extack) 3681 { 3682 u32 idx; 3683 3684 if (attr) { 3685 idx = nla_get_u32(attr); 3686 if (!idx) { 3687 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 3688 return -EINVAL; 3689 } 3690 *nh_id_p = idx; 3691 } else { 3692 *nh_id_p = 0; 3693 } 3694 3695 return 0; 3696 } 3697 3698 static int nh_valid_dump_bucket_req(const struct nlmsghdr *nlh, 3699 struct nh_dump_filter *filter, 3700 struct netlink_callback *cb) 3701 { 3702 struct nlattr *res_tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_dump)]; 3703 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump_bucket)]; 3704 int err; 3705 3706 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3707 ARRAY_SIZE(rtm_nh_policy_dump_bucket) - 1, 3708 rtm_nh_policy_dump_bucket, NULL); 3709 if (err < 0) 3710 return err; 3711 3712 err = nh_valid_dump_nhid(tb[NHA_ID], &filter->nh_id, cb->extack); 3713 if (err) 3714 return err; 3715 3716 if (tb[NHA_RES_BUCKET]) { 3717 size_t max = ARRAY_SIZE(rtm_nh_res_bucket_policy_dump) - 1; 3718 3719 err = nla_parse_nested(res_tb, max, 3720 tb[NHA_RES_BUCKET], 3721 rtm_nh_res_bucket_policy_dump, 3722 cb->extack); 3723 if (err < 0) 3724 return err; 3725 3726 err = nh_valid_dump_nhid(res_tb[NHA_RES_BUCKET_NH_ID], 3727 &filter->res_bucket_nh_id, 3728 cb->extack); 3729 if (err) 3730 return err; 3731 } 3732 3733 return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 3734 } 3735 3736 struct rtm_dump_res_bucket_ctx { 3737 struct rtm_dump_nh_ctx nh; 3738 u16 bucket_index; 3739 }; 3740 3741 static struct rtm_dump_res_bucket_ctx * 3742 rtm_dump_res_bucket_ctx(struct netlink_callback *cb) 3743 { 3744 struct rtm_dump_res_bucket_ctx *ctx = (void *)cb->ctx; 3745 3746 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 3747 return ctx; 3748 } 3749 3750 struct rtm_dump_nexthop_bucket_data { 3751 struct rtm_dump_res_bucket_ctx *ctx; 3752 struct nh_dump_filter filter; 3753 }; 3754 3755 static int rtm_dump_nexthop_bucket_nh(struct sk_buff *skb, 3756 struct netlink_callback *cb, 3757 struct nexthop *nh, 3758 struct rtm_dump_nexthop_bucket_data *dd) 3759 { 3760 u32 portid = NETLINK_CB(cb->skb).portid; 3761 struct nhmsg *nhm = nlmsg_data(cb->nlh); 3762 struct nh_res_table *res_table; 3763 struct nh_group *nhg; 3764 u16 bucket_index; 3765 int err; 3766 3767 nhg = rtnl_dereference(nh->nh_grp); 3768 res_table = rtnl_dereference(nhg->res_table); 3769 for (bucket_index = dd->ctx->bucket_index; 3770 bucket_index < res_table->num_nh_buckets; 3771 bucket_index++) { 3772 struct nh_res_bucket *bucket; 3773 struct nh_grp_entry *nhge; 3774 3775 bucket = &res_table->nh_buckets[bucket_index]; 3776 nhge = rtnl_dereference(bucket->nh_entry); 3777 if (nh_dump_filtered(nhge->nh, &dd->filter, nhm->nh_family)) 3778 continue; 3779 3780 if (dd->filter.res_bucket_nh_id && 3781 dd->filter.res_bucket_nh_id != nhge->nh->id) 3782 continue; 3783 3784 dd->ctx->bucket_index = bucket_index; 3785 err = nh_fill_res_bucket(skb, nh, bucket, bucket_index, 3786 RTM_NEWNEXTHOPBUCKET, portid, 3787 cb->nlh->nlmsg_seq, NLM_F_MULTI, 3788 cb->extack); 3789 if (err) 3790 return err; 3791 } 3792 3793 dd->ctx->bucket_index = 0; 3794 3795 return 0; 3796 } 3797 3798 static int rtm_dump_nexthop_bucket_cb(struct sk_buff *skb, 3799 struct netlink_callback *cb, 3800 struct nexthop *nh, void *data) 3801 { 3802 struct rtm_dump_nexthop_bucket_data *dd = data; 3803 struct nh_group *nhg; 3804 3805 if (!nh->is_group) 3806 return 0; 3807 3808 nhg = rtnl_dereference(nh->nh_grp); 3809 if (!nhg->resilient) 3810 return 0; 3811 3812 return rtm_dump_nexthop_bucket_nh(skb, cb, nh, dd); 3813 } 3814 3815 /* rtnl */ 3816 static int rtm_dump_nexthop_bucket(struct sk_buff *skb, 3817 struct netlink_callback *cb) 3818 { 3819 struct rtm_dump_res_bucket_ctx *ctx = rtm_dump_res_bucket_ctx(cb); 3820 struct rtm_dump_nexthop_bucket_data dd = { .ctx = ctx }; 3821 struct net *net = sock_net(skb->sk); 3822 struct nexthop *nh; 3823 int err; 3824 3825 err = nh_valid_dump_bucket_req(cb->nlh, &dd.filter, cb); 3826 if (err) 3827 return err; 3828 3829 if (dd.filter.nh_id) { 3830 nh = nexthop_find_group_resilient(net, dd.filter.nh_id, 3831 cb->extack); 3832 if (IS_ERR(nh)) 3833 return PTR_ERR(nh); 3834 err = rtm_dump_nexthop_bucket_nh(skb, cb, nh, &dd); 3835 } else { 3836 struct rb_root *root = &net->nexthop.rb_root; 3837 3838 err = rtm_dump_walk_nexthops(skb, cb, root, &ctx->nh, 3839 &rtm_dump_nexthop_bucket_cb, &dd); 3840 } 3841 3842 cb->seq = net->nexthop.seq; 3843 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 3844 return err; 3845 } 3846 3847 static int nh_valid_get_bucket_req_res_bucket(struct nlattr *res, 3848 u16 *bucket_index, 3849 struct netlink_ext_ack *extack) 3850 { 3851 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_get)]; 3852 int err; 3853 3854 err = nla_parse_nested(tb, ARRAY_SIZE(rtm_nh_res_bucket_policy_get) - 1, 3855 res, rtm_nh_res_bucket_policy_get, extack); 3856 if (err < 0) 3857 return err; 3858 3859 if (!tb[NHA_RES_BUCKET_INDEX]) { 3860 NL_SET_ERR_MSG(extack, "Bucket index is missing"); 3861 return -EINVAL; 3862 } 3863 3864 *bucket_index = nla_get_u16(tb[NHA_RES_BUCKET_INDEX]); 3865 return 0; 3866 } 3867 3868 static int nh_valid_get_bucket_req(const struct nlmsghdr *nlh, 3869 u32 *id, u16 *bucket_index, 3870 struct netlink_ext_ack *extack) 3871 { 3872 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get_bucket)]; 3873 int err; 3874 3875 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 3876 ARRAY_SIZE(rtm_nh_policy_get_bucket) - 1, 3877 rtm_nh_policy_get_bucket, extack); 3878 if (err < 0) 3879 return err; 3880 3881 err = nh_valid_get_del_req(nlh, tb, id, NULL, extack); 3882 if (err) 3883 return err; 3884 3885 if (!tb[NHA_RES_BUCKET]) { 3886 NL_SET_ERR_MSG(extack, "Bucket information is missing"); 3887 return -EINVAL; 3888 } 3889 3890 err = nh_valid_get_bucket_req_res_bucket(tb[NHA_RES_BUCKET], 3891 bucket_index, extack); 3892 if (err) 3893 return err; 3894 3895 return 0; 3896 } 3897 3898 /* rtnl */ 3899 static int rtm_get_nexthop_bucket(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3900 struct netlink_ext_ack *extack) 3901 { 3902 struct net *net = sock_net(in_skb->sk); 3903 struct nh_res_table *res_table; 3904 struct sk_buff *skb = NULL; 3905 struct nh_group *nhg; 3906 struct nexthop *nh; 3907 u16 bucket_index; 3908 int err; 3909 u32 id; 3910 3911 err = nh_valid_get_bucket_req(nlh, &id, &bucket_index, extack); 3912 if (err) 3913 return err; 3914 3915 nh = nexthop_find_group_resilient(net, id, extack); 3916 if (IS_ERR(nh)) 3917 return PTR_ERR(nh); 3918 3919 nhg = rtnl_dereference(nh->nh_grp); 3920 res_table = rtnl_dereference(nhg->res_table); 3921 if (bucket_index >= res_table->num_nh_buckets) { 3922 NL_SET_ERR_MSG(extack, "Bucket index out of bounds"); 3923 return -ENOENT; 3924 } 3925 3926 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 3927 if (!skb) 3928 return -ENOBUFS; 3929 3930 err = nh_fill_res_bucket(skb, nh, &res_table->nh_buckets[bucket_index], 3931 bucket_index, RTM_NEWNEXTHOPBUCKET, 3932 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 3933 0, extack); 3934 if (err < 0) { 3935 WARN_ON(err == -EMSGSIZE); 3936 goto errout_free; 3937 } 3938 3939 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3940 3941 errout_free: 3942 kfree_skb(skb); 3943 return err; 3944 } 3945 3946 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 3947 { 3948 unsigned int hash = nh_dev_hashfn(dev->ifindex); 3949 struct net *net = dev_net(dev); 3950 struct hlist_head *head = &net->nexthop.devhash[hash]; 3951 struct hlist_node *n; 3952 struct nh_info *nhi; 3953 3954 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 3955 if (nhi->fib_nhc.nhc_dev == dev) { 3956 if (nhi->family == AF_INET) 3957 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 3958 orig_mtu); 3959 } 3960 } 3961 } 3962 3963 /* rtnl */ 3964 static int nh_netdev_event(struct notifier_block *this, 3965 unsigned long event, void *ptr) 3966 { 3967 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3968 struct netdev_notifier_info_ext *info_ext; 3969 3970 switch (event) { 3971 case NETDEV_DOWN: 3972 case NETDEV_UNREGISTER: 3973 nexthop_flush_dev(dev, event); 3974 break; 3975 case NETDEV_CHANGE: 3976 if (!(netif_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 3977 nexthop_flush_dev(dev, event); 3978 break; 3979 case NETDEV_CHANGEMTU: 3980 info_ext = ptr; 3981 nexthop_sync_mtu(dev, info_ext->ext.mtu); 3982 rt_cache_flush(dev_net(dev)); 3983 break; 3984 } 3985 return NOTIFY_DONE; 3986 } 3987 3988 static struct notifier_block nh_netdev_notifier = { 3989 .notifier_call = nh_netdev_event, 3990 }; 3991 3992 static int nexthops_dump(struct net *net, struct notifier_block *nb, 3993 enum nexthop_event_type event_type, 3994 struct netlink_ext_ack *extack) 3995 { 3996 struct rb_root *root = &net->nexthop.rb_root; 3997 struct rb_node *node; 3998 int err = 0; 3999 4000 for (node = rb_first(root); node; node = rb_next(node)) { 4001 struct nexthop *nh; 4002 4003 nh = rb_entry(node, struct nexthop, rb_node); 4004 err = call_nexthop_notifier(nb, net, event_type, nh, extack); 4005 if (err) 4006 break; 4007 } 4008 4009 return err; 4010 } 4011 4012 int register_nexthop_notifier(struct net *net, struct notifier_block *nb, 4013 struct netlink_ext_ack *extack) 4014 { 4015 int err; 4016 4017 rtnl_lock(); 4018 err = nexthops_dump(net, nb, NEXTHOP_EVENT_REPLACE, extack); 4019 if (err) 4020 goto unlock; 4021 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain, 4022 nb); 4023 unlock: 4024 rtnl_unlock(); 4025 return err; 4026 } 4027 EXPORT_SYMBOL(register_nexthop_notifier); 4028 4029 int __unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 4030 { 4031 int err; 4032 4033 err = blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 4034 nb); 4035 if (!err) 4036 nexthops_dump(net, nb, NEXTHOP_EVENT_DEL, NULL); 4037 return err; 4038 } 4039 EXPORT_SYMBOL(__unregister_nexthop_notifier); 4040 4041 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 4042 { 4043 int err; 4044 4045 rtnl_lock(); 4046 err = __unregister_nexthop_notifier(net, nb); 4047 rtnl_unlock(); 4048 return err; 4049 } 4050 EXPORT_SYMBOL(unregister_nexthop_notifier); 4051 4052 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 4053 { 4054 struct nexthop *nexthop; 4055 4056 rcu_read_lock(); 4057 4058 nexthop = nexthop_find_by_id(net, id); 4059 if (!nexthop) 4060 goto out; 4061 4062 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 4063 if (offload) 4064 nexthop->nh_flags |= RTNH_F_OFFLOAD; 4065 if (trap) 4066 nexthop->nh_flags |= RTNH_F_TRAP; 4067 4068 out: 4069 rcu_read_unlock(); 4070 } 4071 EXPORT_SYMBOL(nexthop_set_hw_flags); 4072 4073 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index, 4074 bool offload, bool trap) 4075 { 4076 struct nh_res_table *res_table; 4077 struct nh_res_bucket *bucket; 4078 struct nexthop *nexthop; 4079 struct nh_group *nhg; 4080 4081 rcu_read_lock(); 4082 4083 nexthop = nexthop_find_by_id(net, id); 4084 if (!nexthop || !nexthop->is_group) 4085 goto out; 4086 4087 nhg = rcu_dereference(nexthop->nh_grp); 4088 if (!nhg->resilient) 4089 goto out; 4090 4091 if (bucket_index >= nhg->res_table->num_nh_buckets) 4092 goto out; 4093 4094 res_table = rcu_dereference(nhg->res_table); 4095 bucket = &res_table->nh_buckets[bucket_index]; 4096 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 4097 if (offload) 4098 bucket->nh_flags |= RTNH_F_OFFLOAD; 4099 if (trap) 4100 bucket->nh_flags |= RTNH_F_TRAP; 4101 4102 out: 4103 rcu_read_unlock(); 4104 } 4105 EXPORT_SYMBOL(nexthop_bucket_set_hw_flags); 4106 4107 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets, 4108 unsigned long *activity) 4109 { 4110 struct nh_res_table *res_table; 4111 struct nexthop *nexthop; 4112 struct nh_group *nhg; 4113 u16 i; 4114 4115 rcu_read_lock(); 4116 4117 nexthop = nexthop_find_by_id(net, id); 4118 if (!nexthop || !nexthop->is_group) 4119 goto out; 4120 4121 nhg = rcu_dereference(nexthop->nh_grp); 4122 if (!nhg->resilient) 4123 goto out; 4124 4125 /* Instead of silently ignoring some buckets, demand that the sizes 4126 * be the same. 4127 */ 4128 res_table = rcu_dereference(nhg->res_table); 4129 if (num_buckets != res_table->num_nh_buckets) 4130 goto out; 4131 4132 for (i = 0; i < num_buckets; i++) { 4133 if (test_bit(i, activity)) 4134 nh_res_bucket_set_busy(&res_table->nh_buckets[i]); 4135 } 4136 4137 out: 4138 rcu_read_unlock(); 4139 } 4140 EXPORT_SYMBOL(nexthop_res_grp_activity_update); 4141 4142 static void __net_exit nexthop_net_exit_rtnl(struct net *net, 4143 struct list_head *dev_to_kill) 4144 { 4145 ASSERT_RTNL_NET(net); 4146 flush_all_nexthops(net); 4147 } 4148 4149 static void __net_exit nexthop_net_exit(struct net *net) 4150 { 4151 kfree(net->nexthop.devhash); 4152 net->nexthop.devhash = NULL; 4153 } 4154 4155 static int __net_init nexthop_net_init(struct net *net) 4156 { 4157 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 4158 4159 net->nexthop.rb_root = RB_ROOT; 4160 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 4161 if (!net->nexthop.devhash) 4162 return -ENOMEM; 4163 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 4164 4165 return 0; 4166 } 4167 4168 static struct pernet_operations nexthop_net_ops = { 4169 .init = nexthop_net_init, 4170 .exit = nexthop_net_exit, 4171 .exit_rtnl = nexthop_net_exit_rtnl, 4172 }; 4173 4174 static const struct rtnl_msg_handler nexthop_rtnl_msg_handlers[] __initconst = { 4175 {.msgtype = RTM_NEWNEXTHOP, .doit = rtm_new_nexthop, 4176 .flags = RTNL_FLAG_DOIT_PERNET}, 4177 {.msgtype = RTM_DELNEXTHOP, .doit = rtm_del_nexthop, 4178 .flags = RTNL_FLAG_DOIT_PERNET}, 4179 {.msgtype = RTM_GETNEXTHOP, .doit = rtm_get_nexthop, 4180 .dumpit = rtm_dump_nexthop}, 4181 {.msgtype = RTM_GETNEXTHOPBUCKET, .doit = rtm_get_nexthop_bucket, 4182 .dumpit = rtm_dump_nexthop_bucket}, 4183 {.protocol = PF_INET, .msgtype = RTM_NEWNEXTHOP, 4184 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET}, 4185 {.protocol = PF_INET, .msgtype = RTM_GETNEXTHOP, 4186 .dumpit = rtm_dump_nexthop}, 4187 {.protocol = PF_INET6, .msgtype = RTM_NEWNEXTHOP, 4188 .doit = rtm_new_nexthop, .flags = RTNL_FLAG_DOIT_PERNET}, 4189 {.protocol = PF_INET6, .msgtype = RTM_GETNEXTHOP, 4190 .dumpit = rtm_dump_nexthop}, 4191 }; 4192 4193 static int __init nexthop_init(void) 4194 { 4195 register_pernet_subsys(&nexthop_net_ops); 4196 4197 register_netdevice_notifier(&nh_netdev_notifier); 4198 4199 rtnl_register_many(nexthop_rtnl_msg_handlers); 4200 4201 return 0; 4202 } 4203 subsys_initcall(nexthop_init); 4204