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