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); 105702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 106702cea56SDavid Ahern int strict); 1078d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt); 108d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 1098d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 110d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 11116a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 11216a16cd3SDavid Ahern unsigned int flags); 1138d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, 11435732d01SWei Wang struct in6_addr *daddr, 11535732d01SWei Wang struct in6_addr *saddr); 1161da177e4SLinus Torvalds 11770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1188d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 119b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 120830218c1SDavid Ahern const struct in6_addr *gwaddr, 121830218c1SDavid Ahern struct net_device *dev, 12295c96174SEric Dumazet unsigned int pref); 1238d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 124b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 125830218c1SDavid Ahern const struct in6_addr *gwaddr, 126830218c1SDavid Ahern struct net_device *dev); 12770ceb4f5SYOSHIFUJI Hideaki #endif 12870ceb4f5SYOSHIFUJI Hideaki 1298d0b94afSMartin KaFai Lau struct uncached_list { 1308d0b94afSMartin KaFai Lau spinlock_t lock; 1318d0b94afSMartin KaFai Lau struct list_head head; 1328d0b94afSMartin KaFai Lau }; 1338d0b94afSMartin KaFai Lau 1348d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1358d0b94afSMartin KaFai Lau 136510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt) 1378d0b94afSMartin KaFai Lau { 1388d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1398d0b94afSMartin KaFai Lau 1408d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1418d0b94afSMartin KaFai Lau 1428d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1438d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1448d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1458d0b94afSMartin KaFai Lau } 1468d0b94afSMartin KaFai Lau 147510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt) 1488d0b94afSMartin KaFai Lau { 1498d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1508d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 15181eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1528d0b94afSMartin KaFai Lau 1538d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1548d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15581eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1568d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1578d0b94afSMartin KaFai Lau } 1588d0b94afSMartin KaFai Lau } 1598d0b94afSMartin KaFai Lau 1608d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1618d0b94afSMartin KaFai Lau { 1628d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1638d0b94afSMartin KaFai Lau int cpu; 1648d0b94afSMartin KaFai Lau 165e332bc67SEric W. Biederman if (dev == loopback_dev) 166e332bc67SEric W. Biederman return; 167e332bc67SEric W. Biederman 1688d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1698d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1708d0b94afSMartin KaFai Lau struct rt6_info *rt; 1718d0b94afSMartin KaFai Lau 1728d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1738d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1748d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1758d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1768d0b94afSMartin KaFai Lau 177e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1788d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1798d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1808d0b94afSMartin KaFai Lau } 1818d0b94afSMartin KaFai Lau 182e332bc67SEric W. Biederman if (rt_dev == dev) { 1838d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1848d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1858d0b94afSMartin KaFai Lau dev_put(rt_dev); 1868d0b94afSMartin KaFai Lau } 1878d0b94afSMartin KaFai Lau } 1888d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1898d0b94afSMartin KaFai Lau } 1908d0b94afSMartin KaFai Lau } 1918d0b94afSMartin KaFai Lau 192f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p, 193f894cbf8SDavid S. Miller struct sk_buff *skb, 194f894cbf8SDavid S. Miller const void *daddr) 19539232973SDavid S. Miller { 196a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19739232973SDavid S. Miller return (const void *) p; 198f894cbf8SDavid S. Miller else if (skb) 199f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 20039232973SDavid S. Miller return daddr; 20139232973SDavid S. Miller } 20239232973SDavid S. Miller 203f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, 204f8a1b43bSDavid Ahern struct net_device *dev, 205f894cbf8SDavid S. Miller struct sk_buff *skb, 206f894cbf8SDavid S. Miller const void *daddr) 207d3aaeb38SDavid S. Miller { 20839232973SDavid S. Miller struct neighbour *n; 20939232973SDavid S. Miller 210f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(gw, skb, daddr); 211f8a1b43bSDavid Ahern n = __ipv6_neigh_lookup(dev, daddr); 212f83c7790SDavid S. Miller if (n) 213f83c7790SDavid S. Miller return n; 2147adf3246SStefano Brivio 2157adf3246SStefano Brivio n = neigh_create(&nd_tbl, daddr, dev); 2167adf3246SStefano Brivio return IS_ERR(n) ? NULL : n; 217f8a1b43bSDavid Ahern } 218f8a1b43bSDavid Ahern 219f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, 220f8a1b43bSDavid Ahern struct sk_buff *skb, 221f8a1b43bSDavid Ahern const void *daddr) 222f8a1b43bSDavid Ahern { 223f8a1b43bSDavid Ahern const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); 224f8a1b43bSDavid Ahern 225f8a1b43bSDavid Ahern return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); 226f83c7790SDavid S. Miller } 227f83c7790SDavid S. Miller 22863fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22963fca65dSJulian Anastasov { 23063fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23163fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23263fca65dSJulian Anastasov 233f8a1b43bSDavid Ahern daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); 23463fca65dSJulian Anastasov if (!daddr) 23563fca65dSJulian Anastasov return; 23663fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23763fca65dSJulian Anastasov return; 23863fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23963fca65dSJulian Anastasov return; 24063fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24163fca65dSJulian Anastasov } 24263fca65dSJulian Anastasov 2439a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2441da177e4SLinus Torvalds .family = AF_INET6, 2451da177e4SLinus Torvalds .gc = ip6_dst_gc, 2461da177e4SLinus Torvalds .gc_thresh = 1024, 2471da177e4SLinus Torvalds .check = ip6_dst_check, 2480dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 249ebb762f2SSteffen Klassert .mtu = ip6_mtu, 250d4ead6b3SDavid Ahern .cow_metrics = dst_cow_metrics_generic, 2511da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2521da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2531da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2541da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2551da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2566e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2579f8955ccSEric W. Biederman .local_out = __ip6_local_out, 258f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 25963fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2601da177e4SLinus Torvalds }; 2611da177e4SLinus Torvalds 262ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 263ec831ea7SRoland Dreier { 264618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 265618f9bc7SSteffen Klassert 266618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 267ec831ea7SRoland Dreier } 268ec831ea7SRoland Dreier 2696700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2706700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27114e50e57SDavid S. Miller { 27214e50e57SDavid S. Miller } 27314e50e57SDavid S. Miller 2746700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2756700c270SDavid S. Miller struct sk_buff *skb) 276b587ee3bSDavid S. Miller { 277b587ee3bSDavid S. Miller } 278b587ee3bSDavid S. Miller 27914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28014e50e57SDavid S. Miller .family = AF_INET6, 28114e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28214e50e57SDavid S. Miller .check = ip6_dst_check, 283ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 284214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28514e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 286b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2870a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 288f8a1b43bSDavid Ahern .neigh_lookup = ip6_dst_neigh_lookup, 28914e50e57SDavid S. Miller }; 29014e50e57SDavid S. Miller 29162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29214edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29362fa8a84SDavid S. Miller }; 29462fa8a84SDavid S. Miller 2958d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = { 29693c2fb25SDavid Ahern .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), 29793c2fb25SDavid Ahern .fib6_protocol = RTPROT_KERNEL, 29893c2fb25SDavid Ahern .fib6_metric = ~(u32)0, 29993c2fb25SDavid Ahern .fib6_ref = ATOMIC_INIT(1), 300421842edSDavid Ahern .fib6_type = RTN_UNREACHABLE, 301421842edSDavid Ahern .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, 302421842edSDavid Ahern }; 303421842edSDavid Ahern 304fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 3051da177e4SLinus Torvalds .dst = { 3061da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3071da177e4SLinus Torvalds .__use = 1, 3082c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3091da177e4SLinus Torvalds .error = -ENETUNREACH, 3101da177e4SLinus Torvalds .input = ip6_pkt_discard, 3111da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3121da177e4SLinus Torvalds }, 3131da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3141da177e4SLinus Torvalds }; 3151da177e4SLinus Torvalds 316101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 317101367c2SThomas Graf 318fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 319101367c2SThomas Graf .dst = { 320101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 321101367c2SThomas Graf .__use = 1, 3222c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 323101367c2SThomas Graf .error = -EACCES, 3249ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3259ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 326101367c2SThomas Graf }, 327101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 328101367c2SThomas Graf }; 329101367c2SThomas Graf 330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 331101367c2SThomas Graf .dst = { 332101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 333101367c2SThomas Graf .__use = 1, 3342c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 335101367c2SThomas Graf .error = -EINVAL, 336352e512cSHerbert Xu .input = dst_discard, 337ede2059dSEric W. Biederman .output = dst_discard_out, 338101367c2SThomas Graf }, 339101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 340101367c2SThomas Graf }; 341101367c2SThomas Graf 342101367c2SThomas Graf #endif 343101367c2SThomas Graf 344ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 345ebfa45f0SMartin KaFai Lau { 346ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 347ebfa45f0SMartin KaFai Lau 348ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 349ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 350ebfa45f0SMartin KaFai Lau } 351ebfa45f0SMartin KaFai Lau 3521da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 35393531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, 354ad706862SMartin KaFai Lau int flags) 3551da177e4SLinus Torvalds { 35697bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 357b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 358cf911662SDavid S. Miller 35981eb8447SWei Wang if (rt) { 360ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 36181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 36281eb8447SWei Wang } 3638104891bSSteffen Klassert 364cf911662SDavid S. Miller return rt; 3651da177e4SLinus Torvalds } 3669ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 367d52d3997SMartin KaFai Lau 3681da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 371a68886a6SDavid Ahern struct fib6_info *from; 3728d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3731da177e4SLinus Torvalds 3741620a336SDavid Ahern ip_dst_metrics_put(dst); 3758d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3768d0b94afSMartin KaFai Lau 3778d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37838308473SDavid S. Miller if (idev) { 3791da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3801da177e4SLinus Torvalds in6_dev_put(idev); 3811da177e4SLinus Torvalds } 3821716a961SGao feng 383a68886a6SDavid Ahern rcu_read_lock(); 384a68886a6SDavid Ahern from = rcu_dereference(rt->from); 385a68886a6SDavid Ahern rcu_assign_pointer(rt->from, NULL); 38693531c67SDavid Ahern fib6_info_release(from); 387a68886a6SDavid Ahern rcu_read_unlock(); 388b3419363SDavid S. Miller } 389b3419363SDavid S. Miller 3901da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3911da177e4SLinus Torvalds int how) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3941da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3955a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 396c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3971da177e4SLinus Torvalds 398e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 399e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 40038308473SDavid S. Miller if (loopback_idev) { 4011da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4021da177e4SLinus Torvalds in6_dev_put(idev); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds } 40597cac082SDavid S. Miller } 4061da177e4SLinus Torvalds 4075973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4085973fb1eSMartin KaFai Lau { 4095973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4105973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4115973fb1eSMartin KaFai Lau else 4125973fb1eSMartin KaFai Lau return false; 4135973fb1eSMartin KaFai Lau } 4145973fb1eSMartin KaFai Lau 415a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4161da177e4SLinus Torvalds { 417a68886a6SDavid Ahern struct fib6_info *from; 418a68886a6SDavid Ahern 419a68886a6SDavid Ahern from = rcu_dereference(rt->from); 420a68886a6SDavid Ahern 4211716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4221716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 423a50feda5SEric Dumazet return true; 424a68886a6SDavid Ahern } else if (from) { 4251e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 426a68886a6SDavid Ahern fib6_check_expired(from); 4271716a961SGao feng } 428a50feda5SEric Dumazet return false; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4313b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net, 4328d1c802bSDavid Ahern struct fib6_info *match, 43352bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 434b75cc8f9SDavid Ahern const struct sk_buff *skb, 43552bd4c0cSNicolas Dichtel int strict) 43651ebd318SNicolas Dichtel { 4378d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 43851ebd318SNicolas Dichtel 439b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 440b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 441b673d6ccSJakub Sitnicki */ 442b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 443b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 444b673d6ccSJakub Sitnicki 445ad1601aeSDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_nh_upper_bound)) 4463d709f69SIdo Schimmel return match; 447bbfcd776SIdo Schimmel 44893c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 44993c2fb25SDavid Ahern fib6_siblings) { 450702cea56SDavid Ahern const struct fib6_nh *nh = &sibling->fib6_nh; 4515e670d84SDavid Ahern int nh_upper_bound; 4525e670d84SDavid Ahern 453702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4545e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4553d709f69SIdo Schimmel continue; 456702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 45752bd4c0cSNicolas Dichtel break; 45851ebd318SNicolas Dichtel match = sibling; 45951ebd318SNicolas Dichtel break; 46051ebd318SNicolas Dichtel } 4613d709f69SIdo Schimmel 46251ebd318SNicolas Dichtel return match; 46351ebd318SNicolas Dichtel } 46451ebd318SNicolas Dichtel 4651da177e4SLinus Torvalds /* 46666f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds 4698d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net, 4708d1c802bSDavid Ahern struct fib6_info *rt, 471b71d1d42SEric Dumazet const struct in6_addr *saddr, 4721da177e4SLinus Torvalds int oif, 473d420895eSYOSHIFUJI Hideaki int flags) 4741da177e4SLinus Torvalds { 4758d1c802bSDavid Ahern struct fib6_info *sprt; 4761da177e4SLinus Torvalds 4775e670d84SDavid Ahern if (!oif && ipv6_addr_any(saddr) && 478ad1601aeSDavid Ahern !(rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)) 4798067bb8cSIdo Schimmel return rt; 480dd3abc4eSYOSHIFUJI Hideaki 4818fb11a9aSDavid Ahern for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) { 482ad1601aeSDavid Ahern const struct net_device *dev = sprt->fib6_nh.fib_nh_dev; 483dd3abc4eSYOSHIFUJI Hideaki 484ad1601aeSDavid Ahern if (sprt->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 4858067bb8cSIdo Schimmel continue; 4868067bb8cSIdo Schimmel 487dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4881da177e4SLinus Torvalds if (dev->ifindex == oif) 4891da177e4SLinus Torvalds return sprt; 490dd3abc4eSYOSHIFUJI Hideaki } else { 491dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 492dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 493dd3abc4eSYOSHIFUJI Hideaki return sprt; 494dd3abc4eSYOSHIFUJI Hideaki } 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 497eea68cd3SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) 498421842edSDavid Ahern return net->ipv6.fib6_null_entry; 4991da177e4SLinus Torvalds 500ad1601aeSDavid Ahern return rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 50327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 504c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 505c2f17e82SHannes Frederic Sowa struct work_struct work; 506c2f17e82SHannes Frederic Sowa struct in6_addr target; 507c2f17e82SHannes Frederic Sowa struct net_device *dev; 508c2f17e82SHannes Frederic Sowa }; 509c2f17e82SHannes Frederic Sowa 510c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 511c2f17e82SHannes Frederic Sowa { 512c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 513c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 514c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 515c2f17e82SHannes Frederic Sowa 516c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 517adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 518c2f17e82SHannes Frederic Sowa dev_put(work->dev); 519662f5533SMichael Büsch kfree(work); 520c2f17e82SHannes Frederic Sowa } 521c2f17e82SHannes Frederic Sowa 522cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 52327097255SYOSHIFUJI Hideaki { 524f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 5255e670d84SDavid Ahern const struct in6_addr *nh_gw; 526f2c31e32SEric Dumazet struct neighbour *neigh; 5275e670d84SDavid Ahern struct net_device *dev; 528f547fac6SSabrina Dubroca struct inet6_dev *idev; 5295e670d84SDavid Ahern 53027097255SYOSHIFUJI Hideaki /* 53127097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 53227097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 53327097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 53427097255SYOSHIFUJI Hideaki * 53527097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 53627097255SYOSHIFUJI Hideaki * to no more than one per minute. 53727097255SYOSHIFUJI Hideaki */ 538cc3a86c8SDavid Ahern if (fib6_nh->fib_nh_gw_family) 539fdd6681dSAmerigo Wang return; 5405e670d84SDavid Ahern 541cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 542cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 5432152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 544f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 5455e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 5462152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5478d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5488d6c31bfSMartin KaFai Lau goto out; 5498d6c31bfSMartin KaFai Lau 5502152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 551990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 552990edb42SMartin KaFai Lau time_after(jiffies, 553dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 554c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 555990edb42SMartin KaFai Lau if (work) 5567e980569SJiri Benc __neigh_set_probe_once(neigh); 557990edb42SMartin KaFai Lau } 558c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 559cc3a86c8SDavid Ahern } else if (time_after(jiffies, fib6_nh->last_probe + 560f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 561990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 562990edb42SMartin KaFai Lau } 563c2f17e82SHannes Frederic Sowa 564c2f17e82SHannes Frederic Sowa if (work) { 565cc3a86c8SDavid Ahern fib6_nh->last_probe = jiffies; 566c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 5675e670d84SDavid Ahern work->target = *nh_gw; 5685e670d84SDavid Ahern dev_hold(dev); 5695e670d84SDavid Ahern work->dev = dev; 570c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 571c2f17e82SHannes Frederic Sowa } 572990edb42SMartin KaFai Lau 5738d6c31bfSMartin KaFai Lau out: 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 575f2c31e32SEric Dumazet } 57627097255SYOSHIFUJI Hideaki #else 577cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 57827097255SYOSHIFUJI Hideaki { 57927097255SYOSHIFUJI Hideaki } 58027097255SYOSHIFUJI Hideaki #endif 58127097255SYOSHIFUJI Hideaki 5821da177e4SLinus Torvalds /* 583554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 5841da177e4SLinus Torvalds */ 5851ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 5861da177e4SLinus Torvalds { 587afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 5885e670d84SDavid Ahern struct neighbour *neigh; 589f2c31e32SEric Dumazet 590145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5911ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 5921ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 593145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 594145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 595554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 596afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 597398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 598a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 599afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6007e980569SJiri Benc else 6017e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 602398bcbebSYOSHIFUJI Hideaki #endif 603145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 604afc154e9SHannes Frederic Sowa } else { 605afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6067e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 607a5a81f0bSPaul Marks } 608145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 609145a3621SYOSHIFUJI Hideaki / 吉藤英明 610a5a81f0bSPaul Marks return ret; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 613702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 614702cea56SDavid Ahern int strict) 615554cfb7eSYOSHIFUJI Hideaki { 6166e1809a5SDavid Ahern int m = 0; 6174d0c5911SYOSHIFUJI Hideaki 6186e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 6196e1809a5SDavid Ahern m = 2; 6206e1809a5SDavid Ahern 62177d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 622afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 623ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 624702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 625ebacaaa0SYOSHIFUJI Hideaki #endif 6261ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 627702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 6281ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 629afc154e9SHannes Frederic Sowa if (n < 0) 630afc154e9SHannes Frederic Sowa return n; 631afc154e9SHannes Frederic Sowa } 632554cfb7eSYOSHIFUJI Hideaki return m; 633554cfb7eSYOSHIFUJI Hideaki } 634554cfb7eSYOSHIFUJI Hideaki 635*28679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 636*28679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 637554cfb7eSYOSHIFUJI Hideaki { 638afc154e9SHannes Frederic Sowa bool match_do_rr = false; 639*28679ed1SDavid Ahern bool rc = false; 640*28679ed1SDavid Ahern int m; 64135103d11SAndy Gospodarek 642*28679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 6438067bb8cSIdo Schimmel goto out; 6448067bb8cSIdo Schimmel 645*28679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 646*28679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 647d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 64835103d11SAndy Gospodarek goto out; 649554cfb7eSYOSHIFUJI Hideaki 650*28679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 6517e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 652afc154e9SHannes Frederic Sowa match_do_rr = true; 653afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6547e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 655f11e6659SDavid S. Miller goto out; 6561da177e4SLinus Torvalds } 657f11e6659SDavid S. Miller 658afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 659*28679ed1SDavid Ahern rt6_probe(nh); 660afc154e9SHannes Frederic Sowa 6617e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 662afc154e9SHannes Frederic Sowa if (m > *mpri) { 663afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 664afc154e9SHannes Frederic Sowa *mpri = m; 665*28679ed1SDavid Ahern rc = true; 666afc154e9SHannes Frederic Sowa } 667f11e6659SDavid S. Miller out: 668*28679ed1SDavid Ahern return rc; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6718d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn, 6728d1c802bSDavid Ahern struct fib6_info *leaf, 6738d1c802bSDavid Ahern struct fib6_info *rr_head, 674afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 675afc154e9SHannes Frederic Sowa bool *do_rr) 676f11e6659SDavid S. Miller { 6778d1c802bSDavid Ahern struct fib6_info *rt, *match, *cont; 678*28679ed1SDavid Ahern struct fib6_nh *nh; 679f11e6659SDavid S. Miller int mpri = -1; 680f11e6659SDavid S. Miller 681f11e6659SDavid S. Miller match = NULL; 6829fbdcfafSSteffen Klassert cont = NULL; 6838fb11a9aSDavid Ahern for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) { 68493c2fb25SDavid Ahern if (rt->fib6_metric != metric) { 6859fbdcfafSSteffen Klassert cont = rt; 6869fbdcfafSSteffen Klassert break; 6879fbdcfafSSteffen Klassert } 6889fbdcfafSSteffen Klassert 689*28679ed1SDavid Ahern if (fib6_check_expired(rt)) 690*28679ed1SDavid Ahern continue; 691*28679ed1SDavid Ahern 692*28679ed1SDavid Ahern nh = &rt->fib6_nh; 693*28679ed1SDavid Ahern if (find_match(nh, rt->fib6_flags, oif, strict, &mpri, do_rr)) 694*28679ed1SDavid Ahern match = rt; 6959fbdcfafSSteffen Klassert } 6969fbdcfafSSteffen Klassert 69766f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 6988fb11a9aSDavid Ahern rt = rcu_dereference(rt->fib6_next)) { 69993c2fb25SDavid Ahern if (rt->fib6_metric != metric) { 7009fbdcfafSSteffen Klassert cont = rt; 7019fbdcfafSSteffen Klassert break; 7029fbdcfafSSteffen Klassert } 7039fbdcfafSSteffen Klassert 704*28679ed1SDavid Ahern if (fib6_check_expired(rt)) 705*28679ed1SDavid Ahern continue; 706*28679ed1SDavid Ahern 707*28679ed1SDavid Ahern nh = &rt->fib6_nh; 708*28679ed1SDavid Ahern if (find_match(nh, rt->fib6_flags, oif, strict, &mpri, do_rr)) 709*28679ed1SDavid Ahern match = rt; 7109fbdcfafSSteffen Klassert } 7119fbdcfafSSteffen Klassert 7129fbdcfafSSteffen Klassert if (match || !cont) 7139fbdcfafSSteffen Klassert return match; 7149fbdcfafSSteffen Klassert 715*28679ed1SDavid Ahern for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next)) { 716*28679ed1SDavid Ahern if (fib6_check_expired(rt)) 717*28679ed1SDavid Ahern continue; 718*28679ed1SDavid Ahern 719*28679ed1SDavid Ahern nh = &rt->fib6_nh; 720*28679ed1SDavid Ahern if (find_match(nh, rt->fib6_flags, oif, strict, &mpri, do_rr)) 721*28679ed1SDavid Ahern match = rt; 722*28679ed1SDavid Ahern } 723f11e6659SDavid S. Miller 724f11e6659SDavid S. Miller return match; 725f11e6659SDavid S. Miller } 726f11e6659SDavid S. Miller 7278d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn, 7288d1040e8SWei Wang int oif, int strict) 729f11e6659SDavid S. Miller { 7308d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 7318d1c802bSDavid Ahern struct fib6_info *match, *rt0; 732afc154e9SHannes Frederic Sowa bool do_rr = false; 73317ecf590SWei Wang int key_plen; 734f11e6659SDavid S. Miller 735421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 736421842edSDavid Ahern return net->ipv6.fib6_null_entry; 7378d1040e8SWei Wang 73866f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 739f11e6659SDavid S. Miller if (!rt0) 74066f5d6ceSWei Wang rt0 = leaf; 741f11e6659SDavid S. Miller 74217ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 74317ecf590SWei Wang * and fn->leaf does not points to its child's leaf 74417ecf590SWei Wang * (This might happen if all routes under fn are deleted from 74517ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 74617ecf590SWei Wang */ 74793c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 74817ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 74993c2fb25SDavid Ahern if (rt0->fib6_src.plen) 75093c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 75117ecf590SWei Wang #endif 75217ecf590SWei Wang if (fn->fn_bit != key_plen) 753421842edSDavid Ahern return net->ipv6.fib6_null_entry; 75417ecf590SWei Wang 75593c2fb25SDavid Ahern match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict, 756afc154e9SHannes Frederic Sowa &do_rr); 757f11e6659SDavid S. Miller 758afc154e9SHannes Frederic Sowa if (do_rr) { 7598fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 760f11e6659SDavid S. Miller 761554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 76293c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 7638d1040e8SWei Wang next = leaf; 764f11e6659SDavid S. Miller 76566f5d6ceSWei Wang if (next != rt0) { 76693c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 76766f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 76893c2fb25SDavid Ahern if (next->fib6_node) 76966f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 77093c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 77166f5d6ceSWei Wang } 772554cfb7eSYOSHIFUJI Hideaki } 773554cfb7eSYOSHIFUJI Hideaki 774421842edSDavid Ahern return match ? match : net->ipv6.fib6_null_entry; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7778d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt) 7788b9df265SMartin KaFai Lau { 779bdf00467SDavid Ahern return (rt->fib6_flags & RTF_NONEXTHOP) || rt->fib6_nh.fib_nh_gw_family; 7808b9df265SMartin KaFai Lau } 7818b9df265SMartin KaFai Lau 78270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 784b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78570ceb4f5SYOSHIFUJI Hideaki { 786c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78770ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 78870ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 78970ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7904bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 7918d1c802bSDavid Ahern struct fib6_info *rt; 79270ceb4f5SYOSHIFUJI Hideaki 79370ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 79470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79570ceb4f5SYOSHIFUJI Hideaki } 79670ceb4f5SYOSHIFUJI Hideaki 79770ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 79870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 79970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 80470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80570ceb4f5SYOSHIFUJI Hideaki } 80670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 80870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80970ceb4f5SYOSHIFUJI Hideaki } 81070ceb4f5SYOSHIFUJI Hideaki } 81170ceb4f5SYOSHIFUJI Hideaki 81270ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81370ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8143933fc95SJens Rosenboom return -EINVAL; 81570ceb4f5SYOSHIFUJI Hideaki 8164bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81770ceb4f5SYOSHIFUJI Hideaki 81870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 81970ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82070ceb4f5SYOSHIFUJI Hideaki else { 82170ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82270ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82370ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 82470ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82570ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82670ceb4f5SYOSHIFUJI Hideaki } 82770ceb4f5SYOSHIFUJI Hideaki 828f104a567SDuan Jiong if (rinfo->prefix_len == 0) 829afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 830f104a567SDuan Jiong else 831f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 832830218c1SDavid Ahern gwaddr, dev); 83370ceb4f5SYOSHIFUJI Hideaki 83470ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 835afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 83670ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83770ceb4f5SYOSHIFUJI Hideaki } 83870ceb4f5SYOSHIFUJI Hideaki 83970ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 840830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 841830218c1SDavid Ahern dev, pref); 84270ceb4f5SYOSHIFUJI Hideaki else if (rt) 84393c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 84493c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84570ceb4f5SYOSHIFUJI Hideaki 84670ceb4f5SYOSHIFUJI Hideaki if (rt) { 8471716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 84814895687SDavid Ahern fib6_clean_expires(rt); 8491716a961SGao feng else 85014895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 8511716a961SGao feng 85293531c67SDavid Ahern fib6_info_release(rt); 85370ceb4f5SYOSHIFUJI Hideaki } 85470ceb4f5SYOSHIFUJI Hideaki return 0; 85570ceb4f5SYOSHIFUJI Hideaki } 85670ceb4f5SYOSHIFUJI Hideaki #endif 85770ceb4f5SYOSHIFUJI Hideaki 858ae90d867SDavid Ahern /* 859ae90d867SDavid Ahern * Misc support functions 860ae90d867SDavid Ahern */ 861ae90d867SDavid Ahern 862ae90d867SDavid Ahern /* called with rcu_lock held */ 8638d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt) 864ae90d867SDavid Ahern { 865ad1601aeSDavid Ahern struct net_device *dev = rt->fib6_nh.fib_nh_dev; 866ae90d867SDavid Ahern 86793c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 868ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 869ae90d867SDavid Ahern * device if it is a master device, the master device if 870ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 871ae90d867SDavid Ahern */ 872ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 87393c2fb25SDavid Ahern !rt6_need_strict(&rt->fib6_dst.addr)) 874ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 875ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 876ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 877ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 878ae90d867SDavid Ahern * case we want dev returned to be dev 879ae90d867SDavid Ahern */ 880ae90d867SDavid Ahern } 881ae90d867SDavid Ahern 882ae90d867SDavid Ahern return dev; 883ae90d867SDavid Ahern } 884ae90d867SDavid Ahern 8856edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 8866edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 8876edb3c96SDavid Ahern [RTN_UNICAST] = 0, 8886edb3c96SDavid Ahern [RTN_LOCAL] = 0, 8896edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 8906edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 8916edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 8926edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 8936edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 8946edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 8956edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 8966edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 8976edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 8986edb3c96SDavid Ahern }; 8996edb3c96SDavid Ahern 9006edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 9016edb3c96SDavid Ahern { 9026edb3c96SDavid Ahern return fib6_prop[fib6_type]; 9036edb3c96SDavid Ahern } 9046edb3c96SDavid Ahern 9058d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 9063b6761d1SDavid Ahern { 9073b6761d1SDavid Ahern unsigned short flags = 0; 9083b6761d1SDavid Ahern 9093b6761d1SDavid Ahern if (rt->dst_nocount) 9103b6761d1SDavid Ahern flags |= DST_NOCOUNT; 9113b6761d1SDavid Ahern if (rt->dst_nopolicy) 9123b6761d1SDavid Ahern flags |= DST_NOPOLICY; 9133b6761d1SDavid Ahern if (rt->dst_host) 9143b6761d1SDavid Ahern flags |= DST_HOST; 9153b6761d1SDavid Ahern 9163b6761d1SDavid Ahern return flags; 9173b6761d1SDavid Ahern } 9183b6761d1SDavid Ahern 9198d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort) 9206edb3c96SDavid Ahern { 9216edb3c96SDavid Ahern rt->dst.error = ip6_rt_type_to_error(ort->fib6_type); 9226edb3c96SDavid Ahern 9236edb3c96SDavid Ahern switch (ort->fib6_type) { 9246edb3c96SDavid Ahern case RTN_BLACKHOLE: 9256edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 9266edb3c96SDavid Ahern rt->dst.input = dst_discard; 9276edb3c96SDavid Ahern break; 9286edb3c96SDavid Ahern case RTN_PROHIBIT: 9296edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 9306edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 9316edb3c96SDavid Ahern break; 9326edb3c96SDavid Ahern case RTN_THROW: 9336edb3c96SDavid Ahern case RTN_UNREACHABLE: 9346edb3c96SDavid Ahern default: 9356edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 9366edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 9376edb3c96SDavid Ahern break; 9386edb3c96SDavid Ahern } 9396edb3c96SDavid Ahern } 9406edb3c96SDavid Ahern 9418d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort) 9426edb3c96SDavid Ahern { 94393c2fb25SDavid Ahern if (ort->fib6_flags & RTF_REJECT) { 9446edb3c96SDavid Ahern ip6_rt_init_dst_reject(rt, ort); 9456edb3c96SDavid Ahern return; 9466edb3c96SDavid Ahern } 9476edb3c96SDavid Ahern 9486edb3c96SDavid Ahern rt->dst.error = 0; 9496edb3c96SDavid Ahern rt->dst.output = ip6_output; 9506edb3c96SDavid Ahern 951d23c4b63SHangbin Liu if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) { 9526edb3c96SDavid Ahern rt->dst.input = ip6_input; 95393c2fb25SDavid Ahern } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 9546edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 9556edb3c96SDavid Ahern } else { 9566edb3c96SDavid Ahern rt->dst.input = ip6_forward; 9576edb3c96SDavid Ahern } 9586edb3c96SDavid Ahern 959ad1601aeSDavid Ahern if (ort->fib6_nh.fib_nh_lws) { 960ad1601aeSDavid Ahern rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.fib_nh_lws); 9616edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 9626edb3c96SDavid Ahern } 9636edb3c96SDavid Ahern 9646edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 9656edb3c96SDavid Ahern } 9666edb3c96SDavid Ahern 967e873e4b9SWei Wang /* Caller must already hold reference to @from */ 9688d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 969ae90d867SDavid Ahern { 970ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 971a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 972e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 973ae90d867SDavid Ahern } 974ae90d867SDavid Ahern 975e873e4b9SWei Wang /* Caller must already hold reference to @ort */ 9768d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort) 977ae90d867SDavid Ahern { 978dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ort); 979dcd1f572SDavid Ahern 9806edb3c96SDavid Ahern ip6_rt_init_dst(rt, ort); 9816edb3c96SDavid Ahern 98293c2fb25SDavid Ahern rt->rt6i_dst = ort->fib6_dst; 983dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 98493c2fb25SDavid Ahern rt->rt6i_flags = ort->fib6_flags; 985bdf00467SDavid Ahern if (ort->fib6_nh.fib_nh_gw_family) { 986ad1601aeSDavid Ahern rt->rt6i_gateway = ort->fib6_nh.fib_nh_gw6; 9872b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 9882b2450caSDavid Ahern } 989ae90d867SDavid Ahern rt6_set_from(rt, ort); 990ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 99193c2fb25SDavid Ahern rt->rt6i_src = ort->fib6_src; 992ae90d867SDavid Ahern #endif 993ae90d867SDavid Ahern } 994ae90d867SDavid Ahern 995a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 996a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 997a3c00e46SMartin KaFai Lau { 99866f5d6ceSWei Wang struct fib6_node *pn, *sn; 999a3c00e46SMartin KaFai Lau while (1) { 1000a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1001a3c00e46SMartin KaFai Lau return NULL; 100266f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 100366f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 100466f5d6ceSWei Wang if (sn && sn != fn) 10056454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1006a3c00e46SMartin KaFai Lau else 1007a3c00e46SMartin KaFai Lau fn = pn; 1008a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1009a3c00e46SMartin KaFai Lau return fn; 1010a3c00e46SMartin KaFai Lau } 1011a3c00e46SMartin KaFai Lau } 1012c71099acSThomas Graf 101310585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1014d3843fe5SWei Wang { 1015d3843fe5SWei Wang struct rt6_info *rt = *prt; 1016d3843fe5SWei Wang 1017d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1018d3843fe5SWei Wang return true; 101910585b43SDavid Ahern if (net) { 1020d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1021d3843fe5SWei Wang dst_hold(&rt->dst); 1022d3843fe5SWei Wang } else { 1023d3843fe5SWei Wang rt = NULL; 1024d3843fe5SWei Wang } 1025d3843fe5SWei Wang *prt = rt; 1026d3843fe5SWei Wang return false; 1027d3843fe5SWei Wang } 1028d3843fe5SWei Wang 1029dec9b0e2SDavid Ahern /* called with rcu_lock held */ 10308d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt) 1031dec9b0e2SDavid Ahern { 10323b6761d1SDavid Ahern unsigned short flags = fib6_info_dst_flags(rt); 1033ad1601aeSDavid Ahern struct net_device *dev = rt->fib6_nh.fib_nh_dev; 1034dec9b0e2SDavid Ahern struct rt6_info *nrt; 1035dec9b0e2SDavid Ahern 1036e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 10371c87e79aSXin Long goto fallback; 1038e873e4b9SWei Wang 103993531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 10401c87e79aSXin Long if (!nrt) { 1041e873e4b9SWei Wang fib6_info_release(rt); 10421c87e79aSXin Long goto fallback; 10431c87e79aSXin Long } 1044dec9b0e2SDavid Ahern 10451c87e79aSXin Long ip6_rt_copy_init(nrt, rt); 10461c87e79aSXin Long return nrt; 10471c87e79aSXin Long 10481c87e79aSXin Long fallback: 10491c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 10501c87e79aSXin Long dst_hold(&nrt->dst); 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) { 109210585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 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); 1099dec9b0e2SDavid Ahern } 1100d3843fe5SWei Wang 110166f5d6ceSWei Wang rcu_read_unlock(); 1102b811580dSDavid Ahern 11031da177e4SLinus Torvalds return rt; 1104c71099acSThomas Graf } 1105c71099acSThomas Graf 1106ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1107b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1108ea6e574eSFlorian Westphal { 1109b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1110ea6e574eSFlorian Westphal } 1111ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1112ea6e574eSFlorian Westphal 11139acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1114b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1115b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1116c71099acSThomas Graf { 11174c9483b2SDavid S. Miller struct flowi6 fl6 = { 11184c9483b2SDavid S. Miller .flowi6_oif = oif, 11194c9483b2SDavid S. Miller .daddr = *daddr, 1120c71099acSThomas Graf }; 1121c71099acSThomas Graf struct dst_entry *dst; 112277d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1123c71099acSThomas Graf 1124adaa70bbSThomas Graf if (saddr) { 11254c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1126adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1127adaa70bbSThomas Graf } 1128adaa70bbSThomas Graf 1129b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1130c71099acSThomas Graf if (dst->error == 0) 1131c71099acSThomas Graf return (struct rt6_info *) dst; 1132c71099acSThomas Graf 1133c71099acSThomas Graf dst_release(dst); 1134c71099acSThomas Graf 11351da177e4SLinus Torvalds return NULL; 11361da177e4SLinus Torvalds } 11377159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 11387159039aSYOSHIFUJI Hideaki 1139c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 11401cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 11411cfb71eeSWei Wang * route is released. 11421cfb71eeSWei Wang * Caller must hold dst before calling it. 11431da177e4SLinus Torvalds */ 11441da177e4SLinus Torvalds 11458d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1146333c4301SDavid Ahern struct netlink_ext_ack *extack) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds int err; 1149c71099acSThomas Graf struct fib6_table *table; 11501da177e4SLinus Torvalds 115193c2fb25SDavid Ahern table = rt->fib6_table; 115266f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1153d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 115466f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds return err; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 11598d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 116040e22e8fSThomas Graf { 1161afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1162e715b6d3SFlorian Westphal 1163d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 116440e22e8fSThomas Graf } 116540e22e8fSThomas Graf 11668d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort, 116721efcfa0SEric Dumazet const struct in6_addr *daddr, 1168b71d1d42SEric Dumazet const struct in6_addr *saddr) 11691da177e4SLinus Torvalds { 11704832c30dSDavid Ahern struct net_device *dev; 11711da177e4SLinus Torvalds struct rt6_info *rt; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* 11741da177e4SLinus Torvalds * Clone the route. 11751da177e4SLinus Torvalds */ 11761da177e4SLinus Torvalds 1177e873e4b9SWei Wang if (!fib6_info_hold_safe(ort)) 1178e873e4b9SWei Wang return NULL; 1179e873e4b9SWei Wang 11804832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 118193531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1182e873e4b9SWei Wang if (!rt) { 1183e873e4b9SWei Wang fib6_info_release(ort); 118483a09abdSMartin KaFai Lau return NULL; 1185e873e4b9SWei Wang } 118683a09abdSMartin KaFai Lau 118783a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 11888b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 118983a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 119083a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 119183a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 11928b9df265SMartin KaFai Lau 11938b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 119493c2fb25SDavid Ahern if (ort->fib6_dst.plen != 128 && 119593c2fb25SDavid Ahern ipv6_addr_equal(&ort->fib6_dst.addr, daddr)) 119658c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 11971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 11981da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 11994e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 12001da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds #endif 120395a9a5baSYOSHIFUJI Hideaki } 120495a9a5baSYOSHIFUJI Hideaki 1205299d9939SYOSHIFUJI Hideaki return rt; 1206299d9939SYOSHIFUJI Hideaki } 1207299d9939SYOSHIFUJI Hideaki 12088d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt) 1209d52d3997SMartin KaFai Lau { 12103b6761d1SDavid Ahern unsigned short flags = fib6_info_dst_flags(rt); 12114832c30dSDavid Ahern struct net_device *dev; 1212d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1213d52d3997SMartin KaFai Lau 1214e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 1215e873e4b9SWei Wang return NULL; 1216e873e4b9SWei Wang 12174832c30dSDavid Ahern rcu_read_lock(); 12184832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 121993531c67SDavid Ahern pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); 12204832c30dSDavid Ahern rcu_read_unlock(); 1221e873e4b9SWei Wang if (!pcpu_rt) { 1222e873e4b9SWei Wang fib6_info_release(rt); 1223d52d3997SMartin KaFai Lau return NULL; 1224e873e4b9SWei Wang } 1225d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1226d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1227d52d3997SMartin KaFai Lau return pcpu_rt; 1228d52d3997SMartin KaFai Lau } 1229d52d3997SMartin KaFai Lau 123066f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 12318d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt) 1232d52d3997SMartin KaFai Lau { 1233a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1234d52d3997SMartin KaFai Lau 1235d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1236d52d3997SMartin KaFai Lau pcpu_rt = *p; 1237d52d3997SMartin KaFai Lau 1238d4ead6b3SDavid Ahern if (pcpu_rt) 123910585b43SDavid Ahern ip6_hold_safe(NULL, &pcpu_rt); 1240d3843fe5SWei Wang 1241a73e4195SMartin KaFai Lau return pcpu_rt; 1242a73e4195SMartin KaFai Lau } 1243a73e4195SMartin KaFai Lau 1244afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 12458d1c802bSDavid Ahern struct fib6_info *rt) 1246a73e4195SMartin KaFai Lau { 1247a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1248d52d3997SMartin KaFai Lau 1249d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1250d52d3997SMartin KaFai Lau if (!pcpu_rt) { 12519c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 12529c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1253d52d3997SMartin KaFai Lau } 1254d52d3997SMartin KaFai Lau 1255a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1256a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1257d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1258951f788aSEric Dumazet BUG_ON(prev); 1259a94b9367SWei Wang 1260d52d3997SMartin KaFai Lau return pcpu_rt; 1261d52d3997SMartin KaFai Lau } 1262d52d3997SMartin KaFai Lau 126335732d01SWei Wang /* exception hash table implementation 126435732d01SWei Wang */ 126535732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 126635732d01SWei Wang 126735732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 126835732d01SWei Wang * Caller must hold rt6_exception_lock 126935732d01SWei Wang */ 127035732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 127135732d01SWei Wang struct rt6_exception *rt6_ex) 127235732d01SWei Wang { 1273f5b51fe8SPaolo Abeni struct fib6_info *from; 1274b2427e67SColin Ian King struct net *net; 127581eb8447SWei Wang 127635732d01SWei Wang if (!bucket || !rt6_ex) 127735732d01SWei Wang return; 1278b2427e67SColin Ian King 1279b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1280f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1281f5b51fe8SPaolo Abeni 1282f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1283f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1284f5b51fe8SPaolo Abeni */ 1285f5b51fe8SPaolo Abeni from = rcu_dereference_protected(rt6_ex->rt6i->from, 1286f5b51fe8SPaolo Abeni lockdep_is_held(&rt6_exception_lock)); 1287f5b51fe8SPaolo Abeni rcu_assign_pointer(rt6_ex->rt6i->from, NULL); 1288f5b51fe8SPaolo Abeni fib6_info_release(from); 1289f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1290f5b51fe8SPaolo Abeni 129135732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 129277634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 129335732d01SWei Wang kfree_rcu(rt6_ex, rcu); 129435732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 129535732d01SWei Wang bucket->depth--; 129635732d01SWei Wang } 129735732d01SWei Wang 129835732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 129935732d01SWei Wang * Caller must hold rt6_exception_lock 130035732d01SWei Wang */ 130135732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 130235732d01SWei Wang { 130335732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 130435732d01SWei Wang 130535732d01SWei Wang if (!bucket) 130635732d01SWei Wang return; 130735732d01SWei Wang 130835732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 130935732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 131035732d01SWei Wang oldest = rt6_ex; 131135732d01SWei Wang } 131235732d01SWei Wang rt6_remove_exception(bucket, oldest); 131335732d01SWei Wang } 131435732d01SWei Wang 131535732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 131635732d01SWei Wang const struct in6_addr *src) 131735732d01SWei Wang { 131835732d01SWei Wang static u32 seed __read_mostly; 131935732d01SWei Wang u32 val; 132035732d01SWei Wang 132135732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 132235732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 132335732d01SWei Wang 132435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 132535732d01SWei Wang if (src) 132635732d01SWei Wang val = jhash(src, sizeof(*src), val); 132735732d01SWei Wang #endif 132835732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 132935732d01SWei Wang } 133035732d01SWei Wang 133135732d01SWei Wang /* Helper function to find the cached rt in the hash table 133235732d01SWei Wang * and update bucket pointer to point to the bucket for this 133335732d01SWei Wang * (daddr, saddr) pair 133435732d01SWei Wang * Caller must hold rt6_exception_lock 133535732d01SWei Wang */ 133635732d01SWei Wang static struct rt6_exception * 133735732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 133835732d01SWei Wang const struct in6_addr *daddr, 133935732d01SWei Wang const struct in6_addr *saddr) 134035732d01SWei Wang { 134135732d01SWei Wang struct rt6_exception *rt6_ex; 134235732d01SWei Wang u32 hval; 134335732d01SWei Wang 134435732d01SWei Wang if (!(*bucket) || !daddr) 134535732d01SWei Wang return NULL; 134635732d01SWei Wang 134735732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 134835732d01SWei Wang *bucket += hval; 134935732d01SWei Wang 135035732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 135135732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 135235732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 135335732d01SWei Wang 135435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 135535732d01SWei Wang if (matched && saddr) 135635732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 135735732d01SWei Wang #endif 135835732d01SWei Wang if (matched) 135935732d01SWei Wang return rt6_ex; 136035732d01SWei Wang } 136135732d01SWei Wang return NULL; 136235732d01SWei Wang } 136335732d01SWei Wang 136435732d01SWei Wang /* Helper function to find the cached rt in the hash table 136535732d01SWei Wang * and update bucket pointer to point to the bucket for this 136635732d01SWei Wang * (daddr, saddr) pair 136735732d01SWei Wang * Caller must hold rcu_read_lock() 136835732d01SWei Wang */ 136935732d01SWei Wang static struct rt6_exception * 137035732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 137135732d01SWei Wang const struct in6_addr *daddr, 137235732d01SWei Wang const struct in6_addr *saddr) 137335732d01SWei Wang { 137435732d01SWei Wang struct rt6_exception *rt6_ex; 137535732d01SWei Wang u32 hval; 137635732d01SWei Wang 137735732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 137835732d01SWei Wang 137935732d01SWei Wang if (!(*bucket) || !daddr) 138035732d01SWei Wang return NULL; 138135732d01SWei Wang 138235732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 138335732d01SWei Wang *bucket += hval; 138435732d01SWei Wang 138535732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 138635732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 138735732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 138835732d01SWei Wang 138935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139035732d01SWei Wang if (matched && saddr) 139135732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 139235732d01SWei Wang #endif 139335732d01SWei Wang if (matched) 139435732d01SWei Wang return rt6_ex; 139535732d01SWei Wang } 139635732d01SWei Wang return NULL; 139735732d01SWei Wang } 139835732d01SWei Wang 13998d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt) 140035732d01SWei Wang { 1401d4ead6b3SDavid Ahern unsigned int mtu; 1402d4ead6b3SDavid Ahern 1403dcd1f572SDavid Ahern if (rt->fib6_pmtu) { 1404dcd1f572SDavid Ahern mtu = rt->fib6_pmtu; 1405dcd1f572SDavid Ahern } else { 1406dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 1407dcd1f572SDavid Ahern struct inet6_dev *idev; 1408dcd1f572SDavid Ahern 1409dcd1f572SDavid Ahern rcu_read_lock(); 1410dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1411dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1412dcd1f572SDavid Ahern rcu_read_unlock(); 1413dcd1f572SDavid Ahern } 1414dcd1f572SDavid Ahern 1415d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1416d4ead6b3SDavid Ahern 1417ad1601aeSDavid Ahern return mtu - lwtunnel_headroom(rt->fib6_nh.fib_nh_lws, mtu); 1418d4ead6b3SDavid Ahern } 1419d4ead6b3SDavid Ahern 142035732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 14218d1c802bSDavid Ahern struct fib6_info *ort) 142235732d01SWei Wang { 14235e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 142435732d01SWei Wang struct rt6_exception_bucket *bucket; 142535732d01SWei Wang struct in6_addr *src_key = NULL; 142635732d01SWei Wang struct rt6_exception *rt6_ex; 142735732d01SWei Wang int err = 0; 142835732d01SWei Wang 142935732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 143035732d01SWei Wang 143135732d01SWei Wang if (ort->exception_bucket_flushed) { 143235732d01SWei Wang err = -EINVAL; 143335732d01SWei Wang goto out; 143435732d01SWei Wang } 143535732d01SWei Wang 143635732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 143735732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 143835732d01SWei Wang if (!bucket) { 143935732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 144035732d01SWei Wang GFP_ATOMIC); 144135732d01SWei Wang if (!bucket) { 144235732d01SWei Wang err = -ENOMEM; 144335732d01SWei Wang goto out; 144435732d01SWei Wang } 144535732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 144635732d01SWei Wang } 144735732d01SWei Wang 144835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 144935732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 145035732d01SWei Wang * and exception table is indexed by a hash of 145135732d01SWei Wang * both rt6i_dst and rt6i_src. 145235732d01SWei Wang * Otherwise, the exception table is indexed by 145335732d01SWei Wang * a hash of only rt6i_dst. 145435732d01SWei Wang */ 145593c2fb25SDavid Ahern if (ort->fib6_src.plen) 145635732d01SWei Wang src_key = &nrt->rt6i_src.addr; 145735732d01SWei Wang #endif 1458f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1459f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1460f5bbe7eeSWei Wang * is less than ort's mtu value. 1461f5bbe7eeSWei Wang */ 1462d4ead6b3SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) { 1463f5bbe7eeSWei Wang err = -EINVAL; 1464f5bbe7eeSWei Wang goto out; 1465f5bbe7eeSWei Wang } 146660006a48SWei Wang 146735732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 146835732d01SWei Wang src_key); 146935732d01SWei Wang if (rt6_ex) 147035732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 147135732d01SWei Wang 147235732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 147335732d01SWei Wang if (!rt6_ex) { 147435732d01SWei Wang err = -ENOMEM; 147535732d01SWei Wang goto out; 147635732d01SWei Wang } 147735732d01SWei Wang rt6_ex->rt6i = nrt; 147835732d01SWei Wang rt6_ex->stamp = jiffies; 147935732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 148035732d01SWei Wang bucket->depth++; 148181eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 148235732d01SWei Wang 148335732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 148435732d01SWei Wang rt6_exception_remove_oldest(bucket); 148535732d01SWei Wang 148635732d01SWei Wang out: 148735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 148835732d01SWei Wang 148935732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1490b886d5f2SPaolo Abeni if (!err) { 149193c2fb25SDavid Ahern spin_lock_bh(&ort->fib6_table->tb6_lock); 14927aef6859SDavid Ahern fib6_update_sernum(net, ort); 149393c2fb25SDavid Ahern spin_unlock_bh(&ort->fib6_table->tb6_lock); 1494b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1495b886d5f2SPaolo Abeni } 149635732d01SWei Wang 149735732d01SWei Wang return err; 149835732d01SWei Wang } 149935732d01SWei Wang 15008d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt) 150135732d01SWei Wang { 150235732d01SWei Wang struct rt6_exception_bucket *bucket; 150335732d01SWei Wang struct rt6_exception *rt6_ex; 150435732d01SWei Wang struct hlist_node *tmp; 150535732d01SWei Wang int i; 150635732d01SWei Wang 150735732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 150835732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 150935732d01SWei Wang rt->exception_bucket_flushed = 1; 151035732d01SWei Wang 151135732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 151235732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 151335732d01SWei Wang if (!bucket) 151435732d01SWei Wang goto out; 151535732d01SWei Wang 151635732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 151735732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 151835732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 151935732d01SWei Wang WARN_ON_ONCE(bucket->depth); 152035732d01SWei Wang bucket++; 152135732d01SWei Wang } 152235732d01SWei Wang 152335732d01SWei Wang out: 152435732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 152535732d01SWei Wang } 152635732d01SWei Wang 152735732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 152835732d01SWei Wang * Caller has to hold rcu_read_lock() 152935732d01SWei Wang */ 15308d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, 153135732d01SWei Wang struct in6_addr *daddr, 153235732d01SWei Wang struct in6_addr *saddr) 153335732d01SWei Wang { 153435732d01SWei Wang struct rt6_exception_bucket *bucket; 153535732d01SWei Wang struct in6_addr *src_key = NULL; 153635732d01SWei Wang struct rt6_exception *rt6_ex; 153735732d01SWei Wang struct rt6_info *res = NULL; 153835732d01SWei Wang 153935732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 154035732d01SWei Wang 154135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 154235732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 154335732d01SWei Wang * and exception table is indexed by a hash of 154435732d01SWei Wang * both rt6i_dst and rt6i_src. 154535732d01SWei Wang * Otherwise, the exception table is indexed by 154635732d01SWei Wang * a hash of only rt6i_dst. 154735732d01SWei Wang */ 154893c2fb25SDavid Ahern if (rt->fib6_src.plen) 154935732d01SWei Wang src_key = saddr; 155035732d01SWei Wang #endif 155135732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 155235732d01SWei Wang 155335732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 155435732d01SWei Wang res = rt6_ex->rt6i; 155535732d01SWei Wang 155635732d01SWei Wang return res; 155735732d01SWei Wang } 155835732d01SWei Wang 155935732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 156023fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 156135732d01SWei Wang { 156235732d01SWei Wang struct rt6_exception_bucket *bucket; 156335732d01SWei Wang struct in6_addr *src_key = NULL; 156435732d01SWei Wang struct rt6_exception *rt6_ex; 15658a14e46fSDavid Ahern struct fib6_info *from; 156635732d01SWei Wang int err; 156735732d01SWei Wang 1568091311deSEric Dumazet from = rcu_dereference(rt->from); 156935732d01SWei Wang if (!from || 1570442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 157135732d01SWei Wang return -EINVAL; 157235732d01SWei Wang 157335732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 157435732d01SWei Wang return -ENOENT; 157535732d01SWei Wang 157635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 157735732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 157835732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 157935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 158035732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 158135732d01SWei Wang * and exception table is indexed by a hash of 158235732d01SWei Wang * both rt6i_dst and rt6i_src. 158335732d01SWei Wang * Otherwise, the exception table is indexed by 158435732d01SWei Wang * a hash of only rt6i_dst. 158535732d01SWei Wang */ 158693c2fb25SDavid Ahern if (from->fib6_src.plen) 158735732d01SWei Wang src_key = &rt->rt6i_src.addr; 158835732d01SWei Wang #endif 158935732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 159035732d01SWei Wang &rt->rt6i_dst.addr, 159135732d01SWei Wang src_key); 159235732d01SWei Wang if (rt6_ex) { 159335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 159435732d01SWei Wang err = 0; 159535732d01SWei Wang } else { 159635732d01SWei Wang err = -ENOENT; 159735732d01SWei Wang } 159835732d01SWei Wang 159935732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 160035732d01SWei Wang return err; 160135732d01SWei Wang } 160235732d01SWei Wang 160335732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 160435732d01SWei Wang * refresh its stamp 160535732d01SWei Wang */ 160635732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 160735732d01SWei Wang { 160835732d01SWei Wang struct rt6_exception_bucket *bucket; 160935732d01SWei Wang struct in6_addr *src_key = NULL; 161035732d01SWei Wang struct rt6_exception *rt6_ex; 1611193f3685SPaolo Abeni struct fib6_info *from; 161235732d01SWei Wang 161335732d01SWei Wang rcu_read_lock(); 1614193f3685SPaolo Abeni from = rcu_dereference(rt->from); 1615193f3685SPaolo Abeni if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1616193f3685SPaolo Abeni goto unlock; 1617193f3685SPaolo Abeni 161835732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 161935732d01SWei Wang 162035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 162135732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 162235732d01SWei Wang * and exception table is indexed by a hash of 162335732d01SWei Wang * both rt6i_dst and rt6i_src. 162435732d01SWei Wang * Otherwise, the exception table is indexed by 162535732d01SWei Wang * a hash of only rt6i_dst. 162635732d01SWei Wang */ 162793c2fb25SDavid Ahern if (from->fib6_src.plen) 162835732d01SWei Wang src_key = &rt->rt6i_src.addr; 162935732d01SWei Wang #endif 163035732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 163135732d01SWei Wang &rt->rt6i_dst.addr, 163235732d01SWei Wang src_key); 163335732d01SWei Wang if (rt6_ex) 163435732d01SWei Wang rt6_ex->stamp = jiffies; 163535732d01SWei Wang 1636193f3685SPaolo Abeni unlock: 163735732d01SWei Wang rcu_read_unlock(); 163835732d01SWei Wang } 163935732d01SWei Wang 1640e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1641e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1642e9fa1495SStefano Brivio { 1643e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1644e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1645e9fa1495SStefano Brivio * reflect PMTU decreases. 1646e9fa1495SStefano Brivio * 1647e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1648e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1649e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1650e9fa1495SStefano Brivio * handle this. 1651e9fa1495SStefano Brivio */ 1652e9fa1495SStefano Brivio 1653e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1654e9fa1495SStefano Brivio return true; 1655e9fa1495SStefano Brivio 1656e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1657e9fa1495SStefano Brivio return true; 1658e9fa1495SStefano Brivio 1659e9fa1495SStefano Brivio return false; 1660e9fa1495SStefano Brivio } 1661e9fa1495SStefano Brivio 1662e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 16638d1c802bSDavid Ahern struct fib6_info *rt, int mtu) 1664f5bbe7eeSWei Wang { 1665f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1666f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1667f5bbe7eeSWei Wang int i; 1668f5bbe7eeSWei Wang 1669f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1670f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1671f5bbe7eeSWei Wang 1672e9fa1495SStefano Brivio if (!bucket) 1673e9fa1495SStefano Brivio return; 1674e9fa1495SStefano Brivio 1675f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1676f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1677f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1678e9fa1495SStefano Brivio 1679e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 1680d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 1681f5bbe7eeSWei Wang * been updated. 1682f5bbe7eeSWei Wang */ 1683d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 1684e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 1685d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 1686f5bbe7eeSWei Wang } 1687f5bbe7eeSWei Wang bucket++; 1688f5bbe7eeSWei Wang } 1689f5bbe7eeSWei Wang } 1690f5bbe7eeSWei Wang 1691b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1692b16cb459SWei Wang 16938d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt, 1694b16cb459SWei Wang struct in6_addr *gateway) 1695b16cb459SWei Wang { 1696b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1697b16cb459SWei Wang struct rt6_exception *rt6_ex; 1698b16cb459SWei Wang struct hlist_node *tmp; 1699b16cb459SWei Wang int i; 1700b16cb459SWei Wang 1701b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1702b16cb459SWei Wang return; 1703b16cb459SWei Wang 1704b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1705b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1706b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1707b16cb459SWei Wang 1708b16cb459SWei Wang if (bucket) { 1709b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1710b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1711b16cb459SWei Wang &bucket->chain, hlist) { 1712b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1713b16cb459SWei Wang 1714b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1715b16cb459SWei Wang RTF_CACHE_GATEWAY && 1716b16cb459SWei Wang ipv6_addr_equal(gateway, 1717b16cb459SWei Wang &entry->rt6i_gateway)) { 1718b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1719b16cb459SWei Wang } 1720b16cb459SWei Wang } 1721b16cb459SWei Wang bucket++; 1722b16cb459SWei Wang } 1723b16cb459SWei Wang } 1724b16cb459SWei Wang 1725b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1726b16cb459SWei Wang } 1727b16cb459SWei Wang 1728c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1729c757faa8SWei Wang struct rt6_exception *rt6_ex, 1730c757faa8SWei Wang struct fib6_gc_args *gc_args, 1731c757faa8SWei Wang unsigned long now) 1732c757faa8SWei Wang { 1733c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1734c757faa8SWei Wang 17351859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 17361859bac0SPaolo Abeni * even if others have still references to them, so that on next 17371859bac0SPaolo Abeni * dst_check() such references can be dropped. 17381859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 17391859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 17401859bac0SPaolo Abeni */ 174131afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 174231afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1743c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1744c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1745c757faa8SWei Wang return; 174631afeb42SWei Wang } 174731afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 174831afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 174931afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 175031afeb42SWei Wang return; 175131afeb42SWei Wang } 175231afeb42SWei Wang 175331afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 1754c757faa8SWei Wang struct neighbour *neigh; 1755c757faa8SWei Wang __u8 neigh_flags = 0; 1756c757faa8SWei Wang 17571bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 17581bfa26ffSEric Dumazet if (neigh) 1759c757faa8SWei Wang neigh_flags = neigh->flags; 17601bfa26ffSEric Dumazet 1761c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1762c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1763c757faa8SWei Wang rt); 1764c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1765c757faa8SWei Wang return; 1766c757faa8SWei Wang } 1767c757faa8SWei Wang } 176831afeb42SWei Wang 1769c757faa8SWei Wang gc_args->more++; 1770c757faa8SWei Wang } 1771c757faa8SWei Wang 17728d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt, 1773c757faa8SWei Wang struct fib6_gc_args *gc_args, 1774c757faa8SWei Wang unsigned long now) 1775c757faa8SWei Wang { 1776c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1777c757faa8SWei Wang struct rt6_exception *rt6_ex; 1778c757faa8SWei Wang struct hlist_node *tmp; 1779c757faa8SWei Wang int i; 1780c757faa8SWei Wang 1781c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1782c757faa8SWei Wang return; 1783c757faa8SWei Wang 17841bfa26ffSEric Dumazet rcu_read_lock_bh(); 17851bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 1786c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1787c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1788c757faa8SWei Wang 1789c757faa8SWei Wang if (bucket) { 1790c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1791c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1792c757faa8SWei Wang &bucket->chain, hlist) { 1793c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1794c757faa8SWei Wang gc_args, now); 1795c757faa8SWei Wang } 1796c757faa8SWei Wang bucket++; 1797c757faa8SWei Wang } 1798c757faa8SWei Wang } 17991bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 18001bfa26ffSEric Dumazet rcu_read_unlock_bh(); 1801c757faa8SWei Wang } 1802c757faa8SWei Wang 18031d053da9SDavid Ahern /* must be called with rcu lock held */ 18041d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table, 18051d053da9SDavid Ahern int oif, struct flowi6 *fl6, int strict) 18061da177e4SLinus Torvalds { 1807367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 18088d1c802bSDavid Ahern struct fib6_info *f6i; 18091da177e4SLinus Torvalds 18106454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1811367efcb9SMartin KaFai Lau saved_fn = fn; 18121da177e4SLinus Torvalds 1813ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1814ca254490SDavid Ahern oif = 0; 1815ca254490SDavid Ahern 1816a3c00e46SMartin KaFai Lau redo_rt6_select: 181723fb93a4SDavid Ahern f6i = rt6_select(net, fn, oif, strict); 181823fb93a4SDavid Ahern if (f6i == net->ipv6.fib6_null_entry) { 1819a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1820a3c00e46SMartin KaFai Lau if (fn) 1821a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1822367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1823367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1824367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1825367efcb9SMartin KaFai Lau fn = saved_fn; 1826367efcb9SMartin KaFai Lau goto redo_rt6_select; 1827367efcb9SMartin KaFai Lau } 1828a3c00e46SMartin KaFai Lau } 1829a3c00e46SMartin KaFai Lau 1830d4bea421SDavid Ahern trace_fib6_table_lookup(net, f6i, table, fl6); 1831d52d3997SMartin KaFai Lau 18321d053da9SDavid Ahern return f6i; 18331d053da9SDavid Ahern } 18341d053da9SDavid Ahern 18351d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 18361d053da9SDavid Ahern int oif, struct flowi6 *fl6, 18371d053da9SDavid Ahern const struct sk_buff *skb, int flags) 18381d053da9SDavid Ahern { 18391d053da9SDavid Ahern struct fib6_info *f6i; 18401d053da9SDavid Ahern struct rt6_info *rt; 18411d053da9SDavid Ahern int strict = 0; 18421d053da9SDavid Ahern 18431d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 18441d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 18451d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 18461d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 18471d053da9SDavid Ahern 18481d053da9SDavid Ahern rcu_read_lock(); 18491d053da9SDavid Ahern 18501d053da9SDavid Ahern f6i = fib6_table_lookup(net, table, oif, fl6, strict); 18511d053da9SDavid Ahern if (f6i->fib6_nsiblings) 18521d053da9SDavid Ahern f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict); 18531d053da9SDavid Ahern 185423fb93a4SDavid Ahern if (f6i == net->ipv6.fib6_null_entry) { 1855421842edSDavid Ahern rt = net->ipv6.ip6_null_entry; 185666f5d6ceSWei Wang rcu_read_unlock(); 1857d3843fe5SWei Wang dst_hold(&rt->dst); 1858d3843fe5SWei Wang return rt; 1859d3843fe5SWei Wang } 186023fb93a4SDavid Ahern 186123fb93a4SDavid Ahern /*Search through exception table */ 186223fb93a4SDavid Ahern rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr); 186323fb93a4SDavid Ahern if (rt) { 186410585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 18651da177e4SLinus Torvalds dst_use_noref(&rt->dst, jiffies); 1866d4ead6b3SDavid Ahern 186766f5d6ceSWei Wang rcu_read_unlock(); 1868d52d3997SMartin KaFai Lau return rt; 18693da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 1870bdf00467SDavid Ahern !f6i->fib6_nh.fib_nh_gw_family)) { 18713da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 18723da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 18733da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 18743da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 18753da59bd9SMartin KaFai Lau */ 18763da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 18773da59bd9SMartin KaFai Lau 187823fb93a4SDavid Ahern uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL); 1879d52d3997SMartin KaFai Lau 18804d85cd0cSDavid Ahern rcu_read_unlock(); 18813da59bd9SMartin KaFai Lau 18821cfb71eeSWei Wang if (uncached_rt) { 18831cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 18841cfb71eeSWei Wang * No need for another dst_hold() 18851cfb71eeSWei Wang */ 18868d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 188781eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 18881cfb71eeSWei Wang } else { 18893da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 18903da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 18911cfb71eeSWei Wang } 1892b811580dSDavid Ahern 18933da59bd9SMartin KaFai Lau return uncached_rt; 1894d52d3997SMartin KaFai Lau } else { 1895d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1896d52d3997SMartin KaFai Lau 1897d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1898d52d3997SMartin KaFai Lau 1899951f788aSEric Dumazet local_bh_disable(); 190023fb93a4SDavid Ahern pcpu_rt = rt6_get_pcpu_route(f6i); 1901d52d3997SMartin KaFai Lau 190293531c67SDavid Ahern if (!pcpu_rt) 190323fb93a4SDavid Ahern pcpu_rt = rt6_make_pcpu_route(net, f6i); 190493531c67SDavid Ahern 1905951f788aSEric Dumazet local_bh_enable(); 1906951f788aSEric Dumazet rcu_read_unlock(); 1907d4bea421SDavid Ahern 1908d52d3997SMartin KaFai Lau return pcpu_rt; 1909d52d3997SMartin KaFai Lau } 1910c71099acSThomas Graf } 19119ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1912c71099acSThomas Graf 1913b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 1914b75cc8f9SDavid Ahern struct fib6_table *table, 1915b75cc8f9SDavid Ahern struct flowi6 *fl6, 1916b75cc8f9SDavid Ahern const struct sk_buff *skb, 1917b75cc8f9SDavid Ahern int flags) 19184acad72dSPavel Emelyanov { 1919b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 19204acad72dSPavel Emelyanov } 19214acad72dSPavel Emelyanov 1922d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 192372331bc0SShmulik Ladkani struct net_device *dev, 1924b75cc8f9SDavid Ahern struct flowi6 *fl6, 1925b75cc8f9SDavid Ahern const struct sk_buff *skb, 1926b75cc8f9SDavid Ahern int flags) 192772331bc0SShmulik Ladkani { 192872331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 192972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 193072331bc0SShmulik Ladkani 1931b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 193272331bc0SShmulik Ladkani } 1933d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 193472331bc0SShmulik Ladkani 193523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 19365e5d6fedSRoopa Prabhu struct flow_keys *keys, 19375e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 193823aebdacSJakub Sitnicki { 193923aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 194023aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 19415e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 194223aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 194323aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 194423aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 1945cea67a2dSEric Dumazet struct icmp6hdr _icmph; 194623aebdacSJakub Sitnicki 194723aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 194823aebdacSJakub Sitnicki goto out; 194923aebdacSJakub Sitnicki 1950cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 1951cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 1952cea67a2dSEric Dumazet if (!icmph) 1953cea67a2dSEric Dumazet goto out; 1954cea67a2dSEric Dumazet 195523aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 195623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 195723aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 195823aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 195923aebdacSJakub Sitnicki goto out; 196023aebdacSJakub Sitnicki 196123aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 196223aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 196323aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 196423aebdacSJakub Sitnicki if (!inner_iph) 196523aebdacSJakub Sitnicki goto out; 196623aebdacSJakub Sitnicki 196723aebdacSJakub Sitnicki key_iph = inner_iph; 19685e5d6fedSRoopa Prabhu _flkeys = NULL; 196923aebdacSJakub Sitnicki out: 19705e5d6fedSRoopa Prabhu if (_flkeys) { 19715e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 19725e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 19735e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 19745e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 19755e5d6fedSRoopa Prabhu } else { 197623aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 197723aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 1978fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 197923aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 198023aebdacSJakub Sitnicki } 19815e5d6fedSRoopa Prabhu } 198223aebdacSJakub Sitnicki 198323aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 1984b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 1985b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 198623aebdacSJakub Sitnicki { 198723aebdacSJakub Sitnicki struct flow_keys hash_keys; 19889a2a537aSDavid Ahern u32 mhash; 198923aebdacSJakub Sitnicki 1990bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 1991b4bac172SDavid Ahern case 0: 19926f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 19936f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 19949a2a537aSDavid Ahern if (skb) { 19955e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 19969a2a537aSDavid Ahern } else { 19979a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 19989a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 1999fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 20009a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 200123aebdacSJakub Sitnicki } 2002b4bac172SDavid Ahern break; 2003b4bac172SDavid Ahern case 1: 2004b4bac172SDavid Ahern if (skb) { 2005b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2006b4bac172SDavid Ahern struct flow_keys keys; 2007b4bac172SDavid Ahern 2008b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2009b4bac172SDavid Ahern if (skb->l4_hash) 2010b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2011b4bac172SDavid Ahern 2012b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2013b4bac172SDavid Ahern 2014b4bac172SDavid Ahern if (!flkeys) { 2015b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2016b4bac172SDavid Ahern flkeys = &keys; 2017b4bac172SDavid Ahern } 2018b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2019b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2020b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2021b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2022b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2023b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2024b4bac172SDavid Ahern } else { 2025b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2026b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2027b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2028b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2029b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2030b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2031b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2032b4bac172SDavid Ahern } 2033b4bac172SDavid Ahern break; 2034b4bac172SDavid Ahern } 20359a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 203623aebdacSJakub Sitnicki 20379a2a537aSDavid Ahern return mhash >> 1; 203823aebdacSJakub Sitnicki } 203923aebdacSJakub Sitnicki 2040c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2041c71099acSThomas Graf { 2042b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2043c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 2044adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 2045904af04dSJiri Benc struct ip_tunnel_info *tun_info; 20464c9483b2SDavid S. Miller struct flowi6 fl6 = { 2047e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 20484c9483b2SDavid S. Miller .daddr = iph->daddr, 20494c9483b2SDavid S. Miller .saddr = iph->saddr, 20506502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 20514c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 20524c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2053c71099acSThomas Graf }; 20545e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2055adaa70bbSThomas Graf 2056904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 205746fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2058904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 20595e5d6fedSRoopa Prabhu 20605e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 20615e5d6fedSRoopa Prabhu flkeys = &_flkeys; 20625e5d6fedSRoopa Prabhu 206323aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2064b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 206506e9d040SJiri Benc skb_dst_drop(skb); 2066b75cc8f9SDavid Ahern skb_dst_set(skb, 2067b75cc8f9SDavid Ahern ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); 2068c71099acSThomas Graf } 2069c71099acSThomas Graf 2070b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2071b75cc8f9SDavid Ahern struct fib6_table *table, 2072b75cc8f9SDavid Ahern struct flowi6 *fl6, 2073b75cc8f9SDavid Ahern const struct sk_buff *skb, 2074b75cc8f9SDavid Ahern int flags) 2075c71099acSThomas Graf { 2076b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2077c71099acSThomas Graf } 2078c71099acSThomas Graf 20796f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 20806f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2081c71099acSThomas Graf { 2082d46a9d67SDavid Ahern bool any_src; 2083c71099acSThomas Graf 20843ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 20853ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 20864c1feac5SDavid Ahern struct dst_entry *dst; 20874c1feac5SDavid Ahern 20884c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2089ca254490SDavid Ahern if (dst) 2090ca254490SDavid Ahern return dst; 20914c1feac5SDavid Ahern } 2092ca254490SDavid Ahern 20931fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 20944dc27d1cSDavid McCullough 2095d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2096741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2097d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 209877d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2099c71099acSThomas Graf 2100d46a9d67SDavid Ahern if (!any_src) 2101adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 21020c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 21030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2104adaa70bbSThomas Graf 2105b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 21061da177e4SLinus Torvalds } 21076f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 21081da177e4SLinus Torvalds 21092774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 211014e50e57SDavid S. Miller { 21115c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 21121dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 211314e50e57SDavid S. Miller struct dst_entry *new = NULL; 211414e50e57SDavid S. Miller 21151dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 211662cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 211714e50e57SDavid S. Miller if (rt) { 21180a1f5962SMartin KaFai Lau rt6_info_init(rt); 211981eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 21200a1f5962SMartin KaFai Lau 2121d8d1f30bSChangli Gao new = &rt->dst; 212214e50e57SDavid S. Miller new->__use = 1; 2123352e512cSHerbert Xu new->input = dst_discard; 2124ede2059dSEric W. Biederman new->output = dst_discard_out; 212514e50e57SDavid S. Miller 2126defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 212714e50e57SDavid S. Miller 21281dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 21294e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 21300a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 213114e50e57SDavid S. Miller 213214e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 213314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 213414e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 213514e50e57SDavid S. Miller #endif 213614e50e57SDavid S. Miller } 213714e50e57SDavid S. Miller 213869ead7afSDavid S. Miller dst_release(dst_orig); 213969ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 214014e50e57SDavid S. Miller } 214114e50e57SDavid S. Miller 21421da177e4SLinus Torvalds /* 21431da177e4SLinus Torvalds * Destination cache support functions 21441da177e4SLinus Torvalds */ 21451da177e4SLinus Torvalds 21468d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 21473da59bd9SMartin KaFai Lau { 214836143645SSteffen Klassert u32 rt_cookie = 0; 2149c5cff856SWei Wang 21508ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 215193531c67SDavid Ahern return false; 215293531c67SDavid Ahern 215393531c67SDavid Ahern if (fib6_check_expired(f6i)) 215493531c67SDavid Ahern return false; 215593531c67SDavid Ahern 215693531c67SDavid Ahern return true; 215793531c67SDavid Ahern } 215893531c67SDavid Ahern 2159a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2160a68886a6SDavid Ahern struct fib6_info *from, 2161a68886a6SDavid Ahern u32 cookie) 21623da59bd9SMartin KaFai Lau { 2163c5cff856SWei Wang u32 rt_cookie = 0; 2164c5cff856SWei Wang 2165a68886a6SDavid Ahern if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || 216693531c67SDavid Ahern rt_cookie != cookie) 21673da59bd9SMartin KaFai Lau return NULL; 21683da59bd9SMartin KaFai Lau 21693da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 21703da59bd9SMartin KaFai Lau return NULL; 21713da59bd9SMartin KaFai Lau 21723da59bd9SMartin KaFai Lau return &rt->dst; 21733da59bd9SMartin KaFai Lau } 21743da59bd9SMartin KaFai Lau 2175a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2176a68886a6SDavid Ahern struct fib6_info *from, 2177a68886a6SDavid Ahern u32 cookie) 21783da59bd9SMartin KaFai Lau { 21795973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 21805973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2181a68886a6SDavid Ahern fib6_check(from, cookie)) 21823da59bd9SMartin KaFai Lau return &rt->dst; 21833da59bd9SMartin KaFai Lau else 21843da59bd9SMartin KaFai Lau return NULL; 21853da59bd9SMartin KaFai Lau } 21863da59bd9SMartin KaFai Lau 21871da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 21881da177e4SLinus Torvalds { 2189a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2190a68886a6SDavid Ahern struct fib6_info *from; 21911da177e4SLinus Torvalds struct rt6_info *rt; 21921da177e4SLinus Torvalds 2193a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2194a87b7dc9SDavid Ahern 2195a87b7dc9SDavid Ahern rcu_read_lock(); 21961da177e4SLinus Torvalds 21976f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 21986f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 21996f3118b5SNicolas Dichtel * into this function always. 22006f3118b5SNicolas Dichtel */ 2201e3bc10bdSHannes Frederic Sowa 2202a68886a6SDavid Ahern from = rcu_dereference(rt->from); 22034b32b5adSMartin KaFai Lau 2204a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2205a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2206a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 22073da59bd9SMartin KaFai Lau else 2208a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2209a87b7dc9SDavid Ahern 2210a87b7dc9SDavid Ahern rcu_read_unlock(); 2211a87b7dc9SDavid Ahern 2212a87b7dc9SDavid Ahern return dst_ret; 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 22161da177e4SLinus Torvalds { 22171da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds if (rt) { 222054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2221c3c14da0SDavid Ahern rcu_read_lock(); 222254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 222393531c67SDavid Ahern rt6_remove_exception_rt(rt); 222454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 22251da177e4SLinus Torvalds } 2226c3c14da0SDavid Ahern rcu_read_unlock(); 222754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 222854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 222954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 223054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 223154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 223254c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 22361da177e4SLinus Torvalds { 22371da177e4SLinus Torvalds struct rt6_info *rt; 22381da177e4SLinus Torvalds 22393ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 22401da177e4SLinus Torvalds 2241adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 22421da177e4SLinus Torvalds if (rt) { 22438a14e46fSDavid Ahern rcu_read_lock(); 22441eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 224593531c67SDavid Ahern rt6_remove_exception_rt(rt); 2246c5cff856SWei Wang } else { 2247a68886a6SDavid Ahern struct fib6_info *from; 2248c5cff856SWei Wang struct fib6_node *fn; 2249c5cff856SWei Wang 2250a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2251a68886a6SDavid Ahern if (from) { 2252a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2253c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2254c5cff856SWei Wang fn->fn_sernum = -1; 2255a68886a6SDavid Ahern } 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds rcu_read_unlock(); 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22616a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 22626a3e030fSDavid Ahern { 2263a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2264a68886a6SDavid Ahern struct fib6_info *from; 2265a68886a6SDavid Ahern 2266a68886a6SDavid Ahern rcu_read_lock(); 2267a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2268a68886a6SDavid Ahern if (from) 2269a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2270a68886a6SDavid Ahern rcu_read_unlock(); 2271a68886a6SDavid Ahern } 22726a3e030fSDavid Ahern 22736a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 22746a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 22756700c270SDavid S. Miller } 22761da177e4SLinus Torvalds 227745e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 227845e4fd26SMartin KaFai Lau { 227945e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 228045e4fd26SMartin KaFai Lau 2281d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 228245e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 228345e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 228445e4fd26SMartin KaFai Lau } 228545e4fd26SMartin KaFai Lau 22860d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 22870d3f6d29SMartin KaFai Lau { 22880d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 22891490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 22900d3f6d29SMartin KaFai Lau } 22910d3f6d29SMartin KaFai Lau 229245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 229345e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 22941da177e4SLinus Torvalds { 22950dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 22961da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 22971da177e4SLinus Torvalds 229819bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 229919bda36cSXin Long return; 230019bda36cSXin Long 230145e4fd26SMartin KaFai Lau if (iph) { 230245e4fd26SMartin KaFai Lau daddr = &iph->daddr; 230345e4fd26SMartin KaFai Lau saddr = &iph->saddr; 230445e4fd26SMartin KaFai Lau } else if (sk) { 230545e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 230645e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 230745e4fd26SMartin KaFai Lau } else { 23080dec879fSJulian Anastasov daddr = NULL; 23090dec879fSJulian Anastasov saddr = NULL; 23101da177e4SLinus Torvalds } 23110dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 23120dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 23130dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 23140dec879fSJulian Anastasov return; 23150dec879fSJulian Anastasov 23160dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 23170dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 23182b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 23192b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 23202b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 23210dec879fSJulian Anastasov } else if (daddr) { 2322a68886a6SDavid Ahern struct fib6_info *from; 23230dec879fSJulian Anastasov struct rt6_info *nrt6; 23240dec879fSJulian Anastasov 23254d85cd0cSDavid Ahern rcu_read_lock(); 2326a68886a6SDavid Ahern from = rcu_dereference(rt6->from); 2327a68886a6SDavid Ahern nrt6 = ip6_rt_cache_alloc(from, daddr, saddr); 232845e4fd26SMartin KaFai Lau if (nrt6) { 232945e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 2330a68886a6SDavid Ahern if (rt6_insert_exception(nrt6, from)) 23312b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 233245e4fd26SMartin KaFai Lau } 2333a68886a6SDavid Ahern rcu_read_unlock(); 233445e4fd26SMartin KaFai Lau } 233545e4fd26SMartin KaFai Lau } 233645e4fd26SMartin KaFai Lau 233745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 233845e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 233945e4fd26SMartin KaFai Lau { 234045e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 234342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2344e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 234581aded24SDavid S. Miller { 234681aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 234781aded24SDavid S. Miller struct dst_entry *dst; 2348dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2349dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2350dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2351dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2352dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2353dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2354dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2355dc92095dSMaciej Żenczykowski }; 235681aded24SDavid S. Miller 235781aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 235881aded24SDavid S. Miller if (!dst->error) 235945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 236081aded24SDavid S. Miller dst_release(dst); 236181aded24SDavid S. Miller } 236281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 236381aded24SDavid S. Miller 236481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 236581aded24SDavid S. Miller { 23667ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 236733c162a9SMartin KaFai Lau struct dst_entry *dst; 236833c162a9SMartin KaFai Lau 23697ddacfa5SDavid Ahern if (!oif && skb->dev) 23707ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 23717ddacfa5SDavid Ahern 23727ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 237333c162a9SMartin KaFai Lau 237433c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 237533c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 237633c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 237733c162a9SMartin KaFai Lau return; 237833c162a9SMartin KaFai Lau 237933c162a9SMartin KaFai Lau bh_lock_sock(sk); 238033c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 238133c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 238233c162a9SMartin KaFai Lau bh_unlock_sock(sk); 238381aded24SDavid S. Miller } 238481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 238581aded24SDavid S. Miller 23867d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 23877d6850f7SAlexey Kodanev const struct flowi6 *fl6) 23887d6850f7SAlexey Kodanev { 23897d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 23907d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 23917d6850f7SAlexey Kodanev #endif 23927d6850f7SAlexey Kodanev 23937d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 23947d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 23957d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 23967d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 23977d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 23987d6850f7SAlexey Kodanev &np->saddr : 23997d6850f7SAlexey Kodanev #endif 24007d6850f7SAlexey Kodanev NULL); 24017d6850f7SAlexey Kodanev } 24027d6850f7SAlexey Kodanev 2403b55b76b2SDuan Jiong /* Handle redirects */ 2404b55b76b2SDuan Jiong struct ip6rd_flowi { 2405b55b76b2SDuan Jiong struct flowi6 fl6; 2406b55b76b2SDuan Jiong struct in6_addr gateway; 2407b55b76b2SDuan Jiong }; 2408b55b76b2SDuan Jiong 2409b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2410b55b76b2SDuan Jiong struct fib6_table *table, 2411b55b76b2SDuan Jiong struct flowi6 *fl6, 2412b75cc8f9SDavid Ahern const struct sk_buff *skb, 2413b55b76b2SDuan Jiong int flags) 2414b55b76b2SDuan Jiong { 2415b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 241623fb93a4SDavid Ahern struct rt6_info *ret = NULL, *rt_cache; 24178d1c802bSDavid Ahern struct fib6_info *rt; 2418b55b76b2SDuan Jiong struct fib6_node *fn; 2419b55b76b2SDuan Jiong 2420b55b76b2SDuan Jiong /* Get the "current" route for this destination and 242167c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2422b55b76b2SDuan Jiong * 2423b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2424b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2425b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2426b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2427b55b76b2SDuan Jiong * routes. 2428b55b76b2SDuan Jiong */ 2429b55b76b2SDuan Jiong 243066f5d6ceSWei Wang rcu_read_lock(); 24316454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2432b55b76b2SDuan Jiong restart: 243366f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 2434ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 24358067bb8cSIdo Schimmel continue; 243614895687SDavid Ahern if (fib6_check_expired(rt)) 2437b55b76b2SDuan Jiong continue; 243893c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2439b55b76b2SDuan Jiong break; 2440bdf00467SDavid Ahern if (!rt->fib6_nh.fib_nh_gw_family) 2441b55b76b2SDuan Jiong continue; 2442ad1601aeSDavid Ahern if (fl6->flowi6_oif != rt->fib6_nh.fib_nh_dev->ifindex) 2443b55b76b2SDuan Jiong continue; 24442b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 24452b760fcfSWei Wang * in the case of an ip redirect. 24462b760fcfSWei Wang * So we keep searching in the exception table if the gateway 24472b760fcfSWei Wang * is different. 24482b760fcfSWei Wang */ 2449ad1601aeSDavid Ahern if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.fib_nh_gw6)) { 24502b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 24512b760fcfSWei Wang &fl6->daddr, 24522b760fcfSWei Wang &fl6->saddr); 24532b760fcfSWei Wang if (rt_cache && 24542b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 24552b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 245623fb93a4SDavid Ahern ret = rt_cache; 24572b760fcfSWei Wang break; 24582b760fcfSWei Wang } 2459b55b76b2SDuan Jiong continue; 24602b760fcfSWei Wang } 2461b55b76b2SDuan Jiong break; 2462b55b76b2SDuan Jiong } 2463b55b76b2SDuan Jiong 2464b55b76b2SDuan Jiong if (!rt) 2465421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 246693c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 246723fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2468b0a1ba59SMartin KaFai Lau goto out; 2469b0a1ba59SMartin KaFai Lau } 2470b0a1ba59SMartin KaFai Lau 2471421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2472a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2473a3c00e46SMartin KaFai Lau if (fn) 2474a3c00e46SMartin KaFai Lau goto restart; 2475b55b76b2SDuan Jiong } 2476a3c00e46SMartin KaFai Lau 2477b0a1ba59SMartin KaFai Lau out: 247823fb93a4SDavid Ahern if (ret) 247910585b43SDavid Ahern ip6_hold_safe(net, &ret); 248023fb93a4SDavid Ahern else 248123fb93a4SDavid Ahern ret = ip6_create_rt_rcu(rt); 2482b55b76b2SDuan Jiong 248366f5d6ceSWei Wang rcu_read_unlock(); 2484b55b76b2SDuan Jiong 2485b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 248623fb93a4SDavid Ahern return ret; 2487b55b76b2SDuan Jiong }; 2488b55b76b2SDuan Jiong 2489b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2490b55b76b2SDuan Jiong const struct flowi6 *fl6, 2491b75cc8f9SDavid Ahern const struct sk_buff *skb, 2492b55b76b2SDuan Jiong const struct in6_addr *gateway) 2493b55b76b2SDuan Jiong { 2494b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2495b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2496b55b76b2SDuan Jiong 2497b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2498b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2499b55b76b2SDuan Jiong 2500b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2501b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2502b55b76b2SDuan Jiong } 2503b55b76b2SDuan Jiong 2504e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2505e2d118a1SLorenzo Colitti kuid_t uid) 25063a5ad2eeSDavid S. Miller { 25073a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 25083a5ad2eeSDavid S. Miller struct dst_entry *dst; 25091f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 25101f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 25111f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 25121f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 25131f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 25141f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 25151f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 25161f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 25171f7f10acSMaciej Żenczykowski }; 25183a5ad2eeSDavid S. Miller 2519b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 25206700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 25213a5ad2eeSDavid S. Miller dst_release(dst); 25223a5ad2eeSDavid S. Miller } 25233a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 25243a5ad2eeSDavid S. Miller 2525d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 2526c92a59ecSDuan Jiong { 2527c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2528c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2529c92a59ecSDuan Jiong struct dst_entry *dst; 25300b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 25310b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 25320b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 25330b26fb17SMaciej Żenczykowski .daddr = msg->dest, 25340b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 25350b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 25360b26fb17SMaciej Żenczykowski }; 2537c92a59ecSDuan Jiong 2538b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 2539c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2540c92a59ecSDuan Jiong dst_release(dst); 2541c92a59ecSDuan Jiong } 2542c92a59ecSDuan Jiong 25433a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 25443a5ad2eeSDavid S. Miller { 2545e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2546e2d118a1SLorenzo Colitti sk->sk_uid); 25473a5ad2eeSDavid S. Miller } 25483a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 25493a5ad2eeSDavid S. Miller 25500dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 25511da177e4SLinus Torvalds { 25520dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 25530dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 25540dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 25550dbaee3bSDavid S. Miller 25561da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 25571da177e4SLinus Torvalds 25585578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 25595578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds /* 25621da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 25631da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 25641da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 25651da177e4SLinus Torvalds * rely only on pmtu discovery" 25661da177e4SLinus Torvalds */ 25671da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 25681da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 25691da177e4SLinus Torvalds return mtu; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds 2572ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2573d33e4553SDavid S. Miller { 2574d33e4553SDavid S. Miller struct inet6_dev *idev; 2575d4ead6b3SDavid Ahern unsigned int mtu; 2576618f9bc7SSteffen Klassert 25774b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 25784b32b5adSMartin KaFai Lau if (mtu) 25794b32b5adSMartin KaFai Lau goto out; 25804b32b5adSMartin KaFai Lau 2581618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2582d33e4553SDavid S. Miller 2583d33e4553SDavid S. Miller rcu_read_lock(); 2584d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2585d33e4553SDavid S. Miller if (idev) 2586d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2587d33e4553SDavid S. Miller rcu_read_unlock(); 2588d33e4553SDavid S. Miller 258930f78d8eSEric Dumazet out: 259014972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 259114972cbdSRoopa Prabhu 259214972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2593d33e4553SDavid S. Miller } 2594d33e4553SDavid S. Miller 2595901731b8SDavid Ahern /* MTU selection: 2596901731b8SDavid Ahern * 1. mtu on route is locked - use it 2597901731b8SDavid Ahern * 2. mtu from nexthop exception 2598901731b8SDavid Ahern * 3. mtu from egress device 2599901731b8SDavid Ahern * 2600901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 2601901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 2602901731b8SDavid Ahern */ 2603901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr, 2604901731b8SDavid Ahern struct in6_addr *saddr) 2605901731b8SDavid Ahern { 2606901731b8SDavid Ahern struct rt6_exception_bucket *bucket; 2607901731b8SDavid Ahern struct rt6_exception *rt6_ex; 2608901731b8SDavid Ahern struct in6_addr *src_key; 2609901731b8SDavid Ahern struct inet6_dev *idev; 2610901731b8SDavid Ahern u32 mtu = 0; 2611901731b8SDavid Ahern 2612901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 2613901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 2614901731b8SDavid Ahern if (mtu) 2615901731b8SDavid Ahern goto out; 2616901731b8SDavid Ahern } 2617901731b8SDavid Ahern 2618901731b8SDavid Ahern src_key = NULL; 2619901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 2620901731b8SDavid Ahern if (f6i->fib6_src.plen) 2621901731b8SDavid Ahern src_key = saddr; 2622901731b8SDavid Ahern #endif 2623901731b8SDavid Ahern 2624901731b8SDavid Ahern bucket = rcu_dereference(f6i->rt6i_exception_bucket); 2625901731b8SDavid Ahern rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 2626901731b8SDavid Ahern if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 2627901731b8SDavid Ahern mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU); 2628901731b8SDavid Ahern 2629901731b8SDavid Ahern if (likely(!mtu)) { 2630901731b8SDavid Ahern struct net_device *dev = fib6_info_nh_dev(f6i); 2631901731b8SDavid Ahern 2632901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 2633901731b8SDavid Ahern idev = __in6_dev_get(dev); 2634901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 2635901731b8SDavid Ahern mtu = idev->cnf.mtu6; 2636901731b8SDavid Ahern } 2637901731b8SDavid Ahern 2638901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 2639901731b8SDavid Ahern out: 2640901731b8SDavid Ahern return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu); 2641901731b8SDavid Ahern } 2642901731b8SDavid Ahern 26433b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 264487a11578SDavid S. Miller struct flowi6 *fl6) 26451da177e4SLinus Torvalds { 264687a11578SDavid S. Miller struct dst_entry *dst; 26471da177e4SLinus Torvalds struct rt6_info *rt; 26481da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2649c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26501da177e4SLinus Torvalds 265138308473SDavid S. Miller if (unlikely(!idev)) 2652122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 26531da177e4SLinus Torvalds 2654ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 265538308473SDavid S. Miller if (unlikely(!rt)) { 26561da177e4SLinus Torvalds in6_dev_put(idev); 265787a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 26581da177e4SLinus Torvalds goto out; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 26618e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2662588753f1SBrendan McGrath rt->dst.input = ip6_input; 26638e2ec639SYan, Zheng rt->dst.output = ip6_output; 2664550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 266587a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 26668e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 26678e2ec639SYan, Zheng rt->rt6i_idev = idev; 266814edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 26691da177e4SLinus Torvalds 26704c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 2671587fea74SWei Wang * do proper release of the net_device 2672587fea74SWei Wang */ 2673587fea74SWei Wang rt6_uncached_list_add(rt); 267481eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 26751da177e4SLinus Torvalds 267687a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 267787a11578SDavid S. Miller 26781da177e4SLinus Torvalds out: 267987a11578SDavid S. Miller return dst; 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds 2682569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 26831da177e4SLinus Torvalds { 268486393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 26857019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 26867019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 26877019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 26887019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 26897019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2690fc66f95cSEric Dumazet int entries; 26911da177e4SLinus Torvalds 2692fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 269349a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2694fc66f95cSEric Dumazet entries <= rt_max_size) 26951da177e4SLinus Torvalds goto out; 26961da177e4SLinus Torvalds 26976891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 269814956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2699fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2700fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 27017019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 27021da177e4SLinus Torvalds out: 27037019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2704fc66f95cSEric Dumazet return entries > rt_max_size; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27078c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 27088c14586fSDavid Ahern struct fib6_config *cfg, 2709f4797b33SDavid Ahern const struct in6_addr *gw_addr, 2710f4797b33SDavid Ahern u32 tbid, int flags) 27118c14586fSDavid Ahern { 27128c14586fSDavid Ahern struct flowi6 fl6 = { 27138c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 27148c14586fSDavid Ahern .daddr = *gw_addr, 27158c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 27168c14586fSDavid Ahern }; 27178c14586fSDavid Ahern struct fib6_table *table; 27188c14586fSDavid Ahern struct rt6_info *rt; 27198c14586fSDavid Ahern 2720f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 27218c14586fSDavid Ahern if (!table) 27228c14586fSDavid Ahern return NULL; 27238c14586fSDavid Ahern 27248c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 27258c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 27268c14586fSDavid Ahern 2727f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 2728b75cc8f9SDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); 27298c14586fSDavid Ahern 27308c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 27318c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 27328c14586fSDavid Ahern ip6_rt_put(rt); 27338c14586fSDavid Ahern rt = NULL; 27348c14586fSDavid Ahern } 27358c14586fSDavid Ahern 27368c14586fSDavid Ahern return rt; 27378c14586fSDavid Ahern } 27388c14586fSDavid Ahern 2739fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 2740fc1e64e1SDavid Ahern struct fib6_config *cfg, 27419fbb704cSDavid Ahern const struct net_device *dev, 2742fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 2743fc1e64e1SDavid Ahern { 274444750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 2745fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 2746fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 2747bf1dc8baSPaolo Abeni struct fib6_info *from; 2748fc1e64e1SDavid Ahern struct rt6_info *grt; 2749fc1e64e1SDavid Ahern int err; 2750fc1e64e1SDavid Ahern 2751fc1e64e1SDavid Ahern err = 0; 2752fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 2753fc1e64e1SDavid Ahern if (grt) { 2754bf1dc8baSPaolo Abeni rcu_read_lock(); 2755bf1dc8baSPaolo Abeni from = rcu_dereference(grt->from); 275658e354c0SDavid Ahern if (!grt->dst.error && 27574ed591c8SDavid Ahern /* ignore match if it is the default route */ 2758bf1dc8baSPaolo Abeni from && !ipv6_addr_any(&from->fib6_dst.addr) && 275958e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 276044750f84SDavid Ahern NL_SET_ERR_MSG(extack, 276144750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 2762fc1e64e1SDavid Ahern err = -EINVAL; 2763fc1e64e1SDavid Ahern } 2764bf1dc8baSPaolo Abeni rcu_read_unlock(); 2765fc1e64e1SDavid Ahern 2766fc1e64e1SDavid Ahern ip6_rt_put(grt); 2767fc1e64e1SDavid Ahern } 2768fc1e64e1SDavid Ahern 2769fc1e64e1SDavid Ahern return err; 2770fc1e64e1SDavid Ahern } 2771fc1e64e1SDavid Ahern 27721edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 27731edce99fSDavid Ahern struct fib6_config *cfg, 27741edce99fSDavid Ahern struct net_device **_dev, 27751edce99fSDavid Ahern struct inet6_dev **idev) 27761edce99fSDavid Ahern { 27771edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 27781edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 27791edce99fSDavid Ahern struct rt6_info *grt = NULL; 27801edce99fSDavid Ahern int err = -EHOSTUNREACH; 27811edce99fSDavid Ahern 27821edce99fSDavid Ahern if (cfg->fc_table) { 2783f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 2784f4797b33SDavid Ahern 2785f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 2786f4797b33SDavid Ahern cfg->fc_table, flags); 27871edce99fSDavid Ahern if (grt) { 27881edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 27891edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 27901edce99fSDavid Ahern ip6_rt_put(grt); 27911edce99fSDavid Ahern grt = NULL; 27921edce99fSDavid Ahern } 27931edce99fSDavid Ahern } 27941edce99fSDavid Ahern } 27951edce99fSDavid Ahern 27961edce99fSDavid Ahern if (!grt) 2797b75cc8f9SDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); 27981edce99fSDavid Ahern 27991edce99fSDavid Ahern if (!grt) 28001edce99fSDavid Ahern goto out; 28011edce99fSDavid Ahern 28021edce99fSDavid Ahern if (dev) { 28031edce99fSDavid Ahern if (dev != grt->dst.dev) { 28041edce99fSDavid Ahern ip6_rt_put(grt); 28051edce99fSDavid Ahern goto out; 28061edce99fSDavid Ahern } 28071edce99fSDavid Ahern } else { 28081edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 28091edce99fSDavid Ahern *idev = grt->rt6i_idev; 28101edce99fSDavid Ahern dev_hold(dev); 28111edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 28121edce99fSDavid Ahern } 28131edce99fSDavid Ahern 28141edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 28151edce99fSDavid Ahern err = 0; 28161edce99fSDavid Ahern 28171edce99fSDavid Ahern ip6_rt_put(grt); 28181edce99fSDavid Ahern 28191edce99fSDavid Ahern out: 28201edce99fSDavid Ahern return err; 28211edce99fSDavid Ahern } 28221edce99fSDavid Ahern 28239fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 28249fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 28259fbb704cSDavid Ahern struct netlink_ext_ack *extack) 28269fbb704cSDavid Ahern { 28279fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 28289fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 2829232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 28309fbb704cSDavid Ahern const struct net_device *dev = *_dev; 2831232378e8SDavid Ahern bool need_addr_check = !dev; 28329fbb704cSDavid Ahern int err = -EINVAL; 28339fbb704cSDavid Ahern 28349fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 28359fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 28369fbb704cSDavid Ahern * will return already-added prefix route via interface that 28379fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 28389fbb704cSDavid Ahern */ 2839232378e8SDavid Ahern if (dev && 2840232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2841232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 28429fbb704cSDavid Ahern goto out; 28439fbb704cSDavid Ahern } 28449fbb704cSDavid Ahern 28459fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 28469fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 28479fbb704cSDavid Ahern * addresses as nexthop address. 28489fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 28499fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 28509fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 28519fbb704cSDavid Ahern * some exceptions. --ANK 28529fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 28539fbb704cSDavid Ahern * addressing 28549fbb704cSDavid Ahern */ 28559fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 28569fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 28579fbb704cSDavid Ahern goto out; 28589fbb704cSDavid Ahern } 28599fbb704cSDavid Ahern 28609fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 28619fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 28629fbb704cSDavid Ahern else 28639fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 28649fbb704cSDavid Ahern 28659fbb704cSDavid Ahern if (err) 28669fbb704cSDavid Ahern goto out; 28679fbb704cSDavid Ahern } 28689fbb704cSDavid Ahern 28699fbb704cSDavid Ahern /* reload in case device was changed */ 28709fbb704cSDavid Ahern dev = *_dev; 28719fbb704cSDavid Ahern 28729fbb704cSDavid Ahern err = -EINVAL; 28739fbb704cSDavid Ahern if (!dev) { 28749fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 28759fbb704cSDavid Ahern goto out; 28769fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 28779fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 28789fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 28799fbb704cSDavid Ahern goto out; 28809fbb704cSDavid Ahern } 2881232378e8SDavid Ahern 2882232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 2883232378e8SDavid Ahern * egress device has been resolved. 2884232378e8SDavid Ahern */ 2885232378e8SDavid Ahern if (need_addr_check && 2886232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2887232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 2888232378e8SDavid Ahern goto out; 2889232378e8SDavid Ahern } 2890232378e8SDavid Ahern 28919fbb704cSDavid Ahern err = 0; 28929fbb704cSDavid Ahern out: 28939fbb704cSDavid Ahern return err; 28949fbb704cSDavid Ahern } 28959fbb704cSDavid Ahern 289683c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 289783c44251SDavid Ahern { 289883c44251SDavid Ahern if ((flags & RTF_REJECT) || 289983c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 290083c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 290183c44251SDavid Ahern !(flags & RTF_LOCAL))) 290283c44251SDavid Ahern return true; 290383c44251SDavid Ahern 290483c44251SDavid Ahern return false; 290583c44251SDavid Ahern } 290683c44251SDavid Ahern 290783c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 290883c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 290983c44251SDavid Ahern struct netlink_ext_ack *extack) 291083c44251SDavid Ahern { 291183c44251SDavid Ahern struct net_device *dev = NULL; 291283c44251SDavid Ahern struct inet6_dev *idev = NULL; 291383c44251SDavid Ahern int addr_type; 291483c44251SDavid Ahern int err; 291583c44251SDavid Ahern 2916f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 2917f1741730SDavid Ahern 291883c44251SDavid Ahern err = -ENODEV; 291983c44251SDavid Ahern if (cfg->fc_ifindex) { 292083c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 292183c44251SDavid Ahern if (!dev) 292283c44251SDavid Ahern goto out; 292383c44251SDavid Ahern idev = in6_dev_get(dev); 292483c44251SDavid Ahern if (!idev) 292583c44251SDavid Ahern goto out; 292683c44251SDavid Ahern } 292783c44251SDavid Ahern 292883c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 292983c44251SDavid Ahern if (!dev) { 293083c44251SDavid Ahern NL_SET_ERR_MSG(extack, 293183c44251SDavid Ahern "Nexthop device required for onlink"); 293283c44251SDavid Ahern goto out; 293383c44251SDavid Ahern } 293483c44251SDavid Ahern 293583c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 293683c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 293783c44251SDavid Ahern err = -ENETDOWN; 293883c44251SDavid Ahern goto out; 293983c44251SDavid Ahern } 294083c44251SDavid Ahern 2941ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 294283c44251SDavid Ahern } 294383c44251SDavid Ahern 2944ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 294583c44251SDavid Ahern 294683c44251SDavid Ahern /* We cannot add true routes via loopback here, 294783c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 294883c44251SDavid Ahern */ 294983c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 295083c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 295183c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 295283c44251SDavid Ahern if (dev != net->loopback_dev) { 295383c44251SDavid Ahern if (dev) { 295483c44251SDavid Ahern dev_put(dev); 295583c44251SDavid Ahern in6_dev_put(idev); 295683c44251SDavid Ahern } 295783c44251SDavid Ahern dev = net->loopback_dev; 295883c44251SDavid Ahern dev_hold(dev); 295983c44251SDavid Ahern idev = in6_dev_get(dev); 296083c44251SDavid Ahern if (!idev) { 296183c44251SDavid Ahern err = -ENODEV; 296283c44251SDavid Ahern goto out; 296383c44251SDavid Ahern } 296483c44251SDavid Ahern } 296583c44251SDavid Ahern goto set_dev; 296683c44251SDavid Ahern } 296783c44251SDavid Ahern 296883c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 296983c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 297083c44251SDavid Ahern if (err) 297183c44251SDavid Ahern goto out; 297283c44251SDavid Ahern 2973ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 2974bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 297583c44251SDavid Ahern } 297683c44251SDavid Ahern 297783c44251SDavid Ahern err = -ENODEV; 297883c44251SDavid Ahern if (!dev) 297983c44251SDavid Ahern goto out; 298083c44251SDavid Ahern 298183c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 298283c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 298383c44251SDavid Ahern err = -EACCES; 298483c44251SDavid Ahern goto out; 298583c44251SDavid Ahern } 298683c44251SDavid Ahern 298783c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 298883c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 298983c44251SDavid Ahern err = -ENETDOWN; 299083c44251SDavid Ahern goto out; 299183c44251SDavid Ahern } 299283c44251SDavid Ahern 299383c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 299483c44251SDavid Ahern !netif_carrier_ok(dev)) 2995ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 299683c44251SDavid Ahern 2997979e276eSDavid Ahern err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, 2998979e276eSDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 2999979e276eSDavid Ahern if (err) 3000979e276eSDavid Ahern goto out; 300183c44251SDavid Ahern set_dev: 3002ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3003f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 300483c44251SDavid Ahern err = 0; 300583c44251SDavid Ahern out: 300683c44251SDavid Ahern if (idev) 300783c44251SDavid Ahern in6_dev_put(idev); 300883c44251SDavid Ahern 300983c44251SDavid Ahern if (err) { 3010ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3011ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 301283c44251SDavid Ahern if (dev) 301383c44251SDavid Ahern dev_put(dev); 301483c44251SDavid Ahern } 301583c44251SDavid Ahern 301683c44251SDavid Ahern return err; 301783c44251SDavid Ahern } 301883c44251SDavid Ahern 3019dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3020dac7d0f2SDavid Ahern { 3021979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3022dac7d0f2SDavid Ahern } 3023dac7d0f2SDavid Ahern 30248d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3025acb54e3cSDavid Ahern gfp_t gfp_flags, 3026333c4301SDavid Ahern struct netlink_ext_ack *extack) 30271da177e4SLinus Torvalds { 30285578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 30298d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3030c71099acSThomas Graf struct fib6_table *table; 30318c5b83f0SRoopa Prabhu int err = -EINVAL; 303283c44251SDavid Ahern int addr_type; 30331da177e4SLinus Torvalds 3034557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3035d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3036d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3037557c44beSDavid Ahern goto out; 3038d5d531cbSDavid Ahern } 3039557c44beSDavid Ahern 30402ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 30412ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 30422ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 30432ea2352eSWei Wang goto out; 30442ea2352eSWei Wang } 30452ea2352eSWei Wang 3046e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3047e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3048e8478e80SDavid Ahern goto out; 3049e8478e80SDavid Ahern } 3050e8478e80SDavid Ahern 3051d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3052d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 30538c5b83f0SRoopa Prabhu goto out; 3054d5d531cbSDavid Ahern } 3055d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3056d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3057d5d531cbSDavid Ahern goto out; 3058d5d531cbSDavid Ahern } 30591da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3060d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3061d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3062d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 30638c5b83f0SRoopa Prabhu goto out; 3064d5d531cbSDavid Ahern } 30651da177e4SLinus Torvalds #endif 3066fc1e64e1SDavid Ahern 3067c71099acSThomas Graf err = -ENOBUFS; 306838308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3069d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3070d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 307138308473SDavid S. Miller if (!table) { 3072f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3073d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3074d71314b4SMatti Vaittinen } 3075d71314b4SMatti Vaittinen } else { 3076d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3077d71314b4SMatti Vaittinen } 307838308473SDavid S. Miller 307938308473SDavid S. Miller if (!table) 3080c71099acSThomas Graf goto out; 3081c71099acSThomas Graf 30821da177e4SLinus Torvalds err = -ENOMEM; 308393531c67SDavid Ahern rt = fib6_info_alloc(gfp_flags); 308493531c67SDavid Ahern if (!rt) 30851da177e4SLinus Torvalds goto out; 308693531c67SDavid Ahern 3087d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3088d7e774f3SDavid Ahern extack); 3089767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3090767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3091fda21d46SEric Dumazet /* Do not leave garbage there. */ 3092fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 3093767a2217SDavid Ahern goto out; 3094767a2217SDavid Ahern } 3095767a2217SDavid Ahern 309693531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 309793531c67SDavid Ahern rt->dst_nocount = true; 30981da177e4SLinus Torvalds 30991716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 310014895687SDavid Ahern fib6_set_expires(rt, jiffies + 31011716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 31021716a961SGao feng else 310314895687SDavid Ahern fib6_clean_expires(rt); 31041da177e4SLinus Torvalds 310586872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 310686872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 310793c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 310886872cb5SThomas Graf 310983c44251SDavid Ahern rt->fib6_table = table; 311083c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 311183c44251SDavid Ahern rt->fib6_type = cfg->fc_type; 31122b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 311319e42e45SRoopa Prabhu 311493c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 311593c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 311693c2fb25SDavid Ahern if (rt->fib6_dst.plen == 128) 31173b6761d1SDavid Ahern rt->dst_host = true; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 312093c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 312193c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 31221da177e4SLinus Torvalds #endif 312383c44251SDavid Ahern err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack); 31241da177e4SLinus Torvalds if (err) 31251da177e4SLinus Torvalds goto out; 31269fbb704cSDavid Ahern 312783c44251SDavid Ahern /* We cannot add true routes via loopback here, 312883c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 312983c44251SDavid Ahern */ 313083c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3131ad1601aeSDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type)) 313283c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3133955ec4cbSDavid Ahern 3134c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 313583c44251SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 313683c44251SDavid Ahern 3137c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3138d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3139c3968a85SDaniel Walter err = -EINVAL; 3140c3968a85SDaniel Walter goto out; 3141c3968a85SDaniel Walter } 314293c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 314393c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3144c3968a85SDaniel Walter } else 314593c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3146c3968a85SDaniel Walter 31478c5b83f0SRoopa Prabhu return rt; 31481da177e4SLinus Torvalds out: 314993531c67SDavid Ahern fib6_info_release(rt); 31508c5b83f0SRoopa Prabhu return ERR_PTR(err); 31516b9ea5a6SRoopa Prabhu } 31526b9ea5a6SRoopa Prabhu 3153acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3154333c4301SDavid Ahern struct netlink_ext_ack *extack) 31556b9ea5a6SRoopa Prabhu { 31568d1c802bSDavid Ahern struct fib6_info *rt; 31576b9ea5a6SRoopa Prabhu int err; 31586b9ea5a6SRoopa Prabhu 3159acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3160d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3161d4ead6b3SDavid Ahern return PTR_ERR(rt); 31626b9ea5a6SRoopa Prabhu 3163d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 316493531c67SDavid Ahern fib6_info_release(rt); 31656b9ea5a6SRoopa Prabhu 31661da177e4SLinus Torvalds return err; 31671da177e4SLinus Torvalds } 31681da177e4SLinus Torvalds 31698d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 31701da177e4SLinus Torvalds { 3171afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3172c71099acSThomas Graf struct fib6_table *table; 3173afb1d4b5SDavid Ahern int err; 31741da177e4SLinus Torvalds 3175421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 31766825a26cSGao feng err = -ENOENT; 31776825a26cSGao feng goto out; 31786825a26cSGao feng } 31796c813a72SPatrick McHardy 318093c2fb25SDavid Ahern table = rt->fib6_table; 318166f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 318286872cb5SThomas Graf err = fib6_del(rt, info); 318366f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 31841da177e4SLinus Torvalds 31856825a26cSGao feng out: 318693531c67SDavid Ahern fib6_info_release(rt); 31871da177e4SLinus Torvalds return err; 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds 31908d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt) 3191e0a1ad73SThomas Graf { 3192afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net }; 3193afb1d4b5SDavid Ahern 3194528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3195e0a1ad73SThomas Graf } 3196e0a1ad73SThomas Graf 31978d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 31980ae81335SDavid Ahern { 31990ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3200e3330039SWANG Cong struct net *net = info->nl_net; 320116a16cd3SDavid Ahern struct sk_buff *skb = NULL; 32020ae81335SDavid Ahern struct fib6_table *table; 3203e3330039SWANG Cong int err = -ENOENT; 32040ae81335SDavid Ahern 3205421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3206e3330039SWANG Cong goto out_put; 320793c2fb25SDavid Ahern table = rt->fib6_table; 320866f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 32090ae81335SDavid Ahern 321093c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 32118d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 32120ae81335SDavid Ahern 321316a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 321416a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 321516a16cd3SDavid Ahern if (skb) { 321616a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 321716a16cd3SDavid Ahern 3218d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 321916a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 322016a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 322116a16cd3SDavid Ahern kfree_skb(skb); 322216a16cd3SDavid Ahern skb = NULL; 322316a16cd3SDavid Ahern } else 322416a16cd3SDavid Ahern info->skip_notify = 1; 322516a16cd3SDavid Ahern } 322616a16cd3SDavid Ahern 32270ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 322893c2fb25SDavid Ahern &rt->fib6_siblings, 322993c2fb25SDavid Ahern fib6_siblings) { 32300ae81335SDavid Ahern err = fib6_del(sibling, info); 32310ae81335SDavid Ahern if (err) 3232e3330039SWANG Cong goto out_unlock; 32330ae81335SDavid Ahern } 32340ae81335SDavid Ahern } 32350ae81335SDavid Ahern 32360ae81335SDavid Ahern err = fib6_del(rt, info); 3237e3330039SWANG Cong out_unlock: 323866f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3239e3330039SWANG Cong out_put: 324093531c67SDavid Ahern fib6_info_release(rt); 324116a16cd3SDavid Ahern 324216a16cd3SDavid Ahern if (skb) { 3243e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 324416a16cd3SDavid Ahern info->nlh, gfp_any()); 324516a16cd3SDavid Ahern } 32460ae81335SDavid Ahern return err; 32470ae81335SDavid Ahern } 32480ae81335SDavid Ahern 324923fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 325023fb93a4SDavid Ahern { 325123fb93a4SDavid Ahern int rc = -ESRCH; 325223fb93a4SDavid Ahern 325323fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 325423fb93a4SDavid Ahern goto out; 325523fb93a4SDavid Ahern 325623fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 325723fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 325823fb93a4SDavid Ahern goto out; 3259761f6026SXin Long 326023fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 326123fb93a4SDavid Ahern out: 326223fb93a4SDavid Ahern return rc; 326323fb93a4SDavid Ahern } 326423fb93a4SDavid Ahern 3265333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3266333c4301SDavid Ahern struct netlink_ext_ack *extack) 32671da177e4SLinus Torvalds { 32688d1c802bSDavid Ahern struct rt6_info *rt_cache; 3269c71099acSThomas Graf struct fib6_table *table; 32708d1c802bSDavid Ahern struct fib6_info *rt; 32711da177e4SLinus Torvalds struct fib6_node *fn; 32721da177e4SLinus Torvalds int err = -ESRCH; 32731da177e4SLinus Torvalds 32745578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3275d5d531cbSDavid Ahern if (!table) { 3276d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3277c71099acSThomas Graf return err; 3278d5d531cbSDavid Ahern } 32791da177e4SLinus Torvalds 328066f5d6ceSWei Wang rcu_read_lock(); 3281c71099acSThomas Graf 3282c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 328386872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 328438fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 32852b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 32861da177e4SLinus Torvalds 32871da177e4SLinus Torvalds if (fn) { 328866f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3289ad1601aeSDavid Ahern struct fib6_nh *nh; 3290ad1601aeSDavid Ahern 32912b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 329223fb93a4SDavid Ahern int rc; 329323fb93a4SDavid Ahern 32942b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 32952b760fcfSWei Wang &cfg->fc_src); 329623fb93a4SDavid Ahern if (rt_cache) { 329723fb93a4SDavid Ahern rc = ip6_del_cached_rt(rt_cache, cfg); 32989e575010SEric Dumazet if (rc != -ESRCH) { 32999e575010SEric Dumazet rcu_read_unlock(); 330023fb93a4SDavid Ahern return rc; 330123fb93a4SDavid Ahern } 33029e575010SEric Dumazet } 33031f56a01fSMartin KaFai Lau continue; 33042b760fcfSWei Wang } 3305ad1601aeSDavid Ahern 3306ad1601aeSDavid Ahern nh = &rt->fib6_nh; 330786872cb5SThomas Graf if (cfg->fc_ifindex && 3308ad1601aeSDavid Ahern (!nh->fib_nh_dev || 3309ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 33101da177e4SLinus Torvalds continue; 331186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 3312ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 33131da177e4SLinus Torvalds continue; 331493c2fb25SDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 33151da177e4SLinus Torvalds continue; 331693c2fb25SDavid Ahern if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) 3317c2ed1880SMantas M continue; 3318e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3319e873e4b9SWei Wang continue; 332066f5d6ceSWei Wang rcu_read_unlock(); 33211da177e4SLinus Torvalds 33220ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 33230ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 332486872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 33250ae81335SDavid Ahern 33260ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 33271da177e4SLinus Torvalds } 33281da177e4SLinus Torvalds } 332966f5d6ceSWei Wang rcu_read_unlock(); 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds return err; 33321da177e4SLinus Torvalds } 33331da177e4SLinus Torvalds 33346700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3335a6279458SYOSHIFUJI Hideaki { 3336a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3337e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 3338e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3339e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3340e8599ff4SDavid S. Miller struct neighbour *neigh; 3341a68886a6SDavid Ahern struct fib6_info *from; 334271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 33436e157b6aSDavid S. Miller int optlen, on_link; 33446e157b6aSDavid S. Miller u8 *lladdr; 3345e8599ff4SDavid S. Miller 334629a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 334771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3348e8599ff4SDavid S. Miller 3349e8599ff4SDavid S. Miller if (optlen < 0) { 33506e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3351e8599ff4SDavid S. Miller return; 3352e8599ff4SDavid S. Miller } 3353e8599ff4SDavid S. Miller 335471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3355e8599ff4SDavid S. Miller 335671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 33576e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3358e8599ff4SDavid S. Miller return; 3359e8599ff4SDavid S. Miller } 3360e8599ff4SDavid S. Miller 33616e157b6aSDavid S. Miller on_link = 0; 336271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3363e8599ff4SDavid S. Miller on_link = 1; 336471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3365e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 33666e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3367e8599ff4SDavid S. Miller return; 3368e8599ff4SDavid S. Miller } 3369e8599ff4SDavid S. Miller 3370e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3371e8599ff4SDavid S. Miller if (!in6_dev) 3372e8599ff4SDavid S. Miller return; 3373e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3374e8599ff4SDavid S. Miller return; 3375e8599ff4SDavid S. Miller 3376e8599ff4SDavid S. Miller /* RFC2461 8.1: 3377e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3378e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3379e8599ff4SDavid S. Miller */ 3380e8599ff4SDavid S. Miller 3381f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3382e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3383e8599ff4SDavid S. Miller return; 3384e8599ff4SDavid S. Miller } 33856e157b6aSDavid S. Miller 33866e157b6aSDavid S. Miller lladdr = NULL; 3387e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3388e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3389e8599ff4SDavid S. Miller skb->dev); 3390e8599ff4SDavid S. Miller if (!lladdr) { 3391e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3392e8599ff4SDavid S. Miller return; 3393e8599ff4SDavid S. Miller } 3394e8599ff4SDavid S. Miller } 3395e8599ff4SDavid S. Miller 33966e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3397ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 33986e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 33996e157b6aSDavid S. Miller return; 34006e157b6aSDavid S. Miller } 34016e157b6aSDavid S. Miller 34026e157b6aSDavid S. Miller /* Redirect received -> path was valid. 34036e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 34046e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 34056e157b6aSDavid S. Miller */ 34060dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 34076e157b6aSDavid S. Miller 340871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3409e8599ff4SDavid S. Miller if (!neigh) 3410e8599ff4SDavid S. Miller return; 3411e8599ff4SDavid S. Miller 34121da177e4SLinus Torvalds /* 34131da177e4SLinus Torvalds * We have finally decided to accept it. 34141da177e4SLinus Torvalds */ 34151da177e4SLinus Torvalds 3416f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 34171da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 34181da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 34191da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3420f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3421f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 34221da177e4SLinus Torvalds 34234d85cd0cSDavid Ahern rcu_read_lock(); 3424a68886a6SDavid Ahern from = rcu_dereference(rt->from); 3425e873e4b9SWei Wang /* This fib6_info_hold() is safe here because we hold reference to rt 3426e873e4b9SWei Wang * and rt already holds reference to fib6_info. 3427e873e4b9SWei Wang */ 34288a14e46fSDavid Ahern fib6_info_hold(from); 34294d85cd0cSDavid Ahern rcu_read_unlock(); 34308a14e46fSDavid Ahern 34318a14e46fSDavid Ahern nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL); 343238308473SDavid S. Miller if (!nrt) 34331da177e4SLinus Torvalds goto out; 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 34361da177e4SLinus Torvalds if (on_link) 34371da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 34381da177e4SLinus Torvalds 34394e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 34401da177e4SLinus Torvalds 34412b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 34422b760fcfSWei Wang * a cached route because rt6_insert_exception() will 34432b760fcfSWei Wang * takes care of it 34442b760fcfSWei Wang */ 34458a14e46fSDavid Ahern if (rt6_insert_exception(nrt, from)) { 34462b760fcfSWei Wang dst_release_immediate(&nrt->dst); 34472b760fcfSWei Wang goto out; 34482b760fcfSWei Wang } 34491da177e4SLinus Torvalds 3450d8d1f30bSChangli Gao netevent.old = &rt->dst; 3451d8d1f30bSChangli Gao netevent.new = &nrt->dst; 345271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 345360592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 34548d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 34558d71740cSTom Tucker 34561da177e4SLinus Torvalds out: 34578a14e46fSDavid Ahern fib6_info_release(from); 3458e8599ff4SDavid S. Miller neigh_release(neigh); 34596e157b6aSDavid S. Miller } 34606e157b6aSDavid S. Miller 346170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 34628d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 3463b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3464830218c1SDavid Ahern const struct in6_addr *gwaddr, 3465830218c1SDavid Ahern struct net_device *dev) 346670ceb4f5SYOSHIFUJI Hideaki { 3467830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3468830218c1SDavid Ahern int ifindex = dev->ifindex; 346970ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 34708d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3471c71099acSThomas Graf struct fib6_table *table; 347270ceb4f5SYOSHIFUJI Hideaki 3473830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 347438308473SDavid S. Miller if (!table) 3475c71099acSThomas Graf return NULL; 3476c71099acSThomas Graf 347766f5d6ceSWei Wang rcu_read_lock(); 347838fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 347970ceb4f5SYOSHIFUJI Hideaki if (!fn) 348070ceb4f5SYOSHIFUJI Hideaki goto out; 348170ceb4f5SYOSHIFUJI Hideaki 348266f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3483ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex) 348470ceb4f5SYOSHIFUJI Hideaki continue; 34852b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 3486bdf00467SDavid Ahern !rt->fib6_nh.fib_nh_gw_family) 348770ceb4f5SYOSHIFUJI Hideaki continue; 3488ad1601aeSDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr)) 348970ceb4f5SYOSHIFUJI Hideaki continue; 3490e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3491e873e4b9SWei Wang continue; 349270ceb4f5SYOSHIFUJI Hideaki break; 349370ceb4f5SYOSHIFUJI Hideaki } 349470ceb4f5SYOSHIFUJI Hideaki out: 349566f5d6ceSWei Wang rcu_read_unlock(); 349670ceb4f5SYOSHIFUJI Hideaki return rt; 349770ceb4f5SYOSHIFUJI Hideaki } 349870ceb4f5SYOSHIFUJI Hideaki 34998d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 3500b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3501830218c1SDavid Ahern const struct in6_addr *gwaddr, 3502830218c1SDavid Ahern struct net_device *dev, 350395c96174SEric Dumazet unsigned int pref) 350470ceb4f5SYOSHIFUJI Hideaki { 350586872cb5SThomas Graf struct fib6_config cfg = { 3506238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3507830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 350886872cb5SThomas Graf .fc_dst_len = prefixlen, 350986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 351086872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3511b91d5329SXin Long .fc_protocol = RTPROT_RA, 3512e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 351315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3514efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3515efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 351686872cb5SThomas Graf }; 351770ceb4f5SYOSHIFUJI Hideaki 3518830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 35194e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 35204e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 352186872cb5SThomas Graf 3522e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3523e317da96SYOSHIFUJI Hideaki if (!prefixlen) 352486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 352570ceb4f5SYOSHIFUJI Hideaki 3526acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 352770ceb4f5SYOSHIFUJI Hideaki 3528830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 352970ceb4f5SYOSHIFUJI Hideaki } 353070ceb4f5SYOSHIFUJI Hideaki #endif 353170ceb4f5SYOSHIFUJI Hideaki 35328d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 3533afb1d4b5SDavid Ahern const struct in6_addr *addr, 3534afb1d4b5SDavid Ahern struct net_device *dev) 35351da177e4SLinus Torvalds { 3536830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 35378d1c802bSDavid Ahern struct fib6_info *rt; 3538c71099acSThomas Graf struct fib6_table *table; 35391da177e4SLinus Torvalds 3540afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 354138308473SDavid S. Miller if (!table) 3542c71099acSThomas Graf return NULL; 35431da177e4SLinus Torvalds 354466f5d6ceSWei Wang rcu_read_lock(); 354566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3546ad1601aeSDavid Ahern struct fib6_nh *nh = &rt->fib6_nh; 3547ad1601aeSDavid Ahern 3548ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 354993c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 3550ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 35511da177e4SLinus Torvalds break; 35521da177e4SLinus Torvalds } 3553e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 3554e873e4b9SWei Wang rt = NULL; 355566f5d6ceSWei Wang rcu_read_unlock(); 35561da177e4SLinus Torvalds return rt; 35571da177e4SLinus Torvalds } 35581da177e4SLinus Torvalds 35598d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 3560afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 3561ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3562ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 35631da177e4SLinus Torvalds { 356486872cb5SThomas Graf struct fib6_config cfg = { 3565ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3566238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 356786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 356886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 356986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3570b91d5329SXin Long .fc_protocol = RTPROT_RA, 3571e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 357215e47304SEric W. Biederman .fc_nlinfo.portid = 0, 35735578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3574afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 357586872cb5SThomas Graf }; 35761da177e4SLinus Torvalds 35774e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 35781da177e4SLinus Torvalds 3579acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 3580830218c1SDavid Ahern struct fib6_table *table; 3581830218c1SDavid Ahern 3582830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3583830218c1SDavid Ahern if (table) 3584830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3585830218c1SDavid Ahern } 35861da177e4SLinus Torvalds 3587afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds 3590afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 3591afb1d4b5SDavid Ahern struct fib6_table *table) 35921da177e4SLinus Torvalds { 35938d1c802bSDavid Ahern struct fib6_info *rt; 35941da177e4SLinus Torvalds 35951da177e4SLinus Torvalds restart: 359666f5d6ceSWei Wang rcu_read_lock(); 359766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3598dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 3599dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 3600dcd1f572SDavid Ahern 360193c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 3602e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 3603e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 360466f5d6ceSWei Wang rcu_read_unlock(); 3605afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 36061da177e4SLinus Torvalds goto restart; 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds } 360966f5d6ceSWei Wang rcu_read_unlock(); 3610830218c1SDavid Ahern 3611830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3612830218c1SDavid Ahern } 3613830218c1SDavid Ahern 3614830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3615830218c1SDavid Ahern { 3616830218c1SDavid Ahern struct fib6_table *table; 3617830218c1SDavid Ahern struct hlist_head *head; 3618830218c1SDavid Ahern unsigned int h; 3619830218c1SDavid Ahern 3620830218c1SDavid Ahern rcu_read_lock(); 3621830218c1SDavid Ahern 3622830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3623830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3624830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3625830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3626afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 3627830218c1SDavid Ahern } 3628830218c1SDavid Ahern } 3629830218c1SDavid Ahern 3630830218c1SDavid Ahern rcu_read_unlock(); 36311da177e4SLinus Torvalds } 36321da177e4SLinus Torvalds 36335578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 36345578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 363586872cb5SThomas Graf struct fib6_config *cfg) 363686872cb5SThomas Graf { 36378823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 36388823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 36398823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 36408823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 364167f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 36428823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 36438823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 36448823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 36458823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 36468823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 364786872cb5SThomas Graf 36488823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 364986872cb5SThomas Graf 36508823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 36518823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 36528823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 36538823a3acSMaciej Żenczykowski }; 365486872cb5SThomas Graf } 365586872cb5SThomas Graf 36565578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 36571da177e4SLinus Torvalds { 365886872cb5SThomas Graf struct fib6_config cfg; 36591da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 36601da177e4SLinus Torvalds int err; 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds switch (cmd) { 36631da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 36641da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3665af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 36661da177e4SLinus Torvalds return -EPERM; 36671da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 36681da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 36691da177e4SLinus Torvalds if (err) 36701da177e4SLinus Torvalds return -EFAULT; 36711da177e4SLinus Torvalds 36725578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 367386872cb5SThomas Graf 36741da177e4SLinus Torvalds rtnl_lock(); 36751da177e4SLinus Torvalds switch (cmd) { 36761da177e4SLinus Torvalds case SIOCADDRT: 3677acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 36781da177e4SLinus Torvalds break; 36791da177e4SLinus Torvalds case SIOCDELRT: 3680333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 36811da177e4SLinus Torvalds break; 36821da177e4SLinus Torvalds default: 36831da177e4SLinus Torvalds err = -EINVAL; 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds rtnl_unlock(); 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds return err; 36883ff50b79SStephen Hemminger } 36891da177e4SLinus Torvalds 36901da177e4SLinus Torvalds return -EINVAL; 36911da177e4SLinus Torvalds } 36921da177e4SLinus Torvalds 36931da177e4SLinus Torvalds /* 36941da177e4SLinus Torvalds * Drop the packet on the floor 36951da177e4SLinus Torvalds */ 36961da177e4SLinus Torvalds 3697d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 36981da177e4SLinus Torvalds { 3699612f09e8SYOSHIFUJI Hideaki int type; 3700adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3701612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3702612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 37030660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 370445bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 3705bdb7cc64SStephen Suryaputra IP6_INC_STATS(dev_net(dst->dev), 3706bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), 37073bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3708612f09e8SYOSHIFUJI Hideaki break; 3709612f09e8SYOSHIFUJI Hideaki } 3710612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3711612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 37123bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 37133bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3714612f09e8SYOSHIFUJI Hideaki break; 3715612f09e8SYOSHIFUJI Hideaki } 37163ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 37171da177e4SLinus Torvalds kfree_skb(skb); 37181da177e4SLinus Torvalds return 0; 37191da177e4SLinus Torvalds } 37201da177e4SLinus Torvalds 37219ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 37229ce8ade0SThomas Graf { 3723612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 37249ce8ade0SThomas Graf } 37259ce8ade0SThomas Graf 3726ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 37271da177e4SLinus Torvalds { 3728adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3729612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 37301da177e4SLinus Torvalds } 37311da177e4SLinus Torvalds 37329ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 37339ce8ade0SThomas Graf { 3734612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 37359ce8ade0SThomas Graf } 37369ce8ade0SThomas Graf 3737ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 37389ce8ade0SThomas Graf { 3739adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3740612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 37419ce8ade0SThomas Graf } 37429ce8ade0SThomas Graf 37431da177e4SLinus Torvalds /* 37441da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 37451da177e4SLinus Torvalds */ 37461da177e4SLinus Torvalds 3747360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 3748afb1d4b5SDavid Ahern struct inet6_dev *idev, 37491da177e4SLinus Torvalds const struct in6_addr *addr, 3750acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 37511da177e4SLinus Torvalds { 3752c7a1ce39SDavid Ahern struct fib6_config cfg = { 3753c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 3754c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 3755c7a1ce39SDavid Ahern .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, 3756c7a1ce39SDavid Ahern .fc_dst = *addr, 3757c7a1ce39SDavid Ahern .fc_dst_len = 128, 3758c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 3759c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 3760c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 3761c7a1ce39SDavid Ahern }; 37625f02ce24SDavid Ahern 3763e8478e80SDavid Ahern if (anycast) { 3764c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 3765c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 3766e8478e80SDavid Ahern } else { 3767c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 3768c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 3769e8478e80SDavid Ahern } 37701da177e4SLinus Torvalds 3771c7a1ce39SDavid Ahern return ip6_route_info_create(&cfg, gfp_flags, NULL); 37721da177e4SLinus Torvalds } 37731da177e4SLinus Torvalds 3774c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3775c3968a85SDaniel Walter struct arg_dev_net_ip { 3776c3968a85SDaniel Walter struct net_device *dev; 3777c3968a85SDaniel Walter struct net *net; 3778c3968a85SDaniel Walter struct in6_addr *addr; 3779c3968a85SDaniel Walter }; 3780c3968a85SDaniel Walter 37818d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 3782c3968a85SDaniel Walter { 3783c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3784c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3785c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3786c3968a85SDaniel Walter 3787ad1601aeSDavid Ahern if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) && 3788421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 378993c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 379060006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3791c3968a85SDaniel Walter /* remove prefsrc entry */ 379293c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 379360006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3794c3968a85SDaniel Walter } 3795c3968a85SDaniel Walter return 0; 3796c3968a85SDaniel Walter } 3797c3968a85SDaniel Walter 3798c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3799c3968a85SDaniel Walter { 3800c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3801c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3802c3968a85SDaniel Walter .dev = ifp->idev->dev, 3803c3968a85SDaniel Walter .net = net, 3804c3968a85SDaniel Walter .addr = &ifp->addr, 3805c3968a85SDaniel Walter }; 38060c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3807c3968a85SDaniel Walter } 3808c3968a85SDaniel Walter 38092b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 3810be7a010dSDuan Jiong 3811be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 38128d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 3813be7a010dSDuan Jiong { 3814be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3815be7a010dSDuan Jiong 381693c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3817bdf00467SDavid Ahern rt->fib6_nh.fib_nh_gw_family && 3818ad1601aeSDavid Ahern ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) { 3819be7a010dSDuan Jiong return -1; 3820be7a010dSDuan Jiong } 3821b16cb459SWei Wang 3822b16cb459SWei Wang /* Further clean up cached routes in exception table. 3823b16cb459SWei Wang * This is needed because cached route may have a different 3824b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3825b16cb459SWei Wang */ 3826b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3827b16cb459SWei Wang 3828be7a010dSDuan Jiong return 0; 3829be7a010dSDuan Jiong } 3830be7a010dSDuan Jiong 3831be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3832be7a010dSDuan Jiong { 3833be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3834be7a010dSDuan Jiong } 3835be7a010dSDuan Jiong 38362127d95aSIdo Schimmel struct arg_netdev_event { 38372127d95aSIdo Schimmel const struct net_device *dev; 38384c981e28SIdo Schimmel union { 38392127d95aSIdo Schimmel unsigned int nh_flags; 38404c981e28SIdo Schimmel unsigned long event; 38414c981e28SIdo Schimmel }; 38422127d95aSIdo Schimmel }; 38432127d95aSIdo Schimmel 38448d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 3845d7dedee1SIdo Schimmel { 38468d1c802bSDavid Ahern struct fib6_info *iter; 3847d7dedee1SIdo Schimmel struct fib6_node *fn; 3848d7dedee1SIdo Schimmel 384993c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 385093c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3851d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 385293c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3853d7dedee1SIdo Schimmel while (iter) { 385493c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 385533bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 3856d7dedee1SIdo Schimmel return iter; 38578fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 385893c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3859d7dedee1SIdo Schimmel } 3860d7dedee1SIdo Schimmel 3861d7dedee1SIdo Schimmel return NULL; 3862d7dedee1SIdo Schimmel } 3863d7dedee1SIdo Schimmel 38648d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 3865d7dedee1SIdo Schimmel { 3866ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD || 3867ad1601aeSDavid Ahern (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN && 3868ad1601aeSDavid Ahern ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev))) 3869d7dedee1SIdo Schimmel return true; 3870d7dedee1SIdo Schimmel 3871d7dedee1SIdo Schimmel return false; 3872d7dedee1SIdo Schimmel } 3873d7dedee1SIdo Schimmel 38748d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 3875d7dedee1SIdo Schimmel { 38768d1c802bSDavid Ahern struct fib6_info *iter; 3877d7dedee1SIdo Schimmel int total = 0; 3878d7dedee1SIdo Schimmel 3879d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 3880ad1601aeSDavid Ahern total += rt->fib6_nh.fib_nh_weight; 3881d7dedee1SIdo Schimmel 388293c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 3883d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 3884ad1601aeSDavid Ahern total += iter->fib6_nh.fib_nh_weight; 3885d7dedee1SIdo Schimmel } 3886d7dedee1SIdo Schimmel 3887d7dedee1SIdo Schimmel return total; 3888d7dedee1SIdo Schimmel } 3889d7dedee1SIdo Schimmel 38908d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 3891d7dedee1SIdo Schimmel { 3892d7dedee1SIdo Schimmel int upper_bound = -1; 3893d7dedee1SIdo Schimmel 3894d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 3895ad1601aeSDavid Ahern *weight += rt->fib6_nh.fib_nh_weight; 3896d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 3897d7dedee1SIdo Schimmel total) - 1; 3898d7dedee1SIdo Schimmel } 3899ad1601aeSDavid Ahern atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound); 3900d7dedee1SIdo Schimmel } 3901d7dedee1SIdo Schimmel 39028d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 3903d7dedee1SIdo Schimmel { 39048d1c802bSDavid Ahern struct fib6_info *iter; 3905d7dedee1SIdo Schimmel int weight = 0; 3906d7dedee1SIdo Schimmel 3907d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 3908d7dedee1SIdo Schimmel 390993c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 3910d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 3911d7dedee1SIdo Schimmel } 3912d7dedee1SIdo Schimmel 39138d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 3914d7dedee1SIdo Schimmel { 39158d1c802bSDavid Ahern struct fib6_info *first; 3916d7dedee1SIdo Schimmel int total; 3917d7dedee1SIdo Schimmel 3918d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 3919d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 3920d7dedee1SIdo Schimmel * sibling route. 3921d7dedee1SIdo Schimmel */ 392293c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 3923d7dedee1SIdo Schimmel return; 3924d7dedee1SIdo Schimmel 3925d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 3926d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 3927d7dedee1SIdo Schimmel * onwards. 3928d7dedee1SIdo Schimmel */ 3929d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 3930d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 3931d7dedee1SIdo Schimmel return; 3932d7dedee1SIdo Schimmel 3933d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 3934d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 3935d7dedee1SIdo Schimmel } 3936d7dedee1SIdo Schimmel 39378d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 39382127d95aSIdo Schimmel { 39392127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 39407aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 39412127d95aSIdo Schimmel 3942ad1601aeSDavid Ahern if (rt != net->ipv6.fib6_null_entry && 3943ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_dev == arg->dev) { 3944ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags; 39457aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 3946d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 39471de178edSIdo Schimmel } 39482127d95aSIdo Schimmel 39492127d95aSIdo Schimmel return 0; 39502127d95aSIdo Schimmel } 39512127d95aSIdo Schimmel 39522127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) 39532127d95aSIdo Schimmel { 39542127d95aSIdo Schimmel struct arg_netdev_event arg = { 39552127d95aSIdo Schimmel .dev = dev, 39566802f3adSIdo Schimmel { 39572127d95aSIdo Schimmel .nh_flags = nh_flags, 39586802f3adSIdo Schimmel }, 39592127d95aSIdo Schimmel }; 39602127d95aSIdo Schimmel 39612127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 39622127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 39632127d95aSIdo Schimmel 39642127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 39652127d95aSIdo Schimmel } 39662127d95aSIdo Schimmel 39678d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 39681de178edSIdo Schimmel const struct net_device *dev) 39691de178edSIdo Schimmel { 39708d1c802bSDavid Ahern struct fib6_info *iter; 39711de178edSIdo Schimmel 3972ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == dev) 39731de178edSIdo Schimmel return true; 397493c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 3975ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == dev) 39761de178edSIdo Schimmel return true; 39771de178edSIdo Schimmel 39781de178edSIdo Schimmel return false; 39791de178edSIdo Schimmel } 39801de178edSIdo Schimmel 39818d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 39821de178edSIdo Schimmel { 39838d1c802bSDavid Ahern struct fib6_info *iter; 39841de178edSIdo Schimmel 39851de178edSIdo Schimmel rt->should_flush = 1; 398693c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 39871de178edSIdo Schimmel iter->should_flush = 1; 39881de178edSIdo Schimmel } 39891de178edSIdo Schimmel 39908d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 39911de178edSIdo Schimmel const struct net_device *down_dev) 39921de178edSIdo Schimmel { 39938d1c802bSDavid Ahern struct fib6_info *iter; 39941de178edSIdo Schimmel unsigned int dead = 0; 39951de178edSIdo Schimmel 3996ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == down_dev || 3997ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 39981de178edSIdo Schimmel dead++; 399993c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4000ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == down_dev || 4001ad1601aeSDavid Ahern iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 40021de178edSIdo Schimmel dead++; 40031de178edSIdo Schimmel 40041de178edSIdo Schimmel return dead; 40051de178edSIdo Schimmel } 40061de178edSIdo Schimmel 40078d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 40081de178edSIdo Schimmel const struct net_device *dev, 40091de178edSIdo Schimmel unsigned int nh_flags) 40101de178edSIdo Schimmel { 40118d1c802bSDavid Ahern struct fib6_info *iter; 40121de178edSIdo Schimmel 4013ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == dev) 4014ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags |= nh_flags; 401593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4016ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == dev) 4017ad1601aeSDavid Ahern iter->fib6_nh.fib_nh_flags |= nh_flags; 40181de178edSIdo Schimmel } 40191de178edSIdo Schimmel 4020a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 40218d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 40221da177e4SLinus Torvalds { 40234c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 40244c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 40257aef6859SDavid Ahern struct net *net = dev_net(dev); 40268ed67789SDaniel Lezcano 4027421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 402827c6fa73SIdo Schimmel return 0; 402927c6fa73SIdo Schimmel 403027c6fa73SIdo Schimmel switch (arg->event) { 403127c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 4032ad1601aeSDavid Ahern return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; 403327c6fa73SIdo Schimmel case NETDEV_DOWN: 40341de178edSIdo Schimmel if (rt->should_flush) 403527c6fa73SIdo Schimmel return -1; 403693c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 4037ad1601aeSDavid Ahern return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; 40381de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 40391de178edSIdo Schimmel unsigned int count; 40401de178edSIdo Schimmel 40411de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 404293c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 40431de178edSIdo Schimmel rt6_multipath_flush(rt); 40441de178edSIdo Schimmel return -1; 40451de178edSIdo Schimmel } 40461de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 40471de178edSIdo Schimmel RTNH_F_LINKDOWN); 40487aef6859SDavid Ahern fib6_update_sernum(net, rt); 4049d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 40501de178edSIdo Schimmel } 40511de178edSIdo Schimmel return -2; 405227c6fa73SIdo Schimmel case NETDEV_CHANGE: 4053ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev != dev || 405493c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 405527c6fa73SIdo Schimmel break; 4056ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN; 4057d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 405827c6fa73SIdo Schimmel break; 40592b241361SIdo Schimmel } 4060c159d30cSDavid S. Miller 40611da177e4SLinus Torvalds return 0; 40621da177e4SLinus Torvalds } 40631da177e4SLinus Torvalds 406427c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 40651da177e4SLinus Torvalds { 40664c981e28SIdo Schimmel struct arg_netdev_event arg = { 40678ed67789SDaniel Lezcano .dev = dev, 40686802f3adSIdo Schimmel { 40694c981e28SIdo Schimmel .event = event, 40706802f3adSIdo Schimmel }, 40718ed67789SDaniel Lezcano }; 40727c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 40738ed67789SDaniel Lezcano 40747c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 40757c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 40767c6bb7d2SDavid Ahern else 40777c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 40784c981e28SIdo Schimmel } 40794c981e28SIdo Schimmel 40804c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 40814c981e28SIdo Schimmel { 40824c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 40834c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 40844c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 40851da177e4SLinus Torvalds } 40861da177e4SLinus Torvalds 408795c96174SEric Dumazet struct rt6_mtu_change_arg { 40881da177e4SLinus Torvalds struct net_device *dev; 408995c96174SEric Dumazet unsigned int mtu; 40901da177e4SLinus Torvalds }; 40911da177e4SLinus Torvalds 40928d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg) 40931da177e4SLinus Torvalds { 40941da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 40951da177e4SLinus Torvalds struct inet6_dev *idev; 40961da177e4SLinus Torvalds 40971da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 40981da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 40991da177e4SLinus Torvalds We still use this lock to block changes 41001da177e4SLinus Torvalds caused by addrconf/ndisc. 41011da177e4SLinus Torvalds */ 41021da177e4SLinus Torvalds 41031da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 410438308473SDavid S. Miller if (!idev) 41051da177e4SLinus Torvalds return 0; 41061da177e4SLinus Torvalds 41071da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 41081da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 41091da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 41101da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 41111da177e4SLinus Torvalds */ 4112ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == arg->dev && 4113d4ead6b3SDavid Ahern !fib6_metric_locked(rt, RTAX_MTU)) { 4114d4ead6b3SDavid Ahern u32 mtu = rt->fib6_pmtu; 4115d4ead6b3SDavid Ahern 4116d4ead6b3SDavid Ahern if (mtu >= arg->mtu || 4117d4ead6b3SDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4118d4ead6b3SDavid Ahern fib6_metric_set(rt, RTAX_MTU, arg->mtu); 4119d4ead6b3SDavid Ahern 4120f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 4121e9fa1495SStefano Brivio rt6_exceptions_update_pmtu(idev, rt, arg->mtu); 4122f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 41234b32b5adSMartin KaFai Lau } 41241da177e4SLinus Torvalds return 0; 41251da177e4SLinus Torvalds } 41261da177e4SLinus Torvalds 412795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 41281da177e4SLinus Torvalds { 4129c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4130c71099acSThomas Graf .dev = dev, 4131c71099acSThomas Graf .mtu = mtu, 4132c71099acSThomas Graf }; 41331da177e4SLinus Torvalds 41340c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 41351da177e4SLinus Torvalds } 41361da177e4SLinus Torvalds 4137ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 41385176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4139aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 414086872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4141ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 414286872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 414386872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 414451ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4145c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 414619e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 414719e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 414832bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4149622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 41503b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4151aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4152eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4153eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4154eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 415586872cb5SThomas Graf }; 415686872cb5SThomas Graf 415786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4158333c4301SDavid Ahern struct fib6_config *cfg, 4159333c4301SDavid Ahern struct netlink_ext_ack *extack) 41601da177e4SLinus Torvalds { 416186872cb5SThomas Graf struct rtmsg *rtm; 416286872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4163c78ba6d6SLubomir Rintel unsigned int pref; 416486872cb5SThomas Graf int err; 41651da177e4SLinus Torvalds 4166fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4167dac9c979SDavid Ahern extack); 416886872cb5SThomas Graf if (err < 0) 416986872cb5SThomas Graf goto errout; 41701da177e4SLinus Torvalds 417186872cb5SThomas Graf err = -EINVAL; 417286872cb5SThomas Graf rtm = nlmsg_data(nlh); 417386872cb5SThomas Graf 417484db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 417584db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 417684db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 417784db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 417884db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 417984db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 418084db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 418184db8407SMaciej Żenczykowski 418284db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 418384db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 418484db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 418584db8407SMaciej Żenczykowski }; 418686872cb5SThomas Graf 4187ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4188ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4189b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4190b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 419186872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 419286872cb5SThomas Graf 4193ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4194ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4195ab79ad14SMaciej Żenczykowski 41961f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 41971f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 41981f56a01fSMartin KaFai Lau 4199fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4200fc1e64e1SDavid Ahern 420186872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 420267b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 420386872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 42041da177e4SLinus Torvalds } 4205e3818541SDavid Ahern if (tb[RTA_VIA]) { 4206e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 4207e3818541SDavid Ahern goto errout; 4208e3818541SDavid Ahern } 420986872cb5SThomas Graf 421086872cb5SThomas Graf if (tb[RTA_DST]) { 421186872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 421286872cb5SThomas Graf 421386872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 421486872cb5SThomas Graf goto errout; 421586872cb5SThomas Graf 421686872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 42171da177e4SLinus Torvalds } 421886872cb5SThomas Graf 421986872cb5SThomas Graf if (tb[RTA_SRC]) { 422086872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 422186872cb5SThomas Graf 422286872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 422386872cb5SThomas Graf goto errout; 422486872cb5SThomas Graf 422586872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 42261da177e4SLinus Torvalds } 422786872cb5SThomas Graf 4228c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 422967b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4230c3968a85SDaniel Walter 423186872cb5SThomas Graf if (tb[RTA_OIF]) 423286872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 423386872cb5SThomas Graf 423486872cb5SThomas Graf if (tb[RTA_PRIORITY]) 423586872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 423686872cb5SThomas Graf 423786872cb5SThomas Graf if (tb[RTA_METRICS]) { 423886872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 423986872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 42401da177e4SLinus Torvalds } 424186872cb5SThomas Graf 424286872cb5SThomas Graf if (tb[RTA_TABLE]) 424386872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 424486872cb5SThomas Graf 424551ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 424651ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 424751ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 42489ed59592SDavid Ahern 42499ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4250c255bd68SDavid Ahern cfg->fc_mp_len, extack); 42519ed59592SDavid Ahern if (err < 0) 42529ed59592SDavid Ahern goto errout; 425351ebd318SNicolas Dichtel } 425451ebd318SNicolas Dichtel 4255c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4256c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4257c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4258c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4259c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4260c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4261c78ba6d6SLubomir Rintel } 4262c78ba6d6SLubomir Rintel 426319e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 426419e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 426519e42e45SRoopa Prabhu 42669ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 426719e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 426819e42e45SRoopa Prabhu 4269c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 42709ed59592SDavid Ahern if (err < 0) 42719ed59592SDavid Ahern goto errout; 42729ed59592SDavid Ahern } 42739ed59592SDavid Ahern 427432bc201eSXin Long if (tb[RTA_EXPIRES]) { 427532bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 427632bc201eSXin Long 427732bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 427832bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 427932bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 428032bc201eSXin Long } 428132bc201eSXin Long } 428232bc201eSXin Long 428386872cb5SThomas Graf err = 0; 428486872cb5SThomas Graf errout: 428586872cb5SThomas Graf return err; 42861da177e4SLinus Torvalds } 42871da177e4SLinus Torvalds 42886b9ea5a6SRoopa Prabhu struct rt6_nh { 42898d1c802bSDavid Ahern struct fib6_info *fib6_info; 42906b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 42916b9ea5a6SRoopa Prabhu struct list_head next; 42926b9ea5a6SRoopa Prabhu }; 42936b9ea5a6SRoopa Prabhu 4294d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 4295d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 42968d1c802bSDavid Ahern struct fib6_info *rt, 42978d1c802bSDavid Ahern struct fib6_config *r_cfg) 42986b9ea5a6SRoopa Prabhu { 42996b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 43006b9ea5a6SRoopa Prabhu int err = -EEXIST; 43016b9ea5a6SRoopa Prabhu 43026b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 43038d1c802bSDavid Ahern /* check if fib6_info already exists */ 43048d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 43056b9ea5a6SRoopa Prabhu return err; 43066b9ea5a6SRoopa Prabhu } 43076b9ea5a6SRoopa Prabhu 43086b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 43096b9ea5a6SRoopa Prabhu if (!nh) 43106b9ea5a6SRoopa Prabhu return -ENOMEM; 43118d1c802bSDavid Ahern nh->fib6_info = rt; 43126b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 43136b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 43146b9ea5a6SRoopa Prabhu 43156b9ea5a6SRoopa Prabhu return 0; 43166b9ea5a6SRoopa Prabhu } 43176b9ea5a6SRoopa Prabhu 43188d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 43198d1c802bSDavid Ahern struct fib6_info *rt_last, 43203b1137feSDavid Ahern struct nl_info *info, 43213b1137feSDavid Ahern __u16 nlflags) 43223b1137feSDavid Ahern { 43233b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 43243b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 43253b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 43263b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 43273b1137feSDavid Ahern * the list, find the first sibling of the last route appended 43283b1137feSDavid Ahern */ 432993c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 433093c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 43318d1c802bSDavid Ahern struct fib6_info, 433293c2fb25SDavid Ahern fib6_siblings); 43333b1137feSDavid Ahern } 43343b1137feSDavid Ahern 43353b1137feSDavid Ahern if (rt) 43363b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 43373b1137feSDavid Ahern } 43383b1137feSDavid Ahern 4339333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4340333c4301SDavid Ahern struct netlink_ext_ack *extack) 434151ebd318SNicolas Dichtel { 43428d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 43433b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 434451ebd318SNicolas Dichtel struct fib6_config r_cfg; 434551ebd318SNicolas Dichtel struct rtnexthop *rtnh; 43468d1c802bSDavid Ahern struct fib6_info *rt; 43476b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 43486b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 43493b1137feSDavid Ahern __u16 nlflags; 435051ebd318SNicolas Dichtel int remaining; 435151ebd318SNicolas Dichtel int attrlen; 43526b9ea5a6SRoopa Prabhu int err = 1; 43536b9ea5a6SRoopa Prabhu int nhn = 0; 43546b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 43556b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 43566b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 435751ebd318SNicolas Dichtel 43583b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 43593b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 43603b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 43613b1137feSDavid Ahern 436235f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 436351ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 436451ebd318SNicolas Dichtel 43656b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 43668d1c802bSDavid Ahern * fib6_info structs per nexthop 43676b9ea5a6SRoopa Prabhu */ 436851ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 436951ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 437051ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 437151ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 437251ebd318SNicolas Dichtel 437351ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 437451ebd318SNicolas Dichtel if (attrlen > 0) { 437551ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 437651ebd318SNicolas Dichtel 437751ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 437851ebd318SNicolas Dichtel if (nla) { 437967b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 438051ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 438151ebd318SNicolas Dichtel } 438219e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 438319e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 438419e42e45SRoopa Prabhu if (nla) 438519e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 438651ebd318SNicolas Dichtel } 43876b9ea5a6SRoopa Prabhu 438868e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 4389acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 43908c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 43918c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 43928c5b83f0SRoopa Prabhu rt = NULL; 43936b9ea5a6SRoopa Prabhu goto cleanup; 43948c5b83f0SRoopa Prabhu } 4395b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 4396b5d2d75eSDavid Ahern err = -EINVAL; 4397b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 4398b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 4399b5d2d75eSDavid Ahern fib6_info_release(rt); 4400b5d2d75eSDavid Ahern goto cleanup; 4401b5d2d75eSDavid Ahern } 44026b9ea5a6SRoopa Prabhu 4403ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1; 4404398958aeSIdo Schimmel 4405d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 4406d4ead6b3SDavid Ahern rt, &r_cfg); 440751ebd318SNicolas Dichtel if (err) { 440893531c67SDavid Ahern fib6_info_release(rt); 44096b9ea5a6SRoopa Prabhu goto cleanup; 441051ebd318SNicolas Dichtel } 44116b9ea5a6SRoopa Prabhu 44126b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 441351ebd318SNicolas Dichtel } 44146b9ea5a6SRoopa Prabhu 44153b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 44163b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 44173b1137feSDavid Ahern * the full route when done 44183b1137feSDavid Ahern */ 44193b1137feSDavid Ahern info->skip_notify = 1; 44203b1137feSDavid Ahern 44216b9ea5a6SRoopa Prabhu err_nh = NULL; 44226b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 44238d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 44248d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 44253b1137feSDavid Ahern 4426f7225172SDavid Ahern if (!err) { 4427f7225172SDavid Ahern /* save reference to last route successfully inserted */ 4428f7225172SDavid Ahern rt_last = nh->fib6_info; 4429f7225172SDavid Ahern 44306b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 4431f7225172SDavid Ahern if (!rt_notif) 44328d1c802bSDavid Ahern rt_notif = nh->fib6_info; 4433f7225172SDavid Ahern } 44346b9ea5a6SRoopa Prabhu 44358d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 44368d1c802bSDavid Ahern nh->fib6_info = NULL; 44376b9ea5a6SRoopa Prabhu if (err) { 44386b9ea5a6SRoopa Prabhu if (replace && nhn) 4439a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 4440a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 44416b9ea5a6SRoopa Prabhu err_nh = nh; 44426b9ea5a6SRoopa Prabhu goto add_errout; 44436b9ea5a6SRoopa Prabhu } 44446b9ea5a6SRoopa Prabhu 44451a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 444627596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 444727596472SMichal Kubeček * we have already failed to add the first nexthop: 444827596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 444927596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 445027596472SMichal Kubeček * be added to it. 44511a72418bSNicolas Dichtel */ 445227596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 445327596472SMichal Kubeček NLM_F_REPLACE); 44546b9ea5a6SRoopa Prabhu nhn++; 44556b9ea5a6SRoopa Prabhu } 44566b9ea5a6SRoopa Prabhu 44573b1137feSDavid Ahern /* success ... tell user about new route */ 44583b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 44596b9ea5a6SRoopa Prabhu goto cleanup; 44606b9ea5a6SRoopa Prabhu 44616b9ea5a6SRoopa Prabhu add_errout: 44623b1137feSDavid Ahern /* send notification for routes that were added so that 44633b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 44643b1137feSDavid Ahern * coherent 44653b1137feSDavid Ahern */ 44663b1137feSDavid Ahern if (rt_notif) 44673b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 44683b1137feSDavid Ahern 44696b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 44706b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 44716b9ea5a6SRoopa Prabhu if (err_nh == nh) 44726b9ea5a6SRoopa Prabhu break; 4473333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 44746b9ea5a6SRoopa Prabhu } 44756b9ea5a6SRoopa Prabhu 44766b9ea5a6SRoopa Prabhu cleanup: 44776b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 44788d1c802bSDavid Ahern if (nh->fib6_info) 44798d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 44806b9ea5a6SRoopa Prabhu list_del(&nh->next); 44816b9ea5a6SRoopa Prabhu kfree(nh); 44826b9ea5a6SRoopa Prabhu } 44836b9ea5a6SRoopa Prabhu 44846b9ea5a6SRoopa Prabhu return err; 44856b9ea5a6SRoopa Prabhu } 44866b9ea5a6SRoopa Prabhu 4487333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 4488333c4301SDavid Ahern struct netlink_ext_ack *extack) 44896b9ea5a6SRoopa Prabhu { 44906b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 44916b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 44926b9ea5a6SRoopa Prabhu int remaining; 44936b9ea5a6SRoopa Prabhu int attrlen; 44946b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 44956b9ea5a6SRoopa Prabhu 44966b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 44976b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 44986b9ea5a6SRoopa Prabhu 44996b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 45006b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 45016b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 45026b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 45036b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 45046b9ea5a6SRoopa Prabhu 45056b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 45066b9ea5a6SRoopa Prabhu if (attrlen > 0) { 45076b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 45086b9ea5a6SRoopa Prabhu 45096b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 45106b9ea5a6SRoopa Prabhu if (nla) { 45116b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 45126b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 45136b9ea5a6SRoopa Prabhu } 45146b9ea5a6SRoopa Prabhu } 4515333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 45166b9ea5a6SRoopa Prabhu if (err) 45176b9ea5a6SRoopa Prabhu last_err = err; 45186b9ea5a6SRoopa Prabhu 451951ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 452051ebd318SNicolas Dichtel } 452151ebd318SNicolas Dichtel 452251ebd318SNicolas Dichtel return last_err; 452351ebd318SNicolas Dichtel } 452451ebd318SNicolas Dichtel 4525c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4526c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45271da177e4SLinus Torvalds { 452886872cb5SThomas Graf struct fib6_config cfg; 452986872cb5SThomas Graf int err; 45301da177e4SLinus Torvalds 4531333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 453286872cb5SThomas Graf if (err < 0) 453386872cb5SThomas Graf return err; 453486872cb5SThomas Graf 453551ebd318SNicolas Dichtel if (cfg.fc_mp) 4536333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 45370ae81335SDavid Ahern else { 45380ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 4539333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 45401da177e4SLinus Torvalds } 45410ae81335SDavid Ahern } 45421da177e4SLinus Torvalds 4543c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4544c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45451da177e4SLinus Torvalds { 454686872cb5SThomas Graf struct fib6_config cfg; 454786872cb5SThomas Graf int err; 45481da177e4SLinus Torvalds 4549333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 455086872cb5SThomas Graf if (err < 0) 455186872cb5SThomas Graf return err; 455286872cb5SThomas Graf 455367f69513SDavid Ahern if (cfg.fc_metric == 0) 455467f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 455567f69513SDavid Ahern 455651ebd318SNicolas Dichtel if (cfg.fc_mp) 4557333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 455851ebd318SNicolas Dichtel else 4559acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 45601da177e4SLinus Torvalds } 45611da177e4SLinus Torvalds 45628d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt) 4563339bf98fSThomas Graf { 4564beb1afacSDavid Ahern int nexthop_len = 0; 4565beb1afacSDavid Ahern 456693c2fb25SDavid Ahern if (rt->fib6_nsiblings) { 4567beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 4568beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 4569beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 4570ad1601aeSDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws); 4571beb1afacSDavid Ahern 457293c2fb25SDavid Ahern nexthop_len *= rt->fib6_nsiblings; 4573beb1afacSDavid Ahern } 4574beb1afacSDavid Ahern 4575339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 4576339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 4577339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 4578339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 4579339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 4580339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 4581339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 4582339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 4583339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 45846a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 4585ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 4586c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 458719e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 4588ad1601aeSDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws) 4589beb1afacSDavid Ahern + nexthop_len; 4590beb1afacSDavid Ahern } 4591beb1afacSDavid Ahern 4592d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 45938d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 4594d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 459515e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4596f8cfe2ceSDavid Ahern unsigned int flags) 45971da177e4SLinus Torvalds { 459822d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 459922d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 460022d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 46011da177e4SLinus Torvalds struct nlmsghdr *nlh; 460222d0bd82SXin Long struct rtmsg *rtm; 4603d4ead6b3SDavid Ahern long expires = 0; 46041da177e4SLinus Torvalds 460515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 460638308473SDavid S. Miller if (!nlh) 460726932566SPatrick McHardy return -EMSGSIZE; 46082d7202bfSThomas Graf 460922d0bd82SXin Long if (rt6) { 461022d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 461122d0bd82SXin Long rt6_src = &rt6->rt6i_src; 461222d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 461322d0bd82SXin Long } else { 461422d0bd82SXin Long rt6_dst = &rt->fib6_dst; 461522d0bd82SXin Long rt6_src = &rt->fib6_src; 461622d0bd82SXin Long rt6_flags = rt->fib6_flags; 461722d0bd82SXin Long } 461822d0bd82SXin Long 46192d7202bfSThomas Graf rtm = nlmsg_data(nlh); 46201da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 462122d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 462222d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 46231da177e4SLinus Torvalds rtm->rtm_tos = 0; 462493c2fb25SDavid Ahern if (rt->fib6_table) 462593c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 4626c71099acSThomas Graf else 46279e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 462897f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 4629c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4630c78679e8SDavid S. Miller goto nla_put_failure; 4631e8478e80SDavid Ahern 4632e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 46331da177e4SLinus Torvalds rtm->rtm_flags = 0; 46341da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 463593c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 46361da177e4SLinus Torvalds 463722d0bd82SXin Long if (rt6_flags & RTF_CACHE) 46381da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 46391da177e4SLinus Torvalds 4640d4ead6b3SDavid Ahern if (dest) { 4641d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 4642c78679e8SDavid S. Miller goto nla_put_failure; 46431da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 46441da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 464522d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 4646c78679e8SDavid S. Miller goto nla_put_failure; 46471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 46481da177e4SLinus Torvalds if (src) { 4649930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4650c78679e8SDavid S. Miller goto nla_put_failure; 46511da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4652c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 465322d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 4654c78679e8SDavid S. Miller goto nla_put_failure; 46551da177e4SLinus Torvalds #endif 46567bc570c8SYOSHIFUJI Hideaki if (iif) { 46577bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 465822d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 4659fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 46602cf75070SNikolay Aleksandrov 46617bc570c8SYOSHIFUJI Hideaki if (err == 0) 46627bc570c8SYOSHIFUJI Hideaki return 0; 4663fd61c6baSDavid Ahern if (err < 0) 46647bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 46657bc570c8SYOSHIFUJI Hideaki } else 46667bc570c8SYOSHIFUJI Hideaki #endif 4667c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4668c78679e8SDavid S. Miller goto nla_put_failure; 4669d4ead6b3SDavid Ahern } else if (dest) { 46701da177e4SLinus Torvalds struct in6_addr saddr_buf; 4671d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 4672930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4673c78679e8SDavid S. Miller goto nla_put_failure; 4674c3968a85SDaniel Walter } 4675c3968a85SDaniel Walter 467693c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 4677c3968a85SDaniel Walter struct in6_addr saddr_buf; 467893c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 4679930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4680c78679e8SDavid S. Miller goto nla_put_failure; 46811da177e4SLinus Torvalds } 46822d7202bfSThomas Graf 4683d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 4684d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 46852d7202bfSThomas Graf goto nla_put_failure; 46862d7202bfSThomas Graf 468793c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 4688beb1afacSDavid Ahern goto nla_put_failure; 4689beb1afacSDavid Ahern 4690beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4691beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4692beb1afacSDavid Ahern */ 469322d0bd82SXin Long if (rt6) { 469422d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 469522d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 469622d0bd82SXin Long goto nla_put_failure; 469722d0bd82SXin Long 469822d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 469922d0bd82SXin Long goto nla_put_failure; 470022d0bd82SXin Long } else if (rt->fib6_nsiblings) { 47018d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 4702beb1afacSDavid Ahern struct nlattr *mp; 4703beb1afacSDavid Ahern 4704beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4705beb1afacSDavid Ahern if (!mp) 4706beb1afacSDavid Ahern goto nla_put_failure; 4707beb1afacSDavid Ahern 4708c0a72077SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common, 4709c0a72077SDavid Ahern rt->fib6_nh.fib_nh_weight) < 0) 4710beb1afacSDavid Ahern goto nla_put_failure; 4711beb1afacSDavid Ahern 4712beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 471393c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 4714c0a72077SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common, 4715c0a72077SDavid Ahern sibling->fib6_nh.fib_nh_weight) < 0) 471694f826b8SEric Dumazet goto nla_put_failure; 471794f826b8SEric Dumazet } 47182d7202bfSThomas Graf 4719beb1afacSDavid Ahern nla_nest_end(skb, mp); 4720beb1afacSDavid Ahern } else { 4721c0a72077SDavid Ahern if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common, 4722c0a72077SDavid Ahern &rtm->rtm_flags, false) < 0) 4723c78679e8SDavid S. Miller goto nla_put_failure; 4724beb1afacSDavid Ahern } 47258253947eSLi Wei 472622d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 472714895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 472814895687SDavid Ahern expires -= jiffies; 472914895687SDavid Ahern } 473069cdf8f9SYOSHIFUJI Hideaki 4731d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 4732e3703b3dSThomas Graf goto nla_put_failure; 47331da177e4SLinus Torvalds 473422d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 4735c78ba6d6SLubomir Rintel goto nla_put_failure; 4736c78ba6d6SLubomir Rintel 473719e42e45SRoopa Prabhu 4738053c095aSJohannes Berg nlmsg_end(skb, nlh); 4739053c095aSJohannes Berg return 0; 47402d7202bfSThomas Graf 47412d7202bfSThomas Graf nla_put_failure: 474226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 474326932566SPatrick McHardy return -EMSGSIZE; 47441da177e4SLinus Torvalds } 47451da177e4SLinus Torvalds 474613e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 474713e38901SDavid Ahern const struct net_device *dev) 474813e38901SDavid Ahern { 4749ad1601aeSDavid Ahern if (f6i->fib6_nh.fib_nh_dev == dev) 475013e38901SDavid Ahern return true; 475113e38901SDavid Ahern 475213e38901SDavid Ahern if (f6i->fib6_nsiblings) { 475313e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 475413e38901SDavid Ahern 475513e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 475613e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 4757ad1601aeSDavid Ahern if (sibling->fib6_nh.fib_nh_dev == dev) 475813e38901SDavid Ahern return true; 475913e38901SDavid Ahern } 476013e38901SDavid Ahern } 476113e38901SDavid Ahern 476213e38901SDavid Ahern return false; 476313e38901SDavid Ahern } 476413e38901SDavid Ahern 47658d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg) 47661da177e4SLinus Torvalds { 47671da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 476813e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 476913e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 47701f17e2f2SDavid Ahern struct net *net = arg->net; 47711f17e2f2SDavid Ahern 4772421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 47731f17e2f2SDavid Ahern return 0; 47741da177e4SLinus Torvalds 477513e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 477693c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 4777f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4778f8cfe2ceSDavid Ahern return 1; 4779f8cfe2ceSDavid Ahern } 478013e38901SDavid Ahern if (filter->filter_set) { 478113e38901SDavid Ahern if ((filter->rt_type && rt->fib6_type != filter->rt_type) || 478213e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 478313e38901SDavid Ahern (filter->protocol && rt->fib6_protocol != filter->protocol)) { 478413e38901SDavid Ahern return 1; 478513e38901SDavid Ahern } 478613e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 4787f8cfe2ceSDavid Ahern } 47881da177e4SLinus Torvalds 4789d4ead6b3SDavid Ahern return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, 4790d4ead6b3SDavid Ahern RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, 479113e38901SDavid Ahern arg->cb->nlh->nlmsg_seq, flags); 47921da177e4SLinus Torvalds } 47931da177e4SLinus Torvalds 47940eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 47950eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 47960eff0a27SJakub Kicinski struct nlattr **tb, 47970eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 47980eff0a27SJakub Kicinski { 47990eff0a27SJakub Kicinski struct rtmsg *rtm; 48000eff0a27SJakub Kicinski int i, err; 48010eff0a27SJakub Kicinski 48020eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 48030eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 48040eff0a27SJakub Kicinski "Invalid header for get route request"); 48050eff0a27SJakub Kicinski return -EINVAL; 48060eff0a27SJakub Kicinski } 48070eff0a27SJakub Kicinski 48080eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 48090eff0a27SJakub Kicinski return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, 48100eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 48110eff0a27SJakub Kicinski 48120eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 48130eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 48140eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 48150eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 48160eff0a27SJakub Kicinski rtm->rtm_type) { 48170eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 48180eff0a27SJakub Kicinski return -EINVAL; 48190eff0a27SJakub Kicinski } 48200eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 48210eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 48220eff0a27SJakub Kicinski "Invalid flags for get route request"); 48230eff0a27SJakub Kicinski return -EINVAL; 48240eff0a27SJakub Kicinski } 48250eff0a27SJakub Kicinski 48260eff0a27SJakub Kicinski err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 48270eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 48280eff0a27SJakub Kicinski if (err) 48290eff0a27SJakub Kicinski return err; 48300eff0a27SJakub Kicinski 48310eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 48320eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 48330eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 48340eff0a27SJakub Kicinski return -EINVAL; 48350eff0a27SJakub Kicinski } 48360eff0a27SJakub Kicinski 48370eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 48380eff0a27SJakub Kicinski if (!tb[i]) 48390eff0a27SJakub Kicinski continue; 48400eff0a27SJakub Kicinski 48410eff0a27SJakub Kicinski switch (i) { 48420eff0a27SJakub Kicinski case RTA_SRC: 48430eff0a27SJakub Kicinski case RTA_DST: 48440eff0a27SJakub Kicinski case RTA_IIF: 48450eff0a27SJakub Kicinski case RTA_OIF: 48460eff0a27SJakub Kicinski case RTA_MARK: 48470eff0a27SJakub Kicinski case RTA_UID: 48480eff0a27SJakub Kicinski case RTA_SPORT: 48490eff0a27SJakub Kicinski case RTA_DPORT: 48500eff0a27SJakub Kicinski case RTA_IP_PROTO: 48510eff0a27SJakub Kicinski break; 48520eff0a27SJakub Kicinski default: 48530eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 48540eff0a27SJakub Kicinski return -EINVAL; 48550eff0a27SJakub Kicinski } 48560eff0a27SJakub Kicinski } 48570eff0a27SJakub Kicinski 48580eff0a27SJakub Kicinski return 0; 48590eff0a27SJakub Kicinski } 48600eff0a27SJakub Kicinski 4861c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4862c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 48631da177e4SLinus Torvalds { 48643b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4865ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 486618c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 4867a68886a6SDavid Ahern struct fib6_info *from; 486818c3a61cSRoopa Prabhu struct dst_entry *dst; 48691da177e4SLinus Torvalds struct rt6_info *rt; 4870ab364a6fSThomas Graf struct sk_buff *skb; 4871ab364a6fSThomas Graf struct rtmsg *rtm; 4872744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 487318c3a61cSRoopa Prabhu bool fibmatch; 4874ab364a6fSThomas Graf 48750eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 4876ab364a6fSThomas Graf if (err < 0) 4877ab364a6fSThomas Graf goto errout; 4878ab364a6fSThomas Graf 4879ab364a6fSThomas Graf err = -EINVAL; 488038b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 488138b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 488218c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4883ab364a6fSThomas Graf 4884ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4885ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4886ab364a6fSThomas Graf goto errout; 4887ab364a6fSThomas Graf 48884e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4889ab364a6fSThomas Graf } 4890ab364a6fSThomas Graf 4891ab364a6fSThomas Graf if (tb[RTA_DST]) { 4892ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4893ab364a6fSThomas Graf goto errout; 4894ab364a6fSThomas Graf 48954e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4896ab364a6fSThomas Graf } 4897ab364a6fSThomas Graf 4898ab364a6fSThomas Graf if (tb[RTA_IIF]) 4899ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4900ab364a6fSThomas Graf 4901ab364a6fSThomas Graf if (tb[RTA_OIF]) 490272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4903ab364a6fSThomas Graf 49042e47b291SLorenzo Colitti if (tb[RTA_MARK]) 49052e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 49062e47b291SLorenzo Colitti 4907622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4908622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4909622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4910622ec2c9SLorenzo Colitti else 4911622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4912622ec2c9SLorenzo Colitti 4913eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 4914eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 4915eacb9384SRoopa Prabhu 4916eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 4917eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 4918eacb9384SRoopa Prabhu 4919eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 4920eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 49215e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 49225e1a99eaSHangbin Liu extack); 4923eacb9384SRoopa Prabhu if (err) 4924eacb9384SRoopa Prabhu goto errout; 4925eacb9384SRoopa Prabhu } 4926eacb9384SRoopa Prabhu 4927ab364a6fSThomas Graf if (iif) { 4928ab364a6fSThomas Graf struct net_device *dev; 492972331bc0SShmulik Ladkani int flags = 0; 493072331bc0SShmulik Ladkani 4931121622dbSFlorian Westphal rcu_read_lock(); 4932121622dbSFlorian Westphal 4933121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4934ab364a6fSThomas Graf if (!dev) { 4935121622dbSFlorian Westphal rcu_read_unlock(); 4936ab364a6fSThomas Graf err = -ENODEV; 4937ab364a6fSThomas Graf goto errout; 4938ab364a6fSThomas Graf } 493972331bc0SShmulik Ladkani 494072331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 494172331bc0SShmulik Ladkani 494272331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 494372331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 494472331bc0SShmulik Ladkani 4945b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 4946121622dbSFlorian Westphal 4947121622dbSFlorian Westphal rcu_read_unlock(); 494872331bc0SShmulik Ladkani } else { 494972331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 495072331bc0SShmulik Ladkani 495118c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 495218c3a61cSRoopa Prabhu } 495318c3a61cSRoopa Prabhu 495418c3a61cSRoopa Prabhu 495518c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 495618c3a61cSRoopa Prabhu if (rt->dst.error) { 495718c3a61cSRoopa Prabhu err = rt->dst.error; 495818c3a61cSRoopa Prabhu ip6_rt_put(rt); 495918c3a61cSRoopa Prabhu goto errout; 4960ab364a6fSThomas Graf } 49611da177e4SLinus Torvalds 49629d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 49639d6acb3bSWANG Cong err = rt->dst.error; 49649d6acb3bSWANG Cong ip6_rt_put(rt); 49659d6acb3bSWANG Cong goto errout; 49669d6acb3bSWANG Cong } 49679d6acb3bSWANG Cong 49681da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 496938308473SDavid S. Miller if (!skb) { 497094e187c0SAmerigo Wang ip6_rt_put(rt); 4971ab364a6fSThomas Graf err = -ENOBUFS; 4972ab364a6fSThomas Graf goto errout; 4973ab364a6fSThomas Graf } 49741da177e4SLinus Torvalds 4975d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 4976a68886a6SDavid Ahern 4977a68886a6SDavid Ahern rcu_read_lock(); 4978a68886a6SDavid Ahern from = rcu_dereference(rt->from); 4979a68886a6SDavid Ahern 498018c3a61cSRoopa Prabhu if (fibmatch) 4981a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif, 498218c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 498318c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 498418c3a61cSRoopa Prabhu else 4985a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 4986a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 4987d4ead6b3SDavid Ahern NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 4988d4ead6b3SDavid Ahern 0); 4989a68886a6SDavid Ahern rcu_read_unlock(); 4990a68886a6SDavid Ahern 49911da177e4SLinus Torvalds if (err < 0) { 4992ab364a6fSThomas Graf kfree_skb(skb); 4993ab364a6fSThomas Graf goto errout; 49941da177e4SLinus Torvalds } 49951da177e4SLinus Torvalds 499615e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4997ab364a6fSThomas Graf errout: 49981da177e4SLinus Torvalds return err; 49991da177e4SLinus Torvalds } 50001da177e4SLinus Torvalds 50018d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 500237a1d361SRoopa Prabhu unsigned int nlm_flags) 50031da177e4SLinus Torvalds { 50041da177e4SLinus Torvalds struct sk_buff *skb; 50055578689aSDaniel Lezcano struct net *net = info->nl_net; 5006528c4cebSDenis V. Lunev u32 seq; 5007528c4cebSDenis V. Lunev int err; 50080d51aa80SJamal Hadi Salim 5009528c4cebSDenis V. Lunev err = -ENOBUFS; 501038308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 501186872cb5SThomas Graf 501219e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 501338308473SDavid S. Miller if (!skb) 501421713ebcSThomas Graf goto errout; 50151da177e4SLinus Torvalds 5016d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 5017f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 501826932566SPatrick McHardy if (err < 0) { 501926932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 502026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 502126932566SPatrick McHardy kfree_skb(skb); 502226932566SPatrick McHardy goto errout; 502326932566SPatrick McHardy } 502415e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 50255578689aSDaniel Lezcano info->nlh, gfp_any()); 50261ce85fe4SPablo Neira Ayuso return; 502721713ebcSThomas Graf errout: 502821713ebcSThomas Graf if (err < 0) 50295578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 50301da177e4SLinus Torvalds } 50311da177e4SLinus Torvalds 50328ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 5033351638e7SJiri Pirko unsigned long event, void *ptr) 50348ed67789SDaniel Lezcano { 5035351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5036c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 50378ed67789SDaniel Lezcano 5038242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 5039242d3a49SWANG Cong return NOTIFY_OK; 5040242d3a49SWANG Cong 5041242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 5042ad1601aeSDavid Ahern net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev; 5043d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 50448ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 50458ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5046d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 50478ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 5048d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 50498ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 50508ed67789SDaniel Lezcano #endif 505176da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 505276da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 505376da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 505476da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 505576da0704SWANG Cong */ 505612d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 5057242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 505812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 505912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 5060242d3a49SWANG Cong #endif 50618ed67789SDaniel Lezcano } 50628ed67789SDaniel Lezcano 50638ed67789SDaniel Lezcano return NOTIFY_OK; 50648ed67789SDaniel Lezcano } 50658ed67789SDaniel Lezcano 50661da177e4SLinus Torvalds /* 50671da177e4SLinus Torvalds * /proc 50681da177e4SLinus Torvalds */ 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 50711da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 50721da177e4SLinus Torvalds { 507369ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 50741da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 507569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 507669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 507781eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 507869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 507969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 5080fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 508169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 50821da177e4SLinus Torvalds 50831da177e4SLinus Torvalds return 0; 50841da177e4SLinus Torvalds } 50851da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 50861da177e4SLinus Torvalds 50871da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 50881da177e4SLinus Torvalds 50891da177e4SLinus Torvalds static 5090fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 50911da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 50921da177e4SLinus Torvalds { 5093c486da34SLucian Adrian Grijincu struct net *net; 5094c486da34SLucian Adrian Grijincu int delay; 5095f0fb9b28SAditya Pakki int ret; 5096c486da34SLucian Adrian Grijincu if (!write) 5097c486da34SLucian Adrian Grijincu return -EINVAL; 5098c486da34SLucian Adrian Grijincu 5099c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 5100c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 5101f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5102f0fb9b28SAditya Pakki if (ret) 5103f0fb9b28SAditya Pakki return ret; 5104f0fb9b28SAditya Pakki 51052ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 51061da177e4SLinus Torvalds return 0; 51071da177e4SLinus Torvalds } 51081da177e4SLinus Torvalds 51097c6bb7d2SDavid Ahern static int zero; 51107c6bb7d2SDavid Ahern static int one = 1; 51117c6bb7d2SDavid Ahern 5112ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 51131da177e4SLinus Torvalds { 51141da177e4SLinus Torvalds .procname = "flush", 51154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 51161da177e4SLinus Torvalds .maxlen = sizeof(int), 511789c8b3a1SDave Jones .mode = 0200, 51186d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 51191da177e4SLinus Torvalds }, 51201da177e4SLinus Torvalds { 51211da177e4SLinus Torvalds .procname = "gc_thresh", 51229a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 51231da177e4SLinus Torvalds .maxlen = sizeof(int), 51241da177e4SLinus Torvalds .mode = 0644, 51256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 51261da177e4SLinus Torvalds }, 51271da177e4SLinus Torvalds { 51281da177e4SLinus Torvalds .procname = "max_size", 51294990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 51301da177e4SLinus Torvalds .maxlen = sizeof(int), 51311da177e4SLinus Torvalds .mode = 0644, 51326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 51331da177e4SLinus Torvalds }, 51341da177e4SLinus Torvalds { 51351da177e4SLinus Torvalds .procname = "gc_min_interval", 51364990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 51371da177e4SLinus Torvalds .maxlen = sizeof(int), 51381da177e4SLinus Torvalds .mode = 0644, 51396d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 51401da177e4SLinus Torvalds }, 51411da177e4SLinus Torvalds { 51421da177e4SLinus Torvalds .procname = "gc_timeout", 51434990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 51441da177e4SLinus Torvalds .maxlen = sizeof(int), 51451da177e4SLinus Torvalds .mode = 0644, 51466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 51471da177e4SLinus Torvalds }, 51481da177e4SLinus Torvalds { 51491da177e4SLinus Torvalds .procname = "gc_interval", 51504990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 51511da177e4SLinus Torvalds .maxlen = sizeof(int), 51521da177e4SLinus Torvalds .mode = 0644, 51536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 51541da177e4SLinus Torvalds }, 51551da177e4SLinus Torvalds { 51561da177e4SLinus Torvalds .procname = "gc_elasticity", 51574990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 51581da177e4SLinus Torvalds .maxlen = sizeof(int), 51591da177e4SLinus Torvalds .mode = 0644, 5160f3d3f616SMin Zhang .proc_handler = proc_dointvec, 51611da177e4SLinus Torvalds }, 51621da177e4SLinus Torvalds { 51631da177e4SLinus Torvalds .procname = "mtu_expires", 51644990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 51651da177e4SLinus Torvalds .maxlen = sizeof(int), 51661da177e4SLinus Torvalds .mode = 0644, 51676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 51681da177e4SLinus Torvalds }, 51691da177e4SLinus Torvalds { 51701da177e4SLinus Torvalds .procname = "min_adv_mss", 51714990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 51721da177e4SLinus Torvalds .maxlen = sizeof(int), 51731da177e4SLinus Torvalds .mode = 0644, 5174f3d3f616SMin Zhang .proc_handler = proc_dointvec, 51751da177e4SLinus Torvalds }, 51761da177e4SLinus Torvalds { 51771da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 51784990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 51791da177e4SLinus Torvalds .maxlen = sizeof(int), 51801da177e4SLinus Torvalds .mode = 0644, 51816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 51821da177e4SLinus Torvalds }, 51837c6bb7d2SDavid Ahern { 51847c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 51857c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 51867c6bb7d2SDavid Ahern .maxlen = sizeof(int), 51877c6bb7d2SDavid Ahern .mode = 0644, 51887c6bb7d2SDavid Ahern .proc_handler = proc_dointvec, 51897c6bb7d2SDavid Ahern .extra1 = &zero, 51907c6bb7d2SDavid Ahern .extra2 = &one, 51917c6bb7d2SDavid Ahern }, 5192f8572d8fSEric W. Biederman { } 51931da177e4SLinus Torvalds }; 51941da177e4SLinus Torvalds 51952c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 5196760f2d01SDaniel Lezcano { 5197760f2d01SDaniel Lezcano struct ctl_table *table; 5198760f2d01SDaniel Lezcano 5199760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 5200760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 5201760f2d01SDaniel Lezcano GFP_KERNEL); 52025ee09105SYOSHIFUJI Hideaki 52035ee09105SYOSHIFUJI Hideaki if (table) { 52045ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 5205c486da34SLucian Adrian Grijincu table[0].extra1 = net; 520686393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 52075ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 52085ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 52095ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 52105ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 52115ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 52125ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 52135ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 52149c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 52157c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 5216464dc801SEric W. Biederman 5217464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 5218464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 5219464dc801SEric W. Biederman table[0].procname = NULL; 52205ee09105SYOSHIFUJI Hideaki } 52215ee09105SYOSHIFUJI Hideaki 5222760f2d01SDaniel Lezcano return table; 5223760f2d01SDaniel Lezcano } 52241da177e4SLinus Torvalds #endif 52251da177e4SLinus Torvalds 52262c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 5227cdb18761SDaniel Lezcano { 5228633d424bSPavel Emelyanov int ret = -ENOMEM; 52298ed67789SDaniel Lezcano 523086393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 523186393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 5232f2fc6a54SBenjamin Thery 5233fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 5234fc66f95cSEric Dumazet goto out_ip6_dst_ops; 5235fc66f95cSEric Dumazet 5236421842edSDavid Ahern net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template, 5237421842edSDavid Ahern sizeof(*net->ipv6.fib6_null_entry), 5238421842edSDavid Ahern GFP_KERNEL); 5239421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 5240421842edSDavid Ahern goto out_ip6_dst_entries; 5241421842edSDavid Ahern 52428ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 52438ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 52448ed67789SDaniel Lezcano GFP_KERNEL); 52458ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 5246421842edSDavid Ahern goto out_fib6_null_entry; 5247d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 524862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 524962fa8a84SDavid S. Miller ip6_template_metrics, true); 52508ed67789SDaniel Lezcano 52518ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5252feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 52538ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 52548ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 52558ed67789SDaniel Lezcano GFP_KERNEL); 525668fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 525768fffc67SPeter Zijlstra goto out_ip6_null_entry; 5258d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 525962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 526062fa8a84SDavid S. Miller ip6_template_metrics, true); 52618ed67789SDaniel Lezcano 52628ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 52638ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 52648ed67789SDaniel Lezcano GFP_KERNEL); 526568fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 526668fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 5267d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 526862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 526962fa8a84SDavid S. Miller ip6_template_metrics, true); 52708ed67789SDaniel Lezcano #endif 52718ed67789SDaniel Lezcano 5272b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 5273b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 5274b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 5275b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 5276b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 5277b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 5278b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 5279b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 52807c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 5281b339a47cSPeter Zijlstra 52826891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 52836891a346SBenjamin Thery 52848ed67789SDaniel Lezcano ret = 0; 52858ed67789SDaniel Lezcano out: 52868ed67789SDaniel Lezcano return ret; 5287f2fc6a54SBenjamin Thery 528868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 528968fffc67SPeter Zijlstra out_ip6_prohibit_entry: 529068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 529168fffc67SPeter Zijlstra out_ip6_null_entry: 529268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 529368fffc67SPeter Zijlstra #endif 5294421842edSDavid Ahern out_fib6_null_entry: 5295421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 5296fc66f95cSEric Dumazet out_ip6_dst_entries: 5297fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5298f2fc6a54SBenjamin Thery out_ip6_dst_ops: 5299f2fc6a54SBenjamin Thery goto out; 5300cdb18761SDaniel Lezcano } 5301cdb18761SDaniel Lezcano 53022c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 5303cdb18761SDaniel Lezcano { 5304421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 53058ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 53068ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 53078ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 53088ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 53098ed67789SDaniel Lezcano #endif 531041bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5311cdb18761SDaniel Lezcano } 5312cdb18761SDaniel Lezcano 5313d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 5314d189634eSThomas Graf { 5315d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5316c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 5317c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 53183617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 53193617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 5320d189634eSThomas Graf #endif 5321d189634eSThomas Graf return 0; 5322d189634eSThomas Graf } 5323d189634eSThomas Graf 5324d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 5325d189634eSThomas Graf { 5326d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5327ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 5328ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 5329d189634eSThomas Graf #endif 5330d189634eSThomas Graf } 5331d189634eSThomas Graf 5332cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 5333cdb18761SDaniel Lezcano .init = ip6_route_net_init, 5334cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 5335cdb18761SDaniel Lezcano }; 5336cdb18761SDaniel Lezcano 5337c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 5338c3426b47SDavid S. Miller { 5339c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 5340c3426b47SDavid S. Miller 5341c3426b47SDavid S. Miller if (!bp) 5342c3426b47SDavid S. Miller return -ENOMEM; 5343c3426b47SDavid S. Miller inet_peer_base_init(bp); 5344c3426b47SDavid S. Miller net->ipv6.peers = bp; 5345c3426b47SDavid S. Miller return 0; 5346c3426b47SDavid S. Miller } 5347c3426b47SDavid S. Miller 5348c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5349c3426b47SDavid S. Miller { 5350c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5351c3426b47SDavid S. Miller 5352c3426b47SDavid S. Miller net->ipv6.peers = NULL; 535356a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5354c3426b47SDavid S. Miller kfree(bp); 5355c3426b47SDavid S. Miller } 5356c3426b47SDavid S. Miller 53572b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5358c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5359c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 5360c3426b47SDavid S. Miller }; 5361c3426b47SDavid S. Miller 5362d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 5363d189634eSThomas Graf .init = ip6_route_net_init_late, 5364d189634eSThomas Graf .exit = ip6_route_net_exit_late, 5365d189634eSThomas Graf }; 5366d189634eSThomas Graf 53678ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 53688ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 5369242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 53708ed67789SDaniel Lezcano }; 53718ed67789SDaniel Lezcano 53722f460933SWANG Cong void __init ip6_route_init_special_entries(void) 53732f460933SWANG Cong { 53742f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 53752f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 53762f460933SWANG Cong * manually for init_net */ 5377ad1601aeSDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev; 53782f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 53792f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53802f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 53812f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 53822f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53832f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 53842f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 53852f460933SWANG Cong #endif 53862f460933SWANG Cong } 53872f460933SWANG Cong 5388433d49c3SDaniel Lezcano int __init ip6_route_init(void) 53891da177e4SLinus Torvalds { 5390433d49c3SDaniel Lezcano int ret; 53918d0b94afSMartin KaFai Lau int cpu; 5392433d49c3SDaniel Lezcano 53939a7ec3a9SDaniel Lezcano ret = -ENOMEM; 53949a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 53959a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 53969a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 53979a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 5398c19a28e1SFernando Carrijo goto out; 539914e50e57SDavid S. Miller 5400fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 54018ed67789SDaniel Lezcano if (ret) 5402bdb3289fSDaniel Lezcano goto out_kmem_cache; 5403bdb3289fSDaniel Lezcano 5404c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 5405c3426b47SDavid S. Miller if (ret) 5406e8803b6cSDavid S. Miller goto out_dst_entries; 54072a0c451aSThomas Graf 54087e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 54097e52b33bSDavid S. Miller if (ret) 54107e52b33bSDavid S. Miller goto out_register_inetpeer; 5411c3426b47SDavid S. Miller 54125dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 54135dc121e9SArnaud Ebalard 5414e8803b6cSDavid S. Miller ret = fib6_init(); 5415433d49c3SDaniel Lezcano if (ret) 54168ed67789SDaniel Lezcano goto out_register_subsys; 5417433d49c3SDaniel Lezcano 5418433d49c3SDaniel Lezcano ret = xfrm6_init(); 5419433d49c3SDaniel Lezcano if (ret) 5420e8803b6cSDavid S. Miller goto out_fib6_init; 5421c35b7e72SDaniel Lezcano 5422433d49c3SDaniel Lezcano ret = fib6_rules_init(); 5423433d49c3SDaniel Lezcano if (ret) 5424433d49c3SDaniel Lezcano goto xfrm6_init; 54257e5449c2SDaniel Lezcano 5426d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 5427d189634eSThomas Graf if (ret) 5428d189634eSThomas Graf goto fib6_rules_init; 5429d189634eSThomas Graf 543016feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 543116feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 543216feebcfSFlorian Westphal if (ret < 0) 543316feebcfSFlorian Westphal goto out_register_late_subsys; 543416feebcfSFlorian Westphal 543516feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 543616feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 543716feebcfSFlorian Westphal if (ret < 0) 543816feebcfSFlorian Westphal goto out_register_late_subsys; 543916feebcfSFlorian Westphal 544016feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 544116feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 544216feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 544316feebcfSFlorian Westphal if (ret < 0) 5444d189634eSThomas Graf goto out_register_late_subsys; 5445433d49c3SDaniel Lezcano 54468ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 5447cdb18761SDaniel Lezcano if (ret) 5448d189634eSThomas Graf goto out_register_late_subsys; 54498ed67789SDaniel Lezcano 54508d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 54518d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 54528d0b94afSMartin KaFai Lau 54538d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 54548d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 54558d0b94afSMartin KaFai Lau } 54568d0b94afSMartin KaFai Lau 5457433d49c3SDaniel Lezcano out: 5458433d49c3SDaniel Lezcano return ret; 5459433d49c3SDaniel Lezcano 5460d189634eSThomas Graf out_register_late_subsys: 546116feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 5462d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5463433d49c3SDaniel Lezcano fib6_rules_init: 5464433d49c3SDaniel Lezcano fib6_rules_cleanup(); 5465433d49c3SDaniel Lezcano xfrm6_init: 5466433d49c3SDaniel Lezcano xfrm6_fini(); 54672a0c451aSThomas Graf out_fib6_init: 54682a0c451aSThomas Graf fib6_gc_cleanup(); 54698ed67789SDaniel Lezcano out_register_subsys: 54708ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 54717e52b33bSDavid S. Miller out_register_inetpeer: 54727e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 5473fc66f95cSEric Dumazet out_dst_entries: 5474fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 5475433d49c3SDaniel Lezcano out_kmem_cache: 5476f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 5477433d49c3SDaniel Lezcano goto out; 54781da177e4SLinus Torvalds } 54791da177e4SLinus Torvalds 54801da177e4SLinus Torvalds void ip6_route_cleanup(void) 54811da177e4SLinus Torvalds { 54828ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 5483d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5484101367c2SThomas Graf fib6_rules_cleanup(); 54851da177e4SLinus Torvalds xfrm6_fini(); 54861da177e4SLinus Torvalds fib6_gc_cleanup(); 5487c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 54888ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 548941bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 5490f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 54911da177e4SLinus Torvalds } 5492