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