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