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; 14681eb8447SWei Wang struct net *net = dev_net(rt->dst.dev); 1478d0b94afSMartin KaFai Lau 1488d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1498d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 15081eb8447SWei Wang atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); 1518d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1528d0b94afSMartin KaFai Lau } 1538d0b94afSMartin KaFai Lau } 1548d0b94afSMartin KaFai Lau 1558d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1568d0b94afSMartin KaFai Lau { 1578d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1588d0b94afSMartin KaFai Lau int cpu; 1598d0b94afSMartin KaFai Lau 160e332bc67SEric W. Biederman if (dev == loopback_dev) 161e332bc67SEric W. Biederman return; 162e332bc67SEric W. Biederman 1638d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1648d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1658d0b94afSMartin KaFai Lau struct rt6_info *rt; 1668d0b94afSMartin KaFai Lau 1678d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1688d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1698d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1708d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1718d0b94afSMartin KaFai Lau 172e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1738d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1748d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1758d0b94afSMartin KaFai Lau } 1768d0b94afSMartin KaFai Lau 177e332bc67SEric W. Biederman if (rt_dev == dev) { 1788d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1798d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1808d0b94afSMartin KaFai Lau dev_put(rt_dev); 1818d0b94afSMartin KaFai Lau } 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1848d0b94afSMartin KaFai Lau } 1858d0b94afSMartin KaFai Lau } 1868d0b94afSMartin KaFai Lau 187d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 188d52d3997SMartin KaFai Lau { 189d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 190d52d3997SMartin KaFai Lau } 191d52d3997SMartin KaFai Lau 19206582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 19306582540SDavid S. Miller { 19406582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19506582540SDavid S. Miller 196d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 197d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 198d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1994b32b5adSMartin KaFai Lau return NULL; 2004b32b5adSMartin KaFai Lau else 2013b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 20206582540SDavid S. Miller } 20306582540SDavid S. Miller 204f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 205f894cbf8SDavid S. Miller struct sk_buff *skb, 206f894cbf8SDavid S. Miller const void *daddr) 20739232973SDavid S. Miller { 20839232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20939232973SDavid S. Miller 210a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 21139232973SDavid S. Miller return (const void *) p; 212f894cbf8SDavid S. Miller else if (skb) 213f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 21439232973SDavid S. Miller return daddr; 21539232973SDavid S. Miller } 21639232973SDavid S. Miller 217f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 218f894cbf8SDavid S. Miller struct sk_buff *skb, 219f894cbf8SDavid S. Miller const void *daddr) 220d3aaeb38SDavid S. Miller { 22139232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 22239232973SDavid S. Miller struct neighbour *n; 22339232973SDavid S. Miller 224f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2258e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 226f83c7790SDavid S. Miller if (n) 227f83c7790SDavid S. Miller return n; 228f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 229f83c7790SDavid S. Miller } 230f83c7790SDavid S. Miller 23163fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23263fca65dSJulian Anastasov { 23363fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23463fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23563fca65dSJulian Anastasov 23663fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23763fca65dSJulian Anastasov if (!daddr) 23863fca65dSJulian Anastasov return; 23963fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 24063fca65dSJulian Anastasov return; 24163fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24263fca65dSJulian Anastasov return; 24363fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24463fca65dSJulian Anastasov } 24563fca65dSJulian Anastasov 2469a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2471da177e4SLinus Torvalds .family = AF_INET6, 2481da177e4SLinus Torvalds .gc = ip6_dst_gc, 2491da177e4SLinus Torvalds .gc_thresh = 1024, 2501da177e4SLinus Torvalds .check = ip6_dst_check, 2510dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 252ebb762f2SSteffen Klassert .mtu = ip6_mtu, 25306582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2541da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2551da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2561da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2571da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2581da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2596e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2609f8955ccSEric W. Biederman .local_out = __ip6_local_out, 261d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26263fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2631da177e4SLinus Torvalds }; 2641da177e4SLinus Torvalds 265ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 266ec831ea7SRoland Dreier { 267618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 268618f9bc7SSteffen Klassert 269618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 270ec831ea7SRoland Dreier } 271ec831ea7SRoland Dreier 2726700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2736700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27414e50e57SDavid S. Miller { 27514e50e57SDavid S. Miller } 27614e50e57SDavid S. Miller 2776700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2786700c270SDavid S. Miller struct sk_buff *skb) 279b587ee3bSDavid S. Miller { 280b587ee3bSDavid S. Miller } 281b587ee3bSDavid S. Miller 28214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28314e50e57SDavid S. Miller .family = AF_INET6, 28414e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28514e50e57SDavid S. Miller .check = ip6_dst_check, 286ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 287214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28814e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 289b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2900a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 291d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 29214e50e57SDavid S. Miller }; 29314e50e57SDavid S. Miller 29462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29514edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29662fa8a84SDavid S. Miller }; 29762fa8a84SDavid S. Miller 298fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2991da177e4SLinus Torvalds .dst = { 3001da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 3011da177e4SLinus Torvalds .__use = 1, 3022c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3031da177e4SLinus Torvalds .error = -ENETUNREACH, 3041da177e4SLinus Torvalds .input = ip6_pkt_discard, 3051da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3061da177e4SLinus Torvalds }, 3071da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3084f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3091da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3101da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3111da177e4SLinus Torvalds }; 3121da177e4SLinus Torvalds 313101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 314101367c2SThomas Graf 315fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 316101367c2SThomas Graf .dst = { 317101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 318101367c2SThomas Graf .__use = 1, 3192c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 320101367c2SThomas Graf .error = -EACCES, 3219ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3229ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 323101367c2SThomas Graf }, 324101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3254f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 326101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 327101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 328101367c2SThomas Graf }; 329101367c2SThomas Graf 330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 331101367c2SThomas Graf .dst = { 332101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 333101367c2SThomas Graf .__use = 1, 3342c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 335101367c2SThomas Graf .error = -EINVAL, 336352e512cSHerbert Xu .input = dst_discard, 337ede2059dSEric W. Biederman .output = dst_discard_out, 338101367c2SThomas Graf }, 339101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3404f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 341101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 342101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 343101367c2SThomas Graf }; 344101367c2SThomas Graf 345101367c2SThomas Graf #endif 346101367c2SThomas Graf 347ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 348ebfa45f0SMartin KaFai Lau { 349ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 350ebfa45f0SMartin KaFai Lau 351ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 352ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 353ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 354ebfa45f0SMartin KaFai Lau } 355ebfa45f0SMartin KaFai Lau 3561da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 357d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 358957c665fSDavid S. Miller struct net_device *dev, 359ad706862SMartin KaFai Lau int flags) 3601da177e4SLinus Torvalds { 36197bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 362b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 363cf911662SDavid S. Miller 36481eb8447SWei Wang if (rt) { 365ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 36681eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 36781eb8447SWei Wang } 3688104891bSSteffen Klassert 369cf911662SDavid S. Miller return rt; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3729ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 373d52d3997SMartin KaFai Lau struct net_device *dev, 374ad706862SMartin KaFai Lau int flags) 375d52d3997SMartin KaFai Lau { 376ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 377d52d3997SMartin KaFai Lau 378d52d3997SMartin KaFai Lau if (rt) { 379d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 380bfd8e5a4SEric Dumazet if (!rt->rt6i_pcpu) { 381587fea74SWei Wang dst_release_immediate(&rt->dst); 382d52d3997SMartin KaFai Lau return NULL; 383d52d3997SMartin KaFai Lau } 384d52d3997SMartin KaFai Lau } 385d52d3997SMartin KaFai Lau 386d52d3997SMartin KaFai Lau return rt; 387d52d3997SMartin KaFai Lau } 3889ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 389d52d3997SMartin KaFai Lau 3901da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 39335732d01SWei Wang struct rt6_exception_bucket *bucket; 394ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3958d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3961da177e4SLinus Torvalds 3978e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 398d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 3998d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4008d0b94afSMartin KaFai Lau 4018d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40238308473SDavid S. Miller if (idev) { 4031da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4041da177e4SLinus Torvalds in6_dev_put(idev); 4051da177e4SLinus Torvalds } 40635732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 40735732d01SWei Wang if (bucket) { 40835732d01SWei Wang rt->rt6i_exception_bucket = NULL; 40935732d01SWei Wang kfree(bucket); 41035732d01SWei Wang } 4111716a961SGao feng 412ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 413ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 414b3419363SDavid S. Miller } 415b3419363SDavid S. Miller 4161da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4171da177e4SLinus Torvalds int how) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4201da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4215a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 422c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4231da177e4SLinus Torvalds 424e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 425e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 42638308473SDavid S. Miller if (loopback_idev) { 4271da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4281da177e4SLinus Torvalds in6_dev_put(idev); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 43197cac082SDavid S. Miller } 4321da177e4SLinus Torvalds 4335973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4345973fb1eSMartin KaFai Lau { 4355973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4365973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4375973fb1eSMartin KaFai Lau else 4385973fb1eSMartin KaFai Lau return false; 4395973fb1eSMartin KaFai Lau } 4405973fb1eSMartin KaFai Lau 441a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4421da177e4SLinus Torvalds { 4431716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4441716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 445a50feda5SEric Dumazet return true; 4461716a961SGao feng } else if (rt->dst.from) { 4471e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4481e2ea8adSXin Long rt6_check_expired((struct rt6_info *)rt->dst.from); 4491716a961SGao feng } 450a50feda5SEric Dumazet return false; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 45351ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 45452bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 45552bd4c0cSNicolas Dichtel int strict) 45651ebd318SNicolas Dichtel { 45751ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 45851ebd318SNicolas Dichtel int route_choosen; 45951ebd318SNicolas Dichtel 460b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 461b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 462b673d6ccSJakub Sitnicki */ 463b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 464b673d6ccSJakub Sitnicki fl6->mp_hash = rt6_multipath_hash(fl6, NULL); 465b673d6ccSJakub Sitnicki 466b673d6ccSJakub Sitnicki route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1); 46751ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 46851ebd318SNicolas Dichtel * (siblings does not include ourself) 46951ebd318SNicolas Dichtel */ 47051ebd318SNicolas Dichtel if (route_choosen) 47151ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47251ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47351ebd318SNicolas Dichtel route_choosen--; 47451ebd318SNicolas Dichtel if (route_choosen == 0) { 47552bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 47652bd4c0cSNicolas Dichtel break; 47751ebd318SNicolas Dichtel match = sibling; 47851ebd318SNicolas Dichtel break; 47951ebd318SNicolas Dichtel } 48051ebd318SNicolas Dichtel } 48151ebd318SNicolas Dichtel return match; 48251ebd318SNicolas Dichtel } 48351ebd318SNicolas Dichtel 4841da177e4SLinus Torvalds /* 48566f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4861da177e4SLinus Torvalds */ 4871da177e4SLinus Torvalds 4888ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4898ed67789SDaniel Lezcano struct rt6_info *rt, 490b71d1d42SEric Dumazet const struct in6_addr *saddr, 4911da177e4SLinus Torvalds int oif, 492d420895eSYOSHIFUJI Hideaki int flags) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds struct rt6_info *local = NULL; 4951da177e4SLinus Torvalds struct rt6_info *sprt; 4961da177e4SLinus Torvalds 497dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 498dd3abc4eSYOSHIFUJI Hideaki goto out; 499dd3abc4eSYOSHIFUJI Hideaki 50066f5d6ceSWei Wang for (sprt = rt; sprt; sprt = rcu_dereference(sprt->dst.rt6_next)) { 501d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 502dd3abc4eSYOSHIFUJI Hideaki 503dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5041da177e4SLinus Torvalds if (dev->ifindex == oif) 5051da177e4SLinus Torvalds return sprt; 5061da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 50738308473SDavid S. Miller if (!sprt->rt6i_idev || 5081da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 50917fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5101da177e4SLinus Torvalds continue; 51117fb0b2bSDavid Ahern if (local && 51217fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5131da177e4SLinus Torvalds continue; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds local = sprt; 5161da177e4SLinus Torvalds } 517dd3abc4eSYOSHIFUJI Hideaki } else { 518dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 519dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 520dd3abc4eSYOSHIFUJI Hideaki return sprt; 521dd3abc4eSYOSHIFUJI Hideaki } 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 524dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5251da177e4SLinus Torvalds if (local) 5261da177e4SLinus Torvalds return local; 5271da177e4SLinus Torvalds 528d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5298ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5301da177e4SLinus Torvalds } 531dd3abc4eSYOSHIFUJI Hideaki out: 5321da177e4SLinus Torvalds return rt; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 53527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 536c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 537c2f17e82SHannes Frederic Sowa struct work_struct work; 538c2f17e82SHannes Frederic Sowa struct in6_addr target; 539c2f17e82SHannes Frederic Sowa struct net_device *dev; 540c2f17e82SHannes Frederic Sowa }; 541c2f17e82SHannes Frederic Sowa 542c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 543c2f17e82SHannes Frederic Sowa { 544c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 545c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 546c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 547c2f17e82SHannes Frederic Sowa 548c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 549adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 550c2f17e82SHannes Frederic Sowa dev_put(work->dev); 551662f5533SMichael Büsch kfree(work); 552c2f17e82SHannes Frederic Sowa } 553c2f17e82SHannes Frederic Sowa 55427097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 55527097255SYOSHIFUJI Hideaki { 556990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 557f2c31e32SEric Dumazet struct neighbour *neigh; 55827097255SYOSHIFUJI Hideaki /* 55927097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56027097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56127097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56227097255SYOSHIFUJI Hideaki * 56327097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56427097255SYOSHIFUJI Hideaki * to no more than one per minute. 56527097255SYOSHIFUJI Hideaki */ 5662152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 567fdd6681dSAmerigo Wang return; 5682152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5692152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5702152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5718d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5728d6c31bfSMartin KaFai Lau goto out; 5738d6c31bfSMartin KaFai Lau 574990edb42SMartin KaFai Lau work = NULL; 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 576990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 577990edb42SMartin KaFai Lau time_after(jiffies, 578990edb42SMartin KaFai Lau neigh->updated + 579990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 580c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 581990edb42SMartin KaFai Lau if (work) 5827e980569SJiri Benc __neigh_set_probe_once(neigh); 583990edb42SMartin KaFai Lau } 584c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 585990edb42SMartin KaFai Lau } else { 586990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 587990edb42SMartin KaFai Lau } 588c2f17e82SHannes Frederic Sowa 589c2f17e82SHannes Frederic Sowa if (work) { 590c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 591c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 592c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 593c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 594c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 595c2f17e82SHannes Frederic Sowa } 596990edb42SMartin KaFai Lau 5978d6c31bfSMartin KaFai Lau out: 5982152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 599f2c31e32SEric Dumazet } 60027097255SYOSHIFUJI Hideaki #else 60127097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60227097255SYOSHIFUJI Hideaki { 60327097255SYOSHIFUJI Hideaki } 60427097255SYOSHIFUJI Hideaki #endif 60527097255SYOSHIFUJI Hideaki 6061da177e4SLinus Torvalds /* 607554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6081da177e4SLinus Torvalds */ 609b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6101da177e4SLinus Torvalds { 611d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 612161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 613554cfb7eSYOSHIFUJI Hideaki return 2; 614161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 615161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 616161980f4SDavid S. Miller return 1; 617554cfb7eSYOSHIFUJI Hideaki return 0; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 620afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6211da177e4SLinus Torvalds { 622f2c31e32SEric Dumazet struct neighbour *neigh; 623afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 624f2c31e32SEric Dumazet 6254d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6264d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 627afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 628145a3621SYOSHIFUJI Hideaki / 吉藤英明 629145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 630145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 631145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 632145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 633554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 634afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 635398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 636a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 637afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6387e980569SJiri Benc else 6397e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 640398bcbebSYOSHIFUJI Hideaki #endif 641145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 642afc154e9SHannes Frederic Sowa } else { 643afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6447e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 645a5a81f0bSPaul Marks } 646145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 647145a3621SYOSHIFUJI Hideaki / 吉藤英明 648a5a81f0bSPaul Marks return ret; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 651554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 652554cfb7eSYOSHIFUJI Hideaki int strict) 653554cfb7eSYOSHIFUJI Hideaki { 654a5a81f0bSPaul Marks int m; 6554d0c5911SYOSHIFUJI Hideaki 6564d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 65777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 658afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 659ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 660ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 661ebacaaa0SYOSHIFUJI Hideaki #endif 662afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 663afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 664afc154e9SHannes Frederic Sowa if (n < 0) 665afc154e9SHannes Frederic Sowa return n; 666afc154e9SHannes Frederic Sowa } 667554cfb7eSYOSHIFUJI Hideaki return m; 668554cfb7eSYOSHIFUJI Hideaki } 669554cfb7eSYOSHIFUJI Hideaki 670f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 671afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 672afc154e9SHannes Frederic Sowa bool *do_rr) 673554cfb7eSYOSHIFUJI Hideaki { 674554cfb7eSYOSHIFUJI Hideaki int m; 675afc154e9SHannes Frederic Sowa bool match_do_rr = false; 67635103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 67735103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 67835103d11SAndy Gospodarek 67935103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 680d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 681d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68235103d11SAndy Gospodarek goto out; 683554cfb7eSYOSHIFUJI Hideaki 684554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 685f11e6659SDavid S. Miller goto out; 686554cfb7eSYOSHIFUJI Hideaki 687554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6887e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 689afc154e9SHannes Frederic Sowa match_do_rr = true; 690afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6917e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 692f11e6659SDavid S. Miller goto out; 6931da177e4SLinus Torvalds } 694f11e6659SDavid S. Miller 695afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 696afc154e9SHannes Frederic Sowa rt6_probe(rt); 697afc154e9SHannes Frederic Sowa 6987e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 699afc154e9SHannes Frederic Sowa if (m > *mpri) { 700afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 701afc154e9SHannes Frederic Sowa *mpri = m; 702afc154e9SHannes Frederic Sowa match = rt; 703afc154e9SHannes Frederic Sowa } 704f11e6659SDavid S. Miller out: 705f11e6659SDavid S. Miller return match; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 708f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 7098d1040e8SWei Wang struct rt6_info *leaf, 710f11e6659SDavid S. Miller struct rt6_info *rr_head, 711afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 712afc154e9SHannes Frederic Sowa bool *do_rr) 713f11e6659SDavid S. Miller { 7149fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 715f11e6659SDavid S. Miller int mpri = -1; 716f11e6659SDavid S. Miller 717f11e6659SDavid S. Miller match = NULL; 7189fbdcfafSSteffen Klassert cont = NULL; 71966f5d6ceSWei Wang for (rt = rr_head; rt; rt = rcu_dereference(rt->dst.rt6_next)) { 7209fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7219fbdcfafSSteffen Klassert cont = rt; 7229fbdcfafSSteffen Klassert break; 7239fbdcfafSSteffen Klassert } 7249fbdcfafSSteffen Klassert 725afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7269fbdcfafSSteffen Klassert } 7279fbdcfafSSteffen Klassert 72866f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 72966f5d6ceSWei Wang rt = rcu_dereference(rt->dst.rt6_next)) { 7309fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7319fbdcfafSSteffen Klassert cont = rt; 7329fbdcfafSSteffen Klassert break; 7339fbdcfafSSteffen Klassert } 7349fbdcfafSSteffen Klassert 7359fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7369fbdcfafSSteffen Klassert } 7379fbdcfafSSteffen Klassert 7389fbdcfafSSteffen Klassert if (match || !cont) 7399fbdcfafSSteffen Klassert return match; 7409fbdcfafSSteffen Klassert 74166f5d6ceSWei Wang for (rt = cont; rt; rt = rcu_dereference(rt->dst.rt6_next)) 742afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 743f11e6659SDavid S. Miller 744f11e6659SDavid S. Miller return match; 745f11e6659SDavid S. Miller } 746f11e6659SDavid S. Miller 7478d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, 7488d1040e8SWei Wang int oif, int strict) 749f11e6659SDavid S. Miller { 75066f5d6ceSWei Wang struct rt6_info *leaf = rcu_dereference(fn->leaf); 751f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 752afc154e9SHannes Frederic Sowa bool do_rr = false; 75317ecf590SWei Wang int key_plen; 754f11e6659SDavid S. Miller 7558d1040e8SWei Wang if (!leaf) 7568d1040e8SWei Wang return net->ipv6.ip6_null_entry; 7578d1040e8SWei Wang 75866f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 759f11e6659SDavid S. Miller if (!rt0) 76066f5d6ceSWei Wang rt0 = leaf; 761f11e6659SDavid S. Miller 76217ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 76317ecf590SWei Wang * and fn->leaf does not points to its child's leaf 76417ecf590SWei Wang * (This might happen if all routes under fn are deleted from 76517ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 76617ecf590SWei Wang */ 76717ecf590SWei Wang key_plen = rt0->rt6i_dst.plen; 76817ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 76917ecf590SWei Wang if (rt0->rt6i_src.plen) 77017ecf590SWei Wang key_plen = rt0->rt6i_src.plen; 77117ecf590SWei Wang #endif 77217ecf590SWei Wang if (fn->fn_bit != key_plen) 77317ecf590SWei Wang return net->ipv6.ip6_null_entry; 77417ecf590SWei Wang 7758d1040e8SWei Wang match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, 776afc154e9SHannes Frederic Sowa &do_rr); 777f11e6659SDavid S. Miller 778afc154e9SHannes Frederic Sowa if (do_rr) { 77966f5d6ceSWei Wang struct rt6_info *next = rcu_dereference(rt0->dst.rt6_next); 780f11e6659SDavid S. Miller 781554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 782f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 7838d1040e8SWei Wang next = leaf; 784f11e6659SDavid S. Miller 78566f5d6ceSWei Wang if (next != rt0) { 78666f5d6ceSWei Wang spin_lock_bh(&leaf->rt6i_table->tb6_lock); 78766f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 78866f5d6ceSWei Wang if (next->rt6i_node) 78966f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 79066f5d6ceSWei Wang spin_unlock_bh(&leaf->rt6i_table->tb6_lock); 79166f5d6ceSWei Wang } 792554cfb7eSYOSHIFUJI Hideaki } 793554cfb7eSYOSHIFUJI Hideaki 794a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7978b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7988b9df265SMartin KaFai Lau { 7998b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 8008b9df265SMartin KaFai Lau } 8018b9df265SMartin KaFai Lau 80270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 80370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 804b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 80570ceb4f5SYOSHIFUJI Hideaki { 806c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 80770ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 80870ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 80970ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 8104bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 81170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 81270ceb4f5SYOSHIFUJI Hideaki 81370ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 81470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81570ceb4f5SYOSHIFUJI Hideaki } 81670ceb4f5SYOSHIFUJI Hideaki 81770ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 81870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 81970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 82170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 82370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 82470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82570ceb4f5SYOSHIFUJI Hideaki } 82670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 82770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 82870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82970ceb4f5SYOSHIFUJI Hideaki } 83070ceb4f5SYOSHIFUJI Hideaki } 83170ceb4f5SYOSHIFUJI Hideaki 83270ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 83370ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8343933fc95SJens Rosenboom return -EINVAL; 83570ceb4f5SYOSHIFUJI Hideaki 8364bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 83770ceb4f5SYOSHIFUJI Hideaki 83870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 83970ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 84070ceb4f5SYOSHIFUJI Hideaki else { 84170ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 84270ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 84370ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 84470ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 84570ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 84670ceb4f5SYOSHIFUJI Hideaki } 84770ceb4f5SYOSHIFUJI Hideaki 848f104a567SDuan Jiong if (rinfo->prefix_len == 0) 849f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 850f104a567SDuan Jiong else 851f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 852830218c1SDavid Ahern gwaddr, dev); 85370ceb4f5SYOSHIFUJI Hideaki 85470ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 855e0a1ad73SThomas Graf ip6_del_rt(rt); 85670ceb4f5SYOSHIFUJI Hideaki rt = NULL; 85770ceb4f5SYOSHIFUJI Hideaki } 85870ceb4f5SYOSHIFUJI Hideaki 85970ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 860830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 861830218c1SDavid Ahern dev, pref); 86270ceb4f5SYOSHIFUJI Hideaki else if (rt) 86370ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 86470ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 86570ceb4f5SYOSHIFUJI Hideaki 86670ceb4f5SYOSHIFUJI Hideaki if (rt) { 8671716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8681716a961SGao feng rt6_clean_expires(rt); 8691716a961SGao feng else 8701716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8711716a961SGao feng 87294e187c0SAmerigo Wang ip6_rt_put(rt); 87370ceb4f5SYOSHIFUJI Hideaki } 87470ceb4f5SYOSHIFUJI Hideaki return 0; 87570ceb4f5SYOSHIFUJI Hideaki } 87670ceb4f5SYOSHIFUJI Hideaki #endif 87770ceb4f5SYOSHIFUJI Hideaki 878a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 879a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 880a3c00e46SMartin KaFai Lau { 88166f5d6ceSWei Wang struct fib6_node *pn, *sn; 882a3c00e46SMartin KaFai Lau while (1) { 883a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 884a3c00e46SMartin KaFai Lau return NULL; 88566f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 88666f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 88766f5d6ceSWei Wang if (sn && sn != fn) 88866f5d6ceSWei Wang fn = fib6_lookup(sn, NULL, saddr); 889a3c00e46SMartin KaFai Lau else 890a3c00e46SMartin KaFai Lau fn = pn; 891a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 892a3c00e46SMartin KaFai Lau return fn; 893a3c00e46SMartin KaFai Lau } 894a3c00e46SMartin KaFai Lau } 895c71099acSThomas Graf 896d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 897d3843fe5SWei Wang bool null_fallback) 898d3843fe5SWei Wang { 899d3843fe5SWei Wang struct rt6_info *rt = *prt; 900d3843fe5SWei Wang 901d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 902d3843fe5SWei Wang return true; 903d3843fe5SWei Wang if (null_fallback) { 904d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 905d3843fe5SWei Wang dst_hold(&rt->dst); 906d3843fe5SWei Wang } else { 907d3843fe5SWei Wang rt = NULL; 908d3843fe5SWei Wang } 909d3843fe5SWei Wang *prt = rt; 910d3843fe5SWei Wang return false; 911d3843fe5SWei Wang } 912d3843fe5SWei Wang 9138ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 9148ed67789SDaniel Lezcano struct fib6_table *table, 9154c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9161da177e4SLinus Torvalds { 9172b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 9181da177e4SLinus Torvalds struct fib6_node *fn; 9191da177e4SLinus Torvalds 92066f5d6ceSWei Wang rcu_read_lock(); 9214c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 922c71099acSThomas Graf restart: 92366f5d6ceSWei Wang rt = rcu_dereference(fn->leaf); 92466f5d6ceSWei Wang if (!rt) { 92566f5d6ceSWei Wang rt = net->ipv6.ip6_null_entry; 92666f5d6ceSWei Wang } else { 92766f5d6ceSWei Wang rt = rt6_device_match(net, rt, &fl6->saddr, 92866f5d6ceSWei Wang fl6->flowi6_oif, flags); 92951ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 93066f5d6ceSWei Wang rt = rt6_multipath_select(rt, fl6, 93166f5d6ceSWei Wang fl6->flowi6_oif, flags); 93266f5d6ceSWei Wang } 933a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 934a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 935a3c00e46SMartin KaFai Lau if (fn) 936a3c00e46SMartin KaFai Lau goto restart; 937a3c00e46SMartin KaFai Lau } 9382b760fcfSWei Wang /* Search through exception table */ 9392b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 9402b760fcfSWei Wang if (rt_cache) 9412b760fcfSWei Wang rt = rt_cache; 9422b760fcfSWei Wang 943d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 944d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 945d3843fe5SWei Wang 94666f5d6ceSWei Wang rcu_read_unlock(); 947b811580dSDavid Ahern 948b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 949b811580dSDavid Ahern 9501da177e4SLinus Torvalds return rt; 951c71099acSThomas Graf 952c71099acSThomas Graf } 953c71099acSThomas Graf 954ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 955ea6e574eSFlorian Westphal int flags) 956ea6e574eSFlorian Westphal { 957ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 958ea6e574eSFlorian Westphal } 959ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 960ea6e574eSFlorian Westphal 9619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9629acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 963c71099acSThomas Graf { 9644c9483b2SDavid S. Miller struct flowi6 fl6 = { 9654c9483b2SDavid S. Miller .flowi6_oif = oif, 9664c9483b2SDavid S. Miller .daddr = *daddr, 967c71099acSThomas Graf }; 968c71099acSThomas Graf struct dst_entry *dst; 96977d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 970c71099acSThomas Graf 971adaa70bbSThomas Graf if (saddr) { 9724c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 973adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 974adaa70bbSThomas Graf } 975adaa70bbSThomas Graf 9764c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 977c71099acSThomas Graf if (dst->error == 0) 978c71099acSThomas Graf return (struct rt6_info *) dst; 979c71099acSThomas Graf 980c71099acSThomas Graf dst_release(dst); 981c71099acSThomas Graf 9821da177e4SLinus Torvalds return NULL; 9831da177e4SLinus Torvalds } 9847159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9857159039aSYOSHIFUJI Hideaki 986c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9871cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9881cfb71eeSWei Wang * route is released. 9891cfb71eeSWei Wang * Caller must hold dst before calling it. 9901da177e4SLinus Torvalds */ 9911da177e4SLinus Torvalds 992e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 993333c4301SDavid Ahern struct mx6_config *mxc, 994333c4301SDavid Ahern struct netlink_ext_ack *extack) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds int err; 997c71099acSThomas Graf struct fib6_table *table; 9981da177e4SLinus Torvalds 999c71099acSThomas Graf table = rt->rt6i_table; 100066f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1001333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 100266f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds return err; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 100740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 100840e22e8fSThomas Graf { 1009e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 1010e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 1011e715b6d3SFlorian Westphal 10121cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 10131cfb71eeSWei Wang dst_hold(&rt->dst); 1014333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 101540e22e8fSThomas Graf } 101640e22e8fSThomas Graf 10174832c30dSDavid Ahern /* called with rcu_lock held */ 10184832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 10194832c30dSDavid Ahern { 10204832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 10214832c30dSDavid Ahern 10224832c30dSDavid Ahern if (rt->rt6i_flags & RTF_LOCAL) { 10234832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 10244832c30dSDavid Ahern * device if it is a master device, the master device if 10254832c30dSDavid Ahern * device is enslaved, and the loopback as the default 10264832c30dSDavid Ahern */ 10274832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 10284832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 10294832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 10304832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 10314832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 10324832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 10334832c30dSDavid Ahern * case we want dev returned to be dev 10344832c30dSDavid Ahern */ 10354832c30dSDavid Ahern } 10364832c30dSDavid Ahern 10374832c30dSDavid Ahern return dev; 10384832c30dSDavid Ahern } 10394832c30dSDavid Ahern 10408b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 104121efcfa0SEric Dumazet const struct in6_addr *daddr, 1042b71d1d42SEric Dumazet const struct in6_addr *saddr) 10431da177e4SLinus Torvalds { 10444832c30dSDavid Ahern struct net_device *dev; 10451da177e4SLinus Torvalds struct rt6_info *rt; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 10481da177e4SLinus Torvalds * Clone the route. 10491da177e4SLinus Torvalds */ 10501da177e4SLinus Torvalds 1051d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 105283a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 10531da177e4SLinus Torvalds 10544832c30dSDavid Ahern rcu_read_lock(); 10554832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10564832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10574832c30dSDavid Ahern rcu_read_unlock(); 105883a09abdSMartin KaFai Lau if (!rt) 105983a09abdSMartin KaFai Lau return NULL; 106083a09abdSMartin KaFai Lau 106183a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10628b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 106383a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 106483a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 106583a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 106683a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10678b9df265SMartin KaFai Lau 10688b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1069bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 107021efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 107158c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10721da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10731da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10744e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10751da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds #endif 107895a9a5baSYOSHIFUJI Hideaki } 107995a9a5baSYOSHIFUJI Hideaki 1080299d9939SYOSHIFUJI Hideaki return rt; 1081299d9939SYOSHIFUJI Hideaki } 1082299d9939SYOSHIFUJI Hideaki 1083d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1084d52d3997SMartin KaFai Lau { 10854832c30dSDavid Ahern struct net_device *dev; 1086d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1087d52d3997SMartin KaFai Lau 10884832c30dSDavid Ahern rcu_read_lock(); 10894832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10904832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10914832c30dSDavid Ahern rcu_read_unlock(); 1092d52d3997SMartin KaFai Lau if (!pcpu_rt) 1093d52d3997SMartin KaFai Lau return NULL; 1094d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1095d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1096d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1097d52d3997SMartin KaFai Lau return pcpu_rt; 1098d52d3997SMartin KaFai Lau } 1099d52d3997SMartin KaFai Lau 110066f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1101d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1102d52d3997SMartin KaFai Lau { 1103a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1104d52d3997SMartin KaFai Lau 1105d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1106d52d3997SMartin KaFai Lau pcpu_rt = *p; 1107d52d3997SMartin KaFai Lau 1108d3843fe5SWei Wang if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false)) 1109a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1110d3843fe5SWei Wang 1111a73e4195SMartin KaFai Lau return pcpu_rt; 1112a73e4195SMartin KaFai Lau } 1113a73e4195SMartin KaFai Lau 1114a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1115a73e4195SMartin KaFai Lau { 1116a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1117d52d3997SMartin KaFai Lau 1118d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1119d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1120d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1121d52d3997SMartin KaFai Lau 11229c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 11239c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1124d52d3997SMartin KaFai Lau } 1125d52d3997SMartin KaFai Lau 1126a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1127a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1128d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1129951f788aSEric Dumazet BUG_ON(prev); 1130a94b9367SWei Wang 1131d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1132d52d3997SMartin KaFai Lau return pcpu_rt; 1133d52d3997SMartin KaFai Lau } 1134d52d3997SMartin KaFai Lau 113535732d01SWei Wang /* exception hash table implementation 113635732d01SWei Wang */ 113735732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 113835732d01SWei Wang 113935732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 114035732d01SWei Wang * Caller must hold rt6_exception_lock 114135732d01SWei Wang */ 114235732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 114335732d01SWei Wang struct rt6_exception *rt6_ex) 114435732d01SWei Wang { 1145b2427e67SColin Ian King struct net *net; 114681eb8447SWei Wang 114735732d01SWei Wang if (!bucket || !rt6_ex) 114835732d01SWei Wang return; 1149b2427e67SColin Ian King 1150b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 115135732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 115235732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 115335732d01SWei Wang rt6_release(rt6_ex->rt6i); 115435732d01SWei Wang kfree_rcu(rt6_ex, rcu); 115535732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 115635732d01SWei Wang bucket->depth--; 115781eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache--; 115835732d01SWei Wang } 115935732d01SWei Wang 116035732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 116135732d01SWei Wang * Caller must hold rt6_exception_lock 116235732d01SWei Wang */ 116335732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 116435732d01SWei Wang { 116535732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 116635732d01SWei Wang 116735732d01SWei Wang if (!bucket) 116835732d01SWei Wang return; 116935732d01SWei Wang 117035732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 117135732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 117235732d01SWei Wang oldest = rt6_ex; 117335732d01SWei Wang } 117435732d01SWei Wang rt6_remove_exception(bucket, oldest); 117535732d01SWei Wang } 117635732d01SWei Wang 117735732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 117835732d01SWei Wang const struct in6_addr *src) 117935732d01SWei Wang { 118035732d01SWei Wang static u32 seed __read_mostly; 118135732d01SWei Wang u32 val; 118235732d01SWei Wang 118335732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 118435732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 118535732d01SWei Wang 118635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 118735732d01SWei Wang if (src) 118835732d01SWei Wang val = jhash(src, sizeof(*src), val); 118935732d01SWei Wang #endif 119035732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 119135732d01SWei Wang } 119235732d01SWei Wang 119335732d01SWei Wang /* Helper function to find the cached rt in the hash table 119435732d01SWei Wang * and update bucket pointer to point to the bucket for this 119535732d01SWei Wang * (daddr, saddr) pair 119635732d01SWei Wang * Caller must hold rt6_exception_lock 119735732d01SWei Wang */ 119835732d01SWei Wang static struct rt6_exception * 119935732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 120035732d01SWei Wang const struct in6_addr *daddr, 120135732d01SWei Wang const struct in6_addr *saddr) 120235732d01SWei Wang { 120335732d01SWei Wang struct rt6_exception *rt6_ex; 120435732d01SWei Wang u32 hval; 120535732d01SWei Wang 120635732d01SWei Wang if (!(*bucket) || !daddr) 120735732d01SWei Wang return NULL; 120835732d01SWei Wang 120935732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 121035732d01SWei Wang *bucket += hval; 121135732d01SWei Wang 121235732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 121335732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 121435732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 121535732d01SWei Wang 121635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 121735732d01SWei Wang if (matched && saddr) 121835732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 121935732d01SWei Wang #endif 122035732d01SWei Wang if (matched) 122135732d01SWei Wang return rt6_ex; 122235732d01SWei Wang } 122335732d01SWei Wang return NULL; 122435732d01SWei Wang } 122535732d01SWei Wang 122635732d01SWei Wang /* Helper function to find the cached rt in the hash table 122735732d01SWei Wang * and update bucket pointer to point to the bucket for this 122835732d01SWei Wang * (daddr, saddr) pair 122935732d01SWei Wang * Caller must hold rcu_read_lock() 123035732d01SWei Wang */ 123135732d01SWei Wang static struct rt6_exception * 123235732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 123335732d01SWei Wang const struct in6_addr *daddr, 123435732d01SWei Wang const struct in6_addr *saddr) 123535732d01SWei Wang { 123635732d01SWei Wang struct rt6_exception *rt6_ex; 123735732d01SWei Wang u32 hval; 123835732d01SWei Wang 123935732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 124035732d01SWei Wang 124135732d01SWei Wang if (!(*bucket) || !daddr) 124235732d01SWei Wang return NULL; 124335732d01SWei Wang 124435732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 124535732d01SWei Wang *bucket += hval; 124635732d01SWei Wang 124735732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 124835732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 124935732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 125035732d01SWei Wang 125135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 125235732d01SWei Wang if (matched && saddr) 125335732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 125435732d01SWei Wang #endif 125535732d01SWei Wang if (matched) 125635732d01SWei Wang return rt6_ex; 125735732d01SWei Wang } 125835732d01SWei Wang return NULL; 125935732d01SWei Wang } 126035732d01SWei Wang 126135732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 126235732d01SWei Wang struct rt6_info *ort) 126335732d01SWei Wang { 126481eb8447SWei Wang struct net *net = dev_net(ort->dst.dev); 126535732d01SWei Wang struct rt6_exception_bucket *bucket; 126635732d01SWei Wang struct in6_addr *src_key = NULL; 126735732d01SWei Wang struct rt6_exception *rt6_ex; 126835732d01SWei Wang int err = 0; 126935732d01SWei Wang 127035732d01SWei Wang /* ort can't be a cache or pcpu route */ 127135732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 127235732d01SWei Wang ort = (struct rt6_info *)ort->dst.from; 127335732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 127435732d01SWei Wang 127535732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 127635732d01SWei Wang 127735732d01SWei Wang if (ort->exception_bucket_flushed) { 127835732d01SWei Wang err = -EINVAL; 127935732d01SWei Wang goto out; 128035732d01SWei Wang } 128135732d01SWei Wang 128235732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 128335732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 128435732d01SWei Wang if (!bucket) { 128535732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 128635732d01SWei Wang GFP_ATOMIC); 128735732d01SWei Wang if (!bucket) { 128835732d01SWei Wang err = -ENOMEM; 128935732d01SWei Wang goto out; 129035732d01SWei Wang } 129135732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 129235732d01SWei Wang } 129335732d01SWei Wang 129435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 129535732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 129635732d01SWei Wang * and exception table is indexed by a hash of 129735732d01SWei Wang * both rt6i_dst and rt6i_src. 129835732d01SWei Wang * Otherwise, the exception table is indexed by 129935732d01SWei Wang * a hash of only rt6i_dst. 130035732d01SWei Wang */ 130135732d01SWei Wang if (ort->rt6i_src.plen) 130235732d01SWei Wang src_key = &nrt->rt6i_src.addr; 130335732d01SWei Wang #endif 130460006a48SWei Wang 130560006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 130660006a48SWei Wang * in rt6_remove_prefsrc() 130760006a48SWei Wang */ 130860006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1309f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1310f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1311f5bbe7eeSWei Wang * is less than ort's mtu value. 1312f5bbe7eeSWei Wang */ 1313f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1314f5bbe7eeSWei Wang err = -EINVAL; 1315f5bbe7eeSWei Wang goto out; 1316f5bbe7eeSWei Wang } 131760006a48SWei Wang 131835732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 131935732d01SWei Wang src_key); 132035732d01SWei Wang if (rt6_ex) 132135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 132235732d01SWei Wang 132335732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 132435732d01SWei Wang if (!rt6_ex) { 132535732d01SWei Wang err = -ENOMEM; 132635732d01SWei Wang goto out; 132735732d01SWei Wang } 132835732d01SWei Wang rt6_ex->rt6i = nrt; 132935732d01SWei Wang rt6_ex->stamp = jiffies; 133035732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 133135732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 133235732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 133335732d01SWei Wang bucket->depth++; 133481eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 133535732d01SWei Wang 133635732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 133735732d01SWei Wang rt6_exception_remove_oldest(bucket); 133835732d01SWei Wang 133935732d01SWei Wang out: 134035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 134135732d01SWei Wang 134235732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1343*b886d5f2SPaolo Abeni if (!err) { 134435732d01SWei Wang fib6_update_sernum(ort); 1345*b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1346*b886d5f2SPaolo Abeni } 134735732d01SWei Wang 134835732d01SWei Wang return err; 134935732d01SWei Wang } 135035732d01SWei Wang 135135732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 135235732d01SWei Wang { 135335732d01SWei Wang struct rt6_exception_bucket *bucket; 135435732d01SWei Wang struct rt6_exception *rt6_ex; 135535732d01SWei Wang struct hlist_node *tmp; 135635732d01SWei Wang int i; 135735732d01SWei Wang 135835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 135935732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 136035732d01SWei Wang rt->exception_bucket_flushed = 1; 136135732d01SWei Wang 136235732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 136335732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 136435732d01SWei Wang if (!bucket) 136535732d01SWei Wang goto out; 136635732d01SWei Wang 136735732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 136835732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 136935732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 137035732d01SWei Wang WARN_ON_ONCE(bucket->depth); 137135732d01SWei Wang bucket++; 137235732d01SWei Wang } 137335732d01SWei Wang 137435732d01SWei Wang out: 137535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 137635732d01SWei Wang } 137735732d01SWei Wang 137835732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 137935732d01SWei Wang * Caller has to hold rcu_read_lock() 138035732d01SWei Wang */ 138135732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 138235732d01SWei Wang struct in6_addr *daddr, 138335732d01SWei Wang struct in6_addr *saddr) 138435732d01SWei Wang { 138535732d01SWei Wang struct rt6_exception_bucket *bucket; 138635732d01SWei Wang struct in6_addr *src_key = NULL; 138735732d01SWei Wang struct rt6_exception *rt6_ex; 138835732d01SWei Wang struct rt6_info *res = NULL; 138935732d01SWei Wang 139035732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 139135732d01SWei Wang 139235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139335732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 139435732d01SWei Wang * and exception table is indexed by a hash of 139535732d01SWei Wang * both rt6i_dst and rt6i_src. 139635732d01SWei Wang * Otherwise, the exception table is indexed by 139735732d01SWei Wang * a hash of only rt6i_dst. 139835732d01SWei Wang */ 139935732d01SWei Wang if (rt->rt6i_src.plen) 140035732d01SWei Wang src_key = saddr; 140135732d01SWei Wang #endif 140235732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 140335732d01SWei Wang 140435732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 140535732d01SWei Wang res = rt6_ex->rt6i; 140635732d01SWei Wang 140735732d01SWei Wang return res; 140835732d01SWei Wang } 140935732d01SWei Wang 141035732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 141135732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 141235732d01SWei Wang { 141335732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 141435732d01SWei Wang struct rt6_exception_bucket *bucket; 141535732d01SWei Wang struct in6_addr *src_key = NULL; 141635732d01SWei Wang struct rt6_exception *rt6_ex; 141735732d01SWei Wang int err; 141835732d01SWei Wang 141935732d01SWei Wang if (!from || 1420442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 142135732d01SWei Wang return -EINVAL; 142235732d01SWei Wang 142335732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 142435732d01SWei Wang return -ENOENT; 142535732d01SWei Wang 142635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 142735732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 142835732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 142935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143035732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 143135732d01SWei Wang * and exception table is indexed by a hash of 143235732d01SWei Wang * both rt6i_dst and rt6i_src. 143335732d01SWei Wang * Otherwise, the exception table is indexed by 143435732d01SWei Wang * a hash of only rt6i_dst. 143535732d01SWei Wang */ 143635732d01SWei Wang if (from->rt6i_src.plen) 143735732d01SWei Wang src_key = &rt->rt6i_src.addr; 143835732d01SWei Wang #endif 143935732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 144035732d01SWei Wang &rt->rt6i_dst.addr, 144135732d01SWei Wang src_key); 144235732d01SWei Wang if (rt6_ex) { 144335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 144435732d01SWei Wang err = 0; 144535732d01SWei Wang } else { 144635732d01SWei Wang err = -ENOENT; 144735732d01SWei Wang } 144835732d01SWei Wang 144935732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 145035732d01SWei Wang return err; 145135732d01SWei Wang } 145235732d01SWei Wang 145335732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 145435732d01SWei Wang * refresh its stamp 145535732d01SWei Wang */ 145635732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 145735732d01SWei Wang { 145835732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 145935732d01SWei Wang struct rt6_exception_bucket *bucket; 146035732d01SWei Wang struct in6_addr *src_key = NULL; 146135732d01SWei Wang struct rt6_exception *rt6_ex; 146235732d01SWei Wang 146335732d01SWei Wang if (!from || 1464442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 146535732d01SWei Wang return; 146635732d01SWei Wang 146735732d01SWei Wang rcu_read_lock(); 146835732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 146935732d01SWei Wang 147035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 147135732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 147235732d01SWei Wang * and exception table is indexed by a hash of 147335732d01SWei Wang * both rt6i_dst and rt6i_src. 147435732d01SWei Wang * Otherwise, the exception table is indexed by 147535732d01SWei Wang * a hash of only rt6i_dst. 147635732d01SWei Wang */ 147735732d01SWei Wang if (from->rt6i_src.plen) 147835732d01SWei Wang src_key = &rt->rt6i_src.addr; 147935732d01SWei Wang #endif 148035732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 148135732d01SWei Wang &rt->rt6i_dst.addr, 148235732d01SWei Wang src_key); 148335732d01SWei Wang if (rt6_ex) 148435732d01SWei Wang rt6_ex->stamp = jiffies; 148535732d01SWei Wang 148635732d01SWei Wang rcu_read_unlock(); 148735732d01SWei Wang } 148835732d01SWei Wang 148960006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 149060006a48SWei Wang { 149160006a48SWei Wang struct rt6_exception_bucket *bucket; 149260006a48SWei Wang struct rt6_exception *rt6_ex; 149360006a48SWei Wang int i; 149460006a48SWei Wang 149560006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 149660006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 149760006a48SWei Wang 149860006a48SWei Wang if (bucket) { 149960006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 150060006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 150160006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 150260006a48SWei Wang } 150360006a48SWei Wang bucket++; 150460006a48SWei Wang } 150560006a48SWei Wang } 150660006a48SWei Wang } 150760006a48SWei Wang 1508f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu) 1509f5bbe7eeSWei Wang { 1510f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1511f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1512f5bbe7eeSWei Wang int i; 1513f5bbe7eeSWei Wang 1514f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1515f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1516f5bbe7eeSWei Wang 1517f5bbe7eeSWei Wang if (bucket) { 1518f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1519f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1520f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1521f5bbe7eeSWei Wang /* For RTF_CACHE with rt6i_pmtu == 0 1522f5bbe7eeSWei Wang * (i.e. a redirected route), 1523f5bbe7eeSWei Wang * the metrics of its rt->dst.from has already 1524f5bbe7eeSWei Wang * been updated. 1525f5bbe7eeSWei Wang */ 1526f5bbe7eeSWei Wang if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu) 1527f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1528f5bbe7eeSWei Wang } 1529f5bbe7eeSWei Wang bucket++; 1530f5bbe7eeSWei Wang } 1531f5bbe7eeSWei Wang } 1532f5bbe7eeSWei Wang } 1533f5bbe7eeSWei Wang 1534b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1535b16cb459SWei Wang 1536b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1537b16cb459SWei Wang struct in6_addr *gateway) 1538b16cb459SWei Wang { 1539b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1540b16cb459SWei Wang struct rt6_exception *rt6_ex; 1541b16cb459SWei Wang struct hlist_node *tmp; 1542b16cb459SWei Wang int i; 1543b16cb459SWei Wang 1544b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1545b16cb459SWei Wang return; 1546b16cb459SWei Wang 1547b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1548b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1549b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1550b16cb459SWei Wang 1551b16cb459SWei Wang if (bucket) { 1552b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1553b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1554b16cb459SWei Wang &bucket->chain, hlist) { 1555b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1556b16cb459SWei Wang 1557b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1558b16cb459SWei Wang RTF_CACHE_GATEWAY && 1559b16cb459SWei Wang ipv6_addr_equal(gateway, 1560b16cb459SWei Wang &entry->rt6i_gateway)) { 1561b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1562b16cb459SWei Wang } 1563b16cb459SWei Wang } 1564b16cb459SWei Wang bucket++; 1565b16cb459SWei Wang } 1566b16cb459SWei Wang } 1567b16cb459SWei Wang 1568b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1569b16cb459SWei Wang } 1570b16cb459SWei Wang 1571c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1572c757faa8SWei Wang struct rt6_exception *rt6_ex, 1573c757faa8SWei Wang struct fib6_gc_args *gc_args, 1574c757faa8SWei Wang unsigned long now) 1575c757faa8SWei Wang { 1576c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1577c757faa8SWei Wang 1578c757faa8SWei Wang if (atomic_read(&rt->dst.__refcnt) == 1 && 1579c757faa8SWei Wang time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1580c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1581c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1582c757faa8SWei Wang return; 1583c757faa8SWei Wang } else if (rt->rt6i_flags & RTF_GATEWAY) { 1584c757faa8SWei Wang struct neighbour *neigh; 1585c757faa8SWei Wang __u8 neigh_flags = 0; 1586c757faa8SWei Wang 1587c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1588c757faa8SWei Wang if (neigh) { 1589c757faa8SWei Wang neigh_flags = neigh->flags; 1590c757faa8SWei Wang neigh_release(neigh); 1591c757faa8SWei Wang } 1592c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1593c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1594c757faa8SWei Wang rt); 1595c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1596c757faa8SWei Wang return; 1597c757faa8SWei Wang } 1598c757faa8SWei Wang } 1599c757faa8SWei Wang gc_args->more++; 1600c757faa8SWei Wang } 1601c757faa8SWei Wang 1602c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1603c757faa8SWei Wang struct fib6_gc_args *gc_args, 1604c757faa8SWei Wang unsigned long now) 1605c757faa8SWei Wang { 1606c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1607c757faa8SWei Wang struct rt6_exception *rt6_ex; 1608c757faa8SWei Wang struct hlist_node *tmp; 1609c757faa8SWei Wang int i; 1610c757faa8SWei Wang 1611c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1612c757faa8SWei Wang return; 1613c757faa8SWei Wang 1614c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1615c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1616c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1617c757faa8SWei Wang 1618c757faa8SWei Wang if (bucket) { 1619c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1620c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1621c757faa8SWei Wang &bucket->chain, hlist) { 1622c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1623c757faa8SWei Wang gc_args, now); 1624c757faa8SWei Wang } 1625c757faa8SWei Wang bucket++; 1626c757faa8SWei Wang } 1627c757faa8SWei Wang } 1628c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1629c757faa8SWei Wang } 1630c757faa8SWei Wang 16319ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 16329ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 16331da177e4SLinus Torvalds { 1634367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 16352b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 1636c71099acSThomas Graf int strict = 0; 16371da177e4SLinus Torvalds 163877d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1639d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1640367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1641367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16421da177e4SLinus Torvalds 164366f5d6ceSWei Wang rcu_read_lock(); 16441da177e4SLinus Torvalds 16454c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1646367efcb9SMartin KaFai Lau saved_fn = fn; 16471da177e4SLinus Torvalds 1648ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1649ca254490SDavid Ahern oif = 0; 1650ca254490SDavid Ahern 1651a3c00e46SMartin KaFai Lau redo_rt6_select: 16528d1040e8SWei Wang rt = rt6_select(net, fn, oif, strict); 165352bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1654367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1655a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1656a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1657a3c00e46SMartin KaFai Lau if (fn) 1658a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1659367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1660367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1661367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1662367efcb9SMartin KaFai Lau fn = saved_fn; 1663367efcb9SMartin KaFai Lau goto redo_rt6_select; 1664367efcb9SMartin KaFai Lau } 1665a3c00e46SMartin KaFai Lau } 1666a3c00e46SMartin KaFai Lau 16672b760fcfSWei Wang /*Search through exception table */ 16682b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 16692b760fcfSWei Wang if (rt_cache) 16702b760fcfSWei Wang rt = rt_cache; 1671d52d3997SMartin KaFai Lau 1672d3843fe5SWei Wang if (rt == net->ipv6.ip6_null_entry) { 167366f5d6ceSWei Wang rcu_read_unlock(); 1674d3843fe5SWei Wang dst_hold(&rt->dst); 1675d3843fe5SWei Wang trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1676d3843fe5SWei Wang return rt; 1677d3843fe5SWei Wang } else if (rt->rt6i_flags & RTF_CACHE) { 1678d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1679d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1680d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1681d3843fe5SWei Wang } 168266f5d6ceSWei Wang rcu_read_unlock(); 1683b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1684d52d3997SMartin KaFai Lau return rt; 16853da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 16863da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 16873da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 16883da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 16893da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 16903da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 16913da59bd9SMartin KaFai Lau */ 1692c71099acSThomas Graf 16933da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 16943da59bd9SMartin KaFai Lau 1695d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1696d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1697d3843fe5SWei Wang } else { 169866f5d6ceSWei Wang rcu_read_unlock(); 1699d3843fe5SWei Wang uncached_rt = rt; 1700d3843fe5SWei Wang goto uncached_rt_out; 1701d3843fe5SWei Wang } 170266f5d6ceSWei Wang rcu_read_unlock(); 1703d52d3997SMartin KaFai Lau 17043da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 17053da59bd9SMartin KaFai Lau dst_release(&rt->dst); 17063da59bd9SMartin KaFai Lau 17071cfb71eeSWei Wang if (uncached_rt) { 17081cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 17091cfb71eeSWei Wang * No need for another dst_hold() 17101cfb71eeSWei Wang */ 17118d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 171281eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 17131cfb71eeSWei Wang } else { 17143da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 17153da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 17161cfb71eeSWei Wang } 1717b811580dSDavid Ahern 1718d3843fe5SWei Wang uncached_rt_out: 1719b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 17203da59bd9SMartin KaFai Lau return uncached_rt; 17213da59bd9SMartin KaFai Lau 1722d52d3997SMartin KaFai Lau } else { 1723d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1724d52d3997SMartin KaFai Lau 1725d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1726d52d3997SMartin KaFai Lau 1727d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1728951f788aSEric Dumazet local_bh_disable(); 1729d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1730d52d3997SMartin KaFai Lau 1731951f788aSEric Dumazet if (!pcpu_rt) { 1732a94b9367SWei Wang /* atomic_inc_not_zero() is needed when using rcu */ 1733a94b9367SWei Wang if (atomic_inc_not_zero(&rt->rt6i_ref)) { 1734951f788aSEric Dumazet /* No dst_hold() on rt is needed because grabbing 1735a94b9367SWei Wang * rt->rt6i_ref makes sure rt can't be released. 17369c7370a1SMartin KaFai Lau */ 17379c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 1738a94b9367SWei Wang rt6_release(rt); 1739a94b9367SWei Wang } else { 1740a94b9367SWei Wang /* rt is already removed from tree */ 1741a94b9367SWei Wang pcpu_rt = net->ipv6.ip6_null_entry; 1742a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1743a94b9367SWei Wang } 17449c7370a1SMartin KaFai Lau } 1745951f788aSEric Dumazet local_bh_enable(); 1746951f788aSEric Dumazet rcu_read_unlock(); 1747b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1748d52d3997SMartin KaFai Lau return pcpu_rt; 1749d52d3997SMartin KaFai Lau } 1750c71099acSThomas Graf } 17519ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1752c71099acSThomas Graf 17538ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 17544c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 17554acad72dSPavel Emelyanov { 17564c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 17574acad72dSPavel Emelyanov } 17584acad72dSPavel Emelyanov 1759d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 176072331bc0SShmulik Ladkani struct net_device *dev, 176172331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 176272331bc0SShmulik Ladkani { 176372331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 176472331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 176572331bc0SShmulik Ladkani 176672331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 176772331bc0SShmulik Ladkani } 1768d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 176972331bc0SShmulik Ladkani 177023aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 177123aebdacSJakub Sitnicki struct flow_keys *keys) 177223aebdacSJakub Sitnicki { 177323aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 177423aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 177523aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 177623aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 177723aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 177823aebdacSJakub Sitnicki 177923aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 178023aebdacSJakub Sitnicki goto out; 178123aebdacSJakub Sitnicki 178223aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 178323aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 178423aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 178523aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 178623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 178723aebdacSJakub Sitnicki goto out; 178823aebdacSJakub Sitnicki 178923aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 179023aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 179123aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 179223aebdacSJakub Sitnicki if (!inner_iph) 179323aebdacSJakub Sitnicki goto out; 179423aebdacSJakub Sitnicki 179523aebdacSJakub Sitnicki key_iph = inner_iph; 179623aebdacSJakub Sitnicki out: 179723aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 179823aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 179923aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 180023aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 180123aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 180223aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 180323aebdacSJakub Sitnicki } 180423aebdacSJakub Sitnicki 180523aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 180623aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 180723aebdacSJakub Sitnicki { 180823aebdacSJakub Sitnicki struct flow_keys hash_keys; 180923aebdacSJakub Sitnicki 181023aebdacSJakub Sitnicki if (skb) { 181123aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 181223aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 181323aebdacSJakub Sitnicki } 181423aebdacSJakub Sitnicki 181523aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 181623aebdacSJakub Sitnicki } 181723aebdacSJakub Sitnicki 1818c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1819c71099acSThomas Graf { 1820b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1821c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1822adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1823904af04dSJiri Benc struct ip_tunnel_info *tun_info; 18244c9483b2SDavid S. Miller struct flowi6 fl6 = { 1825e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 18264c9483b2SDavid S. Miller .daddr = iph->daddr, 18274c9483b2SDavid S. Miller .saddr = iph->saddr, 18286502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 18294c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 18304c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1831c71099acSThomas Graf }; 1832adaa70bbSThomas Graf 1833904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 183446fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1835904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 183623aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 183723aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 183806e9d040SJiri Benc skb_dst_drop(skb); 183972331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1840c71099acSThomas Graf } 1841c71099acSThomas Graf 18428ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 18434c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1844c71099acSThomas Graf { 18454c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1846c71099acSThomas Graf } 1847c71099acSThomas Graf 18486f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 18496f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1850c71099acSThomas Graf { 1851d46a9d67SDavid Ahern bool any_src; 1852c71099acSThomas Graf 18534c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 18544c1feac5SDavid Ahern struct dst_entry *dst; 18554c1feac5SDavid Ahern 18564c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1857ca254490SDavid Ahern if (dst) 1858ca254490SDavid Ahern return dst; 18594c1feac5SDavid Ahern } 1860ca254490SDavid Ahern 18611fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 18624dc27d1cSDavid McCullough 1863d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1864741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1865d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 186677d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1867c71099acSThomas Graf 1868d46a9d67SDavid Ahern if (!any_src) 1869adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 18700c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 18710c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1872adaa70bbSThomas Graf 18734c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 18741da177e4SLinus Torvalds } 18756f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 18761da177e4SLinus Torvalds 18772774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 187814e50e57SDavid S. Miller { 18795c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 18801dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 188114e50e57SDavid S. Miller struct dst_entry *new = NULL; 188214e50e57SDavid S. Miller 18831dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 188462cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 188514e50e57SDavid S. Miller if (rt) { 18860a1f5962SMartin KaFai Lau rt6_info_init(rt); 188781eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 18880a1f5962SMartin KaFai Lau 1889d8d1f30bSChangli Gao new = &rt->dst; 189014e50e57SDavid S. Miller new->__use = 1; 1891352e512cSHerbert Xu new->input = dst_discard; 1892ede2059dSEric W. Biederman new->output = dst_discard_out; 189314e50e57SDavid S. Miller 1894defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 189514e50e57SDavid S. Miller 18961dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 18974e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18980a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 189914e50e57SDavid S. Miller rt->rt6i_metric = 0; 190014e50e57SDavid S. Miller 190114e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 190214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 190314e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 190414e50e57SDavid S. Miller #endif 190514e50e57SDavid S. Miller } 190614e50e57SDavid S. Miller 190769ead7afSDavid S. Miller dst_release(dst_orig); 190869ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 190914e50e57SDavid S. Miller } 191014e50e57SDavid S. Miller 19111da177e4SLinus Torvalds /* 19121da177e4SLinus Torvalds * Destination cache support functions 19131da177e4SLinus Torvalds */ 19141da177e4SLinus Torvalds 19154b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 19164b32b5adSMartin KaFai Lau { 19174b32b5adSMartin KaFai Lau if (rt->dst.from && 19184b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 19194b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 19204b32b5adSMartin KaFai Lau } 19214b32b5adSMartin KaFai Lau 19223da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 19233da59bd9SMartin KaFai Lau { 192436143645SSteffen Klassert u32 rt_cookie = 0; 1925c5cff856SWei Wang 1926c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 19273da59bd9SMartin KaFai Lau return NULL; 19283da59bd9SMartin KaFai Lau 19293da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 19303da59bd9SMartin KaFai Lau return NULL; 19313da59bd9SMartin KaFai Lau 19323da59bd9SMartin KaFai Lau return &rt->dst; 19333da59bd9SMartin KaFai Lau } 19343da59bd9SMartin KaFai Lau 19353da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 19363da59bd9SMartin KaFai Lau { 19375973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 19385973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 19393da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 19403da59bd9SMartin KaFai Lau return &rt->dst; 19413da59bd9SMartin KaFai Lau else 19423da59bd9SMartin KaFai Lau return NULL; 19433da59bd9SMartin KaFai Lau } 19443da59bd9SMartin KaFai Lau 19451da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 19461da177e4SLinus Torvalds { 19471da177e4SLinus Torvalds struct rt6_info *rt; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 19501da177e4SLinus Torvalds 19516f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 19526f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 19536f3118b5SNicolas Dichtel * into this function always. 19546f3118b5SNicolas Dichtel */ 1955e3bc10bdSHannes Frederic Sowa 19564b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 19574b32b5adSMartin KaFai Lau 195802bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1959a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 19603da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 19613da59bd9SMartin KaFai Lau else 19623da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 19631da177e4SLinus Torvalds } 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 19661da177e4SLinus Torvalds { 19671da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds if (rt) { 197054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 197154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1972e0a1ad73SThomas Graf ip6_del_rt(rt); 197354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 19741da177e4SLinus Torvalds } 197554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 197654c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 197754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 197854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 197954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 198054c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 19841da177e4SLinus Torvalds { 19851da177e4SLinus Torvalds struct rt6_info *rt; 19861da177e4SLinus Torvalds 19873ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 19881da177e4SLinus Torvalds 1989adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 19901da177e4SLinus Torvalds if (rt) { 19911eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1992ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 19938e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 1994c5cff856SWei Wang } else { 1995c5cff856SWei Wang struct fib6_node *fn; 1996c5cff856SWei Wang 1997c5cff856SWei Wang rcu_read_lock(); 1998c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 1999c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2000c5cff856SWei Wang fn->fn_sernum = -1; 2001c5cff856SWei Wang rcu_read_unlock(); 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds } 20041eb4f758SHannes Frederic Sowa } 20051da177e4SLinus Torvalds 200645e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 200745e4fd26SMartin KaFai Lau { 200845e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 200945e4fd26SMartin KaFai Lau 201045e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 201145e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 201245e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 201345e4fd26SMartin KaFai Lau } 201445e4fd26SMartin KaFai Lau 20150d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 20160d3f6d29SMartin KaFai Lau { 20170d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 20184e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 20194e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 20200d3f6d29SMartin KaFai Lau } 20210d3f6d29SMartin KaFai Lau 202245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 202345e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 20241da177e4SLinus Torvalds { 20250dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 20261da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 20271da177e4SLinus Torvalds 202845e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 202945e4fd26SMartin KaFai Lau return; 203045e4fd26SMartin KaFai Lau 203119bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 203219bda36cSXin Long return; 203319bda36cSXin Long 203445e4fd26SMartin KaFai Lau if (iph) { 203545e4fd26SMartin KaFai Lau daddr = &iph->daddr; 203645e4fd26SMartin KaFai Lau saddr = &iph->saddr; 203745e4fd26SMartin KaFai Lau } else if (sk) { 203845e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 203945e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 204045e4fd26SMartin KaFai Lau } else { 20410dec879fSJulian Anastasov daddr = NULL; 20420dec879fSJulian Anastasov saddr = NULL; 20431da177e4SLinus Torvalds } 20440dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 20450dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 20460dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 20470dec879fSJulian Anastasov return; 20480dec879fSJulian Anastasov 20490dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 20500dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 20512b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 20522b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 20532b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 20540dec879fSJulian Anastasov } else if (daddr) { 20550dec879fSJulian Anastasov struct rt6_info *nrt6; 20560dec879fSJulian Anastasov 205745e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 205845e4fd26SMartin KaFai Lau if (nrt6) { 205945e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 20602b760fcfSWei Wang if (rt6_insert_exception(nrt6, rt6)) 20612b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 206245e4fd26SMartin KaFai Lau } 206345e4fd26SMartin KaFai Lau } 206445e4fd26SMartin KaFai Lau } 206545e4fd26SMartin KaFai Lau 206645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 206745e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 206845e4fd26SMartin KaFai Lau { 206945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 207242ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2073e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 207481aded24SDavid S. Miller { 207581aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 207681aded24SDavid S. Miller struct dst_entry *dst; 207781aded24SDavid S. Miller struct flowi6 fl6; 207881aded24SDavid S. Miller 207981aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 208081aded24SDavid S. Miller fl6.flowi6_oif = oif; 20811b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 208281aded24SDavid S. Miller fl6.daddr = iph->daddr; 208381aded24SDavid S. Miller fl6.saddr = iph->saddr; 20846502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2085e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 208681aded24SDavid S. Miller 208781aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 208881aded24SDavid S. Miller if (!dst->error) 208945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 209081aded24SDavid S. Miller dst_release(dst); 209181aded24SDavid S. Miller } 209281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 209381aded24SDavid S. Miller 209481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 209581aded24SDavid S. Miller { 209633c162a9SMartin KaFai Lau struct dst_entry *dst; 209733c162a9SMartin KaFai Lau 209881aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2099e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 210033c162a9SMartin KaFai Lau 210133c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 210233c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 210333c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 210433c162a9SMartin KaFai Lau return; 210533c162a9SMartin KaFai Lau 210633c162a9SMartin KaFai Lau bh_lock_sock(sk); 210733c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 210833c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 210933c162a9SMartin KaFai Lau bh_unlock_sock(sk); 211081aded24SDavid S. Miller } 211181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 211281aded24SDavid S. Miller 2113b55b76b2SDuan Jiong /* Handle redirects */ 2114b55b76b2SDuan Jiong struct ip6rd_flowi { 2115b55b76b2SDuan Jiong struct flowi6 fl6; 2116b55b76b2SDuan Jiong struct in6_addr gateway; 2117b55b76b2SDuan Jiong }; 2118b55b76b2SDuan Jiong 2119b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2120b55b76b2SDuan Jiong struct fib6_table *table, 2121b55b76b2SDuan Jiong struct flowi6 *fl6, 2122b55b76b2SDuan Jiong int flags) 2123b55b76b2SDuan Jiong { 2124b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 21252b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2126b55b76b2SDuan Jiong struct fib6_node *fn; 2127b55b76b2SDuan Jiong 2128b55b76b2SDuan Jiong /* Get the "current" route for this destination and 212967c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2130b55b76b2SDuan Jiong * 2131b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2132b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2133b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2134b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2135b55b76b2SDuan Jiong * routes. 2136b55b76b2SDuan Jiong */ 2137b55b76b2SDuan Jiong 213866f5d6ceSWei Wang rcu_read_lock(); 2139b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2140b55b76b2SDuan Jiong restart: 214166f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 2142b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2143b55b76b2SDuan Jiong continue; 2144b55b76b2SDuan Jiong if (rt->dst.error) 2145b55b76b2SDuan Jiong break; 2146b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2147b55b76b2SDuan Jiong continue; 2148b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2149b55b76b2SDuan Jiong continue; 21502b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 21512b760fcfSWei Wang * in the case of an ip redirect. 21522b760fcfSWei Wang * So we keep searching in the exception table if the gateway 21532b760fcfSWei Wang * is different. 21542b760fcfSWei Wang */ 21552b760fcfSWei Wang if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) { 21562b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 21572b760fcfSWei Wang &fl6->daddr, 21582b760fcfSWei Wang &fl6->saddr); 21592b760fcfSWei Wang if (rt_cache && 21602b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 21612b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 21622b760fcfSWei Wang rt = rt_cache; 21632b760fcfSWei Wang break; 21642b760fcfSWei Wang } 2165b55b76b2SDuan Jiong continue; 21662b760fcfSWei Wang } 2167b55b76b2SDuan Jiong break; 2168b55b76b2SDuan Jiong } 2169b55b76b2SDuan Jiong 2170b55b76b2SDuan Jiong if (!rt) 2171b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2172b55b76b2SDuan Jiong else if (rt->dst.error) { 2173b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2174b0a1ba59SMartin KaFai Lau goto out; 2175b0a1ba59SMartin KaFai Lau } 2176b0a1ba59SMartin KaFai Lau 2177b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2178a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2179a3c00e46SMartin KaFai Lau if (fn) 2180a3c00e46SMartin KaFai Lau goto restart; 2181b55b76b2SDuan Jiong } 2182a3c00e46SMartin KaFai Lau 2183b0a1ba59SMartin KaFai Lau out: 2184d3843fe5SWei Wang ip6_hold_safe(net, &rt, true); 2185b55b76b2SDuan Jiong 218666f5d6ceSWei Wang rcu_read_unlock(); 2187b55b76b2SDuan Jiong 2188b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 2189b55b76b2SDuan Jiong return rt; 2190b55b76b2SDuan Jiong }; 2191b55b76b2SDuan Jiong 2192b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2193b55b76b2SDuan Jiong const struct flowi6 *fl6, 2194b55b76b2SDuan Jiong const struct in6_addr *gateway) 2195b55b76b2SDuan Jiong { 2196b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2197b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2198b55b76b2SDuan Jiong 2199b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2200b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2201b55b76b2SDuan Jiong 2202b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2203b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2204b55b76b2SDuan Jiong } 2205b55b76b2SDuan Jiong 2206e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2207e2d118a1SLorenzo Colitti kuid_t uid) 22083a5ad2eeSDavid S. Miller { 22093a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 22103a5ad2eeSDavid S. Miller struct dst_entry *dst; 22113a5ad2eeSDavid S. Miller struct flowi6 fl6; 22123a5ad2eeSDavid S. Miller 22133a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2214e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 22153a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 22163a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 22173a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 22183a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 22196502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2220e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 22213a5ad2eeSDavid S. Miller 2222b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 22236700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 22243a5ad2eeSDavid S. Miller dst_release(dst); 22253a5ad2eeSDavid S. Miller } 22263a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 22273a5ad2eeSDavid S. Miller 2228c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2229c92a59ecSDuan Jiong u32 mark) 2230c92a59ecSDuan Jiong { 2231c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2232c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2233c92a59ecSDuan Jiong struct dst_entry *dst; 2234c92a59ecSDuan Jiong struct flowi6 fl6; 2235c92a59ecSDuan Jiong 2236c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2237e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2238c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2239c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2240c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2241c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2242e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2243c92a59ecSDuan Jiong 2244b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2245c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2246c92a59ecSDuan Jiong dst_release(dst); 2247c92a59ecSDuan Jiong } 2248c92a59ecSDuan Jiong 22493a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 22503a5ad2eeSDavid S. Miller { 2251e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2252e2d118a1SLorenzo Colitti sk->sk_uid); 22533a5ad2eeSDavid S. Miller } 22543a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 22553a5ad2eeSDavid S. Miller 22560dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 22571da177e4SLinus Torvalds { 22580dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 22590dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 22600dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 22610dbaee3bSDavid S. Miller 22621da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 22631da177e4SLinus Torvalds 22645578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 22655578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds /* 22681da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 22691da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 22701da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 22711da177e4SLinus Torvalds * rely only on pmtu discovery" 22721da177e4SLinus Torvalds */ 22731da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 22741da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 22751da177e4SLinus Torvalds return mtu; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 2278ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2279d33e4553SDavid S. Miller { 22804b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 22814b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2282d33e4553SDavid S. Miller struct inet6_dev *idev; 2283618f9bc7SSteffen Klassert 2284618f9bc7SSteffen Klassert if (mtu) 228530f78d8eSEric Dumazet goto out; 2286618f9bc7SSteffen Klassert 22874b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 22884b32b5adSMartin KaFai Lau if (mtu) 22894b32b5adSMartin KaFai Lau goto out; 22904b32b5adSMartin KaFai Lau 2291618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2292d33e4553SDavid S. Miller 2293d33e4553SDavid S. Miller rcu_read_lock(); 2294d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2295d33e4553SDavid S. Miller if (idev) 2296d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2297d33e4553SDavid S. Miller rcu_read_unlock(); 2298d33e4553SDavid S. Miller 229930f78d8eSEric Dumazet out: 230014972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 230114972cbdSRoopa Prabhu 230214972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2303d33e4553SDavid S. Miller } 2304d33e4553SDavid S. Miller 23053b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 230687a11578SDavid S. Miller struct flowi6 *fl6) 23071da177e4SLinus Torvalds { 230887a11578SDavid S. Miller struct dst_entry *dst; 23091da177e4SLinus Torvalds struct rt6_info *rt; 23101da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2311c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 23121da177e4SLinus Torvalds 231338308473SDavid S. Miller if (unlikely(!idev)) 2314122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 23151da177e4SLinus Torvalds 2316ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 231738308473SDavid S. Miller if (unlikely(!rt)) { 23181da177e4SLinus Torvalds in6_dev_put(idev); 231987a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 23201da177e4SLinus Torvalds goto out; 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds 23238e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 23248e2ec639SYan, Zheng rt->dst.output = ip6_output; 2325550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 232687a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 23278e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 23288e2ec639SYan, Zheng rt->rt6i_idev = idev; 232914edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 23301da177e4SLinus Torvalds 2331587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2332587fea74SWei Wang * do proper release of the net_device 2333587fea74SWei Wang */ 2334587fea74SWei Wang rt6_uncached_list_add(rt); 233581eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 23361da177e4SLinus Torvalds 233787a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 233887a11578SDavid S. Miller 23391da177e4SLinus Torvalds out: 234087a11578SDavid S. Miller return dst; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 2343569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 23441da177e4SLinus Torvalds { 234586393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 23467019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 23477019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 23487019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 23497019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 23507019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2351fc66f95cSEric Dumazet int entries; 23521da177e4SLinus Torvalds 2353fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 235449a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2355fc66f95cSEric Dumazet entries <= rt_max_size) 23561da177e4SLinus Torvalds goto out; 23571da177e4SLinus Torvalds 23586891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 235914956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2360fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2361fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 23627019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 23631da177e4SLinus Torvalds out: 23647019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2365fc66f95cSEric Dumazet return entries > rt_max_size; 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 2368e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2369e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2370e715b6d3SFlorian Westphal { 2371c3a8d947SDaniel Borkmann bool ecn_ca = false; 2372e715b6d3SFlorian Westphal struct nlattr *nla; 2373e715b6d3SFlorian Westphal int remaining; 2374e715b6d3SFlorian Westphal u32 *mp; 2375e715b6d3SFlorian Westphal 237663159f29SIan Morris if (!cfg->fc_mx) 2377e715b6d3SFlorian Westphal return 0; 2378e715b6d3SFlorian Westphal 2379e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2380e715b6d3SFlorian Westphal if (unlikely(!mp)) 2381e715b6d3SFlorian Westphal return -ENOMEM; 2382e715b6d3SFlorian Westphal 2383e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2384e715b6d3SFlorian Westphal int type = nla_type(nla); 2385ea697639SDaniel Borkmann u32 val; 2386ea697639SDaniel Borkmann 23871bb14807SDaniel Borkmann if (!type) 23881bb14807SDaniel Borkmann continue; 2389e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2390e715b6d3SFlorian Westphal goto err; 23911bb14807SDaniel Borkmann 2392ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2393ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2394e715b6d3SFlorian Westphal 2395ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 2396c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 2397ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2398ea697639SDaniel Borkmann goto err; 2399ea697639SDaniel Borkmann } else { 2400ea697639SDaniel Borkmann val = nla_get_u32(nla); 2401ea697639SDaniel Borkmann } 2402626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2403626abd59SPaolo Abeni val = 255; 2404b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2405b8d3e416SDaniel Borkmann goto err; 2406ea697639SDaniel Borkmann 2407ea697639SDaniel Borkmann mp[type - 1] = val; 2408e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2409e715b6d3SFlorian Westphal } 2410e715b6d3SFlorian Westphal 2411c3a8d947SDaniel Borkmann if (ecn_ca) { 2412c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2413c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2414c3a8d947SDaniel Borkmann } 2415e715b6d3SFlorian Westphal 2416c3a8d947SDaniel Borkmann mxc->mx = mp; 2417e715b6d3SFlorian Westphal return 0; 2418e715b6d3SFlorian Westphal err: 2419e715b6d3SFlorian Westphal kfree(mp); 2420e715b6d3SFlorian Westphal return -EINVAL; 2421e715b6d3SFlorian Westphal } 24221da177e4SLinus Torvalds 24238c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 24248c14586fSDavid Ahern struct fib6_config *cfg, 24258c14586fSDavid Ahern const struct in6_addr *gw_addr) 24268c14586fSDavid Ahern { 24278c14586fSDavid Ahern struct flowi6 fl6 = { 24288c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 24298c14586fSDavid Ahern .daddr = *gw_addr, 24308c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 24318c14586fSDavid Ahern }; 24328c14586fSDavid Ahern struct fib6_table *table; 24338c14586fSDavid Ahern struct rt6_info *rt; 2434d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 24358c14586fSDavid Ahern 24368c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 24378c14586fSDavid Ahern if (!table) 24388c14586fSDavid Ahern return NULL; 24398c14586fSDavid Ahern 24408c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 24418c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 24428c14586fSDavid Ahern 24438c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 24448c14586fSDavid Ahern 24458c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 24468c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 24478c14586fSDavid Ahern ip6_rt_put(rt); 24488c14586fSDavid Ahern rt = NULL; 24498c14586fSDavid Ahern } 24508c14586fSDavid Ahern 24518c14586fSDavid Ahern return rt; 24528c14586fSDavid Ahern } 24538c14586fSDavid Ahern 2454333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2455333c4301SDavid Ahern struct netlink_ext_ack *extack) 24561da177e4SLinus Torvalds { 24575578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 24581da177e4SLinus Torvalds struct rt6_info *rt = NULL; 24591da177e4SLinus Torvalds struct net_device *dev = NULL; 24601da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2461c71099acSThomas Graf struct fib6_table *table; 24621da177e4SLinus Torvalds int addr_type; 24638c5b83f0SRoopa Prabhu int err = -EINVAL; 24641da177e4SLinus Torvalds 2465557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2466d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2467d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2468557c44beSDavid Ahern goto out; 2469d5d531cbSDavid Ahern } 2470557c44beSDavid Ahern 2471d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2472d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 24738c5b83f0SRoopa Prabhu goto out; 2474d5d531cbSDavid Ahern } 2475d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2476d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2477d5d531cbSDavid Ahern goto out; 2478d5d531cbSDavid Ahern } 24791da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2480d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2481d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2482d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 24838c5b83f0SRoopa Prabhu goto out; 2484d5d531cbSDavid Ahern } 24851da177e4SLinus Torvalds #endif 248686872cb5SThomas Graf if (cfg->fc_ifindex) { 24871da177e4SLinus Torvalds err = -ENODEV; 24885578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 24891da177e4SLinus Torvalds if (!dev) 24901da177e4SLinus Torvalds goto out; 24911da177e4SLinus Torvalds idev = in6_dev_get(dev); 24921da177e4SLinus Torvalds if (!idev) 24931da177e4SLinus Torvalds goto out; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 249686872cb5SThomas Graf if (cfg->fc_metric == 0) 249786872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 24981da177e4SLinus Torvalds 2499c71099acSThomas Graf err = -ENOBUFS; 250038308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2501d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2502d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 250338308473SDavid S. Miller if (!table) { 2504f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2505d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2506d71314b4SMatti Vaittinen } 2507d71314b4SMatti Vaittinen } else { 2508d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2509d71314b4SMatti Vaittinen } 251038308473SDavid S. Miller 251138308473SDavid S. Miller if (!table) 2512c71099acSThomas Graf goto out; 2513c71099acSThomas Graf 2514ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2515ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 25161da177e4SLinus Torvalds 251738308473SDavid S. Miller if (!rt) { 25181da177e4SLinus Torvalds err = -ENOMEM; 25191da177e4SLinus Torvalds goto out; 25201da177e4SLinus Torvalds } 25211da177e4SLinus Torvalds 25221716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 25231716a961SGao feng rt6_set_expires(rt, jiffies + 25241716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 25251716a961SGao feng else 25261716a961SGao feng rt6_clean_expires(rt); 25271da177e4SLinus Torvalds 252886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 252986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 253086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 253186872cb5SThomas Graf 253286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2535d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2536ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2537ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 25381da177e4SLinus Torvalds else 2539d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 25401da177e4SLinus Torvalds 2541d8d1f30bSChangli Gao rt->dst.output = ip6_output; 25421da177e4SLinus Torvalds 254319e42e45SRoopa Prabhu if (cfg->fc_encap) { 254419e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 254519e42e45SRoopa Prabhu 254630357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2547127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 25489ae28727SDavid Ahern &lwtstate, extack); 254919e42e45SRoopa Prabhu if (err) 255019e42e45SRoopa Prabhu goto out; 255161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 255261adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 255361adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 255461adedf3SJiri Benc rt->dst.output = lwtunnel_output; 255519e42e45SRoopa Prabhu } 255661adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 255761adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 255861adedf3SJiri Benc rt->dst.input = lwtunnel_input; 255925368623STom Herbert } 256025368623STom Herbert } 256119e42e45SRoopa Prabhu 256286872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 256386872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2564afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 256511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 256886872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 256986872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 25701da177e4SLinus Torvalds #endif 25711da177e4SLinus Torvalds 257286872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 25751da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 25761da177e4SLinus Torvalds */ 257786872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 257838308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 257938308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 258038308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 25811da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 25825578689aSDaniel Lezcano if (dev != net->loopback_dev) { 25831da177e4SLinus Torvalds if (dev) { 25841da177e4SLinus Torvalds dev_put(dev); 25851da177e4SLinus Torvalds in6_dev_put(idev); 25861da177e4SLinus Torvalds } 25875578689aSDaniel Lezcano dev = net->loopback_dev; 25881da177e4SLinus Torvalds dev_hold(dev); 25891da177e4SLinus Torvalds idev = in6_dev_get(dev); 25901da177e4SLinus Torvalds if (!idev) { 25911da177e4SLinus Torvalds err = -ENODEV; 25921da177e4SLinus Torvalds goto out; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2596ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2597ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2598ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2599ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 26007150aedeSKamala R rt->dst.input = dst_discard; 2601ef2c7d7bSNicolas Dichtel break; 2602ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2603ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 26047150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 26057150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2606ef2c7d7bSNicolas Dichtel break; 2607b4949ab2SNicolas Dichtel case RTN_THROW: 26080315e382SNikola Forró case RTN_UNREACHABLE: 2609ef2c7d7bSNicolas Dichtel default: 26107150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 26110315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 26120315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 26137150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 26147150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2615ef2c7d7bSNicolas Dichtel break; 2616ef2c7d7bSNicolas Dichtel } 26171da177e4SLinus Torvalds goto install_route; 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds 262086872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2621b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 26221da177e4SLinus Torvalds int gwa_type; 26231da177e4SLinus Torvalds 262486872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2625330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 262648ed7b26SFlorian Westphal 262748ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 262848ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 262948ed7b26SFlorian Westphal * will return already-added prefix route via interface that 263048ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 263148ed7b26SFlorian Westphal */ 263248ed7b26SFlorian Westphal err = -EINVAL; 2633330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2634330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2635d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2636d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 263748ed7b26SFlorian Westphal goto out; 2638d5d531cbSDavid Ahern } 26394e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 26428c14586fSDavid Ahern struct rt6_info *grt = NULL; 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 26451da177e4SLinus Torvalds addresses as nexthop address. 26461da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 26471da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 26481da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 26491da177e4SLinus Torvalds some exceptions. --ANK 265096d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 265196d5822cSErik Nordmark addressing 26521da177e4SLinus Torvalds */ 265396d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2654d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2655d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2656d5d531cbSDavid Ahern "Invalid gateway address"); 26571da177e4SLinus Torvalds goto out; 2658d5d531cbSDavid Ahern } 26591da177e4SLinus Torvalds 2660a435a07fSVincent Bernat if (cfg->fc_table) { 26618c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 26628c14586fSDavid Ahern 2663a435a07fSVincent Bernat if (grt) { 2664a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2665a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2666a435a07fSVincent Bernat ip6_rt_put(grt); 2667a435a07fSVincent Bernat grt = NULL; 2668a435a07fSVincent Bernat } 2669a435a07fSVincent Bernat } 2670a435a07fSVincent Bernat } 2671a435a07fSVincent Bernat 26728c14586fSDavid Ahern if (!grt) 26738c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 26748c14586fSDavid Ahern cfg->fc_ifindex, 1); 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds err = -EHOSTUNREACH; 267738308473SDavid S. Miller if (!grt) 26781da177e4SLinus Torvalds goto out; 26791da177e4SLinus Torvalds if (dev) { 2680d1918542SDavid S. Miller if (dev != grt->dst.dev) { 268194e187c0SAmerigo Wang ip6_rt_put(grt); 26821da177e4SLinus Torvalds goto out; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds } else { 2685d1918542SDavid S. Miller dev = grt->dst.dev; 26861da177e4SLinus Torvalds idev = grt->rt6i_idev; 26871da177e4SLinus Torvalds dev_hold(dev); 26881da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 26911da177e4SLinus Torvalds err = 0; 269294e187c0SAmerigo Wang ip6_rt_put(grt); 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds if (err) 26951da177e4SLinus Torvalds goto out; 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds err = -EINVAL; 2698d5d531cbSDavid Ahern if (!dev) { 2699d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 27001da177e4SLinus Torvalds goto out; 2701d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2702d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2703d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2704d5d531cbSDavid Ahern goto out; 2705d5d531cbSDavid Ahern } 27061da177e4SLinus Torvalds } 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds err = -ENODEV; 270938308473SDavid S. Miller if (!dev) 27101da177e4SLinus Torvalds goto out; 27111da177e4SLinus Torvalds 2712c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2713c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2714d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2715c3968a85SDaniel Walter err = -EINVAL; 2716c3968a85SDaniel Walter goto out; 2717c3968a85SDaniel Walter } 27184e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2719c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2720c3968a85SDaniel Walter } else 2721c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2722c3968a85SDaniel Walter 272386872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds install_route: 2726d8d1f30bSChangli Gao rt->dst.dev = dev; 27271da177e4SLinus Torvalds rt->rt6i_idev = idev; 2728c71099acSThomas Graf rt->rt6i_table = table; 272963152fc0SDaniel Lezcano 2730c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 273163152fc0SDaniel Lezcano 27328c5b83f0SRoopa Prabhu return rt; 27331da177e4SLinus Torvalds out: 27341da177e4SLinus Torvalds if (dev) 27351da177e4SLinus Torvalds dev_put(dev); 27361da177e4SLinus Torvalds if (idev) 27371da177e4SLinus Torvalds in6_dev_put(idev); 2738587fea74SWei Wang if (rt) 2739587fea74SWei Wang dst_release_immediate(&rt->dst); 27406b9ea5a6SRoopa Prabhu 27418c5b83f0SRoopa Prabhu return ERR_PTR(err); 27426b9ea5a6SRoopa Prabhu } 27436b9ea5a6SRoopa Prabhu 2744333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2745333c4301SDavid Ahern struct netlink_ext_ack *extack) 27466b9ea5a6SRoopa Prabhu { 27476b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 27488c5b83f0SRoopa Prabhu struct rt6_info *rt; 27496b9ea5a6SRoopa Prabhu int err; 27506b9ea5a6SRoopa Prabhu 2751333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 27528c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 27538c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 27548c5b83f0SRoopa Prabhu rt = NULL; 27556b9ea5a6SRoopa Prabhu goto out; 27568c5b83f0SRoopa Prabhu } 27576b9ea5a6SRoopa Prabhu 27586b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 27596b9ea5a6SRoopa Prabhu if (err) 27606b9ea5a6SRoopa Prabhu goto out; 27616b9ea5a6SRoopa Prabhu 2762333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 27636b9ea5a6SRoopa Prabhu 27646b9ea5a6SRoopa Prabhu kfree(mxc.mx); 27656b9ea5a6SRoopa Prabhu 27666b9ea5a6SRoopa Prabhu return err; 27676b9ea5a6SRoopa Prabhu out: 2768587fea74SWei Wang if (rt) 2769587fea74SWei Wang dst_release_immediate(&rt->dst); 27706b9ea5a6SRoopa Prabhu 27711da177e4SLinus Torvalds return err; 27721da177e4SLinus Torvalds } 27731da177e4SLinus Torvalds 277486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 27751da177e4SLinus Torvalds { 27761da177e4SLinus Torvalds int err; 2777c71099acSThomas Graf struct fib6_table *table; 2778d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 27791da177e4SLinus Torvalds 2780a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 27816825a26cSGao feng err = -ENOENT; 27826825a26cSGao feng goto out; 27836825a26cSGao feng } 27846c813a72SPatrick McHardy 2785c71099acSThomas Graf table = rt->rt6i_table; 278666f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 278786872cb5SThomas Graf err = fib6_del(rt, info); 278866f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 27891da177e4SLinus Torvalds 27906825a26cSGao feng out: 279194e187c0SAmerigo Wang ip6_rt_put(rt); 27921da177e4SLinus Torvalds return err; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 2795e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2796e0a1ad73SThomas Graf { 27974d1169c1SDenis V. Lunev struct nl_info info = { 2798d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 27994d1169c1SDenis V. Lunev }; 2800528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2801e0a1ad73SThomas Graf } 2802e0a1ad73SThomas Graf 28030ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 28040ae81335SDavid Ahern { 28050ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2806e3330039SWANG Cong struct net *net = info->nl_net; 280716a16cd3SDavid Ahern struct sk_buff *skb = NULL; 28080ae81335SDavid Ahern struct fib6_table *table; 2809e3330039SWANG Cong int err = -ENOENT; 28100ae81335SDavid Ahern 2811e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2812e3330039SWANG Cong goto out_put; 28130ae81335SDavid Ahern table = rt->rt6i_table; 281466f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 28150ae81335SDavid Ahern 28160ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 28170ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 28180ae81335SDavid Ahern 281916a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 282016a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 282116a16cd3SDavid Ahern if (skb) { 282216a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 282316a16cd3SDavid Ahern 2824e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 282516a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 282616a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 282716a16cd3SDavid Ahern kfree_skb(skb); 282816a16cd3SDavid Ahern skb = NULL; 282916a16cd3SDavid Ahern } else 283016a16cd3SDavid Ahern info->skip_notify = 1; 283116a16cd3SDavid Ahern } 283216a16cd3SDavid Ahern 28330ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 28340ae81335SDavid Ahern &rt->rt6i_siblings, 28350ae81335SDavid Ahern rt6i_siblings) { 28360ae81335SDavid Ahern err = fib6_del(sibling, info); 28370ae81335SDavid Ahern if (err) 2838e3330039SWANG Cong goto out_unlock; 28390ae81335SDavid Ahern } 28400ae81335SDavid Ahern } 28410ae81335SDavid Ahern 28420ae81335SDavid Ahern err = fib6_del(rt, info); 2843e3330039SWANG Cong out_unlock: 284466f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 2845e3330039SWANG Cong out_put: 28460ae81335SDavid Ahern ip6_rt_put(rt); 284716a16cd3SDavid Ahern 284816a16cd3SDavid Ahern if (skb) { 2849e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 285016a16cd3SDavid Ahern info->nlh, gfp_any()); 285116a16cd3SDavid Ahern } 28520ae81335SDavid Ahern return err; 28530ae81335SDavid Ahern } 28540ae81335SDavid Ahern 2855333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2856333c4301SDavid Ahern struct netlink_ext_ack *extack) 28571da177e4SLinus Torvalds { 28582b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2859c71099acSThomas Graf struct fib6_table *table; 28601da177e4SLinus Torvalds struct fib6_node *fn; 28611da177e4SLinus Torvalds int err = -ESRCH; 28621da177e4SLinus Torvalds 28635578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2864d5d531cbSDavid Ahern if (!table) { 2865d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2866c71099acSThomas Graf return err; 2867d5d531cbSDavid Ahern } 28681da177e4SLinus Torvalds 286966f5d6ceSWei Wang rcu_read_lock(); 2870c71099acSThomas Graf 2871c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 287286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 287338fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 28742b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds if (fn) { 287766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 28782b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 28792b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 28802b760fcfSWei Wang &cfg->fc_src); 28812b760fcfSWei Wang if (!rt_cache) 28821f56a01fSMartin KaFai Lau continue; 28832b760fcfSWei Wang rt = rt_cache; 28842b760fcfSWei Wang } 288586872cb5SThomas Graf if (cfg->fc_ifindex && 2886d1918542SDavid S. Miller (!rt->dst.dev || 2887d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 28881da177e4SLinus Torvalds continue; 288986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 289086872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 28911da177e4SLinus Torvalds continue; 289286872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 28931da177e4SLinus Torvalds continue; 2894c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2895c2ed1880SMantas M continue; 2896d3843fe5SWei Wang if (!dst_hold_safe(&rt->dst)) 2897d3843fe5SWei Wang break; 289866f5d6ceSWei Wang rcu_read_unlock(); 28991da177e4SLinus Torvalds 29000ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 29010ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 290286872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 29030ae81335SDavid Ahern 29040ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds } 290766f5d6ceSWei Wang rcu_read_unlock(); 29081da177e4SLinus Torvalds 29091da177e4SLinus Torvalds return err; 29101da177e4SLinus Torvalds } 29111da177e4SLinus Torvalds 29126700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2913a6279458SYOSHIFUJI Hideaki { 2914a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2915e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2916e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2917e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2918e8599ff4SDavid S. Miller struct neighbour *neigh; 291971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 29206e157b6aSDavid S. Miller int optlen, on_link; 29216e157b6aSDavid S. Miller u8 *lladdr; 2922e8599ff4SDavid S. Miller 292329a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 292471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2925e8599ff4SDavid S. Miller 2926e8599ff4SDavid S. Miller if (optlen < 0) { 29276e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2928e8599ff4SDavid S. Miller return; 2929e8599ff4SDavid S. Miller } 2930e8599ff4SDavid S. Miller 293171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2932e8599ff4SDavid S. Miller 293371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 29346e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2935e8599ff4SDavid S. Miller return; 2936e8599ff4SDavid S. Miller } 2937e8599ff4SDavid S. Miller 29386e157b6aSDavid S. Miller on_link = 0; 293971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2940e8599ff4SDavid S. Miller on_link = 1; 294171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2942e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 29436e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2944e8599ff4SDavid S. Miller return; 2945e8599ff4SDavid S. Miller } 2946e8599ff4SDavid S. Miller 2947e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2948e8599ff4SDavid S. Miller if (!in6_dev) 2949e8599ff4SDavid S. Miller return; 2950e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2951e8599ff4SDavid S. Miller return; 2952e8599ff4SDavid S. Miller 2953e8599ff4SDavid S. Miller /* RFC2461 8.1: 2954e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2955e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2956e8599ff4SDavid S. Miller */ 2957e8599ff4SDavid S. Miller 2958f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2959e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2960e8599ff4SDavid S. Miller return; 2961e8599ff4SDavid S. Miller } 29626e157b6aSDavid S. Miller 29636e157b6aSDavid S. Miller lladdr = NULL; 2964e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2965e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2966e8599ff4SDavid S. Miller skb->dev); 2967e8599ff4SDavid S. Miller if (!lladdr) { 2968e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2969e8599ff4SDavid S. Miller return; 2970e8599ff4SDavid S. Miller } 2971e8599ff4SDavid S. Miller } 2972e8599ff4SDavid S. Miller 29736e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2974ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 29756e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 29766e157b6aSDavid S. Miller return; 29776e157b6aSDavid S. Miller } 29786e157b6aSDavid S. Miller 29796e157b6aSDavid S. Miller /* Redirect received -> path was valid. 29806e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 29816e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 29826e157b6aSDavid S. Miller */ 29830dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 29846e157b6aSDavid S. Miller 298571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2986e8599ff4SDavid S. Miller if (!neigh) 2987e8599ff4SDavid S. Miller return; 2988e8599ff4SDavid S. Miller 29891da177e4SLinus Torvalds /* 29901da177e4SLinus Torvalds * We have finally decided to accept it. 29911da177e4SLinus Torvalds */ 29921da177e4SLinus Torvalds 2993f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 29941da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 29951da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 29961da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2997f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2998f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 29991da177e4SLinus Torvalds 300083a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 300138308473SDavid S. Miller if (!nrt) 30021da177e4SLinus Torvalds goto out; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 30051da177e4SLinus Torvalds if (on_link) 30061da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 30071da177e4SLinus Torvalds 3008b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 30094e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 30101da177e4SLinus Torvalds 30112b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 30122b760fcfSWei Wang * a cached route because rt6_insert_exception() will 30132b760fcfSWei Wang * takes care of it 30142b760fcfSWei Wang */ 30152b760fcfSWei Wang if (rt6_insert_exception(nrt, rt)) { 30162b760fcfSWei Wang dst_release_immediate(&nrt->dst); 30172b760fcfSWei Wang goto out; 30182b760fcfSWei Wang } 30191da177e4SLinus Torvalds 3020d8d1f30bSChangli Gao netevent.old = &rt->dst; 3021d8d1f30bSChangli Gao netevent.new = &nrt->dst; 302271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 302360592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 30248d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 30258d71740cSTom Tucker 30261da177e4SLinus Torvalds out: 3027e8599ff4SDavid S. Miller neigh_release(neigh); 30286e157b6aSDavid S. Miller } 30296e157b6aSDavid S. Miller 30301da177e4SLinus Torvalds /* 30311da177e4SLinus Torvalds * Misc support functions 30321da177e4SLinus Torvalds */ 30331da177e4SLinus Torvalds 30344b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 30354b32b5adSMartin KaFai Lau { 30364b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 30374b32b5adSMartin KaFai Lau 30384b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 30394b32b5adSMartin KaFai Lau dst_hold(&from->dst); 30404b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 30414b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 30424b32b5adSMartin KaFai Lau } 30434b32b5adSMartin KaFai Lau 304483a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 30451da177e4SLinus Torvalds { 3046d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 3047d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 304883a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 3049d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 30501da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 30511da177e4SLinus Torvalds if (rt->rt6i_idev) 30521da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 3053d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 30544e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 30551716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 30561716a961SGao feng rt6_set_from(rt, ort); 305783a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 30581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 305983a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 30601da177e4SLinus Torvalds #endif 306183a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 3062c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 306361adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds 306670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 3067efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 3068b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3069830218c1SDavid Ahern const struct in6_addr *gwaddr, 3070830218c1SDavid Ahern struct net_device *dev) 307170ceb4f5SYOSHIFUJI Hideaki { 3072830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3073830218c1SDavid Ahern int ifindex = dev->ifindex; 307470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 307570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3076c71099acSThomas Graf struct fib6_table *table; 307770ceb4f5SYOSHIFUJI Hideaki 3078830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 307938308473SDavid S. Miller if (!table) 3080c71099acSThomas Graf return NULL; 3081c71099acSThomas Graf 308266f5d6ceSWei Wang rcu_read_lock(); 308338fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 308470ceb4f5SYOSHIFUJI Hideaki if (!fn) 308570ceb4f5SYOSHIFUJI Hideaki goto out; 308670ceb4f5SYOSHIFUJI Hideaki 308766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3088d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 308970ceb4f5SYOSHIFUJI Hideaki continue; 309070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 309170ceb4f5SYOSHIFUJI Hideaki continue; 309270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 309370ceb4f5SYOSHIFUJI Hideaki continue; 3094d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 309570ceb4f5SYOSHIFUJI Hideaki break; 309670ceb4f5SYOSHIFUJI Hideaki } 309770ceb4f5SYOSHIFUJI Hideaki out: 309866f5d6ceSWei Wang rcu_read_unlock(); 309970ceb4f5SYOSHIFUJI Hideaki return rt; 310070ceb4f5SYOSHIFUJI Hideaki } 310170ceb4f5SYOSHIFUJI Hideaki 3102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3103b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3104830218c1SDavid Ahern const struct in6_addr *gwaddr, 3105830218c1SDavid Ahern struct net_device *dev, 310695c96174SEric Dumazet unsigned int pref) 310770ceb4f5SYOSHIFUJI Hideaki { 310886872cb5SThomas Graf struct fib6_config cfg = { 3109238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3110830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 311186872cb5SThomas Graf .fc_dst_len = prefixlen, 311286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 311386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3114b91d5329SXin Long .fc_protocol = RTPROT_RA, 311515e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3116efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3117efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 311886872cb5SThomas Graf }; 311970ceb4f5SYOSHIFUJI Hideaki 3120830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 31214e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 31224e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 312386872cb5SThomas Graf 3124e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3125e317da96SYOSHIFUJI Hideaki if (!prefixlen) 312686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 312770ceb4f5SYOSHIFUJI Hideaki 3128333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 312970ceb4f5SYOSHIFUJI Hideaki 3130830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 313170ceb4f5SYOSHIFUJI Hideaki } 313270ceb4f5SYOSHIFUJI Hideaki #endif 313370ceb4f5SYOSHIFUJI Hideaki 3134b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 31351da177e4SLinus Torvalds { 3136830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 31371da177e4SLinus Torvalds struct rt6_info *rt; 3138c71099acSThomas Graf struct fib6_table *table; 31391da177e4SLinus Torvalds 3140830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 314138308473SDavid S. Miller if (!table) 3142c71099acSThomas Graf return NULL; 31431da177e4SLinus Torvalds 314466f5d6ceSWei Wang rcu_read_lock(); 314566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3146d1918542SDavid S. Miller if (dev == rt->dst.dev && 3147045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 31481da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 31491da177e4SLinus Torvalds break; 31501da177e4SLinus Torvalds } 31511da177e4SLinus Torvalds if (rt) 3152d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 315366f5d6ceSWei Wang rcu_read_unlock(); 31541da177e4SLinus Torvalds return rt; 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds 3157b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3158ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3159ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 31601da177e4SLinus Torvalds { 316186872cb5SThomas Graf struct fib6_config cfg = { 3162ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3163238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 316486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 316586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 316686872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3167b91d5329SXin Long .fc_protocol = RTPROT_RA, 316815e47304SEric W. Biederman .fc_nlinfo.portid = 0, 31695578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3170c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 317186872cb5SThomas Graf }; 31721da177e4SLinus Torvalds 31734e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 31741da177e4SLinus Torvalds 3175333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3176830218c1SDavid Ahern struct fib6_table *table; 3177830218c1SDavid Ahern 3178830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3179830218c1SDavid Ahern if (table) 3180830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3181830218c1SDavid Ahern } 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 31841da177e4SLinus Torvalds } 31851da177e4SLinus Torvalds 3186830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 31871da177e4SLinus Torvalds { 31881da177e4SLinus Torvalds struct rt6_info *rt; 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds restart: 319166f5d6ceSWei Wang rcu_read_lock(); 319266f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 31933e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 31943e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3195d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) { 319666f5d6ceSWei Wang rcu_read_unlock(); 3197e0a1ad73SThomas Graf ip6_del_rt(rt); 3198d3843fe5SWei Wang } else { 319966f5d6ceSWei Wang rcu_read_unlock(); 3200d3843fe5SWei Wang } 32011da177e4SLinus Torvalds goto restart; 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds } 320466f5d6ceSWei Wang rcu_read_unlock(); 3205830218c1SDavid Ahern 3206830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3207830218c1SDavid Ahern } 3208830218c1SDavid Ahern 3209830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3210830218c1SDavid Ahern { 3211830218c1SDavid Ahern struct fib6_table *table; 3212830218c1SDavid Ahern struct hlist_head *head; 3213830218c1SDavid Ahern unsigned int h; 3214830218c1SDavid Ahern 3215830218c1SDavid Ahern rcu_read_lock(); 3216830218c1SDavid Ahern 3217830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3218830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3219830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3220830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3221830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3222830218c1SDavid Ahern } 3223830218c1SDavid Ahern } 3224830218c1SDavid Ahern 3225830218c1SDavid Ahern rcu_read_unlock(); 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds 32285578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 32295578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 323086872cb5SThomas Graf struct fib6_config *cfg) 323186872cb5SThomas Graf { 323286872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 323386872cb5SThomas Graf 3234ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3235ca254490SDavid Ahern : RT6_TABLE_MAIN; 323686872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 323786872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 323886872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 323986872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 324086872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 324186872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 324286872cb5SThomas Graf 32435578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3244f1243c2dSBenjamin Thery 32454e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 32464e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 32474e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 324886872cb5SThomas Graf } 324986872cb5SThomas Graf 32505578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 32511da177e4SLinus Torvalds { 325286872cb5SThomas Graf struct fib6_config cfg; 32531da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 32541da177e4SLinus Torvalds int err; 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds switch (cmd) { 32571da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 32581da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3259af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 32601da177e4SLinus Torvalds return -EPERM; 32611da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 32621da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 32631da177e4SLinus Torvalds if (err) 32641da177e4SLinus Torvalds return -EFAULT; 32651da177e4SLinus Torvalds 32665578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 326786872cb5SThomas Graf 32681da177e4SLinus Torvalds rtnl_lock(); 32691da177e4SLinus Torvalds switch (cmd) { 32701da177e4SLinus Torvalds case SIOCADDRT: 3271333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 32721da177e4SLinus Torvalds break; 32731da177e4SLinus Torvalds case SIOCDELRT: 3274333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 32751da177e4SLinus Torvalds break; 32761da177e4SLinus Torvalds default: 32771da177e4SLinus Torvalds err = -EINVAL; 32781da177e4SLinus Torvalds } 32791da177e4SLinus Torvalds rtnl_unlock(); 32801da177e4SLinus Torvalds 32811da177e4SLinus Torvalds return err; 32823ff50b79SStephen Hemminger } 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds return -EINVAL; 32851da177e4SLinus Torvalds } 32861da177e4SLinus Torvalds 32871da177e4SLinus Torvalds /* 32881da177e4SLinus Torvalds * Drop the packet on the floor 32891da177e4SLinus Torvalds */ 32901da177e4SLinus Torvalds 3291d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 32921da177e4SLinus Torvalds { 3293612f09e8SYOSHIFUJI Hideaki int type; 3294adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3295612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3296612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 32970660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 329845bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 32993bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 33003bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3301612f09e8SYOSHIFUJI Hideaki break; 3302612f09e8SYOSHIFUJI Hideaki } 3303612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3304612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 33053bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 33063bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3307612f09e8SYOSHIFUJI Hideaki break; 3308612f09e8SYOSHIFUJI Hideaki } 33093ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 33101da177e4SLinus Torvalds kfree_skb(skb); 33111da177e4SLinus Torvalds return 0; 33121da177e4SLinus Torvalds } 33131da177e4SLinus Torvalds 33149ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 33159ce8ade0SThomas Graf { 3316612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 33179ce8ade0SThomas Graf } 33189ce8ade0SThomas Graf 3319ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33201da177e4SLinus Torvalds { 3321adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3322612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds 33259ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 33269ce8ade0SThomas Graf { 3327612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 33289ce8ade0SThomas Graf } 33299ce8ade0SThomas Graf 3330ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33319ce8ade0SThomas Graf { 3332adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3333612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 33349ce8ade0SThomas Graf } 33359ce8ade0SThomas Graf 33361da177e4SLinus Torvalds /* 33371da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 33381da177e4SLinus Torvalds */ 33391da177e4SLinus Torvalds 33401da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 33411da177e4SLinus Torvalds const struct in6_addr *addr, 33428f031519SDavid S. Miller bool anycast) 33431da177e4SLinus Torvalds { 3344ca254490SDavid Ahern u32 tb_id; 3345c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 33464832c30dSDavid Ahern struct net_device *dev = idev->dev; 33475f02ce24SDavid Ahern struct rt6_info *rt; 33485f02ce24SDavid Ahern 33495f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3350a3300ef4SHannes Frederic Sowa if (!rt) 33511da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 33521da177e4SLinus Torvalds 33531da177e4SLinus Torvalds in6_dev_hold(idev); 33541da177e4SLinus Torvalds 335511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3356d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3357d8d1f30bSChangli Gao rt->dst.output = ip6_output; 33581da177e4SLinus Torvalds rt->rt6i_idev = idev; 33591da177e4SLinus Torvalds 336094b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 33611da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 336258c4fb86SYOSHIFUJI Hideaki if (anycast) 336358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 336458c4fb86SYOSHIFUJI Hideaki else 33651da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 33661da177e4SLinus Torvalds 3367550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 33684e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 33691da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3370ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3371ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds return rt; 33741da177e4SLinus Torvalds } 33751da177e4SLinus Torvalds 3376c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3377c3968a85SDaniel Walter struct arg_dev_net_ip { 3378c3968a85SDaniel Walter struct net_device *dev; 3379c3968a85SDaniel Walter struct net *net; 3380c3968a85SDaniel Walter struct in6_addr *addr; 3381c3968a85SDaniel Walter }; 3382c3968a85SDaniel Walter 3383c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3384c3968a85SDaniel Walter { 3385c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3386c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3387c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3388c3968a85SDaniel Walter 3389d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3390c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3391c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 339260006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3393c3968a85SDaniel Walter /* remove prefsrc entry */ 3394c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 339560006a48SWei Wang /* need to update cache as well */ 339660006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 339760006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3398c3968a85SDaniel Walter } 3399c3968a85SDaniel Walter return 0; 3400c3968a85SDaniel Walter } 3401c3968a85SDaniel Walter 3402c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3403c3968a85SDaniel Walter { 3404c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3405c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3406c3968a85SDaniel Walter .dev = ifp->idev->dev, 3407c3968a85SDaniel Walter .net = net, 3408c3968a85SDaniel Walter .addr = &ifp->addr, 3409c3968a85SDaniel Walter }; 34100c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3411c3968a85SDaniel Walter } 3412c3968a85SDaniel Walter 3413be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3414be7a010dSDuan Jiong 3415be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3416be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3417be7a010dSDuan Jiong { 3418be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3419be7a010dSDuan Jiong 34202b760fcfSWei Wang if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3421be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3422be7a010dSDuan Jiong return -1; 3423be7a010dSDuan Jiong } 3424b16cb459SWei Wang 3425b16cb459SWei Wang /* Further clean up cached routes in exception table. 3426b16cb459SWei Wang * This is needed because cached route may have a different 3427b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3428b16cb459SWei Wang */ 3429b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3430b16cb459SWei Wang 3431be7a010dSDuan Jiong return 0; 3432be7a010dSDuan Jiong } 3433be7a010dSDuan Jiong 3434be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3435be7a010dSDuan Jiong { 3436be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3437be7a010dSDuan Jiong } 3438be7a010dSDuan Jiong 34398ed67789SDaniel Lezcano struct arg_dev_net { 34408ed67789SDaniel Lezcano struct net_device *dev; 34418ed67789SDaniel Lezcano struct net *net; 34428ed67789SDaniel Lezcano }; 34438ed67789SDaniel Lezcano 3444a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 34451da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 34461da177e4SLinus Torvalds { 3447bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3448bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 34498ed67789SDaniel Lezcano 3450d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3451a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3452a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 34538397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3454a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 34551da177e4SLinus Torvalds return -1; 3456c159d30cSDavid S. Miller 34571da177e4SLinus Torvalds return 0; 34581da177e4SLinus Torvalds } 34591da177e4SLinus Torvalds 3460f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 34611da177e4SLinus Torvalds { 34628ed67789SDaniel Lezcano struct arg_dev_net adn = { 34638ed67789SDaniel Lezcano .dev = dev, 34648ed67789SDaniel Lezcano .net = net, 34658ed67789SDaniel Lezcano }; 34668ed67789SDaniel Lezcano 34670c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3468e332bc67SEric W. Biederman if (dev) 34698d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 347295c96174SEric Dumazet struct rt6_mtu_change_arg { 34731da177e4SLinus Torvalds struct net_device *dev; 347495c96174SEric Dumazet unsigned int mtu; 34751da177e4SLinus Torvalds }; 34761da177e4SLinus Torvalds 34771da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 34781da177e4SLinus Torvalds { 34791da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 34801da177e4SLinus Torvalds struct inet6_dev *idev; 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 34831da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 34841da177e4SLinus Torvalds We still use this lock to block changes 34851da177e4SLinus Torvalds caused by addrconf/ndisc. 34861da177e4SLinus Torvalds */ 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 348938308473SDavid S. Miller if (!idev) 34901da177e4SLinus Torvalds return 0; 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 34931da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 34941da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 34951da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 34961da177e4SLinus Torvalds */ 34971da177e4SLinus Torvalds /* 34981da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 34991da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 35001da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 35011da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 35021da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 35031da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 350467c408cfSAlexander Alemayhu PMTU discovery. 35051da177e4SLinus Torvalds */ 3506d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3507fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 35084b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3509f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 35102b760fcfSWei Wang if (dst_mtu(&rt->dst) >= arg->mtu || 3511d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 35124b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3513defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3514566cfd8fSSimon Arlott } 3515f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3516f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 35174b32b5adSMartin KaFai Lau } 35181da177e4SLinus Torvalds return 0; 35191da177e4SLinus Torvalds } 35201da177e4SLinus Torvalds 352195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 35221da177e4SLinus Torvalds { 3523c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3524c71099acSThomas Graf .dev = dev, 3525c71099acSThomas Graf .mtu = mtu, 3526c71099acSThomas Graf }; 35271da177e4SLinus Torvalds 35280c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds 3531ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 35325176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 353386872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3534ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 353586872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 353686872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 353751ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3538c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 353919e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 354019e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 354132bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3542622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 35433b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 354486872cb5SThomas Graf }; 354586872cb5SThomas Graf 354686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3547333c4301SDavid Ahern struct fib6_config *cfg, 3548333c4301SDavid Ahern struct netlink_ext_ack *extack) 35491da177e4SLinus Torvalds { 355086872cb5SThomas Graf struct rtmsg *rtm; 355186872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3552c78ba6d6SLubomir Rintel unsigned int pref; 355386872cb5SThomas Graf int err; 35541da177e4SLinus Torvalds 3555fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3556fceb6435SJohannes Berg NULL); 355786872cb5SThomas Graf if (err < 0) 355886872cb5SThomas Graf goto errout; 35591da177e4SLinus Torvalds 356086872cb5SThomas Graf err = -EINVAL; 356186872cb5SThomas Graf rtm = nlmsg_data(nlh); 356286872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 356386872cb5SThomas Graf 356486872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 356586872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 356686872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 356786872cb5SThomas Graf cfg->fc_flags = RTF_UP; 356886872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3569ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 357086872cb5SThomas Graf 3571ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3572ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3573b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3574b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 357586872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 357686872cb5SThomas Graf 3577ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3578ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3579ab79ad14SMaciej Żenczykowski 35801f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 35811f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 35821f56a01fSMartin KaFai Lau 358315e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 358486872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 35853b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 358686872cb5SThomas Graf 358786872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 358867b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 358986872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 35901da177e4SLinus Torvalds } 359186872cb5SThomas Graf 359286872cb5SThomas Graf if (tb[RTA_DST]) { 359386872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 359486872cb5SThomas Graf 359586872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 359686872cb5SThomas Graf goto errout; 359786872cb5SThomas Graf 359886872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 35991da177e4SLinus Torvalds } 360086872cb5SThomas Graf 360186872cb5SThomas Graf if (tb[RTA_SRC]) { 360286872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 360386872cb5SThomas Graf 360486872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 360586872cb5SThomas Graf goto errout; 360686872cb5SThomas Graf 360786872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 36081da177e4SLinus Torvalds } 360986872cb5SThomas Graf 3610c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 361167b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3612c3968a85SDaniel Walter 361386872cb5SThomas Graf if (tb[RTA_OIF]) 361486872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 361586872cb5SThomas Graf 361686872cb5SThomas Graf if (tb[RTA_PRIORITY]) 361786872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 361886872cb5SThomas Graf 361986872cb5SThomas Graf if (tb[RTA_METRICS]) { 362086872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 362186872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 36221da177e4SLinus Torvalds } 362386872cb5SThomas Graf 362486872cb5SThomas Graf if (tb[RTA_TABLE]) 362586872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 362686872cb5SThomas Graf 362751ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 362851ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 362951ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 36309ed59592SDavid Ahern 36319ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3632c255bd68SDavid Ahern cfg->fc_mp_len, extack); 36339ed59592SDavid Ahern if (err < 0) 36349ed59592SDavid Ahern goto errout; 363551ebd318SNicolas Dichtel } 363651ebd318SNicolas Dichtel 3637c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3638c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3639c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3640c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3641c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3642c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3643c78ba6d6SLubomir Rintel } 3644c78ba6d6SLubomir Rintel 364519e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 364619e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 364719e42e45SRoopa Prabhu 36489ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 364919e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 365019e42e45SRoopa Prabhu 3651c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 36529ed59592SDavid Ahern if (err < 0) 36539ed59592SDavid Ahern goto errout; 36549ed59592SDavid Ahern } 36559ed59592SDavid Ahern 365632bc201eSXin Long if (tb[RTA_EXPIRES]) { 365732bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 365832bc201eSXin Long 365932bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 366032bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 366132bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 366232bc201eSXin Long } 366332bc201eSXin Long } 366432bc201eSXin Long 366586872cb5SThomas Graf err = 0; 366686872cb5SThomas Graf errout: 366786872cb5SThomas Graf return err; 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 36706b9ea5a6SRoopa Prabhu struct rt6_nh { 36716b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 36726b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36736b9ea5a6SRoopa Prabhu struct mx6_config mxc; 36746b9ea5a6SRoopa Prabhu struct list_head next; 36756b9ea5a6SRoopa Prabhu }; 36766b9ea5a6SRoopa Prabhu 36776b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 36786b9ea5a6SRoopa Prabhu { 36796b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36806b9ea5a6SRoopa Prabhu 36816b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36827d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 36836b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 36846b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 36856b9ea5a6SRoopa Prabhu } 36866b9ea5a6SRoopa Prabhu } 36876b9ea5a6SRoopa Prabhu 36886b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 36896b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 36906b9ea5a6SRoopa Prabhu { 36916b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36926b9ea5a6SRoopa Prabhu int err = -EEXIST; 36936b9ea5a6SRoopa Prabhu 36946b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36956b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3696f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 36976b9ea5a6SRoopa Prabhu return err; 36986b9ea5a6SRoopa Prabhu } 36996b9ea5a6SRoopa Prabhu 37006b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 37016b9ea5a6SRoopa Prabhu if (!nh) 37026b9ea5a6SRoopa Prabhu return -ENOMEM; 37036b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 37046b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 37056b9ea5a6SRoopa Prabhu if (err) { 37066b9ea5a6SRoopa Prabhu kfree(nh); 37076b9ea5a6SRoopa Prabhu return err; 37086b9ea5a6SRoopa Prabhu } 37096b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 37106b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 37116b9ea5a6SRoopa Prabhu 37126b9ea5a6SRoopa Prabhu return 0; 37136b9ea5a6SRoopa Prabhu } 37146b9ea5a6SRoopa Prabhu 37153b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 37163b1137feSDavid Ahern struct rt6_info *rt_last, 37173b1137feSDavid Ahern struct nl_info *info, 37183b1137feSDavid Ahern __u16 nlflags) 37193b1137feSDavid Ahern { 37203b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 37213b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 37223b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 37233b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 37243b1137feSDavid Ahern * the list, find the first sibling of the last route appended 37253b1137feSDavid Ahern */ 37263b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 37273b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 37283b1137feSDavid Ahern struct rt6_info, 37293b1137feSDavid Ahern rt6i_siblings); 37303b1137feSDavid Ahern } 37313b1137feSDavid Ahern 37323b1137feSDavid Ahern if (rt) 37333b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 37343b1137feSDavid Ahern } 37353b1137feSDavid Ahern 3736333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3737333c4301SDavid Ahern struct netlink_ext_ack *extack) 373851ebd318SNicolas Dichtel { 37393b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 37403b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 374151ebd318SNicolas Dichtel struct fib6_config r_cfg; 374251ebd318SNicolas Dichtel struct rtnexthop *rtnh; 37436b9ea5a6SRoopa Prabhu struct rt6_info *rt; 37446b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 37456b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 37463b1137feSDavid Ahern __u16 nlflags; 374751ebd318SNicolas Dichtel int remaining; 374851ebd318SNicolas Dichtel int attrlen; 37496b9ea5a6SRoopa Prabhu int err = 1; 37506b9ea5a6SRoopa Prabhu int nhn = 0; 37516b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 37526b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 37536b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 375451ebd318SNicolas Dichtel 37553b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 37563b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 37573b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 37583b1137feSDavid Ahern 375935f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 376051ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 376151ebd318SNicolas Dichtel 37626b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 37636b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 37646b9ea5a6SRoopa Prabhu */ 376551ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 376651ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 376751ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 376851ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 376951ebd318SNicolas Dichtel 377051ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 377151ebd318SNicolas Dichtel if (attrlen > 0) { 377251ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 377351ebd318SNicolas Dichtel 377451ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 377551ebd318SNicolas Dichtel if (nla) { 377667b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 377751ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 377851ebd318SNicolas Dichtel } 377919e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 378019e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 378119e42e45SRoopa Prabhu if (nla) 378219e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 378351ebd318SNicolas Dichtel } 37846b9ea5a6SRoopa Prabhu 3785333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 37868c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 37878c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 37888c5b83f0SRoopa Prabhu rt = NULL; 37896b9ea5a6SRoopa Prabhu goto cleanup; 37908c5b83f0SRoopa Prabhu } 37916b9ea5a6SRoopa Prabhu 37926b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 379351ebd318SNicolas Dichtel if (err) { 3794587fea74SWei Wang dst_release_immediate(&rt->dst); 37956b9ea5a6SRoopa Prabhu goto cleanup; 379651ebd318SNicolas Dichtel } 37976b9ea5a6SRoopa Prabhu 37986b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 379951ebd318SNicolas Dichtel } 38006b9ea5a6SRoopa Prabhu 38013b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 38023b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 38033b1137feSDavid Ahern * the full route when done 38043b1137feSDavid Ahern */ 38053b1137feSDavid Ahern info->skip_notify = 1; 38063b1137feSDavid Ahern 38076b9ea5a6SRoopa Prabhu err_nh = NULL; 38086b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 38093b1137feSDavid Ahern rt_last = nh->rt6_info; 3810333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 38113b1137feSDavid Ahern /* save reference to first route for notification */ 38123b1137feSDavid Ahern if (!rt_notif && !err) 38133b1137feSDavid Ahern rt_notif = nh->rt6_info; 38143b1137feSDavid Ahern 38156b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 38166b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 38176b9ea5a6SRoopa Prabhu if (err) { 38186b9ea5a6SRoopa Prabhu if (replace && nhn) 38196b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 38206b9ea5a6SRoopa Prabhu err_nh = nh; 38216b9ea5a6SRoopa Prabhu goto add_errout; 38226b9ea5a6SRoopa Prabhu } 38236b9ea5a6SRoopa Prabhu 38241a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 382527596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 382627596472SMichal Kubeček * we have already failed to add the first nexthop: 382727596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 382827596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 382927596472SMichal Kubeček * be added to it. 38301a72418bSNicolas Dichtel */ 383127596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 383227596472SMichal Kubeček NLM_F_REPLACE); 38336b9ea5a6SRoopa Prabhu nhn++; 38346b9ea5a6SRoopa Prabhu } 38356b9ea5a6SRoopa Prabhu 38363b1137feSDavid Ahern /* success ... tell user about new route */ 38373b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38386b9ea5a6SRoopa Prabhu goto cleanup; 38396b9ea5a6SRoopa Prabhu 38406b9ea5a6SRoopa Prabhu add_errout: 38413b1137feSDavid Ahern /* send notification for routes that were added so that 38423b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 38433b1137feSDavid Ahern * coherent 38443b1137feSDavid Ahern */ 38453b1137feSDavid Ahern if (rt_notif) 38463b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38473b1137feSDavid Ahern 38486b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 38496b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 38506b9ea5a6SRoopa Prabhu if (err_nh == nh) 38516b9ea5a6SRoopa Prabhu break; 3852333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 38536b9ea5a6SRoopa Prabhu } 38546b9ea5a6SRoopa Prabhu 38556b9ea5a6SRoopa Prabhu cleanup: 38566b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3857587fea74SWei Wang if (nh->rt6_info) 3858587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 38596b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 38606b9ea5a6SRoopa Prabhu list_del(&nh->next); 38616b9ea5a6SRoopa Prabhu kfree(nh); 38626b9ea5a6SRoopa Prabhu } 38636b9ea5a6SRoopa Prabhu 38646b9ea5a6SRoopa Prabhu return err; 38656b9ea5a6SRoopa Prabhu } 38666b9ea5a6SRoopa Prabhu 3867333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3868333c4301SDavid Ahern struct netlink_ext_ack *extack) 38696b9ea5a6SRoopa Prabhu { 38706b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 38716b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 38726b9ea5a6SRoopa Prabhu int remaining; 38736b9ea5a6SRoopa Prabhu int attrlen; 38746b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 38756b9ea5a6SRoopa Prabhu 38766b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 38776b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 38786b9ea5a6SRoopa Prabhu 38796b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 38806b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 38816b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 38826b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 38836b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 38846b9ea5a6SRoopa Prabhu 38856b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 38866b9ea5a6SRoopa Prabhu if (attrlen > 0) { 38876b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 38886b9ea5a6SRoopa Prabhu 38896b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 38906b9ea5a6SRoopa Prabhu if (nla) { 38916b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 38926b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 38936b9ea5a6SRoopa Prabhu } 38946b9ea5a6SRoopa Prabhu } 3895333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 38966b9ea5a6SRoopa Prabhu if (err) 38976b9ea5a6SRoopa Prabhu last_err = err; 38986b9ea5a6SRoopa Prabhu 389951ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 390051ebd318SNicolas Dichtel } 390151ebd318SNicolas Dichtel 390251ebd318SNicolas Dichtel return last_err; 390351ebd318SNicolas Dichtel } 390451ebd318SNicolas Dichtel 3905c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3906c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39071da177e4SLinus Torvalds { 390886872cb5SThomas Graf struct fib6_config cfg; 390986872cb5SThomas Graf int err; 39101da177e4SLinus Torvalds 3911333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 391286872cb5SThomas Graf if (err < 0) 391386872cb5SThomas Graf return err; 391486872cb5SThomas Graf 391551ebd318SNicolas Dichtel if (cfg.fc_mp) 3916333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 39170ae81335SDavid Ahern else { 39180ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3919333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 39201da177e4SLinus Torvalds } 39210ae81335SDavid Ahern } 39221da177e4SLinus Torvalds 3923c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3924c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39251da177e4SLinus Torvalds { 392686872cb5SThomas Graf struct fib6_config cfg; 392786872cb5SThomas Graf int err; 39281da177e4SLinus Torvalds 3929333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 393086872cb5SThomas Graf if (err < 0) 393186872cb5SThomas Graf return err; 393286872cb5SThomas Graf 393351ebd318SNicolas Dichtel if (cfg.fc_mp) 3934333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 393551ebd318SNicolas Dichtel else 3936333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 39371da177e4SLinus Torvalds } 39381da177e4SLinus Torvalds 3939beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3940339bf98fSThomas Graf { 3941beb1afacSDavid Ahern int nexthop_len = 0; 3942beb1afacSDavid Ahern 3943beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3944beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3945beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3946beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3947beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3948beb1afacSDavid Ahern 3949beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3950beb1afacSDavid Ahern } 3951beb1afacSDavid Ahern 3952339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3953339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3954339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3955339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3956339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3957339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3958339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3959339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3960339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 39616a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3962ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3963c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 396419e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3965beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3966beb1afacSDavid Ahern + nexthop_len; 3967beb1afacSDavid Ahern } 3968beb1afacSDavid Ahern 3969beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 39705be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3971beb1afacSDavid Ahern { 3972beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3973beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3974beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3975beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3976beb1afacSDavid Ahern } 3977beb1afacSDavid Ahern 3978beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3979beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3980beb1afacSDavid Ahern goto nla_put_failure; 3981beb1afacSDavid Ahern } 3982beb1afacSDavid Ahern 3983fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 398461e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 398561e4d01eSIdo Schimmel 39865be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 39875be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3988beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3989beb1afacSDavid Ahern goto nla_put_failure; 3990beb1afacSDavid Ahern 3991beb1afacSDavid Ahern if (rt->dst.lwtstate && 3992beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3993beb1afacSDavid Ahern goto nla_put_failure; 3994beb1afacSDavid Ahern 3995beb1afacSDavid Ahern return 0; 3996beb1afacSDavid Ahern 3997beb1afacSDavid Ahern nla_put_failure: 3998beb1afacSDavid Ahern return -EMSGSIZE; 3999beb1afacSDavid Ahern } 4000beb1afacSDavid Ahern 40015be083ceSDavid Ahern /* add multipath next hop */ 4002beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 4003beb1afacSDavid Ahern { 4004beb1afacSDavid Ahern struct rtnexthop *rtnh; 4005beb1afacSDavid Ahern unsigned int flags = 0; 4006beb1afacSDavid Ahern 4007beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 4008beb1afacSDavid Ahern if (!rtnh) 4009beb1afacSDavid Ahern goto nla_put_failure; 4010beb1afacSDavid Ahern 4011beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 4012beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 4013beb1afacSDavid Ahern 40145be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 4015beb1afacSDavid Ahern goto nla_put_failure; 4016beb1afacSDavid Ahern 4017beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 4018beb1afacSDavid Ahern 4019beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4020beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4021beb1afacSDavid Ahern 4022beb1afacSDavid Ahern return 0; 4023beb1afacSDavid Ahern 4024beb1afacSDavid Ahern nla_put_failure: 4025beb1afacSDavid Ahern return -EMSGSIZE; 4026339bf98fSThomas Graf } 4027339bf98fSThomas Graf 4028191cd582SBrian Haley static int rt6_fill_node(struct net *net, 4029191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 40300d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 403115e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4032f8cfe2ceSDavid Ahern unsigned int flags) 40331da177e4SLinus Torvalds { 40344b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 40351da177e4SLinus Torvalds struct rtmsg *rtm; 40361da177e4SLinus Torvalds struct nlmsghdr *nlh; 4037e3703b3dSThomas Graf long expires; 40389e762a4aSPatrick McHardy u32 table; 40391da177e4SLinus Torvalds 404015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 404138308473SDavid S. Miller if (!nlh) 404226932566SPatrick McHardy return -EMSGSIZE; 40432d7202bfSThomas Graf 40442d7202bfSThomas Graf rtm = nlmsg_data(nlh); 40451da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 40461da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 40471da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 40481da177e4SLinus Torvalds rtm->rtm_tos = 0; 4049c71099acSThomas Graf if (rt->rt6i_table) 40509e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 4051c71099acSThomas Graf else 40529e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 40539e762a4aSPatrick McHardy rtm->rtm_table = table; 4054c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4055c78679e8SDavid S. Miller goto nla_put_failure; 4056ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 4057ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4058ef2c7d7bSNicolas Dichtel case -EINVAL: 4059ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4060ef2c7d7bSNicolas Dichtel break; 4061ef2c7d7bSNicolas Dichtel case -EACCES: 4062ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4063ef2c7d7bSNicolas Dichtel break; 4064b4949ab2SNicolas Dichtel case -EAGAIN: 4065b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4066b4949ab2SNicolas Dichtel break; 4067ef2c7d7bSNicolas Dichtel default: 40681da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4069ef2c7d7bSNicolas Dichtel break; 4070ef2c7d7bSNicolas Dichtel } 4071ef2c7d7bSNicolas Dichtel } 4072ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4073ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 40744ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 40754ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4076d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 40771da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 40781da177e4SLinus Torvalds else 40791da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 40801da177e4SLinus Torvalds rtm->rtm_flags = 0; 40811da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 40821da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 40831da177e4SLinus Torvalds 40841da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 40851da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 40861da177e4SLinus Torvalds 40871da177e4SLinus Torvalds if (dst) { 4088930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4089c78679e8SDavid S. Miller goto nla_put_failure; 40901da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 40911da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4092930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4093c78679e8SDavid S. Miller goto nla_put_failure; 40941da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 40951da177e4SLinus Torvalds if (src) { 4096930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4097c78679e8SDavid S. Miller goto nla_put_failure; 40981da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4099c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4100930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4101c78679e8SDavid S. Miller goto nla_put_failure; 41021da177e4SLinus Torvalds #endif 41037bc570c8SYOSHIFUJI Hideaki if (iif) { 41047bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 41057bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4106fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 41072cf75070SNikolay Aleksandrov 41087bc570c8SYOSHIFUJI Hideaki if (err == 0) 41097bc570c8SYOSHIFUJI Hideaki return 0; 4110fd61c6baSDavid Ahern if (err < 0) 41117bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 41127bc570c8SYOSHIFUJI Hideaki } else 41137bc570c8SYOSHIFUJI Hideaki #endif 4114c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4115c78679e8SDavid S. Miller goto nla_put_failure; 41167bc570c8SYOSHIFUJI Hideaki } else if (dst) { 41171da177e4SLinus Torvalds struct in6_addr saddr_buf; 4118c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4119930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4120c78679e8SDavid S. Miller goto nla_put_failure; 4121c3968a85SDaniel Walter } 4122c3968a85SDaniel Walter 4123c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4124c3968a85SDaniel Walter struct in6_addr saddr_buf; 41254e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4126930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4127c78679e8SDavid S. Miller goto nla_put_failure; 41281da177e4SLinus Torvalds } 41292d7202bfSThomas Graf 41304b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 41314b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 41324b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 41334b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 41342d7202bfSThomas Graf goto nla_put_failure; 41352d7202bfSThomas Graf 4136beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4137beb1afacSDavid Ahern goto nla_put_failure; 4138beb1afacSDavid Ahern 4139beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4140beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4141beb1afacSDavid Ahern */ 4142beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4143beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4144beb1afacSDavid Ahern struct nlattr *mp; 4145beb1afacSDavid Ahern 4146beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4147beb1afacSDavid Ahern if (!mp) 4148beb1afacSDavid Ahern goto nla_put_failure; 4149beb1afacSDavid Ahern 4150beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4151beb1afacSDavid Ahern goto nla_put_failure; 4152beb1afacSDavid Ahern 4153beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4154beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4155beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 415694f826b8SEric Dumazet goto nla_put_failure; 415794f826b8SEric Dumazet } 41582d7202bfSThomas Graf 4159beb1afacSDavid Ahern nla_nest_end(skb, mp); 4160beb1afacSDavid Ahern } else { 41615be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4162c78679e8SDavid S. Miller goto nla_put_failure; 4163beb1afacSDavid Ahern } 41648253947eSLi Wei 41658253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 416669cdf8f9SYOSHIFUJI Hideaki 416787a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4168e3703b3dSThomas Graf goto nla_put_failure; 41691da177e4SLinus Torvalds 4170c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4171c78ba6d6SLubomir Rintel goto nla_put_failure; 4172c78ba6d6SLubomir Rintel 417319e42e45SRoopa Prabhu 4174053c095aSJohannes Berg nlmsg_end(skb, nlh); 4175053c095aSJohannes Berg return 0; 41762d7202bfSThomas Graf 41772d7202bfSThomas Graf nla_put_failure: 417826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 417926932566SPatrick McHardy return -EMSGSIZE; 41801da177e4SLinus Torvalds } 41811da177e4SLinus Torvalds 41821b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 41831da177e4SLinus Torvalds { 41841da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 41851f17e2f2SDavid Ahern struct net *net = arg->net; 41861f17e2f2SDavid Ahern 41871f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 41881f17e2f2SDavid Ahern return 0; 41891da177e4SLinus Torvalds 41902d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 41912d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4192f8cfe2ceSDavid Ahern 4193f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4194f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4195f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4196f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4197f8cfe2ceSDavid Ahern return 1; 4198f8cfe2ceSDavid Ahern } 4199f8cfe2ceSDavid Ahern } 42001da177e4SLinus Torvalds 42011f17e2f2SDavid Ahern return rt6_fill_node(net, 4202191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 420315e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4204f8cfe2ceSDavid Ahern NLM_F_MULTI); 42051da177e4SLinus Torvalds } 42061da177e4SLinus Torvalds 4207c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4208c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 42091da177e4SLinus Torvalds { 42103b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4211ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 421218c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 421318c3a61cSRoopa Prabhu struct dst_entry *dst; 42141da177e4SLinus Torvalds struct rt6_info *rt; 4215ab364a6fSThomas Graf struct sk_buff *skb; 4216ab364a6fSThomas Graf struct rtmsg *rtm; 42174c9483b2SDavid S. Miller struct flowi6 fl6; 421818c3a61cSRoopa Prabhu bool fibmatch; 4219ab364a6fSThomas Graf 4220fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4221c21ef3e3SDavid Ahern extack); 4222ab364a6fSThomas Graf if (err < 0) 4223ab364a6fSThomas Graf goto errout; 4224ab364a6fSThomas Graf 4225ab364a6fSThomas Graf err = -EINVAL; 42264c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 422738b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 422838b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 422918c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4230ab364a6fSThomas Graf 4231ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4232ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4233ab364a6fSThomas Graf goto errout; 4234ab364a6fSThomas Graf 42354e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4236ab364a6fSThomas Graf } 4237ab364a6fSThomas Graf 4238ab364a6fSThomas Graf if (tb[RTA_DST]) { 4239ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4240ab364a6fSThomas Graf goto errout; 4241ab364a6fSThomas Graf 42424e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4243ab364a6fSThomas Graf } 4244ab364a6fSThomas Graf 4245ab364a6fSThomas Graf if (tb[RTA_IIF]) 4246ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4247ab364a6fSThomas Graf 4248ab364a6fSThomas Graf if (tb[RTA_OIF]) 424972331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4250ab364a6fSThomas Graf 42512e47b291SLorenzo Colitti if (tb[RTA_MARK]) 42522e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 42532e47b291SLorenzo Colitti 4254622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4255622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4256622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4257622ec2c9SLorenzo Colitti else 4258622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4259622ec2c9SLorenzo Colitti 4260ab364a6fSThomas Graf if (iif) { 4261ab364a6fSThomas Graf struct net_device *dev; 426272331bc0SShmulik Ladkani int flags = 0; 426372331bc0SShmulik Ladkani 4264121622dbSFlorian Westphal rcu_read_lock(); 4265121622dbSFlorian Westphal 4266121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4267ab364a6fSThomas Graf if (!dev) { 4268121622dbSFlorian Westphal rcu_read_unlock(); 4269ab364a6fSThomas Graf err = -ENODEV; 4270ab364a6fSThomas Graf goto errout; 4271ab364a6fSThomas Graf } 427272331bc0SShmulik Ladkani 427372331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 427472331bc0SShmulik Ladkani 427572331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 427672331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 427772331bc0SShmulik Ladkani 427818c3a61cSRoopa Prabhu if (!fibmatch) 427918c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4280401481e0SArnd Bergmann else 4281401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4282121622dbSFlorian Westphal 4283121622dbSFlorian Westphal rcu_read_unlock(); 428472331bc0SShmulik Ladkani } else { 428572331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 428672331bc0SShmulik Ladkani 428718c3a61cSRoopa Prabhu if (!fibmatch) 428818c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4289401481e0SArnd Bergmann else 4290401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 429118c3a61cSRoopa Prabhu } 429218c3a61cSRoopa Prabhu 429318c3a61cSRoopa Prabhu 429418c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 429518c3a61cSRoopa Prabhu if (rt->dst.error) { 429618c3a61cSRoopa Prabhu err = rt->dst.error; 429718c3a61cSRoopa Prabhu ip6_rt_put(rt); 429818c3a61cSRoopa Prabhu goto errout; 4299ab364a6fSThomas Graf } 43001da177e4SLinus Torvalds 43019d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 43029d6acb3bSWANG Cong err = rt->dst.error; 43039d6acb3bSWANG Cong ip6_rt_put(rt); 43049d6acb3bSWANG Cong goto errout; 43059d6acb3bSWANG Cong } 43069d6acb3bSWANG Cong 43071da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 430838308473SDavid S. Miller if (!skb) { 430994e187c0SAmerigo Wang ip6_rt_put(rt); 4310ab364a6fSThomas Graf err = -ENOBUFS; 4311ab364a6fSThomas Graf goto errout; 4312ab364a6fSThomas Graf } 43131da177e4SLinus Torvalds 4314d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 431518c3a61cSRoopa Prabhu if (fibmatch) 431618c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 431718c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 431818c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 431918c3a61cSRoopa Prabhu else 43204c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 432115e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4322f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 43231da177e4SLinus Torvalds if (err < 0) { 4324ab364a6fSThomas Graf kfree_skb(skb); 4325ab364a6fSThomas Graf goto errout; 43261da177e4SLinus Torvalds } 43271da177e4SLinus Torvalds 432815e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4329ab364a6fSThomas Graf errout: 43301da177e4SLinus Torvalds return err; 43311da177e4SLinus Torvalds } 43321da177e4SLinus Torvalds 433337a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 433437a1d361SRoopa Prabhu unsigned int nlm_flags) 43351da177e4SLinus Torvalds { 43361da177e4SLinus Torvalds struct sk_buff *skb; 43375578689aSDaniel Lezcano struct net *net = info->nl_net; 4338528c4cebSDenis V. Lunev u32 seq; 4339528c4cebSDenis V. Lunev int err; 43400d51aa80SJamal Hadi Salim 4341528c4cebSDenis V. Lunev err = -ENOBUFS; 434238308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 434386872cb5SThomas Graf 434419e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 434538308473SDavid S. Miller if (!skb) 434621713ebcSThomas Graf goto errout; 43471da177e4SLinus Torvalds 4348191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4349f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 435026932566SPatrick McHardy if (err < 0) { 435126932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 435226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 435326932566SPatrick McHardy kfree_skb(skb); 435426932566SPatrick McHardy goto errout; 435526932566SPatrick McHardy } 435615e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 43575578689aSDaniel Lezcano info->nlh, gfp_any()); 43581ce85fe4SPablo Neira Ayuso return; 435921713ebcSThomas Graf errout: 436021713ebcSThomas Graf if (err < 0) 43615578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 43621da177e4SLinus Torvalds } 43631da177e4SLinus Torvalds 43648ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4365351638e7SJiri Pirko unsigned long event, void *ptr) 43668ed67789SDaniel Lezcano { 4367351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4368c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 43698ed67789SDaniel Lezcano 4370242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4371242d3a49SWANG Cong return NOTIFY_OK; 4372242d3a49SWANG Cong 4373242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4374d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 43758ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 43768ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4377d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 43788ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4379d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 43808ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 43818ed67789SDaniel Lezcano #endif 438276da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 438376da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 438476da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 438576da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 438676da0704SWANG Cong */ 438712d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4388242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 438912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 439012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4391242d3a49SWANG Cong #endif 43928ed67789SDaniel Lezcano } 43938ed67789SDaniel Lezcano 43948ed67789SDaniel Lezcano return NOTIFY_OK; 43958ed67789SDaniel Lezcano } 43968ed67789SDaniel Lezcano 43971da177e4SLinus Torvalds /* 43981da177e4SLinus Torvalds * /proc 43991da177e4SLinus Torvalds */ 44001da177e4SLinus Torvalds 44011da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 44021da177e4SLinus Torvalds 440333120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 440433120b30SAlexey Dobriyan .owner = THIS_MODULE, 440533120b30SAlexey Dobriyan .open = ipv6_route_open, 440633120b30SAlexey Dobriyan .read = seq_read, 440733120b30SAlexey Dobriyan .llseek = seq_lseek, 44088d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 440933120b30SAlexey Dobriyan }; 441033120b30SAlexey Dobriyan 44111da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 44121da177e4SLinus Torvalds { 441369ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 44141da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 441569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 441669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 441781eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 441869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 441969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4420fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 442169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 44221da177e4SLinus Torvalds 44231da177e4SLinus Torvalds return 0; 44241da177e4SLinus Torvalds } 44251da177e4SLinus Torvalds 44261da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 44271da177e4SLinus Torvalds { 4428de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 442969ddb805SDaniel Lezcano } 443069ddb805SDaniel Lezcano 44319a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 44321da177e4SLinus Torvalds .owner = THIS_MODULE, 44331da177e4SLinus Torvalds .open = rt6_stats_seq_open, 44341da177e4SLinus Torvalds .read = seq_read, 44351da177e4SLinus Torvalds .llseek = seq_lseek, 4436b6fcbdb4SPavel Emelyanov .release = single_release_net, 44371da177e4SLinus Torvalds }; 44381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 44391da177e4SLinus Torvalds 44401da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 44411da177e4SLinus Torvalds 44421da177e4SLinus Torvalds static 4443fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 44441da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 44451da177e4SLinus Torvalds { 4446c486da34SLucian Adrian Grijincu struct net *net; 4447c486da34SLucian Adrian Grijincu int delay; 4448c486da34SLucian Adrian Grijincu if (!write) 4449c486da34SLucian Adrian Grijincu return -EINVAL; 4450c486da34SLucian Adrian Grijincu 4451c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4452c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 44538d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 44542ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 44551da177e4SLinus Torvalds return 0; 44561da177e4SLinus Torvalds } 44571da177e4SLinus Torvalds 4458fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 44591da177e4SLinus Torvalds { 44601da177e4SLinus Torvalds .procname = "flush", 44614990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 44621da177e4SLinus Torvalds .maxlen = sizeof(int), 446389c8b3a1SDave Jones .mode = 0200, 44646d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 44651da177e4SLinus Torvalds }, 44661da177e4SLinus Torvalds { 44671da177e4SLinus Torvalds .procname = "gc_thresh", 44689a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 44691da177e4SLinus Torvalds .maxlen = sizeof(int), 44701da177e4SLinus Torvalds .mode = 0644, 44716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44721da177e4SLinus Torvalds }, 44731da177e4SLinus Torvalds { 44741da177e4SLinus Torvalds .procname = "max_size", 44754990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 44761da177e4SLinus Torvalds .maxlen = sizeof(int), 44771da177e4SLinus Torvalds .mode = 0644, 44786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44791da177e4SLinus Torvalds }, 44801da177e4SLinus Torvalds { 44811da177e4SLinus Torvalds .procname = "gc_min_interval", 44824990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44831da177e4SLinus Torvalds .maxlen = sizeof(int), 44841da177e4SLinus Torvalds .mode = 0644, 44856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44861da177e4SLinus Torvalds }, 44871da177e4SLinus Torvalds { 44881da177e4SLinus Torvalds .procname = "gc_timeout", 44894990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 44901da177e4SLinus Torvalds .maxlen = sizeof(int), 44911da177e4SLinus Torvalds .mode = 0644, 44926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44931da177e4SLinus Torvalds }, 44941da177e4SLinus Torvalds { 44951da177e4SLinus Torvalds .procname = "gc_interval", 44964990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 44971da177e4SLinus Torvalds .maxlen = sizeof(int), 44981da177e4SLinus Torvalds .mode = 0644, 44996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45001da177e4SLinus Torvalds }, 45011da177e4SLinus Torvalds { 45021da177e4SLinus Torvalds .procname = "gc_elasticity", 45034990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 45041da177e4SLinus Torvalds .maxlen = sizeof(int), 45051da177e4SLinus Torvalds .mode = 0644, 4506f3d3f616SMin Zhang .proc_handler = proc_dointvec, 45071da177e4SLinus Torvalds }, 45081da177e4SLinus Torvalds { 45091da177e4SLinus Torvalds .procname = "mtu_expires", 45104990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 45111da177e4SLinus Torvalds .maxlen = sizeof(int), 45121da177e4SLinus Torvalds .mode = 0644, 45136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45141da177e4SLinus Torvalds }, 45151da177e4SLinus Torvalds { 45161da177e4SLinus Torvalds .procname = "min_adv_mss", 45174990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 45181da177e4SLinus Torvalds .maxlen = sizeof(int), 45191da177e4SLinus Torvalds .mode = 0644, 4520f3d3f616SMin Zhang .proc_handler = proc_dointvec, 45211da177e4SLinus Torvalds }, 45221da177e4SLinus Torvalds { 45231da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 45244990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 45251da177e4SLinus Torvalds .maxlen = sizeof(int), 45261da177e4SLinus Torvalds .mode = 0644, 45276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 45281da177e4SLinus Torvalds }, 4529f8572d8fSEric W. Biederman { } 45301da177e4SLinus Torvalds }; 45311da177e4SLinus Torvalds 45322c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4533760f2d01SDaniel Lezcano { 4534760f2d01SDaniel Lezcano struct ctl_table *table; 4535760f2d01SDaniel Lezcano 4536760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4537760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4538760f2d01SDaniel Lezcano GFP_KERNEL); 45395ee09105SYOSHIFUJI Hideaki 45405ee09105SYOSHIFUJI Hideaki if (table) { 45415ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4542c486da34SLucian Adrian Grijincu table[0].extra1 = net; 454386393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 45445ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 45455ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 45465ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 45475ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 45485ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 45495ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 45505ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 45519c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4552464dc801SEric W. Biederman 4553464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4554464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4555464dc801SEric W. Biederman table[0].procname = NULL; 45565ee09105SYOSHIFUJI Hideaki } 45575ee09105SYOSHIFUJI Hideaki 4558760f2d01SDaniel Lezcano return table; 4559760f2d01SDaniel Lezcano } 45601da177e4SLinus Torvalds #endif 45611da177e4SLinus Torvalds 45622c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4563cdb18761SDaniel Lezcano { 4564633d424bSPavel Emelyanov int ret = -ENOMEM; 45658ed67789SDaniel Lezcano 456686393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 456786393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4568f2fc6a54SBenjamin Thery 4569fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4570fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4571fc66f95cSEric Dumazet 45728ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 45738ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 45748ed67789SDaniel Lezcano GFP_KERNEL); 45758ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4576fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4577d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 45788ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4579d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 458062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 458162fa8a84SDavid S. Miller ip6_template_metrics, true); 45828ed67789SDaniel Lezcano 45838ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4584feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 45858ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 45868ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 45878ed67789SDaniel Lezcano GFP_KERNEL); 458868fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 458968fffc67SPeter Zijlstra goto out_ip6_null_entry; 4590d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 45918ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4592d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 459362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 459462fa8a84SDavid S. Miller ip6_template_metrics, true); 45958ed67789SDaniel Lezcano 45968ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 45978ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 45988ed67789SDaniel Lezcano GFP_KERNEL); 459968fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 460068fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4601d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 46028ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4603d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 460462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 460562fa8a84SDavid S. Miller ip6_template_metrics, true); 46068ed67789SDaniel Lezcano #endif 46078ed67789SDaniel Lezcano 4608b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4609b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4610b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4611b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4612b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4613b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4614b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4615b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4616b339a47cSPeter Zijlstra 46176891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 46186891a346SBenjamin Thery 46198ed67789SDaniel Lezcano ret = 0; 46208ed67789SDaniel Lezcano out: 46218ed67789SDaniel Lezcano return ret; 4622f2fc6a54SBenjamin Thery 462368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 462468fffc67SPeter Zijlstra out_ip6_prohibit_entry: 462568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 462668fffc67SPeter Zijlstra out_ip6_null_entry: 462768fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 462868fffc67SPeter Zijlstra #endif 4629fc66f95cSEric Dumazet out_ip6_dst_entries: 4630fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4631f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4632f2fc6a54SBenjamin Thery goto out; 4633cdb18761SDaniel Lezcano } 4634cdb18761SDaniel Lezcano 46352c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4636cdb18761SDaniel Lezcano { 46378ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 46388ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46398ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 46408ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 46418ed67789SDaniel Lezcano #endif 464241bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4643cdb18761SDaniel Lezcano } 4644cdb18761SDaniel Lezcano 4645d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4646d189634eSThomas Graf { 4647d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4648d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4649d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4650d189634eSThomas Graf #endif 4651d189634eSThomas Graf return 0; 4652d189634eSThomas Graf } 4653d189634eSThomas Graf 4654d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4655d189634eSThomas Graf { 4656d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4657ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4658ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4659d189634eSThomas Graf #endif 4660d189634eSThomas Graf } 4661d189634eSThomas Graf 4662cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4663cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4664cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4665cdb18761SDaniel Lezcano }; 4666cdb18761SDaniel Lezcano 4667c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4668c3426b47SDavid S. Miller { 4669c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4670c3426b47SDavid S. Miller 4671c3426b47SDavid S. Miller if (!bp) 4672c3426b47SDavid S. Miller return -ENOMEM; 4673c3426b47SDavid S. Miller inet_peer_base_init(bp); 4674c3426b47SDavid S. Miller net->ipv6.peers = bp; 4675c3426b47SDavid S. Miller return 0; 4676c3426b47SDavid S. Miller } 4677c3426b47SDavid S. Miller 4678c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4679c3426b47SDavid S. Miller { 4680c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4681c3426b47SDavid S. Miller 4682c3426b47SDavid S. Miller net->ipv6.peers = NULL; 468356a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4684c3426b47SDavid S. Miller kfree(bp); 4685c3426b47SDavid S. Miller } 4686c3426b47SDavid S. Miller 46872b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4688c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4689c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4690c3426b47SDavid S. Miller }; 4691c3426b47SDavid S. Miller 4692d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4693d189634eSThomas Graf .init = ip6_route_net_init_late, 4694d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4695d189634eSThomas Graf }; 4696d189634eSThomas Graf 46978ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 46988ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4699242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 47008ed67789SDaniel Lezcano }; 47018ed67789SDaniel Lezcano 47022f460933SWANG Cong void __init ip6_route_init_special_entries(void) 47032f460933SWANG Cong { 47042f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 47052f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 47062f460933SWANG Cong * manually for init_net */ 47072f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 47082f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47092f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 47102f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 47112f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47122f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 47132f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47142f460933SWANG Cong #endif 47152f460933SWANG Cong } 47162f460933SWANG Cong 4717433d49c3SDaniel Lezcano int __init ip6_route_init(void) 47181da177e4SLinus Torvalds { 4719433d49c3SDaniel Lezcano int ret; 47208d0b94afSMartin KaFai Lau int cpu; 4721433d49c3SDaniel Lezcano 47229a7ec3a9SDaniel Lezcano ret = -ENOMEM; 47239a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 47249a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 47259a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 47269a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4727c19a28e1SFernando Carrijo goto out; 472814e50e57SDavid S. Miller 4729fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 47308ed67789SDaniel Lezcano if (ret) 4731bdb3289fSDaniel Lezcano goto out_kmem_cache; 4732bdb3289fSDaniel Lezcano 4733c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4734c3426b47SDavid S. Miller if (ret) 4735e8803b6cSDavid S. Miller goto out_dst_entries; 47362a0c451aSThomas Graf 47377e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 47387e52b33bSDavid S. Miller if (ret) 47397e52b33bSDavid S. Miller goto out_register_inetpeer; 4740c3426b47SDavid S. Miller 47415dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 47425dc121e9SArnaud Ebalard 4743e8803b6cSDavid S. Miller ret = fib6_init(); 4744433d49c3SDaniel Lezcano if (ret) 47458ed67789SDaniel Lezcano goto out_register_subsys; 4746433d49c3SDaniel Lezcano 4747433d49c3SDaniel Lezcano ret = xfrm6_init(); 4748433d49c3SDaniel Lezcano if (ret) 4749e8803b6cSDavid S. Miller goto out_fib6_init; 4750c35b7e72SDaniel Lezcano 4751433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4752433d49c3SDaniel Lezcano if (ret) 4753433d49c3SDaniel Lezcano goto xfrm6_init; 47547e5449c2SDaniel Lezcano 4755d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4756d189634eSThomas Graf if (ret) 4757d189634eSThomas Graf goto fib6_rules_init; 4758d189634eSThomas Graf 4759433d49c3SDaniel Lezcano ret = -ENOBUFS; 4760b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4761b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4762e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4763e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4764d189634eSThomas Graf goto out_register_late_subsys; 4765433d49c3SDaniel Lezcano 47668ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4767cdb18761SDaniel Lezcano if (ret) 4768d189634eSThomas Graf goto out_register_late_subsys; 47698ed67789SDaniel Lezcano 47708d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 47718d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 47728d0b94afSMartin KaFai Lau 47738d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 47748d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 47758d0b94afSMartin KaFai Lau } 47768d0b94afSMartin KaFai Lau 4777433d49c3SDaniel Lezcano out: 4778433d49c3SDaniel Lezcano return ret; 4779433d49c3SDaniel Lezcano 4780d189634eSThomas Graf out_register_late_subsys: 4781d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4782433d49c3SDaniel Lezcano fib6_rules_init: 4783433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4784433d49c3SDaniel Lezcano xfrm6_init: 4785433d49c3SDaniel Lezcano xfrm6_fini(); 47862a0c451aSThomas Graf out_fib6_init: 47872a0c451aSThomas Graf fib6_gc_cleanup(); 47888ed67789SDaniel Lezcano out_register_subsys: 47898ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 47907e52b33bSDavid S. Miller out_register_inetpeer: 47917e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4792fc66f95cSEric Dumazet out_dst_entries: 4793fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4794433d49c3SDaniel Lezcano out_kmem_cache: 4795f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4796433d49c3SDaniel Lezcano goto out; 47971da177e4SLinus Torvalds } 47981da177e4SLinus Torvalds 47991da177e4SLinus Torvalds void ip6_route_cleanup(void) 48001da177e4SLinus Torvalds { 48018ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4802d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4803101367c2SThomas Graf fib6_rules_cleanup(); 48041da177e4SLinus Torvalds xfrm6_fini(); 48051da177e4SLinus Torvalds fib6_gc_cleanup(); 4806c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 48078ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 480841bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4809f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 48101da177e4SLinus Torvalds } 4811