1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux INET6 implementation 4 * FIB front-end. 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 */ 9 10 /* Changes: 11 * 12 * YOSHIFUJI Hideaki @USAGI 13 * reworked default router selection. 14 * - respect outgoing interface 15 * - select from (probably) reachable routers (i.e. 16 * routers in REACHABLE, STALE, DELAY or PROBE states). 17 * - always select the same router if it is (probably) 18 * reachable. otherwise, round-robin the list. 19 * Ville Nuorvala 20 * Fixed routing subtrees. 21 */ 22 23 #define pr_fmt(fmt) "IPv6: " fmt 24 25 #include <linux/capability.h> 26 #include <linux/errno.h> 27 #include <linux/export.h> 28 #include <linux/types.h> 29 #include <linux/times.h> 30 #include <linux/socket.h> 31 #include <linux/sockios.h> 32 #include <linux/net.h> 33 #include <linux/route.h> 34 #include <linux/netdevice.h> 35 #include <linux/in6.h> 36 #include <linux/mroute6.h> 37 #include <linux/init.h> 38 #include <linux/if_arp.h> 39 #include <linux/proc_fs.h> 40 #include <linux/seq_file.h> 41 #include <linux/nsproxy.h> 42 #include <linux/slab.h> 43 #include <linux/jhash.h> 44 #include <linux/siphash.h> 45 #include <net/net_namespace.h> 46 #include <net/snmp.h> 47 #include <net/ipv6.h> 48 #include <net/ip6_fib.h> 49 #include <net/ip6_route.h> 50 #include <net/ndisc.h> 51 #include <net/addrconf.h> 52 #include <net/tcp.h> 53 #include <linux/rtnetlink.h> 54 #include <net/dst.h> 55 #include <net/dst_metadata.h> 56 #include <net/xfrm.h> 57 #include <net/netevent.h> 58 #include <net/netlink.h> 59 #include <net/rtnh.h> 60 #include <net/lwtunnel.h> 61 #include <net/ip_tunnels.h> 62 #include <net/l3mdev.h> 63 #include <net/ip.h> 64 #include <linux/uaccess.h> 65 #include <linux/btf_ids.h> 66 67 #ifdef CONFIG_SYSCTL 68 #include <linux/sysctl.h> 69 #endif 70 71 static int ip6_rt_type_to_error(u8 fib6_type); 72 73 #define CREATE_TRACE_POINTS 74 #include <trace/events/fib6.h> 75 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); 76 #undef CREATE_TRACE_POINTS 77 78 enum rt6_nud_state { 79 RT6_NUD_FAIL_HARD = -3, 80 RT6_NUD_FAIL_PROBE = -2, 81 RT6_NUD_FAIL_DO_RR = -1, 82 RT6_NUD_SUCCEED = 1 83 }; 84 85 INDIRECT_CALLABLE_SCOPE 86 struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 87 static unsigned int ip6_default_advmss(const struct dst_entry *dst); 88 INDIRECT_CALLABLE_SCOPE 89 unsigned int ip6_mtu(const struct dst_entry *dst); 90 static void ip6_negative_advice(struct sock *sk, 91 struct dst_entry *dst); 92 static void ip6_dst_destroy(struct dst_entry *); 93 static void ip6_dst_ifdown(struct dst_entry *, 94 struct net_device *dev); 95 static void ip6_dst_gc(struct dst_ops *ops); 96 97 static int ip6_pkt_discard(struct sk_buff *skb); 98 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 99 static int ip6_pkt_prohibit(struct sk_buff *skb); 100 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 101 static void ip6_link_failure(struct sk_buff *skb); 102 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 103 struct sk_buff *skb, u32 mtu, 104 bool confirm_neigh); 105 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 106 struct sk_buff *skb); 107 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 108 int strict); 109 static size_t rt6_nlmsg_size(struct fib6_info *f6i); 110 static int rt6_fill_node(struct net *net, struct sk_buff *skb, 111 struct fib6_info *rt, struct dst_entry *dst, 112 struct in6_addr *dest, struct in6_addr *src, 113 int iif, int type, u32 portid, u32 seq, 114 unsigned int flags); 115 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 116 const struct in6_addr *daddr, 117 const struct in6_addr *saddr); 118 119 #ifdef CONFIG_IPV6_ROUTE_INFO 120 static struct fib6_info *rt6_add_route_info(struct net *net, 121 const struct in6_addr *prefix, int prefixlen, 122 const struct in6_addr *gwaddr, 123 struct net_device *dev, 124 unsigned int pref); 125 static struct fib6_info *rt6_get_route_info(struct net *net, 126 const struct in6_addr *prefix, int prefixlen, 127 const struct in6_addr *gwaddr, 128 struct net_device *dev); 129 #endif 130 131 struct uncached_list { 132 spinlock_t lock; 133 struct list_head head; 134 }; 135 136 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 137 138 void rt6_uncached_list_add(struct rt6_info *rt) 139 { 140 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 141 142 rt->dst.rt_uncached_list = ul; 143 144 spin_lock_bh(&ul->lock); 145 list_add_tail(&rt->dst.rt_uncached, &ul->head); 146 spin_unlock_bh(&ul->lock); 147 } 148 149 void rt6_uncached_list_del(struct rt6_info *rt) 150 { 151 struct uncached_list *ul = rt->dst.rt_uncached_list; 152 153 if (ul) { 154 spin_lock_bh(&ul->lock); 155 list_del_init(&rt->dst.rt_uncached); 156 spin_unlock_bh(&ul->lock); 157 } 158 } 159 160 static void rt6_uncached_list_flush_dev(struct net_device *dev) 161 { 162 int cpu; 163 164 for_each_possible_cpu(cpu) { 165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 166 struct rt6_info *rt, *safe; 167 168 if (list_empty(&ul->head)) 169 continue; 170 171 spin_lock_bh(&ul->lock); 172 list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) { 173 struct inet6_dev *rt_idev = rt->rt6i_idev; 174 struct net_device *rt_dev = rt->dst.dev; 175 bool handled = false; 176 177 if (rt_idev && rt_idev->dev == dev) { 178 rt->rt6i_idev = in6_dev_get(blackhole_netdev); 179 in6_dev_put(rt_idev); 180 handled = true; 181 } 182 183 if (rt_dev == dev) { 184 rt->dst.dev = blackhole_netdev; 185 netdev_ref_replace(rt_dev, blackhole_netdev, 186 &rt->dst.dev_tracker, 187 GFP_ATOMIC); 188 handled = true; 189 } 190 if (handled) 191 list_del_init(&rt->dst.rt_uncached); 192 } 193 spin_unlock_bh(&ul->lock); 194 } 195 } 196 197 static inline const void *choose_neigh_daddr(const struct in6_addr *p, 198 struct sk_buff *skb, 199 const void *daddr) 200 { 201 if (!ipv6_addr_any(p)) 202 return (const void *) p; 203 else if (skb) 204 return &ipv6_hdr(skb)->daddr; 205 return daddr; 206 } 207 208 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 209 struct net_device *dev, 210 struct sk_buff *skb, 211 const void *daddr) 212 { 213 struct neighbour *n; 214 215 daddr = choose_neigh_daddr(gw, skb, daddr); 216 n = __ipv6_neigh_lookup(dev, daddr); 217 if (n) 218 return n; 219 220 n = neigh_create(&nd_tbl, daddr, dev); 221 return IS_ERR(n) ? NULL : n; 222 } 223 224 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 225 struct sk_buff *skb, 226 const void *daddr) 227 { 228 const struct rt6_info *rt = dst_rt6_info(dst); 229 230 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), 231 dst_dev(dst), skb, daddr); 232 } 233 234 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 235 { 236 const struct rt6_info *rt = dst_rt6_info(dst); 237 struct net_device *dev = dst_dev(dst); 238 239 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr); 240 if (!daddr) 241 return; 242 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 243 return; 244 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 245 return; 246 __ipv6_confirm_neigh(dev, daddr); 247 } 248 249 static struct dst_ops ip6_dst_ops_template = { 250 .family = AF_INET6, 251 .gc = ip6_dst_gc, 252 .gc_thresh = 1024, 253 .check = ip6_dst_check, 254 .default_advmss = ip6_default_advmss, 255 .mtu = ip6_mtu, 256 .cow_metrics = dst_cow_metrics_generic, 257 .destroy = ip6_dst_destroy, 258 .ifdown = ip6_dst_ifdown, 259 .negative_advice = ip6_negative_advice, 260 .link_failure = ip6_link_failure, 261 .update_pmtu = ip6_rt_update_pmtu, 262 .redirect = rt6_do_redirect, 263 .local_out = __ip6_local_out, 264 .neigh_lookup = ip6_dst_neigh_lookup, 265 .confirm_neigh = ip6_confirm_neigh, 266 }; 267 268 static struct dst_ops ip6_dst_blackhole_ops = { 269 .family = AF_INET6, 270 .default_advmss = ip6_default_advmss, 271 .neigh_lookup = ip6_dst_neigh_lookup, 272 .check = ip6_dst_check, 273 .destroy = ip6_dst_destroy, 274 .cow_metrics = dst_cow_metrics_generic, 275 .update_pmtu = dst_blackhole_update_pmtu, 276 .redirect = dst_blackhole_redirect, 277 .mtu = dst_blackhole_mtu, 278 }; 279 280 static const u32 ip6_template_metrics[RTAX_MAX] = { 281 [RTAX_HOPLIMIT - 1] = 0, 282 }; 283 284 static const struct fib6_info fib6_null_entry_template = { 285 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 286 .fib6_protocol = RTPROT_KERNEL, 287 .fib6_metric = ~(u32)0, 288 .fib6_ref = REFCOUNT_INIT(1), 289 .fib6_type = RTN_UNREACHABLE, 290 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 291 }; 292 293 static const struct rt6_info ip6_null_entry_template = { 294 .dst = { 295 .__rcuref = RCUREF_INIT(1), 296 .__use = 1, 297 .obsolete = DST_OBSOLETE_FORCE_CHK, 298 .error = -ENETUNREACH, 299 .input = ip6_pkt_discard, 300 .output = ip6_pkt_discard_out, 301 }, 302 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 303 }; 304 305 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 306 307 static const struct rt6_info ip6_prohibit_entry_template = { 308 .dst = { 309 .__rcuref = RCUREF_INIT(1), 310 .__use = 1, 311 .obsolete = DST_OBSOLETE_FORCE_CHK, 312 .error = -EACCES, 313 .input = ip6_pkt_prohibit, 314 .output = ip6_pkt_prohibit_out, 315 }, 316 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 317 }; 318 319 static const struct rt6_info ip6_blk_hole_entry_template = { 320 .dst = { 321 .__rcuref = RCUREF_INIT(1), 322 .__use = 1, 323 .obsolete = DST_OBSOLETE_FORCE_CHK, 324 .error = -EINVAL, 325 .input = dst_discard, 326 .output = dst_discard_out, 327 }, 328 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 329 }; 330 331 #endif 332 333 static void rt6_info_init(struct rt6_info *rt) 334 { 335 memset_after(rt, 0, dst); 336 } 337 338 /* allocate dst with ip6_dst_ops */ 339 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 340 int flags) 341 { 342 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 343 DST_OBSOLETE_FORCE_CHK, flags); 344 345 if (rt) { 346 rt6_info_init(rt); 347 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 348 } 349 350 return rt; 351 } 352 EXPORT_SYMBOL(ip6_dst_alloc); 353 354 static void ip6_dst_destroy(struct dst_entry *dst) 355 { 356 struct rt6_info *rt = dst_rt6_info(dst); 357 struct fib6_info *from; 358 struct inet6_dev *idev; 359 360 ip_dst_metrics_put(dst); 361 rt6_uncached_list_del(rt); 362 363 idev = rt->rt6i_idev; 364 if (idev) { 365 rt->rt6i_idev = NULL; 366 in6_dev_put(idev); 367 } 368 369 from = unrcu_pointer(xchg(&rt->from, NULL)); 370 fib6_info_release(from); 371 } 372 373 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev) 374 { 375 struct rt6_info *rt = dst_rt6_info(dst); 376 struct inet6_dev *idev = rt->rt6i_idev; 377 struct fib6_info *from; 378 379 if (idev && idev->dev != blackhole_netdev) { 380 struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev); 381 382 if (blackhole_idev) { 383 rt->rt6i_idev = blackhole_idev; 384 in6_dev_put(idev); 385 } 386 } 387 from = unrcu_pointer(xchg(&rt->from, NULL)); 388 fib6_info_release(from); 389 } 390 391 static bool __rt6_check_expired(const struct rt6_info *rt) 392 { 393 if (rt->rt6i_flags & RTF_EXPIRES) 394 return time_after(jiffies, READ_ONCE(rt->dst.expires)); 395 return false; 396 } 397 398 static bool rt6_check_expired(const struct rt6_info *rt) 399 { 400 struct fib6_info *from; 401 402 from = rcu_dereference(rt->from); 403 404 if (rt->rt6i_flags & RTF_EXPIRES) { 405 if (time_after(jiffies, READ_ONCE(rt->dst.expires))) 406 return true; 407 } else if (from) { 408 return READ_ONCE(rt->dst.obsolete) != DST_OBSOLETE_FORCE_CHK || 409 fib6_check_expired(from); 410 } 411 return false; 412 } 413 414 static struct fib6_info * 415 rt6_multipath_first_sibling_rcu(const struct fib6_info *rt) 416 { 417 struct fib6_info *iter; 418 struct fib6_node *fn; 419 420 fn = rcu_dereference(rt->fib6_node); 421 if (!fn) 422 goto out; 423 iter = rcu_dereference(fn->leaf); 424 if (!iter) 425 goto out; 426 427 while (iter) { 428 if (iter->fib6_metric == rt->fib6_metric && 429 rt6_qualify_for_ecmp(iter)) 430 return iter; 431 iter = rcu_dereference(iter->fib6_next); 432 } 433 434 out: 435 return NULL; 436 } 437 438 void fib6_select_path(const struct net *net, struct fib6_result *res, 439 struct flowi6 *fl6, int oif, bool have_oif_match, 440 const struct sk_buff *skb, int strict) 441 { 442 struct fib6_info *first, *match = res->f6i; 443 struct fib6_info *sibling; 444 int hash; 445 446 if (!match->nh && (!match->fib6_nsiblings || have_oif_match)) 447 goto out; 448 449 if (match->nh && have_oif_match && res->nh) 450 return; 451 452 if (skb) 453 IP6CB(skb)->flags |= IP6SKB_MULTIPATH; 454 455 /* We might have already computed the hash for ICMPv6 errors. In such 456 * case it will always be non-zero. Otherwise now is the time to do it. 457 */ 458 if (!fl6->mp_hash && 459 (!match->nh || nexthop_is_multipath(match->nh))) 460 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 461 462 if (unlikely(match->nh)) { 463 nexthop_path_fib6_result(res, fl6->mp_hash); 464 return; 465 } 466 467 first = rt6_multipath_first_sibling_rcu(match); 468 if (!first) 469 goto out; 470 471 hash = fl6->mp_hash; 472 if (hash <= atomic_read(&first->fib6_nh->fib_nh_upper_bound)) { 473 if (rt6_score_route(first->fib6_nh, first->fib6_flags, oif, 474 strict) >= 0) 475 match = first; 476 goto out; 477 } 478 479 list_for_each_entry_rcu(sibling, &first->fib6_siblings, 480 fib6_siblings) { 481 const struct fib6_nh *nh = sibling->fib6_nh; 482 int nh_upper_bound; 483 484 if (!READ_ONCE(first->fib6_nsiblings)) 485 break; 486 487 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 488 if (hash > nh_upper_bound) 489 continue; 490 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 491 break; 492 match = sibling; 493 break; 494 } 495 496 out: 497 res->f6i = match; 498 res->nh = match->fib6_nh; 499 } 500 501 /* 502 * Route lookup. rcu_read_lock() should be held. 503 */ 504 505 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 506 const struct in6_addr *saddr, int oif, int flags) 507 { 508 const struct net_device *dev; 509 510 if (nh->fib_nh_flags & RTNH_F_DEAD) 511 return false; 512 513 dev = nh->fib_nh_dev; 514 if (oif) { 515 if (dev->ifindex == oif) 516 return true; 517 } else { 518 if (ipv6_chk_addr(net, saddr, dev, 519 flags & RT6_LOOKUP_F_IFACE)) 520 return true; 521 } 522 523 return false; 524 } 525 526 struct fib6_nh_dm_arg { 527 struct net *net; 528 const struct in6_addr *saddr; 529 int oif; 530 int flags; 531 struct fib6_nh *nh; 532 }; 533 534 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) 535 { 536 struct fib6_nh_dm_arg *arg = _arg; 537 538 arg->nh = nh; 539 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, 540 arg->flags); 541 } 542 543 /* returns fib6_nh from nexthop or NULL */ 544 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, 545 struct fib6_result *res, 546 const struct in6_addr *saddr, 547 int oif, int flags) 548 { 549 struct fib6_nh_dm_arg arg = { 550 .net = net, 551 .saddr = saddr, 552 .oif = oif, 553 .flags = flags, 554 }; 555 556 if (nexthop_is_blackhole(nh)) 557 return NULL; 558 559 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) 560 return arg.nh; 561 562 return NULL; 563 } 564 565 static void rt6_device_match(struct net *net, struct fib6_result *res, 566 const struct in6_addr *saddr, int oif, int flags) 567 { 568 struct fib6_info *f6i = res->f6i; 569 struct fib6_info *spf6i; 570 struct fib6_nh *nh; 571 572 if (!oif && ipv6_addr_any(saddr)) { 573 if (unlikely(f6i->nh)) { 574 nh = nexthop_fib6_nh(f6i->nh); 575 if (nexthop_is_blackhole(f6i->nh)) 576 goto out_blackhole; 577 } else { 578 nh = f6i->fib6_nh; 579 } 580 if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 581 goto out; 582 } 583 584 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 585 bool matched = false; 586 587 if (unlikely(spf6i->nh)) { 588 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, 589 oif, flags); 590 if (nh) 591 matched = true; 592 } else { 593 nh = spf6i->fib6_nh; 594 if (__rt6_device_match(net, nh, saddr, oif, flags)) 595 matched = true; 596 } 597 if (matched) { 598 res->f6i = spf6i; 599 goto out; 600 } 601 } 602 603 if (oif && flags & RT6_LOOKUP_F_IFACE) { 604 res->f6i = net->ipv6.fib6_null_entry; 605 nh = res->f6i->fib6_nh; 606 goto out; 607 } 608 609 if (unlikely(f6i->nh)) { 610 nh = nexthop_fib6_nh(f6i->nh); 611 if (nexthop_is_blackhole(f6i->nh)) 612 goto out_blackhole; 613 } else { 614 nh = f6i->fib6_nh; 615 } 616 617 if (nh->fib_nh_flags & RTNH_F_DEAD) { 618 res->f6i = net->ipv6.fib6_null_entry; 619 nh = res->f6i->fib6_nh; 620 } 621 out: 622 res->nh = nh; 623 res->fib6_type = res->f6i->fib6_type; 624 res->fib6_flags = res->f6i->fib6_flags; 625 return; 626 627 out_blackhole: 628 res->fib6_flags |= RTF_REJECT; 629 res->fib6_type = RTN_BLACKHOLE; 630 res->nh = nh; 631 } 632 633 #ifdef CONFIG_IPV6_ROUTER_PREF 634 struct __rt6_probe_work { 635 struct work_struct work; 636 struct in6_addr target; 637 struct net_device *dev; 638 netdevice_tracker dev_tracker; 639 }; 640 641 static void rt6_probe_deferred(struct work_struct *w) 642 { 643 struct in6_addr mcaddr; 644 struct __rt6_probe_work *work = 645 container_of(w, struct __rt6_probe_work, work); 646 647 addrconf_addr_solict_mult(&work->target, &mcaddr); 648 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 649 netdev_put(work->dev, &work->dev_tracker); 650 kfree(work); 651 } 652 653 static void rt6_probe(struct fib6_nh *fib6_nh) 654 { 655 struct __rt6_probe_work *work = NULL; 656 const struct in6_addr *nh_gw; 657 unsigned long last_probe; 658 struct neighbour *neigh; 659 struct net_device *dev; 660 struct inet6_dev *idev; 661 662 /* 663 * Okay, this does not seem to be appropriate 664 * for now, however, we need to check if it 665 * is really so; aka Router Reachability Probing. 666 * 667 * Router Reachability Probe MUST be rate-limited 668 * to no more than one per minute. 669 */ 670 if (!fib6_nh->fib_nh_gw_family) 671 return; 672 673 nh_gw = &fib6_nh->fib_nh_gw6; 674 dev = fib6_nh->fib_nh_dev; 675 rcu_read_lock(); 676 last_probe = READ_ONCE(fib6_nh->last_probe); 677 idev = __in6_dev_get(dev); 678 if (!idev) 679 goto out; 680 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 681 if (neigh) { 682 if (READ_ONCE(neigh->nud_state) & NUD_VALID) 683 goto out; 684 685 write_lock_bh(&neigh->lock); 686 if (!(neigh->nud_state & NUD_VALID) && 687 time_after(jiffies, 688 neigh->updated + 689 READ_ONCE(idev->cnf.rtr_probe_interval))) { 690 work = kmalloc_obj(*work, GFP_ATOMIC); 691 if (work) 692 __neigh_set_probe_once(neigh); 693 } 694 write_unlock_bh(&neigh->lock); 695 } else if (time_after(jiffies, last_probe + 696 READ_ONCE(idev->cnf.rtr_probe_interval))) { 697 work = kmalloc_obj(*work, GFP_ATOMIC); 698 } 699 700 if (!work || cmpxchg(&fib6_nh->last_probe, 701 last_probe, jiffies) != last_probe) { 702 kfree(work); 703 } else { 704 INIT_WORK(&work->work, rt6_probe_deferred); 705 work->target = *nh_gw; 706 netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC); 707 work->dev = dev; 708 schedule_work(&work->work); 709 } 710 711 out: 712 rcu_read_unlock(); 713 } 714 #else 715 static inline void rt6_probe(struct fib6_nh *fib6_nh) 716 { 717 } 718 #endif 719 720 /* 721 * Default Router Selection (RFC 2461 6.3.6) 722 */ 723 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 724 { 725 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 726 struct neighbour *neigh; 727 728 rcu_read_lock(); 729 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 730 &fib6_nh->fib_nh_gw6); 731 if (neigh) { 732 u8 nud_state = READ_ONCE(neigh->nud_state); 733 734 if (nud_state & NUD_VALID) 735 ret = RT6_NUD_SUCCEED; 736 #ifdef CONFIG_IPV6_ROUTER_PREF 737 else if (!(nud_state & NUD_FAILED)) 738 ret = RT6_NUD_SUCCEED; 739 else 740 ret = RT6_NUD_FAIL_PROBE; 741 #endif 742 } else { 743 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 744 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 745 } 746 rcu_read_unlock(); 747 748 return ret; 749 } 750 751 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 752 int strict) 753 { 754 int m = 0; 755 756 if (!oif || nh->fib_nh_dev->ifindex == oif) 757 m = 2; 758 759 if (!m && (strict & RT6_LOOKUP_F_IFACE)) 760 return RT6_NUD_FAIL_HARD; 761 #ifdef CONFIG_IPV6_ROUTER_PREF 762 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 763 #endif 764 if ((strict & RT6_LOOKUP_F_REACHABLE) && 765 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 766 int n = rt6_check_neigh(nh); 767 if (n < 0) 768 return n; 769 } 770 return m; 771 } 772 773 static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 774 int oif, int strict, int *mpri, bool *do_rr) 775 { 776 bool match_do_rr = false; 777 bool rc = false; 778 int m; 779 780 if (nh->fib_nh_flags & RTNH_F_DEAD) 781 goto out; 782 783 if (ip6_ignore_linkdown(nh->fib_nh_dev) && 784 nh->fib_nh_flags & RTNH_F_LINKDOWN && 785 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 786 goto out; 787 788 m = rt6_score_route(nh, fib6_flags, oif, strict); 789 if (m == RT6_NUD_FAIL_DO_RR) { 790 match_do_rr = true; 791 m = 0; /* lowest valid score */ 792 } else if (m == RT6_NUD_FAIL_HARD) { 793 goto out; 794 } 795 796 if (strict & RT6_LOOKUP_F_REACHABLE) 797 rt6_probe(nh); 798 799 /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 800 if (m > *mpri) { 801 *do_rr = match_do_rr; 802 *mpri = m; 803 rc = true; 804 } 805 out: 806 return rc; 807 } 808 809 struct fib6_nh_frl_arg { 810 u32 flags; 811 int oif; 812 int strict; 813 int *mpri; 814 bool *do_rr; 815 struct fib6_nh *nh; 816 }; 817 818 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) 819 { 820 struct fib6_nh_frl_arg *arg = _arg; 821 822 arg->nh = nh; 823 return find_match(nh, arg->flags, arg->oif, arg->strict, 824 arg->mpri, arg->do_rr); 825 } 826 827 static void __find_rr_leaf(struct fib6_info *f6i_start, 828 struct fib6_info *nomatch, u32 metric, 829 struct fib6_result *res, struct fib6_info **cont, 830 int oif, int strict, bool *do_rr, int *mpri) 831 { 832 struct fib6_info *f6i; 833 834 for (f6i = f6i_start; 835 f6i && f6i != nomatch; 836 f6i = rcu_dereference(f6i->fib6_next)) { 837 bool matched = false; 838 struct fib6_nh *nh; 839 840 if (cont && f6i->fib6_metric != metric) { 841 *cont = f6i; 842 return; 843 } 844 845 if (fib6_check_expired(f6i)) 846 continue; 847 848 if (unlikely(f6i->nh)) { 849 struct fib6_nh_frl_arg arg = { 850 .flags = f6i->fib6_flags, 851 .oif = oif, 852 .strict = strict, 853 .mpri = mpri, 854 .do_rr = do_rr 855 }; 856 857 if (nexthop_is_blackhole(f6i->nh)) { 858 res->fib6_flags = RTF_REJECT; 859 res->fib6_type = RTN_BLACKHOLE; 860 res->f6i = f6i; 861 res->nh = nexthop_fib6_nh(f6i->nh); 862 return; 863 } 864 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, 865 &arg)) { 866 matched = true; 867 nh = arg.nh; 868 } 869 } else { 870 nh = f6i->fib6_nh; 871 if (find_match(nh, f6i->fib6_flags, oif, strict, 872 mpri, do_rr)) 873 matched = true; 874 } 875 if (matched) { 876 res->f6i = f6i; 877 res->nh = nh; 878 res->fib6_flags = f6i->fib6_flags; 879 res->fib6_type = f6i->fib6_type; 880 } 881 } 882 } 883 884 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 885 struct fib6_info *rr_head, int oif, int strict, 886 bool *do_rr, struct fib6_result *res) 887 { 888 u32 metric = rr_head->fib6_metric; 889 struct fib6_info *cont = NULL; 890 int mpri = -1; 891 892 __find_rr_leaf(rr_head, NULL, metric, res, &cont, 893 oif, strict, do_rr, &mpri); 894 895 __find_rr_leaf(leaf, rr_head, metric, res, &cont, 896 oif, strict, do_rr, &mpri); 897 898 if (res->f6i || !cont) 899 return; 900 901 __find_rr_leaf(cont, NULL, metric, res, NULL, 902 oif, strict, do_rr, &mpri); 903 } 904 905 static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 906 struct fib6_result *res, int strict) 907 { 908 struct fib6_info *leaf = rcu_dereference(fn->leaf); 909 struct fib6_info *rt0; 910 bool do_rr = false; 911 int key_plen; 912 913 /* make sure this function or its helpers sets f6i */ 914 res->f6i = NULL; 915 916 if (!leaf || leaf == net->ipv6.fib6_null_entry) 917 goto out; 918 919 rt0 = rcu_dereference(fn->rr_ptr); 920 if (!rt0) 921 rt0 = leaf; 922 923 /* Double check to make sure fn is not an intermediate node 924 * and fn->leaf does not points to its child's leaf 925 * (This might happen if all routes under fn are deleted from 926 * the tree and fib6_repair_tree() is called on the node.) 927 */ 928 key_plen = rt0->fib6_dst.plen; 929 #ifdef CONFIG_IPV6_SUBTREES 930 if (rt0->fib6_src.plen) 931 key_plen = rt0->fib6_src.plen; 932 #endif 933 if (fn->fn_bit != key_plen) 934 goto out; 935 936 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 937 if (do_rr) { 938 struct fib6_info *next = rcu_dereference(rt0->fib6_next); 939 940 /* no entries matched; do round-robin */ 941 if (!next || next->fib6_metric != rt0->fib6_metric) 942 next = leaf; 943 944 if (next != rt0) { 945 spin_lock_bh(&leaf->fib6_table->tb6_lock); 946 /* make sure next is not being deleted from the tree */ 947 if (next->fib6_node) 948 rcu_assign_pointer(fn->rr_ptr, next); 949 spin_unlock_bh(&leaf->fib6_table->tb6_lock); 950 } 951 } 952 953 out: 954 if (!res->f6i) { 955 res->f6i = net->ipv6.fib6_null_entry; 956 res->nh = res->f6i->fib6_nh; 957 res->fib6_flags = res->f6i->fib6_flags; 958 res->fib6_type = res->f6i->fib6_type; 959 } 960 } 961 962 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 963 { 964 return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 965 res->nh->fib_nh_gw_family; 966 } 967 968 #ifdef CONFIG_IPV6_ROUTE_INFO 969 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 970 const struct in6_addr *gwaddr) 971 { 972 struct net *net = dev_net(dev); 973 struct route_info *rinfo = (struct route_info *) opt; 974 struct in6_addr prefix_buf, *prefix; 975 struct fib6_table *table; 976 unsigned int pref; 977 unsigned long lifetime; 978 struct fib6_info *rt; 979 980 if (len < sizeof(struct route_info)) { 981 return -EINVAL; 982 } 983 984 /* Sanity check for prefix_len and length */ 985 if (rinfo->length > 3) { 986 return -EINVAL; 987 } else if (rinfo->prefix_len > 128) { 988 return -EINVAL; 989 } else if (rinfo->prefix_len > 64) { 990 if (rinfo->length < 2) { 991 return -EINVAL; 992 } 993 } else if (rinfo->prefix_len > 0) { 994 if (rinfo->length < 1) { 995 return -EINVAL; 996 } 997 } 998 999 pref = rinfo->route_pref; 1000 if (pref == ICMPV6_ROUTER_PREF_INVALID) 1001 return -EINVAL; 1002 1003 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 1004 1005 if (rinfo->length == 3) 1006 prefix = (struct in6_addr *)rinfo->prefix; 1007 else { 1008 /* this function is safe */ 1009 ipv6_addr_prefix(&prefix_buf, 1010 (struct in6_addr *)rinfo->prefix, 1011 rinfo->prefix_len); 1012 prefix = &prefix_buf; 1013 } 1014 1015 if (rinfo->prefix_len == 0) 1016 rt = rt6_get_dflt_router(net, gwaddr, dev); 1017 else 1018 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 1019 gwaddr, dev); 1020 1021 if (rt && !lifetime) { 1022 ip6_del_rt(net, rt, false); 1023 rt = NULL; 1024 } 1025 1026 if (!rt && lifetime) 1027 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 1028 dev, pref); 1029 else if (rt) 1030 rt->fib6_flags = RTF_ROUTEINFO | 1031 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 1032 1033 if (rt) { 1034 table = rt->fib6_table; 1035 spin_lock_bh(&table->tb6_lock); 1036 1037 if (!addrconf_finite_timeout(lifetime)) { 1038 fib6_clean_expires(rt); 1039 fib6_may_remove_gc_list(net, rt); 1040 } else { 1041 fib6_set_expires(rt, jiffies + HZ * lifetime); 1042 fib6_add_gc_list(rt); 1043 } 1044 1045 spin_unlock_bh(&table->tb6_lock); 1046 1047 fib6_info_release(rt); 1048 } 1049 return 0; 1050 } 1051 #endif 1052 1053 /* 1054 * Misc support functions 1055 */ 1056 1057 /* called with rcu_lock held */ 1058 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 1059 { 1060 struct net_device *dev = res->nh->fib_nh_dev; 1061 1062 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 1063 /* for copies of local routes, dst->dev needs to be the 1064 * device if it is a master device, the master device if 1065 * device is enslaved, and the loopback as the default 1066 */ 1067 if (netif_is_l3_slave(dev) && 1068 !rt6_need_strict(&res->f6i->fib6_dst.addr)) 1069 dev = l3mdev_master_dev_rcu(dev) ? : 1070 dev_net(dev)->loopback_dev; 1071 else if (!netif_is_l3_master(dev)) 1072 dev = dev_net(dev)->loopback_dev; 1073 /* last case is netif_is_l3_master(dev) is true in which 1074 * case we want dev returned to be dev 1075 */ 1076 } 1077 1078 return dev; 1079 } 1080 1081 static const int fib6_prop[RTN_MAX + 1] = { 1082 [RTN_UNSPEC] = 0, 1083 [RTN_UNICAST] = 0, 1084 [RTN_LOCAL] = 0, 1085 [RTN_BROADCAST] = 0, 1086 [RTN_ANYCAST] = 0, 1087 [RTN_MULTICAST] = 0, 1088 [RTN_BLACKHOLE] = -EINVAL, 1089 [RTN_UNREACHABLE] = -EHOSTUNREACH, 1090 [RTN_PROHIBIT] = -EACCES, 1091 [RTN_THROW] = -EAGAIN, 1092 [RTN_NAT] = -EINVAL, 1093 [RTN_XRESOLVE] = -EINVAL, 1094 }; 1095 1096 static int ip6_rt_type_to_error(u8 fib6_type) 1097 { 1098 return fib6_prop[fib6_type]; 1099 } 1100 1101 static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 1102 { 1103 unsigned short flags = 0; 1104 1105 if (rt->dst_nocount) 1106 flags |= DST_NOCOUNT; 1107 if (rt->dst_nopolicy) 1108 flags |= DST_NOPOLICY; 1109 1110 return flags; 1111 } 1112 1113 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 1114 { 1115 rt->dst.error = ip6_rt_type_to_error(fib6_type); 1116 1117 switch (fib6_type) { 1118 case RTN_BLACKHOLE: 1119 rt->dst.output = dst_discard_out; 1120 rt->dst.input = dst_discard; 1121 break; 1122 case RTN_PROHIBIT: 1123 rt->dst.output = ip6_pkt_prohibit_out; 1124 rt->dst.input = ip6_pkt_prohibit; 1125 break; 1126 case RTN_THROW: 1127 case RTN_UNREACHABLE: 1128 default: 1129 rt->dst.output = ip6_pkt_discard_out; 1130 rt->dst.input = ip6_pkt_discard; 1131 break; 1132 } 1133 } 1134 1135 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 1136 { 1137 struct fib6_info *f6i = res->f6i; 1138 1139 if (res->fib6_flags & RTF_REJECT) { 1140 ip6_rt_init_dst_reject(rt, res->fib6_type); 1141 return; 1142 } 1143 1144 rt->dst.error = 0; 1145 rt->dst.output = ip6_output; 1146 1147 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 1148 rt->dst.input = ip6_input; 1149 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 1150 rt->dst.input = ip6_mc_input; 1151 rt->dst.output = ip6_mr_output; 1152 } else { 1153 rt->dst.input = ip6_forward; 1154 } 1155 1156 if (res->nh->fib_nh_lws) { 1157 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 1158 lwtunnel_set_redirect(&rt->dst); 1159 } 1160 1161 rt->dst.lastuse = jiffies; 1162 } 1163 1164 /* Caller must already hold reference to @from */ 1165 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1166 { 1167 rt->rt6i_flags &= ~RTF_EXPIRES; 1168 rcu_assign_pointer(rt->from, from); 1169 ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1170 } 1171 1172 /* Caller must already hold reference to f6i in result */ 1173 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1174 { 1175 const struct fib6_nh *nh = res->nh; 1176 const struct net_device *dev = nh->fib_nh_dev; 1177 struct fib6_info *f6i = res->f6i; 1178 1179 ip6_rt_init_dst(rt, res); 1180 1181 rt->rt6i_dst = f6i->fib6_dst; 1182 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 1183 rt->rt6i_flags = res->fib6_flags; 1184 if (nh->fib_nh_gw_family) { 1185 rt->rt6i_gateway = nh->fib_nh_gw6; 1186 rt->rt6i_flags |= RTF_GATEWAY; 1187 } 1188 rt6_set_from(rt, f6i); 1189 #ifdef CONFIG_IPV6_SUBTREES 1190 rt->rt6i_src = f6i->fib6_src; 1191 #endif 1192 } 1193 1194 static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1195 struct in6_addr *saddr) 1196 { 1197 struct fib6_node *pn, *sn; 1198 while (1) { 1199 if (fn->fn_flags & RTN_TL_ROOT) 1200 return NULL; 1201 pn = rcu_dereference(fn->parent); 1202 sn = FIB6_SUBTREE(pn); 1203 if (sn && sn != fn) 1204 fn = fib6_node_lookup(sn, NULL, saddr); 1205 else 1206 fn = pn; 1207 if (fn->fn_flags & RTN_RTINFO) 1208 return fn; 1209 } 1210 } 1211 1212 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1213 { 1214 struct rt6_info *rt = *prt; 1215 1216 if (dst_hold_safe(&rt->dst)) 1217 return true; 1218 if (net) { 1219 rt = net->ipv6.ip6_null_entry; 1220 dst_hold(&rt->dst); 1221 } else { 1222 rt = NULL; 1223 } 1224 *prt = rt; 1225 return false; 1226 } 1227 1228 /* called with rcu_lock held */ 1229 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1230 { 1231 struct net_device *dev = res->nh->fib_nh_dev; 1232 struct fib6_info *f6i = res->f6i; 1233 unsigned short flags; 1234 struct rt6_info *nrt; 1235 1236 if (!fib6_info_hold_safe(f6i)) 1237 goto fallback; 1238 1239 flags = fib6_info_dst_flags(f6i); 1240 nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 1241 if (!nrt) { 1242 fib6_info_release(f6i); 1243 goto fallback; 1244 } 1245 1246 ip6_rt_copy_init(nrt, res); 1247 return nrt; 1248 1249 fallback: 1250 nrt = dev_net(dev)->ipv6.ip6_null_entry; 1251 dst_hold(&nrt->dst); 1252 return nrt; 1253 } 1254 1255 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net, 1256 struct fib6_table *table, 1257 struct flowi6 *fl6, 1258 const struct sk_buff *skb, 1259 int flags) 1260 { 1261 struct fib6_result res = {}; 1262 struct fib6_node *fn; 1263 struct rt6_info *rt; 1264 1265 rcu_read_lock(); 1266 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1267 restart: 1268 res.f6i = rcu_dereference(fn->leaf); 1269 if (!res.f6i) 1270 res.f6i = net->ipv6.fib6_null_entry; 1271 else 1272 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 1273 flags); 1274 1275 if (res.f6i == net->ipv6.fib6_null_entry) { 1276 fn = fib6_backtrack(fn, &fl6->saddr); 1277 if (fn) 1278 goto restart; 1279 1280 rt = net->ipv6.ip6_null_entry; 1281 dst_hold(&rt->dst); 1282 goto out; 1283 } else if (res.fib6_flags & RTF_REJECT) { 1284 goto do_create; 1285 } 1286 1287 fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1288 fl6->flowi6_oif != 0, skb, flags); 1289 1290 /* Search through exception table */ 1291 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 1292 if (rt) { 1293 if (ip6_hold_safe(net, &rt)) 1294 dst_use_noref(&rt->dst, jiffies); 1295 } else { 1296 do_create: 1297 rt = ip6_create_rt_rcu(&res); 1298 } 1299 1300 out: 1301 trace_fib6_table_lookup(net, &res, table, fl6); 1302 1303 rcu_read_unlock(); 1304 1305 return rt; 1306 } 1307 1308 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1309 const struct sk_buff *skb, int flags) 1310 { 1311 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1312 } 1313 EXPORT_SYMBOL_GPL(ip6_route_lookup); 1314 1315 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1316 const struct in6_addr *saddr, int oif, 1317 const struct sk_buff *skb, int strict) 1318 { 1319 struct flowi6 fl6 = { 1320 .flowi6_oif = oif, 1321 .daddr = *daddr, 1322 }; 1323 struct dst_entry *dst; 1324 int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1325 1326 if (saddr) { 1327 memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1328 flags |= RT6_LOOKUP_F_HAS_SADDR; 1329 } 1330 1331 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1332 if (dst->error == 0) 1333 return dst_rt6_info(dst); 1334 1335 dst_release(dst); 1336 1337 return NULL; 1338 } 1339 EXPORT_SYMBOL(rt6_lookup); 1340 1341 /* ip6_ins_rt is called with FREE table->tb6_lock. 1342 * It takes new route entry, the addition fails by any reason the 1343 * route is released. 1344 * Caller must hold dst before calling it. 1345 */ 1346 1347 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1348 struct netlink_ext_ack *extack) 1349 { 1350 int err; 1351 struct fib6_table *table; 1352 1353 table = rt->fib6_table; 1354 spin_lock_bh(&table->tb6_lock); 1355 err = fib6_add(&table->tb6_root, rt, info, extack); 1356 spin_unlock_bh(&table->tb6_lock); 1357 1358 return err; 1359 } 1360 1361 int ip6_ins_rt(struct net *net, struct fib6_info *rt) 1362 { 1363 struct nl_info info = { .nl_net = net, }; 1364 1365 return __ip6_ins_rt(rt, &info, NULL); 1366 } 1367 1368 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 1369 const struct in6_addr *daddr, 1370 const struct in6_addr *saddr) 1371 { 1372 struct fib6_info *f6i = res->f6i; 1373 struct net_device *dev; 1374 struct rt6_info *rt; 1375 1376 /* 1377 * Clone the route. 1378 */ 1379 1380 if (!fib6_info_hold_safe(f6i)) 1381 return NULL; 1382 1383 dev = ip6_rt_get_dev_rcu(res); 1384 rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1385 if (!rt) { 1386 fib6_info_release(f6i); 1387 return NULL; 1388 } 1389 1390 ip6_rt_copy_init(rt, res); 1391 rt->rt6i_flags |= RTF_CACHE; 1392 rt->rt6i_dst.addr = *daddr; 1393 rt->rt6i_dst.plen = 128; 1394 1395 if (!rt6_is_gw_or_nonexthop(res)) { 1396 if (f6i->fib6_dst.plen != 128 && 1397 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 1398 rt->rt6i_flags |= RTF_ANYCAST; 1399 #ifdef CONFIG_IPV6_SUBTREES 1400 if (rt->rt6i_src.plen && saddr) { 1401 rt->rt6i_src.addr = *saddr; 1402 rt->rt6i_src.plen = 128; 1403 } 1404 #endif 1405 } 1406 1407 return rt; 1408 } 1409 1410 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1411 { 1412 struct fib6_info *f6i = res->f6i; 1413 unsigned short flags = fib6_info_dst_flags(f6i); 1414 struct net_device *dev; 1415 struct rt6_info *pcpu_rt; 1416 1417 if (!fib6_info_hold_safe(f6i)) 1418 return NULL; 1419 1420 rcu_read_lock(); 1421 dev = ip6_rt_get_dev_rcu(res); 1422 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT); 1423 rcu_read_unlock(); 1424 if (!pcpu_rt) { 1425 fib6_info_release(f6i); 1426 return NULL; 1427 } 1428 ip6_rt_copy_init(pcpu_rt, res); 1429 pcpu_rt->rt6i_flags |= RTF_PCPU; 1430 1431 if (f6i->nh) 1432 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev)); 1433 1434 return pcpu_rt; 1435 } 1436 1437 static bool rt6_is_valid(const struct rt6_info *rt6) 1438 { 1439 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev)); 1440 } 1441 1442 /* It should be called with rcu_read_lock() acquired */ 1443 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1444 { 1445 struct rt6_info *pcpu_rt; 1446 1447 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); 1448 1449 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) { 1450 struct rt6_info *prev, **p; 1451 1452 p = this_cpu_ptr(res->nh->rt6i_pcpu); 1453 /* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */ 1454 prev = xchg(p, NULL); 1455 if (prev) { 1456 dst_dev_put(&prev->dst); 1457 dst_release(&prev->dst); 1458 } 1459 1460 pcpu_rt = NULL; 1461 } 1462 1463 return pcpu_rt; 1464 } 1465 1466 static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1467 const struct fib6_result *res) 1468 { 1469 struct rt6_info *pcpu_rt, *prev, **p; 1470 1471 pcpu_rt = ip6_rt_pcpu_alloc(res); 1472 if (!pcpu_rt) 1473 return NULL; 1474 1475 p = this_cpu_ptr(res->nh->rt6i_pcpu); 1476 prev = cmpxchg(p, NULL, pcpu_rt); 1477 if (unlikely(prev)) { 1478 /* 1479 * Another task on this CPU already installed a pcpu_rt. 1480 * This can happen on PREEMPT_RT where preemption is possible. 1481 * Free our allocation and return the existing one. 1482 */ 1483 WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RT)); 1484 1485 dst_dev_put(&pcpu_rt->dst); 1486 dst_release(&pcpu_rt->dst); 1487 return prev; 1488 } 1489 1490 if (res->f6i->fib6_destroying) { 1491 struct fib6_info *from; 1492 1493 from = unrcu_pointer(xchg(&pcpu_rt->from, NULL)); 1494 fib6_info_release(from); 1495 } 1496 1497 return pcpu_rt; 1498 } 1499 1500 /* exception hash table implementation 1501 */ 1502 static DEFINE_SPINLOCK(rt6_exception_lock); 1503 1504 /* Remove rt6_ex from hash table and free the memory 1505 * Caller must hold rt6_exception_lock 1506 */ 1507 static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 1508 struct rt6_exception *rt6_ex) 1509 { 1510 struct net *net; 1511 1512 if (!bucket || !rt6_ex) 1513 return; 1514 1515 net = dev_net(rt6_ex->rt6i->dst.dev); 1516 net->ipv6.rt6_stats->fib_rt_cache--; 1517 1518 /* purge completely the exception to allow releasing the held resources: 1519 * some [sk] cache may keep the dst around for unlimited time 1520 */ 1521 dst_dev_put(&rt6_ex->rt6i->dst); 1522 1523 hlist_del_rcu(&rt6_ex->hlist); 1524 dst_release(&rt6_ex->rt6i->dst); 1525 kfree_rcu(rt6_ex, rcu); 1526 WARN_ON_ONCE(!bucket->depth); 1527 bucket->depth--; 1528 } 1529 1530 /* Remove oldest rt6_ex in bucket and free the memory 1531 * Caller must hold rt6_exception_lock 1532 */ 1533 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 1534 { 1535 struct rt6_exception *rt6_ex, *oldest = NULL; 1536 1537 if (!bucket) 1538 return; 1539 1540 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1541 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 1542 oldest = rt6_ex; 1543 } 1544 rt6_remove_exception(bucket, oldest); 1545 } 1546 1547 static u32 rt6_exception_hash(const struct in6_addr *dst, 1548 const struct in6_addr *src) 1549 { 1550 static siphash_aligned_key_t rt6_exception_key; 1551 struct { 1552 struct in6_addr dst; 1553 struct in6_addr src; 1554 } __aligned(SIPHASH_ALIGNMENT) combined = { 1555 .dst = *dst, 1556 }; 1557 u64 val; 1558 1559 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key)); 1560 1561 #ifdef CONFIG_IPV6_SUBTREES 1562 if (src) 1563 combined.src = *src; 1564 #endif 1565 val = siphash(&combined, sizeof(combined), &rt6_exception_key); 1566 1567 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 1568 } 1569 1570 /* Helper function to find the cached rt in the hash table 1571 * and update bucket pointer to point to the bucket for this 1572 * (daddr, saddr) pair 1573 * Caller must hold rt6_exception_lock 1574 */ 1575 static struct rt6_exception * 1576 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 1577 const struct in6_addr *daddr, 1578 const struct in6_addr *saddr) 1579 { 1580 struct rt6_exception *rt6_ex; 1581 u32 hval; 1582 1583 if (!(*bucket) || !daddr) 1584 return NULL; 1585 1586 hval = rt6_exception_hash(daddr, saddr); 1587 *bucket += hval; 1588 1589 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 1590 struct rt6_info *rt6 = rt6_ex->rt6i; 1591 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 1592 1593 #ifdef CONFIG_IPV6_SUBTREES 1594 if (matched && saddr) 1595 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 1596 #endif 1597 if (matched) 1598 return rt6_ex; 1599 } 1600 return NULL; 1601 } 1602 1603 /* Helper function to find the cached rt in the hash table 1604 * and update bucket pointer to point to the bucket for this 1605 * (daddr, saddr) pair 1606 * Caller must hold rcu_read_lock() 1607 */ 1608 static struct rt6_exception * 1609 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 1610 const struct in6_addr *daddr, 1611 const struct in6_addr *saddr) 1612 { 1613 struct rt6_exception *rt6_ex; 1614 u32 hval; 1615 1616 WARN_ON_ONCE(!rcu_read_lock_held()); 1617 1618 if (!(*bucket) || !daddr) 1619 return NULL; 1620 1621 hval = rt6_exception_hash(daddr, saddr); 1622 *bucket += hval; 1623 1624 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 1625 struct rt6_info *rt6 = rt6_ex->rt6i; 1626 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 1627 1628 #ifdef CONFIG_IPV6_SUBTREES 1629 if (matched && saddr) 1630 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 1631 #endif 1632 if (matched) 1633 return rt6_ex; 1634 } 1635 return NULL; 1636 } 1637 1638 static unsigned int fib6_mtu(const struct fib6_result *res) 1639 { 1640 const struct fib6_nh *nh = res->nh; 1641 unsigned int mtu; 1642 1643 if (res->f6i->fib6_pmtu) { 1644 mtu = res->f6i->fib6_pmtu; 1645 } else { 1646 struct net_device *dev = nh->fib_nh_dev; 1647 struct inet6_dev *idev; 1648 1649 rcu_read_lock(); 1650 idev = __in6_dev_get(dev); 1651 if (!idev) { 1652 rcu_read_unlock(); 1653 return 0; 1654 } 1655 mtu = READ_ONCE(idev->cnf.mtu6); 1656 rcu_read_unlock(); 1657 } 1658 1659 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1660 1661 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1662 } 1663 1664 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL 1665 1666 /* used when the flushed bit is not relevant, only access to the bucket 1667 * (ie., all bucket users except rt6_insert_exception); 1668 * 1669 * called under rcu lock; sometimes called with rt6_exception_lock held 1670 */ 1671 static 1672 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, 1673 spinlock_t *lock) 1674 { 1675 struct rt6_exception_bucket *bucket; 1676 1677 if (lock) 1678 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1679 lockdep_is_held(lock)); 1680 else 1681 bucket = rcu_dereference(nh->rt6i_exception_bucket); 1682 1683 /* remove bucket flushed bit if set */ 1684 if (bucket) { 1685 unsigned long p = (unsigned long)bucket; 1686 1687 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; 1688 bucket = (struct rt6_exception_bucket *)p; 1689 } 1690 1691 return bucket; 1692 } 1693 1694 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) 1695 { 1696 unsigned long p = (unsigned long)bucket; 1697 1698 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); 1699 } 1700 1701 /* called with rt6_exception_lock held */ 1702 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, 1703 spinlock_t *lock) 1704 { 1705 struct rt6_exception_bucket *bucket; 1706 unsigned long p; 1707 1708 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1709 lockdep_is_held(lock)); 1710 1711 p = (unsigned long)bucket; 1712 p |= FIB6_EXCEPTION_BUCKET_FLUSHED; 1713 bucket = (struct rt6_exception_bucket *)p; 1714 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1715 } 1716 1717 static int rt6_insert_exception(struct rt6_info *nrt, 1718 const struct fib6_result *res) 1719 { 1720 struct net *net = dev_net(nrt->dst.dev); 1721 struct rt6_exception_bucket *bucket; 1722 struct fib6_info *f6i = res->f6i; 1723 struct in6_addr *src_key = NULL; 1724 struct rt6_exception *rt6_ex; 1725 struct fib6_nh *nh = res->nh; 1726 int max_depth; 1727 int err = 0; 1728 1729 spin_lock_bh(&rt6_exception_lock); 1730 1731 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1732 lockdep_is_held(&rt6_exception_lock)); 1733 if (!bucket) { 1734 bucket = kzalloc_objs(*bucket, FIB6_EXCEPTION_BUCKET_SIZE, 1735 GFP_ATOMIC); 1736 if (!bucket) { 1737 err = -ENOMEM; 1738 goto out; 1739 } 1740 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1741 } else if (fib6_nh_excptn_bucket_flushed(bucket)) { 1742 err = -EINVAL; 1743 goto out; 1744 } 1745 1746 #ifdef CONFIG_IPV6_SUBTREES 1747 /* fib6_src.plen != 0 indicates f6i is in subtree 1748 * and exception table is indexed by a hash of 1749 * both fib6_dst and fib6_src. 1750 * Otherwise, the exception table is indexed by 1751 * a hash of only fib6_dst. 1752 */ 1753 if (f6i->fib6_src.plen) 1754 src_key = &nrt->rt6i_src.addr; 1755 #endif 1756 /* rt6_mtu_change() might lower mtu on f6i. 1757 * Only insert this exception route if its mtu 1758 * is less than f6i's mtu value. 1759 */ 1760 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1761 err = -EINVAL; 1762 goto out; 1763 } 1764 1765 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 1766 src_key); 1767 if (rt6_ex) 1768 rt6_remove_exception(bucket, rt6_ex); 1769 1770 rt6_ex = kzalloc_obj(*rt6_ex, GFP_ATOMIC); 1771 if (!rt6_ex) { 1772 err = -ENOMEM; 1773 goto out; 1774 } 1775 rt6_ex->rt6i = nrt; 1776 rt6_ex->stamp = jiffies; 1777 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 1778 bucket->depth++; 1779 net->ipv6.rt6_stats->fib_rt_cache++; 1780 1781 /* Randomize max depth to avoid some side channels attacks. */ 1782 max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH); 1783 while (bucket->depth > max_depth) 1784 rt6_exception_remove_oldest(bucket); 1785 1786 out: 1787 spin_unlock_bh(&rt6_exception_lock); 1788 1789 /* Update fn->fn_sernum to invalidate all cached dst */ 1790 if (!err) { 1791 spin_lock_bh(&f6i->fib6_table->tb6_lock); 1792 fib6_update_sernum(net, f6i); 1793 fib6_add_gc_list(f6i); 1794 spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1795 fib6_force_start_gc(net); 1796 } 1797 1798 return err; 1799 } 1800 1801 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) 1802 { 1803 struct rt6_exception_bucket *bucket; 1804 struct rt6_exception *rt6_ex; 1805 struct hlist_node *tmp; 1806 int i; 1807 1808 spin_lock_bh(&rt6_exception_lock); 1809 1810 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1811 if (!bucket) 1812 goto out; 1813 1814 /* Prevent rt6_insert_exception() to recreate the bucket list */ 1815 if (!from) 1816 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); 1817 1818 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1819 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { 1820 if (!from || 1821 rcu_access_pointer(rt6_ex->rt6i->from) == from) 1822 rt6_remove_exception(bucket, rt6_ex); 1823 } 1824 WARN_ON_ONCE(!from && bucket->depth); 1825 bucket++; 1826 } 1827 out: 1828 spin_unlock_bh(&rt6_exception_lock); 1829 } 1830 1831 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) 1832 { 1833 struct fib6_info *f6i = arg; 1834 1835 fib6_nh_flush_exceptions(nh, f6i); 1836 1837 return 0; 1838 } 1839 1840 void rt6_flush_exceptions(struct fib6_info *f6i) 1841 { 1842 if (f6i->nh) { 1843 rcu_read_lock(); 1844 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, f6i); 1845 rcu_read_unlock(); 1846 } else { 1847 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); 1848 } 1849 } 1850 1851 /* Find cached rt in the hash table inside passed in rt 1852 * Caller has to hold rcu_read_lock() 1853 */ 1854 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1855 const struct in6_addr *daddr, 1856 const struct in6_addr *saddr) 1857 { 1858 const struct in6_addr *src_key = NULL; 1859 struct rt6_exception_bucket *bucket; 1860 struct rt6_exception *rt6_ex; 1861 struct rt6_info *ret = NULL; 1862 1863 #ifdef CONFIG_IPV6_SUBTREES 1864 /* fib6i_src.plen != 0 indicates f6i is in subtree 1865 * and exception table is indexed by a hash of 1866 * both fib6_dst and fib6_src. 1867 * However, the src addr used to create the hash 1868 * might not be exactly the passed in saddr which 1869 * is a /128 addr from the flow. 1870 * So we need to use f6i->fib6_src to redo lookup 1871 * if the passed in saddr does not find anything. 1872 * (See the logic in ip6_rt_cache_alloc() on how 1873 * rt->rt6i_src is updated.) 1874 */ 1875 if (res->f6i->fib6_src.plen) 1876 src_key = saddr; 1877 find_ex: 1878 #endif 1879 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); 1880 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 1881 1882 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 1883 ret = rt6_ex->rt6i; 1884 1885 #ifdef CONFIG_IPV6_SUBTREES 1886 /* Use fib6_src as src_key and redo lookup */ 1887 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1888 src_key = &res->f6i->fib6_src.addr; 1889 goto find_ex; 1890 } 1891 #endif 1892 1893 return ret; 1894 } 1895 1896 /* Remove the passed in cached rt from the hash table that contains it */ 1897 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, 1898 const struct rt6_info *rt) 1899 { 1900 const struct in6_addr *src_key = NULL; 1901 struct rt6_exception_bucket *bucket; 1902 struct rt6_exception *rt6_ex; 1903 int err; 1904 1905 if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 1906 return -ENOENT; 1907 1908 spin_lock_bh(&rt6_exception_lock); 1909 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1910 1911 #ifdef CONFIG_IPV6_SUBTREES 1912 /* rt6i_src.plen != 0 indicates 'from' is in subtree 1913 * and exception table is indexed by a hash of 1914 * both rt6i_dst and rt6i_src. 1915 * Otherwise, the exception table is indexed by 1916 * a hash of only rt6i_dst. 1917 */ 1918 if (plen) 1919 src_key = &rt->rt6i_src.addr; 1920 #endif 1921 rt6_ex = __rt6_find_exception_spinlock(&bucket, 1922 &rt->rt6i_dst.addr, 1923 src_key); 1924 if (rt6_ex) { 1925 rt6_remove_exception(bucket, rt6_ex); 1926 err = 0; 1927 } else { 1928 err = -ENOENT; 1929 } 1930 1931 spin_unlock_bh(&rt6_exception_lock); 1932 return err; 1933 } 1934 1935 struct fib6_nh_excptn_arg { 1936 struct rt6_info *rt; 1937 int plen; 1938 }; 1939 1940 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) 1941 { 1942 struct fib6_nh_excptn_arg *arg = _arg; 1943 int err; 1944 1945 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); 1946 if (err == 0) 1947 return 1; 1948 1949 return 0; 1950 } 1951 1952 static int rt6_remove_exception_rt(struct rt6_info *rt) 1953 { 1954 struct fib6_info *from; 1955 1956 from = rcu_dereference(rt->from); 1957 if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1958 return -EINVAL; 1959 1960 if (from->nh) { 1961 struct fib6_nh_excptn_arg arg = { 1962 .rt = rt, 1963 .plen = from->fib6_src.plen 1964 }; 1965 int rc; 1966 1967 /* rc = 1 means an entry was found */ 1968 rc = nexthop_for_each_fib6_nh(from->nh, 1969 rt6_nh_remove_exception_rt, 1970 &arg); 1971 return rc ? 0 : -ENOENT; 1972 } 1973 1974 return fib6_nh_remove_exception(from->fib6_nh, 1975 from->fib6_src.plen, rt); 1976 } 1977 1978 /* Find rt6_ex which contains the passed in rt cache and 1979 * refresh its stamp 1980 */ 1981 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, 1982 const struct rt6_info *rt) 1983 { 1984 const struct in6_addr *src_key = NULL; 1985 struct rt6_exception_bucket *bucket; 1986 struct rt6_exception *rt6_ex; 1987 1988 bucket = fib6_nh_get_excptn_bucket(nh, NULL); 1989 #ifdef CONFIG_IPV6_SUBTREES 1990 /* rt6i_src.plen != 0 indicates 'from' is in subtree 1991 * and exception table is indexed by a hash of 1992 * both rt6i_dst and rt6i_src. 1993 * Otherwise, the exception table is indexed by 1994 * a hash of only rt6i_dst. 1995 */ 1996 if (plen) 1997 src_key = &rt->rt6i_src.addr; 1998 #endif 1999 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); 2000 if (rt6_ex) 2001 rt6_ex->stamp = jiffies; 2002 } 2003 2004 struct fib6_nh_match_arg { 2005 const struct net_device *dev; 2006 const struct in6_addr *gw; 2007 struct fib6_nh *match; 2008 }; 2009 2010 /* determine if fib6_nh has given device and gateway */ 2011 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) 2012 { 2013 struct fib6_nh_match_arg *arg = _arg; 2014 2015 if (arg->dev != nh->fib_nh_dev || 2016 (arg->gw && !nh->fib_nh_gw_family) || 2017 (!arg->gw && nh->fib_nh_gw_family) || 2018 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) 2019 return 0; 2020 2021 arg->match = nh; 2022 2023 /* found a match, break the loop */ 2024 return 1; 2025 } 2026 2027 static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 2028 { 2029 struct fib6_info *from; 2030 struct fib6_nh *fib6_nh; 2031 2032 rcu_read_lock(); 2033 2034 from = rcu_dereference(rt->from); 2035 if (!from || !(rt->rt6i_flags & RTF_CACHE)) 2036 goto unlock; 2037 2038 if (from->nh) { 2039 struct fib6_nh_match_arg arg = { 2040 .dev = rt->dst.dev, 2041 .gw = &rt->rt6i_gateway, 2042 }; 2043 2044 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); 2045 2046 if (!arg.match) 2047 goto unlock; 2048 fib6_nh = arg.match; 2049 } else { 2050 fib6_nh = from->fib6_nh; 2051 } 2052 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); 2053 unlock: 2054 rcu_read_unlock(); 2055 } 2056 2057 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 2058 struct rt6_info *rt, int mtu) 2059 { 2060 u32 dmtu = dst6_mtu(&rt->dst); 2061 2062 /* If the new MTU is lower than the route PMTU, this new MTU will be the 2063 * lowest MTU in the path: always allow updating the route PMTU to 2064 * reflect PMTU decreases. 2065 * 2066 * If the new MTU is higher, and the route PMTU is equal to the local 2067 * MTU, this means the old MTU is the lowest in the path, so allow 2068 * updating it: if other nodes now have lower MTUs, PMTU discovery will 2069 * handle this. 2070 */ 2071 2072 if (dmtu >= mtu) 2073 return true; 2074 2075 if (dmtu == idev->cnf.mtu6) 2076 return true; 2077 2078 return false; 2079 } 2080 2081 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 2082 const struct fib6_nh *nh, int mtu) 2083 { 2084 struct rt6_exception_bucket *bucket; 2085 struct rt6_exception *rt6_ex; 2086 int i; 2087 2088 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2089 if (!bucket) 2090 return; 2091 2092 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2093 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 2094 struct rt6_info *entry = rt6_ex->rt6i; 2095 2096 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 2097 * route), the metrics of its rt->from have already 2098 * been updated. 2099 */ 2100 if (dst_metric_raw(&entry->dst, RTAX_MTU) && 2101 rt6_mtu_change_route_allowed(idev, entry, mtu)) 2102 dst_metric_set(&entry->dst, RTAX_MTU, mtu); 2103 } 2104 bucket++; 2105 } 2106 } 2107 2108 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2109 2110 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, 2111 const struct in6_addr *gateway) 2112 { 2113 struct rt6_exception_bucket *bucket; 2114 struct rt6_exception *rt6_ex; 2115 struct hlist_node *tmp; 2116 int i; 2117 2118 if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2119 return; 2120 2121 spin_lock_bh(&rt6_exception_lock); 2122 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2123 if (bucket) { 2124 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2125 hlist_for_each_entry_safe(rt6_ex, tmp, 2126 &bucket->chain, hlist) { 2127 struct rt6_info *entry = rt6_ex->rt6i; 2128 2129 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 2130 RTF_CACHE_GATEWAY && 2131 ipv6_addr_equal(gateway, 2132 &entry->rt6i_gateway)) { 2133 rt6_remove_exception(bucket, rt6_ex); 2134 } 2135 } 2136 bucket++; 2137 } 2138 } 2139 2140 spin_unlock_bh(&rt6_exception_lock); 2141 } 2142 2143 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 2144 struct rt6_exception *rt6_ex, 2145 struct fib6_gc_args *gc_args, 2146 unsigned long now) 2147 { 2148 struct rt6_info *rt = rt6_ex->rt6i; 2149 2150 /* we are pruning and obsoleting aged-out and non gateway exceptions 2151 * even if others have still references to them, so that on next 2152 * dst_check() such references can be dropped. 2153 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 2154 * expired, independently from their aging, as per RFC 8201 section 4 2155 */ 2156 if (!(rt->rt6i_flags & RTF_EXPIRES)) { 2157 if (time_after_eq(now, READ_ONCE(rt->dst.lastuse) + 2158 gc_args->timeout)) { 2159 pr_debug("aging clone %p\n", rt); 2160 rt6_remove_exception(bucket, rt6_ex); 2161 return; 2162 } 2163 } else if (time_after(jiffies, READ_ONCE(rt->dst.expires))) { 2164 pr_debug("purging expired route %p\n", rt); 2165 rt6_remove_exception(bucket, rt6_ex); 2166 return; 2167 } 2168 2169 if (rt->rt6i_flags & RTF_GATEWAY) { 2170 struct neighbour *neigh; 2171 2172 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 2173 2174 if (!(neigh && (neigh->flags & NTF_ROUTER))) { 2175 pr_debug("purging route %p via non-router but gateway\n", 2176 rt); 2177 rt6_remove_exception(bucket, rt6_ex); 2178 return; 2179 } 2180 } 2181 2182 gc_args->more++; 2183 } 2184 2185 static void fib6_nh_age_exceptions(const struct fib6_nh *nh, 2186 struct fib6_gc_args *gc_args, 2187 unsigned long now) 2188 { 2189 struct rt6_exception_bucket *bucket; 2190 struct rt6_exception *rt6_ex; 2191 struct hlist_node *tmp; 2192 int i; 2193 2194 if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2195 return; 2196 2197 rcu_read_lock_bh(); 2198 spin_lock(&rt6_exception_lock); 2199 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2200 if (bucket) { 2201 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2202 hlist_for_each_entry_safe(rt6_ex, tmp, 2203 &bucket->chain, hlist) { 2204 rt6_age_examine_exception(bucket, rt6_ex, 2205 gc_args, now); 2206 } 2207 bucket++; 2208 } 2209 } 2210 spin_unlock(&rt6_exception_lock); 2211 rcu_read_unlock_bh(); 2212 } 2213 2214 struct fib6_nh_age_excptn_arg { 2215 struct fib6_gc_args *gc_args; 2216 unsigned long now; 2217 }; 2218 2219 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) 2220 { 2221 struct fib6_nh_age_excptn_arg *arg = _arg; 2222 2223 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); 2224 return 0; 2225 } 2226 2227 void rt6_age_exceptions(struct fib6_info *f6i, 2228 struct fib6_gc_args *gc_args, 2229 unsigned long now) 2230 { 2231 if (f6i->nh) { 2232 struct fib6_nh_age_excptn_arg arg = { 2233 .gc_args = gc_args, 2234 .now = now 2235 }; 2236 2237 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, 2238 &arg); 2239 } else { 2240 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); 2241 } 2242 } 2243 2244 /* must be called with rcu lock held */ 2245 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 2246 struct flowi6 *fl6, struct fib6_result *res, int strict) 2247 { 2248 struct fib6_node *fn, *saved_fn; 2249 2250 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2251 saved_fn = fn; 2252 2253 redo_rt6_select: 2254 rt6_select(net, fn, oif, res, strict); 2255 if (res->f6i == net->ipv6.fib6_null_entry) { 2256 fn = fib6_backtrack(fn, &fl6->saddr); 2257 if (fn) 2258 goto redo_rt6_select; 2259 else if (strict & RT6_LOOKUP_F_REACHABLE) { 2260 /* also consider unreachable route */ 2261 strict &= ~RT6_LOOKUP_F_REACHABLE; 2262 fn = saved_fn; 2263 goto redo_rt6_select; 2264 } 2265 } 2266 2267 trace_fib6_table_lookup(net, res, table, fl6); 2268 2269 return 0; 2270 } 2271 2272 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 2273 int oif, struct flowi6 *fl6, 2274 const struct sk_buff *skb, int flags) 2275 { 2276 struct fib6_result res = {}; 2277 struct rt6_info *rt = NULL; 2278 int strict = 0; 2279 2280 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && 2281 !rcu_read_lock_held()); 2282 2283 strict |= flags & RT6_LOOKUP_F_IFACE; 2284 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 2285 if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0) 2286 strict |= RT6_LOOKUP_F_REACHABLE; 2287 2288 rcu_read_lock(); 2289 2290 fib6_table_lookup(net, table, oif, fl6, &res, strict); 2291 if (res.f6i == net->ipv6.fib6_null_entry) 2292 goto out; 2293 2294 fib6_select_path(net, &res, fl6, oif, false, skb, strict); 2295 2296 /*Search through exception table */ 2297 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 2298 if (rt) { 2299 goto out; 2300 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 2301 !res.nh->fib_nh_gw_family)) { 2302 /* Create a RTF_CACHE clone which will not be 2303 * owned by the fib6 tree. It is for the special case where 2304 * the daddr in the skb during the neighbor look-up is different 2305 * from the fl6->daddr used to look-up route here. 2306 */ 2307 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 2308 2309 if (rt) { 2310 /* 1 refcnt is taken during ip6_rt_cache_alloc(). 2311 * As rt6_uncached_list_add() does not consume refcnt, 2312 * this refcnt is always returned to the caller even 2313 * if caller sets RT6_LOOKUP_F_DST_NOREF flag. 2314 */ 2315 rt6_uncached_list_add(rt); 2316 rcu_read_unlock(); 2317 2318 return rt; 2319 } 2320 } else { 2321 /* Get a percpu copy */ 2322 local_bh_disable(); 2323 rt = rt6_get_pcpu_route(&res); 2324 2325 if (!rt) 2326 rt = rt6_make_pcpu_route(net, &res); 2327 2328 local_bh_enable(); 2329 } 2330 out: 2331 if (!rt) 2332 rt = net->ipv6.ip6_null_entry; 2333 if (!(flags & RT6_LOOKUP_F_DST_NOREF)) 2334 ip6_hold_safe(net, &rt); 2335 rcu_read_unlock(); 2336 2337 return rt; 2338 } 2339 EXPORT_SYMBOL_GPL(ip6_pol_route); 2340 2341 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net, 2342 struct fib6_table *table, 2343 struct flowi6 *fl6, 2344 const struct sk_buff *skb, 2345 int flags) 2346 { 2347 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 2348 } 2349 2350 struct dst_entry *ip6_route_input_lookup(struct net *net, 2351 struct net_device *dev, 2352 struct flowi6 *fl6, 2353 const struct sk_buff *skb, 2354 int flags) 2355 { 2356 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 2357 flags |= RT6_LOOKUP_F_IFACE; 2358 2359 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 2360 } 2361 EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 2362 2363 static void ip6_multipath_l3_keys(const struct sk_buff *skb, 2364 struct flow_keys *keys, 2365 struct flow_keys *flkeys) 2366 { 2367 const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 2368 const struct ipv6hdr *key_iph = outer_iph; 2369 struct flow_keys *_flkeys = flkeys; 2370 const struct ipv6hdr *inner_iph; 2371 const struct icmp6hdr *icmph; 2372 struct ipv6hdr _inner_iph; 2373 struct icmp6hdr _icmph; 2374 2375 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 2376 goto out; 2377 2378 icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2379 sizeof(_icmph), &_icmph); 2380 if (!icmph) 2381 goto out; 2382 2383 if (!icmpv6_is_err(icmph->icmp6_type)) 2384 goto out; 2385 2386 inner_iph = skb_header_pointer(skb, 2387 skb_transport_offset(skb) + sizeof(*icmph), 2388 sizeof(_inner_iph), &_inner_iph); 2389 if (!inner_iph) 2390 goto out; 2391 2392 key_iph = inner_iph; 2393 _flkeys = NULL; 2394 out: 2395 if (_flkeys) { 2396 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 2397 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 2398 keys->tags.flow_label = _flkeys->tags.flow_label; 2399 keys->basic.ip_proto = _flkeys->basic.ip_proto; 2400 } else { 2401 keys->addrs.v6addrs.src = key_iph->saddr; 2402 keys->addrs.v6addrs.dst = key_iph->daddr; 2403 keys->tags.flow_label = ip6_flowlabel(key_iph); 2404 keys->basic.ip_proto = key_iph->nexthdr; 2405 } 2406 } 2407 2408 static u32 rt6_multipath_custom_hash_outer(const struct net *net, 2409 const struct sk_buff *skb, 2410 bool *p_has_inner) 2411 { 2412 u32 hash_fields = ip6_multipath_hash_fields(net); 2413 struct flow_keys keys, hash_keys; 2414 2415 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 2416 return 0; 2417 2418 memset(&hash_keys, 0, sizeof(hash_keys)); 2419 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP); 2420 2421 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2422 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 2423 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 2424 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 2425 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 2426 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 2427 hash_keys.basic.ip_proto = keys.basic.ip_proto; 2428 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL) 2429 hash_keys.tags.flow_label = keys.tags.flow_label; 2430 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 2431 hash_keys.ports.src = keys.ports.src; 2432 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 2433 hash_keys.ports.dst = keys.ports.dst; 2434 2435 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION); 2436 return fib_multipath_hash_from_keys(net, &hash_keys); 2437 } 2438 2439 static u32 rt6_multipath_custom_hash_inner(const struct net *net, 2440 const struct sk_buff *skb, 2441 bool has_inner) 2442 { 2443 u32 hash_fields = ip6_multipath_hash_fields(net); 2444 struct flow_keys keys, hash_keys; 2445 2446 /* We assume the packet carries an encapsulation, but if none was 2447 * encountered during dissection of the outer flow, then there is no 2448 * point in calling the flow dissector again. 2449 */ 2450 if (!has_inner) 2451 return 0; 2452 2453 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK)) 2454 return 0; 2455 2456 memset(&hash_keys, 0, sizeof(hash_keys)); 2457 skb_flow_dissect_flow_keys(skb, &keys, 0); 2458 2459 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION)) 2460 return 0; 2461 2462 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2463 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2464 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 2465 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 2466 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 2467 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 2468 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2469 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2470 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 2471 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 2472 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 2473 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 2474 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL) 2475 hash_keys.tags.flow_label = keys.tags.flow_label; 2476 } 2477 2478 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO) 2479 hash_keys.basic.ip_proto = keys.basic.ip_proto; 2480 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT) 2481 hash_keys.ports.src = keys.ports.src; 2482 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT) 2483 hash_keys.ports.dst = keys.ports.dst; 2484 2485 return fib_multipath_hash_from_keys(net, &hash_keys); 2486 } 2487 2488 static u32 rt6_multipath_custom_hash_skb(const struct net *net, 2489 const struct sk_buff *skb) 2490 { 2491 u32 mhash, mhash_inner; 2492 bool has_inner = true; 2493 2494 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner); 2495 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner); 2496 2497 return jhash_2words(mhash, mhash_inner, 0); 2498 } 2499 2500 static u32 rt6_multipath_custom_hash_fl6(const struct net *net, 2501 const struct flowi6 *fl6) 2502 { 2503 u32 hash_fields = ip6_multipath_hash_fields(net); 2504 struct flow_keys hash_keys; 2505 2506 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 2507 return 0; 2508 2509 memset(&hash_keys, 0, sizeof(hash_keys)); 2510 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2511 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 2512 hash_keys.addrs.v6addrs.src = fl6->saddr; 2513 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 2514 hash_keys.addrs.v6addrs.dst = fl6->daddr; 2515 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 2516 hash_keys.basic.ip_proto = fl6->flowi6_proto; 2517 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL) 2518 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2519 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) { 2520 if (fl6->flowi6_flags & FLOWI_FLAG_ANY_SPORT) 2521 hash_keys.ports.src = (__force __be16)get_random_u16(); 2522 else 2523 hash_keys.ports.src = fl6->fl6_sport; 2524 } 2525 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 2526 hash_keys.ports.dst = fl6->fl6_dport; 2527 2528 return fib_multipath_hash_from_keys(net, &hash_keys); 2529 } 2530 2531 /* if skb is set it will be used and fl6 can be NULL */ 2532 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2533 const struct sk_buff *skb, struct flow_keys *flkeys) 2534 { 2535 struct flow_keys hash_keys; 2536 u32 mhash = 0; 2537 2538 switch (ip6_multipath_hash_policy(net)) { 2539 case 0: 2540 memset(&hash_keys, 0, sizeof(hash_keys)); 2541 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2542 if (skb) { 2543 ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 2544 } else { 2545 hash_keys.addrs.v6addrs.src = fl6->saddr; 2546 hash_keys.addrs.v6addrs.dst = fl6->daddr; 2547 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2548 hash_keys.basic.ip_proto = fl6->flowi6_proto; 2549 } 2550 mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2551 break; 2552 case 1: 2553 if (skb) { 2554 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2555 struct flow_keys keys; 2556 2557 /* short-circuit if we already have L4 hash present */ 2558 if (skb->l4_hash) 2559 return skb_get_hash_raw(skb) >> 1; 2560 2561 memset(&hash_keys, 0, sizeof(hash_keys)); 2562 2563 if (!flkeys) { 2564 skb_flow_dissect_flow_keys(skb, &keys, flag); 2565 flkeys = &keys; 2566 } 2567 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2568 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2569 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2570 hash_keys.ports.src = flkeys->ports.src; 2571 hash_keys.ports.dst = flkeys->ports.dst; 2572 hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2573 } else { 2574 memset(&hash_keys, 0, sizeof(hash_keys)); 2575 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2576 hash_keys.addrs.v6addrs.src = fl6->saddr; 2577 hash_keys.addrs.v6addrs.dst = fl6->daddr; 2578 if (fl6->flowi6_flags & FLOWI_FLAG_ANY_SPORT) 2579 hash_keys.ports.src = (__force __be16)get_random_u16(); 2580 else 2581 hash_keys.ports.src = fl6->fl6_sport; 2582 hash_keys.ports.dst = fl6->fl6_dport; 2583 hash_keys.basic.ip_proto = fl6->flowi6_proto; 2584 } 2585 mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2586 break; 2587 case 2: 2588 memset(&hash_keys, 0, sizeof(hash_keys)); 2589 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2590 if (skb) { 2591 struct flow_keys keys; 2592 2593 if (!flkeys) { 2594 skb_flow_dissect_flow_keys(skb, &keys, 0); 2595 flkeys = &keys; 2596 } 2597 2598 /* Inner can be v4 or v6 */ 2599 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2600 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2601 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2602 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2603 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2604 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2605 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2606 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2607 hash_keys.tags.flow_label = flkeys->tags.flow_label; 2608 hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2609 } else { 2610 /* Same as case 0 */ 2611 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2612 ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 2613 } 2614 } else { 2615 /* Same as case 0 */ 2616 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2617 hash_keys.addrs.v6addrs.src = fl6->saddr; 2618 hash_keys.addrs.v6addrs.dst = fl6->daddr; 2619 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2620 hash_keys.basic.ip_proto = fl6->flowi6_proto; 2621 } 2622 mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2623 break; 2624 case 3: 2625 if (skb) 2626 mhash = rt6_multipath_custom_hash_skb(net, skb); 2627 else 2628 mhash = rt6_multipath_custom_hash_fl6(net, fl6); 2629 break; 2630 } 2631 2632 return mhash >> 1; 2633 } 2634 2635 /* Called with rcu held */ 2636 void ip6_route_input(struct sk_buff *skb) 2637 { 2638 const struct ipv6hdr *iph = ipv6_hdr(skb); 2639 struct net *net = dev_net(skb->dev); 2640 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; 2641 struct ip_tunnel_info *tun_info; 2642 struct flowi6 fl6 = { 2643 .flowi6_iif = skb->dev->ifindex, 2644 .daddr = iph->daddr, 2645 .saddr = iph->saddr, 2646 .flowlabel = ip6_flowinfo(iph), 2647 .flowi6_mark = skb->mark, 2648 .flowi6_proto = iph->nexthdr, 2649 }; 2650 struct flow_keys *flkeys = NULL, _flkeys; 2651 2652 tun_info = skb_tunnel_info(skb); 2653 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2654 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 2655 2656 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 2657 flkeys = &_flkeys; 2658 2659 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2660 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 2661 skb_dst_drop(skb); 2662 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, 2663 &fl6, skb, flags)); 2664 } 2665 EXPORT_SYMBOL_GPL(ip6_route_input); 2666 2667 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net, 2668 struct fib6_table *table, 2669 struct flowi6 *fl6, 2670 const struct sk_buff *skb, 2671 int flags) 2672 { 2673 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2674 } 2675 2676 static struct dst_entry *ip6_route_output_flags_noref(struct net *net, 2677 const struct sock *sk, 2678 struct flowi6 *fl6, 2679 int flags) 2680 { 2681 bool any_src; 2682 2683 if (ipv6_addr_type(&fl6->daddr) & 2684 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 2685 struct dst_entry *dst; 2686 2687 /* This function does not take refcnt on the dst */ 2688 dst = l3mdev_link_scope_lookup(net, fl6); 2689 if (dst) 2690 return dst; 2691 } 2692 2693 fl6->flowi6_iif = LOOPBACK_IFINDEX; 2694 2695 flags |= RT6_LOOKUP_F_DST_NOREF; 2696 any_src = ipv6_addr_any(&fl6->saddr); 2697 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2698 (fl6->flowi6_oif && any_src)) 2699 flags |= RT6_LOOKUP_F_IFACE; 2700 2701 if (!any_src) 2702 flags |= RT6_LOOKUP_F_HAS_SADDR; 2703 else if (sk) 2704 flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs)); 2705 2706 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 2707 } 2708 2709 struct dst_entry *ip6_route_output_flags(struct net *net, 2710 const struct sock *sk, 2711 struct flowi6 *fl6, 2712 int flags) 2713 { 2714 struct dst_entry *dst; 2715 struct rt6_info *rt6; 2716 2717 rcu_read_lock(); 2718 dst = ip6_route_output_flags_noref(net, sk, fl6, flags); 2719 rt6 = dst_rt6_info(dst); 2720 /* For dst cached in uncached_list, refcnt is already taken. */ 2721 if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) { 2722 dst = &net->ipv6.ip6_null_entry->dst; 2723 dst_hold(dst); 2724 } 2725 rcu_read_unlock(); 2726 2727 return dst; 2728 } 2729 EXPORT_SYMBOL_GPL(ip6_route_output_flags); 2730 2731 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 2732 { 2733 struct rt6_info *rt, *ort = dst_rt6_info(dst_orig); 2734 struct net_device *loopback_dev = net->loopback_dev; 2735 struct dst_entry *new = NULL; 2736 2737 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 2738 DST_OBSOLETE_DEAD, 0); 2739 if (rt) { 2740 rt6_info_init(rt); 2741 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 2742 2743 new = &rt->dst; 2744 new->__use = 1; 2745 new->input = dst_discard; 2746 new->output = dst_discard_out; 2747 2748 dst_copy_metrics(new, &ort->dst); 2749 2750 rt->rt6i_idev = in6_dev_get(loopback_dev); 2751 rt->rt6i_gateway = ort->rt6i_gateway; 2752 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 2753 2754 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 2755 #ifdef CONFIG_IPV6_SUBTREES 2756 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 2757 #endif 2758 } 2759 2760 dst_release(dst_orig); 2761 return new ? new : ERR_PTR(-ENOMEM); 2762 } 2763 2764 /* 2765 * Destination cache support functions 2766 */ 2767 2768 static bool fib6_check(struct fib6_info *f6i, u32 cookie) 2769 { 2770 u32 rt_cookie = 0; 2771 2772 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 2773 return false; 2774 2775 if (fib6_check_expired(f6i)) 2776 return false; 2777 2778 return true; 2779 } 2780 2781 static struct dst_entry *rt6_check(struct rt6_info *rt, 2782 struct fib6_info *from, 2783 u32 cookie) 2784 { 2785 u32 rt_cookie = 0; 2786 2787 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || 2788 rt_cookie != cookie) 2789 return NULL; 2790 2791 if (rt6_check_expired(rt)) 2792 return NULL; 2793 2794 return &rt->dst; 2795 } 2796 2797 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2798 struct fib6_info *from, 2799 u32 cookie) 2800 { 2801 if (!__rt6_check_expired(rt) && 2802 READ_ONCE(rt->dst.obsolete) == DST_OBSOLETE_FORCE_CHK && 2803 fib6_check(from, cookie)) 2804 return &rt->dst; 2805 return NULL; 2806 } 2807 2808 INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, 2809 u32 cookie) 2810 { 2811 struct dst_entry *dst_ret; 2812 struct fib6_info *from; 2813 struct rt6_info *rt; 2814 2815 rt = dst_rt6_info(dst); 2816 2817 if (rt->sernum) 2818 return rt6_is_valid(rt) ? dst : NULL; 2819 2820 rcu_read_lock(); 2821 2822 /* All IPV6 dsts are created with ->obsolete set to the value 2823 * DST_OBSOLETE_FORCE_CHK which forces validation calls down 2824 * into this function always. 2825 */ 2826 2827 from = rcu_dereference(rt->from); 2828 2829 if (from && (rt->rt6i_flags & RTF_PCPU || 2830 unlikely(!list_empty(&rt->dst.rt_uncached)))) 2831 dst_ret = rt6_dst_from_check(rt, from, cookie); 2832 else 2833 dst_ret = rt6_check(rt, from, cookie); 2834 2835 rcu_read_unlock(); 2836 2837 return dst_ret; 2838 } 2839 EXPORT_INDIRECT_CALLABLE(ip6_dst_check); 2840 2841 static void ip6_negative_advice(struct sock *sk, 2842 struct dst_entry *dst) 2843 { 2844 struct rt6_info *rt = dst_rt6_info(dst); 2845 2846 if (rt->rt6i_flags & RTF_CACHE) { 2847 rcu_read_lock(); 2848 if (rt6_check_expired(rt)) { 2849 /* rt/dst can not be destroyed yet, 2850 * because of rcu_read_lock() 2851 */ 2852 sk_dst_reset(sk); 2853 rt6_remove_exception_rt(rt); 2854 } 2855 rcu_read_unlock(); 2856 return; 2857 } 2858 sk_dst_reset(sk); 2859 } 2860 2861 static void ip6_link_failure(struct sk_buff *skb) 2862 { 2863 struct rt6_info *rt; 2864 2865 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 2866 2867 rt = dst_rt6_info(skb_dst(skb)); 2868 if (rt) { 2869 rcu_read_lock(); 2870 if (rt->rt6i_flags & RTF_CACHE) { 2871 rt6_remove_exception_rt(rt); 2872 } else { 2873 struct fib6_info *from; 2874 struct fib6_node *fn; 2875 2876 from = rcu_dereference(rt->from); 2877 if (from) { 2878 fn = rcu_dereference(from->fib6_node); 2879 if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2880 WRITE_ONCE(fn->fn_sernum, -1); 2881 } 2882 } 2883 rcu_read_unlock(); 2884 } 2885 } 2886 2887 static void rt6_update_expires(struct rt6_info *rt0, int timeout) 2888 { 2889 if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2890 struct fib6_info *from; 2891 2892 rcu_read_lock(); 2893 from = rcu_dereference(rt0->from); 2894 if (from) 2895 WRITE_ONCE(rt0->dst.expires, from->expires); 2896 rcu_read_unlock(); 2897 } 2898 2899 dst_set_expires(&rt0->dst, timeout); 2900 rt0->rt6i_flags |= RTF_EXPIRES; 2901 } 2902 2903 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 2904 { 2905 struct net *net = dev_net(rt->dst.dev); 2906 2907 dst_metric_set(&rt->dst, RTAX_MTU, mtu); 2908 rt->rt6i_flags |= RTF_MODIFIED; 2909 rt6_update_expires(rt, READ_ONCE(net->ipv6.sysctl.ip6_rt_mtu_expires)); 2910 } 2911 2912 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 2913 { 2914 return !(rt->rt6i_flags & RTF_CACHE) && 2915 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 2916 } 2917 2918 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 2919 const struct ipv6hdr *iph, u32 mtu, 2920 bool confirm_neigh) 2921 { 2922 const struct in6_addr *daddr, *saddr; 2923 struct rt6_info *rt6 = dst_rt6_info(dst); 2924 2925 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU) 2926 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it. 2927 * [see also comment in rt6_mtu_change_route()] 2928 */ 2929 2930 if (iph) { 2931 daddr = &iph->daddr; 2932 saddr = &iph->saddr; 2933 } else if (sk) { 2934 daddr = &sk->sk_v6_daddr; 2935 saddr = &inet6_sk(sk)->saddr; 2936 } else { 2937 daddr = NULL; 2938 saddr = NULL; 2939 } 2940 2941 if (confirm_neigh) 2942 dst_confirm_neigh(dst, daddr); 2943 2944 if (mtu < IPV6_MIN_MTU) 2945 return; 2946 if (mtu >= dst6_mtu(dst)) 2947 return; 2948 2949 if (!rt6_cache_allowed_for_pmtu(rt6)) { 2950 rt6_do_update_pmtu(rt6, mtu); 2951 /* update rt6_ex->stamp for cache */ 2952 if (rt6->rt6i_flags & RTF_CACHE) 2953 rt6_update_exception_stamp_rt(rt6); 2954 } else if (daddr) { 2955 struct fib6_result res = {}; 2956 struct rt6_info *nrt6; 2957 2958 rcu_read_lock(); 2959 res.f6i = rcu_dereference(rt6->from); 2960 if (!res.f6i) 2961 goto out_unlock; 2962 2963 res.fib6_flags = res.f6i->fib6_flags; 2964 res.fib6_type = res.f6i->fib6_type; 2965 2966 if (res.f6i->nh) { 2967 struct fib6_nh_match_arg arg = { 2968 .dev = dst_dev_rcu(dst), 2969 .gw = &rt6->rt6i_gateway, 2970 }; 2971 2972 nexthop_for_each_fib6_nh(res.f6i->nh, 2973 fib6_nh_find_match, &arg); 2974 2975 /* fib6_info uses a nexthop that does not have fib6_nh 2976 * using the dst->dev + gw. Should be impossible. 2977 */ 2978 if (!arg.match) 2979 goto out_unlock; 2980 2981 res.nh = arg.match; 2982 } else { 2983 res.nh = res.f6i->fib6_nh; 2984 } 2985 2986 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 2987 if (nrt6) { 2988 rt6_do_update_pmtu(nrt6, mtu); 2989 if (rt6_insert_exception(nrt6, &res)) 2990 dst_release_immediate(&nrt6->dst); 2991 } 2992 out_unlock: 2993 rcu_read_unlock(); 2994 } 2995 } 2996 2997 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 2998 struct sk_buff *skb, u32 mtu, 2999 bool confirm_neigh) 3000 { 3001 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, 3002 confirm_neigh); 3003 } 3004 3005 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 3006 int oif, u32 mark, kuid_t uid) 3007 { 3008 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 3009 struct dst_entry *dst; 3010 struct flowi6 fl6 = { 3011 .flowi6_oif = oif, 3012 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 3013 .daddr = iph->daddr, 3014 .saddr = iph->saddr, 3015 .flowlabel = ip6_flowinfo(iph), 3016 .flowi6_uid = uid, 3017 }; 3018 3019 dst = ip6_route_output(net, NULL, &fl6); 3020 if (!dst->error) 3021 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); 3022 dst_release(dst); 3023 } 3024 EXPORT_SYMBOL_GPL(ip6_update_pmtu); 3025 3026 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 3027 { 3028 int oif = sk->sk_bound_dev_if; 3029 struct dst_entry *dst; 3030 3031 if (!oif && skb->dev) 3032 oif = l3mdev_master_ifindex(skb->dev); 3033 3034 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark), 3035 sk_uid(sk)); 3036 3037 dst = __sk_dst_get(sk); 3038 if (!dst || !READ_ONCE(dst->obsolete) || 3039 dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 3040 return; 3041 3042 bh_lock_sock(sk); 3043 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 3044 ip6_datagram_dst_update(sk, false); 3045 bh_unlock_sock(sk); 3046 } 3047 3048 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 3049 const struct flowi6 *fl6) 3050 { 3051 #ifdef CONFIG_IPV6_SUBTREES 3052 struct ipv6_pinfo *np = inet6_sk(sk); 3053 #endif 3054 3055 ip6_dst_store(sk, dst, 3056 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr), 3057 #ifdef CONFIG_IPV6_SUBTREES 3058 ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 3059 true : 3060 #endif 3061 false); 3062 } 3063 3064 static bool ip6_redirect_nh_match(const struct fib6_result *res, 3065 struct flowi6 *fl6, 3066 const struct in6_addr *gw, 3067 struct rt6_info **ret) 3068 { 3069 const struct fib6_nh *nh = res->nh; 3070 3071 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 3072 fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 3073 return false; 3074 3075 /* rt_cache's gateway might be different from its 'parent' 3076 * in the case of an ip redirect. 3077 * So we keep searching in the exception table if the gateway 3078 * is different. 3079 */ 3080 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 3081 struct rt6_info *rt_cache; 3082 3083 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 3084 if (rt_cache && 3085 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 3086 *ret = rt_cache; 3087 return true; 3088 } 3089 return false; 3090 } 3091 return true; 3092 } 3093 3094 struct fib6_nh_rd_arg { 3095 struct fib6_result *res; 3096 struct flowi6 *fl6; 3097 const struct in6_addr *gw; 3098 struct rt6_info **ret; 3099 }; 3100 3101 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) 3102 { 3103 struct fib6_nh_rd_arg *arg = _arg; 3104 3105 arg->res->nh = nh; 3106 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); 3107 } 3108 3109 /* Handle redirects */ 3110 struct ip6rd_flowi { 3111 struct flowi6 fl6; 3112 struct in6_addr gateway; 3113 }; 3114 3115 INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net, 3116 struct fib6_table *table, 3117 struct flowi6 *fl6, 3118 const struct sk_buff *skb, 3119 int flags) 3120 { 3121 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 3122 struct rt6_info *ret = NULL; 3123 struct fib6_result res = {}; 3124 struct fib6_nh_rd_arg arg = { 3125 .res = &res, 3126 .fl6 = fl6, 3127 .gw = &rdfl->gateway, 3128 .ret = &ret 3129 }; 3130 struct fib6_info *rt; 3131 struct fib6_node *fn; 3132 3133 /* Get the "current" route for this destination and 3134 * check if the redirect has come from appropriate router. 3135 * 3136 * RFC 4861 specifies that redirects should only be 3137 * accepted if they come from the nexthop to the target. 3138 * Due to the way the routes are chosen, this notion 3139 * is a bit fuzzy and one might need to check all possible 3140 * routes. 3141 */ 3142 3143 rcu_read_lock(); 3144 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 3145 restart: 3146 for_each_fib6_node_rt_rcu(fn) { 3147 res.f6i = rt; 3148 if (fib6_check_expired(rt)) 3149 continue; 3150 if (rt->fib6_flags & RTF_REJECT) 3151 break; 3152 if (unlikely(rt->nh)) { 3153 if (nexthop_is_blackhole(rt->nh)) 3154 continue; 3155 /* on match, res->nh is filled in and potentially ret */ 3156 if (nexthop_for_each_fib6_nh(rt->nh, 3157 fib6_nh_redirect_match, 3158 &arg)) 3159 goto out; 3160 } else { 3161 res.nh = rt->fib6_nh; 3162 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, 3163 &ret)) 3164 goto out; 3165 } 3166 } 3167 3168 if (!rt) 3169 rt = net->ipv6.fib6_null_entry; 3170 else if (rt->fib6_flags & RTF_REJECT) { 3171 ret = net->ipv6.ip6_null_entry; 3172 goto out; 3173 } 3174 3175 if (rt == net->ipv6.fib6_null_entry) { 3176 fn = fib6_backtrack(fn, &fl6->saddr); 3177 if (fn) 3178 goto restart; 3179 } 3180 3181 res.f6i = rt; 3182 res.nh = rt->fib6_nh; 3183 out: 3184 if (ret) { 3185 ip6_hold_safe(net, &ret); 3186 } else { 3187 res.fib6_flags = res.f6i->fib6_flags; 3188 res.fib6_type = res.f6i->fib6_type; 3189 ret = ip6_create_rt_rcu(&res); 3190 } 3191 3192 rcu_read_unlock(); 3193 3194 trace_fib6_table_lookup(net, &res, table, fl6); 3195 return ret; 3196 }; 3197 3198 static struct dst_entry *ip6_route_redirect(struct net *net, 3199 const struct flowi6 *fl6, 3200 const struct sk_buff *skb, 3201 const struct in6_addr *gateway) 3202 { 3203 int flags = RT6_LOOKUP_F_HAS_SADDR; 3204 struct ip6rd_flowi rdfl; 3205 3206 rdfl.fl6 = *fl6; 3207 rdfl.gateway = *gateway; 3208 3209 return fib6_rule_lookup(net, &rdfl.fl6, skb, 3210 flags, __ip6_route_redirect); 3211 } 3212 3213 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 3214 kuid_t uid) 3215 { 3216 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 3217 struct dst_entry *dst; 3218 struct flowi6 fl6 = { 3219 .flowi6_iif = LOOPBACK_IFINDEX, 3220 .flowi6_oif = oif, 3221 .flowi6_mark = mark, 3222 .daddr = iph->daddr, 3223 .saddr = iph->saddr, 3224 .flowlabel = ip6_flowinfo(iph), 3225 .flowi6_uid = uid, 3226 }; 3227 3228 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 3229 rt6_do_redirect(dst, NULL, skb); 3230 dst_release(dst); 3231 } 3232 EXPORT_SYMBOL_GPL(ip6_redirect); 3233 3234 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 3235 { 3236 const struct ipv6hdr *iph = ipv6_hdr(skb); 3237 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 3238 struct dst_entry *dst; 3239 struct flowi6 fl6 = { 3240 .flowi6_iif = LOOPBACK_IFINDEX, 3241 .flowi6_oif = oif, 3242 .daddr = msg->dest, 3243 .saddr = iph->daddr, 3244 .flowi6_uid = sock_net_uid(net, NULL), 3245 }; 3246 3247 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 3248 rt6_do_redirect(dst, NULL, skb); 3249 dst_release(dst); 3250 } 3251 3252 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 3253 { 3254 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, 3255 READ_ONCE(sk->sk_mark), sk_uid(sk)); 3256 } 3257 3258 static unsigned int ip6_default_advmss(const struct dst_entry *dst) 3259 { 3260 unsigned int mtu = dst6_mtu(dst); 3261 struct net *net; 3262 3263 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 3264 3265 rcu_read_lock(); 3266 3267 net = dst_dev_net_rcu(dst); 3268 mtu = max_t(unsigned int, mtu, 3269 READ_ONCE(net->ipv6.sysctl.ip6_rt_min_advmss)); 3270 3271 rcu_read_unlock(); 3272 3273 /* 3274 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 3275 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 3276 * IPV6_MAXPLEN is also valid and means: "any MSS, 3277 * rely only on pmtu discovery" 3278 */ 3279 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 3280 mtu = IPV6_MAXPLEN; 3281 return mtu; 3282 } 3283 3284 INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst) 3285 { 3286 return ip6_dst_mtu_maybe_forward(dst, false); 3287 } 3288 EXPORT_INDIRECT_CALLABLE(ip6_mtu); 3289 3290 /* MTU selection: 3291 * 1. mtu on route is locked - use it 3292 * 2. mtu from nexthop exception 3293 * 3. mtu from egress device 3294 * 3295 * based on ip6_dst_mtu_forward and exception logic of 3296 * rt6_find_cached_rt; called with rcu_read_lock 3297 */ 3298 u32 ip6_mtu_from_fib6(const struct fib6_result *res, 3299 const struct in6_addr *daddr, 3300 const struct in6_addr *saddr) 3301 { 3302 const struct fib6_nh *nh = res->nh; 3303 struct fib6_info *f6i = res->f6i; 3304 struct inet6_dev *idev; 3305 struct rt6_info *rt; 3306 u32 mtu = 0; 3307 3308 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 3309 mtu = f6i->fib6_pmtu; 3310 if (mtu) 3311 goto out; 3312 } 3313 3314 rt = rt6_find_cached_rt(res, daddr, saddr); 3315 if (unlikely(rt)) { 3316 mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 3317 } else { 3318 struct net_device *dev = nh->fib_nh_dev; 3319 3320 mtu = IPV6_MIN_MTU; 3321 idev = __in6_dev_get(dev); 3322 if (idev) 3323 mtu = max_t(u32, mtu, READ_ONCE(idev->cnf.mtu6)); 3324 } 3325 3326 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 3327 out: 3328 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 3329 } 3330 3331 struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 3332 struct flowi6 *fl6) 3333 { 3334 struct dst_entry *dst; 3335 struct rt6_info *rt; 3336 struct inet6_dev *idev = in6_dev_get(dev); 3337 struct net *net = dev_net(dev); 3338 3339 if (unlikely(!idev)) 3340 return ERR_PTR(-ENODEV); 3341 3342 rt = ip6_dst_alloc(net, dev, 0); 3343 if (unlikely(!rt)) { 3344 in6_dev_put(idev); 3345 dst = ERR_PTR(-ENOMEM); 3346 goto out; 3347 } 3348 3349 rt->dst.input = ip6_input; 3350 rt->dst.output = ip6_output; 3351 rt->rt6i_gateway = fl6->daddr; 3352 rt->rt6i_dst.addr = fl6->daddr; 3353 rt->rt6i_dst.plen = 128; 3354 rt->rt6i_idev = idev; 3355 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 3356 3357 /* Add this dst into uncached_list so that rt6_disable_ip() can 3358 * do proper release of the net_device 3359 */ 3360 rt6_uncached_list_add(rt); 3361 3362 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 3363 3364 out: 3365 return dst; 3366 } 3367 3368 static void ip6_dst_gc(struct dst_ops *ops) 3369 { 3370 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 3371 int rt_min_interval = READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_min_interval); 3372 int rt_elasticity = READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_elasticity); 3373 int rt_gc_timeout = READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_timeout); 3374 unsigned long rt_last_gc = READ_ONCE(net->ipv6.ip6_rt_last_gc); 3375 unsigned int val; 3376 int entries; 3377 3378 if (time_after(rt_last_gc + rt_min_interval, jiffies)) 3379 goto out; 3380 3381 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true); 3382 entries = dst_entries_get_slow(ops); 3383 if (entries < ops->gc_thresh) 3384 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1); 3385 out: 3386 val = atomic_read(&net->ipv6.ip6_rt_gc_expire); 3387 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity)); 3388 } 3389 3390 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, 3391 const struct in6_addr *gw_addr, u32 tbid, 3392 int flags, struct fib6_result *res) 3393 { 3394 struct flowi6 fl6 = { 3395 .flowi6_oif = cfg->fc_ifindex, 3396 .daddr = *gw_addr, 3397 .saddr = cfg->fc_prefsrc, 3398 }; 3399 struct fib6_table *table; 3400 int err; 3401 3402 table = fib6_get_table(net, tbid); 3403 if (!table) 3404 return -EINVAL; 3405 3406 if (!ipv6_addr_any(&cfg->fc_prefsrc)) 3407 flags |= RT6_LOOKUP_F_HAS_SADDR; 3408 3409 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 3410 3411 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); 3412 if (!err && res->f6i != net->ipv6.fib6_null_entry) 3413 fib6_select_path(net, res, &fl6, cfg->fc_ifindex, 3414 cfg->fc_ifindex != 0, NULL, flags); 3415 3416 return err; 3417 } 3418 3419 static int ip6_route_check_nh_onlink(struct net *net, 3420 struct fib6_config *cfg, 3421 const struct net_device *dev, 3422 struct netlink_ext_ack *extack) 3423 { 3424 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 3425 const struct in6_addr *gw_addr = &cfg->fc_gateway; 3426 struct fib6_result res = {}; 3427 int err; 3428 3429 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); 3430 if (!err && !(res.fib6_flags & RTF_REJECT) && 3431 res.fib6_type != RTN_UNICAST) { 3432 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); 3433 err = -EINVAL; 3434 } 3435 3436 return err; 3437 } 3438 3439 static int ip6_route_check_nh(struct net *net, 3440 struct fib6_config *cfg, 3441 struct net_device **_dev, 3442 netdevice_tracker *dev_tracker, 3443 struct inet6_dev **idev) 3444 { 3445 const struct in6_addr *gw_addr = &cfg->fc_gateway; 3446 struct net_device *dev = _dev ? *_dev : NULL; 3447 int flags = RT6_LOOKUP_F_IFACE; 3448 struct fib6_result res = {}; 3449 int err = -EHOSTUNREACH; 3450 3451 if (cfg->fc_table) { 3452 err = ip6_nh_lookup_table(net, cfg, gw_addr, 3453 cfg->fc_table, flags, &res); 3454 /* gw_addr can not require a gateway or resolve to a reject 3455 * route. If a device is given, it must match the result. 3456 */ 3457 if (err || res.fib6_flags & RTF_REJECT || 3458 res.nh->fib_nh_gw_family || 3459 (dev && dev != res.nh->fib_nh_dev)) 3460 err = -EHOSTUNREACH; 3461 } 3462 3463 if (err < 0) { 3464 struct flowi6 fl6 = { 3465 .flowi6_oif = cfg->fc_ifindex, 3466 .daddr = *gw_addr, 3467 }; 3468 3469 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); 3470 if (err || res.fib6_flags & RTF_REJECT || 3471 res.nh->fib_nh_gw_family) 3472 err = -EHOSTUNREACH; 3473 3474 if (err) 3475 return err; 3476 3477 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, 3478 cfg->fc_ifindex != 0, NULL, flags); 3479 } 3480 3481 err = 0; 3482 if (dev) { 3483 if (dev != res.nh->fib_nh_dev) 3484 err = -EHOSTUNREACH; 3485 } else { 3486 *_dev = dev = res.nh->fib_nh_dev; 3487 netdev_hold(dev, dev_tracker, GFP_ATOMIC); 3488 *idev = in6_dev_get(dev); 3489 } 3490 3491 return err; 3492 } 3493 3494 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 3495 struct net_device **_dev, 3496 netdevice_tracker *dev_tracker, 3497 struct inet6_dev **idev, 3498 struct netlink_ext_ack *extack) 3499 { 3500 const struct in6_addr *gw_addr = &cfg->fc_gateway; 3501 int gwa_type = ipv6_addr_type(gw_addr); 3502 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 3503 const struct net_device *dev = *_dev; 3504 bool need_addr_check = !dev; 3505 int err = -EINVAL; 3506 3507 /* if gw_addr is local we will fail to detect this in case 3508 * address is still TENTATIVE (DAD in progress). rt6_lookup() 3509 * will return already-added prefix route via interface that 3510 * prefix route was assigned to, which might be non-loopback. 3511 */ 3512 if (dev && 3513 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3514 NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3515 goto out; 3516 } 3517 3518 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 3519 /* IPv6 strictly inhibits using not link-local 3520 * addresses as nexthop address. 3521 * Otherwise, router will not able to send redirects. 3522 * It is very good, but in some (rare!) circumstances 3523 * (SIT, PtP, NBMA NOARP links) it is handy to allow 3524 * some exceptions. --ANK 3525 * We allow IPv4-mapped nexthops to support RFC4798-type 3526 * addressing 3527 */ 3528 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 3529 NL_SET_ERR_MSG(extack, "Invalid gateway address"); 3530 goto out; 3531 } 3532 3533 rcu_read_lock(); 3534 3535 if (cfg->fc_flags & RTNH_F_ONLINK) 3536 err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 3537 else 3538 err = ip6_route_check_nh(net, cfg, _dev, dev_tracker, 3539 idev); 3540 3541 rcu_read_unlock(); 3542 3543 if (err) 3544 goto out; 3545 } 3546 3547 /* reload in case device was changed */ 3548 dev = *_dev; 3549 3550 err = -EINVAL; 3551 if (!dev) { 3552 NL_SET_ERR_MSG(extack, "Egress device not specified"); 3553 goto out; 3554 } else if (dev->flags & IFF_LOOPBACK) { 3555 NL_SET_ERR_MSG(extack, 3556 "Egress device can not be loopback device for this route"); 3557 goto out; 3558 } 3559 3560 /* if we did not check gw_addr above, do so now that the 3561 * egress device has been resolved. 3562 */ 3563 if (need_addr_check && 3564 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3565 NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3566 goto out; 3567 } 3568 3569 err = 0; 3570 out: 3571 return err; 3572 } 3573 3574 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 3575 { 3576 if ((flags & RTF_REJECT) || 3577 (dev && (dev->flags & IFF_LOOPBACK) && 3578 !(addr_type & IPV6_ADDR_LOOPBACK) && 3579 !(flags & (RTF_ANYCAST | RTF_LOCAL)))) 3580 return true; 3581 3582 return false; 3583 } 3584 3585 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 3586 struct fib6_config *cfg, gfp_t gfp_flags, 3587 struct netlink_ext_ack *extack) 3588 { 3589 netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker; 3590 struct net_device *dev = NULL; 3591 struct inet6_dev *idev = NULL; 3592 int err; 3593 3594 if (!ipv6_mod_enabled()) { 3595 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel"); 3596 return -EAFNOSUPPORT; 3597 } 3598 3599 fib6_nh->fib_nh_family = AF_INET6; 3600 #ifdef CONFIG_IPV6_ROUTER_PREF 3601 fib6_nh->last_probe = jiffies; 3602 #endif 3603 if (cfg->fc_is_fdb) { 3604 fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3605 fib6_nh->fib_nh_gw_family = AF_INET6; 3606 return 0; 3607 } 3608 3609 err = -ENODEV; 3610 if (cfg->fc_ifindex) { 3611 dev = netdev_get_by_index(net, cfg->fc_ifindex, 3612 dev_tracker, gfp_flags); 3613 if (!dev) 3614 goto out; 3615 idev = in6_dev_get(dev); 3616 if (!idev) 3617 goto out; 3618 } 3619 3620 if (cfg->fc_flags & RTNH_F_ONLINK) { 3621 if (!dev) { 3622 NL_SET_ERR_MSG(extack, 3623 "Nexthop device required for onlink"); 3624 goto out; 3625 } 3626 3627 if (!(dev->flags & IFF_UP)) { 3628 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 3629 err = -ENETDOWN; 3630 goto out; 3631 } 3632 3633 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 3634 } 3635 3636 fib6_nh->fib_nh_weight = 1; 3637 3638 /* Reset the nexthop device to the loopback device in case of reject 3639 * routes. 3640 */ 3641 if (cfg->fc_flags & RTF_REJECT) { 3642 /* hold loopback dev/idev if we haven't done so. */ 3643 if (dev != net->loopback_dev) { 3644 if (dev) { 3645 netdev_put(dev, dev_tracker); 3646 in6_dev_put(idev); 3647 } 3648 dev = net->loopback_dev; 3649 netdev_hold(dev, dev_tracker, gfp_flags); 3650 idev = in6_dev_get(dev); 3651 if (!idev) { 3652 err = -ENODEV; 3653 goto out; 3654 } 3655 } 3656 goto pcpu_alloc; 3657 } 3658 3659 if (cfg->fc_flags & RTF_GATEWAY) { 3660 err = ip6_validate_gw(net, cfg, &dev, dev_tracker, 3661 &idev, extack); 3662 if (err) 3663 goto out; 3664 3665 fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3666 fib6_nh->fib_nh_gw_family = AF_INET6; 3667 } 3668 3669 err = -ENODEV; 3670 if (!dev) 3671 goto out; 3672 3673 if (!idev || idev->cnf.disable_ipv6) { 3674 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 3675 err = -EACCES; 3676 goto out; 3677 } 3678 3679 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 3680 NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 3681 err = -ENETDOWN; 3682 goto out; 3683 } 3684 3685 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 3686 !netif_carrier_ok(dev)) 3687 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 3688 3689 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap, 3690 cfg->fc_encap_type, cfg, gfp_flags, extack); 3691 if (err) 3692 goto out; 3693 3694 pcpu_alloc: 3695 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3696 if (!fib6_nh->rt6i_pcpu) { 3697 err = -ENOMEM; 3698 goto out; 3699 } 3700 3701 fib6_nh->fib_nh_dev = dev; 3702 fib6_nh->fib_nh_oif = dev->ifindex; 3703 err = 0; 3704 out: 3705 if (idev) 3706 in6_dev_put(idev); 3707 3708 if (err) { 3709 fib_nh_common_release(&fib6_nh->nh_common); 3710 fib6_nh->nh_common.nhc_pcpu_rth_output = NULL; 3711 fib6_nh->fib_nh_lws = NULL; 3712 netdev_put(dev, dev_tracker); 3713 } 3714 3715 return err; 3716 } 3717 3718 void fib6_nh_release(struct fib6_nh *fib6_nh) 3719 { 3720 struct rt6_exception_bucket *bucket; 3721 3722 rcu_read_lock(); 3723 3724 fib6_nh_flush_exceptions(fib6_nh, NULL); 3725 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); 3726 if (bucket) { 3727 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); 3728 kfree(bucket); 3729 } 3730 3731 rcu_read_unlock(); 3732 3733 fib6_nh_release_dsts(fib6_nh); 3734 free_percpu(fib6_nh->rt6i_pcpu); 3735 3736 fib_nh_common_release(&fib6_nh->nh_common); 3737 } 3738 3739 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh) 3740 { 3741 int cpu; 3742 3743 if (!fib6_nh->rt6i_pcpu) 3744 return; 3745 3746 for_each_possible_cpu(cpu) { 3747 struct rt6_info *pcpu_rt, **ppcpu_rt; 3748 3749 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 3750 pcpu_rt = xchg(ppcpu_rt, NULL); 3751 if (pcpu_rt) { 3752 dst_dev_put(&pcpu_rt->dst); 3753 dst_release(&pcpu_rt->dst); 3754 } 3755 } 3756 } 3757 3758 static int fib6_config_validate(struct fib6_config *cfg, 3759 struct netlink_ext_ack *extack) 3760 { 3761 /* RTF_PCPU is an internal flag; can not be set by userspace */ 3762 if (cfg->fc_flags & RTF_PCPU) { 3763 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3764 goto errout; 3765 } 3766 3767 /* RTF_CACHE is an internal flag; can not be set by userspace */ 3768 if (cfg->fc_flags & RTF_CACHE) { 3769 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 3770 goto errout; 3771 } 3772 3773 if (cfg->fc_type > RTN_MAX) { 3774 NL_SET_ERR_MSG(extack, "Invalid route type"); 3775 goto errout; 3776 } 3777 3778 if (cfg->fc_dst_len > 128) { 3779 NL_SET_ERR_MSG(extack, "Invalid prefix length"); 3780 goto errout; 3781 } 3782 3783 #ifdef CONFIG_IPV6_SUBTREES 3784 if (cfg->fc_src_len > 128) { 3785 NL_SET_ERR_MSG(extack, "Invalid source address length"); 3786 goto errout; 3787 } 3788 3789 if (cfg->fc_nh_id && cfg->fc_src_len) { 3790 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); 3791 goto errout; 3792 } 3793 #else 3794 if (cfg->fc_src_len) { 3795 NL_SET_ERR_MSG(extack, 3796 "Specifying source address requires IPV6_SUBTREES to be enabled"); 3797 goto errout; 3798 } 3799 #endif 3800 return 0; 3801 errout: 3802 return -EINVAL; 3803 } 3804 3805 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3806 gfp_t gfp_flags, 3807 struct netlink_ext_ack *extack) 3808 { 3809 struct net *net = cfg->fc_nlinfo.nl_net; 3810 struct fib6_table *table; 3811 struct fib6_info *rt; 3812 int err; 3813 3814 if (cfg->fc_nlinfo.nlh && 3815 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3816 table = fib6_get_table(net, cfg->fc_table); 3817 if (!table) { 3818 pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3819 table = fib6_new_table(net, cfg->fc_table); 3820 } 3821 } else { 3822 table = fib6_new_table(net, cfg->fc_table); 3823 } 3824 if (!table) { 3825 err = -ENOBUFS; 3826 goto err; 3827 } 3828 3829 rt = fib6_info_alloc(gfp_flags, !cfg->fc_nh_id); 3830 if (!rt) { 3831 err = -ENOMEM; 3832 goto err; 3833 } 3834 3835 rt->fib6_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, 3836 extack); 3837 if (IS_ERR(rt->fib6_metrics)) { 3838 err = PTR_ERR(rt->fib6_metrics); 3839 goto free; 3840 } 3841 3842 if (cfg->fc_flags & RTF_ADDRCONF) 3843 rt->dst_nocount = true; 3844 3845 if (cfg->fc_flags & RTF_EXPIRES) 3846 fib6_set_expires(rt, jiffies + 3847 clock_t_to_jiffies(cfg->fc_expires)); 3848 3849 if (cfg->fc_protocol == RTPROT_UNSPEC) 3850 cfg->fc_protocol = RTPROT_BOOT; 3851 3852 rt->fib6_protocol = cfg->fc_protocol; 3853 rt->fib6_table = table; 3854 rt->fib6_metric = cfg->fc_metric; 3855 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; 3856 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 3857 3858 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 3859 rt->fib6_dst.plen = cfg->fc_dst_len; 3860 3861 #ifdef CONFIG_IPV6_SUBTREES 3862 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 3863 rt->fib6_src.plen = cfg->fc_src_len; 3864 #endif 3865 return rt; 3866 free: 3867 kfree(rt); 3868 err: 3869 return ERR_PTR(err); 3870 } 3871 3872 static int ip6_route_info_create_nh(struct fib6_info *rt, 3873 struct fib6_config *cfg, 3874 gfp_t gfp_flags, 3875 struct netlink_ext_ack *extack) 3876 { 3877 struct net *net = cfg->fc_nlinfo.nl_net; 3878 struct fib6_nh *fib6_nh; 3879 int err; 3880 3881 if (cfg->fc_nh_id) { 3882 struct nexthop *nh; 3883 3884 rcu_read_lock(); 3885 3886 nh = nexthop_find_by_id(net, cfg->fc_nh_id); 3887 if (!nh) { 3888 err = -EINVAL; 3889 NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 3890 goto out_free; 3891 } 3892 3893 err = fib6_check_nexthop(nh, cfg, extack); 3894 if (err) 3895 goto out_free; 3896 3897 if (!nexthop_get(nh)) { 3898 NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 3899 err = -ENOENT; 3900 goto out_free; 3901 } 3902 3903 rt->nh = nh; 3904 fib6_nh = nexthop_fib6_nh(rt->nh); 3905 3906 rcu_read_unlock(); 3907 } else { 3908 int addr_type; 3909 3910 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); 3911 if (err) 3912 goto out_release; 3913 3914 fib6_nh = rt->fib6_nh; 3915 3916 /* We cannot add true routes via loopback here, they would 3917 * result in kernel looping; promote them to reject routes 3918 */ 3919 addr_type = ipv6_addr_type(&cfg->fc_dst); 3920 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, 3921 addr_type)) 3922 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3923 } 3924 3925 if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3926 struct net_device *dev = fib6_nh->fib_nh_dev; 3927 3928 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3929 NL_SET_ERR_MSG(extack, "Invalid source address"); 3930 err = -EINVAL; 3931 goto out_release; 3932 } 3933 rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 3934 rt->fib6_prefsrc.plen = 128; 3935 } 3936 3937 return 0; 3938 out_release: 3939 fib6_info_release(rt); 3940 return err; 3941 out_free: 3942 rcu_read_unlock(); 3943 ip_fib_metrics_put(rt->fib6_metrics); 3944 kfree(rt); 3945 return err; 3946 } 3947 3948 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3949 struct netlink_ext_ack *extack) 3950 { 3951 struct fib6_info *rt; 3952 int err; 3953 3954 err = fib6_config_validate(cfg, extack); 3955 if (err) 3956 return err; 3957 3958 rt = ip6_route_info_create(cfg, gfp_flags, extack); 3959 if (IS_ERR(rt)) 3960 return PTR_ERR(rt); 3961 3962 err = ip6_route_info_create_nh(rt, cfg, gfp_flags, extack); 3963 if (err) 3964 return err; 3965 3966 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 3967 fib6_info_release(rt); 3968 3969 return err; 3970 } 3971 3972 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 3973 { 3974 struct net *net = info->nl_net; 3975 struct fib6_table *table; 3976 int err; 3977 3978 if (rt == net->ipv6.fib6_null_entry) { 3979 err = -ENOENT; 3980 goto out; 3981 } 3982 3983 table = rt->fib6_table; 3984 spin_lock_bh(&table->tb6_lock); 3985 err = fib6_del(rt, info); 3986 spin_unlock_bh(&table->tb6_lock); 3987 3988 out: 3989 fib6_info_release(rt); 3990 return err; 3991 } 3992 3993 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify) 3994 { 3995 struct nl_info info = { 3996 .nl_net = net, 3997 .skip_notify = skip_notify 3998 }; 3999 4000 return __ip6_del_rt(rt, &info); 4001 } 4002 4003 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 4004 { 4005 struct nl_info *info = &cfg->fc_nlinfo; 4006 struct net *net = info->nl_net; 4007 struct sk_buff *skb = NULL; 4008 struct fib6_table *table; 4009 int err = -ENOENT; 4010 4011 if (rt == net->ipv6.fib6_null_entry) 4012 goto out_put; 4013 table = rt->fib6_table; 4014 spin_lock_bh(&table->tb6_lock); 4015 4016 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 4017 struct fib6_info *sibling, *next_sibling; 4018 struct fib6_node *fn; 4019 4020 /* prefer to send a single notification with all hops */ 4021 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 4022 if (skb) { 4023 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 4024 4025 if (rt6_fill_node(net, skb, rt, NULL, 4026 NULL, NULL, 0, RTM_DELROUTE, 4027 info->portid, seq, 0) < 0) { 4028 kfree_skb(skb); 4029 skb = NULL; 4030 } else 4031 info->skip_notify = 1; 4032 } 4033 4034 /* 'rt' points to the first sibling route. If it is not the 4035 * leaf, then we do not need to send a notification. Otherwise, 4036 * we need to check if the last sibling has a next route or not 4037 * and emit a replace or delete notification, respectively. 4038 */ 4039 info->skip_notify_kernel = 1; 4040 fn = rcu_dereference_protected(rt->fib6_node, 4041 lockdep_is_held(&table->tb6_lock)); 4042 if (rcu_access_pointer(fn->leaf) == rt) { 4043 struct fib6_info *last_sibling, *replace_rt; 4044 4045 last_sibling = list_last_entry(&rt->fib6_siblings, 4046 struct fib6_info, 4047 fib6_siblings); 4048 replace_rt = rcu_dereference_protected( 4049 last_sibling->fib6_next, 4050 lockdep_is_held(&table->tb6_lock)); 4051 if (replace_rt) 4052 call_fib6_entry_notifiers_replace(net, 4053 replace_rt); 4054 else 4055 call_fib6_multipath_entry_notifiers(net, 4056 FIB_EVENT_ENTRY_DEL, 4057 rt, rt->fib6_nsiblings, 4058 NULL); 4059 } 4060 list_for_each_entry_safe(sibling, next_sibling, 4061 &rt->fib6_siblings, 4062 fib6_siblings) { 4063 err = fib6_del(sibling, info); 4064 if (err) 4065 goto out_unlock; 4066 } 4067 } 4068 4069 err = fib6_del(rt, info); 4070 out_unlock: 4071 spin_unlock_bh(&table->tb6_lock); 4072 out_put: 4073 fib6_info_release(rt); 4074 4075 if (skb) { 4076 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 4077 info->nlh, gfp_any()); 4078 } 4079 return err; 4080 } 4081 4082 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 4083 { 4084 int rc = -ESRCH; 4085 4086 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 4087 goto out; 4088 4089 if (cfg->fc_flags & RTF_GATEWAY && 4090 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 4091 goto out; 4092 4093 rc = rt6_remove_exception_rt(rt); 4094 out: 4095 return rc; 4096 } 4097 4098 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, 4099 struct fib6_nh *nh) 4100 { 4101 struct fib6_result res = { 4102 .f6i = rt, 4103 .nh = nh, 4104 }; 4105 struct rt6_info *rt_cache; 4106 4107 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); 4108 if (rt_cache) 4109 return __ip6_del_cached_rt(rt_cache, cfg); 4110 4111 return 0; 4112 } 4113 4114 struct fib6_nh_del_cached_rt_arg { 4115 struct fib6_config *cfg; 4116 struct fib6_info *f6i; 4117 }; 4118 4119 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) 4120 { 4121 struct fib6_nh_del_cached_rt_arg *arg = _arg; 4122 int rc; 4123 4124 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); 4125 return rc != -ESRCH ? rc : 0; 4126 } 4127 4128 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) 4129 { 4130 struct fib6_nh_del_cached_rt_arg arg = { 4131 .cfg = cfg, 4132 .f6i = f6i 4133 }; 4134 4135 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); 4136 } 4137 4138 static int ip6_route_del(struct fib6_config *cfg, 4139 struct netlink_ext_ack *extack) 4140 { 4141 struct fib6_table *table; 4142 struct fib6_info *rt; 4143 struct fib6_node *fn; 4144 int err = -ESRCH; 4145 4146 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 4147 if (!table) { 4148 NL_SET_ERR_MSG(extack, "FIB table does not exist"); 4149 return err; 4150 } 4151 4152 rcu_read_lock(); 4153 4154 fn = fib6_locate(&table->tb6_root, 4155 &cfg->fc_dst, cfg->fc_dst_len, 4156 &cfg->fc_src, cfg->fc_src_len, 4157 !(cfg->fc_flags & RTF_CACHE)); 4158 4159 if (fn) { 4160 for_each_fib6_node_rt_rcu(fn) { 4161 struct fib6_nh *nh; 4162 4163 if (rt->nh && cfg->fc_nh_id && 4164 rt->nh->id != cfg->fc_nh_id) 4165 continue; 4166 4167 if (cfg->fc_flags & RTF_CACHE) { 4168 int rc = 0; 4169 4170 if (rt->nh) { 4171 rc = ip6_del_cached_rt_nh(cfg, rt); 4172 } else if (cfg->fc_nh_id) { 4173 continue; 4174 } else { 4175 nh = rt->fib6_nh; 4176 rc = ip6_del_cached_rt(cfg, rt, nh); 4177 } 4178 if (rc != -ESRCH) { 4179 rcu_read_unlock(); 4180 return rc; 4181 } 4182 continue; 4183 } 4184 4185 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 4186 continue; 4187 if (cfg->fc_protocol && 4188 cfg->fc_protocol != rt->fib6_protocol) 4189 continue; 4190 4191 if (rt->nh) { 4192 if (!fib6_info_hold_safe(rt)) 4193 continue; 4194 4195 err = __ip6_del_rt(rt, &cfg->fc_nlinfo); 4196 break; 4197 } 4198 if (cfg->fc_nh_id) 4199 continue; 4200 4201 nh = rt->fib6_nh; 4202 if (cfg->fc_ifindex && 4203 (!nh->fib_nh_dev || 4204 nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 4205 continue; 4206 if (cfg->fc_flags & RTF_GATEWAY && 4207 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 4208 continue; 4209 if (!fib6_info_hold_safe(rt)) 4210 continue; 4211 4212 /* if gateway was specified only delete the one hop */ 4213 if (cfg->fc_flags & RTF_GATEWAY) 4214 err = __ip6_del_rt(rt, &cfg->fc_nlinfo); 4215 else 4216 err = __ip6_del_rt_siblings(rt, cfg); 4217 break; 4218 } 4219 } 4220 rcu_read_unlock(); 4221 4222 return err; 4223 } 4224 4225 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 4226 { 4227 struct netevent_redirect netevent; 4228 struct rt6_info *rt, *nrt = NULL; 4229 struct fib6_result res = {}; 4230 struct ndisc_options ndopts; 4231 struct inet6_dev *in6_dev; 4232 struct neighbour *neigh; 4233 struct rd_msg *msg; 4234 int optlen, on_link; 4235 u8 *lladdr; 4236 4237 optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 4238 optlen -= sizeof(*msg); 4239 4240 if (optlen < 0) { 4241 net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 4242 return; 4243 } 4244 4245 msg = (struct rd_msg *)icmp6_hdr(skb); 4246 4247 if (ipv6_addr_is_multicast(&msg->dest)) { 4248 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 4249 return; 4250 } 4251 4252 on_link = 0; 4253 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 4254 on_link = 1; 4255 } else if (ipv6_addr_type(&msg->target) != 4256 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 4257 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 4258 return; 4259 } 4260 4261 in6_dev = __in6_dev_get(skb->dev); 4262 if (!in6_dev) 4263 return; 4264 if (READ_ONCE(in6_dev->cnf.forwarding) || 4265 !READ_ONCE(in6_dev->cnf.accept_redirects)) 4266 return; 4267 4268 /* RFC2461 8.1: 4269 * The IP source address of the Redirect MUST be the same as the current 4270 * first-hop router for the specified ICMP Destination Address. 4271 */ 4272 4273 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 4274 net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 4275 return; 4276 } 4277 4278 lladdr = NULL; 4279 if (ndopts.nd_opts_tgt_lladdr) { 4280 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 4281 skb->dev); 4282 if (!lladdr) { 4283 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 4284 return; 4285 } 4286 } 4287 4288 rt = dst_rt6_info(dst); 4289 if (rt->rt6i_flags & RTF_REJECT) { 4290 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 4291 return; 4292 } 4293 4294 /* Redirect received -> path was valid. 4295 * Look, redirects are sent only in response to data packets, 4296 * so that this nexthop apparently is reachable. --ANK 4297 */ 4298 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 4299 4300 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 4301 if (!neigh) 4302 return; 4303 4304 /* 4305 * We have finally decided to accept it. 4306 */ 4307 4308 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 4309 NEIGH_UPDATE_F_WEAK_OVERRIDE| 4310 NEIGH_UPDATE_F_OVERRIDE| 4311 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 4312 NEIGH_UPDATE_F_ISROUTER)), 4313 NDISC_REDIRECT, &ndopts); 4314 4315 rcu_read_lock(); 4316 res.f6i = rcu_dereference(rt->from); 4317 if (!res.f6i) 4318 goto out; 4319 4320 if (res.f6i->nh) { 4321 struct fib6_nh_match_arg arg = { 4322 .dev = dst_dev_rcu(dst), 4323 .gw = &rt->rt6i_gateway, 4324 }; 4325 4326 nexthop_for_each_fib6_nh(res.f6i->nh, 4327 fib6_nh_find_match, &arg); 4328 4329 /* fib6_info uses a nexthop that does not have fib6_nh 4330 * using the dst->dev. Should be impossible 4331 */ 4332 if (!arg.match) 4333 goto out; 4334 res.nh = arg.match; 4335 } else { 4336 res.nh = res.f6i->fib6_nh; 4337 } 4338 4339 res.fib6_flags = res.f6i->fib6_flags; 4340 res.fib6_type = res.f6i->fib6_type; 4341 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 4342 if (!nrt) 4343 goto out; 4344 4345 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 4346 if (on_link) 4347 nrt->rt6i_flags &= ~RTF_GATEWAY; 4348 4349 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 4350 4351 /* rt6_insert_exception() will take care of duplicated exceptions */ 4352 if (rt6_insert_exception(nrt, &res)) { 4353 dst_release_immediate(&nrt->dst); 4354 goto out; 4355 } 4356 4357 netevent.old = &rt->dst; 4358 netevent.new = &nrt->dst; 4359 netevent.daddr = &msg->dest; 4360 netevent.neigh = neigh; 4361 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 4362 4363 out: 4364 rcu_read_unlock(); 4365 neigh_release(neigh); 4366 } 4367 4368 #ifdef CONFIG_IPV6_ROUTE_INFO 4369 static struct fib6_info *rt6_get_route_info(struct net *net, 4370 const struct in6_addr *prefix, int prefixlen, 4371 const struct in6_addr *gwaddr, 4372 struct net_device *dev) 4373 { 4374 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 4375 int ifindex = dev->ifindex; 4376 struct fib6_node *fn; 4377 struct fib6_info *rt = NULL; 4378 struct fib6_table *table; 4379 4380 table = fib6_get_table(net, tb_id); 4381 if (!table) 4382 return NULL; 4383 4384 rcu_read_lock(); 4385 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 4386 if (!fn) 4387 goto out; 4388 4389 for_each_fib6_node_rt_rcu(fn) { 4390 /* these routes do not use nexthops */ 4391 if (rt->nh) 4392 continue; 4393 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) 4394 continue; 4395 if (!(rt->fib6_flags & RTF_ROUTEINFO) || 4396 !rt->fib6_nh->fib_nh_gw_family) 4397 continue; 4398 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) 4399 continue; 4400 if (!fib6_info_hold_safe(rt)) 4401 continue; 4402 break; 4403 } 4404 out: 4405 rcu_read_unlock(); 4406 return rt; 4407 } 4408 4409 static struct fib6_info *rt6_add_route_info(struct net *net, 4410 const struct in6_addr *prefix, int prefixlen, 4411 const struct in6_addr *gwaddr, 4412 struct net_device *dev, 4413 unsigned int pref) 4414 { 4415 struct fib6_config cfg = { 4416 .fc_metric = IP6_RT_PRIO_USER, 4417 .fc_ifindex = dev->ifindex, 4418 .fc_dst_len = prefixlen, 4419 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 4420 RTF_UP | RTF_PREF(pref), 4421 .fc_protocol = RTPROT_RA, 4422 .fc_type = RTN_UNICAST, 4423 .fc_nlinfo.portid = 0, 4424 .fc_nlinfo.nlh = NULL, 4425 .fc_nlinfo.nl_net = net, 4426 }; 4427 4428 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 4429 cfg.fc_dst = *prefix; 4430 cfg.fc_gateway = *gwaddr; 4431 4432 /* We should treat it as a default route if prefix length is 0. */ 4433 if (!prefixlen) 4434 cfg.fc_flags |= RTF_DEFAULT; 4435 4436 ip6_route_add(&cfg, GFP_ATOMIC, NULL); 4437 4438 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 4439 } 4440 #endif 4441 4442 struct fib6_info *rt6_get_dflt_router(struct net *net, 4443 const struct in6_addr *addr, 4444 struct net_device *dev) 4445 { 4446 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 4447 struct fib6_info *rt; 4448 struct fib6_table *table; 4449 4450 table = fib6_get_table(net, tb_id); 4451 if (!table) 4452 return NULL; 4453 4454 rcu_read_lock(); 4455 for_each_fib6_node_rt_rcu(&table->tb6_root) { 4456 struct fib6_nh *nh; 4457 4458 /* RA routes do not use nexthops */ 4459 if (rt->nh) 4460 continue; 4461 4462 nh = rt->fib6_nh; 4463 if (dev == nh->fib_nh_dev && 4464 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 4465 ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 4466 break; 4467 } 4468 if (rt && !fib6_info_hold_safe(rt)) 4469 rt = NULL; 4470 rcu_read_unlock(); 4471 return rt; 4472 } 4473 4474 struct fib6_info *rt6_add_dflt_router(struct net *net, 4475 const struct in6_addr *gwaddr, 4476 struct net_device *dev, 4477 unsigned int pref, 4478 u32 defrtr_usr_metric, 4479 int lifetime) 4480 { 4481 struct fib6_config cfg = { 4482 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 4483 .fc_metric = defrtr_usr_metric, 4484 .fc_ifindex = dev->ifindex, 4485 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 4486 RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 4487 .fc_protocol = RTPROT_RA, 4488 .fc_type = RTN_UNICAST, 4489 .fc_nlinfo.portid = 0, 4490 .fc_nlinfo.nlh = NULL, 4491 .fc_nlinfo.nl_net = net, 4492 .fc_expires = jiffies_to_clock_t(lifetime * HZ), 4493 }; 4494 4495 cfg.fc_gateway = *gwaddr; 4496 4497 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 4498 struct fib6_table *table; 4499 4500 table = fib6_get_table(dev_net(dev), cfg.fc_table); 4501 if (table) 4502 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 4503 } 4504 4505 return rt6_get_dflt_router(net, gwaddr, dev); 4506 } 4507 4508 static void __rt6_purge_dflt_routers(struct net *net, 4509 struct fib6_table *table) 4510 { 4511 struct fib6_info *rt; 4512 4513 restart: 4514 rcu_read_lock(); 4515 for_each_fib6_node_rt_rcu(&table->tb6_root) { 4516 struct net_device *dev = fib6_info_nh_dev(rt); 4517 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 4518 4519 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 4520 (!idev || idev->cnf.accept_ra != 2) && 4521 fib6_info_hold_safe(rt)) { 4522 rcu_read_unlock(); 4523 ip6_del_rt(net, rt, false); 4524 goto restart; 4525 } 4526 } 4527 rcu_read_unlock(); 4528 4529 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 4530 } 4531 4532 void rt6_purge_dflt_routers(struct net *net) 4533 { 4534 struct fib6_table *table; 4535 struct hlist_head *head; 4536 unsigned int h; 4537 4538 rcu_read_lock(); 4539 4540 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 4541 head = &net->ipv6.fib_table_hash[h]; 4542 hlist_for_each_entry_rcu(table, head, tb6_hlist) { 4543 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 4544 __rt6_purge_dflt_routers(net, table); 4545 } 4546 } 4547 4548 rcu_read_unlock(); 4549 } 4550 4551 static void rtmsg_to_fib6_config(struct net *net, 4552 struct in6_rtmsg *rtmsg, 4553 struct fib6_config *cfg) 4554 { 4555 *cfg = (struct fib6_config){ 4556 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 4557 : RT6_TABLE_MAIN, 4558 .fc_ifindex = rtmsg->rtmsg_ifindex, 4559 .fc_metric = rtmsg->rtmsg_metric, 4560 .fc_expires = rtmsg->rtmsg_info, 4561 .fc_dst_len = rtmsg->rtmsg_dst_len, 4562 .fc_src_len = rtmsg->rtmsg_src_len, 4563 .fc_flags = rtmsg->rtmsg_flags, 4564 .fc_type = rtmsg->rtmsg_type, 4565 4566 .fc_nlinfo.nl_net = net, 4567 4568 .fc_dst = rtmsg->rtmsg_dst, 4569 .fc_src = rtmsg->rtmsg_src, 4570 .fc_gateway = rtmsg->rtmsg_gateway, 4571 }; 4572 } 4573 4574 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg) 4575 { 4576 struct fib6_config cfg; 4577 int err; 4578 4579 if (cmd != SIOCADDRT && cmd != SIOCDELRT) 4580 return -EINVAL; 4581 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4582 return -EPERM; 4583 4584 rtmsg_to_fib6_config(net, rtmsg, &cfg); 4585 4586 switch (cmd) { 4587 case SIOCADDRT: 4588 /* Only do the default setting of fc_metric in route adding */ 4589 if (cfg.fc_metric == 0) 4590 cfg.fc_metric = IP6_RT_PRIO_USER; 4591 err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 4592 break; 4593 case SIOCDELRT: 4594 err = ip6_route_del(&cfg, NULL); 4595 break; 4596 } 4597 4598 return err; 4599 } 4600 4601 /* 4602 * Drop the packet on the floor 4603 */ 4604 4605 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 4606 { 4607 struct dst_entry *dst = skb_dst(skb); 4608 struct net_device *dev = dst_dev(dst); 4609 struct net *net = dev_net(dev); 4610 struct inet6_dev *idev; 4611 SKB_DR(reason); 4612 int type; 4613 4614 if (netif_is_l3_master(skb->dev) || 4615 dev == net->loopback_dev) 4616 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 4617 else 4618 idev = ip6_dst_idev(dst); 4619 4620 switch (ipstats_mib_noroutes) { 4621 case IPSTATS_MIB_INNOROUTES: 4622 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 4623 if (type == IPV6_ADDR_ANY) { 4624 SKB_DR_SET(reason, IP_INADDRERRORS); 4625 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 4626 break; 4627 } 4628 SKB_DR_SET(reason, IP_INNOROUTES); 4629 fallthrough; 4630 case IPSTATS_MIB_OUTNOROUTES: 4631 SKB_DR_OR(reason, IP_OUTNOROUTES); 4632 IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 4633 break; 4634 } 4635 4636 /* Start over by dropping the dst for l3mdev case */ 4637 if (netif_is_l3_master(skb->dev)) 4638 skb_dst_drop(skb); 4639 4640 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 4641 kfree_skb_reason(skb, reason); 4642 return 0; 4643 } 4644 4645 static int ip6_pkt_discard(struct sk_buff *skb) 4646 { 4647 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 4648 } 4649 4650 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 4651 { 4652 skb->dev = skb_dst_dev(skb); 4653 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 4654 } 4655 4656 static int ip6_pkt_prohibit(struct sk_buff *skb) 4657 { 4658 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 4659 } 4660 4661 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 4662 { 4663 skb->dev = skb_dst_dev(skb); 4664 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 4665 } 4666 4667 /* 4668 * Allocate a dst for local (unicast / anycast) address. 4669 */ 4670 4671 struct fib6_info *addrconf_f6i_alloc(struct net *net, 4672 struct inet6_dev *idev, 4673 const struct in6_addr *addr, 4674 bool anycast, gfp_t gfp_flags, 4675 struct netlink_ext_ack *extack) 4676 { 4677 struct fib6_config cfg = { 4678 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 4679 .fc_ifindex = idev->dev->ifindex, 4680 .fc_flags = RTF_UP | RTF_NONEXTHOP, 4681 .fc_dst = *addr, 4682 .fc_dst_len = 128, 4683 .fc_protocol = RTPROT_KERNEL, 4684 .fc_nlinfo.nl_net = net, 4685 .fc_ignore_dev_down = true, 4686 }; 4687 struct fib6_info *f6i; 4688 int err; 4689 4690 if (anycast) { 4691 cfg.fc_type = RTN_ANYCAST; 4692 cfg.fc_flags |= RTF_ANYCAST; 4693 } else { 4694 cfg.fc_type = RTN_LOCAL; 4695 cfg.fc_flags |= RTF_LOCAL; 4696 } 4697 4698 f6i = ip6_route_info_create(&cfg, gfp_flags, extack); 4699 if (IS_ERR(f6i)) 4700 return f6i; 4701 4702 err = ip6_route_info_create_nh(f6i, &cfg, gfp_flags, extack); 4703 if (err) 4704 return ERR_PTR(err); 4705 4706 f6i->dst_nocount = true; 4707 4708 if (!anycast && 4709 (READ_ONCE(net->ipv6.devconf_all->disable_policy) || 4710 READ_ONCE(idev->cnf.disable_policy))) 4711 f6i->dst_nopolicy = true; 4712 4713 return f6i; 4714 } 4715 4716 /* remove deleted ip from prefsrc entries */ 4717 struct arg_dev_net_ip { 4718 struct net *net; 4719 struct in6_addr *addr; 4720 }; 4721 4722 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 4723 { 4724 struct net *net = ((struct arg_dev_net_ip *)arg)->net; 4725 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 4726 4727 if (!rt->nh && 4728 rt != net->ipv6.fib6_null_entry && 4729 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) && 4730 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) { 4731 spin_lock_bh(&rt6_exception_lock); 4732 /* remove prefsrc entry */ 4733 rt->fib6_prefsrc.plen = 0; 4734 spin_unlock_bh(&rt6_exception_lock); 4735 } 4736 return 0; 4737 } 4738 4739 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 4740 { 4741 struct net *net = dev_net(ifp->idev->dev); 4742 struct arg_dev_net_ip adni = { 4743 .net = net, 4744 .addr = &ifp->addr, 4745 }; 4746 fib6_clean_all(net, fib6_remove_prefsrc, &adni); 4747 } 4748 4749 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 4750 4751 /* Remove routers and update dst entries when gateway turn into host. */ 4752 static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 4753 { 4754 struct in6_addr *gateway = (struct in6_addr *)arg; 4755 struct fib6_nh *nh; 4756 4757 /* RA routes do not use nexthops */ 4758 if (rt->nh) 4759 return 0; 4760 4761 nh = rt->fib6_nh; 4762 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 4763 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) 4764 return -1; 4765 4766 /* Further clean up cached routes in exception table. 4767 * This is needed because cached route may have a different 4768 * gateway than its 'parent' in the case of an ip redirect. 4769 */ 4770 fib6_nh_exceptions_clean_tohost(nh, gateway); 4771 4772 return 0; 4773 } 4774 4775 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 4776 { 4777 fib6_clean_all(net, fib6_clean_tohost, gateway); 4778 } 4779 4780 struct arg_netdev_event { 4781 const struct net_device *dev; 4782 union { 4783 unsigned char nh_flags; 4784 unsigned long event; 4785 }; 4786 }; 4787 4788 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 4789 { 4790 struct fib6_info *iter; 4791 struct fib6_node *fn; 4792 4793 fn = rcu_dereference_protected(rt->fib6_node, 4794 lockdep_is_held(&rt->fib6_table->tb6_lock)); 4795 iter = rcu_dereference_protected(fn->leaf, 4796 lockdep_is_held(&rt->fib6_table->tb6_lock)); 4797 while (iter) { 4798 if (iter->fib6_metric == rt->fib6_metric && 4799 rt6_qualify_for_ecmp(iter)) 4800 return iter; 4801 iter = rcu_dereference_protected(iter->fib6_next, 4802 lockdep_is_held(&rt->fib6_table->tb6_lock)); 4803 } 4804 4805 return NULL; 4806 } 4807 4808 /* only called for fib entries with builtin fib6_nh */ 4809 static bool rt6_is_dead(const struct fib6_info *rt) 4810 { 4811 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || 4812 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && 4813 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) 4814 return true; 4815 4816 return false; 4817 } 4818 4819 static int rt6_multipath_total_weight(const struct fib6_info *rt) 4820 { 4821 struct fib6_info *iter; 4822 int total = 0; 4823 4824 if (!rt6_is_dead(rt)) 4825 total += rt->fib6_nh->fib_nh_weight; 4826 4827 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4828 if (!rt6_is_dead(iter)) 4829 total += iter->fib6_nh->fib_nh_weight; 4830 } 4831 4832 return total; 4833 } 4834 4835 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4836 { 4837 int upper_bound = -1; 4838 4839 if (!rt6_is_dead(rt)) { 4840 *weight += rt->fib6_nh->fib_nh_weight; 4841 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4842 total) - 1; 4843 } 4844 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); 4845 } 4846 4847 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4848 { 4849 struct fib6_info *iter; 4850 int weight = 0; 4851 4852 rt6_upper_bound_set(rt, &weight, total); 4853 4854 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4855 rt6_upper_bound_set(iter, &weight, total); 4856 } 4857 4858 void rt6_multipath_rebalance(struct fib6_info *rt) 4859 { 4860 struct fib6_info *first; 4861 int total; 4862 4863 /* In case the entire multipath route was marked for flushing, 4864 * then there is no need to rebalance upon the removal of every 4865 * sibling route. 4866 */ 4867 if (!rt->fib6_nsiblings || rt->should_flush) 4868 return; 4869 4870 /* During lookup routes are evaluated in order, so we need to 4871 * make sure upper bounds are assigned from the first sibling 4872 * onwards. 4873 */ 4874 first = rt6_multipath_first_sibling(rt); 4875 if (WARN_ON_ONCE(!first)) 4876 return; 4877 4878 total = rt6_multipath_total_weight(first); 4879 rt6_multipath_upper_bound_set(first, total); 4880 } 4881 4882 static int fib6_ifup(struct fib6_info *rt, void *p_arg) 4883 { 4884 const struct arg_netdev_event *arg = p_arg; 4885 struct net *net = dev_net(arg->dev); 4886 4887 if (rt != net->ipv6.fib6_null_entry && !rt->nh && 4888 rt->fib6_nh->fib_nh_dev == arg->dev) { 4889 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; 4890 fib6_update_sernum_upto_root(net, rt); 4891 rt6_multipath_rebalance(rt); 4892 } 4893 4894 return 0; 4895 } 4896 4897 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 4898 { 4899 struct arg_netdev_event arg = { 4900 .dev = dev, 4901 { 4902 .nh_flags = nh_flags, 4903 }, 4904 }; 4905 4906 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 4907 arg.nh_flags |= RTNH_F_LINKDOWN; 4908 4909 fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 4910 } 4911 4912 /* only called for fib entries with inline fib6_nh */ 4913 static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 4914 const struct net_device *dev) 4915 { 4916 struct fib6_info *iter; 4917 4918 if (rt->fib6_nh->fib_nh_dev == dev) 4919 return true; 4920 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4921 if (iter->fib6_nh->fib_nh_dev == dev) 4922 return true; 4923 4924 return false; 4925 } 4926 4927 static void rt6_multipath_flush(struct fib6_info *rt) 4928 { 4929 struct fib6_info *iter; 4930 4931 rt->should_flush = 1; 4932 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4933 iter->should_flush = 1; 4934 } 4935 4936 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 4937 const struct net_device *down_dev) 4938 { 4939 struct fib6_info *iter; 4940 unsigned int dead = 0; 4941 4942 if (rt->fib6_nh->fib_nh_dev == down_dev || 4943 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 4944 dead++; 4945 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4946 if (iter->fib6_nh->fib_nh_dev == down_dev || 4947 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 4948 dead++; 4949 4950 return dead; 4951 } 4952 4953 static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 4954 const struct net_device *dev, 4955 unsigned char nh_flags) 4956 { 4957 struct fib6_info *iter; 4958 4959 if (rt->fib6_nh->fib_nh_dev == dev) 4960 rt->fib6_nh->fib_nh_flags |= nh_flags; 4961 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4962 if (iter->fib6_nh->fib_nh_dev == dev) 4963 iter->fib6_nh->fib_nh_flags |= nh_flags; 4964 } 4965 4966 /* called with write lock held for table with rt */ 4967 static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 4968 { 4969 const struct arg_netdev_event *arg = p_arg; 4970 const struct net_device *dev = arg->dev; 4971 struct net *net = dev_net(dev); 4972 4973 if (rt == net->ipv6.fib6_null_entry || rt->nh) 4974 return 0; 4975 4976 switch (arg->event) { 4977 case NETDEV_UNREGISTER: 4978 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 4979 case NETDEV_DOWN: 4980 if (rt->should_flush) 4981 return -1; 4982 if (!rt->fib6_nsiblings) 4983 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 4984 if (rt6_multipath_uses_dev(rt, dev)) { 4985 unsigned int count; 4986 4987 count = rt6_multipath_dead_count(rt, dev); 4988 if (rt->fib6_nsiblings + 1 == count) { 4989 rt6_multipath_flush(rt); 4990 return -1; 4991 } 4992 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 4993 RTNH_F_LINKDOWN); 4994 fib6_update_sernum(net, rt); 4995 rt6_multipath_rebalance(rt); 4996 } 4997 return -2; 4998 case NETDEV_CHANGE: 4999 if (rt->fib6_nh->fib_nh_dev != dev || 5000 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 5001 break; 5002 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 5003 fib6_update_sernum(net, rt); 5004 rt6_multipath_rebalance(rt); 5005 break; 5006 } 5007 5008 return 0; 5009 } 5010 5011 void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 5012 { 5013 struct arg_netdev_event arg = { 5014 .dev = dev, 5015 { 5016 .event = event, 5017 }, 5018 }; 5019 struct net *net = dev_net(dev); 5020 5021 if (READ_ONCE(net->ipv6.sysctl.skip_notify_on_dev_down)) 5022 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 5023 else 5024 fib6_clean_all(net, fib6_ifdown, &arg); 5025 } 5026 5027 void rt6_disable_ip(struct net_device *dev, unsigned long event) 5028 { 5029 rt6_sync_down_dev(dev, event); 5030 rt6_uncached_list_flush_dev(dev); 5031 neigh_ifdown(&nd_tbl, dev); 5032 } 5033 5034 struct rt6_mtu_change_arg { 5035 struct net_device *dev; 5036 unsigned int mtu; 5037 struct fib6_info *f6i; 5038 }; 5039 5040 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) 5041 { 5042 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; 5043 struct fib6_info *f6i = arg->f6i; 5044 5045 /* For administrative MTU increase, there is no way to discover 5046 * IPv6 PMTU increase, so PMTU increase should be updated here. 5047 * Since RFC 1981 doesn't include administrative MTU increase 5048 * update PMTU increase is a MUST. (i.e. jumbo frame) 5049 */ 5050 if (nh->fib_nh_dev == arg->dev) { 5051 struct inet6_dev *idev = __in6_dev_get(arg->dev); 5052 u32 mtu = f6i->fib6_pmtu; 5053 5054 if (mtu >= arg->mtu || 5055 (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 5056 fib6_metric_set(f6i, RTAX_MTU, arg->mtu); 5057 5058 spin_lock_bh(&rt6_exception_lock); 5059 rt6_exceptions_update_pmtu(idev, nh, arg->mtu); 5060 spin_unlock_bh(&rt6_exception_lock); 5061 } 5062 5063 return 0; 5064 } 5065 5066 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) 5067 { 5068 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 5069 struct inet6_dev *idev; 5070 5071 /* In IPv6 pmtu discovery is not optional, 5072 so that RTAX_MTU lock cannot disable it. 5073 We still use this lock to block changes 5074 caused by addrconf/ndisc. 5075 */ 5076 5077 idev = __in6_dev_get(arg->dev); 5078 if (!idev) 5079 return 0; 5080 5081 if (fib6_metric_locked(f6i, RTAX_MTU)) 5082 return 0; 5083 5084 arg->f6i = f6i; 5085 if (f6i->nh) { 5086 /* fib6_nh_mtu_change only returns 0, so this is safe */ 5087 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, 5088 arg); 5089 } 5090 5091 return fib6_nh_mtu_change(f6i->fib6_nh, arg); 5092 } 5093 5094 void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 5095 { 5096 struct rt6_mtu_change_arg arg = { 5097 .dev = dev, 5098 .mtu = mtu, 5099 }; 5100 5101 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 5102 } 5103 5104 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 5105 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 5106 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 5107 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 5108 [RTA_OIF] = { .type = NLA_U32 }, 5109 [RTA_IIF] = { .type = NLA_U32 }, 5110 [RTA_PRIORITY] = { .type = NLA_U32 }, 5111 [RTA_METRICS] = { .type = NLA_NESTED }, 5112 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 5113 [RTA_PREF] = { .type = NLA_U8 }, 5114 [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 5115 [RTA_ENCAP] = { .type = NLA_NESTED }, 5116 [RTA_EXPIRES] = { .type = NLA_U32 }, 5117 [RTA_UID] = { .type = NLA_U32 }, 5118 [RTA_MARK] = { .type = NLA_U32 }, 5119 [RTA_TABLE] = { .type = NLA_U32 }, 5120 [RTA_IP_PROTO] = { .type = NLA_U8 }, 5121 [RTA_SPORT] = { .type = NLA_U16 }, 5122 [RTA_DPORT] = { .type = NLA_U16 }, 5123 [RTA_NH_ID] = { .type = NLA_U32 }, 5124 [RTA_FLOWLABEL] = { .type = NLA_BE32 }, 5125 }; 5126 5127 static int rtm_to_fib6_multipath_config(struct fib6_config *cfg, 5128 struct netlink_ext_ack *extack, 5129 bool newroute) 5130 { 5131 struct rtnexthop *rtnh; 5132 int remaining; 5133 5134 remaining = cfg->fc_mp_len; 5135 rtnh = (struct rtnexthop *)cfg->fc_mp; 5136 5137 if (!rtnh_ok(rtnh, remaining)) { 5138 NL_SET_ERR_MSG(extack, "Invalid nexthop configuration - no valid nexthops"); 5139 return -EINVAL; 5140 } 5141 5142 do { 5143 bool has_gateway = cfg->fc_flags & RTF_GATEWAY; 5144 int attrlen = rtnh_attrlen(rtnh); 5145 5146 if (attrlen > 0) { 5147 struct nlattr *nla, *attrs; 5148 5149 attrs = rtnh_attrs(rtnh); 5150 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 5151 if (nla) { 5152 if (nla_len(nla) < sizeof(cfg->fc_gateway)) { 5153 NL_SET_ERR_MSG(extack, 5154 "Invalid IPv6 address in RTA_GATEWAY"); 5155 return -EINVAL; 5156 } 5157 5158 has_gateway = true; 5159 } 5160 } 5161 5162 if (newroute && (cfg->fc_nh_id || !has_gateway)) { 5163 NL_SET_ERR_MSG(extack, 5164 "Device only routes can not be added for IPv6 using the multipath API."); 5165 return -EINVAL; 5166 } 5167 5168 rtnh = rtnh_next(rtnh, &remaining); 5169 } while (rtnh_ok(rtnh, remaining)); 5170 5171 return lwtunnel_valid_encap_type_attr(cfg->fc_mp, cfg->fc_mp_len, extack); 5172 } 5173 5174 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 5175 struct fib6_config *cfg, 5176 struct netlink_ext_ack *extack) 5177 { 5178 bool newroute = nlh->nlmsg_type == RTM_NEWROUTE; 5179 struct nlattr *tb[RTA_MAX+1]; 5180 struct rtmsg *rtm; 5181 unsigned int pref; 5182 int err; 5183 5184 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 5185 rtm_ipv6_policy, extack); 5186 if (err < 0) 5187 goto errout; 5188 5189 err = -EINVAL; 5190 rtm = nlmsg_data(nlh); 5191 5192 if (rtm->rtm_tos) { 5193 NL_SET_ERR_MSG(extack, 5194 "Invalid dsfield (tos): option not available for IPv6"); 5195 goto errout; 5196 } 5197 5198 if (tb[RTA_FLOWLABEL]) { 5199 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL], 5200 "Flow label cannot be specified for this operation"); 5201 goto errout; 5202 } 5203 5204 *cfg = (struct fib6_config){ 5205 .fc_table = rtm->rtm_table, 5206 .fc_dst_len = rtm->rtm_dst_len, 5207 .fc_src_len = rtm->rtm_src_len, 5208 .fc_flags = RTF_UP, 5209 .fc_protocol = rtm->rtm_protocol, 5210 .fc_type = rtm->rtm_type, 5211 5212 .fc_nlinfo.portid = NETLINK_CB(skb).portid, 5213 .fc_nlinfo.nlh = nlh, 5214 .fc_nlinfo.nl_net = sock_net(skb->sk), 5215 }; 5216 5217 if (rtm->rtm_type == RTN_UNREACHABLE || 5218 rtm->rtm_type == RTN_BLACKHOLE || 5219 rtm->rtm_type == RTN_PROHIBIT || 5220 rtm->rtm_type == RTN_THROW) 5221 cfg->fc_flags |= RTF_REJECT; 5222 5223 if (rtm->rtm_type == RTN_LOCAL) 5224 cfg->fc_flags |= RTF_LOCAL; 5225 5226 if (rtm->rtm_flags & RTM_F_CLONED) 5227 cfg->fc_flags |= RTF_CACHE; 5228 5229 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 5230 5231 if (tb[RTA_NH_ID]) { 5232 if (tb[RTA_GATEWAY] || tb[RTA_OIF] || 5233 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { 5234 NL_SET_ERR_MSG(extack, 5235 "Nexthop specification and nexthop id are mutually exclusive"); 5236 goto errout; 5237 } 5238 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); 5239 } 5240 5241 if (tb[RTA_GATEWAY]) { 5242 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 5243 cfg->fc_flags |= RTF_GATEWAY; 5244 } 5245 if (tb[RTA_VIA]) { 5246 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 5247 goto errout; 5248 } 5249 5250 if (tb[RTA_DST]) { 5251 int plen = (rtm->rtm_dst_len + 7) >> 3; 5252 5253 if (nla_len(tb[RTA_DST]) < plen) 5254 goto errout; 5255 5256 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 5257 } 5258 5259 if (tb[RTA_SRC]) { 5260 int plen = (rtm->rtm_src_len + 7) >> 3; 5261 5262 if (nla_len(tb[RTA_SRC]) < plen) 5263 goto errout; 5264 5265 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 5266 } 5267 5268 if (tb[RTA_PREFSRC]) 5269 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 5270 5271 if (tb[RTA_OIF]) 5272 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 5273 5274 if (tb[RTA_PRIORITY]) 5275 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 5276 5277 if (tb[RTA_METRICS]) { 5278 cfg->fc_mx = nla_data(tb[RTA_METRICS]); 5279 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 5280 } 5281 5282 if (tb[RTA_TABLE]) 5283 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 5284 5285 if (tb[RTA_MULTIPATH]) { 5286 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 5287 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 5288 5289 err = rtm_to_fib6_multipath_config(cfg, extack, newroute); 5290 if (err < 0) 5291 goto errout; 5292 } 5293 5294 if (tb[RTA_PREF]) { 5295 pref = nla_get_u8(tb[RTA_PREF]); 5296 if (pref != ICMPV6_ROUTER_PREF_LOW && 5297 pref != ICMPV6_ROUTER_PREF_HIGH) 5298 pref = ICMPV6_ROUTER_PREF_MEDIUM; 5299 cfg->fc_flags |= RTF_PREF(pref); 5300 } 5301 5302 if (tb[RTA_ENCAP]) 5303 cfg->fc_encap = tb[RTA_ENCAP]; 5304 5305 if (tb[RTA_ENCAP_TYPE]) { 5306 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 5307 5308 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 5309 if (err < 0) 5310 goto errout; 5311 } 5312 5313 if (tb[RTA_EXPIRES]) { 5314 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 5315 5316 if (addrconf_finite_timeout(timeout)) { 5317 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 5318 cfg->fc_flags |= RTF_EXPIRES; 5319 } 5320 } 5321 5322 err = 0; 5323 errout: 5324 return err; 5325 } 5326 5327 struct rt6_nh { 5328 struct fib6_info *fib6_info; 5329 struct fib6_config r_cfg; 5330 struct list_head list; 5331 }; 5332 5333 static int ip6_route_info_append(struct list_head *rt6_nh_list, 5334 struct fib6_info *rt, 5335 struct fib6_config *r_cfg) 5336 { 5337 struct rt6_nh *nh; 5338 5339 list_for_each_entry(nh, rt6_nh_list, list) { 5340 /* check if fib6_info already exists */ 5341 if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 5342 return -EEXIST; 5343 } 5344 5345 nh = kzalloc_obj(*nh); 5346 if (!nh) 5347 return -ENOMEM; 5348 5349 nh->fib6_info = rt; 5350 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 5351 list_add_tail(&nh->list, rt6_nh_list); 5352 5353 return 0; 5354 } 5355 5356 static void ip6_route_mpath_notify(struct fib6_info *rt, 5357 struct fib6_info *rt_last, 5358 struct nl_info *info, 5359 __u16 nlflags) 5360 { 5361 /* if this is an APPEND route, then rt points to the first route 5362 * inserted and rt_last points to last route inserted. Userspace 5363 * wants a consistent dump of the route which starts at the first 5364 * nexthop. Since sibling routes are always added at the end of 5365 * the list, find the first sibling of the last route appended 5366 */ 5367 rcu_read_lock(); 5368 5369 if ((nlflags & NLM_F_APPEND) && rt_last && 5370 READ_ONCE(rt_last->fib6_nsiblings)) { 5371 rt = list_first_or_null_rcu(&rt_last->fib6_siblings, 5372 struct fib6_info, 5373 fib6_siblings); 5374 } 5375 5376 if (rt) 5377 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 5378 5379 rcu_read_unlock(); 5380 } 5381 5382 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) 5383 { 5384 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); 5385 bool should_notify = false; 5386 struct fib6_info *leaf; 5387 struct fib6_node *fn; 5388 5389 rcu_read_lock(); 5390 fn = rcu_dereference(rt->fib6_node); 5391 if (!fn) 5392 goto out; 5393 5394 leaf = rcu_dereference(fn->leaf); 5395 if (!leaf) 5396 goto out; 5397 5398 if (rt == leaf || 5399 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && 5400 rt6_qualify_for_ecmp(leaf))) 5401 should_notify = true; 5402 out: 5403 rcu_read_unlock(); 5404 5405 return should_notify; 5406 } 5407 5408 static int ip6_route_multipath_add(struct fib6_config *cfg, 5409 struct netlink_ext_ack *extack) 5410 { 5411 struct fib6_info *rt_notif = NULL, *rt_last = NULL; 5412 struct nl_info *info = &cfg->fc_nlinfo; 5413 struct rt6_nh *nh, *nh_safe; 5414 struct fib6_config r_cfg; 5415 struct rtnexthop *rtnh; 5416 LIST_HEAD(rt6_nh_list); 5417 struct rt6_nh *err_nh; 5418 struct fib6_info *rt; 5419 __u16 nlflags; 5420 int remaining; 5421 int attrlen; 5422 int replace; 5423 int nhn = 0; 5424 int err; 5425 5426 err = fib6_config_validate(cfg, extack); 5427 if (err) 5428 return err; 5429 5430 replace = (cfg->fc_nlinfo.nlh && 5431 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 5432 5433 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 5434 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 5435 nlflags |= NLM_F_APPEND; 5436 5437 remaining = cfg->fc_mp_len; 5438 rtnh = (struct rtnexthop *)cfg->fc_mp; 5439 5440 /* Parse a Multipath Entry and build a list (rt6_nh_list) of 5441 * fib6_info structs per nexthop 5442 */ 5443 while (rtnh_ok(rtnh, remaining)) { 5444 memcpy(&r_cfg, cfg, sizeof(*cfg)); 5445 if (rtnh->rtnh_ifindex) 5446 r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 5447 5448 attrlen = rtnh_attrlen(rtnh); 5449 if (attrlen > 0) { 5450 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 5451 5452 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 5453 if (nla) { 5454 r_cfg.fc_gateway = nla_get_in6_addr(nla); 5455 r_cfg.fc_flags |= RTF_GATEWAY; 5456 } 5457 5458 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 5459 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 5460 if (nla) 5461 r_cfg.fc_encap_type = nla_get_u16(nla); 5462 } 5463 5464 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 5465 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 5466 if (IS_ERR(rt)) { 5467 err = PTR_ERR(rt); 5468 rt = NULL; 5469 goto cleanup; 5470 } 5471 5472 err = ip6_route_info_create_nh(rt, &r_cfg, GFP_KERNEL, extack); 5473 if (err) { 5474 rt = NULL; 5475 goto cleanup; 5476 } 5477 5478 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; 5479 5480 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 5481 if (err) { 5482 fib6_info_release(rt); 5483 goto cleanup; 5484 } 5485 5486 rtnh = rtnh_next(rtnh, &remaining); 5487 } 5488 5489 /* for add and replace send one notification with all nexthops. 5490 * Skip the notification in fib6_add_rt2node and send one with 5491 * the full route when done 5492 */ 5493 info->skip_notify = 1; 5494 5495 /* For add and replace, send one notification with all nexthops. For 5496 * append, send one notification with all appended nexthops. 5497 */ 5498 info->skip_notify_kernel = 1; 5499 5500 err_nh = NULL; 5501 list_for_each_entry(nh, &rt6_nh_list, list) { 5502 err = __ip6_ins_rt(nh->fib6_info, info, extack); 5503 5504 if (err) { 5505 if (replace && nhn) 5506 NL_SET_ERR_MSG_MOD(extack, 5507 "multipath route replace failed (check consistency of installed routes)"); 5508 err_nh = nh; 5509 goto add_errout; 5510 } 5511 /* save reference to last route successfully inserted */ 5512 rt_last = nh->fib6_info; 5513 5514 /* save reference to first route for notification */ 5515 if (!rt_notif) 5516 rt_notif = nh->fib6_info; 5517 5518 /* Because each route is added like a single route we remove 5519 * these flags after the first nexthop: if there is a collision, 5520 * we have already failed to add the first nexthop: 5521 * fib6_add_rt2node() has rejected it; when replacing, old 5522 * nexthops have been replaced by first new, the rest should 5523 * be added to it. 5524 */ 5525 if (cfg->fc_nlinfo.nlh) { 5526 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 5527 NLM_F_REPLACE); 5528 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; 5529 } 5530 nhn++; 5531 } 5532 5533 /* An in-kernel notification should only be sent in case the new 5534 * multipath route is added as the first route in the node, or if 5535 * it was appended to it. We pass 'rt_notif' since it is the first 5536 * sibling and might allow us to skip some checks in the replace case. 5537 */ 5538 if (ip6_route_mpath_should_notify(rt_notif)) { 5539 enum fib_event_type fib_event; 5540 5541 if (rt_notif->fib6_nsiblings != nhn - 1) 5542 fib_event = FIB_EVENT_ENTRY_APPEND; 5543 else 5544 fib_event = FIB_EVENT_ENTRY_REPLACE; 5545 5546 err = call_fib6_multipath_entry_notifiers(info->nl_net, 5547 fib_event, rt_notif, 5548 nhn - 1, extack); 5549 if (err) { 5550 /* Delete all the siblings that were just added */ 5551 err_nh = NULL; 5552 goto add_errout; 5553 } 5554 } 5555 5556 /* success ... tell user about new route */ 5557 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 5558 goto cleanup; 5559 5560 add_errout: 5561 /* send notification for routes that were added so that 5562 * the delete notifications sent by ip6_route_del are 5563 * coherent 5564 */ 5565 if (rt_notif) 5566 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 5567 5568 /* Delete routes that were already added */ 5569 list_for_each_entry(nh, &rt6_nh_list, list) { 5570 if (err_nh == nh) 5571 break; 5572 ip6_route_del(&nh->r_cfg, extack); 5573 } 5574 5575 cleanup: 5576 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, list) { 5577 fib6_info_release(nh->fib6_info); 5578 list_del(&nh->list); 5579 kfree(nh); 5580 } 5581 5582 return err; 5583 } 5584 5585 static int ip6_route_multipath_del(struct fib6_config *cfg, 5586 struct netlink_ext_ack *extack) 5587 { 5588 struct fib6_config r_cfg; 5589 struct rtnexthop *rtnh; 5590 int last_err = 0; 5591 int remaining; 5592 int attrlen; 5593 int err; 5594 5595 remaining = cfg->fc_mp_len; 5596 rtnh = (struct rtnexthop *)cfg->fc_mp; 5597 5598 /* Parse a Multipath Entry */ 5599 while (rtnh_ok(rtnh, remaining)) { 5600 memcpy(&r_cfg, cfg, sizeof(*cfg)); 5601 if (rtnh->rtnh_ifindex) 5602 r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 5603 5604 attrlen = rtnh_attrlen(rtnh); 5605 if (attrlen > 0) { 5606 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 5607 5608 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 5609 if (nla) { 5610 r_cfg.fc_gateway = nla_get_in6_addr(nla); 5611 r_cfg.fc_flags |= RTF_GATEWAY; 5612 } 5613 } 5614 5615 err = ip6_route_del(&r_cfg, extack); 5616 if (err) 5617 last_err = err; 5618 5619 rtnh = rtnh_next(rtnh, &remaining); 5620 } 5621 5622 return last_err; 5623 } 5624 5625 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5626 struct netlink_ext_ack *extack) 5627 { 5628 struct fib6_config cfg; 5629 int err; 5630 5631 err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 5632 if (err < 0) 5633 return err; 5634 5635 if (cfg.fc_nh_id) { 5636 rcu_read_lock(); 5637 err = !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id); 5638 rcu_read_unlock(); 5639 5640 if (err) { 5641 NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 5642 return -EINVAL; 5643 } 5644 } 5645 5646 if (cfg.fc_mp) { 5647 return ip6_route_multipath_del(&cfg, extack); 5648 } else { 5649 cfg.fc_delete_all_nh = 1; 5650 return ip6_route_del(&cfg, extack); 5651 } 5652 } 5653 5654 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5655 struct netlink_ext_ack *extack) 5656 { 5657 struct fib6_config cfg; 5658 int err; 5659 5660 err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 5661 if (err < 0) 5662 return err; 5663 5664 if (cfg.fc_metric == 0) 5665 cfg.fc_metric = IP6_RT_PRIO_USER; 5666 5667 if (cfg.fc_mp) 5668 return ip6_route_multipath_add(&cfg, extack); 5669 else 5670 return ip6_route_add(&cfg, GFP_KERNEL, extack); 5671 } 5672 5673 /* add the overhead of this fib6_nh to nexthop_len */ 5674 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) 5675 { 5676 int *nexthop_len = arg; 5677 5678 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ 5679 + NLA_ALIGN(sizeof(struct rtnexthop)) 5680 + nla_total_size(16); /* RTA_GATEWAY */ 5681 5682 if (nh->fib_nh_lws) { 5683 /* RTA_ENCAP_TYPE */ 5684 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5685 /* RTA_ENCAP */ 5686 *nexthop_len += nla_total_size(2); 5687 } 5688 5689 return 0; 5690 } 5691 5692 static size_t rt6_nlmsg_size(struct fib6_info *f6i) 5693 { 5694 struct fib6_info *sibling; 5695 struct fib6_nh *nh; 5696 int nexthop_len; 5697 5698 if (f6i->nh) { 5699 nexthop_len = nla_total_size(4); /* RTA_NH_ID */ 5700 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, 5701 &nexthop_len); 5702 goto common; 5703 } 5704 5705 rcu_read_lock(); 5706 retry: 5707 nh = f6i->fib6_nh; 5708 nexthop_len = 0; 5709 if (READ_ONCE(f6i->fib6_nsiblings)) { 5710 rt6_nh_nlmsg_size(nh, &nexthop_len); 5711 5712 list_for_each_entry_rcu(sibling, &f6i->fib6_siblings, 5713 fib6_siblings) { 5714 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len); 5715 if (!READ_ONCE(f6i->fib6_nsiblings)) 5716 goto retry; 5717 } 5718 } 5719 rcu_read_unlock(); 5720 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5721 common: 5722 return NLMSG_ALIGN(sizeof(struct rtmsg)) 5723 + nla_total_size(16) /* RTA_SRC */ 5724 + nla_total_size(16) /* RTA_DST */ 5725 + nla_total_size(16) /* RTA_GATEWAY */ 5726 + nla_total_size(16) /* RTA_PREFSRC */ 5727 + nla_total_size(4) /* RTA_TABLE */ 5728 + nla_total_size(4) /* RTA_IIF */ 5729 + nla_total_size(4) /* RTA_OIF */ 5730 + nla_total_size(4) /* RTA_PRIORITY */ 5731 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 5732 + nla_total_size(sizeof(struct rta_cacheinfo)) 5733 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 5734 + nla_total_size(1) /* RTA_PREF */ 5735 + nexthop_len; 5736 } 5737 5738 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, 5739 unsigned char *flags) 5740 { 5741 if (nexthop_is_multipath(nh)) { 5742 struct nlattr *mp; 5743 5744 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5745 if (!mp) 5746 goto nla_put_failure; 5747 5748 if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) 5749 goto nla_put_failure; 5750 5751 nla_nest_end(skb, mp); 5752 } else { 5753 struct fib6_nh *fib6_nh; 5754 5755 fib6_nh = nexthop_fib6_nh(nh); 5756 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, 5757 flags, false) < 0) 5758 goto nla_put_failure; 5759 } 5760 5761 return 0; 5762 5763 nla_put_failure: 5764 return -EMSGSIZE; 5765 } 5766 5767 static int rt6_fill_node(struct net *net, struct sk_buff *skb, 5768 struct fib6_info *rt, struct dst_entry *dst, 5769 struct in6_addr *dest, struct in6_addr *src, 5770 int iif, int type, u32 portid, u32 seq, 5771 unsigned int flags) 5772 { 5773 struct rt6_info *rt6 = dst_rt6_info(dst); 5774 struct rt6key *rt6_dst, *rt6_src; 5775 u32 *pmetrics, table, rt6_flags; 5776 unsigned char nh_flags = 0; 5777 struct nlmsghdr *nlh; 5778 struct rtmsg *rtm; 5779 long expires = 0; 5780 5781 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 5782 if (!nlh) 5783 return -EMSGSIZE; 5784 5785 if (rt6) { 5786 rt6_dst = &rt6->rt6i_dst; 5787 rt6_src = &rt6->rt6i_src; 5788 rt6_flags = rt6->rt6i_flags; 5789 } else { 5790 rt6_dst = &rt->fib6_dst; 5791 rt6_src = &rt->fib6_src; 5792 rt6_flags = rt->fib6_flags; 5793 } 5794 5795 rtm = nlmsg_data(nlh); 5796 rtm->rtm_family = AF_INET6; 5797 rtm->rtm_dst_len = rt6_dst->plen; 5798 rtm->rtm_src_len = rt6_src->plen; 5799 rtm->rtm_tos = 0; 5800 if (rt->fib6_table) 5801 table = rt->fib6_table->tb6_id; 5802 else 5803 table = RT6_TABLE_UNSPEC; 5804 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 5805 if (nla_put_u32(skb, RTA_TABLE, table)) 5806 goto nla_put_failure; 5807 5808 rtm->rtm_type = rt->fib6_type; 5809 rtm->rtm_flags = 0; 5810 rtm->rtm_scope = RT_SCOPE_UNIVERSE; 5811 rtm->rtm_protocol = rt->fib6_protocol; 5812 5813 if (rt6_flags & RTF_CACHE) 5814 rtm->rtm_flags |= RTM_F_CLONED; 5815 5816 if (dest) { 5817 if (nla_put_in6_addr(skb, RTA_DST, dest)) 5818 goto nla_put_failure; 5819 rtm->rtm_dst_len = 128; 5820 } else if (rtm->rtm_dst_len) 5821 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 5822 goto nla_put_failure; 5823 #ifdef CONFIG_IPV6_SUBTREES 5824 if (src) { 5825 if (nla_put_in6_addr(skb, RTA_SRC, src)) 5826 goto nla_put_failure; 5827 rtm->rtm_src_len = 128; 5828 } else if (rtm->rtm_src_len && 5829 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 5830 goto nla_put_failure; 5831 #endif 5832 if (iif) { 5833 #ifdef CONFIG_IPV6_MROUTE 5834 if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 5835 int err = ip6mr_get_route(net, skb, rtm, portid); 5836 5837 if (err == 0) 5838 return 0; 5839 if (err < 0) 5840 goto nla_put_failure; 5841 } else 5842 #endif 5843 if (nla_put_u32(skb, RTA_IIF, iif)) 5844 goto nla_put_failure; 5845 } else if (dest) { 5846 struct in6_addr saddr_buf; 5847 if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 && 5848 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5849 goto nla_put_failure; 5850 } 5851 5852 if (rt->fib6_prefsrc.plen) { 5853 struct in6_addr saddr_buf; 5854 saddr_buf = rt->fib6_prefsrc.addr; 5855 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5856 goto nla_put_failure; 5857 } 5858 5859 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 5860 if (rtnetlink_put_metrics(skb, pmetrics) < 0) 5861 goto nla_put_failure; 5862 5863 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 5864 goto nla_put_failure; 5865 5866 /* For multipath routes, walk the siblings list and add 5867 * each as a nexthop within RTA_MULTIPATH. 5868 */ 5869 if (rt6) { 5870 struct net_device *dev; 5871 5872 if (rt6_flags & RTF_GATEWAY && 5873 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 5874 goto nla_put_failure; 5875 5876 dev = dst_dev(dst); 5877 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex)) 5878 goto nla_put_failure; 5879 5880 if (lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0) 5881 goto nla_put_failure; 5882 } else if (READ_ONCE(rt->fib6_nsiblings)) { 5883 struct fib6_info *sibling; 5884 struct nlattr *mp; 5885 5886 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5887 if (!mp) 5888 goto nla_put_failure; 5889 5890 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, 5891 rt->fib6_nh->fib_nh_weight, AF_INET6, 5892 0) < 0) 5893 goto nla_put_failure; 5894 5895 rcu_read_lock(); 5896 5897 list_for_each_entry_rcu(sibling, &rt->fib6_siblings, 5898 fib6_siblings) { 5899 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, 5900 sibling->fib6_nh->fib_nh_weight, 5901 AF_INET6, 0) < 0) { 5902 rcu_read_unlock(); 5903 5904 goto nla_put_failure; 5905 } 5906 if (!READ_ONCE(rt->fib6_nsiblings)) 5907 break; 5908 } 5909 5910 rcu_read_unlock(); 5911 5912 nla_nest_end(skb, mp); 5913 } else if (rt->nh) { 5914 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) 5915 goto nla_put_failure; 5916 5917 if (nexthop_is_blackhole(rt->nh)) 5918 rtm->rtm_type = RTN_BLACKHOLE; 5919 5920 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) && 5921 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) 5922 goto nla_put_failure; 5923 5924 rtm->rtm_flags |= nh_flags; 5925 } else { 5926 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, 5927 &nh_flags, false) < 0) 5928 goto nla_put_failure; 5929 5930 rtm->rtm_flags |= nh_flags; 5931 } 5932 5933 if (rt6_flags & RTF_EXPIRES) { 5934 expires = dst ? READ_ONCE(dst->expires) : rt->expires; 5935 expires -= jiffies; 5936 } 5937 5938 if (!dst) { 5939 if (READ_ONCE(rt->offload)) 5940 rtm->rtm_flags |= RTM_F_OFFLOAD; 5941 if (READ_ONCE(rt->trap)) 5942 rtm->rtm_flags |= RTM_F_TRAP; 5943 if (READ_ONCE(rt->offload_failed)) 5944 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED; 5945 } 5946 5947 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 5948 goto nla_put_failure; 5949 5950 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 5951 goto nla_put_failure; 5952 5953 5954 nlmsg_end(skb, nlh); 5955 return 0; 5956 5957 nla_put_failure: 5958 nlmsg_cancel(skb, nlh); 5959 return -EMSGSIZE; 5960 } 5961 5962 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) 5963 { 5964 const struct net_device *dev = arg; 5965 5966 if (nh->fib_nh_dev == dev) 5967 return 1; 5968 5969 return 0; 5970 } 5971 5972 static bool fib6_info_uses_dev(const struct fib6_info *f6i, 5973 const struct net_device *dev) 5974 { 5975 if (f6i->nh) { 5976 struct net_device *_dev = (struct net_device *)dev; 5977 5978 return !!nexthop_for_each_fib6_nh(f6i->nh, 5979 fib6_info_nh_uses_dev, 5980 _dev); 5981 } 5982 5983 if (f6i->fib6_nh->fib_nh_dev == dev) 5984 return true; 5985 5986 if (READ_ONCE(f6i->fib6_nsiblings)) { 5987 const struct fib6_info *sibling; 5988 5989 rcu_read_lock(); 5990 list_for_each_entry_rcu(sibling, &f6i->fib6_siblings, 5991 fib6_siblings) { 5992 if (sibling->fib6_nh->fib_nh_dev == dev) { 5993 rcu_read_unlock(); 5994 return true; 5995 } 5996 if (!READ_ONCE(f6i->fib6_nsiblings)) 5997 break; 5998 } 5999 rcu_read_unlock(); 6000 } 6001 return false; 6002 } 6003 6004 struct fib6_nh_exception_dump_walker { 6005 struct rt6_rtnl_dump_arg *dump; 6006 struct fib6_info *rt; 6007 unsigned int flags; 6008 unsigned int skip; 6009 unsigned int count; 6010 }; 6011 6012 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) 6013 { 6014 struct fib6_nh_exception_dump_walker *w = arg; 6015 struct rt6_rtnl_dump_arg *dump = w->dump; 6016 struct rt6_exception_bucket *bucket; 6017 struct rt6_exception *rt6_ex; 6018 int i, err; 6019 6020 bucket = fib6_nh_get_excptn_bucket(nh, NULL); 6021 if (!bucket) 6022 return 0; 6023 6024 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 6025 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 6026 if (w->skip) { 6027 w->skip--; 6028 continue; 6029 } 6030 6031 /* Expiration of entries doesn't bump sernum, insertion 6032 * does. Removal is triggered by insertion, so we can 6033 * rely on the fact that if entries change between two 6034 * partial dumps, this node is scanned again completely, 6035 * see rt6_insert_exception() and fib6_dump_table(). 6036 * 6037 * Count expired entries we go through as handled 6038 * entries that we'll skip next time, in case of partial 6039 * node dump. Otherwise, if entries expire meanwhile, 6040 * we'll skip the wrong amount. 6041 */ 6042 if (rt6_check_expired(rt6_ex->rt6i)) { 6043 w->count++; 6044 continue; 6045 } 6046 6047 err = rt6_fill_node(dump->net, dump->skb, w->rt, 6048 &rt6_ex->rt6i->dst, NULL, NULL, 0, 6049 RTM_NEWROUTE, 6050 NETLINK_CB(dump->cb->skb).portid, 6051 dump->cb->nlh->nlmsg_seq, w->flags); 6052 if (err) 6053 return err; 6054 6055 w->count++; 6056 } 6057 bucket++; 6058 } 6059 6060 return 0; 6061 } 6062 6063 /* Return -1 if done with node, number of handled routes on partial dump */ 6064 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) 6065 { 6066 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 6067 struct fib_dump_filter *filter = &arg->filter; 6068 unsigned int flags = NLM_F_MULTI; 6069 struct net *net = arg->net; 6070 int count = 0; 6071 6072 if (rt == net->ipv6.fib6_null_entry) 6073 return -1; 6074 6075 if ((filter->flags & RTM_F_PREFIX) && 6076 !(rt->fib6_flags & RTF_PREFIX_RT)) { 6077 /* success since this is not a prefix route */ 6078 return -1; 6079 } 6080 if (filter->filter_set && 6081 ((filter->rt_type && rt->fib6_type != filter->rt_type) || 6082 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 6083 (filter->protocol && rt->fib6_protocol != filter->protocol))) { 6084 return -1; 6085 } 6086 6087 if (filter->filter_set || 6088 !filter->dump_routes || !filter->dump_exceptions) { 6089 flags |= NLM_F_DUMP_FILTERED; 6090 } 6091 6092 if (filter->dump_routes) { 6093 if (skip) { 6094 skip--; 6095 } else { 6096 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 6097 0, RTM_NEWROUTE, 6098 NETLINK_CB(arg->cb->skb).portid, 6099 arg->cb->nlh->nlmsg_seq, flags)) { 6100 return 0; 6101 } 6102 count++; 6103 } 6104 } 6105 6106 if (filter->dump_exceptions) { 6107 struct fib6_nh_exception_dump_walker w = { .dump = arg, 6108 .rt = rt, 6109 .flags = flags, 6110 .skip = skip, 6111 .count = 0 }; 6112 int err; 6113 6114 rcu_read_lock(); 6115 if (rt->nh) { 6116 err = nexthop_for_each_fib6_nh(rt->nh, 6117 rt6_nh_dump_exceptions, 6118 &w); 6119 } else { 6120 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); 6121 } 6122 rcu_read_unlock(); 6123 6124 if (err) 6125 return count + w.count; 6126 } 6127 6128 return -1; 6129 } 6130 6131 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 6132 const struct nlmsghdr *nlh, 6133 struct nlattr **tb, 6134 struct netlink_ext_ack *extack) 6135 { 6136 struct rtmsg *rtm; 6137 int i, err; 6138 6139 rtm = nlmsg_payload(nlh, sizeof(*rtm)); 6140 if (!rtm) { 6141 NL_SET_ERR_MSG_MOD(extack, 6142 "Invalid header for get route request"); 6143 return -EINVAL; 6144 } 6145 6146 if (!netlink_strict_get_check(skb)) 6147 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 6148 rtm_ipv6_policy, extack); 6149 6150 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 6151 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 6152 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 6153 rtm->rtm_type) { 6154 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 6155 return -EINVAL; 6156 } 6157 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 6158 NL_SET_ERR_MSG_MOD(extack, 6159 "Invalid flags for get route request"); 6160 return -EINVAL; 6161 } 6162 6163 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 6164 rtm_ipv6_policy, extack); 6165 if (err) 6166 return err; 6167 6168 if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 6169 (tb[RTA_DST] && !rtm->rtm_dst_len)) { 6170 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 6171 return -EINVAL; 6172 } 6173 6174 if (tb[RTA_FLOWLABEL] && 6175 (nla_get_be32(tb[RTA_FLOWLABEL]) & ~IPV6_FLOWLABEL_MASK)) { 6176 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL], 6177 "Invalid flow label"); 6178 return -EINVAL; 6179 } 6180 6181 for (i = 0; i <= RTA_MAX; i++) { 6182 if (!tb[i]) 6183 continue; 6184 6185 switch (i) { 6186 case RTA_SRC: 6187 case RTA_DST: 6188 case RTA_IIF: 6189 case RTA_OIF: 6190 case RTA_MARK: 6191 case RTA_UID: 6192 case RTA_SPORT: 6193 case RTA_DPORT: 6194 case RTA_IP_PROTO: 6195 case RTA_FLOWLABEL: 6196 break; 6197 default: 6198 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 6199 return -EINVAL; 6200 } 6201 } 6202 6203 return 0; 6204 } 6205 6206 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 6207 struct netlink_ext_ack *extack) 6208 { 6209 struct net *net = sock_net(in_skb->sk); 6210 struct nlattr *tb[RTA_MAX+1]; 6211 int err, iif = 0, oif = 0; 6212 struct fib6_info *from; 6213 struct dst_entry *dst; 6214 struct rt6_info *rt; 6215 struct sk_buff *skb; 6216 struct rtmsg *rtm; 6217 struct flowi6 fl6 = {}; 6218 __be32 flowlabel; 6219 bool fibmatch; 6220 6221 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 6222 if (err < 0) 6223 goto errout; 6224 6225 err = -EINVAL; 6226 rtm = nlmsg_data(nlh); 6227 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 6228 6229 if (tb[RTA_SRC]) { 6230 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 6231 goto errout; 6232 6233 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 6234 } 6235 6236 if (tb[RTA_DST]) { 6237 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 6238 goto errout; 6239 6240 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 6241 } 6242 6243 if (tb[RTA_IIF]) 6244 iif = nla_get_u32(tb[RTA_IIF]); 6245 6246 if (tb[RTA_OIF]) 6247 oif = nla_get_u32(tb[RTA_OIF]); 6248 6249 if (tb[RTA_MARK]) 6250 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 6251 6252 if (tb[RTA_UID]) 6253 fl6.flowi6_uid = make_kuid(current_user_ns(), 6254 nla_get_u32(tb[RTA_UID])); 6255 else 6256 fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 6257 6258 if (tb[RTA_SPORT]) 6259 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 6260 6261 if (tb[RTA_DPORT]) 6262 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 6263 6264 if (tb[RTA_IP_PROTO]) { 6265 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 6266 &fl6.flowi6_proto, AF_INET6, 6267 extack); 6268 if (err) 6269 goto errout; 6270 } 6271 6272 flowlabel = nla_get_be32_default(tb[RTA_FLOWLABEL], 0); 6273 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, flowlabel); 6274 6275 if (iif) { 6276 struct net_device *dev; 6277 int flags = 0; 6278 6279 rcu_read_lock(); 6280 6281 dev = dev_get_by_index_rcu(net, iif); 6282 if (!dev) { 6283 rcu_read_unlock(); 6284 err = -ENODEV; 6285 goto errout; 6286 } 6287 6288 fl6.flowi6_iif = iif; 6289 6290 if (!ipv6_addr_any(&fl6.saddr)) 6291 flags |= RT6_LOOKUP_F_HAS_SADDR; 6292 6293 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 6294 6295 rcu_read_unlock(); 6296 } else { 6297 fl6.flowi6_oif = oif; 6298 6299 dst = ip6_route_output(net, NULL, &fl6); 6300 } 6301 6302 6303 rt = dst_rt6_info(dst); 6304 if (rt->dst.error) { 6305 err = rt->dst.error; 6306 ip6_rt_put(rt); 6307 goto errout; 6308 } 6309 6310 if (rt == net->ipv6.ip6_null_entry) { 6311 err = rt->dst.error; 6312 ip6_rt_put(rt); 6313 goto errout; 6314 } 6315 6316 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 6317 if (!skb) { 6318 ip6_rt_put(rt); 6319 err = -ENOBUFS; 6320 goto errout; 6321 } 6322 6323 skb_dst_set(skb, &rt->dst); 6324 6325 rcu_read_lock(); 6326 from = rcu_dereference(rt->from); 6327 if (from) { 6328 if (fibmatch) 6329 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 6330 iif, RTM_NEWROUTE, 6331 NETLINK_CB(in_skb).portid, 6332 nlh->nlmsg_seq, 0); 6333 else 6334 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 6335 &fl6.saddr, iif, RTM_NEWROUTE, 6336 NETLINK_CB(in_skb).portid, 6337 nlh->nlmsg_seq, 0); 6338 } else { 6339 err = -ENETUNREACH; 6340 } 6341 rcu_read_unlock(); 6342 6343 if (err < 0) { 6344 kfree_skb(skb); 6345 goto errout; 6346 } 6347 6348 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 6349 errout: 6350 return err; 6351 } 6352 6353 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 6354 unsigned int nlm_flags) 6355 { 6356 struct net *net = info->nl_net; 6357 struct sk_buff *skb; 6358 size_t sz; 6359 u32 seq; 6360 int err; 6361 6362 err = -ENOBUFS; 6363 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 6364 6365 rcu_read_lock(); 6366 sz = rt6_nlmsg_size(rt); 6367 retry: 6368 skb = nlmsg_new(sz, GFP_ATOMIC); 6369 if (!skb) 6370 goto errout; 6371 6372 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 6373 event, info->portid, seq, nlm_flags); 6374 if (err < 0) { 6375 kfree_skb(skb); 6376 /* -EMSGSIZE implies needed space grew under us. */ 6377 if (err == -EMSGSIZE) { 6378 sz = max(rt6_nlmsg_size(rt), sz << 1); 6379 goto retry; 6380 } 6381 goto errout; 6382 } 6383 6384 rcu_read_unlock(); 6385 6386 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 6387 info->nlh, GFP_ATOMIC); 6388 return; 6389 errout: 6390 rcu_read_unlock(); 6391 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 6392 } 6393 6394 void fib6_rt_update(struct net *net, struct fib6_info *rt, 6395 struct nl_info *info) 6396 { 6397 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 6398 struct sk_buff *skb; 6399 int err = -ENOBUFS; 6400 6401 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 6402 if (!skb) 6403 goto errout; 6404 6405 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 6406 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 6407 if (err < 0) { 6408 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 6409 WARN_ON(err == -EMSGSIZE); 6410 kfree_skb(skb); 6411 goto errout; 6412 } 6413 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 6414 info->nlh, gfp_any()); 6415 return; 6416 errout: 6417 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 6418 } 6419 6420 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i, 6421 bool offload, bool trap, bool offload_failed) 6422 { 6423 u8 fib_notify_on_flag_change; 6424 struct sk_buff *skb; 6425 int err; 6426 6427 if (READ_ONCE(f6i->offload) == offload && 6428 READ_ONCE(f6i->trap) == trap && 6429 READ_ONCE(f6i->offload_failed) == offload_failed) 6430 return; 6431 6432 WRITE_ONCE(f6i->offload, offload); 6433 WRITE_ONCE(f6i->trap, trap); 6434 6435 fib_notify_on_flag_change = READ_ONCE(net->ipv6.sysctl.fib_notify_on_flag_change); 6436 /* 2 means send notifications only if offload_failed was changed. */ 6437 if (fib_notify_on_flag_change == 2 && 6438 READ_ONCE(f6i->offload_failed) == offload_failed) 6439 return; 6440 6441 WRITE_ONCE(f6i->offload_failed, offload_failed); 6442 6443 if (!rcu_access_pointer(f6i->fib6_node)) 6444 /* The route was removed from the tree, do not send 6445 * notification. 6446 */ 6447 return; 6448 6449 if (!fib_notify_on_flag_change) 6450 return; 6451 6452 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL); 6453 if (!skb) { 6454 err = -ENOBUFS; 6455 goto errout; 6456 } 6457 6458 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0, 6459 0, 0); 6460 if (err < 0) { 6461 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 6462 WARN_ON(err == -EMSGSIZE); 6463 kfree_skb(skb); 6464 goto errout; 6465 } 6466 6467 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL); 6468 return; 6469 6470 errout: 6471 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 6472 } 6473 EXPORT_SYMBOL(fib6_info_hw_flags_set); 6474 6475 static int ip6_route_dev_notify(struct notifier_block *this, 6476 unsigned long event, void *ptr) 6477 { 6478 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6479 struct net *net = dev_net(dev); 6480 6481 if (!(dev->flags & IFF_LOOPBACK)) 6482 return NOTIFY_OK; 6483 6484 if (event == NETDEV_REGISTER) { 6485 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; 6486 net->ipv6.ip6_null_entry->dst.dev = dev; 6487 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 6488 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6489 net->ipv6.ip6_prohibit_entry->dst.dev = dev; 6490 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 6491 net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 6492 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 6493 #endif 6494 } else if (event == NETDEV_UNREGISTER && 6495 dev->reg_state != NETREG_UNREGISTERED) { 6496 /* NETDEV_UNREGISTER could be fired for multiple times by 6497 * netdev_wait_allrefs(). Make sure we only call this once. 6498 */ 6499 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 6500 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6501 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 6502 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 6503 #endif 6504 } 6505 6506 return NOTIFY_OK; 6507 } 6508 6509 /* 6510 * /proc 6511 */ 6512 6513 #ifdef CONFIG_PROC_FS 6514 static int rt6_stats_seq_show(struct seq_file *seq, void *v) 6515 { 6516 struct net *net = (struct net *)seq->private; 6517 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 6518 net->ipv6.rt6_stats->fib_nodes, 6519 net->ipv6.rt6_stats->fib_route_nodes, 6520 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 6521 net->ipv6.rt6_stats->fib_rt_entries, 6522 net->ipv6.rt6_stats->fib_rt_cache, 6523 dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 6524 net->ipv6.rt6_stats->fib_discarded_routes); 6525 6526 return 0; 6527 } 6528 #endif /* CONFIG_PROC_FS */ 6529 6530 #ifdef CONFIG_SYSCTL 6531 6532 static int ipv6_sysctl_rtcache_flush(const struct ctl_table *ctl, int write, 6533 void *buffer, size_t *lenp, loff_t *ppos) 6534 { 6535 struct net *net; 6536 int delay; 6537 int ret; 6538 if (!write) 6539 return -EINVAL; 6540 6541 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6542 if (ret) 6543 return ret; 6544 6545 net = (struct net *)ctl->extra1; 6546 delay = READ_ONCE(net->ipv6.sysctl.flush_delay); 6547 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 6548 return 0; 6549 } 6550 6551 static struct ctl_table ipv6_route_table_template[] = { 6552 { 6553 .procname = "max_size", 6554 .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 6555 .maxlen = sizeof(int), 6556 .mode = 0644, 6557 .proc_handler = proc_dointvec, 6558 }, 6559 { 6560 .procname = "gc_thresh", 6561 .data = &ip6_dst_ops_template.gc_thresh, 6562 .maxlen = sizeof(int), 6563 .mode = 0644, 6564 .proc_handler = proc_dointvec, 6565 }, 6566 { 6567 .procname = "flush", 6568 .data = &init_net.ipv6.sysctl.flush_delay, 6569 .maxlen = sizeof(int), 6570 .mode = 0200, 6571 .proc_handler = ipv6_sysctl_rtcache_flush 6572 }, 6573 { 6574 .procname = "gc_min_interval", 6575 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 6576 .maxlen = sizeof(int), 6577 .mode = 0644, 6578 .proc_handler = proc_dointvec_jiffies, 6579 }, 6580 { 6581 .procname = "gc_timeout", 6582 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 6583 .maxlen = sizeof(int), 6584 .mode = 0644, 6585 .proc_handler = proc_dointvec_jiffies, 6586 }, 6587 { 6588 .procname = "gc_interval", 6589 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 6590 .maxlen = sizeof(int), 6591 .mode = 0644, 6592 .proc_handler = proc_dointvec_jiffies, 6593 }, 6594 { 6595 .procname = "gc_elasticity", 6596 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 6597 .maxlen = sizeof(int), 6598 .mode = 0644, 6599 .proc_handler = proc_dointvec, 6600 }, 6601 { 6602 .procname = "mtu_expires", 6603 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 6604 .maxlen = sizeof(int), 6605 .mode = 0644, 6606 .proc_handler = proc_dointvec_jiffies, 6607 }, 6608 { 6609 .procname = "min_adv_mss", 6610 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 6611 .maxlen = sizeof(int), 6612 .mode = 0644, 6613 .proc_handler = proc_dointvec, 6614 }, 6615 { 6616 .procname = "gc_min_interval_ms", 6617 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 6618 .maxlen = sizeof(int), 6619 .mode = 0644, 6620 .proc_handler = proc_dointvec_ms_jiffies, 6621 }, 6622 { 6623 .procname = "skip_notify_on_dev_down", 6624 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 6625 .maxlen = sizeof(u8), 6626 .mode = 0644, 6627 .proc_handler = proc_dou8vec_minmax, 6628 .extra1 = SYSCTL_ZERO, 6629 .extra2 = SYSCTL_ONE, 6630 }, 6631 }; 6632 6633 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 6634 { 6635 struct ctl_table *table; 6636 6637 table = kmemdup(ipv6_route_table_template, 6638 sizeof(ipv6_route_table_template), 6639 GFP_KERNEL); 6640 6641 if (table) { 6642 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size; 6643 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 6644 table[2].data = &net->ipv6.sysctl.flush_delay; 6645 table[2].extra1 = net; 6646 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 6647 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 6648 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 6649 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 6650 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 6651 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 6652 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 6653 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 6654 } 6655 6656 return table; 6657 } 6658 6659 size_t ipv6_route_sysctl_table_size(struct net *net) 6660 { 6661 /* Don't export sysctls to unprivileged users */ 6662 if (net->user_ns != &init_user_ns) 6663 return 1; 6664 6665 return ARRAY_SIZE(ipv6_route_table_template); 6666 } 6667 #endif 6668 6669 static int __net_init ip6_route_net_init(struct net *net) 6670 { 6671 int ret = -ENOMEM; 6672 6673 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 6674 sizeof(net->ipv6.ip6_dst_ops)); 6675 6676 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 6677 goto out_ip6_dst_ops; 6678 6679 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); 6680 if (!net->ipv6.fib6_null_entry) 6681 goto out_ip6_dst_entries; 6682 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, 6683 sizeof(*net->ipv6.fib6_null_entry)); 6684 6685 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 6686 sizeof(*net->ipv6.ip6_null_entry), 6687 GFP_KERNEL); 6688 if (!net->ipv6.ip6_null_entry) 6689 goto out_fib6_null_entry; 6690 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 6691 dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 6692 ip6_template_metrics, true); 6693 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached); 6694 6695 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6696 net->ipv6.fib6_has_custom_rules = false; 6697 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 6698 sizeof(*net->ipv6.ip6_prohibit_entry), 6699 GFP_KERNEL); 6700 if (!net->ipv6.ip6_prohibit_entry) 6701 goto out_ip6_null_entry; 6702 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 6703 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 6704 ip6_template_metrics, true); 6705 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached); 6706 6707 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 6708 sizeof(*net->ipv6.ip6_blk_hole_entry), 6709 GFP_KERNEL); 6710 if (!net->ipv6.ip6_blk_hole_entry) 6711 goto out_ip6_prohibit_entry; 6712 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 6713 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 6714 ip6_template_metrics, true); 6715 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached); 6716 #ifdef CONFIG_IPV6_SUBTREES 6717 net->ipv6.fib6_routes_require_src = 0; 6718 #endif 6719 #endif 6720 6721 net->ipv6.sysctl.flush_delay = 0; 6722 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX; 6723 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 6724 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 6725 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 6726 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 6727 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 6728 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 6729 net->ipv6.sysctl.skip_notify_on_dev_down = 0; 6730 6731 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ); 6732 6733 ret = 0; 6734 out: 6735 return ret; 6736 6737 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6738 out_ip6_prohibit_entry: 6739 kfree(net->ipv6.ip6_prohibit_entry); 6740 out_ip6_null_entry: 6741 kfree(net->ipv6.ip6_null_entry); 6742 #endif 6743 out_fib6_null_entry: 6744 kfree(net->ipv6.fib6_null_entry); 6745 out_ip6_dst_entries: 6746 dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6747 out_ip6_dst_ops: 6748 goto out; 6749 } 6750 6751 static void __net_exit ip6_route_net_exit(struct net *net) 6752 { 6753 kfree(net->ipv6.fib6_null_entry); 6754 kfree(net->ipv6.ip6_null_entry); 6755 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6756 kfree(net->ipv6.ip6_prohibit_entry); 6757 kfree(net->ipv6.ip6_blk_hole_entry); 6758 #endif 6759 dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6760 } 6761 6762 static int __net_init ip6_route_net_init_late(struct net *net) 6763 { 6764 #ifdef CONFIG_PROC_FS 6765 if (!proc_create_net("ipv6_route", 0, net->proc_net, 6766 &ipv6_route_seq_ops, 6767 sizeof(struct ipv6_route_iter))) 6768 return -ENOMEM; 6769 6770 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net, 6771 rt6_stats_seq_show, NULL)) { 6772 remove_proc_entry("ipv6_route", net->proc_net); 6773 return -ENOMEM; 6774 } 6775 #endif 6776 return 0; 6777 } 6778 6779 static void __net_exit ip6_route_net_exit_late(struct net *net) 6780 { 6781 #ifdef CONFIG_PROC_FS 6782 remove_proc_entry("ipv6_route", net->proc_net); 6783 remove_proc_entry("rt6_stats", net->proc_net); 6784 #endif 6785 } 6786 6787 static struct pernet_operations ip6_route_net_ops = { 6788 .init = ip6_route_net_init, 6789 .exit = ip6_route_net_exit, 6790 }; 6791 6792 static int __net_init ipv6_inetpeer_init(struct net *net) 6793 { 6794 struct inet_peer_base *bp = kmalloc_obj(*bp); 6795 6796 if (!bp) 6797 return -ENOMEM; 6798 inet_peer_base_init(bp); 6799 net->ipv6.peers = bp; 6800 return 0; 6801 } 6802 6803 static void __net_exit ipv6_inetpeer_exit(struct net *net) 6804 { 6805 struct inet_peer_base *bp = net->ipv6.peers; 6806 6807 net->ipv6.peers = NULL; 6808 inetpeer_invalidate_tree(bp); 6809 kfree(bp); 6810 } 6811 6812 static struct pernet_operations ipv6_inetpeer_ops = { 6813 .init = ipv6_inetpeer_init, 6814 .exit = ipv6_inetpeer_exit, 6815 }; 6816 6817 static struct pernet_operations ip6_route_net_late_ops = { 6818 .init = ip6_route_net_init_late, 6819 .exit = ip6_route_net_exit_late, 6820 }; 6821 6822 static struct notifier_block ip6_route_dev_notifier = { 6823 .notifier_call = ip6_route_dev_notify, 6824 .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 6825 }; 6826 6827 void __init ip6_route_init_special_entries(void) 6828 { 6829 /* Registering of the loopback is done before this portion of code, 6830 * the loopback reference in rt6_info will not be taken, do it 6831 * manually for init_net */ 6832 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; 6833 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 6834 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 6835 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6836 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 6837 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 6838 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 6839 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 6840 #endif 6841 } 6842 6843 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6844 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt) 6845 6846 BTF_ID_LIST_SINGLE(btf_fib6_info_id, struct, fib6_info) 6847 6848 static const struct bpf_iter_seq_info ipv6_route_seq_info = { 6849 .seq_ops = &ipv6_route_seq_ops, 6850 .init_seq_private = bpf_iter_init_seq_net, 6851 .fini_seq_private = bpf_iter_fini_seq_net, 6852 .seq_priv_size = sizeof(struct ipv6_route_iter), 6853 }; 6854 6855 static struct bpf_iter_reg ipv6_route_reg_info = { 6856 .target = "ipv6_route", 6857 .ctx_arg_info_size = 1, 6858 .ctx_arg_info = { 6859 { offsetof(struct bpf_iter__ipv6_route, rt), 6860 PTR_TO_BTF_ID_OR_NULL }, 6861 }, 6862 .seq_info = &ipv6_route_seq_info, 6863 }; 6864 6865 static int __init bpf_iter_register(void) 6866 { 6867 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id; 6868 return bpf_iter_reg_target(&ipv6_route_reg_info); 6869 } 6870 6871 static void bpf_iter_unregister(void) 6872 { 6873 bpf_iter_unreg_target(&ipv6_route_reg_info); 6874 } 6875 #endif 6876 6877 static const struct rtnl_msg_handler ip6_route_rtnl_msg_handlers[] __initconst_or_module = { 6878 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWROUTE, 6879 .doit = inet6_rtm_newroute, .flags = RTNL_FLAG_DOIT_UNLOCKED}, 6880 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELROUTE, 6881 .doit = inet6_rtm_delroute, .flags = RTNL_FLAG_DOIT_UNLOCKED}, 6882 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETROUTE, 6883 .doit = inet6_rtm_getroute, .flags = RTNL_FLAG_DOIT_UNLOCKED}, 6884 }; 6885 6886 int __init ip6_route_init(void) 6887 { 6888 int ret; 6889 int cpu; 6890 6891 ret = -ENOMEM; 6892 ip6_dst_ops_template.kmem_cachep = 6893 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 6894 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL); 6895 if (!ip6_dst_ops_template.kmem_cachep) 6896 goto out; 6897 6898 ret = dst_entries_init(&ip6_dst_blackhole_ops); 6899 if (ret) 6900 goto out_kmem_cache; 6901 6902 ret = register_pernet_subsys(&ipv6_inetpeer_ops); 6903 if (ret) 6904 goto out_dst_entries; 6905 6906 ret = register_pernet_subsys(&ip6_route_net_ops); 6907 if (ret) 6908 goto out_register_inetpeer; 6909 6910 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 6911 6912 ret = fib6_init(); 6913 if (ret) 6914 goto out_register_subsys; 6915 6916 ret = xfrm6_init(); 6917 if (ret) 6918 goto out_fib6_init; 6919 6920 ret = fib6_rules_init(); 6921 if (ret) 6922 goto xfrm6_init; 6923 6924 ret = register_pernet_subsys(&ip6_route_net_late_ops); 6925 if (ret) 6926 goto fib6_rules_init; 6927 6928 ret = rtnl_register_many(ip6_route_rtnl_msg_handlers); 6929 if (ret < 0) 6930 goto out_register_late_subsys; 6931 6932 ret = register_netdevice_notifier(&ip6_route_dev_notifier); 6933 if (ret) 6934 goto out_register_late_subsys; 6935 6936 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6937 ret = bpf_iter_register(); 6938 if (ret) 6939 goto out_register_notifier; 6940 #endif 6941 6942 for_each_possible_cpu(cpu) { 6943 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 6944 6945 INIT_LIST_HEAD(&ul->head); 6946 spin_lock_init(&ul->lock); 6947 } 6948 6949 out: 6950 return ret; 6951 6952 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6953 out_register_notifier: 6954 unregister_netdevice_notifier(&ip6_route_dev_notifier); 6955 #endif 6956 out_register_late_subsys: 6957 rtnl_unregister_all(PF_INET6); 6958 unregister_pernet_subsys(&ip6_route_net_late_ops); 6959 fib6_rules_init: 6960 fib6_rules_cleanup(); 6961 xfrm6_init: 6962 xfrm6_fini(); 6963 out_fib6_init: 6964 fib6_gc_cleanup(); 6965 out_register_subsys: 6966 unregister_pernet_subsys(&ip6_route_net_ops); 6967 out_register_inetpeer: 6968 unregister_pernet_subsys(&ipv6_inetpeer_ops); 6969 out_dst_entries: 6970 dst_entries_destroy(&ip6_dst_blackhole_ops); 6971 out_kmem_cache: 6972 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6973 goto out; 6974 } 6975 6976 void ip6_route_cleanup(void) 6977 { 6978 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6979 bpf_iter_unregister(); 6980 #endif 6981 unregister_netdevice_notifier(&ip6_route_dev_notifier); 6982 unregister_pernet_subsys(&ip6_route_net_late_ops); 6983 fib6_rules_cleanup(); 6984 xfrm6_fini(); 6985 fib6_gc_cleanup(); 6986 unregister_pernet_subsys(&ipv6_inetpeer_ops); 6987 unregister_pernet_subsys(&ip6_route_net_ops); 6988 dst_entries_destroy(&ip6_dst_blackhole_ops); 6989 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6990 } 6991