11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 4735732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 66eacb9384SRoopa Prabhu #include <net/ip.h> 677c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 701da177e4SLinus Torvalds #include <linux/sysctl.h> 711da177e4SLinus Torvalds #endif 721da177e4SLinus Torvalds 7330d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type); 7430d444d3SDavid Ahern 7530d444d3SDavid Ahern #define CREATE_TRACE_POINTS 7630d444d3SDavid Ahern #include <trace/events/fib6.h> 7730d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); 7830d444d3SDavid Ahern #undef CREATE_TRACE_POINTS 7930d444d3SDavid Ahern 80afc154e9SHannes Frederic Sowa enum rt6_nud_state { 817e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 827e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 837e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 84afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 85afc154e9SHannes Frederic Sowa }; 86afc154e9SHannes Frederic Sowa 871da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 880dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 89ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 911da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 921da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 931da177e4SLinus Torvalds struct net_device *dev, int how); 94569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 97ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 987150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 99ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 1001da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 1016700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 1026700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 1036700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 1046700c270SDavid S. Miller struct sk_buff *skb); 1058d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict); 1068d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt); 107d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1088d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 109d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 11016a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 11116a16cd3SDavid Ahern unsigned int flags); 1128d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, 11335732d01SWei Wang struct in6_addr *daddr, 11435732d01SWei Wang struct in6_addr *saddr); 1151da177e4SLinus Torvalds 11670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1178d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 118b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 119830218c1SDavid Ahern const struct in6_addr *gwaddr, 120830218c1SDavid Ahern struct net_device *dev, 12195c96174SEric Dumazet unsigned int pref); 1228d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 123b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 124830218c1SDavid Ahern const struct in6_addr *gwaddr, 125830218c1SDavid Ahern struct net_device *dev); 12670ceb4f5SYOSHIFUJI Hideaki #endif 12770ceb4f5SYOSHIFUJI Hideaki 1288d0b94afSMartin KaFai Lau struct uncached_list { 1298d0b94afSMartin KaFai Lau spinlock_t lock; 1308d0b94afSMartin KaFai Lau struct list_head head; 1318d0b94afSMartin KaFai Lau }; 1328d0b94afSMartin KaFai Lau 1338d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1348d0b94afSMartin KaFai Lau 135510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1368d0b94afSMartin KaFai Lau { 1378d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1388d0b94afSMartin KaFai Lau 1398d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1408d0b94afSMartin KaFai Lau 1418d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1428d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1438d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1448d0b94afSMartin KaFai Lau } 1458d0b94afSMartin KaFai Lau 146510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1478d0b94afSMartin KaFai Lau { 1488d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1498d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 15081eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1518d0b94afSMartin KaFai Lau 1528d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1538d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15481eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1558d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1568d0b94afSMartin KaFai Lau } 1578d0b94afSMartin KaFai Lau } 1588d0b94afSMartin KaFai Lau 1598d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1608d0b94afSMartin KaFai Lau { 1618d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1628d0b94afSMartin KaFai Lau int cpu; 1638d0b94afSMartin KaFai Lau 164e332bc67SEric W. Biederman if (dev == loopback_dev) 165e332bc67SEric W. Biederman return; 166e332bc67SEric W. Biederman 1678d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1688d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1698d0b94afSMartin KaFai Lau struct rt6_info *rt; 1708d0b94afSMartin KaFai Lau 1718d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1728d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1738d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1748d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1758d0b94afSMartin KaFai Lau 176e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1778d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1788d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau 181e332bc67SEric W. Biederman if (rt_dev == dev) { 1828d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1838d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1848d0b94afSMartin KaFai Lau dev_put(rt_dev); 1858d0b94afSMartin KaFai Lau } 1868d0b94afSMartin KaFai Lau } 1878d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1888d0b94afSMartin KaFai Lau } 1898d0b94afSMartin KaFai Lau } 1908d0b94afSMartin KaFai Lau 191f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 192f894cbf8SDavid S. Miller struct sk_buff *skb, 193f894cbf8SDavid S. Miller const void *daddr) 19439232973SDavid S. Miller { 195a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19639232973SDavid S. Miller return (const void *) p; 197f894cbf8SDavid S. Miller else if (skb) 198f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 19939232973SDavid S. Miller return daddr; 20039232973SDavid S. Miller } 20139232973SDavid S. Miller 202f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 203f8a1b43bSDavid Ahern struct net_device *dev, 204f894cbf8SDavid S. Miller struct sk_buff *skb, 205f894cbf8SDavid S. Miller const void *daddr) 206d3aaeb38SDavid S. Miller { 20739232973SDavid S. Miller struct neighbour *n; 20839232973SDavid S. Miller 209f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 210f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 211f83c7790SDavid S. Miller if (n) 212f83c7790SDavid S. Miller return n; 213f8a1b43bSDavid Ahern return neigh_create(&nd_tbl, daddr, dev); 214f8a1b43bSDavid Ahern } 215f8a1b43bSDavid Ahern 216f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 217f8a1b43bSDavid Ahern struct sk_buff *skb, 218f8a1b43bSDavid Ahern const void *daddr) 219f8a1b43bSDavid Ahern { 220f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 221f8a1b43bSDavid Ahern 222f8a1b43bSDavid Ahern return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); 223f83c7790SDavid S. Miller } 224f83c7790SDavid S. Miller 22563fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22663fca65dSJulian Anastasov { 22763fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22863fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 22963fca65dSJulian Anastasov 230f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); 23163fca65dSJulian Anastasov if (!daddr) 23263fca65dSJulian Anastasov return; 23363fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23463fca65dSJulian Anastasov return; 23563fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23863fca65dSJulian Anastasov } 23963fca65dSJulian Anastasov 2409a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2411da177e4SLinus Torvalds .family = AF_INET6, 2421da177e4SLinus Torvalds .gc = ip6_dst_gc, 2431da177e4SLinus Torvalds .gc_thresh = 1024, 2441da177e4SLinus Torvalds .check = ip6_dst_check, 2450dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 246ebb762f2SSteffen Klassert .mtu = ip6_mtu, 247d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2481da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2491da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2501da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2511da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2521da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2536e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2549f8955ccSEric W. Biederman .local_out = __ip6_local_out, 255f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 25663fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2571da177e4SLinus Torvalds }; 2581da177e4SLinus Torvalds 259ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 260ec831ea7SRoland Dreier { 261618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 262618f9bc7SSteffen Klassert 263618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 264ec831ea7SRoland Dreier } 265ec831ea7SRoland Dreier 2666700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2676700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 26814e50e57SDavid S. Miller { 26914e50e57SDavid S. Miller } 27014e50e57SDavid S. Miller 2716700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2726700c270SDavid S. Miller struct sk_buff *skb) 273b587ee3bSDavid S. Miller { 274b587ee3bSDavid S. Miller } 275b587ee3bSDavid S. Miller 27614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27714e50e57SDavid S. Miller .family = AF_INET6, 27814e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 27914e50e57SDavid S. Miller .check = ip6_dst_check, 280ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 281214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28214e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 283b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2840a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 285f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 28614e50e57SDavid S. Miller }; 28714e50e57SDavid S. Miller 28862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 28914edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29062fa8a84SDavid S. Miller }; 29162fa8a84SDavid S. Miller 2928d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 29393c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 29493c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 29593c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 29693c2fb25SDavid Ahern .fib6_ref = ATOMIC_INIT(1), 297421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 298421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 299421842edSDavid Ahern }; 300421842edSDavid Ahern 301fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 3021da177e4SLinus Torvalds .dst = { 3031da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3041da177e4SLinus Torvalds .__use = 1, 3052c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3061da177e4SLinus Torvalds .error = -ENETUNREACH, 3071da177e4SLinus Torvalds .input = ip6_pkt_discard, 3081da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3091da177e4SLinus Torvalds }, 3101da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3111da177e4SLinus Torvalds }; 3121da177e4SLinus Torvalds 313101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 314101367c2SThomas Graf 315fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 316101367c2SThomas Graf .dst = { 317101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 318101367c2SThomas Graf .__use = 1, 3192c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 320101367c2SThomas Graf .error = -EACCES, 3219ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3229ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 323101367c2SThomas Graf }, 324101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 325101367c2SThomas Graf }; 326101367c2SThomas Graf 327fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 328101367c2SThomas Graf .dst = { 329101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 330101367c2SThomas Graf .__use = 1, 3312c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 332101367c2SThomas Graf .error = -EINVAL, 333352e512cSHerbert Xu .input = dst_discard, 334ede2059dSEric W. Biederman .output = dst_discard_out, 335101367c2SThomas Graf }, 336101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 337101367c2SThomas Graf }; 338101367c2SThomas Graf 339101367c2SThomas Graf #endif 340101367c2SThomas Graf 341ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 342ebfa45f0SMartin KaFai Lau { 343ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 344ebfa45f0SMartin KaFai Lau 345ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 346ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 347ebfa45f0SMartin KaFai Lau } 348ebfa45f0SMartin KaFai Lau 3491da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 35093531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 351ad706862SMartin KaFai Lau int flags) 3521da177e4SLinus Torvalds { 35397bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 354b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 355cf911662SDavid S. Miller 35681eb8447SWei Wang if (rt) { 357ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 35881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 35981eb8447SWei Wang } 3608104891bSSteffen Klassert 361cf911662SDavid S. Miller return rt; 3621da177e4SLinus Torvalds } 3639ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 364d52d3997SMartin KaFai Lau 3651da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 368a68886a6SDavid Ahern struct fib6_info *from; 3698d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3701da177e4SLinus Torvalds 3718e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 3728d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3738d0b94afSMartin KaFai Lau 3748d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37538308473SDavid S. Miller if (idev) { 3761da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3771da177e4SLinus Torvalds in6_dev_put(idev); 3781da177e4SLinus Torvalds } 3791716a961SGao feng 380a68886a6SDavid Ahern rcu_read_lock(); 381a68886a6SDavid Ahern from = rcu_dereference(rt->from); 382a68886a6SDavid Ahern rcu_assign_pointer(rt->from, NULL); 38393531c67SDavid Ahern fib6_info_release(from); 384a68886a6SDavid Ahern rcu_read_unlock(); 385b3419363SDavid S. Miller } 386b3419363SDavid S. Miller 3871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3881da177e4SLinus Torvalds int how) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3911da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3925a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 393c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3941da177e4SLinus Torvalds 395e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 396e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 39738308473SDavid S. Miller if (loopback_idev) { 3981da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3991da177e4SLinus Torvalds in6_dev_put(idev); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds } 40297cac082SDavid S. Miller } 4031da177e4SLinus Torvalds 4045973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4055973fb1eSMartin KaFai Lau { 4065973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4075973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4085973fb1eSMartin KaFai Lau else 4095973fb1eSMartin KaFai Lau return false; 4105973fb1eSMartin KaFai Lau } 4115973fb1eSMartin KaFai Lau 412a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4131da177e4SLinus Torvalds { 414a68886a6SDavid Ahern struct fib6_info *from; 415a68886a6SDavid Ahern 416a68886a6SDavid Ahern from = rcu_dereference(rt->from); 417a68886a6SDavid Ahern 4181716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4191716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 420a50feda5SEric Dumazet return true; 421a68886a6SDavid Ahern } else if (from) { 4221e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 423a68886a6SDavid Ahern fib6_check_expired(from); 4241716a961SGao feng } 425a50feda5SEric Dumazet return false; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4283b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net, 4298d1c802bSDavid Ahern struct fib6_info *match, 43052bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 431b75cc8f9SDavid Ahern const struct sk_buff *skb, 43252bd4c0cSNicolas Dichtel int strict) 43351ebd318SNicolas Dichtel { 4348d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 43551ebd318SNicolas Dichtel 436b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 437b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 438b673d6ccSJakub Sitnicki */ 439b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 440b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 441b673d6ccSJakub Sitnicki 4425e670d84SDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound)) 4433d709f69SIdo Schimmel return match; 444bbfcd776SIdo Schimmel 44593c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 44693c2fb25SDavid Ahern fib6_siblings) { 4475e670d84SDavid Ahern int nh_upper_bound; 4485e670d84SDavid Ahern 4495e670d84SDavid Ahern nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound); 4505e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4513d709f69SIdo Schimmel continue; 45252bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 45352bd4c0cSNicolas Dichtel break; 45451ebd318SNicolas Dichtel match = sibling; 45551ebd318SNicolas Dichtel break; 45651ebd318SNicolas Dichtel } 4573d709f69SIdo Schimmel 45851ebd318SNicolas Dichtel return match; 45951ebd318SNicolas Dichtel } 46051ebd318SNicolas Dichtel 4611da177e4SLinus Torvalds /* 46266f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 4658d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net, 4668d1c802bSDavid Ahern struct fib6_info *rt, 467b71d1d42SEric Dumazet const struct in6_addr *saddr, 4681da177e4SLinus Torvalds int oif, 469d420895eSYOSHIFUJI Hideaki int flags) 4701da177e4SLinus Torvalds { 4718d1c802bSDavid Ahern struct fib6_info *sprt; 4721da177e4SLinus Torvalds 4735e670d84SDavid Ahern if (!oif && ipv6_addr_any(saddr) && 4745e670d84SDavid Ahern !(rt->fib6_nh.nh_flags & RTNH_F_DEAD)) 4758067bb8cSIdo Schimmel return rt; 476dd3abc4eSYOSHIFUJI Hideaki 4778fb11a9aSDavid Ahern for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) { 4785e670d84SDavid Ahern const struct net_device *dev = sprt->fib6_nh.nh_dev; 479dd3abc4eSYOSHIFUJI Hideaki 4805e670d84SDavid Ahern if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD) 4818067bb8cSIdo Schimmel continue; 4828067bb8cSIdo Schimmel 483dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4841da177e4SLinus Torvalds if (dev->ifindex == oif) 4851da177e4SLinus Torvalds return sprt; 486dd3abc4eSYOSHIFUJI Hideaki } else { 487dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 488dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 489dd3abc4eSYOSHIFUJI Hideaki return sprt; 490dd3abc4eSYOSHIFUJI Hideaki } 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 493eea68cd3SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) 494421842edSDavid Ahern return net->ipv6.fib6_null_entry; 4951da177e4SLinus Torvalds 496421842edSDavid Ahern return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 49927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 500c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 501c2f17e82SHannes Frederic Sowa struct work_struct work; 502c2f17e82SHannes Frederic Sowa struct in6_addr target; 503c2f17e82SHannes Frederic Sowa struct net_device *dev; 504c2f17e82SHannes Frederic Sowa }; 505c2f17e82SHannes Frederic Sowa 506c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 507c2f17e82SHannes Frederic Sowa { 508c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 509c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 510c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 511c2f17e82SHannes Frederic Sowa 512c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 513adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 514c2f17e82SHannes Frederic Sowa dev_put(work->dev); 515662f5533SMichael Büsch kfree(work); 516c2f17e82SHannes Frederic Sowa } 517c2f17e82SHannes Frederic Sowa 5188d1c802bSDavid Ahern static void rt6_probe(struct fib6_info *rt) 51927097255SYOSHIFUJI Hideaki { 520990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 5215e670d84SDavid Ahern const struct in6_addr *nh_gw; 522f2c31e32SEric Dumazet struct neighbour *neigh; 5235e670d84SDavid Ahern struct net_device *dev; 5245e670d84SDavid Ahern 52527097255SYOSHIFUJI Hideaki /* 52627097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 52727097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 52827097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 52927097255SYOSHIFUJI Hideaki * 53027097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 53127097255SYOSHIFUJI Hideaki * to no more than one per minute. 53227097255SYOSHIFUJI Hideaki */ 53393c2fb25SDavid Ahern if (!rt || !(rt->fib6_flags & RTF_GATEWAY)) 534fdd6681dSAmerigo Wang return; 5355e670d84SDavid Ahern 5365e670d84SDavid Ahern nh_gw = &rt->fib6_nh.nh_gw; 5375e670d84SDavid Ahern dev = rt->fib6_nh.nh_dev; 5382152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5395e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 5402152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 541dcd1f572SDavid Ahern struct inet6_dev *idev; 542dcd1f572SDavid Ahern 5438d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5448d6c31bfSMartin KaFai Lau goto out; 5458d6c31bfSMartin KaFai Lau 546dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 547990edb42SMartin KaFai Lau work = NULL; 5482152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 549990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 550990edb42SMartin KaFai Lau time_after(jiffies, 551dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 552c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 553990edb42SMartin KaFai Lau if (work) 5547e980569SJiri Benc __neigh_set_probe_once(neigh); 555990edb42SMartin KaFai Lau } 556c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 557990edb42SMartin KaFai Lau } else { 558990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 559990edb42SMartin KaFai Lau } 560c2f17e82SHannes Frederic Sowa 561c2f17e82SHannes Frederic Sowa if (work) { 562c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 5635e670d84SDavid Ahern work->target = *nh_gw; 5645e670d84SDavid Ahern dev_hold(dev); 5655e670d84SDavid Ahern work->dev = dev; 566c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 567c2f17e82SHannes Frederic Sowa } 568990edb42SMartin KaFai Lau 5698d6c31bfSMartin KaFai Lau out: 5702152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 571f2c31e32SEric Dumazet } 57227097255SYOSHIFUJI Hideaki #else 5738d1c802bSDavid Ahern static inline void rt6_probe(struct fib6_info *rt) 57427097255SYOSHIFUJI Hideaki { 57527097255SYOSHIFUJI Hideaki } 57627097255SYOSHIFUJI Hideaki #endif 57727097255SYOSHIFUJI Hideaki 5781da177e4SLinus Torvalds /* 579554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 5801da177e4SLinus Torvalds */ 5818d1c802bSDavid Ahern static inline int rt6_check_dev(struct fib6_info *rt, int oif) 5821da177e4SLinus Torvalds { 5835e670d84SDavid Ahern const struct net_device *dev = rt->fib6_nh.nh_dev; 5845e670d84SDavid Ahern 585161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 586554cfb7eSYOSHIFUJI Hideaki return 2; 587554cfb7eSYOSHIFUJI Hideaki return 0; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5908d1c802bSDavid Ahern static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt) 5911da177e4SLinus Torvalds { 592afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 5935e670d84SDavid Ahern struct neighbour *neigh; 594f2c31e32SEric Dumazet 59593c2fb25SDavid Ahern if (rt->fib6_flags & RTF_NONEXTHOP || 59693c2fb25SDavid Ahern !(rt->fib6_flags & RTF_GATEWAY)) 597afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 598145a3621SYOSHIFUJI Hideaki / 吉藤英明 599145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6005e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev, 6015e670d84SDavid Ahern &rt->fib6_nh.nh_gw); 602145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 603145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 604554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 605afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 606398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 607a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 608afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6097e980569SJiri Benc else 6107e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 611398bcbebSYOSHIFUJI Hideaki #endif 612145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 613afc154e9SHannes Frederic Sowa } else { 614afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6157e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 616a5a81f0bSPaul Marks } 617145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 618145a3621SYOSHIFUJI Hideaki / 吉藤英明 619a5a81f0bSPaul Marks return ret; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6228d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict) 623554cfb7eSYOSHIFUJI Hideaki { 624a5a81f0bSPaul Marks int m; 6254d0c5911SYOSHIFUJI Hideaki 6264d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 62777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 628afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 629ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 63093c2fb25SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2; 631ebacaaa0SYOSHIFUJI Hideaki #endif 632afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 633afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 634afc154e9SHannes Frederic Sowa if (n < 0) 635afc154e9SHannes Frederic Sowa return n; 636afc154e9SHannes Frederic Sowa } 637554cfb7eSYOSHIFUJI Hideaki return m; 638554cfb7eSYOSHIFUJI Hideaki } 639554cfb7eSYOSHIFUJI Hideaki 640dcd1f572SDavid Ahern /* called with rc_read_lock held */ 641dcd1f572SDavid Ahern static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i) 642dcd1f572SDavid Ahern { 643dcd1f572SDavid Ahern const struct net_device *dev = fib6_info_nh_dev(f6i); 644dcd1f572SDavid Ahern bool rc = false; 645dcd1f572SDavid Ahern 646dcd1f572SDavid Ahern if (dev) { 647dcd1f572SDavid Ahern const struct inet6_dev *idev = __in6_dev_get(dev); 648dcd1f572SDavid Ahern 649dcd1f572SDavid Ahern rc = !!idev->cnf.ignore_routes_with_linkdown; 650dcd1f572SDavid Ahern } 651dcd1f572SDavid Ahern 652dcd1f572SDavid Ahern return rc; 653dcd1f572SDavid Ahern } 654dcd1f572SDavid Ahern 6558d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict, 6568d1c802bSDavid Ahern int *mpri, struct fib6_info *match, 657afc154e9SHannes Frederic Sowa bool *do_rr) 658554cfb7eSYOSHIFUJI Hideaki { 659554cfb7eSYOSHIFUJI Hideaki int m; 660afc154e9SHannes Frederic Sowa bool match_do_rr = false; 66135103d11SAndy Gospodarek 6625e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) 6638067bb8cSIdo Schimmel goto out; 6648067bb8cSIdo Schimmel 665dcd1f572SDavid Ahern if (fib6_ignore_linkdown(rt) && 6665e670d84SDavid Ahern rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && 667d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 66835103d11SAndy Gospodarek goto out; 669554cfb7eSYOSHIFUJI Hideaki 67014895687SDavid Ahern if (fib6_check_expired(rt)) 671f11e6659SDavid S. Miller goto out; 672554cfb7eSYOSHIFUJI Hideaki 673554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6747e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 675afc154e9SHannes Frederic Sowa match_do_rr = true; 676afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6777e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 678f11e6659SDavid S. Miller goto out; 6791da177e4SLinus Torvalds } 680f11e6659SDavid S. Miller 681afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 682afc154e9SHannes Frederic Sowa rt6_probe(rt); 683afc154e9SHannes Frederic Sowa 6847e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 685afc154e9SHannes Frederic Sowa if (m > *mpri) { 686afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 687afc154e9SHannes Frederic Sowa *mpri = m; 688afc154e9SHannes Frederic Sowa match = rt; 689afc154e9SHannes Frederic Sowa } 690f11e6659SDavid S. Miller out: 691f11e6659SDavid S. Miller return match; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6948d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn, 6958d1c802bSDavid Ahern struct fib6_info *leaf, 6968d1c802bSDavid Ahern struct fib6_info *rr_head, 697afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 698afc154e9SHannes Frederic Sowa bool *do_rr) 699f11e6659SDavid S. Miller { 7008d1c802bSDavid Ahern struct fib6_info *rt, *match, *cont; 701f11e6659SDavid S. Miller int mpri = -1; 702f11e6659SDavid S. Miller 703f11e6659SDavid S. Miller match = NULL; 7049fbdcfafSSteffen Klassert cont = NULL; 7058fb11a9aSDavid Ahern for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) { 70693c2fb25SDavid Ahern if (rt->fib6_metric != metric) { 7079fbdcfafSSteffen Klassert cont = rt; 7089fbdcfafSSteffen Klassert break; 7099fbdcfafSSteffen Klassert } 7109fbdcfafSSteffen Klassert 711afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7129fbdcfafSSteffen Klassert } 7139fbdcfafSSteffen Klassert 71466f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 7158fb11a9aSDavid Ahern rt = rcu_dereference(rt->fib6_next)) { 71693c2fb25SDavid Ahern if (rt->fib6_metric != metric) { 7179fbdcfafSSteffen Klassert cont = rt; 7189fbdcfafSSteffen Klassert break; 7199fbdcfafSSteffen Klassert } 7209fbdcfafSSteffen Klassert 7219fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7229fbdcfafSSteffen Klassert } 7239fbdcfafSSteffen Klassert 7249fbdcfafSSteffen Klassert if (match || !cont) 7259fbdcfafSSteffen Klassert return match; 7269fbdcfafSSteffen Klassert 7278fb11a9aSDavid Ahern for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next)) 728afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 729f11e6659SDavid S. Miller 730f11e6659SDavid S. Miller return match; 731f11e6659SDavid S. Miller } 732f11e6659SDavid S. Miller 7338d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn, 7348d1040e8SWei Wang int oif, int strict) 735f11e6659SDavid S. Miller { 7368d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 7378d1c802bSDavid Ahern struct fib6_info *match, *rt0; 738afc154e9SHannes Frederic Sowa bool do_rr = false; 73917ecf590SWei Wang int key_plen; 740f11e6659SDavid S. Miller 741421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 742421842edSDavid Ahern return net->ipv6.fib6_null_entry; 7438d1040e8SWei Wang 74466f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 745f11e6659SDavid S. Miller if (!rt0) 74666f5d6ceSWei Wang rt0 = leaf; 747f11e6659SDavid S. Miller 74817ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 74917ecf590SWei Wang * and fn->leaf does not points to its child's leaf 75017ecf590SWei Wang * (This might happen if all routes under fn are deleted from 75117ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 75217ecf590SWei Wang */ 75393c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 75417ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 75593c2fb25SDavid Ahern if (rt0->fib6_src.plen) 75693c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 75717ecf590SWei Wang #endif 75817ecf590SWei Wang if (fn->fn_bit != key_plen) 759421842edSDavid Ahern return net->ipv6.fib6_null_entry; 76017ecf590SWei Wang 76193c2fb25SDavid Ahern match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict, 762afc154e9SHannes Frederic Sowa &do_rr); 763f11e6659SDavid S. Miller 764afc154e9SHannes Frederic Sowa if (do_rr) { 7658fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 766f11e6659SDavid S. Miller 767554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 76893c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 7698d1040e8SWei Wang next = leaf; 770f11e6659SDavid S. Miller 77166f5d6ceSWei Wang if (next != rt0) { 77293c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 77366f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 77493c2fb25SDavid Ahern if (next->fib6_node) 77566f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 77693c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 77766f5d6ceSWei Wang } 778554cfb7eSYOSHIFUJI Hideaki } 779554cfb7eSYOSHIFUJI Hideaki 780421842edSDavid Ahern return match ? match : net->ipv6.fib6_null_entry; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7838d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt) 7848b9df265SMartin KaFai Lau { 78593c2fb25SDavid Ahern return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7868b9df265SMartin KaFai Lau } 7878b9df265SMartin KaFai Lau 78870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 790b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 79170ceb4f5SYOSHIFUJI Hideaki { 792c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 79370ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 79470ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 79570ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7964bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 7978d1c802bSDavid Ahern struct fib6_info *rt; 79870ceb4f5SYOSHIFUJI Hideaki 79970ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 80070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80170ceb4f5SYOSHIFUJI Hideaki } 80270ceb4f5SYOSHIFUJI Hideaki 80370ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 80470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 80570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 81070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81170ceb4f5SYOSHIFUJI Hideaki } 81270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 81370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 81470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81570ceb4f5SYOSHIFUJI Hideaki } 81670ceb4f5SYOSHIFUJI Hideaki } 81770ceb4f5SYOSHIFUJI Hideaki 81870ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81970ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8203933fc95SJens Rosenboom return -EINVAL; 82170ceb4f5SYOSHIFUJI Hideaki 8224bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 82370ceb4f5SYOSHIFUJI Hideaki 82470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 82570ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82670ceb4f5SYOSHIFUJI Hideaki else { 82770ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82870ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82970ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 83070ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 83170ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 83270ceb4f5SYOSHIFUJI Hideaki } 83370ceb4f5SYOSHIFUJI Hideaki 834f104a567SDuan Jiong if (rinfo->prefix_len == 0) 835afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 836f104a567SDuan Jiong else 837f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 838830218c1SDavid Ahern gwaddr, dev); 83970ceb4f5SYOSHIFUJI Hideaki 84070ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 841afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 84270ceb4f5SYOSHIFUJI Hideaki rt = NULL; 84370ceb4f5SYOSHIFUJI Hideaki } 84470ceb4f5SYOSHIFUJI Hideaki 84570ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 846830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 847830218c1SDavid Ahern dev, pref); 84870ceb4f5SYOSHIFUJI Hideaki else if (rt) 84993c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 85093c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 85170ceb4f5SYOSHIFUJI Hideaki 85270ceb4f5SYOSHIFUJI Hideaki if (rt) { 8531716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 85414895687SDavid Ahern fib6_clean_expires(rt); 8551716a961SGao feng else 85614895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 8571716a961SGao feng 85893531c67SDavid Ahern fib6_info_release(rt); 85970ceb4f5SYOSHIFUJI Hideaki } 86070ceb4f5SYOSHIFUJI Hideaki return 0; 86170ceb4f5SYOSHIFUJI Hideaki } 86270ceb4f5SYOSHIFUJI Hideaki #endif 86370ceb4f5SYOSHIFUJI Hideaki 864ae90d867SDavid Ahern /* 865ae90d867SDavid Ahern * Misc support functions 866ae90d867SDavid Ahern */ 867ae90d867SDavid Ahern 868ae90d867SDavid Ahern /* called with rcu_lock held */ 8698d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt) 870ae90d867SDavid Ahern { 8715e670d84SDavid Ahern struct net_device *dev = rt->fib6_nh.nh_dev; 872ae90d867SDavid Ahern 87393c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 874ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 875ae90d867SDavid Ahern * device if it is a master device, the master device if 876ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 877ae90d867SDavid Ahern */ 878ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 87993c2fb25SDavid Ahern !rt6_need_strict(&rt->fib6_dst.addr)) 880ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 881ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 882ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 883ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 884ae90d867SDavid Ahern * case we want dev returned to be dev 885ae90d867SDavid Ahern */ 886ae90d867SDavid Ahern } 887ae90d867SDavid Ahern 888ae90d867SDavid Ahern return dev; 889ae90d867SDavid Ahern } 890ae90d867SDavid Ahern 8916edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 8926edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 8936edb3c96SDavid Ahern [RTN_UNICAST] = 0, 8946edb3c96SDavid Ahern [RTN_LOCAL] = 0, 8956edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 8966edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 8976edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 8986edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 8996edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 9006edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 9016edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 9026edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 9036edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 9046edb3c96SDavid Ahern }; 9056edb3c96SDavid Ahern 9066edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 9076edb3c96SDavid Ahern { 9086edb3c96SDavid Ahern return fib6_prop[fib6_type]; 9096edb3c96SDavid Ahern } 9106edb3c96SDavid Ahern 9118d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 9123b6761d1SDavid Ahern { 9133b6761d1SDavid Ahern unsigned short flags = 0; 9143b6761d1SDavid Ahern 9153b6761d1SDavid Ahern if (rt->dst_nocount) 9163b6761d1SDavid Ahern flags |= DST_NOCOUNT; 9173b6761d1SDavid Ahern if (rt->dst_nopolicy) 9183b6761d1SDavid Ahern flags |= DST_NOPOLICY; 9193b6761d1SDavid Ahern if (rt->dst_host) 9203b6761d1SDavid Ahern flags |= DST_HOST; 9213b6761d1SDavid Ahern 9223b6761d1SDavid Ahern return flags; 9233b6761d1SDavid Ahern } 9243b6761d1SDavid Ahern 9258d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort) 9266edb3c96SDavid Ahern { 9276edb3c96SDavid Ahern rt->dst.error = ip6_rt_type_to_error(ort->fib6_type); 9286edb3c96SDavid Ahern 9296edb3c96SDavid Ahern switch (ort->fib6_type) { 9306edb3c96SDavid Ahern case RTN_BLACKHOLE: 9316edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 9326edb3c96SDavid Ahern rt->dst.input = dst_discard; 9336edb3c96SDavid Ahern break; 9346edb3c96SDavid Ahern case RTN_PROHIBIT: 9356edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 9366edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 9376edb3c96SDavid Ahern break; 9386edb3c96SDavid Ahern case RTN_THROW: 9396edb3c96SDavid Ahern case RTN_UNREACHABLE: 9406edb3c96SDavid Ahern default: 9416edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 9426edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 9436edb3c96SDavid Ahern break; 9446edb3c96SDavid Ahern } 9456edb3c96SDavid Ahern } 9466edb3c96SDavid Ahern 9478d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort) 9486edb3c96SDavid Ahern { 9493b6761d1SDavid Ahern rt->dst.flags |= fib6_info_dst_flags(ort); 9503b6761d1SDavid Ahern 95193c2fb25SDavid Ahern if (ort->fib6_flags & RTF_REJECT) { 9526edb3c96SDavid Ahern ip6_rt_init_dst_reject(rt, ort); 9536edb3c96SDavid Ahern return; 9546edb3c96SDavid Ahern } 9556edb3c96SDavid Ahern 9566edb3c96SDavid Ahern rt->dst.error = 0; 9576edb3c96SDavid Ahern rt->dst.output = ip6_output; 9586edb3c96SDavid Ahern 9596edb3c96SDavid Ahern if (ort->fib6_type == RTN_LOCAL) { 9606edb3c96SDavid Ahern rt->dst.input = ip6_input; 96193c2fb25SDavid Ahern } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 9626edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 9636edb3c96SDavid Ahern } else { 9646edb3c96SDavid Ahern rt->dst.input = ip6_forward; 9656edb3c96SDavid Ahern } 9666edb3c96SDavid Ahern 9676edb3c96SDavid Ahern if (ort->fib6_nh.nh_lwtstate) { 9686edb3c96SDavid Ahern rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); 9696edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 9706edb3c96SDavid Ahern } 9716edb3c96SDavid Ahern 9726edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 9736edb3c96SDavid Ahern } 9746edb3c96SDavid Ahern 9758d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 976ae90d867SDavid Ahern { 977ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 97893531c67SDavid Ahern fib6_info_hold(from); 979a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 980d4ead6b3SDavid Ahern dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true); 981d4ead6b3SDavid Ahern if (from->fib6_metrics != &dst_default_metrics) { 982d4ead6b3SDavid Ahern rt->dst._metrics |= DST_METRICS_REFCOUNTED; 983d4ead6b3SDavid Ahern refcount_inc(&from->fib6_metrics->refcnt); 984d4ead6b3SDavid Ahern } 985ae90d867SDavid Ahern } 986ae90d867SDavid Ahern 9878d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort) 988ae90d867SDavid Ahern { 989dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ort); 990dcd1f572SDavid Ahern 9916edb3c96SDavid Ahern ip6_rt_init_dst(rt, ort); 9926edb3c96SDavid Ahern 99393c2fb25SDavid Ahern rt->rt6i_dst = ort->fib6_dst; 994dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 9955e670d84SDavid Ahern rt->rt6i_gateway = ort->fib6_nh.nh_gw; 99693c2fb25SDavid Ahern rt->rt6i_flags = ort->fib6_flags; 997ae90d867SDavid Ahern rt6_set_from(rt, ort); 998ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 99993c2fb25SDavid Ahern rt->rt6i_src = ort->fib6_src; 1000ae90d867SDavid Ahern #endif 100193c2fb25SDavid Ahern rt->rt6i_prefsrc = ort->fib6_prefsrc; 10025e670d84SDavid Ahern rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); 1003ae90d867SDavid Ahern } 1004ae90d867SDavid Ahern 1005a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1006a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1007a3c00e46SMartin KaFai Lau { 100866f5d6ceSWei Wang struct fib6_node *pn, *sn; 1009a3c00e46SMartin KaFai Lau while (1) { 1010a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1011a3c00e46SMartin KaFai Lau return NULL; 101266f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 101366f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 101466f5d6ceSWei Wang if (sn && sn != fn) 10156454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1016a3c00e46SMartin KaFai Lau else 1017a3c00e46SMartin KaFai Lau fn = pn; 1018a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1019a3c00e46SMartin KaFai Lau return fn; 1020a3c00e46SMartin KaFai Lau } 1021a3c00e46SMartin KaFai Lau } 1022c71099acSThomas Graf 1023d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 1024d3843fe5SWei Wang bool null_fallback) 1025d3843fe5SWei Wang { 1026d3843fe5SWei Wang struct rt6_info *rt = *prt; 1027d3843fe5SWei Wang 1028d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1029d3843fe5SWei Wang return true; 1030d3843fe5SWei Wang if (null_fallback) { 1031d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1032d3843fe5SWei Wang dst_hold(&rt->dst); 1033d3843fe5SWei Wang } else { 1034d3843fe5SWei Wang rt = NULL; 1035d3843fe5SWei Wang } 1036d3843fe5SWei Wang *prt = rt; 1037d3843fe5SWei Wang return false; 1038d3843fe5SWei Wang } 1039d3843fe5SWei Wang 1040dec9b0e2SDavid Ahern /* called with rcu_lock held */ 10418d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt) 1042dec9b0e2SDavid Ahern { 10433b6761d1SDavid Ahern unsigned short flags = fib6_info_dst_flags(rt); 1044dec9b0e2SDavid Ahern struct net_device *dev = rt->fib6_nh.nh_dev; 1045dec9b0e2SDavid Ahern struct rt6_info *nrt; 1046dec9b0e2SDavid Ahern 104793531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 1048dec9b0e2SDavid Ahern if (nrt) 1049dec9b0e2SDavid Ahern ip6_rt_copy_init(nrt, rt); 1050dec9b0e2SDavid Ahern 1051dec9b0e2SDavid Ahern return nrt; 1052dec9b0e2SDavid Ahern } 1053dec9b0e2SDavid Ahern 10548ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 10558ed67789SDaniel Lezcano struct fib6_table *table, 1056b75cc8f9SDavid Ahern struct flowi6 *fl6, 1057b75cc8f9SDavid Ahern const struct sk_buff *skb, 1058b75cc8f9SDavid Ahern int flags) 10591da177e4SLinus Torvalds { 10608d1c802bSDavid Ahern struct fib6_info *f6i; 10611da177e4SLinus Torvalds struct fib6_node *fn; 106223fb93a4SDavid Ahern struct rt6_info *rt; 10631da177e4SLinus Torvalds 1064b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1065b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 1066b6cdbc85SDavid Ahern 106766f5d6ceSWei Wang rcu_read_lock(); 10686454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1069c71099acSThomas Graf restart: 107023fb93a4SDavid Ahern f6i = rcu_dereference(fn->leaf); 107123fb93a4SDavid Ahern if (!f6i) { 107223fb93a4SDavid Ahern f6i = net->ipv6.fib6_null_entry; 107366f5d6ceSWei Wang } else { 107423fb93a4SDavid Ahern f6i = rt6_device_match(net, f6i, &fl6->saddr, 107566f5d6ceSWei Wang fl6->flowi6_oif, flags); 107693c2fb25SDavid Ahern if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0) 10773b290a31SDavid Ahern f6i = fib6_multipath_select(net, f6i, fl6, 10783b290a31SDavid Ahern fl6->flowi6_oif, skb, 10793b290a31SDavid Ahern flags); 108066f5d6ceSWei Wang } 108123fb93a4SDavid Ahern if (f6i == net->ipv6.fib6_null_entry) { 1082a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1083a3c00e46SMartin KaFai Lau if (fn) 1084a3c00e46SMartin KaFai Lau goto restart; 1085a3c00e46SMartin KaFai Lau } 10862b760fcfSWei Wang 1087d4bea421SDavid Ahern trace_fib6_table_lookup(net, f6i, table, fl6); 1088d4bea421SDavid Ahern 10894c9483b2SDavid S. Miller /* Search through exception table */ 109023fb93a4SDavid Ahern rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr); 109123fb93a4SDavid Ahern if (rt) { 1092d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 1093d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 109423fb93a4SDavid Ahern } else if (f6i == net->ipv6.fib6_null_entry) { 1095dec9b0e2SDavid Ahern rt = net->ipv6.ip6_null_entry; 1096dec9b0e2SDavid Ahern dst_hold(&rt->dst); 109723fb93a4SDavid Ahern } else { 109823fb93a4SDavid Ahern rt = ip6_create_rt_rcu(f6i); 109923fb93a4SDavid Ahern if (!rt) { 110023fb93a4SDavid Ahern rt = net->ipv6.ip6_null_entry; 110123fb93a4SDavid Ahern dst_hold(&rt->dst); 110223fb93a4SDavid Ahern } 1103dec9b0e2SDavid Ahern } 1104d3843fe5SWei Wang 110566f5d6ceSWei Wang rcu_read_unlock(); 1106b811580dSDavid Ahern 11071da177e4SLinus Torvalds return rt; 1108c71099acSThomas Graf } 1109c71099acSThomas Graf 1110ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1111b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1112ea6e574eSFlorian Westphal { 1113b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1114ea6e574eSFlorian Westphal } 1115ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1116ea6e574eSFlorian Westphal 11179acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1118b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1119b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1120c71099acSThomas Graf { 11214c9483b2SDavid S. Miller struct flowi6 fl6 = { 11224c9483b2SDavid S. Miller .flowi6_oif = oif, 11234c9483b2SDavid S. Miller .daddr = *daddr, 1124c71099acSThomas Graf }; 1125c71099acSThomas Graf struct dst_entry *dst; 112677d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1127c71099acSThomas Graf 1128adaa70bbSThomas Graf if (saddr) { 11294c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1130adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1131adaa70bbSThomas Graf } 1132adaa70bbSThomas Graf 1133b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1134c71099acSThomas Graf if (dst->error == 0) 1135c71099acSThomas Graf return (struct rt6_info *) dst; 1136c71099acSThomas Graf 1137c71099acSThomas Graf dst_release(dst); 1138c71099acSThomas Graf 11391da177e4SLinus Torvalds return NULL; 11401da177e4SLinus Torvalds } 11417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 11427159039aSYOSHIFUJI Hideaki 1143c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 11441cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 11451cfb71eeSWei Wang * route is released. 11461cfb71eeSWei Wang * Caller must hold dst before calling it. 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds 11498d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1150333c4301SDavid Ahern struct netlink_ext_ack *extack) 11511da177e4SLinus Torvalds { 11521da177e4SLinus Torvalds int err; 1153c71099acSThomas Graf struct fib6_table *table; 11541da177e4SLinus Torvalds 115593c2fb25SDavid Ahern table = rt->fib6_table; 115666f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1157d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 115866f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds return err; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11638d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 116440e22e8fSThomas Graf { 1165afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1166e715b6d3SFlorian Westphal 1167d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 116840e22e8fSThomas Graf } 116940e22e8fSThomas Graf 11708d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort, 117121efcfa0SEric Dumazet const struct in6_addr *daddr, 1172b71d1d42SEric Dumazet const struct in6_addr *saddr) 11731da177e4SLinus Torvalds { 11744832c30dSDavid Ahern struct net_device *dev; 11751da177e4SLinus Torvalds struct rt6_info *rt; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* 11781da177e4SLinus Torvalds * Clone the route. 11791da177e4SLinus Torvalds */ 11801da177e4SLinus Torvalds 11814832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 118293531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 118383a09abdSMartin KaFai Lau if (!rt) 118483a09abdSMartin KaFai Lau return NULL; 118583a09abdSMartin KaFai Lau 118683a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 11878b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 118883a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 118983a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 119083a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 11918b9df265SMartin KaFai Lau 11928b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 119393c2fb25SDavid Ahern if (ort->fib6_dst.plen != 128 && 119493c2fb25SDavid Ahern ipv6_addr_equal(&ort->fib6_dst.addr, daddr)) 119558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 11961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 11971da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 11984e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 11991da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds #endif 120295a9a5baSYOSHIFUJI Hideaki } 120395a9a5baSYOSHIFUJI Hideaki 1204299d9939SYOSHIFUJI Hideaki return rt; 1205299d9939SYOSHIFUJI Hideaki } 1206299d9939SYOSHIFUJI Hideaki 12078d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt) 1208d52d3997SMartin KaFai Lau { 12093b6761d1SDavid Ahern unsigned short flags = fib6_info_dst_flags(rt); 12104832c30dSDavid Ahern struct net_device *dev; 1211d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1212d52d3997SMartin KaFai Lau 12134832c30dSDavid Ahern rcu_read_lock(); 12144832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 121593531c67SDavid Ahern pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); 12164832c30dSDavid Ahern rcu_read_unlock(); 1217d52d3997SMartin KaFai Lau if (!pcpu_rt) 1218d52d3997SMartin KaFai Lau return NULL; 1219d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1220d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1221d52d3997SMartin KaFai Lau return pcpu_rt; 1222d52d3997SMartin KaFai Lau } 1223d52d3997SMartin KaFai Lau 122466f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 12258d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt) 1226d52d3997SMartin KaFai Lau { 1227a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1228d52d3997SMartin KaFai Lau 1229d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1230d52d3997SMartin KaFai Lau pcpu_rt = *p; 1231d52d3997SMartin KaFai Lau 1232d4ead6b3SDavid Ahern if (pcpu_rt) 1233d4ead6b3SDavid Ahern ip6_hold_safe(NULL, &pcpu_rt, false); 1234d3843fe5SWei Wang 1235a73e4195SMartin KaFai Lau return pcpu_rt; 1236a73e4195SMartin KaFai Lau } 1237a73e4195SMartin KaFai Lau 1238afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 12398d1c802bSDavid Ahern struct fib6_info *rt) 1240a73e4195SMartin KaFai Lau { 1241a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1242d52d3997SMartin KaFai Lau 1243d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1244d52d3997SMartin KaFai Lau if (!pcpu_rt) { 12459c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 12469c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1247d52d3997SMartin KaFai Lau } 1248d52d3997SMartin KaFai Lau 1249a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1250a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1251d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1252951f788aSEric Dumazet BUG_ON(prev); 1253a94b9367SWei Wang 1254d52d3997SMartin KaFai Lau return pcpu_rt; 1255d52d3997SMartin KaFai Lau } 1256d52d3997SMartin KaFai Lau 125735732d01SWei Wang /* exception hash table implementation 125835732d01SWei Wang */ 125935732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 126035732d01SWei Wang 126135732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 126235732d01SWei Wang * Caller must hold rt6_exception_lock 126335732d01SWei Wang */ 126435732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 126535732d01SWei Wang struct rt6_exception *rt6_ex) 126635732d01SWei Wang { 1267b2427e67SColin Ian King struct net *net; 126881eb8447SWei Wang 126935732d01SWei Wang if (!bucket || !rt6_ex) 127035732d01SWei Wang return; 1271b2427e67SColin Ian King 1272b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 127335732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 127477634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 127535732d01SWei Wang kfree_rcu(rt6_ex, rcu); 127635732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 127735732d01SWei Wang bucket->depth--; 127881eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache--; 127935732d01SWei Wang } 128035732d01SWei Wang 128135732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 128235732d01SWei Wang * Caller must hold rt6_exception_lock 128335732d01SWei Wang */ 128435732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 128535732d01SWei Wang { 128635732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 128735732d01SWei Wang 128835732d01SWei Wang if (!bucket) 128935732d01SWei Wang return; 129035732d01SWei Wang 129135732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 129235732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 129335732d01SWei Wang oldest = rt6_ex; 129435732d01SWei Wang } 129535732d01SWei Wang rt6_remove_exception(bucket, oldest); 129635732d01SWei Wang } 129735732d01SWei Wang 129835732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 129935732d01SWei Wang const struct in6_addr *src) 130035732d01SWei Wang { 130135732d01SWei Wang static u32 seed __read_mostly; 130235732d01SWei Wang u32 val; 130335732d01SWei Wang 130435732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 130535732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 130635732d01SWei Wang 130735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 130835732d01SWei Wang if (src) 130935732d01SWei Wang val = jhash(src, sizeof(*src), val); 131035732d01SWei Wang #endif 131135732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 131235732d01SWei Wang } 131335732d01SWei Wang 131435732d01SWei Wang /* Helper function to find the cached rt in the hash table 131535732d01SWei Wang * and update bucket pointer to point to the bucket for this 131635732d01SWei Wang * (daddr, saddr) pair 131735732d01SWei Wang * Caller must hold rt6_exception_lock 131835732d01SWei Wang */ 131935732d01SWei Wang static struct rt6_exception * 132035732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 132135732d01SWei Wang const struct in6_addr *daddr, 132235732d01SWei Wang const struct in6_addr *saddr) 132335732d01SWei Wang { 132435732d01SWei Wang struct rt6_exception *rt6_ex; 132535732d01SWei Wang u32 hval; 132635732d01SWei Wang 132735732d01SWei Wang if (!(*bucket) || !daddr) 132835732d01SWei Wang return NULL; 132935732d01SWei Wang 133035732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 133135732d01SWei Wang *bucket += hval; 133235732d01SWei Wang 133335732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 133435732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 133535732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 133635732d01SWei Wang 133735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 133835732d01SWei Wang if (matched && saddr) 133935732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 134035732d01SWei Wang #endif 134135732d01SWei Wang if (matched) 134235732d01SWei Wang return rt6_ex; 134335732d01SWei Wang } 134435732d01SWei Wang return NULL; 134535732d01SWei Wang } 134635732d01SWei Wang 134735732d01SWei Wang /* Helper function to find the cached rt in the hash table 134835732d01SWei Wang * and update bucket pointer to point to the bucket for this 134935732d01SWei Wang * (daddr, saddr) pair 135035732d01SWei Wang * Caller must hold rcu_read_lock() 135135732d01SWei Wang */ 135235732d01SWei Wang static struct rt6_exception * 135335732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 135435732d01SWei Wang const struct in6_addr *daddr, 135535732d01SWei Wang const struct in6_addr *saddr) 135635732d01SWei Wang { 135735732d01SWei Wang struct rt6_exception *rt6_ex; 135835732d01SWei Wang u32 hval; 135935732d01SWei Wang 136035732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 136135732d01SWei Wang 136235732d01SWei Wang if (!(*bucket) || !daddr) 136335732d01SWei Wang return NULL; 136435732d01SWei Wang 136535732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 136635732d01SWei Wang *bucket += hval; 136735732d01SWei Wang 136835732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 136935732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 137035732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 137135732d01SWei Wang 137235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 137335732d01SWei Wang if (matched && saddr) 137435732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 137535732d01SWei Wang #endif 137635732d01SWei Wang if (matched) 137735732d01SWei Wang return rt6_ex; 137835732d01SWei Wang } 137935732d01SWei Wang return NULL; 138035732d01SWei Wang } 138135732d01SWei Wang 13828d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt) 138335732d01SWei Wang { 1384d4ead6b3SDavid Ahern unsigned int mtu; 1385d4ead6b3SDavid Ahern 1386dcd1f572SDavid Ahern if (rt->fib6_pmtu) { 1387dcd1f572SDavid Ahern mtu = rt->fib6_pmtu; 1388dcd1f572SDavid Ahern } else { 1389dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 1390dcd1f572SDavid Ahern struct inet6_dev *idev; 1391dcd1f572SDavid Ahern 1392dcd1f572SDavid Ahern rcu_read_lock(); 1393dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1394dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1395dcd1f572SDavid Ahern rcu_read_unlock(); 1396dcd1f572SDavid Ahern } 1397dcd1f572SDavid Ahern 1398d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1399d4ead6b3SDavid Ahern 1400d4ead6b3SDavid Ahern return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu); 1401d4ead6b3SDavid Ahern } 1402d4ead6b3SDavid Ahern 140335732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 14048d1c802bSDavid Ahern struct fib6_info *ort) 140535732d01SWei Wang { 14065e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 140735732d01SWei Wang struct rt6_exception_bucket *bucket; 140835732d01SWei Wang struct in6_addr *src_key = NULL; 140935732d01SWei Wang struct rt6_exception *rt6_ex; 141035732d01SWei Wang int err = 0; 141135732d01SWei Wang 141235732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 141335732d01SWei Wang 141435732d01SWei Wang if (ort->exception_bucket_flushed) { 141535732d01SWei Wang err = -EINVAL; 141635732d01SWei Wang goto out; 141735732d01SWei Wang } 141835732d01SWei Wang 141935732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 142035732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 142135732d01SWei Wang if (!bucket) { 142235732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 142335732d01SWei Wang GFP_ATOMIC); 142435732d01SWei Wang if (!bucket) { 142535732d01SWei Wang err = -ENOMEM; 142635732d01SWei Wang goto out; 142735732d01SWei Wang } 142835732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 142935732d01SWei Wang } 143035732d01SWei Wang 143135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143235732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 143335732d01SWei Wang * and exception table is indexed by a hash of 143435732d01SWei Wang * both rt6i_dst and rt6i_src. 143535732d01SWei Wang * Otherwise, the exception table is indexed by 143635732d01SWei Wang * a hash of only rt6i_dst. 143735732d01SWei Wang */ 143893c2fb25SDavid Ahern if (ort->fib6_src.plen) 143935732d01SWei Wang src_key = &nrt->rt6i_src.addr; 144035732d01SWei Wang #endif 144160006a48SWei Wang 144260006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 144360006a48SWei Wang * in rt6_remove_prefsrc() 144460006a48SWei Wang */ 144593c2fb25SDavid Ahern nrt->rt6i_prefsrc = ort->fib6_prefsrc; 1446f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1447f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1448f5bbe7eeSWei Wang * is less than ort's mtu value. 1449f5bbe7eeSWei Wang */ 1450d4ead6b3SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) { 1451f5bbe7eeSWei Wang err = -EINVAL; 1452f5bbe7eeSWei Wang goto out; 1453f5bbe7eeSWei Wang } 145460006a48SWei Wang 145535732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 145635732d01SWei Wang src_key); 145735732d01SWei Wang if (rt6_ex) 145835732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 145935732d01SWei Wang 146035732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 146135732d01SWei Wang if (!rt6_ex) { 146235732d01SWei Wang err = -ENOMEM; 146335732d01SWei Wang goto out; 146435732d01SWei Wang } 146535732d01SWei Wang rt6_ex->rt6i = nrt; 146635732d01SWei Wang rt6_ex->stamp = jiffies; 146735732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 146835732d01SWei Wang bucket->depth++; 146981eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 147035732d01SWei Wang 147135732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 147235732d01SWei Wang rt6_exception_remove_oldest(bucket); 147335732d01SWei Wang 147435732d01SWei Wang out: 147535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 147635732d01SWei Wang 147735732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1478b886d5f2SPaolo Abeni if (!err) { 147993c2fb25SDavid Ahern spin_lock_bh(&ort->fib6_table->tb6_lock); 14807aef6859SDavid Ahern fib6_update_sernum(net, ort); 148193c2fb25SDavid Ahern spin_unlock_bh(&ort->fib6_table->tb6_lock); 1482b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1483b886d5f2SPaolo Abeni } 148435732d01SWei Wang 148535732d01SWei Wang return err; 148635732d01SWei Wang } 148735732d01SWei Wang 14888d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt) 148935732d01SWei Wang { 149035732d01SWei Wang struct rt6_exception_bucket *bucket; 149135732d01SWei Wang struct rt6_exception *rt6_ex; 149235732d01SWei Wang struct hlist_node *tmp; 149335732d01SWei Wang int i; 149435732d01SWei Wang 149535732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 149635732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 149735732d01SWei Wang rt->exception_bucket_flushed = 1; 149835732d01SWei Wang 149935732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 150035732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 150135732d01SWei Wang if (!bucket) 150235732d01SWei Wang goto out; 150335732d01SWei Wang 150435732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 150535732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 150635732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 150735732d01SWei Wang WARN_ON_ONCE(bucket->depth); 150835732d01SWei Wang bucket++; 150935732d01SWei Wang } 151035732d01SWei Wang 151135732d01SWei Wang out: 151235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 151335732d01SWei Wang } 151435732d01SWei Wang 151535732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 151635732d01SWei Wang * Caller has to hold rcu_read_lock() 151735732d01SWei Wang */ 15188d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, 151935732d01SWei Wang struct in6_addr *daddr, 152035732d01SWei Wang struct in6_addr *saddr) 152135732d01SWei Wang { 152235732d01SWei Wang struct rt6_exception_bucket *bucket; 152335732d01SWei Wang struct in6_addr *src_key = NULL; 152435732d01SWei Wang struct rt6_exception *rt6_ex; 152535732d01SWei Wang struct rt6_info *res = NULL; 152635732d01SWei Wang 152735732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 152835732d01SWei Wang 152935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 153035732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 153135732d01SWei Wang * and exception table is indexed by a hash of 153235732d01SWei Wang * both rt6i_dst and rt6i_src. 153335732d01SWei Wang * Otherwise, the exception table is indexed by 153435732d01SWei Wang * a hash of only rt6i_dst. 153535732d01SWei Wang */ 153693c2fb25SDavid Ahern if (rt->fib6_src.plen) 153735732d01SWei Wang src_key = saddr; 153835732d01SWei Wang #endif 153935732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 154035732d01SWei Wang 154135732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 154235732d01SWei Wang res = rt6_ex->rt6i; 154335732d01SWei Wang 154435732d01SWei Wang return res; 154535732d01SWei Wang } 154635732d01SWei Wang 154735732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 154823fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 154935732d01SWei Wang { 155035732d01SWei Wang struct rt6_exception_bucket *bucket; 155135732d01SWei Wang struct in6_addr *src_key = NULL; 155235732d01SWei Wang struct rt6_exception *rt6_ex; 15538a14e46fSDavid Ahern struct fib6_info *from; 155435732d01SWei Wang int err; 155535732d01SWei Wang 1556091311deSEric Dumazet from = rcu_dereference(rt->from); 155735732d01SWei Wang if (!from || 1558442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 155935732d01SWei Wang return -EINVAL; 156035732d01SWei Wang 156135732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 156235732d01SWei Wang return -ENOENT; 156335732d01SWei Wang 156435732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 156535732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 156635732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 156735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 156835732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 156935732d01SWei Wang * and exception table is indexed by a hash of 157035732d01SWei Wang * both rt6i_dst and rt6i_src. 157135732d01SWei Wang * Otherwise, the exception table is indexed by 157235732d01SWei Wang * a hash of only rt6i_dst. 157335732d01SWei Wang */ 157493c2fb25SDavid Ahern if (from->fib6_src.plen) 157535732d01SWei Wang src_key = &rt->rt6i_src.addr; 157635732d01SWei Wang #endif 157735732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 157835732d01SWei Wang &rt->rt6i_dst.addr, 157935732d01SWei Wang src_key); 158035732d01SWei Wang if (rt6_ex) { 158135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 158235732d01SWei Wang err = 0; 158335732d01SWei Wang } else { 158435732d01SWei Wang err = -ENOENT; 158535732d01SWei Wang } 158635732d01SWei Wang 158735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 158835732d01SWei Wang return err; 158935732d01SWei Wang } 159035732d01SWei Wang 159135732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 159235732d01SWei Wang * refresh its stamp 159335732d01SWei Wang */ 159435732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 159535732d01SWei Wang { 159635732d01SWei Wang struct rt6_exception_bucket *bucket; 15978d1c802bSDavid Ahern struct fib6_info *from = rt->from; 159835732d01SWei Wang struct in6_addr *src_key = NULL; 159935732d01SWei Wang struct rt6_exception *rt6_ex; 160035732d01SWei Wang 160135732d01SWei Wang if (!from || 1602442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 160335732d01SWei Wang return; 160435732d01SWei Wang 160535732d01SWei Wang rcu_read_lock(); 160635732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 160735732d01SWei Wang 160835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 160935732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 161035732d01SWei Wang * and exception table is indexed by a hash of 161135732d01SWei Wang * both rt6i_dst and rt6i_src. 161235732d01SWei Wang * Otherwise, the exception table is indexed by 161335732d01SWei Wang * a hash of only rt6i_dst. 161435732d01SWei Wang */ 161593c2fb25SDavid Ahern if (from->fib6_src.plen) 161635732d01SWei Wang src_key = &rt->rt6i_src.addr; 161735732d01SWei Wang #endif 161835732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 161935732d01SWei Wang &rt->rt6i_dst.addr, 162035732d01SWei Wang src_key); 162135732d01SWei Wang if (rt6_ex) 162235732d01SWei Wang rt6_ex->stamp = jiffies; 162335732d01SWei Wang 162435732d01SWei Wang rcu_read_unlock(); 162535732d01SWei Wang } 162635732d01SWei Wang 16278d1c802bSDavid Ahern static void rt6_exceptions_remove_prefsrc(struct fib6_info *rt) 162860006a48SWei Wang { 162960006a48SWei Wang struct rt6_exception_bucket *bucket; 163060006a48SWei Wang struct rt6_exception *rt6_ex; 163160006a48SWei Wang int i; 163260006a48SWei Wang 163360006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 163460006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 163560006a48SWei Wang 163660006a48SWei Wang if (bucket) { 163760006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 163860006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 163960006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 164060006a48SWei Wang } 164160006a48SWei Wang bucket++; 164260006a48SWei Wang } 164360006a48SWei Wang } 164460006a48SWei Wang } 164560006a48SWei Wang 1646e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1647e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1648e9fa1495SStefano Brivio { 1649e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1650e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1651e9fa1495SStefano Brivio * reflect PMTU decreases. 1652e9fa1495SStefano Brivio * 1653e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1654e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1655e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1656e9fa1495SStefano Brivio * handle this. 1657e9fa1495SStefano Brivio */ 1658e9fa1495SStefano Brivio 1659e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1660e9fa1495SStefano Brivio return true; 1661e9fa1495SStefano Brivio 1662e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1663e9fa1495SStefano Brivio return true; 1664e9fa1495SStefano Brivio 1665e9fa1495SStefano Brivio return false; 1666e9fa1495SStefano Brivio } 1667e9fa1495SStefano Brivio 1668e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 16698d1c802bSDavid Ahern struct fib6_info *rt, int mtu) 1670f5bbe7eeSWei Wang { 1671f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1672f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1673f5bbe7eeSWei Wang int i; 1674f5bbe7eeSWei Wang 1675f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1676f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1677f5bbe7eeSWei Wang 1678e9fa1495SStefano Brivio if (!bucket) 1679e9fa1495SStefano Brivio return; 1680e9fa1495SStefano Brivio 1681f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1682f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1683f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1684e9fa1495SStefano Brivio 1685e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 1686d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 1687f5bbe7eeSWei Wang * been updated. 1688f5bbe7eeSWei Wang */ 1689d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 1690e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 1691d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 1692f5bbe7eeSWei Wang } 1693f5bbe7eeSWei Wang bucket++; 1694f5bbe7eeSWei Wang } 1695f5bbe7eeSWei Wang } 1696f5bbe7eeSWei Wang 1697b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1698b16cb459SWei Wang 16998d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt, 1700b16cb459SWei Wang struct in6_addr *gateway) 1701b16cb459SWei Wang { 1702b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1703b16cb459SWei Wang struct rt6_exception *rt6_ex; 1704b16cb459SWei Wang struct hlist_node *tmp; 1705b16cb459SWei Wang int i; 1706b16cb459SWei Wang 1707b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1708b16cb459SWei Wang return; 1709b16cb459SWei Wang 1710b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1711b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1712b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1713b16cb459SWei Wang 1714b16cb459SWei Wang if (bucket) { 1715b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1716b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1717b16cb459SWei Wang &bucket->chain, hlist) { 1718b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1719b16cb459SWei Wang 1720b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1721b16cb459SWei Wang RTF_CACHE_GATEWAY && 1722b16cb459SWei Wang ipv6_addr_equal(gateway, 1723b16cb459SWei Wang &entry->rt6i_gateway)) { 1724b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1725b16cb459SWei Wang } 1726b16cb459SWei Wang } 1727b16cb459SWei Wang bucket++; 1728b16cb459SWei Wang } 1729b16cb459SWei Wang } 1730b16cb459SWei Wang 1731b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1732b16cb459SWei Wang } 1733b16cb459SWei Wang 1734c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1735c757faa8SWei Wang struct rt6_exception *rt6_ex, 1736c757faa8SWei Wang struct fib6_gc_args *gc_args, 1737c757faa8SWei Wang unsigned long now) 1738c757faa8SWei Wang { 1739c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1740c757faa8SWei Wang 17411859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 17421859bac0SPaolo Abeni * even if others have still references to them, so that on next 17431859bac0SPaolo Abeni * dst_check() such references can be dropped. 17441859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 17451859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 17461859bac0SPaolo Abeni */ 174731afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 174831afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1749c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1750c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1751c757faa8SWei Wang return; 175231afeb42SWei Wang } 175331afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 175431afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 175531afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 175631afeb42SWei Wang return; 175731afeb42SWei Wang } 175831afeb42SWei Wang 175931afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 1760c757faa8SWei Wang struct neighbour *neigh; 1761c757faa8SWei Wang __u8 neigh_flags = 0; 1762c757faa8SWei Wang 17631bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 17641bfa26ffSEric Dumazet if (neigh) 1765c757faa8SWei Wang neigh_flags = neigh->flags; 17661bfa26ffSEric Dumazet 1767c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1768c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1769c757faa8SWei Wang rt); 1770c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1771c757faa8SWei Wang return; 1772c757faa8SWei Wang } 1773c757faa8SWei Wang } 177431afeb42SWei Wang 1775c757faa8SWei Wang gc_args->more++; 1776c757faa8SWei Wang } 1777c757faa8SWei Wang 17788d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt, 1779c757faa8SWei Wang struct fib6_gc_args *gc_args, 1780c757faa8SWei Wang unsigned long now) 1781c757faa8SWei Wang { 1782c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1783c757faa8SWei Wang struct rt6_exception *rt6_ex; 1784c757faa8SWei Wang struct hlist_node *tmp; 1785c757faa8SWei Wang int i; 1786c757faa8SWei Wang 1787c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1788c757faa8SWei Wang return; 1789c757faa8SWei Wang 17901bfa26ffSEric Dumazet rcu_read_lock_bh(); 17911bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 1792c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1793c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1794c757faa8SWei Wang 1795c757faa8SWei Wang if (bucket) { 1796c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1797c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1798c757faa8SWei Wang &bucket->chain, hlist) { 1799c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1800c757faa8SWei Wang gc_args, now); 1801c757faa8SWei Wang } 1802c757faa8SWei Wang bucket++; 1803c757faa8SWei Wang } 1804c757faa8SWei Wang } 18051bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 18061bfa26ffSEric Dumazet rcu_read_unlock_bh(); 1807c757faa8SWei Wang } 1808c757faa8SWei Wang 18091d053da9SDavid Ahern /* must be called with rcu lock held */ 18101d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table, 18111d053da9SDavid Ahern int oif, struct flowi6 *fl6, int strict) 18121da177e4SLinus Torvalds { 1813367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 18148d1c802bSDavid Ahern struct fib6_info *f6i; 18151da177e4SLinus Torvalds 18166454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1817367efcb9SMartin KaFai Lau saved_fn = fn; 18181da177e4SLinus Torvalds 1819ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1820ca254490SDavid Ahern oif = 0; 1821ca254490SDavid Ahern 1822a3c00e46SMartin KaFai Lau redo_rt6_select: 182323fb93a4SDavid Ahern f6i = rt6_select(net, fn, oif, strict); 182423fb93a4SDavid Ahern if (f6i == net->ipv6.fib6_null_entry) { 1825a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1826a3c00e46SMartin KaFai Lau if (fn) 1827a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1828367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1829367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1830367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1831367efcb9SMartin KaFai Lau fn = saved_fn; 1832367efcb9SMartin KaFai Lau goto redo_rt6_select; 1833367efcb9SMartin KaFai Lau } 1834a3c00e46SMartin KaFai Lau } 1835a3c00e46SMartin KaFai Lau 1836d4bea421SDavid Ahern trace_fib6_table_lookup(net, f6i, table, fl6); 1837d52d3997SMartin KaFai Lau 18381d053da9SDavid Ahern return f6i; 18391d053da9SDavid Ahern } 18401d053da9SDavid Ahern 18411d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 18421d053da9SDavid Ahern int oif, struct flowi6 *fl6, 18431d053da9SDavid Ahern const struct sk_buff *skb, int flags) 18441d053da9SDavid Ahern { 18451d053da9SDavid Ahern struct fib6_info *f6i; 18461d053da9SDavid Ahern struct rt6_info *rt; 18471d053da9SDavid Ahern int strict = 0; 18481d053da9SDavid Ahern 18491d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 18501d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 18511d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 18521d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 18531d053da9SDavid Ahern 18541d053da9SDavid Ahern rcu_read_lock(); 18551d053da9SDavid Ahern 18561d053da9SDavid Ahern f6i = fib6_table_lookup(net, table, oif, fl6, strict); 18571d053da9SDavid Ahern if (f6i->fib6_nsiblings) 18581d053da9SDavid Ahern f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict); 18591d053da9SDavid Ahern 186023fb93a4SDavid Ahern if (f6i == net->ipv6.fib6_null_entry) { 1861421842edSDavid Ahern rt = net->ipv6.ip6_null_entry; 186266f5d6ceSWei Wang rcu_read_unlock(); 1863d3843fe5SWei Wang dst_hold(&rt->dst); 1864d3843fe5SWei Wang return rt; 1865d3843fe5SWei Wang } 186623fb93a4SDavid Ahern 186723fb93a4SDavid Ahern /*Search through exception table */ 186823fb93a4SDavid Ahern rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr); 186923fb93a4SDavid Ahern if (rt) { 1870d4ead6b3SDavid Ahern if (ip6_hold_safe(net, &rt, true)) 18711da177e4SLinus Torvalds dst_use_noref(&rt->dst, jiffies); 1872d4ead6b3SDavid Ahern 187366f5d6ceSWei Wang rcu_read_unlock(); 1874d52d3997SMartin KaFai Lau return rt; 18753da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 187693c2fb25SDavid Ahern !(f6i->fib6_flags & RTF_GATEWAY))) { 18773da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 18783da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 18793da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 18803da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 18813da59bd9SMartin KaFai Lau */ 18823da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 18833da59bd9SMartin KaFai Lau 188423fb93a4SDavid Ahern uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL); 1885d52d3997SMartin KaFai Lau 18864d85cd0cSDavid Ahern rcu_read_unlock(); 18873da59bd9SMartin KaFai Lau 18881cfb71eeSWei Wang if (uncached_rt) { 18891cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 18901cfb71eeSWei Wang * No need for another dst_hold() 18911cfb71eeSWei Wang */ 18928d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 189381eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 18941cfb71eeSWei Wang } else { 18953da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 18963da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 18971cfb71eeSWei Wang } 1898b811580dSDavid Ahern 18993da59bd9SMartin KaFai Lau return uncached_rt; 1900d52d3997SMartin KaFai Lau } else { 1901d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1902d52d3997SMartin KaFai Lau 1903d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1904d52d3997SMartin KaFai Lau 1905951f788aSEric Dumazet local_bh_disable(); 190623fb93a4SDavid Ahern pcpu_rt = rt6_get_pcpu_route(f6i); 1907d52d3997SMartin KaFai Lau 190893531c67SDavid Ahern if (!pcpu_rt) 190923fb93a4SDavid Ahern pcpu_rt = rt6_make_pcpu_route(net, f6i); 191093531c67SDavid Ahern 1911951f788aSEric Dumazet local_bh_enable(); 1912951f788aSEric Dumazet rcu_read_unlock(); 1913d4bea421SDavid Ahern 1914d52d3997SMartin KaFai Lau return pcpu_rt; 1915d52d3997SMartin KaFai Lau } 1916c71099acSThomas Graf } 19179ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1918c71099acSThomas Graf 1919b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 1920b75cc8f9SDavid Ahern struct fib6_table *table, 1921b75cc8f9SDavid Ahern struct flowi6 *fl6, 1922b75cc8f9SDavid Ahern const struct sk_buff *skb, 1923b75cc8f9SDavid Ahern int flags) 19244acad72dSPavel Emelyanov { 1925b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 19264acad72dSPavel Emelyanov } 19274acad72dSPavel Emelyanov 1928d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 192972331bc0SShmulik Ladkani struct net_device *dev, 1930b75cc8f9SDavid Ahern struct flowi6 *fl6, 1931b75cc8f9SDavid Ahern const struct sk_buff *skb, 1932b75cc8f9SDavid Ahern int flags) 193372331bc0SShmulik Ladkani { 193472331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 193572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 193672331bc0SShmulik Ladkani 1937b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 193872331bc0SShmulik Ladkani } 1939d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 194072331bc0SShmulik Ladkani 194123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 19425e5d6fedSRoopa Prabhu struct flow_keys *keys, 19435e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 194423aebdacSJakub Sitnicki { 194523aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 194623aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 19475e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 194823aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 194923aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 195023aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 1951cea67a2dSEric Dumazet struct icmp6hdr _icmph; 195223aebdacSJakub Sitnicki 195323aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 195423aebdacSJakub Sitnicki goto out; 195523aebdacSJakub Sitnicki 1956cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 1957cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 1958cea67a2dSEric Dumazet if (!icmph) 1959cea67a2dSEric Dumazet goto out; 1960cea67a2dSEric Dumazet 196123aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 196223aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 196323aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 196423aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 196523aebdacSJakub Sitnicki goto out; 196623aebdacSJakub Sitnicki 196723aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 196823aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 196923aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 197023aebdacSJakub Sitnicki if (!inner_iph) 197123aebdacSJakub Sitnicki goto out; 197223aebdacSJakub Sitnicki 197323aebdacSJakub Sitnicki key_iph = inner_iph; 19745e5d6fedSRoopa Prabhu _flkeys = NULL; 197523aebdacSJakub Sitnicki out: 19765e5d6fedSRoopa Prabhu if (_flkeys) { 19775e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 19785e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 19795e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 19805e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 19815e5d6fedSRoopa Prabhu } else { 198223aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 198323aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 1984fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 198523aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 198623aebdacSJakub Sitnicki } 19875e5d6fedSRoopa Prabhu } 198823aebdacSJakub Sitnicki 198923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 1990b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 1991b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 199223aebdacSJakub Sitnicki { 199323aebdacSJakub Sitnicki struct flow_keys hash_keys; 19949a2a537aSDavid Ahern u32 mhash; 199523aebdacSJakub Sitnicki 1996bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 1997b4bac172SDavid Ahern case 0: 19986f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 19996f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 20009a2a537aSDavid Ahern if (skb) { 20015e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 20029a2a537aSDavid Ahern } else { 20039a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 20049a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2005fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 20069a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 200723aebdacSJakub Sitnicki } 2008b4bac172SDavid Ahern break; 2009b4bac172SDavid Ahern case 1: 2010b4bac172SDavid Ahern if (skb) { 2011b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2012b4bac172SDavid Ahern struct flow_keys keys; 2013b4bac172SDavid Ahern 2014b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2015b4bac172SDavid Ahern if (skb->l4_hash) 2016b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2017b4bac172SDavid Ahern 2018b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2019b4bac172SDavid Ahern 2020b4bac172SDavid Ahern if (!flkeys) { 2021b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2022b4bac172SDavid Ahern flkeys = &keys; 2023b4bac172SDavid Ahern } 2024b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2025b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2026b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2027b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2028b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2029b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2030b4bac172SDavid Ahern } else { 2031b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2032b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2033b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2034b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2035b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2036b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2037b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2038b4bac172SDavid Ahern } 2039b4bac172SDavid Ahern break; 2040b4bac172SDavid Ahern } 20419a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 204223aebdacSJakub Sitnicki 20439a2a537aSDavid Ahern return mhash >> 1; 204423aebdacSJakub Sitnicki } 204523aebdacSJakub Sitnicki 2046c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2047c71099acSThomas Graf { 2048b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2049c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 2050adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 2051904af04dSJiri Benc struct ip_tunnel_info *tun_info; 20524c9483b2SDavid S. Miller struct flowi6 fl6 = { 2053e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 20544c9483b2SDavid S. Miller .daddr = iph->daddr, 20554c9483b2SDavid S. Miller .saddr = iph->saddr, 20566502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 20574c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 20584c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2059c71099acSThomas Graf }; 20605e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2061adaa70bbSThomas Graf 2062904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 206346fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2064904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 20655e5d6fedSRoopa Prabhu 20665e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 20675e5d6fedSRoopa Prabhu flkeys = &_flkeys; 20685e5d6fedSRoopa Prabhu 206923aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2070b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 207106e9d040SJiri Benc skb_dst_drop(skb); 2072b75cc8f9SDavid Ahern skb_dst_set(skb, 2073b75cc8f9SDavid Ahern ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); 2074c71099acSThomas Graf } 2075c71099acSThomas Graf 2076b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2077b75cc8f9SDavid Ahern struct fib6_table *table, 2078b75cc8f9SDavid Ahern struct flowi6 *fl6, 2079b75cc8f9SDavid Ahern const struct sk_buff *skb, 2080b75cc8f9SDavid Ahern int flags) 2081c71099acSThomas Graf { 2082b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2083c71099acSThomas Graf } 2084c71099acSThomas Graf 20856f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 20866f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2087c71099acSThomas Graf { 2088d46a9d67SDavid Ahern bool any_src; 2089c71099acSThomas Graf 20904c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 20914c1feac5SDavid Ahern struct dst_entry *dst; 20924c1feac5SDavid Ahern 20934c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2094ca254490SDavid Ahern if (dst) 2095ca254490SDavid Ahern return dst; 20964c1feac5SDavid Ahern } 2097ca254490SDavid Ahern 20981fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 20994dc27d1cSDavid McCullough 2100d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2101741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2102d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 210377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2104c71099acSThomas Graf 2105d46a9d67SDavid Ahern if (!any_src) 2106adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 21070c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 21080c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2109adaa70bbSThomas Graf 2110b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 21111da177e4SLinus Torvalds } 21126f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 21131da177e4SLinus Torvalds 21142774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 211514e50e57SDavid S. Miller { 21165c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 21171dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 211814e50e57SDavid S. Miller struct dst_entry *new = NULL; 211914e50e57SDavid S. Miller 21201dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 212162cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 212214e50e57SDavid S. Miller if (rt) { 21230a1f5962SMartin KaFai Lau rt6_info_init(rt); 212481eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 21250a1f5962SMartin KaFai Lau 2126d8d1f30bSChangli Gao new = &rt->dst; 212714e50e57SDavid S. Miller new->__use = 1; 2128352e512cSHerbert Xu new->input = dst_discard; 2129ede2059dSEric W. Biederman new->output = dst_discard_out; 213014e50e57SDavid S. Miller 2131defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 213214e50e57SDavid S. Miller 21331dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 21344e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 21350a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 213614e50e57SDavid S. Miller 213714e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 213814e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 213914e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 214014e50e57SDavid S. Miller #endif 214114e50e57SDavid S. Miller } 214214e50e57SDavid S. Miller 214369ead7afSDavid S. Miller dst_release(dst_orig); 214469ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 214514e50e57SDavid S. Miller } 214614e50e57SDavid S. Miller 21471da177e4SLinus Torvalds /* 21481da177e4SLinus Torvalds * Destination cache support functions 21491da177e4SLinus Torvalds */ 21501da177e4SLinus Torvalds 21518d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 21523da59bd9SMartin KaFai Lau { 215336143645SSteffen Klassert u32 rt_cookie = 0; 2154c5cff856SWei Wang 21558ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 215693531c67SDavid Ahern return false; 215793531c67SDavid Ahern 215893531c67SDavid Ahern if (fib6_check_expired(f6i)) 215993531c67SDavid Ahern return false; 216093531c67SDavid Ahern 216193531c67SDavid Ahern return true; 216293531c67SDavid Ahern } 216393531c67SDavid Ahern 2164a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2165a68886a6SDavid Ahern struct fib6_info *from, 2166a68886a6SDavid Ahern u32 cookie) 21673da59bd9SMartin KaFai Lau { 2168c5cff856SWei Wang u32 rt_cookie = 0; 2169c5cff856SWei Wang 2170a68886a6SDavid Ahern if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || 217193531c67SDavid Ahern rt_cookie != cookie) 21723da59bd9SMartin KaFai Lau return NULL; 21733da59bd9SMartin KaFai Lau 21743da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 21753da59bd9SMartin KaFai Lau return NULL; 21763da59bd9SMartin KaFai Lau 21773da59bd9SMartin KaFai Lau return &rt->dst; 21783da59bd9SMartin KaFai Lau } 21793da59bd9SMartin KaFai Lau 2180a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2181a68886a6SDavid Ahern struct fib6_info *from, 2182a68886a6SDavid Ahern u32 cookie) 21833da59bd9SMartin KaFai Lau { 21845973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 21855973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2186a68886a6SDavid Ahern fib6_check(from, cookie)) 21873da59bd9SMartin KaFai Lau return &rt->dst; 21883da59bd9SMartin KaFai Lau else 21893da59bd9SMartin KaFai Lau return NULL; 21903da59bd9SMartin KaFai Lau } 21913da59bd9SMartin KaFai Lau 21921da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 21931da177e4SLinus Torvalds { 2194a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2195a68886a6SDavid Ahern struct fib6_info *from; 21961da177e4SLinus Torvalds struct rt6_info *rt; 21971da177e4SLinus Torvalds 2198a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2199a87b7dc9SDavid Ahern 2200a87b7dc9SDavid Ahern rcu_read_lock(); 22011da177e4SLinus Torvalds 22026f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 22036f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 22046f3118b5SNicolas Dichtel * into this function always. 22056f3118b5SNicolas Dichtel */ 2206e3bc10bdSHannes Frederic Sowa 2207a68886a6SDavid Ahern from = rcu_dereference(rt->from); 22084b32b5adSMartin KaFai Lau 2209a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2210a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2211a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 22123da59bd9SMartin KaFai Lau else 2213a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2214a87b7dc9SDavid Ahern 2215a87b7dc9SDavid Ahern rcu_read_unlock(); 2216a87b7dc9SDavid Ahern 2217a87b7dc9SDavid Ahern return dst_ret; 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 22211da177e4SLinus Torvalds { 22221da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (rt) { 222554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2226c3c14da0SDavid Ahern rcu_read_lock(); 222754c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 222893531c67SDavid Ahern rt6_remove_exception_rt(rt); 222954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 22301da177e4SLinus Torvalds } 2231c3c14da0SDavid Ahern rcu_read_unlock(); 223254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 223354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 223454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 223554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 223654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 223754c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 22411da177e4SLinus Torvalds { 22421da177e4SLinus Torvalds struct rt6_info *rt; 22431da177e4SLinus Torvalds 22443ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 22451da177e4SLinus Torvalds 2246adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 22471da177e4SLinus Torvalds if (rt) { 22488a14e46fSDavid Ahern rcu_read_lock(); 22491eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 2250ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 225193531c67SDavid Ahern rt6_remove_exception_rt(rt); 2252c5cff856SWei Wang } else { 2253a68886a6SDavid Ahern struct fib6_info *from; 2254c5cff856SWei Wang struct fib6_node *fn; 2255c5cff856SWei Wang 2256a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2257a68886a6SDavid Ahern if (from) { 2258a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2259c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2260c5cff856SWei Wang fn->fn_sernum = -1; 2261a68886a6SDavid Ahern } 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds rcu_read_unlock(); 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22676a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 22686a3e030fSDavid Ahern { 2269a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2270a68886a6SDavid Ahern struct fib6_info *from; 2271a68886a6SDavid Ahern 2272a68886a6SDavid Ahern rcu_read_lock(); 2273a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2274a68886a6SDavid Ahern if (from) 2275a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2276a68886a6SDavid Ahern rcu_read_unlock(); 2277a68886a6SDavid Ahern } 22786a3e030fSDavid Ahern 22796a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 22806a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 22816700c270SDavid S. Miller } 22821da177e4SLinus Torvalds 228345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 228445e4fd26SMartin KaFai Lau { 228545e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 228645e4fd26SMartin KaFai Lau 2287d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 228845e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 228945e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 229045e4fd26SMartin KaFai Lau } 229145e4fd26SMartin KaFai Lau 22920d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 22930d3f6d29SMartin KaFai Lau { 2294a68886a6SDavid Ahern bool from_set; 2295a68886a6SDavid Ahern 2296a68886a6SDavid Ahern rcu_read_lock(); 2297a68886a6SDavid Ahern from_set = !!rcu_dereference(rt->from); 2298a68886a6SDavid Ahern rcu_read_unlock(); 2299a68886a6SDavid Ahern 23000d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 2301a68886a6SDavid Ahern (rt->rt6i_flags & RTF_PCPU || from_set); 23020d3f6d29SMartin KaFai Lau } 23030d3f6d29SMartin KaFai Lau 230445e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 230545e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 23061da177e4SLinus Torvalds { 23070dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 23081da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 23091da177e4SLinus Torvalds 231019bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 231119bda36cSXin Long return; 231219bda36cSXin Long 231345e4fd26SMartin KaFai Lau if (iph) { 231445e4fd26SMartin KaFai Lau daddr = &iph->daddr; 231545e4fd26SMartin KaFai Lau saddr = &iph->saddr; 231645e4fd26SMartin KaFai Lau } else if (sk) { 231745e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 231845e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 231945e4fd26SMartin KaFai Lau } else { 23200dec879fSJulian Anastasov daddr = NULL; 23210dec879fSJulian Anastasov saddr = NULL; 23221da177e4SLinus Torvalds } 23230dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 23240dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 23250dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 23260dec879fSJulian Anastasov return; 23270dec879fSJulian Anastasov 23280dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 23290dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 23302b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 23312b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 23322b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 23330dec879fSJulian Anastasov } else if (daddr) { 2334a68886a6SDavid Ahern struct fib6_info *from; 23350dec879fSJulian Anastasov struct rt6_info *nrt6; 23360dec879fSJulian Anastasov 23374d85cd0cSDavid Ahern rcu_read_lock(); 2338a68886a6SDavid Ahern from = rcu_dereference(rt6->from); 2339a68886a6SDavid Ahern nrt6 = ip6_rt_cache_alloc(from, daddr, saddr); 234045e4fd26SMartin KaFai Lau if (nrt6) { 234145e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 2342a68886a6SDavid Ahern if (rt6_insert_exception(nrt6, from)) 23432b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 234445e4fd26SMartin KaFai Lau } 2345a68886a6SDavid Ahern rcu_read_unlock(); 234645e4fd26SMartin KaFai Lau } 234745e4fd26SMartin KaFai Lau } 234845e4fd26SMartin KaFai Lau 234945e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 235045e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 235145e4fd26SMartin KaFai Lau { 235245e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds 235542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2356e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 235781aded24SDavid S. Miller { 235881aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 235981aded24SDavid S. Miller struct dst_entry *dst; 236081aded24SDavid S. Miller struct flowi6 fl6; 236181aded24SDavid S. Miller 236281aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 236381aded24SDavid S. Miller fl6.flowi6_oif = oif; 23641b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 236581aded24SDavid S. Miller fl6.daddr = iph->daddr; 236681aded24SDavid S. Miller fl6.saddr = iph->saddr; 23676502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2368e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 236981aded24SDavid S. Miller 237081aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 237181aded24SDavid S. Miller if (!dst->error) 237245e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 237381aded24SDavid S. Miller dst_release(dst); 237481aded24SDavid S. Miller } 237581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 237681aded24SDavid S. Miller 237781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 237881aded24SDavid S. Miller { 237933c162a9SMartin KaFai Lau struct dst_entry *dst; 238033c162a9SMartin KaFai Lau 238181aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2382e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 238333c162a9SMartin KaFai Lau 238433c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 238533c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 238633c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 238733c162a9SMartin KaFai Lau return; 238833c162a9SMartin KaFai Lau 238933c162a9SMartin KaFai Lau bh_lock_sock(sk); 239033c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 239133c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 239233c162a9SMartin KaFai Lau bh_unlock_sock(sk); 239381aded24SDavid S. Miller } 239481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 239581aded24SDavid S. Miller 23967d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 23977d6850f7SAlexey Kodanev const struct flowi6 *fl6) 23987d6850f7SAlexey Kodanev { 23997d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 24007d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 24017d6850f7SAlexey Kodanev #endif 24027d6850f7SAlexey Kodanev 24037d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 24047d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 24057d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 24067d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 24077d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 24087d6850f7SAlexey Kodanev &np->saddr : 24097d6850f7SAlexey Kodanev #endif 24107d6850f7SAlexey Kodanev NULL); 24117d6850f7SAlexey Kodanev } 24127d6850f7SAlexey Kodanev 2413b55b76b2SDuan Jiong /* Handle redirects */ 2414b55b76b2SDuan Jiong struct ip6rd_flowi { 2415b55b76b2SDuan Jiong struct flowi6 fl6; 2416b55b76b2SDuan Jiong struct in6_addr gateway; 2417b55b76b2SDuan Jiong }; 2418b55b76b2SDuan Jiong 2419b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2420b55b76b2SDuan Jiong struct fib6_table *table, 2421b55b76b2SDuan Jiong struct flowi6 *fl6, 2422b75cc8f9SDavid Ahern const struct sk_buff *skb, 2423b55b76b2SDuan Jiong int flags) 2424b55b76b2SDuan Jiong { 2425b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 242623fb93a4SDavid Ahern struct rt6_info *ret = NULL, *rt_cache; 24278d1c802bSDavid Ahern struct fib6_info *rt; 2428b55b76b2SDuan Jiong struct fib6_node *fn; 2429b55b76b2SDuan Jiong 2430b55b76b2SDuan Jiong /* Get the "current" route for this destination and 243167c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2432b55b76b2SDuan Jiong * 2433b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2434b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2435b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2436b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2437b55b76b2SDuan Jiong * routes. 2438b55b76b2SDuan Jiong */ 2439b55b76b2SDuan Jiong 244066f5d6ceSWei Wang rcu_read_lock(); 24416454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2442b55b76b2SDuan Jiong restart: 244366f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 24445e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) 24458067bb8cSIdo Schimmel continue; 244614895687SDavid Ahern if (fib6_check_expired(rt)) 2447b55b76b2SDuan Jiong continue; 244893c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2449b55b76b2SDuan Jiong break; 245093c2fb25SDavid Ahern if (!(rt->fib6_flags & RTF_GATEWAY)) 2451b55b76b2SDuan Jiong continue; 24525e670d84SDavid Ahern if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex) 2453b55b76b2SDuan Jiong continue; 24542b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 24552b760fcfSWei Wang * in the case of an ip redirect. 24562b760fcfSWei Wang * So we keep searching in the exception table if the gateway 24572b760fcfSWei Wang * is different. 24582b760fcfSWei Wang */ 24595e670d84SDavid Ahern if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) { 24602b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 24612b760fcfSWei Wang &fl6->daddr, 24622b760fcfSWei Wang &fl6->saddr); 24632b760fcfSWei Wang if (rt_cache && 24642b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 24652b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 246623fb93a4SDavid Ahern ret = rt_cache; 24672b760fcfSWei Wang break; 24682b760fcfSWei Wang } 2469b55b76b2SDuan Jiong continue; 24702b760fcfSWei Wang } 2471b55b76b2SDuan Jiong break; 2472b55b76b2SDuan Jiong } 2473b55b76b2SDuan Jiong 2474b55b76b2SDuan Jiong if (!rt) 2475421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 247693c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 247723fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2478b0a1ba59SMartin KaFai Lau goto out; 2479b0a1ba59SMartin KaFai Lau } 2480b0a1ba59SMartin KaFai Lau 2481421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2482a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2483a3c00e46SMartin KaFai Lau if (fn) 2484a3c00e46SMartin KaFai Lau goto restart; 2485b55b76b2SDuan Jiong } 2486a3c00e46SMartin KaFai Lau 2487b0a1ba59SMartin KaFai Lau out: 248823fb93a4SDavid Ahern if (ret) 248923fb93a4SDavid Ahern dst_hold(&ret->dst); 249023fb93a4SDavid Ahern else 249123fb93a4SDavid Ahern ret = ip6_create_rt_rcu(rt); 2492b55b76b2SDuan Jiong 249366f5d6ceSWei Wang rcu_read_unlock(); 2494b55b76b2SDuan Jiong 2495b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 249623fb93a4SDavid Ahern return ret; 2497b55b76b2SDuan Jiong }; 2498b55b76b2SDuan Jiong 2499b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2500b55b76b2SDuan Jiong const struct flowi6 *fl6, 2501b75cc8f9SDavid Ahern const struct sk_buff *skb, 2502b55b76b2SDuan Jiong const struct in6_addr *gateway) 2503b55b76b2SDuan Jiong { 2504b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2505b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2506b55b76b2SDuan Jiong 2507b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2508b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2509b55b76b2SDuan Jiong 2510b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2511b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2512b55b76b2SDuan Jiong } 2513b55b76b2SDuan Jiong 2514e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2515e2d118a1SLorenzo Colitti kuid_t uid) 25163a5ad2eeSDavid S. Miller { 25173a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 25183a5ad2eeSDavid S. Miller struct dst_entry *dst; 25193a5ad2eeSDavid S. Miller struct flowi6 fl6; 25203a5ad2eeSDavid S. Miller 25213a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2522e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 25233a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 25243a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 25253a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 25263a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 25276502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2528e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 25293a5ad2eeSDavid S. Miller 2530b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 25316700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 25323a5ad2eeSDavid S. Miller dst_release(dst); 25333a5ad2eeSDavid S. Miller } 25343a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 25353a5ad2eeSDavid S. Miller 2536c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2537c92a59ecSDuan Jiong u32 mark) 2538c92a59ecSDuan Jiong { 2539c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2540c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2541c92a59ecSDuan Jiong struct dst_entry *dst; 2542c92a59ecSDuan Jiong struct flowi6 fl6; 2543c92a59ecSDuan Jiong 2544c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2545e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2546c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2547c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2548c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2549c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2550e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2551c92a59ecSDuan Jiong 2552b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 2553c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2554c92a59ecSDuan Jiong dst_release(dst); 2555c92a59ecSDuan Jiong } 2556c92a59ecSDuan Jiong 25573a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 25583a5ad2eeSDavid S. Miller { 2559e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2560e2d118a1SLorenzo Colitti sk->sk_uid); 25613a5ad2eeSDavid S. Miller } 25623a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 25633a5ad2eeSDavid S. Miller 25640dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 25651da177e4SLinus Torvalds { 25660dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 25670dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 25680dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 25690dbaee3bSDavid S. Miller 25701da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 25711da177e4SLinus Torvalds 25725578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 25735578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds /* 25761da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 25771da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 25781da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 25791da177e4SLinus Torvalds * rely only on pmtu discovery" 25801da177e4SLinus Torvalds */ 25811da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 25821da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 25831da177e4SLinus Torvalds return mtu; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 2586ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2587d33e4553SDavid S. Miller { 2588d33e4553SDavid S. Miller struct inet6_dev *idev; 2589d4ead6b3SDavid Ahern unsigned int mtu; 2590618f9bc7SSteffen Klassert 25914b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 25924b32b5adSMartin KaFai Lau if (mtu) 25934b32b5adSMartin KaFai Lau goto out; 25944b32b5adSMartin KaFai Lau 2595618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2596d33e4553SDavid S. Miller 2597d33e4553SDavid S. Miller rcu_read_lock(); 2598d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2599d33e4553SDavid S. Miller if (idev) 2600d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2601d33e4553SDavid S. Miller rcu_read_unlock(); 2602d33e4553SDavid S. Miller 260330f78d8eSEric Dumazet out: 260414972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 260514972cbdSRoopa Prabhu 260614972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2607d33e4553SDavid S. Miller } 2608d33e4553SDavid S. Miller 2609901731b8SDavid Ahern /* MTU selection: 2610901731b8SDavid Ahern * 1. mtu on route is locked - use it 2611901731b8SDavid Ahern * 2. mtu from nexthop exception 2612901731b8SDavid Ahern * 3. mtu from egress device 2613901731b8SDavid Ahern * 2614901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 2615901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 2616901731b8SDavid Ahern */ 2617901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr, 2618901731b8SDavid Ahern struct in6_addr *saddr) 2619901731b8SDavid Ahern { 2620901731b8SDavid Ahern struct rt6_exception_bucket *bucket; 2621901731b8SDavid Ahern struct rt6_exception *rt6_ex; 2622901731b8SDavid Ahern struct in6_addr *src_key; 2623901731b8SDavid Ahern struct inet6_dev *idev; 2624901731b8SDavid Ahern u32 mtu = 0; 2625901731b8SDavid Ahern 2626901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 2627901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 2628901731b8SDavid Ahern if (mtu) 2629901731b8SDavid Ahern goto out; 2630901731b8SDavid Ahern } 2631901731b8SDavid Ahern 2632901731b8SDavid Ahern src_key = NULL; 2633901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 2634901731b8SDavid Ahern if (f6i->fib6_src.plen) 2635901731b8SDavid Ahern src_key = saddr; 2636901731b8SDavid Ahern #endif 2637901731b8SDavid Ahern 2638901731b8SDavid Ahern bucket = rcu_dereference(f6i->rt6i_exception_bucket); 2639901731b8SDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 2640901731b8SDavid Ahern if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 2641901731b8SDavid Ahern mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU); 2642901731b8SDavid Ahern 2643901731b8SDavid Ahern if (likely(!mtu)) { 2644901731b8SDavid Ahern struct net_device *dev = fib6_info_nh_dev(f6i); 2645901731b8SDavid Ahern 2646901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 2647901731b8SDavid Ahern idev = __in6_dev_get(dev); 2648901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 2649901731b8SDavid Ahern mtu = idev->cnf.mtu6; 2650901731b8SDavid Ahern } 2651901731b8SDavid Ahern 2652901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 2653901731b8SDavid Ahern out: 2654901731b8SDavid Ahern return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu); 2655901731b8SDavid Ahern } 2656901731b8SDavid Ahern 26573b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 265887a11578SDavid S. Miller struct flowi6 *fl6) 26591da177e4SLinus Torvalds { 266087a11578SDavid S. Miller struct dst_entry *dst; 26611da177e4SLinus Torvalds struct rt6_info *rt; 26621da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2663c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26641da177e4SLinus Torvalds 266538308473SDavid S. Miller if (unlikely(!idev)) 2666122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 26671da177e4SLinus Torvalds 2668ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 266938308473SDavid S. Miller if (unlikely(!rt)) { 26701da177e4SLinus Torvalds in6_dev_put(idev); 267187a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 26721da177e4SLinus Torvalds goto out; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26758e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2676588753f1SBrendan McGrath rt->dst.input = ip6_input; 26778e2ec639SYan, Zheng rt->dst.output = ip6_output; 2678550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 267987a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 26808e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 26818e2ec639SYan, Zheng rt->rt6i_idev = idev; 268214edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 26831da177e4SLinus Torvalds 26844c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 2685587fea74SWei Wang * do proper release of the net_device 2686587fea74SWei Wang */ 2687587fea74SWei Wang rt6_uncached_list_add(rt); 268881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 26891da177e4SLinus Torvalds 269087a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 269187a11578SDavid S. Miller 26921da177e4SLinus Torvalds out: 269387a11578SDavid S. Miller return dst; 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 2696569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 26971da177e4SLinus Torvalds { 269886393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 26997019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 27007019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 27017019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 27027019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 27037019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2704fc66f95cSEric Dumazet int entries; 27051da177e4SLinus Torvalds 2706fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 270749a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2708fc66f95cSEric Dumazet entries <= rt_max_size) 27091da177e4SLinus Torvalds goto out; 27101da177e4SLinus Torvalds 27116891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 271214956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2713fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2714fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 27157019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 27161da177e4SLinus Torvalds out: 27177019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2718fc66f95cSEric Dumazet return entries > rt_max_size; 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds 27218d1c802bSDavid Ahern static int ip6_convert_metrics(struct net *net, struct fib6_info *rt, 2722d4ead6b3SDavid Ahern struct fib6_config *cfg) 2723e715b6d3SFlorian Westphal { 2724263243d6SEric Dumazet struct dst_metrics *p; 2725e715b6d3SFlorian Westphal 272663159f29SIan Morris if (!cfg->fc_mx) 2727e715b6d3SFlorian Westphal return 0; 2728e715b6d3SFlorian Westphal 2729263243d6SEric Dumazet p = kzalloc(sizeof(*rt->fib6_metrics), GFP_KERNEL); 2730263243d6SEric Dumazet if (unlikely(!p)) 2731e715b6d3SFlorian Westphal return -ENOMEM; 2732e715b6d3SFlorian Westphal 2733263243d6SEric Dumazet refcount_set(&p->refcnt, 1); 2734263243d6SEric Dumazet rt->fib6_metrics = p; 2735ea697639SDaniel Borkmann 2736263243d6SEric Dumazet return ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len, p->metrics); 2737e715b6d3SFlorian Westphal } 27381da177e4SLinus Torvalds 27398c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 27408c14586fSDavid Ahern struct fib6_config *cfg, 2741f4797b33SDavid Ahern const struct in6_addr *gw_addr, 2742f4797b33SDavid Ahern u32 tbid, int flags) 27438c14586fSDavid Ahern { 27448c14586fSDavid Ahern struct flowi6 fl6 = { 27458c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 27468c14586fSDavid Ahern .daddr = *gw_addr, 27478c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 27488c14586fSDavid Ahern }; 27498c14586fSDavid Ahern struct fib6_table *table; 27508c14586fSDavid Ahern struct rt6_info *rt; 27518c14586fSDavid Ahern 2752f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 27538c14586fSDavid Ahern if (!table) 27548c14586fSDavid Ahern return NULL; 27558c14586fSDavid Ahern 27568c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 27578c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 27588c14586fSDavid Ahern 2759f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 2760b75cc8f9SDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); 27618c14586fSDavid Ahern 27628c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 27638c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 27648c14586fSDavid Ahern ip6_rt_put(rt); 27658c14586fSDavid Ahern rt = NULL; 27668c14586fSDavid Ahern } 27678c14586fSDavid Ahern 27688c14586fSDavid Ahern return rt; 27698c14586fSDavid Ahern } 27708c14586fSDavid Ahern 2771fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 2772fc1e64e1SDavid Ahern struct fib6_config *cfg, 27739fbb704cSDavid Ahern const struct net_device *dev, 2774fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 2775fc1e64e1SDavid Ahern { 277644750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 2777fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 2778fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 2779fc1e64e1SDavid Ahern struct rt6_info *grt; 2780fc1e64e1SDavid Ahern int err; 2781fc1e64e1SDavid Ahern 2782fc1e64e1SDavid Ahern err = 0; 2783fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 2784fc1e64e1SDavid Ahern if (grt) { 278558e354c0SDavid Ahern if (!grt->dst.error && 278658e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 278744750f84SDavid Ahern NL_SET_ERR_MSG(extack, 278844750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 2789fc1e64e1SDavid Ahern err = -EINVAL; 2790fc1e64e1SDavid Ahern } 2791fc1e64e1SDavid Ahern 2792fc1e64e1SDavid Ahern ip6_rt_put(grt); 2793fc1e64e1SDavid Ahern } 2794fc1e64e1SDavid Ahern 2795fc1e64e1SDavid Ahern return err; 2796fc1e64e1SDavid Ahern } 2797fc1e64e1SDavid Ahern 27981edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 27991edce99fSDavid Ahern struct fib6_config *cfg, 28001edce99fSDavid Ahern struct net_device **_dev, 28011edce99fSDavid Ahern struct inet6_dev **idev) 28021edce99fSDavid Ahern { 28031edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 28041edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 28051edce99fSDavid Ahern struct rt6_info *grt = NULL; 28061edce99fSDavid Ahern int err = -EHOSTUNREACH; 28071edce99fSDavid Ahern 28081edce99fSDavid Ahern if (cfg->fc_table) { 2809f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 2810f4797b33SDavid Ahern 2811f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 2812f4797b33SDavid Ahern cfg->fc_table, flags); 28131edce99fSDavid Ahern if (grt) { 28141edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 28151edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 28161edce99fSDavid Ahern ip6_rt_put(grt); 28171edce99fSDavid Ahern grt = NULL; 28181edce99fSDavid Ahern } 28191edce99fSDavid Ahern } 28201edce99fSDavid Ahern } 28211edce99fSDavid Ahern 28221edce99fSDavid Ahern if (!grt) 2823b75cc8f9SDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); 28241edce99fSDavid Ahern 28251edce99fSDavid Ahern if (!grt) 28261edce99fSDavid Ahern goto out; 28271edce99fSDavid Ahern 28281edce99fSDavid Ahern if (dev) { 28291edce99fSDavid Ahern if (dev != grt->dst.dev) { 28301edce99fSDavid Ahern ip6_rt_put(grt); 28311edce99fSDavid Ahern goto out; 28321edce99fSDavid Ahern } 28331edce99fSDavid Ahern } else { 28341edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 28351edce99fSDavid Ahern *idev = grt->rt6i_idev; 28361edce99fSDavid Ahern dev_hold(dev); 28371edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 28381edce99fSDavid Ahern } 28391edce99fSDavid Ahern 28401edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 28411edce99fSDavid Ahern err = 0; 28421edce99fSDavid Ahern 28431edce99fSDavid Ahern ip6_rt_put(grt); 28441edce99fSDavid Ahern 28451edce99fSDavid Ahern out: 28461edce99fSDavid Ahern return err; 28471edce99fSDavid Ahern } 28481edce99fSDavid Ahern 28499fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 28509fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 28519fbb704cSDavid Ahern struct netlink_ext_ack *extack) 28529fbb704cSDavid Ahern { 28539fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 28549fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 2855232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 28569fbb704cSDavid Ahern const struct net_device *dev = *_dev; 2857232378e8SDavid Ahern bool need_addr_check = !dev; 28589fbb704cSDavid Ahern int err = -EINVAL; 28599fbb704cSDavid Ahern 28609fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 28619fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 28629fbb704cSDavid Ahern * will return already-added prefix route via interface that 28639fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 28649fbb704cSDavid Ahern */ 2865232378e8SDavid Ahern if (dev && 2866232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2867232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 28689fbb704cSDavid Ahern goto out; 28699fbb704cSDavid Ahern } 28709fbb704cSDavid Ahern 28719fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 28729fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 28739fbb704cSDavid Ahern * addresses as nexthop address. 28749fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 28759fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 28769fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 28779fbb704cSDavid Ahern * some exceptions. --ANK 28789fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 28799fbb704cSDavid Ahern * addressing 28809fbb704cSDavid Ahern */ 28819fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 28829fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 28839fbb704cSDavid Ahern goto out; 28849fbb704cSDavid Ahern } 28859fbb704cSDavid Ahern 28869fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 28879fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 28889fbb704cSDavid Ahern else 28899fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 28909fbb704cSDavid Ahern 28919fbb704cSDavid Ahern if (err) 28929fbb704cSDavid Ahern goto out; 28939fbb704cSDavid Ahern } 28949fbb704cSDavid Ahern 28959fbb704cSDavid Ahern /* reload in case device was changed */ 28969fbb704cSDavid Ahern dev = *_dev; 28979fbb704cSDavid Ahern 28989fbb704cSDavid Ahern err = -EINVAL; 28999fbb704cSDavid Ahern if (!dev) { 29009fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 29019fbb704cSDavid Ahern goto out; 29029fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 29039fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 29049fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 29059fbb704cSDavid Ahern goto out; 29069fbb704cSDavid Ahern } 2907232378e8SDavid Ahern 2908232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 2909232378e8SDavid Ahern * egress device has been resolved. 2910232378e8SDavid Ahern */ 2911232378e8SDavid Ahern if (need_addr_check && 2912232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2913232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 2914232378e8SDavid Ahern goto out; 2915232378e8SDavid Ahern } 2916232378e8SDavid Ahern 29179fbb704cSDavid Ahern err = 0; 29189fbb704cSDavid Ahern out: 29199fbb704cSDavid Ahern return err; 29209fbb704cSDavid Ahern } 29219fbb704cSDavid Ahern 29228d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 2923acb54e3cSDavid Ahern gfp_t gfp_flags, 2924333c4301SDavid Ahern struct netlink_ext_ack *extack) 29251da177e4SLinus Torvalds { 29265578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 29278d1c802bSDavid Ahern struct fib6_info *rt = NULL; 29281da177e4SLinus Torvalds struct net_device *dev = NULL; 29291da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2930c71099acSThomas Graf struct fib6_table *table; 29311da177e4SLinus Torvalds int addr_type; 29328c5b83f0SRoopa Prabhu int err = -EINVAL; 29331da177e4SLinus Torvalds 2934557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2935d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2936d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2937557c44beSDavid Ahern goto out; 2938d5d531cbSDavid Ahern } 2939557c44beSDavid Ahern 29402ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 29412ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 29422ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 29432ea2352eSWei Wang goto out; 29442ea2352eSWei Wang } 29452ea2352eSWei Wang 2946e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 2947e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 2948e8478e80SDavid Ahern goto out; 2949e8478e80SDavid Ahern } 2950e8478e80SDavid Ahern 2951d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2952d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 29538c5b83f0SRoopa Prabhu goto out; 2954d5d531cbSDavid Ahern } 2955d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2956d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2957d5d531cbSDavid Ahern goto out; 2958d5d531cbSDavid Ahern } 29591da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2960d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2961d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2962d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 29638c5b83f0SRoopa Prabhu goto out; 2964d5d531cbSDavid Ahern } 29651da177e4SLinus Torvalds #endif 296686872cb5SThomas Graf if (cfg->fc_ifindex) { 29671da177e4SLinus Torvalds err = -ENODEV; 29685578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 29691da177e4SLinus Torvalds if (!dev) 29701da177e4SLinus Torvalds goto out; 29711da177e4SLinus Torvalds idev = in6_dev_get(dev); 29721da177e4SLinus Torvalds if (!idev) 29731da177e4SLinus Torvalds goto out; 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 297686872cb5SThomas Graf if (cfg->fc_metric == 0) 297786872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 29781da177e4SLinus Torvalds 2979fc1e64e1SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 2980fc1e64e1SDavid Ahern if (!dev) { 2981fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, 2982fc1e64e1SDavid Ahern "Nexthop device required for onlink"); 2983fc1e64e1SDavid Ahern err = -ENODEV; 2984fc1e64e1SDavid Ahern goto out; 2985fc1e64e1SDavid Ahern } 2986fc1e64e1SDavid Ahern 2987fc1e64e1SDavid Ahern if (!(dev->flags & IFF_UP)) { 2988fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 2989fc1e64e1SDavid Ahern err = -ENETDOWN; 2990fc1e64e1SDavid Ahern goto out; 2991fc1e64e1SDavid Ahern } 2992fc1e64e1SDavid Ahern } 2993fc1e64e1SDavid Ahern 2994c71099acSThomas Graf err = -ENOBUFS; 299538308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2996d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2997d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 299838308473SDavid S. Miller if (!table) { 2999f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3000d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3001d71314b4SMatti Vaittinen } 3002d71314b4SMatti Vaittinen } else { 3003d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3004d71314b4SMatti Vaittinen } 300538308473SDavid S. Miller 300638308473SDavid S. Miller if (!table) 3007c71099acSThomas Graf goto out; 3008c71099acSThomas Graf 30091da177e4SLinus Torvalds err = -ENOMEM; 301093531c67SDavid Ahern rt = fib6_info_alloc(gfp_flags); 301193531c67SDavid Ahern if (!rt) 30121da177e4SLinus Torvalds goto out; 301393531c67SDavid Ahern 301493531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 301593531c67SDavid Ahern rt->dst_nocount = true; 30161da177e4SLinus Torvalds 3017d4ead6b3SDavid Ahern err = ip6_convert_metrics(net, rt, cfg); 3018d4ead6b3SDavid Ahern if (err < 0) 3019d4ead6b3SDavid Ahern goto out; 30201da177e4SLinus Torvalds 30211716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 302214895687SDavid Ahern fib6_set_expires(rt, jiffies + 30231716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 30241716a961SGao feng else 302514895687SDavid Ahern fib6_clean_expires(rt); 30261da177e4SLinus Torvalds 302786872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 302886872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 302993c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 303086872cb5SThomas Graf 303186872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 30321da177e4SLinus Torvalds 303319e42e45SRoopa Prabhu if (cfg->fc_encap) { 303419e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 303519e42e45SRoopa Prabhu 303630357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 3037127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 30389ae28727SDavid Ahern &lwtstate, extack); 303919e42e45SRoopa Prabhu if (err) 304019e42e45SRoopa Prabhu goto out; 30415e670d84SDavid Ahern rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate); 304225368623STom Herbert } 304319e42e45SRoopa Prabhu 304493c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 304593c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 304693c2fb25SDavid Ahern if (rt->fib6_dst.plen == 128) 30473b6761d1SDavid Ahern rt->dst_host = true; 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 305093c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 305193c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 30521da177e4SLinus Torvalds #endif 30531da177e4SLinus Torvalds 305493c2fb25SDavid Ahern rt->fib6_metric = cfg->fc_metric; 30555e670d84SDavid Ahern rt->fib6_nh.nh_weight = 1; 30561da177e4SLinus Torvalds 3057e8478e80SDavid Ahern rt->fib6_type = cfg->fc_type; 30581da177e4SLinus Torvalds 30591da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 30601da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 30611da177e4SLinus Torvalds */ 306286872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 306338308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 306438308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 306538308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 30661da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 30675578689aSDaniel Lezcano if (dev != net->loopback_dev) { 30681da177e4SLinus Torvalds if (dev) { 30691da177e4SLinus Torvalds dev_put(dev); 30701da177e4SLinus Torvalds in6_dev_put(idev); 30711da177e4SLinus Torvalds } 30725578689aSDaniel Lezcano dev = net->loopback_dev; 30731da177e4SLinus Torvalds dev_hold(dev); 30741da177e4SLinus Torvalds idev = in6_dev_get(dev); 30751da177e4SLinus Torvalds if (!idev) { 30761da177e4SLinus Torvalds err = -ENODEV; 30771da177e4SLinus Torvalds goto out; 30781da177e4SLinus Torvalds } 30791da177e4SLinus Torvalds } 308093c2fb25SDavid Ahern rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP; 30811da177e4SLinus Torvalds goto install_route; 30821da177e4SLinus Torvalds } 30831da177e4SLinus Torvalds 308486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 30859fbb704cSDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 30861da177e4SLinus Torvalds if (err) 30871da177e4SLinus Torvalds goto out; 30889fbb704cSDavid Ahern 308993531c67SDavid Ahern rt->fib6_nh.nh_gw = cfg->fc_gateway; 30901da177e4SLinus Torvalds } 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds err = -ENODEV; 309338308473SDavid S. Miller if (!dev) 30941da177e4SLinus Torvalds goto out; 30951da177e4SLinus Torvalds 3096428604fbSLorenzo Bianconi if (idev->cnf.disable_ipv6) { 3097428604fbSLorenzo Bianconi NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 3098428604fbSLorenzo Bianconi err = -EACCES; 3099428604fbSLorenzo Bianconi goto out; 3100428604fbSLorenzo Bianconi } 3101428604fbSLorenzo Bianconi 3102955ec4cbSDavid Ahern if (!(dev->flags & IFF_UP)) { 3103955ec4cbSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 3104955ec4cbSDavid Ahern err = -ENETDOWN; 3105955ec4cbSDavid Ahern goto out; 3106955ec4cbSDavid Ahern } 3107955ec4cbSDavid Ahern 3108c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 3109c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3110d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3111c3968a85SDaniel Walter err = -EINVAL; 3112c3968a85SDaniel Walter goto out; 3113c3968a85SDaniel Walter } 311493c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 311593c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3116c3968a85SDaniel Walter } else 311793c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3118c3968a85SDaniel Walter 311993c2fb25SDavid Ahern rt->fib6_flags = cfg->fc_flags; 31201da177e4SLinus Torvalds 31211da177e4SLinus Torvalds install_route: 312293c2fb25SDavid Ahern if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) && 31235609b80aSIdo Schimmel !netif_carrier_ok(dev)) 31245e670d84SDavid Ahern rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; 31255e670d84SDavid Ahern rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK); 312693531c67SDavid Ahern rt->fib6_nh.nh_dev = dev; 312793c2fb25SDavid Ahern rt->fib6_table = table; 312863152fc0SDaniel Lezcano 3129c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 313063152fc0SDaniel Lezcano 3131dcd1f572SDavid Ahern if (idev) 3132dcd1f572SDavid Ahern in6_dev_put(idev); 3133dcd1f572SDavid Ahern 31348c5b83f0SRoopa Prabhu return rt; 31351da177e4SLinus Torvalds out: 31361da177e4SLinus Torvalds if (dev) 31371da177e4SLinus Torvalds dev_put(dev); 31381da177e4SLinus Torvalds if (idev) 31391da177e4SLinus Torvalds in6_dev_put(idev); 31406b9ea5a6SRoopa Prabhu 314193531c67SDavid Ahern fib6_info_release(rt); 31428c5b83f0SRoopa Prabhu return ERR_PTR(err); 31436b9ea5a6SRoopa Prabhu } 31446b9ea5a6SRoopa Prabhu 3145acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3146333c4301SDavid Ahern struct netlink_ext_ack *extack) 31476b9ea5a6SRoopa Prabhu { 31488d1c802bSDavid Ahern struct fib6_info *rt; 31496b9ea5a6SRoopa Prabhu int err; 31506b9ea5a6SRoopa Prabhu 3151acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3152d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3153d4ead6b3SDavid Ahern return PTR_ERR(rt); 31546b9ea5a6SRoopa Prabhu 3155d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 315693531c67SDavid Ahern fib6_info_release(rt); 31576b9ea5a6SRoopa Prabhu 31581da177e4SLinus Torvalds return err; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 31618d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 31621da177e4SLinus Torvalds { 3163afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3164c71099acSThomas Graf struct fib6_table *table; 3165afb1d4b5SDavid Ahern int err; 31661da177e4SLinus Torvalds 3167421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 31686825a26cSGao feng err = -ENOENT; 31696825a26cSGao feng goto out; 31706825a26cSGao feng } 31716c813a72SPatrick McHardy 317293c2fb25SDavid Ahern table = rt->fib6_table; 317366f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 317486872cb5SThomas Graf err = fib6_del(rt, info); 317566f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 31761da177e4SLinus Torvalds 31776825a26cSGao feng out: 317893531c67SDavid Ahern fib6_info_release(rt); 31791da177e4SLinus Torvalds return err; 31801da177e4SLinus Torvalds } 31811da177e4SLinus Torvalds 31828d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt) 3183e0a1ad73SThomas Graf { 3184afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net }; 3185afb1d4b5SDavid Ahern 3186528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3187e0a1ad73SThomas Graf } 3188e0a1ad73SThomas Graf 31898d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 31900ae81335SDavid Ahern { 31910ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3192e3330039SWANG Cong struct net *net = info->nl_net; 319316a16cd3SDavid Ahern struct sk_buff *skb = NULL; 31940ae81335SDavid Ahern struct fib6_table *table; 3195e3330039SWANG Cong int err = -ENOENT; 31960ae81335SDavid Ahern 3197421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3198e3330039SWANG Cong goto out_put; 319993c2fb25SDavid Ahern table = rt->fib6_table; 320066f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 32010ae81335SDavid Ahern 320293c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 32038d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 32040ae81335SDavid Ahern 320516a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 320616a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 320716a16cd3SDavid Ahern if (skb) { 320816a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 320916a16cd3SDavid Ahern 3210d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 321116a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 321216a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 321316a16cd3SDavid Ahern kfree_skb(skb); 321416a16cd3SDavid Ahern skb = NULL; 321516a16cd3SDavid Ahern } else 321616a16cd3SDavid Ahern info->skip_notify = 1; 321716a16cd3SDavid Ahern } 321816a16cd3SDavid Ahern 32190ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 322093c2fb25SDavid Ahern &rt->fib6_siblings, 322193c2fb25SDavid Ahern fib6_siblings) { 32220ae81335SDavid Ahern err = fib6_del(sibling, info); 32230ae81335SDavid Ahern if (err) 3224e3330039SWANG Cong goto out_unlock; 32250ae81335SDavid Ahern } 32260ae81335SDavid Ahern } 32270ae81335SDavid Ahern 32280ae81335SDavid Ahern err = fib6_del(rt, info); 3229e3330039SWANG Cong out_unlock: 323066f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3231e3330039SWANG Cong out_put: 323293531c67SDavid Ahern fib6_info_release(rt); 323316a16cd3SDavid Ahern 323416a16cd3SDavid Ahern if (skb) { 3235e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 323616a16cd3SDavid Ahern info->nlh, gfp_any()); 323716a16cd3SDavid Ahern } 32380ae81335SDavid Ahern return err; 32390ae81335SDavid Ahern } 32400ae81335SDavid Ahern 324123fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 324223fb93a4SDavid Ahern { 324323fb93a4SDavid Ahern int rc = -ESRCH; 324423fb93a4SDavid Ahern 324523fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 324623fb93a4SDavid Ahern goto out; 324723fb93a4SDavid Ahern 324823fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 324923fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 325023fb93a4SDavid Ahern goto out; 325123fb93a4SDavid Ahern if (dst_hold_safe(&rt->dst)) 325223fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 325323fb93a4SDavid Ahern out: 325423fb93a4SDavid Ahern return rc; 325523fb93a4SDavid Ahern } 325623fb93a4SDavid Ahern 3257333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3258333c4301SDavid Ahern struct netlink_ext_ack *extack) 32591da177e4SLinus Torvalds { 32608d1c802bSDavid Ahern struct rt6_info *rt_cache; 3261c71099acSThomas Graf struct fib6_table *table; 32628d1c802bSDavid Ahern struct fib6_info *rt; 32631da177e4SLinus Torvalds struct fib6_node *fn; 32641da177e4SLinus Torvalds int err = -ESRCH; 32651da177e4SLinus Torvalds 32665578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3267d5d531cbSDavid Ahern if (!table) { 3268d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3269c71099acSThomas Graf return err; 3270d5d531cbSDavid Ahern } 32711da177e4SLinus Torvalds 327266f5d6ceSWei Wang rcu_read_lock(); 3273c71099acSThomas Graf 3274c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 327586872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 327638fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 32772b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds if (fn) { 328066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 32812b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 328223fb93a4SDavid Ahern int rc; 328323fb93a4SDavid Ahern 32842b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 32852b760fcfSWei Wang &cfg->fc_src); 328623fb93a4SDavid Ahern if (rt_cache) { 328723fb93a4SDavid Ahern rc = ip6_del_cached_rt(rt_cache, cfg); 32889e575010SEric Dumazet if (rc != -ESRCH) { 32899e575010SEric Dumazet rcu_read_unlock(); 329023fb93a4SDavid Ahern return rc; 329123fb93a4SDavid Ahern } 32929e575010SEric Dumazet } 32931f56a01fSMartin KaFai Lau continue; 32942b760fcfSWei Wang } 329586872cb5SThomas Graf if (cfg->fc_ifindex && 32965e670d84SDavid Ahern (!rt->fib6_nh.nh_dev || 32975e670d84SDavid Ahern rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex)) 32981da177e4SLinus Torvalds continue; 329986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 33005e670d84SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw)) 33011da177e4SLinus Torvalds continue; 330293c2fb25SDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 33031da177e4SLinus Torvalds continue; 330493c2fb25SDavid Ahern if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) 3305c2ed1880SMantas M continue; 330693531c67SDavid Ahern fib6_info_hold(rt); 330766f5d6ceSWei Wang rcu_read_unlock(); 33081da177e4SLinus Torvalds 33090ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 33100ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 331186872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 33120ae81335SDavid Ahern 33130ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds } 331666f5d6ceSWei Wang rcu_read_unlock(); 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds return err; 33191da177e4SLinus Torvalds } 33201da177e4SLinus Torvalds 33216700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3322a6279458SYOSHIFUJI Hideaki { 3323a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3324e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 3325e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3326e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3327e8599ff4SDavid S. Miller struct neighbour *neigh; 3328a68886a6SDavid Ahern struct fib6_info *from; 332971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 33306e157b6aSDavid S. Miller int optlen, on_link; 33316e157b6aSDavid S. Miller u8 *lladdr; 3332e8599ff4SDavid S. Miller 333329a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 333471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3335e8599ff4SDavid S. Miller 3336e8599ff4SDavid S. Miller if (optlen < 0) { 33376e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3338e8599ff4SDavid S. Miller return; 3339e8599ff4SDavid S. Miller } 3340e8599ff4SDavid S. Miller 334171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3342e8599ff4SDavid S. Miller 334371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 33446e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3345e8599ff4SDavid S. Miller return; 3346e8599ff4SDavid S. Miller } 3347e8599ff4SDavid S. Miller 33486e157b6aSDavid S. Miller on_link = 0; 334971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3350e8599ff4SDavid S. Miller on_link = 1; 335171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3352e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 33536e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3354e8599ff4SDavid S. Miller return; 3355e8599ff4SDavid S. Miller } 3356e8599ff4SDavid S. Miller 3357e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3358e8599ff4SDavid S. Miller if (!in6_dev) 3359e8599ff4SDavid S. Miller return; 3360e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3361e8599ff4SDavid S. Miller return; 3362e8599ff4SDavid S. Miller 3363e8599ff4SDavid S. Miller /* RFC2461 8.1: 3364e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3365e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3366e8599ff4SDavid S. Miller */ 3367e8599ff4SDavid S. Miller 3368f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3369e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3370e8599ff4SDavid S. Miller return; 3371e8599ff4SDavid S. Miller } 33726e157b6aSDavid S. Miller 33736e157b6aSDavid S. Miller lladdr = NULL; 3374e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3375e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3376e8599ff4SDavid S. Miller skb->dev); 3377e8599ff4SDavid S. Miller if (!lladdr) { 3378e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3379e8599ff4SDavid S. Miller return; 3380e8599ff4SDavid S. Miller } 3381e8599ff4SDavid S. Miller } 3382e8599ff4SDavid S. Miller 33836e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3384ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 33856e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 33866e157b6aSDavid S. Miller return; 33876e157b6aSDavid S. Miller } 33886e157b6aSDavid S. Miller 33896e157b6aSDavid S. Miller /* Redirect received -> path was valid. 33906e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 33916e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 33926e157b6aSDavid S. Miller */ 33930dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 33946e157b6aSDavid S. Miller 339571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3396e8599ff4SDavid S. Miller if (!neigh) 3397e8599ff4SDavid S. Miller return; 3398e8599ff4SDavid S. Miller 33991da177e4SLinus Torvalds /* 34001da177e4SLinus Torvalds * We have finally decided to accept it. 34011da177e4SLinus Torvalds */ 34021da177e4SLinus Torvalds 3403f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 34041da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 34051da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 34061da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3407f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3408f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 34091da177e4SLinus Torvalds 34104d85cd0cSDavid Ahern rcu_read_lock(); 3411a68886a6SDavid Ahern from = rcu_dereference(rt->from); 34128a14e46fSDavid Ahern fib6_info_hold(from); 34134d85cd0cSDavid Ahern rcu_read_unlock(); 34148a14e46fSDavid Ahern 34158a14e46fSDavid Ahern nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL); 341638308473SDavid S. Miller if (!nrt) 34171da177e4SLinus Torvalds goto out; 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 34201da177e4SLinus Torvalds if (on_link) 34211da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 34221da177e4SLinus Torvalds 34234e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 34241da177e4SLinus Torvalds 34252b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 34262b760fcfSWei Wang * a cached route because rt6_insert_exception() will 34272b760fcfSWei Wang * takes care of it 34282b760fcfSWei Wang */ 34298a14e46fSDavid Ahern if (rt6_insert_exception(nrt, from)) { 34302b760fcfSWei Wang dst_release_immediate(&nrt->dst); 34312b760fcfSWei Wang goto out; 34322b760fcfSWei Wang } 34331da177e4SLinus Torvalds 3434d8d1f30bSChangli Gao netevent.old = &rt->dst; 3435d8d1f30bSChangli Gao netevent.new = &nrt->dst; 343671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 343760592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 34388d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 34398d71740cSTom Tucker 34401da177e4SLinus Torvalds out: 34418a14e46fSDavid Ahern fib6_info_release(from); 3442e8599ff4SDavid S. Miller neigh_release(neigh); 34436e157b6aSDavid S. Miller } 34446e157b6aSDavid S. Miller 344570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 34468d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 3447b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3448830218c1SDavid Ahern const struct in6_addr *gwaddr, 3449830218c1SDavid Ahern struct net_device *dev) 345070ceb4f5SYOSHIFUJI Hideaki { 3451830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3452830218c1SDavid Ahern int ifindex = dev->ifindex; 345370ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 34548d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3455c71099acSThomas Graf struct fib6_table *table; 345670ceb4f5SYOSHIFUJI Hideaki 3457830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 345838308473SDavid S. Miller if (!table) 3459c71099acSThomas Graf return NULL; 3460c71099acSThomas Graf 346166f5d6ceSWei Wang rcu_read_lock(); 346238fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 346370ceb4f5SYOSHIFUJI Hideaki if (!fn) 346470ceb4f5SYOSHIFUJI Hideaki goto out; 346570ceb4f5SYOSHIFUJI Hideaki 346666f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 34675e670d84SDavid Ahern if (rt->fib6_nh.nh_dev->ifindex != ifindex) 346870ceb4f5SYOSHIFUJI Hideaki continue; 346993c2fb25SDavid Ahern if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 347070ceb4f5SYOSHIFUJI Hideaki continue; 34715e670d84SDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr)) 347270ceb4f5SYOSHIFUJI Hideaki continue; 34738d1c802bSDavid Ahern fib6_info_hold(rt); 347470ceb4f5SYOSHIFUJI Hideaki break; 347570ceb4f5SYOSHIFUJI Hideaki } 347670ceb4f5SYOSHIFUJI Hideaki out: 347766f5d6ceSWei Wang rcu_read_unlock(); 347870ceb4f5SYOSHIFUJI Hideaki return rt; 347970ceb4f5SYOSHIFUJI Hideaki } 348070ceb4f5SYOSHIFUJI Hideaki 34818d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 3482b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3483830218c1SDavid Ahern const struct in6_addr *gwaddr, 3484830218c1SDavid Ahern struct net_device *dev, 348595c96174SEric Dumazet unsigned int pref) 348670ceb4f5SYOSHIFUJI Hideaki { 348786872cb5SThomas Graf struct fib6_config cfg = { 3488238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3489830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 349086872cb5SThomas Graf .fc_dst_len = prefixlen, 349186872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 349286872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3493b91d5329SXin Long .fc_protocol = RTPROT_RA, 3494e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 349515e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3496efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3497efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 349886872cb5SThomas Graf }; 349970ceb4f5SYOSHIFUJI Hideaki 3500830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 35014e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 35024e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 350386872cb5SThomas Graf 3504e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3505e317da96SYOSHIFUJI Hideaki if (!prefixlen) 350686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 350770ceb4f5SYOSHIFUJI Hideaki 3508acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 350970ceb4f5SYOSHIFUJI Hideaki 3510830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 351170ceb4f5SYOSHIFUJI Hideaki } 351270ceb4f5SYOSHIFUJI Hideaki #endif 351370ceb4f5SYOSHIFUJI Hideaki 35148d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 3515afb1d4b5SDavid Ahern const struct in6_addr *addr, 3516afb1d4b5SDavid Ahern struct net_device *dev) 35171da177e4SLinus Torvalds { 3518830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 35198d1c802bSDavid Ahern struct fib6_info *rt; 3520c71099acSThomas Graf struct fib6_table *table; 35211da177e4SLinus Torvalds 3522afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 352338308473SDavid S. Miller if (!table) 3524c71099acSThomas Graf return NULL; 35251da177e4SLinus Torvalds 352666f5d6ceSWei Wang rcu_read_lock(); 352766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 35285e670d84SDavid Ahern if (dev == rt->fib6_nh.nh_dev && 352993c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 35305e670d84SDavid Ahern ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr)) 35311da177e4SLinus Torvalds break; 35321da177e4SLinus Torvalds } 35331da177e4SLinus Torvalds if (rt) 35348d1c802bSDavid Ahern fib6_info_hold(rt); 353566f5d6ceSWei Wang rcu_read_unlock(); 35361da177e4SLinus Torvalds return rt; 35371da177e4SLinus Torvalds } 35381da177e4SLinus Torvalds 35398d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 3540afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 3541ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3542ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 35431da177e4SLinus Torvalds { 354486872cb5SThomas Graf struct fib6_config cfg = { 3545ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3546238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 354786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 354886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 354986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3550b91d5329SXin Long .fc_protocol = RTPROT_RA, 3551e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 355215e47304SEric W. Biederman .fc_nlinfo.portid = 0, 35535578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3554afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 355586872cb5SThomas Graf }; 35561da177e4SLinus Torvalds 35574e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 35581da177e4SLinus Torvalds 3559acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 3560830218c1SDavid Ahern struct fib6_table *table; 3561830218c1SDavid Ahern 3562830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3563830218c1SDavid Ahern if (table) 3564830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3565830218c1SDavid Ahern } 35661da177e4SLinus Torvalds 3567afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 3570afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 3571afb1d4b5SDavid Ahern struct fib6_table *table) 35721da177e4SLinus Torvalds { 35738d1c802bSDavid Ahern struct fib6_info *rt; 35741da177e4SLinus Torvalds 35751da177e4SLinus Torvalds restart: 357666f5d6ceSWei Wang rcu_read_lock(); 357766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3578dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 3579dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 3580dcd1f572SDavid Ahern 358193c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 3582dcd1f572SDavid Ahern (!idev || idev->cnf.accept_ra != 2)) { 358393531c67SDavid Ahern fib6_info_hold(rt); 358466f5d6ceSWei Wang rcu_read_unlock(); 3585afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 35861da177e4SLinus Torvalds goto restart; 35871da177e4SLinus Torvalds } 35881da177e4SLinus Torvalds } 358966f5d6ceSWei Wang rcu_read_unlock(); 3590830218c1SDavid Ahern 3591830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3592830218c1SDavid Ahern } 3593830218c1SDavid Ahern 3594830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3595830218c1SDavid Ahern { 3596830218c1SDavid Ahern struct fib6_table *table; 3597830218c1SDavid Ahern struct hlist_head *head; 3598830218c1SDavid Ahern unsigned int h; 3599830218c1SDavid Ahern 3600830218c1SDavid Ahern rcu_read_lock(); 3601830218c1SDavid Ahern 3602830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3603830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3604830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3605830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3606afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 3607830218c1SDavid Ahern } 3608830218c1SDavid Ahern } 3609830218c1SDavid Ahern 3610830218c1SDavid Ahern rcu_read_unlock(); 36111da177e4SLinus Torvalds } 36121da177e4SLinus Torvalds 36135578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 36145578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 361586872cb5SThomas Graf struct fib6_config *cfg) 361686872cb5SThomas Graf { 361786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 361886872cb5SThomas Graf 3619ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3620ca254490SDavid Ahern : RT6_TABLE_MAIN; 362186872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 362286872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 362386872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 362486872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 362586872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 362686872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 3627e8478e80SDavid Ahern cfg->fc_type = rtmsg->rtmsg_type; 362886872cb5SThomas Graf 36295578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3630f1243c2dSBenjamin Thery 36314e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 36324e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 36334e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 363486872cb5SThomas Graf } 363586872cb5SThomas Graf 36365578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 36371da177e4SLinus Torvalds { 363886872cb5SThomas Graf struct fib6_config cfg; 36391da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 36401da177e4SLinus Torvalds int err; 36411da177e4SLinus Torvalds 36421da177e4SLinus Torvalds switch (cmd) { 36431da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 36441da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3645af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 36461da177e4SLinus Torvalds return -EPERM; 36471da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 36481da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 36491da177e4SLinus Torvalds if (err) 36501da177e4SLinus Torvalds return -EFAULT; 36511da177e4SLinus Torvalds 36525578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 365386872cb5SThomas Graf 36541da177e4SLinus Torvalds rtnl_lock(); 36551da177e4SLinus Torvalds switch (cmd) { 36561da177e4SLinus Torvalds case SIOCADDRT: 3657acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 36581da177e4SLinus Torvalds break; 36591da177e4SLinus Torvalds case SIOCDELRT: 3660333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 36611da177e4SLinus Torvalds break; 36621da177e4SLinus Torvalds default: 36631da177e4SLinus Torvalds err = -EINVAL; 36641da177e4SLinus Torvalds } 36651da177e4SLinus Torvalds rtnl_unlock(); 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds return err; 36683ff50b79SStephen Hemminger } 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds return -EINVAL; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds /* 36741da177e4SLinus Torvalds * Drop the packet on the floor 36751da177e4SLinus Torvalds */ 36761da177e4SLinus Torvalds 3677d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 36781da177e4SLinus Torvalds { 3679612f09e8SYOSHIFUJI Hideaki int type; 3680adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3681612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3682612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 36830660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 368445bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 3685bdb7cc64SStephen Suryaputra IP6_INC_STATS(dev_net(dst->dev), 3686bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), 36873bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3688612f09e8SYOSHIFUJI Hideaki break; 3689612f09e8SYOSHIFUJI Hideaki } 3690612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3691612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 36923bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 36933bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3694612f09e8SYOSHIFUJI Hideaki break; 3695612f09e8SYOSHIFUJI Hideaki } 36963ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 36971da177e4SLinus Torvalds kfree_skb(skb); 36981da177e4SLinus Torvalds return 0; 36991da177e4SLinus Torvalds } 37001da177e4SLinus Torvalds 37019ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 37029ce8ade0SThomas Graf { 3703612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 37049ce8ade0SThomas Graf } 37059ce8ade0SThomas Graf 3706ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 37071da177e4SLinus Torvalds { 3708adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3709612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds 37129ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 37139ce8ade0SThomas Graf { 3714612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 37159ce8ade0SThomas Graf } 37169ce8ade0SThomas Graf 3717ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 37189ce8ade0SThomas Graf { 3719adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3720612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 37219ce8ade0SThomas Graf } 37229ce8ade0SThomas Graf 37231da177e4SLinus Torvalds /* 37241da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 37251da177e4SLinus Torvalds */ 37261da177e4SLinus Torvalds 3727360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 3728afb1d4b5SDavid Ahern struct inet6_dev *idev, 37291da177e4SLinus Torvalds const struct in6_addr *addr, 3730acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 37311da177e4SLinus Torvalds { 3732ca254490SDavid Ahern u32 tb_id; 37334832c30dSDavid Ahern struct net_device *dev = idev->dev; 3734360a9887SDavid Ahern struct fib6_info *f6i; 37355f02ce24SDavid Ahern 3736360a9887SDavid Ahern f6i = fib6_info_alloc(gfp_flags); 3737360a9887SDavid Ahern if (!f6i) 37381da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 37391da177e4SLinus Torvalds 3740360a9887SDavid Ahern f6i->dst_nocount = true; 3741360a9887SDavid Ahern f6i->dst_host = true; 3742360a9887SDavid Ahern f6i->fib6_protocol = RTPROT_KERNEL; 3743360a9887SDavid Ahern f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP; 3744e8478e80SDavid Ahern if (anycast) { 3745360a9887SDavid Ahern f6i->fib6_type = RTN_ANYCAST; 3746360a9887SDavid Ahern f6i->fib6_flags |= RTF_ANYCAST; 3747e8478e80SDavid Ahern } else { 3748360a9887SDavid Ahern f6i->fib6_type = RTN_LOCAL; 3749360a9887SDavid Ahern f6i->fib6_flags |= RTF_LOCAL; 3750e8478e80SDavid Ahern } 37511da177e4SLinus Torvalds 3752360a9887SDavid Ahern f6i->fib6_nh.nh_gw = *addr; 375393531c67SDavid Ahern dev_hold(dev); 3754360a9887SDavid Ahern f6i->fib6_nh.nh_dev = dev; 3755360a9887SDavid Ahern f6i->fib6_dst.addr = *addr; 3756360a9887SDavid Ahern f6i->fib6_dst.plen = 128; 3757ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3758360a9887SDavid Ahern f6i->fib6_table = fib6_get_table(net, tb_id); 37591da177e4SLinus Torvalds 3760360a9887SDavid Ahern return f6i; 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds 3763c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3764c3968a85SDaniel Walter struct arg_dev_net_ip { 3765c3968a85SDaniel Walter struct net_device *dev; 3766c3968a85SDaniel Walter struct net *net; 3767c3968a85SDaniel Walter struct in6_addr *addr; 3768c3968a85SDaniel Walter }; 3769c3968a85SDaniel Walter 37708d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 3771c3968a85SDaniel Walter { 3772c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3773c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3774c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3775c3968a85SDaniel Walter 37765e670d84SDavid Ahern if (((void *)rt->fib6_nh.nh_dev == dev || !dev) && 3777421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 377893c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 377960006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3780c3968a85SDaniel Walter /* remove prefsrc entry */ 378193c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 378260006a48SWei Wang /* need to update cache as well */ 378360006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 378460006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3785c3968a85SDaniel Walter } 3786c3968a85SDaniel Walter return 0; 3787c3968a85SDaniel Walter } 3788c3968a85SDaniel Walter 3789c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3790c3968a85SDaniel Walter { 3791c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3792c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3793c3968a85SDaniel Walter .dev = ifp->idev->dev, 3794c3968a85SDaniel Walter .net = net, 3795c3968a85SDaniel Walter .addr = &ifp->addr, 3796c3968a85SDaniel Walter }; 37970c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3798c3968a85SDaniel Walter } 3799c3968a85SDaniel Walter 3800be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3801be7a010dSDuan Jiong 3802be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 38038d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 3804be7a010dSDuan Jiong { 3805be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3806be7a010dSDuan Jiong 380793c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 38085e670d84SDavid Ahern ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) { 3809be7a010dSDuan Jiong return -1; 3810be7a010dSDuan Jiong } 3811b16cb459SWei Wang 3812b16cb459SWei Wang /* Further clean up cached routes in exception table. 3813b16cb459SWei Wang * This is needed because cached route may have a different 3814b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3815b16cb459SWei Wang */ 3816b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3817b16cb459SWei Wang 3818be7a010dSDuan Jiong return 0; 3819be7a010dSDuan Jiong } 3820be7a010dSDuan Jiong 3821be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3822be7a010dSDuan Jiong { 3823be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3824be7a010dSDuan Jiong } 3825be7a010dSDuan Jiong 38262127d95aSIdo Schimmel struct arg_netdev_event { 38272127d95aSIdo Schimmel const struct net_device *dev; 38284c981e28SIdo Schimmel union { 38292127d95aSIdo Schimmel unsigned int nh_flags; 38304c981e28SIdo Schimmel unsigned long event; 38314c981e28SIdo Schimmel }; 38322127d95aSIdo Schimmel }; 38332127d95aSIdo Schimmel 38348d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 3835d7dedee1SIdo Schimmel { 38368d1c802bSDavid Ahern struct fib6_info *iter; 3837d7dedee1SIdo Schimmel struct fib6_node *fn; 3838d7dedee1SIdo Schimmel 383993c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 384093c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3841d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 384293c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3843d7dedee1SIdo Schimmel while (iter) { 384493c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 3845*33bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 3846d7dedee1SIdo Schimmel return iter; 38478fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 384893c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3849d7dedee1SIdo Schimmel } 3850d7dedee1SIdo Schimmel 3851d7dedee1SIdo Schimmel return NULL; 3852d7dedee1SIdo Schimmel } 3853d7dedee1SIdo Schimmel 38548d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 3855d7dedee1SIdo Schimmel { 38565e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_DEAD || 38575e670d84SDavid Ahern (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && 3858dcd1f572SDavid Ahern fib6_ignore_linkdown(rt))) 3859d7dedee1SIdo Schimmel return true; 3860d7dedee1SIdo Schimmel 3861d7dedee1SIdo Schimmel return false; 3862d7dedee1SIdo Schimmel } 3863d7dedee1SIdo Schimmel 38648d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 3865d7dedee1SIdo Schimmel { 38668d1c802bSDavid Ahern struct fib6_info *iter; 3867d7dedee1SIdo Schimmel int total = 0; 3868d7dedee1SIdo Schimmel 3869d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 38705e670d84SDavid Ahern total += rt->fib6_nh.nh_weight; 3871d7dedee1SIdo Schimmel 387293c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 3873d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 38745e670d84SDavid Ahern total += iter->fib6_nh.nh_weight; 3875d7dedee1SIdo Schimmel } 3876d7dedee1SIdo Schimmel 3877d7dedee1SIdo Schimmel return total; 3878d7dedee1SIdo Schimmel } 3879d7dedee1SIdo Schimmel 38808d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 3881d7dedee1SIdo Schimmel { 3882d7dedee1SIdo Schimmel int upper_bound = -1; 3883d7dedee1SIdo Schimmel 3884d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 38855e670d84SDavid Ahern *weight += rt->fib6_nh.nh_weight; 3886d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 3887d7dedee1SIdo Schimmel total) - 1; 3888d7dedee1SIdo Schimmel } 38895e670d84SDavid Ahern atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound); 3890d7dedee1SIdo Schimmel } 3891d7dedee1SIdo Schimmel 38928d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 3893d7dedee1SIdo Schimmel { 38948d1c802bSDavid Ahern struct fib6_info *iter; 3895d7dedee1SIdo Schimmel int weight = 0; 3896d7dedee1SIdo Schimmel 3897d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 3898d7dedee1SIdo Schimmel 389993c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 3900d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 3901d7dedee1SIdo Schimmel } 3902d7dedee1SIdo Schimmel 39038d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 3904d7dedee1SIdo Schimmel { 39058d1c802bSDavid Ahern struct fib6_info *first; 3906d7dedee1SIdo Schimmel int total; 3907d7dedee1SIdo Schimmel 3908d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 3909d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 3910d7dedee1SIdo Schimmel * sibling route. 3911d7dedee1SIdo Schimmel */ 391293c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 3913d7dedee1SIdo Schimmel return; 3914d7dedee1SIdo Schimmel 3915d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 3916d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 3917d7dedee1SIdo Schimmel * onwards. 3918d7dedee1SIdo Schimmel */ 3919d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 3920d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 3921d7dedee1SIdo Schimmel return; 3922d7dedee1SIdo Schimmel 3923d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 3924d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 3925d7dedee1SIdo Schimmel } 3926d7dedee1SIdo Schimmel 39278d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 39282127d95aSIdo Schimmel { 39292127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 39307aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 39312127d95aSIdo Schimmel 3932421842edSDavid Ahern if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) { 39335e670d84SDavid Ahern rt->fib6_nh.nh_flags &= ~arg->nh_flags; 39347aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 3935d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 39361de178edSIdo Schimmel } 39372127d95aSIdo Schimmel 39382127d95aSIdo Schimmel return 0; 39392127d95aSIdo Schimmel } 39402127d95aSIdo Schimmel 39412127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) 39422127d95aSIdo Schimmel { 39432127d95aSIdo Schimmel struct arg_netdev_event arg = { 39442127d95aSIdo Schimmel .dev = dev, 39456802f3adSIdo Schimmel { 39462127d95aSIdo Schimmel .nh_flags = nh_flags, 39476802f3adSIdo Schimmel }, 39482127d95aSIdo Schimmel }; 39492127d95aSIdo Schimmel 39502127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 39512127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 39522127d95aSIdo Schimmel 39532127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 39542127d95aSIdo Schimmel } 39552127d95aSIdo Schimmel 39568d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 39571de178edSIdo Schimmel const struct net_device *dev) 39581de178edSIdo Schimmel { 39598d1c802bSDavid Ahern struct fib6_info *iter; 39601de178edSIdo Schimmel 39615e670d84SDavid Ahern if (rt->fib6_nh.nh_dev == dev) 39621de178edSIdo Schimmel return true; 396393c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 39645e670d84SDavid Ahern if (iter->fib6_nh.nh_dev == dev) 39651de178edSIdo Schimmel return true; 39661de178edSIdo Schimmel 39671de178edSIdo Schimmel return false; 39681de178edSIdo Schimmel } 39691de178edSIdo Schimmel 39708d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 39711de178edSIdo Schimmel { 39728d1c802bSDavid Ahern struct fib6_info *iter; 39731de178edSIdo Schimmel 39741de178edSIdo Schimmel rt->should_flush = 1; 397593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 39761de178edSIdo Schimmel iter->should_flush = 1; 39771de178edSIdo Schimmel } 39781de178edSIdo Schimmel 39798d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 39801de178edSIdo Schimmel const struct net_device *down_dev) 39811de178edSIdo Schimmel { 39828d1c802bSDavid Ahern struct fib6_info *iter; 39831de178edSIdo Schimmel unsigned int dead = 0; 39841de178edSIdo Schimmel 39855e670d84SDavid Ahern if (rt->fib6_nh.nh_dev == down_dev || 39865e670d84SDavid Ahern rt->fib6_nh.nh_flags & RTNH_F_DEAD) 39871de178edSIdo Schimmel dead++; 398893c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 39895e670d84SDavid Ahern if (iter->fib6_nh.nh_dev == down_dev || 39905e670d84SDavid Ahern iter->fib6_nh.nh_flags & RTNH_F_DEAD) 39911de178edSIdo Schimmel dead++; 39921de178edSIdo Schimmel 39931de178edSIdo Schimmel return dead; 39941de178edSIdo Schimmel } 39951de178edSIdo Schimmel 39968d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 39971de178edSIdo Schimmel const struct net_device *dev, 39981de178edSIdo Schimmel unsigned int nh_flags) 39991de178edSIdo Schimmel { 40008d1c802bSDavid Ahern struct fib6_info *iter; 40011de178edSIdo Schimmel 40025e670d84SDavid Ahern if (rt->fib6_nh.nh_dev == dev) 40035e670d84SDavid Ahern rt->fib6_nh.nh_flags |= nh_flags; 400493c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 40055e670d84SDavid Ahern if (iter->fib6_nh.nh_dev == dev) 40065e670d84SDavid Ahern iter->fib6_nh.nh_flags |= nh_flags; 40071de178edSIdo Schimmel } 40081de178edSIdo Schimmel 4009a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 40108d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 40111da177e4SLinus Torvalds { 40124c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 40134c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 40147aef6859SDavid Ahern struct net *net = dev_net(dev); 40158ed67789SDaniel Lezcano 4016421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 401727c6fa73SIdo Schimmel return 0; 401827c6fa73SIdo Schimmel 401927c6fa73SIdo Schimmel switch (arg->event) { 402027c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 40215e670d84SDavid Ahern return rt->fib6_nh.nh_dev == dev ? -1 : 0; 402227c6fa73SIdo Schimmel case NETDEV_DOWN: 40231de178edSIdo Schimmel if (rt->should_flush) 402427c6fa73SIdo Schimmel return -1; 402593c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 40265e670d84SDavid Ahern return rt->fib6_nh.nh_dev == dev ? -1 : 0; 40271de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 40281de178edSIdo Schimmel unsigned int count; 40291de178edSIdo Schimmel 40301de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 403193c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 40321de178edSIdo Schimmel rt6_multipath_flush(rt); 40331de178edSIdo Schimmel return -1; 40341de178edSIdo Schimmel } 40351de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 40361de178edSIdo Schimmel RTNH_F_LINKDOWN); 40377aef6859SDavid Ahern fib6_update_sernum(net, rt); 4038d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 40391de178edSIdo Schimmel } 40401de178edSIdo Schimmel return -2; 404127c6fa73SIdo Schimmel case NETDEV_CHANGE: 40425e670d84SDavid Ahern if (rt->fib6_nh.nh_dev != dev || 404393c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 404427c6fa73SIdo Schimmel break; 40455e670d84SDavid Ahern rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; 4046d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 404727c6fa73SIdo Schimmel break; 40482b241361SIdo Schimmel } 4049c159d30cSDavid S. Miller 40501da177e4SLinus Torvalds return 0; 40511da177e4SLinus Torvalds } 40521da177e4SLinus Torvalds 405327c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 40541da177e4SLinus Torvalds { 40554c981e28SIdo Schimmel struct arg_netdev_event arg = { 40568ed67789SDaniel Lezcano .dev = dev, 40576802f3adSIdo Schimmel { 40584c981e28SIdo Schimmel .event = event, 40596802f3adSIdo Schimmel }, 40608ed67789SDaniel Lezcano }; 40618ed67789SDaniel Lezcano 40624c981e28SIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifdown, &arg); 40634c981e28SIdo Schimmel } 40644c981e28SIdo Schimmel 40654c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 40664c981e28SIdo Schimmel { 40674c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 40684c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 40694c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 40701da177e4SLinus Torvalds } 40711da177e4SLinus Torvalds 407295c96174SEric Dumazet struct rt6_mtu_change_arg { 40731da177e4SLinus Torvalds struct net_device *dev; 407495c96174SEric Dumazet unsigned int mtu; 40751da177e4SLinus Torvalds }; 40761da177e4SLinus Torvalds 40778d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg) 40781da177e4SLinus Torvalds { 40791da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 40801da177e4SLinus Torvalds struct inet6_dev *idev; 40811da177e4SLinus Torvalds 40821da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 40831da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 40841da177e4SLinus Torvalds We still use this lock to block changes 40851da177e4SLinus Torvalds caused by addrconf/ndisc. 40861da177e4SLinus Torvalds */ 40871da177e4SLinus Torvalds 40881da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 408938308473SDavid S. Miller if (!idev) 40901da177e4SLinus Torvalds return 0; 40911da177e4SLinus Torvalds 40921da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 40931da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 40941da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 40951da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 40961da177e4SLinus Torvalds */ 40975e670d84SDavid Ahern if (rt->fib6_nh.nh_dev == arg->dev && 4098d4ead6b3SDavid Ahern !fib6_metric_locked(rt, RTAX_MTU)) { 4099d4ead6b3SDavid Ahern u32 mtu = rt->fib6_pmtu; 4100d4ead6b3SDavid Ahern 4101d4ead6b3SDavid Ahern if (mtu >= arg->mtu || 4102d4ead6b3SDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4103d4ead6b3SDavid Ahern fib6_metric_set(rt, RTAX_MTU, arg->mtu); 4104d4ead6b3SDavid Ahern 4105f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 4106e9fa1495SStefano Brivio rt6_exceptions_update_pmtu(idev, rt, arg->mtu); 4107f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 41084b32b5adSMartin KaFai Lau } 41091da177e4SLinus Torvalds return 0; 41101da177e4SLinus Torvalds } 41111da177e4SLinus Torvalds 411295c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 41131da177e4SLinus Torvalds { 4114c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4115c71099acSThomas Graf .dev = dev, 4116c71099acSThomas Graf .mtu = mtu, 4117c71099acSThomas Graf }; 41181da177e4SLinus Torvalds 41190c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 41201da177e4SLinus Torvalds } 41211da177e4SLinus Torvalds 4122ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 41235176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4124aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 412586872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4126ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 412786872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 412886872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 412951ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4130c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 413119e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 413219e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 413332bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4134622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 41353b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4136aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4137eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4138eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4139eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 414086872cb5SThomas Graf }; 414186872cb5SThomas Graf 414286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4143333c4301SDavid Ahern struct fib6_config *cfg, 4144333c4301SDavid Ahern struct netlink_ext_ack *extack) 41451da177e4SLinus Torvalds { 414686872cb5SThomas Graf struct rtmsg *rtm; 414786872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4148c78ba6d6SLubomir Rintel unsigned int pref; 414986872cb5SThomas Graf int err; 41501da177e4SLinus Torvalds 4151fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4152fceb6435SJohannes Berg NULL); 415386872cb5SThomas Graf if (err < 0) 415486872cb5SThomas Graf goto errout; 41551da177e4SLinus Torvalds 415686872cb5SThomas Graf err = -EINVAL; 415786872cb5SThomas Graf rtm = nlmsg_data(nlh); 415886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 415986872cb5SThomas Graf 416086872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 416186872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 416286872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 416386872cb5SThomas Graf cfg->fc_flags = RTF_UP; 416486872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 4165ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 416686872cb5SThomas Graf 4167ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4168ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4169b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4170b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 417186872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 417286872cb5SThomas Graf 4173ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4174ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4175ab79ad14SMaciej Żenczykowski 41761f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 41771f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 41781f56a01fSMartin KaFai Lau 4179fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4180fc1e64e1SDavid Ahern 418115e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 418286872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 41833b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 418486872cb5SThomas Graf 418586872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 418667b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 418786872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 41881da177e4SLinus Torvalds } 418986872cb5SThomas Graf 419086872cb5SThomas Graf if (tb[RTA_DST]) { 419186872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 419286872cb5SThomas Graf 419386872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 419486872cb5SThomas Graf goto errout; 419586872cb5SThomas Graf 419686872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 41971da177e4SLinus Torvalds } 419886872cb5SThomas Graf 419986872cb5SThomas Graf if (tb[RTA_SRC]) { 420086872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 420186872cb5SThomas Graf 420286872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 420386872cb5SThomas Graf goto errout; 420486872cb5SThomas Graf 420586872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 42061da177e4SLinus Torvalds } 420786872cb5SThomas Graf 4208c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 420967b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4210c3968a85SDaniel Walter 421186872cb5SThomas Graf if (tb[RTA_OIF]) 421286872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 421386872cb5SThomas Graf 421486872cb5SThomas Graf if (tb[RTA_PRIORITY]) 421586872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 421686872cb5SThomas Graf 421786872cb5SThomas Graf if (tb[RTA_METRICS]) { 421886872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 421986872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 42201da177e4SLinus Torvalds } 422186872cb5SThomas Graf 422286872cb5SThomas Graf if (tb[RTA_TABLE]) 422386872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 422486872cb5SThomas Graf 422551ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 422651ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 422751ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 42289ed59592SDavid Ahern 42299ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4230c255bd68SDavid Ahern cfg->fc_mp_len, extack); 42319ed59592SDavid Ahern if (err < 0) 42329ed59592SDavid Ahern goto errout; 423351ebd318SNicolas Dichtel } 423451ebd318SNicolas Dichtel 4235c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4236c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4237c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4238c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4239c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4240c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4241c78ba6d6SLubomir Rintel } 4242c78ba6d6SLubomir Rintel 424319e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 424419e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 424519e42e45SRoopa Prabhu 42469ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 424719e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 424819e42e45SRoopa Prabhu 4249c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 42509ed59592SDavid Ahern if (err < 0) 42519ed59592SDavid Ahern goto errout; 42529ed59592SDavid Ahern } 42539ed59592SDavid Ahern 425432bc201eSXin Long if (tb[RTA_EXPIRES]) { 425532bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 425632bc201eSXin Long 425732bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 425832bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 425932bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 426032bc201eSXin Long } 426132bc201eSXin Long } 426232bc201eSXin Long 426386872cb5SThomas Graf err = 0; 426486872cb5SThomas Graf errout: 426586872cb5SThomas Graf return err; 42661da177e4SLinus Torvalds } 42671da177e4SLinus Torvalds 42686b9ea5a6SRoopa Prabhu struct rt6_nh { 42698d1c802bSDavid Ahern struct fib6_info *fib6_info; 42706b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 42716b9ea5a6SRoopa Prabhu struct list_head next; 42726b9ea5a6SRoopa Prabhu }; 42736b9ea5a6SRoopa Prabhu 42746b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 42756b9ea5a6SRoopa Prabhu { 42766b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 42776b9ea5a6SRoopa Prabhu 42786b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 42797d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 42806b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 42816b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 42826b9ea5a6SRoopa Prabhu } 42836b9ea5a6SRoopa Prabhu } 42846b9ea5a6SRoopa Prabhu 4285d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 4286d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 42878d1c802bSDavid Ahern struct fib6_info *rt, 42888d1c802bSDavid Ahern struct fib6_config *r_cfg) 42896b9ea5a6SRoopa Prabhu { 42906b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 42916b9ea5a6SRoopa Prabhu int err = -EEXIST; 42926b9ea5a6SRoopa Prabhu 42936b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 42948d1c802bSDavid Ahern /* check if fib6_info already exists */ 42958d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 42966b9ea5a6SRoopa Prabhu return err; 42976b9ea5a6SRoopa Prabhu } 42986b9ea5a6SRoopa Prabhu 42996b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 43006b9ea5a6SRoopa Prabhu if (!nh) 43016b9ea5a6SRoopa Prabhu return -ENOMEM; 43028d1c802bSDavid Ahern nh->fib6_info = rt; 4303d4ead6b3SDavid Ahern err = ip6_convert_metrics(net, rt, r_cfg); 43046b9ea5a6SRoopa Prabhu if (err) { 43056b9ea5a6SRoopa Prabhu kfree(nh); 43066b9ea5a6SRoopa Prabhu return err; 43076b9ea5a6SRoopa Prabhu } 43086b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 43096b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 43106b9ea5a6SRoopa Prabhu 43116b9ea5a6SRoopa Prabhu return 0; 43126b9ea5a6SRoopa Prabhu } 43136b9ea5a6SRoopa Prabhu 43148d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 43158d1c802bSDavid Ahern struct fib6_info *rt_last, 43163b1137feSDavid Ahern struct nl_info *info, 43173b1137feSDavid Ahern __u16 nlflags) 43183b1137feSDavid Ahern { 43193b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 43203b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 43213b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 43223b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 43233b1137feSDavid Ahern * the list, find the first sibling of the last route appended 43243b1137feSDavid Ahern */ 432593c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 432693c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 43278d1c802bSDavid Ahern struct fib6_info, 432893c2fb25SDavid Ahern fib6_siblings); 43293b1137feSDavid Ahern } 43303b1137feSDavid Ahern 43313b1137feSDavid Ahern if (rt) 43323b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 43333b1137feSDavid Ahern } 43343b1137feSDavid Ahern 4335333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4336333c4301SDavid Ahern struct netlink_ext_ack *extack) 433751ebd318SNicolas Dichtel { 43388d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 43393b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 434051ebd318SNicolas Dichtel struct fib6_config r_cfg; 434151ebd318SNicolas Dichtel struct rtnexthop *rtnh; 43428d1c802bSDavid Ahern struct fib6_info *rt; 43436b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 43446b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 43453b1137feSDavid Ahern __u16 nlflags; 434651ebd318SNicolas Dichtel int remaining; 434751ebd318SNicolas Dichtel int attrlen; 43486b9ea5a6SRoopa Prabhu int err = 1; 43496b9ea5a6SRoopa Prabhu int nhn = 0; 43506b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 43516b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 43526b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 435351ebd318SNicolas Dichtel 43543b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 43553b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 43563b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 43573b1137feSDavid Ahern 435835f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 435951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 436051ebd318SNicolas Dichtel 43616b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 43628d1c802bSDavid Ahern * fib6_info structs per nexthop 43636b9ea5a6SRoopa Prabhu */ 436451ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 436551ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 436651ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 436751ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 436851ebd318SNicolas Dichtel 436951ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 437051ebd318SNicolas Dichtel if (attrlen > 0) { 437151ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 437251ebd318SNicolas Dichtel 437351ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 437451ebd318SNicolas Dichtel if (nla) { 437567b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 437651ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 437751ebd318SNicolas Dichtel } 437819e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 437919e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 438019e42e45SRoopa Prabhu if (nla) 438119e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 438251ebd318SNicolas Dichtel } 43836b9ea5a6SRoopa Prabhu 438468e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 4385acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 43868c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 43878c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 43888c5b83f0SRoopa Prabhu rt = NULL; 43896b9ea5a6SRoopa Prabhu goto cleanup; 43908c5b83f0SRoopa Prabhu } 43916b9ea5a6SRoopa Prabhu 43925e670d84SDavid Ahern rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1; 4393398958aeSIdo Schimmel 4394d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 4395d4ead6b3SDavid Ahern rt, &r_cfg); 439651ebd318SNicolas Dichtel if (err) { 439793531c67SDavid Ahern fib6_info_release(rt); 43986b9ea5a6SRoopa Prabhu goto cleanup; 439951ebd318SNicolas Dichtel } 44006b9ea5a6SRoopa Prabhu 44016b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 440251ebd318SNicolas Dichtel } 44036b9ea5a6SRoopa Prabhu 44043b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 44053b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 44063b1137feSDavid Ahern * the full route when done 44073b1137feSDavid Ahern */ 44083b1137feSDavid Ahern info->skip_notify = 1; 44093b1137feSDavid Ahern 44106b9ea5a6SRoopa Prabhu err_nh = NULL; 44116b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 44128d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 44138d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 44143b1137feSDavid Ahern 4415f7225172SDavid Ahern if (!err) { 4416f7225172SDavid Ahern /* save reference to last route successfully inserted */ 4417f7225172SDavid Ahern rt_last = nh->fib6_info; 4418f7225172SDavid Ahern 44196b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 4420f7225172SDavid Ahern if (!rt_notif) 44218d1c802bSDavid Ahern rt_notif = nh->fib6_info; 4422f7225172SDavid Ahern } 44236b9ea5a6SRoopa Prabhu 44248d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 44258d1c802bSDavid Ahern nh->fib6_info = NULL; 44266b9ea5a6SRoopa Prabhu if (err) { 44276b9ea5a6SRoopa Prabhu if (replace && nhn) 44286b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 44296b9ea5a6SRoopa Prabhu err_nh = nh; 44306b9ea5a6SRoopa Prabhu goto add_errout; 44316b9ea5a6SRoopa Prabhu } 44326b9ea5a6SRoopa Prabhu 44331a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 443427596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 443527596472SMichal Kubeček * we have already failed to add the first nexthop: 443627596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 443727596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 443827596472SMichal Kubeček * be added to it. 44391a72418bSNicolas Dichtel */ 444027596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 444127596472SMichal Kubeček NLM_F_REPLACE); 44426b9ea5a6SRoopa Prabhu nhn++; 44436b9ea5a6SRoopa Prabhu } 44446b9ea5a6SRoopa Prabhu 44453b1137feSDavid Ahern /* success ... tell user about new route */ 44463b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 44476b9ea5a6SRoopa Prabhu goto cleanup; 44486b9ea5a6SRoopa Prabhu 44496b9ea5a6SRoopa Prabhu add_errout: 44503b1137feSDavid Ahern /* send notification for routes that were added so that 44513b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 44523b1137feSDavid Ahern * coherent 44533b1137feSDavid Ahern */ 44543b1137feSDavid Ahern if (rt_notif) 44553b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 44563b1137feSDavid Ahern 44576b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 44586b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 44596b9ea5a6SRoopa Prabhu if (err_nh == nh) 44606b9ea5a6SRoopa Prabhu break; 4461333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 44626b9ea5a6SRoopa Prabhu } 44636b9ea5a6SRoopa Prabhu 44646b9ea5a6SRoopa Prabhu cleanup: 44656b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 44668d1c802bSDavid Ahern if (nh->fib6_info) 44678d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 44686b9ea5a6SRoopa Prabhu list_del(&nh->next); 44696b9ea5a6SRoopa Prabhu kfree(nh); 44706b9ea5a6SRoopa Prabhu } 44716b9ea5a6SRoopa Prabhu 44726b9ea5a6SRoopa Prabhu return err; 44736b9ea5a6SRoopa Prabhu } 44746b9ea5a6SRoopa Prabhu 4475333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 4476333c4301SDavid Ahern struct netlink_ext_ack *extack) 44776b9ea5a6SRoopa Prabhu { 44786b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 44796b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 44806b9ea5a6SRoopa Prabhu int remaining; 44816b9ea5a6SRoopa Prabhu int attrlen; 44826b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 44836b9ea5a6SRoopa Prabhu 44846b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 44856b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 44866b9ea5a6SRoopa Prabhu 44876b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 44886b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 44896b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 44906b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 44916b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 44926b9ea5a6SRoopa Prabhu 44936b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 44946b9ea5a6SRoopa Prabhu if (attrlen > 0) { 44956b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 44966b9ea5a6SRoopa Prabhu 44976b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 44986b9ea5a6SRoopa Prabhu if (nla) { 44996b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 45006b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 45016b9ea5a6SRoopa Prabhu } 45026b9ea5a6SRoopa Prabhu } 4503333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 45046b9ea5a6SRoopa Prabhu if (err) 45056b9ea5a6SRoopa Prabhu last_err = err; 45066b9ea5a6SRoopa Prabhu 450751ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 450851ebd318SNicolas Dichtel } 450951ebd318SNicolas Dichtel 451051ebd318SNicolas Dichtel return last_err; 451151ebd318SNicolas Dichtel } 451251ebd318SNicolas Dichtel 4513c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4514c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45151da177e4SLinus Torvalds { 451686872cb5SThomas Graf struct fib6_config cfg; 451786872cb5SThomas Graf int err; 45181da177e4SLinus Torvalds 4519333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 452086872cb5SThomas Graf if (err < 0) 452186872cb5SThomas Graf return err; 452286872cb5SThomas Graf 452351ebd318SNicolas Dichtel if (cfg.fc_mp) 4524333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 45250ae81335SDavid Ahern else { 45260ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 4527333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 45281da177e4SLinus Torvalds } 45290ae81335SDavid Ahern } 45301da177e4SLinus Torvalds 4531c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4532c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45331da177e4SLinus Torvalds { 453486872cb5SThomas Graf struct fib6_config cfg; 453586872cb5SThomas Graf int err; 45361da177e4SLinus Torvalds 4537333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 453886872cb5SThomas Graf if (err < 0) 453986872cb5SThomas Graf return err; 454086872cb5SThomas Graf 454151ebd318SNicolas Dichtel if (cfg.fc_mp) 4542333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 454351ebd318SNicolas Dichtel else 4544acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 45451da177e4SLinus Torvalds } 45461da177e4SLinus Torvalds 45478d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt) 4548339bf98fSThomas Graf { 4549beb1afacSDavid Ahern int nexthop_len = 0; 4550beb1afacSDavid Ahern 455193c2fb25SDavid Ahern if (rt->fib6_nsiblings) { 4552beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 4553beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 4554beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 45555e670d84SDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate); 4556beb1afacSDavid Ahern 455793c2fb25SDavid Ahern nexthop_len *= rt->fib6_nsiblings; 4558beb1afacSDavid Ahern } 4559beb1afacSDavid Ahern 4560339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 4561339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 4562339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 4563339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 4564339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 4565339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 4566339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 4567339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 4568339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 45696a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 4570ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 4571c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 457219e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 45735e670d84SDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate) 4574beb1afacSDavid Ahern + nexthop_len; 4575beb1afacSDavid Ahern } 4576beb1afacSDavid Ahern 45778d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt, 45785be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 4579beb1afacSDavid Ahern { 45805e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) 4581f9d882eaSIdo Schimmel *flags |= RTNH_F_DEAD; 4582f9d882eaSIdo Schimmel 45835e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) { 4584beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 4585dcd1f572SDavid Ahern 4586dcd1f572SDavid Ahern rcu_read_lock(); 4587dcd1f572SDavid Ahern if (fib6_ignore_linkdown(rt)) 4588beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 4589dcd1f572SDavid Ahern rcu_read_unlock(); 4590beb1afacSDavid Ahern } 4591beb1afacSDavid Ahern 459293c2fb25SDavid Ahern if (rt->fib6_flags & RTF_GATEWAY) { 45935e670d84SDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0) 4594beb1afacSDavid Ahern goto nla_put_failure; 4595beb1afacSDavid Ahern } 4596beb1afacSDavid Ahern 45975e670d84SDavid Ahern *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK); 45985e670d84SDavid Ahern if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD) 459961e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 460061e4d01eSIdo Schimmel 46015be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 46025e670d84SDavid Ahern if (!skip_oif && rt->fib6_nh.nh_dev && 46035e670d84SDavid Ahern nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex)) 4604beb1afacSDavid Ahern goto nla_put_failure; 4605beb1afacSDavid Ahern 46065e670d84SDavid Ahern if (rt->fib6_nh.nh_lwtstate && 46075e670d84SDavid Ahern lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0) 4608beb1afacSDavid Ahern goto nla_put_failure; 4609beb1afacSDavid Ahern 4610beb1afacSDavid Ahern return 0; 4611beb1afacSDavid Ahern 4612beb1afacSDavid Ahern nla_put_failure: 4613beb1afacSDavid Ahern return -EMSGSIZE; 4614beb1afacSDavid Ahern } 4615beb1afacSDavid Ahern 46165be083ceSDavid Ahern /* add multipath next hop */ 46178d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt) 4618beb1afacSDavid Ahern { 46195e670d84SDavid Ahern const struct net_device *dev = rt->fib6_nh.nh_dev; 4620beb1afacSDavid Ahern struct rtnexthop *rtnh; 4621beb1afacSDavid Ahern unsigned int flags = 0; 4622beb1afacSDavid Ahern 4623beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 4624beb1afacSDavid Ahern if (!rtnh) 4625beb1afacSDavid Ahern goto nla_put_failure; 4626beb1afacSDavid Ahern 46275e670d84SDavid Ahern rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1; 46285e670d84SDavid Ahern rtnh->rtnh_ifindex = dev ? dev->ifindex : 0; 4629beb1afacSDavid Ahern 46305be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 4631beb1afacSDavid Ahern goto nla_put_failure; 4632beb1afacSDavid Ahern 4633beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 4634beb1afacSDavid Ahern 4635beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4636beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4637beb1afacSDavid Ahern 4638beb1afacSDavid Ahern return 0; 4639beb1afacSDavid Ahern 4640beb1afacSDavid Ahern nla_put_failure: 4641beb1afacSDavid Ahern return -EMSGSIZE; 4642339bf98fSThomas Graf } 4643339bf98fSThomas Graf 4644d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 46458d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 4646d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 464715e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4648f8cfe2ceSDavid Ahern unsigned int flags) 46491da177e4SLinus Torvalds { 46501da177e4SLinus Torvalds struct rtmsg *rtm; 46511da177e4SLinus Torvalds struct nlmsghdr *nlh; 4652d4ead6b3SDavid Ahern long expires = 0; 4653d4ead6b3SDavid Ahern u32 *pmetrics; 46549e762a4aSPatrick McHardy u32 table; 46551da177e4SLinus Torvalds 465615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 465738308473SDavid S. Miller if (!nlh) 465826932566SPatrick McHardy return -EMSGSIZE; 46592d7202bfSThomas Graf 46602d7202bfSThomas Graf rtm = nlmsg_data(nlh); 46611da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 466293c2fb25SDavid Ahern rtm->rtm_dst_len = rt->fib6_dst.plen; 466393c2fb25SDavid Ahern rtm->rtm_src_len = rt->fib6_src.plen; 46641da177e4SLinus Torvalds rtm->rtm_tos = 0; 466593c2fb25SDavid Ahern if (rt->fib6_table) 466693c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 4667c71099acSThomas Graf else 46689e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 46699e762a4aSPatrick McHardy rtm->rtm_table = table; 4670c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4671c78679e8SDavid S. Miller goto nla_put_failure; 4672e8478e80SDavid Ahern 4673e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 46741da177e4SLinus Torvalds rtm->rtm_flags = 0; 46751da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 467693c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 46771da177e4SLinus Torvalds 467893c2fb25SDavid Ahern if (rt->fib6_flags & RTF_CACHE) 46791da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 46801da177e4SLinus Torvalds 4681d4ead6b3SDavid Ahern if (dest) { 4682d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 4683c78679e8SDavid S. Miller goto nla_put_failure; 46841da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 46851da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 468693c2fb25SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr)) 4687c78679e8SDavid S. Miller goto nla_put_failure; 46881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 46891da177e4SLinus Torvalds if (src) { 4690930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4691c78679e8SDavid S. Miller goto nla_put_failure; 46921da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4693c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 469493c2fb25SDavid Ahern nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr)) 4695c78679e8SDavid S. Miller goto nla_put_failure; 46961da177e4SLinus Torvalds #endif 46977bc570c8SYOSHIFUJI Hideaki if (iif) { 46987bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 469993c2fb25SDavid Ahern if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) { 4700fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 47012cf75070SNikolay Aleksandrov 47027bc570c8SYOSHIFUJI Hideaki if (err == 0) 47037bc570c8SYOSHIFUJI Hideaki return 0; 4704fd61c6baSDavid Ahern if (err < 0) 47057bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 47067bc570c8SYOSHIFUJI Hideaki } else 47077bc570c8SYOSHIFUJI Hideaki #endif 4708c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4709c78679e8SDavid S. Miller goto nla_put_failure; 4710d4ead6b3SDavid Ahern } else if (dest) { 47111da177e4SLinus Torvalds struct in6_addr saddr_buf; 4712d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 4713930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4714c78679e8SDavid S. Miller goto nla_put_failure; 4715c3968a85SDaniel Walter } 4716c3968a85SDaniel Walter 471793c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 4718c3968a85SDaniel Walter struct in6_addr saddr_buf; 471993c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 4720930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4721c78679e8SDavid S. Miller goto nla_put_failure; 47221da177e4SLinus Torvalds } 47232d7202bfSThomas Graf 4724d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 4725d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 47262d7202bfSThomas Graf goto nla_put_failure; 47272d7202bfSThomas Graf 472893c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 4729beb1afacSDavid Ahern goto nla_put_failure; 4730beb1afacSDavid Ahern 4731beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4732beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4733beb1afacSDavid Ahern */ 473493c2fb25SDavid Ahern if (rt->fib6_nsiblings) { 47358d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 4736beb1afacSDavid Ahern struct nlattr *mp; 4737beb1afacSDavid Ahern 4738beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4739beb1afacSDavid Ahern if (!mp) 4740beb1afacSDavid Ahern goto nla_put_failure; 4741beb1afacSDavid Ahern 4742beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4743beb1afacSDavid Ahern goto nla_put_failure; 4744beb1afacSDavid Ahern 4745beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 474693c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 4747beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 474894f826b8SEric Dumazet goto nla_put_failure; 474994f826b8SEric Dumazet } 47502d7202bfSThomas Graf 4751beb1afacSDavid Ahern nla_nest_end(skb, mp); 4752beb1afacSDavid Ahern } else { 47535be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4754c78679e8SDavid S. Miller goto nla_put_failure; 4755beb1afacSDavid Ahern } 47568253947eSLi Wei 475793c2fb25SDavid Ahern if (rt->fib6_flags & RTF_EXPIRES) { 475814895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 475914895687SDavid Ahern expires -= jiffies; 476014895687SDavid Ahern } 476169cdf8f9SYOSHIFUJI Hideaki 4762d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 4763e3703b3dSThomas Graf goto nla_put_failure; 47641da177e4SLinus Torvalds 476593c2fb25SDavid Ahern if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags))) 4766c78ba6d6SLubomir Rintel goto nla_put_failure; 4767c78ba6d6SLubomir Rintel 476819e42e45SRoopa Prabhu 4769053c095aSJohannes Berg nlmsg_end(skb, nlh); 4770053c095aSJohannes Berg return 0; 47712d7202bfSThomas Graf 47722d7202bfSThomas Graf nla_put_failure: 477326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 477426932566SPatrick McHardy return -EMSGSIZE; 47751da177e4SLinus Torvalds } 47761da177e4SLinus Torvalds 47778d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg) 47781da177e4SLinus Torvalds { 47791da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 47801f17e2f2SDavid Ahern struct net *net = arg->net; 47811f17e2f2SDavid Ahern 4782421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 47831f17e2f2SDavid Ahern return 0; 47841da177e4SLinus Torvalds 47852d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 47862d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4787f8cfe2ceSDavid Ahern 4788f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4789f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 479093c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 4791f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4792f8cfe2ceSDavid Ahern return 1; 4793f8cfe2ceSDavid Ahern } 4794f8cfe2ceSDavid Ahern } 47951da177e4SLinus Torvalds 4796d4ead6b3SDavid Ahern return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, 4797d4ead6b3SDavid Ahern RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, 4798d4ead6b3SDavid Ahern arg->cb->nlh->nlmsg_seq, NLM_F_MULTI); 47991da177e4SLinus Torvalds } 48001da177e4SLinus Torvalds 4801c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4802c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 48031da177e4SLinus Torvalds { 48043b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4805ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 480618c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 4807a68886a6SDavid Ahern struct fib6_info *from; 480818c3a61cSRoopa Prabhu struct dst_entry *dst; 48091da177e4SLinus Torvalds struct rt6_info *rt; 4810ab364a6fSThomas Graf struct sk_buff *skb; 4811ab364a6fSThomas Graf struct rtmsg *rtm; 48124c9483b2SDavid S. Miller struct flowi6 fl6; 481318c3a61cSRoopa Prabhu bool fibmatch; 4814ab364a6fSThomas Graf 4815fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4816c21ef3e3SDavid Ahern extack); 4817ab364a6fSThomas Graf if (err < 0) 4818ab364a6fSThomas Graf goto errout; 4819ab364a6fSThomas Graf 4820ab364a6fSThomas Graf err = -EINVAL; 48214c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 482238b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 482338b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 482418c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4825ab364a6fSThomas Graf 4826ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4827ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4828ab364a6fSThomas Graf goto errout; 4829ab364a6fSThomas Graf 48304e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4831ab364a6fSThomas Graf } 4832ab364a6fSThomas Graf 4833ab364a6fSThomas Graf if (tb[RTA_DST]) { 4834ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4835ab364a6fSThomas Graf goto errout; 4836ab364a6fSThomas Graf 48374e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4838ab364a6fSThomas Graf } 4839ab364a6fSThomas Graf 4840ab364a6fSThomas Graf if (tb[RTA_IIF]) 4841ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4842ab364a6fSThomas Graf 4843ab364a6fSThomas Graf if (tb[RTA_OIF]) 484472331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4845ab364a6fSThomas Graf 48462e47b291SLorenzo Colitti if (tb[RTA_MARK]) 48472e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 48482e47b291SLorenzo Colitti 4849622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4850622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4851622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4852622ec2c9SLorenzo Colitti else 4853622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4854622ec2c9SLorenzo Colitti 4855eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 4856eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 4857eacb9384SRoopa Prabhu 4858eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 4859eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 4860eacb9384SRoopa Prabhu 4861eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 4862eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 4863eacb9384SRoopa Prabhu &fl6.flowi6_proto, extack); 4864eacb9384SRoopa Prabhu if (err) 4865eacb9384SRoopa Prabhu goto errout; 4866eacb9384SRoopa Prabhu } 4867eacb9384SRoopa Prabhu 4868ab364a6fSThomas Graf if (iif) { 4869ab364a6fSThomas Graf struct net_device *dev; 487072331bc0SShmulik Ladkani int flags = 0; 487172331bc0SShmulik Ladkani 4872121622dbSFlorian Westphal rcu_read_lock(); 4873121622dbSFlorian Westphal 4874121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4875ab364a6fSThomas Graf if (!dev) { 4876121622dbSFlorian Westphal rcu_read_unlock(); 4877ab364a6fSThomas Graf err = -ENODEV; 4878ab364a6fSThomas Graf goto errout; 4879ab364a6fSThomas Graf } 488072331bc0SShmulik Ladkani 488172331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 488272331bc0SShmulik Ladkani 488372331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 488472331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 488572331bc0SShmulik Ladkani 4886b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 4887121622dbSFlorian Westphal 4888121622dbSFlorian Westphal rcu_read_unlock(); 488972331bc0SShmulik Ladkani } else { 489072331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 489172331bc0SShmulik Ladkani 489218c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 489318c3a61cSRoopa Prabhu } 489418c3a61cSRoopa Prabhu 489518c3a61cSRoopa Prabhu 489618c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 489718c3a61cSRoopa Prabhu if (rt->dst.error) { 489818c3a61cSRoopa Prabhu err = rt->dst.error; 489918c3a61cSRoopa Prabhu ip6_rt_put(rt); 490018c3a61cSRoopa Prabhu goto errout; 4901ab364a6fSThomas Graf } 49021da177e4SLinus Torvalds 49039d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 49049d6acb3bSWANG Cong err = rt->dst.error; 49059d6acb3bSWANG Cong ip6_rt_put(rt); 49069d6acb3bSWANG Cong goto errout; 49079d6acb3bSWANG Cong } 49089d6acb3bSWANG Cong 49091da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 491038308473SDavid S. Miller if (!skb) { 491194e187c0SAmerigo Wang ip6_rt_put(rt); 4912ab364a6fSThomas Graf err = -ENOBUFS; 4913ab364a6fSThomas Graf goto errout; 4914ab364a6fSThomas Graf } 49151da177e4SLinus Torvalds 4916d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 4917a68886a6SDavid Ahern 4918a68886a6SDavid Ahern rcu_read_lock(); 4919a68886a6SDavid Ahern from = rcu_dereference(rt->from); 4920a68886a6SDavid Ahern 492118c3a61cSRoopa Prabhu if (fibmatch) 4922a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif, 492318c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 492418c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 492518c3a61cSRoopa Prabhu else 4926a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 4927a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 4928d4ead6b3SDavid Ahern NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 4929d4ead6b3SDavid Ahern 0); 4930a68886a6SDavid Ahern rcu_read_unlock(); 4931a68886a6SDavid Ahern 49321da177e4SLinus Torvalds if (err < 0) { 4933ab364a6fSThomas Graf kfree_skb(skb); 4934ab364a6fSThomas Graf goto errout; 49351da177e4SLinus Torvalds } 49361da177e4SLinus Torvalds 493715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4938ab364a6fSThomas Graf errout: 49391da177e4SLinus Torvalds return err; 49401da177e4SLinus Torvalds } 49411da177e4SLinus Torvalds 49428d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 494337a1d361SRoopa Prabhu unsigned int nlm_flags) 49441da177e4SLinus Torvalds { 49451da177e4SLinus Torvalds struct sk_buff *skb; 49465578689aSDaniel Lezcano struct net *net = info->nl_net; 4947528c4cebSDenis V. Lunev u32 seq; 4948528c4cebSDenis V. Lunev int err; 49490d51aa80SJamal Hadi Salim 4950528c4cebSDenis V. Lunev err = -ENOBUFS; 495138308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 495286872cb5SThomas Graf 495319e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 495438308473SDavid S. Miller if (!skb) 495521713ebcSThomas Graf goto errout; 49561da177e4SLinus Torvalds 4957d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 4958f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 495926932566SPatrick McHardy if (err < 0) { 496026932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 496126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 496226932566SPatrick McHardy kfree_skb(skb); 496326932566SPatrick McHardy goto errout; 496426932566SPatrick McHardy } 496515e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 49665578689aSDaniel Lezcano info->nlh, gfp_any()); 49671ce85fe4SPablo Neira Ayuso return; 496821713ebcSThomas Graf errout: 496921713ebcSThomas Graf if (err < 0) 49705578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 49711da177e4SLinus Torvalds } 49721da177e4SLinus Torvalds 49738ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4974351638e7SJiri Pirko unsigned long event, void *ptr) 49758ed67789SDaniel Lezcano { 4976351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4977c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 49788ed67789SDaniel Lezcano 4979242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4980242d3a49SWANG Cong return NOTIFY_OK; 4981242d3a49SWANG Cong 4982242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4983421842edSDavid Ahern net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev; 4984d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 49858ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 49868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4987d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 49888ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4989d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 49908ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 49918ed67789SDaniel Lezcano #endif 499276da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 499376da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 499476da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 499576da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 499676da0704SWANG Cong */ 499712d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4998242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 499912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 500012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 5001242d3a49SWANG Cong #endif 50028ed67789SDaniel Lezcano } 50038ed67789SDaniel Lezcano 50048ed67789SDaniel Lezcano return NOTIFY_OK; 50058ed67789SDaniel Lezcano } 50068ed67789SDaniel Lezcano 50071da177e4SLinus Torvalds /* 50081da177e4SLinus Torvalds * /proc 50091da177e4SLinus Torvalds */ 50101da177e4SLinus Torvalds 50111da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 50121da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 50131da177e4SLinus Torvalds { 501469ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 50151da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 501669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 501769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 501881eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 501969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 502069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 5021fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 502269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 50231da177e4SLinus Torvalds 50241da177e4SLinus Torvalds return 0; 50251da177e4SLinus Torvalds } 50261da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 50271da177e4SLinus Torvalds 50281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 50291da177e4SLinus Torvalds 50301da177e4SLinus Torvalds static 5031fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 50321da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 50331da177e4SLinus Torvalds { 5034c486da34SLucian Adrian Grijincu struct net *net; 5035c486da34SLucian Adrian Grijincu int delay; 5036c486da34SLucian Adrian Grijincu if (!write) 5037c486da34SLucian Adrian Grijincu return -EINVAL; 5038c486da34SLucian Adrian Grijincu 5039c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 5040c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 50418d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 50422ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 50431da177e4SLinus Torvalds return 0; 50441da177e4SLinus Torvalds } 50451da177e4SLinus Torvalds 5046fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 50471da177e4SLinus Torvalds { 50481da177e4SLinus Torvalds .procname = "flush", 50494990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 50501da177e4SLinus Torvalds .maxlen = sizeof(int), 505189c8b3a1SDave Jones .mode = 0200, 50526d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 50531da177e4SLinus Torvalds }, 50541da177e4SLinus Torvalds { 50551da177e4SLinus Torvalds .procname = "gc_thresh", 50569a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 50571da177e4SLinus Torvalds .maxlen = sizeof(int), 50581da177e4SLinus Torvalds .mode = 0644, 50596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 50601da177e4SLinus Torvalds }, 50611da177e4SLinus Torvalds { 50621da177e4SLinus Torvalds .procname = "max_size", 50634990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 50641da177e4SLinus Torvalds .maxlen = sizeof(int), 50651da177e4SLinus Torvalds .mode = 0644, 50666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 50671da177e4SLinus Torvalds }, 50681da177e4SLinus Torvalds { 50691da177e4SLinus Torvalds .procname = "gc_min_interval", 50704990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 50711da177e4SLinus Torvalds .maxlen = sizeof(int), 50721da177e4SLinus Torvalds .mode = 0644, 50736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 50741da177e4SLinus Torvalds }, 50751da177e4SLinus Torvalds { 50761da177e4SLinus Torvalds .procname = "gc_timeout", 50774990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 50781da177e4SLinus Torvalds .maxlen = sizeof(int), 50791da177e4SLinus Torvalds .mode = 0644, 50806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 50811da177e4SLinus Torvalds }, 50821da177e4SLinus Torvalds { 50831da177e4SLinus Torvalds .procname = "gc_interval", 50844990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 50851da177e4SLinus Torvalds .maxlen = sizeof(int), 50861da177e4SLinus Torvalds .mode = 0644, 50876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 50881da177e4SLinus Torvalds }, 50891da177e4SLinus Torvalds { 50901da177e4SLinus Torvalds .procname = "gc_elasticity", 50914990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 50921da177e4SLinus Torvalds .maxlen = sizeof(int), 50931da177e4SLinus Torvalds .mode = 0644, 5094f3d3f616SMin Zhang .proc_handler = proc_dointvec, 50951da177e4SLinus Torvalds }, 50961da177e4SLinus Torvalds { 50971da177e4SLinus Torvalds .procname = "mtu_expires", 50984990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 50991da177e4SLinus Torvalds .maxlen = sizeof(int), 51001da177e4SLinus Torvalds .mode = 0644, 51016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 51021da177e4SLinus Torvalds }, 51031da177e4SLinus Torvalds { 51041da177e4SLinus Torvalds .procname = "min_adv_mss", 51054990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 51061da177e4SLinus Torvalds .maxlen = sizeof(int), 51071da177e4SLinus Torvalds .mode = 0644, 5108f3d3f616SMin Zhang .proc_handler = proc_dointvec, 51091da177e4SLinus Torvalds }, 51101da177e4SLinus Torvalds { 51111da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 51124990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 51131da177e4SLinus Torvalds .maxlen = sizeof(int), 51141da177e4SLinus Torvalds .mode = 0644, 51156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 51161da177e4SLinus Torvalds }, 5117f8572d8fSEric W. Biederman { } 51181da177e4SLinus Torvalds }; 51191da177e4SLinus Torvalds 51202c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 5121760f2d01SDaniel Lezcano { 5122760f2d01SDaniel Lezcano struct ctl_table *table; 5123760f2d01SDaniel Lezcano 5124760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 5125760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 5126760f2d01SDaniel Lezcano GFP_KERNEL); 51275ee09105SYOSHIFUJI Hideaki 51285ee09105SYOSHIFUJI Hideaki if (table) { 51295ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 5130c486da34SLucian Adrian Grijincu table[0].extra1 = net; 513186393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 51325ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 51335ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 51345ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 51355ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 51365ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 51375ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 51385ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 51399c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 5140464dc801SEric W. Biederman 5141464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 5142464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 5143464dc801SEric W. Biederman table[0].procname = NULL; 51445ee09105SYOSHIFUJI Hideaki } 51455ee09105SYOSHIFUJI Hideaki 5146760f2d01SDaniel Lezcano return table; 5147760f2d01SDaniel Lezcano } 51481da177e4SLinus Torvalds #endif 51491da177e4SLinus Torvalds 51502c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 5151cdb18761SDaniel Lezcano { 5152633d424bSPavel Emelyanov int ret = -ENOMEM; 51538ed67789SDaniel Lezcano 515486393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 515586393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 5156f2fc6a54SBenjamin Thery 5157fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 5158fc66f95cSEric Dumazet goto out_ip6_dst_ops; 5159fc66f95cSEric Dumazet 5160421842edSDavid Ahern net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template, 5161421842edSDavid Ahern sizeof(*net->ipv6.fib6_null_entry), 5162421842edSDavid Ahern GFP_KERNEL); 5163421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 5164421842edSDavid Ahern goto out_ip6_dst_entries; 5165421842edSDavid Ahern 51668ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 51678ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 51688ed67789SDaniel Lezcano GFP_KERNEL); 51698ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 5170421842edSDavid Ahern goto out_fib6_null_entry; 5171d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 517262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 517362fa8a84SDavid S. Miller ip6_template_metrics, true); 51748ed67789SDaniel Lezcano 51758ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5176feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 51778ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 51788ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 51798ed67789SDaniel Lezcano GFP_KERNEL); 518068fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 518168fffc67SPeter Zijlstra goto out_ip6_null_entry; 5182d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 518362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 518462fa8a84SDavid S. Miller ip6_template_metrics, true); 51858ed67789SDaniel Lezcano 51868ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 51878ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 51888ed67789SDaniel Lezcano GFP_KERNEL); 518968fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 519068fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 5191d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 519262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 519362fa8a84SDavid S. Miller ip6_template_metrics, true); 51948ed67789SDaniel Lezcano #endif 51958ed67789SDaniel Lezcano 5196b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 5197b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 5198b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 5199b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 5200b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 5201b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 5202b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 5203b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 5204b339a47cSPeter Zijlstra 52056891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 52066891a346SBenjamin Thery 52078ed67789SDaniel Lezcano ret = 0; 52088ed67789SDaniel Lezcano out: 52098ed67789SDaniel Lezcano return ret; 5210f2fc6a54SBenjamin Thery 521168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 521268fffc67SPeter Zijlstra out_ip6_prohibit_entry: 521368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 521468fffc67SPeter Zijlstra out_ip6_null_entry: 521568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 521668fffc67SPeter Zijlstra #endif 5217421842edSDavid Ahern out_fib6_null_entry: 5218421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 5219fc66f95cSEric Dumazet out_ip6_dst_entries: 5220fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5221f2fc6a54SBenjamin Thery out_ip6_dst_ops: 5222f2fc6a54SBenjamin Thery goto out; 5223cdb18761SDaniel Lezcano } 5224cdb18761SDaniel Lezcano 52252c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 5226cdb18761SDaniel Lezcano { 5227421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 52288ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 52298ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 52308ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 52318ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 52328ed67789SDaniel Lezcano #endif 523341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5234cdb18761SDaniel Lezcano } 5235cdb18761SDaniel Lezcano 5236d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 5237d189634eSThomas Graf { 5238d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5239c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 5240c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 52413617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 52423617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 5243d189634eSThomas Graf #endif 5244d189634eSThomas Graf return 0; 5245d189634eSThomas Graf } 5246d189634eSThomas Graf 5247d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 5248d189634eSThomas Graf { 5249d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5250ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 5251ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 5252d189634eSThomas Graf #endif 5253d189634eSThomas Graf } 5254d189634eSThomas Graf 5255cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 5256cdb18761SDaniel Lezcano .init = ip6_route_net_init, 5257cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 5258cdb18761SDaniel Lezcano }; 5259cdb18761SDaniel Lezcano 5260c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 5261c3426b47SDavid S. Miller { 5262c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 5263c3426b47SDavid S. Miller 5264c3426b47SDavid S. Miller if (!bp) 5265c3426b47SDavid S. Miller return -ENOMEM; 5266c3426b47SDavid S. Miller inet_peer_base_init(bp); 5267c3426b47SDavid S. Miller net->ipv6.peers = bp; 5268c3426b47SDavid S. Miller return 0; 5269c3426b47SDavid S. Miller } 5270c3426b47SDavid S. Miller 5271c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5272c3426b47SDavid S. Miller { 5273c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5274c3426b47SDavid S. Miller 5275c3426b47SDavid S. Miller net->ipv6.peers = NULL; 527656a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5277c3426b47SDavid S. Miller kfree(bp); 5278c3426b47SDavid S. Miller } 5279c3426b47SDavid S. Miller 52802b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5281c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5282c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 5283c3426b47SDavid S. Miller }; 5284c3426b47SDavid S. Miller 5285d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 5286d189634eSThomas Graf .init = ip6_route_net_init_late, 5287d189634eSThomas Graf .exit = ip6_route_net_exit_late, 5288d189634eSThomas Graf }; 5289d189634eSThomas Graf 52908ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 52918ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 5292242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 52938ed67789SDaniel Lezcano }; 52948ed67789SDaniel Lezcano 52952f460933SWANG Cong void __init ip6_route_init_special_entries(void) 52962f460933SWANG Cong { 52972f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 52982f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 52992f460933SWANG Cong * manually for init_net */ 5300421842edSDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev; 53012f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 53022f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53032f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 53042f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 53052f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53062f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 53072f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53082f460933SWANG Cong #endif 53092f460933SWANG Cong } 53102f460933SWANG Cong 5311433d49c3SDaniel Lezcano int __init ip6_route_init(void) 53121da177e4SLinus Torvalds { 5313433d49c3SDaniel Lezcano int ret; 53148d0b94afSMartin KaFai Lau int cpu; 5315433d49c3SDaniel Lezcano 53169a7ec3a9SDaniel Lezcano ret = -ENOMEM; 53179a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 53189a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 53199a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 53209a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 5321c19a28e1SFernando Carrijo goto out; 532214e50e57SDavid S. Miller 5323fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 53248ed67789SDaniel Lezcano if (ret) 5325bdb3289fSDaniel Lezcano goto out_kmem_cache; 5326bdb3289fSDaniel Lezcano 5327c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 5328c3426b47SDavid S. Miller if (ret) 5329e8803b6cSDavid S. Miller goto out_dst_entries; 53302a0c451aSThomas Graf 53317e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 53327e52b33bSDavid S. Miller if (ret) 53337e52b33bSDavid S. Miller goto out_register_inetpeer; 5334c3426b47SDavid S. Miller 53355dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 53365dc121e9SArnaud Ebalard 5337e8803b6cSDavid S. Miller ret = fib6_init(); 5338433d49c3SDaniel Lezcano if (ret) 53398ed67789SDaniel Lezcano goto out_register_subsys; 5340433d49c3SDaniel Lezcano 5341433d49c3SDaniel Lezcano ret = xfrm6_init(); 5342433d49c3SDaniel Lezcano if (ret) 5343e8803b6cSDavid S. Miller goto out_fib6_init; 5344c35b7e72SDaniel Lezcano 5345433d49c3SDaniel Lezcano ret = fib6_rules_init(); 5346433d49c3SDaniel Lezcano if (ret) 5347433d49c3SDaniel Lezcano goto xfrm6_init; 53487e5449c2SDaniel Lezcano 5349d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 5350d189634eSThomas Graf if (ret) 5351d189634eSThomas Graf goto fib6_rules_init; 5352d189634eSThomas Graf 535316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 535416feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 535516feebcfSFlorian Westphal if (ret < 0) 535616feebcfSFlorian Westphal goto out_register_late_subsys; 535716feebcfSFlorian Westphal 535816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 535916feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 536016feebcfSFlorian Westphal if (ret < 0) 536116feebcfSFlorian Westphal goto out_register_late_subsys; 536216feebcfSFlorian Westphal 536316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 536416feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 536516feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 536616feebcfSFlorian Westphal if (ret < 0) 5367d189634eSThomas Graf goto out_register_late_subsys; 5368433d49c3SDaniel Lezcano 53698ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 5370cdb18761SDaniel Lezcano if (ret) 5371d189634eSThomas Graf goto out_register_late_subsys; 53728ed67789SDaniel Lezcano 53738d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 53748d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 53758d0b94afSMartin KaFai Lau 53768d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 53778d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 53788d0b94afSMartin KaFai Lau } 53798d0b94afSMartin KaFai Lau 5380433d49c3SDaniel Lezcano out: 5381433d49c3SDaniel Lezcano return ret; 5382433d49c3SDaniel Lezcano 5383d189634eSThomas Graf out_register_late_subsys: 538416feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 5385d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5386433d49c3SDaniel Lezcano fib6_rules_init: 5387433d49c3SDaniel Lezcano fib6_rules_cleanup(); 5388433d49c3SDaniel Lezcano xfrm6_init: 5389433d49c3SDaniel Lezcano xfrm6_fini(); 53902a0c451aSThomas Graf out_fib6_init: 53912a0c451aSThomas Graf fib6_gc_cleanup(); 53928ed67789SDaniel Lezcano out_register_subsys: 53938ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 53947e52b33bSDavid S. Miller out_register_inetpeer: 53957e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 5396fc66f95cSEric Dumazet out_dst_entries: 5397fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 5398433d49c3SDaniel Lezcano out_kmem_cache: 5399f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 5400433d49c3SDaniel Lezcano goto out; 54011da177e4SLinus Torvalds } 54021da177e4SLinus Torvalds 54031da177e4SLinus Torvalds void ip6_route_cleanup(void) 54041da177e4SLinus Torvalds { 54058ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 5406d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5407101367c2SThomas Graf fib6_rules_cleanup(); 54081da177e4SLinus Torvalds xfrm6_fini(); 54091da177e4SLinus Torvalds fib6_gc_cleanup(); 5410c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 54118ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 541241bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 5413f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 54141da177e4SLinus Torvalds } 5415