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> 47*35732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 66b811580dSDavid Ahern #include <trace/events/fib6.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 711da177e4SLinus Torvalds #include <linux/sysctl.h> 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 74afc154e9SHannes Frederic Sowa enum rt6_nud_state { 757e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 767e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 777e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 78afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 79afc154e9SHannes Frederic Sowa }; 80afc154e9SHannes Frederic Sowa 8183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 821da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 830dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 84ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 851da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 881da177e4SLinus Torvalds struct net_device *dev, int how); 89569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 92ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 937150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 94ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 951da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 966700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 976700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 996700c270SDavid S. Miller struct sk_buff *skb); 1004b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 10152bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 10216a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt); 10316a16cd3SDavid Ahern static int rt6_fill_node(struct net *net, 10416a16cd3SDavid Ahern struct sk_buff *skb, struct rt6_info *rt, 10516a16cd3SDavid Ahern struct in6_addr *dst, struct in6_addr *src, 10616a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10716a16cd3SDavid Ahern unsigned int flags); 108*35732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 109*35732d01SWei Wang struct in6_addr *daddr, 110*35732d01SWei Wang struct in6_addr *saddr); 1111da177e4SLinus Torvalds 11270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 113efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 114b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 115830218c1SDavid Ahern const struct in6_addr *gwaddr, 116830218c1SDavid Ahern struct net_device *dev, 11795c96174SEric Dumazet unsigned int pref); 118efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_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); 12270ceb4f5SYOSHIFUJI Hideaki #endif 12370ceb4f5SYOSHIFUJI Hideaki 1248d0b94afSMartin KaFai Lau struct uncached_list { 1258d0b94afSMartin KaFai Lau spinlock_t lock; 1268d0b94afSMartin KaFai Lau struct list_head head; 1278d0b94afSMartin KaFai Lau }; 1288d0b94afSMartin KaFai Lau 1298d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt) 1328d0b94afSMartin KaFai Lau { 1338d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1348d0b94afSMartin KaFai Lau 1358d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1388d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1398d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1408d0b94afSMartin KaFai Lau } 1418d0b94afSMartin KaFai Lau 1428d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1438d0b94afSMartin KaFai Lau { 1448d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1458d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1468d0b94afSMartin KaFai Lau 1478d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1488d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1498d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1508d0b94afSMartin KaFai Lau } 1518d0b94afSMartin KaFai Lau } 1528d0b94afSMartin KaFai Lau 1538d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1548d0b94afSMartin KaFai Lau { 1558d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1568d0b94afSMartin KaFai Lau int cpu; 1578d0b94afSMartin KaFai Lau 158e332bc67SEric W. Biederman if (dev == loopback_dev) 159e332bc67SEric W. Biederman return; 160e332bc67SEric W. Biederman 1618d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1628d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1638d0b94afSMartin KaFai Lau struct rt6_info *rt; 1648d0b94afSMartin KaFai Lau 1658d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1668d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1678d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1688d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1698d0b94afSMartin KaFai Lau 170e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1718d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1728d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1738d0b94afSMartin KaFai Lau } 1748d0b94afSMartin KaFai Lau 175e332bc67SEric W. Biederman if (rt_dev == dev) { 1768d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1778d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1788d0b94afSMartin KaFai Lau dev_put(rt_dev); 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau } 1818d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau 185d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 186d52d3997SMartin KaFai Lau { 187d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 188d52d3997SMartin KaFai Lau } 189d52d3997SMartin KaFai Lau 19006582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 19106582540SDavid S. Miller { 19206582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19306582540SDavid S. Miller 194d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 195d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 196d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1974b32b5adSMartin KaFai Lau return NULL; 1984b32b5adSMartin KaFai Lau else 1993b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 20006582540SDavid S. Miller } 20106582540SDavid S. Miller 202f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 203f894cbf8SDavid S. Miller struct sk_buff *skb, 204f894cbf8SDavid S. Miller const void *daddr) 20539232973SDavid S. Miller { 20639232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20739232973SDavid S. Miller 208a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20939232973SDavid S. Miller return (const void *) p; 210f894cbf8SDavid S. Miller else if (skb) 211f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 21239232973SDavid S. Miller return daddr; 21339232973SDavid S. Miller } 21439232973SDavid S. Miller 215f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 216f894cbf8SDavid S. Miller struct sk_buff *skb, 217f894cbf8SDavid S. Miller const void *daddr) 218d3aaeb38SDavid S. Miller { 21939232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 22039232973SDavid S. Miller struct neighbour *n; 22139232973SDavid S. Miller 222f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2238e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 224f83c7790SDavid S. Miller if (n) 225f83c7790SDavid S. Miller return n; 226f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 227f83c7790SDavid S. Miller } 228f83c7790SDavid S. Miller 22963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23063fca65dSJulian Anastasov { 23163fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23263fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23363fca65dSJulian Anastasov 23463fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23563fca65dSJulian Anastasov if (!daddr) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23863fca65dSJulian Anastasov return; 23963fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24063fca65dSJulian Anastasov return; 24163fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24263fca65dSJulian Anastasov } 24363fca65dSJulian Anastasov 2449a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2451da177e4SLinus Torvalds .family = AF_INET6, 2461da177e4SLinus Torvalds .gc = ip6_dst_gc, 2471da177e4SLinus Torvalds .gc_thresh = 1024, 2481da177e4SLinus Torvalds .check = ip6_dst_check, 2490dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 250ebb762f2SSteffen Klassert .mtu = ip6_mtu, 25106582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2521da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2531da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2541da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2551da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2561da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2576e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2589f8955ccSEric W. Biederman .local_out = __ip6_local_out, 259d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26063fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 263ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 264ec831ea7SRoland Dreier { 265618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 266618f9bc7SSteffen Klassert 267618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 268ec831ea7SRoland Dreier } 269ec831ea7SRoland Dreier 2706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2716700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27214e50e57SDavid S. Miller { 27314e50e57SDavid S. Miller } 27414e50e57SDavid S. Miller 2756700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2766700c270SDavid S. Miller struct sk_buff *skb) 277b587ee3bSDavid S. Miller { 278b587ee3bSDavid S. Miller } 279b587ee3bSDavid S. Miller 28014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28114e50e57SDavid S. Miller .family = AF_INET6, 28214e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28314e50e57SDavid S. Miller .check = ip6_dst_check, 284ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 285214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28614e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 287b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2880a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 289d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 29014e50e57SDavid S. Miller }; 29114e50e57SDavid S. Miller 29262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29314edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29462fa8a84SDavid S. Miller }; 29562fa8a84SDavid S. Miller 296fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2971da177e4SLinus Torvalds .dst = { 2981da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2991da177e4SLinus Torvalds .__use = 1, 3002c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3011da177e4SLinus Torvalds .error = -ENETUNREACH, 3021da177e4SLinus Torvalds .input = ip6_pkt_discard, 3031da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3041da177e4SLinus Torvalds }, 3051da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3064f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3071da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3081da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 311101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 312101367c2SThomas Graf 313fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 314101367c2SThomas Graf .dst = { 315101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 316101367c2SThomas Graf .__use = 1, 3172c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 318101367c2SThomas Graf .error = -EACCES, 3199ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3209ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 321101367c2SThomas Graf }, 322101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3234f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 324101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 325101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 326101367c2SThomas Graf }; 327101367c2SThomas Graf 328fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 329101367c2SThomas Graf .dst = { 330101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 331101367c2SThomas Graf .__use = 1, 3322c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 333101367c2SThomas Graf .error = -EINVAL, 334352e512cSHerbert Xu .input = dst_discard, 335ede2059dSEric W. Biederman .output = dst_discard_out, 336101367c2SThomas Graf }, 337101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3384f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 339101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 340101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 341101367c2SThomas Graf }; 342101367c2SThomas Graf 343101367c2SThomas Graf #endif 344101367c2SThomas Graf 345ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 346ebfa45f0SMartin KaFai Lau { 347ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 348ebfa45f0SMartin KaFai Lau 349ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 350ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 351ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 352ebfa45f0SMartin KaFai Lau } 353ebfa45f0SMartin KaFai Lau 3541da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 355d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 356957c665fSDavid S. Miller struct net_device *dev, 357ad706862SMartin KaFai Lau int flags) 3581da177e4SLinus Torvalds { 35997bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 360b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 361cf911662SDavid S. Miller 362ebfa45f0SMartin KaFai Lau if (rt) 363ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3648104891bSSteffen Klassert 365cf911662SDavid S. Miller return rt; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3689ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 369d52d3997SMartin KaFai Lau struct net_device *dev, 370ad706862SMartin KaFai Lau int flags) 371d52d3997SMartin KaFai Lau { 372ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 373d52d3997SMartin KaFai Lau 374d52d3997SMartin KaFai Lau if (rt) { 375d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 376d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 377d52d3997SMartin KaFai Lau int cpu; 378d52d3997SMartin KaFai Lau 379d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 380d52d3997SMartin KaFai Lau struct rt6_info **p; 381d52d3997SMartin KaFai Lau 382d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 383d52d3997SMartin KaFai Lau /* no one shares rt */ 384d52d3997SMartin KaFai Lau *p = NULL; 385d52d3997SMartin KaFai Lau } 386d52d3997SMartin KaFai Lau } else { 387587fea74SWei Wang dst_release_immediate(&rt->dst); 388d52d3997SMartin KaFai Lau return NULL; 389d52d3997SMartin KaFai Lau } 390d52d3997SMartin KaFai Lau } 391d52d3997SMartin KaFai Lau 392d52d3997SMartin KaFai Lau return rt; 393d52d3997SMartin KaFai Lau } 3949ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 395d52d3997SMartin KaFai Lau 3961da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3971da177e4SLinus Torvalds { 3981da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 399*35732d01SWei Wang struct rt6_exception_bucket *bucket; 400ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 4018d0b94afSMartin KaFai Lau struct inet6_dev *idev; 4021da177e4SLinus Torvalds 4038e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 404d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4058d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4068d0b94afSMartin KaFai Lau 4078d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40838308473SDavid S. Miller if (idev) { 4091da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4101da177e4SLinus Torvalds in6_dev_put(idev); 4111da177e4SLinus Torvalds } 412*35732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 413*35732d01SWei Wang if (bucket) { 414*35732d01SWei Wang rt->rt6i_exception_bucket = NULL; 415*35732d01SWei Wang kfree(bucket); 416*35732d01SWei Wang } 4171716a961SGao feng 418ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 419ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 420b3419363SDavid S. Miller } 421b3419363SDavid S. Miller 4221da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4231da177e4SLinus Torvalds int how) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4261da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4275a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 428c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4291da177e4SLinus Torvalds 430e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 431e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 43238308473SDavid S. Miller if (loopback_idev) { 4331da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4341da177e4SLinus Torvalds in6_dev_put(idev); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds } 43797cac082SDavid S. Miller } 4381da177e4SLinus Torvalds 4395973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4405973fb1eSMartin KaFai Lau { 4415973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4425973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4435973fb1eSMartin KaFai Lau else 4445973fb1eSMartin KaFai Lau return false; 4455973fb1eSMartin KaFai Lau } 4465973fb1eSMartin KaFai Lau 447a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4481da177e4SLinus Torvalds { 4491716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4501716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 451a50feda5SEric Dumazet return true; 4521716a961SGao feng } else if (rt->dst.from) { 4531e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4541e2ea8adSXin Long rt6_check_expired((struct rt6_info *)rt->dst.from); 4551716a961SGao feng } 456a50feda5SEric Dumazet return false; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 45951ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 46052bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46152bd4c0cSNicolas Dichtel int strict) 46251ebd318SNicolas Dichtel { 46351ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46451ebd318SNicolas Dichtel int route_choosen; 46551ebd318SNicolas Dichtel 466b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 467b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 468b673d6ccSJakub Sitnicki */ 469b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 470b673d6ccSJakub Sitnicki fl6->mp_hash = rt6_multipath_hash(fl6, NULL); 471b673d6ccSJakub Sitnicki 472b673d6ccSJakub Sitnicki route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1); 47351ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 47451ebd318SNicolas Dichtel * (siblings does not include ourself) 47551ebd318SNicolas Dichtel */ 47651ebd318SNicolas Dichtel if (route_choosen) 47751ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47851ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47951ebd318SNicolas Dichtel route_choosen--; 48051ebd318SNicolas Dichtel if (route_choosen == 0) { 48152bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48252bd4c0cSNicolas Dichtel break; 48351ebd318SNicolas Dichtel match = sibling; 48451ebd318SNicolas Dichtel break; 48551ebd318SNicolas Dichtel } 48651ebd318SNicolas Dichtel } 48751ebd318SNicolas Dichtel return match; 48851ebd318SNicolas Dichtel } 48951ebd318SNicolas Dichtel 4901da177e4SLinus Torvalds /* 491c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds 4948ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4958ed67789SDaniel Lezcano struct rt6_info *rt, 496b71d1d42SEric Dumazet const struct in6_addr *saddr, 4971da177e4SLinus Torvalds int oif, 498d420895eSYOSHIFUJI Hideaki int flags) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct rt6_info *local = NULL; 5011da177e4SLinus Torvalds struct rt6_info *sprt; 5021da177e4SLinus Torvalds 503dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 504dd3abc4eSYOSHIFUJI Hideaki goto out; 505dd3abc4eSYOSHIFUJI Hideaki 506d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 507d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 508dd3abc4eSYOSHIFUJI Hideaki 509dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5101da177e4SLinus Torvalds if (dev->ifindex == oif) 5111da177e4SLinus Torvalds return sprt; 5121da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51338308473SDavid S. Miller if (!sprt->rt6i_idev || 5141da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51517fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5161da177e4SLinus Torvalds continue; 51717fb0b2bSDavid Ahern if (local && 51817fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5191da177e4SLinus Torvalds continue; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds local = sprt; 5221da177e4SLinus Torvalds } 523dd3abc4eSYOSHIFUJI Hideaki } else { 524dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 525dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 526dd3abc4eSYOSHIFUJI Hideaki return sprt; 527dd3abc4eSYOSHIFUJI Hideaki } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5311da177e4SLinus Torvalds if (local) 5321da177e4SLinus Torvalds return local; 5331da177e4SLinus Torvalds 534d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5358ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5361da177e4SLinus Torvalds } 537dd3abc4eSYOSHIFUJI Hideaki out: 5381da177e4SLinus Torvalds return rt; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 54127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 542c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 543c2f17e82SHannes Frederic Sowa struct work_struct work; 544c2f17e82SHannes Frederic Sowa struct in6_addr target; 545c2f17e82SHannes Frederic Sowa struct net_device *dev; 546c2f17e82SHannes Frederic Sowa }; 547c2f17e82SHannes Frederic Sowa 548c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 549c2f17e82SHannes Frederic Sowa { 550c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 551c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 552c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 553c2f17e82SHannes Frederic Sowa 554c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 555adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 556c2f17e82SHannes Frederic Sowa dev_put(work->dev); 557662f5533SMichael Büsch kfree(work); 558c2f17e82SHannes Frederic Sowa } 559c2f17e82SHannes Frederic Sowa 56027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 56127097255SYOSHIFUJI Hideaki { 562990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 563f2c31e32SEric Dumazet struct neighbour *neigh; 56427097255SYOSHIFUJI Hideaki /* 56527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56827097255SYOSHIFUJI Hideaki * 56927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 57027097255SYOSHIFUJI Hideaki * to no more than one per minute. 57127097255SYOSHIFUJI Hideaki */ 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 573fdd6681dSAmerigo Wang return; 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5778d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5788d6c31bfSMartin KaFai Lau goto out; 5798d6c31bfSMartin KaFai Lau 580990edb42SMartin KaFai Lau work = NULL; 5812152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 582990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 583990edb42SMartin KaFai Lau time_after(jiffies, 584990edb42SMartin KaFai Lau neigh->updated + 585990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 586c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 587990edb42SMartin KaFai Lau if (work) 5887e980569SJiri Benc __neigh_set_probe_once(neigh); 589990edb42SMartin KaFai Lau } 590c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 591990edb42SMartin KaFai Lau } else { 592990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 593990edb42SMartin KaFai Lau } 594c2f17e82SHannes Frederic Sowa 595c2f17e82SHannes Frederic Sowa if (work) { 596c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 597c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 598c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 599c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 600c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 601c2f17e82SHannes Frederic Sowa } 602990edb42SMartin KaFai Lau 6038d6c31bfSMartin KaFai Lau out: 6042152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 605f2c31e32SEric Dumazet } 60627097255SYOSHIFUJI Hideaki #else 60727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60827097255SYOSHIFUJI Hideaki { 60927097255SYOSHIFUJI Hideaki } 61027097255SYOSHIFUJI Hideaki #endif 61127097255SYOSHIFUJI Hideaki 6121da177e4SLinus Torvalds /* 613554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6141da177e4SLinus Torvalds */ 615b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6161da177e4SLinus Torvalds { 617d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 618161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 619554cfb7eSYOSHIFUJI Hideaki return 2; 620161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 621161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 622161980f4SDavid S. Miller return 1; 623554cfb7eSYOSHIFUJI Hideaki return 0; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 626afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6271da177e4SLinus Torvalds { 628f2c31e32SEric Dumazet struct neighbour *neigh; 629afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 630f2c31e32SEric Dumazet 6314d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6324d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 633afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 638145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 639554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 640afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 641398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 642a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 643afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6447e980569SJiri Benc else 6457e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 646398bcbebSYOSHIFUJI Hideaki #endif 647145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 648afc154e9SHannes Frederic Sowa } else { 649afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6507e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 651a5a81f0bSPaul Marks } 652145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 653145a3621SYOSHIFUJI Hideaki / 吉藤英明 654a5a81f0bSPaul Marks return ret; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 657554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 658554cfb7eSYOSHIFUJI Hideaki int strict) 659554cfb7eSYOSHIFUJI Hideaki { 660a5a81f0bSPaul Marks int m; 6614d0c5911SYOSHIFUJI Hideaki 6624d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66377d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 664afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 665ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 666ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 667ebacaaa0SYOSHIFUJI Hideaki #endif 668afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 669afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 670afc154e9SHannes Frederic Sowa if (n < 0) 671afc154e9SHannes Frederic Sowa return n; 672afc154e9SHannes Frederic Sowa } 673554cfb7eSYOSHIFUJI Hideaki return m; 674554cfb7eSYOSHIFUJI Hideaki } 675554cfb7eSYOSHIFUJI Hideaki 676f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 677afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 678afc154e9SHannes Frederic Sowa bool *do_rr) 679554cfb7eSYOSHIFUJI Hideaki { 680554cfb7eSYOSHIFUJI Hideaki int m; 681afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68235103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68335103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68435103d11SAndy Gospodarek 68535103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 686d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 687d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68835103d11SAndy Gospodarek goto out; 689554cfb7eSYOSHIFUJI Hideaki 690554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 691f11e6659SDavid S. Miller goto out; 692554cfb7eSYOSHIFUJI Hideaki 693554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6947e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 695afc154e9SHannes Frederic Sowa match_do_rr = true; 696afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6977e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 698f11e6659SDavid S. Miller goto out; 6991da177e4SLinus Torvalds } 700f11e6659SDavid S. Miller 701afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 702afc154e9SHannes Frederic Sowa rt6_probe(rt); 703afc154e9SHannes Frederic Sowa 7047e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 705afc154e9SHannes Frederic Sowa if (m > *mpri) { 706afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 707afc154e9SHannes Frederic Sowa *mpri = m; 708afc154e9SHannes Frederic Sowa match = rt; 709afc154e9SHannes Frederic Sowa } 710f11e6659SDavid S. Miller out: 711f11e6659SDavid S. Miller return match; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 714f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 715f11e6659SDavid S. Miller struct rt6_info *rr_head, 716afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 717afc154e9SHannes Frederic Sowa bool *do_rr) 718f11e6659SDavid S. Miller { 7199fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 720f11e6659SDavid S. Miller int mpri = -1; 721f11e6659SDavid S. Miller 722f11e6659SDavid S. Miller match = NULL; 7239fbdcfafSSteffen Klassert cont = NULL; 7249fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7259fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7269fbdcfafSSteffen Klassert cont = rt; 7279fbdcfafSSteffen Klassert break; 7289fbdcfafSSteffen Klassert } 7299fbdcfafSSteffen Klassert 730afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7319fbdcfafSSteffen Klassert } 7329fbdcfafSSteffen Klassert 7339fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7349fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7359fbdcfafSSteffen Klassert cont = rt; 7369fbdcfafSSteffen Klassert break; 7379fbdcfafSSteffen Klassert } 7389fbdcfafSSteffen Klassert 7399fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7409fbdcfafSSteffen Klassert } 7419fbdcfafSSteffen Klassert 7429fbdcfafSSteffen Klassert if (match || !cont) 7439fbdcfafSSteffen Klassert return match; 7449fbdcfafSSteffen Klassert 7459fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 746afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 747f11e6659SDavid S. Miller 748f11e6659SDavid S. Miller return match; 749f11e6659SDavid S. Miller } 750f11e6659SDavid S. Miller 751f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 752f11e6659SDavid S. Miller { 753f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7548ed67789SDaniel Lezcano struct net *net; 755afc154e9SHannes Frederic Sowa bool do_rr = false; 756f11e6659SDavid S. Miller 757f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 758f11e6659SDavid S. Miller if (!rt0) 759f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 760f11e6659SDavid S. Miller 761afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 762afc154e9SHannes Frederic Sowa &do_rr); 763f11e6659SDavid S. Miller 764afc154e9SHannes Frederic Sowa if (do_rr) { 765d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 766f11e6659SDavid S. Miller 767554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 768f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 769f11e6659SDavid S. Miller next = fn->leaf; 770f11e6659SDavid S. Miller 771f11e6659SDavid S. Miller if (next != rt0) 772f11e6659SDavid S. Miller fn->rr_ptr = next; 773554cfb7eSYOSHIFUJI Hideaki } 774554cfb7eSYOSHIFUJI Hideaki 775d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 776a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7798b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7808b9df265SMartin KaFai Lau { 7818b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7828b9df265SMartin KaFai Lau } 7838b9df265SMartin KaFai Lau 78470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 786b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78770ceb4f5SYOSHIFUJI Hideaki { 788c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 79070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 79170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7924bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 79370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 79470ceb4f5SYOSHIFUJI Hideaki 79570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 79670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79770ceb4f5SYOSHIFUJI Hideaki } 79870ceb4f5SYOSHIFUJI Hideaki 79970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 80070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 80170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 80670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80770ceb4f5SYOSHIFUJI Hideaki } 80870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 81070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81170ceb4f5SYOSHIFUJI Hideaki } 81270ceb4f5SYOSHIFUJI Hideaki } 81370ceb4f5SYOSHIFUJI Hideaki 81470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8163933fc95SJens Rosenboom return -EINVAL; 81770ceb4f5SYOSHIFUJI Hideaki 8184bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81970ceb4f5SYOSHIFUJI Hideaki 82070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 82170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82270ceb4f5SYOSHIFUJI Hideaki else { 82370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 82670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82870ceb4f5SYOSHIFUJI Hideaki } 82970ceb4f5SYOSHIFUJI Hideaki 830f104a567SDuan Jiong if (rinfo->prefix_len == 0) 831f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 832f104a567SDuan Jiong else 833f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 834830218c1SDavid Ahern gwaddr, dev); 83570ceb4f5SYOSHIFUJI Hideaki 83670ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 837e0a1ad73SThomas Graf ip6_del_rt(rt); 83870ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83970ceb4f5SYOSHIFUJI Hideaki } 84070ceb4f5SYOSHIFUJI Hideaki 84170ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 842830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 843830218c1SDavid Ahern dev, pref); 84470ceb4f5SYOSHIFUJI Hideaki else if (rt) 84570ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 84670ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84770ceb4f5SYOSHIFUJI Hideaki 84870ceb4f5SYOSHIFUJI Hideaki if (rt) { 8491716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8501716a961SGao feng rt6_clean_expires(rt); 8511716a961SGao feng else 8521716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8531716a961SGao feng 85494e187c0SAmerigo Wang ip6_rt_put(rt); 85570ceb4f5SYOSHIFUJI Hideaki } 85670ceb4f5SYOSHIFUJI Hideaki return 0; 85770ceb4f5SYOSHIFUJI Hideaki } 85870ceb4f5SYOSHIFUJI Hideaki #endif 85970ceb4f5SYOSHIFUJI Hideaki 860a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 861a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 862a3c00e46SMartin KaFai Lau { 863a3c00e46SMartin KaFai Lau struct fib6_node *pn; 864a3c00e46SMartin KaFai Lau while (1) { 865a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 866a3c00e46SMartin KaFai Lau return NULL; 867a3c00e46SMartin KaFai Lau pn = fn->parent; 868a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 869a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 870a3c00e46SMartin KaFai Lau else 871a3c00e46SMartin KaFai Lau fn = pn; 872a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 873a3c00e46SMartin KaFai Lau return fn; 874a3c00e46SMartin KaFai Lau } 875a3c00e46SMartin KaFai Lau } 876c71099acSThomas Graf 8778ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8788ed67789SDaniel Lezcano struct fib6_table *table, 8794c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds struct fib6_node *fn; 8821da177e4SLinus Torvalds struct rt6_info *rt; 8831da177e4SLinus Torvalds 884c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8854c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 886c71099acSThomas Graf restart: 887c71099acSThomas Graf rt = fn->leaf; 8884c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 88951ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 89052bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 891a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 892a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 893a3c00e46SMartin KaFai Lau if (fn) 894a3c00e46SMartin KaFai Lau goto restart; 895a3c00e46SMartin KaFai Lau } 896d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 897c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 898b811580dSDavid Ahern 899b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 900b811580dSDavid Ahern 9011da177e4SLinus Torvalds return rt; 902c71099acSThomas Graf 903c71099acSThomas Graf } 904c71099acSThomas Graf 905ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 906ea6e574eSFlorian Westphal int flags) 907ea6e574eSFlorian Westphal { 908ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 909ea6e574eSFlorian Westphal } 910ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 911ea6e574eSFlorian Westphal 9129acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9139acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 914c71099acSThomas Graf { 9154c9483b2SDavid S. Miller struct flowi6 fl6 = { 9164c9483b2SDavid S. Miller .flowi6_oif = oif, 9174c9483b2SDavid S. Miller .daddr = *daddr, 918c71099acSThomas Graf }; 919c71099acSThomas Graf struct dst_entry *dst; 92077d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 921c71099acSThomas Graf 922adaa70bbSThomas Graf if (saddr) { 9234c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 924adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 925adaa70bbSThomas Graf } 926adaa70bbSThomas Graf 9274c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 928c71099acSThomas Graf if (dst->error == 0) 929c71099acSThomas Graf return (struct rt6_info *) dst; 930c71099acSThomas Graf 931c71099acSThomas Graf dst_release(dst); 932c71099acSThomas Graf 9331da177e4SLinus Torvalds return NULL; 9341da177e4SLinus Torvalds } 9357159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9367159039aSYOSHIFUJI Hideaki 937c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9381cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9391cfb71eeSWei Wang * route is released. 9401cfb71eeSWei Wang * Caller must hold dst before calling it. 9411da177e4SLinus Torvalds */ 9421da177e4SLinus Torvalds 943e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 944333c4301SDavid Ahern struct mx6_config *mxc, 945333c4301SDavid Ahern struct netlink_ext_ack *extack) 9461da177e4SLinus Torvalds { 9471da177e4SLinus Torvalds int err; 948c71099acSThomas Graf struct fib6_table *table; 9491da177e4SLinus Torvalds 950c71099acSThomas Graf table = rt->rt6i_table; 951c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 952333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 953c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds return err; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 95840e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 95940e22e8fSThomas Graf { 960e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 961e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 962e715b6d3SFlorian Westphal 9631cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9641cfb71eeSWei Wang dst_hold(&rt->dst); 965333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 96640e22e8fSThomas Graf } 96740e22e8fSThomas Graf 9684832c30dSDavid Ahern /* called with rcu_lock held */ 9694832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 9704832c30dSDavid Ahern { 9714832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 9724832c30dSDavid Ahern 9734832c30dSDavid Ahern if (rt->rt6i_flags & RTF_LOCAL) { 9744832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 9754832c30dSDavid Ahern * device if it is a master device, the master device if 9764832c30dSDavid Ahern * device is enslaved, and the loopback as the default 9774832c30dSDavid Ahern */ 9784832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 9794832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 9804832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 9814832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 9824832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 9834832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 9844832c30dSDavid Ahern * case we want dev returned to be dev 9854832c30dSDavid Ahern */ 9864832c30dSDavid Ahern } 9874832c30dSDavid Ahern 9884832c30dSDavid Ahern return dev; 9894832c30dSDavid Ahern } 9904832c30dSDavid Ahern 9918b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 99221efcfa0SEric Dumazet const struct in6_addr *daddr, 993b71d1d42SEric Dumazet const struct in6_addr *saddr) 9941da177e4SLinus Torvalds { 9954832c30dSDavid Ahern struct net_device *dev; 9961da177e4SLinus Torvalds struct rt6_info *rt; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* 9991da177e4SLinus Torvalds * Clone the route. 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds 1002d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 100383a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 10041da177e4SLinus Torvalds 10054832c30dSDavid Ahern rcu_read_lock(); 10064832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10074832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10084832c30dSDavid Ahern rcu_read_unlock(); 100983a09abdSMartin KaFai Lau if (!rt) 101083a09abdSMartin KaFai Lau return NULL; 101183a09abdSMartin KaFai Lau 101283a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10138b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 101483a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 101583a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 101683a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 101783a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10188b9df265SMartin KaFai Lau 10198b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1020bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 102121efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 102258c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10241da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10254e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10261da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds #endif 102995a9a5baSYOSHIFUJI Hideaki } 103095a9a5baSYOSHIFUJI Hideaki 1031299d9939SYOSHIFUJI Hideaki return rt; 1032299d9939SYOSHIFUJI Hideaki } 1033299d9939SYOSHIFUJI Hideaki 1034d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1035d52d3997SMartin KaFai Lau { 10364832c30dSDavid Ahern struct net_device *dev; 1037d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1038d52d3997SMartin KaFai Lau 10394832c30dSDavid Ahern rcu_read_lock(); 10404832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10414832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10424832c30dSDavid Ahern rcu_read_unlock(); 1043d52d3997SMartin KaFai Lau if (!pcpu_rt) 1044d52d3997SMartin KaFai Lau return NULL; 1045d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1046d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1047d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1048d52d3997SMartin KaFai Lau return pcpu_rt; 1049d52d3997SMartin KaFai Lau } 1050d52d3997SMartin KaFai Lau 1051d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1052d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1053d52d3997SMartin KaFai Lau { 1054a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1055d52d3997SMartin KaFai Lau 1056d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1057d52d3997SMartin KaFai Lau pcpu_rt = *p; 1058d52d3997SMartin KaFai Lau 1059a73e4195SMartin KaFai Lau if (pcpu_rt) { 1060a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1061a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1062a73e4195SMartin KaFai Lau } 1063a73e4195SMartin KaFai Lau return pcpu_rt; 1064a73e4195SMartin KaFai Lau } 1065a73e4195SMartin KaFai Lau 1066a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1067a73e4195SMartin KaFai Lau { 10689c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1069a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1070d52d3997SMartin KaFai Lau 1071d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1072d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1073d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1074d52d3997SMartin KaFai Lau 10759c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10769c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1077d52d3997SMartin KaFai Lau } 1078d52d3997SMartin KaFai Lau 10799c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10809c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1081a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1082d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1083d52d3997SMartin KaFai Lau if (prev) { 1084d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1085587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1086d52d3997SMartin KaFai Lau pcpu_rt = prev; 1087d52d3997SMartin KaFai Lau } 10889c7370a1SMartin KaFai Lau } else { 10899c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10909c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10919c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10929c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10939c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10949c7370a1SMartin KaFai Lau */ 1095587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 10969c7370a1SMartin KaFai Lau pcpu_rt = rt; 10979c7370a1SMartin KaFai Lau } 1098d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1099d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 11009c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1101d52d3997SMartin KaFai Lau return pcpu_rt; 1102d52d3997SMartin KaFai Lau } 1103d52d3997SMartin KaFai Lau 1104*35732d01SWei Wang /* exception hash table implementation 1105*35732d01SWei Wang */ 1106*35732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 1107*35732d01SWei Wang 1108*35732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 1109*35732d01SWei Wang * Caller must hold rt6_exception_lock 1110*35732d01SWei Wang */ 1111*35732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 1112*35732d01SWei Wang struct rt6_exception *rt6_ex) 1113*35732d01SWei Wang { 1114*35732d01SWei Wang if (!bucket || !rt6_ex) 1115*35732d01SWei Wang return; 1116*35732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 1117*35732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 1118*35732d01SWei Wang rt6_release(rt6_ex->rt6i); 1119*35732d01SWei Wang kfree_rcu(rt6_ex, rcu); 1120*35732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 1121*35732d01SWei Wang bucket->depth--; 1122*35732d01SWei Wang } 1123*35732d01SWei Wang 1124*35732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 1125*35732d01SWei Wang * Caller must hold rt6_exception_lock 1126*35732d01SWei Wang */ 1127*35732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 1128*35732d01SWei Wang { 1129*35732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 1130*35732d01SWei Wang 1131*35732d01SWei Wang if (!bucket) 1132*35732d01SWei Wang return; 1133*35732d01SWei Wang 1134*35732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1135*35732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 1136*35732d01SWei Wang oldest = rt6_ex; 1137*35732d01SWei Wang } 1138*35732d01SWei Wang rt6_remove_exception(bucket, oldest); 1139*35732d01SWei Wang } 1140*35732d01SWei Wang 1141*35732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 1142*35732d01SWei Wang const struct in6_addr *src) 1143*35732d01SWei Wang { 1144*35732d01SWei Wang static u32 seed __read_mostly; 1145*35732d01SWei Wang u32 val; 1146*35732d01SWei Wang 1147*35732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 1148*35732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 1149*35732d01SWei Wang 1150*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1151*35732d01SWei Wang if (src) 1152*35732d01SWei Wang val = jhash(src, sizeof(*src), val); 1153*35732d01SWei Wang #endif 1154*35732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 1155*35732d01SWei Wang } 1156*35732d01SWei Wang 1157*35732d01SWei Wang /* Helper function to find the cached rt in the hash table 1158*35732d01SWei Wang * and update bucket pointer to point to the bucket for this 1159*35732d01SWei Wang * (daddr, saddr) pair 1160*35732d01SWei Wang * Caller must hold rt6_exception_lock 1161*35732d01SWei Wang */ 1162*35732d01SWei Wang static struct rt6_exception * 1163*35732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 1164*35732d01SWei Wang const struct in6_addr *daddr, 1165*35732d01SWei Wang const struct in6_addr *saddr) 1166*35732d01SWei Wang { 1167*35732d01SWei Wang struct rt6_exception *rt6_ex; 1168*35732d01SWei Wang u32 hval; 1169*35732d01SWei Wang 1170*35732d01SWei Wang if (!(*bucket) || !daddr) 1171*35732d01SWei Wang return NULL; 1172*35732d01SWei Wang 1173*35732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 1174*35732d01SWei Wang *bucket += hval; 1175*35732d01SWei Wang 1176*35732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 1177*35732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 1178*35732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 1179*35732d01SWei Wang 1180*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1181*35732d01SWei Wang if (matched && saddr) 1182*35732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 1183*35732d01SWei Wang #endif 1184*35732d01SWei Wang if (matched) 1185*35732d01SWei Wang return rt6_ex; 1186*35732d01SWei Wang } 1187*35732d01SWei Wang return NULL; 1188*35732d01SWei Wang } 1189*35732d01SWei Wang 1190*35732d01SWei Wang /* Helper function to find the cached rt in the hash table 1191*35732d01SWei Wang * and update bucket pointer to point to the bucket for this 1192*35732d01SWei Wang * (daddr, saddr) pair 1193*35732d01SWei Wang * Caller must hold rcu_read_lock() 1194*35732d01SWei Wang */ 1195*35732d01SWei Wang static struct rt6_exception * 1196*35732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 1197*35732d01SWei Wang const struct in6_addr *daddr, 1198*35732d01SWei Wang const struct in6_addr *saddr) 1199*35732d01SWei Wang { 1200*35732d01SWei Wang struct rt6_exception *rt6_ex; 1201*35732d01SWei Wang u32 hval; 1202*35732d01SWei Wang 1203*35732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 1204*35732d01SWei Wang 1205*35732d01SWei Wang if (!(*bucket) || !daddr) 1206*35732d01SWei Wang return NULL; 1207*35732d01SWei Wang 1208*35732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 1209*35732d01SWei Wang *bucket += hval; 1210*35732d01SWei Wang 1211*35732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 1212*35732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 1213*35732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 1214*35732d01SWei Wang 1215*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1216*35732d01SWei Wang if (matched && saddr) 1217*35732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 1218*35732d01SWei Wang #endif 1219*35732d01SWei Wang if (matched) 1220*35732d01SWei Wang return rt6_ex; 1221*35732d01SWei Wang } 1222*35732d01SWei Wang return NULL; 1223*35732d01SWei Wang } 1224*35732d01SWei Wang 1225*35732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 1226*35732d01SWei Wang struct rt6_info *ort) 1227*35732d01SWei Wang { 1228*35732d01SWei Wang struct rt6_exception_bucket *bucket; 1229*35732d01SWei Wang struct in6_addr *src_key = NULL; 1230*35732d01SWei Wang struct rt6_exception *rt6_ex; 1231*35732d01SWei Wang int err = 0; 1232*35732d01SWei Wang 1233*35732d01SWei Wang /* ort can't be a cache or pcpu route */ 1234*35732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 1235*35732d01SWei Wang ort = (struct rt6_info *)ort->dst.from; 1236*35732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 1237*35732d01SWei Wang 1238*35732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1239*35732d01SWei Wang 1240*35732d01SWei Wang if (ort->exception_bucket_flushed) { 1241*35732d01SWei Wang err = -EINVAL; 1242*35732d01SWei Wang goto out; 1243*35732d01SWei Wang } 1244*35732d01SWei Wang 1245*35732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 1246*35732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 1247*35732d01SWei Wang if (!bucket) { 1248*35732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 1249*35732d01SWei Wang GFP_ATOMIC); 1250*35732d01SWei Wang if (!bucket) { 1251*35732d01SWei Wang err = -ENOMEM; 1252*35732d01SWei Wang goto out; 1253*35732d01SWei Wang } 1254*35732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 1255*35732d01SWei Wang } 1256*35732d01SWei Wang 1257*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1258*35732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 1259*35732d01SWei Wang * and exception table is indexed by a hash of 1260*35732d01SWei Wang * both rt6i_dst and rt6i_src. 1261*35732d01SWei Wang * Otherwise, the exception table is indexed by 1262*35732d01SWei Wang * a hash of only rt6i_dst. 1263*35732d01SWei Wang */ 1264*35732d01SWei Wang if (ort->rt6i_src.plen) 1265*35732d01SWei Wang src_key = &nrt->rt6i_src.addr; 1266*35732d01SWei Wang #endif 1267*35732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 1268*35732d01SWei Wang src_key); 1269*35732d01SWei Wang if (rt6_ex) 1270*35732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1271*35732d01SWei Wang 1272*35732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 1273*35732d01SWei Wang if (!rt6_ex) { 1274*35732d01SWei Wang err = -ENOMEM; 1275*35732d01SWei Wang goto out; 1276*35732d01SWei Wang } 1277*35732d01SWei Wang rt6_ex->rt6i = nrt; 1278*35732d01SWei Wang rt6_ex->stamp = jiffies; 1279*35732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 1280*35732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 1281*35732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 1282*35732d01SWei Wang bucket->depth++; 1283*35732d01SWei Wang 1284*35732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 1285*35732d01SWei Wang rt6_exception_remove_oldest(bucket); 1286*35732d01SWei Wang 1287*35732d01SWei Wang out: 1288*35732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 1289*35732d01SWei Wang 1290*35732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1291*35732d01SWei Wang if (!err) 1292*35732d01SWei Wang fib6_update_sernum(ort); 1293*35732d01SWei Wang 1294*35732d01SWei Wang return err; 1295*35732d01SWei Wang } 1296*35732d01SWei Wang 1297*35732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 1298*35732d01SWei Wang { 1299*35732d01SWei Wang struct rt6_exception_bucket *bucket; 1300*35732d01SWei Wang struct rt6_exception *rt6_ex; 1301*35732d01SWei Wang struct hlist_node *tmp; 1302*35732d01SWei Wang int i; 1303*35732d01SWei Wang 1304*35732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1305*35732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 1306*35732d01SWei Wang rt->exception_bucket_flushed = 1; 1307*35732d01SWei Wang 1308*35732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1309*35732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 1310*35732d01SWei Wang if (!bucket) 1311*35732d01SWei Wang goto out; 1312*35732d01SWei Wang 1313*35732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1314*35732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 1315*35732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1316*35732d01SWei Wang WARN_ON_ONCE(bucket->depth); 1317*35732d01SWei Wang bucket++; 1318*35732d01SWei Wang } 1319*35732d01SWei Wang 1320*35732d01SWei Wang out: 1321*35732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 1322*35732d01SWei Wang } 1323*35732d01SWei Wang 1324*35732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 1325*35732d01SWei Wang * Caller has to hold rcu_read_lock() 1326*35732d01SWei Wang */ 1327*35732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 1328*35732d01SWei Wang struct in6_addr *daddr, 1329*35732d01SWei Wang struct in6_addr *saddr) 1330*35732d01SWei Wang { 1331*35732d01SWei Wang struct rt6_exception_bucket *bucket; 1332*35732d01SWei Wang struct in6_addr *src_key = NULL; 1333*35732d01SWei Wang struct rt6_exception *rt6_ex; 1334*35732d01SWei Wang struct rt6_info *res = NULL; 1335*35732d01SWei Wang 1336*35732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 1337*35732d01SWei Wang 1338*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1339*35732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 1340*35732d01SWei Wang * and exception table is indexed by a hash of 1341*35732d01SWei Wang * both rt6i_dst and rt6i_src. 1342*35732d01SWei Wang * Otherwise, the exception table is indexed by 1343*35732d01SWei Wang * a hash of only rt6i_dst. 1344*35732d01SWei Wang */ 1345*35732d01SWei Wang if (rt->rt6i_src.plen) 1346*35732d01SWei Wang src_key = saddr; 1347*35732d01SWei Wang #endif 1348*35732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 1349*35732d01SWei Wang 1350*35732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 1351*35732d01SWei Wang res = rt6_ex->rt6i; 1352*35732d01SWei Wang 1353*35732d01SWei Wang return res; 1354*35732d01SWei Wang } 1355*35732d01SWei Wang 1356*35732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 1357*35732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 1358*35732d01SWei Wang { 1359*35732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 1360*35732d01SWei Wang struct rt6_exception_bucket *bucket; 1361*35732d01SWei Wang struct in6_addr *src_key = NULL; 1362*35732d01SWei Wang struct rt6_exception *rt6_ex; 1363*35732d01SWei Wang int err; 1364*35732d01SWei Wang 1365*35732d01SWei Wang if (!from || 1366*35732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 1367*35732d01SWei Wang return -EINVAL; 1368*35732d01SWei Wang 1369*35732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 1370*35732d01SWei Wang return -ENOENT; 1371*35732d01SWei Wang 1372*35732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 1373*35732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 1374*35732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 1375*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1376*35732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 1377*35732d01SWei Wang * and exception table is indexed by a hash of 1378*35732d01SWei Wang * both rt6i_dst and rt6i_src. 1379*35732d01SWei Wang * Otherwise, the exception table is indexed by 1380*35732d01SWei Wang * a hash of only rt6i_dst. 1381*35732d01SWei Wang */ 1382*35732d01SWei Wang if (from->rt6i_src.plen) 1383*35732d01SWei Wang src_key = &rt->rt6i_src.addr; 1384*35732d01SWei Wang #endif 1385*35732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 1386*35732d01SWei Wang &rt->rt6i_dst.addr, 1387*35732d01SWei Wang src_key); 1388*35732d01SWei Wang if (rt6_ex) { 1389*35732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 1390*35732d01SWei Wang err = 0; 1391*35732d01SWei Wang } else { 1392*35732d01SWei Wang err = -ENOENT; 1393*35732d01SWei Wang } 1394*35732d01SWei Wang 1395*35732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 1396*35732d01SWei Wang return err; 1397*35732d01SWei Wang } 1398*35732d01SWei Wang 1399*35732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 1400*35732d01SWei Wang * refresh its stamp 1401*35732d01SWei Wang */ 1402*35732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 1403*35732d01SWei Wang { 1404*35732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 1405*35732d01SWei Wang struct rt6_exception_bucket *bucket; 1406*35732d01SWei Wang struct in6_addr *src_key = NULL; 1407*35732d01SWei Wang struct rt6_exception *rt6_ex; 1408*35732d01SWei Wang 1409*35732d01SWei Wang if (!from || 1410*35732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 1411*35732d01SWei Wang return; 1412*35732d01SWei Wang 1413*35732d01SWei Wang rcu_read_lock(); 1414*35732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 1415*35732d01SWei Wang 1416*35732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 1417*35732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 1418*35732d01SWei Wang * and exception table is indexed by a hash of 1419*35732d01SWei Wang * both rt6i_dst and rt6i_src. 1420*35732d01SWei Wang * Otherwise, the exception table is indexed by 1421*35732d01SWei Wang * a hash of only rt6i_dst. 1422*35732d01SWei Wang */ 1423*35732d01SWei Wang if (from->rt6i_src.plen) 1424*35732d01SWei Wang src_key = &rt->rt6i_src.addr; 1425*35732d01SWei Wang #endif 1426*35732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 1427*35732d01SWei Wang &rt->rt6i_dst.addr, 1428*35732d01SWei Wang src_key); 1429*35732d01SWei Wang if (rt6_ex) 1430*35732d01SWei Wang rt6_ex->stamp = jiffies; 1431*35732d01SWei Wang 1432*35732d01SWei Wang rcu_read_unlock(); 1433*35732d01SWei Wang } 1434*35732d01SWei Wang 14359ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 14369ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 14371da177e4SLinus Torvalds { 1438367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 143945e4fd26SMartin KaFai Lau struct rt6_info *rt; 1440c71099acSThomas Graf int strict = 0; 14411da177e4SLinus Torvalds 144277d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1443d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1444367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1445367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 14461da177e4SLinus Torvalds 1447c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 14481da177e4SLinus Torvalds 14494c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1450367efcb9SMartin KaFai Lau saved_fn = fn; 14511da177e4SLinus Torvalds 1452ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1453ca254490SDavid Ahern oif = 0; 1454ca254490SDavid Ahern 1455a3c00e46SMartin KaFai Lau redo_rt6_select: 1456367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 145752bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1458367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1459a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1460a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1461a3c00e46SMartin KaFai Lau if (fn) 1462a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1463367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1464367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1465367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1466367efcb9SMartin KaFai Lau fn = saved_fn; 1467367efcb9SMartin KaFai Lau goto redo_rt6_select; 1468367efcb9SMartin KaFai Lau } 1469a3c00e46SMartin KaFai Lau } 1470a3c00e46SMartin KaFai Lau 1471d52d3997SMartin KaFai Lau 1472d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 14733da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1474c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 14751da177e4SLinus Torvalds 1476d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1477b811580dSDavid Ahern 1478b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1479d52d3997SMartin KaFai Lau return rt; 14803da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 14813da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 14823da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 14833da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 14843da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 14853da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 14863da59bd9SMartin KaFai Lau */ 1487c71099acSThomas Graf 14883da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 14893da59bd9SMartin KaFai Lau 1490d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1491d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1492d52d3997SMartin KaFai Lau 14933da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 14943da59bd9SMartin KaFai Lau dst_release(&rt->dst); 14953da59bd9SMartin KaFai Lau 14961cfb71eeSWei Wang if (uncached_rt) { 14971cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 14981cfb71eeSWei Wang * No need for another dst_hold() 14991cfb71eeSWei Wang */ 15008d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 15011cfb71eeSWei Wang } else { 15023da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 15033da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 15041cfb71eeSWei Wang } 1505b811580dSDavid Ahern 1506b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 15073da59bd9SMartin KaFai Lau return uncached_rt; 15083da59bd9SMartin KaFai Lau 1509d52d3997SMartin KaFai Lau } else { 1510d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1511d52d3997SMartin KaFai Lau 1512d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1513d52d3997SMartin KaFai Lau 1514d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1515d52d3997SMartin KaFai Lau rt->dst.__use++; 1516d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1517d52d3997SMartin KaFai Lau 15189c7370a1SMartin KaFai Lau if (pcpu_rt) { 1519d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 15209c7370a1SMartin KaFai Lau } else { 15219c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 15229c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 15239c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 15249c7370a1SMartin KaFai Lau */ 15259c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 15269c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 15279c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 15289c7370a1SMartin KaFai Lau dst_release(&rt->dst); 15299c7370a1SMartin KaFai Lau } 1530d52d3997SMartin KaFai Lau 1531b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1532d52d3997SMartin KaFai Lau return pcpu_rt; 15339c7370a1SMartin KaFai Lau 1534d52d3997SMartin KaFai Lau } 1535c71099acSThomas Graf } 15369ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1537c71099acSThomas Graf 15388ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 15394c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 15404acad72dSPavel Emelyanov { 15414c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 15424acad72dSPavel Emelyanov } 15434acad72dSPavel Emelyanov 1544d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 154572331bc0SShmulik Ladkani struct net_device *dev, 154672331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 154772331bc0SShmulik Ladkani { 154872331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 154972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 155072331bc0SShmulik Ladkani 155172331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 155272331bc0SShmulik Ladkani } 1553d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 155472331bc0SShmulik Ladkani 155523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 155623aebdacSJakub Sitnicki struct flow_keys *keys) 155723aebdacSJakub Sitnicki { 155823aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 155923aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 156023aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 156123aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 156223aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 156323aebdacSJakub Sitnicki 156423aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 156523aebdacSJakub Sitnicki goto out; 156623aebdacSJakub Sitnicki 156723aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 156823aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 156923aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 157023aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 157123aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 157223aebdacSJakub Sitnicki goto out; 157323aebdacSJakub Sitnicki 157423aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 157523aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 157623aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 157723aebdacSJakub Sitnicki if (!inner_iph) 157823aebdacSJakub Sitnicki goto out; 157923aebdacSJakub Sitnicki 158023aebdacSJakub Sitnicki key_iph = inner_iph; 158123aebdacSJakub Sitnicki out: 158223aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 158323aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 158423aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 158523aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 158623aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 158723aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 158823aebdacSJakub Sitnicki } 158923aebdacSJakub Sitnicki 159023aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 159123aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 159223aebdacSJakub Sitnicki { 159323aebdacSJakub Sitnicki struct flow_keys hash_keys; 159423aebdacSJakub Sitnicki 159523aebdacSJakub Sitnicki if (skb) { 159623aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 159723aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 159823aebdacSJakub Sitnicki } 159923aebdacSJakub Sitnicki 160023aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 160123aebdacSJakub Sitnicki } 160223aebdacSJakub Sitnicki 1603c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1604c71099acSThomas Graf { 1605b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1606c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1607adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1608904af04dSJiri Benc struct ip_tunnel_info *tun_info; 16094c9483b2SDavid S. Miller struct flowi6 fl6 = { 1610e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 16114c9483b2SDavid S. Miller .daddr = iph->daddr, 16124c9483b2SDavid S. Miller .saddr = iph->saddr, 16136502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 16144c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 16154c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1616c71099acSThomas Graf }; 1617adaa70bbSThomas Graf 1618904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 161946fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1620904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 162123aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 162223aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 162306e9d040SJiri Benc skb_dst_drop(skb); 162472331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1625c71099acSThomas Graf } 1626c71099acSThomas Graf 16278ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 16284c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1629c71099acSThomas Graf { 16304c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1631c71099acSThomas Graf } 1632c71099acSThomas Graf 16336f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 16346f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1635c71099acSThomas Graf { 1636d46a9d67SDavid Ahern bool any_src; 1637c71099acSThomas Graf 16384c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 16394c1feac5SDavid Ahern struct dst_entry *dst; 16404c1feac5SDavid Ahern 16414c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1642ca254490SDavid Ahern if (dst) 1643ca254490SDavid Ahern return dst; 16444c1feac5SDavid Ahern } 1645ca254490SDavid Ahern 16461fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 16474dc27d1cSDavid McCullough 1648d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1649741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1650d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 165177d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1652c71099acSThomas Graf 1653d46a9d67SDavid Ahern if (!any_src) 1654adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 16550c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 16560c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1657adaa70bbSThomas Graf 16584c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 16591da177e4SLinus Torvalds } 16606f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 16611da177e4SLinus Torvalds 16622774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 166314e50e57SDavid S. Miller { 16645c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 16651dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 166614e50e57SDavid S. Miller struct dst_entry *new = NULL; 166714e50e57SDavid S. Miller 16681dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1669b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 167014e50e57SDavid S. Miller if (rt) { 16710a1f5962SMartin KaFai Lau rt6_info_init(rt); 16720a1f5962SMartin KaFai Lau 1673d8d1f30bSChangli Gao new = &rt->dst; 167414e50e57SDavid S. Miller new->__use = 1; 1675352e512cSHerbert Xu new->input = dst_discard; 1676ede2059dSEric W. Biederman new->output = dst_discard_out; 167714e50e57SDavid S. Miller 1678defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 167914e50e57SDavid S. Miller 16801dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 16814e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 16820a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 168314e50e57SDavid S. Miller rt->rt6i_metric = 0; 168414e50e57SDavid S. Miller 168514e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 168614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 168714e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 168814e50e57SDavid S. Miller #endif 168914e50e57SDavid S. Miller } 169014e50e57SDavid S. Miller 169169ead7afSDavid S. Miller dst_release(dst_orig); 169269ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 169314e50e57SDavid S. Miller } 169414e50e57SDavid S. Miller 16951da177e4SLinus Torvalds /* 16961da177e4SLinus Torvalds * Destination cache support functions 16971da177e4SLinus Torvalds */ 16981da177e4SLinus Torvalds 16994b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 17004b32b5adSMartin KaFai Lau { 17014b32b5adSMartin KaFai Lau if (rt->dst.from && 17024b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 17034b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 17044b32b5adSMartin KaFai Lau } 17054b32b5adSMartin KaFai Lau 17063da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 17073da59bd9SMartin KaFai Lau { 170836143645SSteffen Klassert u32 rt_cookie = 0; 1709c5cff856SWei Wang 1710c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 17113da59bd9SMartin KaFai Lau return NULL; 17123da59bd9SMartin KaFai Lau 17133da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 17143da59bd9SMartin KaFai Lau return NULL; 17153da59bd9SMartin KaFai Lau 17163da59bd9SMartin KaFai Lau return &rt->dst; 17173da59bd9SMartin KaFai Lau } 17183da59bd9SMartin KaFai Lau 17193da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 17203da59bd9SMartin KaFai Lau { 17215973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 17225973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 17233da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 17243da59bd9SMartin KaFai Lau return &rt->dst; 17253da59bd9SMartin KaFai Lau else 17263da59bd9SMartin KaFai Lau return NULL; 17273da59bd9SMartin KaFai Lau } 17283da59bd9SMartin KaFai Lau 17291da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds struct rt6_info *rt; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 17341da177e4SLinus Torvalds 17356f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 17366f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 17376f3118b5SNicolas Dichtel * into this function always. 17386f3118b5SNicolas Dichtel */ 1739e3bc10bdSHannes Frederic Sowa 17404b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 17414b32b5adSMartin KaFai Lau 174202bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1743a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 17443da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 17453da59bd9SMartin KaFai Lau else 17463da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds if (rt) { 175454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 175554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1756e0a1ad73SThomas Graf ip6_del_rt(rt); 175754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 17581da177e4SLinus Torvalds } 175954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 176054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 176154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 176254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 176354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 176454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 17681da177e4SLinus Torvalds { 17691da177e4SLinus Torvalds struct rt6_info *rt; 17701da177e4SLinus Torvalds 17713ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 17721da177e4SLinus Torvalds 1773adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 17741da177e4SLinus Torvalds if (rt) { 17751eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1776ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 17778e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 1778c5cff856SWei Wang } else { 1779c5cff856SWei Wang struct fib6_node *fn; 1780c5cff856SWei Wang 1781c5cff856SWei Wang rcu_read_lock(); 1782c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 1783c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 1784c5cff856SWei Wang fn->fn_sernum = -1; 1785c5cff856SWei Wang rcu_read_unlock(); 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds } 17881eb4f758SHannes Frederic Sowa } 17891da177e4SLinus Torvalds 179045e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 179145e4fd26SMartin KaFai Lau { 179245e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 179345e4fd26SMartin KaFai Lau 179445e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 179545e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 179645e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 179745e4fd26SMartin KaFai Lau } 179845e4fd26SMartin KaFai Lau 17990d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 18000d3f6d29SMartin KaFai Lau { 18010d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 18024e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 18034e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 18040d3f6d29SMartin KaFai Lau } 18050d3f6d29SMartin KaFai Lau 180645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 180745e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 18081da177e4SLinus Torvalds { 18090dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 18101da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 18111da177e4SLinus Torvalds 181245e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 181345e4fd26SMartin KaFai Lau return; 181445e4fd26SMartin KaFai Lau 181519bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 181619bda36cSXin Long return; 181719bda36cSXin Long 181845e4fd26SMartin KaFai Lau if (iph) { 181945e4fd26SMartin KaFai Lau daddr = &iph->daddr; 182045e4fd26SMartin KaFai Lau saddr = &iph->saddr; 182145e4fd26SMartin KaFai Lau } else if (sk) { 182245e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 182345e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 182445e4fd26SMartin KaFai Lau } else { 18250dec879fSJulian Anastasov daddr = NULL; 18260dec879fSJulian Anastasov saddr = NULL; 18271da177e4SLinus Torvalds } 18280dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 18290dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 18300dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 18310dec879fSJulian Anastasov return; 18320dec879fSJulian Anastasov 18330dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 18340dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 18350dec879fSJulian Anastasov } else if (daddr) { 18360dec879fSJulian Anastasov struct rt6_info *nrt6; 18370dec879fSJulian Anastasov 183845e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 183945e4fd26SMartin KaFai Lau if (nrt6) { 184045e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 184145e4fd26SMartin KaFai Lau 184245e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 184345e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 184445e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 184545e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 184645e4fd26SMartin KaFai Lau */ 184745e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 18481cfb71eeSWei Wang /* Release the reference taken in 18491cfb71eeSWei Wang * ip6_rt_cache_alloc() 18501cfb71eeSWei Wang */ 18511cfb71eeSWei Wang dst_release(&nrt6->dst); 185245e4fd26SMartin KaFai Lau } 185345e4fd26SMartin KaFai Lau } 185445e4fd26SMartin KaFai Lau } 185545e4fd26SMartin KaFai Lau 185645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 185745e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 185845e4fd26SMartin KaFai Lau { 185945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 186242ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1863e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 186481aded24SDavid S. Miller { 186581aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 186681aded24SDavid S. Miller struct dst_entry *dst; 186781aded24SDavid S. Miller struct flowi6 fl6; 186881aded24SDavid S. Miller 186981aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 187081aded24SDavid S. Miller fl6.flowi6_oif = oif; 18711b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 187281aded24SDavid S. Miller fl6.daddr = iph->daddr; 187381aded24SDavid S. Miller fl6.saddr = iph->saddr; 18746502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1875e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 187681aded24SDavid S. Miller 187781aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 187881aded24SDavid S. Miller if (!dst->error) 187945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 188081aded24SDavid S. Miller dst_release(dst); 188181aded24SDavid S. Miller } 188281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 188381aded24SDavid S. Miller 188481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 188581aded24SDavid S. Miller { 188633c162a9SMartin KaFai Lau struct dst_entry *dst; 188733c162a9SMartin KaFai Lau 188881aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1889e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 189033c162a9SMartin KaFai Lau 189133c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 189233c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 189333c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 189433c162a9SMartin KaFai Lau return; 189533c162a9SMartin KaFai Lau 189633c162a9SMartin KaFai Lau bh_lock_sock(sk); 189733c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 189833c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 189933c162a9SMartin KaFai Lau bh_unlock_sock(sk); 190081aded24SDavid S. Miller } 190181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 190281aded24SDavid S. Miller 1903b55b76b2SDuan Jiong /* Handle redirects */ 1904b55b76b2SDuan Jiong struct ip6rd_flowi { 1905b55b76b2SDuan Jiong struct flowi6 fl6; 1906b55b76b2SDuan Jiong struct in6_addr gateway; 1907b55b76b2SDuan Jiong }; 1908b55b76b2SDuan Jiong 1909b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1910b55b76b2SDuan Jiong struct fib6_table *table, 1911b55b76b2SDuan Jiong struct flowi6 *fl6, 1912b55b76b2SDuan Jiong int flags) 1913b55b76b2SDuan Jiong { 1914b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1915b55b76b2SDuan Jiong struct rt6_info *rt; 1916b55b76b2SDuan Jiong struct fib6_node *fn; 1917b55b76b2SDuan Jiong 1918b55b76b2SDuan Jiong /* Get the "current" route for this destination and 191967c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1920b55b76b2SDuan Jiong * 1921b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1922b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1923b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1924b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1925b55b76b2SDuan Jiong * routes. 1926b55b76b2SDuan Jiong */ 1927b55b76b2SDuan Jiong 1928b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1929b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1930b55b76b2SDuan Jiong restart: 1931b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1932b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1933b55b76b2SDuan Jiong continue; 1934b55b76b2SDuan Jiong if (rt->dst.error) 1935b55b76b2SDuan Jiong break; 1936b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1937b55b76b2SDuan Jiong continue; 1938b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1939b55b76b2SDuan Jiong continue; 1940b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1941b55b76b2SDuan Jiong continue; 1942b55b76b2SDuan Jiong break; 1943b55b76b2SDuan Jiong } 1944b55b76b2SDuan Jiong 1945b55b76b2SDuan Jiong if (!rt) 1946b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1947b55b76b2SDuan Jiong else if (rt->dst.error) { 1948b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1949b0a1ba59SMartin KaFai Lau goto out; 1950b0a1ba59SMartin KaFai Lau } 1951b0a1ba59SMartin KaFai Lau 1952b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1953a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1954a3c00e46SMartin KaFai Lau if (fn) 1955a3c00e46SMartin KaFai Lau goto restart; 1956b55b76b2SDuan Jiong } 1957a3c00e46SMartin KaFai Lau 1958b0a1ba59SMartin KaFai Lau out: 1959b55b76b2SDuan Jiong dst_hold(&rt->dst); 1960b55b76b2SDuan Jiong 1961b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1962b55b76b2SDuan Jiong 1963b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1964b55b76b2SDuan Jiong return rt; 1965b55b76b2SDuan Jiong }; 1966b55b76b2SDuan Jiong 1967b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1968b55b76b2SDuan Jiong const struct flowi6 *fl6, 1969b55b76b2SDuan Jiong const struct in6_addr *gateway) 1970b55b76b2SDuan Jiong { 1971b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1972b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1973b55b76b2SDuan Jiong 1974b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1975b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1976b55b76b2SDuan Jiong 1977b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1978b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1979b55b76b2SDuan Jiong } 1980b55b76b2SDuan Jiong 1981e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1982e2d118a1SLorenzo Colitti kuid_t uid) 19833a5ad2eeSDavid S. Miller { 19843a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 19853a5ad2eeSDavid S. Miller struct dst_entry *dst; 19863a5ad2eeSDavid S. Miller struct flowi6 fl6; 19873a5ad2eeSDavid S. Miller 19883a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1989e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 19903a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 19913a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 19923a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 19933a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 19946502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1995e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 19963a5ad2eeSDavid S. Miller 1997b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 19986700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 19993a5ad2eeSDavid S. Miller dst_release(dst); 20003a5ad2eeSDavid S. Miller } 20013a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 20023a5ad2eeSDavid S. Miller 2003c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2004c92a59ecSDuan Jiong u32 mark) 2005c92a59ecSDuan Jiong { 2006c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2007c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2008c92a59ecSDuan Jiong struct dst_entry *dst; 2009c92a59ecSDuan Jiong struct flowi6 fl6; 2010c92a59ecSDuan Jiong 2011c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2012e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2013c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2014c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2015c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2016c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2017e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2018c92a59ecSDuan Jiong 2019b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2020c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2021c92a59ecSDuan Jiong dst_release(dst); 2022c92a59ecSDuan Jiong } 2023c92a59ecSDuan Jiong 20243a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 20253a5ad2eeSDavid S. Miller { 2026e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2027e2d118a1SLorenzo Colitti sk->sk_uid); 20283a5ad2eeSDavid S. Miller } 20293a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 20303a5ad2eeSDavid S. Miller 20310dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 20321da177e4SLinus Torvalds { 20330dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 20340dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 20350dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 20360dbaee3bSDavid S. Miller 20371da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 20381da177e4SLinus Torvalds 20395578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 20405578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* 20431da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 20441da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 20451da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 20461da177e4SLinus Torvalds * rely only on pmtu discovery" 20471da177e4SLinus Torvalds */ 20481da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 20491da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 20501da177e4SLinus Torvalds return mtu; 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds 2053ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2054d33e4553SDavid S. Miller { 20554b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 20564b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2057d33e4553SDavid S. Miller struct inet6_dev *idev; 2058618f9bc7SSteffen Klassert 2059618f9bc7SSteffen Klassert if (mtu) 206030f78d8eSEric Dumazet goto out; 2061618f9bc7SSteffen Klassert 20624b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 20634b32b5adSMartin KaFai Lau if (mtu) 20644b32b5adSMartin KaFai Lau goto out; 20654b32b5adSMartin KaFai Lau 2066618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2067d33e4553SDavid S. Miller 2068d33e4553SDavid S. Miller rcu_read_lock(); 2069d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2070d33e4553SDavid S. Miller if (idev) 2071d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2072d33e4553SDavid S. Miller rcu_read_unlock(); 2073d33e4553SDavid S. Miller 207430f78d8eSEric Dumazet out: 207514972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 207614972cbdSRoopa Prabhu 207714972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2078d33e4553SDavid S. Miller } 2079d33e4553SDavid S. Miller 20803b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 208187a11578SDavid S. Miller struct flowi6 *fl6) 20821da177e4SLinus Torvalds { 208387a11578SDavid S. Miller struct dst_entry *dst; 20841da177e4SLinus Torvalds struct rt6_info *rt; 20851da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2086c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 20871da177e4SLinus Torvalds 208838308473SDavid S. Miller if (unlikely(!idev)) 2089122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 20901da177e4SLinus Torvalds 2091ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 209238308473SDavid S. Miller if (unlikely(!rt)) { 20931da177e4SLinus Torvalds in6_dev_put(idev); 209487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 20951da177e4SLinus Torvalds goto out; 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20988e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 20998e2ec639SYan, Zheng rt->dst.output = ip6_output; 2100550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 210187a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 21028e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 21038e2ec639SYan, Zheng rt->rt6i_idev = idev; 210414edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 21051da177e4SLinus Torvalds 2106587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2107587fea74SWei Wang * do proper release of the net_device 2108587fea74SWei Wang */ 2109587fea74SWei Wang rt6_uncached_list_add(rt); 21101da177e4SLinus Torvalds 211187a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 211287a11578SDavid S. Miller 21131da177e4SLinus Torvalds out: 211487a11578SDavid S. Miller return dst; 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 2117569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 21181da177e4SLinus Torvalds { 211986393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 21207019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 21217019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 21227019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 21237019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 21247019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2125fc66f95cSEric Dumazet int entries; 21261da177e4SLinus Torvalds 2127fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 212849a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2129fc66f95cSEric Dumazet entries <= rt_max_size) 21301da177e4SLinus Torvalds goto out; 21311da177e4SLinus Torvalds 21326891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 213314956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2134fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2135fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 21367019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 21371da177e4SLinus Torvalds out: 21387019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2139fc66f95cSEric Dumazet return entries > rt_max_size; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 2142e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2143e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2144e715b6d3SFlorian Westphal { 2145c3a8d947SDaniel Borkmann bool ecn_ca = false; 2146e715b6d3SFlorian Westphal struct nlattr *nla; 2147e715b6d3SFlorian Westphal int remaining; 2148e715b6d3SFlorian Westphal u32 *mp; 2149e715b6d3SFlorian Westphal 215063159f29SIan Morris if (!cfg->fc_mx) 2151e715b6d3SFlorian Westphal return 0; 2152e715b6d3SFlorian Westphal 2153e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2154e715b6d3SFlorian Westphal if (unlikely(!mp)) 2155e715b6d3SFlorian Westphal return -ENOMEM; 2156e715b6d3SFlorian Westphal 2157e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2158e715b6d3SFlorian Westphal int type = nla_type(nla); 2159ea697639SDaniel Borkmann u32 val; 2160ea697639SDaniel Borkmann 21611bb14807SDaniel Borkmann if (!type) 21621bb14807SDaniel Borkmann continue; 2163e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2164e715b6d3SFlorian Westphal goto err; 21651bb14807SDaniel Borkmann 2166ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2167ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2168e715b6d3SFlorian Westphal 2169ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 2170c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 2171ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2172ea697639SDaniel Borkmann goto err; 2173ea697639SDaniel Borkmann } else { 2174ea697639SDaniel Borkmann val = nla_get_u32(nla); 2175ea697639SDaniel Borkmann } 2176626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2177626abd59SPaolo Abeni val = 255; 2178b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2179b8d3e416SDaniel Borkmann goto err; 2180ea697639SDaniel Borkmann 2181ea697639SDaniel Borkmann mp[type - 1] = val; 2182e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2183e715b6d3SFlorian Westphal } 2184e715b6d3SFlorian Westphal 2185c3a8d947SDaniel Borkmann if (ecn_ca) { 2186c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2187c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2188c3a8d947SDaniel Borkmann } 2189e715b6d3SFlorian Westphal 2190c3a8d947SDaniel Borkmann mxc->mx = mp; 2191e715b6d3SFlorian Westphal return 0; 2192e715b6d3SFlorian Westphal err: 2193e715b6d3SFlorian Westphal kfree(mp); 2194e715b6d3SFlorian Westphal return -EINVAL; 2195e715b6d3SFlorian Westphal } 21961da177e4SLinus Torvalds 21978c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 21988c14586fSDavid Ahern struct fib6_config *cfg, 21998c14586fSDavid Ahern const struct in6_addr *gw_addr) 22008c14586fSDavid Ahern { 22018c14586fSDavid Ahern struct flowi6 fl6 = { 22028c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 22038c14586fSDavid Ahern .daddr = *gw_addr, 22048c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 22058c14586fSDavid Ahern }; 22068c14586fSDavid Ahern struct fib6_table *table; 22078c14586fSDavid Ahern struct rt6_info *rt; 2208d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 22098c14586fSDavid Ahern 22108c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 22118c14586fSDavid Ahern if (!table) 22128c14586fSDavid Ahern return NULL; 22138c14586fSDavid Ahern 22148c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 22158c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 22168c14586fSDavid Ahern 22178c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 22188c14586fSDavid Ahern 22198c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 22208c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 22218c14586fSDavid Ahern ip6_rt_put(rt); 22228c14586fSDavid Ahern rt = NULL; 22238c14586fSDavid Ahern } 22248c14586fSDavid Ahern 22258c14586fSDavid Ahern return rt; 22268c14586fSDavid Ahern } 22278c14586fSDavid Ahern 2228333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2229333c4301SDavid Ahern struct netlink_ext_ack *extack) 22301da177e4SLinus Torvalds { 22315578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 22321da177e4SLinus Torvalds struct rt6_info *rt = NULL; 22331da177e4SLinus Torvalds struct net_device *dev = NULL; 22341da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2235c71099acSThomas Graf struct fib6_table *table; 22361da177e4SLinus Torvalds int addr_type; 22378c5b83f0SRoopa Prabhu int err = -EINVAL; 22381da177e4SLinus Torvalds 2239557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2240d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2241d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2242557c44beSDavid Ahern goto out; 2243d5d531cbSDavid Ahern } 2244557c44beSDavid Ahern 2245d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2246d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 22478c5b83f0SRoopa Prabhu goto out; 2248d5d531cbSDavid Ahern } 2249d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2250d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2251d5d531cbSDavid Ahern goto out; 2252d5d531cbSDavid Ahern } 22531da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2254d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2255d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2256d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 22578c5b83f0SRoopa Prabhu goto out; 2258d5d531cbSDavid Ahern } 22591da177e4SLinus Torvalds #endif 226086872cb5SThomas Graf if (cfg->fc_ifindex) { 22611da177e4SLinus Torvalds err = -ENODEV; 22625578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 22631da177e4SLinus Torvalds if (!dev) 22641da177e4SLinus Torvalds goto out; 22651da177e4SLinus Torvalds idev = in6_dev_get(dev); 22661da177e4SLinus Torvalds if (!idev) 22671da177e4SLinus Torvalds goto out; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 227086872cb5SThomas Graf if (cfg->fc_metric == 0) 227186872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 22721da177e4SLinus Torvalds 2273c71099acSThomas Graf err = -ENOBUFS; 227438308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2275d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2276d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 227738308473SDavid S. Miller if (!table) { 2278f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2279d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2280d71314b4SMatti Vaittinen } 2281d71314b4SMatti Vaittinen } else { 2282d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2283d71314b4SMatti Vaittinen } 228438308473SDavid S. Miller 228538308473SDavid S. Miller if (!table) 2286c71099acSThomas Graf goto out; 2287c71099acSThomas Graf 2288ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2289ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 22901da177e4SLinus Torvalds 229138308473SDavid S. Miller if (!rt) { 22921da177e4SLinus Torvalds err = -ENOMEM; 22931da177e4SLinus Torvalds goto out; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 22961716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 22971716a961SGao feng rt6_set_expires(rt, jiffies + 22981716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 22991716a961SGao feng else 23001716a961SGao feng rt6_clean_expires(rt); 23011da177e4SLinus Torvalds 230286872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 230386872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 230486872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 230586872cb5SThomas Graf 230686872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2309d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2310ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2311ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 23121da177e4SLinus Torvalds else 2313d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 23141da177e4SLinus Torvalds 2315d8d1f30bSChangli Gao rt->dst.output = ip6_output; 23161da177e4SLinus Torvalds 231719e42e45SRoopa Prabhu if (cfg->fc_encap) { 231819e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 231919e42e45SRoopa Prabhu 232030357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2321127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 23229ae28727SDavid Ahern &lwtstate, extack); 232319e42e45SRoopa Prabhu if (err) 232419e42e45SRoopa Prabhu goto out; 232561adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 232661adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 232761adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 232861adedf3SJiri Benc rt->dst.output = lwtunnel_output; 232919e42e45SRoopa Prabhu } 233061adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 233161adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 233261adedf3SJiri Benc rt->dst.input = lwtunnel_input; 233325368623STom Herbert } 233425368623STom Herbert } 233519e42e45SRoopa Prabhu 233686872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 233786872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2338afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 233911d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 234286872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 234386872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 23441da177e4SLinus Torvalds #endif 23451da177e4SLinus Torvalds 234686872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 23491da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 23501da177e4SLinus Torvalds */ 235186872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 235238308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 235338308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 235438308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 23551da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 23565578689aSDaniel Lezcano if (dev != net->loopback_dev) { 23571da177e4SLinus Torvalds if (dev) { 23581da177e4SLinus Torvalds dev_put(dev); 23591da177e4SLinus Torvalds in6_dev_put(idev); 23601da177e4SLinus Torvalds } 23615578689aSDaniel Lezcano dev = net->loopback_dev; 23621da177e4SLinus Torvalds dev_hold(dev); 23631da177e4SLinus Torvalds idev = in6_dev_get(dev); 23641da177e4SLinus Torvalds if (!idev) { 23651da177e4SLinus Torvalds err = -ENODEV; 23661da177e4SLinus Torvalds goto out; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2370ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2371ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2372ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2373ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 23747150aedeSKamala R rt->dst.input = dst_discard; 2375ef2c7d7bSNicolas Dichtel break; 2376ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2377ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 23787150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 23797150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2380ef2c7d7bSNicolas Dichtel break; 2381b4949ab2SNicolas Dichtel case RTN_THROW: 23820315e382SNikola Forró case RTN_UNREACHABLE: 2383ef2c7d7bSNicolas Dichtel default: 23847150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 23850315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 23860315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 23877150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 23887150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2389ef2c7d7bSNicolas Dichtel break; 2390ef2c7d7bSNicolas Dichtel } 23911da177e4SLinus Torvalds goto install_route; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 239486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2395b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 23961da177e4SLinus Torvalds int gwa_type; 23971da177e4SLinus Torvalds 239886872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2399330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 240048ed7b26SFlorian Westphal 240148ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 240248ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 240348ed7b26SFlorian Westphal * will return already-added prefix route via interface that 240448ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 240548ed7b26SFlorian Westphal */ 240648ed7b26SFlorian Westphal err = -EINVAL; 2407330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2408330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2409d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2410d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 241148ed7b26SFlorian Westphal goto out; 2412d5d531cbSDavid Ahern } 24134e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 24168c14586fSDavid Ahern struct rt6_info *grt = NULL; 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 24191da177e4SLinus Torvalds addresses as nexthop address. 24201da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 24211da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 24221da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 24231da177e4SLinus Torvalds some exceptions. --ANK 242496d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 242596d5822cSErik Nordmark addressing 24261da177e4SLinus Torvalds */ 242796d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2428d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2429d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2430d5d531cbSDavid Ahern "Invalid gateway address"); 24311da177e4SLinus Torvalds goto out; 2432d5d531cbSDavid Ahern } 24331da177e4SLinus Torvalds 2434a435a07fSVincent Bernat if (cfg->fc_table) { 24358c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 24368c14586fSDavid Ahern 2437a435a07fSVincent Bernat if (grt) { 2438a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2439a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2440a435a07fSVincent Bernat ip6_rt_put(grt); 2441a435a07fSVincent Bernat grt = NULL; 2442a435a07fSVincent Bernat } 2443a435a07fSVincent Bernat } 2444a435a07fSVincent Bernat } 2445a435a07fSVincent Bernat 24468c14586fSDavid Ahern if (!grt) 24478c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 24488c14586fSDavid Ahern cfg->fc_ifindex, 1); 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds err = -EHOSTUNREACH; 245138308473SDavid S. Miller if (!grt) 24521da177e4SLinus Torvalds goto out; 24531da177e4SLinus Torvalds if (dev) { 2454d1918542SDavid S. Miller if (dev != grt->dst.dev) { 245594e187c0SAmerigo Wang ip6_rt_put(grt); 24561da177e4SLinus Torvalds goto out; 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds } else { 2459d1918542SDavid S. Miller dev = grt->dst.dev; 24601da177e4SLinus Torvalds idev = grt->rt6i_idev; 24611da177e4SLinus Torvalds dev_hold(dev); 24621da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 24651da177e4SLinus Torvalds err = 0; 246694e187c0SAmerigo Wang ip6_rt_put(grt); 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds if (err) 24691da177e4SLinus Torvalds goto out; 24701da177e4SLinus Torvalds } 24711da177e4SLinus Torvalds err = -EINVAL; 2472d5d531cbSDavid Ahern if (!dev) { 2473d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 24741da177e4SLinus Torvalds goto out; 2475d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2476d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2477d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2478d5d531cbSDavid Ahern goto out; 2479d5d531cbSDavid Ahern } 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds err = -ENODEV; 248338308473SDavid S. Miller if (!dev) 24841da177e4SLinus Torvalds goto out; 24851da177e4SLinus Torvalds 2486c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2487c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2488d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2489c3968a85SDaniel Walter err = -EINVAL; 2490c3968a85SDaniel Walter goto out; 2491c3968a85SDaniel Walter } 24924e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2493c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2494c3968a85SDaniel Walter } else 2495c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2496c3968a85SDaniel Walter 249786872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds install_route: 2500d8d1f30bSChangli Gao rt->dst.dev = dev; 25011da177e4SLinus Torvalds rt->rt6i_idev = idev; 2502c71099acSThomas Graf rt->rt6i_table = table; 250363152fc0SDaniel Lezcano 2504c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 250563152fc0SDaniel Lezcano 25068c5b83f0SRoopa Prabhu return rt; 25071da177e4SLinus Torvalds out: 25081da177e4SLinus Torvalds if (dev) 25091da177e4SLinus Torvalds dev_put(dev); 25101da177e4SLinus Torvalds if (idev) 25111da177e4SLinus Torvalds in6_dev_put(idev); 2512587fea74SWei Wang if (rt) 2513587fea74SWei Wang dst_release_immediate(&rt->dst); 25146b9ea5a6SRoopa Prabhu 25158c5b83f0SRoopa Prabhu return ERR_PTR(err); 25166b9ea5a6SRoopa Prabhu } 25176b9ea5a6SRoopa Prabhu 2518333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2519333c4301SDavid Ahern struct netlink_ext_ack *extack) 25206b9ea5a6SRoopa Prabhu { 25216b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 25228c5b83f0SRoopa Prabhu struct rt6_info *rt; 25236b9ea5a6SRoopa Prabhu int err; 25246b9ea5a6SRoopa Prabhu 2525333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 25268c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 25278c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 25288c5b83f0SRoopa Prabhu rt = NULL; 25296b9ea5a6SRoopa Prabhu goto out; 25308c5b83f0SRoopa Prabhu } 25316b9ea5a6SRoopa Prabhu 25326b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 25336b9ea5a6SRoopa Prabhu if (err) 25346b9ea5a6SRoopa Prabhu goto out; 25356b9ea5a6SRoopa Prabhu 2536333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 25376b9ea5a6SRoopa Prabhu 25386b9ea5a6SRoopa Prabhu kfree(mxc.mx); 25396b9ea5a6SRoopa Prabhu 25406b9ea5a6SRoopa Prabhu return err; 25416b9ea5a6SRoopa Prabhu out: 2542587fea74SWei Wang if (rt) 2543587fea74SWei Wang dst_release_immediate(&rt->dst); 25446b9ea5a6SRoopa Prabhu 25451da177e4SLinus Torvalds return err; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 254886872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 25491da177e4SLinus Torvalds { 25501da177e4SLinus Torvalds int err; 2551c71099acSThomas Graf struct fib6_table *table; 2552d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 25531da177e4SLinus Torvalds 2554a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 25556825a26cSGao feng err = -ENOENT; 25566825a26cSGao feng goto out; 25576825a26cSGao feng } 25586c813a72SPatrick McHardy 2559c71099acSThomas Graf table = rt->rt6i_table; 2560c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 256186872cb5SThomas Graf err = fib6_del(rt, info); 2562c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 25631da177e4SLinus Torvalds 25646825a26cSGao feng out: 256594e187c0SAmerigo Wang ip6_rt_put(rt); 25661da177e4SLinus Torvalds return err; 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 2569e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2570e0a1ad73SThomas Graf { 25714d1169c1SDenis V. Lunev struct nl_info info = { 2572d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 25734d1169c1SDenis V. Lunev }; 2574528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2575e0a1ad73SThomas Graf } 2576e0a1ad73SThomas Graf 25770ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 25780ae81335SDavid Ahern { 25790ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2580e3330039SWANG Cong struct net *net = info->nl_net; 258116a16cd3SDavid Ahern struct sk_buff *skb = NULL; 25820ae81335SDavid Ahern struct fib6_table *table; 2583e3330039SWANG Cong int err = -ENOENT; 25840ae81335SDavid Ahern 2585e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2586e3330039SWANG Cong goto out_put; 25870ae81335SDavid Ahern table = rt->rt6i_table; 25880ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 25890ae81335SDavid Ahern 25900ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 25910ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 25920ae81335SDavid Ahern 259316a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 259416a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 259516a16cd3SDavid Ahern if (skb) { 259616a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 259716a16cd3SDavid Ahern 2598e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 259916a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 260016a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 260116a16cd3SDavid Ahern kfree_skb(skb); 260216a16cd3SDavid Ahern skb = NULL; 260316a16cd3SDavid Ahern } else 260416a16cd3SDavid Ahern info->skip_notify = 1; 260516a16cd3SDavid Ahern } 260616a16cd3SDavid Ahern 26070ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 26080ae81335SDavid Ahern &rt->rt6i_siblings, 26090ae81335SDavid Ahern rt6i_siblings) { 26100ae81335SDavid Ahern err = fib6_del(sibling, info); 26110ae81335SDavid Ahern if (err) 2612e3330039SWANG Cong goto out_unlock; 26130ae81335SDavid Ahern } 26140ae81335SDavid Ahern } 26150ae81335SDavid Ahern 26160ae81335SDavid Ahern err = fib6_del(rt, info); 2617e3330039SWANG Cong out_unlock: 26180ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2619e3330039SWANG Cong out_put: 26200ae81335SDavid Ahern ip6_rt_put(rt); 262116a16cd3SDavid Ahern 262216a16cd3SDavid Ahern if (skb) { 2623e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 262416a16cd3SDavid Ahern info->nlh, gfp_any()); 262516a16cd3SDavid Ahern } 26260ae81335SDavid Ahern return err; 26270ae81335SDavid Ahern } 26280ae81335SDavid Ahern 2629333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2630333c4301SDavid Ahern struct netlink_ext_ack *extack) 26311da177e4SLinus Torvalds { 2632c71099acSThomas Graf struct fib6_table *table; 26331da177e4SLinus Torvalds struct fib6_node *fn; 26341da177e4SLinus Torvalds struct rt6_info *rt; 26351da177e4SLinus Torvalds int err = -ESRCH; 26361da177e4SLinus Torvalds 26375578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2638d5d531cbSDavid Ahern if (!table) { 2639d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2640c71099acSThomas Graf return err; 2641d5d531cbSDavid Ahern } 26421da177e4SLinus Torvalds 2643c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2644c71099acSThomas Graf 2645c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 264686872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 264786872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds if (fn) { 2650d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 26511f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 26521f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 26531f56a01fSMartin KaFai Lau continue; 265486872cb5SThomas Graf if (cfg->fc_ifindex && 2655d1918542SDavid S. Miller (!rt->dst.dev || 2656d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 26571da177e4SLinus Torvalds continue; 265886872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 265986872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 26601da177e4SLinus Torvalds continue; 266186872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 26621da177e4SLinus Torvalds continue; 2663c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2664c2ed1880SMantas M continue; 2665d8d1f30bSChangli Gao dst_hold(&rt->dst); 2666c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 26671da177e4SLinus Torvalds 26680ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 26690ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 267086872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 26710ae81335SDavid Ahern 26720ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds } 2675c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds return err; 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds 26806700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2681a6279458SYOSHIFUJI Hideaki { 2682a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2683e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2684e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2685e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2686e8599ff4SDavid S. Miller struct neighbour *neigh; 268771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 26886e157b6aSDavid S. Miller int optlen, on_link; 26896e157b6aSDavid S. Miller u8 *lladdr; 2690e8599ff4SDavid S. Miller 269129a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 269271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2693e8599ff4SDavid S. Miller 2694e8599ff4SDavid S. Miller if (optlen < 0) { 26956e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2696e8599ff4SDavid S. Miller return; 2697e8599ff4SDavid S. Miller } 2698e8599ff4SDavid S. Miller 269971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2700e8599ff4SDavid S. Miller 270171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 27026e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2703e8599ff4SDavid S. Miller return; 2704e8599ff4SDavid S. Miller } 2705e8599ff4SDavid S. Miller 27066e157b6aSDavid S. Miller on_link = 0; 270771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2708e8599ff4SDavid S. Miller on_link = 1; 270971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2710e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 27116e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2712e8599ff4SDavid S. Miller return; 2713e8599ff4SDavid S. Miller } 2714e8599ff4SDavid S. Miller 2715e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2716e8599ff4SDavid S. Miller if (!in6_dev) 2717e8599ff4SDavid S. Miller return; 2718e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2719e8599ff4SDavid S. Miller return; 2720e8599ff4SDavid S. Miller 2721e8599ff4SDavid S. Miller /* RFC2461 8.1: 2722e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2723e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2724e8599ff4SDavid S. Miller */ 2725e8599ff4SDavid S. Miller 2726f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2727e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2728e8599ff4SDavid S. Miller return; 2729e8599ff4SDavid S. Miller } 27306e157b6aSDavid S. Miller 27316e157b6aSDavid S. Miller lladdr = NULL; 2732e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2733e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2734e8599ff4SDavid S. Miller skb->dev); 2735e8599ff4SDavid S. Miller if (!lladdr) { 2736e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2737e8599ff4SDavid S. Miller return; 2738e8599ff4SDavid S. Miller } 2739e8599ff4SDavid S. Miller } 2740e8599ff4SDavid S. Miller 27416e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2742ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 27436e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 27446e157b6aSDavid S. Miller return; 27456e157b6aSDavid S. Miller } 27466e157b6aSDavid S. Miller 27476e157b6aSDavid S. Miller /* Redirect received -> path was valid. 27486e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 27496e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 27506e157b6aSDavid S. Miller */ 27510dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 27526e157b6aSDavid S. Miller 275371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2754e8599ff4SDavid S. Miller if (!neigh) 2755e8599ff4SDavid S. Miller return; 2756e8599ff4SDavid S. Miller 27571da177e4SLinus Torvalds /* 27581da177e4SLinus Torvalds * We have finally decided to accept it. 27591da177e4SLinus Torvalds */ 27601da177e4SLinus Torvalds 2761f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 27621da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 27631da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 27641da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2765f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2766f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 27671da177e4SLinus Torvalds 276883a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 276938308473SDavid S. Miller if (!nrt) 27701da177e4SLinus Torvalds goto out; 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 27731da177e4SLinus Torvalds if (on_link) 27741da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 27751da177e4SLinus Torvalds 2776b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 27774e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 27781da177e4SLinus Torvalds 277940e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 27801cfb71eeSWei Wang goto out_release; 27811da177e4SLinus Torvalds 2782d8d1f30bSChangli Gao netevent.old = &rt->dst; 2783d8d1f30bSChangli Gao netevent.new = &nrt->dst; 278471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 278560592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 27868d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 27878d71740cSTom Tucker 27881da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 27896e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2790e0a1ad73SThomas Graf ip6_del_rt(rt); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27931cfb71eeSWei Wang out_release: 27941cfb71eeSWei Wang /* Release the reference taken in 27951cfb71eeSWei Wang * ip6_rt_cache_alloc() 27961cfb71eeSWei Wang */ 27971cfb71eeSWei Wang dst_release(&nrt->dst); 27981cfb71eeSWei Wang 27991da177e4SLinus Torvalds out: 2800e8599ff4SDavid S. Miller neigh_release(neigh); 28016e157b6aSDavid S. Miller } 28026e157b6aSDavid S. Miller 28031da177e4SLinus Torvalds /* 28041da177e4SLinus Torvalds * Misc support functions 28051da177e4SLinus Torvalds */ 28061da177e4SLinus Torvalds 28074b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 28084b32b5adSMartin KaFai Lau { 28094b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 28104b32b5adSMartin KaFai Lau 28114b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 28124b32b5adSMartin KaFai Lau dst_hold(&from->dst); 28134b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 28144b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 28154b32b5adSMartin KaFai Lau } 28164b32b5adSMartin KaFai Lau 281783a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 28181da177e4SLinus Torvalds { 2819d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2820d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 282183a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2822d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 28231da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 28241da177e4SLinus Torvalds if (rt->rt6i_idev) 28251da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2826d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 28274e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 28281716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 28291716a961SGao feng rt6_set_from(rt, ort); 283083a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 28311da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 283283a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 28331da177e4SLinus Torvalds #endif 283483a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2835c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 283661adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 283970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2840efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2841b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2842830218c1SDavid Ahern const struct in6_addr *gwaddr, 2843830218c1SDavid Ahern struct net_device *dev) 284470ceb4f5SYOSHIFUJI Hideaki { 2845830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2846830218c1SDavid Ahern int ifindex = dev->ifindex; 284770ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 284870ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2849c71099acSThomas Graf struct fib6_table *table; 285070ceb4f5SYOSHIFUJI Hideaki 2851830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 285238308473SDavid S. Miller if (!table) 2853c71099acSThomas Graf return NULL; 2854c71099acSThomas Graf 28555744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2856c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 285770ceb4f5SYOSHIFUJI Hideaki if (!fn) 285870ceb4f5SYOSHIFUJI Hideaki goto out; 285970ceb4f5SYOSHIFUJI Hideaki 2860d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2861d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 286270ceb4f5SYOSHIFUJI Hideaki continue; 286370ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 286470ceb4f5SYOSHIFUJI Hideaki continue; 286570ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 286670ceb4f5SYOSHIFUJI Hideaki continue; 2867d8d1f30bSChangli Gao dst_hold(&rt->dst); 286870ceb4f5SYOSHIFUJI Hideaki break; 286970ceb4f5SYOSHIFUJI Hideaki } 287070ceb4f5SYOSHIFUJI Hideaki out: 28715744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 287270ceb4f5SYOSHIFUJI Hideaki return rt; 287370ceb4f5SYOSHIFUJI Hideaki } 287470ceb4f5SYOSHIFUJI Hideaki 2875efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2876b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2877830218c1SDavid Ahern const struct in6_addr *gwaddr, 2878830218c1SDavid Ahern struct net_device *dev, 287995c96174SEric Dumazet unsigned int pref) 288070ceb4f5SYOSHIFUJI Hideaki { 288186872cb5SThomas Graf struct fib6_config cfg = { 2882238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2883830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 288486872cb5SThomas Graf .fc_dst_len = prefixlen, 288586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 288686872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 2887b91d5329SXin Long .fc_protocol = RTPROT_RA, 288815e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2889efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2890efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 289186872cb5SThomas Graf }; 289270ceb4f5SYOSHIFUJI Hideaki 2893830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 28944e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 28954e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 289686872cb5SThomas Graf 2897e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2898e317da96SYOSHIFUJI Hideaki if (!prefixlen) 289986872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 290070ceb4f5SYOSHIFUJI Hideaki 2901333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 290270ceb4f5SYOSHIFUJI Hideaki 2903830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 290470ceb4f5SYOSHIFUJI Hideaki } 290570ceb4f5SYOSHIFUJI Hideaki #endif 290670ceb4f5SYOSHIFUJI Hideaki 2907b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 29081da177e4SLinus Torvalds { 2909830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 29101da177e4SLinus Torvalds struct rt6_info *rt; 2911c71099acSThomas Graf struct fib6_table *table; 29121da177e4SLinus Torvalds 2913830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 291438308473SDavid S. Miller if (!table) 2915c71099acSThomas Graf return NULL; 29161da177e4SLinus Torvalds 29175744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2918d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2919d1918542SDavid S. Miller if (dev == rt->dst.dev && 2920045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 29211da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 29221da177e4SLinus Torvalds break; 29231da177e4SLinus Torvalds } 29241da177e4SLinus Torvalds if (rt) 2925d8d1f30bSChangli Gao dst_hold(&rt->dst); 29265744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 29271da177e4SLinus Torvalds return rt; 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 2930b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2931ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2932ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 29331da177e4SLinus Torvalds { 293486872cb5SThomas Graf struct fib6_config cfg = { 2935ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2936238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 293786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 293886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 293986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 2940b91d5329SXin Long .fc_protocol = RTPROT_RA, 294115e47304SEric W. Biederman .fc_nlinfo.portid = 0, 29425578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2943c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 294486872cb5SThomas Graf }; 29451da177e4SLinus Torvalds 29464e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 29471da177e4SLinus Torvalds 2948333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2949830218c1SDavid Ahern struct fib6_table *table; 2950830218c1SDavid Ahern 2951830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2952830218c1SDavid Ahern if (table) 2953830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2954830218c1SDavid Ahern } 29551da177e4SLinus Torvalds 29561da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 2959830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 29601da177e4SLinus Torvalds { 29611da177e4SLinus Torvalds struct rt6_info *rt; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds restart: 2964c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2965d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 29663e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 29673e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2968d8d1f30bSChangli Gao dst_hold(&rt->dst); 2969c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2970e0a1ad73SThomas Graf ip6_del_rt(rt); 29711da177e4SLinus Torvalds goto restart; 29721da177e4SLinus Torvalds } 29731da177e4SLinus Torvalds } 2974c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2975830218c1SDavid Ahern 2976830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 2977830218c1SDavid Ahern } 2978830218c1SDavid Ahern 2979830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 2980830218c1SDavid Ahern { 2981830218c1SDavid Ahern struct fib6_table *table; 2982830218c1SDavid Ahern struct hlist_head *head; 2983830218c1SDavid Ahern unsigned int h; 2984830218c1SDavid Ahern 2985830218c1SDavid Ahern rcu_read_lock(); 2986830218c1SDavid Ahern 2987830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2988830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 2989830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2990830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 2991830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 2992830218c1SDavid Ahern } 2993830218c1SDavid Ahern } 2994830218c1SDavid Ahern 2995830218c1SDavid Ahern rcu_read_unlock(); 29961da177e4SLinus Torvalds } 29971da177e4SLinus Torvalds 29985578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 29995578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 300086872cb5SThomas Graf struct fib6_config *cfg) 300186872cb5SThomas Graf { 300286872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 300386872cb5SThomas Graf 3004ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3005ca254490SDavid Ahern : RT6_TABLE_MAIN; 300686872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 300786872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 300886872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 300986872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 301086872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 301186872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 301286872cb5SThomas Graf 30135578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3014f1243c2dSBenjamin Thery 30154e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 30164e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 30174e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 301886872cb5SThomas Graf } 301986872cb5SThomas Graf 30205578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 30211da177e4SLinus Torvalds { 302286872cb5SThomas Graf struct fib6_config cfg; 30231da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 30241da177e4SLinus Torvalds int err; 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds switch (cmd) { 30271da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 30281da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3029af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30301da177e4SLinus Torvalds return -EPERM; 30311da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 30321da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 30331da177e4SLinus Torvalds if (err) 30341da177e4SLinus Torvalds return -EFAULT; 30351da177e4SLinus Torvalds 30365578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 303786872cb5SThomas Graf 30381da177e4SLinus Torvalds rtnl_lock(); 30391da177e4SLinus Torvalds switch (cmd) { 30401da177e4SLinus Torvalds case SIOCADDRT: 3041333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 30421da177e4SLinus Torvalds break; 30431da177e4SLinus Torvalds case SIOCDELRT: 3044333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 30451da177e4SLinus Torvalds break; 30461da177e4SLinus Torvalds default: 30471da177e4SLinus Torvalds err = -EINVAL; 30481da177e4SLinus Torvalds } 30491da177e4SLinus Torvalds rtnl_unlock(); 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds return err; 30523ff50b79SStephen Hemminger } 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds return -EINVAL; 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds /* 30581da177e4SLinus Torvalds * Drop the packet on the floor 30591da177e4SLinus Torvalds */ 30601da177e4SLinus Torvalds 3061d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 30621da177e4SLinus Torvalds { 3063612f09e8SYOSHIFUJI Hideaki int type; 3064adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3065612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3066612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 30670660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 306845bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 30693bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 30703bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3071612f09e8SYOSHIFUJI Hideaki break; 3072612f09e8SYOSHIFUJI Hideaki } 3073612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3074612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 30753bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 30763bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3077612f09e8SYOSHIFUJI Hideaki break; 3078612f09e8SYOSHIFUJI Hideaki } 30793ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 30801da177e4SLinus Torvalds kfree_skb(skb); 30811da177e4SLinus Torvalds return 0; 30821da177e4SLinus Torvalds } 30831da177e4SLinus Torvalds 30849ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 30859ce8ade0SThomas Graf { 3086612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 30879ce8ade0SThomas Graf } 30889ce8ade0SThomas Graf 3089ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 30901da177e4SLinus Torvalds { 3091adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3092612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds 30959ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 30969ce8ade0SThomas Graf { 3097612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 30989ce8ade0SThomas Graf } 30999ce8ade0SThomas Graf 3100ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 31019ce8ade0SThomas Graf { 3102adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3103612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 31049ce8ade0SThomas Graf } 31059ce8ade0SThomas Graf 31061da177e4SLinus Torvalds /* 31071da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 31081da177e4SLinus Torvalds */ 31091da177e4SLinus Torvalds 31101da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 31111da177e4SLinus Torvalds const struct in6_addr *addr, 31128f031519SDavid S. Miller bool anycast) 31131da177e4SLinus Torvalds { 3114ca254490SDavid Ahern u32 tb_id; 3115c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 31164832c30dSDavid Ahern struct net_device *dev = idev->dev; 31175f02ce24SDavid Ahern struct rt6_info *rt; 31185f02ce24SDavid Ahern 31195f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3120a3300ef4SHannes Frederic Sowa if (!rt) 31211da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds in6_dev_hold(idev); 31241da177e4SLinus Torvalds 312511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3126d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3127d8d1f30bSChangli Gao rt->dst.output = ip6_output; 31281da177e4SLinus Torvalds rt->rt6i_idev = idev; 31291da177e4SLinus Torvalds 313094b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 31311da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 313258c4fb86SYOSHIFUJI Hideaki if (anycast) 313358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 313458c4fb86SYOSHIFUJI Hideaki else 31351da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 31361da177e4SLinus Torvalds 3137550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 31384e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 31391da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3140ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3141ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds return rt; 31441da177e4SLinus Torvalds } 31451da177e4SLinus Torvalds 3146c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3147c3968a85SDaniel Walter struct arg_dev_net_ip { 3148c3968a85SDaniel Walter struct net_device *dev; 3149c3968a85SDaniel Walter struct net *net; 3150c3968a85SDaniel Walter struct in6_addr *addr; 3151c3968a85SDaniel Walter }; 3152c3968a85SDaniel Walter 3153c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3154c3968a85SDaniel Walter { 3155c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3156c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3157c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3158c3968a85SDaniel Walter 3159d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3160c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3161c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 3162c3968a85SDaniel Walter /* remove prefsrc entry */ 3163c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 3164c3968a85SDaniel Walter } 3165c3968a85SDaniel Walter return 0; 3166c3968a85SDaniel Walter } 3167c3968a85SDaniel Walter 3168c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3169c3968a85SDaniel Walter { 3170c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3171c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3172c3968a85SDaniel Walter .dev = ifp->idev->dev, 3173c3968a85SDaniel Walter .net = net, 3174c3968a85SDaniel Walter .addr = &ifp->addr, 3175c3968a85SDaniel Walter }; 31760c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3177c3968a85SDaniel Walter } 3178c3968a85SDaniel Walter 3179be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3180be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 3181be7a010dSDuan Jiong 3182be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3183be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3184be7a010dSDuan Jiong { 3185be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3186be7a010dSDuan Jiong 3187be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 3188be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 3189be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3190be7a010dSDuan Jiong return -1; 3191be7a010dSDuan Jiong } 3192be7a010dSDuan Jiong return 0; 3193be7a010dSDuan Jiong } 3194be7a010dSDuan Jiong 3195be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3196be7a010dSDuan Jiong { 3197be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3198be7a010dSDuan Jiong } 3199be7a010dSDuan Jiong 32008ed67789SDaniel Lezcano struct arg_dev_net { 32018ed67789SDaniel Lezcano struct net_device *dev; 32028ed67789SDaniel Lezcano struct net *net; 32038ed67789SDaniel Lezcano }; 32048ed67789SDaniel Lezcano 3205a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 32061da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 32071da177e4SLinus Torvalds { 3208bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3209bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 32108ed67789SDaniel Lezcano 3211d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3212a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3213a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 32148397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3215a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 32161da177e4SLinus Torvalds return -1; 3217c159d30cSDavid S. Miller 32181da177e4SLinus Torvalds return 0; 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds 3221f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 32221da177e4SLinus Torvalds { 32238ed67789SDaniel Lezcano struct arg_dev_net adn = { 32248ed67789SDaniel Lezcano .dev = dev, 32258ed67789SDaniel Lezcano .net = net, 32268ed67789SDaniel Lezcano }; 32278ed67789SDaniel Lezcano 32280c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3229e332bc67SEric W. Biederman if (dev) 32308d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 32311da177e4SLinus Torvalds } 32321da177e4SLinus Torvalds 323395c96174SEric Dumazet struct rt6_mtu_change_arg { 32341da177e4SLinus Torvalds struct net_device *dev; 323595c96174SEric Dumazet unsigned int mtu; 32361da177e4SLinus Torvalds }; 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 32391da177e4SLinus Torvalds { 32401da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 32411da177e4SLinus Torvalds struct inet6_dev *idev; 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 32441da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 32451da177e4SLinus Torvalds We still use this lock to block changes 32461da177e4SLinus Torvalds caused by addrconf/ndisc. 32471da177e4SLinus Torvalds */ 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 325038308473SDavid S. Miller if (!idev) 32511da177e4SLinus Torvalds return 0; 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 32541da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 32551da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 32561da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 32571da177e4SLinus Torvalds */ 32581da177e4SLinus Torvalds /* 32591da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 32601da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 32611da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 32621da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 32631da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 32641da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 326567c408cfSAlexander Alemayhu PMTU discovery. 32661da177e4SLinus Torvalds */ 3267d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3268fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 32694b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 32704b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 32714b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 32724b32b5adSMartin KaFai Lau * (i.e. a redirected route), 32734b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 32744b32b5adSMartin KaFai Lau * been updated. 32754b32b5adSMartin KaFai Lau */ 32764b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 32774b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 32784b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 3279d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 32804b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3281defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3282566cfd8fSSimon Arlott } 32834b32b5adSMartin KaFai Lau } 32841da177e4SLinus Torvalds return 0; 32851da177e4SLinus Torvalds } 32861da177e4SLinus Torvalds 328795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 32881da177e4SLinus Torvalds { 3289c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3290c71099acSThomas Graf .dev = dev, 3291c71099acSThomas Graf .mtu = mtu, 3292c71099acSThomas Graf }; 32931da177e4SLinus Torvalds 32940c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds 3297ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 32985176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 329986872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3300ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 330186872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 330286872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 330351ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3304c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 330519e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 330619e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 330732bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3308622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 33093b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 331086872cb5SThomas Graf }; 331186872cb5SThomas Graf 331286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3313333c4301SDavid Ahern struct fib6_config *cfg, 3314333c4301SDavid Ahern struct netlink_ext_ack *extack) 33151da177e4SLinus Torvalds { 331686872cb5SThomas Graf struct rtmsg *rtm; 331786872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3318c78ba6d6SLubomir Rintel unsigned int pref; 331986872cb5SThomas Graf int err; 33201da177e4SLinus Torvalds 3321fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3322fceb6435SJohannes Berg NULL); 332386872cb5SThomas Graf if (err < 0) 332486872cb5SThomas Graf goto errout; 33251da177e4SLinus Torvalds 332686872cb5SThomas Graf err = -EINVAL; 332786872cb5SThomas Graf rtm = nlmsg_data(nlh); 332886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 332986872cb5SThomas Graf 333086872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 333186872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 333286872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 333386872cb5SThomas Graf cfg->fc_flags = RTF_UP; 333486872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3335ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 333686872cb5SThomas Graf 3337ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3338ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3339b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3340b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 334186872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 334286872cb5SThomas Graf 3343ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3344ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3345ab79ad14SMaciej Żenczykowski 33461f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 33471f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 33481f56a01fSMartin KaFai Lau 334915e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 335086872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 33513b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 335286872cb5SThomas Graf 335386872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 335467b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 335586872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 33561da177e4SLinus Torvalds } 335786872cb5SThomas Graf 335886872cb5SThomas Graf if (tb[RTA_DST]) { 335986872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 336086872cb5SThomas Graf 336186872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 336286872cb5SThomas Graf goto errout; 336386872cb5SThomas Graf 336486872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 33651da177e4SLinus Torvalds } 336686872cb5SThomas Graf 336786872cb5SThomas Graf if (tb[RTA_SRC]) { 336886872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 336986872cb5SThomas Graf 337086872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 337186872cb5SThomas Graf goto errout; 337286872cb5SThomas Graf 337386872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 33741da177e4SLinus Torvalds } 337586872cb5SThomas Graf 3376c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 337767b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3378c3968a85SDaniel Walter 337986872cb5SThomas Graf if (tb[RTA_OIF]) 338086872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 338186872cb5SThomas Graf 338286872cb5SThomas Graf if (tb[RTA_PRIORITY]) 338386872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 338486872cb5SThomas Graf 338586872cb5SThomas Graf if (tb[RTA_METRICS]) { 338686872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 338786872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 33881da177e4SLinus Torvalds } 338986872cb5SThomas Graf 339086872cb5SThomas Graf if (tb[RTA_TABLE]) 339186872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 339286872cb5SThomas Graf 339351ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 339451ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 339551ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 33969ed59592SDavid Ahern 33979ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3398c255bd68SDavid Ahern cfg->fc_mp_len, extack); 33999ed59592SDavid Ahern if (err < 0) 34009ed59592SDavid Ahern goto errout; 340151ebd318SNicolas Dichtel } 340251ebd318SNicolas Dichtel 3403c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3404c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3405c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3406c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3407c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3408c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3409c78ba6d6SLubomir Rintel } 3410c78ba6d6SLubomir Rintel 341119e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 341219e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 341319e42e45SRoopa Prabhu 34149ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 341519e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 341619e42e45SRoopa Prabhu 3417c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 34189ed59592SDavid Ahern if (err < 0) 34199ed59592SDavid Ahern goto errout; 34209ed59592SDavid Ahern } 34219ed59592SDavid Ahern 342232bc201eSXin Long if (tb[RTA_EXPIRES]) { 342332bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 342432bc201eSXin Long 342532bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 342632bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 342732bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 342832bc201eSXin Long } 342932bc201eSXin Long } 343032bc201eSXin Long 343186872cb5SThomas Graf err = 0; 343286872cb5SThomas Graf errout: 343386872cb5SThomas Graf return err; 34341da177e4SLinus Torvalds } 34351da177e4SLinus Torvalds 34366b9ea5a6SRoopa Prabhu struct rt6_nh { 34376b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 34386b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 34396b9ea5a6SRoopa Prabhu struct mx6_config mxc; 34406b9ea5a6SRoopa Prabhu struct list_head next; 34416b9ea5a6SRoopa Prabhu }; 34426b9ea5a6SRoopa Prabhu 34436b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 34446b9ea5a6SRoopa Prabhu { 34456b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 34466b9ea5a6SRoopa Prabhu 34476b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 34487d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 34496b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 34506b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 34516b9ea5a6SRoopa Prabhu } 34526b9ea5a6SRoopa Prabhu } 34536b9ea5a6SRoopa Prabhu 34546b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 34556b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 34566b9ea5a6SRoopa Prabhu { 34576b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 34586b9ea5a6SRoopa Prabhu int err = -EEXIST; 34596b9ea5a6SRoopa Prabhu 34606b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 34616b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3462f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 34636b9ea5a6SRoopa Prabhu return err; 34646b9ea5a6SRoopa Prabhu } 34656b9ea5a6SRoopa Prabhu 34666b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 34676b9ea5a6SRoopa Prabhu if (!nh) 34686b9ea5a6SRoopa Prabhu return -ENOMEM; 34696b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 34706b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 34716b9ea5a6SRoopa Prabhu if (err) { 34726b9ea5a6SRoopa Prabhu kfree(nh); 34736b9ea5a6SRoopa Prabhu return err; 34746b9ea5a6SRoopa Prabhu } 34756b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 34766b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 34776b9ea5a6SRoopa Prabhu 34786b9ea5a6SRoopa Prabhu return 0; 34796b9ea5a6SRoopa Prabhu } 34806b9ea5a6SRoopa Prabhu 34813b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 34823b1137feSDavid Ahern struct rt6_info *rt_last, 34833b1137feSDavid Ahern struct nl_info *info, 34843b1137feSDavid Ahern __u16 nlflags) 34853b1137feSDavid Ahern { 34863b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 34873b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 34883b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 34893b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 34903b1137feSDavid Ahern * the list, find the first sibling of the last route appended 34913b1137feSDavid Ahern */ 34923b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 34933b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 34943b1137feSDavid Ahern struct rt6_info, 34953b1137feSDavid Ahern rt6i_siblings); 34963b1137feSDavid Ahern } 34973b1137feSDavid Ahern 34983b1137feSDavid Ahern if (rt) 34993b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 35003b1137feSDavid Ahern } 35013b1137feSDavid Ahern 3502333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3503333c4301SDavid Ahern struct netlink_ext_ack *extack) 350451ebd318SNicolas Dichtel { 35053b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 35063b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 350751ebd318SNicolas Dichtel struct fib6_config r_cfg; 350851ebd318SNicolas Dichtel struct rtnexthop *rtnh; 35096b9ea5a6SRoopa Prabhu struct rt6_info *rt; 35106b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 35116b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 35123b1137feSDavid Ahern __u16 nlflags; 351351ebd318SNicolas Dichtel int remaining; 351451ebd318SNicolas Dichtel int attrlen; 35156b9ea5a6SRoopa Prabhu int err = 1; 35166b9ea5a6SRoopa Prabhu int nhn = 0; 35176b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 35186b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 35196b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 352051ebd318SNicolas Dichtel 35213b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 35223b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 35233b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 35243b1137feSDavid Ahern 352535f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 352651ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 352751ebd318SNicolas Dichtel 35286b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 35296b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 35306b9ea5a6SRoopa Prabhu */ 353151ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 353251ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 353351ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 353451ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 353551ebd318SNicolas Dichtel 353651ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 353751ebd318SNicolas Dichtel if (attrlen > 0) { 353851ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 353951ebd318SNicolas Dichtel 354051ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 354151ebd318SNicolas Dichtel if (nla) { 354267b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 354351ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 354451ebd318SNicolas Dichtel } 354519e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 354619e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 354719e42e45SRoopa Prabhu if (nla) 354819e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 354951ebd318SNicolas Dichtel } 35506b9ea5a6SRoopa Prabhu 3551333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 35528c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 35538c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 35548c5b83f0SRoopa Prabhu rt = NULL; 35556b9ea5a6SRoopa Prabhu goto cleanup; 35568c5b83f0SRoopa Prabhu } 35576b9ea5a6SRoopa Prabhu 35586b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 355951ebd318SNicolas Dichtel if (err) { 3560587fea74SWei Wang dst_release_immediate(&rt->dst); 35616b9ea5a6SRoopa Prabhu goto cleanup; 356251ebd318SNicolas Dichtel } 35636b9ea5a6SRoopa Prabhu 35646b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 356551ebd318SNicolas Dichtel } 35666b9ea5a6SRoopa Prabhu 35673b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 35683b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 35693b1137feSDavid Ahern * the full route when done 35703b1137feSDavid Ahern */ 35713b1137feSDavid Ahern info->skip_notify = 1; 35723b1137feSDavid Ahern 35736b9ea5a6SRoopa Prabhu err_nh = NULL; 35746b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 35753b1137feSDavid Ahern rt_last = nh->rt6_info; 3576333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 35773b1137feSDavid Ahern /* save reference to first route for notification */ 35783b1137feSDavid Ahern if (!rt_notif && !err) 35793b1137feSDavid Ahern rt_notif = nh->rt6_info; 35803b1137feSDavid Ahern 35816b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 35826b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 35836b9ea5a6SRoopa Prabhu if (err) { 35846b9ea5a6SRoopa Prabhu if (replace && nhn) 35856b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 35866b9ea5a6SRoopa Prabhu err_nh = nh; 35876b9ea5a6SRoopa Prabhu goto add_errout; 35886b9ea5a6SRoopa Prabhu } 35896b9ea5a6SRoopa Prabhu 35901a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 359127596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 359227596472SMichal Kubeček * we have already failed to add the first nexthop: 359327596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 359427596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 359527596472SMichal Kubeček * be added to it. 35961a72418bSNicolas Dichtel */ 359727596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 359827596472SMichal Kubeček NLM_F_REPLACE); 35996b9ea5a6SRoopa Prabhu nhn++; 36006b9ea5a6SRoopa Prabhu } 36016b9ea5a6SRoopa Prabhu 36023b1137feSDavid Ahern /* success ... tell user about new route */ 36033b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 36046b9ea5a6SRoopa Prabhu goto cleanup; 36056b9ea5a6SRoopa Prabhu 36066b9ea5a6SRoopa Prabhu add_errout: 36073b1137feSDavid Ahern /* send notification for routes that were added so that 36083b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 36093b1137feSDavid Ahern * coherent 36103b1137feSDavid Ahern */ 36113b1137feSDavid Ahern if (rt_notif) 36123b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 36133b1137feSDavid Ahern 36146b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 36156b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 36166b9ea5a6SRoopa Prabhu if (err_nh == nh) 36176b9ea5a6SRoopa Prabhu break; 3618333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 36196b9ea5a6SRoopa Prabhu } 36206b9ea5a6SRoopa Prabhu 36216b9ea5a6SRoopa Prabhu cleanup: 36226b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3623587fea74SWei Wang if (nh->rt6_info) 3624587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 36256b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 36266b9ea5a6SRoopa Prabhu list_del(&nh->next); 36276b9ea5a6SRoopa Prabhu kfree(nh); 36286b9ea5a6SRoopa Prabhu } 36296b9ea5a6SRoopa Prabhu 36306b9ea5a6SRoopa Prabhu return err; 36316b9ea5a6SRoopa Prabhu } 36326b9ea5a6SRoopa Prabhu 3633333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3634333c4301SDavid Ahern struct netlink_ext_ack *extack) 36356b9ea5a6SRoopa Prabhu { 36366b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36376b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 36386b9ea5a6SRoopa Prabhu int remaining; 36396b9ea5a6SRoopa Prabhu int attrlen; 36406b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 36416b9ea5a6SRoopa Prabhu 36426b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 36436b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 36446b9ea5a6SRoopa Prabhu 36456b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 36466b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 36476b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 36486b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 36496b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 36506b9ea5a6SRoopa Prabhu 36516b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 36526b9ea5a6SRoopa Prabhu if (attrlen > 0) { 36536b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 36546b9ea5a6SRoopa Prabhu 36556b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 36566b9ea5a6SRoopa Prabhu if (nla) { 36576b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 36586b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 36596b9ea5a6SRoopa Prabhu } 36606b9ea5a6SRoopa Prabhu } 3661333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 36626b9ea5a6SRoopa Prabhu if (err) 36636b9ea5a6SRoopa Prabhu last_err = err; 36646b9ea5a6SRoopa Prabhu 366551ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 366651ebd318SNicolas Dichtel } 366751ebd318SNicolas Dichtel 366851ebd318SNicolas Dichtel return last_err; 366951ebd318SNicolas Dichtel } 367051ebd318SNicolas Dichtel 3671c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3672c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 36731da177e4SLinus Torvalds { 367486872cb5SThomas Graf struct fib6_config cfg; 367586872cb5SThomas Graf int err; 36761da177e4SLinus Torvalds 3677333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 367886872cb5SThomas Graf if (err < 0) 367986872cb5SThomas Graf return err; 368086872cb5SThomas Graf 368151ebd318SNicolas Dichtel if (cfg.fc_mp) 3682333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 36830ae81335SDavid Ahern else { 36840ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3685333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 36861da177e4SLinus Torvalds } 36870ae81335SDavid Ahern } 36881da177e4SLinus Torvalds 3689c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3690c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 36911da177e4SLinus Torvalds { 369286872cb5SThomas Graf struct fib6_config cfg; 369386872cb5SThomas Graf int err; 36941da177e4SLinus Torvalds 3695333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 369686872cb5SThomas Graf if (err < 0) 369786872cb5SThomas Graf return err; 369886872cb5SThomas Graf 369951ebd318SNicolas Dichtel if (cfg.fc_mp) 3700333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 370151ebd318SNicolas Dichtel else 3702333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 37031da177e4SLinus Torvalds } 37041da177e4SLinus Torvalds 3705beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3706339bf98fSThomas Graf { 3707beb1afacSDavid Ahern int nexthop_len = 0; 3708beb1afacSDavid Ahern 3709beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3710beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3711beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3712beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3713beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3714beb1afacSDavid Ahern 3715beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3716beb1afacSDavid Ahern } 3717beb1afacSDavid Ahern 3718339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3719339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3720339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3721339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3722339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3723339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3724339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3725339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3726339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 37276a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3728ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3729c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 373019e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3731beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3732beb1afacSDavid Ahern + nexthop_len; 3733beb1afacSDavid Ahern } 3734beb1afacSDavid Ahern 3735beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 37365be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3737beb1afacSDavid Ahern { 3738beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3739beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3740beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3741beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3742beb1afacSDavid Ahern } 3743beb1afacSDavid Ahern 3744beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3745beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3746beb1afacSDavid Ahern goto nla_put_failure; 3747beb1afacSDavid Ahern } 3748beb1afacSDavid Ahern 3749fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 375061e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 375161e4d01eSIdo Schimmel 37525be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 37535be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3754beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3755beb1afacSDavid Ahern goto nla_put_failure; 3756beb1afacSDavid Ahern 3757beb1afacSDavid Ahern if (rt->dst.lwtstate && 3758beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3759beb1afacSDavid Ahern goto nla_put_failure; 3760beb1afacSDavid Ahern 3761beb1afacSDavid Ahern return 0; 3762beb1afacSDavid Ahern 3763beb1afacSDavid Ahern nla_put_failure: 3764beb1afacSDavid Ahern return -EMSGSIZE; 3765beb1afacSDavid Ahern } 3766beb1afacSDavid Ahern 37675be083ceSDavid Ahern /* add multipath next hop */ 3768beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3769beb1afacSDavid Ahern { 3770beb1afacSDavid Ahern struct rtnexthop *rtnh; 3771beb1afacSDavid Ahern unsigned int flags = 0; 3772beb1afacSDavid Ahern 3773beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3774beb1afacSDavid Ahern if (!rtnh) 3775beb1afacSDavid Ahern goto nla_put_failure; 3776beb1afacSDavid Ahern 3777beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3778beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3779beb1afacSDavid Ahern 37805be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3781beb1afacSDavid Ahern goto nla_put_failure; 3782beb1afacSDavid Ahern 3783beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3784beb1afacSDavid Ahern 3785beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3786beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3787beb1afacSDavid Ahern 3788beb1afacSDavid Ahern return 0; 3789beb1afacSDavid Ahern 3790beb1afacSDavid Ahern nla_put_failure: 3791beb1afacSDavid Ahern return -EMSGSIZE; 3792339bf98fSThomas Graf } 3793339bf98fSThomas Graf 3794191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3795191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 37960d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 379715e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3798f8cfe2ceSDavid Ahern unsigned int flags) 37991da177e4SLinus Torvalds { 38004b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 38011da177e4SLinus Torvalds struct rtmsg *rtm; 38021da177e4SLinus Torvalds struct nlmsghdr *nlh; 3803e3703b3dSThomas Graf long expires; 38049e762a4aSPatrick McHardy u32 table; 38051da177e4SLinus Torvalds 380615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 380738308473SDavid S. Miller if (!nlh) 380826932566SPatrick McHardy return -EMSGSIZE; 38092d7202bfSThomas Graf 38102d7202bfSThomas Graf rtm = nlmsg_data(nlh); 38111da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 38121da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 38131da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 38141da177e4SLinus Torvalds rtm->rtm_tos = 0; 3815c71099acSThomas Graf if (rt->rt6i_table) 38169e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3817c71099acSThomas Graf else 38189e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 38199e762a4aSPatrick McHardy rtm->rtm_table = table; 3820c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3821c78679e8SDavid S. Miller goto nla_put_failure; 3822ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3823ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3824ef2c7d7bSNicolas Dichtel case -EINVAL: 3825ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3826ef2c7d7bSNicolas Dichtel break; 3827ef2c7d7bSNicolas Dichtel case -EACCES: 3828ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3829ef2c7d7bSNicolas Dichtel break; 3830b4949ab2SNicolas Dichtel case -EAGAIN: 3831b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3832b4949ab2SNicolas Dichtel break; 3833ef2c7d7bSNicolas Dichtel default: 38341da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3835ef2c7d7bSNicolas Dichtel break; 3836ef2c7d7bSNicolas Dichtel } 3837ef2c7d7bSNicolas Dichtel } 3838ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3839ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 38404ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 38414ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3842d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 38431da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 38441da177e4SLinus Torvalds else 38451da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 38461da177e4SLinus Torvalds rtm->rtm_flags = 0; 38471da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 38481da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 38491da177e4SLinus Torvalds 38501da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 38511da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 38521da177e4SLinus Torvalds 38531da177e4SLinus Torvalds if (dst) { 3854930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3855c78679e8SDavid S. Miller goto nla_put_failure; 38561da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 38571da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3858930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3859c78679e8SDavid S. Miller goto nla_put_failure; 38601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 38611da177e4SLinus Torvalds if (src) { 3862930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3863c78679e8SDavid S. Miller goto nla_put_failure; 38641da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3865c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3866930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3867c78679e8SDavid S. Miller goto nla_put_failure; 38681da177e4SLinus Torvalds #endif 38697bc570c8SYOSHIFUJI Hideaki if (iif) { 38707bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 38717bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3872fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 38732cf75070SNikolay Aleksandrov 38747bc570c8SYOSHIFUJI Hideaki if (err == 0) 38757bc570c8SYOSHIFUJI Hideaki return 0; 3876fd61c6baSDavid Ahern if (err < 0) 38777bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 38787bc570c8SYOSHIFUJI Hideaki } else 38797bc570c8SYOSHIFUJI Hideaki #endif 3880c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3881c78679e8SDavid S. Miller goto nla_put_failure; 38827bc570c8SYOSHIFUJI Hideaki } else if (dst) { 38831da177e4SLinus Torvalds struct in6_addr saddr_buf; 3884c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3885930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3886c78679e8SDavid S. Miller goto nla_put_failure; 3887c3968a85SDaniel Walter } 3888c3968a85SDaniel Walter 3889c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3890c3968a85SDaniel Walter struct in6_addr saddr_buf; 38914e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3892930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3893c78679e8SDavid S. Miller goto nla_put_failure; 38941da177e4SLinus Torvalds } 38952d7202bfSThomas Graf 38964b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 38974b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 38984b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 38994b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 39002d7202bfSThomas Graf goto nla_put_failure; 39012d7202bfSThomas Graf 3902beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3903beb1afacSDavid Ahern goto nla_put_failure; 3904beb1afacSDavid Ahern 3905beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3906beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3907beb1afacSDavid Ahern */ 3908beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3909beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3910beb1afacSDavid Ahern struct nlattr *mp; 3911beb1afacSDavid Ahern 3912beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3913beb1afacSDavid Ahern if (!mp) 3914beb1afacSDavid Ahern goto nla_put_failure; 3915beb1afacSDavid Ahern 3916beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3917beb1afacSDavid Ahern goto nla_put_failure; 3918beb1afacSDavid Ahern 3919beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3920beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3921beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 392294f826b8SEric Dumazet goto nla_put_failure; 392394f826b8SEric Dumazet } 39242d7202bfSThomas Graf 3925beb1afacSDavid Ahern nla_nest_end(skb, mp); 3926beb1afacSDavid Ahern } else { 39275be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3928c78679e8SDavid S. Miller goto nla_put_failure; 3929beb1afacSDavid Ahern } 39308253947eSLi Wei 39318253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 393269cdf8f9SYOSHIFUJI Hideaki 393387a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3934e3703b3dSThomas Graf goto nla_put_failure; 39351da177e4SLinus Torvalds 3936c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3937c78ba6d6SLubomir Rintel goto nla_put_failure; 3938c78ba6d6SLubomir Rintel 393919e42e45SRoopa Prabhu 3940053c095aSJohannes Berg nlmsg_end(skb, nlh); 3941053c095aSJohannes Berg return 0; 39422d7202bfSThomas Graf 39432d7202bfSThomas Graf nla_put_failure: 394426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 394526932566SPatrick McHardy return -EMSGSIZE; 39461da177e4SLinus Torvalds } 39471da177e4SLinus Torvalds 39481b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 39491da177e4SLinus Torvalds { 39501da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 39511f17e2f2SDavid Ahern struct net *net = arg->net; 39521f17e2f2SDavid Ahern 39531f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 39541f17e2f2SDavid Ahern return 0; 39551da177e4SLinus Torvalds 39562d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 39572d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3958f8cfe2ceSDavid Ahern 3959f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3960f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3961f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3962f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3963f8cfe2ceSDavid Ahern return 1; 3964f8cfe2ceSDavid Ahern } 3965f8cfe2ceSDavid Ahern } 39661da177e4SLinus Torvalds 39671f17e2f2SDavid Ahern return rt6_fill_node(net, 3968191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 396915e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3970f8cfe2ceSDavid Ahern NLM_F_MULTI); 39711da177e4SLinus Torvalds } 39721da177e4SLinus Torvalds 3973c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3974c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39751da177e4SLinus Torvalds { 39763b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3977ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 397818c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 397918c3a61cSRoopa Prabhu struct dst_entry *dst; 39801da177e4SLinus Torvalds struct rt6_info *rt; 3981ab364a6fSThomas Graf struct sk_buff *skb; 3982ab364a6fSThomas Graf struct rtmsg *rtm; 39834c9483b2SDavid S. Miller struct flowi6 fl6; 398418c3a61cSRoopa Prabhu bool fibmatch; 3985ab364a6fSThomas Graf 3986fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3987c21ef3e3SDavid Ahern extack); 3988ab364a6fSThomas Graf if (err < 0) 3989ab364a6fSThomas Graf goto errout; 3990ab364a6fSThomas Graf 3991ab364a6fSThomas Graf err = -EINVAL; 39924c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 399338b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 399438b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 399518c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 3996ab364a6fSThomas Graf 3997ab364a6fSThomas Graf if (tb[RTA_SRC]) { 3998ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 3999ab364a6fSThomas Graf goto errout; 4000ab364a6fSThomas Graf 40014e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4002ab364a6fSThomas Graf } 4003ab364a6fSThomas Graf 4004ab364a6fSThomas Graf if (tb[RTA_DST]) { 4005ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4006ab364a6fSThomas Graf goto errout; 4007ab364a6fSThomas Graf 40084e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4009ab364a6fSThomas Graf } 4010ab364a6fSThomas Graf 4011ab364a6fSThomas Graf if (tb[RTA_IIF]) 4012ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4013ab364a6fSThomas Graf 4014ab364a6fSThomas Graf if (tb[RTA_OIF]) 401572331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4016ab364a6fSThomas Graf 40172e47b291SLorenzo Colitti if (tb[RTA_MARK]) 40182e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 40192e47b291SLorenzo Colitti 4020622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4021622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4022622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4023622ec2c9SLorenzo Colitti else 4024622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4025622ec2c9SLorenzo Colitti 4026ab364a6fSThomas Graf if (iif) { 4027ab364a6fSThomas Graf struct net_device *dev; 402872331bc0SShmulik Ladkani int flags = 0; 402972331bc0SShmulik Ladkani 4030121622dbSFlorian Westphal rcu_read_lock(); 4031121622dbSFlorian Westphal 4032121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4033ab364a6fSThomas Graf if (!dev) { 4034121622dbSFlorian Westphal rcu_read_unlock(); 4035ab364a6fSThomas Graf err = -ENODEV; 4036ab364a6fSThomas Graf goto errout; 4037ab364a6fSThomas Graf } 403872331bc0SShmulik Ladkani 403972331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 404072331bc0SShmulik Ladkani 404172331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 404272331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 404372331bc0SShmulik Ladkani 404418c3a61cSRoopa Prabhu if (!fibmatch) 404518c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4046401481e0SArnd Bergmann else 4047401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4048121622dbSFlorian Westphal 4049121622dbSFlorian Westphal rcu_read_unlock(); 405072331bc0SShmulik Ladkani } else { 405172331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 405272331bc0SShmulik Ladkani 405318c3a61cSRoopa Prabhu if (!fibmatch) 405418c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4055401481e0SArnd Bergmann else 4056401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 405718c3a61cSRoopa Prabhu } 405818c3a61cSRoopa Prabhu 405918c3a61cSRoopa Prabhu 406018c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 406118c3a61cSRoopa Prabhu if (rt->dst.error) { 406218c3a61cSRoopa Prabhu err = rt->dst.error; 406318c3a61cSRoopa Prabhu ip6_rt_put(rt); 406418c3a61cSRoopa Prabhu goto errout; 4065ab364a6fSThomas Graf } 40661da177e4SLinus Torvalds 40679d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 40689d6acb3bSWANG Cong err = rt->dst.error; 40699d6acb3bSWANG Cong ip6_rt_put(rt); 40709d6acb3bSWANG Cong goto errout; 40719d6acb3bSWANG Cong } 40729d6acb3bSWANG Cong 40731da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 407438308473SDavid S. Miller if (!skb) { 407594e187c0SAmerigo Wang ip6_rt_put(rt); 4076ab364a6fSThomas Graf err = -ENOBUFS; 4077ab364a6fSThomas Graf goto errout; 4078ab364a6fSThomas Graf } 40791da177e4SLinus Torvalds 4080d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 408118c3a61cSRoopa Prabhu if (fibmatch) 408218c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 408318c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 408418c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 408518c3a61cSRoopa Prabhu else 40864c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 408715e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4088f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 40891da177e4SLinus Torvalds if (err < 0) { 4090ab364a6fSThomas Graf kfree_skb(skb); 4091ab364a6fSThomas Graf goto errout; 40921da177e4SLinus Torvalds } 40931da177e4SLinus Torvalds 409415e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4095ab364a6fSThomas Graf errout: 40961da177e4SLinus Torvalds return err; 40971da177e4SLinus Torvalds } 40981da177e4SLinus Torvalds 409937a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 410037a1d361SRoopa Prabhu unsigned int nlm_flags) 41011da177e4SLinus Torvalds { 41021da177e4SLinus Torvalds struct sk_buff *skb; 41035578689aSDaniel Lezcano struct net *net = info->nl_net; 4104528c4cebSDenis V. Lunev u32 seq; 4105528c4cebSDenis V. Lunev int err; 41060d51aa80SJamal Hadi Salim 4107528c4cebSDenis V. Lunev err = -ENOBUFS; 410838308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 410986872cb5SThomas Graf 411019e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 411138308473SDavid S. Miller if (!skb) 411221713ebcSThomas Graf goto errout; 41131da177e4SLinus Torvalds 4114191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4115f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 411626932566SPatrick McHardy if (err < 0) { 411726932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 411826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 411926932566SPatrick McHardy kfree_skb(skb); 412026932566SPatrick McHardy goto errout; 412126932566SPatrick McHardy } 412215e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 41235578689aSDaniel Lezcano info->nlh, gfp_any()); 41241ce85fe4SPablo Neira Ayuso return; 412521713ebcSThomas Graf errout: 412621713ebcSThomas Graf if (err < 0) 41275578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 41281da177e4SLinus Torvalds } 41291da177e4SLinus Torvalds 41308ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4131351638e7SJiri Pirko unsigned long event, void *ptr) 41328ed67789SDaniel Lezcano { 4133351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4134c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 41358ed67789SDaniel Lezcano 4136242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4137242d3a49SWANG Cong return NOTIFY_OK; 4138242d3a49SWANG Cong 4139242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4140d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 41418ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 41428ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4143d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 41448ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4145d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 41468ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 41478ed67789SDaniel Lezcano #endif 414876da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 414976da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 415076da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 415176da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 415276da0704SWANG Cong */ 415312d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4154242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 415512d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 415612d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4157242d3a49SWANG Cong #endif 41588ed67789SDaniel Lezcano } 41598ed67789SDaniel Lezcano 41608ed67789SDaniel Lezcano return NOTIFY_OK; 41618ed67789SDaniel Lezcano } 41628ed67789SDaniel Lezcano 41631da177e4SLinus Torvalds /* 41641da177e4SLinus Torvalds * /proc 41651da177e4SLinus Torvalds */ 41661da177e4SLinus Torvalds 41671da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41681da177e4SLinus Torvalds 416933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 417033120b30SAlexey Dobriyan .owner = THIS_MODULE, 417133120b30SAlexey Dobriyan .open = ipv6_route_open, 417233120b30SAlexey Dobriyan .read = seq_read, 417333120b30SAlexey Dobriyan .llseek = seq_lseek, 41748d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 417533120b30SAlexey Dobriyan }; 417633120b30SAlexey Dobriyan 41771da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 41781da177e4SLinus Torvalds { 417969ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 41801da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 418169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 418269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 418369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 418469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 418569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4186fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 418769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 41881da177e4SLinus Torvalds 41891da177e4SLinus Torvalds return 0; 41901da177e4SLinus Torvalds } 41911da177e4SLinus Torvalds 41921da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 41931da177e4SLinus Torvalds { 4194de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 419569ddb805SDaniel Lezcano } 419669ddb805SDaniel Lezcano 41979a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 41981da177e4SLinus Torvalds .owner = THIS_MODULE, 41991da177e4SLinus Torvalds .open = rt6_stats_seq_open, 42001da177e4SLinus Torvalds .read = seq_read, 42011da177e4SLinus Torvalds .llseek = seq_lseek, 4202b6fcbdb4SPavel Emelyanov .release = single_release_net, 42031da177e4SLinus Torvalds }; 42041da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 42051da177e4SLinus Torvalds 42061da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 42071da177e4SLinus Torvalds 42081da177e4SLinus Torvalds static 4209fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 42101da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 42111da177e4SLinus Torvalds { 4212c486da34SLucian Adrian Grijincu struct net *net; 4213c486da34SLucian Adrian Grijincu int delay; 4214c486da34SLucian Adrian Grijincu if (!write) 4215c486da34SLucian Adrian Grijincu return -EINVAL; 4216c486da34SLucian Adrian Grijincu 4217c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4218c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 42198d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 42202ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 42211da177e4SLinus Torvalds return 0; 42221da177e4SLinus Torvalds } 42231da177e4SLinus Torvalds 4224fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 42251da177e4SLinus Torvalds { 42261da177e4SLinus Torvalds .procname = "flush", 42274990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 42281da177e4SLinus Torvalds .maxlen = sizeof(int), 422989c8b3a1SDave Jones .mode = 0200, 42306d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 42311da177e4SLinus Torvalds }, 42321da177e4SLinus Torvalds { 42331da177e4SLinus Torvalds .procname = "gc_thresh", 42349a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 42351da177e4SLinus Torvalds .maxlen = sizeof(int), 42361da177e4SLinus Torvalds .mode = 0644, 42376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42381da177e4SLinus Torvalds }, 42391da177e4SLinus Torvalds { 42401da177e4SLinus Torvalds .procname = "max_size", 42414990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 42421da177e4SLinus Torvalds .maxlen = sizeof(int), 42431da177e4SLinus Torvalds .mode = 0644, 42446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42451da177e4SLinus Torvalds }, 42461da177e4SLinus Torvalds { 42471da177e4SLinus Torvalds .procname = "gc_min_interval", 42484990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 42491da177e4SLinus Torvalds .maxlen = sizeof(int), 42501da177e4SLinus Torvalds .mode = 0644, 42516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42521da177e4SLinus Torvalds }, 42531da177e4SLinus Torvalds { 42541da177e4SLinus Torvalds .procname = "gc_timeout", 42554990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 42561da177e4SLinus Torvalds .maxlen = sizeof(int), 42571da177e4SLinus Torvalds .mode = 0644, 42586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42591da177e4SLinus Torvalds }, 42601da177e4SLinus Torvalds { 42611da177e4SLinus Torvalds .procname = "gc_interval", 42624990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 42631da177e4SLinus Torvalds .maxlen = sizeof(int), 42641da177e4SLinus Torvalds .mode = 0644, 42656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42661da177e4SLinus Torvalds }, 42671da177e4SLinus Torvalds { 42681da177e4SLinus Torvalds .procname = "gc_elasticity", 42694990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 42701da177e4SLinus Torvalds .maxlen = sizeof(int), 42711da177e4SLinus Torvalds .mode = 0644, 4272f3d3f616SMin Zhang .proc_handler = proc_dointvec, 42731da177e4SLinus Torvalds }, 42741da177e4SLinus Torvalds { 42751da177e4SLinus Torvalds .procname = "mtu_expires", 42764990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 42771da177e4SLinus Torvalds .maxlen = sizeof(int), 42781da177e4SLinus Torvalds .mode = 0644, 42796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42801da177e4SLinus Torvalds }, 42811da177e4SLinus Torvalds { 42821da177e4SLinus Torvalds .procname = "min_adv_mss", 42834990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 42841da177e4SLinus Torvalds .maxlen = sizeof(int), 42851da177e4SLinus Torvalds .mode = 0644, 4286f3d3f616SMin Zhang .proc_handler = proc_dointvec, 42871da177e4SLinus Torvalds }, 42881da177e4SLinus Torvalds { 42891da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 42904990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 42911da177e4SLinus Torvalds .maxlen = sizeof(int), 42921da177e4SLinus Torvalds .mode = 0644, 42936d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 42941da177e4SLinus Torvalds }, 4295f8572d8fSEric W. Biederman { } 42961da177e4SLinus Torvalds }; 42971da177e4SLinus Torvalds 42982c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4299760f2d01SDaniel Lezcano { 4300760f2d01SDaniel Lezcano struct ctl_table *table; 4301760f2d01SDaniel Lezcano 4302760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4303760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4304760f2d01SDaniel Lezcano GFP_KERNEL); 43055ee09105SYOSHIFUJI Hideaki 43065ee09105SYOSHIFUJI Hideaki if (table) { 43075ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4308c486da34SLucian Adrian Grijincu table[0].extra1 = net; 430986393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 43105ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 43115ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 43125ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 43135ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 43145ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 43155ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 43165ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 43179c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4318464dc801SEric W. Biederman 4319464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4320464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4321464dc801SEric W. Biederman table[0].procname = NULL; 43225ee09105SYOSHIFUJI Hideaki } 43235ee09105SYOSHIFUJI Hideaki 4324760f2d01SDaniel Lezcano return table; 4325760f2d01SDaniel Lezcano } 43261da177e4SLinus Torvalds #endif 43271da177e4SLinus Torvalds 43282c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4329cdb18761SDaniel Lezcano { 4330633d424bSPavel Emelyanov int ret = -ENOMEM; 43318ed67789SDaniel Lezcano 433286393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 433386393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4334f2fc6a54SBenjamin Thery 4335fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4336fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4337fc66f95cSEric Dumazet 43388ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 43398ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 43408ed67789SDaniel Lezcano GFP_KERNEL); 43418ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4342fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4343d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 43448ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4345d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 434662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 434762fa8a84SDavid S. Miller ip6_template_metrics, true); 43488ed67789SDaniel Lezcano 43498ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4350feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 43518ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 43528ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 43538ed67789SDaniel Lezcano GFP_KERNEL); 435468fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 435568fffc67SPeter Zijlstra goto out_ip6_null_entry; 4356d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 43578ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4358d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 435962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 436062fa8a84SDavid S. Miller ip6_template_metrics, true); 43618ed67789SDaniel Lezcano 43628ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 43638ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 43648ed67789SDaniel Lezcano GFP_KERNEL); 436568fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 436668fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4367d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 43688ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4369d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 437062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 437162fa8a84SDavid S. Miller ip6_template_metrics, true); 43728ed67789SDaniel Lezcano #endif 43738ed67789SDaniel Lezcano 4374b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4375b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4376b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4377b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4378b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4379b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4380b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4381b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4382b339a47cSPeter Zijlstra 43836891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 43846891a346SBenjamin Thery 43858ed67789SDaniel Lezcano ret = 0; 43868ed67789SDaniel Lezcano out: 43878ed67789SDaniel Lezcano return ret; 4388f2fc6a54SBenjamin Thery 438968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 439068fffc67SPeter Zijlstra out_ip6_prohibit_entry: 439168fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 439268fffc67SPeter Zijlstra out_ip6_null_entry: 439368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 439468fffc67SPeter Zijlstra #endif 4395fc66f95cSEric Dumazet out_ip6_dst_entries: 4396fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4397f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4398f2fc6a54SBenjamin Thery goto out; 4399cdb18761SDaniel Lezcano } 4400cdb18761SDaniel Lezcano 44012c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4402cdb18761SDaniel Lezcano { 44038ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 44048ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 44058ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 44068ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 44078ed67789SDaniel Lezcano #endif 440841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4409cdb18761SDaniel Lezcano } 4410cdb18761SDaniel Lezcano 4411d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4412d189634eSThomas Graf { 4413d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4414d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4415d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4416d189634eSThomas Graf #endif 4417d189634eSThomas Graf return 0; 4418d189634eSThomas Graf } 4419d189634eSThomas Graf 4420d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4421d189634eSThomas Graf { 4422d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4423ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4424ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4425d189634eSThomas Graf #endif 4426d189634eSThomas Graf } 4427d189634eSThomas Graf 4428cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4429cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4430cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4431cdb18761SDaniel Lezcano }; 4432cdb18761SDaniel Lezcano 4433c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4434c3426b47SDavid S. Miller { 4435c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4436c3426b47SDavid S. Miller 4437c3426b47SDavid S. Miller if (!bp) 4438c3426b47SDavid S. Miller return -ENOMEM; 4439c3426b47SDavid S. Miller inet_peer_base_init(bp); 4440c3426b47SDavid S. Miller net->ipv6.peers = bp; 4441c3426b47SDavid S. Miller return 0; 4442c3426b47SDavid S. Miller } 4443c3426b47SDavid S. Miller 4444c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4445c3426b47SDavid S. Miller { 4446c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4447c3426b47SDavid S. Miller 4448c3426b47SDavid S. Miller net->ipv6.peers = NULL; 444956a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4450c3426b47SDavid S. Miller kfree(bp); 4451c3426b47SDavid S. Miller } 4452c3426b47SDavid S. Miller 44532b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4454c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4455c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4456c3426b47SDavid S. Miller }; 4457c3426b47SDavid S. Miller 4458d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4459d189634eSThomas Graf .init = ip6_route_net_init_late, 4460d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4461d189634eSThomas Graf }; 4462d189634eSThomas Graf 44638ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 44648ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4465242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 44668ed67789SDaniel Lezcano }; 44678ed67789SDaniel Lezcano 44682f460933SWANG Cong void __init ip6_route_init_special_entries(void) 44692f460933SWANG Cong { 44702f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 44712f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 44722f460933SWANG Cong * manually for init_net */ 44732f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 44742f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 44752f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 44762f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 44772f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 44782f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 44792f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 44802f460933SWANG Cong #endif 44812f460933SWANG Cong } 44822f460933SWANG Cong 4483433d49c3SDaniel Lezcano int __init ip6_route_init(void) 44841da177e4SLinus Torvalds { 4485433d49c3SDaniel Lezcano int ret; 44868d0b94afSMartin KaFai Lau int cpu; 4487433d49c3SDaniel Lezcano 44889a7ec3a9SDaniel Lezcano ret = -ENOMEM; 44899a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 44909a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 44919a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 44929a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4493c19a28e1SFernando Carrijo goto out; 449414e50e57SDavid S. Miller 4495fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 44968ed67789SDaniel Lezcano if (ret) 4497bdb3289fSDaniel Lezcano goto out_kmem_cache; 4498bdb3289fSDaniel Lezcano 4499c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4500c3426b47SDavid S. Miller if (ret) 4501e8803b6cSDavid S. Miller goto out_dst_entries; 45022a0c451aSThomas Graf 45037e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 45047e52b33bSDavid S. Miller if (ret) 45057e52b33bSDavid S. Miller goto out_register_inetpeer; 4506c3426b47SDavid S. Miller 45075dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 45085dc121e9SArnaud Ebalard 4509e8803b6cSDavid S. Miller ret = fib6_init(); 4510433d49c3SDaniel Lezcano if (ret) 45118ed67789SDaniel Lezcano goto out_register_subsys; 4512433d49c3SDaniel Lezcano 4513433d49c3SDaniel Lezcano ret = xfrm6_init(); 4514433d49c3SDaniel Lezcano if (ret) 4515e8803b6cSDavid S. Miller goto out_fib6_init; 4516c35b7e72SDaniel Lezcano 4517433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4518433d49c3SDaniel Lezcano if (ret) 4519433d49c3SDaniel Lezcano goto xfrm6_init; 45207e5449c2SDaniel Lezcano 4521d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4522d189634eSThomas Graf if (ret) 4523d189634eSThomas Graf goto fib6_rules_init; 4524d189634eSThomas Graf 4525433d49c3SDaniel Lezcano ret = -ENOBUFS; 4526b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4527b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4528e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4529e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4530d189634eSThomas Graf goto out_register_late_subsys; 4531433d49c3SDaniel Lezcano 45328ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4533cdb18761SDaniel Lezcano if (ret) 4534d189634eSThomas Graf goto out_register_late_subsys; 45358ed67789SDaniel Lezcano 45368d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 45378d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 45388d0b94afSMartin KaFai Lau 45398d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 45408d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 45418d0b94afSMartin KaFai Lau } 45428d0b94afSMartin KaFai Lau 4543433d49c3SDaniel Lezcano out: 4544433d49c3SDaniel Lezcano return ret; 4545433d49c3SDaniel Lezcano 4546d189634eSThomas Graf out_register_late_subsys: 4547d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4548433d49c3SDaniel Lezcano fib6_rules_init: 4549433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4550433d49c3SDaniel Lezcano xfrm6_init: 4551433d49c3SDaniel Lezcano xfrm6_fini(); 45522a0c451aSThomas Graf out_fib6_init: 45532a0c451aSThomas Graf fib6_gc_cleanup(); 45548ed67789SDaniel Lezcano out_register_subsys: 45558ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 45567e52b33bSDavid S. Miller out_register_inetpeer: 45577e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4558fc66f95cSEric Dumazet out_dst_entries: 4559fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4560433d49c3SDaniel Lezcano out_kmem_cache: 4561f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4562433d49c3SDaniel Lezcano goto out; 45631da177e4SLinus Torvalds } 45641da177e4SLinus Torvalds 45651da177e4SLinus Torvalds void ip6_route_cleanup(void) 45661da177e4SLinus Torvalds { 45678ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4568d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4569101367c2SThomas Graf fib6_rules_cleanup(); 45701da177e4SLinus Torvalds xfrm6_fini(); 45711da177e4SLinus Torvalds fib6_gc_cleanup(); 4572c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 45738ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 457441bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4575f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 45761da177e4SLinus Torvalds } 4577