1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
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.
20 * Fixed routing subtrees.
23 #define pr_fmt(fmt) "IPv6: " fmt
25 #include <linux/capability.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/times.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/route.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/mroute6.h>
37 #include <linux/init.h>
38 #include <linux/if_arp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/nsproxy.h>
42 #include <linux/slab.h>
43 #include <linux/jhash.h>
44 #include <linux/siphash.h>
45 #include <net/net_namespace.h>
48 #include <net/ip6_fib.h>
49 #include <net/ip6_route.h>
50 #include <net/ndisc.h>
51 #include <net/addrconf.h>
53 #include <linux/rtnetlink.h>
55 #include <net/dst_metadata.h>
57 #include <net/netevent.h>
58 #include <net/netlink.h>
60 #include <net/lwtunnel.h>
61 #include <net/ip_tunnels.h>
62 #include <net/l3mdev.h>
64 #include <linux/uaccess.h>
65 #include <linux/btf_ids.h>
68 #include <linux/sysctl.h>
71 static int ip6_rt_type_to_error(u8 fib6_type);
73 #define CREATE_TRACE_POINTS
74 #include <trace/events/fib6.h>
75 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
76 #undef CREATE_TRACE_POINTS
79 RT6_NUD_FAIL_HARD = -3,
80 RT6_NUD_FAIL_PROBE = -2,
81 RT6_NUD_FAIL_DO_RR = -1,
85 INDIRECT_CALLABLE_SCOPE
86 struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
87 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
88 INDIRECT_CALLABLE_SCOPE
89 unsigned int ip6_mtu(const struct dst_entry *dst);
90 static void ip6_negative_advice(struct sock *sk,
91 struct dst_entry *dst);
92 static void ip6_dst_destroy(struct dst_entry *);
93 static void ip6_dst_ifdown(struct dst_entry *,
94 struct net_device *dev);
95 static void ip6_dst_gc(struct dst_ops *ops);
97 static int ip6_pkt_discard(struct sk_buff *skb);
98 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
99 static int ip6_pkt_prohibit(struct sk_buff *skb);
100 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
101 static void ip6_link_failure(struct sk_buff *skb);
102 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
103 struct sk_buff *skb, u32 mtu,
105 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
106 struct sk_buff *skb);
107 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
109 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
110 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
111 struct fib6_info *rt, struct dst_entry *dst,
112 struct in6_addr *dest, struct in6_addr *src,
113 int iif, int type, u32 portid, u32 seq,
115 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
116 const struct in6_addr *daddr,
117 const struct in6_addr *saddr);
119 #ifdef CONFIG_IPV6_ROUTE_INFO
120 static struct fib6_info *rt6_add_route_info(struct net *net,
121 const struct in6_addr *prefix, int prefixlen,
122 const struct in6_addr *gwaddr,
123 struct net_device *dev,
125 static struct fib6_info *rt6_get_route_info(struct net *net,
126 const struct in6_addr *prefix, int prefixlen,
127 const struct in6_addr *gwaddr,
128 struct net_device *dev);
131 struct uncached_list {
133 struct list_head head;
134 struct list_head quarantine;
137 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
139 void rt6_uncached_list_add(struct rt6_info *rt)
141 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
143 rt->dst.rt_uncached_list = ul;
145 spin_lock_bh(&ul->lock);
146 list_add_tail(&rt->dst.rt_uncached, &ul->head);
147 spin_unlock_bh(&ul->lock);
150 void rt6_uncached_list_del(struct rt6_info *rt)
152 if (!list_empty(&rt->dst.rt_uncached)) {
153 struct uncached_list *ul = rt->dst.rt_uncached_list;
155 spin_lock_bh(&ul->lock);
156 list_del_init(&rt->dst.rt_uncached);
157 spin_unlock_bh(&ul->lock);
161 static void rt6_uncached_list_flush_dev(struct net_device *dev)
165 for_each_possible_cpu(cpu) {
166 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167 struct rt6_info *rt, *safe;
169 if (list_empty(&ul->head))
172 spin_lock_bh(&ul->lock);
173 list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
174 struct inet6_dev *rt_idev = rt->rt6i_idev;
175 struct net_device *rt_dev = rt->dst.dev;
176 bool handled = false;
178 if (rt_idev->dev == dev) {
179 rt->rt6i_idev = in6_dev_get(blackhole_netdev);
180 in6_dev_put(rt_idev);
185 rt->dst.dev = blackhole_netdev;
186 netdev_ref_replace(rt_dev, blackhole_netdev,
187 &rt->dst.dev_tracker,
192 list_move(&rt->dst.rt_uncached,
195 spin_unlock_bh(&ul->lock);
199 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
203 if (!ipv6_addr_any(p))
204 return (const void *) p;
206 return &ipv6_hdr(skb)->daddr;
210 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
211 struct net_device *dev,
217 daddr = choose_neigh_daddr(gw, skb, daddr);
218 n = __ipv6_neigh_lookup(dev, daddr);
222 n = neigh_create(&nd_tbl, daddr, dev);
223 return IS_ERR(n) ? NULL : n;
226 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
230 const struct rt6_info *rt = dst_rt6_info(dst);
232 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
233 dst->dev, skb, daddr);
236 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
238 const struct rt6_info *rt = dst_rt6_info(dst);
239 struct net_device *dev = dst->dev;
241 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
244 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
246 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
248 __ipv6_confirm_neigh(dev, daddr);
251 static struct dst_ops ip6_dst_ops_template = {
255 .check = ip6_dst_check,
256 .default_advmss = ip6_default_advmss,
258 .cow_metrics = dst_cow_metrics_generic,
259 .destroy = ip6_dst_destroy,
260 .ifdown = ip6_dst_ifdown,
261 .negative_advice = ip6_negative_advice,
262 .link_failure = ip6_link_failure,
263 .update_pmtu = ip6_rt_update_pmtu,
264 .redirect = rt6_do_redirect,
265 .local_out = __ip6_local_out,
266 .neigh_lookup = ip6_dst_neigh_lookup,
267 .confirm_neigh = ip6_confirm_neigh,
270 static struct dst_ops ip6_dst_blackhole_ops = {
272 .default_advmss = ip6_default_advmss,
273 .neigh_lookup = ip6_dst_neigh_lookup,
274 .check = ip6_dst_check,
275 .destroy = ip6_dst_destroy,
276 .cow_metrics = dst_cow_metrics_generic,
277 .update_pmtu = dst_blackhole_update_pmtu,
278 .redirect = dst_blackhole_redirect,
279 .mtu = dst_blackhole_mtu,
282 static const u32 ip6_template_metrics[RTAX_MAX] = {
283 [RTAX_HOPLIMIT - 1] = 0,
286 static const struct fib6_info fib6_null_entry_template = {
287 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
288 .fib6_protocol = RTPROT_KERNEL,
289 .fib6_metric = ~(u32)0,
290 .fib6_ref = REFCOUNT_INIT(1),
291 .fib6_type = RTN_UNREACHABLE,
292 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
295 static const struct rt6_info ip6_null_entry_template = {
297 .__rcuref = RCUREF_INIT(1),
299 .obsolete = DST_OBSOLETE_FORCE_CHK,
300 .error = -ENETUNREACH,
301 .input = ip6_pkt_discard,
302 .output = ip6_pkt_discard_out,
304 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
307 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
309 static const struct rt6_info ip6_prohibit_entry_template = {
311 .__rcuref = RCUREF_INIT(1),
313 .obsolete = DST_OBSOLETE_FORCE_CHK,
315 .input = ip6_pkt_prohibit,
316 .output = ip6_pkt_prohibit_out,
318 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
321 static const struct rt6_info ip6_blk_hole_entry_template = {
323 .__rcuref = RCUREF_INIT(1),
325 .obsolete = DST_OBSOLETE_FORCE_CHK,
327 .input = dst_discard,
328 .output = dst_discard_out,
330 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
335 static void rt6_info_init(struct rt6_info *rt)
337 memset_after(rt, 0, dst);
340 /* allocate dst with ip6_dst_ops */
341 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
344 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
345 DST_OBSOLETE_FORCE_CHK, flags);
349 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
354 EXPORT_SYMBOL(ip6_dst_alloc);
356 static void ip6_dst_destroy(struct dst_entry *dst)
358 struct rt6_info *rt = dst_rt6_info(dst);
359 struct fib6_info *from;
360 struct inet6_dev *idev;
362 ip_dst_metrics_put(dst);
363 rt6_uncached_list_del(rt);
365 idev = rt->rt6i_idev;
367 rt->rt6i_idev = NULL;
371 from = xchg((__force struct fib6_info **)&rt->from, NULL);
372 fib6_info_release(from);
375 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
377 struct rt6_info *rt = dst_rt6_info(dst);
378 struct inet6_dev *idev = rt->rt6i_idev;
380 if (idev && idev->dev != blackhole_netdev) {
381 struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev);
383 if (blackhole_idev) {
384 rt->rt6i_idev = blackhole_idev;
390 static bool __rt6_check_expired(const struct rt6_info *rt)
392 if (rt->rt6i_flags & RTF_EXPIRES)
393 return time_after(jiffies, rt->dst.expires);
398 static bool rt6_check_expired(const struct rt6_info *rt)
400 struct fib6_info *from;
402 from = rcu_dereference(rt->from);
404 if (rt->rt6i_flags & RTF_EXPIRES) {
405 if (time_after(jiffies, rt->dst.expires))
408 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
409 fib6_check_expired(from);
414 void fib6_select_path(const struct net *net, struct fib6_result *res,
415 struct flowi6 *fl6, int oif, bool have_oif_match,
416 const struct sk_buff *skb, int strict)
418 struct fib6_info *sibling, *next_sibling;
419 struct fib6_info *match = res->f6i;
421 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
424 if (match->nh && have_oif_match && res->nh)
428 IP6CB(skb)->flags |= IP6SKB_MULTIPATH;
430 /* We might have already computed the hash for ICMPv6 errors. In such
431 * case it will always be non-zero. Otherwise now is the time to do it.
434 (!match->nh || nexthop_is_multipath(match->nh)))
435 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
437 if (unlikely(match->nh)) {
438 nexthop_path_fib6_result(res, fl6->mp_hash);
442 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
445 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
447 const struct fib6_nh *nh = sibling->fib6_nh;
450 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
451 if (fl6->mp_hash > nh_upper_bound)
453 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
461 res->nh = match->fib6_nh;
465 * Route lookup. rcu_read_lock() should be held.
468 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
469 const struct in6_addr *saddr, int oif, int flags)
471 const struct net_device *dev;
473 if (nh->fib_nh_flags & RTNH_F_DEAD)
476 dev = nh->fib_nh_dev;
478 if (dev->ifindex == oif)
481 if (ipv6_chk_addr(net, saddr, dev,
482 flags & RT6_LOOKUP_F_IFACE))
489 struct fib6_nh_dm_arg {
491 const struct in6_addr *saddr;
497 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
499 struct fib6_nh_dm_arg *arg = _arg;
502 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
506 /* returns fib6_nh from nexthop or NULL */
507 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
508 struct fib6_result *res,
509 const struct in6_addr *saddr,
512 struct fib6_nh_dm_arg arg = {
519 if (nexthop_is_blackhole(nh))
522 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
528 static void rt6_device_match(struct net *net, struct fib6_result *res,
529 const struct in6_addr *saddr, int oif, int flags)
531 struct fib6_info *f6i = res->f6i;
532 struct fib6_info *spf6i;
535 if (!oif && ipv6_addr_any(saddr)) {
536 if (unlikely(f6i->nh)) {
537 nh = nexthop_fib6_nh(f6i->nh);
538 if (nexthop_is_blackhole(f6i->nh))
543 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
547 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
548 bool matched = false;
550 if (unlikely(spf6i->nh)) {
551 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
557 if (__rt6_device_match(net, nh, saddr, oif, flags))
566 if (oif && flags & RT6_LOOKUP_F_IFACE) {
567 res->f6i = net->ipv6.fib6_null_entry;
568 nh = res->f6i->fib6_nh;
572 if (unlikely(f6i->nh)) {
573 nh = nexthop_fib6_nh(f6i->nh);
574 if (nexthop_is_blackhole(f6i->nh))
580 if (nh->fib_nh_flags & RTNH_F_DEAD) {
581 res->f6i = net->ipv6.fib6_null_entry;
582 nh = res->f6i->fib6_nh;
586 res->fib6_type = res->f6i->fib6_type;
587 res->fib6_flags = res->f6i->fib6_flags;
591 res->fib6_flags |= RTF_REJECT;
592 res->fib6_type = RTN_BLACKHOLE;
596 #ifdef CONFIG_IPV6_ROUTER_PREF
597 struct __rt6_probe_work {
598 struct work_struct work;
599 struct in6_addr target;
600 struct net_device *dev;
601 netdevice_tracker dev_tracker;
604 static void rt6_probe_deferred(struct work_struct *w)
606 struct in6_addr mcaddr;
607 struct __rt6_probe_work *work =
608 container_of(w, struct __rt6_probe_work, work);
610 addrconf_addr_solict_mult(&work->target, &mcaddr);
611 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
612 netdev_put(work->dev, &work->dev_tracker);
616 static void rt6_probe(struct fib6_nh *fib6_nh)
618 struct __rt6_probe_work *work = NULL;
619 const struct in6_addr *nh_gw;
620 unsigned long last_probe;
621 struct neighbour *neigh;
622 struct net_device *dev;
623 struct inet6_dev *idev;
626 * Okay, this does not seem to be appropriate
627 * for now, however, we need to check if it
628 * is really so; aka Router Reachability Probing.
630 * Router Reachability Probe MUST be rate-limited
631 * to no more than one per minute.
633 if (!fib6_nh->fib_nh_gw_family)
636 nh_gw = &fib6_nh->fib_nh_gw6;
637 dev = fib6_nh->fib_nh_dev;
639 last_probe = READ_ONCE(fib6_nh->last_probe);
640 idev = __in6_dev_get(dev);
643 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
645 if (READ_ONCE(neigh->nud_state) & NUD_VALID)
648 write_lock_bh(&neigh->lock);
649 if (!(neigh->nud_state & NUD_VALID) &&
652 READ_ONCE(idev->cnf.rtr_probe_interval))) {
653 work = kmalloc(sizeof(*work), GFP_ATOMIC);
655 __neigh_set_probe_once(neigh);
657 write_unlock_bh(&neigh->lock);
658 } else if (time_after(jiffies, last_probe +
659 READ_ONCE(idev->cnf.rtr_probe_interval))) {
660 work = kmalloc(sizeof(*work), GFP_ATOMIC);
663 if (!work || cmpxchg(&fib6_nh->last_probe,
664 last_probe, jiffies) != last_probe) {
667 INIT_WORK(&work->work, rt6_probe_deferred);
668 work->target = *nh_gw;
669 netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC);
671 schedule_work(&work->work);
678 static inline void rt6_probe(struct fib6_nh *fib6_nh)
684 * Default Router Selection (RFC 2461 6.3.6)
686 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
688 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
689 struct neighbour *neigh;
692 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
693 &fib6_nh->fib_nh_gw6);
695 u8 nud_state = READ_ONCE(neigh->nud_state);
697 if (nud_state & NUD_VALID)
698 ret = RT6_NUD_SUCCEED;
699 #ifdef CONFIG_IPV6_ROUTER_PREF
700 else if (!(nud_state & NUD_FAILED))
701 ret = RT6_NUD_SUCCEED;
703 ret = RT6_NUD_FAIL_PROBE;
706 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
707 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
714 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
719 if (!oif || nh->fib_nh_dev->ifindex == oif)
722 if (!m && (strict & RT6_LOOKUP_F_IFACE))
723 return RT6_NUD_FAIL_HARD;
724 #ifdef CONFIG_IPV6_ROUTER_PREF
725 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
727 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
728 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
729 int n = rt6_check_neigh(nh);
736 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
737 int oif, int strict, int *mpri, bool *do_rr)
739 bool match_do_rr = false;
743 if (nh->fib_nh_flags & RTNH_F_DEAD)
746 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
747 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
748 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
751 m = rt6_score_route(nh, fib6_flags, oif, strict);
752 if (m == RT6_NUD_FAIL_DO_RR) {
754 m = 0; /* lowest valid score */
755 } else if (m == RT6_NUD_FAIL_HARD) {
759 if (strict & RT6_LOOKUP_F_REACHABLE)
762 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
764 *do_rr = match_do_rr;
772 struct fib6_nh_frl_arg {
781 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
783 struct fib6_nh_frl_arg *arg = _arg;
786 return find_match(nh, arg->flags, arg->oif, arg->strict,
787 arg->mpri, arg->do_rr);
790 static void __find_rr_leaf(struct fib6_info *f6i_start,
791 struct fib6_info *nomatch, u32 metric,
792 struct fib6_result *res, struct fib6_info **cont,
793 int oif, int strict, bool *do_rr, int *mpri)
795 struct fib6_info *f6i;
797 for (f6i = f6i_start;
798 f6i && f6i != nomatch;
799 f6i = rcu_dereference(f6i->fib6_next)) {
800 bool matched = false;
803 if (cont && f6i->fib6_metric != metric) {
808 if (fib6_check_expired(f6i))
811 if (unlikely(f6i->nh)) {
812 struct fib6_nh_frl_arg arg = {
813 .flags = f6i->fib6_flags,
820 if (nexthop_is_blackhole(f6i->nh)) {
821 res->fib6_flags = RTF_REJECT;
822 res->fib6_type = RTN_BLACKHOLE;
824 res->nh = nexthop_fib6_nh(f6i->nh);
827 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
834 if (find_match(nh, f6i->fib6_flags, oif, strict,
841 res->fib6_flags = f6i->fib6_flags;
842 res->fib6_type = f6i->fib6_type;
847 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
848 struct fib6_info *rr_head, int oif, int strict,
849 bool *do_rr, struct fib6_result *res)
851 u32 metric = rr_head->fib6_metric;
852 struct fib6_info *cont = NULL;
855 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
856 oif, strict, do_rr, &mpri);
858 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
859 oif, strict, do_rr, &mpri);
861 if (res->f6i || !cont)
864 __find_rr_leaf(cont, NULL, metric, res, NULL,
865 oif, strict, do_rr, &mpri);
868 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
869 struct fib6_result *res, int strict)
871 struct fib6_info *leaf = rcu_dereference(fn->leaf);
872 struct fib6_info *rt0;
876 /* make sure this function or its helpers sets f6i */
879 if (!leaf || leaf == net->ipv6.fib6_null_entry)
882 rt0 = rcu_dereference(fn->rr_ptr);
886 /* Double check to make sure fn is not an intermediate node
887 * and fn->leaf does not points to its child's leaf
888 * (This might happen if all routes under fn are deleted from
889 * the tree and fib6_repair_tree() is called on the node.)
891 key_plen = rt0->fib6_dst.plen;
892 #ifdef CONFIG_IPV6_SUBTREES
893 if (rt0->fib6_src.plen)
894 key_plen = rt0->fib6_src.plen;
896 if (fn->fn_bit != key_plen)
899 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
901 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
903 /* no entries matched; do round-robin */
904 if (!next || next->fib6_metric != rt0->fib6_metric)
908 spin_lock_bh(&leaf->fib6_table->tb6_lock);
909 /* make sure next is not being deleted from the tree */
911 rcu_assign_pointer(fn->rr_ptr, next);
912 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
918 res->f6i = net->ipv6.fib6_null_entry;
919 res->nh = res->f6i->fib6_nh;
920 res->fib6_flags = res->f6i->fib6_flags;
921 res->fib6_type = res->f6i->fib6_type;
925 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
927 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
928 res->nh->fib_nh_gw_family;
931 #ifdef CONFIG_IPV6_ROUTE_INFO
932 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
933 const struct in6_addr *gwaddr)
935 struct net *net = dev_net(dev);
936 struct route_info *rinfo = (struct route_info *) opt;
937 struct in6_addr prefix_buf, *prefix;
938 struct fib6_table *table;
940 unsigned long lifetime;
941 struct fib6_info *rt;
943 if (len < sizeof(struct route_info)) {
947 /* Sanity check for prefix_len and length */
948 if (rinfo->length > 3) {
950 } else if (rinfo->prefix_len > 128) {
952 } else if (rinfo->prefix_len > 64) {
953 if (rinfo->length < 2) {
956 } else if (rinfo->prefix_len > 0) {
957 if (rinfo->length < 1) {
962 pref = rinfo->route_pref;
963 if (pref == ICMPV6_ROUTER_PREF_INVALID)
966 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
968 if (rinfo->length == 3)
969 prefix = (struct in6_addr *)rinfo->prefix;
971 /* this function is safe */
972 ipv6_addr_prefix(&prefix_buf,
973 (struct in6_addr *)rinfo->prefix,
975 prefix = &prefix_buf;
978 if (rinfo->prefix_len == 0)
979 rt = rt6_get_dflt_router(net, gwaddr, dev);
981 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
984 if (rt && !lifetime) {
985 ip6_del_rt(net, rt, false);
990 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993 rt->fib6_flags = RTF_ROUTEINFO |
994 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
997 table = rt->fib6_table;
998 spin_lock_bh(&table->tb6_lock);
1000 if (!addrconf_finite_timeout(lifetime)) {
1001 fib6_clean_expires(rt);
1002 fib6_remove_gc_list(rt);
1004 fib6_set_expires(rt, jiffies + HZ * lifetime);
1005 fib6_add_gc_list(rt);
1008 spin_unlock_bh(&table->tb6_lock);
1010 fib6_info_release(rt);
1017 * Misc support functions
1020 /* called with rcu_lock held */
1021 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1023 struct net_device *dev = res->nh->fib_nh_dev;
1025 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1026 /* for copies of local routes, dst->dev needs to be the
1027 * device if it is a master device, the master device if
1028 * device is enslaved, and the loopback as the default
1030 if (netif_is_l3_slave(dev) &&
1031 !rt6_need_strict(&res->f6i->fib6_dst.addr))
1032 dev = l3mdev_master_dev_rcu(dev);
1033 else if (!netif_is_l3_master(dev))
1034 dev = dev_net(dev)->loopback_dev;
1035 /* last case is netif_is_l3_master(dev) is true in which
1036 * case we want dev returned to be dev
1043 static const int fib6_prop[RTN_MAX + 1] = {
1047 [RTN_BROADCAST] = 0,
1049 [RTN_MULTICAST] = 0,
1050 [RTN_BLACKHOLE] = -EINVAL,
1051 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1052 [RTN_PROHIBIT] = -EACCES,
1053 [RTN_THROW] = -EAGAIN,
1054 [RTN_NAT] = -EINVAL,
1055 [RTN_XRESOLVE] = -EINVAL,
1058 static int ip6_rt_type_to_error(u8 fib6_type)
1060 return fib6_prop[fib6_type];
1063 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1065 unsigned short flags = 0;
1067 if (rt->dst_nocount)
1068 flags |= DST_NOCOUNT;
1069 if (rt->dst_nopolicy)
1070 flags |= DST_NOPOLICY;
1075 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1077 rt->dst.error = ip6_rt_type_to_error(fib6_type);
1079 switch (fib6_type) {
1081 rt->dst.output = dst_discard_out;
1082 rt->dst.input = dst_discard;
1085 rt->dst.output = ip6_pkt_prohibit_out;
1086 rt->dst.input = ip6_pkt_prohibit;
1089 case RTN_UNREACHABLE:
1091 rt->dst.output = ip6_pkt_discard_out;
1092 rt->dst.input = ip6_pkt_discard;
1097 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1099 struct fib6_info *f6i = res->f6i;
1101 if (res->fib6_flags & RTF_REJECT) {
1102 ip6_rt_init_dst_reject(rt, res->fib6_type);
1107 rt->dst.output = ip6_output;
1109 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1110 rt->dst.input = ip6_input;
1111 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1112 rt->dst.input = ip6_mc_input;
1114 rt->dst.input = ip6_forward;
1117 if (res->nh->fib_nh_lws) {
1118 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1119 lwtunnel_set_redirect(&rt->dst);
1122 rt->dst.lastuse = jiffies;
1125 /* Caller must already hold reference to @from */
1126 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1128 rt->rt6i_flags &= ~RTF_EXPIRES;
1129 rcu_assign_pointer(rt->from, from);
1130 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1133 /* Caller must already hold reference to f6i in result */
1134 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1136 const struct fib6_nh *nh = res->nh;
1137 const struct net_device *dev = nh->fib_nh_dev;
1138 struct fib6_info *f6i = res->f6i;
1140 ip6_rt_init_dst(rt, res);
1142 rt->rt6i_dst = f6i->fib6_dst;
1143 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1144 rt->rt6i_flags = res->fib6_flags;
1145 if (nh->fib_nh_gw_family) {
1146 rt->rt6i_gateway = nh->fib_nh_gw6;
1147 rt->rt6i_flags |= RTF_GATEWAY;
1149 rt6_set_from(rt, f6i);
1150 #ifdef CONFIG_IPV6_SUBTREES
1151 rt->rt6i_src = f6i->fib6_src;
1155 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1156 struct in6_addr *saddr)
1158 struct fib6_node *pn, *sn;
1160 if (fn->fn_flags & RTN_TL_ROOT)
1162 pn = rcu_dereference(fn->parent);
1163 sn = FIB6_SUBTREE(pn);
1165 fn = fib6_node_lookup(sn, NULL, saddr);
1168 if (fn->fn_flags & RTN_RTINFO)
1173 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1175 struct rt6_info *rt = *prt;
1177 if (dst_hold_safe(&rt->dst))
1180 rt = net->ipv6.ip6_null_entry;
1189 /* called with rcu_lock held */
1190 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1192 struct net_device *dev = res->nh->fib_nh_dev;
1193 struct fib6_info *f6i = res->f6i;
1194 unsigned short flags;
1195 struct rt6_info *nrt;
1197 if (!fib6_info_hold_safe(f6i))
1200 flags = fib6_info_dst_flags(f6i);
1201 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1203 fib6_info_release(f6i);
1207 ip6_rt_copy_init(nrt, res);
1211 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1212 dst_hold(&nrt->dst);
1216 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
1217 struct fib6_table *table,
1219 const struct sk_buff *skb,
1222 struct fib6_result res = {};
1223 struct fib6_node *fn;
1224 struct rt6_info *rt;
1227 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1229 res.f6i = rcu_dereference(fn->leaf);
1231 res.f6i = net->ipv6.fib6_null_entry;
1233 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1236 if (res.f6i == net->ipv6.fib6_null_entry) {
1237 fn = fib6_backtrack(fn, &fl6->saddr);
1241 rt = net->ipv6.ip6_null_entry;
1244 } else if (res.fib6_flags & RTF_REJECT) {
1248 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1249 fl6->flowi6_oif != 0, skb, flags);
1251 /* Search through exception table */
1252 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1254 if (ip6_hold_safe(net, &rt))
1255 dst_use_noref(&rt->dst, jiffies);
1258 rt = ip6_create_rt_rcu(&res);
1262 trace_fib6_table_lookup(net, &res, table, fl6);
1269 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1270 const struct sk_buff *skb, int flags)
1272 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1274 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1276 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1277 const struct in6_addr *saddr, int oif,
1278 const struct sk_buff *skb, int strict)
1280 struct flowi6 fl6 = {
1284 struct dst_entry *dst;
1285 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1288 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1289 flags |= RT6_LOOKUP_F_HAS_SADDR;
1292 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1293 if (dst->error == 0)
1294 return dst_rt6_info(dst);
1300 EXPORT_SYMBOL(rt6_lookup);
1302 /* ip6_ins_rt is called with FREE table->tb6_lock.
1303 * It takes new route entry, the addition fails by any reason the
1304 * route is released.
1305 * Caller must hold dst before calling it.
1308 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1309 struct netlink_ext_ack *extack)
1312 struct fib6_table *table;
1314 table = rt->fib6_table;
1315 spin_lock_bh(&table->tb6_lock);
1316 err = fib6_add(&table->tb6_root, rt, info, extack);
1317 spin_unlock_bh(&table->tb6_lock);
1322 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1324 struct nl_info info = { .nl_net = net, };
1326 return __ip6_ins_rt(rt, &info, NULL);
1329 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1330 const struct in6_addr *daddr,
1331 const struct in6_addr *saddr)
1333 struct fib6_info *f6i = res->f6i;
1334 struct net_device *dev;
1335 struct rt6_info *rt;
1341 if (!fib6_info_hold_safe(f6i))
1344 dev = ip6_rt_get_dev_rcu(res);
1345 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1347 fib6_info_release(f6i);
1351 ip6_rt_copy_init(rt, res);
1352 rt->rt6i_flags |= RTF_CACHE;
1353 rt->rt6i_dst.addr = *daddr;
1354 rt->rt6i_dst.plen = 128;
1356 if (!rt6_is_gw_or_nonexthop(res)) {
1357 if (f6i->fib6_dst.plen != 128 &&
1358 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1359 rt->rt6i_flags |= RTF_ANYCAST;
1360 #ifdef CONFIG_IPV6_SUBTREES
1361 if (rt->rt6i_src.plen && saddr) {
1362 rt->rt6i_src.addr = *saddr;
1363 rt->rt6i_src.plen = 128;
1371 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1373 struct fib6_info *f6i = res->f6i;
1374 unsigned short flags = fib6_info_dst_flags(f6i);
1375 struct net_device *dev;
1376 struct rt6_info *pcpu_rt;
1378 if (!fib6_info_hold_safe(f6i))
1382 dev = ip6_rt_get_dev_rcu(res);
1383 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
1386 fib6_info_release(f6i);
1389 ip6_rt_copy_init(pcpu_rt, res);
1390 pcpu_rt->rt6i_flags |= RTF_PCPU;
1393 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1398 static bool rt6_is_valid(const struct rt6_info *rt6)
1400 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1403 /* It should be called with rcu_read_lock() acquired */
1404 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1406 struct rt6_info *pcpu_rt;
1408 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1410 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1411 struct rt6_info *prev, **p;
1413 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1414 /* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
1415 prev = xchg(p, NULL);
1417 dst_dev_put(&prev->dst);
1418 dst_release(&prev->dst);
1427 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1428 const struct fib6_result *res)
1430 struct rt6_info *pcpu_rt, *prev, **p;
1432 pcpu_rt = ip6_rt_pcpu_alloc(res);
1436 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1437 prev = cmpxchg(p, NULL, pcpu_rt);
1440 if (res->f6i->fib6_destroying) {
1441 struct fib6_info *from;
1443 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1444 fib6_info_release(from);
1450 /* exception hash table implementation
1452 static DEFINE_SPINLOCK(rt6_exception_lock);
1454 /* Remove rt6_ex from hash table and free the memory
1455 * Caller must hold rt6_exception_lock
1457 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1458 struct rt6_exception *rt6_ex)
1460 struct fib6_info *from;
1463 if (!bucket || !rt6_ex)
1466 net = dev_net(rt6_ex->rt6i->dst.dev);
1467 net->ipv6.rt6_stats->fib_rt_cache--;
1469 /* purge completely the exception to allow releasing the held resources:
1470 * some [sk] cache may keep the dst around for unlimited time
1472 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1473 fib6_info_release(from);
1474 dst_dev_put(&rt6_ex->rt6i->dst);
1476 hlist_del_rcu(&rt6_ex->hlist);
1477 dst_release(&rt6_ex->rt6i->dst);
1478 kfree_rcu(rt6_ex, rcu);
1479 WARN_ON_ONCE(!bucket->depth);
1483 /* Remove oldest rt6_ex in bucket and free the memory
1484 * Caller must hold rt6_exception_lock
1486 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1488 struct rt6_exception *rt6_ex, *oldest = NULL;
1493 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1494 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1497 rt6_remove_exception(bucket, oldest);
1500 static u32 rt6_exception_hash(const struct in6_addr *dst,
1501 const struct in6_addr *src)
1503 static siphash_aligned_key_t rt6_exception_key;
1505 struct in6_addr dst;
1506 struct in6_addr src;
1507 } __aligned(SIPHASH_ALIGNMENT) combined = {
1512 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
1514 #ifdef CONFIG_IPV6_SUBTREES
1516 combined.src = *src;
1518 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1520 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1523 /* Helper function to find the cached rt in the hash table
1524 * and update bucket pointer to point to the bucket for this
1525 * (daddr, saddr) pair
1526 * Caller must hold rt6_exception_lock
1528 static struct rt6_exception *
1529 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1530 const struct in6_addr *daddr,
1531 const struct in6_addr *saddr)
1533 struct rt6_exception *rt6_ex;
1536 if (!(*bucket) || !daddr)
1539 hval = rt6_exception_hash(daddr, saddr);
1542 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1543 struct rt6_info *rt6 = rt6_ex->rt6i;
1544 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1546 #ifdef CONFIG_IPV6_SUBTREES
1547 if (matched && saddr)
1548 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1556 /* Helper function to find the cached rt in the hash table
1557 * and update bucket pointer to point to the bucket for this
1558 * (daddr, saddr) pair
1559 * Caller must hold rcu_read_lock()
1561 static struct rt6_exception *
1562 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1563 const struct in6_addr *daddr,
1564 const struct in6_addr *saddr)
1566 struct rt6_exception *rt6_ex;
1569 WARN_ON_ONCE(!rcu_read_lock_held());
1571 if (!(*bucket) || !daddr)
1574 hval = rt6_exception_hash(daddr, saddr);
1577 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1578 struct rt6_info *rt6 = rt6_ex->rt6i;
1579 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1581 #ifdef CONFIG_IPV6_SUBTREES
1582 if (matched && saddr)
1583 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1591 static unsigned int fib6_mtu(const struct fib6_result *res)
1593 const struct fib6_nh *nh = res->nh;
1596 if (res->f6i->fib6_pmtu) {
1597 mtu = res->f6i->fib6_pmtu;
1599 struct net_device *dev = nh->fib_nh_dev;
1600 struct inet6_dev *idev;
1603 idev = __in6_dev_get(dev);
1604 mtu = READ_ONCE(idev->cnf.mtu6);
1608 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1610 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1613 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1615 /* used when the flushed bit is not relevant, only access to the bucket
1616 * (ie., all bucket users except rt6_insert_exception);
1618 * called under rcu lock; sometimes called with rt6_exception_lock held
1621 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1624 struct rt6_exception_bucket *bucket;
1627 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1628 lockdep_is_held(lock));
1630 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1632 /* remove bucket flushed bit if set */
1634 unsigned long p = (unsigned long)bucket;
1636 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1637 bucket = (struct rt6_exception_bucket *)p;
1643 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1645 unsigned long p = (unsigned long)bucket;
1647 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1650 /* called with rt6_exception_lock held */
1651 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1654 struct rt6_exception_bucket *bucket;
1657 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1658 lockdep_is_held(lock));
1660 p = (unsigned long)bucket;
1661 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1662 bucket = (struct rt6_exception_bucket *)p;
1663 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1666 static int rt6_insert_exception(struct rt6_info *nrt,
1667 const struct fib6_result *res)
1669 struct net *net = dev_net(nrt->dst.dev);
1670 struct rt6_exception_bucket *bucket;
1671 struct fib6_info *f6i = res->f6i;
1672 struct in6_addr *src_key = NULL;
1673 struct rt6_exception *rt6_ex;
1674 struct fib6_nh *nh = res->nh;
1678 spin_lock_bh(&rt6_exception_lock);
1680 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1681 lockdep_is_held(&rt6_exception_lock));
1683 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1689 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1690 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1695 #ifdef CONFIG_IPV6_SUBTREES
1696 /* fib6_src.plen != 0 indicates f6i is in subtree
1697 * and exception table is indexed by a hash of
1698 * both fib6_dst and fib6_src.
1699 * Otherwise, the exception table is indexed by
1700 * a hash of only fib6_dst.
1702 if (f6i->fib6_src.plen)
1703 src_key = &nrt->rt6i_src.addr;
1705 /* rt6_mtu_change() might lower mtu on f6i.
1706 * Only insert this exception route if its mtu
1707 * is less than f6i's mtu value.
1709 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1714 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1717 rt6_remove_exception(bucket, rt6_ex);
1719 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1725 rt6_ex->stamp = jiffies;
1726 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1728 net->ipv6.rt6_stats->fib_rt_cache++;
1730 /* Randomize max depth to avoid some side channels attacks. */
1731 max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH);
1732 while (bucket->depth > max_depth)
1733 rt6_exception_remove_oldest(bucket);
1736 spin_unlock_bh(&rt6_exception_lock);
1738 /* Update fn->fn_sernum to invalidate all cached dst */
1740 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1741 fib6_update_sernum(net, f6i);
1742 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1743 fib6_force_start_gc(net);
1749 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1751 struct rt6_exception_bucket *bucket;
1752 struct rt6_exception *rt6_ex;
1753 struct hlist_node *tmp;
1756 spin_lock_bh(&rt6_exception_lock);
1758 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1762 /* Prevent rt6_insert_exception() to recreate the bucket list */
1764 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1766 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1767 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1769 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1770 rt6_remove_exception(bucket, rt6_ex);
1772 WARN_ON_ONCE(!from && bucket->depth);
1776 spin_unlock_bh(&rt6_exception_lock);
1779 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1781 struct fib6_info *f6i = arg;
1783 fib6_nh_flush_exceptions(nh, f6i);
1788 void rt6_flush_exceptions(struct fib6_info *f6i)
1791 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1794 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1797 /* Find cached rt in the hash table inside passed in rt
1798 * Caller has to hold rcu_read_lock()
1800 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1801 const struct in6_addr *daddr,
1802 const struct in6_addr *saddr)
1804 const struct in6_addr *src_key = NULL;
1805 struct rt6_exception_bucket *bucket;
1806 struct rt6_exception *rt6_ex;
1807 struct rt6_info *ret = NULL;
1809 #ifdef CONFIG_IPV6_SUBTREES
1810 /* fib6i_src.plen != 0 indicates f6i is in subtree
1811 * and exception table is indexed by a hash of
1812 * both fib6_dst and fib6_src.
1813 * However, the src addr used to create the hash
1814 * might not be exactly the passed in saddr which
1815 * is a /128 addr from the flow.
1816 * So we need to use f6i->fib6_src to redo lookup
1817 * if the passed in saddr does not find anything.
1818 * (See the logic in ip6_rt_cache_alloc() on how
1819 * rt->rt6i_src is updated.)
1821 if (res->f6i->fib6_src.plen)
1825 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1826 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1828 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1831 #ifdef CONFIG_IPV6_SUBTREES
1832 /* Use fib6_src as src_key and redo lookup */
1833 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1834 src_key = &res->f6i->fib6_src.addr;
1842 /* Remove the passed in cached rt from the hash table that contains it */
1843 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1844 const struct rt6_info *rt)
1846 const struct in6_addr *src_key = NULL;
1847 struct rt6_exception_bucket *bucket;
1848 struct rt6_exception *rt6_ex;
1851 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1854 spin_lock_bh(&rt6_exception_lock);
1855 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1857 #ifdef CONFIG_IPV6_SUBTREES
1858 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1859 * and exception table is indexed by a hash of
1860 * both rt6i_dst and rt6i_src.
1861 * Otherwise, the exception table is indexed by
1862 * a hash of only rt6i_dst.
1865 src_key = &rt->rt6i_src.addr;
1867 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1871 rt6_remove_exception(bucket, rt6_ex);
1877 spin_unlock_bh(&rt6_exception_lock);
1881 struct fib6_nh_excptn_arg {
1882 struct rt6_info *rt;
1886 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1888 struct fib6_nh_excptn_arg *arg = _arg;
1891 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1898 static int rt6_remove_exception_rt(struct rt6_info *rt)
1900 struct fib6_info *from;
1902 from = rcu_dereference(rt->from);
1903 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1907 struct fib6_nh_excptn_arg arg = {
1909 .plen = from->fib6_src.plen
1913 /* rc = 1 means an entry was found */
1914 rc = nexthop_for_each_fib6_nh(from->nh,
1915 rt6_nh_remove_exception_rt,
1917 return rc ? 0 : -ENOENT;
1920 return fib6_nh_remove_exception(from->fib6_nh,
1921 from->fib6_src.plen, rt);
1924 /* Find rt6_ex which contains the passed in rt cache and
1927 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1928 const struct rt6_info *rt)
1930 const struct in6_addr *src_key = NULL;
1931 struct rt6_exception_bucket *bucket;
1932 struct rt6_exception *rt6_ex;
1934 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1935 #ifdef CONFIG_IPV6_SUBTREES
1936 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1937 * and exception table is indexed by a hash of
1938 * both rt6i_dst and rt6i_src.
1939 * Otherwise, the exception table is indexed by
1940 * a hash of only rt6i_dst.
1943 src_key = &rt->rt6i_src.addr;
1945 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1947 rt6_ex->stamp = jiffies;
1950 struct fib6_nh_match_arg {
1951 const struct net_device *dev;
1952 const struct in6_addr *gw;
1953 struct fib6_nh *match;
1956 /* determine if fib6_nh has given device and gateway */
1957 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1959 struct fib6_nh_match_arg *arg = _arg;
1961 if (arg->dev != nh->fib_nh_dev ||
1962 (arg->gw && !nh->fib_nh_gw_family) ||
1963 (!arg->gw && nh->fib_nh_gw_family) ||
1964 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1969 /* found a match, break the loop */
1973 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1975 struct fib6_info *from;
1976 struct fib6_nh *fib6_nh;
1980 from = rcu_dereference(rt->from);
1981 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1985 struct fib6_nh_match_arg arg = {
1987 .gw = &rt->rt6i_gateway,
1990 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1994 fib6_nh = arg.match;
1996 fib6_nh = from->fib6_nh;
1998 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
2003 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
2004 struct rt6_info *rt, int mtu)
2006 /* If the new MTU is lower than the route PMTU, this new MTU will be the
2007 * lowest MTU in the path: always allow updating the route PMTU to
2008 * reflect PMTU decreases.
2010 * If the new MTU is higher, and the route PMTU is equal to the local
2011 * MTU, this means the old MTU is the lowest in the path, so allow
2012 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2016 if (dst_mtu(&rt->dst) >= mtu)
2019 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2025 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2026 const struct fib6_nh *nh, int mtu)
2028 struct rt6_exception_bucket *bucket;
2029 struct rt6_exception *rt6_ex;
2032 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2036 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2037 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2038 struct rt6_info *entry = rt6_ex->rt6i;
2040 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2041 * route), the metrics of its rt->from have already
2044 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2045 rt6_mtu_change_route_allowed(idev, entry, mtu))
2046 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2052 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2054 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2055 const struct in6_addr *gateway)
2057 struct rt6_exception_bucket *bucket;
2058 struct rt6_exception *rt6_ex;
2059 struct hlist_node *tmp;
2062 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2065 spin_lock_bh(&rt6_exception_lock);
2066 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2068 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2069 hlist_for_each_entry_safe(rt6_ex, tmp,
2070 &bucket->chain, hlist) {
2071 struct rt6_info *entry = rt6_ex->rt6i;
2073 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2074 RTF_CACHE_GATEWAY &&
2075 ipv6_addr_equal(gateway,
2076 &entry->rt6i_gateway)) {
2077 rt6_remove_exception(bucket, rt6_ex);
2084 spin_unlock_bh(&rt6_exception_lock);
2087 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2088 struct rt6_exception *rt6_ex,
2089 struct fib6_gc_args *gc_args,
2092 struct rt6_info *rt = rt6_ex->rt6i;
2094 /* we are pruning and obsoleting aged-out and non gateway exceptions
2095 * even if others have still references to them, so that on next
2096 * dst_check() such references can be dropped.
2097 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2098 * expired, independently from their aging, as per RFC 8201 section 4
2100 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2101 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2102 pr_debug("aging clone %p\n", rt);
2103 rt6_remove_exception(bucket, rt6_ex);
2106 } else if (time_after(jiffies, rt->dst.expires)) {
2107 pr_debug("purging expired route %p\n", rt);
2108 rt6_remove_exception(bucket, rt6_ex);
2112 if (rt->rt6i_flags & RTF_GATEWAY) {
2113 struct neighbour *neigh;
2115 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2117 if (!(neigh && (neigh->flags & NTF_ROUTER))) {
2118 pr_debug("purging route %p via non-router but gateway\n",
2120 rt6_remove_exception(bucket, rt6_ex);
2128 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2129 struct fib6_gc_args *gc_args,
2132 struct rt6_exception_bucket *bucket;
2133 struct rt6_exception *rt6_ex;
2134 struct hlist_node *tmp;
2137 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2141 spin_lock(&rt6_exception_lock);
2142 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2144 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2145 hlist_for_each_entry_safe(rt6_ex, tmp,
2146 &bucket->chain, hlist) {
2147 rt6_age_examine_exception(bucket, rt6_ex,
2153 spin_unlock(&rt6_exception_lock);
2154 rcu_read_unlock_bh();
2157 struct fib6_nh_age_excptn_arg {
2158 struct fib6_gc_args *gc_args;
2162 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2164 struct fib6_nh_age_excptn_arg *arg = _arg;
2166 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2170 void rt6_age_exceptions(struct fib6_info *f6i,
2171 struct fib6_gc_args *gc_args,
2175 struct fib6_nh_age_excptn_arg arg = {
2180 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2183 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2187 /* must be called with rcu lock held */
2188 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2189 struct flowi6 *fl6, struct fib6_result *res, int strict)
2191 struct fib6_node *fn, *saved_fn;
2193 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2197 rt6_select(net, fn, oif, res, strict);
2198 if (res->f6i == net->ipv6.fib6_null_entry) {
2199 fn = fib6_backtrack(fn, &fl6->saddr);
2201 goto redo_rt6_select;
2202 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2203 /* also consider unreachable route */
2204 strict &= ~RT6_LOOKUP_F_REACHABLE;
2206 goto redo_rt6_select;
2210 trace_fib6_table_lookup(net, res, table, fl6);
2215 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2216 int oif, struct flowi6 *fl6,
2217 const struct sk_buff *skb, int flags)
2219 struct fib6_result res = {};
2220 struct rt6_info *rt = NULL;
2223 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2224 !rcu_read_lock_held());
2226 strict |= flags & RT6_LOOKUP_F_IFACE;
2227 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2228 if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
2229 strict |= RT6_LOOKUP_F_REACHABLE;
2233 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2234 if (res.f6i == net->ipv6.fib6_null_entry)
2237 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2239 /*Search through exception table */
2240 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2243 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2244 !res.nh->fib_nh_gw_family)) {
2245 /* Create a RTF_CACHE clone which will not be
2246 * owned by the fib6 tree. It is for the special case where
2247 * the daddr in the skb during the neighbor look-up is different
2248 * from the fl6->daddr used to look-up route here.
2250 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2253 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2254 * As rt6_uncached_list_add() does not consume refcnt,
2255 * this refcnt is always returned to the caller even
2256 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2258 rt6_uncached_list_add(rt);
2264 /* Get a percpu copy */
2266 rt = rt6_get_pcpu_route(&res);
2269 rt = rt6_make_pcpu_route(net, &res);
2275 rt = net->ipv6.ip6_null_entry;
2276 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2277 ip6_hold_safe(net, &rt);
2282 EXPORT_SYMBOL_GPL(ip6_pol_route);
2284 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
2285 struct fib6_table *table,
2287 const struct sk_buff *skb,
2290 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2293 struct dst_entry *ip6_route_input_lookup(struct net *net,
2294 struct net_device *dev,
2296 const struct sk_buff *skb,
2299 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2300 flags |= RT6_LOOKUP_F_IFACE;
2302 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2304 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2306 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2307 struct flow_keys *keys,
2308 struct flow_keys *flkeys)
2310 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2311 const struct ipv6hdr *key_iph = outer_iph;
2312 struct flow_keys *_flkeys = flkeys;
2313 const struct ipv6hdr *inner_iph;
2314 const struct icmp6hdr *icmph;
2315 struct ipv6hdr _inner_iph;
2316 struct icmp6hdr _icmph;
2318 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2321 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2322 sizeof(_icmph), &_icmph);
2326 if (!icmpv6_is_err(icmph->icmp6_type))
2329 inner_iph = skb_header_pointer(skb,
2330 skb_transport_offset(skb) + sizeof(*icmph),
2331 sizeof(_inner_iph), &_inner_iph);
2335 key_iph = inner_iph;
2339 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2340 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2341 keys->tags.flow_label = _flkeys->tags.flow_label;
2342 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2344 keys->addrs.v6addrs.src = key_iph->saddr;
2345 keys->addrs.v6addrs.dst = key_iph->daddr;
2346 keys->tags.flow_label = ip6_flowlabel(key_iph);
2347 keys->basic.ip_proto = key_iph->nexthdr;
2351 static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2352 const struct sk_buff *skb,
2355 u32 hash_fields = ip6_multipath_hash_fields(net);
2356 struct flow_keys keys, hash_keys;
2358 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2361 memset(&hash_keys, 0, sizeof(hash_keys));
2362 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2364 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2365 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2366 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2367 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2368 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2369 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2370 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2371 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2372 hash_keys.tags.flow_label = keys.tags.flow_label;
2373 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2374 hash_keys.ports.src = keys.ports.src;
2375 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2376 hash_keys.ports.dst = keys.ports.dst;
2378 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2379 return flow_hash_from_keys(&hash_keys);
2382 static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2383 const struct sk_buff *skb,
2386 u32 hash_fields = ip6_multipath_hash_fields(net);
2387 struct flow_keys keys, hash_keys;
2389 /* We assume the packet carries an encapsulation, but if none was
2390 * encountered during dissection of the outer flow, then there is no
2391 * point in calling the flow dissector again.
2396 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2399 memset(&hash_keys, 0, sizeof(hash_keys));
2400 skb_flow_dissect_flow_keys(skb, &keys, 0);
2402 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2405 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2406 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2407 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2408 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2409 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2410 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2411 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2412 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2413 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2414 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2415 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2416 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2417 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2418 hash_keys.tags.flow_label = keys.tags.flow_label;
2421 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2422 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2423 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2424 hash_keys.ports.src = keys.ports.src;
2425 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2426 hash_keys.ports.dst = keys.ports.dst;
2428 return flow_hash_from_keys(&hash_keys);
2431 static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2432 const struct sk_buff *skb)
2434 u32 mhash, mhash_inner;
2435 bool has_inner = true;
2437 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2438 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2440 return jhash_2words(mhash, mhash_inner, 0);
2443 static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2444 const struct flowi6 *fl6)
2446 u32 hash_fields = ip6_multipath_hash_fields(net);
2447 struct flow_keys hash_keys;
2449 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2452 memset(&hash_keys, 0, sizeof(hash_keys));
2453 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2454 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2455 hash_keys.addrs.v6addrs.src = fl6->saddr;
2456 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2457 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2458 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2459 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2460 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2461 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2462 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2463 hash_keys.ports.src = fl6->fl6_sport;
2464 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2465 hash_keys.ports.dst = fl6->fl6_dport;
2467 return flow_hash_from_keys(&hash_keys);
2470 /* if skb is set it will be used and fl6 can be NULL */
2471 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2472 const struct sk_buff *skb, struct flow_keys *flkeys)
2474 struct flow_keys hash_keys;
2477 switch (ip6_multipath_hash_policy(net)) {
2479 memset(&hash_keys, 0, sizeof(hash_keys));
2480 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2482 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2484 hash_keys.addrs.v6addrs.src = fl6->saddr;
2485 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2486 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2487 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2489 mhash = flow_hash_from_keys(&hash_keys);
2493 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2494 struct flow_keys keys;
2496 /* short-circuit if we already have L4 hash present */
2498 return skb_get_hash_raw(skb) >> 1;
2500 memset(&hash_keys, 0, sizeof(hash_keys));
2503 skb_flow_dissect_flow_keys(skb, &keys, flag);
2506 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2507 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2508 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2509 hash_keys.ports.src = flkeys->ports.src;
2510 hash_keys.ports.dst = flkeys->ports.dst;
2511 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2513 memset(&hash_keys, 0, sizeof(hash_keys));
2514 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2515 hash_keys.addrs.v6addrs.src = fl6->saddr;
2516 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2517 hash_keys.ports.src = fl6->fl6_sport;
2518 hash_keys.ports.dst = fl6->fl6_dport;
2519 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2521 mhash = flow_hash_from_keys(&hash_keys);
2524 memset(&hash_keys, 0, sizeof(hash_keys));
2525 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2527 struct flow_keys keys;
2530 skb_flow_dissect_flow_keys(skb, &keys, 0);
2534 /* Inner can be v4 or v6 */
2535 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2536 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2537 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2538 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2539 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2540 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2541 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2542 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2543 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2544 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2546 /* Same as case 0 */
2547 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2548 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2551 /* Same as case 0 */
2552 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2553 hash_keys.addrs.v6addrs.src = fl6->saddr;
2554 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2555 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2556 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2558 mhash = flow_hash_from_keys(&hash_keys);
2562 mhash = rt6_multipath_custom_hash_skb(net, skb);
2564 mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2571 /* Called with rcu held */
2572 void ip6_route_input(struct sk_buff *skb)
2574 const struct ipv6hdr *iph = ipv6_hdr(skb);
2575 struct net *net = dev_net(skb->dev);
2576 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2577 struct ip_tunnel_info *tun_info;
2578 struct flowi6 fl6 = {
2579 .flowi6_iif = skb->dev->ifindex,
2580 .daddr = iph->daddr,
2581 .saddr = iph->saddr,
2582 .flowlabel = ip6_flowinfo(iph),
2583 .flowi6_mark = skb->mark,
2584 .flowi6_proto = iph->nexthdr,
2586 struct flow_keys *flkeys = NULL, _flkeys;
2588 tun_info = skb_tunnel_info(skb);
2589 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2590 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2592 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2595 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2596 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2598 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2602 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
2603 struct fib6_table *table,
2605 const struct sk_buff *skb,
2608 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2611 static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2612 const struct sock *sk,
2618 if (ipv6_addr_type(&fl6->daddr) &
2619 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2620 struct dst_entry *dst;
2622 /* This function does not take refcnt on the dst */
2623 dst = l3mdev_link_scope_lookup(net, fl6);
2628 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2630 flags |= RT6_LOOKUP_F_DST_NOREF;
2631 any_src = ipv6_addr_any(&fl6->saddr);
2632 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2633 (fl6->flowi6_oif && any_src))
2634 flags |= RT6_LOOKUP_F_IFACE;
2637 flags |= RT6_LOOKUP_F_HAS_SADDR;
2639 flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs));
2641 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2644 struct dst_entry *ip6_route_output_flags(struct net *net,
2645 const struct sock *sk,
2649 struct dst_entry *dst;
2650 struct rt6_info *rt6;
2653 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2654 rt6 = dst_rt6_info(dst);
2655 /* For dst cached in uncached_list, refcnt is already taken. */
2656 if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) {
2657 dst = &net->ipv6.ip6_null_entry->dst;
2664 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2666 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2668 struct rt6_info *rt, *ort = dst_rt6_info(dst_orig);
2669 struct net_device *loopback_dev = net->loopback_dev;
2670 struct dst_entry *new = NULL;
2672 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev,
2673 DST_OBSOLETE_DEAD, 0);
2676 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2680 new->input = dst_discard;
2681 new->output = dst_discard_out;
2683 dst_copy_metrics(new, &ort->dst);
2685 rt->rt6i_idev = in6_dev_get(loopback_dev);
2686 rt->rt6i_gateway = ort->rt6i_gateway;
2687 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2689 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2690 #ifdef CONFIG_IPV6_SUBTREES
2691 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2695 dst_release(dst_orig);
2696 return new ? new : ERR_PTR(-ENOMEM);
2700 * Destination cache support functions
2703 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2707 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2710 if (fib6_check_expired(f6i))
2716 static struct dst_entry *rt6_check(struct rt6_info *rt,
2717 struct fib6_info *from,
2722 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2723 rt_cookie != cookie)
2726 if (rt6_check_expired(rt))
2732 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2733 struct fib6_info *from,
2736 if (!__rt6_check_expired(rt) &&
2737 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2738 fib6_check(from, cookie))
2744 INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2747 struct dst_entry *dst_ret;
2748 struct fib6_info *from;
2749 struct rt6_info *rt;
2751 rt = dst_rt6_info(dst);
2754 return rt6_is_valid(rt) ? dst : NULL;
2758 /* All IPV6 dsts are created with ->obsolete set to the value
2759 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2760 * into this function always.
2763 from = rcu_dereference(rt->from);
2765 if (from && (rt->rt6i_flags & RTF_PCPU ||
2766 unlikely(!list_empty(&rt->dst.rt_uncached))))
2767 dst_ret = rt6_dst_from_check(rt, from, cookie);
2769 dst_ret = rt6_check(rt, from, cookie);
2775 EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
2777 static void ip6_negative_advice(struct sock *sk,
2778 struct dst_entry *dst)
2780 struct rt6_info *rt = dst_rt6_info(dst);
2782 if (rt->rt6i_flags & RTF_CACHE) {
2784 if (rt6_check_expired(rt)) {
2785 /* counteract the dst_release() in sk_dst_reset() */
2789 rt6_remove_exception_rt(rt);
2797 static void ip6_link_failure(struct sk_buff *skb)
2799 struct rt6_info *rt;
2801 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2803 rt = dst_rt6_info(skb_dst(skb));
2806 if (rt->rt6i_flags & RTF_CACHE) {
2807 rt6_remove_exception_rt(rt);
2809 struct fib6_info *from;
2810 struct fib6_node *fn;
2812 from = rcu_dereference(rt->from);
2814 fn = rcu_dereference(from->fib6_node);
2815 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2816 WRITE_ONCE(fn->fn_sernum, -1);
2823 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2825 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2826 struct fib6_info *from;
2829 from = rcu_dereference(rt0->from);
2831 rt0->dst.expires = from->expires;
2835 dst_set_expires(&rt0->dst, timeout);
2836 rt0->rt6i_flags |= RTF_EXPIRES;
2839 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2841 struct net *net = dev_net(rt->dst.dev);
2843 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2844 rt->rt6i_flags |= RTF_MODIFIED;
2845 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2848 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2850 return !(rt->rt6i_flags & RTF_CACHE) &&
2851 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2854 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2855 const struct ipv6hdr *iph, u32 mtu,
2858 const struct in6_addr *daddr, *saddr;
2859 struct rt6_info *rt6 = dst_rt6_info(dst);
2861 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2862 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2863 * [see also comment in rt6_mtu_change_route()]
2867 daddr = &iph->daddr;
2868 saddr = &iph->saddr;
2870 daddr = &sk->sk_v6_daddr;
2871 saddr = &inet6_sk(sk)->saddr;
2878 dst_confirm_neigh(dst, daddr);
2880 if (mtu < IPV6_MIN_MTU)
2882 if (mtu >= dst_mtu(dst))
2885 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2886 rt6_do_update_pmtu(rt6, mtu);
2887 /* update rt6_ex->stamp for cache */
2888 if (rt6->rt6i_flags & RTF_CACHE)
2889 rt6_update_exception_stamp_rt(rt6);
2891 struct fib6_result res = {};
2892 struct rt6_info *nrt6;
2895 res.f6i = rcu_dereference(rt6->from);
2899 res.fib6_flags = res.f6i->fib6_flags;
2900 res.fib6_type = res.f6i->fib6_type;
2903 struct fib6_nh_match_arg arg = {
2905 .gw = &rt6->rt6i_gateway,
2908 nexthop_for_each_fib6_nh(res.f6i->nh,
2909 fib6_nh_find_match, &arg);
2911 /* fib6_info uses a nexthop that does not have fib6_nh
2912 * using the dst->dev + gw. Should be impossible.
2919 res.nh = res.f6i->fib6_nh;
2922 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2924 rt6_do_update_pmtu(nrt6, mtu);
2925 if (rt6_insert_exception(nrt6, &res))
2926 dst_release_immediate(&nrt6->dst);
2933 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2934 struct sk_buff *skb, u32 mtu,
2937 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2941 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2942 int oif, u32 mark, kuid_t uid)
2944 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2945 struct dst_entry *dst;
2946 struct flowi6 fl6 = {
2948 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2949 .daddr = iph->daddr,
2950 .saddr = iph->saddr,
2951 .flowlabel = ip6_flowinfo(iph),
2955 dst = ip6_route_output(net, NULL, &fl6);
2957 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
2960 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2962 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2964 int oif = sk->sk_bound_dev_if;
2965 struct dst_entry *dst;
2967 if (!oif && skb->dev)
2968 oif = l3mdev_master_ifindex(skb->dev);
2970 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark),
2973 dst = __sk_dst_get(sk);
2974 if (!dst || !dst->obsolete ||
2975 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2979 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2980 ip6_datagram_dst_update(sk, false);
2983 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2985 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2986 const struct flowi6 *fl6)
2988 #ifdef CONFIG_IPV6_SUBTREES
2989 struct ipv6_pinfo *np = inet6_sk(sk);
2992 ip6_dst_store(sk, dst,
2993 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2994 &sk->sk_v6_daddr : NULL,
2995 #ifdef CONFIG_IPV6_SUBTREES
2996 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
3002 static bool ip6_redirect_nh_match(const struct fib6_result *res,
3004 const struct in6_addr *gw,
3005 struct rt6_info **ret)
3007 const struct fib6_nh *nh = res->nh;
3009 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
3010 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
3013 /* rt_cache's gateway might be different from its 'parent'
3014 * in the case of an ip redirect.
3015 * So we keep searching in the exception table if the gateway
3018 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
3019 struct rt6_info *rt_cache;
3021 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
3023 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
3032 struct fib6_nh_rd_arg {
3033 struct fib6_result *res;
3035 const struct in6_addr *gw;
3036 struct rt6_info **ret;
3039 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3041 struct fib6_nh_rd_arg *arg = _arg;
3044 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3047 /* Handle redirects */
3048 struct ip6rd_flowi {
3050 struct in6_addr gateway;
3053 INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
3054 struct fib6_table *table,
3056 const struct sk_buff *skb,
3059 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
3060 struct rt6_info *ret = NULL;
3061 struct fib6_result res = {};
3062 struct fib6_nh_rd_arg arg = {
3065 .gw = &rdfl->gateway,
3068 struct fib6_info *rt;
3069 struct fib6_node *fn;
3071 /* Get the "current" route for this destination and
3072 * check if the redirect has come from appropriate router.
3074 * RFC 4861 specifies that redirects should only be
3075 * accepted if they come from the nexthop to the target.
3076 * Due to the way the routes are chosen, this notion
3077 * is a bit fuzzy and one might need to check all possible
3082 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
3084 for_each_fib6_node_rt_rcu(fn) {
3086 if (fib6_check_expired(rt))
3088 if (rt->fib6_flags & RTF_REJECT)
3090 if (unlikely(rt->nh)) {
3091 if (nexthop_is_blackhole(rt->nh))
3093 /* on match, res->nh is filled in and potentially ret */
3094 if (nexthop_for_each_fib6_nh(rt->nh,
3095 fib6_nh_redirect_match,
3099 res.nh = rt->fib6_nh;
3100 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3107 rt = net->ipv6.fib6_null_entry;
3108 else if (rt->fib6_flags & RTF_REJECT) {
3109 ret = net->ipv6.ip6_null_entry;
3113 if (rt == net->ipv6.fib6_null_entry) {
3114 fn = fib6_backtrack(fn, &fl6->saddr);
3120 res.nh = rt->fib6_nh;
3123 ip6_hold_safe(net, &ret);
3125 res.fib6_flags = res.f6i->fib6_flags;
3126 res.fib6_type = res.f6i->fib6_type;
3127 ret = ip6_create_rt_rcu(&res);
3132 trace_fib6_table_lookup(net, &res, table, fl6);
3136 static struct dst_entry *ip6_route_redirect(struct net *net,
3137 const struct flowi6 *fl6,
3138 const struct sk_buff *skb,
3139 const struct in6_addr *gateway)
3141 int flags = RT6_LOOKUP_F_HAS_SADDR;
3142 struct ip6rd_flowi rdfl;
3145 rdfl.gateway = *gateway;
3147 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3148 flags, __ip6_route_redirect);
3151 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3154 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3155 struct dst_entry *dst;
3156 struct flowi6 fl6 = {
3157 .flowi6_iif = LOOPBACK_IFINDEX,
3159 .flowi6_mark = mark,
3160 .daddr = iph->daddr,
3161 .saddr = iph->saddr,
3162 .flowlabel = ip6_flowinfo(iph),
3166 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3167 rt6_do_redirect(dst, NULL, skb);
3170 EXPORT_SYMBOL_GPL(ip6_redirect);
3172 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3174 const struct ipv6hdr *iph = ipv6_hdr(skb);
3175 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3176 struct dst_entry *dst;
3177 struct flowi6 fl6 = {
3178 .flowi6_iif = LOOPBACK_IFINDEX,
3181 .saddr = iph->daddr,
3182 .flowi6_uid = sock_net_uid(net, NULL),
3185 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3186 rt6_do_redirect(dst, NULL, skb);
3190 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3192 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
3193 READ_ONCE(sk->sk_mark), sk->sk_uid);
3195 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3197 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3199 struct net_device *dev = dst->dev;
3200 unsigned int mtu = dst_mtu(dst);
3201 struct net *net = dev_net(dev);
3203 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3205 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3206 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3209 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3210 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3211 * IPV6_MAXPLEN is also valid and means: "any MSS,
3212 * rely only on pmtu discovery"
3214 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3219 INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
3221 return ip6_dst_mtu_maybe_forward(dst, false);
3223 EXPORT_INDIRECT_CALLABLE(ip6_mtu);
3226 * 1. mtu on route is locked - use it
3227 * 2. mtu from nexthop exception
3228 * 3. mtu from egress device
3230 * based on ip6_dst_mtu_forward and exception logic of
3231 * rt6_find_cached_rt; called with rcu_read_lock
3233 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3234 const struct in6_addr *daddr,
3235 const struct in6_addr *saddr)
3237 const struct fib6_nh *nh = res->nh;
3238 struct fib6_info *f6i = res->f6i;
3239 struct inet6_dev *idev;
3240 struct rt6_info *rt;
3243 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3244 mtu = f6i->fib6_pmtu;
3249 rt = rt6_find_cached_rt(res, daddr, saddr);
3251 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3253 struct net_device *dev = nh->fib_nh_dev;
3256 idev = __in6_dev_get(dev);
3258 mtu = max_t(u32, mtu, READ_ONCE(idev->cnf.mtu6));
3261 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3263 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3266 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3269 struct dst_entry *dst;
3270 struct rt6_info *rt;
3271 struct inet6_dev *idev = in6_dev_get(dev);
3272 struct net *net = dev_net(dev);
3274 if (unlikely(!idev))
3275 return ERR_PTR(-ENODEV);
3277 rt = ip6_dst_alloc(net, dev, 0);
3278 if (unlikely(!rt)) {
3280 dst = ERR_PTR(-ENOMEM);
3284 rt->dst.input = ip6_input;
3285 rt->dst.output = ip6_output;
3286 rt->rt6i_gateway = fl6->daddr;
3287 rt->rt6i_dst.addr = fl6->daddr;
3288 rt->rt6i_dst.plen = 128;
3289 rt->rt6i_idev = idev;
3290 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3292 /* Add this dst into uncached_list so that rt6_disable_ip() can
3293 * do proper release of the net_device
3295 rt6_uncached_list_add(rt);
3297 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3303 static void ip6_dst_gc(struct dst_ops *ops)
3305 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3306 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3307 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3308 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3309 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3313 if (time_after(rt_last_gc + rt_min_interval, jiffies))
3316 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
3317 entries = dst_entries_get_slow(ops);
3318 if (entries < ops->gc_thresh)
3319 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
3321 val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
3322 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
3325 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3326 const struct in6_addr *gw_addr, u32 tbid,
3327 int flags, struct fib6_result *res)
3329 struct flowi6 fl6 = {
3330 .flowi6_oif = cfg->fc_ifindex,
3332 .saddr = cfg->fc_prefsrc,
3334 struct fib6_table *table;
3337 table = fib6_get_table(net, tbid);
3341 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3342 flags |= RT6_LOOKUP_F_HAS_SADDR;
3344 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3346 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3347 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3348 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3349 cfg->fc_ifindex != 0, NULL, flags);
3354 static int ip6_route_check_nh_onlink(struct net *net,
3355 struct fib6_config *cfg,
3356 const struct net_device *dev,
3357 struct netlink_ext_ack *extack)
3359 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3360 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3361 struct fib6_result res = {};
3364 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3365 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3366 /* ignore match if it is the default route */
3367 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3368 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3369 NL_SET_ERR_MSG(extack,
3370 "Nexthop has invalid gateway or device mismatch");
3377 static int ip6_route_check_nh(struct net *net,
3378 struct fib6_config *cfg,
3379 struct net_device **_dev,
3380 netdevice_tracker *dev_tracker,
3381 struct inet6_dev **idev)
3383 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3384 struct net_device *dev = _dev ? *_dev : NULL;
3385 int flags = RT6_LOOKUP_F_IFACE;
3386 struct fib6_result res = {};
3387 int err = -EHOSTUNREACH;
3389 if (cfg->fc_table) {
3390 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3391 cfg->fc_table, flags, &res);
3392 /* gw_addr can not require a gateway or resolve to a reject
3393 * route. If a device is given, it must match the result.
3395 if (err || res.fib6_flags & RTF_REJECT ||
3396 res.nh->fib_nh_gw_family ||
3397 (dev && dev != res.nh->fib_nh_dev))
3398 err = -EHOSTUNREACH;
3402 struct flowi6 fl6 = {
3403 .flowi6_oif = cfg->fc_ifindex,
3407 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3408 if (err || res.fib6_flags & RTF_REJECT ||
3409 res.nh->fib_nh_gw_family)
3410 err = -EHOSTUNREACH;
3415 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3416 cfg->fc_ifindex != 0, NULL, flags);
3421 if (dev != res.nh->fib_nh_dev)
3422 err = -EHOSTUNREACH;
3424 *_dev = dev = res.nh->fib_nh_dev;
3425 netdev_hold(dev, dev_tracker, GFP_ATOMIC);
3426 *idev = in6_dev_get(dev);
3432 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3433 struct net_device **_dev,
3434 netdevice_tracker *dev_tracker,
3435 struct inet6_dev **idev,
3436 struct netlink_ext_ack *extack)
3438 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3439 int gwa_type = ipv6_addr_type(gw_addr);
3440 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3441 const struct net_device *dev = *_dev;
3442 bool need_addr_check = !dev;
3445 /* if gw_addr is local we will fail to detect this in case
3446 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3447 * will return already-added prefix route via interface that
3448 * prefix route was assigned to, which might be non-loopback.
3451 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3452 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3456 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3457 /* IPv6 strictly inhibits using not link-local
3458 * addresses as nexthop address.
3459 * Otherwise, router will not able to send redirects.
3460 * It is very good, but in some (rare!) circumstances
3461 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3462 * some exceptions. --ANK
3463 * We allow IPv4-mapped nexthops to support RFC4798-type
3466 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3467 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3473 if (cfg->fc_flags & RTNH_F_ONLINK)
3474 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3476 err = ip6_route_check_nh(net, cfg, _dev, dev_tracker,
3485 /* reload in case device was changed */
3490 NL_SET_ERR_MSG(extack, "Egress device not specified");
3492 } else if (dev->flags & IFF_LOOPBACK) {
3493 NL_SET_ERR_MSG(extack,
3494 "Egress device can not be loopback device for this route");
3498 /* if we did not check gw_addr above, do so now that the
3499 * egress device has been resolved.
3501 if (need_addr_check &&
3502 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3503 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3512 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3514 if ((flags & RTF_REJECT) ||
3515 (dev && (dev->flags & IFF_LOOPBACK) &&
3516 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3517 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
3523 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3524 struct fib6_config *cfg, gfp_t gfp_flags,
3525 struct netlink_ext_ack *extack)
3527 netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker;
3528 struct net_device *dev = NULL;
3529 struct inet6_dev *idev = NULL;
3533 fib6_nh->fib_nh_family = AF_INET6;
3534 #ifdef CONFIG_IPV6_ROUTER_PREF
3535 fib6_nh->last_probe = jiffies;
3537 if (cfg->fc_is_fdb) {
3538 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3539 fib6_nh->fib_nh_gw_family = AF_INET6;
3544 if (cfg->fc_ifindex) {
3545 dev = netdev_get_by_index(net, cfg->fc_ifindex,
3546 dev_tracker, gfp_flags);
3549 idev = in6_dev_get(dev);
3554 if (cfg->fc_flags & RTNH_F_ONLINK) {
3556 NL_SET_ERR_MSG(extack,
3557 "Nexthop device required for onlink");
3561 if (!(dev->flags & IFF_UP)) {
3562 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3567 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3570 fib6_nh->fib_nh_weight = 1;
3572 /* We cannot add true routes via loopback here,
3573 * they would result in kernel looping; promote them to reject routes
3575 addr_type = ipv6_addr_type(&cfg->fc_dst);
3576 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3577 /* hold loopback dev/idev if we haven't done so. */
3578 if (dev != net->loopback_dev) {
3580 netdev_put(dev, dev_tracker);
3583 dev = net->loopback_dev;
3584 netdev_hold(dev, dev_tracker, gfp_flags);
3585 idev = in6_dev_get(dev);
3594 if (cfg->fc_flags & RTF_GATEWAY) {
3595 err = ip6_validate_gw(net, cfg, &dev, dev_tracker,
3600 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3601 fib6_nh->fib_nh_gw_family = AF_INET6;
3608 if (!idev || idev->cnf.disable_ipv6) {
3609 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3614 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3615 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3620 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3621 !netif_carrier_ok(dev))
3622 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3624 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
3625 cfg->fc_encap_type, cfg, gfp_flags, extack);
3630 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3631 if (!fib6_nh->rt6i_pcpu) {
3636 fib6_nh->fib_nh_dev = dev;
3637 fib6_nh->fib_nh_oif = dev->ifindex;
3644 lwtstate_put(fib6_nh->fib_nh_lws);
3645 fib6_nh->fib_nh_lws = NULL;
3646 netdev_put(dev, dev_tracker);
3652 void fib6_nh_release(struct fib6_nh *fib6_nh)
3654 struct rt6_exception_bucket *bucket;
3658 fib6_nh_flush_exceptions(fib6_nh, NULL);
3659 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3661 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3667 fib6_nh_release_dsts(fib6_nh);
3668 free_percpu(fib6_nh->rt6i_pcpu);
3670 fib_nh_common_release(&fib6_nh->nh_common);
3673 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3677 if (!fib6_nh->rt6i_pcpu)
3680 for_each_possible_cpu(cpu) {
3681 struct rt6_info *pcpu_rt, **ppcpu_rt;
3683 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3684 pcpu_rt = xchg(ppcpu_rt, NULL);
3686 dst_dev_put(&pcpu_rt->dst);
3687 dst_release(&pcpu_rt->dst);
3692 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3694 struct netlink_ext_ack *extack)
3696 struct net *net = cfg->fc_nlinfo.nl_net;
3697 struct fib6_info *rt = NULL;
3698 struct nexthop *nh = NULL;
3699 struct fib6_table *table;
3700 struct fib6_nh *fib6_nh;
3704 /* RTF_PCPU is an internal flag; can not be set by userspace */
3705 if (cfg->fc_flags & RTF_PCPU) {
3706 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3710 /* RTF_CACHE is an internal flag; can not be set by userspace */
3711 if (cfg->fc_flags & RTF_CACHE) {
3712 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3716 if (cfg->fc_type > RTN_MAX) {
3717 NL_SET_ERR_MSG(extack, "Invalid route type");
3721 if (cfg->fc_dst_len > 128) {
3722 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3725 if (cfg->fc_src_len > 128) {
3726 NL_SET_ERR_MSG(extack, "Invalid source address length");
3729 #ifndef CONFIG_IPV6_SUBTREES
3730 if (cfg->fc_src_len) {
3731 NL_SET_ERR_MSG(extack,
3732 "Specifying source address requires IPV6_SUBTREES to be enabled");
3736 if (cfg->fc_nh_id) {
3737 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3739 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3742 err = fib6_check_nexthop(nh, cfg, extack);
3748 if (cfg->fc_nlinfo.nlh &&
3749 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3750 table = fib6_get_table(net, cfg->fc_table);
3752 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3753 table = fib6_new_table(net, cfg->fc_table);
3756 table = fib6_new_table(net, cfg->fc_table);
3763 rt = fib6_info_alloc(gfp_flags, !nh);
3767 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3769 if (IS_ERR(rt->fib6_metrics)) {
3770 err = PTR_ERR(rt->fib6_metrics);
3771 /* Do not leave garbage there. */
3772 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3776 if (cfg->fc_flags & RTF_ADDRCONF)
3777 rt->dst_nocount = true;
3779 if (cfg->fc_flags & RTF_EXPIRES)
3780 fib6_set_expires(rt, jiffies +
3781 clock_t_to_jiffies(cfg->fc_expires));
3783 if (cfg->fc_protocol == RTPROT_UNSPEC)
3784 cfg->fc_protocol = RTPROT_BOOT;
3785 rt->fib6_protocol = cfg->fc_protocol;
3787 rt->fib6_table = table;
3788 rt->fib6_metric = cfg->fc_metric;
3789 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3790 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3792 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3793 rt->fib6_dst.plen = cfg->fc_dst_len;
3795 #ifdef CONFIG_IPV6_SUBTREES
3796 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3797 rt->fib6_src.plen = cfg->fc_src_len;
3800 if (rt->fib6_src.plen) {
3801 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3804 if (!nexthop_get(nh)) {
3805 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3809 fib6_nh = nexthop_fib6_nh(rt->nh);
3811 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3815 fib6_nh = rt->fib6_nh;
3817 /* We cannot add true routes via loopback here, they would
3818 * result in kernel looping; promote them to reject routes
3820 addr_type = ipv6_addr_type(&cfg->fc_dst);
3821 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3823 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3826 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3827 struct net_device *dev = fib6_nh->fib_nh_dev;
3829 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3830 NL_SET_ERR_MSG(extack, "Invalid source address");
3834 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3835 rt->fib6_prefsrc.plen = 128;
3837 rt->fib6_prefsrc.plen = 0;
3841 fib6_info_release(rt);
3842 return ERR_PTR(err);
3844 ip_fib_metrics_put(rt->fib6_metrics);
3846 return ERR_PTR(err);
3849 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3850 struct netlink_ext_ack *extack)
3852 struct fib6_info *rt;
3855 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3859 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3860 fib6_info_release(rt);
3865 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3867 struct net *net = info->nl_net;
3868 struct fib6_table *table;
3871 if (rt == net->ipv6.fib6_null_entry) {
3876 table = rt->fib6_table;
3877 spin_lock_bh(&table->tb6_lock);
3878 err = fib6_del(rt, info);
3879 spin_unlock_bh(&table->tb6_lock);
3882 fib6_info_release(rt);
3886 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3888 struct nl_info info = {
3890 .skip_notify = skip_notify
3893 return __ip6_del_rt(rt, &info);
3896 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3898 struct nl_info *info = &cfg->fc_nlinfo;
3899 struct net *net = info->nl_net;
3900 struct sk_buff *skb = NULL;
3901 struct fib6_table *table;
3904 if (rt == net->ipv6.fib6_null_entry)
3906 table = rt->fib6_table;
3907 spin_lock_bh(&table->tb6_lock);
3909 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3910 struct fib6_info *sibling, *next_sibling;
3911 struct fib6_node *fn;
3913 /* prefer to send a single notification with all hops */
3914 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3916 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3918 if (rt6_fill_node(net, skb, rt, NULL,
3919 NULL, NULL, 0, RTM_DELROUTE,
3920 info->portid, seq, 0) < 0) {
3924 info->skip_notify = 1;
3927 /* 'rt' points to the first sibling route. If it is not the
3928 * leaf, then we do not need to send a notification. Otherwise,
3929 * we need to check if the last sibling has a next route or not
3930 * and emit a replace or delete notification, respectively.
3932 info->skip_notify_kernel = 1;
3933 fn = rcu_dereference_protected(rt->fib6_node,
3934 lockdep_is_held(&table->tb6_lock));
3935 if (rcu_access_pointer(fn->leaf) == rt) {
3936 struct fib6_info *last_sibling, *replace_rt;
3938 last_sibling = list_last_entry(&rt->fib6_siblings,
3941 replace_rt = rcu_dereference_protected(
3942 last_sibling->fib6_next,
3943 lockdep_is_held(&table->tb6_lock));
3945 call_fib6_entry_notifiers_replace(net,
3948 call_fib6_multipath_entry_notifiers(net,
3949 FIB_EVENT_ENTRY_DEL,
3950 rt, rt->fib6_nsiblings,
3953 list_for_each_entry_safe(sibling, next_sibling,
3956 err = fib6_del(sibling, info);
3962 err = fib6_del(rt, info);
3964 spin_unlock_bh(&table->tb6_lock);
3966 fib6_info_release(rt);
3969 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3970 info->nlh, gfp_any());
3975 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3979 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3982 if (cfg->fc_flags & RTF_GATEWAY &&
3983 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3986 rc = rt6_remove_exception_rt(rt);
3991 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3994 struct fib6_result res = {
3998 struct rt6_info *rt_cache;
4000 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
4002 return __ip6_del_cached_rt(rt_cache, cfg);
4007 struct fib6_nh_del_cached_rt_arg {
4008 struct fib6_config *cfg;
4009 struct fib6_info *f6i;
4012 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
4014 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4017 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4018 return rc != -ESRCH ? rc : 0;
4021 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4023 struct fib6_nh_del_cached_rt_arg arg = {
4028 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4031 static int ip6_route_del(struct fib6_config *cfg,
4032 struct netlink_ext_ack *extack)
4034 struct fib6_table *table;
4035 struct fib6_info *rt;
4036 struct fib6_node *fn;
4039 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
4041 NL_SET_ERR_MSG(extack, "FIB table does not exist");
4047 fn = fib6_locate(&table->tb6_root,
4048 &cfg->fc_dst, cfg->fc_dst_len,
4049 &cfg->fc_src, cfg->fc_src_len,
4050 !(cfg->fc_flags & RTF_CACHE));
4053 for_each_fib6_node_rt_rcu(fn) {
4056 if (rt->nh && cfg->fc_nh_id &&
4057 rt->nh->id != cfg->fc_nh_id)
4060 if (cfg->fc_flags & RTF_CACHE) {
4064 rc = ip6_del_cached_rt_nh(cfg, rt);
4065 } else if (cfg->fc_nh_id) {
4069 rc = ip6_del_cached_rt(cfg, rt, nh);
4078 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4080 if (cfg->fc_protocol &&
4081 cfg->fc_protocol != rt->fib6_protocol)
4085 if (!fib6_info_hold_safe(rt))
4089 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4095 if (cfg->fc_ifindex &&
4097 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
4099 if (cfg->fc_flags & RTF_GATEWAY &&
4100 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
4102 if (!fib6_info_hold_safe(rt))
4106 /* if gateway was specified only delete the one hop */
4107 if (cfg->fc_flags & RTF_GATEWAY)
4108 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4110 return __ip6_del_rt_siblings(rt, cfg);
4118 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4120 struct netevent_redirect netevent;
4121 struct rt6_info *rt, *nrt = NULL;
4122 struct fib6_result res = {};
4123 struct ndisc_options ndopts;
4124 struct inet6_dev *in6_dev;
4125 struct neighbour *neigh;
4127 int optlen, on_link;
4130 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4131 optlen -= sizeof(*msg);
4134 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4138 msg = (struct rd_msg *)icmp6_hdr(skb);
4140 if (ipv6_addr_is_multicast(&msg->dest)) {
4141 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4146 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4148 } else if (ipv6_addr_type(&msg->target) !=
4149 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4150 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4154 in6_dev = __in6_dev_get(skb->dev);
4157 if (READ_ONCE(in6_dev->cnf.forwarding) ||
4158 !READ_ONCE(in6_dev->cnf.accept_redirects))
4162 * The IP source address of the Redirect MUST be the same as the current
4163 * first-hop router for the specified ICMP Destination Address.
4166 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4167 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4172 if (ndopts.nd_opts_tgt_lladdr) {
4173 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4176 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4181 rt = dst_rt6_info(dst);
4182 if (rt->rt6i_flags & RTF_REJECT) {
4183 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4187 /* Redirect received -> path was valid.
4188 * Look, redirects are sent only in response to data packets,
4189 * so that this nexthop apparently is reachable. --ANK
4191 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4193 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4198 * We have finally decided to accept it.
4201 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4202 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4203 NEIGH_UPDATE_F_OVERRIDE|
4204 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4205 NEIGH_UPDATE_F_ISROUTER)),
4206 NDISC_REDIRECT, &ndopts);
4209 res.f6i = rcu_dereference(rt->from);
4214 struct fib6_nh_match_arg arg = {
4216 .gw = &rt->rt6i_gateway,
4219 nexthop_for_each_fib6_nh(res.f6i->nh,
4220 fib6_nh_find_match, &arg);
4222 /* fib6_info uses a nexthop that does not have fib6_nh
4223 * using the dst->dev. Should be impossible
4229 res.nh = res.f6i->fib6_nh;
4232 res.fib6_flags = res.f6i->fib6_flags;
4233 res.fib6_type = res.f6i->fib6_type;
4234 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4238 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4240 nrt->rt6i_flags &= ~RTF_GATEWAY;
4242 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4244 /* rt6_insert_exception() will take care of duplicated exceptions */
4245 if (rt6_insert_exception(nrt, &res)) {
4246 dst_release_immediate(&nrt->dst);
4250 netevent.old = &rt->dst;
4251 netevent.new = &nrt->dst;
4252 netevent.daddr = &msg->dest;
4253 netevent.neigh = neigh;
4254 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4258 neigh_release(neigh);
4261 #ifdef CONFIG_IPV6_ROUTE_INFO
4262 static struct fib6_info *rt6_get_route_info(struct net *net,
4263 const struct in6_addr *prefix, int prefixlen,
4264 const struct in6_addr *gwaddr,
4265 struct net_device *dev)
4267 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4268 int ifindex = dev->ifindex;
4269 struct fib6_node *fn;
4270 struct fib6_info *rt = NULL;
4271 struct fib6_table *table;
4273 table = fib6_get_table(net, tb_id);
4278 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4282 for_each_fib6_node_rt_rcu(fn) {
4283 /* these routes do not use nexthops */
4286 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4288 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4289 !rt->fib6_nh->fib_nh_gw_family)
4291 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4293 if (!fib6_info_hold_safe(rt))
4302 static struct fib6_info *rt6_add_route_info(struct net *net,
4303 const struct in6_addr *prefix, int prefixlen,
4304 const struct in6_addr *gwaddr,
4305 struct net_device *dev,
4308 struct fib6_config cfg = {
4309 .fc_metric = IP6_RT_PRIO_USER,
4310 .fc_ifindex = dev->ifindex,
4311 .fc_dst_len = prefixlen,
4312 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4313 RTF_UP | RTF_PREF(pref),
4314 .fc_protocol = RTPROT_RA,
4315 .fc_type = RTN_UNICAST,
4316 .fc_nlinfo.portid = 0,
4317 .fc_nlinfo.nlh = NULL,
4318 .fc_nlinfo.nl_net = net,
4321 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4322 cfg.fc_dst = *prefix;
4323 cfg.fc_gateway = *gwaddr;
4325 /* We should treat it as a default route if prefix length is 0. */
4327 cfg.fc_flags |= RTF_DEFAULT;
4329 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4331 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4335 struct fib6_info *rt6_get_dflt_router(struct net *net,
4336 const struct in6_addr *addr,
4337 struct net_device *dev)
4339 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4340 struct fib6_info *rt;
4341 struct fib6_table *table;
4343 table = fib6_get_table(net, tb_id);
4348 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4351 /* RA routes do not use nexthops */
4356 if (dev == nh->fib_nh_dev &&
4357 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4358 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4361 if (rt && !fib6_info_hold_safe(rt))
4367 struct fib6_info *rt6_add_dflt_router(struct net *net,
4368 const struct in6_addr *gwaddr,
4369 struct net_device *dev,
4371 u32 defrtr_usr_metric,
4374 struct fib6_config cfg = {
4375 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4376 .fc_metric = defrtr_usr_metric,
4377 .fc_ifindex = dev->ifindex,
4378 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4379 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4380 .fc_protocol = RTPROT_RA,
4381 .fc_type = RTN_UNICAST,
4382 .fc_nlinfo.portid = 0,
4383 .fc_nlinfo.nlh = NULL,
4384 .fc_nlinfo.nl_net = net,
4385 .fc_expires = jiffies_to_clock_t(lifetime * HZ),
4388 cfg.fc_gateway = *gwaddr;
4390 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4391 struct fib6_table *table;
4393 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4395 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4398 return rt6_get_dflt_router(net, gwaddr, dev);
4401 static void __rt6_purge_dflt_routers(struct net *net,
4402 struct fib6_table *table)
4404 struct fib6_info *rt;
4408 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4409 struct net_device *dev = fib6_info_nh_dev(rt);
4410 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4412 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4413 (!idev || idev->cnf.accept_ra != 2) &&
4414 fib6_info_hold_safe(rt)) {
4416 ip6_del_rt(net, rt, false);
4422 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4425 void rt6_purge_dflt_routers(struct net *net)
4427 struct fib6_table *table;
4428 struct hlist_head *head;
4433 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4434 head = &net->ipv6.fib_table_hash[h];
4435 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4436 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4437 __rt6_purge_dflt_routers(net, table);
4444 static void rtmsg_to_fib6_config(struct net *net,
4445 struct in6_rtmsg *rtmsg,
4446 struct fib6_config *cfg)
4448 *cfg = (struct fib6_config){
4449 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4451 .fc_ifindex = rtmsg->rtmsg_ifindex,
4452 .fc_metric = rtmsg->rtmsg_metric,
4453 .fc_expires = rtmsg->rtmsg_info,
4454 .fc_dst_len = rtmsg->rtmsg_dst_len,
4455 .fc_src_len = rtmsg->rtmsg_src_len,
4456 .fc_flags = rtmsg->rtmsg_flags,
4457 .fc_type = rtmsg->rtmsg_type,
4459 .fc_nlinfo.nl_net = net,
4461 .fc_dst = rtmsg->rtmsg_dst,
4462 .fc_src = rtmsg->rtmsg_src,
4463 .fc_gateway = rtmsg->rtmsg_gateway,
4467 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
4469 struct fib6_config cfg;
4472 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4474 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4477 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4482 /* Only do the default setting of fc_metric in route adding */
4483 if (cfg.fc_metric == 0)
4484 cfg.fc_metric = IP6_RT_PRIO_USER;
4485 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4488 err = ip6_route_del(&cfg, NULL);
4496 * Drop the packet on the floor
4499 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4501 struct dst_entry *dst = skb_dst(skb);
4502 struct net *net = dev_net(dst->dev);
4503 struct inet6_dev *idev;
4507 if (netif_is_l3_master(skb->dev) ||
4508 dst->dev == net->loopback_dev)
4509 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4511 idev = ip6_dst_idev(dst);
4513 switch (ipstats_mib_noroutes) {
4514 case IPSTATS_MIB_INNOROUTES:
4515 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4516 if (type == IPV6_ADDR_ANY) {
4517 SKB_DR_SET(reason, IP_INADDRERRORS);
4518 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4521 SKB_DR_SET(reason, IP_INNOROUTES);
4523 case IPSTATS_MIB_OUTNOROUTES:
4524 SKB_DR_OR(reason, IP_OUTNOROUTES);
4525 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4529 /* Start over by dropping the dst for l3mdev case */
4530 if (netif_is_l3_master(skb->dev))
4533 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4534 kfree_skb_reason(skb, reason);
4538 static int ip6_pkt_discard(struct sk_buff *skb)
4540 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4543 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4545 skb->dev = skb_dst(skb)->dev;
4546 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4549 static int ip6_pkt_prohibit(struct sk_buff *skb)
4551 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4554 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4556 skb->dev = skb_dst(skb)->dev;
4557 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4561 * Allocate a dst for local (unicast / anycast) address.
4564 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4565 struct inet6_dev *idev,
4566 const struct in6_addr *addr,
4567 bool anycast, gfp_t gfp_flags,
4568 struct netlink_ext_ack *extack)
4570 struct fib6_config cfg = {
4571 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4572 .fc_ifindex = idev->dev->ifindex,
4573 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4576 .fc_protocol = RTPROT_KERNEL,
4577 .fc_nlinfo.nl_net = net,
4578 .fc_ignore_dev_down = true,
4580 struct fib6_info *f6i;
4583 cfg.fc_type = RTN_ANYCAST;
4584 cfg.fc_flags |= RTF_ANYCAST;
4586 cfg.fc_type = RTN_LOCAL;
4587 cfg.fc_flags |= RTF_LOCAL;
4590 f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
4592 f6i->dst_nocount = true;
4595 (READ_ONCE(net->ipv6.devconf_all->disable_policy) ||
4596 READ_ONCE(idev->cnf.disable_policy)))
4597 f6i->dst_nopolicy = true;
4603 /* remove deleted ip from prefsrc entries */
4604 struct arg_dev_net_ip {
4606 struct in6_addr *addr;
4609 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4611 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4612 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4615 rt != net->ipv6.fib6_null_entry &&
4616 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
4617 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
4618 spin_lock_bh(&rt6_exception_lock);
4619 /* remove prefsrc entry */
4620 rt->fib6_prefsrc.plen = 0;
4621 spin_unlock_bh(&rt6_exception_lock);
4626 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4628 struct net *net = dev_net(ifp->idev->dev);
4629 struct arg_dev_net_ip adni = {
4633 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4636 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4638 /* Remove routers and update dst entries when gateway turn into host. */
4639 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4641 struct in6_addr *gateway = (struct in6_addr *)arg;
4644 /* RA routes do not use nexthops */
4649 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4650 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4653 /* Further clean up cached routes in exception table.
4654 * This is needed because cached route may have a different
4655 * gateway than its 'parent' in the case of an ip redirect.
4657 fib6_nh_exceptions_clean_tohost(nh, gateway);
4662 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4664 fib6_clean_all(net, fib6_clean_tohost, gateway);
4667 struct arg_netdev_event {
4668 const struct net_device *dev;
4670 unsigned char nh_flags;
4671 unsigned long event;
4675 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4677 struct fib6_info *iter;
4678 struct fib6_node *fn;
4680 fn = rcu_dereference_protected(rt->fib6_node,
4681 lockdep_is_held(&rt->fib6_table->tb6_lock));
4682 iter = rcu_dereference_protected(fn->leaf,
4683 lockdep_is_held(&rt->fib6_table->tb6_lock));
4685 if (iter->fib6_metric == rt->fib6_metric &&
4686 rt6_qualify_for_ecmp(iter))
4688 iter = rcu_dereference_protected(iter->fib6_next,
4689 lockdep_is_held(&rt->fib6_table->tb6_lock));
4695 /* only called for fib entries with builtin fib6_nh */
4696 static bool rt6_is_dead(const struct fib6_info *rt)
4698 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4699 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4700 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4706 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4708 struct fib6_info *iter;
4711 if (!rt6_is_dead(rt))
4712 total += rt->fib6_nh->fib_nh_weight;
4714 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4715 if (!rt6_is_dead(iter))
4716 total += iter->fib6_nh->fib_nh_weight;
4722 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4724 int upper_bound = -1;
4726 if (!rt6_is_dead(rt)) {
4727 *weight += rt->fib6_nh->fib_nh_weight;
4728 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4731 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4734 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4736 struct fib6_info *iter;
4739 rt6_upper_bound_set(rt, &weight, total);
4741 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4742 rt6_upper_bound_set(iter, &weight, total);
4745 void rt6_multipath_rebalance(struct fib6_info *rt)
4747 struct fib6_info *first;
4750 /* In case the entire multipath route was marked for flushing,
4751 * then there is no need to rebalance upon the removal of every
4754 if (!rt->fib6_nsiblings || rt->should_flush)
4757 /* During lookup routes are evaluated in order, so we need to
4758 * make sure upper bounds are assigned from the first sibling
4761 first = rt6_multipath_first_sibling(rt);
4762 if (WARN_ON_ONCE(!first))
4765 total = rt6_multipath_total_weight(first);
4766 rt6_multipath_upper_bound_set(first, total);
4769 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4771 const struct arg_netdev_event *arg = p_arg;
4772 struct net *net = dev_net(arg->dev);
4774 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4775 rt->fib6_nh->fib_nh_dev == arg->dev) {
4776 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4777 fib6_update_sernum_upto_root(net, rt);
4778 rt6_multipath_rebalance(rt);
4784 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4786 struct arg_netdev_event arg = {
4789 .nh_flags = nh_flags,
4793 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4794 arg.nh_flags |= RTNH_F_LINKDOWN;
4796 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4799 /* only called for fib entries with inline fib6_nh */
4800 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4801 const struct net_device *dev)
4803 struct fib6_info *iter;
4805 if (rt->fib6_nh->fib_nh_dev == dev)
4807 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4808 if (iter->fib6_nh->fib_nh_dev == dev)
4814 static void rt6_multipath_flush(struct fib6_info *rt)
4816 struct fib6_info *iter;
4818 rt->should_flush = 1;
4819 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4820 iter->should_flush = 1;
4823 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4824 const struct net_device *down_dev)
4826 struct fib6_info *iter;
4827 unsigned int dead = 0;
4829 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4830 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4832 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4833 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4834 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4840 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4841 const struct net_device *dev,
4842 unsigned char nh_flags)
4844 struct fib6_info *iter;
4846 if (rt->fib6_nh->fib_nh_dev == dev)
4847 rt->fib6_nh->fib_nh_flags |= nh_flags;
4848 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4849 if (iter->fib6_nh->fib_nh_dev == dev)
4850 iter->fib6_nh->fib_nh_flags |= nh_flags;
4853 /* called with write lock held for table with rt */
4854 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4856 const struct arg_netdev_event *arg = p_arg;
4857 const struct net_device *dev = arg->dev;
4858 struct net *net = dev_net(dev);
4860 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4863 switch (arg->event) {
4864 case NETDEV_UNREGISTER:
4865 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4867 if (rt->should_flush)
4869 if (!rt->fib6_nsiblings)
4870 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4871 if (rt6_multipath_uses_dev(rt, dev)) {
4874 count = rt6_multipath_dead_count(rt, dev);
4875 if (rt->fib6_nsiblings + 1 == count) {
4876 rt6_multipath_flush(rt);
4879 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4881 fib6_update_sernum(net, rt);
4882 rt6_multipath_rebalance(rt);
4886 if (rt->fib6_nh->fib_nh_dev != dev ||
4887 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4889 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4890 rt6_multipath_rebalance(rt);
4897 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4899 struct arg_netdev_event arg = {
4905 struct net *net = dev_net(dev);
4907 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4908 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4910 fib6_clean_all(net, fib6_ifdown, &arg);
4913 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4915 rt6_sync_down_dev(dev, event);
4916 rt6_uncached_list_flush_dev(dev);
4917 neigh_ifdown(&nd_tbl, dev);
4920 struct rt6_mtu_change_arg {
4921 struct net_device *dev;
4923 struct fib6_info *f6i;
4926 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4928 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4929 struct fib6_info *f6i = arg->f6i;
4931 /* For administrative MTU increase, there is no way to discover
4932 * IPv6 PMTU increase, so PMTU increase should be updated here.
4933 * Since RFC 1981 doesn't include administrative MTU increase
4934 * update PMTU increase is a MUST. (i.e. jumbo frame)
4936 if (nh->fib_nh_dev == arg->dev) {
4937 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4938 u32 mtu = f6i->fib6_pmtu;
4940 if (mtu >= arg->mtu ||
4941 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4942 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4944 spin_lock_bh(&rt6_exception_lock);
4945 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4946 spin_unlock_bh(&rt6_exception_lock);
4952 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4954 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4955 struct inet6_dev *idev;
4957 /* In IPv6 pmtu discovery is not optional,
4958 so that RTAX_MTU lock cannot disable it.
4959 We still use this lock to block changes
4960 caused by addrconf/ndisc.
4963 idev = __in6_dev_get(arg->dev);
4967 if (fib6_metric_locked(f6i, RTAX_MTU))
4972 /* fib6_nh_mtu_change only returns 0, so this is safe */
4973 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4977 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4980 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4982 struct rt6_mtu_change_arg arg = {
4987 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4990 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4991 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
4992 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4993 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4994 [RTA_OIF] = { .type = NLA_U32 },
4995 [RTA_IIF] = { .type = NLA_U32 },
4996 [RTA_PRIORITY] = { .type = NLA_U32 },
4997 [RTA_METRICS] = { .type = NLA_NESTED },
4998 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4999 [RTA_PREF] = { .type = NLA_U8 },
5000 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
5001 [RTA_ENCAP] = { .type = NLA_NESTED },
5002 [RTA_EXPIRES] = { .type = NLA_U32 },
5003 [RTA_UID] = { .type = NLA_U32 },
5004 [RTA_MARK] = { .type = NLA_U32 },
5005 [RTA_TABLE] = { .type = NLA_U32 },
5006 [RTA_IP_PROTO] = { .type = NLA_U8 },
5007 [RTA_SPORT] = { .type = NLA_U16 },
5008 [RTA_DPORT] = { .type = NLA_U16 },
5009 [RTA_NH_ID] = { .type = NLA_U32 },
5012 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
5013 struct fib6_config *cfg,
5014 struct netlink_ext_ack *extack)
5017 struct nlattr *tb[RTA_MAX+1];
5021 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5022 rtm_ipv6_policy, extack);
5027 rtm = nlmsg_data(nlh);
5030 NL_SET_ERR_MSG(extack,
5031 "Invalid dsfield (tos): option not available for IPv6");
5035 *cfg = (struct fib6_config){
5036 .fc_table = rtm->rtm_table,
5037 .fc_dst_len = rtm->rtm_dst_len,
5038 .fc_src_len = rtm->rtm_src_len,
5040 .fc_protocol = rtm->rtm_protocol,
5041 .fc_type = rtm->rtm_type,
5043 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5044 .fc_nlinfo.nlh = nlh,
5045 .fc_nlinfo.nl_net = sock_net(skb->sk),
5048 if (rtm->rtm_type == RTN_UNREACHABLE ||
5049 rtm->rtm_type == RTN_BLACKHOLE ||
5050 rtm->rtm_type == RTN_PROHIBIT ||
5051 rtm->rtm_type == RTN_THROW)
5052 cfg->fc_flags |= RTF_REJECT;
5054 if (rtm->rtm_type == RTN_LOCAL)
5055 cfg->fc_flags |= RTF_LOCAL;
5057 if (rtm->rtm_flags & RTM_F_CLONED)
5058 cfg->fc_flags |= RTF_CACHE;
5060 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5062 if (tb[RTA_NH_ID]) {
5063 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5064 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5065 NL_SET_ERR_MSG(extack,
5066 "Nexthop specification and nexthop id are mutually exclusive");
5069 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5072 if (tb[RTA_GATEWAY]) {
5073 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
5074 cfg->fc_flags |= RTF_GATEWAY;
5077 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5082 int plen = (rtm->rtm_dst_len + 7) >> 3;
5084 if (nla_len(tb[RTA_DST]) < plen)
5087 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
5091 int plen = (rtm->rtm_src_len + 7) >> 3;
5093 if (nla_len(tb[RTA_SRC]) < plen)
5096 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
5099 if (tb[RTA_PREFSRC])
5100 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
5103 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5105 if (tb[RTA_PRIORITY])
5106 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5108 if (tb[RTA_METRICS]) {
5109 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5110 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
5114 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5116 if (tb[RTA_MULTIPATH]) {
5117 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5118 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
5120 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
5121 cfg->fc_mp_len, extack);
5127 pref = nla_get_u8(tb[RTA_PREF]);
5128 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5129 pref != ICMPV6_ROUTER_PREF_HIGH)
5130 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5131 cfg->fc_flags |= RTF_PREF(pref);
5135 cfg->fc_encap = tb[RTA_ENCAP];
5137 if (tb[RTA_ENCAP_TYPE]) {
5138 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5140 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5145 if (tb[RTA_EXPIRES]) {
5146 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5148 if (addrconf_finite_timeout(timeout)) {
5149 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5150 cfg->fc_flags |= RTF_EXPIRES;
5160 struct fib6_info *fib6_info;
5161 struct fib6_config r_cfg;
5162 struct list_head next;
5165 static int ip6_route_info_append(struct net *net,
5166 struct list_head *rt6_nh_list,
5167 struct fib6_info *rt,
5168 struct fib6_config *r_cfg)
5173 list_for_each_entry(nh, rt6_nh_list, next) {
5174 /* check if fib6_info already exists */
5175 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5179 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5183 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5184 list_add_tail(&nh->next, rt6_nh_list);
5189 static void ip6_route_mpath_notify(struct fib6_info *rt,
5190 struct fib6_info *rt_last,
5191 struct nl_info *info,
5194 /* if this is an APPEND route, then rt points to the first route
5195 * inserted and rt_last points to last route inserted. Userspace
5196 * wants a consistent dump of the route which starts at the first
5197 * nexthop. Since sibling routes are always added at the end of
5198 * the list, find the first sibling of the last route appended
5200 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5201 rt = list_first_entry(&rt_last->fib6_siblings,
5207 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5210 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5212 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5213 bool should_notify = false;
5214 struct fib6_info *leaf;
5215 struct fib6_node *fn;
5218 fn = rcu_dereference(rt->fib6_node);
5222 leaf = rcu_dereference(fn->leaf);
5227 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5228 rt6_qualify_for_ecmp(leaf)))
5229 should_notify = true;
5233 return should_notify;
5236 static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
5237 struct netlink_ext_ack *extack)
5239 if (nla_len(nla) < sizeof(*gw)) {
5240 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
5244 *gw = nla_get_in6_addr(nla);
5249 static int ip6_route_multipath_add(struct fib6_config *cfg,
5250 struct netlink_ext_ack *extack)
5252 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5253 struct nl_info *info = &cfg->fc_nlinfo;
5254 struct fib6_config r_cfg;
5255 struct rtnexthop *rtnh;
5256 struct fib6_info *rt;
5257 struct rt6_nh *err_nh;
5258 struct rt6_nh *nh, *nh_safe;
5264 int replace = (cfg->fc_nlinfo.nlh &&
5265 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5266 LIST_HEAD(rt6_nh_list);
5268 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5269 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5270 nlflags |= NLM_F_APPEND;
5272 remaining = cfg->fc_mp_len;
5273 rtnh = (struct rtnexthop *)cfg->fc_mp;
5275 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5276 * fib6_info structs per nexthop
5278 while (rtnh_ok(rtnh, remaining)) {
5279 memcpy(&r_cfg, cfg, sizeof(*cfg));
5280 if (rtnh->rtnh_ifindex)
5281 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5283 attrlen = rtnh_attrlen(rtnh);
5285 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5287 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5289 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5294 r_cfg.fc_flags |= RTF_GATEWAY;
5296 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5298 /* RTA_ENCAP_TYPE length checked in
5299 * lwtunnel_valid_encap_type_attr
5301 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5303 r_cfg.fc_encap_type = nla_get_u16(nla);
5306 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5307 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5313 if (!rt6_qualify_for_ecmp(rt)) {
5315 NL_SET_ERR_MSG(extack,
5316 "Device only routes can not be added for IPv6 using the multipath API.");
5317 fib6_info_release(rt);
5321 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5323 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5326 fib6_info_release(rt);
5330 rtnh = rtnh_next(rtnh, &remaining);
5333 if (list_empty(&rt6_nh_list)) {
5334 NL_SET_ERR_MSG(extack,
5335 "Invalid nexthop configuration - no valid nexthops");
5339 /* for add and replace send one notification with all nexthops.
5340 * Skip the notification in fib6_add_rt2node and send one with
5341 * the full route when done
5343 info->skip_notify = 1;
5345 /* For add and replace, send one notification with all nexthops. For
5346 * append, send one notification with all appended nexthops.
5348 info->skip_notify_kernel = 1;
5351 list_for_each_entry(nh, &rt6_nh_list, next) {
5352 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5356 NL_SET_ERR_MSG_MOD(extack,
5357 "multipath route replace failed (check consistency of installed routes)");
5361 /* save reference to last route successfully inserted */
5362 rt_last = nh->fib6_info;
5364 /* save reference to first route for notification */
5366 rt_notif = nh->fib6_info;
5368 /* Because each route is added like a single route we remove
5369 * these flags after the first nexthop: if there is a collision,
5370 * we have already failed to add the first nexthop:
5371 * fib6_add_rt2node() has rejected it; when replacing, old
5372 * nexthops have been replaced by first new, the rest should
5375 if (cfg->fc_nlinfo.nlh) {
5376 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5378 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5383 /* An in-kernel notification should only be sent in case the new
5384 * multipath route is added as the first route in the node, or if
5385 * it was appended to it. We pass 'rt_notif' since it is the first
5386 * sibling and might allow us to skip some checks in the replace case.
5388 if (ip6_route_mpath_should_notify(rt_notif)) {
5389 enum fib_event_type fib_event;
5391 if (rt_notif->fib6_nsiblings != nhn - 1)
5392 fib_event = FIB_EVENT_ENTRY_APPEND;
5394 fib_event = FIB_EVENT_ENTRY_REPLACE;
5396 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5397 fib_event, rt_notif,
5400 /* Delete all the siblings that were just added */
5406 /* success ... tell user about new route */
5407 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5411 /* send notification for routes that were added so that
5412 * the delete notifications sent by ip6_route_del are
5416 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5418 /* Delete routes that were already added */
5419 list_for_each_entry(nh, &rt6_nh_list, next) {
5422 ip6_route_del(&nh->r_cfg, extack);
5426 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5427 fib6_info_release(nh->fib6_info);
5428 list_del(&nh->next);
5435 static int ip6_route_multipath_del(struct fib6_config *cfg,
5436 struct netlink_ext_ack *extack)
5438 struct fib6_config r_cfg;
5439 struct rtnexthop *rtnh;
5445 remaining = cfg->fc_mp_len;
5446 rtnh = (struct rtnexthop *)cfg->fc_mp;
5448 /* Parse a Multipath Entry */
5449 while (rtnh_ok(rtnh, remaining)) {
5450 memcpy(&r_cfg, cfg, sizeof(*cfg));
5451 if (rtnh->rtnh_ifindex)
5452 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5454 attrlen = rtnh_attrlen(rtnh);
5456 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5458 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5460 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5467 r_cfg.fc_flags |= RTF_GATEWAY;
5470 err = ip6_route_del(&r_cfg, extack);
5475 rtnh = rtnh_next(rtnh, &remaining);
5481 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5482 struct netlink_ext_ack *extack)
5484 struct fib6_config cfg;
5487 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5492 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5493 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5498 return ip6_route_multipath_del(&cfg, extack);
5500 cfg.fc_delete_all_nh = 1;
5501 return ip6_route_del(&cfg, extack);
5505 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5506 struct netlink_ext_ack *extack)
5508 struct fib6_config cfg;
5511 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5515 if (cfg.fc_metric == 0)
5516 cfg.fc_metric = IP6_RT_PRIO_USER;
5519 return ip6_route_multipath_add(&cfg, extack);
5521 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5524 /* add the overhead of this fib6_nh to nexthop_len */
5525 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5527 int *nexthop_len = arg;
5529 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5530 + NLA_ALIGN(sizeof(struct rtnexthop))
5531 + nla_total_size(16); /* RTA_GATEWAY */
5533 if (nh->fib_nh_lws) {
5534 /* RTA_ENCAP_TYPE */
5535 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5537 *nexthop_len += nla_total_size(2);
5543 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5548 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5549 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5552 struct fib6_info *sibling, *next_sibling;
5553 struct fib6_nh *nh = f6i->fib6_nh;
5556 if (f6i->fib6_nsiblings) {
5557 rt6_nh_nlmsg_size(nh, &nexthop_len);
5559 list_for_each_entry_safe(sibling, next_sibling,
5560 &f6i->fib6_siblings, fib6_siblings) {
5561 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
5564 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5567 return NLMSG_ALIGN(sizeof(struct rtmsg))
5568 + nla_total_size(16) /* RTA_SRC */
5569 + nla_total_size(16) /* RTA_DST */
5570 + nla_total_size(16) /* RTA_GATEWAY */
5571 + nla_total_size(16) /* RTA_PREFSRC */
5572 + nla_total_size(4) /* RTA_TABLE */
5573 + nla_total_size(4) /* RTA_IIF */
5574 + nla_total_size(4) /* RTA_OIF */
5575 + nla_total_size(4) /* RTA_PRIORITY */
5576 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5577 + nla_total_size(sizeof(struct rta_cacheinfo))
5578 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5579 + nla_total_size(1) /* RTA_PREF */
5583 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5584 unsigned char *flags)
5586 if (nexthop_is_multipath(nh)) {
5589 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5591 goto nla_put_failure;
5593 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5594 goto nla_put_failure;
5596 nla_nest_end(skb, mp);
5598 struct fib6_nh *fib6_nh;
5600 fib6_nh = nexthop_fib6_nh(nh);
5601 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5603 goto nla_put_failure;
5612 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5613 struct fib6_info *rt, struct dst_entry *dst,
5614 struct in6_addr *dest, struct in6_addr *src,
5615 int iif, int type, u32 portid, u32 seq,
5618 struct rt6_info *rt6 = dst_rt6_info(dst);
5619 struct rt6key *rt6_dst, *rt6_src;
5620 u32 *pmetrics, table, rt6_flags;
5621 unsigned char nh_flags = 0;
5622 struct nlmsghdr *nlh;
5626 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5631 rt6_dst = &rt6->rt6i_dst;
5632 rt6_src = &rt6->rt6i_src;
5633 rt6_flags = rt6->rt6i_flags;
5635 rt6_dst = &rt->fib6_dst;
5636 rt6_src = &rt->fib6_src;
5637 rt6_flags = rt->fib6_flags;
5640 rtm = nlmsg_data(nlh);
5641 rtm->rtm_family = AF_INET6;
5642 rtm->rtm_dst_len = rt6_dst->plen;
5643 rtm->rtm_src_len = rt6_src->plen;
5646 table = rt->fib6_table->tb6_id;
5648 table = RT6_TABLE_UNSPEC;
5649 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5650 if (nla_put_u32(skb, RTA_TABLE, table))
5651 goto nla_put_failure;
5653 rtm->rtm_type = rt->fib6_type;
5655 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5656 rtm->rtm_protocol = rt->fib6_protocol;
5658 if (rt6_flags & RTF_CACHE)
5659 rtm->rtm_flags |= RTM_F_CLONED;
5662 if (nla_put_in6_addr(skb, RTA_DST, dest))
5663 goto nla_put_failure;
5664 rtm->rtm_dst_len = 128;
5665 } else if (rtm->rtm_dst_len)
5666 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5667 goto nla_put_failure;
5668 #ifdef CONFIG_IPV6_SUBTREES
5670 if (nla_put_in6_addr(skb, RTA_SRC, src))
5671 goto nla_put_failure;
5672 rtm->rtm_src_len = 128;
5673 } else if (rtm->rtm_src_len &&
5674 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5675 goto nla_put_failure;
5678 #ifdef CONFIG_IPV6_MROUTE
5679 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5680 int err = ip6mr_get_route(net, skb, rtm, portid);
5685 goto nla_put_failure;
5688 if (nla_put_u32(skb, RTA_IIF, iif))
5689 goto nla_put_failure;
5691 struct in6_addr saddr_buf;
5692 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5693 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5694 goto nla_put_failure;
5697 if (rt->fib6_prefsrc.plen) {
5698 struct in6_addr saddr_buf;
5699 saddr_buf = rt->fib6_prefsrc.addr;
5700 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5701 goto nla_put_failure;
5704 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5705 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5706 goto nla_put_failure;
5708 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5709 goto nla_put_failure;
5711 /* For multipath routes, walk the siblings list and add
5712 * each as a nexthop within RTA_MULTIPATH.
5715 if (rt6_flags & RTF_GATEWAY &&
5716 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5717 goto nla_put_failure;
5719 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5720 goto nla_put_failure;
5722 if (dst->lwtstate &&
5723 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5724 goto nla_put_failure;
5725 } else if (rt->fib6_nsiblings) {
5726 struct fib6_info *sibling, *next_sibling;
5729 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5731 goto nla_put_failure;
5733 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5734 rt->fib6_nh->fib_nh_weight, AF_INET6,
5736 goto nla_put_failure;
5738 list_for_each_entry_safe(sibling, next_sibling,
5739 &rt->fib6_siblings, fib6_siblings) {
5740 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5741 sibling->fib6_nh->fib_nh_weight,
5743 goto nla_put_failure;
5746 nla_nest_end(skb, mp);
5747 } else if (rt->nh) {
5748 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5749 goto nla_put_failure;
5751 if (nexthop_is_blackhole(rt->nh))
5752 rtm->rtm_type = RTN_BLACKHOLE;
5754 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
5755 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5756 goto nla_put_failure;
5758 rtm->rtm_flags |= nh_flags;
5760 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5761 &nh_flags, false) < 0)
5762 goto nla_put_failure;
5764 rtm->rtm_flags |= nh_flags;
5767 if (rt6_flags & RTF_EXPIRES) {
5768 expires = dst ? dst->expires : rt->expires;
5773 if (READ_ONCE(rt->offload))
5774 rtm->rtm_flags |= RTM_F_OFFLOAD;
5775 if (READ_ONCE(rt->trap))
5776 rtm->rtm_flags |= RTM_F_TRAP;
5777 if (READ_ONCE(rt->offload_failed))
5778 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5781 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5782 goto nla_put_failure;
5784 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5785 goto nla_put_failure;
5788 nlmsg_end(skb, nlh);
5792 nlmsg_cancel(skb, nlh);
5796 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5798 const struct net_device *dev = arg;
5800 if (nh->fib_nh_dev == dev)
5806 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5807 const struct net_device *dev)
5810 struct net_device *_dev = (struct net_device *)dev;
5812 return !!nexthop_for_each_fib6_nh(f6i->nh,
5813 fib6_info_nh_uses_dev,
5817 if (f6i->fib6_nh->fib_nh_dev == dev)
5820 if (f6i->fib6_nsiblings) {
5821 struct fib6_info *sibling, *next_sibling;
5823 list_for_each_entry_safe(sibling, next_sibling,
5824 &f6i->fib6_siblings, fib6_siblings) {
5825 if (sibling->fib6_nh->fib_nh_dev == dev)
5833 struct fib6_nh_exception_dump_walker {
5834 struct rt6_rtnl_dump_arg *dump;
5835 struct fib6_info *rt;
5841 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5843 struct fib6_nh_exception_dump_walker *w = arg;
5844 struct rt6_rtnl_dump_arg *dump = w->dump;
5845 struct rt6_exception_bucket *bucket;
5846 struct rt6_exception *rt6_ex;
5849 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5853 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5854 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5860 /* Expiration of entries doesn't bump sernum, insertion
5861 * does. Removal is triggered by insertion, so we can
5862 * rely on the fact that if entries change between two
5863 * partial dumps, this node is scanned again completely,
5864 * see rt6_insert_exception() and fib6_dump_table().
5866 * Count expired entries we go through as handled
5867 * entries that we'll skip next time, in case of partial
5868 * node dump. Otherwise, if entries expire meanwhile,
5869 * we'll skip the wrong amount.
5871 if (rt6_check_expired(rt6_ex->rt6i)) {
5876 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5877 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5879 NETLINK_CB(dump->cb->skb).portid,
5880 dump->cb->nlh->nlmsg_seq, w->flags);
5892 /* Return -1 if done with node, number of handled routes on partial dump */
5893 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5895 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5896 struct fib_dump_filter *filter = &arg->filter;
5897 unsigned int flags = NLM_F_MULTI;
5898 struct net *net = arg->net;
5901 if (rt == net->ipv6.fib6_null_entry)
5904 if ((filter->flags & RTM_F_PREFIX) &&
5905 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5906 /* success since this is not a prefix route */
5909 if (filter->filter_set &&
5910 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5911 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5912 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5916 if (filter->filter_set ||
5917 !filter->dump_routes || !filter->dump_exceptions) {
5918 flags |= NLM_F_DUMP_FILTERED;
5921 if (filter->dump_routes) {
5925 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5927 NETLINK_CB(arg->cb->skb).portid,
5928 arg->cb->nlh->nlmsg_seq, flags)) {
5935 if (filter->dump_exceptions) {
5936 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5945 err = nexthop_for_each_fib6_nh(rt->nh,
5946 rt6_nh_dump_exceptions,
5949 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5954 return count + w.count;
5960 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5961 const struct nlmsghdr *nlh,
5963 struct netlink_ext_ack *extack)
5968 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5969 NL_SET_ERR_MSG_MOD(extack,
5970 "Invalid header for get route request");
5974 if (!netlink_strict_get_check(skb))
5975 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5976 rtm_ipv6_policy, extack);
5978 rtm = nlmsg_data(nlh);
5979 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5980 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5981 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5983 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5986 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5987 NL_SET_ERR_MSG_MOD(extack,
5988 "Invalid flags for get route request");
5992 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5993 rtm_ipv6_policy, extack);
5997 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5998 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5999 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
6003 for (i = 0; i <= RTA_MAX; i++) {
6019 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
6027 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
6028 struct netlink_ext_ack *extack)
6030 struct net *net = sock_net(in_skb->sk);
6031 struct nlattr *tb[RTA_MAX+1];
6032 int err, iif = 0, oif = 0;
6033 struct fib6_info *from;
6034 struct dst_entry *dst;
6035 struct rt6_info *rt;
6036 struct sk_buff *skb;
6038 struct flowi6 fl6 = {};
6041 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
6046 rtm = nlmsg_data(nlh);
6047 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
6048 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
6051 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6054 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
6058 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6061 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
6065 iif = nla_get_u32(tb[RTA_IIF]);
6068 oif = nla_get_u32(tb[RTA_OIF]);
6071 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6074 fl6.flowi6_uid = make_kuid(current_user_ns(),
6075 nla_get_u32(tb[RTA_UID]));
6077 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6080 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6083 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6085 if (tb[RTA_IP_PROTO]) {
6086 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
6087 &fl6.flowi6_proto, AF_INET6,
6094 struct net_device *dev;
6099 dev = dev_get_by_index_rcu(net, iif);
6106 fl6.flowi6_iif = iif;
6108 if (!ipv6_addr_any(&fl6.saddr))
6109 flags |= RT6_LOOKUP_F_HAS_SADDR;
6111 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6115 fl6.flowi6_oif = oif;
6117 dst = ip6_route_output(net, NULL, &fl6);
6121 rt = dst_rt6_info(dst);
6122 if (rt->dst.error) {
6123 err = rt->dst.error;
6128 if (rt == net->ipv6.ip6_null_entry) {
6129 err = rt->dst.error;
6134 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
6141 skb_dst_set(skb, &rt->dst);
6144 from = rcu_dereference(rt->from);
6147 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6149 NETLINK_CB(in_skb).portid,
6152 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6153 &fl6.saddr, iif, RTM_NEWROUTE,
6154 NETLINK_CB(in_skb).portid,
6166 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6171 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6172 unsigned int nlm_flags)
6174 struct sk_buff *skb;
6175 struct net *net = info->nl_net;
6180 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6182 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6186 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6187 event, info->portid, seq, nlm_flags);
6189 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6190 WARN_ON(err == -EMSGSIZE);
6194 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6195 info->nlh, gfp_any());
6199 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6202 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6203 struct nl_info *info)
6205 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6206 struct sk_buff *skb;
6209 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6213 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6214 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6216 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6217 WARN_ON(err == -EMSGSIZE);
6221 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6222 info->nlh, gfp_any());
6226 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6229 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
6230 bool offload, bool trap, bool offload_failed)
6232 struct sk_buff *skb;
6235 if (READ_ONCE(f6i->offload) == offload &&
6236 READ_ONCE(f6i->trap) == trap &&
6237 READ_ONCE(f6i->offload_failed) == offload_failed)
6240 WRITE_ONCE(f6i->offload, offload);
6241 WRITE_ONCE(f6i->trap, trap);
6243 /* 2 means send notifications only if offload_failed was changed. */
6244 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6245 READ_ONCE(f6i->offload_failed) == offload_failed)
6248 WRITE_ONCE(f6i->offload_failed, offload_failed);
6250 if (!rcu_access_pointer(f6i->fib6_node))
6251 /* The route was removed from the tree, do not send
6256 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6259 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6265 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6268 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6269 WARN_ON(err == -EMSGSIZE);
6274 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6278 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6280 EXPORT_SYMBOL(fib6_info_hw_flags_set);
6282 static int ip6_route_dev_notify(struct notifier_block *this,
6283 unsigned long event, void *ptr)
6285 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6286 struct net *net = dev_net(dev);
6288 if (!(dev->flags & IFF_LOOPBACK))
6291 if (event == NETDEV_REGISTER) {
6292 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6293 net->ipv6.ip6_null_entry->dst.dev = dev;
6294 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6295 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6296 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6297 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6298 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6299 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6301 } else if (event == NETDEV_UNREGISTER &&
6302 dev->reg_state != NETREG_UNREGISTERED) {
6303 /* NETDEV_UNREGISTER could be fired for multiple times by
6304 * netdev_wait_allrefs(). Make sure we only call this once.
6306 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6307 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6308 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6309 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6320 #ifdef CONFIG_PROC_FS
6321 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6323 struct net *net = (struct net *)seq->private;
6324 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6325 net->ipv6.rt6_stats->fib_nodes,
6326 net->ipv6.rt6_stats->fib_route_nodes,
6327 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6328 net->ipv6.rt6_stats->fib_rt_entries,
6329 net->ipv6.rt6_stats->fib_rt_cache,
6330 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6331 net->ipv6.rt6_stats->fib_discarded_routes);
6335 #endif /* CONFIG_PROC_FS */
6337 #ifdef CONFIG_SYSCTL
6339 static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6340 void *buffer, size_t *lenp, loff_t *ppos)
6348 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6352 net = (struct net *)ctl->extra1;
6353 delay = net->ipv6.sysctl.flush_delay;
6354 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6358 static struct ctl_table ipv6_route_table_template[] = {
6360 .procname = "max_size",
6361 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6362 .maxlen = sizeof(int),
6364 .proc_handler = proc_dointvec,
6367 .procname = "gc_thresh",
6368 .data = &ip6_dst_ops_template.gc_thresh,
6369 .maxlen = sizeof(int),
6371 .proc_handler = proc_dointvec,
6374 .procname = "flush",
6375 .data = &init_net.ipv6.sysctl.flush_delay,
6376 .maxlen = sizeof(int),
6378 .proc_handler = ipv6_sysctl_rtcache_flush
6381 .procname = "gc_min_interval",
6382 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6383 .maxlen = sizeof(int),
6385 .proc_handler = proc_dointvec_jiffies,
6388 .procname = "gc_timeout",
6389 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6390 .maxlen = sizeof(int),
6392 .proc_handler = proc_dointvec_jiffies,
6395 .procname = "gc_interval",
6396 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6397 .maxlen = sizeof(int),
6399 .proc_handler = proc_dointvec_jiffies,
6402 .procname = "gc_elasticity",
6403 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6404 .maxlen = sizeof(int),
6406 .proc_handler = proc_dointvec,
6409 .procname = "mtu_expires",
6410 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6411 .maxlen = sizeof(int),
6413 .proc_handler = proc_dointvec_jiffies,
6416 .procname = "min_adv_mss",
6417 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6418 .maxlen = sizeof(int),
6420 .proc_handler = proc_dointvec,
6423 .procname = "gc_min_interval_ms",
6424 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6425 .maxlen = sizeof(int),
6427 .proc_handler = proc_dointvec_ms_jiffies,
6430 .procname = "skip_notify_on_dev_down",
6431 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6432 .maxlen = sizeof(u8),
6434 .proc_handler = proc_dou8vec_minmax,
6435 .extra1 = SYSCTL_ZERO,
6436 .extra2 = SYSCTL_ONE,
6440 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6442 struct ctl_table *table;
6444 table = kmemdup(ipv6_route_table_template,
6445 sizeof(ipv6_route_table_template),
6449 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
6450 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6451 table[2].data = &net->ipv6.sysctl.flush_delay;
6452 table[2].extra1 = net;
6453 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6454 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6455 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6456 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6457 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6458 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6459 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6460 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6466 size_t ipv6_route_sysctl_table_size(struct net *net)
6468 /* Don't export sysctls to unprivileged users */
6469 if (net->user_ns != &init_user_ns)
6472 return ARRAY_SIZE(ipv6_route_table_template);
6476 static int __net_init ip6_route_net_init(struct net *net)
6480 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6481 sizeof(net->ipv6.ip6_dst_ops));
6483 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6484 goto out_ip6_dst_ops;
6486 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6487 if (!net->ipv6.fib6_null_entry)
6488 goto out_ip6_dst_entries;
6489 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6490 sizeof(*net->ipv6.fib6_null_entry));
6492 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6493 sizeof(*net->ipv6.ip6_null_entry),
6495 if (!net->ipv6.ip6_null_entry)
6496 goto out_fib6_null_entry;
6497 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6498 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6499 ip6_template_metrics, true);
6500 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
6502 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6503 net->ipv6.fib6_has_custom_rules = false;
6504 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6505 sizeof(*net->ipv6.ip6_prohibit_entry),
6507 if (!net->ipv6.ip6_prohibit_entry)
6508 goto out_ip6_null_entry;
6509 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6510 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6511 ip6_template_metrics, true);
6512 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
6514 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6515 sizeof(*net->ipv6.ip6_blk_hole_entry),
6517 if (!net->ipv6.ip6_blk_hole_entry)
6518 goto out_ip6_prohibit_entry;
6519 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6520 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6521 ip6_template_metrics, true);
6522 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
6523 #ifdef CONFIG_IPV6_SUBTREES
6524 net->ipv6.fib6_routes_require_src = 0;
6528 net->ipv6.sysctl.flush_delay = 0;
6529 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
6530 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6531 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6532 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6533 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6534 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6535 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6536 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6538 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
6544 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6545 out_ip6_prohibit_entry:
6546 kfree(net->ipv6.ip6_prohibit_entry);
6548 kfree(net->ipv6.ip6_null_entry);
6550 out_fib6_null_entry:
6551 kfree(net->ipv6.fib6_null_entry);
6552 out_ip6_dst_entries:
6553 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6558 static void __net_exit ip6_route_net_exit(struct net *net)
6560 kfree(net->ipv6.fib6_null_entry);
6561 kfree(net->ipv6.ip6_null_entry);
6562 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6563 kfree(net->ipv6.ip6_prohibit_entry);
6564 kfree(net->ipv6.ip6_blk_hole_entry);
6566 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6569 static int __net_init ip6_route_net_init_late(struct net *net)
6571 #ifdef CONFIG_PROC_FS
6572 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6573 &ipv6_route_seq_ops,
6574 sizeof(struct ipv6_route_iter)))
6577 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6578 rt6_stats_seq_show, NULL)) {
6579 remove_proc_entry("ipv6_route", net->proc_net);
6586 static void __net_exit ip6_route_net_exit_late(struct net *net)
6588 #ifdef CONFIG_PROC_FS
6589 remove_proc_entry("ipv6_route", net->proc_net);
6590 remove_proc_entry("rt6_stats", net->proc_net);
6594 static struct pernet_operations ip6_route_net_ops = {
6595 .init = ip6_route_net_init,
6596 .exit = ip6_route_net_exit,
6599 static int __net_init ipv6_inetpeer_init(struct net *net)
6601 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6605 inet_peer_base_init(bp);
6606 net->ipv6.peers = bp;
6610 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6612 struct inet_peer_base *bp = net->ipv6.peers;
6614 net->ipv6.peers = NULL;
6615 inetpeer_invalidate_tree(bp);
6619 static struct pernet_operations ipv6_inetpeer_ops = {
6620 .init = ipv6_inetpeer_init,
6621 .exit = ipv6_inetpeer_exit,
6624 static struct pernet_operations ip6_route_net_late_ops = {
6625 .init = ip6_route_net_init_late,
6626 .exit = ip6_route_net_exit_late,
6629 static struct notifier_block ip6_route_dev_notifier = {
6630 .notifier_call = ip6_route_dev_notify,
6631 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6634 void __init ip6_route_init_special_entries(void)
6636 /* Registering of the loopback is done before this portion of code,
6637 * the loopback reference in rt6_info will not be taken, do it
6638 * manually for init_net */
6639 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6640 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6641 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6642 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6643 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6644 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6645 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6646 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6650 #if IS_BUILTIN(CONFIG_IPV6)
6651 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6652 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6654 BTF_ID_LIST(btf_fib6_info_id)
6655 BTF_ID(struct, fib6_info)
6657 static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6658 .seq_ops = &ipv6_route_seq_ops,
6659 .init_seq_private = bpf_iter_init_seq_net,
6660 .fini_seq_private = bpf_iter_fini_seq_net,
6661 .seq_priv_size = sizeof(struct ipv6_route_iter),
6664 static struct bpf_iter_reg ipv6_route_reg_info = {
6665 .target = "ipv6_route",
6666 .ctx_arg_info_size = 1,
6668 { offsetof(struct bpf_iter__ipv6_route, rt),
6669 PTR_TO_BTF_ID_OR_NULL },
6671 .seq_info = &ipv6_route_seq_info,
6674 static int __init bpf_iter_register(void)
6676 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
6677 return bpf_iter_reg_target(&ipv6_route_reg_info);
6680 static void bpf_iter_unregister(void)
6682 bpf_iter_unreg_target(&ipv6_route_reg_info);
6687 int __init ip6_route_init(void)
6693 ip6_dst_ops_template.kmem_cachep =
6694 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6695 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
6696 if (!ip6_dst_ops_template.kmem_cachep)
6699 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6701 goto out_kmem_cache;
6703 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6705 goto out_dst_entries;
6707 ret = register_pernet_subsys(&ip6_route_net_ops);
6709 goto out_register_inetpeer;
6711 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6715 goto out_register_subsys;
6721 ret = fib6_rules_init();
6725 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6727 goto fib6_rules_init;
6729 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6730 inet6_rtm_newroute, NULL, 0);
6732 goto out_register_late_subsys;
6734 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6735 inet6_rtm_delroute, NULL, 0);
6737 goto out_register_late_subsys;
6739 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6740 inet6_rtm_getroute, NULL,
6741 RTNL_FLAG_DOIT_UNLOCKED);
6743 goto out_register_late_subsys;
6745 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6747 goto out_register_late_subsys;
6749 #if IS_BUILTIN(CONFIG_IPV6)
6750 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6751 ret = bpf_iter_register();
6753 goto out_register_late_subsys;
6757 for_each_possible_cpu(cpu) {
6758 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6760 INIT_LIST_HEAD(&ul->head);
6761 INIT_LIST_HEAD(&ul->quarantine);
6762 spin_lock_init(&ul->lock);
6768 out_register_late_subsys:
6769 rtnl_unregister_all(PF_INET6);
6770 unregister_pernet_subsys(&ip6_route_net_late_ops);
6772 fib6_rules_cleanup();
6777 out_register_subsys:
6778 unregister_pernet_subsys(&ip6_route_net_ops);
6779 out_register_inetpeer:
6780 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6782 dst_entries_destroy(&ip6_dst_blackhole_ops);
6784 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6788 void ip6_route_cleanup(void)
6790 #if IS_BUILTIN(CONFIG_IPV6)
6791 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6792 bpf_iter_unregister();
6795 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6796 unregister_pernet_subsys(&ip6_route_net_late_ops);
6797 fib6_rules_cleanup();
6800 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6801 unregister_pernet_subsys(&ip6_route_net_ops);
6802 dst_entries_destroy(&ip6_dst_blackhole_ops);
6803 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);