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