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 <net/arp.h> 12 #include <net/ipv6_stubs.h> 13 #include <net/lwtunnel.h> 14 #include <net/ndisc.h> 15 #include <net/nexthop.h> 16 #include <net/route.h> 17 #include <net/sock.h> 18 19 static void remove_nexthop(struct net *net, struct nexthop *nh, 20 struct nl_info *nlinfo); 21 22 #define NH_DEV_HASHBITS 8 23 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS) 24 25 static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = { 26 [NHA_ID] = { .type = NLA_U32 }, 27 [NHA_GROUP] = { .type = NLA_BINARY }, 28 [NHA_GROUP_TYPE] = { .type = NLA_U16 }, 29 [NHA_BLACKHOLE] = { .type = NLA_FLAG }, 30 [NHA_OIF] = { .type = NLA_U32 }, 31 [NHA_GATEWAY] = { .type = NLA_BINARY }, 32 [NHA_ENCAP_TYPE] = { .type = NLA_U16 }, 33 [NHA_ENCAP] = { .type = NLA_NESTED }, 34 [NHA_GROUPS] = { .type = NLA_FLAG }, 35 [NHA_MASTER] = { .type = NLA_U32 }, 36 [NHA_FDB] = { .type = NLA_FLAG }, 37 }; 38 39 static bool nexthop_notifiers_is_empty(struct net *net) 40 { 41 return !net->nexthop.notifier_chain.head; 42 } 43 44 static void 45 __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info, 46 const struct nexthop *nh) 47 { 48 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 49 50 nh_info->dev = nhi->fib_nhc.nhc_dev; 51 nh_info->gw_family = nhi->fib_nhc.nhc_gw_family; 52 if (nh_info->gw_family == AF_INET) 53 nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4; 54 else if (nh_info->gw_family == AF_INET6) 55 nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6; 56 57 nh_info->is_reject = nhi->reject_nh; 58 nh_info->is_fdb = nhi->fdb_nh; 59 nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate; 60 } 61 62 static int nh_notifier_single_info_init(struct nh_notifier_info *info, 63 const struct nexthop *nh) 64 { 65 info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL); 66 if (!info->nh) 67 return -ENOMEM; 68 69 __nh_notifier_single_info_init(info->nh, nh); 70 71 return 0; 72 } 73 74 static void nh_notifier_single_info_fini(struct nh_notifier_info *info) 75 { 76 kfree(info->nh); 77 } 78 79 static int nh_notifier_grp_info_init(struct nh_notifier_info *info, 80 const struct nexthop *nh) 81 { 82 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 83 u16 num_nh = nhg->num_nh; 84 int i; 85 86 info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh), 87 GFP_KERNEL); 88 if (!info->nh_grp) 89 return -ENOMEM; 90 91 info->nh_grp->num_nh = num_nh; 92 info->nh_grp->is_fdb = nhg->fdb_nh; 93 94 for (i = 0; i < num_nh; i++) { 95 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 96 97 info->nh_grp->nh_entries[i].id = nhge->nh->id; 98 info->nh_grp->nh_entries[i].weight = nhge->weight; 99 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh, 100 nhge->nh); 101 } 102 103 return 0; 104 } 105 106 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info) 107 { 108 kfree(info->nh_grp); 109 } 110 111 static int nh_notifier_info_init(struct nh_notifier_info *info, 112 const struct nexthop *nh) 113 { 114 info->id = nh->id; 115 info->is_grp = nh->is_group; 116 117 if (info->is_grp) 118 return nh_notifier_grp_info_init(info, nh); 119 else 120 return nh_notifier_single_info_init(info, nh); 121 } 122 123 static void nh_notifier_info_fini(struct nh_notifier_info *info) 124 { 125 if (info->is_grp) 126 nh_notifier_grp_info_fini(info); 127 else 128 nh_notifier_single_info_fini(info); 129 } 130 131 static int call_nexthop_notifiers(struct net *net, 132 enum nexthop_event_type event_type, 133 struct nexthop *nh, 134 struct netlink_ext_ack *extack) 135 { 136 struct nh_notifier_info info = { 137 .net = net, 138 .extack = extack, 139 }; 140 int err; 141 142 ASSERT_RTNL(); 143 144 if (nexthop_notifiers_is_empty(net)) 145 return 0; 146 147 err = nh_notifier_info_init(&info, nh); 148 if (err) { 149 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 150 return err; 151 } 152 153 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 154 event_type, &info); 155 nh_notifier_info_fini(&info); 156 157 return notifier_to_errno(err); 158 } 159 160 static unsigned int nh_dev_hashfn(unsigned int val) 161 { 162 unsigned int mask = NH_DEV_HASHSIZE - 1; 163 164 return (val ^ 165 (val >> NH_DEV_HASHBITS) ^ 166 (val >> (NH_DEV_HASHBITS * 2))) & mask; 167 } 168 169 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi) 170 { 171 struct net_device *dev = nhi->fib_nhc.nhc_dev; 172 struct hlist_head *head; 173 unsigned int hash; 174 175 WARN_ON(!dev); 176 177 hash = nh_dev_hashfn(dev->ifindex); 178 head = &net->nexthop.devhash[hash]; 179 hlist_add_head(&nhi->dev_hash, head); 180 } 181 182 static void nexthop_free_mpath(struct nexthop *nh) 183 { 184 struct nh_group *nhg; 185 int i; 186 187 nhg = rcu_dereference_raw(nh->nh_grp); 188 for (i = 0; i < nhg->num_nh; ++i) { 189 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 190 191 WARN_ON(!list_empty(&nhge->nh_list)); 192 nexthop_put(nhge->nh); 193 } 194 195 WARN_ON(nhg->spare == nhg); 196 197 kfree(nhg->spare); 198 kfree(nhg); 199 } 200 201 static void nexthop_free_single(struct nexthop *nh) 202 { 203 struct nh_info *nhi; 204 205 nhi = rcu_dereference_raw(nh->nh_info); 206 switch (nhi->family) { 207 case AF_INET: 208 fib_nh_release(nh->net, &nhi->fib_nh); 209 break; 210 case AF_INET6: 211 ipv6_stub->fib6_nh_release(&nhi->fib6_nh); 212 break; 213 } 214 kfree(nhi); 215 } 216 217 void nexthop_free_rcu(struct rcu_head *head) 218 { 219 struct nexthop *nh = container_of(head, struct nexthop, rcu); 220 221 if (nh->is_group) 222 nexthop_free_mpath(nh); 223 else 224 nexthop_free_single(nh); 225 226 kfree(nh); 227 } 228 EXPORT_SYMBOL_GPL(nexthop_free_rcu); 229 230 static struct nexthop *nexthop_alloc(void) 231 { 232 struct nexthop *nh; 233 234 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL); 235 if (nh) { 236 INIT_LIST_HEAD(&nh->fi_list); 237 INIT_LIST_HEAD(&nh->f6i_list); 238 INIT_LIST_HEAD(&nh->grp_list); 239 INIT_LIST_HEAD(&nh->fdb_list); 240 } 241 return nh; 242 } 243 244 static struct nh_group *nexthop_grp_alloc(u16 num_nh) 245 { 246 struct nh_group *nhg; 247 248 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL); 249 if (nhg) 250 nhg->num_nh = num_nh; 251 252 return nhg; 253 } 254 255 static void nh_base_seq_inc(struct net *net) 256 { 257 while (++net->nexthop.seq == 0) 258 ; 259 } 260 261 /* no reference taken; rcu lock or rtnl must be held */ 262 struct nexthop *nexthop_find_by_id(struct net *net, u32 id) 263 { 264 struct rb_node **pp, *parent = NULL, *next; 265 266 pp = &net->nexthop.rb_root.rb_node; 267 while (1) { 268 struct nexthop *nh; 269 270 next = rcu_dereference_raw(*pp); 271 if (!next) 272 break; 273 parent = next; 274 275 nh = rb_entry(parent, struct nexthop, rb_node); 276 if (id < nh->id) 277 pp = &next->rb_left; 278 else if (id > nh->id) 279 pp = &next->rb_right; 280 else 281 return nh; 282 } 283 return NULL; 284 } 285 EXPORT_SYMBOL_GPL(nexthop_find_by_id); 286 287 /* used for auto id allocation; called with rtnl held */ 288 static u32 nh_find_unused_id(struct net *net) 289 { 290 u32 id_start = net->nexthop.last_id_allocated; 291 292 while (1) { 293 net->nexthop.last_id_allocated++; 294 if (net->nexthop.last_id_allocated == id_start) 295 break; 296 297 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated)) 298 return net->nexthop.last_id_allocated; 299 } 300 return 0; 301 } 302 303 static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg) 304 { 305 struct nexthop_grp *p; 306 size_t len = nhg->num_nh * sizeof(*p); 307 struct nlattr *nla; 308 u16 group_type = 0; 309 int i; 310 311 if (nhg->mpath) 312 group_type = NEXTHOP_GRP_TYPE_MPATH; 313 314 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type)) 315 goto nla_put_failure; 316 317 nla = nla_reserve(skb, NHA_GROUP, len); 318 if (!nla) 319 goto nla_put_failure; 320 321 p = nla_data(nla); 322 for (i = 0; i < nhg->num_nh; ++i) { 323 p->id = nhg->nh_entries[i].nh->id; 324 p->weight = nhg->nh_entries[i].weight - 1; 325 p += 1; 326 } 327 328 return 0; 329 330 nla_put_failure: 331 return -EMSGSIZE; 332 } 333 334 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh, 335 int event, u32 portid, u32 seq, unsigned int nlflags) 336 { 337 struct fib6_nh *fib6_nh; 338 struct fib_nh *fib_nh; 339 struct nlmsghdr *nlh; 340 struct nh_info *nhi; 341 struct nhmsg *nhm; 342 343 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 344 if (!nlh) 345 return -EMSGSIZE; 346 347 nhm = nlmsg_data(nlh); 348 nhm->nh_family = AF_UNSPEC; 349 nhm->nh_flags = nh->nh_flags; 350 nhm->nh_protocol = nh->protocol; 351 nhm->nh_scope = 0; 352 nhm->resvd = 0; 353 354 if (nla_put_u32(skb, NHA_ID, nh->id)) 355 goto nla_put_failure; 356 357 if (nh->is_group) { 358 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 359 360 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB)) 361 goto nla_put_failure; 362 if (nla_put_nh_group(skb, nhg)) 363 goto nla_put_failure; 364 goto out; 365 } 366 367 nhi = rtnl_dereference(nh->nh_info); 368 nhm->nh_family = nhi->family; 369 if (nhi->reject_nh) { 370 if (nla_put_flag(skb, NHA_BLACKHOLE)) 371 goto nla_put_failure; 372 goto out; 373 } else if (nhi->fdb_nh) { 374 if (nla_put_flag(skb, NHA_FDB)) 375 goto nla_put_failure; 376 } else { 377 const struct net_device *dev; 378 379 dev = nhi->fib_nhc.nhc_dev; 380 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex)) 381 goto nla_put_failure; 382 } 383 384 nhm->nh_scope = nhi->fib_nhc.nhc_scope; 385 switch (nhi->family) { 386 case AF_INET: 387 fib_nh = &nhi->fib_nh; 388 if (fib_nh->fib_nh_gw_family && 389 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4)) 390 goto nla_put_failure; 391 break; 392 393 case AF_INET6: 394 fib6_nh = &nhi->fib6_nh; 395 if (fib6_nh->fib_nh_gw_family && 396 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6)) 397 goto nla_put_failure; 398 break; 399 } 400 401 if (nhi->fib_nhc.nhc_lwtstate && 402 lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate, 403 NHA_ENCAP, NHA_ENCAP_TYPE) < 0) 404 goto nla_put_failure; 405 406 out: 407 nlmsg_end(skb, nlh); 408 return 0; 409 410 nla_put_failure: 411 nlmsg_cancel(skb, nlh); 412 return -EMSGSIZE; 413 } 414 415 static size_t nh_nlmsg_size_grp(struct nexthop *nh) 416 { 417 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 418 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh; 419 420 return nla_total_size(sz) + 421 nla_total_size(2); /* NHA_GROUP_TYPE */ 422 } 423 424 static size_t nh_nlmsg_size_single(struct nexthop *nh) 425 { 426 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 427 size_t sz; 428 429 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE 430 * are mutually exclusive 431 */ 432 sz = nla_total_size(4); /* NHA_OIF */ 433 434 switch (nhi->family) { 435 case AF_INET: 436 if (nhi->fib_nh.fib_nh_gw_family) 437 sz += nla_total_size(4); /* NHA_GATEWAY */ 438 break; 439 440 case AF_INET6: 441 /* NHA_GATEWAY */ 442 if (nhi->fib6_nh.fib_nh_gw_family) 443 sz += nla_total_size(sizeof(const struct in6_addr)); 444 break; 445 } 446 447 if (nhi->fib_nhc.nhc_lwtstate) { 448 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate); 449 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */ 450 } 451 452 return sz; 453 } 454 455 static size_t nh_nlmsg_size(struct nexthop *nh) 456 { 457 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg)); 458 459 sz += nla_total_size(4); /* NHA_ID */ 460 461 if (nh->is_group) 462 sz += nh_nlmsg_size_grp(nh); 463 else 464 sz += nh_nlmsg_size_single(nh); 465 466 return sz; 467 } 468 469 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info) 470 { 471 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0; 472 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 473 struct sk_buff *skb; 474 int err = -ENOBUFS; 475 476 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any()); 477 if (!skb) 478 goto errout; 479 480 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags); 481 if (err < 0) { 482 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */ 483 WARN_ON(err == -EMSGSIZE); 484 kfree_skb(skb); 485 goto errout; 486 } 487 488 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP, 489 info->nlh, gfp_any()); 490 return; 491 errout: 492 if (err < 0) 493 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err); 494 } 495 496 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths, 497 bool *is_fdb, struct netlink_ext_ack *extack) 498 { 499 if (nh->is_group) { 500 struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 501 502 /* nested multipath (group within a group) is not 503 * supported 504 */ 505 if (nhg->mpath) { 506 NL_SET_ERR_MSG(extack, 507 "Multipath group can not be a nexthop within a group"); 508 return false; 509 } 510 *is_fdb = nhg->fdb_nh; 511 } else { 512 struct nh_info *nhi = rtnl_dereference(nh->nh_info); 513 514 if (nhi->reject_nh && npaths > 1) { 515 NL_SET_ERR_MSG(extack, 516 "Blackhole nexthop can not be used in a group with more than 1 path"); 517 return false; 518 } 519 *is_fdb = nhi->fdb_nh; 520 } 521 522 return true; 523 } 524 525 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family, 526 struct netlink_ext_ack *extack) 527 { 528 struct nh_info *nhi; 529 530 nhi = rtnl_dereference(nh->nh_info); 531 532 if (!nhi->fdb_nh) { 533 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops"); 534 return -EINVAL; 535 } 536 537 if (*nh_family == AF_UNSPEC) { 538 *nh_family = nhi->family; 539 } else if (*nh_family != nhi->family) { 540 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops"); 541 return -EINVAL; 542 } 543 544 return 0; 545 } 546 547 static int nh_check_attr_group(struct net *net, struct nlattr *tb[], 548 struct netlink_ext_ack *extack) 549 { 550 unsigned int len = nla_len(tb[NHA_GROUP]); 551 u8 nh_family = AF_UNSPEC; 552 struct nexthop_grp *nhg; 553 unsigned int i, j; 554 u8 nhg_fdb = 0; 555 556 if (!len || len & (sizeof(struct nexthop_grp) - 1)) { 557 NL_SET_ERR_MSG(extack, 558 "Invalid length for nexthop group attribute"); 559 return -EINVAL; 560 } 561 562 /* convert len to number of nexthop ids */ 563 len /= sizeof(*nhg); 564 565 nhg = nla_data(tb[NHA_GROUP]); 566 for (i = 0; i < len; ++i) { 567 if (nhg[i].resvd1 || nhg[i].resvd2) { 568 NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0"); 569 return -EINVAL; 570 } 571 if (nhg[i].weight > 254) { 572 NL_SET_ERR_MSG(extack, "Invalid value for weight"); 573 return -EINVAL; 574 } 575 for (j = i + 1; j < len; ++j) { 576 if (nhg[i].id == nhg[j].id) { 577 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group"); 578 return -EINVAL; 579 } 580 } 581 } 582 583 if (tb[NHA_FDB]) 584 nhg_fdb = 1; 585 nhg = nla_data(tb[NHA_GROUP]); 586 for (i = 0; i < len; ++i) { 587 struct nexthop *nh; 588 bool is_fdb_nh; 589 590 nh = nexthop_find_by_id(net, nhg[i].id); 591 if (!nh) { 592 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 593 return -EINVAL; 594 } 595 if (!valid_group_nh(nh, len, &is_fdb_nh, extack)) 596 return -EINVAL; 597 598 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack)) 599 return -EINVAL; 600 601 if (!nhg_fdb && is_fdb_nh) { 602 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops"); 603 return -EINVAL; 604 } 605 } 606 for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) { 607 if (!tb[i]) 608 continue; 609 if (tb[NHA_FDB]) 610 continue; 611 NL_SET_ERR_MSG(extack, 612 "No other attributes can be set in nexthop groups"); 613 return -EINVAL; 614 } 615 616 return 0; 617 } 618 619 static bool ipv6_good_nh(const struct fib6_nh *nh) 620 { 621 int state = NUD_REACHABLE; 622 struct neighbour *n; 623 624 rcu_read_lock_bh(); 625 626 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6); 627 if (n) 628 state = n->nud_state; 629 630 rcu_read_unlock_bh(); 631 632 return !!(state & NUD_VALID); 633 } 634 635 static bool ipv4_good_nh(const struct fib_nh *nh) 636 { 637 int state = NUD_REACHABLE; 638 struct neighbour *n; 639 640 rcu_read_lock_bh(); 641 642 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, 643 (__force u32)nh->fib_nh_gw4); 644 if (n) 645 state = n->nud_state; 646 647 rcu_read_unlock_bh(); 648 649 return !!(state & NUD_VALID); 650 } 651 652 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash) 653 { 654 struct nexthop *rc = NULL; 655 struct nh_group *nhg; 656 int i; 657 658 if (!nh->is_group) 659 return nh; 660 661 nhg = rcu_dereference(nh->nh_grp); 662 for (i = 0; i < nhg->num_nh; ++i) { 663 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 664 struct nh_info *nhi; 665 666 if (hash > atomic_read(&nhge->upper_bound)) 667 continue; 668 669 nhi = rcu_dereference(nhge->nh->nh_info); 670 if (nhi->fdb_nh) 671 return nhge->nh; 672 673 /* nexthops always check if it is good and does 674 * not rely on a sysctl for this behavior 675 */ 676 switch (nhi->family) { 677 case AF_INET: 678 if (ipv4_good_nh(&nhi->fib_nh)) 679 return nhge->nh; 680 break; 681 case AF_INET6: 682 if (ipv6_good_nh(&nhi->fib6_nh)) 683 return nhge->nh; 684 break; 685 } 686 687 if (!rc) 688 rc = nhge->nh; 689 } 690 691 return rc; 692 } 693 EXPORT_SYMBOL_GPL(nexthop_select_path); 694 695 int nexthop_for_each_fib6_nh(struct nexthop *nh, 696 int (*cb)(struct fib6_nh *nh, void *arg), 697 void *arg) 698 { 699 struct nh_info *nhi; 700 int err; 701 702 if (nh->is_group) { 703 struct nh_group *nhg; 704 int i; 705 706 nhg = rcu_dereference_rtnl(nh->nh_grp); 707 for (i = 0; i < nhg->num_nh; i++) { 708 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 709 710 nhi = rcu_dereference_rtnl(nhge->nh->nh_info); 711 err = cb(&nhi->fib6_nh, arg); 712 if (err) 713 return err; 714 } 715 } else { 716 nhi = rcu_dereference_rtnl(nh->nh_info); 717 err = cb(&nhi->fib6_nh, arg); 718 if (err) 719 return err; 720 } 721 722 return 0; 723 } 724 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh); 725 726 static int check_src_addr(const struct in6_addr *saddr, 727 struct netlink_ext_ack *extack) 728 { 729 if (!ipv6_addr_any(saddr)) { 730 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects"); 731 return -EINVAL; 732 } 733 return 0; 734 } 735 736 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, 737 struct netlink_ext_ack *extack) 738 { 739 struct nh_info *nhi; 740 bool is_fdb_nh; 741 742 /* fib6_src is unique to a fib6_info and limits the ability to cache 743 * routes in fib6_nh within a nexthop that is potentially shared 744 * across multiple fib entries. If the config wants to use source 745 * routing it can not use nexthop objects. mlxsw also does not allow 746 * fib6_src on routes. 747 */ 748 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0) 749 return -EINVAL; 750 751 if (nh->is_group) { 752 struct nh_group *nhg; 753 754 nhg = rtnl_dereference(nh->nh_grp); 755 if (nhg->has_v4) 756 goto no_v4_nh; 757 is_fdb_nh = nhg->fdb_nh; 758 } else { 759 nhi = rtnl_dereference(nh->nh_info); 760 if (nhi->family == AF_INET) 761 goto no_v4_nh; 762 is_fdb_nh = nhi->fdb_nh; 763 } 764 765 if (is_fdb_nh) { 766 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 767 return -EINVAL; 768 } 769 770 return 0; 771 no_v4_nh: 772 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop"); 773 return -EINVAL; 774 } 775 EXPORT_SYMBOL_GPL(fib6_check_nexthop); 776 777 /* if existing nexthop has ipv6 routes linked to it, need 778 * to verify this new spec works with ipv6 779 */ 780 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new, 781 struct netlink_ext_ack *extack) 782 { 783 struct fib6_info *f6i; 784 785 if (list_empty(&old->f6i_list)) 786 return 0; 787 788 list_for_each_entry(f6i, &old->f6i_list, nh_list) { 789 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0) 790 return -EINVAL; 791 } 792 793 return fib6_check_nexthop(new, NULL, extack); 794 } 795 796 static int nexthop_check_scope(struct nh_info *nhi, u8 scope, 797 struct netlink_ext_ack *extack) 798 { 799 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) { 800 NL_SET_ERR_MSG(extack, 801 "Route with host scope can not have a gateway"); 802 return -EINVAL; 803 } 804 805 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) { 806 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop"); 807 return -EINVAL; 808 } 809 810 return 0; 811 } 812 813 /* Invoked by fib add code to verify nexthop by id is ok with 814 * config for prefix; parts of fib_check_nh not done when nexthop 815 * object is used. 816 */ 817 int fib_check_nexthop(struct nexthop *nh, u8 scope, 818 struct netlink_ext_ack *extack) 819 { 820 struct nh_info *nhi; 821 int err = 0; 822 823 if (nh->is_group) { 824 struct nh_group *nhg; 825 826 nhg = rtnl_dereference(nh->nh_grp); 827 if (nhg->fdb_nh) { 828 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 829 err = -EINVAL; 830 goto out; 831 } 832 833 if (scope == RT_SCOPE_HOST) { 834 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops"); 835 err = -EINVAL; 836 goto out; 837 } 838 839 /* all nexthops in a group have the same scope */ 840 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info); 841 err = nexthop_check_scope(nhi, scope, extack); 842 } else { 843 nhi = rtnl_dereference(nh->nh_info); 844 if (nhi->fdb_nh) { 845 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 846 err = -EINVAL; 847 goto out; 848 } 849 err = nexthop_check_scope(nhi, scope, extack); 850 } 851 852 out: 853 return err; 854 } 855 856 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new, 857 struct netlink_ext_ack *extack) 858 { 859 struct fib_info *fi; 860 861 list_for_each_entry(fi, &old->fi_list, nh_list) { 862 int err; 863 864 err = fib_check_nexthop(new, fi->fib_scope, extack); 865 if (err) 866 return err; 867 } 868 return 0; 869 } 870 871 static void nh_group_rebalance(struct nh_group *nhg) 872 { 873 int total = 0; 874 int w = 0; 875 int i; 876 877 for (i = 0; i < nhg->num_nh; ++i) 878 total += nhg->nh_entries[i].weight; 879 880 for (i = 0; i < nhg->num_nh; ++i) { 881 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 882 int upper_bound; 883 884 w += nhge->weight; 885 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1; 886 atomic_set(&nhge->upper_bound, upper_bound); 887 } 888 } 889 890 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, 891 struct nl_info *nlinfo) 892 { 893 struct nh_grp_entry *nhges, *new_nhges; 894 struct nexthop *nhp = nhge->nh_parent; 895 struct nexthop *nh = nhge->nh; 896 struct nh_group *nhg, *newg; 897 int i, j; 898 899 WARN_ON(!nh); 900 901 nhg = rtnl_dereference(nhp->nh_grp); 902 newg = nhg->spare; 903 904 /* last entry, keep it visible and remove the parent */ 905 if (nhg->num_nh == 1) { 906 remove_nexthop(net, nhp, nlinfo); 907 return; 908 } 909 910 newg->has_v4 = false; 911 newg->mpath = nhg->mpath; 912 newg->fdb_nh = nhg->fdb_nh; 913 newg->num_nh = nhg->num_nh; 914 915 /* copy old entries to new except the one getting removed */ 916 nhges = nhg->nh_entries; 917 new_nhges = newg->nh_entries; 918 for (i = 0, j = 0; i < nhg->num_nh; ++i) { 919 struct nh_info *nhi; 920 921 /* current nexthop getting removed */ 922 if (nhg->nh_entries[i].nh == nh) { 923 newg->num_nh--; 924 continue; 925 } 926 927 nhi = rtnl_dereference(nhges[i].nh->nh_info); 928 if (nhi->family == AF_INET) 929 newg->has_v4 = true; 930 931 list_del(&nhges[i].nh_list); 932 new_nhges[j].nh_parent = nhges[i].nh_parent; 933 new_nhges[j].nh = nhges[i].nh; 934 new_nhges[j].weight = nhges[i].weight; 935 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); 936 j++; 937 } 938 939 nh_group_rebalance(newg); 940 rcu_assign_pointer(nhp->nh_grp, newg); 941 942 list_del(&nhge->nh_list); 943 nexthop_put(nhge->nh); 944 945 if (nlinfo) 946 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); 947 } 948 949 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh, 950 struct nl_info *nlinfo) 951 { 952 struct nh_grp_entry *nhge, *tmp; 953 954 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) 955 remove_nh_grp_entry(net, nhge, nlinfo); 956 957 /* make sure all see the newly published array before releasing rtnl */ 958 synchronize_net(); 959 } 960 961 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) 962 { 963 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 964 int i, num_nh = nhg->num_nh; 965 966 for (i = 0; i < num_nh; ++i) { 967 struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 968 969 if (WARN_ON(!nhge->nh)) 970 continue; 971 972 list_del_init(&nhge->nh_list); 973 } 974 } 975 976 /* not called for nexthop replace */ 977 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh) 978 { 979 struct fib6_info *f6i, *tmp; 980 bool do_flush = false; 981 struct fib_info *fi; 982 983 list_for_each_entry(fi, &nh->fi_list, nh_list) { 984 fi->fib_flags |= RTNH_F_DEAD; 985 do_flush = true; 986 } 987 if (do_flush) 988 fib_flush(net); 989 990 /* ip6_del_rt removes the entry from this list hence the _safe */ 991 list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) { 992 /* __ip6_del_rt does a release, so do a hold here */ 993 fib6_info_hold(f6i); 994 ipv6_stub->ip6_del_rt(net, f6i, 995 !net->ipv4.sysctl_nexthop_compat_mode); 996 } 997 } 998 999 static void __remove_nexthop(struct net *net, struct nexthop *nh, 1000 struct nl_info *nlinfo) 1001 { 1002 __remove_nexthop_fib(net, nh); 1003 1004 if (nh->is_group) { 1005 remove_nexthop_group(nh, nlinfo); 1006 } else { 1007 struct nh_info *nhi; 1008 1009 nhi = rtnl_dereference(nh->nh_info); 1010 if (nhi->fib_nhc.nhc_dev) 1011 hlist_del(&nhi->dev_hash); 1012 1013 remove_nexthop_from_groups(net, nh, nlinfo); 1014 } 1015 } 1016 1017 static void remove_nexthop(struct net *net, struct nexthop *nh, 1018 struct nl_info *nlinfo) 1019 { 1020 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL); 1021 1022 /* remove from the tree */ 1023 rb_erase(&nh->rb_node, &net->nexthop.rb_root); 1024 1025 if (nlinfo) 1026 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); 1027 1028 __remove_nexthop(net, nh, nlinfo); 1029 nh_base_seq_inc(net); 1030 1031 nexthop_put(nh); 1032 } 1033 1034 /* if any FIB entries reference this nexthop, any dst entries 1035 * need to be regenerated 1036 */ 1037 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh) 1038 { 1039 struct fib6_info *f6i; 1040 1041 if (!list_empty(&nh->fi_list)) 1042 rt_cache_flush(net); 1043 1044 list_for_each_entry(f6i, &nh->f6i_list, nh_list) 1045 ipv6_stub->fib6_update_sernum(net, f6i); 1046 } 1047 1048 static int replace_nexthop_grp(struct net *net, struct nexthop *old, 1049 struct nexthop *new, 1050 struct netlink_ext_ack *extack) 1051 { 1052 struct nh_group *oldg, *newg; 1053 int i; 1054 1055 if (!new->is_group) { 1056 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); 1057 return -EINVAL; 1058 } 1059 1060 oldg = rtnl_dereference(old->nh_grp); 1061 newg = rtnl_dereference(new->nh_grp); 1062 1063 /* update parents - used by nexthop code for cleanup */ 1064 for (i = 0; i < newg->num_nh; i++) 1065 newg->nh_entries[i].nh_parent = old; 1066 1067 rcu_assign_pointer(old->nh_grp, newg); 1068 1069 for (i = 0; i < oldg->num_nh; i++) 1070 oldg->nh_entries[i].nh_parent = new; 1071 1072 rcu_assign_pointer(new->nh_grp, oldg); 1073 1074 return 0; 1075 } 1076 1077 static void nh_group_v4_update(struct nh_group *nhg) 1078 { 1079 struct nh_grp_entry *nhges; 1080 bool has_v4 = false; 1081 int i; 1082 1083 nhges = nhg->nh_entries; 1084 for (i = 0; i < nhg->num_nh; i++) { 1085 struct nh_info *nhi; 1086 1087 nhi = rtnl_dereference(nhges[i].nh->nh_info); 1088 if (nhi->family == AF_INET) 1089 has_v4 = true; 1090 } 1091 nhg->has_v4 = has_v4; 1092 } 1093 1094 static int replace_nexthop_single(struct net *net, struct nexthop *old, 1095 struct nexthop *new, 1096 struct netlink_ext_ack *extack) 1097 { 1098 struct nh_info *oldi, *newi; 1099 1100 if (new->is_group) { 1101 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); 1102 return -EINVAL; 1103 } 1104 1105 oldi = rtnl_dereference(old->nh_info); 1106 newi = rtnl_dereference(new->nh_info); 1107 1108 newi->nh_parent = old; 1109 oldi->nh_parent = new; 1110 1111 old->protocol = new->protocol; 1112 old->nh_flags = new->nh_flags; 1113 1114 rcu_assign_pointer(old->nh_info, newi); 1115 rcu_assign_pointer(new->nh_info, oldi); 1116 1117 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially 1118 * update IPv4 indication in all the groups using the nexthop. 1119 */ 1120 if (oldi->family == AF_INET && newi->family == AF_INET6) { 1121 struct nh_grp_entry *nhge; 1122 1123 list_for_each_entry(nhge, &old->grp_list, nh_list) { 1124 struct nexthop *nhp = nhge->nh_parent; 1125 struct nh_group *nhg; 1126 1127 nhg = rtnl_dereference(nhp->nh_grp); 1128 nh_group_v4_update(nhg); 1129 } 1130 } 1131 1132 return 0; 1133 } 1134 1135 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh, 1136 struct nl_info *info) 1137 { 1138 struct fib6_info *f6i; 1139 1140 if (!list_empty(&nh->fi_list)) { 1141 struct fib_info *fi; 1142 1143 /* expectation is a few fib_info per nexthop and then 1144 * a lot of routes per fib_info. So mark the fib_info 1145 * and then walk the fib tables once 1146 */ 1147 list_for_each_entry(fi, &nh->fi_list, nh_list) 1148 fi->nh_updated = true; 1149 1150 fib_info_notify_update(net, info); 1151 1152 list_for_each_entry(fi, &nh->fi_list, nh_list) 1153 fi->nh_updated = false; 1154 } 1155 1156 list_for_each_entry(f6i, &nh->f6i_list, nh_list) 1157 ipv6_stub->fib6_rt_update(net, f6i, info); 1158 } 1159 1160 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries 1161 * linked to this nexthop and for all groups that the nexthop 1162 * is a member of 1163 */ 1164 static void nexthop_replace_notify(struct net *net, struct nexthop *nh, 1165 struct nl_info *info) 1166 { 1167 struct nh_grp_entry *nhge; 1168 1169 __nexthop_replace_notify(net, nh, info); 1170 1171 list_for_each_entry(nhge, &nh->grp_list, nh_list) 1172 __nexthop_replace_notify(net, nhge->nh_parent, info); 1173 } 1174 1175 static int replace_nexthop(struct net *net, struct nexthop *old, 1176 struct nexthop *new, struct netlink_ext_ack *extack) 1177 { 1178 bool new_is_reject = false; 1179 struct nh_grp_entry *nhge; 1180 int err; 1181 1182 /* check that existing FIB entries are ok with the 1183 * new nexthop definition 1184 */ 1185 err = fib_check_nh_list(old, new, extack); 1186 if (err) 1187 return err; 1188 1189 err = fib6_check_nh_list(old, new, extack); 1190 if (err) 1191 return err; 1192 1193 if (!new->is_group) { 1194 struct nh_info *nhi = rtnl_dereference(new->nh_info); 1195 1196 new_is_reject = nhi->reject_nh; 1197 } 1198 1199 list_for_each_entry(nhge, &old->grp_list, nh_list) { 1200 /* if new nexthop is a blackhole, any groups using this 1201 * nexthop cannot have more than 1 path 1202 */ 1203 if (new_is_reject && 1204 nexthop_num_path(nhge->nh_parent) > 1) { 1205 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); 1206 return -EINVAL; 1207 } 1208 1209 err = fib_check_nh_list(nhge->nh_parent, new, extack); 1210 if (err) 1211 return err; 1212 1213 err = fib6_check_nh_list(nhge->nh_parent, new, extack); 1214 if (err) 1215 return err; 1216 } 1217 1218 if (old->is_group) 1219 err = replace_nexthop_grp(net, old, new, extack); 1220 else 1221 err = replace_nexthop_single(net, old, new, extack); 1222 1223 if (!err) { 1224 nh_rt_cache_flush(net, old); 1225 1226 __remove_nexthop(net, new, NULL); 1227 nexthop_put(new); 1228 } 1229 1230 return err; 1231 } 1232 1233 /* called with rtnl_lock held */ 1234 static int insert_nexthop(struct net *net, struct nexthop *new_nh, 1235 struct nh_config *cfg, struct netlink_ext_ack *extack) 1236 { 1237 struct rb_node **pp, *parent = NULL, *next; 1238 struct rb_root *root = &net->nexthop.rb_root; 1239 bool replace = !!(cfg->nlflags & NLM_F_REPLACE); 1240 bool create = !!(cfg->nlflags & NLM_F_CREATE); 1241 u32 new_id = new_nh->id; 1242 int replace_notify = 0; 1243 int rc = -EEXIST; 1244 1245 pp = &root->rb_node; 1246 while (1) { 1247 struct nexthop *nh; 1248 1249 next = *pp; 1250 if (!next) 1251 break; 1252 1253 parent = next; 1254 1255 nh = rb_entry(parent, struct nexthop, rb_node); 1256 if (new_id < nh->id) { 1257 pp = &next->rb_left; 1258 } else if (new_id > nh->id) { 1259 pp = &next->rb_right; 1260 } else if (replace) { 1261 rc = replace_nexthop(net, nh, new_nh, extack); 1262 if (!rc) { 1263 new_nh = nh; /* send notification with old nh */ 1264 replace_notify = 1; 1265 } 1266 goto out; 1267 } else { 1268 /* id already exists and not a replace */ 1269 goto out; 1270 } 1271 } 1272 1273 if (replace && !create) { 1274 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); 1275 rc = -ENOENT; 1276 goto out; 1277 } 1278 1279 rb_link_node_rcu(&new_nh->rb_node, parent, pp); 1280 rb_insert_color(&new_nh->rb_node, root); 1281 1282 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack); 1283 if (rc) 1284 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root); 1285 1286 out: 1287 if (!rc) { 1288 nh_base_seq_inc(net); 1289 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 1290 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode) 1291 nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 1292 } 1293 1294 return rc; 1295 } 1296 1297 /* rtnl */ 1298 /* remove all nexthops tied to a device being deleted */ 1299 static void nexthop_flush_dev(struct net_device *dev) 1300 { 1301 unsigned int hash = nh_dev_hashfn(dev->ifindex); 1302 struct net *net = dev_net(dev); 1303 struct hlist_head *head = &net->nexthop.devhash[hash]; 1304 struct hlist_node *n; 1305 struct nh_info *nhi; 1306 1307 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 1308 if (nhi->fib_nhc.nhc_dev != dev) 1309 continue; 1310 1311 remove_nexthop(net, nhi->nh_parent, NULL); 1312 } 1313 } 1314 1315 /* rtnl; called when net namespace is deleted */ 1316 static void flush_all_nexthops(struct net *net) 1317 { 1318 struct rb_root *root = &net->nexthop.rb_root; 1319 struct rb_node *node; 1320 struct nexthop *nh; 1321 1322 while ((node = rb_first(root))) { 1323 nh = rb_entry(node, struct nexthop, rb_node); 1324 remove_nexthop(net, nh, NULL); 1325 cond_resched(); 1326 } 1327 } 1328 1329 static struct nexthop *nexthop_create_group(struct net *net, 1330 struct nh_config *cfg) 1331 { 1332 struct nlattr *grps_attr = cfg->nh_grp; 1333 struct nexthop_grp *entry = nla_data(grps_attr); 1334 u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 1335 struct nh_group *nhg; 1336 struct nexthop *nh; 1337 int i; 1338 1339 if (WARN_ON(!num_nh)) 1340 return ERR_PTR(-EINVAL); 1341 1342 nh = nexthop_alloc(); 1343 if (!nh) 1344 return ERR_PTR(-ENOMEM); 1345 1346 nh->is_group = 1; 1347 1348 nhg = nexthop_grp_alloc(num_nh); 1349 if (!nhg) { 1350 kfree(nh); 1351 return ERR_PTR(-ENOMEM); 1352 } 1353 1354 /* spare group used for removals */ 1355 nhg->spare = nexthop_grp_alloc(num_nh); 1356 if (!nhg->spare) { 1357 kfree(nhg); 1358 kfree(nh); 1359 return ERR_PTR(-ENOMEM); 1360 } 1361 nhg->spare->spare = nhg; 1362 1363 for (i = 0; i < nhg->num_nh; ++i) { 1364 struct nexthop *nhe; 1365 struct nh_info *nhi; 1366 1367 nhe = nexthop_find_by_id(net, entry[i].id); 1368 if (!nexthop_get(nhe)) 1369 goto out_no_nh; 1370 1371 nhi = rtnl_dereference(nhe->nh_info); 1372 if (nhi->family == AF_INET) 1373 nhg->has_v4 = true; 1374 1375 nhg->nh_entries[i].nh = nhe; 1376 nhg->nh_entries[i].weight = entry[i].weight + 1; 1377 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 1378 nhg->nh_entries[i].nh_parent = nh; 1379 } 1380 1381 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 1382 nhg->mpath = 1; 1383 nh_group_rebalance(nhg); 1384 } 1385 1386 if (cfg->nh_fdb) 1387 nhg->fdb_nh = 1; 1388 1389 rcu_assign_pointer(nh->nh_grp, nhg); 1390 1391 return nh; 1392 1393 out_no_nh: 1394 for (; i >= 0; --i) 1395 nexthop_put(nhg->nh_entries[i].nh); 1396 1397 kfree(nhg->spare); 1398 kfree(nhg); 1399 kfree(nh); 1400 1401 return ERR_PTR(-ENOENT); 1402 } 1403 1404 static int nh_create_ipv4(struct net *net, struct nexthop *nh, 1405 struct nh_info *nhi, struct nh_config *cfg, 1406 struct netlink_ext_ack *extack) 1407 { 1408 struct fib_nh *fib_nh = &nhi->fib_nh; 1409 struct fib_config fib_cfg = { 1410 .fc_oif = cfg->nh_ifindex, 1411 .fc_gw4 = cfg->gw.ipv4, 1412 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 1413 .fc_flags = cfg->nh_flags, 1414 .fc_encap = cfg->nh_encap, 1415 .fc_encap_type = cfg->nh_encap_type, 1416 }; 1417 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 1418 int err; 1419 1420 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 1421 if (err) { 1422 fib_nh_release(net, fib_nh); 1423 goto out; 1424 } 1425 1426 if (nhi->fdb_nh) 1427 goto out; 1428 1429 /* sets nh_dev if successful */ 1430 err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 1431 if (!err) { 1432 nh->nh_flags = fib_nh->fib_nh_flags; 1433 fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 1434 fib_nh->fib_nh_scope); 1435 } else { 1436 fib_nh_release(net, fib_nh); 1437 } 1438 out: 1439 return err; 1440 } 1441 1442 static int nh_create_ipv6(struct net *net, struct nexthop *nh, 1443 struct nh_info *nhi, struct nh_config *cfg, 1444 struct netlink_ext_ack *extack) 1445 { 1446 struct fib6_nh *fib6_nh = &nhi->fib6_nh; 1447 struct fib6_config fib6_cfg = { 1448 .fc_table = l3mdev_fib_table(cfg->dev), 1449 .fc_ifindex = cfg->nh_ifindex, 1450 .fc_gateway = cfg->gw.ipv6, 1451 .fc_flags = cfg->nh_flags, 1452 .fc_encap = cfg->nh_encap, 1453 .fc_encap_type = cfg->nh_encap_type, 1454 .fc_is_fdb = cfg->nh_fdb, 1455 }; 1456 int err; 1457 1458 if (!ipv6_addr_any(&cfg->gw.ipv6)) 1459 fib6_cfg.fc_flags |= RTF_GATEWAY; 1460 1461 /* sets nh_dev if successful */ 1462 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, 1463 extack); 1464 if (err) 1465 ipv6_stub->fib6_nh_release(fib6_nh); 1466 else 1467 nh->nh_flags = fib6_nh->fib_nh_flags; 1468 1469 return err; 1470 } 1471 1472 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 1473 struct netlink_ext_ack *extack) 1474 { 1475 struct nh_info *nhi; 1476 struct nexthop *nh; 1477 int err = 0; 1478 1479 nh = nexthop_alloc(); 1480 if (!nh) 1481 return ERR_PTR(-ENOMEM); 1482 1483 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL); 1484 if (!nhi) { 1485 kfree(nh); 1486 return ERR_PTR(-ENOMEM); 1487 } 1488 1489 nh->nh_flags = cfg->nh_flags; 1490 nh->net = net; 1491 1492 nhi->nh_parent = nh; 1493 nhi->family = cfg->nh_family; 1494 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 1495 1496 if (cfg->nh_fdb) 1497 nhi->fdb_nh = 1; 1498 1499 if (cfg->nh_blackhole) { 1500 nhi->reject_nh = 1; 1501 cfg->nh_ifindex = net->loopback_dev->ifindex; 1502 } 1503 1504 switch (cfg->nh_family) { 1505 case AF_INET: 1506 err = nh_create_ipv4(net, nh, nhi, cfg, extack); 1507 break; 1508 case AF_INET6: 1509 err = nh_create_ipv6(net, nh, nhi, cfg, extack); 1510 break; 1511 } 1512 1513 if (err) { 1514 kfree(nhi); 1515 kfree(nh); 1516 return ERR_PTR(err); 1517 } 1518 1519 /* add the entry to the device based hash */ 1520 if (!nhi->fdb_nh) 1521 nexthop_devhash_add(net, nhi); 1522 1523 rcu_assign_pointer(nh->nh_info, nhi); 1524 1525 return nh; 1526 } 1527 1528 /* called with rtnl lock held */ 1529 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 1530 struct netlink_ext_ack *extack) 1531 { 1532 struct nexthop *nh; 1533 int err; 1534 1535 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) { 1536 NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 1537 return ERR_PTR(-EINVAL); 1538 } 1539 1540 if (!cfg->nh_id) { 1541 cfg->nh_id = nh_find_unused_id(net); 1542 if (!cfg->nh_id) { 1543 NL_SET_ERR_MSG(extack, "No unused id"); 1544 return ERR_PTR(-EINVAL); 1545 } 1546 } 1547 1548 if (cfg->nh_grp) 1549 nh = nexthop_create_group(net, cfg); 1550 else 1551 nh = nexthop_create(net, cfg, extack); 1552 1553 if (IS_ERR(nh)) 1554 return nh; 1555 1556 refcount_set(&nh->refcnt, 1); 1557 nh->id = cfg->nh_id; 1558 nh->protocol = cfg->nh_protocol; 1559 nh->net = net; 1560 1561 err = insert_nexthop(net, nh, cfg, extack); 1562 if (err) { 1563 __remove_nexthop(net, nh, NULL); 1564 nexthop_put(nh); 1565 nh = ERR_PTR(err); 1566 } 1567 1568 return nh; 1569 } 1570 1571 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 1572 struct nlmsghdr *nlh, struct nh_config *cfg, 1573 struct netlink_ext_ack *extack) 1574 { 1575 struct nhmsg *nhm = nlmsg_data(nlh); 1576 struct nlattr *tb[NHA_MAX + 1]; 1577 int err; 1578 1579 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1580 extack); 1581 if (err < 0) 1582 return err; 1583 1584 err = -EINVAL; 1585 if (nhm->resvd || nhm->nh_scope) { 1586 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 1587 goto out; 1588 } 1589 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 1590 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 1591 goto out; 1592 } 1593 1594 switch (nhm->nh_family) { 1595 case AF_INET: 1596 case AF_INET6: 1597 break; 1598 case AF_UNSPEC: 1599 if (tb[NHA_GROUP]) 1600 break; 1601 fallthrough; 1602 default: 1603 NL_SET_ERR_MSG(extack, "Invalid address family"); 1604 goto out; 1605 } 1606 1607 if (tb[NHA_GROUPS] || tb[NHA_MASTER]) { 1608 NL_SET_ERR_MSG(extack, "Invalid attributes in request"); 1609 goto out; 1610 } 1611 1612 memset(cfg, 0, sizeof(*cfg)); 1613 cfg->nlflags = nlh->nlmsg_flags; 1614 cfg->nlinfo.portid = NETLINK_CB(skb).portid; 1615 cfg->nlinfo.nlh = nlh; 1616 cfg->nlinfo.nl_net = net; 1617 1618 cfg->nh_family = nhm->nh_family; 1619 cfg->nh_protocol = nhm->nh_protocol; 1620 cfg->nh_flags = nhm->nh_flags; 1621 1622 if (tb[NHA_ID]) 1623 cfg->nh_id = nla_get_u32(tb[NHA_ID]); 1624 1625 if (tb[NHA_FDB]) { 1626 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 1627 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 1628 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 1629 goto out; 1630 } 1631 if (nhm->nh_flags) { 1632 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 1633 goto out; 1634 } 1635 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 1636 } 1637 1638 if (tb[NHA_GROUP]) { 1639 if (nhm->nh_family != AF_UNSPEC) { 1640 NL_SET_ERR_MSG(extack, "Invalid family for group"); 1641 goto out; 1642 } 1643 cfg->nh_grp = tb[NHA_GROUP]; 1644 1645 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 1646 if (tb[NHA_GROUP_TYPE]) 1647 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 1648 1649 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 1650 NL_SET_ERR_MSG(extack, "Invalid group type"); 1651 goto out; 1652 } 1653 err = nh_check_attr_group(net, tb, extack); 1654 1655 /* no other attributes should be set */ 1656 goto out; 1657 } 1658 1659 if (tb[NHA_BLACKHOLE]) { 1660 if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 1661 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 1662 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 1663 goto out; 1664 } 1665 1666 cfg->nh_blackhole = 1; 1667 err = 0; 1668 goto out; 1669 } 1670 1671 if (!cfg->nh_fdb && !tb[NHA_OIF]) { 1672 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 1673 goto out; 1674 } 1675 1676 if (!cfg->nh_fdb && tb[NHA_OIF]) { 1677 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 1678 if (cfg->nh_ifindex) 1679 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 1680 1681 if (!cfg->dev) { 1682 NL_SET_ERR_MSG(extack, "Invalid device index"); 1683 goto out; 1684 } else if (!(cfg->dev->flags & IFF_UP)) { 1685 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 1686 err = -ENETDOWN; 1687 goto out; 1688 } else if (!netif_carrier_ok(cfg->dev)) { 1689 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 1690 err = -ENETDOWN; 1691 goto out; 1692 } 1693 } 1694 1695 err = -EINVAL; 1696 if (tb[NHA_GATEWAY]) { 1697 struct nlattr *gwa = tb[NHA_GATEWAY]; 1698 1699 switch (cfg->nh_family) { 1700 case AF_INET: 1701 if (nla_len(gwa) != sizeof(u32)) { 1702 NL_SET_ERR_MSG(extack, "Invalid gateway"); 1703 goto out; 1704 } 1705 cfg->gw.ipv4 = nla_get_be32(gwa); 1706 break; 1707 case AF_INET6: 1708 if (nla_len(gwa) != sizeof(struct in6_addr)) { 1709 NL_SET_ERR_MSG(extack, "Invalid gateway"); 1710 goto out; 1711 } 1712 cfg->gw.ipv6 = nla_get_in6_addr(gwa); 1713 break; 1714 default: 1715 NL_SET_ERR_MSG(extack, 1716 "Unknown address family for gateway"); 1717 goto out; 1718 } 1719 } else { 1720 /* device only nexthop (no gateway) */ 1721 if (cfg->nh_flags & RTNH_F_ONLINK) { 1722 NL_SET_ERR_MSG(extack, 1723 "ONLINK flag can not be set for nexthop without a gateway"); 1724 goto out; 1725 } 1726 } 1727 1728 if (tb[NHA_ENCAP]) { 1729 cfg->nh_encap = tb[NHA_ENCAP]; 1730 1731 if (!tb[NHA_ENCAP_TYPE]) { 1732 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 1733 goto out; 1734 } 1735 1736 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 1737 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 1738 if (err < 0) 1739 goto out; 1740 1741 } else if (tb[NHA_ENCAP_TYPE]) { 1742 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 1743 goto out; 1744 } 1745 1746 1747 err = 0; 1748 out: 1749 return err; 1750 } 1751 1752 /* rtnl */ 1753 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1754 struct netlink_ext_ack *extack) 1755 { 1756 struct net *net = sock_net(skb->sk); 1757 struct nh_config cfg; 1758 struct nexthop *nh; 1759 int err; 1760 1761 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack); 1762 if (!err) { 1763 nh = nexthop_add(net, &cfg, extack); 1764 if (IS_ERR(nh)) 1765 err = PTR_ERR(nh); 1766 } 1767 1768 return err; 1769 } 1770 1771 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id, 1772 struct netlink_ext_ack *extack) 1773 { 1774 struct nhmsg *nhm = nlmsg_data(nlh); 1775 struct nlattr *tb[NHA_MAX + 1]; 1776 int err, i; 1777 1778 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1779 extack); 1780 if (err < 0) 1781 return err; 1782 1783 err = -EINVAL; 1784 for (i = 0; i < __NHA_MAX; ++i) { 1785 if (!tb[i]) 1786 continue; 1787 1788 switch (i) { 1789 case NHA_ID: 1790 break; 1791 default: 1792 NL_SET_ERR_MSG_ATTR(extack, tb[i], 1793 "Unexpected attribute in request"); 1794 goto out; 1795 } 1796 } 1797 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1798 NL_SET_ERR_MSG(extack, "Invalid values in header"); 1799 goto out; 1800 } 1801 1802 if (!tb[NHA_ID]) { 1803 NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 1804 goto out; 1805 } 1806 1807 *id = nla_get_u32(tb[NHA_ID]); 1808 if (!(*id)) 1809 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 1810 else 1811 err = 0; 1812 out: 1813 return err; 1814 } 1815 1816 /* rtnl */ 1817 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1818 struct netlink_ext_ack *extack) 1819 { 1820 struct net *net = sock_net(skb->sk); 1821 struct nl_info nlinfo = { 1822 .nlh = nlh, 1823 .nl_net = net, 1824 .portid = NETLINK_CB(skb).portid, 1825 }; 1826 struct nexthop *nh; 1827 int err; 1828 u32 id; 1829 1830 err = nh_valid_get_del_req(nlh, &id, extack); 1831 if (err) 1832 return err; 1833 1834 nh = nexthop_find_by_id(net, id); 1835 if (!nh) 1836 return -ENOENT; 1837 1838 remove_nexthop(net, nh, &nlinfo); 1839 1840 return 0; 1841 } 1842 1843 /* rtnl */ 1844 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 1845 struct netlink_ext_ack *extack) 1846 { 1847 struct net *net = sock_net(in_skb->sk); 1848 struct sk_buff *skb = NULL; 1849 struct nexthop *nh; 1850 int err; 1851 u32 id; 1852 1853 err = nh_valid_get_del_req(nlh, &id, extack); 1854 if (err) 1855 return err; 1856 1857 err = -ENOBUFS; 1858 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1859 if (!skb) 1860 goto out; 1861 1862 err = -ENOENT; 1863 nh = nexthop_find_by_id(net, id); 1864 if (!nh) 1865 goto errout_free; 1866 1867 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 1868 nlh->nlmsg_seq, 0); 1869 if (err < 0) { 1870 WARN_ON(err == -EMSGSIZE); 1871 goto errout_free; 1872 } 1873 1874 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 1875 out: 1876 return err; 1877 errout_free: 1878 kfree_skb(skb); 1879 goto out; 1880 } 1881 1882 static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx, 1883 bool group_filter, u8 family) 1884 { 1885 const struct net_device *dev; 1886 const struct nh_info *nhi; 1887 1888 if (group_filter && !nh->is_group) 1889 return true; 1890 1891 if (!dev_idx && !master_idx && !family) 1892 return false; 1893 1894 if (nh->is_group) 1895 return true; 1896 1897 nhi = rtnl_dereference(nh->nh_info); 1898 if (family && nhi->family != family) 1899 return true; 1900 1901 dev = nhi->fib_nhc.nhc_dev; 1902 if (dev_idx && (!dev || dev->ifindex != dev_idx)) 1903 return true; 1904 1905 if (master_idx) { 1906 struct net_device *master; 1907 1908 if (!dev) 1909 return true; 1910 1911 master = netdev_master_upper_dev_get((struct net_device *)dev); 1912 if (!master || master->ifindex != master_idx) 1913 return true; 1914 } 1915 1916 return false; 1917 } 1918 1919 static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx, 1920 int *master_idx, bool *group_filter, 1921 bool *fdb_filter, struct netlink_callback *cb) 1922 { 1923 struct netlink_ext_ack *extack = cb->extack; 1924 struct nlattr *tb[NHA_MAX + 1]; 1925 struct nhmsg *nhm; 1926 int err, i; 1927 u32 idx; 1928 1929 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1930 NULL); 1931 if (err < 0) 1932 return err; 1933 1934 for (i = 0; i <= NHA_MAX; ++i) { 1935 if (!tb[i]) 1936 continue; 1937 1938 switch (i) { 1939 case NHA_OIF: 1940 idx = nla_get_u32(tb[i]); 1941 if (idx > INT_MAX) { 1942 NL_SET_ERR_MSG(extack, "Invalid device index"); 1943 return -EINVAL; 1944 } 1945 *dev_idx = idx; 1946 break; 1947 case NHA_MASTER: 1948 idx = nla_get_u32(tb[i]); 1949 if (idx > INT_MAX) { 1950 NL_SET_ERR_MSG(extack, "Invalid master device index"); 1951 return -EINVAL; 1952 } 1953 *master_idx = idx; 1954 break; 1955 case NHA_GROUPS: 1956 *group_filter = true; 1957 break; 1958 case NHA_FDB: 1959 *fdb_filter = true; 1960 break; 1961 default: 1962 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); 1963 return -EINVAL; 1964 } 1965 } 1966 1967 nhm = nlmsg_data(nlh); 1968 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1969 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 1970 return -EINVAL; 1971 } 1972 1973 return 0; 1974 } 1975 1976 /* rtnl */ 1977 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 1978 { 1979 bool group_filter = false, fdb_filter = false; 1980 struct nhmsg *nhm = nlmsg_data(cb->nlh); 1981 int dev_filter_idx = 0, master_idx = 0; 1982 struct net *net = sock_net(skb->sk); 1983 struct rb_root *root = &net->nexthop.rb_root; 1984 struct rb_node *node; 1985 int idx = 0, s_idx; 1986 int err; 1987 1988 err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx, 1989 &group_filter, &fdb_filter, cb); 1990 if (err < 0) 1991 return err; 1992 1993 s_idx = cb->args[0]; 1994 for (node = rb_first(root); node; node = rb_next(node)) { 1995 struct nexthop *nh; 1996 1997 if (idx < s_idx) 1998 goto cont; 1999 2000 nh = rb_entry(node, struct nexthop, rb_node); 2001 if (nh_dump_filtered(nh, dev_filter_idx, master_idx, 2002 group_filter, nhm->nh_family)) 2003 goto cont; 2004 2005 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 2006 NETLINK_CB(cb->skb).portid, 2007 cb->nlh->nlmsg_seq, NLM_F_MULTI); 2008 if (err < 0) { 2009 if (likely(skb->len)) 2010 goto out; 2011 2012 goto out_err; 2013 } 2014 cont: 2015 idx++; 2016 } 2017 2018 out: 2019 err = skb->len; 2020 out_err: 2021 cb->args[0] = idx; 2022 cb->seq = net->nexthop.seq; 2023 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 2024 2025 return err; 2026 } 2027 2028 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 2029 { 2030 unsigned int hash = nh_dev_hashfn(dev->ifindex); 2031 struct net *net = dev_net(dev); 2032 struct hlist_head *head = &net->nexthop.devhash[hash]; 2033 struct hlist_node *n; 2034 struct nh_info *nhi; 2035 2036 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 2037 if (nhi->fib_nhc.nhc_dev == dev) { 2038 if (nhi->family == AF_INET) 2039 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 2040 orig_mtu); 2041 } 2042 } 2043 } 2044 2045 /* rtnl */ 2046 static int nh_netdev_event(struct notifier_block *this, 2047 unsigned long event, void *ptr) 2048 { 2049 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2050 struct netdev_notifier_info_ext *info_ext; 2051 2052 switch (event) { 2053 case NETDEV_DOWN: 2054 case NETDEV_UNREGISTER: 2055 nexthop_flush_dev(dev); 2056 break; 2057 case NETDEV_CHANGE: 2058 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 2059 nexthop_flush_dev(dev); 2060 break; 2061 case NETDEV_CHANGEMTU: 2062 info_ext = ptr; 2063 nexthop_sync_mtu(dev, info_ext->ext.mtu); 2064 rt_cache_flush(dev_net(dev)); 2065 break; 2066 } 2067 return NOTIFY_DONE; 2068 } 2069 2070 static struct notifier_block nh_netdev_notifier = { 2071 .notifier_call = nh_netdev_event, 2072 }; 2073 2074 int register_nexthop_notifier(struct net *net, struct notifier_block *nb) 2075 { 2076 return blocking_notifier_chain_register(&net->nexthop.notifier_chain, 2077 nb); 2078 } 2079 EXPORT_SYMBOL(register_nexthop_notifier); 2080 2081 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 2082 { 2083 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 2084 nb); 2085 } 2086 EXPORT_SYMBOL(unregister_nexthop_notifier); 2087 2088 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 2089 { 2090 struct nexthop *nexthop; 2091 2092 rcu_read_lock(); 2093 2094 nexthop = nexthop_find_by_id(net, id); 2095 if (!nexthop) 2096 goto out; 2097 2098 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 2099 if (offload) 2100 nexthop->nh_flags |= RTNH_F_OFFLOAD; 2101 if (trap) 2102 nexthop->nh_flags |= RTNH_F_TRAP; 2103 2104 out: 2105 rcu_read_unlock(); 2106 } 2107 EXPORT_SYMBOL(nexthop_set_hw_flags); 2108 2109 static void __net_exit nexthop_net_exit(struct net *net) 2110 { 2111 rtnl_lock(); 2112 flush_all_nexthops(net); 2113 rtnl_unlock(); 2114 kfree(net->nexthop.devhash); 2115 } 2116 2117 static int __net_init nexthop_net_init(struct net *net) 2118 { 2119 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 2120 2121 net->nexthop.rb_root = RB_ROOT; 2122 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 2123 if (!net->nexthop.devhash) 2124 return -ENOMEM; 2125 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 2126 2127 return 0; 2128 } 2129 2130 static struct pernet_operations nexthop_net_ops = { 2131 .init = nexthop_net_init, 2132 .exit = nexthop_net_exit, 2133 }; 2134 2135 static int __init nexthop_init(void) 2136 { 2137 register_pernet_subsys(&nexthop_net_ops); 2138 2139 register_netdevice_notifier(&nh_netdev_notifier); 2140 2141 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2142 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0); 2143 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop, 2144 rtm_dump_nexthop, 0); 2145 2146 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2147 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2148 2149 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2150 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2151 2152 return 0; 2153 } 2154 subsys_initcall(nexthop_init); 2155