12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * FIB front-end. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds /* Changes: 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 131da177e4SLinus Torvalds * reworked default router selection. 141da177e4SLinus Torvalds * - respect outgoing interface 151da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 161da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 171da177e4SLinus Torvalds * - always select the same router if it is (probably) 181da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 19c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 20c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 24f3213831SJoe Perches 254fc268d2SRandy Dunlap #include <linux/capability.h> 261da177e4SLinus Torvalds #include <linux/errno.h> 27bc3b2d7fSPaul Gortmaker #include <linux/export.h> 281da177e4SLinus Torvalds #include <linux/types.h> 291da177e4SLinus Torvalds #include <linux/times.h> 301da177e4SLinus Torvalds #include <linux/socket.h> 311da177e4SLinus Torvalds #include <linux/sockios.h> 321da177e4SLinus Torvalds #include <linux/net.h> 331da177e4SLinus Torvalds #include <linux/route.h> 341da177e4SLinus Torvalds #include <linux/netdevice.h> 351da177e4SLinus Torvalds #include <linux/in6.h> 367bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 371da177e4SLinus Torvalds #include <linux/init.h> 381da177e4SLinus Torvalds #include <linux/if_arp.h> 391da177e4SLinus Torvalds #include <linux/proc_fs.h> 401da177e4SLinus Torvalds #include <linux/seq_file.h> 415b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 4335732d01SWei Wang #include <linux/jhash.h> 44457c4cbcSEric W. Biederman #include <net/net_namespace.h> 451da177e4SLinus Torvalds #include <net/snmp.h> 461da177e4SLinus Torvalds #include <net/ipv6.h> 471da177e4SLinus Torvalds #include <net/ip6_fib.h> 481da177e4SLinus Torvalds #include <net/ip6_route.h> 491da177e4SLinus Torvalds #include <net/ndisc.h> 501da177e4SLinus Torvalds #include <net/addrconf.h> 511da177e4SLinus Torvalds #include <net/tcp.h> 521da177e4SLinus Torvalds #include <linux/rtnetlink.h> 531da177e4SLinus Torvalds #include <net/dst.h> 54904af04dSJiri Benc #include <net/dst_metadata.h> 551da177e4SLinus Torvalds #include <net/xfrm.h> 568d71740cSTom Tucker #include <net/netevent.h> 5721713ebcSThomas Graf #include <net/netlink.h> 583c618c1dSDavid Ahern #include <net/rtnh.h> 5919e42e45SRoopa Prabhu #include <net/lwtunnel.h> 60904af04dSJiri Benc #include <net/ip_tunnels.h> 61ca254490SDavid Ahern #include <net/l3mdev.h> 62eacb9384SRoopa Prabhu #include <net/ip.h> 637c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 661da177e4SLinus Torvalds #include <linux/sysctl.h> 671da177e4SLinus Torvalds #endif 681da177e4SLinus Torvalds 6930d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type); 7030d444d3SDavid Ahern 7130d444d3SDavid Ahern #define CREATE_TRACE_POINTS 7230d444d3SDavid Ahern #include <trace/events/fib6.h> 7330d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); 7430d444d3SDavid Ahern #undef CREATE_TRACE_POINTS 7530d444d3SDavid Ahern 76afc154e9SHannes Frederic Sowa enum rt6_nud_state { 777e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 787e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 797e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 80afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 81afc154e9SHannes Frederic Sowa }; 82afc154e9SHannes Frederic Sowa 831da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 840dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 85ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 861da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 881da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 891da177e4SLinus Torvalds struct net_device *dev, int how); 90569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 93ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 947150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 95ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 961da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 976700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 986700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 996700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 1006700c270SDavid S. Miller struct sk_buff *skb); 101702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 102702cea56SDavid Ahern int strict); 103a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i); 104d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1058d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 106d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 10716a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10816a16cd3SDavid Ahern unsigned int flags); 1097e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 110510e2cedSWei Wang const struct in6_addr *daddr, 111510e2cedSWei Wang const struct in6_addr *saddr); 1121da177e4SLinus Torvalds 11370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1148d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 115b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 116830218c1SDavid Ahern const struct in6_addr *gwaddr, 117830218c1SDavid Ahern struct net_device *dev, 11895c96174SEric Dumazet unsigned int pref); 1198d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 120b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 121830218c1SDavid Ahern const struct in6_addr *gwaddr, 122830218c1SDavid Ahern struct net_device *dev); 12370ceb4f5SYOSHIFUJI Hideaki #endif 12470ceb4f5SYOSHIFUJI Hideaki 1258d0b94afSMartin KaFai Lau struct uncached_list { 1268d0b94afSMartin KaFai Lau spinlock_t lock; 1278d0b94afSMartin KaFai Lau struct list_head head; 1288d0b94afSMartin KaFai Lau }; 1298d0b94afSMartin KaFai Lau 1308d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1318d0b94afSMartin KaFai Lau 132510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1338d0b94afSMartin KaFai Lau { 1348d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1358d0b94afSMartin KaFai Lau 1368d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1378d0b94afSMartin KaFai Lau 1388d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1398d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1408d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1418d0b94afSMartin KaFai Lau } 1428d0b94afSMartin KaFai Lau 143510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1448d0b94afSMartin KaFai Lau { 1458d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1468d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 14781eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1488d0b94afSMartin KaFai Lau 1498d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1508d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15181eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1528d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1538d0b94afSMartin KaFai Lau } 1548d0b94afSMartin KaFai Lau } 1558d0b94afSMartin KaFai Lau 1568d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1578d0b94afSMartin KaFai Lau { 1588d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1598d0b94afSMartin KaFai Lau int cpu; 1608d0b94afSMartin KaFai Lau 161e332bc67SEric W. Biederman if (dev == loopback_dev) 162e332bc67SEric W. Biederman return; 163e332bc67SEric W. Biederman 1648d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1658d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1668d0b94afSMartin KaFai Lau struct rt6_info *rt; 1678d0b94afSMartin KaFai Lau 1688d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1698d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1708d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1718d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1728d0b94afSMartin KaFai Lau 173e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1748d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1758d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1768d0b94afSMartin KaFai Lau } 1778d0b94afSMartin KaFai Lau 178e332bc67SEric W. Biederman if (rt_dev == dev) { 1798d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1808d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1818d0b94afSMartin KaFai Lau dev_put(rt_dev); 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1858d0b94afSMartin KaFai Lau } 1868d0b94afSMartin KaFai Lau } 1878d0b94afSMartin KaFai Lau 188f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 189f894cbf8SDavid S. Miller struct sk_buff *skb, 190f894cbf8SDavid S. Miller const void *daddr) 19139232973SDavid S. Miller { 192a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19339232973SDavid S. Miller return (const void *) p; 194f894cbf8SDavid S. Miller else if (skb) 195f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 19639232973SDavid S. Miller return daddr; 19739232973SDavid S. Miller } 19839232973SDavid S. Miller 199f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 200f8a1b43bSDavid Ahern struct net_device *dev, 201f894cbf8SDavid S. Miller struct sk_buff *skb, 202f894cbf8SDavid S. Miller const void *daddr) 203d3aaeb38SDavid S. Miller { 20439232973SDavid S. Miller struct neighbour *n; 20539232973SDavid S. Miller 206f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 207f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 208f83c7790SDavid S. Miller if (n) 209f83c7790SDavid S. Miller return n; 2107adf3246SStefano Brivio 2117adf3246SStefano Brivio n = neigh_create(&nd_tbl, daddr, dev); 2127adf3246SStefano Brivio return IS_ERR(n) ? NULL : n; 213f8a1b43bSDavid Ahern } 214f8a1b43bSDavid Ahern 215f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 216f8a1b43bSDavid Ahern struct sk_buff *skb, 217f8a1b43bSDavid Ahern const void *daddr) 218f8a1b43bSDavid Ahern { 219f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 220f8a1b43bSDavid Ahern 221f8a1b43bSDavid Ahern return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); 222f83c7790SDavid S. Miller } 223f83c7790SDavid S. Miller 22463fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22563fca65dSJulian Anastasov { 22663fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22763fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 22863fca65dSJulian Anastasov 229f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); 23063fca65dSJulian Anastasov if (!daddr) 23163fca65dSJulian Anastasov return; 23263fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23363fca65dSJulian Anastasov return; 23463fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23563fca65dSJulian Anastasov return; 23663fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23763fca65dSJulian Anastasov } 23863fca65dSJulian Anastasov 2399a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2401da177e4SLinus Torvalds .family = AF_INET6, 2411da177e4SLinus Torvalds .gc = ip6_dst_gc, 2421da177e4SLinus Torvalds .gc_thresh = 1024, 2431da177e4SLinus Torvalds .check = ip6_dst_check, 2440dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 245ebb762f2SSteffen Klassert .mtu = ip6_mtu, 246d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2471da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2481da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2491da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2501da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2511da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2526e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2539f8955ccSEric W. Biederman .local_out = __ip6_local_out, 254f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 25563fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2561da177e4SLinus Torvalds }; 2571da177e4SLinus Torvalds 258ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 259ec831ea7SRoland Dreier { 260618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 261618f9bc7SSteffen Klassert 262618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 263ec831ea7SRoland Dreier } 264ec831ea7SRoland Dreier 2656700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2666700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 26714e50e57SDavid S. Miller { 26814e50e57SDavid S. Miller } 26914e50e57SDavid S. Miller 2706700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2716700c270SDavid S. Miller struct sk_buff *skb) 272b587ee3bSDavid S. Miller { 273b587ee3bSDavid S. Miller } 274b587ee3bSDavid S. Miller 27514e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27614e50e57SDavid S. Miller .family = AF_INET6, 27714e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 27814e50e57SDavid S. Miller .check = ip6_dst_check, 279ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 280214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28114e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 282b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2830a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 284f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 28514e50e57SDavid S. Miller }; 28614e50e57SDavid S. Miller 28762fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 28814edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 28962fa8a84SDavid S. Miller }; 29062fa8a84SDavid S. Miller 2918d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 29293c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 29393c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 29493c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 295f05713e0SEric Dumazet .fib6_ref = REFCOUNT_INIT(1), 296421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 297421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 298421842edSDavid Ahern }; 299421842edSDavid Ahern 300fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 3011da177e4SLinus Torvalds .dst = { 3021da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3031da177e4SLinus Torvalds .__use = 1, 3042c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3051da177e4SLinus Torvalds .error = -ENETUNREACH, 3061da177e4SLinus Torvalds .input = ip6_pkt_discard, 3071da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3081da177e4SLinus Torvalds }, 3091da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3101da177e4SLinus Torvalds }; 3111da177e4SLinus Torvalds 312101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 313101367c2SThomas Graf 314fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 315101367c2SThomas Graf .dst = { 316101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 317101367c2SThomas Graf .__use = 1, 3182c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 319101367c2SThomas Graf .error = -EACCES, 3209ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3219ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 322101367c2SThomas Graf }, 323101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 324101367c2SThomas Graf }; 325101367c2SThomas Graf 326fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 327101367c2SThomas Graf .dst = { 328101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 329101367c2SThomas Graf .__use = 1, 3302c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 331101367c2SThomas Graf .error = -EINVAL, 332352e512cSHerbert Xu .input = dst_discard, 333ede2059dSEric W. Biederman .output = dst_discard_out, 334101367c2SThomas Graf }, 335101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 336101367c2SThomas Graf }; 337101367c2SThomas Graf 338101367c2SThomas Graf #endif 339101367c2SThomas Graf 340ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 341ebfa45f0SMartin KaFai Lau { 342ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 343ebfa45f0SMartin KaFai Lau 344ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 345ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 346ebfa45f0SMartin KaFai Lau } 347ebfa45f0SMartin KaFai Lau 3481da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 34993531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 350ad706862SMartin KaFai Lau int flags) 3511da177e4SLinus Torvalds { 35297bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 353b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 354cf911662SDavid S. Miller 35581eb8447SWei Wang if (rt) { 356ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 35781eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 35881eb8447SWei Wang } 3598104891bSSteffen Klassert 360cf911662SDavid S. Miller return rt; 3611da177e4SLinus Torvalds } 3629ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 363d52d3997SMartin KaFai Lau 3641da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 367a68886a6SDavid Ahern struct fib6_info *from; 3688d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3691da177e4SLinus Torvalds 3701620a336SDavid Ahern ip_dst_metrics_put(dst); 3718d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3728d0b94afSMartin KaFai Lau 3738d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37438308473SDavid S. Miller if (idev) { 3751da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3761da177e4SLinus Torvalds in6_dev_put(idev); 3771da177e4SLinus Torvalds } 3781716a961SGao feng 3790e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt->from, NULL); 38093531c67SDavid Ahern fib6_info_release(from); 381b3419363SDavid S. Miller } 382b3419363SDavid S. Miller 3831da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3841da177e4SLinus Torvalds int how) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3871da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3885a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 389c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3901da177e4SLinus Torvalds 391e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 392e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 39338308473SDavid S. Miller if (loopback_idev) { 3941da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3951da177e4SLinus Torvalds in6_dev_put(idev); 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds } 39897cac082SDavid S. Miller } 3991da177e4SLinus Torvalds 4005973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4015973fb1eSMartin KaFai Lau { 4025973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4035973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4045973fb1eSMartin KaFai Lau else 4055973fb1eSMartin KaFai Lau return false; 4065973fb1eSMartin KaFai Lau } 4075973fb1eSMartin KaFai Lau 408a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4091da177e4SLinus Torvalds { 410a68886a6SDavid Ahern struct fib6_info *from; 411a68886a6SDavid Ahern 412a68886a6SDavid Ahern from = rcu_dereference(rt->from); 413a68886a6SDavid Ahern 4141716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4151716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 416a50feda5SEric Dumazet return true; 417a68886a6SDavid Ahern } else if (from) { 4181e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 419a68886a6SDavid Ahern fib6_check_expired(from); 4201716a961SGao feng } 421a50feda5SEric Dumazet return false; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 424b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res, 425b1d40991SDavid Ahern struct flowi6 *fl6, int oif, bool have_oif_match, 426b1d40991SDavid Ahern const struct sk_buff *skb, int strict) 42751ebd318SNicolas Dichtel { 4288d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 429b1d40991SDavid Ahern struct fib6_info *match = res->f6i; 430b1d40991SDavid Ahern 431f88d8ea6SDavid Ahern if ((!match->fib6_nsiblings && !match->nh) || have_oif_match) 432b1d40991SDavid Ahern goto out; 43351ebd318SNicolas Dichtel 434b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 435b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 436b673d6ccSJakub Sitnicki */ 437f88d8ea6SDavid Ahern if (!fl6->mp_hash && 438f88d8ea6SDavid Ahern (!match->nh || nexthop_is_multipath(match->nh))) 439b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 440b673d6ccSJakub Sitnicki 441f88d8ea6SDavid Ahern if (unlikely(match->nh)) { 442f88d8ea6SDavid Ahern nexthop_path_fib6_result(res, fl6->mp_hash); 443f88d8ea6SDavid Ahern return; 444f88d8ea6SDavid Ahern } 445f88d8ea6SDavid Ahern 4461cf844c7SDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) 447b1d40991SDavid Ahern goto out; 448bbfcd776SIdo Schimmel 44993c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 45093c2fb25SDavid Ahern fib6_siblings) { 4511cf844c7SDavid Ahern const struct fib6_nh *nh = sibling->fib6_nh; 4525e670d84SDavid Ahern int nh_upper_bound; 4535e670d84SDavid Ahern 454702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4555e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4563d709f69SIdo Schimmel continue; 457702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 45852bd4c0cSNicolas Dichtel break; 45951ebd318SNicolas Dichtel match = sibling; 46051ebd318SNicolas Dichtel break; 46151ebd318SNicolas Dichtel } 4623d709f69SIdo Schimmel 463b1d40991SDavid Ahern out: 464b1d40991SDavid Ahern res->f6i = match; 4651cf844c7SDavid Ahern res->nh = match->fib6_nh; 46651ebd318SNicolas Dichtel } 46751ebd318SNicolas Dichtel 4681da177e4SLinus Torvalds /* 46966f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4701da177e4SLinus Torvalds */ 4711da177e4SLinus Torvalds 4720c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 4730c59d006SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4740c59d006SDavid Ahern { 4750c59d006SDavid Ahern const struct net_device *dev; 4760c59d006SDavid Ahern 4770c59d006SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 4780c59d006SDavid Ahern return false; 4790c59d006SDavid Ahern 4800c59d006SDavid Ahern dev = nh->fib_nh_dev; 4810c59d006SDavid Ahern if (oif) { 4820c59d006SDavid Ahern if (dev->ifindex == oif) 4830c59d006SDavid Ahern return true; 4840c59d006SDavid Ahern } else { 4850c59d006SDavid Ahern if (ipv6_chk_addr(net, saddr, dev, 4860c59d006SDavid Ahern flags & RT6_LOOKUP_F_IFACE)) 4870c59d006SDavid Ahern return true; 4880c59d006SDavid Ahern } 4890c59d006SDavid Ahern 4900c59d006SDavid Ahern return false; 4910c59d006SDavid Ahern } 4920c59d006SDavid Ahern 493962b6803SDavid Ahern struct fib6_nh_dm_arg { 494962b6803SDavid Ahern struct net *net; 495962b6803SDavid Ahern const struct in6_addr *saddr; 496962b6803SDavid Ahern int oif; 497962b6803SDavid Ahern int flags; 498962b6803SDavid Ahern struct fib6_nh *nh; 499962b6803SDavid Ahern }; 500962b6803SDavid Ahern 501962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) 502962b6803SDavid Ahern { 503962b6803SDavid Ahern struct fib6_nh_dm_arg *arg = _arg; 504962b6803SDavid Ahern 505962b6803SDavid Ahern arg->nh = nh; 506962b6803SDavid Ahern return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, 507962b6803SDavid Ahern arg->flags); 508962b6803SDavid Ahern } 509962b6803SDavid Ahern 510962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */ 511962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, 512962b6803SDavid Ahern struct fib6_result *res, 513962b6803SDavid Ahern const struct in6_addr *saddr, 514962b6803SDavid Ahern int oif, int flags) 515962b6803SDavid Ahern { 516962b6803SDavid Ahern struct fib6_nh_dm_arg arg = { 517962b6803SDavid Ahern .net = net, 518962b6803SDavid Ahern .saddr = saddr, 519962b6803SDavid Ahern .oif = oif, 520962b6803SDavid Ahern .flags = flags, 521962b6803SDavid Ahern }; 522962b6803SDavid Ahern 523962b6803SDavid Ahern if (nexthop_is_blackhole(nh)) 524962b6803SDavid Ahern return NULL; 525962b6803SDavid Ahern 526962b6803SDavid Ahern if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) 527962b6803SDavid Ahern return arg.nh; 528962b6803SDavid Ahern 529962b6803SDavid Ahern return NULL; 530962b6803SDavid Ahern } 531962b6803SDavid Ahern 53275ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res, 53375ef7389SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 5341da177e4SLinus Torvalds { 53575ef7389SDavid Ahern struct fib6_info *f6i = res->f6i; 53675ef7389SDavid Ahern struct fib6_info *spf6i; 53775ef7389SDavid Ahern struct fib6_nh *nh; 5381da177e4SLinus Torvalds 53975ef7389SDavid Ahern if (!oif && ipv6_addr_any(saddr)) { 540f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 541f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 542f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 543f88d8ea6SDavid Ahern goto out_blackhole; 544f88d8ea6SDavid Ahern } else { 5451cf844c7SDavid Ahern nh = f6i->fib6_nh; 546f88d8ea6SDavid Ahern } 5477d21fec9SDavid Ahern if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 5487d21fec9SDavid Ahern goto out; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 55175ef7389SDavid Ahern for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 552962b6803SDavid Ahern bool matched = false; 553962b6803SDavid Ahern 554962b6803SDavid Ahern if (unlikely(spf6i->nh)) { 555962b6803SDavid Ahern nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, 556962b6803SDavid Ahern oif, flags); 557962b6803SDavid Ahern if (nh) 558962b6803SDavid Ahern matched = true; 559962b6803SDavid Ahern } else { 5601cf844c7SDavid Ahern nh = spf6i->fib6_nh; 561962b6803SDavid Ahern if (__rt6_device_match(net, nh, saddr, oif, flags)) 562962b6803SDavid Ahern matched = true; 563962b6803SDavid Ahern } 564962b6803SDavid Ahern if (matched) { 56575ef7389SDavid Ahern res->f6i = spf6i; 5667d21fec9SDavid Ahern goto out; 56775ef7389SDavid Ahern } 56875ef7389SDavid Ahern } 5691da177e4SLinus Torvalds 57075ef7389SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) { 57175ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5721cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 5737d21fec9SDavid Ahern goto out; 57475ef7389SDavid Ahern } 57575ef7389SDavid Ahern 576f88d8ea6SDavid Ahern if (unlikely(f6i->nh)) { 577f88d8ea6SDavid Ahern nh = nexthop_fib6_nh(f6i->nh); 578f88d8ea6SDavid Ahern if (nexthop_is_blackhole(f6i->nh)) 579f88d8ea6SDavid Ahern goto out_blackhole; 580f88d8ea6SDavid Ahern } else { 5811cf844c7SDavid Ahern nh = f6i->fib6_nh; 582f88d8ea6SDavid Ahern } 583f88d8ea6SDavid Ahern 5847d21fec9SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) { 58575ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5861cf844c7SDavid Ahern nh = res->f6i->fib6_nh; 58775ef7389SDavid Ahern } 5887d21fec9SDavid Ahern out: 5897d21fec9SDavid Ahern res->nh = nh; 5907d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 5917d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 592f88d8ea6SDavid Ahern return; 593f88d8ea6SDavid Ahern 594f88d8ea6SDavid Ahern out_blackhole: 595f88d8ea6SDavid Ahern res->fib6_flags |= RTF_REJECT; 596f88d8ea6SDavid Ahern res->fib6_type = RTN_BLACKHOLE; 597f88d8ea6SDavid Ahern res->nh = nh; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 60027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 601c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 602c2f17e82SHannes Frederic Sowa struct work_struct work; 603c2f17e82SHannes Frederic Sowa struct in6_addr target; 604c2f17e82SHannes Frederic Sowa struct net_device *dev; 605c2f17e82SHannes Frederic Sowa }; 606c2f17e82SHannes Frederic Sowa 607c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 608c2f17e82SHannes Frederic Sowa { 609c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 610c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 611c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 612c2f17e82SHannes Frederic Sowa 613c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 614adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 615c2f17e82SHannes Frederic Sowa dev_put(work->dev); 616662f5533SMichael Büsch kfree(work); 617c2f17e82SHannes Frederic Sowa } 618c2f17e82SHannes Frederic Sowa 619cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 62027097255SYOSHIFUJI Hideaki { 621f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 6225e670d84SDavid Ahern const struct in6_addr *nh_gw; 623f2c31e32SEric Dumazet struct neighbour *neigh; 6245e670d84SDavid Ahern struct net_device *dev; 625f547fac6SSabrina Dubroca struct inet6_dev *idev; 6265e670d84SDavid Ahern 62727097255SYOSHIFUJI Hideaki /* 62827097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 62927097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 63027097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 63127097255SYOSHIFUJI Hideaki * 63227097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 63327097255SYOSHIFUJI Hideaki * to no more than one per minute. 63427097255SYOSHIFUJI Hideaki */ 635cc3a86c8SDavid Ahern if (fib6_nh->fib_nh_gw_family) 636fdd6681dSAmerigo Wang return; 6375e670d84SDavid Ahern 638cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 639cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 6402152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 641f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 6425e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 6432152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 6448d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 6458d6c31bfSMartin KaFai Lau goto out; 6468d6c31bfSMartin KaFai Lau 6472152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 648990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 649990edb42SMartin KaFai Lau time_after(jiffies, 650dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 651c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 652990edb42SMartin KaFai Lau if (work) 6537e980569SJiri Benc __neigh_set_probe_once(neigh); 654990edb42SMartin KaFai Lau } 655c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 656cc3a86c8SDavid Ahern } else if (time_after(jiffies, fib6_nh->last_probe + 657f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 658990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 659990edb42SMartin KaFai Lau } 660c2f17e82SHannes Frederic Sowa 661c2f17e82SHannes Frederic Sowa if (work) { 662cc3a86c8SDavid Ahern fib6_nh->last_probe = jiffies; 663c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 6645e670d84SDavid Ahern work->target = *nh_gw; 6655e670d84SDavid Ahern dev_hold(dev); 6665e670d84SDavid Ahern work->dev = dev; 667c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 668c2f17e82SHannes Frederic Sowa } 669990edb42SMartin KaFai Lau 6708d6c31bfSMartin KaFai Lau out: 6712152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 672f2c31e32SEric Dumazet } 67327097255SYOSHIFUJI Hideaki #else 674cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 67527097255SYOSHIFUJI Hideaki { 67627097255SYOSHIFUJI Hideaki } 67727097255SYOSHIFUJI Hideaki #endif 67827097255SYOSHIFUJI Hideaki 6791da177e4SLinus Torvalds /* 680554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6811da177e4SLinus Torvalds */ 6821ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 6831da177e4SLinus Torvalds { 684afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 6855e670d84SDavid Ahern struct neighbour *neigh; 686f2c31e32SEric Dumazet 687145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6881ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 6891ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 690145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 691145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 692554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 693afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 694398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 695a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 696afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6977e980569SJiri Benc else 6987e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 699398bcbebSYOSHIFUJI Hideaki #endif 700145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 701afc154e9SHannes Frederic Sowa } else { 702afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 7037e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 704a5a81f0bSPaul Marks } 705145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 706145a3621SYOSHIFUJI Hideaki / 吉藤英明 707a5a81f0bSPaul Marks return ret; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 711702cea56SDavid Ahern int strict) 712554cfb7eSYOSHIFUJI Hideaki { 7136e1809a5SDavid Ahern int m = 0; 7144d0c5911SYOSHIFUJI Hideaki 7156e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 7166e1809a5SDavid Ahern m = 2; 7176e1809a5SDavid Ahern 71877d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 719afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 720ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 721702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 722ebacaaa0SYOSHIFUJI Hideaki #endif 7231ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 724702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 7251ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 726afc154e9SHannes Frederic Sowa if (n < 0) 727afc154e9SHannes Frederic Sowa return n; 728afc154e9SHannes Frederic Sowa } 729554cfb7eSYOSHIFUJI Hideaki return m; 730554cfb7eSYOSHIFUJI Hideaki } 731554cfb7eSYOSHIFUJI Hideaki 73228679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 73328679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 734554cfb7eSYOSHIFUJI Hideaki { 735afc154e9SHannes Frederic Sowa bool match_do_rr = false; 73628679ed1SDavid Ahern bool rc = false; 73728679ed1SDavid Ahern int m; 73835103d11SAndy Gospodarek 73928679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 7408067bb8cSIdo Schimmel goto out; 7418067bb8cSIdo Schimmel 74228679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 74328679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 744d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 74535103d11SAndy Gospodarek goto out; 746554cfb7eSYOSHIFUJI Hideaki 74728679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 7487e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 749afc154e9SHannes Frederic Sowa match_do_rr = true; 750afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 7517e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 752f11e6659SDavid S. Miller goto out; 7531da177e4SLinus Torvalds } 754f11e6659SDavid S. Miller 755afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 75628679ed1SDavid Ahern rt6_probe(nh); 757afc154e9SHannes Frederic Sowa 7587e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 759afc154e9SHannes Frederic Sowa if (m > *mpri) { 760afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 761afc154e9SHannes Frederic Sowa *mpri = m; 76228679ed1SDavid Ahern rc = true; 763afc154e9SHannes Frederic Sowa } 764f11e6659SDavid S. Miller out: 76528679ed1SDavid Ahern return rc; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 76817a5984eSDavid Ahern struct fib6_nh_frl_arg { 76917a5984eSDavid Ahern u32 flags; 77017a5984eSDavid Ahern int oif; 77117a5984eSDavid Ahern int strict; 77217a5984eSDavid Ahern int *mpri; 77317a5984eSDavid Ahern bool *do_rr; 77417a5984eSDavid Ahern struct fib6_nh *nh; 77517a5984eSDavid Ahern }; 77617a5984eSDavid Ahern 77717a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) 77817a5984eSDavid Ahern { 77917a5984eSDavid Ahern struct fib6_nh_frl_arg *arg = _arg; 78017a5984eSDavid Ahern 78117a5984eSDavid Ahern arg->nh = nh; 78217a5984eSDavid Ahern return find_match(nh, arg->flags, arg->oif, arg->strict, 78317a5984eSDavid Ahern arg->mpri, arg->do_rr); 78417a5984eSDavid Ahern } 78517a5984eSDavid Ahern 786b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start, 78730c15f03SDavid Ahern struct fib6_info *nomatch, u32 metric, 788b7bc4b6aSDavid Ahern struct fib6_result *res, struct fib6_info **cont, 78930c15f03SDavid Ahern int oif, int strict, bool *do_rr, int *mpri) 79030c15f03SDavid Ahern { 791b7bc4b6aSDavid Ahern struct fib6_info *f6i; 79230c15f03SDavid Ahern 793b7bc4b6aSDavid Ahern for (f6i = f6i_start; 794b7bc4b6aSDavid Ahern f6i && f6i != nomatch; 795b7bc4b6aSDavid Ahern f6i = rcu_dereference(f6i->fib6_next)) { 79617a5984eSDavid Ahern bool matched = false; 79730c15f03SDavid Ahern struct fib6_nh *nh; 79830c15f03SDavid Ahern 799b7bc4b6aSDavid Ahern if (cont && f6i->fib6_metric != metric) { 800b7bc4b6aSDavid Ahern *cont = f6i; 80130c15f03SDavid Ahern return; 80230c15f03SDavid Ahern } 80330c15f03SDavid Ahern 804b7bc4b6aSDavid Ahern if (fib6_check_expired(f6i)) 80530c15f03SDavid Ahern continue; 80630c15f03SDavid Ahern 80717a5984eSDavid Ahern if (unlikely(f6i->nh)) { 80817a5984eSDavid Ahern struct fib6_nh_frl_arg arg = { 80917a5984eSDavid Ahern .flags = f6i->fib6_flags, 81017a5984eSDavid Ahern .oif = oif, 81117a5984eSDavid Ahern .strict = strict, 81217a5984eSDavid Ahern .mpri = mpri, 81317a5984eSDavid Ahern .do_rr = do_rr 81417a5984eSDavid Ahern }; 81517a5984eSDavid Ahern 81617a5984eSDavid Ahern if (nexthop_is_blackhole(f6i->nh)) { 81717a5984eSDavid Ahern res->fib6_flags = RTF_REJECT; 81817a5984eSDavid Ahern res->fib6_type = RTN_BLACKHOLE; 81917a5984eSDavid Ahern res->f6i = f6i; 82017a5984eSDavid Ahern res->nh = nexthop_fib6_nh(f6i->nh); 82117a5984eSDavid Ahern return; 82217a5984eSDavid Ahern } 82317a5984eSDavid Ahern if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, 82417a5984eSDavid Ahern &arg)) { 82517a5984eSDavid Ahern matched = true; 82617a5984eSDavid Ahern nh = arg.nh; 82717a5984eSDavid Ahern } 82817a5984eSDavid Ahern } else { 8291cf844c7SDavid Ahern nh = f6i->fib6_nh; 83017a5984eSDavid Ahern if (find_match(nh, f6i->fib6_flags, oif, strict, 83117a5984eSDavid Ahern mpri, do_rr)) 83217a5984eSDavid Ahern matched = true; 83317a5984eSDavid Ahern } 83417a5984eSDavid Ahern if (matched) { 835b7bc4b6aSDavid Ahern res->f6i = f6i; 836b7bc4b6aSDavid Ahern res->nh = nh; 8377d21fec9SDavid Ahern res->fib6_flags = f6i->fib6_flags; 8387d21fec9SDavid Ahern res->fib6_type = f6i->fib6_type; 839b7bc4b6aSDavid Ahern } 84030c15f03SDavid Ahern } 84130c15f03SDavid Ahern } 84230c15f03SDavid Ahern 843b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 844b7bc4b6aSDavid Ahern struct fib6_info *rr_head, int oif, int strict, 845b7bc4b6aSDavid Ahern bool *do_rr, struct fib6_result *res) 846f11e6659SDavid S. Miller { 847b7bc4b6aSDavid Ahern u32 metric = rr_head->fib6_metric; 848b7bc4b6aSDavid Ahern struct fib6_info *cont = NULL; 849f11e6659SDavid S. Miller int mpri = -1; 850f11e6659SDavid S. Miller 851b7bc4b6aSDavid Ahern __find_rr_leaf(rr_head, NULL, metric, res, &cont, 85230c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8539fbdcfafSSteffen Klassert 854b7bc4b6aSDavid Ahern __find_rr_leaf(leaf, rr_head, metric, res, &cont, 85530c15f03SDavid Ahern oif, strict, do_rr, &mpri); 8569fbdcfafSSteffen Klassert 857b7bc4b6aSDavid Ahern if (res->f6i || !cont) 858b7bc4b6aSDavid Ahern return; 8599fbdcfafSSteffen Klassert 860b7bc4b6aSDavid Ahern __find_rr_leaf(cont, NULL, metric, res, NULL, 86130c15f03SDavid Ahern oif, strict, do_rr, &mpri); 862f11e6659SDavid S. Miller } 863f11e6659SDavid S. Miller 864b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 865b7bc4b6aSDavid Ahern struct fib6_result *res, int strict) 866f11e6659SDavid S. Miller { 8678d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 868b7bc4b6aSDavid Ahern struct fib6_info *rt0; 869afc154e9SHannes Frederic Sowa bool do_rr = false; 87017ecf590SWei Wang int key_plen; 871f11e6659SDavid S. Miller 872b7bc4b6aSDavid Ahern /* make sure this function or its helpers sets f6i */ 873b7bc4b6aSDavid Ahern res->f6i = NULL; 874b7bc4b6aSDavid Ahern 875421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 876b7bc4b6aSDavid Ahern goto out; 8778d1040e8SWei Wang 87866f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 879f11e6659SDavid S. Miller if (!rt0) 88066f5d6ceSWei Wang rt0 = leaf; 881f11e6659SDavid S. Miller 88217ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 88317ecf590SWei Wang * and fn->leaf does not points to its child's leaf 88417ecf590SWei Wang * (This might happen if all routes under fn are deleted from 88517ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 88617ecf590SWei Wang */ 88793c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 88817ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 88993c2fb25SDavid Ahern if (rt0->fib6_src.plen) 89093c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 89117ecf590SWei Wang #endif 89217ecf590SWei Wang if (fn->fn_bit != key_plen) 893b7bc4b6aSDavid Ahern goto out; 89417ecf590SWei Wang 895b7bc4b6aSDavid Ahern find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 896afc154e9SHannes Frederic Sowa if (do_rr) { 8978fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 898f11e6659SDavid S. Miller 899554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 90093c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 9018d1040e8SWei Wang next = leaf; 902f11e6659SDavid S. Miller 90366f5d6ceSWei Wang if (next != rt0) { 90493c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 90566f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 90693c2fb25SDavid Ahern if (next->fib6_node) 90766f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 90893c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 90966f5d6ceSWei Wang } 910554cfb7eSYOSHIFUJI Hideaki } 911554cfb7eSYOSHIFUJI Hideaki 912b7bc4b6aSDavid Ahern out: 913b7bc4b6aSDavid Ahern if (!res->f6i) { 914b7bc4b6aSDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 9151cf844c7SDavid Ahern res->nh = res->f6i->fib6_nh; 9167d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 9177d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 918b7bc4b6aSDavid Ahern } 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 92185bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 9228b9df265SMartin KaFai Lau { 92385bd05deSDavid Ahern return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 92485bd05deSDavid Ahern res->nh->fib_nh_gw_family; 9258b9df265SMartin KaFai Lau } 9268b9df265SMartin KaFai Lau 92770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 92870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 929b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 93070ceb4f5SYOSHIFUJI Hideaki { 931c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 93270ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 93370ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 93470ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 9354bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 9368d1c802bSDavid Ahern struct fib6_info *rt; 93770ceb4f5SYOSHIFUJI Hideaki 93870ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 93970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94070ceb4f5SYOSHIFUJI Hideaki } 94170ceb4f5SYOSHIFUJI Hideaki 94270ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 94370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 94470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 94670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 94770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 94870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 94970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95070ceb4f5SYOSHIFUJI Hideaki } 95170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 95270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 95370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 95470ceb4f5SYOSHIFUJI Hideaki } 95570ceb4f5SYOSHIFUJI Hideaki } 95670ceb4f5SYOSHIFUJI Hideaki 95770ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 95870ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 9593933fc95SJens Rosenboom return -EINVAL; 96070ceb4f5SYOSHIFUJI Hideaki 9614bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 96270ceb4f5SYOSHIFUJI Hideaki 96370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 96470ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 96570ceb4f5SYOSHIFUJI Hideaki else { 96670ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 96770ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 96870ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 96970ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 97070ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 97170ceb4f5SYOSHIFUJI Hideaki } 97270ceb4f5SYOSHIFUJI Hideaki 973f104a567SDuan Jiong if (rinfo->prefix_len == 0) 974afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 975f104a567SDuan Jiong else 976f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 977830218c1SDavid Ahern gwaddr, dev); 97870ceb4f5SYOSHIFUJI Hideaki 97970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 980afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 98170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 98270ceb4f5SYOSHIFUJI Hideaki } 98370ceb4f5SYOSHIFUJI Hideaki 98470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 985830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 986830218c1SDavid Ahern dev, pref); 98770ceb4f5SYOSHIFUJI Hideaki else if (rt) 98893c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 98993c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 99070ceb4f5SYOSHIFUJI Hideaki 99170ceb4f5SYOSHIFUJI Hideaki if (rt) { 9921716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 99314895687SDavid Ahern fib6_clean_expires(rt); 9941716a961SGao feng else 99514895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 9961716a961SGao feng 99793531c67SDavid Ahern fib6_info_release(rt); 99870ceb4f5SYOSHIFUJI Hideaki } 99970ceb4f5SYOSHIFUJI Hideaki return 0; 100070ceb4f5SYOSHIFUJI Hideaki } 100170ceb4f5SYOSHIFUJI Hideaki #endif 100270ceb4f5SYOSHIFUJI Hideaki 1003ae90d867SDavid Ahern /* 1004ae90d867SDavid Ahern * Misc support functions 1005ae90d867SDavid Ahern */ 1006ae90d867SDavid Ahern 1007ae90d867SDavid Ahern /* called with rcu_lock held */ 10080d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 1009ae90d867SDavid Ahern { 10100d161581SDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 1011ae90d867SDavid Ahern 10127d21fec9SDavid Ahern if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 1013ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 1014ae90d867SDavid Ahern * device if it is a master device, the master device if 1015ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 1016ae90d867SDavid Ahern */ 1017ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 10187d21fec9SDavid Ahern !rt6_need_strict(&res->f6i->fib6_dst.addr)) 1019ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 1020ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 1021ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 1022ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 1023ae90d867SDavid Ahern * case we want dev returned to be dev 1024ae90d867SDavid Ahern */ 1025ae90d867SDavid Ahern } 1026ae90d867SDavid Ahern 1027ae90d867SDavid Ahern return dev; 1028ae90d867SDavid Ahern } 1029ae90d867SDavid Ahern 10306edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 10316edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 10326edb3c96SDavid Ahern [RTN_UNICAST] = 0, 10336edb3c96SDavid Ahern [RTN_LOCAL] = 0, 10346edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 10356edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 10366edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 10376edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 10386edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 10396edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 10406edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 10416edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 10426edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 10436edb3c96SDavid Ahern }; 10446edb3c96SDavid Ahern 10456edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 10466edb3c96SDavid Ahern { 10476edb3c96SDavid Ahern return fib6_prop[fib6_type]; 10486edb3c96SDavid Ahern } 10496edb3c96SDavid Ahern 10508d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 10513b6761d1SDavid Ahern { 10523b6761d1SDavid Ahern unsigned short flags = 0; 10533b6761d1SDavid Ahern 10543b6761d1SDavid Ahern if (rt->dst_nocount) 10553b6761d1SDavid Ahern flags |= DST_NOCOUNT; 10563b6761d1SDavid Ahern if (rt->dst_nopolicy) 10573b6761d1SDavid Ahern flags |= DST_NOPOLICY; 10583b6761d1SDavid Ahern if (rt->dst_host) 10593b6761d1SDavid Ahern flags |= DST_HOST; 10603b6761d1SDavid Ahern 10613b6761d1SDavid Ahern return flags; 10623b6761d1SDavid Ahern } 10633b6761d1SDavid Ahern 10647d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 10656edb3c96SDavid Ahern { 10667d21fec9SDavid Ahern rt->dst.error = ip6_rt_type_to_error(fib6_type); 10676edb3c96SDavid Ahern 10687d21fec9SDavid Ahern switch (fib6_type) { 10696edb3c96SDavid Ahern case RTN_BLACKHOLE: 10706edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 10716edb3c96SDavid Ahern rt->dst.input = dst_discard; 10726edb3c96SDavid Ahern break; 10736edb3c96SDavid Ahern case RTN_PROHIBIT: 10746edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 10756edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 10766edb3c96SDavid Ahern break; 10776edb3c96SDavid Ahern case RTN_THROW: 10786edb3c96SDavid Ahern case RTN_UNREACHABLE: 10796edb3c96SDavid Ahern default: 10806edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 10816edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 10826edb3c96SDavid Ahern break; 10836edb3c96SDavid Ahern } 10846edb3c96SDavid Ahern } 10856edb3c96SDavid Ahern 10860d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 10876edb3c96SDavid Ahern { 10887d21fec9SDavid Ahern struct fib6_info *f6i = res->f6i; 10890d161581SDavid Ahern 10907d21fec9SDavid Ahern if (res->fib6_flags & RTF_REJECT) { 10917d21fec9SDavid Ahern ip6_rt_init_dst_reject(rt, res->fib6_type); 10926edb3c96SDavid Ahern return; 10936edb3c96SDavid Ahern } 10946edb3c96SDavid Ahern 10956edb3c96SDavid Ahern rt->dst.error = 0; 10966edb3c96SDavid Ahern rt->dst.output = ip6_output; 10976edb3c96SDavid Ahern 10987d21fec9SDavid Ahern if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 10996edb3c96SDavid Ahern rt->dst.input = ip6_input; 11007d21fec9SDavid Ahern } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 11016edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 11026edb3c96SDavid Ahern } else { 11036edb3c96SDavid Ahern rt->dst.input = ip6_forward; 11046edb3c96SDavid Ahern } 11056edb3c96SDavid Ahern 11060d161581SDavid Ahern if (res->nh->fib_nh_lws) { 11070d161581SDavid Ahern rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 11086edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 11096edb3c96SDavid Ahern } 11106edb3c96SDavid Ahern 11116edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 11126edb3c96SDavid Ahern } 11136edb3c96SDavid Ahern 1114e873e4b9SWei Wang /* Caller must already hold reference to @from */ 11158d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1116ae90d867SDavid Ahern { 1117ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 1118a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 1119e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1120ae90d867SDavid Ahern } 1121ae90d867SDavid Ahern 11220d161581SDavid Ahern /* Caller must already hold reference to f6i in result */ 11230d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1124ae90d867SDavid Ahern { 11250d161581SDavid Ahern const struct fib6_nh *nh = res->nh; 11260d161581SDavid Ahern const struct net_device *dev = nh->fib_nh_dev; 11270d161581SDavid Ahern struct fib6_info *f6i = res->f6i; 1128dcd1f572SDavid Ahern 11290d161581SDavid Ahern ip6_rt_init_dst(rt, res); 11306edb3c96SDavid Ahern 11310d161581SDavid Ahern rt->rt6i_dst = f6i->fib6_dst; 1132dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 11337d21fec9SDavid Ahern rt->rt6i_flags = res->fib6_flags; 11340d161581SDavid Ahern if (nh->fib_nh_gw_family) { 11350d161581SDavid Ahern rt->rt6i_gateway = nh->fib_nh_gw6; 11362b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 11372b2450caSDavid Ahern } 11380d161581SDavid Ahern rt6_set_from(rt, f6i); 1139ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 11400d161581SDavid Ahern rt->rt6i_src = f6i->fib6_src; 1141ae90d867SDavid Ahern #endif 1142ae90d867SDavid Ahern } 1143ae90d867SDavid Ahern 1144a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1145a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1146a3c00e46SMartin KaFai Lau { 114766f5d6ceSWei Wang struct fib6_node *pn, *sn; 1148a3c00e46SMartin KaFai Lau while (1) { 1149a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1150a3c00e46SMartin KaFai Lau return NULL; 115166f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 115266f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 115366f5d6ceSWei Wang if (sn && sn != fn) 11546454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1155a3c00e46SMartin KaFai Lau else 1156a3c00e46SMartin KaFai Lau fn = pn; 1157a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1158a3c00e46SMartin KaFai Lau return fn; 1159a3c00e46SMartin KaFai Lau } 1160a3c00e46SMartin KaFai Lau } 1161c71099acSThomas Graf 116210585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1163d3843fe5SWei Wang { 1164d3843fe5SWei Wang struct rt6_info *rt = *prt; 1165d3843fe5SWei Wang 1166d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1167d3843fe5SWei Wang return true; 116810585b43SDavid Ahern if (net) { 1169d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1170d3843fe5SWei Wang dst_hold(&rt->dst); 1171d3843fe5SWei Wang } else { 1172d3843fe5SWei Wang rt = NULL; 1173d3843fe5SWei Wang } 1174d3843fe5SWei Wang *prt = rt; 1175d3843fe5SWei Wang return false; 1176d3843fe5SWei Wang } 1177d3843fe5SWei Wang 1178dec9b0e2SDavid Ahern /* called with rcu_lock held */ 11799b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1180dec9b0e2SDavid Ahern { 11819b6b35abSDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 11829b6b35abSDavid Ahern struct fib6_info *f6i = res->f6i; 11839b6b35abSDavid Ahern unsigned short flags; 1184dec9b0e2SDavid Ahern struct rt6_info *nrt; 1185dec9b0e2SDavid Ahern 11869b6b35abSDavid Ahern if (!fib6_info_hold_safe(f6i)) 11871c87e79aSXin Long goto fallback; 1188e873e4b9SWei Wang 11899b6b35abSDavid Ahern flags = fib6_info_dst_flags(f6i); 119093531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 11911c87e79aSXin Long if (!nrt) { 11929b6b35abSDavid Ahern fib6_info_release(f6i); 11931c87e79aSXin Long goto fallback; 11941c87e79aSXin Long } 1195dec9b0e2SDavid Ahern 11960d161581SDavid Ahern ip6_rt_copy_init(nrt, res); 11971c87e79aSXin Long return nrt; 11981c87e79aSXin Long 11991c87e79aSXin Long fallback: 12001c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 12011c87e79aSXin Long dst_hold(&nrt->dst); 1202dec9b0e2SDavid Ahern return nrt; 1203dec9b0e2SDavid Ahern } 1204dec9b0e2SDavid Ahern 12058ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 12068ed67789SDaniel Lezcano struct fib6_table *table, 1207b75cc8f9SDavid Ahern struct flowi6 *fl6, 1208b75cc8f9SDavid Ahern const struct sk_buff *skb, 1209b75cc8f9SDavid Ahern int flags) 12101da177e4SLinus Torvalds { 1211b1d40991SDavid Ahern struct fib6_result res = {}; 12121da177e4SLinus Torvalds struct fib6_node *fn; 121323fb93a4SDavid Ahern struct rt6_info *rt; 12141da177e4SLinus Torvalds 1215b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1216b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 1217b6cdbc85SDavid Ahern 121866f5d6ceSWei Wang rcu_read_lock(); 12196454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1220c71099acSThomas Graf restart: 1221b1d40991SDavid Ahern res.f6i = rcu_dereference(fn->leaf); 1222b1d40991SDavid Ahern if (!res.f6i) 1223b1d40991SDavid Ahern res.f6i = net->ipv6.fib6_null_entry; 1224af52a52cSDavid Ahern else 122575ef7389SDavid Ahern rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 122675ef7389SDavid Ahern flags); 1227af52a52cSDavid Ahern 1228b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1229a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1230a3c00e46SMartin KaFai Lau if (fn) 1231a3c00e46SMartin KaFai Lau goto restart; 1232af52a52cSDavid Ahern 1233af52a52cSDavid Ahern rt = net->ipv6.ip6_null_entry; 1234af52a52cSDavid Ahern dst_hold(&rt->dst); 1235af52a52cSDavid Ahern goto out; 1236f88d8ea6SDavid Ahern } else if (res.fib6_flags & RTF_REJECT) { 1237f88d8ea6SDavid Ahern goto do_create; 1238a3c00e46SMartin KaFai Lau } 12392b760fcfSWei Wang 1240b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1241b1d40991SDavid Ahern fl6->flowi6_oif != 0, skb, flags); 1242b1d40991SDavid Ahern 12434c9483b2SDavid S. Miller /* Search through exception table */ 12447e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 124523fb93a4SDavid Ahern if (rt) { 124610585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 1247d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 124823fb93a4SDavid Ahern } else { 1249f88d8ea6SDavid Ahern do_create: 12509b6b35abSDavid Ahern rt = ip6_create_rt_rcu(&res); 1251dec9b0e2SDavid Ahern } 1252d3843fe5SWei Wang 1253af52a52cSDavid Ahern out: 12548ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 1255af52a52cSDavid Ahern 125666f5d6ceSWei Wang rcu_read_unlock(); 1257b811580dSDavid Ahern 12581da177e4SLinus Torvalds return rt; 1259c71099acSThomas Graf } 1260c71099acSThomas Graf 1261ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1262b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1263ea6e574eSFlorian Westphal { 1264b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1265ea6e574eSFlorian Westphal } 1266ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1267ea6e574eSFlorian Westphal 12689acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1269b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1270b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1271c71099acSThomas Graf { 12724c9483b2SDavid S. Miller struct flowi6 fl6 = { 12734c9483b2SDavid S. Miller .flowi6_oif = oif, 12744c9483b2SDavid S. Miller .daddr = *daddr, 1275c71099acSThomas Graf }; 1276c71099acSThomas Graf struct dst_entry *dst; 127777d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1278c71099acSThomas Graf 1279adaa70bbSThomas Graf if (saddr) { 12804c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1281adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1282adaa70bbSThomas Graf } 1283adaa70bbSThomas Graf 1284b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1285c71099acSThomas Graf if (dst->error == 0) 1286c71099acSThomas Graf return (struct rt6_info *) dst; 1287c71099acSThomas Graf 1288c71099acSThomas Graf dst_release(dst); 1289c71099acSThomas Graf 12901da177e4SLinus Torvalds return NULL; 12911da177e4SLinus Torvalds } 12927159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 12937159039aSYOSHIFUJI Hideaki 1294c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 12951cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 12961cfb71eeSWei Wang * route is released. 12971cfb71eeSWei Wang * Caller must hold dst before calling it. 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds 13008d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1301333c4301SDavid Ahern struct netlink_ext_ack *extack) 13021da177e4SLinus Torvalds { 13031da177e4SLinus Torvalds int err; 1304c71099acSThomas Graf struct fib6_table *table; 13051da177e4SLinus Torvalds 130693c2fb25SDavid Ahern table = rt->fib6_table; 130766f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1308d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 130966f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds return err; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13148d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 131540e22e8fSThomas Graf { 1316afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1317e715b6d3SFlorian Westphal 1318d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 131940e22e8fSThomas Graf } 132040e22e8fSThomas Graf 132185bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 132221efcfa0SEric Dumazet const struct in6_addr *daddr, 1323b71d1d42SEric Dumazet const struct in6_addr *saddr) 13241da177e4SLinus Torvalds { 132585bd05deSDavid Ahern struct fib6_info *f6i = res->f6i; 13264832c30dSDavid Ahern struct net_device *dev; 13271da177e4SLinus Torvalds struct rt6_info *rt; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds /* 13301da177e4SLinus Torvalds * Clone the route. 13311da177e4SLinus Torvalds */ 13321da177e4SLinus Torvalds 133385bd05deSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1334e873e4b9SWei Wang return NULL; 1335e873e4b9SWei Wang 13360d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 133793531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1338e873e4b9SWei Wang if (!rt) { 133985bd05deSDavid Ahern fib6_info_release(f6i); 134083a09abdSMartin KaFai Lau return NULL; 1341e873e4b9SWei Wang } 134283a09abdSMartin KaFai Lau 13430d161581SDavid Ahern ip6_rt_copy_init(rt, res); 13448b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 134583a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 134683a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 134783a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 13488b9df265SMartin KaFai Lau 134985bd05deSDavid Ahern if (!rt6_is_gw_or_nonexthop(res)) { 135085bd05deSDavid Ahern if (f6i->fib6_dst.plen != 128 && 135185bd05deSDavid Ahern ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 135258c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 13531da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 13541da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 13554e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 13561da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds #endif 135995a9a5baSYOSHIFUJI Hideaki } 136095a9a5baSYOSHIFUJI Hideaki 1361299d9939SYOSHIFUJI Hideaki return rt; 1362299d9939SYOSHIFUJI Hideaki } 1363299d9939SYOSHIFUJI Hideaki 1364db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1365d52d3997SMartin KaFai Lau { 1366db3fedeeSDavid Ahern struct fib6_info *f6i = res->f6i; 1367db3fedeeSDavid Ahern unsigned short flags = fib6_info_dst_flags(f6i); 13684832c30dSDavid Ahern struct net_device *dev; 1369d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1370d52d3997SMartin KaFai Lau 1371db3fedeeSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1372e873e4b9SWei Wang return NULL; 1373e873e4b9SWei Wang 13744832c30dSDavid Ahern rcu_read_lock(); 13750d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 137693531c67SDavid Ahern pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); 13774832c30dSDavid Ahern rcu_read_unlock(); 1378e873e4b9SWei Wang if (!pcpu_rt) { 1379db3fedeeSDavid Ahern fib6_info_release(f6i); 1380d52d3997SMartin KaFai Lau return NULL; 1381e873e4b9SWei Wang } 13820d161581SDavid Ahern ip6_rt_copy_init(pcpu_rt, res); 1383d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1384d52d3997SMartin KaFai Lau return pcpu_rt; 1385d52d3997SMartin KaFai Lau } 1386d52d3997SMartin KaFai Lau 138766f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1388db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1389d52d3997SMartin KaFai Lau { 1390c353071aSEric Dumazet struct rt6_info *pcpu_rt; 1391d52d3997SMartin KaFai Lau 1392c353071aSEric Dumazet pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); 1393d52d3997SMartin KaFai Lau 1394d4ead6b3SDavid Ahern if (pcpu_rt) 139510585b43SDavid Ahern ip6_hold_safe(NULL, &pcpu_rt); 1396d3843fe5SWei Wang 1397a73e4195SMartin KaFai Lau return pcpu_rt; 1398a73e4195SMartin KaFai Lau } 1399a73e4195SMartin KaFai Lau 1400afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1401db3fedeeSDavid Ahern const struct fib6_result *res) 1402a73e4195SMartin KaFai Lau { 1403a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1404d52d3997SMartin KaFai Lau 1405db3fedeeSDavid Ahern pcpu_rt = ip6_rt_pcpu_alloc(res); 1406d52d3997SMartin KaFai Lau if (!pcpu_rt) { 14079c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 14089c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1409d52d3997SMartin KaFai Lau } 1410d52d3997SMartin KaFai Lau 1411a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1412f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1413d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1414951f788aSEric Dumazet BUG_ON(prev); 1415a94b9367SWei Wang 141661fb0d01SEric Dumazet if (res->f6i->fib6_destroying) { 141761fb0d01SEric Dumazet struct fib6_info *from; 141861fb0d01SEric Dumazet 141961fb0d01SEric Dumazet from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 142061fb0d01SEric Dumazet fib6_info_release(from); 142161fb0d01SEric Dumazet } 142261fb0d01SEric Dumazet 1423d52d3997SMartin KaFai Lau return pcpu_rt; 1424d52d3997SMartin KaFai Lau } 1425d52d3997SMartin KaFai Lau 142635732d01SWei Wang /* exception hash table implementation 142735732d01SWei Wang */ 142835732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 142935732d01SWei Wang 143035732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 143135732d01SWei Wang * Caller must hold rt6_exception_lock 143235732d01SWei Wang */ 143335732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 143435732d01SWei Wang struct rt6_exception *rt6_ex) 143535732d01SWei Wang { 1436f5b51fe8SPaolo Abeni struct fib6_info *from; 1437b2427e67SColin Ian King struct net *net; 143881eb8447SWei Wang 143935732d01SWei Wang if (!bucket || !rt6_ex) 144035732d01SWei Wang return; 1441b2427e67SColin Ian King 1442b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1443f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1444f5b51fe8SPaolo Abeni 1445f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1446f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1447f5b51fe8SPaolo Abeni */ 14480e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1449f5b51fe8SPaolo Abeni fib6_info_release(from); 1450f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1451f5b51fe8SPaolo Abeni 145235732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 145377634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 145435732d01SWei Wang kfree_rcu(rt6_ex, rcu); 145535732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 145635732d01SWei Wang bucket->depth--; 145735732d01SWei Wang } 145835732d01SWei Wang 145935732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 146035732d01SWei Wang * Caller must hold rt6_exception_lock 146135732d01SWei Wang */ 146235732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 146335732d01SWei Wang { 146435732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 146535732d01SWei Wang 146635732d01SWei Wang if (!bucket) 146735732d01SWei Wang return; 146835732d01SWei Wang 146935732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 147035732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 147135732d01SWei Wang oldest = rt6_ex; 147235732d01SWei Wang } 147335732d01SWei Wang rt6_remove_exception(bucket, oldest); 147435732d01SWei Wang } 147535732d01SWei Wang 147635732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 147735732d01SWei Wang const struct in6_addr *src) 147835732d01SWei Wang { 147935732d01SWei Wang static u32 seed __read_mostly; 148035732d01SWei Wang u32 val; 148135732d01SWei Wang 148235732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 148335732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 148435732d01SWei Wang 148535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 148635732d01SWei Wang if (src) 148735732d01SWei Wang val = jhash(src, sizeof(*src), val); 148835732d01SWei Wang #endif 148935732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 149035732d01SWei Wang } 149135732d01SWei Wang 149235732d01SWei Wang /* Helper function to find the cached rt in the hash table 149335732d01SWei Wang * and update bucket pointer to point to the bucket for this 149435732d01SWei Wang * (daddr, saddr) pair 149535732d01SWei Wang * Caller must hold rt6_exception_lock 149635732d01SWei Wang */ 149735732d01SWei Wang static struct rt6_exception * 149835732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 149935732d01SWei Wang const struct in6_addr *daddr, 150035732d01SWei Wang const struct in6_addr *saddr) 150135732d01SWei Wang { 150235732d01SWei Wang struct rt6_exception *rt6_ex; 150335732d01SWei Wang u32 hval; 150435732d01SWei Wang 150535732d01SWei Wang if (!(*bucket) || !daddr) 150635732d01SWei Wang return NULL; 150735732d01SWei Wang 150835732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 150935732d01SWei Wang *bucket += hval; 151035732d01SWei Wang 151135732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 151235732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 151335732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 151435732d01SWei Wang 151535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 151635732d01SWei Wang if (matched && saddr) 151735732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 151835732d01SWei Wang #endif 151935732d01SWei Wang if (matched) 152035732d01SWei Wang return rt6_ex; 152135732d01SWei Wang } 152235732d01SWei Wang return NULL; 152335732d01SWei Wang } 152435732d01SWei Wang 152535732d01SWei Wang /* Helper function to find the cached rt in the hash table 152635732d01SWei Wang * and update bucket pointer to point to the bucket for this 152735732d01SWei Wang * (daddr, saddr) pair 152835732d01SWei Wang * Caller must hold rcu_read_lock() 152935732d01SWei Wang */ 153035732d01SWei Wang static struct rt6_exception * 153135732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 153235732d01SWei Wang const struct in6_addr *daddr, 153335732d01SWei Wang const struct in6_addr *saddr) 153435732d01SWei Wang { 153535732d01SWei Wang struct rt6_exception *rt6_ex; 153635732d01SWei Wang u32 hval; 153735732d01SWei Wang 153835732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 153935732d01SWei Wang 154035732d01SWei Wang if (!(*bucket) || !daddr) 154135732d01SWei Wang return NULL; 154235732d01SWei Wang 154335732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 154435732d01SWei Wang *bucket += hval; 154535732d01SWei Wang 154635732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 154735732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 154835732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 154935732d01SWei Wang 155035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 155135732d01SWei Wang if (matched && saddr) 155235732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 155335732d01SWei Wang #endif 155435732d01SWei Wang if (matched) 155535732d01SWei Wang return rt6_ex; 155635732d01SWei Wang } 155735732d01SWei Wang return NULL; 155835732d01SWei Wang } 155935732d01SWei Wang 1560b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res) 156135732d01SWei Wang { 1562b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 1563d4ead6b3SDavid Ahern unsigned int mtu; 1564d4ead6b3SDavid Ahern 1565b748f260SDavid Ahern if (res->f6i->fib6_pmtu) { 1566b748f260SDavid Ahern mtu = res->f6i->fib6_pmtu; 1567dcd1f572SDavid Ahern } else { 1568b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 1569dcd1f572SDavid Ahern struct inet6_dev *idev; 1570dcd1f572SDavid Ahern 1571dcd1f572SDavid Ahern rcu_read_lock(); 1572dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1573dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1574dcd1f572SDavid Ahern rcu_read_unlock(); 1575dcd1f572SDavid Ahern } 1576dcd1f572SDavid Ahern 1577d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1578d4ead6b3SDavid Ahern 1579b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1580d4ead6b3SDavid Ahern } 1581d4ead6b3SDavid Ahern 1582cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL 1583cc5c073aSDavid Ahern 1584cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket 1585cc5c073aSDavid Ahern * (ie., all bucket users except rt6_insert_exception); 1586cc5c073aSDavid Ahern * 1587cc5c073aSDavid Ahern * called under rcu lock; sometimes called with rt6_exception_lock held 1588cc5c073aSDavid Ahern */ 1589cc5c073aSDavid Ahern static 1590cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, 1591cc5c073aSDavid Ahern spinlock_t *lock) 1592cc5c073aSDavid Ahern { 1593cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1594cc5c073aSDavid Ahern 1595cc5c073aSDavid Ahern if (lock) 1596cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1597cc5c073aSDavid Ahern lockdep_is_held(lock)); 1598cc5c073aSDavid Ahern else 1599cc5c073aSDavid Ahern bucket = rcu_dereference(nh->rt6i_exception_bucket); 1600cc5c073aSDavid Ahern 1601cc5c073aSDavid Ahern /* remove bucket flushed bit if set */ 1602cc5c073aSDavid Ahern if (bucket) { 1603cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1604cc5c073aSDavid Ahern 1605cc5c073aSDavid Ahern p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; 1606cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1607cc5c073aSDavid Ahern } 1608cc5c073aSDavid Ahern 1609cc5c073aSDavid Ahern return bucket; 1610cc5c073aSDavid Ahern } 1611cc5c073aSDavid Ahern 1612cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) 1613cc5c073aSDavid Ahern { 1614cc5c073aSDavid Ahern unsigned long p = (unsigned long)bucket; 1615cc5c073aSDavid Ahern 1616cc5c073aSDavid Ahern return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); 1617cc5c073aSDavid Ahern } 1618cc5c073aSDavid Ahern 1619cc5c073aSDavid Ahern /* called with rt6_exception_lock held */ 1620cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, 1621cc5c073aSDavid Ahern spinlock_t *lock) 1622cc5c073aSDavid Ahern { 1623cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 1624cc5c073aSDavid Ahern unsigned long p; 1625cc5c073aSDavid Ahern 1626cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 1627cc5c073aSDavid Ahern lockdep_is_held(lock)); 1628cc5c073aSDavid Ahern 1629cc5c073aSDavid Ahern p = (unsigned long)bucket; 1630cc5c073aSDavid Ahern p |= FIB6_EXCEPTION_BUCKET_FLUSHED; 1631cc5c073aSDavid Ahern bucket = (struct rt6_exception_bucket *)p; 1632cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1633cc5c073aSDavid Ahern } 1634cc5c073aSDavid Ahern 163535732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 16365012f0a5SDavid Ahern const struct fib6_result *res) 163735732d01SWei Wang { 16385e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 163935732d01SWei Wang struct rt6_exception_bucket *bucket; 1640cc5c073aSDavid Ahern struct fib6_info *f6i = res->f6i; 164135732d01SWei Wang struct in6_addr *src_key = NULL; 164235732d01SWei Wang struct rt6_exception *rt6_ex; 1643cc5c073aSDavid Ahern struct fib6_nh *nh = res->nh; 164435732d01SWei Wang int err = 0; 164535732d01SWei Wang 164635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 164735732d01SWei Wang 1648cc5c073aSDavid Ahern bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, 164935732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 165035732d01SWei Wang if (!bucket) { 165135732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 165235732d01SWei Wang GFP_ATOMIC); 165335732d01SWei Wang if (!bucket) { 165435732d01SWei Wang err = -ENOMEM; 165535732d01SWei Wang goto out; 165635732d01SWei Wang } 1657cc5c073aSDavid Ahern rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); 1658cc5c073aSDavid Ahern } else if (fib6_nh_excptn_bucket_flushed(bucket)) { 1659cc5c073aSDavid Ahern err = -EINVAL; 1660cc5c073aSDavid Ahern goto out; 166135732d01SWei Wang } 166235732d01SWei Wang 166335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 16645012f0a5SDavid Ahern /* fib6_src.plen != 0 indicates f6i is in subtree 166535732d01SWei Wang * and exception table is indexed by a hash of 16665012f0a5SDavid Ahern * both fib6_dst and fib6_src. 166735732d01SWei Wang * Otherwise, the exception table is indexed by 16685012f0a5SDavid Ahern * a hash of only fib6_dst. 166935732d01SWei Wang */ 16705012f0a5SDavid Ahern if (f6i->fib6_src.plen) 167135732d01SWei Wang src_key = &nrt->rt6i_src.addr; 167235732d01SWei Wang #endif 16735012f0a5SDavid Ahern /* rt6_mtu_change() might lower mtu on f6i. 1674f5bbe7eeSWei Wang * Only insert this exception route if its mtu 16755012f0a5SDavid Ahern * is less than f6i's mtu value. 1676f5bbe7eeSWei Wang */ 1677b748f260SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1678f5bbe7eeSWei Wang err = -EINVAL; 1679f5bbe7eeSWei Wang goto out; 1680f5bbe7eeSWei Wang } 168160006a48SWei Wang 168235732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 168335732d01SWei Wang src_key); 168435732d01SWei Wang if (rt6_ex) 168535732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 168635732d01SWei Wang 168735732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 168835732d01SWei Wang if (!rt6_ex) { 168935732d01SWei Wang err = -ENOMEM; 169035732d01SWei Wang goto out; 169135732d01SWei Wang } 169235732d01SWei Wang rt6_ex->rt6i = nrt; 169335732d01SWei Wang rt6_ex->stamp = jiffies; 169435732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 169535732d01SWei Wang bucket->depth++; 169681eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 169735732d01SWei Wang 169835732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 169935732d01SWei Wang rt6_exception_remove_oldest(bucket); 170035732d01SWei Wang 170135732d01SWei Wang out: 170235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 170335732d01SWei Wang 170435732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1705b886d5f2SPaolo Abeni if (!err) { 17065012f0a5SDavid Ahern spin_lock_bh(&f6i->fib6_table->tb6_lock); 17075012f0a5SDavid Ahern fib6_update_sernum(net, f6i); 17085012f0a5SDavid Ahern spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1709b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1710b886d5f2SPaolo Abeni } 171135732d01SWei Wang 171235732d01SWei Wang return err; 171335732d01SWei Wang } 171435732d01SWei Wang 1715c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) 171635732d01SWei Wang { 171735732d01SWei Wang struct rt6_exception_bucket *bucket; 171835732d01SWei Wang struct rt6_exception *rt6_ex; 171935732d01SWei Wang struct hlist_node *tmp; 172035732d01SWei Wang int i; 172135732d01SWei Wang 172235732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 172335732d01SWei Wang 1724cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 172535732d01SWei Wang if (!bucket) 172635732d01SWei Wang goto out; 172735732d01SWei Wang 1728cc5c073aSDavid Ahern /* Prevent rt6_insert_exception() to recreate the bucket list */ 1729cc5c073aSDavid Ahern if (!from) 1730cc5c073aSDavid Ahern fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); 1731cc5c073aSDavid Ahern 173235732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1733cc5c073aSDavid Ahern hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { 1734cc5c073aSDavid Ahern if (!from || 1735cc5c073aSDavid Ahern rcu_access_pointer(rt6_ex->rt6i->from) == from) 173635732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1737cc5c073aSDavid Ahern } 1738cc5c073aSDavid Ahern WARN_ON_ONCE(!from && bucket->depth); 173935732d01SWei Wang bucket++; 174035732d01SWei Wang } 174135732d01SWei Wang out: 174235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 174335732d01SWei Wang } 174435732d01SWei Wang 1745e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) 1746e659ba31SDavid Ahern { 1747e659ba31SDavid Ahern struct fib6_info *f6i = arg; 1748e659ba31SDavid Ahern 1749e659ba31SDavid Ahern fib6_nh_flush_exceptions(nh, f6i); 1750e659ba31SDavid Ahern 1751e659ba31SDavid Ahern return 0; 1752e659ba31SDavid Ahern } 1753e659ba31SDavid Ahern 1754c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i) 1755c0b220cfSDavid Ahern { 1756e659ba31SDavid Ahern if (f6i->nh) 1757e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, 1758e659ba31SDavid Ahern f6i); 1759e659ba31SDavid Ahern else 17601cf844c7SDavid Ahern fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); 1761c0b220cfSDavid Ahern } 1762c0b220cfSDavid Ahern 176335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 176435732d01SWei Wang * Caller has to hold rcu_read_lock() 176535732d01SWei Wang */ 17667e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1767510e2cedSWei Wang const struct in6_addr *daddr, 1768510e2cedSWei Wang const struct in6_addr *saddr) 176935732d01SWei Wang { 1770510e2cedSWei Wang const struct in6_addr *src_key = NULL; 177135732d01SWei Wang struct rt6_exception_bucket *bucket; 177235732d01SWei Wang struct rt6_exception *rt6_ex; 17737e4b5128SDavid Ahern struct rt6_info *ret = NULL; 177435732d01SWei Wang 177535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 17767e4b5128SDavid Ahern /* fib6i_src.plen != 0 indicates f6i is in subtree 177735732d01SWei Wang * and exception table is indexed by a hash of 17787e4b5128SDavid Ahern * both fib6_dst and fib6_src. 1779510e2cedSWei Wang * However, the src addr used to create the hash 1780510e2cedSWei Wang * might not be exactly the passed in saddr which 1781510e2cedSWei Wang * is a /128 addr from the flow. 1782510e2cedSWei Wang * So we need to use f6i->fib6_src to redo lookup 1783510e2cedSWei Wang * if the passed in saddr does not find anything. 1784510e2cedSWei Wang * (See the logic in ip6_rt_cache_alloc() on how 1785510e2cedSWei Wang * rt->rt6i_src is updated.) 178635732d01SWei Wang */ 17877e4b5128SDavid Ahern if (res->f6i->fib6_src.plen) 178835732d01SWei Wang src_key = saddr; 1789510e2cedSWei Wang find_ex: 179035732d01SWei Wang #endif 1791cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); 179235732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 179335732d01SWei Wang 179435732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 17957e4b5128SDavid Ahern ret = rt6_ex->rt6i; 179635732d01SWei Wang 1797510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES 1798510e2cedSWei Wang /* Use fib6_src as src_key and redo lookup */ 1799510e2cedSWei Wang if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1800510e2cedSWei Wang src_key = &res->f6i->fib6_src.addr; 1801510e2cedSWei Wang goto find_ex; 1802510e2cedSWei Wang } 1803510e2cedSWei Wang #endif 1804510e2cedSWei Wang 18057e4b5128SDavid Ahern return ret; 180635732d01SWei Wang } 180735732d01SWei Wang 180835732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 1809cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, 1810c0b220cfSDavid Ahern const struct rt6_info *rt) 181135732d01SWei Wang { 1812c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 181335732d01SWei Wang struct rt6_exception_bucket *bucket; 181435732d01SWei Wang struct rt6_exception *rt6_ex; 181535732d01SWei Wang int err; 181635732d01SWei Wang 1817cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 181835732d01SWei Wang return -ENOENT; 181935732d01SWei Wang 182035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1821cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1822cc5c073aSDavid Ahern 182335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1824cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1825cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1826cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 182735732d01SWei Wang * Otherwise, the exception table is indexed by 182835732d01SWei Wang * a hash of only rt6i_dst. 182935732d01SWei Wang */ 1830c0b220cfSDavid Ahern if (plen) 183135732d01SWei Wang src_key = &rt->rt6i_src.addr; 183235732d01SWei Wang #endif 183335732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 183435732d01SWei Wang &rt->rt6i_dst.addr, 183535732d01SWei Wang src_key); 183635732d01SWei Wang if (rt6_ex) { 183735732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 183835732d01SWei Wang err = 0; 183935732d01SWei Wang } else { 184035732d01SWei Wang err = -ENOENT; 184135732d01SWei Wang } 184235732d01SWei Wang 184335732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 184435732d01SWei Wang return err; 184535732d01SWei Wang } 184635732d01SWei Wang 1847e659ba31SDavid Ahern struct fib6_nh_excptn_arg { 1848e659ba31SDavid Ahern struct rt6_info *rt; 1849e659ba31SDavid Ahern int plen; 1850e659ba31SDavid Ahern }; 1851e659ba31SDavid Ahern 1852e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) 1853e659ba31SDavid Ahern { 1854e659ba31SDavid Ahern struct fib6_nh_excptn_arg *arg = _arg; 1855e659ba31SDavid Ahern int err; 1856e659ba31SDavid Ahern 1857e659ba31SDavid Ahern err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); 1858e659ba31SDavid Ahern if (err == 0) 1859e659ba31SDavid Ahern return 1; 1860e659ba31SDavid Ahern 1861e659ba31SDavid Ahern return 0; 1862e659ba31SDavid Ahern } 1863e659ba31SDavid Ahern 1864c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 1865c0b220cfSDavid Ahern { 1866c0b220cfSDavid Ahern struct fib6_info *from; 1867c0b220cfSDavid Ahern 1868c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1869cc5c073aSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1870c0b220cfSDavid Ahern return -EINVAL; 1871c0b220cfSDavid Ahern 1872e659ba31SDavid Ahern if (from->nh) { 1873e659ba31SDavid Ahern struct fib6_nh_excptn_arg arg = { 1874e659ba31SDavid Ahern .rt = rt, 1875e659ba31SDavid Ahern .plen = from->fib6_src.plen 1876e659ba31SDavid Ahern }; 1877e659ba31SDavid Ahern int rc; 1878e659ba31SDavid Ahern 1879e659ba31SDavid Ahern /* rc = 1 means an entry was found */ 1880e659ba31SDavid Ahern rc = nexthop_for_each_fib6_nh(from->nh, 1881e659ba31SDavid Ahern rt6_nh_remove_exception_rt, 1882e659ba31SDavid Ahern &arg); 1883e659ba31SDavid Ahern return rc ? 0 : -ENOENT; 1884e659ba31SDavid Ahern } 1885e659ba31SDavid Ahern 18861cf844c7SDavid Ahern return fib6_nh_remove_exception(from->fib6_nh, 1887cc5c073aSDavid Ahern from->fib6_src.plen, rt); 1888c0b220cfSDavid Ahern } 1889c0b220cfSDavid Ahern 189035732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 189135732d01SWei Wang * refresh its stamp 189235732d01SWei Wang */ 1893cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, 1894c0b220cfSDavid Ahern const struct rt6_info *rt) 189535732d01SWei Wang { 1896c0b220cfSDavid Ahern const struct in6_addr *src_key = NULL; 189735732d01SWei Wang struct rt6_exception_bucket *bucket; 189835732d01SWei Wang struct rt6_exception *rt6_ex; 1899193f3685SPaolo Abeni 1900cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, NULL); 190135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1902cc5c073aSDavid Ahern /* rt6i_src.plen != 0 indicates 'from' is in subtree 1903cc5c073aSDavid Ahern * and exception table is indexed by a hash of 1904cc5c073aSDavid Ahern * both rt6i_dst and rt6i_src. 190535732d01SWei Wang * Otherwise, the exception table is indexed by 190635732d01SWei Wang * a hash of only rt6i_dst. 190735732d01SWei Wang */ 1908c0b220cfSDavid Ahern if (plen) 190935732d01SWei Wang src_key = &rt->rt6i_src.addr; 191035732d01SWei Wang #endif 1911cc5c073aSDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); 191235732d01SWei Wang if (rt6_ex) 191335732d01SWei Wang rt6_ex->stamp = jiffies; 1914c0b220cfSDavid Ahern } 191535732d01SWei Wang 1916e659ba31SDavid Ahern struct fib6_nh_match_arg { 1917e659ba31SDavid Ahern const struct net_device *dev; 1918e659ba31SDavid Ahern const struct in6_addr *gw; 1919e659ba31SDavid Ahern struct fib6_nh *match; 1920e659ba31SDavid Ahern }; 1921e659ba31SDavid Ahern 1922e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */ 1923e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) 1924e659ba31SDavid Ahern { 1925e659ba31SDavid Ahern struct fib6_nh_match_arg *arg = _arg; 1926e659ba31SDavid Ahern 1927e659ba31SDavid Ahern if (arg->dev != nh->fib_nh_dev || 1928e659ba31SDavid Ahern (arg->gw && !nh->fib_nh_gw_family) || 1929e659ba31SDavid Ahern (!arg->gw && nh->fib_nh_gw_family) || 1930e659ba31SDavid Ahern (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) 1931e659ba31SDavid Ahern return 0; 1932e659ba31SDavid Ahern 1933e659ba31SDavid Ahern arg->match = nh; 1934e659ba31SDavid Ahern 1935e659ba31SDavid Ahern /* found a match, break the loop */ 1936e659ba31SDavid Ahern return 1; 1937e659ba31SDavid Ahern } 1938e659ba31SDavid Ahern 1939c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 1940c0b220cfSDavid Ahern { 1941c0b220cfSDavid Ahern struct fib6_info *from; 1942e659ba31SDavid Ahern struct fib6_nh *fib6_nh; 1943c0b220cfSDavid Ahern 1944c0b220cfSDavid Ahern rcu_read_lock(); 1945c0b220cfSDavid Ahern 1946c0b220cfSDavid Ahern from = rcu_dereference(rt->from); 1947c0b220cfSDavid Ahern if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1948c0b220cfSDavid Ahern goto unlock; 1949c0b220cfSDavid Ahern 1950e659ba31SDavid Ahern if (from->nh) { 1951e659ba31SDavid Ahern struct fib6_nh_match_arg arg = { 1952e659ba31SDavid Ahern .dev = rt->dst.dev, 1953e659ba31SDavid Ahern .gw = &rt->rt6i_gateway, 1954e659ba31SDavid Ahern }; 1955e659ba31SDavid Ahern 1956e659ba31SDavid Ahern nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); 1957e659ba31SDavid Ahern 1958e659ba31SDavid Ahern if (!arg.match) 1959e659ba31SDavid Ahern return; 1960e659ba31SDavid Ahern fib6_nh = arg.match; 1961e659ba31SDavid Ahern } else { 1962e659ba31SDavid Ahern fib6_nh = from->fib6_nh; 1963e659ba31SDavid Ahern } 1964e659ba31SDavid Ahern fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); 1965193f3685SPaolo Abeni unlock: 196635732d01SWei Wang rcu_read_unlock(); 196735732d01SWei Wang } 196835732d01SWei Wang 1969e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1970e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1971e9fa1495SStefano Brivio { 1972e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1973e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1974e9fa1495SStefano Brivio * reflect PMTU decreases. 1975e9fa1495SStefano Brivio * 1976e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1977e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1978e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1979e9fa1495SStefano Brivio * handle this. 1980e9fa1495SStefano Brivio */ 1981e9fa1495SStefano Brivio 1982e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1983e9fa1495SStefano Brivio return true; 1984e9fa1495SStefano Brivio 1985e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1986e9fa1495SStefano Brivio return true; 1987e9fa1495SStefano Brivio 1988e9fa1495SStefano Brivio return false; 1989e9fa1495SStefano Brivio } 1990e9fa1495SStefano Brivio 1991e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 1992cc5c073aSDavid Ahern const struct fib6_nh *nh, int mtu) 1993f5bbe7eeSWei Wang { 1994f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1995f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1996f5bbe7eeSWei Wang int i; 1997f5bbe7eeSWei Wang 1998cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 1999e9fa1495SStefano Brivio if (!bucket) 2000e9fa1495SStefano Brivio return; 2001e9fa1495SStefano Brivio 2002f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2003f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 2004f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2005e9fa1495SStefano Brivio 2006e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 2007d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 2008f5bbe7eeSWei Wang * been updated. 2009f5bbe7eeSWei Wang */ 2010d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 2011e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 2012d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 2013f5bbe7eeSWei Wang } 2014f5bbe7eeSWei Wang bucket++; 2015f5bbe7eeSWei Wang } 2016f5bbe7eeSWei Wang } 2017f5bbe7eeSWei Wang 2018b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2019b16cb459SWei Wang 2020cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, 2021cc5c073aSDavid Ahern const struct in6_addr *gateway) 2022b16cb459SWei Wang { 2023b16cb459SWei Wang struct rt6_exception_bucket *bucket; 2024b16cb459SWei Wang struct rt6_exception *rt6_ex; 2025b16cb459SWei Wang struct hlist_node *tmp; 2026b16cb459SWei Wang int i; 2027b16cb459SWei Wang 2028cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2029b16cb459SWei Wang return; 2030b16cb459SWei Wang 2031b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 2032cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2033b16cb459SWei Wang if (bucket) { 2034b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2035b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2036b16cb459SWei Wang &bucket->chain, hlist) { 2037b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 2038b16cb459SWei Wang 2039b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 2040b16cb459SWei Wang RTF_CACHE_GATEWAY && 2041b16cb459SWei Wang ipv6_addr_equal(gateway, 2042b16cb459SWei Wang &entry->rt6i_gateway)) { 2043b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 2044b16cb459SWei Wang } 2045b16cb459SWei Wang } 2046b16cb459SWei Wang bucket++; 2047b16cb459SWei Wang } 2048b16cb459SWei Wang } 2049b16cb459SWei Wang 2050b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 2051b16cb459SWei Wang } 2052b16cb459SWei Wang 2053c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 2054c757faa8SWei Wang struct rt6_exception *rt6_ex, 2055c757faa8SWei Wang struct fib6_gc_args *gc_args, 2056c757faa8SWei Wang unsigned long now) 2057c757faa8SWei Wang { 2058c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 2059c757faa8SWei Wang 20601859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 20611859bac0SPaolo Abeni * even if others have still references to them, so that on next 20621859bac0SPaolo Abeni * dst_check() such references can be dropped. 20631859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 20641859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 20651859bac0SPaolo Abeni */ 206631afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 206731afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 2068c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 2069c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2070c757faa8SWei Wang return; 207131afeb42SWei Wang } 207231afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 207331afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 207431afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 207531afeb42SWei Wang return; 207631afeb42SWei Wang } 207731afeb42SWei Wang 207831afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 2079c757faa8SWei Wang struct neighbour *neigh; 2080c757faa8SWei Wang __u8 neigh_flags = 0; 2081c757faa8SWei Wang 20821bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 20831bfa26ffSEric Dumazet if (neigh) 2084c757faa8SWei Wang neigh_flags = neigh->flags; 20851bfa26ffSEric Dumazet 2086c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 2087c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 2088c757faa8SWei Wang rt); 2089c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 2090c757faa8SWei Wang return; 2091c757faa8SWei Wang } 2092c757faa8SWei Wang } 209331afeb42SWei Wang 2094c757faa8SWei Wang gc_args->more++; 2095c757faa8SWei Wang } 2096c757faa8SWei Wang 2097cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh, 2098c757faa8SWei Wang struct fib6_gc_args *gc_args, 2099c757faa8SWei Wang unsigned long now) 2100c757faa8SWei Wang { 2101c757faa8SWei Wang struct rt6_exception_bucket *bucket; 2102c757faa8SWei Wang struct rt6_exception *rt6_ex; 2103c757faa8SWei Wang struct hlist_node *tmp; 2104c757faa8SWei Wang int i; 2105c757faa8SWei Wang 2106cc5c073aSDavid Ahern if (!rcu_access_pointer(nh->rt6i_exception_bucket)) 2107c757faa8SWei Wang return; 2108c757faa8SWei Wang 21091bfa26ffSEric Dumazet rcu_read_lock_bh(); 21101bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 2111cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); 2112c757faa8SWei Wang if (bucket) { 2113c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 2114c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 2115c757faa8SWei Wang &bucket->chain, hlist) { 2116c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 2117c757faa8SWei Wang gc_args, now); 2118c757faa8SWei Wang } 2119c757faa8SWei Wang bucket++; 2120c757faa8SWei Wang } 2121c757faa8SWei Wang } 21221bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 21231bfa26ffSEric Dumazet rcu_read_unlock_bh(); 2124c757faa8SWei Wang } 2125c757faa8SWei Wang 2126e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg { 2127e659ba31SDavid Ahern struct fib6_gc_args *gc_args; 2128e659ba31SDavid Ahern unsigned long now; 2129e659ba31SDavid Ahern }; 2130e659ba31SDavid Ahern 2131e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) 2132e659ba31SDavid Ahern { 2133e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg *arg = _arg; 2134e659ba31SDavid Ahern 2135e659ba31SDavid Ahern fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); 2136e659ba31SDavid Ahern return 0; 2137e659ba31SDavid Ahern } 2138e659ba31SDavid Ahern 2139cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i, 2140c0b220cfSDavid Ahern struct fib6_gc_args *gc_args, 2141c0b220cfSDavid Ahern unsigned long now) 2142c0b220cfSDavid Ahern { 2143e659ba31SDavid Ahern if (f6i->nh) { 2144e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg arg = { 2145e659ba31SDavid Ahern .gc_args = gc_args, 2146e659ba31SDavid Ahern .now = now 2147e659ba31SDavid Ahern }; 2148e659ba31SDavid Ahern 2149e659ba31SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, 2150e659ba31SDavid Ahern &arg); 2151e659ba31SDavid Ahern } else { 21521cf844c7SDavid Ahern fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); 2153c0b220cfSDavid Ahern } 2154e659ba31SDavid Ahern } 2155c0b220cfSDavid Ahern 21561d053da9SDavid Ahern /* must be called with rcu lock held */ 2157effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 2158effda4ddSDavid Ahern struct flowi6 *fl6, struct fib6_result *res, int strict) 21591da177e4SLinus Torvalds { 2160367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 21611da177e4SLinus Torvalds 21626454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2163367efcb9SMartin KaFai Lau saved_fn = fn; 21641da177e4SLinus Torvalds 2165ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 2166ca254490SDavid Ahern oif = 0; 2167ca254490SDavid Ahern 2168a3c00e46SMartin KaFai Lau redo_rt6_select: 2169effda4ddSDavid Ahern rt6_select(net, fn, oif, res, strict); 2170effda4ddSDavid Ahern if (res->f6i == net->ipv6.fib6_null_entry) { 2171a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2172a3c00e46SMartin KaFai Lau if (fn) 2173a3c00e46SMartin KaFai Lau goto redo_rt6_select; 2174367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 2175367efcb9SMartin KaFai Lau /* also consider unreachable route */ 2176367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 2177367efcb9SMartin KaFai Lau fn = saved_fn; 2178367efcb9SMartin KaFai Lau goto redo_rt6_select; 2179367efcb9SMartin KaFai Lau } 2180a3c00e46SMartin KaFai Lau } 2181a3c00e46SMartin KaFai Lau 2182effda4ddSDavid Ahern trace_fib6_table_lookup(net, res, table, fl6); 2183d52d3997SMartin KaFai Lau 2184effda4ddSDavid Ahern return 0; 21851d053da9SDavid Ahern } 21861d053da9SDavid Ahern 21871d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 21881d053da9SDavid Ahern int oif, struct flowi6 *fl6, 21891d053da9SDavid Ahern const struct sk_buff *skb, int flags) 21901d053da9SDavid Ahern { 2191b1d40991SDavid Ahern struct fib6_result res = {}; 21921d053da9SDavid Ahern struct rt6_info *rt; 21931d053da9SDavid Ahern int strict = 0; 21941d053da9SDavid Ahern 21951d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 21961d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 21971d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 21981d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 21991d053da9SDavid Ahern 22001d053da9SDavid Ahern rcu_read_lock(); 22011d053da9SDavid Ahern 2202effda4ddSDavid Ahern fib6_table_lookup(net, table, oif, fl6, &res, strict); 2203b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 2204421842edSDavid Ahern rt = net->ipv6.ip6_null_entry; 220566f5d6ceSWei Wang rcu_read_unlock(); 2206d3843fe5SWei Wang dst_hold(&rt->dst); 2207d3843fe5SWei Wang return rt; 2208d3843fe5SWei Wang } 220923fb93a4SDavid Ahern 2210b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, oif, false, skb, strict); 2211d83009d4SDavid Ahern 221223fb93a4SDavid Ahern /*Search through exception table */ 22137e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 221423fb93a4SDavid Ahern if (rt) { 221510585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 22161da177e4SLinus Torvalds dst_use_noref(&rt->dst, jiffies); 2217d4ead6b3SDavid Ahern 221866f5d6ceSWei Wang rcu_read_unlock(); 2219d52d3997SMartin KaFai Lau return rt; 22203da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 2221b1d40991SDavid Ahern !res.nh->fib_nh_gw_family)) { 22223da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 22233da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 22243da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 22253da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 22263da59bd9SMartin KaFai Lau */ 22273da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 22283da59bd9SMartin KaFai Lau 222985bd05deSDavid Ahern uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 2230d52d3997SMartin KaFai Lau 22314d85cd0cSDavid Ahern rcu_read_unlock(); 22323da59bd9SMartin KaFai Lau 22331cfb71eeSWei Wang if (uncached_rt) { 22341cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 22351cfb71eeSWei Wang * No need for another dst_hold() 22361cfb71eeSWei Wang */ 22378d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 223881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 22391cfb71eeSWei Wang } else { 22403da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 22413da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 22421cfb71eeSWei Wang } 2243b811580dSDavid Ahern 22443da59bd9SMartin KaFai Lau return uncached_rt; 2245d52d3997SMartin KaFai Lau } else { 2246d52d3997SMartin KaFai Lau /* Get a percpu copy */ 2247d52d3997SMartin KaFai Lau 2248d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 2249d52d3997SMartin KaFai Lau 2250951f788aSEric Dumazet local_bh_disable(); 2251db3fedeeSDavid Ahern pcpu_rt = rt6_get_pcpu_route(&res); 2252d52d3997SMartin KaFai Lau 225393531c67SDavid Ahern if (!pcpu_rt) 2254db3fedeeSDavid Ahern pcpu_rt = rt6_make_pcpu_route(net, &res); 225593531c67SDavid Ahern 2256951f788aSEric Dumazet local_bh_enable(); 2257951f788aSEric Dumazet rcu_read_unlock(); 2258d4bea421SDavid Ahern 2259d52d3997SMartin KaFai Lau return pcpu_rt; 2260d52d3997SMartin KaFai Lau } 2261c71099acSThomas Graf } 22629ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 2263c71099acSThomas Graf 2264b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 2265b75cc8f9SDavid Ahern struct fib6_table *table, 2266b75cc8f9SDavid Ahern struct flowi6 *fl6, 2267b75cc8f9SDavid Ahern const struct sk_buff *skb, 2268b75cc8f9SDavid Ahern int flags) 22694acad72dSPavel Emelyanov { 2270b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 22714acad72dSPavel Emelyanov } 22724acad72dSPavel Emelyanov 2273d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 227472331bc0SShmulik Ladkani struct net_device *dev, 2275b75cc8f9SDavid Ahern struct flowi6 *fl6, 2276b75cc8f9SDavid Ahern const struct sk_buff *skb, 2277b75cc8f9SDavid Ahern int flags) 227872331bc0SShmulik Ladkani { 227972331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 228072331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 228172331bc0SShmulik Ladkani 2282b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 228372331bc0SShmulik Ladkani } 2284d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 228572331bc0SShmulik Ladkani 228623aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 22875e5d6fedSRoopa Prabhu struct flow_keys *keys, 22885e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 228923aebdacSJakub Sitnicki { 229023aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 229123aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 22925e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 229323aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 229423aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 229523aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 2296cea67a2dSEric Dumazet struct icmp6hdr _icmph; 229723aebdacSJakub Sitnicki 229823aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 229923aebdacSJakub Sitnicki goto out; 230023aebdacSJakub Sitnicki 2301cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2302cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 2303cea67a2dSEric Dumazet if (!icmph) 2304cea67a2dSEric Dumazet goto out; 2305cea67a2dSEric Dumazet 230623aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 230723aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 230823aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 230923aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 231023aebdacSJakub Sitnicki goto out; 231123aebdacSJakub Sitnicki 231223aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 231323aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 231423aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 231523aebdacSJakub Sitnicki if (!inner_iph) 231623aebdacSJakub Sitnicki goto out; 231723aebdacSJakub Sitnicki 231823aebdacSJakub Sitnicki key_iph = inner_iph; 23195e5d6fedSRoopa Prabhu _flkeys = NULL; 232023aebdacSJakub Sitnicki out: 23215e5d6fedSRoopa Prabhu if (_flkeys) { 23225e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 23235e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 23245e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 23255e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 23265e5d6fedSRoopa Prabhu } else { 232723aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 232823aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 2329fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 233023aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 233123aebdacSJakub Sitnicki } 23325e5d6fedSRoopa Prabhu } 233323aebdacSJakub Sitnicki 233423aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 2335b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2336b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 233723aebdacSJakub Sitnicki { 233823aebdacSJakub Sitnicki struct flow_keys hash_keys; 23399a2a537aSDavid Ahern u32 mhash; 234023aebdacSJakub Sitnicki 2341bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 2342b4bac172SDavid Ahern case 0: 23436f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 23446f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 23459a2a537aSDavid Ahern if (skb) { 23465e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 23479a2a537aSDavid Ahern } else { 23489a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 23499a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2350fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 23519a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 235223aebdacSJakub Sitnicki } 2353b4bac172SDavid Ahern break; 2354b4bac172SDavid Ahern case 1: 2355b4bac172SDavid Ahern if (skb) { 2356b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2357b4bac172SDavid Ahern struct flow_keys keys; 2358b4bac172SDavid Ahern 2359b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2360b4bac172SDavid Ahern if (skb->l4_hash) 2361b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2362b4bac172SDavid Ahern 2363b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2364b4bac172SDavid Ahern 2365b4bac172SDavid Ahern if (!flkeys) { 2366b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2367b4bac172SDavid Ahern flkeys = &keys; 2368b4bac172SDavid Ahern } 2369b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2370b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2371b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2372b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2373b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2374b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2375b4bac172SDavid Ahern } else { 2376b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2377b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2378b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2379b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2380b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2381b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2382b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2383b4bac172SDavid Ahern } 2384b4bac172SDavid Ahern break; 2385b4bac172SDavid Ahern } 23869a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 238723aebdacSJakub Sitnicki 23889a2a537aSDavid Ahern return mhash >> 1; 238923aebdacSJakub Sitnicki } 239023aebdacSJakub Sitnicki 2391c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2392c71099acSThomas Graf { 2393b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2394c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 2395adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 2396904af04dSJiri Benc struct ip_tunnel_info *tun_info; 23974c9483b2SDavid S. Miller struct flowi6 fl6 = { 2398e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 23994c9483b2SDavid S. Miller .daddr = iph->daddr, 24004c9483b2SDavid S. Miller .saddr = iph->saddr, 24016502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 24024c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 24034c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2404c71099acSThomas Graf }; 24055e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2406adaa70bbSThomas Graf 2407904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 240846fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2409904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 24105e5d6fedSRoopa Prabhu 24115e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 24125e5d6fedSRoopa Prabhu flkeys = &_flkeys; 24135e5d6fedSRoopa Prabhu 241423aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2415b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 241606e9d040SJiri Benc skb_dst_drop(skb); 2417b75cc8f9SDavid Ahern skb_dst_set(skb, 2418b75cc8f9SDavid Ahern ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); 2419c71099acSThomas Graf } 2420c71099acSThomas Graf 2421b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2422b75cc8f9SDavid Ahern struct fib6_table *table, 2423b75cc8f9SDavid Ahern struct flowi6 *fl6, 2424b75cc8f9SDavid Ahern const struct sk_buff *skb, 2425b75cc8f9SDavid Ahern int flags) 2426c71099acSThomas Graf { 2427b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2428c71099acSThomas Graf } 2429c71099acSThomas Graf 24306f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 24316f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2432c71099acSThomas Graf { 2433d46a9d67SDavid Ahern bool any_src; 2434c71099acSThomas Graf 24353ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 24363ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 24374c1feac5SDavid Ahern struct dst_entry *dst; 24384c1feac5SDavid Ahern 24394c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2440ca254490SDavid Ahern if (dst) 2441ca254490SDavid Ahern return dst; 24424c1feac5SDavid Ahern } 2443ca254490SDavid Ahern 24441fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 24454dc27d1cSDavid McCullough 2446d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2447741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2448d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 244977d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2450c71099acSThomas Graf 2451d46a9d67SDavid Ahern if (!any_src) 2452adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 24530c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 24540c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2455adaa70bbSThomas Graf 2456b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 24571da177e4SLinus Torvalds } 24586f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 24591da177e4SLinus Torvalds 24602774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 246114e50e57SDavid S. Miller { 24625c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 24631dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 246414e50e57SDavid S. Miller struct dst_entry *new = NULL; 246514e50e57SDavid S. Miller 24661dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 246762cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 246814e50e57SDavid S. Miller if (rt) { 24690a1f5962SMartin KaFai Lau rt6_info_init(rt); 247081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 24710a1f5962SMartin KaFai Lau 2472d8d1f30bSChangli Gao new = &rt->dst; 247314e50e57SDavid S. Miller new->__use = 1; 2474352e512cSHerbert Xu new->input = dst_discard; 2475ede2059dSEric W. Biederman new->output = dst_discard_out; 247614e50e57SDavid S. Miller 2477defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 247814e50e57SDavid S. Miller 24791dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 24804e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 24810a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 248214e50e57SDavid S. Miller 248314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 248414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 248514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 248614e50e57SDavid S. Miller #endif 248714e50e57SDavid S. Miller } 248814e50e57SDavid S. Miller 248969ead7afSDavid S. Miller dst_release(dst_orig); 249069ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 249114e50e57SDavid S. Miller } 249214e50e57SDavid S. Miller 24931da177e4SLinus Torvalds /* 24941da177e4SLinus Torvalds * Destination cache support functions 24951da177e4SLinus Torvalds */ 24961da177e4SLinus Torvalds 24978d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 24983da59bd9SMartin KaFai Lau { 249936143645SSteffen Klassert u32 rt_cookie = 0; 2500c5cff856SWei Wang 25018ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 250293531c67SDavid Ahern return false; 250393531c67SDavid Ahern 250493531c67SDavid Ahern if (fib6_check_expired(f6i)) 250593531c67SDavid Ahern return false; 250693531c67SDavid Ahern 250793531c67SDavid Ahern return true; 250893531c67SDavid Ahern } 250993531c67SDavid Ahern 2510a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2511a68886a6SDavid Ahern struct fib6_info *from, 2512a68886a6SDavid Ahern u32 cookie) 25133da59bd9SMartin KaFai Lau { 2514c5cff856SWei Wang u32 rt_cookie = 0; 2515c5cff856SWei Wang 2516a68886a6SDavid Ahern if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || 251793531c67SDavid Ahern rt_cookie != cookie) 25183da59bd9SMartin KaFai Lau return NULL; 25193da59bd9SMartin KaFai Lau 25203da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 25213da59bd9SMartin KaFai Lau return NULL; 25223da59bd9SMartin KaFai Lau 25233da59bd9SMartin KaFai Lau return &rt->dst; 25243da59bd9SMartin KaFai Lau } 25253da59bd9SMartin KaFai Lau 2526a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2527a68886a6SDavid Ahern struct fib6_info *from, 2528a68886a6SDavid Ahern u32 cookie) 25293da59bd9SMartin KaFai Lau { 25305973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 25315973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2532a68886a6SDavid Ahern fib6_check(from, cookie)) 25333da59bd9SMartin KaFai Lau return &rt->dst; 25343da59bd9SMartin KaFai Lau else 25353da59bd9SMartin KaFai Lau return NULL; 25363da59bd9SMartin KaFai Lau } 25373da59bd9SMartin KaFai Lau 25381da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 25391da177e4SLinus Torvalds { 2540a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2541a68886a6SDavid Ahern struct fib6_info *from; 25421da177e4SLinus Torvalds struct rt6_info *rt; 25431da177e4SLinus Torvalds 2544a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2545a87b7dc9SDavid Ahern 2546a87b7dc9SDavid Ahern rcu_read_lock(); 25471da177e4SLinus Torvalds 25486f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 25496f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 25506f3118b5SNicolas Dichtel * into this function always. 25516f3118b5SNicolas Dichtel */ 2552e3bc10bdSHannes Frederic Sowa 2553a68886a6SDavid Ahern from = rcu_dereference(rt->from); 25544b32b5adSMartin KaFai Lau 2555a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2556a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2557a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 25583da59bd9SMartin KaFai Lau else 2559a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2560a87b7dc9SDavid Ahern 2561a87b7dc9SDavid Ahern rcu_read_unlock(); 2562a87b7dc9SDavid Ahern 2563a87b7dc9SDavid Ahern return dst_ret; 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 25671da177e4SLinus Torvalds { 25681da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds if (rt) { 257154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2572c3c14da0SDavid Ahern rcu_read_lock(); 257354c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 257493531c67SDavid Ahern rt6_remove_exception_rt(rt); 257554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 25761da177e4SLinus Torvalds } 2577c3c14da0SDavid Ahern rcu_read_unlock(); 257854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 257954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 258054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 258154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 258254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 258354c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 25871da177e4SLinus Torvalds { 25881da177e4SLinus Torvalds struct rt6_info *rt; 25891da177e4SLinus Torvalds 25903ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 25911da177e4SLinus Torvalds 2592adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 25931da177e4SLinus Torvalds if (rt) { 25948a14e46fSDavid Ahern rcu_read_lock(); 25951eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 259693531c67SDavid Ahern rt6_remove_exception_rt(rt); 2597c5cff856SWei Wang } else { 2598a68886a6SDavid Ahern struct fib6_info *from; 2599c5cff856SWei Wang struct fib6_node *fn; 2600c5cff856SWei Wang 2601a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2602a68886a6SDavid Ahern if (from) { 2603a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2604c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2605c5cff856SWei Wang fn->fn_sernum = -1; 2606a68886a6SDavid Ahern } 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds rcu_read_unlock(); 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26126a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 26136a3e030fSDavid Ahern { 2614a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2615a68886a6SDavid Ahern struct fib6_info *from; 2616a68886a6SDavid Ahern 2617a68886a6SDavid Ahern rcu_read_lock(); 2618a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2619a68886a6SDavid Ahern if (from) 2620a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2621a68886a6SDavid Ahern rcu_read_unlock(); 2622a68886a6SDavid Ahern } 26236a3e030fSDavid Ahern 26246a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 26256a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 26266700c270SDavid S. Miller } 26271da177e4SLinus Torvalds 262845e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 262945e4fd26SMartin KaFai Lau { 263045e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 263145e4fd26SMartin KaFai Lau 2632d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 263345e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 263445e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 263545e4fd26SMartin KaFai Lau } 263645e4fd26SMartin KaFai Lau 26370d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 26380d3f6d29SMartin KaFai Lau { 26390d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 26401490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 26410d3f6d29SMartin KaFai Lau } 26420d3f6d29SMartin KaFai Lau 264345e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 264445e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 26451da177e4SLinus Torvalds { 26460dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 26471da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 26481da177e4SLinus Torvalds 264919bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 265019bda36cSXin Long return; 265119bda36cSXin Long 265245e4fd26SMartin KaFai Lau if (iph) { 265345e4fd26SMartin KaFai Lau daddr = &iph->daddr; 265445e4fd26SMartin KaFai Lau saddr = &iph->saddr; 265545e4fd26SMartin KaFai Lau } else if (sk) { 265645e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 265745e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 265845e4fd26SMartin KaFai Lau } else { 26590dec879fSJulian Anastasov daddr = NULL; 26600dec879fSJulian Anastasov saddr = NULL; 26611da177e4SLinus Torvalds } 26620dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 26630dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 26640dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 26650dec879fSJulian Anastasov return; 26660dec879fSJulian Anastasov 26670dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 26680dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 26692b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 26702b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 26712b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 26720dec879fSJulian Anastasov } else if (daddr) { 267385bd05deSDavid Ahern struct fib6_result res = {}; 26740dec879fSJulian Anastasov struct rt6_info *nrt6; 26750dec879fSJulian Anastasov 26764d85cd0cSDavid Ahern rcu_read_lock(); 267785bd05deSDavid Ahern res.f6i = rcu_dereference(rt6->from); 267885bd05deSDavid Ahern if (!res.f6i) { 26799c69a132SJonathan Lemon rcu_read_unlock(); 26809c69a132SJonathan Lemon return; 26819c69a132SJonathan Lemon } 26821cf844c7SDavid Ahern res.nh = res.f6i->fib6_nh; 26837d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 26847d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 26857d21fec9SDavid Ahern 268685bd05deSDavid Ahern nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 268745e4fd26SMartin KaFai Lau if (nrt6) { 268845e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 26895012f0a5SDavid Ahern if (rt6_insert_exception(nrt6, &res)) 26902b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 269145e4fd26SMartin KaFai Lau } 2692a68886a6SDavid Ahern rcu_read_unlock(); 269345e4fd26SMartin KaFai Lau } 269445e4fd26SMartin KaFai Lau } 269545e4fd26SMartin KaFai Lau 269645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 269745e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 269845e4fd26SMartin KaFai Lau { 269945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 270242ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2703e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 270481aded24SDavid S. Miller { 270581aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 270681aded24SDavid S. Miller struct dst_entry *dst; 2707dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2708dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2709dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2710dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2711dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2712dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2713dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2714dc92095dSMaciej Żenczykowski }; 271581aded24SDavid S. Miller 271681aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 271781aded24SDavid S. Miller if (!dst->error) 271845e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 271981aded24SDavid S. Miller dst_release(dst); 272081aded24SDavid S. Miller } 272181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 272281aded24SDavid S. Miller 272381aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 272481aded24SDavid S. Miller { 27257ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 272633c162a9SMartin KaFai Lau struct dst_entry *dst; 272733c162a9SMartin KaFai Lau 27287ddacfa5SDavid Ahern if (!oif && skb->dev) 27297ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 27307ddacfa5SDavid Ahern 27317ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 273233c162a9SMartin KaFai Lau 273333c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 273433c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 273533c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 273633c162a9SMartin KaFai Lau return; 273733c162a9SMartin KaFai Lau 273833c162a9SMartin KaFai Lau bh_lock_sock(sk); 273933c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 274033c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 274133c162a9SMartin KaFai Lau bh_unlock_sock(sk); 274281aded24SDavid S. Miller } 274381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 274481aded24SDavid S. Miller 27457d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 27467d6850f7SAlexey Kodanev const struct flowi6 *fl6) 27477d6850f7SAlexey Kodanev { 27487d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 27497d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 27507d6850f7SAlexey Kodanev #endif 27517d6850f7SAlexey Kodanev 27527d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 27537d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 27547d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 27557d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 27567d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 27577d6850f7SAlexey Kodanev &np->saddr : 27587d6850f7SAlexey Kodanev #endif 27597d6850f7SAlexey Kodanev NULL); 27607d6850f7SAlexey Kodanev } 27617d6850f7SAlexey Kodanev 27629b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res, 27630b34eb00SDavid Ahern struct flowi6 *fl6, 27640b34eb00SDavid Ahern const struct in6_addr *gw, 27650b34eb00SDavid Ahern struct rt6_info **ret) 27660b34eb00SDavid Ahern { 27679b6b35abSDavid Ahern const struct fib6_nh *nh = res->nh; 27689b6b35abSDavid Ahern 27690b34eb00SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 27700b34eb00SDavid Ahern fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 27710b34eb00SDavid Ahern return false; 27720b34eb00SDavid Ahern 27730b34eb00SDavid Ahern /* rt_cache's gateway might be different from its 'parent' 27740b34eb00SDavid Ahern * in the case of an ip redirect. 27750b34eb00SDavid Ahern * So we keep searching in the exception table if the gateway 27760b34eb00SDavid Ahern * is different. 27770b34eb00SDavid Ahern */ 27780b34eb00SDavid Ahern if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 27790b34eb00SDavid Ahern struct rt6_info *rt_cache; 27800b34eb00SDavid Ahern 27819b6b35abSDavid Ahern rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 27820b34eb00SDavid Ahern if (rt_cache && 27830b34eb00SDavid Ahern ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 27840b34eb00SDavid Ahern *ret = rt_cache; 27850b34eb00SDavid Ahern return true; 27860b34eb00SDavid Ahern } 27870b34eb00SDavid Ahern return false; 27880b34eb00SDavid Ahern } 27890b34eb00SDavid Ahern return true; 27900b34eb00SDavid Ahern } 27910b34eb00SDavid Ahern 2792*c55c8988SDavid Ahern struct fib6_nh_rd_arg { 2793*c55c8988SDavid Ahern struct fib6_result *res; 2794*c55c8988SDavid Ahern struct flowi6 *fl6; 2795*c55c8988SDavid Ahern const struct in6_addr *gw; 2796*c55c8988SDavid Ahern struct rt6_info **ret; 2797*c55c8988SDavid Ahern }; 2798*c55c8988SDavid Ahern 2799*c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) 2800*c55c8988SDavid Ahern { 2801*c55c8988SDavid Ahern struct fib6_nh_rd_arg *arg = _arg; 2802*c55c8988SDavid Ahern 2803*c55c8988SDavid Ahern arg->res->nh = nh; 2804*c55c8988SDavid Ahern return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); 2805*c55c8988SDavid Ahern } 2806*c55c8988SDavid Ahern 2807b55b76b2SDuan Jiong /* Handle redirects */ 2808b55b76b2SDuan Jiong struct ip6rd_flowi { 2809b55b76b2SDuan Jiong struct flowi6 fl6; 2810b55b76b2SDuan Jiong struct in6_addr gateway; 2811b55b76b2SDuan Jiong }; 2812b55b76b2SDuan Jiong 2813b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2814b55b76b2SDuan Jiong struct fib6_table *table, 2815b55b76b2SDuan Jiong struct flowi6 *fl6, 2816b75cc8f9SDavid Ahern const struct sk_buff *skb, 2817b55b76b2SDuan Jiong int flags) 2818b55b76b2SDuan Jiong { 2819b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 28200b34eb00SDavid Ahern struct rt6_info *ret = NULL; 28219b6b35abSDavid Ahern struct fib6_result res = {}; 2822*c55c8988SDavid Ahern struct fib6_nh_rd_arg arg = { 2823*c55c8988SDavid Ahern .res = &res, 2824*c55c8988SDavid Ahern .fl6 = fl6, 2825*c55c8988SDavid Ahern .gw = &rdfl->gateway, 2826*c55c8988SDavid Ahern .ret = &ret 2827*c55c8988SDavid Ahern }; 28288d1c802bSDavid Ahern struct fib6_info *rt; 2829b55b76b2SDuan Jiong struct fib6_node *fn; 2830b55b76b2SDuan Jiong 283131680ac2SDavid Ahern /* l3mdev_update_flow overrides oif if the device is enslaved; in 283231680ac2SDavid Ahern * this case we must match on the real ingress device, so reset it 283331680ac2SDavid Ahern */ 283431680ac2SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 283531680ac2SDavid Ahern fl6->flowi6_oif = skb->dev->ifindex; 283631680ac2SDavid Ahern 2837b55b76b2SDuan Jiong /* Get the "current" route for this destination and 283867c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2839b55b76b2SDuan Jiong * 2840b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2841b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2842b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2843b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2844b55b76b2SDuan Jiong * routes. 2845b55b76b2SDuan Jiong */ 2846b55b76b2SDuan Jiong 284766f5d6ceSWei Wang rcu_read_lock(); 28486454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2849b55b76b2SDuan Jiong restart: 285066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 28519b6b35abSDavid Ahern res.f6i = rt; 285214895687SDavid Ahern if (fib6_check_expired(rt)) 2853b55b76b2SDuan Jiong continue; 285493c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2855b55b76b2SDuan Jiong break; 2856*c55c8988SDavid Ahern if (unlikely(rt->nh)) { 2857*c55c8988SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 2858*c55c8988SDavid Ahern continue; 2859*c55c8988SDavid Ahern /* on match, res->nh is filled in and potentially ret */ 2860*c55c8988SDavid Ahern if (nexthop_for_each_fib6_nh(rt->nh, 2861*c55c8988SDavid Ahern fib6_nh_redirect_match, 2862*c55c8988SDavid Ahern &arg)) 28630b34eb00SDavid Ahern goto out; 2864*c55c8988SDavid Ahern } else { 2865*c55c8988SDavid Ahern res.nh = rt->fib6_nh; 2866*c55c8988SDavid Ahern if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, 2867*c55c8988SDavid Ahern &ret)) 2868*c55c8988SDavid Ahern goto out; 2869*c55c8988SDavid Ahern } 2870b55b76b2SDuan Jiong } 2871b55b76b2SDuan Jiong 2872b55b76b2SDuan Jiong if (!rt) 2873421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 287493c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 287523fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2876b0a1ba59SMartin KaFai Lau goto out; 2877b0a1ba59SMartin KaFai Lau } 2878b0a1ba59SMartin KaFai Lau 2879421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2880a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2881a3c00e46SMartin KaFai Lau if (fn) 2882a3c00e46SMartin KaFai Lau goto restart; 2883b55b76b2SDuan Jiong } 2884a3c00e46SMartin KaFai Lau 28859b6b35abSDavid Ahern res.f6i = rt; 28861cf844c7SDavid Ahern res.nh = rt->fib6_nh; 2887b0a1ba59SMartin KaFai Lau out: 28887d21fec9SDavid Ahern if (ret) { 288910585b43SDavid Ahern ip6_hold_safe(net, &ret); 28907d21fec9SDavid Ahern } else { 28917d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 28927d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 28939b6b35abSDavid Ahern ret = ip6_create_rt_rcu(&res); 28947d21fec9SDavid Ahern } 2895b55b76b2SDuan Jiong 289666f5d6ceSWei Wang rcu_read_unlock(); 2897b55b76b2SDuan Jiong 28988ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 289923fb93a4SDavid Ahern return ret; 2900b55b76b2SDuan Jiong }; 2901b55b76b2SDuan Jiong 2902b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2903b55b76b2SDuan Jiong const struct flowi6 *fl6, 2904b75cc8f9SDavid Ahern const struct sk_buff *skb, 2905b55b76b2SDuan Jiong const struct in6_addr *gateway) 2906b55b76b2SDuan Jiong { 2907b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2908b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2909b55b76b2SDuan Jiong 2910b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2911b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2912b55b76b2SDuan Jiong 2913b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2914b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2915b55b76b2SDuan Jiong } 2916b55b76b2SDuan Jiong 2917e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2918e2d118a1SLorenzo Colitti kuid_t uid) 29193a5ad2eeSDavid S. Miller { 29203a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 29213a5ad2eeSDavid S. Miller struct dst_entry *dst; 29221f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 29231f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 29241f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 29251f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 29261f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 29271f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 29281f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 29291f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 29301f7f10acSMaciej Żenczykowski }; 29313a5ad2eeSDavid S. Miller 2932b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 29336700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 29343a5ad2eeSDavid S. Miller dst_release(dst); 29353a5ad2eeSDavid S. Miller } 29363a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 29373a5ad2eeSDavid S. Miller 2938d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 2939c92a59ecSDuan Jiong { 2940c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2941c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2942c92a59ecSDuan Jiong struct dst_entry *dst; 29430b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 29440b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 29450b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 29460b26fb17SMaciej Żenczykowski .daddr = msg->dest, 29470b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 29480b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 29490b26fb17SMaciej Żenczykowski }; 2950c92a59ecSDuan Jiong 2951b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 2952c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2953c92a59ecSDuan Jiong dst_release(dst); 2954c92a59ecSDuan Jiong } 2955c92a59ecSDuan Jiong 29563a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 29573a5ad2eeSDavid S. Miller { 2958e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2959e2d118a1SLorenzo Colitti sk->sk_uid); 29603a5ad2eeSDavid S. Miller } 29613a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 29623a5ad2eeSDavid S. Miller 29630dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 29641da177e4SLinus Torvalds { 29650dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 29660dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 29670dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 29680dbaee3bSDavid S. Miller 29691da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 29701da177e4SLinus Torvalds 29715578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 29725578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds /* 29751da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 29761da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 29771da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 29781da177e4SLinus Torvalds * rely only on pmtu discovery" 29791da177e4SLinus Torvalds */ 29801da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 29811da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 29821da177e4SLinus Torvalds return mtu; 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds 2985ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2986d33e4553SDavid S. Miller { 2987d33e4553SDavid S. Miller struct inet6_dev *idev; 2988d4ead6b3SDavid Ahern unsigned int mtu; 2989618f9bc7SSteffen Klassert 29904b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 29914b32b5adSMartin KaFai Lau if (mtu) 29924b32b5adSMartin KaFai Lau goto out; 29934b32b5adSMartin KaFai Lau 2994618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2995d33e4553SDavid S. Miller 2996d33e4553SDavid S. Miller rcu_read_lock(); 2997d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2998d33e4553SDavid S. Miller if (idev) 2999d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 3000d33e4553SDavid S. Miller rcu_read_unlock(); 3001d33e4553SDavid S. Miller 300230f78d8eSEric Dumazet out: 300314972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 300414972cbdSRoopa Prabhu 300514972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 3006d33e4553SDavid S. Miller } 3007d33e4553SDavid S. Miller 3008901731b8SDavid Ahern /* MTU selection: 3009901731b8SDavid Ahern * 1. mtu on route is locked - use it 3010901731b8SDavid Ahern * 2. mtu from nexthop exception 3011901731b8SDavid Ahern * 3. mtu from egress device 3012901731b8SDavid Ahern * 3013901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 3014901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 3015901731b8SDavid Ahern */ 3016b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res, 3017b748f260SDavid Ahern const struct in6_addr *daddr, 3018b748f260SDavid Ahern const struct in6_addr *saddr) 3019901731b8SDavid Ahern { 3020b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 3021b748f260SDavid Ahern struct fib6_info *f6i = res->f6i; 3022901731b8SDavid Ahern struct inet6_dev *idev; 3023510e2cedSWei Wang struct rt6_info *rt; 3024901731b8SDavid Ahern u32 mtu = 0; 3025901731b8SDavid Ahern 3026901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 3027901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 3028901731b8SDavid Ahern if (mtu) 3029901731b8SDavid Ahern goto out; 3030901731b8SDavid Ahern } 3031901731b8SDavid Ahern 3032510e2cedSWei Wang rt = rt6_find_cached_rt(res, daddr, saddr); 3033510e2cedSWei Wang if (unlikely(rt)) { 3034510e2cedSWei Wang mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 3035510e2cedSWei Wang } else { 3036b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 3037901731b8SDavid Ahern 3038901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 3039901731b8SDavid Ahern idev = __in6_dev_get(dev); 3040901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 3041901731b8SDavid Ahern mtu = idev->cnf.mtu6; 3042901731b8SDavid Ahern } 3043901731b8SDavid Ahern 3044901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 3045901731b8SDavid Ahern out: 3046b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 3047901731b8SDavid Ahern } 3048901731b8SDavid Ahern 30493b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 305087a11578SDavid S. Miller struct flowi6 *fl6) 30511da177e4SLinus Torvalds { 305287a11578SDavid S. Miller struct dst_entry *dst; 30531da177e4SLinus Torvalds struct rt6_info *rt; 30541da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 3055c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 30561da177e4SLinus Torvalds 305738308473SDavid S. Miller if (unlikely(!idev)) 3058122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 30591da177e4SLinus Torvalds 3060ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 306138308473SDavid S. Miller if (unlikely(!rt)) { 30621da177e4SLinus Torvalds in6_dev_put(idev); 306387a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 30641da177e4SLinus Torvalds goto out; 30651da177e4SLinus Torvalds } 30661da177e4SLinus Torvalds 30678e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 3068588753f1SBrendan McGrath rt->dst.input = ip6_input; 30698e2ec639SYan, Zheng rt->dst.output = ip6_output; 3070550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 307187a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 30728e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 30738e2ec639SYan, Zheng rt->rt6i_idev = idev; 307414edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 30751da177e4SLinus Torvalds 30764c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 3077587fea74SWei Wang * do proper release of the net_device 3078587fea74SWei Wang */ 3079587fea74SWei Wang rt6_uncached_list_add(rt); 308081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 30811da177e4SLinus Torvalds 308287a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 308387a11578SDavid S. Miller 30841da177e4SLinus Torvalds out: 308587a11578SDavid S. Miller return dst; 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 3088569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 30891da177e4SLinus Torvalds { 309086393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 30917019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 30927019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 30937019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 30947019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 30957019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 3096fc66f95cSEric Dumazet int entries; 30971da177e4SLinus Torvalds 3098fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 309949a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 3100fc66f95cSEric Dumazet entries <= rt_max_size) 31011da177e4SLinus Torvalds goto out; 31021da177e4SLinus Torvalds 31036891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 310414956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 3105fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 3106fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 31077019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 31081da177e4SLinus Torvalds out: 31097019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 3110fc66f95cSEric Dumazet return entries > rt_max_size; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 31138c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 31148c14586fSDavid Ahern struct fib6_config *cfg, 3115f4797b33SDavid Ahern const struct in6_addr *gw_addr, 3116f4797b33SDavid Ahern u32 tbid, int flags) 31178c14586fSDavid Ahern { 31188c14586fSDavid Ahern struct flowi6 fl6 = { 31198c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 31208c14586fSDavid Ahern .daddr = *gw_addr, 31218c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 31228c14586fSDavid Ahern }; 31238c14586fSDavid Ahern struct fib6_table *table; 31248c14586fSDavid Ahern struct rt6_info *rt; 31258c14586fSDavid Ahern 3126f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 31278c14586fSDavid Ahern if (!table) 31288c14586fSDavid Ahern return NULL; 31298c14586fSDavid Ahern 31308c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 31318c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 31328c14586fSDavid Ahern 3133f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 3134b75cc8f9SDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); 31358c14586fSDavid Ahern 31368c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 31378c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 31388c14586fSDavid Ahern ip6_rt_put(rt); 31398c14586fSDavid Ahern rt = NULL; 31408c14586fSDavid Ahern } 31418c14586fSDavid Ahern 31428c14586fSDavid Ahern return rt; 31438c14586fSDavid Ahern } 31448c14586fSDavid Ahern 3145fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 3146fc1e64e1SDavid Ahern struct fib6_config *cfg, 31479fbb704cSDavid Ahern const struct net_device *dev, 3148fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 3149fc1e64e1SDavid Ahern { 315044750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 3151fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 3152fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 3153bf1dc8baSPaolo Abeni struct fib6_info *from; 3154fc1e64e1SDavid Ahern struct rt6_info *grt; 3155fc1e64e1SDavid Ahern int err; 3156fc1e64e1SDavid Ahern 3157fc1e64e1SDavid Ahern err = 0; 3158fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 3159fc1e64e1SDavid Ahern if (grt) { 3160bf1dc8baSPaolo Abeni rcu_read_lock(); 3161bf1dc8baSPaolo Abeni from = rcu_dereference(grt->from); 316258e354c0SDavid Ahern if (!grt->dst.error && 31634ed591c8SDavid Ahern /* ignore match if it is the default route */ 3164bf1dc8baSPaolo Abeni from && !ipv6_addr_any(&from->fib6_dst.addr) && 316558e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 316644750f84SDavid Ahern NL_SET_ERR_MSG(extack, 316744750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 3168fc1e64e1SDavid Ahern err = -EINVAL; 3169fc1e64e1SDavid Ahern } 3170bf1dc8baSPaolo Abeni rcu_read_unlock(); 3171fc1e64e1SDavid Ahern 3172fc1e64e1SDavid Ahern ip6_rt_put(grt); 3173fc1e64e1SDavid Ahern } 3174fc1e64e1SDavid Ahern 3175fc1e64e1SDavid Ahern return err; 3176fc1e64e1SDavid Ahern } 3177fc1e64e1SDavid Ahern 31781edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 31791edce99fSDavid Ahern struct fib6_config *cfg, 31801edce99fSDavid Ahern struct net_device **_dev, 31811edce99fSDavid Ahern struct inet6_dev **idev) 31821edce99fSDavid Ahern { 31831edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 31841edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 31851edce99fSDavid Ahern struct rt6_info *grt = NULL; 31861edce99fSDavid Ahern int err = -EHOSTUNREACH; 31871edce99fSDavid Ahern 31881edce99fSDavid Ahern if (cfg->fc_table) { 3189f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 3190f4797b33SDavid Ahern 3191f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 3192f4797b33SDavid Ahern cfg->fc_table, flags); 31931edce99fSDavid Ahern if (grt) { 31941edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 31951edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 31961edce99fSDavid Ahern ip6_rt_put(grt); 31971edce99fSDavid Ahern grt = NULL; 31981edce99fSDavid Ahern } 31991edce99fSDavid Ahern } 32001edce99fSDavid Ahern } 32011edce99fSDavid Ahern 32021edce99fSDavid Ahern if (!grt) 3203b75cc8f9SDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); 32041edce99fSDavid Ahern 32051edce99fSDavid Ahern if (!grt) 32061edce99fSDavid Ahern goto out; 32071edce99fSDavid Ahern 32081edce99fSDavid Ahern if (dev) { 32091edce99fSDavid Ahern if (dev != grt->dst.dev) { 32101edce99fSDavid Ahern ip6_rt_put(grt); 32111edce99fSDavid Ahern goto out; 32121edce99fSDavid Ahern } 32131edce99fSDavid Ahern } else { 32141edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 32151edce99fSDavid Ahern *idev = grt->rt6i_idev; 32161edce99fSDavid Ahern dev_hold(dev); 32171edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 32181edce99fSDavid Ahern } 32191edce99fSDavid Ahern 32201edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 32211edce99fSDavid Ahern err = 0; 32221edce99fSDavid Ahern 32231edce99fSDavid Ahern ip6_rt_put(grt); 32241edce99fSDavid Ahern 32251edce99fSDavid Ahern out: 32261edce99fSDavid Ahern return err; 32271edce99fSDavid Ahern } 32281edce99fSDavid Ahern 32299fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 32309fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 32319fbb704cSDavid Ahern struct netlink_ext_ack *extack) 32329fbb704cSDavid Ahern { 32339fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 32349fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 3235232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 32369fbb704cSDavid Ahern const struct net_device *dev = *_dev; 3237232378e8SDavid Ahern bool need_addr_check = !dev; 32389fbb704cSDavid Ahern int err = -EINVAL; 32399fbb704cSDavid Ahern 32409fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 32419fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 32429fbb704cSDavid Ahern * will return already-added prefix route via interface that 32439fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 32449fbb704cSDavid Ahern */ 3245232378e8SDavid Ahern if (dev && 3246232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3247232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 32489fbb704cSDavid Ahern goto out; 32499fbb704cSDavid Ahern } 32509fbb704cSDavid Ahern 32519fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 32529fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 32539fbb704cSDavid Ahern * addresses as nexthop address. 32549fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 32559fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 32569fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 32579fbb704cSDavid Ahern * some exceptions. --ANK 32589fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 32599fbb704cSDavid Ahern * addressing 32609fbb704cSDavid Ahern */ 32619fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 32629fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 32639fbb704cSDavid Ahern goto out; 32649fbb704cSDavid Ahern } 32659fbb704cSDavid Ahern 32669fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 32679fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 32689fbb704cSDavid Ahern else 32699fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 32709fbb704cSDavid Ahern 32719fbb704cSDavid Ahern if (err) 32729fbb704cSDavid Ahern goto out; 32739fbb704cSDavid Ahern } 32749fbb704cSDavid Ahern 32759fbb704cSDavid Ahern /* reload in case device was changed */ 32769fbb704cSDavid Ahern dev = *_dev; 32779fbb704cSDavid Ahern 32789fbb704cSDavid Ahern err = -EINVAL; 32799fbb704cSDavid Ahern if (!dev) { 32809fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 32819fbb704cSDavid Ahern goto out; 32829fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 32839fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 32849fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 32859fbb704cSDavid Ahern goto out; 32869fbb704cSDavid Ahern } 3287232378e8SDavid Ahern 3288232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 3289232378e8SDavid Ahern * egress device has been resolved. 3290232378e8SDavid Ahern */ 3291232378e8SDavid Ahern if (need_addr_check && 3292232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 3293232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 3294232378e8SDavid Ahern goto out; 3295232378e8SDavid Ahern } 3296232378e8SDavid Ahern 32979fbb704cSDavid Ahern err = 0; 32989fbb704cSDavid Ahern out: 32999fbb704cSDavid Ahern return err; 33009fbb704cSDavid Ahern } 33019fbb704cSDavid Ahern 330283c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 330383c44251SDavid Ahern { 330483c44251SDavid Ahern if ((flags & RTF_REJECT) || 330583c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 330683c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 330783c44251SDavid Ahern !(flags & RTF_LOCAL))) 330883c44251SDavid Ahern return true; 330983c44251SDavid Ahern 331083c44251SDavid Ahern return false; 331183c44251SDavid Ahern } 331283c44251SDavid Ahern 331383c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 331483c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 331583c44251SDavid Ahern struct netlink_ext_ack *extack) 331683c44251SDavid Ahern { 331783c44251SDavid Ahern struct net_device *dev = NULL; 331883c44251SDavid Ahern struct inet6_dev *idev = NULL; 331983c44251SDavid Ahern int addr_type; 332083c44251SDavid Ahern int err; 332183c44251SDavid Ahern 3322f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 3323f1741730SDavid Ahern 332483c44251SDavid Ahern err = -ENODEV; 332583c44251SDavid Ahern if (cfg->fc_ifindex) { 332683c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 332783c44251SDavid Ahern if (!dev) 332883c44251SDavid Ahern goto out; 332983c44251SDavid Ahern idev = in6_dev_get(dev); 333083c44251SDavid Ahern if (!idev) 333183c44251SDavid Ahern goto out; 333283c44251SDavid Ahern } 333383c44251SDavid Ahern 333483c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 333583c44251SDavid Ahern if (!dev) { 333683c44251SDavid Ahern NL_SET_ERR_MSG(extack, 333783c44251SDavid Ahern "Nexthop device required for onlink"); 333883c44251SDavid Ahern goto out; 333983c44251SDavid Ahern } 334083c44251SDavid Ahern 334183c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 334283c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 334383c44251SDavid Ahern err = -ENETDOWN; 334483c44251SDavid Ahern goto out; 334583c44251SDavid Ahern } 334683c44251SDavid Ahern 3347ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 334883c44251SDavid Ahern } 334983c44251SDavid Ahern 3350ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 335183c44251SDavid Ahern 335283c44251SDavid Ahern /* We cannot add true routes via loopback here, 335383c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 335483c44251SDavid Ahern */ 335583c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 335683c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 335783c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 335883c44251SDavid Ahern if (dev != net->loopback_dev) { 335983c44251SDavid Ahern if (dev) { 336083c44251SDavid Ahern dev_put(dev); 336183c44251SDavid Ahern in6_dev_put(idev); 336283c44251SDavid Ahern } 336383c44251SDavid Ahern dev = net->loopback_dev; 336483c44251SDavid Ahern dev_hold(dev); 336583c44251SDavid Ahern idev = in6_dev_get(dev); 336683c44251SDavid Ahern if (!idev) { 336783c44251SDavid Ahern err = -ENODEV; 336883c44251SDavid Ahern goto out; 336983c44251SDavid Ahern } 337083c44251SDavid Ahern } 33717dd73168SDavid Ahern goto pcpu_alloc; 337283c44251SDavid Ahern } 337383c44251SDavid Ahern 337483c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 337583c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 337683c44251SDavid Ahern if (err) 337783c44251SDavid Ahern goto out; 337883c44251SDavid Ahern 3379ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3380bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 338183c44251SDavid Ahern } 338283c44251SDavid Ahern 338383c44251SDavid Ahern err = -ENODEV; 338483c44251SDavid Ahern if (!dev) 338583c44251SDavid Ahern goto out; 338683c44251SDavid Ahern 338783c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 338883c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 338983c44251SDavid Ahern err = -EACCES; 339083c44251SDavid Ahern goto out; 339183c44251SDavid Ahern } 339283c44251SDavid Ahern 339383c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 339483c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 339583c44251SDavid Ahern err = -ENETDOWN; 339683c44251SDavid Ahern goto out; 339783c44251SDavid Ahern } 339883c44251SDavid Ahern 339983c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 340083c44251SDavid Ahern !netif_carrier_ok(dev)) 3401ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 340283c44251SDavid Ahern 34037dd73168SDavid Ahern err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, 34047dd73168SDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 34057dd73168SDavid Ahern if (err) 34067dd73168SDavid Ahern goto out; 34077dd73168SDavid Ahern 34087dd73168SDavid Ahern pcpu_alloc: 3409f40b6ae2SDavid Ahern fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3410f40b6ae2SDavid Ahern if (!fib6_nh->rt6i_pcpu) { 3411f40b6ae2SDavid Ahern err = -ENOMEM; 3412f40b6ae2SDavid Ahern goto out; 3413f40b6ae2SDavid Ahern } 3414f40b6ae2SDavid Ahern 3415ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3416f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 341783c44251SDavid Ahern err = 0; 341883c44251SDavid Ahern out: 341983c44251SDavid Ahern if (idev) 342083c44251SDavid Ahern in6_dev_put(idev); 342183c44251SDavid Ahern 342283c44251SDavid Ahern if (err) { 3423ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3424ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 342583c44251SDavid Ahern if (dev) 342683c44251SDavid Ahern dev_put(dev); 342783c44251SDavid Ahern } 342883c44251SDavid Ahern 342983c44251SDavid Ahern return err; 343083c44251SDavid Ahern } 343183c44251SDavid Ahern 3432dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3433dac7d0f2SDavid Ahern { 3434cc5c073aSDavid Ahern struct rt6_exception_bucket *bucket; 3435cc5c073aSDavid Ahern 3436cc5c073aSDavid Ahern rcu_read_lock(); 3437cc5c073aSDavid Ahern 3438cc5c073aSDavid Ahern fib6_nh_flush_exceptions(fib6_nh, NULL); 3439cc5c073aSDavid Ahern bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); 3440cc5c073aSDavid Ahern if (bucket) { 3441cc5c073aSDavid Ahern rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); 3442cc5c073aSDavid Ahern kfree(bucket); 3443cc5c073aSDavid Ahern } 3444cc5c073aSDavid Ahern 3445cc5c073aSDavid Ahern rcu_read_unlock(); 3446cc5c073aSDavid Ahern 3447f40b6ae2SDavid Ahern if (fib6_nh->rt6i_pcpu) { 3448f40b6ae2SDavid Ahern int cpu; 3449f40b6ae2SDavid Ahern 3450f40b6ae2SDavid Ahern for_each_possible_cpu(cpu) { 3451f40b6ae2SDavid Ahern struct rt6_info **ppcpu_rt; 3452f40b6ae2SDavid Ahern struct rt6_info *pcpu_rt; 3453f40b6ae2SDavid Ahern 3454f40b6ae2SDavid Ahern ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 3455f40b6ae2SDavid Ahern pcpu_rt = *ppcpu_rt; 3456f40b6ae2SDavid Ahern if (pcpu_rt) { 3457f40b6ae2SDavid Ahern dst_dev_put(&pcpu_rt->dst); 3458f40b6ae2SDavid Ahern dst_release(&pcpu_rt->dst); 3459f40b6ae2SDavid Ahern *ppcpu_rt = NULL; 3460f40b6ae2SDavid Ahern } 3461f40b6ae2SDavid Ahern } 3462f40b6ae2SDavid Ahern 3463f40b6ae2SDavid Ahern free_percpu(fib6_nh->rt6i_pcpu); 3464f40b6ae2SDavid Ahern } 3465f40b6ae2SDavid Ahern 3466979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3467dac7d0f2SDavid Ahern } 3468dac7d0f2SDavid Ahern 34698d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3470acb54e3cSDavid Ahern gfp_t gfp_flags, 3471333c4301SDavid Ahern struct netlink_ext_ack *extack) 34721da177e4SLinus Torvalds { 34735578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 34748d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3475f88d8ea6SDavid Ahern struct nexthop *nh = NULL; 3476c71099acSThomas Graf struct fib6_table *table; 3477f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 34788c5b83f0SRoopa Prabhu int err = -EINVAL; 347983c44251SDavid Ahern int addr_type; 34801da177e4SLinus Torvalds 3481557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3482d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3483d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3484557c44beSDavid Ahern goto out; 3485d5d531cbSDavid Ahern } 3486557c44beSDavid Ahern 34872ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 34882ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 34892ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 34902ea2352eSWei Wang goto out; 34912ea2352eSWei Wang } 34922ea2352eSWei Wang 3493e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3494e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3495e8478e80SDavid Ahern goto out; 3496e8478e80SDavid Ahern } 3497e8478e80SDavid Ahern 3498d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3499d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 35008c5b83f0SRoopa Prabhu goto out; 3501d5d531cbSDavid Ahern } 3502d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3503d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3504d5d531cbSDavid Ahern goto out; 3505d5d531cbSDavid Ahern } 35061da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3507d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3508d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3509d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 35108c5b83f0SRoopa Prabhu goto out; 3511d5d531cbSDavid Ahern } 35121da177e4SLinus Torvalds #endif 3513fc1e64e1SDavid Ahern 3514c71099acSThomas Graf err = -ENOBUFS; 351538308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3516d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3517d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 351838308473SDavid S. Miller if (!table) { 3519f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3520d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3521d71314b4SMatti Vaittinen } 3522d71314b4SMatti Vaittinen } else { 3523d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3524d71314b4SMatti Vaittinen } 352538308473SDavid S. Miller 352638308473SDavid S. Miller if (!table) 3527c71099acSThomas Graf goto out; 3528c71099acSThomas Graf 35291da177e4SLinus Torvalds err = -ENOMEM; 3530f88d8ea6SDavid Ahern rt = fib6_info_alloc(gfp_flags, !nh); 353193531c67SDavid Ahern if (!rt) 35321da177e4SLinus Torvalds goto out; 353393531c67SDavid Ahern 3534d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3535d7e774f3SDavid Ahern extack); 3536767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3537767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3538fda21d46SEric Dumazet /* Do not leave garbage there. */ 3539fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 3540767a2217SDavid Ahern goto out; 3541767a2217SDavid Ahern } 3542767a2217SDavid Ahern 354393531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 354493531c67SDavid Ahern rt->dst_nocount = true; 35451da177e4SLinus Torvalds 35461716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 354714895687SDavid Ahern fib6_set_expires(rt, jiffies + 35481716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 35491716a961SGao feng else 355014895687SDavid Ahern fib6_clean_expires(rt); 35511da177e4SLinus Torvalds 355286872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 355386872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 355493c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 355586872cb5SThomas Graf 355683c44251SDavid Ahern rt->fib6_table = table; 355783c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 355883c44251SDavid Ahern rt->fib6_type = cfg->fc_type; 35592b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 356019e42e45SRoopa Prabhu 356193c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 356293c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 356393c2fb25SDavid Ahern if (rt->fib6_dst.plen == 128) 35643b6761d1SDavid Ahern rt->dst_host = true; 35651da177e4SLinus Torvalds 35661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 356793c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 356893c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 35691da177e4SLinus Torvalds #endif 3570f88d8ea6SDavid Ahern if (nh) { 3571f88d8ea6SDavid Ahern if (!nexthop_get(nh)) { 3572f88d8ea6SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); 3573f88d8ea6SDavid Ahern goto out; 3574f88d8ea6SDavid Ahern } 3575f88d8ea6SDavid Ahern if (rt->fib6_src.plen) { 35764daa95afSColin Ian King NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); 3577f88d8ea6SDavid Ahern goto out; 3578f88d8ea6SDavid Ahern } 3579f88d8ea6SDavid Ahern rt->nh = nh; 3580f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(rt->nh); 3581f88d8ea6SDavid Ahern } else { 35821cf844c7SDavid Ahern err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); 35831da177e4SLinus Torvalds if (err) 35841da177e4SLinus Torvalds goto out; 35859fbb704cSDavid Ahern 3586f88d8ea6SDavid Ahern fib6_nh = rt->fib6_nh; 3587f88d8ea6SDavid Ahern 3588f88d8ea6SDavid Ahern /* We cannot add true routes via loopback here, they would 3589f88d8ea6SDavid Ahern * result in kernel looping; promote them to reject routes 359083c44251SDavid Ahern */ 359183c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3592f88d8ea6SDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, 3593f88d8ea6SDavid Ahern addr_type)) 359483c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3595f88d8ea6SDavid Ahern } 3596955ec4cbSDavid Ahern 3597c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3598f88d8ea6SDavid Ahern struct net_device *dev = fib6_nh->fib_nh_dev; 359983c44251SDavid Ahern 3600c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3601d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3602c3968a85SDaniel Walter err = -EINVAL; 3603c3968a85SDaniel Walter goto out; 3604c3968a85SDaniel Walter } 360593c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 360693c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3607c3968a85SDaniel Walter } else 360893c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3609c3968a85SDaniel Walter 36108c5b83f0SRoopa Prabhu return rt; 36111da177e4SLinus Torvalds out: 361293531c67SDavid Ahern fib6_info_release(rt); 36138c5b83f0SRoopa Prabhu return ERR_PTR(err); 36146b9ea5a6SRoopa Prabhu } 36156b9ea5a6SRoopa Prabhu 3616acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3617333c4301SDavid Ahern struct netlink_ext_ack *extack) 36186b9ea5a6SRoopa Prabhu { 36198d1c802bSDavid Ahern struct fib6_info *rt; 36206b9ea5a6SRoopa Prabhu int err; 36216b9ea5a6SRoopa Prabhu 3622acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3623d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3624d4ead6b3SDavid Ahern return PTR_ERR(rt); 36256b9ea5a6SRoopa Prabhu 3626d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 362793531c67SDavid Ahern fib6_info_release(rt); 36286b9ea5a6SRoopa Prabhu 36291da177e4SLinus Torvalds return err; 36301da177e4SLinus Torvalds } 36311da177e4SLinus Torvalds 36328d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 36331da177e4SLinus Torvalds { 3634afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3635c71099acSThomas Graf struct fib6_table *table; 3636afb1d4b5SDavid Ahern int err; 36371da177e4SLinus Torvalds 3638421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 36396825a26cSGao feng err = -ENOENT; 36406825a26cSGao feng goto out; 36416825a26cSGao feng } 36426c813a72SPatrick McHardy 364393c2fb25SDavid Ahern table = rt->fib6_table; 364466f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 364586872cb5SThomas Graf err = fib6_del(rt, info); 364666f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 36471da177e4SLinus Torvalds 36486825a26cSGao feng out: 364993531c67SDavid Ahern fib6_info_release(rt); 36501da177e4SLinus Torvalds return err; 36511da177e4SLinus Torvalds } 36521da177e4SLinus Torvalds 36538d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt) 3654e0a1ad73SThomas Graf { 3655afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net }; 3656afb1d4b5SDavid Ahern 3657528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3658e0a1ad73SThomas Graf } 3659e0a1ad73SThomas Graf 36608d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 36610ae81335SDavid Ahern { 36620ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3663e3330039SWANG Cong struct net *net = info->nl_net; 366416a16cd3SDavid Ahern struct sk_buff *skb = NULL; 36650ae81335SDavid Ahern struct fib6_table *table; 3666e3330039SWANG Cong int err = -ENOENT; 36670ae81335SDavid Ahern 3668421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3669e3330039SWANG Cong goto out_put; 367093c2fb25SDavid Ahern table = rt->fib6_table; 367166f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 36720ae81335SDavid Ahern 367393c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 36748d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 36750ae81335SDavid Ahern 367616a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 367716a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 367816a16cd3SDavid Ahern if (skb) { 367916a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 368016a16cd3SDavid Ahern 3681d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 368216a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 368316a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 368416a16cd3SDavid Ahern kfree_skb(skb); 368516a16cd3SDavid Ahern skb = NULL; 368616a16cd3SDavid Ahern } else 368716a16cd3SDavid Ahern info->skip_notify = 1; 368816a16cd3SDavid Ahern } 368916a16cd3SDavid Ahern 36900ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 369193c2fb25SDavid Ahern &rt->fib6_siblings, 369293c2fb25SDavid Ahern fib6_siblings) { 36930ae81335SDavid Ahern err = fib6_del(sibling, info); 36940ae81335SDavid Ahern if (err) 3695e3330039SWANG Cong goto out_unlock; 36960ae81335SDavid Ahern } 36970ae81335SDavid Ahern } 36980ae81335SDavid Ahern 36990ae81335SDavid Ahern err = fib6_del(rt, info); 3700e3330039SWANG Cong out_unlock: 370166f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3702e3330039SWANG Cong out_put: 370393531c67SDavid Ahern fib6_info_release(rt); 370416a16cd3SDavid Ahern 370516a16cd3SDavid Ahern if (skb) { 3706e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 370716a16cd3SDavid Ahern info->nlh, gfp_any()); 370816a16cd3SDavid Ahern } 37090ae81335SDavid Ahern return err; 37100ae81335SDavid Ahern } 37110ae81335SDavid Ahern 37120fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 371323fb93a4SDavid Ahern { 371423fb93a4SDavid Ahern int rc = -ESRCH; 371523fb93a4SDavid Ahern 371623fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 371723fb93a4SDavid Ahern goto out; 371823fb93a4SDavid Ahern 371923fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 372023fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 372123fb93a4SDavid Ahern goto out; 3722761f6026SXin Long 372323fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 372423fb93a4SDavid Ahern out: 372523fb93a4SDavid Ahern return rc; 372623fb93a4SDavid Ahern } 372723fb93a4SDavid Ahern 37280fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, 37290fa6efc5SDavid Ahern struct fib6_nh *nh) 37300fa6efc5SDavid Ahern { 37310fa6efc5SDavid Ahern struct fib6_result res = { 37320fa6efc5SDavid Ahern .f6i = rt, 37330fa6efc5SDavid Ahern .nh = nh, 37340fa6efc5SDavid Ahern }; 37350fa6efc5SDavid Ahern struct rt6_info *rt_cache; 37360fa6efc5SDavid Ahern 37370fa6efc5SDavid Ahern rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); 37380fa6efc5SDavid Ahern if (rt_cache) 37390fa6efc5SDavid Ahern return __ip6_del_cached_rt(rt_cache, cfg); 37400fa6efc5SDavid Ahern 37410fa6efc5SDavid Ahern return 0; 37420fa6efc5SDavid Ahern } 37430fa6efc5SDavid Ahern 3744333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3745333c4301SDavid Ahern struct netlink_ext_ack *extack) 37461da177e4SLinus Torvalds { 3747c71099acSThomas Graf struct fib6_table *table; 37488d1c802bSDavid Ahern struct fib6_info *rt; 37491da177e4SLinus Torvalds struct fib6_node *fn; 37501da177e4SLinus Torvalds int err = -ESRCH; 37511da177e4SLinus Torvalds 37525578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3753d5d531cbSDavid Ahern if (!table) { 3754d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3755c71099acSThomas Graf return err; 3756d5d531cbSDavid Ahern } 37571da177e4SLinus Torvalds 375866f5d6ceSWei Wang rcu_read_lock(); 3759c71099acSThomas Graf 3760c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 376186872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 376238fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 37632b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds if (fn) { 376666f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3767ad1601aeSDavid Ahern struct fib6_nh *nh; 3768ad1601aeSDavid Ahern 37691cf844c7SDavid Ahern nh = rt->fib6_nh; 37702b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 377123fb93a4SDavid Ahern int rc; 377223fb93a4SDavid Ahern 37730fa6efc5SDavid Ahern rc = ip6_del_cached_rt(cfg, rt, nh); 37749e575010SEric Dumazet if (rc != -ESRCH) { 37759e575010SEric Dumazet rcu_read_unlock(); 377623fb93a4SDavid Ahern return rc; 377723fb93a4SDavid Ahern } 37781f56a01fSMartin KaFai Lau continue; 37792b760fcfSWei Wang } 3780ad1601aeSDavid Ahern 378186872cb5SThomas Graf if (cfg->fc_ifindex && 3782ad1601aeSDavid Ahern (!nh->fib_nh_dev || 3783ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 37841da177e4SLinus Torvalds continue; 378586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 3786ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 37871da177e4SLinus Torvalds continue; 378893c2fb25SDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 37891da177e4SLinus Torvalds continue; 379093c2fb25SDavid Ahern if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) 3791c2ed1880SMantas M continue; 3792e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3793e873e4b9SWei Wang continue; 379466f5d6ceSWei Wang rcu_read_unlock(); 37951da177e4SLinus Torvalds 37960ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 37970ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 379886872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 37990ae81335SDavid Ahern 38000ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds } 380366f5d6ceSWei Wang rcu_read_unlock(); 38041da177e4SLinus Torvalds 38051da177e4SLinus Torvalds return err; 38061da177e4SLinus Torvalds } 38071da177e4SLinus Torvalds 38086700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3809a6279458SYOSHIFUJI Hideaki { 3810a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3811e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 381285bd05deSDavid Ahern struct fib6_result res = {}; 3813e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3814e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3815e8599ff4SDavid S. Miller struct neighbour *neigh; 381671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 38176e157b6aSDavid S. Miller int optlen, on_link; 38186e157b6aSDavid S. Miller u8 *lladdr; 3819e8599ff4SDavid S. Miller 382029a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 382171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3822e8599ff4SDavid S. Miller 3823e8599ff4SDavid S. Miller if (optlen < 0) { 38246e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3825e8599ff4SDavid S. Miller return; 3826e8599ff4SDavid S. Miller } 3827e8599ff4SDavid S. Miller 382871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3829e8599ff4SDavid S. Miller 383071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 38316e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3832e8599ff4SDavid S. Miller return; 3833e8599ff4SDavid S. Miller } 3834e8599ff4SDavid S. Miller 38356e157b6aSDavid S. Miller on_link = 0; 383671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3837e8599ff4SDavid S. Miller on_link = 1; 383871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3839e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 38406e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3841e8599ff4SDavid S. Miller return; 3842e8599ff4SDavid S. Miller } 3843e8599ff4SDavid S. Miller 3844e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3845e8599ff4SDavid S. Miller if (!in6_dev) 3846e8599ff4SDavid S. Miller return; 3847e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3848e8599ff4SDavid S. Miller return; 3849e8599ff4SDavid S. Miller 3850e8599ff4SDavid S. Miller /* RFC2461 8.1: 3851e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3852e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3853e8599ff4SDavid S. Miller */ 3854e8599ff4SDavid S. Miller 3855f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3856e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3857e8599ff4SDavid S. Miller return; 3858e8599ff4SDavid S. Miller } 38596e157b6aSDavid S. Miller 38606e157b6aSDavid S. Miller lladdr = NULL; 3861e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3862e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3863e8599ff4SDavid S. Miller skb->dev); 3864e8599ff4SDavid S. Miller if (!lladdr) { 3865e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3866e8599ff4SDavid S. Miller return; 3867e8599ff4SDavid S. Miller } 3868e8599ff4SDavid S. Miller } 3869e8599ff4SDavid S. Miller 38706e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3871ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 38726e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 38736e157b6aSDavid S. Miller return; 38746e157b6aSDavid S. Miller } 38756e157b6aSDavid S. Miller 38766e157b6aSDavid S. Miller /* Redirect received -> path was valid. 38776e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 38786e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 38796e157b6aSDavid S. Miller */ 38800dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 38816e157b6aSDavid S. Miller 388271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3883e8599ff4SDavid S. Miller if (!neigh) 3884e8599ff4SDavid S. Miller return; 3885e8599ff4SDavid S. Miller 38861da177e4SLinus Torvalds /* 38871da177e4SLinus Torvalds * We have finally decided to accept it. 38881da177e4SLinus Torvalds */ 38891da177e4SLinus Torvalds 3890f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 38911da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 38921da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 38931da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3894f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3895f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 38961da177e4SLinus Torvalds 38974d85cd0cSDavid Ahern rcu_read_lock(); 389885bd05deSDavid Ahern res.f6i = rcu_dereference(rt->from); 3899ff24e498SDavid S. Miller if (!res.f6i) 3900886b7a50SMartin KaFai Lau goto out; 39018a14e46fSDavid Ahern 39021cf844c7SDavid Ahern res.nh = res.f6i->fib6_nh; 39037d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 39047d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 390585bd05deSDavid Ahern nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 390638308473SDavid S. Miller if (!nrt) 39071da177e4SLinus Torvalds goto out; 39081da177e4SLinus Torvalds 39091da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 39101da177e4SLinus Torvalds if (on_link) 39111da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 39121da177e4SLinus Torvalds 39134e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 39141da177e4SLinus Torvalds 3915886b7a50SMartin KaFai Lau /* rt6_insert_exception() will take care of duplicated exceptions */ 39165012f0a5SDavid Ahern if (rt6_insert_exception(nrt, &res)) { 39172b760fcfSWei Wang dst_release_immediate(&nrt->dst); 39182b760fcfSWei Wang goto out; 39192b760fcfSWei Wang } 39201da177e4SLinus Torvalds 3921d8d1f30bSChangli Gao netevent.old = &rt->dst; 3922d8d1f30bSChangli Gao netevent.new = &nrt->dst; 392371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 392460592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 39258d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 39268d71740cSTom Tucker 39271da177e4SLinus Torvalds out: 3928886b7a50SMartin KaFai Lau rcu_read_unlock(); 3929e8599ff4SDavid S. Miller neigh_release(neigh); 39306e157b6aSDavid S. Miller } 39316e157b6aSDavid S. Miller 393270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 39338d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 3934b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3935830218c1SDavid Ahern const struct in6_addr *gwaddr, 3936830218c1SDavid Ahern struct net_device *dev) 393770ceb4f5SYOSHIFUJI Hideaki { 3938830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3939830218c1SDavid Ahern int ifindex = dev->ifindex; 394070ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 39418d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3942c71099acSThomas Graf struct fib6_table *table; 394370ceb4f5SYOSHIFUJI Hideaki 3944830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 394538308473SDavid S. Miller if (!table) 3946c71099acSThomas Graf return NULL; 3947c71099acSThomas Graf 394866f5d6ceSWei Wang rcu_read_lock(); 394938fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 395070ceb4f5SYOSHIFUJI Hideaki if (!fn) 395170ceb4f5SYOSHIFUJI Hideaki goto out; 395270ceb4f5SYOSHIFUJI Hideaki 395366f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3954f88d8ea6SDavid Ahern /* these routes do not use nexthops */ 3955f88d8ea6SDavid Ahern if (rt->nh) 3956f88d8ea6SDavid Ahern continue; 39571cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) 395870ceb4f5SYOSHIFUJI Hideaki continue; 39592b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 39601cf844c7SDavid Ahern !rt->fib6_nh->fib_nh_gw_family) 396170ceb4f5SYOSHIFUJI Hideaki continue; 39621cf844c7SDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) 396370ceb4f5SYOSHIFUJI Hideaki continue; 3964e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3965e873e4b9SWei Wang continue; 396670ceb4f5SYOSHIFUJI Hideaki break; 396770ceb4f5SYOSHIFUJI Hideaki } 396870ceb4f5SYOSHIFUJI Hideaki out: 396966f5d6ceSWei Wang rcu_read_unlock(); 397070ceb4f5SYOSHIFUJI Hideaki return rt; 397170ceb4f5SYOSHIFUJI Hideaki } 397270ceb4f5SYOSHIFUJI Hideaki 39738d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 3974b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3975830218c1SDavid Ahern const struct in6_addr *gwaddr, 3976830218c1SDavid Ahern struct net_device *dev, 397795c96174SEric Dumazet unsigned int pref) 397870ceb4f5SYOSHIFUJI Hideaki { 397986872cb5SThomas Graf struct fib6_config cfg = { 3980238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3981830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 398286872cb5SThomas Graf .fc_dst_len = prefixlen, 398386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 398486872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3985b91d5329SXin Long .fc_protocol = RTPROT_RA, 3986e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 398715e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3988efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3989efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 399086872cb5SThomas Graf }; 399170ceb4f5SYOSHIFUJI Hideaki 3992830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 39934e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 39944e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 399586872cb5SThomas Graf 3996e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3997e317da96SYOSHIFUJI Hideaki if (!prefixlen) 399886872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 399970ceb4f5SYOSHIFUJI Hideaki 4000acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 400170ceb4f5SYOSHIFUJI Hideaki 4002830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 400370ceb4f5SYOSHIFUJI Hideaki } 400470ceb4f5SYOSHIFUJI Hideaki #endif 400570ceb4f5SYOSHIFUJI Hideaki 40068d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 4007afb1d4b5SDavid Ahern const struct in6_addr *addr, 4008afb1d4b5SDavid Ahern struct net_device *dev) 40091da177e4SLinus Torvalds { 4010830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 40118d1c802bSDavid Ahern struct fib6_info *rt; 4012c71099acSThomas Graf struct fib6_table *table; 40131da177e4SLinus Torvalds 4014afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 401538308473SDavid S. Miller if (!table) 4016c71099acSThomas Graf return NULL; 40171da177e4SLinus Torvalds 401866f5d6ceSWei Wang rcu_read_lock(); 401966f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4020f88d8ea6SDavid Ahern struct fib6_nh *nh; 4021ad1601aeSDavid Ahern 4022f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4023f88d8ea6SDavid Ahern if (rt->nh) 4024f88d8ea6SDavid Ahern continue; 4025f88d8ea6SDavid Ahern 4026f88d8ea6SDavid Ahern nh = rt->fib6_nh; 4027ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 402893c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 4029ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 40301da177e4SLinus Torvalds break; 40311da177e4SLinus Torvalds } 4032e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 4033e873e4b9SWei Wang rt = NULL; 403466f5d6ceSWei Wang rcu_read_unlock(); 40351da177e4SLinus Torvalds return rt; 40361da177e4SLinus Torvalds } 40371da177e4SLinus Torvalds 40388d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 4039afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 4040ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 4041ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 40421da177e4SLinus Torvalds { 404386872cb5SThomas Graf struct fib6_config cfg = { 4044ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 4045238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 404686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 404786872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 404886872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 4049b91d5329SXin Long .fc_protocol = RTPROT_RA, 4050e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 405115e47304SEric W. Biederman .fc_nlinfo.portid = 0, 40525578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 4053afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 405486872cb5SThomas Graf }; 40551da177e4SLinus Torvalds 40564e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 40571da177e4SLinus Torvalds 4058acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 4059830218c1SDavid Ahern struct fib6_table *table; 4060830218c1SDavid Ahern 4061830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 4062830218c1SDavid Ahern if (table) 4063830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 4064830218c1SDavid Ahern } 40651da177e4SLinus Torvalds 4066afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds 4069afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 4070afb1d4b5SDavid Ahern struct fib6_table *table) 40711da177e4SLinus Torvalds { 40728d1c802bSDavid Ahern struct fib6_info *rt; 40731da177e4SLinus Torvalds 40741da177e4SLinus Torvalds restart: 407566f5d6ceSWei Wang rcu_read_lock(); 407666f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 4077dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 4078dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 4079dcd1f572SDavid Ahern 408093c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 4081e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 4082e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 408366f5d6ceSWei Wang rcu_read_unlock(); 4084afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 40851da177e4SLinus Torvalds goto restart; 40861da177e4SLinus Torvalds } 40871da177e4SLinus Torvalds } 408866f5d6ceSWei Wang rcu_read_unlock(); 4089830218c1SDavid Ahern 4090830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 4091830218c1SDavid Ahern } 4092830218c1SDavid Ahern 4093830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 4094830218c1SDavid Ahern { 4095830218c1SDavid Ahern struct fib6_table *table; 4096830218c1SDavid Ahern struct hlist_head *head; 4097830218c1SDavid Ahern unsigned int h; 4098830218c1SDavid Ahern 4099830218c1SDavid Ahern rcu_read_lock(); 4100830218c1SDavid Ahern 4101830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 4102830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 4103830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 4104830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 4105afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 4106830218c1SDavid Ahern } 4107830218c1SDavid Ahern } 4108830218c1SDavid Ahern 4109830218c1SDavid Ahern rcu_read_unlock(); 41101da177e4SLinus Torvalds } 41111da177e4SLinus Torvalds 41125578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 41135578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 411486872cb5SThomas Graf struct fib6_config *cfg) 411586872cb5SThomas Graf { 41168823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 41178823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 41188823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 41198823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 412067f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 41218823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 41228823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 41238823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 41248823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 41258823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 412686872cb5SThomas Graf 41278823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 412886872cb5SThomas Graf 41298823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 41308823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 41318823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 41328823a3acSMaciej Żenczykowski }; 413386872cb5SThomas Graf } 413486872cb5SThomas Graf 41355578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 41361da177e4SLinus Torvalds { 413786872cb5SThomas Graf struct fib6_config cfg; 41381da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 41391da177e4SLinus Torvalds int err; 41401da177e4SLinus Torvalds 41411da177e4SLinus Torvalds switch (cmd) { 41421da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 41431da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 4144af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 41451da177e4SLinus Torvalds return -EPERM; 41461da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 41471da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 41481da177e4SLinus Torvalds if (err) 41491da177e4SLinus Torvalds return -EFAULT; 41501da177e4SLinus Torvalds 41515578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 415286872cb5SThomas Graf 41531da177e4SLinus Torvalds rtnl_lock(); 41541da177e4SLinus Torvalds switch (cmd) { 41551da177e4SLinus Torvalds case SIOCADDRT: 4156acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 41571da177e4SLinus Torvalds break; 41581da177e4SLinus Torvalds case SIOCDELRT: 4159333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 41601da177e4SLinus Torvalds break; 41611da177e4SLinus Torvalds default: 41621da177e4SLinus Torvalds err = -EINVAL; 41631da177e4SLinus Torvalds } 41641da177e4SLinus Torvalds rtnl_unlock(); 41651da177e4SLinus Torvalds 41661da177e4SLinus Torvalds return err; 41673ff50b79SStephen Hemminger } 41681da177e4SLinus Torvalds 41691da177e4SLinus Torvalds return -EINVAL; 41701da177e4SLinus Torvalds } 41711da177e4SLinus Torvalds 41721da177e4SLinus Torvalds /* 41731da177e4SLinus Torvalds * Drop the packet on the floor 41741da177e4SLinus Torvalds */ 41751da177e4SLinus Torvalds 4176d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 41771da177e4SLinus Torvalds { 4178adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 41791d3fd8a1SStephen Suryaputra struct net *net = dev_net(dst->dev); 41801d3fd8a1SStephen Suryaputra struct inet6_dev *idev; 41811d3fd8a1SStephen Suryaputra int type; 41821d3fd8a1SStephen Suryaputra 41831d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev) && 41841d3fd8a1SStephen Suryaputra dst->dev == net->loopback_dev) 41851d3fd8a1SStephen Suryaputra idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 41861d3fd8a1SStephen Suryaputra else 41871d3fd8a1SStephen Suryaputra idev = ip6_dst_idev(dst); 41881d3fd8a1SStephen Suryaputra 4189612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 4190612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 41910660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 419245bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 41931d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 4194612f09e8SYOSHIFUJI Hideaki break; 4195612f09e8SYOSHIFUJI Hideaki } 4196612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 4197612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 41981d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 4199612f09e8SYOSHIFUJI Hideaki break; 4200612f09e8SYOSHIFUJI Hideaki } 42011d3fd8a1SStephen Suryaputra 42021d3fd8a1SStephen Suryaputra /* Start over by dropping the dst for l3mdev case */ 42031d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev)) 42041d3fd8a1SStephen Suryaputra skb_dst_drop(skb); 42051d3fd8a1SStephen Suryaputra 42063ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 42071da177e4SLinus Torvalds kfree_skb(skb); 42081da177e4SLinus Torvalds return 0; 42091da177e4SLinus Torvalds } 42101da177e4SLinus Torvalds 42119ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 42129ce8ade0SThomas Graf { 4213612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 42149ce8ade0SThomas Graf } 42159ce8ade0SThomas Graf 4216ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 42171da177e4SLinus Torvalds { 4218adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4219612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 42201da177e4SLinus Torvalds } 42211da177e4SLinus Torvalds 42229ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 42239ce8ade0SThomas Graf { 4224612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 42259ce8ade0SThomas Graf } 42269ce8ade0SThomas Graf 4227ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 42289ce8ade0SThomas Graf { 4229adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 4230612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 42319ce8ade0SThomas Graf } 42329ce8ade0SThomas Graf 42331da177e4SLinus Torvalds /* 42341da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 42351da177e4SLinus Torvalds */ 42361da177e4SLinus Torvalds 4237360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 4238afb1d4b5SDavid Ahern struct inet6_dev *idev, 42391da177e4SLinus Torvalds const struct in6_addr *addr, 4240acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 42411da177e4SLinus Torvalds { 4242c7a1ce39SDavid Ahern struct fib6_config cfg = { 4243c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 4244c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 4245c7a1ce39SDavid Ahern .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, 4246c7a1ce39SDavid Ahern .fc_dst = *addr, 4247c7a1ce39SDavid Ahern .fc_dst_len = 128, 4248c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 4249c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 4250c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 4251c7a1ce39SDavid Ahern }; 42525f02ce24SDavid Ahern 4253e8478e80SDavid Ahern if (anycast) { 4254c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 4255c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 4256e8478e80SDavid Ahern } else { 4257c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 4258c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 4259e8478e80SDavid Ahern } 42601da177e4SLinus Torvalds 4261c7a1ce39SDavid Ahern return ip6_route_info_create(&cfg, gfp_flags, NULL); 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 4264c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 4265c3968a85SDaniel Walter struct arg_dev_net_ip { 4266c3968a85SDaniel Walter struct net_device *dev; 4267c3968a85SDaniel Walter struct net *net; 4268c3968a85SDaniel Walter struct in6_addr *addr; 4269c3968a85SDaniel Walter }; 4270c3968a85SDaniel Walter 42718d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 4272c3968a85SDaniel Walter { 4273c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 4274c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 4275c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 4276c3968a85SDaniel Walter 4277f88d8ea6SDavid Ahern if (!rt->nh && 4278f88d8ea6SDavid Ahern ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && 4279421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 428093c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 428160006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 4282c3968a85SDaniel Walter /* remove prefsrc entry */ 428393c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 428460006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 4285c3968a85SDaniel Walter } 4286c3968a85SDaniel Walter return 0; 4287c3968a85SDaniel Walter } 4288c3968a85SDaniel Walter 4289c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 4290c3968a85SDaniel Walter { 4291c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 4292c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 4293c3968a85SDaniel Walter .dev = ifp->idev->dev, 4294c3968a85SDaniel Walter .net = net, 4295c3968a85SDaniel Walter .addr = &ifp->addr, 4296c3968a85SDaniel Walter }; 42970c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 4298c3968a85SDaniel Walter } 4299c3968a85SDaniel Walter 43002b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 4301be7a010dSDuan Jiong 4302be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 43038d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 4304be7a010dSDuan Jiong { 4305be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 4306f88d8ea6SDavid Ahern struct fib6_nh *nh; 4307be7a010dSDuan Jiong 4308f88d8ea6SDavid Ahern /* RA routes do not use nexthops */ 4309f88d8ea6SDavid Ahern if (rt->nh) 4310f88d8ea6SDavid Ahern return 0; 4311f88d8ea6SDavid Ahern 4312f88d8ea6SDavid Ahern nh = rt->fib6_nh; 431393c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 4314cc5c073aSDavid Ahern nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) 4315be7a010dSDuan Jiong return -1; 4316b16cb459SWei Wang 4317b16cb459SWei Wang /* Further clean up cached routes in exception table. 4318b16cb459SWei Wang * This is needed because cached route may have a different 4319b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 4320b16cb459SWei Wang */ 4321cc5c073aSDavid Ahern fib6_nh_exceptions_clean_tohost(nh, gateway); 4322b16cb459SWei Wang 4323be7a010dSDuan Jiong return 0; 4324be7a010dSDuan Jiong } 4325be7a010dSDuan Jiong 4326be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 4327be7a010dSDuan Jiong { 4328be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 4329be7a010dSDuan Jiong } 4330be7a010dSDuan Jiong 43312127d95aSIdo Schimmel struct arg_netdev_event { 43322127d95aSIdo Schimmel const struct net_device *dev; 43334c981e28SIdo Schimmel union { 4334ecc5663cSDavid Ahern unsigned char nh_flags; 43354c981e28SIdo Schimmel unsigned long event; 43364c981e28SIdo Schimmel }; 43372127d95aSIdo Schimmel }; 43382127d95aSIdo Schimmel 43398d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 4340d7dedee1SIdo Schimmel { 43418d1c802bSDavid Ahern struct fib6_info *iter; 4342d7dedee1SIdo Schimmel struct fib6_node *fn; 4343d7dedee1SIdo Schimmel 434493c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 434593c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4346d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 434793c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4348d7dedee1SIdo Schimmel while (iter) { 434993c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 435033bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 4351d7dedee1SIdo Schimmel return iter; 43528fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 435393c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 4354d7dedee1SIdo Schimmel } 4355d7dedee1SIdo Schimmel 4356d7dedee1SIdo Schimmel return NULL; 4357d7dedee1SIdo Schimmel } 4358d7dedee1SIdo Schimmel 4359f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */ 43608d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 4361d7dedee1SIdo Schimmel { 43621cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || 43631cf844c7SDavid Ahern (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && 43641cf844c7SDavid Ahern ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) 4365d7dedee1SIdo Schimmel return true; 4366d7dedee1SIdo Schimmel 4367d7dedee1SIdo Schimmel return false; 4368d7dedee1SIdo Schimmel } 4369d7dedee1SIdo Schimmel 43708d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 4371d7dedee1SIdo Schimmel { 43728d1c802bSDavid Ahern struct fib6_info *iter; 4373d7dedee1SIdo Schimmel int total = 0; 4374d7dedee1SIdo Schimmel 4375d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 43761cf844c7SDavid Ahern total += rt->fib6_nh->fib_nh_weight; 4377d7dedee1SIdo Schimmel 437893c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4379d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 43801cf844c7SDavid Ahern total += iter->fib6_nh->fib_nh_weight; 4381d7dedee1SIdo Schimmel } 4382d7dedee1SIdo Schimmel 4383d7dedee1SIdo Schimmel return total; 4384d7dedee1SIdo Schimmel } 4385d7dedee1SIdo Schimmel 43868d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4387d7dedee1SIdo Schimmel { 4388d7dedee1SIdo Schimmel int upper_bound = -1; 4389d7dedee1SIdo Schimmel 4390d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 43911cf844c7SDavid Ahern *weight += rt->fib6_nh->fib_nh_weight; 4392d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4393d7dedee1SIdo Schimmel total) - 1; 4394d7dedee1SIdo Schimmel } 43951cf844c7SDavid Ahern atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); 4396d7dedee1SIdo Schimmel } 4397d7dedee1SIdo Schimmel 43988d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4399d7dedee1SIdo Schimmel { 44008d1c802bSDavid Ahern struct fib6_info *iter; 4401d7dedee1SIdo Schimmel int weight = 0; 4402d7dedee1SIdo Schimmel 4403d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 4404d7dedee1SIdo Schimmel 440593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4406d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 4407d7dedee1SIdo Schimmel } 4408d7dedee1SIdo Schimmel 44098d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 4410d7dedee1SIdo Schimmel { 44118d1c802bSDavid Ahern struct fib6_info *first; 4412d7dedee1SIdo Schimmel int total; 4413d7dedee1SIdo Schimmel 4414d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 4415d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 4416d7dedee1SIdo Schimmel * sibling route. 4417d7dedee1SIdo Schimmel */ 441893c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 4419d7dedee1SIdo Schimmel return; 4420d7dedee1SIdo Schimmel 4421d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 4422d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 4423d7dedee1SIdo Schimmel * onwards. 4424d7dedee1SIdo Schimmel */ 4425d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 4426d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 4427d7dedee1SIdo Schimmel return; 4428d7dedee1SIdo Schimmel 4429d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 4430d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 4431d7dedee1SIdo Schimmel } 4432d7dedee1SIdo Schimmel 44338d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 44342127d95aSIdo Schimmel { 44352127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 44367aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 44372127d95aSIdo Schimmel 4438f88d8ea6SDavid Ahern if (rt != net->ipv6.fib6_null_entry && !rt->nh && 44391cf844c7SDavid Ahern rt->fib6_nh->fib_nh_dev == arg->dev) { 44401cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; 44417aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 4442d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 44431de178edSIdo Schimmel } 44442127d95aSIdo Schimmel 44452127d95aSIdo Schimmel return 0; 44462127d95aSIdo Schimmel } 44472127d95aSIdo Schimmel 4448ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 44492127d95aSIdo Schimmel { 44502127d95aSIdo Schimmel struct arg_netdev_event arg = { 44512127d95aSIdo Schimmel .dev = dev, 44526802f3adSIdo Schimmel { 44532127d95aSIdo Schimmel .nh_flags = nh_flags, 44546802f3adSIdo Schimmel }, 44552127d95aSIdo Schimmel }; 44562127d95aSIdo Schimmel 44572127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 44582127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 44592127d95aSIdo Schimmel 44602127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 44612127d95aSIdo Schimmel } 44622127d95aSIdo Schimmel 4463f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */ 44648d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 44651de178edSIdo Schimmel const struct net_device *dev) 44661de178edSIdo Schimmel { 44678d1c802bSDavid Ahern struct fib6_info *iter; 44681de178edSIdo Schimmel 44691cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 44701de178edSIdo Schimmel return true; 447193c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 44721cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 44731de178edSIdo Schimmel return true; 44741de178edSIdo Schimmel 44751de178edSIdo Schimmel return false; 44761de178edSIdo Schimmel } 44771de178edSIdo Schimmel 44788d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 44791de178edSIdo Schimmel { 44808d1c802bSDavid Ahern struct fib6_info *iter; 44811de178edSIdo Schimmel 44821de178edSIdo Schimmel rt->should_flush = 1; 448393c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 44841de178edSIdo Schimmel iter->should_flush = 1; 44851de178edSIdo Schimmel } 44861de178edSIdo Schimmel 44878d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 44881de178edSIdo Schimmel const struct net_device *down_dev) 44891de178edSIdo Schimmel { 44908d1c802bSDavid Ahern struct fib6_info *iter; 44911de178edSIdo Schimmel unsigned int dead = 0; 44921de178edSIdo Schimmel 44931cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == down_dev || 44941cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 44951de178edSIdo Schimmel dead++; 449693c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 44971cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == down_dev || 44981cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) 44991de178edSIdo Schimmel dead++; 45001de178edSIdo Schimmel 45011de178edSIdo Schimmel return dead; 45021de178edSIdo Schimmel } 45031de178edSIdo Schimmel 45048d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 45051de178edSIdo Schimmel const struct net_device *dev, 4506ecc5663cSDavid Ahern unsigned char nh_flags) 45071de178edSIdo Schimmel { 45088d1c802bSDavid Ahern struct fib6_info *iter; 45091de178edSIdo Schimmel 45101cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev == dev) 45111cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= nh_flags; 451293c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 45131cf844c7SDavid Ahern if (iter->fib6_nh->fib_nh_dev == dev) 45141cf844c7SDavid Ahern iter->fib6_nh->fib_nh_flags |= nh_flags; 45151de178edSIdo Schimmel } 45161de178edSIdo Schimmel 4517a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 45188d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 45191da177e4SLinus Torvalds { 45204c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 45214c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 45227aef6859SDavid Ahern struct net *net = dev_net(dev); 45238ed67789SDaniel Lezcano 4524f88d8ea6SDavid Ahern if (rt == net->ipv6.fib6_null_entry || rt->nh) 452527c6fa73SIdo Schimmel return 0; 452627c6fa73SIdo Schimmel 452727c6fa73SIdo Schimmel switch (arg->event) { 452827c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 45291cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 453027c6fa73SIdo Schimmel case NETDEV_DOWN: 45311de178edSIdo Schimmel if (rt->should_flush) 453227c6fa73SIdo Schimmel return -1; 453393c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 45341cf844c7SDavid Ahern return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; 45351de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 45361de178edSIdo Schimmel unsigned int count; 45371de178edSIdo Schimmel 45381de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 453993c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 45401de178edSIdo Schimmel rt6_multipath_flush(rt); 45411de178edSIdo Schimmel return -1; 45421de178edSIdo Schimmel } 45431de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 45441de178edSIdo Schimmel RTNH_F_LINKDOWN); 45457aef6859SDavid Ahern fib6_update_sernum(net, rt); 4546d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 45471de178edSIdo Schimmel } 45481de178edSIdo Schimmel return -2; 454927c6fa73SIdo Schimmel case NETDEV_CHANGE: 45501cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev != dev || 455193c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 455227c6fa73SIdo Schimmel break; 45531cf844c7SDavid Ahern rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 4554d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 455527c6fa73SIdo Schimmel break; 45562b241361SIdo Schimmel } 4557c159d30cSDavid S. Miller 45581da177e4SLinus Torvalds return 0; 45591da177e4SLinus Torvalds } 45601da177e4SLinus Torvalds 456127c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 45621da177e4SLinus Torvalds { 45634c981e28SIdo Schimmel struct arg_netdev_event arg = { 45648ed67789SDaniel Lezcano .dev = dev, 45656802f3adSIdo Schimmel { 45664c981e28SIdo Schimmel .event = event, 45676802f3adSIdo Schimmel }, 45688ed67789SDaniel Lezcano }; 45697c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 45708ed67789SDaniel Lezcano 45717c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 45727c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 45737c6bb7d2SDavid Ahern else 45747c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 45754c981e28SIdo Schimmel } 45764c981e28SIdo Schimmel 45774c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 45784c981e28SIdo Schimmel { 45794c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 45804c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 45814c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 45821da177e4SLinus Torvalds } 45831da177e4SLinus Torvalds 458495c96174SEric Dumazet struct rt6_mtu_change_arg { 45851da177e4SLinus Torvalds struct net_device *dev; 458695c96174SEric Dumazet unsigned int mtu; 4587c0b220cfSDavid Ahern struct fib6_info *f6i; 45881da177e4SLinus Torvalds }; 45891da177e4SLinus Torvalds 4590cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) 4591c0b220cfSDavid Ahern { 4592c0b220cfSDavid Ahern struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; 4593cc5c073aSDavid Ahern struct fib6_info *f6i = arg->f6i; 4594c0b220cfSDavid Ahern 4595c0b220cfSDavid Ahern /* For administrative MTU increase, there is no way to discover 4596c0b220cfSDavid Ahern * IPv6 PMTU increase, so PMTU increase should be updated here. 4597c0b220cfSDavid Ahern * Since RFC 1981 doesn't include administrative MTU increase 4598c0b220cfSDavid Ahern * update PMTU increase is a MUST. (i.e. jumbo frame) 4599c0b220cfSDavid Ahern */ 4600c0b220cfSDavid Ahern if (nh->fib_nh_dev == arg->dev) { 4601c0b220cfSDavid Ahern struct inet6_dev *idev = __in6_dev_get(arg->dev); 4602c0b220cfSDavid Ahern u32 mtu = f6i->fib6_pmtu; 4603c0b220cfSDavid Ahern 4604c0b220cfSDavid Ahern if (mtu >= arg->mtu || 4605c0b220cfSDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4606c0b220cfSDavid Ahern fib6_metric_set(f6i, RTAX_MTU, arg->mtu); 4607c0b220cfSDavid Ahern 4608c0b220cfSDavid Ahern spin_lock_bh(&rt6_exception_lock); 4609cc5c073aSDavid Ahern rt6_exceptions_update_pmtu(idev, nh, arg->mtu); 4610c0b220cfSDavid Ahern spin_unlock_bh(&rt6_exception_lock); 4611c0b220cfSDavid Ahern } 4612c0b220cfSDavid Ahern 4613c0b220cfSDavid Ahern return 0; 4614c0b220cfSDavid Ahern } 4615c0b220cfSDavid Ahern 4616c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) 46171da177e4SLinus Torvalds { 46181da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 46191da177e4SLinus Torvalds struct inet6_dev *idev; 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 46221da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 46231da177e4SLinus Torvalds We still use this lock to block changes 46241da177e4SLinus Torvalds caused by addrconf/ndisc. 46251da177e4SLinus Torvalds */ 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 462838308473SDavid S. Miller if (!idev) 46291da177e4SLinus Torvalds return 0; 46301da177e4SLinus Torvalds 4631c0b220cfSDavid Ahern if (fib6_metric_locked(f6i, RTAX_MTU)) 46321da177e4SLinus Torvalds return 0; 4633c0b220cfSDavid Ahern 4634c0b220cfSDavid Ahern arg->f6i = f6i; 46351cf844c7SDavid Ahern return fib6_nh_mtu_change(f6i->fib6_nh, arg); 46361da177e4SLinus Torvalds } 46371da177e4SLinus Torvalds 463895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 46391da177e4SLinus Torvalds { 4640c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4641c71099acSThomas Graf .dev = dev, 4642c71099acSThomas Graf .mtu = mtu, 4643c71099acSThomas Graf }; 46441da177e4SLinus Torvalds 46450c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 46461da177e4SLinus Torvalds } 46471da177e4SLinus Torvalds 4648ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 464975425657SDavid Ahern [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 46505176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4651aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 465286872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4653ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 465486872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 465586872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 465651ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4657c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 465819e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 465919e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 466032bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4661622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 46623b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4663aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4664eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4665eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4666eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 466786872cb5SThomas Graf }; 466886872cb5SThomas Graf 466986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4670333c4301SDavid Ahern struct fib6_config *cfg, 4671333c4301SDavid Ahern struct netlink_ext_ack *extack) 46721da177e4SLinus Torvalds { 467386872cb5SThomas Graf struct rtmsg *rtm; 467486872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4675c78ba6d6SLubomir Rintel unsigned int pref; 467686872cb5SThomas Graf int err; 46771da177e4SLinus Torvalds 46788cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 46798cb08174SJohannes Berg rtm_ipv6_policy, extack); 468086872cb5SThomas Graf if (err < 0) 468186872cb5SThomas Graf goto errout; 46821da177e4SLinus Torvalds 468386872cb5SThomas Graf err = -EINVAL; 468486872cb5SThomas Graf rtm = nlmsg_data(nlh); 468586872cb5SThomas Graf 468684db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 468784db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 468884db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 468984db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 469084db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 469184db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 469284db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 469384db8407SMaciej Żenczykowski 469484db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 469584db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 469684db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 469784db8407SMaciej Żenczykowski }; 469886872cb5SThomas Graf 4699ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4700ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4701b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4702b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 470386872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 470486872cb5SThomas Graf 4705ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4706ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4707ab79ad14SMaciej Żenczykowski 47081f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 47091f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 47101f56a01fSMartin KaFai Lau 4711fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4712fc1e64e1SDavid Ahern 471386872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 471467b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 471586872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 47161da177e4SLinus Torvalds } 4717e3818541SDavid Ahern if (tb[RTA_VIA]) { 4718e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 4719e3818541SDavid Ahern goto errout; 4720e3818541SDavid Ahern } 472186872cb5SThomas Graf 472286872cb5SThomas Graf if (tb[RTA_DST]) { 472386872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 472486872cb5SThomas Graf 472586872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 472686872cb5SThomas Graf goto errout; 472786872cb5SThomas Graf 472886872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 47291da177e4SLinus Torvalds } 473086872cb5SThomas Graf 473186872cb5SThomas Graf if (tb[RTA_SRC]) { 473286872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 473386872cb5SThomas Graf 473486872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 473586872cb5SThomas Graf goto errout; 473686872cb5SThomas Graf 473786872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 47381da177e4SLinus Torvalds } 473986872cb5SThomas Graf 4740c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 474167b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4742c3968a85SDaniel Walter 474386872cb5SThomas Graf if (tb[RTA_OIF]) 474486872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 474586872cb5SThomas Graf 474686872cb5SThomas Graf if (tb[RTA_PRIORITY]) 474786872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 474886872cb5SThomas Graf 474986872cb5SThomas Graf if (tb[RTA_METRICS]) { 475086872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 475186872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 47521da177e4SLinus Torvalds } 475386872cb5SThomas Graf 475486872cb5SThomas Graf if (tb[RTA_TABLE]) 475586872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 475686872cb5SThomas Graf 475751ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 475851ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 475951ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 47609ed59592SDavid Ahern 47619ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4762c255bd68SDavid Ahern cfg->fc_mp_len, extack); 47639ed59592SDavid Ahern if (err < 0) 47649ed59592SDavid Ahern goto errout; 476551ebd318SNicolas Dichtel } 476651ebd318SNicolas Dichtel 4767c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4768c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4769c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4770c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4771c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4772c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4773c78ba6d6SLubomir Rintel } 4774c78ba6d6SLubomir Rintel 477519e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 477619e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 477719e42e45SRoopa Prabhu 47789ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 477919e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 478019e42e45SRoopa Prabhu 4781c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 47829ed59592SDavid Ahern if (err < 0) 47839ed59592SDavid Ahern goto errout; 47849ed59592SDavid Ahern } 47859ed59592SDavid Ahern 478632bc201eSXin Long if (tb[RTA_EXPIRES]) { 478732bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 478832bc201eSXin Long 478932bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 479032bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 479132bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 479232bc201eSXin Long } 479332bc201eSXin Long } 479432bc201eSXin Long 479586872cb5SThomas Graf err = 0; 479686872cb5SThomas Graf errout: 479786872cb5SThomas Graf return err; 47981da177e4SLinus Torvalds } 47991da177e4SLinus Torvalds 48006b9ea5a6SRoopa Prabhu struct rt6_nh { 48018d1c802bSDavid Ahern struct fib6_info *fib6_info; 48026b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 48036b9ea5a6SRoopa Prabhu struct list_head next; 48046b9ea5a6SRoopa Prabhu }; 48056b9ea5a6SRoopa Prabhu 4806d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 4807d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 48088d1c802bSDavid Ahern struct fib6_info *rt, 48098d1c802bSDavid Ahern struct fib6_config *r_cfg) 48106b9ea5a6SRoopa Prabhu { 48116b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 48126b9ea5a6SRoopa Prabhu int err = -EEXIST; 48136b9ea5a6SRoopa Prabhu 48146b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 48158d1c802bSDavid Ahern /* check if fib6_info already exists */ 48168d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 48176b9ea5a6SRoopa Prabhu return err; 48186b9ea5a6SRoopa Prabhu } 48196b9ea5a6SRoopa Prabhu 48206b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 48216b9ea5a6SRoopa Prabhu if (!nh) 48226b9ea5a6SRoopa Prabhu return -ENOMEM; 48238d1c802bSDavid Ahern nh->fib6_info = rt; 48246b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 48256b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 48266b9ea5a6SRoopa Prabhu 48276b9ea5a6SRoopa Prabhu return 0; 48286b9ea5a6SRoopa Prabhu } 48296b9ea5a6SRoopa Prabhu 48308d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 48318d1c802bSDavid Ahern struct fib6_info *rt_last, 48323b1137feSDavid Ahern struct nl_info *info, 48333b1137feSDavid Ahern __u16 nlflags) 48343b1137feSDavid Ahern { 48353b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 48363b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 48373b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 48383b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 48393b1137feSDavid Ahern * the list, find the first sibling of the last route appended 48403b1137feSDavid Ahern */ 484193c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 484293c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 48438d1c802bSDavid Ahern struct fib6_info, 484493c2fb25SDavid Ahern fib6_siblings); 48453b1137feSDavid Ahern } 48463b1137feSDavid Ahern 48473b1137feSDavid Ahern if (rt) 48483b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 48493b1137feSDavid Ahern } 48503b1137feSDavid Ahern 4851333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4852333c4301SDavid Ahern struct netlink_ext_ack *extack) 485351ebd318SNicolas Dichtel { 48548d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 48553b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 485651ebd318SNicolas Dichtel struct fib6_config r_cfg; 485751ebd318SNicolas Dichtel struct rtnexthop *rtnh; 48588d1c802bSDavid Ahern struct fib6_info *rt; 48596b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 48606b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 48613b1137feSDavid Ahern __u16 nlflags; 486251ebd318SNicolas Dichtel int remaining; 486351ebd318SNicolas Dichtel int attrlen; 48646b9ea5a6SRoopa Prabhu int err = 1; 48656b9ea5a6SRoopa Prabhu int nhn = 0; 48666b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 48676b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 48686b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 486951ebd318SNicolas Dichtel 48703b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 48713b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 48723b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 48733b1137feSDavid Ahern 487435f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 487551ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 487651ebd318SNicolas Dichtel 48776b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 48788d1c802bSDavid Ahern * fib6_info structs per nexthop 48796b9ea5a6SRoopa Prabhu */ 488051ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 488151ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 488251ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 488351ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 488451ebd318SNicolas Dichtel 488551ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 488651ebd318SNicolas Dichtel if (attrlen > 0) { 488751ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 488851ebd318SNicolas Dichtel 488951ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 489051ebd318SNicolas Dichtel if (nla) { 489167b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 489251ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 489351ebd318SNicolas Dichtel } 489419e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 489519e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 489619e42e45SRoopa Prabhu if (nla) 489719e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 489851ebd318SNicolas Dichtel } 48996b9ea5a6SRoopa Prabhu 490068e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 4901acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 49028c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 49038c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 49048c5b83f0SRoopa Prabhu rt = NULL; 49056b9ea5a6SRoopa Prabhu goto cleanup; 49068c5b83f0SRoopa Prabhu } 4907b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 4908b5d2d75eSDavid Ahern err = -EINVAL; 4909b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 4910b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 4911b5d2d75eSDavid Ahern fib6_info_release(rt); 4912b5d2d75eSDavid Ahern goto cleanup; 4913b5d2d75eSDavid Ahern } 49146b9ea5a6SRoopa Prabhu 49151cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; 4916398958aeSIdo Schimmel 4917d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 4918d4ead6b3SDavid Ahern rt, &r_cfg); 491951ebd318SNicolas Dichtel if (err) { 492093531c67SDavid Ahern fib6_info_release(rt); 49216b9ea5a6SRoopa Prabhu goto cleanup; 492251ebd318SNicolas Dichtel } 49236b9ea5a6SRoopa Prabhu 49246b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 492551ebd318SNicolas Dichtel } 49266b9ea5a6SRoopa Prabhu 49273b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 49283b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 49293b1137feSDavid Ahern * the full route when done 49303b1137feSDavid Ahern */ 49313b1137feSDavid Ahern info->skip_notify = 1; 49323b1137feSDavid Ahern 49336b9ea5a6SRoopa Prabhu err_nh = NULL; 49346b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 49358d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 49368d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 49373b1137feSDavid Ahern 4938f7225172SDavid Ahern if (!err) { 4939f7225172SDavid Ahern /* save reference to last route successfully inserted */ 4940f7225172SDavid Ahern rt_last = nh->fib6_info; 4941f7225172SDavid Ahern 49426b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 4943f7225172SDavid Ahern if (!rt_notif) 49448d1c802bSDavid Ahern rt_notif = nh->fib6_info; 4945f7225172SDavid Ahern } 49466b9ea5a6SRoopa Prabhu 49478d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 49488d1c802bSDavid Ahern nh->fib6_info = NULL; 49496b9ea5a6SRoopa Prabhu if (err) { 49506b9ea5a6SRoopa Prabhu if (replace && nhn) 4951a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 4952a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 49536b9ea5a6SRoopa Prabhu err_nh = nh; 49546b9ea5a6SRoopa Prabhu goto add_errout; 49556b9ea5a6SRoopa Prabhu } 49566b9ea5a6SRoopa Prabhu 49571a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 495827596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 495927596472SMichal Kubeček * we have already failed to add the first nexthop: 496027596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 496127596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 496227596472SMichal Kubeček * be added to it. 49631a72418bSNicolas Dichtel */ 496427596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 496527596472SMichal Kubeček NLM_F_REPLACE); 49666b9ea5a6SRoopa Prabhu nhn++; 49676b9ea5a6SRoopa Prabhu } 49686b9ea5a6SRoopa Prabhu 49693b1137feSDavid Ahern /* success ... tell user about new route */ 49703b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 49716b9ea5a6SRoopa Prabhu goto cleanup; 49726b9ea5a6SRoopa Prabhu 49736b9ea5a6SRoopa Prabhu add_errout: 49743b1137feSDavid Ahern /* send notification for routes that were added so that 49753b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 49763b1137feSDavid Ahern * coherent 49773b1137feSDavid Ahern */ 49783b1137feSDavid Ahern if (rt_notif) 49793b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 49803b1137feSDavid Ahern 49816b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 49826b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 49836b9ea5a6SRoopa Prabhu if (err_nh == nh) 49846b9ea5a6SRoopa Prabhu break; 4985333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 49866b9ea5a6SRoopa Prabhu } 49876b9ea5a6SRoopa Prabhu 49886b9ea5a6SRoopa Prabhu cleanup: 49896b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 49908d1c802bSDavid Ahern if (nh->fib6_info) 49918d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 49926b9ea5a6SRoopa Prabhu list_del(&nh->next); 49936b9ea5a6SRoopa Prabhu kfree(nh); 49946b9ea5a6SRoopa Prabhu } 49956b9ea5a6SRoopa Prabhu 49966b9ea5a6SRoopa Prabhu return err; 49976b9ea5a6SRoopa Prabhu } 49986b9ea5a6SRoopa Prabhu 4999333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 5000333c4301SDavid Ahern struct netlink_ext_ack *extack) 50016b9ea5a6SRoopa Prabhu { 50026b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 50036b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 50046b9ea5a6SRoopa Prabhu int remaining; 50056b9ea5a6SRoopa Prabhu int attrlen; 50066b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 50076b9ea5a6SRoopa Prabhu 50086b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 50096b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 50106b9ea5a6SRoopa Prabhu 50116b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 50126b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 50136b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 50146b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 50156b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 50166b9ea5a6SRoopa Prabhu 50176b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 50186b9ea5a6SRoopa Prabhu if (attrlen > 0) { 50196b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 50206b9ea5a6SRoopa Prabhu 50216b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 50226b9ea5a6SRoopa Prabhu if (nla) { 50236b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 50246b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 50256b9ea5a6SRoopa Prabhu } 50266b9ea5a6SRoopa Prabhu } 5027333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 50286b9ea5a6SRoopa Prabhu if (err) 50296b9ea5a6SRoopa Prabhu last_err = err; 50306b9ea5a6SRoopa Prabhu 503151ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 503251ebd318SNicolas Dichtel } 503351ebd318SNicolas Dichtel 503451ebd318SNicolas Dichtel return last_err; 503551ebd318SNicolas Dichtel } 503651ebd318SNicolas Dichtel 5037c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5038c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 50391da177e4SLinus Torvalds { 504086872cb5SThomas Graf struct fib6_config cfg; 504186872cb5SThomas Graf int err; 50421da177e4SLinus Torvalds 5043333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 504486872cb5SThomas Graf if (err < 0) 504586872cb5SThomas Graf return err; 504686872cb5SThomas Graf 504751ebd318SNicolas Dichtel if (cfg.fc_mp) 5048333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 50490ae81335SDavid Ahern else { 50500ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 5051333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 50521da177e4SLinus Torvalds } 50530ae81335SDavid Ahern } 50541da177e4SLinus Torvalds 5055c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 5056c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 50571da177e4SLinus Torvalds { 505886872cb5SThomas Graf struct fib6_config cfg; 505986872cb5SThomas Graf int err; 50601da177e4SLinus Torvalds 5061333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 506286872cb5SThomas Graf if (err < 0) 506386872cb5SThomas Graf return err; 506486872cb5SThomas Graf 506567f69513SDavid Ahern if (cfg.fc_metric == 0) 506667f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 506767f69513SDavid Ahern 506851ebd318SNicolas Dichtel if (cfg.fc_mp) 5069333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 507051ebd318SNicolas Dichtel else 5071acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 50721da177e4SLinus Torvalds } 50731da177e4SLinus Torvalds 5074a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */ 5075a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) 5076339bf98fSThomas Graf { 5077a1b7a1f0SDavid Ahern int *nexthop_len = arg; 5078beb1afacSDavid Ahern 5079a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ 5080a1b7a1f0SDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5081a1b7a1f0SDavid Ahern + nla_total_size(16); /* RTA_GATEWAY */ 5082f88d8ea6SDavid Ahern 5083a1b7a1f0SDavid Ahern if (nh->fib_nh_lws) { 5084a1b7a1f0SDavid Ahern /* RTA_ENCAP_TYPE */ 5085a1b7a1f0SDavid Ahern *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5086a1b7a1f0SDavid Ahern /* RTA_ENCAP */ 5087a1b7a1f0SDavid Ahern *nexthop_len += nla_total_size(2); 5088a1b7a1f0SDavid Ahern } 5089a1b7a1f0SDavid Ahern 5090a1b7a1f0SDavid Ahern return 0; 5091a1b7a1f0SDavid Ahern } 5092a1b7a1f0SDavid Ahern 5093a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i) 5094a1b7a1f0SDavid Ahern { 5095a1b7a1f0SDavid Ahern int nexthop_len; 5096a1b7a1f0SDavid Ahern 5097a1b7a1f0SDavid Ahern if (f6i->nh) { 5098a1b7a1f0SDavid Ahern nexthop_len = nla_total_size(4); /* RTA_NH_ID */ 5099a1b7a1f0SDavid Ahern nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, 5100a1b7a1f0SDavid Ahern &nexthop_len); 5101a1b7a1f0SDavid Ahern } else { 5102a1b7a1f0SDavid Ahern struct fib6_nh *nh = f6i->fib6_nh; 5103a1b7a1f0SDavid Ahern 5104a1b7a1f0SDavid Ahern nexthop_len = 0; 5105a1b7a1f0SDavid Ahern if (f6i->fib6_nsiblings) { 5106beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 5107beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 5108beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 5109a1b7a1f0SDavid Ahern + lwtunnel_get_encap_size(nh->fib_nh_lws); 5110beb1afacSDavid Ahern 5111a1b7a1f0SDavid Ahern nexthop_len *= f6i->fib6_nsiblings; 5112a1b7a1f0SDavid Ahern } 5113a1b7a1f0SDavid Ahern nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); 5114beb1afacSDavid Ahern } 5115beb1afacSDavid Ahern 5116339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 5117339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 5118339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 5119339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 5120339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 5121339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 5122339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 5123339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 5124339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 51256a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 5126ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 5127c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 512819e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 5129beb1afacSDavid Ahern + nexthop_len; 5130beb1afacSDavid Ahern } 5131beb1afacSDavid Ahern 5132f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, 5133f88d8ea6SDavid Ahern unsigned char *flags) 5134f88d8ea6SDavid Ahern { 5135f88d8ea6SDavid Ahern if (nexthop_is_multipath(nh)) { 5136f88d8ea6SDavid Ahern struct nlattr *mp; 5137f88d8ea6SDavid Ahern 5138f88d8ea6SDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 5139f88d8ea6SDavid Ahern if (!mp) 5140f88d8ea6SDavid Ahern goto nla_put_failure; 5141f88d8ea6SDavid Ahern 5142f88d8ea6SDavid Ahern if (nexthop_mpath_fill_node(skb, nh)) 5143f88d8ea6SDavid Ahern goto nla_put_failure; 5144f88d8ea6SDavid Ahern 5145f88d8ea6SDavid Ahern nla_nest_end(skb, mp); 5146f88d8ea6SDavid Ahern } else { 5147f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh; 5148f88d8ea6SDavid Ahern 5149f88d8ea6SDavid Ahern fib6_nh = nexthop_fib6_nh(nh); 5150f88d8ea6SDavid Ahern if (fib_nexthop_info(skb, &fib6_nh->nh_common, 5151f88d8ea6SDavid Ahern flags, false) < 0) 5152f88d8ea6SDavid Ahern goto nla_put_failure; 5153f88d8ea6SDavid Ahern } 5154f88d8ea6SDavid Ahern 5155f88d8ea6SDavid Ahern return 0; 5156f88d8ea6SDavid Ahern 5157f88d8ea6SDavid Ahern nla_put_failure: 5158f88d8ea6SDavid Ahern return -EMSGSIZE; 5159f88d8ea6SDavid Ahern } 5160f88d8ea6SDavid Ahern 5161d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 51628d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 5163d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 516415e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 5165f8cfe2ceSDavid Ahern unsigned int flags) 51661da177e4SLinus Torvalds { 516722d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 516822d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 516922d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 5170f88d8ea6SDavid Ahern unsigned char nh_flags = 0; 51711da177e4SLinus Torvalds struct nlmsghdr *nlh; 517222d0bd82SXin Long struct rtmsg *rtm; 5173d4ead6b3SDavid Ahern long expires = 0; 51741da177e4SLinus Torvalds 517515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 517638308473SDavid S. Miller if (!nlh) 517726932566SPatrick McHardy return -EMSGSIZE; 51782d7202bfSThomas Graf 517922d0bd82SXin Long if (rt6) { 518022d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 518122d0bd82SXin Long rt6_src = &rt6->rt6i_src; 518222d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 518322d0bd82SXin Long } else { 518422d0bd82SXin Long rt6_dst = &rt->fib6_dst; 518522d0bd82SXin Long rt6_src = &rt->fib6_src; 518622d0bd82SXin Long rt6_flags = rt->fib6_flags; 518722d0bd82SXin Long } 518822d0bd82SXin Long 51892d7202bfSThomas Graf rtm = nlmsg_data(nlh); 51901da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 519122d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 519222d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 51931da177e4SLinus Torvalds rtm->rtm_tos = 0; 519493c2fb25SDavid Ahern if (rt->fib6_table) 519593c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 5196c71099acSThomas Graf else 51979e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 519897f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 5199c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 5200c78679e8SDavid S. Miller goto nla_put_failure; 5201e8478e80SDavid Ahern 5202e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 52031da177e4SLinus Torvalds rtm->rtm_flags = 0; 52041da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 520593c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 52061da177e4SLinus Torvalds 520722d0bd82SXin Long if (rt6_flags & RTF_CACHE) 52081da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 52091da177e4SLinus Torvalds 5210d4ead6b3SDavid Ahern if (dest) { 5211d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 5212c78679e8SDavid S. Miller goto nla_put_failure; 52131da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 52141da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 521522d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 5216c78679e8SDavid S. Miller goto nla_put_failure; 52171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 52181da177e4SLinus Torvalds if (src) { 5219930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 5220c78679e8SDavid S. Miller goto nla_put_failure; 52211da177e4SLinus Torvalds rtm->rtm_src_len = 128; 5222c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 522322d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 5224c78679e8SDavid S. Miller goto nla_put_failure; 52251da177e4SLinus Torvalds #endif 52267bc570c8SYOSHIFUJI Hideaki if (iif) { 52277bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 522822d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 5229fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 52302cf75070SNikolay Aleksandrov 52317bc570c8SYOSHIFUJI Hideaki if (err == 0) 52327bc570c8SYOSHIFUJI Hideaki return 0; 5233fd61c6baSDavid Ahern if (err < 0) 52347bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 52357bc570c8SYOSHIFUJI Hideaki } else 52367bc570c8SYOSHIFUJI Hideaki #endif 5237c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 5238c78679e8SDavid S. Miller goto nla_put_failure; 5239d4ead6b3SDavid Ahern } else if (dest) { 52401da177e4SLinus Torvalds struct in6_addr saddr_buf; 5241d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 5242930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5243c78679e8SDavid S. Miller goto nla_put_failure; 5244c3968a85SDaniel Walter } 5245c3968a85SDaniel Walter 524693c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 5247c3968a85SDaniel Walter struct in6_addr saddr_buf; 524893c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 5249930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 5250c78679e8SDavid S. Miller goto nla_put_failure; 52511da177e4SLinus Torvalds } 52522d7202bfSThomas Graf 5253d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 5254d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 52552d7202bfSThomas Graf goto nla_put_failure; 52562d7202bfSThomas Graf 525793c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 5258beb1afacSDavid Ahern goto nla_put_failure; 5259beb1afacSDavid Ahern 5260beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 5261beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 5262beb1afacSDavid Ahern */ 526322d0bd82SXin Long if (rt6) { 526422d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 526522d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 526622d0bd82SXin Long goto nla_put_failure; 526722d0bd82SXin Long 526822d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 526922d0bd82SXin Long goto nla_put_failure; 527022d0bd82SXin Long } else if (rt->fib6_nsiblings) { 52718d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 5272beb1afacSDavid Ahern struct nlattr *mp; 5273beb1afacSDavid Ahern 5274ae0be8deSMichal Kubecek mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 5275beb1afacSDavid Ahern if (!mp) 5276beb1afacSDavid Ahern goto nla_put_failure; 5277beb1afacSDavid Ahern 52781cf844c7SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, 52791cf844c7SDavid Ahern rt->fib6_nh->fib_nh_weight) < 0) 5280beb1afacSDavid Ahern goto nla_put_failure; 5281beb1afacSDavid Ahern 5282beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 528393c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 52841cf844c7SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, 52851cf844c7SDavid Ahern sibling->fib6_nh->fib_nh_weight) < 0) 528694f826b8SEric Dumazet goto nla_put_failure; 528794f826b8SEric Dumazet } 52882d7202bfSThomas Graf 5289beb1afacSDavid Ahern nla_nest_end(skb, mp); 5290f88d8ea6SDavid Ahern } else if (rt->nh) { 5291f88d8ea6SDavid Ahern if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) 5292f88d8ea6SDavid Ahern goto nla_put_failure; 5293ecc5663cSDavid Ahern 5294f88d8ea6SDavid Ahern if (nexthop_is_blackhole(rt->nh)) 5295f88d8ea6SDavid Ahern rtm->rtm_type = RTN_BLACKHOLE; 5296f88d8ea6SDavid Ahern 5297f88d8ea6SDavid Ahern if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) 5298f88d8ea6SDavid Ahern goto nla_put_failure; 5299f88d8ea6SDavid Ahern 5300f88d8ea6SDavid Ahern rtm->rtm_flags |= nh_flags; 5301f88d8ea6SDavid Ahern } else { 53021cf844c7SDavid Ahern if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, 5303ecc5663cSDavid Ahern &nh_flags, false) < 0) 5304c78679e8SDavid S. Miller goto nla_put_failure; 5305ecc5663cSDavid Ahern 5306ecc5663cSDavid Ahern rtm->rtm_flags |= nh_flags; 5307beb1afacSDavid Ahern } 53088253947eSLi Wei 530922d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 531014895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 531114895687SDavid Ahern expires -= jiffies; 531214895687SDavid Ahern } 531369cdf8f9SYOSHIFUJI Hideaki 5314d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 5315e3703b3dSThomas Graf goto nla_put_failure; 53161da177e4SLinus Torvalds 531722d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 5318c78ba6d6SLubomir Rintel goto nla_put_failure; 5319c78ba6d6SLubomir Rintel 532019e42e45SRoopa Prabhu 5321053c095aSJohannes Berg nlmsg_end(skb, nlh); 5322053c095aSJohannes Berg return 0; 53232d7202bfSThomas Graf 53242d7202bfSThomas Graf nla_put_failure: 532526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 532626932566SPatrick McHardy return -EMSGSIZE; 53271da177e4SLinus Torvalds } 53281da177e4SLinus Torvalds 53292c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) 53302c170e07SDavid Ahern { 53312c170e07SDavid Ahern const struct net_device *dev = arg; 53322c170e07SDavid Ahern 53332c170e07SDavid Ahern if (nh->fib_nh_dev == dev) 53342c170e07SDavid Ahern return 1; 53352c170e07SDavid Ahern 53362c170e07SDavid Ahern return 0; 53372c170e07SDavid Ahern } 53382c170e07SDavid Ahern 533913e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 534013e38901SDavid Ahern const struct net_device *dev) 534113e38901SDavid Ahern { 53422c170e07SDavid Ahern if (f6i->nh) { 53432c170e07SDavid Ahern struct net_device *_dev = (struct net_device *)dev; 53442c170e07SDavid Ahern 53452c170e07SDavid Ahern return !!nexthop_for_each_fib6_nh(f6i->nh, 53462c170e07SDavid Ahern fib6_info_nh_uses_dev, 53472c170e07SDavid Ahern _dev); 53482c170e07SDavid Ahern } 53492c170e07SDavid Ahern 53501cf844c7SDavid Ahern if (f6i->fib6_nh->fib_nh_dev == dev) 535113e38901SDavid Ahern return true; 535213e38901SDavid Ahern 535313e38901SDavid Ahern if (f6i->fib6_nsiblings) { 535413e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 535513e38901SDavid Ahern 535613e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 535713e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 53581cf844c7SDavid Ahern if (sibling->fib6_nh->fib_nh_dev == dev) 535913e38901SDavid Ahern return true; 536013e38901SDavid Ahern } 536113e38901SDavid Ahern } 536213e38901SDavid Ahern 536313e38901SDavid Ahern return false; 536413e38901SDavid Ahern } 536513e38901SDavid Ahern 53668d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg) 53671da177e4SLinus Torvalds { 53681da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 536913e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 537013e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 53711f17e2f2SDavid Ahern struct net *net = arg->net; 53721f17e2f2SDavid Ahern 5373421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 53741f17e2f2SDavid Ahern return 0; 53751da177e4SLinus Torvalds 537613e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 537793c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 5378f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 5379f8cfe2ceSDavid Ahern return 1; 5380f8cfe2ceSDavid Ahern } 538113e38901SDavid Ahern if (filter->filter_set) { 538213e38901SDavid Ahern if ((filter->rt_type && rt->fib6_type != filter->rt_type) || 538313e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 538413e38901SDavid Ahern (filter->protocol && rt->fib6_protocol != filter->protocol)) { 538513e38901SDavid Ahern return 1; 538613e38901SDavid Ahern } 538713e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 5388f8cfe2ceSDavid Ahern } 53891da177e4SLinus Torvalds 5390d4ead6b3SDavid Ahern return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, 5391d4ead6b3SDavid Ahern RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, 539213e38901SDavid Ahern arg->cb->nlh->nlmsg_seq, flags); 53931da177e4SLinus Torvalds } 53941da177e4SLinus Torvalds 53950eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 53960eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 53970eff0a27SJakub Kicinski struct nlattr **tb, 53980eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 53990eff0a27SJakub Kicinski { 54000eff0a27SJakub Kicinski struct rtmsg *rtm; 54010eff0a27SJakub Kicinski int i, err; 54020eff0a27SJakub Kicinski 54030eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 54040eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 54050eff0a27SJakub Kicinski "Invalid header for get route request"); 54060eff0a27SJakub Kicinski return -EINVAL; 54070eff0a27SJakub Kicinski } 54080eff0a27SJakub Kicinski 54090eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 54108cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 54110eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 54120eff0a27SJakub Kicinski 54130eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 54140eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 54150eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 54160eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 54170eff0a27SJakub Kicinski rtm->rtm_type) { 54180eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 54190eff0a27SJakub Kicinski return -EINVAL; 54200eff0a27SJakub Kicinski } 54210eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 54220eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 54230eff0a27SJakub Kicinski "Invalid flags for get route request"); 54240eff0a27SJakub Kicinski return -EINVAL; 54250eff0a27SJakub Kicinski } 54260eff0a27SJakub Kicinski 54278cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 54280eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 54290eff0a27SJakub Kicinski if (err) 54300eff0a27SJakub Kicinski return err; 54310eff0a27SJakub Kicinski 54320eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 54330eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 54340eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 54350eff0a27SJakub Kicinski return -EINVAL; 54360eff0a27SJakub Kicinski } 54370eff0a27SJakub Kicinski 54380eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 54390eff0a27SJakub Kicinski if (!tb[i]) 54400eff0a27SJakub Kicinski continue; 54410eff0a27SJakub Kicinski 54420eff0a27SJakub Kicinski switch (i) { 54430eff0a27SJakub Kicinski case RTA_SRC: 54440eff0a27SJakub Kicinski case RTA_DST: 54450eff0a27SJakub Kicinski case RTA_IIF: 54460eff0a27SJakub Kicinski case RTA_OIF: 54470eff0a27SJakub Kicinski case RTA_MARK: 54480eff0a27SJakub Kicinski case RTA_UID: 54490eff0a27SJakub Kicinski case RTA_SPORT: 54500eff0a27SJakub Kicinski case RTA_DPORT: 54510eff0a27SJakub Kicinski case RTA_IP_PROTO: 54520eff0a27SJakub Kicinski break; 54530eff0a27SJakub Kicinski default: 54540eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 54550eff0a27SJakub Kicinski return -EINVAL; 54560eff0a27SJakub Kicinski } 54570eff0a27SJakub Kicinski } 54580eff0a27SJakub Kicinski 54590eff0a27SJakub Kicinski return 0; 54600eff0a27SJakub Kicinski } 54610eff0a27SJakub Kicinski 5462c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5463c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 54641da177e4SLinus Torvalds { 54653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 5466ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 546718c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 5468a68886a6SDavid Ahern struct fib6_info *from; 546918c3a61cSRoopa Prabhu struct dst_entry *dst; 54701da177e4SLinus Torvalds struct rt6_info *rt; 5471ab364a6fSThomas Graf struct sk_buff *skb; 5472ab364a6fSThomas Graf struct rtmsg *rtm; 5473744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 547418c3a61cSRoopa Prabhu bool fibmatch; 5475ab364a6fSThomas Graf 54760eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 5477ab364a6fSThomas Graf if (err < 0) 5478ab364a6fSThomas Graf goto errout; 5479ab364a6fSThomas Graf 5480ab364a6fSThomas Graf err = -EINVAL; 548138b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 548238b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 548318c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 5484ab364a6fSThomas Graf 5485ab364a6fSThomas Graf if (tb[RTA_SRC]) { 5486ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 5487ab364a6fSThomas Graf goto errout; 5488ab364a6fSThomas Graf 54894e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 5490ab364a6fSThomas Graf } 5491ab364a6fSThomas Graf 5492ab364a6fSThomas Graf if (tb[RTA_DST]) { 5493ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 5494ab364a6fSThomas Graf goto errout; 5495ab364a6fSThomas Graf 54964e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 5497ab364a6fSThomas Graf } 5498ab364a6fSThomas Graf 5499ab364a6fSThomas Graf if (tb[RTA_IIF]) 5500ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 5501ab364a6fSThomas Graf 5502ab364a6fSThomas Graf if (tb[RTA_OIF]) 550372331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 5504ab364a6fSThomas Graf 55052e47b291SLorenzo Colitti if (tb[RTA_MARK]) 55062e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 55072e47b291SLorenzo Colitti 5508622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 5509622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 5510622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 5511622ec2c9SLorenzo Colitti else 5512622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 5513622ec2c9SLorenzo Colitti 5514eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 5515eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 5516eacb9384SRoopa Prabhu 5517eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 5518eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 5519eacb9384SRoopa Prabhu 5520eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 5521eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 55225e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 55235e1a99eaSHangbin Liu extack); 5524eacb9384SRoopa Prabhu if (err) 5525eacb9384SRoopa Prabhu goto errout; 5526eacb9384SRoopa Prabhu } 5527eacb9384SRoopa Prabhu 5528ab364a6fSThomas Graf if (iif) { 5529ab364a6fSThomas Graf struct net_device *dev; 553072331bc0SShmulik Ladkani int flags = 0; 553172331bc0SShmulik Ladkani 5532121622dbSFlorian Westphal rcu_read_lock(); 5533121622dbSFlorian Westphal 5534121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 5535ab364a6fSThomas Graf if (!dev) { 5536121622dbSFlorian Westphal rcu_read_unlock(); 5537ab364a6fSThomas Graf err = -ENODEV; 5538ab364a6fSThomas Graf goto errout; 5539ab364a6fSThomas Graf } 554072331bc0SShmulik Ladkani 554172331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 554272331bc0SShmulik Ladkani 554372331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 554472331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 554572331bc0SShmulik Ladkani 5546b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 5547121622dbSFlorian Westphal 5548121622dbSFlorian Westphal rcu_read_unlock(); 554972331bc0SShmulik Ladkani } else { 555072331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 555172331bc0SShmulik Ladkani 555218c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 555318c3a61cSRoopa Prabhu } 555418c3a61cSRoopa Prabhu 555518c3a61cSRoopa Prabhu 555618c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 555718c3a61cSRoopa Prabhu if (rt->dst.error) { 555818c3a61cSRoopa Prabhu err = rt->dst.error; 555918c3a61cSRoopa Prabhu ip6_rt_put(rt); 556018c3a61cSRoopa Prabhu goto errout; 5561ab364a6fSThomas Graf } 55621da177e4SLinus Torvalds 55639d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 55649d6acb3bSWANG Cong err = rt->dst.error; 55659d6acb3bSWANG Cong ip6_rt_put(rt); 55669d6acb3bSWANG Cong goto errout; 55679d6acb3bSWANG Cong } 55689d6acb3bSWANG Cong 55691da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 557038308473SDavid S. Miller if (!skb) { 557194e187c0SAmerigo Wang ip6_rt_put(rt); 5572ab364a6fSThomas Graf err = -ENOBUFS; 5573ab364a6fSThomas Graf goto errout; 5574ab364a6fSThomas Graf } 55751da177e4SLinus Torvalds 5576d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 5577a68886a6SDavid Ahern 5578a68886a6SDavid Ahern rcu_read_lock(); 5579a68886a6SDavid Ahern from = rcu_dereference(rt->from); 5580886b7a50SMartin KaFai Lau if (from) { 558118c3a61cSRoopa Prabhu if (fibmatch) 5582886b7a50SMartin KaFai Lau err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 5583886b7a50SMartin KaFai Lau iif, RTM_NEWROUTE, 5584886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 558518c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 558618c3a61cSRoopa Prabhu else 5587a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 5588a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 5589886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 5590886b7a50SMartin KaFai Lau nlh->nlmsg_seq, 0); 5591886b7a50SMartin KaFai Lau } else { 5592886b7a50SMartin KaFai Lau err = -ENETUNREACH; 5593886b7a50SMartin KaFai Lau } 5594a68886a6SDavid Ahern rcu_read_unlock(); 5595a68886a6SDavid Ahern 55961da177e4SLinus Torvalds if (err < 0) { 5597ab364a6fSThomas Graf kfree_skb(skb); 5598ab364a6fSThomas Graf goto errout; 55991da177e4SLinus Torvalds } 56001da177e4SLinus Torvalds 560115e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 5602ab364a6fSThomas Graf errout: 56031da177e4SLinus Torvalds return err; 56041da177e4SLinus Torvalds } 56051da177e4SLinus Torvalds 56068d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 560737a1d361SRoopa Prabhu unsigned int nlm_flags) 56081da177e4SLinus Torvalds { 56091da177e4SLinus Torvalds struct sk_buff *skb; 56105578689aSDaniel Lezcano struct net *net = info->nl_net; 5611528c4cebSDenis V. Lunev u32 seq; 5612528c4cebSDenis V. Lunev int err; 56130d51aa80SJamal Hadi Salim 5614528c4cebSDenis V. Lunev err = -ENOBUFS; 561538308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 561686872cb5SThomas Graf 561719e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 561838308473SDavid S. Miller if (!skb) 561921713ebcSThomas Graf goto errout; 56201da177e4SLinus Torvalds 5621d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 5622f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 562326932566SPatrick McHardy if (err < 0) { 562426932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 562526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 562626932566SPatrick McHardy kfree_skb(skb); 562726932566SPatrick McHardy goto errout; 562826932566SPatrick McHardy } 562915e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 56305578689aSDaniel Lezcano info->nlh, gfp_any()); 56311ce85fe4SPablo Neira Ayuso return; 563221713ebcSThomas Graf errout: 563321713ebcSThomas Graf if (err < 0) 56345578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 56351da177e4SLinus Torvalds } 56361da177e4SLinus Torvalds 563719a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt, 563819a3b7eeSDavid Ahern struct nl_info *info) 563919a3b7eeSDavid Ahern { 564019a3b7eeSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 564119a3b7eeSDavid Ahern struct sk_buff *skb; 564219a3b7eeSDavid Ahern int err = -ENOBUFS; 564319a3b7eeSDavid Ahern 564419a3b7eeSDavid Ahern /* call_fib6_entry_notifiers will be removed when in-kernel notifier 564519a3b7eeSDavid Ahern * is implemented and supported for nexthop objects 564619a3b7eeSDavid Ahern */ 564719a3b7eeSDavid Ahern call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); 564819a3b7eeSDavid Ahern 564919a3b7eeSDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 565019a3b7eeSDavid Ahern if (!skb) 565119a3b7eeSDavid Ahern goto errout; 565219a3b7eeSDavid Ahern 565319a3b7eeSDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 565419a3b7eeSDavid Ahern RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 565519a3b7eeSDavid Ahern if (err < 0) { 565619a3b7eeSDavid Ahern /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 565719a3b7eeSDavid Ahern WARN_ON(err == -EMSGSIZE); 565819a3b7eeSDavid Ahern kfree_skb(skb); 565919a3b7eeSDavid Ahern goto errout; 566019a3b7eeSDavid Ahern } 566119a3b7eeSDavid Ahern rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 566219a3b7eeSDavid Ahern info->nlh, gfp_any()); 566319a3b7eeSDavid Ahern return; 566419a3b7eeSDavid Ahern errout: 566519a3b7eeSDavid Ahern if (err < 0) 566619a3b7eeSDavid Ahern rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 566719a3b7eeSDavid Ahern } 566819a3b7eeSDavid Ahern 56698ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 5670351638e7SJiri Pirko unsigned long event, void *ptr) 56718ed67789SDaniel Lezcano { 5672351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5673c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 56748ed67789SDaniel Lezcano 5675242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 5676242d3a49SWANG Cong return NOTIFY_OK; 5677242d3a49SWANG Cong 5678242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 56791cf844c7SDavid Ahern net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; 5680d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 56818ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 56828ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5683d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 56848ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 5685d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 56868ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 56878ed67789SDaniel Lezcano #endif 568876da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 568976da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 569076da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 569176da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 569276da0704SWANG Cong */ 569312d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 5694242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 569512d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 569612d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 5697242d3a49SWANG Cong #endif 56988ed67789SDaniel Lezcano } 56998ed67789SDaniel Lezcano 57008ed67789SDaniel Lezcano return NOTIFY_OK; 57018ed67789SDaniel Lezcano } 57028ed67789SDaniel Lezcano 57031da177e4SLinus Torvalds /* 57041da177e4SLinus Torvalds * /proc 57051da177e4SLinus Torvalds */ 57061da177e4SLinus Torvalds 57071da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 57081da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 57091da177e4SLinus Torvalds { 571069ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 57111da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 571269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 571369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 571481eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 571569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 571669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 5717fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 571869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 57191da177e4SLinus Torvalds 57201da177e4SLinus Torvalds return 0; 57211da177e4SLinus Torvalds } 57221da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 57231da177e4SLinus Torvalds 57241da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 57251da177e4SLinus Torvalds 57261da177e4SLinus Torvalds static 5727fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 57281da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 57291da177e4SLinus Torvalds { 5730c486da34SLucian Adrian Grijincu struct net *net; 5731c486da34SLucian Adrian Grijincu int delay; 5732f0fb9b28SAditya Pakki int ret; 5733c486da34SLucian Adrian Grijincu if (!write) 5734c486da34SLucian Adrian Grijincu return -EINVAL; 5735c486da34SLucian Adrian Grijincu 5736c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 5737c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 5738f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5739f0fb9b28SAditya Pakki if (ret) 5740f0fb9b28SAditya Pakki return ret; 5741f0fb9b28SAditya Pakki 57422ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 57431da177e4SLinus Torvalds return 0; 57441da177e4SLinus Torvalds } 57451da177e4SLinus Torvalds 57467c6bb7d2SDavid Ahern static int zero; 57477c6bb7d2SDavid Ahern static int one = 1; 57487c6bb7d2SDavid Ahern 5749ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 57501da177e4SLinus Torvalds { 57511da177e4SLinus Torvalds .procname = "flush", 57524990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 57531da177e4SLinus Torvalds .maxlen = sizeof(int), 575489c8b3a1SDave Jones .mode = 0200, 57556d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 57561da177e4SLinus Torvalds }, 57571da177e4SLinus Torvalds { 57581da177e4SLinus Torvalds .procname = "gc_thresh", 57599a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 57601da177e4SLinus Torvalds .maxlen = sizeof(int), 57611da177e4SLinus Torvalds .mode = 0644, 57626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57631da177e4SLinus Torvalds }, 57641da177e4SLinus Torvalds { 57651da177e4SLinus Torvalds .procname = "max_size", 57664990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 57671da177e4SLinus Torvalds .maxlen = sizeof(int), 57681da177e4SLinus Torvalds .mode = 0644, 57696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57701da177e4SLinus Torvalds }, 57711da177e4SLinus Torvalds { 57721da177e4SLinus Torvalds .procname = "gc_min_interval", 57734990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 57741da177e4SLinus Torvalds .maxlen = sizeof(int), 57751da177e4SLinus Torvalds .mode = 0644, 57766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57771da177e4SLinus Torvalds }, 57781da177e4SLinus Torvalds { 57791da177e4SLinus Torvalds .procname = "gc_timeout", 57804990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 57811da177e4SLinus Torvalds .maxlen = sizeof(int), 57821da177e4SLinus Torvalds .mode = 0644, 57836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57841da177e4SLinus Torvalds }, 57851da177e4SLinus Torvalds { 57861da177e4SLinus Torvalds .procname = "gc_interval", 57874990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 57881da177e4SLinus Torvalds .maxlen = sizeof(int), 57891da177e4SLinus Torvalds .mode = 0644, 57906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57911da177e4SLinus Torvalds }, 57921da177e4SLinus Torvalds { 57931da177e4SLinus Torvalds .procname = "gc_elasticity", 57944990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 57951da177e4SLinus Torvalds .maxlen = sizeof(int), 57961da177e4SLinus Torvalds .mode = 0644, 5797f3d3f616SMin Zhang .proc_handler = proc_dointvec, 57981da177e4SLinus Torvalds }, 57991da177e4SLinus Torvalds { 58001da177e4SLinus Torvalds .procname = "mtu_expires", 58014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 58021da177e4SLinus Torvalds .maxlen = sizeof(int), 58031da177e4SLinus Torvalds .mode = 0644, 58046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 58051da177e4SLinus Torvalds }, 58061da177e4SLinus Torvalds { 58071da177e4SLinus Torvalds .procname = "min_adv_mss", 58084990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 58091da177e4SLinus Torvalds .maxlen = sizeof(int), 58101da177e4SLinus Torvalds .mode = 0644, 5811f3d3f616SMin Zhang .proc_handler = proc_dointvec, 58121da177e4SLinus Torvalds }, 58131da177e4SLinus Torvalds { 58141da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 58154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 58161da177e4SLinus Torvalds .maxlen = sizeof(int), 58171da177e4SLinus Torvalds .mode = 0644, 58186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 58191da177e4SLinus Torvalds }, 58207c6bb7d2SDavid Ahern { 58217c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 58227c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 58237c6bb7d2SDavid Ahern .maxlen = sizeof(int), 58247c6bb7d2SDavid Ahern .mode = 0644, 58257c6bb7d2SDavid Ahern .proc_handler = proc_dointvec, 58267c6bb7d2SDavid Ahern .extra1 = &zero, 58277c6bb7d2SDavid Ahern .extra2 = &one, 58287c6bb7d2SDavid Ahern }, 5829f8572d8fSEric W. Biederman { } 58301da177e4SLinus Torvalds }; 58311da177e4SLinus Torvalds 58322c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 5833760f2d01SDaniel Lezcano { 5834760f2d01SDaniel Lezcano struct ctl_table *table; 5835760f2d01SDaniel Lezcano 5836760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 5837760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 5838760f2d01SDaniel Lezcano GFP_KERNEL); 58395ee09105SYOSHIFUJI Hideaki 58405ee09105SYOSHIFUJI Hideaki if (table) { 58415ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 5842c486da34SLucian Adrian Grijincu table[0].extra1 = net; 584386393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 58445ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 58455ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 58465ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 58475ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 58485ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 58495ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 58505ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 58519c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 58527c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 5853464dc801SEric W. Biederman 5854464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 5855464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 5856464dc801SEric W. Biederman table[0].procname = NULL; 58575ee09105SYOSHIFUJI Hideaki } 58585ee09105SYOSHIFUJI Hideaki 5859760f2d01SDaniel Lezcano return table; 5860760f2d01SDaniel Lezcano } 58611da177e4SLinus Torvalds #endif 58621da177e4SLinus Torvalds 58632c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 5864cdb18761SDaniel Lezcano { 5865633d424bSPavel Emelyanov int ret = -ENOMEM; 58668ed67789SDaniel Lezcano 586786393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 586886393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 5869f2fc6a54SBenjamin Thery 5870fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 5871fc66f95cSEric Dumazet goto out_ip6_dst_ops; 5872fc66f95cSEric Dumazet 58731cf844c7SDavid Ahern net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); 5874421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 5875421842edSDavid Ahern goto out_ip6_dst_entries; 58761cf844c7SDavid Ahern memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, 58771cf844c7SDavid Ahern sizeof(*net->ipv6.fib6_null_entry)); 5878421842edSDavid Ahern 58798ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 58808ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 58818ed67789SDaniel Lezcano GFP_KERNEL); 58828ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 5883421842edSDavid Ahern goto out_fib6_null_entry; 5884d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 588562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 588662fa8a84SDavid S. Miller ip6_template_metrics, true); 58878ed67789SDaniel Lezcano 58888ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5889feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 58908ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 58918ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 58928ed67789SDaniel Lezcano GFP_KERNEL); 589368fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 589468fffc67SPeter Zijlstra goto out_ip6_null_entry; 5895d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 589662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 589762fa8a84SDavid S. Miller ip6_template_metrics, true); 58988ed67789SDaniel Lezcano 58998ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 59008ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 59018ed67789SDaniel Lezcano GFP_KERNEL); 590268fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 590368fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 5904d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 590562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 590662fa8a84SDavid S. Miller ip6_template_metrics, true); 59078ed67789SDaniel Lezcano #endif 59088ed67789SDaniel Lezcano 5909b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 5910b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 5911b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 5912b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 5913b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 5914b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 5915b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 5916b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 59177c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 5918b339a47cSPeter Zijlstra 59196891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 59206891a346SBenjamin Thery 59218ed67789SDaniel Lezcano ret = 0; 59228ed67789SDaniel Lezcano out: 59238ed67789SDaniel Lezcano return ret; 5924f2fc6a54SBenjamin Thery 592568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 592668fffc67SPeter Zijlstra out_ip6_prohibit_entry: 592768fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 592868fffc67SPeter Zijlstra out_ip6_null_entry: 592968fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 593068fffc67SPeter Zijlstra #endif 5931421842edSDavid Ahern out_fib6_null_entry: 5932421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 5933fc66f95cSEric Dumazet out_ip6_dst_entries: 5934fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5935f2fc6a54SBenjamin Thery out_ip6_dst_ops: 5936f2fc6a54SBenjamin Thery goto out; 5937cdb18761SDaniel Lezcano } 5938cdb18761SDaniel Lezcano 59392c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 5940cdb18761SDaniel Lezcano { 5941421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 59428ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 59438ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 59448ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 59458ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 59468ed67789SDaniel Lezcano #endif 594741bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5948cdb18761SDaniel Lezcano } 5949cdb18761SDaniel Lezcano 5950d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 5951d189634eSThomas Graf { 5952d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5953c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 5954c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 59553617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 59563617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 5957d189634eSThomas Graf #endif 5958d189634eSThomas Graf return 0; 5959d189634eSThomas Graf } 5960d189634eSThomas Graf 5961d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 5962d189634eSThomas Graf { 5963d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5964ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 5965ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 5966d189634eSThomas Graf #endif 5967d189634eSThomas Graf } 5968d189634eSThomas Graf 5969cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 5970cdb18761SDaniel Lezcano .init = ip6_route_net_init, 5971cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 5972cdb18761SDaniel Lezcano }; 5973cdb18761SDaniel Lezcano 5974c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 5975c3426b47SDavid S. Miller { 5976c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 5977c3426b47SDavid S. Miller 5978c3426b47SDavid S. Miller if (!bp) 5979c3426b47SDavid S. Miller return -ENOMEM; 5980c3426b47SDavid S. Miller inet_peer_base_init(bp); 5981c3426b47SDavid S. Miller net->ipv6.peers = bp; 5982c3426b47SDavid S. Miller return 0; 5983c3426b47SDavid S. Miller } 5984c3426b47SDavid S. Miller 5985c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5986c3426b47SDavid S. Miller { 5987c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5988c3426b47SDavid S. Miller 5989c3426b47SDavid S. Miller net->ipv6.peers = NULL; 599056a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5991c3426b47SDavid S. Miller kfree(bp); 5992c3426b47SDavid S. Miller } 5993c3426b47SDavid S. Miller 59942b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5995c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5996c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 5997c3426b47SDavid S. Miller }; 5998c3426b47SDavid S. Miller 5999d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 6000d189634eSThomas Graf .init = ip6_route_net_init_late, 6001d189634eSThomas Graf .exit = ip6_route_net_exit_late, 6002d189634eSThomas Graf }; 6003d189634eSThomas Graf 60048ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 60058ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 6006242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 60078ed67789SDaniel Lezcano }; 60088ed67789SDaniel Lezcano 60092f460933SWANG Cong void __init ip6_route_init_special_entries(void) 60102f460933SWANG Cong { 60112f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 60122f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 60132f460933SWANG Cong * manually for init_net */ 60141cf844c7SDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; 60152f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 60162f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 60172f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 60182f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 60192f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 60202f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 60212f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 60222f460933SWANG Cong #endif 60232f460933SWANG Cong } 60242f460933SWANG Cong 6025433d49c3SDaniel Lezcano int __init ip6_route_init(void) 60261da177e4SLinus Torvalds { 6027433d49c3SDaniel Lezcano int ret; 60288d0b94afSMartin KaFai Lau int cpu; 6029433d49c3SDaniel Lezcano 60309a7ec3a9SDaniel Lezcano ret = -ENOMEM; 60319a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 60329a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 60339a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 60349a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 6035c19a28e1SFernando Carrijo goto out; 603614e50e57SDavid S. Miller 6037fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 60388ed67789SDaniel Lezcano if (ret) 6039bdb3289fSDaniel Lezcano goto out_kmem_cache; 6040bdb3289fSDaniel Lezcano 6041c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 6042c3426b47SDavid S. Miller if (ret) 6043e8803b6cSDavid S. Miller goto out_dst_entries; 60442a0c451aSThomas Graf 60457e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 60467e52b33bSDavid S. Miller if (ret) 60477e52b33bSDavid S. Miller goto out_register_inetpeer; 6048c3426b47SDavid S. Miller 60495dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 60505dc121e9SArnaud Ebalard 6051e8803b6cSDavid S. Miller ret = fib6_init(); 6052433d49c3SDaniel Lezcano if (ret) 60538ed67789SDaniel Lezcano goto out_register_subsys; 6054433d49c3SDaniel Lezcano 6055433d49c3SDaniel Lezcano ret = xfrm6_init(); 6056433d49c3SDaniel Lezcano if (ret) 6057e8803b6cSDavid S. Miller goto out_fib6_init; 6058c35b7e72SDaniel Lezcano 6059433d49c3SDaniel Lezcano ret = fib6_rules_init(); 6060433d49c3SDaniel Lezcano if (ret) 6061433d49c3SDaniel Lezcano goto xfrm6_init; 60627e5449c2SDaniel Lezcano 6063d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 6064d189634eSThomas Graf if (ret) 6065d189634eSThomas Graf goto fib6_rules_init; 6066d189634eSThomas Graf 606716feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 606816feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 606916feebcfSFlorian Westphal if (ret < 0) 607016feebcfSFlorian Westphal goto out_register_late_subsys; 607116feebcfSFlorian Westphal 607216feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 607316feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 607416feebcfSFlorian Westphal if (ret < 0) 607516feebcfSFlorian Westphal goto out_register_late_subsys; 607616feebcfSFlorian Westphal 607716feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 607816feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 607916feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 608016feebcfSFlorian Westphal if (ret < 0) 6081d189634eSThomas Graf goto out_register_late_subsys; 6082433d49c3SDaniel Lezcano 60838ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 6084cdb18761SDaniel Lezcano if (ret) 6085d189634eSThomas Graf goto out_register_late_subsys; 60868ed67789SDaniel Lezcano 60878d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 60888d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 60898d0b94afSMartin KaFai Lau 60908d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 60918d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 60928d0b94afSMartin KaFai Lau } 60938d0b94afSMartin KaFai Lau 6094433d49c3SDaniel Lezcano out: 6095433d49c3SDaniel Lezcano return ret; 6096433d49c3SDaniel Lezcano 6097d189634eSThomas Graf out_register_late_subsys: 609816feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6099d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6100433d49c3SDaniel Lezcano fib6_rules_init: 6101433d49c3SDaniel Lezcano fib6_rules_cleanup(); 6102433d49c3SDaniel Lezcano xfrm6_init: 6103433d49c3SDaniel Lezcano xfrm6_fini(); 61042a0c451aSThomas Graf out_fib6_init: 61052a0c451aSThomas Graf fib6_gc_cleanup(); 61068ed67789SDaniel Lezcano out_register_subsys: 61078ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 61087e52b33bSDavid S. Miller out_register_inetpeer: 61097e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 6110fc66f95cSEric Dumazet out_dst_entries: 6111fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 6112433d49c3SDaniel Lezcano out_kmem_cache: 6113f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 6114433d49c3SDaniel Lezcano goto out; 61151da177e4SLinus Torvalds } 61161da177e4SLinus Torvalds 61171da177e4SLinus Torvalds void ip6_route_cleanup(void) 61181da177e4SLinus Torvalds { 61198ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 6120d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 6121101367c2SThomas Graf fib6_rules_cleanup(); 61221da177e4SLinus Torvalds xfrm6_fini(); 61231da177e4SLinus Torvalds fib6_gc_cleanup(); 6124c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 61258ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 612641bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 6127f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 61281da177e4SLinus Torvalds } 6129