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 rc = 0; 1282 out: 1283 if (!rc) { 1284 nh_base_seq_inc(net); 1285 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 1286 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode) 1287 nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 1288 } 1289 1290 return rc; 1291 } 1292 1293 /* rtnl */ 1294 /* remove all nexthops tied to a device being deleted */ 1295 static void nexthop_flush_dev(struct net_device *dev) 1296 { 1297 unsigned int hash = nh_dev_hashfn(dev->ifindex); 1298 struct net *net = dev_net(dev); 1299 struct hlist_head *head = &net->nexthop.devhash[hash]; 1300 struct hlist_node *n; 1301 struct nh_info *nhi; 1302 1303 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 1304 if (nhi->fib_nhc.nhc_dev != dev) 1305 continue; 1306 1307 remove_nexthop(net, nhi->nh_parent, NULL); 1308 } 1309 } 1310 1311 /* rtnl; called when net namespace is deleted */ 1312 static void flush_all_nexthops(struct net *net) 1313 { 1314 struct rb_root *root = &net->nexthop.rb_root; 1315 struct rb_node *node; 1316 struct nexthop *nh; 1317 1318 while ((node = rb_first(root))) { 1319 nh = rb_entry(node, struct nexthop, rb_node); 1320 remove_nexthop(net, nh, NULL); 1321 cond_resched(); 1322 } 1323 } 1324 1325 static struct nexthop *nexthop_create_group(struct net *net, 1326 struct nh_config *cfg) 1327 { 1328 struct nlattr *grps_attr = cfg->nh_grp; 1329 struct nexthop_grp *entry = nla_data(grps_attr); 1330 u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 1331 struct nh_group *nhg; 1332 struct nexthop *nh; 1333 int i; 1334 1335 if (WARN_ON(!num_nh)) 1336 return ERR_PTR(-EINVAL); 1337 1338 nh = nexthop_alloc(); 1339 if (!nh) 1340 return ERR_PTR(-ENOMEM); 1341 1342 nh->is_group = 1; 1343 1344 nhg = nexthop_grp_alloc(num_nh); 1345 if (!nhg) { 1346 kfree(nh); 1347 return ERR_PTR(-ENOMEM); 1348 } 1349 1350 /* spare group used for removals */ 1351 nhg->spare = nexthop_grp_alloc(num_nh); 1352 if (!nhg->spare) { 1353 kfree(nhg); 1354 kfree(nh); 1355 return ERR_PTR(-ENOMEM); 1356 } 1357 nhg->spare->spare = nhg; 1358 1359 for (i = 0; i < nhg->num_nh; ++i) { 1360 struct nexthop *nhe; 1361 struct nh_info *nhi; 1362 1363 nhe = nexthop_find_by_id(net, entry[i].id); 1364 if (!nexthop_get(nhe)) 1365 goto out_no_nh; 1366 1367 nhi = rtnl_dereference(nhe->nh_info); 1368 if (nhi->family == AF_INET) 1369 nhg->has_v4 = true; 1370 1371 nhg->nh_entries[i].nh = nhe; 1372 nhg->nh_entries[i].weight = entry[i].weight + 1; 1373 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 1374 nhg->nh_entries[i].nh_parent = nh; 1375 } 1376 1377 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 1378 nhg->mpath = 1; 1379 nh_group_rebalance(nhg); 1380 } 1381 1382 if (cfg->nh_fdb) 1383 nhg->fdb_nh = 1; 1384 1385 rcu_assign_pointer(nh->nh_grp, nhg); 1386 1387 return nh; 1388 1389 out_no_nh: 1390 for (; i >= 0; --i) 1391 nexthop_put(nhg->nh_entries[i].nh); 1392 1393 kfree(nhg->spare); 1394 kfree(nhg); 1395 kfree(nh); 1396 1397 return ERR_PTR(-ENOENT); 1398 } 1399 1400 static int nh_create_ipv4(struct net *net, struct nexthop *nh, 1401 struct nh_info *nhi, struct nh_config *cfg, 1402 struct netlink_ext_ack *extack) 1403 { 1404 struct fib_nh *fib_nh = &nhi->fib_nh; 1405 struct fib_config fib_cfg = { 1406 .fc_oif = cfg->nh_ifindex, 1407 .fc_gw4 = cfg->gw.ipv4, 1408 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 1409 .fc_flags = cfg->nh_flags, 1410 .fc_encap = cfg->nh_encap, 1411 .fc_encap_type = cfg->nh_encap_type, 1412 }; 1413 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 1414 int err; 1415 1416 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 1417 if (err) { 1418 fib_nh_release(net, fib_nh); 1419 goto out; 1420 } 1421 1422 if (nhi->fdb_nh) 1423 goto out; 1424 1425 /* sets nh_dev if successful */ 1426 err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 1427 if (!err) { 1428 nh->nh_flags = fib_nh->fib_nh_flags; 1429 fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 1430 fib_nh->fib_nh_scope); 1431 } else { 1432 fib_nh_release(net, fib_nh); 1433 } 1434 out: 1435 return err; 1436 } 1437 1438 static int nh_create_ipv6(struct net *net, struct nexthop *nh, 1439 struct nh_info *nhi, struct nh_config *cfg, 1440 struct netlink_ext_ack *extack) 1441 { 1442 struct fib6_nh *fib6_nh = &nhi->fib6_nh; 1443 struct fib6_config fib6_cfg = { 1444 .fc_table = l3mdev_fib_table(cfg->dev), 1445 .fc_ifindex = cfg->nh_ifindex, 1446 .fc_gateway = cfg->gw.ipv6, 1447 .fc_flags = cfg->nh_flags, 1448 .fc_encap = cfg->nh_encap, 1449 .fc_encap_type = cfg->nh_encap_type, 1450 .fc_is_fdb = cfg->nh_fdb, 1451 }; 1452 int err; 1453 1454 if (!ipv6_addr_any(&cfg->gw.ipv6)) 1455 fib6_cfg.fc_flags |= RTF_GATEWAY; 1456 1457 /* sets nh_dev if successful */ 1458 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, 1459 extack); 1460 if (err) 1461 ipv6_stub->fib6_nh_release(fib6_nh); 1462 else 1463 nh->nh_flags = fib6_nh->fib_nh_flags; 1464 1465 return err; 1466 } 1467 1468 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 1469 struct netlink_ext_ack *extack) 1470 { 1471 struct nh_info *nhi; 1472 struct nexthop *nh; 1473 int err = 0; 1474 1475 nh = nexthop_alloc(); 1476 if (!nh) 1477 return ERR_PTR(-ENOMEM); 1478 1479 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL); 1480 if (!nhi) { 1481 kfree(nh); 1482 return ERR_PTR(-ENOMEM); 1483 } 1484 1485 nh->nh_flags = cfg->nh_flags; 1486 nh->net = net; 1487 1488 nhi->nh_parent = nh; 1489 nhi->family = cfg->nh_family; 1490 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 1491 1492 if (cfg->nh_fdb) 1493 nhi->fdb_nh = 1; 1494 1495 if (cfg->nh_blackhole) { 1496 nhi->reject_nh = 1; 1497 cfg->nh_ifindex = net->loopback_dev->ifindex; 1498 } 1499 1500 switch (cfg->nh_family) { 1501 case AF_INET: 1502 err = nh_create_ipv4(net, nh, nhi, cfg, extack); 1503 break; 1504 case AF_INET6: 1505 err = nh_create_ipv6(net, nh, nhi, cfg, extack); 1506 break; 1507 } 1508 1509 if (err) { 1510 kfree(nhi); 1511 kfree(nh); 1512 return ERR_PTR(err); 1513 } 1514 1515 /* add the entry to the device based hash */ 1516 if (!nhi->fdb_nh) 1517 nexthop_devhash_add(net, nhi); 1518 1519 rcu_assign_pointer(nh->nh_info, nhi); 1520 1521 return nh; 1522 } 1523 1524 /* called with rtnl lock held */ 1525 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 1526 struct netlink_ext_ack *extack) 1527 { 1528 struct nexthop *nh; 1529 int err; 1530 1531 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) { 1532 NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 1533 return ERR_PTR(-EINVAL); 1534 } 1535 1536 if (!cfg->nh_id) { 1537 cfg->nh_id = nh_find_unused_id(net); 1538 if (!cfg->nh_id) { 1539 NL_SET_ERR_MSG(extack, "No unused id"); 1540 return ERR_PTR(-EINVAL); 1541 } 1542 } 1543 1544 if (cfg->nh_grp) 1545 nh = nexthop_create_group(net, cfg); 1546 else 1547 nh = nexthop_create(net, cfg, extack); 1548 1549 if (IS_ERR(nh)) 1550 return nh; 1551 1552 refcount_set(&nh->refcnt, 1); 1553 nh->id = cfg->nh_id; 1554 nh->protocol = cfg->nh_protocol; 1555 nh->net = net; 1556 1557 err = insert_nexthop(net, nh, cfg, extack); 1558 if (err) { 1559 __remove_nexthop(net, nh, NULL); 1560 nexthop_put(nh); 1561 nh = ERR_PTR(err); 1562 } 1563 1564 return nh; 1565 } 1566 1567 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 1568 struct nlmsghdr *nlh, struct nh_config *cfg, 1569 struct netlink_ext_ack *extack) 1570 { 1571 struct nhmsg *nhm = nlmsg_data(nlh); 1572 struct nlattr *tb[NHA_MAX + 1]; 1573 int err; 1574 1575 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1576 extack); 1577 if (err < 0) 1578 return err; 1579 1580 err = -EINVAL; 1581 if (nhm->resvd || nhm->nh_scope) { 1582 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 1583 goto out; 1584 } 1585 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 1586 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 1587 goto out; 1588 } 1589 1590 switch (nhm->nh_family) { 1591 case AF_INET: 1592 case AF_INET6: 1593 break; 1594 case AF_UNSPEC: 1595 if (tb[NHA_GROUP]) 1596 break; 1597 fallthrough; 1598 default: 1599 NL_SET_ERR_MSG(extack, "Invalid address family"); 1600 goto out; 1601 } 1602 1603 if (tb[NHA_GROUPS] || tb[NHA_MASTER]) { 1604 NL_SET_ERR_MSG(extack, "Invalid attributes in request"); 1605 goto out; 1606 } 1607 1608 memset(cfg, 0, sizeof(*cfg)); 1609 cfg->nlflags = nlh->nlmsg_flags; 1610 cfg->nlinfo.portid = NETLINK_CB(skb).portid; 1611 cfg->nlinfo.nlh = nlh; 1612 cfg->nlinfo.nl_net = net; 1613 1614 cfg->nh_family = nhm->nh_family; 1615 cfg->nh_protocol = nhm->nh_protocol; 1616 cfg->nh_flags = nhm->nh_flags; 1617 1618 if (tb[NHA_ID]) 1619 cfg->nh_id = nla_get_u32(tb[NHA_ID]); 1620 1621 if (tb[NHA_FDB]) { 1622 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 1623 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 1624 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 1625 goto out; 1626 } 1627 if (nhm->nh_flags) { 1628 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 1629 goto out; 1630 } 1631 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 1632 } 1633 1634 if (tb[NHA_GROUP]) { 1635 if (nhm->nh_family != AF_UNSPEC) { 1636 NL_SET_ERR_MSG(extack, "Invalid family for group"); 1637 goto out; 1638 } 1639 cfg->nh_grp = tb[NHA_GROUP]; 1640 1641 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 1642 if (tb[NHA_GROUP_TYPE]) 1643 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 1644 1645 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 1646 NL_SET_ERR_MSG(extack, "Invalid group type"); 1647 goto out; 1648 } 1649 err = nh_check_attr_group(net, tb, extack); 1650 1651 /* no other attributes should be set */ 1652 goto out; 1653 } 1654 1655 if (tb[NHA_BLACKHOLE]) { 1656 if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 1657 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 1658 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 1659 goto out; 1660 } 1661 1662 cfg->nh_blackhole = 1; 1663 err = 0; 1664 goto out; 1665 } 1666 1667 if (!cfg->nh_fdb && !tb[NHA_OIF]) { 1668 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 1669 goto out; 1670 } 1671 1672 if (!cfg->nh_fdb && tb[NHA_OIF]) { 1673 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 1674 if (cfg->nh_ifindex) 1675 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 1676 1677 if (!cfg->dev) { 1678 NL_SET_ERR_MSG(extack, "Invalid device index"); 1679 goto out; 1680 } else if (!(cfg->dev->flags & IFF_UP)) { 1681 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 1682 err = -ENETDOWN; 1683 goto out; 1684 } else if (!netif_carrier_ok(cfg->dev)) { 1685 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 1686 err = -ENETDOWN; 1687 goto out; 1688 } 1689 } 1690 1691 err = -EINVAL; 1692 if (tb[NHA_GATEWAY]) { 1693 struct nlattr *gwa = tb[NHA_GATEWAY]; 1694 1695 switch (cfg->nh_family) { 1696 case AF_INET: 1697 if (nla_len(gwa) != sizeof(u32)) { 1698 NL_SET_ERR_MSG(extack, "Invalid gateway"); 1699 goto out; 1700 } 1701 cfg->gw.ipv4 = nla_get_be32(gwa); 1702 break; 1703 case AF_INET6: 1704 if (nla_len(gwa) != sizeof(struct in6_addr)) { 1705 NL_SET_ERR_MSG(extack, "Invalid gateway"); 1706 goto out; 1707 } 1708 cfg->gw.ipv6 = nla_get_in6_addr(gwa); 1709 break; 1710 default: 1711 NL_SET_ERR_MSG(extack, 1712 "Unknown address family for gateway"); 1713 goto out; 1714 } 1715 } else { 1716 /* device only nexthop (no gateway) */ 1717 if (cfg->nh_flags & RTNH_F_ONLINK) { 1718 NL_SET_ERR_MSG(extack, 1719 "ONLINK flag can not be set for nexthop without a gateway"); 1720 goto out; 1721 } 1722 } 1723 1724 if (tb[NHA_ENCAP]) { 1725 cfg->nh_encap = tb[NHA_ENCAP]; 1726 1727 if (!tb[NHA_ENCAP_TYPE]) { 1728 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 1729 goto out; 1730 } 1731 1732 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 1733 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 1734 if (err < 0) 1735 goto out; 1736 1737 } else if (tb[NHA_ENCAP_TYPE]) { 1738 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 1739 goto out; 1740 } 1741 1742 1743 err = 0; 1744 out: 1745 return err; 1746 } 1747 1748 /* rtnl */ 1749 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1750 struct netlink_ext_ack *extack) 1751 { 1752 struct net *net = sock_net(skb->sk); 1753 struct nh_config cfg; 1754 struct nexthop *nh; 1755 int err; 1756 1757 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack); 1758 if (!err) { 1759 nh = nexthop_add(net, &cfg, extack); 1760 if (IS_ERR(nh)) 1761 err = PTR_ERR(nh); 1762 } 1763 1764 return err; 1765 } 1766 1767 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id, 1768 struct netlink_ext_ack *extack) 1769 { 1770 struct nhmsg *nhm = nlmsg_data(nlh); 1771 struct nlattr *tb[NHA_MAX + 1]; 1772 int err, i; 1773 1774 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1775 extack); 1776 if (err < 0) 1777 return err; 1778 1779 err = -EINVAL; 1780 for (i = 0; i < __NHA_MAX; ++i) { 1781 if (!tb[i]) 1782 continue; 1783 1784 switch (i) { 1785 case NHA_ID: 1786 break; 1787 default: 1788 NL_SET_ERR_MSG_ATTR(extack, tb[i], 1789 "Unexpected attribute in request"); 1790 goto out; 1791 } 1792 } 1793 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1794 NL_SET_ERR_MSG(extack, "Invalid values in header"); 1795 goto out; 1796 } 1797 1798 if (!tb[NHA_ID]) { 1799 NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 1800 goto out; 1801 } 1802 1803 *id = nla_get_u32(tb[NHA_ID]); 1804 if (!(*id)) 1805 NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 1806 else 1807 err = 0; 1808 out: 1809 return err; 1810 } 1811 1812 /* rtnl */ 1813 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1814 struct netlink_ext_ack *extack) 1815 { 1816 struct net *net = sock_net(skb->sk); 1817 struct nl_info nlinfo = { 1818 .nlh = nlh, 1819 .nl_net = net, 1820 .portid = NETLINK_CB(skb).portid, 1821 }; 1822 struct nexthop *nh; 1823 int err; 1824 u32 id; 1825 1826 err = nh_valid_get_del_req(nlh, &id, extack); 1827 if (err) 1828 return err; 1829 1830 nh = nexthop_find_by_id(net, id); 1831 if (!nh) 1832 return -ENOENT; 1833 1834 remove_nexthop(net, nh, &nlinfo); 1835 1836 return 0; 1837 } 1838 1839 /* rtnl */ 1840 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 1841 struct netlink_ext_ack *extack) 1842 { 1843 struct net *net = sock_net(in_skb->sk); 1844 struct sk_buff *skb = NULL; 1845 struct nexthop *nh; 1846 int err; 1847 u32 id; 1848 1849 err = nh_valid_get_del_req(nlh, &id, extack); 1850 if (err) 1851 return err; 1852 1853 err = -ENOBUFS; 1854 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1855 if (!skb) 1856 goto out; 1857 1858 err = -ENOENT; 1859 nh = nexthop_find_by_id(net, id); 1860 if (!nh) 1861 goto errout_free; 1862 1863 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 1864 nlh->nlmsg_seq, 0); 1865 if (err < 0) { 1866 WARN_ON(err == -EMSGSIZE); 1867 goto errout_free; 1868 } 1869 1870 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 1871 out: 1872 return err; 1873 errout_free: 1874 kfree_skb(skb); 1875 goto out; 1876 } 1877 1878 static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx, 1879 bool group_filter, u8 family) 1880 { 1881 const struct net_device *dev; 1882 const struct nh_info *nhi; 1883 1884 if (group_filter && !nh->is_group) 1885 return true; 1886 1887 if (!dev_idx && !master_idx && !family) 1888 return false; 1889 1890 if (nh->is_group) 1891 return true; 1892 1893 nhi = rtnl_dereference(nh->nh_info); 1894 if (family && nhi->family != family) 1895 return true; 1896 1897 dev = nhi->fib_nhc.nhc_dev; 1898 if (dev_idx && (!dev || dev->ifindex != dev_idx)) 1899 return true; 1900 1901 if (master_idx) { 1902 struct net_device *master; 1903 1904 if (!dev) 1905 return true; 1906 1907 master = netdev_master_upper_dev_get((struct net_device *)dev); 1908 if (!master || master->ifindex != master_idx) 1909 return true; 1910 } 1911 1912 return false; 1913 } 1914 1915 static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx, 1916 int *master_idx, bool *group_filter, 1917 bool *fdb_filter, struct netlink_callback *cb) 1918 { 1919 struct netlink_ext_ack *extack = cb->extack; 1920 struct nlattr *tb[NHA_MAX + 1]; 1921 struct nhmsg *nhm; 1922 int err, i; 1923 u32 idx; 1924 1925 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1926 NULL); 1927 if (err < 0) 1928 return err; 1929 1930 for (i = 0; i <= NHA_MAX; ++i) { 1931 if (!tb[i]) 1932 continue; 1933 1934 switch (i) { 1935 case NHA_OIF: 1936 idx = nla_get_u32(tb[i]); 1937 if (idx > INT_MAX) { 1938 NL_SET_ERR_MSG(extack, "Invalid device index"); 1939 return -EINVAL; 1940 } 1941 *dev_idx = idx; 1942 break; 1943 case NHA_MASTER: 1944 idx = nla_get_u32(tb[i]); 1945 if (idx > INT_MAX) { 1946 NL_SET_ERR_MSG(extack, "Invalid master device index"); 1947 return -EINVAL; 1948 } 1949 *master_idx = idx; 1950 break; 1951 case NHA_GROUPS: 1952 *group_filter = true; 1953 break; 1954 case NHA_FDB: 1955 *fdb_filter = true; 1956 break; 1957 default: 1958 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); 1959 return -EINVAL; 1960 } 1961 } 1962 1963 nhm = nlmsg_data(nlh); 1964 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1965 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 1966 return -EINVAL; 1967 } 1968 1969 return 0; 1970 } 1971 1972 /* rtnl */ 1973 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 1974 { 1975 bool group_filter = false, fdb_filter = false; 1976 struct nhmsg *nhm = nlmsg_data(cb->nlh); 1977 int dev_filter_idx = 0, master_idx = 0; 1978 struct net *net = sock_net(skb->sk); 1979 struct rb_root *root = &net->nexthop.rb_root; 1980 struct rb_node *node; 1981 int idx = 0, s_idx; 1982 int err; 1983 1984 err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx, 1985 &group_filter, &fdb_filter, cb); 1986 if (err < 0) 1987 return err; 1988 1989 s_idx = cb->args[0]; 1990 for (node = rb_first(root); node; node = rb_next(node)) { 1991 struct nexthop *nh; 1992 1993 if (idx < s_idx) 1994 goto cont; 1995 1996 nh = rb_entry(node, struct nexthop, rb_node); 1997 if (nh_dump_filtered(nh, dev_filter_idx, master_idx, 1998 group_filter, nhm->nh_family)) 1999 goto cont; 2000 2001 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 2002 NETLINK_CB(cb->skb).portid, 2003 cb->nlh->nlmsg_seq, NLM_F_MULTI); 2004 if (err < 0) { 2005 if (likely(skb->len)) 2006 goto out; 2007 2008 goto out_err; 2009 } 2010 cont: 2011 idx++; 2012 } 2013 2014 out: 2015 err = skb->len; 2016 out_err: 2017 cb->args[0] = idx; 2018 cb->seq = net->nexthop.seq; 2019 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 2020 2021 return err; 2022 } 2023 2024 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 2025 { 2026 unsigned int hash = nh_dev_hashfn(dev->ifindex); 2027 struct net *net = dev_net(dev); 2028 struct hlist_head *head = &net->nexthop.devhash[hash]; 2029 struct hlist_node *n; 2030 struct nh_info *nhi; 2031 2032 hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 2033 if (nhi->fib_nhc.nhc_dev == dev) { 2034 if (nhi->family == AF_INET) 2035 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 2036 orig_mtu); 2037 } 2038 } 2039 } 2040 2041 /* rtnl */ 2042 static int nh_netdev_event(struct notifier_block *this, 2043 unsigned long event, void *ptr) 2044 { 2045 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2046 struct netdev_notifier_info_ext *info_ext; 2047 2048 switch (event) { 2049 case NETDEV_DOWN: 2050 case NETDEV_UNREGISTER: 2051 nexthop_flush_dev(dev); 2052 break; 2053 case NETDEV_CHANGE: 2054 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 2055 nexthop_flush_dev(dev); 2056 break; 2057 case NETDEV_CHANGEMTU: 2058 info_ext = ptr; 2059 nexthop_sync_mtu(dev, info_ext->ext.mtu); 2060 rt_cache_flush(dev_net(dev)); 2061 break; 2062 } 2063 return NOTIFY_DONE; 2064 } 2065 2066 static struct notifier_block nh_netdev_notifier = { 2067 .notifier_call = nh_netdev_event, 2068 }; 2069 2070 int register_nexthop_notifier(struct net *net, struct notifier_block *nb) 2071 { 2072 return blocking_notifier_chain_register(&net->nexthop.notifier_chain, 2073 nb); 2074 } 2075 EXPORT_SYMBOL(register_nexthop_notifier); 2076 2077 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 2078 { 2079 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 2080 nb); 2081 } 2082 EXPORT_SYMBOL(unregister_nexthop_notifier); 2083 2084 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 2085 { 2086 struct nexthop *nexthop; 2087 2088 rcu_read_lock(); 2089 2090 nexthop = nexthop_find_by_id(net, id); 2091 if (!nexthop) 2092 goto out; 2093 2094 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 2095 if (offload) 2096 nexthop->nh_flags |= RTNH_F_OFFLOAD; 2097 if (trap) 2098 nexthop->nh_flags |= RTNH_F_TRAP; 2099 2100 out: 2101 rcu_read_unlock(); 2102 } 2103 EXPORT_SYMBOL(nexthop_set_hw_flags); 2104 2105 static void __net_exit nexthop_net_exit(struct net *net) 2106 { 2107 rtnl_lock(); 2108 flush_all_nexthops(net); 2109 rtnl_unlock(); 2110 kfree(net->nexthop.devhash); 2111 } 2112 2113 static int __net_init nexthop_net_init(struct net *net) 2114 { 2115 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 2116 2117 net->nexthop.rb_root = RB_ROOT; 2118 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 2119 if (!net->nexthop.devhash) 2120 return -ENOMEM; 2121 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 2122 2123 return 0; 2124 } 2125 2126 static struct pernet_operations nexthop_net_ops = { 2127 .init = nexthop_net_init, 2128 .exit = nexthop_net_exit, 2129 }; 2130 2131 static int __init nexthop_init(void) 2132 { 2133 register_pernet_subsys(&nexthop_net_ops); 2134 2135 register_netdevice_notifier(&nh_netdev_notifier); 2136 2137 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2138 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0); 2139 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop, 2140 rtm_dump_nexthop, 0); 2141 2142 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2143 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2144 2145 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2146 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2147 2148 return 0; 2149 } 2150 subsys_initcall(nexthop_init); 2151