12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * FIB front-end. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds /* Changes: 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 131da177e4SLinus Torvalds * reworked default router selection. 141da177e4SLinus Torvalds * - respect outgoing interface 151da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 161da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 171da177e4SLinus Torvalds * - always select the same router if it is (probably) 181da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 19c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 20c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 24f3213831SJoe Perches 254fc268d2SRandy Dunlap #include <linux/capability.h> 261da177e4SLinus Torvalds #include <linux/errno.h> 27bc3b2d7fSPaul Gortmaker #include <linux/export.h> 281da177e4SLinus Torvalds #include <linux/types.h> 291da177e4SLinus Torvalds #include <linux/times.h> 301da177e4SLinus Torvalds #include <linux/socket.h> 311da177e4SLinus Torvalds #include <linux/sockios.h> 321da177e4SLinus Torvalds #include <linux/net.h> 331da177e4SLinus Torvalds #include <linux/route.h> 341da177e4SLinus Torvalds #include <linux/netdevice.h> 351da177e4SLinus Torvalds #include <linux/in6.h> 367bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 371da177e4SLinus Torvalds #include <linux/init.h> 381da177e4SLinus Torvalds #include <linux/if_arp.h> 391da177e4SLinus Torvalds #include <linux/proc_fs.h> 401da177e4SLinus Torvalds #include <linux/seq_file.h> 415b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 4335732d01SWei Wang #include <linux/jhash.h> 44457c4cbcSEric W. Biederman #include <net/net_namespace.h> 451da177e4SLinus Torvalds #include <net/snmp.h> 461da177e4SLinus Torvalds #include <net/ipv6.h> 471da177e4SLinus Torvalds #include <net/ip6_fib.h> 481da177e4SLinus Torvalds #include <net/ip6_route.h> 491da177e4SLinus Torvalds #include <net/ndisc.h> 501da177e4SLinus Torvalds #include <net/addrconf.h> 511da177e4SLinus Torvalds #include <net/tcp.h> 521da177e4SLinus Torvalds #include <linux/rtnetlink.h> 531da177e4SLinus Torvalds #include <net/dst.h> 54904af04dSJiri Benc #include <net/dst_metadata.h> 551da177e4SLinus Torvalds #include <net/xfrm.h> 568d71740cSTom Tucker #include <net/netevent.h> 5721713ebcSThomas Graf #include <net/netlink.h> 583c618c1dSDavid Ahern #include <net/rtnh.h> 5919e42e45SRoopa Prabhu #include <net/lwtunnel.h> 60904af04dSJiri Benc #include <net/ip_tunnels.h> 61ca254490SDavid Ahern #include <net/l3mdev.h> 62eacb9384SRoopa Prabhu #include <net/ip.h> 637c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 661da177e4SLinus Torvalds #include <linux/sysctl.h> 671da177e4SLinus Torvalds #endif 681da177e4SLinus Torvalds 6930d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type); 7030d444d3SDavid Ahern 7130d444d3SDavid Ahern #define CREATE_TRACE_POINTS 7230d444d3SDavid Ahern #include <trace/events/fib6.h> 7330d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); 7430d444d3SDavid Ahern #undef CREATE_TRACE_POINTS 7530d444d3SDavid Ahern 76afc154e9SHannes Frederic Sowa enum rt6_nud_state { 777e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 787e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 797e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 80afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 81afc154e9SHannes Frederic Sowa }; 82afc154e9SHannes Frederic Sowa 831da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 840dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 85ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 861da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 881da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 891da177e4SLinus Torvalds struct net_device *dev, int how); 90569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 93ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 947150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 95ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 961da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 976700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 986700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 996700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 1006700c270SDavid S. Miller struct sk_buff *skb); 101702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 102702cea56SDavid Ahern int strict); 103a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i); 104d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1058d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 106d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 10716a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10816a16cd3SDavid Ahern unsigned int flags); 1097e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 110510e2cedSWei Wang const struct in6_addr *daddr, 111510e2cedSWei Wang const struct in6_addr *saddr); 1121da177e4SLinus Torvalds 11370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1148d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 115b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 116830218c1SDavid Ahern const struct in6_addr *gwaddr, 117830218c1SDavid Ahern struct net_device *dev, 11895c96174SEric Dumazet unsigned int pref); 1198d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 120b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 121830218c1SDavid Ahern const struct in6_addr *gwaddr, 122830218c1SDavid Ahern struct net_device *dev); 12370ceb4f5SYOSHIFUJI Hideaki #endif 12470ceb4f5SYOSHIFUJI Hideaki 1258d0b94afSMartin KaFai Lau struct uncached_list { 1268d0b94afSMartin KaFai Lau spinlock_t lock; 1278d0b94afSMartin KaFai Lau struct list_head head; 1288d0b94afSMartin KaFai Lau }; 1298d0b94afSMartin KaFai Lau 1308d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1318d0b94afSMartin KaFai Lau 132510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1338d0b94afSMartin KaFai Lau { 1348d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1358d0b94afSMartin KaFai Lau 1368d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1378d0b94afSMartin KaFai Lau 1388d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1398d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1408d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1418d0b94afSMartin KaFai Lau } 1428d0b94afSMartin KaFai Lau 143510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1448d0b94afSMartin KaFai Lau { 1458d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1468d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 14781eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1488d0b94afSMartin KaFai Lau 1498d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1508d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15181eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1528d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1538d0b94afSMartin KaFai Lau } 1548d0b94afSMartin KaFai Lau } 1558d0b94afSMartin KaFai Lau 1568d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1578d0b94afSMartin KaFai Lau { 1588d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1598d0b94afSMartin KaFai Lau int cpu; 1608d0b94afSMartin KaFai Lau 161e332bc67SEric W. Biederman if (dev == loopback_dev) 162e332bc67SEric W. Biederman return; 163e332bc67SEric W. Biederman 1648d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1658d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1668d0b94afSMartin KaFai Lau struct rt6_info *rt; 1678d0b94afSMartin KaFai Lau 1688d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1698d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1708d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1718d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1728d0b94afSMartin KaFai Lau 173e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1748d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1758d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1768d0b94afSMartin KaFai Lau } 1778d0b94afSMartin KaFai Lau 178e332bc67SEric W. Biederman if (rt_dev == dev) { 1798d7017fdSMahesh Bandewar rt->dst.dev = blackhole_netdev; 1808d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1818d0b94afSMartin KaFai Lau dev_put(rt_dev); 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1858d0b94afSMartin KaFai Lau } 1868d0b94afSMartin KaFai Lau } 1878d0b94afSMartin KaFai Lau 188f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 189f894cbf8SDavid S. Miller struct sk_buff *skb, 190f894cbf8SDavid S. Miller const void *daddr) 19139232973SDavid S. Miller { 192a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19339232973SDavid S. Miller return (const void *) p; 194f894cbf8SDavid S. Miller else if (skb) 195f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 19639232973SDavid S. Miller return daddr; 19739232973SDavid S. Miller } 19839232973SDavid S. Miller 199f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 200f8a1b43bSDavid Ahern struct net_device *dev, 201f894cbf8SDavid S. Miller struct sk_buff *skb, 202f894cbf8SDavid S. Miller const void *daddr) 203d3aaeb38SDavid S. Miller { 20439232973SDavid S. Miller struct neighbour *n; 20539232973SDavid S. Miller 206f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 207f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 208f83c7790SDavid S. Miller if (n) 209f83c7790SDavid S. Miller return n; 2107adf3246SStefano Brivio 2117adf3246SStefano Brivio n = neigh_create(&nd_tbl, daddr, dev); 2127adf3246SStefano Brivio return IS_ERR(n) ? NULL : n; 213f8a1b43bSDavid Ahern } 214f8a1b43bSDavid Ahern 215f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 216f8a1b43bSDavid Ahern struct sk_buff *skb, 217f8a1b43bSDavid Ahern const void *daddr) 218f8a1b43bSDavid Ahern { 219f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 220f8a1b43bSDavid Ahern 2212c6b55f4SNicolas Dichtel return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), 2222c6b55f4SNicolas Dichtel dst->dev, skb, daddr); 223f83c7790SDavid S. Miller } 224f83c7790SDavid S. Miller 22563fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22663fca65dSJulian Anastasov { 22763fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22863fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 22963fca65dSJulian Anastasov 230f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); 23163fca65dSJulian Anastasov if (!daddr) 23263fca65dSJulian Anastasov return; 23363fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23463fca65dSJulian Anastasov return; 23563fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23863fca65dSJulian Anastasov } 23963fca65dSJulian Anastasov 2409a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2411da177e4SLinus Torvalds .family = AF_INET6, 2421da177e4SLinus Torvalds .gc = ip6_dst_gc, 2431da177e4SLinus Torvalds .gc_thresh = 1024, 2441da177e4SLinus Torvalds .check = ip6_dst_check, 2450dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 246ebb762f2SSteffen Klassert .mtu = ip6_mtu, 247d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2481da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2491da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2501da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2511da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2521da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2536e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2549f8955ccSEric W. Biederman .local_out = __ip6_local_out, 255f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 25663fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2571da177e4SLinus Torvalds }; 2581da177e4SLinus Torvalds 259ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 260ec831ea7SRoland Dreier { 261618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 262618f9bc7SSteffen Klassert 263618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 264ec831ea7SRoland Dreier } 265ec831ea7SRoland Dreier 2666700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2676700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 26814e50e57SDavid S. Miller { 26914e50e57SDavid S. Miller } 27014e50e57SDavid S. Miller 2716700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2726700c270SDavid S. Miller struct sk_buff *skb) 273b587ee3bSDavid S. Miller { 274b587ee3bSDavid S. Miller } 275b587ee3bSDavid S. Miller 27614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27714e50e57SDavid S. Miller .family = AF_INET6, 27814e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 27914e50e57SDavid S. Miller .check = ip6_dst_check, 280ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 281214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28214e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 283b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2840a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 285f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 28614e50e57SDavid S. Miller }; 28714e50e57SDavid S. Miller 28862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 28914edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29062fa8a84SDavid S. Miller }; 29162fa8a84SDavid S. Miller 2928d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 29393c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 29493c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 29593c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 296f05713e0SEric Dumazet .fib6_ref = REFCOUNT_INIT(1), 297421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 298421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 299421842edSDavid Ahern }; 300421842edSDavid Ahern 301fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 3021da177e4SLinus Torvalds .dst = { 3031da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3041da177e4SLinus Torvalds .__use = 1, 3052c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3061da177e4SLinus Torvalds .error = -ENETUNREACH, 3071da177e4SLinus Torvalds .input = ip6_pkt_discard, 3081da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3091da177e4SLinus Torvalds }, 3101da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 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), 325101367c2SThomas Graf }; 326101367c2SThomas Graf 327fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 328101367c2SThomas Graf .dst = { 329101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 330101367c2SThomas Graf .__use = 1, 3312c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 332101367c2SThomas Graf .error = -EINVAL, 333352e512cSHerbert Xu .input = dst_discard, 334ede2059dSEric W. Biederman .output = dst_discard_out, 335101367c2SThomas Graf }, 336101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 337101367c2SThomas Graf }; 338101367c2SThomas Graf 339101367c2SThomas Graf #endif 340101367c2SThomas Graf 341ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 342ebfa45f0SMartin KaFai Lau { 343ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 344ebfa45f0SMartin KaFai Lau 345ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 346ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 347ebfa45f0SMartin KaFai Lau } 348ebfa45f0SMartin KaFai Lau 3491da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 35093531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 351ad706862SMartin KaFai Lau int flags) 3521da177e4SLinus Torvalds { 35397bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 354b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 355cf911662SDavid S. Miller 35681eb8447SWei Wang if (rt) { 357ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 35881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 35981eb8447SWei Wang } 3608104891bSSteffen Klassert 361cf911662SDavid S. Miller return rt; 3621da177e4SLinus Torvalds } 3639ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 364d52d3997SMartin KaFai Lau 3651da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 368a68886a6SDavid Ahern struct fib6_info *from; 3698d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3701da177e4SLinus Torvalds 3711620a336SDavid Ahern ip_dst_metrics_put(dst); 3728d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3738d0b94afSMartin KaFai Lau 3748d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37538308473SDavid S. Miller if (idev) { 3761da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3771da177e4SLinus Torvalds in6_dev_put(idev); 3781da177e4SLinus Torvalds } 3791716a961SGao feng 3800e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt->from, NULL); 38193531c67SDavid Ahern fib6_info_release(from); 382b3419363SDavid S. Miller } 383b3419363SDavid S. Miller 3841da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3851da177e4SLinus Torvalds int how) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3881da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3895a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 390c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3911da177e4SLinus Torvalds 392e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 393e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 39438308473SDavid S. Miller if (loopback_idev) { 3951da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3961da177e4SLinus Torvalds in6_dev_put(idev); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 39997cac082SDavid S. Miller } 4001da177e4SLinus Torvalds 4015973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4025973fb1eSMartin KaFai Lau { 4035973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4045973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4055973fb1eSMartin KaFai Lau else 4065973fb1eSMartin KaFai Lau return false; 4075973fb1eSMartin KaFai Lau } 4085973fb1eSMartin KaFai Lau 409a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4101da177e4SLinus Torvalds { 411a68886a6SDavid Ahern struct fib6_info *from; 412a68886a6SDavid Ahern 413a68886a6SDavid Ahern from = rcu_dereference(rt->from); 414a68886a6SDavid Ahern 4151716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4161716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 417a50feda5SEric Dumazet return true; 418a68886a6SDavid Ahern } else if (from) { 4191e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 420a68886a6SDavid Ahern fib6_check_expired(from); 4211716a961SGao feng } 422a50feda5SEric Dumazet return false; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 425b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res, 426b1d40991SDavid Ahern struct flowi6 *fl6, int oif, bool have_oif_match, 427b1d40991SDavid Ahern const struct sk_buff *skb, int strict) 42851ebd318SNicolas Dichtel { 4298d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 430b1d40991SDavid Ahern struct fib6_info *match = res->f6i; 431b1d40991SDavid Ahern 432f88d8ea6SDavid Ahern if ((!match->fib6_nsiblings && !match->nh) || have_oif_match) 433b1d40991SDavid Ahern goto out; 43451ebd318SNicolas Dichtel 435b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 436b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 437b673d6ccSJakub Sitnicki */ 438f88d8ea6SDavid Ahern if (!fl6->mp_hash && 439f88d8ea6SDavid Ahern (!match->nh || nexthop_is_multipath(match->nh))) 440b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 441b673d6ccSJakub Sitnicki 442f88d8ea6SDavid Ahern if (unlikely(match->nh)) { 443f88d8ea6SDavid Ahern nexthop_path_fib6_result(res, fl6->mp_hash); 444f88d8ea6SDavid Ahern return; 445f88d8ea6SDavid Ahern } 446f88d8ea6SDavid Ahern 4471cf844c7SDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) 448b1d40991SDavid Ahern goto out; 449bbfcd776SIdo Schimmel 45093c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 45193c2fb25SDavid Ahern fib6_siblings) { 4521cf844c7SDavid Ahern const struct fib6_nh *nh = sibling->fib6_nh; 4535e670d84SDavid Ahern int nh_upper_bound; 4545e670d84SDavid Ahern 455702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4565e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4573d709f69SIdo Schimmel continue; 458702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 45952bd4c0cSNicolas Dichtel break; 46051ebd318SNicolas Dichtel match = sibling; 46151ebd318SNicolas Dichtel break; 46251ebd318SNicolas Dichtel } 4633d709f69SIdo Schimmel 464b1d40991SDavid Ahern out: 465b1d40991SDavid Ahern res->f6i = match; 4661cf844c7SDavid Ahern res->nh = match->fib6_nh; 46751ebd318SNicolas Dichtel } 46851ebd318SNicolas Dichtel 4691da177e4SLinus Torvalds /* 47066f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds 4730c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 4740c59d006SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4750c59d006SDavid Ahern { 4760c59d006SDavid Ahern const struct net_device *dev; 4770c59d006SDavid Ahern 4780c59d006SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 4790c59d006SDavid Ahern return false; 4800c59d006SDavid Ahern 4810c59d006SDavid Ahern dev = nh->fib_nh_dev; 4820c59d006SDavid Ahern if (oif) { 4830c59d006SDavid Ahern if (dev->ifindex == oif) 4840c59d006SDavid Ahern return true; 4850c59d006SDavid Ahern } else { 4860c59d006SDavid Ahern if (ipv6_chk_addr(net, saddr, dev, 4870c59d006SDavid Ahern flags & RT6_LOOKUP_F_IFACE)) 4880c59d006SDavid Ahern return true; 4890c59d006SDavid Ahern } 4900c59d006SDavid Ahern 4910c59d006SDavid Ahern return false; 4920c59d006SDavid Ahern } 4930c59d006SDavid Ahern 494962b6803SDavid Ahern struct fib6_nh_dm_arg { 495962b6803SDavid Ahern struct net *net; 496962b6803SDavid Ahern const struct in6_addr *saddr; 497962b6803SDavid Ahern int oif; 498962b6803SDavid Ahern int flags; 499962b6803SDavid Ahern struct fib6_nh *nh; 500962b6803SDavid Ahern }; 501962b6803SDavid Ahern 502962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) 503962b6803SDavid Ahern { 504962b6803SDavid Ahern struct fib6_nh_dm_arg *arg = _arg; 505962b6803SDavid Ahern 506962b6803SDavid Ahern arg->nh = nh; 507962b6803SDavid Ahern return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, 508962b6803SDavid Ahern arg->flags); 509962b6803SDavid Ahern } 510962b6803SDavid Ahern 511962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */ 512962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, 513962b6803SDavid Ahern struct fib6_result *res, 514962b6803SDavid Ahern const struct in6_addr *saddr, 515962b6803SDavid Ahern int oif, int flags) 516962b6803SDavid Ahern { 517962b6803SDavid Ahern struct fib6_nh_dm_arg arg = { 518962b6803SDavid Ahern .net = net, 519962b6803SDavid Ahern .saddr = saddr, 520962b6803SDavid Ahern .oif = oif, 521962b6803SDavid Ahern .flags = flags, 522962b6803SDavid Ahern }; 523962b6803SDavid Ahern 524962b6803SDavid Ahern if (nexthop_is_blackhole(nh)) 525962b6803SDavid Ahern return NULL; 526962b6803SDavid Ahern 527962b6803SDavid Ahern if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) 528962b6803SDavid Ahern return arg.nh; 529962b6803SDavid Ahern 530962b6803SDavid Ahern return NULL; 531962b6803SDavid Ahern } 532962b6803SDavid Ahern 53375ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res, 53475ef7389SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 5351da177e4SLinus Torvalds { 53675ef7389SDavid Ahern struct fib6_info *f6i = res->f6i; 53775ef7389SDavid Ahern struct fib6_info *spf6i; 53875ef7389SDavid Ahern struct fib6_nh *nh; 5391da177e4SLinus Torvalds 54075ef7389SDavid Ahern if (!oif && ipv6_addr_any(saddr)) { 541f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 542f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 543f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 544f88d8ea6SDavid Ahern goto out_blackhole; 545f88d8ea6SDavid Ahern } else { 5461cf844c7SDavid Ahern nh = f6i->fib6_nh; 547f88d8ea6SDavid Ahern } 5487d21fec9SDavid Ahern if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 5497d21fec9SDavid Ahern goto out; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 55275ef7389SDavid Ahern for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 553962b6803SDavid Ahern bool matched = false; 554962b6803SDavid Ahern 555962b6803SDavid Ahern if (unlikely(spf6i->nh)) { 556962b6803SDavid Ahern nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, 557962b6803SDavid Ahern oif, flags); 558962b6803SDavid Ahern if (nh) 559962b6803SDavid Ahern matched = true; 560962b6803SDavid Ahern } else { 5611cf844c7SDavid Ahern nh = spf6i->fib6_nh; 562962b6803SDavid Ahern if (__rt6_device_match(net, nh, saddr, oif, flags)) 563962b6803SDavid Ahern matched = true; 564962b6803SDavid Ahern } 565962b6803SDavid Ahern if (matched) { 56675ef7389SDavid Ahern res->f6i = spf6i; 5677d21fec9SDavid Ahern goto out; 56875ef7389SDavid Ahern } 56975ef7389SDavid Ahern } 5701da177e4SLinus Torvalds 57175ef7389SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) { 57275ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5731cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 5747d21fec9SDavid Ahern goto out; 57575ef7389SDavid Ahern } 57675ef7389SDavid Ahern 577f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 578f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 579f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 580f88d8ea6SDavid Ahern goto out_blackhole; 581f88d8ea6SDavid Ahern } else { 5821cf844c7SDavid Ahern nh = f6i->fib6_nh; 583f88d8ea6SDavid Ahern } 584f88d8ea6SDavid Ahern 5857d21fec9SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) { 58675ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5871cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 58875ef7389SDavid Ahern } 5897d21fec9SDavid Ahern out: 5907d21fec9SDavid Ahern res->nh = nh; 5917d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 5927d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 593f88d8ea6SDavid Ahern return; 594f88d8ea6SDavid Ahern 595f88d8ea6SDavid Ahern out_blackhole: 596f88d8ea6SDavid Ahern res->fib6_flags |= RTF_REJECT; 597f88d8ea6SDavid Ahern res->fib6_type = RTN_BLACKHOLE; 598f88d8ea6SDavid Ahern res->nh = nh; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 60127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 602c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 603c2f17e82SHannes Frederic Sowa struct work_struct work; 604c2f17e82SHannes Frederic Sowa struct in6_addr target; 605c2f17e82SHannes Frederic Sowa struct net_device *dev; 606c2f17e82SHannes Frederic Sowa }; 607c2f17e82SHannes Frederic Sowa 608c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 609c2f17e82SHannes Frederic Sowa { 610c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 611c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 612c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 613c2f17e82SHannes Frederic Sowa 614c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 615adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 616c2f17e82SHannes Frederic Sowa dev_put(work->dev); 617662f5533SMichael Büsch kfree(work); 618c2f17e82SHannes Frederic Sowa } 619c2f17e82SHannes Frederic Sowa 620cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 62127097255SYOSHIFUJI Hideaki { 622f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 6235e670d84SDavid Ahern const struct in6_addr *nh_gw; 624f2c31e32SEric Dumazet struct neighbour *neigh; 6255e670d84SDavid Ahern struct net_device *dev; 626f547fac6SSabrina Dubroca struct inet6_dev *idev; 6275e670d84SDavid Ahern 62827097255SYOSHIFUJI Hideaki /* 62927097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 63027097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 63127097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 63227097255SYOSHIFUJI Hideaki * 63327097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 63427097255SYOSHIFUJI Hideaki * to no more than one per minute. 63527097255SYOSHIFUJI Hideaki */ 636cc3a86c8SDavid Ahern if (fib6_nh->fib_nh_gw_family) 637fdd6681dSAmerigo Wang return; 6385e670d84SDavid Ahern 639cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 640cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 6412152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 642f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 6435e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 6442152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 6458d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 6468d6c31bfSMartin KaFai Lau goto out; 6478d6c31bfSMartin KaFai Lau 6482152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 649990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 650990edb42SMartin KaFai Lau time_after(jiffies, 651dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 652c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 653990edb42SMartin KaFai Lau if (work) 6547e980569SJiri Benc __neigh_set_probe_once(neigh); 655990edb42SMartin KaFai Lau } 656c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 657cc3a86c8SDavid Ahern } else if (time_after(jiffies, fib6_nh->last_probe + 658f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 659990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 660990edb42SMartin KaFai Lau } 661c2f17e82SHannes Frederic Sowa 662c2f17e82SHannes Frederic Sowa if (work) { 663cc3a86c8SDavid Ahern fib6_nh->last_probe = jiffies; 664c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 6655e670d84SDavid Ahern work->target = *nh_gw; 6665e670d84SDavid Ahern dev_hold(dev); 6675e670d84SDavid Ahern work->dev = dev; 668c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 669c2f17e82SHannes Frederic Sowa } 670990edb42SMartin KaFai Lau 6718d6c31bfSMartin KaFai Lau out: 6722152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 673f2c31e32SEric Dumazet } 67427097255SYOSHIFUJI Hideaki #else 675cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 67627097255SYOSHIFUJI Hideaki { 67727097255SYOSHIFUJI Hideaki } 67827097255SYOSHIFUJI Hideaki #endif 67927097255SYOSHIFUJI Hideaki 6801da177e4SLinus Torvalds /* 681554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6821da177e4SLinus Torvalds */ 6831ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 6841da177e4SLinus Torvalds { 685afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 6865e670d84SDavid Ahern struct neighbour *neigh; 687f2c31e32SEric Dumazet 688145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6891ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 6901ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 691145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 692145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 693554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 694afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 695398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 696a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 697afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6987e980569SJiri Benc else 6997e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 700398bcbebSYOSHIFUJI Hideaki #endif 701145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 702afc154e9SHannes Frederic Sowa } else { 703afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 7047e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 705a5a81f0bSPaul Marks } 706145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 707145a3621SYOSHIFUJI Hideaki / 吉藤英明 708a5a81f0bSPaul Marks return ret; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 711702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 712702cea56SDavid Ahern int strict) 713554cfb7eSYOSHIFUJI Hideaki { 7146e1809a5SDavid Ahern int m = 0; 7154d0c5911SYOSHIFUJI Hideaki 7166e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 7176e1809a5SDavid Ahern m = 2; 7186e1809a5SDavid Ahern 71977d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 720afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 721ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 722702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 723ebacaaa0SYOSHIFUJI Hideaki #endif 7241ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 725702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 7261ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 727afc154e9SHannes Frederic Sowa if (n < 0) 728afc154e9SHannes Frederic Sowa return n; 729afc154e9SHannes Frederic Sowa } 730554cfb7eSYOSHIFUJI Hideaki return m; 731554cfb7eSYOSHIFUJI Hideaki } 732554cfb7eSYOSHIFUJI Hideaki 73328679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 73428679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 735554cfb7eSYOSHIFUJI Hideaki { 736afc154e9SHannes Frederic Sowa bool match_do_rr = false; 73728679ed1SDavid Ahern bool rc = false; 73828679ed1SDavid Ahern int m; 73935103d11SAndy Gospodarek 74028679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 7418067bb8cSIdo Schimmel goto out; 7428067bb8cSIdo Schimmel 74328679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 74428679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 745d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 74635103d11SAndy Gospodarek goto out; 747554cfb7eSYOSHIFUJI Hideaki 74828679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 7497e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 750afc154e9SHannes Frederic Sowa match_do_rr = true; 751afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 7527e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 753f11e6659SDavid S. Miller goto out; 7541da177e4SLinus Torvalds } 755f11e6659SDavid S. Miller 756afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 75728679ed1SDavid Ahern rt6_probe(nh); 758afc154e9SHannes Frederic Sowa 7597e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 760afc154e9SHannes Frederic Sowa if (m > *mpri) { 761afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 762afc154e9SHannes Frederic Sowa *mpri = m; 76328679ed1SDavid Ahern rc = true; 764afc154e9SHannes Frederic Sowa } 765f11e6659SDavid S. Miller out: 76628679ed1SDavid Ahern return rc; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 76917a5984eSDavid Ahern struct fib6_nh_frl_arg { 77017a5984eSDavid Ahern u32 flags; 77117a5984eSDavid Ahern int oif; 77217a5984eSDavid Ahern int strict; 77317a5984eSDavid Ahern int *mpri; 77417a5984eSDavid Ahern bool *do_rr; 77517a5984eSDavid Ahern struct fib6_nh *nh; 77617a5984eSDavid Ahern }; 77717a5984eSDavid Ahern 77817a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) 77917a5984eSDavid Ahern { 78017a5984eSDavid Ahern struct fib6_nh_frl_arg *arg = _arg; 78117a5984eSDavid Ahern 78217a5984eSDavid Ahern arg->nh = nh; 78317a5984eSDavid Ahern return find_match(nh, arg->flags, arg->oif, arg->strict, 78417a5984eSDavid Ahern arg->mpri, arg->do_rr); 78517a5984eSDavid Ahern } 78617a5984eSDavid Ahern 787b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start, 78830c15f03SDavid Ahern struct fib6_info *nomatch, u32 metric, 789b7bc4b6aSDavid Ahern struct fib6_result *res, struct fib6_info **cont, 79030c15f03SDavid Ahern int oif, int strict, bool *do_rr, int *mpri) 79130c15f03SDavid Ahern { 792b7bc4b6aSDavid Ahern struct fib6_info *f6i; 79330c15f03SDavid Ahern 794b7bc4b6aSDavid Ahern for (f6i = f6i_start; 795b7bc4b6aSDavid Ahern f6i && f6i != nomatch; 796b7bc4b6aSDavid Ahern f6i = rcu_dereference(f6i->fib6_next)) { 79717a5984eSDavid Ahern bool matched = false; 79830c15f03SDavid Ahern struct fib6_nh *nh; 79930c15f03SDavid Ahern 800b7bc4b6aSDavid Ahern if (cont && f6i->fib6_metric != metric) { 801b7bc4b6aSDavid Ahern *cont = f6i; 80230c15f03SDavid Ahern return; 80330c15f03SDavid Ahern } 80430c15f03SDavid Ahern 805b7bc4b6aSDavid Ahern if (fib6_check_expired(f6i)) 80630c15f03SDavid Ahern continue; 80730c15f03SDavid Ahern 80817a5984eSDavid Ahern if (unlikely(f6i->nh)) { 80917a5984eSDavid Ahern struct fib6_nh_frl_arg arg = { 81017a5984eSDavid Ahern .flags = f6i->fib6_flags, 81117a5984eSDavid Ahern .oif = oif, 81217a5984eSDavid Ahern .strict = strict, 81317a5984eSDavid Ahern .mpri = mpri, 81417a5984eSDavid Ahern .do_rr = do_rr 81517a5984eSDavid Ahern }; 81617a5984eSDavid Ahern 81717a5984eSDavid Ahern if (nexthop_is_blackhole(f6i->nh)) { 81817a5984eSDavid Ahern res->fib6_flags = RTF_REJECT; 81917a5984eSDavid Ahern res->fib6_type = RTN_BLACKHOLE; 82017a5984eSDavid Ahern res->f6i = f6i; 82117a5984eSDavid Ahern res->nh = nexthop_fib6_nh(f6i->nh); 82217a5984eSDavid Ahern return; 82317a5984eSDavid Ahern } 82417a5984eSDavid Ahern if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, 82517a5984eSDavid Ahern &arg)) { 82617a5984eSDavid Ahern matched = true; 82717a5984eSDavid Ahern nh = arg.nh; 82817a5984eSDavid Ahern } 82917a5984eSDavid Ahern } else { 8301cf844c7SDavid Ahern nh = f6i->fib6_nh; 83117a5984eSDavid Ahern if (find_match(nh, f6i->fib6_flags, oif, strict, 83217a5984eSDavid Ahern mpri, do_rr)) 83317a5984eSDavid Ahern matched = true; 83417a5984eSDavid Ahern } 83517a5984eSDavid Ahern if (matched) { 836b7bc4b6aSDavid Ahern res->f6i = f6i; 837b7bc4b6aSDavid Ahern res->nh = nh; 8387d21fec9SDavid Ahern res->fib6_flags = f6i->fib6_flags; 8397d21fec9SDavid Ahern res->fib6_type = f6i->fib6_type; 840b7bc4b6aSDavid Ahern } 84130c15f03SDavid Ahern } 84230c15f03SDavid Ahern } 84330c15f03SDavid Ahern 844b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 845b7bc4b6aSDavid Ahern struct fib6_info *rr_head, int oif, int strict, 846b7bc4b6aSDavid Ahern bool *do_rr, struct fib6_result *res) 847f11e6659SDavid S. Miller { 848b7bc4b6aSDavid Ahern u32 metric = rr_head->fib6_metric; 849b7bc4b6aSDavid Ahern struct fib6_info *cont = NULL; 850f11e6659SDavid S. Miller int mpri = -1; 851f11e6659SDavid S. Miller 852b7bc4b6aSDavid Ahern __find_rr_leaf(rr_head, NULL, metric, res, &cont, 85330c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8549fbdcfafSSteffen Klassert 855b7bc4b6aSDavid Ahern __find_rr_leaf(leaf, rr_head, metric, res, &cont, 85630c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8579fbdcfafSSteffen Klassert 858b7bc4b6aSDavid Ahern if (res->f6i || !cont) 859b7bc4b6aSDavid Ahern return; 8609fbdcfafSSteffen Klassert 861b7bc4b6aSDavid Ahern __find_rr_leaf(cont, NULL, metric, res, NULL, 86230c15f03SDavid Ahern oif, strict, do_rr, &mpri); 863f11e6659SDavid S. Miller } 864f11e6659SDavid S. Miller 865b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 866b7bc4b6aSDavid Ahern struct fib6_result *res, int strict) 867f11e6659SDavid S. Miller { 8688d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 869b7bc4b6aSDavid Ahern struct fib6_info *rt0; 870afc154e9SHannes Frederic Sowa bool do_rr = false; 87117ecf590SWei Wang int key_plen; 872f11e6659SDavid S. Miller 873b7bc4b6aSDavid Ahern /* make sure this function or its helpers sets f6i */ 874b7bc4b6aSDavid Ahern res->f6i = NULL; 875b7bc4b6aSDavid Ahern 876421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 877b7bc4b6aSDavid Ahern goto out; 8788d1040e8SWei Wang 87966f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 880f11e6659SDavid S. Miller if (!rt0) 88166f5d6ceSWei Wang rt0 = leaf; 882f11e6659SDavid S. Miller 88317ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 88417ecf590SWei Wang * and fn->leaf does not points to its child's leaf 88517ecf590SWei Wang * (This might happen if all routes under fn are deleted from 88617ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 88717ecf590SWei Wang */ 88893c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 88917ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 89093c2fb25SDavid Ahern if (rt0->fib6_src.plen) 89193c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 89217ecf590SWei Wang #endif 89317ecf590SWei Wang if (fn->fn_bit != key_plen) 894b7bc4b6aSDavid Ahern goto out; 89517ecf590SWei Wang 896b7bc4b6aSDavid Ahern find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 897afc154e9SHannes Frederic Sowa if (do_rr) { 8988fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 899f11e6659SDavid S. Miller 900554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 90193c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 9028d1040e8SWei Wang next = leaf; 903f11e6659SDavid S. Miller 90466f5d6ceSWei Wang if (next != rt0) { 90593c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 90666f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 90793c2fb25SDavid Ahern if (next->fib6_node) 90866f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 90993c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 91066f5d6ceSWei Wang } 911554cfb7eSYOSHIFUJI Hideaki } 912554cfb7eSYOSHIFUJI Hideaki 913b7bc4b6aSDavid Ahern out: 914b7bc4b6aSDavid Ahern if (!res->f6i) { 915b7bc4b6aSDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 9161cf844c7SDavid Ahern res->nh = res->f6i->fib6_nh; 9177d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 9187d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 919b7bc4b6aSDavid Ahern } 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 92285bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 9238b9df265SMartin KaFai Lau { 92485bd05deSDavid Ahern return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 92585bd05deSDavid Ahern res->nh->fib_nh_gw_family; 9268b9df265SMartin KaFai Lau } 9278b9df265SMartin KaFai Lau 92870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 92970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 930b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 93170ceb4f5SYOSHIFUJI Hideaki { 932c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 93370ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 93470ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 93570ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 9364bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 9378d1c802bSDavid Ahern struct fib6_info *rt; 93870ceb4f5SYOSHIFUJI Hideaki 93970ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 94070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94170ceb4f5SYOSHIFUJI Hideaki } 94270ceb4f5SYOSHIFUJI Hideaki 94370ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 94470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 94570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 94770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 94970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 95070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95170ceb4f5SYOSHIFUJI Hideaki } 95270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 95370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 95470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95570ceb4f5SYOSHIFUJI Hideaki } 95670ceb4f5SYOSHIFUJI Hideaki } 95770ceb4f5SYOSHIFUJI Hideaki 95870ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 95970ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 9603933fc95SJens Rosenboom return -EINVAL; 96170ceb4f5SYOSHIFUJI Hideaki 9624bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 96370ceb4f5SYOSHIFUJI Hideaki 96470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 96570ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 96670ceb4f5SYOSHIFUJI Hideaki else { 96770ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 96870ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 96970ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 97070ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 97170ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 97270ceb4f5SYOSHIFUJI Hideaki } 97370ceb4f5SYOSHIFUJI Hideaki 974f104a567SDuan Jiong if (rinfo->prefix_len == 0) 975afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 976f104a567SDuan Jiong else 977f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 978830218c1SDavid Ahern gwaddr, dev); 97970ceb4f5SYOSHIFUJI Hideaki 98070ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 981afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 98270ceb4f5SYOSHIFUJI Hideaki rt = NULL; 98370ceb4f5SYOSHIFUJI Hideaki } 98470ceb4f5SYOSHIFUJI Hideaki 98570ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 986830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 987830218c1SDavid Ahern dev, pref); 98870ceb4f5SYOSHIFUJI Hideaki else if (rt) 98993c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 99093c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 99170ceb4f5SYOSHIFUJI Hideaki 99270ceb4f5SYOSHIFUJI Hideaki if (rt) { 9931716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 99414895687SDavid Ahern fib6_clean_expires(rt); 9951716a961SGao feng else 99614895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 9971716a961SGao feng 99893531c67SDavid Ahern fib6_info_release(rt); 99970ceb4f5SYOSHIFUJI Hideaki } 100070ceb4f5SYOSHIFUJI Hideaki return 0; 100170ceb4f5SYOSHIFUJI Hideaki } 100270ceb4f5SYOSHIFUJI Hideaki #endif 100370ceb4f5SYOSHIFUJI Hideaki 1004ae90d867SDavid Ahern /* 1005ae90d867SDavid Ahern * Misc support functions 1006ae90d867SDavid Ahern */ 1007ae90d867SDavid Ahern 1008ae90d867SDavid Ahern /* called with rcu_lock held */ 10090d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 1010ae90d867SDavid Ahern { 10110d161581SDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 1012ae90d867SDavid Ahern 10137d21fec9SDavid Ahern if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 1014ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 1015ae90d867SDavid Ahern * device if it is a master device, the master device if 1016ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 1017ae90d867SDavid Ahern */ 1018ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 10197d21fec9SDavid Ahern !rt6_need_strict(&res->f6i->fib6_dst.addr)) 1020ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 1021ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 1022ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 1023ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 1024ae90d867SDavid Ahern * case we want dev returned to be dev 1025ae90d867SDavid Ahern */ 1026ae90d867SDavid Ahern } 1027ae90d867SDavid Ahern 1028ae90d867SDavid Ahern return dev; 1029ae90d867SDavid Ahern } 1030ae90d867SDavid Ahern 10316edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 10326edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 10336edb3c96SDavid Ahern [RTN_UNICAST] = 0, 10346edb3c96SDavid Ahern [RTN_LOCAL] = 0, 10356edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 10366edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 10376edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 10386edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 10396edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 10406edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 10416edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 10426edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 10436edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 10446edb3c96SDavid Ahern }; 10456edb3c96SDavid Ahern 10466edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 10476edb3c96SDavid Ahern { 10486edb3c96SDavid Ahern return fib6_prop[fib6_type]; 10496edb3c96SDavid Ahern } 10506edb3c96SDavid Ahern 10518d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 10523b6761d1SDavid Ahern { 10533b6761d1SDavid Ahern unsigned short flags = 0; 10543b6761d1SDavid Ahern 10553b6761d1SDavid Ahern if (rt->dst_nocount) 10563b6761d1SDavid Ahern flags |= DST_NOCOUNT; 10573b6761d1SDavid Ahern if (rt->dst_nopolicy) 10583b6761d1SDavid Ahern flags |= DST_NOPOLICY; 10593b6761d1SDavid Ahern if (rt->dst_host) 10603b6761d1SDavid Ahern flags |= DST_HOST; 10613b6761d1SDavid Ahern 10623b6761d1SDavid Ahern return flags; 10633b6761d1SDavid Ahern } 10643b6761d1SDavid Ahern 10657d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 10666edb3c96SDavid Ahern { 10677d21fec9SDavid Ahern rt->dst.error = ip6_rt_type_to_error(fib6_type); 10686edb3c96SDavid Ahern 10697d21fec9SDavid Ahern switch (fib6_type) { 10706edb3c96SDavid Ahern case RTN_BLACKHOLE: 10716edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 10726edb3c96SDavid Ahern rt->dst.input = dst_discard; 10736edb3c96SDavid Ahern break; 10746edb3c96SDavid Ahern case RTN_PROHIBIT: 10756edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 10766edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 10776edb3c96SDavid Ahern break; 10786edb3c96SDavid Ahern case RTN_THROW: 10796edb3c96SDavid Ahern case RTN_UNREACHABLE: 10806edb3c96SDavid Ahern default: 10816edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 10826edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 10836edb3c96SDavid Ahern break; 10846edb3c96SDavid Ahern } 10856edb3c96SDavid Ahern } 10866edb3c96SDavid Ahern 10870d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 10886edb3c96SDavid Ahern { 10897d21fec9SDavid Ahern struct fib6_info *f6i = res->f6i; 10900d161581SDavid Ahern 10917d21fec9SDavid Ahern if (res->fib6_flags & RTF_REJECT) { 10927d21fec9SDavid Ahern ip6_rt_init_dst_reject(rt, res->fib6_type); 10936edb3c96SDavid Ahern return; 10946edb3c96SDavid Ahern } 10956edb3c96SDavid Ahern 10966edb3c96SDavid Ahern rt->dst.error = 0; 10976edb3c96SDavid Ahern rt->dst.output = ip6_output; 10986edb3c96SDavid Ahern 10997d21fec9SDavid Ahern if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 11006edb3c96SDavid Ahern rt->dst.input = ip6_input; 11017d21fec9SDavid Ahern } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 11026edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 11036edb3c96SDavid Ahern } else { 11046edb3c96SDavid Ahern rt->dst.input = ip6_forward; 11056edb3c96SDavid Ahern } 11066edb3c96SDavid Ahern 11070d161581SDavid Ahern if (res->nh->fib_nh_lws) { 11080d161581SDavid Ahern rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 11096edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 11106edb3c96SDavid Ahern } 11116edb3c96SDavid Ahern 11126edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 11136edb3c96SDavid Ahern } 11146edb3c96SDavid Ahern 1115e873e4b9SWei Wang /* Caller must already hold reference to @from */ 11168d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1117ae90d867SDavid Ahern { 1118ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 1119a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 1120e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1121ae90d867SDavid Ahern } 1122ae90d867SDavid Ahern 11230d161581SDavid Ahern /* Caller must already hold reference to f6i in result */ 11240d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1125ae90d867SDavid Ahern { 11260d161581SDavid Ahern const struct fib6_nh *nh = res->nh; 11270d161581SDavid Ahern const struct net_device *dev = nh->fib_nh_dev; 11280d161581SDavid Ahern struct fib6_info *f6i = res->f6i; 1129dcd1f572SDavid Ahern 11300d161581SDavid Ahern ip6_rt_init_dst(rt, res); 11316edb3c96SDavid Ahern 11320d161581SDavid Ahern rt->rt6i_dst = f6i->fib6_dst; 1133dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 11347d21fec9SDavid Ahern rt->rt6i_flags = res->fib6_flags; 11350d161581SDavid Ahern if (nh->fib_nh_gw_family) { 11360d161581SDavid Ahern rt->rt6i_gateway = nh->fib_nh_gw6; 11372b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 11382b2450caSDavid Ahern } 11390d161581SDavid Ahern rt6_set_from(rt, f6i); 1140ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 11410d161581SDavid Ahern rt->rt6i_src = f6i->fib6_src; 1142ae90d867SDavid Ahern #endif 1143ae90d867SDavid Ahern } 1144ae90d867SDavid Ahern 1145a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1146a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1147a3c00e46SMartin KaFai Lau { 114866f5d6ceSWei Wang struct fib6_node *pn, *sn; 1149a3c00e46SMartin KaFai Lau while (1) { 1150a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1151a3c00e46SMartin KaFai Lau return NULL; 115266f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 115366f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 115466f5d6ceSWei Wang if (sn && sn != fn) 11556454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1156a3c00e46SMartin KaFai Lau else 1157a3c00e46SMartin KaFai Lau fn = pn; 1158a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1159a3c00e46SMartin KaFai Lau return fn; 1160a3c00e46SMartin KaFai Lau } 1161a3c00e46SMartin KaFai Lau } 1162c71099acSThomas Graf 116310585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1164d3843fe5SWei Wang { 1165d3843fe5SWei Wang struct rt6_info *rt = *prt; 1166d3843fe5SWei Wang 1167d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1168d3843fe5SWei Wang return true; 116910585b43SDavid Ahern if (net) { 1170d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1171d3843fe5SWei Wang dst_hold(&rt->dst); 1172d3843fe5SWei Wang } else { 1173d3843fe5SWei Wang rt = NULL; 1174d3843fe5SWei Wang } 1175d3843fe5SWei Wang *prt = rt; 1176d3843fe5SWei Wang return false; 1177d3843fe5SWei Wang } 1178d3843fe5SWei Wang 1179dec9b0e2SDavid Ahern /* called with rcu_lock held */ 11809b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1181dec9b0e2SDavid Ahern { 11829b6b35abSDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 11839b6b35abSDavid Ahern struct fib6_info *f6i = res->f6i; 11849b6b35abSDavid Ahern unsigned short flags; 1185dec9b0e2SDavid Ahern struct rt6_info *nrt; 1186dec9b0e2SDavid Ahern 11879b6b35abSDavid Ahern if (!fib6_info_hold_safe(f6i)) 11881c87e79aSXin Long goto fallback; 1189e873e4b9SWei Wang 11909b6b35abSDavid Ahern flags = fib6_info_dst_flags(f6i); 119193531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 11921c87e79aSXin Long if (!nrt) { 11939b6b35abSDavid Ahern fib6_info_release(f6i); 11941c87e79aSXin Long goto fallback; 11951c87e79aSXin Long } 1196dec9b0e2SDavid Ahern 11970d161581SDavid Ahern ip6_rt_copy_init(nrt, res); 11981c87e79aSXin Long return nrt; 11991c87e79aSXin Long 12001c87e79aSXin Long fallback: 12011c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 12021c87e79aSXin Long dst_hold(&nrt->dst); 1203dec9b0e2SDavid Ahern return nrt; 1204dec9b0e2SDavid Ahern } 1205dec9b0e2SDavid Ahern 12068ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 12078ed67789SDaniel Lezcano struct fib6_table *table, 1208b75cc8f9SDavid Ahern struct flowi6 *fl6, 1209b75cc8f9SDavid Ahern const struct sk_buff *skb, 1210b75cc8f9SDavid Ahern int flags) 12111da177e4SLinus Torvalds { 1212b1d40991SDavid Ahern struct fib6_result res = {}; 12131da177e4SLinus Torvalds struct fib6_node *fn; 121423fb93a4SDavid Ahern struct rt6_info *rt; 12151da177e4SLinus Torvalds 1216b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1217b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 1218b6cdbc85SDavid Ahern 121966f5d6ceSWei Wang rcu_read_lock(); 12206454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1221c71099acSThomas Graf restart: 1222b1d40991SDavid Ahern res.f6i = rcu_dereference(fn->leaf); 1223b1d40991SDavid Ahern if (!res.f6i) 1224b1d40991SDavid Ahern res.f6i = net->ipv6.fib6_null_entry; 1225af52a52cSDavid Ahern else 122675ef7389SDavid Ahern rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 122775ef7389SDavid Ahern flags); 1228af52a52cSDavid Ahern 1229b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1230a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1231a3c00e46SMartin KaFai Lau if (fn) 1232a3c00e46SMartin KaFai Lau goto restart; 1233af52a52cSDavid Ahern 1234af52a52cSDavid Ahern rt = net->ipv6.ip6_null_entry; 1235af52a52cSDavid Ahern dst_hold(&rt->dst); 1236af52a52cSDavid Ahern goto out; 1237f88d8ea6SDavid Ahern } else if (res.fib6_flags & RTF_REJECT) { 1238f88d8ea6SDavid Ahern goto do_create; 1239a3c00e46SMartin KaFai Lau } 12402b760fcfSWei Wang 1241b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1242b1d40991SDavid Ahern fl6->flowi6_oif != 0, skb, flags); 1243b1d40991SDavid Ahern 12444c9483b2SDavid S. Miller /* Search through exception table */ 12457e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 124623fb93a4SDavid Ahern if (rt) { 124710585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 1248d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 124923fb93a4SDavid Ahern } else { 1250f88d8ea6SDavid Ahern do_create: 12519b6b35abSDavid Ahern rt = ip6_create_rt_rcu(&res); 1252dec9b0e2SDavid Ahern } 1253d3843fe5SWei Wang 1254af52a52cSDavid Ahern out: 12558ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 1256af52a52cSDavid Ahern 125766f5d6ceSWei Wang rcu_read_unlock(); 1258b811580dSDavid Ahern 12591da177e4SLinus Torvalds return rt; 1260c71099acSThomas Graf } 1261c71099acSThomas Graf 1262ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1263b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1264ea6e574eSFlorian Westphal { 1265b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1266ea6e574eSFlorian Westphal } 1267ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1268ea6e574eSFlorian Westphal 12699acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1270b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1271b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1272c71099acSThomas Graf { 12734c9483b2SDavid S. Miller struct flowi6 fl6 = { 12744c9483b2SDavid S. Miller .flowi6_oif = oif, 12754c9483b2SDavid S. Miller .daddr = *daddr, 1276c71099acSThomas Graf }; 1277c71099acSThomas Graf struct dst_entry *dst; 127877d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1279c71099acSThomas Graf 1280adaa70bbSThomas Graf if (saddr) { 12814c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1282adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1283adaa70bbSThomas Graf } 1284adaa70bbSThomas Graf 1285b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1286c71099acSThomas Graf if (dst->error == 0) 1287c71099acSThomas Graf return (struct rt6_info *) dst; 1288c71099acSThomas Graf 1289c71099acSThomas Graf dst_release(dst); 1290c71099acSThomas Graf 12911da177e4SLinus Torvalds return NULL; 12921da177e4SLinus Torvalds } 12937159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 12947159039aSYOSHIFUJI Hideaki 1295c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 12961cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 12971cfb71eeSWei Wang * route is released. 12981cfb71eeSWei Wang * Caller must hold dst before calling it. 12991da177e4SLinus Torvalds */ 13001da177e4SLinus Torvalds 13018d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1302333c4301SDavid Ahern struct netlink_ext_ack *extack) 13031da177e4SLinus Torvalds { 13041da177e4SLinus Torvalds int err; 1305c71099acSThomas Graf struct fib6_table *table; 13061da177e4SLinus Torvalds 130793c2fb25SDavid Ahern table = rt->fib6_table; 130866f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1309d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 131066f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds return err; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13158d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 131640e22e8fSThomas Graf { 1317afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1318e715b6d3SFlorian Westphal 1319d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 132040e22e8fSThomas Graf } 132140e22e8fSThomas Graf 132285bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 132321efcfa0SEric Dumazet const struct in6_addr *daddr, 1324b71d1d42SEric Dumazet const struct in6_addr *saddr) 13251da177e4SLinus Torvalds { 132685bd05deSDavid Ahern struct fib6_info *f6i = res->f6i; 13274832c30dSDavid Ahern struct net_device *dev; 13281da177e4SLinus Torvalds struct rt6_info *rt; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* 13311da177e4SLinus Torvalds * Clone the route. 13321da177e4SLinus Torvalds */ 13331da177e4SLinus Torvalds 133485bd05deSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1335e873e4b9SWei Wang return NULL; 1336e873e4b9SWei Wang 13370d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 133893531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1339e873e4b9SWei Wang if (!rt) { 134085bd05deSDavid Ahern fib6_info_release(f6i); 134183a09abdSMartin KaFai Lau return NULL; 1342e873e4b9SWei Wang } 134383a09abdSMartin KaFai Lau 13440d161581SDavid Ahern ip6_rt_copy_init(rt, res); 13458b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 134683a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 134783a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 134883a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 13498b9df265SMartin KaFai Lau 135085bd05deSDavid Ahern if (!rt6_is_gw_or_nonexthop(res)) { 135185bd05deSDavid Ahern if (f6i->fib6_dst.plen != 128 && 135285bd05deSDavid Ahern ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 135358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 13541da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 13551da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 13564e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 13571da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds #endif 136095a9a5baSYOSHIFUJI Hideaki } 136195a9a5baSYOSHIFUJI Hideaki 1362299d9939SYOSHIFUJI Hideaki return rt; 1363299d9939SYOSHIFUJI Hideaki } 1364299d9939SYOSHIFUJI Hideaki 1365db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1366d52d3997SMartin KaFai Lau { 1367db3fedeeSDavid Ahern struct fib6_info *f6i = res->f6i; 1368db3fedeeSDavid Ahern unsigned short flags = fib6_info_dst_flags(f6i); 13694832c30dSDavid Ahern struct net_device *dev; 1370d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1371d52d3997SMartin KaFai Lau 1372db3fedeeSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1373e873e4b9SWei Wang return NULL; 1374e873e4b9SWei Wang 13754832c30dSDavid Ahern rcu_read_lock(); 13760d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 137793531c67SDavid Ahern pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); 13784832c30dSDavid Ahern rcu_read_unlock(); 1379e873e4b9SWei Wang if (!pcpu_rt) { 1380db3fedeeSDavid Ahern fib6_info_release(f6i); 1381d52d3997SMartin KaFai Lau return NULL; 1382e873e4b9SWei Wang } 13830d161581SDavid Ahern ip6_rt_copy_init(pcpu_rt, res); 1384d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1385d52d3997SMartin KaFai Lau return pcpu_rt; 1386d52d3997SMartin KaFai Lau } 1387d52d3997SMartin KaFai Lau 138866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1389db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1390d52d3997SMartin KaFai Lau { 1391c353071aSEric Dumazet struct rt6_info *pcpu_rt; 1392d52d3997SMartin KaFai Lau 1393c353071aSEric Dumazet pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); 1394d52d3997SMartin KaFai Lau 1395a73e4195SMartin KaFai Lau return pcpu_rt; 1396a73e4195SMartin KaFai Lau } 1397a73e4195SMartin KaFai Lau 1398afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1399db3fedeeSDavid Ahern const struct fib6_result *res) 1400a73e4195SMartin KaFai Lau { 1401a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1402d52d3997SMartin KaFai Lau 1403db3fedeeSDavid Ahern pcpu_rt = ip6_rt_pcpu_alloc(res); 14040e09edccSWei Wang if (!pcpu_rt) 14050e09edccSWei Wang return NULL; 1406d52d3997SMartin KaFai Lau 1407f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1408d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1409951f788aSEric Dumazet BUG_ON(prev); 1410a94b9367SWei Wang 141161fb0d01SEric Dumazet if (res->f6i->fib6_destroying) { 141261fb0d01SEric Dumazet struct fib6_info *from; 141361fb0d01SEric Dumazet 141461fb0d01SEric Dumazet from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 141561fb0d01SEric Dumazet fib6_info_release(from); 141661fb0d01SEric Dumazet } 141761fb0d01SEric Dumazet 1418d52d3997SMartin KaFai Lau return pcpu_rt; 1419d52d3997SMartin KaFai Lau } 1420d52d3997SMartin KaFai Lau 142135732d01SWei Wang /* exception hash table implementation 142235732d01SWei Wang */ 142335732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 142435732d01SWei Wang 142535732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 142635732d01SWei Wang * Caller must hold rt6_exception_lock 142735732d01SWei Wang */ 142835732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 142935732d01SWei Wang struct rt6_exception *rt6_ex) 143035732d01SWei Wang { 1431f5b51fe8SPaolo Abeni struct fib6_info *from; 1432b2427e67SColin Ian King struct net *net; 143381eb8447SWei Wang 143435732d01SWei Wang if (!bucket || !rt6_ex) 143535732d01SWei Wang return; 1436b2427e67SColin Ian King 1437b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1438f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1439f5b51fe8SPaolo Abeni 1440f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1441f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1442f5b51fe8SPaolo Abeni */ 14430e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1444f5b51fe8SPaolo Abeni fib6_info_release(from); 1445f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1446f5b51fe8SPaolo Abeni 144735732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 144877634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 144935732d01SWei Wang kfree_rcu(rt6_ex, rcu); 145035732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 145135732d01SWei Wang bucket->depth--; 145235732d01SWei Wang } 145335732d01SWei Wang 145435732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 145535732d01SWei Wang * Caller must hold rt6_exception_lock 145635732d01SWei Wang */ 145735732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 145835732d01SWei Wang { 145935732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 146035732d01SWei Wang 146135732d01SWei Wang if (!bucket) 146235732d01SWei Wang return; 146335732d01SWei Wang 146435732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 146535732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 146635732d01SWei Wang oldest = rt6_ex; 146735732d01SWei Wang } 146835732d01SWei Wang rt6_remove_exception(bucket, oldest); 146935732d01SWei Wang } 147035732d01SWei Wang 147135732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 147235732d01SWei Wang const struct in6_addr *src) 147335732d01SWei Wang { 147435732d01SWei Wang static u32 seed __read_mostly; 147535732d01SWei Wang u32 val; 147635732d01SWei Wang 147735732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 147835732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 147935732d01SWei Wang 148035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 148135732d01SWei Wang if (src) 148235732d01SWei Wang val = jhash(src, sizeof(*src), val); 148335732d01SWei Wang #endif 148435732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 148535732d01SWei Wang } 148635732d01SWei Wang 148735732d01SWei Wang /* Helper function to find the cached rt in the hash table 148835732d01SWei Wang * and update bucket pointer to point to the bucket for this 148935732d01SWei Wang * (daddr, saddr) pair 149035732d01SWei Wang * Caller must hold rt6_exception_lock 149135732d01SWei Wang */ 149235732d01SWei Wang static struct rt6_exception * 149335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 149435732d01SWei Wang const struct in6_addr *daddr, 149535732d01SWei Wang const struct in6_addr *saddr) 149635732d01SWei Wang { 149735732d01SWei Wang struct rt6_exception *rt6_ex; 149835732d01SWei Wang u32 hval; 149935732d01SWei Wang 150035732d01SWei Wang if (!(*bucket) || !daddr) 150135732d01SWei Wang return NULL; 150235732d01SWei Wang 150335732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 150435732d01SWei Wang *bucket += hval; 150535732d01SWei Wang 150635732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 150735732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 150835732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 150935732d01SWei Wang 151035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 151135732d01SWei Wang if (matched && saddr) 151235732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 151335732d01SWei Wang #endif 151435732d01SWei Wang if (matched) 151535732d01SWei Wang return rt6_ex; 151635732d01SWei Wang } 151735732d01SWei Wang return NULL; 151835732d01SWei Wang } 151935732d01SWei Wang 152035732d01SWei Wang /* Helper function to find the cached rt in the hash table 152135732d01SWei Wang * and update bucket pointer to point to the bucket for this 152235732d01SWei Wang * (daddr, saddr) pair 152335732d01SWei Wang * Caller must hold rcu_read_lock() 152435732d01SWei Wang */ 152535732d01SWei Wang static struct rt6_exception * 152635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 152735732d01SWei Wang const struct in6_addr *daddr, 152835732d01SWei Wang const struct in6_addr *saddr) 152935732d01SWei Wang { 153035732d01SWei Wang struct rt6_exception *rt6_ex; 153135732d01SWei Wang u32 hval; 153235732d01SWei Wang 153335732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 153435732d01SWei Wang 153535732d01SWei Wang if (!(*bucket) || !daddr) 153635732d01SWei Wang return NULL; 153735732d01SWei Wang 153835732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 153935732d01SWei Wang *bucket += hval; 154035732d01SWei Wang 154135732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 154235732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 154335732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 154435732d01SWei Wang 154535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 154635732d01SWei Wang if (matched && saddr) 154735732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 154835732d01SWei Wang #endif 154935732d01SWei Wang if (matched) 155035732d01SWei Wang return rt6_ex; 155135732d01SWei Wang } 155235732d01SWei Wang return NULL; 155335732d01SWei Wang } 155435732d01SWei Wang 1555b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res) 155635732d01SWei Wang { 1557b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 1558d4ead6b3SDavid Ahern unsigned int mtu; 1559d4ead6b3SDavid Ahern 1560b748f260SDavid Ahern if (res->f6i->fib6_pmtu) { 1561b748f260SDavid Ahern mtu = res->f6i->fib6_pmtu; 1562dcd1f572SDavid Ahern } else { 1563b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 1564dcd1f572SDavid Ahern struct inet6_dev *idev; 1565dcd1f572SDavid Ahern 1566dcd1f572SDavid Ahern rcu_read_lock(); 1567dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1568dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1569dcd1f572SDavid Ahern rcu_read_unlock(); 1570dcd1f572SDavid Ahern } 1571dcd1f572SDavid Ahern 1572d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1573d4ead6b3SDavid Ahern 1574b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1575d4ead6b3SDavid Ahern } 1576d4ead6b3SDavid Ahern 1577cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL 1578cc5c073aSDavid Ahern 1579cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket 1580cc5c073aSDavid Ahern * (ie., all bucket users except rt6_insert_exception); 1581cc5c073aSDavid Ahern * 1582cc5c073aSDavid Ahern * called under rcu lock; sometimes called with rt6_exception_lock held 1583cc5c073aSDavid Ahern */ 1584cc5c073aSDavid Ahern static 1585cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, 1586cc5c073aSDavid Ahern spinlock_t *lock) 1587cc5c073aSDavid Ahern { 1588cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1589cc5c073aSDavid Ahern 1590cc5c073aSDavid Ahern if (lock) 1591cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1592cc5c073aSDavid Ahern lockdep_is_held(lock)); 1593cc5c073aSDavid Ahern else 1594cc5c073aSDavid Ahern bucket = rcu_dereference(nh->rt6i_exception_bucket); 1595cc5c073aSDavid Ahern 1596cc5c073aSDavid Ahern /* remove bucket flushed bit if set */ 1597cc5c073aSDavid Ahern if (bucket) { 1598cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1599cc5c073aSDavid Ahern 1600cc5c073aSDavid Ahern p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; 1601cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1602cc5c073aSDavid Ahern } 1603cc5c073aSDavid Ahern 1604cc5c073aSDavid Ahern return bucket; 1605cc5c073aSDavid Ahern } 1606cc5c073aSDavid Ahern 1607cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) 1608cc5c073aSDavid Ahern { 1609cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1610cc5c073aSDavid Ahern 1611cc5c073aSDavid Ahern return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); 1612cc5c073aSDavid Ahern } 1613cc5c073aSDavid Ahern 1614cc5c073aSDavid Ahern /* called with rt6_exception_lock held */ 1615cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, 1616cc5c073aSDavid Ahern spinlock_t *lock) 1617cc5c073aSDavid Ahern { 1618cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1619cc5c073aSDavid Ahern unsigned long p; 1620cc5c073aSDavid Ahern 1621cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1622cc5c073aSDavid Ahern lockdep_is_held(lock)); 1623cc5c073aSDavid Ahern 1624cc5c073aSDavid Ahern p = (unsigned long)bucket; 1625cc5c073aSDavid Ahern p |= FIB6_EXCEPTION_BUCKET_FLUSHED; 1626cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1627cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1628cc5c073aSDavid Ahern } 1629cc5c073aSDavid Ahern 163035732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 16315012f0a5SDavid Ahern const struct fib6_result *res) 163235732d01SWei Wang { 16335e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 163435732d01SWei Wang struct rt6_exception_bucket *bucket; 1635cc5c073aSDavid Ahern struct fib6_info *f6i = res->f6i; 163635732d01SWei Wang struct in6_addr *src_key = NULL; 163735732d01SWei Wang struct rt6_exception *rt6_ex; 1638cc5c073aSDavid Ahern struct fib6_nh *nh = res->nh; 163935732d01SWei Wang int err = 0; 164035732d01SWei Wang 164135732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 164235732d01SWei Wang 1643cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 164435732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 164535732d01SWei Wang if (!bucket) { 164635732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 164735732d01SWei Wang GFP_ATOMIC); 164835732d01SWei Wang if (!bucket) { 164935732d01SWei Wang err = -ENOMEM; 165035732d01SWei Wang goto out; 165135732d01SWei Wang } 1652cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1653cc5c073aSDavid Ahern } else if (fib6_nh_excptn_bucket_flushed(bucket)) { 1654cc5c073aSDavid Ahern err = -EINVAL; 1655cc5c073aSDavid Ahern goto out; 165635732d01SWei Wang } 165735732d01SWei Wang 165835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 16595012f0a5SDavid Ahern /* fib6_src.plen != 0 indicates f6i is in subtree 166035732d01SWei Wang * and exception table is indexed by a hash of 16615012f0a5SDavid Ahern * both fib6_dst and fib6_src. 166235732d01SWei Wang * Otherwise, the exception table is indexed by 16635012f0a5SDavid Ahern * a hash of only fib6_dst. 166435732d01SWei Wang */ 16655012f0a5SDavid Ahern if (f6i->fib6_src.plen) 166635732d01SWei Wang src_key = &nrt->rt6i_src.addr; 166735732d01SWei Wang #endif 16685012f0a5SDavid Ahern /* rt6_mtu_change() might lower mtu on f6i. 1669f5bbe7eeSWei Wang * Only insert this exception route if its mtu 16705012f0a5SDavid Ahern * is less than f6i's mtu value. 1671f5bbe7eeSWei Wang */ 1672b748f260SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1673f5bbe7eeSWei Wang err = -EINVAL; 1674f5bbe7eeSWei Wang goto out; 1675f5bbe7eeSWei Wang } 167660006a48SWei Wang 167735732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 167835732d01SWei Wang src_key); 167935732d01SWei Wang if (rt6_ex) 168035732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 168135732d01SWei Wang 168235732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 168335732d01SWei Wang if (!rt6_ex) { 168435732d01SWei Wang err = -ENOMEM; 168535732d01SWei Wang goto out; 168635732d01SWei Wang } 168735732d01SWei Wang rt6_ex->rt6i = nrt; 168835732d01SWei Wang rt6_ex->stamp = jiffies; 168935732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 169035732d01SWei Wang bucket->depth++; 169181eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 169235732d01SWei Wang 169335732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 169435732d01SWei Wang rt6_exception_remove_oldest(bucket); 169535732d01SWei Wang 169635732d01SWei Wang out: 169735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 169835732d01SWei Wang 169935732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1700b886d5f2SPaolo Abeni if (!err) { 17015012f0a5SDavid Ahern spin_lock_bh(&f6i->fib6_table->tb6_lock); 17025012f0a5SDavid Ahern fib6_update_sernum(net, f6i); 17035012f0a5SDavid Ahern spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1704b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1705b886d5f2SPaolo Abeni } 170635732d01SWei Wang 170735732d01SWei Wang return err; 170835732d01SWei Wang } 170935732d01SWei Wang 1710c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) 171135732d01SWei Wang { 171235732d01SWei Wang struct rt6_exception_bucket *bucket; 171335732d01SWei Wang struct rt6_exception *rt6_ex; 171435732d01SWei Wang struct hlist_node *tmp; 171535732d01SWei Wang int i; 171635732d01SWei Wang 171735732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 171835732d01SWei Wang 1719cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 172035732d01SWei Wang if (!bucket) 172135732d01SWei Wang goto out; 172235732d01SWei Wang 1723cc5c073aSDavid Ahern /* Prevent rt6_insert_exception() to recreate the bucket list */ 1724cc5c073aSDavid Ahern if (!from) 1725cc5c073aSDavid Ahern fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); 1726cc5c073aSDavid Ahern 172735732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1728cc5c073aSDavid Ahern hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { 1729cc5c073aSDavid Ahern if (!from || 1730cc5c073aSDavid Ahern rcu_access_pointer(rt6_ex->rt6i->from) == from) 173135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1732cc5c073aSDavid Ahern } 1733cc5c073aSDavid Ahern WARN_ON_ONCE(!from && bucket->depth); 173435732d01SWei Wang bucket++; 173535732d01SWei Wang } 173635732d01SWei Wang out: 173735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 173835732d01SWei Wang } 173935732d01SWei Wang 1740e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) 1741e659ba31SDavid Ahern { 1742e659ba31SDavid Ahern struct fib6_info *f6i = arg; 1743e659ba31SDavid Ahern 1744e659ba31SDavid Ahern fib6_nh_flush_exceptions(nh, f6i); 1745e659ba31SDavid Ahern 1746e659ba31SDavid Ahern return 0; 1747e659ba31SDavid Ahern } 1748e659ba31SDavid Ahern 1749c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i) 1750c0b220cfSDavid Ahern { 1751e659ba31SDavid Ahern if (f6i->nh) 1752e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, 1753e659ba31SDavid Ahern f6i); 1754e659ba31SDavid Ahern else 17551cf844c7SDavid Ahern fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); 1756c0b220cfSDavid Ahern } 1757c0b220cfSDavid Ahern 175835732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 175935732d01SWei Wang * Caller has to hold rcu_read_lock() 176035732d01SWei Wang */ 17617e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1762510e2cedSWei Wang const struct in6_addr *daddr, 1763510e2cedSWei Wang const struct in6_addr *saddr) 176435732d01SWei Wang { 1765510e2cedSWei Wang const struct in6_addr *src_key = NULL; 176635732d01SWei Wang struct rt6_exception_bucket *bucket; 176735732d01SWei Wang struct rt6_exception *rt6_ex; 17687e4b5128SDavid Ahern struct rt6_info *ret = NULL; 176935732d01SWei Wang 177035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 17717e4b5128SDavid Ahern /* fib6i_src.plen != 0 indicates f6i is in subtree 177235732d01SWei Wang * and exception table is indexed by a hash of 17737e4b5128SDavid Ahern * both fib6_dst and fib6_src. 1774510e2cedSWei Wang * However, the src addr used to create the hash 1775510e2cedSWei Wang * might not be exactly the passed in saddr which 1776510e2cedSWei Wang * is a /128 addr from the flow. 1777510e2cedSWei Wang * So we need to use f6i->fib6_src to redo lookup 1778510e2cedSWei Wang * if the passed in saddr does not find anything. 1779510e2cedSWei Wang * (See the logic in ip6_rt_cache_alloc() on how 1780510e2cedSWei Wang * rt->rt6i_src is updated.) 178135732d01SWei Wang */ 17827e4b5128SDavid Ahern if (res->f6i->fib6_src.plen) 178335732d01SWei Wang src_key = saddr; 1784510e2cedSWei Wang find_ex: 178535732d01SWei Wang #endif 1786cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); 178735732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 178835732d01SWei Wang 178935732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 17907e4b5128SDavid Ahern ret = rt6_ex->rt6i; 179135732d01SWei Wang 1792510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES 1793510e2cedSWei Wang /* Use fib6_src as src_key and redo lookup */ 1794510e2cedSWei Wang if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1795510e2cedSWei Wang src_key = &res->f6i->fib6_src.addr; 1796510e2cedSWei Wang goto find_ex; 1797510e2cedSWei Wang } 1798510e2cedSWei Wang #endif 1799510e2cedSWei Wang 18007e4b5128SDavid Ahern return ret; 180135732d01SWei Wang } 180235732d01SWei Wang 180335732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 1804cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, 1805c0b220cfSDavid Ahern const struct rt6_info *rt) 180635732d01SWei Wang { 1807c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 180835732d01SWei Wang struct rt6_exception_bucket *bucket; 180935732d01SWei Wang struct rt6_exception *rt6_ex; 181035732d01SWei Wang int err; 181135732d01SWei Wang 1812cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 181335732d01SWei Wang return -ENOENT; 181435732d01SWei Wang 181535732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1816cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1817cc5c073aSDavid Ahern 181835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1819cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1820cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1821cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 182235732d01SWei Wang * Otherwise, the exception table is indexed by 182335732d01SWei Wang * a hash of only rt6i_dst. 182435732d01SWei Wang */ 1825c0b220cfSDavid Ahern if (plen) 182635732d01SWei Wang src_key = &rt->rt6i_src.addr; 182735732d01SWei Wang #endif 182835732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 182935732d01SWei Wang &rt->rt6i_dst.addr, 183035732d01SWei Wang src_key); 183135732d01SWei Wang if (rt6_ex) { 183235732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 183335732d01SWei Wang err = 0; 183435732d01SWei Wang } else { 183535732d01SWei Wang err = -ENOENT; 183635732d01SWei Wang } 183735732d01SWei Wang 183835732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 183935732d01SWei Wang return err; 184035732d01SWei Wang } 184135732d01SWei Wang 1842e659ba31SDavid Ahern struct fib6_nh_excptn_arg { 1843e659ba31SDavid Ahern struct rt6_info *rt; 1844e659ba31SDavid Ahern int plen; 1845e659ba31SDavid Ahern }; 1846e659ba31SDavid Ahern 1847e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) 1848e659ba31SDavid Ahern { 1849e659ba31SDavid Ahern struct fib6_nh_excptn_arg *arg = _arg; 1850e659ba31SDavid Ahern int err; 1851e659ba31SDavid Ahern 1852e659ba31SDavid Ahern err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); 1853e659ba31SDavid Ahern if (err == 0) 1854e659ba31SDavid Ahern return 1; 1855e659ba31SDavid Ahern 1856e659ba31SDavid Ahern return 0; 1857e659ba31SDavid Ahern } 1858e659ba31SDavid Ahern 1859c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 1860c0b220cfSDavid Ahern { 1861c0b220cfSDavid Ahern struct fib6_info *from; 1862c0b220cfSDavid Ahern 1863c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1864cc5c073aSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1865c0b220cfSDavid Ahern return -EINVAL; 1866c0b220cfSDavid Ahern 1867e659ba31SDavid Ahern if (from->nh) { 1868e659ba31SDavid Ahern struct fib6_nh_excptn_arg arg = { 1869e659ba31SDavid Ahern .rt = rt, 1870e659ba31SDavid Ahern .plen = from->fib6_src.plen 1871e659ba31SDavid Ahern }; 1872e659ba31SDavid Ahern int rc; 1873e659ba31SDavid Ahern 1874e659ba31SDavid Ahern /* rc = 1 means an entry was found */ 1875e659ba31SDavid Ahern rc = nexthop_for_each_fib6_nh(from->nh, 1876e659ba31SDavid Ahern rt6_nh_remove_exception_rt, 1877e659ba31SDavid Ahern &arg); 1878e659ba31SDavid Ahern return rc ? 0 : -ENOENT; 1879e659ba31SDavid Ahern } 1880e659ba31SDavid Ahern 18811cf844c7SDavid Ahern return fib6_nh_remove_exception(from->fib6_nh, 1882cc5c073aSDavid Ahern from->fib6_src.plen, rt); 1883c0b220cfSDavid Ahern } 1884c0b220cfSDavid Ahern 188535732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 188635732d01SWei Wang * refresh its stamp 188735732d01SWei Wang */ 1888cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, 1889c0b220cfSDavid Ahern const struct rt6_info *rt) 189035732d01SWei Wang { 1891c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 189235732d01SWei Wang struct rt6_exception_bucket *bucket; 189335732d01SWei Wang struct rt6_exception *rt6_ex; 1894193f3685SPaolo Abeni 1895cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, NULL); 189635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1897cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1898cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1899cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 190035732d01SWei Wang * Otherwise, the exception table is indexed by 190135732d01SWei Wang * a hash of only rt6i_dst. 190235732d01SWei Wang */ 1903c0b220cfSDavid Ahern if (plen) 190435732d01SWei Wang src_key = &rt->rt6i_src.addr; 190535732d01SWei Wang #endif 1906cc5c073aSDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); 190735732d01SWei Wang if (rt6_ex) 190835732d01SWei Wang rt6_ex->stamp = jiffies; 1909c0b220cfSDavid Ahern } 191035732d01SWei Wang 1911e659ba31SDavid Ahern struct fib6_nh_match_arg { 1912e659ba31SDavid Ahern const struct net_device *dev; 1913e659ba31SDavid Ahern const struct in6_addr *gw; 1914e659ba31SDavid Ahern struct fib6_nh *match; 1915e659ba31SDavid Ahern }; 1916e659ba31SDavid Ahern 1917e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */ 1918e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) 1919e659ba31SDavid Ahern { 1920e659ba31SDavid Ahern struct fib6_nh_match_arg *arg = _arg; 1921e659ba31SDavid Ahern 1922e659ba31SDavid Ahern if (arg->dev != nh->fib_nh_dev || 1923e659ba31SDavid Ahern (arg->gw && !nh->fib_nh_gw_family) || 1924e659ba31SDavid Ahern (!arg->gw && nh->fib_nh_gw_family) || 1925e659ba31SDavid Ahern (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) 1926e659ba31SDavid Ahern return 0; 1927e659ba31SDavid Ahern 1928e659ba31SDavid Ahern arg->match = nh; 1929e659ba31SDavid Ahern 1930e659ba31SDavid Ahern /* found a match, break the loop */ 1931e659ba31SDavid Ahern return 1; 1932e659ba31SDavid Ahern } 1933e659ba31SDavid Ahern 1934c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 1935c0b220cfSDavid Ahern { 1936c0b220cfSDavid Ahern struct fib6_info *from; 1937e659ba31SDavid Ahern struct fib6_nh *fib6_nh; 1938c0b220cfSDavid Ahern 1939c0b220cfSDavid Ahern rcu_read_lock(); 1940c0b220cfSDavid Ahern 1941c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1942c0b220cfSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1943c0b220cfSDavid Ahern goto unlock; 1944c0b220cfSDavid Ahern 1945e659ba31SDavid Ahern if (from->nh) { 1946e659ba31SDavid Ahern struct fib6_nh_match_arg arg = { 1947e659ba31SDavid Ahern .dev = rt->dst.dev, 1948e659ba31SDavid Ahern .gw = &rt->rt6i_gateway, 1949e659ba31SDavid Ahern }; 1950e659ba31SDavid Ahern 1951e659ba31SDavid Ahern nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); 1952e659ba31SDavid Ahern 1953e659ba31SDavid Ahern if (!arg.match) 1954e659ba31SDavid Ahern return; 1955e659ba31SDavid Ahern fib6_nh = arg.match; 1956e659ba31SDavid Ahern } else { 1957e659ba31SDavid Ahern fib6_nh = from->fib6_nh; 1958e659ba31SDavid Ahern } 1959e659ba31SDavid Ahern fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); 1960193f3685SPaolo Abeni unlock: 196135732d01SWei Wang rcu_read_unlock(); 196235732d01SWei Wang } 196335732d01SWei Wang 1964e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1965e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1966e9fa1495SStefano Brivio { 1967e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1968e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1969e9fa1495SStefano Brivio * reflect PMTU decreases. 1970e9fa1495SStefano Brivio * 1971e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1972e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1973e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1974e9fa1495SStefano Brivio * handle this. 1975e9fa1495SStefano Brivio */ 1976e9fa1495SStefano Brivio 1977e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1978e9fa1495SStefano Brivio return true; 1979e9fa1495SStefano Brivio 1980e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1981e9fa1495SStefano Brivio return true; 1982e9fa1495SStefano Brivio 1983e9fa1495SStefano Brivio return false; 1984e9fa1495SStefano Brivio } 1985e9fa1495SStefano Brivio 1986e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 1987cc5c073aSDavid Ahern const struct fib6_nh *nh, int mtu) 1988f5bbe7eeSWei Wang { 1989f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1990f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1991f5bbe7eeSWei Wang int i; 1992f5bbe7eeSWei Wang 1993cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1994e9fa1495SStefano Brivio if (!bucket) 1995e9fa1495SStefano Brivio return; 1996e9fa1495SStefano Brivio 1997f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1998f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1999f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2000e9fa1495SStefano Brivio 2001e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 2002d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 2003f5bbe7eeSWei Wang * been updated. 2004f5bbe7eeSWei Wang */ 2005d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 2006e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 2007d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 2008f5bbe7eeSWei Wang } 2009f5bbe7eeSWei Wang bucket++; 2010f5bbe7eeSWei Wang } 2011f5bbe7eeSWei Wang } 2012f5bbe7eeSWei Wang 2013b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2014b16cb459SWei Wang 2015cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, 2016cc5c073aSDavid Ahern const struct in6_addr *gateway) 2017b16cb459SWei Wang { 2018b16cb459SWei Wang struct rt6_exception_bucket *bucket; 2019b16cb459SWei Wang struct rt6_exception *rt6_ex; 2020b16cb459SWei Wang struct hlist_node *tmp; 2021b16cb459SWei Wang int i; 2022b16cb459SWei Wang 2023cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2024b16cb459SWei Wang return; 2025b16cb459SWei Wang 2026b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 2027cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2028b16cb459SWei Wang if (bucket) { 2029b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2030b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2031b16cb459SWei Wang &bucket->chain, hlist) { 2032b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2033b16cb459SWei Wang 2034b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 2035b16cb459SWei Wang RTF_CACHE_GATEWAY && 2036b16cb459SWei Wang ipv6_addr_equal(gateway, 2037b16cb459SWei Wang &entry->rt6i_gateway)) { 2038b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 2039b16cb459SWei Wang } 2040b16cb459SWei Wang } 2041b16cb459SWei Wang bucket++; 2042b16cb459SWei Wang } 2043b16cb459SWei Wang } 2044b16cb459SWei Wang 2045b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 2046b16cb459SWei Wang } 2047b16cb459SWei Wang 2048c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 2049c757faa8SWei Wang struct rt6_exception *rt6_ex, 2050c757faa8SWei Wang struct fib6_gc_args *gc_args, 2051c757faa8SWei Wang unsigned long now) 2052c757faa8SWei Wang { 2053c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 2054c757faa8SWei Wang 20551859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 20561859bac0SPaolo Abeni * even if others have still references to them, so that on next 20571859bac0SPaolo Abeni * dst_check() such references can be dropped. 20581859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 20591859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 20601859bac0SPaolo Abeni */ 206131afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 206231afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 2063c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 2064c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2065c757faa8SWei Wang return; 206631afeb42SWei Wang } 206731afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 206831afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 206931afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 207031afeb42SWei Wang return; 207131afeb42SWei Wang } 207231afeb42SWei Wang 207331afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 2074c757faa8SWei Wang struct neighbour *neigh; 2075c757faa8SWei Wang __u8 neigh_flags = 0; 2076c757faa8SWei Wang 20771bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 20781bfa26ffSEric Dumazet if (neigh) 2079c757faa8SWei Wang neigh_flags = neigh->flags; 20801bfa26ffSEric Dumazet 2081c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 2082c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 2083c757faa8SWei Wang rt); 2084c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2085c757faa8SWei Wang return; 2086c757faa8SWei Wang } 2087c757faa8SWei Wang } 208831afeb42SWei Wang 2089c757faa8SWei Wang gc_args->more++; 2090c757faa8SWei Wang } 2091c757faa8SWei Wang 2092cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh, 2093c757faa8SWei Wang struct fib6_gc_args *gc_args, 2094c757faa8SWei Wang unsigned long now) 2095c757faa8SWei Wang { 2096c757faa8SWei Wang struct rt6_exception_bucket *bucket; 2097c757faa8SWei Wang struct rt6_exception *rt6_ex; 2098c757faa8SWei Wang struct hlist_node *tmp; 2099c757faa8SWei Wang int i; 2100c757faa8SWei Wang 2101cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2102c757faa8SWei Wang return; 2103c757faa8SWei Wang 21041bfa26ffSEric Dumazet rcu_read_lock_bh(); 21051bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 2106cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2107c757faa8SWei Wang if (bucket) { 2108c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2109c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2110c757faa8SWei Wang &bucket->chain, hlist) { 2111c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 2112c757faa8SWei Wang gc_args, now); 2113c757faa8SWei Wang } 2114c757faa8SWei Wang bucket++; 2115c757faa8SWei Wang } 2116c757faa8SWei Wang } 21171bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 21181bfa26ffSEric Dumazet rcu_read_unlock_bh(); 2119c757faa8SWei Wang } 2120c757faa8SWei Wang 2121e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg { 2122e659ba31SDavid Ahern struct fib6_gc_args *gc_args; 2123e659ba31SDavid Ahern unsigned long now; 2124e659ba31SDavid Ahern }; 2125e659ba31SDavid Ahern 2126e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) 2127e659ba31SDavid Ahern { 2128e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg *arg = _arg; 2129e659ba31SDavid Ahern 2130e659ba31SDavid Ahern fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); 2131e659ba31SDavid Ahern return 0; 2132e659ba31SDavid Ahern } 2133e659ba31SDavid Ahern 2134cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i, 2135c0b220cfSDavid Ahern struct fib6_gc_args *gc_args, 2136c0b220cfSDavid Ahern unsigned long now) 2137c0b220cfSDavid Ahern { 2138e659ba31SDavid Ahern if (f6i->nh) { 2139e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg arg = { 2140e659ba31SDavid Ahern .gc_args = gc_args, 2141e659ba31SDavid Ahern .now = now 2142e659ba31SDavid Ahern }; 2143e659ba31SDavid Ahern 2144e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, 2145e659ba31SDavid Ahern &arg); 2146e659ba31SDavid Ahern } else { 21471cf844c7SDavid Ahern fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); 2148c0b220cfSDavid Ahern } 2149e659ba31SDavid Ahern } 2150c0b220cfSDavid Ahern 21511d053da9SDavid Ahern /* must be called with rcu lock held */ 2152effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 2153effda4ddSDavid Ahern struct flowi6 *fl6, struct fib6_result *res, int strict) 21541da177e4SLinus Torvalds { 2155367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 21561da177e4SLinus Torvalds 21576454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2158367efcb9SMartin KaFai Lau saved_fn = fn; 21591da177e4SLinus Torvalds 2160ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 2161ca254490SDavid Ahern oif = 0; 2162ca254490SDavid Ahern 2163a3c00e46SMartin KaFai Lau redo_rt6_select: 2164effda4ddSDavid Ahern rt6_select(net, fn, oif, res, strict); 2165effda4ddSDavid Ahern if (res->f6i == net->ipv6.fib6_null_entry) { 2166a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2167a3c00e46SMartin KaFai Lau if (fn) 2168a3c00e46SMartin KaFai Lau goto redo_rt6_select; 2169367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 2170367efcb9SMartin KaFai Lau /* also consider unreachable route */ 2171367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 2172367efcb9SMartin KaFai Lau fn = saved_fn; 2173367efcb9SMartin KaFai Lau goto redo_rt6_select; 2174367efcb9SMartin KaFai Lau } 2175a3c00e46SMartin KaFai Lau } 2176a3c00e46SMartin KaFai Lau 2177effda4ddSDavid Ahern trace_fib6_table_lookup(net, res, table, fl6); 2178d52d3997SMartin KaFai Lau 2179effda4ddSDavid Ahern return 0; 21801d053da9SDavid Ahern } 21811d053da9SDavid Ahern 21821d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 21831d053da9SDavid Ahern int oif, struct flowi6 *fl6, 21841d053da9SDavid Ahern const struct sk_buff *skb, int flags) 21851d053da9SDavid Ahern { 2186b1d40991SDavid Ahern struct fib6_result res = {}; 21870e09edccSWei Wang struct rt6_info *rt = NULL; 21881d053da9SDavid Ahern int strict = 0; 21891d053da9SDavid Ahern 21900e09edccSWei Wang WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && 21910e09edccSWei Wang !rcu_read_lock_held()); 21920e09edccSWei Wang 21931d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 21941d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 21951d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 21961d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 21971d053da9SDavid Ahern 21981d053da9SDavid Ahern rcu_read_lock(); 21991d053da9SDavid Ahern 2200effda4ddSDavid Ahern fib6_table_lookup(net, table, oif, fl6, &res, strict); 22010e09edccSWei Wang if (res.f6i == net->ipv6.fib6_null_entry) 22020e09edccSWei Wang goto out; 220323fb93a4SDavid Ahern 2204b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, oif, false, skb, strict); 2205d83009d4SDavid Ahern 220623fb93a4SDavid Ahern /*Search through exception table */ 22077e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 220823fb93a4SDavid Ahern if (rt) { 22090e09edccSWei Wang goto out; 22103da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 2211b1d40991SDavid Ahern !res.nh->fib_nh_gw_family)) { 22123da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 22133da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 22143da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 22153da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 22163da59bd9SMartin KaFai Lau */ 22170e09edccSWei Wang rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 22183da59bd9SMartin KaFai Lau 22190e09edccSWei Wang if (rt) { 22200e09edccSWei Wang /* 1 refcnt is taken during ip6_rt_cache_alloc(). 22210e09edccSWei Wang * As rt6_uncached_list_add() does not consume refcnt, 22220e09edccSWei Wang * this refcnt is always returned to the caller even 22230e09edccSWei Wang * if caller sets RT6_LOOKUP_F_DST_NOREF flag. 22240e09edccSWei Wang */ 22250e09edccSWei Wang rt6_uncached_list_add(rt); 22260e09edccSWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 22274d85cd0cSDavid Ahern rcu_read_unlock(); 22283da59bd9SMartin KaFai Lau 22290e09edccSWei Wang return rt; 22301cfb71eeSWei Wang } 2231d52d3997SMartin KaFai Lau } else { 2232d52d3997SMartin KaFai Lau /* Get a percpu copy */ 2233951f788aSEric Dumazet local_bh_disable(); 22340e09edccSWei Wang rt = rt6_get_pcpu_route(&res); 2235d52d3997SMartin KaFai Lau 22360e09edccSWei Wang if (!rt) 22370e09edccSWei Wang rt = rt6_make_pcpu_route(net, &res); 223893531c67SDavid Ahern 2239951f788aSEric Dumazet local_bh_enable(); 22400e09edccSWei Wang } 22410e09edccSWei Wang out: 22420e09edccSWei Wang if (!rt) 22430e09edccSWei Wang rt = net->ipv6.ip6_null_entry; 22440e09edccSWei Wang if (!(flags & RT6_LOOKUP_F_DST_NOREF)) 22450e09edccSWei Wang ip6_hold_safe(net, &rt); 2246951f788aSEric Dumazet rcu_read_unlock(); 2247d4bea421SDavid Ahern 22480e09edccSWei Wang return rt; 2249c71099acSThomas Graf } 22509ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 2251c71099acSThomas Graf 2252b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 2253b75cc8f9SDavid Ahern struct fib6_table *table, 2254b75cc8f9SDavid Ahern struct flowi6 *fl6, 2255b75cc8f9SDavid Ahern const struct sk_buff *skb, 2256b75cc8f9SDavid Ahern int flags) 22574acad72dSPavel Emelyanov { 2258b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 22594acad72dSPavel Emelyanov } 22604acad72dSPavel Emelyanov 2261d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 226272331bc0SShmulik Ladkani struct net_device *dev, 2263b75cc8f9SDavid Ahern struct flowi6 *fl6, 2264b75cc8f9SDavid Ahern const struct sk_buff *skb, 2265b75cc8f9SDavid Ahern int flags) 226672331bc0SShmulik Ladkani { 226772331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 226872331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 226972331bc0SShmulik Ladkani 2270b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 227172331bc0SShmulik Ladkani } 2272d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 227372331bc0SShmulik Ladkani 227423aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 22755e5d6fedSRoopa Prabhu struct flow_keys *keys, 22765e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 227723aebdacSJakub Sitnicki { 227823aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 227923aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 22805e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 228123aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 228223aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 228323aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 2284cea67a2dSEric Dumazet struct icmp6hdr _icmph; 228523aebdacSJakub Sitnicki 228623aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 228723aebdacSJakub Sitnicki goto out; 228823aebdacSJakub Sitnicki 2289cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2290cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 2291cea67a2dSEric Dumazet if (!icmph) 2292cea67a2dSEric Dumazet goto out; 2293cea67a2dSEric Dumazet 229423aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 229523aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 229623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 229723aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 229823aebdacSJakub Sitnicki goto out; 229923aebdacSJakub Sitnicki 230023aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 230123aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 230223aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 230323aebdacSJakub Sitnicki if (!inner_iph) 230423aebdacSJakub Sitnicki goto out; 230523aebdacSJakub Sitnicki 230623aebdacSJakub Sitnicki key_iph = inner_iph; 23075e5d6fedSRoopa Prabhu _flkeys = NULL; 230823aebdacSJakub Sitnicki out: 23095e5d6fedSRoopa Prabhu if (_flkeys) { 23105e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 23115e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 23125e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 23135e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 23145e5d6fedSRoopa Prabhu } else { 231523aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 231623aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 2317fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 231823aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 231923aebdacSJakub Sitnicki } 23205e5d6fedSRoopa Prabhu } 232123aebdacSJakub Sitnicki 232223aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 2323b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2324b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 232523aebdacSJakub Sitnicki { 232623aebdacSJakub Sitnicki struct flow_keys hash_keys; 23279a2a537aSDavid Ahern u32 mhash; 232823aebdacSJakub Sitnicki 2329bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 2330b4bac172SDavid Ahern case 0: 23316f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 23326f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 23339a2a537aSDavid Ahern if (skb) { 23345e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 23359a2a537aSDavid Ahern } else { 23369a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 23379a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2338fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 23399a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 234023aebdacSJakub Sitnicki } 2341b4bac172SDavid Ahern break; 2342b4bac172SDavid Ahern case 1: 2343b4bac172SDavid Ahern if (skb) { 2344b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2345b4bac172SDavid Ahern struct flow_keys keys; 2346b4bac172SDavid Ahern 2347b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2348b4bac172SDavid Ahern if (skb->l4_hash) 2349b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2350b4bac172SDavid Ahern 2351b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2352b4bac172SDavid Ahern 2353b4bac172SDavid Ahern if (!flkeys) { 2354b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2355b4bac172SDavid Ahern flkeys = &keys; 2356b4bac172SDavid Ahern } 2357b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2358b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2359b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2360b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2361b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2362b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2363b4bac172SDavid Ahern } else { 2364b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2365b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2366b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2367b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2368b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2369b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2370b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2371b4bac172SDavid Ahern } 2372b4bac172SDavid Ahern break; 2373*d8f74f09SStephen Suryaputra case 2: 2374*d8f74f09SStephen Suryaputra memset(&hash_keys, 0, sizeof(hash_keys)); 2375*d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2376*d8f74f09SStephen Suryaputra if (skb) { 2377*d8f74f09SStephen Suryaputra struct flow_keys keys; 2378*d8f74f09SStephen Suryaputra 2379*d8f74f09SStephen Suryaputra if (!flkeys) { 2380*d8f74f09SStephen Suryaputra skb_flow_dissect_flow_keys(skb, &keys, 0); 2381*d8f74f09SStephen Suryaputra flkeys = &keys; 2382*d8f74f09SStephen Suryaputra } 2383*d8f74f09SStephen Suryaputra 2384*d8f74f09SStephen Suryaputra /* Inner can be v4 or v6 */ 2385*d8f74f09SStephen Suryaputra if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2386*d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2387*d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2388*d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2389*d8f74f09SStephen Suryaputra } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2390*d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2391*d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2392*d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2393*d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = flkeys->tags.flow_label; 2394*d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2395*d8f74f09SStephen Suryaputra } else { 2396*d8f74f09SStephen Suryaputra /* Same as case 0 */ 2397*d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2398*d8f74f09SStephen Suryaputra ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 2399*d8f74f09SStephen Suryaputra } 2400*d8f74f09SStephen Suryaputra } else { 2401*d8f74f09SStephen Suryaputra /* Same as case 0 */ 2402*d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2403*d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = fl6->saddr; 2404*d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = fl6->daddr; 2405*d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2406*d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = fl6->flowi6_proto; 2407*d8f74f09SStephen Suryaputra } 2408*d8f74f09SStephen Suryaputra break; 2409b4bac172SDavid Ahern } 24109a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 241123aebdacSJakub Sitnicki 24129a2a537aSDavid Ahern return mhash >> 1; 241323aebdacSJakub Sitnicki } 241423aebdacSJakub Sitnicki 241567f415ddSWei Wang /* Called with rcu held */ 2416c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2417c71099acSThomas Graf { 2418b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2419c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 242067f415ddSWei Wang int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; 2421904af04dSJiri Benc struct ip_tunnel_info *tun_info; 24224c9483b2SDavid S. Miller struct flowi6 fl6 = { 2423e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 24244c9483b2SDavid S. Miller .daddr = iph->daddr, 24254c9483b2SDavid S. Miller .saddr = iph->saddr, 24266502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 24274c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 24284c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2429c71099acSThomas Graf }; 24305e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2431adaa70bbSThomas Graf 2432904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 243346fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2434904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 24355e5d6fedSRoopa Prabhu 24365e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 24375e5d6fedSRoopa Prabhu flkeys = &_flkeys; 24385e5d6fedSRoopa Prabhu 243923aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2440b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 244106e9d040SJiri Benc skb_dst_drop(skb); 244267f415ddSWei Wang skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, 244367f415ddSWei Wang &fl6, skb, flags)); 2444c71099acSThomas Graf } 2445c71099acSThomas Graf 2446b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2447b75cc8f9SDavid Ahern struct fib6_table *table, 2448b75cc8f9SDavid Ahern struct flowi6 *fl6, 2449b75cc8f9SDavid Ahern const struct sk_buff *skb, 2450b75cc8f9SDavid Ahern int flags) 2451c71099acSThomas Graf { 2452b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2453c71099acSThomas Graf } 2454c71099acSThomas Graf 24557d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags_noref(struct net *net, 24567d9e5f42SWei Wang const struct sock *sk, 24576f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2458c71099acSThomas Graf { 2459d46a9d67SDavid Ahern bool any_src; 2460c71099acSThomas Graf 24613ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 24623ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 24634c1feac5SDavid Ahern struct dst_entry *dst; 24644c1feac5SDavid Ahern 24657d9e5f42SWei Wang /* This function does not take refcnt on the dst */ 24664c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2467ca254490SDavid Ahern if (dst) 2468ca254490SDavid Ahern return dst; 24694c1feac5SDavid Ahern } 2470ca254490SDavid Ahern 24711fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 24724dc27d1cSDavid McCullough 24737d9e5f42SWei Wang flags |= RT6_LOOKUP_F_DST_NOREF; 2474d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2475741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2476d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 247777d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2478c71099acSThomas Graf 2479d46a9d67SDavid Ahern if (!any_src) 2480adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 24810c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 24820c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2483adaa70bbSThomas Graf 2484b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 24851da177e4SLinus Torvalds } 24867d9e5f42SWei Wang EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref); 24877d9e5f42SWei Wang 24887d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net, 24897d9e5f42SWei Wang const struct sock *sk, 24907d9e5f42SWei Wang struct flowi6 *fl6, 24917d9e5f42SWei Wang int flags) 24927d9e5f42SWei Wang { 24937d9e5f42SWei Wang struct dst_entry *dst; 24947d9e5f42SWei Wang struct rt6_info *rt6; 24957d9e5f42SWei Wang 24967d9e5f42SWei Wang rcu_read_lock(); 24977d9e5f42SWei Wang dst = ip6_route_output_flags_noref(net, sk, fl6, flags); 24987d9e5f42SWei Wang rt6 = (struct rt6_info *)dst; 24997d9e5f42SWei Wang /* For dst cached in uncached_list, refcnt is already taken. */ 25007d9e5f42SWei Wang if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) { 25017d9e5f42SWei Wang dst = &net->ipv6.ip6_null_entry->dst; 25027d9e5f42SWei Wang dst_hold(dst); 25037d9e5f42SWei Wang } 25047d9e5f42SWei Wang rcu_read_unlock(); 25057d9e5f42SWei Wang 25067d9e5f42SWei Wang return dst; 25077d9e5f42SWei Wang } 25086f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 25091da177e4SLinus Torvalds 25102774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 251114e50e57SDavid S. Miller { 25125c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 25131dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 251414e50e57SDavid S. Miller struct dst_entry *new = NULL; 251514e50e57SDavid S. Miller 25161dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 251762cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 251814e50e57SDavid S. Miller if (rt) { 25190a1f5962SMartin KaFai Lau rt6_info_init(rt); 252081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 25210a1f5962SMartin KaFai Lau 2522d8d1f30bSChangli Gao new = &rt->dst; 252314e50e57SDavid S. Miller new->__use = 1; 2524352e512cSHerbert Xu new->input = dst_discard; 2525ede2059dSEric W. Biederman new->output = dst_discard_out; 252614e50e57SDavid S. Miller 2527defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 252814e50e57SDavid S. Miller 25291dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 25304e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 25310a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 253214e50e57SDavid S. Miller 253314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 253414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 253514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 253614e50e57SDavid S. Miller #endif 253714e50e57SDavid S. Miller } 253814e50e57SDavid S. Miller 253969ead7afSDavid S. Miller dst_release(dst_orig); 254069ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 254114e50e57SDavid S. Miller } 254214e50e57SDavid S. Miller 25431da177e4SLinus Torvalds /* 25441da177e4SLinus Torvalds * Destination cache support functions 25451da177e4SLinus Torvalds */ 25461da177e4SLinus Torvalds 25478d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 25483da59bd9SMartin KaFai Lau { 254936143645SSteffen Klassert u32 rt_cookie = 0; 2550c5cff856SWei Wang 25518ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 255293531c67SDavid Ahern return false; 255393531c67SDavid Ahern 255493531c67SDavid Ahern if (fib6_check_expired(f6i)) 255593531c67SDavid Ahern return false; 255693531c67SDavid Ahern 255793531c67SDavid Ahern return true; 255893531c67SDavid Ahern } 255993531c67SDavid Ahern 2560a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2561a68886a6SDavid Ahern struct fib6_info *from, 2562a68886a6SDavid Ahern u32 cookie) 25633da59bd9SMartin KaFai Lau { 2564c5cff856SWei Wang u32 rt_cookie = 0; 2565c5cff856SWei Wang 2566a68886a6SDavid Ahern if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || 256793531c67SDavid Ahern rt_cookie != cookie) 25683da59bd9SMartin KaFai Lau return NULL; 25693da59bd9SMartin KaFai Lau 25703da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 25713da59bd9SMartin KaFai Lau return NULL; 25723da59bd9SMartin KaFai Lau 25733da59bd9SMartin KaFai Lau return &rt->dst; 25743da59bd9SMartin KaFai Lau } 25753da59bd9SMartin KaFai Lau 2576a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2577a68886a6SDavid Ahern struct fib6_info *from, 2578a68886a6SDavid Ahern u32 cookie) 25793da59bd9SMartin KaFai Lau { 25805973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 25815973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2582a68886a6SDavid Ahern fib6_check(from, cookie)) 25833da59bd9SMartin KaFai Lau return &rt->dst; 25843da59bd9SMartin KaFai Lau else 25853da59bd9SMartin KaFai Lau return NULL; 25863da59bd9SMartin KaFai Lau } 25873da59bd9SMartin KaFai Lau 25881da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 25891da177e4SLinus Torvalds { 2590a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2591a68886a6SDavid Ahern struct fib6_info *from; 25921da177e4SLinus Torvalds struct rt6_info *rt; 25931da177e4SLinus Torvalds 2594a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2595a87b7dc9SDavid Ahern 2596a87b7dc9SDavid Ahern rcu_read_lock(); 25971da177e4SLinus Torvalds 25986f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 25996f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 26006f3118b5SNicolas Dichtel * into this function always. 26016f3118b5SNicolas Dichtel */ 2602e3bc10bdSHannes Frederic Sowa 2603a68886a6SDavid Ahern from = rcu_dereference(rt->from); 26044b32b5adSMartin KaFai Lau 2605a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2606a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2607a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 26083da59bd9SMartin KaFai Lau else 2609a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2610a87b7dc9SDavid Ahern 2611a87b7dc9SDavid Ahern rcu_read_unlock(); 2612a87b7dc9SDavid Ahern 2613a87b7dc9SDavid Ahern return dst_ret; 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 26171da177e4SLinus Torvalds { 26181da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds if (rt) { 262154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2622c3c14da0SDavid Ahern rcu_read_lock(); 262354c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 262493531c67SDavid Ahern rt6_remove_exception_rt(rt); 262554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 26261da177e4SLinus Torvalds } 2627c3c14da0SDavid Ahern rcu_read_unlock(); 262854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 262954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 263054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 263154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 263254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 263354c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 26371da177e4SLinus Torvalds { 26381da177e4SLinus Torvalds struct rt6_info *rt; 26391da177e4SLinus Torvalds 26403ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 26411da177e4SLinus Torvalds 2642adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 26431da177e4SLinus Torvalds if (rt) { 26448a14e46fSDavid Ahern rcu_read_lock(); 26451eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 264693531c67SDavid Ahern rt6_remove_exception_rt(rt); 2647c5cff856SWei Wang } else { 2648a68886a6SDavid Ahern struct fib6_info *from; 2649c5cff856SWei Wang struct fib6_node *fn; 2650c5cff856SWei Wang 2651a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2652a68886a6SDavid Ahern if (from) { 2653a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2654c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2655c5cff856SWei Wang fn->fn_sernum = -1; 2656a68886a6SDavid Ahern } 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds rcu_read_unlock(); 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds 26626a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 26636a3e030fSDavid Ahern { 2664a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2665a68886a6SDavid Ahern struct fib6_info *from; 2666a68886a6SDavid Ahern 2667a68886a6SDavid Ahern rcu_read_lock(); 2668a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2669a68886a6SDavid Ahern if (from) 2670a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2671a68886a6SDavid Ahern rcu_read_unlock(); 2672a68886a6SDavid Ahern } 26736a3e030fSDavid Ahern 26746a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 26756a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 26766700c270SDavid S. Miller } 26771da177e4SLinus Torvalds 267845e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 267945e4fd26SMartin KaFai Lau { 268045e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 268145e4fd26SMartin KaFai Lau 2682d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 268345e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 268445e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 268545e4fd26SMartin KaFai Lau } 268645e4fd26SMartin KaFai Lau 26870d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 26880d3f6d29SMartin KaFai Lau { 26890d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 26901490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 26910d3f6d29SMartin KaFai Lau } 26920d3f6d29SMartin KaFai Lau 269345e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 269445e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 26951da177e4SLinus Torvalds { 26960dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 26971da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 26981da177e4SLinus Torvalds 269919bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 270019bda36cSXin Long return; 270119bda36cSXin Long 270245e4fd26SMartin KaFai Lau if (iph) { 270345e4fd26SMartin KaFai Lau daddr = &iph->daddr; 270445e4fd26SMartin KaFai Lau saddr = &iph->saddr; 270545e4fd26SMartin KaFai Lau } else if (sk) { 270645e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 270745e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 270845e4fd26SMartin KaFai Lau } else { 27090dec879fSJulian Anastasov daddr = NULL; 27100dec879fSJulian Anastasov saddr = NULL; 27111da177e4SLinus Torvalds } 27120dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 27130dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 27140dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 27150dec879fSJulian Anastasov return; 27160dec879fSJulian Anastasov 27170dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 27180dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 27192b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 27202b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 27212b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 27220dec879fSJulian Anastasov } else if (daddr) { 272385bd05deSDavid Ahern struct fib6_result res = {}; 27240dec879fSJulian Anastasov struct rt6_info *nrt6; 27250dec879fSJulian Anastasov 27264d85cd0cSDavid Ahern rcu_read_lock(); 272785bd05deSDavid Ahern res.f6i = rcu_dereference(rt6->from); 272885bd05deSDavid Ahern if (!res.f6i) { 27299c69a132SJonathan Lemon rcu_read_unlock(); 27309c69a132SJonathan Lemon return; 27319c69a132SJonathan Lemon } 27327d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 27337d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 27347d21fec9SDavid Ahern 27352d44234bSDavid Ahern if (res.f6i->nh) { 27362d44234bSDavid Ahern struct fib6_nh_match_arg arg = { 27372d44234bSDavid Ahern .dev = dst->dev, 27382d44234bSDavid Ahern .gw = &rt6->rt6i_gateway, 27392d44234bSDavid Ahern }; 27402d44234bSDavid Ahern 27412d44234bSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 27422d44234bSDavid Ahern fib6_nh_find_match, &arg); 27432d44234bSDavid Ahern 27442d44234bSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 27452d44234bSDavid Ahern * using the dst->dev + gw. Should be impossible. 27462d44234bSDavid Ahern */ 27472d44234bSDavid Ahern if (!arg.match) { 27482d44234bSDavid Ahern rcu_read_unlock(); 27492d44234bSDavid Ahern return; 27502d44234bSDavid Ahern } 27512d44234bSDavid Ahern 27522d44234bSDavid Ahern res.nh = arg.match; 27532d44234bSDavid Ahern } else { 27542d44234bSDavid Ahern res.nh = res.f6i->fib6_nh; 27552d44234bSDavid Ahern } 27562d44234bSDavid Ahern 275785bd05deSDavid Ahern nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 275845e4fd26SMartin KaFai Lau if (nrt6) { 275945e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 27605012f0a5SDavid Ahern if (rt6_insert_exception(nrt6, &res)) 27612b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 276245e4fd26SMartin KaFai Lau } 2763a68886a6SDavid Ahern rcu_read_unlock(); 276445e4fd26SMartin KaFai Lau } 276545e4fd26SMartin KaFai Lau } 276645e4fd26SMartin KaFai Lau 276745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 276845e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 276945e4fd26SMartin KaFai Lau { 277045e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds 277342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2774e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 277581aded24SDavid S. Miller { 277681aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 277781aded24SDavid S. Miller struct dst_entry *dst; 2778dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2779dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2780dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2781dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2782dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2783dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2784dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2785dc92095dSMaciej Żenczykowski }; 278681aded24SDavid S. Miller 278781aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 278881aded24SDavid S. Miller if (!dst->error) 278945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 279081aded24SDavid S. Miller dst_release(dst); 279181aded24SDavid S. Miller } 279281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 279381aded24SDavid S. Miller 279481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 279581aded24SDavid S. Miller { 27967ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 279733c162a9SMartin KaFai Lau struct dst_entry *dst; 279833c162a9SMartin KaFai Lau 27997ddacfa5SDavid Ahern if (!oif && skb->dev) 28007ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 28017ddacfa5SDavid Ahern 28027ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 280333c162a9SMartin KaFai Lau 280433c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 280533c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 280633c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 280733c162a9SMartin KaFai Lau return; 280833c162a9SMartin KaFai Lau 280933c162a9SMartin KaFai Lau bh_lock_sock(sk); 281033c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 281133c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 281233c162a9SMartin KaFai Lau bh_unlock_sock(sk); 281381aded24SDavid S. Miller } 281481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 281581aded24SDavid S. Miller 28167d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 28177d6850f7SAlexey Kodanev const struct flowi6 *fl6) 28187d6850f7SAlexey Kodanev { 28197d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 28207d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 28217d6850f7SAlexey Kodanev #endif 28227d6850f7SAlexey Kodanev 28237d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 28247d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 28257d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 28267d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 28277d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 28287d6850f7SAlexey Kodanev &np->saddr : 28297d6850f7SAlexey Kodanev #endif 28307d6850f7SAlexey Kodanev NULL); 28317d6850f7SAlexey Kodanev } 28327d6850f7SAlexey Kodanev 28339b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res, 28340b34eb00SDavid Ahern struct flowi6 *fl6, 28350b34eb00SDavid Ahern const struct in6_addr *gw, 28360b34eb00SDavid Ahern struct rt6_info **ret) 28370b34eb00SDavid Ahern { 28389b6b35abSDavid Ahern const struct fib6_nh *nh = res->nh; 28399b6b35abSDavid Ahern 28400b34eb00SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 28410b34eb00SDavid Ahern fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 28420b34eb00SDavid Ahern return false; 28430b34eb00SDavid Ahern 28440b34eb00SDavid Ahern /* rt_cache's gateway might be different from its 'parent' 28450b34eb00SDavid Ahern * in the case of an ip redirect. 28460b34eb00SDavid Ahern * So we keep searching in the exception table if the gateway 28470b34eb00SDavid Ahern * is different. 28480b34eb00SDavid Ahern */ 28490b34eb00SDavid Ahern if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 28500b34eb00SDavid Ahern struct rt6_info *rt_cache; 28510b34eb00SDavid Ahern 28529b6b35abSDavid Ahern rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 28530b34eb00SDavid Ahern if (rt_cache && 28540b34eb00SDavid Ahern ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 28550b34eb00SDavid Ahern *ret = rt_cache; 28560b34eb00SDavid Ahern return true; 28570b34eb00SDavid Ahern } 28580b34eb00SDavid Ahern return false; 28590b34eb00SDavid Ahern } 28600b34eb00SDavid Ahern return true; 28610b34eb00SDavid Ahern } 28620b34eb00SDavid Ahern 2863c55c8988SDavid Ahern struct fib6_nh_rd_arg { 2864c55c8988SDavid Ahern struct fib6_result *res; 2865c55c8988SDavid Ahern struct flowi6 *fl6; 2866c55c8988SDavid Ahern const struct in6_addr *gw; 2867c55c8988SDavid Ahern struct rt6_info **ret; 2868c55c8988SDavid Ahern }; 2869c55c8988SDavid Ahern 2870c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) 2871c55c8988SDavid Ahern { 2872c55c8988SDavid Ahern struct fib6_nh_rd_arg *arg = _arg; 2873c55c8988SDavid Ahern 2874c55c8988SDavid Ahern arg->res->nh = nh; 2875c55c8988SDavid Ahern return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); 2876c55c8988SDavid Ahern } 2877c55c8988SDavid Ahern 2878b55b76b2SDuan Jiong /* Handle redirects */ 2879b55b76b2SDuan Jiong struct ip6rd_flowi { 2880b55b76b2SDuan Jiong struct flowi6 fl6; 2881b55b76b2SDuan Jiong struct in6_addr gateway; 2882b55b76b2SDuan Jiong }; 2883b55b76b2SDuan Jiong 2884b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2885b55b76b2SDuan Jiong struct fib6_table *table, 2886b55b76b2SDuan Jiong struct flowi6 *fl6, 2887b75cc8f9SDavid Ahern const struct sk_buff *skb, 2888b55b76b2SDuan Jiong int flags) 2889b55b76b2SDuan Jiong { 2890b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 28910b34eb00SDavid Ahern struct rt6_info *ret = NULL; 28929b6b35abSDavid Ahern struct fib6_result res = {}; 2893c55c8988SDavid Ahern struct fib6_nh_rd_arg arg = { 2894c55c8988SDavid Ahern .res = &res, 2895c55c8988SDavid Ahern .fl6 = fl6, 2896c55c8988SDavid Ahern .gw = &rdfl->gateway, 2897c55c8988SDavid Ahern .ret = &ret 2898c55c8988SDavid Ahern }; 28998d1c802bSDavid Ahern struct fib6_info *rt; 2900b55b76b2SDuan Jiong struct fib6_node *fn; 2901b55b76b2SDuan Jiong 290231680ac2SDavid Ahern /* l3mdev_update_flow overrides oif if the device is enslaved; in 290331680ac2SDavid Ahern * this case we must match on the real ingress device, so reset it 290431680ac2SDavid Ahern */ 290531680ac2SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 290631680ac2SDavid Ahern fl6->flowi6_oif = skb->dev->ifindex; 290731680ac2SDavid Ahern 2908b55b76b2SDuan Jiong /* Get the "current" route for this destination and 290967c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2910b55b76b2SDuan Jiong * 2911b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2912b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2913b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2914b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2915b55b76b2SDuan Jiong * routes. 2916b55b76b2SDuan Jiong */ 2917b55b76b2SDuan Jiong 291866f5d6ceSWei Wang rcu_read_lock(); 29196454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2920b55b76b2SDuan Jiong restart: 292166f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 29229b6b35abSDavid Ahern res.f6i = rt; 292314895687SDavid Ahern if (fib6_check_expired(rt)) 2924b55b76b2SDuan Jiong continue; 292593c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2926b55b76b2SDuan Jiong break; 2927c55c8988SDavid Ahern if (unlikely(rt->nh)) { 2928c55c8988SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 2929c55c8988SDavid Ahern continue; 2930c55c8988SDavid Ahern /* on match, res->nh is filled in and potentially ret */ 2931c55c8988SDavid Ahern if (nexthop_for_each_fib6_nh(rt->nh, 2932c55c8988SDavid Ahern fib6_nh_redirect_match, 2933c55c8988SDavid Ahern &arg)) 29340b34eb00SDavid Ahern goto out; 2935c55c8988SDavid Ahern } else { 2936c55c8988SDavid Ahern res.nh = rt->fib6_nh; 2937c55c8988SDavid Ahern if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, 2938c55c8988SDavid Ahern &ret)) 2939c55c8988SDavid Ahern goto out; 2940c55c8988SDavid Ahern } 2941b55b76b2SDuan Jiong } 2942b55b76b2SDuan Jiong 2943b55b76b2SDuan Jiong if (!rt) 2944421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 294593c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 294623fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2947b0a1ba59SMartin KaFai Lau goto out; 2948b0a1ba59SMartin KaFai Lau } 2949b0a1ba59SMartin KaFai Lau 2950421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2951a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2952a3c00e46SMartin KaFai Lau if (fn) 2953a3c00e46SMartin KaFai Lau goto restart; 2954b55b76b2SDuan Jiong } 2955a3c00e46SMartin KaFai Lau 29569b6b35abSDavid Ahern res.f6i = rt; 29571cf844c7SDavid Ahern res.nh = rt->fib6_nh; 2958b0a1ba59SMartin KaFai Lau out: 29597d21fec9SDavid Ahern if (ret) { 296010585b43SDavid Ahern ip6_hold_safe(net, &ret); 29617d21fec9SDavid Ahern } else { 29627d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 29637d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 29649b6b35abSDavid Ahern ret = ip6_create_rt_rcu(&res); 29657d21fec9SDavid Ahern } 2966b55b76b2SDuan Jiong 296766f5d6ceSWei Wang rcu_read_unlock(); 2968b55b76b2SDuan Jiong 29698ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 297023fb93a4SDavid Ahern return ret; 2971b55b76b2SDuan Jiong }; 2972b55b76b2SDuan Jiong 2973b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2974b55b76b2SDuan Jiong const struct flowi6 *fl6, 2975b75cc8f9SDavid Ahern const struct sk_buff *skb, 2976b55b76b2SDuan Jiong const struct in6_addr *gateway) 2977b55b76b2SDuan Jiong { 2978b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2979b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2980b55b76b2SDuan Jiong 2981b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2982b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2983b55b76b2SDuan Jiong 2984b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2985b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2986b55b76b2SDuan Jiong } 2987b55b76b2SDuan Jiong 2988e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2989e2d118a1SLorenzo Colitti kuid_t uid) 29903a5ad2eeSDavid S. Miller { 29913a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 29923a5ad2eeSDavid S. Miller struct dst_entry *dst; 29931f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 29941f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 29951f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 29961f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 29971f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 29981f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 29991f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 30001f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 30011f7f10acSMaciej Żenczykowski }; 30023a5ad2eeSDavid S. Miller 3003b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 30046700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 30053a5ad2eeSDavid S. Miller dst_release(dst); 30063a5ad2eeSDavid S. Miller } 30073a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 30083a5ad2eeSDavid S. Miller 3009d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 3010c92a59ecSDuan Jiong { 3011c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 3012c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 3013c92a59ecSDuan Jiong struct dst_entry *dst; 30140b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 30150b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 30160b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 30170b26fb17SMaciej Żenczykowski .daddr = msg->dest, 30180b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 30190b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 30200b26fb17SMaciej Żenczykowski }; 3021c92a59ecSDuan Jiong 3022b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 3023c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 3024c92a59ecSDuan Jiong dst_release(dst); 3025c92a59ecSDuan Jiong } 3026c92a59ecSDuan Jiong 30273a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 30283a5ad2eeSDavid S. Miller { 3029e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 3030e2d118a1SLorenzo Colitti sk->sk_uid); 30313a5ad2eeSDavid S. Miller } 30323a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 30333a5ad2eeSDavid S. Miller 30340dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 30351da177e4SLinus Torvalds { 30360dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 30370dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 30380dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 30390dbaee3bSDavid S. Miller 30401da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 30411da177e4SLinus Torvalds 30425578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 30435578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds /* 30461da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 30471da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 30481da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 30491da177e4SLinus Torvalds * rely only on pmtu discovery" 30501da177e4SLinus Torvalds */ 30511da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 30521da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 30531da177e4SLinus Torvalds return mtu; 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds 3056ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 3057d33e4553SDavid S. Miller { 3058d33e4553SDavid S. Miller struct inet6_dev *idev; 3059d4ead6b3SDavid Ahern unsigned int mtu; 3060618f9bc7SSteffen Klassert 30614b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 30624b32b5adSMartin KaFai Lau if (mtu) 30634b32b5adSMartin KaFai Lau goto out; 30644b32b5adSMartin KaFai Lau 3065618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 3066d33e4553SDavid S. Miller 3067d33e4553SDavid S. Miller rcu_read_lock(); 3068d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 3069d33e4553SDavid S. Miller if (idev) 3070d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 3071d33e4553SDavid S. Miller rcu_read_unlock(); 3072d33e4553SDavid S. Miller 307330f78d8eSEric Dumazet out: 307414972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 307514972cbdSRoopa Prabhu 307614972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 3077d33e4553SDavid S. Miller } 3078d33e4553SDavid S. Miller 3079901731b8SDavid Ahern /* MTU selection: 3080901731b8SDavid Ahern * 1. mtu on route is locked - use it 3081901731b8SDavid Ahern * 2. mtu from nexthop exception 3082901731b8SDavid Ahern * 3. mtu from egress device 3083901731b8SDavid Ahern * 3084901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 3085901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 3086901731b8SDavid Ahern */ 3087b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res, 3088b748f260SDavid Ahern const struct in6_addr *daddr, 3089b748f260SDavid Ahern const struct in6_addr *saddr) 3090901731b8SDavid Ahern { 3091b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 3092b748f260SDavid Ahern struct fib6_info *f6i = res->f6i; 3093901731b8SDavid Ahern struct inet6_dev *idev; 3094510e2cedSWei Wang struct rt6_info *rt; 3095901731b8SDavid Ahern u32 mtu = 0; 3096901731b8SDavid Ahern 3097901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 3098901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 3099901731b8SDavid Ahern if (mtu) 3100901731b8SDavid Ahern goto out; 3101901731b8SDavid Ahern } 3102901731b8SDavid Ahern 3103510e2cedSWei Wang rt = rt6_find_cached_rt(res, daddr, saddr); 3104510e2cedSWei Wang if (unlikely(rt)) { 3105510e2cedSWei Wang mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 3106510e2cedSWei Wang } else { 3107b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 3108901731b8SDavid Ahern 3109901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 3110901731b8SDavid Ahern idev = __in6_dev_get(dev); 3111901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 3112901731b8SDavid Ahern mtu = idev->cnf.mtu6; 3113901731b8SDavid Ahern } 3114901731b8SDavid Ahern 3115901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 3116901731b8SDavid Ahern out: 3117b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 3118901731b8SDavid Ahern } 3119901731b8SDavid Ahern 31203b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 312187a11578SDavid S. Miller struct flowi6 *fl6) 31221da177e4SLinus Torvalds { 312387a11578SDavid S. Miller struct dst_entry *dst; 31241da177e4SLinus Torvalds struct rt6_info *rt; 31251da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 3126c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 31271da177e4SLinus Torvalds 312838308473SDavid S. Miller if (unlikely(!idev)) 3129122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 31301da177e4SLinus Torvalds 3131ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 313238308473SDavid S. Miller if (unlikely(!rt)) { 31331da177e4SLinus Torvalds in6_dev_put(idev); 313487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 31351da177e4SLinus Torvalds goto out; 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 31388e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 3139588753f1SBrendan McGrath rt->dst.input = ip6_input; 31408e2ec639SYan, Zheng rt->dst.output = ip6_output; 3141550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 314287a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 31438e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 31448e2ec639SYan, Zheng rt->rt6i_idev = idev; 314514edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 31461da177e4SLinus Torvalds 31474c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 3148587fea74SWei Wang * do proper release of the net_device 3149587fea74SWei Wang */ 3150587fea74SWei Wang rt6_uncached_list_add(rt); 315181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 31521da177e4SLinus Torvalds 315387a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 315487a11578SDavid S. Miller 31551da177e4SLinus Torvalds out: 315687a11578SDavid S. Miller return dst; 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds 3159569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 31601da177e4SLinus Torvalds { 316186393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 31627019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 31637019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 31647019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 31657019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 31667019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 3167fc66f95cSEric Dumazet int entries; 31681da177e4SLinus Torvalds 3169fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 317049a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 3171fc66f95cSEric Dumazet entries <= rt_max_size) 31721da177e4SLinus Torvalds goto out; 31731da177e4SLinus Torvalds 31746891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 317514956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 3176fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 3177fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 31787019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 31791da177e4SLinus Torvalds out: 31807019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 3181fc66f95cSEric Dumazet return entries > rt_max_size; 31821da177e4SLinus Torvalds } 31831da177e4SLinus Torvalds 3184b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, 3185b2c709ccSDavid Ahern const struct in6_addr *gw_addr, u32 tbid, 3186b2c709ccSDavid Ahern int flags, struct fib6_result *res) 31878c14586fSDavid Ahern { 31888c14586fSDavid Ahern struct flowi6 fl6 = { 31898c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 31908c14586fSDavid Ahern .daddr = *gw_addr, 31918c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 31928c14586fSDavid Ahern }; 31938c14586fSDavid Ahern struct fib6_table *table; 3194b2c709ccSDavid Ahern int err; 31958c14586fSDavid Ahern 3196f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 31978c14586fSDavid Ahern if (!table) 3198b2c709ccSDavid Ahern return -EINVAL; 31998c14586fSDavid Ahern 32008c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 32018c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 32028c14586fSDavid Ahern 3203f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 32048c14586fSDavid Ahern 3205b2c709ccSDavid Ahern err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); 3206b2c709ccSDavid Ahern if (!err && res->f6i != net->ipv6.fib6_null_entry) 3207b2c709ccSDavid Ahern fib6_select_path(net, res, &fl6, cfg->fc_ifindex, 3208b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 32098c14586fSDavid Ahern 3210b2c709ccSDavid Ahern return err; 32118c14586fSDavid Ahern } 32128c14586fSDavid Ahern 3213fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 3214fc1e64e1SDavid Ahern struct fib6_config *cfg, 32159fbb704cSDavid Ahern const struct net_device *dev, 3216fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 3217fc1e64e1SDavid Ahern { 3218b2c709ccSDavid Ahern u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 3219fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 3220b2c709ccSDavid Ahern struct fib6_result res = {}; 3221fc1e64e1SDavid Ahern int err; 3222fc1e64e1SDavid Ahern 3223b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); 3224b2c709ccSDavid Ahern if (!err && !(res.fib6_flags & RTF_REJECT) && 32254ed591c8SDavid Ahern /* ignore match if it is the default route */ 3226b2c709ccSDavid Ahern !ipv6_addr_any(&res.f6i->fib6_dst.addr) && 3227b2c709ccSDavid Ahern (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { 322844750f84SDavid Ahern NL_SET_ERR_MSG(extack, 322944750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 3230fc1e64e1SDavid Ahern err = -EINVAL; 3231fc1e64e1SDavid Ahern } 3232fc1e64e1SDavid Ahern 3233fc1e64e1SDavid Ahern return err; 3234fc1e64e1SDavid Ahern } 3235fc1e64e1SDavid Ahern 32361edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 32371edce99fSDavid Ahern struct fib6_config *cfg, 32381edce99fSDavid Ahern struct net_device **_dev, 32391edce99fSDavid Ahern struct inet6_dev **idev) 32401edce99fSDavid Ahern { 32411edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 32421edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 3243b2c709ccSDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 3244b2c709ccSDavid Ahern struct fib6_result res = {}; 32451edce99fSDavid Ahern int err = -EHOSTUNREACH; 32461edce99fSDavid Ahern 32471edce99fSDavid Ahern if (cfg->fc_table) { 3248b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, 3249b2c709ccSDavid Ahern cfg->fc_table, flags, &res); 3250b2c709ccSDavid Ahern /* gw_addr can not require a gateway or resolve to a reject 3251b2c709ccSDavid Ahern * route. If a device is given, it must match the result. 3252b2c709ccSDavid Ahern */ 3253b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3254b2c709ccSDavid Ahern res.nh->fib_nh_gw_family || 3255b2c709ccSDavid Ahern (dev && dev != res.nh->fib_nh_dev)) 3256b2c709ccSDavid Ahern err = -EHOSTUNREACH; 32571edce99fSDavid Ahern } 32581edce99fSDavid Ahern 3259b2c709ccSDavid Ahern if (err < 0) { 3260b2c709ccSDavid Ahern struct flowi6 fl6 = { 3261b2c709ccSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 3262b2c709ccSDavid Ahern .daddr = *gw_addr, 3263b2c709ccSDavid Ahern }; 32641edce99fSDavid Ahern 3265b2c709ccSDavid Ahern err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); 3266b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3267b2c709ccSDavid Ahern res.nh->fib_nh_gw_family) 3268b2c709ccSDavid Ahern err = -EHOSTUNREACH; 32691edce99fSDavid Ahern 3270b2c709ccSDavid Ahern if (err) 3271b2c709ccSDavid Ahern return err; 3272b2c709ccSDavid Ahern 3273b2c709ccSDavid Ahern fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, 3274b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 32751edce99fSDavid Ahern } 32761edce99fSDavid Ahern 32771edce99fSDavid Ahern err = 0; 3278b2c709ccSDavid Ahern if (dev) { 3279b2c709ccSDavid Ahern if (dev != res.nh->fib_nh_dev) 3280b2c709ccSDavid Ahern err = -EHOSTUNREACH; 3281b2c709ccSDavid Ahern } else { 3282b2c709ccSDavid Ahern *_dev = dev = res.nh->fib_nh_dev; 3283b2c709ccSDavid Ahern dev_hold(dev); 3284b2c709ccSDavid Ahern *idev = in6_dev_get(dev); 3285b2c709ccSDavid Ahern } 32861edce99fSDavid Ahern 32871edce99fSDavid Ahern return err; 32881edce99fSDavid Ahern } 32891edce99fSDavid Ahern 32909fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 32919fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 32929fbb704cSDavid Ahern struct netlink_ext_ack *extack) 32939fbb704cSDavid Ahern { 32949fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 32959fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 3296232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 32979fbb704cSDavid Ahern const struct net_device *dev = *_dev; 3298232378e8SDavid Ahern bool need_addr_check = !dev; 32999fbb704cSDavid Ahern int err = -EINVAL; 33009fbb704cSDavid Ahern 33019fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 33029fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 33039fbb704cSDavid Ahern * will return already-added prefix route via interface that 33049fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 33059fbb704cSDavid Ahern */ 3306232378e8SDavid Ahern if (dev && 3307232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3308232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 33099fbb704cSDavid Ahern goto out; 33109fbb704cSDavid Ahern } 33119fbb704cSDavid Ahern 33129fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 33139fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 33149fbb704cSDavid Ahern * addresses as nexthop address. 33159fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 33169fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 33179fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 33189fbb704cSDavid Ahern * some exceptions. --ANK 33199fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 33209fbb704cSDavid Ahern * addressing 33219fbb704cSDavid Ahern */ 33229fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 33239fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 33249fbb704cSDavid Ahern goto out; 33259fbb704cSDavid Ahern } 33269fbb704cSDavid Ahern 3327b2c709ccSDavid Ahern rcu_read_lock(); 3328b2c709ccSDavid Ahern 33299fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 33309fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 33319fbb704cSDavid Ahern else 33329fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 33339fbb704cSDavid Ahern 3334b2c709ccSDavid Ahern rcu_read_unlock(); 3335b2c709ccSDavid Ahern 33369fbb704cSDavid Ahern if (err) 33379fbb704cSDavid Ahern goto out; 33389fbb704cSDavid Ahern } 33399fbb704cSDavid Ahern 33409fbb704cSDavid Ahern /* reload in case device was changed */ 33419fbb704cSDavid Ahern dev = *_dev; 33429fbb704cSDavid Ahern 33439fbb704cSDavid Ahern err = -EINVAL; 33449fbb704cSDavid Ahern if (!dev) { 33459fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 33469fbb704cSDavid Ahern goto out; 33479fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 33489fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 33499fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 33509fbb704cSDavid Ahern goto out; 33519fbb704cSDavid Ahern } 3352232378e8SDavid Ahern 3353232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 3354232378e8SDavid Ahern * egress device has been resolved. 3355232378e8SDavid Ahern */ 3356232378e8SDavid Ahern if (need_addr_check && 3357232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3358232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3359232378e8SDavid Ahern goto out; 3360232378e8SDavid Ahern } 3361232378e8SDavid Ahern 33629fbb704cSDavid Ahern err = 0; 33639fbb704cSDavid Ahern out: 33649fbb704cSDavid Ahern return err; 33659fbb704cSDavid Ahern } 33669fbb704cSDavid Ahern 336783c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 336883c44251SDavid Ahern { 336983c44251SDavid Ahern if ((flags & RTF_REJECT) || 337083c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 337183c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 337283c44251SDavid Ahern !(flags & RTF_LOCAL))) 337383c44251SDavid Ahern return true; 337483c44251SDavid Ahern 337583c44251SDavid Ahern return false; 337683c44251SDavid Ahern } 337783c44251SDavid Ahern 337883c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 337983c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 338083c44251SDavid Ahern struct netlink_ext_ack *extack) 338183c44251SDavid Ahern { 338283c44251SDavid Ahern struct net_device *dev = NULL; 338383c44251SDavid Ahern struct inet6_dev *idev = NULL; 338483c44251SDavid Ahern int addr_type; 338583c44251SDavid Ahern int err; 338683c44251SDavid Ahern 3387f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 3388f1741730SDavid Ahern 338983c44251SDavid Ahern err = -ENODEV; 339083c44251SDavid Ahern if (cfg->fc_ifindex) { 339183c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 339283c44251SDavid Ahern if (!dev) 339383c44251SDavid Ahern goto out; 339483c44251SDavid Ahern idev = in6_dev_get(dev); 339583c44251SDavid Ahern if (!idev) 339683c44251SDavid Ahern goto out; 339783c44251SDavid Ahern } 339883c44251SDavid Ahern 339983c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 340083c44251SDavid Ahern if (!dev) { 340183c44251SDavid Ahern NL_SET_ERR_MSG(extack, 340283c44251SDavid Ahern "Nexthop device required for onlink"); 340383c44251SDavid Ahern goto out; 340483c44251SDavid Ahern } 340583c44251SDavid Ahern 340683c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 340783c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 340883c44251SDavid Ahern err = -ENETDOWN; 340983c44251SDavid Ahern goto out; 341083c44251SDavid Ahern } 341183c44251SDavid Ahern 3412ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 341383c44251SDavid Ahern } 341483c44251SDavid Ahern 3415ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 341683c44251SDavid Ahern 341783c44251SDavid Ahern /* We cannot add true routes via loopback here, 341883c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 341983c44251SDavid Ahern */ 342083c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 342183c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 342283c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 342383c44251SDavid Ahern if (dev != net->loopback_dev) { 342483c44251SDavid Ahern if (dev) { 342583c44251SDavid Ahern dev_put(dev); 342683c44251SDavid Ahern in6_dev_put(idev); 342783c44251SDavid Ahern } 342883c44251SDavid Ahern dev = net->loopback_dev; 342983c44251SDavid Ahern dev_hold(dev); 343083c44251SDavid Ahern idev = in6_dev_get(dev); 343183c44251SDavid Ahern if (!idev) { 343283c44251SDavid Ahern err = -ENODEV; 343383c44251SDavid Ahern goto out; 343483c44251SDavid Ahern } 343583c44251SDavid Ahern } 34367dd73168SDavid Ahern goto pcpu_alloc; 343783c44251SDavid Ahern } 343883c44251SDavid Ahern 343983c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 344083c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 344183c44251SDavid Ahern if (err) 344283c44251SDavid Ahern goto out; 344383c44251SDavid Ahern 3444ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3445bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 344683c44251SDavid Ahern } 344783c44251SDavid Ahern 344883c44251SDavid Ahern err = -ENODEV; 344983c44251SDavid Ahern if (!dev) 345083c44251SDavid Ahern goto out; 345183c44251SDavid Ahern 345283c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 345383c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 345483c44251SDavid Ahern err = -EACCES; 345583c44251SDavid Ahern goto out; 345683c44251SDavid Ahern } 345783c44251SDavid Ahern 345883c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 345983c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 346083c44251SDavid Ahern err = -ENETDOWN; 346183c44251SDavid Ahern goto out; 346283c44251SDavid Ahern } 346383c44251SDavid Ahern 346483c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 346583c44251SDavid Ahern !netif_carrier_ok(dev)) 3466ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 346783c44251SDavid Ahern 34687dd73168SDavid Ahern err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, 34697dd73168SDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 34707dd73168SDavid Ahern if (err) 34717dd73168SDavid Ahern goto out; 34727dd73168SDavid Ahern 34737dd73168SDavid Ahern pcpu_alloc: 3474f40b6ae2SDavid Ahern fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3475f40b6ae2SDavid Ahern if (!fib6_nh->rt6i_pcpu) { 3476f40b6ae2SDavid Ahern err = -ENOMEM; 3477f40b6ae2SDavid Ahern goto out; 3478f40b6ae2SDavid Ahern } 3479f40b6ae2SDavid Ahern 3480ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3481f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 348283c44251SDavid Ahern err = 0; 348383c44251SDavid Ahern out: 348483c44251SDavid Ahern if (idev) 348583c44251SDavid Ahern in6_dev_put(idev); 348683c44251SDavid Ahern 348783c44251SDavid Ahern if (err) { 3488ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3489ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 349083c44251SDavid Ahern if (dev) 349183c44251SDavid Ahern dev_put(dev); 349283c44251SDavid Ahern } 349383c44251SDavid Ahern 349483c44251SDavid Ahern return err; 349583c44251SDavid Ahern } 349683c44251SDavid Ahern 3497dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3498dac7d0f2SDavid Ahern { 3499cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 3500cc5c073aSDavid Ahern 3501cc5c073aSDavid Ahern rcu_read_lock(); 3502cc5c073aSDavid Ahern 3503cc5c073aSDavid Ahern fib6_nh_flush_exceptions(fib6_nh, NULL); 3504cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); 3505cc5c073aSDavid Ahern if (bucket) { 3506cc5c073aSDavid Ahern rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); 3507cc5c073aSDavid Ahern kfree(bucket); 3508cc5c073aSDavid Ahern } 3509cc5c073aSDavid Ahern 3510cc5c073aSDavid Ahern rcu_read_unlock(); 3511cc5c073aSDavid Ahern 3512f40b6ae2SDavid Ahern if (fib6_nh->rt6i_pcpu) { 3513f40b6ae2SDavid Ahern int cpu; 3514f40b6ae2SDavid Ahern 3515f40b6ae2SDavid Ahern for_each_possible_cpu(cpu) { 3516f40b6ae2SDavid Ahern struct rt6_info **ppcpu_rt; 3517f40b6ae2SDavid Ahern struct rt6_info *pcpu_rt; 3518f40b6ae2SDavid Ahern 3519f40b6ae2SDavid Ahern ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 3520f40b6ae2SDavid Ahern pcpu_rt = *ppcpu_rt; 3521f40b6ae2SDavid Ahern if (pcpu_rt) { 3522f40b6ae2SDavid Ahern dst_dev_put(&pcpu_rt->dst); 3523f40b6ae2SDavid Ahern dst_release(&pcpu_rt->dst); 3524f40b6ae2SDavid Ahern *ppcpu_rt = NULL; 3525f40b6ae2SDavid Ahern } 3526f40b6ae2SDavid Ahern } 3527f40b6ae2SDavid Ahern 3528f40b6ae2SDavid Ahern free_percpu(fib6_nh->rt6i_pcpu); 3529f40b6ae2SDavid Ahern } 3530f40b6ae2SDavid Ahern 3531979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3532dac7d0f2SDavid Ahern } 3533dac7d0f2SDavid Ahern 35348d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3535acb54e3cSDavid Ahern gfp_t gfp_flags, 3536333c4301SDavid Ahern struct netlink_ext_ack *extack) 35371da177e4SLinus Torvalds { 35385578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 35398d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3540f88d8ea6SDavid Ahern struct nexthop *nh = NULL; 3541c71099acSThomas Graf struct fib6_table *table; 3542f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 35438c5b83f0SRoopa Prabhu int err = -EINVAL; 354483c44251SDavid Ahern int addr_type; 35451da177e4SLinus Torvalds 3546557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3547d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3548d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3549557c44beSDavid Ahern goto out; 3550d5d531cbSDavid Ahern } 3551557c44beSDavid Ahern 35522ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 35532ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 35542ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 35552ea2352eSWei Wang goto out; 35562ea2352eSWei Wang } 35572ea2352eSWei Wang 3558e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3559e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3560e8478e80SDavid Ahern goto out; 3561e8478e80SDavid Ahern } 3562e8478e80SDavid Ahern 3563d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3564d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 35658c5b83f0SRoopa Prabhu goto out; 3566d5d531cbSDavid Ahern } 3567d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3568d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3569d5d531cbSDavid Ahern goto out; 3570d5d531cbSDavid Ahern } 35711da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3572d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3573d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3574d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 35758c5b83f0SRoopa Prabhu goto out; 3576d5d531cbSDavid Ahern } 35771da177e4SLinus Torvalds #endif 35785b98324eSDavid Ahern if (cfg->fc_nh_id) { 35795b98324eSDavid Ahern nh = nexthop_find_by_id(net, cfg->fc_nh_id); 35805b98324eSDavid Ahern if (!nh) { 35815b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 35825b98324eSDavid Ahern goto out; 35835b98324eSDavid Ahern } 35845b98324eSDavid Ahern err = fib6_check_nexthop(nh, cfg, extack); 35855b98324eSDavid Ahern if (err) 35865b98324eSDavid Ahern goto out; 35875b98324eSDavid Ahern } 3588fc1e64e1SDavid Ahern 3589c71099acSThomas Graf err = -ENOBUFS; 359038308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3591d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3592d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 359338308473SDavid S. Miller if (!table) { 3594f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3595d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3596d71314b4SMatti Vaittinen } 3597d71314b4SMatti Vaittinen } else { 3598d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3599d71314b4SMatti Vaittinen } 360038308473SDavid S. Miller 360138308473SDavid S. Miller if (!table) 3602c71099acSThomas Graf goto out; 3603c71099acSThomas Graf 36041da177e4SLinus Torvalds err = -ENOMEM; 3605f88d8ea6SDavid Ahern rt = fib6_info_alloc(gfp_flags, !nh); 360693531c67SDavid Ahern if (!rt) 36071da177e4SLinus Torvalds goto out; 360893531c67SDavid Ahern 3609d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3610d7e774f3SDavid Ahern extack); 3611767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3612767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3613fda21d46SEric Dumazet /* Do not leave garbage there. */ 3614fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 3615767a2217SDavid Ahern goto out; 3616767a2217SDavid Ahern } 3617767a2217SDavid Ahern 361893531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 361993531c67SDavid Ahern rt->dst_nocount = true; 36201da177e4SLinus Torvalds 36211716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 362214895687SDavid Ahern fib6_set_expires(rt, jiffies + 36231716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 36241716a961SGao feng else 362514895687SDavid Ahern fib6_clean_expires(rt); 36261da177e4SLinus Torvalds 362786872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 362886872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 362993c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 363086872cb5SThomas Graf 363183c44251SDavid Ahern rt->fib6_table = table; 363283c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 3633c7036d97SDavid Ahern rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; 36342b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 363519e42e45SRoopa Prabhu 363693c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 363793c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 363893c2fb25SDavid Ahern if (rt->fib6_dst.plen == 128) 36393b6761d1SDavid Ahern rt->dst_host = true; 36401da177e4SLinus Torvalds 36411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 364293c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 364393c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 36441da177e4SLinus Torvalds #endif 3645f88d8ea6SDavid Ahern if (nh) { 3646f88d8ea6SDavid Ahern if (!nexthop_get(nh)) { 3647f88d8ea6SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 3648f88d8ea6SDavid Ahern goto out; 3649f88d8ea6SDavid Ahern } 3650f88d8ea6SDavid Ahern if (rt->fib6_src.plen) { 36514daa95afSColin Ian King NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); 3652f88d8ea6SDavid Ahern goto out; 3653f88d8ea6SDavid Ahern } 3654f88d8ea6SDavid Ahern rt->nh = nh; 3655f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(rt->nh); 3656f88d8ea6SDavid Ahern } else { 36571cf844c7SDavid Ahern err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); 36581da177e4SLinus Torvalds if (err) 36591da177e4SLinus Torvalds goto out; 36609fbb704cSDavid Ahern 3661f88d8ea6SDavid Ahern fib6_nh = rt->fib6_nh; 3662f88d8ea6SDavid Ahern 3663f88d8ea6SDavid Ahern /* We cannot add true routes via loopback here, they would 3664f88d8ea6SDavid Ahern * result in kernel looping; promote them to reject routes 366583c44251SDavid Ahern */ 366683c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3667f88d8ea6SDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, 3668f88d8ea6SDavid Ahern addr_type)) 366983c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3670f88d8ea6SDavid Ahern } 3671955ec4cbSDavid Ahern 3672c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3673f88d8ea6SDavid Ahern struct net_device *dev = fib6_nh->fib_nh_dev; 367483c44251SDavid Ahern 3675c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3676d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3677c3968a85SDaniel Walter err = -EINVAL; 3678c3968a85SDaniel Walter goto out; 3679c3968a85SDaniel Walter } 368093c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 368193c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3682c3968a85SDaniel Walter } else 368393c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3684c3968a85SDaniel Walter 36858c5b83f0SRoopa Prabhu return rt; 36861da177e4SLinus Torvalds out: 368793531c67SDavid Ahern fib6_info_release(rt); 36888c5b83f0SRoopa Prabhu return ERR_PTR(err); 36896b9ea5a6SRoopa Prabhu } 36906b9ea5a6SRoopa Prabhu 3691acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3692333c4301SDavid Ahern struct netlink_ext_ack *extack) 36936b9ea5a6SRoopa Prabhu { 36948d1c802bSDavid Ahern struct fib6_info *rt; 36956b9ea5a6SRoopa Prabhu int err; 36966b9ea5a6SRoopa Prabhu 3697acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3698d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3699d4ead6b3SDavid Ahern return PTR_ERR(rt); 37006b9ea5a6SRoopa Prabhu 3701d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 370293531c67SDavid Ahern fib6_info_release(rt); 37036b9ea5a6SRoopa Prabhu 37041da177e4SLinus Torvalds return err; 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 37078d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 37081da177e4SLinus Torvalds { 3709afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3710c71099acSThomas Graf struct fib6_table *table; 3711afb1d4b5SDavid Ahern int err; 37121da177e4SLinus Torvalds 3713421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 37146825a26cSGao feng err = -ENOENT; 37156825a26cSGao feng goto out; 37166825a26cSGao feng } 37176c813a72SPatrick McHardy 371893c2fb25SDavid Ahern table = rt->fib6_table; 371966f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 372086872cb5SThomas Graf err = fib6_del(rt, info); 372166f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 37221da177e4SLinus Torvalds 37236825a26cSGao feng out: 372493531c67SDavid Ahern fib6_info_release(rt); 37251da177e4SLinus Torvalds return err; 37261da177e4SLinus Torvalds } 37271da177e4SLinus Torvalds 37288d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt) 3729e0a1ad73SThomas Graf { 3730afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net }; 3731afb1d4b5SDavid Ahern 3732528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3733e0a1ad73SThomas Graf } 3734e0a1ad73SThomas Graf 37358d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 37360ae81335SDavid Ahern { 37370ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3738e3330039SWANG Cong struct net *net = info->nl_net; 373916a16cd3SDavid Ahern struct sk_buff *skb = NULL; 37400ae81335SDavid Ahern struct fib6_table *table; 3741e3330039SWANG Cong int err = -ENOENT; 37420ae81335SDavid Ahern 3743421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3744e3330039SWANG Cong goto out_put; 374593c2fb25SDavid Ahern table = rt->fib6_table; 374666f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 37470ae81335SDavid Ahern 374893c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 37498d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 37500ae81335SDavid Ahern 375116a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 375216a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 375316a16cd3SDavid Ahern if (skb) { 375416a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 375516a16cd3SDavid Ahern 3756d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 375716a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 375816a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 375916a16cd3SDavid Ahern kfree_skb(skb); 376016a16cd3SDavid Ahern skb = NULL; 376116a16cd3SDavid Ahern } else 376216a16cd3SDavid Ahern info->skip_notify = 1; 376316a16cd3SDavid Ahern } 376416a16cd3SDavid Ahern 37652881fd61SIdo Schimmel info->skip_notify_kernel = 1; 37662881fd61SIdo Schimmel call_fib6_multipath_entry_notifiers(net, 37672881fd61SIdo Schimmel FIB_EVENT_ENTRY_DEL, 37682881fd61SIdo Schimmel rt, 37692881fd61SIdo Schimmel rt->fib6_nsiblings, 37702881fd61SIdo Schimmel NULL); 37710ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 377293c2fb25SDavid Ahern &rt->fib6_siblings, 377393c2fb25SDavid Ahern fib6_siblings) { 37740ae81335SDavid Ahern err = fib6_del(sibling, info); 37750ae81335SDavid Ahern if (err) 3776e3330039SWANG Cong goto out_unlock; 37770ae81335SDavid Ahern } 37780ae81335SDavid Ahern } 37790ae81335SDavid Ahern 37800ae81335SDavid Ahern err = fib6_del(rt, info); 3781e3330039SWANG Cong out_unlock: 378266f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3783e3330039SWANG Cong out_put: 378493531c67SDavid Ahern fib6_info_release(rt); 378516a16cd3SDavid Ahern 378616a16cd3SDavid Ahern if (skb) { 3787e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 378816a16cd3SDavid Ahern info->nlh, gfp_any()); 378916a16cd3SDavid Ahern } 37900ae81335SDavid Ahern return err; 37910ae81335SDavid Ahern } 37920ae81335SDavid Ahern 37930fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 379423fb93a4SDavid Ahern { 379523fb93a4SDavid Ahern int rc = -ESRCH; 379623fb93a4SDavid Ahern 379723fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 379823fb93a4SDavid Ahern goto out; 379923fb93a4SDavid Ahern 380023fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 380123fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 380223fb93a4SDavid Ahern goto out; 3803761f6026SXin Long 380423fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 380523fb93a4SDavid Ahern out: 380623fb93a4SDavid Ahern return rc; 380723fb93a4SDavid Ahern } 380823fb93a4SDavid Ahern 38090fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, 38100fa6efc5SDavid Ahern struct fib6_nh *nh) 38110fa6efc5SDavid Ahern { 38120fa6efc5SDavid Ahern struct fib6_result res = { 38130fa6efc5SDavid Ahern .f6i = rt, 38140fa6efc5SDavid Ahern .nh = nh, 38150fa6efc5SDavid Ahern }; 38160fa6efc5SDavid Ahern struct rt6_info *rt_cache; 38170fa6efc5SDavid Ahern 38180fa6efc5SDavid Ahern rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); 38190fa6efc5SDavid Ahern if (rt_cache) 38200fa6efc5SDavid Ahern return __ip6_del_cached_rt(rt_cache, cfg); 38210fa6efc5SDavid Ahern 38220fa6efc5SDavid Ahern return 0; 38230fa6efc5SDavid Ahern } 38240fa6efc5SDavid Ahern 38255b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg { 38265b98324eSDavid Ahern struct fib6_config *cfg; 38275b98324eSDavid Ahern struct fib6_info *f6i; 38285b98324eSDavid Ahern }; 38295b98324eSDavid Ahern 38305b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) 38315b98324eSDavid Ahern { 38325b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg *arg = _arg; 38335b98324eSDavid Ahern int rc; 38345b98324eSDavid Ahern 38355b98324eSDavid Ahern rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); 38365b98324eSDavid Ahern return rc != -ESRCH ? rc : 0; 38375b98324eSDavid Ahern } 38385b98324eSDavid Ahern 38395b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) 38405b98324eSDavid Ahern { 38415b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg arg = { 38425b98324eSDavid Ahern .cfg = cfg, 38435b98324eSDavid Ahern .f6i = f6i 38445b98324eSDavid Ahern }; 38455b98324eSDavid Ahern 38465b98324eSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); 38475b98324eSDavid Ahern } 38485b98324eSDavid Ahern 3849333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3850333c4301SDavid Ahern struct netlink_ext_ack *extack) 38511da177e4SLinus Torvalds { 3852c71099acSThomas Graf struct fib6_table *table; 38538d1c802bSDavid Ahern struct fib6_info *rt; 38541da177e4SLinus Torvalds struct fib6_node *fn; 38551da177e4SLinus Torvalds int err = -ESRCH; 38561da177e4SLinus Torvalds 38575578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3858d5d531cbSDavid Ahern if (!table) { 3859d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3860c71099acSThomas Graf return err; 3861d5d531cbSDavid Ahern } 38621da177e4SLinus Torvalds 386366f5d6ceSWei Wang rcu_read_lock(); 3864c71099acSThomas Graf 3865c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 386686872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 386738fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 38682b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 38691da177e4SLinus Torvalds 38701da177e4SLinus Torvalds if (fn) { 387166f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3872ad1601aeSDavid Ahern struct fib6_nh *nh; 3873ad1601aeSDavid Ahern 38743401bfb1SStefano Brivio if (rt->nh && cfg->fc_nh_id && 38753401bfb1SStefano Brivio rt->nh->id != cfg->fc_nh_id) 38765b98324eSDavid Ahern continue; 387723fb93a4SDavid Ahern 38785b98324eSDavid Ahern if (cfg->fc_flags & RTF_CACHE) { 38795b98324eSDavid Ahern int rc = 0; 38805b98324eSDavid Ahern 38815b98324eSDavid Ahern if (rt->nh) { 38825b98324eSDavid Ahern rc = ip6_del_cached_rt_nh(cfg, rt); 38835b98324eSDavid Ahern } else if (cfg->fc_nh_id) { 38845b98324eSDavid Ahern continue; 38855b98324eSDavid Ahern } else { 38865b98324eSDavid Ahern nh = rt->fib6_nh; 38870fa6efc5SDavid Ahern rc = ip6_del_cached_rt(cfg, rt, nh); 38885b98324eSDavid Ahern } 38899e575010SEric Dumazet if (rc != -ESRCH) { 38909e575010SEric Dumazet rcu_read_unlock(); 389123fb93a4SDavid Ahern return rc; 389223fb93a4SDavid Ahern } 38931f56a01fSMartin KaFai Lau continue; 38942b760fcfSWei Wang } 3895ad1601aeSDavid Ahern 38965b98324eSDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 38975b98324eSDavid Ahern continue; 38985b98324eSDavid Ahern if (cfg->fc_protocol && 38995b98324eSDavid Ahern cfg->fc_protocol != rt->fib6_protocol) 39005b98324eSDavid Ahern continue; 39015b98324eSDavid Ahern 39025b98324eSDavid Ahern if (rt->nh) { 39035b98324eSDavid Ahern if (!fib6_info_hold_safe(rt)) 39045b98324eSDavid Ahern continue; 39055b98324eSDavid Ahern rcu_read_unlock(); 39065b98324eSDavid Ahern 39075b98324eSDavid Ahern return __ip6_del_rt(rt, &cfg->fc_nlinfo); 39085b98324eSDavid Ahern } 39095b98324eSDavid Ahern if (cfg->fc_nh_id) 39105b98324eSDavid Ahern continue; 39115b98324eSDavid Ahern 39125b98324eSDavid Ahern nh = rt->fib6_nh; 391386872cb5SThomas Graf if (cfg->fc_ifindex && 3914ad1601aeSDavid Ahern (!nh->fib_nh_dev || 3915ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 39161da177e4SLinus Torvalds continue; 391786872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 3918ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 39191da177e4SLinus Torvalds continue; 3920e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3921e873e4b9SWei Wang continue; 392266f5d6ceSWei Wang rcu_read_unlock(); 39231da177e4SLinus Torvalds 39240ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 39250ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 392686872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 39270ae81335SDavid Ahern 39280ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 39291da177e4SLinus Torvalds } 39301da177e4SLinus Torvalds } 393166f5d6ceSWei Wang rcu_read_unlock(); 39321da177e4SLinus Torvalds 39331da177e4SLinus Torvalds return err; 39341da177e4SLinus Torvalds } 39351da177e4SLinus Torvalds 39366700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3937a6279458SYOSHIFUJI Hideaki { 3938a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3939e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 394085bd05deSDavid Ahern struct fib6_result res = {}; 3941e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3942e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3943e8599ff4SDavid S. Miller struct neighbour *neigh; 394471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 39456e157b6aSDavid S. Miller int optlen, on_link; 39466e157b6aSDavid S. Miller u8 *lladdr; 3947e8599ff4SDavid S. Miller 394829a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 394971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3950e8599ff4SDavid S. Miller 3951e8599ff4SDavid S. Miller if (optlen < 0) { 39526e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3953e8599ff4SDavid S. Miller return; 3954e8599ff4SDavid S. Miller } 3955e8599ff4SDavid S. Miller 395671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3957e8599ff4SDavid S. Miller 395871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 39596e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3960e8599ff4SDavid S. Miller return; 3961e8599ff4SDavid S. Miller } 3962e8599ff4SDavid S. Miller 39636e157b6aSDavid S. Miller on_link = 0; 396471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3965e8599ff4SDavid S. Miller on_link = 1; 396671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3967e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 39686e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3969e8599ff4SDavid S. Miller return; 3970e8599ff4SDavid S. Miller } 3971e8599ff4SDavid S. Miller 3972e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3973e8599ff4SDavid S. Miller if (!in6_dev) 3974e8599ff4SDavid S. Miller return; 3975e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3976e8599ff4SDavid S. Miller return; 3977e8599ff4SDavid S. Miller 3978e8599ff4SDavid S. Miller /* RFC2461 8.1: 3979e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3980e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3981e8599ff4SDavid S. Miller */ 3982e8599ff4SDavid S. Miller 3983f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3984e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3985e8599ff4SDavid S. Miller return; 3986e8599ff4SDavid S. Miller } 39876e157b6aSDavid S. Miller 39886e157b6aSDavid S. Miller lladdr = NULL; 3989e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3990e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3991e8599ff4SDavid S. Miller skb->dev); 3992e8599ff4SDavid S. Miller if (!lladdr) { 3993e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3994e8599ff4SDavid S. Miller return; 3995e8599ff4SDavid S. Miller } 3996e8599ff4SDavid S. Miller } 3997e8599ff4SDavid S. Miller 39986e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3999ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 40006e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 40016e157b6aSDavid S. Miller return; 40026e157b6aSDavid S. Miller } 40036e157b6aSDavid S. Miller 40046e157b6aSDavid S. Miller /* Redirect received -> path was valid. 40056e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 40066e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 40076e157b6aSDavid S. Miller */ 40080dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 40096e157b6aSDavid S. Miller 401071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 4011e8599ff4SDavid S. Miller if (!neigh) 4012e8599ff4SDavid S. Miller return; 4013e8599ff4SDavid S. Miller 40141da177e4SLinus Torvalds /* 40151da177e4SLinus Torvalds * We have finally decided to accept it. 40161da177e4SLinus Torvalds */ 40171da177e4SLinus Torvalds 4018f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 40191da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 40201da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 40211da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 4022f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 4023f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 40241da177e4SLinus Torvalds 40254d85cd0cSDavid Ahern rcu_read_lock(); 402685bd05deSDavid Ahern res.f6i = rcu_dereference(rt->from); 4027ff24e498SDavid S. Miller if (!res.f6i) 4028886b7a50SMartin KaFai Lau goto out; 40298a14e46fSDavid Ahern 403049d5b8efSDavid Ahern if (res.f6i->nh) { 403149d5b8efSDavid Ahern struct fib6_nh_match_arg arg = { 403249d5b8efSDavid Ahern .dev = dst->dev, 403349d5b8efSDavid Ahern .gw = &rt->rt6i_gateway, 403449d5b8efSDavid Ahern }; 403549d5b8efSDavid Ahern 403649d5b8efSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 403749d5b8efSDavid Ahern fib6_nh_find_match, &arg); 403849d5b8efSDavid Ahern 403949d5b8efSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 404049d5b8efSDavid Ahern * using the dst->dev. Should be impossible 404149d5b8efSDavid Ahern */ 404249d5b8efSDavid Ahern if (!arg.match) 404349d5b8efSDavid Ahern goto out; 404449d5b8efSDavid Ahern res.nh = arg.match; 404549d5b8efSDavid Ahern } else { 40461cf844c7SDavid Ahern res.nh = res.f6i->fib6_nh; 404749d5b8efSDavid Ahern } 404849d5b8efSDavid Ahern 40497d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 40507d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 405185bd05deSDavid Ahern nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 405238308473SDavid S. Miller if (!nrt) 40531da177e4SLinus Torvalds goto out; 40541da177e4SLinus Torvalds 40551da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 40561da177e4SLinus Torvalds if (on_link) 40571da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 40581da177e4SLinus Torvalds 40594e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 40601da177e4SLinus Torvalds 4061886b7a50SMartin KaFai Lau /* rt6_insert_exception() will take care of duplicated exceptions */ 40625012f0a5SDavid Ahern if (rt6_insert_exception(nrt, &res)) { 40632b760fcfSWei Wang dst_release_immediate(&nrt->dst); 40642b760fcfSWei Wang goto out; 40652b760fcfSWei Wang } 40661da177e4SLinus Torvalds 4067d8d1f30bSChangli Gao netevent.old = &rt->dst; 4068d8d1f30bSChangli Gao netevent.new = &nrt->dst; 406971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 407060592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 40718d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 40728d71740cSTom Tucker 40731da177e4SLinus Torvalds out: 4074886b7a50SMartin KaFai Lau rcu_read_unlock(); 4075e8599ff4SDavid S. Miller neigh_release(neigh); 40766e157b6aSDavid S. Miller } 40776e157b6aSDavid S. Miller 407870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 40798d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 4080b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4081830218c1SDavid Ahern const struct in6_addr *gwaddr, 4082830218c1SDavid Ahern struct net_device *dev) 408370ceb4f5SYOSHIFUJI Hideaki { 4084830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 4085830218c1SDavid Ahern int ifindex = dev->ifindex; 408670ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 40878d1c802bSDavid Ahern struct fib6_info *rt = NULL; 4088c71099acSThomas Graf struct fib6_table *table; 408970ceb4f5SYOSHIFUJI Hideaki 4090830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 409138308473SDavid S. Miller if (!table) 4092c71099acSThomas Graf return NULL; 4093c71099acSThomas Graf 409466f5d6ceSWei Wang rcu_read_lock(); 409538fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 409670ceb4f5SYOSHIFUJI Hideaki if (!fn) 409770ceb4f5SYOSHIFUJI Hideaki goto out; 409870ceb4f5SYOSHIFUJI Hideaki 409966f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 4100f88d8ea6SDavid Ahern /* these routes do not use nexthops */ 4101f88d8ea6SDavid Ahern if (rt->nh) 4102f88d8ea6SDavid Ahern continue; 41031cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) 410470ceb4f5SYOSHIFUJI Hideaki continue; 41052b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 41061cf844c7SDavid Ahern !rt->fib6_nh->fib_nh_gw_family) 410770ceb4f5SYOSHIFUJI Hideaki continue; 41081cf844c7SDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) 410970ceb4f5SYOSHIFUJI Hideaki continue; 4110e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 4111e873e4b9SWei Wang continue; 411270ceb4f5SYOSHIFUJI Hideaki break; 411370ceb4f5SYOSHIFUJI Hideaki } 411470ceb4f5SYOSHIFUJI Hideaki out: 411566f5d6ceSWei Wang rcu_read_unlock(); 411670ceb4f5SYOSHIFUJI Hideaki return rt; 411770ceb4f5SYOSHIFUJI Hideaki } 411870ceb4f5SYOSHIFUJI Hideaki 41198d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 4120b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4121830218c1SDavid Ahern const struct in6_addr *gwaddr, 4122830218c1SDavid Ahern struct net_device *dev, 412395c96174SEric Dumazet unsigned int pref) 412470ceb4f5SYOSHIFUJI Hideaki { 412586872cb5SThomas Graf struct fib6_config cfg = { 4126238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 4127830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 412886872cb5SThomas Graf .fc_dst_len = prefixlen, 412986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 413086872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 4131b91d5329SXin Long .fc_protocol = RTPROT_RA, 4132e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 413315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 4134efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 4135efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 413686872cb5SThomas Graf }; 413770ceb4f5SYOSHIFUJI Hideaki 4138830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 41394e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 41404e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 414186872cb5SThomas Graf 4142e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 4143e317da96SYOSHIFUJI Hideaki if (!prefixlen) 414486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 414570ceb4f5SYOSHIFUJI Hideaki 4146acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 414770ceb4f5SYOSHIFUJI Hideaki 4148830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 414970ceb4f5SYOSHIFUJI Hideaki } 415070ceb4f5SYOSHIFUJI Hideaki #endif 415170ceb4f5SYOSHIFUJI Hideaki 41528d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 4153afb1d4b5SDavid Ahern const struct in6_addr *addr, 4154afb1d4b5SDavid Ahern struct net_device *dev) 41551da177e4SLinus Torvalds { 4156830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 41578d1c802bSDavid Ahern struct fib6_info *rt; 4158c71099acSThomas Graf struct fib6_table *table; 41591da177e4SLinus Torvalds 4160afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 416138308473SDavid S. Miller if (!table) 4162c71099acSThomas Graf return NULL; 41631da177e4SLinus Torvalds 416466f5d6ceSWei Wang rcu_read_lock(); 416566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4166f88d8ea6SDavid Ahern struct fib6_nh *nh; 4167ad1601aeSDavid Ahern 4168f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4169f88d8ea6SDavid Ahern if (rt->nh) 4170f88d8ea6SDavid Ahern continue; 4171f88d8ea6SDavid Ahern 4172f88d8ea6SDavid Ahern nh = rt->fib6_nh; 4173ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 417493c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 4175ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 41761da177e4SLinus Torvalds break; 41771da177e4SLinus Torvalds } 4178e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 4179e873e4b9SWei Wang rt = NULL; 418066f5d6ceSWei Wang rcu_read_unlock(); 41811da177e4SLinus Torvalds return rt; 41821da177e4SLinus Torvalds } 41831da177e4SLinus Torvalds 41848d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 4185afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 4186ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 4187ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 41881da177e4SLinus Torvalds { 418986872cb5SThomas Graf struct fib6_config cfg = { 4190ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 4191238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 419286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 419386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 419486872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 4195b91d5329SXin Long .fc_protocol = RTPROT_RA, 4196e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 419715e47304SEric W. Biederman .fc_nlinfo.portid = 0, 41985578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 4199afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 420086872cb5SThomas Graf }; 42011da177e4SLinus Torvalds 42024e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 42031da177e4SLinus Torvalds 4204acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 4205830218c1SDavid Ahern struct fib6_table *table; 4206830218c1SDavid Ahern 4207830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 4208830218c1SDavid Ahern if (table) 4209830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 4210830218c1SDavid Ahern } 42111da177e4SLinus Torvalds 4212afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 42131da177e4SLinus Torvalds } 42141da177e4SLinus Torvalds 4215afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 4216afb1d4b5SDavid Ahern struct fib6_table *table) 42171da177e4SLinus Torvalds { 42188d1c802bSDavid Ahern struct fib6_info *rt; 42191da177e4SLinus Torvalds 42201da177e4SLinus Torvalds restart: 422166f5d6ceSWei Wang rcu_read_lock(); 422266f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4223dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 4224dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 4225dcd1f572SDavid Ahern 422693c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 4227e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 4228e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 422966f5d6ceSWei Wang rcu_read_unlock(); 4230afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 42311da177e4SLinus Torvalds goto restart; 42321da177e4SLinus Torvalds } 42331da177e4SLinus Torvalds } 423466f5d6ceSWei Wang rcu_read_unlock(); 4235830218c1SDavid Ahern 4236830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 4237830218c1SDavid Ahern } 4238830218c1SDavid Ahern 4239830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 4240830218c1SDavid Ahern { 4241830218c1SDavid Ahern struct fib6_table *table; 4242830218c1SDavid Ahern struct hlist_head *head; 4243830218c1SDavid Ahern unsigned int h; 4244830218c1SDavid Ahern 4245830218c1SDavid Ahern rcu_read_lock(); 4246830218c1SDavid Ahern 4247830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 4248830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 4249830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 4250830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 4251afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 4252830218c1SDavid Ahern } 4253830218c1SDavid Ahern } 4254830218c1SDavid Ahern 4255830218c1SDavid Ahern rcu_read_unlock(); 42561da177e4SLinus Torvalds } 42571da177e4SLinus Torvalds 42585578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 42595578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 426086872cb5SThomas Graf struct fib6_config *cfg) 426186872cb5SThomas Graf { 42628823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 42638823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 42648823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 42658823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 426667f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 42678823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 42688823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 42698823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 42708823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 42718823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 427286872cb5SThomas Graf 42738823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 427486872cb5SThomas Graf 42758823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 42768823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 42778823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 42788823a3acSMaciej Żenczykowski }; 427986872cb5SThomas Graf } 428086872cb5SThomas Graf 42815578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 42821da177e4SLinus Torvalds { 428386872cb5SThomas Graf struct fib6_config cfg; 42841da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 42851da177e4SLinus Torvalds int err; 42861da177e4SLinus Torvalds 42871da177e4SLinus Torvalds switch (cmd) { 42881da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 42891da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 4290af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 42911da177e4SLinus Torvalds return -EPERM; 42921da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 42931da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 42941da177e4SLinus Torvalds if (err) 42951da177e4SLinus Torvalds return -EFAULT; 42961da177e4SLinus Torvalds 42975578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 429886872cb5SThomas Graf 42991da177e4SLinus Torvalds rtnl_lock(); 43001da177e4SLinus Torvalds switch (cmd) { 43011da177e4SLinus Torvalds case SIOCADDRT: 4302acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 43031da177e4SLinus Torvalds break; 43041da177e4SLinus Torvalds case SIOCDELRT: 4305333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 43061da177e4SLinus Torvalds break; 43071da177e4SLinus Torvalds default: 43081da177e4SLinus Torvalds err = -EINVAL; 43091da177e4SLinus Torvalds } 43101da177e4SLinus Torvalds rtnl_unlock(); 43111da177e4SLinus Torvalds 43121da177e4SLinus Torvalds return err; 43133ff50b79SStephen Hemminger } 43141da177e4SLinus Torvalds 43151da177e4SLinus Torvalds return -EINVAL; 43161da177e4SLinus Torvalds } 43171da177e4SLinus Torvalds 43181da177e4SLinus Torvalds /* 43191da177e4SLinus Torvalds * Drop the packet on the floor 43201da177e4SLinus Torvalds */ 43211da177e4SLinus Torvalds 4322d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 43231da177e4SLinus Torvalds { 4324adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 43251d3fd8a1SStephen Suryaputra struct net *net = dev_net(dst->dev); 43261d3fd8a1SStephen Suryaputra struct inet6_dev *idev; 43271d3fd8a1SStephen Suryaputra int type; 43281d3fd8a1SStephen Suryaputra 43291d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev) && 43301d3fd8a1SStephen Suryaputra dst->dev == net->loopback_dev) 43311d3fd8a1SStephen Suryaputra idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 43321d3fd8a1SStephen Suryaputra else 43331d3fd8a1SStephen Suryaputra idev = ip6_dst_idev(dst); 43341d3fd8a1SStephen Suryaputra 4335612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 4336612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 43370660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 433845bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 43391d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 4340612f09e8SYOSHIFUJI Hideaki break; 4341612f09e8SYOSHIFUJI Hideaki } 4342612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 4343612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 43441d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 4345612f09e8SYOSHIFUJI Hideaki break; 4346612f09e8SYOSHIFUJI Hideaki } 43471d3fd8a1SStephen Suryaputra 43481d3fd8a1SStephen Suryaputra /* Start over by dropping the dst for l3mdev case */ 43491d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev)) 43501d3fd8a1SStephen Suryaputra skb_dst_drop(skb); 43511d3fd8a1SStephen Suryaputra 43523ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 43531da177e4SLinus Torvalds kfree_skb(skb); 43541da177e4SLinus Torvalds return 0; 43551da177e4SLinus Torvalds } 43561da177e4SLinus Torvalds 43579ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 43589ce8ade0SThomas Graf { 4359612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 43609ce8ade0SThomas Graf } 43619ce8ade0SThomas Graf 4362ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 43631da177e4SLinus Torvalds { 4364adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4365612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 43661da177e4SLinus Torvalds } 43671da177e4SLinus Torvalds 43689ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 43699ce8ade0SThomas Graf { 4370612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 43719ce8ade0SThomas Graf } 43729ce8ade0SThomas Graf 4373ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 43749ce8ade0SThomas Graf { 4375adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4376612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 43779ce8ade0SThomas Graf } 43789ce8ade0SThomas Graf 43791da177e4SLinus Torvalds /* 43801da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 43811da177e4SLinus Torvalds */ 43821da177e4SLinus Torvalds 4383360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 4384afb1d4b5SDavid Ahern struct inet6_dev *idev, 43851da177e4SLinus Torvalds const struct in6_addr *addr, 4386acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 43871da177e4SLinus Torvalds { 4388c7a1ce39SDavid Ahern struct fib6_config cfg = { 4389c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 4390c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 4391c7a1ce39SDavid Ahern .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, 4392c7a1ce39SDavid Ahern .fc_dst = *addr, 4393c7a1ce39SDavid Ahern .fc_dst_len = 128, 4394c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 4395c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 4396c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 4397c7a1ce39SDavid Ahern }; 43985f02ce24SDavid Ahern 4399e8478e80SDavid Ahern if (anycast) { 4400c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 4401c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 4402e8478e80SDavid Ahern } else { 4403c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 4404c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 4405e8478e80SDavid Ahern } 44061da177e4SLinus Torvalds 4407c7a1ce39SDavid Ahern return ip6_route_info_create(&cfg, gfp_flags, NULL); 44081da177e4SLinus Torvalds } 44091da177e4SLinus Torvalds 4410c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 4411c3968a85SDaniel Walter struct arg_dev_net_ip { 4412c3968a85SDaniel Walter struct net_device *dev; 4413c3968a85SDaniel Walter struct net *net; 4414c3968a85SDaniel Walter struct in6_addr *addr; 4415c3968a85SDaniel Walter }; 4416c3968a85SDaniel Walter 44178d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 4418c3968a85SDaniel Walter { 4419c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 4420c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 4421c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 4422c3968a85SDaniel Walter 4423f88d8ea6SDavid Ahern if (!rt->nh && 4424f88d8ea6SDavid Ahern ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && 4425421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 442693c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 442760006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 4428c3968a85SDaniel Walter /* remove prefsrc entry */ 442993c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 443060006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 4431c3968a85SDaniel Walter } 4432c3968a85SDaniel Walter return 0; 4433c3968a85SDaniel Walter } 4434c3968a85SDaniel Walter 4435c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 4436c3968a85SDaniel Walter { 4437c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 4438c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 4439c3968a85SDaniel Walter .dev = ifp->idev->dev, 4440c3968a85SDaniel Walter .net = net, 4441c3968a85SDaniel Walter .addr = &ifp->addr, 4442c3968a85SDaniel Walter }; 44430c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 4444c3968a85SDaniel Walter } 4445c3968a85SDaniel Walter 44462b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 4447be7a010dSDuan Jiong 4448be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 44498d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 4450be7a010dSDuan Jiong { 4451be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 4452f88d8ea6SDavid Ahern struct fib6_nh *nh; 4453be7a010dSDuan Jiong 4454f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4455f88d8ea6SDavid Ahern if (rt->nh) 4456f88d8ea6SDavid Ahern return 0; 4457f88d8ea6SDavid Ahern 4458f88d8ea6SDavid Ahern nh = rt->fib6_nh; 445993c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 4460cc5c073aSDavid Ahern nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) 4461be7a010dSDuan Jiong return -1; 4462b16cb459SWei Wang 4463b16cb459SWei Wang /* Further clean up cached routes in exception table. 4464b16cb459SWei Wang * This is needed because cached route may have a different 4465b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 4466b16cb459SWei Wang */ 4467cc5c073aSDavid Ahern fib6_nh_exceptions_clean_tohost(nh, gateway); 4468b16cb459SWei Wang 4469be7a010dSDuan Jiong return 0; 4470be7a010dSDuan Jiong } 4471be7a010dSDuan Jiong 4472be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 4473be7a010dSDuan Jiong { 4474be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 4475be7a010dSDuan Jiong } 4476be7a010dSDuan Jiong 44772127d95aSIdo Schimmel struct arg_netdev_event { 44782127d95aSIdo Schimmel const struct net_device *dev; 44794c981e28SIdo Schimmel union { 4480ecc5663cSDavid Ahern unsigned char nh_flags; 44814c981e28SIdo Schimmel unsigned long event; 44824c981e28SIdo Schimmel }; 44832127d95aSIdo Schimmel }; 44842127d95aSIdo Schimmel 44858d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 4486d7dedee1SIdo Schimmel { 44878d1c802bSDavid Ahern struct fib6_info *iter; 4488d7dedee1SIdo Schimmel struct fib6_node *fn; 4489d7dedee1SIdo Schimmel 449093c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 449193c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4492d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 449393c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4494d7dedee1SIdo Schimmel while (iter) { 449593c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 449633bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 4497d7dedee1SIdo Schimmel return iter; 44988fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 449993c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4500d7dedee1SIdo Schimmel } 4501d7dedee1SIdo Schimmel 4502d7dedee1SIdo Schimmel return NULL; 4503d7dedee1SIdo Schimmel } 4504d7dedee1SIdo Schimmel 4505f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */ 45068d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 4507d7dedee1SIdo Schimmel { 45081cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || 45091cf844c7SDavid Ahern (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && 45101cf844c7SDavid Ahern ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) 4511d7dedee1SIdo Schimmel return true; 4512d7dedee1SIdo Schimmel 4513d7dedee1SIdo Schimmel return false; 4514d7dedee1SIdo Schimmel } 4515d7dedee1SIdo Schimmel 45168d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 4517d7dedee1SIdo Schimmel { 45188d1c802bSDavid Ahern struct fib6_info *iter; 4519d7dedee1SIdo Schimmel int total = 0; 4520d7dedee1SIdo Schimmel 4521d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 45221cf844c7SDavid Ahern total += rt->fib6_nh->fib_nh_weight; 4523d7dedee1SIdo Schimmel 452493c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4525d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 45261cf844c7SDavid Ahern total += iter->fib6_nh->fib_nh_weight; 4527d7dedee1SIdo Schimmel } 4528d7dedee1SIdo Schimmel 4529d7dedee1SIdo Schimmel return total; 4530d7dedee1SIdo Schimmel } 4531d7dedee1SIdo Schimmel 45328d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4533d7dedee1SIdo Schimmel { 4534d7dedee1SIdo Schimmel int upper_bound = -1; 4535d7dedee1SIdo Schimmel 4536d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 45371cf844c7SDavid Ahern *weight += rt->fib6_nh->fib_nh_weight; 4538d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4539d7dedee1SIdo Schimmel total) - 1; 4540d7dedee1SIdo Schimmel } 45411cf844c7SDavid Ahern atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); 4542d7dedee1SIdo Schimmel } 4543d7dedee1SIdo Schimmel 45448d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4545d7dedee1SIdo Schimmel { 45468d1c802bSDavid Ahern struct fib6_info *iter; 4547d7dedee1SIdo Schimmel int weight = 0; 4548d7dedee1SIdo Schimmel 4549d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 4550d7dedee1SIdo Schimmel 455193c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4552d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 4553d7dedee1SIdo Schimmel } 4554d7dedee1SIdo Schimmel 45558d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 4556d7dedee1SIdo Schimmel { 45578d1c802bSDavid Ahern struct fib6_info *first; 4558d7dedee1SIdo Schimmel int total; 4559d7dedee1SIdo Schimmel 4560d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 4561d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 4562d7dedee1SIdo Schimmel * sibling route. 4563d7dedee1SIdo Schimmel */ 456493c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 4565d7dedee1SIdo Schimmel return; 4566d7dedee1SIdo Schimmel 4567d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 4568d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 4569d7dedee1SIdo Schimmel * onwards. 4570d7dedee1SIdo Schimmel */ 4571d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 4572d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 4573d7dedee1SIdo Schimmel return; 4574d7dedee1SIdo Schimmel 4575d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 4576d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 4577d7dedee1SIdo Schimmel } 4578d7dedee1SIdo Schimmel 45798d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 45802127d95aSIdo Schimmel { 45812127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 45827aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 45832127d95aSIdo Schimmel 4584f88d8ea6SDavid Ahern if (rt != net->ipv6.fib6_null_entry && !rt->nh && 45851cf844c7SDavid Ahern rt->fib6_nh->fib_nh_dev == arg->dev) { 45861cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; 45877aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 4588d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 45891de178edSIdo Schimmel } 45902127d95aSIdo Schimmel 45912127d95aSIdo Schimmel return 0; 45922127d95aSIdo Schimmel } 45932127d95aSIdo Schimmel 4594ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 45952127d95aSIdo Schimmel { 45962127d95aSIdo Schimmel struct arg_netdev_event arg = { 45972127d95aSIdo Schimmel .dev = dev, 45986802f3adSIdo Schimmel { 45992127d95aSIdo Schimmel .nh_flags = nh_flags, 46006802f3adSIdo Schimmel }, 46012127d95aSIdo Schimmel }; 46022127d95aSIdo Schimmel 46032127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 46042127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 46052127d95aSIdo Schimmel 46062127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 46072127d95aSIdo Schimmel } 46082127d95aSIdo Schimmel 4609f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */ 46108d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 46111de178edSIdo Schimmel const struct net_device *dev) 46121de178edSIdo Schimmel { 46138d1c802bSDavid Ahern struct fib6_info *iter; 46141de178edSIdo Schimmel 46151cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 46161de178edSIdo Schimmel return true; 461793c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46181cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 46191de178edSIdo Schimmel return true; 46201de178edSIdo Schimmel 46211de178edSIdo Schimmel return false; 46221de178edSIdo Schimmel } 46231de178edSIdo Schimmel 46248d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 46251de178edSIdo Schimmel { 46268d1c802bSDavid Ahern struct fib6_info *iter; 46271de178edSIdo Schimmel 46281de178edSIdo Schimmel rt->should_flush = 1; 462993c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46301de178edSIdo Schimmel iter->should_flush = 1; 46311de178edSIdo Schimmel } 46321de178edSIdo Schimmel 46338d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 46341de178edSIdo Schimmel const struct net_device *down_dev) 46351de178edSIdo Schimmel { 46368d1c802bSDavid Ahern struct fib6_info *iter; 46371de178edSIdo Schimmel unsigned int dead = 0; 46381de178edSIdo Schimmel 46391cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == down_dev || 46401cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 46411de178edSIdo Schimmel dead++; 464293c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46431cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == down_dev || 46441cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 46451de178edSIdo Schimmel dead++; 46461de178edSIdo Schimmel 46471de178edSIdo Schimmel return dead; 46481de178edSIdo Schimmel } 46491de178edSIdo Schimmel 46508d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 46511de178edSIdo Schimmel const struct net_device *dev, 4652ecc5663cSDavid Ahern unsigned char nh_flags) 46531de178edSIdo Schimmel { 46548d1c802bSDavid Ahern struct fib6_info *iter; 46551de178edSIdo Schimmel 46561cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 46571cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= nh_flags; 465893c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46591cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 46601cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags |= nh_flags; 46611de178edSIdo Schimmel } 46621de178edSIdo Schimmel 4663a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 46648d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 46651da177e4SLinus Torvalds { 46664c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 46674c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 46687aef6859SDavid Ahern struct net *net = dev_net(dev); 46698ed67789SDaniel Lezcano 4670f88d8ea6SDavid Ahern if (rt == net->ipv6.fib6_null_entry || rt->nh) 467127c6fa73SIdo Schimmel return 0; 467227c6fa73SIdo Schimmel 467327c6fa73SIdo Schimmel switch (arg->event) { 467427c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 46751cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 467627c6fa73SIdo Schimmel case NETDEV_DOWN: 46771de178edSIdo Schimmel if (rt->should_flush) 467827c6fa73SIdo Schimmel return -1; 467993c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 46801cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 46811de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 46821de178edSIdo Schimmel unsigned int count; 46831de178edSIdo Schimmel 46841de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 468593c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 46861de178edSIdo Schimmel rt6_multipath_flush(rt); 46871de178edSIdo Schimmel return -1; 46881de178edSIdo Schimmel } 46891de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 46901de178edSIdo Schimmel RTNH_F_LINKDOWN); 46917aef6859SDavid Ahern fib6_update_sernum(net, rt); 4692d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 46931de178edSIdo Schimmel } 46941de178edSIdo Schimmel return -2; 469527c6fa73SIdo Schimmel case NETDEV_CHANGE: 46961cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev != dev || 469793c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 469827c6fa73SIdo Schimmel break; 46991cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 4700d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 470127c6fa73SIdo Schimmel break; 47022b241361SIdo Schimmel } 4703c159d30cSDavid S. Miller 47041da177e4SLinus Torvalds return 0; 47051da177e4SLinus Torvalds } 47061da177e4SLinus Torvalds 470727c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 47081da177e4SLinus Torvalds { 47094c981e28SIdo Schimmel struct arg_netdev_event arg = { 47108ed67789SDaniel Lezcano .dev = dev, 47116802f3adSIdo Schimmel { 47124c981e28SIdo Schimmel .event = event, 47136802f3adSIdo Schimmel }, 47148ed67789SDaniel Lezcano }; 47157c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 47168ed67789SDaniel Lezcano 47177c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 47187c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 47197c6bb7d2SDavid Ahern else 47207c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 47214c981e28SIdo Schimmel } 47224c981e28SIdo Schimmel 47234c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 47244c981e28SIdo Schimmel { 47254c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 47264c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 47274c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 47281da177e4SLinus Torvalds } 47291da177e4SLinus Torvalds 473095c96174SEric Dumazet struct rt6_mtu_change_arg { 47311da177e4SLinus Torvalds struct net_device *dev; 473295c96174SEric Dumazet unsigned int mtu; 4733c0b220cfSDavid Ahern struct fib6_info *f6i; 47341da177e4SLinus Torvalds }; 47351da177e4SLinus Torvalds 4736cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) 4737c0b220cfSDavid Ahern { 4738c0b220cfSDavid Ahern struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; 4739cc5c073aSDavid Ahern struct fib6_info *f6i = arg->f6i; 4740c0b220cfSDavid Ahern 4741c0b220cfSDavid Ahern /* For administrative MTU increase, there is no way to discover 4742c0b220cfSDavid Ahern * IPv6 PMTU increase, so PMTU increase should be updated here. 4743c0b220cfSDavid Ahern * Since RFC 1981 doesn't include administrative MTU increase 4744c0b220cfSDavid Ahern * update PMTU increase is a MUST. (i.e. jumbo frame) 4745c0b220cfSDavid Ahern */ 4746c0b220cfSDavid Ahern if (nh->fib_nh_dev == arg->dev) { 4747c0b220cfSDavid Ahern struct inet6_dev *idev = __in6_dev_get(arg->dev); 4748c0b220cfSDavid Ahern u32 mtu = f6i->fib6_pmtu; 4749c0b220cfSDavid Ahern 4750c0b220cfSDavid Ahern if (mtu >= arg->mtu || 4751c0b220cfSDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4752c0b220cfSDavid Ahern fib6_metric_set(f6i, RTAX_MTU, arg->mtu); 4753c0b220cfSDavid Ahern 4754c0b220cfSDavid Ahern spin_lock_bh(&rt6_exception_lock); 4755cc5c073aSDavid Ahern rt6_exceptions_update_pmtu(idev, nh, arg->mtu); 4756c0b220cfSDavid Ahern spin_unlock_bh(&rt6_exception_lock); 4757c0b220cfSDavid Ahern } 4758c0b220cfSDavid Ahern 4759c0b220cfSDavid Ahern return 0; 4760c0b220cfSDavid Ahern } 4761c0b220cfSDavid Ahern 4762c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) 47631da177e4SLinus Torvalds { 47641da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 47651da177e4SLinus Torvalds struct inet6_dev *idev; 47661da177e4SLinus Torvalds 47671da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 47681da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 47691da177e4SLinus Torvalds We still use this lock to block changes 47701da177e4SLinus Torvalds caused by addrconf/ndisc. 47711da177e4SLinus Torvalds */ 47721da177e4SLinus Torvalds 47731da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 477438308473SDavid S. Miller if (!idev) 47751da177e4SLinus Torvalds return 0; 47761da177e4SLinus Torvalds 4777c0b220cfSDavid Ahern if (fib6_metric_locked(f6i, RTAX_MTU)) 47781da177e4SLinus Torvalds return 0; 4779c0b220cfSDavid Ahern 4780c0b220cfSDavid Ahern arg->f6i = f6i; 47812d44234bSDavid Ahern if (f6i->nh) { 47822d44234bSDavid Ahern /* fib6_nh_mtu_change only returns 0, so this is safe */ 47832d44234bSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, 47842d44234bSDavid Ahern arg); 47852d44234bSDavid Ahern } 47862d44234bSDavid Ahern 47871cf844c7SDavid Ahern return fib6_nh_mtu_change(f6i->fib6_nh, arg); 47881da177e4SLinus Torvalds } 47891da177e4SLinus Torvalds 479095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 47911da177e4SLinus Torvalds { 4792c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4793c71099acSThomas Graf .dev = dev, 4794c71099acSThomas Graf .mtu = mtu, 4795c71099acSThomas Graf }; 47961da177e4SLinus Torvalds 47970c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 47981da177e4SLinus Torvalds } 47991da177e4SLinus Torvalds 4800ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 480175425657SDavid Ahern [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 48025176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4803aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 480486872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4805ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 480686872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 480786872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 480851ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4809c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 481019e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 481119e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 481232bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4813622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 48143b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4815aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4816eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4817eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4818eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 48195b98324eSDavid Ahern [RTA_NH_ID] = { .type = NLA_U32 }, 482086872cb5SThomas Graf }; 482186872cb5SThomas Graf 482286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4823333c4301SDavid Ahern struct fib6_config *cfg, 4824333c4301SDavid Ahern struct netlink_ext_ack *extack) 48251da177e4SLinus Torvalds { 482686872cb5SThomas Graf struct rtmsg *rtm; 482786872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4828c78ba6d6SLubomir Rintel unsigned int pref; 482986872cb5SThomas Graf int err; 48301da177e4SLinus Torvalds 48318cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 48328cb08174SJohannes Berg rtm_ipv6_policy, extack); 483386872cb5SThomas Graf if (err < 0) 483486872cb5SThomas Graf goto errout; 48351da177e4SLinus Torvalds 483686872cb5SThomas Graf err = -EINVAL; 483786872cb5SThomas Graf rtm = nlmsg_data(nlh); 483886872cb5SThomas Graf 483984db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 484084db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 484184db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 484284db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 484384db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 484484db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 484584db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 484684db8407SMaciej Żenczykowski 484784db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 484884db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 484984db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 485084db8407SMaciej Żenczykowski }; 485186872cb5SThomas Graf 4852ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4853ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4854b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4855b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 485686872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 485786872cb5SThomas Graf 4858ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4859ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4860ab79ad14SMaciej Żenczykowski 48611f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 48621f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 48631f56a01fSMartin KaFai Lau 4864fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4865fc1e64e1SDavid Ahern 48665b98324eSDavid Ahern if (tb[RTA_NH_ID]) { 48675b98324eSDavid Ahern if (tb[RTA_GATEWAY] || tb[RTA_OIF] || 48685b98324eSDavid Ahern tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { 48695b98324eSDavid Ahern NL_SET_ERR_MSG(extack, 48705b98324eSDavid Ahern "Nexthop specification and nexthop id are mutually exclusive"); 48715b98324eSDavid Ahern goto errout; 48725b98324eSDavid Ahern } 48735b98324eSDavid Ahern cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); 48745b98324eSDavid Ahern } 48755b98324eSDavid Ahern 487686872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 487767b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 487886872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 48791da177e4SLinus Torvalds } 4880e3818541SDavid Ahern if (tb[RTA_VIA]) { 4881e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 4882e3818541SDavid Ahern goto errout; 4883e3818541SDavid Ahern } 488486872cb5SThomas Graf 488586872cb5SThomas Graf if (tb[RTA_DST]) { 488686872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 488786872cb5SThomas Graf 488886872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 488986872cb5SThomas Graf goto errout; 489086872cb5SThomas Graf 489186872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 48921da177e4SLinus Torvalds } 489386872cb5SThomas Graf 489486872cb5SThomas Graf if (tb[RTA_SRC]) { 489586872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 489686872cb5SThomas Graf 489786872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 489886872cb5SThomas Graf goto errout; 489986872cb5SThomas Graf 490086872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 49011da177e4SLinus Torvalds } 490286872cb5SThomas Graf 4903c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 490467b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4905c3968a85SDaniel Walter 490686872cb5SThomas Graf if (tb[RTA_OIF]) 490786872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 490886872cb5SThomas Graf 490986872cb5SThomas Graf if (tb[RTA_PRIORITY]) 491086872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 491186872cb5SThomas Graf 491286872cb5SThomas Graf if (tb[RTA_METRICS]) { 491386872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 491486872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 49151da177e4SLinus Torvalds } 491686872cb5SThomas Graf 491786872cb5SThomas Graf if (tb[RTA_TABLE]) 491886872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 491986872cb5SThomas Graf 492051ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 492151ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 492251ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 49239ed59592SDavid Ahern 49249ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4925c255bd68SDavid Ahern cfg->fc_mp_len, extack); 49269ed59592SDavid Ahern if (err < 0) 49279ed59592SDavid Ahern goto errout; 492851ebd318SNicolas Dichtel } 492951ebd318SNicolas Dichtel 4930c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4931c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4932c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4933c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4934c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4935c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4936c78ba6d6SLubomir Rintel } 4937c78ba6d6SLubomir Rintel 493819e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 493919e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 494019e42e45SRoopa Prabhu 49419ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 494219e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 494319e42e45SRoopa Prabhu 4944c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 49459ed59592SDavid Ahern if (err < 0) 49469ed59592SDavid Ahern goto errout; 49479ed59592SDavid Ahern } 49489ed59592SDavid Ahern 494932bc201eSXin Long if (tb[RTA_EXPIRES]) { 495032bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 495132bc201eSXin Long 495232bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 495332bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 495432bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 495532bc201eSXin Long } 495632bc201eSXin Long } 495732bc201eSXin Long 495886872cb5SThomas Graf err = 0; 495986872cb5SThomas Graf errout: 496086872cb5SThomas Graf return err; 49611da177e4SLinus Torvalds } 49621da177e4SLinus Torvalds 49636b9ea5a6SRoopa Prabhu struct rt6_nh { 49648d1c802bSDavid Ahern struct fib6_info *fib6_info; 49656b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 49666b9ea5a6SRoopa Prabhu struct list_head next; 49676b9ea5a6SRoopa Prabhu }; 49686b9ea5a6SRoopa Prabhu 4969d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 4970d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 49718d1c802bSDavid Ahern struct fib6_info *rt, 49728d1c802bSDavid Ahern struct fib6_config *r_cfg) 49736b9ea5a6SRoopa Prabhu { 49746b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 49756b9ea5a6SRoopa Prabhu int err = -EEXIST; 49766b9ea5a6SRoopa Prabhu 49776b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 49788d1c802bSDavid Ahern /* check if fib6_info already exists */ 49798d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 49806b9ea5a6SRoopa Prabhu return err; 49816b9ea5a6SRoopa Prabhu } 49826b9ea5a6SRoopa Prabhu 49836b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 49846b9ea5a6SRoopa Prabhu if (!nh) 49856b9ea5a6SRoopa Prabhu return -ENOMEM; 49868d1c802bSDavid Ahern nh->fib6_info = rt; 49876b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 49886b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 49896b9ea5a6SRoopa Prabhu 49906b9ea5a6SRoopa Prabhu return 0; 49916b9ea5a6SRoopa Prabhu } 49926b9ea5a6SRoopa Prabhu 49938d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 49948d1c802bSDavid Ahern struct fib6_info *rt_last, 49953b1137feSDavid Ahern struct nl_info *info, 49963b1137feSDavid Ahern __u16 nlflags) 49973b1137feSDavid Ahern { 49983b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 49993b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 50003b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 50013b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 50023b1137feSDavid Ahern * the list, find the first sibling of the last route appended 50033b1137feSDavid Ahern */ 500493c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 500593c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 50068d1c802bSDavid Ahern struct fib6_info, 500793c2fb25SDavid Ahern fib6_siblings); 50083b1137feSDavid Ahern } 50093b1137feSDavid Ahern 50103b1137feSDavid Ahern if (rt) 50113b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 50123b1137feSDavid Ahern } 50133b1137feSDavid Ahern 5014333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 5015333c4301SDavid Ahern struct netlink_ext_ack *extack) 501651ebd318SNicolas Dichtel { 50178d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 50183b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 5019ebee3cadSIdo Schimmel enum fib_event_type event_type; 502051ebd318SNicolas Dichtel struct fib6_config r_cfg; 502151ebd318SNicolas Dichtel struct rtnexthop *rtnh; 50228d1c802bSDavid Ahern struct fib6_info *rt; 50236b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 50246b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 50253b1137feSDavid Ahern __u16 nlflags; 502651ebd318SNicolas Dichtel int remaining; 502751ebd318SNicolas Dichtel int attrlen; 50286b9ea5a6SRoopa Prabhu int err = 1; 50296b9ea5a6SRoopa Prabhu int nhn = 0; 50306b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 50316b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 50326b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 503351ebd318SNicolas Dichtel 50343b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 50353b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 50363b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 50373b1137feSDavid Ahern 503835f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 503951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 504051ebd318SNicolas Dichtel 50416b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 50428d1c802bSDavid Ahern * fib6_info structs per nexthop 50436b9ea5a6SRoopa Prabhu */ 504451ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 504551ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 504651ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 504751ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 504851ebd318SNicolas Dichtel 504951ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 505051ebd318SNicolas Dichtel if (attrlen > 0) { 505151ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 505251ebd318SNicolas Dichtel 505351ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 505451ebd318SNicolas Dichtel if (nla) { 505567b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 505651ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 505751ebd318SNicolas Dichtel } 505819e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 505919e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 506019e42e45SRoopa Prabhu if (nla) 506119e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 506251ebd318SNicolas Dichtel } 50636b9ea5a6SRoopa Prabhu 506468e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 5065acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 50668c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 50678c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 50688c5b83f0SRoopa Prabhu rt = NULL; 50696b9ea5a6SRoopa Prabhu goto cleanup; 50708c5b83f0SRoopa Prabhu } 5071b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 5072b5d2d75eSDavid Ahern err = -EINVAL; 5073b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 5074b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 5075b5d2d75eSDavid Ahern fib6_info_release(rt); 5076b5d2d75eSDavid Ahern goto cleanup; 5077b5d2d75eSDavid Ahern } 50786b9ea5a6SRoopa Prabhu 50791cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; 5080398958aeSIdo Schimmel 5081d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 5082d4ead6b3SDavid Ahern rt, &r_cfg); 508351ebd318SNicolas Dichtel if (err) { 508493531c67SDavid Ahern fib6_info_release(rt); 50856b9ea5a6SRoopa Prabhu goto cleanup; 508651ebd318SNicolas Dichtel } 50876b9ea5a6SRoopa Prabhu 50886b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 508951ebd318SNicolas Dichtel } 50906b9ea5a6SRoopa Prabhu 50919eee3b49SIdo Schimmel if (list_empty(&rt6_nh_list)) { 50929eee3b49SIdo Schimmel NL_SET_ERR_MSG(extack, 50939eee3b49SIdo Schimmel "Invalid nexthop configuration - no valid nexthops"); 50949eee3b49SIdo Schimmel return -EINVAL; 50959eee3b49SIdo Schimmel } 50969eee3b49SIdo Schimmel 50973b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 50983b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 50993b1137feSDavid Ahern * the full route when done 51003b1137feSDavid Ahern */ 51013b1137feSDavid Ahern info->skip_notify = 1; 51023b1137feSDavid Ahern 5103ebee3cadSIdo Schimmel /* For add and replace, send one notification with all nexthops. For 5104ebee3cadSIdo Schimmel * append, send one notification with all appended nexthops. 5105ebee3cadSIdo Schimmel */ 5106ebee3cadSIdo Schimmel info->skip_notify_kernel = 1; 5107ebee3cadSIdo Schimmel 51086b9ea5a6SRoopa Prabhu err_nh = NULL; 51096b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 51108d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 51118d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 51123b1137feSDavid Ahern 5113f7225172SDavid Ahern if (!err) { 5114f7225172SDavid Ahern /* save reference to last route successfully inserted */ 5115f7225172SDavid Ahern rt_last = nh->fib6_info; 5116f7225172SDavid Ahern 51176b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 5118f7225172SDavid Ahern if (!rt_notif) 51198d1c802bSDavid Ahern rt_notif = nh->fib6_info; 5120f7225172SDavid Ahern } 51216b9ea5a6SRoopa Prabhu 51228d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 51238d1c802bSDavid Ahern nh->fib6_info = NULL; 51246b9ea5a6SRoopa Prabhu if (err) { 51256b9ea5a6SRoopa Prabhu if (replace && nhn) 5126a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 5127a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 51286b9ea5a6SRoopa Prabhu err_nh = nh; 51296b9ea5a6SRoopa Prabhu goto add_errout; 51306b9ea5a6SRoopa Prabhu } 51316b9ea5a6SRoopa Prabhu 51321a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 513327596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 513427596472SMichal Kubeček * we have already failed to add the first nexthop: 513527596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 513627596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 513727596472SMichal Kubeček * be added to it. 51381a72418bSNicolas Dichtel */ 513927596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 514027596472SMichal Kubeček NLM_F_REPLACE); 51416b9ea5a6SRoopa Prabhu nhn++; 51426b9ea5a6SRoopa Prabhu } 51436b9ea5a6SRoopa Prabhu 5144ebee3cadSIdo Schimmel event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD; 5145ebee3cadSIdo Schimmel err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type, 5146ebee3cadSIdo Schimmel rt_notif, nhn - 1, extack); 5147ebee3cadSIdo Schimmel if (err) { 5148ebee3cadSIdo Schimmel /* Delete all the siblings that were just added */ 5149ebee3cadSIdo Schimmel err_nh = NULL; 5150ebee3cadSIdo Schimmel goto add_errout; 5151ebee3cadSIdo Schimmel } 5152ebee3cadSIdo Schimmel 51533b1137feSDavid Ahern /* success ... tell user about new route */ 51543b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 51556b9ea5a6SRoopa Prabhu goto cleanup; 51566b9ea5a6SRoopa Prabhu 51576b9ea5a6SRoopa Prabhu add_errout: 51583b1137feSDavid Ahern /* send notification for routes that were added so that 51593b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 51603b1137feSDavid Ahern * coherent 51613b1137feSDavid Ahern */ 51623b1137feSDavid Ahern if (rt_notif) 51633b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 51643b1137feSDavid Ahern 51656b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 51666b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 51676b9ea5a6SRoopa Prabhu if (err_nh == nh) 51686b9ea5a6SRoopa Prabhu break; 5169333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 51706b9ea5a6SRoopa Prabhu } 51716b9ea5a6SRoopa Prabhu 51726b9ea5a6SRoopa Prabhu cleanup: 51736b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 51748d1c802bSDavid Ahern if (nh->fib6_info) 51758d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 51766b9ea5a6SRoopa Prabhu list_del(&nh->next); 51776b9ea5a6SRoopa Prabhu kfree(nh); 51786b9ea5a6SRoopa Prabhu } 51796b9ea5a6SRoopa Prabhu 51806b9ea5a6SRoopa Prabhu return err; 51816b9ea5a6SRoopa Prabhu } 51826b9ea5a6SRoopa Prabhu 5183333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 5184333c4301SDavid Ahern struct netlink_ext_ack *extack) 51856b9ea5a6SRoopa Prabhu { 51866b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 51876b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 51886b9ea5a6SRoopa Prabhu int remaining; 51896b9ea5a6SRoopa Prabhu int attrlen; 51906b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 51916b9ea5a6SRoopa Prabhu 51926b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 51936b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 51946b9ea5a6SRoopa Prabhu 51956b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 51966b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 51976b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 51986b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 51996b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 52006b9ea5a6SRoopa Prabhu 52016b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 52026b9ea5a6SRoopa Prabhu if (attrlen > 0) { 52036b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 52046b9ea5a6SRoopa Prabhu 52056b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 52066b9ea5a6SRoopa Prabhu if (nla) { 52076b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 52086b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 52096b9ea5a6SRoopa Prabhu } 52106b9ea5a6SRoopa Prabhu } 5211333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 52126b9ea5a6SRoopa Prabhu if (err) 52136b9ea5a6SRoopa Prabhu last_err = err; 52146b9ea5a6SRoopa Prabhu 521551ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 521651ebd318SNicolas Dichtel } 521751ebd318SNicolas Dichtel 521851ebd318SNicolas Dichtel return last_err; 521951ebd318SNicolas Dichtel } 522051ebd318SNicolas Dichtel 5221c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5222c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 52231da177e4SLinus Torvalds { 522486872cb5SThomas Graf struct fib6_config cfg; 522586872cb5SThomas Graf int err; 52261da177e4SLinus Torvalds 5227333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 522886872cb5SThomas Graf if (err < 0) 522986872cb5SThomas Graf return err; 523086872cb5SThomas Graf 52315b98324eSDavid Ahern if (cfg.fc_nh_id && 52325b98324eSDavid Ahern !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { 52335b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 52345b98324eSDavid Ahern return -EINVAL; 52355b98324eSDavid Ahern } 52365b98324eSDavid Ahern 523751ebd318SNicolas Dichtel if (cfg.fc_mp) 5238333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 52390ae81335SDavid Ahern else { 52400ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 5241333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 52421da177e4SLinus Torvalds } 52430ae81335SDavid Ahern } 52441da177e4SLinus Torvalds 5245c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5246c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 52471da177e4SLinus Torvalds { 524886872cb5SThomas Graf struct fib6_config cfg; 524986872cb5SThomas Graf int err; 52501da177e4SLinus Torvalds 5251333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 525286872cb5SThomas Graf if (err < 0) 525386872cb5SThomas Graf return err; 525486872cb5SThomas Graf 525567f69513SDavid Ahern if (cfg.fc_metric == 0) 525667f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 525767f69513SDavid Ahern 525851ebd318SNicolas Dichtel if (cfg.fc_mp) 5259333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 526051ebd318SNicolas Dichtel else 5261acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 52621da177e4SLinus Torvalds } 52631da177e4SLinus Torvalds 5264a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */ 5265a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) 5266339bf98fSThomas Graf { 5267a1b7a1f0SDavid Ahern int *nexthop_len = arg; 5268beb1afacSDavid Ahern 5269a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ 5270a1b7a1f0SDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5271a1b7a1f0SDavid Ahern + nla_total_size(16); /* RTA_GATEWAY */ 5272f88d8ea6SDavid Ahern 5273a1b7a1f0SDavid Ahern if (nh->fib_nh_lws) { 5274a1b7a1f0SDavid Ahern /* RTA_ENCAP_TYPE */ 5275a1b7a1f0SDavid Ahern *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5276a1b7a1f0SDavid Ahern /* RTA_ENCAP */ 5277a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(2); 5278a1b7a1f0SDavid Ahern } 5279a1b7a1f0SDavid Ahern 5280a1b7a1f0SDavid Ahern return 0; 5281a1b7a1f0SDavid Ahern } 5282a1b7a1f0SDavid Ahern 5283a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i) 5284a1b7a1f0SDavid Ahern { 5285a1b7a1f0SDavid Ahern int nexthop_len; 5286a1b7a1f0SDavid Ahern 5287a1b7a1f0SDavid Ahern if (f6i->nh) { 5288a1b7a1f0SDavid Ahern nexthop_len = nla_total_size(4); /* RTA_NH_ID */ 5289a1b7a1f0SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, 5290a1b7a1f0SDavid Ahern &nexthop_len); 5291a1b7a1f0SDavid Ahern } else { 5292a1b7a1f0SDavid Ahern struct fib6_nh *nh = f6i->fib6_nh; 5293a1b7a1f0SDavid Ahern 5294a1b7a1f0SDavid Ahern nexthop_len = 0; 5295a1b7a1f0SDavid Ahern if (f6i->fib6_nsiblings) { 5296beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 5297beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5298beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 5299a1b7a1f0SDavid Ahern + lwtunnel_get_encap_size(nh->fib_nh_lws); 5300beb1afacSDavid Ahern 5301a1b7a1f0SDavid Ahern nexthop_len *= f6i->fib6_nsiblings; 5302a1b7a1f0SDavid Ahern } 5303a1b7a1f0SDavid Ahern nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5304beb1afacSDavid Ahern } 5305beb1afacSDavid Ahern 5306339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 5307339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 5308339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 5309339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 5310339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 5311339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 5312339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 5313339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 5314339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 53156a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 5316ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 5317c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 531819e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 5319beb1afacSDavid Ahern + nexthop_len; 5320beb1afacSDavid Ahern } 5321beb1afacSDavid Ahern 5322f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, 5323f88d8ea6SDavid Ahern unsigned char *flags) 5324f88d8ea6SDavid Ahern { 5325f88d8ea6SDavid Ahern if (nexthop_is_multipath(nh)) { 5326f88d8ea6SDavid Ahern struct nlattr *mp; 5327f88d8ea6SDavid Ahern 5328f88d8ea6SDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 5329f88d8ea6SDavid Ahern if (!mp) 5330f88d8ea6SDavid Ahern goto nla_put_failure; 5331f88d8ea6SDavid Ahern 5332f88d8ea6SDavid Ahern if (nexthop_mpath_fill_node(skb, nh)) 5333f88d8ea6SDavid Ahern goto nla_put_failure; 5334f88d8ea6SDavid Ahern 5335f88d8ea6SDavid Ahern nla_nest_end(skb, mp); 5336f88d8ea6SDavid Ahern } else { 5337f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 5338f88d8ea6SDavid Ahern 5339f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(nh); 5340f88d8ea6SDavid Ahern if (fib_nexthop_info(skb, &fib6_nh->nh_common, 5341f88d8ea6SDavid Ahern flags, false) < 0) 5342f88d8ea6SDavid Ahern goto nla_put_failure; 5343f88d8ea6SDavid Ahern } 5344f88d8ea6SDavid Ahern 5345f88d8ea6SDavid Ahern return 0; 5346f88d8ea6SDavid Ahern 5347f88d8ea6SDavid Ahern nla_put_failure: 5348f88d8ea6SDavid Ahern return -EMSGSIZE; 5349f88d8ea6SDavid Ahern } 5350f88d8ea6SDavid Ahern 5351d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 53528d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 5353d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 535415e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 5355f8cfe2ceSDavid Ahern unsigned int flags) 53561da177e4SLinus Torvalds { 535722d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 535822d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 535922d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 5360f88d8ea6SDavid Ahern unsigned char nh_flags = 0; 53611da177e4SLinus Torvalds struct nlmsghdr *nlh; 536222d0bd82SXin Long struct rtmsg *rtm; 5363d4ead6b3SDavid Ahern long expires = 0; 53641da177e4SLinus Torvalds 536515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 536638308473SDavid S. Miller if (!nlh) 536726932566SPatrick McHardy return -EMSGSIZE; 53682d7202bfSThomas Graf 536922d0bd82SXin Long if (rt6) { 537022d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 537122d0bd82SXin Long rt6_src = &rt6->rt6i_src; 537222d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 537322d0bd82SXin Long } else { 537422d0bd82SXin Long rt6_dst = &rt->fib6_dst; 537522d0bd82SXin Long rt6_src = &rt->fib6_src; 537622d0bd82SXin Long rt6_flags = rt->fib6_flags; 537722d0bd82SXin Long } 537822d0bd82SXin Long 53792d7202bfSThomas Graf rtm = nlmsg_data(nlh); 53801da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 538122d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 538222d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 53831da177e4SLinus Torvalds rtm->rtm_tos = 0; 538493c2fb25SDavid Ahern if (rt->fib6_table) 538593c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 5386c71099acSThomas Graf else 53879e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 538897f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 5389c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 5390c78679e8SDavid S. Miller goto nla_put_failure; 5391e8478e80SDavid Ahern 5392e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 53931da177e4SLinus Torvalds rtm->rtm_flags = 0; 53941da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 539593c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 53961da177e4SLinus Torvalds 539722d0bd82SXin Long if (rt6_flags & RTF_CACHE) 53981da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 53991da177e4SLinus Torvalds 5400d4ead6b3SDavid Ahern if (dest) { 5401d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 5402c78679e8SDavid S. Miller goto nla_put_failure; 54031da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 54041da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 540522d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 5406c78679e8SDavid S. Miller goto nla_put_failure; 54071da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 54081da177e4SLinus Torvalds if (src) { 5409930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 5410c78679e8SDavid S. Miller goto nla_put_failure; 54111da177e4SLinus Torvalds rtm->rtm_src_len = 128; 5412c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 541322d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 5414c78679e8SDavid S. Miller goto nla_put_failure; 54151da177e4SLinus Torvalds #endif 54167bc570c8SYOSHIFUJI Hideaki if (iif) { 54177bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 541822d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 5419fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 54202cf75070SNikolay Aleksandrov 54217bc570c8SYOSHIFUJI Hideaki if (err == 0) 54227bc570c8SYOSHIFUJI Hideaki return 0; 5423fd61c6baSDavid Ahern if (err < 0) 54247bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 54257bc570c8SYOSHIFUJI Hideaki } else 54267bc570c8SYOSHIFUJI Hideaki #endif 5427c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 5428c78679e8SDavid S. Miller goto nla_put_failure; 5429d4ead6b3SDavid Ahern } else if (dest) { 54301da177e4SLinus Torvalds struct in6_addr saddr_buf; 5431d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 5432930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5433c78679e8SDavid S. Miller goto nla_put_failure; 5434c3968a85SDaniel Walter } 5435c3968a85SDaniel Walter 543693c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 5437c3968a85SDaniel Walter struct in6_addr saddr_buf; 543893c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 5439930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5440c78679e8SDavid S. Miller goto nla_put_failure; 54411da177e4SLinus Torvalds } 54422d7202bfSThomas Graf 5443d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 5444d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 54452d7202bfSThomas Graf goto nla_put_failure; 54462d7202bfSThomas Graf 544793c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 5448beb1afacSDavid Ahern goto nla_put_failure; 5449beb1afacSDavid Ahern 5450beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 5451beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 5452beb1afacSDavid Ahern */ 545322d0bd82SXin Long if (rt6) { 545422d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 545522d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 545622d0bd82SXin Long goto nla_put_failure; 545722d0bd82SXin Long 545822d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 545922d0bd82SXin Long goto nla_put_failure; 546022d0bd82SXin Long } else if (rt->fib6_nsiblings) { 54618d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 5462beb1afacSDavid Ahern struct nlattr *mp; 5463beb1afacSDavid Ahern 5464ae0be8deSMichal Kubecek mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5465beb1afacSDavid Ahern if (!mp) 5466beb1afacSDavid Ahern goto nla_put_failure; 5467beb1afacSDavid Ahern 54681cf844c7SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, 54691cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight) < 0) 5470beb1afacSDavid Ahern goto nla_put_failure; 5471beb1afacSDavid Ahern 5472beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 547393c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 54741cf844c7SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, 54751cf844c7SDavid Ahern sibling->fib6_nh->fib_nh_weight) < 0) 547694f826b8SEric Dumazet goto nla_put_failure; 547794f826b8SEric Dumazet } 54782d7202bfSThomas Graf 5479beb1afacSDavid Ahern nla_nest_end(skb, mp); 5480f88d8ea6SDavid Ahern } else if (rt->nh) { 5481f88d8ea6SDavid Ahern if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) 5482f88d8ea6SDavid Ahern goto nla_put_failure; 5483ecc5663cSDavid Ahern 5484f88d8ea6SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 5485f88d8ea6SDavid Ahern rtm->rtm_type = RTN_BLACKHOLE; 5486f88d8ea6SDavid Ahern 5487f88d8ea6SDavid Ahern if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) 5488f88d8ea6SDavid Ahern goto nla_put_failure; 5489f88d8ea6SDavid Ahern 5490f88d8ea6SDavid Ahern rtm->rtm_flags |= nh_flags; 5491f88d8ea6SDavid Ahern } else { 54921cf844c7SDavid Ahern if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, 5493ecc5663cSDavid Ahern &nh_flags, false) < 0) 5494c78679e8SDavid S. Miller goto nla_put_failure; 5495ecc5663cSDavid Ahern 5496ecc5663cSDavid Ahern rtm->rtm_flags |= nh_flags; 5497beb1afacSDavid Ahern } 54988253947eSLi Wei 549922d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 550014895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 550114895687SDavid Ahern expires -= jiffies; 550214895687SDavid Ahern } 550369cdf8f9SYOSHIFUJI Hideaki 5504d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 5505e3703b3dSThomas Graf goto nla_put_failure; 55061da177e4SLinus Torvalds 550722d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 5508c78ba6d6SLubomir Rintel goto nla_put_failure; 5509c78ba6d6SLubomir Rintel 551019e42e45SRoopa Prabhu 5511053c095aSJohannes Berg nlmsg_end(skb, nlh); 5512053c095aSJohannes Berg return 0; 55132d7202bfSThomas Graf 55142d7202bfSThomas Graf nla_put_failure: 551526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 551626932566SPatrick McHardy return -EMSGSIZE; 55171da177e4SLinus Torvalds } 55181da177e4SLinus Torvalds 55192c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) 55202c170e07SDavid Ahern { 55212c170e07SDavid Ahern const struct net_device *dev = arg; 55222c170e07SDavid Ahern 55232c170e07SDavid Ahern if (nh->fib_nh_dev == dev) 55242c170e07SDavid Ahern return 1; 55252c170e07SDavid Ahern 55262c170e07SDavid Ahern return 0; 55272c170e07SDavid Ahern } 55282c170e07SDavid Ahern 552913e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 553013e38901SDavid Ahern const struct net_device *dev) 553113e38901SDavid Ahern { 55322c170e07SDavid Ahern if (f6i->nh) { 55332c170e07SDavid Ahern struct net_device *_dev = (struct net_device *)dev; 55342c170e07SDavid Ahern 55352c170e07SDavid Ahern return !!nexthop_for_each_fib6_nh(f6i->nh, 55362c170e07SDavid Ahern fib6_info_nh_uses_dev, 55372c170e07SDavid Ahern _dev); 55382c170e07SDavid Ahern } 55392c170e07SDavid Ahern 55401cf844c7SDavid Ahern if (f6i->fib6_nh->fib_nh_dev == dev) 554113e38901SDavid Ahern return true; 554213e38901SDavid Ahern 554313e38901SDavid Ahern if (f6i->fib6_nsiblings) { 554413e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 554513e38901SDavid Ahern 554613e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 554713e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 55481cf844c7SDavid Ahern if (sibling->fib6_nh->fib_nh_dev == dev) 554913e38901SDavid Ahern return true; 555013e38901SDavid Ahern } 555113e38901SDavid Ahern } 555213e38901SDavid Ahern 555313e38901SDavid Ahern return false; 555413e38901SDavid Ahern } 555513e38901SDavid Ahern 55561e47b483SStefano Brivio struct fib6_nh_exception_dump_walker { 55571e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump; 55581e47b483SStefano Brivio struct fib6_info *rt; 55591e47b483SStefano Brivio unsigned int flags; 55601e47b483SStefano Brivio unsigned int skip; 55611e47b483SStefano Brivio unsigned int count; 55621e47b483SStefano Brivio }; 55631e47b483SStefano Brivio 55641e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) 55651e47b483SStefano Brivio { 55661e47b483SStefano Brivio struct fib6_nh_exception_dump_walker *w = arg; 55671e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump = w->dump; 55681e47b483SStefano Brivio struct rt6_exception_bucket *bucket; 55691e47b483SStefano Brivio struct rt6_exception *rt6_ex; 55701e47b483SStefano Brivio int i, err; 55711e47b483SStefano Brivio 55721e47b483SStefano Brivio bucket = fib6_nh_get_excptn_bucket(nh, NULL); 55731e47b483SStefano Brivio if (!bucket) 55741e47b483SStefano Brivio return 0; 55751e47b483SStefano Brivio 55761e47b483SStefano Brivio for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 55771e47b483SStefano Brivio hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 55781e47b483SStefano Brivio if (w->skip) { 55791e47b483SStefano Brivio w->skip--; 55801e47b483SStefano Brivio continue; 55811e47b483SStefano Brivio } 55821e47b483SStefano Brivio 55831e47b483SStefano Brivio /* Expiration of entries doesn't bump sernum, insertion 55841e47b483SStefano Brivio * does. Removal is triggered by insertion, so we can 55851e47b483SStefano Brivio * rely on the fact that if entries change between two 55861e47b483SStefano Brivio * partial dumps, this node is scanned again completely, 55871e47b483SStefano Brivio * see rt6_insert_exception() and fib6_dump_table(). 55881e47b483SStefano Brivio * 55891e47b483SStefano Brivio * Count expired entries we go through as handled 55901e47b483SStefano Brivio * entries that we'll skip next time, in case of partial 55911e47b483SStefano Brivio * node dump. Otherwise, if entries expire meanwhile, 55921e47b483SStefano Brivio * we'll skip the wrong amount. 55931e47b483SStefano Brivio */ 55941e47b483SStefano Brivio if (rt6_check_expired(rt6_ex->rt6i)) { 55951e47b483SStefano Brivio w->count++; 55961e47b483SStefano Brivio continue; 55971e47b483SStefano Brivio } 55981e47b483SStefano Brivio 55991e47b483SStefano Brivio err = rt6_fill_node(dump->net, dump->skb, w->rt, 56001e47b483SStefano Brivio &rt6_ex->rt6i->dst, NULL, NULL, 0, 56011e47b483SStefano Brivio RTM_NEWROUTE, 56021e47b483SStefano Brivio NETLINK_CB(dump->cb->skb).portid, 56031e47b483SStefano Brivio dump->cb->nlh->nlmsg_seq, w->flags); 56041e47b483SStefano Brivio if (err) 56051e47b483SStefano Brivio return err; 56061e47b483SStefano Brivio 56071e47b483SStefano Brivio w->count++; 56081e47b483SStefano Brivio } 56091e47b483SStefano Brivio bucket++; 56101e47b483SStefano Brivio } 56111e47b483SStefano Brivio 56121e47b483SStefano Brivio return 0; 56131e47b483SStefano Brivio } 56141e47b483SStefano Brivio 5615bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */ 56161e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) 56171da177e4SLinus Torvalds { 56181da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 561913e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 562013e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 56211f17e2f2SDavid Ahern struct net *net = arg->net; 56221e47b483SStefano Brivio int count = 0; 56231f17e2f2SDavid Ahern 5624421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 5625bf9a8a06SStefano Brivio return -1; 56261da177e4SLinus Torvalds 562713e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 562893c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 5629f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 5630bf9a8a06SStefano Brivio return -1; 5631f8cfe2ceSDavid Ahern } 56321e47b483SStefano Brivio if (filter->filter_set && 56331e47b483SStefano Brivio ((filter->rt_type && rt->fib6_type != filter->rt_type) || 563413e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 56351e47b483SStefano Brivio (filter->protocol && rt->fib6_protocol != filter->protocol))) { 5636bf9a8a06SStefano Brivio return -1; 563713e38901SDavid Ahern } 56381e47b483SStefano Brivio 56391e47b483SStefano Brivio if (filter->filter_set || 56401e47b483SStefano Brivio !filter->dump_routes || !filter->dump_exceptions) { 564113e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 5642f8cfe2ceSDavid Ahern } 56431da177e4SLinus Torvalds 56441e47b483SStefano Brivio if (filter->dump_routes) { 56451e47b483SStefano Brivio if (skip) { 56461e47b483SStefano Brivio skip--; 56471e47b483SStefano Brivio } else { 56481e47b483SStefano Brivio if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 56491e47b483SStefano Brivio 0, RTM_NEWROUTE, 5650bf9a8a06SStefano Brivio NETLINK_CB(arg->cb->skb).portid, 56511e47b483SStefano Brivio arg->cb->nlh->nlmsg_seq, flags)) { 5652bf9a8a06SStefano Brivio return 0; 56531e47b483SStefano Brivio } 56541e47b483SStefano Brivio count++; 56551e47b483SStefano Brivio } 56561e47b483SStefano Brivio } 56571e47b483SStefano Brivio 56581e47b483SStefano Brivio if (filter->dump_exceptions) { 56591e47b483SStefano Brivio struct fib6_nh_exception_dump_walker w = { .dump = arg, 56601e47b483SStefano Brivio .rt = rt, 56611e47b483SStefano Brivio .flags = flags, 56621e47b483SStefano Brivio .skip = skip, 56631e47b483SStefano Brivio .count = 0 }; 56641e47b483SStefano Brivio int err; 56651e47b483SStefano Brivio 56663b525691SEric Dumazet rcu_read_lock(); 56671e47b483SStefano Brivio if (rt->nh) { 56681e47b483SStefano Brivio err = nexthop_for_each_fib6_nh(rt->nh, 56691e47b483SStefano Brivio rt6_nh_dump_exceptions, 56701e47b483SStefano Brivio &w); 56711e47b483SStefano Brivio } else { 56721e47b483SStefano Brivio err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); 56731e47b483SStefano Brivio } 56743b525691SEric Dumazet rcu_read_unlock(); 56751e47b483SStefano Brivio 56761e47b483SStefano Brivio if (err) 56771e47b483SStefano Brivio return count += w.count; 56781e47b483SStefano Brivio } 5679bf9a8a06SStefano Brivio 5680bf9a8a06SStefano Brivio return -1; 56811da177e4SLinus Torvalds } 56821da177e4SLinus Torvalds 56830eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 56840eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 56850eff0a27SJakub Kicinski struct nlattr **tb, 56860eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 56870eff0a27SJakub Kicinski { 56880eff0a27SJakub Kicinski struct rtmsg *rtm; 56890eff0a27SJakub Kicinski int i, err; 56900eff0a27SJakub Kicinski 56910eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 56920eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 56930eff0a27SJakub Kicinski "Invalid header for get route request"); 56940eff0a27SJakub Kicinski return -EINVAL; 56950eff0a27SJakub Kicinski } 56960eff0a27SJakub Kicinski 56970eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 56988cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 56990eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 57000eff0a27SJakub Kicinski 57010eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 57020eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 57030eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 57040eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 57050eff0a27SJakub Kicinski rtm->rtm_type) { 57060eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 57070eff0a27SJakub Kicinski return -EINVAL; 57080eff0a27SJakub Kicinski } 57090eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 57100eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 57110eff0a27SJakub Kicinski "Invalid flags for get route request"); 57120eff0a27SJakub Kicinski return -EINVAL; 57130eff0a27SJakub Kicinski } 57140eff0a27SJakub Kicinski 57158cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 57160eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 57170eff0a27SJakub Kicinski if (err) 57180eff0a27SJakub Kicinski return err; 57190eff0a27SJakub Kicinski 57200eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 57210eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 57220eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 57230eff0a27SJakub Kicinski return -EINVAL; 57240eff0a27SJakub Kicinski } 57250eff0a27SJakub Kicinski 57260eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 57270eff0a27SJakub Kicinski if (!tb[i]) 57280eff0a27SJakub Kicinski continue; 57290eff0a27SJakub Kicinski 57300eff0a27SJakub Kicinski switch (i) { 57310eff0a27SJakub Kicinski case RTA_SRC: 57320eff0a27SJakub Kicinski case RTA_DST: 57330eff0a27SJakub Kicinski case RTA_IIF: 57340eff0a27SJakub Kicinski case RTA_OIF: 57350eff0a27SJakub Kicinski case RTA_MARK: 57360eff0a27SJakub Kicinski case RTA_UID: 57370eff0a27SJakub Kicinski case RTA_SPORT: 57380eff0a27SJakub Kicinski case RTA_DPORT: 57390eff0a27SJakub Kicinski case RTA_IP_PROTO: 57400eff0a27SJakub Kicinski break; 57410eff0a27SJakub Kicinski default: 57420eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 57430eff0a27SJakub Kicinski return -EINVAL; 57440eff0a27SJakub Kicinski } 57450eff0a27SJakub Kicinski } 57460eff0a27SJakub Kicinski 57470eff0a27SJakub Kicinski return 0; 57480eff0a27SJakub Kicinski } 57490eff0a27SJakub Kicinski 5750c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5751c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 57521da177e4SLinus Torvalds { 57533b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 5754ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 575518c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 5756a68886a6SDavid Ahern struct fib6_info *from; 575718c3a61cSRoopa Prabhu struct dst_entry *dst; 57581da177e4SLinus Torvalds struct rt6_info *rt; 5759ab364a6fSThomas Graf struct sk_buff *skb; 5760ab364a6fSThomas Graf struct rtmsg *rtm; 5761744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 576218c3a61cSRoopa Prabhu bool fibmatch; 5763ab364a6fSThomas Graf 57640eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 5765ab364a6fSThomas Graf if (err < 0) 5766ab364a6fSThomas Graf goto errout; 5767ab364a6fSThomas Graf 5768ab364a6fSThomas Graf err = -EINVAL; 576938b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 577038b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 577118c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 5772ab364a6fSThomas Graf 5773ab364a6fSThomas Graf if (tb[RTA_SRC]) { 5774ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 5775ab364a6fSThomas Graf goto errout; 5776ab364a6fSThomas Graf 57774e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 5778ab364a6fSThomas Graf } 5779ab364a6fSThomas Graf 5780ab364a6fSThomas Graf if (tb[RTA_DST]) { 5781ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 5782ab364a6fSThomas Graf goto errout; 5783ab364a6fSThomas Graf 57844e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 5785ab364a6fSThomas Graf } 5786ab364a6fSThomas Graf 5787ab364a6fSThomas Graf if (tb[RTA_IIF]) 5788ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 5789ab364a6fSThomas Graf 5790ab364a6fSThomas Graf if (tb[RTA_OIF]) 579172331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 5792ab364a6fSThomas Graf 57932e47b291SLorenzo Colitti if (tb[RTA_MARK]) 57942e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 57952e47b291SLorenzo Colitti 5796622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 5797622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 5798622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 5799622ec2c9SLorenzo Colitti else 5800622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 5801622ec2c9SLorenzo Colitti 5802eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 5803eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 5804eacb9384SRoopa Prabhu 5805eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 5806eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 5807eacb9384SRoopa Prabhu 5808eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 5809eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 58105e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 58115e1a99eaSHangbin Liu extack); 5812eacb9384SRoopa Prabhu if (err) 5813eacb9384SRoopa Prabhu goto errout; 5814eacb9384SRoopa Prabhu } 5815eacb9384SRoopa Prabhu 5816ab364a6fSThomas Graf if (iif) { 5817ab364a6fSThomas Graf struct net_device *dev; 581872331bc0SShmulik Ladkani int flags = 0; 581972331bc0SShmulik Ladkani 5820121622dbSFlorian Westphal rcu_read_lock(); 5821121622dbSFlorian Westphal 5822121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 5823ab364a6fSThomas Graf if (!dev) { 5824121622dbSFlorian Westphal rcu_read_unlock(); 5825ab364a6fSThomas Graf err = -ENODEV; 5826ab364a6fSThomas Graf goto errout; 5827ab364a6fSThomas Graf } 582872331bc0SShmulik Ladkani 582972331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 583072331bc0SShmulik Ladkani 583172331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 583272331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 583372331bc0SShmulik Ladkani 5834b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 5835121622dbSFlorian Westphal 5836121622dbSFlorian Westphal rcu_read_unlock(); 583772331bc0SShmulik Ladkani } else { 583872331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 583972331bc0SShmulik Ladkani 584018c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 584118c3a61cSRoopa Prabhu } 584218c3a61cSRoopa Prabhu 584318c3a61cSRoopa Prabhu 584418c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 584518c3a61cSRoopa Prabhu if (rt->dst.error) { 584618c3a61cSRoopa Prabhu err = rt->dst.error; 584718c3a61cSRoopa Prabhu ip6_rt_put(rt); 584818c3a61cSRoopa Prabhu goto errout; 5849ab364a6fSThomas Graf } 58501da177e4SLinus Torvalds 58519d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 58529d6acb3bSWANG Cong err = rt->dst.error; 58539d6acb3bSWANG Cong ip6_rt_put(rt); 58549d6acb3bSWANG Cong goto errout; 58559d6acb3bSWANG Cong } 58569d6acb3bSWANG Cong 58571da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 585838308473SDavid S. Miller if (!skb) { 585994e187c0SAmerigo Wang ip6_rt_put(rt); 5860ab364a6fSThomas Graf err = -ENOBUFS; 5861ab364a6fSThomas Graf goto errout; 5862ab364a6fSThomas Graf } 58631da177e4SLinus Torvalds 5864d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 5865a68886a6SDavid Ahern 5866a68886a6SDavid Ahern rcu_read_lock(); 5867a68886a6SDavid Ahern from = rcu_dereference(rt->from); 5868886b7a50SMartin KaFai Lau if (from) { 586918c3a61cSRoopa Prabhu if (fibmatch) 5870886b7a50SMartin KaFai Lau err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 5871886b7a50SMartin KaFai Lau iif, RTM_NEWROUTE, 5872886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 587318c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 587418c3a61cSRoopa Prabhu else 5875a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 5876a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 5877886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 5878886b7a50SMartin KaFai Lau nlh->nlmsg_seq, 0); 5879886b7a50SMartin KaFai Lau } else { 5880886b7a50SMartin KaFai Lau err = -ENETUNREACH; 5881886b7a50SMartin KaFai Lau } 5882a68886a6SDavid Ahern rcu_read_unlock(); 5883a68886a6SDavid Ahern 58841da177e4SLinus Torvalds if (err < 0) { 5885ab364a6fSThomas Graf kfree_skb(skb); 5886ab364a6fSThomas Graf goto errout; 58871da177e4SLinus Torvalds } 58881da177e4SLinus Torvalds 588915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 5890ab364a6fSThomas Graf errout: 58911da177e4SLinus Torvalds return err; 58921da177e4SLinus Torvalds } 58931da177e4SLinus Torvalds 58948d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 589537a1d361SRoopa Prabhu unsigned int nlm_flags) 58961da177e4SLinus Torvalds { 58971da177e4SLinus Torvalds struct sk_buff *skb; 58985578689aSDaniel Lezcano struct net *net = info->nl_net; 5899528c4cebSDenis V. Lunev u32 seq; 5900528c4cebSDenis V. Lunev int err; 59010d51aa80SJamal Hadi Salim 5902528c4cebSDenis V. Lunev err = -ENOBUFS; 590338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 590486872cb5SThomas Graf 590519e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 590638308473SDavid S. Miller if (!skb) 590721713ebcSThomas Graf goto errout; 59081da177e4SLinus Torvalds 5909d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 5910f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 591126932566SPatrick McHardy if (err < 0) { 591226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 591326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 591426932566SPatrick McHardy kfree_skb(skb); 591526932566SPatrick McHardy goto errout; 591626932566SPatrick McHardy } 591715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 59185578689aSDaniel Lezcano info->nlh, gfp_any()); 59191ce85fe4SPablo Neira Ayuso return; 592021713ebcSThomas Graf errout: 592121713ebcSThomas Graf if (err < 0) 59225578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 59231da177e4SLinus Torvalds } 59241da177e4SLinus Torvalds 592519a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt, 592619a3b7eeSDavid Ahern struct nl_info *info) 592719a3b7eeSDavid Ahern { 592819a3b7eeSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 592919a3b7eeSDavid Ahern struct sk_buff *skb; 593019a3b7eeSDavid Ahern int err = -ENOBUFS; 593119a3b7eeSDavid Ahern 593219a3b7eeSDavid Ahern /* call_fib6_entry_notifiers will be removed when in-kernel notifier 593319a3b7eeSDavid Ahern * is implemented and supported for nexthop objects 593419a3b7eeSDavid Ahern */ 593519a3b7eeSDavid Ahern call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); 593619a3b7eeSDavid Ahern 593719a3b7eeSDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 593819a3b7eeSDavid Ahern if (!skb) 593919a3b7eeSDavid Ahern goto errout; 594019a3b7eeSDavid Ahern 594119a3b7eeSDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 594219a3b7eeSDavid Ahern RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 594319a3b7eeSDavid Ahern if (err < 0) { 594419a3b7eeSDavid Ahern /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 594519a3b7eeSDavid Ahern WARN_ON(err == -EMSGSIZE); 594619a3b7eeSDavid Ahern kfree_skb(skb); 594719a3b7eeSDavid Ahern goto errout; 594819a3b7eeSDavid Ahern } 594919a3b7eeSDavid Ahern rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 595019a3b7eeSDavid Ahern info->nlh, gfp_any()); 595119a3b7eeSDavid Ahern return; 595219a3b7eeSDavid Ahern errout: 595319a3b7eeSDavid Ahern if (err < 0) 595419a3b7eeSDavid Ahern rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 595519a3b7eeSDavid Ahern } 595619a3b7eeSDavid Ahern 59578ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 5958351638e7SJiri Pirko unsigned long event, void *ptr) 59598ed67789SDaniel Lezcano { 5960351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5961c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 59628ed67789SDaniel Lezcano 5963242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 5964242d3a49SWANG Cong return NOTIFY_OK; 5965242d3a49SWANG Cong 5966242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 59671cf844c7SDavid Ahern net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; 5968d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 59698ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 59708ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5971d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 59728ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 5973d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 59748ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 59758ed67789SDaniel Lezcano #endif 597676da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 597776da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 597876da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 597976da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 598076da0704SWANG Cong */ 598112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 5982242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 598312d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 598412d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 5985242d3a49SWANG Cong #endif 59868ed67789SDaniel Lezcano } 59878ed67789SDaniel Lezcano 59888ed67789SDaniel Lezcano return NOTIFY_OK; 59898ed67789SDaniel Lezcano } 59908ed67789SDaniel Lezcano 59911da177e4SLinus Torvalds /* 59921da177e4SLinus Torvalds * /proc 59931da177e4SLinus Torvalds */ 59941da177e4SLinus Torvalds 59951da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 59961da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 59971da177e4SLinus Torvalds { 599869ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 59991da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 600069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 600169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 600281eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 600369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 600469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 6005fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 600669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 60071da177e4SLinus Torvalds 60081da177e4SLinus Torvalds return 0; 60091da177e4SLinus Torvalds } 60101da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 60111da177e4SLinus Torvalds 60121da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 60131da177e4SLinus Torvalds 60141da177e4SLinus Torvalds static 6015fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 60161da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 60171da177e4SLinus Torvalds { 6018c486da34SLucian Adrian Grijincu struct net *net; 6019c486da34SLucian Adrian Grijincu int delay; 6020f0fb9b28SAditya Pakki int ret; 6021c486da34SLucian Adrian Grijincu if (!write) 6022c486da34SLucian Adrian Grijincu return -EINVAL; 6023c486da34SLucian Adrian Grijincu 6024c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 6025c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 6026f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6027f0fb9b28SAditya Pakki if (ret) 6028f0fb9b28SAditya Pakki return ret; 6029f0fb9b28SAditya Pakki 60302ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 60311da177e4SLinus Torvalds return 0; 60321da177e4SLinus Torvalds } 60331da177e4SLinus Torvalds 60347c6bb7d2SDavid Ahern static int zero; 60357c6bb7d2SDavid Ahern static int one = 1; 60367c6bb7d2SDavid Ahern 6037ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 60381da177e4SLinus Torvalds { 60391da177e4SLinus Torvalds .procname = "flush", 60404990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 60411da177e4SLinus Torvalds .maxlen = sizeof(int), 604289c8b3a1SDave Jones .mode = 0200, 60436d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 60441da177e4SLinus Torvalds }, 60451da177e4SLinus Torvalds { 60461da177e4SLinus Torvalds .procname = "gc_thresh", 60479a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 60481da177e4SLinus Torvalds .maxlen = sizeof(int), 60491da177e4SLinus Torvalds .mode = 0644, 60506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60511da177e4SLinus Torvalds }, 60521da177e4SLinus Torvalds { 60531da177e4SLinus Torvalds .procname = "max_size", 60544990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 60551da177e4SLinus Torvalds .maxlen = sizeof(int), 60561da177e4SLinus Torvalds .mode = 0644, 60576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60581da177e4SLinus Torvalds }, 60591da177e4SLinus Torvalds { 60601da177e4SLinus Torvalds .procname = "gc_min_interval", 60614990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 60621da177e4SLinus Torvalds .maxlen = sizeof(int), 60631da177e4SLinus Torvalds .mode = 0644, 60646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60651da177e4SLinus Torvalds }, 60661da177e4SLinus Torvalds { 60671da177e4SLinus Torvalds .procname = "gc_timeout", 60684990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 60691da177e4SLinus Torvalds .maxlen = sizeof(int), 60701da177e4SLinus Torvalds .mode = 0644, 60716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60721da177e4SLinus Torvalds }, 60731da177e4SLinus Torvalds { 60741da177e4SLinus Torvalds .procname = "gc_interval", 60754990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 60761da177e4SLinus Torvalds .maxlen = sizeof(int), 60771da177e4SLinus Torvalds .mode = 0644, 60786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60791da177e4SLinus Torvalds }, 60801da177e4SLinus Torvalds { 60811da177e4SLinus Torvalds .procname = "gc_elasticity", 60824990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 60831da177e4SLinus Torvalds .maxlen = sizeof(int), 60841da177e4SLinus Torvalds .mode = 0644, 6085f3d3f616SMin Zhang .proc_handler = proc_dointvec, 60861da177e4SLinus Torvalds }, 60871da177e4SLinus Torvalds { 60881da177e4SLinus Torvalds .procname = "mtu_expires", 60894990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 60901da177e4SLinus Torvalds .maxlen = sizeof(int), 60911da177e4SLinus Torvalds .mode = 0644, 60926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60931da177e4SLinus Torvalds }, 60941da177e4SLinus Torvalds { 60951da177e4SLinus Torvalds .procname = "min_adv_mss", 60964990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 60971da177e4SLinus Torvalds .maxlen = sizeof(int), 60981da177e4SLinus Torvalds .mode = 0644, 6099f3d3f616SMin Zhang .proc_handler = proc_dointvec, 61001da177e4SLinus Torvalds }, 61011da177e4SLinus Torvalds { 61021da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 61034990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 61041da177e4SLinus Torvalds .maxlen = sizeof(int), 61051da177e4SLinus Torvalds .mode = 0644, 61066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 61071da177e4SLinus Torvalds }, 61087c6bb7d2SDavid Ahern { 61097c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 61107c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 61117c6bb7d2SDavid Ahern .maxlen = sizeof(int), 61127c6bb7d2SDavid Ahern .mode = 0644, 6113b8e8a863SEiichi Tsukata .proc_handler = proc_dointvec_minmax, 61147c6bb7d2SDavid Ahern .extra1 = &zero, 61157c6bb7d2SDavid Ahern .extra2 = &one, 61167c6bb7d2SDavid Ahern }, 6117f8572d8fSEric W. Biederman { } 61181da177e4SLinus Torvalds }; 61191da177e4SLinus Torvalds 61202c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 6121760f2d01SDaniel Lezcano { 6122760f2d01SDaniel Lezcano struct ctl_table *table; 6123760f2d01SDaniel Lezcano 6124760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 6125760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 6126760f2d01SDaniel Lezcano GFP_KERNEL); 61275ee09105SYOSHIFUJI Hideaki 61285ee09105SYOSHIFUJI Hideaki if (table) { 61295ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 6130c486da34SLucian Adrian Grijincu table[0].extra1 = net; 613186393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 61325ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 61335ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 61345ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 61355ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 61365ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 61375ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 61385ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 61399c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 61407c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 6141464dc801SEric W. Biederman 6142464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 6143464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 6144464dc801SEric W. Biederman table[0].procname = NULL; 61455ee09105SYOSHIFUJI Hideaki } 61465ee09105SYOSHIFUJI Hideaki 6147760f2d01SDaniel Lezcano return table; 6148760f2d01SDaniel Lezcano } 61491da177e4SLinus Torvalds #endif 61501da177e4SLinus Torvalds 61512c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 6152cdb18761SDaniel Lezcano { 6153633d424bSPavel Emelyanov int ret = -ENOMEM; 61548ed67789SDaniel Lezcano 615586393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 615686393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 6157f2fc6a54SBenjamin Thery 6158fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 6159fc66f95cSEric Dumazet goto out_ip6_dst_ops; 6160fc66f95cSEric Dumazet 61611cf844c7SDavid Ahern net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); 6162421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 6163421842edSDavid Ahern goto out_ip6_dst_entries; 61641cf844c7SDavid Ahern memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, 61651cf844c7SDavid Ahern sizeof(*net->ipv6.fib6_null_entry)); 6166421842edSDavid Ahern 61678ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 61688ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 61698ed67789SDaniel Lezcano GFP_KERNEL); 61708ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 6171421842edSDavid Ahern goto out_fib6_null_entry; 6172d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 617362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 617462fa8a84SDavid S. Miller ip6_template_metrics, true); 617574109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached); 61768ed67789SDaniel Lezcano 61778ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6178feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 61798ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 61808ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 61818ed67789SDaniel Lezcano GFP_KERNEL); 618268fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 618368fffc67SPeter Zijlstra goto out_ip6_null_entry; 6184d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 618562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 618662fa8a84SDavid S. Miller ip6_template_metrics, true); 618774109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached); 61888ed67789SDaniel Lezcano 61898ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 61908ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 61918ed67789SDaniel Lezcano GFP_KERNEL); 619268fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 619368fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 6194d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 619562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 619662fa8a84SDavid S. Miller ip6_template_metrics, true); 619774109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached); 61988ed67789SDaniel Lezcano #endif 61998ed67789SDaniel Lezcano 6200b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 6201b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 6202b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 6203b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 6204b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 6205b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 6206b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 6207b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 62087c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 6209b339a47cSPeter Zijlstra 62106891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 62116891a346SBenjamin Thery 62128ed67789SDaniel Lezcano ret = 0; 62138ed67789SDaniel Lezcano out: 62148ed67789SDaniel Lezcano return ret; 6215f2fc6a54SBenjamin Thery 621668fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 621768fffc67SPeter Zijlstra out_ip6_prohibit_entry: 621868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 621968fffc67SPeter Zijlstra out_ip6_null_entry: 622068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 622168fffc67SPeter Zijlstra #endif 6222421842edSDavid Ahern out_fib6_null_entry: 6223421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 6224fc66f95cSEric Dumazet out_ip6_dst_entries: 6225fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6226f2fc6a54SBenjamin Thery out_ip6_dst_ops: 6227f2fc6a54SBenjamin Thery goto out; 6228cdb18761SDaniel Lezcano } 6229cdb18761SDaniel Lezcano 62302c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 6231cdb18761SDaniel Lezcano { 6232421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 62338ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 62348ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 62358ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 62368ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 62378ed67789SDaniel Lezcano #endif 623841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6239cdb18761SDaniel Lezcano } 6240cdb18761SDaniel Lezcano 6241d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 6242d189634eSThomas Graf { 6243d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6244c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 6245c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 62463617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 62473617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 6248d189634eSThomas Graf #endif 6249d189634eSThomas Graf return 0; 6250d189634eSThomas Graf } 6251d189634eSThomas Graf 6252d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 6253d189634eSThomas Graf { 6254d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6255ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 6256ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 6257d189634eSThomas Graf #endif 6258d189634eSThomas Graf } 6259d189634eSThomas Graf 6260cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 6261cdb18761SDaniel Lezcano .init = ip6_route_net_init, 6262cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 6263cdb18761SDaniel Lezcano }; 6264cdb18761SDaniel Lezcano 6265c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 6266c3426b47SDavid S. Miller { 6267c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 6268c3426b47SDavid S. Miller 6269c3426b47SDavid S. Miller if (!bp) 6270c3426b47SDavid S. Miller return -ENOMEM; 6271c3426b47SDavid S. Miller inet_peer_base_init(bp); 6272c3426b47SDavid S. Miller net->ipv6.peers = bp; 6273c3426b47SDavid S. Miller return 0; 6274c3426b47SDavid S. Miller } 6275c3426b47SDavid S. Miller 6276c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 6277c3426b47SDavid S. Miller { 6278c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 6279c3426b47SDavid S. Miller 6280c3426b47SDavid S. Miller net->ipv6.peers = NULL; 628156a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 6282c3426b47SDavid S. Miller kfree(bp); 6283c3426b47SDavid S. Miller } 6284c3426b47SDavid S. Miller 62852b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 6286c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 6287c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 6288c3426b47SDavid S. Miller }; 6289c3426b47SDavid S. Miller 6290d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 6291d189634eSThomas Graf .init = ip6_route_net_init_late, 6292d189634eSThomas Graf .exit = ip6_route_net_exit_late, 6293d189634eSThomas Graf }; 6294d189634eSThomas Graf 62958ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 62968ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 6297242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 62988ed67789SDaniel Lezcano }; 62998ed67789SDaniel Lezcano 63002f460933SWANG Cong void __init ip6_route_init_special_entries(void) 63012f460933SWANG Cong { 63022f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 63032f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 63042f460933SWANG Cong * manually for init_net */ 63051cf844c7SDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; 63062f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 63072f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 63082f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 63092f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 63102f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 63112f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 63122f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 63132f460933SWANG Cong #endif 63142f460933SWANG Cong } 63152f460933SWANG Cong 6316433d49c3SDaniel Lezcano int __init ip6_route_init(void) 63171da177e4SLinus Torvalds { 6318433d49c3SDaniel Lezcano int ret; 63198d0b94afSMartin KaFai Lau int cpu; 6320433d49c3SDaniel Lezcano 63219a7ec3a9SDaniel Lezcano ret = -ENOMEM; 63229a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 63239a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 63249a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 63259a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 6326c19a28e1SFernando Carrijo goto out; 632714e50e57SDavid S. Miller 6328fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 63298ed67789SDaniel Lezcano if (ret) 6330bdb3289fSDaniel Lezcano goto out_kmem_cache; 6331bdb3289fSDaniel Lezcano 6332c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 6333c3426b47SDavid S. Miller if (ret) 6334e8803b6cSDavid S. Miller goto out_dst_entries; 63352a0c451aSThomas Graf 63367e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 63377e52b33bSDavid S. Miller if (ret) 63387e52b33bSDavid S. Miller goto out_register_inetpeer; 6339c3426b47SDavid S. Miller 63405dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 63415dc121e9SArnaud Ebalard 6342e8803b6cSDavid S. Miller ret = fib6_init(); 6343433d49c3SDaniel Lezcano if (ret) 63448ed67789SDaniel Lezcano goto out_register_subsys; 6345433d49c3SDaniel Lezcano 6346433d49c3SDaniel Lezcano ret = xfrm6_init(); 6347433d49c3SDaniel Lezcano if (ret) 6348e8803b6cSDavid S. Miller goto out_fib6_init; 6349c35b7e72SDaniel Lezcano 6350433d49c3SDaniel Lezcano ret = fib6_rules_init(); 6351433d49c3SDaniel Lezcano if (ret) 6352433d49c3SDaniel Lezcano goto xfrm6_init; 63537e5449c2SDaniel Lezcano 6354d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 6355d189634eSThomas Graf if (ret) 6356d189634eSThomas Graf goto fib6_rules_init; 6357d189634eSThomas Graf 635816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 635916feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 636016feebcfSFlorian Westphal if (ret < 0) 636116feebcfSFlorian Westphal goto out_register_late_subsys; 636216feebcfSFlorian Westphal 636316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 636416feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 636516feebcfSFlorian Westphal if (ret < 0) 636616feebcfSFlorian Westphal goto out_register_late_subsys; 636716feebcfSFlorian Westphal 636816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 636916feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 637016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 637116feebcfSFlorian Westphal if (ret < 0) 6372d189634eSThomas Graf goto out_register_late_subsys; 6373433d49c3SDaniel Lezcano 63748ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 6375cdb18761SDaniel Lezcano if (ret) 6376d189634eSThomas Graf goto out_register_late_subsys; 63778ed67789SDaniel Lezcano 63788d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 63798d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 63808d0b94afSMartin KaFai Lau 63818d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 63828d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 63838d0b94afSMartin KaFai Lau } 63848d0b94afSMartin KaFai Lau 6385433d49c3SDaniel Lezcano out: 6386433d49c3SDaniel Lezcano return ret; 6387433d49c3SDaniel Lezcano 6388d189634eSThomas Graf out_register_late_subsys: 638916feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6390d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6391433d49c3SDaniel Lezcano fib6_rules_init: 6392433d49c3SDaniel Lezcano fib6_rules_cleanup(); 6393433d49c3SDaniel Lezcano xfrm6_init: 6394433d49c3SDaniel Lezcano xfrm6_fini(); 63952a0c451aSThomas Graf out_fib6_init: 63962a0c451aSThomas Graf fib6_gc_cleanup(); 63978ed67789SDaniel Lezcano out_register_subsys: 63988ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 63997e52b33bSDavid S. Miller out_register_inetpeer: 64007e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 6401fc66f95cSEric Dumazet out_dst_entries: 6402fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 6403433d49c3SDaniel Lezcano out_kmem_cache: 6404f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6405433d49c3SDaniel Lezcano goto out; 64061da177e4SLinus Torvalds } 64071da177e4SLinus Torvalds 64081da177e4SLinus Torvalds void ip6_route_cleanup(void) 64091da177e4SLinus Torvalds { 64108ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 6411d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6412101367c2SThomas Graf fib6_rules_cleanup(); 64131da177e4SLinus Torvalds xfrm6_fini(); 64141da177e4SLinus Torvalds fib6_gc_cleanup(); 6415c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 64168ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 641741bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 6418f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 64191da177e4SLinus Torvalds } 6420