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