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