1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
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;
136 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
138 void rt6_uncached_list_add(struct rt6_info *rt)
140 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
142 rt->dst.rt_uncached_list = ul;
144 spin_lock_bh(&ul->lock);
145 list_add_tail(&rt->dst.rt_uncached, &ul->head);
146 spin_unlock_bh(&ul->lock);
149 void rt6_uncached_list_del(struct rt6_info *rt)
151 if (!list_empty(&rt->dst.rt_uncached)) {
152 struct uncached_list *ul = rt->dst.rt_uncached_list;
154 spin_lock_bh(&ul->lock);
155 list_del_init(&rt->dst.rt_uncached);
156 spin_unlock_bh(&ul->lock);
160 static void rt6_uncached_list_flush_dev(struct net_device *dev)
164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
166 struct rt6_info *rt, *safe;
168 if (list_empty(&ul->head))
171 spin_lock_bh(&ul->lock);
172 list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
173 struct inet6_dev *rt_idev = rt->rt6i_idev;
174 struct net_device *rt_dev = rt->dst.dev;
175 bool handled = false;
177 if (rt_idev && rt_idev->dev == dev) {
178 rt->rt6i_idev = in6_dev_get(blackhole_netdev);
179 in6_dev_put(rt_idev);
184 rt->dst.dev = blackhole_netdev;
185 netdev_ref_replace(rt_dev, blackhole_netdev,
186 &rt->dst.dev_tracker,
191 list_del_init(&rt->dst.rt_uncached);
193 spin_unlock_bh(&ul->lock);
197 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
201 if (!ipv6_addr_any(p))
202 return (const void *) p;
204 return &ipv6_hdr(skb)->daddr;
208 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
209 struct net_device *dev,
215 daddr = choose_neigh_daddr(gw, skb, daddr);
216 n = __ipv6_neigh_lookup(dev, daddr);
220 n = neigh_create(&nd_tbl, daddr, dev);
221 return IS_ERR(n) ? NULL : n;
224 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
228 const struct rt6_info *rt = dst_rt6_info(dst);
230 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
231 dst->dev, skb, daddr);
234 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
236 const struct rt6_info *rt = dst_rt6_info(dst);
237 struct net_device *dev = dst->dev;
239 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
242 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
244 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
246 __ipv6_confirm_neigh(dev, daddr);
249 static struct dst_ops ip6_dst_ops_template = {
253 .check = ip6_dst_check,
254 .default_advmss = ip6_default_advmss,
256 .cow_metrics = dst_cow_metrics_generic,
257 .destroy = ip6_dst_destroy,
258 .ifdown = ip6_dst_ifdown,
259 .negative_advice = ip6_negative_advice,
260 .link_failure = ip6_link_failure,
261 .update_pmtu = ip6_rt_update_pmtu,
262 .redirect = rt6_do_redirect,
263 .local_out = __ip6_local_out,
264 .neigh_lookup = ip6_dst_neigh_lookup,
265 .confirm_neigh = ip6_confirm_neigh,
268 static struct dst_ops ip6_dst_blackhole_ops = {
270 .default_advmss = ip6_default_advmss,
271 .neigh_lookup = ip6_dst_neigh_lookup,
272 .check = ip6_dst_check,
273 .destroy = ip6_dst_destroy,
274 .cow_metrics = dst_cow_metrics_generic,
275 .update_pmtu = dst_blackhole_update_pmtu,
276 .redirect = dst_blackhole_redirect,
277 .mtu = dst_blackhole_mtu,
280 static const u32 ip6_template_metrics[RTAX_MAX] = {
281 [RTAX_HOPLIMIT - 1] = 0,
284 static const struct fib6_info fib6_null_entry_template = {
285 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
286 .fib6_protocol = RTPROT_KERNEL,
287 .fib6_metric = ~(u32)0,
288 .fib6_ref = REFCOUNT_INIT(1),
289 .fib6_type = RTN_UNREACHABLE,
290 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
293 static const struct rt6_info ip6_null_entry_template = {
295 .__rcuref = RCUREF_INIT(1),
297 .obsolete = DST_OBSOLETE_FORCE_CHK,
298 .error = -ENETUNREACH,
299 .input = ip6_pkt_discard,
300 .output = ip6_pkt_discard_out,
302 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
305 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
307 static const struct rt6_info ip6_prohibit_entry_template = {
309 .__rcuref = RCUREF_INIT(1),
311 .obsolete = DST_OBSOLETE_FORCE_CHK,
313 .input = ip6_pkt_prohibit,
314 .output = ip6_pkt_prohibit_out,
316 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
319 static const struct rt6_info ip6_blk_hole_entry_template = {
321 .__rcuref = RCUREF_INIT(1),
323 .obsolete = DST_OBSOLETE_FORCE_CHK,
325 .input = dst_discard,
326 .output = dst_discard_out,
328 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
333 static void rt6_info_init(struct rt6_info *rt)
335 memset_after(rt, 0, dst);
338 /* allocate dst with ip6_dst_ops */
339 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
342 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
343 DST_OBSOLETE_FORCE_CHK, flags);
347 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
352 EXPORT_SYMBOL(ip6_dst_alloc);
354 static void ip6_dst_destroy(struct dst_entry *dst)
356 struct rt6_info *rt = dst_rt6_info(dst);
357 struct fib6_info *from;
358 struct inet6_dev *idev;
360 ip_dst_metrics_put(dst);
361 rt6_uncached_list_del(rt);
363 idev = rt->rt6i_idev;
365 rt->rt6i_idev = NULL;
369 from = unrcu_pointer(xchg(&rt->from, NULL));
370 fib6_info_release(from);
373 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
375 struct rt6_info *rt = dst_rt6_info(dst);
376 struct inet6_dev *idev = rt->rt6i_idev;
377 struct fib6_info *from;
379 if (idev && idev->dev != blackhole_netdev) {
380 struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev);
382 if (blackhole_idev) {
383 rt->rt6i_idev = blackhole_idev;
387 from = unrcu_pointer(xchg(&rt->from, NULL));
388 fib6_info_release(from);
391 static bool __rt6_check_expired(const struct rt6_info *rt)
393 if (rt->rt6i_flags & RTF_EXPIRES)
394 return time_after(jiffies, rt->dst.expires);
399 static bool rt6_check_expired(const struct rt6_info *rt)
401 struct fib6_info *from;
403 from = rcu_dereference(rt->from);
405 if (rt->rt6i_flags & RTF_EXPIRES) {
406 if (time_after(jiffies, rt->dst.expires))
409 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
410 fib6_check_expired(from);
415 static struct fib6_info *
416 rt6_multipath_first_sibling_rcu(const struct fib6_info *rt)
418 struct fib6_info *iter;
419 struct fib6_node *fn;
421 fn = rcu_dereference(rt->fib6_node);
424 iter = rcu_dereference(fn->leaf);
429 if (iter->fib6_metric == rt->fib6_metric &&
430 rt6_qualify_for_ecmp(iter))
432 iter = rcu_dereference(iter->fib6_next);
439 void fib6_select_path(const struct net *net, struct fib6_result *res,
440 struct flowi6 *fl6, int oif, bool have_oif_match,
441 const struct sk_buff *skb, int strict)
443 struct fib6_info *first, *match = res->f6i;
444 struct fib6_info *sibling;
447 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
450 if (match->nh && have_oif_match && res->nh)
454 IP6CB(skb)->flags |= IP6SKB_MULTIPATH;
456 /* We might have already computed the hash for ICMPv6 errors. In such
457 * case it will always be non-zero. Otherwise now is the time to do it.
460 (!match->nh || nexthop_is_multipath(match->nh)))
461 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
463 if (unlikely(match->nh)) {
464 nexthop_path_fib6_result(res, fl6->mp_hash);
468 first = rt6_multipath_first_sibling_rcu(match);
473 if (hash <= atomic_read(&first->fib6_nh->fib_nh_upper_bound)) {
474 if (rt6_score_route(first->fib6_nh, first->fib6_flags, oif,
480 list_for_each_entry_rcu(sibling, &first->fib6_siblings,
482 const struct fib6_nh *nh = sibling->fib6_nh;
485 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
486 if (hash > nh_upper_bound)
488 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
496 res->nh = match->fib6_nh;
500 * Route lookup. rcu_read_lock() should be held.
503 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
504 const struct in6_addr *saddr, int oif, int flags)
506 const struct net_device *dev;
508 if (nh->fib_nh_flags & RTNH_F_DEAD)
511 dev = nh->fib_nh_dev;
513 if (dev->ifindex == oif)
516 if (ipv6_chk_addr(net, saddr, dev,
517 flags & RT6_LOOKUP_F_IFACE))
524 struct fib6_nh_dm_arg {
526 const struct in6_addr *saddr;
532 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
534 struct fib6_nh_dm_arg *arg = _arg;
537 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
541 /* returns fib6_nh from nexthop or NULL */
542 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
543 struct fib6_result *res,
544 const struct in6_addr *saddr,
547 struct fib6_nh_dm_arg arg = {
554 if (nexthop_is_blackhole(nh))
557 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
563 static void rt6_device_match(struct net *net, struct fib6_result *res,
564 const struct in6_addr *saddr, int oif, int flags)
566 struct fib6_info *f6i = res->f6i;
567 struct fib6_info *spf6i;
570 if (!oif && ipv6_addr_any(saddr)) {
571 if (unlikely(f6i->nh)) {
572 nh = nexthop_fib6_nh(f6i->nh);
573 if (nexthop_is_blackhole(f6i->nh))
578 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
582 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
583 bool matched = false;
585 if (unlikely(spf6i->nh)) {
586 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
592 if (__rt6_device_match(net, nh, saddr, oif, flags))
601 if (oif && flags & RT6_LOOKUP_F_IFACE) {
602 res->f6i = net->ipv6.fib6_null_entry;
603 nh = res->f6i->fib6_nh;
607 if (unlikely(f6i->nh)) {
608 nh = nexthop_fib6_nh(f6i->nh);
609 if (nexthop_is_blackhole(f6i->nh))
615 if (nh->fib_nh_flags & RTNH_F_DEAD) {
616 res->f6i = net->ipv6.fib6_null_entry;
617 nh = res->f6i->fib6_nh;
621 res->fib6_type = res->f6i->fib6_type;
622 res->fib6_flags = res->f6i->fib6_flags;
626 res->fib6_flags |= RTF_REJECT;
627 res->fib6_type = RTN_BLACKHOLE;
631 #ifdef CONFIG_IPV6_ROUTER_PREF
632 struct __rt6_probe_work {
633 struct work_struct work;
634 struct in6_addr target;
635 struct net_device *dev;
636 netdevice_tracker dev_tracker;
639 static void rt6_probe_deferred(struct work_struct *w)
641 struct in6_addr mcaddr;
642 struct __rt6_probe_work *work =
643 container_of(w, struct __rt6_probe_work, work);
645 addrconf_addr_solict_mult(&work->target, &mcaddr);
646 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
647 netdev_put(work->dev, &work->dev_tracker);
651 static void rt6_probe(struct fib6_nh *fib6_nh)
653 struct __rt6_probe_work *work = NULL;
654 const struct in6_addr *nh_gw;
655 unsigned long last_probe;
656 struct neighbour *neigh;
657 struct net_device *dev;
658 struct inet6_dev *idev;
661 * Okay, this does not seem to be appropriate
662 * for now, however, we need to check if it
663 * is really so; aka Router Reachability Probing.
665 * Router Reachability Probe MUST be rate-limited
666 * to no more than one per minute.
668 if (!fib6_nh->fib_nh_gw_family)
671 nh_gw = &fib6_nh->fib_nh_gw6;
672 dev = fib6_nh->fib_nh_dev;
674 last_probe = READ_ONCE(fib6_nh->last_probe);
675 idev = __in6_dev_get(dev);
678 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
680 if (READ_ONCE(neigh->nud_state) & NUD_VALID)
683 write_lock_bh(&neigh->lock);
684 if (!(neigh->nud_state & NUD_VALID) &&
687 READ_ONCE(idev->cnf.rtr_probe_interval))) {
688 work = kmalloc(sizeof(*work), GFP_ATOMIC);
690 __neigh_set_probe_once(neigh);
692 write_unlock_bh(&neigh->lock);
693 } else if (time_after(jiffies, last_probe +
694 READ_ONCE(idev->cnf.rtr_probe_interval))) {
695 work = kmalloc(sizeof(*work), GFP_ATOMIC);
698 if (!work || cmpxchg(&fib6_nh->last_probe,
699 last_probe, jiffies) != last_probe) {
702 INIT_WORK(&work->work, rt6_probe_deferred);
703 work->target = *nh_gw;
704 netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC);
706 schedule_work(&work->work);
713 static inline void rt6_probe(struct fib6_nh *fib6_nh)
719 * Default Router Selection (RFC 2461 6.3.6)
721 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
723 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
724 struct neighbour *neigh;
727 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
728 &fib6_nh->fib_nh_gw6);
730 u8 nud_state = READ_ONCE(neigh->nud_state);
732 if (nud_state & NUD_VALID)
733 ret = RT6_NUD_SUCCEED;
734 #ifdef CONFIG_IPV6_ROUTER_PREF
735 else if (!(nud_state & NUD_FAILED))
736 ret = RT6_NUD_SUCCEED;
738 ret = RT6_NUD_FAIL_PROBE;
741 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
742 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
749 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
754 if (!oif || nh->fib_nh_dev->ifindex == oif)
757 if (!m && (strict & RT6_LOOKUP_F_IFACE))
758 return RT6_NUD_FAIL_HARD;
759 #ifdef CONFIG_IPV6_ROUTER_PREF
760 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
762 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
763 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
764 int n = rt6_check_neigh(nh);
771 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
772 int oif, int strict, int *mpri, bool *do_rr)
774 bool match_do_rr = false;
778 if (nh->fib_nh_flags & RTNH_F_DEAD)
781 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
782 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
783 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
786 m = rt6_score_route(nh, fib6_flags, oif, strict);
787 if (m == RT6_NUD_FAIL_DO_RR) {
789 m = 0; /* lowest valid score */
790 } else if (m == RT6_NUD_FAIL_HARD) {
794 if (strict & RT6_LOOKUP_F_REACHABLE)
797 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
799 *do_rr = match_do_rr;
807 struct fib6_nh_frl_arg {
816 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
818 struct fib6_nh_frl_arg *arg = _arg;
821 return find_match(nh, arg->flags, arg->oif, arg->strict,
822 arg->mpri, arg->do_rr);
825 static void __find_rr_leaf(struct fib6_info *f6i_start,
826 struct fib6_info *nomatch, u32 metric,
827 struct fib6_result *res, struct fib6_info **cont,
828 int oif, int strict, bool *do_rr, int *mpri)
830 struct fib6_info *f6i;
832 for (f6i = f6i_start;
833 f6i && f6i != nomatch;
834 f6i = rcu_dereference(f6i->fib6_next)) {
835 bool matched = false;
838 if (cont && f6i->fib6_metric != metric) {
843 if (fib6_check_expired(f6i))
846 if (unlikely(f6i->nh)) {
847 struct fib6_nh_frl_arg arg = {
848 .flags = f6i->fib6_flags,
855 if (nexthop_is_blackhole(f6i->nh)) {
856 res->fib6_flags = RTF_REJECT;
857 res->fib6_type = RTN_BLACKHOLE;
859 res->nh = nexthop_fib6_nh(f6i->nh);
862 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
869 if (find_match(nh, f6i->fib6_flags, oif, strict,
876 res->fib6_flags = f6i->fib6_flags;
877 res->fib6_type = f6i->fib6_type;
882 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
883 struct fib6_info *rr_head, int oif, int strict,
884 bool *do_rr, struct fib6_result *res)
886 u32 metric = rr_head->fib6_metric;
887 struct fib6_info *cont = NULL;
890 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
891 oif, strict, do_rr, &mpri);
893 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
894 oif, strict, do_rr, &mpri);
896 if (res->f6i || !cont)
899 __find_rr_leaf(cont, NULL, metric, res, NULL,
900 oif, strict, do_rr, &mpri);
903 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
904 struct fib6_result *res, int strict)
906 struct fib6_info *leaf = rcu_dereference(fn->leaf);
907 struct fib6_info *rt0;
911 /* make sure this function or its helpers sets f6i */
914 if (!leaf || leaf == net->ipv6.fib6_null_entry)
917 rt0 = rcu_dereference(fn->rr_ptr);
921 /* Double check to make sure fn is not an intermediate node
922 * and fn->leaf does not points to its child's leaf
923 * (This might happen if all routes under fn are deleted from
924 * the tree and fib6_repair_tree() is called on the node.)
926 key_plen = rt0->fib6_dst.plen;
927 #ifdef CONFIG_IPV6_SUBTREES
928 if (rt0->fib6_src.plen)
929 key_plen = rt0->fib6_src.plen;
931 if (fn->fn_bit != key_plen)
934 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
936 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
938 /* no entries matched; do round-robin */
939 if (!next || next->fib6_metric != rt0->fib6_metric)
943 spin_lock_bh(&leaf->fib6_table->tb6_lock);
944 /* make sure next is not being deleted from the tree */
946 rcu_assign_pointer(fn->rr_ptr, next);
947 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
953 res->f6i = net->ipv6.fib6_null_entry;
954 res->nh = res->f6i->fib6_nh;
955 res->fib6_flags = res->f6i->fib6_flags;
956 res->fib6_type = res->f6i->fib6_type;
960 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
962 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
963 res->nh->fib_nh_gw_family;
966 #ifdef CONFIG_IPV6_ROUTE_INFO
967 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
968 const struct in6_addr *gwaddr)
970 struct net *net = dev_net(dev);
971 struct route_info *rinfo = (struct route_info *) opt;
972 struct in6_addr prefix_buf, *prefix;
973 struct fib6_table *table;
975 unsigned long lifetime;
976 struct fib6_info *rt;
978 if (len < sizeof(struct route_info)) {
982 /* Sanity check for prefix_len and length */
983 if (rinfo->length > 3) {
985 } else if (rinfo->prefix_len > 128) {
987 } else if (rinfo->prefix_len > 64) {
988 if (rinfo->length < 2) {
991 } else if (rinfo->prefix_len > 0) {
992 if (rinfo->length < 1) {
997 pref = rinfo->route_pref;
998 if (pref == ICMPV6_ROUTER_PREF_INVALID)
1001 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
1003 if (rinfo->length == 3)
1004 prefix = (struct in6_addr *)rinfo->prefix;
1006 /* this function is safe */
1007 ipv6_addr_prefix(&prefix_buf,
1008 (struct in6_addr *)rinfo->prefix,
1010 prefix = &prefix_buf;
1013 if (rinfo->prefix_len == 0)
1014 rt = rt6_get_dflt_router(net, gwaddr, dev);
1016 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
1019 if (rt && !lifetime) {
1020 ip6_del_rt(net, rt, false);
1024 if (!rt && lifetime)
1025 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
1028 rt->fib6_flags = RTF_ROUTEINFO |
1029 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1032 table = rt->fib6_table;
1033 spin_lock_bh(&table->tb6_lock);
1035 if (!addrconf_finite_timeout(lifetime)) {
1036 fib6_clean_expires(rt);
1037 fib6_remove_gc_list(rt);
1039 fib6_set_expires(rt, jiffies + HZ * lifetime);
1040 fib6_add_gc_list(rt);
1043 spin_unlock_bh(&table->tb6_lock);
1045 fib6_info_release(rt);
1052 * Misc support functions
1055 /* called with rcu_lock held */
1056 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1058 struct net_device *dev = res->nh->fib_nh_dev;
1060 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1061 /* for copies of local routes, dst->dev needs to be the
1062 * device if it is a master device, the master device if
1063 * device is enslaved, and the loopback as the default
1065 if (netif_is_l3_slave(dev) &&
1066 !rt6_need_strict(&res->f6i->fib6_dst.addr))
1067 dev = l3mdev_master_dev_rcu(dev);
1068 else if (!netif_is_l3_master(dev))
1069 dev = dev_net(dev)->loopback_dev;
1070 /* last case is netif_is_l3_master(dev) is true in which
1071 * case we want dev returned to be dev
1078 static const int fib6_prop[RTN_MAX + 1] = {
1082 [RTN_BROADCAST] = 0,
1084 [RTN_MULTICAST] = 0,
1085 [RTN_BLACKHOLE] = -EINVAL,
1086 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1087 [RTN_PROHIBIT] = -EACCES,
1088 [RTN_THROW] = -EAGAIN,
1089 [RTN_NAT] = -EINVAL,
1090 [RTN_XRESOLVE] = -EINVAL,
1093 static int ip6_rt_type_to_error(u8 fib6_type)
1095 return fib6_prop[fib6_type];
1098 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1100 unsigned short flags = 0;
1102 if (rt->dst_nocount)
1103 flags |= DST_NOCOUNT;
1104 if (rt->dst_nopolicy)
1105 flags |= DST_NOPOLICY;
1110 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1112 rt->dst.error = ip6_rt_type_to_error(fib6_type);
1114 switch (fib6_type) {
1116 rt->dst.output = dst_discard_out;
1117 rt->dst.input = dst_discard;
1120 rt->dst.output = ip6_pkt_prohibit_out;
1121 rt->dst.input = ip6_pkt_prohibit;
1124 case RTN_UNREACHABLE:
1126 rt->dst.output = ip6_pkt_discard_out;
1127 rt->dst.input = ip6_pkt_discard;
1132 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1134 struct fib6_info *f6i = res->f6i;
1136 if (res->fib6_flags & RTF_REJECT) {
1137 ip6_rt_init_dst_reject(rt, res->fib6_type);
1142 rt->dst.output = ip6_output;
1144 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1145 rt->dst.input = ip6_input;
1146 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1147 rt->dst.input = ip6_mc_input;
1149 rt->dst.input = ip6_forward;
1152 if (res->nh->fib_nh_lws) {
1153 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1154 lwtunnel_set_redirect(&rt->dst);
1157 rt->dst.lastuse = jiffies;
1160 /* Caller must already hold reference to @from */
1161 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1163 rt->rt6i_flags &= ~RTF_EXPIRES;
1164 rcu_assign_pointer(rt->from, from);
1165 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1168 /* Caller must already hold reference to f6i in result */
1169 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1171 const struct fib6_nh *nh = res->nh;
1172 const struct net_device *dev = nh->fib_nh_dev;
1173 struct fib6_info *f6i = res->f6i;
1175 ip6_rt_init_dst(rt, res);
1177 rt->rt6i_dst = f6i->fib6_dst;
1178 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1179 rt->rt6i_flags = res->fib6_flags;
1180 if (nh->fib_nh_gw_family) {
1181 rt->rt6i_gateway = nh->fib_nh_gw6;
1182 rt->rt6i_flags |= RTF_GATEWAY;
1184 rt6_set_from(rt, f6i);
1185 #ifdef CONFIG_IPV6_SUBTREES
1186 rt->rt6i_src = f6i->fib6_src;
1190 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1191 struct in6_addr *saddr)
1193 struct fib6_node *pn, *sn;
1195 if (fn->fn_flags & RTN_TL_ROOT)
1197 pn = rcu_dereference(fn->parent);
1198 sn = FIB6_SUBTREE(pn);
1200 fn = fib6_node_lookup(sn, NULL, saddr);
1203 if (fn->fn_flags & RTN_RTINFO)
1208 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1210 struct rt6_info *rt = *prt;
1212 if (dst_hold_safe(&rt->dst))
1215 rt = net->ipv6.ip6_null_entry;
1224 /* called with rcu_lock held */
1225 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1227 struct net_device *dev = res->nh->fib_nh_dev;
1228 struct fib6_info *f6i = res->f6i;
1229 unsigned short flags;
1230 struct rt6_info *nrt;
1232 if (!fib6_info_hold_safe(f6i))
1235 flags = fib6_info_dst_flags(f6i);
1236 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1238 fib6_info_release(f6i);
1242 ip6_rt_copy_init(nrt, res);
1246 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1247 dst_hold(&nrt->dst);
1251 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
1252 struct fib6_table *table,
1254 const struct sk_buff *skb,
1257 struct fib6_result res = {};
1258 struct fib6_node *fn;
1259 struct rt6_info *rt;
1262 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1264 res.f6i = rcu_dereference(fn->leaf);
1266 res.f6i = net->ipv6.fib6_null_entry;
1268 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1271 if (res.f6i == net->ipv6.fib6_null_entry) {
1272 fn = fib6_backtrack(fn, &fl6->saddr);
1276 rt = net->ipv6.ip6_null_entry;
1279 } else if (res.fib6_flags & RTF_REJECT) {
1283 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1284 fl6->flowi6_oif != 0, skb, flags);
1286 /* Search through exception table */
1287 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1289 if (ip6_hold_safe(net, &rt))
1290 dst_use_noref(&rt->dst, jiffies);
1293 rt = ip6_create_rt_rcu(&res);
1297 trace_fib6_table_lookup(net, &res, table, fl6);
1304 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1305 const struct sk_buff *skb, int flags)
1307 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1309 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1311 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1312 const struct in6_addr *saddr, int oif,
1313 const struct sk_buff *skb, int strict)
1315 struct flowi6 fl6 = {
1319 struct dst_entry *dst;
1320 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1323 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1324 flags |= RT6_LOOKUP_F_HAS_SADDR;
1327 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1328 if (dst->error == 0)
1329 return dst_rt6_info(dst);
1335 EXPORT_SYMBOL(rt6_lookup);
1337 /* ip6_ins_rt is called with FREE table->tb6_lock.
1338 * It takes new route entry, the addition fails by any reason the
1339 * route is released.
1340 * Caller must hold dst before calling it.
1343 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1344 struct netlink_ext_ack *extack)
1347 struct fib6_table *table;
1349 table = rt->fib6_table;
1350 spin_lock_bh(&table->tb6_lock);
1351 err = fib6_add(&table->tb6_root, rt, info, extack);
1352 spin_unlock_bh(&table->tb6_lock);
1357 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1359 struct nl_info info = { .nl_net = net, };
1361 return __ip6_ins_rt(rt, &info, NULL);
1364 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1365 const struct in6_addr *daddr,
1366 const struct in6_addr *saddr)
1368 struct fib6_info *f6i = res->f6i;
1369 struct net_device *dev;
1370 struct rt6_info *rt;
1376 if (!fib6_info_hold_safe(f6i))
1379 dev = ip6_rt_get_dev_rcu(res);
1380 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1382 fib6_info_release(f6i);
1386 ip6_rt_copy_init(rt, res);
1387 rt->rt6i_flags |= RTF_CACHE;
1388 rt->rt6i_dst.addr = *daddr;
1389 rt->rt6i_dst.plen = 128;
1391 if (!rt6_is_gw_or_nonexthop(res)) {
1392 if (f6i->fib6_dst.plen != 128 &&
1393 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1394 rt->rt6i_flags |= RTF_ANYCAST;
1395 #ifdef CONFIG_IPV6_SUBTREES
1396 if (rt->rt6i_src.plen && saddr) {
1397 rt->rt6i_src.addr = *saddr;
1398 rt->rt6i_src.plen = 128;
1406 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1408 struct fib6_info *f6i = res->f6i;
1409 unsigned short flags = fib6_info_dst_flags(f6i);
1410 struct net_device *dev;
1411 struct rt6_info *pcpu_rt;
1413 if (!fib6_info_hold_safe(f6i))
1417 dev = ip6_rt_get_dev_rcu(res);
1418 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
1421 fib6_info_release(f6i);
1424 ip6_rt_copy_init(pcpu_rt, res);
1425 pcpu_rt->rt6i_flags |= RTF_PCPU;
1428 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1433 static bool rt6_is_valid(const struct rt6_info *rt6)
1435 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1438 /* It should be called with rcu_read_lock() acquired */
1439 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1441 struct rt6_info *pcpu_rt;
1443 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1445 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1446 struct rt6_info *prev, **p;
1448 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1449 /* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */
1450 prev = xchg(p, NULL);
1452 dst_dev_put(&prev->dst);
1453 dst_release(&prev->dst);
1462 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1463 const struct fib6_result *res)
1465 struct rt6_info *pcpu_rt, *prev, **p;
1467 pcpu_rt = ip6_rt_pcpu_alloc(res);
1471 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1472 prev = cmpxchg(p, NULL, pcpu_rt);
1475 if (res->f6i->fib6_destroying) {
1476 struct fib6_info *from;
1478 from = unrcu_pointer(xchg(&pcpu_rt->from, NULL));
1479 fib6_info_release(from);
1485 /* exception hash table implementation
1487 static DEFINE_SPINLOCK(rt6_exception_lock);
1489 /* Remove rt6_ex from hash table and free the memory
1490 * Caller must hold rt6_exception_lock
1492 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1493 struct rt6_exception *rt6_ex)
1497 if (!bucket || !rt6_ex)
1500 net = dev_net(rt6_ex->rt6i->dst.dev);
1501 net->ipv6.rt6_stats->fib_rt_cache--;
1503 /* purge completely the exception to allow releasing the held resources:
1504 * some [sk] cache may keep the dst around for unlimited time
1506 dst_dev_put(&rt6_ex->rt6i->dst);
1508 hlist_del_rcu(&rt6_ex->hlist);
1509 dst_release(&rt6_ex->rt6i->dst);
1510 kfree_rcu(rt6_ex, rcu);
1511 WARN_ON_ONCE(!bucket->depth);
1515 /* Remove oldest rt6_ex in bucket and free the memory
1516 * Caller must hold rt6_exception_lock
1518 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1520 struct rt6_exception *rt6_ex, *oldest = NULL;
1525 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1526 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1529 rt6_remove_exception(bucket, oldest);
1532 static u32 rt6_exception_hash(const struct in6_addr *dst,
1533 const struct in6_addr *src)
1535 static siphash_aligned_key_t rt6_exception_key;
1537 struct in6_addr dst;
1538 struct in6_addr src;
1539 } __aligned(SIPHASH_ALIGNMENT) combined = {
1544 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
1546 #ifdef CONFIG_IPV6_SUBTREES
1548 combined.src = *src;
1550 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1552 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1555 /* Helper function to find the cached rt in the hash table
1556 * and update bucket pointer to point to the bucket for this
1557 * (daddr, saddr) pair
1558 * Caller must hold rt6_exception_lock
1560 static struct rt6_exception *
1561 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1562 const struct in6_addr *daddr,
1563 const struct in6_addr *saddr)
1565 struct rt6_exception *rt6_ex;
1568 if (!(*bucket) || !daddr)
1571 hval = rt6_exception_hash(daddr, saddr);
1574 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1575 struct rt6_info *rt6 = rt6_ex->rt6i;
1576 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1578 #ifdef CONFIG_IPV6_SUBTREES
1579 if (matched && saddr)
1580 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1588 /* Helper function to find the cached rt in the hash table
1589 * and update bucket pointer to point to the bucket for this
1590 * (daddr, saddr) pair
1591 * Caller must hold rcu_read_lock()
1593 static struct rt6_exception *
1594 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1595 const struct in6_addr *daddr,
1596 const struct in6_addr *saddr)
1598 struct rt6_exception *rt6_ex;
1601 WARN_ON_ONCE(!rcu_read_lock_held());
1603 if (!(*bucket) || !daddr)
1606 hval = rt6_exception_hash(daddr, saddr);
1609 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1610 struct rt6_info *rt6 = rt6_ex->rt6i;
1611 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1613 #ifdef CONFIG_IPV6_SUBTREES
1614 if (matched && saddr)
1615 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1623 static unsigned int fib6_mtu(const struct fib6_result *res)
1625 const struct fib6_nh *nh = res->nh;
1628 if (res->f6i->fib6_pmtu) {
1629 mtu = res->f6i->fib6_pmtu;
1631 struct net_device *dev = nh->fib_nh_dev;
1632 struct inet6_dev *idev;
1635 idev = __in6_dev_get(dev);
1636 mtu = READ_ONCE(idev->cnf.mtu6);
1640 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1642 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1645 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1647 /* used when the flushed bit is not relevant, only access to the bucket
1648 * (ie., all bucket users except rt6_insert_exception);
1650 * called under rcu lock; sometimes called with rt6_exception_lock held
1653 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1656 struct rt6_exception_bucket *bucket;
1659 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1660 lockdep_is_held(lock));
1662 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1664 /* remove bucket flushed bit if set */
1666 unsigned long p = (unsigned long)bucket;
1668 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1669 bucket = (struct rt6_exception_bucket *)p;
1675 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1677 unsigned long p = (unsigned long)bucket;
1679 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1682 /* called with rt6_exception_lock held */
1683 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1686 struct rt6_exception_bucket *bucket;
1689 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1690 lockdep_is_held(lock));
1692 p = (unsigned long)bucket;
1693 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1694 bucket = (struct rt6_exception_bucket *)p;
1695 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1698 static int rt6_insert_exception(struct rt6_info *nrt,
1699 const struct fib6_result *res)
1701 struct net *net = dev_net(nrt->dst.dev);
1702 struct rt6_exception_bucket *bucket;
1703 struct fib6_info *f6i = res->f6i;
1704 struct in6_addr *src_key = NULL;
1705 struct rt6_exception *rt6_ex;
1706 struct fib6_nh *nh = res->nh;
1710 spin_lock_bh(&rt6_exception_lock);
1712 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1713 lockdep_is_held(&rt6_exception_lock));
1715 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1721 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1722 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1727 #ifdef CONFIG_IPV6_SUBTREES
1728 /* fib6_src.plen != 0 indicates f6i is in subtree
1729 * and exception table is indexed by a hash of
1730 * both fib6_dst and fib6_src.
1731 * Otherwise, the exception table is indexed by
1732 * a hash of only fib6_dst.
1734 if (f6i->fib6_src.plen)
1735 src_key = &nrt->rt6i_src.addr;
1737 /* rt6_mtu_change() might lower mtu on f6i.
1738 * Only insert this exception route if its mtu
1739 * is less than f6i's mtu value.
1741 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1746 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1749 rt6_remove_exception(bucket, rt6_ex);
1751 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1757 rt6_ex->stamp = jiffies;
1758 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1760 net->ipv6.rt6_stats->fib_rt_cache++;
1762 /* Randomize max depth to avoid some side channels attacks. */
1763 max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH);
1764 while (bucket->depth > max_depth)
1765 rt6_exception_remove_oldest(bucket);
1768 spin_unlock_bh(&rt6_exception_lock);
1770 /* Update fn->fn_sernum to invalidate all cached dst */
1772 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1773 fib6_update_sernum(net, f6i);
1774 fib6_add_gc_list(f6i);
1775 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1776 fib6_force_start_gc(net);
1782 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1784 struct rt6_exception_bucket *bucket;
1785 struct rt6_exception *rt6_ex;
1786 struct hlist_node *tmp;
1789 spin_lock_bh(&rt6_exception_lock);
1791 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1795 /* Prevent rt6_insert_exception() to recreate the bucket list */
1797 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1799 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1800 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1802 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1803 rt6_remove_exception(bucket, rt6_ex);
1805 WARN_ON_ONCE(!from && bucket->depth);
1809 spin_unlock_bh(&rt6_exception_lock);
1812 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1814 struct fib6_info *f6i = arg;
1816 fib6_nh_flush_exceptions(nh, f6i);
1821 void rt6_flush_exceptions(struct fib6_info *f6i)
1825 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, f6i);
1828 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1832 /* Find cached rt in the hash table inside passed in rt
1833 * Caller has to hold rcu_read_lock()
1835 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1836 const struct in6_addr *daddr,
1837 const struct in6_addr *saddr)
1839 const struct in6_addr *src_key = NULL;
1840 struct rt6_exception_bucket *bucket;
1841 struct rt6_exception *rt6_ex;
1842 struct rt6_info *ret = NULL;
1844 #ifdef CONFIG_IPV6_SUBTREES
1845 /* fib6i_src.plen != 0 indicates f6i is in subtree
1846 * and exception table is indexed by a hash of
1847 * both fib6_dst and fib6_src.
1848 * However, the src addr used to create the hash
1849 * might not be exactly the passed in saddr which
1850 * is a /128 addr from the flow.
1851 * So we need to use f6i->fib6_src to redo lookup
1852 * if the passed in saddr does not find anything.
1853 * (See the logic in ip6_rt_cache_alloc() on how
1854 * rt->rt6i_src is updated.)
1856 if (res->f6i->fib6_src.plen)
1860 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1861 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1863 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1866 #ifdef CONFIG_IPV6_SUBTREES
1867 /* Use fib6_src as src_key and redo lookup */
1868 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1869 src_key = &res->f6i->fib6_src.addr;
1877 /* Remove the passed in cached rt from the hash table that contains it */
1878 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1879 const struct rt6_info *rt)
1881 const struct in6_addr *src_key = NULL;
1882 struct rt6_exception_bucket *bucket;
1883 struct rt6_exception *rt6_ex;
1886 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1889 spin_lock_bh(&rt6_exception_lock);
1890 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1892 #ifdef CONFIG_IPV6_SUBTREES
1893 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1894 * and exception table is indexed by a hash of
1895 * both rt6i_dst and rt6i_src.
1896 * Otherwise, the exception table is indexed by
1897 * a hash of only rt6i_dst.
1900 src_key = &rt->rt6i_src.addr;
1902 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1906 rt6_remove_exception(bucket, rt6_ex);
1912 spin_unlock_bh(&rt6_exception_lock);
1916 struct fib6_nh_excptn_arg {
1917 struct rt6_info *rt;
1921 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1923 struct fib6_nh_excptn_arg *arg = _arg;
1926 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1933 static int rt6_remove_exception_rt(struct rt6_info *rt)
1935 struct fib6_info *from;
1937 from = rcu_dereference(rt->from);
1938 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1942 struct fib6_nh_excptn_arg arg = {
1944 .plen = from->fib6_src.plen
1948 /* rc = 1 means an entry was found */
1949 rc = nexthop_for_each_fib6_nh(from->nh,
1950 rt6_nh_remove_exception_rt,
1952 return rc ? 0 : -ENOENT;
1955 return fib6_nh_remove_exception(from->fib6_nh,
1956 from->fib6_src.plen, rt);
1959 /* Find rt6_ex which contains the passed in rt cache and
1962 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1963 const struct rt6_info *rt)
1965 const struct in6_addr *src_key = NULL;
1966 struct rt6_exception_bucket *bucket;
1967 struct rt6_exception *rt6_ex;
1969 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1970 #ifdef CONFIG_IPV6_SUBTREES
1971 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1972 * and exception table is indexed by a hash of
1973 * both rt6i_dst and rt6i_src.
1974 * Otherwise, the exception table is indexed by
1975 * a hash of only rt6i_dst.
1978 src_key = &rt->rt6i_src.addr;
1980 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1982 rt6_ex->stamp = jiffies;
1985 struct fib6_nh_match_arg {
1986 const struct net_device *dev;
1987 const struct in6_addr *gw;
1988 struct fib6_nh *match;
1991 /* determine if fib6_nh has given device and gateway */
1992 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1994 struct fib6_nh_match_arg *arg = _arg;
1996 if (arg->dev != nh->fib_nh_dev ||
1997 (arg->gw && !nh->fib_nh_gw_family) ||
1998 (!arg->gw && nh->fib_nh_gw_family) ||
1999 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
2004 /* found a match, break the loop */
2008 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
2010 struct fib6_info *from;
2011 struct fib6_nh *fib6_nh;
2015 from = rcu_dereference(rt->from);
2016 if (!from || !(rt->rt6i_flags & RTF_CACHE))
2020 struct fib6_nh_match_arg arg = {
2022 .gw = &rt->rt6i_gateway,
2025 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
2029 fib6_nh = arg.match;
2031 fib6_nh = from->fib6_nh;
2033 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
2038 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
2039 struct rt6_info *rt, int mtu)
2041 /* If the new MTU is lower than the route PMTU, this new MTU will be the
2042 * lowest MTU in the path: always allow updating the route PMTU to
2043 * reflect PMTU decreases.
2045 * If the new MTU is higher, and the route PMTU is equal to the local
2046 * MTU, this means the old MTU is the lowest in the path, so allow
2047 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2051 if (dst_mtu(&rt->dst) >= mtu)
2054 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2060 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2061 const struct fib6_nh *nh, int mtu)
2063 struct rt6_exception_bucket *bucket;
2064 struct rt6_exception *rt6_ex;
2067 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2071 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2072 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2073 struct rt6_info *entry = rt6_ex->rt6i;
2075 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2076 * route), the metrics of its rt->from have already
2079 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2080 rt6_mtu_change_route_allowed(idev, entry, mtu))
2081 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2087 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2089 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2090 const struct in6_addr *gateway)
2092 struct rt6_exception_bucket *bucket;
2093 struct rt6_exception *rt6_ex;
2094 struct hlist_node *tmp;
2097 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2100 spin_lock_bh(&rt6_exception_lock);
2101 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2103 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2104 hlist_for_each_entry_safe(rt6_ex, tmp,
2105 &bucket->chain, hlist) {
2106 struct rt6_info *entry = rt6_ex->rt6i;
2108 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2109 RTF_CACHE_GATEWAY &&
2110 ipv6_addr_equal(gateway,
2111 &entry->rt6i_gateway)) {
2112 rt6_remove_exception(bucket, rt6_ex);
2119 spin_unlock_bh(&rt6_exception_lock);
2122 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2123 struct rt6_exception *rt6_ex,
2124 struct fib6_gc_args *gc_args,
2127 struct rt6_info *rt = rt6_ex->rt6i;
2129 /* we are pruning and obsoleting aged-out and non gateway exceptions
2130 * even if others have still references to them, so that on next
2131 * dst_check() such references can be dropped.
2132 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2133 * expired, independently from their aging, as per RFC 8201 section 4
2135 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2136 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2137 pr_debug("aging clone %p\n", rt);
2138 rt6_remove_exception(bucket, rt6_ex);
2141 } else if (time_after(jiffies, rt->dst.expires)) {
2142 pr_debug("purging expired route %p\n", rt);
2143 rt6_remove_exception(bucket, rt6_ex);
2147 if (rt->rt6i_flags & RTF_GATEWAY) {
2148 struct neighbour *neigh;
2150 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2152 if (!(neigh && (neigh->flags & NTF_ROUTER))) {
2153 pr_debug("purging route %p via non-router but gateway\n",
2155 rt6_remove_exception(bucket, rt6_ex);
2163 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2164 struct fib6_gc_args *gc_args,
2167 struct rt6_exception_bucket *bucket;
2168 struct rt6_exception *rt6_ex;
2169 struct hlist_node *tmp;
2172 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2176 spin_lock(&rt6_exception_lock);
2177 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2179 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2180 hlist_for_each_entry_safe(rt6_ex, tmp,
2181 &bucket->chain, hlist) {
2182 rt6_age_examine_exception(bucket, rt6_ex,
2188 spin_unlock(&rt6_exception_lock);
2189 rcu_read_unlock_bh();
2192 struct fib6_nh_age_excptn_arg {
2193 struct fib6_gc_args *gc_args;
2197 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2199 struct fib6_nh_age_excptn_arg *arg = _arg;
2201 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2205 void rt6_age_exceptions(struct fib6_info *f6i,
2206 struct fib6_gc_args *gc_args,
2210 struct fib6_nh_age_excptn_arg arg = {
2215 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2218 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2222 /* must be called with rcu lock held */
2223 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2224 struct flowi6 *fl6, struct fib6_result *res, int strict)
2226 struct fib6_node *fn, *saved_fn;
2228 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2232 rt6_select(net, fn, oif, res, strict);
2233 if (res->f6i == net->ipv6.fib6_null_entry) {
2234 fn = fib6_backtrack(fn, &fl6->saddr);
2236 goto redo_rt6_select;
2237 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2238 /* also consider unreachable route */
2239 strict &= ~RT6_LOOKUP_F_REACHABLE;
2241 goto redo_rt6_select;
2245 trace_fib6_table_lookup(net, res, table, fl6);
2250 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2251 int oif, struct flowi6 *fl6,
2252 const struct sk_buff *skb, int flags)
2254 struct fib6_result res = {};
2255 struct rt6_info *rt = NULL;
2258 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2259 !rcu_read_lock_held());
2261 strict |= flags & RT6_LOOKUP_F_IFACE;
2262 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2263 if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
2264 strict |= RT6_LOOKUP_F_REACHABLE;
2268 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2269 if (res.f6i == net->ipv6.fib6_null_entry)
2272 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2274 /*Search through exception table */
2275 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2278 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2279 !res.nh->fib_nh_gw_family)) {
2280 /* Create a RTF_CACHE clone which will not be
2281 * owned by the fib6 tree. It is for the special case where
2282 * the daddr in the skb during the neighbor look-up is different
2283 * from the fl6->daddr used to look-up route here.
2285 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2288 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2289 * As rt6_uncached_list_add() does not consume refcnt,
2290 * this refcnt is always returned to the caller even
2291 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2293 rt6_uncached_list_add(rt);
2299 /* Get a percpu copy */
2301 rt = rt6_get_pcpu_route(&res);
2304 rt = rt6_make_pcpu_route(net, &res);
2310 rt = net->ipv6.ip6_null_entry;
2311 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2312 ip6_hold_safe(net, &rt);
2317 EXPORT_SYMBOL_GPL(ip6_pol_route);
2319 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
2320 struct fib6_table *table,
2322 const struct sk_buff *skb,
2325 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2328 struct dst_entry *ip6_route_input_lookup(struct net *net,
2329 struct net_device *dev,
2331 const struct sk_buff *skb,
2334 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2335 flags |= RT6_LOOKUP_F_IFACE;
2337 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2339 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2341 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2342 struct flow_keys *keys,
2343 struct flow_keys *flkeys)
2345 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2346 const struct ipv6hdr *key_iph = outer_iph;
2347 struct flow_keys *_flkeys = flkeys;
2348 const struct ipv6hdr *inner_iph;
2349 const struct icmp6hdr *icmph;
2350 struct ipv6hdr _inner_iph;
2351 struct icmp6hdr _icmph;
2353 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2356 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2357 sizeof(_icmph), &_icmph);
2361 if (!icmpv6_is_err(icmph->icmp6_type))
2364 inner_iph = skb_header_pointer(skb,
2365 skb_transport_offset(skb) + sizeof(*icmph),
2366 sizeof(_inner_iph), &_inner_iph);
2370 key_iph = inner_iph;
2374 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2375 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2376 keys->tags.flow_label = _flkeys->tags.flow_label;
2377 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2379 keys->addrs.v6addrs.src = key_iph->saddr;
2380 keys->addrs.v6addrs.dst = key_iph->daddr;
2381 keys->tags.flow_label = ip6_flowlabel(key_iph);
2382 keys->basic.ip_proto = key_iph->nexthdr;
2386 static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2387 const struct sk_buff *skb,
2390 u32 hash_fields = ip6_multipath_hash_fields(net);
2391 struct flow_keys keys, hash_keys;
2393 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2396 memset(&hash_keys, 0, sizeof(hash_keys));
2397 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2399 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2400 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2401 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2402 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2403 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2404 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2405 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2406 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2407 hash_keys.tags.flow_label = keys.tags.flow_label;
2408 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2409 hash_keys.ports.src = keys.ports.src;
2410 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2411 hash_keys.ports.dst = keys.ports.dst;
2413 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2414 return fib_multipath_hash_from_keys(net, &hash_keys);
2417 static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2418 const struct sk_buff *skb,
2421 u32 hash_fields = ip6_multipath_hash_fields(net);
2422 struct flow_keys keys, hash_keys;
2424 /* We assume the packet carries an encapsulation, but if none was
2425 * encountered during dissection of the outer flow, then there is no
2426 * point in calling the flow dissector again.
2431 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2434 memset(&hash_keys, 0, sizeof(hash_keys));
2435 skb_flow_dissect_flow_keys(skb, &keys, 0);
2437 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2440 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2441 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2442 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2443 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2444 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2445 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2446 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2447 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2448 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2449 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2450 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2451 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2452 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2453 hash_keys.tags.flow_label = keys.tags.flow_label;
2456 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2457 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2458 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2459 hash_keys.ports.src = keys.ports.src;
2460 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2461 hash_keys.ports.dst = keys.ports.dst;
2463 return fib_multipath_hash_from_keys(net, &hash_keys);
2466 static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2467 const struct sk_buff *skb)
2469 u32 mhash, mhash_inner;
2470 bool has_inner = true;
2472 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2473 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2475 return jhash_2words(mhash, mhash_inner, 0);
2478 static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2479 const struct flowi6 *fl6)
2481 u32 hash_fields = ip6_multipath_hash_fields(net);
2482 struct flow_keys hash_keys;
2484 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2487 memset(&hash_keys, 0, sizeof(hash_keys));
2488 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2489 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2490 hash_keys.addrs.v6addrs.src = fl6->saddr;
2491 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2492 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2493 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2494 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2495 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2496 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2497 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) {
2498 if (fl6->flowi6_flags & FLOWI_FLAG_ANY_SPORT)
2499 hash_keys.ports.src = (__force __be16)get_random_u16();
2501 hash_keys.ports.src = fl6->fl6_sport;
2503 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2504 hash_keys.ports.dst = fl6->fl6_dport;
2506 return fib_multipath_hash_from_keys(net, &hash_keys);
2509 /* if skb is set it will be used and fl6 can be NULL */
2510 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2511 const struct sk_buff *skb, struct flow_keys *flkeys)
2513 struct flow_keys hash_keys;
2516 switch (ip6_multipath_hash_policy(net)) {
2518 memset(&hash_keys, 0, sizeof(hash_keys));
2519 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2521 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2523 hash_keys.addrs.v6addrs.src = fl6->saddr;
2524 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2525 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2526 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2528 mhash = fib_multipath_hash_from_keys(net, &hash_keys);
2532 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2533 struct flow_keys keys;
2535 /* short-circuit if we already have L4 hash present */
2537 return skb_get_hash_raw(skb) >> 1;
2539 memset(&hash_keys, 0, sizeof(hash_keys));
2542 skb_flow_dissect_flow_keys(skb, &keys, flag);
2545 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2546 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2547 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2548 hash_keys.ports.src = flkeys->ports.src;
2549 hash_keys.ports.dst = flkeys->ports.dst;
2550 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2552 memset(&hash_keys, 0, sizeof(hash_keys));
2553 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2554 hash_keys.addrs.v6addrs.src = fl6->saddr;
2555 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2556 if (fl6->flowi6_flags & FLOWI_FLAG_ANY_SPORT)
2557 hash_keys.ports.src = (__force __be16)get_random_u16();
2559 hash_keys.ports.src = fl6->fl6_sport;
2560 hash_keys.ports.dst = fl6->fl6_dport;
2561 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2563 mhash = fib_multipath_hash_from_keys(net, &hash_keys);
2566 memset(&hash_keys, 0, sizeof(hash_keys));
2567 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2569 struct flow_keys keys;
2572 skb_flow_dissect_flow_keys(skb, &keys, 0);
2576 /* Inner can be v4 or v6 */
2577 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2578 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2579 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2580 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2581 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2582 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2583 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2584 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2585 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2586 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2588 /* Same as case 0 */
2589 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2590 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2593 /* Same as case 0 */
2594 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2595 hash_keys.addrs.v6addrs.src = fl6->saddr;
2596 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2597 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2598 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2600 mhash = fib_multipath_hash_from_keys(net, &hash_keys);
2604 mhash = rt6_multipath_custom_hash_skb(net, skb);
2606 mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2613 /* Called with rcu held */
2614 void ip6_route_input(struct sk_buff *skb)
2616 const struct ipv6hdr *iph = ipv6_hdr(skb);
2617 struct net *net = dev_net(skb->dev);
2618 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2619 struct ip_tunnel_info *tun_info;
2620 struct flowi6 fl6 = {
2621 .flowi6_iif = skb->dev->ifindex,
2622 .daddr = iph->daddr,
2623 .saddr = iph->saddr,
2624 .flowlabel = ip6_flowinfo(iph),
2625 .flowi6_mark = skb->mark,
2626 .flowi6_proto = iph->nexthdr,
2628 struct flow_keys *flkeys = NULL, _flkeys;
2630 tun_info = skb_tunnel_info(skb);
2631 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2632 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2634 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2637 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2638 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2640 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2644 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
2645 struct fib6_table *table,
2647 const struct sk_buff *skb,
2650 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2653 static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2654 const struct sock *sk,
2660 if (ipv6_addr_type(&fl6->daddr) &
2661 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2662 struct dst_entry *dst;
2664 /* This function does not take refcnt on the dst */
2665 dst = l3mdev_link_scope_lookup(net, fl6);
2670 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2672 flags |= RT6_LOOKUP_F_DST_NOREF;
2673 any_src = ipv6_addr_any(&fl6->saddr);
2674 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2675 (fl6->flowi6_oif && any_src))
2676 flags |= RT6_LOOKUP_F_IFACE;
2679 flags |= RT6_LOOKUP_F_HAS_SADDR;
2681 flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs));
2683 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2686 struct dst_entry *ip6_route_output_flags(struct net *net,
2687 const struct sock *sk,
2691 struct dst_entry *dst;
2692 struct rt6_info *rt6;
2695 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2696 rt6 = dst_rt6_info(dst);
2697 /* For dst cached in uncached_list, refcnt is already taken. */
2698 if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) {
2699 dst = &net->ipv6.ip6_null_entry->dst;
2706 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2708 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2710 struct rt6_info *rt, *ort = dst_rt6_info(dst_orig);
2711 struct net_device *loopback_dev = net->loopback_dev;
2712 struct dst_entry *new = NULL;
2714 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev,
2715 DST_OBSOLETE_DEAD, 0);
2718 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2722 new->input = dst_discard;
2723 new->output = dst_discard_out;
2725 dst_copy_metrics(new, &ort->dst);
2727 rt->rt6i_idev = in6_dev_get(loopback_dev);
2728 rt->rt6i_gateway = ort->rt6i_gateway;
2729 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2731 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2732 #ifdef CONFIG_IPV6_SUBTREES
2733 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2737 dst_release(dst_orig);
2738 return new ? new : ERR_PTR(-ENOMEM);
2742 * Destination cache support functions
2745 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2749 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2752 if (fib6_check_expired(f6i))
2758 static struct dst_entry *rt6_check(struct rt6_info *rt,
2759 struct fib6_info *from,
2764 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2765 rt_cookie != cookie)
2768 if (rt6_check_expired(rt))
2774 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2775 struct fib6_info *from,
2778 if (!__rt6_check_expired(rt) &&
2779 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2780 fib6_check(from, cookie))
2786 INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2789 struct dst_entry *dst_ret;
2790 struct fib6_info *from;
2791 struct rt6_info *rt;
2793 rt = dst_rt6_info(dst);
2796 return rt6_is_valid(rt) ? dst : NULL;
2800 /* All IPV6 dsts are created with ->obsolete set to the value
2801 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2802 * into this function always.
2805 from = rcu_dereference(rt->from);
2807 if (from && (rt->rt6i_flags & RTF_PCPU ||
2808 unlikely(!list_empty(&rt->dst.rt_uncached))))
2809 dst_ret = rt6_dst_from_check(rt, from, cookie);
2811 dst_ret = rt6_check(rt, from, cookie);
2817 EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
2819 static void ip6_negative_advice(struct sock *sk,
2820 struct dst_entry *dst)
2822 struct rt6_info *rt = dst_rt6_info(dst);
2824 if (rt->rt6i_flags & RTF_CACHE) {
2826 if (rt6_check_expired(rt)) {
2827 /* rt/dst can not be destroyed yet,
2828 * because of rcu_read_lock()
2831 rt6_remove_exception_rt(rt);
2839 static void ip6_link_failure(struct sk_buff *skb)
2841 struct rt6_info *rt;
2843 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2845 rt = dst_rt6_info(skb_dst(skb));
2848 if (rt->rt6i_flags & RTF_CACHE) {
2849 rt6_remove_exception_rt(rt);
2851 struct fib6_info *from;
2852 struct fib6_node *fn;
2854 from = rcu_dereference(rt->from);
2856 fn = rcu_dereference(from->fib6_node);
2857 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2858 WRITE_ONCE(fn->fn_sernum, -1);
2865 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2867 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2868 struct fib6_info *from;
2871 from = rcu_dereference(rt0->from);
2873 rt0->dst.expires = from->expires;
2877 dst_set_expires(&rt0->dst, timeout);
2878 rt0->rt6i_flags |= RTF_EXPIRES;
2881 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2883 struct net *net = dev_net(rt->dst.dev);
2885 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2886 rt->rt6i_flags |= RTF_MODIFIED;
2887 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2890 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2892 return !(rt->rt6i_flags & RTF_CACHE) &&
2893 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2896 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2897 const struct ipv6hdr *iph, u32 mtu,
2900 const struct in6_addr *daddr, *saddr;
2901 struct rt6_info *rt6 = dst_rt6_info(dst);
2903 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2904 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2905 * [see also comment in rt6_mtu_change_route()]
2909 daddr = &iph->daddr;
2910 saddr = &iph->saddr;
2912 daddr = &sk->sk_v6_daddr;
2913 saddr = &inet6_sk(sk)->saddr;
2920 dst_confirm_neigh(dst, daddr);
2922 if (mtu < IPV6_MIN_MTU)
2924 if (mtu >= dst_mtu(dst))
2927 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2928 rt6_do_update_pmtu(rt6, mtu);
2929 /* update rt6_ex->stamp for cache */
2930 if (rt6->rt6i_flags & RTF_CACHE)
2931 rt6_update_exception_stamp_rt(rt6);
2933 struct fib6_result res = {};
2934 struct rt6_info *nrt6;
2937 res.f6i = rcu_dereference(rt6->from);
2941 res.fib6_flags = res.f6i->fib6_flags;
2942 res.fib6_type = res.f6i->fib6_type;
2945 struct fib6_nh_match_arg arg = {
2947 .gw = &rt6->rt6i_gateway,
2950 nexthop_for_each_fib6_nh(res.f6i->nh,
2951 fib6_nh_find_match, &arg);
2953 /* fib6_info uses a nexthop that does not have fib6_nh
2954 * using the dst->dev + gw. Should be impossible.
2961 res.nh = res.f6i->fib6_nh;
2964 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2966 rt6_do_update_pmtu(nrt6, mtu);
2967 if (rt6_insert_exception(nrt6, &res))
2968 dst_release_immediate(&nrt6->dst);
2975 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2976 struct sk_buff *skb, u32 mtu,
2979 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2983 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2984 int oif, u32 mark, kuid_t uid)
2986 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2987 struct dst_entry *dst;
2988 struct flowi6 fl6 = {
2990 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2991 .daddr = iph->daddr,
2992 .saddr = iph->saddr,
2993 .flowlabel = ip6_flowinfo(iph),
2997 dst = ip6_route_output(net, NULL, &fl6);
2999 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
3002 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
3004 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
3006 int oif = sk->sk_bound_dev_if;
3007 struct dst_entry *dst;
3009 if (!oif && skb->dev)
3010 oif = l3mdev_master_ifindex(skb->dev);
3012 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark),
3015 dst = __sk_dst_get(sk);
3016 if (!dst || !dst->obsolete ||
3017 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
3021 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
3022 ip6_datagram_dst_update(sk, false);
3025 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
3027 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
3028 const struct flowi6 *fl6)
3030 #ifdef CONFIG_IPV6_SUBTREES
3031 struct ipv6_pinfo *np = inet6_sk(sk);
3034 ip6_dst_store(sk, dst,
3035 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
3036 &sk->sk_v6_daddr : NULL,
3037 #ifdef CONFIG_IPV6_SUBTREES
3038 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
3044 static bool ip6_redirect_nh_match(const struct fib6_result *res,
3046 const struct in6_addr *gw,
3047 struct rt6_info **ret)
3049 const struct fib6_nh *nh = res->nh;
3051 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
3052 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
3055 /* rt_cache's gateway might be different from its 'parent'
3056 * in the case of an ip redirect.
3057 * So we keep searching in the exception table if the gateway
3060 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
3061 struct rt6_info *rt_cache;
3063 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
3065 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
3074 struct fib6_nh_rd_arg {
3075 struct fib6_result *res;
3077 const struct in6_addr *gw;
3078 struct rt6_info **ret;
3081 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3083 struct fib6_nh_rd_arg *arg = _arg;
3086 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3089 /* Handle redirects */
3090 struct ip6rd_flowi {
3092 struct in6_addr gateway;
3095 INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
3096 struct fib6_table *table,
3098 const struct sk_buff *skb,
3101 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
3102 struct rt6_info *ret = NULL;
3103 struct fib6_result res = {};
3104 struct fib6_nh_rd_arg arg = {
3107 .gw = &rdfl->gateway,
3110 struct fib6_info *rt;
3111 struct fib6_node *fn;
3113 /* Get the "current" route for this destination and
3114 * check if the redirect has come from appropriate router.
3116 * RFC 4861 specifies that redirects should only be
3117 * accepted if they come from the nexthop to the target.
3118 * Due to the way the routes are chosen, this notion
3119 * is a bit fuzzy and one might need to check all possible
3124 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
3126 for_each_fib6_node_rt_rcu(fn) {
3128 if (fib6_check_expired(rt))
3130 if (rt->fib6_flags & RTF_REJECT)
3132 if (unlikely(rt->nh)) {
3133 if (nexthop_is_blackhole(rt->nh))
3135 /* on match, res->nh is filled in and potentially ret */
3136 if (nexthop_for_each_fib6_nh(rt->nh,
3137 fib6_nh_redirect_match,
3141 res.nh = rt->fib6_nh;
3142 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3149 rt = net->ipv6.fib6_null_entry;
3150 else if (rt->fib6_flags & RTF_REJECT) {
3151 ret = net->ipv6.ip6_null_entry;
3155 if (rt == net->ipv6.fib6_null_entry) {
3156 fn = fib6_backtrack(fn, &fl6->saddr);
3162 res.nh = rt->fib6_nh;
3165 ip6_hold_safe(net, &ret);
3167 res.fib6_flags = res.f6i->fib6_flags;
3168 res.fib6_type = res.f6i->fib6_type;
3169 ret = ip6_create_rt_rcu(&res);
3174 trace_fib6_table_lookup(net, &res, table, fl6);
3178 static struct dst_entry *ip6_route_redirect(struct net *net,
3179 const struct flowi6 *fl6,
3180 const struct sk_buff *skb,
3181 const struct in6_addr *gateway)
3183 int flags = RT6_LOOKUP_F_HAS_SADDR;
3184 struct ip6rd_flowi rdfl;
3187 rdfl.gateway = *gateway;
3189 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3190 flags, __ip6_route_redirect);
3193 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3196 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3197 struct dst_entry *dst;
3198 struct flowi6 fl6 = {
3199 .flowi6_iif = LOOPBACK_IFINDEX,
3201 .flowi6_mark = mark,
3202 .daddr = iph->daddr,
3203 .saddr = iph->saddr,
3204 .flowlabel = ip6_flowinfo(iph),
3208 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3209 rt6_do_redirect(dst, NULL, skb);
3212 EXPORT_SYMBOL_GPL(ip6_redirect);
3214 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3216 const struct ipv6hdr *iph = ipv6_hdr(skb);
3217 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3218 struct dst_entry *dst;
3219 struct flowi6 fl6 = {
3220 .flowi6_iif = LOOPBACK_IFINDEX,
3223 .saddr = iph->daddr,
3224 .flowi6_uid = sock_net_uid(net, NULL),
3227 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3228 rt6_do_redirect(dst, NULL, skb);
3232 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3234 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
3235 READ_ONCE(sk->sk_mark), sk->sk_uid);
3237 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3239 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3241 struct net_device *dev = dst->dev;
3242 unsigned int mtu = dst_mtu(dst);
3245 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3249 net = dev_net_rcu(dev);
3250 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3251 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3256 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3257 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3258 * IPV6_MAXPLEN is also valid and means: "any MSS,
3259 * rely only on pmtu discovery"
3261 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3266 INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
3268 return ip6_dst_mtu_maybe_forward(dst, false);
3270 EXPORT_INDIRECT_CALLABLE(ip6_mtu);
3273 * 1. mtu on route is locked - use it
3274 * 2. mtu from nexthop exception
3275 * 3. mtu from egress device
3277 * based on ip6_dst_mtu_forward and exception logic of
3278 * rt6_find_cached_rt; called with rcu_read_lock
3280 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3281 const struct in6_addr *daddr,
3282 const struct in6_addr *saddr)
3284 const struct fib6_nh *nh = res->nh;
3285 struct fib6_info *f6i = res->f6i;
3286 struct inet6_dev *idev;
3287 struct rt6_info *rt;
3290 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3291 mtu = f6i->fib6_pmtu;
3296 rt = rt6_find_cached_rt(res, daddr, saddr);
3298 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3300 struct net_device *dev = nh->fib_nh_dev;
3303 idev = __in6_dev_get(dev);
3305 mtu = max_t(u32, mtu, READ_ONCE(idev->cnf.mtu6));
3308 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3310 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3313 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3316 struct dst_entry *dst;
3317 struct rt6_info *rt;
3318 struct inet6_dev *idev = in6_dev_get(dev);
3319 struct net *net = dev_net(dev);
3321 if (unlikely(!idev))
3322 return ERR_PTR(-ENODEV);
3324 rt = ip6_dst_alloc(net, dev, 0);
3325 if (unlikely(!rt)) {
3327 dst = ERR_PTR(-ENOMEM);
3331 rt->dst.input = ip6_input;
3332 rt->dst.output = ip6_output;
3333 rt->rt6i_gateway = fl6->daddr;
3334 rt->rt6i_dst.addr = fl6->daddr;
3335 rt->rt6i_dst.plen = 128;
3336 rt->rt6i_idev = idev;
3337 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3339 /* Add this dst into uncached_list so that rt6_disable_ip() can
3340 * do proper release of the net_device
3342 rt6_uncached_list_add(rt);
3344 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3350 static void ip6_dst_gc(struct dst_ops *ops)
3352 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3353 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3354 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3355 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3356 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3360 if (time_after(rt_last_gc + rt_min_interval, jiffies))
3363 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
3364 entries = dst_entries_get_slow(ops);
3365 if (entries < ops->gc_thresh)
3366 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
3368 val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
3369 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
3372 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3373 const struct in6_addr *gw_addr, u32 tbid,
3374 int flags, struct fib6_result *res)
3376 struct flowi6 fl6 = {
3377 .flowi6_oif = cfg->fc_ifindex,
3379 .saddr = cfg->fc_prefsrc,
3381 struct fib6_table *table;
3384 table = fib6_get_table(net, tbid);
3388 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3389 flags |= RT6_LOOKUP_F_HAS_SADDR;
3391 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3393 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3394 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3395 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3396 cfg->fc_ifindex != 0, NULL, flags);
3401 static int ip6_route_check_nh_onlink(struct net *net,
3402 struct fib6_config *cfg,
3403 const struct net_device *dev,
3404 struct netlink_ext_ack *extack)
3406 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3407 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3408 struct fib6_result res = {};
3411 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3412 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3413 /* ignore match if it is the default route */
3414 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3415 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3416 NL_SET_ERR_MSG(extack,
3417 "Nexthop has invalid gateway or device mismatch");
3424 static int ip6_route_check_nh(struct net *net,
3425 struct fib6_config *cfg,
3426 struct net_device **_dev,
3427 netdevice_tracker *dev_tracker,
3428 struct inet6_dev **idev)
3430 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3431 struct net_device *dev = _dev ? *_dev : NULL;
3432 int flags = RT6_LOOKUP_F_IFACE;
3433 struct fib6_result res = {};
3434 int err = -EHOSTUNREACH;
3436 if (cfg->fc_table) {
3437 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3438 cfg->fc_table, flags, &res);
3439 /* gw_addr can not require a gateway or resolve to a reject
3440 * route. If a device is given, it must match the result.
3442 if (err || res.fib6_flags & RTF_REJECT ||
3443 res.nh->fib_nh_gw_family ||
3444 (dev && dev != res.nh->fib_nh_dev))
3445 err = -EHOSTUNREACH;
3449 struct flowi6 fl6 = {
3450 .flowi6_oif = cfg->fc_ifindex,
3454 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3455 if (err || res.fib6_flags & RTF_REJECT ||
3456 res.nh->fib_nh_gw_family)
3457 err = -EHOSTUNREACH;
3462 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3463 cfg->fc_ifindex != 0, NULL, flags);
3468 if (dev != res.nh->fib_nh_dev)
3469 err = -EHOSTUNREACH;
3471 *_dev = dev = res.nh->fib_nh_dev;
3472 netdev_hold(dev, dev_tracker, GFP_ATOMIC);
3473 *idev = in6_dev_get(dev);
3479 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3480 struct net_device **_dev,
3481 netdevice_tracker *dev_tracker,
3482 struct inet6_dev **idev,
3483 struct netlink_ext_ack *extack)
3485 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3486 int gwa_type = ipv6_addr_type(gw_addr);
3487 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3488 const struct net_device *dev = *_dev;
3489 bool need_addr_check = !dev;
3492 /* if gw_addr is local we will fail to detect this in case
3493 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3494 * will return already-added prefix route via interface that
3495 * prefix route was assigned to, which might be non-loopback.
3498 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3499 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3503 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3504 /* IPv6 strictly inhibits using not link-local
3505 * addresses as nexthop address.
3506 * Otherwise, router will not able to send redirects.
3507 * It is very good, but in some (rare!) circumstances
3508 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3509 * some exceptions. --ANK
3510 * We allow IPv4-mapped nexthops to support RFC4798-type
3513 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3514 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3520 if (cfg->fc_flags & RTNH_F_ONLINK)
3521 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3523 err = ip6_route_check_nh(net, cfg, _dev, dev_tracker,
3532 /* reload in case device was changed */
3537 NL_SET_ERR_MSG(extack, "Egress device not specified");
3539 } else if (dev->flags & IFF_LOOPBACK) {
3540 NL_SET_ERR_MSG(extack,
3541 "Egress device can not be loopback device for this route");
3545 /* if we did not check gw_addr above, do so now that the
3546 * egress device has been resolved.
3548 if (need_addr_check &&
3549 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3550 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3559 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3561 if ((flags & RTF_REJECT) ||
3562 (dev && (dev->flags & IFF_LOOPBACK) &&
3563 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3564 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
3570 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3571 struct fib6_config *cfg, gfp_t gfp_flags,
3572 struct netlink_ext_ack *extack)
3574 netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker;
3575 struct net_device *dev = NULL;
3576 struct inet6_dev *idev = NULL;
3580 fib6_nh->fib_nh_family = AF_INET6;
3581 #ifdef CONFIG_IPV6_ROUTER_PREF
3582 fib6_nh->last_probe = jiffies;
3584 if (cfg->fc_is_fdb) {
3585 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3586 fib6_nh->fib_nh_gw_family = AF_INET6;
3591 if (cfg->fc_ifindex) {
3592 dev = netdev_get_by_index(net, cfg->fc_ifindex,
3593 dev_tracker, gfp_flags);
3596 idev = in6_dev_get(dev);
3601 if (cfg->fc_flags & RTNH_F_ONLINK) {
3603 NL_SET_ERR_MSG(extack,
3604 "Nexthop device required for onlink");
3608 if (!(dev->flags & IFF_UP)) {
3609 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3614 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3617 fib6_nh->fib_nh_weight = 1;
3619 /* We cannot add true routes via loopback here,
3620 * they would result in kernel looping; promote them to reject routes
3622 addr_type = ipv6_addr_type(&cfg->fc_dst);
3623 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3624 /* hold loopback dev/idev if we haven't done so. */
3625 if (dev != net->loopback_dev) {
3627 netdev_put(dev, dev_tracker);
3630 dev = net->loopback_dev;
3631 netdev_hold(dev, dev_tracker, gfp_flags);
3632 idev = in6_dev_get(dev);
3641 if (cfg->fc_flags & RTF_GATEWAY) {
3642 err = ip6_validate_gw(net, cfg, &dev, dev_tracker,
3647 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3648 fib6_nh->fib_nh_gw_family = AF_INET6;
3655 if (!idev || idev->cnf.disable_ipv6) {
3656 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3661 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3662 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3667 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3668 !netif_carrier_ok(dev))
3669 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3671 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
3672 cfg->fc_encap_type, cfg, gfp_flags, extack);
3677 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3678 if (!fib6_nh->rt6i_pcpu) {
3683 fib6_nh->fib_nh_dev = dev;
3684 fib6_nh->fib_nh_oif = dev->ifindex;
3691 fib_nh_common_release(&fib6_nh->nh_common);
3692 fib6_nh->nh_common.nhc_pcpu_rth_output = NULL;
3693 fib6_nh->fib_nh_lws = NULL;
3694 netdev_put(dev, dev_tracker);
3700 void fib6_nh_release(struct fib6_nh *fib6_nh)
3702 struct rt6_exception_bucket *bucket;
3706 fib6_nh_flush_exceptions(fib6_nh, NULL);
3707 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3709 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3715 fib6_nh_release_dsts(fib6_nh);
3716 free_percpu(fib6_nh->rt6i_pcpu);
3718 fib_nh_common_release(&fib6_nh->nh_common);
3721 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3725 if (!fib6_nh->rt6i_pcpu)
3728 for_each_possible_cpu(cpu) {
3729 struct rt6_info *pcpu_rt, **ppcpu_rt;
3731 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3732 pcpu_rt = xchg(ppcpu_rt, NULL);
3734 dst_dev_put(&pcpu_rt->dst);
3735 dst_release(&pcpu_rt->dst);
3740 static int fib6_config_validate(struct fib6_config *cfg,
3741 struct netlink_ext_ack *extack)
3743 /* RTF_PCPU is an internal flag; can not be set by userspace */
3744 if (cfg->fc_flags & RTF_PCPU) {
3745 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3749 /* RTF_CACHE is an internal flag; can not be set by userspace */
3750 if (cfg->fc_flags & RTF_CACHE) {
3751 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3755 if (cfg->fc_type > RTN_MAX) {
3756 NL_SET_ERR_MSG(extack, "Invalid route type");
3760 if (cfg->fc_dst_len > 128) {
3761 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3765 #ifdef CONFIG_IPV6_SUBTREES
3766 if (cfg->fc_src_len > 128) {
3767 NL_SET_ERR_MSG(extack, "Invalid source address length");
3771 if (cfg->fc_nh_id && cfg->fc_src_len) {
3772 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3776 if (cfg->fc_src_len) {
3777 NL_SET_ERR_MSG(extack,
3778 "Specifying source address requires IPV6_SUBTREES to be enabled");
3787 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3789 struct netlink_ext_ack *extack)
3791 struct net *net = cfg->fc_nlinfo.nl_net;
3792 struct fib6_table *table;
3793 struct fib6_info *rt;
3796 if (cfg->fc_nlinfo.nlh &&
3797 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3798 table = fib6_get_table(net, cfg->fc_table);
3800 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3801 table = fib6_new_table(net, cfg->fc_table);
3804 table = fib6_new_table(net, cfg->fc_table);
3811 rt = fib6_info_alloc(gfp_flags, !cfg->fc_nh_id);
3817 rt->fib6_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len,
3819 if (IS_ERR(rt->fib6_metrics)) {
3820 err = PTR_ERR(rt->fib6_metrics);
3824 if (cfg->fc_flags & RTF_ADDRCONF)
3825 rt->dst_nocount = true;
3827 if (cfg->fc_flags & RTF_EXPIRES)
3828 fib6_set_expires(rt, jiffies +
3829 clock_t_to_jiffies(cfg->fc_expires));
3831 if (cfg->fc_protocol == RTPROT_UNSPEC)
3832 cfg->fc_protocol = RTPROT_BOOT;
3834 rt->fib6_protocol = cfg->fc_protocol;
3835 rt->fib6_table = table;
3836 rt->fib6_metric = cfg->fc_metric;
3837 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3838 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3840 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3841 rt->fib6_dst.plen = cfg->fc_dst_len;
3843 #ifdef CONFIG_IPV6_SUBTREES
3844 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3845 rt->fib6_src.plen = cfg->fc_src_len;
3851 return ERR_PTR(err);
3854 static int ip6_route_info_create_nh(struct fib6_info *rt,
3855 struct fib6_config *cfg,
3857 struct netlink_ext_ack *extack)
3859 struct net *net = cfg->fc_nlinfo.nl_net;
3860 struct fib6_nh *fib6_nh;
3863 if (cfg->fc_nh_id) {
3868 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3871 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3875 err = fib6_check_nexthop(nh, cfg, extack);
3879 if (!nexthop_get(nh)) {
3880 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3886 fib6_nh = nexthop_fib6_nh(rt->nh);
3892 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3896 fib6_nh = rt->fib6_nh;
3898 /* We cannot add true routes via loopback here, they would
3899 * result in kernel looping; promote them to reject routes
3901 addr_type = ipv6_addr_type(&cfg->fc_dst);
3902 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3904 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3907 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3908 struct net_device *dev = fib6_nh->fib_nh_dev;
3910 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3911 NL_SET_ERR_MSG(extack, "Invalid source address");
3915 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3916 rt->fib6_prefsrc.plen = 128;
3921 fib6_info_release(rt);
3925 ip_fib_metrics_put(rt->fib6_metrics);
3930 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3931 struct netlink_ext_ack *extack)
3933 struct fib6_info *rt;
3936 err = fib6_config_validate(cfg, extack);
3940 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3944 err = ip6_route_info_create_nh(rt, cfg, gfp_flags, extack);
3948 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3949 fib6_info_release(rt);
3954 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3956 struct net *net = info->nl_net;
3957 struct fib6_table *table;
3960 if (rt == net->ipv6.fib6_null_entry) {
3965 table = rt->fib6_table;
3966 spin_lock_bh(&table->tb6_lock);
3967 err = fib6_del(rt, info);
3968 spin_unlock_bh(&table->tb6_lock);
3971 fib6_info_release(rt);
3975 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3977 struct nl_info info = {
3979 .skip_notify = skip_notify
3982 return __ip6_del_rt(rt, &info);
3985 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3987 struct nl_info *info = &cfg->fc_nlinfo;
3988 struct net *net = info->nl_net;
3989 struct sk_buff *skb = NULL;
3990 struct fib6_table *table;
3993 if (rt == net->ipv6.fib6_null_entry)
3995 table = rt->fib6_table;
3996 spin_lock_bh(&table->tb6_lock);
3998 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3999 struct fib6_info *sibling, *next_sibling;
4000 struct fib6_node *fn;
4002 /* prefer to send a single notification with all hops */
4003 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
4005 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
4007 if (rt6_fill_node(net, skb, rt, NULL,
4008 NULL, NULL, 0, RTM_DELROUTE,
4009 info->portid, seq, 0) < 0) {
4013 info->skip_notify = 1;
4016 /* 'rt' points to the first sibling route. If it is not the
4017 * leaf, then we do not need to send a notification. Otherwise,
4018 * we need to check if the last sibling has a next route or not
4019 * and emit a replace or delete notification, respectively.
4021 info->skip_notify_kernel = 1;
4022 fn = rcu_dereference_protected(rt->fib6_node,
4023 lockdep_is_held(&table->tb6_lock));
4024 if (rcu_access_pointer(fn->leaf) == rt) {
4025 struct fib6_info *last_sibling, *replace_rt;
4027 last_sibling = list_last_entry(&rt->fib6_siblings,
4030 replace_rt = rcu_dereference_protected(
4031 last_sibling->fib6_next,
4032 lockdep_is_held(&table->tb6_lock));
4034 call_fib6_entry_notifiers_replace(net,
4037 call_fib6_multipath_entry_notifiers(net,
4038 FIB_EVENT_ENTRY_DEL,
4039 rt, rt->fib6_nsiblings,
4042 list_for_each_entry_safe(sibling, next_sibling,
4045 err = fib6_del(sibling, info);
4051 err = fib6_del(rt, info);
4053 spin_unlock_bh(&table->tb6_lock);
4055 fib6_info_release(rt);
4058 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
4059 info->nlh, gfp_any());
4064 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
4068 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
4071 if (cfg->fc_flags & RTF_GATEWAY &&
4072 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
4075 rc = rt6_remove_exception_rt(rt);
4080 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
4083 struct fib6_result res = {
4087 struct rt6_info *rt_cache;
4089 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
4091 return __ip6_del_cached_rt(rt_cache, cfg);
4096 struct fib6_nh_del_cached_rt_arg {
4097 struct fib6_config *cfg;
4098 struct fib6_info *f6i;
4101 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
4103 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4106 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4107 return rc != -ESRCH ? rc : 0;
4110 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4112 struct fib6_nh_del_cached_rt_arg arg = {
4117 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4120 static int ip6_route_del(struct fib6_config *cfg,
4121 struct netlink_ext_ack *extack)
4123 struct fib6_table *table;
4124 struct fib6_info *rt;
4125 struct fib6_node *fn;
4128 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
4130 NL_SET_ERR_MSG(extack, "FIB table does not exist");
4136 fn = fib6_locate(&table->tb6_root,
4137 &cfg->fc_dst, cfg->fc_dst_len,
4138 &cfg->fc_src, cfg->fc_src_len,
4139 !(cfg->fc_flags & RTF_CACHE));
4142 for_each_fib6_node_rt_rcu(fn) {
4145 if (rt->nh && cfg->fc_nh_id &&
4146 rt->nh->id != cfg->fc_nh_id)
4149 if (cfg->fc_flags & RTF_CACHE) {
4153 rc = ip6_del_cached_rt_nh(cfg, rt);
4154 } else if (cfg->fc_nh_id) {
4158 rc = ip6_del_cached_rt(cfg, rt, nh);
4167 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4169 if (cfg->fc_protocol &&
4170 cfg->fc_protocol != rt->fib6_protocol)
4174 if (!fib6_info_hold_safe(rt))
4177 err = __ip6_del_rt(rt, &cfg->fc_nlinfo);
4184 if (cfg->fc_ifindex &&
4186 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
4188 if (cfg->fc_flags & RTF_GATEWAY &&
4189 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
4191 if (!fib6_info_hold_safe(rt))
4194 /* if gateway was specified only delete the one hop */
4195 if (cfg->fc_flags & RTF_GATEWAY)
4196 err = __ip6_del_rt(rt, &cfg->fc_nlinfo);
4198 err = __ip6_del_rt_siblings(rt, cfg);
4207 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4209 struct netevent_redirect netevent;
4210 struct rt6_info *rt, *nrt = NULL;
4211 struct fib6_result res = {};
4212 struct ndisc_options ndopts;
4213 struct inet6_dev *in6_dev;
4214 struct neighbour *neigh;
4216 int optlen, on_link;
4219 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4220 optlen -= sizeof(*msg);
4223 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4227 msg = (struct rd_msg *)icmp6_hdr(skb);
4229 if (ipv6_addr_is_multicast(&msg->dest)) {
4230 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4235 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4237 } else if (ipv6_addr_type(&msg->target) !=
4238 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4239 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4243 in6_dev = __in6_dev_get(skb->dev);
4246 if (READ_ONCE(in6_dev->cnf.forwarding) ||
4247 !READ_ONCE(in6_dev->cnf.accept_redirects))
4251 * The IP source address of the Redirect MUST be the same as the current
4252 * first-hop router for the specified ICMP Destination Address.
4255 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4256 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4261 if (ndopts.nd_opts_tgt_lladdr) {
4262 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4265 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4270 rt = dst_rt6_info(dst);
4271 if (rt->rt6i_flags & RTF_REJECT) {
4272 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4276 /* Redirect received -> path was valid.
4277 * Look, redirects are sent only in response to data packets,
4278 * so that this nexthop apparently is reachable. --ANK
4280 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4282 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4287 * We have finally decided to accept it.
4290 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4291 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4292 NEIGH_UPDATE_F_OVERRIDE|
4293 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4294 NEIGH_UPDATE_F_ISROUTER)),
4295 NDISC_REDIRECT, &ndopts);
4298 res.f6i = rcu_dereference(rt->from);
4303 struct fib6_nh_match_arg arg = {
4305 .gw = &rt->rt6i_gateway,
4308 nexthop_for_each_fib6_nh(res.f6i->nh,
4309 fib6_nh_find_match, &arg);
4311 /* fib6_info uses a nexthop that does not have fib6_nh
4312 * using the dst->dev. Should be impossible
4318 res.nh = res.f6i->fib6_nh;
4321 res.fib6_flags = res.f6i->fib6_flags;
4322 res.fib6_type = res.f6i->fib6_type;
4323 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4327 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4329 nrt->rt6i_flags &= ~RTF_GATEWAY;
4331 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4333 /* rt6_insert_exception() will take care of duplicated exceptions */
4334 if (rt6_insert_exception(nrt, &res)) {
4335 dst_release_immediate(&nrt->dst);
4339 netevent.old = &rt->dst;
4340 netevent.new = &nrt->dst;
4341 netevent.daddr = &msg->dest;
4342 netevent.neigh = neigh;
4343 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4347 neigh_release(neigh);
4350 #ifdef CONFIG_IPV6_ROUTE_INFO
4351 static struct fib6_info *rt6_get_route_info(struct net *net,
4352 const struct in6_addr *prefix, int prefixlen,
4353 const struct in6_addr *gwaddr,
4354 struct net_device *dev)
4356 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4357 int ifindex = dev->ifindex;
4358 struct fib6_node *fn;
4359 struct fib6_info *rt = NULL;
4360 struct fib6_table *table;
4362 table = fib6_get_table(net, tb_id);
4367 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4371 for_each_fib6_node_rt_rcu(fn) {
4372 /* these routes do not use nexthops */
4375 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4377 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4378 !rt->fib6_nh->fib_nh_gw_family)
4380 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4382 if (!fib6_info_hold_safe(rt))
4391 static struct fib6_info *rt6_add_route_info(struct net *net,
4392 const struct in6_addr *prefix, int prefixlen,
4393 const struct in6_addr *gwaddr,
4394 struct net_device *dev,
4397 struct fib6_config cfg = {
4398 .fc_metric = IP6_RT_PRIO_USER,
4399 .fc_ifindex = dev->ifindex,
4400 .fc_dst_len = prefixlen,
4401 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4402 RTF_UP | RTF_PREF(pref),
4403 .fc_protocol = RTPROT_RA,
4404 .fc_type = RTN_UNICAST,
4405 .fc_nlinfo.portid = 0,
4406 .fc_nlinfo.nlh = NULL,
4407 .fc_nlinfo.nl_net = net,
4410 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4411 cfg.fc_dst = *prefix;
4412 cfg.fc_gateway = *gwaddr;
4414 /* We should treat it as a default route if prefix length is 0. */
4416 cfg.fc_flags |= RTF_DEFAULT;
4418 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4420 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4424 struct fib6_info *rt6_get_dflt_router(struct net *net,
4425 const struct in6_addr *addr,
4426 struct net_device *dev)
4428 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4429 struct fib6_info *rt;
4430 struct fib6_table *table;
4432 table = fib6_get_table(net, tb_id);
4437 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4440 /* RA routes do not use nexthops */
4445 if (dev == nh->fib_nh_dev &&
4446 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4447 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4450 if (rt && !fib6_info_hold_safe(rt))
4456 struct fib6_info *rt6_add_dflt_router(struct net *net,
4457 const struct in6_addr *gwaddr,
4458 struct net_device *dev,
4460 u32 defrtr_usr_metric,
4463 struct fib6_config cfg = {
4464 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4465 .fc_metric = defrtr_usr_metric,
4466 .fc_ifindex = dev->ifindex,
4467 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4468 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4469 .fc_protocol = RTPROT_RA,
4470 .fc_type = RTN_UNICAST,
4471 .fc_nlinfo.portid = 0,
4472 .fc_nlinfo.nlh = NULL,
4473 .fc_nlinfo.nl_net = net,
4474 .fc_expires = jiffies_to_clock_t(lifetime * HZ),
4477 cfg.fc_gateway = *gwaddr;
4479 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4480 struct fib6_table *table;
4482 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4484 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4487 return rt6_get_dflt_router(net, gwaddr, dev);
4490 static void __rt6_purge_dflt_routers(struct net *net,
4491 struct fib6_table *table)
4493 struct fib6_info *rt;
4497 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4498 struct net_device *dev = fib6_info_nh_dev(rt);
4499 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4501 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4502 (!idev || idev->cnf.accept_ra != 2) &&
4503 fib6_info_hold_safe(rt)) {
4505 ip6_del_rt(net, rt, false);
4511 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4514 void rt6_purge_dflt_routers(struct net *net)
4516 struct fib6_table *table;
4517 struct hlist_head *head;
4522 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4523 head = &net->ipv6.fib_table_hash[h];
4524 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4525 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4526 __rt6_purge_dflt_routers(net, table);
4533 static void rtmsg_to_fib6_config(struct net *net,
4534 struct in6_rtmsg *rtmsg,
4535 struct fib6_config *cfg)
4537 *cfg = (struct fib6_config){
4538 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4540 .fc_ifindex = rtmsg->rtmsg_ifindex,
4541 .fc_metric = rtmsg->rtmsg_metric,
4542 .fc_expires = rtmsg->rtmsg_info,
4543 .fc_dst_len = rtmsg->rtmsg_dst_len,
4544 .fc_src_len = rtmsg->rtmsg_src_len,
4545 .fc_flags = rtmsg->rtmsg_flags,
4546 .fc_type = rtmsg->rtmsg_type,
4548 .fc_nlinfo.nl_net = net,
4550 .fc_dst = rtmsg->rtmsg_dst,
4551 .fc_src = rtmsg->rtmsg_src,
4552 .fc_gateway = rtmsg->rtmsg_gateway,
4556 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
4558 struct fib6_config cfg;
4561 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4563 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4566 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4570 /* Only do the default setting of fc_metric in route adding */
4571 if (cfg.fc_metric == 0)
4572 cfg.fc_metric = IP6_RT_PRIO_USER;
4573 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4576 err = ip6_route_del(&cfg, NULL);
4584 * Drop the packet on the floor
4587 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4589 struct dst_entry *dst = skb_dst(skb);
4590 struct net *net = dev_net(dst->dev);
4591 struct inet6_dev *idev;
4595 if (netif_is_l3_master(skb->dev) ||
4596 dst->dev == net->loopback_dev)
4597 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4599 idev = ip6_dst_idev(dst);
4601 switch (ipstats_mib_noroutes) {
4602 case IPSTATS_MIB_INNOROUTES:
4603 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4604 if (type == IPV6_ADDR_ANY) {
4605 SKB_DR_SET(reason, IP_INADDRERRORS);
4606 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4609 SKB_DR_SET(reason, IP_INNOROUTES);
4611 case IPSTATS_MIB_OUTNOROUTES:
4612 SKB_DR_OR(reason, IP_OUTNOROUTES);
4613 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4617 /* Start over by dropping the dst for l3mdev case */
4618 if (netif_is_l3_master(skb->dev))
4621 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4622 kfree_skb_reason(skb, reason);
4626 static int ip6_pkt_discard(struct sk_buff *skb)
4628 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4631 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4633 skb->dev = skb_dst(skb)->dev;
4634 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4637 static int ip6_pkt_prohibit(struct sk_buff *skb)
4639 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4642 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4644 skb->dev = skb_dst(skb)->dev;
4645 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4649 * Allocate a dst for local (unicast / anycast) address.
4652 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4653 struct inet6_dev *idev,
4654 const struct in6_addr *addr,
4655 bool anycast, gfp_t gfp_flags,
4656 struct netlink_ext_ack *extack)
4658 struct fib6_config cfg = {
4659 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4660 .fc_ifindex = idev->dev->ifindex,
4661 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4664 .fc_protocol = RTPROT_KERNEL,
4665 .fc_nlinfo.nl_net = net,
4666 .fc_ignore_dev_down = true,
4668 struct fib6_info *f6i;
4672 cfg.fc_type = RTN_ANYCAST;
4673 cfg.fc_flags |= RTF_ANYCAST;
4675 cfg.fc_type = RTN_LOCAL;
4676 cfg.fc_flags |= RTF_LOCAL;
4679 f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
4683 err = ip6_route_info_create_nh(f6i, &cfg, gfp_flags, extack);
4685 return ERR_PTR(err);
4687 f6i->dst_nocount = true;
4690 (READ_ONCE(net->ipv6.devconf_all->disable_policy) ||
4691 READ_ONCE(idev->cnf.disable_policy)))
4692 f6i->dst_nopolicy = true;
4697 /* remove deleted ip from prefsrc entries */
4698 struct arg_dev_net_ip {
4700 struct in6_addr *addr;
4703 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4705 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4706 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4709 rt != net->ipv6.fib6_null_entry &&
4710 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
4711 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
4712 spin_lock_bh(&rt6_exception_lock);
4713 /* remove prefsrc entry */
4714 rt->fib6_prefsrc.plen = 0;
4715 spin_unlock_bh(&rt6_exception_lock);
4720 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4722 struct net *net = dev_net(ifp->idev->dev);
4723 struct arg_dev_net_ip adni = {
4727 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4730 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4732 /* Remove routers and update dst entries when gateway turn into host. */
4733 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4735 struct in6_addr *gateway = (struct in6_addr *)arg;
4738 /* RA routes do not use nexthops */
4743 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4744 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4747 /* Further clean up cached routes in exception table.
4748 * This is needed because cached route may have a different
4749 * gateway than its 'parent' in the case of an ip redirect.
4751 fib6_nh_exceptions_clean_tohost(nh, gateway);
4756 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4758 fib6_clean_all(net, fib6_clean_tohost, gateway);
4761 struct arg_netdev_event {
4762 const struct net_device *dev;
4764 unsigned char nh_flags;
4765 unsigned long event;
4769 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4771 struct fib6_info *iter;
4772 struct fib6_node *fn;
4774 fn = rcu_dereference_protected(rt->fib6_node,
4775 lockdep_is_held(&rt->fib6_table->tb6_lock));
4776 iter = rcu_dereference_protected(fn->leaf,
4777 lockdep_is_held(&rt->fib6_table->tb6_lock));
4779 if (iter->fib6_metric == rt->fib6_metric &&
4780 rt6_qualify_for_ecmp(iter))
4782 iter = rcu_dereference_protected(iter->fib6_next,
4783 lockdep_is_held(&rt->fib6_table->tb6_lock));
4789 /* only called for fib entries with builtin fib6_nh */
4790 static bool rt6_is_dead(const struct fib6_info *rt)
4792 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4793 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4794 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4800 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4802 struct fib6_info *iter;
4805 if (!rt6_is_dead(rt))
4806 total += rt->fib6_nh->fib_nh_weight;
4808 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4809 if (!rt6_is_dead(iter))
4810 total += iter->fib6_nh->fib_nh_weight;
4816 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4818 int upper_bound = -1;
4820 if (!rt6_is_dead(rt)) {
4821 *weight += rt->fib6_nh->fib_nh_weight;
4822 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4825 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4828 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4830 struct fib6_info *iter;
4833 rt6_upper_bound_set(rt, &weight, total);
4835 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4836 rt6_upper_bound_set(iter, &weight, total);
4839 void rt6_multipath_rebalance(struct fib6_info *rt)
4841 struct fib6_info *first;
4844 /* In case the entire multipath route was marked for flushing,
4845 * then there is no need to rebalance upon the removal of every
4848 if (!rt->fib6_nsiblings || rt->should_flush)
4851 /* During lookup routes are evaluated in order, so we need to
4852 * make sure upper bounds are assigned from the first sibling
4855 first = rt6_multipath_first_sibling(rt);
4856 if (WARN_ON_ONCE(!first))
4859 total = rt6_multipath_total_weight(first);
4860 rt6_multipath_upper_bound_set(first, total);
4863 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4865 const struct arg_netdev_event *arg = p_arg;
4866 struct net *net = dev_net(arg->dev);
4868 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4869 rt->fib6_nh->fib_nh_dev == arg->dev) {
4870 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4871 fib6_update_sernum_upto_root(net, rt);
4872 rt6_multipath_rebalance(rt);
4878 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4880 struct arg_netdev_event arg = {
4883 .nh_flags = nh_flags,
4887 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4888 arg.nh_flags |= RTNH_F_LINKDOWN;
4890 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4893 /* only called for fib entries with inline fib6_nh */
4894 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4895 const struct net_device *dev)
4897 struct fib6_info *iter;
4899 if (rt->fib6_nh->fib_nh_dev == dev)
4901 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4902 if (iter->fib6_nh->fib_nh_dev == dev)
4908 static void rt6_multipath_flush(struct fib6_info *rt)
4910 struct fib6_info *iter;
4912 rt->should_flush = 1;
4913 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4914 iter->should_flush = 1;
4917 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4918 const struct net_device *down_dev)
4920 struct fib6_info *iter;
4921 unsigned int dead = 0;
4923 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4924 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4926 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4927 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4928 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4934 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4935 const struct net_device *dev,
4936 unsigned char nh_flags)
4938 struct fib6_info *iter;
4940 if (rt->fib6_nh->fib_nh_dev == dev)
4941 rt->fib6_nh->fib_nh_flags |= nh_flags;
4942 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4943 if (iter->fib6_nh->fib_nh_dev == dev)
4944 iter->fib6_nh->fib_nh_flags |= nh_flags;
4947 /* called with write lock held for table with rt */
4948 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4950 const struct arg_netdev_event *arg = p_arg;
4951 const struct net_device *dev = arg->dev;
4952 struct net *net = dev_net(dev);
4954 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4957 switch (arg->event) {
4958 case NETDEV_UNREGISTER:
4959 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4961 if (rt->should_flush)
4963 if (!rt->fib6_nsiblings)
4964 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4965 if (rt6_multipath_uses_dev(rt, dev)) {
4968 count = rt6_multipath_dead_count(rt, dev);
4969 if (rt->fib6_nsiblings + 1 == count) {
4970 rt6_multipath_flush(rt);
4973 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4975 fib6_update_sernum(net, rt);
4976 rt6_multipath_rebalance(rt);
4980 if (rt->fib6_nh->fib_nh_dev != dev ||
4981 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4983 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4984 rt6_multipath_rebalance(rt);
4991 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4993 struct arg_netdev_event arg = {
4999 struct net *net = dev_net(dev);
5001 if (net->ipv6.sysctl.skip_notify_on_dev_down)
5002 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
5004 fib6_clean_all(net, fib6_ifdown, &arg);
5007 void rt6_disable_ip(struct net_device *dev, unsigned long event)
5009 rt6_sync_down_dev(dev, event);
5010 rt6_uncached_list_flush_dev(dev);
5011 neigh_ifdown(&nd_tbl, dev);
5014 struct rt6_mtu_change_arg {
5015 struct net_device *dev;
5017 struct fib6_info *f6i;
5020 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
5022 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
5023 struct fib6_info *f6i = arg->f6i;
5025 /* For administrative MTU increase, there is no way to discover
5026 * IPv6 PMTU increase, so PMTU increase should be updated here.
5027 * Since RFC 1981 doesn't include administrative MTU increase
5028 * update PMTU increase is a MUST. (i.e. jumbo frame)
5030 if (nh->fib_nh_dev == arg->dev) {
5031 struct inet6_dev *idev = __in6_dev_get(arg->dev);
5032 u32 mtu = f6i->fib6_pmtu;
5034 if (mtu >= arg->mtu ||
5035 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
5036 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
5038 spin_lock_bh(&rt6_exception_lock);
5039 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
5040 spin_unlock_bh(&rt6_exception_lock);
5046 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
5048 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
5049 struct inet6_dev *idev;
5051 /* In IPv6 pmtu discovery is not optional,
5052 so that RTAX_MTU lock cannot disable it.
5053 We still use this lock to block changes
5054 caused by addrconf/ndisc.
5057 idev = __in6_dev_get(arg->dev);
5061 if (fib6_metric_locked(f6i, RTAX_MTU))
5066 /* fib6_nh_mtu_change only returns 0, so this is safe */
5067 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
5071 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
5074 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
5076 struct rt6_mtu_change_arg arg = {
5081 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
5084 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
5085 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
5086 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
5087 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
5088 [RTA_OIF] = { .type = NLA_U32 },
5089 [RTA_IIF] = { .type = NLA_U32 },
5090 [RTA_PRIORITY] = { .type = NLA_U32 },
5091 [RTA_METRICS] = { .type = NLA_NESTED },
5092 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
5093 [RTA_PREF] = { .type = NLA_U8 },
5094 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
5095 [RTA_ENCAP] = { .type = NLA_NESTED },
5096 [RTA_EXPIRES] = { .type = NLA_U32 },
5097 [RTA_UID] = { .type = NLA_U32 },
5098 [RTA_MARK] = { .type = NLA_U32 },
5099 [RTA_TABLE] = { .type = NLA_U32 },
5100 [RTA_IP_PROTO] = { .type = NLA_U8 },
5101 [RTA_SPORT] = { .type = NLA_U16 },
5102 [RTA_DPORT] = { .type = NLA_U16 },
5103 [RTA_NH_ID] = { .type = NLA_U32 },
5104 [RTA_FLOWLABEL] = { .type = NLA_BE32 },
5107 static int rtm_to_fib6_multipath_config(struct fib6_config *cfg,
5108 struct netlink_ext_ack *extack,
5111 struct rtnexthop *rtnh;
5114 remaining = cfg->fc_mp_len;
5115 rtnh = (struct rtnexthop *)cfg->fc_mp;
5117 if (!rtnh_ok(rtnh, remaining)) {
5118 NL_SET_ERR_MSG(extack, "Invalid nexthop configuration - no valid nexthops");
5123 bool has_gateway = cfg->fc_flags & RTF_GATEWAY;
5124 int attrlen = rtnh_attrlen(rtnh);
5127 struct nlattr *nla, *attrs;
5129 attrs = rtnh_attrs(rtnh);
5130 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5132 if (nla_len(nla) < sizeof(cfg->fc_gateway)) {
5133 NL_SET_ERR_MSG(extack,
5134 "Invalid IPv6 address in RTA_GATEWAY");
5142 if (newroute && (cfg->fc_nh_id || !has_gateway)) {
5143 NL_SET_ERR_MSG(extack,
5144 "Device only routes can not be added for IPv6 using the multipath API.");
5148 rtnh = rtnh_next(rtnh, &remaining);
5149 } while (rtnh_ok(rtnh, remaining));
5151 return lwtunnel_valid_encap_type_attr(cfg->fc_mp, cfg->fc_mp_len, extack);
5154 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
5155 struct fib6_config *cfg,
5156 struct netlink_ext_ack *extack)
5158 bool newroute = nlh->nlmsg_type == RTM_NEWROUTE;
5159 struct nlattr *tb[RTA_MAX+1];
5164 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5165 rtm_ipv6_policy, extack);
5170 rtm = nlmsg_data(nlh);
5173 NL_SET_ERR_MSG(extack,
5174 "Invalid dsfield (tos): option not available for IPv6");
5178 if (tb[RTA_FLOWLABEL]) {
5179 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL],
5180 "Flow label cannot be specified for this operation");
5184 *cfg = (struct fib6_config){
5185 .fc_table = rtm->rtm_table,
5186 .fc_dst_len = rtm->rtm_dst_len,
5187 .fc_src_len = rtm->rtm_src_len,
5189 .fc_protocol = rtm->rtm_protocol,
5190 .fc_type = rtm->rtm_type,
5192 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5193 .fc_nlinfo.nlh = nlh,
5194 .fc_nlinfo.nl_net = sock_net(skb->sk),
5197 if (rtm->rtm_type == RTN_UNREACHABLE ||
5198 rtm->rtm_type == RTN_BLACKHOLE ||
5199 rtm->rtm_type == RTN_PROHIBIT ||
5200 rtm->rtm_type == RTN_THROW)
5201 cfg->fc_flags |= RTF_REJECT;
5203 if (rtm->rtm_type == RTN_LOCAL)
5204 cfg->fc_flags |= RTF_LOCAL;
5206 if (rtm->rtm_flags & RTM_F_CLONED)
5207 cfg->fc_flags |= RTF_CACHE;
5209 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5211 if (tb[RTA_NH_ID]) {
5212 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5213 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5214 NL_SET_ERR_MSG(extack,
5215 "Nexthop specification and nexthop id are mutually exclusive");
5218 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5221 if (tb[RTA_GATEWAY]) {
5222 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
5223 cfg->fc_flags |= RTF_GATEWAY;
5226 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5231 int plen = (rtm->rtm_dst_len + 7) >> 3;
5233 if (nla_len(tb[RTA_DST]) < plen)
5236 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
5240 int plen = (rtm->rtm_src_len + 7) >> 3;
5242 if (nla_len(tb[RTA_SRC]) < plen)
5245 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
5248 if (tb[RTA_PREFSRC])
5249 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
5252 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5254 if (tb[RTA_PRIORITY])
5255 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5257 if (tb[RTA_METRICS]) {
5258 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5259 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
5263 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5265 if (tb[RTA_MULTIPATH]) {
5266 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5267 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
5269 err = rtm_to_fib6_multipath_config(cfg, extack, newroute);
5275 pref = nla_get_u8(tb[RTA_PREF]);
5276 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5277 pref != ICMPV6_ROUTER_PREF_HIGH)
5278 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5279 cfg->fc_flags |= RTF_PREF(pref);
5283 cfg->fc_encap = tb[RTA_ENCAP];
5285 if (tb[RTA_ENCAP_TYPE]) {
5286 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5288 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5293 if (tb[RTA_EXPIRES]) {
5294 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5296 if (addrconf_finite_timeout(timeout)) {
5297 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5298 cfg->fc_flags |= RTF_EXPIRES;
5308 struct fib6_info *fib6_info;
5309 struct fib6_config r_cfg;
5310 struct list_head list;
5313 static int ip6_route_info_append(struct list_head *rt6_nh_list,
5314 struct fib6_info *rt,
5315 struct fib6_config *r_cfg)
5319 list_for_each_entry(nh, rt6_nh_list, list) {
5320 /* check if fib6_info already exists */
5321 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5325 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5330 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5331 list_add_tail(&nh->list, rt6_nh_list);
5336 static void ip6_route_mpath_notify(struct fib6_info *rt,
5337 struct fib6_info *rt_last,
5338 struct nl_info *info,
5341 /* if this is an APPEND route, then rt points to the first route
5342 * inserted and rt_last points to last route inserted. Userspace
5343 * wants a consistent dump of the route which starts at the first
5344 * nexthop. Since sibling routes are always added at the end of
5345 * the list, find the first sibling of the last route appended
5349 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5350 rt = list_first_or_null_rcu(&rt_last->fib6_siblings,
5356 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5361 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5363 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5364 bool should_notify = false;
5365 struct fib6_info *leaf;
5366 struct fib6_node *fn;
5369 fn = rcu_dereference(rt->fib6_node);
5373 leaf = rcu_dereference(fn->leaf);
5378 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5379 rt6_qualify_for_ecmp(leaf)))
5380 should_notify = true;
5384 return should_notify;
5387 static int ip6_route_multipath_add(struct fib6_config *cfg,
5388 struct netlink_ext_ack *extack)
5390 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5391 struct nl_info *info = &cfg->fc_nlinfo;
5392 struct rt6_nh *nh, *nh_safe;
5393 struct fib6_config r_cfg;
5394 struct rtnexthop *rtnh;
5395 LIST_HEAD(rt6_nh_list);
5396 struct rt6_nh *err_nh;
5397 struct fib6_info *rt;
5405 err = fib6_config_validate(cfg, extack);
5409 replace = (cfg->fc_nlinfo.nlh &&
5410 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5412 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5413 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5414 nlflags |= NLM_F_APPEND;
5416 remaining = cfg->fc_mp_len;
5417 rtnh = (struct rtnexthop *)cfg->fc_mp;
5419 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5420 * fib6_info structs per nexthop
5422 while (rtnh_ok(rtnh, remaining)) {
5423 memcpy(&r_cfg, cfg, sizeof(*cfg));
5424 if (rtnh->rtnh_ifindex)
5425 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5427 attrlen = rtnh_attrlen(rtnh);
5429 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5431 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5433 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5434 r_cfg.fc_flags |= RTF_GATEWAY;
5437 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5438 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5440 r_cfg.fc_encap_type = nla_get_u16(nla);
5443 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5444 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5451 err = ip6_route_info_create_nh(rt, &r_cfg, GFP_KERNEL, extack);
5457 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5459 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
5461 fib6_info_release(rt);
5465 rtnh = rtnh_next(rtnh, &remaining);
5468 /* for add and replace send one notification with all nexthops.
5469 * Skip the notification in fib6_add_rt2node and send one with
5470 * the full route when done
5472 info->skip_notify = 1;
5474 /* For add and replace, send one notification with all nexthops. For
5475 * append, send one notification with all appended nexthops.
5477 info->skip_notify_kernel = 1;
5480 list_for_each_entry(nh, &rt6_nh_list, list) {
5481 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5485 NL_SET_ERR_MSG_MOD(extack,
5486 "multipath route replace failed (check consistency of installed routes)");
5490 /* save reference to last route successfully inserted */
5491 rt_last = nh->fib6_info;
5493 /* save reference to first route for notification */
5495 rt_notif = nh->fib6_info;
5497 /* Because each route is added like a single route we remove
5498 * these flags after the first nexthop: if there is a collision,
5499 * we have already failed to add the first nexthop:
5500 * fib6_add_rt2node() has rejected it; when replacing, old
5501 * nexthops have been replaced by first new, the rest should
5504 if (cfg->fc_nlinfo.nlh) {
5505 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5507 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5512 /* An in-kernel notification should only be sent in case the new
5513 * multipath route is added as the first route in the node, or if
5514 * it was appended to it. We pass 'rt_notif' since it is the first
5515 * sibling and might allow us to skip some checks in the replace case.
5517 if (ip6_route_mpath_should_notify(rt_notif)) {
5518 enum fib_event_type fib_event;
5520 if (rt_notif->fib6_nsiblings != nhn - 1)
5521 fib_event = FIB_EVENT_ENTRY_APPEND;
5523 fib_event = FIB_EVENT_ENTRY_REPLACE;
5525 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5526 fib_event, rt_notif,
5529 /* Delete all the siblings that were just added */
5535 /* success ... tell user about new route */
5536 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5540 /* send notification for routes that were added so that
5541 * the delete notifications sent by ip6_route_del are
5545 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5547 /* Delete routes that were already added */
5548 list_for_each_entry(nh, &rt6_nh_list, list) {
5551 ip6_route_del(&nh->r_cfg, extack);
5555 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, list) {
5556 fib6_info_release(nh->fib6_info);
5557 list_del(&nh->list);
5564 static int ip6_route_multipath_del(struct fib6_config *cfg,
5565 struct netlink_ext_ack *extack)
5567 struct fib6_config r_cfg;
5568 struct rtnexthop *rtnh;
5574 remaining = cfg->fc_mp_len;
5575 rtnh = (struct rtnexthop *)cfg->fc_mp;
5577 /* Parse a Multipath Entry */
5578 while (rtnh_ok(rtnh, remaining)) {
5579 memcpy(&r_cfg, cfg, sizeof(*cfg));
5580 if (rtnh->rtnh_ifindex)
5581 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5583 attrlen = rtnh_attrlen(rtnh);
5585 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5587 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5589 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5590 r_cfg.fc_flags |= RTF_GATEWAY;
5594 err = ip6_route_del(&r_cfg, extack);
5598 rtnh = rtnh_next(rtnh, &remaining);
5604 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5605 struct netlink_ext_ack *extack)
5607 struct fib6_config cfg;
5610 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5616 err = !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id);
5620 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5626 return ip6_route_multipath_del(&cfg, extack);
5628 cfg.fc_delete_all_nh = 1;
5629 return ip6_route_del(&cfg, extack);
5633 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5634 struct netlink_ext_ack *extack)
5636 struct fib6_config cfg;
5639 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5643 if (cfg.fc_metric == 0)
5644 cfg.fc_metric = IP6_RT_PRIO_USER;
5647 return ip6_route_multipath_add(&cfg, extack);
5649 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5652 /* add the overhead of this fib6_nh to nexthop_len */
5653 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5655 int *nexthop_len = arg;
5657 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5658 + NLA_ALIGN(sizeof(struct rtnexthop))
5659 + nla_total_size(16); /* RTA_GATEWAY */
5661 if (nh->fib_nh_lws) {
5662 /* RTA_ENCAP_TYPE */
5663 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5665 *nexthop_len += nla_total_size(2);
5671 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5676 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5677 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5680 struct fib6_nh *nh = f6i->fib6_nh;
5681 struct fib6_info *sibling;
5684 if (f6i->fib6_nsiblings) {
5685 rt6_nh_nlmsg_size(nh, &nexthop_len);
5689 list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
5691 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
5696 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5699 return NLMSG_ALIGN(sizeof(struct rtmsg))
5700 + nla_total_size(16) /* RTA_SRC */
5701 + nla_total_size(16) /* RTA_DST */
5702 + nla_total_size(16) /* RTA_GATEWAY */
5703 + nla_total_size(16) /* RTA_PREFSRC */
5704 + nla_total_size(4) /* RTA_TABLE */
5705 + nla_total_size(4) /* RTA_IIF */
5706 + nla_total_size(4) /* RTA_OIF */
5707 + nla_total_size(4) /* RTA_PRIORITY */
5708 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5709 + nla_total_size(sizeof(struct rta_cacheinfo))
5710 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5711 + nla_total_size(1) /* RTA_PREF */
5715 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5716 unsigned char *flags)
5718 if (nexthop_is_multipath(nh)) {
5721 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5723 goto nla_put_failure;
5725 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5726 goto nla_put_failure;
5728 nla_nest_end(skb, mp);
5730 struct fib6_nh *fib6_nh;
5732 fib6_nh = nexthop_fib6_nh(nh);
5733 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5735 goto nla_put_failure;
5744 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5745 struct fib6_info *rt, struct dst_entry *dst,
5746 struct in6_addr *dest, struct in6_addr *src,
5747 int iif, int type, u32 portid, u32 seq,
5750 struct rt6_info *rt6 = dst_rt6_info(dst);
5751 struct rt6key *rt6_dst, *rt6_src;
5752 u32 *pmetrics, table, rt6_flags;
5753 unsigned char nh_flags = 0;
5754 struct nlmsghdr *nlh;
5758 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5763 rt6_dst = &rt6->rt6i_dst;
5764 rt6_src = &rt6->rt6i_src;
5765 rt6_flags = rt6->rt6i_flags;
5767 rt6_dst = &rt->fib6_dst;
5768 rt6_src = &rt->fib6_src;
5769 rt6_flags = rt->fib6_flags;
5772 rtm = nlmsg_data(nlh);
5773 rtm->rtm_family = AF_INET6;
5774 rtm->rtm_dst_len = rt6_dst->plen;
5775 rtm->rtm_src_len = rt6_src->plen;
5778 table = rt->fib6_table->tb6_id;
5780 table = RT6_TABLE_UNSPEC;
5781 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5782 if (nla_put_u32(skb, RTA_TABLE, table))
5783 goto nla_put_failure;
5785 rtm->rtm_type = rt->fib6_type;
5787 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5788 rtm->rtm_protocol = rt->fib6_protocol;
5790 if (rt6_flags & RTF_CACHE)
5791 rtm->rtm_flags |= RTM_F_CLONED;
5794 if (nla_put_in6_addr(skb, RTA_DST, dest))
5795 goto nla_put_failure;
5796 rtm->rtm_dst_len = 128;
5797 } else if (rtm->rtm_dst_len)
5798 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5799 goto nla_put_failure;
5800 #ifdef CONFIG_IPV6_SUBTREES
5802 if (nla_put_in6_addr(skb, RTA_SRC, src))
5803 goto nla_put_failure;
5804 rtm->rtm_src_len = 128;
5805 } else if (rtm->rtm_src_len &&
5806 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5807 goto nla_put_failure;
5810 #ifdef CONFIG_IPV6_MROUTE
5811 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5812 int err = ip6mr_get_route(net, skb, rtm, portid);
5817 goto nla_put_failure;
5820 if (nla_put_u32(skb, RTA_IIF, iif))
5821 goto nla_put_failure;
5823 struct in6_addr saddr_buf;
5824 if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
5825 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5826 goto nla_put_failure;
5829 if (rt->fib6_prefsrc.plen) {
5830 struct in6_addr saddr_buf;
5831 saddr_buf = rt->fib6_prefsrc.addr;
5832 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5833 goto nla_put_failure;
5836 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5837 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5838 goto nla_put_failure;
5840 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5841 goto nla_put_failure;
5843 /* For multipath routes, walk the siblings list and add
5844 * each as a nexthop within RTA_MULTIPATH.
5847 if (rt6_flags & RTF_GATEWAY &&
5848 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5849 goto nla_put_failure;
5851 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5852 goto nla_put_failure;
5854 if (dst->lwtstate &&
5855 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5856 goto nla_put_failure;
5857 } else if (rt->fib6_nsiblings) {
5858 struct fib6_info *sibling;
5861 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5863 goto nla_put_failure;
5865 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5866 rt->fib6_nh->fib_nh_weight, AF_INET6,
5868 goto nla_put_failure;
5872 list_for_each_entry_rcu(sibling, &rt->fib6_siblings,
5874 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5875 sibling->fib6_nh->fib_nh_weight,
5879 goto nla_put_failure;
5885 nla_nest_end(skb, mp);
5886 } else if (rt->nh) {
5887 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5888 goto nla_put_failure;
5890 if (nexthop_is_blackhole(rt->nh))
5891 rtm->rtm_type = RTN_BLACKHOLE;
5893 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
5894 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5895 goto nla_put_failure;
5897 rtm->rtm_flags |= nh_flags;
5899 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5900 &nh_flags, false) < 0)
5901 goto nla_put_failure;
5903 rtm->rtm_flags |= nh_flags;
5906 if (rt6_flags & RTF_EXPIRES) {
5907 expires = dst ? dst->expires : rt->expires;
5912 if (READ_ONCE(rt->offload))
5913 rtm->rtm_flags |= RTM_F_OFFLOAD;
5914 if (READ_ONCE(rt->trap))
5915 rtm->rtm_flags |= RTM_F_TRAP;
5916 if (READ_ONCE(rt->offload_failed))
5917 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5920 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5921 goto nla_put_failure;
5923 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5924 goto nla_put_failure;
5927 nlmsg_end(skb, nlh);
5931 nlmsg_cancel(skb, nlh);
5935 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5937 const struct net_device *dev = arg;
5939 if (nh->fib_nh_dev == dev)
5945 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5946 const struct net_device *dev)
5949 struct net_device *_dev = (struct net_device *)dev;
5951 return !!nexthop_for_each_fib6_nh(f6i->nh,
5952 fib6_info_nh_uses_dev,
5956 if (f6i->fib6_nh->fib_nh_dev == dev)
5959 if (f6i->fib6_nsiblings) {
5960 struct fib6_info *sibling, *next_sibling;
5962 list_for_each_entry_safe(sibling, next_sibling,
5963 &f6i->fib6_siblings, fib6_siblings) {
5964 if (sibling->fib6_nh->fib_nh_dev == dev)
5972 struct fib6_nh_exception_dump_walker {
5973 struct rt6_rtnl_dump_arg *dump;
5974 struct fib6_info *rt;
5980 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5982 struct fib6_nh_exception_dump_walker *w = arg;
5983 struct rt6_rtnl_dump_arg *dump = w->dump;
5984 struct rt6_exception_bucket *bucket;
5985 struct rt6_exception *rt6_ex;
5988 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5992 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5993 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5999 /* Expiration of entries doesn't bump sernum, insertion
6000 * does. Removal is triggered by insertion, so we can
6001 * rely on the fact that if entries change between two
6002 * partial dumps, this node is scanned again completely,
6003 * see rt6_insert_exception() and fib6_dump_table().
6005 * Count expired entries we go through as handled
6006 * entries that we'll skip next time, in case of partial
6007 * node dump. Otherwise, if entries expire meanwhile,
6008 * we'll skip the wrong amount.
6010 if (rt6_check_expired(rt6_ex->rt6i)) {
6015 err = rt6_fill_node(dump->net, dump->skb, w->rt,
6016 &rt6_ex->rt6i->dst, NULL, NULL, 0,
6018 NETLINK_CB(dump->cb->skb).portid,
6019 dump->cb->nlh->nlmsg_seq, w->flags);
6031 /* Return -1 if done with node, number of handled routes on partial dump */
6032 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
6034 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
6035 struct fib_dump_filter *filter = &arg->filter;
6036 unsigned int flags = NLM_F_MULTI;
6037 struct net *net = arg->net;
6040 if (rt == net->ipv6.fib6_null_entry)
6043 if ((filter->flags & RTM_F_PREFIX) &&
6044 !(rt->fib6_flags & RTF_PREFIX_RT)) {
6045 /* success since this is not a prefix route */
6048 if (filter->filter_set &&
6049 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
6050 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
6051 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
6055 if (filter->filter_set ||
6056 !filter->dump_routes || !filter->dump_exceptions) {
6057 flags |= NLM_F_DUMP_FILTERED;
6060 if (filter->dump_routes) {
6064 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
6066 NETLINK_CB(arg->cb->skb).portid,
6067 arg->cb->nlh->nlmsg_seq, flags)) {
6074 if (filter->dump_exceptions) {
6075 struct fib6_nh_exception_dump_walker w = { .dump = arg,
6084 err = nexthop_for_each_fib6_nh(rt->nh,
6085 rt6_nh_dump_exceptions,
6088 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
6093 return count + w.count;
6099 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
6100 const struct nlmsghdr *nlh,
6102 struct netlink_ext_ack *extack)
6107 rtm = nlmsg_payload(nlh, sizeof(*rtm));
6109 NL_SET_ERR_MSG_MOD(extack,
6110 "Invalid header for get route request");
6114 if (!netlink_strict_get_check(skb))
6115 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
6116 rtm_ipv6_policy, extack);
6118 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
6119 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
6120 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
6122 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
6125 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
6126 NL_SET_ERR_MSG_MOD(extack,
6127 "Invalid flags for get route request");
6131 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
6132 rtm_ipv6_policy, extack);
6136 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
6137 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
6138 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
6142 if (tb[RTA_FLOWLABEL] &&
6143 (nla_get_be32(tb[RTA_FLOWLABEL]) & ~IPV6_FLOWLABEL_MASK)) {
6144 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL],
6145 "Invalid flow label");
6149 for (i = 0; i <= RTA_MAX; i++) {
6166 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
6174 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
6175 struct netlink_ext_ack *extack)
6177 struct net *net = sock_net(in_skb->sk);
6178 struct nlattr *tb[RTA_MAX+1];
6179 int err, iif = 0, oif = 0;
6180 struct fib6_info *from;
6181 struct dst_entry *dst;
6182 struct rt6_info *rt;
6183 struct sk_buff *skb;
6185 struct flowi6 fl6 = {};
6189 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
6194 rtm = nlmsg_data(nlh);
6195 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
6198 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6201 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
6205 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6208 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
6212 iif = nla_get_u32(tb[RTA_IIF]);
6215 oif = nla_get_u32(tb[RTA_OIF]);
6218 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6221 fl6.flowi6_uid = make_kuid(current_user_ns(),
6222 nla_get_u32(tb[RTA_UID]));
6224 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6227 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6230 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6232 if (tb[RTA_IP_PROTO]) {
6233 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
6234 &fl6.flowi6_proto, AF_INET6,
6240 flowlabel = nla_get_be32_default(tb[RTA_FLOWLABEL], 0);
6241 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, flowlabel);
6244 struct net_device *dev;
6249 dev = dev_get_by_index_rcu(net, iif);
6256 fl6.flowi6_iif = iif;
6258 if (!ipv6_addr_any(&fl6.saddr))
6259 flags |= RT6_LOOKUP_F_HAS_SADDR;
6261 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6265 fl6.flowi6_oif = oif;
6267 dst = ip6_route_output(net, NULL, &fl6);
6271 rt = dst_rt6_info(dst);
6272 if (rt->dst.error) {
6273 err = rt->dst.error;
6278 if (rt == net->ipv6.ip6_null_entry) {
6279 err = rt->dst.error;
6284 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
6291 skb_dst_set(skb, &rt->dst);
6294 from = rcu_dereference(rt->from);
6297 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6299 NETLINK_CB(in_skb).portid,
6302 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6303 &fl6.saddr, iif, RTM_NEWROUTE,
6304 NETLINK_CB(in_skb).portid,
6316 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6321 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6322 unsigned int nlm_flags)
6324 struct sk_buff *skb;
6325 struct net *net = info->nl_net;
6330 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6334 skb = nlmsg_new(rt6_nlmsg_size(rt), GFP_ATOMIC);
6338 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6339 event, info->portid, seq, nlm_flags);
6341 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6342 WARN_ON(err == -EMSGSIZE);
6349 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6350 info->nlh, GFP_ATOMIC);
6354 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6357 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6358 struct nl_info *info)
6360 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6361 struct sk_buff *skb;
6364 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6368 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6369 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6371 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6372 WARN_ON(err == -EMSGSIZE);
6376 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6377 info->nlh, gfp_any());
6380 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6383 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
6384 bool offload, bool trap, bool offload_failed)
6386 struct sk_buff *skb;
6389 if (READ_ONCE(f6i->offload) == offload &&
6390 READ_ONCE(f6i->trap) == trap &&
6391 READ_ONCE(f6i->offload_failed) == offload_failed)
6394 WRITE_ONCE(f6i->offload, offload);
6395 WRITE_ONCE(f6i->trap, trap);
6397 /* 2 means send notifications only if offload_failed was changed. */
6398 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6399 READ_ONCE(f6i->offload_failed) == offload_failed)
6402 WRITE_ONCE(f6i->offload_failed, offload_failed);
6404 if (!rcu_access_pointer(f6i->fib6_node))
6405 /* The route was removed from the tree, do not send
6410 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6413 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6419 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6422 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6423 WARN_ON(err == -EMSGSIZE);
6428 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6432 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6434 EXPORT_SYMBOL(fib6_info_hw_flags_set);
6436 static int ip6_route_dev_notify(struct notifier_block *this,
6437 unsigned long event, void *ptr)
6439 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6440 struct net *net = dev_net(dev);
6442 if (!(dev->flags & IFF_LOOPBACK))
6445 if (event == NETDEV_REGISTER) {
6446 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6447 net->ipv6.ip6_null_entry->dst.dev = dev;
6448 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6449 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6450 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6451 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6452 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6453 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6455 } else if (event == NETDEV_UNREGISTER &&
6456 dev->reg_state != NETREG_UNREGISTERED) {
6457 /* NETDEV_UNREGISTER could be fired for multiple times by
6458 * netdev_wait_allrefs(). Make sure we only call this once.
6460 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6461 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6462 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6463 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6474 #ifdef CONFIG_PROC_FS
6475 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6477 struct net *net = (struct net *)seq->private;
6478 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6479 net->ipv6.rt6_stats->fib_nodes,
6480 net->ipv6.rt6_stats->fib_route_nodes,
6481 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6482 net->ipv6.rt6_stats->fib_rt_entries,
6483 net->ipv6.rt6_stats->fib_rt_cache,
6484 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6485 net->ipv6.rt6_stats->fib_discarded_routes);
6489 #endif /* CONFIG_PROC_FS */
6491 #ifdef CONFIG_SYSCTL
6493 static int ipv6_sysctl_rtcache_flush(const struct ctl_table *ctl, int write,
6494 void *buffer, size_t *lenp, loff_t *ppos)
6502 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6506 net = (struct net *)ctl->extra1;
6507 delay = net->ipv6.sysctl.flush_delay;
6508 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6512 static struct ctl_table ipv6_route_table_template[] = {
6514 .procname = "max_size",
6515 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6516 .maxlen = sizeof(int),
6518 .proc_handler = proc_dointvec,
6521 .procname = "gc_thresh",
6522 .data = &ip6_dst_ops_template.gc_thresh,
6523 .maxlen = sizeof(int),
6525 .proc_handler = proc_dointvec,
6528 .procname = "flush",
6529 .data = &init_net.ipv6.sysctl.flush_delay,
6530 .maxlen = sizeof(int),
6532 .proc_handler = ipv6_sysctl_rtcache_flush
6535 .procname = "gc_min_interval",
6536 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6537 .maxlen = sizeof(int),
6539 .proc_handler = proc_dointvec_jiffies,
6542 .procname = "gc_timeout",
6543 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6544 .maxlen = sizeof(int),
6546 .proc_handler = proc_dointvec_jiffies,
6549 .procname = "gc_interval",
6550 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6551 .maxlen = sizeof(int),
6553 .proc_handler = proc_dointvec_jiffies,
6556 .procname = "gc_elasticity",
6557 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6558 .maxlen = sizeof(int),
6560 .proc_handler = proc_dointvec,
6563 .procname = "mtu_expires",
6564 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6565 .maxlen = sizeof(int),
6567 .proc_handler = proc_dointvec_jiffies,
6570 .procname = "min_adv_mss",
6571 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6572 .maxlen = sizeof(int),
6574 .proc_handler = proc_dointvec,
6577 .procname = "gc_min_interval_ms",
6578 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6579 .maxlen = sizeof(int),
6581 .proc_handler = proc_dointvec_ms_jiffies,
6584 .procname = "skip_notify_on_dev_down",
6585 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6586 .maxlen = sizeof(u8),
6588 .proc_handler = proc_dou8vec_minmax,
6589 .extra1 = SYSCTL_ZERO,
6590 .extra2 = SYSCTL_ONE,
6594 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6596 struct ctl_table *table;
6598 table = kmemdup(ipv6_route_table_template,
6599 sizeof(ipv6_route_table_template),
6603 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
6604 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6605 table[2].data = &net->ipv6.sysctl.flush_delay;
6606 table[2].extra1 = net;
6607 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6608 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6609 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6610 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6611 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6612 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6613 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6614 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6620 size_t ipv6_route_sysctl_table_size(struct net *net)
6622 /* Don't export sysctls to unprivileged users */
6623 if (net->user_ns != &init_user_ns)
6626 return ARRAY_SIZE(ipv6_route_table_template);
6630 static int __net_init ip6_route_net_init(struct net *net)
6634 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6635 sizeof(net->ipv6.ip6_dst_ops));
6637 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6638 goto out_ip6_dst_ops;
6640 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6641 if (!net->ipv6.fib6_null_entry)
6642 goto out_ip6_dst_entries;
6643 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6644 sizeof(*net->ipv6.fib6_null_entry));
6646 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6647 sizeof(*net->ipv6.ip6_null_entry),
6649 if (!net->ipv6.ip6_null_entry)
6650 goto out_fib6_null_entry;
6651 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6652 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6653 ip6_template_metrics, true);
6654 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
6656 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6657 net->ipv6.fib6_has_custom_rules = false;
6658 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6659 sizeof(*net->ipv6.ip6_prohibit_entry),
6661 if (!net->ipv6.ip6_prohibit_entry)
6662 goto out_ip6_null_entry;
6663 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6664 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6665 ip6_template_metrics, true);
6666 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
6668 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6669 sizeof(*net->ipv6.ip6_blk_hole_entry),
6671 if (!net->ipv6.ip6_blk_hole_entry)
6672 goto out_ip6_prohibit_entry;
6673 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6674 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6675 ip6_template_metrics, true);
6676 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
6677 #ifdef CONFIG_IPV6_SUBTREES
6678 net->ipv6.fib6_routes_require_src = 0;
6682 net->ipv6.sysctl.flush_delay = 0;
6683 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
6684 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6685 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6686 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6687 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6688 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6689 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6690 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6692 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
6698 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6699 out_ip6_prohibit_entry:
6700 kfree(net->ipv6.ip6_prohibit_entry);
6702 kfree(net->ipv6.ip6_null_entry);
6704 out_fib6_null_entry:
6705 kfree(net->ipv6.fib6_null_entry);
6706 out_ip6_dst_entries:
6707 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6712 static void __net_exit ip6_route_net_exit(struct net *net)
6714 kfree(net->ipv6.fib6_null_entry);
6715 kfree(net->ipv6.ip6_null_entry);
6716 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6717 kfree(net->ipv6.ip6_prohibit_entry);
6718 kfree(net->ipv6.ip6_blk_hole_entry);
6720 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6723 static int __net_init ip6_route_net_init_late(struct net *net)
6725 #ifdef CONFIG_PROC_FS
6726 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6727 &ipv6_route_seq_ops,
6728 sizeof(struct ipv6_route_iter)))
6731 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6732 rt6_stats_seq_show, NULL)) {
6733 remove_proc_entry("ipv6_route", net->proc_net);
6740 static void __net_exit ip6_route_net_exit_late(struct net *net)
6742 #ifdef CONFIG_PROC_FS
6743 remove_proc_entry("ipv6_route", net->proc_net);
6744 remove_proc_entry("rt6_stats", net->proc_net);
6748 static struct pernet_operations ip6_route_net_ops = {
6749 .init = ip6_route_net_init,
6750 .exit = ip6_route_net_exit,
6753 static int __net_init ipv6_inetpeer_init(struct net *net)
6755 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6759 inet_peer_base_init(bp);
6760 net->ipv6.peers = bp;
6764 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6766 struct inet_peer_base *bp = net->ipv6.peers;
6768 net->ipv6.peers = NULL;
6769 inetpeer_invalidate_tree(bp);
6773 static struct pernet_operations ipv6_inetpeer_ops = {
6774 .init = ipv6_inetpeer_init,
6775 .exit = ipv6_inetpeer_exit,
6778 static struct pernet_operations ip6_route_net_late_ops = {
6779 .init = ip6_route_net_init_late,
6780 .exit = ip6_route_net_exit_late,
6783 static struct notifier_block ip6_route_dev_notifier = {
6784 .notifier_call = ip6_route_dev_notify,
6785 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6788 void __init ip6_route_init_special_entries(void)
6790 /* Registering of the loopback is done before this portion of code,
6791 * the loopback reference in rt6_info will not be taken, do it
6792 * manually for init_net */
6793 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6794 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6795 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6796 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6797 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6798 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6799 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6800 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6804 #if IS_BUILTIN(CONFIG_IPV6)
6805 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6806 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6808 BTF_ID_LIST(btf_fib6_info_id)
6809 BTF_ID(struct, fib6_info)
6811 static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6812 .seq_ops = &ipv6_route_seq_ops,
6813 .init_seq_private = bpf_iter_init_seq_net,
6814 .fini_seq_private = bpf_iter_fini_seq_net,
6815 .seq_priv_size = sizeof(struct ipv6_route_iter),
6818 static struct bpf_iter_reg ipv6_route_reg_info = {
6819 .target = "ipv6_route",
6820 .ctx_arg_info_size = 1,
6822 { offsetof(struct bpf_iter__ipv6_route, rt),
6823 PTR_TO_BTF_ID_OR_NULL },
6825 .seq_info = &ipv6_route_seq_info,
6828 static int __init bpf_iter_register(void)
6830 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
6831 return bpf_iter_reg_target(&ipv6_route_reg_info);
6834 static void bpf_iter_unregister(void)
6836 bpf_iter_unreg_target(&ipv6_route_reg_info);
6841 static const struct rtnl_msg_handler ip6_route_rtnl_msg_handlers[] __initconst_or_module = {
6842 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWROUTE,
6843 .doit = inet6_rtm_newroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6844 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELROUTE,
6845 .doit = inet6_rtm_delroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6846 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETROUTE,
6847 .doit = inet6_rtm_getroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6850 int __init ip6_route_init(void)
6856 ip6_dst_ops_template.kmem_cachep =
6857 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6858 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
6859 if (!ip6_dst_ops_template.kmem_cachep)
6862 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6864 goto out_kmem_cache;
6866 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6868 goto out_dst_entries;
6870 ret = register_pernet_subsys(&ip6_route_net_ops);
6872 goto out_register_inetpeer;
6874 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6878 goto out_register_subsys;
6884 ret = fib6_rules_init();
6888 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6890 goto fib6_rules_init;
6892 ret = rtnl_register_many(ip6_route_rtnl_msg_handlers);
6894 goto out_register_late_subsys;
6896 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6898 goto out_register_late_subsys;
6900 #if IS_BUILTIN(CONFIG_IPV6)
6901 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6902 ret = bpf_iter_register();
6904 goto out_register_late_subsys;
6908 for_each_possible_cpu(cpu) {
6909 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6911 INIT_LIST_HEAD(&ul->head);
6912 spin_lock_init(&ul->lock);
6918 out_register_late_subsys:
6919 rtnl_unregister_all(PF_INET6);
6920 unregister_pernet_subsys(&ip6_route_net_late_ops);
6922 fib6_rules_cleanup();
6927 out_register_subsys:
6928 unregister_pernet_subsys(&ip6_route_net_ops);
6929 out_register_inetpeer:
6930 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6932 dst_entries_destroy(&ip6_dst_blackhole_ops);
6934 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6938 void ip6_route_cleanup(void)
6940 #if IS_BUILTIN(CONFIG_IPV6)
6941 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6942 bpf_iter_unregister();
6945 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6946 unregister_pernet_subsys(&ip6_route_net_late_ops);
6947 fib6_rules_cleanup();
6950 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6951 unregister_pernet_subsys(&ip6_route_net_ops);
6952 dst_entries_destroy(&ip6_dst_blackhole_ops);
6953 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);