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