1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * IPv4 Forwarding Information Base: semantics. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 */ 11 12 #include <linux/uaccess.h> 13 #include <linux/bitops.h> 14 #include <linux/types.h> 15 #include <linux/kernel.h> 16 #include <linux/jiffies.h> 17 #include <linux/mm.h> 18 #include <linux/string.h> 19 #include <linux/socket.h> 20 #include <linux/sockios.h> 21 #include <linux/errno.h> 22 #include <linux/in.h> 23 #include <linux/inet.h> 24 #include <linux/inetdevice.h> 25 #include <linux/netdevice.h> 26 #include <linux/if_arp.h> 27 #include <linux/proc_fs.h> 28 #include <linux/skbuff.h> 29 #include <linux/init.h> 30 #include <linux/slab.h> 31 #include <linux/netlink.h> 32 33 #include <net/arp.h> 34 #include <net/ip.h> 35 #include <net/protocol.h> 36 #include <net/route.h> 37 #include <net/tcp.h> 38 #include <net/sock.h> 39 #include <net/ip_fib.h> 40 #include <net/ip6_fib.h> 41 #include <net/nexthop.h> 42 #include <net/netlink.h> 43 #include <net/rtnh.h> 44 #include <net/lwtunnel.h> 45 #include <net/fib_notifier.h> 46 #include <net/addrconf.h> 47 48 #include "fib_lookup.h" 49 50 static DEFINE_SPINLOCK(fib_info_lock); 51 static struct hlist_head *fib_info_hash; 52 static struct hlist_head *fib_info_laddrhash; 53 static unsigned int fib_info_hash_size; 54 static unsigned int fib_info_cnt; 55 56 #define DEVINDEX_HASHBITS 8 57 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) 58 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; 59 60 /* for_nexthops and change_nexthops only used when nexthop object 61 * is not set in a fib_info. The logic within can reference fib_nh. 62 */ 63 #ifdef CONFIG_IP_ROUTE_MULTIPATH 64 65 #define for_nexthops(fi) { \ 66 int nhsel; const struct fib_nh *nh; \ 67 for (nhsel = 0, nh = (fi)->fib_nh; \ 68 nhsel < fib_info_num_path((fi)); \ 69 nh++, nhsel++) 70 71 #define change_nexthops(fi) { \ 72 int nhsel; struct fib_nh *nexthop_nh; \ 73 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 74 nhsel < fib_info_num_path((fi)); \ 75 nexthop_nh++, nhsel++) 76 77 #else /* CONFIG_IP_ROUTE_MULTIPATH */ 78 79 /* Hope, that gcc will optimize it to get rid of dummy loop */ 80 81 #define for_nexthops(fi) { \ 82 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ 83 for (nhsel = 0; nhsel < 1; nhsel++) 84 85 #define change_nexthops(fi) { \ 86 int nhsel; \ 87 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ 88 for (nhsel = 0; nhsel < 1; nhsel++) 89 90 #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 91 92 #define endfor_nexthops(fi) } 93 94 95 const struct fib_prop fib_props[RTN_MAX + 1] = { 96 [RTN_UNSPEC] = { 97 .error = 0, 98 .scope = RT_SCOPE_NOWHERE, 99 }, 100 [RTN_UNICAST] = { 101 .error = 0, 102 .scope = RT_SCOPE_UNIVERSE, 103 }, 104 [RTN_LOCAL] = { 105 .error = 0, 106 .scope = RT_SCOPE_HOST, 107 }, 108 [RTN_BROADCAST] = { 109 .error = 0, 110 .scope = RT_SCOPE_LINK, 111 }, 112 [RTN_ANYCAST] = { 113 .error = 0, 114 .scope = RT_SCOPE_LINK, 115 }, 116 [RTN_MULTICAST] = { 117 .error = 0, 118 .scope = RT_SCOPE_UNIVERSE, 119 }, 120 [RTN_BLACKHOLE] = { 121 .error = -EINVAL, 122 .scope = RT_SCOPE_UNIVERSE, 123 }, 124 [RTN_UNREACHABLE] = { 125 .error = -EHOSTUNREACH, 126 .scope = RT_SCOPE_UNIVERSE, 127 }, 128 [RTN_PROHIBIT] = { 129 .error = -EACCES, 130 .scope = RT_SCOPE_UNIVERSE, 131 }, 132 [RTN_THROW] = { 133 .error = -EAGAIN, 134 .scope = RT_SCOPE_UNIVERSE, 135 }, 136 [RTN_NAT] = { 137 .error = -EINVAL, 138 .scope = RT_SCOPE_NOWHERE, 139 }, 140 [RTN_XRESOLVE] = { 141 .error = -EINVAL, 142 .scope = RT_SCOPE_NOWHERE, 143 }, 144 }; 145 146 static void rt_fibinfo_free(struct rtable __rcu **rtp) 147 { 148 struct rtable *rt = rcu_dereference_protected(*rtp, 1); 149 150 if (!rt) 151 return; 152 153 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL); 154 * because we waited an RCU grace period before calling 155 * free_fib_info_rcu() 156 */ 157 158 dst_dev_put(&rt->dst); 159 dst_release_immediate(&rt->dst); 160 } 161 162 static void free_nh_exceptions(struct fib_nh_common *nhc) 163 { 164 struct fnhe_hash_bucket *hash; 165 int i; 166 167 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1); 168 if (!hash) 169 return; 170 for (i = 0; i < FNHE_HASH_SIZE; i++) { 171 struct fib_nh_exception *fnhe; 172 173 fnhe = rcu_dereference_protected(hash[i].chain, 1); 174 while (fnhe) { 175 struct fib_nh_exception *next; 176 177 next = rcu_dereference_protected(fnhe->fnhe_next, 1); 178 179 rt_fibinfo_free(&fnhe->fnhe_rth_input); 180 rt_fibinfo_free(&fnhe->fnhe_rth_output); 181 182 kfree(fnhe); 183 184 fnhe = next; 185 } 186 } 187 kfree(hash); 188 } 189 190 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) 191 { 192 int cpu; 193 194 if (!rtp) 195 return; 196 197 for_each_possible_cpu(cpu) { 198 struct rtable *rt; 199 200 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1); 201 if (rt) { 202 dst_dev_put(&rt->dst); 203 dst_release_immediate(&rt->dst); 204 } 205 } 206 free_percpu(rtp); 207 } 208 209 void fib_nh_common_release(struct fib_nh_common *nhc) 210 { 211 if (nhc->nhc_dev) 212 dev_put(nhc->nhc_dev); 213 214 lwtstate_put(nhc->nhc_lwtstate); 215 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output); 216 rt_fibinfo_free(&nhc->nhc_rth_input); 217 free_nh_exceptions(nhc); 218 } 219 EXPORT_SYMBOL_GPL(fib_nh_common_release); 220 221 void fib_nh_release(struct net *net, struct fib_nh *fib_nh) 222 { 223 #ifdef CONFIG_IP_ROUTE_CLASSID 224 if (fib_nh->nh_tclassid) 225 net->ipv4.fib_num_tclassid_users--; 226 #endif 227 fib_nh_common_release(&fib_nh->nh_common); 228 } 229 230 /* Release a nexthop info record */ 231 static void free_fib_info_rcu(struct rcu_head *head) 232 { 233 struct fib_info *fi = container_of(head, struct fib_info, rcu); 234 235 if (fi->nh) { 236 nexthop_put(fi->nh); 237 } else { 238 change_nexthops(fi) { 239 fib_nh_release(fi->fib_net, nexthop_nh); 240 } endfor_nexthops(fi); 241 } 242 243 ip_fib_metrics_put(fi->fib_metrics); 244 245 kfree(fi); 246 } 247 248 void free_fib_info(struct fib_info *fi) 249 { 250 if (fi->fib_dead == 0) { 251 pr_warn("Freeing alive fib_info %p\n", fi); 252 return; 253 } 254 fib_info_cnt--; 255 256 call_rcu(&fi->rcu, free_fib_info_rcu); 257 } 258 EXPORT_SYMBOL_GPL(free_fib_info); 259 260 void fib_release_info(struct fib_info *fi) 261 { 262 spin_lock_bh(&fib_info_lock); 263 if (fi && --fi->fib_treeref == 0) { 264 hlist_del(&fi->fib_hash); 265 if (fi->fib_prefsrc) 266 hlist_del(&fi->fib_lhash); 267 if (fi->nh) { 268 list_del(&fi->nh_list); 269 } else { 270 change_nexthops(fi) { 271 if (!nexthop_nh->fib_nh_dev) 272 continue; 273 hlist_del(&nexthop_nh->nh_hash); 274 } endfor_nexthops(fi) 275 } 276 fi->fib_dead = 1; 277 fib_info_put(fi); 278 } 279 spin_unlock_bh(&fib_info_lock); 280 } 281 282 static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi) 283 { 284 const struct fib_nh *onh; 285 286 if (fi->nh || ofi->nh) 287 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1; 288 289 if (ofi->fib_nhs == 0) 290 return 0; 291 292 for_nexthops(fi) { 293 onh = fib_info_nh(ofi, nhsel); 294 295 if (nh->fib_nh_oif != onh->fib_nh_oif || 296 nh->fib_nh_gw_family != onh->fib_nh_gw_family || 297 nh->fib_nh_scope != onh->fib_nh_scope || 298 #ifdef CONFIG_IP_ROUTE_MULTIPATH 299 nh->fib_nh_weight != onh->fib_nh_weight || 300 #endif 301 #ifdef CONFIG_IP_ROUTE_CLASSID 302 nh->nh_tclassid != onh->nh_tclassid || 303 #endif 304 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) || 305 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK)) 306 return -1; 307 308 if (nh->fib_nh_gw_family == AF_INET && 309 nh->fib_nh_gw4 != onh->fib_nh_gw4) 310 return -1; 311 312 if (nh->fib_nh_gw_family == AF_INET6 && 313 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6)) 314 return -1; 315 } endfor_nexthops(fi); 316 return 0; 317 } 318 319 static inline unsigned int fib_devindex_hashfn(unsigned int val) 320 { 321 unsigned int mask = DEVINDEX_HASHSIZE - 1; 322 323 return (val ^ 324 (val >> DEVINDEX_HASHBITS) ^ 325 (val >> (DEVINDEX_HASHBITS * 2))) & mask; 326 } 327 328 static inline unsigned int fib_info_hashfn(const struct fib_info *fi) 329 { 330 unsigned int mask = (fib_info_hash_size - 1); 331 unsigned int val = fi->fib_nhs; 332 333 val ^= (fi->fib_protocol << 8) | fi->fib_scope; 334 val ^= (__force u32)fi->fib_prefsrc; 335 val ^= fi->fib_priority; 336 337 if (fi->nh) { 338 val ^= fib_devindex_hashfn(fi->nh->id); 339 } else { 340 for_nexthops(fi) { 341 val ^= fib_devindex_hashfn(nh->fib_nh_oif); 342 } endfor_nexthops(fi) 343 } 344 345 return (val ^ (val >> 7) ^ (val >> 12)) & mask; 346 } 347 348 static struct fib_info *fib_find_info(struct fib_info *nfi) 349 { 350 struct hlist_head *head; 351 struct fib_info *fi; 352 unsigned int hash; 353 354 hash = fib_info_hashfn(nfi); 355 head = &fib_info_hash[hash]; 356 357 hlist_for_each_entry(fi, head, fib_hash) { 358 if (!net_eq(fi->fib_net, nfi->fib_net)) 359 continue; 360 if (fi->fib_nhs != nfi->fib_nhs) 361 continue; 362 if (nfi->fib_protocol == fi->fib_protocol && 363 nfi->fib_scope == fi->fib_scope && 364 nfi->fib_prefsrc == fi->fib_prefsrc && 365 nfi->fib_priority == fi->fib_priority && 366 nfi->fib_type == fi->fib_type && 367 memcmp(nfi->fib_metrics, fi->fib_metrics, 368 sizeof(u32) * RTAX_MAX) == 0 && 369 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) && 370 nh_comp(fi, nfi) == 0) 371 return fi; 372 } 373 374 return NULL; 375 } 376 377 /* Check, that the gateway is already configured. 378 * Used only by redirect accept routine. 379 */ 380 int ip_fib_check_default(__be32 gw, struct net_device *dev) 381 { 382 struct hlist_head *head; 383 struct fib_nh *nh; 384 unsigned int hash; 385 386 spin_lock(&fib_info_lock); 387 388 hash = fib_devindex_hashfn(dev->ifindex); 389 head = &fib_info_devhash[hash]; 390 hlist_for_each_entry(nh, head, nh_hash) { 391 if (nh->fib_nh_dev == dev && 392 nh->fib_nh_gw4 == gw && 393 !(nh->fib_nh_flags & RTNH_F_DEAD)) { 394 spin_unlock(&fib_info_lock); 395 return 0; 396 } 397 } 398 399 spin_unlock(&fib_info_lock); 400 401 return -1; 402 } 403 404 static inline size_t fib_nlmsg_size(struct fib_info *fi) 405 { 406 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) 407 + nla_total_size(4) /* RTA_TABLE */ 408 + nla_total_size(4) /* RTA_DST */ 409 + nla_total_size(4) /* RTA_PRIORITY */ 410 + nla_total_size(4) /* RTA_PREFSRC */ 411 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */ 412 unsigned int nhs = fib_info_num_path(fi); 413 414 /* space for nested metrics */ 415 payload += nla_total_size((RTAX_MAX * nla_total_size(4))); 416 417 if (fi->nh) 418 payload += nla_total_size(4); /* RTA_NH_ID */ 419 420 if (nhs) { 421 size_t nh_encapsize = 0; 422 /* Also handles the special case nhs == 1 */ 423 424 /* each nexthop is packed in an attribute */ 425 size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); 426 unsigned int i; 427 428 /* may contain flow and gateway attribute */ 429 nhsize += 2 * nla_total_size(4); 430 431 /* grab encap info */ 432 for (i = 0; i < fib_info_num_path(fi); i++) { 433 struct fib_nh_common *nhc = fib_info_nhc(fi, i); 434 435 if (nhc->nhc_lwtstate) { 436 /* RTA_ENCAP_TYPE */ 437 nh_encapsize += lwtunnel_get_encap_size( 438 nhc->nhc_lwtstate); 439 /* RTA_ENCAP */ 440 nh_encapsize += nla_total_size(2); 441 } 442 } 443 444 /* all nexthops are packed in a nested attribute */ 445 payload += nla_total_size((nhs * nhsize) + nh_encapsize); 446 447 } 448 449 return payload; 450 } 451 452 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, 453 int dst_len, u32 tb_id, const struct nl_info *info, 454 unsigned int nlm_flags) 455 { 456 struct sk_buff *skb; 457 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 458 int err = -ENOBUFS; 459 460 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); 461 if (!skb) 462 goto errout; 463 464 err = fib_dump_info(skb, info->portid, seq, event, tb_id, 465 fa->fa_type, key, dst_len, 466 fa->fa_tos, fa->fa_info, nlm_flags); 467 if (err < 0) { 468 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ 469 WARN_ON(err == -EMSGSIZE); 470 kfree_skb(skb); 471 goto errout; 472 } 473 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE, 474 info->nlh, GFP_KERNEL); 475 return; 476 errout: 477 if (err < 0) 478 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); 479 } 480 481 static int fib_detect_death(struct fib_info *fi, int order, 482 struct fib_info **last_resort, int *last_idx, 483 int dflt) 484 { 485 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0); 486 struct neighbour *n; 487 int state = NUD_NONE; 488 489 if (likely(nhc->nhc_gw_family == AF_INET)) 490 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev); 491 else if (nhc->nhc_gw_family == AF_INET6) 492 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6, 493 nhc->nhc_dev); 494 else 495 n = NULL; 496 497 if (n) { 498 state = n->nud_state; 499 neigh_release(n); 500 } else { 501 return 0; 502 } 503 if (state == NUD_REACHABLE) 504 return 0; 505 if ((state & NUD_VALID) && order != dflt) 506 return 0; 507 if ((state & NUD_VALID) || 508 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) { 509 *last_resort = fi; 510 *last_idx = order; 511 } 512 return 1; 513 } 514 515 int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap, 516 u16 encap_type, void *cfg, gfp_t gfp_flags, 517 struct netlink_ext_ack *extack) 518 { 519 int err; 520 521 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *, 522 gfp_flags); 523 if (!nhc->nhc_pcpu_rth_output) 524 return -ENOMEM; 525 526 if (encap) { 527 struct lwtunnel_state *lwtstate; 528 529 if (encap_type == LWTUNNEL_ENCAP_NONE) { 530 NL_SET_ERR_MSG(extack, "LWT encap type not specified"); 531 err = -EINVAL; 532 goto lwt_failure; 533 } 534 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family, 535 cfg, &lwtstate, extack); 536 if (err) 537 goto lwt_failure; 538 539 nhc->nhc_lwtstate = lwtstate_get(lwtstate); 540 } 541 542 return 0; 543 544 lwt_failure: 545 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output); 546 nhc->nhc_pcpu_rth_output = NULL; 547 return err; 548 } 549 EXPORT_SYMBOL_GPL(fib_nh_common_init); 550 551 int fib_nh_init(struct net *net, struct fib_nh *nh, 552 struct fib_config *cfg, int nh_weight, 553 struct netlink_ext_ack *extack) 554 { 555 int err; 556 557 nh->fib_nh_family = AF_INET; 558 559 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap, 560 cfg->fc_encap_type, cfg, GFP_KERNEL, extack); 561 if (err) 562 return err; 563 564 nh->fib_nh_oif = cfg->fc_oif; 565 nh->fib_nh_gw_family = cfg->fc_gw_family; 566 if (cfg->fc_gw_family == AF_INET) 567 nh->fib_nh_gw4 = cfg->fc_gw4; 568 else if (cfg->fc_gw_family == AF_INET6) 569 nh->fib_nh_gw6 = cfg->fc_gw6; 570 571 nh->fib_nh_flags = cfg->fc_flags; 572 573 #ifdef CONFIG_IP_ROUTE_CLASSID 574 nh->nh_tclassid = cfg->fc_flow; 575 if (nh->nh_tclassid) 576 net->ipv4.fib_num_tclassid_users++; 577 #endif 578 #ifdef CONFIG_IP_ROUTE_MULTIPATH 579 nh->fib_nh_weight = nh_weight; 580 #endif 581 return 0; 582 } 583 584 #ifdef CONFIG_IP_ROUTE_MULTIPATH 585 586 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining, 587 struct netlink_ext_ack *extack) 588 { 589 int nhs = 0; 590 591 while (rtnh_ok(rtnh, remaining)) { 592 nhs++; 593 rtnh = rtnh_next(rtnh, &remaining); 594 } 595 596 /* leftover implies invalid nexthop configuration, discard it */ 597 if (remaining > 0) { 598 NL_SET_ERR_MSG(extack, 599 "Invalid nexthop configuration - extra data after nexthops"); 600 nhs = 0; 601 } 602 603 return nhs; 604 } 605 606 /* only called when fib_nh is integrated into fib_info */ 607 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, 608 int remaining, struct fib_config *cfg, 609 struct netlink_ext_ack *extack) 610 { 611 struct net *net = fi->fib_net; 612 struct fib_config fib_cfg; 613 struct fib_nh *nh; 614 int ret; 615 616 change_nexthops(fi) { 617 int attrlen; 618 619 memset(&fib_cfg, 0, sizeof(fib_cfg)); 620 621 if (!rtnh_ok(rtnh, remaining)) { 622 NL_SET_ERR_MSG(extack, 623 "Invalid nexthop configuration - extra data after nexthop"); 624 return -EINVAL; 625 } 626 627 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { 628 NL_SET_ERR_MSG(extack, 629 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN"); 630 return -EINVAL; 631 } 632 633 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; 634 fib_cfg.fc_oif = rtnh->rtnh_ifindex; 635 636 attrlen = rtnh_attrlen(rtnh); 637 if (attrlen > 0) { 638 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh); 639 640 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 641 nlav = nla_find(attrs, attrlen, RTA_VIA); 642 if (nla && nlav) { 643 NL_SET_ERR_MSG(extack, 644 "Nexthop configuration can not contain both GATEWAY and VIA"); 645 return -EINVAL; 646 } 647 if (nla) { 648 fib_cfg.fc_gw4 = nla_get_in_addr(nla); 649 if (fib_cfg.fc_gw4) 650 fib_cfg.fc_gw_family = AF_INET; 651 } else if (nlav) { 652 ret = fib_gw_from_via(&fib_cfg, nlav, extack); 653 if (ret) 654 goto errout; 655 } 656 657 nla = nla_find(attrs, attrlen, RTA_FLOW); 658 if (nla) 659 fib_cfg.fc_flow = nla_get_u32(nla); 660 661 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 662 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 663 if (nla) 664 fib_cfg.fc_encap_type = nla_get_u16(nla); 665 } 666 667 ret = fib_nh_init(net, nexthop_nh, &fib_cfg, 668 rtnh->rtnh_hops + 1, extack); 669 if (ret) 670 goto errout; 671 672 rtnh = rtnh_next(rtnh, &remaining); 673 } endfor_nexthops(fi); 674 675 ret = -EINVAL; 676 nh = fib_info_nh(fi, 0); 677 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) { 678 NL_SET_ERR_MSG(extack, 679 "Nexthop device index does not match RTA_OIF"); 680 goto errout; 681 } 682 if (cfg->fc_gw_family) { 683 if (cfg->fc_gw_family != nh->fib_nh_gw_family || 684 (cfg->fc_gw_family == AF_INET && 685 nh->fib_nh_gw4 != cfg->fc_gw4) || 686 (cfg->fc_gw_family == AF_INET6 && 687 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) { 688 NL_SET_ERR_MSG(extack, 689 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA"); 690 goto errout; 691 } 692 } 693 #ifdef CONFIG_IP_ROUTE_CLASSID 694 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) { 695 NL_SET_ERR_MSG(extack, 696 "Nexthop class id does not match RTA_FLOW"); 697 goto errout; 698 } 699 #endif 700 ret = 0; 701 errout: 702 return ret; 703 } 704 705 /* only called when fib_nh is integrated into fib_info */ 706 static void fib_rebalance(struct fib_info *fi) 707 { 708 int total; 709 int w; 710 711 if (fib_info_num_path(fi) < 2) 712 return; 713 714 total = 0; 715 for_nexthops(fi) { 716 if (nh->fib_nh_flags & RTNH_F_DEAD) 717 continue; 718 719 if (ip_ignore_linkdown(nh->fib_nh_dev) && 720 nh->fib_nh_flags & RTNH_F_LINKDOWN) 721 continue; 722 723 total += nh->fib_nh_weight; 724 } endfor_nexthops(fi); 725 726 w = 0; 727 change_nexthops(fi) { 728 int upper_bound; 729 730 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) { 731 upper_bound = -1; 732 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) && 733 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) { 734 upper_bound = -1; 735 } else { 736 w += nexthop_nh->fib_nh_weight; 737 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, 738 total) - 1; 739 } 740 741 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound); 742 } endfor_nexthops(fi); 743 } 744 #else /* CONFIG_IP_ROUTE_MULTIPATH */ 745 746 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, 747 int remaining, struct fib_config *cfg, 748 struct netlink_ext_ack *extack) 749 { 750 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel"); 751 752 return -EINVAL; 753 } 754 755 #define fib_rebalance(fi) do { } while (0) 756 757 #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 758 759 static int fib_encap_match(u16 encap_type, 760 struct nlattr *encap, 761 const struct fib_nh *nh, 762 const struct fib_config *cfg, 763 struct netlink_ext_ack *extack) 764 { 765 struct lwtunnel_state *lwtstate; 766 int ret, result = 0; 767 768 if (encap_type == LWTUNNEL_ENCAP_NONE) 769 return 0; 770 771 ret = lwtunnel_build_state(encap_type, encap, AF_INET, 772 cfg, &lwtstate, extack); 773 if (!ret) { 774 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws); 775 lwtstate_free(lwtstate); 776 } 777 778 return result; 779 } 780 781 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi, 782 struct netlink_ext_ack *extack) 783 { 784 #ifdef CONFIG_IP_ROUTE_MULTIPATH 785 struct rtnexthop *rtnh; 786 int remaining; 787 #endif 788 789 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) 790 return 1; 791 792 if (cfg->fc_oif || cfg->fc_gw_family) { 793 struct fib_nh *nh = fib_info_nh(fi, 0); 794 795 if (cfg->fc_encap) { 796 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap, 797 nh, cfg, extack)) 798 return 1; 799 } 800 #ifdef CONFIG_IP_ROUTE_CLASSID 801 if (cfg->fc_flow && 802 cfg->fc_flow != nh->nh_tclassid) 803 return 1; 804 #endif 805 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) || 806 (cfg->fc_gw_family && 807 cfg->fc_gw_family != nh->fib_nh_gw_family)) 808 return 1; 809 810 if (cfg->fc_gw_family == AF_INET && 811 cfg->fc_gw4 != nh->fib_nh_gw4) 812 return 1; 813 814 if (cfg->fc_gw_family == AF_INET6 && 815 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6)) 816 return 1; 817 818 return 0; 819 } 820 821 #ifdef CONFIG_IP_ROUTE_MULTIPATH 822 if (!cfg->fc_mp) 823 return 0; 824 825 rtnh = cfg->fc_mp; 826 remaining = cfg->fc_mp_len; 827 828 for_nexthops(fi) { 829 int attrlen; 830 831 if (!rtnh_ok(rtnh, remaining)) 832 return -EINVAL; 833 834 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif) 835 return 1; 836 837 attrlen = rtnh_attrlen(rtnh); 838 if (attrlen > 0) { 839 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh); 840 841 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 842 nlav = nla_find(attrs, attrlen, RTA_VIA); 843 if (nla && nlav) { 844 NL_SET_ERR_MSG(extack, 845 "Nexthop configuration can not contain both GATEWAY and VIA"); 846 return -EINVAL; 847 } 848 849 if (nla) { 850 if (nh->fib_nh_gw_family != AF_INET || 851 nla_get_in_addr(nla) != nh->fib_nh_gw4) 852 return 1; 853 } else if (nlav) { 854 struct fib_config cfg2; 855 int err; 856 857 err = fib_gw_from_via(&cfg2, nlav, extack); 858 if (err) 859 return err; 860 861 switch (nh->fib_nh_gw_family) { 862 case AF_INET: 863 if (cfg2.fc_gw_family != AF_INET || 864 cfg2.fc_gw4 != nh->fib_nh_gw4) 865 return 1; 866 break; 867 case AF_INET6: 868 if (cfg2.fc_gw_family != AF_INET6 || 869 ipv6_addr_cmp(&cfg2.fc_gw6, 870 &nh->fib_nh_gw6)) 871 return 1; 872 break; 873 } 874 } 875 876 #ifdef CONFIG_IP_ROUTE_CLASSID 877 nla = nla_find(attrs, attrlen, RTA_FLOW); 878 if (nla && nla_get_u32(nla) != nh->nh_tclassid) 879 return 1; 880 #endif 881 } 882 883 rtnh = rtnh_next(rtnh, &remaining); 884 } endfor_nexthops(fi); 885 #endif 886 return 0; 887 } 888 889 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi) 890 { 891 struct nlattr *nla; 892 int remaining; 893 894 if (!cfg->fc_mx) 895 return true; 896 897 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 898 int type = nla_type(nla); 899 u32 fi_val, val; 900 901 if (!type) 902 continue; 903 if (type > RTAX_MAX) 904 return false; 905 906 if (type == RTAX_CC_ALGO) { 907 char tmp[TCP_CA_NAME_MAX]; 908 bool ecn_ca = false; 909 910 nla_strlcpy(tmp, nla, sizeof(tmp)); 911 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca); 912 } else { 913 if (nla_len(nla) != sizeof(u32)) 914 return false; 915 val = nla_get_u32(nla); 916 } 917 918 fi_val = fi->fib_metrics->metrics[type - 1]; 919 if (type == RTAX_FEATURES) 920 fi_val &= ~DST_FEATURE_ECN_CA; 921 922 if (fi_val != val) 923 return false; 924 } 925 926 return true; 927 } 928 929 static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh, 930 u32 table, struct netlink_ext_ack *extack) 931 { 932 struct fib6_config cfg = { 933 .fc_table = table, 934 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY, 935 .fc_ifindex = nh->fib_nh_oif, 936 .fc_gateway = nh->fib_nh_gw6, 937 }; 938 struct fib6_nh fib6_nh = {}; 939 int err; 940 941 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack); 942 if (!err) { 943 nh->fib_nh_dev = fib6_nh.fib_nh_dev; 944 dev_hold(nh->fib_nh_dev); 945 nh->fib_nh_oif = nh->fib_nh_dev->ifindex; 946 nh->fib_nh_scope = RT_SCOPE_LINK; 947 948 ipv6_stub->fib6_nh_release(&fib6_nh); 949 } 950 951 return err; 952 } 953 954 /* 955 * Picture 956 * ------- 957 * 958 * Semantics of nexthop is very messy by historical reasons. 959 * We have to take into account, that: 960 * a) gateway can be actually local interface address, 961 * so that gatewayed route is direct. 962 * b) gateway must be on-link address, possibly 963 * described not by an ifaddr, but also by a direct route. 964 * c) If both gateway and interface are specified, they should not 965 * contradict. 966 * d) If we use tunnel routes, gateway could be not on-link. 967 * 968 * Attempt to reconcile all of these (alas, self-contradictory) conditions 969 * results in pretty ugly and hairy code with obscure logic. 970 * 971 * I chose to generalized it instead, so that the size 972 * of code does not increase practically, but it becomes 973 * much more general. 974 * Every prefix is assigned a "scope" value: "host" is local address, 975 * "link" is direct route, 976 * [ ... "site" ... "interior" ... ] 977 * and "universe" is true gateway route with global meaning. 978 * 979 * Every prefix refers to a set of "nexthop"s (gw, oif), 980 * where gw must have narrower scope. This recursion stops 981 * when gw has LOCAL scope or if "nexthop" is declared ONLINK, 982 * which means that gw is forced to be on link. 983 * 984 * Code is still hairy, but now it is apparently logically 985 * consistent and very flexible. F.e. as by-product it allows 986 * to co-exists in peace independent exterior and interior 987 * routing processes. 988 * 989 * Normally it looks as following. 990 * 991 * {universe prefix} -> (gw, oif) [scope link] 992 * | 993 * |-> {link prefix} -> (gw, oif) [scope local] 994 * | 995 * |-> {local prefix} (terminal node) 996 */ 997 static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table, 998 u8 scope, struct netlink_ext_ack *extack) 999 { 1000 struct net_device *dev; 1001 struct fib_result res; 1002 int err; 1003 1004 if (nh->fib_nh_flags & RTNH_F_ONLINK) { 1005 unsigned int addr_type; 1006 1007 if (scope >= RT_SCOPE_LINK) { 1008 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope"); 1009 return -EINVAL; 1010 } 1011 dev = __dev_get_by_index(net, nh->fib_nh_oif); 1012 if (!dev) { 1013 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink"); 1014 return -ENODEV; 1015 } 1016 if (!(dev->flags & IFF_UP)) { 1017 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 1018 return -ENETDOWN; 1019 } 1020 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4); 1021 if (addr_type != RTN_UNICAST) { 1022 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); 1023 return -EINVAL; 1024 } 1025 if (!netif_carrier_ok(dev)) 1026 nh->fib_nh_flags |= RTNH_F_LINKDOWN; 1027 nh->fib_nh_dev = dev; 1028 dev_hold(dev); 1029 nh->fib_nh_scope = RT_SCOPE_LINK; 1030 return 0; 1031 } 1032 rcu_read_lock(); 1033 { 1034 struct fib_table *tbl = NULL; 1035 struct flowi4 fl4 = { 1036 .daddr = nh->fib_nh_gw4, 1037 .flowi4_scope = scope + 1, 1038 .flowi4_oif = nh->fib_nh_oif, 1039 .flowi4_iif = LOOPBACK_IFINDEX, 1040 }; 1041 1042 /* It is not necessary, but requires a bit of thinking */ 1043 if (fl4.flowi4_scope < RT_SCOPE_LINK) 1044 fl4.flowi4_scope = RT_SCOPE_LINK; 1045 1046 if (table) 1047 tbl = fib_get_table(net, table); 1048 1049 if (tbl) 1050 err = fib_table_lookup(tbl, &fl4, &res, 1051 FIB_LOOKUP_IGNORE_LINKSTATE | 1052 FIB_LOOKUP_NOREF); 1053 1054 /* on error or if no table given do full lookup. This 1055 * is needed for example when nexthops are in the local 1056 * table rather than the given table 1057 */ 1058 if (!tbl || err) { 1059 err = fib_lookup(net, &fl4, &res, 1060 FIB_LOOKUP_IGNORE_LINKSTATE); 1061 } 1062 1063 if (err) { 1064 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); 1065 goto out; 1066 } 1067 } 1068 1069 err = -EINVAL; 1070 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) { 1071 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); 1072 goto out; 1073 } 1074 nh->fib_nh_scope = res.scope; 1075 nh->fib_nh_oif = FIB_RES_OIF(res); 1076 nh->fib_nh_dev = dev = FIB_RES_DEV(res); 1077 if (!dev) { 1078 NL_SET_ERR_MSG(extack, 1079 "No egress device for nexthop gateway"); 1080 goto out; 1081 } 1082 dev_hold(dev); 1083 if (!netif_carrier_ok(dev)) 1084 nh->fib_nh_flags |= RTNH_F_LINKDOWN; 1085 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; 1086 out: 1087 rcu_read_unlock(); 1088 return err; 1089 } 1090 1091 static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh, 1092 struct netlink_ext_ack *extack) 1093 { 1094 struct in_device *in_dev; 1095 int err; 1096 1097 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) { 1098 NL_SET_ERR_MSG(extack, 1099 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set"); 1100 return -EINVAL; 1101 } 1102 1103 rcu_read_lock(); 1104 1105 err = -ENODEV; 1106 in_dev = inetdev_by_index(net, nh->fib_nh_oif); 1107 if (!in_dev) 1108 goto out; 1109 err = -ENETDOWN; 1110 if (!(in_dev->dev->flags & IFF_UP)) { 1111 NL_SET_ERR_MSG(extack, "Device for nexthop is not up"); 1112 goto out; 1113 } 1114 1115 nh->fib_nh_dev = in_dev->dev; 1116 dev_hold(nh->fib_nh_dev); 1117 nh->fib_nh_scope = RT_SCOPE_HOST; 1118 if (!netif_carrier_ok(nh->fib_nh_dev)) 1119 nh->fib_nh_flags |= RTNH_F_LINKDOWN; 1120 err = 0; 1121 out: 1122 rcu_read_unlock(); 1123 return err; 1124 } 1125 1126 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope, 1127 struct netlink_ext_ack *extack) 1128 { 1129 int err; 1130 1131 if (nh->fib_nh_gw_family == AF_INET) 1132 err = fib_check_nh_v4_gw(net, nh, table, scope, extack); 1133 else if (nh->fib_nh_gw_family == AF_INET6) 1134 err = fib_check_nh_v6_gw(net, nh, table, extack); 1135 else 1136 err = fib_check_nh_nongw(net, nh, extack); 1137 1138 return err; 1139 } 1140 1141 static inline unsigned int fib_laddr_hashfn(__be32 val) 1142 { 1143 unsigned int mask = (fib_info_hash_size - 1); 1144 1145 return ((__force u32)val ^ 1146 ((__force u32)val >> 7) ^ 1147 ((__force u32)val >> 14)) & mask; 1148 } 1149 1150 static struct hlist_head *fib_info_hash_alloc(int bytes) 1151 { 1152 if (bytes <= PAGE_SIZE) 1153 return kzalloc(bytes, GFP_KERNEL); 1154 else 1155 return (struct hlist_head *) 1156 __get_free_pages(GFP_KERNEL | __GFP_ZERO, 1157 get_order(bytes)); 1158 } 1159 1160 static void fib_info_hash_free(struct hlist_head *hash, int bytes) 1161 { 1162 if (!hash) 1163 return; 1164 1165 if (bytes <= PAGE_SIZE) 1166 kfree(hash); 1167 else 1168 free_pages((unsigned long) hash, get_order(bytes)); 1169 } 1170 1171 static void fib_info_hash_move(struct hlist_head *new_info_hash, 1172 struct hlist_head *new_laddrhash, 1173 unsigned int new_size) 1174 { 1175 struct hlist_head *old_info_hash, *old_laddrhash; 1176 unsigned int old_size = fib_info_hash_size; 1177 unsigned int i, bytes; 1178 1179 spin_lock_bh(&fib_info_lock); 1180 old_info_hash = fib_info_hash; 1181 old_laddrhash = fib_info_laddrhash; 1182 fib_info_hash_size = new_size; 1183 1184 for (i = 0; i < old_size; i++) { 1185 struct hlist_head *head = &fib_info_hash[i]; 1186 struct hlist_node *n; 1187 struct fib_info *fi; 1188 1189 hlist_for_each_entry_safe(fi, n, head, fib_hash) { 1190 struct hlist_head *dest; 1191 unsigned int new_hash; 1192 1193 new_hash = fib_info_hashfn(fi); 1194 dest = &new_info_hash[new_hash]; 1195 hlist_add_head(&fi->fib_hash, dest); 1196 } 1197 } 1198 fib_info_hash = new_info_hash; 1199 1200 for (i = 0; i < old_size; i++) { 1201 struct hlist_head *lhead = &fib_info_laddrhash[i]; 1202 struct hlist_node *n; 1203 struct fib_info *fi; 1204 1205 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) { 1206 struct hlist_head *ldest; 1207 unsigned int new_hash; 1208 1209 new_hash = fib_laddr_hashfn(fi->fib_prefsrc); 1210 ldest = &new_laddrhash[new_hash]; 1211 hlist_add_head(&fi->fib_lhash, ldest); 1212 } 1213 } 1214 fib_info_laddrhash = new_laddrhash; 1215 1216 spin_unlock_bh(&fib_info_lock); 1217 1218 bytes = old_size * sizeof(struct hlist_head *); 1219 fib_info_hash_free(old_info_hash, bytes); 1220 fib_info_hash_free(old_laddrhash, bytes); 1221 } 1222 1223 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc, 1224 unsigned char scope) 1225 { 1226 struct fib_nh *nh; 1227 1228 if (nhc->nhc_family != AF_INET) 1229 return inet_select_addr(nhc->nhc_dev, 0, scope); 1230 1231 nh = container_of(nhc, struct fib_nh, nh_common); 1232 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope); 1233 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); 1234 1235 return nh->nh_saddr; 1236 } 1237 1238 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res) 1239 { 1240 struct fib_nh_common *nhc = res->nhc; 1241 1242 if (res->fi->fib_prefsrc) 1243 return res->fi->fib_prefsrc; 1244 1245 if (nhc->nhc_family == AF_INET) { 1246 struct fib_nh *nh; 1247 1248 nh = container_of(nhc, struct fib_nh, nh_common); 1249 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid)) 1250 return nh->nh_saddr; 1251 } 1252 1253 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope); 1254 } 1255 1256 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc) 1257 { 1258 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || 1259 fib_prefsrc != cfg->fc_dst) { 1260 u32 tb_id = cfg->fc_table; 1261 int rc; 1262 1263 if (tb_id == RT_TABLE_MAIN) 1264 tb_id = RT_TABLE_LOCAL; 1265 1266 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, 1267 fib_prefsrc, tb_id); 1268 1269 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) { 1270 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, 1271 fib_prefsrc, RT_TABLE_LOCAL); 1272 } 1273 1274 if (rc != RTN_LOCAL) 1275 return false; 1276 } 1277 return true; 1278 } 1279 1280 struct fib_info *fib_create_info(struct fib_config *cfg, 1281 struct netlink_ext_ack *extack) 1282 { 1283 int err; 1284 struct fib_info *fi = NULL; 1285 struct nexthop *nh = NULL; 1286 struct fib_info *ofi; 1287 int nhs = 1; 1288 struct net *net = cfg->fc_nlinfo.nl_net; 1289 1290 if (cfg->fc_type > RTN_MAX) 1291 goto err_inval; 1292 1293 /* Fast check to catch the most weird cases */ 1294 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) { 1295 NL_SET_ERR_MSG(extack, "Invalid scope"); 1296 goto err_inval; 1297 } 1298 1299 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { 1300 NL_SET_ERR_MSG(extack, 1301 "Invalid rtm_flags - can not contain DEAD or LINKDOWN"); 1302 goto err_inval; 1303 } 1304 1305 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1306 if (cfg->fc_mp) { 1307 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack); 1308 if (nhs == 0) 1309 goto err_inval; 1310 } 1311 #endif 1312 1313 err = -ENOBUFS; 1314 if (fib_info_cnt >= fib_info_hash_size) { 1315 unsigned int new_size = fib_info_hash_size << 1; 1316 struct hlist_head *new_info_hash; 1317 struct hlist_head *new_laddrhash; 1318 unsigned int bytes; 1319 1320 if (!new_size) 1321 new_size = 16; 1322 bytes = new_size * sizeof(struct hlist_head *); 1323 new_info_hash = fib_info_hash_alloc(bytes); 1324 new_laddrhash = fib_info_hash_alloc(bytes); 1325 if (!new_info_hash || !new_laddrhash) { 1326 fib_info_hash_free(new_info_hash, bytes); 1327 fib_info_hash_free(new_laddrhash, bytes); 1328 } else 1329 fib_info_hash_move(new_info_hash, new_laddrhash, new_size); 1330 1331 if (!fib_info_hash_size) 1332 goto failure; 1333 } 1334 1335 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); 1336 if (!fi) 1337 goto failure; 1338 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx, 1339 cfg->fc_mx_len, extack); 1340 if (IS_ERR(fi->fib_metrics)) { 1341 err = PTR_ERR(fi->fib_metrics); 1342 kfree(fi); 1343 return ERR_PTR(err); 1344 } 1345 1346 fib_info_cnt++; 1347 fi->fib_net = net; 1348 fi->fib_protocol = cfg->fc_protocol; 1349 fi->fib_scope = cfg->fc_scope; 1350 fi->fib_flags = cfg->fc_flags; 1351 fi->fib_priority = cfg->fc_priority; 1352 fi->fib_prefsrc = cfg->fc_prefsrc; 1353 fi->fib_type = cfg->fc_type; 1354 fi->fib_tb_id = cfg->fc_table; 1355 1356 fi->fib_nhs = nhs; 1357 if (nh) { 1358 if (!nexthop_get(nh)) { 1359 NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 1360 err = -EINVAL; 1361 } else { 1362 err = 0; 1363 fi->nh = nh; 1364 } 1365 } else { 1366 change_nexthops(fi) { 1367 nexthop_nh->nh_parent = fi; 1368 } endfor_nexthops(fi) 1369 1370 if (cfg->fc_mp) 1371 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, 1372 extack); 1373 else 1374 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack); 1375 } 1376 1377 if (err != 0) 1378 goto failure; 1379 1380 if (fib_props[cfg->fc_type].error) { 1381 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) { 1382 NL_SET_ERR_MSG(extack, 1383 "Gateway, device and multipath can not be specified for this route type"); 1384 goto err_inval; 1385 } 1386 goto link_it; 1387 } else { 1388 switch (cfg->fc_type) { 1389 case RTN_UNICAST: 1390 case RTN_LOCAL: 1391 case RTN_BROADCAST: 1392 case RTN_ANYCAST: 1393 case RTN_MULTICAST: 1394 break; 1395 default: 1396 NL_SET_ERR_MSG(extack, "Invalid route type"); 1397 goto err_inval; 1398 } 1399 } 1400 1401 if (cfg->fc_scope > RT_SCOPE_HOST) { 1402 NL_SET_ERR_MSG(extack, "Invalid scope"); 1403 goto err_inval; 1404 } 1405 1406 if (fi->nh) { 1407 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack); 1408 if (err) 1409 goto failure; 1410 } else if (cfg->fc_scope == RT_SCOPE_HOST) { 1411 struct fib_nh *nh = fi->fib_nh; 1412 1413 /* Local address is added. */ 1414 if (nhs != 1) { 1415 NL_SET_ERR_MSG(extack, 1416 "Route with host scope can not have multiple nexthops"); 1417 goto err_inval; 1418 } 1419 if (nh->fib_nh_gw_family) { 1420 NL_SET_ERR_MSG(extack, 1421 "Route with host scope can not have a gateway"); 1422 goto err_inval; 1423 } 1424 nh->fib_nh_scope = RT_SCOPE_NOWHERE; 1425 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif); 1426 err = -ENODEV; 1427 if (!nh->fib_nh_dev) 1428 goto failure; 1429 } else { 1430 int linkdown = 0; 1431 1432 change_nexthops(fi) { 1433 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh, 1434 cfg->fc_table, cfg->fc_scope, 1435 extack); 1436 if (err != 0) 1437 goto failure; 1438 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) 1439 linkdown++; 1440 } endfor_nexthops(fi) 1441 if (linkdown == fi->fib_nhs) 1442 fi->fib_flags |= RTNH_F_LINKDOWN; 1443 } 1444 1445 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) { 1446 NL_SET_ERR_MSG(extack, "Invalid prefsrc address"); 1447 goto err_inval; 1448 } 1449 1450 if (!fi->nh) { 1451 change_nexthops(fi) { 1452 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common, 1453 fi->fib_scope); 1454 if (nexthop_nh->fib_nh_gw_family == AF_INET6) 1455 fi->fib_nh_is_v6 = true; 1456 } endfor_nexthops(fi) 1457 1458 fib_rebalance(fi); 1459 } 1460 1461 link_it: 1462 ofi = fib_find_info(fi); 1463 if (ofi) { 1464 fi->fib_dead = 1; 1465 free_fib_info(fi); 1466 ofi->fib_treeref++; 1467 return ofi; 1468 } 1469 1470 fi->fib_treeref++; 1471 refcount_set(&fi->fib_clntref, 1); 1472 spin_lock_bh(&fib_info_lock); 1473 hlist_add_head(&fi->fib_hash, 1474 &fib_info_hash[fib_info_hashfn(fi)]); 1475 if (fi->fib_prefsrc) { 1476 struct hlist_head *head; 1477 1478 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; 1479 hlist_add_head(&fi->fib_lhash, head); 1480 } 1481 if (fi->nh) { 1482 list_add(&fi->nh_list, &nh->fi_list); 1483 } else { 1484 change_nexthops(fi) { 1485 struct hlist_head *head; 1486 unsigned int hash; 1487 1488 if (!nexthop_nh->fib_nh_dev) 1489 continue; 1490 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex); 1491 head = &fib_info_devhash[hash]; 1492 hlist_add_head(&nexthop_nh->nh_hash, head); 1493 } endfor_nexthops(fi) 1494 } 1495 spin_unlock_bh(&fib_info_lock); 1496 return fi; 1497 1498 err_inval: 1499 err = -EINVAL; 1500 1501 failure: 1502 if (fi) { 1503 fi->fib_dead = 1; 1504 free_fib_info(fi); 1505 } 1506 1507 return ERR_PTR(err); 1508 } 1509 1510 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc, 1511 unsigned char *flags, bool skip_oif) 1512 { 1513 if (nhc->nhc_flags & RTNH_F_DEAD) 1514 *flags |= RTNH_F_DEAD; 1515 1516 if (nhc->nhc_flags & RTNH_F_LINKDOWN) { 1517 *flags |= RTNH_F_LINKDOWN; 1518 1519 rcu_read_lock(); 1520 switch (nhc->nhc_family) { 1521 case AF_INET: 1522 if (ip_ignore_linkdown(nhc->nhc_dev)) 1523 *flags |= RTNH_F_DEAD; 1524 break; 1525 case AF_INET6: 1526 if (ip6_ignore_linkdown(nhc->nhc_dev)) 1527 *flags |= RTNH_F_DEAD; 1528 break; 1529 } 1530 rcu_read_unlock(); 1531 } 1532 1533 switch (nhc->nhc_gw_family) { 1534 case AF_INET: 1535 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4)) 1536 goto nla_put_failure; 1537 break; 1538 case AF_INET6: 1539 /* if gateway family does not match nexthop family 1540 * gateway is encoded as RTA_VIA 1541 */ 1542 if (nhc->nhc_gw_family != nhc->nhc_family) { 1543 int alen = sizeof(struct in6_addr); 1544 struct nlattr *nla; 1545 struct rtvia *via; 1546 1547 nla = nla_reserve(skb, RTA_VIA, alen + 2); 1548 if (!nla) 1549 goto nla_put_failure; 1550 1551 via = nla_data(nla); 1552 via->rtvia_family = AF_INET6; 1553 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen); 1554 } else if (nla_put_in6_addr(skb, RTA_GATEWAY, 1555 &nhc->nhc_gw.ipv6) < 0) { 1556 goto nla_put_failure; 1557 } 1558 break; 1559 } 1560 1561 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK); 1562 if (nhc->nhc_flags & RTNH_F_OFFLOAD) 1563 *flags |= RTNH_F_OFFLOAD; 1564 1565 if (!skip_oif && nhc->nhc_dev && 1566 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex)) 1567 goto nla_put_failure; 1568 1569 if (nhc->nhc_lwtstate && 1570 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate, 1571 RTA_ENCAP, RTA_ENCAP_TYPE) < 0) 1572 goto nla_put_failure; 1573 1574 return 0; 1575 1576 nla_put_failure: 1577 return -EMSGSIZE; 1578 } 1579 EXPORT_SYMBOL_GPL(fib_nexthop_info); 1580 1581 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6) 1582 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc, 1583 int nh_weight) 1584 { 1585 const struct net_device *dev = nhc->nhc_dev; 1586 struct rtnexthop *rtnh; 1587 unsigned char flags = 0; 1588 1589 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 1590 if (!rtnh) 1591 goto nla_put_failure; 1592 1593 rtnh->rtnh_hops = nh_weight - 1; 1594 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0; 1595 1596 if (fib_nexthop_info(skb, nhc, &flags, true) < 0) 1597 goto nla_put_failure; 1598 1599 rtnh->rtnh_flags = flags; 1600 1601 /* length of rtnetlink header + attributes */ 1602 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 1603 1604 return 0; 1605 1606 nla_put_failure: 1607 return -EMSGSIZE; 1608 } 1609 EXPORT_SYMBOL_GPL(fib_add_nexthop); 1610 #endif 1611 1612 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1613 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi) 1614 { 1615 struct nlattr *mp; 1616 1617 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 1618 if (!mp) 1619 goto nla_put_failure; 1620 1621 if (unlikely(fi->nh)) { 1622 if (nexthop_mpath_fill_node(skb, fi->nh) < 0) 1623 goto nla_put_failure; 1624 goto mp_end; 1625 } 1626 1627 for_nexthops(fi) { 1628 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0) 1629 goto nla_put_failure; 1630 #ifdef CONFIG_IP_ROUTE_CLASSID 1631 if (nh->nh_tclassid && 1632 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) 1633 goto nla_put_failure; 1634 #endif 1635 } endfor_nexthops(fi); 1636 1637 mp_end: 1638 nla_nest_end(skb, mp); 1639 1640 return 0; 1641 1642 nla_put_failure: 1643 return -EMSGSIZE; 1644 } 1645 #else 1646 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi) 1647 { 1648 return 0; 1649 } 1650 #endif 1651 1652 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, 1653 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, 1654 struct fib_info *fi, unsigned int flags) 1655 { 1656 unsigned int nhs = fib_info_num_path(fi); 1657 struct nlmsghdr *nlh; 1658 struct rtmsg *rtm; 1659 1660 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); 1661 if (!nlh) 1662 return -EMSGSIZE; 1663 1664 rtm = nlmsg_data(nlh); 1665 rtm->rtm_family = AF_INET; 1666 rtm->rtm_dst_len = dst_len; 1667 rtm->rtm_src_len = 0; 1668 rtm->rtm_tos = tos; 1669 if (tb_id < 256) 1670 rtm->rtm_table = tb_id; 1671 else 1672 rtm->rtm_table = RT_TABLE_COMPAT; 1673 if (nla_put_u32(skb, RTA_TABLE, tb_id)) 1674 goto nla_put_failure; 1675 rtm->rtm_type = type; 1676 rtm->rtm_flags = fi->fib_flags; 1677 rtm->rtm_scope = fi->fib_scope; 1678 rtm->rtm_protocol = fi->fib_protocol; 1679 1680 if (rtm->rtm_dst_len && 1681 nla_put_in_addr(skb, RTA_DST, dst)) 1682 goto nla_put_failure; 1683 if (fi->fib_priority && 1684 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) 1685 goto nla_put_failure; 1686 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0) 1687 goto nla_put_failure; 1688 1689 if (fi->fib_prefsrc && 1690 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc)) 1691 goto nla_put_failure; 1692 1693 if (fi->nh) { 1694 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id)) 1695 goto nla_put_failure; 1696 if (nexthop_is_blackhole(fi->nh)) 1697 rtm->rtm_type = RTN_BLACKHOLE; 1698 } 1699 1700 if (nhs == 1) { 1701 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0); 1702 unsigned char flags = 0; 1703 1704 if (fib_nexthop_info(skb, nhc, &flags, false) < 0) 1705 goto nla_put_failure; 1706 1707 rtm->rtm_flags = flags; 1708 #ifdef CONFIG_IP_ROUTE_CLASSID 1709 if (nhc->nhc_family == AF_INET) { 1710 struct fib_nh *nh; 1711 1712 nh = container_of(nhc, struct fib_nh, nh_common); 1713 if (nh->nh_tclassid && 1714 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) 1715 goto nla_put_failure; 1716 } 1717 #endif 1718 } else { 1719 if (fib_add_multipath(skb, fi) < 0) 1720 goto nla_put_failure; 1721 } 1722 1723 nlmsg_end(skb, nlh); 1724 return 0; 1725 1726 nla_put_failure: 1727 nlmsg_cancel(skb, nlh); 1728 return -EMSGSIZE; 1729 } 1730 1731 /* 1732 * Update FIB if: 1733 * - local address disappeared -> we must delete all the entries 1734 * referring to it. 1735 * - device went down -> we must shutdown all nexthops going via it. 1736 */ 1737 int fib_sync_down_addr(struct net_device *dev, __be32 local) 1738 { 1739 int ret = 0; 1740 unsigned int hash = fib_laddr_hashfn(local); 1741 struct hlist_head *head = &fib_info_laddrhash[hash]; 1742 struct net *net = dev_net(dev); 1743 int tb_id = l3mdev_fib_table(dev); 1744 struct fib_info *fi; 1745 1746 if (!fib_info_laddrhash || local == 0) 1747 return 0; 1748 1749 hlist_for_each_entry(fi, head, fib_lhash) { 1750 if (!net_eq(fi->fib_net, net) || 1751 fi->fib_tb_id != tb_id) 1752 continue; 1753 if (fi->fib_prefsrc == local) { 1754 fi->fib_flags |= RTNH_F_DEAD; 1755 ret++; 1756 } 1757 } 1758 return ret; 1759 } 1760 1761 static int call_fib_nh_notifiers(struct fib_nh *nh, 1762 enum fib_event_type event_type) 1763 { 1764 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev); 1765 struct fib_nh_notifier_info info = { 1766 .fib_nh = nh, 1767 }; 1768 1769 switch (event_type) { 1770 case FIB_EVENT_NH_ADD: 1771 if (nh->fib_nh_flags & RTNH_F_DEAD) 1772 break; 1773 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) 1774 break; 1775 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type, 1776 &info.info); 1777 case FIB_EVENT_NH_DEL: 1778 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) || 1779 (nh->fib_nh_flags & RTNH_F_DEAD)) 1780 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), 1781 event_type, &info.info); 1782 default: 1783 break; 1784 } 1785 1786 return NOTIFY_DONE; 1787 } 1788 1789 /* Update the PMTU of exceptions when: 1790 * - the new MTU of the first hop becomes smaller than the PMTU 1791 * - the old MTU was the same as the PMTU, and it limited discovery of 1792 * larger MTUs on the path. With that limit raised, we can now 1793 * discover larger MTUs 1794 * A special case is locked exceptions, for which the PMTU is smaller 1795 * than the minimal accepted PMTU: 1796 * - if the new MTU is greater than the PMTU, don't make any change 1797 * - otherwise, unlock and set PMTU 1798 */ 1799 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig) 1800 { 1801 struct fnhe_hash_bucket *bucket; 1802 int i; 1803 1804 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1); 1805 if (!bucket) 1806 return; 1807 1808 for (i = 0; i < FNHE_HASH_SIZE; i++) { 1809 struct fib_nh_exception *fnhe; 1810 1811 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1); 1812 fnhe; 1813 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) { 1814 if (fnhe->fnhe_mtu_locked) { 1815 if (new <= fnhe->fnhe_pmtu) { 1816 fnhe->fnhe_pmtu = new; 1817 fnhe->fnhe_mtu_locked = false; 1818 } 1819 } else if (new < fnhe->fnhe_pmtu || 1820 orig == fnhe->fnhe_pmtu) { 1821 fnhe->fnhe_pmtu = new; 1822 } 1823 } 1824 } 1825 } 1826 1827 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu) 1828 { 1829 unsigned int hash = fib_devindex_hashfn(dev->ifindex); 1830 struct hlist_head *head = &fib_info_devhash[hash]; 1831 struct fib_nh *nh; 1832 1833 hlist_for_each_entry(nh, head, nh_hash) { 1834 if (nh->fib_nh_dev == dev) 1835 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu); 1836 } 1837 } 1838 1839 /* Event force Flags Description 1840 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host 1841 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host 1842 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed 1843 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed 1844 * 1845 * only used when fib_nh is built into fib_info 1846 */ 1847 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force) 1848 { 1849 int ret = 0; 1850 int scope = RT_SCOPE_NOWHERE; 1851 struct fib_info *prev_fi = NULL; 1852 unsigned int hash = fib_devindex_hashfn(dev->ifindex); 1853 struct hlist_head *head = &fib_info_devhash[hash]; 1854 struct fib_nh *nh; 1855 1856 if (force) 1857 scope = -1; 1858 1859 hlist_for_each_entry(nh, head, nh_hash) { 1860 struct fib_info *fi = nh->nh_parent; 1861 int dead; 1862 1863 BUG_ON(!fi->fib_nhs); 1864 if (nh->fib_nh_dev != dev || fi == prev_fi) 1865 continue; 1866 prev_fi = fi; 1867 dead = 0; 1868 change_nexthops(fi) { 1869 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) 1870 dead++; 1871 else if (nexthop_nh->fib_nh_dev == dev && 1872 nexthop_nh->fib_nh_scope != scope) { 1873 switch (event) { 1874 case NETDEV_DOWN: 1875 case NETDEV_UNREGISTER: 1876 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD; 1877 /* fall through */ 1878 case NETDEV_CHANGE: 1879 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 1880 break; 1881 } 1882 call_fib_nh_notifiers(nexthop_nh, 1883 FIB_EVENT_NH_DEL); 1884 dead++; 1885 } 1886 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1887 if (event == NETDEV_UNREGISTER && 1888 nexthop_nh->fib_nh_dev == dev) { 1889 dead = fi->fib_nhs; 1890 break; 1891 } 1892 #endif 1893 } endfor_nexthops(fi) 1894 if (dead == fi->fib_nhs) { 1895 switch (event) { 1896 case NETDEV_DOWN: 1897 case NETDEV_UNREGISTER: 1898 fi->fib_flags |= RTNH_F_DEAD; 1899 /* fall through */ 1900 case NETDEV_CHANGE: 1901 fi->fib_flags |= RTNH_F_LINKDOWN; 1902 break; 1903 } 1904 ret++; 1905 } 1906 1907 fib_rebalance(fi); 1908 } 1909 1910 return ret; 1911 } 1912 1913 /* Must be invoked inside of an RCU protected region. */ 1914 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res) 1915 { 1916 struct fib_info *fi = NULL, *last_resort = NULL; 1917 struct hlist_head *fa_head = res->fa_head; 1918 struct fib_table *tb = res->table; 1919 u8 slen = 32 - res->prefixlen; 1920 int order = -1, last_idx = -1; 1921 struct fib_alias *fa, *fa1 = NULL; 1922 u32 last_prio = res->fi->fib_priority; 1923 u8 last_tos = 0; 1924 1925 hlist_for_each_entry_rcu(fa, fa_head, fa_list) { 1926 struct fib_info *next_fi = fa->fa_info; 1927 struct fib_nh *nh; 1928 1929 if (fa->fa_slen != slen) 1930 continue; 1931 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) 1932 continue; 1933 if (fa->tb_id != tb->tb_id) 1934 continue; 1935 if (next_fi->fib_priority > last_prio && 1936 fa->fa_tos == last_tos) { 1937 if (last_tos) 1938 continue; 1939 break; 1940 } 1941 if (next_fi->fib_flags & RTNH_F_DEAD) 1942 continue; 1943 last_tos = fa->fa_tos; 1944 last_prio = next_fi->fib_priority; 1945 1946 if (next_fi->fib_scope != res->scope || 1947 fa->fa_type != RTN_UNICAST) 1948 continue; 1949 1950 nh = fib_info_nh(next_fi, 0); 1951 if (!nh->fib_nh_gw4 || nh->fib_nh_scope != RT_SCOPE_LINK) 1952 continue; 1953 1954 fib_alias_accessed(fa); 1955 1956 if (!fi) { 1957 if (next_fi != res->fi) 1958 break; 1959 fa1 = fa; 1960 } else if (!fib_detect_death(fi, order, &last_resort, 1961 &last_idx, fa1->fa_default)) { 1962 fib_result_assign(res, fi); 1963 fa1->fa_default = order; 1964 goto out; 1965 } 1966 fi = next_fi; 1967 order++; 1968 } 1969 1970 if (order <= 0 || !fi) { 1971 if (fa1) 1972 fa1->fa_default = -1; 1973 goto out; 1974 } 1975 1976 if (!fib_detect_death(fi, order, &last_resort, &last_idx, 1977 fa1->fa_default)) { 1978 fib_result_assign(res, fi); 1979 fa1->fa_default = order; 1980 goto out; 1981 } 1982 1983 if (last_idx >= 0) 1984 fib_result_assign(res, last_resort); 1985 fa1->fa_default = last_idx; 1986 out: 1987 return; 1988 } 1989 1990 /* 1991 * Dead device goes up. We wake up dead nexthops. 1992 * It takes sense only on multipath routes. 1993 * 1994 * only used when fib_nh is built into fib_info 1995 */ 1996 int fib_sync_up(struct net_device *dev, unsigned char nh_flags) 1997 { 1998 struct fib_info *prev_fi; 1999 unsigned int hash; 2000 struct hlist_head *head; 2001 struct fib_nh *nh; 2002 int ret; 2003 2004 if (!(dev->flags & IFF_UP)) 2005 return 0; 2006 2007 if (nh_flags & RTNH_F_DEAD) { 2008 unsigned int flags = dev_get_flags(dev); 2009 2010 if (flags & (IFF_RUNNING | IFF_LOWER_UP)) 2011 nh_flags |= RTNH_F_LINKDOWN; 2012 } 2013 2014 prev_fi = NULL; 2015 hash = fib_devindex_hashfn(dev->ifindex); 2016 head = &fib_info_devhash[hash]; 2017 ret = 0; 2018 2019 hlist_for_each_entry(nh, head, nh_hash) { 2020 struct fib_info *fi = nh->nh_parent; 2021 int alive; 2022 2023 BUG_ON(!fi->fib_nhs); 2024 if (nh->fib_nh_dev != dev || fi == prev_fi) 2025 continue; 2026 2027 prev_fi = fi; 2028 alive = 0; 2029 change_nexthops(fi) { 2030 if (!(nexthop_nh->fib_nh_flags & nh_flags)) { 2031 alive++; 2032 continue; 2033 } 2034 if (!nexthop_nh->fib_nh_dev || 2035 !(nexthop_nh->fib_nh_dev->flags & IFF_UP)) 2036 continue; 2037 if (nexthop_nh->fib_nh_dev != dev || 2038 !__in_dev_get_rtnl(dev)) 2039 continue; 2040 alive++; 2041 nexthop_nh->fib_nh_flags &= ~nh_flags; 2042 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD); 2043 } endfor_nexthops(fi) 2044 2045 if (alive > 0) { 2046 fi->fib_flags &= ~nh_flags; 2047 ret++; 2048 } 2049 2050 fib_rebalance(fi); 2051 } 2052 2053 return ret; 2054 } 2055 2056 #ifdef CONFIG_IP_ROUTE_MULTIPATH 2057 static bool fib_good_nh(const struct fib_nh *nh) 2058 { 2059 int state = NUD_REACHABLE; 2060 2061 if (nh->fib_nh_scope == RT_SCOPE_LINK) { 2062 struct neighbour *n; 2063 2064 rcu_read_lock_bh(); 2065 2066 if (likely(nh->fib_nh_gw_family == AF_INET)) 2067 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, 2068 (__force u32)nh->fib_nh_gw4); 2069 else if (nh->fib_nh_gw_family == AF_INET6) 2070 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, 2071 &nh->fib_nh_gw6); 2072 else 2073 n = NULL; 2074 if (n) 2075 state = n->nud_state; 2076 2077 rcu_read_unlock_bh(); 2078 } 2079 2080 return !!(state & NUD_VALID); 2081 } 2082 2083 void fib_select_multipath(struct fib_result *res, int hash) 2084 { 2085 struct fib_info *fi = res->fi; 2086 struct net *net = fi->fib_net; 2087 bool first = false; 2088 2089 if (unlikely(res->fi->nh)) { 2090 nexthop_path_fib_result(res, hash); 2091 return; 2092 } 2093 2094 change_nexthops(fi) { 2095 if (net->ipv4.sysctl_fib_multipath_use_neigh) { 2096 if (!fib_good_nh(nexthop_nh)) 2097 continue; 2098 if (!first) { 2099 res->nh_sel = nhsel; 2100 res->nhc = &nexthop_nh->nh_common; 2101 first = true; 2102 } 2103 } 2104 2105 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound)) 2106 continue; 2107 2108 res->nh_sel = nhsel; 2109 res->nhc = &nexthop_nh->nh_common; 2110 return; 2111 } endfor_nexthops(fi); 2112 } 2113 #endif 2114 2115 void fib_select_path(struct net *net, struct fib_result *res, 2116 struct flowi4 *fl4, const struct sk_buff *skb) 2117 { 2118 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) 2119 goto check_saddr; 2120 2121 #ifdef CONFIG_IP_ROUTE_MULTIPATH 2122 if (fib_info_num_path(res->fi) > 1) { 2123 int h = fib_multipath_hash(net, fl4, skb, NULL); 2124 2125 fib_select_multipath(res, h); 2126 } 2127 else 2128 #endif 2129 if (!res->prefixlen && 2130 res->table->tb_num_default > 1 && 2131 res->type == RTN_UNICAST) 2132 fib_select_default(fl4, res); 2133 2134 check_saddr: 2135 if (!fl4->saddr) 2136 fl4->saddr = fib_result_prefsrc(net, res); 2137 } 2138