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