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