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