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