1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
12 * YOSHIFUJI Hideaki @USAGI
13 * reworked default router selection.
14 * - respect outgoing interface
15 * - select from (probably) reachable routers (i.e.
16 * routers in REACHABLE, STALE, DELAY or PROBE states).
17 * - always select the same router if it is (probably)
18 * reachable. otherwise, round-robin the list.
20 * Fixed routing subtrees.
23 #define pr_fmt(fmt) "IPv6: " fmt
25 #include <linux/capability.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/times.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/route.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/mroute6.h>
37 #include <linux/init.h>
38 #include <linux/if_arp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/nsproxy.h>
42 #include <linux/slab.h>
43 #include <linux/jhash.h>
44 #include <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,
100 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
101 struct sk_buff *skb);
102 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
104 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
105 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
106 struct fib6_info *rt, struct dst_entry *dst,
107 struct in6_addr *dest, struct in6_addr *src,
108 int iif, int type, u32 portid, u32 seq,
110 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
111 const struct in6_addr *daddr,
112 const struct in6_addr *saddr);
114 #ifdef CONFIG_IPV6_ROUTE_INFO
115 static struct fib6_info *rt6_add_route_info(struct net *net,
116 const struct in6_addr *prefix, int prefixlen,
117 const struct in6_addr *gwaddr,
118 struct net_device *dev,
120 static struct fib6_info *rt6_get_route_info(struct net *net,
121 const struct in6_addr *prefix, int prefixlen,
122 const struct in6_addr *gwaddr,
123 struct net_device *dev);
126 struct uncached_list {
128 struct list_head head;
131 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
133 void rt6_uncached_list_add(struct rt6_info *rt)
135 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
137 rt->rt6i_uncached_list = ul;
139 spin_lock_bh(&ul->lock);
140 list_add_tail(&rt->rt6i_uncached, &ul->head);
141 spin_unlock_bh(&ul->lock);
144 void rt6_uncached_list_del(struct rt6_info *rt)
146 if (!list_empty(&rt->rt6i_uncached)) {
147 struct uncached_list *ul = rt->rt6i_uncached_list;
148 struct net *net = dev_net(rt->dst.dev);
150 spin_lock_bh(&ul->lock);
151 list_del(&rt->rt6i_uncached);
152 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
153 spin_unlock_bh(&ul->lock);
157 static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
159 struct net_device *loopback_dev = net->loopback_dev;
162 if (dev == loopback_dev)
165 for_each_possible_cpu(cpu) {
166 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
169 spin_lock_bh(&ul->lock);
170 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
171 struct inet6_dev *rt_idev = rt->rt6i_idev;
172 struct net_device *rt_dev = rt->dst.dev;
174 if (rt_idev->dev == dev) {
175 rt->rt6i_idev = in6_dev_get(loopback_dev);
176 in6_dev_put(rt_idev);
180 rt->dst.dev = blackhole_netdev;
181 dev_hold(rt->dst.dev);
185 spin_unlock_bh(&ul->lock);
189 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
193 if (!ipv6_addr_any(p))
194 return (const void *) p;
196 return &ipv6_hdr(skb)->daddr;
200 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201 struct net_device *dev,
207 daddr = choose_neigh_daddr(gw, skb, daddr);
208 n = __ipv6_neigh_lookup(dev, daddr);
212 n = neigh_create(&nd_tbl, daddr, dev);
213 return IS_ERR(n) ? NULL : n;
216 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
222 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
223 dst->dev, skb, daddr);
226 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
228 struct net_device *dev = dst->dev;
229 struct rt6_info *rt = (struct rt6_info *)dst;
231 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
234 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
236 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
238 __ipv6_confirm_neigh(dev, daddr);
241 static struct dst_ops ip6_dst_ops_template = {
245 .check = ip6_dst_check,
246 .default_advmss = ip6_default_advmss,
248 .cow_metrics = dst_cow_metrics_generic,
249 .destroy = ip6_dst_destroy,
250 .ifdown = ip6_dst_ifdown,
251 .negative_advice = ip6_negative_advice,
252 .link_failure = ip6_link_failure,
253 .update_pmtu = ip6_rt_update_pmtu,
254 .redirect = rt6_do_redirect,
255 .local_out = __ip6_local_out,
256 .neigh_lookup = ip6_dst_neigh_lookup,
257 .confirm_neigh = ip6_confirm_neigh,
260 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
262 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
264 return mtu ? : dst->dev->mtu;
267 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
268 struct sk_buff *skb, u32 mtu,
273 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
278 static struct dst_ops ip6_dst_blackhole_ops = {
280 .destroy = ip6_dst_destroy,
281 .check = ip6_dst_check,
282 .mtu = ip6_blackhole_mtu,
283 .default_advmss = ip6_default_advmss,
284 .update_pmtu = ip6_rt_blackhole_update_pmtu,
285 .redirect = ip6_rt_blackhole_redirect,
286 .cow_metrics = dst_cow_metrics_generic,
287 .neigh_lookup = ip6_dst_neigh_lookup,
290 static const u32 ip6_template_metrics[RTAX_MAX] = {
291 [RTAX_HOPLIMIT - 1] = 0,
294 static const struct fib6_info fib6_null_entry_template = {
295 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
296 .fib6_protocol = RTPROT_KERNEL,
297 .fib6_metric = ~(u32)0,
298 .fib6_ref = REFCOUNT_INIT(1),
299 .fib6_type = RTN_UNREACHABLE,
300 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
303 static const struct rt6_info ip6_null_entry_template = {
305 .__refcnt = ATOMIC_INIT(1),
307 .obsolete = DST_OBSOLETE_FORCE_CHK,
308 .error = -ENETUNREACH,
309 .input = ip6_pkt_discard,
310 .output = ip6_pkt_discard_out,
312 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
315 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
317 static const struct rt6_info ip6_prohibit_entry_template = {
319 .__refcnt = ATOMIC_INIT(1),
321 .obsolete = DST_OBSOLETE_FORCE_CHK,
323 .input = ip6_pkt_prohibit,
324 .output = ip6_pkt_prohibit_out,
326 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
329 static const struct rt6_info ip6_blk_hole_entry_template = {
331 .__refcnt = ATOMIC_INIT(1),
333 .obsolete = DST_OBSOLETE_FORCE_CHK,
335 .input = dst_discard,
336 .output = dst_discard_out,
338 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
343 static void rt6_info_init(struct rt6_info *rt)
345 struct dst_entry *dst = &rt->dst;
347 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
348 INIT_LIST_HEAD(&rt->rt6i_uncached);
351 /* allocate dst with ip6_dst_ops */
352 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
355 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
356 1, DST_OBSOLETE_FORCE_CHK, flags);
360 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
365 EXPORT_SYMBOL(ip6_dst_alloc);
367 static void ip6_dst_destroy(struct dst_entry *dst)
369 struct rt6_info *rt = (struct rt6_info *)dst;
370 struct fib6_info *from;
371 struct inet6_dev *idev;
373 ip_dst_metrics_put(dst);
374 rt6_uncached_list_del(rt);
376 idev = rt->rt6i_idev;
378 rt->rt6i_idev = NULL;
382 from = xchg((__force struct fib6_info **)&rt->from, NULL);
383 fib6_info_release(from);
386 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
389 struct rt6_info *rt = (struct rt6_info *)dst;
390 struct inet6_dev *idev = rt->rt6i_idev;
391 struct net_device *loopback_dev =
392 dev_net(dev)->loopback_dev;
394 if (idev && idev->dev != loopback_dev) {
395 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
397 rt->rt6i_idev = loopback_idev;
403 static bool __rt6_check_expired(const struct rt6_info *rt)
405 if (rt->rt6i_flags & RTF_EXPIRES)
406 return time_after(jiffies, rt->dst.expires);
411 static bool rt6_check_expired(const struct rt6_info *rt)
413 struct fib6_info *from;
415 from = rcu_dereference(rt->from);
417 if (rt->rt6i_flags & RTF_EXPIRES) {
418 if (time_after(jiffies, rt->dst.expires))
421 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
422 fib6_check_expired(from);
427 void fib6_select_path(const struct net *net, struct fib6_result *res,
428 struct flowi6 *fl6, int oif, bool have_oif_match,
429 const struct sk_buff *skb, int strict)
431 struct fib6_info *sibling, *next_sibling;
432 struct fib6_info *match = res->f6i;
434 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
437 if (match->nh && have_oif_match && res->nh)
440 /* We might have already computed the hash for ICMPv6 errors. In such
441 * case it will always be non-zero. Otherwise now is the time to do it.
444 (!match->nh || nexthop_is_multipath(match->nh)))
445 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
447 if (unlikely(match->nh)) {
448 nexthop_path_fib6_result(res, fl6->mp_hash);
452 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
455 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
457 const struct fib6_nh *nh = sibling->fib6_nh;
460 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
461 if (fl6->mp_hash > nh_upper_bound)
463 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
471 res->nh = match->fib6_nh;
475 * Route lookup. rcu_read_lock() should be held.
478 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
479 const struct in6_addr *saddr, int oif, int flags)
481 const struct net_device *dev;
483 if (nh->fib_nh_flags & RTNH_F_DEAD)
486 dev = nh->fib_nh_dev;
488 if (dev->ifindex == oif)
491 if (ipv6_chk_addr(net, saddr, dev,
492 flags & RT6_LOOKUP_F_IFACE))
499 struct fib6_nh_dm_arg {
501 const struct in6_addr *saddr;
507 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
509 struct fib6_nh_dm_arg *arg = _arg;
512 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
516 /* returns fib6_nh from nexthop or NULL */
517 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
518 struct fib6_result *res,
519 const struct in6_addr *saddr,
522 struct fib6_nh_dm_arg arg = {
529 if (nexthop_is_blackhole(nh))
532 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
538 static void rt6_device_match(struct net *net, struct fib6_result *res,
539 const struct in6_addr *saddr, int oif, int flags)
541 struct fib6_info *f6i = res->f6i;
542 struct fib6_info *spf6i;
545 if (!oif && ipv6_addr_any(saddr)) {
546 if (unlikely(f6i->nh)) {
547 nh = nexthop_fib6_nh(f6i->nh);
548 if (nexthop_is_blackhole(f6i->nh))
553 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
557 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
558 bool matched = false;
560 if (unlikely(spf6i->nh)) {
561 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
567 if (__rt6_device_match(net, nh, saddr, oif, flags))
576 if (oif && flags & RT6_LOOKUP_F_IFACE) {
577 res->f6i = net->ipv6.fib6_null_entry;
578 nh = res->f6i->fib6_nh;
582 if (unlikely(f6i->nh)) {
583 nh = nexthop_fib6_nh(f6i->nh);
584 if (nexthop_is_blackhole(f6i->nh))
590 if (nh->fib_nh_flags & RTNH_F_DEAD) {
591 res->f6i = net->ipv6.fib6_null_entry;
592 nh = res->f6i->fib6_nh;
596 res->fib6_type = res->f6i->fib6_type;
597 res->fib6_flags = res->f6i->fib6_flags;
601 res->fib6_flags |= RTF_REJECT;
602 res->fib6_type = RTN_BLACKHOLE;
606 #ifdef CONFIG_IPV6_ROUTER_PREF
607 struct __rt6_probe_work {
608 struct work_struct work;
609 struct in6_addr target;
610 struct net_device *dev;
613 static void rt6_probe_deferred(struct work_struct *w)
615 struct in6_addr mcaddr;
616 struct __rt6_probe_work *work =
617 container_of(w, struct __rt6_probe_work, work);
619 addrconf_addr_solict_mult(&work->target, &mcaddr);
620 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
625 static void rt6_probe(struct fib6_nh *fib6_nh)
627 struct __rt6_probe_work *work = NULL;
628 const struct in6_addr *nh_gw;
629 unsigned long last_probe;
630 struct neighbour *neigh;
631 struct net_device *dev;
632 struct inet6_dev *idev;
635 * Okay, this does not seem to be appropriate
636 * for now, however, we need to check if it
637 * is really so; aka Router Reachability Probing.
639 * Router Reachability Probe MUST be rate-limited
640 * to no more than one per minute.
642 if (!fib6_nh->fib_nh_gw_family)
645 nh_gw = &fib6_nh->fib_nh_gw6;
646 dev = fib6_nh->fib_nh_dev;
648 last_probe = READ_ONCE(fib6_nh->last_probe);
649 idev = __in6_dev_get(dev);
650 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
652 if (neigh->nud_state & NUD_VALID)
655 write_lock(&neigh->lock);
656 if (!(neigh->nud_state & NUD_VALID) &&
658 neigh->updated + idev->cnf.rtr_probe_interval)) {
659 work = kmalloc(sizeof(*work), GFP_ATOMIC);
661 __neigh_set_probe_once(neigh);
663 write_unlock(&neigh->lock);
664 } else if (time_after(jiffies, last_probe +
665 idev->cnf.rtr_probe_interval)) {
666 work = kmalloc(sizeof(*work), GFP_ATOMIC);
669 if (!work || cmpxchg(&fib6_nh->last_probe,
670 last_probe, jiffies) != last_probe) {
673 INIT_WORK(&work->work, rt6_probe_deferred);
674 work->target = *nh_gw;
677 schedule_work(&work->work);
681 rcu_read_unlock_bh();
684 static inline void rt6_probe(struct fib6_nh *fib6_nh)
690 * Default Router Selection (RFC 2461 6.3.6)
692 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
694 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
695 struct neighbour *neigh;
698 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
699 &fib6_nh->fib_nh_gw6);
701 read_lock(&neigh->lock);
702 if (neigh->nud_state & NUD_VALID)
703 ret = RT6_NUD_SUCCEED;
704 #ifdef CONFIG_IPV6_ROUTER_PREF
705 else if (!(neigh->nud_state & NUD_FAILED))
706 ret = RT6_NUD_SUCCEED;
708 ret = RT6_NUD_FAIL_PROBE;
710 read_unlock(&neigh->lock);
712 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
713 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
715 rcu_read_unlock_bh();
720 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
725 if (!oif || nh->fib_nh_dev->ifindex == oif)
728 if (!m && (strict & RT6_LOOKUP_F_IFACE))
729 return RT6_NUD_FAIL_HARD;
730 #ifdef CONFIG_IPV6_ROUTER_PREF
731 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
733 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
734 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
735 int n = rt6_check_neigh(nh);
742 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
743 int oif, int strict, int *mpri, bool *do_rr)
745 bool match_do_rr = false;
749 if (nh->fib_nh_flags & RTNH_F_DEAD)
752 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
753 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
754 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
757 m = rt6_score_route(nh, fib6_flags, oif, strict);
758 if (m == RT6_NUD_FAIL_DO_RR) {
760 m = 0; /* lowest valid score */
761 } else if (m == RT6_NUD_FAIL_HARD) {
765 if (strict & RT6_LOOKUP_F_REACHABLE)
768 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
770 *do_rr = match_do_rr;
778 struct fib6_nh_frl_arg {
787 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
789 struct fib6_nh_frl_arg *arg = _arg;
792 return find_match(nh, arg->flags, arg->oif, arg->strict,
793 arg->mpri, arg->do_rr);
796 static void __find_rr_leaf(struct fib6_info *f6i_start,
797 struct fib6_info *nomatch, u32 metric,
798 struct fib6_result *res, struct fib6_info **cont,
799 int oif, int strict, bool *do_rr, int *mpri)
801 struct fib6_info *f6i;
803 for (f6i = f6i_start;
804 f6i && f6i != nomatch;
805 f6i = rcu_dereference(f6i->fib6_next)) {
806 bool matched = false;
809 if (cont && f6i->fib6_metric != metric) {
814 if (fib6_check_expired(f6i))
817 if (unlikely(f6i->nh)) {
818 struct fib6_nh_frl_arg arg = {
819 .flags = f6i->fib6_flags,
826 if (nexthop_is_blackhole(f6i->nh)) {
827 res->fib6_flags = RTF_REJECT;
828 res->fib6_type = RTN_BLACKHOLE;
830 res->nh = nexthop_fib6_nh(f6i->nh);
833 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
840 if (find_match(nh, f6i->fib6_flags, oif, strict,
847 res->fib6_flags = f6i->fib6_flags;
848 res->fib6_type = f6i->fib6_type;
853 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
854 struct fib6_info *rr_head, int oif, int strict,
855 bool *do_rr, struct fib6_result *res)
857 u32 metric = rr_head->fib6_metric;
858 struct fib6_info *cont = NULL;
861 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
862 oif, strict, do_rr, &mpri);
864 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
865 oif, strict, do_rr, &mpri);
867 if (res->f6i || !cont)
870 __find_rr_leaf(cont, NULL, metric, res, NULL,
871 oif, strict, do_rr, &mpri);
874 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
875 struct fib6_result *res, int strict)
877 struct fib6_info *leaf = rcu_dereference(fn->leaf);
878 struct fib6_info *rt0;
882 /* make sure this function or its helpers sets f6i */
885 if (!leaf || leaf == net->ipv6.fib6_null_entry)
888 rt0 = rcu_dereference(fn->rr_ptr);
892 /* Double check to make sure fn is not an intermediate node
893 * and fn->leaf does not points to its child's leaf
894 * (This might happen if all routes under fn are deleted from
895 * the tree and fib6_repair_tree() is called on the node.)
897 key_plen = rt0->fib6_dst.plen;
898 #ifdef CONFIG_IPV6_SUBTREES
899 if (rt0->fib6_src.plen)
900 key_plen = rt0->fib6_src.plen;
902 if (fn->fn_bit != key_plen)
905 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
907 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
909 /* no entries matched; do round-robin */
910 if (!next || next->fib6_metric != rt0->fib6_metric)
914 spin_lock_bh(&leaf->fib6_table->tb6_lock);
915 /* make sure next is not being deleted from the tree */
917 rcu_assign_pointer(fn->rr_ptr, next);
918 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
924 res->f6i = net->ipv6.fib6_null_entry;
925 res->nh = res->f6i->fib6_nh;
926 res->fib6_flags = res->f6i->fib6_flags;
927 res->fib6_type = res->f6i->fib6_type;
931 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
933 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
934 res->nh->fib_nh_gw_family;
937 #ifdef CONFIG_IPV6_ROUTE_INFO
938 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
939 const struct in6_addr *gwaddr)
941 struct net *net = dev_net(dev);
942 struct route_info *rinfo = (struct route_info *) opt;
943 struct in6_addr prefix_buf, *prefix;
945 unsigned long lifetime;
946 struct fib6_info *rt;
948 if (len < sizeof(struct route_info)) {
952 /* Sanity check for prefix_len and length */
953 if (rinfo->length > 3) {
955 } else if (rinfo->prefix_len > 128) {
957 } else if (rinfo->prefix_len > 64) {
958 if (rinfo->length < 2) {
961 } else if (rinfo->prefix_len > 0) {
962 if (rinfo->length < 1) {
967 pref = rinfo->route_pref;
968 if (pref == ICMPV6_ROUTER_PREF_INVALID)
971 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
973 if (rinfo->length == 3)
974 prefix = (struct in6_addr *)rinfo->prefix;
976 /* this function is safe */
977 ipv6_addr_prefix(&prefix_buf,
978 (struct in6_addr *)rinfo->prefix,
980 prefix = &prefix_buf;
983 if (rinfo->prefix_len == 0)
984 rt = rt6_get_dflt_router(net, gwaddr, dev);
986 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
989 if (rt && !lifetime) {
990 ip6_del_rt(net, rt, false);
995 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
998 rt->fib6_flags = RTF_ROUTEINFO |
999 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1002 if (!addrconf_finite_timeout(lifetime))
1003 fib6_clean_expires(rt);
1005 fib6_set_expires(rt, jiffies + HZ * lifetime);
1007 fib6_info_release(rt);
1014 * Misc support functions
1017 /* called with rcu_lock held */
1018 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1020 struct net_device *dev = res->nh->fib_nh_dev;
1022 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1023 /* for copies of local routes, dst->dev needs to be the
1024 * device if it is a master device, the master device if
1025 * device is enslaved, and the loopback as the default
1027 if (netif_is_l3_slave(dev) &&
1028 !rt6_need_strict(&res->f6i->fib6_dst.addr))
1029 dev = l3mdev_master_dev_rcu(dev);
1030 else if (!netif_is_l3_master(dev))
1031 dev = dev_net(dev)->loopback_dev;
1032 /* last case is netif_is_l3_master(dev) is true in which
1033 * case we want dev returned to be dev
1040 static const int fib6_prop[RTN_MAX + 1] = {
1044 [RTN_BROADCAST] = 0,
1046 [RTN_MULTICAST] = 0,
1047 [RTN_BLACKHOLE] = -EINVAL,
1048 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1049 [RTN_PROHIBIT] = -EACCES,
1050 [RTN_THROW] = -EAGAIN,
1051 [RTN_NAT] = -EINVAL,
1052 [RTN_XRESOLVE] = -EINVAL,
1055 static int ip6_rt_type_to_error(u8 fib6_type)
1057 return fib6_prop[fib6_type];
1060 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1062 unsigned short flags = 0;
1064 if (rt->dst_nocount)
1065 flags |= DST_NOCOUNT;
1066 if (rt->dst_nopolicy)
1067 flags |= DST_NOPOLICY;
1072 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1074 rt->dst.error = ip6_rt_type_to_error(fib6_type);
1076 switch (fib6_type) {
1078 rt->dst.output = dst_discard_out;
1079 rt->dst.input = dst_discard;
1082 rt->dst.output = ip6_pkt_prohibit_out;
1083 rt->dst.input = ip6_pkt_prohibit;
1086 case RTN_UNREACHABLE:
1088 rt->dst.output = ip6_pkt_discard_out;
1089 rt->dst.input = ip6_pkt_discard;
1094 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1096 struct fib6_info *f6i = res->f6i;
1098 if (res->fib6_flags & RTF_REJECT) {
1099 ip6_rt_init_dst_reject(rt, res->fib6_type);
1104 rt->dst.output = ip6_output;
1106 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1107 rt->dst.input = ip6_input;
1108 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1109 rt->dst.input = ip6_mc_input;
1111 rt->dst.input = ip6_forward;
1114 if (res->nh->fib_nh_lws) {
1115 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1116 lwtunnel_set_redirect(&rt->dst);
1119 rt->dst.lastuse = jiffies;
1122 /* Caller must already hold reference to @from */
1123 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1125 rt->rt6i_flags &= ~RTF_EXPIRES;
1126 rcu_assign_pointer(rt->from, from);
1127 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1130 /* Caller must already hold reference to f6i in result */
1131 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1133 const struct fib6_nh *nh = res->nh;
1134 const struct net_device *dev = nh->fib_nh_dev;
1135 struct fib6_info *f6i = res->f6i;
1137 ip6_rt_init_dst(rt, res);
1139 rt->rt6i_dst = f6i->fib6_dst;
1140 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1141 rt->rt6i_flags = res->fib6_flags;
1142 if (nh->fib_nh_gw_family) {
1143 rt->rt6i_gateway = nh->fib_nh_gw6;
1144 rt->rt6i_flags |= RTF_GATEWAY;
1146 rt6_set_from(rt, f6i);
1147 #ifdef CONFIG_IPV6_SUBTREES
1148 rt->rt6i_src = f6i->fib6_src;
1152 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1153 struct in6_addr *saddr)
1155 struct fib6_node *pn, *sn;
1157 if (fn->fn_flags & RTN_TL_ROOT)
1159 pn = rcu_dereference(fn->parent);
1160 sn = FIB6_SUBTREE(pn);
1162 fn = fib6_node_lookup(sn, NULL, saddr);
1165 if (fn->fn_flags & RTN_RTINFO)
1170 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1172 struct rt6_info *rt = *prt;
1174 if (dst_hold_safe(&rt->dst))
1177 rt = net->ipv6.ip6_null_entry;
1186 /* called with rcu_lock held */
1187 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1189 struct net_device *dev = res->nh->fib_nh_dev;
1190 struct fib6_info *f6i = res->f6i;
1191 unsigned short flags;
1192 struct rt6_info *nrt;
1194 if (!fib6_info_hold_safe(f6i))
1197 flags = fib6_info_dst_flags(f6i);
1198 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1200 fib6_info_release(f6i);
1204 ip6_rt_copy_init(nrt, res);
1208 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1209 dst_hold(&nrt->dst);
1213 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1214 struct fib6_table *table,
1216 const struct sk_buff *skb,
1219 struct fib6_result res = {};
1220 struct fib6_node *fn;
1221 struct rt6_info *rt;
1223 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1224 flags &= ~RT6_LOOKUP_F_IFACE;
1227 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1229 res.f6i = rcu_dereference(fn->leaf);
1231 res.f6i = net->ipv6.fib6_null_entry;
1233 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1236 if (res.f6i == net->ipv6.fib6_null_entry) {
1237 fn = fib6_backtrack(fn, &fl6->saddr);
1241 rt = net->ipv6.ip6_null_entry;
1244 } else if (res.fib6_flags & RTF_REJECT) {
1248 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1249 fl6->flowi6_oif != 0, skb, flags);
1251 /* Search through exception table */
1252 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1254 if (ip6_hold_safe(net, &rt))
1255 dst_use_noref(&rt->dst, jiffies);
1258 rt = ip6_create_rt_rcu(&res);
1262 trace_fib6_table_lookup(net, &res, table, fl6);
1269 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1270 const struct sk_buff *skb, int flags)
1272 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1274 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1276 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1277 const struct in6_addr *saddr, int oif,
1278 const struct sk_buff *skb, int strict)
1280 struct flowi6 fl6 = {
1284 struct dst_entry *dst;
1285 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1288 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1289 flags |= RT6_LOOKUP_F_HAS_SADDR;
1292 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1293 if (dst->error == 0)
1294 return (struct rt6_info *) dst;
1300 EXPORT_SYMBOL(rt6_lookup);
1302 /* ip6_ins_rt is called with FREE table->tb6_lock.
1303 * It takes new route entry, the addition fails by any reason the
1304 * route is released.
1305 * Caller must hold dst before calling it.
1308 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1309 struct netlink_ext_ack *extack)
1312 struct fib6_table *table;
1314 table = rt->fib6_table;
1315 spin_lock_bh(&table->tb6_lock);
1316 err = fib6_add(&table->tb6_root, rt, info, extack);
1317 spin_unlock_bh(&table->tb6_lock);
1322 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1324 struct nl_info info = { .nl_net = net, };
1326 return __ip6_ins_rt(rt, &info, NULL);
1329 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1330 const struct in6_addr *daddr,
1331 const struct in6_addr *saddr)
1333 struct fib6_info *f6i = res->f6i;
1334 struct net_device *dev;
1335 struct rt6_info *rt;
1341 if (!fib6_info_hold_safe(f6i))
1344 dev = ip6_rt_get_dev_rcu(res);
1345 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1347 fib6_info_release(f6i);
1351 ip6_rt_copy_init(rt, res);
1352 rt->rt6i_flags |= RTF_CACHE;
1353 rt->rt6i_dst.addr = *daddr;
1354 rt->rt6i_dst.plen = 128;
1356 if (!rt6_is_gw_or_nonexthop(res)) {
1357 if (f6i->fib6_dst.plen != 128 &&
1358 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1359 rt->rt6i_flags |= RTF_ANYCAST;
1360 #ifdef CONFIG_IPV6_SUBTREES
1361 if (rt->rt6i_src.plen && saddr) {
1362 rt->rt6i_src.addr = *saddr;
1363 rt->rt6i_src.plen = 128;
1371 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1373 struct fib6_info *f6i = res->f6i;
1374 unsigned short flags = fib6_info_dst_flags(f6i);
1375 struct net_device *dev;
1376 struct rt6_info *pcpu_rt;
1378 if (!fib6_info_hold_safe(f6i))
1382 dev = ip6_rt_get_dev_rcu(res);
1383 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
1386 fib6_info_release(f6i);
1389 ip6_rt_copy_init(pcpu_rt, res);
1390 pcpu_rt->rt6i_flags |= RTF_PCPU;
1393 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1398 static bool rt6_is_valid(const struct rt6_info *rt6)
1400 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1403 /* It should be called with rcu_read_lock() acquired */
1404 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1406 struct rt6_info *pcpu_rt;
1408 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1410 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1411 struct rt6_info *prev, **p;
1413 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1414 prev = xchg(p, NULL);
1416 dst_dev_put(&prev->dst);
1417 dst_release(&prev->dst);
1426 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1427 const struct fib6_result *res)
1429 struct rt6_info *pcpu_rt, *prev, **p;
1431 pcpu_rt = ip6_rt_pcpu_alloc(res);
1435 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1436 prev = cmpxchg(p, NULL, pcpu_rt);
1439 if (res->f6i->fib6_destroying) {
1440 struct fib6_info *from;
1442 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1443 fib6_info_release(from);
1449 /* exception hash table implementation
1451 static DEFINE_SPINLOCK(rt6_exception_lock);
1453 /* Remove rt6_ex from hash table and free the memory
1454 * Caller must hold rt6_exception_lock
1456 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1457 struct rt6_exception *rt6_ex)
1459 struct fib6_info *from;
1462 if (!bucket || !rt6_ex)
1465 net = dev_net(rt6_ex->rt6i->dst.dev);
1466 net->ipv6.rt6_stats->fib_rt_cache--;
1468 /* purge completely the exception to allow releasing the held resources:
1469 * some [sk] cache may keep the dst around for unlimited time
1471 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1472 fib6_info_release(from);
1473 dst_dev_put(&rt6_ex->rt6i->dst);
1475 hlist_del_rcu(&rt6_ex->hlist);
1476 dst_release(&rt6_ex->rt6i->dst);
1477 kfree_rcu(rt6_ex, rcu);
1478 WARN_ON_ONCE(!bucket->depth);
1482 /* Remove oldest rt6_ex in bucket and free the memory
1483 * Caller must hold rt6_exception_lock
1485 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1487 struct rt6_exception *rt6_ex, *oldest = NULL;
1492 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1493 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1496 rt6_remove_exception(bucket, oldest);
1499 static u32 rt6_exception_hash(const struct in6_addr *dst,
1500 const struct in6_addr *src)
1502 static u32 seed __read_mostly;
1505 net_get_random_once(&seed, sizeof(seed));
1506 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
1508 #ifdef CONFIG_IPV6_SUBTREES
1510 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
1512 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1515 /* Helper function to find the cached rt in the hash table
1516 * and update bucket pointer to point to the bucket for this
1517 * (daddr, saddr) pair
1518 * Caller must hold rt6_exception_lock
1520 static struct rt6_exception *
1521 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1522 const struct in6_addr *daddr,
1523 const struct in6_addr *saddr)
1525 struct rt6_exception *rt6_ex;
1528 if (!(*bucket) || !daddr)
1531 hval = rt6_exception_hash(daddr, saddr);
1534 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1535 struct rt6_info *rt6 = rt6_ex->rt6i;
1536 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1538 #ifdef CONFIG_IPV6_SUBTREES
1539 if (matched && saddr)
1540 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1548 /* Helper function to find the cached rt in the hash table
1549 * and update bucket pointer to point to the bucket for this
1550 * (daddr, saddr) pair
1551 * Caller must hold rcu_read_lock()
1553 static struct rt6_exception *
1554 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1555 const struct in6_addr *daddr,
1556 const struct in6_addr *saddr)
1558 struct rt6_exception *rt6_ex;
1561 WARN_ON_ONCE(!rcu_read_lock_held());
1563 if (!(*bucket) || !daddr)
1566 hval = rt6_exception_hash(daddr, saddr);
1569 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1570 struct rt6_info *rt6 = rt6_ex->rt6i;
1571 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1573 #ifdef CONFIG_IPV6_SUBTREES
1574 if (matched && saddr)
1575 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1583 static unsigned int fib6_mtu(const struct fib6_result *res)
1585 const struct fib6_nh *nh = res->nh;
1588 if (res->f6i->fib6_pmtu) {
1589 mtu = res->f6i->fib6_pmtu;
1591 struct net_device *dev = nh->fib_nh_dev;
1592 struct inet6_dev *idev;
1595 idev = __in6_dev_get(dev);
1596 mtu = idev->cnf.mtu6;
1600 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1602 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1605 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1607 /* used when the flushed bit is not relevant, only access to the bucket
1608 * (ie., all bucket users except rt6_insert_exception);
1610 * called under rcu lock; sometimes called with rt6_exception_lock held
1613 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1616 struct rt6_exception_bucket *bucket;
1619 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1620 lockdep_is_held(lock));
1622 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1624 /* remove bucket flushed bit if set */
1626 unsigned long p = (unsigned long)bucket;
1628 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1629 bucket = (struct rt6_exception_bucket *)p;
1635 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1637 unsigned long p = (unsigned long)bucket;
1639 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1642 /* called with rt6_exception_lock held */
1643 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1646 struct rt6_exception_bucket *bucket;
1649 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1650 lockdep_is_held(lock));
1652 p = (unsigned long)bucket;
1653 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1654 bucket = (struct rt6_exception_bucket *)p;
1655 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1658 static int rt6_insert_exception(struct rt6_info *nrt,
1659 const struct fib6_result *res)
1661 struct net *net = dev_net(nrt->dst.dev);
1662 struct rt6_exception_bucket *bucket;
1663 struct fib6_info *f6i = res->f6i;
1664 struct in6_addr *src_key = NULL;
1665 struct rt6_exception *rt6_ex;
1666 struct fib6_nh *nh = res->nh;
1669 spin_lock_bh(&rt6_exception_lock);
1671 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1672 lockdep_is_held(&rt6_exception_lock));
1674 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1680 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1681 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1686 #ifdef CONFIG_IPV6_SUBTREES
1687 /* fib6_src.plen != 0 indicates f6i is in subtree
1688 * and exception table is indexed by a hash of
1689 * both fib6_dst and fib6_src.
1690 * Otherwise, the exception table is indexed by
1691 * a hash of only fib6_dst.
1693 if (f6i->fib6_src.plen)
1694 src_key = &nrt->rt6i_src.addr;
1696 /* rt6_mtu_change() might lower mtu on f6i.
1697 * Only insert this exception route if its mtu
1698 * is less than f6i's mtu value.
1700 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1705 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1708 rt6_remove_exception(bucket, rt6_ex);
1710 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1716 rt6_ex->stamp = jiffies;
1717 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1719 net->ipv6.rt6_stats->fib_rt_cache++;
1721 if (bucket->depth > FIB6_MAX_DEPTH)
1722 rt6_exception_remove_oldest(bucket);
1725 spin_unlock_bh(&rt6_exception_lock);
1727 /* Update fn->fn_sernum to invalidate all cached dst */
1729 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1730 fib6_update_sernum(net, f6i);
1731 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1732 fib6_force_start_gc(net);
1738 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1740 struct rt6_exception_bucket *bucket;
1741 struct rt6_exception *rt6_ex;
1742 struct hlist_node *tmp;
1745 spin_lock_bh(&rt6_exception_lock);
1747 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1751 /* Prevent rt6_insert_exception() to recreate the bucket list */
1753 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1755 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1756 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1758 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1759 rt6_remove_exception(bucket, rt6_ex);
1761 WARN_ON_ONCE(!from && bucket->depth);
1765 spin_unlock_bh(&rt6_exception_lock);
1768 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1770 struct fib6_info *f6i = arg;
1772 fib6_nh_flush_exceptions(nh, f6i);
1777 void rt6_flush_exceptions(struct fib6_info *f6i)
1780 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1783 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1786 /* Find cached rt in the hash table inside passed in rt
1787 * Caller has to hold rcu_read_lock()
1789 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1790 const struct in6_addr *daddr,
1791 const struct in6_addr *saddr)
1793 const struct in6_addr *src_key = NULL;
1794 struct rt6_exception_bucket *bucket;
1795 struct rt6_exception *rt6_ex;
1796 struct rt6_info *ret = NULL;
1798 #ifdef CONFIG_IPV6_SUBTREES
1799 /* fib6i_src.plen != 0 indicates f6i is in subtree
1800 * and exception table is indexed by a hash of
1801 * both fib6_dst and fib6_src.
1802 * However, the src addr used to create the hash
1803 * might not be exactly the passed in saddr which
1804 * is a /128 addr from the flow.
1805 * So we need to use f6i->fib6_src to redo lookup
1806 * if the passed in saddr does not find anything.
1807 * (See the logic in ip6_rt_cache_alloc() on how
1808 * rt->rt6i_src is updated.)
1810 if (res->f6i->fib6_src.plen)
1814 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1815 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1817 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1820 #ifdef CONFIG_IPV6_SUBTREES
1821 /* Use fib6_src as src_key and redo lookup */
1822 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1823 src_key = &res->f6i->fib6_src.addr;
1831 /* Remove the passed in cached rt from the hash table that contains it */
1832 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1833 const struct rt6_info *rt)
1835 const struct in6_addr *src_key = NULL;
1836 struct rt6_exception_bucket *bucket;
1837 struct rt6_exception *rt6_ex;
1840 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1843 spin_lock_bh(&rt6_exception_lock);
1844 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1846 #ifdef CONFIG_IPV6_SUBTREES
1847 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1848 * and exception table is indexed by a hash of
1849 * both rt6i_dst and rt6i_src.
1850 * Otherwise, the exception table is indexed by
1851 * a hash of only rt6i_dst.
1854 src_key = &rt->rt6i_src.addr;
1856 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1860 rt6_remove_exception(bucket, rt6_ex);
1866 spin_unlock_bh(&rt6_exception_lock);
1870 struct fib6_nh_excptn_arg {
1871 struct rt6_info *rt;
1875 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1877 struct fib6_nh_excptn_arg *arg = _arg;
1880 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1887 static int rt6_remove_exception_rt(struct rt6_info *rt)
1889 struct fib6_info *from;
1891 from = rcu_dereference(rt->from);
1892 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1896 struct fib6_nh_excptn_arg arg = {
1898 .plen = from->fib6_src.plen
1902 /* rc = 1 means an entry was found */
1903 rc = nexthop_for_each_fib6_nh(from->nh,
1904 rt6_nh_remove_exception_rt,
1906 return rc ? 0 : -ENOENT;
1909 return fib6_nh_remove_exception(from->fib6_nh,
1910 from->fib6_src.plen, rt);
1913 /* Find rt6_ex which contains the passed in rt cache and
1916 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1917 const struct rt6_info *rt)
1919 const struct in6_addr *src_key = NULL;
1920 struct rt6_exception_bucket *bucket;
1921 struct rt6_exception *rt6_ex;
1923 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1924 #ifdef CONFIG_IPV6_SUBTREES
1925 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1926 * and exception table is indexed by a hash of
1927 * both rt6i_dst and rt6i_src.
1928 * Otherwise, the exception table is indexed by
1929 * a hash of only rt6i_dst.
1932 src_key = &rt->rt6i_src.addr;
1934 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1936 rt6_ex->stamp = jiffies;
1939 struct fib6_nh_match_arg {
1940 const struct net_device *dev;
1941 const struct in6_addr *gw;
1942 struct fib6_nh *match;
1945 /* determine if fib6_nh has given device and gateway */
1946 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1948 struct fib6_nh_match_arg *arg = _arg;
1950 if (arg->dev != nh->fib_nh_dev ||
1951 (arg->gw && !nh->fib_nh_gw_family) ||
1952 (!arg->gw && nh->fib_nh_gw_family) ||
1953 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1958 /* found a match, break the loop */
1962 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1964 struct fib6_info *from;
1965 struct fib6_nh *fib6_nh;
1969 from = rcu_dereference(rt->from);
1970 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1974 struct fib6_nh_match_arg arg = {
1976 .gw = &rt->rt6i_gateway,
1979 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1983 fib6_nh = arg.match;
1985 fib6_nh = from->fib6_nh;
1987 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1992 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1993 struct rt6_info *rt, int mtu)
1995 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1996 * lowest MTU in the path: always allow updating the route PMTU to
1997 * reflect PMTU decreases.
1999 * If the new MTU is higher, and the route PMTU is equal to the local
2000 * MTU, this means the old MTU is the lowest in the path, so allow
2001 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2005 if (dst_mtu(&rt->dst) >= mtu)
2008 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2014 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2015 const struct fib6_nh *nh, int mtu)
2017 struct rt6_exception_bucket *bucket;
2018 struct rt6_exception *rt6_ex;
2021 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2025 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2026 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2027 struct rt6_info *entry = rt6_ex->rt6i;
2029 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2030 * route), the metrics of its rt->from have already
2033 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2034 rt6_mtu_change_route_allowed(idev, entry, mtu))
2035 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2041 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2043 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2044 const struct in6_addr *gateway)
2046 struct rt6_exception_bucket *bucket;
2047 struct rt6_exception *rt6_ex;
2048 struct hlist_node *tmp;
2051 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2054 spin_lock_bh(&rt6_exception_lock);
2055 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2057 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2058 hlist_for_each_entry_safe(rt6_ex, tmp,
2059 &bucket->chain, hlist) {
2060 struct rt6_info *entry = rt6_ex->rt6i;
2062 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2063 RTF_CACHE_GATEWAY &&
2064 ipv6_addr_equal(gateway,
2065 &entry->rt6i_gateway)) {
2066 rt6_remove_exception(bucket, rt6_ex);
2073 spin_unlock_bh(&rt6_exception_lock);
2076 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2077 struct rt6_exception *rt6_ex,
2078 struct fib6_gc_args *gc_args,
2081 struct rt6_info *rt = rt6_ex->rt6i;
2083 /* we are pruning and obsoleting aged-out and non gateway exceptions
2084 * even if others have still references to them, so that on next
2085 * dst_check() such references can be dropped.
2086 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2087 * expired, independently from their aging, as per RFC 8201 section 4
2089 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2090 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2091 RT6_TRACE("aging clone %p\n", rt);
2092 rt6_remove_exception(bucket, rt6_ex);
2095 } else if (time_after(jiffies, rt->dst.expires)) {
2096 RT6_TRACE("purging expired route %p\n", rt);
2097 rt6_remove_exception(bucket, rt6_ex);
2101 if (rt->rt6i_flags & RTF_GATEWAY) {
2102 struct neighbour *neigh;
2103 __u8 neigh_flags = 0;
2105 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2107 neigh_flags = neigh->flags;
2109 if (!(neigh_flags & NTF_ROUTER)) {
2110 RT6_TRACE("purging route %p via non-router but gateway\n",
2112 rt6_remove_exception(bucket, rt6_ex);
2120 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2121 struct fib6_gc_args *gc_args,
2124 struct rt6_exception_bucket *bucket;
2125 struct rt6_exception *rt6_ex;
2126 struct hlist_node *tmp;
2129 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2133 spin_lock(&rt6_exception_lock);
2134 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2136 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2137 hlist_for_each_entry_safe(rt6_ex, tmp,
2138 &bucket->chain, hlist) {
2139 rt6_age_examine_exception(bucket, rt6_ex,
2145 spin_unlock(&rt6_exception_lock);
2146 rcu_read_unlock_bh();
2149 struct fib6_nh_age_excptn_arg {
2150 struct fib6_gc_args *gc_args;
2154 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2156 struct fib6_nh_age_excptn_arg *arg = _arg;
2158 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2162 void rt6_age_exceptions(struct fib6_info *f6i,
2163 struct fib6_gc_args *gc_args,
2167 struct fib6_nh_age_excptn_arg arg = {
2172 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2175 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2179 /* must be called with rcu lock held */
2180 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2181 struct flowi6 *fl6, struct fib6_result *res, int strict)
2183 struct fib6_node *fn, *saved_fn;
2185 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2188 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2192 rt6_select(net, fn, oif, res, strict);
2193 if (res->f6i == net->ipv6.fib6_null_entry) {
2194 fn = fib6_backtrack(fn, &fl6->saddr);
2196 goto redo_rt6_select;
2197 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2198 /* also consider unreachable route */
2199 strict &= ~RT6_LOOKUP_F_REACHABLE;
2201 goto redo_rt6_select;
2205 trace_fib6_table_lookup(net, res, table, fl6);
2210 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2211 int oif, struct flowi6 *fl6,
2212 const struct sk_buff *skb, int flags)
2214 struct fib6_result res = {};
2215 struct rt6_info *rt = NULL;
2218 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2219 !rcu_read_lock_held());
2221 strict |= flags & RT6_LOOKUP_F_IFACE;
2222 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2223 if (net->ipv6.devconf_all->forwarding == 0)
2224 strict |= RT6_LOOKUP_F_REACHABLE;
2228 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2229 if (res.f6i == net->ipv6.fib6_null_entry)
2232 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2234 /*Search through exception table */
2235 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2238 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2239 !res.nh->fib_nh_gw_family)) {
2240 /* Create a RTF_CACHE clone which will not be
2241 * owned by the fib6 tree. It is for the special case where
2242 * the daddr in the skb during the neighbor look-up is different
2243 * from the fl6->daddr used to look-up route here.
2245 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2248 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2249 * As rt6_uncached_list_add() does not consume refcnt,
2250 * this refcnt is always returned to the caller even
2251 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2253 rt6_uncached_list_add(rt);
2254 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
2260 /* Get a percpu copy */
2262 rt = rt6_get_pcpu_route(&res);
2265 rt = rt6_make_pcpu_route(net, &res);
2271 rt = net->ipv6.ip6_null_entry;
2272 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2273 ip6_hold_safe(net, &rt);
2278 EXPORT_SYMBOL_GPL(ip6_pol_route);
2280 static struct rt6_info *ip6_pol_route_input(struct net *net,
2281 struct fib6_table *table,
2283 const struct sk_buff *skb,
2286 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2289 struct dst_entry *ip6_route_input_lookup(struct net *net,
2290 struct net_device *dev,
2292 const struct sk_buff *skb,
2295 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2296 flags |= RT6_LOOKUP_F_IFACE;
2298 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2300 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2302 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2303 struct flow_keys *keys,
2304 struct flow_keys *flkeys)
2306 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2307 const struct ipv6hdr *key_iph = outer_iph;
2308 struct flow_keys *_flkeys = flkeys;
2309 const struct ipv6hdr *inner_iph;
2310 const struct icmp6hdr *icmph;
2311 struct ipv6hdr _inner_iph;
2312 struct icmp6hdr _icmph;
2314 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2317 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2318 sizeof(_icmph), &_icmph);
2322 if (!icmpv6_is_err(icmph->icmp6_type))
2325 inner_iph = skb_header_pointer(skb,
2326 skb_transport_offset(skb) + sizeof(*icmph),
2327 sizeof(_inner_iph), &_inner_iph);
2331 key_iph = inner_iph;
2335 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2336 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2337 keys->tags.flow_label = _flkeys->tags.flow_label;
2338 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2340 keys->addrs.v6addrs.src = key_iph->saddr;
2341 keys->addrs.v6addrs.dst = key_iph->daddr;
2342 keys->tags.flow_label = ip6_flowlabel(key_iph);
2343 keys->basic.ip_proto = key_iph->nexthdr;
2347 /* if skb is set it will be used and fl6 can be NULL */
2348 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2349 const struct sk_buff *skb, struct flow_keys *flkeys)
2351 struct flow_keys hash_keys;
2354 switch (ip6_multipath_hash_policy(net)) {
2356 memset(&hash_keys, 0, sizeof(hash_keys));
2357 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2359 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2361 hash_keys.addrs.v6addrs.src = fl6->saddr;
2362 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2363 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2364 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2369 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2370 struct flow_keys keys;
2372 /* short-circuit if we already have L4 hash present */
2374 return skb_get_hash_raw(skb) >> 1;
2376 memset(&hash_keys, 0, sizeof(hash_keys));
2379 skb_flow_dissect_flow_keys(skb, &keys, flag);
2382 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2383 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2384 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2385 hash_keys.ports.src = flkeys->ports.src;
2386 hash_keys.ports.dst = flkeys->ports.dst;
2387 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2389 memset(&hash_keys, 0, sizeof(hash_keys));
2390 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2391 hash_keys.addrs.v6addrs.src = fl6->saddr;
2392 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2393 hash_keys.ports.src = fl6->fl6_sport;
2394 hash_keys.ports.dst = fl6->fl6_dport;
2395 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2399 memset(&hash_keys, 0, sizeof(hash_keys));
2400 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2402 struct flow_keys keys;
2405 skb_flow_dissect_flow_keys(skb, &keys, 0);
2409 /* Inner can be v4 or v6 */
2410 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2411 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2412 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2413 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2414 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2415 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2416 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2417 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2418 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2419 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2421 /* Same as case 0 */
2422 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2423 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2426 /* Same as case 0 */
2427 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2428 hash_keys.addrs.v6addrs.src = fl6->saddr;
2429 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2430 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2431 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2435 mhash = flow_hash_from_keys(&hash_keys);
2440 /* Called with rcu held */
2441 void ip6_route_input(struct sk_buff *skb)
2443 const struct ipv6hdr *iph = ipv6_hdr(skb);
2444 struct net *net = dev_net(skb->dev);
2445 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2446 struct ip_tunnel_info *tun_info;
2447 struct flowi6 fl6 = {
2448 .flowi6_iif = skb->dev->ifindex,
2449 .daddr = iph->daddr,
2450 .saddr = iph->saddr,
2451 .flowlabel = ip6_flowinfo(iph),
2452 .flowi6_mark = skb->mark,
2453 .flowi6_proto = iph->nexthdr,
2455 struct flow_keys *flkeys = NULL, _flkeys;
2457 tun_info = skb_tunnel_info(skb);
2458 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2459 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2461 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2464 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2465 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2467 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2471 static struct rt6_info *ip6_pol_route_output(struct net *net,
2472 struct fib6_table *table,
2474 const struct sk_buff *skb,
2477 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2480 struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2481 const struct sock *sk,
2482 struct flowi6 *fl6, int flags)
2486 if (ipv6_addr_type(&fl6->daddr) &
2487 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2488 struct dst_entry *dst;
2490 /* This function does not take refcnt on the dst */
2491 dst = l3mdev_link_scope_lookup(net, fl6);
2496 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2498 flags |= RT6_LOOKUP_F_DST_NOREF;
2499 any_src = ipv6_addr_any(&fl6->saddr);
2500 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2501 (fl6->flowi6_oif && any_src))
2502 flags |= RT6_LOOKUP_F_IFACE;
2505 flags |= RT6_LOOKUP_F_HAS_SADDR;
2507 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2509 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2511 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2513 struct dst_entry *ip6_route_output_flags(struct net *net,
2514 const struct sock *sk,
2518 struct dst_entry *dst;
2519 struct rt6_info *rt6;
2522 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2523 rt6 = (struct rt6_info *)dst;
2524 /* For dst cached in uncached_list, refcnt is already taken. */
2525 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2526 dst = &net->ipv6.ip6_null_entry->dst;
2533 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2535 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2537 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2538 struct net_device *loopback_dev = net->loopback_dev;
2539 struct dst_entry *new = NULL;
2541 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2542 DST_OBSOLETE_DEAD, 0);
2545 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2549 new->input = dst_discard;
2550 new->output = dst_discard_out;
2552 dst_copy_metrics(new, &ort->dst);
2554 rt->rt6i_idev = in6_dev_get(loopback_dev);
2555 rt->rt6i_gateway = ort->rt6i_gateway;
2556 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2558 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2559 #ifdef CONFIG_IPV6_SUBTREES
2560 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2564 dst_release(dst_orig);
2565 return new ? new : ERR_PTR(-ENOMEM);
2569 * Destination cache support functions
2572 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2576 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2579 if (fib6_check_expired(f6i))
2585 static struct dst_entry *rt6_check(struct rt6_info *rt,
2586 struct fib6_info *from,
2591 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2592 rt_cookie != cookie)
2595 if (rt6_check_expired(rt))
2601 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2602 struct fib6_info *from,
2605 if (!__rt6_check_expired(rt) &&
2606 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2607 fib6_check(from, cookie))
2613 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2615 struct dst_entry *dst_ret;
2616 struct fib6_info *from;
2617 struct rt6_info *rt;
2619 rt = container_of(dst, struct rt6_info, dst);
2622 return rt6_is_valid(rt) ? dst : NULL;
2626 /* All IPV6 dsts are created with ->obsolete set to the value
2627 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2628 * into this function always.
2631 from = rcu_dereference(rt->from);
2633 if (from && (rt->rt6i_flags & RTF_PCPU ||
2634 unlikely(!list_empty(&rt->rt6i_uncached))))
2635 dst_ret = rt6_dst_from_check(rt, from, cookie);
2637 dst_ret = rt6_check(rt, from, cookie);
2644 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2646 struct rt6_info *rt = (struct rt6_info *) dst;
2649 if (rt->rt6i_flags & RTF_CACHE) {
2651 if (rt6_check_expired(rt)) {
2652 rt6_remove_exception_rt(rt);
2664 static void ip6_link_failure(struct sk_buff *skb)
2666 struct rt6_info *rt;
2668 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2670 rt = (struct rt6_info *) skb_dst(skb);
2673 if (rt->rt6i_flags & RTF_CACHE) {
2674 rt6_remove_exception_rt(rt);
2676 struct fib6_info *from;
2677 struct fib6_node *fn;
2679 from = rcu_dereference(rt->from);
2681 fn = rcu_dereference(from->fib6_node);
2682 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2690 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2692 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2693 struct fib6_info *from;
2696 from = rcu_dereference(rt0->from);
2698 rt0->dst.expires = from->expires;
2702 dst_set_expires(&rt0->dst, timeout);
2703 rt0->rt6i_flags |= RTF_EXPIRES;
2706 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2708 struct net *net = dev_net(rt->dst.dev);
2710 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2711 rt->rt6i_flags |= RTF_MODIFIED;
2712 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2715 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2717 return !(rt->rt6i_flags & RTF_CACHE) &&
2718 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2721 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2722 const struct ipv6hdr *iph, u32 mtu,
2725 const struct in6_addr *daddr, *saddr;
2726 struct rt6_info *rt6 = (struct rt6_info *)dst;
2728 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2729 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2730 * [see also comment in rt6_mtu_change_route()]
2734 daddr = &iph->daddr;
2735 saddr = &iph->saddr;
2737 daddr = &sk->sk_v6_daddr;
2738 saddr = &inet6_sk(sk)->saddr;
2745 dst_confirm_neigh(dst, daddr);
2747 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2748 if (mtu >= dst_mtu(dst))
2751 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2752 rt6_do_update_pmtu(rt6, mtu);
2753 /* update rt6_ex->stamp for cache */
2754 if (rt6->rt6i_flags & RTF_CACHE)
2755 rt6_update_exception_stamp_rt(rt6);
2757 struct fib6_result res = {};
2758 struct rt6_info *nrt6;
2761 res.f6i = rcu_dereference(rt6->from);
2765 res.fib6_flags = res.f6i->fib6_flags;
2766 res.fib6_type = res.f6i->fib6_type;
2769 struct fib6_nh_match_arg arg = {
2771 .gw = &rt6->rt6i_gateway,
2774 nexthop_for_each_fib6_nh(res.f6i->nh,
2775 fib6_nh_find_match, &arg);
2777 /* fib6_info uses a nexthop that does not have fib6_nh
2778 * using the dst->dev + gw. Should be impossible.
2785 res.nh = res.f6i->fib6_nh;
2788 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2790 rt6_do_update_pmtu(nrt6, mtu);
2791 if (rt6_insert_exception(nrt6, &res))
2792 dst_release_immediate(&nrt6->dst);
2799 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2800 struct sk_buff *skb, u32 mtu,
2803 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2807 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2808 int oif, u32 mark, kuid_t uid)
2810 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2811 struct dst_entry *dst;
2812 struct flowi6 fl6 = {
2814 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2815 .daddr = iph->daddr,
2816 .saddr = iph->saddr,
2817 .flowlabel = ip6_flowinfo(iph),
2821 dst = ip6_route_output(net, NULL, &fl6);
2823 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
2826 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2828 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2830 int oif = sk->sk_bound_dev_if;
2831 struct dst_entry *dst;
2833 if (!oif && skb->dev)
2834 oif = l3mdev_master_ifindex(skb->dev);
2836 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2838 dst = __sk_dst_get(sk);
2839 if (!dst || !dst->obsolete ||
2840 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2844 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2845 ip6_datagram_dst_update(sk, false);
2848 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2850 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2851 const struct flowi6 *fl6)
2853 #ifdef CONFIG_IPV6_SUBTREES
2854 struct ipv6_pinfo *np = inet6_sk(sk);
2857 ip6_dst_store(sk, dst,
2858 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2859 &sk->sk_v6_daddr : NULL,
2860 #ifdef CONFIG_IPV6_SUBTREES
2861 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2867 static bool ip6_redirect_nh_match(const struct fib6_result *res,
2869 const struct in6_addr *gw,
2870 struct rt6_info **ret)
2872 const struct fib6_nh *nh = res->nh;
2874 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2875 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2878 /* rt_cache's gateway might be different from its 'parent'
2879 * in the case of an ip redirect.
2880 * So we keep searching in the exception table if the gateway
2883 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2884 struct rt6_info *rt_cache;
2886 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2888 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2897 struct fib6_nh_rd_arg {
2898 struct fib6_result *res;
2900 const struct in6_addr *gw;
2901 struct rt6_info **ret;
2904 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2906 struct fib6_nh_rd_arg *arg = _arg;
2909 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2912 /* Handle redirects */
2913 struct ip6rd_flowi {
2915 struct in6_addr gateway;
2918 static struct rt6_info *__ip6_route_redirect(struct net *net,
2919 struct fib6_table *table,
2921 const struct sk_buff *skb,
2924 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2925 struct rt6_info *ret = NULL;
2926 struct fib6_result res = {};
2927 struct fib6_nh_rd_arg arg = {
2930 .gw = &rdfl->gateway,
2933 struct fib6_info *rt;
2934 struct fib6_node *fn;
2936 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2937 * this case we must match on the real ingress device, so reset it
2939 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2940 fl6->flowi6_oif = skb->dev->ifindex;
2942 /* Get the "current" route for this destination and
2943 * check if the redirect has come from appropriate router.
2945 * RFC 4861 specifies that redirects should only be
2946 * accepted if they come from the nexthop to the target.
2947 * Due to the way the routes are chosen, this notion
2948 * is a bit fuzzy and one might need to check all possible
2953 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2955 for_each_fib6_node_rt_rcu(fn) {
2957 if (fib6_check_expired(rt))
2959 if (rt->fib6_flags & RTF_REJECT)
2961 if (unlikely(rt->nh)) {
2962 if (nexthop_is_blackhole(rt->nh))
2964 /* on match, res->nh is filled in and potentially ret */
2965 if (nexthop_for_each_fib6_nh(rt->nh,
2966 fib6_nh_redirect_match,
2970 res.nh = rt->fib6_nh;
2971 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2978 rt = net->ipv6.fib6_null_entry;
2979 else if (rt->fib6_flags & RTF_REJECT) {
2980 ret = net->ipv6.ip6_null_entry;
2984 if (rt == net->ipv6.fib6_null_entry) {
2985 fn = fib6_backtrack(fn, &fl6->saddr);
2991 res.nh = rt->fib6_nh;
2994 ip6_hold_safe(net, &ret);
2996 res.fib6_flags = res.f6i->fib6_flags;
2997 res.fib6_type = res.f6i->fib6_type;
2998 ret = ip6_create_rt_rcu(&res);
3003 trace_fib6_table_lookup(net, &res, table, fl6);
3007 static struct dst_entry *ip6_route_redirect(struct net *net,
3008 const struct flowi6 *fl6,
3009 const struct sk_buff *skb,
3010 const struct in6_addr *gateway)
3012 int flags = RT6_LOOKUP_F_HAS_SADDR;
3013 struct ip6rd_flowi rdfl;
3016 rdfl.gateway = *gateway;
3018 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3019 flags, __ip6_route_redirect);
3022 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3025 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3026 struct dst_entry *dst;
3027 struct flowi6 fl6 = {
3028 .flowi6_iif = LOOPBACK_IFINDEX,
3030 .flowi6_mark = mark,
3031 .daddr = iph->daddr,
3032 .saddr = iph->saddr,
3033 .flowlabel = ip6_flowinfo(iph),
3037 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3038 rt6_do_redirect(dst, NULL, skb);
3041 EXPORT_SYMBOL_GPL(ip6_redirect);
3043 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3045 const struct ipv6hdr *iph = ipv6_hdr(skb);
3046 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3047 struct dst_entry *dst;
3048 struct flowi6 fl6 = {
3049 .flowi6_iif = LOOPBACK_IFINDEX,
3052 .saddr = iph->daddr,
3053 .flowi6_uid = sock_net_uid(net, NULL),
3056 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3057 rt6_do_redirect(dst, NULL, skb);
3061 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3063 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3066 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3068 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3070 struct net_device *dev = dst->dev;
3071 unsigned int mtu = dst_mtu(dst);
3072 struct net *net = dev_net(dev);
3074 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3076 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3077 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3080 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3081 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3082 * IPV6_MAXPLEN is also valid and means: "any MSS,
3083 * rely only on pmtu discovery"
3085 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3090 static unsigned int ip6_mtu(const struct dst_entry *dst)
3092 struct inet6_dev *idev;
3095 mtu = dst_metric_raw(dst, RTAX_MTU);
3102 idev = __in6_dev_get(dst->dev);
3104 mtu = idev->cnf.mtu6;
3108 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3110 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3114 * 1. mtu on route is locked - use it
3115 * 2. mtu from nexthop exception
3116 * 3. mtu from egress device
3118 * based on ip6_dst_mtu_forward and exception logic of
3119 * rt6_find_cached_rt; called with rcu_read_lock
3121 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3122 const struct in6_addr *daddr,
3123 const struct in6_addr *saddr)
3125 const struct fib6_nh *nh = res->nh;
3126 struct fib6_info *f6i = res->f6i;
3127 struct inet6_dev *idev;
3128 struct rt6_info *rt;
3131 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3132 mtu = f6i->fib6_pmtu;
3137 rt = rt6_find_cached_rt(res, daddr, saddr);
3139 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3141 struct net_device *dev = nh->fib_nh_dev;
3144 idev = __in6_dev_get(dev);
3145 if (idev && idev->cnf.mtu6 > mtu)
3146 mtu = idev->cnf.mtu6;
3149 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3151 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3154 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3157 struct dst_entry *dst;
3158 struct rt6_info *rt;
3159 struct inet6_dev *idev = in6_dev_get(dev);
3160 struct net *net = dev_net(dev);
3162 if (unlikely(!idev))
3163 return ERR_PTR(-ENODEV);
3165 rt = ip6_dst_alloc(net, dev, 0);
3166 if (unlikely(!rt)) {
3168 dst = ERR_PTR(-ENOMEM);
3172 rt->dst.input = ip6_input;
3173 rt->dst.output = ip6_output;
3174 rt->rt6i_gateway = fl6->daddr;
3175 rt->rt6i_dst.addr = fl6->daddr;
3176 rt->rt6i_dst.plen = 128;
3177 rt->rt6i_idev = idev;
3178 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3180 /* Add this dst into uncached_list so that rt6_disable_ip() can
3181 * do proper release of the net_device
3183 rt6_uncached_list_add(rt);
3184 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3186 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3192 static int ip6_dst_gc(struct dst_ops *ops)
3194 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3195 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3196 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3197 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3198 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3199 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3202 entries = dst_entries_get_fast(ops);
3203 if (entries > rt_max_size)
3204 entries = dst_entries_get_slow(ops);
3206 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3207 entries <= rt_max_size)
3210 net->ipv6.ip6_rt_gc_expire++;
3211 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3212 entries = dst_entries_get_slow(ops);
3213 if (entries < ops->gc_thresh)
3214 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
3216 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3217 return entries > rt_max_size;
3220 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3221 const struct in6_addr *gw_addr, u32 tbid,
3222 int flags, struct fib6_result *res)
3224 struct flowi6 fl6 = {
3225 .flowi6_oif = cfg->fc_ifindex,
3227 .saddr = cfg->fc_prefsrc,
3229 struct fib6_table *table;
3232 table = fib6_get_table(net, tbid);
3236 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3237 flags |= RT6_LOOKUP_F_HAS_SADDR;
3239 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3241 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3242 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3243 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3244 cfg->fc_ifindex != 0, NULL, flags);
3249 static int ip6_route_check_nh_onlink(struct net *net,
3250 struct fib6_config *cfg,
3251 const struct net_device *dev,
3252 struct netlink_ext_ack *extack)
3254 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3255 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3256 struct fib6_result res = {};
3259 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3260 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3261 /* ignore match if it is the default route */
3262 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3263 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3264 NL_SET_ERR_MSG(extack,
3265 "Nexthop has invalid gateway or device mismatch");
3272 static int ip6_route_check_nh(struct net *net,
3273 struct fib6_config *cfg,
3274 struct net_device **_dev,
3275 struct inet6_dev **idev)
3277 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3278 struct net_device *dev = _dev ? *_dev : NULL;
3279 int flags = RT6_LOOKUP_F_IFACE;
3280 struct fib6_result res = {};
3281 int err = -EHOSTUNREACH;
3283 if (cfg->fc_table) {
3284 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3285 cfg->fc_table, flags, &res);
3286 /* gw_addr can not require a gateway or resolve to a reject
3287 * route. If a device is given, it must match the result.
3289 if (err || res.fib6_flags & RTF_REJECT ||
3290 res.nh->fib_nh_gw_family ||
3291 (dev && dev != res.nh->fib_nh_dev))
3292 err = -EHOSTUNREACH;
3296 struct flowi6 fl6 = {
3297 .flowi6_oif = cfg->fc_ifindex,
3301 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3302 if (err || res.fib6_flags & RTF_REJECT ||
3303 res.nh->fib_nh_gw_family)
3304 err = -EHOSTUNREACH;
3309 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3310 cfg->fc_ifindex != 0, NULL, flags);
3315 if (dev != res.nh->fib_nh_dev)
3316 err = -EHOSTUNREACH;
3318 *_dev = dev = res.nh->fib_nh_dev;
3320 *idev = in6_dev_get(dev);
3326 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3327 struct net_device **_dev, struct inet6_dev **idev,
3328 struct netlink_ext_ack *extack)
3330 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3331 int gwa_type = ipv6_addr_type(gw_addr);
3332 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3333 const struct net_device *dev = *_dev;
3334 bool need_addr_check = !dev;
3337 /* if gw_addr is local we will fail to detect this in case
3338 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3339 * will return already-added prefix route via interface that
3340 * prefix route was assigned to, which might be non-loopback.
3343 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3344 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3348 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3349 /* IPv6 strictly inhibits using not link-local
3350 * addresses as nexthop address.
3351 * Otherwise, router will not able to send redirects.
3352 * It is very good, but in some (rare!) circumstances
3353 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3354 * some exceptions. --ANK
3355 * We allow IPv4-mapped nexthops to support RFC4798-type
3358 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3359 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3365 if (cfg->fc_flags & RTNH_F_ONLINK)
3366 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3368 err = ip6_route_check_nh(net, cfg, _dev, idev);
3376 /* reload in case device was changed */
3381 NL_SET_ERR_MSG(extack, "Egress device not specified");
3383 } else if (dev->flags & IFF_LOOPBACK) {
3384 NL_SET_ERR_MSG(extack,
3385 "Egress device can not be loopback device for this route");
3389 /* if we did not check gw_addr above, do so now that the
3390 * egress device has been resolved.
3392 if (need_addr_check &&
3393 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3394 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3403 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3405 if ((flags & RTF_REJECT) ||
3406 (dev && (dev->flags & IFF_LOOPBACK) &&
3407 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3408 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
3414 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3415 struct fib6_config *cfg, gfp_t gfp_flags,
3416 struct netlink_ext_ack *extack)
3418 struct net_device *dev = NULL;
3419 struct inet6_dev *idev = NULL;
3423 fib6_nh->fib_nh_family = AF_INET6;
3424 #ifdef CONFIG_IPV6_ROUTER_PREF
3425 fib6_nh->last_probe = jiffies;
3427 if (cfg->fc_is_fdb) {
3428 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3429 fib6_nh->fib_nh_gw_family = AF_INET6;
3434 if (cfg->fc_ifindex) {
3435 dev = dev_get_by_index(net, cfg->fc_ifindex);
3438 idev = in6_dev_get(dev);
3443 if (cfg->fc_flags & RTNH_F_ONLINK) {
3445 NL_SET_ERR_MSG(extack,
3446 "Nexthop device required for onlink");
3450 if (!(dev->flags & IFF_UP)) {
3451 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3456 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3459 fib6_nh->fib_nh_weight = 1;
3461 /* We cannot add true routes via loopback here,
3462 * they would result in kernel looping; promote them to reject routes
3464 addr_type = ipv6_addr_type(&cfg->fc_dst);
3465 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3466 /* hold loopback dev/idev if we haven't done so. */
3467 if (dev != net->loopback_dev) {
3472 dev = net->loopback_dev;
3474 idev = in6_dev_get(dev);
3483 if (cfg->fc_flags & RTF_GATEWAY) {
3484 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3488 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3489 fib6_nh->fib_nh_gw_family = AF_INET6;
3496 if (idev->cnf.disable_ipv6) {
3497 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3502 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3503 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3508 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3509 !netif_carrier_ok(dev))
3510 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3512 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
3513 cfg->fc_encap_type, cfg, gfp_flags, extack);
3518 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3519 if (!fib6_nh->rt6i_pcpu) {
3524 fib6_nh->fib_nh_dev = dev;
3525 fib6_nh->fib_nh_oif = dev->ifindex;
3532 lwtstate_put(fib6_nh->fib_nh_lws);
3533 fib6_nh->fib_nh_lws = NULL;
3541 void fib6_nh_release(struct fib6_nh *fib6_nh)
3543 struct rt6_exception_bucket *bucket;
3547 fib6_nh_flush_exceptions(fib6_nh, NULL);
3548 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3550 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3556 if (fib6_nh->rt6i_pcpu) {
3559 for_each_possible_cpu(cpu) {
3560 struct rt6_info **ppcpu_rt;
3561 struct rt6_info *pcpu_rt;
3563 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3564 pcpu_rt = *ppcpu_rt;
3566 dst_dev_put(&pcpu_rt->dst);
3567 dst_release(&pcpu_rt->dst);
3572 free_percpu(fib6_nh->rt6i_pcpu);
3575 fib_nh_common_release(&fib6_nh->nh_common);
3578 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3580 struct netlink_ext_ack *extack)
3582 struct net *net = cfg->fc_nlinfo.nl_net;
3583 struct fib6_info *rt = NULL;
3584 struct nexthop *nh = NULL;
3585 struct fib6_table *table;
3586 struct fib6_nh *fib6_nh;
3590 /* RTF_PCPU is an internal flag; can not be set by userspace */
3591 if (cfg->fc_flags & RTF_PCPU) {
3592 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3596 /* RTF_CACHE is an internal flag; can not be set by userspace */
3597 if (cfg->fc_flags & RTF_CACHE) {
3598 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3602 if (cfg->fc_type > RTN_MAX) {
3603 NL_SET_ERR_MSG(extack, "Invalid route type");
3607 if (cfg->fc_dst_len > 128) {
3608 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3611 if (cfg->fc_src_len > 128) {
3612 NL_SET_ERR_MSG(extack, "Invalid source address length");
3615 #ifndef CONFIG_IPV6_SUBTREES
3616 if (cfg->fc_src_len) {
3617 NL_SET_ERR_MSG(extack,
3618 "Specifying source address requires IPV6_SUBTREES to be enabled");
3622 if (cfg->fc_nh_id) {
3623 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3625 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3628 err = fib6_check_nexthop(nh, cfg, extack);
3634 if (cfg->fc_nlinfo.nlh &&
3635 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3636 table = fib6_get_table(net, cfg->fc_table);
3638 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3639 table = fib6_new_table(net, cfg->fc_table);
3642 table = fib6_new_table(net, cfg->fc_table);
3649 rt = fib6_info_alloc(gfp_flags, !nh);
3653 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3655 if (IS_ERR(rt->fib6_metrics)) {
3656 err = PTR_ERR(rt->fib6_metrics);
3657 /* Do not leave garbage there. */
3658 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3662 if (cfg->fc_flags & RTF_ADDRCONF)
3663 rt->dst_nocount = true;
3665 if (cfg->fc_flags & RTF_EXPIRES)
3666 fib6_set_expires(rt, jiffies +
3667 clock_t_to_jiffies(cfg->fc_expires));
3669 fib6_clean_expires(rt);
3671 if (cfg->fc_protocol == RTPROT_UNSPEC)
3672 cfg->fc_protocol = RTPROT_BOOT;
3673 rt->fib6_protocol = cfg->fc_protocol;
3675 rt->fib6_table = table;
3676 rt->fib6_metric = cfg->fc_metric;
3677 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3678 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3680 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3681 rt->fib6_dst.plen = cfg->fc_dst_len;
3683 #ifdef CONFIG_IPV6_SUBTREES
3684 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3685 rt->fib6_src.plen = cfg->fc_src_len;
3688 if (!nexthop_get(nh)) {
3689 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3692 if (rt->fib6_src.plen) {
3693 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3697 fib6_nh = nexthop_fib6_nh(rt->nh);
3699 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3703 fib6_nh = rt->fib6_nh;
3705 /* We cannot add true routes via loopback here, they would
3706 * result in kernel looping; promote them to reject routes
3708 addr_type = ipv6_addr_type(&cfg->fc_dst);
3709 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3711 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3714 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3715 struct net_device *dev = fib6_nh->fib_nh_dev;
3717 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3718 NL_SET_ERR_MSG(extack, "Invalid source address");
3722 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3723 rt->fib6_prefsrc.plen = 128;
3725 rt->fib6_prefsrc.plen = 0;
3729 fib6_info_release(rt);
3730 return ERR_PTR(err);
3733 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3734 struct netlink_ext_ack *extack)
3736 struct fib6_info *rt;
3739 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3743 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3744 fib6_info_release(rt);
3749 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3751 struct net *net = info->nl_net;
3752 struct fib6_table *table;
3755 if (rt == net->ipv6.fib6_null_entry) {
3760 table = rt->fib6_table;
3761 spin_lock_bh(&table->tb6_lock);
3762 err = fib6_del(rt, info);
3763 spin_unlock_bh(&table->tb6_lock);
3766 fib6_info_release(rt);
3770 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3772 struct nl_info info = {
3774 .skip_notify = skip_notify
3777 return __ip6_del_rt(rt, &info);
3780 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3782 struct nl_info *info = &cfg->fc_nlinfo;
3783 struct net *net = info->nl_net;
3784 struct sk_buff *skb = NULL;
3785 struct fib6_table *table;
3788 if (rt == net->ipv6.fib6_null_entry)
3790 table = rt->fib6_table;
3791 spin_lock_bh(&table->tb6_lock);
3793 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3794 struct fib6_info *sibling, *next_sibling;
3795 struct fib6_node *fn;
3797 /* prefer to send a single notification with all hops */
3798 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3800 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3802 if (rt6_fill_node(net, skb, rt, NULL,
3803 NULL, NULL, 0, RTM_DELROUTE,
3804 info->portid, seq, 0) < 0) {
3808 info->skip_notify = 1;
3811 /* 'rt' points to the first sibling route. If it is not the
3812 * leaf, then we do not need to send a notification. Otherwise,
3813 * we need to check if the last sibling has a next route or not
3814 * and emit a replace or delete notification, respectively.
3816 info->skip_notify_kernel = 1;
3817 fn = rcu_dereference_protected(rt->fib6_node,
3818 lockdep_is_held(&table->tb6_lock));
3819 if (rcu_access_pointer(fn->leaf) == rt) {
3820 struct fib6_info *last_sibling, *replace_rt;
3822 last_sibling = list_last_entry(&rt->fib6_siblings,
3825 replace_rt = rcu_dereference_protected(
3826 last_sibling->fib6_next,
3827 lockdep_is_held(&table->tb6_lock));
3829 call_fib6_entry_notifiers_replace(net,
3832 call_fib6_multipath_entry_notifiers(net,
3833 FIB_EVENT_ENTRY_DEL,
3834 rt, rt->fib6_nsiblings,
3837 list_for_each_entry_safe(sibling, next_sibling,
3840 err = fib6_del(sibling, info);
3846 err = fib6_del(rt, info);
3848 spin_unlock_bh(&table->tb6_lock);
3850 fib6_info_release(rt);
3853 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3854 info->nlh, gfp_any());
3859 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3863 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3866 if (cfg->fc_flags & RTF_GATEWAY &&
3867 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3870 rc = rt6_remove_exception_rt(rt);
3875 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3878 struct fib6_result res = {
3882 struct rt6_info *rt_cache;
3884 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3886 return __ip6_del_cached_rt(rt_cache, cfg);
3891 struct fib6_nh_del_cached_rt_arg {
3892 struct fib6_config *cfg;
3893 struct fib6_info *f6i;
3896 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3898 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3901 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3902 return rc != -ESRCH ? rc : 0;
3905 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3907 struct fib6_nh_del_cached_rt_arg arg = {
3912 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3915 static int ip6_route_del(struct fib6_config *cfg,
3916 struct netlink_ext_ack *extack)
3918 struct fib6_table *table;
3919 struct fib6_info *rt;
3920 struct fib6_node *fn;
3923 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3925 NL_SET_ERR_MSG(extack, "FIB table does not exist");
3931 fn = fib6_locate(&table->tb6_root,
3932 &cfg->fc_dst, cfg->fc_dst_len,
3933 &cfg->fc_src, cfg->fc_src_len,
3934 !(cfg->fc_flags & RTF_CACHE));
3937 for_each_fib6_node_rt_rcu(fn) {
3940 if (rt->nh && cfg->fc_nh_id &&
3941 rt->nh->id != cfg->fc_nh_id)
3944 if (cfg->fc_flags & RTF_CACHE) {
3948 rc = ip6_del_cached_rt_nh(cfg, rt);
3949 } else if (cfg->fc_nh_id) {
3953 rc = ip6_del_cached_rt(cfg, rt, nh);
3962 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3964 if (cfg->fc_protocol &&
3965 cfg->fc_protocol != rt->fib6_protocol)
3969 if (!fib6_info_hold_safe(rt))
3973 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3979 if (cfg->fc_ifindex &&
3981 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
3983 if (cfg->fc_flags & RTF_GATEWAY &&
3984 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
3986 if (!fib6_info_hold_safe(rt))
3990 /* if gateway was specified only delete the one hop */
3991 if (cfg->fc_flags & RTF_GATEWAY)
3992 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3994 return __ip6_del_rt_siblings(rt, cfg);
4002 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4004 struct netevent_redirect netevent;
4005 struct rt6_info *rt, *nrt = NULL;
4006 struct fib6_result res = {};
4007 struct ndisc_options ndopts;
4008 struct inet6_dev *in6_dev;
4009 struct neighbour *neigh;
4011 int optlen, on_link;
4014 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4015 optlen -= sizeof(*msg);
4018 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4022 msg = (struct rd_msg *)icmp6_hdr(skb);
4024 if (ipv6_addr_is_multicast(&msg->dest)) {
4025 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4030 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4032 } else if (ipv6_addr_type(&msg->target) !=
4033 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4034 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4038 in6_dev = __in6_dev_get(skb->dev);
4041 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4045 * The IP source address of the Redirect MUST be the same as the current
4046 * first-hop router for the specified ICMP Destination Address.
4049 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4050 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4055 if (ndopts.nd_opts_tgt_lladdr) {
4056 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4059 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4064 rt = (struct rt6_info *) dst;
4065 if (rt->rt6i_flags & RTF_REJECT) {
4066 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4070 /* Redirect received -> path was valid.
4071 * Look, redirects are sent only in response to data packets,
4072 * so that this nexthop apparently is reachable. --ANK
4074 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4076 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4081 * We have finally decided to accept it.
4084 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4085 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4086 NEIGH_UPDATE_F_OVERRIDE|
4087 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4088 NEIGH_UPDATE_F_ISROUTER)),
4089 NDISC_REDIRECT, &ndopts);
4092 res.f6i = rcu_dereference(rt->from);
4097 struct fib6_nh_match_arg arg = {
4099 .gw = &rt->rt6i_gateway,
4102 nexthop_for_each_fib6_nh(res.f6i->nh,
4103 fib6_nh_find_match, &arg);
4105 /* fib6_info uses a nexthop that does not have fib6_nh
4106 * using the dst->dev. Should be impossible
4112 res.nh = res.f6i->fib6_nh;
4115 res.fib6_flags = res.f6i->fib6_flags;
4116 res.fib6_type = res.f6i->fib6_type;
4117 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4121 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4123 nrt->rt6i_flags &= ~RTF_GATEWAY;
4125 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4127 /* rt6_insert_exception() will take care of duplicated exceptions */
4128 if (rt6_insert_exception(nrt, &res)) {
4129 dst_release_immediate(&nrt->dst);
4133 netevent.old = &rt->dst;
4134 netevent.new = &nrt->dst;
4135 netevent.daddr = &msg->dest;
4136 netevent.neigh = neigh;
4137 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4141 neigh_release(neigh);
4144 #ifdef CONFIG_IPV6_ROUTE_INFO
4145 static struct fib6_info *rt6_get_route_info(struct net *net,
4146 const struct in6_addr *prefix, int prefixlen,
4147 const struct in6_addr *gwaddr,
4148 struct net_device *dev)
4150 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4151 int ifindex = dev->ifindex;
4152 struct fib6_node *fn;
4153 struct fib6_info *rt = NULL;
4154 struct fib6_table *table;
4156 table = fib6_get_table(net, tb_id);
4161 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4165 for_each_fib6_node_rt_rcu(fn) {
4166 /* these routes do not use nexthops */
4169 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4171 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4172 !rt->fib6_nh->fib_nh_gw_family)
4174 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4176 if (!fib6_info_hold_safe(rt))
4185 static struct fib6_info *rt6_add_route_info(struct net *net,
4186 const struct in6_addr *prefix, int prefixlen,
4187 const struct in6_addr *gwaddr,
4188 struct net_device *dev,
4191 struct fib6_config cfg = {
4192 .fc_metric = IP6_RT_PRIO_USER,
4193 .fc_ifindex = dev->ifindex,
4194 .fc_dst_len = prefixlen,
4195 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4196 RTF_UP | 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_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4205 cfg.fc_dst = *prefix;
4206 cfg.fc_gateway = *gwaddr;
4208 /* We should treat it as a default route if prefix length is 0. */
4210 cfg.fc_flags |= RTF_DEFAULT;
4212 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4214 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4218 struct fib6_info *rt6_get_dflt_router(struct net *net,
4219 const struct in6_addr *addr,
4220 struct net_device *dev)
4222 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4223 struct fib6_info *rt;
4224 struct fib6_table *table;
4226 table = fib6_get_table(net, tb_id);
4231 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4234 /* RA routes do not use nexthops */
4239 if (dev == nh->fib_nh_dev &&
4240 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4241 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4244 if (rt && !fib6_info_hold_safe(rt))
4250 struct fib6_info *rt6_add_dflt_router(struct net *net,
4251 const struct in6_addr *gwaddr,
4252 struct net_device *dev,
4255 struct fib6_config cfg = {
4256 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4257 .fc_metric = IP6_RT_PRIO_USER,
4258 .fc_ifindex = dev->ifindex,
4259 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4260 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4261 .fc_protocol = RTPROT_RA,
4262 .fc_type = RTN_UNICAST,
4263 .fc_nlinfo.portid = 0,
4264 .fc_nlinfo.nlh = NULL,
4265 .fc_nlinfo.nl_net = net,
4268 cfg.fc_gateway = *gwaddr;
4270 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4271 struct fib6_table *table;
4273 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4275 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4278 return rt6_get_dflt_router(net, gwaddr, dev);
4281 static void __rt6_purge_dflt_routers(struct net *net,
4282 struct fib6_table *table)
4284 struct fib6_info *rt;
4288 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4289 struct net_device *dev = fib6_info_nh_dev(rt);
4290 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4292 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4293 (!idev || idev->cnf.accept_ra != 2) &&
4294 fib6_info_hold_safe(rt)) {
4296 ip6_del_rt(net, rt, false);
4302 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4305 void rt6_purge_dflt_routers(struct net *net)
4307 struct fib6_table *table;
4308 struct hlist_head *head;
4313 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4314 head = &net->ipv6.fib_table_hash[h];
4315 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4316 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4317 __rt6_purge_dflt_routers(net, table);
4324 static void rtmsg_to_fib6_config(struct net *net,
4325 struct in6_rtmsg *rtmsg,
4326 struct fib6_config *cfg)
4328 *cfg = (struct fib6_config){
4329 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4331 .fc_ifindex = rtmsg->rtmsg_ifindex,
4332 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4333 .fc_expires = rtmsg->rtmsg_info,
4334 .fc_dst_len = rtmsg->rtmsg_dst_len,
4335 .fc_src_len = rtmsg->rtmsg_src_len,
4336 .fc_flags = rtmsg->rtmsg_flags,
4337 .fc_type = rtmsg->rtmsg_type,
4339 .fc_nlinfo.nl_net = net,
4341 .fc_dst = rtmsg->rtmsg_dst,
4342 .fc_src = rtmsg->rtmsg_src,
4343 .fc_gateway = rtmsg->rtmsg_gateway,
4347 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
4349 struct fib6_config cfg;
4352 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4354 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4357 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4362 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4365 err = ip6_route_del(&cfg, NULL);
4373 * Drop the packet on the floor
4376 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4378 struct dst_entry *dst = skb_dst(skb);
4379 struct net *net = dev_net(dst->dev);
4380 struct inet6_dev *idev;
4383 if (netif_is_l3_master(skb->dev) &&
4384 dst->dev == net->loopback_dev)
4385 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4387 idev = ip6_dst_idev(dst);
4389 switch (ipstats_mib_noroutes) {
4390 case IPSTATS_MIB_INNOROUTES:
4391 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4392 if (type == IPV6_ADDR_ANY) {
4393 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4397 case IPSTATS_MIB_OUTNOROUTES:
4398 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4402 /* Start over by dropping the dst for l3mdev case */
4403 if (netif_is_l3_master(skb->dev))
4406 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4411 static int ip6_pkt_discard(struct sk_buff *skb)
4413 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4416 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4418 skb->dev = skb_dst(skb)->dev;
4419 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4422 static int ip6_pkt_prohibit(struct sk_buff *skb)
4424 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4427 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4429 skb->dev = skb_dst(skb)->dev;
4430 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4434 * Allocate a dst for local (unicast / anycast) address.
4437 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4438 struct inet6_dev *idev,
4439 const struct in6_addr *addr,
4440 bool anycast, gfp_t gfp_flags)
4442 struct fib6_config cfg = {
4443 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4444 .fc_ifindex = idev->dev->ifindex,
4445 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4448 .fc_protocol = RTPROT_KERNEL,
4449 .fc_nlinfo.nl_net = net,
4450 .fc_ignore_dev_down = true,
4452 struct fib6_info *f6i;
4455 cfg.fc_type = RTN_ANYCAST;
4456 cfg.fc_flags |= RTF_ANYCAST;
4458 cfg.fc_type = RTN_LOCAL;
4459 cfg.fc_flags |= RTF_LOCAL;
4462 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
4464 f6i->dst_nocount = true;
4468 /* remove deleted ip from prefsrc entries */
4469 struct arg_dev_net_ip {
4470 struct net_device *dev;
4472 struct in6_addr *addr;
4475 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4477 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4478 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4479 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4482 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4483 rt != net->ipv6.fib6_null_entry &&
4484 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4485 spin_lock_bh(&rt6_exception_lock);
4486 /* remove prefsrc entry */
4487 rt->fib6_prefsrc.plen = 0;
4488 spin_unlock_bh(&rt6_exception_lock);
4493 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4495 struct net *net = dev_net(ifp->idev->dev);
4496 struct arg_dev_net_ip adni = {
4497 .dev = ifp->idev->dev,
4501 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4504 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4506 /* Remove routers and update dst entries when gateway turn into host. */
4507 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4509 struct in6_addr *gateway = (struct in6_addr *)arg;
4512 /* RA routes do not use nexthops */
4517 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4518 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4521 /* Further clean up cached routes in exception table.
4522 * This is needed because cached route may have a different
4523 * gateway than its 'parent' in the case of an ip redirect.
4525 fib6_nh_exceptions_clean_tohost(nh, gateway);
4530 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4532 fib6_clean_all(net, fib6_clean_tohost, gateway);
4535 struct arg_netdev_event {
4536 const struct net_device *dev;
4538 unsigned char nh_flags;
4539 unsigned long event;
4543 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4545 struct fib6_info *iter;
4546 struct fib6_node *fn;
4548 fn = rcu_dereference_protected(rt->fib6_node,
4549 lockdep_is_held(&rt->fib6_table->tb6_lock));
4550 iter = rcu_dereference_protected(fn->leaf,
4551 lockdep_is_held(&rt->fib6_table->tb6_lock));
4553 if (iter->fib6_metric == rt->fib6_metric &&
4554 rt6_qualify_for_ecmp(iter))
4556 iter = rcu_dereference_protected(iter->fib6_next,
4557 lockdep_is_held(&rt->fib6_table->tb6_lock));
4563 /* only called for fib entries with builtin fib6_nh */
4564 static bool rt6_is_dead(const struct fib6_info *rt)
4566 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4567 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4568 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4574 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4576 struct fib6_info *iter;
4579 if (!rt6_is_dead(rt))
4580 total += rt->fib6_nh->fib_nh_weight;
4582 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4583 if (!rt6_is_dead(iter))
4584 total += iter->fib6_nh->fib_nh_weight;
4590 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4592 int upper_bound = -1;
4594 if (!rt6_is_dead(rt)) {
4595 *weight += rt->fib6_nh->fib_nh_weight;
4596 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4599 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4602 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4604 struct fib6_info *iter;
4607 rt6_upper_bound_set(rt, &weight, total);
4609 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4610 rt6_upper_bound_set(iter, &weight, total);
4613 void rt6_multipath_rebalance(struct fib6_info *rt)
4615 struct fib6_info *first;
4618 /* In case the entire multipath route was marked for flushing,
4619 * then there is no need to rebalance upon the removal of every
4622 if (!rt->fib6_nsiblings || rt->should_flush)
4625 /* During lookup routes are evaluated in order, so we need to
4626 * make sure upper bounds are assigned from the first sibling
4629 first = rt6_multipath_first_sibling(rt);
4630 if (WARN_ON_ONCE(!first))
4633 total = rt6_multipath_total_weight(first);
4634 rt6_multipath_upper_bound_set(first, total);
4637 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4639 const struct arg_netdev_event *arg = p_arg;
4640 struct net *net = dev_net(arg->dev);
4642 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4643 rt->fib6_nh->fib_nh_dev == arg->dev) {
4644 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4645 fib6_update_sernum_upto_root(net, rt);
4646 rt6_multipath_rebalance(rt);
4652 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4654 struct arg_netdev_event arg = {
4657 .nh_flags = nh_flags,
4661 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4662 arg.nh_flags |= RTNH_F_LINKDOWN;
4664 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4667 /* only called for fib entries with inline fib6_nh */
4668 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4669 const struct net_device *dev)
4671 struct fib6_info *iter;
4673 if (rt->fib6_nh->fib_nh_dev == dev)
4675 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4676 if (iter->fib6_nh->fib_nh_dev == dev)
4682 static void rt6_multipath_flush(struct fib6_info *rt)
4684 struct fib6_info *iter;
4686 rt->should_flush = 1;
4687 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4688 iter->should_flush = 1;
4691 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4692 const struct net_device *down_dev)
4694 struct fib6_info *iter;
4695 unsigned int dead = 0;
4697 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4698 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4700 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4701 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4702 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4708 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4709 const struct net_device *dev,
4710 unsigned char nh_flags)
4712 struct fib6_info *iter;
4714 if (rt->fib6_nh->fib_nh_dev == dev)
4715 rt->fib6_nh->fib_nh_flags |= nh_flags;
4716 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4717 if (iter->fib6_nh->fib_nh_dev == dev)
4718 iter->fib6_nh->fib_nh_flags |= nh_flags;
4721 /* called with write lock held for table with rt */
4722 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4724 const struct arg_netdev_event *arg = p_arg;
4725 const struct net_device *dev = arg->dev;
4726 struct net *net = dev_net(dev);
4728 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4731 switch (arg->event) {
4732 case NETDEV_UNREGISTER:
4733 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4735 if (rt->should_flush)
4737 if (!rt->fib6_nsiblings)
4738 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4739 if (rt6_multipath_uses_dev(rt, dev)) {
4742 count = rt6_multipath_dead_count(rt, dev);
4743 if (rt->fib6_nsiblings + 1 == count) {
4744 rt6_multipath_flush(rt);
4747 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4749 fib6_update_sernum(net, rt);
4750 rt6_multipath_rebalance(rt);
4754 if (rt->fib6_nh->fib_nh_dev != dev ||
4755 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4757 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4758 rt6_multipath_rebalance(rt);
4765 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4767 struct arg_netdev_event arg = {
4773 struct net *net = dev_net(dev);
4775 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4776 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4778 fib6_clean_all(net, fib6_ifdown, &arg);
4781 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4783 rt6_sync_down_dev(dev, event);
4784 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4785 neigh_ifdown(&nd_tbl, dev);
4788 struct rt6_mtu_change_arg {
4789 struct net_device *dev;
4791 struct fib6_info *f6i;
4794 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4796 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4797 struct fib6_info *f6i = arg->f6i;
4799 /* For administrative MTU increase, there is no way to discover
4800 * IPv6 PMTU increase, so PMTU increase should be updated here.
4801 * Since RFC 1981 doesn't include administrative MTU increase
4802 * update PMTU increase is a MUST. (i.e. jumbo frame)
4804 if (nh->fib_nh_dev == arg->dev) {
4805 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4806 u32 mtu = f6i->fib6_pmtu;
4808 if (mtu >= arg->mtu ||
4809 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4810 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4812 spin_lock_bh(&rt6_exception_lock);
4813 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4814 spin_unlock_bh(&rt6_exception_lock);
4820 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4822 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4823 struct inet6_dev *idev;
4825 /* In IPv6 pmtu discovery is not optional,
4826 so that RTAX_MTU lock cannot disable it.
4827 We still use this lock to block changes
4828 caused by addrconf/ndisc.
4831 idev = __in6_dev_get(arg->dev);
4835 if (fib6_metric_locked(f6i, RTAX_MTU))
4840 /* fib6_nh_mtu_change only returns 0, so this is safe */
4841 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4845 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4848 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4850 struct rt6_mtu_change_arg arg = {
4855 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4858 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4859 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
4860 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4861 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4862 [RTA_OIF] = { .type = NLA_U32 },
4863 [RTA_IIF] = { .type = NLA_U32 },
4864 [RTA_PRIORITY] = { .type = NLA_U32 },
4865 [RTA_METRICS] = { .type = NLA_NESTED },
4866 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4867 [RTA_PREF] = { .type = NLA_U8 },
4868 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4869 [RTA_ENCAP] = { .type = NLA_NESTED },
4870 [RTA_EXPIRES] = { .type = NLA_U32 },
4871 [RTA_UID] = { .type = NLA_U32 },
4872 [RTA_MARK] = { .type = NLA_U32 },
4873 [RTA_TABLE] = { .type = NLA_U32 },
4874 [RTA_IP_PROTO] = { .type = NLA_U8 },
4875 [RTA_SPORT] = { .type = NLA_U16 },
4876 [RTA_DPORT] = { .type = NLA_U16 },
4877 [RTA_NH_ID] = { .type = NLA_U32 },
4880 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4881 struct fib6_config *cfg,
4882 struct netlink_ext_ack *extack)
4885 struct nlattr *tb[RTA_MAX+1];
4889 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4890 rtm_ipv6_policy, extack);
4895 rtm = nlmsg_data(nlh);
4897 *cfg = (struct fib6_config){
4898 .fc_table = rtm->rtm_table,
4899 .fc_dst_len = rtm->rtm_dst_len,
4900 .fc_src_len = rtm->rtm_src_len,
4902 .fc_protocol = rtm->rtm_protocol,
4903 .fc_type = rtm->rtm_type,
4905 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4906 .fc_nlinfo.nlh = nlh,
4907 .fc_nlinfo.nl_net = sock_net(skb->sk),
4910 if (rtm->rtm_type == RTN_UNREACHABLE ||
4911 rtm->rtm_type == RTN_BLACKHOLE ||
4912 rtm->rtm_type == RTN_PROHIBIT ||
4913 rtm->rtm_type == RTN_THROW)
4914 cfg->fc_flags |= RTF_REJECT;
4916 if (rtm->rtm_type == RTN_LOCAL)
4917 cfg->fc_flags |= RTF_LOCAL;
4919 if (rtm->rtm_flags & RTM_F_CLONED)
4920 cfg->fc_flags |= RTF_CACHE;
4922 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4924 if (tb[RTA_NH_ID]) {
4925 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4926 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4927 NL_SET_ERR_MSG(extack,
4928 "Nexthop specification and nexthop id are mutually exclusive");
4931 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4934 if (tb[RTA_GATEWAY]) {
4935 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4936 cfg->fc_flags |= RTF_GATEWAY;
4939 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4944 int plen = (rtm->rtm_dst_len + 7) >> 3;
4946 if (nla_len(tb[RTA_DST]) < plen)
4949 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4953 int plen = (rtm->rtm_src_len + 7) >> 3;
4955 if (nla_len(tb[RTA_SRC]) < plen)
4958 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4961 if (tb[RTA_PREFSRC])
4962 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4965 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4967 if (tb[RTA_PRIORITY])
4968 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4970 if (tb[RTA_METRICS]) {
4971 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4972 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4976 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4978 if (tb[RTA_MULTIPATH]) {
4979 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4980 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
4982 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4983 cfg->fc_mp_len, extack);
4989 pref = nla_get_u8(tb[RTA_PREF]);
4990 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4991 pref != ICMPV6_ROUTER_PREF_HIGH)
4992 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4993 cfg->fc_flags |= RTF_PREF(pref);
4997 cfg->fc_encap = tb[RTA_ENCAP];
4999 if (tb[RTA_ENCAP_TYPE]) {
5000 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5002 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5007 if (tb[RTA_EXPIRES]) {
5008 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5010 if (addrconf_finite_timeout(timeout)) {
5011 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5012 cfg->fc_flags |= RTF_EXPIRES;
5022 struct fib6_info *fib6_info;
5023 struct fib6_config r_cfg;
5024 struct list_head next;
5027 static int ip6_route_info_append(struct net *net,
5028 struct list_head *rt6_nh_list,
5029 struct fib6_info *rt,
5030 struct fib6_config *r_cfg)
5035 list_for_each_entry(nh, rt6_nh_list, next) {
5036 /* check if fib6_info already exists */
5037 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5041 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5045 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5046 list_add_tail(&nh->next, rt6_nh_list);
5051 static void ip6_route_mpath_notify(struct fib6_info *rt,
5052 struct fib6_info *rt_last,
5053 struct nl_info *info,
5056 /* if this is an APPEND route, then rt points to the first route
5057 * inserted and rt_last points to last route inserted. Userspace
5058 * wants a consistent dump of the route which starts at the first
5059 * nexthop. Since sibling routes are always added at the end of
5060 * the list, find the first sibling of the last route appended
5062 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5063 rt = list_first_entry(&rt_last->fib6_siblings,
5069 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5072 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5074 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5075 bool should_notify = false;
5076 struct fib6_info *leaf;
5077 struct fib6_node *fn;
5080 fn = rcu_dereference(rt->fib6_node);
5084 leaf = rcu_dereference(fn->leaf);
5089 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5090 rt6_qualify_for_ecmp(leaf)))
5091 should_notify = true;
5095 return should_notify;
5098 static int ip6_route_multipath_add(struct fib6_config *cfg,
5099 struct netlink_ext_ack *extack)
5101 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5102 struct nl_info *info = &cfg->fc_nlinfo;
5103 struct fib6_config r_cfg;
5104 struct rtnexthop *rtnh;
5105 struct fib6_info *rt;
5106 struct rt6_nh *err_nh;
5107 struct rt6_nh *nh, *nh_safe;
5113 int replace = (cfg->fc_nlinfo.nlh &&
5114 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5115 LIST_HEAD(rt6_nh_list);
5117 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5118 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5119 nlflags |= NLM_F_APPEND;
5121 remaining = cfg->fc_mp_len;
5122 rtnh = (struct rtnexthop *)cfg->fc_mp;
5124 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5125 * fib6_info structs per nexthop
5127 while (rtnh_ok(rtnh, remaining)) {
5128 memcpy(&r_cfg, cfg, sizeof(*cfg));
5129 if (rtnh->rtnh_ifindex)
5130 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5132 attrlen = rtnh_attrlen(rtnh);
5134 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5136 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5138 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5139 r_cfg.fc_flags |= RTF_GATEWAY;
5141 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5142 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5144 r_cfg.fc_encap_type = nla_get_u16(nla);
5147 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5148 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5154 if (!rt6_qualify_for_ecmp(rt)) {
5156 NL_SET_ERR_MSG(extack,
5157 "Device only routes can not be added for IPv6 using the multipath API.");
5158 fib6_info_release(rt);
5162 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5164 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5167 fib6_info_release(rt);
5171 rtnh = rtnh_next(rtnh, &remaining);
5174 if (list_empty(&rt6_nh_list)) {
5175 NL_SET_ERR_MSG(extack,
5176 "Invalid nexthop configuration - no valid nexthops");
5180 /* for add and replace send one notification with all nexthops.
5181 * Skip the notification in fib6_add_rt2node and send one with
5182 * the full route when done
5184 info->skip_notify = 1;
5186 /* For add and replace, send one notification with all nexthops. For
5187 * append, send one notification with all appended nexthops.
5189 info->skip_notify_kernel = 1;
5192 list_for_each_entry(nh, &rt6_nh_list, next) {
5193 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5194 fib6_info_release(nh->fib6_info);
5197 /* save reference to last route successfully inserted */
5198 rt_last = nh->fib6_info;
5200 /* save reference to first route for notification */
5202 rt_notif = nh->fib6_info;
5205 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5206 nh->fib6_info = NULL;
5209 NL_SET_ERR_MSG_MOD(extack,
5210 "multipath route replace failed (check consistency of installed routes)");
5215 /* Because each route is added like a single route we remove
5216 * these flags after the first nexthop: if there is a collision,
5217 * we have already failed to add the first nexthop:
5218 * fib6_add_rt2node() has rejected it; when replacing, old
5219 * nexthops have been replaced by first new, the rest should
5222 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5224 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5228 /* An in-kernel notification should only be sent in case the new
5229 * multipath route is added as the first route in the node, or if
5230 * it was appended to it. We pass 'rt_notif' since it is the first
5231 * sibling and might allow us to skip some checks in the replace case.
5233 if (ip6_route_mpath_should_notify(rt_notif)) {
5234 enum fib_event_type fib_event;
5236 if (rt_notif->fib6_nsiblings != nhn - 1)
5237 fib_event = FIB_EVENT_ENTRY_APPEND;
5239 fib_event = FIB_EVENT_ENTRY_REPLACE;
5241 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5242 fib_event, rt_notif,
5245 /* Delete all the siblings that were just added */
5251 /* success ... tell user about new route */
5252 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5256 /* send notification for routes that were added so that
5257 * the delete notifications sent by ip6_route_del are
5261 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5263 /* Delete routes that were already added */
5264 list_for_each_entry(nh, &rt6_nh_list, next) {
5267 ip6_route_del(&nh->r_cfg, extack);
5271 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5273 fib6_info_release(nh->fib6_info);
5274 list_del(&nh->next);
5281 static int ip6_route_multipath_del(struct fib6_config *cfg,
5282 struct netlink_ext_ack *extack)
5284 struct fib6_config r_cfg;
5285 struct rtnexthop *rtnh;
5288 int err = 1, last_err = 0;
5290 remaining = cfg->fc_mp_len;
5291 rtnh = (struct rtnexthop *)cfg->fc_mp;
5293 /* Parse a Multipath Entry */
5294 while (rtnh_ok(rtnh, remaining)) {
5295 memcpy(&r_cfg, cfg, sizeof(*cfg));
5296 if (rtnh->rtnh_ifindex)
5297 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5299 attrlen = rtnh_attrlen(rtnh);
5301 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5303 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5305 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5306 r_cfg.fc_flags |= RTF_GATEWAY;
5309 err = ip6_route_del(&r_cfg, extack);
5313 rtnh = rtnh_next(rtnh, &remaining);
5319 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5320 struct netlink_ext_ack *extack)
5322 struct fib6_config cfg;
5325 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5330 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5331 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5336 return ip6_route_multipath_del(&cfg, extack);
5338 cfg.fc_delete_all_nh = 1;
5339 return ip6_route_del(&cfg, extack);
5343 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5344 struct netlink_ext_ack *extack)
5346 struct fib6_config cfg;
5349 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5353 if (cfg.fc_metric == 0)
5354 cfg.fc_metric = IP6_RT_PRIO_USER;
5357 return ip6_route_multipath_add(&cfg, extack);
5359 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5362 /* add the overhead of this fib6_nh to nexthop_len */
5363 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5365 int *nexthop_len = arg;
5367 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5368 + NLA_ALIGN(sizeof(struct rtnexthop))
5369 + nla_total_size(16); /* RTA_GATEWAY */
5371 if (nh->fib_nh_lws) {
5372 /* RTA_ENCAP_TYPE */
5373 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5375 *nexthop_len += nla_total_size(2);
5381 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5386 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5387 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5390 struct fib6_nh *nh = f6i->fib6_nh;
5393 if (f6i->fib6_nsiblings) {
5394 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5395 + NLA_ALIGN(sizeof(struct rtnexthop))
5396 + nla_total_size(16) /* RTA_GATEWAY */
5397 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5399 nexthop_len *= f6i->fib6_nsiblings;
5401 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5404 return NLMSG_ALIGN(sizeof(struct rtmsg))
5405 + nla_total_size(16) /* RTA_SRC */
5406 + nla_total_size(16) /* RTA_DST */
5407 + nla_total_size(16) /* RTA_GATEWAY */
5408 + nla_total_size(16) /* RTA_PREFSRC */
5409 + nla_total_size(4) /* RTA_TABLE */
5410 + nla_total_size(4) /* RTA_IIF */
5411 + nla_total_size(4) /* RTA_OIF */
5412 + nla_total_size(4) /* RTA_PRIORITY */
5413 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5414 + nla_total_size(sizeof(struct rta_cacheinfo))
5415 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5416 + nla_total_size(1) /* RTA_PREF */
5420 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5421 unsigned char *flags)
5423 if (nexthop_is_multipath(nh)) {
5426 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5428 goto nla_put_failure;
5430 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5431 goto nla_put_failure;
5433 nla_nest_end(skb, mp);
5435 struct fib6_nh *fib6_nh;
5437 fib6_nh = nexthop_fib6_nh(nh);
5438 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5440 goto nla_put_failure;
5449 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5450 struct fib6_info *rt, struct dst_entry *dst,
5451 struct in6_addr *dest, struct in6_addr *src,
5452 int iif, int type, u32 portid, u32 seq,
5455 struct rt6_info *rt6 = (struct rt6_info *)dst;
5456 struct rt6key *rt6_dst, *rt6_src;
5457 u32 *pmetrics, table, rt6_flags;
5458 unsigned char nh_flags = 0;
5459 struct nlmsghdr *nlh;
5463 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5468 rt6_dst = &rt6->rt6i_dst;
5469 rt6_src = &rt6->rt6i_src;
5470 rt6_flags = rt6->rt6i_flags;
5472 rt6_dst = &rt->fib6_dst;
5473 rt6_src = &rt->fib6_src;
5474 rt6_flags = rt->fib6_flags;
5477 rtm = nlmsg_data(nlh);
5478 rtm->rtm_family = AF_INET6;
5479 rtm->rtm_dst_len = rt6_dst->plen;
5480 rtm->rtm_src_len = rt6_src->plen;
5483 table = rt->fib6_table->tb6_id;
5485 table = RT6_TABLE_UNSPEC;
5486 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5487 if (nla_put_u32(skb, RTA_TABLE, table))
5488 goto nla_put_failure;
5490 rtm->rtm_type = rt->fib6_type;
5492 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5493 rtm->rtm_protocol = rt->fib6_protocol;
5495 if (rt6_flags & RTF_CACHE)
5496 rtm->rtm_flags |= RTM_F_CLONED;
5499 if (nla_put_in6_addr(skb, RTA_DST, dest))
5500 goto nla_put_failure;
5501 rtm->rtm_dst_len = 128;
5502 } else if (rtm->rtm_dst_len)
5503 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5504 goto nla_put_failure;
5505 #ifdef CONFIG_IPV6_SUBTREES
5507 if (nla_put_in6_addr(skb, RTA_SRC, src))
5508 goto nla_put_failure;
5509 rtm->rtm_src_len = 128;
5510 } else if (rtm->rtm_src_len &&
5511 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5512 goto nla_put_failure;
5515 #ifdef CONFIG_IPV6_MROUTE
5516 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5517 int err = ip6mr_get_route(net, skb, rtm, portid);
5522 goto nla_put_failure;
5525 if (nla_put_u32(skb, RTA_IIF, iif))
5526 goto nla_put_failure;
5528 struct in6_addr saddr_buf;
5529 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5530 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5531 goto nla_put_failure;
5534 if (rt->fib6_prefsrc.plen) {
5535 struct in6_addr saddr_buf;
5536 saddr_buf = rt->fib6_prefsrc.addr;
5537 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5538 goto nla_put_failure;
5541 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5542 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5543 goto nla_put_failure;
5545 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5546 goto nla_put_failure;
5548 /* For multipath routes, walk the siblings list and add
5549 * each as a nexthop within RTA_MULTIPATH.
5552 if (rt6_flags & RTF_GATEWAY &&
5553 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5554 goto nla_put_failure;
5556 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5557 goto nla_put_failure;
5558 } else if (rt->fib6_nsiblings) {
5559 struct fib6_info *sibling, *next_sibling;
5562 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5564 goto nla_put_failure;
5566 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5567 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5568 goto nla_put_failure;
5570 list_for_each_entry_safe(sibling, next_sibling,
5571 &rt->fib6_siblings, fib6_siblings) {
5572 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5573 sibling->fib6_nh->fib_nh_weight,
5575 goto nla_put_failure;
5578 nla_nest_end(skb, mp);
5579 } else if (rt->nh) {
5580 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5581 goto nla_put_failure;
5583 if (nexthop_is_blackhole(rt->nh))
5584 rtm->rtm_type = RTN_BLACKHOLE;
5586 if (net->ipv4.sysctl_nexthop_compat_mode &&
5587 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5588 goto nla_put_failure;
5590 rtm->rtm_flags |= nh_flags;
5592 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5593 &nh_flags, false) < 0)
5594 goto nla_put_failure;
5596 rtm->rtm_flags |= nh_flags;
5599 if (rt6_flags & RTF_EXPIRES) {
5600 expires = dst ? dst->expires : rt->expires;
5606 rtm->rtm_flags |= RTM_F_OFFLOAD;
5608 rtm->rtm_flags |= RTM_F_TRAP;
5611 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5612 goto nla_put_failure;
5614 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5615 goto nla_put_failure;
5618 nlmsg_end(skb, nlh);
5622 nlmsg_cancel(skb, nlh);
5626 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5628 const struct net_device *dev = arg;
5630 if (nh->fib_nh_dev == dev)
5636 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5637 const struct net_device *dev)
5640 struct net_device *_dev = (struct net_device *)dev;
5642 return !!nexthop_for_each_fib6_nh(f6i->nh,
5643 fib6_info_nh_uses_dev,
5647 if (f6i->fib6_nh->fib_nh_dev == dev)
5650 if (f6i->fib6_nsiblings) {
5651 struct fib6_info *sibling, *next_sibling;
5653 list_for_each_entry_safe(sibling, next_sibling,
5654 &f6i->fib6_siblings, fib6_siblings) {
5655 if (sibling->fib6_nh->fib_nh_dev == dev)
5663 struct fib6_nh_exception_dump_walker {
5664 struct rt6_rtnl_dump_arg *dump;
5665 struct fib6_info *rt;
5671 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5673 struct fib6_nh_exception_dump_walker *w = arg;
5674 struct rt6_rtnl_dump_arg *dump = w->dump;
5675 struct rt6_exception_bucket *bucket;
5676 struct rt6_exception *rt6_ex;
5679 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5683 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5684 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5690 /* Expiration of entries doesn't bump sernum, insertion
5691 * does. Removal is triggered by insertion, so we can
5692 * rely on the fact that if entries change between two
5693 * partial dumps, this node is scanned again completely,
5694 * see rt6_insert_exception() and fib6_dump_table().
5696 * Count expired entries we go through as handled
5697 * entries that we'll skip next time, in case of partial
5698 * node dump. Otherwise, if entries expire meanwhile,
5699 * we'll skip the wrong amount.
5701 if (rt6_check_expired(rt6_ex->rt6i)) {
5706 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5707 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5709 NETLINK_CB(dump->cb->skb).portid,
5710 dump->cb->nlh->nlmsg_seq, w->flags);
5722 /* Return -1 if done with node, number of handled routes on partial dump */
5723 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5725 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5726 struct fib_dump_filter *filter = &arg->filter;
5727 unsigned int flags = NLM_F_MULTI;
5728 struct net *net = arg->net;
5731 if (rt == net->ipv6.fib6_null_entry)
5734 if ((filter->flags & RTM_F_PREFIX) &&
5735 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5736 /* success since this is not a prefix route */
5739 if (filter->filter_set &&
5740 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5741 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5742 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5746 if (filter->filter_set ||
5747 !filter->dump_routes || !filter->dump_exceptions) {
5748 flags |= NLM_F_DUMP_FILTERED;
5751 if (filter->dump_routes) {
5755 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5757 NETLINK_CB(arg->cb->skb).portid,
5758 arg->cb->nlh->nlmsg_seq, flags)) {
5765 if (filter->dump_exceptions) {
5766 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5775 err = nexthop_for_each_fib6_nh(rt->nh,
5776 rt6_nh_dump_exceptions,
5779 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5784 return count += w.count;
5790 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5791 const struct nlmsghdr *nlh,
5793 struct netlink_ext_ack *extack)
5798 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5799 NL_SET_ERR_MSG_MOD(extack,
5800 "Invalid header for get route request");
5804 if (!netlink_strict_get_check(skb))
5805 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5806 rtm_ipv6_policy, extack);
5808 rtm = nlmsg_data(nlh);
5809 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5810 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5811 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5813 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5816 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5817 NL_SET_ERR_MSG_MOD(extack,
5818 "Invalid flags for get route request");
5822 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5823 rtm_ipv6_policy, extack);
5827 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5828 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5829 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5833 for (i = 0; i <= RTA_MAX; i++) {
5849 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5857 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5858 struct netlink_ext_ack *extack)
5860 struct net *net = sock_net(in_skb->sk);
5861 struct nlattr *tb[RTA_MAX+1];
5862 int err, iif = 0, oif = 0;
5863 struct fib6_info *from;
5864 struct dst_entry *dst;
5865 struct rt6_info *rt;
5866 struct sk_buff *skb;
5868 struct flowi6 fl6 = {};
5871 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5876 rtm = nlmsg_data(nlh);
5877 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5878 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5881 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5884 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5888 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5891 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5895 iif = nla_get_u32(tb[RTA_IIF]);
5898 oif = nla_get_u32(tb[RTA_OIF]);
5901 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5904 fl6.flowi6_uid = make_kuid(current_user_ns(),
5905 nla_get_u32(tb[RTA_UID]));
5907 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5910 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5913 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5915 if (tb[RTA_IP_PROTO]) {
5916 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
5917 &fl6.flowi6_proto, AF_INET6,
5924 struct net_device *dev;
5929 dev = dev_get_by_index_rcu(net, iif);
5936 fl6.flowi6_iif = iif;
5938 if (!ipv6_addr_any(&fl6.saddr))
5939 flags |= RT6_LOOKUP_F_HAS_SADDR;
5941 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5945 fl6.flowi6_oif = oif;
5947 dst = ip6_route_output(net, NULL, &fl6);
5951 rt = container_of(dst, struct rt6_info, dst);
5952 if (rt->dst.error) {
5953 err = rt->dst.error;
5958 if (rt == net->ipv6.ip6_null_entry) {
5959 err = rt->dst.error;
5964 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
5971 skb_dst_set(skb, &rt->dst);
5974 from = rcu_dereference(rt->from);
5977 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5979 NETLINK_CB(in_skb).portid,
5982 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5983 &fl6.saddr, iif, RTM_NEWROUTE,
5984 NETLINK_CB(in_skb).portid,
5996 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6001 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6002 unsigned int nlm_flags)
6004 struct sk_buff *skb;
6005 struct net *net = info->nl_net;
6010 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6012 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6016 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6017 event, info->portid, seq, nlm_flags);
6019 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6020 WARN_ON(err == -EMSGSIZE);
6024 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6025 info->nlh, gfp_any());
6029 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6032 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6033 struct nl_info *info)
6035 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6036 struct sk_buff *skb;
6039 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6040 * is implemented and supported for nexthop objects
6042 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6044 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6048 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6049 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6051 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6052 WARN_ON(err == -EMSGSIZE);
6056 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6057 info->nlh, gfp_any());
6061 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6064 static int ip6_route_dev_notify(struct notifier_block *this,
6065 unsigned long event, void *ptr)
6067 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6068 struct net *net = dev_net(dev);
6070 if (!(dev->flags & IFF_LOOPBACK))
6073 if (event == NETDEV_REGISTER) {
6074 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6075 net->ipv6.ip6_null_entry->dst.dev = dev;
6076 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6077 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6078 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6079 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6080 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6081 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6083 } else if (event == NETDEV_UNREGISTER &&
6084 dev->reg_state != NETREG_UNREGISTERED) {
6085 /* NETDEV_UNREGISTER could be fired for multiple times by
6086 * netdev_wait_allrefs(). Make sure we only call this once.
6088 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6089 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6090 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6091 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6102 #ifdef CONFIG_PROC_FS
6103 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6105 struct net *net = (struct net *)seq->private;
6106 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6107 net->ipv6.rt6_stats->fib_nodes,
6108 net->ipv6.rt6_stats->fib_route_nodes,
6109 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6110 net->ipv6.rt6_stats->fib_rt_entries,
6111 net->ipv6.rt6_stats->fib_rt_cache,
6112 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6113 net->ipv6.rt6_stats->fib_discarded_routes);
6117 #endif /* CONFIG_PROC_FS */
6119 #ifdef CONFIG_SYSCTL
6121 static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6122 void *buffer, size_t *lenp, loff_t *ppos)
6130 net = (struct net *)ctl->extra1;
6131 delay = net->ipv6.sysctl.flush_delay;
6132 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6136 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6140 static struct ctl_table ipv6_route_table_template[] = {
6142 .procname = "flush",
6143 .data = &init_net.ipv6.sysctl.flush_delay,
6144 .maxlen = sizeof(int),
6146 .proc_handler = ipv6_sysctl_rtcache_flush
6149 .procname = "gc_thresh",
6150 .data = &ip6_dst_ops_template.gc_thresh,
6151 .maxlen = sizeof(int),
6153 .proc_handler = proc_dointvec,
6156 .procname = "max_size",
6157 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6158 .maxlen = sizeof(int),
6160 .proc_handler = proc_dointvec,
6163 .procname = "gc_min_interval",
6164 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6165 .maxlen = sizeof(int),
6167 .proc_handler = proc_dointvec_jiffies,
6170 .procname = "gc_timeout",
6171 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6172 .maxlen = sizeof(int),
6174 .proc_handler = proc_dointvec_jiffies,
6177 .procname = "gc_interval",
6178 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6179 .maxlen = sizeof(int),
6181 .proc_handler = proc_dointvec_jiffies,
6184 .procname = "gc_elasticity",
6185 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6186 .maxlen = sizeof(int),
6188 .proc_handler = proc_dointvec,
6191 .procname = "mtu_expires",
6192 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6193 .maxlen = sizeof(int),
6195 .proc_handler = proc_dointvec_jiffies,
6198 .procname = "min_adv_mss",
6199 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6200 .maxlen = sizeof(int),
6202 .proc_handler = proc_dointvec,
6205 .procname = "gc_min_interval_ms",
6206 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6207 .maxlen = sizeof(int),
6209 .proc_handler = proc_dointvec_ms_jiffies,
6212 .procname = "skip_notify_on_dev_down",
6213 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6214 .maxlen = sizeof(int),
6216 .proc_handler = proc_dointvec_minmax,
6217 .extra1 = SYSCTL_ZERO,
6218 .extra2 = SYSCTL_ONE,
6223 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6225 struct ctl_table *table;
6227 table = kmemdup(ipv6_route_table_template,
6228 sizeof(ipv6_route_table_template),
6232 table[0].data = &net->ipv6.sysctl.flush_delay;
6233 table[0].extra1 = net;
6234 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6235 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6236 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6237 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6238 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6239 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6240 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6241 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6242 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6243 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6245 /* Don't export sysctls to unprivileged users */
6246 if (net->user_ns != &init_user_ns)
6247 table[0].procname = NULL;
6254 static int __net_init ip6_route_net_init(struct net *net)
6258 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6259 sizeof(net->ipv6.ip6_dst_ops));
6261 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6262 goto out_ip6_dst_ops;
6264 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6265 if (!net->ipv6.fib6_null_entry)
6266 goto out_ip6_dst_entries;
6267 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6268 sizeof(*net->ipv6.fib6_null_entry));
6270 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6271 sizeof(*net->ipv6.ip6_null_entry),
6273 if (!net->ipv6.ip6_null_entry)
6274 goto out_fib6_null_entry;
6275 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6276 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6277 ip6_template_metrics, true);
6278 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
6280 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6281 net->ipv6.fib6_has_custom_rules = false;
6282 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6283 sizeof(*net->ipv6.ip6_prohibit_entry),
6285 if (!net->ipv6.ip6_prohibit_entry)
6286 goto out_ip6_null_entry;
6287 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6288 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6289 ip6_template_metrics, true);
6290 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
6292 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6293 sizeof(*net->ipv6.ip6_blk_hole_entry),
6295 if (!net->ipv6.ip6_blk_hole_entry)
6296 goto out_ip6_prohibit_entry;
6297 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6298 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6299 ip6_template_metrics, true);
6300 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6301 #ifdef CONFIG_IPV6_SUBTREES
6302 net->ipv6.fib6_routes_require_src = 0;
6306 net->ipv6.sysctl.flush_delay = 0;
6307 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6308 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6309 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6310 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6311 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6312 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6313 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6314 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6316 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6322 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6323 out_ip6_prohibit_entry:
6324 kfree(net->ipv6.ip6_prohibit_entry);
6326 kfree(net->ipv6.ip6_null_entry);
6328 out_fib6_null_entry:
6329 kfree(net->ipv6.fib6_null_entry);
6330 out_ip6_dst_entries:
6331 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6336 static void __net_exit ip6_route_net_exit(struct net *net)
6338 kfree(net->ipv6.fib6_null_entry);
6339 kfree(net->ipv6.ip6_null_entry);
6340 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6341 kfree(net->ipv6.ip6_prohibit_entry);
6342 kfree(net->ipv6.ip6_blk_hole_entry);
6344 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6347 static int __net_init ip6_route_net_init_late(struct net *net)
6349 #ifdef CONFIG_PROC_FS
6350 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6351 sizeof(struct ipv6_route_iter));
6352 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6353 rt6_stats_seq_show, NULL);
6358 static void __net_exit ip6_route_net_exit_late(struct net *net)
6360 #ifdef CONFIG_PROC_FS
6361 remove_proc_entry("ipv6_route", net->proc_net);
6362 remove_proc_entry("rt6_stats", net->proc_net);
6366 static struct pernet_operations ip6_route_net_ops = {
6367 .init = ip6_route_net_init,
6368 .exit = ip6_route_net_exit,
6371 static int __net_init ipv6_inetpeer_init(struct net *net)
6373 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6377 inet_peer_base_init(bp);
6378 net->ipv6.peers = bp;
6382 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6384 struct inet_peer_base *bp = net->ipv6.peers;
6386 net->ipv6.peers = NULL;
6387 inetpeer_invalidate_tree(bp);
6391 static struct pernet_operations ipv6_inetpeer_ops = {
6392 .init = ipv6_inetpeer_init,
6393 .exit = ipv6_inetpeer_exit,
6396 static struct pernet_operations ip6_route_net_late_ops = {
6397 .init = ip6_route_net_init_late,
6398 .exit = ip6_route_net_exit_late,
6401 static struct notifier_block ip6_route_dev_notifier = {
6402 .notifier_call = ip6_route_dev_notify,
6403 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6406 void __init ip6_route_init_special_entries(void)
6408 /* Registering of the loopback is done before this portion of code,
6409 * the loopback reference in rt6_info will not be taken, do it
6410 * manually for init_net */
6411 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6412 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6413 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6414 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6415 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6416 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6417 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6418 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6422 #if IS_BUILTIN(CONFIG_IPV6)
6423 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6424 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6426 static const struct bpf_iter_reg ipv6_route_reg_info = {
6427 .target = "ipv6_route",
6428 .seq_ops = &ipv6_route_seq_ops,
6429 .init_seq_private = bpf_iter_init_seq_net,
6430 .fini_seq_private = bpf_iter_fini_seq_net,
6431 .seq_priv_size = sizeof(struct ipv6_route_iter),
6432 .ctx_arg_info_size = 1,
6434 { offsetof(struct bpf_iter__ipv6_route, rt),
6435 PTR_TO_BTF_ID_OR_NULL },
6439 static int __init bpf_iter_register(void)
6441 return bpf_iter_reg_target(&ipv6_route_reg_info);
6444 static void bpf_iter_unregister(void)
6446 bpf_iter_unreg_target(&ipv6_route_reg_info);
6451 int __init ip6_route_init(void)
6457 ip6_dst_ops_template.kmem_cachep =
6458 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6459 SLAB_HWCACHE_ALIGN, NULL);
6460 if (!ip6_dst_ops_template.kmem_cachep)
6463 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6465 goto out_kmem_cache;
6467 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6469 goto out_dst_entries;
6471 ret = register_pernet_subsys(&ip6_route_net_ops);
6473 goto out_register_inetpeer;
6475 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6479 goto out_register_subsys;
6485 ret = fib6_rules_init();
6489 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6491 goto fib6_rules_init;
6493 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6494 inet6_rtm_newroute, NULL, 0);
6496 goto out_register_late_subsys;
6498 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6499 inet6_rtm_delroute, NULL, 0);
6501 goto out_register_late_subsys;
6503 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6504 inet6_rtm_getroute, NULL,
6505 RTNL_FLAG_DOIT_UNLOCKED);
6507 goto out_register_late_subsys;
6509 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6511 goto out_register_late_subsys;
6513 #if IS_BUILTIN(CONFIG_IPV6)
6514 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6515 ret = bpf_iter_register();
6517 goto out_register_late_subsys;
6521 for_each_possible_cpu(cpu) {
6522 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6524 INIT_LIST_HEAD(&ul->head);
6525 spin_lock_init(&ul->lock);
6531 out_register_late_subsys:
6532 rtnl_unregister_all(PF_INET6);
6533 unregister_pernet_subsys(&ip6_route_net_late_ops);
6535 fib6_rules_cleanup();
6540 out_register_subsys:
6541 unregister_pernet_subsys(&ip6_route_net_ops);
6542 out_register_inetpeer:
6543 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6545 dst_entries_destroy(&ip6_dst_blackhole_ops);
6547 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6551 void ip6_route_cleanup(void)
6553 #if IS_BUILTIN(CONFIG_IPV6)
6554 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6555 bpf_iter_unregister();
6558 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6559 unregister_pernet_subsys(&ip6_route_net_late_ops);
6560 fib6_rules_cleanup();
6563 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6564 unregister_pernet_subsys(&ip6_route_net_ops);
6565 dst_entries_destroy(&ip6_dst_blackhole_ops);
6566 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);