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 <net/net_namespace.h>
47 #include <net/ip6_fib.h>
48 #include <net/ip6_route.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
52 #include <linux/rtnetlink.h>
54 #include <net/dst_metadata.h>
56 #include <net/netevent.h>
57 #include <net/netlink.h>
59 #include <net/lwtunnel.h>
60 #include <net/ip_tunnels.h>
61 #include <net/l3mdev.h>
63 #include <linux/uaccess.h>
66 #include <linux/sysctl.h>
69 static int ip6_rt_type_to_error(u8 fib6_type);
71 #define CREATE_TRACE_POINTS
72 #include <trace/events/fib6.h>
73 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74 #undef CREATE_TRACE_POINTS
77 RT6_NUD_FAIL_HARD = -3,
78 RT6_NUD_FAIL_PROBE = -2,
79 RT6_NUD_FAIL_DO_RR = -1,
83 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
84 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
85 static unsigned int ip6_mtu(const struct dst_entry *dst);
86 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87 static void ip6_dst_destroy(struct dst_entry *);
88 static void ip6_dst_ifdown(struct dst_entry *,
89 struct net_device *dev, int how);
90 static int ip6_dst_gc(struct dst_ops *ops);
92 static int ip6_pkt_discard(struct sk_buff *skb);
93 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
94 static int ip6_pkt_prohibit(struct sk_buff *skb);
95 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
96 static void ip6_link_failure(struct sk_buff *skb);
97 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
98 struct sk_buff *skb, u32 mtu);
99 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 struct sk_buff *skb);
101 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
104 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
105 struct fib6_info *rt, struct dst_entry *dst,
106 struct in6_addr *dest, struct in6_addr *src,
107 int iif, int type, u32 portid, u32 seq,
109 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
110 const struct in6_addr *daddr,
111 const struct in6_addr *saddr);
113 #ifdef CONFIG_IPV6_ROUTE_INFO
114 static struct fib6_info *rt6_add_route_info(struct net *net,
115 const struct in6_addr *prefix, int prefixlen,
116 const struct in6_addr *gwaddr,
117 struct net_device *dev,
119 static struct fib6_info *rt6_get_route_info(struct net *net,
120 const struct in6_addr *prefix, int prefixlen,
121 const struct in6_addr *gwaddr,
122 struct net_device *dev);
125 struct uncached_list {
127 struct list_head head;
130 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
132 void rt6_uncached_list_add(struct rt6_info *rt)
134 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
136 rt->rt6i_uncached_list = ul;
138 spin_lock_bh(&ul->lock);
139 list_add_tail(&rt->rt6i_uncached, &ul->head);
140 spin_unlock_bh(&ul->lock);
143 void rt6_uncached_list_del(struct rt6_info *rt)
145 if (!list_empty(&rt->rt6i_uncached)) {
146 struct uncached_list *ul = rt->rt6i_uncached_list;
147 struct net *net = dev_net(rt->dst.dev);
149 spin_lock_bh(&ul->lock);
150 list_del(&rt->rt6i_uncached);
151 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
152 spin_unlock_bh(&ul->lock);
156 static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
158 struct net_device *loopback_dev = net->loopback_dev;
161 if (dev == loopback_dev)
164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
168 spin_lock_bh(&ul->lock);
169 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
170 struct inet6_dev *rt_idev = rt->rt6i_idev;
171 struct net_device *rt_dev = rt->dst.dev;
173 if (rt_idev->dev == dev) {
174 rt->rt6i_idev = in6_dev_get(loopback_dev);
175 in6_dev_put(rt_idev);
179 rt->dst.dev = blackhole_netdev;
180 dev_hold(rt->dst.dev);
184 spin_unlock_bh(&ul->lock);
188 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
192 if (!ipv6_addr_any(p))
193 return (const void *) p;
195 return &ipv6_hdr(skb)->daddr;
199 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
200 struct net_device *dev,
206 daddr = choose_neigh_daddr(gw, skb, daddr);
207 n = __ipv6_neigh_lookup(dev, daddr);
211 n = neigh_create(&nd_tbl, daddr, dev);
212 return IS_ERR(n) ? NULL : n;
215 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
219 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
222 dst->dev, skb, daddr);
225 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
227 struct net_device *dev = dst->dev;
228 struct rt6_info *rt = (struct rt6_info *)dst;
230 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
233 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
235 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
237 __ipv6_confirm_neigh(dev, daddr);
240 static struct dst_ops ip6_dst_ops_template = {
244 .check = ip6_dst_check,
245 .default_advmss = ip6_default_advmss,
247 .cow_metrics = dst_cow_metrics_generic,
248 .destroy = ip6_dst_destroy,
249 .ifdown = ip6_dst_ifdown,
250 .negative_advice = ip6_negative_advice,
251 .link_failure = ip6_link_failure,
252 .update_pmtu = ip6_rt_update_pmtu,
253 .redirect = rt6_do_redirect,
254 .local_out = __ip6_local_out,
255 .neigh_lookup = ip6_dst_neigh_lookup,
256 .confirm_neigh = ip6_confirm_neigh,
259 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
261 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263 return mtu ? : dst->dev->mtu;
266 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
267 struct sk_buff *skb, u32 mtu)
271 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
276 static struct dst_ops ip6_dst_blackhole_ops = {
278 .destroy = ip6_dst_destroy,
279 .check = ip6_dst_check,
280 .mtu = ip6_blackhole_mtu,
281 .default_advmss = ip6_default_advmss,
282 .update_pmtu = ip6_rt_blackhole_update_pmtu,
283 .redirect = ip6_rt_blackhole_redirect,
284 .cow_metrics = dst_cow_metrics_generic,
285 .neigh_lookup = ip6_dst_neigh_lookup,
288 static const u32 ip6_template_metrics[RTAX_MAX] = {
289 [RTAX_HOPLIMIT - 1] = 0,
292 static const struct fib6_info fib6_null_entry_template = {
293 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
294 .fib6_protocol = RTPROT_KERNEL,
295 .fib6_metric = ~(u32)0,
296 .fib6_ref = REFCOUNT_INIT(1),
297 .fib6_type = RTN_UNREACHABLE,
298 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
301 static const struct rt6_info ip6_null_entry_template = {
303 .__refcnt = ATOMIC_INIT(1),
305 .obsolete = DST_OBSOLETE_FORCE_CHK,
306 .error = -ENETUNREACH,
307 .input = ip6_pkt_discard,
308 .output = ip6_pkt_discard_out,
310 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
313 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
315 static const struct rt6_info ip6_prohibit_entry_template = {
317 .__refcnt = ATOMIC_INIT(1),
319 .obsolete = DST_OBSOLETE_FORCE_CHK,
321 .input = ip6_pkt_prohibit,
322 .output = ip6_pkt_prohibit_out,
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
327 static const struct rt6_info ip6_blk_hole_entry_template = {
329 .__refcnt = ATOMIC_INIT(1),
331 .obsolete = DST_OBSOLETE_FORCE_CHK,
333 .input = dst_discard,
334 .output = dst_discard_out,
336 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
341 static void rt6_info_init(struct rt6_info *rt)
343 struct dst_entry *dst = &rt->dst;
345 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
346 INIT_LIST_HEAD(&rt->rt6i_uncached);
349 /* allocate dst with ip6_dst_ops */
350 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
354 1, DST_OBSOLETE_FORCE_CHK, flags);
358 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
363 EXPORT_SYMBOL(ip6_dst_alloc);
365 static void ip6_dst_destroy(struct dst_entry *dst)
367 struct rt6_info *rt = (struct rt6_info *)dst;
368 struct fib6_info *from;
369 struct inet6_dev *idev;
371 ip_dst_metrics_put(dst);
372 rt6_uncached_list_del(rt);
374 idev = rt->rt6i_idev;
376 rt->rt6i_idev = NULL;
380 from = xchg((__force struct fib6_info **)&rt->from, NULL);
381 fib6_info_release(from);
384 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
387 struct rt6_info *rt = (struct rt6_info *)dst;
388 struct inet6_dev *idev = rt->rt6i_idev;
389 struct net_device *loopback_dev =
390 dev_net(dev)->loopback_dev;
392 if (idev && idev->dev != loopback_dev) {
393 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
395 rt->rt6i_idev = loopback_idev;
401 static bool __rt6_check_expired(const struct rt6_info *rt)
403 if (rt->rt6i_flags & RTF_EXPIRES)
404 return time_after(jiffies, rt->dst.expires);
409 static bool rt6_check_expired(const struct rt6_info *rt)
411 struct fib6_info *from;
413 from = rcu_dereference(rt->from);
415 if (rt->rt6i_flags & RTF_EXPIRES) {
416 if (time_after(jiffies, rt->dst.expires))
419 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
420 fib6_check_expired(from);
425 void fib6_select_path(const struct net *net, struct fib6_result *res,
426 struct flowi6 *fl6, int oif, bool have_oif_match,
427 const struct sk_buff *skb, int strict)
429 struct fib6_info *sibling, *next_sibling;
430 struct fib6_info *match = res->f6i;
432 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
435 /* We might have already computed the hash for ICMPv6 errors. In such
436 * case it will always be non-zero. Otherwise now is the time to do it.
439 (!match->nh || nexthop_is_multipath(match->nh)))
440 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
442 if (unlikely(match->nh)) {
443 nexthop_path_fib6_result(res, fl6->mp_hash);
447 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
450 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
452 const struct fib6_nh *nh = sibling->fib6_nh;
455 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
456 if (fl6->mp_hash > nh_upper_bound)
458 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
466 res->nh = match->fib6_nh;
470 * Route lookup. rcu_read_lock() should be held.
473 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
474 const struct in6_addr *saddr, int oif, int flags)
476 const struct net_device *dev;
478 if (nh->fib_nh_flags & RTNH_F_DEAD)
481 dev = nh->fib_nh_dev;
483 if (dev->ifindex == oif)
486 if (ipv6_chk_addr(net, saddr, dev,
487 flags & RT6_LOOKUP_F_IFACE))
494 struct fib6_nh_dm_arg {
496 const struct in6_addr *saddr;
502 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
504 struct fib6_nh_dm_arg *arg = _arg;
507 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
511 /* returns fib6_nh from nexthop or NULL */
512 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
513 struct fib6_result *res,
514 const struct in6_addr *saddr,
517 struct fib6_nh_dm_arg arg = {
524 if (nexthop_is_blackhole(nh))
527 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
533 static void rt6_device_match(struct net *net, struct fib6_result *res,
534 const struct in6_addr *saddr, int oif, int flags)
536 struct fib6_info *f6i = res->f6i;
537 struct fib6_info *spf6i;
540 if (!oif && ipv6_addr_any(saddr)) {
541 if (unlikely(f6i->nh)) {
542 nh = nexthop_fib6_nh(f6i->nh);
543 if (nexthop_is_blackhole(f6i->nh))
548 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
552 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
553 bool matched = false;
555 if (unlikely(spf6i->nh)) {
556 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
562 if (__rt6_device_match(net, nh, saddr, oif, flags))
571 if (oif && flags & RT6_LOOKUP_F_IFACE) {
572 res->f6i = net->ipv6.fib6_null_entry;
573 nh = res->f6i->fib6_nh;
577 if (unlikely(f6i->nh)) {
578 nh = nexthop_fib6_nh(f6i->nh);
579 if (nexthop_is_blackhole(f6i->nh))
585 if (nh->fib_nh_flags & RTNH_F_DEAD) {
586 res->f6i = net->ipv6.fib6_null_entry;
587 nh = res->f6i->fib6_nh;
591 res->fib6_type = res->f6i->fib6_type;
592 res->fib6_flags = res->f6i->fib6_flags;
596 res->fib6_flags |= RTF_REJECT;
597 res->fib6_type = RTN_BLACKHOLE;
601 #ifdef CONFIG_IPV6_ROUTER_PREF
602 struct __rt6_probe_work {
603 struct work_struct work;
604 struct in6_addr target;
605 struct net_device *dev;
608 static void rt6_probe_deferred(struct work_struct *w)
610 struct in6_addr mcaddr;
611 struct __rt6_probe_work *work =
612 container_of(w, struct __rt6_probe_work, work);
614 addrconf_addr_solict_mult(&work->target, &mcaddr);
615 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
620 static void rt6_probe(struct fib6_nh *fib6_nh)
622 struct __rt6_probe_work *work = NULL;
623 const struct in6_addr *nh_gw;
624 unsigned long last_probe;
625 struct neighbour *neigh;
626 struct net_device *dev;
627 struct inet6_dev *idev;
630 * Okay, this does not seem to be appropriate
631 * for now, however, we need to check if it
632 * is really so; aka Router Reachability Probing.
634 * Router Reachability Probe MUST be rate-limited
635 * to no more than one per minute.
637 if (!fib6_nh->fib_nh_gw_family)
640 nh_gw = &fib6_nh->fib_nh_gw6;
641 dev = fib6_nh->fib_nh_dev;
643 last_probe = READ_ONCE(fib6_nh->last_probe);
644 idev = __in6_dev_get(dev);
645 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
647 if (neigh->nud_state & NUD_VALID)
650 write_lock(&neigh->lock);
651 if (!(neigh->nud_state & NUD_VALID) &&
653 neigh->updated + idev->cnf.rtr_probe_interval)) {
654 work = kmalloc(sizeof(*work), GFP_ATOMIC);
656 __neigh_set_probe_once(neigh);
658 write_unlock(&neigh->lock);
659 } else if (time_after(jiffies, last_probe +
660 idev->cnf.rtr_probe_interval)) {
661 work = kmalloc(sizeof(*work), GFP_ATOMIC);
664 if (!work || cmpxchg(&fib6_nh->last_probe,
665 last_probe, jiffies) != last_probe) {
668 INIT_WORK(&work->work, rt6_probe_deferred);
669 work->target = *nh_gw;
672 schedule_work(&work->work);
676 rcu_read_unlock_bh();
679 static inline void rt6_probe(struct fib6_nh *fib6_nh)
685 * Default Router Selection (RFC 2461 6.3.6)
687 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
689 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
690 struct neighbour *neigh;
693 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
694 &fib6_nh->fib_nh_gw6);
696 read_lock(&neigh->lock);
697 if (neigh->nud_state & NUD_VALID)
698 ret = RT6_NUD_SUCCEED;
699 #ifdef CONFIG_IPV6_ROUTER_PREF
700 else if (!(neigh->nud_state & NUD_FAILED))
701 ret = RT6_NUD_SUCCEED;
703 ret = RT6_NUD_FAIL_PROBE;
705 read_unlock(&neigh->lock);
707 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
708 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
710 rcu_read_unlock_bh();
715 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
720 if (!oif || nh->fib_nh_dev->ifindex == oif)
723 if (!m && (strict & RT6_LOOKUP_F_IFACE))
724 return RT6_NUD_FAIL_HARD;
725 #ifdef CONFIG_IPV6_ROUTER_PREF
726 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
728 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
729 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
730 int n = rt6_check_neigh(nh);
737 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
738 int oif, int strict, int *mpri, bool *do_rr)
740 bool match_do_rr = false;
744 if (nh->fib_nh_flags & RTNH_F_DEAD)
747 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
748 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
749 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
752 m = rt6_score_route(nh, fib6_flags, oif, strict);
753 if (m == RT6_NUD_FAIL_DO_RR) {
755 m = 0; /* lowest valid score */
756 } else if (m == RT6_NUD_FAIL_HARD) {
760 if (strict & RT6_LOOKUP_F_REACHABLE)
763 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
765 *do_rr = match_do_rr;
773 struct fib6_nh_frl_arg {
782 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
784 struct fib6_nh_frl_arg *arg = _arg;
787 return find_match(nh, arg->flags, arg->oif, arg->strict,
788 arg->mpri, arg->do_rr);
791 static void __find_rr_leaf(struct fib6_info *f6i_start,
792 struct fib6_info *nomatch, u32 metric,
793 struct fib6_result *res, struct fib6_info **cont,
794 int oif, int strict, bool *do_rr, int *mpri)
796 struct fib6_info *f6i;
798 for (f6i = f6i_start;
799 f6i && f6i != nomatch;
800 f6i = rcu_dereference(f6i->fib6_next)) {
801 bool matched = false;
804 if (cont && f6i->fib6_metric != metric) {
809 if (fib6_check_expired(f6i))
812 if (unlikely(f6i->nh)) {
813 struct fib6_nh_frl_arg arg = {
814 .flags = f6i->fib6_flags,
821 if (nexthop_is_blackhole(f6i->nh)) {
822 res->fib6_flags = RTF_REJECT;
823 res->fib6_type = RTN_BLACKHOLE;
825 res->nh = nexthop_fib6_nh(f6i->nh);
828 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
835 if (find_match(nh, f6i->fib6_flags, oif, strict,
842 res->fib6_flags = f6i->fib6_flags;
843 res->fib6_type = f6i->fib6_type;
848 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
849 struct fib6_info *rr_head, int oif, int strict,
850 bool *do_rr, struct fib6_result *res)
852 u32 metric = rr_head->fib6_metric;
853 struct fib6_info *cont = NULL;
856 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
857 oif, strict, do_rr, &mpri);
859 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
860 oif, strict, do_rr, &mpri);
862 if (res->f6i || !cont)
865 __find_rr_leaf(cont, NULL, metric, res, NULL,
866 oif, strict, do_rr, &mpri);
869 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
870 struct fib6_result *res, int strict)
872 struct fib6_info *leaf = rcu_dereference(fn->leaf);
873 struct fib6_info *rt0;
877 /* make sure this function or its helpers sets f6i */
880 if (!leaf || leaf == net->ipv6.fib6_null_entry)
883 rt0 = rcu_dereference(fn->rr_ptr);
887 /* Double check to make sure fn is not an intermediate node
888 * and fn->leaf does not points to its child's leaf
889 * (This might happen if all routes under fn are deleted from
890 * the tree and fib6_repair_tree() is called on the node.)
892 key_plen = rt0->fib6_dst.plen;
893 #ifdef CONFIG_IPV6_SUBTREES
894 if (rt0->fib6_src.plen)
895 key_plen = rt0->fib6_src.plen;
897 if (fn->fn_bit != key_plen)
900 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
902 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
904 /* no entries matched; do round-robin */
905 if (!next || next->fib6_metric != rt0->fib6_metric)
909 spin_lock_bh(&leaf->fib6_table->tb6_lock);
910 /* make sure next is not being deleted from the tree */
912 rcu_assign_pointer(fn->rr_ptr, next);
913 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
919 res->f6i = net->ipv6.fib6_null_entry;
920 res->nh = res->f6i->fib6_nh;
921 res->fib6_flags = res->f6i->fib6_flags;
922 res->fib6_type = res->f6i->fib6_type;
926 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
928 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
929 res->nh->fib_nh_gw_family;
932 #ifdef CONFIG_IPV6_ROUTE_INFO
933 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
934 const struct in6_addr *gwaddr)
936 struct net *net = dev_net(dev);
937 struct route_info *rinfo = (struct route_info *) opt;
938 struct in6_addr prefix_buf, *prefix;
940 unsigned long lifetime;
941 struct fib6_info *rt;
943 if (len < sizeof(struct route_info)) {
947 /* Sanity check for prefix_len and length */
948 if (rinfo->length > 3) {
950 } else if (rinfo->prefix_len > 128) {
952 } else if (rinfo->prefix_len > 64) {
953 if (rinfo->length < 2) {
956 } else if (rinfo->prefix_len > 0) {
957 if (rinfo->length < 1) {
962 pref = rinfo->route_pref;
963 if (pref == ICMPV6_ROUTER_PREF_INVALID)
966 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
968 if (rinfo->length == 3)
969 prefix = (struct in6_addr *)rinfo->prefix;
971 /* this function is safe */
972 ipv6_addr_prefix(&prefix_buf,
973 (struct in6_addr *)rinfo->prefix,
975 prefix = &prefix_buf;
978 if (rinfo->prefix_len == 0)
979 rt = rt6_get_dflt_router(net, gwaddr, dev);
981 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
984 if (rt && !lifetime) {
990 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993 rt->fib6_flags = RTF_ROUTEINFO |
994 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
997 if (!addrconf_finite_timeout(lifetime))
998 fib6_clean_expires(rt);
1000 fib6_set_expires(rt, jiffies + HZ * lifetime);
1002 fib6_info_release(rt);
1009 * Misc support functions
1012 /* called with rcu_lock held */
1013 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1015 struct net_device *dev = res->nh->fib_nh_dev;
1017 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1018 /* for copies of local routes, dst->dev needs to be the
1019 * device if it is a master device, the master device if
1020 * device is enslaved, and the loopback as the default
1022 if (netif_is_l3_slave(dev) &&
1023 !rt6_need_strict(&res->f6i->fib6_dst.addr))
1024 dev = l3mdev_master_dev_rcu(dev);
1025 else if (!netif_is_l3_master(dev))
1026 dev = dev_net(dev)->loopback_dev;
1027 /* last case is netif_is_l3_master(dev) is true in which
1028 * case we want dev returned to be dev
1035 static const int fib6_prop[RTN_MAX + 1] = {
1039 [RTN_BROADCAST] = 0,
1041 [RTN_MULTICAST] = 0,
1042 [RTN_BLACKHOLE] = -EINVAL,
1043 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1044 [RTN_PROHIBIT] = -EACCES,
1045 [RTN_THROW] = -EAGAIN,
1046 [RTN_NAT] = -EINVAL,
1047 [RTN_XRESOLVE] = -EINVAL,
1050 static int ip6_rt_type_to_error(u8 fib6_type)
1052 return fib6_prop[fib6_type];
1055 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1057 unsigned short flags = 0;
1059 if (rt->dst_nocount)
1060 flags |= DST_NOCOUNT;
1061 if (rt->dst_nopolicy)
1062 flags |= DST_NOPOLICY;
1069 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1071 rt->dst.error = ip6_rt_type_to_error(fib6_type);
1073 switch (fib6_type) {
1075 rt->dst.output = dst_discard_out;
1076 rt->dst.input = dst_discard;
1079 rt->dst.output = ip6_pkt_prohibit_out;
1080 rt->dst.input = ip6_pkt_prohibit;
1083 case RTN_UNREACHABLE:
1085 rt->dst.output = ip6_pkt_discard_out;
1086 rt->dst.input = ip6_pkt_discard;
1091 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1093 struct fib6_info *f6i = res->f6i;
1095 if (res->fib6_flags & RTF_REJECT) {
1096 ip6_rt_init_dst_reject(rt, res->fib6_type);
1101 rt->dst.output = ip6_output;
1103 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1104 rt->dst.input = ip6_input;
1105 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1106 rt->dst.input = ip6_mc_input;
1108 rt->dst.input = ip6_forward;
1111 if (res->nh->fib_nh_lws) {
1112 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1113 lwtunnel_set_redirect(&rt->dst);
1116 rt->dst.lastuse = jiffies;
1119 /* Caller must already hold reference to @from */
1120 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1122 rt->rt6i_flags &= ~RTF_EXPIRES;
1123 rcu_assign_pointer(rt->from, from);
1124 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1127 /* Caller must already hold reference to f6i in result */
1128 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1130 const struct fib6_nh *nh = res->nh;
1131 const struct net_device *dev = nh->fib_nh_dev;
1132 struct fib6_info *f6i = res->f6i;
1134 ip6_rt_init_dst(rt, res);
1136 rt->rt6i_dst = f6i->fib6_dst;
1137 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1138 rt->rt6i_flags = res->fib6_flags;
1139 if (nh->fib_nh_gw_family) {
1140 rt->rt6i_gateway = nh->fib_nh_gw6;
1141 rt->rt6i_flags |= RTF_GATEWAY;
1143 rt6_set_from(rt, f6i);
1144 #ifdef CONFIG_IPV6_SUBTREES
1145 rt->rt6i_src = f6i->fib6_src;
1149 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1150 struct in6_addr *saddr)
1152 struct fib6_node *pn, *sn;
1154 if (fn->fn_flags & RTN_TL_ROOT)
1156 pn = rcu_dereference(fn->parent);
1157 sn = FIB6_SUBTREE(pn);
1159 fn = fib6_node_lookup(sn, NULL, saddr);
1162 if (fn->fn_flags & RTN_RTINFO)
1167 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1169 struct rt6_info *rt = *prt;
1171 if (dst_hold_safe(&rt->dst))
1174 rt = net->ipv6.ip6_null_entry;
1183 /* called with rcu_lock held */
1184 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1186 struct net_device *dev = res->nh->fib_nh_dev;
1187 struct fib6_info *f6i = res->f6i;
1188 unsigned short flags;
1189 struct rt6_info *nrt;
1191 if (!fib6_info_hold_safe(f6i))
1194 flags = fib6_info_dst_flags(f6i);
1195 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1197 fib6_info_release(f6i);
1201 ip6_rt_copy_init(nrt, res);
1205 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1206 dst_hold(&nrt->dst);
1210 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1211 struct fib6_table *table,
1213 const struct sk_buff *skb,
1216 struct fib6_result res = {};
1217 struct fib6_node *fn;
1218 struct rt6_info *rt;
1220 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1221 flags &= ~RT6_LOOKUP_F_IFACE;
1224 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1226 res.f6i = rcu_dereference(fn->leaf);
1228 res.f6i = net->ipv6.fib6_null_entry;
1230 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1233 if (res.f6i == net->ipv6.fib6_null_entry) {
1234 fn = fib6_backtrack(fn, &fl6->saddr);
1238 rt = net->ipv6.ip6_null_entry;
1241 } else if (res.fib6_flags & RTF_REJECT) {
1245 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1246 fl6->flowi6_oif != 0, skb, flags);
1248 /* Search through exception table */
1249 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1251 if (ip6_hold_safe(net, &rt))
1252 dst_use_noref(&rt->dst, jiffies);
1255 rt = ip6_create_rt_rcu(&res);
1259 trace_fib6_table_lookup(net, &res, table, fl6);
1266 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1267 const struct sk_buff *skb, int flags)
1269 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1271 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1273 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1274 const struct in6_addr *saddr, int oif,
1275 const struct sk_buff *skb, int strict)
1277 struct flowi6 fl6 = {
1281 struct dst_entry *dst;
1282 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1285 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1286 flags |= RT6_LOOKUP_F_HAS_SADDR;
1289 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1290 if (dst->error == 0)
1291 return (struct rt6_info *) dst;
1297 EXPORT_SYMBOL(rt6_lookup);
1299 /* ip6_ins_rt is called with FREE table->tb6_lock.
1300 * It takes new route entry, the addition fails by any reason the
1301 * route is released.
1302 * Caller must hold dst before calling it.
1305 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1306 struct netlink_ext_ack *extack)
1309 struct fib6_table *table;
1311 table = rt->fib6_table;
1312 spin_lock_bh(&table->tb6_lock);
1313 err = fib6_add(&table->tb6_root, rt, info, extack);
1314 spin_unlock_bh(&table->tb6_lock);
1319 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1321 struct nl_info info = { .nl_net = net, };
1323 return __ip6_ins_rt(rt, &info, NULL);
1326 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1327 const struct in6_addr *daddr,
1328 const struct in6_addr *saddr)
1330 struct fib6_info *f6i = res->f6i;
1331 struct net_device *dev;
1332 struct rt6_info *rt;
1338 if (!fib6_info_hold_safe(f6i))
1341 dev = ip6_rt_get_dev_rcu(res);
1342 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1344 fib6_info_release(f6i);
1348 ip6_rt_copy_init(rt, res);
1349 rt->rt6i_flags |= RTF_CACHE;
1350 rt->dst.flags |= DST_HOST;
1351 rt->rt6i_dst.addr = *daddr;
1352 rt->rt6i_dst.plen = 128;
1354 if (!rt6_is_gw_or_nonexthop(res)) {
1355 if (f6i->fib6_dst.plen != 128 &&
1356 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1357 rt->rt6i_flags |= RTF_ANYCAST;
1358 #ifdef CONFIG_IPV6_SUBTREES
1359 if (rt->rt6i_src.plen && saddr) {
1360 rt->rt6i_src.addr = *saddr;
1361 rt->rt6i_src.plen = 128;
1369 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1371 struct fib6_info *f6i = res->f6i;
1372 unsigned short flags = fib6_info_dst_flags(f6i);
1373 struct net_device *dev;
1374 struct rt6_info *pcpu_rt;
1376 if (!fib6_info_hold_safe(f6i))
1380 dev = ip6_rt_get_dev_rcu(res);
1381 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
1384 fib6_info_release(f6i);
1387 ip6_rt_copy_init(pcpu_rt, res);
1388 pcpu_rt->rt6i_flags |= RTF_PCPU;
1392 /* It should be called with rcu_read_lock() acquired */
1393 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1395 struct rt6_info *pcpu_rt;
1397 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1402 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1403 const struct fib6_result *res)
1405 struct rt6_info *pcpu_rt, *prev, **p;
1407 pcpu_rt = ip6_rt_pcpu_alloc(res);
1411 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1412 prev = cmpxchg(p, NULL, pcpu_rt);
1415 if (res->f6i->fib6_destroying) {
1416 struct fib6_info *from;
1418 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1419 fib6_info_release(from);
1425 /* exception hash table implementation
1427 static DEFINE_SPINLOCK(rt6_exception_lock);
1429 /* Remove rt6_ex from hash table and free the memory
1430 * Caller must hold rt6_exception_lock
1432 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1433 struct rt6_exception *rt6_ex)
1435 struct fib6_info *from;
1438 if (!bucket || !rt6_ex)
1441 net = dev_net(rt6_ex->rt6i->dst.dev);
1442 net->ipv6.rt6_stats->fib_rt_cache--;
1444 /* purge completely the exception to allow releasing the held resources:
1445 * some [sk] cache may keep the dst around for unlimited time
1447 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1448 fib6_info_release(from);
1449 dst_dev_put(&rt6_ex->rt6i->dst);
1451 hlist_del_rcu(&rt6_ex->hlist);
1452 dst_release(&rt6_ex->rt6i->dst);
1453 kfree_rcu(rt6_ex, rcu);
1454 WARN_ON_ONCE(!bucket->depth);
1458 /* Remove oldest rt6_ex in bucket and free the memory
1459 * Caller must hold rt6_exception_lock
1461 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1463 struct rt6_exception *rt6_ex, *oldest = NULL;
1468 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1469 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1472 rt6_remove_exception(bucket, oldest);
1475 static u32 rt6_exception_hash(const struct in6_addr *dst,
1476 const struct in6_addr *src)
1478 static u32 seed __read_mostly;
1481 net_get_random_once(&seed, sizeof(seed));
1482 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
1484 #ifdef CONFIG_IPV6_SUBTREES
1486 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
1488 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1491 /* Helper function to find the cached rt in the hash table
1492 * and update bucket pointer to point to the bucket for this
1493 * (daddr, saddr) pair
1494 * Caller must hold rt6_exception_lock
1496 static struct rt6_exception *
1497 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1498 const struct in6_addr *daddr,
1499 const struct in6_addr *saddr)
1501 struct rt6_exception *rt6_ex;
1504 if (!(*bucket) || !daddr)
1507 hval = rt6_exception_hash(daddr, saddr);
1510 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1511 struct rt6_info *rt6 = rt6_ex->rt6i;
1512 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1514 #ifdef CONFIG_IPV6_SUBTREES
1515 if (matched && saddr)
1516 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1524 /* Helper function to find the cached rt in the hash table
1525 * and update bucket pointer to point to the bucket for this
1526 * (daddr, saddr) pair
1527 * Caller must hold rcu_read_lock()
1529 static struct rt6_exception *
1530 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1531 const struct in6_addr *daddr,
1532 const struct in6_addr *saddr)
1534 struct rt6_exception *rt6_ex;
1537 WARN_ON_ONCE(!rcu_read_lock_held());
1539 if (!(*bucket) || !daddr)
1542 hval = rt6_exception_hash(daddr, saddr);
1545 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1546 struct rt6_info *rt6 = rt6_ex->rt6i;
1547 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1549 #ifdef CONFIG_IPV6_SUBTREES
1550 if (matched && saddr)
1551 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1559 static unsigned int fib6_mtu(const struct fib6_result *res)
1561 const struct fib6_nh *nh = res->nh;
1564 if (res->f6i->fib6_pmtu) {
1565 mtu = res->f6i->fib6_pmtu;
1567 struct net_device *dev = nh->fib_nh_dev;
1568 struct inet6_dev *idev;
1571 idev = __in6_dev_get(dev);
1572 mtu = idev->cnf.mtu6;
1576 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1578 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1581 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1583 /* used when the flushed bit is not relevant, only access to the bucket
1584 * (ie., all bucket users except rt6_insert_exception);
1586 * called under rcu lock; sometimes called with rt6_exception_lock held
1589 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1592 struct rt6_exception_bucket *bucket;
1595 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1596 lockdep_is_held(lock));
1598 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1600 /* remove bucket flushed bit if set */
1602 unsigned long p = (unsigned long)bucket;
1604 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1605 bucket = (struct rt6_exception_bucket *)p;
1611 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1613 unsigned long p = (unsigned long)bucket;
1615 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1618 /* called with rt6_exception_lock held */
1619 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1622 struct rt6_exception_bucket *bucket;
1625 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1626 lockdep_is_held(lock));
1628 p = (unsigned long)bucket;
1629 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1630 bucket = (struct rt6_exception_bucket *)p;
1631 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1634 static int rt6_insert_exception(struct rt6_info *nrt,
1635 const struct fib6_result *res)
1637 struct net *net = dev_net(nrt->dst.dev);
1638 struct rt6_exception_bucket *bucket;
1639 struct fib6_info *f6i = res->f6i;
1640 struct in6_addr *src_key = NULL;
1641 struct rt6_exception *rt6_ex;
1642 struct fib6_nh *nh = res->nh;
1645 spin_lock_bh(&rt6_exception_lock);
1647 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1648 lockdep_is_held(&rt6_exception_lock));
1650 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1656 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1657 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1662 #ifdef CONFIG_IPV6_SUBTREES
1663 /* fib6_src.plen != 0 indicates f6i is in subtree
1664 * and exception table is indexed by a hash of
1665 * both fib6_dst and fib6_src.
1666 * Otherwise, the exception table is indexed by
1667 * a hash of only fib6_dst.
1669 if (f6i->fib6_src.plen)
1670 src_key = &nrt->rt6i_src.addr;
1672 /* rt6_mtu_change() might lower mtu on f6i.
1673 * Only insert this exception route if its mtu
1674 * is less than f6i's mtu value.
1676 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1681 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1684 rt6_remove_exception(bucket, rt6_ex);
1686 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1692 rt6_ex->stamp = jiffies;
1693 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1695 net->ipv6.rt6_stats->fib_rt_cache++;
1697 if (bucket->depth > FIB6_MAX_DEPTH)
1698 rt6_exception_remove_oldest(bucket);
1701 spin_unlock_bh(&rt6_exception_lock);
1703 /* Update fn->fn_sernum to invalidate all cached dst */
1705 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1706 fib6_update_sernum(net, f6i);
1707 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1708 fib6_force_start_gc(net);
1714 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1716 struct rt6_exception_bucket *bucket;
1717 struct rt6_exception *rt6_ex;
1718 struct hlist_node *tmp;
1721 spin_lock_bh(&rt6_exception_lock);
1723 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1727 /* Prevent rt6_insert_exception() to recreate the bucket list */
1729 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1731 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1732 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1734 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1735 rt6_remove_exception(bucket, rt6_ex);
1737 WARN_ON_ONCE(!from && bucket->depth);
1741 spin_unlock_bh(&rt6_exception_lock);
1744 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1746 struct fib6_info *f6i = arg;
1748 fib6_nh_flush_exceptions(nh, f6i);
1753 void rt6_flush_exceptions(struct fib6_info *f6i)
1756 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1759 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1762 /* Find cached rt in the hash table inside passed in rt
1763 * Caller has to hold rcu_read_lock()
1765 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1766 const struct in6_addr *daddr,
1767 const struct in6_addr *saddr)
1769 const struct in6_addr *src_key = NULL;
1770 struct rt6_exception_bucket *bucket;
1771 struct rt6_exception *rt6_ex;
1772 struct rt6_info *ret = NULL;
1774 #ifdef CONFIG_IPV6_SUBTREES
1775 /* fib6i_src.plen != 0 indicates f6i is in subtree
1776 * and exception table is indexed by a hash of
1777 * both fib6_dst and fib6_src.
1778 * However, the src addr used to create the hash
1779 * might not be exactly the passed in saddr which
1780 * is a /128 addr from the flow.
1781 * So we need to use f6i->fib6_src to redo lookup
1782 * if the passed in saddr does not find anything.
1783 * (See the logic in ip6_rt_cache_alloc() on how
1784 * rt->rt6i_src is updated.)
1786 if (res->f6i->fib6_src.plen)
1790 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1791 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1793 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1796 #ifdef CONFIG_IPV6_SUBTREES
1797 /* Use fib6_src as src_key and redo lookup */
1798 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1799 src_key = &res->f6i->fib6_src.addr;
1807 /* Remove the passed in cached rt from the hash table that contains it */
1808 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1809 const struct rt6_info *rt)
1811 const struct in6_addr *src_key = NULL;
1812 struct rt6_exception_bucket *bucket;
1813 struct rt6_exception *rt6_ex;
1816 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1819 spin_lock_bh(&rt6_exception_lock);
1820 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1822 #ifdef CONFIG_IPV6_SUBTREES
1823 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1824 * and exception table is indexed by a hash of
1825 * both rt6i_dst and rt6i_src.
1826 * Otherwise, the exception table is indexed by
1827 * a hash of only rt6i_dst.
1830 src_key = &rt->rt6i_src.addr;
1832 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1836 rt6_remove_exception(bucket, rt6_ex);
1842 spin_unlock_bh(&rt6_exception_lock);
1846 struct fib6_nh_excptn_arg {
1847 struct rt6_info *rt;
1851 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1853 struct fib6_nh_excptn_arg *arg = _arg;
1856 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1863 static int rt6_remove_exception_rt(struct rt6_info *rt)
1865 struct fib6_info *from;
1867 from = rcu_dereference(rt->from);
1868 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1872 struct fib6_nh_excptn_arg arg = {
1874 .plen = from->fib6_src.plen
1878 /* rc = 1 means an entry was found */
1879 rc = nexthop_for_each_fib6_nh(from->nh,
1880 rt6_nh_remove_exception_rt,
1882 return rc ? 0 : -ENOENT;
1885 return fib6_nh_remove_exception(from->fib6_nh,
1886 from->fib6_src.plen, rt);
1889 /* Find rt6_ex which contains the passed in rt cache and
1892 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1893 const struct rt6_info *rt)
1895 const struct in6_addr *src_key = NULL;
1896 struct rt6_exception_bucket *bucket;
1897 struct rt6_exception *rt6_ex;
1899 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1900 #ifdef CONFIG_IPV6_SUBTREES
1901 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1902 * and exception table is indexed by a hash of
1903 * both rt6i_dst and rt6i_src.
1904 * Otherwise, the exception table is indexed by
1905 * a hash of only rt6i_dst.
1908 src_key = &rt->rt6i_src.addr;
1910 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1912 rt6_ex->stamp = jiffies;
1915 struct fib6_nh_match_arg {
1916 const struct net_device *dev;
1917 const struct in6_addr *gw;
1918 struct fib6_nh *match;
1921 /* determine if fib6_nh has given device and gateway */
1922 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1924 struct fib6_nh_match_arg *arg = _arg;
1926 if (arg->dev != nh->fib_nh_dev ||
1927 (arg->gw && !nh->fib_nh_gw_family) ||
1928 (!arg->gw && nh->fib_nh_gw_family) ||
1929 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1934 /* found a match, break the loop */
1938 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1940 struct fib6_info *from;
1941 struct fib6_nh *fib6_nh;
1945 from = rcu_dereference(rt->from);
1946 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1950 struct fib6_nh_match_arg arg = {
1952 .gw = &rt->rt6i_gateway,
1955 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1959 fib6_nh = arg.match;
1961 fib6_nh = from->fib6_nh;
1963 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1968 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1969 struct rt6_info *rt, int mtu)
1971 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1972 * lowest MTU in the path: always allow updating the route PMTU to
1973 * reflect PMTU decreases.
1975 * If the new MTU is higher, and the route PMTU is equal to the local
1976 * MTU, this means the old MTU is the lowest in the path, so allow
1977 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1981 if (dst_mtu(&rt->dst) >= mtu)
1984 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1990 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1991 const struct fib6_nh *nh, int mtu)
1993 struct rt6_exception_bucket *bucket;
1994 struct rt6_exception *rt6_ex;
1997 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2001 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2002 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2003 struct rt6_info *entry = rt6_ex->rt6i;
2005 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2006 * route), the metrics of its rt->from have already
2009 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2010 rt6_mtu_change_route_allowed(idev, entry, mtu))
2011 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2017 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2019 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2020 const struct in6_addr *gateway)
2022 struct rt6_exception_bucket *bucket;
2023 struct rt6_exception *rt6_ex;
2024 struct hlist_node *tmp;
2027 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2030 spin_lock_bh(&rt6_exception_lock);
2031 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2033 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2034 hlist_for_each_entry_safe(rt6_ex, tmp,
2035 &bucket->chain, hlist) {
2036 struct rt6_info *entry = rt6_ex->rt6i;
2038 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2039 RTF_CACHE_GATEWAY &&
2040 ipv6_addr_equal(gateway,
2041 &entry->rt6i_gateway)) {
2042 rt6_remove_exception(bucket, rt6_ex);
2049 spin_unlock_bh(&rt6_exception_lock);
2052 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2053 struct rt6_exception *rt6_ex,
2054 struct fib6_gc_args *gc_args,
2057 struct rt6_info *rt = rt6_ex->rt6i;
2059 /* we are pruning and obsoleting aged-out and non gateway exceptions
2060 * even if others have still references to them, so that on next
2061 * dst_check() such references can be dropped.
2062 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2063 * expired, independently from their aging, as per RFC 8201 section 4
2065 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2066 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2067 RT6_TRACE("aging clone %p\n", rt);
2068 rt6_remove_exception(bucket, rt6_ex);
2071 } else if (time_after(jiffies, rt->dst.expires)) {
2072 RT6_TRACE("purging expired route %p\n", rt);
2073 rt6_remove_exception(bucket, rt6_ex);
2077 if (rt->rt6i_flags & RTF_GATEWAY) {
2078 struct neighbour *neigh;
2079 __u8 neigh_flags = 0;
2081 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2083 neigh_flags = neigh->flags;
2085 if (!(neigh_flags & NTF_ROUTER)) {
2086 RT6_TRACE("purging route %p via non-router but gateway\n",
2088 rt6_remove_exception(bucket, rt6_ex);
2096 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2097 struct fib6_gc_args *gc_args,
2100 struct rt6_exception_bucket *bucket;
2101 struct rt6_exception *rt6_ex;
2102 struct hlist_node *tmp;
2105 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2109 spin_lock(&rt6_exception_lock);
2110 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2112 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2113 hlist_for_each_entry_safe(rt6_ex, tmp,
2114 &bucket->chain, hlist) {
2115 rt6_age_examine_exception(bucket, rt6_ex,
2121 spin_unlock(&rt6_exception_lock);
2122 rcu_read_unlock_bh();
2125 struct fib6_nh_age_excptn_arg {
2126 struct fib6_gc_args *gc_args;
2130 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2132 struct fib6_nh_age_excptn_arg *arg = _arg;
2134 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2138 void rt6_age_exceptions(struct fib6_info *f6i,
2139 struct fib6_gc_args *gc_args,
2143 struct fib6_nh_age_excptn_arg arg = {
2148 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2151 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2155 /* must be called with rcu lock held */
2156 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2157 struct flowi6 *fl6, struct fib6_result *res, int strict)
2159 struct fib6_node *fn, *saved_fn;
2161 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2164 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2168 rt6_select(net, fn, oif, res, strict);
2169 if (res->f6i == net->ipv6.fib6_null_entry) {
2170 fn = fib6_backtrack(fn, &fl6->saddr);
2172 goto redo_rt6_select;
2173 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2174 /* also consider unreachable route */
2175 strict &= ~RT6_LOOKUP_F_REACHABLE;
2177 goto redo_rt6_select;
2181 trace_fib6_table_lookup(net, res, table, fl6);
2186 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2187 int oif, struct flowi6 *fl6,
2188 const struct sk_buff *skb, int flags)
2190 struct fib6_result res = {};
2191 struct rt6_info *rt = NULL;
2194 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2195 !rcu_read_lock_held());
2197 strict |= flags & RT6_LOOKUP_F_IFACE;
2198 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2199 if (net->ipv6.devconf_all->forwarding == 0)
2200 strict |= RT6_LOOKUP_F_REACHABLE;
2204 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2205 if (res.f6i == net->ipv6.fib6_null_entry)
2208 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2210 /*Search through exception table */
2211 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2214 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2215 !res.nh->fib_nh_gw_family)) {
2216 /* Create a RTF_CACHE clone which will not be
2217 * owned by the fib6 tree. It is for the special case where
2218 * the daddr in the skb during the neighbor look-up is different
2219 * from the fl6->daddr used to look-up route here.
2221 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2224 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2225 * As rt6_uncached_list_add() does not consume refcnt,
2226 * this refcnt is always returned to the caller even
2227 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2229 rt6_uncached_list_add(rt);
2230 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
2236 /* Get a percpu copy */
2238 rt = rt6_get_pcpu_route(&res);
2241 rt = rt6_make_pcpu_route(net, &res);
2247 rt = net->ipv6.ip6_null_entry;
2248 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2249 ip6_hold_safe(net, &rt);
2254 EXPORT_SYMBOL_GPL(ip6_pol_route);
2256 static struct rt6_info *ip6_pol_route_input(struct net *net,
2257 struct fib6_table *table,
2259 const struct sk_buff *skb,
2262 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2265 struct dst_entry *ip6_route_input_lookup(struct net *net,
2266 struct net_device *dev,
2268 const struct sk_buff *skb,
2271 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2272 flags |= RT6_LOOKUP_F_IFACE;
2274 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2276 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2278 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2279 struct flow_keys *keys,
2280 struct flow_keys *flkeys)
2282 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2283 const struct ipv6hdr *key_iph = outer_iph;
2284 struct flow_keys *_flkeys = flkeys;
2285 const struct ipv6hdr *inner_iph;
2286 const struct icmp6hdr *icmph;
2287 struct ipv6hdr _inner_iph;
2288 struct icmp6hdr _icmph;
2290 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2293 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2294 sizeof(_icmph), &_icmph);
2298 if (!icmpv6_is_err(icmph->icmp6_type))
2301 inner_iph = skb_header_pointer(skb,
2302 skb_transport_offset(skb) + sizeof(*icmph),
2303 sizeof(_inner_iph), &_inner_iph);
2307 key_iph = inner_iph;
2311 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2312 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2313 keys->tags.flow_label = _flkeys->tags.flow_label;
2314 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2316 keys->addrs.v6addrs.src = key_iph->saddr;
2317 keys->addrs.v6addrs.dst = key_iph->daddr;
2318 keys->tags.flow_label = ip6_flowlabel(key_iph);
2319 keys->basic.ip_proto = key_iph->nexthdr;
2323 /* if skb is set it will be used and fl6 can be NULL */
2324 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2325 const struct sk_buff *skb, struct flow_keys *flkeys)
2327 struct flow_keys hash_keys;
2330 switch (ip6_multipath_hash_policy(net)) {
2332 memset(&hash_keys, 0, sizeof(hash_keys));
2333 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2335 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2337 hash_keys.addrs.v6addrs.src = fl6->saddr;
2338 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2339 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2340 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2345 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2346 struct flow_keys keys;
2348 /* short-circuit if we already have L4 hash present */
2350 return skb_get_hash_raw(skb) >> 1;
2352 memset(&hash_keys, 0, sizeof(hash_keys));
2355 skb_flow_dissect_flow_keys(skb, &keys, flag);
2358 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2359 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2360 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2361 hash_keys.ports.src = flkeys->ports.src;
2362 hash_keys.ports.dst = flkeys->ports.dst;
2363 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2365 memset(&hash_keys, 0, sizeof(hash_keys));
2366 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2367 hash_keys.addrs.v6addrs.src = fl6->saddr;
2368 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2369 hash_keys.ports.src = fl6->fl6_sport;
2370 hash_keys.ports.dst = fl6->fl6_dport;
2371 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2375 memset(&hash_keys, 0, sizeof(hash_keys));
2376 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2378 struct flow_keys keys;
2381 skb_flow_dissect_flow_keys(skb, &keys, 0);
2385 /* Inner can be v4 or v6 */
2386 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2387 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2388 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2389 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2390 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2391 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2392 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2393 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2394 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2395 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2397 /* Same as case 0 */
2398 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2399 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2402 /* Same as case 0 */
2403 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2404 hash_keys.addrs.v6addrs.src = fl6->saddr;
2405 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2406 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2407 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2411 mhash = flow_hash_from_keys(&hash_keys);
2416 /* Called with rcu held */
2417 void ip6_route_input(struct sk_buff *skb)
2419 const struct ipv6hdr *iph = ipv6_hdr(skb);
2420 struct net *net = dev_net(skb->dev);
2421 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2422 struct ip_tunnel_info *tun_info;
2423 struct flowi6 fl6 = {
2424 .flowi6_iif = skb->dev->ifindex,
2425 .daddr = iph->daddr,
2426 .saddr = iph->saddr,
2427 .flowlabel = ip6_flowinfo(iph),
2428 .flowi6_mark = skb->mark,
2429 .flowi6_proto = iph->nexthdr,
2431 struct flow_keys *flkeys = NULL, _flkeys;
2433 tun_info = skb_tunnel_info(skb);
2434 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2435 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2437 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2440 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2441 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2443 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2447 static struct rt6_info *ip6_pol_route_output(struct net *net,
2448 struct fib6_table *table,
2450 const struct sk_buff *skb,
2453 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2456 struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2457 const struct sock *sk,
2458 struct flowi6 *fl6, int flags)
2462 if (ipv6_addr_type(&fl6->daddr) &
2463 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2464 struct dst_entry *dst;
2466 /* This function does not take refcnt on the dst */
2467 dst = l3mdev_link_scope_lookup(net, fl6);
2472 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2474 flags |= RT6_LOOKUP_F_DST_NOREF;
2475 any_src = ipv6_addr_any(&fl6->saddr);
2476 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2477 (fl6->flowi6_oif && any_src))
2478 flags |= RT6_LOOKUP_F_IFACE;
2481 flags |= RT6_LOOKUP_F_HAS_SADDR;
2483 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2485 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2487 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2489 struct dst_entry *ip6_route_output_flags(struct net *net,
2490 const struct sock *sk,
2494 struct dst_entry *dst;
2495 struct rt6_info *rt6;
2498 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2499 rt6 = (struct rt6_info *)dst;
2500 /* For dst cached in uncached_list, refcnt is already taken. */
2501 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2502 dst = &net->ipv6.ip6_null_entry->dst;
2509 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2511 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2513 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2514 struct net_device *loopback_dev = net->loopback_dev;
2515 struct dst_entry *new = NULL;
2517 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2518 DST_OBSOLETE_DEAD, 0);
2521 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2525 new->input = dst_discard;
2526 new->output = dst_discard_out;
2528 dst_copy_metrics(new, &ort->dst);
2530 rt->rt6i_idev = in6_dev_get(loopback_dev);
2531 rt->rt6i_gateway = ort->rt6i_gateway;
2532 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2534 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2535 #ifdef CONFIG_IPV6_SUBTREES
2536 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2540 dst_release(dst_orig);
2541 return new ? new : ERR_PTR(-ENOMEM);
2545 * Destination cache support functions
2548 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2552 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2555 if (fib6_check_expired(f6i))
2561 static struct dst_entry *rt6_check(struct rt6_info *rt,
2562 struct fib6_info *from,
2567 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2568 rt_cookie != cookie)
2571 if (rt6_check_expired(rt))
2577 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2578 struct fib6_info *from,
2581 if (!__rt6_check_expired(rt) &&
2582 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2583 fib6_check(from, cookie))
2589 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2591 struct dst_entry *dst_ret;
2592 struct fib6_info *from;
2593 struct rt6_info *rt;
2595 rt = container_of(dst, struct rt6_info, dst);
2599 /* All IPV6 dsts are created with ->obsolete set to the value
2600 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2601 * into this function always.
2604 from = rcu_dereference(rt->from);
2606 if (from && (rt->rt6i_flags & RTF_PCPU ||
2607 unlikely(!list_empty(&rt->rt6i_uncached))))
2608 dst_ret = rt6_dst_from_check(rt, from, cookie);
2610 dst_ret = rt6_check(rt, from, cookie);
2617 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2619 struct rt6_info *rt = (struct rt6_info *) dst;
2622 if (rt->rt6i_flags & RTF_CACHE) {
2624 if (rt6_check_expired(rt)) {
2625 rt6_remove_exception_rt(rt);
2637 static void ip6_link_failure(struct sk_buff *skb)
2639 struct rt6_info *rt;
2641 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2643 rt = (struct rt6_info *) skb_dst(skb);
2646 if (rt->rt6i_flags & RTF_CACHE) {
2647 rt6_remove_exception_rt(rt);
2649 struct fib6_info *from;
2650 struct fib6_node *fn;
2652 from = rcu_dereference(rt->from);
2654 fn = rcu_dereference(from->fib6_node);
2655 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2663 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2665 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2666 struct fib6_info *from;
2669 from = rcu_dereference(rt0->from);
2671 rt0->dst.expires = from->expires;
2675 dst_set_expires(&rt0->dst, timeout);
2676 rt0->rt6i_flags |= RTF_EXPIRES;
2679 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2681 struct net *net = dev_net(rt->dst.dev);
2683 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2684 rt->rt6i_flags |= RTF_MODIFIED;
2685 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2688 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2690 return !(rt->rt6i_flags & RTF_CACHE) &&
2691 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2694 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2695 const struct ipv6hdr *iph, u32 mtu)
2697 const struct in6_addr *daddr, *saddr;
2698 struct rt6_info *rt6 = (struct rt6_info *)dst;
2700 if (dst_metric_locked(dst, RTAX_MTU))
2704 daddr = &iph->daddr;
2705 saddr = &iph->saddr;
2707 daddr = &sk->sk_v6_daddr;
2708 saddr = &inet6_sk(sk)->saddr;
2713 dst_confirm_neigh(dst, daddr);
2714 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2715 if (mtu >= dst_mtu(dst))
2718 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2719 rt6_do_update_pmtu(rt6, mtu);
2720 /* update rt6_ex->stamp for cache */
2721 if (rt6->rt6i_flags & RTF_CACHE)
2722 rt6_update_exception_stamp_rt(rt6);
2724 struct fib6_result res = {};
2725 struct rt6_info *nrt6;
2728 res.f6i = rcu_dereference(rt6->from);
2732 res.fib6_flags = res.f6i->fib6_flags;
2733 res.fib6_type = res.f6i->fib6_type;
2736 struct fib6_nh_match_arg arg = {
2738 .gw = &rt6->rt6i_gateway,
2741 nexthop_for_each_fib6_nh(res.f6i->nh,
2742 fib6_nh_find_match, &arg);
2744 /* fib6_info uses a nexthop that does not have fib6_nh
2745 * using the dst->dev + gw. Should be impossible.
2752 res.nh = res.f6i->fib6_nh;
2755 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2757 rt6_do_update_pmtu(nrt6, mtu);
2758 if (rt6_insert_exception(nrt6, &res))
2759 dst_release_immediate(&nrt6->dst);
2766 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2767 struct sk_buff *skb, u32 mtu)
2769 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2772 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2773 int oif, u32 mark, kuid_t uid)
2775 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2776 struct dst_entry *dst;
2777 struct flowi6 fl6 = {
2779 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2780 .daddr = iph->daddr,
2781 .saddr = iph->saddr,
2782 .flowlabel = ip6_flowinfo(iph),
2786 dst = ip6_route_output(net, NULL, &fl6);
2788 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
2791 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2793 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2795 int oif = sk->sk_bound_dev_if;
2796 struct dst_entry *dst;
2798 if (!oif && skb->dev)
2799 oif = l3mdev_master_ifindex(skb->dev);
2801 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2803 dst = __sk_dst_get(sk);
2804 if (!dst || !dst->obsolete ||
2805 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2809 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2810 ip6_datagram_dst_update(sk, false);
2813 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2815 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2816 const struct flowi6 *fl6)
2818 #ifdef CONFIG_IPV6_SUBTREES
2819 struct ipv6_pinfo *np = inet6_sk(sk);
2822 ip6_dst_store(sk, dst,
2823 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2824 &sk->sk_v6_daddr : NULL,
2825 #ifdef CONFIG_IPV6_SUBTREES
2826 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2832 static bool ip6_redirect_nh_match(const struct fib6_result *res,
2834 const struct in6_addr *gw,
2835 struct rt6_info **ret)
2837 const struct fib6_nh *nh = res->nh;
2839 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2840 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2843 /* rt_cache's gateway might be different from its 'parent'
2844 * in the case of an ip redirect.
2845 * So we keep searching in the exception table if the gateway
2848 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2849 struct rt6_info *rt_cache;
2851 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2853 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2862 struct fib6_nh_rd_arg {
2863 struct fib6_result *res;
2865 const struct in6_addr *gw;
2866 struct rt6_info **ret;
2869 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2871 struct fib6_nh_rd_arg *arg = _arg;
2874 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2877 /* Handle redirects */
2878 struct ip6rd_flowi {
2880 struct in6_addr gateway;
2883 static struct rt6_info *__ip6_route_redirect(struct net *net,
2884 struct fib6_table *table,
2886 const struct sk_buff *skb,
2889 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2890 struct rt6_info *ret = NULL;
2891 struct fib6_result res = {};
2892 struct fib6_nh_rd_arg arg = {
2895 .gw = &rdfl->gateway,
2898 struct fib6_info *rt;
2899 struct fib6_node *fn;
2901 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2902 * this case we must match on the real ingress device, so reset it
2904 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2905 fl6->flowi6_oif = skb->dev->ifindex;
2907 /* Get the "current" route for this destination and
2908 * check if the redirect has come from appropriate router.
2910 * RFC 4861 specifies that redirects should only be
2911 * accepted if they come from the nexthop to the target.
2912 * Due to the way the routes are chosen, this notion
2913 * is a bit fuzzy and one might need to check all possible
2918 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2920 for_each_fib6_node_rt_rcu(fn) {
2922 if (fib6_check_expired(rt))
2924 if (rt->fib6_flags & RTF_REJECT)
2926 if (unlikely(rt->nh)) {
2927 if (nexthop_is_blackhole(rt->nh))
2929 /* on match, res->nh is filled in and potentially ret */
2930 if (nexthop_for_each_fib6_nh(rt->nh,
2931 fib6_nh_redirect_match,
2935 res.nh = rt->fib6_nh;
2936 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2943 rt = net->ipv6.fib6_null_entry;
2944 else if (rt->fib6_flags & RTF_REJECT) {
2945 ret = net->ipv6.ip6_null_entry;
2949 if (rt == net->ipv6.fib6_null_entry) {
2950 fn = fib6_backtrack(fn, &fl6->saddr);
2956 res.nh = rt->fib6_nh;
2959 ip6_hold_safe(net, &ret);
2961 res.fib6_flags = res.f6i->fib6_flags;
2962 res.fib6_type = res.f6i->fib6_type;
2963 ret = ip6_create_rt_rcu(&res);
2968 trace_fib6_table_lookup(net, &res, table, fl6);
2972 static struct dst_entry *ip6_route_redirect(struct net *net,
2973 const struct flowi6 *fl6,
2974 const struct sk_buff *skb,
2975 const struct in6_addr *gateway)
2977 int flags = RT6_LOOKUP_F_HAS_SADDR;
2978 struct ip6rd_flowi rdfl;
2981 rdfl.gateway = *gateway;
2983 return fib6_rule_lookup(net, &rdfl.fl6, skb,
2984 flags, __ip6_route_redirect);
2987 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2990 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2991 struct dst_entry *dst;
2992 struct flowi6 fl6 = {
2993 .flowi6_iif = LOOPBACK_IFINDEX,
2995 .flowi6_mark = mark,
2996 .daddr = iph->daddr,
2997 .saddr = iph->saddr,
2998 .flowlabel = ip6_flowinfo(iph),
3002 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3003 rt6_do_redirect(dst, NULL, skb);
3006 EXPORT_SYMBOL_GPL(ip6_redirect);
3008 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3010 const struct ipv6hdr *iph = ipv6_hdr(skb);
3011 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3012 struct dst_entry *dst;
3013 struct flowi6 fl6 = {
3014 .flowi6_iif = LOOPBACK_IFINDEX,
3017 .saddr = iph->daddr,
3018 .flowi6_uid = sock_net_uid(net, NULL),
3021 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3022 rt6_do_redirect(dst, NULL, skb);
3026 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3028 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3031 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3033 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3035 struct net_device *dev = dst->dev;
3036 unsigned int mtu = dst_mtu(dst);
3037 struct net *net = dev_net(dev);
3039 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3041 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3042 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3045 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3046 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3047 * IPV6_MAXPLEN is also valid and means: "any MSS,
3048 * rely only on pmtu discovery"
3050 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3055 static unsigned int ip6_mtu(const struct dst_entry *dst)
3057 struct inet6_dev *idev;
3060 mtu = dst_metric_raw(dst, RTAX_MTU);
3067 idev = __in6_dev_get(dst->dev);
3069 mtu = idev->cnf.mtu6;
3073 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3075 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3079 * 1. mtu on route is locked - use it
3080 * 2. mtu from nexthop exception
3081 * 3. mtu from egress device
3083 * based on ip6_dst_mtu_forward and exception logic of
3084 * rt6_find_cached_rt; called with rcu_read_lock
3086 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3087 const struct in6_addr *daddr,
3088 const struct in6_addr *saddr)
3090 const struct fib6_nh *nh = res->nh;
3091 struct fib6_info *f6i = res->f6i;
3092 struct inet6_dev *idev;
3093 struct rt6_info *rt;
3096 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3097 mtu = f6i->fib6_pmtu;
3102 rt = rt6_find_cached_rt(res, daddr, saddr);
3104 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3106 struct net_device *dev = nh->fib_nh_dev;
3109 idev = __in6_dev_get(dev);
3110 if (idev && idev->cnf.mtu6 > mtu)
3111 mtu = idev->cnf.mtu6;
3114 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3116 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3119 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3122 struct dst_entry *dst;
3123 struct rt6_info *rt;
3124 struct inet6_dev *idev = in6_dev_get(dev);
3125 struct net *net = dev_net(dev);
3127 if (unlikely(!idev))
3128 return ERR_PTR(-ENODEV);
3130 rt = ip6_dst_alloc(net, dev, 0);
3131 if (unlikely(!rt)) {
3133 dst = ERR_PTR(-ENOMEM);
3137 rt->dst.flags |= DST_HOST;
3138 rt->dst.input = ip6_input;
3139 rt->dst.output = ip6_output;
3140 rt->rt6i_gateway = fl6->daddr;
3141 rt->rt6i_dst.addr = fl6->daddr;
3142 rt->rt6i_dst.plen = 128;
3143 rt->rt6i_idev = idev;
3144 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3146 /* Add this dst into uncached_list so that rt6_disable_ip() can
3147 * do proper release of the net_device
3149 rt6_uncached_list_add(rt);
3150 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3152 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3158 static int ip6_dst_gc(struct dst_ops *ops)
3160 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3161 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3162 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3163 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3164 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3165 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3168 entries = dst_entries_get_fast(ops);
3169 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3170 entries <= rt_max_size)
3173 net->ipv6.ip6_rt_gc_expire++;
3174 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3175 entries = dst_entries_get_slow(ops);
3176 if (entries < ops->gc_thresh)
3177 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
3179 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3180 return entries > rt_max_size;
3183 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3184 const struct in6_addr *gw_addr, u32 tbid,
3185 int flags, struct fib6_result *res)
3187 struct flowi6 fl6 = {
3188 .flowi6_oif = cfg->fc_ifindex,
3190 .saddr = cfg->fc_prefsrc,
3192 struct fib6_table *table;
3195 table = fib6_get_table(net, tbid);
3199 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3200 flags |= RT6_LOOKUP_F_HAS_SADDR;
3202 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3204 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3205 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3206 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3207 cfg->fc_ifindex != 0, NULL, flags);
3212 static int ip6_route_check_nh_onlink(struct net *net,
3213 struct fib6_config *cfg,
3214 const struct net_device *dev,
3215 struct netlink_ext_ack *extack)
3217 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3218 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3219 struct fib6_result res = {};
3222 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3223 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3224 /* ignore match if it is the default route */
3225 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3226 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3227 NL_SET_ERR_MSG(extack,
3228 "Nexthop has invalid gateway or device mismatch");
3235 static int ip6_route_check_nh(struct net *net,
3236 struct fib6_config *cfg,
3237 struct net_device **_dev,
3238 struct inet6_dev **idev)
3240 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3241 struct net_device *dev = _dev ? *_dev : NULL;
3242 int flags = RT6_LOOKUP_F_IFACE;
3243 struct fib6_result res = {};
3244 int err = -EHOSTUNREACH;
3246 if (cfg->fc_table) {
3247 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3248 cfg->fc_table, flags, &res);
3249 /* gw_addr can not require a gateway or resolve to a reject
3250 * route. If a device is given, it must match the result.
3252 if (err || res.fib6_flags & RTF_REJECT ||
3253 res.nh->fib_nh_gw_family ||
3254 (dev && dev != res.nh->fib_nh_dev))
3255 err = -EHOSTUNREACH;
3259 struct flowi6 fl6 = {
3260 .flowi6_oif = cfg->fc_ifindex,
3264 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3265 if (err || res.fib6_flags & RTF_REJECT ||
3266 res.nh->fib_nh_gw_family)
3267 err = -EHOSTUNREACH;
3272 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3273 cfg->fc_ifindex != 0, NULL, flags);
3278 if (dev != res.nh->fib_nh_dev)
3279 err = -EHOSTUNREACH;
3281 *_dev = dev = res.nh->fib_nh_dev;
3283 *idev = in6_dev_get(dev);
3289 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3290 struct net_device **_dev, struct inet6_dev **idev,
3291 struct netlink_ext_ack *extack)
3293 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3294 int gwa_type = ipv6_addr_type(gw_addr);
3295 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3296 const struct net_device *dev = *_dev;
3297 bool need_addr_check = !dev;
3300 /* if gw_addr is local we will fail to detect this in case
3301 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3302 * will return already-added prefix route via interface that
3303 * prefix route was assigned to, which might be non-loopback.
3306 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3307 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3311 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3312 /* IPv6 strictly inhibits using not link-local
3313 * addresses as nexthop address.
3314 * Otherwise, router will not able to send redirects.
3315 * It is very good, but in some (rare!) circumstances
3316 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3317 * some exceptions. --ANK
3318 * We allow IPv4-mapped nexthops to support RFC4798-type
3321 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3322 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3328 if (cfg->fc_flags & RTNH_F_ONLINK)
3329 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3331 err = ip6_route_check_nh(net, cfg, _dev, idev);
3339 /* reload in case device was changed */
3344 NL_SET_ERR_MSG(extack, "Egress device not specified");
3346 } else if (dev->flags & IFF_LOOPBACK) {
3347 NL_SET_ERR_MSG(extack,
3348 "Egress device can not be loopback device for this route");
3352 /* if we did not check gw_addr above, do so now that the
3353 * egress device has been resolved.
3355 if (need_addr_check &&
3356 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3357 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3366 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3368 if ((flags & RTF_REJECT) ||
3369 (dev && (dev->flags & IFF_LOOPBACK) &&
3370 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3371 !(flags & RTF_LOCAL)))
3377 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3378 struct fib6_config *cfg, gfp_t gfp_flags,
3379 struct netlink_ext_ack *extack)
3381 struct net_device *dev = NULL;
3382 struct inet6_dev *idev = NULL;
3386 fib6_nh->fib_nh_family = AF_INET6;
3387 #ifdef CONFIG_IPV6_ROUTER_PREF
3388 fib6_nh->last_probe = jiffies;
3392 if (cfg->fc_ifindex) {
3393 dev = dev_get_by_index(net, cfg->fc_ifindex);
3396 idev = in6_dev_get(dev);
3401 if (cfg->fc_flags & RTNH_F_ONLINK) {
3403 NL_SET_ERR_MSG(extack,
3404 "Nexthop device required for onlink");
3408 if (!(dev->flags & IFF_UP)) {
3409 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3414 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3417 fib6_nh->fib_nh_weight = 1;
3419 /* We cannot add true routes via loopback here,
3420 * they would result in kernel looping; promote them to reject routes
3422 addr_type = ipv6_addr_type(&cfg->fc_dst);
3423 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3424 /* hold loopback dev/idev if we haven't done so. */
3425 if (dev != net->loopback_dev) {
3430 dev = net->loopback_dev;
3432 idev = in6_dev_get(dev);
3441 if (cfg->fc_flags & RTF_GATEWAY) {
3442 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3446 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3447 fib6_nh->fib_nh_gw_family = AF_INET6;
3454 if (idev->cnf.disable_ipv6) {
3455 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3460 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3461 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3466 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3467 !netif_carrier_ok(dev))
3468 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3470 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3471 cfg->fc_encap_type, cfg, gfp_flags, extack);
3476 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3477 if (!fib6_nh->rt6i_pcpu) {
3482 fib6_nh->fib_nh_dev = dev;
3483 fib6_nh->fib_nh_oif = dev->ifindex;
3490 lwtstate_put(fib6_nh->fib_nh_lws);
3491 fib6_nh->fib_nh_lws = NULL;
3499 void fib6_nh_release(struct fib6_nh *fib6_nh)
3501 struct rt6_exception_bucket *bucket;
3505 fib6_nh_flush_exceptions(fib6_nh, NULL);
3506 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3508 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3514 if (fib6_nh->rt6i_pcpu) {
3517 for_each_possible_cpu(cpu) {
3518 struct rt6_info **ppcpu_rt;
3519 struct rt6_info *pcpu_rt;
3521 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3522 pcpu_rt = *ppcpu_rt;
3524 dst_dev_put(&pcpu_rt->dst);
3525 dst_release(&pcpu_rt->dst);
3530 free_percpu(fib6_nh->rt6i_pcpu);
3533 fib_nh_common_release(&fib6_nh->nh_common);
3536 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3538 struct netlink_ext_ack *extack)
3540 struct net *net = cfg->fc_nlinfo.nl_net;
3541 struct fib6_info *rt = NULL;
3542 struct nexthop *nh = NULL;
3543 struct fib6_table *table;
3544 struct fib6_nh *fib6_nh;
3548 /* RTF_PCPU is an internal flag; can not be set by userspace */
3549 if (cfg->fc_flags & RTF_PCPU) {
3550 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3554 /* RTF_CACHE is an internal flag; can not be set by userspace */
3555 if (cfg->fc_flags & RTF_CACHE) {
3556 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3560 if (cfg->fc_type > RTN_MAX) {
3561 NL_SET_ERR_MSG(extack, "Invalid route type");
3565 if (cfg->fc_dst_len > 128) {
3566 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3569 if (cfg->fc_src_len > 128) {
3570 NL_SET_ERR_MSG(extack, "Invalid source address length");
3573 #ifndef CONFIG_IPV6_SUBTREES
3574 if (cfg->fc_src_len) {
3575 NL_SET_ERR_MSG(extack,
3576 "Specifying source address requires IPV6_SUBTREES to be enabled");
3580 if (cfg->fc_nh_id) {
3581 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3583 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3586 err = fib6_check_nexthop(nh, cfg, extack);
3592 if (cfg->fc_nlinfo.nlh &&
3593 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3594 table = fib6_get_table(net, cfg->fc_table);
3596 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3597 table = fib6_new_table(net, cfg->fc_table);
3600 table = fib6_new_table(net, cfg->fc_table);
3607 rt = fib6_info_alloc(gfp_flags, !nh);
3611 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3613 if (IS_ERR(rt->fib6_metrics)) {
3614 err = PTR_ERR(rt->fib6_metrics);
3615 /* Do not leave garbage there. */
3616 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3620 if (cfg->fc_flags & RTF_ADDRCONF)
3621 rt->dst_nocount = true;
3623 if (cfg->fc_flags & RTF_EXPIRES)
3624 fib6_set_expires(rt, jiffies +
3625 clock_t_to_jiffies(cfg->fc_expires));
3627 fib6_clean_expires(rt);
3629 if (cfg->fc_protocol == RTPROT_UNSPEC)
3630 cfg->fc_protocol = RTPROT_BOOT;
3631 rt->fib6_protocol = cfg->fc_protocol;
3633 rt->fib6_table = table;
3634 rt->fib6_metric = cfg->fc_metric;
3635 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3636 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3638 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3639 rt->fib6_dst.plen = cfg->fc_dst_len;
3640 if (rt->fib6_dst.plen == 128)
3641 rt->dst_host = true;
3643 #ifdef CONFIG_IPV6_SUBTREES
3644 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3645 rt->fib6_src.plen = cfg->fc_src_len;
3648 if (!nexthop_get(nh)) {
3649 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3652 if (rt->fib6_src.plen) {
3653 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3657 fib6_nh = nexthop_fib6_nh(rt->nh);
3659 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3663 fib6_nh = rt->fib6_nh;
3665 /* We cannot add true routes via loopback here, they would
3666 * result in kernel looping; promote them to reject routes
3668 addr_type = ipv6_addr_type(&cfg->fc_dst);
3669 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3671 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3674 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3675 struct net_device *dev = fib6_nh->fib_nh_dev;
3677 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3678 NL_SET_ERR_MSG(extack, "Invalid source address");
3682 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3683 rt->fib6_prefsrc.plen = 128;
3685 rt->fib6_prefsrc.plen = 0;
3689 fib6_info_release(rt);
3690 return ERR_PTR(err);
3693 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3694 struct netlink_ext_ack *extack)
3696 struct fib6_info *rt;
3699 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3703 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3704 fib6_info_release(rt);
3709 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3711 struct net *net = info->nl_net;
3712 struct fib6_table *table;
3715 if (rt == net->ipv6.fib6_null_entry) {
3720 table = rt->fib6_table;
3721 spin_lock_bh(&table->tb6_lock);
3722 err = fib6_del(rt, info);
3723 spin_unlock_bh(&table->tb6_lock);
3726 fib6_info_release(rt);
3730 int ip6_del_rt(struct net *net, struct fib6_info *rt)
3732 struct nl_info info = { .nl_net = net };
3734 return __ip6_del_rt(rt, &info);
3737 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3739 struct nl_info *info = &cfg->fc_nlinfo;
3740 struct net *net = info->nl_net;
3741 struct sk_buff *skb = NULL;
3742 struct fib6_table *table;
3745 if (rt == net->ipv6.fib6_null_entry)
3747 table = rt->fib6_table;
3748 spin_lock_bh(&table->tb6_lock);
3750 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3751 struct fib6_info *sibling, *next_sibling;
3753 /* prefer to send a single notification with all hops */
3754 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3756 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3758 if (rt6_fill_node(net, skb, rt, NULL,
3759 NULL, NULL, 0, RTM_DELROUTE,
3760 info->portid, seq, 0) < 0) {
3764 info->skip_notify = 1;
3767 info->skip_notify_kernel = 1;
3768 call_fib6_multipath_entry_notifiers(net,
3769 FIB_EVENT_ENTRY_DEL,
3773 list_for_each_entry_safe(sibling, next_sibling,
3776 err = fib6_del(sibling, info);
3782 err = fib6_del(rt, info);
3784 spin_unlock_bh(&table->tb6_lock);
3786 fib6_info_release(rt);
3789 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3790 info->nlh, gfp_any());
3795 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3799 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3802 if (cfg->fc_flags & RTF_GATEWAY &&
3803 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3806 rc = rt6_remove_exception_rt(rt);
3811 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3814 struct fib6_result res = {
3818 struct rt6_info *rt_cache;
3820 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3822 return __ip6_del_cached_rt(rt_cache, cfg);
3827 struct fib6_nh_del_cached_rt_arg {
3828 struct fib6_config *cfg;
3829 struct fib6_info *f6i;
3832 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3834 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3837 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3838 return rc != -ESRCH ? rc : 0;
3841 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3843 struct fib6_nh_del_cached_rt_arg arg = {
3848 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3851 static int ip6_route_del(struct fib6_config *cfg,
3852 struct netlink_ext_ack *extack)
3854 struct fib6_table *table;
3855 struct fib6_info *rt;
3856 struct fib6_node *fn;
3859 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3861 NL_SET_ERR_MSG(extack, "FIB table does not exist");
3867 fn = fib6_locate(&table->tb6_root,
3868 &cfg->fc_dst, cfg->fc_dst_len,
3869 &cfg->fc_src, cfg->fc_src_len,
3870 !(cfg->fc_flags & RTF_CACHE));
3873 for_each_fib6_node_rt_rcu(fn) {
3876 if (rt->nh && cfg->fc_nh_id &&
3877 rt->nh->id != cfg->fc_nh_id)
3880 if (cfg->fc_flags & RTF_CACHE) {
3884 rc = ip6_del_cached_rt_nh(cfg, rt);
3885 } else if (cfg->fc_nh_id) {
3889 rc = ip6_del_cached_rt(cfg, rt, nh);
3898 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3900 if (cfg->fc_protocol &&
3901 cfg->fc_protocol != rt->fib6_protocol)
3905 if (!fib6_info_hold_safe(rt))
3909 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3915 if (cfg->fc_ifindex &&
3917 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
3919 if (cfg->fc_flags & RTF_GATEWAY &&
3920 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
3922 if (!fib6_info_hold_safe(rt))
3926 /* if gateway was specified only delete the one hop */
3927 if (cfg->fc_flags & RTF_GATEWAY)
3928 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3930 return __ip6_del_rt_siblings(rt, cfg);
3938 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3940 struct netevent_redirect netevent;
3941 struct rt6_info *rt, *nrt = NULL;
3942 struct fib6_result res = {};
3943 struct ndisc_options ndopts;
3944 struct inet6_dev *in6_dev;
3945 struct neighbour *neigh;
3947 int optlen, on_link;
3950 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
3951 optlen -= sizeof(*msg);
3954 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3958 msg = (struct rd_msg *)icmp6_hdr(skb);
3960 if (ipv6_addr_is_multicast(&msg->dest)) {
3961 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3966 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3968 } else if (ipv6_addr_type(&msg->target) !=
3969 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
3970 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3974 in6_dev = __in6_dev_get(skb->dev);
3977 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3981 * The IP source address of the Redirect MUST be the same as the current
3982 * first-hop router for the specified ICMP Destination Address.
3985 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3986 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3991 if (ndopts.nd_opts_tgt_lladdr) {
3992 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3995 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4000 rt = (struct rt6_info *) dst;
4001 if (rt->rt6i_flags & RTF_REJECT) {
4002 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4006 /* Redirect received -> path was valid.
4007 * Look, redirects are sent only in response to data packets,
4008 * so that this nexthop apparently is reachable. --ANK
4010 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4012 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4017 * We have finally decided to accept it.
4020 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4021 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4022 NEIGH_UPDATE_F_OVERRIDE|
4023 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4024 NEIGH_UPDATE_F_ISROUTER)),
4025 NDISC_REDIRECT, &ndopts);
4028 res.f6i = rcu_dereference(rt->from);
4033 struct fib6_nh_match_arg arg = {
4035 .gw = &rt->rt6i_gateway,
4038 nexthop_for_each_fib6_nh(res.f6i->nh,
4039 fib6_nh_find_match, &arg);
4041 /* fib6_info uses a nexthop that does not have fib6_nh
4042 * using the dst->dev. Should be impossible
4048 res.nh = res.f6i->fib6_nh;
4051 res.fib6_flags = res.f6i->fib6_flags;
4052 res.fib6_type = res.f6i->fib6_type;
4053 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4057 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4059 nrt->rt6i_flags &= ~RTF_GATEWAY;
4061 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4063 /* rt6_insert_exception() will take care of duplicated exceptions */
4064 if (rt6_insert_exception(nrt, &res)) {
4065 dst_release_immediate(&nrt->dst);
4069 netevent.old = &rt->dst;
4070 netevent.new = &nrt->dst;
4071 netevent.daddr = &msg->dest;
4072 netevent.neigh = neigh;
4073 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4077 neigh_release(neigh);
4080 #ifdef CONFIG_IPV6_ROUTE_INFO
4081 static struct fib6_info *rt6_get_route_info(struct net *net,
4082 const struct in6_addr *prefix, int prefixlen,
4083 const struct in6_addr *gwaddr,
4084 struct net_device *dev)
4086 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4087 int ifindex = dev->ifindex;
4088 struct fib6_node *fn;
4089 struct fib6_info *rt = NULL;
4090 struct fib6_table *table;
4092 table = fib6_get_table(net, tb_id);
4097 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4101 for_each_fib6_node_rt_rcu(fn) {
4102 /* these routes do not use nexthops */
4105 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4107 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4108 !rt->fib6_nh->fib_nh_gw_family)
4110 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4112 if (!fib6_info_hold_safe(rt))
4121 static struct fib6_info *rt6_add_route_info(struct net *net,
4122 const struct in6_addr *prefix, int prefixlen,
4123 const struct in6_addr *gwaddr,
4124 struct net_device *dev,
4127 struct fib6_config cfg = {
4128 .fc_metric = IP6_RT_PRIO_USER,
4129 .fc_ifindex = dev->ifindex,
4130 .fc_dst_len = prefixlen,
4131 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4132 RTF_UP | RTF_PREF(pref),
4133 .fc_protocol = RTPROT_RA,
4134 .fc_type = RTN_UNICAST,
4135 .fc_nlinfo.portid = 0,
4136 .fc_nlinfo.nlh = NULL,
4137 .fc_nlinfo.nl_net = net,
4140 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4141 cfg.fc_dst = *prefix;
4142 cfg.fc_gateway = *gwaddr;
4144 /* We should treat it as a default route if prefix length is 0. */
4146 cfg.fc_flags |= RTF_DEFAULT;
4148 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4150 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4154 struct fib6_info *rt6_get_dflt_router(struct net *net,
4155 const struct in6_addr *addr,
4156 struct net_device *dev)
4158 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4159 struct fib6_info *rt;
4160 struct fib6_table *table;
4162 table = fib6_get_table(net, tb_id);
4167 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4170 /* RA routes do not use nexthops */
4175 if (dev == nh->fib_nh_dev &&
4176 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4177 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4180 if (rt && !fib6_info_hold_safe(rt))
4186 struct fib6_info *rt6_add_dflt_router(struct net *net,
4187 const struct in6_addr *gwaddr,
4188 struct net_device *dev,
4191 struct fib6_config cfg = {
4192 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4193 .fc_metric = IP6_RT_PRIO_USER,
4194 .fc_ifindex = dev->ifindex,
4195 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4196 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4197 .fc_protocol = RTPROT_RA,
4198 .fc_type = RTN_UNICAST,
4199 .fc_nlinfo.portid = 0,
4200 .fc_nlinfo.nlh = NULL,
4201 .fc_nlinfo.nl_net = net,
4204 cfg.fc_gateway = *gwaddr;
4206 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4207 struct fib6_table *table;
4209 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4211 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4214 return rt6_get_dflt_router(net, gwaddr, dev);
4217 static void __rt6_purge_dflt_routers(struct net *net,
4218 struct fib6_table *table)
4220 struct fib6_info *rt;
4224 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4225 struct net_device *dev = fib6_info_nh_dev(rt);
4226 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4228 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4229 (!idev || idev->cnf.accept_ra != 2) &&
4230 fib6_info_hold_safe(rt)) {
4232 ip6_del_rt(net, rt);
4238 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4241 void rt6_purge_dflt_routers(struct net *net)
4243 struct fib6_table *table;
4244 struct hlist_head *head;
4249 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4250 head = &net->ipv6.fib_table_hash[h];
4251 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4252 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4253 __rt6_purge_dflt_routers(net, table);
4260 static void rtmsg_to_fib6_config(struct net *net,
4261 struct in6_rtmsg *rtmsg,
4262 struct fib6_config *cfg)
4264 *cfg = (struct fib6_config){
4265 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4267 .fc_ifindex = rtmsg->rtmsg_ifindex,
4268 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4269 .fc_expires = rtmsg->rtmsg_info,
4270 .fc_dst_len = rtmsg->rtmsg_dst_len,
4271 .fc_src_len = rtmsg->rtmsg_src_len,
4272 .fc_flags = rtmsg->rtmsg_flags,
4273 .fc_type = rtmsg->rtmsg_type,
4275 .fc_nlinfo.nl_net = net,
4277 .fc_dst = rtmsg->rtmsg_dst,
4278 .fc_src = rtmsg->rtmsg_src,
4279 .fc_gateway = rtmsg->rtmsg_gateway,
4283 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
4285 struct fib6_config cfg;
4286 struct in6_rtmsg rtmsg;
4290 case SIOCADDRT: /* Add a route */
4291 case SIOCDELRT: /* Delete a route */
4292 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4294 err = copy_from_user(&rtmsg, arg,
4295 sizeof(struct in6_rtmsg));
4299 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
4304 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4307 err = ip6_route_del(&cfg, NULL);
4321 * Drop the packet on the floor
4324 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4326 struct dst_entry *dst = skb_dst(skb);
4327 struct net *net = dev_net(dst->dev);
4328 struct inet6_dev *idev;
4331 if (netif_is_l3_master(skb->dev) &&
4332 dst->dev == net->loopback_dev)
4333 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4335 idev = ip6_dst_idev(dst);
4337 switch (ipstats_mib_noroutes) {
4338 case IPSTATS_MIB_INNOROUTES:
4339 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4340 if (type == IPV6_ADDR_ANY) {
4341 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4345 case IPSTATS_MIB_OUTNOROUTES:
4346 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4350 /* Start over by dropping the dst for l3mdev case */
4351 if (netif_is_l3_master(skb->dev))
4354 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4359 static int ip6_pkt_discard(struct sk_buff *skb)
4361 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4364 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4366 skb->dev = skb_dst(skb)->dev;
4367 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4370 static int ip6_pkt_prohibit(struct sk_buff *skb)
4372 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4375 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4377 skb->dev = skb_dst(skb)->dev;
4378 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4382 * Allocate a dst for local (unicast / anycast) address.
4385 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4386 struct inet6_dev *idev,
4387 const struct in6_addr *addr,
4388 bool anycast, gfp_t gfp_flags)
4390 struct fib6_config cfg = {
4391 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4392 .fc_ifindex = idev->dev->ifindex,
4393 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4396 .fc_protocol = RTPROT_KERNEL,
4397 .fc_nlinfo.nl_net = net,
4398 .fc_ignore_dev_down = true,
4400 struct fib6_info *f6i;
4403 cfg.fc_type = RTN_ANYCAST;
4404 cfg.fc_flags |= RTF_ANYCAST;
4406 cfg.fc_type = RTN_LOCAL;
4407 cfg.fc_flags |= RTF_LOCAL;
4410 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
4412 f6i->dst_nocount = true;
4416 /* remove deleted ip from prefsrc entries */
4417 struct arg_dev_net_ip {
4418 struct net_device *dev;
4420 struct in6_addr *addr;
4423 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4425 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4426 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4427 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4430 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4431 rt != net->ipv6.fib6_null_entry &&
4432 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4433 spin_lock_bh(&rt6_exception_lock);
4434 /* remove prefsrc entry */
4435 rt->fib6_prefsrc.plen = 0;
4436 spin_unlock_bh(&rt6_exception_lock);
4441 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4443 struct net *net = dev_net(ifp->idev->dev);
4444 struct arg_dev_net_ip adni = {
4445 .dev = ifp->idev->dev,
4449 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4452 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4454 /* Remove routers and update dst entries when gateway turn into host. */
4455 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4457 struct in6_addr *gateway = (struct in6_addr *)arg;
4460 /* RA routes do not use nexthops */
4465 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4466 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4469 /* Further clean up cached routes in exception table.
4470 * This is needed because cached route may have a different
4471 * gateway than its 'parent' in the case of an ip redirect.
4473 fib6_nh_exceptions_clean_tohost(nh, gateway);
4478 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4480 fib6_clean_all(net, fib6_clean_tohost, gateway);
4483 struct arg_netdev_event {
4484 const struct net_device *dev;
4486 unsigned char nh_flags;
4487 unsigned long event;
4491 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4493 struct fib6_info *iter;
4494 struct fib6_node *fn;
4496 fn = rcu_dereference_protected(rt->fib6_node,
4497 lockdep_is_held(&rt->fib6_table->tb6_lock));
4498 iter = rcu_dereference_protected(fn->leaf,
4499 lockdep_is_held(&rt->fib6_table->tb6_lock));
4501 if (iter->fib6_metric == rt->fib6_metric &&
4502 rt6_qualify_for_ecmp(iter))
4504 iter = rcu_dereference_protected(iter->fib6_next,
4505 lockdep_is_held(&rt->fib6_table->tb6_lock));
4511 /* only called for fib entries with builtin fib6_nh */
4512 static bool rt6_is_dead(const struct fib6_info *rt)
4514 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4515 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4516 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4522 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4524 struct fib6_info *iter;
4527 if (!rt6_is_dead(rt))
4528 total += rt->fib6_nh->fib_nh_weight;
4530 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4531 if (!rt6_is_dead(iter))
4532 total += iter->fib6_nh->fib_nh_weight;
4538 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4540 int upper_bound = -1;
4542 if (!rt6_is_dead(rt)) {
4543 *weight += rt->fib6_nh->fib_nh_weight;
4544 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4547 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4550 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4552 struct fib6_info *iter;
4555 rt6_upper_bound_set(rt, &weight, total);
4557 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4558 rt6_upper_bound_set(iter, &weight, total);
4561 void rt6_multipath_rebalance(struct fib6_info *rt)
4563 struct fib6_info *first;
4566 /* In case the entire multipath route was marked for flushing,
4567 * then there is no need to rebalance upon the removal of every
4570 if (!rt->fib6_nsiblings || rt->should_flush)
4573 /* During lookup routes are evaluated in order, so we need to
4574 * make sure upper bounds are assigned from the first sibling
4577 first = rt6_multipath_first_sibling(rt);
4578 if (WARN_ON_ONCE(!first))
4581 total = rt6_multipath_total_weight(first);
4582 rt6_multipath_upper_bound_set(first, total);
4585 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4587 const struct arg_netdev_event *arg = p_arg;
4588 struct net *net = dev_net(arg->dev);
4590 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4591 rt->fib6_nh->fib_nh_dev == arg->dev) {
4592 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4593 fib6_update_sernum_upto_root(net, rt);
4594 rt6_multipath_rebalance(rt);
4600 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4602 struct arg_netdev_event arg = {
4605 .nh_flags = nh_flags,
4609 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4610 arg.nh_flags |= RTNH_F_LINKDOWN;
4612 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4615 /* only called for fib entries with inline fib6_nh */
4616 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4617 const struct net_device *dev)
4619 struct fib6_info *iter;
4621 if (rt->fib6_nh->fib_nh_dev == dev)
4623 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4624 if (iter->fib6_nh->fib_nh_dev == dev)
4630 static void rt6_multipath_flush(struct fib6_info *rt)
4632 struct fib6_info *iter;
4634 rt->should_flush = 1;
4635 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4636 iter->should_flush = 1;
4639 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4640 const struct net_device *down_dev)
4642 struct fib6_info *iter;
4643 unsigned int dead = 0;
4645 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4646 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4648 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4649 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4650 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4656 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4657 const struct net_device *dev,
4658 unsigned char nh_flags)
4660 struct fib6_info *iter;
4662 if (rt->fib6_nh->fib_nh_dev == dev)
4663 rt->fib6_nh->fib_nh_flags |= nh_flags;
4664 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4665 if (iter->fib6_nh->fib_nh_dev == dev)
4666 iter->fib6_nh->fib_nh_flags |= nh_flags;
4669 /* called with write lock held for table with rt */
4670 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4672 const struct arg_netdev_event *arg = p_arg;
4673 const struct net_device *dev = arg->dev;
4674 struct net *net = dev_net(dev);
4676 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4679 switch (arg->event) {
4680 case NETDEV_UNREGISTER:
4681 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4683 if (rt->should_flush)
4685 if (!rt->fib6_nsiblings)
4686 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4687 if (rt6_multipath_uses_dev(rt, dev)) {
4690 count = rt6_multipath_dead_count(rt, dev);
4691 if (rt->fib6_nsiblings + 1 == count) {
4692 rt6_multipath_flush(rt);
4695 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4697 fib6_update_sernum(net, rt);
4698 rt6_multipath_rebalance(rt);
4702 if (rt->fib6_nh->fib_nh_dev != dev ||
4703 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4705 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4706 rt6_multipath_rebalance(rt);
4713 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4715 struct arg_netdev_event arg = {
4721 struct net *net = dev_net(dev);
4723 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4724 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4726 fib6_clean_all(net, fib6_ifdown, &arg);
4729 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4731 rt6_sync_down_dev(dev, event);
4732 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4733 neigh_ifdown(&nd_tbl, dev);
4736 struct rt6_mtu_change_arg {
4737 struct net_device *dev;
4739 struct fib6_info *f6i;
4742 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4744 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4745 struct fib6_info *f6i = arg->f6i;
4747 /* For administrative MTU increase, there is no way to discover
4748 * IPv6 PMTU increase, so PMTU increase should be updated here.
4749 * Since RFC 1981 doesn't include administrative MTU increase
4750 * update PMTU increase is a MUST. (i.e. jumbo frame)
4752 if (nh->fib_nh_dev == arg->dev) {
4753 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4754 u32 mtu = f6i->fib6_pmtu;
4756 if (mtu >= arg->mtu ||
4757 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4758 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4760 spin_lock_bh(&rt6_exception_lock);
4761 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4762 spin_unlock_bh(&rt6_exception_lock);
4768 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4770 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4771 struct inet6_dev *idev;
4773 /* In IPv6 pmtu discovery is not optional,
4774 so that RTAX_MTU lock cannot disable it.
4775 We still use this lock to block changes
4776 caused by addrconf/ndisc.
4779 idev = __in6_dev_get(arg->dev);
4783 if (fib6_metric_locked(f6i, RTAX_MTU))
4788 /* fib6_nh_mtu_change only returns 0, so this is safe */
4789 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4793 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4796 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4798 struct rt6_mtu_change_arg arg = {
4803 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4806 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4807 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
4808 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4809 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4810 [RTA_OIF] = { .type = NLA_U32 },
4811 [RTA_IIF] = { .type = NLA_U32 },
4812 [RTA_PRIORITY] = { .type = NLA_U32 },
4813 [RTA_METRICS] = { .type = NLA_NESTED },
4814 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4815 [RTA_PREF] = { .type = NLA_U8 },
4816 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4817 [RTA_ENCAP] = { .type = NLA_NESTED },
4818 [RTA_EXPIRES] = { .type = NLA_U32 },
4819 [RTA_UID] = { .type = NLA_U32 },
4820 [RTA_MARK] = { .type = NLA_U32 },
4821 [RTA_TABLE] = { .type = NLA_U32 },
4822 [RTA_IP_PROTO] = { .type = NLA_U8 },
4823 [RTA_SPORT] = { .type = NLA_U16 },
4824 [RTA_DPORT] = { .type = NLA_U16 },
4825 [RTA_NH_ID] = { .type = NLA_U32 },
4828 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4829 struct fib6_config *cfg,
4830 struct netlink_ext_ack *extack)
4833 struct nlattr *tb[RTA_MAX+1];
4837 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4838 rtm_ipv6_policy, extack);
4843 rtm = nlmsg_data(nlh);
4845 *cfg = (struct fib6_config){
4846 .fc_table = rtm->rtm_table,
4847 .fc_dst_len = rtm->rtm_dst_len,
4848 .fc_src_len = rtm->rtm_src_len,
4850 .fc_protocol = rtm->rtm_protocol,
4851 .fc_type = rtm->rtm_type,
4853 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4854 .fc_nlinfo.nlh = nlh,
4855 .fc_nlinfo.nl_net = sock_net(skb->sk),
4858 if (rtm->rtm_type == RTN_UNREACHABLE ||
4859 rtm->rtm_type == RTN_BLACKHOLE ||
4860 rtm->rtm_type == RTN_PROHIBIT ||
4861 rtm->rtm_type == RTN_THROW)
4862 cfg->fc_flags |= RTF_REJECT;
4864 if (rtm->rtm_type == RTN_LOCAL)
4865 cfg->fc_flags |= RTF_LOCAL;
4867 if (rtm->rtm_flags & RTM_F_CLONED)
4868 cfg->fc_flags |= RTF_CACHE;
4870 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4872 if (tb[RTA_NH_ID]) {
4873 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4874 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4875 NL_SET_ERR_MSG(extack,
4876 "Nexthop specification and nexthop id are mutually exclusive");
4879 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4882 if (tb[RTA_GATEWAY]) {
4883 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4884 cfg->fc_flags |= RTF_GATEWAY;
4887 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4892 int plen = (rtm->rtm_dst_len + 7) >> 3;
4894 if (nla_len(tb[RTA_DST]) < plen)
4897 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4901 int plen = (rtm->rtm_src_len + 7) >> 3;
4903 if (nla_len(tb[RTA_SRC]) < plen)
4906 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4909 if (tb[RTA_PREFSRC])
4910 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4913 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4915 if (tb[RTA_PRIORITY])
4916 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4918 if (tb[RTA_METRICS]) {
4919 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4920 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4924 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4926 if (tb[RTA_MULTIPATH]) {
4927 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4928 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
4930 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4931 cfg->fc_mp_len, extack);
4937 pref = nla_get_u8(tb[RTA_PREF]);
4938 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4939 pref != ICMPV6_ROUTER_PREF_HIGH)
4940 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4941 cfg->fc_flags |= RTF_PREF(pref);
4945 cfg->fc_encap = tb[RTA_ENCAP];
4947 if (tb[RTA_ENCAP_TYPE]) {
4948 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4950 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
4955 if (tb[RTA_EXPIRES]) {
4956 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4958 if (addrconf_finite_timeout(timeout)) {
4959 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4960 cfg->fc_flags |= RTF_EXPIRES;
4970 struct fib6_info *fib6_info;
4971 struct fib6_config r_cfg;
4972 struct list_head next;
4975 static int ip6_route_info_append(struct net *net,
4976 struct list_head *rt6_nh_list,
4977 struct fib6_info *rt,
4978 struct fib6_config *r_cfg)
4983 list_for_each_entry(nh, rt6_nh_list, next) {
4984 /* check if fib6_info already exists */
4985 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
4989 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4993 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4994 list_add_tail(&nh->next, rt6_nh_list);
4999 static void ip6_route_mpath_notify(struct fib6_info *rt,
5000 struct fib6_info *rt_last,
5001 struct nl_info *info,
5004 /* if this is an APPEND route, then rt points to the first route
5005 * inserted and rt_last points to last route inserted. Userspace
5006 * wants a consistent dump of the route which starts at the first
5007 * nexthop. Since sibling routes are always added at the end of
5008 * the list, find the first sibling of the last route appended
5010 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5011 rt = list_first_entry(&rt_last->fib6_siblings,
5017 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5020 static int ip6_route_multipath_add(struct fib6_config *cfg,
5021 struct netlink_ext_ack *extack)
5023 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5024 struct nl_info *info = &cfg->fc_nlinfo;
5025 enum fib_event_type event_type;
5026 struct fib6_config r_cfg;
5027 struct rtnexthop *rtnh;
5028 struct fib6_info *rt;
5029 struct rt6_nh *err_nh;
5030 struct rt6_nh *nh, *nh_safe;
5036 int replace = (cfg->fc_nlinfo.nlh &&
5037 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5038 LIST_HEAD(rt6_nh_list);
5040 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5041 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5042 nlflags |= NLM_F_APPEND;
5044 remaining = cfg->fc_mp_len;
5045 rtnh = (struct rtnexthop *)cfg->fc_mp;
5047 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5048 * fib6_info structs per nexthop
5050 while (rtnh_ok(rtnh, remaining)) {
5051 memcpy(&r_cfg, cfg, sizeof(*cfg));
5052 if (rtnh->rtnh_ifindex)
5053 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5055 attrlen = rtnh_attrlen(rtnh);
5057 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5059 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5061 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5062 r_cfg.fc_flags |= RTF_GATEWAY;
5064 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5065 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5067 r_cfg.fc_encap_type = nla_get_u16(nla);
5070 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5071 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5077 if (!rt6_qualify_for_ecmp(rt)) {
5079 NL_SET_ERR_MSG(extack,
5080 "Device only routes can not be added for IPv6 using the multipath API.");
5081 fib6_info_release(rt);
5085 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5087 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5090 fib6_info_release(rt);
5094 rtnh = rtnh_next(rtnh, &remaining);
5097 if (list_empty(&rt6_nh_list)) {
5098 NL_SET_ERR_MSG(extack,
5099 "Invalid nexthop configuration - no valid nexthops");
5103 /* for add and replace send one notification with all nexthops.
5104 * Skip the notification in fib6_add_rt2node and send one with
5105 * the full route when done
5107 info->skip_notify = 1;
5109 /* For add and replace, send one notification with all nexthops. For
5110 * append, send one notification with all appended nexthops.
5112 info->skip_notify_kernel = 1;
5115 list_for_each_entry(nh, &rt6_nh_list, next) {
5116 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5117 fib6_info_release(nh->fib6_info);
5120 /* save reference to last route successfully inserted */
5121 rt_last = nh->fib6_info;
5123 /* save reference to first route for notification */
5125 rt_notif = nh->fib6_info;
5128 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5129 nh->fib6_info = NULL;
5132 NL_SET_ERR_MSG_MOD(extack,
5133 "multipath route replace failed (check consistency of installed routes)");
5138 /* Because each route is added like a single route we remove
5139 * these flags after the first nexthop: if there is a collision,
5140 * we have already failed to add the first nexthop:
5141 * fib6_add_rt2node() has rejected it; when replacing, old
5142 * nexthops have been replaced by first new, the rest should
5145 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5150 event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
5151 err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
5152 rt_notif, nhn - 1, extack);
5154 /* Delete all the siblings that were just added */
5159 /* success ... tell user about new route */
5160 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5164 /* send notification for routes that were added so that
5165 * the delete notifications sent by ip6_route_del are
5169 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5171 /* Delete routes that were already added */
5172 list_for_each_entry(nh, &rt6_nh_list, next) {
5175 ip6_route_del(&nh->r_cfg, extack);
5179 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5181 fib6_info_release(nh->fib6_info);
5182 list_del(&nh->next);
5189 static int ip6_route_multipath_del(struct fib6_config *cfg,
5190 struct netlink_ext_ack *extack)
5192 struct fib6_config r_cfg;
5193 struct rtnexthop *rtnh;
5196 int err = 1, last_err = 0;
5198 remaining = cfg->fc_mp_len;
5199 rtnh = (struct rtnexthop *)cfg->fc_mp;
5201 /* Parse a Multipath Entry */
5202 while (rtnh_ok(rtnh, remaining)) {
5203 memcpy(&r_cfg, cfg, sizeof(*cfg));
5204 if (rtnh->rtnh_ifindex)
5205 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5207 attrlen = rtnh_attrlen(rtnh);
5209 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5211 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5213 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5214 r_cfg.fc_flags |= RTF_GATEWAY;
5217 err = ip6_route_del(&r_cfg, extack);
5221 rtnh = rtnh_next(rtnh, &remaining);
5227 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5228 struct netlink_ext_ack *extack)
5230 struct fib6_config cfg;
5233 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5238 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5239 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5244 return ip6_route_multipath_del(&cfg, extack);
5246 cfg.fc_delete_all_nh = 1;
5247 return ip6_route_del(&cfg, extack);
5251 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5252 struct netlink_ext_ack *extack)
5254 struct fib6_config cfg;
5257 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5261 if (cfg.fc_metric == 0)
5262 cfg.fc_metric = IP6_RT_PRIO_USER;
5265 return ip6_route_multipath_add(&cfg, extack);
5267 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5270 /* add the overhead of this fib6_nh to nexthop_len */
5271 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5273 int *nexthop_len = arg;
5275 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5276 + NLA_ALIGN(sizeof(struct rtnexthop))
5277 + nla_total_size(16); /* RTA_GATEWAY */
5279 if (nh->fib_nh_lws) {
5280 /* RTA_ENCAP_TYPE */
5281 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5283 *nexthop_len += nla_total_size(2);
5289 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5294 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5295 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5298 struct fib6_nh *nh = f6i->fib6_nh;
5301 if (f6i->fib6_nsiblings) {
5302 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5303 + NLA_ALIGN(sizeof(struct rtnexthop))
5304 + nla_total_size(16) /* RTA_GATEWAY */
5305 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5307 nexthop_len *= f6i->fib6_nsiblings;
5309 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5312 return NLMSG_ALIGN(sizeof(struct rtmsg))
5313 + nla_total_size(16) /* RTA_SRC */
5314 + nla_total_size(16) /* RTA_DST */
5315 + nla_total_size(16) /* RTA_GATEWAY */
5316 + nla_total_size(16) /* RTA_PREFSRC */
5317 + nla_total_size(4) /* RTA_TABLE */
5318 + nla_total_size(4) /* RTA_IIF */
5319 + nla_total_size(4) /* RTA_OIF */
5320 + nla_total_size(4) /* RTA_PRIORITY */
5321 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5322 + nla_total_size(sizeof(struct rta_cacheinfo))
5323 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5324 + nla_total_size(1) /* RTA_PREF */
5328 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5329 unsigned char *flags)
5331 if (nexthop_is_multipath(nh)) {
5334 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5336 goto nla_put_failure;
5338 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5339 goto nla_put_failure;
5341 nla_nest_end(skb, mp);
5343 struct fib6_nh *fib6_nh;
5345 fib6_nh = nexthop_fib6_nh(nh);
5346 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5348 goto nla_put_failure;
5357 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5358 struct fib6_info *rt, struct dst_entry *dst,
5359 struct in6_addr *dest, struct in6_addr *src,
5360 int iif, int type, u32 portid, u32 seq,
5363 struct rt6_info *rt6 = (struct rt6_info *)dst;
5364 struct rt6key *rt6_dst, *rt6_src;
5365 u32 *pmetrics, table, rt6_flags;
5366 unsigned char nh_flags = 0;
5367 struct nlmsghdr *nlh;
5371 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5376 rt6_dst = &rt6->rt6i_dst;
5377 rt6_src = &rt6->rt6i_src;
5378 rt6_flags = rt6->rt6i_flags;
5380 rt6_dst = &rt->fib6_dst;
5381 rt6_src = &rt->fib6_src;
5382 rt6_flags = rt->fib6_flags;
5385 rtm = nlmsg_data(nlh);
5386 rtm->rtm_family = AF_INET6;
5387 rtm->rtm_dst_len = rt6_dst->plen;
5388 rtm->rtm_src_len = rt6_src->plen;
5391 table = rt->fib6_table->tb6_id;
5393 table = RT6_TABLE_UNSPEC;
5394 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5395 if (nla_put_u32(skb, RTA_TABLE, table))
5396 goto nla_put_failure;
5398 rtm->rtm_type = rt->fib6_type;
5400 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5401 rtm->rtm_protocol = rt->fib6_protocol;
5403 if (rt6_flags & RTF_CACHE)
5404 rtm->rtm_flags |= RTM_F_CLONED;
5407 if (nla_put_in6_addr(skb, RTA_DST, dest))
5408 goto nla_put_failure;
5409 rtm->rtm_dst_len = 128;
5410 } else if (rtm->rtm_dst_len)
5411 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5412 goto nla_put_failure;
5413 #ifdef CONFIG_IPV6_SUBTREES
5415 if (nla_put_in6_addr(skb, RTA_SRC, src))
5416 goto nla_put_failure;
5417 rtm->rtm_src_len = 128;
5418 } else if (rtm->rtm_src_len &&
5419 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5420 goto nla_put_failure;
5423 #ifdef CONFIG_IPV6_MROUTE
5424 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5425 int err = ip6mr_get_route(net, skb, rtm, portid);
5430 goto nla_put_failure;
5433 if (nla_put_u32(skb, RTA_IIF, iif))
5434 goto nla_put_failure;
5436 struct in6_addr saddr_buf;
5437 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5438 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5439 goto nla_put_failure;
5442 if (rt->fib6_prefsrc.plen) {
5443 struct in6_addr saddr_buf;
5444 saddr_buf = rt->fib6_prefsrc.addr;
5445 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5446 goto nla_put_failure;
5449 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5450 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5451 goto nla_put_failure;
5453 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5454 goto nla_put_failure;
5456 /* For multipath routes, walk the siblings list and add
5457 * each as a nexthop within RTA_MULTIPATH.
5460 if (rt6_flags & RTF_GATEWAY &&
5461 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5462 goto nla_put_failure;
5464 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5465 goto nla_put_failure;
5466 } else if (rt->fib6_nsiblings) {
5467 struct fib6_info *sibling, *next_sibling;
5470 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5472 goto nla_put_failure;
5474 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5475 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5476 goto nla_put_failure;
5478 list_for_each_entry_safe(sibling, next_sibling,
5479 &rt->fib6_siblings, fib6_siblings) {
5480 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5481 sibling->fib6_nh->fib_nh_weight,
5483 goto nla_put_failure;
5486 nla_nest_end(skb, mp);
5487 } else if (rt->nh) {
5488 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5489 goto nla_put_failure;
5491 if (nexthop_is_blackhole(rt->nh))
5492 rtm->rtm_type = RTN_BLACKHOLE;
5494 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5495 goto nla_put_failure;
5497 rtm->rtm_flags |= nh_flags;
5499 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5500 &nh_flags, false) < 0)
5501 goto nla_put_failure;
5503 rtm->rtm_flags |= nh_flags;
5506 if (rt6_flags & RTF_EXPIRES) {
5507 expires = dst ? dst->expires : rt->expires;
5511 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5512 goto nla_put_failure;
5514 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5515 goto nla_put_failure;
5518 nlmsg_end(skb, nlh);
5522 nlmsg_cancel(skb, nlh);
5526 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5528 const struct net_device *dev = arg;
5530 if (nh->fib_nh_dev == dev)
5536 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5537 const struct net_device *dev)
5540 struct net_device *_dev = (struct net_device *)dev;
5542 return !!nexthop_for_each_fib6_nh(f6i->nh,
5543 fib6_info_nh_uses_dev,
5547 if (f6i->fib6_nh->fib_nh_dev == dev)
5550 if (f6i->fib6_nsiblings) {
5551 struct fib6_info *sibling, *next_sibling;
5553 list_for_each_entry_safe(sibling, next_sibling,
5554 &f6i->fib6_siblings, fib6_siblings) {
5555 if (sibling->fib6_nh->fib_nh_dev == dev)
5563 struct fib6_nh_exception_dump_walker {
5564 struct rt6_rtnl_dump_arg *dump;
5565 struct fib6_info *rt;
5571 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5573 struct fib6_nh_exception_dump_walker *w = arg;
5574 struct rt6_rtnl_dump_arg *dump = w->dump;
5575 struct rt6_exception_bucket *bucket;
5576 struct rt6_exception *rt6_ex;
5579 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5583 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5584 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5590 /* Expiration of entries doesn't bump sernum, insertion
5591 * does. Removal is triggered by insertion, so we can
5592 * rely on the fact that if entries change between two
5593 * partial dumps, this node is scanned again completely,
5594 * see rt6_insert_exception() and fib6_dump_table().
5596 * Count expired entries we go through as handled
5597 * entries that we'll skip next time, in case of partial
5598 * node dump. Otherwise, if entries expire meanwhile,
5599 * we'll skip the wrong amount.
5601 if (rt6_check_expired(rt6_ex->rt6i)) {
5606 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5607 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5609 NETLINK_CB(dump->cb->skb).portid,
5610 dump->cb->nlh->nlmsg_seq, w->flags);
5622 /* Return -1 if done with node, number of handled routes on partial dump */
5623 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5625 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5626 struct fib_dump_filter *filter = &arg->filter;
5627 unsigned int flags = NLM_F_MULTI;
5628 struct net *net = arg->net;
5631 if (rt == net->ipv6.fib6_null_entry)
5634 if ((filter->flags & RTM_F_PREFIX) &&
5635 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5636 /* success since this is not a prefix route */
5639 if (filter->filter_set &&
5640 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5641 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5642 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5646 if (filter->filter_set ||
5647 !filter->dump_routes || !filter->dump_exceptions) {
5648 flags |= NLM_F_DUMP_FILTERED;
5651 if (filter->dump_routes) {
5655 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5657 NETLINK_CB(arg->cb->skb).portid,
5658 arg->cb->nlh->nlmsg_seq, flags)) {
5665 if (filter->dump_exceptions) {
5666 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5675 err = nexthop_for_each_fib6_nh(rt->nh,
5676 rt6_nh_dump_exceptions,
5679 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5684 return count += w.count;
5690 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5691 const struct nlmsghdr *nlh,
5693 struct netlink_ext_ack *extack)
5698 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5699 NL_SET_ERR_MSG_MOD(extack,
5700 "Invalid header for get route request");
5704 if (!netlink_strict_get_check(skb))
5705 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5706 rtm_ipv6_policy, extack);
5708 rtm = nlmsg_data(nlh);
5709 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5710 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5711 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5713 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5716 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5717 NL_SET_ERR_MSG_MOD(extack,
5718 "Invalid flags for get route request");
5722 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5723 rtm_ipv6_policy, extack);
5727 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5728 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5729 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5733 for (i = 0; i <= RTA_MAX; i++) {
5749 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5757 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5758 struct netlink_ext_ack *extack)
5760 struct net *net = sock_net(in_skb->sk);
5761 struct nlattr *tb[RTA_MAX+1];
5762 int err, iif = 0, oif = 0;
5763 struct fib6_info *from;
5764 struct dst_entry *dst;
5765 struct rt6_info *rt;
5766 struct sk_buff *skb;
5768 struct flowi6 fl6 = {};
5771 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5776 rtm = nlmsg_data(nlh);
5777 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5778 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5781 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5784 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5788 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5791 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5795 iif = nla_get_u32(tb[RTA_IIF]);
5798 oif = nla_get_u32(tb[RTA_OIF]);
5801 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5804 fl6.flowi6_uid = make_kuid(current_user_ns(),
5805 nla_get_u32(tb[RTA_UID]));
5807 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5810 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5813 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5815 if (tb[RTA_IP_PROTO]) {
5816 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
5817 &fl6.flowi6_proto, AF_INET6,
5824 struct net_device *dev;
5829 dev = dev_get_by_index_rcu(net, iif);
5836 fl6.flowi6_iif = iif;
5838 if (!ipv6_addr_any(&fl6.saddr))
5839 flags |= RT6_LOOKUP_F_HAS_SADDR;
5841 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5845 fl6.flowi6_oif = oif;
5847 dst = ip6_route_output(net, NULL, &fl6);
5851 rt = container_of(dst, struct rt6_info, dst);
5852 if (rt->dst.error) {
5853 err = rt->dst.error;
5858 if (rt == net->ipv6.ip6_null_entry) {
5859 err = rt->dst.error;
5864 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
5871 skb_dst_set(skb, &rt->dst);
5874 from = rcu_dereference(rt->from);
5877 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5879 NETLINK_CB(in_skb).portid,
5882 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5883 &fl6.saddr, iif, RTM_NEWROUTE,
5884 NETLINK_CB(in_skb).portid,
5896 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5901 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
5902 unsigned int nlm_flags)
5904 struct sk_buff *skb;
5905 struct net *net = info->nl_net;
5910 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5912 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5916 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5917 event, info->portid, seq, nlm_flags);
5919 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5920 WARN_ON(err == -EMSGSIZE);
5924 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5925 info->nlh, gfp_any());
5929 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5932 void fib6_rt_update(struct net *net, struct fib6_info *rt,
5933 struct nl_info *info)
5935 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5936 struct sk_buff *skb;
5939 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5940 * is implemented and supported for nexthop objects
5942 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5944 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5948 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5949 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5951 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5952 WARN_ON(err == -EMSGSIZE);
5956 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5957 info->nlh, gfp_any());
5961 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5964 static int ip6_route_dev_notify(struct notifier_block *this,
5965 unsigned long event, void *ptr)
5967 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5968 struct net *net = dev_net(dev);
5970 if (!(dev->flags & IFF_LOOPBACK))
5973 if (event == NETDEV_REGISTER) {
5974 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
5975 net->ipv6.ip6_null_entry->dst.dev = dev;
5976 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5977 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5978 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
5979 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5980 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
5981 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5983 } else if (event == NETDEV_UNREGISTER &&
5984 dev->reg_state != NETREG_UNREGISTERED) {
5985 /* NETDEV_UNREGISTER could be fired for multiple times by
5986 * netdev_wait_allrefs(). Make sure we only call this once.
5988 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5989 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5990 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5991 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6002 #ifdef CONFIG_PROC_FS
6003 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6005 struct net *net = (struct net *)seq->private;
6006 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6007 net->ipv6.rt6_stats->fib_nodes,
6008 net->ipv6.rt6_stats->fib_route_nodes,
6009 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6010 net->ipv6.rt6_stats->fib_rt_entries,
6011 net->ipv6.rt6_stats->fib_rt_cache,
6012 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6013 net->ipv6.rt6_stats->fib_discarded_routes);
6017 #endif /* CONFIG_PROC_FS */
6019 #ifdef CONFIG_SYSCTL
6022 int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6023 void __user *buffer, size_t *lenp, loff_t *ppos)
6031 net = (struct net *)ctl->extra1;
6032 delay = net->ipv6.sysctl.flush_delay;
6033 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6037 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6041 static struct ctl_table ipv6_route_table_template[] = {
6043 .procname = "flush",
6044 .data = &init_net.ipv6.sysctl.flush_delay,
6045 .maxlen = sizeof(int),
6047 .proc_handler = ipv6_sysctl_rtcache_flush
6050 .procname = "gc_thresh",
6051 .data = &ip6_dst_ops_template.gc_thresh,
6052 .maxlen = sizeof(int),
6054 .proc_handler = proc_dointvec,
6057 .procname = "max_size",
6058 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6059 .maxlen = sizeof(int),
6061 .proc_handler = proc_dointvec,
6064 .procname = "gc_min_interval",
6065 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6066 .maxlen = sizeof(int),
6068 .proc_handler = proc_dointvec_jiffies,
6071 .procname = "gc_timeout",
6072 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6073 .maxlen = sizeof(int),
6075 .proc_handler = proc_dointvec_jiffies,
6078 .procname = "gc_interval",
6079 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6080 .maxlen = sizeof(int),
6082 .proc_handler = proc_dointvec_jiffies,
6085 .procname = "gc_elasticity",
6086 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6087 .maxlen = sizeof(int),
6089 .proc_handler = proc_dointvec,
6092 .procname = "mtu_expires",
6093 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6094 .maxlen = sizeof(int),
6096 .proc_handler = proc_dointvec_jiffies,
6099 .procname = "min_adv_mss",
6100 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6101 .maxlen = sizeof(int),
6103 .proc_handler = proc_dointvec,
6106 .procname = "gc_min_interval_ms",
6107 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6108 .maxlen = sizeof(int),
6110 .proc_handler = proc_dointvec_ms_jiffies,
6113 .procname = "skip_notify_on_dev_down",
6114 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6115 .maxlen = sizeof(int),
6117 .proc_handler = proc_dointvec_minmax,
6118 .extra1 = SYSCTL_ZERO,
6119 .extra2 = SYSCTL_ONE,
6124 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6126 struct ctl_table *table;
6128 table = kmemdup(ipv6_route_table_template,
6129 sizeof(ipv6_route_table_template),
6133 table[0].data = &net->ipv6.sysctl.flush_delay;
6134 table[0].extra1 = net;
6135 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6136 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6137 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6138 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6139 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6140 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6141 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6142 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6143 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6144 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6146 /* Don't export sysctls to unprivileged users */
6147 if (net->user_ns != &init_user_ns)
6148 table[0].procname = NULL;
6155 static int __net_init ip6_route_net_init(struct net *net)
6159 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6160 sizeof(net->ipv6.ip6_dst_ops));
6162 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6163 goto out_ip6_dst_ops;
6165 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6166 if (!net->ipv6.fib6_null_entry)
6167 goto out_ip6_dst_entries;
6168 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6169 sizeof(*net->ipv6.fib6_null_entry));
6171 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6172 sizeof(*net->ipv6.ip6_null_entry),
6174 if (!net->ipv6.ip6_null_entry)
6175 goto out_fib6_null_entry;
6176 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6177 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6178 ip6_template_metrics, true);
6179 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
6181 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6182 net->ipv6.fib6_has_custom_rules = false;
6183 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6184 sizeof(*net->ipv6.ip6_prohibit_entry),
6186 if (!net->ipv6.ip6_prohibit_entry)
6187 goto out_ip6_null_entry;
6188 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6189 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6190 ip6_template_metrics, true);
6191 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
6193 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6194 sizeof(*net->ipv6.ip6_blk_hole_entry),
6196 if (!net->ipv6.ip6_blk_hole_entry)
6197 goto out_ip6_prohibit_entry;
6198 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6199 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6200 ip6_template_metrics, true);
6201 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6202 #ifdef CONFIG_IPV6_SUBTREES
6203 net->ipv6.fib6_routes_require_src = 0;
6207 net->ipv6.sysctl.flush_delay = 0;
6208 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6209 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6210 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6211 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6212 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6213 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6214 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6215 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6217 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6223 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6224 out_ip6_prohibit_entry:
6225 kfree(net->ipv6.ip6_prohibit_entry);
6227 kfree(net->ipv6.ip6_null_entry);
6229 out_fib6_null_entry:
6230 kfree(net->ipv6.fib6_null_entry);
6231 out_ip6_dst_entries:
6232 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6237 static void __net_exit ip6_route_net_exit(struct net *net)
6239 kfree(net->ipv6.fib6_null_entry);
6240 kfree(net->ipv6.ip6_null_entry);
6241 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6242 kfree(net->ipv6.ip6_prohibit_entry);
6243 kfree(net->ipv6.ip6_blk_hole_entry);
6245 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6248 static int __net_init ip6_route_net_init_late(struct net *net)
6250 #ifdef CONFIG_PROC_FS
6251 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6252 sizeof(struct ipv6_route_iter));
6253 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6254 rt6_stats_seq_show, NULL);
6259 static void __net_exit ip6_route_net_exit_late(struct net *net)
6261 #ifdef CONFIG_PROC_FS
6262 remove_proc_entry("ipv6_route", net->proc_net);
6263 remove_proc_entry("rt6_stats", net->proc_net);
6267 static struct pernet_operations ip6_route_net_ops = {
6268 .init = ip6_route_net_init,
6269 .exit = ip6_route_net_exit,
6272 static int __net_init ipv6_inetpeer_init(struct net *net)
6274 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6278 inet_peer_base_init(bp);
6279 net->ipv6.peers = bp;
6283 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6285 struct inet_peer_base *bp = net->ipv6.peers;
6287 net->ipv6.peers = NULL;
6288 inetpeer_invalidate_tree(bp);
6292 static struct pernet_operations ipv6_inetpeer_ops = {
6293 .init = ipv6_inetpeer_init,
6294 .exit = ipv6_inetpeer_exit,
6297 static struct pernet_operations ip6_route_net_late_ops = {
6298 .init = ip6_route_net_init_late,
6299 .exit = ip6_route_net_exit_late,
6302 static struct notifier_block ip6_route_dev_notifier = {
6303 .notifier_call = ip6_route_dev_notify,
6304 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6307 void __init ip6_route_init_special_entries(void)
6309 /* Registering of the loopback is done before this portion of code,
6310 * the loopback reference in rt6_info will not be taken, do it
6311 * manually for init_net */
6312 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6313 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6314 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6315 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6316 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6317 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6318 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6319 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6323 int __init ip6_route_init(void)
6329 ip6_dst_ops_template.kmem_cachep =
6330 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6331 SLAB_HWCACHE_ALIGN, NULL);
6332 if (!ip6_dst_ops_template.kmem_cachep)
6335 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6337 goto out_kmem_cache;
6339 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6341 goto out_dst_entries;
6343 ret = register_pernet_subsys(&ip6_route_net_ops);
6345 goto out_register_inetpeer;
6347 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6351 goto out_register_subsys;
6357 ret = fib6_rules_init();
6361 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6363 goto fib6_rules_init;
6365 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6366 inet6_rtm_newroute, NULL, 0);
6368 goto out_register_late_subsys;
6370 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6371 inet6_rtm_delroute, NULL, 0);
6373 goto out_register_late_subsys;
6375 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6376 inet6_rtm_getroute, NULL,
6377 RTNL_FLAG_DOIT_UNLOCKED);
6379 goto out_register_late_subsys;
6381 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6383 goto out_register_late_subsys;
6385 for_each_possible_cpu(cpu) {
6386 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6388 INIT_LIST_HEAD(&ul->head);
6389 spin_lock_init(&ul->lock);
6395 out_register_late_subsys:
6396 rtnl_unregister_all(PF_INET6);
6397 unregister_pernet_subsys(&ip6_route_net_late_ops);
6399 fib6_rules_cleanup();
6404 out_register_subsys:
6405 unregister_pernet_subsys(&ip6_route_net_ops);
6406 out_register_inetpeer:
6407 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6409 dst_entries_destroy(&ip6_dst_blackhole_ops);
6411 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6415 void ip6_route_cleanup(void)
6417 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6418 unregister_pernet_subsys(&ip6_route_net_late_ops);
6419 fib6_rules_cleanup();
6422 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6423 unregister_pernet_subsys(&ip6_route_net_ops);
6424 dst_entries_destroy(&ip6_dst_blackhole_ops);
6425 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);