11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 4735732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 66b811580dSDavid Ahern #include <trace/events/fib6.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 711da177e4SLinus Torvalds #include <linux/sysctl.h> 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 74afc154e9SHannes Frederic Sowa enum rt6_nud_state { 757e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 767e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 777e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 78afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 79afc154e9SHannes Frederic Sowa }; 80afc154e9SHannes Frederic Sowa 8183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 821da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 830dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 84ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 851da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 881da177e4SLinus Torvalds struct net_device *dev, int how); 89569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 92ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 937150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 94ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 951da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 966700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 976700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 996700c270SDavid S. Miller struct sk_buff *skb); 1004b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 10152bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 10216a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt); 10316a16cd3SDavid Ahern static int rt6_fill_node(struct net *net, 10416a16cd3SDavid Ahern struct sk_buff *skb, struct rt6_info *rt, 10516a16cd3SDavid Ahern struct in6_addr *dst, struct in6_addr *src, 10616a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10716a16cd3SDavid Ahern unsigned int flags); 10835732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 10935732d01SWei Wang struct in6_addr *daddr, 11035732d01SWei Wang struct in6_addr *saddr); 1111da177e4SLinus Torvalds 11270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 113efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 114b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 115830218c1SDavid Ahern const struct in6_addr *gwaddr, 116830218c1SDavid Ahern struct net_device *dev, 11795c96174SEric Dumazet unsigned int pref); 118efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 119b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 120830218c1SDavid Ahern const struct in6_addr *gwaddr, 121830218c1SDavid Ahern struct net_device *dev); 12270ceb4f5SYOSHIFUJI Hideaki #endif 12370ceb4f5SYOSHIFUJI Hideaki 1248d0b94afSMartin KaFai Lau struct uncached_list { 1258d0b94afSMartin KaFai Lau spinlock_t lock; 1268d0b94afSMartin KaFai Lau struct list_head head; 1278d0b94afSMartin KaFai Lau }; 1288d0b94afSMartin KaFai Lau 1298d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1308d0b94afSMartin KaFai Lau 131510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1328d0b94afSMartin KaFai Lau { 1338d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1348d0b94afSMartin KaFai Lau 1358d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1388d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1398d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1408d0b94afSMartin KaFai Lau } 1418d0b94afSMartin KaFai Lau 142510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1438d0b94afSMartin KaFai Lau { 1448d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1458d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 14681eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1478d0b94afSMartin KaFai Lau 1488d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1498d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15081eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1518d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1528d0b94afSMartin KaFai Lau } 1538d0b94afSMartin KaFai Lau } 1548d0b94afSMartin KaFai Lau 1558d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1568d0b94afSMartin KaFai Lau { 1578d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1588d0b94afSMartin KaFai Lau int cpu; 1598d0b94afSMartin KaFai Lau 160e332bc67SEric W. Biederman if (dev == loopback_dev) 161e332bc67SEric W. Biederman return; 162e332bc67SEric W. Biederman 1638d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1648d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1658d0b94afSMartin KaFai Lau struct rt6_info *rt; 1668d0b94afSMartin KaFai Lau 1678d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1688d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1698d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1708d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1718d0b94afSMartin KaFai Lau 172e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1738d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1748d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1758d0b94afSMartin KaFai Lau } 1768d0b94afSMartin KaFai Lau 177e332bc67SEric W. Biederman if (rt_dev == dev) { 1788d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1798d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1808d0b94afSMartin KaFai Lau dev_put(rt_dev); 1818d0b94afSMartin KaFai Lau } 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1848d0b94afSMartin KaFai Lau } 1858d0b94afSMartin KaFai Lau } 1868d0b94afSMartin KaFai Lau 187d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 188d52d3997SMartin KaFai Lau { 1893a2232e9SDavid Miller return dst_metrics_write_ptr(&rt->from->dst); 190d52d3997SMartin KaFai Lau } 191d52d3997SMartin KaFai Lau 19206582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 19306582540SDavid S. Miller { 19406582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19506582540SDavid S. Miller 196d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 197d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 198d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1994b32b5adSMartin KaFai Lau return NULL; 2004b32b5adSMartin KaFai Lau else 2013b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 20206582540SDavid S. Miller } 20306582540SDavid S. Miller 204f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 205f894cbf8SDavid S. Miller struct sk_buff *skb, 206f894cbf8SDavid S. Miller const void *daddr) 20739232973SDavid S. Miller { 20839232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20939232973SDavid S. Miller 210a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 21139232973SDavid S. Miller return (const void *) p; 212f894cbf8SDavid S. Miller else if (skb) 213f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 21439232973SDavid S. Miller return daddr; 21539232973SDavid S. Miller } 21639232973SDavid S. Miller 217f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 218f894cbf8SDavid S. Miller struct sk_buff *skb, 219f894cbf8SDavid S. Miller const void *daddr) 220d3aaeb38SDavid S. Miller { 22139232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 22239232973SDavid S. Miller struct neighbour *n; 22339232973SDavid S. Miller 224f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2258e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 226f83c7790SDavid S. Miller if (n) 227f83c7790SDavid S. Miller return n; 228f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 229f83c7790SDavid S. Miller } 230f83c7790SDavid S. Miller 23163fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23263fca65dSJulian Anastasov { 23363fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23463fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23563fca65dSJulian Anastasov 23663fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23763fca65dSJulian Anastasov if (!daddr) 23863fca65dSJulian Anastasov return; 23963fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 24063fca65dSJulian Anastasov return; 24163fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24263fca65dSJulian Anastasov return; 24363fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24463fca65dSJulian Anastasov } 24563fca65dSJulian Anastasov 2469a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2471da177e4SLinus Torvalds .family = AF_INET6, 2481da177e4SLinus Torvalds .gc = ip6_dst_gc, 2491da177e4SLinus Torvalds .gc_thresh = 1024, 2501da177e4SLinus Torvalds .check = ip6_dst_check, 2510dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 252ebb762f2SSteffen Klassert .mtu = ip6_mtu, 25306582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2541da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2551da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2561da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2571da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2581da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2596e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2609f8955ccSEric W. Biederman .local_out = __ip6_local_out, 261d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26263fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2631da177e4SLinus Torvalds }; 2641da177e4SLinus Torvalds 265ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 266ec831ea7SRoland Dreier { 267618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 268618f9bc7SSteffen Klassert 269618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 270ec831ea7SRoland Dreier } 271ec831ea7SRoland Dreier 2726700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2736700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27414e50e57SDavid S. Miller { 27514e50e57SDavid S. Miller } 27614e50e57SDavid S. Miller 2776700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2786700c270SDavid S. Miller struct sk_buff *skb) 279b587ee3bSDavid S. Miller { 280b587ee3bSDavid S. Miller } 281b587ee3bSDavid S. Miller 28214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28314e50e57SDavid S. Miller .family = AF_INET6, 28414e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28514e50e57SDavid S. Miller .check = ip6_dst_check, 286ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 287214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28814e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 289b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2900a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 291d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 29214e50e57SDavid S. Miller }; 29314e50e57SDavid S. Miller 29462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29514edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29662fa8a84SDavid S. Miller }; 29762fa8a84SDavid S. Miller 298fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2991da177e4SLinus Torvalds .dst = { 3001da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3011da177e4SLinus Torvalds .__use = 1, 3022c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3031da177e4SLinus Torvalds .error = -ENETUNREACH, 3041da177e4SLinus Torvalds .input = ip6_pkt_discard, 3051da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3061da177e4SLinus Torvalds }, 3071da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3084f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3091da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3101da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3111da177e4SLinus Torvalds }; 3121da177e4SLinus Torvalds 313101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 314101367c2SThomas Graf 315fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 316101367c2SThomas Graf .dst = { 317101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 318101367c2SThomas Graf .__use = 1, 3192c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 320101367c2SThomas Graf .error = -EACCES, 3219ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3229ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 323101367c2SThomas Graf }, 324101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3254f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 326101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 327101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 328101367c2SThomas Graf }; 329101367c2SThomas Graf 330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 331101367c2SThomas Graf .dst = { 332101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 333101367c2SThomas Graf .__use = 1, 3342c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 335101367c2SThomas Graf .error = -EINVAL, 336352e512cSHerbert Xu .input = dst_discard, 337ede2059dSEric W. Biederman .output = dst_discard_out, 338101367c2SThomas Graf }, 339101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3404f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 341101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 342101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 343101367c2SThomas Graf }; 344101367c2SThomas Graf 345101367c2SThomas Graf #endif 346101367c2SThomas Graf 347ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 348ebfa45f0SMartin KaFai Lau { 349ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 350ebfa45f0SMartin KaFai Lau 351ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 352ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 353ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 354ebfa45f0SMartin KaFai Lau } 355ebfa45f0SMartin KaFai Lau 3561da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 357d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 358957c665fSDavid S. Miller struct net_device *dev, 359ad706862SMartin KaFai Lau int flags) 3601da177e4SLinus Torvalds { 36197bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 362b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 363cf911662SDavid S. Miller 36481eb8447SWei Wang if (rt) { 365ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 36681eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 36781eb8447SWei Wang } 3688104891bSSteffen Klassert 369cf911662SDavid S. Miller return rt; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3729ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 373d52d3997SMartin KaFai Lau struct net_device *dev, 374ad706862SMartin KaFai Lau int flags) 375d52d3997SMartin KaFai Lau { 376ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 377d52d3997SMartin KaFai Lau 378d52d3997SMartin KaFai Lau if (rt) { 379d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 380bfd8e5a4SEric Dumazet if (!rt->rt6i_pcpu) { 381587fea74SWei Wang dst_release_immediate(&rt->dst); 382d52d3997SMartin KaFai Lau return NULL; 383d52d3997SMartin KaFai Lau } 384d52d3997SMartin KaFai Lau } 385d52d3997SMartin KaFai Lau 386d52d3997SMartin KaFai Lau return rt; 387d52d3997SMartin KaFai Lau } 3889ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 389d52d3997SMartin KaFai Lau 3901da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 39335732d01SWei Wang struct rt6_exception_bucket *bucket; 3943a2232e9SDavid Miller struct rt6_info *from = rt->from; 3958d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3961da177e4SLinus Torvalds 3978e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 398d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 3998d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4008d0b94afSMartin KaFai Lau 4018d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40238308473SDavid S. Miller if (idev) { 4031da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4041da177e4SLinus Torvalds in6_dev_put(idev); 4051da177e4SLinus Torvalds } 40635732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 40735732d01SWei Wang if (bucket) { 40835732d01SWei Wang rt->rt6i_exception_bucket = NULL; 40935732d01SWei Wang kfree(bucket); 41035732d01SWei Wang } 4111716a961SGao feng 4123a2232e9SDavid Miller rt->from = NULL; 4133a2232e9SDavid Miller dst_release(&from->dst); 414b3419363SDavid S. Miller } 415b3419363SDavid S. Miller 4161da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4171da177e4SLinus Torvalds int how) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4201da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4215a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 422c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4231da177e4SLinus Torvalds 424e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 425e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 42638308473SDavid S. Miller if (loopback_idev) { 4271da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4281da177e4SLinus Torvalds in6_dev_put(idev); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 43197cac082SDavid S. Miller } 4321da177e4SLinus Torvalds 4335973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4345973fb1eSMartin KaFai Lau { 4355973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4365973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4375973fb1eSMartin KaFai Lau else 4385973fb1eSMartin KaFai Lau return false; 4395973fb1eSMartin KaFai Lau } 4405973fb1eSMartin KaFai Lau 441a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4421da177e4SLinus Torvalds { 4431716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4441716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 445a50feda5SEric Dumazet return true; 4463a2232e9SDavid Miller } else if (rt->from) { 4471e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4483a2232e9SDavid Miller rt6_check_expired(rt->from); 4491716a961SGao feng } 450a50feda5SEric Dumazet return false; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 453b4bac172SDavid Ahern static struct rt6_info *rt6_multipath_select(const struct net *net, 454b4bac172SDavid Ahern struct rt6_info *match, 45552bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 456b75cc8f9SDavid Ahern const struct sk_buff *skb, 45752bd4c0cSNicolas Dichtel int strict) 45851ebd318SNicolas Dichtel { 45951ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46051ebd318SNicolas Dichtel 461b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 462b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 463b673d6ccSJakub Sitnicki */ 464b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 465b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 466b673d6ccSJakub Sitnicki 4673d709f69SIdo Schimmel if (fl6->mp_hash <= atomic_read(&match->rt6i_nh_upper_bound)) 4683d709f69SIdo Schimmel return match; 469bbfcd776SIdo Schimmel 4703d709f69SIdo Schimmel list_for_each_entry_safe(sibling, next_sibling, &match->rt6i_siblings, 4713d709f69SIdo Schimmel rt6i_siblings) { 4723d709f69SIdo Schimmel if (fl6->mp_hash > atomic_read(&sibling->rt6i_nh_upper_bound)) 4733d709f69SIdo Schimmel continue; 47452bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 47552bd4c0cSNicolas Dichtel break; 47651ebd318SNicolas Dichtel match = sibling; 47751ebd318SNicolas Dichtel break; 47851ebd318SNicolas Dichtel } 4793d709f69SIdo Schimmel 48051ebd318SNicolas Dichtel return match; 48151ebd318SNicolas Dichtel } 48251ebd318SNicolas Dichtel 4831da177e4SLinus Torvalds /* 48466f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds 4878ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4888ed67789SDaniel Lezcano struct rt6_info *rt, 489b71d1d42SEric Dumazet const struct in6_addr *saddr, 4901da177e4SLinus Torvalds int oif, 491d420895eSYOSHIFUJI Hideaki int flags) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct rt6_info *local = NULL; 4941da177e4SLinus Torvalds struct rt6_info *sprt; 4951da177e4SLinus Torvalds 4968067bb8cSIdo Schimmel if (!oif && ipv6_addr_any(saddr) && !(rt->rt6i_nh_flags & RTNH_F_DEAD)) 4978067bb8cSIdo Schimmel return rt; 498dd3abc4eSYOSHIFUJI Hideaki 499071fb37eSDavid Miller for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) { 500d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 501dd3abc4eSYOSHIFUJI Hideaki 5028067bb8cSIdo Schimmel if (sprt->rt6i_nh_flags & RTNH_F_DEAD) 5038067bb8cSIdo Schimmel continue; 5048067bb8cSIdo Schimmel 505dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5061da177e4SLinus Torvalds if (dev->ifindex == oif) 5071da177e4SLinus Torvalds return sprt; 5081da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 50938308473SDavid S. Miller if (!sprt->rt6i_idev || 5101da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51117fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5121da177e4SLinus Torvalds continue; 51317fb0b2bSDavid Ahern if (local && 51417fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5151da177e4SLinus Torvalds continue; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds local = sprt; 5181da177e4SLinus Torvalds } 519dd3abc4eSYOSHIFUJI Hideaki } else { 520dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 521dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 522dd3abc4eSYOSHIFUJI Hideaki return sprt; 523dd3abc4eSYOSHIFUJI Hideaki } 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 526dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5271da177e4SLinus Torvalds if (local) 5281da177e4SLinus Torvalds return local; 5291da177e4SLinus Torvalds 530d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5318ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5321da177e4SLinus Torvalds } 5338067bb8cSIdo Schimmel 5348067bb8cSIdo Schimmel return rt->rt6i_nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : rt; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 53727097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 538c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 539c2f17e82SHannes Frederic Sowa struct work_struct work; 540c2f17e82SHannes Frederic Sowa struct in6_addr target; 541c2f17e82SHannes Frederic Sowa struct net_device *dev; 542c2f17e82SHannes Frederic Sowa }; 543c2f17e82SHannes Frederic Sowa 544c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 545c2f17e82SHannes Frederic Sowa { 546c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 547c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 548c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 549c2f17e82SHannes Frederic Sowa 550c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 551adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 552c2f17e82SHannes Frederic Sowa dev_put(work->dev); 553662f5533SMichael Büsch kfree(work); 554c2f17e82SHannes Frederic Sowa } 555c2f17e82SHannes Frederic Sowa 55627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 55727097255SYOSHIFUJI Hideaki { 558990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 559f2c31e32SEric Dumazet struct neighbour *neigh; 56027097255SYOSHIFUJI Hideaki /* 56127097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56227097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56327097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56427097255SYOSHIFUJI Hideaki * 56527097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56627097255SYOSHIFUJI Hideaki * to no more than one per minute. 56727097255SYOSHIFUJI Hideaki */ 5682152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 569fdd6681dSAmerigo Wang return; 5702152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5712152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5738d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5748d6c31bfSMartin KaFai Lau goto out; 5758d6c31bfSMartin KaFai Lau 576990edb42SMartin KaFai Lau work = NULL; 5772152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 578990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 579990edb42SMartin KaFai Lau time_after(jiffies, 580990edb42SMartin KaFai Lau neigh->updated + 581990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 582c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 583990edb42SMartin KaFai Lau if (work) 5847e980569SJiri Benc __neigh_set_probe_once(neigh); 585990edb42SMartin KaFai Lau } 586c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 587990edb42SMartin KaFai Lau } else { 588990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 589990edb42SMartin KaFai Lau } 590c2f17e82SHannes Frederic Sowa 591c2f17e82SHannes Frederic Sowa if (work) { 592c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 593c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 594c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 595c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 596c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 597c2f17e82SHannes Frederic Sowa } 598990edb42SMartin KaFai Lau 5998d6c31bfSMartin KaFai Lau out: 6002152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 601f2c31e32SEric Dumazet } 60227097255SYOSHIFUJI Hideaki #else 60327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60427097255SYOSHIFUJI Hideaki { 60527097255SYOSHIFUJI Hideaki } 60627097255SYOSHIFUJI Hideaki #endif 60727097255SYOSHIFUJI Hideaki 6081da177e4SLinus Torvalds /* 609554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6101da177e4SLinus Torvalds */ 611b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6121da177e4SLinus Torvalds { 613d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 614161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 615554cfb7eSYOSHIFUJI Hideaki return 2; 616161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 617161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 618161980f4SDavid S. Miller return 1; 619554cfb7eSYOSHIFUJI Hideaki return 0; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 622afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6231da177e4SLinus Torvalds { 624f2c31e32SEric Dumazet struct neighbour *neigh; 625afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 626f2c31e32SEric Dumazet 6274d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6284d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 629afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 630145a3621SYOSHIFUJI Hideaki / 吉藤英明 631145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 632145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 633145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 635554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 636afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 637398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 638a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 639afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6407e980569SJiri Benc else 6417e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 642398bcbebSYOSHIFUJI Hideaki #endif 643145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 644afc154e9SHannes Frederic Sowa } else { 645afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6467e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 647a5a81f0bSPaul Marks } 648145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 649145a3621SYOSHIFUJI Hideaki / 吉藤英明 650a5a81f0bSPaul Marks return ret; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 654554cfb7eSYOSHIFUJI Hideaki int strict) 655554cfb7eSYOSHIFUJI Hideaki { 656a5a81f0bSPaul Marks int m; 6574d0c5911SYOSHIFUJI Hideaki 6584d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 65977d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 660afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 661ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 662ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 663ebacaaa0SYOSHIFUJI Hideaki #endif 664afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 665afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 666afc154e9SHannes Frederic Sowa if (n < 0) 667afc154e9SHannes Frederic Sowa return n; 668afc154e9SHannes Frederic Sowa } 669554cfb7eSYOSHIFUJI Hideaki return m; 670554cfb7eSYOSHIFUJI Hideaki } 671554cfb7eSYOSHIFUJI Hideaki 672f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 673afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 674afc154e9SHannes Frederic Sowa bool *do_rr) 675554cfb7eSYOSHIFUJI Hideaki { 676554cfb7eSYOSHIFUJI Hideaki int m; 677afc154e9SHannes Frederic Sowa bool match_do_rr = false; 67835103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 67935103d11SAndy Gospodarek 6808067bb8cSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 6818067bb8cSIdo Schimmel goto out; 6828067bb8cSIdo Schimmel 68314c5206cSIdo Schimmel if (idev->cnf.ignore_routes_with_linkdown && 68414c5206cSIdo Schimmel rt->rt6i_nh_flags & RTNH_F_LINKDOWN && 685d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68635103d11SAndy Gospodarek goto out; 687554cfb7eSYOSHIFUJI Hideaki 688554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 689f11e6659SDavid S. Miller goto out; 690554cfb7eSYOSHIFUJI Hideaki 691554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6927e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 693afc154e9SHannes Frederic Sowa match_do_rr = true; 694afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6957e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 696f11e6659SDavid S. Miller goto out; 6971da177e4SLinus Torvalds } 698f11e6659SDavid S. Miller 699afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 700afc154e9SHannes Frederic Sowa rt6_probe(rt); 701afc154e9SHannes Frederic Sowa 7027e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 703afc154e9SHannes Frederic Sowa if (m > *mpri) { 704afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 705afc154e9SHannes Frederic Sowa *mpri = m; 706afc154e9SHannes Frederic Sowa match = rt; 707afc154e9SHannes Frederic Sowa } 708f11e6659SDavid S. Miller out: 709f11e6659SDavid S. Miller return match; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 712f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 7138d1040e8SWei Wang struct rt6_info *leaf, 714f11e6659SDavid S. Miller struct rt6_info *rr_head, 715afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 716afc154e9SHannes Frederic Sowa bool *do_rr) 717f11e6659SDavid S. Miller { 7189fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 719f11e6659SDavid S. Miller int mpri = -1; 720f11e6659SDavid S. Miller 721f11e6659SDavid S. Miller match = NULL; 7229fbdcfafSSteffen Klassert cont = NULL; 723071fb37eSDavid Miller for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) { 7249fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7259fbdcfafSSteffen Klassert cont = rt; 7269fbdcfafSSteffen Klassert break; 7279fbdcfafSSteffen Klassert } 7289fbdcfafSSteffen Klassert 729afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7309fbdcfafSSteffen Klassert } 7319fbdcfafSSteffen Klassert 73266f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 733071fb37eSDavid Miller rt = rcu_dereference(rt->rt6_next)) { 7349fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7359fbdcfafSSteffen Klassert cont = rt; 7369fbdcfafSSteffen Klassert break; 7379fbdcfafSSteffen Klassert } 7389fbdcfafSSteffen Klassert 7399fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7409fbdcfafSSteffen Klassert } 7419fbdcfafSSteffen Klassert 7429fbdcfafSSteffen Klassert if (match || !cont) 7439fbdcfafSSteffen Klassert return match; 7449fbdcfafSSteffen Klassert 745071fb37eSDavid Miller for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next)) 746afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 747f11e6659SDavid S. Miller 748f11e6659SDavid S. Miller return match; 749f11e6659SDavid S. Miller } 750f11e6659SDavid S. Miller 7518d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, 7528d1040e8SWei Wang int oif, int strict) 753f11e6659SDavid S. Miller { 75466f5d6ceSWei Wang struct rt6_info *leaf = rcu_dereference(fn->leaf); 755f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 756afc154e9SHannes Frederic Sowa bool do_rr = false; 75717ecf590SWei Wang int key_plen; 758f11e6659SDavid S. Miller 75987b1af8dSWei Wang if (!leaf || leaf == net->ipv6.ip6_null_entry) 7608d1040e8SWei Wang return net->ipv6.ip6_null_entry; 7618d1040e8SWei Wang 76266f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 763f11e6659SDavid S. Miller if (!rt0) 76466f5d6ceSWei Wang rt0 = leaf; 765f11e6659SDavid S. Miller 76617ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 76717ecf590SWei Wang * and fn->leaf does not points to its child's leaf 76817ecf590SWei Wang * (This might happen if all routes under fn are deleted from 76917ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 77017ecf590SWei Wang */ 77117ecf590SWei Wang key_plen = rt0->rt6i_dst.plen; 77217ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 77317ecf590SWei Wang if (rt0->rt6i_src.plen) 77417ecf590SWei Wang key_plen = rt0->rt6i_src.plen; 77517ecf590SWei Wang #endif 77617ecf590SWei Wang if (fn->fn_bit != key_plen) 77717ecf590SWei Wang return net->ipv6.ip6_null_entry; 77817ecf590SWei Wang 7798d1040e8SWei Wang match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, 780afc154e9SHannes Frederic Sowa &do_rr); 781f11e6659SDavid S. Miller 782afc154e9SHannes Frederic Sowa if (do_rr) { 783071fb37eSDavid Miller struct rt6_info *next = rcu_dereference(rt0->rt6_next); 784f11e6659SDavid S. Miller 785554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 786f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 7878d1040e8SWei Wang next = leaf; 788f11e6659SDavid S. Miller 78966f5d6ceSWei Wang if (next != rt0) { 79066f5d6ceSWei Wang spin_lock_bh(&leaf->rt6i_table->tb6_lock); 79166f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 79266f5d6ceSWei Wang if (next->rt6i_node) 79366f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 79466f5d6ceSWei Wang spin_unlock_bh(&leaf->rt6i_table->tb6_lock); 79566f5d6ceSWei Wang } 796554cfb7eSYOSHIFUJI Hideaki } 797554cfb7eSYOSHIFUJI Hideaki 798a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8018b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 8028b9df265SMartin KaFai Lau { 8038b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 8048b9df265SMartin KaFai Lau } 8058b9df265SMartin KaFai Lau 80670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 80770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 808b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 80970ceb4f5SYOSHIFUJI Hideaki { 810c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 81170ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 81270ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 81370ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 8144bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 81570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 81670ceb4f5SYOSHIFUJI Hideaki 81770ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 81870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81970ceb4f5SYOSHIFUJI Hideaki } 82070ceb4f5SYOSHIFUJI Hideaki 82170ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 82270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 82370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 82570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 82770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 82870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82970ceb4f5SYOSHIFUJI Hideaki } 83070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 83170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 83270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83370ceb4f5SYOSHIFUJI Hideaki } 83470ceb4f5SYOSHIFUJI Hideaki } 83570ceb4f5SYOSHIFUJI Hideaki 83670ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 83770ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8383933fc95SJens Rosenboom return -EINVAL; 83970ceb4f5SYOSHIFUJI Hideaki 8404bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 84170ceb4f5SYOSHIFUJI Hideaki 84270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 84370ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 84470ceb4f5SYOSHIFUJI Hideaki else { 84570ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 84670ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 84770ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 84870ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 84970ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 85070ceb4f5SYOSHIFUJI Hideaki } 85170ceb4f5SYOSHIFUJI Hideaki 852f104a567SDuan Jiong if (rinfo->prefix_len == 0) 853f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 854f104a567SDuan Jiong else 855f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 856830218c1SDavid Ahern gwaddr, dev); 85770ceb4f5SYOSHIFUJI Hideaki 85870ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 859e0a1ad73SThomas Graf ip6_del_rt(rt); 86070ceb4f5SYOSHIFUJI Hideaki rt = NULL; 86170ceb4f5SYOSHIFUJI Hideaki } 86270ceb4f5SYOSHIFUJI Hideaki 86370ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 864830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 865830218c1SDavid Ahern dev, pref); 86670ceb4f5SYOSHIFUJI Hideaki else if (rt) 86770ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 86870ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 86970ceb4f5SYOSHIFUJI Hideaki 87070ceb4f5SYOSHIFUJI Hideaki if (rt) { 8711716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8721716a961SGao feng rt6_clean_expires(rt); 8731716a961SGao feng else 8741716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8751716a961SGao feng 87694e187c0SAmerigo Wang ip6_rt_put(rt); 87770ceb4f5SYOSHIFUJI Hideaki } 87870ceb4f5SYOSHIFUJI Hideaki return 0; 87970ceb4f5SYOSHIFUJI Hideaki } 88070ceb4f5SYOSHIFUJI Hideaki #endif 88170ceb4f5SYOSHIFUJI Hideaki 882a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 883a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 884a3c00e46SMartin KaFai Lau { 88566f5d6ceSWei Wang struct fib6_node *pn, *sn; 886a3c00e46SMartin KaFai Lau while (1) { 887a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 888a3c00e46SMartin KaFai Lau return NULL; 88966f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 89066f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 89166f5d6ceSWei Wang if (sn && sn != fn) 89266f5d6ceSWei Wang fn = fib6_lookup(sn, NULL, saddr); 893a3c00e46SMartin KaFai Lau else 894a3c00e46SMartin KaFai Lau fn = pn; 895a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 896a3c00e46SMartin KaFai Lau return fn; 897a3c00e46SMartin KaFai Lau } 898a3c00e46SMartin KaFai Lau } 899c71099acSThomas Graf 900d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 901d3843fe5SWei Wang bool null_fallback) 902d3843fe5SWei Wang { 903d3843fe5SWei Wang struct rt6_info *rt = *prt; 904d3843fe5SWei Wang 905d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 906d3843fe5SWei Wang return true; 907d3843fe5SWei Wang if (null_fallback) { 908d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 909d3843fe5SWei Wang dst_hold(&rt->dst); 910d3843fe5SWei Wang } else { 911d3843fe5SWei Wang rt = NULL; 912d3843fe5SWei Wang } 913d3843fe5SWei Wang *prt = rt; 914d3843fe5SWei Wang return false; 915d3843fe5SWei Wang } 916d3843fe5SWei Wang 9178ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 9188ed67789SDaniel Lezcano struct fib6_table *table, 919b75cc8f9SDavid Ahern struct flowi6 *fl6, 920b75cc8f9SDavid Ahern const struct sk_buff *skb, 921b75cc8f9SDavid Ahern int flags) 9221da177e4SLinus Torvalds { 9232b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 9241da177e4SLinus Torvalds struct fib6_node *fn; 9251da177e4SLinus Torvalds 926b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 927b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 928b6cdbc85SDavid Ahern 92966f5d6ceSWei Wang rcu_read_lock(); 9304c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 931c71099acSThomas Graf restart: 93266f5d6ceSWei Wang rt = rcu_dereference(fn->leaf); 93366f5d6ceSWei Wang if (!rt) { 93466f5d6ceSWei Wang rt = net->ipv6.ip6_null_entry; 93566f5d6ceSWei Wang } else { 93666f5d6ceSWei Wang rt = rt6_device_match(net, rt, &fl6->saddr, 93766f5d6ceSWei Wang fl6->flowi6_oif, flags); 93851ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 939b4bac172SDavid Ahern rt = rt6_multipath_select(net, rt, fl6, fl6->flowi6_oif, 940b75cc8f9SDavid Ahern skb, flags); 94166f5d6ceSWei Wang } 942a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 943a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 944a3c00e46SMartin KaFai Lau if (fn) 945a3c00e46SMartin KaFai Lau goto restart; 946a3c00e46SMartin KaFai Lau } 9472b760fcfSWei Wang /* Search through exception table */ 9482b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 9492b760fcfSWei Wang if (rt_cache) 9502b760fcfSWei Wang rt = rt_cache; 9512b760fcfSWei Wang 952d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 953d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 954d3843fe5SWei Wang 95566f5d6ceSWei Wang rcu_read_unlock(); 956b811580dSDavid Ahern 957b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 958b811580dSDavid Ahern 9591da177e4SLinus Torvalds return rt; 960c71099acSThomas Graf 961c71099acSThomas Graf } 962c71099acSThomas Graf 963ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 964b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 965ea6e574eSFlorian Westphal { 966b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 967ea6e574eSFlorian Westphal } 968ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 969ea6e574eSFlorian Westphal 9709acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 971b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 972b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 973c71099acSThomas Graf { 9744c9483b2SDavid S. Miller struct flowi6 fl6 = { 9754c9483b2SDavid S. Miller .flowi6_oif = oif, 9764c9483b2SDavid S. Miller .daddr = *daddr, 977c71099acSThomas Graf }; 978c71099acSThomas Graf struct dst_entry *dst; 97977d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 980c71099acSThomas Graf 981adaa70bbSThomas Graf if (saddr) { 9824c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 983adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 984adaa70bbSThomas Graf } 985adaa70bbSThomas Graf 986b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 987c71099acSThomas Graf if (dst->error == 0) 988c71099acSThomas Graf return (struct rt6_info *) dst; 989c71099acSThomas Graf 990c71099acSThomas Graf dst_release(dst); 991c71099acSThomas Graf 9921da177e4SLinus Torvalds return NULL; 9931da177e4SLinus Torvalds } 9947159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9957159039aSYOSHIFUJI Hideaki 996c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9971cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9981cfb71eeSWei Wang * route is released. 9991cfb71eeSWei Wang * Caller must hold dst before calling it. 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds 1002e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 1003333c4301SDavid Ahern struct mx6_config *mxc, 1004333c4301SDavid Ahern struct netlink_ext_ack *extack) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds int err; 1007c71099acSThomas Graf struct fib6_table *table; 10081da177e4SLinus Torvalds 1009c71099acSThomas Graf table = rt->rt6i_table; 101066f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1011333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 101266f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds return err; 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 101740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 101840e22e8fSThomas Graf { 1019e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 1020e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 1021e715b6d3SFlorian Westphal 10221cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 10231cfb71eeSWei Wang dst_hold(&rt->dst); 1024333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 102540e22e8fSThomas Graf } 102640e22e8fSThomas Graf 10274832c30dSDavid Ahern /* called with rcu_lock held */ 10284832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 10294832c30dSDavid Ahern { 10304832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 10314832c30dSDavid Ahern 103298d11291SDavid Ahern if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) { 10334832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 10344832c30dSDavid Ahern * device if it is a master device, the master device if 10354832c30dSDavid Ahern * device is enslaved, and the loopback as the default 10364832c30dSDavid Ahern */ 10374832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 10384832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 10394832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 10404832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 10414832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 10424832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 10434832c30dSDavid Ahern * case we want dev returned to be dev 10444832c30dSDavid Ahern */ 10454832c30dSDavid Ahern } 10464832c30dSDavid Ahern 10474832c30dSDavid Ahern return dev; 10484832c30dSDavid Ahern } 10494832c30dSDavid Ahern 10508b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 105121efcfa0SEric Dumazet const struct in6_addr *daddr, 1052b71d1d42SEric Dumazet const struct in6_addr *saddr) 10531da177e4SLinus Torvalds { 10544832c30dSDavid Ahern struct net_device *dev; 10551da177e4SLinus Torvalds struct rt6_info *rt; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Clone the route. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds 1061d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 10623a2232e9SDavid Miller ort = ort->from; 10631da177e4SLinus Torvalds 10644832c30dSDavid Ahern rcu_read_lock(); 10654832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10664832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10674832c30dSDavid Ahern rcu_read_unlock(); 106883a09abdSMartin KaFai Lau if (!rt) 106983a09abdSMartin KaFai Lau return NULL; 107083a09abdSMartin KaFai Lau 107183a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10728b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 107383a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 107483a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 107583a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 107683a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10778b9df265SMartin KaFai Lau 10788b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1079bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 108021efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 108158c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10831da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10844e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10851da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds #endif 108895a9a5baSYOSHIFUJI Hideaki } 108995a9a5baSYOSHIFUJI Hideaki 1090299d9939SYOSHIFUJI Hideaki return rt; 1091299d9939SYOSHIFUJI Hideaki } 1092299d9939SYOSHIFUJI Hideaki 1093d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1094d52d3997SMartin KaFai Lau { 10954832c30dSDavid Ahern struct net_device *dev; 1096d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1097d52d3997SMartin KaFai Lau 10984832c30dSDavid Ahern rcu_read_lock(); 10994832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 11004832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 11014832c30dSDavid Ahern rcu_read_unlock(); 1102d52d3997SMartin KaFai Lau if (!pcpu_rt) 1103d52d3997SMartin KaFai Lau return NULL; 1104d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1105d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1106d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1107d52d3997SMartin KaFai Lau return pcpu_rt; 1108d52d3997SMartin KaFai Lau } 1109d52d3997SMartin KaFai Lau 111066f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1111d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1112d52d3997SMartin KaFai Lau { 1113a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1114d52d3997SMartin KaFai Lau 1115d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1116d52d3997SMartin KaFai Lau pcpu_rt = *p; 1117d52d3997SMartin KaFai Lau 1118d3843fe5SWei Wang if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false)) 1119a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1120d3843fe5SWei Wang 1121a73e4195SMartin KaFai Lau return pcpu_rt; 1122a73e4195SMartin KaFai Lau } 1123a73e4195SMartin KaFai Lau 1124a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1125a73e4195SMartin KaFai Lau { 1126a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1127d52d3997SMartin KaFai Lau 1128d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1129d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1130d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1131d52d3997SMartin KaFai Lau 11329c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 11339c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1134d52d3997SMartin KaFai Lau } 1135d52d3997SMartin KaFai Lau 1136a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1137a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1138d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1139951f788aSEric Dumazet BUG_ON(prev); 1140a94b9367SWei Wang 1141d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1142d52d3997SMartin KaFai Lau return pcpu_rt; 1143d52d3997SMartin KaFai Lau } 1144d52d3997SMartin KaFai Lau 114535732d01SWei Wang /* exception hash table implementation 114635732d01SWei Wang */ 114735732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 114835732d01SWei Wang 114935732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 115035732d01SWei Wang * Caller must hold rt6_exception_lock 115135732d01SWei Wang */ 115235732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 115335732d01SWei Wang struct rt6_exception *rt6_ex) 115435732d01SWei Wang { 1155b2427e67SColin Ian King struct net *net; 115681eb8447SWei Wang 115735732d01SWei Wang if (!bucket || !rt6_ex) 115835732d01SWei Wang return; 1159b2427e67SColin Ian King 1160b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 116135732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 116235732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 116335732d01SWei Wang rt6_release(rt6_ex->rt6i); 116435732d01SWei Wang kfree_rcu(rt6_ex, rcu); 116535732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 116635732d01SWei Wang bucket->depth--; 116781eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache--; 116835732d01SWei Wang } 116935732d01SWei Wang 117035732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 117135732d01SWei Wang * Caller must hold rt6_exception_lock 117235732d01SWei Wang */ 117335732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 117435732d01SWei Wang { 117535732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 117635732d01SWei Wang 117735732d01SWei Wang if (!bucket) 117835732d01SWei Wang return; 117935732d01SWei Wang 118035732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 118135732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 118235732d01SWei Wang oldest = rt6_ex; 118335732d01SWei Wang } 118435732d01SWei Wang rt6_remove_exception(bucket, oldest); 118535732d01SWei Wang } 118635732d01SWei Wang 118735732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 118835732d01SWei Wang const struct in6_addr *src) 118935732d01SWei Wang { 119035732d01SWei Wang static u32 seed __read_mostly; 119135732d01SWei Wang u32 val; 119235732d01SWei Wang 119335732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 119435732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 119535732d01SWei Wang 119635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 119735732d01SWei Wang if (src) 119835732d01SWei Wang val = jhash(src, sizeof(*src), val); 119935732d01SWei Wang #endif 120035732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 120135732d01SWei Wang } 120235732d01SWei Wang 120335732d01SWei Wang /* Helper function to find the cached rt in the hash table 120435732d01SWei Wang * and update bucket pointer to point to the bucket for this 120535732d01SWei Wang * (daddr, saddr) pair 120635732d01SWei Wang * Caller must hold rt6_exception_lock 120735732d01SWei Wang */ 120835732d01SWei Wang static struct rt6_exception * 120935732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 121035732d01SWei Wang const struct in6_addr *daddr, 121135732d01SWei Wang const struct in6_addr *saddr) 121235732d01SWei Wang { 121335732d01SWei Wang struct rt6_exception *rt6_ex; 121435732d01SWei Wang u32 hval; 121535732d01SWei Wang 121635732d01SWei Wang if (!(*bucket) || !daddr) 121735732d01SWei Wang return NULL; 121835732d01SWei Wang 121935732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 122035732d01SWei Wang *bucket += hval; 122135732d01SWei Wang 122235732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 122335732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 122435732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 122535732d01SWei Wang 122635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 122735732d01SWei Wang if (matched && saddr) 122835732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 122935732d01SWei Wang #endif 123035732d01SWei Wang if (matched) 123135732d01SWei Wang return rt6_ex; 123235732d01SWei Wang } 123335732d01SWei Wang return NULL; 123435732d01SWei Wang } 123535732d01SWei Wang 123635732d01SWei Wang /* Helper function to find the cached rt in the hash table 123735732d01SWei Wang * and update bucket pointer to point to the bucket for this 123835732d01SWei Wang * (daddr, saddr) pair 123935732d01SWei Wang * Caller must hold rcu_read_lock() 124035732d01SWei Wang */ 124135732d01SWei Wang static struct rt6_exception * 124235732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 124335732d01SWei Wang const struct in6_addr *daddr, 124435732d01SWei Wang const struct in6_addr *saddr) 124535732d01SWei Wang { 124635732d01SWei Wang struct rt6_exception *rt6_ex; 124735732d01SWei Wang u32 hval; 124835732d01SWei Wang 124935732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 125035732d01SWei Wang 125135732d01SWei Wang if (!(*bucket) || !daddr) 125235732d01SWei Wang return NULL; 125335732d01SWei Wang 125435732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 125535732d01SWei Wang *bucket += hval; 125635732d01SWei Wang 125735732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 125835732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 125935732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 126035732d01SWei Wang 126135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 126235732d01SWei Wang if (matched && saddr) 126335732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 126435732d01SWei Wang #endif 126535732d01SWei Wang if (matched) 126635732d01SWei Wang return rt6_ex; 126735732d01SWei Wang } 126835732d01SWei Wang return NULL; 126935732d01SWei Wang } 127035732d01SWei Wang 127135732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 127235732d01SWei Wang struct rt6_info *ort) 127335732d01SWei Wang { 127481eb8447SWei Wang struct net *net = dev_net(ort->dst.dev); 127535732d01SWei Wang struct rt6_exception_bucket *bucket; 127635732d01SWei Wang struct in6_addr *src_key = NULL; 127735732d01SWei Wang struct rt6_exception *rt6_ex; 127835732d01SWei Wang int err = 0; 127935732d01SWei Wang 128035732d01SWei Wang /* ort can't be a cache or pcpu route */ 128135732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 12823a2232e9SDavid Miller ort = ort->from; 128335732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 128435732d01SWei Wang 128535732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 128635732d01SWei Wang 128735732d01SWei Wang if (ort->exception_bucket_flushed) { 128835732d01SWei Wang err = -EINVAL; 128935732d01SWei Wang goto out; 129035732d01SWei Wang } 129135732d01SWei Wang 129235732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 129335732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 129435732d01SWei Wang if (!bucket) { 129535732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 129635732d01SWei Wang GFP_ATOMIC); 129735732d01SWei Wang if (!bucket) { 129835732d01SWei Wang err = -ENOMEM; 129935732d01SWei Wang goto out; 130035732d01SWei Wang } 130135732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 130235732d01SWei Wang } 130335732d01SWei Wang 130435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 130535732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 130635732d01SWei Wang * and exception table is indexed by a hash of 130735732d01SWei Wang * both rt6i_dst and rt6i_src. 130835732d01SWei Wang * Otherwise, the exception table is indexed by 130935732d01SWei Wang * a hash of only rt6i_dst. 131035732d01SWei Wang */ 131135732d01SWei Wang if (ort->rt6i_src.plen) 131235732d01SWei Wang src_key = &nrt->rt6i_src.addr; 131335732d01SWei Wang #endif 131460006a48SWei Wang 131560006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 131660006a48SWei Wang * in rt6_remove_prefsrc() 131760006a48SWei Wang */ 131860006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1319f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1320f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1321f5bbe7eeSWei Wang * is less than ort's mtu value. 1322f5bbe7eeSWei Wang */ 1323f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1324f5bbe7eeSWei Wang err = -EINVAL; 1325f5bbe7eeSWei Wang goto out; 1326f5bbe7eeSWei Wang } 132760006a48SWei Wang 132835732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 132935732d01SWei Wang src_key); 133035732d01SWei Wang if (rt6_ex) 133135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 133235732d01SWei Wang 133335732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 133435732d01SWei Wang if (!rt6_ex) { 133535732d01SWei Wang err = -ENOMEM; 133635732d01SWei Wang goto out; 133735732d01SWei Wang } 133835732d01SWei Wang rt6_ex->rt6i = nrt; 133935732d01SWei Wang rt6_ex->stamp = jiffies; 134035732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 134135732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 134235732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 134335732d01SWei Wang bucket->depth++; 134481eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 134535732d01SWei Wang 134635732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 134735732d01SWei Wang rt6_exception_remove_oldest(bucket); 134835732d01SWei Wang 134935732d01SWei Wang out: 135035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 135135732d01SWei Wang 135235732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1353b886d5f2SPaolo Abeni if (!err) { 1354922c2ac8SIdo Schimmel spin_lock_bh(&ort->rt6i_table->tb6_lock); 135535732d01SWei Wang fib6_update_sernum(ort); 1356922c2ac8SIdo Schimmel spin_unlock_bh(&ort->rt6i_table->tb6_lock); 1357b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1358b886d5f2SPaolo Abeni } 135935732d01SWei Wang 136035732d01SWei Wang return err; 136135732d01SWei Wang } 136235732d01SWei Wang 136335732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 136435732d01SWei Wang { 136535732d01SWei Wang struct rt6_exception_bucket *bucket; 136635732d01SWei Wang struct rt6_exception *rt6_ex; 136735732d01SWei Wang struct hlist_node *tmp; 136835732d01SWei Wang int i; 136935732d01SWei Wang 137035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 137135732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 137235732d01SWei Wang rt->exception_bucket_flushed = 1; 137335732d01SWei Wang 137435732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 137535732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 137635732d01SWei Wang if (!bucket) 137735732d01SWei Wang goto out; 137835732d01SWei Wang 137935732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 138035732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 138135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 138235732d01SWei Wang WARN_ON_ONCE(bucket->depth); 138335732d01SWei Wang bucket++; 138435732d01SWei Wang } 138535732d01SWei Wang 138635732d01SWei Wang out: 138735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 138835732d01SWei Wang } 138935732d01SWei Wang 139035732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 139135732d01SWei Wang * Caller has to hold rcu_read_lock() 139235732d01SWei Wang */ 139335732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 139435732d01SWei Wang struct in6_addr *daddr, 139535732d01SWei Wang struct in6_addr *saddr) 139635732d01SWei Wang { 139735732d01SWei Wang struct rt6_exception_bucket *bucket; 139835732d01SWei Wang struct in6_addr *src_key = NULL; 139935732d01SWei Wang struct rt6_exception *rt6_ex; 140035732d01SWei Wang struct rt6_info *res = NULL; 140135732d01SWei Wang 140235732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 140335732d01SWei Wang 140435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 140535732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 140635732d01SWei Wang * and exception table is indexed by a hash of 140735732d01SWei Wang * both rt6i_dst and rt6i_src. 140835732d01SWei Wang * Otherwise, the exception table is indexed by 140935732d01SWei Wang * a hash of only rt6i_dst. 141035732d01SWei Wang */ 141135732d01SWei Wang if (rt->rt6i_src.plen) 141235732d01SWei Wang src_key = saddr; 141335732d01SWei Wang #endif 141435732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 141535732d01SWei Wang 141635732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 141735732d01SWei Wang res = rt6_ex->rt6i; 141835732d01SWei Wang 141935732d01SWei Wang return res; 142035732d01SWei Wang } 142135732d01SWei Wang 142235732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 142335732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 142435732d01SWei Wang { 142535732d01SWei Wang struct rt6_exception_bucket *bucket; 14263a2232e9SDavid Miller struct rt6_info *from = rt->from; 142735732d01SWei Wang struct in6_addr *src_key = NULL; 142835732d01SWei Wang struct rt6_exception *rt6_ex; 142935732d01SWei Wang int err; 143035732d01SWei Wang 143135732d01SWei Wang if (!from || 1432442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 143335732d01SWei Wang return -EINVAL; 143435732d01SWei Wang 143535732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 143635732d01SWei Wang return -ENOENT; 143735732d01SWei Wang 143835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 143935732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 144035732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 144135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 144235732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 144335732d01SWei Wang * and exception table is indexed by a hash of 144435732d01SWei Wang * both rt6i_dst and rt6i_src. 144535732d01SWei Wang * Otherwise, the exception table is indexed by 144635732d01SWei Wang * a hash of only rt6i_dst. 144735732d01SWei Wang */ 144835732d01SWei Wang if (from->rt6i_src.plen) 144935732d01SWei Wang src_key = &rt->rt6i_src.addr; 145035732d01SWei Wang #endif 145135732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 145235732d01SWei Wang &rt->rt6i_dst.addr, 145335732d01SWei Wang src_key); 145435732d01SWei Wang if (rt6_ex) { 145535732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 145635732d01SWei Wang err = 0; 145735732d01SWei Wang } else { 145835732d01SWei Wang err = -ENOENT; 145935732d01SWei Wang } 146035732d01SWei Wang 146135732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 146235732d01SWei Wang return err; 146335732d01SWei Wang } 146435732d01SWei Wang 146535732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 146635732d01SWei Wang * refresh its stamp 146735732d01SWei Wang */ 146835732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 146935732d01SWei Wang { 147035732d01SWei Wang struct rt6_exception_bucket *bucket; 14713a2232e9SDavid Miller struct rt6_info *from = rt->from; 147235732d01SWei Wang struct in6_addr *src_key = NULL; 147335732d01SWei Wang struct rt6_exception *rt6_ex; 147435732d01SWei Wang 147535732d01SWei Wang if (!from || 1476442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 147735732d01SWei Wang return; 147835732d01SWei Wang 147935732d01SWei Wang rcu_read_lock(); 148035732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 148135732d01SWei Wang 148235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 148335732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 148435732d01SWei Wang * and exception table is indexed by a hash of 148535732d01SWei Wang * both rt6i_dst and rt6i_src. 148635732d01SWei Wang * Otherwise, the exception table is indexed by 148735732d01SWei Wang * a hash of only rt6i_dst. 148835732d01SWei Wang */ 148935732d01SWei Wang if (from->rt6i_src.plen) 149035732d01SWei Wang src_key = &rt->rt6i_src.addr; 149135732d01SWei Wang #endif 149235732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 149335732d01SWei Wang &rt->rt6i_dst.addr, 149435732d01SWei Wang src_key); 149535732d01SWei Wang if (rt6_ex) 149635732d01SWei Wang rt6_ex->stamp = jiffies; 149735732d01SWei Wang 149835732d01SWei Wang rcu_read_unlock(); 149935732d01SWei Wang } 150035732d01SWei Wang 150160006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 150260006a48SWei Wang { 150360006a48SWei Wang struct rt6_exception_bucket *bucket; 150460006a48SWei Wang struct rt6_exception *rt6_ex; 150560006a48SWei Wang int i; 150660006a48SWei Wang 150760006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 150860006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 150960006a48SWei Wang 151060006a48SWei Wang if (bucket) { 151160006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 151260006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 151360006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 151460006a48SWei Wang } 151560006a48SWei Wang bucket++; 151660006a48SWei Wang } 151760006a48SWei Wang } 151860006a48SWei Wang } 151960006a48SWei Wang 1520e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1521e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1522e9fa1495SStefano Brivio { 1523e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1524e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1525e9fa1495SStefano Brivio * reflect PMTU decreases. 1526e9fa1495SStefano Brivio * 1527e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1528e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1529e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1530e9fa1495SStefano Brivio * handle this. 1531e9fa1495SStefano Brivio */ 1532e9fa1495SStefano Brivio 1533e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1534e9fa1495SStefano Brivio return true; 1535e9fa1495SStefano Brivio 1536e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1537e9fa1495SStefano Brivio return true; 1538e9fa1495SStefano Brivio 1539e9fa1495SStefano Brivio return false; 1540e9fa1495SStefano Brivio } 1541e9fa1495SStefano Brivio 1542e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 1543e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1544f5bbe7eeSWei Wang { 1545f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1546f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1547f5bbe7eeSWei Wang int i; 1548f5bbe7eeSWei Wang 1549f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1550f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1551f5bbe7eeSWei Wang 1552e9fa1495SStefano Brivio if (!bucket) 1553e9fa1495SStefano Brivio return; 1554e9fa1495SStefano Brivio 1555f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1556f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1557f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1558e9fa1495SStefano Brivio 1559e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 1560e9fa1495SStefano Brivio * route), the metrics of its rt->dst.from have already 1561f5bbe7eeSWei Wang * been updated. 1562f5bbe7eeSWei Wang */ 1563e9fa1495SStefano Brivio if (entry->rt6i_pmtu && 1564e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 1565f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1566f5bbe7eeSWei Wang } 1567f5bbe7eeSWei Wang bucket++; 1568f5bbe7eeSWei Wang } 1569f5bbe7eeSWei Wang } 1570f5bbe7eeSWei Wang 1571b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1572b16cb459SWei Wang 1573b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1574b16cb459SWei Wang struct in6_addr *gateway) 1575b16cb459SWei Wang { 1576b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1577b16cb459SWei Wang struct rt6_exception *rt6_ex; 1578b16cb459SWei Wang struct hlist_node *tmp; 1579b16cb459SWei Wang int i; 1580b16cb459SWei Wang 1581b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1582b16cb459SWei Wang return; 1583b16cb459SWei Wang 1584b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1585b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1586b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1587b16cb459SWei Wang 1588b16cb459SWei Wang if (bucket) { 1589b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1590b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1591b16cb459SWei Wang &bucket->chain, hlist) { 1592b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1593b16cb459SWei Wang 1594b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1595b16cb459SWei Wang RTF_CACHE_GATEWAY && 1596b16cb459SWei Wang ipv6_addr_equal(gateway, 1597b16cb459SWei Wang &entry->rt6i_gateway)) { 1598b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1599b16cb459SWei Wang } 1600b16cb459SWei Wang } 1601b16cb459SWei Wang bucket++; 1602b16cb459SWei Wang } 1603b16cb459SWei Wang } 1604b16cb459SWei Wang 1605b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1606b16cb459SWei Wang } 1607b16cb459SWei Wang 1608c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1609c757faa8SWei Wang struct rt6_exception *rt6_ex, 1610c757faa8SWei Wang struct fib6_gc_args *gc_args, 1611c757faa8SWei Wang unsigned long now) 1612c757faa8SWei Wang { 1613c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1614c757faa8SWei Wang 16151859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 16161859bac0SPaolo Abeni * even if others have still references to them, so that on next 16171859bac0SPaolo Abeni * dst_check() such references can be dropped. 16181859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 16191859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 16201859bac0SPaolo Abeni */ 162131afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 162231afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1623c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1624c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1625c757faa8SWei Wang return; 162631afeb42SWei Wang } 162731afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 162831afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 162931afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 163031afeb42SWei Wang return; 163131afeb42SWei Wang } 163231afeb42SWei Wang 163331afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 1634c757faa8SWei Wang struct neighbour *neigh; 1635c757faa8SWei Wang __u8 neigh_flags = 0; 1636c757faa8SWei Wang 16371bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 16381bfa26ffSEric Dumazet if (neigh) 1639c757faa8SWei Wang neigh_flags = neigh->flags; 16401bfa26ffSEric Dumazet 1641c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1642c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1643c757faa8SWei Wang rt); 1644c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1645c757faa8SWei Wang return; 1646c757faa8SWei Wang } 1647c757faa8SWei Wang } 164831afeb42SWei Wang 1649c757faa8SWei Wang gc_args->more++; 1650c757faa8SWei Wang } 1651c757faa8SWei Wang 1652c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1653c757faa8SWei Wang struct fib6_gc_args *gc_args, 1654c757faa8SWei Wang unsigned long now) 1655c757faa8SWei Wang { 1656c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1657c757faa8SWei Wang struct rt6_exception *rt6_ex; 1658c757faa8SWei Wang struct hlist_node *tmp; 1659c757faa8SWei Wang int i; 1660c757faa8SWei Wang 1661c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1662c757faa8SWei Wang return; 1663c757faa8SWei Wang 16641bfa26ffSEric Dumazet rcu_read_lock_bh(); 16651bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 1666c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1667c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1668c757faa8SWei Wang 1669c757faa8SWei Wang if (bucket) { 1670c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1671c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1672c757faa8SWei Wang &bucket->chain, hlist) { 1673c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1674c757faa8SWei Wang gc_args, now); 1675c757faa8SWei Wang } 1676c757faa8SWei Wang bucket++; 1677c757faa8SWei Wang } 1678c757faa8SWei Wang } 16791bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 16801bfa26ffSEric Dumazet rcu_read_unlock_bh(); 1681c757faa8SWei Wang } 1682c757faa8SWei Wang 16839ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 1684b75cc8f9SDavid Ahern int oif, struct flowi6 *fl6, 1685b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 16861da177e4SLinus Torvalds { 1687367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 16882b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 1689c71099acSThomas Graf int strict = 0; 16901da177e4SLinus Torvalds 169177d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1692d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1693367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1694367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16951da177e4SLinus Torvalds 169666f5d6ceSWei Wang rcu_read_lock(); 16971da177e4SLinus Torvalds 16984c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1699367efcb9SMartin KaFai Lau saved_fn = fn; 17001da177e4SLinus Torvalds 1701ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1702ca254490SDavid Ahern oif = 0; 1703ca254490SDavid Ahern 1704a3c00e46SMartin KaFai Lau redo_rt6_select: 17058d1040e8SWei Wang rt = rt6_select(net, fn, oif, strict); 170652bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1707b4bac172SDavid Ahern rt = rt6_multipath_select(net, rt, fl6, oif, skb, strict); 1708a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1709a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1710a3c00e46SMartin KaFai Lau if (fn) 1711a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1712367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1713367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1714367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1715367efcb9SMartin KaFai Lau fn = saved_fn; 1716367efcb9SMartin KaFai Lau goto redo_rt6_select; 1717367efcb9SMartin KaFai Lau } 1718a3c00e46SMartin KaFai Lau } 1719a3c00e46SMartin KaFai Lau 17202b760fcfSWei Wang /*Search through exception table */ 17212b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 17222b760fcfSWei Wang if (rt_cache) 17232b760fcfSWei Wang rt = rt_cache; 1724d52d3997SMartin KaFai Lau 1725d3843fe5SWei Wang if (rt == net->ipv6.ip6_null_entry) { 172666f5d6ceSWei Wang rcu_read_unlock(); 1727d3843fe5SWei Wang dst_hold(&rt->dst); 1728b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1729d3843fe5SWei Wang return rt; 1730d3843fe5SWei Wang } else if (rt->rt6i_flags & RTF_CACHE) { 1731d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1732d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1733d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1734d3843fe5SWei Wang } 173566f5d6ceSWei Wang rcu_read_unlock(); 1736b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1737d52d3997SMartin KaFai Lau return rt; 17383da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 17393da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 17403da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 17413da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 17423da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 17433da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 17443da59bd9SMartin KaFai Lau */ 1745c71099acSThomas Graf 17463da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 17473da59bd9SMartin KaFai Lau 1748d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1749d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1750d3843fe5SWei Wang } else { 175166f5d6ceSWei Wang rcu_read_unlock(); 1752d3843fe5SWei Wang uncached_rt = rt; 1753d3843fe5SWei Wang goto uncached_rt_out; 1754d3843fe5SWei Wang } 175566f5d6ceSWei Wang rcu_read_unlock(); 1756d52d3997SMartin KaFai Lau 17573da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 17583da59bd9SMartin KaFai Lau dst_release(&rt->dst); 17593da59bd9SMartin KaFai Lau 17601cfb71eeSWei Wang if (uncached_rt) { 17611cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 17621cfb71eeSWei Wang * No need for another dst_hold() 17631cfb71eeSWei Wang */ 17648d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 176581eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 17661cfb71eeSWei Wang } else { 17673da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 17683da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 17691cfb71eeSWei Wang } 1770b811580dSDavid Ahern 1771d3843fe5SWei Wang uncached_rt_out: 1772b65f164dSPaolo Abeni trace_fib6_table_lookup(net, uncached_rt, table, fl6); 17733da59bd9SMartin KaFai Lau return uncached_rt; 17743da59bd9SMartin KaFai Lau 1775d52d3997SMartin KaFai Lau } else { 1776d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1777d52d3997SMartin KaFai Lau 1778d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1779d52d3997SMartin KaFai Lau 1780d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1781951f788aSEric Dumazet local_bh_disable(); 1782d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1783d52d3997SMartin KaFai Lau 1784951f788aSEric Dumazet if (!pcpu_rt) { 1785a94b9367SWei Wang /* atomic_inc_not_zero() is needed when using rcu */ 1786a94b9367SWei Wang if (atomic_inc_not_zero(&rt->rt6i_ref)) { 1787951f788aSEric Dumazet /* No dst_hold() on rt is needed because grabbing 1788a94b9367SWei Wang * rt->rt6i_ref makes sure rt can't be released. 17899c7370a1SMartin KaFai Lau */ 17909c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 1791a94b9367SWei Wang rt6_release(rt); 1792a94b9367SWei Wang } else { 1793a94b9367SWei Wang /* rt is already removed from tree */ 1794a94b9367SWei Wang pcpu_rt = net->ipv6.ip6_null_entry; 1795a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1796a94b9367SWei Wang } 17979c7370a1SMartin KaFai Lau } 1798951f788aSEric Dumazet local_bh_enable(); 1799951f788aSEric Dumazet rcu_read_unlock(); 1800b65f164dSPaolo Abeni trace_fib6_table_lookup(net, pcpu_rt, table, fl6); 1801d52d3997SMartin KaFai Lau return pcpu_rt; 1802d52d3997SMartin KaFai Lau } 1803c71099acSThomas Graf } 18049ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1805c71099acSThomas Graf 1806b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 1807b75cc8f9SDavid Ahern struct fib6_table *table, 1808b75cc8f9SDavid Ahern struct flowi6 *fl6, 1809b75cc8f9SDavid Ahern const struct sk_buff *skb, 1810b75cc8f9SDavid Ahern int flags) 18114acad72dSPavel Emelyanov { 1812b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 18134acad72dSPavel Emelyanov } 18144acad72dSPavel Emelyanov 1815d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 181672331bc0SShmulik Ladkani struct net_device *dev, 1817b75cc8f9SDavid Ahern struct flowi6 *fl6, 1818b75cc8f9SDavid Ahern const struct sk_buff *skb, 1819b75cc8f9SDavid Ahern int flags) 182072331bc0SShmulik Ladkani { 182172331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 182272331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 182372331bc0SShmulik Ladkani 1824b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 182572331bc0SShmulik Ladkani } 1826d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 182772331bc0SShmulik Ladkani 182823aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 18295e5d6fedSRoopa Prabhu struct flow_keys *keys, 18305e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 183123aebdacSJakub Sitnicki { 183223aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 183323aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 18345e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 183523aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 183623aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 183723aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 1838cea67a2dSEric Dumazet struct icmp6hdr _icmph; 183923aebdacSJakub Sitnicki 184023aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 184123aebdacSJakub Sitnicki goto out; 184223aebdacSJakub Sitnicki 1843cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 1844cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 1845cea67a2dSEric Dumazet if (!icmph) 1846cea67a2dSEric Dumazet goto out; 1847cea67a2dSEric Dumazet 184823aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 184923aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 185023aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 185123aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 185223aebdacSJakub Sitnicki goto out; 185323aebdacSJakub Sitnicki 185423aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 185523aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 185623aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 185723aebdacSJakub Sitnicki if (!inner_iph) 185823aebdacSJakub Sitnicki goto out; 185923aebdacSJakub Sitnicki 186023aebdacSJakub Sitnicki key_iph = inner_iph; 18615e5d6fedSRoopa Prabhu _flkeys = NULL; 186223aebdacSJakub Sitnicki out: 18635e5d6fedSRoopa Prabhu if (_flkeys) { 18645e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 18655e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 18665e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 18675e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 18685e5d6fedSRoopa Prabhu } else { 186923aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 187023aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 187123aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 187223aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 187323aebdacSJakub Sitnicki } 18745e5d6fedSRoopa Prabhu } 187523aebdacSJakub Sitnicki 187623aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 1877b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 1878b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 187923aebdacSJakub Sitnicki { 188023aebdacSJakub Sitnicki struct flow_keys hash_keys; 18819a2a537aSDavid Ahern u32 mhash; 188223aebdacSJakub Sitnicki 1883bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 1884b4bac172SDavid Ahern case 0: 18856f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 18866f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 18879a2a537aSDavid Ahern if (skb) { 18885e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 18899a2a537aSDavid Ahern } else { 18909a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 18919a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 18929a2a537aSDavid Ahern hash_keys.tags.flow_label = (__force u32)fl6->flowlabel; 18939a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 189423aebdacSJakub Sitnicki } 1895b4bac172SDavid Ahern break; 1896b4bac172SDavid Ahern case 1: 1897b4bac172SDavid Ahern if (skb) { 1898b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 1899b4bac172SDavid Ahern struct flow_keys keys; 1900b4bac172SDavid Ahern 1901b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 1902b4bac172SDavid Ahern if (skb->l4_hash) 1903b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 1904b4bac172SDavid Ahern 1905b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 1906b4bac172SDavid Ahern 1907b4bac172SDavid Ahern if (!flkeys) { 1908b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 1909b4bac172SDavid Ahern flkeys = &keys; 1910b4bac172SDavid Ahern } 1911b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1912b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 1913b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 1914b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 1915b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 1916b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 1917b4bac172SDavid Ahern } else { 1918b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 1919b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1920b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 1921b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 1922b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 1923b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 1924b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 1925b4bac172SDavid Ahern } 1926b4bac172SDavid Ahern break; 1927b4bac172SDavid Ahern } 19289a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 192923aebdacSJakub Sitnicki 19309a2a537aSDavid Ahern return mhash >> 1; 193123aebdacSJakub Sitnicki } 193223aebdacSJakub Sitnicki 1933c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1934c71099acSThomas Graf { 1935b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1936c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1937adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1938904af04dSJiri Benc struct ip_tunnel_info *tun_info; 19394c9483b2SDavid S. Miller struct flowi6 fl6 = { 1940e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 19414c9483b2SDavid S. Miller .daddr = iph->daddr, 19424c9483b2SDavid S. Miller .saddr = iph->saddr, 19436502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 19444c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 19454c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1946c71099acSThomas Graf }; 19475e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 1948adaa70bbSThomas Graf 1949904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 195046fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1951904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 19525e5d6fedSRoopa Prabhu 19535e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 19545e5d6fedSRoopa Prabhu flkeys = &_flkeys; 19555e5d6fedSRoopa Prabhu 195623aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 1957b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 195806e9d040SJiri Benc skb_dst_drop(skb); 1959b75cc8f9SDavid Ahern skb_dst_set(skb, 1960b75cc8f9SDavid Ahern ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); 1961c71099acSThomas Graf } 1962c71099acSThomas Graf 1963b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 1964b75cc8f9SDavid Ahern struct fib6_table *table, 1965b75cc8f9SDavid Ahern struct flowi6 *fl6, 1966b75cc8f9SDavid Ahern const struct sk_buff *skb, 1967b75cc8f9SDavid Ahern int flags) 1968c71099acSThomas Graf { 1969b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 1970c71099acSThomas Graf } 1971c71099acSThomas Graf 19726f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 19736f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1974c71099acSThomas Graf { 1975d46a9d67SDavid Ahern bool any_src; 1976c71099acSThomas Graf 19774c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 19784c1feac5SDavid Ahern struct dst_entry *dst; 19794c1feac5SDavid Ahern 19804c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1981ca254490SDavid Ahern if (dst) 1982ca254490SDavid Ahern return dst; 19834c1feac5SDavid Ahern } 1984ca254490SDavid Ahern 19851fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 19864dc27d1cSDavid McCullough 1987d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1988741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1989d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 199077d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1991c71099acSThomas Graf 1992d46a9d67SDavid Ahern if (!any_src) 1993adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 19940c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 19950c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1996adaa70bbSThomas Graf 1997b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 19981da177e4SLinus Torvalds } 19996f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 20001da177e4SLinus Torvalds 20012774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 200214e50e57SDavid S. Miller { 20035c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 20041dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 200514e50e57SDavid S. Miller struct dst_entry *new = NULL; 200614e50e57SDavid S. Miller 20071dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 200862cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 200914e50e57SDavid S. Miller if (rt) { 20100a1f5962SMartin KaFai Lau rt6_info_init(rt); 201181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 20120a1f5962SMartin KaFai Lau 2013d8d1f30bSChangli Gao new = &rt->dst; 201414e50e57SDavid S. Miller new->__use = 1; 2015352e512cSHerbert Xu new->input = dst_discard; 2016ede2059dSEric W. Biederman new->output = dst_discard_out; 201714e50e57SDavid S. Miller 2018defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 201914e50e57SDavid S. Miller 20201dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 20214e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 20220a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 202314e50e57SDavid S. Miller rt->rt6i_metric = 0; 202414e50e57SDavid S. Miller 202514e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 202614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 202714e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 202814e50e57SDavid S. Miller #endif 202914e50e57SDavid S. Miller } 203014e50e57SDavid S. Miller 203169ead7afSDavid S. Miller dst_release(dst_orig); 203269ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 203314e50e57SDavid S. Miller } 203414e50e57SDavid S. Miller 20351da177e4SLinus Torvalds /* 20361da177e4SLinus Torvalds * Destination cache support functions 20371da177e4SLinus Torvalds */ 20381da177e4SLinus Torvalds 20394b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 20404b32b5adSMartin KaFai Lau { 20413a2232e9SDavid Miller if (rt->from && 20423a2232e9SDavid Miller dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst)) 20433a2232e9SDavid Miller dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true); 20444b32b5adSMartin KaFai Lau } 20454b32b5adSMartin KaFai Lau 20463da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 20473da59bd9SMartin KaFai Lau { 204836143645SSteffen Klassert u32 rt_cookie = 0; 2049c5cff856SWei Wang 2050c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 20513da59bd9SMartin KaFai Lau return NULL; 20523da59bd9SMartin KaFai Lau 20533da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 20543da59bd9SMartin KaFai Lau return NULL; 20553da59bd9SMartin KaFai Lau 20563da59bd9SMartin KaFai Lau return &rt->dst; 20573da59bd9SMartin KaFai Lau } 20583da59bd9SMartin KaFai Lau 20593da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 20603da59bd9SMartin KaFai Lau { 20615973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 20625973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 20633a2232e9SDavid Miller rt6_check(rt->from, cookie)) 20643da59bd9SMartin KaFai Lau return &rt->dst; 20653da59bd9SMartin KaFai Lau else 20663da59bd9SMartin KaFai Lau return NULL; 20673da59bd9SMartin KaFai Lau } 20683da59bd9SMartin KaFai Lau 20691da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 20701da177e4SLinus Torvalds { 20711da177e4SLinus Torvalds struct rt6_info *rt; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 20741da177e4SLinus Torvalds 20756f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 20766f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 20776f3118b5SNicolas Dichtel * into this function always. 20786f3118b5SNicolas Dichtel */ 2079e3bc10bdSHannes Frederic Sowa 20804b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 20814b32b5adSMartin KaFai Lau 208202bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 20833a2232e9SDavid Miller (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) 20843da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 20853da59bd9SMartin KaFai Lau else 20863da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 20901da177e4SLinus Torvalds { 20911da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds if (rt) { 209454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 209554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 2096e0a1ad73SThomas Graf ip6_del_rt(rt); 209754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 20981da177e4SLinus Torvalds } 209954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 210054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 210154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 210254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 210354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 210454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 21081da177e4SLinus Torvalds { 21091da177e4SLinus Torvalds struct rt6_info *rt; 21101da177e4SLinus Torvalds 21113ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 21121da177e4SLinus Torvalds 2113adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 21141da177e4SLinus Torvalds if (rt) { 21151eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 2116ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 21178e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 2118c5cff856SWei Wang } else { 2119c5cff856SWei Wang struct fib6_node *fn; 2120c5cff856SWei Wang 2121c5cff856SWei Wang rcu_read_lock(); 2122c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 2123c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2124c5cff856SWei Wang fn->fn_sernum = -1; 2125c5cff856SWei Wang rcu_read_unlock(); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds } 21281eb4f758SHannes Frederic Sowa } 21291da177e4SLinus Torvalds 213045e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 213145e4fd26SMartin KaFai Lau { 213245e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 213345e4fd26SMartin KaFai Lau 213445e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 213545e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 213645e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 213745e4fd26SMartin KaFai Lau } 213845e4fd26SMartin KaFai Lau 21390d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 21400d3f6d29SMartin KaFai Lau { 21410d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 21424e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 21434e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 21440d3f6d29SMartin KaFai Lau } 21450d3f6d29SMartin KaFai Lau 214645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 214745e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 21481da177e4SLinus Torvalds { 21490dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 21501da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 21511da177e4SLinus Torvalds 215245e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 215345e4fd26SMartin KaFai Lau return; 215445e4fd26SMartin KaFai Lau 215519bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 215619bda36cSXin Long return; 215719bda36cSXin Long 215845e4fd26SMartin KaFai Lau if (iph) { 215945e4fd26SMartin KaFai Lau daddr = &iph->daddr; 216045e4fd26SMartin KaFai Lau saddr = &iph->saddr; 216145e4fd26SMartin KaFai Lau } else if (sk) { 216245e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 216345e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 216445e4fd26SMartin KaFai Lau } else { 21650dec879fSJulian Anastasov daddr = NULL; 21660dec879fSJulian Anastasov saddr = NULL; 21671da177e4SLinus Torvalds } 21680dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 21690dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 21700dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 21710dec879fSJulian Anastasov return; 21720dec879fSJulian Anastasov 21730dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 21740dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 21752b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 21762b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 21772b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 21780dec879fSJulian Anastasov } else if (daddr) { 21790dec879fSJulian Anastasov struct rt6_info *nrt6; 21800dec879fSJulian Anastasov 218145e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 218245e4fd26SMartin KaFai Lau if (nrt6) { 218345e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 21842b760fcfSWei Wang if (rt6_insert_exception(nrt6, rt6)) 21852b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 218645e4fd26SMartin KaFai Lau } 218745e4fd26SMartin KaFai Lau } 218845e4fd26SMartin KaFai Lau } 218945e4fd26SMartin KaFai Lau 219045e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 219145e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 219245e4fd26SMartin KaFai Lau { 219345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 219642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2197e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 219881aded24SDavid S. Miller { 219981aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 220081aded24SDavid S. Miller struct dst_entry *dst; 220181aded24SDavid S. Miller struct flowi6 fl6; 220281aded24SDavid S. Miller 220381aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 220481aded24SDavid S. Miller fl6.flowi6_oif = oif; 22051b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 220681aded24SDavid S. Miller fl6.daddr = iph->daddr; 220781aded24SDavid S. Miller fl6.saddr = iph->saddr; 22086502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2209e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 221081aded24SDavid S. Miller 221181aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 221281aded24SDavid S. Miller if (!dst->error) 221345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 221481aded24SDavid S. Miller dst_release(dst); 221581aded24SDavid S. Miller } 221681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 221781aded24SDavid S. Miller 221881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 221981aded24SDavid S. Miller { 222033c162a9SMartin KaFai Lau struct dst_entry *dst; 222133c162a9SMartin KaFai Lau 222281aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2223e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 222433c162a9SMartin KaFai Lau 222533c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 222633c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 222733c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 222833c162a9SMartin KaFai Lau return; 222933c162a9SMartin KaFai Lau 223033c162a9SMartin KaFai Lau bh_lock_sock(sk); 223133c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 223233c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 223333c162a9SMartin KaFai Lau bh_unlock_sock(sk); 223481aded24SDavid S. Miller } 223581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 223681aded24SDavid S. Miller 22377d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 22387d6850f7SAlexey Kodanev const struct flowi6 *fl6) 22397d6850f7SAlexey Kodanev { 22407d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 22417d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 22427d6850f7SAlexey Kodanev #endif 22437d6850f7SAlexey Kodanev 22447d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 22457d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 22467d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 22477d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 22487d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 22497d6850f7SAlexey Kodanev &np->saddr : 22507d6850f7SAlexey Kodanev #endif 22517d6850f7SAlexey Kodanev NULL); 22527d6850f7SAlexey Kodanev } 22537d6850f7SAlexey Kodanev 2254b55b76b2SDuan Jiong /* Handle redirects */ 2255b55b76b2SDuan Jiong struct ip6rd_flowi { 2256b55b76b2SDuan Jiong struct flowi6 fl6; 2257b55b76b2SDuan Jiong struct in6_addr gateway; 2258b55b76b2SDuan Jiong }; 2259b55b76b2SDuan Jiong 2260b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2261b55b76b2SDuan Jiong struct fib6_table *table, 2262b55b76b2SDuan Jiong struct flowi6 *fl6, 2263b75cc8f9SDavid Ahern const struct sk_buff *skb, 2264b55b76b2SDuan Jiong int flags) 2265b55b76b2SDuan Jiong { 2266b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 22672b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2268b55b76b2SDuan Jiong struct fib6_node *fn; 2269b55b76b2SDuan Jiong 2270b55b76b2SDuan Jiong /* Get the "current" route for this destination and 227167c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2272b55b76b2SDuan Jiong * 2273b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2274b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2275b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2276b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2277b55b76b2SDuan Jiong * routes. 2278b55b76b2SDuan Jiong */ 2279b55b76b2SDuan Jiong 228066f5d6ceSWei Wang rcu_read_lock(); 2281b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2282b55b76b2SDuan Jiong restart: 228366f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 22848067bb8cSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 22858067bb8cSIdo Schimmel continue; 2286b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2287b55b76b2SDuan Jiong continue; 2288b55b76b2SDuan Jiong if (rt->dst.error) 2289b55b76b2SDuan Jiong break; 2290b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2291b55b76b2SDuan Jiong continue; 2292b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2293b55b76b2SDuan Jiong continue; 22942b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 22952b760fcfSWei Wang * in the case of an ip redirect. 22962b760fcfSWei Wang * So we keep searching in the exception table if the gateway 22972b760fcfSWei Wang * is different. 22982b760fcfSWei Wang */ 22992b760fcfSWei Wang if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) { 23002b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 23012b760fcfSWei Wang &fl6->daddr, 23022b760fcfSWei Wang &fl6->saddr); 23032b760fcfSWei Wang if (rt_cache && 23042b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 23052b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 23062b760fcfSWei Wang rt = rt_cache; 23072b760fcfSWei Wang break; 23082b760fcfSWei Wang } 2309b55b76b2SDuan Jiong continue; 23102b760fcfSWei Wang } 2311b55b76b2SDuan Jiong break; 2312b55b76b2SDuan Jiong } 2313b55b76b2SDuan Jiong 2314b55b76b2SDuan Jiong if (!rt) 2315b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2316b55b76b2SDuan Jiong else if (rt->dst.error) { 2317b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2318b0a1ba59SMartin KaFai Lau goto out; 2319b0a1ba59SMartin KaFai Lau } 2320b0a1ba59SMartin KaFai Lau 2321b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2322a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2323a3c00e46SMartin KaFai Lau if (fn) 2324a3c00e46SMartin KaFai Lau goto restart; 2325b55b76b2SDuan Jiong } 2326a3c00e46SMartin KaFai Lau 2327b0a1ba59SMartin KaFai Lau out: 2328d3843fe5SWei Wang ip6_hold_safe(net, &rt, true); 2329b55b76b2SDuan Jiong 233066f5d6ceSWei Wang rcu_read_unlock(); 2331b55b76b2SDuan Jiong 2332b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 2333b55b76b2SDuan Jiong return rt; 2334b55b76b2SDuan Jiong }; 2335b55b76b2SDuan Jiong 2336b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2337b55b76b2SDuan Jiong const struct flowi6 *fl6, 2338b75cc8f9SDavid Ahern const struct sk_buff *skb, 2339b55b76b2SDuan Jiong const struct in6_addr *gateway) 2340b55b76b2SDuan Jiong { 2341b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2342b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2343b55b76b2SDuan Jiong 2344b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2345b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2346b55b76b2SDuan Jiong 2347b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2348b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2349b55b76b2SDuan Jiong } 2350b55b76b2SDuan Jiong 2351e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2352e2d118a1SLorenzo Colitti kuid_t uid) 23533a5ad2eeSDavid S. Miller { 23543a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 23553a5ad2eeSDavid S. Miller struct dst_entry *dst; 23563a5ad2eeSDavid S. Miller struct flowi6 fl6; 23573a5ad2eeSDavid S. Miller 23583a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2359e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 23603a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 23613a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 23623a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 23633a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 23646502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2365e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 23663a5ad2eeSDavid S. Miller 2367b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 23686700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 23693a5ad2eeSDavid S. Miller dst_release(dst); 23703a5ad2eeSDavid S. Miller } 23713a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 23723a5ad2eeSDavid S. Miller 2373c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2374c92a59ecSDuan Jiong u32 mark) 2375c92a59ecSDuan Jiong { 2376c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2377c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2378c92a59ecSDuan Jiong struct dst_entry *dst; 2379c92a59ecSDuan Jiong struct flowi6 fl6; 2380c92a59ecSDuan Jiong 2381c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2382e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2383c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2384c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2385c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2386c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2387e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2388c92a59ecSDuan Jiong 2389b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 2390c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2391c92a59ecSDuan Jiong dst_release(dst); 2392c92a59ecSDuan Jiong } 2393c92a59ecSDuan Jiong 23943a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 23953a5ad2eeSDavid S. Miller { 2396e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2397e2d118a1SLorenzo Colitti sk->sk_uid); 23983a5ad2eeSDavid S. Miller } 23993a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 24003a5ad2eeSDavid S. Miller 24010dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 24021da177e4SLinus Torvalds { 24030dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 24040dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 24050dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 24060dbaee3bSDavid S. Miller 24071da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 24081da177e4SLinus Torvalds 24095578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 24105578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds /* 24131da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 24141da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 24151da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 24161da177e4SLinus Torvalds * rely only on pmtu discovery" 24171da177e4SLinus Torvalds */ 24181da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 24191da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 24201da177e4SLinus Torvalds return mtu; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 2423ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2424d33e4553SDavid S. Miller { 24254b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 24264b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2427d33e4553SDavid S. Miller struct inet6_dev *idev; 2428618f9bc7SSteffen Klassert 2429618f9bc7SSteffen Klassert if (mtu) 243030f78d8eSEric Dumazet goto out; 2431618f9bc7SSteffen Klassert 24324b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 24334b32b5adSMartin KaFai Lau if (mtu) 24344b32b5adSMartin KaFai Lau goto out; 24354b32b5adSMartin KaFai Lau 2436618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2437d33e4553SDavid S. Miller 2438d33e4553SDavid S. Miller rcu_read_lock(); 2439d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2440d33e4553SDavid S. Miller if (idev) 2441d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2442d33e4553SDavid S. Miller rcu_read_unlock(); 2443d33e4553SDavid S. Miller 244430f78d8eSEric Dumazet out: 244514972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 244614972cbdSRoopa Prabhu 244714972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2448d33e4553SDavid S. Miller } 2449d33e4553SDavid S. Miller 24503b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 245187a11578SDavid S. Miller struct flowi6 *fl6) 24521da177e4SLinus Torvalds { 245387a11578SDavid S. Miller struct dst_entry *dst; 24541da177e4SLinus Torvalds struct rt6_info *rt; 24551da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2456c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 24571da177e4SLinus Torvalds 245838308473SDavid S. Miller if (unlikely(!idev)) 2459122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 24601da177e4SLinus Torvalds 2461ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 246238308473SDavid S. Miller if (unlikely(!rt)) { 24631da177e4SLinus Torvalds in6_dev_put(idev); 246487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 24651da177e4SLinus Torvalds goto out; 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 24688e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2469588753f1SBrendan McGrath rt->dst.input = ip6_input; 24708e2ec639SYan, Zheng rt->dst.output = ip6_output; 2471550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 247287a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 24738e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 24748e2ec639SYan, Zheng rt->rt6i_idev = idev; 247514edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 24761da177e4SLinus Torvalds 24774c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 2478587fea74SWei Wang * do proper release of the net_device 2479587fea74SWei Wang */ 2480587fea74SWei Wang rt6_uncached_list_add(rt); 248181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 24821da177e4SLinus Torvalds 248387a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 248487a11578SDavid S. Miller 24851da177e4SLinus Torvalds out: 248687a11578SDavid S. Miller return dst; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 2489569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 24901da177e4SLinus Torvalds { 249186393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 24927019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 24937019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 24947019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 24957019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 24967019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2497fc66f95cSEric Dumazet int entries; 24981da177e4SLinus Torvalds 2499fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 250049a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2501fc66f95cSEric Dumazet entries <= rt_max_size) 25021da177e4SLinus Torvalds goto out; 25031da177e4SLinus Torvalds 25046891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 250514956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2506fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2507fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 25087019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 25091da177e4SLinus Torvalds out: 25107019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2511fc66f95cSEric Dumazet return entries > rt_max_size; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 2514e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2515e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2516e715b6d3SFlorian Westphal { 25176670e152SStephen Hemminger struct net *net = cfg->fc_nlinfo.nl_net; 2518c3a8d947SDaniel Borkmann bool ecn_ca = false; 2519e715b6d3SFlorian Westphal struct nlattr *nla; 2520e715b6d3SFlorian Westphal int remaining; 2521e715b6d3SFlorian Westphal u32 *mp; 2522e715b6d3SFlorian Westphal 252363159f29SIan Morris if (!cfg->fc_mx) 2524e715b6d3SFlorian Westphal return 0; 2525e715b6d3SFlorian Westphal 2526e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2527e715b6d3SFlorian Westphal if (unlikely(!mp)) 2528e715b6d3SFlorian Westphal return -ENOMEM; 2529e715b6d3SFlorian Westphal 2530e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2531e715b6d3SFlorian Westphal int type = nla_type(nla); 2532ea697639SDaniel Borkmann u32 val; 2533ea697639SDaniel Borkmann 25341bb14807SDaniel Borkmann if (!type) 25351bb14807SDaniel Borkmann continue; 2536e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2537e715b6d3SFlorian Westphal goto err; 25381bb14807SDaniel Borkmann 2539ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2540ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2541e715b6d3SFlorian Westphal 2542ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 25436670e152SStephen Hemminger val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca); 2544ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2545ea697639SDaniel Borkmann goto err; 2546ea697639SDaniel Borkmann } else { 2547ea697639SDaniel Borkmann val = nla_get_u32(nla); 2548ea697639SDaniel Borkmann } 2549626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2550626abd59SPaolo Abeni val = 255; 2551b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2552b8d3e416SDaniel Borkmann goto err; 2553ea697639SDaniel Borkmann 2554ea697639SDaniel Borkmann mp[type - 1] = val; 2555e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2556e715b6d3SFlorian Westphal } 2557e715b6d3SFlorian Westphal 2558c3a8d947SDaniel Borkmann if (ecn_ca) { 2559c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2560c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2561c3a8d947SDaniel Borkmann } 2562e715b6d3SFlorian Westphal 2563c3a8d947SDaniel Borkmann mxc->mx = mp; 2564e715b6d3SFlorian Westphal return 0; 2565e715b6d3SFlorian Westphal err: 2566e715b6d3SFlorian Westphal kfree(mp); 2567e715b6d3SFlorian Westphal return -EINVAL; 2568e715b6d3SFlorian Westphal } 25691da177e4SLinus Torvalds 25708c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 25718c14586fSDavid Ahern struct fib6_config *cfg, 2572f4797b33SDavid Ahern const struct in6_addr *gw_addr, 2573f4797b33SDavid Ahern u32 tbid, int flags) 25748c14586fSDavid Ahern { 25758c14586fSDavid Ahern struct flowi6 fl6 = { 25768c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 25778c14586fSDavid Ahern .daddr = *gw_addr, 25788c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 25798c14586fSDavid Ahern }; 25808c14586fSDavid Ahern struct fib6_table *table; 25818c14586fSDavid Ahern struct rt6_info *rt; 25828c14586fSDavid Ahern 2583f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 25848c14586fSDavid Ahern if (!table) 25858c14586fSDavid Ahern return NULL; 25868c14586fSDavid Ahern 25878c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 25888c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 25898c14586fSDavid Ahern 2590f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 2591b75cc8f9SDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); 25928c14586fSDavid Ahern 25938c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 25948c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 25958c14586fSDavid Ahern ip6_rt_put(rt); 25968c14586fSDavid Ahern rt = NULL; 25978c14586fSDavid Ahern } 25988c14586fSDavid Ahern 25998c14586fSDavid Ahern return rt; 26008c14586fSDavid Ahern } 26018c14586fSDavid Ahern 2602fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 2603fc1e64e1SDavid Ahern struct fib6_config *cfg, 26049fbb704cSDavid Ahern const struct net_device *dev, 2605fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 2606fc1e64e1SDavid Ahern { 260744750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 2608fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 2609fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 2610fc1e64e1SDavid Ahern struct rt6_info *grt; 2611fc1e64e1SDavid Ahern int err; 2612fc1e64e1SDavid Ahern 2613fc1e64e1SDavid Ahern err = 0; 2614fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 2615fc1e64e1SDavid Ahern if (grt) { 261658e354c0SDavid Ahern if (!grt->dst.error && 261758e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 261844750f84SDavid Ahern NL_SET_ERR_MSG(extack, 261944750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 2620fc1e64e1SDavid Ahern err = -EINVAL; 2621fc1e64e1SDavid Ahern } 2622fc1e64e1SDavid Ahern 2623fc1e64e1SDavid Ahern ip6_rt_put(grt); 2624fc1e64e1SDavid Ahern } 2625fc1e64e1SDavid Ahern 2626fc1e64e1SDavid Ahern return err; 2627fc1e64e1SDavid Ahern } 2628fc1e64e1SDavid Ahern 26291edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 26301edce99fSDavid Ahern struct fib6_config *cfg, 26311edce99fSDavid Ahern struct net_device **_dev, 26321edce99fSDavid Ahern struct inet6_dev **idev) 26331edce99fSDavid Ahern { 26341edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 26351edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 26361edce99fSDavid Ahern struct rt6_info *grt = NULL; 26371edce99fSDavid Ahern int err = -EHOSTUNREACH; 26381edce99fSDavid Ahern 26391edce99fSDavid Ahern if (cfg->fc_table) { 2640f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 2641f4797b33SDavid Ahern 2642f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 2643f4797b33SDavid Ahern cfg->fc_table, flags); 26441edce99fSDavid Ahern if (grt) { 26451edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 26461edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 26471edce99fSDavid Ahern ip6_rt_put(grt); 26481edce99fSDavid Ahern grt = NULL; 26491edce99fSDavid Ahern } 26501edce99fSDavid Ahern } 26511edce99fSDavid Ahern } 26521edce99fSDavid Ahern 26531edce99fSDavid Ahern if (!grt) 2654b75cc8f9SDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); 26551edce99fSDavid Ahern 26561edce99fSDavid Ahern if (!grt) 26571edce99fSDavid Ahern goto out; 26581edce99fSDavid Ahern 26591edce99fSDavid Ahern if (dev) { 26601edce99fSDavid Ahern if (dev != grt->dst.dev) { 26611edce99fSDavid Ahern ip6_rt_put(grt); 26621edce99fSDavid Ahern goto out; 26631edce99fSDavid Ahern } 26641edce99fSDavid Ahern } else { 26651edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 26661edce99fSDavid Ahern *idev = grt->rt6i_idev; 26671edce99fSDavid Ahern dev_hold(dev); 26681edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 26691edce99fSDavid Ahern } 26701edce99fSDavid Ahern 26711edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 26721edce99fSDavid Ahern err = 0; 26731edce99fSDavid Ahern 26741edce99fSDavid Ahern ip6_rt_put(grt); 26751edce99fSDavid Ahern 26761edce99fSDavid Ahern out: 26771edce99fSDavid Ahern return err; 26781edce99fSDavid Ahern } 26791edce99fSDavid Ahern 26809fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 26819fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 26829fbb704cSDavid Ahern struct netlink_ext_ack *extack) 26839fbb704cSDavid Ahern { 26849fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 26859fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 2686232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 26879fbb704cSDavid Ahern const struct net_device *dev = *_dev; 2688232378e8SDavid Ahern bool need_addr_check = !dev; 26899fbb704cSDavid Ahern int err = -EINVAL; 26909fbb704cSDavid Ahern 26919fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 26929fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 26939fbb704cSDavid Ahern * will return already-added prefix route via interface that 26949fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 26959fbb704cSDavid Ahern */ 2696232378e8SDavid Ahern if (dev && 2697232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2698232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 26999fbb704cSDavid Ahern goto out; 27009fbb704cSDavid Ahern } 27019fbb704cSDavid Ahern 27029fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 27039fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 27049fbb704cSDavid Ahern * addresses as nexthop address. 27059fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 27069fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 27079fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 27089fbb704cSDavid Ahern * some exceptions. --ANK 27099fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 27109fbb704cSDavid Ahern * addressing 27119fbb704cSDavid Ahern */ 27129fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 27139fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 27149fbb704cSDavid Ahern goto out; 27159fbb704cSDavid Ahern } 27169fbb704cSDavid Ahern 27179fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 27189fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 27199fbb704cSDavid Ahern else 27209fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 27219fbb704cSDavid Ahern 27229fbb704cSDavid Ahern if (err) 27239fbb704cSDavid Ahern goto out; 27249fbb704cSDavid Ahern } 27259fbb704cSDavid Ahern 27269fbb704cSDavid Ahern /* reload in case device was changed */ 27279fbb704cSDavid Ahern dev = *_dev; 27289fbb704cSDavid Ahern 27299fbb704cSDavid Ahern err = -EINVAL; 27309fbb704cSDavid Ahern if (!dev) { 27319fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 27329fbb704cSDavid Ahern goto out; 27339fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 27349fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 27359fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 27369fbb704cSDavid Ahern goto out; 27379fbb704cSDavid Ahern } 2738232378e8SDavid Ahern 2739232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 2740232378e8SDavid Ahern * egress device has been resolved. 2741232378e8SDavid Ahern */ 2742232378e8SDavid Ahern if (need_addr_check && 2743232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2744232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 2745232378e8SDavid Ahern goto out; 2746232378e8SDavid Ahern } 2747232378e8SDavid Ahern 27489fbb704cSDavid Ahern err = 0; 27499fbb704cSDavid Ahern out: 27509fbb704cSDavid Ahern return err; 27519fbb704cSDavid Ahern } 27529fbb704cSDavid Ahern 2753333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2754333c4301SDavid Ahern struct netlink_ext_ack *extack) 27551da177e4SLinus Torvalds { 27565578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 27571da177e4SLinus Torvalds struct rt6_info *rt = NULL; 27581da177e4SLinus Torvalds struct net_device *dev = NULL; 27591da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2760c71099acSThomas Graf struct fib6_table *table; 27611da177e4SLinus Torvalds int addr_type; 27628c5b83f0SRoopa Prabhu int err = -EINVAL; 27631da177e4SLinus Torvalds 2764557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2765d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2766d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2767557c44beSDavid Ahern goto out; 2768d5d531cbSDavid Ahern } 2769557c44beSDavid Ahern 27702ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 27712ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 27722ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 27732ea2352eSWei Wang goto out; 27742ea2352eSWei Wang } 27752ea2352eSWei Wang 2776d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2777d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 27788c5b83f0SRoopa Prabhu goto out; 2779d5d531cbSDavid Ahern } 2780d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2781d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2782d5d531cbSDavid Ahern goto out; 2783d5d531cbSDavid Ahern } 27841da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2785d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2786d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2787d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 27888c5b83f0SRoopa Prabhu goto out; 2789d5d531cbSDavid Ahern } 27901da177e4SLinus Torvalds #endif 279186872cb5SThomas Graf if (cfg->fc_ifindex) { 27921da177e4SLinus Torvalds err = -ENODEV; 27935578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 27941da177e4SLinus Torvalds if (!dev) 27951da177e4SLinus Torvalds goto out; 27961da177e4SLinus Torvalds idev = in6_dev_get(dev); 27971da177e4SLinus Torvalds if (!idev) 27981da177e4SLinus Torvalds goto out; 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 280186872cb5SThomas Graf if (cfg->fc_metric == 0) 280286872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 28031da177e4SLinus Torvalds 2804fc1e64e1SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 2805fc1e64e1SDavid Ahern if (!dev) { 2806fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, 2807fc1e64e1SDavid Ahern "Nexthop device required for onlink"); 2808fc1e64e1SDavid Ahern err = -ENODEV; 2809fc1e64e1SDavid Ahern goto out; 2810fc1e64e1SDavid Ahern } 2811fc1e64e1SDavid Ahern 2812fc1e64e1SDavid Ahern if (!(dev->flags & IFF_UP)) { 2813fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 2814fc1e64e1SDavid Ahern err = -ENETDOWN; 2815fc1e64e1SDavid Ahern goto out; 2816fc1e64e1SDavid Ahern } 2817fc1e64e1SDavid Ahern } 2818fc1e64e1SDavid Ahern 2819c71099acSThomas Graf err = -ENOBUFS; 282038308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2821d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2822d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 282338308473SDavid S. Miller if (!table) { 2824f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2825d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2826d71314b4SMatti Vaittinen } 2827d71314b4SMatti Vaittinen } else { 2828d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2829d71314b4SMatti Vaittinen } 283038308473SDavid S. Miller 283138308473SDavid S. Miller if (!table) 2832c71099acSThomas Graf goto out; 2833c71099acSThomas Graf 2834ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2835ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 28361da177e4SLinus Torvalds 283738308473SDavid S. Miller if (!rt) { 28381da177e4SLinus Torvalds err = -ENOMEM; 28391da177e4SLinus Torvalds goto out; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 28421716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 28431716a961SGao feng rt6_set_expires(rt, jiffies + 28441716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 28451716a961SGao feng else 28461716a961SGao feng rt6_clean_expires(rt); 28471da177e4SLinus Torvalds 284886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 284986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 285086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 285186872cb5SThomas Graf 285286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2855d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2856ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2857ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 28581da177e4SLinus Torvalds else 2859d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 28601da177e4SLinus Torvalds 2861d8d1f30bSChangli Gao rt->dst.output = ip6_output; 28621da177e4SLinus Torvalds 286319e42e45SRoopa Prabhu if (cfg->fc_encap) { 286419e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 286519e42e45SRoopa Prabhu 286630357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2867127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 28689ae28727SDavid Ahern &lwtstate, extack); 286919e42e45SRoopa Prabhu if (err) 287019e42e45SRoopa Prabhu goto out; 287161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 28729942895bSDavid Ahern lwtunnel_set_redirect(&rt->dst); 287325368623STom Herbert } 287419e42e45SRoopa Prabhu 287586872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 287686872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2877afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 287811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 288186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 288286872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 28831da177e4SLinus Torvalds #endif 28841da177e4SLinus Torvalds 288586872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 2886398958aeSIdo Schimmel rt->rt6i_nh_weight = 1; 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 28891da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 28901da177e4SLinus Torvalds */ 289186872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 289238308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 289338308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 289438308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 28951da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 28965578689aSDaniel Lezcano if (dev != net->loopback_dev) { 28971da177e4SLinus Torvalds if (dev) { 28981da177e4SLinus Torvalds dev_put(dev); 28991da177e4SLinus Torvalds in6_dev_put(idev); 29001da177e4SLinus Torvalds } 29015578689aSDaniel Lezcano dev = net->loopback_dev; 29021da177e4SLinus Torvalds dev_hold(dev); 29031da177e4SLinus Torvalds idev = in6_dev_get(dev); 29041da177e4SLinus Torvalds if (!idev) { 29051da177e4SLinus Torvalds err = -ENODEV; 29061da177e4SLinus Torvalds goto out; 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2910ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2911ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2912ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2913ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 29147150aedeSKamala R rt->dst.input = dst_discard; 2915ef2c7d7bSNicolas Dichtel break; 2916ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2917ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 29187150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 29197150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2920ef2c7d7bSNicolas Dichtel break; 2921b4949ab2SNicolas Dichtel case RTN_THROW: 29220315e382SNikola Forró case RTN_UNREACHABLE: 2923ef2c7d7bSNicolas Dichtel default: 29247150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 29250315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 29260315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 29277150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 29287150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2929ef2c7d7bSNicolas Dichtel break; 2930ef2c7d7bSNicolas Dichtel } 29311da177e4SLinus Torvalds goto install_route; 29321da177e4SLinus Torvalds } 29331da177e4SLinus Torvalds 293486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 29359fbb704cSDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 29361da177e4SLinus Torvalds if (err) 29371da177e4SLinus Torvalds goto out; 29389fbb704cSDavid Ahern 29399fbb704cSDavid Ahern rt->rt6i_gateway = cfg->fc_gateway; 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds err = -ENODEV; 294338308473SDavid S. Miller if (!dev) 29441da177e4SLinus Torvalds goto out; 29451da177e4SLinus Torvalds 2946428604fbSLorenzo Bianconi if (idev->cnf.disable_ipv6) { 2947428604fbSLorenzo Bianconi NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 2948428604fbSLorenzo Bianconi err = -EACCES; 2949428604fbSLorenzo Bianconi goto out; 2950428604fbSLorenzo Bianconi } 2951428604fbSLorenzo Bianconi 2952955ec4cbSDavid Ahern if (!(dev->flags & IFF_UP)) { 2953955ec4cbSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 2954955ec4cbSDavid Ahern err = -ENETDOWN; 2955955ec4cbSDavid Ahern goto out; 2956955ec4cbSDavid Ahern } 2957955ec4cbSDavid Ahern 2958c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2959c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2960d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2961c3968a85SDaniel Walter err = -EINVAL; 2962c3968a85SDaniel Walter goto out; 2963c3968a85SDaniel Walter } 29644e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2965c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2966c3968a85SDaniel Walter } else 2967c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2968c3968a85SDaniel Walter 296986872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds install_route: 29725609b80aSIdo Schimmel if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) && 29735609b80aSIdo Schimmel !netif_carrier_ok(dev)) 29745609b80aSIdo Schimmel rt->rt6i_nh_flags |= RTNH_F_LINKDOWN; 2975fc1e64e1SDavid Ahern rt->rt6i_nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK); 2976d8d1f30bSChangli Gao rt->dst.dev = dev; 29771da177e4SLinus Torvalds rt->rt6i_idev = idev; 2978c71099acSThomas Graf rt->rt6i_table = table; 297963152fc0SDaniel Lezcano 2980c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 298163152fc0SDaniel Lezcano 29828c5b83f0SRoopa Prabhu return rt; 29831da177e4SLinus Torvalds out: 29841da177e4SLinus Torvalds if (dev) 29851da177e4SLinus Torvalds dev_put(dev); 29861da177e4SLinus Torvalds if (idev) 29871da177e4SLinus Torvalds in6_dev_put(idev); 2988587fea74SWei Wang if (rt) 2989587fea74SWei Wang dst_release_immediate(&rt->dst); 29906b9ea5a6SRoopa Prabhu 29918c5b83f0SRoopa Prabhu return ERR_PTR(err); 29926b9ea5a6SRoopa Prabhu } 29936b9ea5a6SRoopa Prabhu 2994333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2995333c4301SDavid Ahern struct netlink_ext_ack *extack) 29966b9ea5a6SRoopa Prabhu { 29976b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 29988c5b83f0SRoopa Prabhu struct rt6_info *rt; 29996b9ea5a6SRoopa Prabhu int err; 30006b9ea5a6SRoopa Prabhu 3001333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 30028c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 30038c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 30048c5b83f0SRoopa Prabhu rt = NULL; 30056b9ea5a6SRoopa Prabhu goto out; 30068c5b83f0SRoopa Prabhu } 30076b9ea5a6SRoopa Prabhu 30086b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 30096b9ea5a6SRoopa Prabhu if (err) 30106b9ea5a6SRoopa Prabhu goto out; 30116b9ea5a6SRoopa Prabhu 3012333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 30136b9ea5a6SRoopa Prabhu 30146b9ea5a6SRoopa Prabhu kfree(mxc.mx); 30156b9ea5a6SRoopa Prabhu 30166b9ea5a6SRoopa Prabhu return err; 30176b9ea5a6SRoopa Prabhu out: 3018587fea74SWei Wang if (rt) 3019587fea74SWei Wang dst_release_immediate(&rt->dst); 30206b9ea5a6SRoopa Prabhu 30211da177e4SLinus Torvalds return err; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 302486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 30251da177e4SLinus Torvalds { 30261da177e4SLinus Torvalds int err; 3027c71099acSThomas Graf struct fib6_table *table; 3028d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 30291da177e4SLinus Torvalds 3030a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 30316825a26cSGao feng err = -ENOENT; 30326825a26cSGao feng goto out; 30336825a26cSGao feng } 30346c813a72SPatrick McHardy 3035c71099acSThomas Graf table = rt->rt6i_table; 303666f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 303786872cb5SThomas Graf err = fib6_del(rt, info); 303866f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 30391da177e4SLinus Torvalds 30406825a26cSGao feng out: 304194e187c0SAmerigo Wang ip6_rt_put(rt); 30421da177e4SLinus Torvalds return err; 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds 3045e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 3046e0a1ad73SThomas Graf { 30474d1169c1SDenis V. Lunev struct nl_info info = { 3048d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 30494d1169c1SDenis V. Lunev }; 3050528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3051e0a1ad73SThomas Graf } 3052e0a1ad73SThomas Graf 30530ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 30540ae81335SDavid Ahern { 30550ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3056e3330039SWANG Cong struct net *net = info->nl_net; 305716a16cd3SDavid Ahern struct sk_buff *skb = NULL; 30580ae81335SDavid Ahern struct fib6_table *table; 3059e3330039SWANG Cong int err = -ENOENT; 30600ae81335SDavid Ahern 3061e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 3062e3330039SWANG Cong goto out_put; 30630ae81335SDavid Ahern table = rt->rt6i_table; 306466f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 30650ae81335SDavid Ahern 30660ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 30670ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 30680ae81335SDavid Ahern 306916a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 307016a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 307116a16cd3SDavid Ahern if (skb) { 307216a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 307316a16cd3SDavid Ahern 3074e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 307516a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 307616a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 307716a16cd3SDavid Ahern kfree_skb(skb); 307816a16cd3SDavid Ahern skb = NULL; 307916a16cd3SDavid Ahern } else 308016a16cd3SDavid Ahern info->skip_notify = 1; 308116a16cd3SDavid Ahern } 308216a16cd3SDavid Ahern 30830ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 30840ae81335SDavid Ahern &rt->rt6i_siblings, 30850ae81335SDavid Ahern rt6i_siblings) { 30860ae81335SDavid Ahern err = fib6_del(sibling, info); 30870ae81335SDavid Ahern if (err) 3088e3330039SWANG Cong goto out_unlock; 30890ae81335SDavid Ahern } 30900ae81335SDavid Ahern } 30910ae81335SDavid Ahern 30920ae81335SDavid Ahern err = fib6_del(rt, info); 3093e3330039SWANG Cong out_unlock: 309466f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3095e3330039SWANG Cong out_put: 30960ae81335SDavid Ahern ip6_rt_put(rt); 309716a16cd3SDavid Ahern 309816a16cd3SDavid Ahern if (skb) { 3099e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 310016a16cd3SDavid Ahern info->nlh, gfp_any()); 310116a16cd3SDavid Ahern } 31020ae81335SDavid Ahern return err; 31030ae81335SDavid Ahern } 31040ae81335SDavid Ahern 3105333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3106333c4301SDavid Ahern struct netlink_ext_ack *extack) 31071da177e4SLinus Torvalds { 31082b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 3109c71099acSThomas Graf struct fib6_table *table; 31101da177e4SLinus Torvalds struct fib6_node *fn; 31111da177e4SLinus Torvalds int err = -ESRCH; 31121da177e4SLinus Torvalds 31135578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3114d5d531cbSDavid Ahern if (!table) { 3115d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3116c71099acSThomas Graf return err; 3117d5d531cbSDavid Ahern } 31181da177e4SLinus Torvalds 311966f5d6ceSWei Wang rcu_read_lock(); 3120c71099acSThomas Graf 3121c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 312286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 312338fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 31242b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds if (fn) { 312766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 31282b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 31292b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 31302b760fcfSWei Wang &cfg->fc_src); 31312b760fcfSWei Wang if (!rt_cache) 31321f56a01fSMartin KaFai Lau continue; 31332b760fcfSWei Wang rt = rt_cache; 31342b760fcfSWei Wang } 313586872cb5SThomas Graf if (cfg->fc_ifindex && 3136d1918542SDavid S. Miller (!rt->dst.dev || 3137d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 31381da177e4SLinus Torvalds continue; 313986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 314086872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 31411da177e4SLinus Torvalds continue; 314286872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 31431da177e4SLinus Torvalds continue; 3144c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 3145c2ed1880SMantas M continue; 3146d3843fe5SWei Wang if (!dst_hold_safe(&rt->dst)) 3147d3843fe5SWei Wang break; 314866f5d6ceSWei Wang rcu_read_unlock(); 31491da177e4SLinus Torvalds 31500ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 31510ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 315286872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 31530ae81335SDavid Ahern 31540ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds } 315766f5d6ceSWei Wang rcu_read_unlock(); 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds return err; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31626700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3163a6279458SYOSHIFUJI Hideaki { 3164a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3165e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 3166e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3167e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3168e8599ff4SDavid S. Miller struct neighbour *neigh; 316971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 31706e157b6aSDavid S. Miller int optlen, on_link; 31716e157b6aSDavid S. Miller u8 *lladdr; 3172e8599ff4SDavid S. Miller 317329a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 317471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3175e8599ff4SDavid S. Miller 3176e8599ff4SDavid S. Miller if (optlen < 0) { 31776e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3178e8599ff4SDavid S. Miller return; 3179e8599ff4SDavid S. Miller } 3180e8599ff4SDavid S. Miller 318171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3182e8599ff4SDavid S. Miller 318371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 31846e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3185e8599ff4SDavid S. Miller return; 3186e8599ff4SDavid S. Miller } 3187e8599ff4SDavid S. Miller 31886e157b6aSDavid S. Miller on_link = 0; 318971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3190e8599ff4SDavid S. Miller on_link = 1; 319171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3192e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 31936e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3194e8599ff4SDavid S. Miller return; 3195e8599ff4SDavid S. Miller } 3196e8599ff4SDavid S. Miller 3197e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3198e8599ff4SDavid S. Miller if (!in6_dev) 3199e8599ff4SDavid S. Miller return; 3200e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3201e8599ff4SDavid S. Miller return; 3202e8599ff4SDavid S. Miller 3203e8599ff4SDavid S. Miller /* RFC2461 8.1: 3204e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3205e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3206e8599ff4SDavid S. Miller */ 3207e8599ff4SDavid S. Miller 3208f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3209e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3210e8599ff4SDavid S. Miller return; 3211e8599ff4SDavid S. Miller } 32126e157b6aSDavid S. Miller 32136e157b6aSDavid S. Miller lladdr = NULL; 3214e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3215e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3216e8599ff4SDavid S. Miller skb->dev); 3217e8599ff4SDavid S. Miller if (!lladdr) { 3218e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3219e8599ff4SDavid S. Miller return; 3220e8599ff4SDavid S. Miller } 3221e8599ff4SDavid S. Miller } 3222e8599ff4SDavid S. Miller 32236e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3224ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 32256e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 32266e157b6aSDavid S. Miller return; 32276e157b6aSDavid S. Miller } 32286e157b6aSDavid S. Miller 32296e157b6aSDavid S. Miller /* Redirect received -> path was valid. 32306e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 32316e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 32326e157b6aSDavid S. Miller */ 32330dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 32346e157b6aSDavid S. Miller 323571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3236e8599ff4SDavid S. Miller if (!neigh) 3237e8599ff4SDavid S. Miller return; 3238e8599ff4SDavid S. Miller 32391da177e4SLinus Torvalds /* 32401da177e4SLinus Torvalds * We have finally decided to accept it. 32411da177e4SLinus Torvalds */ 32421da177e4SLinus Torvalds 3243f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 32441da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 32451da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 32461da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3247f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3248f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 32491da177e4SLinus Torvalds 325083a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 325138308473SDavid S. Miller if (!nrt) 32521da177e4SLinus Torvalds goto out; 32531da177e4SLinus Torvalds 32541da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 32551da177e4SLinus Torvalds if (on_link) 32561da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 32571da177e4SLinus Torvalds 3258b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 32594e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 32601da177e4SLinus Torvalds 32612b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 32622b760fcfSWei Wang * a cached route because rt6_insert_exception() will 32632b760fcfSWei Wang * takes care of it 32642b760fcfSWei Wang */ 32652b760fcfSWei Wang if (rt6_insert_exception(nrt, rt)) { 32662b760fcfSWei Wang dst_release_immediate(&nrt->dst); 32672b760fcfSWei Wang goto out; 32682b760fcfSWei Wang } 32691da177e4SLinus Torvalds 3270d8d1f30bSChangli Gao netevent.old = &rt->dst; 3271d8d1f30bSChangli Gao netevent.new = &nrt->dst; 327271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 327360592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 32748d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 32758d71740cSTom Tucker 32761da177e4SLinus Torvalds out: 3277e8599ff4SDavid S. Miller neigh_release(neigh); 32786e157b6aSDavid S. Miller } 32796e157b6aSDavid S. Miller 32801da177e4SLinus Torvalds /* 32811da177e4SLinus Torvalds * Misc support functions 32821da177e4SLinus Torvalds */ 32831da177e4SLinus Torvalds 32844b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 32854b32b5adSMartin KaFai Lau { 32863a2232e9SDavid Miller BUG_ON(from->from); 32874b32b5adSMartin KaFai Lau 32884b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 32894b32b5adSMartin KaFai Lau dst_hold(&from->dst); 32903a2232e9SDavid Miller rt->from = from; 32914b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 32924b32b5adSMartin KaFai Lau } 32934b32b5adSMartin KaFai Lau 329483a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 32951da177e4SLinus Torvalds { 3296d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 3297d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 329883a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 3299d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 33001da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 33011da177e4SLinus Torvalds if (rt->rt6i_idev) 33021da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 3303d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 33044e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 33051716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 33061716a961SGao feng rt6_set_from(rt, ort); 330783a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 33081da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 330983a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 33101da177e4SLinus Torvalds #endif 331183a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 3312c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 331361adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds 331670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 3317efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 3318b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3319830218c1SDavid Ahern const struct in6_addr *gwaddr, 3320830218c1SDavid Ahern struct net_device *dev) 332170ceb4f5SYOSHIFUJI Hideaki { 3322830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3323830218c1SDavid Ahern int ifindex = dev->ifindex; 332470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 332570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3326c71099acSThomas Graf struct fib6_table *table; 332770ceb4f5SYOSHIFUJI Hideaki 3328830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 332938308473SDavid S. Miller if (!table) 3330c71099acSThomas Graf return NULL; 3331c71099acSThomas Graf 333266f5d6ceSWei Wang rcu_read_lock(); 333338fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 333470ceb4f5SYOSHIFUJI Hideaki if (!fn) 333570ceb4f5SYOSHIFUJI Hideaki goto out; 333670ceb4f5SYOSHIFUJI Hideaki 333766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3338d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 333970ceb4f5SYOSHIFUJI Hideaki continue; 334070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 334170ceb4f5SYOSHIFUJI Hideaki continue; 334270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 334370ceb4f5SYOSHIFUJI Hideaki continue; 3344d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 334570ceb4f5SYOSHIFUJI Hideaki break; 334670ceb4f5SYOSHIFUJI Hideaki } 334770ceb4f5SYOSHIFUJI Hideaki out: 334866f5d6ceSWei Wang rcu_read_unlock(); 334970ceb4f5SYOSHIFUJI Hideaki return rt; 335070ceb4f5SYOSHIFUJI Hideaki } 335170ceb4f5SYOSHIFUJI Hideaki 3352efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3353b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3354830218c1SDavid Ahern const struct in6_addr *gwaddr, 3355830218c1SDavid Ahern struct net_device *dev, 335695c96174SEric Dumazet unsigned int pref) 335770ceb4f5SYOSHIFUJI Hideaki { 335886872cb5SThomas Graf struct fib6_config cfg = { 3359238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3360830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 336186872cb5SThomas Graf .fc_dst_len = prefixlen, 336286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 336386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3364b91d5329SXin Long .fc_protocol = RTPROT_RA, 336515e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3366efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3367efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 336886872cb5SThomas Graf }; 336970ceb4f5SYOSHIFUJI Hideaki 3370830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 33714e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 33724e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 337386872cb5SThomas Graf 3374e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3375e317da96SYOSHIFUJI Hideaki if (!prefixlen) 337686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 337770ceb4f5SYOSHIFUJI Hideaki 3378333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 337970ceb4f5SYOSHIFUJI Hideaki 3380830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 338170ceb4f5SYOSHIFUJI Hideaki } 338270ceb4f5SYOSHIFUJI Hideaki #endif 338370ceb4f5SYOSHIFUJI Hideaki 3384b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 33851da177e4SLinus Torvalds { 3386830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 33871da177e4SLinus Torvalds struct rt6_info *rt; 3388c71099acSThomas Graf struct fib6_table *table; 33891da177e4SLinus Torvalds 3390830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 339138308473SDavid S. Miller if (!table) 3392c71099acSThomas Graf return NULL; 33931da177e4SLinus Torvalds 339466f5d6ceSWei Wang rcu_read_lock(); 339566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3396d1918542SDavid S. Miller if (dev == rt->dst.dev && 3397045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 33981da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 33991da177e4SLinus Torvalds break; 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds if (rt) 3402d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 340366f5d6ceSWei Wang rcu_read_unlock(); 34041da177e4SLinus Torvalds return rt; 34051da177e4SLinus Torvalds } 34061da177e4SLinus Torvalds 3407b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3408ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3409ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 34101da177e4SLinus Torvalds { 341186872cb5SThomas Graf struct fib6_config cfg = { 3412ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3413238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 341486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 341586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 341686872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3417b91d5329SXin Long .fc_protocol = RTPROT_RA, 341815e47304SEric W. Biederman .fc_nlinfo.portid = 0, 34195578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3420c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 342186872cb5SThomas Graf }; 34221da177e4SLinus Torvalds 34234e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 34241da177e4SLinus Torvalds 3425333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3426830218c1SDavid Ahern struct fib6_table *table; 3427830218c1SDavid Ahern 3428830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3429830218c1SDavid Ahern if (table) 3430830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3431830218c1SDavid Ahern } 34321da177e4SLinus Torvalds 34331da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 34341da177e4SLinus Torvalds } 34351da177e4SLinus Torvalds 3436830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 34371da177e4SLinus Torvalds { 34381da177e4SLinus Torvalds struct rt6_info *rt; 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds restart: 344166f5d6ceSWei Wang rcu_read_lock(); 344266f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 34433e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 34443e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3445d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) { 344666f5d6ceSWei Wang rcu_read_unlock(); 3447e0a1ad73SThomas Graf ip6_del_rt(rt); 3448d3843fe5SWei Wang } else { 344966f5d6ceSWei Wang rcu_read_unlock(); 3450d3843fe5SWei Wang } 34511da177e4SLinus Torvalds goto restart; 34521da177e4SLinus Torvalds } 34531da177e4SLinus Torvalds } 345466f5d6ceSWei Wang rcu_read_unlock(); 3455830218c1SDavid Ahern 3456830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3457830218c1SDavid Ahern } 3458830218c1SDavid Ahern 3459830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3460830218c1SDavid Ahern { 3461830218c1SDavid Ahern struct fib6_table *table; 3462830218c1SDavid Ahern struct hlist_head *head; 3463830218c1SDavid Ahern unsigned int h; 3464830218c1SDavid Ahern 3465830218c1SDavid Ahern rcu_read_lock(); 3466830218c1SDavid Ahern 3467830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3468830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3469830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3470830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3471830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3472830218c1SDavid Ahern } 3473830218c1SDavid Ahern } 3474830218c1SDavid Ahern 3475830218c1SDavid Ahern rcu_read_unlock(); 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 34785578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 34795578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 348086872cb5SThomas Graf struct fib6_config *cfg) 348186872cb5SThomas Graf { 348286872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 348386872cb5SThomas Graf 3484ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3485ca254490SDavid Ahern : RT6_TABLE_MAIN; 348686872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 348786872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 348886872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 348986872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 349086872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 349186872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 349286872cb5SThomas Graf 34935578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3494f1243c2dSBenjamin Thery 34954e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 34964e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 34974e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 349886872cb5SThomas Graf } 349986872cb5SThomas Graf 35005578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 35011da177e4SLinus Torvalds { 350286872cb5SThomas Graf struct fib6_config cfg; 35031da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 35041da177e4SLinus Torvalds int err; 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds switch (cmd) { 35071da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 35081da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3509af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 35101da177e4SLinus Torvalds return -EPERM; 35111da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 35121da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 35131da177e4SLinus Torvalds if (err) 35141da177e4SLinus Torvalds return -EFAULT; 35151da177e4SLinus Torvalds 35165578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 351786872cb5SThomas Graf 35181da177e4SLinus Torvalds rtnl_lock(); 35191da177e4SLinus Torvalds switch (cmd) { 35201da177e4SLinus Torvalds case SIOCADDRT: 3521333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 35221da177e4SLinus Torvalds break; 35231da177e4SLinus Torvalds case SIOCDELRT: 3524333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 35251da177e4SLinus Torvalds break; 35261da177e4SLinus Torvalds default: 35271da177e4SLinus Torvalds err = -EINVAL; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds rtnl_unlock(); 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds return err; 35323ff50b79SStephen Hemminger } 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds return -EINVAL; 35351da177e4SLinus Torvalds } 35361da177e4SLinus Torvalds 35371da177e4SLinus Torvalds /* 35381da177e4SLinus Torvalds * Drop the packet on the floor 35391da177e4SLinus Torvalds */ 35401da177e4SLinus Torvalds 3541d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 35421da177e4SLinus Torvalds { 3543612f09e8SYOSHIFUJI Hideaki int type; 3544adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3545612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3546612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 35470660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 354845bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 35493bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 35503bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3551612f09e8SYOSHIFUJI Hideaki break; 3552612f09e8SYOSHIFUJI Hideaki } 3553612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3554612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 35553bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 35563bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3557612f09e8SYOSHIFUJI Hideaki break; 3558612f09e8SYOSHIFUJI Hideaki } 35593ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 35601da177e4SLinus Torvalds kfree_skb(skb); 35611da177e4SLinus Torvalds return 0; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds 35649ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 35659ce8ade0SThomas Graf { 3566612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 35679ce8ade0SThomas Graf } 35689ce8ade0SThomas Graf 3569ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 35701da177e4SLinus Torvalds { 3571adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3572612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 35731da177e4SLinus Torvalds } 35741da177e4SLinus Torvalds 35759ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 35769ce8ade0SThomas Graf { 3577612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 35789ce8ade0SThomas Graf } 35799ce8ade0SThomas Graf 3580ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 35819ce8ade0SThomas Graf { 3582adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3583612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 35849ce8ade0SThomas Graf } 35859ce8ade0SThomas Graf 35861da177e4SLinus Torvalds /* 35871da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 35881da177e4SLinus Torvalds */ 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 35911da177e4SLinus Torvalds const struct in6_addr *addr, 35928f031519SDavid S. Miller bool anycast) 35931da177e4SLinus Torvalds { 3594ca254490SDavid Ahern u32 tb_id; 3595c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 35964832c30dSDavid Ahern struct net_device *dev = idev->dev; 35975f02ce24SDavid Ahern struct rt6_info *rt; 35985f02ce24SDavid Ahern 35995f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3600a3300ef4SHannes Frederic Sowa if (!rt) 36011da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds in6_dev_hold(idev); 36041da177e4SLinus Torvalds 360511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3606d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3607d8d1f30bSChangli Gao rt->dst.output = ip6_output; 36081da177e4SLinus Torvalds rt->rt6i_idev = idev; 36091da177e4SLinus Torvalds 361094b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 36111da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 361258c4fb86SYOSHIFUJI Hideaki if (anycast) 361358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 361458c4fb86SYOSHIFUJI Hideaki else 36151da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 36161da177e4SLinus Torvalds 3617550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 36184e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 36191da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3620ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3621ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 36221da177e4SLinus Torvalds 36231da177e4SLinus Torvalds return rt; 36241da177e4SLinus Torvalds } 36251da177e4SLinus Torvalds 3626c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3627c3968a85SDaniel Walter struct arg_dev_net_ip { 3628c3968a85SDaniel Walter struct net_device *dev; 3629c3968a85SDaniel Walter struct net *net; 3630c3968a85SDaniel Walter struct in6_addr *addr; 3631c3968a85SDaniel Walter }; 3632c3968a85SDaniel Walter 3633c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3634c3968a85SDaniel Walter { 3635c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3636c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3637c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3638c3968a85SDaniel Walter 3639d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3640c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3641c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 364260006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3643c3968a85SDaniel Walter /* remove prefsrc entry */ 3644c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 364560006a48SWei Wang /* need to update cache as well */ 364660006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 364760006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3648c3968a85SDaniel Walter } 3649c3968a85SDaniel Walter return 0; 3650c3968a85SDaniel Walter } 3651c3968a85SDaniel Walter 3652c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3653c3968a85SDaniel Walter { 3654c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3655c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3656c3968a85SDaniel Walter .dev = ifp->idev->dev, 3657c3968a85SDaniel Walter .net = net, 3658c3968a85SDaniel Walter .addr = &ifp->addr, 3659c3968a85SDaniel Walter }; 36600c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3661c3968a85SDaniel Walter } 3662c3968a85SDaniel Walter 3663be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3664be7a010dSDuan Jiong 3665be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3666be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3667be7a010dSDuan Jiong { 3668be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3669be7a010dSDuan Jiong 36702b760fcfSWei Wang if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3671be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3672be7a010dSDuan Jiong return -1; 3673be7a010dSDuan Jiong } 3674b16cb459SWei Wang 3675b16cb459SWei Wang /* Further clean up cached routes in exception table. 3676b16cb459SWei Wang * This is needed because cached route may have a different 3677b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3678b16cb459SWei Wang */ 3679b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3680b16cb459SWei Wang 3681be7a010dSDuan Jiong return 0; 3682be7a010dSDuan Jiong } 3683be7a010dSDuan Jiong 3684be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3685be7a010dSDuan Jiong { 3686be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3687be7a010dSDuan Jiong } 3688be7a010dSDuan Jiong 36892127d95aSIdo Schimmel struct arg_netdev_event { 36902127d95aSIdo Schimmel const struct net_device *dev; 36914c981e28SIdo Schimmel union { 36922127d95aSIdo Schimmel unsigned int nh_flags; 36934c981e28SIdo Schimmel unsigned long event; 36944c981e28SIdo Schimmel }; 36952127d95aSIdo Schimmel }; 36962127d95aSIdo Schimmel 3697d7dedee1SIdo Schimmel static struct rt6_info *rt6_multipath_first_sibling(const struct rt6_info *rt) 3698d7dedee1SIdo Schimmel { 3699d7dedee1SIdo Schimmel struct rt6_info *iter; 3700d7dedee1SIdo Schimmel struct fib6_node *fn; 3701d7dedee1SIdo Schimmel 3702d7dedee1SIdo Schimmel fn = rcu_dereference_protected(rt->rt6i_node, 3703d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3704d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 3705d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3706d7dedee1SIdo Schimmel while (iter) { 3707d7dedee1SIdo Schimmel if (iter->rt6i_metric == rt->rt6i_metric && 3708d7dedee1SIdo Schimmel rt6_qualify_for_ecmp(iter)) 3709d7dedee1SIdo Schimmel return iter; 3710d7dedee1SIdo Schimmel iter = rcu_dereference_protected(iter->rt6_next, 3711d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3712d7dedee1SIdo Schimmel } 3713d7dedee1SIdo Schimmel 3714d7dedee1SIdo Schimmel return NULL; 3715d7dedee1SIdo Schimmel } 3716d7dedee1SIdo Schimmel 3717d7dedee1SIdo Schimmel static bool rt6_is_dead(const struct rt6_info *rt) 3718d7dedee1SIdo Schimmel { 3719d7dedee1SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD || 3720d7dedee1SIdo Schimmel (rt->rt6i_nh_flags & RTNH_F_LINKDOWN && 3721d7dedee1SIdo Schimmel rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 3722d7dedee1SIdo Schimmel return true; 3723d7dedee1SIdo Schimmel 3724d7dedee1SIdo Schimmel return false; 3725d7dedee1SIdo Schimmel } 3726d7dedee1SIdo Schimmel 3727d7dedee1SIdo Schimmel static int rt6_multipath_total_weight(const struct rt6_info *rt) 3728d7dedee1SIdo Schimmel { 3729d7dedee1SIdo Schimmel struct rt6_info *iter; 3730d7dedee1SIdo Schimmel int total = 0; 3731d7dedee1SIdo Schimmel 3732d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 3733398958aeSIdo Schimmel total += rt->rt6i_nh_weight; 3734d7dedee1SIdo Schimmel 3735d7dedee1SIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) { 3736d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 3737398958aeSIdo Schimmel total += iter->rt6i_nh_weight; 3738d7dedee1SIdo Schimmel } 3739d7dedee1SIdo Schimmel 3740d7dedee1SIdo Schimmel return total; 3741d7dedee1SIdo Schimmel } 3742d7dedee1SIdo Schimmel 3743d7dedee1SIdo Schimmel static void rt6_upper_bound_set(struct rt6_info *rt, int *weight, int total) 3744d7dedee1SIdo Schimmel { 3745d7dedee1SIdo Schimmel int upper_bound = -1; 3746d7dedee1SIdo Schimmel 3747d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 3748398958aeSIdo Schimmel *weight += rt->rt6i_nh_weight; 3749d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 3750d7dedee1SIdo Schimmel total) - 1; 3751d7dedee1SIdo Schimmel } 3752d7dedee1SIdo Schimmel atomic_set(&rt->rt6i_nh_upper_bound, upper_bound); 3753d7dedee1SIdo Schimmel } 3754d7dedee1SIdo Schimmel 3755d7dedee1SIdo Schimmel static void rt6_multipath_upper_bound_set(struct rt6_info *rt, int total) 3756d7dedee1SIdo Schimmel { 3757d7dedee1SIdo Schimmel struct rt6_info *iter; 3758d7dedee1SIdo Schimmel int weight = 0; 3759d7dedee1SIdo Schimmel 3760d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 3761d7dedee1SIdo Schimmel 3762d7dedee1SIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 3763d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 3764d7dedee1SIdo Schimmel } 3765d7dedee1SIdo Schimmel 3766d7dedee1SIdo Schimmel void rt6_multipath_rebalance(struct rt6_info *rt) 3767d7dedee1SIdo Schimmel { 3768d7dedee1SIdo Schimmel struct rt6_info *first; 3769d7dedee1SIdo Schimmel int total; 3770d7dedee1SIdo Schimmel 3771d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 3772d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 3773d7dedee1SIdo Schimmel * sibling route. 3774d7dedee1SIdo Schimmel */ 3775d7dedee1SIdo Schimmel if (!rt->rt6i_nsiblings || rt->should_flush) 3776d7dedee1SIdo Schimmel return; 3777d7dedee1SIdo Schimmel 3778d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 3779d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 3780d7dedee1SIdo Schimmel * onwards. 3781d7dedee1SIdo Schimmel */ 3782d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 3783d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 3784d7dedee1SIdo Schimmel return; 3785d7dedee1SIdo Schimmel 3786d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 3787d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 3788d7dedee1SIdo Schimmel } 3789d7dedee1SIdo Schimmel 37902127d95aSIdo Schimmel static int fib6_ifup(struct rt6_info *rt, void *p_arg) 37912127d95aSIdo Schimmel { 37922127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 37932127d95aSIdo Schimmel const struct net *net = dev_net(arg->dev); 37942127d95aSIdo Schimmel 37951de178edSIdo Schimmel if (rt != net->ipv6.ip6_null_entry && rt->dst.dev == arg->dev) { 37962127d95aSIdo Schimmel rt->rt6i_nh_flags &= ~arg->nh_flags; 37971de178edSIdo Schimmel fib6_update_sernum_upto_root(dev_net(rt->dst.dev), rt); 3798d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 37991de178edSIdo Schimmel } 38002127d95aSIdo Schimmel 38012127d95aSIdo Schimmel return 0; 38022127d95aSIdo Schimmel } 38032127d95aSIdo Schimmel 38042127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) 38052127d95aSIdo Schimmel { 38062127d95aSIdo Schimmel struct arg_netdev_event arg = { 38072127d95aSIdo Schimmel .dev = dev, 38086802f3adSIdo Schimmel { 38092127d95aSIdo Schimmel .nh_flags = nh_flags, 38106802f3adSIdo Schimmel }, 38112127d95aSIdo Schimmel }; 38122127d95aSIdo Schimmel 38132127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 38142127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 38152127d95aSIdo Schimmel 38162127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 38172127d95aSIdo Schimmel } 38182127d95aSIdo Schimmel 38191de178edSIdo Schimmel static bool rt6_multipath_uses_dev(const struct rt6_info *rt, 38201de178edSIdo Schimmel const struct net_device *dev) 38211de178edSIdo Schimmel { 38221de178edSIdo Schimmel struct rt6_info *iter; 38231de178edSIdo Schimmel 38241de178edSIdo Schimmel if (rt->dst.dev == dev) 38251de178edSIdo Schimmel return true; 38261de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 38271de178edSIdo Schimmel if (iter->dst.dev == dev) 38281de178edSIdo Schimmel return true; 38291de178edSIdo Schimmel 38301de178edSIdo Schimmel return false; 38311de178edSIdo Schimmel } 38321de178edSIdo Schimmel 38331de178edSIdo Schimmel static void rt6_multipath_flush(struct rt6_info *rt) 38341de178edSIdo Schimmel { 38351de178edSIdo Schimmel struct rt6_info *iter; 38361de178edSIdo Schimmel 38371de178edSIdo Schimmel rt->should_flush = 1; 38381de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 38391de178edSIdo Schimmel iter->should_flush = 1; 38401de178edSIdo Schimmel } 38411de178edSIdo Schimmel 38421de178edSIdo Schimmel static unsigned int rt6_multipath_dead_count(const struct rt6_info *rt, 38431de178edSIdo Schimmel const struct net_device *down_dev) 38441de178edSIdo Schimmel { 38451de178edSIdo Schimmel struct rt6_info *iter; 38461de178edSIdo Schimmel unsigned int dead = 0; 38471de178edSIdo Schimmel 38481de178edSIdo Schimmel if (rt->dst.dev == down_dev || rt->rt6i_nh_flags & RTNH_F_DEAD) 38491de178edSIdo Schimmel dead++; 38501de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 38511de178edSIdo Schimmel if (iter->dst.dev == down_dev || 38521de178edSIdo Schimmel iter->rt6i_nh_flags & RTNH_F_DEAD) 38531de178edSIdo Schimmel dead++; 38541de178edSIdo Schimmel 38551de178edSIdo Schimmel return dead; 38561de178edSIdo Schimmel } 38571de178edSIdo Schimmel 38581de178edSIdo Schimmel static void rt6_multipath_nh_flags_set(struct rt6_info *rt, 38591de178edSIdo Schimmel const struct net_device *dev, 38601de178edSIdo Schimmel unsigned int nh_flags) 38611de178edSIdo Schimmel { 38621de178edSIdo Schimmel struct rt6_info *iter; 38631de178edSIdo Schimmel 38641de178edSIdo Schimmel if (rt->dst.dev == dev) 38651de178edSIdo Schimmel rt->rt6i_nh_flags |= nh_flags; 38661de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 38671de178edSIdo Schimmel if (iter->dst.dev == dev) 38681de178edSIdo Schimmel iter->rt6i_nh_flags |= nh_flags; 38691de178edSIdo Schimmel } 38701de178edSIdo Schimmel 3871a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 38724c981e28SIdo Schimmel static int fib6_ifdown(struct rt6_info *rt, void *p_arg) 38731da177e4SLinus Torvalds { 38744c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 38754c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 38764c981e28SIdo Schimmel const struct net *net = dev_net(dev); 38778ed67789SDaniel Lezcano 38781de178edSIdo Schimmel if (rt == net->ipv6.ip6_null_entry) 387927c6fa73SIdo Schimmel return 0; 388027c6fa73SIdo Schimmel 388127c6fa73SIdo Schimmel switch (arg->event) { 388227c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 38831de178edSIdo Schimmel return rt->dst.dev == dev ? -1 : 0; 388427c6fa73SIdo Schimmel case NETDEV_DOWN: 38851de178edSIdo Schimmel if (rt->should_flush) 388627c6fa73SIdo Schimmel return -1; 38871de178edSIdo Schimmel if (!rt->rt6i_nsiblings) 38881de178edSIdo Schimmel return rt->dst.dev == dev ? -1 : 0; 38891de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 38901de178edSIdo Schimmel unsigned int count; 38911de178edSIdo Schimmel 38921de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 38931de178edSIdo Schimmel if (rt->rt6i_nsiblings + 1 == count) { 38941de178edSIdo Schimmel rt6_multipath_flush(rt); 38951de178edSIdo Schimmel return -1; 38961de178edSIdo Schimmel } 38971de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 38981de178edSIdo Schimmel RTNH_F_LINKDOWN); 38991de178edSIdo Schimmel fib6_update_sernum(rt); 3900d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 39011de178edSIdo Schimmel } 39021de178edSIdo Schimmel return -2; 390327c6fa73SIdo Schimmel case NETDEV_CHANGE: 39041de178edSIdo Schimmel if (rt->dst.dev != dev || 39051de178edSIdo Schimmel rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) 390627c6fa73SIdo Schimmel break; 390727c6fa73SIdo Schimmel rt->rt6i_nh_flags |= RTNH_F_LINKDOWN; 3908d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 390927c6fa73SIdo Schimmel break; 39102b241361SIdo Schimmel } 3911c159d30cSDavid S. Miller 39121da177e4SLinus Torvalds return 0; 39131da177e4SLinus Torvalds } 39141da177e4SLinus Torvalds 391527c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 39161da177e4SLinus Torvalds { 39174c981e28SIdo Schimmel struct arg_netdev_event arg = { 39188ed67789SDaniel Lezcano .dev = dev, 39196802f3adSIdo Schimmel { 39204c981e28SIdo Schimmel .event = event, 39216802f3adSIdo Schimmel }, 39228ed67789SDaniel Lezcano }; 39238ed67789SDaniel Lezcano 39244c981e28SIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifdown, &arg); 39254c981e28SIdo Schimmel } 39264c981e28SIdo Schimmel 39274c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 39284c981e28SIdo Schimmel { 39294c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 39304c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 39314c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 39321da177e4SLinus Torvalds } 39331da177e4SLinus Torvalds 393495c96174SEric Dumazet struct rt6_mtu_change_arg { 39351da177e4SLinus Torvalds struct net_device *dev; 393695c96174SEric Dumazet unsigned int mtu; 39371da177e4SLinus Torvalds }; 39381da177e4SLinus Torvalds 39391da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 39401da177e4SLinus Torvalds { 39411da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 39421da177e4SLinus Torvalds struct inet6_dev *idev; 39431da177e4SLinus Torvalds 39441da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 39451da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 39461da177e4SLinus Torvalds We still use this lock to block changes 39471da177e4SLinus Torvalds caused by addrconf/ndisc. 39481da177e4SLinus Torvalds */ 39491da177e4SLinus Torvalds 39501da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 395138308473SDavid S. Miller if (!idev) 39521da177e4SLinus Torvalds return 0; 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 39551da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 39561da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 39571da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 39581da177e4SLinus Torvalds */ 3959d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 39604b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3961f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 3962e9fa1495SStefano Brivio if (dst_metric_raw(&rt->dst, RTAX_MTU) && 3963e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, rt, arg->mtu)) 3964defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3965e9fa1495SStefano Brivio rt6_exceptions_update_pmtu(idev, rt, arg->mtu); 3966f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 39674b32b5adSMartin KaFai Lau } 39681da177e4SLinus Torvalds return 0; 39691da177e4SLinus Torvalds } 39701da177e4SLinus Torvalds 397195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 39721da177e4SLinus Torvalds { 3973c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3974c71099acSThomas Graf .dev = dev, 3975c71099acSThomas Graf .mtu = mtu, 3976c71099acSThomas Graf }; 39771da177e4SLinus Torvalds 39780c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 39791da177e4SLinus Torvalds } 39801da177e4SLinus Torvalds 3981ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 39825176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 3983aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 398486872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3985ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 398686872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 398786872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 398851ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3989c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 399019e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 399119e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 399232bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3993622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 39943b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 3995aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 399686872cb5SThomas Graf }; 399786872cb5SThomas Graf 399886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3999333c4301SDavid Ahern struct fib6_config *cfg, 4000333c4301SDavid Ahern struct netlink_ext_ack *extack) 40011da177e4SLinus Torvalds { 400286872cb5SThomas Graf struct rtmsg *rtm; 400386872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4004c78ba6d6SLubomir Rintel unsigned int pref; 400586872cb5SThomas Graf int err; 40061da177e4SLinus Torvalds 4007fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4008fceb6435SJohannes Berg NULL); 400986872cb5SThomas Graf if (err < 0) 401086872cb5SThomas Graf goto errout; 40111da177e4SLinus Torvalds 401286872cb5SThomas Graf err = -EINVAL; 401386872cb5SThomas Graf rtm = nlmsg_data(nlh); 401486872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 401586872cb5SThomas Graf 401686872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 401786872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 401886872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 401986872cb5SThomas Graf cfg->fc_flags = RTF_UP; 402086872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 4021ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 402286872cb5SThomas Graf 4023ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4024ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4025b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4026b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 402786872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 402886872cb5SThomas Graf 4029ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4030ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4031ab79ad14SMaciej Żenczykowski 40321f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 40331f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 40341f56a01fSMartin KaFai Lau 4035fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4036fc1e64e1SDavid Ahern 403715e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 403886872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 40393b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 404086872cb5SThomas Graf 404186872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 404267b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 404386872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 40441da177e4SLinus Torvalds } 404586872cb5SThomas Graf 404686872cb5SThomas Graf if (tb[RTA_DST]) { 404786872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 404886872cb5SThomas Graf 404986872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 405086872cb5SThomas Graf goto errout; 405186872cb5SThomas Graf 405286872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 40531da177e4SLinus Torvalds } 405486872cb5SThomas Graf 405586872cb5SThomas Graf if (tb[RTA_SRC]) { 405686872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 405786872cb5SThomas Graf 405886872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 405986872cb5SThomas Graf goto errout; 406086872cb5SThomas Graf 406186872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 40621da177e4SLinus Torvalds } 406386872cb5SThomas Graf 4064c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 406567b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4066c3968a85SDaniel Walter 406786872cb5SThomas Graf if (tb[RTA_OIF]) 406886872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 406986872cb5SThomas Graf 407086872cb5SThomas Graf if (tb[RTA_PRIORITY]) 407186872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 407286872cb5SThomas Graf 407386872cb5SThomas Graf if (tb[RTA_METRICS]) { 407486872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 407586872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 40761da177e4SLinus Torvalds } 407786872cb5SThomas Graf 407886872cb5SThomas Graf if (tb[RTA_TABLE]) 407986872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 408086872cb5SThomas Graf 408151ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 408251ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 408351ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 40849ed59592SDavid Ahern 40859ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4086c255bd68SDavid Ahern cfg->fc_mp_len, extack); 40879ed59592SDavid Ahern if (err < 0) 40889ed59592SDavid Ahern goto errout; 408951ebd318SNicolas Dichtel } 409051ebd318SNicolas Dichtel 4091c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4092c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4093c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4094c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4095c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4096c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4097c78ba6d6SLubomir Rintel } 4098c78ba6d6SLubomir Rintel 409919e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 410019e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 410119e42e45SRoopa Prabhu 41029ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 410319e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 410419e42e45SRoopa Prabhu 4105c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 41069ed59592SDavid Ahern if (err < 0) 41079ed59592SDavid Ahern goto errout; 41089ed59592SDavid Ahern } 41099ed59592SDavid Ahern 411032bc201eSXin Long if (tb[RTA_EXPIRES]) { 411132bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 411232bc201eSXin Long 411332bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 411432bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 411532bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 411632bc201eSXin Long } 411732bc201eSXin Long } 411832bc201eSXin Long 411986872cb5SThomas Graf err = 0; 412086872cb5SThomas Graf errout: 412186872cb5SThomas Graf return err; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41246b9ea5a6SRoopa Prabhu struct rt6_nh { 41256b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 41266b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 41276b9ea5a6SRoopa Prabhu struct mx6_config mxc; 41286b9ea5a6SRoopa Prabhu struct list_head next; 41296b9ea5a6SRoopa Prabhu }; 41306b9ea5a6SRoopa Prabhu 41316b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 41326b9ea5a6SRoopa Prabhu { 41336b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 41346b9ea5a6SRoopa Prabhu 41356b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 41367d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 41376b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 41386b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 41396b9ea5a6SRoopa Prabhu } 41406b9ea5a6SRoopa Prabhu } 41416b9ea5a6SRoopa Prabhu 41426b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 41436b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 41446b9ea5a6SRoopa Prabhu { 41456b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 41466b9ea5a6SRoopa Prabhu int err = -EEXIST; 41476b9ea5a6SRoopa Prabhu 41486b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 41496b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 4150f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 41516b9ea5a6SRoopa Prabhu return err; 41526b9ea5a6SRoopa Prabhu } 41536b9ea5a6SRoopa Prabhu 41546b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 41556b9ea5a6SRoopa Prabhu if (!nh) 41566b9ea5a6SRoopa Prabhu return -ENOMEM; 41576b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 41586b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 41596b9ea5a6SRoopa Prabhu if (err) { 41606b9ea5a6SRoopa Prabhu kfree(nh); 41616b9ea5a6SRoopa Prabhu return err; 41626b9ea5a6SRoopa Prabhu } 41636b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 41646b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 41656b9ea5a6SRoopa Prabhu 41666b9ea5a6SRoopa Prabhu return 0; 41676b9ea5a6SRoopa Prabhu } 41686b9ea5a6SRoopa Prabhu 41693b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 41703b1137feSDavid Ahern struct rt6_info *rt_last, 41713b1137feSDavid Ahern struct nl_info *info, 41723b1137feSDavid Ahern __u16 nlflags) 41733b1137feSDavid Ahern { 41743b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 41753b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 41763b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 41773b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 41783b1137feSDavid Ahern * the list, find the first sibling of the last route appended 41793b1137feSDavid Ahern */ 41803b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 41813b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 41823b1137feSDavid Ahern struct rt6_info, 41833b1137feSDavid Ahern rt6i_siblings); 41843b1137feSDavid Ahern } 41853b1137feSDavid Ahern 41863b1137feSDavid Ahern if (rt) 41873b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 41883b1137feSDavid Ahern } 41893b1137feSDavid Ahern 4190333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4191333c4301SDavid Ahern struct netlink_ext_ack *extack) 419251ebd318SNicolas Dichtel { 41933b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 41943b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 419551ebd318SNicolas Dichtel struct fib6_config r_cfg; 419651ebd318SNicolas Dichtel struct rtnexthop *rtnh; 41976b9ea5a6SRoopa Prabhu struct rt6_info *rt; 41986b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 41996b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 42003b1137feSDavid Ahern __u16 nlflags; 420151ebd318SNicolas Dichtel int remaining; 420251ebd318SNicolas Dichtel int attrlen; 42036b9ea5a6SRoopa Prabhu int err = 1; 42046b9ea5a6SRoopa Prabhu int nhn = 0; 42056b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 42066b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 42076b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 420851ebd318SNicolas Dichtel 42093b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 42103b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 42113b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 42123b1137feSDavid Ahern 421335f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 421451ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 421551ebd318SNicolas Dichtel 42166b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 42176b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 42186b9ea5a6SRoopa Prabhu */ 421951ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 422051ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 422151ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 422251ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 422351ebd318SNicolas Dichtel 422451ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 422551ebd318SNicolas Dichtel if (attrlen > 0) { 422651ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 422751ebd318SNicolas Dichtel 422851ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 422951ebd318SNicolas Dichtel if (nla) { 423067b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 423151ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 423251ebd318SNicolas Dichtel } 423319e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 423419e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 423519e42e45SRoopa Prabhu if (nla) 423619e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 423751ebd318SNicolas Dichtel } 42386b9ea5a6SRoopa Prabhu 423968e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 4240333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 42418c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 42428c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 42438c5b83f0SRoopa Prabhu rt = NULL; 42446b9ea5a6SRoopa Prabhu goto cleanup; 42458c5b83f0SRoopa Prabhu } 42466b9ea5a6SRoopa Prabhu 4247398958aeSIdo Schimmel rt->rt6i_nh_weight = rtnh->rtnh_hops + 1; 4248398958aeSIdo Schimmel 42496b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 425051ebd318SNicolas Dichtel if (err) { 4251587fea74SWei Wang dst_release_immediate(&rt->dst); 42526b9ea5a6SRoopa Prabhu goto cleanup; 425351ebd318SNicolas Dichtel } 42546b9ea5a6SRoopa Prabhu 42556b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 425651ebd318SNicolas Dichtel } 42576b9ea5a6SRoopa Prabhu 42583b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 42593b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 42603b1137feSDavid Ahern * the full route when done 42613b1137feSDavid Ahern */ 42623b1137feSDavid Ahern info->skip_notify = 1; 42633b1137feSDavid Ahern 42646b9ea5a6SRoopa Prabhu err_nh = NULL; 42656b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 42663b1137feSDavid Ahern rt_last = nh->rt6_info; 4267333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 42683b1137feSDavid Ahern /* save reference to first route for notification */ 42693b1137feSDavid Ahern if (!rt_notif && !err) 42703b1137feSDavid Ahern rt_notif = nh->rt6_info; 42713b1137feSDavid Ahern 42726b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 42736b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 42746b9ea5a6SRoopa Prabhu if (err) { 42756b9ea5a6SRoopa Prabhu if (replace && nhn) 42766b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 42776b9ea5a6SRoopa Prabhu err_nh = nh; 42786b9ea5a6SRoopa Prabhu goto add_errout; 42796b9ea5a6SRoopa Prabhu } 42806b9ea5a6SRoopa Prabhu 42811a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 428227596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 428327596472SMichal Kubeček * we have already failed to add the first nexthop: 428427596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 428527596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 428627596472SMichal Kubeček * be added to it. 42871a72418bSNicolas Dichtel */ 428827596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 428927596472SMichal Kubeček NLM_F_REPLACE); 42906b9ea5a6SRoopa Prabhu nhn++; 42916b9ea5a6SRoopa Prabhu } 42926b9ea5a6SRoopa Prabhu 42933b1137feSDavid Ahern /* success ... tell user about new route */ 42943b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 42956b9ea5a6SRoopa Prabhu goto cleanup; 42966b9ea5a6SRoopa Prabhu 42976b9ea5a6SRoopa Prabhu add_errout: 42983b1137feSDavid Ahern /* send notification for routes that were added so that 42993b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 43003b1137feSDavid Ahern * coherent 43013b1137feSDavid Ahern */ 43023b1137feSDavid Ahern if (rt_notif) 43033b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 43043b1137feSDavid Ahern 43056b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 43066b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 43076b9ea5a6SRoopa Prabhu if (err_nh == nh) 43086b9ea5a6SRoopa Prabhu break; 4309333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 43106b9ea5a6SRoopa Prabhu } 43116b9ea5a6SRoopa Prabhu 43126b9ea5a6SRoopa Prabhu cleanup: 43136b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 4314587fea74SWei Wang if (nh->rt6_info) 4315587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 43166b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 43176b9ea5a6SRoopa Prabhu list_del(&nh->next); 43186b9ea5a6SRoopa Prabhu kfree(nh); 43196b9ea5a6SRoopa Prabhu } 43206b9ea5a6SRoopa Prabhu 43216b9ea5a6SRoopa Prabhu return err; 43226b9ea5a6SRoopa Prabhu } 43236b9ea5a6SRoopa Prabhu 4324333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 4325333c4301SDavid Ahern struct netlink_ext_ack *extack) 43266b9ea5a6SRoopa Prabhu { 43276b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 43286b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 43296b9ea5a6SRoopa Prabhu int remaining; 43306b9ea5a6SRoopa Prabhu int attrlen; 43316b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 43326b9ea5a6SRoopa Prabhu 43336b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 43346b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 43356b9ea5a6SRoopa Prabhu 43366b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 43376b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 43386b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 43396b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 43406b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 43416b9ea5a6SRoopa Prabhu 43426b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 43436b9ea5a6SRoopa Prabhu if (attrlen > 0) { 43446b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 43456b9ea5a6SRoopa Prabhu 43466b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 43476b9ea5a6SRoopa Prabhu if (nla) { 43486b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 43496b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 43506b9ea5a6SRoopa Prabhu } 43516b9ea5a6SRoopa Prabhu } 4352333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 43536b9ea5a6SRoopa Prabhu if (err) 43546b9ea5a6SRoopa Prabhu last_err = err; 43556b9ea5a6SRoopa Prabhu 435651ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 435751ebd318SNicolas Dichtel } 435851ebd318SNicolas Dichtel 435951ebd318SNicolas Dichtel return last_err; 436051ebd318SNicolas Dichtel } 436151ebd318SNicolas Dichtel 4362c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4363c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 43641da177e4SLinus Torvalds { 436586872cb5SThomas Graf struct fib6_config cfg; 436686872cb5SThomas Graf int err; 43671da177e4SLinus Torvalds 4368333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 436986872cb5SThomas Graf if (err < 0) 437086872cb5SThomas Graf return err; 437186872cb5SThomas Graf 437251ebd318SNicolas Dichtel if (cfg.fc_mp) 4373333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 43740ae81335SDavid Ahern else { 43750ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 4376333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 43771da177e4SLinus Torvalds } 43780ae81335SDavid Ahern } 43791da177e4SLinus Torvalds 4380c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4381c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 43821da177e4SLinus Torvalds { 438386872cb5SThomas Graf struct fib6_config cfg; 438486872cb5SThomas Graf int err; 43851da177e4SLinus Torvalds 4386333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 438786872cb5SThomas Graf if (err < 0) 438886872cb5SThomas Graf return err; 438986872cb5SThomas Graf 439051ebd318SNicolas Dichtel if (cfg.fc_mp) 4391333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 439251ebd318SNicolas Dichtel else 4393333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 43941da177e4SLinus Torvalds } 43951da177e4SLinus Torvalds 4396beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 4397339bf98fSThomas Graf { 4398beb1afacSDavid Ahern int nexthop_len = 0; 4399beb1afacSDavid Ahern 4400beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4401beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 4402beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 4403beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 4404beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 4405beb1afacSDavid Ahern 4406beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 4407beb1afacSDavid Ahern } 4408beb1afacSDavid Ahern 4409339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 4410339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 4411339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 4412339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 4413339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 4414339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 4415339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 4416339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 4417339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 44186a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 4419ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 4420c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 442119e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 4422beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 4423beb1afacSDavid Ahern + nexthop_len; 4424beb1afacSDavid Ahern } 4425beb1afacSDavid Ahern 4426beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 44275be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 4428beb1afacSDavid Ahern { 4429f9d882eaSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 4430f9d882eaSIdo Schimmel *flags |= RTNH_F_DEAD; 4431f9d882eaSIdo Schimmel 443244c9f2f2SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_LINKDOWN) { 4433beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 4434beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 4435beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 4436beb1afacSDavid Ahern } 4437beb1afacSDavid Ahern 4438beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 4439beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 4440beb1afacSDavid Ahern goto nla_put_failure; 4441beb1afacSDavid Ahern } 4442beb1afacSDavid Ahern 4443fc1e64e1SDavid Ahern *flags |= (rt->rt6i_nh_flags & RTNH_F_ONLINK); 4444fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 444561e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 444661e4d01eSIdo Schimmel 44475be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 44485be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 4449beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 4450beb1afacSDavid Ahern goto nla_put_failure; 4451beb1afacSDavid Ahern 4452beb1afacSDavid Ahern if (rt->dst.lwtstate && 4453beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 4454beb1afacSDavid Ahern goto nla_put_failure; 4455beb1afacSDavid Ahern 4456beb1afacSDavid Ahern return 0; 4457beb1afacSDavid Ahern 4458beb1afacSDavid Ahern nla_put_failure: 4459beb1afacSDavid Ahern return -EMSGSIZE; 4460beb1afacSDavid Ahern } 4461beb1afacSDavid Ahern 44625be083ceSDavid Ahern /* add multipath next hop */ 4463beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 4464beb1afacSDavid Ahern { 4465beb1afacSDavid Ahern struct rtnexthop *rtnh; 4466beb1afacSDavid Ahern unsigned int flags = 0; 4467beb1afacSDavid Ahern 4468beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 4469beb1afacSDavid Ahern if (!rtnh) 4470beb1afacSDavid Ahern goto nla_put_failure; 4471beb1afacSDavid Ahern 4472398958aeSIdo Schimmel rtnh->rtnh_hops = rt->rt6i_nh_weight - 1; 4473beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 4474beb1afacSDavid Ahern 44755be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 4476beb1afacSDavid Ahern goto nla_put_failure; 4477beb1afacSDavid Ahern 4478beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 4479beb1afacSDavid Ahern 4480beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4481beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4482beb1afacSDavid Ahern 4483beb1afacSDavid Ahern return 0; 4484beb1afacSDavid Ahern 4485beb1afacSDavid Ahern nla_put_failure: 4486beb1afacSDavid Ahern return -EMSGSIZE; 4487339bf98fSThomas Graf } 4488339bf98fSThomas Graf 4489191cd582SBrian Haley static int rt6_fill_node(struct net *net, 4490191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 44910d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 449215e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4493f8cfe2ceSDavid Ahern unsigned int flags) 44941da177e4SLinus Torvalds { 44954b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 44961da177e4SLinus Torvalds struct rtmsg *rtm; 44971da177e4SLinus Torvalds struct nlmsghdr *nlh; 4498e3703b3dSThomas Graf long expires; 44999e762a4aSPatrick McHardy u32 table; 45001da177e4SLinus Torvalds 450115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 450238308473SDavid S. Miller if (!nlh) 450326932566SPatrick McHardy return -EMSGSIZE; 45042d7202bfSThomas Graf 45052d7202bfSThomas Graf rtm = nlmsg_data(nlh); 45061da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 45071da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 45081da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 45091da177e4SLinus Torvalds rtm->rtm_tos = 0; 4510c71099acSThomas Graf if (rt->rt6i_table) 45119e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 4512c71099acSThomas Graf else 45139e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 45149e762a4aSPatrick McHardy rtm->rtm_table = table; 4515c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4516c78679e8SDavid S. Miller goto nla_put_failure; 4517ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 4518ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4519ef2c7d7bSNicolas Dichtel case -EINVAL: 4520ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4521ef2c7d7bSNicolas Dichtel break; 4522ef2c7d7bSNicolas Dichtel case -EACCES: 4523ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4524ef2c7d7bSNicolas Dichtel break; 4525b4949ab2SNicolas Dichtel case -EAGAIN: 4526b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4527b4949ab2SNicolas Dichtel break; 4528ef2c7d7bSNicolas Dichtel default: 45291da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4530ef2c7d7bSNicolas Dichtel break; 4531ef2c7d7bSNicolas Dichtel } 4532ef2c7d7bSNicolas Dichtel } 4533ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4534ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 45354ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 45364ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4537d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 45381da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 45391da177e4SLinus Torvalds else 45401da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 45411da177e4SLinus Torvalds rtm->rtm_flags = 0; 45421da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 45431da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 45441da177e4SLinus Torvalds 45451da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 45461da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 45471da177e4SLinus Torvalds 45481da177e4SLinus Torvalds if (dst) { 4549930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4550c78679e8SDavid S. Miller goto nla_put_failure; 45511da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 45521da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4553930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4554c78679e8SDavid S. Miller goto nla_put_failure; 45551da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 45561da177e4SLinus Torvalds if (src) { 4557930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4558c78679e8SDavid S. Miller goto nla_put_failure; 45591da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4560c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4561930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4562c78679e8SDavid S. Miller goto nla_put_failure; 45631da177e4SLinus Torvalds #endif 45647bc570c8SYOSHIFUJI Hideaki if (iif) { 45657bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 45667bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4567fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 45682cf75070SNikolay Aleksandrov 45697bc570c8SYOSHIFUJI Hideaki if (err == 0) 45707bc570c8SYOSHIFUJI Hideaki return 0; 4571fd61c6baSDavid Ahern if (err < 0) 45727bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 45737bc570c8SYOSHIFUJI Hideaki } else 45747bc570c8SYOSHIFUJI Hideaki #endif 4575c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4576c78679e8SDavid S. Miller goto nla_put_failure; 45777bc570c8SYOSHIFUJI Hideaki } else if (dst) { 45781da177e4SLinus Torvalds struct in6_addr saddr_buf; 4579c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4580930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4581c78679e8SDavid S. Miller goto nla_put_failure; 4582c3968a85SDaniel Walter } 4583c3968a85SDaniel Walter 4584c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4585c3968a85SDaniel Walter struct in6_addr saddr_buf; 45864e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4587930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4588c78679e8SDavid S. Miller goto nla_put_failure; 45891da177e4SLinus Torvalds } 45902d7202bfSThomas Graf 45914b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 45924b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 45934b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 45944b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 45952d7202bfSThomas Graf goto nla_put_failure; 45962d7202bfSThomas Graf 4597beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4598beb1afacSDavid Ahern goto nla_put_failure; 4599beb1afacSDavid Ahern 4600beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4601beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4602beb1afacSDavid Ahern */ 4603beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4604beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4605beb1afacSDavid Ahern struct nlattr *mp; 4606beb1afacSDavid Ahern 4607beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4608beb1afacSDavid Ahern if (!mp) 4609beb1afacSDavid Ahern goto nla_put_failure; 4610beb1afacSDavid Ahern 4611beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4612beb1afacSDavid Ahern goto nla_put_failure; 4613beb1afacSDavid Ahern 4614beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4615beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4616beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 461794f826b8SEric Dumazet goto nla_put_failure; 461894f826b8SEric Dumazet } 46192d7202bfSThomas Graf 4620beb1afacSDavid Ahern nla_nest_end(skb, mp); 4621beb1afacSDavid Ahern } else { 46225be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4623c78679e8SDavid S. Miller goto nla_put_failure; 4624beb1afacSDavid Ahern } 46258253947eSLi Wei 46268253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 462769cdf8f9SYOSHIFUJI Hideaki 462887a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4629e3703b3dSThomas Graf goto nla_put_failure; 46301da177e4SLinus Torvalds 4631c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4632c78ba6d6SLubomir Rintel goto nla_put_failure; 4633c78ba6d6SLubomir Rintel 463419e42e45SRoopa Prabhu 4635053c095aSJohannes Berg nlmsg_end(skb, nlh); 4636053c095aSJohannes Berg return 0; 46372d7202bfSThomas Graf 46382d7202bfSThomas Graf nla_put_failure: 463926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 464026932566SPatrick McHardy return -EMSGSIZE; 46411da177e4SLinus Torvalds } 46421da177e4SLinus Torvalds 46431b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 46441da177e4SLinus Torvalds { 46451da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 46461f17e2f2SDavid Ahern struct net *net = arg->net; 46471f17e2f2SDavid Ahern 46481f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 46491f17e2f2SDavid Ahern return 0; 46501da177e4SLinus Torvalds 46512d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 46522d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4653f8cfe2ceSDavid Ahern 4654f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4655f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4656f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4657f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4658f8cfe2ceSDavid Ahern return 1; 4659f8cfe2ceSDavid Ahern } 4660f8cfe2ceSDavid Ahern } 46611da177e4SLinus Torvalds 46621f17e2f2SDavid Ahern return rt6_fill_node(net, 4663191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 466415e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4665f8cfe2ceSDavid Ahern NLM_F_MULTI); 46661da177e4SLinus Torvalds } 46671da177e4SLinus Torvalds 4668c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4669c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46701da177e4SLinus Torvalds { 46713b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4672ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 467318c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 467418c3a61cSRoopa Prabhu struct dst_entry *dst; 46751da177e4SLinus Torvalds struct rt6_info *rt; 4676ab364a6fSThomas Graf struct sk_buff *skb; 4677ab364a6fSThomas Graf struct rtmsg *rtm; 46784c9483b2SDavid S. Miller struct flowi6 fl6; 467918c3a61cSRoopa Prabhu bool fibmatch; 4680ab364a6fSThomas Graf 4681fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4682c21ef3e3SDavid Ahern extack); 4683ab364a6fSThomas Graf if (err < 0) 4684ab364a6fSThomas Graf goto errout; 4685ab364a6fSThomas Graf 4686ab364a6fSThomas Graf err = -EINVAL; 46874c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 468838b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 468938b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 469018c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4691ab364a6fSThomas Graf 4692ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4693ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4694ab364a6fSThomas Graf goto errout; 4695ab364a6fSThomas Graf 46964e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4697ab364a6fSThomas Graf } 4698ab364a6fSThomas Graf 4699ab364a6fSThomas Graf if (tb[RTA_DST]) { 4700ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4701ab364a6fSThomas Graf goto errout; 4702ab364a6fSThomas Graf 47034e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4704ab364a6fSThomas Graf } 4705ab364a6fSThomas Graf 4706ab364a6fSThomas Graf if (tb[RTA_IIF]) 4707ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4708ab364a6fSThomas Graf 4709ab364a6fSThomas Graf if (tb[RTA_OIF]) 471072331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4711ab364a6fSThomas Graf 47122e47b291SLorenzo Colitti if (tb[RTA_MARK]) 47132e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 47142e47b291SLorenzo Colitti 4715622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4716622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4717622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4718622ec2c9SLorenzo Colitti else 4719622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4720622ec2c9SLorenzo Colitti 4721ab364a6fSThomas Graf if (iif) { 4722ab364a6fSThomas Graf struct net_device *dev; 472372331bc0SShmulik Ladkani int flags = 0; 472472331bc0SShmulik Ladkani 4725121622dbSFlorian Westphal rcu_read_lock(); 4726121622dbSFlorian Westphal 4727121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4728ab364a6fSThomas Graf if (!dev) { 4729121622dbSFlorian Westphal rcu_read_unlock(); 4730ab364a6fSThomas Graf err = -ENODEV; 4731ab364a6fSThomas Graf goto errout; 4732ab364a6fSThomas Graf } 473372331bc0SShmulik Ladkani 473472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 473572331bc0SShmulik Ladkani 473672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 473772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 473872331bc0SShmulik Ladkani 4739b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 4740121622dbSFlorian Westphal 4741121622dbSFlorian Westphal rcu_read_unlock(); 474272331bc0SShmulik Ladkani } else { 474372331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 474472331bc0SShmulik Ladkani 474518c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 474618c3a61cSRoopa Prabhu } 474718c3a61cSRoopa Prabhu 474818c3a61cSRoopa Prabhu 474918c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 475018c3a61cSRoopa Prabhu if (rt->dst.error) { 475118c3a61cSRoopa Prabhu err = rt->dst.error; 475218c3a61cSRoopa Prabhu ip6_rt_put(rt); 475318c3a61cSRoopa Prabhu goto errout; 4754ab364a6fSThomas Graf } 47551da177e4SLinus Torvalds 47569d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 47579d6acb3bSWANG Cong err = rt->dst.error; 47589d6acb3bSWANG Cong ip6_rt_put(rt); 47599d6acb3bSWANG Cong goto errout; 47609d6acb3bSWANG Cong } 47619d6acb3bSWANG Cong 4762fba961abSDavid S. Miller if (fibmatch && rt->from) { 4763fba961abSDavid S. Miller struct rt6_info *ort = rt->from; 476458acfd71SIdo Schimmel 476558acfd71SIdo Schimmel dst_hold(&ort->dst); 476658acfd71SIdo Schimmel ip6_rt_put(rt); 476758acfd71SIdo Schimmel rt = ort; 476858acfd71SIdo Schimmel } 476958acfd71SIdo Schimmel 47701da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 477138308473SDavid S. Miller if (!skb) { 477294e187c0SAmerigo Wang ip6_rt_put(rt); 4773ab364a6fSThomas Graf err = -ENOBUFS; 4774ab364a6fSThomas Graf goto errout; 4775ab364a6fSThomas Graf } 47761da177e4SLinus Torvalds 4777d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 477818c3a61cSRoopa Prabhu if (fibmatch) 477918c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 478018c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 478118c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 478218c3a61cSRoopa Prabhu else 47834c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 478415e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4785f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 47861da177e4SLinus Torvalds if (err < 0) { 4787ab364a6fSThomas Graf kfree_skb(skb); 4788ab364a6fSThomas Graf goto errout; 47891da177e4SLinus Torvalds } 47901da177e4SLinus Torvalds 479115e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4792ab364a6fSThomas Graf errout: 47931da177e4SLinus Torvalds return err; 47941da177e4SLinus Torvalds } 47951da177e4SLinus Torvalds 479637a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 479737a1d361SRoopa Prabhu unsigned int nlm_flags) 47981da177e4SLinus Torvalds { 47991da177e4SLinus Torvalds struct sk_buff *skb; 48005578689aSDaniel Lezcano struct net *net = info->nl_net; 4801528c4cebSDenis V. Lunev u32 seq; 4802528c4cebSDenis V. Lunev int err; 48030d51aa80SJamal Hadi Salim 4804528c4cebSDenis V. Lunev err = -ENOBUFS; 480538308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 480686872cb5SThomas Graf 480719e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 480838308473SDavid S. Miller if (!skb) 480921713ebcSThomas Graf goto errout; 48101da177e4SLinus Torvalds 4811191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4812f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 481326932566SPatrick McHardy if (err < 0) { 481426932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 481526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 481626932566SPatrick McHardy kfree_skb(skb); 481726932566SPatrick McHardy goto errout; 481826932566SPatrick McHardy } 481915e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 48205578689aSDaniel Lezcano info->nlh, gfp_any()); 48211ce85fe4SPablo Neira Ayuso return; 482221713ebcSThomas Graf errout: 482321713ebcSThomas Graf if (err < 0) 48245578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 48251da177e4SLinus Torvalds } 48261da177e4SLinus Torvalds 48278ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4828351638e7SJiri Pirko unsigned long event, void *ptr) 48298ed67789SDaniel Lezcano { 4830351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4831c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 48328ed67789SDaniel Lezcano 4833242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4834242d3a49SWANG Cong return NOTIFY_OK; 4835242d3a49SWANG Cong 4836242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4837d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 48388ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 48398ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4840d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 48418ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4842d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 48438ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 48448ed67789SDaniel Lezcano #endif 484576da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 484676da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 484776da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 484876da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 484976da0704SWANG Cong */ 485012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4851242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 485212d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 485312d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4854242d3a49SWANG Cong #endif 48558ed67789SDaniel Lezcano } 48568ed67789SDaniel Lezcano 48578ed67789SDaniel Lezcano return NOTIFY_OK; 48588ed67789SDaniel Lezcano } 48598ed67789SDaniel Lezcano 48601da177e4SLinus Torvalds /* 48611da177e4SLinus Torvalds * /proc 48621da177e4SLinus Torvalds */ 48631da177e4SLinus Torvalds 48641da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 48651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 48661da177e4SLinus Torvalds { 486769ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 48681da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 486969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 487069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 487181eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 487269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 487369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4874fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 487569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 48761da177e4SLinus Torvalds 48771da177e4SLinus Torvalds return 0; 48781da177e4SLinus Torvalds } 48791da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 48801da177e4SLinus Torvalds 48811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 48821da177e4SLinus Torvalds 48831da177e4SLinus Torvalds static 4884fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 48851da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 48861da177e4SLinus Torvalds { 4887c486da34SLucian Adrian Grijincu struct net *net; 4888c486da34SLucian Adrian Grijincu int delay; 4889c486da34SLucian Adrian Grijincu if (!write) 4890c486da34SLucian Adrian Grijincu return -EINVAL; 4891c486da34SLucian Adrian Grijincu 4892c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4893c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 48948d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 48952ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 48961da177e4SLinus Torvalds return 0; 48971da177e4SLinus Torvalds } 48981da177e4SLinus Torvalds 4899fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 49001da177e4SLinus Torvalds { 49011da177e4SLinus Torvalds .procname = "flush", 49024990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 49031da177e4SLinus Torvalds .maxlen = sizeof(int), 490489c8b3a1SDave Jones .mode = 0200, 49056d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 49061da177e4SLinus Torvalds }, 49071da177e4SLinus Torvalds { 49081da177e4SLinus Torvalds .procname = "gc_thresh", 49099a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 49101da177e4SLinus Torvalds .maxlen = sizeof(int), 49111da177e4SLinus Torvalds .mode = 0644, 49126d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49131da177e4SLinus Torvalds }, 49141da177e4SLinus Torvalds { 49151da177e4SLinus Torvalds .procname = "max_size", 49164990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 49171da177e4SLinus Torvalds .maxlen = sizeof(int), 49181da177e4SLinus Torvalds .mode = 0644, 49196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49201da177e4SLinus Torvalds }, 49211da177e4SLinus Torvalds { 49221da177e4SLinus Torvalds .procname = "gc_min_interval", 49234990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 49241da177e4SLinus Torvalds .maxlen = sizeof(int), 49251da177e4SLinus Torvalds .mode = 0644, 49266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 49271da177e4SLinus Torvalds }, 49281da177e4SLinus Torvalds { 49291da177e4SLinus Torvalds .procname = "gc_timeout", 49304990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 49311da177e4SLinus Torvalds .maxlen = sizeof(int), 49321da177e4SLinus Torvalds .mode = 0644, 49336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 49341da177e4SLinus Torvalds }, 49351da177e4SLinus Torvalds { 49361da177e4SLinus Torvalds .procname = "gc_interval", 49374990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 49381da177e4SLinus Torvalds .maxlen = sizeof(int), 49391da177e4SLinus Torvalds .mode = 0644, 49406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 49411da177e4SLinus Torvalds }, 49421da177e4SLinus Torvalds { 49431da177e4SLinus Torvalds .procname = "gc_elasticity", 49444990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 49451da177e4SLinus Torvalds .maxlen = sizeof(int), 49461da177e4SLinus Torvalds .mode = 0644, 4947f3d3f616SMin Zhang .proc_handler = proc_dointvec, 49481da177e4SLinus Torvalds }, 49491da177e4SLinus Torvalds { 49501da177e4SLinus Torvalds .procname = "mtu_expires", 49514990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 49521da177e4SLinus Torvalds .maxlen = sizeof(int), 49531da177e4SLinus Torvalds .mode = 0644, 49546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 49551da177e4SLinus Torvalds }, 49561da177e4SLinus Torvalds { 49571da177e4SLinus Torvalds .procname = "min_adv_mss", 49584990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 49591da177e4SLinus Torvalds .maxlen = sizeof(int), 49601da177e4SLinus Torvalds .mode = 0644, 4961f3d3f616SMin Zhang .proc_handler = proc_dointvec, 49621da177e4SLinus Torvalds }, 49631da177e4SLinus Torvalds { 49641da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 49654990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 49661da177e4SLinus Torvalds .maxlen = sizeof(int), 49671da177e4SLinus Torvalds .mode = 0644, 49686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 49691da177e4SLinus Torvalds }, 4970f8572d8fSEric W. Biederman { } 49711da177e4SLinus Torvalds }; 49721da177e4SLinus Torvalds 49732c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4974760f2d01SDaniel Lezcano { 4975760f2d01SDaniel Lezcano struct ctl_table *table; 4976760f2d01SDaniel Lezcano 4977760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4978760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4979760f2d01SDaniel Lezcano GFP_KERNEL); 49805ee09105SYOSHIFUJI Hideaki 49815ee09105SYOSHIFUJI Hideaki if (table) { 49825ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4983c486da34SLucian Adrian Grijincu table[0].extra1 = net; 498486393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 49855ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 49865ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 49875ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 49885ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 49895ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 49905ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 49915ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 49929c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4993464dc801SEric W. Biederman 4994464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4995464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4996464dc801SEric W. Biederman table[0].procname = NULL; 49975ee09105SYOSHIFUJI Hideaki } 49985ee09105SYOSHIFUJI Hideaki 4999760f2d01SDaniel Lezcano return table; 5000760f2d01SDaniel Lezcano } 50011da177e4SLinus Torvalds #endif 50021da177e4SLinus Torvalds 50032c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 5004cdb18761SDaniel Lezcano { 5005633d424bSPavel Emelyanov int ret = -ENOMEM; 50068ed67789SDaniel Lezcano 500786393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 500886393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 5009f2fc6a54SBenjamin Thery 5010fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 5011fc66f95cSEric Dumazet goto out_ip6_dst_ops; 5012fc66f95cSEric Dumazet 50138ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 50148ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 50158ed67789SDaniel Lezcano GFP_KERNEL); 50168ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 5017fc66f95cSEric Dumazet goto out_ip6_dst_entries; 5018d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 501962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 502062fa8a84SDavid S. Miller ip6_template_metrics, true); 50218ed67789SDaniel Lezcano 50228ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5023feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 50248ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 50258ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 50268ed67789SDaniel Lezcano GFP_KERNEL); 502768fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 502868fffc67SPeter Zijlstra goto out_ip6_null_entry; 5029d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 503062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 503162fa8a84SDavid S. Miller ip6_template_metrics, true); 50328ed67789SDaniel Lezcano 50338ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 50348ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 50358ed67789SDaniel Lezcano GFP_KERNEL); 503668fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 503768fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 5038d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 503962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 504062fa8a84SDavid S. Miller ip6_template_metrics, true); 50418ed67789SDaniel Lezcano #endif 50428ed67789SDaniel Lezcano 5043b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 5044b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 5045b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 5046b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 5047b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 5048b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 5049b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 5050b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 5051b339a47cSPeter Zijlstra 50526891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 50536891a346SBenjamin Thery 50548ed67789SDaniel Lezcano ret = 0; 50558ed67789SDaniel Lezcano out: 50568ed67789SDaniel Lezcano return ret; 5057f2fc6a54SBenjamin Thery 505868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 505968fffc67SPeter Zijlstra out_ip6_prohibit_entry: 506068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 506168fffc67SPeter Zijlstra out_ip6_null_entry: 506268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 506368fffc67SPeter Zijlstra #endif 5064fc66f95cSEric Dumazet out_ip6_dst_entries: 5065fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5066f2fc6a54SBenjamin Thery out_ip6_dst_ops: 5067f2fc6a54SBenjamin Thery goto out; 5068cdb18761SDaniel Lezcano } 5069cdb18761SDaniel Lezcano 50702c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 5071cdb18761SDaniel Lezcano { 50728ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 50738ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 50748ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 50758ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 50768ed67789SDaniel Lezcano #endif 507741bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5078cdb18761SDaniel Lezcano } 5079cdb18761SDaniel Lezcano 5080d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 5081d189634eSThomas Graf { 5082d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5083c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 5084c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 5085*3617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 5086*3617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 5087d189634eSThomas Graf #endif 5088d189634eSThomas Graf return 0; 5089d189634eSThomas Graf } 5090d189634eSThomas Graf 5091d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 5092d189634eSThomas Graf { 5093d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5094ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 5095ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 5096d189634eSThomas Graf #endif 5097d189634eSThomas Graf } 5098d189634eSThomas Graf 5099cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 5100cdb18761SDaniel Lezcano .init = ip6_route_net_init, 5101cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 5102cdb18761SDaniel Lezcano }; 5103cdb18761SDaniel Lezcano 5104c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 5105c3426b47SDavid S. Miller { 5106c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 5107c3426b47SDavid S. Miller 5108c3426b47SDavid S. Miller if (!bp) 5109c3426b47SDavid S. Miller return -ENOMEM; 5110c3426b47SDavid S. Miller inet_peer_base_init(bp); 5111c3426b47SDavid S. Miller net->ipv6.peers = bp; 5112c3426b47SDavid S. Miller return 0; 5113c3426b47SDavid S. Miller } 5114c3426b47SDavid S. Miller 5115c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5116c3426b47SDavid S. Miller { 5117c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5118c3426b47SDavid S. Miller 5119c3426b47SDavid S. Miller net->ipv6.peers = NULL; 512056a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5121c3426b47SDavid S. Miller kfree(bp); 5122c3426b47SDavid S. Miller } 5123c3426b47SDavid S. Miller 51242b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5125c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5126c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 5127c3426b47SDavid S. Miller }; 5128c3426b47SDavid S. Miller 5129d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 5130d189634eSThomas Graf .init = ip6_route_net_init_late, 5131d189634eSThomas Graf .exit = ip6_route_net_exit_late, 5132d189634eSThomas Graf }; 5133d189634eSThomas Graf 51348ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 51358ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 5136242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 51378ed67789SDaniel Lezcano }; 51388ed67789SDaniel Lezcano 51392f460933SWANG Cong void __init ip6_route_init_special_entries(void) 51402f460933SWANG Cong { 51412f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 51422f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 51432f460933SWANG Cong * manually for init_net */ 51442f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 51452f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 51462f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 51472f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 51482f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 51492f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 51502f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 51512f460933SWANG Cong #endif 51522f460933SWANG Cong } 51532f460933SWANG Cong 5154433d49c3SDaniel Lezcano int __init ip6_route_init(void) 51551da177e4SLinus Torvalds { 5156433d49c3SDaniel Lezcano int ret; 51578d0b94afSMartin KaFai Lau int cpu; 5158433d49c3SDaniel Lezcano 51599a7ec3a9SDaniel Lezcano ret = -ENOMEM; 51609a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 51619a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 51629a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 51639a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 5164c19a28e1SFernando Carrijo goto out; 516514e50e57SDavid S. Miller 5166fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 51678ed67789SDaniel Lezcano if (ret) 5168bdb3289fSDaniel Lezcano goto out_kmem_cache; 5169bdb3289fSDaniel Lezcano 5170c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 5171c3426b47SDavid S. Miller if (ret) 5172e8803b6cSDavid S. Miller goto out_dst_entries; 51732a0c451aSThomas Graf 51747e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 51757e52b33bSDavid S. Miller if (ret) 51767e52b33bSDavid S. Miller goto out_register_inetpeer; 5177c3426b47SDavid S. Miller 51785dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 51795dc121e9SArnaud Ebalard 5180e8803b6cSDavid S. Miller ret = fib6_init(); 5181433d49c3SDaniel Lezcano if (ret) 51828ed67789SDaniel Lezcano goto out_register_subsys; 5183433d49c3SDaniel Lezcano 5184433d49c3SDaniel Lezcano ret = xfrm6_init(); 5185433d49c3SDaniel Lezcano if (ret) 5186e8803b6cSDavid S. Miller goto out_fib6_init; 5187c35b7e72SDaniel Lezcano 5188433d49c3SDaniel Lezcano ret = fib6_rules_init(); 5189433d49c3SDaniel Lezcano if (ret) 5190433d49c3SDaniel Lezcano goto xfrm6_init; 51917e5449c2SDaniel Lezcano 5192d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 5193d189634eSThomas Graf if (ret) 5194d189634eSThomas Graf goto fib6_rules_init; 5195d189634eSThomas Graf 519616feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 519716feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 519816feebcfSFlorian Westphal if (ret < 0) 519916feebcfSFlorian Westphal goto out_register_late_subsys; 520016feebcfSFlorian Westphal 520116feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 520216feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 520316feebcfSFlorian Westphal if (ret < 0) 520416feebcfSFlorian Westphal goto out_register_late_subsys; 520516feebcfSFlorian Westphal 520616feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 520716feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 520816feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 520916feebcfSFlorian Westphal if (ret < 0) 5210d189634eSThomas Graf goto out_register_late_subsys; 5211433d49c3SDaniel Lezcano 52128ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 5213cdb18761SDaniel Lezcano if (ret) 5214d189634eSThomas Graf goto out_register_late_subsys; 52158ed67789SDaniel Lezcano 52168d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 52178d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 52188d0b94afSMartin KaFai Lau 52198d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 52208d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 52218d0b94afSMartin KaFai Lau } 52228d0b94afSMartin KaFai Lau 5223433d49c3SDaniel Lezcano out: 5224433d49c3SDaniel Lezcano return ret; 5225433d49c3SDaniel Lezcano 5226d189634eSThomas Graf out_register_late_subsys: 522716feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 5228d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5229433d49c3SDaniel Lezcano fib6_rules_init: 5230433d49c3SDaniel Lezcano fib6_rules_cleanup(); 5231433d49c3SDaniel Lezcano xfrm6_init: 5232433d49c3SDaniel Lezcano xfrm6_fini(); 52332a0c451aSThomas Graf out_fib6_init: 52342a0c451aSThomas Graf fib6_gc_cleanup(); 52358ed67789SDaniel Lezcano out_register_subsys: 52368ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 52377e52b33bSDavid S. Miller out_register_inetpeer: 52387e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 5239fc66f95cSEric Dumazet out_dst_entries: 5240fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 5241433d49c3SDaniel Lezcano out_kmem_cache: 5242f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 5243433d49c3SDaniel Lezcano goto out; 52441da177e4SLinus Torvalds } 52451da177e4SLinus Torvalds 52461da177e4SLinus Torvalds void ip6_route_cleanup(void) 52471da177e4SLinus Torvalds { 52488ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 5249d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5250101367c2SThomas Graf fib6_rules_cleanup(); 52511da177e4SLinus Torvalds xfrm6_fini(); 52521da177e4SLinus Torvalds fib6_gc_cleanup(); 5253c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 52548ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 525541bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 5256f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 52571da177e4SLinus Torvalds } 5258