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