2 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
24 * Fixed routing subtrees.
27 #define pr_fmt(fmt) "IPv6: " fmt
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/export.h>
32 #include <linux/types.h>
33 #include <linux/times.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <linux/route.h>
38 #include <linux/netdevice.h>
39 #include <linux/in6.h>
40 #include <linux/mroute6.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/nsproxy.h>
46 #include <linux/slab.h>
47 #include <linux/jhash.h>
48 #include <net/net_namespace.h>
51 #include <net/ip6_fib.h>
52 #include <net/ip6_route.h>
53 #include <net/ndisc.h>
54 #include <net/addrconf.h>
56 #include <linux/rtnetlink.h>
58 #include <net/dst_metadata.h>
60 #include <net/netevent.h>
61 #include <net/netlink.h>
62 #include <net/nexthop.h>
63 #include <net/lwtunnel.h>
64 #include <net/ip_tunnels.h>
65 #include <net/l3mdev.h>
66 #include <trace/events/fib6.h>
68 #include <linux/uaccess.h>
71 #include <linux/sysctl.h>
75 RT6_NUD_FAIL_HARD = -3,
76 RT6_NUD_FAIL_PROBE = -2,
77 RT6_NUD_FAIL_DO_RR = -1,
81 static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
82 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
83 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
84 static unsigned int ip6_mtu(const struct dst_entry *dst);
85 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
86 static void ip6_dst_destroy(struct dst_entry *);
87 static void ip6_dst_ifdown(struct dst_entry *,
88 struct net_device *dev, int how);
89 static int ip6_dst_gc(struct dst_ops *ops);
91 static int ip6_pkt_discard(struct sk_buff *skb);
92 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
93 static int ip6_pkt_prohibit(struct sk_buff *skb);
94 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
95 static void ip6_link_failure(struct sk_buff *skb);
96 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
97 struct sk_buff *skb, u32 mtu);
98 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 static void rt6_dst_from_metrics_check(struct rt6_info *rt);
101 static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
102 static size_t rt6_nlmsg_size(struct rt6_info *rt);
103 static int rt6_fill_node(struct net *net,
104 struct sk_buff *skb, struct rt6_info *rt,
105 struct in6_addr *dst, struct in6_addr *src,
106 int iif, int type, u32 portid, u32 seq,
108 static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
109 struct in6_addr *daddr,
110 struct in6_addr *saddr);
112 #ifdef CONFIG_IPV6_ROUTE_INFO
113 static struct rt6_info *rt6_add_route_info(struct net *net,
114 const struct in6_addr *prefix, int prefixlen,
115 const struct in6_addr *gwaddr,
116 struct net_device *dev,
118 static struct rt6_info *rt6_get_route_info(struct net *net,
119 const struct in6_addr *prefix, int prefixlen,
120 const struct in6_addr *gwaddr,
121 struct net_device *dev);
124 struct uncached_list {
126 struct list_head head;
129 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
131 void rt6_uncached_list_add(struct rt6_info *rt)
133 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
135 rt->rt6i_uncached_list = ul;
137 spin_lock_bh(&ul->lock);
138 list_add_tail(&rt->rt6i_uncached, &ul->head);
139 spin_unlock_bh(&ul->lock);
142 void rt6_uncached_list_del(struct rt6_info *rt)
144 if (!list_empty(&rt->rt6i_uncached)) {
145 struct uncached_list *ul = rt->rt6i_uncached_list;
146 struct net *net = dev_net(rt->dst.dev);
148 spin_lock_bh(&ul->lock);
149 list_del(&rt->rt6i_uncached);
150 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
151 spin_unlock_bh(&ul->lock);
155 static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
157 struct net_device *loopback_dev = net->loopback_dev;
160 if (dev == loopback_dev)
163 for_each_possible_cpu(cpu) {
164 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167 spin_lock_bh(&ul->lock);
168 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
169 struct inet6_dev *rt_idev = rt->rt6i_idev;
170 struct net_device *rt_dev = rt->dst.dev;
172 if (rt_idev->dev == dev) {
173 rt->rt6i_idev = in6_dev_get(loopback_dev);
174 in6_dev_put(rt_idev);
178 rt->dst.dev = loopback_dev;
179 dev_hold(rt->dst.dev);
183 spin_unlock_bh(&ul->lock);
187 static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
189 return dst_metrics_write_ptr(&rt->from->dst);
192 static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
194 struct rt6_info *rt = (struct rt6_info *)dst;
196 if (rt->rt6i_flags & RTF_PCPU)
197 return rt6_pcpu_cow_metrics(rt);
198 else if (rt->rt6i_flags & RTF_CACHE)
201 return dst_cow_metrics_generic(dst, old);
204 static inline const void *choose_neigh_daddr(struct rt6_info *rt,
208 struct in6_addr *p = &rt->rt6i_gateway;
210 if (!ipv6_addr_any(p))
211 return (const void *) p;
213 return &ipv6_hdr(skb)->daddr;
217 static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
221 struct rt6_info *rt = (struct rt6_info *) dst;
224 daddr = choose_neigh_daddr(rt, skb, daddr);
225 n = __ipv6_neigh_lookup(dst->dev, daddr);
228 return neigh_create(&nd_tbl, daddr, dst->dev);
231 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
233 struct net_device *dev = dst->dev;
234 struct rt6_info *rt = (struct rt6_info *)dst;
236 daddr = choose_neigh_daddr(rt, NULL, daddr);
239 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
241 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
243 __ipv6_confirm_neigh(dev, daddr);
246 static struct dst_ops ip6_dst_ops_template = {
250 .check = ip6_dst_check,
251 .default_advmss = ip6_default_advmss,
253 .cow_metrics = ipv6_cow_metrics,
254 .destroy = ip6_dst_destroy,
255 .ifdown = ip6_dst_ifdown,
256 .negative_advice = ip6_negative_advice,
257 .link_failure = ip6_link_failure,
258 .update_pmtu = ip6_rt_update_pmtu,
259 .redirect = rt6_do_redirect,
260 .local_out = __ip6_local_out,
261 .neigh_lookup = ip6_neigh_lookup,
262 .confirm_neigh = ip6_confirm_neigh,
265 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
267 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
269 return mtu ? : dst->dev->mtu;
272 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
273 struct sk_buff *skb, u32 mtu)
277 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
282 static struct dst_ops ip6_dst_blackhole_ops = {
284 .destroy = ip6_dst_destroy,
285 .check = ip6_dst_check,
286 .mtu = ip6_blackhole_mtu,
287 .default_advmss = ip6_default_advmss,
288 .update_pmtu = ip6_rt_blackhole_update_pmtu,
289 .redirect = ip6_rt_blackhole_redirect,
290 .cow_metrics = dst_cow_metrics_generic,
291 .neigh_lookup = ip6_neigh_lookup,
294 static const u32 ip6_template_metrics[RTAX_MAX] = {
295 [RTAX_HOPLIMIT - 1] = 0,
298 static const struct rt6_info ip6_null_entry_template = {
300 .__refcnt = ATOMIC_INIT(1),
302 .obsolete = DST_OBSOLETE_FORCE_CHK,
303 .error = -ENETUNREACH,
304 .input = ip6_pkt_discard,
305 .output = ip6_pkt_discard_out,
307 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
308 .rt6i_protocol = RTPROT_KERNEL,
309 .rt6i_metric = ~(u32) 0,
310 .rt6i_ref = ATOMIC_INIT(1),
313 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
315 static const struct rt6_info ip6_prohibit_entry_template = {
317 .__refcnt = ATOMIC_INIT(1),
319 .obsolete = DST_OBSOLETE_FORCE_CHK,
321 .input = ip6_pkt_prohibit,
322 .output = ip6_pkt_prohibit_out,
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
325 .rt6i_protocol = RTPROT_KERNEL,
326 .rt6i_metric = ~(u32) 0,
327 .rt6i_ref = ATOMIC_INIT(1),
330 static const struct rt6_info ip6_blk_hole_entry_template = {
332 .__refcnt = ATOMIC_INIT(1),
334 .obsolete = DST_OBSOLETE_FORCE_CHK,
336 .input = dst_discard,
337 .output = dst_discard_out,
339 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
340 .rt6i_protocol = RTPROT_KERNEL,
341 .rt6i_metric = ~(u32) 0,
342 .rt6i_ref = ATOMIC_INIT(1),
347 static void rt6_info_init(struct rt6_info *rt)
349 struct dst_entry *dst = &rt->dst;
351 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
352 INIT_LIST_HEAD(&rt->rt6i_siblings);
353 INIT_LIST_HEAD(&rt->rt6i_uncached);
356 /* allocate dst with ip6_dst_ops */
357 static struct rt6_info *__ip6_dst_alloc(struct net *net,
358 struct net_device *dev,
361 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
362 1, DST_OBSOLETE_FORCE_CHK, flags);
366 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
372 struct rt6_info *ip6_dst_alloc(struct net *net,
373 struct net_device *dev,
376 struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
379 rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
380 if (!rt->rt6i_pcpu) {
381 dst_release_immediate(&rt->dst);
388 EXPORT_SYMBOL(ip6_dst_alloc);
390 static void ip6_dst_destroy(struct dst_entry *dst)
392 struct rt6_info *rt = (struct rt6_info *)dst;
393 struct rt6_exception_bucket *bucket;
394 struct rt6_info *from = rt->from;
395 struct inet6_dev *idev;
397 dst_destroy_metrics_generic(dst);
398 free_percpu(rt->rt6i_pcpu);
399 rt6_uncached_list_del(rt);
401 idev = rt->rt6i_idev;
403 rt->rt6i_idev = NULL;
406 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
408 rt->rt6i_exception_bucket = NULL;
413 dst_release(&from->dst);
416 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
419 struct rt6_info *rt = (struct rt6_info *)dst;
420 struct inet6_dev *idev = rt->rt6i_idev;
421 struct net_device *loopback_dev =
422 dev_net(dev)->loopback_dev;
424 if (idev && idev->dev != loopback_dev) {
425 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
427 rt->rt6i_idev = loopback_idev;
433 static bool __rt6_check_expired(const struct rt6_info *rt)
435 if (rt->rt6i_flags & RTF_EXPIRES)
436 return time_after(jiffies, rt->dst.expires);
441 static bool rt6_check_expired(const struct rt6_info *rt)
443 if (rt->rt6i_flags & RTF_EXPIRES) {
444 if (time_after(jiffies, rt->dst.expires))
446 } else if (rt->from) {
447 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
448 rt6_check_expired(rt->from);
453 static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
454 struct flowi6 *fl6, int oif,
457 struct rt6_info *sibling, *next_sibling;
459 /* We might have already computed the hash for ICMPv6 errors. In such
460 * case it will always be non-zero. Otherwise now is the time to do it.
463 fl6->mp_hash = rt6_multipath_hash(fl6, NULL);
465 if (fl6->mp_hash <= atomic_read(&match->rt6i_nh_upper_bound))
468 list_for_each_entry_safe(sibling, next_sibling, &match->rt6i_siblings,
470 if (fl6->mp_hash > atomic_read(&sibling->rt6i_nh_upper_bound))
472 if (rt6_score_route(sibling, oif, strict) < 0)
482 * Route lookup. rcu_read_lock() should be held.
485 static inline struct rt6_info *rt6_device_match(struct net *net,
487 const struct in6_addr *saddr,
491 struct rt6_info *local = NULL;
492 struct rt6_info *sprt;
494 if (!oif && ipv6_addr_any(saddr) && !(rt->rt6i_nh_flags & RTNH_F_DEAD))
497 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
498 struct net_device *dev = sprt->dst.dev;
500 if (sprt->rt6i_nh_flags & RTNH_F_DEAD)
504 if (dev->ifindex == oif)
506 if (dev->flags & IFF_LOOPBACK) {
507 if (!sprt->rt6i_idev ||
508 sprt->rt6i_idev->dev->ifindex != oif) {
509 if (flags & RT6_LOOKUP_F_IFACE)
512 local->rt6i_idev->dev->ifindex == oif)
518 if (ipv6_chk_addr(net, saddr, dev,
519 flags & RT6_LOOKUP_F_IFACE))
528 if (flags & RT6_LOOKUP_F_IFACE)
529 return net->ipv6.ip6_null_entry;
532 return rt->rt6i_nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : rt;
535 #ifdef CONFIG_IPV6_ROUTER_PREF
536 struct __rt6_probe_work {
537 struct work_struct work;
538 struct in6_addr target;
539 struct net_device *dev;
542 static void rt6_probe_deferred(struct work_struct *w)
544 struct in6_addr mcaddr;
545 struct __rt6_probe_work *work =
546 container_of(w, struct __rt6_probe_work, work);
548 addrconf_addr_solict_mult(&work->target, &mcaddr);
549 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
554 static void rt6_probe(struct rt6_info *rt)
556 struct __rt6_probe_work *work;
557 struct neighbour *neigh;
559 * Okay, this does not seem to be appropriate
560 * for now, however, we need to check if it
561 * is really so; aka Router Reachability Probing.
563 * Router Reachability Probe MUST be rate-limited
564 * to no more than one per minute.
566 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
569 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
571 if (neigh->nud_state & NUD_VALID)
575 write_lock(&neigh->lock);
576 if (!(neigh->nud_state & NUD_VALID) &&
579 rt->rt6i_idev->cnf.rtr_probe_interval)) {
580 work = kmalloc(sizeof(*work), GFP_ATOMIC);
582 __neigh_set_probe_once(neigh);
584 write_unlock(&neigh->lock);
586 work = kmalloc(sizeof(*work), GFP_ATOMIC);
590 INIT_WORK(&work->work, rt6_probe_deferred);
591 work->target = rt->rt6i_gateway;
592 dev_hold(rt->dst.dev);
593 work->dev = rt->dst.dev;
594 schedule_work(&work->work);
598 rcu_read_unlock_bh();
601 static inline void rt6_probe(struct rt6_info *rt)
607 * Default Router Selection (RFC 2461 6.3.6)
609 static inline int rt6_check_dev(struct rt6_info *rt, int oif)
611 struct net_device *dev = rt->dst.dev;
612 if (!oif || dev->ifindex == oif)
614 if ((dev->flags & IFF_LOOPBACK) &&
615 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
620 static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
622 struct neighbour *neigh;
623 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
625 if (rt->rt6i_flags & RTF_NONEXTHOP ||
626 !(rt->rt6i_flags & RTF_GATEWAY))
627 return RT6_NUD_SUCCEED;
630 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
632 read_lock(&neigh->lock);
633 if (neigh->nud_state & NUD_VALID)
634 ret = RT6_NUD_SUCCEED;
635 #ifdef CONFIG_IPV6_ROUTER_PREF
636 else if (!(neigh->nud_state & NUD_FAILED))
637 ret = RT6_NUD_SUCCEED;
639 ret = RT6_NUD_FAIL_PROBE;
641 read_unlock(&neigh->lock);
643 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
644 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
646 rcu_read_unlock_bh();
651 static int rt6_score_route(struct rt6_info *rt, int oif,
656 m = rt6_check_dev(rt, oif);
657 if (!m && (strict & RT6_LOOKUP_F_IFACE))
658 return RT6_NUD_FAIL_HARD;
659 #ifdef CONFIG_IPV6_ROUTER_PREF
660 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
662 if (strict & RT6_LOOKUP_F_REACHABLE) {
663 int n = rt6_check_neigh(rt);
670 static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
671 int *mpri, struct rt6_info *match,
675 bool match_do_rr = false;
676 struct inet6_dev *idev = rt->rt6i_idev;
678 if (rt->rt6i_nh_flags & RTNH_F_DEAD)
681 if (idev->cnf.ignore_routes_with_linkdown &&
682 rt->rt6i_nh_flags & RTNH_F_LINKDOWN &&
683 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
686 if (rt6_check_expired(rt))
689 m = rt6_score_route(rt, oif, strict);
690 if (m == RT6_NUD_FAIL_DO_RR) {
692 m = 0; /* lowest valid score */
693 } else if (m == RT6_NUD_FAIL_HARD) {
697 if (strict & RT6_LOOKUP_F_REACHABLE)
700 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
702 *do_rr = match_do_rr;
710 static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
711 struct rt6_info *leaf,
712 struct rt6_info *rr_head,
713 u32 metric, int oif, int strict,
716 struct rt6_info *rt, *match, *cont;
721 for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
722 if (rt->rt6i_metric != metric) {
727 match = find_match(rt, oif, strict, &mpri, match, do_rr);
730 for (rt = leaf; rt && rt != rr_head;
731 rt = rcu_dereference(rt->rt6_next)) {
732 if (rt->rt6i_metric != metric) {
737 match = find_match(rt, oif, strict, &mpri, match, do_rr);
743 for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
744 match = find_match(rt, oif, strict, &mpri, match, do_rr);
749 static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn,
752 struct rt6_info *leaf = rcu_dereference(fn->leaf);
753 struct rt6_info *match, *rt0;
757 if (!leaf || leaf == net->ipv6.ip6_null_entry)
758 return net->ipv6.ip6_null_entry;
760 rt0 = rcu_dereference(fn->rr_ptr);
764 /* Double check to make sure fn is not an intermediate node
765 * and fn->leaf does not points to its child's leaf
766 * (This might happen if all routes under fn are deleted from
767 * the tree and fib6_repair_tree() is called on the node.)
769 key_plen = rt0->rt6i_dst.plen;
770 #ifdef CONFIG_IPV6_SUBTREES
771 if (rt0->rt6i_src.plen)
772 key_plen = rt0->rt6i_src.plen;
774 if (fn->fn_bit != key_plen)
775 return net->ipv6.ip6_null_entry;
777 match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict,
781 struct rt6_info *next = rcu_dereference(rt0->rt6_next);
783 /* no entries matched; do round-robin */
784 if (!next || next->rt6i_metric != rt0->rt6i_metric)
788 spin_lock_bh(&leaf->rt6i_table->tb6_lock);
789 /* make sure next is not being deleted from the tree */
791 rcu_assign_pointer(fn->rr_ptr, next);
792 spin_unlock_bh(&leaf->rt6i_table->tb6_lock);
796 return match ? match : net->ipv6.ip6_null_entry;
799 static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
801 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
804 #ifdef CONFIG_IPV6_ROUTE_INFO
805 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
806 const struct in6_addr *gwaddr)
808 struct net *net = dev_net(dev);
809 struct route_info *rinfo = (struct route_info *) opt;
810 struct in6_addr prefix_buf, *prefix;
812 unsigned long lifetime;
815 if (len < sizeof(struct route_info)) {
819 /* Sanity check for prefix_len and length */
820 if (rinfo->length > 3) {
822 } else if (rinfo->prefix_len > 128) {
824 } else if (rinfo->prefix_len > 64) {
825 if (rinfo->length < 2) {
828 } else if (rinfo->prefix_len > 0) {
829 if (rinfo->length < 1) {
834 pref = rinfo->route_pref;
835 if (pref == ICMPV6_ROUTER_PREF_INVALID)
838 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
840 if (rinfo->length == 3)
841 prefix = (struct in6_addr *)rinfo->prefix;
843 /* this function is safe */
844 ipv6_addr_prefix(&prefix_buf,
845 (struct in6_addr *)rinfo->prefix,
847 prefix = &prefix_buf;
850 if (rinfo->prefix_len == 0)
851 rt = rt6_get_dflt_router(gwaddr, dev);
853 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
856 if (rt && !lifetime) {
862 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
865 rt->rt6i_flags = RTF_ROUTEINFO |
866 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
869 if (!addrconf_finite_timeout(lifetime))
870 rt6_clean_expires(rt);
872 rt6_set_expires(rt, jiffies + HZ * lifetime);
880 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
881 struct in6_addr *saddr)
883 struct fib6_node *pn, *sn;
885 if (fn->fn_flags & RTN_TL_ROOT)
887 pn = rcu_dereference(fn->parent);
888 sn = FIB6_SUBTREE(pn);
890 fn = fib6_lookup(sn, NULL, saddr);
893 if (fn->fn_flags & RTN_RTINFO)
898 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
901 struct rt6_info *rt = *prt;
903 if (dst_hold_safe(&rt->dst))
906 rt = net->ipv6.ip6_null_entry;
915 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
916 struct fib6_table *table,
917 struct flowi6 *fl6, int flags)
919 struct rt6_info *rt, *rt_cache;
920 struct fib6_node *fn;
923 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
925 rt = rcu_dereference(fn->leaf);
927 rt = net->ipv6.ip6_null_entry;
929 rt = rt6_device_match(net, rt, &fl6->saddr,
930 fl6->flowi6_oif, flags);
931 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
932 rt = rt6_multipath_select(rt, fl6,
933 fl6->flowi6_oif, flags);
935 if (rt == net->ipv6.ip6_null_entry) {
936 fn = fib6_backtrack(fn, &fl6->saddr);
940 /* Search through exception table */
941 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
945 if (ip6_hold_safe(net, &rt, true))
946 dst_use_noref(&rt->dst, jiffies);
950 trace_fib6_table_lookup(net, rt, table, fl6);
956 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
959 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
961 EXPORT_SYMBOL_GPL(ip6_route_lookup);
963 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
964 const struct in6_addr *saddr, int oif, int strict)
966 struct flowi6 fl6 = {
970 struct dst_entry *dst;
971 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
974 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
975 flags |= RT6_LOOKUP_F_HAS_SADDR;
978 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
980 return (struct rt6_info *) dst;
986 EXPORT_SYMBOL(rt6_lookup);
988 /* ip6_ins_rt is called with FREE table->tb6_lock.
989 * It takes new route entry, the addition fails by any reason the
991 * Caller must hold dst before calling it.
994 static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
995 struct mx6_config *mxc,
996 struct netlink_ext_ack *extack)
999 struct fib6_table *table;
1001 table = rt->rt6i_table;
1002 spin_lock_bh(&table->tb6_lock);
1003 err = fib6_add(&table->tb6_root, rt, info, mxc, extack);
1004 spin_unlock_bh(&table->tb6_lock);
1009 int ip6_ins_rt(struct rt6_info *rt)
1011 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
1012 struct mx6_config mxc = { .mx = NULL, };
1014 /* Hold dst to account for the reference from the fib6 tree */
1016 return __ip6_ins_rt(rt, &info, &mxc, NULL);
1019 /* called with rcu_lock held */
1020 static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
1022 struct net_device *dev = rt->dst.dev;
1024 if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1025 /* for copies of local routes, dst->dev needs to be the
1026 * device if it is a master device, the master device if
1027 * device is enslaved, and the loopback as the default
1029 if (netif_is_l3_slave(dev) &&
1030 !rt6_need_strict(&rt->rt6i_dst.addr))
1031 dev = l3mdev_master_dev_rcu(dev);
1032 else if (!netif_is_l3_master(dev))
1033 dev = dev_net(dev)->loopback_dev;
1034 /* last case is netif_is_l3_master(dev) is true in which
1035 * case we want dev returned to be dev
1042 static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
1043 const struct in6_addr *daddr,
1044 const struct in6_addr *saddr)
1046 struct net_device *dev;
1047 struct rt6_info *rt;
1053 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
1057 dev = ip6_rt_get_dev_rcu(ort);
1058 rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
1063 ip6_rt_copy_init(rt, ort);
1064 rt->rt6i_flags |= RTF_CACHE;
1065 rt->rt6i_metric = 0;
1066 rt->dst.flags |= DST_HOST;
1067 rt->rt6i_dst.addr = *daddr;
1068 rt->rt6i_dst.plen = 128;
1070 if (!rt6_is_gw_or_nonexthop(ort)) {
1071 if (ort->rt6i_dst.plen != 128 &&
1072 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
1073 rt->rt6i_flags |= RTF_ANYCAST;
1074 #ifdef CONFIG_IPV6_SUBTREES
1075 if (rt->rt6i_src.plen && saddr) {
1076 rt->rt6i_src.addr = *saddr;
1077 rt->rt6i_src.plen = 128;
1085 static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
1087 struct net_device *dev;
1088 struct rt6_info *pcpu_rt;
1091 dev = ip6_rt_get_dev_rcu(rt);
1092 pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
1096 ip6_rt_copy_init(pcpu_rt, rt);
1097 pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
1098 pcpu_rt->rt6i_flags |= RTF_PCPU;
1102 /* It should be called with rcu_read_lock() acquired */
1103 static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
1105 struct rt6_info *pcpu_rt, **p;
1107 p = this_cpu_ptr(rt->rt6i_pcpu);
1110 if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false))
1111 rt6_dst_from_metrics_check(pcpu_rt);
1116 static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
1118 struct rt6_info *pcpu_rt, *prev, **p;
1120 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1122 struct net *net = dev_net(rt->dst.dev);
1124 dst_hold(&net->ipv6.ip6_null_entry->dst);
1125 return net->ipv6.ip6_null_entry;
1128 dst_hold(&pcpu_rt->dst);
1129 p = this_cpu_ptr(rt->rt6i_pcpu);
1130 prev = cmpxchg(p, NULL, pcpu_rt);
1133 rt6_dst_from_metrics_check(pcpu_rt);
1137 /* exception hash table implementation
1139 static DEFINE_SPINLOCK(rt6_exception_lock);
1141 /* Remove rt6_ex from hash table and free the memory
1142 * Caller must hold rt6_exception_lock
1144 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1145 struct rt6_exception *rt6_ex)
1149 if (!bucket || !rt6_ex)
1152 net = dev_net(rt6_ex->rt6i->dst.dev);
1153 rt6_ex->rt6i->rt6i_node = NULL;
1154 hlist_del_rcu(&rt6_ex->hlist);
1155 rt6_release(rt6_ex->rt6i);
1156 kfree_rcu(rt6_ex, rcu);
1157 WARN_ON_ONCE(!bucket->depth);
1159 net->ipv6.rt6_stats->fib_rt_cache--;
1162 /* Remove oldest rt6_ex in bucket and free the memory
1163 * Caller must hold rt6_exception_lock
1165 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1167 struct rt6_exception *rt6_ex, *oldest = NULL;
1172 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1173 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1176 rt6_remove_exception(bucket, oldest);
1179 static u32 rt6_exception_hash(const struct in6_addr *dst,
1180 const struct in6_addr *src)
1182 static u32 seed __read_mostly;
1185 net_get_random_once(&seed, sizeof(seed));
1186 val = jhash(dst, sizeof(*dst), seed);
1188 #ifdef CONFIG_IPV6_SUBTREES
1190 val = jhash(src, sizeof(*src), val);
1192 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1195 /* Helper function to find the cached rt in the hash table
1196 * and update bucket pointer to point to the bucket for this
1197 * (daddr, saddr) pair
1198 * Caller must hold rt6_exception_lock
1200 static struct rt6_exception *
1201 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1202 const struct in6_addr *daddr,
1203 const struct in6_addr *saddr)
1205 struct rt6_exception *rt6_ex;
1208 if (!(*bucket) || !daddr)
1211 hval = rt6_exception_hash(daddr, saddr);
1214 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1215 struct rt6_info *rt6 = rt6_ex->rt6i;
1216 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1218 #ifdef CONFIG_IPV6_SUBTREES
1219 if (matched && saddr)
1220 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1228 /* Helper function to find the cached rt in the hash table
1229 * and update bucket pointer to point to the bucket for this
1230 * (daddr, saddr) pair
1231 * Caller must hold rcu_read_lock()
1233 static struct rt6_exception *
1234 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1235 const struct in6_addr *daddr,
1236 const struct in6_addr *saddr)
1238 struct rt6_exception *rt6_ex;
1241 WARN_ON_ONCE(!rcu_read_lock_held());
1243 if (!(*bucket) || !daddr)
1246 hval = rt6_exception_hash(daddr, saddr);
1249 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1250 struct rt6_info *rt6 = rt6_ex->rt6i;
1251 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1253 #ifdef CONFIG_IPV6_SUBTREES
1254 if (matched && saddr)
1255 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1263 static int rt6_insert_exception(struct rt6_info *nrt,
1264 struct rt6_info *ort)
1266 struct net *net = dev_net(ort->dst.dev);
1267 struct rt6_exception_bucket *bucket;
1268 struct in6_addr *src_key = NULL;
1269 struct rt6_exception *rt6_ex;
1272 /* ort can't be a cache or pcpu route */
1273 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
1275 WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
1277 spin_lock_bh(&rt6_exception_lock);
1279 if (ort->exception_bucket_flushed) {
1284 bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
1285 lockdep_is_held(&rt6_exception_lock));
1287 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1293 rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
1296 #ifdef CONFIG_IPV6_SUBTREES
1297 /* rt6i_src.plen != 0 indicates ort is in subtree
1298 * and exception table is indexed by a hash of
1299 * both rt6i_dst and rt6i_src.
1300 * Otherwise, the exception table is indexed by
1301 * a hash of only rt6i_dst.
1303 if (ort->rt6i_src.plen)
1304 src_key = &nrt->rt6i_src.addr;
1307 /* Update rt6i_prefsrc as it could be changed
1308 * in rt6_remove_prefsrc()
1310 nrt->rt6i_prefsrc = ort->rt6i_prefsrc;
1311 /* rt6_mtu_change() might lower mtu on ort.
1312 * Only insert this exception route if its mtu
1313 * is less than ort's mtu value.
1315 if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) {
1320 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1323 rt6_remove_exception(bucket, rt6_ex);
1325 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1331 rt6_ex->stamp = jiffies;
1332 atomic_inc(&nrt->rt6i_ref);
1333 nrt->rt6i_node = ort->rt6i_node;
1334 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1336 net->ipv6.rt6_stats->fib_rt_cache++;
1338 if (bucket->depth > FIB6_MAX_DEPTH)
1339 rt6_exception_remove_oldest(bucket);
1342 spin_unlock_bh(&rt6_exception_lock);
1344 /* Update fn->fn_sernum to invalidate all cached dst */
1346 spin_lock_bh(&ort->rt6i_table->tb6_lock);
1347 fib6_update_sernum(ort);
1348 spin_unlock_bh(&ort->rt6i_table->tb6_lock);
1349 fib6_force_start_gc(net);
1355 void rt6_flush_exceptions(struct rt6_info *rt)
1357 struct rt6_exception_bucket *bucket;
1358 struct rt6_exception *rt6_ex;
1359 struct hlist_node *tmp;
1362 spin_lock_bh(&rt6_exception_lock);
1363 /* Prevent rt6_insert_exception() to recreate the bucket list */
1364 rt->exception_bucket_flushed = 1;
1366 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1367 lockdep_is_held(&rt6_exception_lock));
1371 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1372 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1373 rt6_remove_exception(bucket, rt6_ex);
1374 WARN_ON_ONCE(bucket->depth);
1379 spin_unlock_bh(&rt6_exception_lock);
1382 /* Find cached rt in the hash table inside passed in rt
1383 * Caller has to hold rcu_read_lock()
1385 static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
1386 struct in6_addr *daddr,
1387 struct in6_addr *saddr)
1389 struct rt6_exception_bucket *bucket;
1390 struct in6_addr *src_key = NULL;
1391 struct rt6_exception *rt6_ex;
1392 struct rt6_info *res = NULL;
1394 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1396 #ifdef CONFIG_IPV6_SUBTREES
1397 /* rt6i_src.plen != 0 indicates rt is in subtree
1398 * and exception table is indexed by a hash of
1399 * both rt6i_dst and rt6i_src.
1400 * Otherwise, the exception table is indexed by
1401 * a hash of only rt6i_dst.
1403 if (rt->rt6i_src.plen)
1406 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1408 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1414 /* Remove the passed in cached rt from the hash table that contains it */
1415 int rt6_remove_exception_rt(struct rt6_info *rt)
1417 struct rt6_exception_bucket *bucket;
1418 struct rt6_info *from = rt->from;
1419 struct in6_addr *src_key = NULL;
1420 struct rt6_exception *rt6_ex;
1424 !(rt->rt6i_flags & RTF_CACHE))
1427 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1430 spin_lock_bh(&rt6_exception_lock);
1431 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1432 lockdep_is_held(&rt6_exception_lock));
1433 #ifdef CONFIG_IPV6_SUBTREES
1434 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1435 * and exception table is indexed by a hash of
1436 * both rt6i_dst and rt6i_src.
1437 * Otherwise, the exception table is indexed by
1438 * a hash of only rt6i_dst.
1440 if (from->rt6i_src.plen)
1441 src_key = &rt->rt6i_src.addr;
1443 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1447 rt6_remove_exception(bucket, rt6_ex);
1453 spin_unlock_bh(&rt6_exception_lock);
1457 /* Find rt6_ex which contains the passed in rt cache and
1460 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1462 struct rt6_exception_bucket *bucket;
1463 struct rt6_info *from = rt->from;
1464 struct in6_addr *src_key = NULL;
1465 struct rt6_exception *rt6_ex;
1468 !(rt->rt6i_flags & RTF_CACHE))
1472 bucket = rcu_dereference(from->rt6i_exception_bucket);
1474 #ifdef CONFIG_IPV6_SUBTREES
1475 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1476 * and exception table is indexed by a hash of
1477 * both rt6i_dst and rt6i_src.
1478 * Otherwise, the exception table is indexed by
1479 * a hash of only rt6i_dst.
1481 if (from->rt6i_src.plen)
1482 src_key = &rt->rt6i_src.addr;
1484 rt6_ex = __rt6_find_exception_rcu(&bucket,
1488 rt6_ex->stamp = jiffies;
1493 static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
1495 struct rt6_exception_bucket *bucket;
1496 struct rt6_exception *rt6_ex;
1499 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1500 lockdep_is_held(&rt6_exception_lock));
1503 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1504 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1505 rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
1512 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1513 struct rt6_info *rt, int mtu)
1515 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1516 * lowest MTU in the path: always allow updating the route PMTU to
1517 * reflect PMTU decreases.
1519 * If the new MTU is higher, and the route PMTU is equal to the local
1520 * MTU, this means the old MTU is the lowest in the path, so allow
1521 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1525 if (dst_mtu(&rt->dst) >= mtu)
1528 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1534 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1535 struct rt6_info *rt, int mtu)
1537 struct rt6_exception_bucket *bucket;
1538 struct rt6_exception *rt6_ex;
1541 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1542 lockdep_is_held(&rt6_exception_lock));
1547 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1548 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1549 struct rt6_info *entry = rt6_ex->rt6i;
1551 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1552 * route), the metrics of its rt->dst.from have already
1555 if (entry->rt6i_pmtu &&
1556 rt6_mtu_change_route_allowed(idev, entry, mtu))
1557 entry->rt6i_pmtu = mtu;
1563 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1565 static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1566 struct in6_addr *gateway)
1568 struct rt6_exception_bucket *bucket;
1569 struct rt6_exception *rt6_ex;
1570 struct hlist_node *tmp;
1573 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1576 spin_lock_bh(&rt6_exception_lock);
1577 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1578 lockdep_is_held(&rt6_exception_lock));
1581 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1582 hlist_for_each_entry_safe(rt6_ex, tmp,
1583 &bucket->chain, hlist) {
1584 struct rt6_info *entry = rt6_ex->rt6i;
1586 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1587 RTF_CACHE_GATEWAY &&
1588 ipv6_addr_equal(gateway,
1589 &entry->rt6i_gateway)) {
1590 rt6_remove_exception(bucket, rt6_ex);
1597 spin_unlock_bh(&rt6_exception_lock);
1600 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1601 struct rt6_exception *rt6_ex,
1602 struct fib6_gc_args *gc_args,
1605 struct rt6_info *rt = rt6_ex->rt6i;
1607 /* we are pruning and obsoleting aged-out and non gateway exceptions
1608 * even if others have still references to them, so that on next
1609 * dst_check() such references can be dropped.
1610 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1611 * expired, independently from their aging, as per RFC 8201 section 4
1613 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1614 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1615 RT6_TRACE("aging clone %p\n", rt);
1616 rt6_remove_exception(bucket, rt6_ex);
1619 } else if (time_after(jiffies, rt->dst.expires)) {
1620 RT6_TRACE("purging expired route %p\n", rt);
1621 rt6_remove_exception(bucket, rt6_ex);
1625 if (rt->rt6i_flags & RTF_GATEWAY) {
1626 struct neighbour *neigh;
1627 __u8 neigh_flags = 0;
1629 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1631 neigh_flags = neigh->flags;
1632 neigh_release(neigh);
1634 if (!(neigh_flags & NTF_ROUTER)) {
1635 RT6_TRACE("purging route %p via non-router but gateway\n",
1637 rt6_remove_exception(bucket, rt6_ex);
1645 void rt6_age_exceptions(struct rt6_info *rt,
1646 struct fib6_gc_args *gc_args,
1649 struct rt6_exception_bucket *bucket;
1650 struct rt6_exception *rt6_ex;
1651 struct hlist_node *tmp;
1654 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1657 spin_lock_bh(&rt6_exception_lock);
1658 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1659 lockdep_is_held(&rt6_exception_lock));
1662 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1663 hlist_for_each_entry_safe(rt6_ex, tmp,
1664 &bucket->chain, hlist) {
1665 rt6_age_examine_exception(bucket, rt6_ex,
1671 spin_unlock_bh(&rt6_exception_lock);
1674 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1675 int oif, struct flowi6 *fl6, int flags)
1677 struct fib6_node *fn, *saved_fn;
1678 struct rt6_info *rt, *rt_cache;
1681 strict |= flags & RT6_LOOKUP_F_IFACE;
1682 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1683 if (net->ipv6.devconf_all->forwarding == 0)
1684 strict |= RT6_LOOKUP_F_REACHABLE;
1688 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1691 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1695 rt = rt6_select(net, fn, oif, strict);
1696 if (rt->rt6i_nsiblings)
1697 rt = rt6_multipath_select(rt, fl6, oif, strict);
1698 if (rt == net->ipv6.ip6_null_entry) {
1699 fn = fib6_backtrack(fn, &fl6->saddr);
1701 goto redo_rt6_select;
1702 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1703 /* also consider unreachable route */
1704 strict &= ~RT6_LOOKUP_F_REACHABLE;
1706 goto redo_rt6_select;
1710 /*Search through exception table */
1711 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
1715 if (rt == net->ipv6.ip6_null_entry) {
1718 trace_fib6_table_lookup(net, rt, table, fl6);
1720 } else if (rt->rt6i_flags & RTF_CACHE) {
1721 if (ip6_hold_safe(net, &rt, true)) {
1722 dst_use_noref(&rt->dst, jiffies);
1723 rt6_dst_from_metrics_check(rt);
1726 trace_fib6_table_lookup(net, rt, table, fl6);
1728 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1729 !(rt->rt6i_flags & RTF_GATEWAY))) {
1730 /* Create a RTF_CACHE clone which will not be
1731 * owned by the fib6 tree. It is for the special case where
1732 * the daddr in the skb during the neighbor look-up is different
1733 * from the fl6->daddr used to look-up route here.
1736 struct rt6_info *uncached_rt;
1738 if (ip6_hold_safe(net, &rt, true)) {
1739 dst_use_noref(&rt->dst, jiffies);
1743 goto uncached_rt_out;
1747 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
1748 dst_release(&rt->dst);
1751 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1752 * No need for another dst_hold()
1754 rt6_uncached_list_add(uncached_rt);
1755 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
1757 uncached_rt = net->ipv6.ip6_null_entry;
1758 dst_hold(&uncached_rt->dst);
1762 trace_fib6_table_lookup(net, uncached_rt, table, fl6);
1766 /* Get a percpu copy */
1768 struct rt6_info *pcpu_rt;
1770 dst_use_noref(&rt->dst, jiffies);
1772 pcpu_rt = rt6_get_pcpu_route(rt);
1775 /* atomic_inc_not_zero() is needed when using rcu */
1776 if (atomic_inc_not_zero(&rt->rt6i_ref)) {
1777 /* No dst_hold() on rt is needed because grabbing
1778 * rt->rt6i_ref makes sure rt can't be released.
1780 pcpu_rt = rt6_make_pcpu_route(rt);
1783 /* rt is already removed from tree */
1784 pcpu_rt = net->ipv6.ip6_null_entry;
1785 dst_hold(&pcpu_rt->dst);
1790 trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
1794 EXPORT_SYMBOL_GPL(ip6_pol_route);
1796 static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
1797 struct flowi6 *fl6, int flags)
1799 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
1802 struct dst_entry *ip6_route_input_lookup(struct net *net,
1803 struct net_device *dev,
1804 struct flowi6 *fl6, int flags)
1806 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1807 flags |= RT6_LOOKUP_F_IFACE;
1809 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1811 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
1813 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
1814 struct flow_keys *keys)
1816 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1817 const struct ipv6hdr *key_iph = outer_iph;
1818 const struct ipv6hdr *inner_iph;
1819 const struct icmp6hdr *icmph;
1820 struct ipv6hdr _inner_iph;
1822 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1825 icmph = icmp6_hdr(skb);
1826 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1827 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1828 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1829 icmph->icmp6_type != ICMPV6_PARAMPROB)
1832 inner_iph = skb_header_pointer(skb,
1833 skb_transport_offset(skb) + sizeof(*icmph),
1834 sizeof(_inner_iph), &_inner_iph);
1838 key_iph = inner_iph;
1840 memset(keys, 0, sizeof(*keys));
1841 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1842 keys->addrs.v6addrs.src = key_iph->saddr;
1843 keys->addrs.v6addrs.dst = key_iph->daddr;
1844 keys->tags.flow_label = ip6_flowinfo(key_iph);
1845 keys->basic.ip_proto = key_iph->nexthdr;
1848 /* if skb is set it will be used and fl6 can be NULL */
1849 u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
1851 struct flow_keys hash_keys;
1854 ip6_multipath_l3_keys(skb, &hash_keys);
1855 return flow_hash_from_keys(&hash_keys) >> 1;
1858 return get_hash_from_flowi6(fl6) >> 1;
1861 void ip6_route_input(struct sk_buff *skb)
1863 const struct ipv6hdr *iph = ipv6_hdr(skb);
1864 struct net *net = dev_net(skb->dev);
1865 int flags = RT6_LOOKUP_F_HAS_SADDR;
1866 struct ip_tunnel_info *tun_info;
1867 struct flowi6 fl6 = {
1868 .flowi6_iif = skb->dev->ifindex,
1869 .daddr = iph->daddr,
1870 .saddr = iph->saddr,
1871 .flowlabel = ip6_flowinfo(iph),
1872 .flowi6_mark = skb->mark,
1873 .flowi6_proto = iph->nexthdr,
1876 tun_info = skb_tunnel_info(skb);
1877 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
1878 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
1879 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
1880 fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
1882 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1885 static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
1886 struct flowi6 *fl6, int flags)
1888 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1891 struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
1892 struct flowi6 *fl6, int flags)
1896 if (rt6_need_strict(&fl6->daddr)) {
1897 struct dst_entry *dst;
1899 dst = l3mdev_link_scope_lookup(net, fl6);
1904 fl6->flowi6_iif = LOOPBACK_IFINDEX;
1906 any_src = ipv6_addr_any(&fl6->saddr);
1907 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
1908 (fl6->flowi6_oif && any_src))
1909 flags |= RT6_LOOKUP_F_IFACE;
1912 flags |= RT6_LOOKUP_F_HAS_SADDR;
1914 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1916 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
1918 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
1920 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
1922 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
1923 struct net_device *loopback_dev = net->loopback_dev;
1924 struct dst_entry *new = NULL;
1926 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
1927 DST_OBSOLETE_DEAD, 0);
1930 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
1934 new->input = dst_discard;
1935 new->output = dst_discard_out;
1937 dst_copy_metrics(new, &ort->dst);
1939 rt->rt6i_idev = in6_dev_get(loopback_dev);
1940 rt->rt6i_gateway = ort->rt6i_gateway;
1941 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
1942 rt->rt6i_metric = 0;
1944 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1945 #ifdef CONFIG_IPV6_SUBTREES
1946 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1950 dst_release(dst_orig);
1951 return new ? new : ERR_PTR(-ENOMEM);
1955 * Destination cache support functions
1958 static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1961 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst))
1962 dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true);
1965 static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
1969 if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
1972 if (rt6_check_expired(rt))
1978 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
1980 if (!__rt6_check_expired(rt) &&
1981 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
1982 rt6_check(rt->from, cookie))
1988 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1990 struct rt6_info *rt;
1992 rt = (struct rt6_info *) dst;
1994 /* All IPV6 dsts are created with ->obsolete set to the value
1995 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1996 * into this function always.
1999 rt6_dst_from_metrics_check(rt);
2001 if (rt->rt6i_flags & RTF_PCPU ||
2002 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
2003 return rt6_dst_from_check(rt, cookie);
2005 return rt6_check(rt, cookie);
2008 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2010 struct rt6_info *rt = (struct rt6_info *) dst;
2013 if (rt->rt6i_flags & RTF_CACHE) {
2014 if (rt6_check_expired(rt)) {
2026 static void ip6_link_failure(struct sk_buff *skb)
2028 struct rt6_info *rt;
2030 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2032 rt = (struct rt6_info *) skb_dst(skb);
2034 if (rt->rt6i_flags & RTF_CACHE) {
2035 if (dst_hold_safe(&rt->dst))
2038 struct fib6_node *fn;
2041 fn = rcu_dereference(rt->rt6i_node);
2042 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2049 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2051 struct net *net = dev_net(rt->dst.dev);
2053 rt->rt6i_flags |= RTF_MODIFIED;
2054 rt->rt6i_pmtu = mtu;
2055 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2058 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2060 return !(rt->rt6i_flags & RTF_CACHE) &&
2061 (rt->rt6i_flags & RTF_PCPU ||
2062 rcu_access_pointer(rt->rt6i_node));
2065 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2066 const struct ipv6hdr *iph, u32 mtu)
2068 const struct in6_addr *daddr, *saddr;
2069 struct rt6_info *rt6 = (struct rt6_info *)dst;
2071 if (rt6->rt6i_flags & RTF_LOCAL)
2074 if (dst_metric_locked(dst, RTAX_MTU))
2078 daddr = &iph->daddr;
2079 saddr = &iph->saddr;
2081 daddr = &sk->sk_v6_daddr;
2082 saddr = &inet6_sk(sk)->saddr;
2087 dst_confirm_neigh(dst, daddr);
2088 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2089 if (mtu >= dst_mtu(dst))
2092 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2093 rt6_do_update_pmtu(rt6, mtu);
2094 /* update rt6_ex->stamp for cache */
2095 if (rt6->rt6i_flags & RTF_CACHE)
2096 rt6_update_exception_stamp_rt(rt6);
2098 struct rt6_info *nrt6;
2100 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
2102 rt6_do_update_pmtu(nrt6, mtu);
2103 if (rt6_insert_exception(nrt6, rt6))
2104 dst_release_immediate(&nrt6->dst);
2109 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2110 struct sk_buff *skb, u32 mtu)
2112 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2115 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2116 int oif, u32 mark, kuid_t uid)
2118 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2119 struct dst_entry *dst;
2122 memset(&fl6, 0, sizeof(fl6));
2123 fl6.flowi6_oif = oif;
2124 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
2125 fl6.daddr = iph->daddr;
2126 fl6.saddr = iph->saddr;
2127 fl6.flowlabel = ip6_flowinfo(iph);
2128 fl6.flowi6_uid = uid;
2130 dst = ip6_route_output(net, NULL, &fl6);
2132 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
2135 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2137 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2139 struct dst_entry *dst;
2141 ip6_update_pmtu(skb, sock_net(sk), mtu,
2142 sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
2144 dst = __sk_dst_get(sk);
2145 if (!dst || !dst->obsolete ||
2146 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2150 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2151 ip6_datagram_dst_update(sk, false);
2154 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2156 /* Handle redirects */
2157 struct ip6rd_flowi {
2159 struct in6_addr gateway;
2162 static struct rt6_info *__ip6_route_redirect(struct net *net,
2163 struct fib6_table *table,
2167 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2168 struct rt6_info *rt, *rt_cache;
2169 struct fib6_node *fn;
2171 /* Get the "current" route for this destination and
2172 * check if the redirect has come from appropriate router.
2174 * RFC 4861 specifies that redirects should only be
2175 * accepted if they come from the nexthop to the target.
2176 * Due to the way the routes are chosen, this notion
2177 * is a bit fuzzy and one might need to check all possible
2182 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2184 for_each_fib6_node_rt_rcu(fn) {
2185 if (rt->rt6i_nh_flags & RTNH_F_DEAD)
2187 if (rt6_check_expired(rt))
2191 if (!(rt->rt6i_flags & RTF_GATEWAY))
2193 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
2195 /* rt_cache's gateway might be different from its 'parent'
2196 * in the case of an ip redirect.
2197 * So we keep searching in the exception table if the gateway
2200 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
2201 rt_cache = rt6_find_cached_rt(rt,
2205 ipv6_addr_equal(&rdfl->gateway,
2206 &rt_cache->rt6i_gateway)) {
2216 rt = net->ipv6.ip6_null_entry;
2217 else if (rt->dst.error) {
2218 rt = net->ipv6.ip6_null_entry;
2222 if (rt == net->ipv6.ip6_null_entry) {
2223 fn = fib6_backtrack(fn, &fl6->saddr);
2229 ip6_hold_safe(net, &rt, true);
2233 trace_fib6_table_lookup(net, rt, table, fl6);
2237 static struct dst_entry *ip6_route_redirect(struct net *net,
2238 const struct flowi6 *fl6,
2239 const struct in6_addr *gateway)
2241 int flags = RT6_LOOKUP_F_HAS_SADDR;
2242 struct ip6rd_flowi rdfl;
2245 rdfl.gateway = *gateway;
2247 return fib6_rule_lookup(net, &rdfl.fl6,
2248 flags, __ip6_route_redirect);
2251 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2254 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2255 struct dst_entry *dst;
2258 memset(&fl6, 0, sizeof(fl6));
2259 fl6.flowi6_iif = LOOPBACK_IFINDEX;
2260 fl6.flowi6_oif = oif;
2261 fl6.flowi6_mark = mark;
2262 fl6.daddr = iph->daddr;
2263 fl6.saddr = iph->saddr;
2264 fl6.flowlabel = ip6_flowinfo(iph);
2265 fl6.flowi6_uid = uid;
2267 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
2268 rt6_do_redirect(dst, NULL, skb);
2271 EXPORT_SYMBOL_GPL(ip6_redirect);
2273 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2276 const struct ipv6hdr *iph = ipv6_hdr(skb);
2277 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2278 struct dst_entry *dst;
2281 memset(&fl6, 0, sizeof(fl6));
2282 fl6.flowi6_iif = LOOPBACK_IFINDEX;
2283 fl6.flowi6_oif = oif;
2284 fl6.flowi6_mark = mark;
2285 fl6.daddr = msg->dest;
2286 fl6.saddr = iph->daddr;
2287 fl6.flowi6_uid = sock_net_uid(net, NULL);
2289 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
2290 rt6_do_redirect(dst, NULL, skb);
2294 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2296 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2299 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2301 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
2303 struct net_device *dev = dst->dev;
2304 unsigned int mtu = dst_mtu(dst);
2305 struct net *net = dev_net(dev);
2307 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2309 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2310 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
2313 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2314 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2315 * IPV6_MAXPLEN is also valid and means: "any MSS,
2316 * rely only on pmtu discovery"
2318 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2323 static unsigned int ip6_mtu(const struct dst_entry *dst)
2325 const struct rt6_info *rt = (const struct rt6_info *)dst;
2326 unsigned int mtu = rt->rt6i_pmtu;
2327 struct inet6_dev *idev;
2332 mtu = dst_metric_raw(dst, RTAX_MTU);
2339 idev = __in6_dev_get(dst->dev);
2341 mtu = idev->cnf.mtu6;
2345 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2347 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2350 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
2353 struct dst_entry *dst;
2354 struct rt6_info *rt;
2355 struct inet6_dev *idev = in6_dev_get(dev);
2356 struct net *net = dev_net(dev);
2358 if (unlikely(!idev))
2359 return ERR_PTR(-ENODEV);
2361 rt = ip6_dst_alloc(net, dev, 0);
2362 if (unlikely(!rt)) {
2364 dst = ERR_PTR(-ENOMEM);
2368 rt->dst.flags |= DST_HOST;
2369 rt->dst.input = ip6_input;
2370 rt->dst.output = ip6_output;
2371 rt->rt6i_gateway = fl6->daddr;
2372 rt->rt6i_dst.addr = fl6->daddr;
2373 rt->rt6i_dst.plen = 128;
2374 rt->rt6i_idev = idev;
2375 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
2377 /* Add this dst into uncached_list so that rt6_disable_ip() can
2378 * do proper release of the net_device
2380 rt6_uncached_list_add(rt);
2381 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
2383 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2389 static int ip6_dst_gc(struct dst_ops *ops)
2391 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
2392 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2393 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2394 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2395 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2396 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2399 entries = dst_entries_get_fast(ops);
2400 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2401 entries <= rt_max_size)
2404 net->ipv6.ip6_rt_gc_expire++;
2405 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2406 entries = dst_entries_get_slow(ops);
2407 if (entries < ops->gc_thresh)
2408 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
2410 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2411 return entries > rt_max_size;
2414 static int ip6_convert_metrics(struct mx6_config *mxc,
2415 const struct fib6_config *cfg)
2417 struct net *net = cfg->fc_nlinfo.nl_net;
2418 bool ecn_ca = false;
2426 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
2430 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
2431 int type = nla_type(nla);
2436 if (unlikely(type > RTAX_MAX))
2439 if (type == RTAX_CC_ALGO) {
2440 char tmp[TCP_CA_NAME_MAX];
2442 nla_strlcpy(tmp, nla, sizeof(tmp));
2443 val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
2444 if (val == TCP_CA_UNSPEC)
2447 val = nla_get_u32(nla);
2449 if (type == RTAX_HOPLIMIT && val > 255)
2451 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
2455 __set_bit(type - 1, mxc->mx_valid);
2459 __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
2460 mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
2470 static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2471 struct fib6_config *cfg,
2472 const struct in6_addr *gw_addr,
2473 u32 tbid, int flags)
2475 struct flowi6 fl6 = {
2476 .flowi6_oif = cfg->fc_ifindex,
2478 .saddr = cfg->fc_prefsrc,
2480 struct fib6_table *table;
2481 struct rt6_info *rt;
2483 table = fib6_get_table(net, tbid);
2487 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2488 flags |= RT6_LOOKUP_F_HAS_SADDR;
2490 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2491 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
2493 /* if table lookup failed, fall back to full lookup */
2494 if (rt == net->ipv6.ip6_null_entry) {
2502 static int ip6_route_check_nh_onlink(struct net *net,
2503 struct fib6_config *cfg,
2504 struct net_device *dev,
2505 struct netlink_ext_ack *extack)
2507 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2508 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2509 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2510 struct rt6_info *grt;
2514 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2516 if (!grt->dst.error &&
2517 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
2518 NL_SET_ERR_MSG(extack,
2519 "Nexthop has invalid gateway or device mismatch");
2529 static int ip6_route_check_nh(struct net *net,
2530 struct fib6_config *cfg,
2531 struct net_device **_dev,
2532 struct inet6_dev **idev)
2534 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2535 struct net_device *dev = _dev ? *_dev : NULL;
2536 struct rt6_info *grt = NULL;
2537 int err = -EHOSTUNREACH;
2539 if (cfg->fc_table) {
2540 int flags = RT6_LOOKUP_F_IFACE;
2542 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2543 cfg->fc_table, flags);
2545 if (grt->rt6i_flags & RTF_GATEWAY ||
2546 (dev && dev != grt->dst.dev)) {
2554 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
2560 if (dev != grt->dst.dev) {
2565 *_dev = dev = grt->dst.dev;
2566 *idev = grt->rt6i_idev;
2568 in6_dev_hold(grt->rt6i_idev);
2571 if (!(grt->rt6i_flags & RTF_GATEWAY))
2580 static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2581 struct netlink_ext_ack *extack)
2583 struct net *net = cfg->fc_nlinfo.nl_net;
2584 struct rt6_info *rt = NULL;
2585 struct net_device *dev = NULL;
2586 struct inet6_dev *idev = NULL;
2587 struct fib6_table *table;
2591 /* RTF_PCPU is an internal flag; can not be set by userspace */
2592 if (cfg->fc_flags & RTF_PCPU) {
2593 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2597 /* RTF_CACHE is an internal flag; can not be set by userspace */
2598 if (cfg->fc_flags & RTF_CACHE) {
2599 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2603 if (cfg->fc_dst_len > 128) {
2604 NL_SET_ERR_MSG(extack, "Invalid prefix length");
2607 if (cfg->fc_src_len > 128) {
2608 NL_SET_ERR_MSG(extack, "Invalid source address length");
2611 #ifndef CONFIG_IPV6_SUBTREES
2612 if (cfg->fc_src_len) {
2613 NL_SET_ERR_MSG(extack,
2614 "Specifying source address requires IPV6_SUBTREES to be enabled");
2618 if (cfg->fc_ifindex) {
2620 dev = dev_get_by_index(net, cfg->fc_ifindex);
2623 idev = in6_dev_get(dev);
2628 if (cfg->fc_metric == 0)
2629 cfg->fc_metric = IP6_RT_PRIO_USER;
2631 if (cfg->fc_flags & RTNH_F_ONLINK) {
2633 NL_SET_ERR_MSG(extack,
2634 "Nexthop device required for onlink");
2639 if (!(dev->flags & IFF_UP)) {
2640 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2647 if (cfg->fc_nlinfo.nlh &&
2648 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2649 table = fib6_get_table(net, cfg->fc_table);
2651 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2652 table = fib6_new_table(net, cfg->fc_table);
2655 table = fib6_new_table(net, cfg->fc_table);
2661 rt = ip6_dst_alloc(net, NULL,
2662 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
2669 if (cfg->fc_flags & RTF_EXPIRES)
2670 rt6_set_expires(rt, jiffies +
2671 clock_t_to_jiffies(cfg->fc_expires));
2673 rt6_clean_expires(rt);
2675 if (cfg->fc_protocol == RTPROT_UNSPEC)
2676 cfg->fc_protocol = RTPROT_BOOT;
2677 rt->rt6i_protocol = cfg->fc_protocol;
2679 addr_type = ipv6_addr_type(&cfg->fc_dst);
2681 if (addr_type & IPV6_ADDR_MULTICAST)
2682 rt->dst.input = ip6_mc_input;
2683 else if (cfg->fc_flags & RTF_LOCAL)
2684 rt->dst.input = ip6_input;
2686 rt->dst.input = ip6_forward;
2688 rt->dst.output = ip6_output;
2690 if (cfg->fc_encap) {
2691 struct lwtunnel_state *lwtstate;
2693 err = lwtunnel_build_state(cfg->fc_encap_type,
2694 cfg->fc_encap, AF_INET6, cfg,
2698 rt->dst.lwtstate = lwtstate_get(lwtstate);
2699 if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
2700 rt->dst.lwtstate->orig_output = rt->dst.output;
2701 rt->dst.output = lwtunnel_output;
2703 if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
2704 rt->dst.lwtstate->orig_input = rt->dst.input;
2705 rt->dst.input = lwtunnel_input;
2709 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2710 rt->rt6i_dst.plen = cfg->fc_dst_len;
2711 if (rt->rt6i_dst.plen == 128)
2712 rt->dst.flags |= DST_HOST;
2714 #ifdef CONFIG_IPV6_SUBTREES
2715 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
2716 rt->rt6i_src.plen = cfg->fc_src_len;
2719 rt->rt6i_metric = cfg->fc_metric;
2720 rt->rt6i_nh_weight = 1;
2722 /* We cannot add true routes via loopback here,
2723 they would result in kernel looping; promote them to reject routes
2725 if ((cfg->fc_flags & RTF_REJECT) ||
2726 (dev && (dev->flags & IFF_LOOPBACK) &&
2727 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2728 !(cfg->fc_flags & RTF_LOCAL))) {
2729 /* hold loopback dev/idev if we haven't done so. */
2730 if (dev != net->loopback_dev) {
2735 dev = net->loopback_dev;
2737 idev = in6_dev_get(dev);
2743 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
2744 switch (cfg->fc_type) {
2746 rt->dst.error = -EINVAL;
2747 rt->dst.output = dst_discard_out;
2748 rt->dst.input = dst_discard;
2751 rt->dst.error = -EACCES;
2752 rt->dst.output = ip6_pkt_prohibit_out;
2753 rt->dst.input = ip6_pkt_prohibit;
2756 case RTN_UNREACHABLE:
2758 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
2759 : (cfg->fc_type == RTN_UNREACHABLE)
2760 ? -EHOSTUNREACH : -ENETUNREACH;
2761 rt->dst.output = ip6_pkt_discard_out;
2762 rt->dst.input = ip6_pkt_discard;
2768 if (cfg->fc_flags & RTF_GATEWAY) {
2769 const struct in6_addr *gw_addr;
2772 gw_addr = &cfg->fc_gateway;
2773 gwa_type = ipv6_addr_type(gw_addr);
2775 /* if gw_addr is local we will fail to detect this in case
2776 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2777 * will return already-added prefix route via interface that
2778 * prefix route was assigned to, which might be non-loopback.
2781 if (ipv6_chk_addr_and_flags(net, gw_addr,
2782 gwa_type & IPV6_ADDR_LINKLOCAL ?
2783 dev : NULL, 0, 0)) {
2784 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2787 rt->rt6i_gateway = *gw_addr;
2789 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
2790 /* IPv6 strictly inhibits using not link-local
2791 addresses as nexthop address.
2792 Otherwise, router will not able to send redirects.
2793 It is very good, but in some (rare!) circumstances
2794 (SIT, PtP, NBMA NOARP links) it is handy to allow
2795 some exceptions. --ANK
2796 We allow IPv4-mapped nexthops to support RFC4798-type
2799 if (!(gwa_type & (IPV6_ADDR_UNICAST |
2800 IPV6_ADDR_MAPPED))) {
2801 NL_SET_ERR_MSG(extack,
2802 "Invalid gateway address");
2806 if (cfg->fc_flags & RTNH_F_ONLINK) {
2807 err = ip6_route_check_nh_onlink(net, cfg, dev,
2810 err = ip6_route_check_nh(net, cfg, &dev, &idev);
2817 NL_SET_ERR_MSG(extack, "Egress device not specified");
2819 } else if (dev->flags & IFF_LOOPBACK) {
2820 NL_SET_ERR_MSG(extack,
2821 "Egress device can not be loopback device for this route");
2830 if (!(dev->flags & IFF_UP)) {
2831 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2836 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2837 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
2838 NL_SET_ERR_MSG(extack, "Invalid source address");
2842 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
2843 rt->rt6i_prefsrc.plen = 128;
2845 rt->rt6i_prefsrc.plen = 0;
2847 rt->rt6i_flags = cfg->fc_flags;
2850 if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
2851 !netif_carrier_ok(dev))
2852 rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
2853 rt->rt6i_nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
2855 rt->rt6i_idev = idev;
2856 rt->rt6i_table = table;
2858 cfg->fc_nlinfo.nl_net = dev_net(dev);
2867 dst_release_immediate(&rt->dst);
2869 return ERR_PTR(err);
2872 int ip6_route_add(struct fib6_config *cfg,
2873 struct netlink_ext_ack *extack)
2875 struct mx6_config mxc = { .mx = NULL, };
2876 struct rt6_info *rt;
2879 rt = ip6_route_info_create(cfg, extack);
2886 err = ip6_convert_metrics(&mxc, cfg);
2890 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
2897 dst_release_immediate(&rt->dst);
2902 static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
2905 struct fib6_table *table;
2906 struct net *net = dev_net(rt->dst.dev);
2908 if (rt == net->ipv6.ip6_null_entry) {
2913 table = rt->rt6i_table;
2914 spin_lock_bh(&table->tb6_lock);
2915 err = fib6_del(rt, info);
2916 spin_unlock_bh(&table->tb6_lock);
2923 int ip6_del_rt(struct rt6_info *rt)
2925 struct nl_info info = {
2926 .nl_net = dev_net(rt->dst.dev),
2928 return __ip6_del_rt(rt, &info);
2931 static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
2933 struct nl_info *info = &cfg->fc_nlinfo;
2934 struct net *net = info->nl_net;
2935 struct sk_buff *skb = NULL;
2936 struct fib6_table *table;
2939 if (rt == net->ipv6.ip6_null_entry)
2941 table = rt->rt6i_table;
2942 spin_lock_bh(&table->tb6_lock);
2944 if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
2945 struct rt6_info *sibling, *next_sibling;
2947 /* prefer to send a single notification with all hops */
2948 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
2950 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
2952 if (rt6_fill_node(net, skb, rt,
2953 NULL, NULL, 0, RTM_DELROUTE,
2954 info->portid, seq, 0) < 0) {
2958 info->skip_notify = 1;
2961 list_for_each_entry_safe(sibling, next_sibling,
2964 err = fib6_del(sibling, info);
2970 err = fib6_del(rt, info);
2972 spin_unlock_bh(&table->tb6_lock);
2977 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
2978 info->nlh, gfp_any());
2983 static int ip6_route_del(struct fib6_config *cfg,
2984 struct netlink_ext_ack *extack)
2986 struct rt6_info *rt, *rt_cache;
2987 struct fib6_table *table;
2988 struct fib6_node *fn;
2991 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
2993 NL_SET_ERR_MSG(extack, "FIB table does not exist");
2999 fn = fib6_locate(&table->tb6_root,
3000 &cfg->fc_dst, cfg->fc_dst_len,
3001 &cfg->fc_src, cfg->fc_src_len,
3002 !(cfg->fc_flags & RTF_CACHE));
3005 for_each_fib6_node_rt_rcu(fn) {
3006 if (cfg->fc_flags & RTF_CACHE) {
3007 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3013 if (cfg->fc_ifindex &&
3015 rt->dst.dev->ifindex != cfg->fc_ifindex))
3017 if (cfg->fc_flags & RTF_GATEWAY &&
3018 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3020 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
3022 if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
3024 if (!dst_hold_safe(&rt->dst))
3028 /* if gateway was specified only delete the one hop */
3029 if (cfg->fc_flags & RTF_GATEWAY)
3030 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3032 return __ip6_del_rt_siblings(rt, cfg);
3040 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3042 struct netevent_redirect netevent;
3043 struct rt6_info *rt, *nrt = NULL;
3044 struct ndisc_options ndopts;
3045 struct inet6_dev *in6_dev;
3046 struct neighbour *neigh;
3048 int optlen, on_link;
3051 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
3052 optlen -= sizeof(*msg);
3055 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3059 msg = (struct rd_msg *)icmp6_hdr(skb);
3061 if (ipv6_addr_is_multicast(&msg->dest)) {
3062 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3067 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3069 } else if (ipv6_addr_type(&msg->target) !=
3070 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
3071 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3075 in6_dev = __in6_dev_get(skb->dev);
3078 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3082 * The IP source address of the Redirect MUST be the same as the current
3083 * first-hop router for the specified ICMP Destination Address.
3086 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3087 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3092 if (ndopts.nd_opts_tgt_lladdr) {
3093 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3096 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3101 rt = (struct rt6_info *) dst;
3102 if (rt->rt6i_flags & RTF_REJECT) {
3103 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3107 /* Redirect received -> path was valid.
3108 * Look, redirects are sent only in response to data packets,
3109 * so that this nexthop apparently is reachable. --ANK
3111 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
3113 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3118 * We have finally decided to accept it.
3121 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
3122 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3123 NEIGH_UPDATE_F_OVERRIDE|
3124 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3125 NEIGH_UPDATE_F_ISROUTER)),
3126 NDISC_REDIRECT, &ndopts);
3128 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
3132 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3134 nrt->rt6i_flags &= ~RTF_GATEWAY;
3136 nrt->rt6i_protocol = RTPROT_REDIRECT;
3137 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
3139 /* No need to remove rt from the exception table if rt is
3140 * a cached route because rt6_insert_exception() will
3143 if (rt6_insert_exception(nrt, rt)) {
3144 dst_release_immediate(&nrt->dst);
3148 netevent.old = &rt->dst;
3149 netevent.new = &nrt->dst;
3150 netevent.daddr = &msg->dest;
3151 netevent.neigh = neigh;
3152 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3155 neigh_release(neigh);
3159 * Misc support functions
3162 static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
3166 rt->rt6i_flags &= ~RTF_EXPIRES;
3167 dst_hold(&from->dst);
3169 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
3172 static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
3174 rt->dst.input = ort->dst.input;
3175 rt->dst.output = ort->dst.output;
3176 rt->rt6i_dst = ort->rt6i_dst;
3177 rt->dst.error = ort->dst.error;
3178 rt->rt6i_idev = ort->rt6i_idev;
3180 in6_dev_hold(rt->rt6i_idev);
3181 rt->dst.lastuse = jiffies;
3182 rt->rt6i_gateway = ort->rt6i_gateway;
3183 rt->rt6i_flags = ort->rt6i_flags;
3184 rt6_set_from(rt, ort);
3185 rt->rt6i_metric = ort->rt6i_metric;
3186 #ifdef CONFIG_IPV6_SUBTREES
3187 rt->rt6i_src = ort->rt6i_src;
3189 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
3190 rt->rt6i_table = ort->rt6i_table;
3191 rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
3194 #ifdef CONFIG_IPV6_ROUTE_INFO
3195 static struct rt6_info *rt6_get_route_info(struct net *net,
3196 const struct in6_addr *prefix, int prefixlen,
3197 const struct in6_addr *gwaddr,
3198 struct net_device *dev)
3200 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3201 int ifindex = dev->ifindex;
3202 struct fib6_node *fn;
3203 struct rt6_info *rt = NULL;
3204 struct fib6_table *table;
3206 table = fib6_get_table(net, tb_id);
3211 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
3215 for_each_fib6_node_rt_rcu(fn) {
3216 if (rt->dst.dev->ifindex != ifindex)
3218 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
3220 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
3222 ip6_hold_safe(NULL, &rt, false);
3230 static struct rt6_info *rt6_add_route_info(struct net *net,
3231 const struct in6_addr *prefix, int prefixlen,
3232 const struct in6_addr *gwaddr,
3233 struct net_device *dev,
3236 struct fib6_config cfg = {
3237 .fc_metric = IP6_RT_PRIO_USER,
3238 .fc_ifindex = dev->ifindex,
3239 .fc_dst_len = prefixlen,
3240 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3241 RTF_UP | RTF_PREF(pref),
3242 .fc_protocol = RTPROT_RA,
3243 .fc_nlinfo.portid = 0,
3244 .fc_nlinfo.nlh = NULL,
3245 .fc_nlinfo.nl_net = net,
3248 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
3249 cfg.fc_dst = *prefix;
3250 cfg.fc_gateway = *gwaddr;
3252 /* We should treat it as a default route if prefix length is 0. */
3254 cfg.fc_flags |= RTF_DEFAULT;
3256 ip6_route_add(&cfg, NULL);
3258 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
3262 struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
3264 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
3265 struct rt6_info *rt;
3266 struct fib6_table *table;
3268 table = fib6_get_table(dev_net(dev), tb_id);
3273 for_each_fib6_node_rt_rcu(&table->tb6_root) {
3274 if (dev == rt->dst.dev &&
3275 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3276 ipv6_addr_equal(&rt->rt6i_gateway, addr))
3280 ip6_hold_safe(NULL, &rt, false);
3285 struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
3286 struct net_device *dev,
3289 struct fib6_config cfg = {
3290 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3291 .fc_metric = IP6_RT_PRIO_USER,
3292 .fc_ifindex = dev->ifindex,
3293 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3294 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3295 .fc_protocol = RTPROT_RA,
3296 .fc_nlinfo.portid = 0,
3297 .fc_nlinfo.nlh = NULL,
3298 .fc_nlinfo.nl_net = dev_net(dev),
3301 cfg.fc_gateway = *gwaddr;
3303 if (!ip6_route_add(&cfg, NULL)) {
3304 struct fib6_table *table;
3306 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3308 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3311 return rt6_get_dflt_router(gwaddr, dev);
3314 static void __rt6_purge_dflt_routers(struct fib6_table *table)
3316 struct rt6_info *rt;
3320 for_each_fib6_node_rt_rcu(&table->tb6_root) {
3321 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3322 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
3323 if (dst_hold_safe(&rt->dst)) {
3334 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3337 void rt6_purge_dflt_routers(struct net *net)
3339 struct fib6_table *table;
3340 struct hlist_head *head;
3345 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3346 head = &net->ipv6.fib_table_hash[h];
3347 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3348 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3349 __rt6_purge_dflt_routers(table);
3356 static void rtmsg_to_fib6_config(struct net *net,
3357 struct in6_rtmsg *rtmsg,
3358 struct fib6_config *cfg)
3360 memset(cfg, 0, sizeof(*cfg));
3362 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3364 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3365 cfg->fc_metric = rtmsg->rtmsg_metric;
3366 cfg->fc_expires = rtmsg->rtmsg_info;
3367 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3368 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3369 cfg->fc_flags = rtmsg->rtmsg_flags;
3371 cfg->fc_nlinfo.nl_net = net;
3373 cfg->fc_dst = rtmsg->rtmsg_dst;
3374 cfg->fc_src = rtmsg->rtmsg_src;
3375 cfg->fc_gateway = rtmsg->rtmsg_gateway;
3378 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
3380 struct fib6_config cfg;
3381 struct in6_rtmsg rtmsg;
3385 case SIOCADDRT: /* Add a route */
3386 case SIOCDELRT: /* Delete a route */
3387 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3389 err = copy_from_user(&rtmsg, arg,
3390 sizeof(struct in6_rtmsg));
3394 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
3399 err = ip6_route_add(&cfg, NULL);
3402 err = ip6_route_del(&cfg, NULL);
3416 * Drop the packet on the floor
3419 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
3422 struct dst_entry *dst = skb_dst(skb);
3423 switch (ipstats_mib_noroutes) {
3424 case IPSTATS_MIB_INNOROUTES:
3425 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
3426 if (type == IPV6_ADDR_ANY) {
3427 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3428 IPSTATS_MIB_INADDRERRORS);
3432 case IPSTATS_MIB_OUTNOROUTES:
3433 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3434 ipstats_mib_noroutes);
3437 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
3442 static int ip6_pkt_discard(struct sk_buff *skb)
3444 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);