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> 623c618c1dSDavid Ahern #include <net/rtnh.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); 1137e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 114510e2cedSWei Wang const struct in6_addr *daddr, 115510e2cedSWei Wang const 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, 299f05713e0SEric Dumazet .fib6_ref = REFCOUNT_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 3830e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt->from, NULL); 38493531c67SDavid Ahern fib6_info_release(from); 385b3419363SDavid S. Miller } 386b3419363SDavid S. Miller 3871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3881da177e4SLinus Torvalds int how) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3911da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3925a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 393c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3941da177e4SLinus Torvalds 395e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 396e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 39738308473SDavid S. Miller if (loopback_idev) { 3981da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3991da177e4SLinus Torvalds in6_dev_put(idev); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds } 40297cac082SDavid S. Miller } 4031da177e4SLinus Torvalds 4045973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4055973fb1eSMartin KaFai Lau { 4065973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4075973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4085973fb1eSMartin KaFai Lau else 4095973fb1eSMartin KaFai Lau return false; 4105973fb1eSMartin KaFai Lau } 4115973fb1eSMartin KaFai Lau 412a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4131da177e4SLinus Torvalds { 414a68886a6SDavid Ahern struct fib6_info *from; 415a68886a6SDavid Ahern 416a68886a6SDavid Ahern from = rcu_dereference(rt->from); 417a68886a6SDavid Ahern 4181716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4191716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 420a50feda5SEric Dumazet return true; 421a68886a6SDavid Ahern } else if (from) { 4221e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 423a68886a6SDavid Ahern fib6_check_expired(from); 4241716a961SGao feng } 425a50feda5SEric Dumazet return false; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 428b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res, 429b1d40991SDavid Ahern struct flowi6 *fl6, int oif, bool have_oif_match, 430b1d40991SDavid Ahern const struct sk_buff *skb, int strict) 43151ebd318SNicolas Dichtel { 4328d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 433b1d40991SDavid Ahern struct fib6_info *match = res->f6i; 434b1d40991SDavid Ahern 435b1d40991SDavid Ahern if (!match->fib6_nsiblings || have_oif_match) 436b1d40991SDavid Ahern goto out; 43751ebd318SNicolas Dichtel 438b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 439b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 440b673d6ccSJakub Sitnicki */ 441b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 442b4bac172SDavid Ahern fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); 443b673d6ccSJakub Sitnicki 444ad1601aeSDavid Ahern if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_nh_upper_bound)) 445b1d40991SDavid Ahern goto out; 446bbfcd776SIdo Schimmel 44793c2fb25SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, 44893c2fb25SDavid Ahern fib6_siblings) { 449702cea56SDavid Ahern const struct fib6_nh *nh = &sibling->fib6_nh; 4505e670d84SDavid Ahern int nh_upper_bound; 4515e670d84SDavid Ahern 452702cea56SDavid Ahern nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); 4535e670d84SDavid Ahern if (fl6->mp_hash > nh_upper_bound) 4543d709f69SIdo Schimmel continue; 455702cea56SDavid Ahern if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) 45652bd4c0cSNicolas Dichtel break; 45751ebd318SNicolas Dichtel match = sibling; 45851ebd318SNicolas Dichtel break; 45951ebd318SNicolas Dichtel } 4603d709f69SIdo Schimmel 461b1d40991SDavid Ahern out: 462b1d40991SDavid Ahern res->f6i = match; 463b1d40991SDavid Ahern res->nh = &match->fib6_nh; 46451ebd318SNicolas Dichtel } 46551ebd318SNicolas Dichtel 4661da177e4SLinus Torvalds /* 46766f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds 4700c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, 4710c59d006SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4720c59d006SDavid Ahern { 4730c59d006SDavid Ahern const struct net_device *dev; 4740c59d006SDavid Ahern 4750c59d006SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 4760c59d006SDavid Ahern return false; 4770c59d006SDavid Ahern 4780c59d006SDavid Ahern dev = nh->fib_nh_dev; 4790c59d006SDavid Ahern if (oif) { 4800c59d006SDavid Ahern if (dev->ifindex == oif) 4810c59d006SDavid Ahern return true; 4820c59d006SDavid Ahern } else { 4830c59d006SDavid Ahern if (ipv6_chk_addr(net, saddr, dev, 4840c59d006SDavid Ahern flags & RT6_LOOKUP_F_IFACE)) 4850c59d006SDavid Ahern return true; 4860c59d006SDavid Ahern } 4870c59d006SDavid Ahern 4880c59d006SDavid Ahern return false; 4890c59d006SDavid Ahern } 4900c59d006SDavid Ahern 49175ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res, 49275ef7389SDavid Ahern const struct in6_addr *saddr, int oif, int flags) 4931da177e4SLinus Torvalds { 49475ef7389SDavid Ahern struct fib6_info *f6i = res->f6i; 49575ef7389SDavid Ahern struct fib6_info *spf6i; 49675ef7389SDavid Ahern struct fib6_nh *nh; 4971da177e4SLinus Torvalds 49875ef7389SDavid Ahern if (!oif && ipv6_addr_any(saddr)) { 49975ef7389SDavid Ahern nh = &f6i->fib6_nh; 5007d21fec9SDavid Ahern if (!(nh->fib_nh_flags & RTNH_F_DEAD)) 5017d21fec9SDavid Ahern goto out; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 50475ef7389SDavid Ahern for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { 50575ef7389SDavid Ahern nh = &spf6i->fib6_nh; 50675ef7389SDavid Ahern if (__rt6_device_match(net, nh, saddr, oif, flags)) { 50775ef7389SDavid Ahern res->f6i = spf6i; 5087d21fec9SDavid Ahern goto out; 50975ef7389SDavid Ahern } 51075ef7389SDavid Ahern } 5111da177e4SLinus Torvalds 51275ef7389SDavid Ahern if (oif && flags & RT6_LOOKUP_F_IFACE) { 51375ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5147d21fec9SDavid Ahern nh = &res->f6i->fib6_nh; 5157d21fec9SDavid Ahern goto out; 51675ef7389SDavid Ahern } 51775ef7389SDavid Ahern 5187d21fec9SDavid Ahern nh = &f6i->fib6_nh; 5197d21fec9SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) { 52075ef7389SDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 5217d21fec9SDavid Ahern nh = &res->f6i->fib6_nh; 52275ef7389SDavid Ahern } 5237d21fec9SDavid Ahern out: 5247d21fec9SDavid Ahern res->nh = nh; 5257d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 5267d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 52927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 530c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 531c2f17e82SHannes Frederic Sowa struct work_struct work; 532c2f17e82SHannes Frederic Sowa struct in6_addr target; 533c2f17e82SHannes Frederic Sowa struct net_device *dev; 534c2f17e82SHannes Frederic Sowa }; 535c2f17e82SHannes Frederic Sowa 536c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 537c2f17e82SHannes Frederic Sowa { 538c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 539c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 540c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 541c2f17e82SHannes Frederic Sowa 542c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 543adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 544c2f17e82SHannes Frederic Sowa dev_put(work->dev); 545662f5533SMichael Büsch kfree(work); 546c2f17e82SHannes Frederic Sowa } 547c2f17e82SHannes Frederic Sowa 548cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh) 54927097255SYOSHIFUJI Hideaki { 550f547fac6SSabrina Dubroca struct __rt6_probe_work *work = NULL; 5515e670d84SDavid Ahern const struct in6_addr *nh_gw; 552f2c31e32SEric Dumazet struct neighbour *neigh; 5535e670d84SDavid Ahern struct net_device *dev; 554f547fac6SSabrina Dubroca struct inet6_dev *idev; 5555e670d84SDavid Ahern 55627097255SYOSHIFUJI Hideaki /* 55727097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 55827097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 55927097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56027097255SYOSHIFUJI Hideaki * 56127097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56227097255SYOSHIFUJI Hideaki * to no more than one per minute. 56327097255SYOSHIFUJI Hideaki */ 564cc3a86c8SDavid Ahern if (fib6_nh->fib_nh_gw_family) 565fdd6681dSAmerigo Wang return; 5665e670d84SDavid Ahern 567cc3a86c8SDavid Ahern nh_gw = &fib6_nh->fib_nh_gw6; 568cc3a86c8SDavid Ahern dev = fib6_nh->fib_nh_dev; 5692152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 570f547fac6SSabrina Dubroca idev = __in6_dev_get(dev); 5715e670d84SDavid Ahern neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5738d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5748d6c31bfSMartin KaFai Lau goto out; 5758d6c31bfSMartin KaFai Lau 5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 577990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 578990edb42SMartin KaFai Lau time_after(jiffies, 579dcd1f572SDavid Ahern neigh->updated + idev->cnf.rtr_probe_interval)) { 580c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 581990edb42SMartin KaFai Lau if (work) 5827e980569SJiri Benc __neigh_set_probe_once(neigh); 583990edb42SMartin KaFai Lau } 584c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 585cc3a86c8SDavid Ahern } else if (time_after(jiffies, fib6_nh->last_probe + 586f547fac6SSabrina Dubroca idev->cnf.rtr_probe_interval)) { 587990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 588990edb42SMartin KaFai Lau } 589c2f17e82SHannes Frederic Sowa 590c2f17e82SHannes Frederic Sowa if (work) { 591cc3a86c8SDavid Ahern fib6_nh->last_probe = jiffies; 592c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 5935e670d84SDavid Ahern work->target = *nh_gw; 5945e670d84SDavid Ahern dev_hold(dev); 5955e670d84SDavid Ahern work->dev = dev; 596c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 597c2f17e82SHannes Frederic Sowa } 598990edb42SMartin KaFai Lau 5998d6c31bfSMartin KaFai Lau out: 6002152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 601f2c31e32SEric Dumazet } 60227097255SYOSHIFUJI Hideaki #else 603cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh) 60427097255SYOSHIFUJI Hideaki { 60527097255SYOSHIFUJI Hideaki } 60627097255SYOSHIFUJI Hideaki #endif 60727097255SYOSHIFUJI Hideaki 6081da177e4SLinus Torvalds /* 609554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6101da177e4SLinus Torvalds */ 6111ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) 6121da177e4SLinus Torvalds { 613afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 6145e670d84SDavid Ahern struct neighbour *neigh; 615f2c31e32SEric Dumazet 616145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 6171ba9a895SDavid Ahern neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, 6181ba9a895SDavid Ahern &fib6_nh->fib_nh_gw6); 619145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 620145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 621554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 622afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 623398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 624a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 625afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6267e980569SJiri Benc else 6277e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 628398bcbebSYOSHIFUJI Hideaki #endif 629145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 630afc154e9SHannes Frederic Sowa } else { 631afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6327e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 633a5a81f0bSPaul Marks } 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 636a5a81f0bSPaul Marks return ret; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 639702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, 640702cea56SDavid Ahern int strict) 641554cfb7eSYOSHIFUJI Hideaki { 6426e1809a5SDavid Ahern int m = 0; 6434d0c5911SYOSHIFUJI Hideaki 6446e1809a5SDavid Ahern if (!oif || nh->fib_nh_dev->ifindex == oif) 6456e1809a5SDavid Ahern m = 2; 6466e1809a5SDavid Ahern 64777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 648afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 649ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 650702cea56SDavid Ahern m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; 651ebacaaa0SYOSHIFUJI Hideaki #endif 6521ba9a895SDavid Ahern if ((strict & RT6_LOOKUP_F_REACHABLE) && 653702cea56SDavid Ahern !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { 6541ba9a895SDavid Ahern int n = rt6_check_neigh(nh); 655afc154e9SHannes Frederic Sowa if (n < 0) 656afc154e9SHannes Frederic Sowa return n; 657afc154e9SHannes Frederic Sowa } 658554cfb7eSYOSHIFUJI Hideaki return m; 659554cfb7eSYOSHIFUJI Hideaki } 660554cfb7eSYOSHIFUJI Hideaki 66128679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags, 66228679ed1SDavid Ahern int oif, int strict, int *mpri, bool *do_rr) 663554cfb7eSYOSHIFUJI Hideaki { 664afc154e9SHannes Frederic Sowa bool match_do_rr = false; 66528679ed1SDavid Ahern bool rc = false; 66628679ed1SDavid Ahern int m; 66735103d11SAndy Gospodarek 66828679ed1SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD) 6698067bb8cSIdo Schimmel goto out; 6708067bb8cSIdo Schimmel 67128679ed1SDavid Ahern if (ip6_ignore_linkdown(nh->fib_nh_dev) && 67228679ed1SDavid Ahern nh->fib_nh_flags & RTNH_F_LINKDOWN && 673d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 67435103d11SAndy Gospodarek goto out; 675554cfb7eSYOSHIFUJI Hideaki 67628679ed1SDavid Ahern m = rt6_score_route(nh, fib6_flags, oif, strict); 6777e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 678afc154e9SHannes Frederic Sowa match_do_rr = true; 679afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6807e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 681f11e6659SDavid S. Miller goto out; 6821da177e4SLinus Torvalds } 683f11e6659SDavid S. Miller 684afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 68528679ed1SDavid Ahern rt6_probe(nh); 686afc154e9SHannes Frederic Sowa 6877e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 688afc154e9SHannes Frederic Sowa if (m > *mpri) { 689afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 690afc154e9SHannes Frederic Sowa *mpri = m; 69128679ed1SDavid Ahern rc = true; 692afc154e9SHannes Frederic Sowa } 693f11e6659SDavid S. Miller out: 69428679ed1SDavid Ahern return rc; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 697b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start, 69830c15f03SDavid Ahern struct fib6_info *nomatch, u32 metric, 699b7bc4b6aSDavid Ahern struct fib6_result *res, struct fib6_info **cont, 70030c15f03SDavid Ahern int oif, int strict, bool *do_rr, int *mpri) 70130c15f03SDavid Ahern { 702b7bc4b6aSDavid Ahern struct fib6_info *f6i; 70330c15f03SDavid Ahern 704b7bc4b6aSDavid Ahern for (f6i = f6i_start; 705b7bc4b6aSDavid Ahern f6i && f6i != nomatch; 706b7bc4b6aSDavid Ahern f6i = rcu_dereference(f6i->fib6_next)) { 70730c15f03SDavid Ahern struct fib6_nh *nh; 70830c15f03SDavid Ahern 709b7bc4b6aSDavid Ahern if (cont && f6i->fib6_metric != metric) { 710b7bc4b6aSDavid Ahern *cont = f6i; 71130c15f03SDavid Ahern return; 71230c15f03SDavid Ahern } 71330c15f03SDavid Ahern 714b7bc4b6aSDavid Ahern if (fib6_check_expired(f6i)) 71530c15f03SDavid Ahern continue; 71630c15f03SDavid Ahern 717b7bc4b6aSDavid Ahern nh = &f6i->fib6_nh; 718b7bc4b6aSDavid Ahern if (find_match(nh, f6i->fib6_flags, oif, strict, mpri, do_rr)) { 719b7bc4b6aSDavid Ahern res->f6i = f6i; 720b7bc4b6aSDavid Ahern res->nh = nh; 7217d21fec9SDavid Ahern res->fib6_flags = f6i->fib6_flags; 7227d21fec9SDavid Ahern res->fib6_type = f6i->fib6_type; 723b7bc4b6aSDavid Ahern } 72430c15f03SDavid Ahern } 72530c15f03SDavid Ahern } 72630c15f03SDavid Ahern 727b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, 728b7bc4b6aSDavid Ahern struct fib6_info *rr_head, int oif, int strict, 729b7bc4b6aSDavid Ahern bool *do_rr, struct fib6_result *res) 730f11e6659SDavid S. Miller { 731b7bc4b6aSDavid Ahern u32 metric = rr_head->fib6_metric; 732b7bc4b6aSDavid Ahern struct fib6_info *cont = NULL; 733f11e6659SDavid S. Miller int mpri = -1; 734f11e6659SDavid S. Miller 735b7bc4b6aSDavid Ahern __find_rr_leaf(rr_head, NULL, metric, res, &cont, 73630c15f03SDavid Ahern oif, strict, do_rr, &mpri); 7379fbdcfafSSteffen Klassert 738b7bc4b6aSDavid Ahern __find_rr_leaf(leaf, rr_head, metric, res, &cont, 73930c15f03SDavid Ahern oif, strict, do_rr, &mpri); 7409fbdcfafSSteffen Klassert 741b7bc4b6aSDavid Ahern if (res->f6i || !cont) 742b7bc4b6aSDavid Ahern return; 7439fbdcfafSSteffen Klassert 744b7bc4b6aSDavid Ahern __find_rr_leaf(cont, NULL, metric, res, NULL, 74530c15f03SDavid Ahern oif, strict, do_rr, &mpri); 746f11e6659SDavid S. Miller } 747f11e6659SDavid S. Miller 748b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif, 749b7bc4b6aSDavid Ahern struct fib6_result *res, int strict) 750f11e6659SDavid S. Miller { 7518d1c802bSDavid Ahern struct fib6_info *leaf = rcu_dereference(fn->leaf); 752b7bc4b6aSDavid Ahern struct fib6_info *rt0; 753afc154e9SHannes Frederic Sowa bool do_rr = false; 75417ecf590SWei Wang int key_plen; 755f11e6659SDavid S. Miller 756b7bc4b6aSDavid Ahern /* make sure this function or its helpers sets f6i */ 757b7bc4b6aSDavid Ahern res->f6i = NULL; 758b7bc4b6aSDavid Ahern 759421842edSDavid Ahern if (!leaf || leaf == net->ipv6.fib6_null_entry) 760b7bc4b6aSDavid Ahern goto out; 7618d1040e8SWei Wang 76266f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 763f11e6659SDavid S. Miller if (!rt0) 76466f5d6ceSWei Wang rt0 = leaf; 765f11e6659SDavid S. Miller 76617ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 76717ecf590SWei Wang * and fn->leaf does not points to its child's leaf 76817ecf590SWei Wang * (This might happen if all routes under fn are deleted from 76917ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 77017ecf590SWei Wang */ 77193c2fb25SDavid Ahern key_plen = rt0->fib6_dst.plen; 77217ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 77393c2fb25SDavid Ahern if (rt0->fib6_src.plen) 77493c2fb25SDavid Ahern key_plen = rt0->fib6_src.plen; 77517ecf590SWei Wang #endif 77617ecf590SWei Wang if (fn->fn_bit != key_plen) 777b7bc4b6aSDavid Ahern goto out; 77817ecf590SWei Wang 779b7bc4b6aSDavid Ahern find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); 780afc154e9SHannes Frederic Sowa if (do_rr) { 7818fb11a9aSDavid Ahern struct fib6_info *next = rcu_dereference(rt0->fib6_next); 782f11e6659SDavid S. Miller 783554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 78493c2fb25SDavid Ahern if (!next || next->fib6_metric != rt0->fib6_metric) 7858d1040e8SWei Wang next = leaf; 786f11e6659SDavid S. Miller 78766f5d6ceSWei Wang if (next != rt0) { 78893c2fb25SDavid Ahern spin_lock_bh(&leaf->fib6_table->tb6_lock); 78966f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 79093c2fb25SDavid Ahern if (next->fib6_node) 79166f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 79293c2fb25SDavid Ahern spin_unlock_bh(&leaf->fib6_table->tb6_lock); 79366f5d6ceSWei Wang } 794554cfb7eSYOSHIFUJI Hideaki } 795554cfb7eSYOSHIFUJI Hideaki 796b7bc4b6aSDavid Ahern out: 797b7bc4b6aSDavid Ahern if (!res->f6i) { 798b7bc4b6aSDavid Ahern res->f6i = net->ipv6.fib6_null_entry; 799b7bc4b6aSDavid Ahern res->nh = &res->f6i->fib6_nh; 8007d21fec9SDavid Ahern res->fib6_flags = res->f6i->fib6_flags; 8017d21fec9SDavid Ahern res->fib6_type = res->f6i->fib6_type; 802b7bc4b6aSDavid Ahern } 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 80585bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) 8068b9df265SMartin KaFai Lau { 80785bd05deSDavid Ahern return (res->f6i->fib6_flags & RTF_NONEXTHOP) || 80885bd05deSDavid Ahern res->nh->fib_nh_gw_family; 8098b9df265SMartin KaFai Lau } 8108b9df265SMartin KaFai Lau 81170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 81270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 813b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 81470ceb4f5SYOSHIFUJI Hideaki { 815c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 81670ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 81770ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 81870ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 8194bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 8208d1c802bSDavid Ahern struct fib6_info *rt; 82170ceb4f5SYOSHIFUJI Hideaki 82270ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 82370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82470ceb4f5SYOSHIFUJI Hideaki } 82570ceb4f5SYOSHIFUJI Hideaki 82670ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 82770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 82870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 83070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 83270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 83370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83470ceb4f5SYOSHIFUJI Hideaki } 83570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 83670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 83770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83870ceb4f5SYOSHIFUJI Hideaki } 83970ceb4f5SYOSHIFUJI Hideaki } 84070ceb4f5SYOSHIFUJI Hideaki 84170ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 84270ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8433933fc95SJens Rosenboom return -EINVAL; 84470ceb4f5SYOSHIFUJI Hideaki 8454bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 84670ceb4f5SYOSHIFUJI Hideaki 84770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 84870ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 84970ceb4f5SYOSHIFUJI Hideaki else { 85070ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 85170ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 85270ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 85370ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 85470ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 85570ceb4f5SYOSHIFUJI Hideaki } 85670ceb4f5SYOSHIFUJI Hideaki 857f104a567SDuan Jiong if (rinfo->prefix_len == 0) 858afb1d4b5SDavid Ahern rt = rt6_get_dflt_router(net, gwaddr, dev); 859f104a567SDuan Jiong else 860f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 861830218c1SDavid Ahern gwaddr, dev); 86270ceb4f5SYOSHIFUJI Hideaki 86370ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 864afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 86570ceb4f5SYOSHIFUJI Hideaki rt = NULL; 86670ceb4f5SYOSHIFUJI Hideaki } 86770ceb4f5SYOSHIFUJI Hideaki 86870ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 869830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 870830218c1SDavid Ahern dev, pref); 87170ceb4f5SYOSHIFUJI Hideaki else if (rt) 87293c2fb25SDavid Ahern rt->fib6_flags = RTF_ROUTEINFO | 87393c2fb25SDavid Ahern (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 87470ceb4f5SYOSHIFUJI Hideaki 87570ceb4f5SYOSHIFUJI Hideaki if (rt) { 8761716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 87714895687SDavid Ahern fib6_clean_expires(rt); 8781716a961SGao feng else 87914895687SDavid Ahern fib6_set_expires(rt, jiffies + HZ * lifetime); 8801716a961SGao feng 88193531c67SDavid Ahern fib6_info_release(rt); 88270ceb4f5SYOSHIFUJI Hideaki } 88370ceb4f5SYOSHIFUJI Hideaki return 0; 88470ceb4f5SYOSHIFUJI Hideaki } 88570ceb4f5SYOSHIFUJI Hideaki #endif 88670ceb4f5SYOSHIFUJI Hideaki 887ae90d867SDavid Ahern /* 888ae90d867SDavid Ahern * Misc support functions 889ae90d867SDavid Ahern */ 890ae90d867SDavid Ahern 891ae90d867SDavid Ahern /* called with rcu_lock held */ 8920d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) 893ae90d867SDavid Ahern { 8940d161581SDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 895ae90d867SDavid Ahern 8967d21fec9SDavid Ahern if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { 897ae90d867SDavid Ahern /* for copies of local routes, dst->dev needs to be the 898ae90d867SDavid Ahern * device if it is a master device, the master device if 899ae90d867SDavid Ahern * device is enslaved, and the loopback as the default 900ae90d867SDavid Ahern */ 901ae90d867SDavid Ahern if (netif_is_l3_slave(dev) && 9027d21fec9SDavid Ahern !rt6_need_strict(&res->f6i->fib6_dst.addr)) 903ae90d867SDavid Ahern dev = l3mdev_master_dev_rcu(dev); 904ae90d867SDavid Ahern else if (!netif_is_l3_master(dev)) 905ae90d867SDavid Ahern dev = dev_net(dev)->loopback_dev; 906ae90d867SDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 907ae90d867SDavid Ahern * case we want dev returned to be dev 908ae90d867SDavid Ahern */ 909ae90d867SDavid Ahern } 910ae90d867SDavid Ahern 911ae90d867SDavid Ahern return dev; 912ae90d867SDavid Ahern } 913ae90d867SDavid Ahern 9146edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = { 9156edb3c96SDavid Ahern [RTN_UNSPEC] = 0, 9166edb3c96SDavid Ahern [RTN_UNICAST] = 0, 9176edb3c96SDavid Ahern [RTN_LOCAL] = 0, 9186edb3c96SDavid Ahern [RTN_BROADCAST] = 0, 9196edb3c96SDavid Ahern [RTN_ANYCAST] = 0, 9206edb3c96SDavid Ahern [RTN_MULTICAST] = 0, 9216edb3c96SDavid Ahern [RTN_BLACKHOLE] = -EINVAL, 9226edb3c96SDavid Ahern [RTN_UNREACHABLE] = -EHOSTUNREACH, 9236edb3c96SDavid Ahern [RTN_PROHIBIT] = -EACCES, 9246edb3c96SDavid Ahern [RTN_THROW] = -EAGAIN, 9256edb3c96SDavid Ahern [RTN_NAT] = -EINVAL, 9266edb3c96SDavid Ahern [RTN_XRESOLVE] = -EINVAL, 9276edb3c96SDavid Ahern }; 9286edb3c96SDavid Ahern 9296edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type) 9306edb3c96SDavid Ahern { 9316edb3c96SDavid Ahern return fib6_prop[fib6_type]; 9326edb3c96SDavid Ahern } 9336edb3c96SDavid Ahern 9348d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt) 9353b6761d1SDavid Ahern { 9363b6761d1SDavid Ahern unsigned short flags = 0; 9373b6761d1SDavid Ahern 9383b6761d1SDavid Ahern if (rt->dst_nocount) 9393b6761d1SDavid Ahern flags |= DST_NOCOUNT; 9403b6761d1SDavid Ahern if (rt->dst_nopolicy) 9413b6761d1SDavid Ahern flags |= DST_NOPOLICY; 9423b6761d1SDavid Ahern if (rt->dst_host) 9433b6761d1SDavid Ahern flags |= DST_HOST; 9443b6761d1SDavid Ahern 9453b6761d1SDavid Ahern return flags; 9463b6761d1SDavid Ahern } 9473b6761d1SDavid Ahern 9487d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) 9496edb3c96SDavid Ahern { 9507d21fec9SDavid Ahern rt->dst.error = ip6_rt_type_to_error(fib6_type); 9516edb3c96SDavid Ahern 9527d21fec9SDavid Ahern switch (fib6_type) { 9536edb3c96SDavid Ahern case RTN_BLACKHOLE: 9546edb3c96SDavid Ahern rt->dst.output = dst_discard_out; 9556edb3c96SDavid Ahern rt->dst.input = dst_discard; 9566edb3c96SDavid Ahern break; 9576edb3c96SDavid Ahern case RTN_PROHIBIT: 9586edb3c96SDavid Ahern rt->dst.output = ip6_pkt_prohibit_out; 9596edb3c96SDavid Ahern rt->dst.input = ip6_pkt_prohibit; 9606edb3c96SDavid Ahern break; 9616edb3c96SDavid Ahern case RTN_THROW: 9626edb3c96SDavid Ahern case RTN_UNREACHABLE: 9636edb3c96SDavid Ahern default: 9646edb3c96SDavid Ahern rt->dst.output = ip6_pkt_discard_out; 9656edb3c96SDavid Ahern rt->dst.input = ip6_pkt_discard; 9666edb3c96SDavid Ahern break; 9676edb3c96SDavid Ahern } 9686edb3c96SDavid Ahern } 9696edb3c96SDavid Ahern 9700d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) 9716edb3c96SDavid Ahern { 9727d21fec9SDavid Ahern struct fib6_info *f6i = res->f6i; 9730d161581SDavid Ahern 9747d21fec9SDavid Ahern if (res->fib6_flags & RTF_REJECT) { 9757d21fec9SDavid Ahern ip6_rt_init_dst_reject(rt, res->fib6_type); 9766edb3c96SDavid Ahern return; 9776edb3c96SDavid Ahern } 9786edb3c96SDavid Ahern 9796edb3c96SDavid Ahern rt->dst.error = 0; 9806edb3c96SDavid Ahern rt->dst.output = ip6_output; 9816edb3c96SDavid Ahern 9827d21fec9SDavid Ahern if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { 9836edb3c96SDavid Ahern rt->dst.input = ip6_input; 9847d21fec9SDavid Ahern } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { 9856edb3c96SDavid Ahern rt->dst.input = ip6_mc_input; 9866edb3c96SDavid Ahern } else { 9876edb3c96SDavid Ahern rt->dst.input = ip6_forward; 9886edb3c96SDavid Ahern } 9896edb3c96SDavid Ahern 9900d161581SDavid Ahern if (res->nh->fib_nh_lws) { 9910d161581SDavid Ahern rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); 9926edb3c96SDavid Ahern lwtunnel_set_redirect(&rt->dst); 9936edb3c96SDavid Ahern } 9946edb3c96SDavid Ahern 9956edb3c96SDavid Ahern rt->dst.lastuse = jiffies; 9966edb3c96SDavid Ahern } 9976edb3c96SDavid Ahern 998e873e4b9SWei Wang /* Caller must already hold reference to @from */ 9998d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) 1000ae90d867SDavid Ahern { 1001ae90d867SDavid Ahern rt->rt6i_flags &= ~RTF_EXPIRES; 1002a68886a6SDavid Ahern rcu_assign_pointer(rt->from, from); 1003e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, from->fib6_metrics); 1004ae90d867SDavid Ahern } 1005ae90d867SDavid Ahern 10060d161581SDavid Ahern /* Caller must already hold reference to f6i in result */ 10070d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) 1008ae90d867SDavid Ahern { 10090d161581SDavid Ahern const struct fib6_nh *nh = res->nh; 10100d161581SDavid Ahern const struct net_device *dev = nh->fib_nh_dev; 10110d161581SDavid Ahern struct fib6_info *f6i = res->f6i; 1012dcd1f572SDavid Ahern 10130d161581SDavid Ahern ip6_rt_init_dst(rt, res); 10146edb3c96SDavid Ahern 10150d161581SDavid Ahern rt->rt6i_dst = f6i->fib6_dst; 1016dcd1f572SDavid Ahern rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; 10177d21fec9SDavid Ahern rt->rt6i_flags = res->fib6_flags; 10180d161581SDavid Ahern if (nh->fib_nh_gw_family) { 10190d161581SDavid Ahern rt->rt6i_gateway = nh->fib_nh_gw6; 10202b2450caSDavid Ahern rt->rt6i_flags |= RTF_GATEWAY; 10212b2450caSDavid Ahern } 10220d161581SDavid Ahern rt6_set_from(rt, f6i); 1023ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES 10240d161581SDavid Ahern rt->rt6i_src = f6i->fib6_src; 1025ae90d867SDavid Ahern #endif 1026ae90d867SDavid Ahern } 1027ae90d867SDavid Ahern 1028a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 1029a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 1030a3c00e46SMartin KaFai Lau { 103166f5d6ceSWei Wang struct fib6_node *pn, *sn; 1032a3c00e46SMartin KaFai Lau while (1) { 1033a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 1034a3c00e46SMartin KaFai Lau return NULL; 103566f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 103666f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 103766f5d6ceSWei Wang if (sn && sn != fn) 10386454743bSDavid Ahern fn = fib6_node_lookup(sn, NULL, saddr); 1039a3c00e46SMartin KaFai Lau else 1040a3c00e46SMartin KaFai Lau fn = pn; 1041a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 1042a3c00e46SMartin KaFai Lau return fn; 1043a3c00e46SMartin KaFai Lau } 1044a3c00e46SMartin KaFai Lau } 1045c71099acSThomas Graf 104610585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) 1047d3843fe5SWei Wang { 1048d3843fe5SWei Wang struct rt6_info *rt = *prt; 1049d3843fe5SWei Wang 1050d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 1051d3843fe5SWei Wang return true; 105210585b43SDavid Ahern if (net) { 1053d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 1054d3843fe5SWei Wang dst_hold(&rt->dst); 1055d3843fe5SWei Wang } else { 1056d3843fe5SWei Wang rt = NULL; 1057d3843fe5SWei Wang } 1058d3843fe5SWei Wang *prt = rt; 1059d3843fe5SWei Wang return false; 1060d3843fe5SWei Wang } 1061d3843fe5SWei Wang 1062dec9b0e2SDavid Ahern /* called with rcu_lock held */ 10639b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) 1064dec9b0e2SDavid Ahern { 10659b6b35abSDavid Ahern struct net_device *dev = res->nh->fib_nh_dev; 10669b6b35abSDavid Ahern struct fib6_info *f6i = res->f6i; 10679b6b35abSDavid Ahern unsigned short flags; 1068dec9b0e2SDavid Ahern struct rt6_info *nrt; 1069dec9b0e2SDavid Ahern 10709b6b35abSDavid Ahern if (!fib6_info_hold_safe(f6i)) 10711c87e79aSXin Long goto fallback; 1072e873e4b9SWei Wang 10739b6b35abSDavid Ahern flags = fib6_info_dst_flags(f6i); 107493531c67SDavid Ahern nrt = ip6_dst_alloc(dev_net(dev), dev, flags); 10751c87e79aSXin Long if (!nrt) { 10769b6b35abSDavid Ahern fib6_info_release(f6i); 10771c87e79aSXin Long goto fallback; 10781c87e79aSXin Long } 1079dec9b0e2SDavid Ahern 10800d161581SDavid Ahern ip6_rt_copy_init(nrt, res); 10811c87e79aSXin Long return nrt; 10821c87e79aSXin Long 10831c87e79aSXin Long fallback: 10841c87e79aSXin Long nrt = dev_net(dev)->ipv6.ip6_null_entry; 10851c87e79aSXin Long dst_hold(&nrt->dst); 1086dec9b0e2SDavid Ahern return nrt; 1087dec9b0e2SDavid Ahern } 1088dec9b0e2SDavid Ahern 10898ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 10908ed67789SDaniel Lezcano struct fib6_table *table, 1091b75cc8f9SDavid Ahern struct flowi6 *fl6, 1092b75cc8f9SDavid Ahern const struct sk_buff *skb, 1093b75cc8f9SDavid Ahern int flags) 10941da177e4SLinus Torvalds { 1095b1d40991SDavid Ahern struct fib6_result res = {}; 10961da177e4SLinus Torvalds struct fib6_node *fn; 109723fb93a4SDavid Ahern struct rt6_info *rt; 10981da177e4SLinus Torvalds 1099b6cdbc85SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1100b6cdbc85SDavid Ahern flags &= ~RT6_LOOKUP_F_IFACE; 1101b6cdbc85SDavid Ahern 110266f5d6ceSWei Wang rcu_read_lock(); 11036454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1104c71099acSThomas Graf restart: 1105b1d40991SDavid Ahern res.f6i = rcu_dereference(fn->leaf); 1106b1d40991SDavid Ahern if (!res.f6i) 1107b1d40991SDavid Ahern res.f6i = net->ipv6.fib6_null_entry; 1108af52a52cSDavid Ahern else 110975ef7389SDavid Ahern rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, 111075ef7389SDavid Ahern flags); 1111af52a52cSDavid Ahern 1112b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1113a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1114a3c00e46SMartin KaFai Lau if (fn) 1115a3c00e46SMartin KaFai Lau goto restart; 1116af52a52cSDavid Ahern 1117af52a52cSDavid Ahern rt = net->ipv6.ip6_null_entry; 1118af52a52cSDavid Ahern dst_hold(&rt->dst); 1119af52a52cSDavid Ahern goto out; 1120a3c00e46SMartin KaFai Lau } 11212b760fcfSWei Wang 1122b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, fl6->flowi6_oif, 1123b1d40991SDavid Ahern fl6->flowi6_oif != 0, skb, flags); 1124b1d40991SDavid Ahern 11254c9483b2SDavid S. Miller /* Search through exception table */ 11267e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 112723fb93a4SDavid Ahern if (rt) { 112810585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 1129d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 113023fb93a4SDavid Ahern } else { 11319b6b35abSDavid Ahern rt = ip6_create_rt_rcu(&res); 1132dec9b0e2SDavid Ahern } 1133d3843fe5SWei Wang 1134af52a52cSDavid Ahern out: 11358ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 1136af52a52cSDavid Ahern 113766f5d6ceSWei Wang rcu_read_unlock(); 1138b811580dSDavid Ahern 11391da177e4SLinus Torvalds return rt; 1140c71099acSThomas Graf } 1141c71099acSThomas Graf 1142ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 1143b75cc8f9SDavid Ahern const struct sk_buff *skb, int flags) 1144ea6e574eSFlorian Westphal { 1145b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); 1146ea6e574eSFlorian Westphal } 1147ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 1148ea6e574eSFlorian Westphal 11499acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 1150b75cc8f9SDavid Ahern const struct in6_addr *saddr, int oif, 1151b75cc8f9SDavid Ahern const struct sk_buff *skb, int strict) 1152c71099acSThomas Graf { 11534c9483b2SDavid S. Miller struct flowi6 fl6 = { 11544c9483b2SDavid S. Miller .flowi6_oif = oif, 11554c9483b2SDavid S. Miller .daddr = *daddr, 1156c71099acSThomas Graf }; 1157c71099acSThomas Graf struct dst_entry *dst; 115877d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 1159c71099acSThomas Graf 1160adaa70bbSThomas Graf if (saddr) { 11614c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 1162adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 1163adaa70bbSThomas Graf } 1164adaa70bbSThomas Graf 1165b75cc8f9SDavid Ahern dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); 1166c71099acSThomas Graf if (dst->error == 0) 1167c71099acSThomas Graf return (struct rt6_info *) dst; 1168c71099acSThomas Graf 1169c71099acSThomas Graf dst_release(dst); 1170c71099acSThomas Graf 11711da177e4SLinus Torvalds return NULL; 11721da177e4SLinus Torvalds } 11737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 11747159039aSYOSHIFUJI Hideaki 1175c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 11761cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 11771cfb71eeSWei Wang * route is released. 11781cfb71eeSWei Wang * Caller must hold dst before calling it. 11791da177e4SLinus Torvalds */ 11801da177e4SLinus Torvalds 11818d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, 1182333c4301SDavid Ahern struct netlink_ext_ack *extack) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds int err; 1185c71099acSThomas Graf struct fib6_table *table; 11861da177e4SLinus Torvalds 118793c2fb25SDavid Ahern table = rt->fib6_table; 118866f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1189d4ead6b3SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, extack); 119066f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds return err; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11958d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt) 119640e22e8fSThomas Graf { 1197afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net, }; 1198e715b6d3SFlorian Westphal 1199d4ead6b3SDavid Ahern return __ip6_ins_rt(rt, &info, NULL); 120040e22e8fSThomas Graf } 120140e22e8fSThomas Graf 120285bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, 120321efcfa0SEric Dumazet const struct in6_addr *daddr, 1204b71d1d42SEric Dumazet const struct in6_addr *saddr) 12051da177e4SLinus Torvalds { 120685bd05deSDavid Ahern struct fib6_info *f6i = res->f6i; 12074832c30dSDavid Ahern struct net_device *dev; 12081da177e4SLinus Torvalds struct rt6_info *rt; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * Clone the route. 12121da177e4SLinus Torvalds */ 12131da177e4SLinus Torvalds 121485bd05deSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1215e873e4b9SWei Wang return NULL; 1216e873e4b9SWei Wang 12170d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 121893531c67SDavid Ahern rt = ip6_dst_alloc(dev_net(dev), dev, 0); 1219e873e4b9SWei Wang if (!rt) { 122085bd05deSDavid Ahern fib6_info_release(f6i); 122183a09abdSMartin KaFai Lau return NULL; 1222e873e4b9SWei Wang } 122383a09abdSMartin KaFai Lau 12240d161581SDavid Ahern ip6_rt_copy_init(rt, res); 12258b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 122683a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 122783a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 122883a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 12298b9df265SMartin KaFai Lau 123085bd05deSDavid Ahern if (!rt6_is_gw_or_nonexthop(res)) { 123185bd05deSDavid Ahern if (f6i->fib6_dst.plen != 128 && 123285bd05deSDavid Ahern ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) 123358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 12341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 12351da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 12364e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 12371da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds #endif 124095a9a5baSYOSHIFUJI Hideaki } 124195a9a5baSYOSHIFUJI Hideaki 1242299d9939SYOSHIFUJI Hideaki return rt; 1243299d9939SYOSHIFUJI Hideaki } 1244299d9939SYOSHIFUJI Hideaki 1245db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) 1246d52d3997SMartin KaFai Lau { 1247db3fedeeSDavid Ahern struct fib6_info *f6i = res->f6i; 1248db3fedeeSDavid Ahern unsigned short flags = fib6_info_dst_flags(f6i); 12494832c30dSDavid Ahern struct net_device *dev; 1250d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1251d52d3997SMartin KaFai Lau 1252db3fedeeSDavid Ahern if (!fib6_info_hold_safe(f6i)) 1253e873e4b9SWei Wang return NULL; 1254e873e4b9SWei Wang 12554832c30dSDavid Ahern rcu_read_lock(); 12560d161581SDavid Ahern dev = ip6_rt_get_dev_rcu(res); 125793531c67SDavid Ahern pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); 12584832c30dSDavid Ahern rcu_read_unlock(); 1259e873e4b9SWei Wang if (!pcpu_rt) { 1260db3fedeeSDavid Ahern fib6_info_release(f6i); 1261d52d3997SMartin KaFai Lau return NULL; 1262e873e4b9SWei Wang } 12630d161581SDavid Ahern ip6_rt_copy_init(pcpu_rt, res); 1264d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1265d52d3997SMartin KaFai Lau return pcpu_rt; 1266d52d3997SMartin KaFai Lau } 1267d52d3997SMartin KaFai Lau 126866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1269db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) 1270d52d3997SMartin KaFai Lau { 1271a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1272d52d3997SMartin KaFai Lau 1273*f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1274d52d3997SMartin KaFai Lau pcpu_rt = *p; 1275d52d3997SMartin KaFai Lau 1276d4ead6b3SDavid Ahern if (pcpu_rt) 127710585b43SDavid Ahern ip6_hold_safe(NULL, &pcpu_rt); 1278d3843fe5SWei Wang 1279a73e4195SMartin KaFai Lau return pcpu_rt; 1280a73e4195SMartin KaFai Lau } 1281a73e4195SMartin KaFai Lau 1282afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net, 1283db3fedeeSDavid Ahern const struct fib6_result *res) 1284a73e4195SMartin KaFai Lau { 1285a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1286d52d3997SMartin KaFai Lau 1287db3fedeeSDavid Ahern pcpu_rt = ip6_rt_pcpu_alloc(res); 1288d52d3997SMartin KaFai Lau if (!pcpu_rt) { 12899c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 12909c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1291d52d3997SMartin KaFai Lau } 1292d52d3997SMartin KaFai Lau 1293a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1294*f40b6ae2SDavid Ahern p = this_cpu_ptr(res->nh->rt6i_pcpu); 1295d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1296951f788aSEric Dumazet BUG_ON(prev); 1297a94b9367SWei Wang 129861fb0d01SEric Dumazet if (res->f6i->fib6_destroying) { 129961fb0d01SEric Dumazet struct fib6_info *from; 130061fb0d01SEric Dumazet 130161fb0d01SEric Dumazet from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 130261fb0d01SEric Dumazet fib6_info_release(from); 130361fb0d01SEric Dumazet } 130461fb0d01SEric Dumazet 1305d52d3997SMartin KaFai Lau return pcpu_rt; 1306d52d3997SMartin KaFai Lau } 1307d52d3997SMartin KaFai Lau 130835732d01SWei Wang /* exception hash table implementation 130935732d01SWei Wang */ 131035732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 131135732d01SWei Wang 131235732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 131335732d01SWei Wang * Caller must hold rt6_exception_lock 131435732d01SWei Wang */ 131535732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 131635732d01SWei Wang struct rt6_exception *rt6_ex) 131735732d01SWei Wang { 1318f5b51fe8SPaolo Abeni struct fib6_info *from; 1319b2427e67SColin Ian King struct net *net; 132081eb8447SWei Wang 132135732d01SWei Wang if (!bucket || !rt6_ex) 132235732d01SWei Wang return; 1323b2427e67SColin Ian King 1324b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 1325f5b51fe8SPaolo Abeni net->ipv6.rt6_stats->fib_rt_cache--; 1326f5b51fe8SPaolo Abeni 1327f5b51fe8SPaolo Abeni /* purge completely the exception to allow releasing the held resources: 1328f5b51fe8SPaolo Abeni * some [sk] cache may keep the dst around for unlimited time 1329f5b51fe8SPaolo Abeni */ 13300e233874SEric Dumazet from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1331f5b51fe8SPaolo Abeni fib6_info_release(from); 1332f5b51fe8SPaolo Abeni dst_dev_put(&rt6_ex->rt6i->dst); 1333f5b51fe8SPaolo Abeni 133435732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 133577634cc6SDavid Ahern dst_release(&rt6_ex->rt6i->dst); 133635732d01SWei Wang kfree_rcu(rt6_ex, rcu); 133735732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 133835732d01SWei Wang bucket->depth--; 133935732d01SWei Wang } 134035732d01SWei Wang 134135732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 134235732d01SWei Wang * Caller must hold rt6_exception_lock 134335732d01SWei Wang */ 134435732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 134535732d01SWei Wang { 134635732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 134735732d01SWei Wang 134835732d01SWei Wang if (!bucket) 134935732d01SWei Wang return; 135035732d01SWei Wang 135135732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 135235732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 135335732d01SWei Wang oldest = rt6_ex; 135435732d01SWei Wang } 135535732d01SWei Wang rt6_remove_exception(bucket, oldest); 135635732d01SWei Wang } 135735732d01SWei Wang 135835732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 135935732d01SWei Wang const struct in6_addr *src) 136035732d01SWei Wang { 136135732d01SWei Wang static u32 seed __read_mostly; 136235732d01SWei Wang u32 val; 136335732d01SWei Wang 136435732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 136535732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 136635732d01SWei Wang 136735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 136835732d01SWei Wang if (src) 136935732d01SWei Wang val = jhash(src, sizeof(*src), val); 137035732d01SWei Wang #endif 137135732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 137235732d01SWei Wang } 137335732d01SWei Wang 137435732d01SWei Wang /* Helper function to find the cached rt in the hash table 137535732d01SWei Wang * and update bucket pointer to point to the bucket for this 137635732d01SWei Wang * (daddr, saddr) pair 137735732d01SWei Wang * Caller must hold rt6_exception_lock 137835732d01SWei Wang */ 137935732d01SWei Wang static struct rt6_exception * 138035732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 138135732d01SWei Wang const struct in6_addr *daddr, 138235732d01SWei Wang const struct in6_addr *saddr) 138335732d01SWei Wang { 138435732d01SWei Wang struct rt6_exception *rt6_ex; 138535732d01SWei Wang u32 hval; 138635732d01SWei Wang 138735732d01SWei Wang if (!(*bucket) || !daddr) 138835732d01SWei Wang return NULL; 138935732d01SWei Wang 139035732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 139135732d01SWei Wang *bucket += hval; 139235732d01SWei Wang 139335732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 139435732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 139535732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 139635732d01SWei Wang 139735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139835732d01SWei Wang if (matched && saddr) 139935732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 140035732d01SWei Wang #endif 140135732d01SWei Wang if (matched) 140235732d01SWei Wang return rt6_ex; 140335732d01SWei Wang } 140435732d01SWei Wang return NULL; 140535732d01SWei Wang } 140635732d01SWei Wang 140735732d01SWei Wang /* Helper function to find the cached rt in the hash table 140835732d01SWei Wang * and update bucket pointer to point to the bucket for this 140935732d01SWei Wang * (daddr, saddr) pair 141035732d01SWei Wang * Caller must hold rcu_read_lock() 141135732d01SWei Wang */ 141235732d01SWei Wang static struct rt6_exception * 141335732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 141435732d01SWei Wang const struct in6_addr *daddr, 141535732d01SWei Wang const struct in6_addr *saddr) 141635732d01SWei Wang { 141735732d01SWei Wang struct rt6_exception *rt6_ex; 141835732d01SWei Wang u32 hval; 141935732d01SWei Wang 142035732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 142135732d01SWei Wang 142235732d01SWei Wang if (!(*bucket) || !daddr) 142335732d01SWei Wang return NULL; 142435732d01SWei Wang 142535732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 142635732d01SWei Wang *bucket += hval; 142735732d01SWei Wang 142835732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 142935732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 143035732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 143135732d01SWei Wang 143235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143335732d01SWei Wang if (matched && saddr) 143435732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 143535732d01SWei Wang #endif 143635732d01SWei Wang if (matched) 143735732d01SWei Wang return rt6_ex; 143835732d01SWei Wang } 143935732d01SWei Wang return NULL; 144035732d01SWei Wang } 144135732d01SWei Wang 1442b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res) 144335732d01SWei Wang { 1444b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 1445d4ead6b3SDavid Ahern unsigned int mtu; 1446d4ead6b3SDavid Ahern 1447b748f260SDavid Ahern if (res->f6i->fib6_pmtu) { 1448b748f260SDavid Ahern mtu = res->f6i->fib6_pmtu; 1449dcd1f572SDavid Ahern } else { 1450b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 1451dcd1f572SDavid Ahern struct inet6_dev *idev; 1452dcd1f572SDavid Ahern 1453dcd1f572SDavid Ahern rcu_read_lock(); 1454dcd1f572SDavid Ahern idev = __in6_dev_get(dev); 1455dcd1f572SDavid Ahern mtu = idev->cnf.mtu6; 1456dcd1f572SDavid Ahern rcu_read_unlock(); 1457dcd1f572SDavid Ahern } 1458dcd1f572SDavid Ahern 1459d4ead6b3SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 1460d4ead6b3SDavid Ahern 1461b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 1462d4ead6b3SDavid Ahern } 1463d4ead6b3SDavid Ahern 146435732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 14655012f0a5SDavid Ahern const struct fib6_result *res) 146635732d01SWei Wang { 14675e670d84SDavid Ahern struct net *net = dev_net(nrt->dst.dev); 146835732d01SWei Wang struct rt6_exception_bucket *bucket; 146935732d01SWei Wang struct in6_addr *src_key = NULL; 147035732d01SWei Wang struct rt6_exception *rt6_ex; 14715012f0a5SDavid Ahern struct fib6_info *f6i = res->f6i; 147235732d01SWei Wang int err = 0; 147335732d01SWei Wang 147435732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 147535732d01SWei Wang 14765012f0a5SDavid Ahern if (f6i->exception_bucket_flushed) { 147735732d01SWei Wang err = -EINVAL; 147835732d01SWei Wang goto out; 147935732d01SWei Wang } 148035732d01SWei Wang 14815012f0a5SDavid Ahern bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 148235732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 148335732d01SWei Wang if (!bucket) { 148435732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 148535732d01SWei Wang GFP_ATOMIC); 148635732d01SWei Wang if (!bucket) { 148735732d01SWei Wang err = -ENOMEM; 148835732d01SWei Wang goto out; 148935732d01SWei Wang } 14905012f0a5SDavid Ahern rcu_assign_pointer(f6i->rt6i_exception_bucket, bucket); 149135732d01SWei Wang } 149235732d01SWei Wang 149335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 14945012f0a5SDavid Ahern /* fib6_src.plen != 0 indicates f6i is in subtree 149535732d01SWei Wang * and exception table is indexed by a hash of 14965012f0a5SDavid Ahern * both fib6_dst and fib6_src. 149735732d01SWei Wang * Otherwise, the exception table is indexed by 14985012f0a5SDavid Ahern * a hash of only fib6_dst. 149935732d01SWei Wang */ 15005012f0a5SDavid Ahern if (f6i->fib6_src.plen) 150135732d01SWei Wang src_key = &nrt->rt6i_src.addr; 150235732d01SWei Wang #endif 15035012f0a5SDavid Ahern /* rt6_mtu_change() might lower mtu on f6i. 1504f5bbe7eeSWei Wang * Only insert this exception route if its mtu 15055012f0a5SDavid Ahern * is less than f6i's mtu value. 1506f5bbe7eeSWei Wang */ 1507b748f260SDavid Ahern if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { 1508f5bbe7eeSWei Wang err = -EINVAL; 1509f5bbe7eeSWei Wang goto out; 1510f5bbe7eeSWei Wang } 151160006a48SWei Wang 151235732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 151335732d01SWei Wang src_key); 151435732d01SWei Wang if (rt6_ex) 151535732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 151635732d01SWei Wang 151735732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 151835732d01SWei Wang if (!rt6_ex) { 151935732d01SWei Wang err = -ENOMEM; 152035732d01SWei Wang goto out; 152135732d01SWei Wang } 152235732d01SWei Wang rt6_ex->rt6i = nrt; 152335732d01SWei Wang rt6_ex->stamp = jiffies; 152435732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 152535732d01SWei Wang bucket->depth++; 152681eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 152735732d01SWei Wang 152835732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 152935732d01SWei Wang rt6_exception_remove_oldest(bucket); 153035732d01SWei Wang 153135732d01SWei Wang out: 153235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 153335732d01SWei Wang 153435732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1535b886d5f2SPaolo Abeni if (!err) { 15365012f0a5SDavid Ahern spin_lock_bh(&f6i->fib6_table->tb6_lock); 15375012f0a5SDavid Ahern fib6_update_sernum(net, f6i); 15385012f0a5SDavid Ahern spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1539b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1540b886d5f2SPaolo Abeni } 154135732d01SWei Wang 154235732d01SWei Wang return err; 154335732d01SWei Wang } 154435732d01SWei Wang 15458d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt) 154635732d01SWei Wang { 154735732d01SWei Wang struct rt6_exception_bucket *bucket; 154835732d01SWei Wang struct rt6_exception *rt6_ex; 154935732d01SWei Wang struct hlist_node *tmp; 155035732d01SWei Wang int i; 155135732d01SWei Wang 155235732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 155335732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 155435732d01SWei Wang rt->exception_bucket_flushed = 1; 155535732d01SWei Wang 155635732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 155735732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 155835732d01SWei Wang if (!bucket) 155935732d01SWei Wang goto out; 156035732d01SWei Wang 156135732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 156235732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 156335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 156435732d01SWei Wang WARN_ON_ONCE(bucket->depth); 156535732d01SWei Wang bucket++; 156635732d01SWei Wang } 156735732d01SWei Wang 156835732d01SWei Wang out: 156935732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 157035732d01SWei Wang } 157135732d01SWei Wang 157235732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 157335732d01SWei Wang * Caller has to hold rcu_read_lock() 157435732d01SWei Wang */ 15757e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, 1576510e2cedSWei Wang const struct in6_addr *daddr, 1577510e2cedSWei Wang const struct in6_addr *saddr) 157835732d01SWei Wang { 1579510e2cedSWei Wang const struct in6_addr *src_key = NULL; 158035732d01SWei Wang struct rt6_exception_bucket *bucket; 158135732d01SWei Wang struct rt6_exception *rt6_ex; 15827e4b5128SDavid Ahern struct rt6_info *ret = NULL; 158335732d01SWei Wang 158435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 15857e4b5128SDavid Ahern /* fib6i_src.plen != 0 indicates f6i is in subtree 158635732d01SWei Wang * and exception table is indexed by a hash of 15877e4b5128SDavid Ahern * both fib6_dst and fib6_src. 1588510e2cedSWei Wang * However, the src addr used to create the hash 1589510e2cedSWei Wang * might not be exactly the passed in saddr which 1590510e2cedSWei Wang * is a /128 addr from the flow. 1591510e2cedSWei Wang * So we need to use f6i->fib6_src to redo lookup 1592510e2cedSWei Wang * if the passed in saddr does not find anything. 1593510e2cedSWei Wang * (See the logic in ip6_rt_cache_alloc() on how 1594510e2cedSWei Wang * rt->rt6i_src is updated.) 159535732d01SWei Wang */ 15967e4b5128SDavid Ahern if (res->f6i->fib6_src.plen) 159735732d01SWei Wang src_key = saddr; 1598510e2cedSWei Wang find_ex: 159935732d01SWei Wang #endif 1600510e2cedSWei Wang bucket = rcu_dereference(res->f6i->rt6i_exception_bucket); 160135732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 160235732d01SWei Wang 160335732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 16047e4b5128SDavid Ahern ret = rt6_ex->rt6i; 160535732d01SWei Wang 1606510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES 1607510e2cedSWei Wang /* Use fib6_src as src_key and redo lookup */ 1608510e2cedSWei Wang if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { 1609510e2cedSWei Wang src_key = &res->f6i->fib6_src.addr; 1610510e2cedSWei Wang goto find_ex; 1611510e2cedSWei Wang } 1612510e2cedSWei Wang #endif 1613510e2cedSWei Wang 16147e4b5128SDavid Ahern return ret; 161535732d01SWei Wang } 161635732d01SWei Wang 161735732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 161823fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt) 161935732d01SWei Wang { 162035732d01SWei Wang struct rt6_exception_bucket *bucket; 162135732d01SWei Wang struct in6_addr *src_key = NULL; 162235732d01SWei Wang struct rt6_exception *rt6_ex; 16238a14e46fSDavid Ahern struct fib6_info *from; 162435732d01SWei Wang int err; 162535732d01SWei Wang 1626091311deSEric Dumazet from = rcu_dereference(rt->from); 162735732d01SWei Wang if (!from || 1628442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 162935732d01SWei Wang return -EINVAL; 163035732d01SWei Wang 163135732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 163235732d01SWei Wang return -ENOENT; 163335732d01SWei Wang 163435732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 163535732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 163635732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 163735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 163835732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 163935732d01SWei Wang * and exception table is indexed by a hash of 164035732d01SWei Wang * both rt6i_dst and rt6i_src. 164135732d01SWei Wang * Otherwise, the exception table is indexed by 164235732d01SWei Wang * a hash of only rt6i_dst. 164335732d01SWei Wang */ 164493c2fb25SDavid Ahern if (from->fib6_src.plen) 164535732d01SWei Wang src_key = &rt->rt6i_src.addr; 164635732d01SWei Wang #endif 164735732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 164835732d01SWei Wang &rt->rt6i_dst.addr, 164935732d01SWei Wang src_key); 165035732d01SWei Wang if (rt6_ex) { 165135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 165235732d01SWei Wang err = 0; 165335732d01SWei Wang } else { 165435732d01SWei Wang err = -ENOENT; 165535732d01SWei Wang } 165635732d01SWei Wang 165735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 165835732d01SWei Wang return err; 165935732d01SWei Wang } 166035732d01SWei Wang 166135732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 166235732d01SWei Wang * refresh its stamp 166335732d01SWei Wang */ 166435732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 166535732d01SWei Wang { 166635732d01SWei Wang struct rt6_exception_bucket *bucket; 166735732d01SWei Wang struct in6_addr *src_key = NULL; 166835732d01SWei Wang struct rt6_exception *rt6_ex; 1669193f3685SPaolo Abeni struct fib6_info *from; 167035732d01SWei Wang 167135732d01SWei Wang rcu_read_lock(); 1672193f3685SPaolo Abeni from = rcu_dereference(rt->from); 1673193f3685SPaolo Abeni if (!from || !(rt->rt6i_flags & RTF_CACHE)) 1674193f3685SPaolo Abeni goto unlock; 1675193f3685SPaolo Abeni 167635732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 167735732d01SWei Wang 167835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 167935732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 168035732d01SWei Wang * and exception table is indexed by a hash of 168135732d01SWei Wang * both rt6i_dst and rt6i_src. 168235732d01SWei Wang * Otherwise, the exception table is indexed by 168335732d01SWei Wang * a hash of only rt6i_dst. 168435732d01SWei Wang */ 168593c2fb25SDavid Ahern if (from->fib6_src.plen) 168635732d01SWei Wang src_key = &rt->rt6i_src.addr; 168735732d01SWei Wang #endif 168835732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 168935732d01SWei Wang &rt->rt6i_dst.addr, 169035732d01SWei Wang src_key); 169135732d01SWei Wang if (rt6_ex) 169235732d01SWei Wang rt6_ex->stamp = jiffies; 169335732d01SWei Wang 1694193f3685SPaolo Abeni unlock: 169535732d01SWei Wang rcu_read_unlock(); 169635732d01SWei Wang } 169735732d01SWei Wang 1698e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, 1699e9fa1495SStefano Brivio struct rt6_info *rt, int mtu) 1700e9fa1495SStefano Brivio { 1701e9fa1495SStefano Brivio /* If the new MTU is lower than the route PMTU, this new MTU will be the 1702e9fa1495SStefano Brivio * lowest MTU in the path: always allow updating the route PMTU to 1703e9fa1495SStefano Brivio * reflect PMTU decreases. 1704e9fa1495SStefano Brivio * 1705e9fa1495SStefano Brivio * If the new MTU is higher, and the route PMTU is equal to the local 1706e9fa1495SStefano Brivio * MTU, this means the old MTU is the lowest in the path, so allow 1707e9fa1495SStefano Brivio * updating it: if other nodes now have lower MTUs, PMTU discovery will 1708e9fa1495SStefano Brivio * handle this. 1709e9fa1495SStefano Brivio */ 1710e9fa1495SStefano Brivio 1711e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) >= mtu) 1712e9fa1495SStefano Brivio return true; 1713e9fa1495SStefano Brivio 1714e9fa1495SStefano Brivio if (dst_mtu(&rt->dst) == idev->cnf.mtu6) 1715e9fa1495SStefano Brivio return true; 1716e9fa1495SStefano Brivio 1717e9fa1495SStefano Brivio return false; 1718e9fa1495SStefano Brivio } 1719e9fa1495SStefano Brivio 1720e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, 17218d1c802bSDavid Ahern struct fib6_info *rt, int mtu) 1722f5bbe7eeSWei Wang { 1723f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1724f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1725f5bbe7eeSWei Wang int i; 1726f5bbe7eeSWei Wang 1727f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1728f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1729f5bbe7eeSWei Wang 1730e9fa1495SStefano Brivio if (!bucket) 1731e9fa1495SStefano Brivio return; 1732e9fa1495SStefano Brivio 1733f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1734f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1735f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1736e9fa1495SStefano Brivio 1737e9fa1495SStefano Brivio /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected 1738d4ead6b3SDavid Ahern * route), the metrics of its rt->from have already 1739f5bbe7eeSWei Wang * been updated. 1740f5bbe7eeSWei Wang */ 1741d4ead6b3SDavid Ahern if (dst_metric_raw(&entry->dst, RTAX_MTU) && 1742e9fa1495SStefano Brivio rt6_mtu_change_route_allowed(idev, entry, mtu)) 1743d4ead6b3SDavid Ahern dst_metric_set(&entry->dst, RTAX_MTU, mtu); 1744f5bbe7eeSWei Wang } 1745f5bbe7eeSWei Wang bucket++; 1746f5bbe7eeSWei Wang } 1747f5bbe7eeSWei Wang } 1748f5bbe7eeSWei Wang 1749b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1750b16cb459SWei Wang 17518d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt, 1752b16cb459SWei Wang struct in6_addr *gateway) 1753b16cb459SWei Wang { 1754b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1755b16cb459SWei Wang struct rt6_exception *rt6_ex; 1756b16cb459SWei Wang struct hlist_node *tmp; 1757b16cb459SWei Wang int i; 1758b16cb459SWei Wang 1759b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1760b16cb459SWei Wang return; 1761b16cb459SWei Wang 1762b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1763b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1764b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1765b16cb459SWei Wang 1766b16cb459SWei Wang if (bucket) { 1767b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1768b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1769b16cb459SWei Wang &bucket->chain, hlist) { 1770b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1771b16cb459SWei Wang 1772b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1773b16cb459SWei Wang RTF_CACHE_GATEWAY && 1774b16cb459SWei Wang ipv6_addr_equal(gateway, 1775b16cb459SWei Wang &entry->rt6i_gateway)) { 1776b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1777b16cb459SWei Wang } 1778b16cb459SWei Wang } 1779b16cb459SWei Wang bucket++; 1780b16cb459SWei Wang } 1781b16cb459SWei Wang } 1782b16cb459SWei Wang 1783b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1784b16cb459SWei Wang } 1785b16cb459SWei Wang 1786c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1787c757faa8SWei Wang struct rt6_exception *rt6_ex, 1788c757faa8SWei Wang struct fib6_gc_args *gc_args, 1789c757faa8SWei Wang unsigned long now) 1790c757faa8SWei Wang { 1791c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1792c757faa8SWei Wang 17931859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 17941859bac0SPaolo Abeni * even if others have still references to them, so that on next 17951859bac0SPaolo Abeni * dst_check() such references can be dropped. 17961859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 17971859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 17981859bac0SPaolo Abeni */ 179931afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 180031afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1801c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1802c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1803c757faa8SWei Wang return; 180431afeb42SWei Wang } 180531afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 180631afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 180731afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 180831afeb42SWei Wang return; 180931afeb42SWei Wang } 181031afeb42SWei Wang 181131afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 1812c757faa8SWei Wang struct neighbour *neigh; 1813c757faa8SWei Wang __u8 neigh_flags = 0; 1814c757faa8SWei Wang 18151bfa26ffSEric Dumazet neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 18161bfa26ffSEric Dumazet if (neigh) 1817c757faa8SWei Wang neigh_flags = neigh->flags; 18181bfa26ffSEric Dumazet 1819c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1820c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1821c757faa8SWei Wang rt); 1822c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1823c757faa8SWei Wang return; 1824c757faa8SWei Wang } 1825c757faa8SWei Wang } 182631afeb42SWei Wang 1827c757faa8SWei Wang gc_args->more++; 1828c757faa8SWei Wang } 1829c757faa8SWei Wang 18308d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt, 1831c757faa8SWei Wang struct fib6_gc_args *gc_args, 1832c757faa8SWei Wang unsigned long now) 1833c757faa8SWei Wang { 1834c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1835c757faa8SWei Wang struct rt6_exception *rt6_ex; 1836c757faa8SWei Wang struct hlist_node *tmp; 1837c757faa8SWei Wang int i; 1838c757faa8SWei Wang 1839c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1840c757faa8SWei Wang return; 1841c757faa8SWei Wang 18421bfa26ffSEric Dumazet rcu_read_lock_bh(); 18431bfa26ffSEric Dumazet spin_lock(&rt6_exception_lock); 1844c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1845c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1846c757faa8SWei Wang 1847c757faa8SWei Wang if (bucket) { 1848c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1849c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1850c757faa8SWei Wang &bucket->chain, hlist) { 1851c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1852c757faa8SWei Wang gc_args, now); 1853c757faa8SWei Wang } 1854c757faa8SWei Wang bucket++; 1855c757faa8SWei Wang } 1856c757faa8SWei Wang } 18571bfa26ffSEric Dumazet spin_unlock(&rt6_exception_lock); 18581bfa26ffSEric Dumazet rcu_read_unlock_bh(); 1859c757faa8SWei Wang } 1860c757faa8SWei Wang 18611d053da9SDavid Ahern /* must be called with rcu lock held */ 1862effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, 1863effda4ddSDavid Ahern struct flowi6 *fl6, struct fib6_result *res, int strict) 18641da177e4SLinus Torvalds { 1865367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 18661da177e4SLinus Torvalds 18676454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1868367efcb9SMartin KaFai Lau saved_fn = fn; 18691da177e4SLinus Torvalds 1870ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1871ca254490SDavid Ahern oif = 0; 1872ca254490SDavid Ahern 1873a3c00e46SMartin KaFai Lau redo_rt6_select: 1874effda4ddSDavid Ahern rt6_select(net, fn, oif, res, strict); 1875effda4ddSDavid Ahern if (res->f6i == net->ipv6.fib6_null_entry) { 1876a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1877a3c00e46SMartin KaFai Lau if (fn) 1878a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1879367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1880367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1881367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1882367efcb9SMartin KaFai Lau fn = saved_fn; 1883367efcb9SMartin KaFai Lau goto redo_rt6_select; 1884367efcb9SMartin KaFai Lau } 1885a3c00e46SMartin KaFai Lau } 1886a3c00e46SMartin KaFai Lau 1887effda4ddSDavid Ahern trace_fib6_table_lookup(net, res, table, fl6); 1888d52d3997SMartin KaFai Lau 1889effda4ddSDavid Ahern return 0; 18901d053da9SDavid Ahern } 18911d053da9SDavid Ahern 18921d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 18931d053da9SDavid Ahern int oif, struct flowi6 *fl6, 18941d053da9SDavid Ahern const struct sk_buff *skb, int flags) 18951d053da9SDavid Ahern { 1896b1d40991SDavid Ahern struct fib6_result res = {}; 18971d053da9SDavid Ahern struct rt6_info *rt; 18981d053da9SDavid Ahern int strict = 0; 18991d053da9SDavid Ahern 19001d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IFACE; 19011d053da9SDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 19021d053da9SDavid Ahern if (net->ipv6.devconf_all->forwarding == 0) 19031d053da9SDavid Ahern strict |= RT6_LOOKUP_F_REACHABLE; 19041d053da9SDavid Ahern 19051d053da9SDavid Ahern rcu_read_lock(); 19061d053da9SDavid Ahern 1907effda4ddSDavid Ahern fib6_table_lookup(net, table, oif, fl6, &res, strict); 1908b1d40991SDavid Ahern if (res.f6i == net->ipv6.fib6_null_entry) { 1909421842edSDavid Ahern rt = net->ipv6.ip6_null_entry; 191066f5d6ceSWei Wang rcu_read_unlock(); 1911d3843fe5SWei Wang dst_hold(&rt->dst); 1912d3843fe5SWei Wang return rt; 1913d3843fe5SWei Wang } 191423fb93a4SDavid Ahern 1915b1d40991SDavid Ahern fib6_select_path(net, &res, fl6, oif, false, skb, strict); 1916d83009d4SDavid Ahern 191723fb93a4SDavid Ahern /*Search through exception table */ 19187e4b5128SDavid Ahern rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); 191923fb93a4SDavid Ahern if (rt) { 192010585b43SDavid Ahern if (ip6_hold_safe(net, &rt)) 19211da177e4SLinus Torvalds dst_use_noref(&rt->dst, jiffies); 1922d4ead6b3SDavid Ahern 192366f5d6ceSWei Wang rcu_read_unlock(); 1924d52d3997SMartin KaFai Lau return rt; 19253da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 1926b1d40991SDavid Ahern !res.nh->fib_nh_gw_family)) { 19273da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 19283da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 19293da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 19303da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 19313da59bd9SMartin KaFai Lau */ 19323da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 19333da59bd9SMartin KaFai Lau 193485bd05deSDavid Ahern uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); 1935d52d3997SMartin KaFai Lau 19364d85cd0cSDavid Ahern rcu_read_unlock(); 19373da59bd9SMartin KaFai Lau 19381cfb71eeSWei Wang if (uncached_rt) { 19391cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 19401cfb71eeSWei Wang * No need for another dst_hold() 19411cfb71eeSWei Wang */ 19428d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 194381eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 19441cfb71eeSWei Wang } else { 19453da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 19463da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 19471cfb71eeSWei Wang } 1948b811580dSDavid Ahern 19493da59bd9SMartin KaFai Lau return uncached_rt; 1950d52d3997SMartin KaFai Lau } else { 1951d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1952d52d3997SMartin KaFai Lau 1953d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1954d52d3997SMartin KaFai Lau 1955951f788aSEric Dumazet local_bh_disable(); 1956db3fedeeSDavid Ahern pcpu_rt = rt6_get_pcpu_route(&res); 1957d52d3997SMartin KaFai Lau 195893531c67SDavid Ahern if (!pcpu_rt) 1959db3fedeeSDavid Ahern pcpu_rt = rt6_make_pcpu_route(net, &res); 196093531c67SDavid Ahern 1961951f788aSEric Dumazet local_bh_enable(); 1962951f788aSEric Dumazet rcu_read_unlock(); 1963d4bea421SDavid Ahern 1964d52d3997SMartin KaFai Lau return pcpu_rt; 1965d52d3997SMartin KaFai Lau } 1966c71099acSThomas Graf } 19679ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1968c71099acSThomas Graf 1969b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net, 1970b75cc8f9SDavid Ahern struct fib6_table *table, 1971b75cc8f9SDavid Ahern struct flowi6 *fl6, 1972b75cc8f9SDavid Ahern const struct sk_buff *skb, 1973b75cc8f9SDavid Ahern int flags) 19744acad72dSPavel Emelyanov { 1975b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); 19764acad72dSPavel Emelyanov } 19774acad72dSPavel Emelyanov 1978d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 197972331bc0SShmulik Ladkani struct net_device *dev, 1980b75cc8f9SDavid Ahern struct flowi6 *fl6, 1981b75cc8f9SDavid Ahern const struct sk_buff *skb, 1982b75cc8f9SDavid Ahern int flags) 198372331bc0SShmulik Ladkani { 198472331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 198572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 198672331bc0SShmulik Ladkani 1987b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); 198872331bc0SShmulik Ladkani } 1989d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 199072331bc0SShmulik Ladkani 199123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 19925e5d6fedSRoopa Prabhu struct flow_keys *keys, 19935e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 199423aebdacSJakub Sitnicki { 199523aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 199623aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 19975e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 199823aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 199923aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 200023aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 2001cea67a2dSEric Dumazet struct icmp6hdr _icmph; 200223aebdacSJakub Sitnicki 200323aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 200423aebdacSJakub Sitnicki goto out; 200523aebdacSJakub Sitnicki 2006cea67a2dSEric Dumazet icmph = skb_header_pointer(skb, skb_transport_offset(skb), 2007cea67a2dSEric Dumazet sizeof(_icmph), &_icmph); 2008cea67a2dSEric Dumazet if (!icmph) 2009cea67a2dSEric Dumazet goto out; 2010cea67a2dSEric Dumazet 201123aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 201223aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 201323aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 201423aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 201523aebdacSJakub Sitnicki goto out; 201623aebdacSJakub Sitnicki 201723aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 201823aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 201923aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 202023aebdacSJakub Sitnicki if (!inner_iph) 202123aebdacSJakub Sitnicki goto out; 202223aebdacSJakub Sitnicki 202323aebdacSJakub Sitnicki key_iph = inner_iph; 20245e5d6fedSRoopa Prabhu _flkeys = NULL; 202523aebdacSJakub Sitnicki out: 20265e5d6fedSRoopa Prabhu if (_flkeys) { 20275e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 20285e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 20295e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 20305e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 20315e5d6fedSRoopa Prabhu } else { 203223aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 203323aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 2034fa1be7e0SMichal Kubecek keys->tags.flow_label = ip6_flowlabel(key_iph); 203523aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 203623aebdacSJakub Sitnicki } 20375e5d6fedSRoopa Prabhu } 203823aebdacSJakub Sitnicki 203923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 2040b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, 2041b4bac172SDavid Ahern const struct sk_buff *skb, struct flow_keys *flkeys) 204223aebdacSJakub Sitnicki { 204323aebdacSJakub Sitnicki struct flow_keys hash_keys; 20449a2a537aSDavid Ahern u32 mhash; 204523aebdacSJakub Sitnicki 2046bbfa047aSDavid S. Miller switch (ip6_multipath_hash_policy(net)) { 2047b4bac172SDavid Ahern case 0: 20486f74b6c2SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 20496f74b6c2SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 20509a2a537aSDavid Ahern if (skb) { 20515e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 20529a2a537aSDavid Ahern } else { 20539a2a537aSDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 20549a2a537aSDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2055fa1be7e0SMichal Kubecek hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 20569a2a537aSDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 205723aebdacSJakub Sitnicki } 2058b4bac172SDavid Ahern break; 2059b4bac172SDavid Ahern case 1: 2060b4bac172SDavid Ahern if (skb) { 2061b4bac172SDavid Ahern unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2062b4bac172SDavid Ahern struct flow_keys keys; 2063b4bac172SDavid Ahern 2064b4bac172SDavid Ahern /* short-circuit if we already have L4 hash present */ 2065b4bac172SDavid Ahern if (skb->l4_hash) 2066b4bac172SDavid Ahern return skb_get_hash_raw(skb) >> 1; 2067b4bac172SDavid Ahern 2068b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2069b4bac172SDavid Ahern 2070b4bac172SDavid Ahern if (!flkeys) { 2071b4bac172SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2072b4bac172SDavid Ahern flkeys = &keys; 2073b4bac172SDavid Ahern } 2074b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2075b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; 2076b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; 2077b4bac172SDavid Ahern hash_keys.ports.src = flkeys->ports.src; 2078b4bac172SDavid Ahern hash_keys.ports.dst = flkeys->ports.dst; 2079b4bac172SDavid Ahern hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2080b4bac172SDavid Ahern } else { 2081b4bac172SDavid Ahern memset(&hash_keys, 0, sizeof(hash_keys)); 2082b4bac172SDavid Ahern hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2083b4bac172SDavid Ahern hash_keys.addrs.v6addrs.src = fl6->saddr; 2084b4bac172SDavid Ahern hash_keys.addrs.v6addrs.dst = fl6->daddr; 2085b4bac172SDavid Ahern hash_keys.ports.src = fl6->fl6_sport; 2086b4bac172SDavid Ahern hash_keys.ports.dst = fl6->fl6_dport; 2087b4bac172SDavid Ahern hash_keys.basic.ip_proto = fl6->flowi6_proto; 2088b4bac172SDavid Ahern } 2089b4bac172SDavid Ahern break; 2090b4bac172SDavid Ahern } 20919a2a537aSDavid Ahern mhash = flow_hash_from_keys(&hash_keys); 209223aebdacSJakub Sitnicki 20939a2a537aSDavid Ahern return mhash >> 1; 209423aebdacSJakub Sitnicki } 209523aebdacSJakub Sitnicki 2096c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 2097c71099acSThomas Graf { 2098b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 2099c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 2100adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 2101904af04dSJiri Benc struct ip_tunnel_info *tun_info; 21024c9483b2SDavid S. Miller struct flowi6 fl6 = { 2103e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 21044c9483b2SDavid S. Miller .daddr = iph->daddr, 21054c9483b2SDavid S. Miller .saddr = iph->saddr, 21066502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 21074c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 21084c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 2109c71099acSThomas Graf }; 21105e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2111adaa70bbSThomas Graf 2112904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 211346fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2114904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 21155e5d6fedSRoopa Prabhu 21165e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 21175e5d6fedSRoopa Prabhu flkeys = &_flkeys; 21185e5d6fedSRoopa Prabhu 211923aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 2120b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); 212106e9d040SJiri Benc skb_dst_drop(skb); 2122b75cc8f9SDavid Ahern skb_dst_set(skb, 2123b75cc8f9SDavid Ahern ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); 2124c71099acSThomas Graf } 2125c71099acSThomas Graf 2126b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net, 2127b75cc8f9SDavid Ahern struct fib6_table *table, 2128b75cc8f9SDavid Ahern struct flowi6 *fl6, 2129b75cc8f9SDavid Ahern const struct sk_buff *skb, 2130b75cc8f9SDavid Ahern int flags) 2131c71099acSThomas Graf { 2132b75cc8f9SDavid Ahern return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); 2133c71099acSThomas Graf } 2134c71099acSThomas Graf 21356f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 21366f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 2137c71099acSThomas Graf { 2138d46a9d67SDavid Ahern bool any_src; 2139c71099acSThomas Graf 21403ede0bbcSRobert Shearman if (ipv6_addr_type(&fl6->daddr) & 21413ede0bbcSRobert Shearman (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { 21424c1feac5SDavid Ahern struct dst_entry *dst; 21434c1feac5SDavid Ahern 21444c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 2145ca254490SDavid Ahern if (dst) 2146ca254490SDavid Ahern return dst; 21474c1feac5SDavid Ahern } 2148ca254490SDavid Ahern 21491fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 21504dc27d1cSDavid McCullough 2151d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 2152741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 2153d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 215477d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 2155c71099acSThomas Graf 2156d46a9d67SDavid Ahern if (!any_src) 2157adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 21580c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 21590c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 2160adaa70bbSThomas Graf 2161b75cc8f9SDavid Ahern return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); 21621da177e4SLinus Torvalds } 21636f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 21641da177e4SLinus Torvalds 21652774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 216614e50e57SDavid S. Miller { 21675c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 21681dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 216914e50e57SDavid S. Miller struct dst_entry *new = NULL; 217014e50e57SDavid S. Miller 21711dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 217262cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 217314e50e57SDavid S. Miller if (rt) { 21740a1f5962SMartin KaFai Lau rt6_info_init(rt); 217581eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 21760a1f5962SMartin KaFai Lau 2177d8d1f30bSChangli Gao new = &rt->dst; 217814e50e57SDavid S. Miller new->__use = 1; 2179352e512cSHerbert Xu new->input = dst_discard; 2180ede2059dSEric W. Biederman new->output = dst_discard_out; 218114e50e57SDavid S. Miller 2182defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 218314e50e57SDavid S. Miller 21841dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 21854e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 21860a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 218714e50e57SDavid S. Miller 218814e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 218914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 219014e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 219114e50e57SDavid S. Miller #endif 219214e50e57SDavid S. Miller } 219314e50e57SDavid S. Miller 219469ead7afSDavid S. Miller dst_release(dst_orig); 219569ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 219614e50e57SDavid S. Miller } 219714e50e57SDavid S. Miller 21981da177e4SLinus Torvalds /* 21991da177e4SLinus Torvalds * Destination cache support functions 22001da177e4SLinus Torvalds */ 22011da177e4SLinus Torvalds 22028d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie) 22033da59bd9SMartin KaFai Lau { 220436143645SSteffen Klassert u32 rt_cookie = 0; 2205c5cff856SWei Wang 22068ae86971SDavid Ahern if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) 220793531c67SDavid Ahern return false; 220893531c67SDavid Ahern 220993531c67SDavid Ahern if (fib6_check_expired(f6i)) 221093531c67SDavid Ahern return false; 221193531c67SDavid Ahern 221293531c67SDavid Ahern return true; 221393531c67SDavid Ahern } 221493531c67SDavid Ahern 2215a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt, 2216a68886a6SDavid Ahern struct fib6_info *from, 2217a68886a6SDavid Ahern u32 cookie) 22183da59bd9SMartin KaFai Lau { 2219c5cff856SWei Wang u32 rt_cookie = 0; 2220c5cff856SWei Wang 2221a68886a6SDavid Ahern if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || 222293531c67SDavid Ahern rt_cookie != cookie) 22233da59bd9SMartin KaFai Lau return NULL; 22243da59bd9SMartin KaFai Lau 22253da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 22263da59bd9SMartin KaFai Lau return NULL; 22273da59bd9SMartin KaFai Lau 22283da59bd9SMartin KaFai Lau return &rt->dst; 22293da59bd9SMartin KaFai Lau } 22303da59bd9SMartin KaFai Lau 2231a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, 2232a68886a6SDavid Ahern struct fib6_info *from, 2233a68886a6SDavid Ahern u32 cookie) 22343da59bd9SMartin KaFai Lau { 22355973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 22365973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 2237a68886a6SDavid Ahern fib6_check(from, cookie)) 22383da59bd9SMartin KaFai Lau return &rt->dst; 22393da59bd9SMartin KaFai Lau else 22403da59bd9SMartin KaFai Lau return NULL; 22413da59bd9SMartin KaFai Lau } 22423da59bd9SMartin KaFai Lau 22431da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 22441da177e4SLinus Torvalds { 2245a87b7dc9SDavid Ahern struct dst_entry *dst_ret; 2246a68886a6SDavid Ahern struct fib6_info *from; 22471da177e4SLinus Torvalds struct rt6_info *rt; 22481da177e4SLinus Torvalds 2249a87b7dc9SDavid Ahern rt = container_of(dst, struct rt6_info, dst); 2250a87b7dc9SDavid Ahern 2251a87b7dc9SDavid Ahern rcu_read_lock(); 22521da177e4SLinus Torvalds 22536f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 22546f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 22556f3118b5SNicolas Dichtel * into this function always. 22566f3118b5SNicolas Dichtel */ 2257e3bc10bdSHannes Frederic Sowa 2258a68886a6SDavid Ahern from = rcu_dereference(rt->from); 22594b32b5adSMartin KaFai Lau 2260a68886a6SDavid Ahern if (from && (rt->rt6i_flags & RTF_PCPU || 2261a68886a6SDavid Ahern unlikely(!list_empty(&rt->rt6i_uncached)))) 2262a68886a6SDavid Ahern dst_ret = rt6_dst_from_check(rt, from, cookie); 22633da59bd9SMartin KaFai Lau else 2264a68886a6SDavid Ahern dst_ret = rt6_check(rt, from, cookie); 2265a87b7dc9SDavid Ahern 2266a87b7dc9SDavid Ahern rcu_read_unlock(); 2267a87b7dc9SDavid Ahern 2268a87b7dc9SDavid Ahern return dst_ret; 22691da177e4SLinus Torvalds } 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds if (rt) { 227654c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 2277c3c14da0SDavid Ahern rcu_read_lock(); 227854c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 227993531c67SDavid Ahern rt6_remove_exception_rt(rt); 228054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 22811da177e4SLinus Torvalds } 2282c3c14da0SDavid Ahern rcu_read_unlock(); 228354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 228454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 228554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 228654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 228754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 228854c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 22921da177e4SLinus Torvalds { 22931da177e4SLinus Torvalds struct rt6_info *rt; 22941da177e4SLinus Torvalds 22953ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 22961da177e4SLinus Torvalds 2297adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 22981da177e4SLinus Torvalds if (rt) { 22998a14e46fSDavid Ahern rcu_read_lock(); 23001eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 230193531c67SDavid Ahern rt6_remove_exception_rt(rt); 2302c5cff856SWei Wang } else { 2303a68886a6SDavid Ahern struct fib6_info *from; 2304c5cff856SWei Wang struct fib6_node *fn; 2305c5cff856SWei Wang 2306a68886a6SDavid Ahern from = rcu_dereference(rt->from); 2307a68886a6SDavid Ahern if (from) { 2308a68886a6SDavid Ahern fn = rcu_dereference(from->fib6_node); 2309c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2310c5cff856SWei Wang fn->fn_sernum = -1; 2311a68886a6SDavid Ahern } 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds rcu_read_unlock(); 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23176a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout) 23186a3e030fSDavid Ahern { 2319a68886a6SDavid Ahern if (!(rt0->rt6i_flags & RTF_EXPIRES)) { 2320a68886a6SDavid Ahern struct fib6_info *from; 2321a68886a6SDavid Ahern 2322a68886a6SDavid Ahern rcu_read_lock(); 2323a68886a6SDavid Ahern from = rcu_dereference(rt0->from); 2324a68886a6SDavid Ahern if (from) 2325a68886a6SDavid Ahern rt0->dst.expires = from->expires; 2326a68886a6SDavid Ahern rcu_read_unlock(); 2327a68886a6SDavid Ahern } 23286a3e030fSDavid Ahern 23296a3e030fSDavid Ahern dst_set_expires(&rt0->dst, timeout); 23306a3e030fSDavid Ahern rt0->rt6i_flags |= RTF_EXPIRES; 23316700c270SDavid S. Miller } 23321da177e4SLinus Torvalds 233345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 233445e4fd26SMartin KaFai Lau { 233545e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 233645e4fd26SMartin KaFai Lau 2337d4ead6b3SDavid Ahern dst_metric_set(&rt->dst, RTAX_MTU, mtu); 233845e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 233945e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 234045e4fd26SMartin KaFai Lau } 234145e4fd26SMartin KaFai Lau 23420d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 23430d3f6d29SMartin KaFai Lau { 23440d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 23451490ed2aSPaolo Abeni (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); 23460d3f6d29SMartin KaFai Lau } 23470d3f6d29SMartin KaFai Lau 234845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 234945e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 23501da177e4SLinus Torvalds { 23510dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 23521da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 23531da177e4SLinus Torvalds 235419bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 235519bda36cSXin Long return; 235619bda36cSXin Long 235745e4fd26SMartin KaFai Lau if (iph) { 235845e4fd26SMartin KaFai Lau daddr = &iph->daddr; 235945e4fd26SMartin KaFai Lau saddr = &iph->saddr; 236045e4fd26SMartin KaFai Lau } else if (sk) { 236145e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 236245e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 236345e4fd26SMartin KaFai Lau } else { 23640dec879fSJulian Anastasov daddr = NULL; 23650dec879fSJulian Anastasov saddr = NULL; 23661da177e4SLinus Torvalds } 23670dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 23680dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 23690dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 23700dec879fSJulian Anastasov return; 23710dec879fSJulian Anastasov 23720dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 23730dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 23742b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 23752b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 23762b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 23770dec879fSJulian Anastasov } else if (daddr) { 237885bd05deSDavid Ahern struct fib6_result res = {}; 23790dec879fSJulian Anastasov struct rt6_info *nrt6; 23800dec879fSJulian Anastasov 23814d85cd0cSDavid Ahern rcu_read_lock(); 238285bd05deSDavid Ahern res.f6i = rcu_dereference(rt6->from); 238385bd05deSDavid Ahern if (!res.f6i) { 23849c69a132SJonathan Lemon rcu_read_unlock(); 23859c69a132SJonathan Lemon return; 23869c69a132SJonathan Lemon } 238785bd05deSDavid Ahern res.nh = &res.f6i->fib6_nh; 23887d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 23897d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 23907d21fec9SDavid Ahern 239185bd05deSDavid Ahern nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); 239245e4fd26SMartin KaFai Lau if (nrt6) { 239345e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 23945012f0a5SDavid Ahern if (rt6_insert_exception(nrt6, &res)) 23952b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 239645e4fd26SMartin KaFai Lau } 2397a68886a6SDavid Ahern rcu_read_unlock(); 239845e4fd26SMartin KaFai Lau } 239945e4fd26SMartin KaFai Lau } 240045e4fd26SMartin KaFai Lau 240145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 240245e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 240345e4fd26SMartin KaFai Lau { 240445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds 240742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2408e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 240981aded24SDavid S. Miller { 241081aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 241181aded24SDavid S. Miller struct dst_entry *dst; 2412dc92095dSMaciej Żenczykowski struct flowi6 fl6 = { 2413dc92095dSMaciej Żenczykowski .flowi6_oif = oif, 2414dc92095dSMaciej Żenczykowski .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), 2415dc92095dSMaciej Żenczykowski .daddr = iph->daddr, 2416dc92095dSMaciej Żenczykowski .saddr = iph->saddr, 2417dc92095dSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 2418dc92095dSMaciej Żenczykowski .flowi6_uid = uid, 2419dc92095dSMaciej Żenczykowski }; 242081aded24SDavid S. Miller 242181aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 242281aded24SDavid S. Miller if (!dst->error) 242345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 242481aded24SDavid S. Miller dst_release(dst); 242581aded24SDavid S. Miller } 242681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 242781aded24SDavid S. Miller 242881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 242981aded24SDavid S. Miller { 24307ddacfa5SDavid Ahern int oif = sk->sk_bound_dev_if; 243133c162a9SMartin KaFai Lau struct dst_entry *dst; 243233c162a9SMartin KaFai Lau 24337ddacfa5SDavid Ahern if (!oif && skb->dev) 24347ddacfa5SDavid Ahern oif = l3mdev_master_ifindex(skb->dev); 24357ddacfa5SDavid Ahern 24367ddacfa5SDavid Ahern ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); 243733c162a9SMartin KaFai Lau 243833c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 243933c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 244033c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 244133c162a9SMartin KaFai Lau return; 244233c162a9SMartin KaFai Lau 244333c162a9SMartin KaFai Lau bh_lock_sock(sk); 244433c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 244533c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 244633c162a9SMartin KaFai Lau bh_unlock_sock(sk); 244781aded24SDavid S. Miller } 244881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 244981aded24SDavid S. Miller 24507d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, 24517d6850f7SAlexey Kodanev const struct flowi6 *fl6) 24527d6850f7SAlexey Kodanev { 24537d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 24547d6850f7SAlexey Kodanev struct ipv6_pinfo *np = inet6_sk(sk); 24557d6850f7SAlexey Kodanev #endif 24567d6850f7SAlexey Kodanev 24577d6850f7SAlexey Kodanev ip6_dst_store(sk, dst, 24587d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? 24597d6850f7SAlexey Kodanev &sk->sk_v6_daddr : NULL, 24607d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES 24617d6850f7SAlexey Kodanev ipv6_addr_equal(&fl6->saddr, &np->saddr) ? 24627d6850f7SAlexey Kodanev &np->saddr : 24637d6850f7SAlexey Kodanev #endif 24647d6850f7SAlexey Kodanev NULL); 24657d6850f7SAlexey Kodanev } 24667d6850f7SAlexey Kodanev 24679b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res, 24680b34eb00SDavid Ahern struct flowi6 *fl6, 24690b34eb00SDavid Ahern const struct in6_addr *gw, 24700b34eb00SDavid Ahern struct rt6_info **ret) 24710b34eb00SDavid Ahern { 24729b6b35abSDavid Ahern const struct fib6_nh *nh = res->nh; 24739b6b35abSDavid Ahern 24740b34eb00SDavid Ahern if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || 24750b34eb00SDavid Ahern fl6->flowi6_oif != nh->fib_nh_dev->ifindex) 24760b34eb00SDavid Ahern return false; 24770b34eb00SDavid Ahern 24780b34eb00SDavid Ahern /* rt_cache's gateway might be different from its 'parent' 24790b34eb00SDavid Ahern * in the case of an ip redirect. 24800b34eb00SDavid Ahern * So we keep searching in the exception table if the gateway 24810b34eb00SDavid Ahern * is different. 24820b34eb00SDavid Ahern */ 24830b34eb00SDavid Ahern if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { 24840b34eb00SDavid Ahern struct rt6_info *rt_cache; 24850b34eb00SDavid Ahern 24869b6b35abSDavid Ahern rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); 24870b34eb00SDavid Ahern if (rt_cache && 24880b34eb00SDavid Ahern ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { 24890b34eb00SDavid Ahern *ret = rt_cache; 24900b34eb00SDavid Ahern return true; 24910b34eb00SDavid Ahern } 24920b34eb00SDavid Ahern return false; 24930b34eb00SDavid Ahern } 24940b34eb00SDavid Ahern return true; 24950b34eb00SDavid Ahern } 24960b34eb00SDavid Ahern 2497b55b76b2SDuan Jiong /* Handle redirects */ 2498b55b76b2SDuan Jiong struct ip6rd_flowi { 2499b55b76b2SDuan Jiong struct flowi6 fl6; 2500b55b76b2SDuan Jiong struct in6_addr gateway; 2501b55b76b2SDuan Jiong }; 2502b55b76b2SDuan Jiong 2503b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2504b55b76b2SDuan Jiong struct fib6_table *table, 2505b55b76b2SDuan Jiong struct flowi6 *fl6, 2506b75cc8f9SDavid Ahern const struct sk_buff *skb, 2507b55b76b2SDuan Jiong int flags) 2508b55b76b2SDuan Jiong { 2509b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 25100b34eb00SDavid Ahern struct rt6_info *ret = NULL; 25119b6b35abSDavid Ahern struct fib6_result res = {}; 25128d1c802bSDavid Ahern struct fib6_info *rt; 2513b55b76b2SDuan Jiong struct fib6_node *fn; 2514b55b76b2SDuan Jiong 2515b55b76b2SDuan Jiong /* Get the "current" route for this destination and 251667c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2517b55b76b2SDuan Jiong * 2518b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2519b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2520b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2521b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2522b55b76b2SDuan Jiong * routes. 2523b55b76b2SDuan Jiong */ 2524b55b76b2SDuan Jiong 252566f5d6ceSWei Wang rcu_read_lock(); 25266454743bSDavid Ahern fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2527b55b76b2SDuan Jiong restart: 252866f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 25299b6b35abSDavid Ahern res.f6i = rt; 25309b6b35abSDavid Ahern res.nh = &rt->fib6_nh; 25319b6b35abSDavid Ahern 253214895687SDavid Ahern if (fib6_check_expired(rt)) 2533b55b76b2SDuan Jiong continue; 253493c2fb25SDavid Ahern if (rt->fib6_flags & RTF_REJECT) 2535b55b76b2SDuan Jiong break; 25369b6b35abSDavid Ahern if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret)) 25370b34eb00SDavid Ahern goto out; 2538b55b76b2SDuan Jiong } 2539b55b76b2SDuan Jiong 2540b55b76b2SDuan Jiong if (!rt) 2541421842edSDavid Ahern rt = net->ipv6.fib6_null_entry; 254293c2fb25SDavid Ahern else if (rt->fib6_flags & RTF_REJECT) { 254323fb93a4SDavid Ahern ret = net->ipv6.ip6_null_entry; 2544b0a1ba59SMartin KaFai Lau goto out; 2545b0a1ba59SMartin KaFai Lau } 2546b0a1ba59SMartin KaFai Lau 2547421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 2548a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2549a3c00e46SMartin KaFai Lau if (fn) 2550a3c00e46SMartin KaFai Lau goto restart; 2551b55b76b2SDuan Jiong } 2552a3c00e46SMartin KaFai Lau 25539b6b35abSDavid Ahern res.f6i = rt; 25549b6b35abSDavid Ahern res.nh = &rt->fib6_nh; 2555b0a1ba59SMartin KaFai Lau out: 25567d21fec9SDavid Ahern if (ret) { 255710585b43SDavid Ahern ip6_hold_safe(net, &ret); 25587d21fec9SDavid Ahern } else { 25597d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 25607d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 25619b6b35abSDavid Ahern ret = ip6_create_rt_rcu(&res); 25627d21fec9SDavid Ahern } 2563b55b76b2SDuan Jiong 256466f5d6ceSWei Wang rcu_read_unlock(); 2565b55b76b2SDuan Jiong 25668ff2e5b2SDavid Ahern trace_fib6_table_lookup(net, &res, table, fl6); 256723fb93a4SDavid Ahern return ret; 2568b55b76b2SDuan Jiong }; 2569b55b76b2SDuan Jiong 2570b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2571b55b76b2SDuan Jiong const struct flowi6 *fl6, 2572b75cc8f9SDavid Ahern const struct sk_buff *skb, 2573b55b76b2SDuan Jiong const struct in6_addr *gateway) 2574b55b76b2SDuan Jiong { 2575b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2576b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2577b55b76b2SDuan Jiong 2578b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2579b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2580b55b76b2SDuan Jiong 2581b75cc8f9SDavid Ahern return fib6_rule_lookup(net, &rdfl.fl6, skb, 2582b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2583b55b76b2SDuan Jiong } 2584b55b76b2SDuan Jiong 2585e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2586e2d118a1SLorenzo Colitti kuid_t uid) 25873a5ad2eeSDavid S. Miller { 25883a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 25893a5ad2eeSDavid S. Miller struct dst_entry *dst; 25901f7f10acSMaciej Żenczykowski struct flowi6 fl6 = { 25911f7f10acSMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 25921f7f10acSMaciej Żenczykowski .flowi6_oif = oif, 25931f7f10acSMaciej Żenczykowski .flowi6_mark = mark, 25941f7f10acSMaciej Żenczykowski .daddr = iph->daddr, 25951f7f10acSMaciej Żenczykowski .saddr = iph->saddr, 25961f7f10acSMaciej Żenczykowski .flowlabel = ip6_flowinfo(iph), 25971f7f10acSMaciej Żenczykowski .flowi6_uid = uid, 25981f7f10acSMaciej Żenczykowski }; 25993a5ad2eeSDavid S. Miller 2600b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); 26016700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 26023a5ad2eeSDavid S. Miller dst_release(dst); 26033a5ad2eeSDavid S. Miller } 26043a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 26053a5ad2eeSDavid S. Miller 2606d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) 2607c92a59ecSDuan Jiong { 2608c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2609c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2610c92a59ecSDuan Jiong struct dst_entry *dst; 26110b26fb17SMaciej Żenczykowski struct flowi6 fl6 = { 26120b26fb17SMaciej Żenczykowski .flowi6_iif = LOOPBACK_IFINDEX, 26130b26fb17SMaciej Żenczykowski .flowi6_oif = oif, 26140b26fb17SMaciej Żenczykowski .daddr = msg->dest, 26150b26fb17SMaciej Żenczykowski .saddr = iph->daddr, 26160b26fb17SMaciej Żenczykowski .flowi6_uid = sock_net_uid(net, NULL), 26170b26fb17SMaciej Żenczykowski }; 2618c92a59ecSDuan Jiong 2619b75cc8f9SDavid Ahern dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); 2620c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2621c92a59ecSDuan Jiong dst_release(dst); 2622c92a59ecSDuan Jiong } 2623c92a59ecSDuan Jiong 26243a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 26253a5ad2eeSDavid S. Miller { 2626e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2627e2d118a1SLorenzo Colitti sk->sk_uid); 26283a5ad2eeSDavid S. Miller } 26293a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 26303a5ad2eeSDavid S. Miller 26310dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 26321da177e4SLinus Torvalds { 26330dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 26340dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 26350dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 26360dbaee3bSDavid S. Miller 26371da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 26381da177e4SLinus Torvalds 26395578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 26405578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds /* 26431da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 26441da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 26451da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 26461da177e4SLinus Torvalds * rely only on pmtu discovery" 26471da177e4SLinus Torvalds */ 26481da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 26491da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 26501da177e4SLinus Torvalds return mtu; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 2653ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2654d33e4553SDavid S. Miller { 2655d33e4553SDavid S. Miller struct inet6_dev *idev; 2656d4ead6b3SDavid Ahern unsigned int mtu; 2657618f9bc7SSteffen Klassert 26584b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 26594b32b5adSMartin KaFai Lau if (mtu) 26604b32b5adSMartin KaFai Lau goto out; 26614b32b5adSMartin KaFai Lau 2662618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2663d33e4553SDavid S. Miller 2664d33e4553SDavid S. Miller rcu_read_lock(); 2665d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2666d33e4553SDavid S. Miller if (idev) 2667d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2668d33e4553SDavid S. Miller rcu_read_unlock(); 2669d33e4553SDavid S. Miller 267030f78d8eSEric Dumazet out: 267114972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 267214972cbdSRoopa Prabhu 267314972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2674d33e4553SDavid S. Miller } 2675d33e4553SDavid S. Miller 2676901731b8SDavid Ahern /* MTU selection: 2677901731b8SDavid Ahern * 1. mtu on route is locked - use it 2678901731b8SDavid Ahern * 2. mtu from nexthop exception 2679901731b8SDavid Ahern * 3. mtu from egress device 2680901731b8SDavid Ahern * 2681901731b8SDavid Ahern * based on ip6_dst_mtu_forward and exception logic of 2682901731b8SDavid Ahern * rt6_find_cached_rt; called with rcu_read_lock 2683901731b8SDavid Ahern */ 2684b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res, 2685b748f260SDavid Ahern const struct in6_addr *daddr, 2686b748f260SDavid Ahern const struct in6_addr *saddr) 2687901731b8SDavid Ahern { 2688b748f260SDavid Ahern const struct fib6_nh *nh = res->nh; 2689b748f260SDavid Ahern struct fib6_info *f6i = res->f6i; 2690901731b8SDavid Ahern struct inet6_dev *idev; 2691510e2cedSWei Wang struct rt6_info *rt; 2692901731b8SDavid Ahern u32 mtu = 0; 2693901731b8SDavid Ahern 2694901731b8SDavid Ahern if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { 2695901731b8SDavid Ahern mtu = f6i->fib6_pmtu; 2696901731b8SDavid Ahern if (mtu) 2697901731b8SDavid Ahern goto out; 2698901731b8SDavid Ahern } 2699901731b8SDavid Ahern 2700510e2cedSWei Wang rt = rt6_find_cached_rt(res, daddr, saddr); 2701510e2cedSWei Wang if (unlikely(rt)) { 2702510e2cedSWei Wang mtu = dst_metric_raw(&rt->dst, RTAX_MTU); 2703510e2cedSWei Wang } else { 2704b748f260SDavid Ahern struct net_device *dev = nh->fib_nh_dev; 2705901731b8SDavid Ahern 2706901731b8SDavid Ahern mtu = IPV6_MIN_MTU; 2707901731b8SDavid Ahern idev = __in6_dev_get(dev); 2708901731b8SDavid Ahern if (idev && idev->cnf.mtu6 > mtu) 2709901731b8SDavid Ahern mtu = idev->cnf.mtu6; 2710901731b8SDavid Ahern } 2711901731b8SDavid Ahern 2712901731b8SDavid Ahern mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 2713901731b8SDavid Ahern out: 2714b748f260SDavid Ahern return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); 2715901731b8SDavid Ahern } 2716901731b8SDavid Ahern 27173b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 271887a11578SDavid S. Miller struct flowi6 *fl6) 27191da177e4SLinus Torvalds { 272087a11578SDavid S. Miller struct dst_entry *dst; 27211da177e4SLinus Torvalds struct rt6_info *rt; 27221da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2723c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 27241da177e4SLinus Torvalds 272538308473SDavid S. Miller if (unlikely(!idev)) 2726122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 27271da177e4SLinus Torvalds 2728ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 272938308473SDavid S. Miller if (unlikely(!rt)) { 27301da177e4SLinus Torvalds in6_dev_put(idev); 273187a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 27321da177e4SLinus Torvalds goto out; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27358e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2736588753f1SBrendan McGrath rt->dst.input = ip6_input; 27378e2ec639SYan, Zheng rt->dst.output = ip6_output; 2738550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 273987a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 27408e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 27418e2ec639SYan, Zheng rt->rt6i_idev = idev; 274214edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 27431da177e4SLinus Torvalds 27444c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 2745587fea74SWei Wang * do proper release of the net_device 2746587fea74SWei Wang */ 2747587fea74SWei Wang rt6_uncached_list_add(rt); 274881eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 27491da177e4SLinus Torvalds 275087a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 275187a11578SDavid S. Miller 27521da177e4SLinus Torvalds out: 275387a11578SDavid S. Miller return dst; 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds 2756569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 27571da177e4SLinus Torvalds { 275886393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 27597019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 27607019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 27617019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 27627019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 27637019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2764fc66f95cSEric Dumazet int entries; 27651da177e4SLinus Torvalds 2766fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 276749a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2768fc66f95cSEric Dumazet entries <= rt_max_size) 27691da177e4SLinus Torvalds goto out; 27701da177e4SLinus Torvalds 27716891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 277214956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2773fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2774fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 27757019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 27761da177e4SLinus Torvalds out: 27777019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2778fc66f95cSEric Dumazet return entries > rt_max_size; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 27818c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 27828c14586fSDavid Ahern struct fib6_config *cfg, 2783f4797b33SDavid Ahern const struct in6_addr *gw_addr, 2784f4797b33SDavid Ahern u32 tbid, int flags) 27858c14586fSDavid Ahern { 27868c14586fSDavid Ahern struct flowi6 fl6 = { 27878c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 27888c14586fSDavid Ahern .daddr = *gw_addr, 27898c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 27908c14586fSDavid Ahern }; 27918c14586fSDavid Ahern struct fib6_table *table; 27928c14586fSDavid Ahern struct rt6_info *rt; 27938c14586fSDavid Ahern 2794f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 27958c14586fSDavid Ahern if (!table) 27968c14586fSDavid Ahern return NULL; 27978c14586fSDavid Ahern 27988c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 27998c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 28008c14586fSDavid Ahern 2801f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 2802b75cc8f9SDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); 28038c14586fSDavid Ahern 28048c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 28058c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 28068c14586fSDavid Ahern ip6_rt_put(rt); 28078c14586fSDavid Ahern rt = NULL; 28088c14586fSDavid Ahern } 28098c14586fSDavid Ahern 28108c14586fSDavid Ahern return rt; 28118c14586fSDavid Ahern } 28128c14586fSDavid Ahern 2813fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 2814fc1e64e1SDavid Ahern struct fib6_config *cfg, 28159fbb704cSDavid Ahern const struct net_device *dev, 2816fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 2817fc1e64e1SDavid Ahern { 281844750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 2819fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 2820fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 2821bf1dc8baSPaolo Abeni struct fib6_info *from; 2822fc1e64e1SDavid Ahern struct rt6_info *grt; 2823fc1e64e1SDavid Ahern int err; 2824fc1e64e1SDavid Ahern 2825fc1e64e1SDavid Ahern err = 0; 2826fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 2827fc1e64e1SDavid Ahern if (grt) { 2828bf1dc8baSPaolo Abeni rcu_read_lock(); 2829bf1dc8baSPaolo Abeni from = rcu_dereference(grt->from); 283058e354c0SDavid Ahern if (!grt->dst.error && 28314ed591c8SDavid Ahern /* ignore match if it is the default route */ 2832bf1dc8baSPaolo Abeni from && !ipv6_addr_any(&from->fib6_dst.addr) && 283358e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 283444750f84SDavid Ahern NL_SET_ERR_MSG(extack, 283544750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 2836fc1e64e1SDavid Ahern err = -EINVAL; 2837fc1e64e1SDavid Ahern } 2838bf1dc8baSPaolo Abeni rcu_read_unlock(); 2839fc1e64e1SDavid Ahern 2840fc1e64e1SDavid Ahern ip6_rt_put(grt); 2841fc1e64e1SDavid Ahern } 2842fc1e64e1SDavid Ahern 2843fc1e64e1SDavid Ahern return err; 2844fc1e64e1SDavid Ahern } 2845fc1e64e1SDavid Ahern 28461edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 28471edce99fSDavid Ahern struct fib6_config *cfg, 28481edce99fSDavid Ahern struct net_device **_dev, 28491edce99fSDavid Ahern struct inet6_dev **idev) 28501edce99fSDavid Ahern { 28511edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 28521edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 28531edce99fSDavid Ahern struct rt6_info *grt = NULL; 28541edce99fSDavid Ahern int err = -EHOSTUNREACH; 28551edce99fSDavid Ahern 28561edce99fSDavid Ahern if (cfg->fc_table) { 2857f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 2858f4797b33SDavid Ahern 2859f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 2860f4797b33SDavid Ahern cfg->fc_table, flags); 28611edce99fSDavid Ahern if (grt) { 28621edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 28631edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 28641edce99fSDavid Ahern ip6_rt_put(grt); 28651edce99fSDavid Ahern grt = NULL; 28661edce99fSDavid Ahern } 28671edce99fSDavid Ahern } 28681edce99fSDavid Ahern } 28691edce99fSDavid Ahern 28701edce99fSDavid Ahern if (!grt) 2871b75cc8f9SDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); 28721edce99fSDavid Ahern 28731edce99fSDavid Ahern if (!grt) 28741edce99fSDavid Ahern goto out; 28751edce99fSDavid Ahern 28761edce99fSDavid Ahern if (dev) { 28771edce99fSDavid Ahern if (dev != grt->dst.dev) { 28781edce99fSDavid Ahern ip6_rt_put(grt); 28791edce99fSDavid Ahern goto out; 28801edce99fSDavid Ahern } 28811edce99fSDavid Ahern } else { 28821edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 28831edce99fSDavid Ahern *idev = grt->rt6i_idev; 28841edce99fSDavid Ahern dev_hold(dev); 28851edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 28861edce99fSDavid Ahern } 28871edce99fSDavid Ahern 28881edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 28891edce99fSDavid Ahern err = 0; 28901edce99fSDavid Ahern 28911edce99fSDavid Ahern ip6_rt_put(grt); 28921edce99fSDavid Ahern 28931edce99fSDavid Ahern out: 28941edce99fSDavid Ahern return err; 28951edce99fSDavid Ahern } 28961edce99fSDavid Ahern 28979fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, 28989fbb704cSDavid Ahern struct net_device **_dev, struct inet6_dev **idev, 28999fbb704cSDavid Ahern struct netlink_ext_ack *extack) 29009fbb704cSDavid Ahern { 29019fbb704cSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 29029fbb704cSDavid Ahern int gwa_type = ipv6_addr_type(gw_addr); 2903232378e8SDavid Ahern bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; 29049fbb704cSDavid Ahern const struct net_device *dev = *_dev; 2905232378e8SDavid Ahern bool need_addr_check = !dev; 29069fbb704cSDavid Ahern int err = -EINVAL; 29079fbb704cSDavid Ahern 29089fbb704cSDavid Ahern /* if gw_addr is local we will fail to detect this in case 29099fbb704cSDavid Ahern * address is still TENTATIVE (DAD in progress). rt6_lookup() 29109fbb704cSDavid Ahern * will return already-added prefix route via interface that 29119fbb704cSDavid Ahern * prefix route was assigned to, which might be non-loopback. 29129fbb704cSDavid Ahern */ 2913232378e8SDavid Ahern if (dev && 2914232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2915232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 29169fbb704cSDavid Ahern goto out; 29179fbb704cSDavid Ahern } 29189fbb704cSDavid Ahern 29199fbb704cSDavid Ahern if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { 29209fbb704cSDavid Ahern /* IPv6 strictly inhibits using not link-local 29219fbb704cSDavid Ahern * addresses as nexthop address. 29229fbb704cSDavid Ahern * Otherwise, router will not able to send redirects. 29239fbb704cSDavid Ahern * It is very good, but in some (rare!) circumstances 29249fbb704cSDavid Ahern * (SIT, PtP, NBMA NOARP links) it is handy to allow 29259fbb704cSDavid Ahern * some exceptions. --ANK 29269fbb704cSDavid Ahern * We allow IPv4-mapped nexthops to support RFC4798-type 29279fbb704cSDavid Ahern * addressing 29289fbb704cSDavid Ahern */ 29299fbb704cSDavid Ahern if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { 29309fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 29319fbb704cSDavid Ahern goto out; 29329fbb704cSDavid Ahern } 29339fbb704cSDavid Ahern 29349fbb704cSDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) 29359fbb704cSDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, extack); 29369fbb704cSDavid Ahern else 29379fbb704cSDavid Ahern err = ip6_route_check_nh(net, cfg, _dev, idev); 29389fbb704cSDavid Ahern 29399fbb704cSDavid Ahern if (err) 29409fbb704cSDavid Ahern goto out; 29419fbb704cSDavid Ahern } 29429fbb704cSDavid Ahern 29439fbb704cSDavid Ahern /* reload in case device was changed */ 29449fbb704cSDavid Ahern dev = *_dev; 29459fbb704cSDavid Ahern 29469fbb704cSDavid Ahern err = -EINVAL; 29479fbb704cSDavid Ahern if (!dev) { 29489fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 29499fbb704cSDavid Ahern goto out; 29509fbb704cSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 29519fbb704cSDavid Ahern NL_SET_ERR_MSG(extack, 29529fbb704cSDavid Ahern "Egress device can not be loopback device for this route"); 29539fbb704cSDavid Ahern goto out; 29549fbb704cSDavid Ahern } 2955232378e8SDavid Ahern 2956232378e8SDavid Ahern /* if we did not check gw_addr above, do so now that the 2957232378e8SDavid Ahern * egress device has been resolved. 2958232378e8SDavid Ahern */ 2959232378e8SDavid Ahern if (need_addr_check && 2960232378e8SDavid Ahern ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { 2961232378e8SDavid Ahern NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); 2962232378e8SDavid Ahern goto out; 2963232378e8SDavid Ahern } 2964232378e8SDavid Ahern 29659fbb704cSDavid Ahern err = 0; 29669fbb704cSDavid Ahern out: 29679fbb704cSDavid Ahern return err; 29689fbb704cSDavid Ahern } 29699fbb704cSDavid Ahern 297083c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) 297183c44251SDavid Ahern { 297283c44251SDavid Ahern if ((flags & RTF_REJECT) || 297383c44251SDavid Ahern (dev && (dev->flags & IFF_LOOPBACK) && 297483c44251SDavid Ahern !(addr_type & IPV6_ADDR_LOOPBACK) && 297583c44251SDavid Ahern !(flags & RTF_LOCAL))) 297683c44251SDavid Ahern return true; 297783c44251SDavid Ahern 297883c44251SDavid Ahern return false; 297983c44251SDavid Ahern } 298083c44251SDavid Ahern 298183c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, 298283c44251SDavid Ahern struct fib6_config *cfg, gfp_t gfp_flags, 298383c44251SDavid Ahern struct netlink_ext_ack *extack) 298483c44251SDavid Ahern { 298583c44251SDavid Ahern struct net_device *dev = NULL; 298683c44251SDavid Ahern struct inet6_dev *idev = NULL; 298783c44251SDavid Ahern int addr_type; 298883c44251SDavid Ahern int err; 298983c44251SDavid Ahern 2990f1741730SDavid Ahern fib6_nh->fib_nh_family = AF_INET6; 2991f1741730SDavid Ahern 299283c44251SDavid Ahern err = -ENODEV; 299383c44251SDavid Ahern if (cfg->fc_ifindex) { 299483c44251SDavid Ahern dev = dev_get_by_index(net, cfg->fc_ifindex); 299583c44251SDavid Ahern if (!dev) 299683c44251SDavid Ahern goto out; 299783c44251SDavid Ahern idev = in6_dev_get(dev); 299883c44251SDavid Ahern if (!idev) 299983c44251SDavid Ahern goto out; 300083c44251SDavid Ahern } 300183c44251SDavid Ahern 300283c44251SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 300383c44251SDavid Ahern if (!dev) { 300483c44251SDavid Ahern NL_SET_ERR_MSG(extack, 300583c44251SDavid Ahern "Nexthop device required for onlink"); 300683c44251SDavid Ahern goto out; 300783c44251SDavid Ahern } 300883c44251SDavid Ahern 300983c44251SDavid Ahern if (!(dev->flags & IFF_UP)) { 301083c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 301183c44251SDavid Ahern err = -ENETDOWN; 301283c44251SDavid Ahern goto out; 301383c44251SDavid Ahern } 301483c44251SDavid Ahern 3015ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; 301683c44251SDavid Ahern } 301783c44251SDavid Ahern 3018ad1601aeSDavid Ahern fib6_nh->fib_nh_weight = 1; 301983c44251SDavid Ahern 302083c44251SDavid Ahern /* We cannot add true routes via loopback here, 302183c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 302283c44251SDavid Ahern */ 302383c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 302483c44251SDavid Ahern if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { 302583c44251SDavid Ahern /* hold loopback dev/idev if we haven't done so. */ 302683c44251SDavid Ahern if (dev != net->loopback_dev) { 302783c44251SDavid Ahern if (dev) { 302883c44251SDavid Ahern dev_put(dev); 302983c44251SDavid Ahern in6_dev_put(idev); 303083c44251SDavid Ahern } 303183c44251SDavid Ahern dev = net->loopback_dev; 303283c44251SDavid Ahern dev_hold(dev); 303383c44251SDavid Ahern idev = in6_dev_get(dev); 303483c44251SDavid Ahern if (!idev) { 303583c44251SDavid Ahern err = -ENODEV; 303683c44251SDavid Ahern goto out; 303783c44251SDavid Ahern } 303883c44251SDavid Ahern } 303983c44251SDavid Ahern goto set_dev; 304083c44251SDavid Ahern } 304183c44251SDavid Ahern 304283c44251SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) { 304383c44251SDavid Ahern err = ip6_validate_gw(net, cfg, &dev, &idev, extack); 304483c44251SDavid Ahern if (err) 304583c44251SDavid Ahern goto out; 304683c44251SDavid Ahern 3047ad1601aeSDavid Ahern fib6_nh->fib_nh_gw6 = cfg->fc_gateway; 3048bdf00467SDavid Ahern fib6_nh->fib_nh_gw_family = AF_INET6; 304983c44251SDavid Ahern } 305083c44251SDavid Ahern 305183c44251SDavid Ahern err = -ENODEV; 305283c44251SDavid Ahern if (!dev) 305383c44251SDavid Ahern goto out; 305483c44251SDavid Ahern 305583c44251SDavid Ahern if (idev->cnf.disable_ipv6) { 305683c44251SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); 305783c44251SDavid Ahern err = -EACCES; 305883c44251SDavid Ahern goto out; 305983c44251SDavid Ahern } 306083c44251SDavid Ahern 306183c44251SDavid Ahern if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { 306283c44251SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 306383c44251SDavid Ahern err = -ENETDOWN; 306483c44251SDavid Ahern goto out; 306583c44251SDavid Ahern } 306683c44251SDavid Ahern 306783c44251SDavid Ahern if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && 306883c44251SDavid Ahern !netif_carrier_ok(dev)) 3069ad1601aeSDavid Ahern fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; 307083c44251SDavid Ahern 3071*f40b6ae2SDavid Ahern fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); 3072*f40b6ae2SDavid Ahern if (!fib6_nh->rt6i_pcpu) { 3073*f40b6ae2SDavid Ahern err = -ENOMEM; 3074*f40b6ae2SDavid Ahern goto out; 3075*f40b6ae2SDavid Ahern } 3076*f40b6ae2SDavid Ahern 3077979e276eSDavid Ahern err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, 3078979e276eSDavid Ahern cfg->fc_encap_type, cfg, gfp_flags, extack); 3079979e276eSDavid Ahern if (err) 3080979e276eSDavid Ahern goto out; 308183c44251SDavid Ahern set_dev: 3082ad1601aeSDavid Ahern fib6_nh->fib_nh_dev = dev; 3083f1741730SDavid Ahern fib6_nh->fib_nh_oif = dev->ifindex; 308483c44251SDavid Ahern err = 0; 308583c44251SDavid Ahern out: 308683c44251SDavid Ahern if (idev) 308783c44251SDavid Ahern in6_dev_put(idev); 308883c44251SDavid Ahern 308983c44251SDavid Ahern if (err) { 3090ad1601aeSDavid Ahern lwtstate_put(fib6_nh->fib_nh_lws); 3091ad1601aeSDavid Ahern fib6_nh->fib_nh_lws = NULL; 309283c44251SDavid Ahern if (dev) 309383c44251SDavid Ahern dev_put(dev); 309483c44251SDavid Ahern } 309583c44251SDavid Ahern 309683c44251SDavid Ahern return err; 309783c44251SDavid Ahern } 309883c44251SDavid Ahern 3099dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh) 3100dac7d0f2SDavid Ahern { 3101*f40b6ae2SDavid Ahern if (fib6_nh->rt6i_pcpu) { 3102*f40b6ae2SDavid Ahern int cpu; 3103*f40b6ae2SDavid Ahern 3104*f40b6ae2SDavid Ahern for_each_possible_cpu(cpu) { 3105*f40b6ae2SDavid Ahern struct rt6_info **ppcpu_rt; 3106*f40b6ae2SDavid Ahern struct rt6_info *pcpu_rt; 3107*f40b6ae2SDavid Ahern 3108*f40b6ae2SDavid Ahern ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 3109*f40b6ae2SDavid Ahern pcpu_rt = *ppcpu_rt; 3110*f40b6ae2SDavid Ahern if (pcpu_rt) { 3111*f40b6ae2SDavid Ahern dst_dev_put(&pcpu_rt->dst); 3112*f40b6ae2SDavid Ahern dst_release(&pcpu_rt->dst); 3113*f40b6ae2SDavid Ahern *ppcpu_rt = NULL; 3114*f40b6ae2SDavid Ahern } 3115*f40b6ae2SDavid Ahern } 3116*f40b6ae2SDavid Ahern 3117*f40b6ae2SDavid Ahern free_percpu(fib6_nh->rt6i_pcpu); 3118*f40b6ae2SDavid Ahern } 3119*f40b6ae2SDavid Ahern 3120979e276eSDavid Ahern fib_nh_common_release(&fib6_nh->nh_common); 3121dac7d0f2SDavid Ahern } 3122dac7d0f2SDavid Ahern 31238d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, 3124acb54e3cSDavid Ahern gfp_t gfp_flags, 3125333c4301SDavid Ahern struct netlink_ext_ack *extack) 31261da177e4SLinus Torvalds { 31275578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 31288d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3129c71099acSThomas Graf struct fib6_table *table; 31308c5b83f0SRoopa Prabhu int err = -EINVAL; 313183c44251SDavid Ahern int addr_type; 31321da177e4SLinus Torvalds 3133557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 3134d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 3135d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 3136557c44beSDavid Ahern goto out; 3137d5d531cbSDavid Ahern } 3138557c44beSDavid Ahern 31392ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 31402ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 31412ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 31422ea2352eSWei Wang goto out; 31432ea2352eSWei Wang } 31442ea2352eSWei Wang 3145e8478e80SDavid Ahern if (cfg->fc_type > RTN_MAX) { 3146e8478e80SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid route type"); 3147e8478e80SDavid Ahern goto out; 3148e8478e80SDavid Ahern } 3149e8478e80SDavid Ahern 3150d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 3151d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 31528c5b83f0SRoopa Prabhu goto out; 3153d5d531cbSDavid Ahern } 3154d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 3155d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 3156d5d531cbSDavid Ahern goto out; 3157d5d531cbSDavid Ahern } 31581da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 3159d5d531cbSDavid Ahern if (cfg->fc_src_len) { 3160d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 3161d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 31628c5b83f0SRoopa Prabhu goto out; 3163d5d531cbSDavid Ahern } 31641da177e4SLinus Torvalds #endif 3165fc1e64e1SDavid Ahern 3166c71099acSThomas Graf err = -ENOBUFS; 316738308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 3168d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 3169d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 317038308473SDavid S. Miller if (!table) { 3171f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 3172d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3173d71314b4SMatti Vaittinen } 3174d71314b4SMatti Vaittinen } else { 3175d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 3176d71314b4SMatti Vaittinen } 317738308473SDavid S. Miller 317838308473SDavid S. Miller if (!table) 3179c71099acSThomas Graf goto out; 3180c71099acSThomas Graf 31811da177e4SLinus Torvalds err = -ENOMEM; 318293531c67SDavid Ahern rt = fib6_info_alloc(gfp_flags); 318393531c67SDavid Ahern if (!rt) 31841da177e4SLinus Torvalds goto out; 318593531c67SDavid Ahern 3186d7e774f3SDavid Ahern rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, 3187d7e774f3SDavid Ahern extack); 3188767a2217SDavid Ahern if (IS_ERR(rt->fib6_metrics)) { 3189767a2217SDavid Ahern err = PTR_ERR(rt->fib6_metrics); 3190fda21d46SEric Dumazet /* Do not leave garbage there. */ 3191fda21d46SEric Dumazet rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; 3192767a2217SDavid Ahern goto out; 3193767a2217SDavid Ahern } 3194767a2217SDavid Ahern 319593531c67SDavid Ahern if (cfg->fc_flags & RTF_ADDRCONF) 319693531c67SDavid Ahern rt->dst_nocount = true; 31971da177e4SLinus Torvalds 31981716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 319914895687SDavid Ahern fib6_set_expires(rt, jiffies + 32001716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 32011716a961SGao feng else 320214895687SDavid Ahern fib6_clean_expires(rt); 32031da177e4SLinus Torvalds 320486872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 320586872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 320693c2fb25SDavid Ahern rt->fib6_protocol = cfg->fc_protocol; 320786872cb5SThomas Graf 320883c44251SDavid Ahern rt->fib6_table = table; 320983c44251SDavid Ahern rt->fib6_metric = cfg->fc_metric; 321083c44251SDavid Ahern rt->fib6_type = cfg->fc_type; 32112b2450caSDavid Ahern rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; 321219e42e45SRoopa Prabhu 321393c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 321493c2fb25SDavid Ahern rt->fib6_dst.plen = cfg->fc_dst_len; 321593c2fb25SDavid Ahern if (rt->fib6_dst.plen == 128) 32163b6761d1SDavid Ahern rt->dst_host = true; 32171da177e4SLinus Torvalds 32181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 321993c2fb25SDavid Ahern ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); 322093c2fb25SDavid Ahern rt->fib6_src.plen = cfg->fc_src_len; 32211da177e4SLinus Torvalds #endif 322283c44251SDavid Ahern err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack); 32231da177e4SLinus Torvalds if (err) 32241da177e4SLinus Torvalds goto out; 32259fbb704cSDavid Ahern 322683c44251SDavid Ahern /* We cannot add true routes via loopback here, 322783c44251SDavid Ahern * they would result in kernel looping; promote them to reject routes 322883c44251SDavid Ahern */ 322983c44251SDavid Ahern addr_type = ipv6_addr_type(&cfg->fc_dst); 3230ad1601aeSDavid Ahern if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type)) 323183c44251SDavid Ahern rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; 3232955ec4cbSDavid Ahern 3233c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 323483c44251SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 323583c44251SDavid Ahern 3236c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 3237d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 3238c3968a85SDaniel Walter err = -EINVAL; 3239c3968a85SDaniel Walter goto out; 3240c3968a85SDaniel Walter } 324193c2fb25SDavid Ahern rt->fib6_prefsrc.addr = cfg->fc_prefsrc; 324293c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 128; 3243c3968a85SDaniel Walter } else 324493c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 3245c3968a85SDaniel Walter 32468c5b83f0SRoopa Prabhu return rt; 32471da177e4SLinus Torvalds out: 324893531c67SDavid Ahern fib6_info_release(rt); 32498c5b83f0SRoopa Prabhu return ERR_PTR(err); 32506b9ea5a6SRoopa Prabhu } 32516b9ea5a6SRoopa Prabhu 3252acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, 3253333c4301SDavid Ahern struct netlink_ext_ack *extack) 32546b9ea5a6SRoopa Prabhu { 32558d1c802bSDavid Ahern struct fib6_info *rt; 32566b9ea5a6SRoopa Prabhu int err; 32576b9ea5a6SRoopa Prabhu 3258acb54e3cSDavid Ahern rt = ip6_route_info_create(cfg, gfp_flags, extack); 3259d4ead6b3SDavid Ahern if (IS_ERR(rt)) 3260d4ead6b3SDavid Ahern return PTR_ERR(rt); 32616b9ea5a6SRoopa Prabhu 3262d4ead6b3SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); 326393531c67SDavid Ahern fib6_info_release(rt); 32646b9ea5a6SRoopa Prabhu 32651da177e4SLinus Torvalds return err; 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds 32688d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) 32691da177e4SLinus Torvalds { 3270afb1d4b5SDavid Ahern struct net *net = info->nl_net; 3271c71099acSThomas Graf struct fib6_table *table; 3272afb1d4b5SDavid Ahern int err; 32731da177e4SLinus Torvalds 3274421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) { 32756825a26cSGao feng err = -ENOENT; 32766825a26cSGao feng goto out; 32776825a26cSGao feng } 32786c813a72SPatrick McHardy 327993c2fb25SDavid Ahern table = rt->fib6_table; 328066f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 328186872cb5SThomas Graf err = fib6_del(rt, info); 328266f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 32831da177e4SLinus Torvalds 32846825a26cSGao feng out: 328593531c67SDavid Ahern fib6_info_release(rt); 32861da177e4SLinus Torvalds return err; 32871da177e4SLinus Torvalds } 32881da177e4SLinus Torvalds 32898d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt) 3290e0a1ad73SThomas Graf { 3291afb1d4b5SDavid Ahern struct nl_info info = { .nl_net = net }; 3292afb1d4b5SDavid Ahern 3293528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 3294e0a1ad73SThomas Graf } 3295e0a1ad73SThomas Graf 32968d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) 32970ae81335SDavid Ahern { 32980ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 3299e3330039SWANG Cong struct net *net = info->nl_net; 330016a16cd3SDavid Ahern struct sk_buff *skb = NULL; 33010ae81335SDavid Ahern struct fib6_table *table; 3302e3330039SWANG Cong int err = -ENOENT; 33030ae81335SDavid Ahern 3304421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 3305e3330039SWANG Cong goto out_put; 330693c2fb25SDavid Ahern table = rt->fib6_table; 330766f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 33080ae81335SDavid Ahern 330993c2fb25SDavid Ahern if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { 33108d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 33110ae81335SDavid Ahern 331216a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 331316a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 331416a16cd3SDavid Ahern if (skb) { 331516a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 331616a16cd3SDavid Ahern 3317d4ead6b3SDavid Ahern if (rt6_fill_node(net, skb, rt, NULL, 331816a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 331916a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 332016a16cd3SDavid Ahern kfree_skb(skb); 332116a16cd3SDavid Ahern skb = NULL; 332216a16cd3SDavid Ahern } else 332316a16cd3SDavid Ahern info->skip_notify = 1; 332416a16cd3SDavid Ahern } 332516a16cd3SDavid Ahern 33260ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 332793c2fb25SDavid Ahern &rt->fib6_siblings, 332893c2fb25SDavid Ahern fib6_siblings) { 33290ae81335SDavid Ahern err = fib6_del(sibling, info); 33300ae81335SDavid Ahern if (err) 3331e3330039SWANG Cong goto out_unlock; 33320ae81335SDavid Ahern } 33330ae81335SDavid Ahern } 33340ae81335SDavid Ahern 33350ae81335SDavid Ahern err = fib6_del(rt, info); 3336e3330039SWANG Cong out_unlock: 333766f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 3338e3330039SWANG Cong out_put: 333993531c67SDavid Ahern fib6_info_release(rt); 334016a16cd3SDavid Ahern 334116a16cd3SDavid Ahern if (skb) { 3342e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 334316a16cd3SDavid Ahern info->nlh, gfp_any()); 334416a16cd3SDavid Ahern } 33450ae81335SDavid Ahern return err; 33460ae81335SDavid Ahern } 33470ae81335SDavid Ahern 334823fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) 334923fb93a4SDavid Ahern { 335023fb93a4SDavid Ahern int rc = -ESRCH; 335123fb93a4SDavid Ahern 335223fb93a4SDavid Ahern if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) 335323fb93a4SDavid Ahern goto out; 335423fb93a4SDavid Ahern 335523fb93a4SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY && 335623fb93a4SDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 335723fb93a4SDavid Ahern goto out; 3358761f6026SXin Long 335923fb93a4SDavid Ahern rc = rt6_remove_exception_rt(rt); 336023fb93a4SDavid Ahern out: 336123fb93a4SDavid Ahern return rc; 336223fb93a4SDavid Ahern } 336323fb93a4SDavid Ahern 3364333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 3365333c4301SDavid Ahern struct netlink_ext_ack *extack) 33661da177e4SLinus Torvalds { 33678d1c802bSDavid Ahern struct rt6_info *rt_cache; 3368c71099acSThomas Graf struct fib6_table *table; 33698d1c802bSDavid Ahern struct fib6_info *rt; 33701da177e4SLinus Torvalds struct fib6_node *fn; 33711da177e4SLinus Torvalds int err = -ESRCH; 33721da177e4SLinus Torvalds 33735578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 3374d5d531cbSDavid Ahern if (!table) { 3375d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 3376c71099acSThomas Graf return err; 3377d5d531cbSDavid Ahern } 33781da177e4SLinus Torvalds 337966f5d6ceSWei Wang rcu_read_lock(); 3380c71099acSThomas Graf 3381c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 338286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 338338fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 33842b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds if (fn) { 338766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3388ad1601aeSDavid Ahern struct fib6_nh *nh; 3389ad1601aeSDavid Ahern 33902b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 33917e4b5128SDavid Ahern struct fib6_result res = { 33927e4b5128SDavid Ahern .f6i = rt, 33937e4b5128SDavid Ahern }; 339423fb93a4SDavid Ahern int rc; 339523fb93a4SDavid Ahern 33967e4b5128SDavid Ahern rt_cache = rt6_find_cached_rt(&res, 33977e4b5128SDavid Ahern &cfg->fc_dst, 33982b760fcfSWei Wang &cfg->fc_src); 339923fb93a4SDavid Ahern if (rt_cache) { 340023fb93a4SDavid Ahern rc = ip6_del_cached_rt(rt_cache, cfg); 34019e575010SEric Dumazet if (rc != -ESRCH) { 34029e575010SEric Dumazet rcu_read_unlock(); 340323fb93a4SDavid Ahern return rc; 340423fb93a4SDavid Ahern } 34059e575010SEric Dumazet } 34061f56a01fSMartin KaFai Lau continue; 34072b760fcfSWei Wang } 3408ad1601aeSDavid Ahern 3409ad1601aeSDavid Ahern nh = &rt->fib6_nh; 341086872cb5SThomas Graf if (cfg->fc_ifindex && 3411ad1601aeSDavid Ahern (!nh->fib_nh_dev || 3412ad1601aeSDavid Ahern nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) 34131da177e4SLinus Torvalds continue; 341486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 3415ad1601aeSDavid Ahern !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) 34161da177e4SLinus Torvalds continue; 341793c2fb25SDavid Ahern if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) 34181da177e4SLinus Torvalds continue; 341993c2fb25SDavid Ahern if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) 3420c2ed1880SMantas M continue; 3421e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3422e873e4b9SWei Wang continue; 342366f5d6ceSWei Wang rcu_read_unlock(); 34241da177e4SLinus Torvalds 34250ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 34260ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 342786872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 34280ae81335SDavid Ahern 34290ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds } 343266f5d6ceSWei Wang rcu_read_unlock(); 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds return err; 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 34376700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3438a6279458SYOSHIFUJI Hideaki { 3439a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3440e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 344185bd05deSDavid Ahern struct fib6_result res = {}; 3442e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3443e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3444e8599ff4SDavid S. Miller struct neighbour *neigh; 344571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 34466e157b6aSDavid S. Miller int optlen, on_link; 34476e157b6aSDavid S. Miller u8 *lladdr; 3448e8599ff4SDavid S. Miller 344929a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 345071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3451e8599ff4SDavid S. Miller 3452e8599ff4SDavid S. Miller if (optlen < 0) { 34536e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3454e8599ff4SDavid S. Miller return; 3455e8599ff4SDavid S. Miller } 3456e8599ff4SDavid S. Miller 345771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3458e8599ff4SDavid S. Miller 345971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 34606e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3461e8599ff4SDavid S. Miller return; 3462e8599ff4SDavid S. Miller } 3463e8599ff4SDavid S. Miller 34646e157b6aSDavid S. Miller on_link = 0; 346571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3466e8599ff4SDavid S. Miller on_link = 1; 346771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3468e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 34696e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3470e8599ff4SDavid S. Miller return; 3471e8599ff4SDavid S. Miller } 3472e8599ff4SDavid S. Miller 3473e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3474e8599ff4SDavid S. Miller if (!in6_dev) 3475e8599ff4SDavid S. Miller return; 3476e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3477e8599ff4SDavid S. Miller return; 3478e8599ff4SDavid S. Miller 3479e8599ff4SDavid S. Miller /* RFC2461 8.1: 3480e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3481e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3482e8599ff4SDavid S. Miller */ 3483e8599ff4SDavid S. Miller 3484f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3485e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3486e8599ff4SDavid S. Miller return; 3487e8599ff4SDavid S. Miller } 34886e157b6aSDavid S. Miller 34896e157b6aSDavid S. Miller lladdr = NULL; 3490e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3491e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3492e8599ff4SDavid S. Miller skb->dev); 3493e8599ff4SDavid S. Miller if (!lladdr) { 3494e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3495e8599ff4SDavid S. Miller return; 3496e8599ff4SDavid S. Miller } 3497e8599ff4SDavid S. Miller } 3498e8599ff4SDavid S. Miller 34996e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3500ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 35016e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 35026e157b6aSDavid S. Miller return; 35036e157b6aSDavid S. Miller } 35046e157b6aSDavid S. Miller 35056e157b6aSDavid S. Miller /* Redirect received -> path was valid. 35066e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 35076e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 35086e157b6aSDavid S. Miller */ 35090dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 35106e157b6aSDavid S. Miller 351171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3512e8599ff4SDavid S. Miller if (!neigh) 3513e8599ff4SDavid S. Miller return; 3514e8599ff4SDavid S. Miller 35151da177e4SLinus Torvalds /* 35161da177e4SLinus Torvalds * We have finally decided to accept it. 35171da177e4SLinus Torvalds */ 35181da177e4SLinus Torvalds 3519f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 35201da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 35211da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 35221da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3523f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3524f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 35251da177e4SLinus Torvalds 35264d85cd0cSDavid Ahern rcu_read_lock(); 352785bd05deSDavid Ahern res.f6i = rcu_dereference(rt->from); 3528ff24e498SDavid S. Miller if (!res.f6i) 3529886b7a50SMartin KaFai Lau goto out; 35308a14e46fSDavid Ahern 353185bd05deSDavid Ahern res.nh = &res.f6i->fib6_nh; 35327d21fec9SDavid Ahern res.fib6_flags = res.f6i->fib6_flags; 35337d21fec9SDavid Ahern res.fib6_type = res.f6i->fib6_type; 353485bd05deSDavid Ahern nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); 353538308473SDavid S. Miller if (!nrt) 35361da177e4SLinus Torvalds goto out; 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 35391da177e4SLinus Torvalds if (on_link) 35401da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 35411da177e4SLinus Torvalds 35424e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 35431da177e4SLinus Torvalds 3544886b7a50SMartin KaFai Lau /* rt6_insert_exception() will take care of duplicated exceptions */ 35455012f0a5SDavid Ahern if (rt6_insert_exception(nrt, &res)) { 35462b760fcfSWei Wang dst_release_immediate(&nrt->dst); 35472b760fcfSWei Wang goto out; 35482b760fcfSWei Wang } 35491da177e4SLinus Torvalds 3550d8d1f30bSChangli Gao netevent.old = &rt->dst; 3551d8d1f30bSChangli Gao netevent.new = &nrt->dst; 355271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 355360592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 35548d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 35558d71740cSTom Tucker 35561da177e4SLinus Torvalds out: 3557886b7a50SMartin KaFai Lau rcu_read_unlock(); 3558e8599ff4SDavid S. Miller neigh_release(neigh); 35596e157b6aSDavid S. Miller } 35606e157b6aSDavid S. Miller 356170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 35628d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net, 3563b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3564830218c1SDavid Ahern const struct in6_addr *gwaddr, 3565830218c1SDavid Ahern struct net_device *dev) 356670ceb4f5SYOSHIFUJI Hideaki { 3567830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3568830218c1SDavid Ahern int ifindex = dev->ifindex; 356970ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 35708d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3571c71099acSThomas Graf struct fib6_table *table; 357270ceb4f5SYOSHIFUJI Hideaki 3573830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 357438308473SDavid S. Miller if (!table) 3575c71099acSThomas Graf return NULL; 3576c71099acSThomas Graf 357766f5d6ceSWei Wang rcu_read_lock(); 357838fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 357970ceb4f5SYOSHIFUJI Hideaki if (!fn) 358070ceb4f5SYOSHIFUJI Hideaki goto out; 358170ceb4f5SYOSHIFUJI Hideaki 358266f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3583ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex) 358470ceb4f5SYOSHIFUJI Hideaki continue; 35852b2450caSDavid Ahern if (!(rt->fib6_flags & RTF_ROUTEINFO) || 3586bdf00467SDavid Ahern !rt->fib6_nh.fib_nh_gw_family) 358770ceb4f5SYOSHIFUJI Hideaki continue; 3588ad1601aeSDavid Ahern if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr)) 358970ceb4f5SYOSHIFUJI Hideaki continue; 3590e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 3591e873e4b9SWei Wang continue; 359270ceb4f5SYOSHIFUJI Hideaki break; 359370ceb4f5SYOSHIFUJI Hideaki } 359470ceb4f5SYOSHIFUJI Hideaki out: 359566f5d6ceSWei Wang rcu_read_unlock(); 359670ceb4f5SYOSHIFUJI Hideaki return rt; 359770ceb4f5SYOSHIFUJI Hideaki } 359870ceb4f5SYOSHIFUJI Hideaki 35998d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net, 3600b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3601830218c1SDavid Ahern const struct in6_addr *gwaddr, 3602830218c1SDavid Ahern struct net_device *dev, 360395c96174SEric Dumazet unsigned int pref) 360470ceb4f5SYOSHIFUJI Hideaki { 360586872cb5SThomas Graf struct fib6_config cfg = { 3606238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3607830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 360886872cb5SThomas Graf .fc_dst_len = prefixlen, 360986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 361086872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3611b91d5329SXin Long .fc_protocol = RTPROT_RA, 3612e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 361315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3614efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3615efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 361686872cb5SThomas Graf }; 361770ceb4f5SYOSHIFUJI Hideaki 3618830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 36194e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 36204e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 362186872cb5SThomas Graf 3622e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3623e317da96SYOSHIFUJI Hideaki if (!prefixlen) 362486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 362570ceb4f5SYOSHIFUJI Hideaki 3626acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 362770ceb4f5SYOSHIFUJI Hideaki 3628830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 362970ceb4f5SYOSHIFUJI Hideaki } 363070ceb4f5SYOSHIFUJI Hideaki #endif 363170ceb4f5SYOSHIFUJI Hideaki 36328d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net, 3633afb1d4b5SDavid Ahern const struct in6_addr *addr, 3634afb1d4b5SDavid Ahern struct net_device *dev) 36351da177e4SLinus Torvalds { 3636830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 36378d1c802bSDavid Ahern struct fib6_info *rt; 3638c71099acSThomas Graf struct fib6_table *table; 36391da177e4SLinus Torvalds 3640afb1d4b5SDavid Ahern table = fib6_get_table(net, tb_id); 364138308473SDavid S. Miller if (!table) 3642c71099acSThomas Graf return NULL; 36431da177e4SLinus Torvalds 364466f5d6ceSWei Wang rcu_read_lock(); 364566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3646ad1601aeSDavid Ahern struct fib6_nh *nh = &rt->fib6_nh; 3647ad1601aeSDavid Ahern 3648ad1601aeSDavid Ahern if (dev == nh->fib_nh_dev && 364993c2fb25SDavid Ahern ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 3650ad1601aeSDavid Ahern ipv6_addr_equal(&nh->fib_nh_gw6, addr)) 36511da177e4SLinus Torvalds break; 36521da177e4SLinus Torvalds } 3653e873e4b9SWei Wang if (rt && !fib6_info_hold_safe(rt)) 3654e873e4b9SWei Wang rt = NULL; 365566f5d6ceSWei Wang rcu_read_unlock(); 36561da177e4SLinus Torvalds return rt; 36571da177e4SLinus Torvalds } 36581da177e4SLinus Torvalds 36598d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net, 3660afb1d4b5SDavid Ahern const struct in6_addr *gwaddr, 3661ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3662ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 36631da177e4SLinus Torvalds { 366486872cb5SThomas Graf struct fib6_config cfg = { 3665ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3666238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 366786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 366886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 366986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3670b91d5329SXin Long .fc_protocol = RTPROT_RA, 3671e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 367215e47304SEric W. Biederman .fc_nlinfo.portid = 0, 36735578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3674afb1d4b5SDavid Ahern .fc_nlinfo.nl_net = net, 367586872cb5SThomas Graf }; 36761da177e4SLinus Torvalds 36774e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 36781da177e4SLinus Torvalds 3679acb54e3cSDavid Ahern if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { 3680830218c1SDavid Ahern struct fib6_table *table; 3681830218c1SDavid Ahern 3682830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3683830218c1SDavid Ahern if (table) 3684830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3685830218c1SDavid Ahern } 36861da177e4SLinus Torvalds 3687afb1d4b5SDavid Ahern return rt6_get_dflt_router(net, gwaddr, dev); 36881da177e4SLinus Torvalds } 36891da177e4SLinus Torvalds 3690afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net, 3691afb1d4b5SDavid Ahern struct fib6_table *table) 36921da177e4SLinus Torvalds { 36938d1c802bSDavid Ahern struct fib6_info *rt; 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds restart: 369666f5d6ceSWei Wang rcu_read_lock(); 369766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3698dcd1f572SDavid Ahern struct net_device *dev = fib6_info_nh_dev(rt); 3699dcd1f572SDavid Ahern struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; 3700dcd1f572SDavid Ahern 370193c2fb25SDavid Ahern if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 3702e873e4b9SWei Wang (!idev || idev->cnf.accept_ra != 2) && 3703e873e4b9SWei Wang fib6_info_hold_safe(rt)) { 370466f5d6ceSWei Wang rcu_read_unlock(); 3705afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 37061da177e4SLinus Torvalds goto restart; 37071da177e4SLinus Torvalds } 37081da177e4SLinus Torvalds } 370966f5d6ceSWei Wang rcu_read_unlock(); 3710830218c1SDavid Ahern 3711830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3712830218c1SDavid Ahern } 3713830218c1SDavid Ahern 3714830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3715830218c1SDavid Ahern { 3716830218c1SDavid Ahern struct fib6_table *table; 3717830218c1SDavid Ahern struct hlist_head *head; 3718830218c1SDavid Ahern unsigned int h; 3719830218c1SDavid Ahern 3720830218c1SDavid Ahern rcu_read_lock(); 3721830218c1SDavid Ahern 3722830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3723830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3724830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3725830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3726afb1d4b5SDavid Ahern __rt6_purge_dflt_routers(net, table); 3727830218c1SDavid Ahern } 3728830218c1SDavid Ahern } 3729830218c1SDavid Ahern 3730830218c1SDavid Ahern rcu_read_unlock(); 37311da177e4SLinus Torvalds } 37321da177e4SLinus Torvalds 37335578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 37345578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 373586872cb5SThomas Graf struct fib6_config *cfg) 373686872cb5SThomas Graf { 37378823a3acSMaciej Żenczykowski *cfg = (struct fib6_config){ 37388823a3acSMaciej Żenczykowski .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 37398823a3acSMaciej Żenczykowski : RT6_TABLE_MAIN, 37408823a3acSMaciej Żenczykowski .fc_ifindex = rtmsg->rtmsg_ifindex, 374167f69513SDavid Ahern .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, 37428823a3acSMaciej Żenczykowski .fc_expires = rtmsg->rtmsg_info, 37438823a3acSMaciej Żenczykowski .fc_dst_len = rtmsg->rtmsg_dst_len, 37448823a3acSMaciej Żenczykowski .fc_src_len = rtmsg->rtmsg_src_len, 37458823a3acSMaciej Żenczykowski .fc_flags = rtmsg->rtmsg_flags, 37468823a3acSMaciej Żenczykowski .fc_type = rtmsg->rtmsg_type, 374786872cb5SThomas Graf 37488823a3acSMaciej Żenczykowski .fc_nlinfo.nl_net = net, 374986872cb5SThomas Graf 37508823a3acSMaciej Żenczykowski .fc_dst = rtmsg->rtmsg_dst, 37518823a3acSMaciej Żenczykowski .fc_src = rtmsg->rtmsg_src, 37528823a3acSMaciej Żenczykowski .fc_gateway = rtmsg->rtmsg_gateway, 37538823a3acSMaciej Żenczykowski }; 375486872cb5SThomas Graf } 375586872cb5SThomas Graf 37565578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 37571da177e4SLinus Torvalds { 375886872cb5SThomas Graf struct fib6_config cfg; 37591da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 37601da177e4SLinus Torvalds int err; 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds switch (cmd) { 37631da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 37641da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3765af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 37661da177e4SLinus Torvalds return -EPERM; 37671da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 37681da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 37691da177e4SLinus Torvalds if (err) 37701da177e4SLinus Torvalds return -EFAULT; 37711da177e4SLinus Torvalds 37725578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 377386872cb5SThomas Graf 37741da177e4SLinus Torvalds rtnl_lock(); 37751da177e4SLinus Torvalds switch (cmd) { 37761da177e4SLinus Torvalds case SIOCADDRT: 3777acb54e3cSDavid Ahern err = ip6_route_add(&cfg, GFP_KERNEL, NULL); 37781da177e4SLinus Torvalds break; 37791da177e4SLinus Torvalds case SIOCDELRT: 3780333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 37811da177e4SLinus Torvalds break; 37821da177e4SLinus Torvalds default: 37831da177e4SLinus Torvalds err = -EINVAL; 37841da177e4SLinus Torvalds } 37851da177e4SLinus Torvalds rtnl_unlock(); 37861da177e4SLinus Torvalds 37871da177e4SLinus Torvalds return err; 37883ff50b79SStephen Hemminger } 37891da177e4SLinus Torvalds 37901da177e4SLinus Torvalds return -EINVAL; 37911da177e4SLinus Torvalds } 37921da177e4SLinus Torvalds 37931da177e4SLinus Torvalds /* 37941da177e4SLinus Torvalds * Drop the packet on the floor 37951da177e4SLinus Torvalds */ 37961da177e4SLinus Torvalds 3797d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 37981da177e4SLinus Torvalds { 3799adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 38001d3fd8a1SStephen Suryaputra struct net *net = dev_net(dst->dev); 38011d3fd8a1SStephen Suryaputra struct inet6_dev *idev; 38021d3fd8a1SStephen Suryaputra int type; 38031d3fd8a1SStephen Suryaputra 38041d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev) && 38051d3fd8a1SStephen Suryaputra dst->dev == net->loopback_dev) 38061d3fd8a1SStephen Suryaputra idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); 38071d3fd8a1SStephen Suryaputra else 38081d3fd8a1SStephen Suryaputra idev = ip6_dst_idev(dst); 38091d3fd8a1SStephen Suryaputra 3810612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3811612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 38120660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 381345bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 38141d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 3815612f09e8SYOSHIFUJI Hideaki break; 3816612f09e8SYOSHIFUJI Hideaki } 3817612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3818612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 38191d3fd8a1SStephen Suryaputra IP6_INC_STATS(net, idev, ipstats_mib_noroutes); 3820612f09e8SYOSHIFUJI Hideaki break; 3821612f09e8SYOSHIFUJI Hideaki } 38221d3fd8a1SStephen Suryaputra 38231d3fd8a1SStephen Suryaputra /* Start over by dropping the dst for l3mdev case */ 38241d3fd8a1SStephen Suryaputra if (netif_is_l3_master(skb->dev)) 38251d3fd8a1SStephen Suryaputra skb_dst_drop(skb); 38261d3fd8a1SStephen Suryaputra 38273ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 38281da177e4SLinus Torvalds kfree_skb(skb); 38291da177e4SLinus Torvalds return 0; 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds 38329ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 38339ce8ade0SThomas Graf { 3834612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 38359ce8ade0SThomas Graf } 38369ce8ade0SThomas Graf 3837ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 38381da177e4SLinus Torvalds { 3839adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3840612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 38411da177e4SLinus Torvalds } 38421da177e4SLinus Torvalds 38439ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 38449ce8ade0SThomas Graf { 3845612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 38469ce8ade0SThomas Graf } 38479ce8ade0SThomas Graf 3848ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 38499ce8ade0SThomas Graf { 3850adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3851612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 38529ce8ade0SThomas Graf } 38539ce8ade0SThomas Graf 38541da177e4SLinus Torvalds /* 38551da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 38561da177e4SLinus Torvalds */ 38571da177e4SLinus Torvalds 3858360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net, 3859afb1d4b5SDavid Ahern struct inet6_dev *idev, 38601da177e4SLinus Torvalds const struct in6_addr *addr, 3861acb54e3cSDavid Ahern bool anycast, gfp_t gfp_flags) 38621da177e4SLinus Torvalds { 3863c7a1ce39SDavid Ahern struct fib6_config cfg = { 3864c7a1ce39SDavid Ahern .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, 3865c7a1ce39SDavid Ahern .fc_ifindex = idev->dev->ifindex, 3866c7a1ce39SDavid Ahern .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, 3867c7a1ce39SDavid Ahern .fc_dst = *addr, 3868c7a1ce39SDavid Ahern .fc_dst_len = 128, 3869c7a1ce39SDavid Ahern .fc_protocol = RTPROT_KERNEL, 3870c7a1ce39SDavid Ahern .fc_nlinfo.nl_net = net, 3871c7a1ce39SDavid Ahern .fc_ignore_dev_down = true, 3872c7a1ce39SDavid Ahern }; 38735f02ce24SDavid Ahern 3874e8478e80SDavid Ahern if (anycast) { 3875c7a1ce39SDavid Ahern cfg.fc_type = RTN_ANYCAST; 3876c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_ANYCAST; 3877e8478e80SDavid Ahern } else { 3878c7a1ce39SDavid Ahern cfg.fc_type = RTN_LOCAL; 3879c7a1ce39SDavid Ahern cfg.fc_flags |= RTF_LOCAL; 3880e8478e80SDavid Ahern } 38811da177e4SLinus Torvalds 3882c7a1ce39SDavid Ahern return ip6_route_info_create(&cfg, gfp_flags, NULL); 38831da177e4SLinus Torvalds } 38841da177e4SLinus Torvalds 3885c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3886c3968a85SDaniel Walter struct arg_dev_net_ip { 3887c3968a85SDaniel Walter struct net_device *dev; 3888c3968a85SDaniel Walter struct net *net; 3889c3968a85SDaniel Walter struct in6_addr *addr; 3890c3968a85SDaniel Walter }; 3891c3968a85SDaniel Walter 38928d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) 3893c3968a85SDaniel Walter { 3894c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3895c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3896c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3897c3968a85SDaniel Walter 3898ad1601aeSDavid Ahern if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) && 3899421842edSDavid Ahern rt != net->ipv6.fib6_null_entry && 390093c2fb25SDavid Ahern ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { 390160006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3902c3968a85SDaniel Walter /* remove prefsrc entry */ 390393c2fb25SDavid Ahern rt->fib6_prefsrc.plen = 0; 390460006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3905c3968a85SDaniel Walter } 3906c3968a85SDaniel Walter return 0; 3907c3968a85SDaniel Walter } 3908c3968a85SDaniel Walter 3909c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3910c3968a85SDaniel Walter { 3911c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3912c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3913c3968a85SDaniel Walter .dev = ifp->idev->dev, 3914c3968a85SDaniel Walter .net = net, 3915c3968a85SDaniel Walter .addr = &ifp->addr, 3916c3968a85SDaniel Walter }; 39170c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3918c3968a85SDaniel Walter } 3919c3968a85SDaniel Walter 39202b2450caSDavid Ahern #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) 3921be7a010dSDuan Jiong 3922be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 39238d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg) 3924be7a010dSDuan Jiong { 3925be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3926be7a010dSDuan Jiong 392793c2fb25SDavid Ahern if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3928bdf00467SDavid Ahern rt->fib6_nh.fib_nh_gw_family && 3929ad1601aeSDavid Ahern ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) { 3930be7a010dSDuan Jiong return -1; 3931be7a010dSDuan Jiong } 3932b16cb459SWei Wang 3933b16cb459SWei Wang /* Further clean up cached routes in exception table. 3934b16cb459SWei Wang * This is needed because cached route may have a different 3935b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3936b16cb459SWei Wang */ 3937b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3938b16cb459SWei Wang 3939be7a010dSDuan Jiong return 0; 3940be7a010dSDuan Jiong } 3941be7a010dSDuan Jiong 3942be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3943be7a010dSDuan Jiong { 3944be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3945be7a010dSDuan Jiong } 3946be7a010dSDuan Jiong 39472127d95aSIdo Schimmel struct arg_netdev_event { 39482127d95aSIdo Schimmel const struct net_device *dev; 39494c981e28SIdo Schimmel union { 3950ecc5663cSDavid Ahern unsigned char nh_flags; 39514c981e28SIdo Schimmel unsigned long event; 39524c981e28SIdo Schimmel }; 39532127d95aSIdo Schimmel }; 39542127d95aSIdo Schimmel 39558d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) 3956d7dedee1SIdo Schimmel { 39578d1c802bSDavid Ahern struct fib6_info *iter; 3958d7dedee1SIdo Schimmel struct fib6_node *fn; 3959d7dedee1SIdo Schimmel 396093c2fb25SDavid Ahern fn = rcu_dereference_protected(rt->fib6_node, 396193c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3962d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 396393c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3964d7dedee1SIdo Schimmel while (iter) { 396593c2fb25SDavid Ahern if (iter->fib6_metric == rt->fib6_metric && 396633bd5ac5SDavid Ahern rt6_qualify_for_ecmp(iter)) 3967d7dedee1SIdo Schimmel return iter; 39688fb11a9aSDavid Ahern iter = rcu_dereference_protected(iter->fib6_next, 396993c2fb25SDavid Ahern lockdep_is_held(&rt->fib6_table->tb6_lock)); 3970d7dedee1SIdo Schimmel } 3971d7dedee1SIdo Schimmel 3972d7dedee1SIdo Schimmel return NULL; 3973d7dedee1SIdo Schimmel } 3974d7dedee1SIdo Schimmel 39758d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt) 3976d7dedee1SIdo Schimmel { 3977ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD || 3978ad1601aeSDavid Ahern (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN && 3979ad1601aeSDavid Ahern ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev))) 3980d7dedee1SIdo Schimmel return true; 3981d7dedee1SIdo Schimmel 3982d7dedee1SIdo Schimmel return false; 3983d7dedee1SIdo Schimmel } 3984d7dedee1SIdo Schimmel 39858d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt) 3986d7dedee1SIdo Schimmel { 39878d1c802bSDavid Ahern struct fib6_info *iter; 3988d7dedee1SIdo Schimmel int total = 0; 3989d7dedee1SIdo Schimmel 3990d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 3991ad1601aeSDavid Ahern total += rt->fib6_nh.fib_nh_weight; 3992d7dedee1SIdo Schimmel 399393c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 3994d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 3995ad1601aeSDavid Ahern total += iter->fib6_nh.fib_nh_weight; 3996d7dedee1SIdo Schimmel } 3997d7dedee1SIdo Schimmel 3998d7dedee1SIdo Schimmel return total; 3999d7dedee1SIdo Schimmel } 4000d7dedee1SIdo Schimmel 40018d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) 4002d7dedee1SIdo Schimmel { 4003d7dedee1SIdo Schimmel int upper_bound = -1; 4004d7dedee1SIdo Schimmel 4005d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 4006ad1601aeSDavid Ahern *weight += rt->fib6_nh.fib_nh_weight; 4007d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 4008d7dedee1SIdo Schimmel total) - 1; 4009d7dedee1SIdo Schimmel } 4010ad1601aeSDavid Ahern atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound); 4011d7dedee1SIdo Schimmel } 4012d7dedee1SIdo Schimmel 40138d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) 4014d7dedee1SIdo Schimmel { 40158d1c802bSDavid Ahern struct fib6_info *iter; 4016d7dedee1SIdo Schimmel int weight = 0; 4017d7dedee1SIdo Schimmel 4018d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 4019d7dedee1SIdo Schimmel 402093c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4021d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 4022d7dedee1SIdo Schimmel } 4023d7dedee1SIdo Schimmel 40248d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt) 4025d7dedee1SIdo Schimmel { 40268d1c802bSDavid Ahern struct fib6_info *first; 4027d7dedee1SIdo Schimmel int total; 4028d7dedee1SIdo Schimmel 4029d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 4030d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 4031d7dedee1SIdo Schimmel * sibling route. 4032d7dedee1SIdo Schimmel */ 403393c2fb25SDavid Ahern if (!rt->fib6_nsiblings || rt->should_flush) 4034d7dedee1SIdo Schimmel return; 4035d7dedee1SIdo Schimmel 4036d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 4037d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 4038d7dedee1SIdo Schimmel * onwards. 4039d7dedee1SIdo Schimmel */ 4040d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 4041d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 4042d7dedee1SIdo Schimmel return; 4043d7dedee1SIdo Schimmel 4044d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 4045d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 4046d7dedee1SIdo Schimmel } 4047d7dedee1SIdo Schimmel 40488d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg) 40492127d95aSIdo Schimmel { 40502127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 40517aef6859SDavid Ahern struct net *net = dev_net(arg->dev); 40522127d95aSIdo Schimmel 4053ad1601aeSDavid Ahern if (rt != net->ipv6.fib6_null_entry && 4054ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_dev == arg->dev) { 4055ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags; 40567aef6859SDavid Ahern fib6_update_sernum_upto_root(net, rt); 4057d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 40581de178edSIdo Schimmel } 40592127d95aSIdo Schimmel 40602127d95aSIdo Schimmel return 0; 40612127d95aSIdo Schimmel } 40622127d95aSIdo Schimmel 4063ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) 40642127d95aSIdo Schimmel { 40652127d95aSIdo Schimmel struct arg_netdev_event arg = { 40662127d95aSIdo Schimmel .dev = dev, 40676802f3adSIdo Schimmel { 40682127d95aSIdo Schimmel .nh_flags = nh_flags, 40696802f3adSIdo Schimmel }, 40702127d95aSIdo Schimmel }; 40712127d95aSIdo Schimmel 40722127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 40732127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 40742127d95aSIdo Schimmel 40752127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 40762127d95aSIdo Schimmel } 40772127d95aSIdo Schimmel 40788d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt, 40791de178edSIdo Schimmel const struct net_device *dev) 40801de178edSIdo Schimmel { 40818d1c802bSDavid Ahern struct fib6_info *iter; 40821de178edSIdo Schimmel 4083ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == dev) 40841de178edSIdo Schimmel return true; 408593c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4086ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == dev) 40871de178edSIdo Schimmel return true; 40881de178edSIdo Schimmel 40891de178edSIdo Schimmel return false; 40901de178edSIdo Schimmel } 40911de178edSIdo Schimmel 40928d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt) 40931de178edSIdo Schimmel { 40948d1c802bSDavid Ahern struct fib6_info *iter; 40951de178edSIdo Schimmel 40961de178edSIdo Schimmel rt->should_flush = 1; 409793c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 40981de178edSIdo Schimmel iter->should_flush = 1; 40991de178edSIdo Schimmel } 41001de178edSIdo Schimmel 41018d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, 41021de178edSIdo Schimmel const struct net_device *down_dev) 41031de178edSIdo Schimmel { 41048d1c802bSDavid Ahern struct fib6_info *iter; 41051de178edSIdo Schimmel unsigned int dead = 0; 41061de178edSIdo Schimmel 4107ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == down_dev || 4108ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 41091de178edSIdo Schimmel dead++; 411093c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4111ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == down_dev || 4112ad1601aeSDavid Ahern iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD) 41131de178edSIdo Schimmel dead++; 41141de178edSIdo Schimmel 41151de178edSIdo Schimmel return dead; 41161de178edSIdo Schimmel } 41171de178edSIdo Schimmel 41188d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt, 41191de178edSIdo Schimmel const struct net_device *dev, 4120ecc5663cSDavid Ahern unsigned char nh_flags) 41211de178edSIdo Schimmel { 41228d1c802bSDavid Ahern struct fib6_info *iter; 41231de178edSIdo Schimmel 4124ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == dev) 4125ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags |= nh_flags; 412693c2fb25SDavid Ahern list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) 4127ad1601aeSDavid Ahern if (iter->fib6_nh.fib_nh_dev == dev) 4128ad1601aeSDavid Ahern iter->fib6_nh.fib_nh_flags |= nh_flags; 41291de178edSIdo Schimmel } 41301de178edSIdo Schimmel 4131a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 41328d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg) 41331da177e4SLinus Torvalds { 41344c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 41354c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 41367aef6859SDavid Ahern struct net *net = dev_net(dev); 41378ed67789SDaniel Lezcano 4138421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 413927c6fa73SIdo Schimmel return 0; 414027c6fa73SIdo Schimmel 414127c6fa73SIdo Schimmel switch (arg->event) { 414227c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 4143ad1601aeSDavid Ahern return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; 414427c6fa73SIdo Schimmel case NETDEV_DOWN: 41451de178edSIdo Schimmel if (rt->should_flush) 414627c6fa73SIdo Schimmel return -1; 414793c2fb25SDavid Ahern if (!rt->fib6_nsiblings) 4148ad1601aeSDavid Ahern return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; 41491de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 41501de178edSIdo Schimmel unsigned int count; 41511de178edSIdo Schimmel 41521de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 415393c2fb25SDavid Ahern if (rt->fib6_nsiblings + 1 == count) { 41541de178edSIdo Schimmel rt6_multipath_flush(rt); 41551de178edSIdo Schimmel return -1; 41561de178edSIdo Schimmel } 41571de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 41581de178edSIdo Schimmel RTNH_F_LINKDOWN); 41597aef6859SDavid Ahern fib6_update_sernum(net, rt); 4160d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 41611de178edSIdo Schimmel } 41621de178edSIdo Schimmel return -2; 416327c6fa73SIdo Schimmel case NETDEV_CHANGE: 4164ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev != dev || 416593c2fb25SDavid Ahern rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) 416627c6fa73SIdo Schimmel break; 4167ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN; 4168d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 416927c6fa73SIdo Schimmel break; 41702b241361SIdo Schimmel } 4171c159d30cSDavid S. Miller 41721da177e4SLinus Torvalds return 0; 41731da177e4SLinus Torvalds } 41741da177e4SLinus Torvalds 417527c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 41761da177e4SLinus Torvalds { 41774c981e28SIdo Schimmel struct arg_netdev_event arg = { 41788ed67789SDaniel Lezcano .dev = dev, 41796802f3adSIdo Schimmel { 41804c981e28SIdo Schimmel .event = event, 41816802f3adSIdo Schimmel }, 41828ed67789SDaniel Lezcano }; 41837c6bb7d2SDavid Ahern struct net *net = dev_net(dev); 41848ed67789SDaniel Lezcano 41857c6bb7d2SDavid Ahern if (net->ipv6.sysctl.skip_notify_on_dev_down) 41867c6bb7d2SDavid Ahern fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); 41877c6bb7d2SDavid Ahern else 41887c6bb7d2SDavid Ahern fib6_clean_all(net, fib6_ifdown, &arg); 41894c981e28SIdo Schimmel } 41904c981e28SIdo Schimmel 41914c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 41924c981e28SIdo Schimmel { 41934c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 41944c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 41954c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 41961da177e4SLinus Torvalds } 41971da177e4SLinus Torvalds 419895c96174SEric Dumazet struct rt6_mtu_change_arg { 41991da177e4SLinus Torvalds struct net_device *dev; 420095c96174SEric Dumazet unsigned int mtu; 42011da177e4SLinus Torvalds }; 42021da177e4SLinus Torvalds 42038d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg) 42041da177e4SLinus Torvalds { 42051da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 42061da177e4SLinus Torvalds struct inet6_dev *idev; 42071da177e4SLinus Torvalds 42081da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 42091da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 42101da177e4SLinus Torvalds We still use this lock to block changes 42111da177e4SLinus Torvalds caused by addrconf/ndisc. 42121da177e4SLinus Torvalds */ 42131da177e4SLinus Torvalds 42141da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 421538308473SDavid S. Miller if (!idev) 42161da177e4SLinus Torvalds return 0; 42171da177e4SLinus Torvalds 42181da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 42191da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 42201da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 42211da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 42221da177e4SLinus Torvalds */ 4223ad1601aeSDavid Ahern if (rt->fib6_nh.fib_nh_dev == arg->dev && 4224d4ead6b3SDavid Ahern !fib6_metric_locked(rt, RTAX_MTU)) { 4225d4ead6b3SDavid Ahern u32 mtu = rt->fib6_pmtu; 4226d4ead6b3SDavid Ahern 4227d4ead6b3SDavid Ahern if (mtu >= arg->mtu || 4228d4ead6b3SDavid Ahern (mtu < arg->mtu && mtu == idev->cnf.mtu6)) 4229d4ead6b3SDavid Ahern fib6_metric_set(rt, RTAX_MTU, arg->mtu); 4230d4ead6b3SDavid Ahern 4231f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 4232e9fa1495SStefano Brivio rt6_exceptions_update_pmtu(idev, rt, arg->mtu); 4233f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 42344b32b5adSMartin KaFai Lau } 42351da177e4SLinus Torvalds return 0; 42361da177e4SLinus Torvalds } 42371da177e4SLinus Torvalds 423895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 42391da177e4SLinus Torvalds { 4240c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 4241c71099acSThomas Graf .dev = dev, 4242c71099acSThomas Graf .mtu = mtu, 4243c71099acSThomas Graf }; 42441da177e4SLinus Torvalds 42450c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 42461da177e4SLinus Torvalds } 42471da177e4SLinus Torvalds 4248ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 424975425657SDavid Ahern [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 42505176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 4251aa8f8778SEric Dumazet [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, 425286872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 4253ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 425486872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 425586872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 425651ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 4257c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 425819e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 425919e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 426032bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 4261622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 42623b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 4263aa8f8778SEric Dumazet [RTA_TABLE] = { .type = NLA_U32 }, 4264eacb9384SRoopa Prabhu [RTA_IP_PROTO] = { .type = NLA_U8 }, 4265eacb9384SRoopa Prabhu [RTA_SPORT] = { .type = NLA_U16 }, 4266eacb9384SRoopa Prabhu [RTA_DPORT] = { .type = NLA_U16 }, 426786872cb5SThomas Graf }; 426886872cb5SThomas Graf 426986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 4270333c4301SDavid Ahern struct fib6_config *cfg, 4271333c4301SDavid Ahern struct netlink_ext_ack *extack) 42721da177e4SLinus Torvalds { 427386872cb5SThomas Graf struct rtmsg *rtm; 427486872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 4275c78ba6d6SLubomir Rintel unsigned int pref; 427686872cb5SThomas Graf int err; 42771da177e4SLinus Torvalds 42788cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 42798cb08174SJohannes Berg rtm_ipv6_policy, extack); 428086872cb5SThomas Graf if (err < 0) 428186872cb5SThomas Graf goto errout; 42821da177e4SLinus Torvalds 428386872cb5SThomas Graf err = -EINVAL; 428486872cb5SThomas Graf rtm = nlmsg_data(nlh); 428586872cb5SThomas Graf 428684db8407SMaciej Żenczykowski *cfg = (struct fib6_config){ 428784db8407SMaciej Żenczykowski .fc_table = rtm->rtm_table, 428884db8407SMaciej Żenczykowski .fc_dst_len = rtm->rtm_dst_len, 428984db8407SMaciej Żenczykowski .fc_src_len = rtm->rtm_src_len, 429084db8407SMaciej Żenczykowski .fc_flags = RTF_UP, 429184db8407SMaciej Żenczykowski .fc_protocol = rtm->rtm_protocol, 429284db8407SMaciej Żenczykowski .fc_type = rtm->rtm_type, 429384db8407SMaciej Żenczykowski 429484db8407SMaciej Żenczykowski .fc_nlinfo.portid = NETLINK_CB(skb).portid, 429584db8407SMaciej Żenczykowski .fc_nlinfo.nlh = nlh, 429684db8407SMaciej Żenczykowski .fc_nlinfo.nl_net = sock_net(skb->sk), 429784db8407SMaciej Żenczykowski }; 429886872cb5SThomas Graf 4299ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 4300ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 4301b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 4302b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 430386872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 430486872cb5SThomas Graf 4305ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 4306ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 4307ab79ad14SMaciej Żenczykowski 43081f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 43091f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 43101f56a01fSMartin KaFai Lau 4311fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 4312fc1e64e1SDavid Ahern 431386872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 431467b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 431586872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 43161da177e4SLinus Torvalds } 4317e3818541SDavid Ahern if (tb[RTA_VIA]) { 4318e3818541SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); 4319e3818541SDavid Ahern goto errout; 4320e3818541SDavid Ahern } 432186872cb5SThomas Graf 432286872cb5SThomas Graf if (tb[RTA_DST]) { 432386872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 432486872cb5SThomas Graf 432586872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 432686872cb5SThomas Graf goto errout; 432786872cb5SThomas Graf 432886872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 43291da177e4SLinus Torvalds } 433086872cb5SThomas Graf 433186872cb5SThomas Graf if (tb[RTA_SRC]) { 433286872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 433386872cb5SThomas Graf 433486872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 433586872cb5SThomas Graf goto errout; 433686872cb5SThomas Graf 433786872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 43381da177e4SLinus Torvalds } 433986872cb5SThomas Graf 4340c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 434167b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 4342c3968a85SDaniel Walter 434386872cb5SThomas Graf if (tb[RTA_OIF]) 434486872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 434586872cb5SThomas Graf 434686872cb5SThomas Graf if (tb[RTA_PRIORITY]) 434786872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 434886872cb5SThomas Graf 434986872cb5SThomas Graf if (tb[RTA_METRICS]) { 435086872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 435186872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 43521da177e4SLinus Torvalds } 435386872cb5SThomas Graf 435486872cb5SThomas Graf if (tb[RTA_TABLE]) 435586872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 435686872cb5SThomas Graf 435751ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 435851ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 435951ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 43609ed59592SDavid Ahern 43619ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 4362c255bd68SDavid Ahern cfg->fc_mp_len, extack); 43639ed59592SDavid Ahern if (err < 0) 43649ed59592SDavid Ahern goto errout; 436551ebd318SNicolas Dichtel } 436651ebd318SNicolas Dichtel 4367c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 4368c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 4369c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 4370c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 4371c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 4372c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 4373c78ba6d6SLubomir Rintel } 4374c78ba6d6SLubomir Rintel 437519e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 437619e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 437719e42e45SRoopa Prabhu 43789ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 437919e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 438019e42e45SRoopa Prabhu 4381c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 43829ed59592SDavid Ahern if (err < 0) 43839ed59592SDavid Ahern goto errout; 43849ed59592SDavid Ahern } 43859ed59592SDavid Ahern 438632bc201eSXin Long if (tb[RTA_EXPIRES]) { 438732bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 438832bc201eSXin Long 438932bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 439032bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 439132bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 439232bc201eSXin Long } 439332bc201eSXin Long } 439432bc201eSXin Long 439586872cb5SThomas Graf err = 0; 439686872cb5SThomas Graf errout: 439786872cb5SThomas Graf return err; 43981da177e4SLinus Torvalds } 43991da177e4SLinus Torvalds 44006b9ea5a6SRoopa Prabhu struct rt6_nh { 44018d1c802bSDavid Ahern struct fib6_info *fib6_info; 44026b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 44036b9ea5a6SRoopa Prabhu struct list_head next; 44046b9ea5a6SRoopa Prabhu }; 44056b9ea5a6SRoopa Prabhu 4406d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net, 4407d4ead6b3SDavid Ahern struct list_head *rt6_nh_list, 44088d1c802bSDavid Ahern struct fib6_info *rt, 44098d1c802bSDavid Ahern struct fib6_config *r_cfg) 44106b9ea5a6SRoopa Prabhu { 44116b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 44126b9ea5a6SRoopa Prabhu int err = -EEXIST; 44136b9ea5a6SRoopa Prabhu 44146b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 44158d1c802bSDavid Ahern /* check if fib6_info already exists */ 44168d1c802bSDavid Ahern if (rt6_duplicate_nexthop(nh->fib6_info, rt)) 44176b9ea5a6SRoopa Prabhu return err; 44186b9ea5a6SRoopa Prabhu } 44196b9ea5a6SRoopa Prabhu 44206b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 44216b9ea5a6SRoopa Prabhu if (!nh) 44226b9ea5a6SRoopa Prabhu return -ENOMEM; 44238d1c802bSDavid Ahern nh->fib6_info = rt; 44246b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 44256b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 44266b9ea5a6SRoopa Prabhu 44276b9ea5a6SRoopa Prabhu return 0; 44286b9ea5a6SRoopa Prabhu } 44296b9ea5a6SRoopa Prabhu 44308d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt, 44318d1c802bSDavid Ahern struct fib6_info *rt_last, 44323b1137feSDavid Ahern struct nl_info *info, 44333b1137feSDavid Ahern __u16 nlflags) 44343b1137feSDavid Ahern { 44353b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 44363b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 44373b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 44383b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 44393b1137feSDavid Ahern * the list, find the first sibling of the last route appended 44403b1137feSDavid Ahern */ 444193c2fb25SDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { 444293c2fb25SDavid Ahern rt = list_first_entry(&rt_last->fib6_siblings, 44438d1c802bSDavid Ahern struct fib6_info, 444493c2fb25SDavid Ahern fib6_siblings); 44453b1137feSDavid Ahern } 44463b1137feSDavid Ahern 44473b1137feSDavid Ahern if (rt) 44483b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 44493b1137feSDavid Ahern } 44503b1137feSDavid Ahern 4451333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4452333c4301SDavid Ahern struct netlink_ext_ack *extack) 445351ebd318SNicolas Dichtel { 44548d1c802bSDavid Ahern struct fib6_info *rt_notif = NULL, *rt_last = NULL; 44553b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 445651ebd318SNicolas Dichtel struct fib6_config r_cfg; 445751ebd318SNicolas Dichtel struct rtnexthop *rtnh; 44588d1c802bSDavid Ahern struct fib6_info *rt; 44596b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 44606b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 44613b1137feSDavid Ahern __u16 nlflags; 446251ebd318SNicolas Dichtel int remaining; 446351ebd318SNicolas Dichtel int attrlen; 44646b9ea5a6SRoopa Prabhu int err = 1; 44656b9ea5a6SRoopa Prabhu int nhn = 0; 44666b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 44676b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 44686b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 446951ebd318SNicolas Dichtel 44703b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 44713b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 44723b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 44733b1137feSDavid Ahern 447435f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 447551ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 447651ebd318SNicolas Dichtel 44776b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 44788d1c802bSDavid Ahern * fib6_info structs per nexthop 44796b9ea5a6SRoopa Prabhu */ 448051ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 448151ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 448251ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 448351ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 448451ebd318SNicolas Dichtel 448551ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 448651ebd318SNicolas Dichtel if (attrlen > 0) { 448751ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 448851ebd318SNicolas Dichtel 448951ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 449051ebd318SNicolas Dichtel if (nla) { 449167b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 449251ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 449351ebd318SNicolas Dichtel } 449419e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 449519e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 449619e42e45SRoopa Prabhu if (nla) 449719e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 449851ebd318SNicolas Dichtel } 44996b9ea5a6SRoopa Prabhu 450068e2ffdeSDavid Ahern r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); 4501acb54e3cSDavid Ahern rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); 45028c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 45038c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 45048c5b83f0SRoopa Prabhu rt = NULL; 45056b9ea5a6SRoopa Prabhu goto cleanup; 45068c5b83f0SRoopa Prabhu } 4507b5d2d75eSDavid Ahern if (!rt6_qualify_for_ecmp(rt)) { 4508b5d2d75eSDavid Ahern err = -EINVAL; 4509b5d2d75eSDavid Ahern NL_SET_ERR_MSG(extack, 4510b5d2d75eSDavid Ahern "Device only routes can not be added for IPv6 using the multipath API."); 4511b5d2d75eSDavid Ahern fib6_info_release(rt); 4512b5d2d75eSDavid Ahern goto cleanup; 4513b5d2d75eSDavid Ahern } 45146b9ea5a6SRoopa Prabhu 4515ad1601aeSDavid Ahern rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1; 4516398958aeSIdo Schimmel 4517d4ead6b3SDavid Ahern err = ip6_route_info_append(info->nl_net, &rt6_nh_list, 4518d4ead6b3SDavid Ahern rt, &r_cfg); 451951ebd318SNicolas Dichtel if (err) { 452093531c67SDavid Ahern fib6_info_release(rt); 45216b9ea5a6SRoopa Prabhu goto cleanup; 452251ebd318SNicolas Dichtel } 45236b9ea5a6SRoopa Prabhu 45246b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 452551ebd318SNicolas Dichtel } 45266b9ea5a6SRoopa Prabhu 45273b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 45283b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 45293b1137feSDavid Ahern * the full route when done 45303b1137feSDavid Ahern */ 45313b1137feSDavid Ahern info->skip_notify = 1; 45323b1137feSDavid Ahern 45336b9ea5a6SRoopa Prabhu err_nh = NULL; 45346b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 45358d1c802bSDavid Ahern err = __ip6_ins_rt(nh->fib6_info, info, extack); 45368d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 45373b1137feSDavid Ahern 4538f7225172SDavid Ahern if (!err) { 4539f7225172SDavid Ahern /* save reference to last route successfully inserted */ 4540f7225172SDavid Ahern rt_last = nh->fib6_info; 4541f7225172SDavid Ahern 45426b9ea5a6SRoopa Prabhu /* save reference to first route for notification */ 4543f7225172SDavid Ahern if (!rt_notif) 45448d1c802bSDavid Ahern rt_notif = nh->fib6_info; 4545f7225172SDavid Ahern } 45466b9ea5a6SRoopa Prabhu 45478d1c802bSDavid Ahern /* nh->fib6_info is used or freed at this point, reset to NULL*/ 45488d1c802bSDavid Ahern nh->fib6_info = NULL; 45496b9ea5a6SRoopa Prabhu if (err) { 45506b9ea5a6SRoopa Prabhu if (replace && nhn) 4551a5a82d84SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 4552a5a82d84SJakub Kicinski "multipath route replace failed (check consistency of installed routes)"); 45536b9ea5a6SRoopa Prabhu err_nh = nh; 45546b9ea5a6SRoopa Prabhu goto add_errout; 45556b9ea5a6SRoopa Prabhu } 45566b9ea5a6SRoopa Prabhu 45571a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 455827596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 455927596472SMichal Kubeček * we have already failed to add the first nexthop: 456027596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 456127596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 456227596472SMichal Kubeček * be added to it. 45631a72418bSNicolas Dichtel */ 456427596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 456527596472SMichal Kubeček NLM_F_REPLACE); 45666b9ea5a6SRoopa Prabhu nhn++; 45676b9ea5a6SRoopa Prabhu } 45686b9ea5a6SRoopa Prabhu 45693b1137feSDavid Ahern /* success ... tell user about new route */ 45703b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 45716b9ea5a6SRoopa Prabhu goto cleanup; 45726b9ea5a6SRoopa Prabhu 45736b9ea5a6SRoopa Prabhu add_errout: 45743b1137feSDavid Ahern /* send notification for routes that were added so that 45753b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 45763b1137feSDavid Ahern * coherent 45773b1137feSDavid Ahern */ 45783b1137feSDavid Ahern if (rt_notif) 45793b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 45803b1137feSDavid Ahern 45816b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 45826b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 45836b9ea5a6SRoopa Prabhu if (err_nh == nh) 45846b9ea5a6SRoopa Prabhu break; 4585333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 45866b9ea5a6SRoopa Prabhu } 45876b9ea5a6SRoopa Prabhu 45886b9ea5a6SRoopa Prabhu cleanup: 45896b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 45908d1c802bSDavid Ahern if (nh->fib6_info) 45918d1c802bSDavid Ahern fib6_info_release(nh->fib6_info); 45926b9ea5a6SRoopa Prabhu list_del(&nh->next); 45936b9ea5a6SRoopa Prabhu kfree(nh); 45946b9ea5a6SRoopa Prabhu } 45956b9ea5a6SRoopa Prabhu 45966b9ea5a6SRoopa Prabhu return err; 45976b9ea5a6SRoopa Prabhu } 45986b9ea5a6SRoopa Prabhu 4599333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 4600333c4301SDavid Ahern struct netlink_ext_ack *extack) 46016b9ea5a6SRoopa Prabhu { 46026b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 46036b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 46046b9ea5a6SRoopa Prabhu int remaining; 46056b9ea5a6SRoopa Prabhu int attrlen; 46066b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 46076b9ea5a6SRoopa Prabhu 46086b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 46096b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 46106b9ea5a6SRoopa Prabhu 46116b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 46126b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 46136b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 46146b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 46156b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 46166b9ea5a6SRoopa Prabhu 46176b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 46186b9ea5a6SRoopa Prabhu if (attrlen > 0) { 46196b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 46206b9ea5a6SRoopa Prabhu 46216b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 46226b9ea5a6SRoopa Prabhu if (nla) { 46236b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 46246b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 46256b9ea5a6SRoopa Prabhu } 46266b9ea5a6SRoopa Prabhu } 4627333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 46286b9ea5a6SRoopa Prabhu if (err) 46296b9ea5a6SRoopa Prabhu last_err = err; 46306b9ea5a6SRoopa Prabhu 463151ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 463251ebd318SNicolas Dichtel } 463351ebd318SNicolas Dichtel 463451ebd318SNicolas Dichtel return last_err; 463551ebd318SNicolas Dichtel } 463651ebd318SNicolas Dichtel 4637c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4638c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46391da177e4SLinus Torvalds { 464086872cb5SThomas Graf struct fib6_config cfg; 464186872cb5SThomas Graf int err; 46421da177e4SLinus Torvalds 4643333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 464486872cb5SThomas Graf if (err < 0) 464586872cb5SThomas Graf return err; 464686872cb5SThomas Graf 464751ebd318SNicolas Dichtel if (cfg.fc_mp) 4648333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 46490ae81335SDavid Ahern else { 46500ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 4651333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 46521da177e4SLinus Torvalds } 46530ae81335SDavid Ahern } 46541da177e4SLinus Torvalds 4655c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4656c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46571da177e4SLinus Torvalds { 465886872cb5SThomas Graf struct fib6_config cfg; 465986872cb5SThomas Graf int err; 46601da177e4SLinus Torvalds 4661333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 466286872cb5SThomas Graf if (err < 0) 466386872cb5SThomas Graf return err; 466486872cb5SThomas Graf 466567f69513SDavid Ahern if (cfg.fc_metric == 0) 466667f69513SDavid Ahern cfg.fc_metric = IP6_RT_PRIO_USER; 466767f69513SDavid Ahern 466851ebd318SNicolas Dichtel if (cfg.fc_mp) 4669333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 467051ebd318SNicolas Dichtel else 4671acb54e3cSDavid Ahern return ip6_route_add(&cfg, GFP_KERNEL, extack); 46721da177e4SLinus Torvalds } 46731da177e4SLinus Torvalds 46748d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt) 4675339bf98fSThomas Graf { 4676beb1afacSDavid Ahern int nexthop_len = 0; 4677beb1afacSDavid Ahern 467893c2fb25SDavid Ahern if (rt->fib6_nsiblings) { 4679beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 4680beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 4681beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 4682ad1601aeSDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws); 4683beb1afacSDavid Ahern 468493c2fb25SDavid Ahern nexthop_len *= rt->fib6_nsiblings; 4685beb1afacSDavid Ahern } 4686beb1afacSDavid Ahern 4687339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 4688339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 4689339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 4690339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 4691339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 4692339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 4693339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 4694339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 4695339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 46966a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 4697ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 4698c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 469919e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 4700ad1601aeSDavid Ahern + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws) 4701beb1afacSDavid Ahern + nexthop_len; 4702beb1afacSDavid Ahern } 4703beb1afacSDavid Ahern 4704d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb, 47058d1c802bSDavid Ahern struct fib6_info *rt, struct dst_entry *dst, 4706d4ead6b3SDavid Ahern struct in6_addr *dest, struct in6_addr *src, 470715e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4708f8cfe2ceSDavid Ahern unsigned int flags) 47091da177e4SLinus Torvalds { 471022d0bd82SXin Long struct rt6_info *rt6 = (struct rt6_info *)dst; 471122d0bd82SXin Long struct rt6key *rt6_dst, *rt6_src; 471222d0bd82SXin Long u32 *pmetrics, table, rt6_flags; 47131da177e4SLinus Torvalds struct nlmsghdr *nlh; 471422d0bd82SXin Long struct rtmsg *rtm; 4715d4ead6b3SDavid Ahern long expires = 0; 47161da177e4SLinus Torvalds 471715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 471838308473SDavid S. Miller if (!nlh) 471926932566SPatrick McHardy return -EMSGSIZE; 47202d7202bfSThomas Graf 472122d0bd82SXin Long if (rt6) { 472222d0bd82SXin Long rt6_dst = &rt6->rt6i_dst; 472322d0bd82SXin Long rt6_src = &rt6->rt6i_src; 472422d0bd82SXin Long rt6_flags = rt6->rt6i_flags; 472522d0bd82SXin Long } else { 472622d0bd82SXin Long rt6_dst = &rt->fib6_dst; 472722d0bd82SXin Long rt6_src = &rt->fib6_src; 472822d0bd82SXin Long rt6_flags = rt->fib6_flags; 472922d0bd82SXin Long } 473022d0bd82SXin Long 47312d7202bfSThomas Graf rtm = nlmsg_data(nlh); 47321da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 473322d0bd82SXin Long rtm->rtm_dst_len = rt6_dst->plen; 473422d0bd82SXin Long rtm->rtm_src_len = rt6_src->plen; 47351da177e4SLinus Torvalds rtm->rtm_tos = 0; 473693c2fb25SDavid Ahern if (rt->fib6_table) 473793c2fb25SDavid Ahern table = rt->fib6_table->tb6_id; 4738c71099acSThomas Graf else 47399e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 474097f0082aSKalash Nainwal rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; 4741c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4742c78679e8SDavid S. Miller goto nla_put_failure; 4743e8478e80SDavid Ahern 4744e8478e80SDavid Ahern rtm->rtm_type = rt->fib6_type; 47451da177e4SLinus Torvalds rtm->rtm_flags = 0; 47461da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 474793c2fb25SDavid Ahern rtm->rtm_protocol = rt->fib6_protocol; 47481da177e4SLinus Torvalds 474922d0bd82SXin Long if (rt6_flags & RTF_CACHE) 47501da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 47511da177e4SLinus Torvalds 4752d4ead6b3SDavid Ahern if (dest) { 4753d4ead6b3SDavid Ahern if (nla_put_in6_addr(skb, RTA_DST, dest)) 4754c78679e8SDavid S. Miller goto nla_put_failure; 47551da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 47561da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 475722d0bd82SXin Long if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) 4758c78679e8SDavid S. Miller goto nla_put_failure; 47591da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 47601da177e4SLinus Torvalds if (src) { 4761930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4762c78679e8SDavid S. Miller goto nla_put_failure; 47631da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4764c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 476522d0bd82SXin Long nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) 4766c78679e8SDavid S. Miller goto nla_put_failure; 47671da177e4SLinus Torvalds #endif 47687bc570c8SYOSHIFUJI Hideaki if (iif) { 47697bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 477022d0bd82SXin Long if (ipv6_addr_is_multicast(&rt6_dst->addr)) { 4771fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 47722cf75070SNikolay Aleksandrov 47737bc570c8SYOSHIFUJI Hideaki if (err == 0) 47747bc570c8SYOSHIFUJI Hideaki return 0; 4775fd61c6baSDavid Ahern if (err < 0) 47767bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 47777bc570c8SYOSHIFUJI Hideaki } else 47787bc570c8SYOSHIFUJI Hideaki #endif 4779c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4780c78679e8SDavid S. Miller goto nla_put_failure; 4781d4ead6b3SDavid Ahern } else if (dest) { 47821da177e4SLinus Torvalds struct in6_addr saddr_buf; 4783d4ead6b3SDavid Ahern if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && 4784930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4785c78679e8SDavid S. Miller goto nla_put_failure; 4786c3968a85SDaniel Walter } 4787c3968a85SDaniel Walter 478893c2fb25SDavid Ahern if (rt->fib6_prefsrc.plen) { 4789c3968a85SDaniel Walter struct in6_addr saddr_buf; 479093c2fb25SDavid Ahern saddr_buf = rt->fib6_prefsrc.addr; 4791930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4792c78679e8SDavid S. Miller goto nla_put_failure; 47931da177e4SLinus Torvalds } 47942d7202bfSThomas Graf 4795d4ead6b3SDavid Ahern pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; 4796d4ead6b3SDavid Ahern if (rtnetlink_put_metrics(skb, pmetrics) < 0) 47972d7202bfSThomas Graf goto nla_put_failure; 47982d7202bfSThomas Graf 479993c2fb25SDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) 4800beb1afacSDavid Ahern goto nla_put_failure; 4801beb1afacSDavid Ahern 4802beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4803beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4804beb1afacSDavid Ahern */ 480522d0bd82SXin Long if (rt6) { 480622d0bd82SXin Long if (rt6_flags & RTF_GATEWAY && 480722d0bd82SXin Long nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) 480822d0bd82SXin Long goto nla_put_failure; 480922d0bd82SXin Long 481022d0bd82SXin Long if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) 481122d0bd82SXin Long goto nla_put_failure; 481222d0bd82SXin Long } else if (rt->fib6_nsiblings) { 48138d1c802bSDavid Ahern struct fib6_info *sibling, *next_sibling; 4814beb1afacSDavid Ahern struct nlattr *mp; 4815beb1afacSDavid Ahern 4816ae0be8deSMichal Kubecek mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); 4817beb1afacSDavid Ahern if (!mp) 4818beb1afacSDavid Ahern goto nla_put_failure; 4819beb1afacSDavid Ahern 4820c0a72077SDavid Ahern if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common, 4821c0a72077SDavid Ahern rt->fib6_nh.fib_nh_weight) < 0) 4822beb1afacSDavid Ahern goto nla_put_failure; 4823beb1afacSDavid Ahern 4824beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 482593c2fb25SDavid Ahern &rt->fib6_siblings, fib6_siblings) { 4826c0a72077SDavid Ahern if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common, 4827c0a72077SDavid Ahern sibling->fib6_nh.fib_nh_weight) < 0) 482894f826b8SEric Dumazet goto nla_put_failure; 482994f826b8SEric Dumazet } 48302d7202bfSThomas Graf 4831beb1afacSDavid Ahern nla_nest_end(skb, mp); 4832beb1afacSDavid Ahern } else { 4833ecc5663cSDavid Ahern unsigned char nh_flags = 0; 4834ecc5663cSDavid Ahern 4835c0a72077SDavid Ahern if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common, 4836ecc5663cSDavid Ahern &nh_flags, false) < 0) 4837c78679e8SDavid S. Miller goto nla_put_failure; 4838ecc5663cSDavid Ahern 4839ecc5663cSDavid Ahern rtm->rtm_flags |= nh_flags; 4840beb1afacSDavid Ahern } 48418253947eSLi Wei 484222d0bd82SXin Long if (rt6_flags & RTF_EXPIRES) { 484314895687SDavid Ahern expires = dst ? dst->expires : rt->expires; 484414895687SDavid Ahern expires -= jiffies; 484514895687SDavid Ahern } 484669cdf8f9SYOSHIFUJI Hideaki 4847d4ead6b3SDavid Ahern if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) 4848e3703b3dSThomas Graf goto nla_put_failure; 48491da177e4SLinus Torvalds 485022d0bd82SXin Long if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) 4851c78ba6d6SLubomir Rintel goto nla_put_failure; 4852c78ba6d6SLubomir Rintel 485319e42e45SRoopa Prabhu 4854053c095aSJohannes Berg nlmsg_end(skb, nlh); 4855053c095aSJohannes Berg return 0; 48562d7202bfSThomas Graf 48572d7202bfSThomas Graf nla_put_failure: 485826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 485926932566SPatrick McHardy return -EMSGSIZE; 48601da177e4SLinus Torvalds } 48611da177e4SLinus Torvalds 486213e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i, 486313e38901SDavid Ahern const struct net_device *dev) 486413e38901SDavid Ahern { 4865ad1601aeSDavid Ahern if (f6i->fib6_nh.fib_nh_dev == dev) 486613e38901SDavid Ahern return true; 486713e38901SDavid Ahern 486813e38901SDavid Ahern if (f6i->fib6_nsiblings) { 486913e38901SDavid Ahern struct fib6_info *sibling, *next_sibling; 487013e38901SDavid Ahern 487113e38901SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 487213e38901SDavid Ahern &f6i->fib6_siblings, fib6_siblings) { 4873ad1601aeSDavid Ahern if (sibling->fib6_nh.fib_nh_dev == dev) 487413e38901SDavid Ahern return true; 487513e38901SDavid Ahern } 487613e38901SDavid Ahern } 487713e38901SDavid Ahern 487813e38901SDavid Ahern return false; 487913e38901SDavid Ahern } 488013e38901SDavid Ahern 48818d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg) 48821da177e4SLinus Torvalds { 48831da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 488413e38901SDavid Ahern struct fib_dump_filter *filter = &arg->filter; 488513e38901SDavid Ahern unsigned int flags = NLM_F_MULTI; 48861f17e2f2SDavid Ahern struct net *net = arg->net; 48871f17e2f2SDavid Ahern 4888421842edSDavid Ahern if (rt == net->ipv6.fib6_null_entry) 48891f17e2f2SDavid Ahern return 0; 48901da177e4SLinus Torvalds 489113e38901SDavid Ahern if ((filter->flags & RTM_F_PREFIX) && 489293c2fb25SDavid Ahern !(rt->fib6_flags & RTF_PREFIX_RT)) { 4893f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4894f8cfe2ceSDavid Ahern return 1; 4895f8cfe2ceSDavid Ahern } 489613e38901SDavid Ahern if (filter->filter_set) { 489713e38901SDavid Ahern if ((filter->rt_type && rt->fib6_type != filter->rt_type) || 489813e38901SDavid Ahern (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || 489913e38901SDavid Ahern (filter->protocol && rt->fib6_protocol != filter->protocol)) { 490013e38901SDavid Ahern return 1; 490113e38901SDavid Ahern } 490213e38901SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 4903f8cfe2ceSDavid Ahern } 49041da177e4SLinus Torvalds 4905d4ead6b3SDavid Ahern return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, 4906d4ead6b3SDavid Ahern RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, 490713e38901SDavid Ahern arg->cb->nlh->nlmsg_seq, flags); 49081da177e4SLinus Torvalds } 49091da177e4SLinus Torvalds 49100eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, 49110eff0a27SJakub Kicinski const struct nlmsghdr *nlh, 49120eff0a27SJakub Kicinski struct nlattr **tb, 49130eff0a27SJakub Kicinski struct netlink_ext_ack *extack) 49140eff0a27SJakub Kicinski { 49150eff0a27SJakub Kicinski struct rtmsg *rtm; 49160eff0a27SJakub Kicinski int i, err; 49170eff0a27SJakub Kicinski 49180eff0a27SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 49190eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 49200eff0a27SJakub Kicinski "Invalid header for get route request"); 49210eff0a27SJakub Kicinski return -EINVAL; 49220eff0a27SJakub Kicinski } 49230eff0a27SJakub Kicinski 49240eff0a27SJakub Kicinski if (!netlink_strict_get_check(skb)) 49258cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 49260eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 49270eff0a27SJakub Kicinski 49280eff0a27SJakub Kicinski rtm = nlmsg_data(nlh); 49290eff0a27SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || 49300eff0a27SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || 49310eff0a27SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || 49320eff0a27SJakub Kicinski rtm->rtm_type) { 49330eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); 49340eff0a27SJakub Kicinski return -EINVAL; 49350eff0a27SJakub Kicinski } 49360eff0a27SJakub Kicinski if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { 49370eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, 49380eff0a27SJakub Kicinski "Invalid flags for get route request"); 49390eff0a27SJakub Kicinski return -EINVAL; 49400eff0a27SJakub Kicinski } 49410eff0a27SJakub Kicinski 49428cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 49430eff0a27SJakub Kicinski rtm_ipv6_policy, extack); 49440eff0a27SJakub Kicinski if (err) 49450eff0a27SJakub Kicinski return err; 49460eff0a27SJakub Kicinski 49470eff0a27SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 49480eff0a27SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 49490eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); 49500eff0a27SJakub Kicinski return -EINVAL; 49510eff0a27SJakub Kicinski } 49520eff0a27SJakub Kicinski 49530eff0a27SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 49540eff0a27SJakub Kicinski if (!tb[i]) 49550eff0a27SJakub Kicinski continue; 49560eff0a27SJakub Kicinski 49570eff0a27SJakub Kicinski switch (i) { 49580eff0a27SJakub Kicinski case RTA_SRC: 49590eff0a27SJakub Kicinski case RTA_DST: 49600eff0a27SJakub Kicinski case RTA_IIF: 49610eff0a27SJakub Kicinski case RTA_OIF: 49620eff0a27SJakub Kicinski case RTA_MARK: 49630eff0a27SJakub Kicinski case RTA_UID: 49640eff0a27SJakub Kicinski case RTA_SPORT: 49650eff0a27SJakub Kicinski case RTA_DPORT: 49660eff0a27SJakub Kicinski case RTA_IP_PROTO: 49670eff0a27SJakub Kicinski break; 49680eff0a27SJakub Kicinski default: 49690eff0a27SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); 49700eff0a27SJakub Kicinski return -EINVAL; 49710eff0a27SJakub Kicinski } 49720eff0a27SJakub Kicinski } 49730eff0a27SJakub Kicinski 49740eff0a27SJakub Kicinski return 0; 49750eff0a27SJakub Kicinski } 49760eff0a27SJakub Kicinski 4977c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4978c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 49791da177e4SLinus Torvalds { 49803b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4981ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 498218c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 4983a68886a6SDavid Ahern struct fib6_info *from; 498418c3a61cSRoopa Prabhu struct dst_entry *dst; 49851da177e4SLinus Torvalds struct rt6_info *rt; 4986ab364a6fSThomas Graf struct sk_buff *skb; 4987ab364a6fSThomas Graf struct rtmsg *rtm; 4988744486d4SMaciej Żenczykowski struct flowi6 fl6 = {}; 498918c3a61cSRoopa Prabhu bool fibmatch; 4990ab364a6fSThomas Graf 49910eff0a27SJakub Kicinski err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 4992ab364a6fSThomas Graf if (err < 0) 4993ab364a6fSThomas Graf goto errout; 4994ab364a6fSThomas Graf 4995ab364a6fSThomas Graf err = -EINVAL; 499638b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 499738b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 499818c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4999ab364a6fSThomas Graf 5000ab364a6fSThomas Graf if (tb[RTA_SRC]) { 5001ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 5002ab364a6fSThomas Graf goto errout; 5003ab364a6fSThomas Graf 50044e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 5005ab364a6fSThomas Graf } 5006ab364a6fSThomas Graf 5007ab364a6fSThomas Graf if (tb[RTA_DST]) { 5008ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 5009ab364a6fSThomas Graf goto errout; 5010ab364a6fSThomas Graf 50114e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 5012ab364a6fSThomas Graf } 5013ab364a6fSThomas Graf 5014ab364a6fSThomas Graf if (tb[RTA_IIF]) 5015ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 5016ab364a6fSThomas Graf 5017ab364a6fSThomas Graf if (tb[RTA_OIF]) 501872331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 5019ab364a6fSThomas Graf 50202e47b291SLorenzo Colitti if (tb[RTA_MARK]) 50212e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 50222e47b291SLorenzo Colitti 5023622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 5024622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 5025622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 5026622ec2c9SLorenzo Colitti else 5027622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 5028622ec2c9SLorenzo Colitti 5029eacb9384SRoopa Prabhu if (tb[RTA_SPORT]) 5030eacb9384SRoopa Prabhu fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); 5031eacb9384SRoopa Prabhu 5032eacb9384SRoopa Prabhu if (tb[RTA_DPORT]) 5033eacb9384SRoopa Prabhu fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); 5034eacb9384SRoopa Prabhu 5035eacb9384SRoopa Prabhu if (tb[RTA_IP_PROTO]) { 5036eacb9384SRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 50375e1a99eaSHangbin Liu &fl6.flowi6_proto, AF_INET6, 50385e1a99eaSHangbin Liu extack); 5039eacb9384SRoopa Prabhu if (err) 5040eacb9384SRoopa Prabhu goto errout; 5041eacb9384SRoopa Prabhu } 5042eacb9384SRoopa Prabhu 5043ab364a6fSThomas Graf if (iif) { 5044ab364a6fSThomas Graf struct net_device *dev; 504572331bc0SShmulik Ladkani int flags = 0; 504672331bc0SShmulik Ladkani 5047121622dbSFlorian Westphal rcu_read_lock(); 5048121622dbSFlorian Westphal 5049121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 5050ab364a6fSThomas Graf if (!dev) { 5051121622dbSFlorian Westphal rcu_read_unlock(); 5052ab364a6fSThomas Graf err = -ENODEV; 5053ab364a6fSThomas Graf goto errout; 5054ab364a6fSThomas Graf } 505572331bc0SShmulik Ladkani 505672331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 505772331bc0SShmulik Ladkani 505872331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 505972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 506072331bc0SShmulik Ladkani 5061b75cc8f9SDavid Ahern dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); 5062121622dbSFlorian Westphal 5063121622dbSFlorian Westphal rcu_read_unlock(); 506472331bc0SShmulik Ladkani } else { 506572331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 506672331bc0SShmulik Ladkani 506718c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 506818c3a61cSRoopa Prabhu } 506918c3a61cSRoopa Prabhu 507018c3a61cSRoopa Prabhu 507118c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 507218c3a61cSRoopa Prabhu if (rt->dst.error) { 507318c3a61cSRoopa Prabhu err = rt->dst.error; 507418c3a61cSRoopa Prabhu ip6_rt_put(rt); 507518c3a61cSRoopa Prabhu goto errout; 5076ab364a6fSThomas Graf } 50771da177e4SLinus Torvalds 50789d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 50799d6acb3bSWANG Cong err = rt->dst.error; 50809d6acb3bSWANG Cong ip6_rt_put(rt); 50819d6acb3bSWANG Cong goto errout; 50829d6acb3bSWANG Cong } 50839d6acb3bSWANG Cong 50841da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 508538308473SDavid S. Miller if (!skb) { 508694e187c0SAmerigo Wang ip6_rt_put(rt); 5087ab364a6fSThomas Graf err = -ENOBUFS; 5088ab364a6fSThomas Graf goto errout; 5089ab364a6fSThomas Graf } 50901da177e4SLinus Torvalds 5091d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 5092a68886a6SDavid Ahern 5093a68886a6SDavid Ahern rcu_read_lock(); 5094a68886a6SDavid Ahern from = rcu_dereference(rt->from); 5095886b7a50SMartin KaFai Lau if (from) { 509618c3a61cSRoopa Prabhu if (fibmatch) 5097886b7a50SMartin KaFai Lau err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, 5098886b7a50SMartin KaFai Lau iif, RTM_NEWROUTE, 5099886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 510018c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 510118c3a61cSRoopa Prabhu else 5102a68886a6SDavid Ahern err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, 5103a68886a6SDavid Ahern &fl6.saddr, iif, RTM_NEWROUTE, 5104886b7a50SMartin KaFai Lau NETLINK_CB(in_skb).portid, 5105886b7a50SMartin KaFai Lau nlh->nlmsg_seq, 0); 5106886b7a50SMartin KaFai Lau } else { 5107886b7a50SMartin KaFai Lau err = -ENETUNREACH; 5108886b7a50SMartin KaFai Lau } 5109a68886a6SDavid Ahern rcu_read_unlock(); 5110a68886a6SDavid Ahern 51111da177e4SLinus Torvalds if (err < 0) { 5112ab364a6fSThomas Graf kfree_skb(skb); 5113ab364a6fSThomas Graf goto errout; 51141da177e4SLinus Torvalds } 51151da177e4SLinus Torvalds 511615e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 5117ab364a6fSThomas Graf errout: 51181da177e4SLinus Torvalds return err; 51191da177e4SLinus Torvalds } 51201da177e4SLinus Torvalds 51218d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, 512237a1d361SRoopa Prabhu unsigned int nlm_flags) 51231da177e4SLinus Torvalds { 51241da177e4SLinus Torvalds struct sk_buff *skb; 51255578689aSDaniel Lezcano struct net *net = info->nl_net; 5126528c4cebSDenis V. Lunev u32 seq; 5127528c4cebSDenis V. Lunev int err; 51280d51aa80SJamal Hadi Salim 5129528c4cebSDenis V. Lunev err = -ENOBUFS; 513038308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 513186872cb5SThomas Graf 513219e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 513338308473SDavid S. Miller if (!skb) 513421713ebcSThomas Graf goto errout; 51351da177e4SLinus Torvalds 5136d4ead6b3SDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 5137f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 513826932566SPatrick McHardy if (err < 0) { 513926932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 514026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 514126932566SPatrick McHardy kfree_skb(skb); 514226932566SPatrick McHardy goto errout; 514326932566SPatrick McHardy } 514415e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 51455578689aSDaniel Lezcano info->nlh, gfp_any()); 51461ce85fe4SPablo Neira Ayuso return; 514721713ebcSThomas Graf errout: 514821713ebcSThomas Graf if (err < 0) 51495578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 51501da177e4SLinus Torvalds } 51511da177e4SLinus Torvalds 515219a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt, 515319a3b7eeSDavid Ahern struct nl_info *info) 515419a3b7eeSDavid Ahern { 515519a3b7eeSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 515619a3b7eeSDavid Ahern struct sk_buff *skb; 515719a3b7eeSDavid Ahern int err = -ENOBUFS; 515819a3b7eeSDavid Ahern 515919a3b7eeSDavid Ahern /* call_fib6_entry_notifiers will be removed when in-kernel notifier 516019a3b7eeSDavid Ahern * is implemented and supported for nexthop objects 516119a3b7eeSDavid Ahern */ 516219a3b7eeSDavid Ahern call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); 516319a3b7eeSDavid Ahern 516419a3b7eeSDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 516519a3b7eeSDavid Ahern if (!skb) 516619a3b7eeSDavid Ahern goto errout; 516719a3b7eeSDavid Ahern 516819a3b7eeSDavid Ahern err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, 516919a3b7eeSDavid Ahern RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); 517019a3b7eeSDavid Ahern if (err < 0) { 517119a3b7eeSDavid Ahern /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 517219a3b7eeSDavid Ahern WARN_ON(err == -EMSGSIZE); 517319a3b7eeSDavid Ahern kfree_skb(skb); 517419a3b7eeSDavid Ahern goto errout; 517519a3b7eeSDavid Ahern } 517619a3b7eeSDavid Ahern rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 517719a3b7eeSDavid Ahern info->nlh, gfp_any()); 517819a3b7eeSDavid Ahern return; 517919a3b7eeSDavid Ahern errout: 518019a3b7eeSDavid Ahern if (err < 0) 518119a3b7eeSDavid Ahern rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 518219a3b7eeSDavid Ahern } 518319a3b7eeSDavid Ahern 51848ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 5185351638e7SJiri Pirko unsigned long event, void *ptr) 51868ed67789SDaniel Lezcano { 5187351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5188c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 51898ed67789SDaniel Lezcano 5190242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 5191242d3a49SWANG Cong return NOTIFY_OK; 5192242d3a49SWANG Cong 5193242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 5194ad1601aeSDavid Ahern net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev; 5195d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 51968ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 51978ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5198d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 51998ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 5200d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 52018ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 52028ed67789SDaniel Lezcano #endif 520376da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 520476da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 520576da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 520676da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 520776da0704SWANG Cong */ 520812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 5209242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 521012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 521112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 5212242d3a49SWANG Cong #endif 52138ed67789SDaniel Lezcano } 52148ed67789SDaniel Lezcano 52158ed67789SDaniel Lezcano return NOTIFY_OK; 52168ed67789SDaniel Lezcano } 52178ed67789SDaniel Lezcano 52181da177e4SLinus Torvalds /* 52191da177e4SLinus Torvalds * /proc 52201da177e4SLinus Torvalds */ 52211da177e4SLinus Torvalds 52221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 52231da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 52241da177e4SLinus Torvalds { 522569ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 52261da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 522769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 522869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 522981eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 523069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 523169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 5232fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 523369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 52341da177e4SLinus Torvalds 52351da177e4SLinus Torvalds return 0; 52361da177e4SLinus Torvalds } 52371da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 52381da177e4SLinus Torvalds 52391da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 52401da177e4SLinus Torvalds 52411da177e4SLinus Torvalds static 5242fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 52431da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 52441da177e4SLinus Torvalds { 5245c486da34SLucian Adrian Grijincu struct net *net; 5246c486da34SLucian Adrian Grijincu int delay; 5247f0fb9b28SAditya Pakki int ret; 5248c486da34SLucian Adrian Grijincu if (!write) 5249c486da34SLucian Adrian Grijincu return -EINVAL; 5250c486da34SLucian Adrian Grijincu 5251c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 5252c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 5253f0fb9b28SAditya Pakki ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5254f0fb9b28SAditya Pakki if (ret) 5255f0fb9b28SAditya Pakki return ret; 5256f0fb9b28SAditya Pakki 52572ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 52581da177e4SLinus Torvalds return 0; 52591da177e4SLinus Torvalds } 52601da177e4SLinus Torvalds 52617c6bb7d2SDavid Ahern static int zero; 52627c6bb7d2SDavid Ahern static int one = 1; 52637c6bb7d2SDavid Ahern 5264ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = { 52651da177e4SLinus Torvalds { 52661da177e4SLinus Torvalds .procname = "flush", 52674990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 52681da177e4SLinus Torvalds .maxlen = sizeof(int), 526989c8b3a1SDave Jones .mode = 0200, 52706d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 52711da177e4SLinus Torvalds }, 52721da177e4SLinus Torvalds { 52731da177e4SLinus Torvalds .procname = "gc_thresh", 52749a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 52751da177e4SLinus Torvalds .maxlen = sizeof(int), 52761da177e4SLinus Torvalds .mode = 0644, 52776d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 52781da177e4SLinus Torvalds }, 52791da177e4SLinus Torvalds { 52801da177e4SLinus Torvalds .procname = "max_size", 52814990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 52821da177e4SLinus Torvalds .maxlen = sizeof(int), 52831da177e4SLinus Torvalds .mode = 0644, 52846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 52851da177e4SLinus Torvalds }, 52861da177e4SLinus Torvalds { 52871da177e4SLinus Torvalds .procname = "gc_min_interval", 52884990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 52891da177e4SLinus Torvalds .maxlen = sizeof(int), 52901da177e4SLinus Torvalds .mode = 0644, 52916d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 52921da177e4SLinus Torvalds }, 52931da177e4SLinus Torvalds { 52941da177e4SLinus Torvalds .procname = "gc_timeout", 52954990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 52961da177e4SLinus Torvalds .maxlen = sizeof(int), 52971da177e4SLinus Torvalds .mode = 0644, 52986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 52991da177e4SLinus Torvalds }, 53001da177e4SLinus Torvalds { 53011da177e4SLinus Torvalds .procname = "gc_interval", 53024990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 53031da177e4SLinus Torvalds .maxlen = sizeof(int), 53041da177e4SLinus Torvalds .mode = 0644, 53056d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 53061da177e4SLinus Torvalds }, 53071da177e4SLinus Torvalds { 53081da177e4SLinus Torvalds .procname = "gc_elasticity", 53094990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 53101da177e4SLinus Torvalds .maxlen = sizeof(int), 53111da177e4SLinus Torvalds .mode = 0644, 5312f3d3f616SMin Zhang .proc_handler = proc_dointvec, 53131da177e4SLinus Torvalds }, 53141da177e4SLinus Torvalds { 53151da177e4SLinus Torvalds .procname = "mtu_expires", 53164990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 53171da177e4SLinus Torvalds .maxlen = sizeof(int), 53181da177e4SLinus Torvalds .mode = 0644, 53196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 53201da177e4SLinus Torvalds }, 53211da177e4SLinus Torvalds { 53221da177e4SLinus Torvalds .procname = "min_adv_mss", 53234990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 53241da177e4SLinus Torvalds .maxlen = sizeof(int), 53251da177e4SLinus Torvalds .mode = 0644, 5326f3d3f616SMin Zhang .proc_handler = proc_dointvec, 53271da177e4SLinus Torvalds }, 53281da177e4SLinus Torvalds { 53291da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 53304990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 53311da177e4SLinus Torvalds .maxlen = sizeof(int), 53321da177e4SLinus Torvalds .mode = 0644, 53336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 53341da177e4SLinus Torvalds }, 53357c6bb7d2SDavid Ahern { 53367c6bb7d2SDavid Ahern .procname = "skip_notify_on_dev_down", 53377c6bb7d2SDavid Ahern .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, 53387c6bb7d2SDavid Ahern .maxlen = sizeof(int), 53397c6bb7d2SDavid Ahern .mode = 0644, 53407c6bb7d2SDavid Ahern .proc_handler = proc_dointvec, 53417c6bb7d2SDavid Ahern .extra1 = &zero, 53427c6bb7d2SDavid Ahern .extra2 = &one, 53437c6bb7d2SDavid Ahern }, 5344f8572d8fSEric W. Biederman { } 53451da177e4SLinus Torvalds }; 53461da177e4SLinus Torvalds 53472c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 5348760f2d01SDaniel Lezcano { 5349760f2d01SDaniel Lezcano struct ctl_table *table; 5350760f2d01SDaniel Lezcano 5351760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 5352760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 5353760f2d01SDaniel Lezcano GFP_KERNEL); 53545ee09105SYOSHIFUJI Hideaki 53555ee09105SYOSHIFUJI Hideaki if (table) { 53565ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 5357c486da34SLucian Adrian Grijincu table[0].extra1 = net; 535886393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 53595ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 53605ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 53615ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 53625ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 53635ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 53645ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 53655ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 53669c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 53677c6bb7d2SDavid Ahern table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; 5368464dc801SEric W. Biederman 5369464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 5370464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 5371464dc801SEric W. Biederman table[0].procname = NULL; 53725ee09105SYOSHIFUJI Hideaki } 53735ee09105SYOSHIFUJI Hideaki 5374760f2d01SDaniel Lezcano return table; 5375760f2d01SDaniel Lezcano } 53761da177e4SLinus Torvalds #endif 53771da177e4SLinus Torvalds 53782c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 5379cdb18761SDaniel Lezcano { 5380633d424bSPavel Emelyanov int ret = -ENOMEM; 53818ed67789SDaniel Lezcano 538286393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 538386393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 5384f2fc6a54SBenjamin Thery 5385fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 5386fc66f95cSEric Dumazet goto out_ip6_dst_ops; 5387fc66f95cSEric Dumazet 5388421842edSDavid Ahern net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template, 5389421842edSDavid Ahern sizeof(*net->ipv6.fib6_null_entry), 5390421842edSDavid Ahern GFP_KERNEL); 5391421842edSDavid Ahern if (!net->ipv6.fib6_null_entry) 5392421842edSDavid Ahern goto out_ip6_dst_entries; 5393421842edSDavid Ahern 53948ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 53958ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 53968ed67789SDaniel Lezcano GFP_KERNEL); 53978ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 5398421842edSDavid Ahern goto out_fib6_null_entry; 5399d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 540062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 540162fa8a84SDavid S. Miller ip6_template_metrics, true); 54028ed67789SDaniel Lezcano 54038ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 5404feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 54058ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 54068ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 54078ed67789SDaniel Lezcano GFP_KERNEL); 540868fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 540968fffc67SPeter Zijlstra goto out_ip6_null_entry; 5410d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 541162fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 541262fa8a84SDavid S. Miller ip6_template_metrics, true); 54138ed67789SDaniel Lezcano 54148ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 54158ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 54168ed67789SDaniel Lezcano GFP_KERNEL); 541768fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 541868fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 5419d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 542062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 542162fa8a84SDavid S. Miller ip6_template_metrics, true); 54228ed67789SDaniel Lezcano #endif 54238ed67789SDaniel Lezcano 5424b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 5425b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 5426b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 5427b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 5428b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 5429b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 5430b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 5431b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 54327c6bb7d2SDavid Ahern net->ipv6.sysctl.skip_notify_on_dev_down = 0; 5433b339a47cSPeter Zijlstra 54346891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 54356891a346SBenjamin Thery 54368ed67789SDaniel Lezcano ret = 0; 54378ed67789SDaniel Lezcano out: 54388ed67789SDaniel Lezcano return ret; 5439f2fc6a54SBenjamin Thery 544068fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 544168fffc67SPeter Zijlstra out_ip6_prohibit_entry: 544268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 544368fffc67SPeter Zijlstra out_ip6_null_entry: 544468fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 544568fffc67SPeter Zijlstra #endif 5446421842edSDavid Ahern out_fib6_null_entry: 5447421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 5448fc66f95cSEric Dumazet out_ip6_dst_entries: 5449fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5450f2fc6a54SBenjamin Thery out_ip6_dst_ops: 5451f2fc6a54SBenjamin Thery goto out; 5452cdb18761SDaniel Lezcano } 5453cdb18761SDaniel Lezcano 54542c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 5455cdb18761SDaniel Lezcano { 5456421842edSDavid Ahern kfree(net->ipv6.fib6_null_entry); 54578ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 54588ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 54598ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 54608ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 54618ed67789SDaniel Lezcano #endif 546241bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 5463cdb18761SDaniel Lezcano } 5464cdb18761SDaniel Lezcano 5465d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 5466d189634eSThomas Graf { 5467d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5468c3506372SChristoph Hellwig proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, 5469c3506372SChristoph Hellwig sizeof(struct ipv6_route_iter)); 54703617d949SChristoph Hellwig proc_create_net_single("rt6_stats", 0444, net->proc_net, 54713617d949SChristoph Hellwig rt6_stats_seq_show, NULL); 5472d189634eSThomas Graf #endif 5473d189634eSThomas Graf return 0; 5474d189634eSThomas Graf } 5475d189634eSThomas Graf 5476d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 5477d189634eSThomas Graf { 5478d189634eSThomas Graf #ifdef CONFIG_PROC_FS 5479ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 5480ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 5481d189634eSThomas Graf #endif 5482d189634eSThomas Graf } 5483d189634eSThomas Graf 5484cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 5485cdb18761SDaniel Lezcano .init = ip6_route_net_init, 5486cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 5487cdb18761SDaniel Lezcano }; 5488cdb18761SDaniel Lezcano 5489c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 5490c3426b47SDavid S. Miller { 5491c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 5492c3426b47SDavid S. Miller 5493c3426b47SDavid S. Miller if (!bp) 5494c3426b47SDavid S. Miller return -ENOMEM; 5495c3426b47SDavid S. Miller inet_peer_base_init(bp); 5496c3426b47SDavid S. Miller net->ipv6.peers = bp; 5497c3426b47SDavid S. Miller return 0; 5498c3426b47SDavid S. Miller } 5499c3426b47SDavid S. Miller 5500c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5501c3426b47SDavid S. Miller { 5502c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5503c3426b47SDavid S. Miller 5504c3426b47SDavid S. Miller net->ipv6.peers = NULL; 550556a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5506c3426b47SDavid S. Miller kfree(bp); 5507c3426b47SDavid S. Miller } 5508c3426b47SDavid S. Miller 55092b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5510c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5511c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 5512c3426b47SDavid S. Miller }; 5513c3426b47SDavid S. Miller 5514d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 5515d189634eSThomas Graf .init = ip6_route_net_init_late, 5516d189634eSThomas Graf .exit = ip6_route_net_exit_late, 5517d189634eSThomas Graf }; 5518d189634eSThomas Graf 55198ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 55208ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 5521242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 55228ed67789SDaniel Lezcano }; 55238ed67789SDaniel Lezcano 55242f460933SWANG Cong void __init ip6_route_init_special_entries(void) 55252f460933SWANG Cong { 55262f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 55272f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 55282f460933SWANG Cong * manually for init_net */ 5529ad1601aeSDavid Ahern init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev; 55302f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 55312f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 55322f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 55332f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 55342f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 55352f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 55362f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 55372f460933SWANG Cong #endif 55382f460933SWANG Cong } 55392f460933SWANG Cong 5540433d49c3SDaniel Lezcano int __init ip6_route_init(void) 55411da177e4SLinus Torvalds { 5542433d49c3SDaniel Lezcano int ret; 55438d0b94afSMartin KaFai Lau int cpu; 5544433d49c3SDaniel Lezcano 55459a7ec3a9SDaniel Lezcano ret = -ENOMEM; 55469a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 55479a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 55489a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 55499a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 5550c19a28e1SFernando Carrijo goto out; 555114e50e57SDavid S. Miller 5552fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 55538ed67789SDaniel Lezcano if (ret) 5554bdb3289fSDaniel Lezcano goto out_kmem_cache; 5555bdb3289fSDaniel Lezcano 5556c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 5557c3426b47SDavid S. Miller if (ret) 5558e8803b6cSDavid S. Miller goto out_dst_entries; 55592a0c451aSThomas Graf 55607e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 55617e52b33bSDavid S. Miller if (ret) 55627e52b33bSDavid S. Miller goto out_register_inetpeer; 5563c3426b47SDavid S. Miller 55645dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 55655dc121e9SArnaud Ebalard 5566e8803b6cSDavid S. Miller ret = fib6_init(); 5567433d49c3SDaniel Lezcano if (ret) 55688ed67789SDaniel Lezcano goto out_register_subsys; 5569433d49c3SDaniel Lezcano 5570433d49c3SDaniel Lezcano ret = xfrm6_init(); 5571433d49c3SDaniel Lezcano if (ret) 5572e8803b6cSDavid S. Miller goto out_fib6_init; 5573c35b7e72SDaniel Lezcano 5574433d49c3SDaniel Lezcano ret = fib6_rules_init(); 5575433d49c3SDaniel Lezcano if (ret) 5576433d49c3SDaniel Lezcano goto xfrm6_init; 55777e5449c2SDaniel Lezcano 5578d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 5579d189634eSThomas Graf if (ret) 5580d189634eSThomas Graf goto fib6_rules_init; 5581d189634eSThomas Graf 558216feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 558316feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 558416feebcfSFlorian Westphal if (ret < 0) 558516feebcfSFlorian Westphal goto out_register_late_subsys; 558616feebcfSFlorian Westphal 558716feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 558816feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 558916feebcfSFlorian Westphal if (ret < 0) 559016feebcfSFlorian Westphal goto out_register_late_subsys; 559116feebcfSFlorian Westphal 559216feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 559316feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 559416feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 559516feebcfSFlorian Westphal if (ret < 0) 5596d189634eSThomas Graf goto out_register_late_subsys; 5597433d49c3SDaniel Lezcano 55988ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 5599cdb18761SDaniel Lezcano if (ret) 5600d189634eSThomas Graf goto out_register_late_subsys; 56018ed67789SDaniel Lezcano 56028d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 56038d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 56048d0b94afSMartin KaFai Lau 56058d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 56068d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 56078d0b94afSMartin KaFai Lau } 56088d0b94afSMartin KaFai Lau 5609433d49c3SDaniel Lezcano out: 5610433d49c3SDaniel Lezcano return ret; 5611433d49c3SDaniel Lezcano 5612d189634eSThomas Graf out_register_late_subsys: 561316feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 5614d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5615433d49c3SDaniel Lezcano fib6_rules_init: 5616433d49c3SDaniel Lezcano fib6_rules_cleanup(); 5617433d49c3SDaniel Lezcano xfrm6_init: 5618433d49c3SDaniel Lezcano xfrm6_fini(); 56192a0c451aSThomas Graf out_fib6_init: 56202a0c451aSThomas Graf fib6_gc_cleanup(); 56218ed67789SDaniel Lezcano out_register_subsys: 56228ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 56237e52b33bSDavid S. Miller out_register_inetpeer: 56247e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 5625fc66f95cSEric Dumazet out_dst_entries: 5626fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 5627433d49c3SDaniel Lezcano out_kmem_cache: 5628f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 5629433d49c3SDaniel Lezcano goto out; 56301da177e4SLinus Torvalds } 56311da177e4SLinus Torvalds 56321da177e4SLinus Torvalds void ip6_route_cleanup(void) 56331da177e4SLinus Torvalds { 56348ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 5635d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5636101367c2SThomas Graf fib6_rules_cleanup(); 56371da177e4SLinus Torvalds xfrm6_fini(); 56381da177e4SLinus Torvalds fib6_gc_cleanup(); 5639c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 56408ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 564141bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 5642f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 56431da177e4SLinus Torvalds } 5644