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