11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 4735732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 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); 10835732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 10935732d01SWei Wang struct in6_addr *daddr, 11035732d01SWei 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; 39935732d01SWei 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 } 41235732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 41335732d01SWei Wang if (bucket) { 41435732d01SWei Wang rt->rt6i_exception_bucket = NULL; 41535732d01SWei Wang kfree(bucket); 41635732d01SWei 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 110435732d01SWei Wang /* exception hash table implementation 110535732d01SWei Wang */ 110635732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 110735732d01SWei Wang 110835732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 110935732d01SWei Wang * Caller must hold rt6_exception_lock 111035732d01SWei Wang */ 111135732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 111235732d01SWei Wang struct rt6_exception *rt6_ex) 111335732d01SWei Wang { 111435732d01SWei Wang if (!bucket || !rt6_ex) 111535732d01SWei Wang return; 111635732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 111735732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 111835732d01SWei Wang rt6_release(rt6_ex->rt6i); 111935732d01SWei Wang kfree_rcu(rt6_ex, rcu); 112035732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 112135732d01SWei Wang bucket->depth--; 112235732d01SWei Wang } 112335732d01SWei Wang 112435732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 112535732d01SWei Wang * Caller must hold rt6_exception_lock 112635732d01SWei Wang */ 112735732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 112835732d01SWei Wang { 112935732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 113035732d01SWei Wang 113135732d01SWei Wang if (!bucket) 113235732d01SWei Wang return; 113335732d01SWei Wang 113435732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 113535732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 113635732d01SWei Wang oldest = rt6_ex; 113735732d01SWei Wang } 113835732d01SWei Wang rt6_remove_exception(bucket, oldest); 113935732d01SWei Wang } 114035732d01SWei Wang 114135732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 114235732d01SWei Wang const struct in6_addr *src) 114335732d01SWei Wang { 114435732d01SWei Wang static u32 seed __read_mostly; 114535732d01SWei Wang u32 val; 114635732d01SWei Wang 114735732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 114835732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 114935732d01SWei Wang 115035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 115135732d01SWei Wang if (src) 115235732d01SWei Wang val = jhash(src, sizeof(*src), val); 115335732d01SWei Wang #endif 115435732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 115535732d01SWei Wang } 115635732d01SWei Wang 115735732d01SWei Wang /* Helper function to find the cached rt in the hash table 115835732d01SWei Wang * and update bucket pointer to point to the bucket for this 115935732d01SWei Wang * (daddr, saddr) pair 116035732d01SWei Wang * Caller must hold rt6_exception_lock 116135732d01SWei Wang */ 116235732d01SWei Wang static struct rt6_exception * 116335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 116435732d01SWei Wang const struct in6_addr *daddr, 116535732d01SWei Wang const struct in6_addr *saddr) 116635732d01SWei Wang { 116735732d01SWei Wang struct rt6_exception *rt6_ex; 116835732d01SWei Wang u32 hval; 116935732d01SWei Wang 117035732d01SWei Wang if (!(*bucket) || !daddr) 117135732d01SWei Wang return NULL; 117235732d01SWei Wang 117335732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 117435732d01SWei Wang *bucket += hval; 117535732d01SWei Wang 117635732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 117735732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 117835732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 117935732d01SWei Wang 118035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 118135732d01SWei Wang if (matched && saddr) 118235732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 118335732d01SWei Wang #endif 118435732d01SWei Wang if (matched) 118535732d01SWei Wang return rt6_ex; 118635732d01SWei Wang } 118735732d01SWei Wang return NULL; 118835732d01SWei Wang } 118935732d01SWei Wang 119035732d01SWei Wang /* Helper function to find the cached rt in the hash table 119135732d01SWei Wang * and update bucket pointer to point to the bucket for this 119235732d01SWei Wang * (daddr, saddr) pair 119335732d01SWei Wang * Caller must hold rcu_read_lock() 119435732d01SWei Wang */ 119535732d01SWei Wang static struct rt6_exception * 119635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 119735732d01SWei Wang const struct in6_addr *daddr, 119835732d01SWei Wang const struct in6_addr *saddr) 119935732d01SWei Wang { 120035732d01SWei Wang struct rt6_exception *rt6_ex; 120135732d01SWei Wang u32 hval; 120235732d01SWei Wang 120335732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 120435732d01SWei Wang 120535732d01SWei Wang if (!(*bucket) || !daddr) 120635732d01SWei Wang return NULL; 120735732d01SWei Wang 120835732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 120935732d01SWei Wang *bucket += hval; 121035732d01SWei Wang 121135732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 121235732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 121335732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 121435732d01SWei Wang 121535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 121635732d01SWei Wang if (matched && saddr) 121735732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 121835732d01SWei Wang #endif 121935732d01SWei Wang if (matched) 122035732d01SWei Wang return rt6_ex; 122135732d01SWei Wang } 122235732d01SWei Wang return NULL; 122335732d01SWei Wang } 122435732d01SWei Wang 122535732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 122635732d01SWei Wang struct rt6_info *ort) 122735732d01SWei Wang { 122835732d01SWei Wang struct rt6_exception_bucket *bucket; 122935732d01SWei Wang struct in6_addr *src_key = NULL; 123035732d01SWei Wang struct rt6_exception *rt6_ex; 123135732d01SWei Wang int err = 0; 123235732d01SWei Wang 123335732d01SWei Wang /* ort can't be a cache or pcpu route */ 123435732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 123535732d01SWei Wang ort = (struct rt6_info *)ort->dst.from; 123635732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 123735732d01SWei Wang 123835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 123935732d01SWei Wang 124035732d01SWei Wang if (ort->exception_bucket_flushed) { 124135732d01SWei Wang err = -EINVAL; 124235732d01SWei Wang goto out; 124335732d01SWei Wang } 124435732d01SWei Wang 124535732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 124635732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 124735732d01SWei Wang if (!bucket) { 124835732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 124935732d01SWei Wang GFP_ATOMIC); 125035732d01SWei Wang if (!bucket) { 125135732d01SWei Wang err = -ENOMEM; 125235732d01SWei Wang goto out; 125335732d01SWei Wang } 125435732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 125535732d01SWei Wang } 125635732d01SWei Wang 125735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 125835732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 125935732d01SWei Wang * and exception table is indexed by a hash of 126035732d01SWei Wang * both rt6i_dst and rt6i_src. 126135732d01SWei Wang * Otherwise, the exception table is indexed by 126235732d01SWei Wang * a hash of only rt6i_dst. 126335732d01SWei Wang */ 126435732d01SWei Wang if (ort->rt6i_src.plen) 126535732d01SWei Wang src_key = &nrt->rt6i_src.addr; 126635732d01SWei Wang #endif 1267*60006a48SWei Wang 1268*60006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 1269*60006a48SWei Wang * in rt6_remove_prefsrc() 1270*60006a48SWei Wang */ 1271*60006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1272*60006a48SWei Wang 127335732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 127435732d01SWei Wang src_key); 127535732d01SWei Wang if (rt6_ex) 127635732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 127735732d01SWei Wang 127835732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 127935732d01SWei Wang if (!rt6_ex) { 128035732d01SWei Wang err = -ENOMEM; 128135732d01SWei Wang goto out; 128235732d01SWei Wang } 128335732d01SWei Wang rt6_ex->rt6i = nrt; 128435732d01SWei Wang rt6_ex->stamp = jiffies; 128535732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 128635732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 128735732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 128835732d01SWei Wang bucket->depth++; 128935732d01SWei Wang 129035732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 129135732d01SWei Wang rt6_exception_remove_oldest(bucket); 129235732d01SWei Wang 129335732d01SWei Wang out: 129435732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 129535732d01SWei Wang 129635732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 129735732d01SWei Wang if (!err) 129835732d01SWei Wang fib6_update_sernum(ort); 129935732d01SWei Wang 130035732d01SWei Wang return err; 130135732d01SWei Wang } 130235732d01SWei Wang 130335732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 130435732d01SWei Wang { 130535732d01SWei Wang struct rt6_exception_bucket *bucket; 130635732d01SWei Wang struct rt6_exception *rt6_ex; 130735732d01SWei Wang struct hlist_node *tmp; 130835732d01SWei Wang int i; 130935732d01SWei Wang 131035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 131135732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 131235732d01SWei Wang rt->exception_bucket_flushed = 1; 131335732d01SWei Wang 131435732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 131535732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 131635732d01SWei Wang if (!bucket) 131735732d01SWei Wang goto out; 131835732d01SWei Wang 131935732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 132035732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 132135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 132235732d01SWei Wang WARN_ON_ONCE(bucket->depth); 132335732d01SWei Wang bucket++; 132435732d01SWei Wang } 132535732d01SWei Wang 132635732d01SWei Wang out: 132735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 132835732d01SWei Wang } 132935732d01SWei Wang 133035732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 133135732d01SWei Wang * Caller has to hold rcu_read_lock() 133235732d01SWei Wang */ 133335732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 133435732d01SWei Wang struct in6_addr *daddr, 133535732d01SWei Wang struct in6_addr *saddr) 133635732d01SWei Wang { 133735732d01SWei Wang struct rt6_exception_bucket *bucket; 133835732d01SWei Wang struct in6_addr *src_key = NULL; 133935732d01SWei Wang struct rt6_exception *rt6_ex; 134035732d01SWei Wang struct rt6_info *res = NULL; 134135732d01SWei Wang 134235732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 134335732d01SWei Wang 134435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 134535732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 134635732d01SWei Wang * and exception table is indexed by a hash of 134735732d01SWei Wang * both rt6i_dst and rt6i_src. 134835732d01SWei Wang * Otherwise, the exception table is indexed by 134935732d01SWei Wang * a hash of only rt6i_dst. 135035732d01SWei Wang */ 135135732d01SWei Wang if (rt->rt6i_src.plen) 135235732d01SWei Wang src_key = saddr; 135335732d01SWei Wang #endif 135435732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 135535732d01SWei Wang 135635732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 135735732d01SWei Wang res = rt6_ex->rt6i; 135835732d01SWei Wang 135935732d01SWei Wang return res; 136035732d01SWei Wang } 136135732d01SWei Wang 136235732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 136335732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 136435732d01SWei Wang { 136535732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 136635732d01SWei Wang struct rt6_exception_bucket *bucket; 136735732d01SWei Wang struct in6_addr *src_key = NULL; 136835732d01SWei Wang struct rt6_exception *rt6_ex; 136935732d01SWei Wang int err; 137035732d01SWei Wang 137135732d01SWei Wang if (!from || 137235732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 137335732d01SWei Wang return -EINVAL; 137435732d01SWei Wang 137535732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 137635732d01SWei Wang return -ENOENT; 137735732d01SWei Wang 137835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 137935732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 138035732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 138135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 138235732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 138335732d01SWei Wang * and exception table is indexed by a hash of 138435732d01SWei Wang * both rt6i_dst and rt6i_src. 138535732d01SWei Wang * Otherwise, the exception table is indexed by 138635732d01SWei Wang * a hash of only rt6i_dst. 138735732d01SWei Wang */ 138835732d01SWei Wang if (from->rt6i_src.plen) 138935732d01SWei Wang src_key = &rt->rt6i_src.addr; 139035732d01SWei Wang #endif 139135732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 139235732d01SWei Wang &rt->rt6i_dst.addr, 139335732d01SWei Wang src_key); 139435732d01SWei Wang if (rt6_ex) { 139535732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 139635732d01SWei Wang err = 0; 139735732d01SWei Wang } else { 139835732d01SWei Wang err = -ENOENT; 139935732d01SWei Wang } 140035732d01SWei Wang 140135732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 140235732d01SWei Wang return err; 140335732d01SWei Wang } 140435732d01SWei Wang 140535732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 140635732d01SWei Wang * refresh its stamp 140735732d01SWei Wang */ 140835732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 140935732d01SWei Wang { 141035732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 141135732d01SWei Wang struct rt6_exception_bucket *bucket; 141235732d01SWei Wang struct in6_addr *src_key = NULL; 141335732d01SWei Wang struct rt6_exception *rt6_ex; 141435732d01SWei Wang 141535732d01SWei Wang if (!from || 141635732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 141735732d01SWei Wang return; 141835732d01SWei Wang 141935732d01SWei Wang rcu_read_lock(); 142035732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 142135732d01SWei Wang 142235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 142335732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 142435732d01SWei Wang * and exception table is indexed by a hash of 142535732d01SWei Wang * both rt6i_dst and rt6i_src. 142635732d01SWei Wang * Otherwise, the exception table is indexed by 142735732d01SWei Wang * a hash of only rt6i_dst. 142835732d01SWei Wang */ 142935732d01SWei Wang if (from->rt6i_src.plen) 143035732d01SWei Wang src_key = &rt->rt6i_src.addr; 143135732d01SWei Wang #endif 143235732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 143335732d01SWei Wang &rt->rt6i_dst.addr, 143435732d01SWei Wang src_key); 143535732d01SWei Wang if (rt6_ex) 143635732d01SWei Wang rt6_ex->stamp = jiffies; 143735732d01SWei Wang 143835732d01SWei Wang rcu_read_unlock(); 143935732d01SWei Wang } 144035732d01SWei Wang 1441*60006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 1442*60006a48SWei Wang { 1443*60006a48SWei Wang struct rt6_exception_bucket *bucket; 1444*60006a48SWei Wang struct rt6_exception *rt6_ex; 1445*60006a48SWei Wang int i; 1446*60006a48SWei Wang 1447*60006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1448*60006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 1449*60006a48SWei Wang 1450*60006a48SWei Wang if (bucket) { 1451*60006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1452*60006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1453*60006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 1454*60006a48SWei Wang } 1455*60006a48SWei Wang bucket++; 1456*60006a48SWei Wang } 1457*60006a48SWei Wang } 1458*60006a48SWei Wang } 1459*60006a48SWei Wang 14609ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 14619ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 14621da177e4SLinus Torvalds { 1463367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 146445e4fd26SMartin KaFai Lau struct rt6_info *rt; 1465c71099acSThomas Graf int strict = 0; 14661da177e4SLinus Torvalds 146777d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1468d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1469367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1470367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 14711da177e4SLinus Torvalds 1472c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 14731da177e4SLinus Torvalds 14744c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1475367efcb9SMartin KaFai Lau saved_fn = fn; 14761da177e4SLinus Torvalds 1477ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1478ca254490SDavid Ahern oif = 0; 1479ca254490SDavid Ahern 1480a3c00e46SMartin KaFai Lau redo_rt6_select: 1481367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 148252bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1483367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1484a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1485a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1486a3c00e46SMartin KaFai Lau if (fn) 1487a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1488367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1489367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1490367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1491367efcb9SMartin KaFai Lau fn = saved_fn; 1492367efcb9SMartin KaFai Lau goto redo_rt6_select; 1493367efcb9SMartin KaFai Lau } 1494a3c00e46SMartin KaFai Lau } 1495a3c00e46SMartin KaFai Lau 1496d52d3997SMartin KaFai Lau 1497d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 14983da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1499c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15001da177e4SLinus Torvalds 1501d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1502b811580dSDavid Ahern 1503b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1504d52d3997SMartin KaFai Lau return rt; 15053da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 15063da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 15073da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 15083da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 15093da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 15103da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 15113da59bd9SMartin KaFai Lau */ 1512c71099acSThomas Graf 15133da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 15143da59bd9SMartin KaFai Lau 1515d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1516d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1517d52d3997SMartin KaFai Lau 15183da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 15193da59bd9SMartin KaFai Lau dst_release(&rt->dst); 15203da59bd9SMartin KaFai Lau 15211cfb71eeSWei Wang if (uncached_rt) { 15221cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 15231cfb71eeSWei Wang * No need for another dst_hold() 15241cfb71eeSWei Wang */ 15258d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 15261cfb71eeSWei Wang } else { 15273da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 15283da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 15291cfb71eeSWei Wang } 1530b811580dSDavid Ahern 1531b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 15323da59bd9SMartin KaFai Lau return uncached_rt; 15333da59bd9SMartin KaFai Lau 1534d52d3997SMartin KaFai Lau } else { 1535d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1536d52d3997SMartin KaFai Lau 1537d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1538d52d3997SMartin KaFai Lau 1539d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1540d52d3997SMartin KaFai Lau rt->dst.__use++; 1541d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1542d52d3997SMartin KaFai Lau 15439c7370a1SMartin KaFai Lau if (pcpu_rt) { 1544d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 15459c7370a1SMartin KaFai Lau } else { 15469c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 15479c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 15489c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 15499c7370a1SMartin KaFai Lau */ 15509c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 15519c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 15529c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 15539c7370a1SMartin KaFai Lau dst_release(&rt->dst); 15549c7370a1SMartin KaFai Lau } 1555d52d3997SMartin KaFai Lau 1556b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1557d52d3997SMartin KaFai Lau return pcpu_rt; 15589c7370a1SMartin KaFai Lau 1559d52d3997SMartin KaFai Lau } 1560c71099acSThomas Graf } 15619ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1562c71099acSThomas Graf 15638ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 15644c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 15654acad72dSPavel Emelyanov { 15664c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 15674acad72dSPavel Emelyanov } 15684acad72dSPavel Emelyanov 1569d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 157072331bc0SShmulik Ladkani struct net_device *dev, 157172331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 157272331bc0SShmulik Ladkani { 157372331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 157472331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 157572331bc0SShmulik Ladkani 157672331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 157772331bc0SShmulik Ladkani } 1578d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 157972331bc0SShmulik Ladkani 158023aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 158123aebdacSJakub Sitnicki struct flow_keys *keys) 158223aebdacSJakub Sitnicki { 158323aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 158423aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 158523aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 158623aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 158723aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 158823aebdacSJakub Sitnicki 158923aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 159023aebdacSJakub Sitnicki goto out; 159123aebdacSJakub Sitnicki 159223aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 159323aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 159423aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 159523aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 159623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 159723aebdacSJakub Sitnicki goto out; 159823aebdacSJakub Sitnicki 159923aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 160023aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 160123aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 160223aebdacSJakub Sitnicki if (!inner_iph) 160323aebdacSJakub Sitnicki goto out; 160423aebdacSJakub Sitnicki 160523aebdacSJakub Sitnicki key_iph = inner_iph; 160623aebdacSJakub Sitnicki out: 160723aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 160823aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 160923aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 161023aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 161123aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 161223aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 161323aebdacSJakub Sitnicki } 161423aebdacSJakub Sitnicki 161523aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 161623aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 161723aebdacSJakub Sitnicki { 161823aebdacSJakub Sitnicki struct flow_keys hash_keys; 161923aebdacSJakub Sitnicki 162023aebdacSJakub Sitnicki if (skb) { 162123aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 162223aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 162323aebdacSJakub Sitnicki } 162423aebdacSJakub Sitnicki 162523aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 162623aebdacSJakub Sitnicki } 162723aebdacSJakub Sitnicki 1628c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1629c71099acSThomas Graf { 1630b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1631c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1632adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1633904af04dSJiri Benc struct ip_tunnel_info *tun_info; 16344c9483b2SDavid S. Miller struct flowi6 fl6 = { 1635e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 16364c9483b2SDavid S. Miller .daddr = iph->daddr, 16374c9483b2SDavid S. Miller .saddr = iph->saddr, 16386502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 16394c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 16404c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1641c71099acSThomas Graf }; 1642adaa70bbSThomas Graf 1643904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 164446fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1645904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 164623aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 164723aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 164806e9d040SJiri Benc skb_dst_drop(skb); 164972331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1650c71099acSThomas Graf } 1651c71099acSThomas Graf 16528ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 16534c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1654c71099acSThomas Graf { 16554c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1656c71099acSThomas Graf } 1657c71099acSThomas Graf 16586f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 16596f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1660c71099acSThomas Graf { 1661d46a9d67SDavid Ahern bool any_src; 1662c71099acSThomas Graf 16634c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 16644c1feac5SDavid Ahern struct dst_entry *dst; 16654c1feac5SDavid Ahern 16664c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1667ca254490SDavid Ahern if (dst) 1668ca254490SDavid Ahern return dst; 16694c1feac5SDavid Ahern } 1670ca254490SDavid Ahern 16711fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 16724dc27d1cSDavid McCullough 1673d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1674741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1675d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 167677d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1677c71099acSThomas Graf 1678d46a9d67SDavid Ahern if (!any_src) 1679adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 16800c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 16810c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1682adaa70bbSThomas Graf 16834c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 16841da177e4SLinus Torvalds } 16856f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 16861da177e4SLinus Torvalds 16872774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 168814e50e57SDavid S. Miller { 16895c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 16901dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 169114e50e57SDavid S. Miller struct dst_entry *new = NULL; 169214e50e57SDavid S. Miller 16931dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1694b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 169514e50e57SDavid S. Miller if (rt) { 16960a1f5962SMartin KaFai Lau rt6_info_init(rt); 16970a1f5962SMartin KaFai Lau 1698d8d1f30bSChangli Gao new = &rt->dst; 169914e50e57SDavid S. Miller new->__use = 1; 1700352e512cSHerbert Xu new->input = dst_discard; 1701ede2059dSEric W. Biederman new->output = dst_discard_out; 170214e50e57SDavid S. Miller 1703defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 170414e50e57SDavid S. Miller 17051dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 17064e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 17070a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 170814e50e57SDavid S. Miller rt->rt6i_metric = 0; 170914e50e57SDavid S. Miller 171014e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 171114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 171214e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 171314e50e57SDavid S. Miller #endif 171414e50e57SDavid S. Miller } 171514e50e57SDavid S. Miller 171669ead7afSDavid S. Miller dst_release(dst_orig); 171769ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 171814e50e57SDavid S. Miller } 171914e50e57SDavid S. Miller 17201da177e4SLinus Torvalds /* 17211da177e4SLinus Torvalds * Destination cache support functions 17221da177e4SLinus Torvalds */ 17231da177e4SLinus Torvalds 17244b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 17254b32b5adSMartin KaFai Lau { 17264b32b5adSMartin KaFai Lau if (rt->dst.from && 17274b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 17284b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 17294b32b5adSMartin KaFai Lau } 17304b32b5adSMartin KaFai Lau 17313da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 17323da59bd9SMartin KaFai Lau { 173336143645SSteffen Klassert u32 rt_cookie = 0; 1734c5cff856SWei Wang 1735c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 17363da59bd9SMartin KaFai Lau return NULL; 17373da59bd9SMartin KaFai Lau 17383da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 17393da59bd9SMartin KaFai Lau return NULL; 17403da59bd9SMartin KaFai Lau 17413da59bd9SMartin KaFai Lau return &rt->dst; 17423da59bd9SMartin KaFai Lau } 17433da59bd9SMartin KaFai Lau 17443da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 17453da59bd9SMartin KaFai Lau { 17465973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 17475973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 17483da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 17493da59bd9SMartin KaFai Lau return &rt->dst; 17503da59bd9SMartin KaFai Lau else 17513da59bd9SMartin KaFai Lau return NULL; 17523da59bd9SMartin KaFai Lau } 17533da59bd9SMartin KaFai Lau 17541da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 17551da177e4SLinus Torvalds { 17561da177e4SLinus Torvalds struct rt6_info *rt; 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 17591da177e4SLinus Torvalds 17606f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 17616f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 17626f3118b5SNicolas Dichtel * into this function always. 17636f3118b5SNicolas Dichtel */ 1764e3bc10bdSHannes Frederic Sowa 17654b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 17664b32b5adSMartin KaFai Lau 176702bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1768a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 17693da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 17703da59bd9SMartin KaFai Lau else 17713da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 17751da177e4SLinus Torvalds { 17761da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds if (rt) { 177954c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 178054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1781e0a1ad73SThomas Graf ip6_del_rt(rt); 178254c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 17831da177e4SLinus Torvalds } 178454c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 178554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 178654c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 178754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 178854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 178954c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds struct rt6_info *rt; 17951da177e4SLinus Torvalds 17963ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 17971da177e4SLinus Torvalds 1798adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 17991da177e4SLinus Torvalds if (rt) { 18001eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1801ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 18028e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 1803c5cff856SWei Wang } else { 1804c5cff856SWei Wang struct fib6_node *fn; 1805c5cff856SWei Wang 1806c5cff856SWei Wang rcu_read_lock(); 1807c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 1808c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 1809c5cff856SWei Wang fn->fn_sernum = -1; 1810c5cff856SWei Wang rcu_read_unlock(); 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds } 18131eb4f758SHannes Frederic Sowa } 18141da177e4SLinus Torvalds 181545e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 181645e4fd26SMartin KaFai Lau { 181745e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 181845e4fd26SMartin KaFai Lau 181945e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 182045e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 182145e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 182245e4fd26SMartin KaFai Lau } 182345e4fd26SMartin KaFai Lau 18240d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 18250d3f6d29SMartin KaFai Lau { 18260d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 18274e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 18284e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 18290d3f6d29SMartin KaFai Lau } 18300d3f6d29SMartin KaFai Lau 183145e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 183245e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 18331da177e4SLinus Torvalds { 18340dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 18351da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 18361da177e4SLinus Torvalds 183745e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 183845e4fd26SMartin KaFai Lau return; 183945e4fd26SMartin KaFai Lau 184019bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 184119bda36cSXin Long return; 184219bda36cSXin Long 184345e4fd26SMartin KaFai Lau if (iph) { 184445e4fd26SMartin KaFai Lau daddr = &iph->daddr; 184545e4fd26SMartin KaFai Lau saddr = &iph->saddr; 184645e4fd26SMartin KaFai Lau } else if (sk) { 184745e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 184845e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 184945e4fd26SMartin KaFai Lau } else { 18500dec879fSJulian Anastasov daddr = NULL; 18510dec879fSJulian Anastasov saddr = NULL; 18521da177e4SLinus Torvalds } 18530dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 18540dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 18550dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 18560dec879fSJulian Anastasov return; 18570dec879fSJulian Anastasov 18580dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 18590dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 18600dec879fSJulian Anastasov } else if (daddr) { 18610dec879fSJulian Anastasov struct rt6_info *nrt6; 18620dec879fSJulian Anastasov 186345e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 186445e4fd26SMartin KaFai Lau if (nrt6) { 186545e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 186645e4fd26SMartin KaFai Lau 186745e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 186845e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 186945e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 187045e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 187145e4fd26SMartin KaFai Lau */ 187245e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 18731cfb71eeSWei Wang /* Release the reference taken in 18741cfb71eeSWei Wang * ip6_rt_cache_alloc() 18751cfb71eeSWei Wang */ 18761cfb71eeSWei Wang dst_release(&nrt6->dst); 187745e4fd26SMartin KaFai Lau } 187845e4fd26SMartin KaFai Lau } 187945e4fd26SMartin KaFai Lau } 188045e4fd26SMartin KaFai Lau 188145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 188245e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 188345e4fd26SMartin KaFai Lau { 188445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 188742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1888e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 188981aded24SDavid S. Miller { 189081aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 189181aded24SDavid S. Miller struct dst_entry *dst; 189281aded24SDavid S. Miller struct flowi6 fl6; 189381aded24SDavid S. Miller 189481aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 189581aded24SDavid S. Miller fl6.flowi6_oif = oif; 18961b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 189781aded24SDavid S. Miller fl6.daddr = iph->daddr; 189881aded24SDavid S. Miller fl6.saddr = iph->saddr; 18996502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1900e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 190181aded24SDavid S. Miller 190281aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 190381aded24SDavid S. Miller if (!dst->error) 190445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 190581aded24SDavid S. Miller dst_release(dst); 190681aded24SDavid S. Miller } 190781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 190881aded24SDavid S. Miller 190981aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 191081aded24SDavid S. Miller { 191133c162a9SMartin KaFai Lau struct dst_entry *dst; 191233c162a9SMartin KaFai Lau 191381aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1914e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 191533c162a9SMartin KaFai Lau 191633c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 191733c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 191833c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 191933c162a9SMartin KaFai Lau return; 192033c162a9SMartin KaFai Lau 192133c162a9SMartin KaFai Lau bh_lock_sock(sk); 192233c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 192333c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 192433c162a9SMartin KaFai Lau bh_unlock_sock(sk); 192581aded24SDavid S. Miller } 192681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 192781aded24SDavid S. Miller 1928b55b76b2SDuan Jiong /* Handle redirects */ 1929b55b76b2SDuan Jiong struct ip6rd_flowi { 1930b55b76b2SDuan Jiong struct flowi6 fl6; 1931b55b76b2SDuan Jiong struct in6_addr gateway; 1932b55b76b2SDuan Jiong }; 1933b55b76b2SDuan Jiong 1934b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1935b55b76b2SDuan Jiong struct fib6_table *table, 1936b55b76b2SDuan Jiong struct flowi6 *fl6, 1937b55b76b2SDuan Jiong int flags) 1938b55b76b2SDuan Jiong { 1939b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1940b55b76b2SDuan Jiong struct rt6_info *rt; 1941b55b76b2SDuan Jiong struct fib6_node *fn; 1942b55b76b2SDuan Jiong 1943b55b76b2SDuan Jiong /* Get the "current" route for this destination and 194467c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1945b55b76b2SDuan Jiong * 1946b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1947b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1948b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1949b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1950b55b76b2SDuan Jiong * routes. 1951b55b76b2SDuan Jiong */ 1952b55b76b2SDuan Jiong 1953b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1954b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1955b55b76b2SDuan Jiong restart: 1956b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1957b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1958b55b76b2SDuan Jiong continue; 1959b55b76b2SDuan Jiong if (rt->dst.error) 1960b55b76b2SDuan Jiong break; 1961b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1962b55b76b2SDuan Jiong continue; 1963b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1964b55b76b2SDuan Jiong continue; 1965b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1966b55b76b2SDuan Jiong continue; 1967b55b76b2SDuan Jiong break; 1968b55b76b2SDuan Jiong } 1969b55b76b2SDuan Jiong 1970b55b76b2SDuan Jiong if (!rt) 1971b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1972b55b76b2SDuan Jiong else if (rt->dst.error) { 1973b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1974b0a1ba59SMartin KaFai Lau goto out; 1975b0a1ba59SMartin KaFai Lau } 1976b0a1ba59SMartin KaFai Lau 1977b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1978a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1979a3c00e46SMartin KaFai Lau if (fn) 1980a3c00e46SMartin KaFai Lau goto restart; 1981b55b76b2SDuan Jiong } 1982a3c00e46SMartin KaFai Lau 1983b0a1ba59SMartin KaFai Lau out: 1984b55b76b2SDuan Jiong dst_hold(&rt->dst); 1985b55b76b2SDuan Jiong 1986b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1987b55b76b2SDuan Jiong 1988b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1989b55b76b2SDuan Jiong return rt; 1990b55b76b2SDuan Jiong }; 1991b55b76b2SDuan Jiong 1992b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1993b55b76b2SDuan Jiong const struct flowi6 *fl6, 1994b55b76b2SDuan Jiong const struct in6_addr *gateway) 1995b55b76b2SDuan Jiong { 1996b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1997b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1998b55b76b2SDuan Jiong 1999b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2000b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2001b55b76b2SDuan Jiong 2002b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2003b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2004b55b76b2SDuan Jiong } 2005b55b76b2SDuan Jiong 2006e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2007e2d118a1SLorenzo Colitti kuid_t uid) 20083a5ad2eeSDavid S. Miller { 20093a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 20103a5ad2eeSDavid S. Miller struct dst_entry *dst; 20113a5ad2eeSDavid S. Miller struct flowi6 fl6; 20123a5ad2eeSDavid S. Miller 20133a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2014e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 20153a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 20163a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 20173a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 20183a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 20196502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2020e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 20213a5ad2eeSDavid S. Miller 2022b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 20236700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 20243a5ad2eeSDavid S. Miller dst_release(dst); 20253a5ad2eeSDavid S. Miller } 20263a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 20273a5ad2eeSDavid S. Miller 2028c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2029c92a59ecSDuan Jiong u32 mark) 2030c92a59ecSDuan Jiong { 2031c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2032c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2033c92a59ecSDuan Jiong struct dst_entry *dst; 2034c92a59ecSDuan Jiong struct flowi6 fl6; 2035c92a59ecSDuan Jiong 2036c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2037e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2038c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2039c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2040c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2041c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2042e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2043c92a59ecSDuan Jiong 2044b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2045c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2046c92a59ecSDuan Jiong dst_release(dst); 2047c92a59ecSDuan Jiong } 2048c92a59ecSDuan Jiong 20493a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 20503a5ad2eeSDavid S. Miller { 2051e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2052e2d118a1SLorenzo Colitti sk->sk_uid); 20533a5ad2eeSDavid S. Miller } 20543a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 20553a5ad2eeSDavid S. Miller 20560dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 20571da177e4SLinus Torvalds { 20580dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 20590dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 20600dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 20610dbaee3bSDavid S. Miller 20621da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 20631da177e4SLinus Torvalds 20645578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 20655578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds /* 20681da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 20691da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 20701da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 20711da177e4SLinus Torvalds * rely only on pmtu discovery" 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 20741da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 20751da177e4SLinus Torvalds return mtu; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 2078ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2079d33e4553SDavid S. Miller { 20804b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 20814b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2082d33e4553SDavid S. Miller struct inet6_dev *idev; 2083618f9bc7SSteffen Klassert 2084618f9bc7SSteffen Klassert if (mtu) 208530f78d8eSEric Dumazet goto out; 2086618f9bc7SSteffen Klassert 20874b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 20884b32b5adSMartin KaFai Lau if (mtu) 20894b32b5adSMartin KaFai Lau goto out; 20904b32b5adSMartin KaFai Lau 2091618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2092d33e4553SDavid S. Miller 2093d33e4553SDavid S. Miller rcu_read_lock(); 2094d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2095d33e4553SDavid S. Miller if (idev) 2096d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2097d33e4553SDavid S. Miller rcu_read_unlock(); 2098d33e4553SDavid S. Miller 209930f78d8eSEric Dumazet out: 210014972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 210114972cbdSRoopa Prabhu 210214972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2103d33e4553SDavid S. Miller } 2104d33e4553SDavid S. Miller 21053b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 210687a11578SDavid S. Miller struct flowi6 *fl6) 21071da177e4SLinus Torvalds { 210887a11578SDavid S. Miller struct dst_entry *dst; 21091da177e4SLinus Torvalds struct rt6_info *rt; 21101da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2111c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 21121da177e4SLinus Torvalds 211338308473SDavid S. Miller if (unlikely(!idev)) 2114122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 21151da177e4SLinus Torvalds 2116ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 211738308473SDavid S. Miller if (unlikely(!rt)) { 21181da177e4SLinus Torvalds in6_dev_put(idev); 211987a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 21201da177e4SLinus Torvalds goto out; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 21238e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 21248e2ec639SYan, Zheng rt->dst.output = ip6_output; 2125550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 212687a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 21278e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 21288e2ec639SYan, Zheng rt->rt6i_idev = idev; 212914edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 21301da177e4SLinus Torvalds 2131587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2132587fea74SWei Wang * do proper release of the net_device 2133587fea74SWei Wang */ 2134587fea74SWei Wang rt6_uncached_list_add(rt); 21351da177e4SLinus Torvalds 213687a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 213787a11578SDavid S. Miller 21381da177e4SLinus Torvalds out: 213987a11578SDavid S. Miller return dst; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 2142569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 21431da177e4SLinus Torvalds { 214486393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 21457019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 21467019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 21477019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 21487019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 21497019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2150fc66f95cSEric Dumazet int entries; 21511da177e4SLinus Torvalds 2152fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 215349a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2154fc66f95cSEric Dumazet entries <= rt_max_size) 21551da177e4SLinus Torvalds goto out; 21561da177e4SLinus Torvalds 21576891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 215814956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2159fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2160fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 21617019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 21621da177e4SLinus Torvalds out: 21637019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2164fc66f95cSEric Dumazet return entries > rt_max_size; 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 2167e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2168e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2169e715b6d3SFlorian Westphal { 2170c3a8d947SDaniel Borkmann bool ecn_ca = false; 2171e715b6d3SFlorian Westphal struct nlattr *nla; 2172e715b6d3SFlorian Westphal int remaining; 2173e715b6d3SFlorian Westphal u32 *mp; 2174e715b6d3SFlorian Westphal 217563159f29SIan Morris if (!cfg->fc_mx) 2176e715b6d3SFlorian Westphal return 0; 2177e715b6d3SFlorian Westphal 2178e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2179e715b6d3SFlorian Westphal if (unlikely(!mp)) 2180e715b6d3SFlorian Westphal return -ENOMEM; 2181e715b6d3SFlorian Westphal 2182e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2183e715b6d3SFlorian Westphal int type = nla_type(nla); 2184ea697639SDaniel Borkmann u32 val; 2185ea697639SDaniel Borkmann 21861bb14807SDaniel Borkmann if (!type) 21871bb14807SDaniel Borkmann continue; 2188e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2189e715b6d3SFlorian Westphal goto err; 21901bb14807SDaniel Borkmann 2191ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2192ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2193e715b6d3SFlorian Westphal 2194ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 2195c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 2196ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2197ea697639SDaniel Borkmann goto err; 2198ea697639SDaniel Borkmann } else { 2199ea697639SDaniel Borkmann val = nla_get_u32(nla); 2200ea697639SDaniel Borkmann } 2201626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2202626abd59SPaolo Abeni val = 255; 2203b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2204b8d3e416SDaniel Borkmann goto err; 2205ea697639SDaniel Borkmann 2206ea697639SDaniel Borkmann mp[type - 1] = val; 2207e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2208e715b6d3SFlorian Westphal } 2209e715b6d3SFlorian Westphal 2210c3a8d947SDaniel Borkmann if (ecn_ca) { 2211c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2212c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2213c3a8d947SDaniel Borkmann } 2214e715b6d3SFlorian Westphal 2215c3a8d947SDaniel Borkmann mxc->mx = mp; 2216e715b6d3SFlorian Westphal return 0; 2217e715b6d3SFlorian Westphal err: 2218e715b6d3SFlorian Westphal kfree(mp); 2219e715b6d3SFlorian Westphal return -EINVAL; 2220e715b6d3SFlorian Westphal } 22211da177e4SLinus Torvalds 22228c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 22238c14586fSDavid Ahern struct fib6_config *cfg, 22248c14586fSDavid Ahern const struct in6_addr *gw_addr) 22258c14586fSDavid Ahern { 22268c14586fSDavid Ahern struct flowi6 fl6 = { 22278c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 22288c14586fSDavid Ahern .daddr = *gw_addr, 22298c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 22308c14586fSDavid Ahern }; 22318c14586fSDavid Ahern struct fib6_table *table; 22328c14586fSDavid Ahern struct rt6_info *rt; 2233d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 22348c14586fSDavid Ahern 22358c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 22368c14586fSDavid Ahern if (!table) 22378c14586fSDavid Ahern return NULL; 22388c14586fSDavid Ahern 22398c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 22408c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 22418c14586fSDavid Ahern 22428c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 22438c14586fSDavid Ahern 22448c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 22458c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 22468c14586fSDavid Ahern ip6_rt_put(rt); 22478c14586fSDavid Ahern rt = NULL; 22488c14586fSDavid Ahern } 22498c14586fSDavid Ahern 22508c14586fSDavid Ahern return rt; 22518c14586fSDavid Ahern } 22528c14586fSDavid Ahern 2253333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2254333c4301SDavid Ahern struct netlink_ext_ack *extack) 22551da177e4SLinus Torvalds { 22565578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 22571da177e4SLinus Torvalds struct rt6_info *rt = NULL; 22581da177e4SLinus Torvalds struct net_device *dev = NULL; 22591da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2260c71099acSThomas Graf struct fib6_table *table; 22611da177e4SLinus Torvalds int addr_type; 22628c5b83f0SRoopa Prabhu int err = -EINVAL; 22631da177e4SLinus Torvalds 2264557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2265d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2266d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2267557c44beSDavid Ahern goto out; 2268d5d531cbSDavid Ahern } 2269557c44beSDavid Ahern 2270d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2271d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 22728c5b83f0SRoopa Prabhu goto out; 2273d5d531cbSDavid Ahern } 2274d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2275d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2276d5d531cbSDavid Ahern goto out; 2277d5d531cbSDavid Ahern } 22781da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2279d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2280d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2281d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 22828c5b83f0SRoopa Prabhu goto out; 2283d5d531cbSDavid Ahern } 22841da177e4SLinus Torvalds #endif 228586872cb5SThomas Graf if (cfg->fc_ifindex) { 22861da177e4SLinus Torvalds err = -ENODEV; 22875578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 22881da177e4SLinus Torvalds if (!dev) 22891da177e4SLinus Torvalds goto out; 22901da177e4SLinus Torvalds idev = in6_dev_get(dev); 22911da177e4SLinus Torvalds if (!idev) 22921da177e4SLinus Torvalds goto out; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 229586872cb5SThomas Graf if (cfg->fc_metric == 0) 229686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 22971da177e4SLinus Torvalds 2298c71099acSThomas Graf err = -ENOBUFS; 229938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2300d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2301d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 230238308473SDavid S. Miller if (!table) { 2303f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2304d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2305d71314b4SMatti Vaittinen } 2306d71314b4SMatti Vaittinen } else { 2307d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2308d71314b4SMatti Vaittinen } 230938308473SDavid S. Miller 231038308473SDavid S. Miller if (!table) 2311c71099acSThomas Graf goto out; 2312c71099acSThomas Graf 2313ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2314ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 23151da177e4SLinus Torvalds 231638308473SDavid S. Miller if (!rt) { 23171da177e4SLinus Torvalds err = -ENOMEM; 23181da177e4SLinus Torvalds goto out; 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds 23211716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 23221716a961SGao feng rt6_set_expires(rt, jiffies + 23231716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 23241716a961SGao feng else 23251716a961SGao feng rt6_clean_expires(rt); 23261da177e4SLinus Torvalds 232786872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 232886872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 232986872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 233086872cb5SThomas Graf 233186872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 23321da177e4SLinus Torvalds 23331da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2334d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2335ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2336ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 23371da177e4SLinus Torvalds else 2338d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 23391da177e4SLinus Torvalds 2340d8d1f30bSChangli Gao rt->dst.output = ip6_output; 23411da177e4SLinus Torvalds 234219e42e45SRoopa Prabhu if (cfg->fc_encap) { 234319e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 234419e42e45SRoopa Prabhu 234530357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2346127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 23479ae28727SDavid Ahern &lwtstate, extack); 234819e42e45SRoopa Prabhu if (err) 234919e42e45SRoopa Prabhu goto out; 235061adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 235161adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 235261adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 235361adedf3SJiri Benc rt->dst.output = lwtunnel_output; 235419e42e45SRoopa Prabhu } 235561adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 235661adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 235761adedf3SJiri Benc rt->dst.input = lwtunnel_input; 235825368623STom Herbert } 235925368623STom Herbert } 236019e42e45SRoopa Prabhu 236186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 236286872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2363afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 236411d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 236786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 236886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 23691da177e4SLinus Torvalds #endif 23701da177e4SLinus Torvalds 237186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 23741da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 23751da177e4SLinus Torvalds */ 237686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 237738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 237838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 237938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 23801da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 23815578689aSDaniel Lezcano if (dev != net->loopback_dev) { 23821da177e4SLinus Torvalds if (dev) { 23831da177e4SLinus Torvalds dev_put(dev); 23841da177e4SLinus Torvalds in6_dev_put(idev); 23851da177e4SLinus Torvalds } 23865578689aSDaniel Lezcano dev = net->loopback_dev; 23871da177e4SLinus Torvalds dev_hold(dev); 23881da177e4SLinus Torvalds idev = in6_dev_get(dev); 23891da177e4SLinus Torvalds if (!idev) { 23901da177e4SLinus Torvalds err = -ENODEV; 23911da177e4SLinus Torvalds goto out; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2395ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2396ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2397ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2398ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 23997150aedeSKamala R rt->dst.input = dst_discard; 2400ef2c7d7bSNicolas Dichtel break; 2401ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2402ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 24037150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 24047150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2405ef2c7d7bSNicolas Dichtel break; 2406b4949ab2SNicolas Dichtel case RTN_THROW: 24070315e382SNikola Forró case RTN_UNREACHABLE: 2408ef2c7d7bSNicolas Dichtel default: 24097150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 24100315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 24110315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 24127150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 24137150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2414ef2c7d7bSNicolas Dichtel break; 2415ef2c7d7bSNicolas Dichtel } 24161da177e4SLinus Torvalds goto install_route; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 241986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2420b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 24211da177e4SLinus Torvalds int gwa_type; 24221da177e4SLinus Torvalds 242386872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2424330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 242548ed7b26SFlorian Westphal 242648ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 242748ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 242848ed7b26SFlorian Westphal * will return already-added prefix route via interface that 242948ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 243048ed7b26SFlorian Westphal */ 243148ed7b26SFlorian Westphal err = -EINVAL; 2432330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2433330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2434d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2435d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 243648ed7b26SFlorian Westphal goto out; 2437d5d531cbSDavid Ahern } 24384e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 24418c14586fSDavid Ahern struct rt6_info *grt = NULL; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 24441da177e4SLinus Torvalds addresses as nexthop address. 24451da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 24461da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 24471da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 24481da177e4SLinus Torvalds some exceptions. --ANK 244996d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 245096d5822cSErik Nordmark addressing 24511da177e4SLinus Torvalds */ 245296d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2453d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2454d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2455d5d531cbSDavid Ahern "Invalid gateway address"); 24561da177e4SLinus Torvalds goto out; 2457d5d531cbSDavid Ahern } 24581da177e4SLinus Torvalds 2459a435a07fSVincent Bernat if (cfg->fc_table) { 24608c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 24618c14586fSDavid Ahern 2462a435a07fSVincent Bernat if (grt) { 2463a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2464a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2465a435a07fSVincent Bernat ip6_rt_put(grt); 2466a435a07fSVincent Bernat grt = NULL; 2467a435a07fSVincent Bernat } 2468a435a07fSVincent Bernat } 2469a435a07fSVincent Bernat } 2470a435a07fSVincent Bernat 24718c14586fSDavid Ahern if (!grt) 24728c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 24738c14586fSDavid Ahern cfg->fc_ifindex, 1); 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds err = -EHOSTUNREACH; 247638308473SDavid S. Miller if (!grt) 24771da177e4SLinus Torvalds goto out; 24781da177e4SLinus Torvalds if (dev) { 2479d1918542SDavid S. Miller if (dev != grt->dst.dev) { 248094e187c0SAmerigo Wang ip6_rt_put(grt); 24811da177e4SLinus Torvalds goto out; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds } else { 2484d1918542SDavid S. Miller dev = grt->dst.dev; 24851da177e4SLinus Torvalds idev = grt->rt6i_idev; 24861da177e4SLinus Torvalds dev_hold(dev); 24871da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 24901da177e4SLinus Torvalds err = 0; 249194e187c0SAmerigo Wang ip6_rt_put(grt); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds if (err) 24941da177e4SLinus Torvalds goto out; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds err = -EINVAL; 2497d5d531cbSDavid Ahern if (!dev) { 2498d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 24991da177e4SLinus Torvalds goto out; 2500d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2501d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2502d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2503d5d531cbSDavid Ahern goto out; 2504d5d531cbSDavid Ahern } 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds err = -ENODEV; 250838308473SDavid S. Miller if (!dev) 25091da177e4SLinus Torvalds goto out; 25101da177e4SLinus Torvalds 2511c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2512c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2513d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2514c3968a85SDaniel Walter err = -EINVAL; 2515c3968a85SDaniel Walter goto out; 2516c3968a85SDaniel Walter } 25174e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2518c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2519c3968a85SDaniel Walter } else 2520c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2521c3968a85SDaniel Walter 252286872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds install_route: 2525d8d1f30bSChangli Gao rt->dst.dev = dev; 25261da177e4SLinus Torvalds rt->rt6i_idev = idev; 2527c71099acSThomas Graf rt->rt6i_table = table; 252863152fc0SDaniel Lezcano 2529c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 253063152fc0SDaniel Lezcano 25318c5b83f0SRoopa Prabhu return rt; 25321da177e4SLinus Torvalds out: 25331da177e4SLinus Torvalds if (dev) 25341da177e4SLinus Torvalds dev_put(dev); 25351da177e4SLinus Torvalds if (idev) 25361da177e4SLinus Torvalds in6_dev_put(idev); 2537587fea74SWei Wang if (rt) 2538587fea74SWei Wang dst_release_immediate(&rt->dst); 25396b9ea5a6SRoopa Prabhu 25408c5b83f0SRoopa Prabhu return ERR_PTR(err); 25416b9ea5a6SRoopa Prabhu } 25426b9ea5a6SRoopa Prabhu 2543333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2544333c4301SDavid Ahern struct netlink_ext_ack *extack) 25456b9ea5a6SRoopa Prabhu { 25466b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 25478c5b83f0SRoopa Prabhu struct rt6_info *rt; 25486b9ea5a6SRoopa Prabhu int err; 25496b9ea5a6SRoopa Prabhu 2550333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 25518c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 25528c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 25538c5b83f0SRoopa Prabhu rt = NULL; 25546b9ea5a6SRoopa Prabhu goto out; 25558c5b83f0SRoopa Prabhu } 25566b9ea5a6SRoopa Prabhu 25576b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 25586b9ea5a6SRoopa Prabhu if (err) 25596b9ea5a6SRoopa Prabhu goto out; 25606b9ea5a6SRoopa Prabhu 2561333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 25626b9ea5a6SRoopa Prabhu 25636b9ea5a6SRoopa Prabhu kfree(mxc.mx); 25646b9ea5a6SRoopa Prabhu 25656b9ea5a6SRoopa Prabhu return err; 25666b9ea5a6SRoopa Prabhu out: 2567587fea74SWei Wang if (rt) 2568587fea74SWei Wang dst_release_immediate(&rt->dst); 25696b9ea5a6SRoopa Prabhu 25701da177e4SLinus Torvalds return err; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds 257386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 25741da177e4SLinus Torvalds { 25751da177e4SLinus Torvalds int err; 2576c71099acSThomas Graf struct fib6_table *table; 2577d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 25781da177e4SLinus Torvalds 2579a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 25806825a26cSGao feng err = -ENOENT; 25816825a26cSGao feng goto out; 25826825a26cSGao feng } 25836c813a72SPatrick McHardy 2584c71099acSThomas Graf table = rt->rt6i_table; 2585c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 258686872cb5SThomas Graf err = fib6_del(rt, info); 2587c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 25881da177e4SLinus Torvalds 25896825a26cSGao feng out: 259094e187c0SAmerigo Wang ip6_rt_put(rt); 25911da177e4SLinus Torvalds return err; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 2594e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2595e0a1ad73SThomas Graf { 25964d1169c1SDenis V. Lunev struct nl_info info = { 2597d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 25984d1169c1SDenis V. Lunev }; 2599528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2600e0a1ad73SThomas Graf } 2601e0a1ad73SThomas Graf 26020ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 26030ae81335SDavid Ahern { 26040ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2605e3330039SWANG Cong struct net *net = info->nl_net; 260616a16cd3SDavid Ahern struct sk_buff *skb = NULL; 26070ae81335SDavid Ahern struct fib6_table *table; 2608e3330039SWANG Cong int err = -ENOENT; 26090ae81335SDavid Ahern 2610e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2611e3330039SWANG Cong goto out_put; 26120ae81335SDavid Ahern table = rt->rt6i_table; 26130ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 26140ae81335SDavid Ahern 26150ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 26160ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 26170ae81335SDavid Ahern 261816a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 261916a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 262016a16cd3SDavid Ahern if (skb) { 262116a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 262216a16cd3SDavid Ahern 2623e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 262416a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 262516a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 262616a16cd3SDavid Ahern kfree_skb(skb); 262716a16cd3SDavid Ahern skb = NULL; 262816a16cd3SDavid Ahern } else 262916a16cd3SDavid Ahern info->skip_notify = 1; 263016a16cd3SDavid Ahern } 263116a16cd3SDavid Ahern 26320ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 26330ae81335SDavid Ahern &rt->rt6i_siblings, 26340ae81335SDavid Ahern rt6i_siblings) { 26350ae81335SDavid Ahern err = fib6_del(sibling, info); 26360ae81335SDavid Ahern if (err) 2637e3330039SWANG Cong goto out_unlock; 26380ae81335SDavid Ahern } 26390ae81335SDavid Ahern } 26400ae81335SDavid Ahern 26410ae81335SDavid Ahern err = fib6_del(rt, info); 2642e3330039SWANG Cong out_unlock: 26430ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2644e3330039SWANG Cong out_put: 26450ae81335SDavid Ahern ip6_rt_put(rt); 264616a16cd3SDavid Ahern 264716a16cd3SDavid Ahern if (skb) { 2648e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 264916a16cd3SDavid Ahern info->nlh, gfp_any()); 265016a16cd3SDavid Ahern } 26510ae81335SDavid Ahern return err; 26520ae81335SDavid Ahern } 26530ae81335SDavid Ahern 2654333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2655333c4301SDavid Ahern struct netlink_ext_ack *extack) 26561da177e4SLinus Torvalds { 2657c71099acSThomas Graf struct fib6_table *table; 26581da177e4SLinus Torvalds struct fib6_node *fn; 26591da177e4SLinus Torvalds struct rt6_info *rt; 26601da177e4SLinus Torvalds int err = -ESRCH; 26611da177e4SLinus Torvalds 26625578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2663d5d531cbSDavid Ahern if (!table) { 2664d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2665c71099acSThomas Graf return err; 2666d5d531cbSDavid Ahern } 26671da177e4SLinus Torvalds 2668c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2669c71099acSThomas Graf 2670c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 267186872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 267286872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds if (fn) { 2675d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 26761f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 26771f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 26781f56a01fSMartin KaFai Lau continue; 267986872cb5SThomas Graf if (cfg->fc_ifindex && 2680d1918542SDavid S. Miller (!rt->dst.dev || 2681d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 26821da177e4SLinus Torvalds continue; 268386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 268486872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 26851da177e4SLinus Torvalds continue; 268686872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 26871da177e4SLinus Torvalds continue; 2688c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2689c2ed1880SMantas M continue; 2690d8d1f30bSChangli Gao dst_hold(&rt->dst); 2691c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 26921da177e4SLinus Torvalds 26930ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 26940ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 269586872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 26960ae81335SDavid Ahern 26970ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds } 2700c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds return err; 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 27056700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2706a6279458SYOSHIFUJI Hideaki { 2707a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2708e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2709e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2710e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2711e8599ff4SDavid S. Miller struct neighbour *neigh; 271271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 27136e157b6aSDavid S. Miller int optlen, on_link; 27146e157b6aSDavid S. Miller u8 *lladdr; 2715e8599ff4SDavid S. Miller 271629a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 271771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2718e8599ff4SDavid S. Miller 2719e8599ff4SDavid S. Miller if (optlen < 0) { 27206e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2721e8599ff4SDavid S. Miller return; 2722e8599ff4SDavid S. Miller } 2723e8599ff4SDavid S. Miller 272471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2725e8599ff4SDavid S. Miller 272671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 27276e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2728e8599ff4SDavid S. Miller return; 2729e8599ff4SDavid S. Miller } 2730e8599ff4SDavid S. Miller 27316e157b6aSDavid S. Miller on_link = 0; 273271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2733e8599ff4SDavid S. Miller on_link = 1; 273471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2735e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 27366e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2737e8599ff4SDavid S. Miller return; 2738e8599ff4SDavid S. Miller } 2739e8599ff4SDavid S. Miller 2740e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2741e8599ff4SDavid S. Miller if (!in6_dev) 2742e8599ff4SDavid S. Miller return; 2743e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2744e8599ff4SDavid S. Miller return; 2745e8599ff4SDavid S. Miller 2746e8599ff4SDavid S. Miller /* RFC2461 8.1: 2747e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2748e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2749e8599ff4SDavid S. Miller */ 2750e8599ff4SDavid S. Miller 2751f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2752e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2753e8599ff4SDavid S. Miller return; 2754e8599ff4SDavid S. Miller } 27556e157b6aSDavid S. Miller 27566e157b6aSDavid S. Miller lladdr = NULL; 2757e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2758e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2759e8599ff4SDavid S. Miller skb->dev); 2760e8599ff4SDavid S. Miller if (!lladdr) { 2761e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2762e8599ff4SDavid S. Miller return; 2763e8599ff4SDavid S. Miller } 2764e8599ff4SDavid S. Miller } 2765e8599ff4SDavid S. Miller 27666e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2767ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 27686e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 27696e157b6aSDavid S. Miller return; 27706e157b6aSDavid S. Miller } 27716e157b6aSDavid S. Miller 27726e157b6aSDavid S. Miller /* Redirect received -> path was valid. 27736e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 27746e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 27756e157b6aSDavid S. Miller */ 27760dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 27776e157b6aSDavid S. Miller 277871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2779e8599ff4SDavid S. Miller if (!neigh) 2780e8599ff4SDavid S. Miller return; 2781e8599ff4SDavid S. Miller 27821da177e4SLinus Torvalds /* 27831da177e4SLinus Torvalds * We have finally decided to accept it. 27841da177e4SLinus Torvalds */ 27851da177e4SLinus Torvalds 2786f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 27871da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 27881da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 27891da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2790f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2791f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 27921da177e4SLinus Torvalds 279383a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 279438308473SDavid S. Miller if (!nrt) 27951da177e4SLinus Torvalds goto out; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 27981da177e4SLinus Torvalds if (on_link) 27991da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 28001da177e4SLinus Torvalds 2801b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 28024e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 28031da177e4SLinus Torvalds 280440e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 28051cfb71eeSWei Wang goto out_release; 28061da177e4SLinus Torvalds 2807d8d1f30bSChangli Gao netevent.old = &rt->dst; 2808d8d1f30bSChangli Gao netevent.new = &nrt->dst; 280971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 281060592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 28118d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 28128d71740cSTom Tucker 28131da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 28146e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2815e0a1ad73SThomas Graf ip6_del_rt(rt); 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28181cfb71eeSWei Wang out_release: 28191cfb71eeSWei Wang /* Release the reference taken in 28201cfb71eeSWei Wang * ip6_rt_cache_alloc() 28211cfb71eeSWei Wang */ 28221cfb71eeSWei Wang dst_release(&nrt->dst); 28231cfb71eeSWei Wang 28241da177e4SLinus Torvalds out: 2825e8599ff4SDavid S. Miller neigh_release(neigh); 28266e157b6aSDavid S. Miller } 28276e157b6aSDavid S. Miller 28281da177e4SLinus Torvalds /* 28291da177e4SLinus Torvalds * Misc support functions 28301da177e4SLinus Torvalds */ 28311da177e4SLinus Torvalds 28324b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 28334b32b5adSMartin KaFai Lau { 28344b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 28354b32b5adSMartin KaFai Lau 28364b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 28374b32b5adSMartin KaFai Lau dst_hold(&from->dst); 28384b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 28394b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 28404b32b5adSMartin KaFai Lau } 28414b32b5adSMartin KaFai Lau 284283a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 28431da177e4SLinus Torvalds { 2844d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2845d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 284683a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2847d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 28481da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 28491da177e4SLinus Torvalds if (rt->rt6i_idev) 28501da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2851d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 28524e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 28531716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 28541716a961SGao feng rt6_set_from(rt, ort); 285583a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 28561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 285783a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 28581da177e4SLinus Torvalds #endif 285983a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2860c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 286161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 286470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2865efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2866b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2867830218c1SDavid Ahern const struct in6_addr *gwaddr, 2868830218c1SDavid Ahern struct net_device *dev) 286970ceb4f5SYOSHIFUJI Hideaki { 2870830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2871830218c1SDavid Ahern int ifindex = dev->ifindex; 287270ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 287370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2874c71099acSThomas Graf struct fib6_table *table; 287570ceb4f5SYOSHIFUJI Hideaki 2876830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 287738308473SDavid S. Miller if (!table) 2878c71099acSThomas Graf return NULL; 2879c71099acSThomas Graf 28805744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2881c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 288270ceb4f5SYOSHIFUJI Hideaki if (!fn) 288370ceb4f5SYOSHIFUJI Hideaki goto out; 288470ceb4f5SYOSHIFUJI Hideaki 2885d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2886d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 288770ceb4f5SYOSHIFUJI Hideaki continue; 288870ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 288970ceb4f5SYOSHIFUJI Hideaki continue; 289070ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 289170ceb4f5SYOSHIFUJI Hideaki continue; 2892d8d1f30bSChangli Gao dst_hold(&rt->dst); 289370ceb4f5SYOSHIFUJI Hideaki break; 289470ceb4f5SYOSHIFUJI Hideaki } 289570ceb4f5SYOSHIFUJI Hideaki out: 28965744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 289770ceb4f5SYOSHIFUJI Hideaki return rt; 289870ceb4f5SYOSHIFUJI Hideaki } 289970ceb4f5SYOSHIFUJI Hideaki 2900efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2901b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2902830218c1SDavid Ahern const struct in6_addr *gwaddr, 2903830218c1SDavid Ahern struct net_device *dev, 290495c96174SEric Dumazet unsigned int pref) 290570ceb4f5SYOSHIFUJI Hideaki { 290686872cb5SThomas Graf struct fib6_config cfg = { 2907238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2908830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 290986872cb5SThomas Graf .fc_dst_len = prefixlen, 291086872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 291186872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 2912b91d5329SXin Long .fc_protocol = RTPROT_RA, 291315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2914efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2915efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 291686872cb5SThomas Graf }; 291770ceb4f5SYOSHIFUJI Hideaki 2918830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 29194e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 29204e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 292186872cb5SThomas Graf 2922e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2923e317da96SYOSHIFUJI Hideaki if (!prefixlen) 292486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 292570ceb4f5SYOSHIFUJI Hideaki 2926333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 292770ceb4f5SYOSHIFUJI Hideaki 2928830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 292970ceb4f5SYOSHIFUJI Hideaki } 293070ceb4f5SYOSHIFUJI Hideaki #endif 293170ceb4f5SYOSHIFUJI Hideaki 2932b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 29331da177e4SLinus Torvalds { 2934830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 29351da177e4SLinus Torvalds struct rt6_info *rt; 2936c71099acSThomas Graf struct fib6_table *table; 29371da177e4SLinus Torvalds 2938830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 293938308473SDavid S. Miller if (!table) 2940c71099acSThomas Graf return NULL; 29411da177e4SLinus Torvalds 29425744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2943d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2944d1918542SDavid S. Miller if (dev == rt->dst.dev && 2945045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 29461da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 29471da177e4SLinus Torvalds break; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds if (rt) 2950d8d1f30bSChangli Gao dst_hold(&rt->dst); 29515744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 29521da177e4SLinus Torvalds return rt; 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds 2955b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2956ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2957ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 29581da177e4SLinus Torvalds { 295986872cb5SThomas Graf struct fib6_config cfg = { 2960ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2961238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 296286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 296386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 296486872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 2965b91d5329SXin Long .fc_protocol = RTPROT_RA, 296615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 29675578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2968c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 296986872cb5SThomas Graf }; 29701da177e4SLinus Torvalds 29714e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 29721da177e4SLinus Torvalds 2973333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2974830218c1SDavid Ahern struct fib6_table *table; 2975830218c1SDavid Ahern 2976830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2977830218c1SDavid Ahern if (table) 2978830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2979830218c1SDavid Ahern } 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 2984830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 29851da177e4SLinus Torvalds { 29861da177e4SLinus Torvalds struct rt6_info *rt; 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds restart: 2989c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2990d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 29913e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 29923e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2993d8d1f30bSChangli Gao dst_hold(&rt->dst); 2994c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2995e0a1ad73SThomas Graf ip6_del_rt(rt); 29961da177e4SLinus Torvalds goto restart; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds } 2999c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3000830218c1SDavid Ahern 3001830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3002830218c1SDavid Ahern } 3003830218c1SDavid Ahern 3004830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3005830218c1SDavid Ahern { 3006830218c1SDavid Ahern struct fib6_table *table; 3007830218c1SDavid Ahern struct hlist_head *head; 3008830218c1SDavid Ahern unsigned int h; 3009830218c1SDavid Ahern 3010830218c1SDavid Ahern rcu_read_lock(); 3011830218c1SDavid Ahern 3012830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3013830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3014830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3015830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3016830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3017830218c1SDavid Ahern } 3018830218c1SDavid Ahern } 3019830218c1SDavid Ahern 3020830218c1SDavid Ahern rcu_read_unlock(); 30211da177e4SLinus Torvalds } 30221da177e4SLinus Torvalds 30235578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 30245578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 302586872cb5SThomas Graf struct fib6_config *cfg) 302686872cb5SThomas Graf { 302786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 302886872cb5SThomas Graf 3029ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3030ca254490SDavid Ahern : RT6_TABLE_MAIN; 303186872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 303286872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 303386872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 303486872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 303586872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 303686872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 303786872cb5SThomas Graf 30385578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3039f1243c2dSBenjamin Thery 30404e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 30414e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 30424e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 304386872cb5SThomas Graf } 304486872cb5SThomas Graf 30455578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 30461da177e4SLinus Torvalds { 304786872cb5SThomas Graf struct fib6_config cfg; 30481da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 30491da177e4SLinus Torvalds int err; 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds switch (cmd) { 30521da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 30531da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3054af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30551da177e4SLinus Torvalds return -EPERM; 30561da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 30571da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 30581da177e4SLinus Torvalds if (err) 30591da177e4SLinus Torvalds return -EFAULT; 30601da177e4SLinus Torvalds 30615578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 306286872cb5SThomas Graf 30631da177e4SLinus Torvalds rtnl_lock(); 30641da177e4SLinus Torvalds switch (cmd) { 30651da177e4SLinus Torvalds case SIOCADDRT: 3066333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 30671da177e4SLinus Torvalds break; 30681da177e4SLinus Torvalds case SIOCDELRT: 3069333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 30701da177e4SLinus Torvalds break; 30711da177e4SLinus Torvalds default: 30721da177e4SLinus Torvalds err = -EINVAL; 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds rtnl_unlock(); 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds return err; 30773ff50b79SStephen Hemminger } 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds return -EINVAL; 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30821da177e4SLinus Torvalds /* 30831da177e4SLinus Torvalds * Drop the packet on the floor 30841da177e4SLinus Torvalds */ 30851da177e4SLinus Torvalds 3086d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 30871da177e4SLinus Torvalds { 3088612f09e8SYOSHIFUJI Hideaki int type; 3089adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3090612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3091612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 30920660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 309345bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 30943bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 30953bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3096612f09e8SYOSHIFUJI Hideaki break; 3097612f09e8SYOSHIFUJI Hideaki } 3098612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3099612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 31003bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 31013bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3102612f09e8SYOSHIFUJI Hideaki break; 3103612f09e8SYOSHIFUJI Hideaki } 31043ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 31051da177e4SLinus Torvalds kfree_skb(skb); 31061da177e4SLinus Torvalds return 0; 31071da177e4SLinus Torvalds } 31081da177e4SLinus Torvalds 31099ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 31109ce8ade0SThomas Graf { 3111612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 31129ce8ade0SThomas Graf } 31139ce8ade0SThomas Graf 3114ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 31151da177e4SLinus Torvalds { 3116adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3117612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 31181da177e4SLinus Torvalds } 31191da177e4SLinus Torvalds 31209ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 31219ce8ade0SThomas Graf { 3122612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 31239ce8ade0SThomas Graf } 31249ce8ade0SThomas Graf 3125ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 31269ce8ade0SThomas Graf { 3127adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3128612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 31299ce8ade0SThomas Graf } 31309ce8ade0SThomas Graf 31311da177e4SLinus Torvalds /* 31321da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 31331da177e4SLinus Torvalds */ 31341da177e4SLinus Torvalds 31351da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 31361da177e4SLinus Torvalds const struct in6_addr *addr, 31378f031519SDavid S. Miller bool anycast) 31381da177e4SLinus Torvalds { 3139ca254490SDavid Ahern u32 tb_id; 3140c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 31414832c30dSDavid Ahern struct net_device *dev = idev->dev; 31425f02ce24SDavid Ahern struct rt6_info *rt; 31435f02ce24SDavid Ahern 31445f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3145a3300ef4SHannes Frederic Sowa if (!rt) 31461da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds in6_dev_hold(idev); 31491da177e4SLinus Torvalds 315011d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3151d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3152d8d1f30bSChangli Gao rt->dst.output = ip6_output; 31531da177e4SLinus Torvalds rt->rt6i_idev = idev; 31541da177e4SLinus Torvalds 315594b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 31561da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 315758c4fb86SYOSHIFUJI Hideaki if (anycast) 315858c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 315958c4fb86SYOSHIFUJI Hideaki else 31601da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 31611da177e4SLinus Torvalds 3162550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 31634e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 31641da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3165ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3166ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 31671da177e4SLinus Torvalds 31681da177e4SLinus Torvalds return rt; 31691da177e4SLinus Torvalds } 31701da177e4SLinus Torvalds 3171c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3172c3968a85SDaniel Walter struct arg_dev_net_ip { 3173c3968a85SDaniel Walter struct net_device *dev; 3174c3968a85SDaniel Walter struct net *net; 3175c3968a85SDaniel Walter struct in6_addr *addr; 3176c3968a85SDaniel Walter }; 3177c3968a85SDaniel Walter 3178c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3179c3968a85SDaniel Walter { 3180c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3181c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3182c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3183c3968a85SDaniel Walter 3184d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3185c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3186c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 3187*60006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3188c3968a85SDaniel Walter /* remove prefsrc entry */ 3189c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 3190*60006a48SWei Wang /* need to update cache as well */ 3191*60006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 3192*60006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3193c3968a85SDaniel Walter } 3194c3968a85SDaniel Walter return 0; 3195c3968a85SDaniel Walter } 3196c3968a85SDaniel Walter 3197c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3198c3968a85SDaniel Walter { 3199c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3200c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3201c3968a85SDaniel Walter .dev = ifp->idev->dev, 3202c3968a85SDaniel Walter .net = net, 3203c3968a85SDaniel Walter .addr = &ifp->addr, 3204c3968a85SDaniel Walter }; 32050c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3206c3968a85SDaniel Walter } 3207c3968a85SDaniel Walter 3208be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3209be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 3210be7a010dSDuan Jiong 3211be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3212be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3213be7a010dSDuan Jiong { 3214be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3215be7a010dSDuan Jiong 3216be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 3217be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 3218be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3219be7a010dSDuan Jiong return -1; 3220be7a010dSDuan Jiong } 3221be7a010dSDuan Jiong return 0; 3222be7a010dSDuan Jiong } 3223be7a010dSDuan Jiong 3224be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3225be7a010dSDuan Jiong { 3226be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3227be7a010dSDuan Jiong } 3228be7a010dSDuan Jiong 32298ed67789SDaniel Lezcano struct arg_dev_net { 32308ed67789SDaniel Lezcano struct net_device *dev; 32318ed67789SDaniel Lezcano struct net *net; 32328ed67789SDaniel Lezcano }; 32338ed67789SDaniel Lezcano 3234a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 32351da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 32361da177e4SLinus Torvalds { 3237bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3238bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 32398ed67789SDaniel Lezcano 3240d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3241a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3242a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 32438397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3244a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 32451da177e4SLinus Torvalds return -1; 3246c159d30cSDavid S. Miller 32471da177e4SLinus Torvalds return 0; 32481da177e4SLinus Torvalds } 32491da177e4SLinus Torvalds 3250f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 32511da177e4SLinus Torvalds { 32528ed67789SDaniel Lezcano struct arg_dev_net adn = { 32538ed67789SDaniel Lezcano .dev = dev, 32548ed67789SDaniel Lezcano .net = net, 32558ed67789SDaniel Lezcano }; 32568ed67789SDaniel Lezcano 32570c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3258e332bc67SEric W. Biederman if (dev) 32598d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 32601da177e4SLinus Torvalds } 32611da177e4SLinus Torvalds 326295c96174SEric Dumazet struct rt6_mtu_change_arg { 32631da177e4SLinus Torvalds struct net_device *dev; 326495c96174SEric Dumazet unsigned int mtu; 32651da177e4SLinus Torvalds }; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 32681da177e4SLinus Torvalds { 32691da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 32701da177e4SLinus Torvalds struct inet6_dev *idev; 32711da177e4SLinus Torvalds 32721da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 32731da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 32741da177e4SLinus Torvalds We still use this lock to block changes 32751da177e4SLinus Torvalds caused by addrconf/ndisc. 32761da177e4SLinus Torvalds */ 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 327938308473SDavid S. Miller if (!idev) 32801da177e4SLinus Torvalds return 0; 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 32831da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 32841da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 32851da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 32861da177e4SLinus Torvalds */ 32871da177e4SLinus Torvalds /* 32881da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 32891da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 32901da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 32911da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 32921da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 32931da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 329467c408cfSAlexander Alemayhu PMTU discovery. 32951da177e4SLinus Torvalds */ 3296d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3297fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 32984b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 32994b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 33004b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 33014b32b5adSMartin KaFai Lau * (i.e. a redirected route), 33024b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 33034b32b5adSMartin KaFai Lau * been updated. 33044b32b5adSMartin KaFai Lau */ 33054b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 33064b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 33074b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 3308d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 33094b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3310defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3311566cfd8fSSimon Arlott } 33124b32b5adSMartin KaFai Lau } 33131da177e4SLinus Torvalds return 0; 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds 331695c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 33171da177e4SLinus Torvalds { 3318c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3319c71099acSThomas Graf .dev = dev, 3320c71099acSThomas Graf .mtu = mtu, 3321c71099acSThomas Graf }; 33221da177e4SLinus Torvalds 33230c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 3326ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 33275176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 332886872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3329ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 333086872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 333186872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 333251ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3333c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 333419e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 333519e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 333632bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3337622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 33383b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 333986872cb5SThomas Graf }; 334086872cb5SThomas Graf 334186872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3342333c4301SDavid Ahern struct fib6_config *cfg, 3343333c4301SDavid Ahern struct netlink_ext_ack *extack) 33441da177e4SLinus Torvalds { 334586872cb5SThomas Graf struct rtmsg *rtm; 334686872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3347c78ba6d6SLubomir Rintel unsigned int pref; 334886872cb5SThomas Graf int err; 33491da177e4SLinus Torvalds 3350fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3351fceb6435SJohannes Berg NULL); 335286872cb5SThomas Graf if (err < 0) 335386872cb5SThomas Graf goto errout; 33541da177e4SLinus Torvalds 335586872cb5SThomas Graf err = -EINVAL; 335686872cb5SThomas Graf rtm = nlmsg_data(nlh); 335786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 335886872cb5SThomas Graf 335986872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 336086872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 336186872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 336286872cb5SThomas Graf cfg->fc_flags = RTF_UP; 336386872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3364ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 336586872cb5SThomas Graf 3366ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3367ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3368b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3369b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 337086872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 337186872cb5SThomas Graf 3372ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3373ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3374ab79ad14SMaciej Żenczykowski 33751f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 33761f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 33771f56a01fSMartin KaFai Lau 337815e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 337986872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 33803b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 338186872cb5SThomas Graf 338286872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 338367b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 338486872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 33851da177e4SLinus Torvalds } 338686872cb5SThomas Graf 338786872cb5SThomas Graf if (tb[RTA_DST]) { 338886872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 338986872cb5SThomas Graf 339086872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 339186872cb5SThomas Graf goto errout; 339286872cb5SThomas Graf 339386872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 33941da177e4SLinus Torvalds } 339586872cb5SThomas Graf 339686872cb5SThomas Graf if (tb[RTA_SRC]) { 339786872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 339886872cb5SThomas Graf 339986872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 340086872cb5SThomas Graf goto errout; 340186872cb5SThomas Graf 340286872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 34031da177e4SLinus Torvalds } 340486872cb5SThomas Graf 3405c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 340667b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3407c3968a85SDaniel Walter 340886872cb5SThomas Graf if (tb[RTA_OIF]) 340986872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 341086872cb5SThomas Graf 341186872cb5SThomas Graf if (tb[RTA_PRIORITY]) 341286872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 341386872cb5SThomas Graf 341486872cb5SThomas Graf if (tb[RTA_METRICS]) { 341586872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 341686872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 34171da177e4SLinus Torvalds } 341886872cb5SThomas Graf 341986872cb5SThomas Graf if (tb[RTA_TABLE]) 342086872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 342186872cb5SThomas Graf 342251ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 342351ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 342451ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 34259ed59592SDavid Ahern 34269ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3427c255bd68SDavid Ahern cfg->fc_mp_len, extack); 34289ed59592SDavid Ahern if (err < 0) 34299ed59592SDavid Ahern goto errout; 343051ebd318SNicolas Dichtel } 343151ebd318SNicolas Dichtel 3432c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3433c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3434c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3435c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3436c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3437c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3438c78ba6d6SLubomir Rintel } 3439c78ba6d6SLubomir Rintel 344019e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 344119e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 344219e42e45SRoopa Prabhu 34439ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 344419e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 344519e42e45SRoopa Prabhu 3446c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 34479ed59592SDavid Ahern if (err < 0) 34489ed59592SDavid Ahern goto errout; 34499ed59592SDavid Ahern } 34509ed59592SDavid Ahern 345132bc201eSXin Long if (tb[RTA_EXPIRES]) { 345232bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 345332bc201eSXin Long 345432bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 345532bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 345632bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 345732bc201eSXin Long } 345832bc201eSXin Long } 345932bc201eSXin Long 346086872cb5SThomas Graf err = 0; 346186872cb5SThomas Graf errout: 346286872cb5SThomas Graf return err; 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds 34656b9ea5a6SRoopa Prabhu struct rt6_nh { 34666b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 34676b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 34686b9ea5a6SRoopa Prabhu struct mx6_config mxc; 34696b9ea5a6SRoopa Prabhu struct list_head next; 34706b9ea5a6SRoopa Prabhu }; 34716b9ea5a6SRoopa Prabhu 34726b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 34736b9ea5a6SRoopa Prabhu { 34746b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 34756b9ea5a6SRoopa Prabhu 34766b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 34777d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 34786b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 34796b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 34806b9ea5a6SRoopa Prabhu } 34816b9ea5a6SRoopa Prabhu } 34826b9ea5a6SRoopa Prabhu 34836b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 34846b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 34856b9ea5a6SRoopa Prabhu { 34866b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 34876b9ea5a6SRoopa Prabhu int err = -EEXIST; 34886b9ea5a6SRoopa Prabhu 34896b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 34906b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3491f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 34926b9ea5a6SRoopa Prabhu return err; 34936b9ea5a6SRoopa Prabhu } 34946b9ea5a6SRoopa Prabhu 34956b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 34966b9ea5a6SRoopa Prabhu if (!nh) 34976b9ea5a6SRoopa Prabhu return -ENOMEM; 34986b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 34996b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 35006b9ea5a6SRoopa Prabhu if (err) { 35016b9ea5a6SRoopa Prabhu kfree(nh); 35026b9ea5a6SRoopa Prabhu return err; 35036b9ea5a6SRoopa Prabhu } 35046b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 35056b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 35066b9ea5a6SRoopa Prabhu 35076b9ea5a6SRoopa Prabhu return 0; 35086b9ea5a6SRoopa Prabhu } 35096b9ea5a6SRoopa Prabhu 35103b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 35113b1137feSDavid Ahern struct rt6_info *rt_last, 35123b1137feSDavid Ahern struct nl_info *info, 35133b1137feSDavid Ahern __u16 nlflags) 35143b1137feSDavid Ahern { 35153b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 35163b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 35173b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 35183b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 35193b1137feSDavid Ahern * the list, find the first sibling of the last route appended 35203b1137feSDavid Ahern */ 35213b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 35223b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 35233b1137feSDavid Ahern struct rt6_info, 35243b1137feSDavid Ahern rt6i_siblings); 35253b1137feSDavid Ahern } 35263b1137feSDavid Ahern 35273b1137feSDavid Ahern if (rt) 35283b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 35293b1137feSDavid Ahern } 35303b1137feSDavid Ahern 3531333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3532333c4301SDavid Ahern struct netlink_ext_ack *extack) 353351ebd318SNicolas Dichtel { 35343b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 35353b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 353651ebd318SNicolas Dichtel struct fib6_config r_cfg; 353751ebd318SNicolas Dichtel struct rtnexthop *rtnh; 35386b9ea5a6SRoopa Prabhu struct rt6_info *rt; 35396b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 35406b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 35413b1137feSDavid Ahern __u16 nlflags; 354251ebd318SNicolas Dichtel int remaining; 354351ebd318SNicolas Dichtel int attrlen; 35446b9ea5a6SRoopa Prabhu int err = 1; 35456b9ea5a6SRoopa Prabhu int nhn = 0; 35466b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 35476b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 35486b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 354951ebd318SNicolas Dichtel 35503b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 35513b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 35523b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 35533b1137feSDavid Ahern 355435f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 355551ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 355651ebd318SNicolas Dichtel 35576b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 35586b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 35596b9ea5a6SRoopa Prabhu */ 356051ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 356151ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 356251ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 356351ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 356451ebd318SNicolas Dichtel 356551ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 356651ebd318SNicolas Dichtel if (attrlen > 0) { 356751ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 356851ebd318SNicolas Dichtel 356951ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 357051ebd318SNicolas Dichtel if (nla) { 357167b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 357251ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 357351ebd318SNicolas Dichtel } 357419e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 357519e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 357619e42e45SRoopa Prabhu if (nla) 357719e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 357851ebd318SNicolas Dichtel } 35796b9ea5a6SRoopa Prabhu 3580333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 35818c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 35828c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 35838c5b83f0SRoopa Prabhu rt = NULL; 35846b9ea5a6SRoopa Prabhu goto cleanup; 35858c5b83f0SRoopa Prabhu } 35866b9ea5a6SRoopa Prabhu 35876b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 358851ebd318SNicolas Dichtel if (err) { 3589587fea74SWei Wang dst_release_immediate(&rt->dst); 35906b9ea5a6SRoopa Prabhu goto cleanup; 359151ebd318SNicolas Dichtel } 35926b9ea5a6SRoopa Prabhu 35936b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 359451ebd318SNicolas Dichtel } 35956b9ea5a6SRoopa Prabhu 35963b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 35973b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 35983b1137feSDavid Ahern * the full route when done 35993b1137feSDavid Ahern */ 36003b1137feSDavid Ahern info->skip_notify = 1; 36013b1137feSDavid Ahern 36026b9ea5a6SRoopa Prabhu err_nh = NULL; 36036b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 36043b1137feSDavid Ahern rt_last = nh->rt6_info; 3605333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 36063b1137feSDavid Ahern /* save reference to first route for notification */ 36073b1137feSDavid Ahern if (!rt_notif && !err) 36083b1137feSDavid Ahern rt_notif = nh->rt6_info; 36093b1137feSDavid Ahern 36106b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 36116b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 36126b9ea5a6SRoopa Prabhu if (err) { 36136b9ea5a6SRoopa Prabhu if (replace && nhn) 36146b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 36156b9ea5a6SRoopa Prabhu err_nh = nh; 36166b9ea5a6SRoopa Prabhu goto add_errout; 36176b9ea5a6SRoopa Prabhu } 36186b9ea5a6SRoopa Prabhu 36191a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 362027596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 362127596472SMichal Kubeček * we have already failed to add the first nexthop: 362227596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 362327596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 362427596472SMichal Kubeček * be added to it. 36251a72418bSNicolas Dichtel */ 362627596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 362727596472SMichal Kubeček NLM_F_REPLACE); 36286b9ea5a6SRoopa Prabhu nhn++; 36296b9ea5a6SRoopa Prabhu } 36306b9ea5a6SRoopa Prabhu 36313b1137feSDavid Ahern /* success ... tell user about new route */ 36323b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 36336b9ea5a6SRoopa Prabhu goto cleanup; 36346b9ea5a6SRoopa Prabhu 36356b9ea5a6SRoopa Prabhu add_errout: 36363b1137feSDavid Ahern /* send notification for routes that were added so that 36373b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 36383b1137feSDavid Ahern * coherent 36393b1137feSDavid Ahern */ 36403b1137feSDavid Ahern if (rt_notif) 36413b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 36423b1137feSDavid Ahern 36436b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 36446b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 36456b9ea5a6SRoopa Prabhu if (err_nh == nh) 36466b9ea5a6SRoopa Prabhu break; 3647333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 36486b9ea5a6SRoopa Prabhu } 36496b9ea5a6SRoopa Prabhu 36506b9ea5a6SRoopa Prabhu cleanup: 36516b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3652587fea74SWei Wang if (nh->rt6_info) 3653587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 36546b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 36556b9ea5a6SRoopa Prabhu list_del(&nh->next); 36566b9ea5a6SRoopa Prabhu kfree(nh); 36576b9ea5a6SRoopa Prabhu } 36586b9ea5a6SRoopa Prabhu 36596b9ea5a6SRoopa Prabhu return err; 36606b9ea5a6SRoopa Prabhu } 36616b9ea5a6SRoopa Prabhu 3662333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3663333c4301SDavid Ahern struct netlink_ext_ack *extack) 36646b9ea5a6SRoopa Prabhu { 36656b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36666b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 36676b9ea5a6SRoopa Prabhu int remaining; 36686b9ea5a6SRoopa Prabhu int attrlen; 36696b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 36706b9ea5a6SRoopa Prabhu 36716b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 36726b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 36736b9ea5a6SRoopa Prabhu 36746b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 36756b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 36766b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 36776b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 36786b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 36796b9ea5a6SRoopa Prabhu 36806b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 36816b9ea5a6SRoopa Prabhu if (attrlen > 0) { 36826b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 36836b9ea5a6SRoopa Prabhu 36846b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 36856b9ea5a6SRoopa Prabhu if (nla) { 36866b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 36876b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 36886b9ea5a6SRoopa Prabhu } 36896b9ea5a6SRoopa Prabhu } 3690333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 36916b9ea5a6SRoopa Prabhu if (err) 36926b9ea5a6SRoopa Prabhu last_err = err; 36936b9ea5a6SRoopa Prabhu 369451ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 369551ebd318SNicolas Dichtel } 369651ebd318SNicolas Dichtel 369751ebd318SNicolas Dichtel return last_err; 369851ebd318SNicolas Dichtel } 369951ebd318SNicolas Dichtel 3700c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3701c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 37021da177e4SLinus Torvalds { 370386872cb5SThomas Graf struct fib6_config cfg; 370486872cb5SThomas Graf int err; 37051da177e4SLinus Torvalds 3706333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 370786872cb5SThomas Graf if (err < 0) 370886872cb5SThomas Graf return err; 370986872cb5SThomas Graf 371051ebd318SNicolas Dichtel if (cfg.fc_mp) 3711333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 37120ae81335SDavid Ahern else { 37130ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3714333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 37151da177e4SLinus Torvalds } 37160ae81335SDavid Ahern } 37171da177e4SLinus Torvalds 3718c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3719c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 37201da177e4SLinus Torvalds { 372186872cb5SThomas Graf struct fib6_config cfg; 372286872cb5SThomas Graf int err; 37231da177e4SLinus Torvalds 3724333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 372586872cb5SThomas Graf if (err < 0) 372686872cb5SThomas Graf return err; 372786872cb5SThomas Graf 372851ebd318SNicolas Dichtel if (cfg.fc_mp) 3729333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 373051ebd318SNicolas Dichtel else 3731333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 3734beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3735339bf98fSThomas Graf { 3736beb1afacSDavid Ahern int nexthop_len = 0; 3737beb1afacSDavid Ahern 3738beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3739beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3740beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3741beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3742beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3743beb1afacSDavid Ahern 3744beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3745beb1afacSDavid Ahern } 3746beb1afacSDavid Ahern 3747339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3748339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3749339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3750339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3751339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3752339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3753339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3754339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3755339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 37566a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3757ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3758c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 375919e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3760beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3761beb1afacSDavid Ahern + nexthop_len; 3762beb1afacSDavid Ahern } 3763beb1afacSDavid Ahern 3764beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 37655be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3766beb1afacSDavid Ahern { 3767beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3768beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3769beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3770beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3771beb1afacSDavid Ahern } 3772beb1afacSDavid Ahern 3773beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3774beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3775beb1afacSDavid Ahern goto nla_put_failure; 3776beb1afacSDavid Ahern } 3777beb1afacSDavid Ahern 3778fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 377961e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 378061e4d01eSIdo Schimmel 37815be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 37825be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3783beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3784beb1afacSDavid Ahern goto nla_put_failure; 3785beb1afacSDavid Ahern 3786beb1afacSDavid Ahern if (rt->dst.lwtstate && 3787beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3788beb1afacSDavid Ahern goto nla_put_failure; 3789beb1afacSDavid Ahern 3790beb1afacSDavid Ahern return 0; 3791beb1afacSDavid Ahern 3792beb1afacSDavid Ahern nla_put_failure: 3793beb1afacSDavid Ahern return -EMSGSIZE; 3794beb1afacSDavid Ahern } 3795beb1afacSDavid Ahern 37965be083ceSDavid Ahern /* add multipath next hop */ 3797beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3798beb1afacSDavid Ahern { 3799beb1afacSDavid Ahern struct rtnexthop *rtnh; 3800beb1afacSDavid Ahern unsigned int flags = 0; 3801beb1afacSDavid Ahern 3802beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3803beb1afacSDavid Ahern if (!rtnh) 3804beb1afacSDavid Ahern goto nla_put_failure; 3805beb1afacSDavid Ahern 3806beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3807beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3808beb1afacSDavid Ahern 38095be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3810beb1afacSDavid Ahern goto nla_put_failure; 3811beb1afacSDavid Ahern 3812beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3813beb1afacSDavid Ahern 3814beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3815beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3816beb1afacSDavid Ahern 3817beb1afacSDavid Ahern return 0; 3818beb1afacSDavid Ahern 3819beb1afacSDavid Ahern nla_put_failure: 3820beb1afacSDavid Ahern return -EMSGSIZE; 3821339bf98fSThomas Graf } 3822339bf98fSThomas Graf 3823191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3824191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 38250d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 382615e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3827f8cfe2ceSDavid Ahern unsigned int flags) 38281da177e4SLinus Torvalds { 38294b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 38301da177e4SLinus Torvalds struct rtmsg *rtm; 38311da177e4SLinus Torvalds struct nlmsghdr *nlh; 3832e3703b3dSThomas Graf long expires; 38339e762a4aSPatrick McHardy u32 table; 38341da177e4SLinus Torvalds 383515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 383638308473SDavid S. Miller if (!nlh) 383726932566SPatrick McHardy return -EMSGSIZE; 38382d7202bfSThomas Graf 38392d7202bfSThomas Graf rtm = nlmsg_data(nlh); 38401da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 38411da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 38421da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 38431da177e4SLinus Torvalds rtm->rtm_tos = 0; 3844c71099acSThomas Graf if (rt->rt6i_table) 38459e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3846c71099acSThomas Graf else 38479e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 38489e762a4aSPatrick McHardy rtm->rtm_table = table; 3849c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3850c78679e8SDavid S. Miller goto nla_put_failure; 3851ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3852ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3853ef2c7d7bSNicolas Dichtel case -EINVAL: 3854ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3855ef2c7d7bSNicolas Dichtel break; 3856ef2c7d7bSNicolas Dichtel case -EACCES: 3857ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3858ef2c7d7bSNicolas Dichtel break; 3859b4949ab2SNicolas Dichtel case -EAGAIN: 3860b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3861b4949ab2SNicolas Dichtel break; 3862ef2c7d7bSNicolas Dichtel default: 38631da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3864ef2c7d7bSNicolas Dichtel break; 3865ef2c7d7bSNicolas Dichtel } 3866ef2c7d7bSNicolas Dichtel } 3867ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3868ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 38694ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 38704ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3871d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 38721da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 38731da177e4SLinus Torvalds else 38741da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 38751da177e4SLinus Torvalds rtm->rtm_flags = 0; 38761da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 38771da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 38781da177e4SLinus Torvalds 38791da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 38801da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds if (dst) { 3883930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3884c78679e8SDavid S. Miller goto nla_put_failure; 38851da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 38861da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3887930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3888c78679e8SDavid S. Miller goto nla_put_failure; 38891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 38901da177e4SLinus Torvalds if (src) { 3891930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3892c78679e8SDavid S. Miller goto nla_put_failure; 38931da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3894c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3895930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3896c78679e8SDavid S. Miller goto nla_put_failure; 38971da177e4SLinus Torvalds #endif 38987bc570c8SYOSHIFUJI Hideaki if (iif) { 38997bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 39007bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3901fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 39022cf75070SNikolay Aleksandrov 39037bc570c8SYOSHIFUJI Hideaki if (err == 0) 39047bc570c8SYOSHIFUJI Hideaki return 0; 3905fd61c6baSDavid Ahern if (err < 0) 39067bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 39077bc570c8SYOSHIFUJI Hideaki } else 39087bc570c8SYOSHIFUJI Hideaki #endif 3909c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3910c78679e8SDavid S. Miller goto nla_put_failure; 39117bc570c8SYOSHIFUJI Hideaki } else if (dst) { 39121da177e4SLinus Torvalds struct in6_addr saddr_buf; 3913c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3914930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3915c78679e8SDavid S. Miller goto nla_put_failure; 3916c3968a85SDaniel Walter } 3917c3968a85SDaniel Walter 3918c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3919c3968a85SDaniel Walter struct in6_addr saddr_buf; 39204e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3921930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3922c78679e8SDavid S. Miller goto nla_put_failure; 39231da177e4SLinus Torvalds } 39242d7202bfSThomas Graf 39254b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 39264b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 39274b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 39284b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 39292d7202bfSThomas Graf goto nla_put_failure; 39302d7202bfSThomas Graf 3931beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3932beb1afacSDavid Ahern goto nla_put_failure; 3933beb1afacSDavid Ahern 3934beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3935beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3936beb1afacSDavid Ahern */ 3937beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3938beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3939beb1afacSDavid Ahern struct nlattr *mp; 3940beb1afacSDavid Ahern 3941beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3942beb1afacSDavid Ahern if (!mp) 3943beb1afacSDavid Ahern goto nla_put_failure; 3944beb1afacSDavid Ahern 3945beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3946beb1afacSDavid Ahern goto nla_put_failure; 3947beb1afacSDavid Ahern 3948beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3949beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3950beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 395194f826b8SEric Dumazet goto nla_put_failure; 395294f826b8SEric Dumazet } 39532d7202bfSThomas Graf 3954beb1afacSDavid Ahern nla_nest_end(skb, mp); 3955beb1afacSDavid Ahern } else { 39565be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3957c78679e8SDavid S. Miller goto nla_put_failure; 3958beb1afacSDavid Ahern } 39598253947eSLi Wei 39608253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 396169cdf8f9SYOSHIFUJI Hideaki 396287a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3963e3703b3dSThomas Graf goto nla_put_failure; 39641da177e4SLinus Torvalds 3965c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3966c78ba6d6SLubomir Rintel goto nla_put_failure; 3967c78ba6d6SLubomir Rintel 396819e42e45SRoopa Prabhu 3969053c095aSJohannes Berg nlmsg_end(skb, nlh); 3970053c095aSJohannes Berg return 0; 39712d7202bfSThomas Graf 39722d7202bfSThomas Graf nla_put_failure: 397326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 397426932566SPatrick McHardy return -EMSGSIZE; 39751da177e4SLinus Torvalds } 39761da177e4SLinus Torvalds 39771b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 39781da177e4SLinus Torvalds { 39791da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 39801f17e2f2SDavid Ahern struct net *net = arg->net; 39811f17e2f2SDavid Ahern 39821f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 39831f17e2f2SDavid Ahern return 0; 39841da177e4SLinus Torvalds 39852d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 39862d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3987f8cfe2ceSDavid Ahern 3988f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3989f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3990f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3991f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3992f8cfe2ceSDavid Ahern return 1; 3993f8cfe2ceSDavid Ahern } 3994f8cfe2ceSDavid Ahern } 39951da177e4SLinus Torvalds 39961f17e2f2SDavid Ahern return rt6_fill_node(net, 3997191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 399815e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3999f8cfe2ceSDavid Ahern NLM_F_MULTI); 40001da177e4SLinus Torvalds } 40011da177e4SLinus Torvalds 4002c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4003c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 40041da177e4SLinus Torvalds { 40053b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4006ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 400718c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 400818c3a61cSRoopa Prabhu struct dst_entry *dst; 40091da177e4SLinus Torvalds struct rt6_info *rt; 4010ab364a6fSThomas Graf struct sk_buff *skb; 4011ab364a6fSThomas Graf struct rtmsg *rtm; 40124c9483b2SDavid S. Miller struct flowi6 fl6; 401318c3a61cSRoopa Prabhu bool fibmatch; 4014ab364a6fSThomas Graf 4015fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4016c21ef3e3SDavid Ahern extack); 4017ab364a6fSThomas Graf if (err < 0) 4018ab364a6fSThomas Graf goto errout; 4019ab364a6fSThomas Graf 4020ab364a6fSThomas Graf err = -EINVAL; 40214c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 402238b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 402338b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 402418c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4025ab364a6fSThomas Graf 4026ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4027ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4028ab364a6fSThomas Graf goto errout; 4029ab364a6fSThomas Graf 40304e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4031ab364a6fSThomas Graf } 4032ab364a6fSThomas Graf 4033ab364a6fSThomas Graf if (tb[RTA_DST]) { 4034ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4035ab364a6fSThomas Graf goto errout; 4036ab364a6fSThomas Graf 40374e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4038ab364a6fSThomas Graf } 4039ab364a6fSThomas Graf 4040ab364a6fSThomas Graf if (tb[RTA_IIF]) 4041ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4042ab364a6fSThomas Graf 4043ab364a6fSThomas Graf if (tb[RTA_OIF]) 404472331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4045ab364a6fSThomas Graf 40462e47b291SLorenzo Colitti if (tb[RTA_MARK]) 40472e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 40482e47b291SLorenzo Colitti 4049622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4050622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4051622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4052622ec2c9SLorenzo Colitti else 4053622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4054622ec2c9SLorenzo Colitti 4055ab364a6fSThomas Graf if (iif) { 4056ab364a6fSThomas Graf struct net_device *dev; 405772331bc0SShmulik Ladkani int flags = 0; 405872331bc0SShmulik Ladkani 4059121622dbSFlorian Westphal rcu_read_lock(); 4060121622dbSFlorian Westphal 4061121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4062ab364a6fSThomas Graf if (!dev) { 4063121622dbSFlorian Westphal rcu_read_unlock(); 4064ab364a6fSThomas Graf err = -ENODEV; 4065ab364a6fSThomas Graf goto errout; 4066ab364a6fSThomas Graf } 406772331bc0SShmulik Ladkani 406872331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 406972331bc0SShmulik Ladkani 407072331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 407172331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 407272331bc0SShmulik Ladkani 407318c3a61cSRoopa Prabhu if (!fibmatch) 407418c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4075401481e0SArnd Bergmann else 4076401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4077121622dbSFlorian Westphal 4078121622dbSFlorian Westphal rcu_read_unlock(); 407972331bc0SShmulik Ladkani } else { 408072331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 408172331bc0SShmulik Ladkani 408218c3a61cSRoopa Prabhu if (!fibmatch) 408318c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4084401481e0SArnd Bergmann else 4085401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 408618c3a61cSRoopa Prabhu } 408718c3a61cSRoopa Prabhu 408818c3a61cSRoopa Prabhu 408918c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 409018c3a61cSRoopa Prabhu if (rt->dst.error) { 409118c3a61cSRoopa Prabhu err = rt->dst.error; 409218c3a61cSRoopa Prabhu ip6_rt_put(rt); 409318c3a61cSRoopa Prabhu goto errout; 4094ab364a6fSThomas Graf } 40951da177e4SLinus Torvalds 40969d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 40979d6acb3bSWANG Cong err = rt->dst.error; 40989d6acb3bSWANG Cong ip6_rt_put(rt); 40999d6acb3bSWANG Cong goto errout; 41009d6acb3bSWANG Cong } 41019d6acb3bSWANG Cong 41021da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 410338308473SDavid S. Miller if (!skb) { 410494e187c0SAmerigo Wang ip6_rt_put(rt); 4105ab364a6fSThomas Graf err = -ENOBUFS; 4106ab364a6fSThomas Graf goto errout; 4107ab364a6fSThomas Graf } 41081da177e4SLinus Torvalds 4109d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 411018c3a61cSRoopa Prabhu if (fibmatch) 411118c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 411218c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 411318c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 411418c3a61cSRoopa Prabhu else 41154c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 411615e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4117f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 41181da177e4SLinus Torvalds if (err < 0) { 4119ab364a6fSThomas Graf kfree_skb(skb); 4120ab364a6fSThomas Graf goto errout; 41211da177e4SLinus Torvalds } 41221da177e4SLinus Torvalds 412315e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4124ab364a6fSThomas Graf errout: 41251da177e4SLinus Torvalds return err; 41261da177e4SLinus Torvalds } 41271da177e4SLinus Torvalds 412837a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 412937a1d361SRoopa Prabhu unsigned int nlm_flags) 41301da177e4SLinus Torvalds { 41311da177e4SLinus Torvalds struct sk_buff *skb; 41325578689aSDaniel Lezcano struct net *net = info->nl_net; 4133528c4cebSDenis V. Lunev u32 seq; 4134528c4cebSDenis V. Lunev int err; 41350d51aa80SJamal Hadi Salim 4136528c4cebSDenis V. Lunev err = -ENOBUFS; 413738308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 413886872cb5SThomas Graf 413919e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 414038308473SDavid S. Miller if (!skb) 414121713ebcSThomas Graf goto errout; 41421da177e4SLinus Torvalds 4143191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4144f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 414526932566SPatrick McHardy if (err < 0) { 414626932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 414726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 414826932566SPatrick McHardy kfree_skb(skb); 414926932566SPatrick McHardy goto errout; 415026932566SPatrick McHardy } 415115e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 41525578689aSDaniel Lezcano info->nlh, gfp_any()); 41531ce85fe4SPablo Neira Ayuso return; 415421713ebcSThomas Graf errout: 415521713ebcSThomas Graf if (err < 0) 41565578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 41571da177e4SLinus Torvalds } 41581da177e4SLinus Torvalds 41598ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4160351638e7SJiri Pirko unsigned long event, void *ptr) 41618ed67789SDaniel Lezcano { 4162351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4163c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 41648ed67789SDaniel Lezcano 4165242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4166242d3a49SWANG Cong return NOTIFY_OK; 4167242d3a49SWANG Cong 4168242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4169d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 41708ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 41718ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4172d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 41738ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4174d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 41758ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 41768ed67789SDaniel Lezcano #endif 417776da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 417876da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 417976da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 418076da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 418176da0704SWANG Cong */ 418212d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4183242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 418412d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 418512d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4186242d3a49SWANG Cong #endif 41878ed67789SDaniel Lezcano } 41888ed67789SDaniel Lezcano 41898ed67789SDaniel Lezcano return NOTIFY_OK; 41908ed67789SDaniel Lezcano } 41918ed67789SDaniel Lezcano 41921da177e4SLinus Torvalds /* 41931da177e4SLinus Torvalds * /proc 41941da177e4SLinus Torvalds */ 41951da177e4SLinus Torvalds 41961da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41971da177e4SLinus Torvalds 419833120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 419933120b30SAlexey Dobriyan .owner = THIS_MODULE, 420033120b30SAlexey Dobriyan .open = ipv6_route_open, 420133120b30SAlexey Dobriyan .read = seq_read, 420233120b30SAlexey Dobriyan .llseek = seq_lseek, 42038d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 420433120b30SAlexey Dobriyan }; 420533120b30SAlexey Dobriyan 42061da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 42071da177e4SLinus Torvalds { 420869ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 42091da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 421069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 421169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 421269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 421369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 421469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4215fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 421669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 42171da177e4SLinus Torvalds 42181da177e4SLinus Torvalds return 0; 42191da177e4SLinus Torvalds } 42201da177e4SLinus Torvalds 42211da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 42221da177e4SLinus Torvalds { 4223de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 422469ddb805SDaniel Lezcano } 422569ddb805SDaniel Lezcano 42269a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 42271da177e4SLinus Torvalds .owner = THIS_MODULE, 42281da177e4SLinus Torvalds .open = rt6_stats_seq_open, 42291da177e4SLinus Torvalds .read = seq_read, 42301da177e4SLinus Torvalds .llseek = seq_lseek, 4231b6fcbdb4SPavel Emelyanov .release = single_release_net, 42321da177e4SLinus Torvalds }; 42331da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 42341da177e4SLinus Torvalds 42351da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 42361da177e4SLinus Torvalds 42371da177e4SLinus Torvalds static 4238fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 42391da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 42401da177e4SLinus Torvalds { 4241c486da34SLucian Adrian Grijincu struct net *net; 4242c486da34SLucian Adrian Grijincu int delay; 4243c486da34SLucian Adrian Grijincu if (!write) 4244c486da34SLucian Adrian Grijincu return -EINVAL; 4245c486da34SLucian Adrian Grijincu 4246c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4247c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 42488d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 42492ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 42501da177e4SLinus Torvalds return 0; 42511da177e4SLinus Torvalds } 42521da177e4SLinus Torvalds 4253fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 42541da177e4SLinus Torvalds { 42551da177e4SLinus Torvalds .procname = "flush", 42564990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 42571da177e4SLinus Torvalds .maxlen = sizeof(int), 425889c8b3a1SDave Jones .mode = 0200, 42596d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 42601da177e4SLinus Torvalds }, 42611da177e4SLinus Torvalds { 42621da177e4SLinus Torvalds .procname = "gc_thresh", 42639a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 42641da177e4SLinus Torvalds .maxlen = sizeof(int), 42651da177e4SLinus Torvalds .mode = 0644, 42666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42671da177e4SLinus Torvalds }, 42681da177e4SLinus Torvalds { 42691da177e4SLinus Torvalds .procname = "max_size", 42704990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 42711da177e4SLinus Torvalds .maxlen = sizeof(int), 42721da177e4SLinus Torvalds .mode = 0644, 42736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42741da177e4SLinus Torvalds }, 42751da177e4SLinus Torvalds { 42761da177e4SLinus Torvalds .procname = "gc_min_interval", 42774990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 42781da177e4SLinus Torvalds .maxlen = sizeof(int), 42791da177e4SLinus Torvalds .mode = 0644, 42806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42811da177e4SLinus Torvalds }, 42821da177e4SLinus Torvalds { 42831da177e4SLinus Torvalds .procname = "gc_timeout", 42844990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 42851da177e4SLinus Torvalds .maxlen = sizeof(int), 42861da177e4SLinus Torvalds .mode = 0644, 42876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42881da177e4SLinus Torvalds }, 42891da177e4SLinus Torvalds { 42901da177e4SLinus Torvalds .procname = "gc_interval", 42914990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 42921da177e4SLinus Torvalds .maxlen = sizeof(int), 42931da177e4SLinus Torvalds .mode = 0644, 42946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42951da177e4SLinus Torvalds }, 42961da177e4SLinus Torvalds { 42971da177e4SLinus Torvalds .procname = "gc_elasticity", 42984990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 42991da177e4SLinus Torvalds .maxlen = sizeof(int), 43001da177e4SLinus Torvalds .mode = 0644, 4301f3d3f616SMin Zhang .proc_handler = proc_dointvec, 43021da177e4SLinus Torvalds }, 43031da177e4SLinus Torvalds { 43041da177e4SLinus Torvalds .procname = "mtu_expires", 43054990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 43061da177e4SLinus Torvalds .maxlen = sizeof(int), 43071da177e4SLinus Torvalds .mode = 0644, 43086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 43091da177e4SLinus Torvalds }, 43101da177e4SLinus Torvalds { 43111da177e4SLinus Torvalds .procname = "min_adv_mss", 43124990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 43131da177e4SLinus Torvalds .maxlen = sizeof(int), 43141da177e4SLinus Torvalds .mode = 0644, 4315f3d3f616SMin Zhang .proc_handler = proc_dointvec, 43161da177e4SLinus Torvalds }, 43171da177e4SLinus Torvalds { 43181da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 43194990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 43201da177e4SLinus Torvalds .maxlen = sizeof(int), 43211da177e4SLinus Torvalds .mode = 0644, 43226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 43231da177e4SLinus Torvalds }, 4324f8572d8fSEric W. Biederman { } 43251da177e4SLinus Torvalds }; 43261da177e4SLinus Torvalds 43272c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4328760f2d01SDaniel Lezcano { 4329760f2d01SDaniel Lezcano struct ctl_table *table; 4330760f2d01SDaniel Lezcano 4331760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4332760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4333760f2d01SDaniel Lezcano GFP_KERNEL); 43345ee09105SYOSHIFUJI Hideaki 43355ee09105SYOSHIFUJI Hideaki if (table) { 43365ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4337c486da34SLucian Adrian Grijincu table[0].extra1 = net; 433886393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 43395ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 43405ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 43415ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 43425ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 43435ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 43445ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 43455ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 43469c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4347464dc801SEric W. Biederman 4348464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4349464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4350464dc801SEric W. Biederman table[0].procname = NULL; 43515ee09105SYOSHIFUJI Hideaki } 43525ee09105SYOSHIFUJI Hideaki 4353760f2d01SDaniel Lezcano return table; 4354760f2d01SDaniel Lezcano } 43551da177e4SLinus Torvalds #endif 43561da177e4SLinus Torvalds 43572c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4358cdb18761SDaniel Lezcano { 4359633d424bSPavel Emelyanov int ret = -ENOMEM; 43608ed67789SDaniel Lezcano 436186393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 436286393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4363f2fc6a54SBenjamin Thery 4364fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4365fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4366fc66f95cSEric Dumazet 43678ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 43688ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 43698ed67789SDaniel Lezcano GFP_KERNEL); 43708ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4371fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4372d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 43738ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4374d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 437562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 437662fa8a84SDavid S. Miller ip6_template_metrics, true); 43778ed67789SDaniel Lezcano 43788ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4379feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 43808ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 43818ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 43828ed67789SDaniel Lezcano GFP_KERNEL); 438368fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 438468fffc67SPeter Zijlstra goto out_ip6_null_entry; 4385d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 43868ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4387d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 438862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 438962fa8a84SDavid S. Miller ip6_template_metrics, true); 43908ed67789SDaniel Lezcano 43918ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 43928ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 43938ed67789SDaniel Lezcano GFP_KERNEL); 439468fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 439568fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4396d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 43978ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4398d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 439962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 440062fa8a84SDavid S. Miller ip6_template_metrics, true); 44018ed67789SDaniel Lezcano #endif 44028ed67789SDaniel Lezcano 4403b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4404b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4405b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4406b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4407b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4408b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4409b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4410b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4411b339a47cSPeter Zijlstra 44126891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 44136891a346SBenjamin Thery 44148ed67789SDaniel Lezcano ret = 0; 44158ed67789SDaniel Lezcano out: 44168ed67789SDaniel Lezcano return ret; 4417f2fc6a54SBenjamin Thery 441868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 441968fffc67SPeter Zijlstra out_ip6_prohibit_entry: 442068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 442168fffc67SPeter Zijlstra out_ip6_null_entry: 442268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 442368fffc67SPeter Zijlstra #endif 4424fc66f95cSEric Dumazet out_ip6_dst_entries: 4425fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4426f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4427f2fc6a54SBenjamin Thery goto out; 4428cdb18761SDaniel Lezcano } 4429cdb18761SDaniel Lezcano 44302c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4431cdb18761SDaniel Lezcano { 44328ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 44338ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 44348ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 44358ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 44368ed67789SDaniel Lezcano #endif 443741bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4438cdb18761SDaniel Lezcano } 4439cdb18761SDaniel Lezcano 4440d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4441d189634eSThomas Graf { 4442d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4443d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4444d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4445d189634eSThomas Graf #endif 4446d189634eSThomas Graf return 0; 4447d189634eSThomas Graf } 4448d189634eSThomas Graf 4449d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4450d189634eSThomas Graf { 4451d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4452ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4453ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4454d189634eSThomas Graf #endif 4455d189634eSThomas Graf } 4456d189634eSThomas Graf 4457cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4458cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4459cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4460cdb18761SDaniel Lezcano }; 4461cdb18761SDaniel Lezcano 4462c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4463c3426b47SDavid S. Miller { 4464c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4465c3426b47SDavid S. Miller 4466c3426b47SDavid S. Miller if (!bp) 4467c3426b47SDavid S. Miller return -ENOMEM; 4468c3426b47SDavid S. Miller inet_peer_base_init(bp); 4469c3426b47SDavid S. Miller net->ipv6.peers = bp; 4470c3426b47SDavid S. Miller return 0; 4471c3426b47SDavid S. Miller } 4472c3426b47SDavid S. Miller 4473c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4474c3426b47SDavid S. Miller { 4475c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4476c3426b47SDavid S. Miller 4477c3426b47SDavid S. Miller net->ipv6.peers = NULL; 447856a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4479c3426b47SDavid S. Miller kfree(bp); 4480c3426b47SDavid S. Miller } 4481c3426b47SDavid S. Miller 44822b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4483c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4484c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4485c3426b47SDavid S. Miller }; 4486c3426b47SDavid S. Miller 4487d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4488d189634eSThomas Graf .init = ip6_route_net_init_late, 4489d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4490d189634eSThomas Graf }; 4491d189634eSThomas Graf 44928ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 44938ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4494242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 44958ed67789SDaniel Lezcano }; 44968ed67789SDaniel Lezcano 44972f460933SWANG Cong void __init ip6_route_init_special_entries(void) 44982f460933SWANG Cong { 44992f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 45002f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 45012f460933SWANG Cong * manually for init_net */ 45022f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 45032f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 45042f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 45052f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 45062f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 45072f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 45082f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 45092f460933SWANG Cong #endif 45102f460933SWANG Cong } 45112f460933SWANG Cong 4512433d49c3SDaniel Lezcano int __init ip6_route_init(void) 45131da177e4SLinus Torvalds { 4514433d49c3SDaniel Lezcano int ret; 45158d0b94afSMartin KaFai Lau int cpu; 4516433d49c3SDaniel Lezcano 45179a7ec3a9SDaniel Lezcano ret = -ENOMEM; 45189a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 45199a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 45209a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 45219a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4522c19a28e1SFernando Carrijo goto out; 452314e50e57SDavid S. Miller 4524fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 45258ed67789SDaniel Lezcano if (ret) 4526bdb3289fSDaniel Lezcano goto out_kmem_cache; 4527bdb3289fSDaniel Lezcano 4528c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4529c3426b47SDavid S. Miller if (ret) 4530e8803b6cSDavid S. Miller goto out_dst_entries; 45312a0c451aSThomas Graf 45327e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 45337e52b33bSDavid S. Miller if (ret) 45347e52b33bSDavid S. Miller goto out_register_inetpeer; 4535c3426b47SDavid S. Miller 45365dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 45375dc121e9SArnaud Ebalard 4538e8803b6cSDavid S. Miller ret = fib6_init(); 4539433d49c3SDaniel Lezcano if (ret) 45408ed67789SDaniel Lezcano goto out_register_subsys; 4541433d49c3SDaniel Lezcano 4542433d49c3SDaniel Lezcano ret = xfrm6_init(); 4543433d49c3SDaniel Lezcano if (ret) 4544e8803b6cSDavid S. Miller goto out_fib6_init; 4545c35b7e72SDaniel Lezcano 4546433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4547433d49c3SDaniel Lezcano if (ret) 4548433d49c3SDaniel Lezcano goto xfrm6_init; 45497e5449c2SDaniel Lezcano 4550d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4551d189634eSThomas Graf if (ret) 4552d189634eSThomas Graf goto fib6_rules_init; 4553d189634eSThomas Graf 4554433d49c3SDaniel Lezcano ret = -ENOBUFS; 4555b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4556b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4557e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4558e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4559d189634eSThomas Graf goto out_register_late_subsys; 4560433d49c3SDaniel Lezcano 45618ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4562cdb18761SDaniel Lezcano if (ret) 4563d189634eSThomas Graf goto out_register_late_subsys; 45648ed67789SDaniel Lezcano 45658d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 45668d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 45678d0b94afSMartin KaFai Lau 45688d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 45698d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 45708d0b94afSMartin KaFai Lau } 45718d0b94afSMartin KaFai Lau 4572433d49c3SDaniel Lezcano out: 4573433d49c3SDaniel Lezcano return ret; 4574433d49c3SDaniel Lezcano 4575d189634eSThomas Graf out_register_late_subsys: 4576d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4577433d49c3SDaniel Lezcano fib6_rules_init: 4578433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4579433d49c3SDaniel Lezcano xfrm6_init: 4580433d49c3SDaniel Lezcano xfrm6_fini(); 45812a0c451aSThomas Graf out_fib6_init: 45822a0c451aSThomas Graf fib6_gc_cleanup(); 45838ed67789SDaniel Lezcano out_register_subsys: 45848ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 45857e52b33bSDavid S. Miller out_register_inetpeer: 45867e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4587fc66f95cSEric Dumazet out_dst_entries: 4588fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4589433d49c3SDaniel Lezcano out_kmem_cache: 4590f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4591433d49c3SDaniel Lezcano goto out; 45921da177e4SLinus Torvalds } 45931da177e4SLinus Torvalds 45941da177e4SLinus Torvalds void ip6_route_cleanup(void) 45951da177e4SLinus Torvalds { 45968ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4597d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4598101367c2SThomas Graf fib6_rules_cleanup(); 45991da177e4SLinus Torvalds xfrm6_fini(); 46001da177e4SLinus Torvalds fib6_gc_cleanup(); 4601c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 46028ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 460341bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4604f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 46051da177e4SLinus Torvalds } 4606