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, 98bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 99bd085ef6SHangbin Liu bool confirm_neigh); 1006700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 1016700c270SDavid S. Miller struct sk_buff *skb); 102702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 103702cea56SDavid Ahern int strict); 104a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i); 105d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1068d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 107d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 10816a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10916a16cd3SDavid Ahern unsigned int flags); 1107e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 111510e2cedSWei Wang const struct in6_addr *daddr, 112510e2cedSWei Wang const struct in6_addr *saddr); 1131da177e4SLinus Torvalds 11470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1158d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 116b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 117830218c1SDavid Ahern const struct in6_addr *gwaddr, 118830218c1SDavid Ahern struct net_device *dev, 11995c96174SEric Dumazet unsigned int pref); 1208d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 121b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 122830218c1SDavid Ahern const struct in6_addr *gwaddr, 123830218c1SDavid Ahern struct net_device *dev); 12470ceb4f5SYOSHIFUJI Hideaki #endif 12570ceb4f5SYOSHIFUJI Hideaki 1268d0b94afSMartin KaFai Lau struct uncached_list { 1278d0b94afSMartin KaFai Lau spinlock_t lock; 1288d0b94afSMartin KaFai Lau struct list_head head; 1298d0b94afSMartin KaFai Lau }; 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1328d0b94afSMartin KaFai Lau 133510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1348d0b94afSMartin KaFai Lau { 1358d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1388d0b94afSMartin KaFai Lau 1398d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1408d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1418d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1428d0b94afSMartin KaFai Lau } 1438d0b94afSMartin KaFai Lau 144510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1458d0b94afSMartin KaFai Lau { 1468d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1478d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 14881eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1498d0b94afSMartin KaFai Lau 1508d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1518d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15281eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1538d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1548d0b94afSMartin KaFai Lau } 1558d0b94afSMartin KaFai Lau } 1568d0b94afSMartin KaFai Lau 1578d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1588d0b94afSMartin KaFai Lau { 1598d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1608d0b94afSMartin KaFai Lau int cpu; 1618d0b94afSMartin KaFai Lau 162e332bc67SEric W. Biederman if (dev == loopback_dev) 163e332bc67SEric W. Biederman return; 164e332bc67SEric W. Biederman 1658d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1668d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1678d0b94afSMartin KaFai Lau struct rt6_info *rt; 1688d0b94afSMartin KaFai Lau 1698d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1708d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1718d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1728d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1738d0b94afSMartin KaFai Lau 174e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1758d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1768d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1778d0b94afSMartin KaFai Lau } 1788d0b94afSMartin KaFai Lau 179e332bc67SEric W. Biederman if (rt_dev == dev) { 1808d7017fdSMahesh Bandewar rt->dst.dev = blackhole_netdev; 1818d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1828d0b94afSMartin KaFai Lau dev_put(rt_dev); 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau } 1858d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1868d0b94afSMartin KaFai Lau } 1878d0b94afSMartin KaFai Lau } 1888d0b94afSMartin KaFai Lau 189f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 190f894cbf8SDavid S. Miller struct sk_buff *skb, 191f894cbf8SDavid S. Miller const void *daddr) 19239232973SDavid S. Miller { 193a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19439232973SDavid S. Miller return (const void *) p; 195f894cbf8SDavid S. Miller else if (skb) 196f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 19739232973SDavid S. Miller return daddr; 19839232973SDavid S. Miller } 19939232973SDavid S. Miller 200f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 201f8a1b43bSDavid Ahern struct net_device *dev, 202f894cbf8SDavid S. Miller struct sk_buff *skb, 203f894cbf8SDavid S. Miller const void *daddr) 204d3aaeb38SDavid S. Miller { 20539232973SDavid S. Miller struct neighbour *n; 20639232973SDavid S. Miller 207f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 208f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 209f83c7790SDavid S. Miller if (n) 210f83c7790SDavid S. Miller return n; 2117adf3246SStefano Brivio 2127adf3246SStefano Brivio n = neigh_create(&nd_tbl, daddr, dev); 2137adf3246SStefano Brivio return IS_ERR(n) ? NULL : n; 214f8a1b43bSDavid Ahern } 215f8a1b43bSDavid Ahern 216f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 217f8a1b43bSDavid Ahern struct sk_buff *skb, 218f8a1b43bSDavid Ahern const void *daddr) 219f8a1b43bSDavid Ahern { 220f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 221f8a1b43bSDavid Ahern 2222c6b55f4SNicolas Dichtel return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), 2232c6b55f4SNicolas Dichtel dst->dev, skb, daddr); 224f83c7790SDavid S. Miller } 225f83c7790SDavid S. Miller 22663fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22763fca65dSJulian Anastasov { 22863fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22963fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23063fca65dSJulian Anastasov 231cbfd6891SStefano Brivio daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr); 23263fca65dSJulian Anastasov if (!daddr) 23363fca65dSJulian Anastasov return; 23463fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23563fca65dSJulian Anastasov return; 23663fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23763fca65dSJulian Anastasov return; 23863fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23963fca65dSJulian Anastasov } 24063fca65dSJulian Anastasov 2419a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2421da177e4SLinus Torvalds .family = AF_INET6, 2431da177e4SLinus Torvalds .gc = ip6_dst_gc, 2441da177e4SLinus Torvalds .gc_thresh = 1024, 2451da177e4SLinus Torvalds .check = ip6_dst_check, 2460dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 247ebb762f2SSteffen Klassert .mtu = ip6_mtu, 248d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2491da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2501da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2511da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2521da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2531da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2546e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2559f8955ccSEric W. Biederman .local_out = __ip6_local_out, 256f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 25763fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2581da177e4SLinus Torvalds }; 2591da177e4SLinus Torvalds 260ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 261ec831ea7SRoland Dreier { 262618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 263618f9bc7SSteffen Klassert 264618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 265ec831ea7SRoland Dreier } 266ec831ea7SRoland Dreier 2676700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 268bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 269bd085ef6SHangbin Liu bool confirm_neigh) 27014e50e57SDavid S. Miller { 27114e50e57SDavid S. Miller } 27214e50e57SDavid S. Miller 2736700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2746700c270SDavid S. Miller struct sk_buff *skb) 275b587ee3bSDavid S. Miller { 276b587ee3bSDavid S. Miller } 277b587ee3bSDavid S. Miller 27814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27914e50e57SDavid S. Miller .family = AF_INET6, 28014e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28114e50e57SDavid S. Miller .check = ip6_dst_check, 282ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 283214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28414e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 285b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2860a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 287f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 28814e50e57SDavid S. Miller }; 28914e50e57SDavid S. Miller 29062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29114edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29262fa8a84SDavid S. Miller }; 29362fa8a84SDavid S. Miller 2948d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 29593c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 29693c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 29793c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 298f05713e0SEric Dumazet .fib6_ref = REFCOUNT_INIT(1), 299421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 300421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 301421842edSDavid Ahern }; 302421842edSDavid Ahern 303fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 3041da177e4SLinus Torvalds .dst = { 3051da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3061da177e4SLinus Torvalds .__use = 1, 3072c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3081da177e4SLinus Torvalds .error = -ENETUNREACH, 3091da177e4SLinus Torvalds .input = ip6_pkt_discard, 3101da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3111da177e4SLinus Torvalds }, 3121da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3131da177e4SLinus Torvalds }; 3141da177e4SLinus Torvalds 315101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 316101367c2SThomas Graf 317fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 318101367c2SThomas Graf .dst = { 319101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 320101367c2SThomas Graf .__use = 1, 3212c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 322101367c2SThomas Graf .error = -EACCES, 3239ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3249ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 325101367c2SThomas Graf }, 326101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 327101367c2SThomas Graf }; 328101367c2SThomas Graf 329fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 330101367c2SThomas Graf .dst = { 331101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 332101367c2SThomas Graf .__use = 1, 3332c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 334101367c2SThomas Graf .error = -EINVAL, 335352e512cSHerbert Xu .input = dst_discard, 336ede2059dSEric W. Biederman .output = dst_discard_out, 337101367c2SThomas Graf }, 338101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 339101367c2SThomas Graf }; 340101367c2SThomas Graf 341101367c2SThomas Graf #endif 342101367c2SThomas Graf 343ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 344ebfa45f0SMartin KaFai Lau { 345ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 346ebfa45f0SMartin KaFai Lau 347ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 348ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 349ebfa45f0SMartin KaFai Lau } 350ebfa45f0SMartin KaFai Lau 3511da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 35293531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 353ad706862SMartin KaFai Lau int flags) 3541da177e4SLinus Torvalds { 35597bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 356b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 357cf911662SDavid S. Miller 35881eb8447SWei Wang if (rt) { 359ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 36081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 36181eb8447SWei Wang } 3628104891bSSteffen Klassert 363cf911662SDavid S. Miller return rt; 3641da177e4SLinus Torvalds } 3659ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 366d52d3997SMartin KaFai Lau 3671da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 370a68886a6SDavid Ahern struct fib6_info *from; 3718d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3721da177e4SLinus Torvalds 3731620a336SDavid Ahern ip_dst_metrics_put(dst); 3748d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3758d0b94afSMartin KaFai Lau 3768d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37738308473SDavid S. Miller if (idev) { 3781da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3791da177e4SLinus Torvalds in6_dev_put(idev); 3801da177e4SLinus Torvalds } 3811716a961SGao feng 3820e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt->from, NULL); 38393531c67SDavid Ahern fib6_info_release(from); 384b3419363SDavid S. Miller } 385b3419363SDavid S. Miller 3861da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3871da177e4SLinus Torvalds int how) 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3901da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3915a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 392c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3931da177e4SLinus Torvalds 394e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 395e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 39638308473SDavid S. Miller if (loopback_idev) { 3971da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3981da177e4SLinus Torvalds in6_dev_put(idev); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds } 40197cac082SDavid S. Miller } 4021da177e4SLinus Torvalds 4035973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4045973fb1eSMartin KaFai Lau { 4055973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4065973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4075973fb1eSMartin KaFai Lau else 4085973fb1eSMartin KaFai Lau return false; 4095973fb1eSMartin KaFai Lau } 4105973fb1eSMartin KaFai Lau 411a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4121da177e4SLinus Torvalds { 413a68886a6SDavid Ahern struct fib6_info *from; 414a68886a6SDavid Ahern 415a68886a6SDavid Ahern from = rcu_dereference(rt->from); 416a68886a6SDavid Ahern 4171716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4181716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 419a50feda5SEric Dumazet return true; 420a68886a6SDavid Ahern } else if (from) { 4211e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 422a68886a6SDavid Ahern fib6_check_expired(from); 4231716a961SGao feng } 424a50feda5SEric Dumazet return false; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 427b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res, 428b1d40991SDavid Ahern struct flowi6 *fl6, int oif, bool have_oif_match, 429b1d40991SDavid Ahern const struct sk_buff *skb, int strict) 43051ebd318SNicolas Dichtel { 4318d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 432b1d40991SDavid Ahern struct fib6_info *match = res->f6i; 433b1d40991SDavid Ahern 434*34fe5a1cSDavid Ahern if (!match->nh && (!match->fib6_nsiblings || have_oif_match)) 435b1d40991SDavid Ahern goto out; 43651ebd318SNicolas Dichtel 437*34fe5a1cSDavid Ahern if (match->nh && have_oif_match && res->nh) 438*34fe5a1cSDavid Ahern return; 439*34fe5a1cSDavid Ahern 440b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 441b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 442b673d6ccSJakub Sitnicki */ 443f88d8ea6SDavid Ahern if (!fl6->mp_hash && 444f88d8ea6SDavid Ahern (!match->nh || nexthop_is_multipath(match->nh))) 445b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 446b673d6ccSJakub Sitnicki 447f88d8ea6SDavid Ahern if (unlikely(match->nh)) { 448f88d8ea6SDavid Ahern nexthop_path_fib6_result(res, fl6->mp_hash); 449f88d8ea6SDavid Ahern return; 450f88d8ea6SDavid Ahern } 451f88d8ea6SDavid Ahern 4521cf844c7SDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) 453b1d40991SDavid Ahern goto out; 454bbfcd776SIdo Schimmel 45593c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 45693c2fb25SDavid Ahern fib6_siblings) { 4571cf844c7SDavid Ahern const struct fib6_nh *nh = sibling->fib6_nh; 4585e670d84SDavid Ahern int nh_upper_bound; 4595e670d84SDavid Ahern 460702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4615e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4623d709f69SIdo Schimmel continue; 463702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 46452bd4c0cSNicolas Dichtel break; 46551ebd318SNicolas Dichtel match = sibling; 46651ebd318SNicolas Dichtel break; 46751ebd318SNicolas Dichtel } 4683d709f69SIdo Schimmel 469b1d40991SDavid Ahern out: 470b1d40991SDavid Ahern res->f6i = match; 4711cf844c7SDavid Ahern res->nh = match->fib6_nh; 47251ebd318SNicolas Dichtel } 47351ebd318SNicolas Dichtel 4741da177e4SLinus Torvalds /* 47566f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds 4780c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 4790c59d006SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4800c59d006SDavid Ahern { 4810c59d006SDavid Ahern const struct net_device *dev; 4820c59d006SDavid Ahern 4830c59d006SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 4840c59d006SDavid Ahern return false; 4850c59d006SDavid Ahern 4860c59d006SDavid Ahern dev = nh->fib_nh_dev; 4870c59d006SDavid Ahern if (oif) { 4880c59d006SDavid Ahern if (dev->ifindex == oif) 4890c59d006SDavid Ahern return true; 4900c59d006SDavid Ahern } else { 4910c59d006SDavid Ahern if (ipv6_chk_addr(net, saddr, dev, 4920c59d006SDavid Ahern flags & RT6_LOOKUP_F_IFACE)) 4930c59d006SDavid Ahern return true; 4940c59d006SDavid Ahern } 4950c59d006SDavid Ahern 4960c59d006SDavid Ahern return false; 4970c59d006SDavid Ahern } 4980c59d006SDavid Ahern 499962b6803SDavid Ahern struct fib6_nh_dm_arg { 500962b6803SDavid Ahern struct net *net; 501962b6803SDavid Ahern const struct in6_addr *saddr; 502962b6803SDavid Ahern int oif; 503962b6803SDavid Ahern int flags; 504962b6803SDavid Ahern struct fib6_nh *nh; 505962b6803SDavid Ahern }; 506962b6803SDavid Ahern 507962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) 508962b6803SDavid Ahern { 509962b6803SDavid Ahern struct fib6_nh_dm_arg *arg = _arg; 510962b6803SDavid Ahern 511962b6803SDavid Ahern arg->nh = nh; 512962b6803SDavid Ahern return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, 513962b6803SDavid Ahern arg->flags); 514962b6803SDavid Ahern } 515962b6803SDavid Ahern 516962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */ 517962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, 518962b6803SDavid Ahern struct fib6_result *res, 519962b6803SDavid Ahern const struct in6_addr *saddr, 520962b6803SDavid Ahern int oif, int flags) 521962b6803SDavid Ahern { 522962b6803SDavid Ahern struct fib6_nh_dm_arg arg = { 523962b6803SDavid Ahern .net = net, 524962b6803SDavid Ahern .saddr = saddr, 525962b6803SDavid Ahern .oif = oif, 526962b6803SDavid Ahern .flags = flags, 527962b6803SDavid Ahern }; 528962b6803SDavid Ahern 529962b6803SDavid Ahern if (nexthop_is_blackhole(nh)) 530962b6803SDavid Ahern return NULL; 531962b6803SDavid Ahern 532962b6803SDavid Ahern if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) 533962b6803SDavid Ahern return arg.nh; 534962b6803SDavid Ahern 535962b6803SDavid Ahern return NULL; 536962b6803SDavid Ahern } 537962b6803SDavid Ahern 53875ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res, 53975ef7389SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 5401da177e4SLinus Torvalds { 54175ef7389SDavid Ahern struct fib6_info *f6i = res->f6i; 54275ef7389SDavid Ahern struct fib6_info *spf6i; 54375ef7389SDavid Ahern struct fib6_nh *nh; 5441da177e4SLinus Torvalds 54575ef7389SDavid Ahern if (!oif && ipv6_addr_any(saddr)) { 546f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 547f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 548f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 549f88d8ea6SDavid Ahern goto out_blackhole; 550f88d8ea6SDavid Ahern } else { 5511cf844c7SDavid Ahern nh = f6i->fib6_nh; 552f88d8ea6SDavid Ahern } 5537d21fec9SDavid Ahern if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 5547d21fec9SDavid Ahern goto out; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 55775ef7389SDavid Ahern for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 558962b6803SDavid Ahern bool matched = false; 559962b6803SDavid Ahern 560962b6803SDavid Ahern if (unlikely(spf6i->nh)) { 561962b6803SDavid Ahern nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, 562962b6803SDavid Ahern oif, flags); 563962b6803SDavid Ahern if (nh) 564962b6803SDavid Ahern matched = true; 565962b6803SDavid Ahern } else { 5661cf844c7SDavid Ahern nh = spf6i->fib6_nh; 567962b6803SDavid Ahern if (__rt6_device_match(net, nh, saddr, oif, flags)) 568962b6803SDavid Ahern matched = true; 569962b6803SDavid Ahern } 570962b6803SDavid Ahern if (matched) { 57175ef7389SDavid Ahern res->f6i = spf6i; 5727d21fec9SDavid Ahern goto out; 57375ef7389SDavid Ahern } 57475ef7389SDavid Ahern } 5751da177e4SLinus Torvalds 57675ef7389SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) { 57775ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5781cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 5797d21fec9SDavid Ahern goto out; 58075ef7389SDavid Ahern } 58175ef7389SDavid Ahern 582f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 583f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 584f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 585f88d8ea6SDavid Ahern goto out_blackhole; 586f88d8ea6SDavid Ahern } else { 5871cf844c7SDavid Ahern nh = f6i->fib6_nh; 588f88d8ea6SDavid Ahern } 589f88d8ea6SDavid Ahern 5907d21fec9SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) { 59175ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5921cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 59375ef7389SDavid Ahern } 5947d21fec9SDavid Ahern out: 5957d21fec9SDavid Ahern res->nh = nh; 5967d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 5977d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 598f88d8ea6SDavid Ahern return; 599f88d8ea6SDavid Ahern 600f88d8ea6SDavid Ahern out_blackhole: 601f88d8ea6SDavid Ahern res->fib6_flags |= RTF_REJECT; 602f88d8ea6SDavid Ahern res->fib6_type = RTN_BLACKHOLE; 603f88d8ea6SDavid Ahern res->nh = nh; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 60627097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 607c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 608c2f17e82SHannes Frederic Sowa struct work_struct work; 609c2f17e82SHannes Frederic Sowa struct in6_addr target; 610c2f17e82SHannes Frederic Sowa struct net_device *dev; 611c2f17e82SHannes Frederic Sowa }; 612c2f17e82SHannes Frederic Sowa 613c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 614c2f17e82SHannes Frederic Sowa { 615c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 616c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 617c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 618c2f17e82SHannes Frederic Sowa 619c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 620adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 621c2f17e82SHannes Frederic Sowa dev_put(work->dev); 622662f5533SMichael Büsch kfree(work); 623c2f17e82SHannes Frederic Sowa } 624c2f17e82SHannes Frederic Sowa 625cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 62627097255SYOSHIFUJI Hideaki { 627f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 6285e670d84SDavid Ahern const struct in6_addr *nh_gw; 6291bef4c22SEric Dumazet unsigned long last_probe; 630f2c31e32SEric Dumazet struct neighbour *neigh; 6315e670d84SDavid Ahern struct net_device *dev; 632f547fac6SSabrina Dubroca struct inet6_dev *idev; 6335e670d84SDavid Ahern 63427097255SYOSHIFUJI Hideaki /* 63527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 63627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 63727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 63827097255SYOSHIFUJI Hideaki * 63927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 64027097255SYOSHIFUJI Hideaki * to no more than one per minute. 64127097255SYOSHIFUJI Hideaki */ 642004b3942SHangbin Liu if (!fib6_nh->fib_nh_gw_family) 643fdd6681dSAmerigo Wang return; 6445e670d84SDavid Ahern 645cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 646cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 6472152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6481bef4c22SEric Dumazet last_probe = READ_ONCE(fib6_nh->last_probe); 649f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 6505e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 6512152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 6528d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 6538d6c31bfSMartin KaFai Lau goto out; 6548d6c31bfSMartin KaFai Lau 6552152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 656990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 657990edb42SMartin KaFai Lau time_after(jiffies, 658dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 659c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 660990edb42SMartin KaFai Lau if (work) 6617e980569SJiri Benc __neigh_set_probe_once(neigh); 662990edb42SMartin KaFai Lau } 663c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 6641bef4c22SEric Dumazet } else if (time_after(jiffies, last_probe + 665f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 666990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 667990edb42SMartin KaFai Lau } 668c2f17e82SHannes Frederic Sowa 6691bef4c22SEric Dumazet if (!work || cmpxchg(&fib6_nh->last_probe, 6701bef4c22SEric Dumazet last_probe, jiffies) != last_probe) { 6711bef4c22SEric Dumazet kfree(work); 6721bef4c22SEric Dumazet } else { 673c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 6745e670d84SDavid Ahern work->target = *nh_gw; 6755e670d84SDavid Ahern dev_hold(dev); 6765e670d84SDavid Ahern work->dev = dev; 677c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 678c2f17e82SHannes Frederic Sowa } 679990edb42SMartin KaFai Lau 6808d6c31bfSMartin KaFai Lau out: 6812152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 682f2c31e32SEric Dumazet } 68327097255SYOSHIFUJI Hideaki #else 684cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 68527097255SYOSHIFUJI Hideaki { 68627097255SYOSHIFUJI Hideaki } 68727097255SYOSHIFUJI Hideaki #endif 68827097255SYOSHIFUJI Hideaki 6891da177e4SLinus Torvalds /* 690554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6911da177e4SLinus Torvalds */ 6921ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 6931da177e4SLinus Torvalds { 694afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 6955e670d84SDavid Ahern struct neighbour *neigh; 696f2c31e32SEric Dumazet 697145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6981ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 6991ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 700145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 701145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 702554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 703afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 704398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 705a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 706afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 7077e980569SJiri Benc else 7087e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 709398bcbebSYOSHIFUJI Hideaki #endif 710145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 711afc154e9SHannes Frederic Sowa } else { 712afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 7137e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 714a5a81f0bSPaul Marks } 715145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 716145a3621SYOSHIFUJI Hideaki / 吉藤英明 717a5a81f0bSPaul Marks return ret; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 720702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 721702cea56SDavid Ahern int strict) 722554cfb7eSYOSHIFUJI Hideaki { 7236e1809a5SDavid Ahern int m = 0; 7244d0c5911SYOSHIFUJI Hideaki 7256e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 7266e1809a5SDavid Ahern m = 2; 7276e1809a5SDavid Ahern 72877d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 729afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 730ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 731702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 732ebacaaa0SYOSHIFUJI Hideaki #endif 7331ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 734702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 7351ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 736afc154e9SHannes Frederic Sowa if (n < 0) 737afc154e9SHannes Frederic Sowa return n; 738afc154e9SHannes Frederic Sowa } 739554cfb7eSYOSHIFUJI Hideaki return m; 740554cfb7eSYOSHIFUJI Hideaki } 741554cfb7eSYOSHIFUJI Hideaki 74228679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 74328679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 744554cfb7eSYOSHIFUJI Hideaki { 745afc154e9SHannes Frederic Sowa bool match_do_rr = false; 74628679ed1SDavid Ahern bool rc = false; 74728679ed1SDavid Ahern int m; 74835103d11SAndy Gospodarek 74928679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 7508067bb8cSIdo Schimmel goto out; 7518067bb8cSIdo Schimmel 75228679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 75328679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 754d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 75535103d11SAndy Gospodarek goto out; 756554cfb7eSYOSHIFUJI Hideaki 75728679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 7587e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 759afc154e9SHannes Frederic Sowa match_do_rr = true; 760afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 7617e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 762f11e6659SDavid S. Miller goto out; 7631da177e4SLinus Torvalds } 764f11e6659SDavid S. Miller 765afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 76628679ed1SDavid Ahern rt6_probe(nh); 767afc154e9SHannes Frederic Sowa 7687e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 769afc154e9SHannes Frederic Sowa if (m > *mpri) { 770afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 771afc154e9SHannes Frederic Sowa *mpri = m; 77228679ed1SDavid Ahern rc = true; 773afc154e9SHannes Frederic Sowa } 774f11e6659SDavid S. Miller out: 77528679ed1SDavid Ahern return rc; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 77817a5984eSDavid Ahern struct fib6_nh_frl_arg { 77917a5984eSDavid Ahern u32 flags; 78017a5984eSDavid Ahern int oif; 78117a5984eSDavid Ahern int strict; 78217a5984eSDavid Ahern int *mpri; 78317a5984eSDavid Ahern bool *do_rr; 78417a5984eSDavid Ahern struct fib6_nh *nh; 78517a5984eSDavid Ahern }; 78617a5984eSDavid Ahern 78717a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) 78817a5984eSDavid Ahern { 78917a5984eSDavid Ahern struct fib6_nh_frl_arg *arg = _arg; 79017a5984eSDavid Ahern 79117a5984eSDavid Ahern arg->nh = nh; 79217a5984eSDavid Ahern return find_match(nh, arg->flags, arg->oif, arg->strict, 79317a5984eSDavid Ahern arg->mpri, arg->do_rr); 79417a5984eSDavid Ahern } 79517a5984eSDavid Ahern 796b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start, 79730c15f03SDavid Ahern struct fib6_info *nomatch, u32 metric, 798b7bc4b6aSDavid Ahern struct fib6_result *res, struct fib6_info **cont, 79930c15f03SDavid Ahern int oif, int strict, bool *do_rr, int *mpri) 80030c15f03SDavid Ahern { 801b7bc4b6aSDavid Ahern struct fib6_info *f6i; 80230c15f03SDavid Ahern 803b7bc4b6aSDavid Ahern for (f6i = f6i_start; 804b7bc4b6aSDavid Ahern f6i && f6i != nomatch; 805b7bc4b6aSDavid Ahern f6i = rcu_dereference(f6i->fib6_next)) { 80617a5984eSDavid Ahern bool matched = false; 80730c15f03SDavid Ahern struct fib6_nh *nh; 80830c15f03SDavid Ahern 809b7bc4b6aSDavid Ahern if (cont && f6i->fib6_metric != metric) { 810b7bc4b6aSDavid Ahern *cont = f6i; 81130c15f03SDavid Ahern return; 81230c15f03SDavid Ahern } 81330c15f03SDavid Ahern 814b7bc4b6aSDavid Ahern if (fib6_check_expired(f6i)) 81530c15f03SDavid Ahern continue; 81630c15f03SDavid Ahern 81717a5984eSDavid Ahern if (unlikely(f6i->nh)) { 81817a5984eSDavid Ahern struct fib6_nh_frl_arg arg = { 81917a5984eSDavid Ahern .flags = f6i->fib6_flags, 82017a5984eSDavid Ahern .oif = oif, 82117a5984eSDavid Ahern .strict = strict, 82217a5984eSDavid Ahern .mpri = mpri, 82317a5984eSDavid Ahern .do_rr = do_rr 82417a5984eSDavid Ahern }; 82517a5984eSDavid Ahern 82617a5984eSDavid Ahern if (nexthop_is_blackhole(f6i->nh)) { 82717a5984eSDavid Ahern res->fib6_flags = RTF_REJECT; 82817a5984eSDavid Ahern res->fib6_type = RTN_BLACKHOLE; 82917a5984eSDavid Ahern res->f6i = f6i; 83017a5984eSDavid Ahern res->nh = nexthop_fib6_nh(f6i->nh); 83117a5984eSDavid Ahern return; 83217a5984eSDavid Ahern } 83317a5984eSDavid Ahern if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, 83417a5984eSDavid Ahern &arg)) { 83517a5984eSDavid Ahern matched = true; 83617a5984eSDavid Ahern nh = arg.nh; 83717a5984eSDavid Ahern } 83817a5984eSDavid Ahern } else { 8391cf844c7SDavid Ahern nh = f6i->fib6_nh; 84017a5984eSDavid Ahern if (find_match(nh, f6i->fib6_flags, oif, strict, 84117a5984eSDavid Ahern mpri, do_rr)) 84217a5984eSDavid Ahern matched = true; 84317a5984eSDavid Ahern } 84417a5984eSDavid Ahern if (matched) { 845b7bc4b6aSDavid Ahern res->f6i = f6i; 846b7bc4b6aSDavid Ahern res->nh = nh; 8477d21fec9SDavid Ahern res->fib6_flags = f6i->fib6_flags; 8487d21fec9SDavid Ahern res->fib6_type = f6i->fib6_type; 849b7bc4b6aSDavid Ahern } 85030c15f03SDavid Ahern } 85130c15f03SDavid Ahern } 85230c15f03SDavid Ahern 853b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 854b7bc4b6aSDavid Ahern struct fib6_info *rr_head, int oif, int strict, 855b7bc4b6aSDavid Ahern bool *do_rr, struct fib6_result *res) 856f11e6659SDavid S. Miller { 857b7bc4b6aSDavid Ahern u32 metric = rr_head->fib6_metric; 858b7bc4b6aSDavid Ahern struct fib6_info *cont = NULL; 859f11e6659SDavid S. Miller int mpri = -1; 860f11e6659SDavid S. Miller 861b7bc4b6aSDavid Ahern __find_rr_leaf(rr_head, NULL, metric, res, &cont, 86230c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8639fbdcfafSSteffen Klassert 864b7bc4b6aSDavid Ahern __find_rr_leaf(leaf, rr_head, metric, res, &cont, 86530c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8669fbdcfafSSteffen Klassert 867b7bc4b6aSDavid Ahern if (res->f6i || !cont) 868b7bc4b6aSDavid Ahern return; 8699fbdcfafSSteffen Klassert 870b7bc4b6aSDavid Ahern __find_rr_leaf(cont, NULL, metric, res, NULL, 87130c15f03SDavid Ahern oif, strict, do_rr, &mpri); 872f11e6659SDavid S. Miller } 873f11e6659SDavid S. Miller 874b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 875b7bc4b6aSDavid Ahern struct fib6_result *res, int strict) 876f11e6659SDavid S. Miller { 8778d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 878b7bc4b6aSDavid Ahern struct fib6_info *rt0; 879afc154e9SHannes Frederic Sowa bool do_rr = false; 88017ecf590SWei Wang int key_plen; 881f11e6659SDavid S. Miller 882b7bc4b6aSDavid Ahern /* make sure this function or its helpers sets f6i */ 883b7bc4b6aSDavid Ahern res->f6i = NULL; 884b7bc4b6aSDavid Ahern 885421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 886b7bc4b6aSDavid Ahern goto out; 8878d1040e8SWei Wang 88866f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 889f11e6659SDavid S. Miller if (!rt0) 89066f5d6ceSWei Wang rt0 = leaf; 891f11e6659SDavid S. Miller 89217ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 89317ecf590SWei Wang * and fn->leaf does not points to its child's leaf 89417ecf590SWei Wang * (This might happen if all routes under fn are deleted from 89517ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 89617ecf590SWei Wang */ 89793c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 89817ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 89993c2fb25SDavid Ahern if (rt0->fib6_src.plen) 90093c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 90117ecf590SWei Wang #endif 90217ecf590SWei Wang if (fn->fn_bit != key_plen) 903b7bc4b6aSDavid Ahern goto out; 90417ecf590SWei Wang 905b7bc4b6aSDavid Ahern find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 906afc154e9SHannes Frederic Sowa if (do_rr) { 9078fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 908f11e6659SDavid S. Miller 909554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 91093c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 9118d1040e8SWei Wang next = leaf; 912f11e6659SDavid S. Miller 91366f5d6ceSWei Wang if (next != rt0) { 91493c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 91566f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 91693c2fb25SDavid Ahern if (next->fib6_node) 91766f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 91893c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 91966f5d6ceSWei Wang } 920554cfb7eSYOSHIFUJI Hideaki } 921554cfb7eSYOSHIFUJI Hideaki 922b7bc4b6aSDavid Ahern out: 923b7bc4b6aSDavid Ahern if (!res->f6i) { 924b7bc4b6aSDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 9251cf844c7SDavid Ahern res->nh = res->f6i->fib6_nh; 9267d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 9277d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 928b7bc4b6aSDavid Ahern } 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 93185bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 9328b9df265SMartin KaFai Lau { 93385bd05deSDavid Ahern return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 93485bd05deSDavid Ahern res->nh->fib_nh_gw_family; 9358b9df265SMartin KaFai Lau } 9368b9df265SMartin KaFai Lau 93770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 93870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 939b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 94070ceb4f5SYOSHIFUJI Hideaki { 941c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 94270ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 94370ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 94470ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 9454bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 9468d1c802bSDavid Ahern struct fib6_info *rt; 94770ceb4f5SYOSHIFUJI Hideaki 94870ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 94970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95070ceb4f5SYOSHIFUJI Hideaki } 95170ceb4f5SYOSHIFUJI Hideaki 95270ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 95370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 95470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 95670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 95870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 95970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 96070ceb4f5SYOSHIFUJI Hideaki } 96170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 96270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 96370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 96470ceb4f5SYOSHIFUJI Hideaki } 96570ceb4f5SYOSHIFUJI Hideaki } 96670ceb4f5SYOSHIFUJI Hideaki 96770ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 96870ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 9693933fc95SJens Rosenboom return -EINVAL; 97070ceb4f5SYOSHIFUJI Hideaki 9714bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 97270ceb4f5SYOSHIFUJI Hideaki 97370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 97470ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 97570ceb4f5SYOSHIFUJI Hideaki else { 97670ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 97770ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 97870ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 97970ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 98070ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 98170ceb4f5SYOSHIFUJI Hideaki } 98270ceb4f5SYOSHIFUJI Hideaki 983f104a567SDuan Jiong if (rinfo->prefix_len == 0) 984afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 985f104a567SDuan Jiong else 986f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 987830218c1SDavid Ahern gwaddr, dev); 98870ceb4f5SYOSHIFUJI Hideaki 98970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 99011dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 99170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 99270ceb4f5SYOSHIFUJI Hideaki } 99370ceb4f5SYOSHIFUJI Hideaki 99470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 995830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 996830218c1SDavid Ahern dev, pref); 99770ceb4f5SYOSHIFUJI Hideaki else if (rt) 99893c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 99993c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 100070ceb4f5SYOSHIFUJI Hideaki 100170ceb4f5SYOSHIFUJI Hideaki if (rt) { 10021716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 100314895687SDavid Ahern fib6_clean_expires(rt); 10041716a961SGao feng else 100514895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 10061716a961SGao feng 100793531c67SDavid Ahern fib6_info_release(rt); 100870ceb4f5SYOSHIFUJI Hideaki } 100970ceb4f5SYOSHIFUJI Hideaki return 0; 101070ceb4f5SYOSHIFUJI Hideaki } 101170ceb4f5SYOSHIFUJI Hideaki #endif 101270ceb4f5SYOSHIFUJI Hideaki 1013ae90d867SDavid Ahern /* 1014ae90d867SDavid Ahern * Misc support functions 1015ae90d867SDavid Ahern */ 1016ae90d867SDavid Ahern 1017ae90d867SDavid Ahern /* called with rcu_lock held */ 10180d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 1019ae90d867SDavid Ahern { 10200d161581SDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 1021ae90d867SDavid Ahern 10227d21fec9SDavid Ahern if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 1023ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 1024ae90d867SDavid Ahern * device if it is a master device, the master device if 1025ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 1026ae90d867SDavid Ahern */ 1027ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 10287d21fec9SDavid Ahern !rt6_need_strict(&res->f6i->fib6_dst.addr)) 1029ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 1030ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 1031ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 1032ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 1033ae90d867SDavid Ahern * case we want dev returned to be dev 1034ae90d867SDavid Ahern */ 1035ae90d867SDavid Ahern } 1036ae90d867SDavid Ahern 1037ae90d867SDavid Ahern return dev; 1038ae90d867SDavid Ahern } 1039ae90d867SDavid Ahern 10406edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 10416edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 10426edb3c96SDavid Ahern [RTN_UNICAST] = 0, 10436edb3c96SDavid Ahern [RTN_LOCAL] = 0, 10446edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 10456edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 10466edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 10476edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 10486edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 10496edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 10506edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 10516edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 10526edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 10536edb3c96SDavid Ahern }; 10546edb3c96SDavid Ahern 10556edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 10566edb3c96SDavid Ahern { 10576edb3c96SDavid Ahern return fib6_prop[fib6_type]; 10586edb3c96SDavid Ahern } 10596edb3c96SDavid Ahern 10608d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 10613b6761d1SDavid Ahern { 10623b6761d1SDavid Ahern unsigned short flags = 0; 10633b6761d1SDavid Ahern 10643b6761d1SDavid Ahern if (rt->dst_nocount) 10653b6761d1SDavid Ahern flags |= DST_NOCOUNT; 10663b6761d1SDavid Ahern if (rt->dst_nopolicy) 10673b6761d1SDavid Ahern flags |= DST_NOPOLICY; 10683b6761d1SDavid Ahern 10693b6761d1SDavid Ahern return flags; 10703b6761d1SDavid Ahern } 10713b6761d1SDavid Ahern 10727d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 10736edb3c96SDavid Ahern { 10747d21fec9SDavid Ahern rt->dst.error = ip6_rt_type_to_error(fib6_type); 10756edb3c96SDavid Ahern 10767d21fec9SDavid Ahern switch (fib6_type) { 10776edb3c96SDavid Ahern case RTN_BLACKHOLE: 10786edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 10796edb3c96SDavid Ahern rt->dst.input = dst_discard; 10806edb3c96SDavid Ahern break; 10816edb3c96SDavid Ahern case RTN_PROHIBIT: 10826edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 10836edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 10846edb3c96SDavid Ahern break; 10856edb3c96SDavid Ahern case RTN_THROW: 10866edb3c96SDavid Ahern case RTN_UNREACHABLE: 10876edb3c96SDavid Ahern default: 10886edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 10896edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 10906edb3c96SDavid Ahern break; 10916edb3c96SDavid Ahern } 10926edb3c96SDavid Ahern } 10936edb3c96SDavid Ahern 10940d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 10956edb3c96SDavid Ahern { 10967d21fec9SDavid Ahern struct fib6_info *f6i = res->f6i; 10970d161581SDavid Ahern 10987d21fec9SDavid Ahern if (res->fib6_flags & RTF_REJECT) { 10997d21fec9SDavid Ahern ip6_rt_init_dst_reject(rt, res->fib6_type); 11006edb3c96SDavid Ahern return; 11016edb3c96SDavid Ahern } 11026edb3c96SDavid Ahern 11036edb3c96SDavid Ahern rt->dst.error = 0; 11046edb3c96SDavid Ahern rt->dst.output = ip6_output; 11056edb3c96SDavid Ahern 11067d21fec9SDavid Ahern if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 11076edb3c96SDavid Ahern rt->dst.input = ip6_input; 11087d21fec9SDavid Ahern } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 11096edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 11106edb3c96SDavid Ahern } else { 11116edb3c96SDavid Ahern rt->dst.input = ip6_forward; 11126edb3c96SDavid Ahern } 11136edb3c96SDavid Ahern 11140d161581SDavid Ahern if (res->nh->fib_nh_lws) { 11150d161581SDavid Ahern rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 11166edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 11176edb3c96SDavid Ahern } 11186edb3c96SDavid Ahern 11196edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 11206edb3c96SDavid Ahern } 11216edb3c96SDavid Ahern 1122e873e4b9SWei Wang /* Caller must already hold reference to @from */ 11238d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1124ae90d867SDavid Ahern { 1125ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 1126a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 1127e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1128ae90d867SDavid Ahern } 1129ae90d867SDavid Ahern 11300d161581SDavid Ahern /* Caller must already hold reference to f6i in result */ 11310d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1132ae90d867SDavid Ahern { 11330d161581SDavid Ahern const struct fib6_nh *nh = res->nh; 11340d161581SDavid Ahern const struct net_device *dev = nh->fib_nh_dev; 11350d161581SDavid Ahern struct fib6_info *f6i = res->f6i; 1136dcd1f572SDavid Ahern 11370d161581SDavid Ahern ip6_rt_init_dst(rt, res); 11386edb3c96SDavid Ahern 11390d161581SDavid Ahern rt->rt6i_dst = f6i->fib6_dst; 1140dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 11417d21fec9SDavid Ahern rt->rt6i_flags = res->fib6_flags; 11420d161581SDavid Ahern if (nh->fib_nh_gw_family) { 11430d161581SDavid Ahern rt->rt6i_gateway = nh->fib_nh_gw6; 11442b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 11452b2450caSDavid Ahern } 11460d161581SDavid Ahern rt6_set_from(rt, f6i); 1147ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 11480d161581SDavid Ahern rt->rt6i_src = f6i->fib6_src; 1149ae90d867SDavid Ahern #endif 1150ae90d867SDavid Ahern } 1151ae90d867SDavid Ahern 1152a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1153a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1154a3c00e46SMartin KaFai Lau { 115566f5d6ceSWei Wang struct fib6_node *pn, *sn; 1156a3c00e46SMartin KaFai Lau while (1) { 1157a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1158a3c00e46SMartin KaFai Lau return NULL; 115966f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 116066f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 116166f5d6ceSWei Wang if (sn && sn != fn) 11626454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1163a3c00e46SMartin KaFai Lau else 1164a3c00e46SMartin KaFai Lau fn = pn; 1165a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1166a3c00e46SMartin KaFai Lau return fn; 1167a3c00e46SMartin KaFai Lau } 1168a3c00e46SMartin KaFai Lau } 1169c71099acSThomas Graf 117010585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1171d3843fe5SWei Wang { 1172d3843fe5SWei Wang struct rt6_info *rt = *prt; 1173d3843fe5SWei Wang 1174d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1175d3843fe5SWei Wang return true; 117610585b43SDavid Ahern if (net) { 1177d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1178d3843fe5SWei Wang dst_hold(&rt->dst); 1179d3843fe5SWei Wang } else { 1180d3843fe5SWei Wang rt = NULL; 1181d3843fe5SWei Wang } 1182d3843fe5SWei Wang *prt = rt; 1183d3843fe5SWei Wang return false; 1184d3843fe5SWei Wang } 1185d3843fe5SWei Wang 1186dec9b0e2SDavid Ahern /* called with rcu_lock held */ 11879b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1188dec9b0e2SDavid Ahern { 11899b6b35abSDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 11909b6b35abSDavid Ahern struct fib6_info *f6i = res->f6i; 11919b6b35abSDavid Ahern unsigned short flags; 1192dec9b0e2SDavid Ahern struct rt6_info *nrt; 1193dec9b0e2SDavid Ahern 11949b6b35abSDavid Ahern if (!fib6_info_hold_safe(f6i)) 11951c87e79aSXin Long goto fallback; 1196e873e4b9SWei Wang 11979b6b35abSDavid Ahern flags = fib6_info_dst_flags(f6i); 119893531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 11991c87e79aSXin Long if (!nrt) { 12009b6b35abSDavid Ahern fib6_info_release(f6i); 12011c87e79aSXin Long goto fallback; 12021c87e79aSXin Long } 1203dec9b0e2SDavid Ahern 12040d161581SDavid Ahern ip6_rt_copy_init(nrt, res); 12051c87e79aSXin Long return nrt; 12061c87e79aSXin Long 12071c87e79aSXin Long fallback: 12081c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 12091c87e79aSXin Long dst_hold(&nrt->dst); 1210dec9b0e2SDavid Ahern return nrt; 1211dec9b0e2SDavid Ahern } 1212dec9b0e2SDavid Ahern 12138ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 12148ed67789SDaniel Lezcano struct fib6_table *table, 1215b75cc8f9SDavid Ahern struct flowi6 *fl6, 1216b75cc8f9SDavid Ahern const struct sk_buff *skb, 1217b75cc8f9SDavid Ahern int flags) 12181da177e4SLinus Torvalds { 1219b1d40991SDavid Ahern struct fib6_result res = {}; 12201da177e4SLinus Torvalds struct fib6_node *fn; 122123fb93a4SDavid Ahern struct rt6_info *rt; 12221da177e4SLinus Torvalds 1223b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1224b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 1225b6cdbc85SDavid Ahern 122666f5d6ceSWei Wang rcu_read_lock(); 12276454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1228c71099acSThomas Graf restart: 1229b1d40991SDavid Ahern res.f6i = rcu_dereference(fn->leaf); 1230b1d40991SDavid Ahern if (!res.f6i) 1231b1d40991SDavid Ahern res.f6i = net->ipv6.fib6_null_entry; 1232af52a52cSDavid Ahern else 123375ef7389SDavid Ahern rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 123475ef7389SDavid Ahern flags); 1235af52a52cSDavid Ahern 1236b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1237a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1238a3c00e46SMartin KaFai Lau if (fn) 1239a3c00e46SMartin KaFai Lau goto restart; 1240af52a52cSDavid Ahern 1241af52a52cSDavid Ahern rt = net->ipv6.ip6_null_entry; 1242af52a52cSDavid Ahern dst_hold(&rt->dst); 1243af52a52cSDavid Ahern goto out; 1244f88d8ea6SDavid Ahern } else if (res.fib6_flags & RTF_REJECT) { 1245f88d8ea6SDavid Ahern goto do_create; 1246a3c00e46SMartin KaFai Lau } 12472b760fcfSWei Wang 1248b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1249b1d40991SDavid Ahern fl6->flowi6_oif != 0, skb, flags); 1250b1d40991SDavid Ahern 12514c9483b2SDavid S. Miller /* Search through exception table */ 12527e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 125323fb93a4SDavid Ahern if (rt) { 125410585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 1255d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 125623fb93a4SDavid Ahern } else { 1257f88d8ea6SDavid Ahern do_create: 12589b6b35abSDavid Ahern rt = ip6_create_rt_rcu(&res); 1259dec9b0e2SDavid Ahern } 1260d3843fe5SWei Wang 1261af52a52cSDavid Ahern out: 12628ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 1263af52a52cSDavid Ahern 126466f5d6ceSWei Wang rcu_read_unlock(); 1265b811580dSDavid Ahern 12661da177e4SLinus Torvalds return rt; 1267c71099acSThomas Graf } 1268c71099acSThomas Graf 1269ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1270b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1271ea6e574eSFlorian Westphal { 1272b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1273ea6e574eSFlorian Westphal } 1274ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1275ea6e574eSFlorian Westphal 12769acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1277b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1278b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1279c71099acSThomas Graf { 12804c9483b2SDavid S. Miller struct flowi6 fl6 = { 12814c9483b2SDavid S. Miller .flowi6_oif = oif, 12824c9483b2SDavid S. Miller .daddr = *daddr, 1283c71099acSThomas Graf }; 1284c71099acSThomas Graf struct dst_entry *dst; 128577d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1286c71099acSThomas Graf 1287adaa70bbSThomas Graf if (saddr) { 12884c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1289adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1290adaa70bbSThomas Graf } 1291adaa70bbSThomas Graf 1292b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1293c71099acSThomas Graf if (dst->error == 0) 1294c71099acSThomas Graf return (struct rt6_info *) dst; 1295c71099acSThomas Graf 1296c71099acSThomas Graf dst_release(dst); 1297c71099acSThomas Graf 12981da177e4SLinus Torvalds return NULL; 12991da177e4SLinus Torvalds } 13007159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 13017159039aSYOSHIFUJI Hideaki 1302c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 13031cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 13041cfb71eeSWei Wang * route is released. 13051cfb71eeSWei Wang * Caller must hold dst before calling it. 13061da177e4SLinus Torvalds */ 13071da177e4SLinus Torvalds 13088d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1309333c4301SDavid Ahern struct netlink_ext_ack *extack) 13101da177e4SLinus Torvalds { 13111da177e4SLinus Torvalds int err; 1312c71099acSThomas Graf struct fib6_table *table; 13131da177e4SLinus Torvalds 131493c2fb25SDavid Ahern table = rt->fib6_table; 131566f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1316d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 131766f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds return err; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13228d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 132340e22e8fSThomas Graf { 1324afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1325e715b6d3SFlorian Westphal 1326d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 132740e22e8fSThomas Graf } 132840e22e8fSThomas Graf 132985bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 133021efcfa0SEric Dumazet const struct in6_addr *daddr, 1331b71d1d42SEric Dumazet const struct in6_addr *saddr) 13321da177e4SLinus Torvalds { 133385bd05deSDavid Ahern struct fib6_info *f6i = res->f6i; 13344832c30dSDavid Ahern struct net_device *dev; 13351da177e4SLinus Torvalds struct rt6_info *rt; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds /* 13381da177e4SLinus Torvalds * Clone the route. 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds 134185bd05deSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1342e873e4b9SWei Wang return NULL; 1343e873e4b9SWei Wang 13440d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 134593531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1346e873e4b9SWei Wang if (!rt) { 134785bd05deSDavid Ahern fib6_info_release(f6i); 134883a09abdSMartin KaFai Lau return NULL; 1349e873e4b9SWei Wang } 135083a09abdSMartin KaFai Lau 13510d161581SDavid Ahern ip6_rt_copy_init(rt, res); 13528b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 135383a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 135483a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 13558b9df265SMartin KaFai Lau 135685bd05deSDavid Ahern if (!rt6_is_gw_or_nonexthop(res)) { 135785bd05deSDavid Ahern if (f6i->fib6_dst.plen != 128 && 135885bd05deSDavid Ahern ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 135958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 13601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 13611da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 13624e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 13631da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds #endif 136695a9a5baSYOSHIFUJI Hideaki } 136795a9a5baSYOSHIFUJI Hideaki 1368299d9939SYOSHIFUJI Hideaki return rt; 1369299d9939SYOSHIFUJI Hideaki } 1370299d9939SYOSHIFUJI Hideaki 1371db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1372d52d3997SMartin KaFai Lau { 1373db3fedeeSDavid Ahern struct fib6_info *f6i = res->f6i; 1374db3fedeeSDavid Ahern unsigned short flags = fib6_info_dst_flags(f6i); 13754832c30dSDavid Ahern struct net_device *dev; 1376d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1377d52d3997SMartin KaFai Lau 1378db3fedeeSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1379e873e4b9SWei Wang return NULL; 1380e873e4b9SWei Wang 13814832c30dSDavid Ahern rcu_read_lock(); 13820d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 1383d8882935SEric Dumazet pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT); 13844832c30dSDavid Ahern rcu_read_unlock(); 1385e873e4b9SWei Wang if (!pcpu_rt) { 1386db3fedeeSDavid Ahern fib6_info_release(f6i); 1387d52d3997SMartin KaFai Lau return NULL; 1388e873e4b9SWei Wang } 13890d161581SDavid Ahern ip6_rt_copy_init(pcpu_rt, res); 1390d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 13918f34e53bSDavid Ahern 13928f34e53bSDavid Ahern if (f6i->nh) 13938f34e53bSDavid Ahern pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev)); 13948f34e53bSDavid Ahern 1395d52d3997SMartin KaFai Lau return pcpu_rt; 1396d52d3997SMartin KaFai Lau } 1397d52d3997SMartin KaFai Lau 13988f34e53bSDavid Ahern static bool rt6_is_valid(const struct rt6_info *rt6) 13998f34e53bSDavid Ahern { 14008f34e53bSDavid Ahern return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev)); 14018f34e53bSDavid Ahern } 14028f34e53bSDavid Ahern 140366f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1404db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1405d52d3997SMartin KaFai Lau { 1406c353071aSEric Dumazet struct rt6_info *pcpu_rt; 1407d52d3997SMartin KaFai Lau 1408c353071aSEric Dumazet pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); 1409d52d3997SMartin KaFai Lau 14108f34e53bSDavid Ahern if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) { 14118f34e53bSDavid Ahern struct rt6_info *prev, **p; 14128f34e53bSDavid Ahern 14138f34e53bSDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 14148f34e53bSDavid Ahern prev = xchg(p, NULL); 14158f34e53bSDavid Ahern if (prev) { 14168f34e53bSDavid Ahern dst_dev_put(&prev->dst); 14178f34e53bSDavid Ahern dst_release(&prev->dst); 14188f34e53bSDavid Ahern } 14198f34e53bSDavid Ahern 14208f34e53bSDavid Ahern pcpu_rt = NULL; 14218f34e53bSDavid Ahern } 14228f34e53bSDavid Ahern 1423a73e4195SMartin KaFai Lau return pcpu_rt; 1424a73e4195SMartin KaFai Lau } 1425a73e4195SMartin KaFai Lau 1426afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1427db3fedeeSDavid Ahern const struct fib6_result *res) 1428a73e4195SMartin KaFai Lau { 1429a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1430d52d3997SMartin KaFai Lau 1431db3fedeeSDavid Ahern pcpu_rt = ip6_rt_pcpu_alloc(res); 14320e09edccSWei Wang if (!pcpu_rt) 14330e09edccSWei Wang return NULL; 1434d52d3997SMartin KaFai Lau 1435f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1436d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1437951f788aSEric Dumazet BUG_ON(prev); 1438a94b9367SWei Wang 143961fb0d01SEric Dumazet if (res->f6i->fib6_destroying) { 144061fb0d01SEric Dumazet struct fib6_info *from; 144161fb0d01SEric Dumazet 144261fb0d01SEric Dumazet from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 144361fb0d01SEric Dumazet fib6_info_release(from); 144461fb0d01SEric Dumazet } 144561fb0d01SEric Dumazet 1446d52d3997SMartin KaFai Lau return pcpu_rt; 1447d52d3997SMartin KaFai Lau } 1448d52d3997SMartin KaFai Lau 144935732d01SWei Wang /* exception hash table implementation 145035732d01SWei Wang */ 145135732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 145235732d01SWei Wang 145335732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 145435732d01SWei Wang * Caller must hold rt6_exception_lock 145535732d01SWei Wang */ 145635732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 145735732d01SWei Wang struct rt6_exception *rt6_ex) 145835732d01SWei Wang { 1459f5b51fe8SPaolo Abeni struct fib6_info *from; 1460b2427e67SColin Ian King struct net *net; 146181eb8447SWei Wang 146235732d01SWei Wang if (!bucket || !rt6_ex) 146335732d01SWei Wang return; 1464b2427e67SColin Ian King 1465b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1466f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1467f5b51fe8SPaolo Abeni 1468f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1469f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1470f5b51fe8SPaolo Abeni */ 14710e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1472f5b51fe8SPaolo Abeni fib6_info_release(from); 1473f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1474f5b51fe8SPaolo Abeni 147535732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 147677634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 147735732d01SWei Wang kfree_rcu(rt6_ex, rcu); 147835732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 147935732d01SWei Wang bucket->depth--; 148035732d01SWei Wang } 148135732d01SWei Wang 148235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 148335732d01SWei Wang * Caller must hold rt6_exception_lock 148435732d01SWei Wang */ 148535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 148635732d01SWei Wang { 148735732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 148835732d01SWei Wang 148935732d01SWei Wang if (!bucket) 149035732d01SWei Wang return; 149135732d01SWei Wang 149235732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 149335732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 149435732d01SWei Wang oldest = rt6_ex; 149535732d01SWei Wang } 149635732d01SWei Wang rt6_remove_exception(bucket, oldest); 149735732d01SWei Wang } 149835732d01SWei Wang 149935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 150035732d01SWei Wang const struct in6_addr *src) 150135732d01SWei Wang { 150235732d01SWei Wang static u32 seed __read_mostly; 150335732d01SWei Wang u32 val; 150435732d01SWei Wang 150535732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 1506b6b556afSEric Dumazet val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed); 150735732d01SWei Wang 150835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 150935732d01SWei Wang if (src) 1510b6b556afSEric Dumazet val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val); 151135732d01SWei Wang #endif 151235732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 151335732d01SWei Wang } 151435732d01SWei Wang 151535732d01SWei Wang /* Helper function to find the cached rt in the hash table 151635732d01SWei Wang * and update bucket pointer to point to the bucket for this 151735732d01SWei Wang * (daddr, saddr) pair 151835732d01SWei Wang * Caller must hold rt6_exception_lock 151935732d01SWei Wang */ 152035732d01SWei Wang static struct rt6_exception * 152135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 152235732d01SWei Wang const struct in6_addr *daddr, 152335732d01SWei Wang const struct in6_addr *saddr) 152435732d01SWei Wang { 152535732d01SWei Wang struct rt6_exception *rt6_ex; 152635732d01SWei Wang u32 hval; 152735732d01SWei Wang 152835732d01SWei Wang if (!(*bucket) || !daddr) 152935732d01SWei Wang return NULL; 153035732d01SWei Wang 153135732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 153235732d01SWei Wang *bucket += hval; 153335732d01SWei Wang 153435732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 153535732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 153635732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 153735732d01SWei Wang 153835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 153935732d01SWei Wang if (matched && saddr) 154035732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 154135732d01SWei Wang #endif 154235732d01SWei Wang if (matched) 154335732d01SWei Wang return rt6_ex; 154435732d01SWei Wang } 154535732d01SWei Wang return NULL; 154635732d01SWei Wang } 154735732d01SWei Wang 154835732d01SWei Wang /* Helper function to find the cached rt in the hash table 154935732d01SWei Wang * and update bucket pointer to point to the bucket for this 155035732d01SWei Wang * (daddr, saddr) pair 155135732d01SWei Wang * Caller must hold rcu_read_lock() 155235732d01SWei Wang */ 155335732d01SWei Wang static struct rt6_exception * 155435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 155535732d01SWei Wang const struct in6_addr *daddr, 155635732d01SWei Wang const struct in6_addr *saddr) 155735732d01SWei Wang { 155835732d01SWei Wang struct rt6_exception *rt6_ex; 155935732d01SWei Wang u32 hval; 156035732d01SWei Wang 156135732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 156235732d01SWei Wang 156335732d01SWei Wang if (!(*bucket) || !daddr) 156435732d01SWei Wang return NULL; 156535732d01SWei Wang 156635732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 156735732d01SWei Wang *bucket += hval; 156835732d01SWei Wang 156935732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 157035732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 157135732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 157235732d01SWei Wang 157335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 157435732d01SWei Wang if (matched && saddr) 157535732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 157635732d01SWei Wang #endif 157735732d01SWei Wang if (matched) 157835732d01SWei Wang return rt6_ex; 157935732d01SWei Wang } 158035732d01SWei Wang return NULL; 158135732d01SWei Wang } 158235732d01SWei Wang 1583b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res) 158435732d01SWei Wang { 1585b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 1586d4ead6b3SDavid Ahern unsigned int mtu; 1587d4ead6b3SDavid Ahern 1588b748f260SDavid Ahern if (res->f6i->fib6_pmtu) { 1589b748f260SDavid Ahern mtu = res->f6i->fib6_pmtu; 1590dcd1f572SDavid Ahern } else { 1591b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 1592dcd1f572SDavid Ahern struct inet6_dev *idev; 1593dcd1f572SDavid Ahern 1594dcd1f572SDavid Ahern rcu_read_lock(); 1595dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1596dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1597dcd1f572SDavid Ahern rcu_read_unlock(); 1598dcd1f572SDavid Ahern } 1599dcd1f572SDavid Ahern 1600d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1601d4ead6b3SDavid Ahern 1602b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1603d4ead6b3SDavid Ahern } 1604d4ead6b3SDavid Ahern 1605cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL 1606cc5c073aSDavid Ahern 1607cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket 1608cc5c073aSDavid Ahern * (ie., all bucket users except rt6_insert_exception); 1609cc5c073aSDavid Ahern * 1610cc5c073aSDavid Ahern * called under rcu lock; sometimes called with rt6_exception_lock held 1611cc5c073aSDavid Ahern */ 1612cc5c073aSDavid Ahern static 1613cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, 1614cc5c073aSDavid Ahern spinlock_t *lock) 1615cc5c073aSDavid Ahern { 1616cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1617cc5c073aSDavid Ahern 1618cc5c073aSDavid Ahern if (lock) 1619cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1620cc5c073aSDavid Ahern lockdep_is_held(lock)); 1621cc5c073aSDavid Ahern else 1622cc5c073aSDavid Ahern bucket = rcu_dereference(nh->rt6i_exception_bucket); 1623cc5c073aSDavid Ahern 1624cc5c073aSDavid Ahern /* remove bucket flushed bit if set */ 1625cc5c073aSDavid Ahern if (bucket) { 1626cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1627cc5c073aSDavid Ahern 1628cc5c073aSDavid Ahern p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; 1629cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1630cc5c073aSDavid Ahern } 1631cc5c073aSDavid Ahern 1632cc5c073aSDavid Ahern return bucket; 1633cc5c073aSDavid Ahern } 1634cc5c073aSDavid Ahern 1635cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) 1636cc5c073aSDavid Ahern { 1637cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1638cc5c073aSDavid Ahern 1639cc5c073aSDavid Ahern return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); 1640cc5c073aSDavid Ahern } 1641cc5c073aSDavid Ahern 1642cc5c073aSDavid Ahern /* called with rt6_exception_lock held */ 1643cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, 1644cc5c073aSDavid Ahern spinlock_t *lock) 1645cc5c073aSDavid Ahern { 1646cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1647cc5c073aSDavid Ahern unsigned long p; 1648cc5c073aSDavid Ahern 1649cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1650cc5c073aSDavid Ahern lockdep_is_held(lock)); 1651cc5c073aSDavid Ahern 1652cc5c073aSDavid Ahern p = (unsigned long)bucket; 1653cc5c073aSDavid Ahern p |= FIB6_EXCEPTION_BUCKET_FLUSHED; 1654cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1655cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1656cc5c073aSDavid Ahern } 1657cc5c073aSDavid Ahern 165835732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 16595012f0a5SDavid Ahern const struct fib6_result *res) 166035732d01SWei Wang { 16615e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 166235732d01SWei Wang struct rt6_exception_bucket *bucket; 1663cc5c073aSDavid Ahern struct fib6_info *f6i = res->f6i; 166435732d01SWei Wang struct in6_addr *src_key = NULL; 166535732d01SWei Wang struct rt6_exception *rt6_ex; 1666cc5c073aSDavid Ahern struct fib6_nh *nh = res->nh; 166735732d01SWei Wang int err = 0; 166835732d01SWei Wang 166935732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 167035732d01SWei Wang 1671cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 167235732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 167335732d01SWei Wang if (!bucket) { 167435732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 167535732d01SWei Wang GFP_ATOMIC); 167635732d01SWei Wang if (!bucket) { 167735732d01SWei Wang err = -ENOMEM; 167835732d01SWei Wang goto out; 167935732d01SWei Wang } 1680cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1681cc5c073aSDavid Ahern } else if (fib6_nh_excptn_bucket_flushed(bucket)) { 1682cc5c073aSDavid Ahern err = -EINVAL; 1683cc5c073aSDavid Ahern goto out; 168435732d01SWei Wang } 168535732d01SWei Wang 168635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 16875012f0a5SDavid Ahern /* fib6_src.plen != 0 indicates f6i is in subtree 168835732d01SWei Wang * and exception table is indexed by a hash of 16895012f0a5SDavid Ahern * both fib6_dst and fib6_src. 169035732d01SWei Wang * Otherwise, the exception table is indexed by 16915012f0a5SDavid Ahern * a hash of only fib6_dst. 169235732d01SWei Wang */ 16935012f0a5SDavid Ahern if (f6i->fib6_src.plen) 169435732d01SWei Wang src_key = &nrt->rt6i_src.addr; 169535732d01SWei Wang #endif 16965012f0a5SDavid Ahern /* rt6_mtu_change() might lower mtu on f6i. 1697f5bbe7eeSWei Wang * Only insert this exception route if its mtu 16985012f0a5SDavid Ahern * is less than f6i's mtu value. 1699f5bbe7eeSWei Wang */ 1700b748f260SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1701f5bbe7eeSWei Wang err = -EINVAL; 1702f5bbe7eeSWei Wang goto out; 1703f5bbe7eeSWei Wang } 170460006a48SWei Wang 170535732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 170635732d01SWei Wang src_key); 170735732d01SWei Wang if (rt6_ex) 170835732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 170935732d01SWei Wang 171035732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 171135732d01SWei Wang if (!rt6_ex) { 171235732d01SWei Wang err = -ENOMEM; 171335732d01SWei Wang goto out; 171435732d01SWei Wang } 171535732d01SWei Wang rt6_ex->rt6i = nrt; 171635732d01SWei Wang rt6_ex->stamp = jiffies; 171735732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 171835732d01SWei Wang bucket->depth++; 171981eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 172035732d01SWei Wang 172135732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 172235732d01SWei Wang rt6_exception_remove_oldest(bucket); 172335732d01SWei Wang 172435732d01SWei Wang out: 172535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 172635732d01SWei Wang 172735732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1728b886d5f2SPaolo Abeni if (!err) { 17295012f0a5SDavid Ahern spin_lock_bh(&f6i->fib6_table->tb6_lock); 17305012f0a5SDavid Ahern fib6_update_sernum(net, f6i); 17315012f0a5SDavid Ahern spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1732b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1733b886d5f2SPaolo Abeni } 173435732d01SWei Wang 173535732d01SWei Wang return err; 173635732d01SWei Wang } 173735732d01SWei Wang 1738c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) 173935732d01SWei Wang { 174035732d01SWei Wang struct rt6_exception_bucket *bucket; 174135732d01SWei Wang struct rt6_exception *rt6_ex; 174235732d01SWei Wang struct hlist_node *tmp; 174335732d01SWei Wang int i; 174435732d01SWei Wang 174535732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 174635732d01SWei Wang 1747cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 174835732d01SWei Wang if (!bucket) 174935732d01SWei Wang goto out; 175035732d01SWei Wang 1751cc5c073aSDavid Ahern /* Prevent rt6_insert_exception() to recreate the bucket list */ 1752cc5c073aSDavid Ahern if (!from) 1753cc5c073aSDavid Ahern fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); 1754cc5c073aSDavid Ahern 175535732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1756cc5c073aSDavid Ahern hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { 1757cc5c073aSDavid Ahern if (!from || 1758cc5c073aSDavid Ahern rcu_access_pointer(rt6_ex->rt6i->from) == from) 175935732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1760cc5c073aSDavid Ahern } 1761cc5c073aSDavid Ahern WARN_ON_ONCE(!from && bucket->depth); 176235732d01SWei Wang bucket++; 176335732d01SWei Wang } 176435732d01SWei Wang out: 176535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 176635732d01SWei Wang } 176735732d01SWei Wang 1768e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) 1769e659ba31SDavid Ahern { 1770e659ba31SDavid Ahern struct fib6_info *f6i = arg; 1771e659ba31SDavid Ahern 1772e659ba31SDavid Ahern fib6_nh_flush_exceptions(nh, f6i); 1773e659ba31SDavid Ahern 1774e659ba31SDavid Ahern return 0; 1775e659ba31SDavid Ahern } 1776e659ba31SDavid Ahern 1777c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i) 1778c0b220cfSDavid Ahern { 1779e659ba31SDavid Ahern if (f6i->nh) 1780e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, 1781e659ba31SDavid Ahern f6i); 1782e659ba31SDavid Ahern else 17831cf844c7SDavid Ahern fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); 1784c0b220cfSDavid Ahern } 1785c0b220cfSDavid Ahern 178635732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 178735732d01SWei Wang * Caller has to hold rcu_read_lock() 178835732d01SWei Wang */ 17897e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1790510e2cedSWei Wang const struct in6_addr *daddr, 1791510e2cedSWei Wang const struct in6_addr *saddr) 179235732d01SWei Wang { 1793510e2cedSWei Wang const struct in6_addr *src_key = NULL; 179435732d01SWei Wang struct rt6_exception_bucket *bucket; 179535732d01SWei Wang struct rt6_exception *rt6_ex; 17967e4b5128SDavid Ahern struct rt6_info *ret = NULL; 179735732d01SWei Wang 179835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 17997e4b5128SDavid Ahern /* fib6i_src.plen != 0 indicates f6i is in subtree 180035732d01SWei Wang * and exception table is indexed by a hash of 18017e4b5128SDavid Ahern * both fib6_dst and fib6_src. 1802510e2cedSWei Wang * However, the src addr used to create the hash 1803510e2cedSWei Wang * might not be exactly the passed in saddr which 1804510e2cedSWei Wang * is a /128 addr from the flow. 1805510e2cedSWei Wang * So we need to use f6i->fib6_src to redo lookup 1806510e2cedSWei Wang * if the passed in saddr does not find anything. 1807510e2cedSWei Wang * (See the logic in ip6_rt_cache_alloc() on how 1808510e2cedSWei Wang * rt->rt6i_src is updated.) 180935732d01SWei Wang */ 18107e4b5128SDavid Ahern if (res->f6i->fib6_src.plen) 181135732d01SWei Wang src_key = saddr; 1812510e2cedSWei Wang find_ex: 181335732d01SWei Wang #endif 1814cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); 181535732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 181635732d01SWei Wang 181735732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 18187e4b5128SDavid Ahern ret = rt6_ex->rt6i; 181935732d01SWei Wang 1820510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES 1821510e2cedSWei Wang /* Use fib6_src as src_key and redo lookup */ 1822510e2cedSWei Wang if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1823510e2cedSWei Wang src_key = &res->f6i->fib6_src.addr; 1824510e2cedSWei Wang goto find_ex; 1825510e2cedSWei Wang } 1826510e2cedSWei Wang #endif 1827510e2cedSWei Wang 18287e4b5128SDavid Ahern return ret; 182935732d01SWei Wang } 183035732d01SWei Wang 183135732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 1832cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, 1833c0b220cfSDavid Ahern const struct rt6_info *rt) 183435732d01SWei Wang { 1835c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 183635732d01SWei Wang struct rt6_exception_bucket *bucket; 183735732d01SWei Wang struct rt6_exception *rt6_ex; 183835732d01SWei Wang int err; 183935732d01SWei Wang 1840cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 184135732d01SWei Wang return -ENOENT; 184235732d01SWei Wang 184335732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1844cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1845cc5c073aSDavid Ahern 184635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1847cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1848cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1849cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 185035732d01SWei Wang * Otherwise, the exception table is indexed by 185135732d01SWei Wang * a hash of only rt6i_dst. 185235732d01SWei Wang */ 1853c0b220cfSDavid Ahern if (plen) 185435732d01SWei Wang src_key = &rt->rt6i_src.addr; 185535732d01SWei Wang #endif 185635732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 185735732d01SWei Wang &rt->rt6i_dst.addr, 185835732d01SWei Wang src_key); 185935732d01SWei Wang if (rt6_ex) { 186035732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 186135732d01SWei Wang err = 0; 186235732d01SWei Wang } else { 186335732d01SWei Wang err = -ENOENT; 186435732d01SWei Wang } 186535732d01SWei Wang 186635732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 186735732d01SWei Wang return err; 186835732d01SWei Wang } 186935732d01SWei Wang 1870e659ba31SDavid Ahern struct fib6_nh_excptn_arg { 1871e659ba31SDavid Ahern struct rt6_info *rt; 1872e659ba31SDavid Ahern int plen; 1873e659ba31SDavid Ahern }; 1874e659ba31SDavid Ahern 1875e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) 1876e659ba31SDavid Ahern { 1877e659ba31SDavid Ahern struct fib6_nh_excptn_arg *arg = _arg; 1878e659ba31SDavid Ahern int err; 1879e659ba31SDavid Ahern 1880e659ba31SDavid Ahern err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); 1881e659ba31SDavid Ahern if (err == 0) 1882e659ba31SDavid Ahern return 1; 1883e659ba31SDavid Ahern 1884e659ba31SDavid Ahern return 0; 1885e659ba31SDavid Ahern } 1886e659ba31SDavid Ahern 1887c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 1888c0b220cfSDavid Ahern { 1889c0b220cfSDavid Ahern struct fib6_info *from; 1890c0b220cfSDavid Ahern 1891c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1892cc5c073aSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1893c0b220cfSDavid Ahern return -EINVAL; 1894c0b220cfSDavid Ahern 1895e659ba31SDavid Ahern if (from->nh) { 1896e659ba31SDavid Ahern struct fib6_nh_excptn_arg arg = { 1897e659ba31SDavid Ahern .rt = rt, 1898e659ba31SDavid Ahern .plen = from->fib6_src.plen 1899e659ba31SDavid Ahern }; 1900e659ba31SDavid Ahern int rc; 1901e659ba31SDavid Ahern 1902e659ba31SDavid Ahern /* rc = 1 means an entry was found */ 1903e659ba31SDavid Ahern rc = nexthop_for_each_fib6_nh(from->nh, 1904e659ba31SDavid Ahern rt6_nh_remove_exception_rt, 1905e659ba31SDavid Ahern &arg); 1906e659ba31SDavid Ahern return rc ? 0 : -ENOENT; 1907e659ba31SDavid Ahern } 1908e659ba31SDavid Ahern 19091cf844c7SDavid Ahern return fib6_nh_remove_exception(from->fib6_nh, 1910cc5c073aSDavid Ahern from->fib6_src.plen, rt); 1911c0b220cfSDavid Ahern } 1912c0b220cfSDavid Ahern 191335732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 191435732d01SWei Wang * refresh its stamp 191535732d01SWei Wang */ 1916cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, 1917c0b220cfSDavid Ahern const struct rt6_info *rt) 191835732d01SWei Wang { 1919c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 192035732d01SWei Wang struct rt6_exception_bucket *bucket; 192135732d01SWei Wang struct rt6_exception *rt6_ex; 1922193f3685SPaolo Abeni 1923cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, NULL); 192435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1925cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1926cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1927cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 192835732d01SWei Wang * Otherwise, the exception table is indexed by 192935732d01SWei Wang * a hash of only rt6i_dst. 193035732d01SWei Wang */ 1931c0b220cfSDavid Ahern if (plen) 193235732d01SWei Wang src_key = &rt->rt6i_src.addr; 193335732d01SWei Wang #endif 1934cc5c073aSDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); 193535732d01SWei Wang if (rt6_ex) 193635732d01SWei Wang rt6_ex->stamp = jiffies; 1937c0b220cfSDavid Ahern } 193835732d01SWei Wang 1939e659ba31SDavid Ahern struct fib6_nh_match_arg { 1940e659ba31SDavid Ahern const struct net_device *dev; 1941e659ba31SDavid Ahern const struct in6_addr *gw; 1942e659ba31SDavid Ahern struct fib6_nh *match; 1943e659ba31SDavid Ahern }; 1944e659ba31SDavid Ahern 1945e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */ 1946e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) 1947e659ba31SDavid Ahern { 1948e659ba31SDavid Ahern struct fib6_nh_match_arg *arg = _arg; 1949e659ba31SDavid Ahern 1950e659ba31SDavid Ahern if (arg->dev != nh->fib_nh_dev || 1951e659ba31SDavid Ahern (arg->gw && !nh->fib_nh_gw_family) || 1952e659ba31SDavid Ahern (!arg->gw && nh->fib_nh_gw_family) || 1953e659ba31SDavid Ahern (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) 1954e659ba31SDavid Ahern return 0; 1955e659ba31SDavid Ahern 1956e659ba31SDavid Ahern arg->match = nh; 1957e659ba31SDavid Ahern 1958e659ba31SDavid Ahern /* found a match, break the loop */ 1959e659ba31SDavid Ahern return 1; 1960e659ba31SDavid Ahern } 1961e659ba31SDavid Ahern 1962c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 1963c0b220cfSDavid Ahern { 1964c0b220cfSDavid Ahern struct fib6_info *from; 1965e659ba31SDavid Ahern struct fib6_nh *fib6_nh; 1966c0b220cfSDavid Ahern 1967c0b220cfSDavid Ahern rcu_read_lock(); 1968c0b220cfSDavid Ahern 1969c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1970c0b220cfSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1971c0b220cfSDavid Ahern goto unlock; 1972c0b220cfSDavid Ahern 1973e659ba31SDavid Ahern if (from->nh) { 1974e659ba31SDavid Ahern struct fib6_nh_match_arg arg = { 1975e659ba31SDavid Ahern .dev = rt->dst.dev, 1976e659ba31SDavid Ahern .gw = &rt->rt6i_gateway, 1977e659ba31SDavid Ahern }; 1978e659ba31SDavid Ahern 1979e659ba31SDavid Ahern nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); 1980e659ba31SDavid Ahern 1981e659ba31SDavid Ahern if (!arg.match) 1982cff6a327SDavid Ahern goto unlock; 1983e659ba31SDavid Ahern fib6_nh = arg.match; 1984e659ba31SDavid Ahern } else { 1985e659ba31SDavid Ahern fib6_nh = from->fib6_nh; 1986e659ba31SDavid Ahern } 1987e659ba31SDavid Ahern fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); 1988193f3685SPaolo Abeni unlock: 198935732d01SWei Wang rcu_read_unlock(); 199035732d01SWei Wang } 199135732d01SWei Wang 1992e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1993e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1994e9fa1495SStefano Brivio { 1995e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1996e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1997e9fa1495SStefano Brivio * reflect PMTU decreases. 1998e9fa1495SStefano Brivio * 1999e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 2000e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 2001e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 2002e9fa1495SStefano Brivio * handle this. 2003e9fa1495SStefano Brivio */ 2004e9fa1495SStefano Brivio 2005e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 2006e9fa1495SStefano Brivio return true; 2007e9fa1495SStefano Brivio 2008e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 2009e9fa1495SStefano Brivio return true; 2010e9fa1495SStefano Brivio 2011e9fa1495SStefano Brivio return false; 2012e9fa1495SStefano Brivio } 2013e9fa1495SStefano Brivio 2014e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 2015cc5c073aSDavid Ahern const struct fib6_nh *nh, int mtu) 2016f5bbe7eeSWei Wang { 2017f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 2018f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 2019f5bbe7eeSWei Wang int i; 2020f5bbe7eeSWei Wang 2021cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2022e9fa1495SStefano Brivio if (!bucket) 2023e9fa1495SStefano Brivio return; 2024e9fa1495SStefano Brivio 2025f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2026f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 2027f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2028e9fa1495SStefano Brivio 2029e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 2030d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 2031f5bbe7eeSWei Wang * been updated. 2032f5bbe7eeSWei Wang */ 2033d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 2034e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 2035d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 2036f5bbe7eeSWei Wang } 2037f5bbe7eeSWei Wang bucket++; 2038f5bbe7eeSWei Wang } 2039f5bbe7eeSWei Wang } 2040f5bbe7eeSWei Wang 2041b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2042b16cb459SWei Wang 2043cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, 2044cc5c073aSDavid Ahern const struct in6_addr *gateway) 2045b16cb459SWei Wang { 2046b16cb459SWei Wang struct rt6_exception_bucket *bucket; 2047b16cb459SWei Wang struct rt6_exception *rt6_ex; 2048b16cb459SWei Wang struct hlist_node *tmp; 2049b16cb459SWei Wang int i; 2050b16cb459SWei Wang 2051cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2052b16cb459SWei Wang return; 2053b16cb459SWei Wang 2054b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 2055cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2056b16cb459SWei Wang if (bucket) { 2057b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2058b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2059b16cb459SWei Wang &bucket->chain, hlist) { 2060b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2061b16cb459SWei Wang 2062b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 2063b16cb459SWei Wang RTF_CACHE_GATEWAY && 2064b16cb459SWei Wang ipv6_addr_equal(gateway, 2065b16cb459SWei Wang &entry->rt6i_gateway)) { 2066b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 2067b16cb459SWei Wang } 2068b16cb459SWei Wang } 2069b16cb459SWei Wang bucket++; 2070b16cb459SWei Wang } 2071b16cb459SWei Wang } 2072b16cb459SWei Wang 2073b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 2074b16cb459SWei Wang } 2075b16cb459SWei Wang 2076c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 2077c757faa8SWei Wang struct rt6_exception *rt6_ex, 2078c757faa8SWei Wang struct fib6_gc_args *gc_args, 2079c757faa8SWei Wang unsigned long now) 2080c757faa8SWei Wang { 2081c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 2082c757faa8SWei Wang 20831859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 20841859bac0SPaolo Abeni * even if others have still references to them, so that on next 20851859bac0SPaolo Abeni * dst_check() such references can be dropped. 20861859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 20871859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 20881859bac0SPaolo Abeni */ 208931afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 209031afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 2091c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 2092c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2093c757faa8SWei Wang return; 209431afeb42SWei Wang } 209531afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 209631afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 209731afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 209831afeb42SWei Wang return; 209931afeb42SWei Wang } 210031afeb42SWei Wang 210131afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 2102c757faa8SWei Wang struct neighbour *neigh; 2103c757faa8SWei Wang __u8 neigh_flags = 0; 2104c757faa8SWei Wang 21051bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 21061bfa26ffSEric Dumazet if (neigh) 2107c757faa8SWei Wang neigh_flags = neigh->flags; 21081bfa26ffSEric Dumazet 2109c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 2110c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 2111c757faa8SWei Wang rt); 2112c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2113c757faa8SWei Wang return; 2114c757faa8SWei Wang } 2115c757faa8SWei Wang } 211631afeb42SWei Wang 2117c757faa8SWei Wang gc_args->more++; 2118c757faa8SWei Wang } 2119c757faa8SWei Wang 2120cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh, 2121c757faa8SWei Wang struct fib6_gc_args *gc_args, 2122c757faa8SWei Wang unsigned long now) 2123c757faa8SWei Wang { 2124c757faa8SWei Wang struct rt6_exception_bucket *bucket; 2125c757faa8SWei Wang struct rt6_exception *rt6_ex; 2126c757faa8SWei Wang struct hlist_node *tmp; 2127c757faa8SWei Wang int i; 2128c757faa8SWei Wang 2129cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2130c757faa8SWei Wang return; 2131c757faa8SWei Wang 21321bfa26ffSEric Dumazet rcu_read_lock_bh(); 21331bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 2134cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2135c757faa8SWei Wang if (bucket) { 2136c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2137c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2138c757faa8SWei Wang &bucket->chain, hlist) { 2139c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 2140c757faa8SWei Wang gc_args, now); 2141c757faa8SWei Wang } 2142c757faa8SWei Wang bucket++; 2143c757faa8SWei Wang } 2144c757faa8SWei Wang } 21451bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 21461bfa26ffSEric Dumazet rcu_read_unlock_bh(); 2147c757faa8SWei Wang } 2148c757faa8SWei Wang 2149e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg { 2150e659ba31SDavid Ahern struct fib6_gc_args *gc_args; 2151e659ba31SDavid Ahern unsigned long now; 2152e659ba31SDavid Ahern }; 2153e659ba31SDavid Ahern 2154e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) 2155e659ba31SDavid Ahern { 2156e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg *arg = _arg; 2157e659ba31SDavid Ahern 2158e659ba31SDavid Ahern fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); 2159e659ba31SDavid Ahern return 0; 2160e659ba31SDavid Ahern } 2161e659ba31SDavid Ahern 2162cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i, 2163c0b220cfSDavid Ahern struct fib6_gc_args *gc_args, 2164c0b220cfSDavid Ahern unsigned long now) 2165c0b220cfSDavid Ahern { 2166e659ba31SDavid Ahern if (f6i->nh) { 2167e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg arg = { 2168e659ba31SDavid Ahern .gc_args = gc_args, 2169e659ba31SDavid Ahern .now = now 2170e659ba31SDavid Ahern }; 2171e659ba31SDavid Ahern 2172e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, 2173e659ba31SDavid Ahern &arg); 2174e659ba31SDavid Ahern } else { 21751cf844c7SDavid Ahern fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); 2176c0b220cfSDavid Ahern } 2177e659ba31SDavid Ahern } 2178c0b220cfSDavid Ahern 21791d053da9SDavid Ahern /* must be called with rcu lock held */ 2180effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 2181effda4ddSDavid Ahern struct flowi6 *fl6, struct fib6_result *res, int strict) 21821da177e4SLinus Torvalds { 2183367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 21841da177e4SLinus Torvalds 21856454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2186367efcb9SMartin KaFai Lau saved_fn = fn; 21871da177e4SLinus Torvalds 2188ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 2189ca254490SDavid Ahern oif = 0; 2190ca254490SDavid Ahern 2191a3c00e46SMartin KaFai Lau redo_rt6_select: 2192effda4ddSDavid Ahern rt6_select(net, fn, oif, res, strict); 2193effda4ddSDavid Ahern if (res->f6i == net->ipv6.fib6_null_entry) { 2194a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2195a3c00e46SMartin KaFai Lau if (fn) 2196a3c00e46SMartin KaFai Lau goto redo_rt6_select; 2197367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 2198367efcb9SMartin KaFai Lau /* also consider unreachable route */ 2199367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 2200367efcb9SMartin KaFai Lau fn = saved_fn; 2201367efcb9SMartin KaFai Lau goto redo_rt6_select; 2202367efcb9SMartin KaFai Lau } 2203a3c00e46SMartin KaFai Lau } 2204a3c00e46SMartin KaFai Lau 2205effda4ddSDavid Ahern trace_fib6_table_lookup(net, res, table, fl6); 2206d52d3997SMartin KaFai Lau 2207effda4ddSDavid Ahern return 0; 22081d053da9SDavid Ahern } 22091d053da9SDavid Ahern 22101d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 22111d053da9SDavid Ahern int oif, struct flowi6 *fl6, 22121d053da9SDavid Ahern const struct sk_buff *skb, int flags) 22131d053da9SDavid Ahern { 2214b1d40991SDavid Ahern struct fib6_result res = {}; 22150e09edccSWei Wang struct rt6_info *rt = NULL; 22161d053da9SDavid Ahern int strict = 0; 22171d053da9SDavid Ahern 22180e09edccSWei Wang WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && 22190e09edccSWei Wang !rcu_read_lock_held()); 22200e09edccSWei Wang 22211d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 22221d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 22231d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 22241d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 22251d053da9SDavid Ahern 22261d053da9SDavid Ahern rcu_read_lock(); 22271d053da9SDavid Ahern 2228effda4ddSDavid Ahern fib6_table_lookup(net, table, oif, fl6, &res, strict); 22290e09edccSWei Wang if (res.f6i == net->ipv6.fib6_null_entry) 22300e09edccSWei Wang goto out; 223123fb93a4SDavid Ahern 2232b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, oif, false, skb, strict); 2233d83009d4SDavid Ahern 223423fb93a4SDavid Ahern /*Search through exception table */ 22357e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 223623fb93a4SDavid Ahern if (rt) { 22370e09edccSWei Wang goto out; 22383da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 2239b1d40991SDavid Ahern !res.nh->fib_nh_gw_family)) { 22403da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 22413da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 22423da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 22433da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 22443da59bd9SMartin KaFai Lau */ 22450e09edccSWei Wang rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 22463da59bd9SMartin KaFai Lau 22470e09edccSWei Wang if (rt) { 22480e09edccSWei Wang /* 1 refcnt is taken during ip6_rt_cache_alloc(). 22490e09edccSWei Wang * As rt6_uncached_list_add() does not consume refcnt, 22500e09edccSWei Wang * this refcnt is always returned to the caller even 22510e09edccSWei Wang * if caller sets RT6_LOOKUP_F_DST_NOREF flag. 22520e09edccSWei Wang */ 22530e09edccSWei Wang rt6_uncached_list_add(rt); 22540e09edccSWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 22554d85cd0cSDavid Ahern rcu_read_unlock(); 22563da59bd9SMartin KaFai Lau 22570e09edccSWei Wang return rt; 22581cfb71eeSWei Wang } 2259d52d3997SMartin KaFai Lau } else { 2260d52d3997SMartin KaFai Lau /* Get a percpu copy */ 2261951f788aSEric Dumazet local_bh_disable(); 22620e09edccSWei Wang rt = rt6_get_pcpu_route(&res); 2263d52d3997SMartin KaFai Lau 22640e09edccSWei Wang if (!rt) 22650e09edccSWei Wang rt = rt6_make_pcpu_route(net, &res); 226693531c67SDavid Ahern 2267951f788aSEric Dumazet local_bh_enable(); 22680e09edccSWei Wang } 22690e09edccSWei Wang out: 22700e09edccSWei Wang if (!rt) 22710e09edccSWei Wang rt = net->ipv6.ip6_null_entry; 22720e09edccSWei Wang if (!(flags & RT6_LOOKUP_F_DST_NOREF)) 22730e09edccSWei Wang ip6_hold_safe(net, &rt); 2274951f788aSEric Dumazet rcu_read_unlock(); 2275d4bea421SDavid Ahern 22760e09edccSWei Wang return rt; 2277c71099acSThomas Graf } 22789ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 2279c71099acSThomas Graf 2280b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 2281b75cc8f9SDavid Ahern struct fib6_table *table, 2282b75cc8f9SDavid Ahern struct flowi6 *fl6, 2283b75cc8f9SDavid Ahern const struct sk_buff *skb, 2284b75cc8f9SDavid Ahern int flags) 22854acad72dSPavel Emelyanov { 2286b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 22874acad72dSPavel Emelyanov } 22884acad72dSPavel Emelyanov 2289d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 229072331bc0SShmulik Ladkani struct net_device *dev, 2291b75cc8f9SDavid Ahern struct flowi6 *fl6, 2292b75cc8f9SDavid Ahern const struct sk_buff *skb, 2293b75cc8f9SDavid Ahern int flags) 229472331bc0SShmulik Ladkani { 229572331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 229672331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 229772331bc0SShmulik Ladkani 2298b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 229972331bc0SShmulik Ladkani } 2300d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 230172331bc0SShmulik Ladkani 230223aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 23035e5d6fedSRoopa Prabhu struct flow_keys *keys, 23045e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 230523aebdacSJakub Sitnicki { 230623aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 230723aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 23085e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 230923aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 231023aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 231123aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 2312cea67a2dSEric Dumazet struct icmp6hdr _icmph; 231323aebdacSJakub Sitnicki 231423aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 231523aebdacSJakub Sitnicki goto out; 231623aebdacSJakub Sitnicki 2317cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2318cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 2319cea67a2dSEric Dumazet if (!icmph) 2320cea67a2dSEric Dumazet goto out; 2321cea67a2dSEric Dumazet 232254074f1dSMatteo Croce if (!icmpv6_is_err(icmph->icmp6_type)) 232323aebdacSJakub Sitnicki goto out; 232423aebdacSJakub Sitnicki 232523aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 232623aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 232723aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 232823aebdacSJakub Sitnicki if (!inner_iph) 232923aebdacSJakub Sitnicki goto out; 233023aebdacSJakub Sitnicki 233123aebdacSJakub Sitnicki key_iph = inner_iph; 23325e5d6fedSRoopa Prabhu _flkeys = NULL; 233323aebdacSJakub Sitnicki out: 23345e5d6fedSRoopa Prabhu if (_flkeys) { 23355e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 23365e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 23375e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 23385e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 23395e5d6fedSRoopa Prabhu } else { 234023aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 234123aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 2342fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 234323aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 234423aebdacSJakub Sitnicki } 23455e5d6fedSRoopa Prabhu } 234623aebdacSJakub Sitnicki 234723aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 2348b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2349b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 235023aebdacSJakub Sitnicki { 235123aebdacSJakub Sitnicki struct flow_keys hash_keys; 23529a2a537aSDavid Ahern u32 mhash; 235323aebdacSJakub Sitnicki 2354bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 2355b4bac172SDavid Ahern case 0: 23566f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 23576f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 23589a2a537aSDavid Ahern if (skb) { 23595e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 23609a2a537aSDavid Ahern } else { 23619a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 23629a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2363fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 23649a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 236523aebdacSJakub Sitnicki } 2366b4bac172SDavid Ahern break; 2367b4bac172SDavid Ahern case 1: 2368b4bac172SDavid Ahern if (skb) { 2369b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2370b4bac172SDavid Ahern struct flow_keys keys; 2371b4bac172SDavid Ahern 2372b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2373b4bac172SDavid Ahern if (skb->l4_hash) 2374b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2375b4bac172SDavid Ahern 2376b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2377b4bac172SDavid Ahern 2378b4bac172SDavid Ahern if (!flkeys) { 2379b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2380b4bac172SDavid Ahern flkeys = &keys; 2381b4bac172SDavid Ahern } 2382b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2383b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2384b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2385b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2386b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2387b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2388b4bac172SDavid Ahern } else { 2389b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2390b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2391b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2392b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2393b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2394b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2395b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2396b4bac172SDavid Ahern } 2397b4bac172SDavid Ahern break; 2398d8f74f09SStephen Suryaputra case 2: 2399d8f74f09SStephen Suryaputra memset(&hash_keys, 0, sizeof(hash_keys)); 2400d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2401d8f74f09SStephen Suryaputra if (skb) { 2402d8f74f09SStephen Suryaputra struct flow_keys keys; 2403d8f74f09SStephen Suryaputra 2404d8f74f09SStephen Suryaputra if (!flkeys) { 2405d8f74f09SStephen Suryaputra skb_flow_dissect_flow_keys(skb, &keys, 0); 2406d8f74f09SStephen Suryaputra flkeys = &keys; 2407d8f74f09SStephen Suryaputra } 2408d8f74f09SStephen Suryaputra 2409d8f74f09SStephen Suryaputra /* Inner can be v4 or v6 */ 2410d8f74f09SStephen Suryaputra if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2411d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2412d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2413d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2414d8f74f09SStephen Suryaputra } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2415d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2416d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2417d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2418d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = flkeys->tags.flow_label; 2419d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2420d8f74f09SStephen Suryaputra } else { 2421d8f74f09SStephen Suryaputra /* Same as case 0 */ 2422d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2423d8f74f09SStephen Suryaputra ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 2424d8f74f09SStephen Suryaputra } 2425d8f74f09SStephen Suryaputra } else { 2426d8f74f09SStephen Suryaputra /* Same as case 0 */ 2427d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2428d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = fl6->saddr; 2429d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = fl6->daddr; 2430d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2431d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = fl6->flowi6_proto; 2432d8f74f09SStephen Suryaputra } 2433d8f74f09SStephen Suryaputra break; 2434b4bac172SDavid Ahern } 24359a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 243623aebdacSJakub Sitnicki 24379a2a537aSDavid Ahern return mhash >> 1; 243823aebdacSJakub Sitnicki } 243923aebdacSJakub Sitnicki 244067f415ddSWei Wang /* Called with rcu held */ 2441c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2442c71099acSThomas Graf { 2443b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2444c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 244567f415ddSWei Wang int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; 2446904af04dSJiri Benc struct ip_tunnel_info *tun_info; 24474c9483b2SDavid S. Miller struct flowi6 fl6 = { 2448e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 24494c9483b2SDavid S. Miller .daddr = iph->daddr, 24504c9483b2SDavid S. Miller .saddr = iph->saddr, 24516502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 24524c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 24534c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2454c71099acSThomas Graf }; 24555e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2456adaa70bbSThomas Graf 2457904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 245846fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2459904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 24605e5d6fedSRoopa Prabhu 24615e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 24625e5d6fedSRoopa Prabhu flkeys = &_flkeys; 24635e5d6fedSRoopa Prabhu 246423aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2465b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 246606e9d040SJiri Benc skb_dst_drop(skb); 246767f415ddSWei Wang skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, 246867f415ddSWei Wang &fl6, skb, flags)); 2469c71099acSThomas Graf } 2470c71099acSThomas Graf 2471b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2472b75cc8f9SDavid Ahern struct fib6_table *table, 2473b75cc8f9SDavid Ahern struct flowi6 *fl6, 2474b75cc8f9SDavid Ahern const struct sk_buff *skb, 2475b75cc8f9SDavid Ahern int flags) 2476c71099acSThomas Graf { 2477b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2478c71099acSThomas Graf } 2479c71099acSThomas Graf 24807d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags_noref(struct net *net, 24817d9e5f42SWei Wang const struct sock *sk, 24826f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2483c71099acSThomas Graf { 2484d46a9d67SDavid Ahern bool any_src; 2485c71099acSThomas Graf 24863ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 24873ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 24884c1feac5SDavid Ahern struct dst_entry *dst; 24894c1feac5SDavid Ahern 24907d9e5f42SWei Wang /* This function does not take refcnt on the dst */ 24914c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2492ca254490SDavid Ahern if (dst) 2493ca254490SDavid Ahern return dst; 24944c1feac5SDavid Ahern } 2495ca254490SDavid Ahern 24961fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 24974dc27d1cSDavid McCullough 24987d9e5f42SWei Wang flags |= RT6_LOOKUP_F_DST_NOREF; 2499d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2500741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2501d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 250277d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2503c71099acSThomas Graf 2504d46a9d67SDavid Ahern if (!any_src) 2505adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 25060c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 25070c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2508adaa70bbSThomas Graf 2509b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 25101da177e4SLinus Torvalds } 25117d9e5f42SWei Wang EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref); 25127d9e5f42SWei Wang 25137d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net, 25147d9e5f42SWei Wang const struct sock *sk, 25157d9e5f42SWei Wang struct flowi6 *fl6, 25167d9e5f42SWei Wang int flags) 25177d9e5f42SWei Wang { 25187d9e5f42SWei Wang struct dst_entry *dst; 25197d9e5f42SWei Wang struct rt6_info *rt6; 25207d9e5f42SWei Wang 25217d9e5f42SWei Wang rcu_read_lock(); 25227d9e5f42SWei Wang dst = ip6_route_output_flags_noref(net, sk, fl6, flags); 25237d9e5f42SWei Wang rt6 = (struct rt6_info *)dst; 25247d9e5f42SWei Wang /* For dst cached in uncached_list, refcnt is already taken. */ 25257d9e5f42SWei Wang if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) { 25267d9e5f42SWei Wang dst = &net->ipv6.ip6_null_entry->dst; 25277d9e5f42SWei Wang dst_hold(dst); 25287d9e5f42SWei Wang } 25297d9e5f42SWei Wang rcu_read_unlock(); 25307d9e5f42SWei Wang 25317d9e5f42SWei Wang return dst; 25327d9e5f42SWei Wang } 25336f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 25341da177e4SLinus Torvalds 25352774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 253614e50e57SDavid S. Miller { 25375c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 25381dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 253914e50e57SDavid S. Miller struct dst_entry *new = NULL; 254014e50e57SDavid S. Miller 25411dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 254262cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 254314e50e57SDavid S. Miller if (rt) { 25440a1f5962SMartin KaFai Lau rt6_info_init(rt); 254581eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 25460a1f5962SMartin KaFai Lau 2547d8d1f30bSChangli Gao new = &rt->dst; 254814e50e57SDavid S. Miller new->__use = 1; 2549352e512cSHerbert Xu new->input = dst_discard; 2550ede2059dSEric W. Biederman new->output = dst_discard_out; 255114e50e57SDavid S. Miller 2552defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 255314e50e57SDavid S. Miller 25541dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 25554e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 25560a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 255714e50e57SDavid S. Miller 255814e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 255914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 256014e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 256114e50e57SDavid S. Miller #endif 256214e50e57SDavid S. Miller } 256314e50e57SDavid S. Miller 256469ead7afSDavid S. Miller dst_release(dst_orig); 256569ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 256614e50e57SDavid S. Miller } 256714e50e57SDavid S. Miller 25681da177e4SLinus Torvalds /* 25691da177e4SLinus Torvalds * Destination cache support functions 25701da177e4SLinus Torvalds */ 25711da177e4SLinus Torvalds 25728d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 25733da59bd9SMartin KaFai Lau { 257436143645SSteffen Klassert u32 rt_cookie = 0; 2575c5cff856SWei Wang 25768ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 257793531c67SDavid Ahern return false; 257893531c67SDavid Ahern 257993531c67SDavid Ahern if (fib6_check_expired(f6i)) 258093531c67SDavid Ahern return false; 258193531c67SDavid Ahern 258293531c67SDavid Ahern return true; 258393531c67SDavid Ahern } 258493531c67SDavid Ahern 2585a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2586a68886a6SDavid Ahern struct fib6_info *from, 2587a68886a6SDavid Ahern u32 cookie) 25883da59bd9SMartin KaFai Lau { 2589c5cff856SWei Wang u32 rt_cookie = 0; 2590c5cff856SWei Wang 259149d05fe2SDavid Ahern if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || 259293531c67SDavid Ahern rt_cookie != cookie) 25933da59bd9SMartin KaFai Lau return NULL; 25943da59bd9SMartin KaFai Lau 25953da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 25963da59bd9SMartin KaFai Lau return NULL; 25973da59bd9SMartin KaFai Lau 25983da59bd9SMartin KaFai Lau return &rt->dst; 25993da59bd9SMartin KaFai Lau } 26003da59bd9SMartin KaFai Lau 2601a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2602a68886a6SDavid Ahern struct fib6_info *from, 2603a68886a6SDavid Ahern u32 cookie) 26043da59bd9SMartin KaFai Lau { 26055973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 26065973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2607a68886a6SDavid Ahern fib6_check(from, cookie)) 26083da59bd9SMartin KaFai Lau return &rt->dst; 26093da59bd9SMartin KaFai Lau else 26103da59bd9SMartin KaFai Lau return NULL; 26113da59bd9SMartin KaFai Lau } 26123da59bd9SMartin KaFai Lau 26131da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 26141da177e4SLinus Torvalds { 2615a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2616a68886a6SDavid Ahern struct fib6_info *from; 26171da177e4SLinus Torvalds struct rt6_info *rt; 26181da177e4SLinus Torvalds 2619a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2620a87b7dc9SDavid Ahern 26218f34e53bSDavid Ahern if (rt->sernum) 26228f34e53bSDavid Ahern return rt6_is_valid(rt) ? dst : NULL; 26238f34e53bSDavid Ahern 2624a87b7dc9SDavid Ahern rcu_read_lock(); 26251da177e4SLinus Torvalds 26266f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 26276f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 26286f3118b5SNicolas Dichtel * into this function always. 26296f3118b5SNicolas Dichtel */ 2630e3bc10bdSHannes Frederic Sowa 2631a68886a6SDavid Ahern from = rcu_dereference(rt->from); 26324b32b5adSMartin KaFai Lau 2633a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2634a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2635a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 26363da59bd9SMartin KaFai Lau else 2637a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2638a87b7dc9SDavid Ahern 2639a87b7dc9SDavid Ahern rcu_read_unlock(); 2640a87b7dc9SDavid Ahern 2641a87b7dc9SDavid Ahern return dst_ret; 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 26451da177e4SLinus Torvalds { 26461da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds if (rt) { 264954c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2650c3c14da0SDavid Ahern rcu_read_lock(); 265154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 265293531c67SDavid Ahern rt6_remove_exception_rt(rt); 265354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 26541da177e4SLinus Torvalds } 2655c3c14da0SDavid Ahern rcu_read_unlock(); 265654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 265754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 265854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 265954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 266054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 266154c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 26651da177e4SLinus Torvalds { 26661da177e4SLinus Torvalds struct rt6_info *rt; 26671da177e4SLinus Torvalds 26683ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 26691da177e4SLinus Torvalds 2670adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 26711da177e4SLinus Torvalds if (rt) { 26728a14e46fSDavid Ahern rcu_read_lock(); 26731eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 267493531c67SDavid Ahern rt6_remove_exception_rt(rt); 2675c5cff856SWei Wang } else { 2676a68886a6SDavid Ahern struct fib6_info *from; 2677c5cff856SWei Wang struct fib6_node *fn; 2678c5cff856SWei Wang 2679a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2680a68886a6SDavid Ahern if (from) { 2681a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2682c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2683c5cff856SWei Wang fn->fn_sernum = -1; 2684a68886a6SDavid Ahern } 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds rcu_read_unlock(); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26906a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 26916a3e030fSDavid Ahern { 2692a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2693a68886a6SDavid Ahern struct fib6_info *from; 2694a68886a6SDavid Ahern 2695a68886a6SDavid Ahern rcu_read_lock(); 2696a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2697a68886a6SDavid Ahern if (from) 2698a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2699a68886a6SDavid Ahern rcu_read_unlock(); 2700a68886a6SDavid Ahern } 27016a3e030fSDavid Ahern 27026a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 27036a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 27046700c270SDavid S. Miller } 27051da177e4SLinus Torvalds 270645e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 270745e4fd26SMartin KaFai Lau { 270845e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 270945e4fd26SMartin KaFai Lau 2710d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 271145e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 271245e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 271345e4fd26SMartin KaFai Lau } 271445e4fd26SMartin KaFai Lau 27150d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 27160d3f6d29SMartin KaFai Lau { 27170d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 27181490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 27190d3f6d29SMartin KaFai Lau } 27200d3f6d29SMartin KaFai Lau 272145e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 2722bd085ef6SHangbin Liu const struct ipv6hdr *iph, u32 mtu, 2723bd085ef6SHangbin Liu bool confirm_neigh) 27241da177e4SLinus Torvalds { 27250dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 27261da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 27271da177e4SLinus Torvalds 272809454fd0SMaciej Żenczykowski /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU) 272909454fd0SMaciej Żenczykowski * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it. 273009454fd0SMaciej Żenczykowski * [see also comment in rt6_mtu_change_route()] 273109454fd0SMaciej Żenczykowski */ 273219bda36cSXin Long 273345e4fd26SMartin KaFai Lau if (iph) { 273445e4fd26SMartin KaFai Lau daddr = &iph->daddr; 273545e4fd26SMartin KaFai Lau saddr = &iph->saddr; 273645e4fd26SMartin KaFai Lau } else if (sk) { 273745e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 273845e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 273945e4fd26SMartin KaFai Lau } else { 27400dec879fSJulian Anastasov daddr = NULL; 27410dec879fSJulian Anastasov saddr = NULL; 27421da177e4SLinus Torvalds } 2743bd085ef6SHangbin Liu 2744bd085ef6SHangbin Liu if (confirm_neigh) 27450dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 2746bd085ef6SHangbin Liu 27470dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 27480dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 27490dec879fSJulian Anastasov return; 27500dec879fSJulian Anastasov 27510dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 27520dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 27532b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 27542b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 27552b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 27560dec879fSJulian Anastasov } else if (daddr) { 275785bd05deSDavid Ahern struct fib6_result res = {}; 27580dec879fSJulian Anastasov struct rt6_info *nrt6; 27590dec879fSJulian Anastasov 27604d85cd0cSDavid Ahern rcu_read_lock(); 276185bd05deSDavid Ahern res.f6i = rcu_dereference(rt6->from); 276243a4b60dSDavid Ahern if (!res.f6i) 276343a4b60dSDavid Ahern goto out_unlock; 276443a4b60dSDavid Ahern 27657d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 27667d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 27677d21fec9SDavid Ahern 27682d44234bSDavid Ahern if (res.f6i->nh) { 27692d44234bSDavid Ahern struct fib6_nh_match_arg arg = { 27702d44234bSDavid Ahern .dev = dst->dev, 27712d44234bSDavid Ahern .gw = &rt6->rt6i_gateway, 27722d44234bSDavid Ahern }; 27732d44234bSDavid Ahern 27742d44234bSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 27752d44234bSDavid Ahern fib6_nh_find_match, &arg); 27762d44234bSDavid Ahern 27772d44234bSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 27782d44234bSDavid Ahern * using the dst->dev + gw. Should be impossible. 27792d44234bSDavid Ahern */ 278043a4b60dSDavid Ahern if (!arg.match) 278143a4b60dSDavid Ahern goto out_unlock; 27822d44234bSDavid Ahern 27832d44234bSDavid Ahern res.nh = arg.match; 27842d44234bSDavid Ahern } else { 27852d44234bSDavid Ahern res.nh = res.f6i->fib6_nh; 27862d44234bSDavid Ahern } 27872d44234bSDavid Ahern 278885bd05deSDavid Ahern nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 278945e4fd26SMartin KaFai Lau if (nrt6) { 279045e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 27915012f0a5SDavid Ahern if (rt6_insert_exception(nrt6, &res)) 27922b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 279345e4fd26SMartin KaFai Lau } 279443a4b60dSDavid Ahern out_unlock: 2795a68886a6SDavid Ahern rcu_read_unlock(); 279645e4fd26SMartin KaFai Lau } 279745e4fd26SMartin KaFai Lau } 279845e4fd26SMartin KaFai Lau 279945e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 2800bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 2801bd085ef6SHangbin Liu bool confirm_neigh) 280245e4fd26SMartin KaFai Lau { 2803bd085ef6SHangbin Liu __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, 2804bd085ef6SHangbin Liu confirm_neigh); 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 280742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2808e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 280981aded24SDavid S. Miller { 281081aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 281181aded24SDavid S. Miller struct dst_entry *dst; 2812dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2813dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2814dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2815dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2816dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2817dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2818dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2819dc92095dSMaciej Żenczykowski }; 282081aded24SDavid S. Miller 282181aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 282281aded24SDavid S. Miller if (!dst->error) 2823bd085ef6SHangbin Liu __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); 282481aded24SDavid S. Miller dst_release(dst); 282581aded24SDavid S. Miller } 282681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 282781aded24SDavid S. Miller 282881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 282981aded24SDavid S. Miller { 28307ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 283133c162a9SMartin KaFai Lau struct dst_entry *dst; 283233c162a9SMartin KaFai Lau 28337ddacfa5SDavid Ahern if (!oif && skb->dev) 28347ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 28357ddacfa5SDavid Ahern 28367ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 283733c162a9SMartin KaFai Lau 283833c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 283933c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 284033c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 284133c162a9SMartin KaFai Lau return; 284233c162a9SMartin KaFai Lau 284333c162a9SMartin KaFai Lau bh_lock_sock(sk); 284433c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 284533c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 284633c162a9SMartin KaFai Lau bh_unlock_sock(sk); 284781aded24SDavid S. Miller } 284881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 284981aded24SDavid S. Miller 28507d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 28517d6850f7SAlexey Kodanev const struct flowi6 *fl6) 28527d6850f7SAlexey Kodanev { 28537d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 28547d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 28557d6850f7SAlexey Kodanev #endif 28567d6850f7SAlexey Kodanev 28577d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 28587d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 28597d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 28607d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 28617d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 28627d6850f7SAlexey Kodanev &np->saddr : 28637d6850f7SAlexey Kodanev #endif 28647d6850f7SAlexey Kodanev NULL); 28657d6850f7SAlexey Kodanev } 28667d6850f7SAlexey Kodanev 28679b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res, 28680b34eb00SDavid Ahern struct flowi6 *fl6, 28690b34eb00SDavid Ahern const struct in6_addr *gw, 28700b34eb00SDavid Ahern struct rt6_info **ret) 28710b34eb00SDavid Ahern { 28729b6b35abSDavid Ahern const struct fib6_nh *nh = res->nh; 28739b6b35abSDavid Ahern 28740b34eb00SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 28750b34eb00SDavid Ahern fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 28760b34eb00SDavid Ahern return false; 28770b34eb00SDavid Ahern 28780b34eb00SDavid Ahern /* rt_cache's gateway might be different from its 'parent' 28790b34eb00SDavid Ahern * in the case of an ip redirect. 28800b34eb00SDavid Ahern * So we keep searching in the exception table if the gateway 28810b34eb00SDavid Ahern * is different. 28820b34eb00SDavid Ahern */ 28830b34eb00SDavid Ahern if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 28840b34eb00SDavid Ahern struct rt6_info *rt_cache; 28850b34eb00SDavid Ahern 28869b6b35abSDavid Ahern rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 28870b34eb00SDavid Ahern if (rt_cache && 28880b34eb00SDavid Ahern ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 28890b34eb00SDavid Ahern *ret = rt_cache; 28900b34eb00SDavid Ahern return true; 28910b34eb00SDavid Ahern } 28920b34eb00SDavid Ahern return false; 28930b34eb00SDavid Ahern } 28940b34eb00SDavid Ahern return true; 28950b34eb00SDavid Ahern } 28960b34eb00SDavid Ahern 2897c55c8988SDavid Ahern struct fib6_nh_rd_arg { 2898c55c8988SDavid Ahern struct fib6_result *res; 2899c55c8988SDavid Ahern struct flowi6 *fl6; 2900c55c8988SDavid Ahern const struct in6_addr *gw; 2901c55c8988SDavid Ahern struct rt6_info **ret; 2902c55c8988SDavid Ahern }; 2903c55c8988SDavid Ahern 2904c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) 2905c55c8988SDavid Ahern { 2906c55c8988SDavid Ahern struct fib6_nh_rd_arg *arg = _arg; 2907c55c8988SDavid Ahern 2908c55c8988SDavid Ahern arg->res->nh = nh; 2909c55c8988SDavid Ahern return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); 2910c55c8988SDavid Ahern } 2911c55c8988SDavid Ahern 2912b55b76b2SDuan Jiong /* Handle redirects */ 2913b55b76b2SDuan Jiong struct ip6rd_flowi { 2914b55b76b2SDuan Jiong struct flowi6 fl6; 2915b55b76b2SDuan Jiong struct in6_addr gateway; 2916b55b76b2SDuan Jiong }; 2917b55b76b2SDuan Jiong 2918b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2919b55b76b2SDuan Jiong struct fib6_table *table, 2920b55b76b2SDuan Jiong struct flowi6 *fl6, 2921b75cc8f9SDavid Ahern const struct sk_buff *skb, 2922b55b76b2SDuan Jiong int flags) 2923b55b76b2SDuan Jiong { 2924b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 29250b34eb00SDavid Ahern struct rt6_info *ret = NULL; 29269b6b35abSDavid Ahern struct fib6_result res = {}; 2927c55c8988SDavid Ahern struct fib6_nh_rd_arg arg = { 2928c55c8988SDavid Ahern .res = &res, 2929c55c8988SDavid Ahern .fl6 = fl6, 2930c55c8988SDavid Ahern .gw = &rdfl->gateway, 2931c55c8988SDavid Ahern .ret = &ret 2932c55c8988SDavid Ahern }; 29338d1c802bSDavid Ahern struct fib6_info *rt; 2934b55b76b2SDuan Jiong struct fib6_node *fn; 2935b55b76b2SDuan Jiong 293631680ac2SDavid Ahern /* l3mdev_update_flow overrides oif if the device is enslaved; in 293731680ac2SDavid Ahern * this case we must match on the real ingress device, so reset it 293831680ac2SDavid Ahern */ 293931680ac2SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 294031680ac2SDavid Ahern fl6->flowi6_oif = skb->dev->ifindex; 294131680ac2SDavid Ahern 2942b55b76b2SDuan Jiong /* Get the "current" route for this destination and 294367c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2944b55b76b2SDuan Jiong * 2945b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2946b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2947b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2948b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2949b55b76b2SDuan Jiong * routes. 2950b55b76b2SDuan Jiong */ 2951b55b76b2SDuan Jiong 295266f5d6ceSWei Wang rcu_read_lock(); 29536454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2954b55b76b2SDuan Jiong restart: 295566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 29569b6b35abSDavid Ahern res.f6i = rt; 295714895687SDavid Ahern if (fib6_check_expired(rt)) 2958b55b76b2SDuan Jiong continue; 295993c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2960b55b76b2SDuan Jiong break; 2961c55c8988SDavid Ahern if (unlikely(rt->nh)) { 2962c55c8988SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 2963c55c8988SDavid Ahern continue; 2964c55c8988SDavid Ahern /* on match, res->nh is filled in and potentially ret */ 2965c55c8988SDavid Ahern if (nexthop_for_each_fib6_nh(rt->nh, 2966c55c8988SDavid Ahern fib6_nh_redirect_match, 2967c55c8988SDavid Ahern &arg)) 29680b34eb00SDavid Ahern goto out; 2969c55c8988SDavid Ahern } else { 2970c55c8988SDavid Ahern res.nh = rt->fib6_nh; 2971c55c8988SDavid Ahern if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, 2972c55c8988SDavid Ahern &ret)) 2973c55c8988SDavid Ahern goto out; 2974c55c8988SDavid Ahern } 2975b55b76b2SDuan Jiong } 2976b55b76b2SDuan Jiong 2977b55b76b2SDuan Jiong if (!rt) 2978421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 297993c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 298023fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2981b0a1ba59SMartin KaFai Lau goto out; 2982b0a1ba59SMartin KaFai Lau } 2983b0a1ba59SMartin KaFai Lau 2984421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2985a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2986a3c00e46SMartin KaFai Lau if (fn) 2987a3c00e46SMartin KaFai Lau goto restart; 2988b55b76b2SDuan Jiong } 2989a3c00e46SMartin KaFai Lau 29909b6b35abSDavid Ahern res.f6i = rt; 29911cf844c7SDavid Ahern res.nh = rt->fib6_nh; 2992b0a1ba59SMartin KaFai Lau out: 29937d21fec9SDavid Ahern if (ret) { 299410585b43SDavid Ahern ip6_hold_safe(net, &ret); 29957d21fec9SDavid Ahern } else { 29967d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 29977d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 29989b6b35abSDavid Ahern ret = ip6_create_rt_rcu(&res); 29997d21fec9SDavid Ahern } 3000b55b76b2SDuan Jiong 300166f5d6ceSWei Wang rcu_read_unlock(); 3002b55b76b2SDuan Jiong 30038ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 300423fb93a4SDavid Ahern return ret; 3005b55b76b2SDuan Jiong }; 3006b55b76b2SDuan Jiong 3007b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 3008b55b76b2SDuan Jiong const struct flowi6 *fl6, 3009b75cc8f9SDavid Ahern const struct sk_buff *skb, 3010b55b76b2SDuan Jiong const struct in6_addr *gateway) 3011b55b76b2SDuan Jiong { 3012b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 3013b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 3014b55b76b2SDuan Jiong 3015b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 3016b55b76b2SDuan Jiong rdfl.gateway = *gateway; 3017b55b76b2SDuan Jiong 3018b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 3019b55b76b2SDuan Jiong flags, __ip6_route_redirect); 3020b55b76b2SDuan Jiong } 3021b55b76b2SDuan Jiong 3022e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 3023e2d118a1SLorenzo Colitti kuid_t uid) 30243a5ad2eeSDavid S. Miller { 30253a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 30263a5ad2eeSDavid S. Miller struct dst_entry *dst; 30271f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 30281f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 30291f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 30301f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 30311f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 30321f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 30331f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 30341f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 30351f7f10acSMaciej Żenczykowski }; 30363a5ad2eeSDavid S. Miller 3037b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 30386700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 30393a5ad2eeSDavid S. Miller dst_release(dst); 30403a5ad2eeSDavid S. Miller } 30413a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 30423a5ad2eeSDavid S. Miller 3043d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 3044c92a59ecSDuan Jiong { 3045c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 3046c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 3047c92a59ecSDuan Jiong struct dst_entry *dst; 30480b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 30490b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 30500b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 30510b26fb17SMaciej Żenczykowski .daddr = msg->dest, 30520b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 30530b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 30540b26fb17SMaciej Żenczykowski }; 3055c92a59ecSDuan Jiong 3056b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 3057c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 3058c92a59ecSDuan Jiong dst_release(dst); 3059c92a59ecSDuan Jiong } 3060c92a59ecSDuan Jiong 30613a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 30623a5ad2eeSDavid S. Miller { 3063e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 3064e2d118a1SLorenzo Colitti sk->sk_uid); 30653a5ad2eeSDavid S. Miller } 30663a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 30673a5ad2eeSDavid S. Miller 30680dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 30691da177e4SLinus Torvalds { 30700dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 30710dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 30720dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 30730dbaee3bSDavid S. Miller 30741da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 30751da177e4SLinus Torvalds 30765578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 30775578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds /* 30801da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 30811da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 30821da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 30831da177e4SLinus Torvalds * rely only on pmtu discovery" 30841da177e4SLinus Torvalds */ 30851da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 30861da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 30871da177e4SLinus Torvalds return mtu; 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 3090ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 3091d33e4553SDavid S. Miller { 3092d33e4553SDavid S. Miller struct inet6_dev *idev; 3093d4ead6b3SDavid Ahern unsigned int mtu; 3094618f9bc7SSteffen Klassert 30954b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 30964b32b5adSMartin KaFai Lau if (mtu) 30974b32b5adSMartin KaFai Lau goto out; 30984b32b5adSMartin KaFai Lau 3099618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 3100d33e4553SDavid S. Miller 3101d33e4553SDavid S. Miller rcu_read_lock(); 3102d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 3103d33e4553SDavid S. Miller if (idev) 3104d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 3105d33e4553SDavid S. Miller rcu_read_unlock(); 3106d33e4553SDavid S. Miller 310730f78d8eSEric Dumazet out: 310814972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 310914972cbdSRoopa Prabhu 311014972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 3111d33e4553SDavid S. Miller } 3112d33e4553SDavid S. Miller 3113901731b8SDavid Ahern /* MTU selection: 3114901731b8SDavid Ahern * 1. mtu on route is locked - use it 3115901731b8SDavid Ahern * 2. mtu from nexthop exception 3116901731b8SDavid Ahern * 3. mtu from egress device 3117901731b8SDavid Ahern * 3118901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 3119901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 3120901731b8SDavid Ahern */ 3121b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res, 3122b748f260SDavid Ahern const struct in6_addr *daddr, 3123b748f260SDavid Ahern const struct in6_addr *saddr) 3124901731b8SDavid Ahern { 3125b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 3126b748f260SDavid Ahern struct fib6_info *f6i = res->f6i; 3127901731b8SDavid Ahern struct inet6_dev *idev; 3128510e2cedSWei Wang struct rt6_info *rt; 3129901731b8SDavid Ahern u32 mtu = 0; 3130901731b8SDavid Ahern 3131901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 3132901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 3133901731b8SDavid Ahern if (mtu) 3134901731b8SDavid Ahern goto out; 3135901731b8SDavid Ahern } 3136901731b8SDavid Ahern 3137510e2cedSWei Wang rt = rt6_find_cached_rt(res, daddr, saddr); 3138510e2cedSWei Wang if (unlikely(rt)) { 3139510e2cedSWei Wang mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 3140510e2cedSWei Wang } else { 3141b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 3142901731b8SDavid Ahern 3143901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 3144901731b8SDavid Ahern idev = __in6_dev_get(dev); 3145901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 3146901731b8SDavid Ahern mtu = idev->cnf.mtu6; 3147901731b8SDavid Ahern } 3148901731b8SDavid Ahern 3149901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 3150901731b8SDavid Ahern out: 3151b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 3152901731b8SDavid Ahern } 3153901731b8SDavid Ahern 31543b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 315587a11578SDavid S. Miller struct flowi6 *fl6) 31561da177e4SLinus Torvalds { 315787a11578SDavid S. Miller struct dst_entry *dst; 31581da177e4SLinus Torvalds struct rt6_info *rt; 31591da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 3160c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 31611da177e4SLinus Torvalds 316238308473SDavid S. Miller if (unlikely(!idev)) 3163122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 31641da177e4SLinus Torvalds 3165ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 316638308473SDavid S. Miller if (unlikely(!rt)) { 31671da177e4SLinus Torvalds in6_dev_put(idev); 316887a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 31691da177e4SLinus Torvalds goto out; 31701da177e4SLinus Torvalds } 31711da177e4SLinus Torvalds 3172588753f1SBrendan McGrath rt->dst.input = ip6_input; 31738e2ec639SYan, Zheng rt->dst.output = ip6_output; 3174550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 317587a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 31768e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 31778e2ec639SYan, Zheng rt->rt6i_idev = idev; 317814edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 31791da177e4SLinus Torvalds 31804c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 3181587fea74SWei Wang * do proper release of the net_device 3182587fea74SWei Wang */ 3183587fea74SWei Wang rt6_uncached_list_add(rt); 318481eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 31851da177e4SLinus Torvalds 318687a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 318787a11578SDavid S. Miller 31881da177e4SLinus Torvalds out: 318987a11578SDavid S. Miller return dst; 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 3192569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 31931da177e4SLinus Torvalds { 319486393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 31957019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 31967019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 31977019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 31987019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 31997019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 3200fc66f95cSEric Dumazet int entries; 32011da177e4SLinus Torvalds 3202fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 3203cf86a086SEric Dumazet if (entries > rt_max_size) 3204cf86a086SEric Dumazet entries = dst_entries_get_slow(ops); 3205cf86a086SEric Dumazet 320649a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 3207fc66f95cSEric Dumazet entries <= rt_max_size) 32081da177e4SLinus Torvalds goto out; 32091da177e4SLinus Torvalds 32106891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 321114956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 3212fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 3213fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 32147019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 32151da177e4SLinus Torvalds out: 32167019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 3217fc66f95cSEric Dumazet return entries > rt_max_size; 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 3220b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, 3221b2c709ccSDavid Ahern const struct in6_addr *gw_addr, u32 tbid, 3222b2c709ccSDavid Ahern int flags, struct fib6_result *res) 32238c14586fSDavid Ahern { 32248c14586fSDavid Ahern struct flowi6 fl6 = { 32258c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 32268c14586fSDavid Ahern .daddr = *gw_addr, 32278c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 32288c14586fSDavid Ahern }; 32298c14586fSDavid Ahern struct fib6_table *table; 3230b2c709ccSDavid Ahern int err; 32318c14586fSDavid Ahern 3232f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 32338c14586fSDavid Ahern if (!table) 3234b2c709ccSDavid Ahern return -EINVAL; 32358c14586fSDavid Ahern 32368c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 32378c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 32388c14586fSDavid Ahern 3239f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 32408c14586fSDavid Ahern 3241b2c709ccSDavid Ahern err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); 3242b2c709ccSDavid Ahern if (!err && res->f6i != net->ipv6.fib6_null_entry) 3243b2c709ccSDavid Ahern fib6_select_path(net, res, &fl6, cfg->fc_ifindex, 3244b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 32458c14586fSDavid Ahern 3246b2c709ccSDavid Ahern return err; 32478c14586fSDavid Ahern } 32488c14586fSDavid Ahern 3249fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 3250fc1e64e1SDavid Ahern struct fib6_config *cfg, 32519fbb704cSDavid Ahern const struct net_device *dev, 3252fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 3253fc1e64e1SDavid Ahern { 3254b2c709ccSDavid Ahern u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 3255fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 3256b2c709ccSDavid Ahern struct fib6_result res = {}; 3257fc1e64e1SDavid Ahern int err; 3258fc1e64e1SDavid Ahern 3259b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); 3260b2c709ccSDavid Ahern if (!err && !(res.fib6_flags & RTF_REJECT) && 32614ed591c8SDavid Ahern /* ignore match if it is the default route */ 3262b2c709ccSDavid Ahern !ipv6_addr_any(&res.f6i->fib6_dst.addr) && 3263b2c709ccSDavid Ahern (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { 326444750f84SDavid Ahern NL_SET_ERR_MSG(extack, 326544750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 3266fc1e64e1SDavid Ahern err = -EINVAL; 3267fc1e64e1SDavid Ahern } 3268fc1e64e1SDavid Ahern 3269fc1e64e1SDavid Ahern return err; 3270fc1e64e1SDavid Ahern } 3271fc1e64e1SDavid Ahern 32721edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 32731edce99fSDavid Ahern struct fib6_config *cfg, 32741edce99fSDavid Ahern struct net_device **_dev, 32751edce99fSDavid Ahern struct inet6_dev **idev) 32761edce99fSDavid Ahern { 32771edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 32781edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 3279b2c709ccSDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 3280b2c709ccSDavid Ahern struct fib6_result res = {}; 32811edce99fSDavid Ahern int err = -EHOSTUNREACH; 32821edce99fSDavid Ahern 32831edce99fSDavid Ahern if (cfg->fc_table) { 3284b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, 3285b2c709ccSDavid Ahern cfg->fc_table, flags, &res); 3286b2c709ccSDavid Ahern /* gw_addr can not require a gateway or resolve to a reject 3287b2c709ccSDavid Ahern * route. If a device is given, it must match the result. 3288b2c709ccSDavid Ahern */ 3289b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3290b2c709ccSDavid Ahern res.nh->fib_nh_gw_family || 3291b2c709ccSDavid Ahern (dev && dev != res.nh->fib_nh_dev)) 3292b2c709ccSDavid Ahern err = -EHOSTUNREACH; 32931edce99fSDavid Ahern } 32941edce99fSDavid Ahern 3295b2c709ccSDavid Ahern if (err < 0) { 3296b2c709ccSDavid Ahern struct flowi6 fl6 = { 3297b2c709ccSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 3298b2c709ccSDavid Ahern .daddr = *gw_addr, 3299b2c709ccSDavid Ahern }; 33001edce99fSDavid Ahern 3301b2c709ccSDavid Ahern err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); 3302b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3303b2c709ccSDavid Ahern res.nh->fib_nh_gw_family) 3304b2c709ccSDavid Ahern err = -EHOSTUNREACH; 33051edce99fSDavid Ahern 3306b2c709ccSDavid Ahern if (err) 3307b2c709ccSDavid Ahern return err; 3308b2c709ccSDavid Ahern 3309b2c709ccSDavid Ahern fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, 3310b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 33111edce99fSDavid Ahern } 33121edce99fSDavid Ahern 33131edce99fSDavid Ahern err = 0; 3314b2c709ccSDavid Ahern if (dev) { 3315b2c709ccSDavid Ahern if (dev != res.nh->fib_nh_dev) 3316b2c709ccSDavid Ahern err = -EHOSTUNREACH; 3317b2c709ccSDavid Ahern } else { 3318b2c709ccSDavid Ahern *_dev = dev = res.nh->fib_nh_dev; 3319b2c709ccSDavid Ahern dev_hold(dev); 3320b2c709ccSDavid Ahern *idev = in6_dev_get(dev); 3321b2c709ccSDavid Ahern } 33221edce99fSDavid Ahern 33231edce99fSDavid Ahern return err; 33241edce99fSDavid Ahern } 33251edce99fSDavid Ahern 33269fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 33279fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 33289fbb704cSDavid Ahern struct netlink_ext_ack *extack) 33299fbb704cSDavid Ahern { 33309fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 33319fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 3332232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 33339fbb704cSDavid Ahern const struct net_device *dev = *_dev; 3334232378e8SDavid Ahern bool need_addr_check = !dev; 33359fbb704cSDavid Ahern int err = -EINVAL; 33369fbb704cSDavid Ahern 33379fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 33389fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 33399fbb704cSDavid Ahern * will return already-added prefix route via interface that 33409fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 33419fbb704cSDavid Ahern */ 3342232378e8SDavid Ahern if (dev && 3343232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3344232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 33459fbb704cSDavid Ahern goto out; 33469fbb704cSDavid Ahern } 33479fbb704cSDavid Ahern 33489fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 33499fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 33509fbb704cSDavid Ahern * addresses as nexthop address. 33519fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 33529fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 33539fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 33549fbb704cSDavid Ahern * some exceptions. --ANK 33559fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 33569fbb704cSDavid Ahern * addressing 33579fbb704cSDavid Ahern */ 33589fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 33599fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 33609fbb704cSDavid Ahern goto out; 33619fbb704cSDavid Ahern } 33629fbb704cSDavid Ahern 3363b2c709ccSDavid Ahern rcu_read_lock(); 3364b2c709ccSDavid Ahern 33659fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 33669fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 33679fbb704cSDavid Ahern else 33689fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 33699fbb704cSDavid Ahern 3370b2c709ccSDavid Ahern rcu_read_unlock(); 3371b2c709ccSDavid Ahern 33729fbb704cSDavid Ahern if (err) 33739fbb704cSDavid Ahern goto out; 33749fbb704cSDavid Ahern } 33759fbb704cSDavid Ahern 33769fbb704cSDavid Ahern /* reload in case device was changed */ 33779fbb704cSDavid Ahern dev = *_dev; 33789fbb704cSDavid Ahern 33799fbb704cSDavid Ahern err = -EINVAL; 33809fbb704cSDavid Ahern if (!dev) { 33819fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 33829fbb704cSDavid Ahern goto out; 33839fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 33849fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 33859fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 33869fbb704cSDavid Ahern goto out; 33879fbb704cSDavid Ahern } 3388232378e8SDavid Ahern 3389232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 3390232378e8SDavid Ahern * egress device has been resolved. 3391232378e8SDavid Ahern */ 3392232378e8SDavid Ahern if (need_addr_check && 3393232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3394232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3395232378e8SDavid Ahern goto out; 3396232378e8SDavid Ahern } 3397232378e8SDavid Ahern 33989fbb704cSDavid Ahern err = 0; 33999fbb704cSDavid Ahern out: 34009fbb704cSDavid Ahern return err; 34019fbb704cSDavid Ahern } 34029fbb704cSDavid Ahern 340383c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 340483c44251SDavid Ahern { 340583c44251SDavid Ahern if ((flags & RTF_REJECT) || 340683c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 340783c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 340883c44251SDavid Ahern !(flags & RTF_LOCAL))) 340983c44251SDavid Ahern return true; 341083c44251SDavid Ahern 341183c44251SDavid Ahern return false; 341283c44251SDavid Ahern } 341383c44251SDavid Ahern 341483c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 341583c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 341683c44251SDavid Ahern struct netlink_ext_ack *extack) 341783c44251SDavid Ahern { 341883c44251SDavid Ahern struct net_device *dev = NULL; 341983c44251SDavid Ahern struct inet6_dev *idev = NULL; 342083c44251SDavid Ahern int addr_type; 342183c44251SDavid Ahern int err; 342283c44251SDavid Ahern 3423f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 34241bef4c22SEric Dumazet #ifdef CONFIG_IPV6_ROUTER_PREF 34251bef4c22SEric Dumazet fib6_nh->last_probe = jiffies; 34261bef4c22SEric Dumazet #endif 342738428d68SRoopa Prabhu if (cfg->fc_is_fdb) { 342838428d68SRoopa Prabhu fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 342938428d68SRoopa Prabhu fib6_nh->fib_nh_gw_family = AF_INET6; 343038428d68SRoopa Prabhu return 0; 343138428d68SRoopa Prabhu } 3432f1741730SDavid Ahern 343383c44251SDavid Ahern err = -ENODEV; 343483c44251SDavid Ahern if (cfg->fc_ifindex) { 343583c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 343683c44251SDavid Ahern if (!dev) 343783c44251SDavid Ahern goto out; 343883c44251SDavid Ahern idev = in6_dev_get(dev); 343983c44251SDavid Ahern if (!idev) 344083c44251SDavid Ahern goto out; 344183c44251SDavid Ahern } 344283c44251SDavid Ahern 344383c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 344483c44251SDavid Ahern if (!dev) { 344583c44251SDavid Ahern NL_SET_ERR_MSG(extack, 344683c44251SDavid Ahern "Nexthop device required for onlink"); 344783c44251SDavid Ahern goto out; 344883c44251SDavid Ahern } 344983c44251SDavid Ahern 345083c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 345183c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 345283c44251SDavid Ahern err = -ENETDOWN; 345383c44251SDavid Ahern goto out; 345483c44251SDavid Ahern } 345583c44251SDavid Ahern 3456ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 345783c44251SDavid Ahern } 345883c44251SDavid Ahern 3459ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 346083c44251SDavid Ahern 346183c44251SDavid Ahern /* We cannot add true routes via loopback here, 346283c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 346383c44251SDavid Ahern */ 346483c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 346583c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 346683c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 346783c44251SDavid Ahern if (dev != net->loopback_dev) { 346883c44251SDavid Ahern if (dev) { 346983c44251SDavid Ahern dev_put(dev); 347083c44251SDavid Ahern in6_dev_put(idev); 347183c44251SDavid Ahern } 347283c44251SDavid Ahern dev = net->loopback_dev; 347383c44251SDavid Ahern dev_hold(dev); 347483c44251SDavid Ahern idev = in6_dev_get(dev); 347583c44251SDavid Ahern if (!idev) { 347683c44251SDavid Ahern err = -ENODEV; 347783c44251SDavid Ahern goto out; 347883c44251SDavid Ahern } 347983c44251SDavid Ahern } 34807dd73168SDavid Ahern goto pcpu_alloc; 348183c44251SDavid Ahern } 348283c44251SDavid Ahern 348383c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 348483c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 348583c44251SDavid Ahern if (err) 348683c44251SDavid Ahern goto out; 348783c44251SDavid Ahern 3488ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3489bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 349083c44251SDavid Ahern } 349183c44251SDavid Ahern 349283c44251SDavid Ahern err = -ENODEV; 349383c44251SDavid Ahern if (!dev) 349483c44251SDavid Ahern goto out; 349583c44251SDavid Ahern 349683c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 349783c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 349883c44251SDavid Ahern err = -EACCES; 349983c44251SDavid Ahern goto out; 350083c44251SDavid Ahern } 350183c44251SDavid Ahern 350283c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 350383c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 350483c44251SDavid Ahern err = -ENETDOWN; 350583c44251SDavid Ahern goto out; 350683c44251SDavid Ahern } 350783c44251SDavid Ahern 350883c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 350983c44251SDavid Ahern !netif_carrier_ok(dev)) 3510ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 351183c44251SDavid Ahern 3512faee6769SAlexander Aring err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap, 35137dd73168SDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 35147dd73168SDavid Ahern if (err) 35157dd73168SDavid Ahern goto out; 35167dd73168SDavid Ahern 35177dd73168SDavid Ahern pcpu_alloc: 3518f40b6ae2SDavid Ahern fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3519f40b6ae2SDavid Ahern if (!fib6_nh->rt6i_pcpu) { 3520f40b6ae2SDavid Ahern err = -ENOMEM; 3521f40b6ae2SDavid Ahern goto out; 3522f40b6ae2SDavid Ahern } 3523f40b6ae2SDavid Ahern 3524ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3525f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 352683c44251SDavid Ahern err = 0; 352783c44251SDavid Ahern out: 352883c44251SDavid Ahern if (idev) 352983c44251SDavid Ahern in6_dev_put(idev); 353083c44251SDavid Ahern 353183c44251SDavid Ahern if (err) { 3532ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3533ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 353483c44251SDavid Ahern if (dev) 353583c44251SDavid Ahern dev_put(dev); 353683c44251SDavid Ahern } 353783c44251SDavid Ahern 353883c44251SDavid Ahern return err; 353983c44251SDavid Ahern } 354083c44251SDavid Ahern 3541dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3542dac7d0f2SDavid Ahern { 3543cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 3544cc5c073aSDavid Ahern 3545cc5c073aSDavid Ahern rcu_read_lock(); 3546cc5c073aSDavid Ahern 3547cc5c073aSDavid Ahern fib6_nh_flush_exceptions(fib6_nh, NULL); 3548cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); 3549cc5c073aSDavid Ahern if (bucket) { 3550cc5c073aSDavid Ahern rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); 3551cc5c073aSDavid Ahern kfree(bucket); 3552cc5c073aSDavid Ahern } 3553cc5c073aSDavid Ahern 3554cc5c073aSDavid Ahern rcu_read_unlock(); 3555cc5c073aSDavid Ahern 3556f40b6ae2SDavid Ahern if (fib6_nh->rt6i_pcpu) { 3557f40b6ae2SDavid Ahern int cpu; 3558f40b6ae2SDavid Ahern 3559f40b6ae2SDavid Ahern for_each_possible_cpu(cpu) { 3560f40b6ae2SDavid Ahern struct rt6_info **ppcpu_rt; 3561f40b6ae2SDavid Ahern struct rt6_info *pcpu_rt; 3562f40b6ae2SDavid Ahern 3563f40b6ae2SDavid Ahern ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 3564f40b6ae2SDavid Ahern pcpu_rt = *ppcpu_rt; 3565f40b6ae2SDavid Ahern if (pcpu_rt) { 3566f40b6ae2SDavid Ahern dst_dev_put(&pcpu_rt->dst); 3567f40b6ae2SDavid Ahern dst_release(&pcpu_rt->dst); 3568f40b6ae2SDavid Ahern *ppcpu_rt = NULL; 3569f40b6ae2SDavid Ahern } 3570f40b6ae2SDavid Ahern } 3571f40b6ae2SDavid Ahern 3572f40b6ae2SDavid Ahern free_percpu(fib6_nh->rt6i_pcpu); 3573f40b6ae2SDavid Ahern } 3574f40b6ae2SDavid Ahern 3575979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3576dac7d0f2SDavid Ahern } 3577dac7d0f2SDavid Ahern 35788d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3579acb54e3cSDavid Ahern gfp_t gfp_flags, 3580333c4301SDavid Ahern struct netlink_ext_ack *extack) 35811da177e4SLinus Torvalds { 35825578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 35838d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3584f88d8ea6SDavid Ahern struct nexthop *nh = NULL; 3585c71099acSThomas Graf struct fib6_table *table; 3586f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 35878c5b83f0SRoopa Prabhu int err = -EINVAL; 358883c44251SDavid Ahern int addr_type; 35891da177e4SLinus Torvalds 3590557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3591d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3592d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3593557c44beSDavid Ahern goto out; 3594d5d531cbSDavid Ahern } 3595557c44beSDavid Ahern 35962ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 35972ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 35982ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 35992ea2352eSWei Wang goto out; 36002ea2352eSWei Wang } 36012ea2352eSWei Wang 3602e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3603e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3604e8478e80SDavid Ahern goto out; 3605e8478e80SDavid Ahern } 3606e8478e80SDavid Ahern 3607d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3608d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 36098c5b83f0SRoopa Prabhu goto out; 3610d5d531cbSDavid Ahern } 3611d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3612d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3613d5d531cbSDavid Ahern goto out; 3614d5d531cbSDavid Ahern } 36151da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3616d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3617d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3618d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 36198c5b83f0SRoopa Prabhu goto out; 3620d5d531cbSDavid Ahern } 36211da177e4SLinus Torvalds #endif 36225b98324eSDavid Ahern if (cfg->fc_nh_id) { 36235b98324eSDavid Ahern nh = nexthop_find_by_id(net, cfg->fc_nh_id); 36245b98324eSDavid Ahern if (!nh) { 36255b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 36265b98324eSDavid Ahern goto out; 36275b98324eSDavid Ahern } 36285b98324eSDavid Ahern err = fib6_check_nexthop(nh, cfg, extack); 36295b98324eSDavid Ahern if (err) 36305b98324eSDavid Ahern goto out; 36315b98324eSDavid Ahern } 3632fc1e64e1SDavid Ahern 3633c71099acSThomas Graf err = -ENOBUFS; 363438308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3635d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3636d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 363738308473SDavid S. Miller if (!table) { 3638f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3639d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3640d71314b4SMatti Vaittinen } 3641d71314b4SMatti Vaittinen } else { 3642d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3643d71314b4SMatti Vaittinen } 364438308473SDavid S. Miller 364538308473SDavid S. Miller if (!table) 3646c71099acSThomas Graf goto out; 3647c71099acSThomas Graf 36481da177e4SLinus Torvalds err = -ENOMEM; 3649f88d8ea6SDavid Ahern rt = fib6_info_alloc(gfp_flags, !nh); 365093531c67SDavid Ahern if (!rt) 36511da177e4SLinus Torvalds goto out; 365293531c67SDavid Ahern 3653d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3654d7e774f3SDavid Ahern extack); 3655767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3656767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3657fda21d46SEric Dumazet /* Do not leave garbage there. */ 3658fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 3659767a2217SDavid Ahern goto out; 3660767a2217SDavid Ahern } 3661767a2217SDavid Ahern 366293531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 366393531c67SDavid Ahern rt->dst_nocount = true; 36641da177e4SLinus Torvalds 36651716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 366614895687SDavid Ahern fib6_set_expires(rt, jiffies + 36671716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 36681716a961SGao feng else 366914895687SDavid Ahern fib6_clean_expires(rt); 36701da177e4SLinus Torvalds 367186872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 367286872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 367393c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 367486872cb5SThomas Graf 367583c44251SDavid Ahern rt->fib6_table = table; 367683c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 3677c7036d97SDavid Ahern rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; 36782b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 367919e42e45SRoopa Prabhu 368093c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 368193c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 368493c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 368593c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 36861da177e4SLinus Torvalds #endif 3687f88d8ea6SDavid Ahern if (nh) { 3688f88d8ea6SDavid Ahern if (!nexthop_get(nh)) { 3689f88d8ea6SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 3690f88d8ea6SDavid Ahern goto out; 3691f88d8ea6SDavid Ahern } 3692f88d8ea6SDavid Ahern if (rt->fib6_src.plen) { 36934daa95afSColin Ian King NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); 3694f88d8ea6SDavid Ahern goto out; 3695f88d8ea6SDavid Ahern } 3696f88d8ea6SDavid Ahern rt->nh = nh; 3697f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(rt->nh); 3698f88d8ea6SDavid Ahern } else { 36991cf844c7SDavid Ahern err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); 37001da177e4SLinus Torvalds if (err) 37011da177e4SLinus Torvalds goto out; 37029fbb704cSDavid Ahern 3703f88d8ea6SDavid Ahern fib6_nh = rt->fib6_nh; 3704f88d8ea6SDavid Ahern 3705f88d8ea6SDavid Ahern /* We cannot add true routes via loopback here, they would 3706f88d8ea6SDavid Ahern * result in kernel looping; promote them to reject routes 370783c44251SDavid Ahern */ 370883c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3709f88d8ea6SDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, 3710f88d8ea6SDavid Ahern addr_type)) 371183c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3712f88d8ea6SDavid Ahern } 3713955ec4cbSDavid Ahern 3714c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3715f88d8ea6SDavid Ahern struct net_device *dev = fib6_nh->fib_nh_dev; 371683c44251SDavid Ahern 3717c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3718d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3719c3968a85SDaniel Walter err = -EINVAL; 3720c3968a85SDaniel Walter goto out; 3721c3968a85SDaniel Walter } 372293c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 372393c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3724c3968a85SDaniel Walter } else 372593c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3726c3968a85SDaniel Walter 37278c5b83f0SRoopa Prabhu return rt; 37281da177e4SLinus Torvalds out: 372993531c67SDavid Ahern fib6_info_release(rt); 37308c5b83f0SRoopa Prabhu return ERR_PTR(err); 37316b9ea5a6SRoopa Prabhu } 37326b9ea5a6SRoopa Prabhu 3733acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3734333c4301SDavid Ahern struct netlink_ext_ack *extack) 37356b9ea5a6SRoopa Prabhu { 37368d1c802bSDavid Ahern struct fib6_info *rt; 37376b9ea5a6SRoopa Prabhu int err; 37386b9ea5a6SRoopa Prabhu 3739acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3740d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3741d4ead6b3SDavid Ahern return PTR_ERR(rt); 37426b9ea5a6SRoopa Prabhu 3743d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 374493531c67SDavid Ahern fib6_info_release(rt); 37456b9ea5a6SRoopa Prabhu 37461da177e4SLinus Torvalds return err; 37471da177e4SLinus Torvalds } 37481da177e4SLinus Torvalds 37498d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 37501da177e4SLinus Torvalds { 3751afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3752c71099acSThomas Graf struct fib6_table *table; 3753afb1d4b5SDavid Ahern int err; 37541da177e4SLinus Torvalds 3755421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 37566825a26cSGao feng err = -ENOENT; 37576825a26cSGao feng goto out; 37586825a26cSGao feng } 37596c813a72SPatrick McHardy 376093c2fb25SDavid Ahern table = rt->fib6_table; 376166f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 376286872cb5SThomas Graf err = fib6_del(rt, info); 376366f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 37641da177e4SLinus Torvalds 37656825a26cSGao feng out: 376693531c67SDavid Ahern fib6_info_release(rt); 37671da177e4SLinus Torvalds return err; 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 377011dd74b3SRoopa Prabhu int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify) 3771e0a1ad73SThomas Graf { 377211dd74b3SRoopa Prabhu struct nl_info info = { 377311dd74b3SRoopa Prabhu .nl_net = net, 377411dd74b3SRoopa Prabhu .skip_notify = skip_notify 377511dd74b3SRoopa Prabhu }; 3776afb1d4b5SDavid Ahern 3777528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3778e0a1ad73SThomas Graf } 3779e0a1ad73SThomas Graf 37808d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 37810ae81335SDavid Ahern { 37820ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3783e3330039SWANG Cong struct net *net = info->nl_net; 378416a16cd3SDavid Ahern struct sk_buff *skb = NULL; 37850ae81335SDavid Ahern struct fib6_table *table; 3786e3330039SWANG Cong int err = -ENOENT; 37870ae81335SDavid Ahern 3788421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3789e3330039SWANG Cong goto out_put; 379093c2fb25SDavid Ahern table = rt->fib6_table; 379166f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 37920ae81335SDavid Ahern 379393c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 37948d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 37950284696bSIdo Schimmel struct fib6_node *fn; 37960ae81335SDavid Ahern 379716a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 379816a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 379916a16cd3SDavid Ahern if (skb) { 380016a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 380116a16cd3SDavid Ahern 3802d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 380316a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 380416a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 380516a16cd3SDavid Ahern kfree_skb(skb); 380616a16cd3SDavid Ahern skb = NULL; 380716a16cd3SDavid Ahern } else 380816a16cd3SDavid Ahern info->skip_notify = 1; 380916a16cd3SDavid Ahern } 381016a16cd3SDavid Ahern 38110284696bSIdo Schimmel /* 'rt' points to the first sibling route. If it is not the 38120284696bSIdo Schimmel * leaf, then we do not need to send a notification. Otherwise, 38130284696bSIdo Schimmel * we need to check if the last sibling has a next route or not 38140284696bSIdo Schimmel * and emit a replace or delete notification, respectively. 38150284696bSIdo Schimmel */ 38162881fd61SIdo Schimmel info->skip_notify_kernel = 1; 38170284696bSIdo Schimmel fn = rcu_dereference_protected(rt->fib6_node, 38180284696bSIdo Schimmel lockdep_is_held(&table->tb6_lock)); 38190284696bSIdo Schimmel if (rcu_access_pointer(fn->leaf) == rt) { 38200284696bSIdo Schimmel struct fib6_info *last_sibling, *replace_rt; 38210284696bSIdo Schimmel 38220284696bSIdo Schimmel last_sibling = list_last_entry(&rt->fib6_siblings, 38230284696bSIdo Schimmel struct fib6_info, 38240284696bSIdo Schimmel fib6_siblings); 38250284696bSIdo Schimmel replace_rt = rcu_dereference_protected( 38260284696bSIdo Schimmel last_sibling->fib6_next, 38270284696bSIdo Schimmel lockdep_is_held(&table->tb6_lock)); 38280284696bSIdo Schimmel if (replace_rt) 38290284696bSIdo Schimmel call_fib6_entry_notifiers_replace(net, 38300284696bSIdo Schimmel replace_rt); 38310284696bSIdo Schimmel else 38320284696bSIdo Schimmel call_fib6_multipath_entry_notifiers(net, 3833caafb250SIdo Schimmel FIB_EVENT_ENTRY_DEL, 38340284696bSIdo Schimmel rt, rt->fib6_nsiblings, 38350284696bSIdo Schimmel NULL); 38360284696bSIdo Schimmel } 38370ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 383893c2fb25SDavid Ahern &rt->fib6_siblings, 383993c2fb25SDavid Ahern fib6_siblings) { 38400ae81335SDavid Ahern err = fib6_del(sibling, info); 38410ae81335SDavid Ahern if (err) 3842e3330039SWANG Cong goto out_unlock; 38430ae81335SDavid Ahern } 38440ae81335SDavid Ahern } 38450ae81335SDavid Ahern 38460ae81335SDavid Ahern err = fib6_del(rt, info); 3847e3330039SWANG Cong out_unlock: 384866f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3849e3330039SWANG Cong out_put: 385093531c67SDavid Ahern fib6_info_release(rt); 385116a16cd3SDavid Ahern 385216a16cd3SDavid Ahern if (skb) { 3853e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 385416a16cd3SDavid Ahern info->nlh, gfp_any()); 385516a16cd3SDavid Ahern } 38560ae81335SDavid Ahern return err; 38570ae81335SDavid Ahern } 38580ae81335SDavid Ahern 38590fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 386023fb93a4SDavid Ahern { 386123fb93a4SDavid Ahern int rc = -ESRCH; 386223fb93a4SDavid Ahern 386323fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 386423fb93a4SDavid Ahern goto out; 386523fb93a4SDavid Ahern 386623fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 386723fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 386823fb93a4SDavid Ahern goto out; 3869761f6026SXin Long 387023fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 387123fb93a4SDavid Ahern out: 387223fb93a4SDavid Ahern return rc; 387323fb93a4SDavid Ahern } 387423fb93a4SDavid Ahern 38750fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, 38760fa6efc5SDavid Ahern struct fib6_nh *nh) 38770fa6efc5SDavid Ahern { 38780fa6efc5SDavid Ahern struct fib6_result res = { 38790fa6efc5SDavid Ahern .f6i = rt, 38800fa6efc5SDavid Ahern .nh = nh, 38810fa6efc5SDavid Ahern }; 38820fa6efc5SDavid Ahern struct rt6_info *rt_cache; 38830fa6efc5SDavid Ahern 38840fa6efc5SDavid Ahern rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); 38850fa6efc5SDavid Ahern if (rt_cache) 38860fa6efc5SDavid Ahern return __ip6_del_cached_rt(rt_cache, cfg); 38870fa6efc5SDavid Ahern 38880fa6efc5SDavid Ahern return 0; 38890fa6efc5SDavid Ahern } 38900fa6efc5SDavid Ahern 38915b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg { 38925b98324eSDavid Ahern struct fib6_config *cfg; 38935b98324eSDavid Ahern struct fib6_info *f6i; 38945b98324eSDavid Ahern }; 38955b98324eSDavid Ahern 38965b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) 38975b98324eSDavid Ahern { 38985b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg *arg = _arg; 38995b98324eSDavid Ahern int rc; 39005b98324eSDavid Ahern 39015b98324eSDavid Ahern rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); 39025b98324eSDavid Ahern return rc != -ESRCH ? rc : 0; 39035b98324eSDavid Ahern } 39045b98324eSDavid Ahern 39055b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) 39065b98324eSDavid Ahern { 39075b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg arg = { 39085b98324eSDavid Ahern .cfg = cfg, 39095b98324eSDavid Ahern .f6i = f6i 39105b98324eSDavid Ahern }; 39115b98324eSDavid Ahern 39125b98324eSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); 39135b98324eSDavid Ahern } 39145b98324eSDavid Ahern 3915333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3916333c4301SDavid Ahern struct netlink_ext_ack *extack) 39171da177e4SLinus Torvalds { 3918c71099acSThomas Graf struct fib6_table *table; 39198d1c802bSDavid Ahern struct fib6_info *rt; 39201da177e4SLinus Torvalds struct fib6_node *fn; 39211da177e4SLinus Torvalds int err = -ESRCH; 39221da177e4SLinus Torvalds 39235578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3924d5d531cbSDavid Ahern if (!table) { 3925d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3926c71099acSThomas Graf return err; 3927d5d531cbSDavid Ahern } 39281da177e4SLinus Torvalds 392966f5d6ceSWei Wang rcu_read_lock(); 3930c71099acSThomas Graf 3931c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 393286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 393338fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 39342b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 39351da177e4SLinus Torvalds 39361da177e4SLinus Torvalds if (fn) { 393766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3938ad1601aeSDavid Ahern struct fib6_nh *nh; 3939ad1601aeSDavid Ahern 39403401bfb1SStefano Brivio if (rt->nh && cfg->fc_nh_id && 39413401bfb1SStefano Brivio rt->nh->id != cfg->fc_nh_id) 39425b98324eSDavid Ahern continue; 394323fb93a4SDavid Ahern 39445b98324eSDavid Ahern if (cfg->fc_flags & RTF_CACHE) { 39455b98324eSDavid Ahern int rc = 0; 39465b98324eSDavid Ahern 39475b98324eSDavid Ahern if (rt->nh) { 39485b98324eSDavid Ahern rc = ip6_del_cached_rt_nh(cfg, rt); 39495b98324eSDavid Ahern } else if (cfg->fc_nh_id) { 39505b98324eSDavid Ahern continue; 39515b98324eSDavid Ahern } else { 39525b98324eSDavid Ahern nh = rt->fib6_nh; 39530fa6efc5SDavid Ahern rc = ip6_del_cached_rt(cfg, rt, nh); 39545b98324eSDavid Ahern } 39559e575010SEric Dumazet if (rc != -ESRCH) { 39569e575010SEric Dumazet rcu_read_unlock(); 395723fb93a4SDavid Ahern return rc; 395823fb93a4SDavid Ahern } 39591f56a01fSMartin KaFai Lau continue; 39602b760fcfSWei Wang } 3961ad1601aeSDavid Ahern 39625b98324eSDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 39635b98324eSDavid Ahern continue; 39645b98324eSDavid Ahern if (cfg->fc_protocol && 39655b98324eSDavid Ahern cfg->fc_protocol != rt->fib6_protocol) 39665b98324eSDavid Ahern continue; 39675b98324eSDavid Ahern 39685b98324eSDavid Ahern if (rt->nh) { 39695b98324eSDavid Ahern if (!fib6_info_hold_safe(rt)) 39705b98324eSDavid Ahern continue; 39715b98324eSDavid Ahern rcu_read_unlock(); 39725b98324eSDavid Ahern 39735b98324eSDavid Ahern return __ip6_del_rt(rt, &cfg->fc_nlinfo); 39745b98324eSDavid Ahern } 39755b98324eSDavid Ahern if (cfg->fc_nh_id) 39765b98324eSDavid Ahern continue; 39775b98324eSDavid Ahern 39785b98324eSDavid Ahern nh = rt->fib6_nh; 397986872cb5SThomas Graf if (cfg->fc_ifindex && 3980ad1601aeSDavid Ahern (!nh->fib_nh_dev || 3981ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 39821da177e4SLinus Torvalds continue; 398386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 3984ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 39851da177e4SLinus Torvalds continue; 3986e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3987e873e4b9SWei Wang continue; 398866f5d6ceSWei Wang rcu_read_unlock(); 39891da177e4SLinus Torvalds 39900ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 39910ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 399286872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 39930ae81335SDavid Ahern 39940ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 39951da177e4SLinus Torvalds } 39961da177e4SLinus Torvalds } 399766f5d6ceSWei Wang rcu_read_unlock(); 39981da177e4SLinus Torvalds 39991da177e4SLinus Torvalds return err; 40001da177e4SLinus Torvalds } 40011da177e4SLinus Torvalds 40026700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 4003a6279458SYOSHIFUJI Hideaki { 4004a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 4005e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 400685bd05deSDavid Ahern struct fib6_result res = {}; 4007e8599ff4SDavid S. Miller struct ndisc_options ndopts; 4008e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 4009e8599ff4SDavid S. Miller struct neighbour *neigh; 401071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 40116e157b6aSDavid S. Miller int optlen, on_link; 40126e157b6aSDavid S. Miller u8 *lladdr; 4013e8599ff4SDavid S. Miller 401429a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 401571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 4016e8599ff4SDavid S. Miller 4017e8599ff4SDavid S. Miller if (optlen < 0) { 40186e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 4019e8599ff4SDavid S. Miller return; 4020e8599ff4SDavid S. Miller } 4021e8599ff4SDavid S. Miller 402271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 4023e8599ff4SDavid S. Miller 402471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 40256e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 4026e8599ff4SDavid S. Miller return; 4027e8599ff4SDavid S. Miller } 4028e8599ff4SDavid S. Miller 40296e157b6aSDavid S. Miller on_link = 0; 403071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 4031e8599ff4SDavid S. Miller on_link = 1; 403271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 4033e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 40346e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 4035e8599ff4SDavid S. Miller return; 4036e8599ff4SDavid S. Miller } 4037e8599ff4SDavid S. Miller 4038e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 4039e8599ff4SDavid S. Miller if (!in6_dev) 4040e8599ff4SDavid S. Miller return; 4041e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 4042e8599ff4SDavid S. Miller return; 4043e8599ff4SDavid S. Miller 4044e8599ff4SDavid S. Miller /* RFC2461 8.1: 4045e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 4046e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 4047e8599ff4SDavid S. Miller */ 4048e8599ff4SDavid S. Miller 4049f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 4050e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 4051e8599ff4SDavid S. Miller return; 4052e8599ff4SDavid S. Miller } 40536e157b6aSDavid S. Miller 40546e157b6aSDavid S. Miller lladdr = NULL; 4055e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 4056e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 4057e8599ff4SDavid S. Miller skb->dev); 4058e8599ff4SDavid S. Miller if (!lladdr) { 4059e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 4060e8599ff4SDavid S. Miller return; 4061e8599ff4SDavid S. Miller } 4062e8599ff4SDavid S. Miller } 4063e8599ff4SDavid S. Miller 40646e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 4065ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 40666e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 40676e157b6aSDavid S. Miller return; 40686e157b6aSDavid S. Miller } 40696e157b6aSDavid S. Miller 40706e157b6aSDavid S. Miller /* Redirect received -> path was valid. 40716e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 40726e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 40736e157b6aSDavid S. Miller */ 40740dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 40756e157b6aSDavid S. Miller 407671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 4077e8599ff4SDavid S. Miller if (!neigh) 4078e8599ff4SDavid S. Miller return; 4079e8599ff4SDavid S. Miller 40801da177e4SLinus Torvalds /* 40811da177e4SLinus Torvalds * We have finally decided to accept it. 40821da177e4SLinus Torvalds */ 40831da177e4SLinus Torvalds 4084f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 40851da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 40861da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 40871da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 4088f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 4089f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 40901da177e4SLinus Torvalds 40914d85cd0cSDavid Ahern rcu_read_lock(); 409285bd05deSDavid Ahern res.f6i = rcu_dereference(rt->from); 4093ff24e498SDavid S. Miller if (!res.f6i) 4094886b7a50SMartin KaFai Lau goto out; 40958a14e46fSDavid Ahern 409649d5b8efSDavid Ahern if (res.f6i->nh) { 409749d5b8efSDavid Ahern struct fib6_nh_match_arg arg = { 409849d5b8efSDavid Ahern .dev = dst->dev, 409949d5b8efSDavid Ahern .gw = &rt->rt6i_gateway, 410049d5b8efSDavid Ahern }; 410149d5b8efSDavid Ahern 410249d5b8efSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 410349d5b8efSDavid Ahern fib6_nh_find_match, &arg); 410449d5b8efSDavid Ahern 410549d5b8efSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 410649d5b8efSDavid Ahern * using the dst->dev. Should be impossible 410749d5b8efSDavid Ahern */ 410849d5b8efSDavid Ahern if (!arg.match) 410949d5b8efSDavid Ahern goto out; 411049d5b8efSDavid Ahern res.nh = arg.match; 411149d5b8efSDavid Ahern } else { 41121cf844c7SDavid Ahern res.nh = res.f6i->fib6_nh; 411349d5b8efSDavid Ahern } 411449d5b8efSDavid Ahern 41157d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 41167d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 411785bd05deSDavid Ahern nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 411838308473SDavid S. Miller if (!nrt) 41191da177e4SLinus Torvalds goto out; 41201da177e4SLinus Torvalds 41211da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 41221da177e4SLinus Torvalds if (on_link) 41231da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 41241da177e4SLinus Torvalds 41254e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 41261da177e4SLinus Torvalds 4127886b7a50SMartin KaFai Lau /* rt6_insert_exception() will take care of duplicated exceptions */ 41285012f0a5SDavid Ahern if (rt6_insert_exception(nrt, &res)) { 41292b760fcfSWei Wang dst_release_immediate(&nrt->dst); 41302b760fcfSWei Wang goto out; 41312b760fcfSWei Wang } 41321da177e4SLinus Torvalds 4133d8d1f30bSChangli Gao netevent.old = &rt->dst; 4134d8d1f30bSChangli Gao netevent.new = &nrt->dst; 413571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 413660592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 41378d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 41388d71740cSTom Tucker 41391da177e4SLinus Torvalds out: 4140886b7a50SMartin KaFai Lau rcu_read_unlock(); 4141e8599ff4SDavid S. Miller neigh_release(neigh); 41426e157b6aSDavid S. Miller } 41436e157b6aSDavid S. Miller 414470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 41458d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 4146b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4147830218c1SDavid Ahern const struct in6_addr *gwaddr, 4148830218c1SDavid Ahern struct net_device *dev) 414970ceb4f5SYOSHIFUJI Hideaki { 4150830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 4151830218c1SDavid Ahern int ifindex = dev->ifindex; 415270ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 41538d1c802bSDavid Ahern struct fib6_info *rt = NULL; 4154c71099acSThomas Graf struct fib6_table *table; 415570ceb4f5SYOSHIFUJI Hideaki 4156830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 415738308473SDavid S. Miller if (!table) 4158c71099acSThomas Graf return NULL; 4159c71099acSThomas Graf 416066f5d6ceSWei Wang rcu_read_lock(); 416138fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 416270ceb4f5SYOSHIFUJI Hideaki if (!fn) 416370ceb4f5SYOSHIFUJI Hideaki goto out; 416470ceb4f5SYOSHIFUJI Hideaki 416566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 4166f88d8ea6SDavid Ahern /* these routes do not use nexthops */ 4167f88d8ea6SDavid Ahern if (rt->nh) 4168f88d8ea6SDavid Ahern continue; 41691cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) 417070ceb4f5SYOSHIFUJI Hideaki continue; 41712b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 41721cf844c7SDavid Ahern !rt->fib6_nh->fib_nh_gw_family) 417370ceb4f5SYOSHIFUJI Hideaki continue; 41741cf844c7SDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) 417570ceb4f5SYOSHIFUJI Hideaki continue; 4176e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 4177e873e4b9SWei Wang continue; 417870ceb4f5SYOSHIFUJI Hideaki break; 417970ceb4f5SYOSHIFUJI Hideaki } 418070ceb4f5SYOSHIFUJI Hideaki out: 418166f5d6ceSWei Wang rcu_read_unlock(); 418270ceb4f5SYOSHIFUJI Hideaki return rt; 418370ceb4f5SYOSHIFUJI Hideaki } 418470ceb4f5SYOSHIFUJI Hideaki 41858d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 4186b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4187830218c1SDavid Ahern const struct in6_addr *gwaddr, 4188830218c1SDavid Ahern struct net_device *dev, 418995c96174SEric Dumazet unsigned int pref) 419070ceb4f5SYOSHIFUJI Hideaki { 419186872cb5SThomas Graf struct fib6_config cfg = { 4192238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 4193830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 419486872cb5SThomas Graf .fc_dst_len = prefixlen, 419586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 419686872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 4197b91d5329SXin Long .fc_protocol = RTPROT_RA, 4198e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 419915e47304SEric W. Biederman .fc_nlinfo.portid = 0, 4200efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 4201efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 420286872cb5SThomas Graf }; 420370ceb4f5SYOSHIFUJI Hideaki 4204830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 42054e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 42064e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 420786872cb5SThomas Graf 4208e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 4209e317da96SYOSHIFUJI Hideaki if (!prefixlen) 421086872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 421170ceb4f5SYOSHIFUJI Hideaki 4212acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 421370ceb4f5SYOSHIFUJI Hideaki 4214830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 421570ceb4f5SYOSHIFUJI Hideaki } 421670ceb4f5SYOSHIFUJI Hideaki #endif 421770ceb4f5SYOSHIFUJI Hideaki 42188d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 4219afb1d4b5SDavid Ahern const struct in6_addr *addr, 4220afb1d4b5SDavid Ahern struct net_device *dev) 42211da177e4SLinus Torvalds { 4222830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 42238d1c802bSDavid Ahern struct fib6_info *rt; 4224c71099acSThomas Graf struct fib6_table *table; 42251da177e4SLinus Torvalds 4226afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 422738308473SDavid S. Miller if (!table) 4228c71099acSThomas Graf return NULL; 42291da177e4SLinus Torvalds 423066f5d6ceSWei Wang rcu_read_lock(); 423166f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4232f88d8ea6SDavid Ahern struct fib6_nh *nh; 4233ad1601aeSDavid Ahern 4234f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4235f88d8ea6SDavid Ahern if (rt->nh) 4236f88d8ea6SDavid Ahern continue; 4237f88d8ea6SDavid Ahern 4238f88d8ea6SDavid Ahern nh = rt->fib6_nh; 4239ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 424093c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 4241ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 42421da177e4SLinus Torvalds break; 42431da177e4SLinus Torvalds } 4244e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 4245e873e4b9SWei Wang rt = NULL; 424666f5d6ceSWei Wang rcu_read_unlock(); 42471da177e4SLinus Torvalds return rt; 42481da177e4SLinus Torvalds } 42491da177e4SLinus Torvalds 42508d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 4251afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 4252ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 4253ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 42541da177e4SLinus Torvalds { 425586872cb5SThomas Graf struct fib6_config cfg = { 4256ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 4257238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 425886872cb5SThomas Graf .fc_ifindex = dev->ifindex, 425986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 426086872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 4261b91d5329SXin Long .fc_protocol = RTPROT_RA, 4262e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 426315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 42645578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 4265afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 426686872cb5SThomas Graf }; 42671da177e4SLinus Torvalds 42684e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 42691da177e4SLinus Torvalds 4270acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 4271830218c1SDavid Ahern struct fib6_table *table; 4272830218c1SDavid Ahern 4273830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 4274830218c1SDavid Ahern if (table) 4275830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 4276830218c1SDavid Ahern } 42771da177e4SLinus Torvalds 4278afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 42791da177e4SLinus Torvalds } 42801da177e4SLinus Torvalds 4281afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 4282afb1d4b5SDavid Ahern struct fib6_table *table) 42831da177e4SLinus Torvalds { 42848d1c802bSDavid Ahern struct fib6_info *rt; 42851da177e4SLinus Torvalds 42861da177e4SLinus Torvalds restart: 428766f5d6ceSWei Wang rcu_read_lock(); 428866f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4289dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 4290dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 4291dcd1f572SDavid Ahern 429293c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 4293e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 4294e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 429566f5d6ceSWei Wang rcu_read_unlock(); 429611dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 42971da177e4SLinus Torvalds goto restart; 42981da177e4SLinus Torvalds } 42991da177e4SLinus Torvalds } 430066f5d6ceSWei Wang rcu_read_unlock(); 4301830218c1SDavid Ahern 4302830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 4303830218c1SDavid Ahern } 4304830218c1SDavid Ahern 4305830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 4306830218c1SDavid Ahern { 4307830218c1SDavid Ahern struct fib6_table *table; 4308830218c1SDavid Ahern struct hlist_head *head; 4309830218c1SDavid Ahern unsigned int h; 4310830218c1SDavid Ahern 4311830218c1SDavid Ahern rcu_read_lock(); 4312830218c1SDavid Ahern 4313830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 4314830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 4315830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 4316830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 4317afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 4318830218c1SDavid Ahern } 4319830218c1SDavid Ahern } 4320830218c1SDavid Ahern 4321830218c1SDavid Ahern rcu_read_unlock(); 43221da177e4SLinus Torvalds } 43231da177e4SLinus Torvalds 43245578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 43255578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 432686872cb5SThomas Graf struct fib6_config *cfg) 432786872cb5SThomas Graf { 43288823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 43298823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 43308823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 43318823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 433267f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 43338823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 43348823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 43358823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 43368823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 43378823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 433886872cb5SThomas Graf 43398823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 434086872cb5SThomas Graf 43418823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 43428823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 43438823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 43448823a3acSMaciej Żenczykowski }; 434586872cb5SThomas Graf } 434686872cb5SThomas Graf 43477c1552daSChristoph Hellwig int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg) 43481da177e4SLinus Torvalds { 434986872cb5SThomas Graf struct fib6_config cfg; 43501da177e4SLinus Torvalds int err; 43511da177e4SLinus Torvalds 43527c1552daSChristoph Hellwig if (cmd != SIOCADDRT && cmd != SIOCDELRT) 43537c1552daSChristoph Hellwig return -EINVAL; 4354af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 43551da177e4SLinus Torvalds return -EPERM; 43561da177e4SLinus Torvalds 43577c1552daSChristoph Hellwig rtmsg_to_fib6_config(net, rtmsg, &cfg); 435886872cb5SThomas Graf 43591da177e4SLinus Torvalds rtnl_lock(); 43601da177e4SLinus Torvalds switch (cmd) { 43611da177e4SLinus Torvalds case SIOCADDRT: 4362acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 43631da177e4SLinus Torvalds break; 43641da177e4SLinus Torvalds case SIOCDELRT: 4365333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 43661da177e4SLinus Torvalds break; 43671da177e4SLinus Torvalds } 43681da177e4SLinus Torvalds rtnl_unlock(); 43691da177e4SLinus Torvalds return err; 43703ff50b79SStephen Hemminger } 43711da177e4SLinus Torvalds 43721da177e4SLinus Torvalds /* 43731da177e4SLinus Torvalds * Drop the packet on the floor 43741da177e4SLinus Torvalds */ 43751da177e4SLinus Torvalds 4376d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 43771da177e4SLinus Torvalds { 4378adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 43791d3fd8a1SStephen Suryaputra struct net *net = dev_net(dst->dev); 43801d3fd8a1SStephen Suryaputra struct inet6_dev *idev; 43811d3fd8a1SStephen Suryaputra int type; 43821d3fd8a1SStephen Suryaputra 43831d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev) && 43841d3fd8a1SStephen Suryaputra dst->dev == net->loopback_dev) 43851d3fd8a1SStephen Suryaputra idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 43861d3fd8a1SStephen Suryaputra else 43871d3fd8a1SStephen Suryaputra idev = ip6_dst_idev(dst); 43881d3fd8a1SStephen Suryaputra 4389612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 4390612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 43910660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 439245bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 43931d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 4394612f09e8SYOSHIFUJI Hideaki break; 4395612f09e8SYOSHIFUJI Hideaki } 4396a8eceea8SJoe Perches fallthrough; 4397612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 43981d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 4399612f09e8SYOSHIFUJI Hideaki break; 4400612f09e8SYOSHIFUJI Hideaki } 44011d3fd8a1SStephen Suryaputra 44021d3fd8a1SStephen Suryaputra /* Start over by dropping the dst for l3mdev case */ 44031d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev)) 44041d3fd8a1SStephen Suryaputra skb_dst_drop(skb); 44051d3fd8a1SStephen Suryaputra 44063ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 44071da177e4SLinus Torvalds kfree_skb(skb); 44081da177e4SLinus Torvalds return 0; 44091da177e4SLinus Torvalds } 44101da177e4SLinus Torvalds 44119ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 44129ce8ade0SThomas Graf { 4413612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 44149ce8ade0SThomas Graf } 44159ce8ade0SThomas Graf 4416ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 44171da177e4SLinus Torvalds { 4418adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4419612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 44201da177e4SLinus Torvalds } 44211da177e4SLinus Torvalds 44229ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 44239ce8ade0SThomas Graf { 4424612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 44259ce8ade0SThomas Graf } 44269ce8ade0SThomas Graf 4427ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 44289ce8ade0SThomas Graf { 4429adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4430612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 44319ce8ade0SThomas Graf } 44329ce8ade0SThomas Graf 44331da177e4SLinus Torvalds /* 44341da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 44351da177e4SLinus Torvalds */ 44361da177e4SLinus Torvalds 4437360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 4438afb1d4b5SDavid Ahern struct inet6_dev *idev, 44391da177e4SLinus Torvalds const struct in6_addr *addr, 4440acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 44411da177e4SLinus Torvalds { 4442c7a1ce39SDavid Ahern struct fib6_config cfg = { 4443c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 4444c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 4445d55a2e37SMaciej Żenczykowski .fc_flags = RTF_UP | RTF_NONEXTHOP, 4446c7a1ce39SDavid Ahern .fc_dst = *addr, 4447c7a1ce39SDavid Ahern .fc_dst_len = 128, 4448c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 4449c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 4450c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 4451c7a1ce39SDavid Ahern }; 4452d55a2e37SMaciej Żenczykowski struct fib6_info *f6i; 44535f02ce24SDavid Ahern 4454e8478e80SDavid Ahern if (anycast) { 4455c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 4456c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 4457e8478e80SDavid Ahern } else { 4458c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 4459c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 4460e8478e80SDavid Ahern } 44611da177e4SLinus Torvalds 4462d55a2e37SMaciej Żenczykowski f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); 44638652f17cSMaciej Żenczykowski if (!IS_ERR(f6i)) 4464d55a2e37SMaciej Żenczykowski f6i->dst_nocount = true; 4465d55a2e37SMaciej Żenczykowski return f6i; 44661da177e4SLinus Torvalds } 44671da177e4SLinus Torvalds 4468c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 4469c3968a85SDaniel Walter struct arg_dev_net_ip { 4470c3968a85SDaniel Walter struct net_device *dev; 4471c3968a85SDaniel Walter struct net *net; 4472c3968a85SDaniel Walter struct in6_addr *addr; 4473c3968a85SDaniel Walter }; 4474c3968a85SDaniel Walter 44758d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 4476c3968a85SDaniel Walter { 4477c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 4478c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 4479c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 4480c3968a85SDaniel Walter 4481f88d8ea6SDavid Ahern if (!rt->nh && 4482f88d8ea6SDavid Ahern ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && 4483421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 448493c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 448560006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 4486c3968a85SDaniel Walter /* remove prefsrc entry */ 448793c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 448860006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 4489c3968a85SDaniel Walter } 4490c3968a85SDaniel Walter return 0; 4491c3968a85SDaniel Walter } 4492c3968a85SDaniel Walter 4493c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 4494c3968a85SDaniel Walter { 4495c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 4496c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 4497c3968a85SDaniel Walter .dev = ifp->idev->dev, 4498c3968a85SDaniel Walter .net = net, 4499c3968a85SDaniel Walter .addr = &ifp->addr, 4500c3968a85SDaniel Walter }; 45010c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 4502c3968a85SDaniel Walter } 4503c3968a85SDaniel Walter 45042b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 4505be7a010dSDuan Jiong 4506be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 45078d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 4508be7a010dSDuan Jiong { 4509be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 4510f88d8ea6SDavid Ahern struct fib6_nh *nh; 4511be7a010dSDuan Jiong 4512f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4513f88d8ea6SDavid Ahern if (rt->nh) 4514f88d8ea6SDavid Ahern return 0; 4515f88d8ea6SDavid Ahern 4516f88d8ea6SDavid Ahern nh = rt->fib6_nh; 451793c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 4518cc5c073aSDavid Ahern nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) 4519be7a010dSDuan Jiong return -1; 4520b16cb459SWei Wang 4521b16cb459SWei Wang /* Further clean up cached routes in exception table. 4522b16cb459SWei Wang * This is needed because cached route may have a different 4523b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 4524b16cb459SWei Wang */ 4525cc5c073aSDavid Ahern fib6_nh_exceptions_clean_tohost(nh, gateway); 4526b16cb459SWei Wang 4527be7a010dSDuan Jiong return 0; 4528be7a010dSDuan Jiong } 4529be7a010dSDuan Jiong 4530be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 4531be7a010dSDuan Jiong { 4532be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 4533be7a010dSDuan Jiong } 4534be7a010dSDuan Jiong 45352127d95aSIdo Schimmel struct arg_netdev_event { 45362127d95aSIdo Schimmel const struct net_device *dev; 45374c981e28SIdo Schimmel union { 4538ecc5663cSDavid Ahern unsigned char nh_flags; 45394c981e28SIdo Schimmel unsigned long event; 45404c981e28SIdo Schimmel }; 45412127d95aSIdo Schimmel }; 45422127d95aSIdo Schimmel 45438d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 4544d7dedee1SIdo Schimmel { 45458d1c802bSDavid Ahern struct fib6_info *iter; 4546d7dedee1SIdo Schimmel struct fib6_node *fn; 4547d7dedee1SIdo Schimmel 454893c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 454993c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4550d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 455193c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4552d7dedee1SIdo Schimmel while (iter) { 455393c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 455433bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 4555d7dedee1SIdo Schimmel return iter; 45568fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 455793c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4558d7dedee1SIdo Schimmel } 4559d7dedee1SIdo Schimmel 4560d7dedee1SIdo Schimmel return NULL; 4561d7dedee1SIdo Schimmel } 4562d7dedee1SIdo Schimmel 4563f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */ 45648d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 4565d7dedee1SIdo Schimmel { 45661cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || 45671cf844c7SDavid Ahern (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && 45681cf844c7SDavid Ahern ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) 4569d7dedee1SIdo Schimmel return true; 4570d7dedee1SIdo Schimmel 4571d7dedee1SIdo Schimmel return false; 4572d7dedee1SIdo Schimmel } 4573d7dedee1SIdo Schimmel 45748d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 4575d7dedee1SIdo Schimmel { 45768d1c802bSDavid Ahern struct fib6_info *iter; 4577d7dedee1SIdo Schimmel int total = 0; 4578d7dedee1SIdo Schimmel 4579d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 45801cf844c7SDavid Ahern total += rt->fib6_nh->fib_nh_weight; 4581d7dedee1SIdo Schimmel 458293c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4583d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 45841cf844c7SDavid Ahern total += iter->fib6_nh->fib_nh_weight; 4585d7dedee1SIdo Schimmel } 4586d7dedee1SIdo Schimmel 4587d7dedee1SIdo Schimmel return total; 4588d7dedee1SIdo Schimmel } 4589d7dedee1SIdo Schimmel 45908d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4591d7dedee1SIdo Schimmel { 4592d7dedee1SIdo Schimmel int upper_bound = -1; 4593d7dedee1SIdo Schimmel 4594d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 45951cf844c7SDavid Ahern *weight += rt->fib6_nh->fib_nh_weight; 4596d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4597d7dedee1SIdo Schimmel total) - 1; 4598d7dedee1SIdo Schimmel } 45991cf844c7SDavid Ahern atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); 4600d7dedee1SIdo Schimmel } 4601d7dedee1SIdo Schimmel 46028d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4603d7dedee1SIdo Schimmel { 46048d1c802bSDavid Ahern struct fib6_info *iter; 4605d7dedee1SIdo Schimmel int weight = 0; 4606d7dedee1SIdo Schimmel 4607d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 4608d7dedee1SIdo Schimmel 460993c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4610d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 4611d7dedee1SIdo Schimmel } 4612d7dedee1SIdo Schimmel 46138d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 4614d7dedee1SIdo Schimmel { 46158d1c802bSDavid Ahern struct fib6_info *first; 4616d7dedee1SIdo Schimmel int total; 4617d7dedee1SIdo Schimmel 4618d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 4619d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 4620d7dedee1SIdo Schimmel * sibling route. 4621d7dedee1SIdo Schimmel */ 462293c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 4623d7dedee1SIdo Schimmel return; 4624d7dedee1SIdo Schimmel 4625d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 4626d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 4627d7dedee1SIdo Schimmel * onwards. 4628d7dedee1SIdo Schimmel */ 4629d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 4630d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 4631d7dedee1SIdo Schimmel return; 4632d7dedee1SIdo Schimmel 4633d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 4634d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 4635d7dedee1SIdo Schimmel } 4636d7dedee1SIdo Schimmel 46378d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 46382127d95aSIdo Schimmel { 46392127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 46407aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 46412127d95aSIdo Schimmel 4642f88d8ea6SDavid Ahern if (rt != net->ipv6.fib6_null_entry && !rt->nh && 46431cf844c7SDavid Ahern rt->fib6_nh->fib_nh_dev == arg->dev) { 46441cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; 46457aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 4646d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 46471de178edSIdo Schimmel } 46482127d95aSIdo Schimmel 46492127d95aSIdo Schimmel return 0; 46502127d95aSIdo Schimmel } 46512127d95aSIdo Schimmel 4652ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 46532127d95aSIdo Schimmel { 46542127d95aSIdo Schimmel struct arg_netdev_event arg = { 46552127d95aSIdo Schimmel .dev = dev, 46566802f3adSIdo Schimmel { 46572127d95aSIdo Schimmel .nh_flags = nh_flags, 46586802f3adSIdo Schimmel }, 46592127d95aSIdo Schimmel }; 46602127d95aSIdo Schimmel 46612127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 46622127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 46632127d95aSIdo Schimmel 46642127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 46652127d95aSIdo Schimmel } 46662127d95aSIdo Schimmel 4667f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */ 46688d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 46691de178edSIdo Schimmel const struct net_device *dev) 46701de178edSIdo Schimmel { 46718d1c802bSDavid Ahern struct fib6_info *iter; 46721de178edSIdo Schimmel 46731cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 46741de178edSIdo Schimmel return true; 467593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46761cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 46771de178edSIdo Schimmel return true; 46781de178edSIdo Schimmel 46791de178edSIdo Schimmel return false; 46801de178edSIdo Schimmel } 46811de178edSIdo Schimmel 46828d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 46831de178edSIdo Schimmel { 46848d1c802bSDavid Ahern struct fib6_info *iter; 46851de178edSIdo Schimmel 46861de178edSIdo Schimmel rt->should_flush = 1; 468793c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 46881de178edSIdo Schimmel iter->should_flush = 1; 46891de178edSIdo Schimmel } 46901de178edSIdo Schimmel 46918d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 46921de178edSIdo Schimmel const struct net_device *down_dev) 46931de178edSIdo Schimmel { 46948d1c802bSDavid Ahern struct fib6_info *iter; 46951de178edSIdo Schimmel unsigned int dead = 0; 46961de178edSIdo Schimmel 46971cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == down_dev || 46981cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 46991de178edSIdo Schimmel dead++; 470093c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 47011cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == down_dev || 47021cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 47031de178edSIdo Schimmel dead++; 47041de178edSIdo Schimmel 47051de178edSIdo Schimmel return dead; 47061de178edSIdo Schimmel } 47071de178edSIdo Schimmel 47088d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 47091de178edSIdo Schimmel const struct net_device *dev, 4710ecc5663cSDavid Ahern unsigned char nh_flags) 47111de178edSIdo Schimmel { 47128d1c802bSDavid Ahern struct fib6_info *iter; 47131de178edSIdo Schimmel 47141cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 47151cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= nh_flags; 471693c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 47171cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 47181cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags |= nh_flags; 47191de178edSIdo Schimmel } 47201de178edSIdo Schimmel 4721a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 47228d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 47231da177e4SLinus Torvalds { 47244c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 47254c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 47267aef6859SDavid Ahern struct net *net = dev_net(dev); 47278ed67789SDaniel Lezcano 4728f88d8ea6SDavid Ahern if (rt == net->ipv6.fib6_null_entry || rt->nh) 472927c6fa73SIdo Schimmel return 0; 473027c6fa73SIdo Schimmel 473127c6fa73SIdo Schimmel switch (arg->event) { 473227c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 47331cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 473427c6fa73SIdo Schimmel case NETDEV_DOWN: 47351de178edSIdo Schimmel if (rt->should_flush) 473627c6fa73SIdo Schimmel return -1; 473793c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 47381cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 47391de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 47401de178edSIdo Schimmel unsigned int count; 47411de178edSIdo Schimmel 47421de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 474393c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 47441de178edSIdo Schimmel rt6_multipath_flush(rt); 47451de178edSIdo Schimmel return -1; 47461de178edSIdo Schimmel } 47471de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 47481de178edSIdo Schimmel RTNH_F_LINKDOWN); 47497aef6859SDavid Ahern fib6_update_sernum(net, rt); 4750d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 47511de178edSIdo Schimmel } 47521de178edSIdo Schimmel return -2; 475327c6fa73SIdo Schimmel case NETDEV_CHANGE: 47541cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev != dev || 475593c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 475627c6fa73SIdo Schimmel break; 47571cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 4758d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 475927c6fa73SIdo Schimmel break; 47602b241361SIdo Schimmel } 4761c159d30cSDavid S. Miller 47621da177e4SLinus Torvalds return 0; 47631da177e4SLinus Torvalds } 47641da177e4SLinus Torvalds 476527c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 47661da177e4SLinus Torvalds { 47674c981e28SIdo Schimmel struct arg_netdev_event arg = { 47688ed67789SDaniel Lezcano .dev = dev, 47696802f3adSIdo Schimmel { 47704c981e28SIdo Schimmel .event = event, 47716802f3adSIdo Schimmel }, 47728ed67789SDaniel Lezcano }; 47737c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 47748ed67789SDaniel Lezcano 47757c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 47767c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 47777c6bb7d2SDavid Ahern else 47787c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 47794c981e28SIdo Schimmel } 47804c981e28SIdo Schimmel 47814c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 47824c981e28SIdo Schimmel { 47834c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 47844c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 47854c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 47861da177e4SLinus Torvalds } 47871da177e4SLinus Torvalds 478895c96174SEric Dumazet struct rt6_mtu_change_arg { 47891da177e4SLinus Torvalds struct net_device *dev; 479095c96174SEric Dumazet unsigned int mtu; 4791c0b220cfSDavid Ahern struct fib6_info *f6i; 47921da177e4SLinus Torvalds }; 47931da177e4SLinus Torvalds 4794cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) 4795c0b220cfSDavid Ahern { 4796c0b220cfSDavid Ahern struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; 4797cc5c073aSDavid Ahern struct fib6_info *f6i = arg->f6i; 4798c0b220cfSDavid Ahern 4799c0b220cfSDavid Ahern /* For administrative MTU increase, there is no way to discover 4800c0b220cfSDavid Ahern * IPv6 PMTU increase, so PMTU increase should be updated here. 4801c0b220cfSDavid Ahern * Since RFC 1981 doesn't include administrative MTU increase 4802c0b220cfSDavid Ahern * update PMTU increase is a MUST. (i.e. jumbo frame) 4803c0b220cfSDavid Ahern */ 4804c0b220cfSDavid Ahern if (nh->fib_nh_dev == arg->dev) { 4805c0b220cfSDavid Ahern struct inet6_dev *idev = __in6_dev_get(arg->dev); 4806c0b220cfSDavid Ahern u32 mtu = f6i->fib6_pmtu; 4807c0b220cfSDavid Ahern 4808c0b220cfSDavid Ahern if (mtu >= arg->mtu || 4809c0b220cfSDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4810c0b220cfSDavid Ahern fib6_metric_set(f6i, RTAX_MTU, arg->mtu); 4811c0b220cfSDavid Ahern 4812c0b220cfSDavid Ahern spin_lock_bh(&rt6_exception_lock); 4813cc5c073aSDavid Ahern rt6_exceptions_update_pmtu(idev, nh, arg->mtu); 4814c0b220cfSDavid Ahern spin_unlock_bh(&rt6_exception_lock); 4815c0b220cfSDavid Ahern } 4816c0b220cfSDavid Ahern 4817c0b220cfSDavid Ahern return 0; 4818c0b220cfSDavid Ahern } 4819c0b220cfSDavid Ahern 4820c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) 48211da177e4SLinus Torvalds { 48221da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 48231da177e4SLinus Torvalds struct inet6_dev *idev; 48241da177e4SLinus Torvalds 48251da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 48261da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 48271da177e4SLinus Torvalds We still use this lock to block changes 48281da177e4SLinus Torvalds caused by addrconf/ndisc. 48291da177e4SLinus Torvalds */ 48301da177e4SLinus Torvalds 48311da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 483238308473SDavid S. Miller if (!idev) 48331da177e4SLinus Torvalds return 0; 48341da177e4SLinus Torvalds 4835c0b220cfSDavid Ahern if (fib6_metric_locked(f6i, RTAX_MTU)) 48361da177e4SLinus Torvalds return 0; 4837c0b220cfSDavid Ahern 4838c0b220cfSDavid Ahern arg->f6i = f6i; 48392d44234bSDavid Ahern if (f6i->nh) { 48402d44234bSDavid Ahern /* fib6_nh_mtu_change only returns 0, so this is safe */ 48412d44234bSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, 48422d44234bSDavid Ahern arg); 48432d44234bSDavid Ahern } 48442d44234bSDavid Ahern 48451cf844c7SDavid Ahern return fib6_nh_mtu_change(f6i->fib6_nh, arg); 48461da177e4SLinus Torvalds } 48471da177e4SLinus Torvalds 484895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 48491da177e4SLinus Torvalds { 4850c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4851c71099acSThomas Graf .dev = dev, 4852c71099acSThomas Graf .mtu = mtu, 4853c71099acSThomas Graf }; 48541da177e4SLinus Torvalds 48550c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 4858ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 485975425657SDavid Ahern [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 48605176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4861aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 486286872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4863ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 486486872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 486586872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 486651ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4867c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 486819e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 486919e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 487032bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4871622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 48723b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4873aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4874eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4875eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4876eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 48775b98324eSDavid Ahern [RTA_NH_ID] = { .type = NLA_U32 }, 487886872cb5SThomas Graf }; 487986872cb5SThomas Graf 488086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4881333c4301SDavid Ahern struct fib6_config *cfg, 4882333c4301SDavid Ahern struct netlink_ext_ack *extack) 48831da177e4SLinus Torvalds { 488486872cb5SThomas Graf struct rtmsg *rtm; 488586872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4886c78ba6d6SLubomir Rintel unsigned int pref; 488786872cb5SThomas Graf int err; 48881da177e4SLinus Torvalds 48898cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 48908cb08174SJohannes Berg rtm_ipv6_policy, extack); 489186872cb5SThomas Graf if (err < 0) 489286872cb5SThomas Graf goto errout; 48931da177e4SLinus Torvalds 489486872cb5SThomas Graf err = -EINVAL; 489586872cb5SThomas Graf rtm = nlmsg_data(nlh); 489686872cb5SThomas Graf 489784db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 489884db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 489984db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 490084db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 490184db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 490284db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 490384db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 490484db8407SMaciej Żenczykowski 490584db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 490684db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 490784db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 490884db8407SMaciej Żenczykowski }; 490986872cb5SThomas Graf 4910ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4911ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4912b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4913b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 491486872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 491586872cb5SThomas Graf 4916ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4917ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4918ab79ad14SMaciej Żenczykowski 49191f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 49201f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 49211f56a01fSMartin KaFai Lau 4922fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4923fc1e64e1SDavid Ahern 49245b98324eSDavid Ahern if (tb[RTA_NH_ID]) { 49255b98324eSDavid Ahern if (tb[RTA_GATEWAY] || tb[RTA_OIF] || 49265b98324eSDavid Ahern tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { 49275b98324eSDavid Ahern NL_SET_ERR_MSG(extack, 49285b98324eSDavid Ahern "Nexthop specification and nexthop id are mutually exclusive"); 49295b98324eSDavid Ahern goto errout; 49305b98324eSDavid Ahern } 49315b98324eSDavid Ahern cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); 49325b98324eSDavid Ahern } 49335b98324eSDavid Ahern 493486872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 493567b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 493686872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 49371da177e4SLinus Torvalds } 4938e3818541SDavid Ahern if (tb[RTA_VIA]) { 4939e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 4940e3818541SDavid Ahern goto errout; 4941e3818541SDavid Ahern } 494286872cb5SThomas Graf 494386872cb5SThomas Graf if (tb[RTA_DST]) { 494486872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 494586872cb5SThomas Graf 494686872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 494786872cb5SThomas Graf goto errout; 494886872cb5SThomas Graf 494986872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 49501da177e4SLinus Torvalds } 495186872cb5SThomas Graf 495286872cb5SThomas Graf if (tb[RTA_SRC]) { 495386872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 495486872cb5SThomas Graf 495586872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 495686872cb5SThomas Graf goto errout; 495786872cb5SThomas Graf 495886872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 49591da177e4SLinus Torvalds } 496086872cb5SThomas Graf 4961c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 496267b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4963c3968a85SDaniel Walter 496486872cb5SThomas Graf if (tb[RTA_OIF]) 496586872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 496686872cb5SThomas Graf 496786872cb5SThomas Graf if (tb[RTA_PRIORITY]) 496886872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 496986872cb5SThomas Graf 497086872cb5SThomas Graf if (tb[RTA_METRICS]) { 497186872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 497286872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 49731da177e4SLinus Torvalds } 497486872cb5SThomas Graf 497586872cb5SThomas Graf if (tb[RTA_TABLE]) 497686872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 497786872cb5SThomas Graf 497851ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 497951ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 498051ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 49819ed59592SDavid Ahern 49829ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4983c255bd68SDavid Ahern cfg->fc_mp_len, extack); 49849ed59592SDavid Ahern if (err < 0) 49859ed59592SDavid Ahern goto errout; 498651ebd318SNicolas Dichtel } 498751ebd318SNicolas Dichtel 4988c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4989c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4990c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4991c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4992c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4993c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4994c78ba6d6SLubomir Rintel } 4995c78ba6d6SLubomir Rintel 499619e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 499719e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 499819e42e45SRoopa Prabhu 49999ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 500019e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 500119e42e45SRoopa Prabhu 5002c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 50039ed59592SDavid Ahern if (err < 0) 50049ed59592SDavid Ahern goto errout; 50059ed59592SDavid Ahern } 50069ed59592SDavid Ahern 500732bc201eSXin Long if (tb[RTA_EXPIRES]) { 500832bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 500932bc201eSXin Long 501032bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 501132bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 501232bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 501332bc201eSXin Long } 501432bc201eSXin Long } 501532bc201eSXin Long 501686872cb5SThomas Graf err = 0; 501786872cb5SThomas Graf errout: 501886872cb5SThomas Graf return err; 50191da177e4SLinus Torvalds } 50201da177e4SLinus Torvalds 50216b9ea5a6SRoopa Prabhu struct rt6_nh { 50228d1c802bSDavid Ahern struct fib6_info *fib6_info; 50236b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 50246b9ea5a6SRoopa Prabhu struct list_head next; 50256b9ea5a6SRoopa Prabhu }; 50266b9ea5a6SRoopa Prabhu 5027d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 5028d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 50298d1c802bSDavid Ahern struct fib6_info *rt, 50308d1c802bSDavid Ahern struct fib6_config *r_cfg) 50316b9ea5a6SRoopa Prabhu { 50326b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 50336b9ea5a6SRoopa Prabhu int err = -EEXIST; 50346b9ea5a6SRoopa Prabhu 50356b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 50368d1c802bSDavid Ahern /* check if fib6_info already exists */ 50378d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 50386b9ea5a6SRoopa Prabhu return err; 50396b9ea5a6SRoopa Prabhu } 50406b9ea5a6SRoopa Prabhu 50416b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 50426b9ea5a6SRoopa Prabhu if (!nh) 50436b9ea5a6SRoopa Prabhu return -ENOMEM; 50448d1c802bSDavid Ahern nh->fib6_info = rt; 50456b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 50466b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 50476b9ea5a6SRoopa Prabhu 50486b9ea5a6SRoopa Prabhu return 0; 50496b9ea5a6SRoopa Prabhu } 50506b9ea5a6SRoopa Prabhu 50518d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 50528d1c802bSDavid Ahern struct fib6_info *rt_last, 50533b1137feSDavid Ahern struct nl_info *info, 50543b1137feSDavid Ahern __u16 nlflags) 50553b1137feSDavid Ahern { 50563b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 50573b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 50583b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 50593b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 50603b1137feSDavid Ahern * the list, find the first sibling of the last route appended 50613b1137feSDavid Ahern */ 506293c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 506393c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 50648d1c802bSDavid Ahern struct fib6_info, 506593c2fb25SDavid Ahern fib6_siblings); 50663b1137feSDavid Ahern } 50673b1137feSDavid Ahern 50683b1137feSDavid Ahern if (rt) 50693b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 50703b1137feSDavid Ahern } 50713b1137feSDavid Ahern 50720ee0f47cSIdo Schimmel static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) 50730ee0f47cSIdo Schimmel { 50740ee0f47cSIdo Schimmel bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); 50750ee0f47cSIdo Schimmel bool should_notify = false; 50760ee0f47cSIdo Schimmel struct fib6_info *leaf; 50770ee0f47cSIdo Schimmel struct fib6_node *fn; 50780ee0f47cSIdo Schimmel 50790ee0f47cSIdo Schimmel rcu_read_lock(); 50800ee0f47cSIdo Schimmel fn = rcu_dereference(rt->fib6_node); 50810ee0f47cSIdo Schimmel if (!fn) 50820ee0f47cSIdo Schimmel goto out; 50830ee0f47cSIdo Schimmel 50840ee0f47cSIdo Schimmel leaf = rcu_dereference(fn->leaf); 50850ee0f47cSIdo Schimmel if (!leaf) 50860ee0f47cSIdo Schimmel goto out; 50870ee0f47cSIdo Schimmel 50880ee0f47cSIdo Schimmel if (rt == leaf || 50890ee0f47cSIdo Schimmel (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && 50900ee0f47cSIdo Schimmel rt6_qualify_for_ecmp(leaf))) 50910ee0f47cSIdo Schimmel should_notify = true; 50920ee0f47cSIdo Schimmel out: 50930ee0f47cSIdo Schimmel rcu_read_unlock(); 50940ee0f47cSIdo Schimmel 50950ee0f47cSIdo Schimmel return should_notify; 50960ee0f47cSIdo Schimmel } 50970ee0f47cSIdo Schimmel 5098333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 5099333c4301SDavid Ahern struct netlink_ext_ack *extack) 510051ebd318SNicolas Dichtel { 51018d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 51023b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 510351ebd318SNicolas Dichtel struct fib6_config r_cfg; 510451ebd318SNicolas Dichtel struct rtnexthop *rtnh; 51058d1c802bSDavid Ahern struct fib6_info *rt; 51066b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 51076b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 51083b1137feSDavid Ahern __u16 nlflags; 510951ebd318SNicolas Dichtel int remaining; 511051ebd318SNicolas Dichtel int attrlen; 51116b9ea5a6SRoopa Prabhu int err = 1; 51126b9ea5a6SRoopa Prabhu int nhn = 0; 51136b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 51146b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 51156b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 511651ebd318SNicolas Dichtel 51173b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 51183b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 51193b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 51203b1137feSDavid Ahern 512135f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 512251ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 512351ebd318SNicolas Dichtel 51246b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 51258d1c802bSDavid Ahern * fib6_info structs per nexthop 51266b9ea5a6SRoopa Prabhu */ 512751ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 512851ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 512951ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 513051ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 513151ebd318SNicolas Dichtel 513251ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 513351ebd318SNicolas Dichtel if (attrlen > 0) { 513451ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 513551ebd318SNicolas Dichtel 513651ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 513751ebd318SNicolas Dichtel if (nla) { 513867b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 513951ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 514051ebd318SNicolas Dichtel } 514119e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 514219e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 514319e42e45SRoopa Prabhu if (nla) 514419e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 514551ebd318SNicolas Dichtel } 51466b9ea5a6SRoopa Prabhu 514768e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 5148acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 51498c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 51508c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 51518c5b83f0SRoopa Prabhu rt = NULL; 51526b9ea5a6SRoopa Prabhu goto cleanup; 51538c5b83f0SRoopa Prabhu } 5154b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 5155b5d2d75eSDavid Ahern err = -EINVAL; 5156b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 5157b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 5158b5d2d75eSDavid Ahern fib6_info_release(rt); 5159b5d2d75eSDavid Ahern goto cleanup; 5160b5d2d75eSDavid Ahern } 51616b9ea5a6SRoopa Prabhu 51621cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; 5163398958aeSIdo Schimmel 5164d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 5165d4ead6b3SDavid Ahern rt, &r_cfg); 516651ebd318SNicolas Dichtel if (err) { 516793531c67SDavid Ahern fib6_info_release(rt); 51686b9ea5a6SRoopa Prabhu goto cleanup; 516951ebd318SNicolas Dichtel } 51706b9ea5a6SRoopa Prabhu 51716b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 517251ebd318SNicolas Dichtel } 51736b9ea5a6SRoopa Prabhu 51749eee3b49SIdo Schimmel if (list_empty(&rt6_nh_list)) { 51759eee3b49SIdo Schimmel NL_SET_ERR_MSG(extack, 51769eee3b49SIdo Schimmel "Invalid nexthop configuration - no valid nexthops"); 51779eee3b49SIdo Schimmel return -EINVAL; 51789eee3b49SIdo Schimmel } 51799eee3b49SIdo Schimmel 51803b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 51813b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 51823b1137feSDavid Ahern * the full route when done 51833b1137feSDavid Ahern */ 51843b1137feSDavid Ahern info->skip_notify = 1; 51853b1137feSDavid Ahern 5186ebee3cadSIdo Schimmel /* For add and replace, send one notification with all nexthops. For 5187ebee3cadSIdo Schimmel * append, send one notification with all appended nexthops. 5188ebee3cadSIdo Schimmel */ 5189ebee3cadSIdo Schimmel info->skip_notify_kernel = 1; 5190ebee3cadSIdo Schimmel 51916b9ea5a6SRoopa Prabhu err_nh = NULL; 51926b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 51938d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 51948d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 51953b1137feSDavid Ahern 5196f7225172SDavid Ahern if (!err) { 5197f7225172SDavid Ahern /* save reference to last route successfully inserted */ 5198f7225172SDavid Ahern rt_last = nh->fib6_info; 5199f7225172SDavid Ahern 52006b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 5201f7225172SDavid Ahern if (!rt_notif) 52028d1c802bSDavid Ahern rt_notif = nh->fib6_info; 5203f7225172SDavid Ahern } 52046b9ea5a6SRoopa Prabhu 52058d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 52068d1c802bSDavid Ahern nh->fib6_info = NULL; 52076b9ea5a6SRoopa Prabhu if (err) { 52086b9ea5a6SRoopa Prabhu if (replace && nhn) 5209a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 5210a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 52116b9ea5a6SRoopa Prabhu err_nh = nh; 52126b9ea5a6SRoopa Prabhu goto add_errout; 52136b9ea5a6SRoopa Prabhu } 52146b9ea5a6SRoopa Prabhu 52151a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 521627596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 521727596472SMichal Kubeček * we have already failed to add the first nexthop: 521827596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 521927596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 522027596472SMichal Kubeček * be added to it. 52211a72418bSNicolas Dichtel */ 522227596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 522327596472SMichal Kubeček NLM_F_REPLACE); 5224afecdb37SBenjamin Poirier cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; 52256b9ea5a6SRoopa Prabhu nhn++; 52266b9ea5a6SRoopa Prabhu } 52276b9ea5a6SRoopa Prabhu 52280ee0f47cSIdo Schimmel /* An in-kernel notification should only be sent in case the new 52290ee0f47cSIdo Schimmel * multipath route is added as the first route in the node, or if 52300ee0f47cSIdo Schimmel * it was appended to it. We pass 'rt_notif' since it is the first 52310ee0f47cSIdo Schimmel * sibling and might allow us to skip some checks in the replace case. 52320ee0f47cSIdo Schimmel */ 52330ee0f47cSIdo Schimmel if (ip6_route_mpath_should_notify(rt_notif)) { 52340ee0f47cSIdo Schimmel enum fib_event_type fib_event; 52350ee0f47cSIdo Schimmel 52360ee0f47cSIdo Schimmel if (rt_notif->fib6_nsiblings != nhn - 1) 52370ee0f47cSIdo Schimmel fib_event = FIB_EVENT_ENTRY_APPEND; 52380ee0f47cSIdo Schimmel else 5239caafb250SIdo Schimmel fib_event = FIB_EVENT_ENTRY_REPLACE; 52400ee0f47cSIdo Schimmel 52410ee0f47cSIdo Schimmel err = call_fib6_multipath_entry_notifiers(info->nl_net, 52420ee0f47cSIdo Schimmel fib_event, rt_notif, 52430ee0f47cSIdo Schimmel nhn - 1, extack); 52440ee0f47cSIdo Schimmel if (err) { 52450ee0f47cSIdo Schimmel /* Delete all the siblings that were just added */ 52460ee0f47cSIdo Schimmel err_nh = NULL; 52470ee0f47cSIdo Schimmel goto add_errout; 52480ee0f47cSIdo Schimmel } 52490ee0f47cSIdo Schimmel } 5250ebee3cadSIdo Schimmel 52513b1137feSDavid Ahern /* success ... tell user about new route */ 52523b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 52536b9ea5a6SRoopa Prabhu goto cleanup; 52546b9ea5a6SRoopa Prabhu 52556b9ea5a6SRoopa Prabhu add_errout: 52563b1137feSDavid Ahern /* send notification for routes that were added so that 52573b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 52583b1137feSDavid Ahern * coherent 52593b1137feSDavid Ahern */ 52603b1137feSDavid Ahern if (rt_notif) 52613b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 52623b1137feSDavid Ahern 52636b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 52646b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 52656b9ea5a6SRoopa Prabhu if (err_nh == nh) 52666b9ea5a6SRoopa Prabhu break; 5267333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 52686b9ea5a6SRoopa Prabhu } 52696b9ea5a6SRoopa Prabhu 52706b9ea5a6SRoopa Prabhu cleanup: 52716b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 52728d1c802bSDavid Ahern if (nh->fib6_info) 52738d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 52746b9ea5a6SRoopa Prabhu list_del(&nh->next); 52756b9ea5a6SRoopa Prabhu kfree(nh); 52766b9ea5a6SRoopa Prabhu } 52776b9ea5a6SRoopa Prabhu 52786b9ea5a6SRoopa Prabhu return err; 52796b9ea5a6SRoopa Prabhu } 52806b9ea5a6SRoopa Prabhu 5281333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 5282333c4301SDavid Ahern struct netlink_ext_ack *extack) 52836b9ea5a6SRoopa Prabhu { 52846b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 52856b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 52866b9ea5a6SRoopa Prabhu int remaining; 52876b9ea5a6SRoopa Prabhu int attrlen; 52886b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 52896b9ea5a6SRoopa Prabhu 52906b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 52916b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 52926b9ea5a6SRoopa Prabhu 52936b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 52946b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 52956b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 52966b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 52976b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 52986b9ea5a6SRoopa Prabhu 52996b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 53006b9ea5a6SRoopa Prabhu if (attrlen > 0) { 53016b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 53026b9ea5a6SRoopa Prabhu 53036b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 53046b9ea5a6SRoopa Prabhu if (nla) { 53056b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 53066b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 53076b9ea5a6SRoopa Prabhu } 53086b9ea5a6SRoopa Prabhu } 5309333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 53106b9ea5a6SRoopa Prabhu if (err) 53116b9ea5a6SRoopa Prabhu last_err = err; 53126b9ea5a6SRoopa Prabhu 531351ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 531451ebd318SNicolas Dichtel } 531551ebd318SNicolas Dichtel 531651ebd318SNicolas Dichtel return last_err; 531751ebd318SNicolas Dichtel } 531851ebd318SNicolas Dichtel 5319c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5320c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 53211da177e4SLinus Torvalds { 532286872cb5SThomas Graf struct fib6_config cfg; 532386872cb5SThomas Graf int err; 53241da177e4SLinus Torvalds 5325333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 532686872cb5SThomas Graf if (err < 0) 532786872cb5SThomas Graf return err; 532886872cb5SThomas Graf 53295b98324eSDavid Ahern if (cfg.fc_nh_id && 53305b98324eSDavid Ahern !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { 53315b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 53325b98324eSDavid Ahern return -EINVAL; 53335b98324eSDavid Ahern } 53345b98324eSDavid Ahern 533551ebd318SNicolas Dichtel if (cfg.fc_mp) 5336333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 53370ae81335SDavid Ahern else { 53380ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 5339333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 53401da177e4SLinus Torvalds } 53410ae81335SDavid Ahern } 53421da177e4SLinus Torvalds 5343c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5344c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 53451da177e4SLinus Torvalds { 534686872cb5SThomas Graf struct fib6_config cfg; 534786872cb5SThomas Graf int err; 53481da177e4SLinus Torvalds 5349333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 535086872cb5SThomas Graf if (err < 0) 535186872cb5SThomas Graf return err; 535286872cb5SThomas Graf 535367f69513SDavid Ahern if (cfg.fc_metric == 0) 535467f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 535567f69513SDavid Ahern 535651ebd318SNicolas Dichtel if (cfg.fc_mp) 5357333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 535851ebd318SNicolas Dichtel else 5359acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 53601da177e4SLinus Torvalds } 53611da177e4SLinus Torvalds 5362a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */ 5363a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) 5364339bf98fSThomas Graf { 5365a1b7a1f0SDavid Ahern int *nexthop_len = arg; 5366beb1afacSDavid Ahern 5367a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ 5368a1b7a1f0SDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5369a1b7a1f0SDavid Ahern + nla_total_size(16); /* RTA_GATEWAY */ 5370f88d8ea6SDavid Ahern 5371a1b7a1f0SDavid Ahern if (nh->fib_nh_lws) { 5372a1b7a1f0SDavid Ahern /* RTA_ENCAP_TYPE */ 5373a1b7a1f0SDavid Ahern *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5374a1b7a1f0SDavid Ahern /* RTA_ENCAP */ 5375a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(2); 5376a1b7a1f0SDavid Ahern } 5377a1b7a1f0SDavid Ahern 5378a1b7a1f0SDavid Ahern return 0; 5379a1b7a1f0SDavid Ahern } 5380a1b7a1f0SDavid Ahern 5381a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i) 5382a1b7a1f0SDavid Ahern { 5383a1b7a1f0SDavid Ahern int nexthop_len; 5384a1b7a1f0SDavid Ahern 5385a1b7a1f0SDavid Ahern if (f6i->nh) { 5386a1b7a1f0SDavid Ahern nexthop_len = nla_total_size(4); /* RTA_NH_ID */ 5387a1b7a1f0SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, 5388a1b7a1f0SDavid Ahern &nexthop_len); 5389a1b7a1f0SDavid Ahern } else { 5390a1b7a1f0SDavid Ahern struct fib6_nh *nh = f6i->fib6_nh; 5391a1b7a1f0SDavid Ahern 5392a1b7a1f0SDavid Ahern nexthop_len = 0; 5393a1b7a1f0SDavid Ahern if (f6i->fib6_nsiblings) { 5394beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 5395beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5396beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 5397a1b7a1f0SDavid Ahern + lwtunnel_get_encap_size(nh->fib_nh_lws); 5398beb1afacSDavid Ahern 5399a1b7a1f0SDavid Ahern nexthop_len *= f6i->fib6_nsiblings; 5400a1b7a1f0SDavid Ahern } 5401a1b7a1f0SDavid Ahern nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5402beb1afacSDavid Ahern } 5403beb1afacSDavid Ahern 5404339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 5405339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 5406339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 5407339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 5408339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 5409339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 5410339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 5411339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 5412339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 54136a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 5414ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 5415c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 541619e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 5417beb1afacSDavid Ahern + nexthop_len; 5418beb1afacSDavid Ahern } 5419beb1afacSDavid Ahern 5420f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, 5421f88d8ea6SDavid Ahern unsigned char *flags) 5422f88d8ea6SDavid Ahern { 5423f88d8ea6SDavid Ahern if (nexthop_is_multipath(nh)) { 5424f88d8ea6SDavid Ahern struct nlattr *mp; 5425f88d8ea6SDavid Ahern 54264255ff05SDavid Ahern mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5427f88d8ea6SDavid Ahern if (!mp) 5428f88d8ea6SDavid Ahern goto nla_put_failure; 5429f88d8ea6SDavid Ahern 54307bdf4de1SDonald Sharp if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) 5431f88d8ea6SDavid Ahern goto nla_put_failure; 5432f88d8ea6SDavid Ahern 5433f88d8ea6SDavid Ahern nla_nest_end(skb, mp); 5434f88d8ea6SDavid Ahern } else { 5435f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 5436f88d8ea6SDavid Ahern 5437f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(nh); 54387bdf4de1SDonald Sharp if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, 5439f88d8ea6SDavid Ahern flags, false) < 0) 5440f88d8ea6SDavid Ahern goto nla_put_failure; 5441f88d8ea6SDavid Ahern } 5442f88d8ea6SDavid Ahern 5443f88d8ea6SDavid Ahern return 0; 5444f88d8ea6SDavid Ahern 5445f88d8ea6SDavid Ahern nla_put_failure: 5446f88d8ea6SDavid Ahern return -EMSGSIZE; 5447f88d8ea6SDavid Ahern } 5448f88d8ea6SDavid Ahern 5449d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 54508d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 5451d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 545215e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 5453f8cfe2ceSDavid Ahern unsigned int flags) 54541da177e4SLinus Torvalds { 545522d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 545622d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 545722d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 5458f88d8ea6SDavid Ahern unsigned char nh_flags = 0; 54591da177e4SLinus Torvalds struct nlmsghdr *nlh; 546022d0bd82SXin Long struct rtmsg *rtm; 5461d4ead6b3SDavid Ahern long expires = 0; 54621da177e4SLinus Torvalds 546315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 546438308473SDavid S. Miller if (!nlh) 546526932566SPatrick McHardy return -EMSGSIZE; 54662d7202bfSThomas Graf 546722d0bd82SXin Long if (rt6) { 546822d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 546922d0bd82SXin Long rt6_src = &rt6->rt6i_src; 547022d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 547122d0bd82SXin Long } else { 547222d0bd82SXin Long rt6_dst = &rt->fib6_dst; 547322d0bd82SXin Long rt6_src = &rt->fib6_src; 547422d0bd82SXin Long rt6_flags = rt->fib6_flags; 547522d0bd82SXin Long } 547622d0bd82SXin Long 54772d7202bfSThomas Graf rtm = nlmsg_data(nlh); 54781da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 547922d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 548022d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 54811da177e4SLinus Torvalds rtm->rtm_tos = 0; 548293c2fb25SDavid Ahern if (rt->fib6_table) 548393c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 5484c71099acSThomas Graf else 54859e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 548697f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 5487c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 5488c78679e8SDavid S. Miller goto nla_put_failure; 5489e8478e80SDavid Ahern 5490e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 54911da177e4SLinus Torvalds rtm->rtm_flags = 0; 54921da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 549393c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 54941da177e4SLinus Torvalds 549522d0bd82SXin Long if (rt6_flags & RTF_CACHE) 54961da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 54971da177e4SLinus Torvalds 5498d4ead6b3SDavid Ahern if (dest) { 5499d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 5500c78679e8SDavid S. Miller goto nla_put_failure; 55011da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 55021da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 550322d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 5504c78679e8SDavid S. Miller goto nla_put_failure; 55051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 55061da177e4SLinus Torvalds if (src) { 5507930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 5508c78679e8SDavid S. Miller goto nla_put_failure; 55091da177e4SLinus Torvalds rtm->rtm_src_len = 128; 5510c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 551122d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 5512c78679e8SDavid S. Miller goto nla_put_failure; 55131da177e4SLinus Torvalds #endif 55147bc570c8SYOSHIFUJI Hideaki if (iif) { 55157bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 551622d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 5517fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 55182cf75070SNikolay Aleksandrov 55197bc570c8SYOSHIFUJI Hideaki if (err == 0) 55207bc570c8SYOSHIFUJI Hideaki return 0; 5521fd61c6baSDavid Ahern if (err < 0) 55227bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 55237bc570c8SYOSHIFUJI Hideaki } else 55247bc570c8SYOSHIFUJI Hideaki #endif 5525c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 5526c78679e8SDavid S. Miller goto nla_put_failure; 5527d4ead6b3SDavid Ahern } else if (dest) { 55281da177e4SLinus Torvalds struct in6_addr saddr_buf; 5529d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 5530930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5531c78679e8SDavid S. Miller goto nla_put_failure; 5532c3968a85SDaniel Walter } 5533c3968a85SDaniel Walter 553493c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 5535c3968a85SDaniel Walter struct in6_addr saddr_buf; 553693c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 5537930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5538c78679e8SDavid S. Miller goto nla_put_failure; 55391da177e4SLinus Torvalds } 55402d7202bfSThomas Graf 5541d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 5542d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 55432d7202bfSThomas Graf goto nla_put_failure; 55442d7202bfSThomas Graf 554593c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 5546beb1afacSDavid Ahern goto nla_put_failure; 5547beb1afacSDavid Ahern 5548beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 5549beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 5550beb1afacSDavid Ahern */ 555122d0bd82SXin Long if (rt6) { 555222d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 555322d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 555422d0bd82SXin Long goto nla_put_failure; 555522d0bd82SXin Long 555622d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 555722d0bd82SXin Long goto nla_put_failure; 555822d0bd82SXin Long } else if (rt->fib6_nsiblings) { 55598d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 5560beb1afacSDavid Ahern struct nlattr *mp; 5561beb1afacSDavid Ahern 5562ae0be8deSMichal Kubecek mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5563beb1afacSDavid Ahern if (!mp) 5564beb1afacSDavid Ahern goto nla_put_failure; 5565beb1afacSDavid Ahern 55661cf844c7SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, 55677bdf4de1SDonald Sharp rt->fib6_nh->fib_nh_weight, AF_INET6) < 0) 5568beb1afacSDavid Ahern goto nla_put_failure; 5569beb1afacSDavid Ahern 5570beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 557193c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 55721cf844c7SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, 55737bdf4de1SDonald Sharp sibling->fib6_nh->fib_nh_weight, 55747bdf4de1SDonald Sharp AF_INET6) < 0) 557594f826b8SEric Dumazet goto nla_put_failure; 557694f826b8SEric Dumazet } 55772d7202bfSThomas Graf 5578beb1afacSDavid Ahern nla_nest_end(skb, mp); 5579f88d8ea6SDavid Ahern } else if (rt->nh) { 5580f88d8ea6SDavid Ahern if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) 5581f88d8ea6SDavid Ahern goto nla_put_failure; 5582ecc5663cSDavid Ahern 5583f88d8ea6SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 5584f88d8ea6SDavid Ahern rtm->rtm_type = RTN_BLACKHOLE; 5585f88d8ea6SDavid Ahern 55864f80116dSRoopa Prabhu if (net->ipv4.sysctl_nexthop_compat_mode && 55874f80116dSRoopa Prabhu rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) 5588f88d8ea6SDavid Ahern goto nla_put_failure; 5589f88d8ea6SDavid Ahern 5590f88d8ea6SDavid Ahern rtm->rtm_flags |= nh_flags; 5591f88d8ea6SDavid Ahern } else { 55927bdf4de1SDonald Sharp if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, 5593ecc5663cSDavid Ahern &nh_flags, false) < 0) 5594c78679e8SDavid S. Miller goto nla_put_failure; 5595ecc5663cSDavid Ahern 5596ecc5663cSDavid Ahern rtm->rtm_flags |= nh_flags; 5597beb1afacSDavid Ahern } 55988253947eSLi Wei 559922d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 560014895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 560114895687SDavid Ahern expires -= jiffies; 560214895687SDavid Ahern } 560369cdf8f9SYOSHIFUJI Hideaki 5604bb3c4ab9SIdo Schimmel if (!dst) { 5605bb3c4ab9SIdo Schimmel if (rt->offload) 5606bb3c4ab9SIdo Schimmel rtm->rtm_flags |= RTM_F_OFFLOAD; 5607bb3c4ab9SIdo Schimmel if (rt->trap) 5608bb3c4ab9SIdo Schimmel rtm->rtm_flags |= RTM_F_TRAP; 5609bb3c4ab9SIdo Schimmel } 5610bb3c4ab9SIdo Schimmel 5611d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 5612e3703b3dSThomas Graf goto nla_put_failure; 56131da177e4SLinus Torvalds 561422d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 5615c78ba6d6SLubomir Rintel goto nla_put_failure; 5616c78ba6d6SLubomir Rintel 561719e42e45SRoopa Prabhu 5618053c095aSJohannes Berg nlmsg_end(skb, nlh); 5619053c095aSJohannes Berg return 0; 56202d7202bfSThomas Graf 56212d7202bfSThomas Graf nla_put_failure: 562226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 562326932566SPatrick McHardy return -EMSGSIZE; 56241da177e4SLinus Torvalds } 56251da177e4SLinus Torvalds 56262c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) 56272c170e07SDavid Ahern { 56282c170e07SDavid Ahern const struct net_device *dev = arg; 56292c170e07SDavid Ahern 56302c170e07SDavid Ahern if (nh->fib_nh_dev == dev) 56312c170e07SDavid Ahern return 1; 56322c170e07SDavid Ahern 56332c170e07SDavid Ahern return 0; 56342c170e07SDavid Ahern } 56352c170e07SDavid Ahern 563613e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 563713e38901SDavid Ahern const struct net_device *dev) 563813e38901SDavid Ahern { 56392c170e07SDavid Ahern if (f6i->nh) { 56402c170e07SDavid Ahern struct net_device *_dev = (struct net_device *)dev; 56412c170e07SDavid Ahern 56422c170e07SDavid Ahern return !!nexthop_for_each_fib6_nh(f6i->nh, 56432c170e07SDavid Ahern fib6_info_nh_uses_dev, 56442c170e07SDavid Ahern _dev); 56452c170e07SDavid Ahern } 56462c170e07SDavid Ahern 56471cf844c7SDavid Ahern if (f6i->fib6_nh->fib_nh_dev == dev) 564813e38901SDavid Ahern return true; 564913e38901SDavid Ahern 565013e38901SDavid Ahern if (f6i->fib6_nsiblings) { 565113e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 565213e38901SDavid Ahern 565313e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 565413e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 56551cf844c7SDavid Ahern if (sibling->fib6_nh->fib_nh_dev == dev) 565613e38901SDavid Ahern return true; 565713e38901SDavid Ahern } 565813e38901SDavid Ahern } 565913e38901SDavid Ahern 566013e38901SDavid Ahern return false; 566113e38901SDavid Ahern } 566213e38901SDavid Ahern 56631e47b483SStefano Brivio struct fib6_nh_exception_dump_walker { 56641e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump; 56651e47b483SStefano Brivio struct fib6_info *rt; 56661e47b483SStefano Brivio unsigned int flags; 56671e47b483SStefano Brivio unsigned int skip; 56681e47b483SStefano Brivio unsigned int count; 56691e47b483SStefano Brivio }; 56701e47b483SStefano Brivio 56711e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) 56721e47b483SStefano Brivio { 56731e47b483SStefano Brivio struct fib6_nh_exception_dump_walker *w = arg; 56741e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump = w->dump; 56751e47b483SStefano Brivio struct rt6_exception_bucket *bucket; 56761e47b483SStefano Brivio struct rt6_exception *rt6_ex; 56771e47b483SStefano Brivio int i, err; 56781e47b483SStefano Brivio 56791e47b483SStefano Brivio bucket = fib6_nh_get_excptn_bucket(nh, NULL); 56801e47b483SStefano Brivio if (!bucket) 56811e47b483SStefano Brivio return 0; 56821e47b483SStefano Brivio 56831e47b483SStefano Brivio for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 56841e47b483SStefano Brivio hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 56851e47b483SStefano Brivio if (w->skip) { 56861e47b483SStefano Brivio w->skip--; 56871e47b483SStefano Brivio continue; 56881e47b483SStefano Brivio } 56891e47b483SStefano Brivio 56901e47b483SStefano Brivio /* Expiration of entries doesn't bump sernum, insertion 56911e47b483SStefano Brivio * does. Removal is triggered by insertion, so we can 56921e47b483SStefano Brivio * rely on the fact that if entries change between two 56931e47b483SStefano Brivio * partial dumps, this node is scanned again completely, 56941e47b483SStefano Brivio * see rt6_insert_exception() and fib6_dump_table(). 56951e47b483SStefano Brivio * 56961e47b483SStefano Brivio * Count expired entries we go through as handled 56971e47b483SStefano Brivio * entries that we'll skip next time, in case of partial 56981e47b483SStefano Brivio * node dump. Otherwise, if entries expire meanwhile, 56991e47b483SStefano Brivio * we'll skip the wrong amount. 57001e47b483SStefano Brivio */ 57011e47b483SStefano Brivio if (rt6_check_expired(rt6_ex->rt6i)) { 57021e47b483SStefano Brivio w->count++; 57031e47b483SStefano Brivio continue; 57041e47b483SStefano Brivio } 57051e47b483SStefano Brivio 57061e47b483SStefano Brivio err = rt6_fill_node(dump->net, dump->skb, w->rt, 57071e47b483SStefano Brivio &rt6_ex->rt6i->dst, NULL, NULL, 0, 57081e47b483SStefano Brivio RTM_NEWROUTE, 57091e47b483SStefano Brivio NETLINK_CB(dump->cb->skb).portid, 57101e47b483SStefano Brivio dump->cb->nlh->nlmsg_seq, w->flags); 57111e47b483SStefano Brivio if (err) 57121e47b483SStefano Brivio return err; 57131e47b483SStefano Brivio 57141e47b483SStefano Brivio w->count++; 57151e47b483SStefano Brivio } 57161e47b483SStefano Brivio bucket++; 57171e47b483SStefano Brivio } 57181e47b483SStefano Brivio 57191e47b483SStefano Brivio return 0; 57201e47b483SStefano Brivio } 57211e47b483SStefano Brivio 5722bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */ 57231e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) 57241da177e4SLinus Torvalds { 57251da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 572613e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 572713e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 57281f17e2f2SDavid Ahern struct net *net = arg->net; 57291e47b483SStefano Brivio int count = 0; 57301f17e2f2SDavid Ahern 5731421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 5732bf9a8a06SStefano Brivio return -1; 57331da177e4SLinus Torvalds 573413e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 573593c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 5736f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 5737bf9a8a06SStefano Brivio return -1; 5738f8cfe2ceSDavid Ahern } 57391e47b483SStefano Brivio if (filter->filter_set && 57401e47b483SStefano Brivio ((filter->rt_type && rt->fib6_type != filter->rt_type) || 574113e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 57421e47b483SStefano Brivio (filter->protocol && rt->fib6_protocol != filter->protocol))) { 5743bf9a8a06SStefano Brivio return -1; 574413e38901SDavid Ahern } 57451e47b483SStefano Brivio 57461e47b483SStefano Brivio if (filter->filter_set || 57471e47b483SStefano Brivio !filter->dump_routes || !filter->dump_exceptions) { 574813e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 5749f8cfe2ceSDavid Ahern } 57501da177e4SLinus Torvalds 57511e47b483SStefano Brivio if (filter->dump_routes) { 57521e47b483SStefano Brivio if (skip) { 57531e47b483SStefano Brivio skip--; 57541e47b483SStefano Brivio } else { 57551e47b483SStefano Brivio if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 57561e47b483SStefano Brivio 0, RTM_NEWROUTE, 5757bf9a8a06SStefano Brivio NETLINK_CB(arg->cb->skb).portid, 57581e47b483SStefano Brivio arg->cb->nlh->nlmsg_seq, flags)) { 5759bf9a8a06SStefano Brivio return 0; 57601e47b483SStefano Brivio } 57611e47b483SStefano Brivio count++; 57621e47b483SStefano Brivio } 57631e47b483SStefano Brivio } 57641e47b483SStefano Brivio 57651e47b483SStefano Brivio if (filter->dump_exceptions) { 57661e47b483SStefano Brivio struct fib6_nh_exception_dump_walker w = { .dump = arg, 57671e47b483SStefano Brivio .rt = rt, 57681e47b483SStefano Brivio .flags = flags, 57691e47b483SStefano Brivio .skip = skip, 57701e47b483SStefano Brivio .count = 0 }; 57711e47b483SStefano Brivio int err; 57721e47b483SStefano Brivio 57733b525691SEric Dumazet rcu_read_lock(); 57741e47b483SStefano Brivio if (rt->nh) { 57751e47b483SStefano Brivio err = nexthop_for_each_fib6_nh(rt->nh, 57761e47b483SStefano Brivio rt6_nh_dump_exceptions, 57771e47b483SStefano Brivio &w); 57781e47b483SStefano Brivio } else { 57791e47b483SStefano Brivio err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); 57801e47b483SStefano Brivio } 57813b525691SEric Dumazet rcu_read_unlock(); 57821e47b483SStefano Brivio 57831e47b483SStefano Brivio if (err) 57841e47b483SStefano Brivio return count += w.count; 57851e47b483SStefano Brivio } 5786bf9a8a06SStefano Brivio 5787bf9a8a06SStefano Brivio return -1; 57881da177e4SLinus Torvalds } 57891da177e4SLinus Torvalds 57900eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 57910eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 57920eff0a27SJakub Kicinski struct nlattr **tb, 57930eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 57940eff0a27SJakub Kicinski { 57950eff0a27SJakub Kicinski struct rtmsg *rtm; 57960eff0a27SJakub Kicinski int i, err; 57970eff0a27SJakub Kicinski 57980eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 57990eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 58000eff0a27SJakub Kicinski "Invalid header for get route request"); 58010eff0a27SJakub Kicinski return -EINVAL; 58020eff0a27SJakub Kicinski } 58030eff0a27SJakub Kicinski 58040eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 58058cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 58060eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 58070eff0a27SJakub Kicinski 58080eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 58090eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 58100eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 58110eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 58120eff0a27SJakub Kicinski rtm->rtm_type) { 58130eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 58140eff0a27SJakub Kicinski return -EINVAL; 58150eff0a27SJakub Kicinski } 58160eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 58170eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 58180eff0a27SJakub Kicinski "Invalid flags for get route request"); 58190eff0a27SJakub Kicinski return -EINVAL; 58200eff0a27SJakub Kicinski } 58210eff0a27SJakub Kicinski 58228cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 58230eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 58240eff0a27SJakub Kicinski if (err) 58250eff0a27SJakub Kicinski return err; 58260eff0a27SJakub Kicinski 58270eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 58280eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 58290eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 58300eff0a27SJakub Kicinski return -EINVAL; 58310eff0a27SJakub Kicinski } 58320eff0a27SJakub Kicinski 58330eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 58340eff0a27SJakub Kicinski if (!tb[i]) 58350eff0a27SJakub Kicinski continue; 58360eff0a27SJakub Kicinski 58370eff0a27SJakub Kicinski switch (i) { 58380eff0a27SJakub Kicinski case RTA_SRC: 58390eff0a27SJakub Kicinski case RTA_DST: 58400eff0a27SJakub Kicinski case RTA_IIF: 58410eff0a27SJakub Kicinski case RTA_OIF: 58420eff0a27SJakub Kicinski case RTA_MARK: 58430eff0a27SJakub Kicinski case RTA_UID: 58440eff0a27SJakub Kicinski case RTA_SPORT: 58450eff0a27SJakub Kicinski case RTA_DPORT: 58460eff0a27SJakub Kicinski case RTA_IP_PROTO: 58470eff0a27SJakub Kicinski break; 58480eff0a27SJakub Kicinski default: 58490eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 58500eff0a27SJakub Kicinski return -EINVAL; 58510eff0a27SJakub Kicinski } 58520eff0a27SJakub Kicinski } 58530eff0a27SJakub Kicinski 58540eff0a27SJakub Kicinski return 0; 58550eff0a27SJakub Kicinski } 58560eff0a27SJakub Kicinski 5857c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5858c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 58591da177e4SLinus Torvalds { 58603b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 5861ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 586218c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 5863a68886a6SDavid Ahern struct fib6_info *from; 586418c3a61cSRoopa Prabhu struct dst_entry *dst; 58651da177e4SLinus Torvalds struct rt6_info *rt; 5866ab364a6fSThomas Graf struct sk_buff *skb; 5867ab364a6fSThomas Graf struct rtmsg *rtm; 5868744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 586918c3a61cSRoopa Prabhu bool fibmatch; 5870ab364a6fSThomas Graf 58710eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 5872ab364a6fSThomas Graf if (err < 0) 5873ab364a6fSThomas Graf goto errout; 5874ab364a6fSThomas Graf 5875ab364a6fSThomas Graf err = -EINVAL; 587638b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 587738b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 587818c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 5879ab364a6fSThomas Graf 5880ab364a6fSThomas Graf if (tb[RTA_SRC]) { 5881ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 5882ab364a6fSThomas Graf goto errout; 5883ab364a6fSThomas Graf 58844e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 5885ab364a6fSThomas Graf } 5886ab364a6fSThomas Graf 5887ab364a6fSThomas Graf if (tb[RTA_DST]) { 5888ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 5889ab364a6fSThomas Graf goto errout; 5890ab364a6fSThomas Graf 58914e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 5892ab364a6fSThomas Graf } 5893ab364a6fSThomas Graf 5894ab364a6fSThomas Graf if (tb[RTA_IIF]) 5895ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 5896ab364a6fSThomas Graf 5897ab364a6fSThomas Graf if (tb[RTA_OIF]) 589872331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 5899ab364a6fSThomas Graf 59002e47b291SLorenzo Colitti if (tb[RTA_MARK]) 59012e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 59022e47b291SLorenzo Colitti 5903622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 5904622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 5905622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 5906622ec2c9SLorenzo Colitti else 5907622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 5908622ec2c9SLorenzo Colitti 5909eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 5910eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 5911eacb9384SRoopa Prabhu 5912eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 5913eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 5914eacb9384SRoopa Prabhu 5915eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 5916eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 59175e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 59185e1a99eaSHangbin Liu extack); 5919eacb9384SRoopa Prabhu if (err) 5920eacb9384SRoopa Prabhu goto errout; 5921eacb9384SRoopa Prabhu } 5922eacb9384SRoopa Prabhu 5923ab364a6fSThomas Graf if (iif) { 5924ab364a6fSThomas Graf struct net_device *dev; 592572331bc0SShmulik Ladkani int flags = 0; 592672331bc0SShmulik Ladkani 5927121622dbSFlorian Westphal rcu_read_lock(); 5928121622dbSFlorian Westphal 5929121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 5930ab364a6fSThomas Graf if (!dev) { 5931121622dbSFlorian Westphal rcu_read_unlock(); 5932ab364a6fSThomas Graf err = -ENODEV; 5933ab364a6fSThomas Graf goto errout; 5934ab364a6fSThomas Graf } 593572331bc0SShmulik Ladkani 593672331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 593772331bc0SShmulik Ladkani 593872331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 593972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 594072331bc0SShmulik Ladkani 5941b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 5942121622dbSFlorian Westphal 5943121622dbSFlorian Westphal rcu_read_unlock(); 594472331bc0SShmulik Ladkani } else { 594572331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 594672331bc0SShmulik Ladkani 594718c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 594818c3a61cSRoopa Prabhu } 594918c3a61cSRoopa Prabhu 595018c3a61cSRoopa Prabhu 595118c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 595218c3a61cSRoopa Prabhu if (rt->dst.error) { 595318c3a61cSRoopa Prabhu err = rt->dst.error; 595418c3a61cSRoopa Prabhu ip6_rt_put(rt); 595518c3a61cSRoopa Prabhu goto errout; 5956ab364a6fSThomas Graf } 59571da177e4SLinus Torvalds 59589d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 59599d6acb3bSWANG Cong err = rt->dst.error; 59609d6acb3bSWANG Cong ip6_rt_put(rt); 59619d6acb3bSWANG Cong goto errout; 59629d6acb3bSWANG Cong } 59639d6acb3bSWANG Cong 59641da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 596538308473SDavid S. Miller if (!skb) { 596694e187c0SAmerigo Wang ip6_rt_put(rt); 5967ab364a6fSThomas Graf err = -ENOBUFS; 5968ab364a6fSThomas Graf goto errout; 5969ab364a6fSThomas Graf } 59701da177e4SLinus Torvalds 5971d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 5972a68886a6SDavid Ahern 5973a68886a6SDavid Ahern rcu_read_lock(); 5974a68886a6SDavid Ahern from = rcu_dereference(rt->from); 5975886b7a50SMartin KaFai Lau if (from) { 597618c3a61cSRoopa Prabhu if (fibmatch) 5977886b7a50SMartin KaFai Lau err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 5978886b7a50SMartin KaFai Lau iif, RTM_NEWROUTE, 5979886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 598018c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 598118c3a61cSRoopa Prabhu else 5982a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 5983a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 5984886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 5985886b7a50SMartin KaFai Lau nlh->nlmsg_seq, 0); 5986886b7a50SMartin KaFai Lau } else { 5987886b7a50SMartin KaFai Lau err = -ENETUNREACH; 5988886b7a50SMartin KaFai Lau } 5989a68886a6SDavid Ahern rcu_read_unlock(); 5990a68886a6SDavid Ahern 59911da177e4SLinus Torvalds if (err < 0) { 5992ab364a6fSThomas Graf kfree_skb(skb); 5993ab364a6fSThomas Graf goto errout; 59941da177e4SLinus Torvalds } 59951da177e4SLinus Torvalds 599615e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 5997ab364a6fSThomas Graf errout: 59981da177e4SLinus Torvalds return err; 59991da177e4SLinus Torvalds } 60001da177e4SLinus Torvalds 60018d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 600237a1d361SRoopa Prabhu unsigned int nlm_flags) 60031da177e4SLinus Torvalds { 60041da177e4SLinus Torvalds struct sk_buff *skb; 60055578689aSDaniel Lezcano struct net *net = info->nl_net; 6006528c4cebSDenis V. Lunev u32 seq; 6007528c4cebSDenis V. Lunev int err; 60080d51aa80SJamal Hadi Salim 6009528c4cebSDenis V. Lunev err = -ENOBUFS; 601038308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 601186872cb5SThomas Graf 601219e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 601338308473SDavid S. Miller if (!skb) 601421713ebcSThomas Graf goto errout; 60151da177e4SLinus Torvalds 6016d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 6017f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 601826932566SPatrick McHardy if (err < 0) { 601926932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 602026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 602126932566SPatrick McHardy kfree_skb(skb); 602226932566SPatrick McHardy goto errout; 602326932566SPatrick McHardy } 602415e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 60255578689aSDaniel Lezcano info->nlh, gfp_any()); 60261ce85fe4SPablo Neira Ayuso return; 602721713ebcSThomas Graf errout: 602821713ebcSThomas Graf if (err < 0) 60295578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 60301da177e4SLinus Torvalds } 60311da177e4SLinus Torvalds 603219a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt, 603319a3b7eeSDavid Ahern struct nl_info *info) 603419a3b7eeSDavid Ahern { 603519a3b7eeSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 603619a3b7eeSDavid Ahern struct sk_buff *skb; 603719a3b7eeSDavid Ahern int err = -ENOBUFS; 603819a3b7eeSDavid Ahern 603919a3b7eeSDavid Ahern /* call_fib6_entry_notifiers will be removed when in-kernel notifier 604019a3b7eeSDavid Ahern * is implemented and supported for nexthop objects 604119a3b7eeSDavid Ahern */ 604219a3b7eeSDavid Ahern call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); 604319a3b7eeSDavid Ahern 604419a3b7eeSDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 604519a3b7eeSDavid Ahern if (!skb) 604619a3b7eeSDavid Ahern goto errout; 604719a3b7eeSDavid Ahern 604819a3b7eeSDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 604919a3b7eeSDavid Ahern RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 605019a3b7eeSDavid Ahern if (err < 0) { 605119a3b7eeSDavid Ahern /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 605219a3b7eeSDavid Ahern WARN_ON(err == -EMSGSIZE); 605319a3b7eeSDavid Ahern kfree_skb(skb); 605419a3b7eeSDavid Ahern goto errout; 605519a3b7eeSDavid Ahern } 605619a3b7eeSDavid Ahern rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 605719a3b7eeSDavid Ahern info->nlh, gfp_any()); 605819a3b7eeSDavid Ahern return; 605919a3b7eeSDavid Ahern errout: 606019a3b7eeSDavid Ahern if (err < 0) 606119a3b7eeSDavid Ahern rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 606219a3b7eeSDavid Ahern } 606319a3b7eeSDavid Ahern 60648ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 6065351638e7SJiri Pirko unsigned long event, void *ptr) 60668ed67789SDaniel Lezcano { 6067351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6068c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 60698ed67789SDaniel Lezcano 6070242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 6071242d3a49SWANG Cong return NOTIFY_OK; 6072242d3a49SWANG Cong 6073242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 60741cf844c7SDavid Ahern net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; 6075d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 60768ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 60778ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6078d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 60798ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 6080d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 60818ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 60828ed67789SDaniel Lezcano #endif 608376da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 608476da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 608576da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 608676da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 608776da0704SWANG Cong */ 608812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 6089242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 609012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 609112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 6092242d3a49SWANG Cong #endif 60938ed67789SDaniel Lezcano } 60948ed67789SDaniel Lezcano 60958ed67789SDaniel Lezcano return NOTIFY_OK; 60968ed67789SDaniel Lezcano } 60978ed67789SDaniel Lezcano 60981da177e4SLinus Torvalds /* 60991da177e4SLinus Torvalds * /proc 61001da177e4SLinus Torvalds */ 61011da177e4SLinus Torvalds 61021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 61031da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 61041da177e4SLinus Torvalds { 610569ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 61061da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 610769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 610869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 610981eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 611069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 611169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 6112fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 611369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 61141da177e4SLinus Torvalds 61151da177e4SLinus Torvalds return 0; 61161da177e4SLinus Torvalds } 61171da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 61181da177e4SLinus Torvalds 61191da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 61201da177e4SLinus Torvalds 612132927393SChristoph Hellwig static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 612232927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 61231da177e4SLinus Torvalds { 6124c486da34SLucian Adrian Grijincu struct net *net; 6125c486da34SLucian Adrian Grijincu int delay; 6126f0fb9b28SAditya Pakki int ret; 6127c486da34SLucian Adrian Grijincu if (!write) 6128c486da34SLucian Adrian Grijincu return -EINVAL; 6129c486da34SLucian Adrian Grijincu 6130c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 6131c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 6132f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6133f0fb9b28SAditya Pakki if (ret) 6134f0fb9b28SAditya Pakki return ret; 6135f0fb9b28SAditya Pakki 61362ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 61371da177e4SLinus Torvalds return 0; 61381da177e4SLinus Torvalds } 61391da177e4SLinus Torvalds 6140ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 61411da177e4SLinus Torvalds { 61421da177e4SLinus Torvalds .procname = "flush", 61434990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 61441da177e4SLinus Torvalds .maxlen = sizeof(int), 614589c8b3a1SDave Jones .mode = 0200, 61466d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 61471da177e4SLinus Torvalds }, 61481da177e4SLinus Torvalds { 61491da177e4SLinus Torvalds .procname = "gc_thresh", 61509a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 61511da177e4SLinus Torvalds .maxlen = sizeof(int), 61521da177e4SLinus Torvalds .mode = 0644, 61536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61541da177e4SLinus Torvalds }, 61551da177e4SLinus Torvalds { 61561da177e4SLinus Torvalds .procname = "max_size", 61574990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 61581da177e4SLinus Torvalds .maxlen = sizeof(int), 61591da177e4SLinus Torvalds .mode = 0644, 61606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61611da177e4SLinus Torvalds }, 61621da177e4SLinus Torvalds { 61631da177e4SLinus Torvalds .procname = "gc_min_interval", 61644990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 61651da177e4SLinus Torvalds .maxlen = sizeof(int), 61661da177e4SLinus Torvalds .mode = 0644, 61676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61681da177e4SLinus Torvalds }, 61691da177e4SLinus Torvalds { 61701da177e4SLinus Torvalds .procname = "gc_timeout", 61714990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 61721da177e4SLinus Torvalds .maxlen = sizeof(int), 61731da177e4SLinus Torvalds .mode = 0644, 61746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61751da177e4SLinus Torvalds }, 61761da177e4SLinus Torvalds { 61771da177e4SLinus Torvalds .procname = "gc_interval", 61784990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 61791da177e4SLinus Torvalds .maxlen = sizeof(int), 61801da177e4SLinus Torvalds .mode = 0644, 61816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61821da177e4SLinus Torvalds }, 61831da177e4SLinus Torvalds { 61841da177e4SLinus Torvalds .procname = "gc_elasticity", 61854990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 61861da177e4SLinus Torvalds .maxlen = sizeof(int), 61871da177e4SLinus Torvalds .mode = 0644, 6188f3d3f616SMin Zhang .proc_handler = proc_dointvec, 61891da177e4SLinus Torvalds }, 61901da177e4SLinus Torvalds { 61911da177e4SLinus Torvalds .procname = "mtu_expires", 61924990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 61931da177e4SLinus Torvalds .maxlen = sizeof(int), 61941da177e4SLinus Torvalds .mode = 0644, 61956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61961da177e4SLinus Torvalds }, 61971da177e4SLinus Torvalds { 61981da177e4SLinus Torvalds .procname = "min_adv_mss", 61994990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 62001da177e4SLinus Torvalds .maxlen = sizeof(int), 62011da177e4SLinus Torvalds .mode = 0644, 6202f3d3f616SMin Zhang .proc_handler = proc_dointvec, 62031da177e4SLinus Torvalds }, 62041da177e4SLinus Torvalds { 62051da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 62064990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 62071da177e4SLinus Torvalds .maxlen = sizeof(int), 62081da177e4SLinus Torvalds .mode = 0644, 62096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 62101da177e4SLinus Torvalds }, 62117c6bb7d2SDavid Ahern { 62127c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 62137c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 62147c6bb7d2SDavid Ahern .maxlen = sizeof(int), 62157c6bb7d2SDavid Ahern .mode = 0644, 6216b8e8a863SEiichi Tsukata .proc_handler = proc_dointvec_minmax, 6217eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO, 6218eec4844fSMatteo Croce .extra2 = SYSCTL_ONE, 62197c6bb7d2SDavid Ahern }, 6220f8572d8fSEric W. Biederman { } 62211da177e4SLinus Torvalds }; 62221da177e4SLinus Torvalds 62232c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 6224760f2d01SDaniel Lezcano { 6225760f2d01SDaniel Lezcano struct ctl_table *table; 6226760f2d01SDaniel Lezcano 6227760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 6228760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 6229760f2d01SDaniel Lezcano GFP_KERNEL); 62305ee09105SYOSHIFUJI Hideaki 62315ee09105SYOSHIFUJI Hideaki if (table) { 62325ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 6233c486da34SLucian Adrian Grijincu table[0].extra1 = net; 623486393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 62355ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 62365ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 62375ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 62385ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 62395ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 62405ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 62415ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 62429c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 62437c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 6244464dc801SEric W. Biederman 6245464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 6246464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 6247464dc801SEric W. Biederman table[0].procname = NULL; 62485ee09105SYOSHIFUJI Hideaki } 62495ee09105SYOSHIFUJI Hideaki 6250760f2d01SDaniel Lezcano return table; 6251760f2d01SDaniel Lezcano } 62521da177e4SLinus Torvalds #endif 62531da177e4SLinus Torvalds 62542c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 6255cdb18761SDaniel Lezcano { 6256633d424bSPavel Emelyanov int ret = -ENOMEM; 62578ed67789SDaniel Lezcano 625886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 625986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 6260f2fc6a54SBenjamin Thery 6261fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 6262fc66f95cSEric Dumazet goto out_ip6_dst_ops; 6263fc66f95cSEric Dumazet 62641cf844c7SDavid Ahern net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); 6265421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 6266421842edSDavid Ahern goto out_ip6_dst_entries; 62671cf844c7SDavid Ahern memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, 62681cf844c7SDavid Ahern sizeof(*net->ipv6.fib6_null_entry)); 6269421842edSDavid Ahern 62708ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 62718ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 62728ed67789SDaniel Lezcano GFP_KERNEL); 62738ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 6274421842edSDavid Ahern goto out_fib6_null_entry; 6275d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 627662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 627762fa8a84SDavid S. Miller ip6_template_metrics, true); 627874109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached); 62798ed67789SDaniel Lezcano 62808ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6281feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 62828ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 62838ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 62848ed67789SDaniel Lezcano GFP_KERNEL); 628568fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 628668fffc67SPeter Zijlstra goto out_ip6_null_entry; 6287d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 628862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 628962fa8a84SDavid S. Miller ip6_template_metrics, true); 629074109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached); 62918ed67789SDaniel Lezcano 62928ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 62938ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 62948ed67789SDaniel Lezcano GFP_KERNEL); 629568fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 629668fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 6297d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 629862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 629962fa8a84SDavid S. Miller ip6_template_metrics, true); 630074109218SWei Wang INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached); 6301b9b33e7cSPaolo Abeni #ifdef CONFIG_IPV6_SUBTREES 6302b9b33e7cSPaolo Abeni net->ipv6.fib6_routes_require_src = 0; 6303b9b33e7cSPaolo Abeni #endif 63048ed67789SDaniel Lezcano #endif 63058ed67789SDaniel Lezcano 6306b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 6307b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 6308b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 6309b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 6310b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 6311b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 6312b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 6313b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 63147c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 6315b339a47cSPeter Zijlstra 63166891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 63176891a346SBenjamin Thery 63188ed67789SDaniel Lezcano ret = 0; 63198ed67789SDaniel Lezcano out: 63208ed67789SDaniel Lezcano return ret; 6321f2fc6a54SBenjamin Thery 632268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 632368fffc67SPeter Zijlstra out_ip6_prohibit_entry: 632468fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 632568fffc67SPeter Zijlstra out_ip6_null_entry: 632668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 632768fffc67SPeter Zijlstra #endif 6328421842edSDavid Ahern out_fib6_null_entry: 6329421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 6330fc66f95cSEric Dumazet out_ip6_dst_entries: 6331fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6332f2fc6a54SBenjamin Thery out_ip6_dst_ops: 6333f2fc6a54SBenjamin Thery goto out; 6334cdb18761SDaniel Lezcano } 6335cdb18761SDaniel Lezcano 63362c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 6337cdb18761SDaniel Lezcano { 6338421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 63398ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 63408ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 63418ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 63428ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 63438ed67789SDaniel Lezcano #endif 634441bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6345cdb18761SDaniel Lezcano } 6346cdb18761SDaniel Lezcano 6347d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 6348d189634eSThomas Graf { 6349d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6350c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 6351c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 63523617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 63533617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 6354d189634eSThomas Graf #endif 6355d189634eSThomas Graf return 0; 6356d189634eSThomas Graf } 6357d189634eSThomas Graf 6358d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 6359d189634eSThomas Graf { 6360d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6361ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 6362ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 6363d189634eSThomas Graf #endif 6364d189634eSThomas Graf } 6365d189634eSThomas Graf 6366cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 6367cdb18761SDaniel Lezcano .init = ip6_route_net_init, 6368cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 6369cdb18761SDaniel Lezcano }; 6370cdb18761SDaniel Lezcano 6371c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 6372c3426b47SDavid S. Miller { 6373c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 6374c3426b47SDavid S. Miller 6375c3426b47SDavid S. Miller if (!bp) 6376c3426b47SDavid S. Miller return -ENOMEM; 6377c3426b47SDavid S. Miller inet_peer_base_init(bp); 6378c3426b47SDavid S. Miller net->ipv6.peers = bp; 6379c3426b47SDavid S. Miller return 0; 6380c3426b47SDavid S. Miller } 6381c3426b47SDavid S. Miller 6382c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 6383c3426b47SDavid S. Miller { 6384c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 6385c3426b47SDavid S. Miller 6386c3426b47SDavid S. Miller net->ipv6.peers = NULL; 638756a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 6388c3426b47SDavid S. Miller kfree(bp); 6389c3426b47SDavid S. Miller } 6390c3426b47SDavid S. Miller 63912b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 6392c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 6393c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 6394c3426b47SDavid S. Miller }; 6395c3426b47SDavid S. Miller 6396d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 6397d189634eSThomas Graf .init = ip6_route_net_init_late, 6398d189634eSThomas Graf .exit = ip6_route_net_exit_late, 6399d189634eSThomas Graf }; 6400d189634eSThomas Graf 64018ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 64028ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 6403242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 64048ed67789SDaniel Lezcano }; 64058ed67789SDaniel Lezcano 64062f460933SWANG Cong void __init ip6_route_init_special_entries(void) 64072f460933SWANG Cong { 64082f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 64092f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 64102f460933SWANG Cong * manually for init_net */ 64111cf844c7SDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; 64122f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 64132f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 64142f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 64152f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 64162f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 64172f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 64182f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 64192f460933SWANG Cong #endif 64202f460933SWANG Cong } 64212f460933SWANG Cong 6422138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6423138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6424138d0be3SYonghong Song DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt) 6425138d0be3SYonghong Song 642615172a46SYonghong Song static const struct bpf_iter_reg ipv6_route_reg_info = { 6427138d0be3SYonghong Song .target = "ipv6_route", 6428138d0be3SYonghong Song .seq_ops = &ipv6_route_seq_ops, 6429138d0be3SYonghong Song .init_seq_private = bpf_iter_init_seq_net, 6430138d0be3SYonghong Song .fini_seq_private = bpf_iter_fini_seq_net, 6431138d0be3SYonghong Song .seq_priv_size = sizeof(struct ipv6_route_iter), 64323c32cc1bSYonghong Song .ctx_arg_info_size = 1, 64333c32cc1bSYonghong Song .ctx_arg_info = { 64343c32cc1bSYonghong Song { offsetof(struct bpf_iter__ipv6_route, rt), 64353c32cc1bSYonghong Song PTR_TO_BTF_ID_OR_NULL }, 64363c32cc1bSYonghong Song }, 6437138d0be3SYonghong Song }; 6438138d0be3SYonghong Song 643915172a46SYonghong Song static int __init bpf_iter_register(void) 644015172a46SYonghong Song { 644115172a46SYonghong Song return bpf_iter_reg_target(&ipv6_route_reg_info); 6442138d0be3SYonghong Song } 6443138d0be3SYonghong Song 6444138d0be3SYonghong Song static void bpf_iter_unregister(void) 6445138d0be3SYonghong Song { 6446ab2ee4fcSYonghong Song bpf_iter_unreg_target(&ipv6_route_reg_info); 6447138d0be3SYonghong Song } 6448138d0be3SYonghong Song #endif 6449138d0be3SYonghong Song #endif 6450138d0be3SYonghong Song 6451433d49c3SDaniel Lezcano int __init ip6_route_init(void) 64521da177e4SLinus Torvalds { 6453433d49c3SDaniel Lezcano int ret; 64548d0b94afSMartin KaFai Lau int cpu; 6455433d49c3SDaniel Lezcano 64569a7ec3a9SDaniel Lezcano ret = -ENOMEM; 64579a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 64589a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 64599a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 64609a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 6461c19a28e1SFernando Carrijo goto out; 646214e50e57SDavid S. Miller 6463fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 64648ed67789SDaniel Lezcano if (ret) 6465bdb3289fSDaniel Lezcano goto out_kmem_cache; 6466bdb3289fSDaniel Lezcano 6467c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 6468c3426b47SDavid S. Miller if (ret) 6469e8803b6cSDavid S. Miller goto out_dst_entries; 64702a0c451aSThomas Graf 64717e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 64727e52b33bSDavid S. Miller if (ret) 64737e52b33bSDavid S. Miller goto out_register_inetpeer; 6474c3426b47SDavid S. Miller 64755dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 64765dc121e9SArnaud Ebalard 6477e8803b6cSDavid S. Miller ret = fib6_init(); 6478433d49c3SDaniel Lezcano if (ret) 64798ed67789SDaniel Lezcano goto out_register_subsys; 6480433d49c3SDaniel Lezcano 6481433d49c3SDaniel Lezcano ret = xfrm6_init(); 6482433d49c3SDaniel Lezcano if (ret) 6483e8803b6cSDavid S. Miller goto out_fib6_init; 6484c35b7e72SDaniel Lezcano 6485433d49c3SDaniel Lezcano ret = fib6_rules_init(); 6486433d49c3SDaniel Lezcano if (ret) 6487433d49c3SDaniel Lezcano goto xfrm6_init; 64887e5449c2SDaniel Lezcano 6489d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 6490d189634eSThomas Graf if (ret) 6491d189634eSThomas Graf goto fib6_rules_init; 6492d189634eSThomas Graf 649316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 649416feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 649516feebcfSFlorian Westphal if (ret < 0) 649616feebcfSFlorian Westphal goto out_register_late_subsys; 649716feebcfSFlorian Westphal 649816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 649916feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 650016feebcfSFlorian Westphal if (ret < 0) 650116feebcfSFlorian Westphal goto out_register_late_subsys; 650216feebcfSFlorian Westphal 650316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 650416feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 650516feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 650616feebcfSFlorian Westphal if (ret < 0) 6507d189634eSThomas Graf goto out_register_late_subsys; 6508433d49c3SDaniel Lezcano 65098ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 6510cdb18761SDaniel Lezcano if (ret) 6511d189634eSThomas Graf goto out_register_late_subsys; 65128ed67789SDaniel Lezcano 6513138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6514138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6515138d0be3SYonghong Song ret = bpf_iter_register(); 6516138d0be3SYonghong Song if (ret) 6517138d0be3SYonghong Song goto out_register_late_subsys; 6518138d0be3SYonghong Song #endif 6519138d0be3SYonghong Song #endif 6520138d0be3SYonghong Song 65218d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 65228d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 65238d0b94afSMartin KaFai Lau 65248d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 65258d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 65268d0b94afSMartin KaFai Lau } 65278d0b94afSMartin KaFai Lau 6528433d49c3SDaniel Lezcano out: 6529433d49c3SDaniel Lezcano return ret; 6530433d49c3SDaniel Lezcano 6531d189634eSThomas Graf out_register_late_subsys: 653216feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6533d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6534433d49c3SDaniel Lezcano fib6_rules_init: 6535433d49c3SDaniel Lezcano fib6_rules_cleanup(); 6536433d49c3SDaniel Lezcano xfrm6_init: 6537433d49c3SDaniel Lezcano xfrm6_fini(); 65382a0c451aSThomas Graf out_fib6_init: 65392a0c451aSThomas Graf fib6_gc_cleanup(); 65408ed67789SDaniel Lezcano out_register_subsys: 65418ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 65427e52b33bSDavid S. Miller out_register_inetpeer: 65437e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 6544fc66f95cSEric Dumazet out_dst_entries: 6545fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 6546433d49c3SDaniel Lezcano out_kmem_cache: 6547f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6548433d49c3SDaniel Lezcano goto out; 65491da177e4SLinus Torvalds } 65501da177e4SLinus Torvalds 65511da177e4SLinus Torvalds void ip6_route_cleanup(void) 65521da177e4SLinus Torvalds { 6553138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6554138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6555138d0be3SYonghong Song bpf_iter_unregister(); 6556138d0be3SYonghong Song #endif 6557138d0be3SYonghong Song #endif 65588ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 6559d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6560101367c2SThomas Graf fib6_rules_cleanup(); 65611da177e4SLinus Torvalds xfrm6_fini(); 65621da177e4SLinus Torvalds fib6_gc_cleanup(); 6563c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 65648ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 656541bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 6566f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 65671da177e4SLinus Torvalds } 6568