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