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