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> 444785305cSEric Dumazet #include <linux/siphash.h> 45457c4cbcSEric W. Biederman #include <net/net_namespace.h> 461da177e4SLinus Torvalds #include <net/snmp.h> 471da177e4SLinus Torvalds #include <net/ipv6.h> 481da177e4SLinus Torvalds #include <net/ip6_fib.h> 491da177e4SLinus Torvalds #include <net/ip6_route.h> 501da177e4SLinus Torvalds #include <net/ndisc.h> 511da177e4SLinus Torvalds #include <net/addrconf.h> 521da177e4SLinus Torvalds #include <net/tcp.h> 531da177e4SLinus Torvalds #include <linux/rtnetlink.h> 541da177e4SLinus Torvalds #include <net/dst.h> 55904af04dSJiri Benc #include <net/dst_metadata.h> 561da177e4SLinus Torvalds #include <net/xfrm.h> 578d71740cSTom Tucker #include <net/netevent.h> 5821713ebcSThomas Graf #include <net/netlink.h> 593c618c1dSDavid Ahern #include <net/rtnh.h> 6019e42e45SRoopa Prabhu #include <net/lwtunnel.h> 61904af04dSJiri Benc #include <net/ip_tunnels.h> 62ca254490SDavid Ahern #include <net/l3mdev.h> 63eacb9384SRoopa Prabhu #include <net/ip.h> 647c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 65951cf368SYonghong Song #include <linux/btf_ids.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 681da177e4SLinus Torvalds #include <linux/sysctl.h> 691da177e4SLinus Torvalds #endif 701da177e4SLinus Torvalds 7130d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type); 7230d444d3SDavid Ahern 7330d444d3SDavid Ahern #define CREATE_TRACE_POINTS 7430d444d3SDavid Ahern #include <trace/events/fib6.h> 7530d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); 7630d444d3SDavid Ahern #undef CREATE_TRACE_POINTS 7730d444d3SDavid Ahern 78afc154e9SHannes Frederic Sowa enum rt6_nud_state { 797e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 807e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 817e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 82afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 83afc154e9SHannes Frederic Sowa }; 84afc154e9SHannes Frederic Sowa 85bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE 86bbd807dfSBrian Vazquez struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 870dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 88f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE 89f67fbeaeSBrian Vazquez unsigned int ip6_mtu(const struct dst_entry *dst); 901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 911da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 921da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 931da177e4SLinus Torvalds struct net_device *dev, int how); 94af6d1034SJon Maxwell static void ip6_dst_gc(struct dst_ops *ops); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 97ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 987150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 99ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 1001da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 1016700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 102bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 103bd085ef6SHangbin Liu bool confirm_neigh); 1046700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 1056700c270SDavid S. Miller struct sk_buff *skb); 106702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 107702cea56SDavid Ahern int strict); 108a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i); 109d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1108d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 111d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 11216a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 11316a16cd3SDavid Ahern unsigned int flags); 1147e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 115510e2cedSWei Wang const struct in6_addr *daddr, 116510e2cedSWei Wang const struct in6_addr *saddr); 1171da177e4SLinus Torvalds 11870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1198d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 120b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 121830218c1SDavid Ahern const struct in6_addr *gwaddr, 122830218c1SDavid Ahern struct net_device *dev, 12395c96174SEric Dumazet unsigned int pref); 1248d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 125b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 126830218c1SDavid Ahern const struct in6_addr *gwaddr, 127830218c1SDavid Ahern struct net_device *dev); 12870ceb4f5SYOSHIFUJI Hideaki #endif 12970ceb4f5SYOSHIFUJI Hideaki 1308d0b94afSMartin KaFai Lau struct uncached_list { 1318d0b94afSMartin KaFai Lau spinlock_t lock; 1328d0b94afSMartin KaFai Lau struct list_head head; 133ba55ef81SEric Dumazet struct list_head quarantine; 1348d0b94afSMartin KaFai Lau }; 1358d0b94afSMartin KaFai Lau 1368d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1378d0b94afSMartin KaFai Lau 138510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1398d0b94afSMartin KaFai Lau { 1408d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1418d0b94afSMartin KaFai Lau 142d288a162SWangyang Guo rt->dst.rt_uncached_list = ul; 1438d0b94afSMartin KaFai Lau 1448d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 145d288a162SWangyang Guo list_add_tail(&rt->dst.rt_uncached, &ul->head); 1468d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1478d0b94afSMartin KaFai Lau } 1488d0b94afSMartin KaFai Lau 149510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1508d0b94afSMartin KaFai Lau { 151d288a162SWangyang Guo if (!list_empty(&rt->dst.rt_uncached)) { 152d288a162SWangyang Guo struct uncached_list *ul = rt->dst.rt_uncached_list; 1538d0b94afSMartin KaFai Lau 1548d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 155d288a162SWangyang Guo list_del_init(&rt->dst.rt_uncached); 1568d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1578d0b94afSMartin KaFai Lau } 1588d0b94afSMartin KaFai Lau } 1598d0b94afSMartin KaFai Lau 160e5f80fcfSEric Dumazet static void rt6_uncached_list_flush_dev(struct net_device *dev) 1618d0b94afSMartin KaFai Lau { 1628d0b94afSMartin KaFai Lau int cpu; 1638d0b94afSMartin KaFai Lau 1648d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1658d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 166ba55ef81SEric Dumazet struct rt6_info *rt, *safe; 167ba55ef81SEric Dumazet 168ba55ef81SEric Dumazet if (list_empty(&ul->head)) 169ba55ef81SEric Dumazet continue; 1708d0b94afSMartin KaFai Lau 1718d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 172d288a162SWangyang Guo list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) { 1738d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1748d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 175ba55ef81SEric Dumazet bool handled = false; 1768d0b94afSMartin KaFai Lau 177e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 178e5f80fcfSEric Dumazet rt->rt6i_idev = in6_dev_get(blackhole_netdev); 1798d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 180ba55ef81SEric Dumazet handled = true; 1818d0b94afSMartin KaFai Lau } 1828d0b94afSMartin KaFai Lau 183e332bc67SEric W. Biederman if (rt_dev == dev) { 1848d7017fdSMahesh Bandewar rt->dst.dev = blackhole_netdev; 185d62607c3SJakub Kicinski netdev_ref_replace(rt_dev, blackhole_netdev, 1869038c320SEric Dumazet &rt->dst.dev_tracker, 1879038c320SEric Dumazet GFP_ATOMIC); 188ba55ef81SEric Dumazet handled = true; 1898d0b94afSMartin KaFai Lau } 190ba55ef81SEric Dumazet if (handled) 191d288a162SWangyang Guo list_move(&rt->dst.rt_uncached, 192ba55ef81SEric Dumazet &ul->quarantine); 1938d0b94afSMartin KaFai Lau } 1948d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1958d0b94afSMartin KaFai Lau } 1968d0b94afSMartin KaFai Lau } 1978d0b94afSMartin KaFai Lau 198f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 199f894cbf8SDavid S. Miller struct sk_buff *skb, 200f894cbf8SDavid S. Miller const void *daddr) 20139232973SDavid S. Miller { 202a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20339232973SDavid S. Miller return (const void *) p; 204f894cbf8SDavid S. Miller else if (skb) 205f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 20639232973SDavid S. Miller return daddr; 20739232973SDavid S. Miller } 20839232973SDavid S. Miller 209f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 210f8a1b43bSDavid Ahern struct net_device *dev, 211f894cbf8SDavid S. Miller struct sk_buff *skb, 212f894cbf8SDavid S. Miller const void *daddr) 213d3aaeb38SDavid S. Miller { 21439232973SDavid S. Miller struct neighbour *n; 21539232973SDavid S. Miller 216f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 217f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 218f83c7790SDavid S. Miller if (n) 219f83c7790SDavid S. Miller return n; 2207adf3246SStefano Brivio 2217adf3246SStefano Brivio n = neigh_create(&nd_tbl, daddr, dev); 2227adf3246SStefano Brivio return IS_ERR(n) ? NULL : n; 223f8a1b43bSDavid Ahern } 224f8a1b43bSDavid Ahern 225f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 226f8a1b43bSDavid Ahern struct sk_buff *skb, 227f8a1b43bSDavid Ahern const void *daddr) 228f8a1b43bSDavid Ahern { 229f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 230f8a1b43bSDavid Ahern 2312c6b55f4SNicolas Dichtel return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), 2322c6b55f4SNicolas Dichtel dst->dev, skb, daddr); 233f83c7790SDavid S. Miller } 234f83c7790SDavid S. Miller 23563fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23663fca65dSJulian Anastasov { 23763fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23863fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23963fca65dSJulian Anastasov 240cbfd6891SStefano Brivio daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr); 24163fca65dSJulian Anastasov if (!daddr) 24263fca65dSJulian Anastasov return; 24363fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 24463fca65dSJulian Anastasov return; 24563fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24663fca65dSJulian Anastasov return; 24763fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24863fca65dSJulian Anastasov } 24963fca65dSJulian Anastasov 2509a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2511da177e4SLinus Torvalds .family = AF_INET6, 2521da177e4SLinus Torvalds .gc = ip6_dst_gc, 2531da177e4SLinus Torvalds .gc_thresh = 1024, 2541da177e4SLinus Torvalds .check = ip6_dst_check, 2550dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 256ebb762f2SSteffen Klassert .mtu = ip6_mtu, 257d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2581da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2591da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2601da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2611da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2621da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2636e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2649f8955ccSEric W. Biederman .local_out = __ip6_local_out, 265f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 26663fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2671da177e4SLinus Torvalds }; 2681da177e4SLinus Torvalds 26914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27014e50e57SDavid S. Miller .family = AF_INET6, 271214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 272f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 273c4c877b2SDaniel Borkmann .check = ip6_dst_check, 274c4c877b2SDaniel Borkmann .destroy = ip6_dst_destroy, 275c4c877b2SDaniel Borkmann .cow_metrics = dst_cow_metrics_generic, 276c4c877b2SDaniel Borkmann .update_pmtu = dst_blackhole_update_pmtu, 277c4c877b2SDaniel Borkmann .redirect = dst_blackhole_redirect, 278c4c877b2SDaniel Borkmann .mtu = dst_blackhole_mtu, 27914e50e57SDavid S. Miller }; 28014e50e57SDavid S. Miller 28162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 28214edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 28362fa8a84SDavid S. Miller }; 28462fa8a84SDavid S. Miller 2858d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 28693c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 28793c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 28893c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 289f05713e0SEric Dumazet .fib6_ref = REFCOUNT_INIT(1), 290421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 291421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 292421842edSDavid Ahern }; 293421842edSDavid Ahern 294fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2951da177e4SLinus Torvalds .dst = { 296bc9d3a9fSThomas Gleixner .__rcuref = RCUREF_INIT(1), 2971da177e4SLinus Torvalds .__use = 1, 2982c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2991da177e4SLinus Torvalds .error = -ENETUNREACH, 3001da177e4SLinus Torvalds .input = ip6_pkt_discard, 3011da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3021da177e4SLinus Torvalds }, 3031da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3041da177e4SLinus Torvalds }; 3051da177e4SLinus Torvalds 306101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 307101367c2SThomas Graf 308fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 309101367c2SThomas Graf .dst = { 310bc9d3a9fSThomas Gleixner .__rcuref = RCUREF_INIT(1), 311101367c2SThomas Graf .__use = 1, 3122c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 313101367c2SThomas Graf .error = -EACCES, 3149ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3159ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 316101367c2SThomas Graf }, 317101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 318101367c2SThomas Graf }; 319101367c2SThomas Graf 320fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 321101367c2SThomas Graf .dst = { 322bc9d3a9fSThomas Gleixner .__rcuref = RCUREF_INIT(1), 323101367c2SThomas Graf .__use = 1, 3242c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 325101367c2SThomas Graf .error = -EINVAL, 326352e512cSHerbert Xu .input = dst_discard, 327ede2059dSEric W. Biederman .output = dst_discard_out, 328101367c2SThomas Graf }, 329101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 330101367c2SThomas Graf }; 331101367c2SThomas Graf 332101367c2SThomas Graf #endif 333101367c2SThomas Graf 334ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 335ebfa45f0SMartin KaFai Lau { 3368f2a83b4SKees Cook memset_after(rt, 0, dst); 337ebfa45f0SMartin KaFai Lau } 338ebfa45f0SMartin KaFai Lau 3391da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 34093531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 341ad706862SMartin KaFai Lau int flags) 3421da177e4SLinus Torvalds { 34397bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 344b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 345cf911662SDavid S. Miller 34681eb8447SWei Wang if (rt) { 347ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 34881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 34981eb8447SWei Wang } 3508104891bSSteffen Klassert 351cf911662SDavid S. Miller return rt; 3521da177e4SLinus Torvalds } 3539ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 354d52d3997SMartin KaFai Lau 3551da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 358a68886a6SDavid Ahern struct fib6_info *from; 3598d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3601da177e4SLinus Torvalds 3611620a336SDavid Ahern ip_dst_metrics_put(dst); 3628d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3638d0b94afSMartin KaFai Lau 3648d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 36538308473SDavid S. Miller if (idev) { 3661da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3671da177e4SLinus Torvalds in6_dev_put(idev); 3681da177e4SLinus Torvalds } 3691716a961SGao feng 3700e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt->from, NULL); 37193531c67SDavid Ahern fib6_info_release(from); 372b3419363SDavid S. Miller } 373b3419363SDavid S. Miller 3741da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3751da177e4SLinus Torvalds int how) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3781da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3791da177e4SLinus Torvalds 380e5f80fcfSEric Dumazet if (idev && idev->dev != blackhole_netdev) { 381e5f80fcfSEric Dumazet struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev); 382e5f80fcfSEric Dumazet 383e5f80fcfSEric Dumazet if (blackhole_idev) { 384e5f80fcfSEric Dumazet rt->rt6i_idev = blackhole_idev; 3851da177e4SLinus Torvalds in6_dev_put(idev); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds } 38897cac082SDavid S. Miller } 3891da177e4SLinus Torvalds 3905973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 3915973fb1eSMartin KaFai Lau { 3925973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 3935973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 3945973fb1eSMartin KaFai Lau else 3955973fb1eSMartin KaFai Lau return false; 3965973fb1eSMartin KaFai Lau } 3975973fb1eSMartin KaFai Lau 398a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3991da177e4SLinus Torvalds { 400a68886a6SDavid Ahern struct fib6_info *from; 401a68886a6SDavid Ahern 402a68886a6SDavid Ahern from = rcu_dereference(rt->from); 403a68886a6SDavid Ahern 4041716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4051716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 406a50feda5SEric Dumazet return true; 407a68886a6SDavid Ahern } else if (from) { 4081e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 409a68886a6SDavid Ahern fib6_check_expired(from); 4101716a961SGao feng } 411a50feda5SEric Dumazet return false; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 414b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res, 415b1d40991SDavid Ahern struct flowi6 *fl6, int oif, bool have_oif_match, 416b1d40991SDavid Ahern const struct sk_buff *skb, int strict) 41751ebd318SNicolas Dichtel { 4188d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 419b1d40991SDavid Ahern struct fib6_info *match = res->f6i; 420b1d40991SDavid Ahern 42134fe5a1cSDavid Ahern if (!match->nh && (!match->fib6_nsiblings || have_oif_match)) 422b1d40991SDavid Ahern goto out; 42351ebd318SNicolas Dichtel 42434fe5a1cSDavid Ahern if (match->nh && have_oif_match && res->nh) 42534fe5a1cSDavid Ahern return; 42634fe5a1cSDavid Ahern 427b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 428b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 429b673d6ccSJakub Sitnicki */ 430f88d8ea6SDavid Ahern if (!fl6->mp_hash && 431f88d8ea6SDavid Ahern (!match->nh || nexthop_is_multipath(match->nh))) 432b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 433b673d6ccSJakub Sitnicki 434f88d8ea6SDavid Ahern if (unlikely(match->nh)) { 435f88d8ea6SDavid Ahern nexthop_path_fib6_result(res, fl6->mp_hash); 436f88d8ea6SDavid Ahern return; 437f88d8ea6SDavid Ahern } 438f88d8ea6SDavid Ahern 4391cf844c7SDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) 440b1d40991SDavid Ahern goto out; 441bbfcd776SIdo Schimmel 44293c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 44393c2fb25SDavid Ahern fib6_siblings) { 4441cf844c7SDavid Ahern const struct fib6_nh *nh = sibling->fib6_nh; 4455e670d84SDavid Ahern int nh_upper_bound; 4465e670d84SDavid Ahern 447702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4485e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4493d709f69SIdo Schimmel continue; 450702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 45152bd4c0cSNicolas Dichtel break; 45251ebd318SNicolas Dichtel match = sibling; 45351ebd318SNicolas Dichtel break; 45451ebd318SNicolas Dichtel } 4553d709f69SIdo Schimmel 456b1d40991SDavid Ahern out: 457b1d40991SDavid Ahern res->f6i = match; 4581cf844c7SDavid Ahern res->nh = match->fib6_nh; 45951ebd318SNicolas Dichtel } 46051ebd318SNicolas Dichtel 4611da177e4SLinus Torvalds /* 46266f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 4650c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 4660c59d006SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4670c59d006SDavid Ahern { 4680c59d006SDavid Ahern const struct net_device *dev; 4690c59d006SDavid Ahern 4700c59d006SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 4710c59d006SDavid Ahern return false; 4720c59d006SDavid Ahern 4730c59d006SDavid Ahern dev = nh->fib_nh_dev; 4740c59d006SDavid Ahern if (oif) { 4750c59d006SDavid Ahern if (dev->ifindex == oif) 4760c59d006SDavid Ahern return true; 4770c59d006SDavid Ahern } else { 4780c59d006SDavid Ahern if (ipv6_chk_addr(net, saddr, dev, 4790c59d006SDavid Ahern flags & RT6_LOOKUP_F_IFACE)) 4800c59d006SDavid Ahern return true; 4810c59d006SDavid Ahern } 4820c59d006SDavid Ahern 4830c59d006SDavid Ahern return false; 4840c59d006SDavid Ahern } 4850c59d006SDavid Ahern 486962b6803SDavid Ahern struct fib6_nh_dm_arg { 487962b6803SDavid Ahern struct net *net; 488962b6803SDavid Ahern const struct in6_addr *saddr; 489962b6803SDavid Ahern int oif; 490962b6803SDavid Ahern int flags; 491962b6803SDavid Ahern struct fib6_nh *nh; 492962b6803SDavid Ahern }; 493962b6803SDavid Ahern 494962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) 495962b6803SDavid Ahern { 496962b6803SDavid Ahern struct fib6_nh_dm_arg *arg = _arg; 497962b6803SDavid Ahern 498962b6803SDavid Ahern arg->nh = nh; 499962b6803SDavid Ahern return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, 500962b6803SDavid Ahern arg->flags); 501962b6803SDavid Ahern } 502962b6803SDavid Ahern 503962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */ 504962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, 505962b6803SDavid Ahern struct fib6_result *res, 506962b6803SDavid Ahern const struct in6_addr *saddr, 507962b6803SDavid Ahern int oif, int flags) 508962b6803SDavid Ahern { 509962b6803SDavid Ahern struct fib6_nh_dm_arg arg = { 510962b6803SDavid Ahern .net = net, 511962b6803SDavid Ahern .saddr = saddr, 512962b6803SDavid Ahern .oif = oif, 513962b6803SDavid Ahern .flags = flags, 514962b6803SDavid Ahern }; 515962b6803SDavid Ahern 516962b6803SDavid Ahern if (nexthop_is_blackhole(nh)) 517962b6803SDavid Ahern return NULL; 518962b6803SDavid Ahern 519962b6803SDavid Ahern if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) 520962b6803SDavid Ahern return arg.nh; 521962b6803SDavid Ahern 522962b6803SDavid Ahern return NULL; 523962b6803SDavid Ahern } 524962b6803SDavid Ahern 52575ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res, 52675ef7389SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 5271da177e4SLinus Torvalds { 52875ef7389SDavid Ahern struct fib6_info *f6i = res->f6i; 52975ef7389SDavid Ahern struct fib6_info *spf6i; 53075ef7389SDavid Ahern struct fib6_nh *nh; 5311da177e4SLinus Torvalds 53275ef7389SDavid Ahern if (!oif && ipv6_addr_any(saddr)) { 533f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 534f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 535f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 536f88d8ea6SDavid Ahern goto out_blackhole; 537f88d8ea6SDavid Ahern } else { 5381cf844c7SDavid Ahern nh = f6i->fib6_nh; 539f88d8ea6SDavid Ahern } 5407d21fec9SDavid Ahern if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 5417d21fec9SDavid Ahern goto out; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 54475ef7389SDavid Ahern for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 545962b6803SDavid Ahern bool matched = false; 546962b6803SDavid Ahern 547962b6803SDavid Ahern if (unlikely(spf6i->nh)) { 548962b6803SDavid Ahern nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, 549962b6803SDavid Ahern oif, flags); 550962b6803SDavid Ahern if (nh) 551962b6803SDavid Ahern matched = true; 552962b6803SDavid Ahern } else { 5531cf844c7SDavid Ahern nh = spf6i->fib6_nh; 554962b6803SDavid Ahern if (__rt6_device_match(net, nh, saddr, oif, flags)) 555962b6803SDavid Ahern matched = true; 556962b6803SDavid Ahern } 557962b6803SDavid Ahern if (matched) { 55875ef7389SDavid Ahern res->f6i = spf6i; 5597d21fec9SDavid Ahern goto out; 56075ef7389SDavid Ahern } 56175ef7389SDavid Ahern } 5621da177e4SLinus Torvalds 56375ef7389SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) { 56475ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5651cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 5667d21fec9SDavid Ahern goto out; 56775ef7389SDavid Ahern } 56875ef7389SDavid Ahern 569f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 570f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 571f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 572f88d8ea6SDavid Ahern goto out_blackhole; 573f88d8ea6SDavid Ahern } else { 5741cf844c7SDavid Ahern nh = f6i->fib6_nh; 575f88d8ea6SDavid Ahern } 576f88d8ea6SDavid Ahern 5777d21fec9SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) { 57875ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5791cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 58075ef7389SDavid Ahern } 5817d21fec9SDavid Ahern out: 5827d21fec9SDavid Ahern res->nh = nh; 5837d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 5847d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 585f88d8ea6SDavid Ahern return; 586f88d8ea6SDavid Ahern 587f88d8ea6SDavid Ahern out_blackhole: 588f88d8ea6SDavid Ahern res->fib6_flags |= RTF_REJECT; 589f88d8ea6SDavid Ahern res->fib6_type = RTN_BLACKHOLE; 590f88d8ea6SDavid Ahern res->nh = nh; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 59327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 594c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 595c2f17e82SHannes Frederic Sowa struct work_struct work; 596c2f17e82SHannes Frederic Sowa struct in6_addr target; 597c2f17e82SHannes Frederic Sowa struct net_device *dev; 598fb67510bSEric Dumazet netdevice_tracker dev_tracker; 599c2f17e82SHannes Frederic Sowa }; 600c2f17e82SHannes Frederic Sowa 601c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 602c2f17e82SHannes Frederic Sowa { 603c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 604c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 605c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 606c2f17e82SHannes Frederic Sowa 607c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 608adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 609d62607c3SJakub Kicinski netdev_put(work->dev, &work->dev_tracker); 610662f5533SMichael Büsch kfree(work); 611c2f17e82SHannes Frederic Sowa } 612c2f17e82SHannes Frederic Sowa 613cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 61427097255SYOSHIFUJI Hideaki { 615f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 6165e670d84SDavid Ahern const struct in6_addr *nh_gw; 6171bef4c22SEric Dumazet unsigned long last_probe; 618f2c31e32SEric Dumazet struct neighbour *neigh; 6195e670d84SDavid Ahern struct net_device *dev; 620f547fac6SSabrina Dubroca struct inet6_dev *idev; 6215e670d84SDavid Ahern 62227097255SYOSHIFUJI Hideaki /* 62327097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 62427097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 62527097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 62627097255SYOSHIFUJI Hideaki * 62727097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 62827097255SYOSHIFUJI Hideaki * to no more than one per minute. 62927097255SYOSHIFUJI Hideaki */ 630004b3942SHangbin Liu if (!fib6_nh->fib_nh_gw_family) 631fdd6681dSAmerigo Wang return; 6325e670d84SDavid Ahern 633cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 634cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 63509eed119SEric Dumazet rcu_read_lock(); 6361bef4c22SEric Dumazet last_probe = READ_ONCE(fib6_nh->last_probe); 637f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 6385e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 6392152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 640b071af52SEric Dumazet if (READ_ONCE(neigh->nud_state) & NUD_VALID) 6418d6c31bfSMartin KaFai Lau goto out; 6428d6c31bfSMartin KaFai Lau 64309eed119SEric Dumazet write_lock_bh(&neigh->lock); 644990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 645990edb42SMartin KaFai Lau time_after(jiffies, 646dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 647c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 648990edb42SMartin KaFai Lau if (work) 6497e980569SJiri Benc __neigh_set_probe_once(neigh); 650990edb42SMartin KaFai Lau } 65109eed119SEric Dumazet write_unlock_bh(&neigh->lock); 6521bef4c22SEric Dumazet } else if (time_after(jiffies, last_probe + 653f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 654990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 655990edb42SMartin KaFai Lau } 656c2f17e82SHannes Frederic Sowa 6571bef4c22SEric Dumazet if (!work || cmpxchg(&fib6_nh->last_probe, 6581bef4c22SEric Dumazet last_probe, jiffies) != last_probe) { 6591bef4c22SEric Dumazet kfree(work); 6601bef4c22SEric Dumazet } else { 661c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 6625e670d84SDavid Ahern work->target = *nh_gw; 663d62607c3SJakub Kicinski netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC); 6645e670d84SDavid Ahern work->dev = dev; 665c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 666c2f17e82SHannes Frederic Sowa } 667990edb42SMartin KaFai Lau 6688d6c31bfSMartin KaFai Lau out: 66909eed119SEric Dumazet rcu_read_unlock(); 670f2c31e32SEric Dumazet } 67127097255SYOSHIFUJI Hideaki #else 672cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 67327097255SYOSHIFUJI Hideaki { 67427097255SYOSHIFUJI Hideaki } 67527097255SYOSHIFUJI Hideaki #endif 67627097255SYOSHIFUJI Hideaki 6771da177e4SLinus Torvalds /* 678554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6791da177e4SLinus Torvalds */ 6801ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 6811da177e4SLinus Torvalds { 682afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 6835e670d84SDavid Ahern struct neighbour *neigh; 684f2c31e32SEric Dumazet 68509eed119SEric Dumazet rcu_read_lock(); 6861ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 6871ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 688145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 689c486640aSEric Dumazet u8 nud_state = READ_ONCE(neigh->nud_state); 690c486640aSEric Dumazet 691c486640aSEric Dumazet if (nud_state & NUD_VALID) 692afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 693398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 694c486640aSEric Dumazet else if (!(nud_state & NUD_FAILED)) 695afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6967e980569SJiri Benc else 6977e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 698398bcbebSYOSHIFUJI Hideaki #endif 699afc154e9SHannes Frederic Sowa } else { 700afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 7017e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 702a5a81f0bSPaul Marks } 70309eed119SEric Dumazet rcu_read_unlock(); 704145a3621SYOSHIFUJI Hideaki / 吉藤英明 705a5a81f0bSPaul Marks return ret; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 708702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 709702cea56SDavid Ahern int strict) 710554cfb7eSYOSHIFUJI Hideaki { 7116e1809a5SDavid Ahern int m = 0; 7124d0c5911SYOSHIFUJI Hideaki 7136e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 7146e1809a5SDavid Ahern m = 2; 7156e1809a5SDavid Ahern 71677d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 717afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 718ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 719702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 720ebacaaa0SYOSHIFUJI Hideaki #endif 7211ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 722702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 7231ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 724afc154e9SHannes Frederic Sowa if (n < 0) 725afc154e9SHannes Frederic Sowa return n; 726afc154e9SHannes Frederic Sowa } 727554cfb7eSYOSHIFUJI Hideaki return m; 728554cfb7eSYOSHIFUJI Hideaki } 729554cfb7eSYOSHIFUJI Hideaki 73028679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 73128679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 732554cfb7eSYOSHIFUJI Hideaki { 733afc154e9SHannes Frederic Sowa bool match_do_rr = false; 73428679ed1SDavid Ahern bool rc = false; 73528679ed1SDavid Ahern int m; 73635103d11SAndy Gospodarek 73728679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 7388067bb8cSIdo Schimmel goto out; 7398067bb8cSIdo Schimmel 74028679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 74128679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 742d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 74335103d11SAndy Gospodarek goto out; 744554cfb7eSYOSHIFUJI Hideaki 74528679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 7467e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 747afc154e9SHannes Frederic Sowa match_do_rr = true; 748afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 7497e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 750f11e6659SDavid S. Miller goto out; 7511da177e4SLinus Torvalds } 752f11e6659SDavid S. Miller 753afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 75428679ed1SDavid Ahern rt6_probe(nh); 755afc154e9SHannes Frederic Sowa 7567e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 757afc154e9SHannes Frederic Sowa if (m > *mpri) { 758afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 759afc154e9SHannes Frederic Sowa *mpri = m; 76028679ed1SDavid Ahern rc = true; 761afc154e9SHannes Frederic Sowa } 762f11e6659SDavid S. Miller out: 76328679ed1SDavid Ahern return rc; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 76617a5984eSDavid Ahern struct fib6_nh_frl_arg { 76717a5984eSDavid Ahern u32 flags; 76817a5984eSDavid Ahern int oif; 76917a5984eSDavid Ahern int strict; 77017a5984eSDavid Ahern int *mpri; 77117a5984eSDavid Ahern bool *do_rr; 77217a5984eSDavid Ahern struct fib6_nh *nh; 77317a5984eSDavid Ahern }; 77417a5984eSDavid Ahern 77517a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) 77617a5984eSDavid Ahern { 77717a5984eSDavid Ahern struct fib6_nh_frl_arg *arg = _arg; 77817a5984eSDavid Ahern 77917a5984eSDavid Ahern arg->nh = nh; 78017a5984eSDavid Ahern return find_match(nh, arg->flags, arg->oif, arg->strict, 78117a5984eSDavid Ahern arg->mpri, arg->do_rr); 78217a5984eSDavid Ahern } 78317a5984eSDavid Ahern 784b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start, 78530c15f03SDavid Ahern struct fib6_info *nomatch, u32 metric, 786b7bc4b6aSDavid Ahern struct fib6_result *res, struct fib6_info **cont, 78730c15f03SDavid Ahern int oif, int strict, bool *do_rr, int *mpri) 78830c15f03SDavid Ahern { 789b7bc4b6aSDavid Ahern struct fib6_info *f6i; 79030c15f03SDavid Ahern 791b7bc4b6aSDavid Ahern for (f6i = f6i_start; 792b7bc4b6aSDavid Ahern f6i && f6i != nomatch; 793b7bc4b6aSDavid Ahern f6i = rcu_dereference(f6i->fib6_next)) { 79417a5984eSDavid Ahern bool matched = false; 79530c15f03SDavid Ahern struct fib6_nh *nh; 79630c15f03SDavid Ahern 797b7bc4b6aSDavid Ahern if (cont && f6i->fib6_metric != metric) { 798b7bc4b6aSDavid Ahern *cont = f6i; 79930c15f03SDavid Ahern return; 80030c15f03SDavid Ahern } 80130c15f03SDavid Ahern 802b7bc4b6aSDavid Ahern if (fib6_check_expired(f6i)) 80330c15f03SDavid Ahern continue; 80430c15f03SDavid Ahern 80517a5984eSDavid Ahern if (unlikely(f6i->nh)) { 80617a5984eSDavid Ahern struct fib6_nh_frl_arg arg = { 80717a5984eSDavid Ahern .flags = f6i->fib6_flags, 80817a5984eSDavid Ahern .oif = oif, 80917a5984eSDavid Ahern .strict = strict, 81017a5984eSDavid Ahern .mpri = mpri, 81117a5984eSDavid Ahern .do_rr = do_rr 81217a5984eSDavid Ahern }; 81317a5984eSDavid Ahern 81417a5984eSDavid Ahern if (nexthop_is_blackhole(f6i->nh)) { 81517a5984eSDavid Ahern res->fib6_flags = RTF_REJECT; 81617a5984eSDavid Ahern res->fib6_type = RTN_BLACKHOLE; 81717a5984eSDavid Ahern res->f6i = f6i; 81817a5984eSDavid Ahern res->nh = nexthop_fib6_nh(f6i->nh); 81917a5984eSDavid Ahern return; 82017a5984eSDavid Ahern } 82117a5984eSDavid Ahern if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, 82217a5984eSDavid Ahern &arg)) { 82317a5984eSDavid Ahern matched = true; 82417a5984eSDavid Ahern nh = arg.nh; 82517a5984eSDavid Ahern } 82617a5984eSDavid Ahern } else { 8271cf844c7SDavid Ahern nh = f6i->fib6_nh; 82817a5984eSDavid Ahern if (find_match(nh, f6i->fib6_flags, oif, strict, 82917a5984eSDavid Ahern mpri, do_rr)) 83017a5984eSDavid Ahern matched = true; 83117a5984eSDavid Ahern } 83217a5984eSDavid Ahern if (matched) { 833b7bc4b6aSDavid Ahern res->f6i = f6i; 834b7bc4b6aSDavid Ahern res->nh = nh; 8357d21fec9SDavid Ahern res->fib6_flags = f6i->fib6_flags; 8367d21fec9SDavid Ahern res->fib6_type = f6i->fib6_type; 837b7bc4b6aSDavid Ahern } 83830c15f03SDavid Ahern } 83930c15f03SDavid Ahern } 84030c15f03SDavid Ahern 841b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 842b7bc4b6aSDavid Ahern struct fib6_info *rr_head, int oif, int strict, 843b7bc4b6aSDavid Ahern bool *do_rr, struct fib6_result *res) 844f11e6659SDavid S. Miller { 845b7bc4b6aSDavid Ahern u32 metric = rr_head->fib6_metric; 846b7bc4b6aSDavid Ahern struct fib6_info *cont = NULL; 847f11e6659SDavid S. Miller int mpri = -1; 848f11e6659SDavid S. Miller 849b7bc4b6aSDavid Ahern __find_rr_leaf(rr_head, NULL, metric, res, &cont, 85030c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8519fbdcfafSSteffen Klassert 852b7bc4b6aSDavid Ahern __find_rr_leaf(leaf, rr_head, metric, res, &cont, 85330c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8549fbdcfafSSteffen Klassert 855b7bc4b6aSDavid Ahern if (res->f6i || !cont) 856b7bc4b6aSDavid Ahern return; 8579fbdcfafSSteffen Klassert 858b7bc4b6aSDavid Ahern __find_rr_leaf(cont, NULL, metric, res, NULL, 85930c15f03SDavid Ahern oif, strict, do_rr, &mpri); 860f11e6659SDavid S. Miller } 861f11e6659SDavid S. Miller 862b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 863b7bc4b6aSDavid Ahern struct fib6_result *res, int strict) 864f11e6659SDavid S. Miller { 8658d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 866b7bc4b6aSDavid Ahern struct fib6_info *rt0; 867afc154e9SHannes Frederic Sowa bool do_rr = false; 86817ecf590SWei Wang int key_plen; 869f11e6659SDavid S. Miller 870b7bc4b6aSDavid Ahern /* make sure this function or its helpers sets f6i */ 871b7bc4b6aSDavid Ahern res->f6i = NULL; 872b7bc4b6aSDavid Ahern 873421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 874b7bc4b6aSDavid Ahern goto out; 8758d1040e8SWei Wang 87666f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 877f11e6659SDavid S. Miller if (!rt0) 87866f5d6ceSWei Wang rt0 = leaf; 879f11e6659SDavid S. Miller 88017ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 88117ecf590SWei Wang * and fn->leaf does not points to its child's leaf 88217ecf590SWei Wang * (This might happen if all routes under fn are deleted from 88317ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 88417ecf590SWei Wang */ 88593c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 88617ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 88793c2fb25SDavid Ahern if (rt0->fib6_src.plen) 88893c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 88917ecf590SWei Wang #endif 89017ecf590SWei Wang if (fn->fn_bit != key_plen) 891b7bc4b6aSDavid Ahern goto out; 89217ecf590SWei Wang 893b7bc4b6aSDavid Ahern find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 894afc154e9SHannes Frederic Sowa if (do_rr) { 8958fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 896f11e6659SDavid S. Miller 897554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 89893c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 8998d1040e8SWei Wang next = leaf; 900f11e6659SDavid S. Miller 90166f5d6ceSWei Wang if (next != rt0) { 90293c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 90366f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 90493c2fb25SDavid Ahern if (next->fib6_node) 90566f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 90693c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 90766f5d6ceSWei Wang } 908554cfb7eSYOSHIFUJI Hideaki } 909554cfb7eSYOSHIFUJI Hideaki 910b7bc4b6aSDavid Ahern out: 911b7bc4b6aSDavid Ahern if (!res->f6i) { 912b7bc4b6aSDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 9131cf844c7SDavid Ahern res->nh = res->f6i->fib6_nh; 9147d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 9157d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 916b7bc4b6aSDavid Ahern } 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 91985bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 9208b9df265SMartin KaFai Lau { 92185bd05deSDavid Ahern return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 92285bd05deSDavid Ahern res->nh->fib_nh_gw_family; 9238b9df265SMartin KaFai Lau } 9248b9df265SMartin KaFai Lau 92570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 92670ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 927b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 92870ceb4f5SYOSHIFUJI Hideaki { 929c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 93070ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 93170ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 93270ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 9334bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 9348d1c802bSDavid Ahern struct fib6_info *rt; 93570ceb4f5SYOSHIFUJI Hideaki 93670ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 93770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 93870ceb4f5SYOSHIFUJI Hideaki } 93970ceb4f5SYOSHIFUJI Hideaki 94070ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 94170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 94270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94370ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 94470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 94670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 94770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94870ceb4f5SYOSHIFUJI Hideaki } 94970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 95070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 95170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95270ceb4f5SYOSHIFUJI Hideaki } 95370ceb4f5SYOSHIFUJI Hideaki } 95470ceb4f5SYOSHIFUJI Hideaki 95570ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 95670ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 9573933fc95SJens Rosenboom return -EINVAL; 95870ceb4f5SYOSHIFUJI Hideaki 9594bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 96070ceb4f5SYOSHIFUJI Hideaki 96170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 96270ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 96370ceb4f5SYOSHIFUJI Hideaki else { 96470ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 96570ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 96670ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 96770ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 96870ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 96970ceb4f5SYOSHIFUJI Hideaki } 97070ceb4f5SYOSHIFUJI Hideaki 971f104a567SDuan Jiong if (rinfo->prefix_len == 0) 972afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 973f104a567SDuan Jiong else 974f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 975830218c1SDavid Ahern gwaddr, dev); 97670ceb4f5SYOSHIFUJI Hideaki 97770ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 97811dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 97970ceb4f5SYOSHIFUJI Hideaki rt = NULL; 98070ceb4f5SYOSHIFUJI Hideaki } 98170ceb4f5SYOSHIFUJI Hideaki 98270ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 983830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 984830218c1SDavid Ahern dev, pref); 98570ceb4f5SYOSHIFUJI Hideaki else if (rt) 98693c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 98793c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 98870ceb4f5SYOSHIFUJI Hideaki 98970ceb4f5SYOSHIFUJI Hideaki if (rt) { 9901716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 99114895687SDavid Ahern fib6_clean_expires(rt); 9921716a961SGao feng else 99314895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 9941716a961SGao feng 99593531c67SDavid Ahern fib6_info_release(rt); 99670ceb4f5SYOSHIFUJI Hideaki } 99770ceb4f5SYOSHIFUJI Hideaki return 0; 99870ceb4f5SYOSHIFUJI Hideaki } 99970ceb4f5SYOSHIFUJI Hideaki #endif 100070ceb4f5SYOSHIFUJI Hideaki 1001ae90d867SDavid Ahern /* 1002ae90d867SDavid Ahern * Misc support functions 1003ae90d867SDavid Ahern */ 1004ae90d867SDavid Ahern 1005ae90d867SDavid Ahern /* called with rcu_lock held */ 10060d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 1007ae90d867SDavid Ahern { 10080d161581SDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 1009ae90d867SDavid Ahern 10107d21fec9SDavid Ahern if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 1011ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 1012ae90d867SDavid Ahern * device if it is a master device, the master device if 1013ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 1014ae90d867SDavid Ahern */ 1015ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 10167d21fec9SDavid Ahern !rt6_need_strict(&res->f6i->fib6_dst.addr)) 1017ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 1018ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 1019ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 1020ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 1021ae90d867SDavid Ahern * case we want dev returned to be dev 1022ae90d867SDavid Ahern */ 1023ae90d867SDavid Ahern } 1024ae90d867SDavid Ahern 1025ae90d867SDavid Ahern return dev; 1026ae90d867SDavid Ahern } 1027ae90d867SDavid Ahern 10286edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 10296edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 10306edb3c96SDavid Ahern [RTN_UNICAST] = 0, 10316edb3c96SDavid Ahern [RTN_LOCAL] = 0, 10326edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 10336edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 10346edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 10356edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 10366edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 10376edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 10386edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 10396edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 10406edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 10416edb3c96SDavid Ahern }; 10426edb3c96SDavid Ahern 10436edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 10446edb3c96SDavid Ahern { 10456edb3c96SDavid Ahern return fib6_prop[fib6_type]; 10466edb3c96SDavid Ahern } 10476edb3c96SDavid Ahern 10488d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 10493b6761d1SDavid Ahern { 10503b6761d1SDavid Ahern unsigned short flags = 0; 10513b6761d1SDavid Ahern 10523b6761d1SDavid Ahern if (rt->dst_nocount) 10533b6761d1SDavid Ahern flags |= DST_NOCOUNT; 10543b6761d1SDavid Ahern if (rt->dst_nopolicy) 10553b6761d1SDavid Ahern flags |= DST_NOPOLICY; 10563b6761d1SDavid Ahern 10573b6761d1SDavid Ahern return flags; 10583b6761d1SDavid Ahern } 10593b6761d1SDavid Ahern 10607d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 10616edb3c96SDavid Ahern { 10627d21fec9SDavid Ahern rt->dst.error = ip6_rt_type_to_error(fib6_type); 10636edb3c96SDavid Ahern 10647d21fec9SDavid Ahern switch (fib6_type) { 10656edb3c96SDavid Ahern case RTN_BLACKHOLE: 10666edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 10676edb3c96SDavid Ahern rt->dst.input = dst_discard; 10686edb3c96SDavid Ahern break; 10696edb3c96SDavid Ahern case RTN_PROHIBIT: 10706edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 10716edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 10726edb3c96SDavid Ahern break; 10736edb3c96SDavid Ahern case RTN_THROW: 10746edb3c96SDavid Ahern case RTN_UNREACHABLE: 10756edb3c96SDavid Ahern default: 10766edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 10776edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 10786edb3c96SDavid Ahern break; 10796edb3c96SDavid Ahern } 10806edb3c96SDavid Ahern } 10816edb3c96SDavid Ahern 10820d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 10836edb3c96SDavid Ahern { 10847d21fec9SDavid Ahern struct fib6_info *f6i = res->f6i; 10850d161581SDavid Ahern 10867d21fec9SDavid Ahern if (res->fib6_flags & RTF_REJECT) { 10877d21fec9SDavid Ahern ip6_rt_init_dst_reject(rt, res->fib6_type); 10886edb3c96SDavid Ahern return; 10896edb3c96SDavid Ahern } 10906edb3c96SDavid Ahern 10916edb3c96SDavid Ahern rt->dst.error = 0; 10926edb3c96SDavid Ahern rt->dst.output = ip6_output; 10936edb3c96SDavid Ahern 10947d21fec9SDavid Ahern if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 10956edb3c96SDavid Ahern rt->dst.input = ip6_input; 10967d21fec9SDavid Ahern } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 10976edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 10986edb3c96SDavid Ahern } else { 10996edb3c96SDavid Ahern rt->dst.input = ip6_forward; 11006edb3c96SDavid Ahern } 11016edb3c96SDavid Ahern 11020d161581SDavid Ahern if (res->nh->fib_nh_lws) { 11030d161581SDavid Ahern rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 11046edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 11056edb3c96SDavid Ahern } 11066edb3c96SDavid Ahern 11076edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 11086edb3c96SDavid Ahern } 11096edb3c96SDavid Ahern 1110e873e4b9SWei Wang /* Caller must already hold reference to @from */ 11118d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1112ae90d867SDavid Ahern { 1113ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 1114a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 1115e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1116ae90d867SDavid Ahern } 1117ae90d867SDavid Ahern 11180d161581SDavid Ahern /* Caller must already hold reference to f6i in result */ 11190d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1120ae90d867SDavid Ahern { 11210d161581SDavid Ahern const struct fib6_nh *nh = res->nh; 11220d161581SDavid Ahern const struct net_device *dev = nh->fib_nh_dev; 11230d161581SDavid Ahern struct fib6_info *f6i = res->f6i; 1124dcd1f572SDavid Ahern 11250d161581SDavid Ahern ip6_rt_init_dst(rt, res); 11266edb3c96SDavid Ahern 11270d161581SDavid Ahern rt->rt6i_dst = f6i->fib6_dst; 1128dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 11297d21fec9SDavid Ahern rt->rt6i_flags = res->fib6_flags; 11300d161581SDavid Ahern if (nh->fib_nh_gw_family) { 11310d161581SDavid Ahern rt->rt6i_gateway = nh->fib_nh_gw6; 11322b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 11332b2450caSDavid Ahern } 11340d161581SDavid Ahern rt6_set_from(rt, f6i); 1135ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 11360d161581SDavid Ahern rt->rt6i_src = f6i->fib6_src; 1137ae90d867SDavid Ahern #endif 1138ae90d867SDavid Ahern } 1139ae90d867SDavid Ahern 1140a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1141a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1142a3c00e46SMartin KaFai Lau { 114366f5d6ceSWei Wang struct fib6_node *pn, *sn; 1144a3c00e46SMartin KaFai Lau while (1) { 1145a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1146a3c00e46SMartin KaFai Lau return NULL; 114766f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 114866f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 114966f5d6ceSWei Wang if (sn && sn != fn) 11506454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1151a3c00e46SMartin KaFai Lau else 1152a3c00e46SMartin KaFai Lau fn = pn; 1153a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1154a3c00e46SMartin KaFai Lau return fn; 1155a3c00e46SMartin KaFai Lau } 1156a3c00e46SMartin KaFai Lau } 1157c71099acSThomas Graf 115810585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1159d3843fe5SWei Wang { 1160d3843fe5SWei Wang struct rt6_info *rt = *prt; 1161d3843fe5SWei Wang 1162d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1163d3843fe5SWei Wang return true; 116410585b43SDavid Ahern if (net) { 1165d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1166d3843fe5SWei Wang dst_hold(&rt->dst); 1167d3843fe5SWei Wang } else { 1168d3843fe5SWei Wang rt = NULL; 1169d3843fe5SWei Wang } 1170d3843fe5SWei Wang *prt = rt; 1171d3843fe5SWei Wang return false; 1172d3843fe5SWei Wang } 1173d3843fe5SWei Wang 1174dec9b0e2SDavid Ahern /* called with rcu_lock held */ 11759b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1176dec9b0e2SDavid Ahern { 11779b6b35abSDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 11789b6b35abSDavid Ahern struct fib6_info *f6i = res->f6i; 11799b6b35abSDavid Ahern unsigned short flags; 1180dec9b0e2SDavid Ahern struct rt6_info *nrt; 1181dec9b0e2SDavid Ahern 11829b6b35abSDavid Ahern if (!fib6_info_hold_safe(f6i)) 11831c87e79aSXin Long goto fallback; 1184e873e4b9SWei Wang 11859b6b35abSDavid Ahern flags = fib6_info_dst_flags(f6i); 118693531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 11871c87e79aSXin Long if (!nrt) { 11889b6b35abSDavid Ahern fib6_info_release(f6i); 11891c87e79aSXin Long goto fallback; 11901c87e79aSXin Long } 1191dec9b0e2SDavid Ahern 11920d161581SDavid Ahern ip6_rt_copy_init(nrt, res); 11931c87e79aSXin Long return nrt; 11941c87e79aSXin Long 11951c87e79aSXin Long fallback: 11961c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 11971c87e79aSXin Long dst_hold(&nrt->dst); 1198dec9b0e2SDavid Ahern return nrt; 1199dec9b0e2SDavid Ahern } 1200dec9b0e2SDavid Ahern 120155cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net, 12028ed67789SDaniel Lezcano struct fib6_table *table, 1203b75cc8f9SDavid Ahern struct flowi6 *fl6, 1204b75cc8f9SDavid Ahern const struct sk_buff *skb, 1205b75cc8f9SDavid Ahern int flags) 12061da177e4SLinus Torvalds { 1207b1d40991SDavid Ahern struct fib6_result res = {}; 12081da177e4SLinus Torvalds struct fib6_node *fn; 120923fb93a4SDavid Ahern struct rt6_info *rt; 12101da177e4SLinus Torvalds 121166f5d6ceSWei Wang rcu_read_lock(); 12126454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1213c71099acSThomas Graf restart: 1214b1d40991SDavid Ahern res.f6i = rcu_dereference(fn->leaf); 1215b1d40991SDavid Ahern if (!res.f6i) 1216b1d40991SDavid Ahern res.f6i = net->ipv6.fib6_null_entry; 1217af52a52cSDavid Ahern else 121875ef7389SDavid Ahern rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 121975ef7389SDavid Ahern flags); 1220af52a52cSDavid Ahern 1221b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1222a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1223a3c00e46SMartin KaFai Lau if (fn) 1224a3c00e46SMartin KaFai Lau goto restart; 1225af52a52cSDavid Ahern 1226af52a52cSDavid Ahern rt = net->ipv6.ip6_null_entry; 1227af52a52cSDavid Ahern dst_hold(&rt->dst); 1228af52a52cSDavid Ahern goto out; 1229f88d8ea6SDavid Ahern } else if (res.fib6_flags & RTF_REJECT) { 1230f88d8ea6SDavid Ahern goto do_create; 1231a3c00e46SMartin KaFai Lau } 12322b760fcfSWei Wang 1233b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1234b1d40991SDavid Ahern fl6->flowi6_oif != 0, skb, flags); 1235b1d40991SDavid Ahern 12364c9483b2SDavid S. Miller /* Search through exception table */ 12377e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 123823fb93a4SDavid Ahern if (rt) { 123910585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 1240d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 124123fb93a4SDavid Ahern } else { 1242f88d8ea6SDavid Ahern do_create: 12439b6b35abSDavid Ahern rt = ip6_create_rt_rcu(&res); 1244dec9b0e2SDavid Ahern } 1245d3843fe5SWei Wang 1246af52a52cSDavid Ahern out: 12478ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 1248af52a52cSDavid Ahern 124966f5d6ceSWei Wang rcu_read_unlock(); 1250b811580dSDavid Ahern 12511da177e4SLinus Torvalds return rt; 1252c71099acSThomas Graf } 1253c71099acSThomas Graf 1254ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1255b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1256ea6e574eSFlorian Westphal { 1257b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1258ea6e574eSFlorian Westphal } 1259ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1260ea6e574eSFlorian Westphal 12619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1262b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1263b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1264c71099acSThomas Graf { 12654c9483b2SDavid S. Miller struct flowi6 fl6 = { 12664c9483b2SDavid S. Miller .flowi6_oif = oif, 12674c9483b2SDavid S. Miller .daddr = *daddr, 1268c71099acSThomas Graf }; 1269c71099acSThomas Graf struct dst_entry *dst; 127077d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1271c71099acSThomas Graf 1272adaa70bbSThomas Graf if (saddr) { 12734c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1274adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1275adaa70bbSThomas Graf } 1276adaa70bbSThomas Graf 1277b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1278c71099acSThomas Graf if (dst->error == 0) 1279c71099acSThomas Graf return (struct rt6_info *) dst; 1280c71099acSThomas Graf 1281c71099acSThomas Graf dst_release(dst); 1282c71099acSThomas Graf 12831da177e4SLinus Torvalds return NULL; 12841da177e4SLinus Torvalds } 12857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 12867159039aSYOSHIFUJI Hideaki 1287c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 12881cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 12891cfb71eeSWei Wang * route is released. 12901cfb71eeSWei Wang * Caller must hold dst before calling it. 12911da177e4SLinus Torvalds */ 12921da177e4SLinus Torvalds 12938d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1294333c4301SDavid Ahern struct netlink_ext_ack *extack) 12951da177e4SLinus Torvalds { 12961da177e4SLinus Torvalds int err; 1297c71099acSThomas Graf struct fib6_table *table; 12981da177e4SLinus Torvalds 129993c2fb25SDavid Ahern table = rt->fib6_table; 130066f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1301d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 130266f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds return err; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 13078d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 130840e22e8fSThomas Graf { 1309afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1310e715b6d3SFlorian Westphal 1311d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 131240e22e8fSThomas Graf } 131340e22e8fSThomas Graf 131485bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 131521efcfa0SEric Dumazet const struct in6_addr *daddr, 1316b71d1d42SEric Dumazet const struct in6_addr *saddr) 13171da177e4SLinus Torvalds { 131885bd05deSDavid Ahern struct fib6_info *f6i = res->f6i; 13194832c30dSDavid Ahern struct net_device *dev; 13201da177e4SLinus Torvalds struct rt6_info *rt; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Clone the route. 13241da177e4SLinus Torvalds */ 13251da177e4SLinus Torvalds 132685bd05deSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1327e873e4b9SWei Wang return NULL; 1328e873e4b9SWei Wang 13290d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 133093531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1331e873e4b9SWei Wang if (!rt) { 133285bd05deSDavid Ahern fib6_info_release(f6i); 133383a09abdSMartin KaFai Lau return NULL; 1334e873e4b9SWei Wang } 133583a09abdSMartin KaFai Lau 13360d161581SDavid Ahern ip6_rt_copy_init(rt, res); 13378b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 133883a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 133983a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 13408b9df265SMartin KaFai Lau 134185bd05deSDavid Ahern if (!rt6_is_gw_or_nonexthop(res)) { 134285bd05deSDavid Ahern if (f6i->fib6_dst.plen != 128 && 134385bd05deSDavid Ahern ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 134458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 13451da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 13461da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 13474e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 13481da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds #endif 135195a9a5baSYOSHIFUJI Hideaki } 135295a9a5baSYOSHIFUJI Hideaki 1353299d9939SYOSHIFUJI Hideaki return rt; 1354299d9939SYOSHIFUJI Hideaki } 1355299d9939SYOSHIFUJI Hideaki 1356db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1357d52d3997SMartin KaFai Lau { 1358db3fedeeSDavid Ahern struct fib6_info *f6i = res->f6i; 1359db3fedeeSDavid Ahern unsigned short flags = fib6_info_dst_flags(f6i); 13604832c30dSDavid Ahern struct net_device *dev; 1361d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1362d52d3997SMartin KaFai Lau 1363db3fedeeSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1364e873e4b9SWei Wang return NULL; 1365e873e4b9SWei Wang 13664832c30dSDavid Ahern rcu_read_lock(); 13670d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 1368d8882935SEric Dumazet pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT); 13694832c30dSDavid Ahern rcu_read_unlock(); 1370e873e4b9SWei Wang if (!pcpu_rt) { 1371db3fedeeSDavid Ahern fib6_info_release(f6i); 1372d52d3997SMartin KaFai Lau return NULL; 1373e873e4b9SWei Wang } 13740d161581SDavid Ahern ip6_rt_copy_init(pcpu_rt, res); 1375d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 13768f34e53bSDavid Ahern 13778f34e53bSDavid Ahern if (f6i->nh) 13788f34e53bSDavid Ahern pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev)); 13798f34e53bSDavid Ahern 1380d52d3997SMartin KaFai Lau return pcpu_rt; 1381d52d3997SMartin KaFai Lau } 1382d52d3997SMartin KaFai Lau 13838f34e53bSDavid Ahern static bool rt6_is_valid(const struct rt6_info *rt6) 13848f34e53bSDavid Ahern { 13858f34e53bSDavid Ahern return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev)); 13868f34e53bSDavid Ahern } 13878f34e53bSDavid Ahern 138866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1389db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1390d52d3997SMartin KaFai Lau { 1391c353071aSEric Dumazet struct rt6_info *pcpu_rt; 1392d52d3997SMartin KaFai Lau 1393c353071aSEric Dumazet pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); 1394d52d3997SMartin KaFai Lau 13958f34e53bSDavid Ahern if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) { 13968f34e53bSDavid Ahern struct rt6_info *prev, **p; 13978f34e53bSDavid Ahern 13988f34e53bSDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 13998f34e53bSDavid Ahern prev = xchg(p, NULL); 14008f34e53bSDavid Ahern if (prev) { 14018f34e53bSDavid Ahern dst_dev_put(&prev->dst); 14028f34e53bSDavid Ahern dst_release(&prev->dst); 14038f34e53bSDavid Ahern } 14048f34e53bSDavid Ahern 14058f34e53bSDavid Ahern pcpu_rt = NULL; 14068f34e53bSDavid Ahern } 14078f34e53bSDavid Ahern 1408a73e4195SMartin KaFai Lau return pcpu_rt; 1409a73e4195SMartin KaFai Lau } 1410a73e4195SMartin KaFai Lau 1411afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1412db3fedeeSDavid Ahern const struct fib6_result *res) 1413a73e4195SMartin KaFai Lau { 1414a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1415d52d3997SMartin KaFai Lau 1416db3fedeeSDavid Ahern pcpu_rt = ip6_rt_pcpu_alloc(res); 14170e09edccSWei Wang if (!pcpu_rt) 14180e09edccSWei Wang return NULL; 1419d52d3997SMartin KaFai Lau 1420f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1421d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1422951f788aSEric Dumazet BUG_ON(prev); 1423a94b9367SWei Wang 142461fb0d01SEric Dumazet if (res->f6i->fib6_destroying) { 142561fb0d01SEric Dumazet struct fib6_info *from; 142661fb0d01SEric Dumazet 142761fb0d01SEric Dumazet from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 142861fb0d01SEric Dumazet fib6_info_release(from); 142961fb0d01SEric Dumazet } 143061fb0d01SEric Dumazet 1431d52d3997SMartin KaFai Lau return pcpu_rt; 1432d52d3997SMartin KaFai Lau } 1433d52d3997SMartin KaFai Lau 143435732d01SWei Wang /* exception hash table implementation 143535732d01SWei Wang */ 143635732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 143735732d01SWei Wang 143835732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 143935732d01SWei Wang * Caller must hold rt6_exception_lock 144035732d01SWei Wang */ 144135732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 144235732d01SWei Wang struct rt6_exception *rt6_ex) 144335732d01SWei Wang { 1444f5b51fe8SPaolo Abeni struct fib6_info *from; 1445b2427e67SColin Ian King struct net *net; 144681eb8447SWei Wang 144735732d01SWei Wang if (!bucket || !rt6_ex) 144835732d01SWei Wang return; 1449b2427e67SColin Ian King 1450b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1451f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1452f5b51fe8SPaolo Abeni 1453f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1454f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1455f5b51fe8SPaolo Abeni */ 14560e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1457f5b51fe8SPaolo Abeni fib6_info_release(from); 1458f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1459f5b51fe8SPaolo Abeni 146035732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 146177634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 146235732d01SWei Wang kfree_rcu(rt6_ex, rcu); 146335732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 146435732d01SWei Wang bucket->depth--; 146535732d01SWei Wang } 146635732d01SWei Wang 146735732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 146835732d01SWei Wang * Caller must hold rt6_exception_lock 146935732d01SWei Wang */ 147035732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 147135732d01SWei Wang { 147235732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 147335732d01SWei Wang 147435732d01SWei Wang if (!bucket) 147535732d01SWei Wang return; 147635732d01SWei Wang 147735732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 147835732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 147935732d01SWei Wang oldest = rt6_ex; 148035732d01SWei Wang } 148135732d01SWei Wang rt6_remove_exception(bucket, oldest); 148235732d01SWei Wang } 148335732d01SWei Wang 148435732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 148535732d01SWei Wang const struct in6_addr *src) 148635732d01SWei Wang { 148749ecc2e9SEric Dumazet static siphash_aligned_key_t rt6_exception_key; 14884785305cSEric Dumazet struct { 14894785305cSEric Dumazet struct in6_addr dst; 14904785305cSEric Dumazet struct in6_addr src; 14914785305cSEric Dumazet } __aligned(SIPHASH_ALIGNMENT) combined = { 14924785305cSEric Dumazet .dst = *dst, 14934785305cSEric Dumazet }; 14944785305cSEric Dumazet u64 val; 149535732d01SWei Wang 14964785305cSEric Dumazet net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key)); 149735732d01SWei Wang 149835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 149935732d01SWei Wang if (src) 15004785305cSEric Dumazet combined.src = *src; 150135732d01SWei Wang #endif 15024785305cSEric Dumazet val = siphash(&combined, sizeof(combined), &rt6_exception_key); 15034785305cSEric Dumazet 15044785305cSEric Dumazet return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 150535732d01SWei Wang } 150635732d01SWei Wang 150735732d01SWei Wang /* Helper function to find the cached rt in the hash table 150835732d01SWei Wang * and update bucket pointer to point to the bucket for this 150935732d01SWei Wang * (daddr, saddr) pair 151035732d01SWei Wang * Caller must hold rt6_exception_lock 151135732d01SWei Wang */ 151235732d01SWei Wang static struct rt6_exception * 151335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 151435732d01SWei Wang const struct in6_addr *daddr, 151535732d01SWei Wang const struct in6_addr *saddr) 151635732d01SWei Wang { 151735732d01SWei Wang struct rt6_exception *rt6_ex; 151835732d01SWei Wang u32 hval; 151935732d01SWei Wang 152035732d01SWei Wang if (!(*bucket) || !daddr) 152135732d01SWei Wang return NULL; 152235732d01SWei Wang 152335732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 152435732d01SWei Wang *bucket += hval; 152535732d01SWei Wang 152635732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 152735732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 152835732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 152935732d01SWei Wang 153035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 153135732d01SWei Wang if (matched && saddr) 153235732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 153335732d01SWei Wang #endif 153435732d01SWei Wang if (matched) 153535732d01SWei Wang return rt6_ex; 153635732d01SWei Wang } 153735732d01SWei Wang return NULL; 153835732d01SWei Wang } 153935732d01SWei Wang 154035732d01SWei Wang /* Helper function to find the cached rt in the hash table 154135732d01SWei Wang * and update bucket pointer to point to the bucket for this 154235732d01SWei Wang * (daddr, saddr) pair 154335732d01SWei Wang * Caller must hold rcu_read_lock() 154435732d01SWei Wang */ 154535732d01SWei Wang static struct rt6_exception * 154635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 154735732d01SWei Wang const struct in6_addr *daddr, 154835732d01SWei Wang const struct in6_addr *saddr) 154935732d01SWei Wang { 155035732d01SWei Wang struct rt6_exception *rt6_ex; 155135732d01SWei Wang u32 hval; 155235732d01SWei Wang 155335732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 155435732d01SWei Wang 155535732d01SWei Wang if (!(*bucket) || !daddr) 155635732d01SWei Wang return NULL; 155735732d01SWei Wang 155835732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 155935732d01SWei Wang *bucket += hval; 156035732d01SWei Wang 156135732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 156235732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 156335732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 156435732d01SWei Wang 156535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 156635732d01SWei Wang if (matched && saddr) 156735732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 156835732d01SWei Wang #endif 156935732d01SWei Wang if (matched) 157035732d01SWei Wang return rt6_ex; 157135732d01SWei Wang } 157235732d01SWei Wang return NULL; 157335732d01SWei Wang } 157435732d01SWei Wang 1575b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res) 157635732d01SWei Wang { 1577b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 1578d4ead6b3SDavid Ahern unsigned int mtu; 1579d4ead6b3SDavid Ahern 1580b748f260SDavid Ahern if (res->f6i->fib6_pmtu) { 1581b748f260SDavid Ahern mtu = res->f6i->fib6_pmtu; 1582dcd1f572SDavid Ahern } else { 1583b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 1584dcd1f572SDavid Ahern struct inet6_dev *idev; 1585dcd1f572SDavid Ahern 1586dcd1f572SDavid Ahern rcu_read_lock(); 1587dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1588dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1589dcd1f572SDavid Ahern rcu_read_unlock(); 1590dcd1f572SDavid Ahern } 1591dcd1f572SDavid Ahern 1592d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1593d4ead6b3SDavid Ahern 1594b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1595d4ead6b3SDavid Ahern } 1596d4ead6b3SDavid Ahern 1597cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL 1598cc5c073aSDavid Ahern 1599cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket 1600cc5c073aSDavid Ahern * (ie., all bucket users except rt6_insert_exception); 1601cc5c073aSDavid Ahern * 1602cc5c073aSDavid Ahern * called under rcu lock; sometimes called with rt6_exception_lock held 1603cc5c073aSDavid Ahern */ 1604cc5c073aSDavid Ahern static 1605cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, 1606cc5c073aSDavid Ahern spinlock_t *lock) 1607cc5c073aSDavid Ahern { 1608cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1609cc5c073aSDavid Ahern 1610cc5c073aSDavid Ahern if (lock) 1611cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1612cc5c073aSDavid Ahern lockdep_is_held(lock)); 1613cc5c073aSDavid Ahern else 1614cc5c073aSDavid Ahern bucket = rcu_dereference(nh->rt6i_exception_bucket); 1615cc5c073aSDavid Ahern 1616cc5c073aSDavid Ahern /* remove bucket flushed bit if set */ 1617cc5c073aSDavid Ahern if (bucket) { 1618cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1619cc5c073aSDavid Ahern 1620cc5c073aSDavid Ahern p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; 1621cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1622cc5c073aSDavid Ahern } 1623cc5c073aSDavid Ahern 1624cc5c073aSDavid Ahern return bucket; 1625cc5c073aSDavid Ahern } 1626cc5c073aSDavid Ahern 1627cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) 1628cc5c073aSDavid Ahern { 1629cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1630cc5c073aSDavid Ahern 1631cc5c073aSDavid Ahern return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); 1632cc5c073aSDavid Ahern } 1633cc5c073aSDavid Ahern 1634cc5c073aSDavid Ahern /* called with rt6_exception_lock held */ 1635cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, 1636cc5c073aSDavid Ahern spinlock_t *lock) 1637cc5c073aSDavid Ahern { 1638cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1639cc5c073aSDavid Ahern unsigned long p; 1640cc5c073aSDavid Ahern 1641cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1642cc5c073aSDavid Ahern lockdep_is_held(lock)); 1643cc5c073aSDavid Ahern 1644cc5c073aSDavid Ahern p = (unsigned long)bucket; 1645cc5c073aSDavid Ahern p |= FIB6_EXCEPTION_BUCKET_FLUSHED; 1646cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1647cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1648cc5c073aSDavid Ahern } 1649cc5c073aSDavid Ahern 165035732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 16515012f0a5SDavid Ahern const struct fib6_result *res) 165235732d01SWei Wang { 16535e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 165435732d01SWei Wang struct rt6_exception_bucket *bucket; 1655cc5c073aSDavid Ahern struct fib6_info *f6i = res->f6i; 165635732d01SWei Wang struct in6_addr *src_key = NULL; 165735732d01SWei Wang struct rt6_exception *rt6_ex; 1658cc5c073aSDavid Ahern struct fib6_nh *nh = res->nh; 1659a00df2caSEric Dumazet int max_depth; 166035732d01SWei Wang int err = 0; 166135732d01SWei Wang 166235732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 166335732d01SWei Wang 1664cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 166535732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 166635732d01SWei Wang if (!bucket) { 166735732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 166835732d01SWei Wang GFP_ATOMIC); 166935732d01SWei Wang if (!bucket) { 167035732d01SWei Wang err = -ENOMEM; 167135732d01SWei Wang goto out; 167235732d01SWei Wang } 1673cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1674cc5c073aSDavid Ahern } else if (fib6_nh_excptn_bucket_flushed(bucket)) { 1675cc5c073aSDavid Ahern err = -EINVAL; 1676cc5c073aSDavid Ahern goto out; 167735732d01SWei Wang } 167835732d01SWei Wang 167935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 16805012f0a5SDavid Ahern /* fib6_src.plen != 0 indicates f6i is in subtree 168135732d01SWei Wang * and exception table is indexed by a hash of 16825012f0a5SDavid Ahern * both fib6_dst and fib6_src. 168335732d01SWei Wang * Otherwise, the exception table is indexed by 16845012f0a5SDavid Ahern * a hash of only fib6_dst. 168535732d01SWei Wang */ 16865012f0a5SDavid Ahern if (f6i->fib6_src.plen) 168735732d01SWei Wang src_key = &nrt->rt6i_src.addr; 168835732d01SWei Wang #endif 16895012f0a5SDavid Ahern /* rt6_mtu_change() might lower mtu on f6i. 1690f5bbe7eeSWei Wang * Only insert this exception route if its mtu 16915012f0a5SDavid Ahern * is less than f6i's mtu value. 1692f5bbe7eeSWei Wang */ 1693b748f260SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1694f5bbe7eeSWei Wang err = -EINVAL; 1695f5bbe7eeSWei Wang goto out; 1696f5bbe7eeSWei Wang } 169760006a48SWei Wang 169835732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 169935732d01SWei Wang src_key); 170035732d01SWei Wang if (rt6_ex) 170135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 170235732d01SWei Wang 170335732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 170435732d01SWei Wang if (!rt6_ex) { 170535732d01SWei Wang err = -ENOMEM; 170635732d01SWei Wang goto out; 170735732d01SWei Wang } 170835732d01SWei Wang rt6_ex->rt6i = nrt; 170935732d01SWei Wang rt6_ex->stamp = jiffies; 171035732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 171135732d01SWei Wang bucket->depth++; 171281eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 171335732d01SWei Wang 1714a00df2caSEric Dumazet /* Randomize max depth to avoid some side channels attacks. */ 17158032bf12SJason A. Donenfeld max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH); 1716a00df2caSEric Dumazet while (bucket->depth > max_depth) 171735732d01SWei Wang rt6_exception_remove_oldest(bucket); 171835732d01SWei Wang 171935732d01SWei Wang out: 172035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 172135732d01SWei Wang 172235732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1723b886d5f2SPaolo Abeni if (!err) { 17245012f0a5SDavid Ahern spin_lock_bh(&f6i->fib6_table->tb6_lock); 17255012f0a5SDavid Ahern fib6_update_sernum(net, f6i); 17265012f0a5SDavid Ahern spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1727b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1728b886d5f2SPaolo Abeni } 172935732d01SWei Wang 173035732d01SWei Wang return err; 173135732d01SWei Wang } 173235732d01SWei Wang 1733c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) 173435732d01SWei Wang { 173535732d01SWei Wang struct rt6_exception_bucket *bucket; 173635732d01SWei Wang struct rt6_exception *rt6_ex; 173735732d01SWei Wang struct hlist_node *tmp; 173835732d01SWei Wang int i; 173935732d01SWei Wang 174035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 174135732d01SWei Wang 1742cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 174335732d01SWei Wang if (!bucket) 174435732d01SWei Wang goto out; 174535732d01SWei Wang 1746cc5c073aSDavid Ahern /* Prevent rt6_insert_exception() to recreate the bucket list */ 1747cc5c073aSDavid Ahern if (!from) 1748cc5c073aSDavid Ahern fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); 1749cc5c073aSDavid Ahern 175035732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1751cc5c073aSDavid Ahern hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { 1752cc5c073aSDavid Ahern if (!from || 1753cc5c073aSDavid Ahern rcu_access_pointer(rt6_ex->rt6i->from) == from) 175435732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1755cc5c073aSDavid Ahern } 1756cc5c073aSDavid Ahern WARN_ON_ONCE(!from && bucket->depth); 175735732d01SWei Wang bucket++; 175835732d01SWei Wang } 175935732d01SWei Wang out: 176035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 176135732d01SWei Wang } 176235732d01SWei Wang 1763e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) 1764e659ba31SDavid Ahern { 1765e659ba31SDavid Ahern struct fib6_info *f6i = arg; 1766e659ba31SDavid Ahern 1767e659ba31SDavid Ahern fib6_nh_flush_exceptions(nh, f6i); 1768e659ba31SDavid Ahern 1769e659ba31SDavid Ahern return 0; 1770e659ba31SDavid Ahern } 1771e659ba31SDavid Ahern 1772c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i) 1773c0b220cfSDavid Ahern { 1774e659ba31SDavid Ahern if (f6i->nh) 1775e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, 1776e659ba31SDavid Ahern f6i); 1777e659ba31SDavid Ahern else 17781cf844c7SDavid Ahern fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); 1779c0b220cfSDavid Ahern } 1780c0b220cfSDavid Ahern 178135732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 178235732d01SWei Wang * Caller has to hold rcu_read_lock() 178335732d01SWei Wang */ 17847e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1785510e2cedSWei Wang const struct in6_addr *daddr, 1786510e2cedSWei Wang const struct in6_addr *saddr) 178735732d01SWei Wang { 1788510e2cedSWei Wang const struct in6_addr *src_key = NULL; 178935732d01SWei Wang struct rt6_exception_bucket *bucket; 179035732d01SWei Wang struct rt6_exception *rt6_ex; 17917e4b5128SDavid Ahern struct rt6_info *ret = NULL; 179235732d01SWei Wang 179335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 17947e4b5128SDavid Ahern /* fib6i_src.plen != 0 indicates f6i is in subtree 179535732d01SWei Wang * and exception table is indexed by a hash of 17967e4b5128SDavid Ahern * both fib6_dst and fib6_src. 1797510e2cedSWei Wang * However, the src addr used to create the hash 1798510e2cedSWei Wang * might not be exactly the passed in saddr which 1799510e2cedSWei Wang * is a /128 addr from the flow. 1800510e2cedSWei Wang * So we need to use f6i->fib6_src to redo lookup 1801510e2cedSWei Wang * if the passed in saddr does not find anything. 1802510e2cedSWei Wang * (See the logic in ip6_rt_cache_alloc() on how 1803510e2cedSWei Wang * rt->rt6i_src is updated.) 180435732d01SWei Wang */ 18057e4b5128SDavid Ahern if (res->f6i->fib6_src.plen) 180635732d01SWei Wang src_key = saddr; 1807510e2cedSWei Wang find_ex: 180835732d01SWei Wang #endif 1809cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); 181035732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 181135732d01SWei Wang 181235732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 18137e4b5128SDavid Ahern ret = rt6_ex->rt6i; 181435732d01SWei Wang 1815510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES 1816510e2cedSWei Wang /* Use fib6_src as src_key and redo lookup */ 1817510e2cedSWei Wang if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1818510e2cedSWei Wang src_key = &res->f6i->fib6_src.addr; 1819510e2cedSWei Wang goto find_ex; 1820510e2cedSWei Wang } 1821510e2cedSWei Wang #endif 1822510e2cedSWei Wang 18237e4b5128SDavid Ahern return ret; 182435732d01SWei Wang } 182535732d01SWei Wang 182635732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 1827cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, 1828c0b220cfSDavid Ahern const struct rt6_info *rt) 182935732d01SWei Wang { 1830c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 183135732d01SWei Wang struct rt6_exception_bucket *bucket; 183235732d01SWei Wang struct rt6_exception *rt6_ex; 183335732d01SWei Wang int err; 183435732d01SWei Wang 1835cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 183635732d01SWei Wang return -ENOENT; 183735732d01SWei Wang 183835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1839cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1840cc5c073aSDavid Ahern 184135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1842cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1843cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1844cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 184535732d01SWei Wang * Otherwise, the exception table is indexed by 184635732d01SWei Wang * a hash of only rt6i_dst. 184735732d01SWei Wang */ 1848c0b220cfSDavid Ahern if (plen) 184935732d01SWei Wang src_key = &rt->rt6i_src.addr; 185035732d01SWei Wang #endif 185135732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 185235732d01SWei Wang &rt->rt6i_dst.addr, 185335732d01SWei Wang src_key); 185435732d01SWei Wang if (rt6_ex) { 185535732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 185635732d01SWei Wang err = 0; 185735732d01SWei Wang } else { 185835732d01SWei Wang err = -ENOENT; 185935732d01SWei Wang } 186035732d01SWei Wang 186135732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 186235732d01SWei Wang return err; 186335732d01SWei Wang } 186435732d01SWei Wang 1865e659ba31SDavid Ahern struct fib6_nh_excptn_arg { 1866e659ba31SDavid Ahern struct rt6_info *rt; 1867e659ba31SDavid Ahern int plen; 1868e659ba31SDavid Ahern }; 1869e659ba31SDavid Ahern 1870e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) 1871e659ba31SDavid Ahern { 1872e659ba31SDavid Ahern struct fib6_nh_excptn_arg *arg = _arg; 1873e659ba31SDavid Ahern int err; 1874e659ba31SDavid Ahern 1875e659ba31SDavid Ahern err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); 1876e659ba31SDavid Ahern if (err == 0) 1877e659ba31SDavid Ahern return 1; 1878e659ba31SDavid Ahern 1879e659ba31SDavid Ahern return 0; 1880e659ba31SDavid Ahern } 1881e659ba31SDavid Ahern 1882c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 1883c0b220cfSDavid Ahern { 1884c0b220cfSDavid Ahern struct fib6_info *from; 1885c0b220cfSDavid Ahern 1886c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1887cc5c073aSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1888c0b220cfSDavid Ahern return -EINVAL; 1889c0b220cfSDavid Ahern 1890e659ba31SDavid Ahern if (from->nh) { 1891e659ba31SDavid Ahern struct fib6_nh_excptn_arg arg = { 1892e659ba31SDavid Ahern .rt = rt, 1893e659ba31SDavid Ahern .plen = from->fib6_src.plen 1894e659ba31SDavid Ahern }; 1895e659ba31SDavid Ahern int rc; 1896e659ba31SDavid Ahern 1897e659ba31SDavid Ahern /* rc = 1 means an entry was found */ 1898e659ba31SDavid Ahern rc = nexthop_for_each_fib6_nh(from->nh, 1899e659ba31SDavid Ahern rt6_nh_remove_exception_rt, 1900e659ba31SDavid Ahern &arg); 1901e659ba31SDavid Ahern return rc ? 0 : -ENOENT; 1902e659ba31SDavid Ahern } 1903e659ba31SDavid Ahern 19041cf844c7SDavid Ahern return fib6_nh_remove_exception(from->fib6_nh, 1905cc5c073aSDavid Ahern from->fib6_src.plen, rt); 1906c0b220cfSDavid Ahern } 1907c0b220cfSDavid Ahern 190835732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 190935732d01SWei Wang * refresh its stamp 191035732d01SWei Wang */ 1911cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, 1912c0b220cfSDavid Ahern const struct rt6_info *rt) 191335732d01SWei Wang { 1914c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 191535732d01SWei Wang struct rt6_exception_bucket *bucket; 191635732d01SWei Wang struct rt6_exception *rt6_ex; 1917193f3685SPaolo Abeni 1918cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, NULL); 191935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1920cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1921cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1922cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 192335732d01SWei Wang * Otherwise, the exception table is indexed by 192435732d01SWei Wang * a hash of only rt6i_dst. 192535732d01SWei Wang */ 1926c0b220cfSDavid Ahern if (plen) 192735732d01SWei Wang src_key = &rt->rt6i_src.addr; 192835732d01SWei Wang #endif 1929cc5c073aSDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); 193035732d01SWei Wang if (rt6_ex) 193135732d01SWei Wang rt6_ex->stamp = jiffies; 1932c0b220cfSDavid Ahern } 193335732d01SWei Wang 1934e659ba31SDavid Ahern struct fib6_nh_match_arg { 1935e659ba31SDavid Ahern const struct net_device *dev; 1936e659ba31SDavid Ahern const struct in6_addr *gw; 1937e659ba31SDavid Ahern struct fib6_nh *match; 1938e659ba31SDavid Ahern }; 1939e659ba31SDavid Ahern 1940e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */ 1941e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) 1942e659ba31SDavid Ahern { 1943e659ba31SDavid Ahern struct fib6_nh_match_arg *arg = _arg; 1944e659ba31SDavid Ahern 1945e659ba31SDavid Ahern if (arg->dev != nh->fib_nh_dev || 1946e659ba31SDavid Ahern (arg->gw && !nh->fib_nh_gw_family) || 1947e659ba31SDavid Ahern (!arg->gw && nh->fib_nh_gw_family) || 1948e659ba31SDavid Ahern (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) 1949e659ba31SDavid Ahern return 0; 1950e659ba31SDavid Ahern 1951e659ba31SDavid Ahern arg->match = nh; 1952e659ba31SDavid Ahern 1953e659ba31SDavid Ahern /* found a match, break the loop */ 1954e659ba31SDavid Ahern return 1; 1955e659ba31SDavid Ahern } 1956e659ba31SDavid Ahern 1957c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 1958c0b220cfSDavid Ahern { 1959c0b220cfSDavid Ahern struct fib6_info *from; 1960e659ba31SDavid Ahern struct fib6_nh *fib6_nh; 1961c0b220cfSDavid Ahern 1962c0b220cfSDavid Ahern rcu_read_lock(); 1963c0b220cfSDavid Ahern 1964c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1965c0b220cfSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1966c0b220cfSDavid Ahern goto unlock; 1967c0b220cfSDavid Ahern 1968e659ba31SDavid Ahern if (from->nh) { 1969e659ba31SDavid Ahern struct fib6_nh_match_arg arg = { 1970e659ba31SDavid Ahern .dev = rt->dst.dev, 1971e659ba31SDavid Ahern .gw = &rt->rt6i_gateway, 1972e659ba31SDavid Ahern }; 1973e659ba31SDavid Ahern 1974e659ba31SDavid Ahern nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); 1975e659ba31SDavid Ahern 1976e659ba31SDavid Ahern if (!arg.match) 1977cff6a327SDavid Ahern goto unlock; 1978e659ba31SDavid Ahern fib6_nh = arg.match; 1979e659ba31SDavid Ahern } else { 1980e659ba31SDavid Ahern fib6_nh = from->fib6_nh; 1981e659ba31SDavid Ahern } 1982e659ba31SDavid Ahern fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); 1983193f3685SPaolo Abeni unlock: 198435732d01SWei Wang rcu_read_unlock(); 198535732d01SWei Wang } 198635732d01SWei Wang 1987e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1988e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1989e9fa1495SStefano Brivio { 1990e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1991e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1992e9fa1495SStefano Brivio * reflect PMTU decreases. 1993e9fa1495SStefano Brivio * 1994e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1995e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1996e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1997e9fa1495SStefano Brivio * handle this. 1998e9fa1495SStefano Brivio */ 1999e9fa1495SStefano Brivio 2000e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 2001e9fa1495SStefano Brivio return true; 2002e9fa1495SStefano Brivio 2003e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 2004e9fa1495SStefano Brivio return true; 2005e9fa1495SStefano Brivio 2006e9fa1495SStefano Brivio return false; 2007e9fa1495SStefano Brivio } 2008e9fa1495SStefano Brivio 2009e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 2010cc5c073aSDavid Ahern const struct fib6_nh *nh, int mtu) 2011f5bbe7eeSWei Wang { 2012f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 2013f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 2014f5bbe7eeSWei Wang int i; 2015f5bbe7eeSWei Wang 2016cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2017e9fa1495SStefano Brivio if (!bucket) 2018e9fa1495SStefano Brivio return; 2019e9fa1495SStefano Brivio 2020f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2021f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 2022f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2023e9fa1495SStefano Brivio 2024e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 2025d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 2026f5bbe7eeSWei Wang * been updated. 2027f5bbe7eeSWei Wang */ 2028d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 2029e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 2030d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 2031f5bbe7eeSWei Wang } 2032f5bbe7eeSWei Wang bucket++; 2033f5bbe7eeSWei Wang } 2034f5bbe7eeSWei Wang } 2035f5bbe7eeSWei Wang 2036b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2037b16cb459SWei Wang 2038cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, 2039cc5c073aSDavid Ahern const struct in6_addr *gateway) 2040b16cb459SWei Wang { 2041b16cb459SWei Wang struct rt6_exception_bucket *bucket; 2042b16cb459SWei Wang struct rt6_exception *rt6_ex; 2043b16cb459SWei Wang struct hlist_node *tmp; 2044b16cb459SWei Wang int i; 2045b16cb459SWei Wang 2046cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2047b16cb459SWei Wang return; 2048b16cb459SWei Wang 2049b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 2050cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2051b16cb459SWei Wang if (bucket) { 2052b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2053b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2054b16cb459SWei Wang &bucket->chain, hlist) { 2055b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2056b16cb459SWei Wang 2057b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 2058b16cb459SWei Wang RTF_CACHE_GATEWAY && 2059b16cb459SWei Wang ipv6_addr_equal(gateway, 2060b16cb459SWei Wang &entry->rt6i_gateway)) { 2061b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 2062b16cb459SWei Wang } 2063b16cb459SWei Wang } 2064b16cb459SWei Wang bucket++; 2065b16cb459SWei Wang } 2066b16cb459SWei Wang } 2067b16cb459SWei Wang 2068b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 2069b16cb459SWei Wang } 2070b16cb459SWei Wang 2071c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 2072c757faa8SWei Wang struct rt6_exception *rt6_ex, 2073c757faa8SWei Wang struct fib6_gc_args *gc_args, 2074c757faa8SWei Wang unsigned long now) 2075c757faa8SWei Wang { 2076c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 2077c757faa8SWei Wang 20781859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 20791859bac0SPaolo Abeni * even if others have still references to them, so that on next 20801859bac0SPaolo Abeni * dst_check() such references can be dropped. 20811859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 20821859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 20831859bac0SPaolo Abeni */ 208431afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 208531afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 2086c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 2087c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2088c757faa8SWei Wang return; 208931afeb42SWei Wang } 209031afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 209131afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 209231afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 209331afeb42SWei Wang return; 209431afeb42SWei Wang } 209531afeb42SWei Wang 209631afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 2097c757faa8SWei Wang struct neighbour *neigh; 2098c757faa8SWei Wang 20991bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 21001bfa26ffSEric Dumazet 2101b7a320c3SXu Jia if (!(neigh && (neigh->flags & NTF_ROUTER))) { 2102c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 2103c757faa8SWei Wang rt); 2104c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2105c757faa8SWei Wang return; 2106c757faa8SWei Wang } 2107c757faa8SWei Wang } 210831afeb42SWei Wang 2109c757faa8SWei Wang gc_args->more++; 2110c757faa8SWei Wang } 2111c757faa8SWei Wang 2112cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh, 2113c757faa8SWei Wang struct fib6_gc_args *gc_args, 2114c757faa8SWei Wang unsigned long now) 2115c757faa8SWei Wang { 2116c757faa8SWei Wang struct rt6_exception_bucket *bucket; 2117c757faa8SWei Wang struct rt6_exception *rt6_ex; 2118c757faa8SWei Wang struct hlist_node *tmp; 2119c757faa8SWei Wang int i; 2120c757faa8SWei Wang 2121cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2122c757faa8SWei Wang return; 2123c757faa8SWei Wang 21241bfa26ffSEric Dumazet rcu_read_lock_bh(); 21251bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 2126cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2127c757faa8SWei Wang if (bucket) { 2128c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2129c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2130c757faa8SWei Wang &bucket->chain, hlist) { 2131c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 2132c757faa8SWei Wang gc_args, now); 2133c757faa8SWei Wang } 2134c757faa8SWei Wang bucket++; 2135c757faa8SWei Wang } 2136c757faa8SWei Wang } 21371bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 21381bfa26ffSEric Dumazet rcu_read_unlock_bh(); 2139c757faa8SWei Wang } 2140c757faa8SWei Wang 2141e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg { 2142e659ba31SDavid Ahern struct fib6_gc_args *gc_args; 2143e659ba31SDavid Ahern unsigned long now; 2144e659ba31SDavid Ahern }; 2145e659ba31SDavid Ahern 2146e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) 2147e659ba31SDavid Ahern { 2148e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg *arg = _arg; 2149e659ba31SDavid Ahern 2150e659ba31SDavid Ahern fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); 2151e659ba31SDavid Ahern return 0; 2152e659ba31SDavid Ahern } 2153e659ba31SDavid Ahern 2154cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i, 2155c0b220cfSDavid Ahern struct fib6_gc_args *gc_args, 2156c0b220cfSDavid Ahern unsigned long now) 2157c0b220cfSDavid Ahern { 2158e659ba31SDavid Ahern if (f6i->nh) { 2159e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg arg = { 2160e659ba31SDavid Ahern .gc_args = gc_args, 2161e659ba31SDavid Ahern .now = now 2162e659ba31SDavid Ahern }; 2163e659ba31SDavid Ahern 2164e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, 2165e659ba31SDavid Ahern &arg); 2166e659ba31SDavid Ahern } else { 21671cf844c7SDavid Ahern fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); 2168c0b220cfSDavid Ahern } 2169e659ba31SDavid Ahern } 2170c0b220cfSDavid Ahern 21711d053da9SDavid Ahern /* must be called with rcu lock held */ 2172effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 2173effda4ddSDavid Ahern struct flowi6 *fl6, struct fib6_result *res, int strict) 21741da177e4SLinus Torvalds { 2175367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 21761da177e4SLinus Torvalds 21776454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2178367efcb9SMartin KaFai Lau saved_fn = fn; 21791da177e4SLinus Torvalds 2180a3c00e46SMartin KaFai Lau redo_rt6_select: 2181effda4ddSDavid Ahern rt6_select(net, fn, oif, res, strict); 2182effda4ddSDavid Ahern if (res->f6i == net->ipv6.fib6_null_entry) { 2183a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2184a3c00e46SMartin KaFai Lau if (fn) 2185a3c00e46SMartin KaFai Lau goto redo_rt6_select; 2186367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 2187367efcb9SMartin KaFai Lau /* also consider unreachable route */ 2188367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 2189367efcb9SMartin KaFai Lau fn = saved_fn; 2190367efcb9SMartin KaFai Lau goto redo_rt6_select; 2191367efcb9SMartin KaFai Lau } 2192a3c00e46SMartin KaFai Lau } 2193a3c00e46SMartin KaFai Lau 2194effda4ddSDavid Ahern trace_fib6_table_lookup(net, res, table, fl6); 2195d52d3997SMartin KaFai Lau 2196effda4ddSDavid Ahern return 0; 21971d053da9SDavid Ahern } 21981d053da9SDavid Ahern 21991d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 22001d053da9SDavid Ahern int oif, struct flowi6 *fl6, 22011d053da9SDavid Ahern const struct sk_buff *skb, int flags) 22021d053da9SDavid Ahern { 2203b1d40991SDavid Ahern struct fib6_result res = {}; 22040e09edccSWei Wang struct rt6_info *rt = NULL; 22051d053da9SDavid Ahern int strict = 0; 22061d053da9SDavid Ahern 22070e09edccSWei Wang WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && 22080e09edccSWei Wang !rcu_read_lock_held()); 22090e09edccSWei Wang 22101d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 22111d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 22121d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 22131d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 22141d053da9SDavid Ahern 22151d053da9SDavid Ahern rcu_read_lock(); 22161d053da9SDavid Ahern 2217effda4ddSDavid Ahern fib6_table_lookup(net, table, oif, fl6, &res, strict); 22180e09edccSWei Wang if (res.f6i == net->ipv6.fib6_null_entry) 22190e09edccSWei Wang goto out; 222023fb93a4SDavid Ahern 2221b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, oif, false, skb, strict); 2222d83009d4SDavid Ahern 222323fb93a4SDavid Ahern /*Search through exception table */ 22247e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 222523fb93a4SDavid Ahern if (rt) { 22260e09edccSWei Wang goto out; 22273da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 2228b1d40991SDavid Ahern !res.nh->fib_nh_gw_family)) { 22293da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 22303da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 22313da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 22323da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 22333da59bd9SMartin KaFai Lau */ 22340e09edccSWei Wang rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 22353da59bd9SMartin KaFai Lau 22360e09edccSWei Wang if (rt) { 22370e09edccSWei Wang /* 1 refcnt is taken during ip6_rt_cache_alloc(). 22380e09edccSWei Wang * As rt6_uncached_list_add() does not consume refcnt, 22390e09edccSWei Wang * this refcnt is always returned to the caller even 22400e09edccSWei Wang * if caller sets RT6_LOOKUP_F_DST_NOREF flag. 22410e09edccSWei Wang */ 22420e09edccSWei Wang rt6_uncached_list_add(rt); 22434d85cd0cSDavid Ahern rcu_read_unlock(); 22443da59bd9SMartin KaFai Lau 22450e09edccSWei Wang return rt; 22461cfb71eeSWei Wang } 2247d52d3997SMartin KaFai Lau } else { 2248d52d3997SMartin KaFai Lau /* Get a percpu copy */ 2249951f788aSEric Dumazet local_bh_disable(); 22500e09edccSWei Wang rt = rt6_get_pcpu_route(&res); 2251d52d3997SMartin KaFai Lau 22520e09edccSWei Wang if (!rt) 22530e09edccSWei Wang rt = rt6_make_pcpu_route(net, &res); 225493531c67SDavid Ahern 2255951f788aSEric Dumazet local_bh_enable(); 22560e09edccSWei Wang } 22570e09edccSWei Wang out: 22580e09edccSWei Wang if (!rt) 22590e09edccSWei Wang rt = net->ipv6.ip6_null_entry; 22600e09edccSWei Wang if (!(flags & RT6_LOOKUP_F_DST_NOREF)) 22610e09edccSWei Wang ip6_hold_safe(net, &rt); 2262951f788aSEric Dumazet rcu_read_unlock(); 2263d4bea421SDavid Ahern 22640e09edccSWei Wang return rt; 2265c71099acSThomas Graf } 22669ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 2267c71099acSThomas Graf 226855cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net, 2269b75cc8f9SDavid Ahern struct fib6_table *table, 2270b75cc8f9SDavid Ahern struct flowi6 *fl6, 2271b75cc8f9SDavid Ahern const struct sk_buff *skb, 2272b75cc8f9SDavid Ahern int flags) 22734acad72dSPavel Emelyanov { 2274b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 22754acad72dSPavel Emelyanov } 22764acad72dSPavel Emelyanov 2277d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 227872331bc0SShmulik Ladkani struct net_device *dev, 2279b75cc8f9SDavid Ahern struct flowi6 *fl6, 2280b75cc8f9SDavid Ahern const struct sk_buff *skb, 2281b75cc8f9SDavid Ahern int flags) 228272331bc0SShmulik Ladkani { 228372331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 228472331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 228572331bc0SShmulik Ladkani 2286b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 228772331bc0SShmulik Ladkani } 2288d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 228972331bc0SShmulik Ladkani 229023aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 22915e5d6fedSRoopa Prabhu struct flow_keys *keys, 22925e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 229323aebdacSJakub Sitnicki { 229423aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 229523aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 22965e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 229723aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 229823aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 229923aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 2300cea67a2dSEric Dumazet struct icmp6hdr _icmph; 230123aebdacSJakub Sitnicki 230223aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 230323aebdacSJakub Sitnicki goto out; 230423aebdacSJakub Sitnicki 2305cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2306cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 2307cea67a2dSEric Dumazet if (!icmph) 2308cea67a2dSEric Dumazet goto out; 2309cea67a2dSEric Dumazet 231054074f1dSMatteo Croce if (!icmpv6_is_err(icmph->icmp6_type)) 231123aebdacSJakub Sitnicki goto out; 231223aebdacSJakub Sitnicki 231323aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 231423aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 231523aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 231623aebdacSJakub Sitnicki if (!inner_iph) 231723aebdacSJakub Sitnicki goto out; 231823aebdacSJakub Sitnicki 231923aebdacSJakub Sitnicki key_iph = inner_iph; 23205e5d6fedSRoopa Prabhu _flkeys = NULL; 232123aebdacSJakub Sitnicki out: 23225e5d6fedSRoopa Prabhu if (_flkeys) { 23235e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 23245e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 23255e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 23265e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 23275e5d6fedSRoopa Prabhu } else { 232823aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 232923aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 2330fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 233123aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 233223aebdacSJakub Sitnicki } 23335e5d6fedSRoopa Prabhu } 233423aebdacSJakub Sitnicki 233573c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_outer(const struct net *net, 233673c2c5cbSIdo Schimmel const struct sk_buff *skb, 233773c2c5cbSIdo Schimmel bool *p_has_inner) 233873c2c5cbSIdo Schimmel { 233973c2c5cbSIdo Schimmel u32 hash_fields = ip6_multipath_hash_fields(net); 234073c2c5cbSIdo Schimmel struct flow_keys keys, hash_keys; 234173c2c5cbSIdo Schimmel 234273c2c5cbSIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 234373c2c5cbSIdo Schimmel return 0; 234473c2c5cbSIdo Schimmel 234573c2c5cbSIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 234673c2c5cbSIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP); 234773c2c5cbSIdo Schimmel 234873c2c5cbSIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 234973c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 235073c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 235173c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 235273c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 235373c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 235473c2c5cbSIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto; 235573c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL) 235673c2c5cbSIdo Schimmel hash_keys.tags.flow_label = keys.tags.flow_label; 235773c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 235873c2c5cbSIdo Schimmel hash_keys.ports.src = keys.ports.src; 235973c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 236073c2c5cbSIdo Schimmel hash_keys.ports.dst = keys.ports.dst; 236173c2c5cbSIdo Schimmel 236273c2c5cbSIdo Schimmel *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION); 236373c2c5cbSIdo Schimmel return flow_hash_from_keys(&hash_keys); 236473c2c5cbSIdo Schimmel } 236573c2c5cbSIdo Schimmel 236673c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_inner(const struct net *net, 236773c2c5cbSIdo Schimmel const struct sk_buff *skb, 236873c2c5cbSIdo Schimmel bool has_inner) 236973c2c5cbSIdo Schimmel { 237073c2c5cbSIdo Schimmel u32 hash_fields = ip6_multipath_hash_fields(net); 237173c2c5cbSIdo Schimmel struct flow_keys keys, hash_keys; 237273c2c5cbSIdo Schimmel 237373c2c5cbSIdo Schimmel /* We assume the packet carries an encapsulation, but if none was 237473c2c5cbSIdo Schimmel * encountered during dissection of the outer flow, then there is no 237573c2c5cbSIdo Schimmel * point in calling the flow dissector again. 237673c2c5cbSIdo Schimmel */ 237773c2c5cbSIdo Schimmel if (!has_inner) 237873c2c5cbSIdo Schimmel return 0; 237973c2c5cbSIdo Schimmel 238073c2c5cbSIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK)) 238173c2c5cbSIdo Schimmel return 0; 238273c2c5cbSIdo Schimmel 238373c2c5cbSIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 238473c2c5cbSIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, 0); 238573c2c5cbSIdo Schimmel 238673c2c5cbSIdo Schimmel if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION)) 238773c2c5cbSIdo Schimmel return 0; 238873c2c5cbSIdo Schimmel 238973c2c5cbSIdo Schimmel if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 239073c2c5cbSIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 239173c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 239273c2c5cbSIdo Schimmel hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 239373c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 239473c2c5cbSIdo Schimmel hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 239573c2c5cbSIdo Schimmel } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 239673c2c5cbSIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 239773c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 239873c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 239973c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 240073c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 240173c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL) 240273c2c5cbSIdo Schimmel hash_keys.tags.flow_label = keys.tags.flow_label; 240373c2c5cbSIdo Schimmel } 240473c2c5cbSIdo Schimmel 240573c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO) 240673c2c5cbSIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto; 240773c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT) 240873c2c5cbSIdo Schimmel hash_keys.ports.src = keys.ports.src; 240973c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT) 241073c2c5cbSIdo Schimmel hash_keys.ports.dst = keys.ports.dst; 241173c2c5cbSIdo Schimmel 241273c2c5cbSIdo Schimmel return flow_hash_from_keys(&hash_keys); 241373c2c5cbSIdo Schimmel } 241473c2c5cbSIdo Schimmel 241573c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_skb(const struct net *net, 241673c2c5cbSIdo Schimmel const struct sk_buff *skb) 241773c2c5cbSIdo Schimmel { 241873c2c5cbSIdo Schimmel u32 mhash, mhash_inner; 241973c2c5cbSIdo Schimmel bool has_inner = true; 242073c2c5cbSIdo Schimmel 242173c2c5cbSIdo Schimmel mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner); 242273c2c5cbSIdo Schimmel mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner); 242373c2c5cbSIdo Schimmel 242473c2c5cbSIdo Schimmel return jhash_2words(mhash, mhash_inner, 0); 242573c2c5cbSIdo Schimmel } 242673c2c5cbSIdo Schimmel 242773c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_fl6(const struct net *net, 242873c2c5cbSIdo Schimmel const struct flowi6 *fl6) 242973c2c5cbSIdo Schimmel { 243073c2c5cbSIdo Schimmel u32 hash_fields = ip6_multipath_hash_fields(net); 243173c2c5cbSIdo Schimmel struct flow_keys hash_keys; 243273c2c5cbSIdo Schimmel 243373c2c5cbSIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 243473c2c5cbSIdo Schimmel return 0; 243573c2c5cbSIdo Schimmel 243673c2c5cbSIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 243773c2c5cbSIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 243873c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 243973c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.src = fl6->saddr; 244073c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 244173c2c5cbSIdo Schimmel hash_keys.addrs.v6addrs.dst = fl6->daddr; 244273c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 244373c2c5cbSIdo Schimmel hash_keys.basic.ip_proto = fl6->flowi6_proto; 244473c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL) 244573c2c5cbSIdo Schimmel hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 244673c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 244773c2c5cbSIdo Schimmel hash_keys.ports.src = fl6->fl6_sport; 244873c2c5cbSIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 244973c2c5cbSIdo Schimmel hash_keys.ports.dst = fl6->fl6_dport; 245073c2c5cbSIdo Schimmel 245173c2c5cbSIdo Schimmel return flow_hash_from_keys(&hash_keys); 245273c2c5cbSIdo Schimmel } 245373c2c5cbSIdo Schimmel 245423aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 2455b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2456b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 245723aebdacSJakub Sitnicki { 245823aebdacSJakub Sitnicki struct flow_keys hash_keys; 2459b95b6e07SIdo Schimmel u32 mhash = 0; 246023aebdacSJakub Sitnicki 2461bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 2462b4bac172SDavid Ahern case 0: 24636f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 24646f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 24659a2a537aSDavid Ahern if (skb) { 24665e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 24679a2a537aSDavid Ahern } else { 24689a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 24699a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2470fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 24719a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 247223aebdacSJakub Sitnicki } 2473b95b6e07SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys); 2474b4bac172SDavid Ahern break; 2475b4bac172SDavid Ahern case 1: 2476b4bac172SDavid Ahern if (skb) { 2477b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2478b4bac172SDavid Ahern struct flow_keys keys; 2479b4bac172SDavid Ahern 2480b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2481b4bac172SDavid Ahern if (skb->l4_hash) 2482b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2483b4bac172SDavid Ahern 2484b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2485b4bac172SDavid Ahern 2486b4bac172SDavid Ahern if (!flkeys) { 2487b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2488b4bac172SDavid Ahern flkeys = &keys; 2489b4bac172SDavid Ahern } 2490b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2491b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2492b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2493b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2494b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2495b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2496b4bac172SDavid Ahern } else { 2497b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2498b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2499b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2500b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2501b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2502b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2503b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2504b4bac172SDavid Ahern } 2505b95b6e07SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys); 2506b4bac172SDavid Ahern break; 2507d8f74f09SStephen Suryaputra case 2: 2508d8f74f09SStephen Suryaputra memset(&hash_keys, 0, sizeof(hash_keys)); 2509d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2510d8f74f09SStephen Suryaputra if (skb) { 2511d8f74f09SStephen Suryaputra struct flow_keys keys; 2512d8f74f09SStephen Suryaputra 2513d8f74f09SStephen Suryaputra if (!flkeys) { 2514d8f74f09SStephen Suryaputra skb_flow_dissect_flow_keys(skb, &keys, 0); 2515d8f74f09SStephen Suryaputra flkeys = &keys; 2516d8f74f09SStephen Suryaputra } 2517d8f74f09SStephen Suryaputra 2518d8f74f09SStephen Suryaputra /* Inner can be v4 or v6 */ 2519d8f74f09SStephen Suryaputra if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2520d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2521d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2522d8f74f09SStephen Suryaputra hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2523d8f74f09SStephen Suryaputra } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2524d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2525d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2526d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2527d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = flkeys->tags.flow_label; 2528d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2529d8f74f09SStephen Suryaputra } else { 2530d8f74f09SStephen Suryaputra /* Same as case 0 */ 2531d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2532d8f74f09SStephen Suryaputra ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 2533d8f74f09SStephen Suryaputra } 2534d8f74f09SStephen Suryaputra } else { 2535d8f74f09SStephen Suryaputra /* Same as case 0 */ 2536d8f74f09SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2537d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.src = fl6->saddr; 2538d8f74f09SStephen Suryaputra hash_keys.addrs.v6addrs.dst = fl6->daddr; 2539d8f74f09SStephen Suryaputra hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 2540d8f74f09SStephen Suryaputra hash_keys.basic.ip_proto = fl6->flowi6_proto; 2541d8f74f09SStephen Suryaputra } 2542b95b6e07SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys); 2543d8f74f09SStephen Suryaputra break; 254473c2c5cbSIdo Schimmel case 3: 254573c2c5cbSIdo Schimmel if (skb) 254673c2c5cbSIdo Schimmel mhash = rt6_multipath_custom_hash_skb(net, skb); 254773c2c5cbSIdo Schimmel else 254873c2c5cbSIdo Schimmel mhash = rt6_multipath_custom_hash_fl6(net, fl6); 254973c2c5cbSIdo Schimmel break; 2550b4bac172SDavid Ahern } 255123aebdacSJakub Sitnicki 25529a2a537aSDavid Ahern return mhash >> 1; 255323aebdacSJakub Sitnicki } 255423aebdacSJakub Sitnicki 255567f415ddSWei Wang /* Called with rcu held */ 2556c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2557c71099acSThomas Graf { 2558b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2559c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 256067f415ddSWei Wang int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; 2561904af04dSJiri Benc struct ip_tunnel_info *tun_info; 25624c9483b2SDavid S. Miller struct flowi6 fl6 = { 2563e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 25644c9483b2SDavid S. Miller .daddr = iph->daddr, 25654c9483b2SDavid S. Miller .saddr = iph->saddr, 25666502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 25674c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 25684c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2569c71099acSThomas Graf }; 25705e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2571adaa70bbSThomas Graf 2572904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 257346fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2574904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 25755e5d6fedSRoopa Prabhu 25765e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 25775e5d6fedSRoopa Prabhu flkeys = &_flkeys; 25785e5d6fedSRoopa Prabhu 257923aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2580b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 258106e9d040SJiri Benc skb_dst_drop(skb); 258267f415ddSWei Wang skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, 258367f415ddSWei Wang &fl6, skb, flags)); 2584c71099acSThomas Graf } 2585c71099acSThomas Graf 258655cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net, 2587b75cc8f9SDavid Ahern struct fib6_table *table, 2588b75cc8f9SDavid Ahern struct flowi6 *fl6, 2589b75cc8f9SDavid Ahern const struct sk_buff *skb, 2590b75cc8f9SDavid Ahern int flags) 2591c71099acSThomas Graf { 2592b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2593c71099acSThomas Graf } 2594c71099acSThomas Graf 259590317bcdSGuillaume Nault static struct dst_entry *ip6_route_output_flags_noref(struct net *net, 25967d9e5f42SWei Wang const struct sock *sk, 259790317bcdSGuillaume Nault struct flowi6 *fl6, 259890317bcdSGuillaume Nault int flags) 2599c71099acSThomas Graf { 2600d46a9d67SDavid Ahern bool any_src; 2601c71099acSThomas Graf 26023ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 26033ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 26044c1feac5SDavid Ahern struct dst_entry *dst; 26054c1feac5SDavid Ahern 26067d9e5f42SWei Wang /* This function does not take refcnt on the dst */ 26074c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2608ca254490SDavid Ahern if (dst) 2609ca254490SDavid Ahern return dst; 26104c1feac5SDavid Ahern } 2611ca254490SDavid Ahern 26121fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 26134dc27d1cSDavid McCullough 26147d9e5f42SWei Wang flags |= RT6_LOOKUP_F_DST_NOREF; 2615d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2616741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2617d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 261877d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2619c71099acSThomas Graf 2620d46a9d67SDavid Ahern if (!any_src) 2621adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 26220c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 26230c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2624adaa70bbSThomas Graf 2625b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 26261da177e4SLinus Torvalds } 26277d9e5f42SWei Wang 26287d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net, 26297d9e5f42SWei Wang const struct sock *sk, 26307d9e5f42SWei Wang struct flowi6 *fl6, 26317d9e5f42SWei Wang int flags) 26327d9e5f42SWei Wang { 26337d9e5f42SWei Wang struct dst_entry *dst; 26347d9e5f42SWei Wang struct rt6_info *rt6; 26357d9e5f42SWei Wang 26367d9e5f42SWei Wang rcu_read_lock(); 26377d9e5f42SWei Wang dst = ip6_route_output_flags_noref(net, sk, fl6, flags); 26387d9e5f42SWei Wang rt6 = (struct rt6_info *)dst; 26397d9e5f42SWei Wang /* For dst cached in uncached_list, refcnt is already taken. */ 2640d288a162SWangyang Guo if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) { 26417d9e5f42SWei Wang dst = &net->ipv6.ip6_null_entry->dst; 26427d9e5f42SWei Wang dst_hold(dst); 26437d9e5f42SWei Wang } 26447d9e5f42SWei Wang rcu_read_unlock(); 26457d9e5f42SWei Wang 26467d9e5f42SWei Wang return dst; 26477d9e5f42SWei Wang } 26486f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 26491da177e4SLinus Torvalds 26502774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 265114e50e57SDavid S. Miller { 26525c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 26531dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 265414e50e57SDavid S. Miller struct dst_entry *new = NULL; 265514e50e57SDavid S. Miller 26561dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 265762cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 265814e50e57SDavid S. Miller if (rt) { 26590a1f5962SMartin KaFai Lau rt6_info_init(rt); 266081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 26610a1f5962SMartin KaFai Lau 2662d8d1f30bSChangli Gao new = &rt->dst; 266314e50e57SDavid S. Miller new->__use = 1; 2664352e512cSHerbert Xu new->input = dst_discard; 2665ede2059dSEric W. Biederman new->output = dst_discard_out; 266614e50e57SDavid S. Miller 2667defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 266814e50e57SDavid S. Miller 26691dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 26704e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 26710a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 267214e50e57SDavid S. Miller 267314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 267414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 267514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 267614e50e57SDavid S. Miller #endif 267714e50e57SDavid S. Miller } 267814e50e57SDavid S. Miller 267969ead7afSDavid S. Miller dst_release(dst_orig); 268069ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 268114e50e57SDavid S. Miller } 268214e50e57SDavid S. Miller 26831da177e4SLinus Torvalds /* 26841da177e4SLinus Torvalds * Destination cache support functions 26851da177e4SLinus Torvalds */ 26861da177e4SLinus Torvalds 26878d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 26883da59bd9SMartin KaFai Lau { 268936143645SSteffen Klassert u32 rt_cookie = 0; 2690c5cff856SWei Wang 26918ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 269293531c67SDavid Ahern return false; 269393531c67SDavid Ahern 269493531c67SDavid Ahern if (fib6_check_expired(f6i)) 269593531c67SDavid Ahern return false; 269693531c67SDavid Ahern 269793531c67SDavid Ahern return true; 269893531c67SDavid Ahern } 269993531c67SDavid Ahern 2700a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2701a68886a6SDavid Ahern struct fib6_info *from, 2702a68886a6SDavid Ahern u32 cookie) 27033da59bd9SMartin KaFai Lau { 2704c5cff856SWei Wang u32 rt_cookie = 0; 2705c5cff856SWei Wang 270649d05fe2SDavid Ahern if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || 270793531c67SDavid Ahern rt_cookie != cookie) 27083da59bd9SMartin KaFai Lau return NULL; 27093da59bd9SMartin KaFai Lau 27103da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 27113da59bd9SMartin KaFai Lau return NULL; 27123da59bd9SMartin KaFai Lau 27133da59bd9SMartin KaFai Lau return &rt->dst; 27143da59bd9SMartin KaFai Lau } 27153da59bd9SMartin KaFai Lau 2716a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2717a68886a6SDavid Ahern struct fib6_info *from, 2718a68886a6SDavid Ahern u32 cookie) 27193da59bd9SMartin KaFai Lau { 27205973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 27215973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2722a68886a6SDavid Ahern fib6_check(from, cookie)) 27233da59bd9SMartin KaFai Lau return &rt->dst; 27243da59bd9SMartin KaFai Lau else 27253da59bd9SMartin KaFai Lau return NULL; 27263da59bd9SMartin KaFai Lau } 27273da59bd9SMartin KaFai Lau 2728bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, 2729bbd807dfSBrian Vazquez u32 cookie) 27301da177e4SLinus Torvalds { 2731a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2732a68886a6SDavid Ahern struct fib6_info *from; 27331da177e4SLinus Torvalds struct rt6_info *rt; 27341da177e4SLinus Torvalds 2735a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2736a87b7dc9SDavid Ahern 27378f34e53bSDavid Ahern if (rt->sernum) 27388f34e53bSDavid Ahern return rt6_is_valid(rt) ? dst : NULL; 27398f34e53bSDavid Ahern 2740a87b7dc9SDavid Ahern rcu_read_lock(); 27411da177e4SLinus Torvalds 27426f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 27436f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 27446f3118b5SNicolas Dichtel * into this function always. 27456f3118b5SNicolas Dichtel */ 2746e3bc10bdSHannes Frederic Sowa 2747a68886a6SDavid Ahern from = rcu_dereference(rt->from); 27484b32b5adSMartin KaFai Lau 2749a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2750d288a162SWangyang Guo unlikely(!list_empty(&rt->dst.rt_uncached)))) 2751a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 27523da59bd9SMartin KaFai Lau else 2753a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2754a87b7dc9SDavid Ahern 2755a87b7dc9SDavid Ahern rcu_read_unlock(); 2756a87b7dc9SDavid Ahern 2757a87b7dc9SDavid Ahern return dst_ret; 27581da177e4SLinus Torvalds } 27599c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_dst_check); 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 27621da177e4SLinus Torvalds { 27631da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds if (rt) { 276654c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2767c3c14da0SDavid Ahern rcu_read_lock(); 276854c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 276993531c67SDavid Ahern rt6_remove_exception_rt(rt); 277054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 27711da177e4SLinus Torvalds } 2772c3c14da0SDavid Ahern rcu_read_unlock(); 277354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 277454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 277554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 277654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 277754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 277854c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 27821da177e4SLinus Torvalds { 27831da177e4SLinus Torvalds struct rt6_info *rt; 27841da177e4SLinus Torvalds 27853ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 27861da177e4SLinus Torvalds 2787adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 27881da177e4SLinus Torvalds if (rt) { 27898a14e46fSDavid Ahern rcu_read_lock(); 27901eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 279193531c67SDavid Ahern rt6_remove_exception_rt(rt); 2792c5cff856SWei Wang } else { 2793a68886a6SDavid Ahern struct fib6_info *from; 2794c5cff856SWei Wang struct fib6_node *fn; 2795c5cff856SWei Wang 2796a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2797a68886a6SDavid Ahern if (from) { 2798a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2799c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2800aafc2e32SEric Dumazet WRITE_ONCE(fn->fn_sernum, -1); 2801a68886a6SDavid Ahern } 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds rcu_read_unlock(); 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28076a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 28086a3e030fSDavid Ahern { 2809a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2810a68886a6SDavid Ahern struct fib6_info *from; 2811a68886a6SDavid Ahern 2812a68886a6SDavid Ahern rcu_read_lock(); 2813a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2814a68886a6SDavid Ahern if (from) 2815a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2816a68886a6SDavid Ahern rcu_read_unlock(); 2817a68886a6SDavid Ahern } 28186a3e030fSDavid Ahern 28196a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 28206a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 28216700c270SDavid S. Miller } 28221da177e4SLinus Torvalds 282345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 282445e4fd26SMartin KaFai Lau { 282545e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 282645e4fd26SMartin KaFai Lau 2827d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 282845e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 282945e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 283045e4fd26SMartin KaFai Lau } 283145e4fd26SMartin KaFai Lau 28320d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 28330d3f6d29SMartin KaFai Lau { 28340d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 28351490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 28360d3f6d29SMartin KaFai Lau } 28370d3f6d29SMartin KaFai Lau 283845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 2839bd085ef6SHangbin Liu const struct ipv6hdr *iph, u32 mtu, 2840bd085ef6SHangbin Liu bool confirm_neigh) 28411da177e4SLinus Torvalds { 28420dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 28431da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 28441da177e4SLinus Torvalds 284509454fd0SMaciej Żenczykowski /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU) 284609454fd0SMaciej Żenczykowski * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it. 284709454fd0SMaciej Żenczykowski * [see also comment in rt6_mtu_change_route()] 284809454fd0SMaciej Żenczykowski */ 284919bda36cSXin Long 285045e4fd26SMartin KaFai Lau if (iph) { 285145e4fd26SMartin KaFai Lau daddr = &iph->daddr; 285245e4fd26SMartin KaFai Lau saddr = &iph->saddr; 285345e4fd26SMartin KaFai Lau } else if (sk) { 285445e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 285545e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 285645e4fd26SMartin KaFai Lau } else { 28570dec879fSJulian Anastasov daddr = NULL; 28580dec879fSJulian Anastasov saddr = NULL; 28591da177e4SLinus Torvalds } 2860bd085ef6SHangbin Liu 2861bd085ef6SHangbin Liu if (confirm_neigh) 28620dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 2863bd085ef6SHangbin Liu 28644a65dff8SGeorg Kohmann if (mtu < IPV6_MIN_MTU) 28654a65dff8SGeorg Kohmann return; 28660dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 28670dec879fSJulian Anastasov return; 28680dec879fSJulian Anastasov 28690dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 28700dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 28712b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 28722b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 28732b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 28740dec879fSJulian Anastasov } else if (daddr) { 287585bd05deSDavid Ahern struct fib6_result res = {}; 28760dec879fSJulian Anastasov struct rt6_info *nrt6; 28770dec879fSJulian Anastasov 28784d85cd0cSDavid Ahern rcu_read_lock(); 287985bd05deSDavid Ahern res.f6i = rcu_dereference(rt6->from); 288043a4b60dSDavid Ahern if (!res.f6i) 288143a4b60dSDavid Ahern goto out_unlock; 288243a4b60dSDavid Ahern 28837d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 28847d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 28857d21fec9SDavid Ahern 28862d44234bSDavid Ahern if (res.f6i->nh) { 28872d44234bSDavid Ahern struct fib6_nh_match_arg arg = { 28882d44234bSDavid Ahern .dev = dst->dev, 28892d44234bSDavid Ahern .gw = &rt6->rt6i_gateway, 28902d44234bSDavid Ahern }; 28912d44234bSDavid Ahern 28922d44234bSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 28932d44234bSDavid Ahern fib6_nh_find_match, &arg); 28942d44234bSDavid Ahern 28952d44234bSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 28962d44234bSDavid Ahern * using the dst->dev + gw. Should be impossible. 28972d44234bSDavid Ahern */ 289843a4b60dSDavid Ahern if (!arg.match) 289943a4b60dSDavid Ahern goto out_unlock; 29002d44234bSDavid Ahern 29012d44234bSDavid Ahern res.nh = arg.match; 29022d44234bSDavid Ahern } else { 29032d44234bSDavid Ahern res.nh = res.f6i->fib6_nh; 29042d44234bSDavid Ahern } 29052d44234bSDavid Ahern 290685bd05deSDavid Ahern nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 290745e4fd26SMartin KaFai Lau if (nrt6) { 290845e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 29095012f0a5SDavid Ahern if (rt6_insert_exception(nrt6, &res)) 29102b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 291145e4fd26SMartin KaFai Lau } 291243a4b60dSDavid Ahern out_unlock: 2913a68886a6SDavid Ahern rcu_read_unlock(); 291445e4fd26SMartin KaFai Lau } 291545e4fd26SMartin KaFai Lau } 291645e4fd26SMartin KaFai Lau 291745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 2918bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 2919bd085ef6SHangbin Liu bool confirm_neigh) 292045e4fd26SMartin KaFai Lau { 2921bd085ef6SHangbin Liu __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, 2922bd085ef6SHangbin Liu confirm_neigh); 29231da177e4SLinus Torvalds } 29241da177e4SLinus Torvalds 292542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2926e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 292781aded24SDavid S. Miller { 292881aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 292981aded24SDavid S. Miller struct dst_entry *dst; 2930dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2931dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2932dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2933dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2934dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2935dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2936dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2937dc92095dSMaciej Żenczykowski }; 293881aded24SDavid S. Miller 293981aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 294081aded24SDavid S. Miller if (!dst->error) 2941bd085ef6SHangbin Liu __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); 294281aded24SDavid S. Miller dst_release(dst); 294381aded24SDavid S. Miller } 294481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 294581aded24SDavid S. Miller 294681aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 294781aded24SDavid S. Miller { 29487ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 294933c162a9SMartin KaFai Lau struct dst_entry *dst; 295033c162a9SMartin KaFai Lau 29517ddacfa5SDavid Ahern if (!oif && skb->dev) 29527ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 29537ddacfa5SDavid Ahern 29547ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 295533c162a9SMartin KaFai Lau 295633c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 295733c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 295833c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 295933c162a9SMartin KaFai Lau return; 296033c162a9SMartin KaFai Lau 296133c162a9SMartin KaFai Lau bh_lock_sock(sk); 296233c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 296333c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 296433c162a9SMartin KaFai Lau bh_unlock_sock(sk); 296581aded24SDavid S. Miller } 296681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 296781aded24SDavid S. Miller 29687d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 29697d6850f7SAlexey Kodanev const struct flowi6 *fl6) 29707d6850f7SAlexey Kodanev { 29717d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 29727d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 29737d6850f7SAlexey Kodanev #endif 29747d6850f7SAlexey Kodanev 29757d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 29767d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 29777d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 29787d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 29797d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 29807d6850f7SAlexey Kodanev &np->saddr : 29817d6850f7SAlexey Kodanev #endif 29827d6850f7SAlexey Kodanev NULL); 29837d6850f7SAlexey Kodanev } 29847d6850f7SAlexey Kodanev 29859b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res, 29860b34eb00SDavid Ahern struct flowi6 *fl6, 29870b34eb00SDavid Ahern const struct in6_addr *gw, 29880b34eb00SDavid Ahern struct rt6_info **ret) 29890b34eb00SDavid Ahern { 29909b6b35abSDavid Ahern const struct fib6_nh *nh = res->nh; 29919b6b35abSDavid Ahern 29920b34eb00SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 29930b34eb00SDavid Ahern fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 29940b34eb00SDavid Ahern return false; 29950b34eb00SDavid Ahern 29960b34eb00SDavid Ahern /* rt_cache's gateway might be different from its 'parent' 29970b34eb00SDavid Ahern * in the case of an ip redirect. 29980b34eb00SDavid Ahern * So we keep searching in the exception table if the gateway 29990b34eb00SDavid Ahern * is different. 30000b34eb00SDavid Ahern */ 30010b34eb00SDavid Ahern if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 30020b34eb00SDavid Ahern struct rt6_info *rt_cache; 30030b34eb00SDavid Ahern 30049b6b35abSDavid Ahern rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 30050b34eb00SDavid Ahern if (rt_cache && 30060b34eb00SDavid Ahern ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 30070b34eb00SDavid Ahern *ret = rt_cache; 30080b34eb00SDavid Ahern return true; 30090b34eb00SDavid Ahern } 30100b34eb00SDavid Ahern return false; 30110b34eb00SDavid Ahern } 30120b34eb00SDavid Ahern return true; 30130b34eb00SDavid Ahern } 30140b34eb00SDavid Ahern 3015c55c8988SDavid Ahern struct fib6_nh_rd_arg { 3016c55c8988SDavid Ahern struct fib6_result *res; 3017c55c8988SDavid Ahern struct flowi6 *fl6; 3018c55c8988SDavid Ahern const struct in6_addr *gw; 3019c55c8988SDavid Ahern struct rt6_info **ret; 3020c55c8988SDavid Ahern }; 3021c55c8988SDavid Ahern 3022c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) 3023c55c8988SDavid Ahern { 3024c55c8988SDavid Ahern struct fib6_nh_rd_arg *arg = _arg; 3025c55c8988SDavid Ahern 3026c55c8988SDavid Ahern arg->res->nh = nh; 3027c55c8988SDavid Ahern return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); 3028c55c8988SDavid Ahern } 3029c55c8988SDavid Ahern 3030b55b76b2SDuan Jiong /* Handle redirects */ 3031b55b76b2SDuan Jiong struct ip6rd_flowi { 3032b55b76b2SDuan Jiong struct flowi6 fl6; 3033b55b76b2SDuan Jiong struct in6_addr gateway; 3034b55b76b2SDuan Jiong }; 3035b55b76b2SDuan Jiong 303655cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net, 3037b55b76b2SDuan Jiong struct fib6_table *table, 3038b55b76b2SDuan Jiong struct flowi6 *fl6, 3039b75cc8f9SDavid Ahern const struct sk_buff *skb, 3040b55b76b2SDuan Jiong int flags) 3041b55b76b2SDuan Jiong { 3042b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 30430b34eb00SDavid Ahern struct rt6_info *ret = NULL; 30449b6b35abSDavid Ahern struct fib6_result res = {}; 3045c55c8988SDavid Ahern struct fib6_nh_rd_arg arg = { 3046c55c8988SDavid Ahern .res = &res, 3047c55c8988SDavid Ahern .fl6 = fl6, 3048c55c8988SDavid Ahern .gw = &rdfl->gateway, 3049c55c8988SDavid Ahern .ret = &ret 3050c55c8988SDavid Ahern }; 30518d1c802bSDavid Ahern struct fib6_info *rt; 3052b55b76b2SDuan Jiong struct fib6_node *fn; 3053b55b76b2SDuan Jiong 3054b55b76b2SDuan Jiong /* Get the "current" route for this destination and 305567c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 3056b55b76b2SDuan Jiong * 3057b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 3058b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 3059b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 3060b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 3061b55b76b2SDuan Jiong * routes. 3062b55b76b2SDuan Jiong */ 3063b55b76b2SDuan Jiong 306466f5d6ceSWei Wang rcu_read_lock(); 30656454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 3066b55b76b2SDuan Jiong restart: 306766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 30689b6b35abSDavid Ahern res.f6i = rt; 306914895687SDavid Ahern if (fib6_check_expired(rt)) 3070b55b76b2SDuan Jiong continue; 307193c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 3072b55b76b2SDuan Jiong break; 3073c55c8988SDavid Ahern if (unlikely(rt->nh)) { 3074c55c8988SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 3075c55c8988SDavid Ahern continue; 3076c55c8988SDavid Ahern /* on match, res->nh is filled in and potentially ret */ 3077c55c8988SDavid Ahern if (nexthop_for_each_fib6_nh(rt->nh, 3078c55c8988SDavid Ahern fib6_nh_redirect_match, 3079c55c8988SDavid Ahern &arg)) 30800b34eb00SDavid Ahern goto out; 3081c55c8988SDavid Ahern } else { 3082c55c8988SDavid Ahern res.nh = rt->fib6_nh; 3083c55c8988SDavid Ahern if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, 3084c55c8988SDavid Ahern &ret)) 3085c55c8988SDavid Ahern goto out; 3086c55c8988SDavid Ahern } 3087b55b76b2SDuan Jiong } 3088b55b76b2SDuan Jiong 3089b55b76b2SDuan Jiong if (!rt) 3090421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 309193c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 309223fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 3093b0a1ba59SMartin KaFai Lau goto out; 3094b0a1ba59SMartin KaFai Lau } 3095b0a1ba59SMartin KaFai Lau 3096421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 3097a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 3098a3c00e46SMartin KaFai Lau if (fn) 3099a3c00e46SMartin KaFai Lau goto restart; 3100b55b76b2SDuan Jiong } 3101a3c00e46SMartin KaFai Lau 31029b6b35abSDavid Ahern res.f6i = rt; 31031cf844c7SDavid Ahern res.nh = rt->fib6_nh; 3104b0a1ba59SMartin KaFai Lau out: 31057d21fec9SDavid Ahern if (ret) { 310610585b43SDavid Ahern ip6_hold_safe(net, &ret); 31077d21fec9SDavid Ahern } else { 31087d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 31097d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 31109b6b35abSDavid Ahern ret = ip6_create_rt_rcu(&res); 31117d21fec9SDavid Ahern } 3112b55b76b2SDuan Jiong 311366f5d6ceSWei Wang rcu_read_unlock(); 3114b55b76b2SDuan Jiong 31158ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 311623fb93a4SDavid Ahern return ret; 3117b55b76b2SDuan Jiong }; 3118b55b76b2SDuan Jiong 3119b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 3120b55b76b2SDuan Jiong const struct flowi6 *fl6, 3121b75cc8f9SDavid Ahern const struct sk_buff *skb, 3122b55b76b2SDuan Jiong const struct in6_addr *gateway) 3123b55b76b2SDuan Jiong { 3124b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 3125b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 3126b55b76b2SDuan Jiong 3127b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 3128b55b76b2SDuan Jiong rdfl.gateway = *gateway; 3129b55b76b2SDuan Jiong 3130b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 3131b55b76b2SDuan Jiong flags, __ip6_route_redirect); 3132b55b76b2SDuan Jiong } 3133b55b76b2SDuan Jiong 3134e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 3135e2d118a1SLorenzo Colitti kuid_t uid) 31363a5ad2eeSDavid S. Miller { 31373a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 31383a5ad2eeSDavid S. Miller struct dst_entry *dst; 31391f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 31401f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 31411f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 31421f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 31431f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 31441f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 31451f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 31461f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 31471f7f10acSMaciej Żenczykowski }; 31483a5ad2eeSDavid S. Miller 3149b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 31506700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 31513a5ad2eeSDavid S. Miller dst_release(dst); 31523a5ad2eeSDavid S. Miller } 31533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 31543a5ad2eeSDavid S. Miller 3155d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 3156c92a59ecSDuan Jiong { 3157c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 3158c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 3159c92a59ecSDuan Jiong struct dst_entry *dst; 31600b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 31610b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 31620b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 31630b26fb17SMaciej Żenczykowski .daddr = msg->dest, 31640b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 31650b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 31660b26fb17SMaciej Żenczykowski }; 3167c92a59ecSDuan Jiong 3168b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 3169c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 3170c92a59ecSDuan Jiong dst_release(dst); 3171c92a59ecSDuan Jiong } 3172c92a59ecSDuan Jiong 31733a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 31743a5ad2eeSDavid S. Miller { 3175e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 3176e2d118a1SLorenzo Colitti sk->sk_uid); 31773a5ad2eeSDavid S. Miller } 31783a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 31793a5ad2eeSDavid S. Miller 31800dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 31811da177e4SLinus Torvalds { 31820dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 31830dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 31840dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 31850dbaee3bSDavid S. Miller 31861da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 31871da177e4SLinus Torvalds 31885578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 31895578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds /* 31921da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 31931da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 31941da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 31951da177e4SLinus Torvalds * rely only on pmtu discovery" 31961da177e4SLinus Torvalds */ 31971da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 31981da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 31991da177e4SLinus Torvalds return mtu; 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 3202f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst) 3203d33e4553SDavid S. Miller { 3204427faee1SVadim Fedorenko return ip6_dst_mtu_maybe_forward(dst, false); 3205d33e4553SDavid S. Miller } 32069c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_mtu); 3207d33e4553SDavid S. Miller 3208901731b8SDavid Ahern /* MTU selection: 3209901731b8SDavid Ahern * 1. mtu on route is locked - use it 3210901731b8SDavid Ahern * 2. mtu from nexthop exception 3211901731b8SDavid Ahern * 3. mtu from egress device 3212901731b8SDavid Ahern * 3213901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 3214901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 3215901731b8SDavid Ahern */ 3216b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res, 3217b748f260SDavid Ahern const struct in6_addr *daddr, 3218b748f260SDavid Ahern const struct in6_addr *saddr) 3219901731b8SDavid Ahern { 3220b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 3221b748f260SDavid Ahern struct fib6_info *f6i = res->f6i; 3222901731b8SDavid Ahern struct inet6_dev *idev; 3223510e2cedSWei Wang struct rt6_info *rt; 3224901731b8SDavid Ahern u32 mtu = 0; 3225901731b8SDavid Ahern 3226901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 3227901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 3228901731b8SDavid Ahern if (mtu) 3229901731b8SDavid Ahern goto out; 3230901731b8SDavid Ahern } 3231901731b8SDavid Ahern 3232510e2cedSWei Wang rt = rt6_find_cached_rt(res, daddr, saddr); 3233510e2cedSWei Wang if (unlikely(rt)) { 3234510e2cedSWei Wang mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 3235510e2cedSWei Wang } else { 3236b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 3237901731b8SDavid Ahern 3238901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 3239901731b8SDavid Ahern idev = __in6_dev_get(dev); 3240901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 3241901731b8SDavid Ahern mtu = idev->cnf.mtu6; 3242901731b8SDavid Ahern } 3243901731b8SDavid Ahern 3244901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 3245901731b8SDavid Ahern out: 3246b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 3247901731b8SDavid Ahern } 3248901731b8SDavid Ahern 32493b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 325087a11578SDavid S. Miller struct flowi6 *fl6) 32511da177e4SLinus Torvalds { 325287a11578SDavid S. Miller struct dst_entry *dst; 32531da177e4SLinus Torvalds struct rt6_info *rt; 32541da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 3255c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 32561da177e4SLinus Torvalds 325738308473SDavid S. Miller if (unlikely(!idev)) 3258122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 32591da177e4SLinus Torvalds 3260ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 326138308473SDavid S. Miller if (unlikely(!rt)) { 32621da177e4SLinus Torvalds in6_dev_put(idev); 326387a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 32641da177e4SLinus Torvalds goto out; 32651da177e4SLinus Torvalds } 32661da177e4SLinus Torvalds 3267588753f1SBrendan McGrath rt->dst.input = ip6_input; 32688e2ec639SYan, Zheng rt->dst.output = ip6_output; 3269550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 327087a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 32718e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 32728e2ec639SYan, Zheng rt->rt6i_idev = idev; 327314edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 32741da177e4SLinus Torvalds 32754c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 3276587fea74SWei Wang * do proper release of the net_device 3277587fea74SWei Wang */ 3278587fea74SWei Wang rt6_uncached_list_add(rt); 32791da177e4SLinus Torvalds 328087a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 328187a11578SDavid S. Miller 32821da177e4SLinus Torvalds out: 328387a11578SDavid S. Miller return dst; 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds 3286af6d1034SJon Maxwell static void ip6_dst_gc(struct dst_ops *ops) 32871da177e4SLinus Torvalds { 328886393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 32897019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 32907019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 32917019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 32927019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 32939cb7c013SEric Dumazet unsigned int val; 3294fc66f95cSEric Dumazet int entries; 32951da177e4SLinus Torvalds 3296af6d1034SJon Maxwell if (time_after(rt_last_gc + rt_min_interval, jiffies)) 32971da177e4SLinus Torvalds goto out; 32981da177e4SLinus Torvalds 32999cb7c013SEric Dumazet fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true); 3300fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 3301fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 33029cb7c013SEric Dumazet atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1); 33031da177e4SLinus Torvalds out: 33049cb7c013SEric Dumazet val = atomic_read(&net->ipv6.ip6_rt_gc_expire); 33059cb7c013SEric Dumazet atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity)); 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 3308b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, 3309b2c709ccSDavid Ahern const struct in6_addr *gw_addr, u32 tbid, 3310b2c709ccSDavid Ahern int flags, struct fib6_result *res) 33118c14586fSDavid Ahern { 33128c14586fSDavid Ahern struct flowi6 fl6 = { 33138c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 33148c14586fSDavid Ahern .daddr = *gw_addr, 33158c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 33168c14586fSDavid Ahern }; 33178c14586fSDavid Ahern struct fib6_table *table; 3318b2c709ccSDavid Ahern int err; 33198c14586fSDavid Ahern 3320f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 33218c14586fSDavid Ahern if (!table) 3322b2c709ccSDavid Ahern return -EINVAL; 33238c14586fSDavid Ahern 33248c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 33258c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 33268c14586fSDavid Ahern 3327f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 33288c14586fSDavid Ahern 3329b2c709ccSDavid Ahern err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); 3330b2c709ccSDavid Ahern if (!err && res->f6i != net->ipv6.fib6_null_entry) 3331b2c709ccSDavid Ahern fib6_select_path(net, res, &fl6, cfg->fc_ifindex, 3332b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 33338c14586fSDavid Ahern 3334b2c709ccSDavid Ahern return err; 33358c14586fSDavid Ahern } 33368c14586fSDavid Ahern 3337fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 3338fc1e64e1SDavid Ahern struct fib6_config *cfg, 33399fbb704cSDavid Ahern const struct net_device *dev, 3340fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 3341fc1e64e1SDavid Ahern { 3342b2c709ccSDavid Ahern u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; 3343fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 3344b2c709ccSDavid Ahern struct fib6_result res = {}; 3345fc1e64e1SDavid Ahern int err; 3346fc1e64e1SDavid Ahern 3347b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); 3348b2c709ccSDavid Ahern if (!err && !(res.fib6_flags & RTF_REJECT) && 33494ed591c8SDavid Ahern /* ignore match if it is the default route */ 3350b2c709ccSDavid Ahern !ipv6_addr_any(&res.f6i->fib6_dst.addr) && 3351b2c709ccSDavid Ahern (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { 335244750f84SDavid Ahern NL_SET_ERR_MSG(extack, 335344750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 3354fc1e64e1SDavid Ahern err = -EINVAL; 3355fc1e64e1SDavid Ahern } 3356fc1e64e1SDavid Ahern 3357fc1e64e1SDavid Ahern return err; 3358fc1e64e1SDavid Ahern } 3359fc1e64e1SDavid Ahern 33601edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 33611edce99fSDavid Ahern struct fib6_config *cfg, 33621edce99fSDavid Ahern struct net_device **_dev, 33631edce99fSDavid Ahern struct inet6_dev **idev) 33641edce99fSDavid Ahern { 33651edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 33661edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 3367b2c709ccSDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 3368b2c709ccSDavid Ahern struct fib6_result res = {}; 33691edce99fSDavid Ahern int err = -EHOSTUNREACH; 33701edce99fSDavid Ahern 33711edce99fSDavid Ahern if (cfg->fc_table) { 3372b2c709ccSDavid Ahern err = ip6_nh_lookup_table(net, cfg, gw_addr, 3373b2c709ccSDavid Ahern cfg->fc_table, flags, &res); 3374b2c709ccSDavid Ahern /* gw_addr can not require a gateway or resolve to a reject 3375b2c709ccSDavid Ahern * route. If a device is given, it must match the result. 3376b2c709ccSDavid Ahern */ 3377b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3378b2c709ccSDavid Ahern res.nh->fib_nh_gw_family || 3379b2c709ccSDavid Ahern (dev && dev != res.nh->fib_nh_dev)) 3380b2c709ccSDavid Ahern err = -EHOSTUNREACH; 33811edce99fSDavid Ahern } 33821edce99fSDavid Ahern 3383b2c709ccSDavid Ahern if (err < 0) { 3384b2c709ccSDavid Ahern struct flowi6 fl6 = { 3385b2c709ccSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 3386b2c709ccSDavid Ahern .daddr = *gw_addr, 3387b2c709ccSDavid Ahern }; 33881edce99fSDavid Ahern 3389b2c709ccSDavid Ahern err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); 3390b2c709ccSDavid Ahern if (err || res.fib6_flags & RTF_REJECT || 3391b2c709ccSDavid Ahern res.nh->fib_nh_gw_family) 3392b2c709ccSDavid Ahern err = -EHOSTUNREACH; 33931edce99fSDavid Ahern 3394b2c709ccSDavid Ahern if (err) 3395b2c709ccSDavid Ahern return err; 3396b2c709ccSDavid Ahern 3397b2c709ccSDavid Ahern fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, 3398b2c709ccSDavid Ahern cfg->fc_ifindex != 0, NULL, flags); 33991edce99fSDavid Ahern } 34001edce99fSDavid Ahern 34011edce99fSDavid Ahern err = 0; 3402b2c709ccSDavid Ahern if (dev) { 3403b2c709ccSDavid Ahern if (dev != res.nh->fib_nh_dev) 3404b2c709ccSDavid Ahern err = -EHOSTUNREACH; 3405b2c709ccSDavid Ahern } else { 3406b2c709ccSDavid Ahern *_dev = dev = res.nh->fib_nh_dev; 3407b2c709ccSDavid Ahern dev_hold(dev); 3408b2c709ccSDavid Ahern *idev = in6_dev_get(dev); 3409b2c709ccSDavid Ahern } 34101edce99fSDavid Ahern 34111edce99fSDavid Ahern return err; 34121edce99fSDavid Ahern } 34131edce99fSDavid Ahern 34149fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 34159fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 34169fbb704cSDavid Ahern struct netlink_ext_ack *extack) 34179fbb704cSDavid Ahern { 34189fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 34199fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 3420232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 34219fbb704cSDavid Ahern const struct net_device *dev = *_dev; 3422232378e8SDavid Ahern bool need_addr_check = !dev; 34239fbb704cSDavid Ahern int err = -EINVAL; 34249fbb704cSDavid Ahern 34259fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 34269fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 34279fbb704cSDavid Ahern * will return already-added prefix route via interface that 34289fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 34299fbb704cSDavid Ahern */ 3430232378e8SDavid Ahern if (dev && 3431232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3432232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 34339fbb704cSDavid Ahern goto out; 34349fbb704cSDavid Ahern } 34359fbb704cSDavid Ahern 34369fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 34379fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 34389fbb704cSDavid Ahern * addresses as nexthop address. 34399fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 34409fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 34419fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 34429fbb704cSDavid Ahern * some exceptions. --ANK 34439fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 34449fbb704cSDavid Ahern * addressing 34459fbb704cSDavid Ahern */ 34469fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 34479fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 34489fbb704cSDavid Ahern goto out; 34499fbb704cSDavid Ahern } 34509fbb704cSDavid Ahern 3451b2c709ccSDavid Ahern rcu_read_lock(); 3452b2c709ccSDavid Ahern 34539fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 34549fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 34559fbb704cSDavid Ahern else 34569fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 34579fbb704cSDavid Ahern 3458b2c709ccSDavid Ahern rcu_read_unlock(); 3459b2c709ccSDavid Ahern 34609fbb704cSDavid Ahern if (err) 34619fbb704cSDavid Ahern goto out; 34629fbb704cSDavid Ahern } 34639fbb704cSDavid Ahern 34649fbb704cSDavid Ahern /* reload in case device was changed */ 34659fbb704cSDavid Ahern dev = *_dev; 34669fbb704cSDavid Ahern 34679fbb704cSDavid Ahern err = -EINVAL; 34689fbb704cSDavid Ahern if (!dev) { 34699fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 34709fbb704cSDavid Ahern goto out; 34719fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 34729fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 34739fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 34749fbb704cSDavid Ahern goto out; 34759fbb704cSDavid Ahern } 3476232378e8SDavid Ahern 3477232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 3478232378e8SDavid Ahern * egress device has been resolved. 3479232378e8SDavid Ahern */ 3480232378e8SDavid Ahern if (need_addr_check && 3481232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3482232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3483232378e8SDavid Ahern goto out; 3484232378e8SDavid Ahern } 3485232378e8SDavid Ahern 34869fbb704cSDavid Ahern err = 0; 34879fbb704cSDavid Ahern out: 34889fbb704cSDavid Ahern return err; 34899fbb704cSDavid Ahern } 34909fbb704cSDavid Ahern 349183c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 349283c44251SDavid Ahern { 349383c44251SDavid Ahern if ((flags & RTF_REJECT) || 349483c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 349583c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 3496aea23c32SDavid Ahern !(flags & (RTF_ANYCAST | RTF_LOCAL)))) 349783c44251SDavid Ahern return true; 349883c44251SDavid Ahern 349983c44251SDavid Ahern return false; 350083c44251SDavid Ahern } 350183c44251SDavid Ahern 350283c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 350383c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 350483c44251SDavid Ahern struct netlink_ext_ack *extack) 350583c44251SDavid Ahern { 350683c44251SDavid Ahern struct net_device *dev = NULL; 350783c44251SDavid Ahern struct inet6_dev *idev = NULL; 350883c44251SDavid Ahern int addr_type; 350983c44251SDavid Ahern int err; 351083c44251SDavid Ahern 3511f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 35121bef4c22SEric Dumazet #ifdef CONFIG_IPV6_ROUTER_PREF 35131bef4c22SEric Dumazet fib6_nh->last_probe = jiffies; 35141bef4c22SEric Dumazet #endif 351538428d68SRoopa Prabhu if (cfg->fc_is_fdb) { 351638428d68SRoopa Prabhu fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 351738428d68SRoopa Prabhu fib6_nh->fib_nh_gw_family = AF_INET6; 351838428d68SRoopa Prabhu return 0; 351938428d68SRoopa Prabhu } 3520f1741730SDavid Ahern 352183c44251SDavid Ahern err = -ENODEV; 352283c44251SDavid Ahern if (cfg->fc_ifindex) { 352383c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 352483c44251SDavid Ahern if (!dev) 352583c44251SDavid Ahern goto out; 352683c44251SDavid Ahern idev = in6_dev_get(dev); 352783c44251SDavid Ahern if (!idev) 352883c44251SDavid Ahern goto out; 352983c44251SDavid Ahern } 353083c44251SDavid Ahern 353183c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 353283c44251SDavid Ahern if (!dev) { 353383c44251SDavid Ahern NL_SET_ERR_MSG(extack, 353483c44251SDavid Ahern "Nexthop device required for onlink"); 353583c44251SDavid Ahern goto out; 353683c44251SDavid Ahern } 353783c44251SDavid Ahern 353883c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 353983c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 354083c44251SDavid Ahern err = -ENETDOWN; 354183c44251SDavid Ahern goto out; 354283c44251SDavid Ahern } 354383c44251SDavid Ahern 3544ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 354583c44251SDavid Ahern } 354683c44251SDavid Ahern 3547ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 354883c44251SDavid Ahern 354983c44251SDavid Ahern /* We cannot add true routes via loopback here, 355083c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 355183c44251SDavid Ahern */ 355283c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 355383c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 355483c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 355583c44251SDavid Ahern if (dev != net->loopback_dev) { 355683c44251SDavid Ahern if (dev) { 355783c44251SDavid Ahern dev_put(dev); 355883c44251SDavid Ahern in6_dev_put(idev); 355983c44251SDavid Ahern } 356083c44251SDavid Ahern dev = net->loopback_dev; 356183c44251SDavid Ahern dev_hold(dev); 356283c44251SDavid Ahern idev = in6_dev_get(dev); 356383c44251SDavid Ahern if (!idev) { 356483c44251SDavid Ahern err = -ENODEV; 356583c44251SDavid Ahern goto out; 356683c44251SDavid Ahern } 356783c44251SDavid Ahern } 35687dd73168SDavid Ahern goto pcpu_alloc; 356983c44251SDavid Ahern } 357083c44251SDavid Ahern 357183c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 357283c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 357383c44251SDavid Ahern if (err) 357483c44251SDavid Ahern goto out; 357583c44251SDavid Ahern 3576ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3577bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 357883c44251SDavid Ahern } 357983c44251SDavid Ahern 358083c44251SDavid Ahern err = -ENODEV; 358183c44251SDavid Ahern if (!dev) 358283c44251SDavid Ahern goto out; 358383c44251SDavid Ahern 358483c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 358583c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 358683c44251SDavid Ahern err = -EACCES; 358783c44251SDavid Ahern goto out; 358883c44251SDavid Ahern } 358983c44251SDavid Ahern 359083c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 359183c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 359283c44251SDavid Ahern err = -ENETDOWN; 359383c44251SDavid Ahern goto out; 359483c44251SDavid Ahern } 359583c44251SDavid Ahern 359683c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 359783c44251SDavid Ahern !netif_carrier_ok(dev)) 3598ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 359983c44251SDavid Ahern 3600faee6769SAlexander Aring err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap, 36017dd73168SDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 36027dd73168SDavid Ahern if (err) 36037dd73168SDavid Ahern goto out; 36047dd73168SDavid Ahern 36057dd73168SDavid Ahern pcpu_alloc: 3606f40b6ae2SDavid Ahern fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3607f40b6ae2SDavid Ahern if (!fib6_nh->rt6i_pcpu) { 3608f40b6ae2SDavid Ahern err = -ENOMEM; 3609f40b6ae2SDavid Ahern goto out; 3610f40b6ae2SDavid Ahern } 3611f40b6ae2SDavid Ahern 3612ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3613e44b14ebSEric Dumazet netdev_tracker_alloc(dev, &fib6_nh->fib_nh_dev_tracker, gfp_flags); 3614e44b14ebSEric Dumazet 3615f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 361683c44251SDavid Ahern err = 0; 361783c44251SDavid Ahern out: 361883c44251SDavid Ahern if (idev) 361983c44251SDavid Ahern in6_dev_put(idev); 362083c44251SDavid Ahern 362183c44251SDavid Ahern if (err) { 3622ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3623ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 362483c44251SDavid Ahern dev_put(dev); 362583c44251SDavid Ahern } 362683c44251SDavid Ahern 362783c44251SDavid Ahern return err; 362883c44251SDavid Ahern } 362983c44251SDavid Ahern 3630dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3631dac7d0f2SDavid Ahern { 3632cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 3633cc5c073aSDavid Ahern 3634cc5c073aSDavid Ahern rcu_read_lock(); 3635cc5c073aSDavid Ahern 3636cc5c073aSDavid Ahern fib6_nh_flush_exceptions(fib6_nh, NULL); 3637cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); 3638cc5c073aSDavid Ahern if (bucket) { 3639cc5c073aSDavid Ahern rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); 3640cc5c073aSDavid Ahern kfree(bucket); 3641cc5c073aSDavid Ahern } 3642cc5c073aSDavid Ahern 3643cc5c073aSDavid Ahern rcu_read_unlock(); 3644cc5c073aSDavid Ahern 364561308050SNikolay Aleksandrov fib6_nh_release_dsts(fib6_nh); 3646f40b6ae2SDavid Ahern free_percpu(fib6_nh->rt6i_pcpu); 3647f40b6ae2SDavid Ahern 3648979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3649dac7d0f2SDavid Ahern } 3650dac7d0f2SDavid Ahern 36518837cbbfSNikolay Aleksandrov void fib6_nh_release_dsts(struct fib6_nh *fib6_nh) 36528837cbbfSNikolay Aleksandrov { 36538837cbbfSNikolay Aleksandrov int cpu; 36548837cbbfSNikolay Aleksandrov 36558837cbbfSNikolay Aleksandrov if (!fib6_nh->rt6i_pcpu) 36568837cbbfSNikolay Aleksandrov return; 36578837cbbfSNikolay Aleksandrov 36588837cbbfSNikolay Aleksandrov for_each_possible_cpu(cpu) { 36598837cbbfSNikolay Aleksandrov struct rt6_info *pcpu_rt, **ppcpu_rt; 36608837cbbfSNikolay Aleksandrov 36618837cbbfSNikolay Aleksandrov ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 36628837cbbfSNikolay Aleksandrov pcpu_rt = xchg(ppcpu_rt, NULL); 36638837cbbfSNikolay Aleksandrov if (pcpu_rt) { 36648837cbbfSNikolay Aleksandrov dst_dev_put(&pcpu_rt->dst); 36658837cbbfSNikolay Aleksandrov dst_release(&pcpu_rt->dst); 36668837cbbfSNikolay Aleksandrov } 36678837cbbfSNikolay Aleksandrov } 36688837cbbfSNikolay Aleksandrov } 36698837cbbfSNikolay Aleksandrov 36708d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3671acb54e3cSDavid Ahern gfp_t gfp_flags, 3672333c4301SDavid Ahern struct netlink_ext_ack *extack) 36731da177e4SLinus Torvalds { 36745578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 36758d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3676f88d8ea6SDavid Ahern struct nexthop *nh = NULL; 3677c71099acSThomas Graf struct fib6_table *table; 3678f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 36798c5b83f0SRoopa Prabhu int err = -EINVAL; 368083c44251SDavid Ahern int addr_type; 36811da177e4SLinus Torvalds 3682557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3683d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3684d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3685557c44beSDavid Ahern goto out; 3686d5d531cbSDavid Ahern } 3687557c44beSDavid Ahern 36882ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 36892ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 36902ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 36912ea2352eSWei Wang goto out; 36922ea2352eSWei Wang } 36932ea2352eSWei Wang 3694e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3695e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3696e8478e80SDavid Ahern goto out; 3697e8478e80SDavid Ahern } 3698e8478e80SDavid Ahern 3699d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3700d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 37018c5b83f0SRoopa Prabhu goto out; 3702d5d531cbSDavid Ahern } 3703d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3704d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3705d5d531cbSDavid Ahern goto out; 3706d5d531cbSDavid Ahern } 37071da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3708d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3709d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3710d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 37118c5b83f0SRoopa Prabhu goto out; 3712d5d531cbSDavid Ahern } 37131da177e4SLinus Torvalds #endif 37145b98324eSDavid Ahern if (cfg->fc_nh_id) { 37155b98324eSDavid Ahern nh = nexthop_find_by_id(net, cfg->fc_nh_id); 37165b98324eSDavid Ahern if (!nh) { 37175b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 37185b98324eSDavid Ahern goto out; 37195b98324eSDavid Ahern } 37205b98324eSDavid Ahern err = fib6_check_nexthop(nh, cfg, extack); 37215b98324eSDavid Ahern if (err) 37225b98324eSDavid Ahern goto out; 37235b98324eSDavid Ahern } 3724fc1e64e1SDavid Ahern 3725c71099acSThomas Graf err = -ENOBUFS; 372638308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3727d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3728d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 372938308473SDavid S. Miller if (!table) { 3730f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3731d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3732d71314b4SMatti Vaittinen } 3733d71314b4SMatti Vaittinen } else { 3734d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3735d71314b4SMatti Vaittinen } 373638308473SDavid S. Miller 373738308473SDavid S. Miller if (!table) 3738c71099acSThomas Graf goto out; 3739c71099acSThomas Graf 37401da177e4SLinus Torvalds err = -ENOMEM; 3741f88d8ea6SDavid Ahern rt = fib6_info_alloc(gfp_flags, !nh); 374293531c67SDavid Ahern if (!rt) 37431da177e4SLinus Torvalds goto out; 374493531c67SDavid Ahern 3745d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3746d7e774f3SDavid Ahern extack); 3747767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3748767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3749fda21d46SEric Dumazet /* Do not leave garbage there. */ 3750fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 37518fb4792fSPaolo Abeni goto out_free; 3752767a2217SDavid Ahern } 3753767a2217SDavid Ahern 375493531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 375593531c67SDavid Ahern rt->dst_nocount = true; 37561da177e4SLinus Torvalds 37571716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 375814895687SDavid Ahern fib6_set_expires(rt, jiffies + 37591716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 37601716a961SGao feng else 376114895687SDavid Ahern fib6_clean_expires(rt); 37621da177e4SLinus Torvalds 376386872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 376486872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 376593c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 376686872cb5SThomas Graf 376783c44251SDavid Ahern rt->fib6_table = table; 376883c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 3769c7036d97SDavid Ahern rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; 37702b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 377119e42e45SRoopa Prabhu 377293c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 377393c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 37741da177e4SLinus Torvalds 37751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 377693c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 377793c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 37781da177e4SLinus Torvalds #endif 3779f88d8ea6SDavid Ahern if (nh) { 3780f88d8ea6SDavid Ahern if (rt->fib6_src.plen) { 37814daa95afSColin Ian King NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); 3782821bbf79SCoco Li goto out_free; 3783f88d8ea6SDavid Ahern } 3784706ec919SXiyu Yang if (!nexthop_get(nh)) { 3785706ec919SXiyu Yang NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 3786821bbf79SCoco Li goto out_free; 3787706ec919SXiyu Yang } 3788f88d8ea6SDavid Ahern rt->nh = nh; 3789f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(rt->nh); 3790f88d8ea6SDavid Ahern } else { 37911cf844c7SDavid Ahern err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); 37921da177e4SLinus Torvalds if (err) 37931da177e4SLinus Torvalds goto out; 37949fbb704cSDavid Ahern 3795f88d8ea6SDavid Ahern fib6_nh = rt->fib6_nh; 3796f88d8ea6SDavid Ahern 3797f88d8ea6SDavid Ahern /* We cannot add true routes via loopback here, they would 3798f88d8ea6SDavid Ahern * result in kernel looping; promote them to reject routes 379983c44251SDavid Ahern */ 380083c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3801f88d8ea6SDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, 3802f88d8ea6SDavid Ahern addr_type)) 380383c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3804f88d8ea6SDavid Ahern } 3805955ec4cbSDavid Ahern 3806c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3807f88d8ea6SDavid Ahern struct net_device *dev = fib6_nh->fib_nh_dev; 380883c44251SDavid Ahern 3809c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3810d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3811c3968a85SDaniel Walter err = -EINVAL; 3812c3968a85SDaniel Walter goto out; 3813c3968a85SDaniel Walter } 381493c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 381593c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3816c3968a85SDaniel Walter } else 381793c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3818c3968a85SDaniel Walter 38198c5b83f0SRoopa Prabhu return rt; 38201da177e4SLinus Torvalds out: 382193531c67SDavid Ahern fib6_info_release(rt); 38228c5b83f0SRoopa Prabhu return ERR_PTR(err); 3823821bbf79SCoco Li out_free: 3824821bbf79SCoco Li ip_fib_metrics_put(rt->fib6_metrics); 3825821bbf79SCoco Li kfree(rt); 3826821bbf79SCoco Li return ERR_PTR(err); 38276b9ea5a6SRoopa Prabhu } 38286b9ea5a6SRoopa Prabhu 3829acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3830333c4301SDavid Ahern struct netlink_ext_ack *extack) 38316b9ea5a6SRoopa Prabhu { 38328d1c802bSDavid Ahern struct fib6_info *rt; 38336b9ea5a6SRoopa Prabhu int err; 38346b9ea5a6SRoopa Prabhu 3835acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3836d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3837d4ead6b3SDavid Ahern return PTR_ERR(rt); 38386b9ea5a6SRoopa Prabhu 3839d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 384093531c67SDavid Ahern fib6_info_release(rt); 38416b9ea5a6SRoopa Prabhu 38421da177e4SLinus Torvalds return err; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 38458d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 38461da177e4SLinus Torvalds { 3847afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3848c71099acSThomas Graf struct fib6_table *table; 3849afb1d4b5SDavid Ahern int err; 38501da177e4SLinus Torvalds 3851421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 38526825a26cSGao feng err = -ENOENT; 38536825a26cSGao feng goto out; 38546825a26cSGao feng } 38556c813a72SPatrick McHardy 385693c2fb25SDavid Ahern table = rt->fib6_table; 385766f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 385886872cb5SThomas Graf err = fib6_del(rt, info); 385966f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 38601da177e4SLinus Torvalds 38616825a26cSGao feng out: 386293531c67SDavid Ahern fib6_info_release(rt); 38631da177e4SLinus Torvalds return err; 38641da177e4SLinus Torvalds } 38651da177e4SLinus Torvalds 386611dd74b3SRoopa Prabhu int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify) 3867e0a1ad73SThomas Graf { 386811dd74b3SRoopa Prabhu struct nl_info info = { 386911dd74b3SRoopa Prabhu .nl_net = net, 387011dd74b3SRoopa Prabhu .skip_notify = skip_notify 387111dd74b3SRoopa Prabhu }; 3872afb1d4b5SDavid Ahern 3873528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3874e0a1ad73SThomas Graf } 3875e0a1ad73SThomas Graf 38768d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 38770ae81335SDavid Ahern { 38780ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3879e3330039SWANG Cong struct net *net = info->nl_net; 388016a16cd3SDavid Ahern struct sk_buff *skb = NULL; 38810ae81335SDavid Ahern struct fib6_table *table; 3882e3330039SWANG Cong int err = -ENOENT; 38830ae81335SDavid Ahern 3884421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3885e3330039SWANG Cong goto out_put; 388693c2fb25SDavid Ahern table = rt->fib6_table; 388766f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 38880ae81335SDavid Ahern 388993c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 38908d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 38910284696bSIdo Schimmel struct fib6_node *fn; 38920ae81335SDavid Ahern 389316a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 389416a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 389516a16cd3SDavid Ahern if (skb) { 389616a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 389716a16cd3SDavid Ahern 3898d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 389916a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 390016a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 390116a16cd3SDavid Ahern kfree_skb(skb); 390216a16cd3SDavid Ahern skb = NULL; 390316a16cd3SDavid Ahern } else 390416a16cd3SDavid Ahern info->skip_notify = 1; 390516a16cd3SDavid Ahern } 390616a16cd3SDavid Ahern 39070284696bSIdo Schimmel /* 'rt' points to the first sibling route. If it is not the 39080284696bSIdo Schimmel * leaf, then we do not need to send a notification. Otherwise, 39090284696bSIdo Schimmel * we need to check if the last sibling has a next route or not 39100284696bSIdo Schimmel * and emit a replace or delete notification, respectively. 39110284696bSIdo Schimmel */ 39122881fd61SIdo Schimmel info->skip_notify_kernel = 1; 39130284696bSIdo Schimmel fn = rcu_dereference_protected(rt->fib6_node, 39140284696bSIdo Schimmel lockdep_is_held(&table->tb6_lock)); 39150284696bSIdo Schimmel if (rcu_access_pointer(fn->leaf) == rt) { 39160284696bSIdo Schimmel struct fib6_info *last_sibling, *replace_rt; 39170284696bSIdo Schimmel 39180284696bSIdo Schimmel last_sibling = list_last_entry(&rt->fib6_siblings, 39190284696bSIdo Schimmel struct fib6_info, 39200284696bSIdo Schimmel fib6_siblings); 39210284696bSIdo Schimmel replace_rt = rcu_dereference_protected( 39220284696bSIdo Schimmel last_sibling->fib6_next, 39230284696bSIdo Schimmel lockdep_is_held(&table->tb6_lock)); 39240284696bSIdo Schimmel if (replace_rt) 39250284696bSIdo Schimmel call_fib6_entry_notifiers_replace(net, 39260284696bSIdo Schimmel replace_rt); 39270284696bSIdo Schimmel else 39280284696bSIdo Schimmel call_fib6_multipath_entry_notifiers(net, 3929caafb250SIdo Schimmel FIB_EVENT_ENTRY_DEL, 39300284696bSIdo Schimmel rt, rt->fib6_nsiblings, 39310284696bSIdo Schimmel NULL); 39320284696bSIdo Schimmel } 39330ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 393493c2fb25SDavid Ahern &rt->fib6_siblings, 393593c2fb25SDavid Ahern fib6_siblings) { 39360ae81335SDavid Ahern err = fib6_del(sibling, info); 39370ae81335SDavid Ahern if (err) 3938e3330039SWANG Cong goto out_unlock; 39390ae81335SDavid Ahern } 39400ae81335SDavid Ahern } 39410ae81335SDavid Ahern 39420ae81335SDavid Ahern err = fib6_del(rt, info); 3943e3330039SWANG Cong out_unlock: 394466f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3945e3330039SWANG Cong out_put: 394693531c67SDavid Ahern fib6_info_release(rt); 394716a16cd3SDavid Ahern 394816a16cd3SDavid Ahern if (skb) { 3949e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 395016a16cd3SDavid Ahern info->nlh, gfp_any()); 395116a16cd3SDavid Ahern } 39520ae81335SDavid Ahern return err; 39530ae81335SDavid Ahern } 39540ae81335SDavid Ahern 39550fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 395623fb93a4SDavid Ahern { 395723fb93a4SDavid Ahern int rc = -ESRCH; 395823fb93a4SDavid Ahern 395923fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 396023fb93a4SDavid Ahern goto out; 396123fb93a4SDavid Ahern 396223fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 396323fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 396423fb93a4SDavid Ahern goto out; 3965761f6026SXin Long 396623fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 396723fb93a4SDavid Ahern out: 396823fb93a4SDavid Ahern return rc; 396923fb93a4SDavid Ahern } 397023fb93a4SDavid Ahern 39710fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, 39720fa6efc5SDavid Ahern struct fib6_nh *nh) 39730fa6efc5SDavid Ahern { 39740fa6efc5SDavid Ahern struct fib6_result res = { 39750fa6efc5SDavid Ahern .f6i = rt, 39760fa6efc5SDavid Ahern .nh = nh, 39770fa6efc5SDavid Ahern }; 39780fa6efc5SDavid Ahern struct rt6_info *rt_cache; 39790fa6efc5SDavid Ahern 39800fa6efc5SDavid Ahern rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); 39810fa6efc5SDavid Ahern if (rt_cache) 39820fa6efc5SDavid Ahern return __ip6_del_cached_rt(rt_cache, cfg); 39830fa6efc5SDavid Ahern 39840fa6efc5SDavid Ahern return 0; 39850fa6efc5SDavid Ahern } 39860fa6efc5SDavid Ahern 39875b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg { 39885b98324eSDavid Ahern struct fib6_config *cfg; 39895b98324eSDavid Ahern struct fib6_info *f6i; 39905b98324eSDavid Ahern }; 39915b98324eSDavid Ahern 39925b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) 39935b98324eSDavid Ahern { 39945b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg *arg = _arg; 39955b98324eSDavid Ahern int rc; 39965b98324eSDavid Ahern 39975b98324eSDavid Ahern rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); 39985b98324eSDavid Ahern return rc != -ESRCH ? rc : 0; 39995b98324eSDavid Ahern } 40005b98324eSDavid Ahern 40015b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) 40025b98324eSDavid Ahern { 40035b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg arg = { 40045b98324eSDavid Ahern .cfg = cfg, 40055b98324eSDavid Ahern .f6i = f6i 40065b98324eSDavid Ahern }; 40075b98324eSDavid Ahern 40085b98324eSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); 40095b98324eSDavid Ahern } 40105b98324eSDavid Ahern 4011333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 4012333c4301SDavid Ahern struct netlink_ext_ack *extack) 40131da177e4SLinus Torvalds { 4014c71099acSThomas Graf struct fib6_table *table; 40158d1c802bSDavid Ahern struct fib6_info *rt; 40161da177e4SLinus Torvalds struct fib6_node *fn; 40171da177e4SLinus Torvalds int err = -ESRCH; 40181da177e4SLinus Torvalds 40195578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 4020d5d531cbSDavid Ahern if (!table) { 4021d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 4022c71099acSThomas Graf return err; 4023d5d531cbSDavid Ahern } 40241da177e4SLinus Torvalds 402566f5d6ceSWei Wang rcu_read_lock(); 4026c71099acSThomas Graf 4027c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 402886872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 402938fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 40302b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 40311da177e4SLinus Torvalds 40321da177e4SLinus Torvalds if (fn) { 403366f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 4034ad1601aeSDavid Ahern struct fib6_nh *nh; 4035ad1601aeSDavid Ahern 40363401bfb1SStefano Brivio if (rt->nh && cfg->fc_nh_id && 40373401bfb1SStefano Brivio rt->nh->id != cfg->fc_nh_id) 40385b98324eSDavid Ahern continue; 403923fb93a4SDavid Ahern 40405b98324eSDavid Ahern if (cfg->fc_flags & RTF_CACHE) { 40415b98324eSDavid Ahern int rc = 0; 40425b98324eSDavid Ahern 40435b98324eSDavid Ahern if (rt->nh) { 40445b98324eSDavid Ahern rc = ip6_del_cached_rt_nh(cfg, rt); 40455b98324eSDavid Ahern } else if (cfg->fc_nh_id) { 40465b98324eSDavid Ahern continue; 40475b98324eSDavid Ahern } else { 40485b98324eSDavid Ahern nh = rt->fib6_nh; 40490fa6efc5SDavid Ahern rc = ip6_del_cached_rt(cfg, rt, nh); 40505b98324eSDavid Ahern } 40519e575010SEric Dumazet if (rc != -ESRCH) { 40529e575010SEric Dumazet rcu_read_unlock(); 405323fb93a4SDavid Ahern return rc; 405423fb93a4SDavid Ahern } 40551f56a01fSMartin KaFai Lau continue; 40562b760fcfSWei Wang } 4057ad1601aeSDavid Ahern 40585b98324eSDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 40595b98324eSDavid Ahern continue; 40605b98324eSDavid Ahern if (cfg->fc_protocol && 40615b98324eSDavid Ahern cfg->fc_protocol != rt->fib6_protocol) 40625b98324eSDavid Ahern continue; 40635b98324eSDavid Ahern 40645b98324eSDavid Ahern if (rt->nh) { 40655b98324eSDavid Ahern if (!fib6_info_hold_safe(rt)) 40665b98324eSDavid Ahern continue; 40675b98324eSDavid Ahern rcu_read_unlock(); 40685b98324eSDavid Ahern 40695b98324eSDavid Ahern return __ip6_del_rt(rt, &cfg->fc_nlinfo); 40705b98324eSDavid Ahern } 40715b98324eSDavid Ahern if (cfg->fc_nh_id) 40725b98324eSDavid Ahern continue; 40735b98324eSDavid Ahern 40745b98324eSDavid Ahern nh = rt->fib6_nh; 407586872cb5SThomas Graf if (cfg->fc_ifindex && 4076ad1601aeSDavid Ahern (!nh->fib_nh_dev || 4077ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 40781da177e4SLinus Torvalds continue; 407986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 4080ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 40811da177e4SLinus Torvalds continue; 4082e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 4083e873e4b9SWei Wang continue; 408466f5d6ceSWei Wang rcu_read_unlock(); 40851da177e4SLinus Torvalds 40860ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 40870ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 408886872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 40890ae81335SDavid Ahern 40900ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 40911da177e4SLinus Torvalds } 40921da177e4SLinus Torvalds } 409366f5d6ceSWei Wang rcu_read_unlock(); 40941da177e4SLinus Torvalds 40951da177e4SLinus Torvalds return err; 40961da177e4SLinus Torvalds } 40971da177e4SLinus Torvalds 40986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 4099a6279458SYOSHIFUJI Hideaki { 4100a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 4101e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 410285bd05deSDavid Ahern struct fib6_result res = {}; 4103e8599ff4SDavid S. Miller struct ndisc_options ndopts; 4104e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 4105e8599ff4SDavid S. Miller struct neighbour *neigh; 410671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 41076e157b6aSDavid S. Miller int optlen, on_link; 41086e157b6aSDavid S. Miller u8 *lladdr; 4109e8599ff4SDavid S. Miller 411029a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 411171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 4112e8599ff4SDavid S. Miller 4113e8599ff4SDavid S. Miller if (optlen < 0) { 41146e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 4115e8599ff4SDavid S. Miller return; 4116e8599ff4SDavid S. Miller } 4117e8599ff4SDavid S. Miller 411871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 4119e8599ff4SDavid S. Miller 412071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 41216e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 4122e8599ff4SDavid S. Miller return; 4123e8599ff4SDavid S. Miller } 4124e8599ff4SDavid S. Miller 41256e157b6aSDavid S. Miller on_link = 0; 412671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 4127e8599ff4SDavid S. Miller on_link = 1; 412871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 4129e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 41306e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 4131e8599ff4SDavid S. Miller return; 4132e8599ff4SDavid S. Miller } 4133e8599ff4SDavid S. Miller 4134e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 4135e8599ff4SDavid S. Miller if (!in6_dev) 4136e8599ff4SDavid S. Miller return; 4137e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 4138e8599ff4SDavid S. Miller return; 4139e8599ff4SDavid S. Miller 4140e8599ff4SDavid S. Miller /* RFC2461 8.1: 4141e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 4142e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 4143e8599ff4SDavid S. Miller */ 4144e8599ff4SDavid S. Miller 4145f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 4146e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 4147e8599ff4SDavid S. Miller return; 4148e8599ff4SDavid S. Miller } 41496e157b6aSDavid S. Miller 41506e157b6aSDavid S. Miller lladdr = NULL; 4151e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 4152e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 4153e8599ff4SDavid S. Miller skb->dev); 4154e8599ff4SDavid S. Miller if (!lladdr) { 4155e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 4156e8599ff4SDavid S. Miller return; 4157e8599ff4SDavid S. Miller } 4158e8599ff4SDavid S. Miller } 4159e8599ff4SDavid S. Miller 41606e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 4161ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 41626e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 41636e157b6aSDavid S. Miller return; 41646e157b6aSDavid S. Miller } 41656e157b6aSDavid S. Miller 41666e157b6aSDavid S. Miller /* Redirect received -> path was valid. 41676e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 41686e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 41696e157b6aSDavid S. Miller */ 41700dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 41716e157b6aSDavid S. Miller 417271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 4173e8599ff4SDavid S. Miller if (!neigh) 4174e8599ff4SDavid S. Miller return; 4175e8599ff4SDavid S. Miller 41761da177e4SLinus Torvalds /* 41771da177e4SLinus Torvalds * We have finally decided to accept it. 41781da177e4SLinus Torvalds */ 41791da177e4SLinus Torvalds 4180f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 41811da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 41821da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 41831da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 4184f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 4185f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 41861da177e4SLinus Torvalds 41874d85cd0cSDavid Ahern rcu_read_lock(); 418885bd05deSDavid Ahern res.f6i = rcu_dereference(rt->from); 4189ff24e498SDavid S. Miller if (!res.f6i) 4190886b7a50SMartin KaFai Lau goto out; 41918a14e46fSDavid Ahern 419249d5b8efSDavid Ahern if (res.f6i->nh) { 419349d5b8efSDavid Ahern struct fib6_nh_match_arg arg = { 419449d5b8efSDavid Ahern .dev = dst->dev, 419549d5b8efSDavid Ahern .gw = &rt->rt6i_gateway, 419649d5b8efSDavid Ahern }; 419749d5b8efSDavid Ahern 419849d5b8efSDavid Ahern nexthop_for_each_fib6_nh(res.f6i->nh, 419949d5b8efSDavid Ahern fib6_nh_find_match, &arg); 420049d5b8efSDavid Ahern 420149d5b8efSDavid Ahern /* fib6_info uses a nexthop that does not have fib6_nh 420249d5b8efSDavid Ahern * using the dst->dev. Should be impossible 420349d5b8efSDavid Ahern */ 420449d5b8efSDavid Ahern if (!arg.match) 420549d5b8efSDavid Ahern goto out; 420649d5b8efSDavid Ahern res.nh = arg.match; 420749d5b8efSDavid Ahern } else { 42081cf844c7SDavid Ahern res.nh = res.f6i->fib6_nh; 420949d5b8efSDavid Ahern } 421049d5b8efSDavid Ahern 42117d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 42127d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 421385bd05deSDavid Ahern nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 421438308473SDavid S. Miller if (!nrt) 42151da177e4SLinus Torvalds goto out; 42161da177e4SLinus Torvalds 42171da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 42181da177e4SLinus Torvalds if (on_link) 42191da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 42201da177e4SLinus Torvalds 42214e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 42221da177e4SLinus Torvalds 4223886b7a50SMartin KaFai Lau /* rt6_insert_exception() will take care of duplicated exceptions */ 42245012f0a5SDavid Ahern if (rt6_insert_exception(nrt, &res)) { 42252b760fcfSWei Wang dst_release_immediate(&nrt->dst); 42262b760fcfSWei Wang goto out; 42272b760fcfSWei Wang } 42281da177e4SLinus Torvalds 4229d8d1f30bSChangli Gao netevent.old = &rt->dst; 4230d8d1f30bSChangli Gao netevent.new = &nrt->dst; 423171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 423260592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 42338d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 42348d71740cSTom Tucker 42351da177e4SLinus Torvalds out: 4236886b7a50SMartin KaFai Lau rcu_read_unlock(); 4237e8599ff4SDavid S. Miller neigh_release(neigh); 42386e157b6aSDavid S. Miller } 42396e157b6aSDavid S. Miller 424070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 42418d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 4242b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4243830218c1SDavid Ahern const struct in6_addr *gwaddr, 4244830218c1SDavid Ahern struct net_device *dev) 424570ceb4f5SYOSHIFUJI Hideaki { 4246830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 4247830218c1SDavid Ahern int ifindex = dev->ifindex; 424870ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 42498d1c802bSDavid Ahern struct fib6_info *rt = NULL; 4250c71099acSThomas Graf struct fib6_table *table; 425170ceb4f5SYOSHIFUJI Hideaki 4252830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 425338308473SDavid S. Miller if (!table) 4254c71099acSThomas Graf return NULL; 4255c71099acSThomas Graf 425666f5d6ceSWei Wang rcu_read_lock(); 425738fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 425870ceb4f5SYOSHIFUJI Hideaki if (!fn) 425970ceb4f5SYOSHIFUJI Hideaki goto out; 426070ceb4f5SYOSHIFUJI Hideaki 426166f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 4262f88d8ea6SDavid Ahern /* these routes do not use nexthops */ 4263f88d8ea6SDavid Ahern if (rt->nh) 4264f88d8ea6SDavid Ahern continue; 42651cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) 426670ceb4f5SYOSHIFUJI Hideaki continue; 42672b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 42681cf844c7SDavid Ahern !rt->fib6_nh->fib_nh_gw_family) 426970ceb4f5SYOSHIFUJI Hideaki continue; 42701cf844c7SDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) 427170ceb4f5SYOSHIFUJI Hideaki continue; 4272e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 4273e873e4b9SWei Wang continue; 427470ceb4f5SYOSHIFUJI Hideaki break; 427570ceb4f5SYOSHIFUJI Hideaki } 427670ceb4f5SYOSHIFUJI Hideaki out: 427766f5d6ceSWei Wang rcu_read_unlock(); 427870ceb4f5SYOSHIFUJI Hideaki return rt; 427970ceb4f5SYOSHIFUJI Hideaki } 428070ceb4f5SYOSHIFUJI Hideaki 42818d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 4282b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 4283830218c1SDavid Ahern const struct in6_addr *gwaddr, 4284830218c1SDavid Ahern struct net_device *dev, 428595c96174SEric Dumazet unsigned int pref) 428670ceb4f5SYOSHIFUJI Hideaki { 428786872cb5SThomas Graf struct fib6_config cfg = { 4288238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 4289830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 429086872cb5SThomas Graf .fc_dst_len = prefixlen, 429186872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 429286872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 4293b91d5329SXin Long .fc_protocol = RTPROT_RA, 4294e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 429515e47304SEric W. Biederman .fc_nlinfo.portid = 0, 4296efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 4297efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 429886872cb5SThomas Graf }; 429970ceb4f5SYOSHIFUJI Hideaki 430091b2c9a0SXu Wang cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 43014e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 43024e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 430386872cb5SThomas Graf 4304e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 4305e317da96SYOSHIFUJI Hideaki if (!prefixlen) 430686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 430770ceb4f5SYOSHIFUJI Hideaki 4308acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 430970ceb4f5SYOSHIFUJI Hideaki 4310830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 431170ceb4f5SYOSHIFUJI Hideaki } 431270ceb4f5SYOSHIFUJI Hideaki #endif 431370ceb4f5SYOSHIFUJI Hideaki 43148d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 4315afb1d4b5SDavid Ahern const struct in6_addr *addr, 4316afb1d4b5SDavid Ahern struct net_device *dev) 43171da177e4SLinus Torvalds { 4318830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 43198d1c802bSDavid Ahern struct fib6_info *rt; 4320c71099acSThomas Graf struct fib6_table *table; 43211da177e4SLinus Torvalds 4322afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 432338308473SDavid S. Miller if (!table) 4324c71099acSThomas Graf return NULL; 43251da177e4SLinus Torvalds 432666f5d6ceSWei Wang rcu_read_lock(); 432766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4328f88d8ea6SDavid Ahern struct fib6_nh *nh; 4329ad1601aeSDavid Ahern 4330f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4331f88d8ea6SDavid Ahern if (rt->nh) 4332f88d8ea6SDavid Ahern continue; 4333f88d8ea6SDavid Ahern 4334f88d8ea6SDavid Ahern nh = rt->fib6_nh; 4335ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 433693c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 4337ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 43381da177e4SLinus Torvalds break; 43391da177e4SLinus Torvalds } 4340e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 4341e873e4b9SWei Wang rt = NULL; 434266f5d6ceSWei Wang rcu_read_unlock(); 43431da177e4SLinus Torvalds return rt; 43441da177e4SLinus Torvalds } 43451da177e4SLinus Torvalds 43468d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 4347afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 4348ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 43496b2e04bcSPraveen Chaudhary unsigned int pref, 43506b2e04bcSPraveen Chaudhary u32 defrtr_usr_metric) 43511da177e4SLinus Torvalds { 435286872cb5SThomas Graf struct fib6_config cfg = { 4353ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 43546b2e04bcSPraveen Chaudhary .fc_metric = defrtr_usr_metric, 435586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 435686872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 435786872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 4358b91d5329SXin Long .fc_protocol = RTPROT_RA, 4359e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 436015e47304SEric W. Biederman .fc_nlinfo.portid = 0, 43615578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 4362afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 436386872cb5SThomas Graf }; 43641da177e4SLinus Torvalds 43654e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 43661da177e4SLinus Torvalds 4367acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 4368830218c1SDavid Ahern struct fib6_table *table; 4369830218c1SDavid Ahern 4370830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 4371830218c1SDavid Ahern if (table) 4372830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 4373830218c1SDavid Ahern } 43741da177e4SLinus Torvalds 4375afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 43761da177e4SLinus Torvalds } 43771da177e4SLinus Torvalds 4378afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 4379afb1d4b5SDavid Ahern struct fib6_table *table) 43801da177e4SLinus Torvalds { 43818d1c802bSDavid Ahern struct fib6_info *rt; 43821da177e4SLinus Torvalds 43831da177e4SLinus Torvalds restart: 438466f5d6ceSWei Wang rcu_read_lock(); 438566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4386dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 4387dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 4388dcd1f572SDavid Ahern 438993c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 4390e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 4391e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 439266f5d6ceSWei Wang rcu_read_unlock(); 439311dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 43941da177e4SLinus Torvalds goto restart; 43951da177e4SLinus Torvalds } 43961da177e4SLinus Torvalds } 439766f5d6ceSWei Wang rcu_read_unlock(); 4398830218c1SDavid Ahern 4399830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 4400830218c1SDavid Ahern } 4401830218c1SDavid Ahern 4402830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 4403830218c1SDavid Ahern { 4404830218c1SDavid Ahern struct fib6_table *table; 4405830218c1SDavid Ahern struct hlist_head *head; 4406830218c1SDavid Ahern unsigned int h; 4407830218c1SDavid Ahern 4408830218c1SDavid Ahern rcu_read_lock(); 4409830218c1SDavid Ahern 4410830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 4411830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 4412830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 4413830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 4414afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 4415830218c1SDavid Ahern } 4416830218c1SDavid Ahern } 4417830218c1SDavid Ahern 4418830218c1SDavid Ahern rcu_read_unlock(); 44191da177e4SLinus Torvalds } 44201da177e4SLinus Torvalds 44215578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 44225578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 442386872cb5SThomas Graf struct fib6_config *cfg) 442486872cb5SThomas Graf { 44258823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 44268823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 44278823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 44288823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 442967f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 44308823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 44318823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 44328823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 44338823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 44348823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 443586872cb5SThomas Graf 44368823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 443786872cb5SThomas Graf 44388823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 44398823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 44408823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 44418823a3acSMaciej Żenczykowski }; 444286872cb5SThomas Graf } 444386872cb5SThomas Graf 44447c1552daSChristoph Hellwig int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg) 44451da177e4SLinus Torvalds { 444686872cb5SThomas Graf struct fib6_config cfg; 44471da177e4SLinus Torvalds int err; 44481da177e4SLinus Torvalds 44497c1552daSChristoph Hellwig if (cmd != SIOCADDRT && cmd != SIOCDELRT) 44507c1552daSChristoph Hellwig return -EINVAL; 4451af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 44521da177e4SLinus Torvalds return -EPERM; 44531da177e4SLinus Torvalds 44547c1552daSChristoph Hellwig rtmsg_to_fib6_config(net, rtmsg, &cfg); 445586872cb5SThomas Graf 44561da177e4SLinus Torvalds rtnl_lock(); 44571da177e4SLinus Torvalds switch (cmd) { 44581da177e4SLinus Torvalds case SIOCADDRT: 4459acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 44601da177e4SLinus Torvalds break; 44611da177e4SLinus Torvalds case SIOCDELRT: 4462333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 44631da177e4SLinus Torvalds break; 44641da177e4SLinus Torvalds } 44651da177e4SLinus Torvalds rtnl_unlock(); 44661da177e4SLinus Torvalds return err; 44673ff50b79SStephen Hemminger } 44681da177e4SLinus Torvalds 44691da177e4SLinus Torvalds /* 44701da177e4SLinus Torvalds * Drop the packet on the floor 44711da177e4SLinus Torvalds */ 44721da177e4SLinus Torvalds 4473d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 44741da177e4SLinus Torvalds { 4475adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 44761d3fd8a1SStephen Suryaputra struct net *net = dev_net(dst->dev); 44771d3fd8a1SStephen Suryaputra struct inet6_dev *idev; 44783ae42cc8SMenglong Dong SKB_DR(reason); 44791d3fd8a1SStephen Suryaputra int type; 44801d3fd8a1SStephen Suryaputra 44811158f79fSDavid Ahern if (netif_is_l3_master(skb->dev) || 44821d3fd8a1SStephen Suryaputra dst->dev == net->loopback_dev) 44831d3fd8a1SStephen Suryaputra idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 44841d3fd8a1SStephen Suryaputra else 44851d3fd8a1SStephen Suryaputra idev = ip6_dst_idev(dst); 44861d3fd8a1SStephen Suryaputra 4487612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 4488612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 44890660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 449045bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 44913ae42cc8SMenglong Dong SKB_DR_SET(reason, IP_INADDRERRORS); 44921d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 4493612f09e8SYOSHIFUJI Hideaki break; 4494612f09e8SYOSHIFUJI Hideaki } 44953ae42cc8SMenglong Dong SKB_DR_SET(reason, IP_INNOROUTES); 4496a8eceea8SJoe Perches fallthrough; 4497612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 44983ae42cc8SMenglong Dong SKB_DR_OR(reason, IP_OUTNOROUTES); 44991d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 4500612f09e8SYOSHIFUJI Hideaki break; 4501612f09e8SYOSHIFUJI Hideaki } 45021d3fd8a1SStephen Suryaputra 45031d3fd8a1SStephen Suryaputra /* Start over by dropping the dst for l3mdev case */ 45041d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev)) 45051d3fd8a1SStephen Suryaputra skb_dst_drop(skb); 45061d3fd8a1SStephen Suryaputra 45073ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 45083ae42cc8SMenglong Dong kfree_skb_reason(skb, reason); 45091da177e4SLinus Torvalds return 0; 45101da177e4SLinus Torvalds } 45111da177e4SLinus Torvalds 45129ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 45139ce8ade0SThomas Graf { 4514612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 45159ce8ade0SThomas Graf } 45169ce8ade0SThomas Graf 4517ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 45181da177e4SLinus Torvalds { 4519adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4520612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 45211da177e4SLinus Torvalds } 45221da177e4SLinus Torvalds 45239ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 45249ce8ade0SThomas Graf { 4525612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 45269ce8ade0SThomas Graf } 45279ce8ade0SThomas Graf 4528ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 45299ce8ade0SThomas Graf { 4530adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4531612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 45329ce8ade0SThomas Graf } 45339ce8ade0SThomas Graf 45341da177e4SLinus Torvalds /* 45351da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 45361da177e4SLinus Torvalds */ 45371da177e4SLinus Torvalds 4538360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 4539afb1d4b5SDavid Ahern struct inet6_dev *idev, 45401da177e4SLinus Torvalds const struct in6_addr *addr, 4541acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 45421da177e4SLinus Torvalds { 4543c7a1ce39SDavid Ahern struct fib6_config cfg = { 4544c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 4545c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 4546d55a2e37SMaciej Żenczykowski .fc_flags = RTF_UP | RTF_NONEXTHOP, 4547c7a1ce39SDavid Ahern .fc_dst = *addr, 4548c7a1ce39SDavid Ahern .fc_dst_len = 128, 4549c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 4550c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 4551c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 4552c7a1ce39SDavid Ahern }; 4553d55a2e37SMaciej Żenczykowski struct fib6_info *f6i; 45545f02ce24SDavid Ahern 4555e8478e80SDavid Ahern if (anycast) { 4556c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 4557c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 4558e8478e80SDavid Ahern } else { 4559c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 4560c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 4561e8478e80SDavid Ahern } 45621da177e4SLinus Torvalds 4563d55a2e37SMaciej Żenczykowski f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); 45643b0dc529SNicolas Dichtel if (!IS_ERR(f6i)) { 4565d55a2e37SMaciej Żenczykowski f6i->dst_nocount = true; 45663b0dc529SNicolas Dichtel 45673b0dc529SNicolas Dichtel if (!anycast && 45683b0dc529SNicolas Dichtel (net->ipv6.devconf_all->disable_policy || 45693b0dc529SNicolas Dichtel idev->cnf.disable_policy)) 45703b0dc529SNicolas Dichtel f6i->dst_nopolicy = true; 45713b0dc529SNicolas Dichtel } 45723b0dc529SNicolas Dichtel 4573d55a2e37SMaciej Żenczykowski return f6i; 45741da177e4SLinus Torvalds } 45751da177e4SLinus Torvalds 4576c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 4577c3968a85SDaniel Walter struct arg_dev_net_ip { 4578c3968a85SDaniel Walter struct net_device *dev; 4579c3968a85SDaniel Walter struct net *net; 4580c3968a85SDaniel Walter struct in6_addr *addr; 4581c3968a85SDaniel Walter }; 4582c3968a85SDaniel Walter 45838d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 4584c3968a85SDaniel Walter { 4585c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 4586c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 4587c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 4588c3968a85SDaniel Walter 4589f88d8ea6SDavid Ahern if (!rt->nh && 4590f88d8ea6SDavid Ahern ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && 4591421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 459293c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 459360006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 4594c3968a85SDaniel Walter /* remove prefsrc entry */ 459593c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 459660006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 4597c3968a85SDaniel Walter } 4598c3968a85SDaniel Walter return 0; 4599c3968a85SDaniel Walter } 4600c3968a85SDaniel Walter 4601c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 4602c3968a85SDaniel Walter { 4603c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 4604c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 4605c3968a85SDaniel Walter .dev = ifp->idev->dev, 4606c3968a85SDaniel Walter .net = net, 4607c3968a85SDaniel Walter .addr = &ifp->addr, 4608c3968a85SDaniel Walter }; 46090c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 4610c3968a85SDaniel Walter } 4611c3968a85SDaniel Walter 46122b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 4613be7a010dSDuan Jiong 4614be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 46158d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 4616be7a010dSDuan Jiong { 4617be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 4618f88d8ea6SDavid Ahern struct fib6_nh *nh; 4619be7a010dSDuan Jiong 4620f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4621f88d8ea6SDavid Ahern if (rt->nh) 4622f88d8ea6SDavid Ahern return 0; 4623f88d8ea6SDavid Ahern 4624f88d8ea6SDavid Ahern nh = rt->fib6_nh; 462593c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 4626cc5c073aSDavid Ahern nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) 4627be7a010dSDuan Jiong return -1; 4628b16cb459SWei Wang 4629b16cb459SWei Wang /* Further clean up cached routes in exception table. 4630b16cb459SWei Wang * This is needed because cached route may have a different 4631b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 4632b16cb459SWei Wang */ 4633cc5c073aSDavid Ahern fib6_nh_exceptions_clean_tohost(nh, gateway); 4634b16cb459SWei Wang 4635be7a010dSDuan Jiong return 0; 4636be7a010dSDuan Jiong } 4637be7a010dSDuan Jiong 4638be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 4639be7a010dSDuan Jiong { 4640be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 4641be7a010dSDuan Jiong } 4642be7a010dSDuan Jiong 46432127d95aSIdo Schimmel struct arg_netdev_event { 46442127d95aSIdo Schimmel const struct net_device *dev; 46454c981e28SIdo Schimmel union { 4646ecc5663cSDavid Ahern unsigned char nh_flags; 46474c981e28SIdo Schimmel unsigned long event; 46484c981e28SIdo Schimmel }; 46492127d95aSIdo Schimmel }; 46502127d95aSIdo Schimmel 46518d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 4652d7dedee1SIdo Schimmel { 46538d1c802bSDavid Ahern struct fib6_info *iter; 4654d7dedee1SIdo Schimmel struct fib6_node *fn; 4655d7dedee1SIdo Schimmel 465693c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 465793c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4658d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 465993c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4660d7dedee1SIdo Schimmel while (iter) { 466193c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 466233bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 4663d7dedee1SIdo Schimmel return iter; 46648fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 466593c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4666d7dedee1SIdo Schimmel } 4667d7dedee1SIdo Schimmel 4668d7dedee1SIdo Schimmel return NULL; 4669d7dedee1SIdo Schimmel } 4670d7dedee1SIdo Schimmel 4671f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */ 46728d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 4673d7dedee1SIdo Schimmel { 46741cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || 46751cf844c7SDavid Ahern (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && 46761cf844c7SDavid Ahern ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) 4677d7dedee1SIdo Schimmel return true; 4678d7dedee1SIdo Schimmel 4679d7dedee1SIdo Schimmel return false; 4680d7dedee1SIdo Schimmel } 4681d7dedee1SIdo Schimmel 46828d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 4683d7dedee1SIdo Schimmel { 46848d1c802bSDavid Ahern struct fib6_info *iter; 4685d7dedee1SIdo Schimmel int total = 0; 4686d7dedee1SIdo Schimmel 4687d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 46881cf844c7SDavid Ahern total += rt->fib6_nh->fib_nh_weight; 4689d7dedee1SIdo Schimmel 469093c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4691d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 46921cf844c7SDavid Ahern total += iter->fib6_nh->fib_nh_weight; 4693d7dedee1SIdo Schimmel } 4694d7dedee1SIdo Schimmel 4695d7dedee1SIdo Schimmel return total; 4696d7dedee1SIdo Schimmel } 4697d7dedee1SIdo Schimmel 46988d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4699d7dedee1SIdo Schimmel { 4700d7dedee1SIdo Schimmel int upper_bound = -1; 4701d7dedee1SIdo Schimmel 4702d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 47031cf844c7SDavid Ahern *weight += rt->fib6_nh->fib_nh_weight; 4704d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4705d7dedee1SIdo Schimmel total) - 1; 4706d7dedee1SIdo Schimmel } 47071cf844c7SDavid Ahern atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); 4708d7dedee1SIdo Schimmel } 4709d7dedee1SIdo Schimmel 47108d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4711d7dedee1SIdo Schimmel { 47128d1c802bSDavid Ahern struct fib6_info *iter; 4713d7dedee1SIdo Schimmel int weight = 0; 4714d7dedee1SIdo Schimmel 4715d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 4716d7dedee1SIdo Schimmel 471793c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4718d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 4719d7dedee1SIdo Schimmel } 4720d7dedee1SIdo Schimmel 47218d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 4722d7dedee1SIdo Schimmel { 47238d1c802bSDavid Ahern struct fib6_info *first; 4724d7dedee1SIdo Schimmel int total; 4725d7dedee1SIdo Schimmel 4726d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 4727d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 4728d7dedee1SIdo Schimmel * sibling route. 4729d7dedee1SIdo Schimmel */ 473093c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 4731d7dedee1SIdo Schimmel return; 4732d7dedee1SIdo Schimmel 4733d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 4734d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 4735d7dedee1SIdo Schimmel * onwards. 4736d7dedee1SIdo Schimmel */ 4737d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 4738d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 4739d7dedee1SIdo Schimmel return; 4740d7dedee1SIdo Schimmel 4741d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 4742d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 4743d7dedee1SIdo Schimmel } 4744d7dedee1SIdo Schimmel 47458d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 47462127d95aSIdo Schimmel { 47472127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 47487aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 47492127d95aSIdo Schimmel 4750f88d8ea6SDavid Ahern if (rt != net->ipv6.fib6_null_entry && !rt->nh && 47511cf844c7SDavid Ahern rt->fib6_nh->fib_nh_dev == arg->dev) { 47521cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; 47537aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 4754d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 47551de178edSIdo Schimmel } 47562127d95aSIdo Schimmel 47572127d95aSIdo Schimmel return 0; 47582127d95aSIdo Schimmel } 47592127d95aSIdo Schimmel 4760ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 47612127d95aSIdo Schimmel { 47622127d95aSIdo Schimmel struct arg_netdev_event arg = { 47632127d95aSIdo Schimmel .dev = dev, 47646802f3adSIdo Schimmel { 47652127d95aSIdo Schimmel .nh_flags = nh_flags, 47666802f3adSIdo Schimmel }, 47672127d95aSIdo Schimmel }; 47682127d95aSIdo Schimmel 47692127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 47702127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 47712127d95aSIdo Schimmel 47722127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 47732127d95aSIdo Schimmel } 47742127d95aSIdo Schimmel 4775f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */ 47768d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 47771de178edSIdo Schimmel const struct net_device *dev) 47781de178edSIdo Schimmel { 47798d1c802bSDavid Ahern struct fib6_info *iter; 47801de178edSIdo Schimmel 47811cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 47821de178edSIdo Schimmel return true; 478393c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 47841cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 47851de178edSIdo Schimmel return true; 47861de178edSIdo Schimmel 47871de178edSIdo Schimmel return false; 47881de178edSIdo Schimmel } 47891de178edSIdo Schimmel 47908d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 47911de178edSIdo Schimmel { 47928d1c802bSDavid Ahern struct fib6_info *iter; 47931de178edSIdo Schimmel 47941de178edSIdo Schimmel rt->should_flush = 1; 479593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 47961de178edSIdo Schimmel iter->should_flush = 1; 47971de178edSIdo Schimmel } 47981de178edSIdo Schimmel 47998d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 48001de178edSIdo Schimmel const struct net_device *down_dev) 48011de178edSIdo Schimmel { 48028d1c802bSDavid Ahern struct fib6_info *iter; 48031de178edSIdo Schimmel unsigned int dead = 0; 48041de178edSIdo Schimmel 48051cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == down_dev || 48061cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 48071de178edSIdo Schimmel dead++; 480893c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 48091cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == down_dev || 48101cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 48111de178edSIdo Schimmel dead++; 48121de178edSIdo Schimmel 48131de178edSIdo Schimmel return dead; 48141de178edSIdo Schimmel } 48151de178edSIdo Schimmel 48168d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 48171de178edSIdo Schimmel const struct net_device *dev, 4818ecc5663cSDavid Ahern unsigned char nh_flags) 48191de178edSIdo Schimmel { 48208d1c802bSDavid Ahern struct fib6_info *iter; 48211de178edSIdo Schimmel 48221cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 48231cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= nh_flags; 482493c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 48251cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 48261cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags |= nh_flags; 48271de178edSIdo Schimmel } 48281de178edSIdo Schimmel 4829a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 48308d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 48311da177e4SLinus Torvalds { 48324c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 48334c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 48347aef6859SDavid Ahern struct net *net = dev_net(dev); 48358ed67789SDaniel Lezcano 4836f88d8ea6SDavid Ahern if (rt == net->ipv6.fib6_null_entry || rt->nh) 483727c6fa73SIdo Schimmel return 0; 483827c6fa73SIdo Schimmel 483927c6fa73SIdo Schimmel switch (arg->event) { 484027c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 48411cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 484227c6fa73SIdo Schimmel case NETDEV_DOWN: 48431de178edSIdo Schimmel if (rt->should_flush) 484427c6fa73SIdo Schimmel return -1; 484593c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 48461cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 48471de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 48481de178edSIdo Schimmel unsigned int count; 48491de178edSIdo Schimmel 48501de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 485193c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 48521de178edSIdo Schimmel rt6_multipath_flush(rt); 48531de178edSIdo Schimmel return -1; 48541de178edSIdo Schimmel } 48551de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 48561de178edSIdo Schimmel RTNH_F_LINKDOWN); 48577aef6859SDavid Ahern fib6_update_sernum(net, rt); 4858d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 48591de178edSIdo Schimmel } 48601de178edSIdo Schimmel return -2; 486127c6fa73SIdo Schimmel case NETDEV_CHANGE: 48621cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev != dev || 486393c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 486427c6fa73SIdo Schimmel break; 48651cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 4866d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 486727c6fa73SIdo Schimmel break; 48682b241361SIdo Schimmel } 4869c159d30cSDavid S. Miller 48701da177e4SLinus Torvalds return 0; 48711da177e4SLinus Torvalds } 48721da177e4SLinus Torvalds 487327c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 48741da177e4SLinus Torvalds { 48754c981e28SIdo Schimmel struct arg_netdev_event arg = { 48768ed67789SDaniel Lezcano .dev = dev, 48776802f3adSIdo Schimmel { 48784c981e28SIdo Schimmel .event = event, 48796802f3adSIdo Schimmel }, 48808ed67789SDaniel Lezcano }; 48817c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 48828ed67789SDaniel Lezcano 48837c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 48847c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 48857c6bb7d2SDavid Ahern else 48867c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 48874c981e28SIdo Schimmel } 48884c981e28SIdo Schimmel 48894c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 48904c981e28SIdo Schimmel { 48914c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 4892e5f80fcfSEric Dumazet rt6_uncached_list_flush_dev(dev); 48934c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 48941da177e4SLinus Torvalds } 48951da177e4SLinus Torvalds 489695c96174SEric Dumazet struct rt6_mtu_change_arg { 48971da177e4SLinus Torvalds struct net_device *dev; 489895c96174SEric Dumazet unsigned int mtu; 4899c0b220cfSDavid Ahern struct fib6_info *f6i; 49001da177e4SLinus Torvalds }; 49011da177e4SLinus Torvalds 4902cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) 4903c0b220cfSDavid Ahern { 4904c0b220cfSDavid Ahern struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; 4905cc5c073aSDavid Ahern struct fib6_info *f6i = arg->f6i; 4906c0b220cfSDavid Ahern 4907c0b220cfSDavid Ahern /* For administrative MTU increase, there is no way to discover 4908c0b220cfSDavid Ahern * IPv6 PMTU increase, so PMTU increase should be updated here. 4909c0b220cfSDavid Ahern * Since RFC 1981 doesn't include administrative MTU increase 4910c0b220cfSDavid Ahern * update PMTU increase is a MUST. (i.e. jumbo frame) 4911c0b220cfSDavid Ahern */ 4912c0b220cfSDavid Ahern if (nh->fib_nh_dev == arg->dev) { 4913c0b220cfSDavid Ahern struct inet6_dev *idev = __in6_dev_get(arg->dev); 4914c0b220cfSDavid Ahern u32 mtu = f6i->fib6_pmtu; 4915c0b220cfSDavid Ahern 4916c0b220cfSDavid Ahern if (mtu >= arg->mtu || 4917c0b220cfSDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4918c0b220cfSDavid Ahern fib6_metric_set(f6i, RTAX_MTU, arg->mtu); 4919c0b220cfSDavid Ahern 4920c0b220cfSDavid Ahern spin_lock_bh(&rt6_exception_lock); 4921cc5c073aSDavid Ahern rt6_exceptions_update_pmtu(idev, nh, arg->mtu); 4922c0b220cfSDavid Ahern spin_unlock_bh(&rt6_exception_lock); 4923c0b220cfSDavid Ahern } 4924c0b220cfSDavid Ahern 4925c0b220cfSDavid Ahern return 0; 4926c0b220cfSDavid Ahern } 4927c0b220cfSDavid Ahern 4928c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) 49291da177e4SLinus Torvalds { 49301da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 49311da177e4SLinus Torvalds struct inet6_dev *idev; 49321da177e4SLinus Torvalds 49331da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 49341da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 49351da177e4SLinus Torvalds We still use this lock to block changes 49361da177e4SLinus Torvalds caused by addrconf/ndisc. 49371da177e4SLinus Torvalds */ 49381da177e4SLinus Torvalds 49391da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 494038308473SDavid S. Miller if (!idev) 49411da177e4SLinus Torvalds return 0; 49421da177e4SLinus Torvalds 4943c0b220cfSDavid Ahern if (fib6_metric_locked(f6i, RTAX_MTU)) 49441da177e4SLinus Torvalds return 0; 4945c0b220cfSDavid Ahern 4946c0b220cfSDavid Ahern arg->f6i = f6i; 49472d44234bSDavid Ahern if (f6i->nh) { 49482d44234bSDavid Ahern /* fib6_nh_mtu_change only returns 0, so this is safe */ 49492d44234bSDavid Ahern return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, 49502d44234bSDavid Ahern arg); 49512d44234bSDavid Ahern } 49522d44234bSDavid Ahern 49531cf844c7SDavid Ahern return fib6_nh_mtu_change(f6i->fib6_nh, arg); 49541da177e4SLinus Torvalds } 49551da177e4SLinus Torvalds 495695c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 49571da177e4SLinus Torvalds { 4958c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4959c71099acSThomas Graf .dev = dev, 4960c71099acSThomas Graf .mtu = mtu, 4961c71099acSThomas Graf }; 49621da177e4SLinus Torvalds 49630c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 49641da177e4SLinus Torvalds } 49651da177e4SLinus Torvalds 4966ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 496775425657SDavid Ahern [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 49685176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4969aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 497086872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4971ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 497286872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 497386872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 497451ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4975c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 497619e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 497719e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 497832bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4979622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 49803b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4981aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4982eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4983eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4984eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 49855b98324eSDavid Ahern [RTA_NH_ID] = { .type = NLA_U32 }, 498686872cb5SThomas Graf }; 498786872cb5SThomas Graf 498886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4989333c4301SDavid Ahern struct fib6_config *cfg, 4990333c4301SDavid Ahern struct netlink_ext_ack *extack) 49911da177e4SLinus Torvalds { 499286872cb5SThomas Graf struct rtmsg *rtm; 499386872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4994c78ba6d6SLubomir Rintel unsigned int pref; 499586872cb5SThomas Graf int err; 49961da177e4SLinus Torvalds 49978cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 49988cb08174SJohannes Berg rtm_ipv6_policy, extack); 499986872cb5SThomas Graf if (err < 0) 500086872cb5SThomas Graf goto errout; 50011da177e4SLinus Torvalds 500286872cb5SThomas Graf err = -EINVAL; 500386872cb5SThomas Graf rtm = nlmsg_data(nlh); 500486872cb5SThomas Graf 5005b9605161SGuillaume Nault if (rtm->rtm_tos) { 5006b9605161SGuillaume Nault NL_SET_ERR_MSG(extack, 5007b9605161SGuillaume Nault "Invalid dsfield (tos): option not available for IPv6"); 5008b9605161SGuillaume Nault goto errout; 5009b9605161SGuillaume Nault } 5010b9605161SGuillaume Nault 501184db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 501284db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 501384db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 501484db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 501584db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 501684db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 501784db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 501884db8407SMaciej Żenczykowski 501984db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 502084db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 502184db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 502284db8407SMaciej Żenczykowski }; 502386872cb5SThomas Graf 5024ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 5025ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 5026b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 5027b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 502886872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 502986872cb5SThomas Graf 5030ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 5031ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 5032ab79ad14SMaciej Żenczykowski 50331f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 50341f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 50351f56a01fSMartin KaFai Lau 5036fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 5037fc1e64e1SDavid Ahern 50385b98324eSDavid Ahern if (tb[RTA_NH_ID]) { 50395b98324eSDavid Ahern if (tb[RTA_GATEWAY] || tb[RTA_OIF] || 50405b98324eSDavid Ahern tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { 50415b98324eSDavid Ahern NL_SET_ERR_MSG(extack, 50425b98324eSDavid Ahern "Nexthop specification and nexthop id are mutually exclusive"); 50435b98324eSDavid Ahern goto errout; 50445b98324eSDavid Ahern } 50455b98324eSDavid Ahern cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); 50465b98324eSDavid Ahern } 50475b98324eSDavid Ahern 504886872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 504967b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 505086872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 50511da177e4SLinus Torvalds } 5052e3818541SDavid Ahern if (tb[RTA_VIA]) { 5053e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 5054e3818541SDavid Ahern goto errout; 5055e3818541SDavid Ahern } 505686872cb5SThomas Graf 505786872cb5SThomas Graf if (tb[RTA_DST]) { 505886872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 505986872cb5SThomas Graf 506086872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 506186872cb5SThomas Graf goto errout; 506286872cb5SThomas Graf 506386872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 50641da177e4SLinus Torvalds } 506586872cb5SThomas Graf 506686872cb5SThomas Graf if (tb[RTA_SRC]) { 506786872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 506886872cb5SThomas Graf 506986872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 507086872cb5SThomas Graf goto errout; 507186872cb5SThomas Graf 507286872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 50731da177e4SLinus Torvalds } 507486872cb5SThomas Graf 5075c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 507667b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 5077c3968a85SDaniel Walter 507886872cb5SThomas Graf if (tb[RTA_OIF]) 507986872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 508086872cb5SThomas Graf 508186872cb5SThomas Graf if (tb[RTA_PRIORITY]) 508286872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 508386872cb5SThomas Graf 508486872cb5SThomas Graf if (tb[RTA_METRICS]) { 508586872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 508686872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 50871da177e4SLinus Torvalds } 508886872cb5SThomas Graf 508986872cb5SThomas Graf if (tb[RTA_TABLE]) 509086872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 509186872cb5SThomas Graf 509251ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 509351ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 509451ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 50959ed59592SDavid Ahern 50969ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 5097c255bd68SDavid Ahern cfg->fc_mp_len, extack); 50989ed59592SDavid Ahern if (err < 0) 50999ed59592SDavid Ahern goto errout; 510051ebd318SNicolas Dichtel } 510151ebd318SNicolas Dichtel 5102c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 5103c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 5104c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 5105c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 5106c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 5107c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 5108c78ba6d6SLubomir Rintel } 5109c78ba6d6SLubomir Rintel 511019e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 511119e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 511219e42e45SRoopa Prabhu 51139ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 511419e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 511519e42e45SRoopa Prabhu 5116c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 51179ed59592SDavid Ahern if (err < 0) 51189ed59592SDavid Ahern goto errout; 51199ed59592SDavid Ahern } 51209ed59592SDavid Ahern 512132bc201eSXin Long if (tb[RTA_EXPIRES]) { 512232bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 512332bc201eSXin Long 512432bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 512532bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 512632bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 512732bc201eSXin Long } 512832bc201eSXin Long } 512932bc201eSXin Long 513086872cb5SThomas Graf err = 0; 513186872cb5SThomas Graf errout: 513286872cb5SThomas Graf return err; 51331da177e4SLinus Torvalds } 51341da177e4SLinus Torvalds 51356b9ea5a6SRoopa Prabhu struct rt6_nh { 51368d1c802bSDavid Ahern struct fib6_info *fib6_info; 51376b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 51386b9ea5a6SRoopa Prabhu struct list_head next; 51396b9ea5a6SRoopa Prabhu }; 51406b9ea5a6SRoopa Prabhu 5141d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 5142d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 51438d1c802bSDavid Ahern struct fib6_info *rt, 51448d1c802bSDavid Ahern struct fib6_config *r_cfg) 51456b9ea5a6SRoopa Prabhu { 51466b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 51476b9ea5a6SRoopa Prabhu int err = -EEXIST; 51486b9ea5a6SRoopa Prabhu 51496b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 51508d1c802bSDavid Ahern /* check if fib6_info already exists */ 51518d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 51526b9ea5a6SRoopa Prabhu return err; 51536b9ea5a6SRoopa Prabhu } 51546b9ea5a6SRoopa Prabhu 51556b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 51566b9ea5a6SRoopa Prabhu if (!nh) 51576b9ea5a6SRoopa Prabhu return -ENOMEM; 51588d1c802bSDavid Ahern nh->fib6_info = rt; 51596b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 51606b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 51616b9ea5a6SRoopa Prabhu 51626b9ea5a6SRoopa Prabhu return 0; 51636b9ea5a6SRoopa Prabhu } 51646b9ea5a6SRoopa Prabhu 51658d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 51668d1c802bSDavid Ahern struct fib6_info *rt_last, 51673b1137feSDavid Ahern struct nl_info *info, 51683b1137feSDavid Ahern __u16 nlflags) 51693b1137feSDavid Ahern { 51703b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 51713b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 51723b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 51733b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 51743b1137feSDavid Ahern * the list, find the first sibling of the last route appended 51753b1137feSDavid Ahern */ 517693c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 517793c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 51788d1c802bSDavid Ahern struct fib6_info, 517993c2fb25SDavid Ahern fib6_siblings); 51803b1137feSDavid Ahern } 51813b1137feSDavid Ahern 51823b1137feSDavid Ahern if (rt) 51833b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 51843b1137feSDavid Ahern } 51853b1137feSDavid Ahern 51860ee0f47cSIdo Schimmel static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) 51870ee0f47cSIdo Schimmel { 51880ee0f47cSIdo Schimmel bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); 51890ee0f47cSIdo Schimmel bool should_notify = false; 51900ee0f47cSIdo Schimmel struct fib6_info *leaf; 51910ee0f47cSIdo Schimmel struct fib6_node *fn; 51920ee0f47cSIdo Schimmel 51930ee0f47cSIdo Schimmel rcu_read_lock(); 51940ee0f47cSIdo Schimmel fn = rcu_dereference(rt->fib6_node); 51950ee0f47cSIdo Schimmel if (!fn) 51960ee0f47cSIdo Schimmel goto out; 51970ee0f47cSIdo Schimmel 51980ee0f47cSIdo Schimmel leaf = rcu_dereference(fn->leaf); 51990ee0f47cSIdo Schimmel if (!leaf) 52000ee0f47cSIdo Schimmel goto out; 52010ee0f47cSIdo Schimmel 52020ee0f47cSIdo Schimmel if (rt == leaf || 52030ee0f47cSIdo Schimmel (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && 52040ee0f47cSIdo Schimmel rt6_qualify_for_ecmp(leaf))) 52050ee0f47cSIdo Schimmel should_notify = true; 52060ee0f47cSIdo Schimmel out: 52070ee0f47cSIdo Schimmel rcu_read_unlock(); 52080ee0f47cSIdo Schimmel 52090ee0f47cSIdo Schimmel return should_notify; 52100ee0f47cSIdo Schimmel } 52110ee0f47cSIdo Schimmel 52124619bcf9SDavid Ahern static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla, 52134619bcf9SDavid Ahern struct netlink_ext_ack *extack) 52144619bcf9SDavid Ahern { 52154619bcf9SDavid Ahern if (nla_len(nla) < sizeof(*gw)) { 52164619bcf9SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY"); 52174619bcf9SDavid Ahern return -EINVAL; 52184619bcf9SDavid Ahern } 52194619bcf9SDavid Ahern 52204619bcf9SDavid Ahern *gw = nla_get_in6_addr(nla); 52214619bcf9SDavid Ahern 52224619bcf9SDavid Ahern return 0; 52234619bcf9SDavid Ahern } 52244619bcf9SDavid Ahern 5225333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 5226333c4301SDavid Ahern struct netlink_ext_ack *extack) 522751ebd318SNicolas Dichtel { 52288d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 52293b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 523051ebd318SNicolas Dichtel struct fib6_config r_cfg; 523151ebd318SNicolas Dichtel struct rtnexthop *rtnh; 52328d1c802bSDavid Ahern struct fib6_info *rt; 52336b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 52346b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 52353b1137feSDavid Ahern __u16 nlflags; 523651ebd318SNicolas Dichtel int remaining; 523751ebd318SNicolas Dichtel int attrlen; 52386b9ea5a6SRoopa Prabhu int err = 1; 52396b9ea5a6SRoopa Prabhu int nhn = 0; 52406b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 52416b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 52426b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 524351ebd318SNicolas Dichtel 52443b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 52453b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 52463b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 52473b1137feSDavid Ahern 524835f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 524951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 525051ebd318SNicolas Dichtel 52516b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 52528d1c802bSDavid Ahern * fib6_info structs per nexthop 52536b9ea5a6SRoopa Prabhu */ 525451ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 525551ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 525651ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 525751ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 525851ebd318SNicolas Dichtel 525951ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 526051ebd318SNicolas Dichtel if (attrlen > 0) { 526151ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 526251ebd318SNicolas Dichtel 526351ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 526451ebd318SNicolas Dichtel if (nla) { 526595bdba23SDavid Ahern err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla, 52664619bcf9SDavid Ahern extack); 526795bdba23SDavid Ahern if (err) 526895bdba23SDavid Ahern goto cleanup; 52694619bcf9SDavid Ahern 527051ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 527151ebd318SNicolas Dichtel } 527219e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 52738bda81a4SDavid Ahern 52748bda81a4SDavid Ahern /* RTA_ENCAP_TYPE length checked in 52758bda81a4SDavid Ahern * lwtunnel_valid_encap_type_attr 52768bda81a4SDavid Ahern */ 527719e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 527819e42e45SRoopa Prabhu if (nla) 527919e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 528051ebd318SNicolas Dichtel } 52816b9ea5a6SRoopa Prabhu 528268e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 5283acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 52848c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 52858c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 52868c5b83f0SRoopa Prabhu rt = NULL; 52876b9ea5a6SRoopa Prabhu goto cleanup; 52888c5b83f0SRoopa Prabhu } 5289b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 5290b5d2d75eSDavid Ahern err = -EINVAL; 5291b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 5292b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 5293b5d2d75eSDavid Ahern fib6_info_release(rt); 5294b5d2d75eSDavid Ahern goto cleanup; 5295b5d2d75eSDavid Ahern } 52966b9ea5a6SRoopa Prabhu 52971cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; 5298398958aeSIdo Schimmel 5299d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 5300d4ead6b3SDavid Ahern rt, &r_cfg); 530151ebd318SNicolas Dichtel if (err) { 530293531c67SDavid Ahern fib6_info_release(rt); 53036b9ea5a6SRoopa Prabhu goto cleanup; 530451ebd318SNicolas Dichtel } 53056b9ea5a6SRoopa Prabhu 53066b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 530751ebd318SNicolas Dichtel } 53086b9ea5a6SRoopa Prabhu 53099eee3b49SIdo Schimmel if (list_empty(&rt6_nh_list)) { 53109eee3b49SIdo Schimmel NL_SET_ERR_MSG(extack, 53119eee3b49SIdo Schimmel "Invalid nexthop configuration - no valid nexthops"); 53129eee3b49SIdo Schimmel return -EINVAL; 53139eee3b49SIdo Schimmel } 53149eee3b49SIdo Schimmel 53153b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 53163b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 53173b1137feSDavid Ahern * the full route when done 53183b1137feSDavid Ahern */ 53193b1137feSDavid Ahern info->skip_notify = 1; 53203b1137feSDavid Ahern 5321ebee3cadSIdo Schimmel /* For add and replace, send one notification with all nexthops. For 5322ebee3cadSIdo Schimmel * append, send one notification with all appended nexthops. 5323ebee3cadSIdo Schimmel */ 5324ebee3cadSIdo Schimmel info->skip_notify_kernel = 1; 5325ebee3cadSIdo Schimmel 53266b9ea5a6SRoopa Prabhu err_nh = NULL; 53276b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 53288d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 53298d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 53303b1137feSDavid Ahern 5331f7225172SDavid Ahern if (!err) { 5332f7225172SDavid Ahern /* save reference to last route successfully inserted */ 5333f7225172SDavid Ahern rt_last = nh->fib6_info; 5334f7225172SDavid Ahern 53356b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 5336f7225172SDavid Ahern if (!rt_notif) 53378d1c802bSDavid Ahern rt_notif = nh->fib6_info; 5338f7225172SDavid Ahern } 53396b9ea5a6SRoopa Prabhu 53408d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 53418d1c802bSDavid Ahern nh->fib6_info = NULL; 53426b9ea5a6SRoopa Prabhu if (err) { 53436b9ea5a6SRoopa Prabhu if (replace && nhn) 5344a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 5345a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 53466b9ea5a6SRoopa Prabhu err_nh = nh; 53476b9ea5a6SRoopa Prabhu goto add_errout; 53486b9ea5a6SRoopa Prabhu } 53496b9ea5a6SRoopa Prabhu 53501a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 535127596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 535227596472SMichal Kubeček * we have already failed to add the first nexthop: 535327596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 535427596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 535527596472SMichal Kubeček * be added to it. 53561a72418bSNicolas Dichtel */ 5357864db232SMuhammad Usama Anjum if (cfg->fc_nlinfo.nlh) { 535827596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 535927596472SMichal Kubeček NLM_F_REPLACE); 5360afecdb37SBenjamin Poirier cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; 5361864db232SMuhammad Usama Anjum } 53626b9ea5a6SRoopa Prabhu nhn++; 53636b9ea5a6SRoopa Prabhu } 53646b9ea5a6SRoopa Prabhu 53650ee0f47cSIdo Schimmel /* An in-kernel notification should only be sent in case the new 53660ee0f47cSIdo Schimmel * multipath route is added as the first route in the node, or if 53670ee0f47cSIdo Schimmel * it was appended to it. We pass 'rt_notif' since it is the first 53680ee0f47cSIdo Schimmel * sibling and might allow us to skip some checks in the replace case. 53690ee0f47cSIdo Schimmel */ 53700ee0f47cSIdo Schimmel if (ip6_route_mpath_should_notify(rt_notif)) { 53710ee0f47cSIdo Schimmel enum fib_event_type fib_event; 53720ee0f47cSIdo Schimmel 53730ee0f47cSIdo Schimmel if (rt_notif->fib6_nsiblings != nhn - 1) 53740ee0f47cSIdo Schimmel fib_event = FIB_EVENT_ENTRY_APPEND; 53750ee0f47cSIdo Schimmel else 5376caafb250SIdo Schimmel fib_event = FIB_EVENT_ENTRY_REPLACE; 53770ee0f47cSIdo Schimmel 53780ee0f47cSIdo Schimmel err = call_fib6_multipath_entry_notifiers(info->nl_net, 53790ee0f47cSIdo Schimmel fib_event, rt_notif, 53800ee0f47cSIdo Schimmel nhn - 1, extack); 53810ee0f47cSIdo Schimmel if (err) { 53820ee0f47cSIdo Schimmel /* Delete all the siblings that were just added */ 53830ee0f47cSIdo Schimmel err_nh = NULL; 53840ee0f47cSIdo Schimmel goto add_errout; 53850ee0f47cSIdo Schimmel } 53860ee0f47cSIdo Schimmel } 5387ebee3cadSIdo Schimmel 53883b1137feSDavid Ahern /* success ... tell user about new route */ 53893b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 53906b9ea5a6SRoopa Prabhu goto cleanup; 53916b9ea5a6SRoopa Prabhu 53926b9ea5a6SRoopa Prabhu add_errout: 53933b1137feSDavid Ahern /* send notification for routes that were added so that 53943b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 53953b1137feSDavid Ahern * coherent 53963b1137feSDavid Ahern */ 53973b1137feSDavid Ahern if (rt_notif) 53983b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 53993b1137feSDavid Ahern 54006b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 54016b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 54026b9ea5a6SRoopa Prabhu if (err_nh == nh) 54036b9ea5a6SRoopa Prabhu break; 5404333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 54056b9ea5a6SRoopa Prabhu } 54066b9ea5a6SRoopa Prabhu 54076b9ea5a6SRoopa Prabhu cleanup: 54086b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 54098d1c802bSDavid Ahern if (nh->fib6_info) 54108d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 54116b9ea5a6SRoopa Prabhu list_del(&nh->next); 54126b9ea5a6SRoopa Prabhu kfree(nh); 54136b9ea5a6SRoopa Prabhu } 54146b9ea5a6SRoopa Prabhu 54156b9ea5a6SRoopa Prabhu return err; 54166b9ea5a6SRoopa Prabhu } 54176b9ea5a6SRoopa Prabhu 5418333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 5419333c4301SDavid Ahern struct netlink_ext_ack *extack) 54206b9ea5a6SRoopa Prabhu { 54216b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 54226b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 54232291267eSColin Ian King int last_err = 0; 54246b9ea5a6SRoopa Prabhu int remaining; 54256b9ea5a6SRoopa Prabhu int attrlen; 54262291267eSColin Ian King int err; 54276b9ea5a6SRoopa Prabhu 54286b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 54296b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 54306b9ea5a6SRoopa Prabhu 54316b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 54326b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 54336b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 54346b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 54356b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 54366b9ea5a6SRoopa Prabhu 54376b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 54386b9ea5a6SRoopa Prabhu if (attrlen > 0) { 54396b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 54406b9ea5a6SRoopa Prabhu 54416b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 54426b9ea5a6SRoopa Prabhu if (nla) { 54431ff15a71SDavid Ahern err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla, 54441ff15a71SDavid Ahern extack); 5445e30a845bSDavid Ahern if (err) { 5446e30a845bSDavid Ahern last_err = err; 5447e30a845bSDavid Ahern goto next_rtnh; 5448e30a845bSDavid Ahern } 54491ff15a71SDavid Ahern 54506b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 54516b9ea5a6SRoopa Prabhu } 54526b9ea5a6SRoopa Prabhu } 5453333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 54546b9ea5a6SRoopa Prabhu if (err) 54556b9ea5a6SRoopa Prabhu last_err = err; 54566b9ea5a6SRoopa Prabhu 5457e30a845bSDavid Ahern next_rtnh: 545851ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 545951ebd318SNicolas Dichtel } 546051ebd318SNicolas Dichtel 546151ebd318SNicolas Dichtel return last_err; 546251ebd318SNicolas Dichtel } 546351ebd318SNicolas Dichtel 5464c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5465c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 54661da177e4SLinus Torvalds { 546786872cb5SThomas Graf struct fib6_config cfg; 546886872cb5SThomas Graf int err; 54691da177e4SLinus Torvalds 5470333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 547186872cb5SThomas Graf if (err < 0) 547286872cb5SThomas Graf return err; 547386872cb5SThomas Graf 54745b98324eSDavid Ahern if (cfg.fc_nh_id && 54755b98324eSDavid Ahern !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { 54765b98324eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 54775b98324eSDavid Ahern return -EINVAL; 54785b98324eSDavid Ahern } 54795b98324eSDavid Ahern 548051ebd318SNicolas Dichtel if (cfg.fc_mp) 5481333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 54820ae81335SDavid Ahern else { 54830ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 5484333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 54851da177e4SLinus Torvalds } 54860ae81335SDavid Ahern } 54871da177e4SLinus Torvalds 5488c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5489c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 54901da177e4SLinus Torvalds { 549186872cb5SThomas Graf struct fib6_config cfg; 549286872cb5SThomas Graf int err; 54931da177e4SLinus Torvalds 5494333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 549586872cb5SThomas Graf if (err < 0) 549686872cb5SThomas Graf return err; 549786872cb5SThomas Graf 549867f69513SDavid Ahern if (cfg.fc_metric == 0) 549967f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 550067f69513SDavid Ahern 550151ebd318SNicolas Dichtel if (cfg.fc_mp) 5502333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 550351ebd318SNicolas Dichtel else 5504acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 55051da177e4SLinus Torvalds } 55061da177e4SLinus Torvalds 5507a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */ 5508a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) 5509339bf98fSThomas Graf { 5510a1b7a1f0SDavid Ahern int *nexthop_len = arg; 5511beb1afacSDavid Ahern 5512a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ 5513a1b7a1f0SDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5514a1b7a1f0SDavid Ahern + nla_total_size(16); /* RTA_GATEWAY */ 5515f88d8ea6SDavid Ahern 5516a1b7a1f0SDavid Ahern if (nh->fib_nh_lws) { 5517a1b7a1f0SDavid Ahern /* RTA_ENCAP_TYPE */ 5518a1b7a1f0SDavid Ahern *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5519a1b7a1f0SDavid Ahern /* RTA_ENCAP */ 5520a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(2); 5521a1b7a1f0SDavid Ahern } 5522a1b7a1f0SDavid Ahern 5523a1b7a1f0SDavid Ahern return 0; 5524a1b7a1f0SDavid Ahern } 5525a1b7a1f0SDavid Ahern 5526a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i) 5527a1b7a1f0SDavid Ahern { 5528a1b7a1f0SDavid Ahern int nexthop_len; 5529a1b7a1f0SDavid Ahern 5530a1b7a1f0SDavid Ahern if (f6i->nh) { 5531a1b7a1f0SDavid Ahern nexthop_len = nla_total_size(4); /* RTA_NH_ID */ 5532a1b7a1f0SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, 5533a1b7a1f0SDavid Ahern &nexthop_len); 5534a1b7a1f0SDavid Ahern } else { 55354cc59f38SLu Wei struct fib6_info *sibling, *next_sibling; 5536a1b7a1f0SDavid Ahern struct fib6_nh *nh = f6i->fib6_nh; 5537a1b7a1f0SDavid Ahern 5538a1b7a1f0SDavid Ahern nexthop_len = 0; 5539a1b7a1f0SDavid Ahern if (f6i->fib6_nsiblings) { 55404cc59f38SLu Wei rt6_nh_nlmsg_size(nh, &nexthop_len); 5541beb1afacSDavid Ahern 55424cc59f38SLu Wei list_for_each_entry_safe(sibling, next_sibling, 55434cc59f38SLu Wei &f6i->fib6_siblings, fib6_siblings) { 55444cc59f38SLu Wei rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len); 55454cc59f38SLu Wei } 5546a1b7a1f0SDavid Ahern } 5547a1b7a1f0SDavid Ahern nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5548beb1afacSDavid Ahern } 5549beb1afacSDavid Ahern 5550339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 5551339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 5552339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 5553339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 5554339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 5555339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 5556339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 5557339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 5558339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 55596a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 5560ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 5561c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 556219e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 5563beb1afacSDavid Ahern + nexthop_len; 5564beb1afacSDavid Ahern } 5565beb1afacSDavid Ahern 5566f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, 5567f88d8ea6SDavid Ahern unsigned char *flags) 5568f88d8ea6SDavid Ahern { 5569f88d8ea6SDavid Ahern if (nexthop_is_multipath(nh)) { 5570f88d8ea6SDavid Ahern struct nlattr *mp; 5571f88d8ea6SDavid Ahern 55724255ff05SDavid Ahern mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5573f88d8ea6SDavid Ahern if (!mp) 5574f88d8ea6SDavid Ahern goto nla_put_failure; 5575f88d8ea6SDavid Ahern 55767bdf4de1SDonald Sharp if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) 5577f88d8ea6SDavid Ahern goto nla_put_failure; 5578f88d8ea6SDavid Ahern 5579f88d8ea6SDavid Ahern nla_nest_end(skb, mp); 5580f88d8ea6SDavid Ahern } else { 5581f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 5582f88d8ea6SDavid Ahern 5583f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(nh); 55847bdf4de1SDonald Sharp if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, 5585f88d8ea6SDavid Ahern flags, false) < 0) 5586f88d8ea6SDavid Ahern goto nla_put_failure; 5587f88d8ea6SDavid Ahern } 5588f88d8ea6SDavid Ahern 5589f88d8ea6SDavid Ahern return 0; 5590f88d8ea6SDavid Ahern 5591f88d8ea6SDavid Ahern nla_put_failure: 5592f88d8ea6SDavid Ahern return -EMSGSIZE; 5593f88d8ea6SDavid Ahern } 5594f88d8ea6SDavid Ahern 5595d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 55968d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 5597d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 559815e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 5599f8cfe2ceSDavid Ahern unsigned int flags) 56001da177e4SLinus Torvalds { 560122d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 560222d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 560322d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 5604f88d8ea6SDavid Ahern unsigned char nh_flags = 0; 56051da177e4SLinus Torvalds struct nlmsghdr *nlh; 560622d0bd82SXin Long struct rtmsg *rtm; 5607d4ead6b3SDavid Ahern long expires = 0; 56081da177e4SLinus Torvalds 560915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 561038308473SDavid S. Miller if (!nlh) 561126932566SPatrick McHardy return -EMSGSIZE; 56122d7202bfSThomas Graf 561322d0bd82SXin Long if (rt6) { 561422d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 561522d0bd82SXin Long rt6_src = &rt6->rt6i_src; 561622d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 561722d0bd82SXin Long } else { 561822d0bd82SXin Long rt6_dst = &rt->fib6_dst; 561922d0bd82SXin Long rt6_src = &rt->fib6_src; 562022d0bd82SXin Long rt6_flags = rt->fib6_flags; 562122d0bd82SXin Long } 562222d0bd82SXin Long 56232d7202bfSThomas Graf rtm = nlmsg_data(nlh); 56241da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 562522d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 562622d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 56271da177e4SLinus Torvalds rtm->rtm_tos = 0; 562893c2fb25SDavid Ahern if (rt->fib6_table) 562993c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 5630c71099acSThomas Graf else 56319e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 563297f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 5633c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 5634c78679e8SDavid S. Miller goto nla_put_failure; 5635e8478e80SDavid Ahern 5636e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 56371da177e4SLinus Torvalds rtm->rtm_flags = 0; 56381da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 563993c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 56401da177e4SLinus Torvalds 564122d0bd82SXin Long if (rt6_flags & RTF_CACHE) 56421da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 56431da177e4SLinus Torvalds 5644d4ead6b3SDavid Ahern if (dest) { 5645d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 5646c78679e8SDavid S. Miller goto nla_put_failure; 56471da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 56481da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 564922d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 5650c78679e8SDavid S. Miller goto nla_put_failure; 56511da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 56521da177e4SLinus Torvalds if (src) { 5653930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 5654c78679e8SDavid S. Miller goto nla_put_failure; 56551da177e4SLinus Torvalds rtm->rtm_src_len = 128; 5656c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 565722d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 5658c78679e8SDavid S. Miller goto nla_put_failure; 56591da177e4SLinus Torvalds #endif 56607bc570c8SYOSHIFUJI Hideaki if (iif) { 56617bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 566222d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 5663fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 56642cf75070SNikolay Aleksandrov 56657bc570c8SYOSHIFUJI Hideaki if (err == 0) 56667bc570c8SYOSHIFUJI Hideaki return 0; 5667fd61c6baSDavid Ahern if (err < 0) 56687bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 56697bc570c8SYOSHIFUJI Hideaki } else 56707bc570c8SYOSHIFUJI Hideaki #endif 5671c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 5672c78679e8SDavid S. Miller goto nla_put_failure; 5673d4ead6b3SDavid Ahern } else if (dest) { 56741da177e4SLinus Torvalds struct in6_addr saddr_buf; 5675d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 5676930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5677c78679e8SDavid S. Miller goto nla_put_failure; 5678c3968a85SDaniel Walter } 5679c3968a85SDaniel Walter 568093c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 5681c3968a85SDaniel Walter struct in6_addr saddr_buf; 568293c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 5683930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5684c78679e8SDavid S. Miller goto nla_put_failure; 56851da177e4SLinus Torvalds } 56862d7202bfSThomas Graf 5687d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 5688d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 56892d7202bfSThomas Graf goto nla_put_failure; 56902d7202bfSThomas Graf 569193c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 5692beb1afacSDavid Ahern goto nla_put_failure; 5693beb1afacSDavid Ahern 5694beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 5695beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 5696beb1afacSDavid Ahern */ 569722d0bd82SXin Long if (rt6) { 569822d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 569922d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 570022d0bd82SXin Long goto nla_put_failure; 570122d0bd82SXin Long 570222d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 570322d0bd82SXin Long goto nla_put_failure; 57046b13d8f7SOliver Herms 57056b13d8f7SOliver Herms if (dst->lwtstate && 57066b13d8f7SOliver Herms lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0) 57076b13d8f7SOliver Herms goto nla_put_failure; 570822d0bd82SXin Long } else if (rt->fib6_nsiblings) { 57098d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 5710beb1afacSDavid Ahern struct nlattr *mp; 5711beb1afacSDavid Ahern 5712ae0be8deSMichal Kubecek mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5713beb1afacSDavid Ahern if (!mp) 5714beb1afacSDavid Ahern goto nla_put_failure; 5715beb1afacSDavid Ahern 57161cf844c7SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, 5717597aa16cSXiao Liang rt->fib6_nh->fib_nh_weight, AF_INET6, 5718597aa16cSXiao Liang 0) < 0) 5719beb1afacSDavid Ahern goto nla_put_failure; 5720beb1afacSDavid Ahern 5721beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 572293c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 57231cf844c7SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, 57247bdf4de1SDonald Sharp sibling->fib6_nh->fib_nh_weight, 5725597aa16cSXiao Liang AF_INET6, 0) < 0) 572694f826b8SEric Dumazet goto nla_put_failure; 572794f826b8SEric Dumazet } 57282d7202bfSThomas Graf 5729beb1afacSDavid Ahern nla_nest_end(skb, mp); 5730f88d8ea6SDavid Ahern } else if (rt->nh) { 5731f88d8ea6SDavid Ahern if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) 5732f88d8ea6SDavid Ahern goto nla_put_failure; 5733ecc5663cSDavid Ahern 5734f88d8ea6SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 5735f88d8ea6SDavid Ahern rtm->rtm_type = RTN_BLACKHOLE; 5736f88d8ea6SDavid Ahern 5737bdf00bf2SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) && 57384f80116dSRoopa Prabhu rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) 5739f88d8ea6SDavid Ahern goto nla_put_failure; 5740f88d8ea6SDavid Ahern 5741f88d8ea6SDavid Ahern rtm->rtm_flags |= nh_flags; 5742f88d8ea6SDavid Ahern } else { 57437bdf4de1SDonald Sharp if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, 5744ecc5663cSDavid Ahern &nh_flags, false) < 0) 5745c78679e8SDavid S. Miller goto nla_put_failure; 5746ecc5663cSDavid Ahern 5747ecc5663cSDavid Ahern rtm->rtm_flags |= nh_flags; 5748beb1afacSDavid Ahern } 57498253947eSLi Wei 575022d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 575114895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 575214895687SDavid Ahern expires -= jiffies; 575314895687SDavid Ahern } 575469cdf8f9SYOSHIFUJI Hideaki 5755bb3c4ab9SIdo Schimmel if (!dst) { 5756d95d6320SEric Dumazet if (READ_ONCE(rt->offload)) 5757bb3c4ab9SIdo Schimmel rtm->rtm_flags |= RTM_F_OFFLOAD; 5758d95d6320SEric Dumazet if (READ_ONCE(rt->trap)) 5759bb3c4ab9SIdo Schimmel rtm->rtm_flags |= RTM_F_TRAP; 5760d95d6320SEric Dumazet if (READ_ONCE(rt->offload_failed)) 57610c5fcf9eSAmit Cohen rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED; 5762bb3c4ab9SIdo Schimmel } 5763bb3c4ab9SIdo Schimmel 5764d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 5765e3703b3dSThomas Graf goto nla_put_failure; 57661da177e4SLinus Torvalds 576722d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 5768c78ba6d6SLubomir Rintel goto nla_put_failure; 5769c78ba6d6SLubomir Rintel 577019e42e45SRoopa Prabhu 5771053c095aSJohannes Berg nlmsg_end(skb, nlh); 5772053c095aSJohannes Berg return 0; 57732d7202bfSThomas Graf 57742d7202bfSThomas Graf nla_put_failure: 577526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 577626932566SPatrick McHardy return -EMSGSIZE; 57771da177e4SLinus Torvalds } 57781da177e4SLinus Torvalds 57792c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) 57802c170e07SDavid Ahern { 57812c170e07SDavid Ahern const struct net_device *dev = arg; 57822c170e07SDavid Ahern 57832c170e07SDavid Ahern if (nh->fib_nh_dev == dev) 57842c170e07SDavid Ahern return 1; 57852c170e07SDavid Ahern 57862c170e07SDavid Ahern return 0; 57872c170e07SDavid Ahern } 57882c170e07SDavid Ahern 578913e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 579013e38901SDavid Ahern const struct net_device *dev) 579113e38901SDavid Ahern { 57922c170e07SDavid Ahern if (f6i->nh) { 57932c170e07SDavid Ahern struct net_device *_dev = (struct net_device *)dev; 57942c170e07SDavid Ahern 57952c170e07SDavid Ahern return !!nexthop_for_each_fib6_nh(f6i->nh, 57962c170e07SDavid Ahern fib6_info_nh_uses_dev, 57972c170e07SDavid Ahern _dev); 57982c170e07SDavid Ahern } 57992c170e07SDavid Ahern 58001cf844c7SDavid Ahern if (f6i->fib6_nh->fib_nh_dev == dev) 580113e38901SDavid Ahern return true; 580213e38901SDavid Ahern 580313e38901SDavid Ahern if (f6i->fib6_nsiblings) { 580413e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 580513e38901SDavid Ahern 580613e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 580713e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 58081cf844c7SDavid Ahern if (sibling->fib6_nh->fib_nh_dev == dev) 580913e38901SDavid Ahern return true; 581013e38901SDavid Ahern } 581113e38901SDavid Ahern } 581213e38901SDavid Ahern 581313e38901SDavid Ahern return false; 581413e38901SDavid Ahern } 581513e38901SDavid Ahern 58161e47b483SStefano Brivio struct fib6_nh_exception_dump_walker { 58171e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump; 58181e47b483SStefano Brivio struct fib6_info *rt; 58191e47b483SStefano Brivio unsigned int flags; 58201e47b483SStefano Brivio unsigned int skip; 58211e47b483SStefano Brivio unsigned int count; 58221e47b483SStefano Brivio }; 58231e47b483SStefano Brivio 58241e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) 58251e47b483SStefano Brivio { 58261e47b483SStefano Brivio struct fib6_nh_exception_dump_walker *w = arg; 58271e47b483SStefano Brivio struct rt6_rtnl_dump_arg *dump = w->dump; 58281e47b483SStefano Brivio struct rt6_exception_bucket *bucket; 58291e47b483SStefano Brivio struct rt6_exception *rt6_ex; 58301e47b483SStefano Brivio int i, err; 58311e47b483SStefano Brivio 58321e47b483SStefano Brivio bucket = fib6_nh_get_excptn_bucket(nh, NULL); 58331e47b483SStefano Brivio if (!bucket) 58341e47b483SStefano Brivio return 0; 58351e47b483SStefano Brivio 58361e47b483SStefano Brivio for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 58371e47b483SStefano Brivio hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 58381e47b483SStefano Brivio if (w->skip) { 58391e47b483SStefano Brivio w->skip--; 58401e47b483SStefano Brivio continue; 58411e47b483SStefano Brivio } 58421e47b483SStefano Brivio 58431e47b483SStefano Brivio /* Expiration of entries doesn't bump sernum, insertion 58441e47b483SStefano Brivio * does. Removal is triggered by insertion, so we can 58451e47b483SStefano Brivio * rely on the fact that if entries change between two 58461e47b483SStefano Brivio * partial dumps, this node is scanned again completely, 58471e47b483SStefano Brivio * see rt6_insert_exception() and fib6_dump_table(). 58481e47b483SStefano Brivio * 58491e47b483SStefano Brivio * Count expired entries we go through as handled 58501e47b483SStefano Brivio * entries that we'll skip next time, in case of partial 58511e47b483SStefano Brivio * node dump. Otherwise, if entries expire meanwhile, 58521e47b483SStefano Brivio * we'll skip the wrong amount. 58531e47b483SStefano Brivio */ 58541e47b483SStefano Brivio if (rt6_check_expired(rt6_ex->rt6i)) { 58551e47b483SStefano Brivio w->count++; 58561e47b483SStefano Brivio continue; 58571e47b483SStefano Brivio } 58581e47b483SStefano Brivio 58591e47b483SStefano Brivio err = rt6_fill_node(dump->net, dump->skb, w->rt, 58601e47b483SStefano Brivio &rt6_ex->rt6i->dst, NULL, NULL, 0, 58611e47b483SStefano Brivio RTM_NEWROUTE, 58621e47b483SStefano Brivio NETLINK_CB(dump->cb->skb).portid, 58631e47b483SStefano Brivio dump->cb->nlh->nlmsg_seq, w->flags); 58641e47b483SStefano Brivio if (err) 58651e47b483SStefano Brivio return err; 58661e47b483SStefano Brivio 58671e47b483SStefano Brivio w->count++; 58681e47b483SStefano Brivio } 58691e47b483SStefano Brivio bucket++; 58701e47b483SStefano Brivio } 58711e47b483SStefano Brivio 58721e47b483SStefano Brivio return 0; 58731e47b483SStefano Brivio } 58741e47b483SStefano Brivio 5875bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */ 58761e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) 58771da177e4SLinus Torvalds { 58781da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 587913e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 588013e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 58811f17e2f2SDavid Ahern struct net *net = arg->net; 58821e47b483SStefano Brivio int count = 0; 58831f17e2f2SDavid Ahern 5884421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 5885bf9a8a06SStefano Brivio return -1; 58861da177e4SLinus Torvalds 588713e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 588893c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 5889f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 5890bf9a8a06SStefano Brivio return -1; 5891f8cfe2ceSDavid Ahern } 58921e47b483SStefano Brivio if (filter->filter_set && 58931e47b483SStefano Brivio ((filter->rt_type && rt->fib6_type != filter->rt_type) || 589413e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 58951e47b483SStefano Brivio (filter->protocol && rt->fib6_protocol != filter->protocol))) { 5896bf9a8a06SStefano Brivio return -1; 589713e38901SDavid Ahern } 58981e47b483SStefano Brivio 58991e47b483SStefano Brivio if (filter->filter_set || 59001e47b483SStefano Brivio !filter->dump_routes || !filter->dump_exceptions) { 590113e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 5902f8cfe2ceSDavid Ahern } 59031da177e4SLinus Torvalds 59041e47b483SStefano Brivio if (filter->dump_routes) { 59051e47b483SStefano Brivio if (skip) { 59061e47b483SStefano Brivio skip--; 59071e47b483SStefano Brivio } else { 59081e47b483SStefano Brivio if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 59091e47b483SStefano Brivio 0, RTM_NEWROUTE, 5910bf9a8a06SStefano Brivio NETLINK_CB(arg->cb->skb).portid, 59111e47b483SStefano Brivio arg->cb->nlh->nlmsg_seq, flags)) { 5912bf9a8a06SStefano Brivio return 0; 59131e47b483SStefano Brivio } 59141e47b483SStefano Brivio count++; 59151e47b483SStefano Brivio } 59161e47b483SStefano Brivio } 59171e47b483SStefano Brivio 59181e47b483SStefano Brivio if (filter->dump_exceptions) { 59191e47b483SStefano Brivio struct fib6_nh_exception_dump_walker w = { .dump = arg, 59201e47b483SStefano Brivio .rt = rt, 59211e47b483SStefano Brivio .flags = flags, 59221e47b483SStefano Brivio .skip = skip, 59231e47b483SStefano Brivio .count = 0 }; 59241e47b483SStefano Brivio int err; 59251e47b483SStefano Brivio 59263b525691SEric Dumazet rcu_read_lock(); 59271e47b483SStefano Brivio if (rt->nh) { 59281e47b483SStefano Brivio err = nexthop_for_each_fib6_nh(rt->nh, 59291e47b483SStefano Brivio rt6_nh_dump_exceptions, 59301e47b483SStefano Brivio &w); 59311e47b483SStefano Brivio } else { 59321e47b483SStefano Brivio err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); 59331e47b483SStefano Brivio } 59343b525691SEric Dumazet rcu_read_unlock(); 59351e47b483SStefano Brivio 59361e47b483SStefano Brivio if (err) 593774fd304fSColin Ian King return count + w.count; 59381e47b483SStefano Brivio } 5939bf9a8a06SStefano Brivio 5940bf9a8a06SStefano Brivio return -1; 59411da177e4SLinus Torvalds } 59421da177e4SLinus Torvalds 59430eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 59440eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 59450eff0a27SJakub Kicinski struct nlattr **tb, 59460eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 59470eff0a27SJakub Kicinski { 59480eff0a27SJakub Kicinski struct rtmsg *rtm; 59490eff0a27SJakub Kicinski int i, err; 59500eff0a27SJakub Kicinski 59510eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 59520eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 59530eff0a27SJakub Kicinski "Invalid header for get route request"); 59540eff0a27SJakub Kicinski return -EINVAL; 59550eff0a27SJakub Kicinski } 59560eff0a27SJakub Kicinski 59570eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 59588cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 59590eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 59600eff0a27SJakub Kicinski 59610eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 59620eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 59630eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 59640eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 59650eff0a27SJakub Kicinski rtm->rtm_type) { 59660eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 59670eff0a27SJakub Kicinski return -EINVAL; 59680eff0a27SJakub Kicinski } 59690eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 59700eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 59710eff0a27SJakub Kicinski "Invalid flags for get route request"); 59720eff0a27SJakub Kicinski return -EINVAL; 59730eff0a27SJakub Kicinski } 59740eff0a27SJakub Kicinski 59758cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 59760eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 59770eff0a27SJakub Kicinski if (err) 59780eff0a27SJakub Kicinski return err; 59790eff0a27SJakub Kicinski 59800eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 59810eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 59820eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 59830eff0a27SJakub Kicinski return -EINVAL; 59840eff0a27SJakub Kicinski } 59850eff0a27SJakub Kicinski 59860eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 59870eff0a27SJakub Kicinski if (!tb[i]) 59880eff0a27SJakub Kicinski continue; 59890eff0a27SJakub Kicinski 59900eff0a27SJakub Kicinski switch (i) { 59910eff0a27SJakub Kicinski case RTA_SRC: 59920eff0a27SJakub Kicinski case RTA_DST: 59930eff0a27SJakub Kicinski case RTA_IIF: 59940eff0a27SJakub Kicinski case RTA_OIF: 59950eff0a27SJakub Kicinski case RTA_MARK: 59960eff0a27SJakub Kicinski case RTA_UID: 59970eff0a27SJakub Kicinski case RTA_SPORT: 59980eff0a27SJakub Kicinski case RTA_DPORT: 59990eff0a27SJakub Kicinski case RTA_IP_PROTO: 60000eff0a27SJakub Kicinski break; 60010eff0a27SJakub Kicinski default: 60020eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 60030eff0a27SJakub Kicinski return -EINVAL; 60040eff0a27SJakub Kicinski } 60050eff0a27SJakub Kicinski } 60060eff0a27SJakub Kicinski 60070eff0a27SJakub Kicinski return 0; 60080eff0a27SJakub Kicinski } 60090eff0a27SJakub Kicinski 6010c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 6011c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 60121da177e4SLinus Torvalds { 60133b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 6014ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 601518c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 6016a68886a6SDavid Ahern struct fib6_info *from; 601718c3a61cSRoopa Prabhu struct dst_entry *dst; 60181da177e4SLinus Torvalds struct rt6_info *rt; 6019ab364a6fSThomas Graf struct sk_buff *skb; 6020ab364a6fSThomas Graf struct rtmsg *rtm; 6021744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 602218c3a61cSRoopa Prabhu bool fibmatch; 6023ab364a6fSThomas Graf 60240eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 6025ab364a6fSThomas Graf if (err < 0) 6026ab364a6fSThomas Graf goto errout; 6027ab364a6fSThomas Graf 6028ab364a6fSThomas Graf err = -EINVAL; 602938b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 603038b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 603118c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 6032ab364a6fSThomas Graf 6033ab364a6fSThomas Graf if (tb[RTA_SRC]) { 6034ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 6035ab364a6fSThomas Graf goto errout; 6036ab364a6fSThomas Graf 60374e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 6038ab364a6fSThomas Graf } 6039ab364a6fSThomas Graf 6040ab364a6fSThomas Graf if (tb[RTA_DST]) { 6041ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 6042ab364a6fSThomas Graf goto errout; 6043ab364a6fSThomas Graf 60444e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 6045ab364a6fSThomas Graf } 6046ab364a6fSThomas Graf 6047ab364a6fSThomas Graf if (tb[RTA_IIF]) 6048ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 6049ab364a6fSThomas Graf 6050ab364a6fSThomas Graf if (tb[RTA_OIF]) 605172331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 6052ab364a6fSThomas Graf 60532e47b291SLorenzo Colitti if (tb[RTA_MARK]) 60542e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 60552e47b291SLorenzo Colitti 6056622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 6057622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 6058622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 6059622ec2c9SLorenzo Colitti else 6060622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 6061622ec2c9SLorenzo Colitti 6062eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 6063eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 6064eacb9384SRoopa Prabhu 6065eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 6066eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 6067eacb9384SRoopa Prabhu 6068eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 6069eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 60705e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 60715e1a99eaSHangbin Liu extack); 6072eacb9384SRoopa Prabhu if (err) 6073eacb9384SRoopa Prabhu goto errout; 6074eacb9384SRoopa Prabhu } 6075eacb9384SRoopa Prabhu 6076ab364a6fSThomas Graf if (iif) { 6077ab364a6fSThomas Graf struct net_device *dev; 607872331bc0SShmulik Ladkani int flags = 0; 607972331bc0SShmulik Ladkani 6080121622dbSFlorian Westphal rcu_read_lock(); 6081121622dbSFlorian Westphal 6082121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 6083ab364a6fSThomas Graf if (!dev) { 6084121622dbSFlorian Westphal rcu_read_unlock(); 6085ab364a6fSThomas Graf err = -ENODEV; 6086ab364a6fSThomas Graf goto errout; 6087ab364a6fSThomas Graf } 608872331bc0SShmulik Ladkani 608972331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 609072331bc0SShmulik Ladkani 609172331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 609272331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 609372331bc0SShmulik Ladkani 6094b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 6095121622dbSFlorian Westphal 6096121622dbSFlorian Westphal rcu_read_unlock(); 609772331bc0SShmulik Ladkani } else { 609872331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 609972331bc0SShmulik Ladkani 610018c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 610118c3a61cSRoopa Prabhu } 610218c3a61cSRoopa Prabhu 610318c3a61cSRoopa Prabhu 610418c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 610518c3a61cSRoopa Prabhu if (rt->dst.error) { 610618c3a61cSRoopa Prabhu err = rt->dst.error; 610718c3a61cSRoopa Prabhu ip6_rt_put(rt); 610818c3a61cSRoopa Prabhu goto errout; 6109ab364a6fSThomas Graf } 61101da177e4SLinus Torvalds 61119d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 61129d6acb3bSWANG Cong err = rt->dst.error; 61139d6acb3bSWANG Cong ip6_rt_put(rt); 61149d6acb3bSWANG Cong goto errout; 61159d6acb3bSWANG Cong } 61169d6acb3bSWANG Cong 61171da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 611838308473SDavid S. Miller if (!skb) { 611994e187c0SAmerigo Wang ip6_rt_put(rt); 6120ab364a6fSThomas Graf err = -ENOBUFS; 6121ab364a6fSThomas Graf goto errout; 6122ab364a6fSThomas Graf } 61231da177e4SLinus Torvalds 6124d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 6125a68886a6SDavid Ahern 6126a68886a6SDavid Ahern rcu_read_lock(); 6127a68886a6SDavid Ahern from = rcu_dereference(rt->from); 6128886b7a50SMartin KaFai Lau if (from) { 612918c3a61cSRoopa Prabhu if (fibmatch) 6130886b7a50SMartin KaFai Lau err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 6131886b7a50SMartin KaFai Lau iif, RTM_NEWROUTE, 6132886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 613318c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 613418c3a61cSRoopa Prabhu else 6135a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 6136a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 6137886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 6138886b7a50SMartin KaFai Lau nlh->nlmsg_seq, 0); 6139886b7a50SMartin KaFai Lau } else { 6140886b7a50SMartin KaFai Lau err = -ENETUNREACH; 6141886b7a50SMartin KaFai Lau } 6142a68886a6SDavid Ahern rcu_read_unlock(); 6143a68886a6SDavid Ahern 61441da177e4SLinus Torvalds if (err < 0) { 6145ab364a6fSThomas Graf kfree_skb(skb); 6146ab364a6fSThomas Graf goto errout; 61471da177e4SLinus Torvalds } 61481da177e4SLinus Torvalds 614915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 6150ab364a6fSThomas Graf errout: 61511da177e4SLinus Torvalds return err; 61521da177e4SLinus Torvalds } 61531da177e4SLinus Torvalds 61548d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 615537a1d361SRoopa Prabhu unsigned int nlm_flags) 61561da177e4SLinus Torvalds { 61571da177e4SLinus Torvalds struct sk_buff *skb; 61585578689aSDaniel Lezcano struct net *net = info->nl_net; 6159528c4cebSDenis V. Lunev u32 seq; 6160528c4cebSDenis V. Lunev int err; 61610d51aa80SJamal Hadi Salim 6162528c4cebSDenis V. Lunev err = -ENOBUFS; 616338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 616486872cb5SThomas Graf 616519e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 616638308473SDavid S. Miller if (!skb) 616721713ebcSThomas Graf goto errout; 61681da177e4SLinus Torvalds 6169d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 6170f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 617126932566SPatrick McHardy if (err < 0) { 617226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 617326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 617426932566SPatrick McHardy kfree_skb(skb); 617526932566SPatrick McHardy goto errout; 617626932566SPatrick McHardy } 617715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 61785578689aSDaniel Lezcano info->nlh, gfp_any()); 61791ce85fe4SPablo Neira Ayuso return; 618021713ebcSThomas Graf errout: 618121713ebcSThomas Graf if (err < 0) 61825578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 61831da177e4SLinus Torvalds } 61841da177e4SLinus Torvalds 618519a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt, 618619a3b7eeSDavid Ahern struct nl_info *info) 618719a3b7eeSDavid Ahern { 618819a3b7eeSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 618919a3b7eeSDavid Ahern struct sk_buff *skb; 619019a3b7eeSDavid Ahern int err = -ENOBUFS; 619119a3b7eeSDavid Ahern 619219a3b7eeSDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 619319a3b7eeSDavid Ahern if (!skb) 619419a3b7eeSDavid Ahern goto errout; 619519a3b7eeSDavid Ahern 619619a3b7eeSDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 619719a3b7eeSDavid Ahern RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 619819a3b7eeSDavid Ahern if (err < 0) { 619919a3b7eeSDavid Ahern /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 620019a3b7eeSDavid Ahern WARN_ON(err == -EMSGSIZE); 620119a3b7eeSDavid Ahern kfree_skb(skb); 620219a3b7eeSDavid Ahern goto errout; 620319a3b7eeSDavid Ahern } 620419a3b7eeSDavid Ahern rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 620519a3b7eeSDavid Ahern info->nlh, gfp_any()); 620619a3b7eeSDavid Ahern return; 620719a3b7eeSDavid Ahern errout: 620819a3b7eeSDavid Ahern if (err < 0) 620919a3b7eeSDavid Ahern rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 621019a3b7eeSDavid Ahern } 621119a3b7eeSDavid Ahern 6212907eea48SAmit Cohen void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i, 62130c5fcf9eSAmit Cohen bool offload, bool trap, bool offload_failed) 6214907eea48SAmit Cohen { 6215907eea48SAmit Cohen struct sk_buff *skb; 6216907eea48SAmit Cohen int err; 6217907eea48SAmit Cohen 6218d95d6320SEric Dumazet if (READ_ONCE(f6i->offload) == offload && 6219d95d6320SEric Dumazet READ_ONCE(f6i->trap) == trap && 6220d95d6320SEric Dumazet READ_ONCE(f6i->offload_failed) == offload_failed) 6221907eea48SAmit Cohen return; 6222907eea48SAmit Cohen 6223d95d6320SEric Dumazet WRITE_ONCE(f6i->offload, offload); 6224d95d6320SEric Dumazet WRITE_ONCE(f6i->trap, trap); 62256fad361aSAmit Cohen 62266fad361aSAmit Cohen /* 2 means send notifications only if offload_failed was changed. */ 62276fad361aSAmit Cohen if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 && 6228d95d6320SEric Dumazet READ_ONCE(f6i->offload_failed) == offload_failed) 62296fad361aSAmit Cohen return; 62306fad361aSAmit Cohen 6231d95d6320SEric Dumazet WRITE_ONCE(f6i->offload_failed, offload_failed); 6232907eea48SAmit Cohen 6233907eea48SAmit Cohen if (!rcu_access_pointer(f6i->fib6_node)) 6234907eea48SAmit Cohen /* The route was removed from the tree, do not send 623589e8347fSBhaskar Chowdhury * notification. 6236907eea48SAmit Cohen */ 6237907eea48SAmit Cohen return; 6238907eea48SAmit Cohen 6239907eea48SAmit Cohen if (!net->ipv6.sysctl.fib_notify_on_flag_change) 6240907eea48SAmit Cohen return; 6241907eea48SAmit Cohen 6242907eea48SAmit Cohen skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL); 6243907eea48SAmit Cohen if (!skb) { 6244907eea48SAmit Cohen err = -ENOBUFS; 6245907eea48SAmit Cohen goto errout; 6246907eea48SAmit Cohen } 6247907eea48SAmit Cohen 6248907eea48SAmit Cohen err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0, 6249907eea48SAmit Cohen 0, 0); 6250907eea48SAmit Cohen if (err < 0) { 6251907eea48SAmit Cohen /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 6252907eea48SAmit Cohen WARN_ON(err == -EMSGSIZE); 6253907eea48SAmit Cohen kfree_skb(skb); 6254907eea48SAmit Cohen goto errout; 6255907eea48SAmit Cohen } 6256907eea48SAmit Cohen 6257907eea48SAmit Cohen rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL); 6258907eea48SAmit Cohen return; 6259907eea48SAmit Cohen 6260907eea48SAmit Cohen errout: 6261907eea48SAmit Cohen rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 6262907eea48SAmit Cohen } 6263907eea48SAmit Cohen EXPORT_SYMBOL(fib6_info_hw_flags_set); 6264907eea48SAmit Cohen 62658ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 6266351638e7SJiri Pirko unsigned long event, void *ptr) 62678ed67789SDaniel Lezcano { 6268351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6269c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 62708ed67789SDaniel Lezcano 6271242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 6272242d3a49SWANG Cong return NOTIFY_OK; 6273242d3a49SWANG Cong 6274242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 62751cf844c7SDavid Ahern net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; 6276d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 62778ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 62788ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6279d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 62808ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 6281d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 62828ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 62838ed67789SDaniel Lezcano #endif 628476da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 628576da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 628676da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 628776da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 628876da0704SWANG Cong */ 628912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 6290242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 629112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 629212d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 6293242d3a49SWANG Cong #endif 62948ed67789SDaniel Lezcano } 62958ed67789SDaniel Lezcano 62968ed67789SDaniel Lezcano return NOTIFY_OK; 62978ed67789SDaniel Lezcano } 62988ed67789SDaniel Lezcano 62991da177e4SLinus Torvalds /* 63001da177e4SLinus Torvalds * /proc 63011da177e4SLinus Torvalds */ 63021da177e4SLinus Torvalds 63031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 63041da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 63051da177e4SLinus Torvalds { 630669ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 63071da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 630869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 630969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 631081eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 631169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 631269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 6313fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 631469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 63151da177e4SLinus Torvalds 63161da177e4SLinus Torvalds return 0; 63171da177e4SLinus Torvalds } 63181da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 63191da177e4SLinus Torvalds 63201da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 63211da177e4SLinus Torvalds 632232927393SChristoph Hellwig static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 632332927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 63241da177e4SLinus Torvalds { 6325c486da34SLucian Adrian Grijincu struct net *net; 6326c486da34SLucian Adrian Grijincu int delay; 6327f0fb9b28SAditya Pakki int ret; 6328c486da34SLucian Adrian Grijincu if (!write) 6329c486da34SLucian Adrian Grijincu return -EINVAL; 6330c486da34SLucian Adrian Grijincu 6331c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 6332c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 6333f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6334f0fb9b28SAditya Pakki if (ret) 6335f0fb9b28SAditya Pakki return ret; 6336f0fb9b28SAditya Pakki 63372ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 63381da177e4SLinus Torvalds return 0; 63391da177e4SLinus Torvalds } 63401da177e4SLinus Torvalds 6341ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 63421da177e4SLinus Torvalds { 634306e6c88fSAlexander Kuznetsov .procname = "max_size", 634406e6c88fSAlexander Kuznetsov .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 63451da177e4SLinus Torvalds .maxlen = sizeof(int), 634606e6c88fSAlexander Kuznetsov .mode = 0644, 634706e6c88fSAlexander Kuznetsov .proc_handler = proc_dointvec, 63481da177e4SLinus Torvalds }, 63491da177e4SLinus Torvalds { 63501da177e4SLinus Torvalds .procname = "gc_thresh", 63519a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 63521da177e4SLinus Torvalds .maxlen = sizeof(int), 63531da177e4SLinus Torvalds .mode = 0644, 63546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63551da177e4SLinus Torvalds }, 63561da177e4SLinus Torvalds { 635706e6c88fSAlexander Kuznetsov .procname = "flush", 635806e6c88fSAlexander Kuznetsov .data = &init_net.ipv6.sysctl.flush_delay, 63591da177e4SLinus Torvalds .maxlen = sizeof(int), 636006e6c88fSAlexander Kuznetsov .mode = 0200, 636106e6c88fSAlexander Kuznetsov .proc_handler = ipv6_sysctl_rtcache_flush 63621da177e4SLinus Torvalds }, 63631da177e4SLinus Torvalds { 63641da177e4SLinus Torvalds .procname = "gc_min_interval", 63654990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 63661da177e4SLinus Torvalds .maxlen = sizeof(int), 63671da177e4SLinus Torvalds .mode = 0644, 63686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63691da177e4SLinus Torvalds }, 63701da177e4SLinus Torvalds { 63711da177e4SLinus Torvalds .procname = "gc_timeout", 63724990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 63731da177e4SLinus Torvalds .maxlen = sizeof(int), 63741da177e4SLinus Torvalds .mode = 0644, 63756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63761da177e4SLinus Torvalds }, 63771da177e4SLinus Torvalds { 63781da177e4SLinus Torvalds .procname = "gc_interval", 63794990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 63801da177e4SLinus Torvalds .maxlen = sizeof(int), 63811da177e4SLinus Torvalds .mode = 0644, 63826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63831da177e4SLinus Torvalds }, 63841da177e4SLinus Torvalds { 63851da177e4SLinus Torvalds .procname = "gc_elasticity", 63864990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 63871da177e4SLinus Torvalds .maxlen = sizeof(int), 63881da177e4SLinus Torvalds .mode = 0644, 6389f3d3f616SMin Zhang .proc_handler = proc_dointvec, 63901da177e4SLinus Torvalds }, 63911da177e4SLinus Torvalds { 63921da177e4SLinus Torvalds .procname = "mtu_expires", 63934990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 63941da177e4SLinus Torvalds .maxlen = sizeof(int), 63951da177e4SLinus Torvalds .mode = 0644, 63966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63971da177e4SLinus Torvalds }, 63981da177e4SLinus Torvalds { 63991da177e4SLinus Torvalds .procname = "min_adv_mss", 64004990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 64011da177e4SLinus Torvalds .maxlen = sizeof(int), 64021da177e4SLinus Torvalds .mode = 0644, 6403f3d3f616SMin Zhang .proc_handler = proc_dointvec, 64041da177e4SLinus Torvalds }, 64051da177e4SLinus Torvalds { 64061da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 64074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 64081da177e4SLinus Torvalds .maxlen = sizeof(int), 64091da177e4SLinus Torvalds .mode = 0644, 64106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 64111da177e4SLinus Torvalds }, 64127c6bb7d2SDavid Ahern { 64137c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 64147c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 6415*ef62c0aeSEric Dumazet .maxlen = sizeof(u8), 64167c6bb7d2SDavid Ahern .mode = 0644, 6417*ef62c0aeSEric Dumazet .proc_handler = proc_dou8vec_minmax, 6418eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO, 6419eec4844fSMatteo Croce .extra2 = SYSCTL_ONE, 64207c6bb7d2SDavid Ahern }, 6421f8572d8fSEric W. Biederman { } 64221da177e4SLinus Torvalds }; 64231da177e4SLinus Torvalds 64242c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 6425760f2d01SDaniel Lezcano { 6426760f2d01SDaniel Lezcano struct ctl_table *table; 6427760f2d01SDaniel Lezcano 6428760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 6429760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 6430760f2d01SDaniel Lezcano GFP_KERNEL); 64315ee09105SYOSHIFUJI Hideaki 64325ee09105SYOSHIFUJI Hideaki if (table) { 643306e6c88fSAlexander Kuznetsov table[0].data = &net->ipv6.sysctl.ip6_rt_max_size; 643486393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 643506e6c88fSAlexander Kuznetsov table[2].data = &net->ipv6.sysctl.flush_delay; 643606e6c88fSAlexander Kuznetsov table[2].extra1 = net; 64375ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 64385ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 64395ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 64405ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 64415ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 64425ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 64439c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 64447c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 6445464dc801SEric W. Biederman 6446464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 6447464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 644806e6c88fSAlexander Kuznetsov table[1].procname = NULL; 64495ee09105SYOSHIFUJI Hideaki } 64505ee09105SYOSHIFUJI Hideaki 6451760f2d01SDaniel Lezcano return table; 6452760f2d01SDaniel Lezcano } 64531da177e4SLinus Torvalds #endif 64541da177e4SLinus Torvalds 64552c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 6456cdb18761SDaniel Lezcano { 6457633d424bSPavel Emelyanov int ret = -ENOMEM; 64588ed67789SDaniel Lezcano 645986393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 646086393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 6461f2fc6a54SBenjamin Thery 6462fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 6463fc66f95cSEric Dumazet goto out_ip6_dst_ops; 6464fc66f95cSEric Dumazet 64651cf844c7SDavid Ahern net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); 6466421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 6467421842edSDavid Ahern goto out_ip6_dst_entries; 64681cf844c7SDavid Ahern memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, 64691cf844c7SDavid Ahern sizeof(*net->ipv6.fib6_null_entry)); 6470421842edSDavid Ahern 64718ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 64728ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 64738ed67789SDaniel Lezcano GFP_KERNEL); 64748ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 6475421842edSDavid Ahern goto out_fib6_null_entry; 6476d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 647762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 647862fa8a84SDavid S. Miller ip6_template_metrics, true); 6479d288a162SWangyang Guo INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached); 64808ed67789SDaniel Lezcano 64818ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 6482feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 64838ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 64848ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 64858ed67789SDaniel Lezcano GFP_KERNEL); 648668fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 648768fffc67SPeter Zijlstra goto out_ip6_null_entry; 6488d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 648962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 649062fa8a84SDavid S. Miller ip6_template_metrics, true); 6491d288a162SWangyang Guo INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached); 64928ed67789SDaniel Lezcano 64938ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 64948ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 64958ed67789SDaniel Lezcano GFP_KERNEL); 649668fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 649768fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 6498d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 649962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 650062fa8a84SDavid S. Miller ip6_template_metrics, true); 6501d288a162SWangyang Guo INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached); 6502b9b33e7cSPaolo Abeni #ifdef CONFIG_IPV6_SUBTREES 6503b9b33e7cSPaolo Abeni net->ipv6.fib6_routes_require_src = 0; 6504b9b33e7cSPaolo Abeni #endif 65058ed67789SDaniel Lezcano #endif 65068ed67789SDaniel Lezcano 6507b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 6508af6d1034SJon Maxwell net->ipv6.sysctl.ip6_rt_max_size = INT_MAX; 6509b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 6510b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 6511b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 6512b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 6513b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 6514b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 65157c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 6516b339a47cSPeter Zijlstra 65179cb7c013SEric Dumazet atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ); 65186891a346SBenjamin Thery 65198ed67789SDaniel Lezcano ret = 0; 65208ed67789SDaniel Lezcano out: 65218ed67789SDaniel Lezcano return ret; 6522f2fc6a54SBenjamin Thery 652368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 652468fffc67SPeter Zijlstra out_ip6_prohibit_entry: 652568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 652668fffc67SPeter Zijlstra out_ip6_null_entry: 652768fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 652868fffc67SPeter Zijlstra #endif 6529421842edSDavid Ahern out_fib6_null_entry: 6530421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 6531fc66f95cSEric Dumazet out_ip6_dst_entries: 6532fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6533f2fc6a54SBenjamin Thery out_ip6_dst_ops: 6534f2fc6a54SBenjamin Thery goto out; 6535cdb18761SDaniel Lezcano } 6536cdb18761SDaniel Lezcano 65372c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 6538cdb18761SDaniel Lezcano { 6539421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 65408ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 65418ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 65428ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 65438ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 65448ed67789SDaniel Lezcano #endif 654541bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 6546cdb18761SDaniel Lezcano } 6547cdb18761SDaniel Lezcano 6548d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 6549d189634eSThomas Graf { 6550d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6551768b3c74SZhengchao Shao if (!proc_create_net("ipv6_route", 0, net->proc_net, 6552768b3c74SZhengchao Shao &ipv6_route_seq_ops, 6553768b3c74SZhengchao Shao sizeof(struct ipv6_route_iter))) 6554768b3c74SZhengchao Shao return -ENOMEM; 6555768b3c74SZhengchao Shao 6556768b3c74SZhengchao Shao if (!proc_create_net_single("rt6_stats", 0444, net->proc_net, 6557768b3c74SZhengchao Shao rt6_stats_seq_show, NULL)) { 6558768b3c74SZhengchao Shao remove_proc_entry("ipv6_route", net->proc_net); 6559768b3c74SZhengchao Shao return -ENOMEM; 6560768b3c74SZhengchao Shao } 6561d189634eSThomas Graf #endif 6562d189634eSThomas Graf return 0; 6563d189634eSThomas Graf } 6564d189634eSThomas Graf 6565d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 6566d189634eSThomas Graf { 6567d189634eSThomas Graf #ifdef CONFIG_PROC_FS 6568ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 6569ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 6570d189634eSThomas Graf #endif 6571d189634eSThomas Graf } 6572d189634eSThomas Graf 6573cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 6574cdb18761SDaniel Lezcano .init = ip6_route_net_init, 6575cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 6576cdb18761SDaniel Lezcano }; 6577cdb18761SDaniel Lezcano 6578c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 6579c3426b47SDavid S. Miller { 6580c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 6581c3426b47SDavid S. Miller 6582c3426b47SDavid S. Miller if (!bp) 6583c3426b47SDavid S. Miller return -ENOMEM; 6584c3426b47SDavid S. Miller inet_peer_base_init(bp); 6585c3426b47SDavid S. Miller net->ipv6.peers = bp; 6586c3426b47SDavid S. Miller return 0; 6587c3426b47SDavid S. Miller } 6588c3426b47SDavid S. Miller 6589c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 6590c3426b47SDavid S. Miller { 6591c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 6592c3426b47SDavid S. Miller 6593c3426b47SDavid S. Miller net->ipv6.peers = NULL; 659456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 6595c3426b47SDavid S. Miller kfree(bp); 6596c3426b47SDavid S. Miller } 6597c3426b47SDavid S. Miller 65982b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 6599c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 6600c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 6601c3426b47SDavid S. Miller }; 6602c3426b47SDavid S. Miller 6603d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 6604d189634eSThomas Graf .init = ip6_route_net_init_late, 6605d189634eSThomas Graf .exit = ip6_route_net_exit_late, 6606d189634eSThomas Graf }; 6607d189634eSThomas Graf 66088ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 66098ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 6610242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 66118ed67789SDaniel Lezcano }; 66128ed67789SDaniel Lezcano 66132f460933SWANG Cong void __init ip6_route_init_special_entries(void) 66142f460933SWANG Cong { 66152f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 66162f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 66172f460933SWANG Cong * manually for init_net */ 66181cf844c7SDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; 66192f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 66202f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 66212f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 66222f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 66232f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 66242f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 66252f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 66262f460933SWANG Cong #endif 66272f460933SWANG Cong } 66282f460933SWANG Cong 6629138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6630138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6631138d0be3SYonghong Song DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt) 6632138d0be3SYonghong Song 6633951cf368SYonghong Song BTF_ID_LIST(btf_fib6_info_id) 6634951cf368SYonghong Song BTF_ID(struct, fib6_info) 6635951cf368SYonghong Song 663614fc6bd6SYonghong Song static const struct bpf_iter_seq_info ipv6_route_seq_info = { 6637138d0be3SYonghong Song .seq_ops = &ipv6_route_seq_ops, 6638138d0be3SYonghong Song .init_seq_private = bpf_iter_init_seq_net, 6639138d0be3SYonghong Song .fini_seq_private = bpf_iter_fini_seq_net, 6640138d0be3SYonghong Song .seq_priv_size = sizeof(struct ipv6_route_iter), 664114fc6bd6SYonghong Song }; 664214fc6bd6SYonghong Song 664314fc6bd6SYonghong Song static struct bpf_iter_reg ipv6_route_reg_info = { 664414fc6bd6SYonghong Song .target = "ipv6_route", 66453c32cc1bSYonghong Song .ctx_arg_info_size = 1, 66463c32cc1bSYonghong Song .ctx_arg_info = { 66473c32cc1bSYonghong Song { offsetof(struct bpf_iter__ipv6_route, rt), 66483c32cc1bSYonghong Song PTR_TO_BTF_ID_OR_NULL }, 66493c32cc1bSYonghong Song }, 665014fc6bd6SYonghong Song .seq_info = &ipv6_route_seq_info, 6651138d0be3SYonghong Song }; 6652138d0be3SYonghong Song 665315172a46SYonghong Song static int __init bpf_iter_register(void) 665415172a46SYonghong Song { 6655951cf368SYonghong Song ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id; 665615172a46SYonghong Song return bpf_iter_reg_target(&ipv6_route_reg_info); 6657138d0be3SYonghong Song } 6658138d0be3SYonghong Song 6659138d0be3SYonghong Song static void bpf_iter_unregister(void) 6660138d0be3SYonghong Song { 6661ab2ee4fcSYonghong Song bpf_iter_unreg_target(&ipv6_route_reg_info); 6662138d0be3SYonghong Song } 6663138d0be3SYonghong Song #endif 6664138d0be3SYonghong Song #endif 6665138d0be3SYonghong Song 6666433d49c3SDaniel Lezcano int __init ip6_route_init(void) 66671da177e4SLinus Torvalds { 6668433d49c3SDaniel Lezcano int ret; 66698d0b94afSMartin KaFai Lau int cpu; 6670433d49c3SDaniel Lezcano 66719a7ec3a9SDaniel Lezcano ret = -ENOMEM; 66729a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 66739a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 66746126891cSVasily Averin SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL); 66759a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 6676c19a28e1SFernando Carrijo goto out; 667714e50e57SDavid S. Miller 6678fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 66798ed67789SDaniel Lezcano if (ret) 6680bdb3289fSDaniel Lezcano goto out_kmem_cache; 6681bdb3289fSDaniel Lezcano 6682c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 6683c3426b47SDavid S. Miller if (ret) 6684e8803b6cSDavid S. Miller goto out_dst_entries; 66852a0c451aSThomas Graf 66867e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 66877e52b33bSDavid S. Miller if (ret) 66887e52b33bSDavid S. Miller goto out_register_inetpeer; 6689c3426b47SDavid S. Miller 66905dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 66915dc121e9SArnaud Ebalard 6692e8803b6cSDavid S. Miller ret = fib6_init(); 6693433d49c3SDaniel Lezcano if (ret) 66948ed67789SDaniel Lezcano goto out_register_subsys; 6695433d49c3SDaniel Lezcano 6696433d49c3SDaniel Lezcano ret = xfrm6_init(); 6697433d49c3SDaniel Lezcano if (ret) 6698e8803b6cSDavid S. Miller goto out_fib6_init; 6699c35b7e72SDaniel Lezcano 6700433d49c3SDaniel Lezcano ret = fib6_rules_init(); 6701433d49c3SDaniel Lezcano if (ret) 6702433d49c3SDaniel Lezcano goto xfrm6_init; 67037e5449c2SDaniel Lezcano 6704d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 6705d189634eSThomas Graf if (ret) 6706d189634eSThomas Graf goto fib6_rules_init; 6707d189634eSThomas Graf 670816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 670916feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 671016feebcfSFlorian Westphal if (ret < 0) 671116feebcfSFlorian Westphal goto out_register_late_subsys; 671216feebcfSFlorian Westphal 671316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 671416feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 671516feebcfSFlorian Westphal if (ret < 0) 671616feebcfSFlorian Westphal goto out_register_late_subsys; 671716feebcfSFlorian Westphal 671816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 671916feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 672016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 672116feebcfSFlorian Westphal if (ret < 0) 6722d189634eSThomas Graf goto out_register_late_subsys; 6723433d49c3SDaniel Lezcano 67248ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 6725cdb18761SDaniel Lezcano if (ret) 6726d189634eSThomas Graf goto out_register_late_subsys; 67278ed67789SDaniel Lezcano 6728138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6729138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6730138d0be3SYonghong Song ret = bpf_iter_register(); 6731138d0be3SYonghong Song if (ret) 6732138d0be3SYonghong Song goto out_register_late_subsys; 6733138d0be3SYonghong Song #endif 6734138d0be3SYonghong Song #endif 6735138d0be3SYonghong Song 67368d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 67378d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 67388d0b94afSMartin KaFai Lau 67398d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 6740ba55ef81SEric Dumazet INIT_LIST_HEAD(&ul->quarantine); 67418d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 67428d0b94afSMartin KaFai Lau } 67438d0b94afSMartin KaFai Lau 6744433d49c3SDaniel Lezcano out: 6745433d49c3SDaniel Lezcano return ret; 6746433d49c3SDaniel Lezcano 6747d189634eSThomas Graf out_register_late_subsys: 674816feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6749d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6750433d49c3SDaniel Lezcano fib6_rules_init: 6751433d49c3SDaniel Lezcano fib6_rules_cleanup(); 6752433d49c3SDaniel Lezcano xfrm6_init: 6753433d49c3SDaniel Lezcano xfrm6_fini(); 67542a0c451aSThomas Graf out_fib6_init: 67552a0c451aSThomas Graf fib6_gc_cleanup(); 67568ed67789SDaniel Lezcano out_register_subsys: 67578ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 67587e52b33bSDavid S. Miller out_register_inetpeer: 67597e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 6760fc66f95cSEric Dumazet out_dst_entries: 6761fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 6762433d49c3SDaniel Lezcano out_kmem_cache: 6763f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6764433d49c3SDaniel Lezcano goto out; 67651da177e4SLinus Torvalds } 67661da177e4SLinus Torvalds 67671da177e4SLinus Torvalds void ip6_route_cleanup(void) 67681da177e4SLinus Torvalds { 6769138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6) 6770138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 6771138d0be3SYonghong Song bpf_iter_unregister(); 6772138d0be3SYonghong Song #endif 6773138d0be3SYonghong Song #endif 67748ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 6775d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6776101367c2SThomas Graf fib6_rules_cleanup(); 67771da177e4SLinus Torvalds xfrm6_fini(); 67781da177e4SLinus Torvalds fib6_gc_cleanup(); 6779c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 67808ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 678141bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 6782f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 67831da177e4SLinus Torvalds } 6784