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