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 struct dst_entry *ip6_negative_advice(struct dst_entry *);
91 static void ip6_dst_destroy(struct dst_entry *);
92 static void ip6_dst_ifdown(struct dst_entry *,
93 struct net_device *dev);
94 static void ip6_dst_gc(struct dst_ops *ops);
96 static int ip6_pkt_discard(struct sk_buff *skb);
97 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
98 static int ip6_pkt_prohibit(struct sk_buff *skb);
99 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
100 static void ip6_link_failure(struct sk_buff *skb);
101 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
102 struct sk_buff *skb, u32 mtu,
104 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
105 struct sk_buff *skb);
106 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
108 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
109 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
110 struct fib6_info *rt, struct dst_entry *dst,
111 struct in6_addr *dest, struct in6_addr *src,
112 int iif, int type, u32 portid, u32 seq,
114 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
115 const struct in6_addr *daddr,
116 const struct in6_addr *saddr);
118 #ifdef CONFIG_IPV6_ROUTE_INFO
119 static struct fib6_info *rt6_add_route_info(struct net *net,
120 const struct in6_addr *prefix, int prefixlen,
121 const struct in6_addr *gwaddr,
122 struct net_device *dev,
124 static struct fib6_info *rt6_get_route_info(struct net *net,
125 const struct in6_addr *prefix, int prefixlen,
126 const struct in6_addr *gwaddr,
127 struct net_device *dev);
130 struct uncached_list {
132 struct list_head head;
133 struct list_head quarantine;
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->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_move(&rt->dst.rt_uncached,
194 spin_unlock_bh(&ul->lock);
198 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
202 if (!ipv6_addr_any(p))
203 return (const void *) p;
205 return &ipv6_hdr(skb)->daddr;
209 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
210 struct net_device *dev,
216 daddr = choose_neigh_daddr(gw, skb, daddr);
217 n = __ipv6_neigh_lookup(dev, daddr);
221 n = neigh_create(&nd_tbl, daddr, dev);
222 return IS_ERR(n) ? NULL : n;
225 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
229 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
231 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
232 dst->dev, skb, daddr);
235 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
237 struct net_device *dev = dst->dev;
238 struct rt6_info *rt = (struct rt6_info *)dst;
240 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
243 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
245 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
247 __ipv6_confirm_neigh(dev, daddr);
250 static struct dst_ops ip6_dst_ops_template = {
254 .check = ip6_dst_check,
255 .default_advmss = ip6_default_advmss,
257 .cow_metrics = dst_cow_metrics_generic,
258 .destroy = ip6_dst_destroy,
259 .ifdown = ip6_dst_ifdown,
260 .negative_advice = ip6_negative_advice,
261 .link_failure = ip6_link_failure,
262 .update_pmtu = ip6_rt_update_pmtu,
263 .redirect = rt6_do_redirect,
264 .local_out = __ip6_local_out,
265 .neigh_lookup = ip6_dst_neigh_lookup,
266 .confirm_neigh = ip6_confirm_neigh,
269 static struct dst_ops ip6_dst_blackhole_ops = {
271 .default_advmss = ip6_default_advmss,
272 .neigh_lookup = ip6_dst_neigh_lookup,
273 .check = ip6_dst_check,
274 .destroy = ip6_dst_destroy,
275 .cow_metrics = dst_cow_metrics_generic,
276 .update_pmtu = dst_blackhole_update_pmtu,
277 .redirect = dst_blackhole_redirect,
278 .mtu = dst_blackhole_mtu,
281 static const u32 ip6_template_metrics[RTAX_MAX] = {
282 [RTAX_HOPLIMIT - 1] = 0,
285 static const struct fib6_info fib6_null_entry_template = {
286 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
287 .fib6_protocol = RTPROT_KERNEL,
288 .fib6_metric = ~(u32)0,
289 .fib6_ref = REFCOUNT_INIT(1),
290 .fib6_type = RTN_UNREACHABLE,
291 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
294 static const struct rt6_info ip6_null_entry_template = {
296 .__rcuref = RCUREF_INIT(1),
298 .obsolete = DST_OBSOLETE_FORCE_CHK,
299 .error = -ENETUNREACH,
300 .input = ip6_pkt_discard,
301 .output = ip6_pkt_discard_out,
303 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
306 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
308 static const struct rt6_info ip6_prohibit_entry_template = {
310 .__rcuref = RCUREF_INIT(1),
312 .obsolete = DST_OBSOLETE_FORCE_CHK,
314 .input = ip6_pkt_prohibit,
315 .output = ip6_pkt_prohibit_out,
317 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
320 static const struct rt6_info ip6_blk_hole_entry_template = {
322 .__rcuref = RCUREF_INIT(1),
324 .obsolete = DST_OBSOLETE_FORCE_CHK,
326 .input = dst_discard,
327 .output = dst_discard_out,
329 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
334 static void rt6_info_init(struct rt6_info *rt)
336 memset_after(rt, 0, dst);
339 /* allocate dst with ip6_dst_ops */
340 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
343 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
344 1, DST_OBSOLETE_FORCE_CHK, flags);
348 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
353 EXPORT_SYMBOL(ip6_dst_alloc);
355 static void ip6_dst_destroy(struct dst_entry *dst)
357 struct rt6_info *rt = (struct rt6_info *)dst;
358 struct fib6_info *from;
359 struct inet6_dev *idev;
361 ip_dst_metrics_put(dst);
362 rt6_uncached_list_del(rt);
364 idev = rt->rt6i_idev;
366 rt->rt6i_idev = NULL;
370 from = xchg((__force struct fib6_info **)&rt->from, NULL);
371 fib6_info_release(from);
374 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
376 struct rt6_info *rt = (struct rt6_info *)dst;
377 struct inet6_dev *idev = rt->rt6i_idev;
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;
389 static bool __rt6_check_expired(const struct rt6_info *rt)
391 if (rt->rt6i_flags & RTF_EXPIRES)
392 return time_after(jiffies, rt->dst.expires);
397 static bool rt6_check_expired(const struct rt6_info *rt)
399 struct fib6_info *from;
401 from = rcu_dereference(rt->from);
403 if (rt->rt6i_flags & RTF_EXPIRES) {
404 if (time_after(jiffies, rt->dst.expires))
407 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
408 fib6_check_expired(from);
413 void fib6_select_path(const struct net *net, struct fib6_result *res,
414 struct flowi6 *fl6, int oif, bool have_oif_match,
415 const struct sk_buff *skb, int strict)
417 struct fib6_info *sibling, *next_sibling;
418 struct fib6_info *match = res->f6i;
420 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
423 if (match->nh && have_oif_match && res->nh)
426 /* We might have already computed the hash for ICMPv6 errors. In such
427 * case it will always be non-zero. Otherwise now is the time to do it.
430 (!match->nh || nexthop_is_multipath(match->nh)))
431 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
433 if (unlikely(match->nh)) {
434 nexthop_path_fib6_result(res, fl6->mp_hash);
438 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
441 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
443 const struct fib6_nh *nh = sibling->fib6_nh;
446 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
447 if (fl6->mp_hash > nh_upper_bound)
449 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
457 res->nh = match->fib6_nh;
461 * Route lookup. rcu_read_lock() should be held.
464 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
465 const struct in6_addr *saddr, int oif, int flags)
467 const struct net_device *dev;
469 if (nh->fib_nh_flags & RTNH_F_DEAD)
472 dev = nh->fib_nh_dev;
474 if (dev->ifindex == oif)
477 if (ipv6_chk_addr(net, saddr, dev,
478 flags & RT6_LOOKUP_F_IFACE))
485 struct fib6_nh_dm_arg {
487 const struct in6_addr *saddr;
493 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
495 struct fib6_nh_dm_arg *arg = _arg;
498 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
502 /* returns fib6_nh from nexthop or NULL */
503 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
504 struct fib6_result *res,
505 const struct in6_addr *saddr,
508 struct fib6_nh_dm_arg arg = {
515 if (nexthop_is_blackhole(nh))
518 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
524 static void rt6_device_match(struct net *net, struct fib6_result *res,
525 const struct in6_addr *saddr, int oif, int flags)
527 struct fib6_info *f6i = res->f6i;
528 struct fib6_info *spf6i;
531 if (!oif && ipv6_addr_any(saddr)) {
532 if (unlikely(f6i->nh)) {
533 nh = nexthop_fib6_nh(f6i->nh);
534 if (nexthop_is_blackhole(f6i->nh))
539 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
543 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
544 bool matched = false;
546 if (unlikely(spf6i->nh)) {
547 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
553 if (__rt6_device_match(net, nh, saddr, oif, flags))
562 if (oif && flags & RT6_LOOKUP_F_IFACE) {
563 res->f6i = net->ipv6.fib6_null_entry;
564 nh = res->f6i->fib6_nh;
568 if (unlikely(f6i->nh)) {
569 nh = nexthop_fib6_nh(f6i->nh);
570 if (nexthop_is_blackhole(f6i->nh))
576 if (nh->fib_nh_flags & RTNH_F_DEAD) {
577 res->f6i = net->ipv6.fib6_null_entry;
578 nh = res->f6i->fib6_nh;
582 res->fib6_type = res->f6i->fib6_type;
583 res->fib6_flags = res->f6i->fib6_flags;
587 res->fib6_flags |= RTF_REJECT;
588 res->fib6_type = RTN_BLACKHOLE;
592 #ifdef CONFIG_IPV6_ROUTER_PREF
593 struct __rt6_probe_work {
594 struct work_struct work;
595 struct in6_addr target;
596 struct net_device *dev;
597 netdevice_tracker dev_tracker;
600 static void rt6_probe_deferred(struct work_struct *w)
602 struct in6_addr mcaddr;
603 struct __rt6_probe_work *work =
604 container_of(w, struct __rt6_probe_work, work);
606 addrconf_addr_solict_mult(&work->target, &mcaddr);
607 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
608 netdev_put(work->dev, &work->dev_tracker);
612 static void rt6_probe(struct fib6_nh *fib6_nh)
614 struct __rt6_probe_work *work = NULL;
615 const struct in6_addr *nh_gw;
616 unsigned long last_probe;
617 struct neighbour *neigh;
618 struct net_device *dev;
619 struct inet6_dev *idev;
622 * Okay, this does not seem to be appropriate
623 * for now, however, we need to check if it
624 * is really so; aka Router Reachability Probing.
626 * Router Reachability Probe MUST be rate-limited
627 * to no more than one per minute.
629 if (!fib6_nh->fib_nh_gw_family)
632 nh_gw = &fib6_nh->fib_nh_gw6;
633 dev = fib6_nh->fib_nh_dev;
635 last_probe = READ_ONCE(fib6_nh->last_probe);
636 idev = __in6_dev_get(dev);
637 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
639 if (READ_ONCE(neigh->nud_state) & NUD_VALID)
642 write_lock_bh(&neigh->lock);
643 if (!(neigh->nud_state & NUD_VALID) &&
645 neigh->updated + idev->cnf.rtr_probe_interval)) {
646 work = kmalloc(sizeof(*work), GFP_ATOMIC);
648 __neigh_set_probe_once(neigh);
650 write_unlock_bh(&neigh->lock);
651 } else if (time_after(jiffies, last_probe +
652 idev->cnf.rtr_probe_interval)) {
653 work = kmalloc(sizeof(*work), GFP_ATOMIC);
656 if (!work || cmpxchg(&fib6_nh->last_probe,
657 last_probe, jiffies) != last_probe) {
660 INIT_WORK(&work->work, rt6_probe_deferred);
661 work->target = *nh_gw;
662 netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC);
664 schedule_work(&work->work);
671 static inline void rt6_probe(struct fib6_nh *fib6_nh)
677 * Default Router Selection (RFC 2461 6.3.6)
679 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
681 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
682 struct neighbour *neigh;
685 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
686 &fib6_nh->fib_nh_gw6);
688 u8 nud_state = READ_ONCE(neigh->nud_state);
690 if (nud_state & NUD_VALID)
691 ret = RT6_NUD_SUCCEED;
692 #ifdef CONFIG_IPV6_ROUTER_PREF
693 else if (!(nud_state & NUD_FAILED))
694 ret = RT6_NUD_SUCCEED;
696 ret = RT6_NUD_FAIL_PROBE;
699 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
700 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
707 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
712 if (!oif || nh->fib_nh_dev->ifindex == oif)
715 if (!m && (strict & RT6_LOOKUP_F_IFACE))
716 return RT6_NUD_FAIL_HARD;
717 #ifdef CONFIG_IPV6_ROUTER_PREF
718 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
720 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
721 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
722 int n = rt6_check_neigh(nh);
729 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
730 int oif, int strict, int *mpri, bool *do_rr)
732 bool match_do_rr = false;
736 if (nh->fib_nh_flags & RTNH_F_DEAD)
739 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
740 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
741 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
744 m = rt6_score_route(nh, fib6_flags, oif, strict);
745 if (m == RT6_NUD_FAIL_DO_RR) {
747 m = 0; /* lowest valid score */
748 } else if (m == RT6_NUD_FAIL_HARD) {
752 if (strict & RT6_LOOKUP_F_REACHABLE)
755 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
757 *do_rr = match_do_rr;
765 struct fib6_nh_frl_arg {
774 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
776 struct fib6_nh_frl_arg *arg = _arg;
779 return find_match(nh, arg->flags, arg->oif, arg->strict,
780 arg->mpri, arg->do_rr);
783 static void __find_rr_leaf(struct fib6_info *f6i_start,
784 struct fib6_info *nomatch, u32 metric,
785 struct fib6_result *res, struct fib6_info **cont,
786 int oif, int strict, bool *do_rr, int *mpri)
788 struct fib6_info *f6i;
790 for (f6i = f6i_start;
791 f6i && f6i != nomatch;
792 f6i = rcu_dereference(f6i->fib6_next)) {
793 bool matched = false;
796 if (cont && f6i->fib6_metric != metric) {
801 if (fib6_check_expired(f6i))
804 if (unlikely(f6i->nh)) {
805 struct fib6_nh_frl_arg arg = {
806 .flags = f6i->fib6_flags,
813 if (nexthop_is_blackhole(f6i->nh)) {
814 res->fib6_flags = RTF_REJECT;
815 res->fib6_type = RTN_BLACKHOLE;
817 res->nh = nexthop_fib6_nh(f6i->nh);
820 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
827 if (find_match(nh, f6i->fib6_flags, oif, strict,
834 res->fib6_flags = f6i->fib6_flags;
835 res->fib6_type = f6i->fib6_type;
840 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
841 struct fib6_info *rr_head, int oif, int strict,
842 bool *do_rr, struct fib6_result *res)
844 u32 metric = rr_head->fib6_metric;
845 struct fib6_info *cont = NULL;
848 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
849 oif, strict, do_rr, &mpri);
851 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
852 oif, strict, do_rr, &mpri);
854 if (res->f6i || !cont)
857 __find_rr_leaf(cont, NULL, metric, res, NULL,
858 oif, strict, do_rr, &mpri);
861 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
862 struct fib6_result *res, int strict)
864 struct fib6_info *leaf = rcu_dereference(fn->leaf);
865 struct fib6_info *rt0;
869 /* make sure this function or its helpers sets f6i */
872 if (!leaf || leaf == net->ipv6.fib6_null_entry)
875 rt0 = rcu_dereference(fn->rr_ptr);
879 /* Double check to make sure fn is not an intermediate node
880 * and fn->leaf does not points to its child's leaf
881 * (This might happen if all routes under fn are deleted from
882 * the tree and fib6_repair_tree() is called on the node.)
884 key_plen = rt0->fib6_dst.plen;
885 #ifdef CONFIG_IPV6_SUBTREES
886 if (rt0->fib6_src.plen)
887 key_plen = rt0->fib6_src.plen;
889 if (fn->fn_bit != key_plen)
892 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
894 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
896 /* no entries matched; do round-robin */
897 if (!next || next->fib6_metric != rt0->fib6_metric)
901 spin_lock_bh(&leaf->fib6_table->tb6_lock);
902 /* make sure next is not being deleted from the tree */
904 rcu_assign_pointer(fn->rr_ptr, next);
905 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
911 res->f6i = net->ipv6.fib6_null_entry;
912 res->nh = res->f6i->fib6_nh;
913 res->fib6_flags = res->f6i->fib6_flags;
914 res->fib6_type = res->f6i->fib6_type;
918 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
920 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
921 res->nh->fib_nh_gw_family;
924 #ifdef CONFIG_IPV6_ROUTE_INFO
925 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
926 const struct in6_addr *gwaddr)
928 struct net *net = dev_net(dev);
929 struct route_info *rinfo = (struct route_info *) opt;
930 struct in6_addr prefix_buf, *prefix;
932 unsigned long lifetime;
933 struct fib6_info *rt;
935 if (len < sizeof(struct route_info)) {
939 /* Sanity check for prefix_len and length */
940 if (rinfo->length > 3) {
942 } else if (rinfo->prefix_len > 128) {
944 } else if (rinfo->prefix_len > 64) {
945 if (rinfo->length < 2) {
948 } else if (rinfo->prefix_len > 0) {
949 if (rinfo->length < 1) {
954 pref = rinfo->route_pref;
955 if (pref == ICMPV6_ROUTER_PREF_INVALID)
958 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
960 if (rinfo->length == 3)
961 prefix = (struct in6_addr *)rinfo->prefix;
963 /* this function is safe */
964 ipv6_addr_prefix(&prefix_buf,
965 (struct in6_addr *)rinfo->prefix,
967 prefix = &prefix_buf;
970 if (rinfo->prefix_len == 0)
971 rt = rt6_get_dflt_router(net, gwaddr, dev);
973 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
976 if (rt && !lifetime) {
977 ip6_del_rt(net, rt, false);
982 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
985 rt->fib6_flags = RTF_ROUTEINFO |
986 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
989 if (!addrconf_finite_timeout(lifetime))
990 fib6_clean_expires(rt);
992 fib6_set_expires(rt, jiffies + HZ * lifetime);
994 fib6_info_release(rt);
1001 * Misc support functions
1004 /* called with rcu_lock held */
1005 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1007 struct net_device *dev = res->nh->fib_nh_dev;
1009 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1010 /* for copies of local routes, dst->dev needs to be the
1011 * device if it is a master device, the master device if
1012 * device is enslaved, and the loopback as the default
1014 if (netif_is_l3_slave(dev) &&
1015 !rt6_need_strict(&res->f6i->fib6_dst.addr))
1016 dev = l3mdev_master_dev_rcu(dev);
1017 else if (!netif_is_l3_master(dev))
1018 dev = dev_net(dev)->loopback_dev;
1019 /* last case is netif_is_l3_master(dev) is true in which
1020 * case we want dev returned to be dev
1027 static const int fib6_prop[RTN_MAX + 1] = {
1031 [RTN_BROADCAST] = 0,
1033 [RTN_MULTICAST] = 0,
1034 [RTN_BLACKHOLE] = -EINVAL,
1035 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1036 [RTN_PROHIBIT] = -EACCES,
1037 [RTN_THROW] = -EAGAIN,
1038 [RTN_NAT] = -EINVAL,
1039 [RTN_XRESOLVE] = -EINVAL,
1042 static int ip6_rt_type_to_error(u8 fib6_type)
1044 return fib6_prop[fib6_type];
1047 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1049 unsigned short flags = 0;
1051 if (rt->dst_nocount)
1052 flags |= DST_NOCOUNT;
1053 if (rt->dst_nopolicy)
1054 flags |= DST_NOPOLICY;
1059 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1061 rt->dst.error = ip6_rt_type_to_error(fib6_type);
1063 switch (fib6_type) {
1065 rt->dst.output = dst_discard_out;
1066 rt->dst.input = dst_discard;
1069 rt->dst.output = ip6_pkt_prohibit_out;
1070 rt->dst.input = ip6_pkt_prohibit;
1073 case RTN_UNREACHABLE:
1075 rt->dst.output = ip6_pkt_discard_out;
1076 rt->dst.input = ip6_pkt_discard;
1081 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1083 struct fib6_info *f6i = res->f6i;
1085 if (res->fib6_flags & RTF_REJECT) {
1086 ip6_rt_init_dst_reject(rt, res->fib6_type);
1091 rt->dst.output = ip6_output;
1093 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1094 rt->dst.input = ip6_input;
1095 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1096 rt->dst.input = ip6_mc_input;
1098 rt->dst.input = ip6_forward;
1101 if (res->nh->fib_nh_lws) {
1102 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1103 lwtunnel_set_redirect(&rt->dst);
1106 rt->dst.lastuse = jiffies;
1109 /* Caller must already hold reference to @from */
1110 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1112 rt->rt6i_flags &= ~RTF_EXPIRES;
1113 rcu_assign_pointer(rt->from, from);
1114 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1117 /* Caller must already hold reference to f6i in result */
1118 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1120 const struct fib6_nh *nh = res->nh;
1121 const struct net_device *dev = nh->fib_nh_dev;
1122 struct fib6_info *f6i = res->f6i;
1124 ip6_rt_init_dst(rt, res);
1126 rt->rt6i_dst = f6i->fib6_dst;
1127 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1128 rt->rt6i_flags = res->fib6_flags;
1129 if (nh->fib_nh_gw_family) {
1130 rt->rt6i_gateway = nh->fib_nh_gw6;
1131 rt->rt6i_flags |= RTF_GATEWAY;
1133 rt6_set_from(rt, f6i);
1134 #ifdef CONFIG_IPV6_SUBTREES
1135 rt->rt6i_src = f6i->fib6_src;
1139 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1140 struct in6_addr *saddr)
1142 struct fib6_node *pn, *sn;
1144 if (fn->fn_flags & RTN_TL_ROOT)
1146 pn = rcu_dereference(fn->parent);
1147 sn = FIB6_SUBTREE(pn);
1149 fn = fib6_node_lookup(sn, NULL, saddr);
1152 if (fn->fn_flags & RTN_RTINFO)
1157 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1159 struct rt6_info *rt = *prt;
1161 if (dst_hold_safe(&rt->dst))
1164 rt = net->ipv6.ip6_null_entry;
1173 /* called with rcu_lock held */
1174 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1176 struct net_device *dev = res->nh->fib_nh_dev;
1177 struct fib6_info *f6i = res->f6i;
1178 unsigned short flags;
1179 struct rt6_info *nrt;
1181 if (!fib6_info_hold_safe(f6i))
1184 flags = fib6_info_dst_flags(f6i);
1185 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1187 fib6_info_release(f6i);
1191 ip6_rt_copy_init(nrt, res);
1195 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1196 dst_hold(&nrt->dst);
1200 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
1201 struct fib6_table *table,
1203 const struct sk_buff *skb,
1206 struct fib6_result res = {};
1207 struct fib6_node *fn;
1208 struct rt6_info *rt;
1211 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1213 res.f6i = rcu_dereference(fn->leaf);
1215 res.f6i = net->ipv6.fib6_null_entry;
1217 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1220 if (res.f6i == net->ipv6.fib6_null_entry) {
1221 fn = fib6_backtrack(fn, &fl6->saddr);
1225 rt = net->ipv6.ip6_null_entry;
1228 } else if (res.fib6_flags & RTF_REJECT) {
1232 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1233 fl6->flowi6_oif != 0, skb, flags);
1235 /* Search through exception table */
1236 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1238 if (ip6_hold_safe(net, &rt))
1239 dst_use_noref(&rt->dst, jiffies);
1242 rt = ip6_create_rt_rcu(&res);
1246 trace_fib6_table_lookup(net, &res, table, fl6);
1253 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1254 const struct sk_buff *skb, int flags)
1256 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1258 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1260 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1261 const struct in6_addr *saddr, int oif,
1262 const struct sk_buff *skb, int strict)
1264 struct flowi6 fl6 = {
1268 struct dst_entry *dst;
1269 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1272 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1273 flags |= RT6_LOOKUP_F_HAS_SADDR;
1276 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1277 if (dst->error == 0)
1278 return (struct rt6_info *) dst;
1284 EXPORT_SYMBOL(rt6_lookup);
1286 /* ip6_ins_rt is called with FREE table->tb6_lock.
1287 * It takes new route entry, the addition fails by any reason the
1288 * route is released.
1289 * Caller must hold dst before calling it.
1292 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1293 struct netlink_ext_ack *extack)
1296 struct fib6_table *table;
1298 table = rt->fib6_table;
1299 spin_lock_bh(&table->tb6_lock);
1300 err = fib6_add(&table->tb6_root, rt, info, extack);
1301 spin_unlock_bh(&table->tb6_lock);
1306 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1308 struct nl_info info = { .nl_net = net, };
1310 return __ip6_ins_rt(rt, &info, NULL);
1313 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1314 const struct in6_addr *daddr,
1315 const struct in6_addr *saddr)
1317 struct fib6_info *f6i = res->f6i;
1318 struct net_device *dev;
1319 struct rt6_info *rt;
1325 if (!fib6_info_hold_safe(f6i))
1328 dev = ip6_rt_get_dev_rcu(res);
1329 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1331 fib6_info_release(f6i);
1335 ip6_rt_copy_init(rt, res);
1336 rt->rt6i_flags |= RTF_CACHE;
1337 rt->rt6i_dst.addr = *daddr;
1338 rt->rt6i_dst.plen = 128;
1340 if (!rt6_is_gw_or_nonexthop(res)) {
1341 if (f6i->fib6_dst.plen != 128 &&
1342 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1343 rt->rt6i_flags |= RTF_ANYCAST;
1344 #ifdef CONFIG_IPV6_SUBTREES
1345 if (rt->rt6i_src.plen && saddr) {
1346 rt->rt6i_src.addr = *saddr;
1347 rt->rt6i_src.plen = 128;
1355 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1357 struct fib6_info *f6i = res->f6i;
1358 unsigned short flags = fib6_info_dst_flags(f6i);
1359 struct net_device *dev;
1360 struct rt6_info *pcpu_rt;
1362 if (!fib6_info_hold_safe(f6i))
1366 dev = ip6_rt_get_dev_rcu(res);
1367 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
1370 fib6_info_release(f6i);
1373 ip6_rt_copy_init(pcpu_rt, res);
1374 pcpu_rt->rt6i_flags |= RTF_PCPU;
1377 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1382 static bool rt6_is_valid(const struct rt6_info *rt6)
1384 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1387 /* It should be called with rcu_read_lock() acquired */
1388 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1390 struct rt6_info *pcpu_rt;
1392 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1394 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1395 struct rt6_info *prev, **p;
1397 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1398 prev = xchg(p, NULL);
1400 dst_dev_put(&prev->dst);
1401 dst_release(&prev->dst);
1410 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1411 const struct fib6_result *res)
1413 struct rt6_info *pcpu_rt, *prev, **p;
1415 pcpu_rt = ip6_rt_pcpu_alloc(res);
1419 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1420 prev = cmpxchg(p, NULL, pcpu_rt);
1423 if (res->f6i->fib6_destroying) {
1424 struct fib6_info *from;
1426 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1427 fib6_info_release(from);
1433 /* exception hash table implementation
1435 static DEFINE_SPINLOCK(rt6_exception_lock);
1437 /* Remove rt6_ex from hash table and free the memory
1438 * Caller must hold rt6_exception_lock
1440 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1441 struct rt6_exception *rt6_ex)
1443 struct fib6_info *from;
1446 if (!bucket || !rt6_ex)
1449 net = dev_net(rt6_ex->rt6i->dst.dev);
1450 net->ipv6.rt6_stats->fib_rt_cache--;
1452 /* purge completely the exception to allow releasing the held resources:
1453 * some [sk] cache may keep the dst around for unlimited time
1455 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1456 fib6_info_release(from);
1457 dst_dev_put(&rt6_ex->rt6i->dst);
1459 hlist_del_rcu(&rt6_ex->hlist);
1460 dst_release(&rt6_ex->rt6i->dst);
1461 kfree_rcu(rt6_ex, rcu);
1462 WARN_ON_ONCE(!bucket->depth);
1466 /* Remove oldest rt6_ex in bucket and free the memory
1467 * Caller must hold rt6_exception_lock
1469 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1471 struct rt6_exception *rt6_ex, *oldest = NULL;
1476 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1477 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1480 rt6_remove_exception(bucket, oldest);
1483 static u32 rt6_exception_hash(const struct in6_addr *dst,
1484 const struct in6_addr *src)
1486 static siphash_aligned_key_t rt6_exception_key;
1488 struct in6_addr dst;
1489 struct in6_addr src;
1490 } __aligned(SIPHASH_ALIGNMENT) combined = {
1495 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
1497 #ifdef CONFIG_IPV6_SUBTREES
1499 combined.src = *src;
1501 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1503 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1506 /* Helper function to find the cached rt in the hash table
1507 * and update bucket pointer to point to the bucket for this
1508 * (daddr, saddr) pair
1509 * Caller must hold rt6_exception_lock
1511 static struct rt6_exception *
1512 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1513 const struct in6_addr *daddr,
1514 const struct in6_addr *saddr)
1516 struct rt6_exception *rt6_ex;
1519 if (!(*bucket) || !daddr)
1522 hval = rt6_exception_hash(daddr, saddr);
1525 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1526 struct rt6_info *rt6 = rt6_ex->rt6i;
1527 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1529 #ifdef CONFIG_IPV6_SUBTREES
1530 if (matched && saddr)
1531 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1539 /* Helper function to find the cached rt in the hash table
1540 * and update bucket pointer to point to the bucket for this
1541 * (daddr, saddr) pair
1542 * Caller must hold rcu_read_lock()
1544 static struct rt6_exception *
1545 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1546 const struct in6_addr *daddr,
1547 const struct in6_addr *saddr)
1549 struct rt6_exception *rt6_ex;
1552 WARN_ON_ONCE(!rcu_read_lock_held());
1554 if (!(*bucket) || !daddr)
1557 hval = rt6_exception_hash(daddr, saddr);
1560 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1561 struct rt6_info *rt6 = rt6_ex->rt6i;
1562 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1564 #ifdef CONFIG_IPV6_SUBTREES
1565 if (matched && saddr)
1566 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1574 static unsigned int fib6_mtu(const struct fib6_result *res)
1576 const struct fib6_nh *nh = res->nh;
1579 if (res->f6i->fib6_pmtu) {
1580 mtu = res->f6i->fib6_pmtu;
1582 struct net_device *dev = nh->fib_nh_dev;
1583 struct inet6_dev *idev;
1586 idev = __in6_dev_get(dev);
1587 mtu = idev->cnf.mtu6;
1591 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1593 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1596 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1598 /* used when the flushed bit is not relevant, only access to the bucket
1599 * (ie., all bucket users except rt6_insert_exception);
1601 * called under rcu lock; sometimes called with rt6_exception_lock held
1604 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1607 struct rt6_exception_bucket *bucket;
1610 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1611 lockdep_is_held(lock));
1613 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1615 /* remove bucket flushed bit if set */
1617 unsigned long p = (unsigned long)bucket;
1619 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1620 bucket = (struct rt6_exception_bucket *)p;
1626 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1628 unsigned long p = (unsigned long)bucket;
1630 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1633 /* called with rt6_exception_lock held */
1634 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1637 struct rt6_exception_bucket *bucket;
1640 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1641 lockdep_is_held(lock));
1643 p = (unsigned long)bucket;
1644 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1645 bucket = (struct rt6_exception_bucket *)p;
1646 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1649 static int rt6_insert_exception(struct rt6_info *nrt,
1650 const struct fib6_result *res)
1652 struct net *net = dev_net(nrt->dst.dev);
1653 struct rt6_exception_bucket *bucket;
1654 struct fib6_info *f6i = res->f6i;
1655 struct in6_addr *src_key = NULL;
1656 struct rt6_exception *rt6_ex;
1657 struct fib6_nh *nh = res->nh;
1661 spin_lock_bh(&rt6_exception_lock);
1663 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1664 lockdep_is_held(&rt6_exception_lock));
1666 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1672 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1673 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1678 #ifdef CONFIG_IPV6_SUBTREES
1679 /* fib6_src.plen != 0 indicates f6i is in subtree
1680 * and exception table is indexed by a hash of
1681 * both fib6_dst and fib6_src.
1682 * Otherwise, the exception table is indexed by
1683 * a hash of only fib6_dst.
1685 if (f6i->fib6_src.plen)
1686 src_key = &nrt->rt6i_src.addr;
1688 /* rt6_mtu_change() might lower mtu on f6i.
1689 * Only insert this exception route if its mtu
1690 * is less than f6i's mtu value.
1692 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1697 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1700 rt6_remove_exception(bucket, rt6_ex);
1702 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1708 rt6_ex->stamp = jiffies;
1709 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1711 net->ipv6.rt6_stats->fib_rt_cache++;
1713 /* Randomize max depth to avoid some side channels attacks. */
1714 max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH);
1715 while (bucket->depth > max_depth)
1716 rt6_exception_remove_oldest(bucket);
1719 spin_unlock_bh(&rt6_exception_lock);
1721 /* Update fn->fn_sernum to invalidate all cached dst */
1723 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1724 fib6_update_sernum(net, f6i);
1725 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1726 fib6_force_start_gc(net);
1732 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1734 struct rt6_exception_bucket *bucket;
1735 struct rt6_exception *rt6_ex;
1736 struct hlist_node *tmp;
1739 spin_lock_bh(&rt6_exception_lock);
1741 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1745 /* Prevent rt6_insert_exception() to recreate the bucket list */
1747 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1749 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1750 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1752 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1753 rt6_remove_exception(bucket, rt6_ex);
1755 WARN_ON_ONCE(!from && bucket->depth);
1759 spin_unlock_bh(&rt6_exception_lock);
1762 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1764 struct fib6_info *f6i = arg;
1766 fib6_nh_flush_exceptions(nh, f6i);
1771 void rt6_flush_exceptions(struct fib6_info *f6i)
1774 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1777 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1780 /* Find cached rt in the hash table inside passed in rt
1781 * Caller has to hold rcu_read_lock()
1783 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1784 const struct in6_addr *daddr,
1785 const struct in6_addr *saddr)
1787 const struct in6_addr *src_key = NULL;
1788 struct rt6_exception_bucket *bucket;
1789 struct rt6_exception *rt6_ex;
1790 struct rt6_info *ret = NULL;
1792 #ifdef CONFIG_IPV6_SUBTREES
1793 /* fib6i_src.plen != 0 indicates f6i is in subtree
1794 * and exception table is indexed by a hash of
1795 * both fib6_dst and fib6_src.
1796 * However, the src addr used to create the hash
1797 * might not be exactly the passed in saddr which
1798 * is a /128 addr from the flow.
1799 * So we need to use f6i->fib6_src to redo lookup
1800 * if the passed in saddr does not find anything.
1801 * (See the logic in ip6_rt_cache_alloc() on how
1802 * rt->rt6i_src is updated.)
1804 if (res->f6i->fib6_src.plen)
1808 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1809 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1811 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1814 #ifdef CONFIG_IPV6_SUBTREES
1815 /* Use fib6_src as src_key and redo lookup */
1816 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1817 src_key = &res->f6i->fib6_src.addr;
1825 /* Remove the passed in cached rt from the hash table that contains it */
1826 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1827 const struct rt6_info *rt)
1829 const struct in6_addr *src_key = NULL;
1830 struct rt6_exception_bucket *bucket;
1831 struct rt6_exception *rt6_ex;
1834 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1837 spin_lock_bh(&rt6_exception_lock);
1838 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1840 #ifdef CONFIG_IPV6_SUBTREES
1841 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1842 * and exception table is indexed by a hash of
1843 * both rt6i_dst and rt6i_src.
1844 * Otherwise, the exception table is indexed by
1845 * a hash of only rt6i_dst.
1848 src_key = &rt->rt6i_src.addr;
1850 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1854 rt6_remove_exception(bucket, rt6_ex);
1860 spin_unlock_bh(&rt6_exception_lock);
1864 struct fib6_nh_excptn_arg {
1865 struct rt6_info *rt;
1869 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1871 struct fib6_nh_excptn_arg *arg = _arg;
1874 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1881 static int rt6_remove_exception_rt(struct rt6_info *rt)
1883 struct fib6_info *from;
1885 from = rcu_dereference(rt->from);
1886 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1890 struct fib6_nh_excptn_arg arg = {
1892 .plen = from->fib6_src.plen
1896 /* rc = 1 means an entry was found */
1897 rc = nexthop_for_each_fib6_nh(from->nh,
1898 rt6_nh_remove_exception_rt,
1900 return rc ? 0 : -ENOENT;
1903 return fib6_nh_remove_exception(from->fib6_nh,
1904 from->fib6_src.plen, rt);
1907 /* Find rt6_ex which contains the passed in rt cache and
1910 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1911 const struct rt6_info *rt)
1913 const struct in6_addr *src_key = NULL;
1914 struct rt6_exception_bucket *bucket;
1915 struct rt6_exception *rt6_ex;
1917 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1918 #ifdef CONFIG_IPV6_SUBTREES
1919 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1920 * and exception table is indexed by a hash of
1921 * both rt6i_dst and rt6i_src.
1922 * Otherwise, the exception table is indexed by
1923 * a hash of only rt6i_dst.
1926 src_key = &rt->rt6i_src.addr;
1928 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1930 rt6_ex->stamp = jiffies;
1933 struct fib6_nh_match_arg {
1934 const struct net_device *dev;
1935 const struct in6_addr *gw;
1936 struct fib6_nh *match;
1939 /* determine if fib6_nh has given device and gateway */
1940 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1942 struct fib6_nh_match_arg *arg = _arg;
1944 if (arg->dev != nh->fib_nh_dev ||
1945 (arg->gw && !nh->fib_nh_gw_family) ||
1946 (!arg->gw && nh->fib_nh_gw_family) ||
1947 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1952 /* found a match, break the loop */
1956 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1958 struct fib6_info *from;
1959 struct fib6_nh *fib6_nh;
1963 from = rcu_dereference(rt->from);
1964 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1968 struct fib6_nh_match_arg arg = {
1970 .gw = &rt->rt6i_gateway,
1973 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1977 fib6_nh = arg.match;
1979 fib6_nh = from->fib6_nh;
1981 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1986 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1987 struct rt6_info *rt, int mtu)
1989 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1990 * lowest MTU in the path: always allow updating the route PMTU to
1991 * reflect PMTU decreases.
1993 * If the new MTU is higher, and the route PMTU is equal to the local
1994 * MTU, this means the old MTU is the lowest in the path, so allow
1995 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1999 if (dst_mtu(&rt->dst) >= mtu)
2002 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2008 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2009 const struct fib6_nh *nh, int mtu)
2011 struct rt6_exception_bucket *bucket;
2012 struct rt6_exception *rt6_ex;
2015 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2019 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2020 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2021 struct rt6_info *entry = rt6_ex->rt6i;
2023 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2024 * route), the metrics of its rt->from have already
2027 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2028 rt6_mtu_change_route_allowed(idev, entry, mtu))
2029 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2035 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2037 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2038 const struct in6_addr *gateway)
2040 struct rt6_exception_bucket *bucket;
2041 struct rt6_exception *rt6_ex;
2042 struct hlist_node *tmp;
2045 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2048 spin_lock_bh(&rt6_exception_lock);
2049 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2051 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2052 hlist_for_each_entry_safe(rt6_ex, tmp,
2053 &bucket->chain, hlist) {
2054 struct rt6_info *entry = rt6_ex->rt6i;
2056 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2057 RTF_CACHE_GATEWAY &&
2058 ipv6_addr_equal(gateway,
2059 &entry->rt6i_gateway)) {
2060 rt6_remove_exception(bucket, rt6_ex);
2067 spin_unlock_bh(&rt6_exception_lock);
2070 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2071 struct rt6_exception *rt6_ex,
2072 struct fib6_gc_args *gc_args,
2075 struct rt6_info *rt = rt6_ex->rt6i;
2077 /* we are pruning and obsoleting aged-out and non gateway exceptions
2078 * even if others have still references to them, so that on next
2079 * dst_check() such references can be dropped.
2080 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2081 * expired, independently from their aging, as per RFC 8201 section 4
2083 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2084 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2085 RT6_TRACE("aging clone %p\n", rt);
2086 rt6_remove_exception(bucket, rt6_ex);
2089 } else if (time_after(jiffies, rt->dst.expires)) {
2090 RT6_TRACE("purging expired route %p\n", rt);
2091 rt6_remove_exception(bucket, rt6_ex);
2095 if (rt->rt6i_flags & RTF_GATEWAY) {
2096 struct neighbour *neigh;
2098 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2100 if (!(neigh && (neigh->flags & NTF_ROUTER))) {
2101 RT6_TRACE("purging route %p via non-router but gateway\n",
2103 rt6_remove_exception(bucket, rt6_ex);
2111 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2112 struct fib6_gc_args *gc_args,
2115 struct rt6_exception_bucket *bucket;
2116 struct rt6_exception *rt6_ex;
2117 struct hlist_node *tmp;
2120 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2124 spin_lock(&rt6_exception_lock);
2125 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2127 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2128 hlist_for_each_entry_safe(rt6_ex, tmp,
2129 &bucket->chain, hlist) {
2130 rt6_age_examine_exception(bucket, rt6_ex,
2136 spin_unlock(&rt6_exception_lock);
2137 rcu_read_unlock_bh();
2140 struct fib6_nh_age_excptn_arg {
2141 struct fib6_gc_args *gc_args;
2145 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2147 struct fib6_nh_age_excptn_arg *arg = _arg;
2149 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2153 void rt6_age_exceptions(struct fib6_info *f6i,
2154 struct fib6_gc_args *gc_args,
2158 struct fib6_nh_age_excptn_arg arg = {
2163 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2166 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2170 /* must be called with rcu lock held */
2171 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2172 struct flowi6 *fl6, struct fib6_result *res, int strict)
2174 struct fib6_node *fn, *saved_fn;
2176 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2180 rt6_select(net, fn, oif, res, strict);
2181 if (res->f6i == net->ipv6.fib6_null_entry) {
2182 fn = fib6_backtrack(fn, &fl6->saddr);
2184 goto redo_rt6_select;
2185 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2186 /* also consider unreachable route */
2187 strict &= ~RT6_LOOKUP_F_REACHABLE;
2189 goto redo_rt6_select;
2193 trace_fib6_table_lookup(net, res, table, fl6);
2198 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2199 int oif, struct flowi6 *fl6,
2200 const struct sk_buff *skb, int flags)
2202 struct fib6_result res = {};
2203 struct rt6_info *rt = NULL;
2206 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2207 !rcu_read_lock_held());
2209 strict |= flags & RT6_LOOKUP_F_IFACE;
2210 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2211 if (net->ipv6.devconf_all->forwarding == 0)
2212 strict |= RT6_LOOKUP_F_REACHABLE;
2216 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2217 if (res.f6i == net->ipv6.fib6_null_entry)
2220 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2222 /*Search through exception table */
2223 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2226 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2227 !res.nh->fib_nh_gw_family)) {
2228 /* Create a RTF_CACHE clone which will not be
2229 * owned by the fib6 tree. It is for the special case where
2230 * the daddr in the skb during the neighbor look-up is different
2231 * from the fl6->daddr used to look-up route here.
2233 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2236 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2237 * As rt6_uncached_list_add() does not consume refcnt,
2238 * this refcnt is always returned to the caller even
2239 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2241 rt6_uncached_list_add(rt);
2247 /* Get a percpu copy */
2249 rt = rt6_get_pcpu_route(&res);
2252 rt = rt6_make_pcpu_route(net, &res);
2258 rt = net->ipv6.ip6_null_entry;
2259 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2260 ip6_hold_safe(net, &rt);
2265 EXPORT_SYMBOL_GPL(ip6_pol_route);
2267 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
2268 struct fib6_table *table,
2270 const struct sk_buff *skb,
2273 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2276 struct dst_entry *ip6_route_input_lookup(struct net *net,
2277 struct net_device *dev,
2279 const struct sk_buff *skb,
2282 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2283 flags |= RT6_LOOKUP_F_IFACE;
2285 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2287 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2289 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2290 struct flow_keys *keys,
2291 struct flow_keys *flkeys)
2293 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2294 const struct ipv6hdr *key_iph = outer_iph;
2295 struct flow_keys *_flkeys = flkeys;
2296 const struct ipv6hdr *inner_iph;
2297 const struct icmp6hdr *icmph;
2298 struct ipv6hdr _inner_iph;
2299 struct icmp6hdr _icmph;
2301 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2304 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2305 sizeof(_icmph), &_icmph);
2309 if (!icmpv6_is_err(icmph->icmp6_type))
2312 inner_iph = skb_header_pointer(skb,
2313 skb_transport_offset(skb) + sizeof(*icmph),
2314 sizeof(_inner_iph), &_inner_iph);
2318 key_iph = inner_iph;
2322 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2323 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2324 keys->tags.flow_label = _flkeys->tags.flow_label;
2325 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2327 keys->addrs.v6addrs.src = key_iph->saddr;
2328 keys->addrs.v6addrs.dst = key_iph->daddr;
2329 keys->tags.flow_label = ip6_flowlabel(key_iph);
2330 keys->basic.ip_proto = key_iph->nexthdr;
2334 static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2335 const struct sk_buff *skb,
2338 u32 hash_fields = ip6_multipath_hash_fields(net);
2339 struct flow_keys keys, hash_keys;
2341 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2344 memset(&hash_keys, 0, sizeof(hash_keys));
2345 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2347 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2348 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2349 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2350 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2351 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2352 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2353 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2354 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2355 hash_keys.tags.flow_label = keys.tags.flow_label;
2356 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2357 hash_keys.ports.src = keys.ports.src;
2358 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2359 hash_keys.ports.dst = keys.ports.dst;
2361 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2362 return flow_hash_from_keys(&hash_keys);
2365 static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2366 const struct sk_buff *skb,
2369 u32 hash_fields = ip6_multipath_hash_fields(net);
2370 struct flow_keys keys, hash_keys;
2372 /* We assume the packet carries an encapsulation, but if none was
2373 * encountered during dissection of the outer flow, then there is no
2374 * point in calling the flow dissector again.
2379 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2382 memset(&hash_keys, 0, sizeof(hash_keys));
2383 skb_flow_dissect_flow_keys(skb, &keys, 0);
2385 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2388 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2389 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2390 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2391 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2392 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2393 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2394 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2395 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2396 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2397 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2398 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2399 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2400 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2401 hash_keys.tags.flow_label = keys.tags.flow_label;
2404 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2405 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2406 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2407 hash_keys.ports.src = keys.ports.src;
2408 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2409 hash_keys.ports.dst = keys.ports.dst;
2411 return flow_hash_from_keys(&hash_keys);
2414 static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2415 const struct sk_buff *skb)
2417 u32 mhash, mhash_inner;
2418 bool has_inner = true;
2420 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2421 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2423 return jhash_2words(mhash, mhash_inner, 0);
2426 static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2427 const struct flowi6 *fl6)
2429 u32 hash_fields = ip6_multipath_hash_fields(net);
2430 struct flow_keys hash_keys;
2432 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2435 memset(&hash_keys, 0, sizeof(hash_keys));
2436 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2437 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2438 hash_keys.addrs.v6addrs.src = fl6->saddr;
2439 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2440 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2441 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2442 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2443 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2444 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2445 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2446 hash_keys.ports.src = fl6->fl6_sport;
2447 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2448 hash_keys.ports.dst = fl6->fl6_dport;
2450 return flow_hash_from_keys(&hash_keys);
2453 /* if skb is set it will be used and fl6 can be NULL */
2454 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2455 const struct sk_buff *skb, struct flow_keys *flkeys)
2457 struct flow_keys hash_keys;
2460 switch (ip6_multipath_hash_policy(net)) {
2462 memset(&hash_keys, 0, sizeof(hash_keys));
2463 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2465 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2467 hash_keys.addrs.v6addrs.src = fl6->saddr;
2468 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2469 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2470 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2472 mhash = flow_hash_from_keys(&hash_keys);
2476 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2477 struct flow_keys keys;
2479 /* short-circuit if we already have L4 hash present */
2481 return skb_get_hash_raw(skb) >> 1;
2483 memset(&hash_keys, 0, sizeof(hash_keys));
2486 skb_flow_dissect_flow_keys(skb, &keys, flag);
2489 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2490 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2491 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2492 hash_keys.ports.src = flkeys->ports.src;
2493 hash_keys.ports.dst = flkeys->ports.dst;
2494 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2496 memset(&hash_keys, 0, sizeof(hash_keys));
2497 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2498 hash_keys.addrs.v6addrs.src = fl6->saddr;
2499 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2500 hash_keys.ports.src = fl6->fl6_sport;
2501 hash_keys.ports.dst = fl6->fl6_dport;
2502 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2504 mhash = flow_hash_from_keys(&hash_keys);
2507 memset(&hash_keys, 0, sizeof(hash_keys));
2508 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2510 struct flow_keys keys;
2513 skb_flow_dissect_flow_keys(skb, &keys, 0);
2517 /* Inner can be v4 or v6 */
2518 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2519 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2520 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2521 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2522 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2523 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2524 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2525 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2526 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2527 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2529 /* Same as case 0 */
2530 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2531 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2534 /* Same as case 0 */
2535 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2536 hash_keys.addrs.v6addrs.src = fl6->saddr;
2537 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2538 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2539 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2541 mhash = flow_hash_from_keys(&hash_keys);
2545 mhash = rt6_multipath_custom_hash_skb(net, skb);
2547 mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2554 /* Called with rcu held */
2555 void ip6_route_input(struct sk_buff *skb)
2557 const struct ipv6hdr *iph = ipv6_hdr(skb);
2558 struct net *net = dev_net(skb->dev);
2559 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2560 struct ip_tunnel_info *tun_info;
2561 struct flowi6 fl6 = {
2562 .flowi6_iif = skb->dev->ifindex,
2563 .daddr = iph->daddr,
2564 .saddr = iph->saddr,
2565 .flowlabel = ip6_flowinfo(iph),
2566 .flowi6_mark = skb->mark,
2567 .flowi6_proto = iph->nexthdr,
2569 struct flow_keys *flkeys = NULL, _flkeys;
2571 tun_info = skb_tunnel_info(skb);
2572 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2573 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2575 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2578 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2579 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2581 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2585 INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
2586 struct fib6_table *table,
2588 const struct sk_buff *skb,
2591 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2594 static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2595 const struct sock *sk,
2601 if (ipv6_addr_type(&fl6->daddr) &
2602 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2603 struct dst_entry *dst;
2605 /* This function does not take refcnt on the dst */
2606 dst = l3mdev_link_scope_lookup(net, fl6);
2611 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2613 flags |= RT6_LOOKUP_F_DST_NOREF;
2614 any_src = ipv6_addr_any(&fl6->saddr);
2615 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2616 (fl6->flowi6_oif && any_src))
2617 flags |= RT6_LOOKUP_F_IFACE;
2620 flags |= RT6_LOOKUP_F_HAS_SADDR;
2622 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2624 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2627 struct dst_entry *ip6_route_output_flags(struct net *net,
2628 const struct sock *sk,
2632 struct dst_entry *dst;
2633 struct rt6_info *rt6;
2636 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2637 rt6 = (struct rt6_info *)dst;
2638 /* For dst cached in uncached_list, refcnt is already taken. */
2639 if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) {
2640 dst = &net->ipv6.ip6_null_entry->dst;
2647 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2649 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2651 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2652 struct net_device *loopback_dev = net->loopback_dev;
2653 struct dst_entry *new = NULL;
2655 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2656 DST_OBSOLETE_DEAD, 0);
2659 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2663 new->input = dst_discard;
2664 new->output = dst_discard_out;
2666 dst_copy_metrics(new, &ort->dst);
2668 rt->rt6i_idev = in6_dev_get(loopback_dev);
2669 rt->rt6i_gateway = ort->rt6i_gateway;
2670 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2672 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2673 #ifdef CONFIG_IPV6_SUBTREES
2674 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2678 dst_release(dst_orig);
2679 return new ? new : ERR_PTR(-ENOMEM);
2683 * Destination cache support functions
2686 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2690 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2693 if (fib6_check_expired(f6i))
2699 static struct dst_entry *rt6_check(struct rt6_info *rt,
2700 struct fib6_info *from,
2705 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2706 rt_cookie != cookie)
2709 if (rt6_check_expired(rt))
2715 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2716 struct fib6_info *from,
2719 if (!__rt6_check_expired(rt) &&
2720 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2721 fib6_check(from, cookie))
2727 INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2730 struct dst_entry *dst_ret;
2731 struct fib6_info *from;
2732 struct rt6_info *rt;
2734 rt = container_of(dst, struct rt6_info, dst);
2737 return rt6_is_valid(rt) ? dst : NULL;
2741 /* All IPV6 dsts are created with ->obsolete set to the value
2742 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2743 * into this function always.
2746 from = rcu_dereference(rt->from);
2748 if (from && (rt->rt6i_flags & RTF_PCPU ||
2749 unlikely(!list_empty(&rt->dst.rt_uncached))))
2750 dst_ret = rt6_dst_from_check(rt, from, cookie);
2752 dst_ret = rt6_check(rt, from, cookie);
2758 EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
2760 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2762 struct rt6_info *rt = (struct rt6_info *) dst;
2765 if (rt->rt6i_flags & RTF_CACHE) {
2767 if (rt6_check_expired(rt)) {
2768 rt6_remove_exception_rt(rt);
2780 static void ip6_link_failure(struct sk_buff *skb)
2782 struct rt6_info *rt;
2784 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2786 rt = (struct rt6_info *) skb_dst(skb);
2789 if (rt->rt6i_flags & RTF_CACHE) {
2790 rt6_remove_exception_rt(rt);
2792 struct fib6_info *from;
2793 struct fib6_node *fn;
2795 from = rcu_dereference(rt->from);
2797 fn = rcu_dereference(from->fib6_node);
2798 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2799 WRITE_ONCE(fn->fn_sernum, -1);
2806 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2808 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2809 struct fib6_info *from;
2812 from = rcu_dereference(rt0->from);
2814 rt0->dst.expires = from->expires;
2818 dst_set_expires(&rt0->dst, timeout);
2819 rt0->rt6i_flags |= RTF_EXPIRES;
2822 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2824 struct net *net = dev_net(rt->dst.dev);
2826 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2827 rt->rt6i_flags |= RTF_MODIFIED;
2828 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2831 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2833 return !(rt->rt6i_flags & RTF_CACHE) &&
2834 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2837 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2838 const struct ipv6hdr *iph, u32 mtu,
2841 const struct in6_addr *daddr, *saddr;
2842 struct rt6_info *rt6 = (struct rt6_info *)dst;
2844 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2845 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2846 * [see also comment in rt6_mtu_change_route()]
2850 daddr = &iph->daddr;
2851 saddr = &iph->saddr;
2853 daddr = &sk->sk_v6_daddr;
2854 saddr = &inet6_sk(sk)->saddr;
2861 dst_confirm_neigh(dst, daddr);
2863 if (mtu < IPV6_MIN_MTU)
2865 if (mtu >= dst_mtu(dst))
2868 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2869 rt6_do_update_pmtu(rt6, mtu);
2870 /* update rt6_ex->stamp for cache */
2871 if (rt6->rt6i_flags & RTF_CACHE)
2872 rt6_update_exception_stamp_rt(rt6);
2874 struct fib6_result res = {};
2875 struct rt6_info *nrt6;
2878 res.f6i = rcu_dereference(rt6->from);
2882 res.fib6_flags = res.f6i->fib6_flags;
2883 res.fib6_type = res.f6i->fib6_type;
2886 struct fib6_nh_match_arg arg = {
2888 .gw = &rt6->rt6i_gateway,
2891 nexthop_for_each_fib6_nh(res.f6i->nh,
2892 fib6_nh_find_match, &arg);
2894 /* fib6_info uses a nexthop that does not have fib6_nh
2895 * using the dst->dev + gw. Should be impossible.
2902 res.nh = res.f6i->fib6_nh;
2905 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2907 rt6_do_update_pmtu(nrt6, mtu);
2908 if (rt6_insert_exception(nrt6, &res))
2909 dst_release_immediate(&nrt6->dst);
2916 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2917 struct sk_buff *skb, u32 mtu,
2920 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2924 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2925 int oif, u32 mark, kuid_t uid)
2927 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2928 struct dst_entry *dst;
2929 struct flowi6 fl6 = {
2931 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2932 .daddr = iph->daddr,
2933 .saddr = iph->saddr,
2934 .flowlabel = ip6_flowinfo(iph),
2938 dst = ip6_route_output(net, NULL, &fl6);
2940 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
2943 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2945 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2947 int oif = sk->sk_bound_dev_if;
2948 struct dst_entry *dst;
2950 if (!oif && skb->dev)
2951 oif = l3mdev_master_ifindex(skb->dev);
2953 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark),
2956 dst = __sk_dst_get(sk);
2957 if (!dst || !dst->obsolete ||
2958 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2962 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2963 ip6_datagram_dst_update(sk, false);
2966 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2968 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2969 const struct flowi6 *fl6)
2971 #ifdef CONFIG_IPV6_SUBTREES
2972 struct ipv6_pinfo *np = inet6_sk(sk);
2975 ip6_dst_store(sk, dst,
2976 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2977 &sk->sk_v6_daddr : NULL,
2978 #ifdef CONFIG_IPV6_SUBTREES
2979 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2985 static bool ip6_redirect_nh_match(const struct fib6_result *res,
2987 const struct in6_addr *gw,
2988 struct rt6_info **ret)
2990 const struct fib6_nh *nh = res->nh;
2992 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2993 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2996 /* rt_cache's gateway might be different from its 'parent'
2997 * in the case of an ip redirect.
2998 * So we keep searching in the exception table if the gateway
3001 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
3002 struct rt6_info *rt_cache;
3004 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
3006 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
3015 struct fib6_nh_rd_arg {
3016 struct fib6_result *res;
3018 const struct in6_addr *gw;
3019 struct rt6_info **ret;
3022 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3024 struct fib6_nh_rd_arg *arg = _arg;
3027 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3030 /* Handle redirects */
3031 struct ip6rd_flowi {
3033 struct in6_addr gateway;
3036 INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
3037 struct fib6_table *table,
3039 const struct sk_buff *skb,
3042 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
3043 struct rt6_info *ret = NULL;
3044 struct fib6_result res = {};
3045 struct fib6_nh_rd_arg arg = {
3048 .gw = &rdfl->gateway,
3051 struct fib6_info *rt;
3052 struct fib6_node *fn;
3054 /* Get the "current" route for this destination and
3055 * check if the redirect has come from appropriate router.
3057 * RFC 4861 specifies that redirects should only be
3058 * accepted if they come from the nexthop to the target.
3059 * Due to the way the routes are chosen, this notion
3060 * is a bit fuzzy and one might need to check all possible
3065 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
3067 for_each_fib6_node_rt_rcu(fn) {
3069 if (fib6_check_expired(rt))
3071 if (rt->fib6_flags & RTF_REJECT)
3073 if (unlikely(rt->nh)) {
3074 if (nexthop_is_blackhole(rt->nh))
3076 /* on match, res->nh is filled in and potentially ret */
3077 if (nexthop_for_each_fib6_nh(rt->nh,
3078 fib6_nh_redirect_match,
3082 res.nh = rt->fib6_nh;
3083 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3090 rt = net->ipv6.fib6_null_entry;
3091 else if (rt->fib6_flags & RTF_REJECT) {
3092 ret = net->ipv6.ip6_null_entry;
3096 if (rt == net->ipv6.fib6_null_entry) {
3097 fn = fib6_backtrack(fn, &fl6->saddr);
3103 res.nh = rt->fib6_nh;
3106 ip6_hold_safe(net, &ret);
3108 res.fib6_flags = res.f6i->fib6_flags;
3109 res.fib6_type = res.f6i->fib6_type;
3110 ret = ip6_create_rt_rcu(&res);
3115 trace_fib6_table_lookup(net, &res, table, fl6);
3119 static struct dst_entry *ip6_route_redirect(struct net *net,
3120 const struct flowi6 *fl6,
3121 const struct sk_buff *skb,
3122 const struct in6_addr *gateway)
3124 int flags = RT6_LOOKUP_F_HAS_SADDR;
3125 struct ip6rd_flowi rdfl;
3128 rdfl.gateway = *gateway;
3130 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3131 flags, __ip6_route_redirect);
3134 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3137 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3138 struct dst_entry *dst;
3139 struct flowi6 fl6 = {
3140 .flowi6_iif = LOOPBACK_IFINDEX,
3142 .flowi6_mark = mark,
3143 .daddr = iph->daddr,
3144 .saddr = iph->saddr,
3145 .flowlabel = ip6_flowinfo(iph),
3149 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3150 rt6_do_redirect(dst, NULL, skb);
3153 EXPORT_SYMBOL_GPL(ip6_redirect);
3155 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3157 const struct ipv6hdr *iph = ipv6_hdr(skb);
3158 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3159 struct dst_entry *dst;
3160 struct flowi6 fl6 = {
3161 .flowi6_iif = LOOPBACK_IFINDEX,
3164 .saddr = iph->daddr,
3165 .flowi6_uid = sock_net_uid(net, NULL),
3168 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3169 rt6_do_redirect(dst, NULL, skb);
3173 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3175 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
3176 READ_ONCE(sk->sk_mark), sk->sk_uid);
3178 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3180 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3182 struct net_device *dev = dst->dev;
3183 unsigned int mtu = dst_mtu(dst);
3184 struct net *net = dev_net(dev);
3186 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3188 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3189 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3192 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3193 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3194 * IPV6_MAXPLEN is also valid and means: "any MSS,
3195 * rely only on pmtu discovery"
3197 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3202 INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
3204 return ip6_dst_mtu_maybe_forward(dst, false);
3206 EXPORT_INDIRECT_CALLABLE(ip6_mtu);
3209 * 1. mtu on route is locked - use it
3210 * 2. mtu from nexthop exception
3211 * 3. mtu from egress device
3213 * based on ip6_dst_mtu_forward and exception logic of
3214 * rt6_find_cached_rt; called with rcu_read_lock
3216 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3217 const struct in6_addr *daddr,
3218 const struct in6_addr *saddr)
3220 const struct fib6_nh *nh = res->nh;
3221 struct fib6_info *f6i = res->f6i;
3222 struct inet6_dev *idev;
3223 struct rt6_info *rt;
3226 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3227 mtu = f6i->fib6_pmtu;
3232 rt = rt6_find_cached_rt(res, daddr, saddr);
3234 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3236 struct net_device *dev = nh->fib_nh_dev;
3239 idev = __in6_dev_get(dev);
3240 if (idev && idev->cnf.mtu6 > mtu)
3241 mtu = idev->cnf.mtu6;
3244 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3246 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3249 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3252 struct dst_entry *dst;
3253 struct rt6_info *rt;
3254 struct inet6_dev *idev = in6_dev_get(dev);
3255 struct net *net = dev_net(dev);
3257 if (unlikely(!idev))
3258 return ERR_PTR(-ENODEV);
3260 rt = ip6_dst_alloc(net, dev, 0);
3261 if (unlikely(!rt)) {
3263 dst = ERR_PTR(-ENOMEM);
3267 rt->dst.input = ip6_input;
3268 rt->dst.output = ip6_output;
3269 rt->rt6i_gateway = fl6->daddr;
3270 rt->rt6i_dst.addr = fl6->daddr;
3271 rt->rt6i_dst.plen = 128;
3272 rt->rt6i_idev = idev;
3273 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3275 /* Add this dst into uncached_list so that rt6_disable_ip() can
3276 * do proper release of the net_device
3278 rt6_uncached_list_add(rt);
3280 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3286 static void ip6_dst_gc(struct dst_ops *ops)
3288 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3289 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3290 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3291 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3292 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3296 if (time_after(rt_last_gc + rt_min_interval, jiffies))
3299 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
3300 entries = dst_entries_get_slow(ops);
3301 if (entries < ops->gc_thresh)
3302 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
3304 val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
3305 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
3308 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3309 const struct in6_addr *gw_addr, u32 tbid,
3310 int flags, struct fib6_result *res)
3312 struct flowi6 fl6 = {
3313 .flowi6_oif = cfg->fc_ifindex,
3315 .saddr = cfg->fc_prefsrc,
3317 struct fib6_table *table;
3320 table = fib6_get_table(net, tbid);
3324 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3325 flags |= RT6_LOOKUP_F_HAS_SADDR;
3327 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3329 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3330 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3331 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3332 cfg->fc_ifindex != 0, NULL, flags);
3337 static int ip6_route_check_nh_onlink(struct net *net,
3338 struct fib6_config *cfg,
3339 const struct net_device *dev,
3340 struct netlink_ext_ack *extack)
3342 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3343 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3344 struct fib6_result res = {};
3347 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3348 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3349 /* ignore match if it is the default route */
3350 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3351 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3352 NL_SET_ERR_MSG(extack,
3353 "Nexthop has invalid gateway or device mismatch");
3360 static int ip6_route_check_nh(struct net *net,
3361 struct fib6_config *cfg,
3362 struct net_device **_dev,
3363 netdevice_tracker *dev_tracker,
3364 struct inet6_dev **idev)
3366 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3367 struct net_device *dev = _dev ? *_dev : NULL;
3368 int flags = RT6_LOOKUP_F_IFACE;
3369 struct fib6_result res = {};
3370 int err = -EHOSTUNREACH;
3372 if (cfg->fc_table) {
3373 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3374 cfg->fc_table, flags, &res);
3375 /* gw_addr can not require a gateway or resolve to a reject
3376 * route. If a device is given, it must match the result.
3378 if (err || res.fib6_flags & RTF_REJECT ||
3379 res.nh->fib_nh_gw_family ||
3380 (dev && dev != res.nh->fib_nh_dev))
3381 err = -EHOSTUNREACH;
3385 struct flowi6 fl6 = {
3386 .flowi6_oif = cfg->fc_ifindex,
3390 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3391 if (err || res.fib6_flags & RTF_REJECT ||
3392 res.nh->fib_nh_gw_family)
3393 err = -EHOSTUNREACH;
3398 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3399 cfg->fc_ifindex != 0, NULL, flags);
3404 if (dev != res.nh->fib_nh_dev)
3405 err = -EHOSTUNREACH;
3407 *_dev = dev = res.nh->fib_nh_dev;
3408 netdev_hold(dev, dev_tracker, GFP_ATOMIC);
3409 *idev = in6_dev_get(dev);
3415 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3416 struct net_device **_dev,
3417 netdevice_tracker *dev_tracker,
3418 struct inet6_dev **idev,
3419 struct netlink_ext_ack *extack)
3421 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3422 int gwa_type = ipv6_addr_type(gw_addr);
3423 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3424 const struct net_device *dev = *_dev;
3425 bool need_addr_check = !dev;
3428 /* if gw_addr is local we will fail to detect this in case
3429 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3430 * will return already-added prefix route via interface that
3431 * prefix route was assigned to, which might be non-loopback.
3434 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3435 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3439 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3440 /* IPv6 strictly inhibits using not link-local
3441 * addresses as nexthop address.
3442 * Otherwise, router will not able to send redirects.
3443 * It is very good, but in some (rare!) circumstances
3444 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3445 * some exceptions. --ANK
3446 * We allow IPv4-mapped nexthops to support RFC4798-type
3449 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3450 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3456 if (cfg->fc_flags & RTNH_F_ONLINK)
3457 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3459 err = ip6_route_check_nh(net, cfg, _dev, dev_tracker,
3468 /* reload in case device was changed */
3473 NL_SET_ERR_MSG(extack, "Egress device not specified");
3475 } else if (dev->flags & IFF_LOOPBACK) {
3476 NL_SET_ERR_MSG(extack,
3477 "Egress device can not be loopback device for this route");
3481 /* if we did not check gw_addr above, do so now that the
3482 * egress device has been resolved.
3484 if (need_addr_check &&
3485 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3486 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3495 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3497 if ((flags & RTF_REJECT) ||
3498 (dev && (dev->flags & IFF_LOOPBACK) &&
3499 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3500 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
3506 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3507 struct fib6_config *cfg, gfp_t gfp_flags,
3508 struct netlink_ext_ack *extack)
3510 netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker;
3511 struct net_device *dev = NULL;
3512 struct inet6_dev *idev = NULL;
3516 fib6_nh->fib_nh_family = AF_INET6;
3517 #ifdef CONFIG_IPV6_ROUTER_PREF
3518 fib6_nh->last_probe = jiffies;
3520 if (cfg->fc_is_fdb) {
3521 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3522 fib6_nh->fib_nh_gw_family = AF_INET6;
3527 if (cfg->fc_ifindex) {
3528 dev = netdev_get_by_index(net, cfg->fc_ifindex,
3529 dev_tracker, gfp_flags);
3532 idev = in6_dev_get(dev);
3537 if (cfg->fc_flags & RTNH_F_ONLINK) {
3539 NL_SET_ERR_MSG(extack,
3540 "Nexthop device required for onlink");
3544 if (!(dev->flags & IFF_UP)) {
3545 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3550 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3553 fib6_nh->fib_nh_weight = 1;
3555 /* We cannot add true routes via loopback here,
3556 * they would result in kernel looping; promote them to reject routes
3558 addr_type = ipv6_addr_type(&cfg->fc_dst);
3559 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3560 /* hold loopback dev/idev if we haven't done so. */
3561 if (dev != net->loopback_dev) {
3563 netdev_put(dev, dev_tracker);
3566 dev = net->loopback_dev;
3567 netdev_hold(dev, dev_tracker, gfp_flags);
3568 idev = in6_dev_get(dev);
3577 if (cfg->fc_flags & RTF_GATEWAY) {
3578 err = ip6_validate_gw(net, cfg, &dev, dev_tracker,
3583 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3584 fib6_nh->fib_nh_gw_family = AF_INET6;
3591 if (idev->cnf.disable_ipv6) {
3592 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3597 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3598 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3603 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3604 !netif_carrier_ok(dev))
3605 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3607 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
3608 cfg->fc_encap_type, cfg, gfp_flags, extack);
3613 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3614 if (!fib6_nh->rt6i_pcpu) {
3619 fib6_nh->fib_nh_dev = dev;
3620 fib6_nh->fib_nh_oif = dev->ifindex;
3627 lwtstate_put(fib6_nh->fib_nh_lws);
3628 fib6_nh->fib_nh_lws = NULL;
3629 netdev_put(dev, dev_tracker);
3635 void fib6_nh_release(struct fib6_nh *fib6_nh)
3637 struct rt6_exception_bucket *bucket;
3641 fib6_nh_flush_exceptions(fib6_nh, NULL);
3642 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3644 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3650 fib6_nh_release_dsts(fib6_nh);
3651 free_percpu(fib6_nh->rt6i_pcpu);
3653 fib_nh_common_release(&fib6_nh->nh_common);
3656 void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3660 if (!fib6_nh->rt6i_pcpu)
3663 for_each_possible_cpu(cpu) {
3664 struct rt6_info *pcpu_rt, **ppcpu_rt;
3666 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3667 pcpu_rt = xchg(ppcpu_rt, NULL);
3669 dst_dev_put(&pcpu_rt->dst);
3670 dst_release(&pcpu_rt->dst);
3675 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3677 struct netlink_ext_ack *extack)
3679 struct net *net = cfg->fc_nlinfo.nl_net;
3680 struct fib6_info *rt = NULL;
3681 struct nexthop *nh = NULL;
3682 struct fib6_table *table;
3683 struct fib6_nh *fib6_nh;
3687 /* RTF_PCPU is an internal flag; can not be set by userspace */
3688 if (cfg->fc_flags & RTF_PCPU) {
3689 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3693 /* RTF_CACHE is an internal flag; can not be set by userspace */
3694 if (cfg->fc_flags & RTF_CACHE) {
3695 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3699 if (cfg->fc_type > RTN_MAX) {
3700 NL_SET_ERR_MSG(extack, "Invalid route type");
3704 if (cfg->fc_dst_len > 128) {
3705 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3708 if (cfg->fc_src_len > 128) {
3709 NL_SET_ERR_MSG(extack, "Invalid source address length");
3712 #ifndef CONFIG_IPV6_SUBTREES
3713 if (cfg->fc_src_len) {
3714 NL_SET_ERR_MSG(extack,
3715 "Specifying source address requires IPV6_SUBTREES to be enabled");
3719 if (cfg->fc_nh_id) {
3720 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3722 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3725 err = fib6_check_nexthop(nh, cfg, extack);
3731 if (cfg->fc_nlinfo.nlh &&
3732 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3733 table = fib6_get_table(net, cfg->fc_table);
3735 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3736 table = fib6_new_table(net, cfg->fc_table);
3739 table = fib6_new_table(net, cfg->fc_table);
3746 rt = fib6_info_alloc(gfp_flags, !nh);
3750 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3752 if (IS_ERR(rt->fib6_metrics)) {
3753 err = PTR_ERR(rt->fib6_metrics);
3754 /* Do not leave garbage there. */
3755 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3759 if (cfg->fc_flags & RTF_ADDRCONF)
3760 rt->dst_nocount = true;
3762 if (cfg->fc_flags & RTF_EXPIRES)
3763 fib6_set_expires_locked(rt, jiffies +
3764 clock_t_to_jiffies(cfg->fc_expires));
3766 fib6_clean_expires_locked(rt);
3768 if (cfg->fc_protocol == RTPROT_UNSPEC)
3769 cfg->fc_protocol = RTPROT_BOOT;
3770 rt->fib6_protocol = cfg->fc_protocol;
3772 rt->fib6_table = table;
3773 rt->fib6_metric = cfg->fc_metric;
3774 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3775 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3777 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3778 rt->fib6_dst.plen = cfg->fc_dst_len;
3780 #ifdef CONFIG_IPV6_SUBTREES
3781 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3782 rt->fib6_src.plen = cfg->fc_src_len;
3785 if (rt->fib6_src.plen) {
3786 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3789 if (!nexthop_get(nh)) {
3790 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3794 fib6_nh = nexthop_fib6_nh(rt->nh);
3796 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3800 fib6_nh = rt->fib6_nh;
3802 /* We cannot add true routes via loopback here, they would
3803 * result in kernel looping; promote them to reject routes
3805 addr_type = ipv6_addr_type(&cfg->fc_dst);
3806 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3808 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3811 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3812 struct net_device *dev = fib6_nh->fib_nh_dev;
3814 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3815 NL_SET_ERR_MSG(extack, "Invalid source address");
3819 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3820 rt->fib6_prefsrc.plen = 128;
3822 rt->fib6_prefsrc.plen = 0;
3826 fib6_info_release(rt);
3827 return ERR_PTR(err);
3829 ip_fib_metrics_put(rt->fib6_metrics);
3831 return ERR_PTR(err);
3834 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3835 struct netlink_ext_ack *extack)
3837 struct fib6_info *rt;
3840 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3844 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3845 fib6_info_release(rt);
3850 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3852 struct net *net = info->nl_net;
3853 struct fib6_table *table;
3856 if (rt == net->ipv6.fib6_null_entry) {
3861 table = rt->fib6_table;
3862 spin_lock_bh(&table->tb6_lock);
3863 err = fib6_del(rt, info);
3864 spin_unlock_bh(&table->tb6_lock);
3867 fib6_info_release(rt);
3871 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3873 struct nl_info info = {
3875 .skip_notify = skip_notify
3878 return __ip6_del_rt(rt, &info);
3881 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3883 struct nl_info *info = &cfg->fc_nlinfo;
3884 struct net *net = info->nl_net;
3885 struct sk_buff *skb = NULL;
3886 struct fib6_table *table;
3889 if (rt == net->ipv6.fib6_null_entry)
3891 table = rt->fib6_table;
3892 spin_lock_bh(&table->tb6_lock);
3894 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3895 struct fib6_info *sibling, *next_sibling;
3896 struct fib6_node *fn;
3898 /* prefer to send a single notification with all hops */
3899 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3901 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3903 if (rt6_fill_node(net, skb, rt, NULL,
3904 NULL, NULL, 0, RTM_DELROUTE,
3905 info->portid, seq, 0) < 0) {
3909 info->skip_notify = 1;
3912 /* 'rt' points to the first sibling route. If it is not the
3913 * leaf, then we do not need to send a notification. Otherwise,
3914 * we need to check if the last sibling has a next route or not
3915 * and emit a replace or delete notification, respectively.
3917 info->skip_notify_kernel = 1;
3918 fn = rcu_dereference_protected(rt->fib6_node,
3919 lockdep_is_held(&table->tb6_lock));
3920 if (rcu_access_pointer(fn->leaf) == rt) {
3921 struct fib6_info *last_sibling, *replace_rt;
3923 last_sibling = list_last_entry(&rt->fib6_siblings,
3926 replace_rt = rcu_dereference_protected(
3927 last_sibling->fib6_next,
3928 lockdep_is_held(&table->tb6_lock));
3930 call_fib6_entry_notifiers_replace(net,
3933 call_fib6_multipath_entry_notifiers(net,
3934 FIB_EVENT_ENTRY_DEL,
3935 rt, rt->fib6_nsiblings,
3938 list_for_each_entry_safe(sibling, next_sibling,
3941 err = fib6_del(sibling, info);
3947 err = fib6_del(rt, info);
3949 spin_unlock_bh(&table->tb6_lock);
3951 fib6_info_release(rt);
3954 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3955 info->nlh, gfp_any());
3960 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3964 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3967 if (cfg->fc_flags & RTF_GATEWAY &&
3968 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3971 rc = rt6_remove_exception_rt(rt);
3976 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3979 struct fib6_result res = {
3983 struct rt6_info *rt_cache;
3985 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3987 return __ip6_del_cached_rt(rt_cache, cfg);
3992 struct fib6_nh_del_cached_rt_arg {
3993 struct fib6_config *cfg;
3994 struct fib6_info *f6i;
3997 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3999 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4002 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4003 return rc != -ESRCH ? rc : 0;
4006 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4008 struct fib6_nh_del_cached_rt_arg arg = {
4013 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4016 static int ip6_route_del(struct fib6_config *cfg,
4017 struct netlink_ext_ack *extack)
4019 struct fib6_table *table;
4020 struct fib6_info *rt;
4021 struct fib6_node *fn;
4024 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
4026 NL_SET_ERR_MSG(extack, "FIB table does not exist");
4032 fn = fib6_locate(&table->tb6_root,
4033 &cfg->fc_dst, cfg->fc_dst_len,
4034 &cfg->fc_src, cfg->fc_src_len,
4035 !(cfg->fc_flags & RTF_CACHE));
4038 for_each_fib6_node_rt_rcu(fn) {
4041 if (rt->nh && cfg->fc_nh_id &&
4042 rt->nh->id != cfg->fc_nh_id)
4045 if (cfg->fc_flags & RTF_CACHE) {
4049 rc = ip6_del_cached_rt_nh(cfg, rt);
4050 } else if (cfg->fc_nh_id) {
4054 rc = ip6_del_cached_rt(cfg, rt, nh);
4063 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4065 if (cfg->fc_protocol &&
4066 cfg->fc_protocol != rt->fib6_protocol)
4070 if (!fib6_info_hold_safe(rt))
4074 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4080 if (cfg->fc_ifindex &&
4082 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
4084 if (cfg->fc_flags & RTF_GATEWAY &&
4085 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
4087 if (!fib6_info_hold_safe(rt))
4091 /* if gateway was specified only delete the one hop */
4092 if (cfg->fc_flags & RTF_GATEWAY)
4093 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4095 return __ip6_del_rt_siblings(rt, cfg);
4103 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4105 struct netevent_redirect netevent;
4106 struct rt6_info *rt, *nrt = NULL;
4107 struct fib6_result res = {};
4108 struct ndisc_options ndopts;
4109 struct inet6_dev *in6_dev;
4110 struct neighbour *neigh;
4112 int optlen, on_link;
4115 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4116 optlen -= sizeof(*msg);
4119 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4123 msg = (struct rd_msg *)icmp6_hdr(skb);
4125 if (ipv6_addr_is_multicast(&msg->dest)) {
4126 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4131 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4133 } else if (ipv6_addr_type(&msg->target) !=
4134 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4135 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4139 in6_dev = __in6_dev_get(skb->dev);
4142 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4146 * The IP source address of the Redirect MUST be the same as the current
4147 * first-hop router for the specified ICMP Destination Address.
4150 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4151 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4156 if (ndopts.nd_opts_tgt_lladdr) {
4157 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4160 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4165 rt = (struct rt6_info *) dst;
4166 if (rt->rt6i_flags & RTF_REJECT) {
4167 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4171 /* Redirect received -> path was valid.
4172 * Look, redirects are sent only in response to data packets,
4173 * so that this nexthop apparently is reachable. --ANK
4175 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4177 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4182 * We have finally decided to accept it.
4185 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4186 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4187 NEIGH_UPDATE_F_OVERRIDE|
4188 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4189 NEIGH_UPDATE_F_ISROUTER)),
4190 NDISC_REDIRECT, &ndopts);
4193 res.f6i = rcu_dereference(rt->from);
4198 struct fib6_nh_match_arg arg = {
4200 .gw = &rt->rt6i_gateway,
4203 nexthop_for_each_fib6_nh(res.f6i->nh,
4204 fib6_nh_find_match, &arg);
4206 /* fib6_info uses a nexthop that does not have fib6_nh
4207 * using the dst->dev. Should be impossible
4213 res.nh = res.f6i->fib6_nh;
4216 res.fib6_flags = res.f6i->fib6_flags;
4217 res.fib6_type = res.f6i->fib6_type;
4218 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4222 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4224 nrt->rt6i_flags &= ~RTF_GATEWAY;
4226 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4228 /* rt6_insert_exception() will take care of duplicated exceptions */
4229 if (rt6_insert_exception(nrt, &res)) {
4230 dst_release_immediate(&nrt->dst);
4234 netevent.old = &rt->dst;
4235 netevent.new = &nrt->dst;
4236 netevent.daddr = &msg->dest;
4237 netevent.neigh = neigh;
4238 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4242 neigh_release(neigh);
4245 #ifdef CONFIG_IPV6_ROUTE_INFO
4246 static struct fib6_info *rt6_get_route_info(struct net *net,
4247 const struct in6_addr *prefix, int prefixlen,
4248 const struct in6_addr *gwaddr,
4249 struct net_device *dev)
4251 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4252 int ifindex = dev->ifindex;
4253 struct fib6_node *fn;
4254 struct fib6_info *rt = NULL;
4255 struct fib6_table *table;
4257 table = fib6_get_table(net, tb_id);
4262 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4266 for_each_fib6_node_rt_rcu(fn) {
4267 /* these routes do not use nexthops */
4270 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4272 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4273 !rt->fib6_nh->fib_nh_gw_family)
4275 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4277 if (!fib6_info_hold_safe(rt))
4286 static struct fib6_info *rt6_add_route_info(struct net *net,
4287 const struct in6_addr *prefix, int prefixlen,
4288 const struct in6_addr *gwaddr,
4289 struct net_device *dev,
4292 struct fib6_config cfg = {
4293 .fc_metric = IP6_RT_PRIO_USER,
4294 .fc_ifindex = dev->ifindex,
4295 .fc_dst_len = prefixlen,
4296 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4297 RTF_UP | RTF_PREF(pref),
4298 .fc_protocol = RTPROT_RA,
4299 .fc_type = RTN_UNICAST,
4300 .fc_nlinfo.portid = 0,
4301 .fc_nlinfo.nlh = NULL,
4302 .fc_nlinfo.nl_net = net,
4305 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4306 cfg.fc_dst = *prefix;
4307 cfg.fc_gateway = *gwaddr;
4309 /* We should treat it as a default route if prefix length is 0. */
4311 cfg.fc_flags |= RTF_DEFAULT;
4313 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4315 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4319 struct fib6_info *rt6_get_dflt_router(struct net *net,
4320 const struct in6_addr *addr,
4321 struct net_device *dev)
4323 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4324 struct fib6_info *rt;
4325 struct fib6_table *table;
4327 table = fib6_get_table(net, tb_id);
4332 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4335 /* RA routes do not use nexthops */
4340 if (dev == nh->fib_nh_dev &&
4341 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4342 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4345 if (rt && !fib6_info_hold_safe(rt))
4351 struct fib6_info *rt6_add_dflt_router(struct net *net,
4352 const struct in6_addr *gwaddr,
4353 struct net_device *dev,
4355 u32 defrtr_usr_metric)
4357 struct fib6_config cfg = {
4358 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4359 .fc_metric = defrtr_usr_metric,
4360 .fc_ifindex = dev->ifindex,
4361 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4362 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4363 .fc_protocol = RTPROT_RA,
4364 .fc_type = RTN_UNICAST,
4365 .fc_nlinfo.portid = 0,
4366 .fc_nlinfo.nlh = NULL,
4367 .fc_nlinfo.nl_net = net,
4370 cfg.fc_gateway = *gwaddr;
4372 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4373 struct fib6_table *table;
4375 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4377 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4380 return rt6_get_dflt_router(net, gwaddr, dev);
4383 static void __rt6_purge_dflt_routers(struct net *net,
4384 struct fib6_table *table)
4386 struct fib6_info *rt;
4390 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4391 struct net_device *dev = fib6_info_nh_dev(rt);
4392 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4394 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4395 (!idev || idev->cnf.accept_ra != 2) &&
4396 fib6_info_hold_safe(rt)) {
4398 ip6_del_rt(net, rt, false);
4404 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4407 void rt6_purge_dflt_routers(struct net *net)
4409 struct fib6_table *table;
4410 struct hlist_head *head;
4415 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4416 head = &net->ipv6.fib_table_hash[h];
4417 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4418 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4419 __rt6_purge_dflt_routers(net, table);
4426 static void rtmsg_to_fib6_config(struct net *net,
4427 struct in6_rtmsg *rtmsg,
4428 struct fib6_config *cfg)
4430 *cfg = (struct fib6_config){
4431 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4433 .fc_ifindex = rtmsg->rtmsg_ifindex,
4434 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4435 .fc_expires = rtmsg->rtmsg_info,
4436 .fc_dst_len = rtmsg->rtmsg_dst_len,
4437 .fc_src_len = rtmsg->rtmsg_src_len,
4438 .fc_flags = rtmsg->rtmsg_flags,
4439 .fc_type = rtmsg->rtmsg_type,
4441 .fc_nlinfo.nl_net = net,
4443 .fc_dst = rtmsg->rtmsg_dst,
4444 .fc_src = rtmsg->rtmsg_src,
4445 .fc_gateway = rtmsg->rtmsg_gateway,
4449 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
4451 struct fib6_config cfg;
4454 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4456 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4459 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4464 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4467 err = ip6_route_del(&cfg, NULL);
4475 * Drop the packet on the floor
4478 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4480 struct dst_entry *dst = skb_dst(skb);
4481 struct net *net = dev_net(dst->dev);
4482 struct inet6_dev *idev;
4486 if (netif_is_l3_master(skb->dev) ||
4487 dst->dev == net->loopback_dev)
4488 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4490 idev = ip6_dst_idev(dst);
4492 switch (ipstats_mib_noroutes) {
4493 case IPSTATS_MIB_INNOROUTES:
4494 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4495 if (type == IPV6_ADDR_ANY) {
4496 SKB_DR_SET(reason, IP_INADDRERRORS);
4497 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4500 SKB_DR_SET(reason, IP_INNOROUTES);
4502 case IPSTATS_MIB_OUTNOROUTES:
4503 SKB_DR_OR(reason, IP_OUTNOROUTES);
4504 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4508 /* Start over by dropping the dst for l3mdev case */
4509 if (netif_is_l3_master(skb->dev))
4512 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4513 kfree_skb_reason(skb, reason);
4517 static int ip6_pkt_discard(struct sk_buff *skb)
4519 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4522 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4524 skb->dev = skb_dst(skb)->dev;
4525 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4528 static int ip6_pkt_prohibit(struct sk_buff *skb)
4530 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4533 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4535 skb->dev = skb_dst(skb)->dev;
4536 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4540 * Allocate a dst for local (unicast / anycast) address.
4543 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4544 struct inet6_dev *idev,
4545 const struct in6_addr *addr,
4546 bool anycast, gfp_t gfp_flags,
4547 struct netlink_ext_ack *extack)
4549 struct fib6_config cfg = {
4550 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4551 .fc_ifindex = idev->dev->ifindex,
4552 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4555 .fc_protocol = RTPROT_KERNEL,
4556 .fc_nlinfo.nl_net = net,
4557 .fc_ignore_dev_down = true,
4559 struct fib6_info *f6i;
4562 cfg.fc_type = RTN_ANYCAST;
4563 cfg.fc_flags |= RTF_ANYCAST;
4565 cfg.fc_type = RTN_LOCAL;
4566 cfg.fc_flags |= RTF_LOCAL;
4569 f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
4571 f6i->dst_nocount = true;
4574 (net->ipv6.devconf_all->disable_policy ||
4575 idev->cnf.disable_policy))
4576 f6i->dst_nopolicy = true;
4582 /* remove deleted ip from prefsrc entries */
4583 struct arg_dev_net_ip {
4585 struct in6_addr *addr;
4588 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4590 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4591 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4594 rt != net->ipv6.fib6_null_entry &&
4595 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
4596 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
4597 spin_lock_bh(&rt6_exception_lock);
4598 /* remove prefsrc entry */
4599 rt->fib6_prefsrc.plen = 0;
4600 spin_unlock_bh(&rt6_exception_lock);
4605 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4607 struct net *net = dev_net(ifp->idev->dev);
4608 struct arg_dev_net_ip adni = {
4612 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4615 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4617 /* Remove routers and update dst entries when gateway turn into host. */
4618 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4620 struct in6_addr *gateway = (struct in6_addr *)arg;
4623 /* RA routes do not use nexthops */
4628 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4629 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4632 /* Further clean up cached routes in exception table.
4633 * This is needed because cached route may have a different
4634 * gateway than its 'parent' in the case of an ip redirect.
4636 fib6_nh_exceptions_clean_tohost(nh, gateway);
4641 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4643 fib6_clean_all(net, fib6_clean_tohost, gateway);
4646 struct arg_netdev_event {
4647 const struct net_device *dev;
4649 unsigned char nh_flags;
4650 unsigned long event;
4654 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4656 struct fib6_info *iter;
4657 struct fib6_node *fn;
4659 fn = rcu_dereference_protected(rt->fib6_node,
4660 lockdep_is_held(&rt->fib6_table->tb6_lock));
4661 iter = rcu_dereference_protected(fn->leaf,
4662 lockdep_is_held(&rt->fib6_table->tb6_lock));
4664 if (iter->fib6_metric == rt->fib6_metric &&
4665 rt6_qualify_for_ecmp(iter))
4667 iter = rcu_dereference_protected(iter->fib6_next,
4668 lockdep_is_held(&rt->fib6_table->tb6_lock));
4674 /* only called for fib entries with builtin fib6_nh */
4675 static bool rt6_is_dead(const struct fib6_info *rt)
4677 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4678 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4679 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4685 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4687 struct fib6_info *iter;
4690 if (!rt6_is_dead(rt))
4691 total += rt->fib6_nh->fib_nh_weight;
4693 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4694 if (!rt6_is_dead(iter))
4695 total += iter->fib6_nh->fib_nh_weight;
4701 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4703 int upper_bound = -1;
4705 if (!rt6_is_dead(rt)) {
4706 *weight += rt->fib6_nh->fib_nh_weight;
4707 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4710 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4713 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4715 struct fib6_info *iter;
4718 rt6_upper_bound_set(rt, &weight, total);
4720 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4721 rt6_upper_bound_set(iter, &weight, total);
4724 void rt6_multipath_rebalance(struct fib6_info *rt)
4726 struct fib6_info *first;
4729 /* In case the entire multipath route was marked for flushing,
4730 * then there is no need to rebalance upon the removal of every
4733 if (!rt->fib6_nsiblings || rt->should_flush)
4736 /* During lookup routes are evaluated in order, so we need to
4737 * make sure upper bounds are assigned from the first sibling
4740 first = rt6_multipath_first_sibling(rt);
4741 if (WARN_ON_ONCE(!first))
4744 total = rt6_multipath_total_weight(first);
4745 rt6_multipath_upper_bound_set(first, total);
4748 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4750 const struct arg_netdev_event *arg = p_arg;
4751 struct net *net = dev_net(arg->dev);
4753 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4754 rt->fib6_nh->fib_nh_dev == arg->dev) {
4755 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4756 fib6_update_sernum_upto_root(net, rt);
4757 rt6_multipath_rebalance(rt);
4763 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4765 struct arg_netdev_event arg = {
4768 .nh_flags = nh_flags,
4772 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4773 arg.nh_flags |= RTNH_F_LINKDOWN;
4775 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4778 /* only called for fib entries with inline fib6_nh */
4779 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4780 const struct net_device *dev)
4782 struct fib6_info *iter;
4784 if (rt->fib6_nh->fib_nh_dev == dev)
4786 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4787 if (iter->fib6_nh->fib_nh_dev == dev)
4793 static void rt6_multipath_flush(struct fib6_info *rt)
4795 struct fib6_info *iter;
4797 rt->should_flush = 1;
4798 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4799 iter->should_flush = 1;
4802 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4803 const struct net_device *down_dev)
4805 struct fib6_info *iter;
4806 unsigned int dead = 0;
4808 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4809 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4811 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4812 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4813 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4819 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4820 const struct net_device *dev,
4821 unsigned char nh_flags)
4823 struct fib6_info *iter;
4825 if (rt->fib6_nh->fib_nh_dev == dev)
4826 rt->fib6_nh->fib_nh_flags |= nh_flags;
4827 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4828 if (iter->fib6_nh->fib_nh_dev == dev)
4829 iter->fib6_nh->fib_nh_flags |= nh_flags;
4832 /* called with write lock held for table with rt */
4833 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4835 const struct arg_netdev_event *arg = p_arg;
4836 const struct net_device *dev = arg->dev;
4837 struct net *net = dev_net(dev);
4839 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4842 switch (arg->event) {
4843 case NETDEV_UNREGISTER:
4844 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4846 if (rt->should_flush)
4848 if (!rt->fib6_nsiblings)
4849 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4850 if (rt6_multipath_uses_dev(rt, dev)) {
4853 count = rt6_multipath_dead_count(rt, dev);
4854 if (rt->fib6_nsiblings + 1 == count) {
4855 rt6_multipath_flush(rt);
4858 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4860 fib6_update_sernum(net, rt);
4861 rt6_multipath_rebalance(rt);
4865 if (rt->fib6_nh->fib_nh_dev != dev ||
4866 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4868 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4869 rt6_multipath_rebalance(rt);
4876 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4878 struct arg_netdev_event arg = {
4884 struct net *net = dev_net(dev);
4886 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4887 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4889 fib6_clean_all(net, fib6_ifdown, &arg);
4892 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4894 rt6_sync_down_dev(dev, event);
4895 rt6_uncached_list_flush_dev(dev);
4896 neigh_ifdown(&nd_tbl, dev);
4899 struct rt6_mtu_change_arg {
4900 struct net_device *dev;
4902 struct fib6_info *f6i;
4905 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4907 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4908 struct fib6_info *f6i = arg->f6i;
4910 /* For administrative MTU increase, there is no way to discover
4911 * IPv6 PMTU increase, so PMTU increase should be updated here.
4912 * Since RFC 1981 doesn't include administrative MTU increase
4913 * update PMTU increase is a MUST. (i.e. jumbo frame)
4915 if (nh->fib_nh_dev == arg->dev) {
4916 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4917 u32 mtu = f6i->fib6_pmtu;
4919 if (mtu >= arg->mtu ||
4920 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4921 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4923 spin_lock_bh(&rt6_exception_lock);
4924 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4925 spin_unlock_bh(&rt6_exception_lock);
4931 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4933 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4934 struct inet6_dev *idev;
4936 /* In IPv6 pmtu discovery is not optional,
4937 so that RTAX_MTU lock cannot disable it.
4938 We still use this lock to block changes
4939 caused by addrconf/ndisc.
4942 idev = __in6_dev_get(arg->dev);
4946 if (fib6_metric_locked(f6i, RTAX_MTU))
4951 /* fib6_nh_mtu_change only returns 0, so this is safe */
4952 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4956 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4959 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4961 struct rt6_mtu_change_arg arg = {
4966 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4969 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4970 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
4971 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4972 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4973 [RTA_OIF] = { .type = NLA_U32 },
4974 [RTA_IIF] = { .type = NLA_U32 },
4975 [RTA_PRIORITY] = { .type = NLA_U32 },
4976 [RTA_METRICS] = { .type = NLA_NESTED },
4977 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4978 [RTA_PREF] = { .type = NLA_U8 },
4979 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4980 [RTA_ENCAP] = { .type = NLA_NESTED },
4981 [RTA_EXPIRES] = { .type = NLA_U32 },
4982 [RTA_UID] = { .type = NLA_U32 },
4983 [RTA_MARK] = { .type = NLA_U32 },
4984 [RTA_TABLE] = { .type = NLA_U32 },
4985 [RTA_IP_PROTO] = { .type = NLA_U8 },
4986 [RTA_SPORT] = { .type = NLA_U16 },
4987 [RTA_DPORT] = { .type = NLA_U16 },
4988 [RTA_NH_ID] = { .type = NLA_U32 },
4991 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4992 struct fib6_config *cfg,
4993 struct netlink_ext_ack *extack)
4996 struct nlattr *tb[RTA_MAX+1];
5000 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5001 rtm_ipv6_policy, extack);
5006 rtm = nlmsg_data(nlh);
5009 NL_SET_ERR_MSG(extack,
5010 "Invalid dsfield (tos): option not available for IPv6");
5014 *cfg = (struct fib6_config){
5015 .fc_table = rtm->rtm_table,
5016 .fc_dst_len = rtm->rtm_dst_len,
5017 .fc_src_len = rtm->rtm_src_len,
5019 .fc_protocol = rtm->rtm_protocol,
5020 .fc_type = rtm->rtm_type,
5022 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5023 .fc_nlinfo.nlh = nlh,
5024 .fc_nlinfo.nl_net = sock_net(skb->sk),
5027 if (rtm->rtm_type == RTN_UNREACHABLE ||
5028 rtm->rtm_type == RTN_BLACKHOLE ||
5029 rtm->rtm_type == RTN_PROHIBIT ||
5030 rtm->rtm_type == RTN_THROW)
5031 cfg->fc_flags |= RTF_REJECT;
5033 if (rtm->rtm_type == RTN_LOCAL)
5034 cfg->fc_flags |= RTF_LOCAL;
5036 if (rtm->rtm_flags & RTM_F_CLONED)
5037 cfg->fc_flags |= RTF_CACHE;
5039 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5041 if (tb[RTA_NH_ID]) {
5042 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5043 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5044 NL_SET_ERR_MSG(extack,
5045 "Nexthop specification and nexthop id are mutually exclusive");
5048 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5051 if (tb[RTA_GATEWAY]) {
5052 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
5053 cfg->fc_flags |= RTF_GATEWAY;
5056 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5061 int plen = (rtm->rtm_dst_len + 7) >> 3;
5063 if (nla_len(tb[RTA_DST]) < plen)
5066 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
5070 int plen = (rtm->rtm_src_len + 7) >> 3;
5072 if (nla_len(tb[RTA_SRC]) < plen)
5075 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
5078 if (tb[RTA_PREFSRC])
5079 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
5082 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5084 if (tb[RTA_PRIORITY])
5085 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5087 if (tb[RTA_METRICS]) {
5088 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5089 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
5093 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5095 if (tb[RTA_MULTIPATH]) {
5096 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5097 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
5099 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
5100 cfg->fc_mp_len, extack);
5106 pref = nla_get_u8(tb[RTA_PREF]);
5107 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5108 pref != ICMPV6_ROUTER_PREF_HIGH)
5109 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5110 cfg->fc_flags |= RTF_PREF(pref);
5114 cfg->fc_encap = tb[RTA_ENCAP];
5116 if (tb[RTA_ENCAP_TYPE]) {
5117 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5119 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5124 if (tb[RTA_EXPIRES]) {
5125 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5127 if (addrconf_finite_timeout(timeout)) {
5128 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5129 cfg->fc_flags |= RTF_EXPIRES;
5139 struct fib6_info *fib6_info;
5140 struct fib6_config r_cfg;
5141 struct list_head next;
5144 static int ip6_route_info_append(struct net *net,
5145 struct list_head *rt6_nh_list,
5146 struct fib6_info *rt,
5147 struct fib6_config *r_cfg)
5152 list_for_each_entry(nh, rt6_nh_list, next) {
5153 /* check if fib6_info already exists */
5154 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5158 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5162 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5163 list_add_tail(&nh->next, rt6_nh_list);
5168 static void ip6_route_mpath_notify(struct fib6_info *rt,
5169 struct fib6_info *rt_last,
5170 struct nl_info *info,
5173 /* if this is an APPEND route, then rt points to the first route
5174 * inserted and rt_last points to last route inserted. Userspace
5175 * wants a consistent dump of the route which starts at the first
5176 * nexthop. Since sibling routes are always added at the end of
5177 * the list, find the first sibling of the last route appended
5179 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5180 rt = list_first_entry(&rt_last->fib6_siblings,
5186 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5189 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5191 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5192 bool should_notify = false;
5193 struct fib6_info *leaf;
5194 struct fib6_node *fn;
5197 fn = rcu_dereference(rt->fib6_node);
5201 leaf = rcu_dereference(fn->leaf);
5206 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5207 rt6_qualify_for_ecmp(leaf)))
5208 should_notify = true;
5212 return should_notify;
5215 static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
5216 struct netlink_ext_ack *extack)
5218 if (nla_len(nla) < sizeof(*gw)) {
5219 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
5223 *gw = nla_get_in6_addr(nla);
5228 static int ip6_route_multipath_add(struct fib6_config *cfg,
5229 struct netlink_ext_ack *extack)
5231 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5232 struct nl_info *info = &cfg->fc_nlinfo;
5233 struct fib6_config r_cfg;
5234 struct rtnexthop *rtnh;
5235 struct fib6_info *rt;
5236 struct rt6_nh *err_nh;
5237 struct rt6_nh *nh, *nh_safe;
5243 int replace = (cfg->fc_nlinfo.nlh &&
5244 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5245 LIST_HEAD(rt6_nh_list);
5247 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5248 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5249 nlflags |= NLM_F_APPEND;
5251 remaining = cfg->fc_mp_len;
5252 rtnh = (struct rtnexthop *)cfg->fc_mp;
5254 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5255 * fib6_info structs per nexthop
5257 while (rtnh_ok(rtnh, remaining)) {
5258 memcpy(&r_cfg, cfg, sizeof(*cfg));
5259 if (rtnh->rtnh_ifindex)
5260 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5262 attrlen = rtnh_attrlen(rtnh);
5264 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5266 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5268 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5273 r_cfg.fc_flags |= RTF_GATEWAY;
5275 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5277 /* RTA_ENCAP_TYPE length checked in
5278 * lwtunnel_valid_encap_type_attr
5280 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5282 r_cfg.fc_encap_type = nla_get_u16(nla);
5285 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5286 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5292 if (!rt6_qualify_for_ecmp(rt)) {
5294 NL_SET_ERR_MSG(extack,
5295 "Device only routes can not be added for IPv6 using the multipath API.");
5296 fib6_info_release(rt);
5300 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5302 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5305 fib6_info_release(rt);
5309 rtnh = rtnh_next(rtnh, &remaining);
5312 if (list_empty(&rt6_nh_list)) {
5313 NL_SET_ERR_MSG(extack,
5314 "Invalid nexthop configuration - no valid nexthops");
5318 /* for add and replace send one notification with all nexthops.
5319 * Skip the notification in fib6_add_rt2node and send one with
5320 * the full route when done
5322 info->skip_notify = 1;
5324 /* For add and replace, send one notification with all nexthops. For
5325 * append, send one notification with all appended nexthops.
5327 info->skip_notify_kernel = 1;
5330 list_for_each_entry(nh, &rt6_nh_list, next) {
5331 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5332 fib6_info_release(nh->fib6_info);
5335 /* save reference to last route successfully inserted */
5336 rt_last = nh->fib6_info;
5338 /* save reference to first route for notification */
5340 rt_notif = nh->fib6_info;
5343 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5344 nh->fib6_info = NULL;
5347 NL_SET_ERR_MSG_MOD(extack,
5348 "multipath route replace failed (check consistency of installed routes)");
5353 /* Because each route is added like a single route we remove
5354 * these flags after the first nexthop: if there is a collision,
5355 * we have already failed to add the first nexthop:
5356 * fib6_add_rt2node() has rejected it; when replacing, old
5357 * nexthops have been replaced by first new, the rest should
5360 if (cfg->fc_nlinfo.nlh) {
5361 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5363 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5368 /* An in-kernel notification should only be sent in case the new
5369 * multipath route is added as the first route in the node, or if
5370 * it was appended to it. We pass 'rt_notif' since it is the first
5371 * sibling and might allow us to skip some checks in the replace case.
5373 if (ip6_route_mpath_should_notify(rt_notif)) {
5374 enum fib_event_type fib_event;
5376 if (rt_notif->fib6_nsiblings != nhn - 1)
5377 fib_event = FIB_EVENT_ENTRY_APPEND;
5379 fib_event = FIB_EVENT_ENTRY_REPLACE;
5381 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5382 fib_event, rt_notif,
5385 /* Delete all the siblings that were just added */
5391 /* success ... tell user about new route */
5392 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5396 /* send notification for routes that were added so that
5397 * the delete notifications sent by ip6_route_del are
5401 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5403 /* Delete routes that were already added */
5404 list_for_each_entry(nh, &rt6_nh_list, next) {
5407 ip6_route_del(&nh->r_cfg, extack);
5411 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5413 fib6_info_release(nh->fib6_info);
5414 list_del(&nh->next);
5421 static int ip6_route_multipath_del(struct fib6_config *cfg,
5422 struct netlink_ext_ack *extack)
5424 struct fib6_config r_cfg;
5425 struct rtnexthop *rtnh;
5431 remaining = cfg->fc_mp_len;
5432 rtnh = (struct rtnexthop *)cfg->fc_mp;
5434 /* Parse a Multipath Entry */
5435 while (rtnh_ok(rtnh, remaining)) {
5436 memcpy(&r_cfg, cfg, sizeof(*cfg));
5437 if (rtnh->rtnh_ifindex)
5438 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5440 attrlen = rtnh_attrlen(rtnh);
5442 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5444 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5446 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5453 r_cfg.fc_flags |= RTF_GATEWAY;
5456 err = ip6_route_del(&r_cfg, extack);
5461 rtnh = rtnh_next(rtnh, &remaining);
5467 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5468 struct netlink_ext_ack *extack)
5470 struct fib6_config cfg;
5473 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5478 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5479 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5484 return ip6_route_multipath_del(&cfg, extack);
5486 cfg.fc_delete_all_nh = 1;
5487 return ip6_route_del(&cfg, extack);
5491 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5492 struct netlink_ext_ack *extack)
5494 struct fib6_config cfg;
5497 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5501 if (cfg.fc_metric == 0)
5502 cfg.fc_metric = IP6_RT_PRIO_USER;
5505 return ip6_route_multipath_add(&cfg, extack);
5507 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5510 /* add the overhead of this fib6_nh to nexthop_len */
5511 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5513 int *nexthop_len = arg;
5515 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5516 + NLA_ALIGN(sizeof(struct rtnexthop))
5517 + nla_total_size(16); /* RTA_GATEWAY */
5519 if (nh->fib_nh_lws) {
5520 /* RTA_ENCAP_TYPE */
5521 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5523 *nexthop_len += nla_total_size(2);
5529 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5534 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5535 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5538 struct fib6_info *sibling, *next_sibling;
5539 struct fib6_nh *nh = f6i->fib6_nh;
5542 if (f6i->fib6_nsiblings) {
5543 rt6_nh_nlmsg_size(nh, &nexthop_len);
5545 list_for_each_entry_safe(sibling, next_sibling,
5546 &f6i->fib6_siblings, fib6_siblings) {
5547 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
5550 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5553 return NLMSG_ALIGN(sizeof(struct rtmsg))
5554 + nla_total_size(16) /* RTA_SRC */
5555 + nla_total_size(16) /* RTA_DST */
5556 + nla_total_size(16) /* RTA_GATEWAY */
5557 + nla_total_size(16) /* RTA_PREFSRC */
5558 + nla_total_size(4) /* RTA_TABLE */
5559 + nla_total_size(4) /* RTA_IIF */
5560 + nla_total_size(4) /* RTA_OIF */
5561 + nla_total_size(4) /* RTA_PRIORITY */
5562 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5563 + nla_total_size(sizeof(struct rta_cacheinfo))
5564 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5565 + nla_total_size(1) /* RTA_PREF */
5569 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5570 unsigned char *flags)
5572 if (nexthop_is_multipath(nh)) {
5575 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5577 goto nla_put_failure;
5579 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5580 goto nla_put_failure;
5582 nla_nest_end(skb, mp);
5584 struct fib6_nh *fib6_nh;
5586 fib6_nh = nexthop_fib6_nh(nh);
5587 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5589 goto nla_put_failure;
5598 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5599 struct fib6_info *rt, struct dst_entry *dst,
5600 struct in6_addr *dest, struct in6_addr *src,
5601 int iif, int type, u32 portid, u32 seq,
5604 struct rt6_info *rt6 = (struct rt6_info *)dst;
5605 struct rt6key *rt6_dst, *rt6_src;
5606 u32 *pmetrics, table, rt6_flags;
5607 unsigned char nh_flags = 0;
5608 struct nlmsghdr *nlh;
5612 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5617 rt6_dst = &rt6->rt6i_dst;
5618 rt6_src = &rt6->rt6i_src;
5619 rt6_flags = rt6->rt6i_flags;
5621 rt6_dst = &rt->fib6_dst;
5622 rt6_src = &rt->fib6_src;
5623 rt6_flags = rt->fib6_flags;
5626 rtm = nlmsg_data(nlh);
5627 rtm->rtm_family = AF_INET6;
5628 rtm->rtm_dst_len = rt6_dst->plen;
5629 rtm->rtm_src_len = rt6_src->plen;
5632 table = rt->fib6_table->tb6_id;
5634 table = RT6_TABLE_UNSPEC;
5635 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5636 if (nla_put_u32(skb, RTA_TABLE, table))
5637 goto nla_put_failure;
5639 rtm->rtm_type = rt->fib6_type;
5641 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5642 rtm->rtm_protocol = rt->fib6_protocol;
5644 if (rt6_flags & RTF_CACHE)
5645 rtm->rtm_flags |= RTM_F_CLONED;
5648 if (nla_put_in6_addr(skb, RTA_DST, dest))
5649 goto nla_put_failure;
5650 rtm->rtm_dst_len = 128;
5651 } else if (rtm->rtm_dst_len)
5652 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5653 goto nla_put_failure;
5654 #ifdef CONFIG_IPV6_SUBTREES
5656 if (nla_put_in6_addr(skb, RTA_SRC, src))
5657 goto nla_put_failure;
5658 rtm->rtm_src_len = 128;
5659 } else if (rtm->rtm_src_len &&
5660 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5661 goto nla_put_failure;
5664 #ifdef CONFIG_IPV6_MROUTE
5665 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5666 int err = ip6mr_get_route(net, skb, rtm, portid);
5671 goto nla_put_failure;
5674 if (nla_put_u32(skb, RTA_IIF, iif))
5675 goto nla_put_failure;
5677 struct in6_addr saddr_buf;
5678 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5679 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5680 goto nla_put_failure;
5683 if (rt->fib6_prefsrc.plen) {
5684 struct in6_addr saddr_buf;
5685 saddr_buf = rt->fib6_prefsrc.addr;
5686 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5687 goto nla_put_failure;
5690 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5691 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5692 goto nla_put_failure;
5694 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5695 goto nla_put_failure;
5697 /* For multipath routes, walk the siblings list and add
5698 * each as a nexthop within RTA_MULTIPATH.
5701 if (rt6_flags & RTF_GATEWAY &&
5702 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5703 goto nla_put_failure;
5705 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5706 goto nla_put_failure;
5708 if (dst->lwtstate &&
5709 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5710 goto nla_put_failure;
5711 } else if (rt->fib6_nsiblings) {
5712 struct fib6_info *sibling, *next_sibling;
5715 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5717 goto nla_put_failure;
5719 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5720 rt->fib6_nh->fib_nh_weight, AF_INET6,
5722 goto nla_put_failure;
5724 list_for_each_entry_safe(sibling, next_sibling,
5725 &rt->fib6_siblings, fib6_siblings) {
5726 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5727 sibling->fib6_nh->fib_nh_weight,
5729 goto nla_put_failure;
5732 nla_nest_end(skb, mp);
5733 } else if (rt->nh) {
5734 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5735 goto nla_put_failure;
5737 if (nexthop_is_blackhole(rt->nh))
5738 rtm->rtm_type = RTN_BLACKHOLE;
5740 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
5741 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5742 goto nla_put_failure;
5744 rtm->rtm_flags |= nh_flags;
5746 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5747 &nh_flags, false) < 0)
5748 goto nla_put_failure;
5750 rtm->rtm_flags |= nh_flags;
5753 if (rt6_flags & RTF_EXPIRES) {
5754 expires = dst ? dst->expires : rt->expires;
5759 if (READ_ONCE(rt->offload))
5760 rtm->rtm_flags |= RTM_F_OFFLOAD;
5761 if (READ_ONCE(rt->trap))
5762 rtm->rtm_flags |= RTM_F_TRAP;
5763 if (READ_ONCE(rt->offload_failed))
5764 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5767 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5768 goto nla_put_failure;
5770 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5771 goto nla_put_failure;
5774 nlmsg_end(skb, nlh);
5778 nlmsg_cancel(skb, nlh);
5782 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5784 const struct net_device *dev = arg;
5786 if (nh->fib_nh_dev == dev)
5792 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5793 const struct net_device *dev)
5796 struct net_device *_dev = (struct net_device *)dev;
5798 return !!nexthop_for_each_fib6_nh(f6i->nh,
5799 fib6_info_nh_uses_dev,
5803 if (f6i->fib6_nh->fib_nh_dev == dev)
5806 if (f6i->fib6_nsiblings) {
5807 struct fib6_info *sibling, *next_sibling;
5809 list_for_each_entry_safe(sibling, next_sibling,
5810 &f6i->fib6_siblings, fib6_siblings) {
5811 if (sibling->fib6_nh->fib_nh_dev == dev)
5819 struct fib6_nh_exception_dump_walker {
5820 struct rt6_rtnl_dump_arg *dump;
5821 struct fib6_info *rt;
5827 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5829 struct fib6_nh_exception_dump_walker *w = arg;
5830 struct rt6_rtnl_dump_arg *dump = w->dump;
5831 struct rt6_exception_bucket *bucket;
5832 struct rt6_exception *rt6_ex;
5835 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5839 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5840 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5846 /* Expiration of entries doesn't bump sernum, insertion
5847 * does. Removal is triggered by insertion, so we can
5848 * rely on the fact that if entries change between two
5849 * partial dumps, this node is scanned again completely,
5850 * see rt6_insert_exception() and fib6_dump_table().
5852 * Count expired entries we go through as handled
5853 * entries that we'll skip next time, in case of partial
5854 * node dump. Otherwise, if entries expire meanwhile,
5855 * we'll skip the wrong amount.
5857 if (rt6_check_expired(rt6_ex->rt6i)) {
5862 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5863 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5865 NETLINK_CB(dump->cb->skb).portid,
5866 dump->cb->nlh->nlmsg_seq, w->flags);
5878 /* Return -1 if done with node, number of handled routes on partial dump */
5879 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5881 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5882 struct fib_dump_filter *filter = &arg->filter;
5883 unsigned int flags = NLM_F_MULTI;
5884 struct net *net = arg->net;
5887 if (rt == net->ipv6.fib6_null_entry)
5890 if ((filter->flags & RTM_F_PREFIX) &&
5891 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5892 /* success since this is not a prefix route */
5895 if (filter->filter_set &&
5896 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5897 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5898 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5902 if (filter->filter_set ||
5903 !filter->dump_routes || !filter->dump_exceptions) {
5904 flags |= NLM_F_DUMP_FILTERED;
5907 if (filter->dump_routes) {
5911 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5913 NETLINK_CB(arg->cb->skb).portid,
5914 arg->cb->nlh->nlmsg_seq, flags)) {
5921 if (filter->dump_exceptions) {
5922 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5931 err = nexthop_for_each_fib6_nh(rt->nh,
5932 rt6_nh_dump_exceptions,
5935 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5940 return count + w.count;
5946 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5947 const struct nlmsghdr *nlh,
5949 struct netlink_ext_ack *extack)
5954 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5955 NL_SET_ERR_MSG_MOD(extack,
5956 "Invalid header for get route request");
5960 if (!netlink_strict_get_check(skb))
5961 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5962 rtm_ipv6_policy, extack);
5964 rtm = nlmsg_data(nlh);
5965 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5966 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5967 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5969 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5972 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5973 NL_SET_ERR_MSG_MOD(extack,
5974 "Invalid flags for get route request");
5978 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5979 rtm_ipv6_policy, extack);
5983 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5984 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5985 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5989 for (i = 0; i <= RTA_MAX; i++) {
6005 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
6013 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
6014 struct netlink_ext_ack *extack)
6016 struct net *net = sock_net(in_skb->sk);
6017 struct nlattr *tb[RTA_MAX+1];
6018 int err, iif = 0, oif = 0;
6019 struct fib6_info *from;
6020 struct dst_entry *dst;
6021 struct rt6_info *rt;
6022 struct sk_buff *skb;
6024 struct flowi6 fl6 = {};
6027 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
6032 rtm = nlmsg_data(nlh);
6033 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
6034 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
6037 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6040 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
6044 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6047 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
6051 iif = nla_get_u32(tb[RTA_IIF]);
6054 oif = nla_get_u32(tb[RTA_OIF]);
6057 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6060 fl6.flowi6_uid = make_kuid(current_user_ns(),
6061 nla_get_u32(tb[RTA_UID]));
6063 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6066 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6069 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6071 if (tb[RTA_IP_PROTO]) {
6072 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
6073 &fl6.flowi6_proto, AF_INET6,
6080 struct net_device *dev;
6085 dev = dev_get_by_index_rcu(net, iif);
6092 fl6.flowi6_iif = iif;
6094 if (!ipv6_addr_any(&fl6.saddr))
6095 flags |= RT6_LOOKUP_F_HAS_SADDR;
6097 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6101 fl6.flowi6_oif = oif;
6103 dst = ip6_route_output(net, NULL, &fl6);
6107 rt = container_of(dst, struct rt6_info, dst);
6108 if (rt->dst.error) {
6109 err = rt->dst.error;
6114 if (rt == net->ipv6.ip6_null_entry) {
6115 err = rt->dst.error;
6120 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
6127 skb_dst_set(skb, &rt->dst);
6130 from = rcu_dereference(rt->from);
6133 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6135 NETLINK_CB(in_skb).portid,
6138 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6139 &fl6.saddr, iif, RTM_NEWROUTE,
6140 NETLINK_CB(in_skb).portid,
6152 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6157 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6158 unsigned int nlm_flags)
6160 struct sk_buff *skb;
6161 struct net *net = info->nl_net;
6166 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6168 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6172 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6173 event, info->portid, seq, nlm_flags);
6175 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6176 WARN_ON(err == -EMSGSIZE);
6180 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6181 info->nlh, gfp_any());
6185 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6188 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6189 struct nl_info *info)
6191 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6192 struct sk_buff *skb;
6195 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6199 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6200 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6202 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6203 WARN_ON(err == -EMSGSIZE);
6207 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6208 info->nlh, gfp_any());
6212 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6215 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
6216 bool offload, bool trap, bool offload_failed)
6218 struct sk_buff *skb;
6221 if (READ_ONCE(f6i->offload) == offload &&
6222 READ_ONCE(f6i->trap) == trap &&
6223 READ_ONCE(f6i->offload_failed) == offload_failed)
6226 WRITE_ONCE(f6i->offload, offload);
6227 WRITE_ONCE(f6i->trap, trap);
6229 /* 2 means send notifications only if offload_failed was changed. */
6230 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6231 READ_ONCE(f6i->offload_failed) == offload_failed)
6234 WRITE_ONCE(f6i->offload_failed, offload_failed);
6236 if (!rcu_access_pointer(f6i->fib6_node))
6237 /* The route was removed from the tree, do not send
6242 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6245 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6251 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6254 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6255 WARN_ON(err == -EMSGSIZE);
6260 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6264 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6266 EXPORT_SYMBOL(fib6_info_hw_flags_set);
6268 static int ip6_route_dev_notify(struct notifier_block *this,
6269 unsigned long event, void *ptr)
6271 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6272 struct net *net = dev_net(dev);
6274 if (!(dev->flags & IFF_LOOPBACK))
6277 if (event == NETDEV_REGISTER) {
6278 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6279 net->ipv6.ip6_null_entry->dst.dev = dev;
6280 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6281 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6282 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6283 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6284 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6285 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6287 } else if (event == NETDEV_UNREGISTER &&
6288 dev->reg_state != NETREG_UNREGISTERED) {
6289 /* NETDEV_UNREGISTER could be fired for multiple times by
6290 * netdev_wait_allrefs(). Make sure we only call this once.
6292 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6293 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6294 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6295 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6306 #ifdef CONFIG_PROC_FS
6307 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6309 struct net *net = (struct net *)seq->private;
6310 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6311 net->ipv6.rt6_stats->fib_nodes,
6312 net->ipv6.rt6_stats->fib_route_nodes,
6313 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6314 net->ipv6.rt6_stats->fib_rt_entries,
6315 net->ipv6.rt6_stats->fib_rt_cache,
6316 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6317 net->ipv6.rt6_stats->fib_discarded_routes);
6321 #endif /* CONFIG_PROC_FS */
6323 #ifdef CONFIG_SYSCTL
6325 static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6326 void *buffer, size_t *lenp, loff_t *ppos)
6334 net = (struct net *)ctl->extra1;
6335 delay = net->ipv6.sysctl.flush_delay;
6336 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6340 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6344 static struct ctl_table ipv6_route_table_template[] = {
6346 .procname = "max_size",
6347 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6348 .maxlen = sizeof(int),
6350 .proc_handler = proc_dointvec,
6353 .procname = "gc_thresh",
6354 .data = &ip6_dst_ops_template.gc_thresh,
6355 .maxlen = sizeof(int),
6357 .proc_handler = proc_dointvec,
6360 .procname = "flush",
6361 .data = &init_net.ipv6.sysctl.flush_delay,
6362 .maxlen = sizeof(int),
6364 .proc_handler = ipv6_sysctl_rtcache_flush
6367 .procname = "gc_min_interval",
6368 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6369 .maxlen = sizeof(int),
6371 .proc_handler = proc_dointvec_jiffies,
6374 .procname = "gc_timeout",
6375 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6376 .maxlen = sizeof(int),
6378 .proc_handler = proc_dointvec_jiffies,
6381 .procname = "gc_interval",
6382 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6383 .maxlen = sizeof(int),
6385 .proc_handler = proc_dointvec_jiffies,
6388 .procname = "gc_elasticity",
6389 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6390 .maxlen = sizeof(int),
6392 .proc_handler = proc_dointvec,
6395 .procname = "mtu_expires",
6396 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6397 .maxlen = sizeof(int),
6399 .proc_handler = proc_dointvec_jiffies,
6402 .procname = "min_adv_mss",
6403 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6404 .maxlen = sizeof(int),
6406 .proc_handler = proc_dointvec,
6409 .procname = "gc_min_interval_ms",
6410 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6411 .maxlen = sizeof(int),
6413 .proc_handler = proc_dointvec_ms_jiffies,
6416 .procname = "skip_notify_on_dev_down",
6417 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6418 .maxlen = sizeof(u8),
6420 .proc_handler = proc_dou8vec_minmax,
6421 .extra1 = SYSCTL_ZERO,
6422 .extra2 = SYSCTL_ONE,
6427 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6429 struct ctl_table *table;
6431 table = kmemdup(ipv6_route_table_template,
6432 sizeof(ipv6_route_table_template),
6436 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
6437 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6438 table[2].data = &net->ipv6.sysctl.flush_delay;
6439 table[2].extra1 = net;
6440 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6441 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6442 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6443 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6444 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6445 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6446 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6447 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6449 /* Don't export sysctls to unprivileged users */
6450 if (net->user_ns != &init_user_ns)
6451 table[1].procname = NULL;
6457 size_t ipv6_route_sysctl_table_size(struct net *net)
6459 /* Don't export sysctls to unprivileged users */
6460 if (net->user_ns != &init_user_ns)
6463 return ARRAY_SIZE(ipv6_route_table_template);
6467 static int __net_init ip6_route_net_init(struct net *net)
6471 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6472 sizeof(net->ipv6.ip6_dst_ops));
6474 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6475 goto out_ip6_dst_ops;
6477 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6478 if (!net->ipv6.fib6_null_entry)
6479 goto out_ip6_dst_entries;
6480 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6481 sizeof(*net->ipv6.fib6_null_entry));
6483 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6484 sizeof(*net->ipv6.ip6_null_entry),
6486 if (!net->ipv6.ip6_null_entry)
6487 goto out_fib6_null_entry;
6488 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6489 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6490 ip6_template_metrics, true);
6491 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
6493 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6494 net->ipv6.fib6_has_custom_rules = false;
6495 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6496 sizeof(*net->ipv6.ip6_prohibit_entry),
6498 if (!net->ipv6.ip6_prohibit_entry)
6499 goto out_ip6_null_entry;
6500 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6501 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6502 ip6_template_metrics, true);
6503 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
6505 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6506 sizeof(*net->ipv6.ip6_blk_hole_entry),
6508 if (!net->ipv6.ip6_blk_hole_entry)
6509 goto out_ip6_prohibit_entry;
6510 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6511 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6512 ip6_template_metrics, true);
6513 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
6514 #ifdef CONFIG_IPV6_SUBTREES
6515 net->ipv6.fib6_routes_require_src = 0;
6519 net->ipv6.sysctl.flush_delay = 0;
6520 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
6521 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6522 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6523 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6524 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6525 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6526 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6527 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6529 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
6535 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6536 out_ip6_prohibit_entry:
6537 kfree(net->ipv6.ip6_prohibit_entry);
6539 kfree(net->ipv6.ip6_null_entry);
6541 out_fib6_null_entry:
6542 kfree(net->ipv6.fib6_null_entry);
6543 out_ip6_dst_entries:
6544 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6549 static void __net_exit ip6_route_net_exit(struct net *net)
6551 kfree(net->ipv6.fib6_null_entry);
6552 kfree(net->ipv6.ip6_null_entry);
6553 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6554 kfree(net->ipv6.ip6_prohibit_entry);
6555 kfree(net->ipv6.ip6_blk_hole_entry);
6557 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6560 static int __net_init ip6_route_net_init_late(struct net *net)
6562 #ifdef CONFIG_PROC_FS
6563 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6564 &ipv6_route_seq_ops,
6565 sizeof(struct ipv6_route_iter)))
6568 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6569 rt6_stats_seq_show, NULL)) {
6570 remove_proc_entry("ipv6_route", net->proc_net);
6577 static void __net_exit ip6_route_net_exit_late(struct net *net)
6579 #ifdef CONFIG_PROC_FS
6580 remove_proc_entry("ipv6_route", net->proc_net);
6581 remove_proc_entry("rt6_stats", net->proc_net);
6585 static struct pernet_operations ip6_route_net_ops = {
6586 .init = ip6_route_net_init,
6587 .exit = ip6_route_net_exit,
6590 static int __net_init ipv6_inetpeer_init(struct net *net)
6592 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6596 inet_peer_base_init(bp);
6597 net->ipv6.peers = bp;
6601 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6603 struct inet_peer_base *bp = net->ipv6.peers;
6605 net->ipv6.peers = NULL;
6606 inetpeer_invalidate_tree(bp);
6610 static struct pernet_operations ipv6_inetpeer_ops = {
6611 .init = ipv6_inetpeer_init,
6612 .exit = ipv6_inetpeer_exit,
6615 static struct pernet_operations ip6_route_net_late_ops = {
6616 .init = ip6_route_net_init_late,
6617 .exit = ip6_route_net_exit_late,
6620 static struct notifier_block ip6_route_dev_notifier = {
6621 .notifier_call = ip6_route_dev_notify,
6622 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6625 void __init ip6_route_init_special_entries(void)
6627 /* Registering of the loopback is done before this portion of code,
6628 * the loopback reference in rt6_info will not be taken, do it
6629 * manually for init_net */
6630 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6631 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6632 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6633 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6634 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6635 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6636 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6637 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6641 #if IS_BUILTIN(CONFIG_IPV6)
6642 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6643 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6645 BTF_ID_LIST(btf_fib6_info_id)
6646 BTF_ID(struct, fib6_info)
6648 static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6649 .seq_ops = &ipv6_route_seq_ops,
6650 .init_seq_private = bpf_iter_init_seq_net,
6651 .fini_seq_private = bpf_iter_fini_seq_net,
6652 .seq_priv_size = sizeof(struct ipv6_route_iter),
6655 static struct bpf_iter_reg ipv6_route_reg_info = {
6656 .target = "ipv6_route",
6657 .ctx_arg_info_size = 1,
6659 { offsetof(struct bpf_iter__ipv6_route, rt),
6660 PTR_TO_BTF_ID_OR_NULL },
6662 .seq_info = &ipv6_route_seq_info,
6665 static int __init bpf_iter_register(void)
6667 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
6668 return bpf_iter_reg_target(&ipv6_route_reg_info);
6671 static void bpf_iter_unregister(void)
6673 bpf_iter_unreg_target(&ipv6_route_reg_info);
6678 int __init ip6_route_init(void)
6684 ip6_dst_ops_template.kmem_cachep =
6685 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6686 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
6687 if (!ip6_dst_ops_template.kmem_cachep)
6690 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6692 goto out_kmem_cache;
6694 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6696 goto out_dst_entries;
6698 ret = register_pernet_subsys(&ip6_route_net_ops);
6700 goto out_register_inetpeer;
6702 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6706 goto out_register_subsys;
6712 ret = fib6_rules_init();
6716 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6718 goto fib6_rules_init;
6720 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6721 inet6_rtm_newroute, NULL, 0);
6723 goto out_register_late_subsys;
6725 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6726 inet6_rtm_delroute, NULL, 0);
6728 goto out_register_late_subsys;
6730 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6731 inet6_rtm_getroute, NULL,
6732 RTNL_FLAG_DOIT_UNLOCKED);
6734 goto out_register_late_subsys;
6736 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6738 goto out_register_late_subsys;
6740 #if IS_BUILTIN(CONFIG_IPV6)
6741 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6742 ret = bpf_iter_register();
6744 goto out_register_late_subsys;
6748 for_each_possible_cpu(cpu) {
6749 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6751 INIT_LIST_HEAD(&ul->head);
6752 INIT_LIST_HEAD(&ul->quarantine);
6753 spin_lock_init(&ul->lock);
6759 out_register_late_subsys:
6760 rtnl_unregister_all(PF_INET6);
6761 unregister_pernet_subsys(&ip6_route_net_late_ops);
6763 fib6_rules_cleanup();
6768 out_register_subsys:
6769 unregister_pernet_subsys(&ip6_route_net_ops);
6770 out_register_inetpeer:
6771 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6773 dst_entries_destroy(&ip6_dst_blackhole_ops);
6775 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6779 void ip6_route_cleanup(void)
6781 #if IS_BUILTIN(CONFIG_IPV6)
6782 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6783 bpf_iter_unregister();
6786 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6787 unregister_pernet_subsys(&ip6_route_net_late_ops);
6788 fib6_rules_cleanup();
6791 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6792 unregister_pernet_subsys(&ip6_route_net_ops);
6793 dst_entries_destroy(&ip6_dst_blackhole_ops);
6794 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);