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