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