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 { 1893a2232e9SDavid Miller return dst_metrics_write_ptr(&rt->from->dst); 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; 3943a2232e9SDavid Miller struct rt6_info *from = rt->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 4123a2232e9SDavid Miller rt->from = NULL; 4133a2232e9SDavid Miller dst_release(&from->dst); 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; 4463a2232e9SDavid Miller } else if (rt->from) { 4471e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4483a2232e9SDavid Miller rt6_check_expired(rt->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 459b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 460b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 461b673d6ccSJakub Sitnicki */ 462b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 463*5e5d6fedSRoopa Prabhu fl6->mp_hash = rt6_multipath_hash(fl6, NULL, NULL); 464b673d6ccSJakub Sitnicki 4653d709f69SIdo Schimmel if (fl6->mp_hash <= atomic_read(&match->rt6i_nh_upper_bound)) 4663d709f69SIdo Schimmel return match; 467bbfcd776SIdo Schimmel 4683d709f69SIdo Schimmel list_for_each_entry_safe(sibling, next_sibling, &match->rt6i_siblings, 4693d709f69SIdo Schimmel rt6i_siblings) { 4703d709f69SIdo Schimmel if (fl6->mp_hash > atomic_read(&sibling->rt6i_nh_upper_bound)) 4713d709f69SIdo Schimmel continue; 47252bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 47352bd4c0cSNicolas Dichtel break; 47451ebd318SNicolas Dichtel match = sibling; 47551ebd318SNicolas Dichtel break; 47651ebd318SNicolas Dichtel } 4773d709f69SIdo Schimmel 47851ebd318SNicolas Dichtel return match; 47951ebd318SNicolas Dichtel } 48051ebd318SNicolas Dichtel 4811da177e4SLinus Torvalds /* 48266f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4831da177e4SLinus Torvalds */ 4841da177e4SLinus Torvalds 4858ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4868ed67789SDaniel Lezcano struct rt6_info *rt, 487b71d1d42SEric Dumazet const struct in6_addr *saddr, 4881da177e4SLinus Torvalds int oif, 489d420895eSYOSHIFUJI Hideaki int flags) 4901da177e4SLinus Torvalds { 4911da177e4SLinus Torvalds struct rt6_info *local = NULL; 4921da177e4SLinus Torvalds struct rt6_info *sprt; 4931da177e4SLinus Torvalds 4948067bb8cSIdo Schimmel if (!oif && ipv6_addr_any(saddr) && !(rt->rt6i_nh_flags & RTNH_F_DEAD)) 4958067bb8cSIdo Schimmel return rt; 496dd3abc4eSYOSHIFUJI Hideaki 497071fb37eSDavid Miller for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) { 498d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 499dd3abc4eSYOSHIFUJI Hideaki 5008067bb8cSIdo Schimmel if (sprt->rt6i_nh_flags & RTNH_F_DEAD) 5018067bb8cSIdo Schimmel continue; 5028067bb8cSIdo Schimmel 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 } 5318067bb8cSIdo Schimmel 5328067bb8cSIdo Schimmel return rt->rt6i_nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : 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 6788067bb8cSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 6798067bb8cSIdo Schimmel goto out; 6808067bb8cSIdo Schimmel 68114c5206cSIdo Schimmel if (idev->cnf.ignore_routes_with_linkdown && 68214c5206cSIdo Schimmel rt->rt6i_nh_flags & RTNH_F_LINKDOWN && 683d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68435103d11SAndy Gospodarek goto out; 685554cfb7eSYOSHIFUJI Hideaki 686554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 687f11e6659SDavid S. Miller goto out; 688554cfb7eSYOSHIFUJI Hideaki 689554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6907e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 691afc154e9SHannes Frederic Sowa match_do_rr = true; 692afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6937e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 694f11e6659SDavid S. Miller goto out; 6951da177e4SLinus Torvalds } 696f11e6659SDavid S. Miller 697afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 698afc154e9SHannes Frederic Sowa rt6_probe(rt); 699afc154e9SHannes Frederic Sowa 7007e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 701afc154e9SHannes Frederic Sowa if (m > *mpri) { 702afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 703afc154e9SHannes Frederic Sowa *mpri = m; 704afc154e9SHannes Frederic Sowa match = rt; 705afc154e9SHannes Frederic Sowa } 706f11e6659SDavid S. Miller out: 707f11e6659SDavid S. Miller return match; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 7118d1040e8SWei Wang struct rt6_info *leaf, 712f11e6659SDavid S. Miller struct rt6_info *rr_head, 713afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 714afc154e9SHannes Frederic Sowa bool *do_rr) 715f11e6659SDavid S. Miller { 7169fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 717f11e6659SDavid S. Miller int mpri = -1; 718f11e6659SDavid S. Miller 719f11e6659SDavid S. Miller match = NULL; 7209fbdcfafSSteffen Klassert cont = NULL; 721071fb37eSDavid Miller for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) { 7229fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7239fbdcfafSSteffen Klassert cont = rt; 7249fbdcfafSSteffen Klassert break; 7259fbdcfafSSteffen Klassert } 7269fbdcfafSSteffen Klassert 727afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7289fbdcfafSSteffen Klassert } 7299fbdcfafSSteffen Klassert 73066f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 731071fb37eSDavid Miller rt = rcu_dereference(rt->rt6_next)) { 7329fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7339fbdcfafSSteffen Klassert cont = rt; 7349fbdcfafSSteffen Klassert break; 7359fbdcfafSSteffen Klassert } 7369fbdcfafSSteffen Klassert 7379fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7389fbdcfafSSteffen Klassert } 7399fbdcfafSSteffen Klassert 7409fbdcfafSSteffen Klassert if (match || !cont) 7419fbdcfafSSteffen Klassert return match; 7429fbdcfafSSteffen Klassert 743071fb37eSDavid Miller for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next)) 744afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 745f11e6659SDavid S. Miller 746f11e6659SDavid S. Miller return match; 747f11e6659SDavid S. Miller } 748f11e6659SDavid S. Miller 7498d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, 7508d1040e8SWei Wang int oif, int strict) 751f11e6659SDavid S. Miller { 75266f5d6ceSWei Wang struct rt6_info *leaf = rcu_dereference(fn->leaf); 753f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 754afc154e9SHannes Frederic Sowa bool do_rr = false; 75517ecf590SWei Wang int key_plen; 756f11e6659SDavid S. Miller 75787b1af8dSWei Wang if (!leaf || leaf == net->ipv6.ip6_null_entry) 7588d1040e8SWei Wang return net->ipv6.ip6_null_entry; 7598d1040e8SWei Wang 76066f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 761f11e6659SDavid S. Miller if (!rt0) 76266f5d6ceSWei Wang rt0 = leaf; 763f11e6659SDavid S. Miller 76417ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 76517ecf590SWei Wang * and fn->leaf does not points to its child's leaf 76617ecf590SWei Wang * (This might happen if all routes under fn are deleted from 76717ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 76817ecf590SWei Wang */ 76917ecf590SWei Wang key_plen = rt0->rt6i_dst.plen; 77017ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 77117ecf590SWei Wang if (rt0->rt6i_src.plen) 77217ecf590SWei Wang key_plen = rt0->rt6i_src.plen; 77317ecf590SWei Wang #endif 77417ecf590SWei Wang if (fn->fn_bit != key_plen) 77517ecf590SWei Wang return net->ipv6.ip6_null_entry; 77617ecf590SWei Wang 7778d1040e8SWei Wang match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, 778afc154e9SHannes Frederic Sowa &do_rr); 779f11e6659SDavid S. Miller 780afc154e9SHannes Frederic Sowa if (do_rr) { 781071fb37eSDavid Miller struct rt6_info *next = rcu_dereference(rt0->rt6_next); 782f11e6659SDavid S. Miller 783554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 784f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 7858d1040e8SWei Wang next = leaf; 786f11e6659SDavid S. Miller 78766f5d6ceSWei Wang if (next != rt0) { 78866f5d6ceSWei Wang spin_lock_bh(&leaf->rt6i_table->tb6_lock); 78966f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 79066f5d6ceSWei Wang if (next->rt6i_node) 79166f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 79266f5d6ceSWei Wang spin_unlock_bh(&leaf->rt6i_table->tb6_lock); 79366f5d6ceSWei Wang } 794554cfb7eSYOSHIFUJI Hideaki } 795554cfb7eSYOSHIFUJI Hideaki 796a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 7998b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 8008b9df265SMartin KaFai Lau { 8018b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 8028b9df265SMartin KaFai Lau } 8038b9df265SMartin KaFai Lau 80470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 80570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 806b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 80770ceb4f5SYOSHIFUJI Hideaki { 808c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 80970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 81070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 81170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 8124bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 81370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 81470ceb4f5SYOSHIFUJI Hideaki 81570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 81670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81770ceb4f5SYOSHIFUJI Hideaki } 81870ceb4f5SYOSHIFUJI Hideaki 81970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 82070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 82170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 82370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 82570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 82670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82770ceb4f5SYOSHIFUJI Hideaki } 82870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 82970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 83070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83170ceb4f5SYOSHIFUJI Hideaki } 83270ceb4f5SYOSHIFUJI Hideaki } 83370ceb4f5SYOSHIFUJI Hideaki 83470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 83570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8363933fc95SJens Rosenboom return -EINVAL; 83770ceb4f5SYOSHIFUJI Hideaki 8384bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 83970ceb4f5SYOSHIFUJI Hideaki 84070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 84170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 84270ceb4f5SYOSHIFUJI Hideaki else { 84370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 84470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 84570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 84670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 84770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 84870ceb4f5SYOSHIFUJI Hideaki } 84970ceb4f5SYOSHIFUJI Hideaki 850f104a567SDuan Jiong if (rinfo->prefix_len == 0) 851f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 852f104a567SDuan Jiong else 853f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 854830218c1SDavid Ahern gwaddr, dev); 85570ceb4f5SYOSHIFUJI Hideaki 85670ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 857e0a1ad73SThomas Graf ip6_del_rt(rt); 85870ceb4f5SYOSHIFUJI Hideaki rt = NULL; 85970ceb4f5SYOSHIFUJI Hideaki } 86070ceb4f5SYOSHIFUJI Hideaki 86170ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 862830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 863830218c1SDavid Ahern dev, pref); 86470ceb4f5SYOSHIFUJI Hideaki else if (rt) 86570ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 86670ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 86770ceb4f5SYOSHIFUJI Hideaki 86870ceb4f5SYOSHIFUJI Hideaki if (rt) { 8691716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8701716a961SGao feng rt6_clean_expires(rt); 8711716a961SGao feng else 8721716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8731716a961SGao feng 87494e187c0SAmerigo Wang ip6_rt_put(rt); 87570ceb4f5SYOSHIFUJI Hideaki } 87670ceb4f5SYOSHIFUJI Hideaki return 0; 87770ceb4f5SYOSHIFUJI Hideaki } 87870ceb4f5SYOSHIFUJI Hideaki #endif 87970ceb4f5SYOSHIFUJI Hideaki 880a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 881a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 882a3c00e46SMartin KaFai Lau { 88366f5d6ceSWei Wang struct fib6_node *pn, *sn; 884a3c00e46SMartin KaFai Lau while (1) { 885a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 886a3c00e46SMartin KaFai Lau return NULL; 88766f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 88866f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 88966f5d6ceSWei Wang if (sn && sn != fn) 89066f5d6ceSWei Wang fn = fib6_lookup(sn, NULL, saddr); 891a3c00e46SMartin KaFai Lau else 892a3c00e46SMartin KaFai Lau fn = pn; 893a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 894a3c00e46SMartin KaFai Lau return fn; 895a3c00e46SMartin KaFai Lau } 896a3c00e46SMartin KaFai Lau } 897c71099acSThomas Graf 898d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 899d3843fe5SWei Wang bool null_fallback) 900d3843fe5SWei Wang { 901d3843fe5SWei Wang struct rt6_info *rt = *prt; 902d3843fe5SWei Wang 903d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 904d3843fe5SWei Wang return true; 905d3843fe5SWei Wang if (null_fallback) { 906d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 907d3843fe5SWei Wang dst_hold(&rt->dst); 908d3843fe5SWei Wang } else { 909d3843fe5SWei Wang rt = NULL; 910d3843fe5SWei Wang } 911d3843fe5SWei Wang *prt = rt; 912d3843fe5SWei Wang return false; 913d3843fe5SWei Wang } 914d3843fe5SWei Wang 9158ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 9168ed67789SDaniel Lezcano struct fib6_table *table, 9174c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9181da177e4SLinus Torvalds { 9192b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 9201da177e4SLinus Torvalds struct fib6_node *fn; 9211da177e4SLinus Torvalds 92266f5d6ceSWei Wang rcu_read_lock(); 9234c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 924c71099acSThomas Graf restart: 92566f5d6ceSWei Wang rt = rcu_dereference(fn->leaf); 92666f5d6ceSWei Wang if (!rt) { 92766f5d6ceSWei Wang rt = net->ipv6.ip6_null_entry; 92866f5d6ceSWei Wang } else { 92966f5d6ceSWei Wang rt = rt6_device_match(net, rt, &fl6->saddr, 93066f5d6ceSWei Wang fl6->flowi6_oif, flags); 93151ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 93266f5d6ceSWei Wang rt = rt6_multipath_select(rt, fl6, 93366f5d6ceSWei Wang fl6->flowi6_oif, flags); 93466f5d6ceSWei Wang } 935a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 936a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 937a3c00e46SMartin KaFai Lau if (fn) 938a3c00e46SMartin KaFai Lau goto restart; 939a3c00e46SMartin KaFai Lau } 9402b760fcfSWei Wang /* Search through exception table */ 9412b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 9422b760fcfSWei Wang if (rt_cache) 9432b760fcfSWei Wang rt = rt_cache; 9442b760fcfSWei Wang 945d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 946d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 947d3843fe5SWei Wang 94866f5d6ceSWei Wang rcu_read_unlock(); 949b811580dSDavid Ahern 950b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 951b811580dSDavid Ahern 9521da177e4SLinus Torvalds return rt; 953c71099acSThomas Graf 954c71099acSThomas Graf } 955c71099acSThomas Graf 956ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 957ea6e574eSFlorian Westphal int flags) 958ea6e574eSFlorian Westphal { 959ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 960ea6e574eSFlorian Westphal } 961ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 962ea6e574eSFlorian Westphal 9639acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9649acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 965c71099acSThomas Graf { 9664c9483b2SDavid S. Miller struct flowi6 fl6 = { 9674c9483b2SDavid S. Miller .flowi6_oif = oif, 9684c9483b2SDavid S. Miller .daddr = *daddr, 969c71099acSThomas Graf }; 970c71099acSThomas Graf struct dst_entry *dst; 97177d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 972c71099acSThomas Graf 973adaa70bbSThomas Graf if (saddr) { 9744c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 975adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 976adaa70bbSThomas Graf } 977adaa70bbSThomas Graf 9784c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 979c71099acSThomas Graf if (dst->error == 0) 980c71099acSThomas Graf return (struct rt6_info *) dst; 981c71099acSThomas Graf 982c71099acSThomas Graf dst_release(dst); 983c71099acSThomas Graf 9841da177e4SLinus Torvalds return NULL; 9851da177e4SLinus Torvalds } 9867159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9877159039aSYOSHIFUJI Hideaki 988c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9891cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9901cfb71eeSWei Wang * route is released. 9911cfb71eeSWei Wang * Caller must hold dst before calling it. 9921da177e4SLinus Torvalds */ 9931da177e4SLinus Torvalds 994e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 995333c4301SDavid Ahern struct mx6_config *mxc, 996333c4301SDavid Ahern struct netlink_ext_ack *extack) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds int err; 999c71099acSThomas Graf struct fib6_table *table; 10001da177e4SLinus Torvalds 1001c71099acSThomas Graf table = rt->rt6i_table; 100266f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1003333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 100466f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds return err; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 100940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 101040e22e8fSThomas Graf { 1011e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 1012e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 1013e715b6d3SFlorian Westphal 10141cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 10151cfb71eeSWei Wang dst_hold(&rt->dst); 1016333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 101740e22e8fSThomas Graf } 101840e22e8fSThomas Graf 10194832c30dSDavid Ahern /* called with rcu_lock held */ 10204832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 10214832c30dSDavid Ahern { 10224832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 10234832c30dSDavid Ahern 102498d11291SDavid Ahern if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) { 10254832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 10264832c30dSDavid Ahern * device if it is a master device, the master device if 10274832c30dSDavid Ahern * device is enslaved, and the loopback as the default 10284832c30dSDavid Ahern */ 10294832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 10304832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 10314832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 10324832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 10334832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 10344832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 10354832c30dSDavid Ahern * case we want dev returned to be dev 10364832c30dSDavid Ahern */ 10374832c30dSDavid Ahern } 10384832c30dSDavid Ahern 10394832c30dSDavid Ahern return dev; 10404832c30dSDavid Ahern } 10414832c30dSDavid Ahern 10428b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 104321efcfa0SEric Dumazet const struct in6_addr *daddr, 1044b71d1d42SEric Dumazet const struct in6_addr *saddr) 10451da177e4SLinus Torvalds { 10464832c30dSDavid Ahern struct net_device *dev; 10471da177e4SLinus Torvalds struct rt6_info *rt; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* 10501da177e4SLinus Torvalds * Clone the route. 10511da177e4SLinus Torvalds */ 10521da177e4SLinus Torvalds 1053d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 10543a2232e9SDavid Miller ort = ort->from; 10551da177e4SLinus Torvalds 10564832c30dSDavid Ahern rcu_read_lock(); 10574832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10584832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10594832c30dSDavid Ahern rcu_read_unlock(); 106083a09abdSMartin KaFai Lau if (!rt) 106183a09abdSMartin KaFai Lau return NULL; 106283a09abdSMartin KaFai Lau 106383a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10648b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 106583a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 106683a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 106783a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 106883a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10698b9df265SMartin KaFai Lau 10708b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1071bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 107221efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 107358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10751da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10764e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10771da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds #endif 108095a9a5baSYOSHIFUJI Hideaki } 108195a9a5baSYOSHIFUJI Hideaki 1082299d9939SYOSHIFUJI Hideaki return rt; 1083299d9939SYOSHIFUJI Hideaki } 1084299d9939SYOSHIFUJI Hideaki 1085d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1086d52d3997SMartin KaFai Lau { 10874832c30dSDavid Ahern struct net_device *dev; 1088d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1089d52d3997SMartin KaFai Lau 10904832c30dSDavid Ahern rcu_read_lock(); 10914832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10924832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10934832c30dSDavid Ahern rcu_read_unlock(); 1094d52d3997SMartin KaFai Lau if (!pcpu_rt) 1095d52d3997SMartin KaFai Lau return NULL; 1096d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1097d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1098d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1099d52d3997SMartin KaFai Lau return pcpu_rt; 1100d52d3997SMartin KaFai Lau } 1101d52d3997SMartin KaFai Lau 110266f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1103d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1104d52d3997SMartin KaFai Lau { 1105a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1106d52d3997SMartin KaFai Lau 1107d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1108d52d3997SMartin KaFai Lau pcpu_rt = *p; 1109d52d3997SMartin KaFai Lau 1110d3843fe5SWei Wang if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false)) 1111a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1112d3843fe5SWei Wang 1113a73e4195SMartin KaFai Lau return pcpu_rt; 1114a73e4195SMartin KaFai Lau } 1115a73e4195SMartin KaFai Lau 1116a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1117a73e4195SMartin KaFai Lau { 1118a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1119d52d3997SMartin KaFai Lau 1120d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1121d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1122d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1123d52d3997SMartin KaFai Lau 11249c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 11259c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1126d52d3997SMartin KaFai Lau } 1127d52d3997SMartin KaFai Lau 1128a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1129a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1130d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1131951f788aSEric Dumazet BUG_ON(prev); 1132a94b9367SWei Wang 1133d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1134d52d3997SMartin KaFai Lau return pcpu_rt; 1135d52d3997SMartin KaFai Lau } 1136d52d3997SMartin KaFai Lau 113735732d01SWei Wang /* exception hash table implementation 113835732d01SWei Wang */ 113935732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 114035732d01SWei Wang 114135732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 114235732d01SWei Wang * Caller must hold rt6_exception_lock 114335732d01SWei Wang */ 114435732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 114535732d01SWei Wang struct rt6_exception *rt6_ex) 114635732d01SWei Wang { 1147b2427e67SColin Ian King struct net *net; 114881eb8447SWei Wang 114935732d01SWei Wang if (!bucket || !rt6_ex) 115035732d01SWei Wang return; 1151b2427e67SColin Ian King 1152b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 115335732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 115435732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 115535732d01SWei Wang rt6_release(rt6_ex->rt6i); 115635732d01SWei Wang kfree_rcu(rt6_ex, rcu); 115735732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 115835732d01SWei Wang bucket->depth--; 115981eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache--; 116035732d01SWei Wang } 116135732d01SWei Wang 116235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 116335732d01SWei Wang * Caller must hold rt6_exception_lock 116435732d01SWei Wang */ 116535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 116635732d01SWei Wang { 116735732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 116835732d01SWei Wang 116935732d01SWei Wang if (!bucket) 117035732d01SWei Wang return; 117135732d01SWei Wang 117235732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 117335732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 117435732d01SWei Wang oldest = rt6_ex; 117535732d01SWei Wang } 117635732d01SWei Wang rt6_remove_exception(bucket, oldest); 117735732d01SWei Wang } 117835732d01SWei Wang 117935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 118035732d01SWei Wang const struct in6_addr *src) 118135732d01SWei Wang { 118235732d01SWei Wang static u32 seed __read_mostly; 118335732d01SWei Wang u32 val; 118435732d01SWei Wang 118535732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 118635732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 118735732d01SWei Wang 118835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 118935732d01SWei Wang if (src) 119035732d01SWei Wang val = jhash(src, sizeof(*src), val); 119135732d01SWei Wang #endif 119235732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 119335732d01SWei Wang } 119435732d01SWei Wang 119535732d01SWei Wang /* Helper function to find the cached rt in the hash table 119635732d01SWei Wang * and update bucket pointer to point to the bucket for this 119735732d01SWei Wang * (daddr, saddr) pair 119835732d01SWei Wang * Caller must hold rt6_exception_lock 119935732d01SWei Wang */ 120035732d01SWei Wang static struct rt6_exception * 120135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 120235732d01SWei Wang const struct in6_addr *daddr, 120335732d01SWei Wang const struct in6_addr *saddr) 120435732d01SWei Wang { 120535732d01SWei Wang struct rt6_exception *rt6_ex; 120635732d01SWei Wang u32 hval; 120735732d01SWei Wang 120835732d01SWei Wang if (!(*bucket) || !daddr) 120935732d01SWei Wang return NULL; 121035732d01SWei Wang 121135732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 121235732d01SWei Wang *bucket += hval; 121335732d01SWei Wang 121435732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 121535732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 121635732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 121735732d01SWei Wang 121835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 121935732d01SWei Wang if (matched && saddr) 122035732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 122135732d01SWei Wang #endif 122235732d01SWei Wang if (matched) 122335732d01SWei Wang return rt6_ex; 122435732d01SWei Wang } 122535732d01SWei Wang return NULL; 122635732d01SWei Wang } 122735732d01SWei Wang 122835732d01SWei Wang /* Helper function to find the cached rt in the hash table 122935732d01SWei Wang * and update bucket pointer to point to the bucket for this 123035732d01SWei Wang * (daddr, saddr) pair 123135732d01SWei Wang * Caller must hold rcu_read_lock() 123235732d01SWei Wang */ 123335732d01SWei Wang static struct rt6_exception * 123435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 123535732d01SWei Wang const struct in6_addr *daddr, 123635732d01SWei Wang const struct in6_addr *saddr) 123735732d01SWei Wang { 123835732d01SWei Wang struct rt6_exception *rt6_ex; 123935732d01SWei Wang u32 hval; 124035732d01SWei Wang 124135732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 124235732d01SWei Wang 124335732d01SWei Wang if (!(*bucket) || !daddr) 124435732d01SWei Wang return NULL; 124535732d01SWei Wang 124635732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 124735732d01SWei Wang *bucket += hval; 124835732d01SWei Wang 124935732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 125035732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 125135732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 125235732d01SWei Wang 125335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 125435732d01SWei Wang if (matched && saddr) 125535732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 125635732d01SWei Wang #endif 125735732d01SWei Wang if (matched) 125835732d01SWei Wang return rt6_ex; 125935732d01SWei Wang } 126035732d01SWei Wang return NULL; 126135732d01SWei Wang } 126235732d01SWei Wang 126335732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 126435732d01SWei Wang struct rt6_info *ort) 126535732d01SWei Wang { 126681eb8447SWei Wang struct net *net = dev_net(ort->dst.dev); 126735732d01SWei Wang struct rt6_exception_bucket *bucket; 126835732d01SWei Wang struct in6_addr *src_key = NULL; 126935732d01SWei Wang struct rt6_exception *rt6_ex; 127035732d01SWei Wang int err = 0; 127135732d01SWei Wang 127235732d01SWei Wang /* ort can't be a cache or pcpu route */ 127335732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 12743a2232e9SDavid Miller ort = ort->from; 127535732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 127635732d01SWei Wang 127735732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 127835732d01SWei Wang 127935732d01SWei Wang if (ort->exception_bucket_flushed) { 128035732d01SWei Wang err = -EINVAL; 128135732d01SWei Wang goto out; 128235732d01SWei Wang } 128335732d01SWei Wang 128435732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 128535732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 128635732d01SWei Wang if (!bucket) { 128735732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 128835732d01SWei Wang GFP_ATOMIC); 128935732d01SWei Wang if (!bucket) { 129035732d01SWei Wang err = -ENOMEM; 129135732d01SWei Wang goto out; 129235732d01SWei Wang } 129335732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 129435732d01SWei Wang } 129535732d01SWei Wang 129635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 129735732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 129835732d01SWei Wang * and exception table is indexed by a hash of 129935732d01SWei Wang * both rt6i_dst and rt6i_src. 130035732d01SWei Wang * Otherwise, the exception table is indexed by 130135732d01SWei Wang * a hash of only rt6i_dst. 130235732d01SWei Wang */ 130335732d01SWei Wang if (ort->rt6i_src.plen) 130435732d01SWei Wang src_key = &nrt->rt6i_src.addr; 130535732d01SWei Wang #endif 130660006a48SWei Wang 130760006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 130860006a48SWei Wang * in rt6_remove_prefsrc() 130960006a48SWei Wang */ 131060006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1311f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1312f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1313f5bbe7eeSWei Wang * is less than ort's mtu value. 1314f5bbe7eeSWei Wang */ 1315f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1316f5bbe7eeSWei Wang err = -EINVAL; 1317f5bbe7eeSWei Wang goto out; 1318f5bbe7eeSWei Wang } 131960006a48SWei Wang 132035732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 132135732d01SWei Wang src_key); 132235732d01SWei Wang if (rt6_ex) 132335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 132435732d01SWei Wang 132535732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 132635732d01SWei Wang if (!rt6_ex) { 132735732d01SWei Wang err = -ENOMEM; 132835732d01SWei Wang goto out; 132935732d01SWei Wang } 133035732d01SWei Wang rt6_ex->rt6i = nrt; 133135732d01SWei Wang rt6_ex->stamp = jiffies; 133235732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 133335732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 133435732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 133535732d01SWei Wang bucket->depth++; 133681eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 133735732d01SWei Wang 133835732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 133935732d01SWei Wang rt6_exception_remove_oldest(bucket); 134035732d01SWei Wang 134135732d01SWei Wang out: 134235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 134335732d01SWei Wang 134435732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1345b886d5f2SPaolo Abeni if (!err) { 1346922c2ac8SIdo Schimmel spin_lock_bh(&ort->rt6i_table->tb6_lock); 134735732d01SWei Wang fib6_update_sernum(ort); 1348922c2ac8SIdo Schimmel spin_unlock_bh(&ort->rt6i_table->tb6_lock); 1349b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1350b886d5f2SPaolo Abeni } 135135732d01SWei Wang 135235732d01SWei Wang return err; 135335732d01SWei Wang } 135435732d01SWei Wang 135535732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 135635732d01SWei Wang { 135735732d01SWei Wang struct rt6_exception_bucket *bucket; 135835732d01SWei Wang struct rt6_exception *rt6_ex; 135935732d01SWei Wang struct hlist_node *tmp; 136035732d01SWei Wang int i; 136135732d01SWei Wang 136235732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 136335732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 136435732d01SWei Wang rt->exception_bucket_flushed = 1; 136535732d01SWei Wang 136635732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 136735732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 136835732d01SWei Wang if (!bucket) 136935732d01SWei Wang goto out; 137035732d01SWei Wang 137135732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 137235732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 137335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 137435732d01SWei Wang WARN_ON_ONCE(bucket->depth); 137535732d01SWei Wang bucket++; 137635732d01SWei Wang } 137735732d01SWei Wang 137835732d01SWei Wang out: 137935732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 138035732d01SWei Wang } 138135732d01SWei Wang 138235732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 138335732d01SWei Wang * Caller has to hold rcu_read_lock() 138435732d01SWei Wang */ 138535732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 138635732d01SWei Wang struct in6_addr *daddr, 138735732d01SWei Wang struct in6_addr *saddr) 138835732d01SWei Wang { 138935732d01SWei Wang struct rt6_exception_bucket *bucket; 139035732d01SWei Wang struct in6_addr *src_key = NULL; 139135732d01SWei Wang struct rt6_exception *rt6_ex; 139235732d01SWei Wang struct rt6_info *res = NULL; 139335732d01SWei Wang 139435732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 139535732d01SWei Wang 139635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139735732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 139835732d01SWei Wang * and exception table is indexed by a hash of 139935732d01SWei Wang * both rt6i_dst and rt6i_src. 140035732d01SWei Wang * Otherwise, the exception table is indexed by 140135732d01SWei Wang * a hash of only rt6i_dst. 140235732d01SWei Wang */ 140335732d01SWei Wang if (rt->rt6i_src.plen) 140435732d01SWei Wang src_key = saddr; 140535732d01SWei Wang #endif 140635732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 140735732d01SWei Wang 140835732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 140935732d01SWei Wang res = rt6_ex->rt6i; 141035732d01SWei Wang 141135732d01SWei Wang return res; 141235732d01SWei Wang } 141335732d01SWei Wang 141435732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 141535732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 141635732d01SWei Wang { 141735732d01SWei Wang struct rt6_exception_bucket *bucket; 14183a2232e9SDavid Miller struct rt6_info *from = rt->from; 141935732d01SWei Wang struct in6_addr *src_key = NULL; 142035732d01SWei Wang struct rt6_exception *rt6_ex; 142135732d01SWei Wang int err; 142235732d01SWei Wang 142335732d01SWei Wang if (!from || 1424442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 142535732d01SWei Wang return -EINVAL; 142635732d01SWei Wang 142735732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 142835732d01SWei Wang return -ENOENT; 142935732d01SWei Wang 143035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 143135732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 143235732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 143335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143435732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 143535732d01SWei Wang * and exception table is indexed by a hash of 143635732d01SWei Wang * both rt6i_dst and rt6i_src. 143735732d01SWei Wang * Otherwise, the exception table is indexed by 143835732d01SWei Wang * a hash of only rt6i_dst. 143935732d01SWei Wang */ 144035732d01SWei Wang if (from->rt6i_src.plen) 144135732d01SWei Wang src_key = &rt->rt6i_src.addr; 144235732d01SWei Wang #endif 144335732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 144435732d01SWei Wang &rt->rt6i_dst.addr, 144535732d01SWei Wang src_key); 144635732d01SWei Wang if (rt6_ex) { 144735732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 144835732d01SWei Wang err = 0; 144935732d01SWei Wang } else { 145035732d01SWei Wang err = -ENOENT; 145135732d01SWei Wang } 145235732d01SWei Wang 145335732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 145435732d01SWei Wang return err; 145535732d01SWei Wang } 145635732d01SWei Wang 145735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 145835732d01SWei Wang * refresh its stamp 145935732d01SWei Wang */ 146035732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 146135732d01SWei Wang { 146235732d01SWei Wang struct rt6_exception_bucket *bucket; 14633a2232e9SDavid Miller struct rt6_info *from = rt->from; 146435732d01SWei Wang struct in6_addr *src_key = NULL; 146535732d01SWei Wang struct rt6_exception *rt6_ex; 146635732d01SWei Wang 146735732d01SWei Wang if (!from || 1468442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 146935732d01SWei Wang return; 147035732d01SWei Wang 147135732d01SWei Wang rcu_read_lock(); 147235732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 147335732d01SWei Wang 147435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 147535732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 147635732d01SWei Wang * and exception table is indexed by a hash of 147735732d01SWei Wang * both rt6i_dst and rt6i_src. 147835732d01SWei Wang * Otherwise, the exception table is indexed by 147935732d01SWei Wang * a hash of only rt6i_dst. 148035732d01SWei Wang */ 148135732d01SWei Wang if (from->rt6i_src.plen) 148235732d01SWei Wang src_key = &rt->rt6i_src.addr; 148335732d01SWei Wang #endif 148435732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 148535732d01SWei Wang &rt->rt6i_dst.addr, 148635732d01SWei Wang src_key); 148735732d01SWei Wang if (rt6_ex) 148835732d01SWei Wang rt6_ex->stamp = jiffies; 148935732d01SWei Wang 149035732d01SWei Wang rcu_read_unlock(); 149135732d01SWei Wang } 149235732d01SWei Wang 149360006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 149460006a48SWei Wang { 149560006a48SWei Wang struct rt6_exception_bucket *bucket; 149660006a48SWei Wang struct rt6_exception *rt6_ex; 149760006a48SWei Wang int i; 149860006a48SWei Wang 149960006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 150060006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 150160006a48SWei Wang 150260006a48SWei Wang if (bucket) { 150360006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 150460006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 150560006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 150660006a48SWei Wang } 150760006a48SWei Wang bucket++; 150860006a48SWei Wang } 150960006a48SWei Wang } 151060006a48SWei Wang } 151160006a48SWei Wang 1512f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu) 1513f5bbe7eeSWei Wang { 1514f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1515f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1516f5bbe7eeSWei Wang int i; 1517f5bbe7eeSWei Wang 1518f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1519f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1520f5bbe7eeSWei Wang 1521f5bbe7eeSWei Wang if (bucket) { 1522f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1523f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1524f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1525f5bbe7eeSWei Wang /* For RTF_CACHE with rt6i_pmtu == 0 1526f5bbe7eeSWei Wang * (i.e. a redirected route), 1527f5bbe7eeSWei Wang * the metrics of its rt->dst.from has already 1528f5bbe7eeSWei Wang * been updated. 1529f5bbe7eeSWei Wang */ 1530f5bbe7eeSWei Wang if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu) 1531f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1532f5bbe7eeSWei Wang } 1533f5bbe7eeSWei Wang bucket++; 1534f5bbe7eeSWei Wang } 1535f5bbe7eeSWei Wang } 1536f5bbe7eeSWei Wang } 1537f5bbe7eeSWei Wang 1538b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1539b16cb459SWei Wang 1540b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1541b16cb459SWei Wang struct in6_addr *gateway) 1542b16cb459SWei Wang { 1543b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1544b16cb459SWei Wang struct rt6_exception *rt6_ex; 1545b16cb459SWei Wang struct hlist_node *tmp; 1546b16cb459SWei Wang int i; 1547b16cb459SWei Wang 1548b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1549b16cb459SWei Wang return; 1550b16cb459SWei Wang 1551b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1552b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1553b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1554b16cb459SWei Wang 1555b16cb459SWei Wang if (bucket) { 1556b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1557b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1558b16cb459SWei Wang &bucket->chain, hlist) { 1559b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1560b16cb459SWei Wang 1561b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1562b16cb459SWei Wang RTF_CACHE_GATEWAY && 1563b16cb459SWei Wang ipv6_addr_equal(gateway, 1564b16cb459SWei Wang &entry->rt6i_gateway)) { 1565b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1566b16cb459SWei Wang } 1567b16cb459SWei Wang } 1568b16cb459SWei Wang bucket++; 1569b16cb459SWei Wang } 1570b16cb459SWei Wang } 1571b16cb459SWei Wang 1572b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1573b16cb459SWei Wang } 1574b16cb459SWei Wang 1575c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1576c757faa8SWei Wang struct rt6_exception *rt6_ex, 1577c757faa8SWei Wang struct fib6_gc_args *gc_args, 1578c757faa8SWei Wang unsigned long now) 1579c757faa8SWei Wang { 1580c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1581c757faa8SWei Wang 15821859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 15831859bac0SPaolo Abeni * even if others have still references to them, so that on next 15841859bac0SPaolo Abeni * dst_check() such references can be dropped. 15851859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 15861859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 15871859bac0SPaolo Abeni */ 158831afeb42SWei Wang if (!(rt->rt6i_flags & RTF_EXPIRES)) { 158931afeb42SWei Wang if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1590c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1591c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1592c757faa8SWei Wang return; 159331afeb42SWei Wang } 159431afeb42SWei Wang } else if (time_after(jiffies, rt->dst.expires)) { 159531afeb42SWei Wang RT6_TRACE("purging expired route %p\n", rt); 159631afeb42SWei Wang rt6_remove_exception(bucket, rt6_ex); 159731afeb42SWei Wang return; 159831afeb42SWei Wang } 159931afeb42SWei Wang 160031afeb42SWei Wang if (rt->rt6i_flags & RTF_GATEWAY) { 1601c757faa8SWei Wang struct neighbour *neigh; 1602c757faa8SWei Wang __u8 neigh_flags = 0; 1603c757faa8SWei Wang 1604c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1605c757faa8SWei Wang if (neigh) { 1606c757faa8SWei Wang neigh_flags = neigh->flags; 1607c757faa8SWei Wang neigh_release(neigh); 1608c757faa8SWei Wang } 1609c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1610c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1611c757faa8SWei Wang rt); 1612c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1613c757faa8SWei Wang return; 1614c757faa8SWei Wang } 1615c757faa8SWei Wang } 161631afeb42SWei Wang 1617c757faa8SWei Wang gc_args->more++; 1618c757faa8SWei Wang } 1619c757faa8SWei Wang 1620c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1621c757faa8SWei Wang struct fib6_gc_args *gc_args, 1622c757faa8SWei Wang unsigned long now) 1623c757faa8SWei Wang { 1624c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1625c757faa8SWei Wang struct rt6_exception *rt6_ex; 1626c757faa8SWei Wang struct hlist_node *tmp; 1627c757faa8SWei Wang int i; 1628c757faa8SWei Wang 1629c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1630c757faa8SWei Wang return; 1631c757faa8SWei Wang 1632c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1633c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1634c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1635c757faa8SWei Wang 1636c757faa8SWei Wang if (bucket) { 1637c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1638c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1639c757faa8SWei Wang &bucket->chain, hlist) { 1640c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1641c757faa8SWei Wang gc_args, now); 1642c757faa8SWei Wang } 1643c757faa8SWei Wang bucket++; 1644c757faa8SWei Wang } 1645c757faa8SWei Wang } 1646c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1647c757faa8SWei Wang } 1648c757faa8SWei Wang 16499ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 16509ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 16511da177e4SLinus Torvalds { 1652367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 16532b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 1654c71099acSThomas Graf int strict = 0; 16551da177e4SLinus Torvalds 165677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1657d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1658367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1659367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16601da177e4SLinus Torvalds 166166f5d6ceSWei Wang rcu_read_lock(); 16621da177e4SLinus Torvalds 16634c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1664367efcb9SMartin KaFai Lau saved_fn = fn; 16651da177e4SLinus Torvalds 1666ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1667ca254490SDavid Ahern oif = 0; 1668ca254490SDavid Ahern 1669a3c00e46SMartin KaFai Lau redo_rt6_select: 16708d1040e8SWei Wang rt = rt6_select(net, fn, oif, strict); 167152bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1672367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1673a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1674a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1675a3c00e46SMartin KaFai Lau if (fn) 1676a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1677367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1678367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1679367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1680367efcb9SMartin KaFai Lau fn = saved_fn; 1681367efcb9SMartin KaFai Lau goto redo_rt6_select; 1682367efcb9SMartin KaFai Lau } 1683a3c00e46SMartin KaFai Lau } 1684a3c00e46SMartin KaFai Lau 16852b760fcfSWei Wang /*Search through exception table */ 16862b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 16872b760fcfSWei Wang if (rt_cache) 16882b760fcfSWei Wang rt = rt_cache; 1689d52d3997SMartin KaFai Lau 1690d3843fe5SWei Wang if (rt == net->ipv6.ip6_null_entry) { 169166f5d6ceSWei Wang rcu_read_unlock(); 1692d3843fe5SWei Wang dst_hold(&rt->dst); 1693b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1694d3843fe5SWei Wang return rt; 1695d3843fe5SWei Wang } else if (rt->rt6i_flags & RTF_CACHE) { 1696d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1697d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1698d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1699d3843fe5SWei Wang } 170066f5d6ceSWei Wang rcu_read_unlock(); 1701b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1702d52d3997SMartin KaFai Lau return rt; 17033da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 17043da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 17053da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 17063da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 17073da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 17083da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 17093da59bd9SMartin KaFai Lau */ 1710c71099acSThomas Graf 17113da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 17123da59bd9SMartin KaFai Lau 1713d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1714d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1715d3843fe5SWei Wang } else { 171666f5d6ceSWei Wang rcu_read_unlock(); 1717d3843fe5SWei Wang uncached_rt = rt; 1718d3843fe5SWei Wang goto uncached_rt_out; 1719d3843fe5SWei Wang } 172066f5d6ceSWei Wang rcu_read_unlock(); 1721d52d3997SMartin KaFai Lau 17223da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 17233da59bd9SMartin KaFai Lau dst_release(&rt->dst); 17243da59bd9SMartin KaFai Lau 17251cfb71eeSWei Wang if (uncached_rt) { 17261cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 17271cfb71eeSWei Wang * No need for another dst_hold() 17281cfb71eeSWei Wang */ 17298d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 173081eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 17311cfb71eeSWei Wang } else { 17323da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 17333da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 17341cfb71eeSWei Wang } 1735b811580dSDavid Ahern 1736d3843fe5SWei Wang uncached_rt_out: 1737b65f164dSPaolo Abeni trace_fib6_table_lookup(net, uncached_rt, table, fl6); 17383da59bd9SMartin KaFai Lau return uncached_rt; 17393da59bd9SMartin KaFai Lau 1740d52d3997SMartin KaFai Lau } else { 1741d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1742d52d3997SMartin KaFai Lau 1743d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1744d52d3997SMartin KaFai Lau 1745d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1746951f788aSEric Dumazet local_bh_disable(); 1747d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1748d52d3997SMartin KaFai Lau 1749951f788aSEric Dumazet if (!pcpu_rt) { 1750a94b9367SWei Wang /* atomic_inc_not_zero() is needed when using rcu */ 1751a94b9367SWei Wang if (atomic_inc_not_zero(&rt->rt6i_ref)) { 1752951f788aSEric Dumazet /* No dst_hold() on rt is needed because grabbing 1753a94b9367SWei Wang * rt->rt6i_ref makes sure rt can't be released. 17549c7370a1SMartin KaFai Lau */ 17559c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 1756a94b9367SWei Wang rt6_release(rt); 1757a94b9367SWei Wang } else { 1758a94b9367SWei Wang /* rt is already removed from tree */ 1759a94b9367SWei Wang pcpu_rt = net->ipv6.ip6_null_entry; 1760a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1761a94b9367SWei Wang } 17629c7370a1SMartin KaFai Lau } 1763951f788aSEric Dumazet local_bh_enable(); 1764951f788aSEric Dumazet rcu_read_unlock(); 1765b65f164dSPaolo Abeni trace_fib6_table_lookup(net, pcpu_rt, table, fl6); 1766d52d3997SMartin KaFai Lau return pcpu_rt; 1767d52d3997SMartin KaFai Lau } 1768c71099acSThomas Graf } 17699ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1770c71099acSThomas Graf 17718ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 17724c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 17734acad72dSPavel Emelyanov { 17744c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 17754acad72dSPavel Emelyanov } 17764acad72dSPavel Emelyanov 1777d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 177872331bc0SShmulik Ladkani struct net_device *dev, 177972331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 178072331bc0SShmulik Ladkani { 178172331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 178272331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 178372331bc0SShmulik Ladkani 178472331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 178572331bc0SShmulik Ladkani } 1786d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 178772331bc0SShmulik Ladkani 178823aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 1789*5e5d6fedSRoopa Prabhu struct flow_keys *keys, 1790*5e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 179123aebdacSJakub Sitnicki { 179223aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 179323aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 1794*5e5d6fedSRoopa Prabhu struct flow_keys *_flkeys = flkeys; 179523aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 179623aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 179723aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 179823aebdacSJakub Sitnicki 179923aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 180023aebdacSJakub Sitnicki goto out; 180123aebdacSJakub Sitnicki 180223aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 180323aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 180423aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 180523aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 180623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 180723aebdacSJakub Sitnicki goto out; 180823aebdacSJakub Sitnicki 180923aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 181023aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 181123aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 181223aebdacSJakub Sitnicki if (!inner_iph) 181323aebdacSJakub Sitnicki goto out; 181423aebdacSJakub Sitnicki 181523aebdacSJakub Sitnicki key_iph = inner_iph; 1816*5e5d6fedSRoopa Prabhu _flkeys = NULL; 181723aebdacSJakub Sitnicki out: 181823aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 181923aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1820*5e5d6fedSRoopa Prabhu if (_flkeys) { 1821*5e5d6fedSRoopa Prabhu keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; 1822*5e5d6fedSRoopa Prabhu keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; 1823*5e5d6fedSRoopa Prabhu keys->tags.flow_label = _flkeys->tags.flow_label; 1824*5e5d6fedSRoopa Prabhu keys->basic.ip_proto = _flkeys->basic.ip_proto; 1825*5e5d6fedSRoopa Prabhu } else { 182623aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 182723aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 182823aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 182923aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 183023aebdacSJakub Sitnicki } 1831*5e5d6fedSRoopa Prabhu } 183223aebdacSJakub Sitnicki 183323aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 1834*5e5d6fedSRoopa Prabhu u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb, 1835*5e5d6fedSRoopa Prabhu struct flow_keys *flkeys) 183623aebdacSJakub Sitnicki { 183723aebdacSJakub Sitnicki struct flow_keys hash_keys; 183823aebdacSJakub Sitnicki 183923aebdacSJakub Sitnicki if (skb) { 1840*5e5d6fedSRoopa Prabhu ip6_multipath_l3_keys(skb, &hash_keys, flkeys); 18417696c06aSIdo Schimmel return flow_hash_from_keys(&hash_keys) >> 1; 184223aebdacSJakub Sitnicki } 184323aebdacSJakub Sitnicki 18447696c06aSIdo Schimmel return get_hash_from_flowi6(fl6) >> 1; 184523aebdacSJakub Sitnicki } 184623aebdacSJakub Sitnicki 1847c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1848c71099acSThomas Graf { 1849b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1850c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1851adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1852904af04dSJiri Benc struct ip_tunnel_info *tun_info; 18534c9483b2SDavid S. Miller struct flowi6 fl6 = { 1854e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 18554c9483b2SDavid S. Miller .daddr = iph->daddr, 18564c9483b2SDavid S. Miller .saddr = iph->saddr, 18576502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 18584c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 18594c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1860c71099acSThomas Graf }; 1861*5e5d6fedSRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 1862adaa70bbSThomas Graf 1863904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 186446fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1865904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 1866*5e5d6fedSRoopa Prabhu 1867*5e5d6fedSRoopa Prabhu if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) 1868*5e5d6fedSRoopa Prabhu flkeys = &_flkeys; 1869*5e5d6fedSRoopa Prabhu 187023aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 1871*5e5d6fedSRoopa Prabhu fl6.mp_hash = rt6_multipath_hash(&fl6, skb, flkeys); 187206e9d040SJiri Benc skb_dst_drop(skb); 187372331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1874c71099acSThomas Graf } 1875c71099acSThomas Graf 18768ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 18774c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1878c71099acSThomas Graf { 18794c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1880c71099acSThomas Graf } 1881c71099acSThomas Graf 18826f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 18836f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1884c71099acSThomas Graf { 1885d46a9d67SDavid Ahern bool any_src; 1886c71099acSThomas Graf 18874c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 18884c1feac5SDavid Ahern struct dst_entry *dst; 18894c1feac5SDavid Ahern 18904c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1891ca254490SDavid Ahern if (dst) 1892ca254490SDavid Ahern return dst; 18934c1feac5SDavid Ahern } 1894ca254490SDavid Ahern 18951fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 18964dc27d1cSDavid McCullough 1897d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1898741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1899d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 190077d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1901c71099acSThomas Graf 1902d46a9d67SDavid Ahern if (!any_src) 1903adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 19040c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 19050c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1906adaa70bbSThomas Graf 19074c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 19081da177e4SLinus Torvalds } 19096f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 19101da177e4SLinus Torvalds 19112774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 191214e50e57SDavid S. Miller { 19135c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 19141dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 191514e50e57SDavid S. Miller struct dst_entry *new = NULL; 191614e50e57SDavid S. Miller 19171dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 191862cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 191914e50e57SDavid S. Miller if (rt) { 19200a1f5962SMartin KaFai Lau rt6_info_init(rt); 192181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 19220a1f5962SMartin KaFai Lau 1923d8d1f30bSChangli Gao new = &rt->dst; 192414e50e57SDavid S. Miller new->__use = 1; 1925352e512cSHerbert Xu new->input = dst_discard; 1926ede2059dSEric W. Biederman new->output = dst_discard_out; 192714e50e57SDavid S. Miller 1928defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 192914e50e57SDavid S. Miller 19301dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 19314e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 19320a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 193314e50e57SDavid S. Miller rt->rt6i_metric = 0; 193414e50e57SDavid S. Miller 193514e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 193614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 193714e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 193814e50e57SDavid S. Miller #endif 193914e50e57SDavid S. Miller } 194014e50e57SDavid S. Miller 194169ead7afSDavid S. Miller dst_release(dst_orig); 194269ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 194314e50e57SDavid S. Miller } 194414e50e57SDavid S. Miller 19451da177e4SLinus Torvalds /* 19461da177e4SLinus Torvalds * Destination cache support functions 19471da177e4SLinus Torvalds */ 19481da177e4SLinus Torvalds 19494b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 19504b32b5adSMartin KaFai Lau { 19513a2232e9SDavid Miller if (rt->from && 19523a2232e9SDavid Miller dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst)) 19533a2232e9SDavid Miller dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true); 19544b32b5adSMartin KaFai Lau } 19554b32b5adSMartin KaFai Lau 19563da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 19573da59bd9SMartin KaFai Lau { 195836143645SSteffen Klassert u32 rt_cookie = 0; 1959c5cff856SWei Wang 1960c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 19613da59bd9SMartin KaFai Lau return NULL; 19623da59bd9SMartin KaFai Lau 19633da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 19643da59bd9SMartin KaFai Lau return NULL; 19653da59bd9SMartin KaFai Lau 19663da59bd9SMartin KaFai Lau return &rt->dst; 19673da59bd9SMartin KaFai Lau } 19683da59bd9SMartin KaFai Lau 19693da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 19703da59bd9SMartin KaFai Lau { 19715973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 19725973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 19733a2232e9SDavid Miller rt6_check(rt->from, cookie)) 19743da59bd9SMartin KaFai Lau return &rt->dst; 19753da59bd9SMartin KaFai Lau else 19763da59bd9SMartin KaFai Lau return NULL; 19773da59bd9SMartin KaFai Lau } 19783da59bd9SMartin KaFai Lau 19791da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 19801da177e4SLinus Torvalds { 19811da177e4SLinus Torvalds struct rt6_info *rt; 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 19841da177e4SLinus Torvalds 19856f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 19866f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 19876f3118b5SNicolas Dichtel * into this function always. 19886f3118b5SNicolas Dichtel */ 1989e3bc10bdSHannes Frederic Sowa 19904b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 19914b32b5adSMartin KaFai Lau 199202bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 19933a2232e9SDavid Miller (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) 19943da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 19953da59bd9SMartin KaFai Lau else 19963da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 20001da177e4SLinus Torvalds { 20011da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (rt) { 200454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 200554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 2006e0a1ad73SThomas Graf ip6_del_rt(rt); 200754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 20081da177e4SLinus Torvalds } 200954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 201054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 201154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 201254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 201354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 201454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 20181da177e4SLinus Torvalds { 20191da177e4SLinus Torvalds struct rt6_info *rt; 20201da177e4SLinus Torvalds 20213ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 20221da177e4SLinus Torvalds 2023adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 20241da177e4SLinus Torvalds if (rt) { 20251eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 2026ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 20278e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 2028c5cff856SWei Wang } else { 2029c5cff856SWei Wang struct fib6_node *fn; 2030c5cff856SWei Wang 2031c5cff856SWei Wang rcu_read_lock(); 2032c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 2033c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2034c5cff856SWei Wang fn->fn_sernum = -1; 2035c5cff856SWei Wang rcu_read_unlock(); 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds } 20381eb4f758SHannes Frederic Sowa } 20391da177e4SLinus Torvalds 204045e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 204145e4fd26SMartin KaFai Lau { 204245e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 204345e4fd26SMartin KaFai Lau 204445e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 204545e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 204645e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 204745e4fd26SMartin KaFai Lau } 204845e4fd26SMartin KaFai Lau 20490d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 20500d3f6d29SMartin KaFai Lau { 20510d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 20524e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 20534e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 20540d3f6d29SMartin KaFai Lau } 20550d3f6d29SMartin KaFai Lau 205645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 205745e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 20581da177e4SLinus Torvalds { 20590dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 20601da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 20611da177e4SLinus Torvalds 206245e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 206345e4fd26SMartin KaFai Lau return; 206445e4fd26SMartin KaFai Lau 206519bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 206619bda36cSXin Long return; 206719bda36cSXin Long 206845e4fd26SMartin KaFai Lau if (iph) { 206945e4fd26SMartin KaFai Lau daddr = &iph->daddr; 207045e4fd26SMartin KaFai Lau saddr = &iph->saddr; 207145e4fd26SMartin KaFai Lau } else if (sk) { 207245e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 207345e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 207445e4fd26SMartin KaFai Lau } else { 20750dec879fSJulian Anastasov daddr = NULL; 20760dec879fSJulian Anastasov saddr = NULL; 20771da177e4SLinus Torvalds } 20780dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 20790dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 20800dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 20810dec879fSJulian Anastasov return; 20820dec879fSJulian Anastasov 20830dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 20840dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 20852b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 20862b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 20872b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 20880dec879fSJulian Anastasov } else if (daddr) { 20890dec879fSJulian Anastasov struct rt6_info *nrt6; 20900dec879fSJulian Anastasov 209145e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 209245e4fd26SMartin KaFai Lau if (nrt6) { 209345e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 20942b760fcfSWei Wang if (rt6_insert_exception(nrt6, rt6)) 20952b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 209645e4fd26SMartin KaFai Lau } 209745e4fd26SMartin KaFai Lau } 209845e4fd26SMartin KaFai Lau } 209945e4fd26SMartin KaFai Lau 210045e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 210145e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 210245e4fd26SMartin KaFai Lau { 210345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 210642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2107e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 210881aded24SDavid S. Miller { 210981aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 211081aded24SDavid S. Miller struct dst_entry *dst; 211181aded24SDavid S. Miller struct flowi6 fl6; 211281aded24SDavid S. Miller 211381aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 211481aded24SDavid S. Miller fl6.flowi6_oif = oif; 21151b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 211681aded24SDavid S. Miller fl6.daddr = iph->daddr; 211781aded24SDavid S. Miller fl6.saddr = iph->saddr; 21186502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2119e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 212081aded24SDavid S. Miller 212181aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 212281aded24SDavid S. Miller if (!dst->error) 212345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 212481aded24SDavid S. Miller dst_release(dst); 212581aded24SDavid S. Miller } 212681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 212781aded24SDavid S. Miller 212881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 212981aded24SDavid S. Miller { 213033c162a9SMartin KaFai Lau struct dst_entry *dst; 213133c162a9SMartin KaFai Lau 213281aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2133e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 213433c162a9SMartin KaFai Lau 213533c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 213633c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 213733c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 213833c162a9SMartin KaFai Lau return; 213933c162a9SMartin KaFai Lau 214033c162a9SMartin KaFai Lau bh_lock_sock(sk); 214133c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 214233c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 214333c162a9SMartin KaFai Lau bh_unlock_sock(sk); 214481aded24SDavid S. Miller } 214581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 214681aded24SDavid S. Miller 2147b55b76b2SDuan Jiong /* Handle redirects */ 2148b55b76b2SDuan Jiong struct ip6rd_flowi { 2149b55b76b2SDuan Jiong struct flowi6 fl6; 2150b55b76b2SDuan Jiong struct in6_addr gateway; 2151b55b76b2SDuan Jiong }; 2152b55b76b2SDuan Jiong 2153b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2154b55b76b2SDuan Jiong struct fib6_table *table, 2155b55b76b2SDuan Jiong struct flowi6 *fl6, 2156b55b76b2SDuan Jiong int flags) 2157b55b76b2SDuan Jiong { 2158b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 21592b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2160b55b76b2SDuan Jiong struct fib6_node *fn; 2161b55b76b2SDuan Jiong 2162b55b76b2SDuan Jiong /* Get the "current" route for this destination and 216367c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2164b55b76b2SDuan Jiong * 2165b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2166b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2167b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2168b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2169b55b76b2SDuan Jiong * routes. 2170b55b76b2SDuan Jiong */ 2171b55b76b2SDuan Jiong 217266f5d6ceSWei Wang rcu_read_lock(); 2173b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2174b55b76b2SDuan Jiong restart: 217566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 21768067bb8cSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 21778067bb8cSIdo Schimmel continue; 2178b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2179b55b76b2SDuan Jiong continue; 2180b55b76b2SDuan Jiong if (rt->dst.error) 2181b55b76b2SDuan Jiong break; 2182b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2183b55b76b2SDuan Jiong continue; 2184b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2185b55b76b2SDuan Jiong continue; 21862b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 21872b760fcfSWei Wang * in the case of an ip redirect. 21882b760fcfSWei Wang * So we keep searching in the exception table if the gateway 21892b760fcfSWei Wang * is different. 21902b760fcfSWei Wang */ 21912b760fcfSWei Wang if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) { 21922b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 21932b760fcfSWei Wang &fl6->daddr, 21942b760fcfSWei Wang &fl6->saddr); 21952b760fcfSWei Wang if (rt_cache && 21962b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 21972b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 21982b760fcfSWei Wang rt = rt_cache; 21992b760fcfSWei Wang break; 22002b760fcfSWei Wang } 2201b55b76b2SDuan Jiong continue; 22022b760fcfSWei Wang } 2203b55b76b2SDuan Jiong break; 2204b55b76b2SDuan Jiong } 2205b55b76b2SDuan Jiong 2206b55b76b2SDuan Jiong if (!rt) 2207b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2208b55b76b2SDuan Jiong else if (rt->dst.error) { 2209b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2210b0a1ba59SMartin KaFai Lau goto out; 2211b0a1ba59SMartin KaFai Lau } 2212b0a1ba59SMartin KaFai Lau 2213b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2214a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2215a3c00e46SMartin KaFai Lau if (fn) 2216a3c00e46SMartin KaFai Lau goto restart; 2217b55b76b2SDuan Jiong } 2218a3c00e46SMartin KaFai Lau 2219b0a1ba59SMartin KaFai Lau out: 2220d3843fe5SWei Wang ip6_hold_safe(net, &rt, true); 2221b55b76b2SDuan Jiong 222266f5d6ceSWei Wang rcu_read_unlock(); 2223b55b76b2SDuan Jiong 2224b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 2225b55b76b2SDuan Jiong return rt; 2226b55b76b2SDuan Jiong }; 2227b55b76b2SDuan Jiong 2228b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2229b55b76b2SDuan Jiong const struct flowi6 *fl6, 2230b55b76b2SDuan Jiong const struct in6_addr *gateway) 2231b55b76b2SDuan Jiong { 2232b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2233b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2234b55b76b2SDuan Jiong 2235b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2236b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2237b55b76b2SDuan Jiong 2238b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2239b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2240b55b76b2SDuan Jiong } 2241b55b76b2SDuan Jiong 2242e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2243e2d118a1SLorenzo Colitti kuid_t uid) 22443a5ad2eeSDavid S. Miller { 22453a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 22463a5ad2eeSDavid S. Miller struct dst_entry *dst; 22473a5ad2eeSDavid S. Miller struct flowi6 fl6; 22483a5ad2eeSDavid S. Miller 22493a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2250e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 22513a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 22523a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 22533a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 22543a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 22556502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2256e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 22573a5ad2eeSDavid S. Miller 2258b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 22596700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 22603a5ad2eeSDavid S. Miller dst_release(dst); 22613a5ad2eeSDavid S. Miller } 22623a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 22633a5ad2eeSDavid S. Miller 2264c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2265c92a59ecSDuan Jiong u32 mark) 2266c92a59ecSDuan Jiong { 2267c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2268c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2269c92a59ecSDuan Jiong struct dst_entry *dst; 2270c92a59ecSDuan Jiong struct flowi6 fl6; 2271c92a59ecSDuan Jiong 2272c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2273e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2274c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2275c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2276c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2277c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2278e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2279c92a59ecSDuan Jiong 2280b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2281c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2282c92a59ecSDuan Jiong dst_release(dst); 2283c92a59ecSDuan Jiong } 2284c92a59ecSDuan Jiong 22853a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 22863a5ad2eeSDavid S. Miller { 2287e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2288e2d118a1SLorenzo Colitti sk->sk_uid); 22893a5ad2eeSDavid S. Miller } 22903a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 22913a5ad2eeSDavid S. Miller 22920dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 22931da177e4SLinus Torvalds { 22940dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 22950dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 22960dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 22970dbaee3bSDavid S. Miller 22981da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 22991da177e4SLinus Torvalds 23005578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 23015578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds /* 23041da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 23051da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 23061da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 23071da177e4SLinus Torvalds * rely only on pmtu discovery" 23081da177e4SLinus Torvalds */ 23091da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 23101da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 23111da177e4SLinus Torvalds return mtu; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 2314ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2315d33e4553SDavid S. Miller { 23164b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 23174b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2318d33e4553SDavid S. Miller struct inet6_dev *idev; 2319618f9bc7SSteffen Klassert 2320618f9bc7SSteffen Klassert if (mtu) 232130f78d8eSEric Dumazet goto out; 2322618f9bc7SSteffen Klassert 23234b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 23244b32b5adSMartin KaFai Lau if (mtu) 23254b32b5adSMartin KaFai Lau goto out; 23264b32b5adSMartin KaFai Lau 2327618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2328d33e4553SDavid S. Miller 2329d33e4553SDavid S. Miller rcu_read_lock(); 2330d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2331d33e4553SDavid S. Miller if (idev) 2332d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2333d33e4553SDavid S. Miller rcu_read_unlock(); 2334d33e4553SDavid S. Miller 233530f78d8eSEric Dumazet out: 233614972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 233714972cbdSRoopa Prabhu 233814972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2339d33e4553SDavid S. Miller } 2340d33e4553SDavid S. Miller 23413b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 234287a11578SDavid S. Miller struct flowi6 *fl6) 23431da177e4SLinus Torvalds { 234487a11578SDavid S. Miller struct dst_entry *dst; 23451da177e4SLinus Torvalds struct rt6_info *rt; 23461da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2347c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 23481da177e4SLinus Torvalds 234938308473SDavid S. Miller if (unlikely(!idev)) 2350122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 23511da177e4SLinus Torvalds 2352ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 235338308473SDavid S. Miller if (unlikely(!rt)) { 23541da177e4SLinus Torvalds in6_dev_put(idev); 235587a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 23561da177e4SLinus Torvalds goto out; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 23598e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2360588753f1SBrendan McGrath rt->dst.input = ip6_input; 23618e2ec639SYan, Zheng rt->dst.output = ip6_output; 2362550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 236387a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 23648e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 23658e2ec639SYan, Zheng rt->rt6i_idev = idev; 236614edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 23671da177e4SLinus Torvalds 23684c981e28SIdo Schimmel /* Add this dst into uncached_list so that rt6_disable_ip() can 2369587fea74SWei Wang * do proper release of the net_device 2370587fea74SWei Wang */ 2371587fea74SWei Wang rt6_uncached_list_add(rt); 237281eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 23731da177e4SLinus Torvalds 237487a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 237587a11578SDavid S. Miller 23761da177e4SLinus Torvalds out: 237787a11578SDavid S. Miller return dst; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 2380569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 23811da177e4SLinus Torvalds { 238286393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 23837019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 23847019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 23857019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 23867019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 23877019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2388fc66f95cSEric Dumazet int entries; 23891da177e4SLinus Torvalds 2390fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 239149a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2392fc66f95cSEric Dumazet entries <= rt_max_size) 23931da177e4SLinus Torvalds goto out; 23941da177e4SLinus Torvalds 23956891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 239614956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2397fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2398fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 23997019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 24001da177e4SLinus Torvalds out: 24017019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2402fc66f95cSEric Dumazet return entries > rt_max_size; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 2405e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2406e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2407e715b6d3SFlorian Westphal { 24086670e152SStephen Hemminger struct net *net = cfg->fc_nlinfo.nl_net; 2409c3a8d947SDaniel Borkmann bool ecn_ca = false; 2410e715b6d3SFlorian Westphal struct nlattr *nla; 2411e715b6d3SFlorian Westphal int remaining; 2412e715b6d3SFlorian Westphal u32 *mp; 2413e715b6d3SFlorian Westphal 241463159f29SIan Morris if (!cfg->fc_mx) 2415e715b6d3SFlorian Westphal return 0; 2416e715b6d3SFlorian Westphal 2417e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2418e715b6d3SFlorian Westphal if (unlikely(!mp)) 2419e715b6d3SFlorian Westphal return -ENOMEM; 2420e715b6d3SFlorian Westphal 2421e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2422e715b6d3SFlorian Westphal int type = nla_type(nla); 2423ea697639SDaniel Borkmann u32 val; 2424ea697639SDaniel Borkmann 24251bb14807SDaniel Borkmann if (!type) 24261bb14807SDaniel Borkmann continue; 2427e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2428e715b6d3SFlorian Westphal goto err; 24291bb14807SDaniel Borkmann 2430ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2431ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2432e715b6d3SFlorian Westphal 2433ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 24346670e152SStephen Hemminger val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca); 2435ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2436ea697639SDaniel Borkmann goto err; 2437ea697639SDaniel Borkmann } else { 2438ea697639SDaniel Borkmann val = nla_get_u32(nla); 2439ea697639SDaniel Borkmann } 2440626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2441626abd59SPaolo Abeni val = 255; 2442b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2443b8d3e416SDaniel Borkmann goto err; 2444ea697639SDaniel Borkmann 2445ea697639SDaniel Borkmann mp[type - 1] = val; 2446e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2447e715b6d3SFlorian Westphal } 2448e715b6d3SFlorian Westphal 2449c3a8d947SDaniel Borkmann if (ecn_ca) { 2450c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2451c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2452c3a8d947SDaniel Borkmann } 2453e715b6d3SFlorian Westphal 2454c3a8d947SDaniel Borkmann mxc->mx = mp; 2455e715b6d3SFlorian Westphal return 0; 2456e715b6d3SFlorian Westphal err: 2457e715b6d3SFlorian Westphal kfree(mp); 2458e715b6d3SFlorian Westphal return -EINVAL; 2459e715b6d3SFlorian Westphal } 24601da177e4SLinus Torvalds 24618c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 24628c14586fSDavid Ahern struct fib6_config *cfg, 2463f4797b33SDavid Ahern const struct in6_addr *gw_addr, 2464f4797b33SDavid Ahern u32 tbid, int flags) 24658c14586fSDavid Ahern { 24668c14586fSDavid Ahern struct flowi6 fl6 = { 24678c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 24688c14586fSDavid Ahern .daddr = *gw_addr, 24698c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 24708c14586fSDavid Ahern }; 24718c14586fSDavid Ahern struct fib6_table *table; 24728c14586fSDavid Ahern struct rt6_info *rt; 24738c14586fSDavid Ahern 2474f4797b33SDavid Ahern table = fib6_get_table(net, tbid); 24758c14586fSDavid Ahern if (!table) 24768c14586fSDavid Ahern return NULL; 24778c14586fSDavid Ahern 24788c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 24798c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 24808c14586fSDavid Ahern 2481f4797b33SDavid Ahern flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; 24828c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 24838c14586fSDavid Ahern 24848c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 24858c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 24868c14586fSDavid Ahern ip6_rt_put(rt); 24878c14586fSDavid Ahern rt = NULL; 24888c14586fSDavid Ahern } 24898c14586fSDavid Ahern 24908c14586fSDavid Ahern return rt; 24918c14586fSDavid Ahern } 24928c14586fSDavid Ahern 2493fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net, 2494fc1e64e1SDavid Ahern struct fib6_config *cfg, 2495fc1e64e1SDavid Ahern struct net_device *dev, 2496fc1e64e1SDavid Ahern struct netlink_ext_ack *extack) 2497fc1e64e1SDavid Ahern { 249844750f84SDavid Ahern u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; 2499fc1e64e1SDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 2500fc1e64e1SDavid Ahern u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; 2501fc1e64e1SDavid Ahern struct rt6_info *grt; 2502fc1e64e1SDavid Ahern int err; 2503fc1e64e1SDavid Ahern 2504fc1e64e1SDavid Ahern err = 0; 2505fc1e64e1SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); 2506fc1e64e1SDavid Ahern if (grt) { 250758e354c0SDavid Ahern if (!grt->dst.error && 250858e354c0SDavid Ahern (grt->rt6i_flags & flags || dev != grt->dst.dev)) { 250944750f84SDavid Ahern NL_SET_ERR_MSG(extack, 251044750f84SDavid Ahern "Nexthop has invalid gateway or device mismatch"); 2511fc1e64e1SDavid Ahern err = -EINVAL; 2512fc1e64e1SDavid Ahern } 2513fc1e64e1SDavid Ahern 2514fc1e64e1SDavid Ahern ip6_rt_put(grt); 2515fc1e64e1SDavid Ahern } 2516fc1e64e1SDavid Ahern 2517fc1e64e1SDavid Ahern return err; 2518fc1e64e1SDavid Ahern } 2519fc1e64e1SDavid Ahern 25201edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net, 25211edce99fSDavid Ahern struct fib6_config *cfg, 25221edce99fSDavid Ahern struct net_device **_dev, 25231edce99fSDavid Ahern struct inet6_dev **idev) 25241edce99fSDavid Ahern { 25251edce99fSDavid Ahern const struct in6_addr *gw_addr = &cfg->fc_gateway; 25261edce99fSDavid Ahern struct net_device *dev = _dev ? *_dev : NULL; 25271edce99fSDavid Ahern struct rt6_info *grt = NULL; 25281edce99fSDavid Ahern int err = -EHOSTUNREACH; 25291edce99fSDavid Ahern 25301edce99fSDavid Ahern if (cfg->fc_table) { 2531f4797b33SDavid Ahern int flags = RT6_LOOKUP_F_IFACE; 2532f4797b33SDavid Ahern 2533f4797b33SDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr, 2534f4797b33SDavid Ahern cfg->fc_table, flags); 25351edce99fSDavid Ahern if (grt) { 25361edce99fSDavid Ahern if (grt->rt6i_flags & RTF_GATEWAY || 25371edce99fSDavid Ahern (dev && dev != grt->dst.dev)) { 25381edce99fSDavid Ahern ip6_rt_put(grt); 25391edce99fSDavid Ahern grt = NULL; 25401edce99fSDavid Ahern } 25411edce99fSDavid Ahern } 25421edce99fSDavid Ahern } 25431edce99fSDavid Ahern 25441edce99fSDavid Ahern if (!grt) 25451edce99fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 25461edce99fSDavid Ahern 25471edce99fSDavid Ahern if (!grt) 25481edce99fSDavid Ahern goto out; 25491edce99fSDavid Ahern 25501edce99fSDavid Ahern if (dev) { 25511edce99fSDavid Ahern if (dev != grt->dst.dev) { 25521edce99fSDavid Ahern ip6_rt_put(grt); 25531edce99fSDavid Ahern goto out; 25541edce99fSDavid Ahern } 25551edce99fSDavid Ahern } else { 25561edce99fSDavid Ahern *_dev = dev = grt->dst.dev; 25571edce99fSDavid Ahern *idev = grt->rt6i_idev; 25581edce99fSDavid Ahern dev_hold(dev); 25591edce99fSDavid Ahern in6_dev_hold(grt->rt6i_idev); 25601edce99fSDavid Ahern } 25611edce99fSDavid Ahern 25621edce99fSDavid Ahern if (!(grt->rt6i_flags & RTF_GATEWAY)) 25631edce99fSDavid Ahern err = 0; 25641edce99fSDavid Ahern 25651edce99fSDavid Ahern ip6_rt_put(grt); 25661edce99fSDavid Ahern 25671edce99fSDavid Ahern out: 25681edce99fSDavid Ahern return err; 25691edce99fSDavid Ahern } 25701edce99fSDavid Ahern 2571333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2572333c4301SDavid Ahern struct netlink_ext_ack *extack) 25731da177e4SLinus Torvalds { 25745578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 25751da177e4SLinus Torvalds struct rt6_info *rt = NULL; 25761da177e4SLinus Torvalds struct net_device *dev = NULL; 25771da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2578c71099acSThomas Graf struct fib6_table *table; 25791da177e4SLinus Torvalds int addr_type; 25808c5b83f0SRoopa Prabhu int err = -EINVAL; 25811da177e4SLinus Torvalds 2582557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2583d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2584d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2585557c44beSDavid Ahern goto out; 2586d5d531cbSDavid Ahern } 2587557c44beSDavid Ahern 25882ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 25892ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 25902ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 25912ea2352eSWei Wang goto out; 25922ea2352eSWei Wang } 25932ea2352eSWei Wang 2594d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2595d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 25968c5b83f0SRoopa Prabhu goto out; 2597d5d531cbSDavid Ahern } 2598d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2599d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2600d5d531cbSDavid Ahern goto out; 2601d5d531cbSDavid Ahern } 26021da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2603d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2604d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2605d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 26068c5b83f0SRoopa Prabhu goto out; 2607d5d531cbSDavid Ahern } 26081da177e4SLinus Torvalds #endif 260986872cb5SThomas Graf if (cfg->fc_ifindex) { 26101da177e4SLinus Torvalds err = -ENODEV; 26115578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 26121da177e4SLinus Torvalds if (!dev) 26131da177e4SLinus Torvalds goto out; 26141da177e4SLinus Torvalds idev = in6_dev_get(dev); 26151da177e4SLinus Torvalds if (!idev) 26161da177e4SLinus Torvalds goto out; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 261986872cb5SThomas Graf if (cfg->fc_metric == 0) 262086872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 26211da177e4SLinus Torvalds 2622fc1e64e1SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 2623fc1e64e1SDavid Ahern if (!dev) { 2624fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, 2625fc1e64e1SDavid Ahern "Nexthop device required for onlink"); 2626fc1e64e1SDavid Ahern err = -ENODEV; 2627fc1e64e1SDavid Ahern goto out; 2628fc1e64e1SDavid Ahern } 2629fc1e64e1SDavid Ahern 2630fc1e64e1SDavid Ahern if (!(dev->flags & IFF_UP)) { 2631fc1e64e1SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 2632fc1e64e1SDavid Ahern err = -ENETDOWN; 2633fc1e64e1SDavid Ahern goto out; 2634fc1e64e1SDavid Ahern } 2635fc1e64e1SDavid Ahern } 2636fc1e64e1SDavid Ahern 2637c71099acSThomas Graf err = -ENOBUFS; 263838308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2639d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2640d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 264138308473SDavid S. Miller if (!table) { 2642f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2643d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2644d71314b4SMatti Vaittinen } 2645d71314b4SMatti Vaittinen } else { 2646d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2647d71314b4SMatti Vaittinen } 264838308473SDavid S. Miller 264938308473SDavid S. Miller if (!table) 2650c71099acSThomas Graf goto out; 2651c71099acSThomas Graf 2652ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2653ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 26541da177e4SLinus Torvalds 265538308473SDavid S. Miller if (!rt) { 26561da177e4SLinus Torvalds err = -ENOMEM; 26571da177e4SLinus Torvalds goto out; 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds 26601716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 26611716a961SGao feng rt6_set_expires(rt, jiffies + 26621716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 26631716a961SGao feng else 26641716a961SGao feng rt6_clean_expires(rt); 26651da177e4SLinus Torvalds 266686872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 266786872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 266886872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 266986872cb5SThomas Graf 267086872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2673d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2674ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2675ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 26761da177e4SLinus Torvalds else 2677d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 26781da177e4SLinus Torvalds 2679d8d1f30bSChangli Gao rt->dst.output = ip6_output; 26801da177e4SLinus Torvalds 268119e42e45SRoopa Prabhu if (cfg->fc_encap) { 268219e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 268319e42e45SRoopa Prabhu 268430357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2685127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 26869ae28727SDavid Ahern &lwtstate, extack); 268719e42e45SRoopa Prabhu if (err) 268819e42e45SRoopa Prabhu goto out; 268961adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 26909942895bSDavid Ahern lwtunnel_set_redirect(&rt->dst); 269125368623STom Herbert } 269219e42e45SRoopa Prabhu 269386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 269486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2695afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 269611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 269986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 270086872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 27011da177e4SLinus Torvalds #endif 27021da177e4SLinus Torvalds 270386872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 2704398958aeSIdo Schimmel rt->rt6i_nh_weight = 1; 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 27071da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 27081da177e4SLinus Torvalds */ 270986872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 271038308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 271138308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 271238308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 27131da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 27145578689aSDaniel Lezcano if (dev != net->loopback_dev) { 27151da177e4SLinus Torvalds if (dev) { 27161da177e4SLinus Torvalds dev_put(dev); 27171da177e4SLinus Torvalds in6_dev_put(idev); 27181da177e4SLinus Torvalds } 27195578689aSDaniel Lezcano dev = net->loopback_dev; 27201da177e4SLinus Torvalds dev_hold(dev); 27211da177e4SLinus Torvalds idev = in6_dev_get(dev); 27221da177e4SLinus Torvalds if (!idev) { 27231da177e4SLinus Torvalds err = -ENODEV; 27241da177e4SLinus Torvalds goto out; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2728ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2729ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2730ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2731ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 27327150aedeSKamala R rt->dst.input = dst_discard; 2733ef2c7d7bSNicolas Dichtel break; 2734ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2735ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 27367150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 27377150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2738ef2c7d7bSNicolas Dichtel break; 2739b4949ab2SNicolas Dichtel case RTN_THROW: 27400315e382SNikola Forró case RTN_UNREACHABLE: 2741ef2c7d7bSNicolas Dichtel default: 27427150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 27430315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 27440315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 27457150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 27467150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2747ef2c7d7bSNicolas Dichtel break; 2748ef2c7d7bSNicolas Dichtel } 27491da177e4SLinus Torvalds goto install_route; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds 275286872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2753b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 27541da177e4SLinus Torvalds int gwa_type; 27551da177e4SLinus Torvalds 275686872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2757330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 275848ed7b26SFlorian Westphal 275948ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 276048ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 276148ed7b26SFlorian Westphal * will return already-added prefix route via interface that 276248ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 276348ed7b26SFlorian Westphal */ 276448ed7b26SFlorian Westphal err = -EINVAL; 2765330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2766330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2767d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2768d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 276948ed7b26SFlorian Westphal goto out; 2770d5d531cbSDavid Ahern } 27714e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 27741da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 27751da177e4SLinus Torvalds addresses as nexthop address. 27761da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 27771da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 27781da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 27791da177e4SLinus Torvalds some exceptions. --ANK 278096d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 278196d5822cSErik Nordmark addressing 27821da177e4SLinus Torvalds */ 278396d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2784d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2785d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2786d5d531cbSDavid Ahern "Invalid gateway address"); 27871da177e4SLinus Torvalds goto out; 2788d5d531cbSDavid Ahern } 27891da177e4SLinus Torvalds 2790fc1e64e1SDavid Ahern if (cfg->fc_flags & RTNH_F_ONLINK) { 2791fc1e64e1SDavid Ahern err = ip6_route_check_nh_onlink(net, cfg, dev, 2792fc1e64e1SDavid Ahern extack); 2793fc1e64e1SDavid Ahern } else { 27941edce99fSDavid Ahern err = ip6_route_check_nh(net, cfg, &dev, &idev); 2795fc1e64e1SDavid Ahern } 27961da177e4SLinus Torvalds if (err) 27971da177e4SLinus Torvalds goto out; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds err = -EINVAL; 2800d5d531cbSDavid Ahern if (!dev) { 2801d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 28021da177e4SLinus Torvalds goto out; 2803d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2804d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2805d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2806d5d531cbSDavid Ahern goto out; 2807d5d531cbSDavid Ahern } 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds err = -ENODEV; 281138308473SDavid S. Miller if (!dev) 28121da177e4SLinus Torvalds goto out; 28131da177e4SLinus Torvalds 2814955ec4cbSDavid Ahern if (!(dev->flags & IFF_UP)) { 2815955ec4cbSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 2816955ec4cbSDavid Ahern err = -ENETDOWN; 2817955ec4cbSDavid Ahern goto out; 2818955ec4cbSDavid Ahern } 2819955ec4cbSDavid Ahern 2820c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2821c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2822d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2823c3968a85SDaniel Walter err = -EINVAL; 2824c3968a85SDaniel Walter goto out; 2825c3968a85SDaniel Walter } 28264e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2827c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2828c3968a85SDaniel Walter } else 2829c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2830c3968a85SDaniel Walter 283186872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds install_route: 28345609b80aSIdo Schimmel if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) && 28355609b80aSIdo Schimmel !netif_carrier_ok(dev)) 28365609b80aSIdo Schimmel rt->rt6i_nh_flags |= RTNH_F_LINKDOWN; 2837fc1e64e1SDavid Ahern rt->rt6i_nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK); 2838d8d1f30bSChangli Gao rt->dst.dev = dev; 28391da177e4SLinus Torvalds rt->rt6i_idev = idev; 2840c71099acSThomas Graf rt->rt6i_table = table; 284163152fc0SDaniel Lezcano 2842c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 284363152fc0SDaniel Lezcano 28448c5b83f0SRoopa Prabhu return rt; 28451da177e4SLinus Torvalds out: 28461da177e4SLinus Torvalds if (dev) 28471da177e4SLinus Torvalds dev_put(dev); 28481da177e4SLinus Torvalds if (idev) 28491da177e4SLinus Torvalds in6_dev_put(idev); 2850587fea74SWei Wang if (rt) 2851587fea74SWei Wang dst_release_immediate(&rt->dst); 28526b9ea5a6SRoopa Prabhu 28538c5b83f0SRoopa Prabhu return ERR_PTR(err); 28546b9ea5a6SRoopa Prabhu } 28556b9ea5a6SRoopa Prabhu 2856333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2857333c4301SDavid Ahern struct netlink_ext_ack *extack) 28586b9ea5a6SRoopa Prabhu { 28596b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 28608c5b83f0SRoopa Prabhu struct rt6_info *rt; 28616b9ea5a6SRoopa Prabhu int err; 28626b9ea5a6SRoopa Prabhu 2863333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 28648c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 28658c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 28668c5b83f0SRoopa Prabhu rt = NULL; 28676b9ea5a6SRoopa Prabhu goto out; 28688c5b83f0SRoopa Prabhu } 28696b9ea5a6SRoopa Prabhu 28706b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 28716b9ea5a6SRoopa Prabhu if (err) 28726b9ea5a6SRoopa Prabhu goto out; 28736b9ea5a6SRoopa Prabhu 2874333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 28756b9ea5a6SRoopa Prabhu 28766b9ea5a6SRoopa Prabhu kfree(mxc.mx); 28776b9ea5a6SRoopa Prabhu 28786b9ea5a6SRoopa Prabhu return err; 28796b9ea5a6SRoopa Prabhu out: 2880587fea74SWei Wang if (rt) 2881587fea74SWei Wang dst_release_immediate(&rt->dst); 28826b9ea5a6SRoopa Prabhu 28831da177e4SLinus Torvalds return err; 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 288686872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 28871da177e4SLinus Torvalds { 28881da177e4SLinus Torvalds int err; 2889c71099acSThomas Graf struct fib6_table *table; 2890d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 28911da177e4SLinus Torvalds 2892a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 28936825a26cSGao feng err = -ENOENT; 28946825a26cSGao feng goto out; 28956825a26cSGao feng } 28966c813a72SPatrick McHardy 2897c71099acSThomas Graf table = rt->rt6i_table; 289866f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 289986872cb5SThomas Graf err = fib6_del(rt, info); 290066f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 29011da177e4SLinus Torvalds 29026825a26cSGao feng out: 290394e187c0SAmerigo Wang ip6_rt_put(rt); 29041da177e4SLinus Torvalds return err; 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 2907e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2908e0a1ad73SThomas Graf { 29094d1169c1SDenis V. Lunev struct nl_info info = { 2910d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 29114d1169c1SDenis V. Lunev }; 2912528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2913e0a1ad73SThomas Graf } 2914e0a1ad73SThomas Graf 29150ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 29160ae81335SDavid Ahern { 29170ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2918e3330039SWANG Cong struct net *net = info->nl_net; 291916a16cd3SDavid Ahern struct sk_buff *skb = NULL; 29200ae81335SDavid Ahern struct fib6_table *table; 2921e3330039SWANG Cong int err = -ENOENT; 29220ae81335SDavid Ahern 2923e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2924e3330039SWANG Cong goto out_put; 29250ae81335SDavid Ahern table = rt->rt6i_table; 292666f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 29270ae81335SDavid Ahern 29280ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 29290ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 29300ae81335SDavid Ahern 293116a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 293216a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 293316a16cd3SDavid Ahern if (skb) { 293416a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 293516a16cd3SDavid Ahern 2936e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 293716a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 293816a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 293916a16cd3SDavid Ahern kfree_skb(skb); 294016a16cd3SDavid Ahern skb = NULL; 294116a16cd3SDavid Ahern } else 294216a16cd3SDavid Ahern info->skip_notify = 1; 294316a16cd3SDavid Ahern } 294416a16cd3SDavid Ahern 29450ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 29460ae81335SDavid Ahern &rt->rt6i_siblings, 29470ae81335SDavid Ahern rt6i_siblings) { 29480ae81335SDavid Ahern err = fib6_del(sibling, info); 29490ae81335SDavid Ahern if (err) 2950e3330039SWANG Cong goto out_unlock; 29510ae81335SDavid Ahern } 29520ae81335SDavid Ahern } 29530ae81335SDavid Ahern 29540ae81335SDavid Ahern err = fib6_del(rt, info); 2955e3330039SWANG Cong out_unlock: 295666f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 2957e3330039SWANG Cong out_put: 29580ae81335SDavid Ahern ip6_rt_put(rt); 295916a16cd3SDavid Ahern 296016a16cd3SDavid Ahern if (skb) { 2961e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 296216a16cd3SDavid Ahern info->nlh, gfp_any()); 296316a16cd3SDavid Ahern } 29640ae81335SDavid Ahern return err; 29650ae81335SDavid Ahern } 29660ae81335SDavid Ahern 2967333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2968333c4301SDavid Ahern struct netlink_ext_ack *extack) 29691da177e4SLinus Torvalds { 29702b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2971c71099acSThomas Graf struct fib6_table *table; 29721da177e4SLinus Torvalds struct fib6_node *fn; 29731da177e4SLinus Torvalds int err = -ESRCH; 29741da177e4SLinus Torvalds 29755578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2976d5d531cbSDavid Ahern if (!table) { 2977d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2978c71099acSThomas Graf return err; 2979d5d531cbSDavid Ahern } 29801da177e4SLinus Torvalds 298166f5d6ceSWei Wang rcu_read_lock(); 2982c71099acSThomas Graf 2983c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 298486872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 298538fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 29862b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds if (fn) { 298966f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 29902b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 29912b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 29922b760fcfSWei Wang &cfg->fc_src); 29932b760fcfSWei Wang if (!rt_cache) 29941f56a01fSMartin KaFai Lau continue; 29952b760fcfSWei Wang rt = rt_cache; 29962b760fcfSWei Wang } 299786872cb5SThomas Graf if (cfg->fc_ifindex && 2998d1918542SDavid S. Miller (!rt->dst.dev || 2999d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 30001da177e4SLinus Torvalds continue; 300186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 300286872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 30031da177e4SLinus Torvalds continue; 300486872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 30051da177e4SLinus Torvalds continue; 3006c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 3007c2ed1880SMantas M continue; 3008d3843fe5SWei Wang if (!dst_hold_safe(&rt->dst)) 3009d3843fe5SWei Wang break; 301066f5d6ceSWei Wang rcu_read_unlock(); 30111da177e4SLinus Torvalds 30120ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 30130ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 301486872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 30150ae81335SDavid Ahern 30160ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds } 301966f5d6ceSWei Wang rcu_read_unlock(); 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds return err; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 30246700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 3025a6279458SYOSHIFUJI Hideaki { 3026a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 3027e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 3028e8599ff4SDavid S. Miller struct ndisc_options ndopts; 3029e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 3030e8599ff4SDavid S. Miller struct neighbour *neigh; 303171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 30326e157b6aSDavid S. Miller int optlen, on_link; 30336e157b6aSDavid S. Miller u8 *lladdr; 3034e8599ff4SDavid S. Miller 303529a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 303671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 3037e8599ff4SDavid S. Miller 3038e8599ff4SDavid S. Miller if (optlen < 0) { 30396e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 3040e8599ff4SDavid S. Miller return; 3041e8599ff4SDavid S. Miller } 3042e8599ff4SDavid S. Miller 304371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 3044e8599ff4SDavid S. Miller 304571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 30466e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 3047e8599ff4SDavid S. Miller return; 3048e8599ff4SDavid S. Miller } 3049e8599ff4SDavid S. Miller 30506e157b6aSDavid S. Miller on_link = 0; 305171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 3052e8599ff4SDavid S. Miller on_link = 1; 305371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 3054e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 30556e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 3056e8599ff4SDavid S. Miller return; 3057e8599ff4SDavid S. Miller } 3058e8599ff4SDavid S. Miller 3059e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 3060e8599ff4SDavid S. Miller if (!in6_dev) 3061e8599ff4SDavid S. Miller return; 3062e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 3063e8599ff4SDavid S. Miller return; 3064e8599ff4SDavid S. Miller 3065e8599ff4SDavid S. Miller /* RFC2461 8.1: 3066e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 3067e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 3068e8599ff4SDavid S. Miller */ 3069e8599ff4SDavid S. Miller 3070f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 3071e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 3072e8599ff4SDavid S. Miller return; 3073e8599ff4SDavid S. Miller } 30746e157b6aSDavid S. Miller 30756e157b6aSDavid S. Miller lladdr = NULL; 3076e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 3077e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 3078e8599ff4SDavid S. Miller skb->dev); 3079e8599ff4SDavid S. Miller if (!lladdr) { 3080e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 3081e8599ff4SDavid S. Miller return; 3082e8599ff4SDavid S. Miller } 3083e8599ff4SDavid S. Miller } 3084e8599ff4SDavid S. Miller 30856e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 3086ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 30876e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 30886e157b6aSDavid S. Miller return; 30896e157b6aSDavid S. Miller } 30906e157b6aSDavid S. Miller 30916e157b6aSDavid S. Miller /* Redirect received -> path was valid. 30926e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 30936e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 30946e157b6aSDavid S. Miller */ 30950dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 30966e157b6aSDavid S. Miller 309771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3098e8599ff4SDavid S. Miller if (!neigh) 3099e8599ff4SDavid S. Miller return; 3100e8599ff4SDavid S. Miller 31011da177e4SLinus Torvalds /* 31021da177e4SLinus Torvalds * We have finally decided to accept it. 31031da177e4SLinus Torvalds */ 31041da177e4SLinus Torvalds 3105f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 31061da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 31071da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 31081da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3109f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3110f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 31111da177e4SLinus Torvalds 311283a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 311338308473SDavid S. Miller if (!nrt) 31141da177e4SLinus Torvalds goto out; 31151da177e4SLinus Torvalds 31161da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 31171da177e4SLinus Torvalds if (on_link) 31181da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 31191da177e4SLinus Torvalds 3120b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 31214e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 31221da177e4SLinus Torvalds 31232b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 31242b760fcfSWei Wang * a cached route because rt6_insert_exception() will 31252b760fcfSWei Wang * takes care of it 31262b760fcfSWei Wang */ 31272b760fcfSWei Wang if (rt6_insert_exception(nrt, rt)) { 31282b760fcfSWei Wang dst_release_immediate(&nrt->dst); 31292b760fcfSWei Wang goto out; 31302b760fcfSWei Wang } 31311da177e4SLinus Torvalds 3132d8d1f30bSChangli Gao netevent.old = &rt->dst; 3133d8d1f30bSChangli Gao netevent.new = &nrt->dst; 313471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 313560592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 31368d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 31378d71740cSTom Tucker 31381da177e4SLinus Torvalds out: 3139e8599ff4SDavid S. Miller neigh_release(neigh); 31406e157b6aSDavid S. Miller } 31416e157b6aSDavid S. Miller 31421da177e4SLinus Torvalds /* 31431da177e4SLinus Torvalds * Misc support functions 31441da177e4SLinus Torvalds */ 31451da177e4SLinus Torvalds 31464b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 31474b32b5adSMartin KaFai Lau { 31483a2232e9SDavid Miller BUG_ON(from->from); 31494b32b5adSMartin KaFai Lau 31504b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 31514b32b5adSMartin KaFai Lau dst_hold(&from->dst); 31523a2232e9SDavid Miller rt->from = from; 31534b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 31544b32b5adSMartin KaFai Lau } 31554b32b5adSMartin KaFai Lau 315683a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 31571da177e4SLinus Torvalds { 3158d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 3159d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 316083a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 3161d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 31621da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 31631da177e4SLinus Torvalds if (rt->rt6i_idev) 31641da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 3165d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 31664e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 31671716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 31681716a961SGao feng rt6_set_from(rt, ort); 316983a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 31701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 317183a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 31721da177e4SLinus Torvalds #endif 317383a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 3174c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 317561adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 31761da177e4SLinus Torvalds } 31771da177e4SLinus Torvalds 317870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 3179efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 3180b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3181830218c1SDavid Ahern const struct in6_addr *gwaddr, 3182830218c1SDavid Ahern struct net_device *dev) 318370ceb4f5SYOSHIFUJI Hideaki { 3184830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3185830218c1SDavid Ahern int ifindex = dev->ifindex; 318670ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 318770ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3188c71099acSThomas Graf struct fib6_table *table; 318970ceb4f5SYOSHIFUJI Hideaki 3190830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 319138308473SDavid S. Miller if (!table) 3192c71099acSThomas Graf return NULL; 3193c71099acSThomas Graf 319466f5d6ceSWei Wang rcu_read_lock(); 319538fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 319670ceb4f5SYOSHIFUJI Hideaki if (!fn) 319770ceb4f5SYOSHIFUJI Hideaki goto out; 319870ceb4f5SYOSHIFUJI Hideaki 319966f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3200d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 320170ceb4f5SYOSHIFUJI Hideaki continue; 320270ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 320370ceb4f5SYOSHIFUJI Hideaki continue; 320470ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 320570ceb4f5SYOSHIFUJI Hideaki continue; 3206d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 320770ceb4f5SYOSHIFUJI Hideaki break; 320870ceb4f5SYOSHIFUJI Hideaki } 320970ceb4f5SYOSHIFUJI Hideaki out: 321066f5d6ceSWei Wang rcu_read_unlock(); 321170ceb4f5SYOSHIFUJI Hideaki return rt; 321270ceb4f5SYOSHIFUJI Hideaki } 321370ceb4f5SYOSHIFUJI Hideaki 3214efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3215b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3216830218c1SDavid Ahern const struct in6_addr *gwaddr, 3217830218c1SDavid Ahern struct net_device *dev, 321895c96174SEric Dumazet unsigned int pref) 321970ceb4f5SYOSHIFUJI Hideaki { 322086872cb5SThomas Graf struct fib6_config cfg = { 3221238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3222830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 322386872cb5SThomas Graf .fc_dst_len = prefixlen, 322486872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 322586872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3226b91d5329SXin Long .fc_protocol = RTPROT_RA, 322715e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3228efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3229efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 323086872cb5SThomas Graf }; 323170ceb4f5SYOSHIFUJI Hideaki 3232830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 32334e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 32344e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 323586872cb5SThomas Graf 3236e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3237e317da96SYOSHIFUJI Hideaki if (!prefixlen) 323886872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 323970ceb4f5SYOSHIFUJI Hideaki 3240333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 324170ceb4f5SYOSHIFUJI Hideaki 3242830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 324370ceb4f5SYOSHIFUJI Hideaki } 324470ceb4f5SYOSHIFUJI Hideaki #endif 324570ceb4f5SYOSHIFUJI Hideaki 3246b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 32471da177e4SLinus Torvalds { 3248830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 32491da177e4SLinus Torvalds struct rt6_info *rt; 3250c71099acSThomas Graf struct fib6_table *table; 32511da177e4SLinus Torvalds 3252830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 325338308473SDavid S. Miller if (!table) 3254c71099acSThomas Graf return NULL; 32551da177e4SLinus Torvalds 325666f5d6ceSWei Wang rcu_read_lock(); 325766f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3258d1918542SDavid S. Miller if (dev == rt->dst.dev && 3259045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 32601da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 32611da177e4SLinus Torvalds break; 32621da177e4SLinus Torvalds } 32631da177e4SLinus Torvalds if (rt) 3264d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 326566f5d6ceSWei Wang rcu_read_unlock(); 32661da177e4SLinus Torvalds return rt; 32671da177e4SLinus Torvalds } 32681da177e4SLinus Torvalds 3269b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3270ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3271ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 32721da177e4SLinus Torvalds { 327386872cb5SThomas Graf struct fib6_config cfg = { 3274ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3275238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 327686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 327786872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 327886872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3279b91d5329SXin Long .fc_protocol = RTPROT_RA, 328015e47304SEric W. Biederman .fc_nlinfo.portid = 0, 32815578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3282c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 328386872cb5SThomas Graf }; 32841da177e4SLinus Torvalds 32854e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 32861da177e4SLinus Torvalds 3287333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3288830218c1SDavid Ahern struct fib6_table *table; 3289830218c1SDavid Ahern 3290830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3291830218c1SDavid Ahern if (table) 3292830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3293830218c1SDavid Ahern } 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 32961da177e4SLinus Torvalds } 32971da177e4SLinus Torvalds 3298830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 32991da177e4SLinus Torvalds { 33001da177e4SLinus Torvalds struct rt6_info *rt; 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds restart: 330366f5d6ceSWei Wang rcu_read_lock(); 330466f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 33053e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 33063e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3307d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) { 330866f5d6ceSWei Wang rcu_read_unlock(); 3309e0a1ad73SThomas Graf ip6_del_rt(rt); 3310d3843fe5SWei Wang } else { 331166f5d6ceSWei Wang rcu_read_unlock(); 3312d3843fe5SWei Wang } 33131da177e4SLinus Torvalds goto restart; 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds } 331666f5d6ceSWei Wang rcu_read_unlock(); 3317830218c1SDavid Ahern 3318830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3319830218c1SDavid Ahern } 3320830218c1SDavid Ahern 3321830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3322830218c1SDavid Ahern { 3323830218c1SDavid Ahern struct fib6_table *table; 3324830218c1SDavid Ahern struct hlist_head *head; 3325830218c1SDavid Ahern unsigned int h; 3326830218c1SDavid Ahern 3327830218c1SDavid Ahern rcu_read_lock(); 3328830218c1SDavid Ahern 3329830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3330830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3331830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3332830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3333830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3334830218c1SDavid Ahern } 3335830218c1SDavid Ahern } 3336830218c1SDavid Ahern 3337830218c1SDavid Ahern rcu_read_unlock(); 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33405578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 33415578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 334286872cb5SThomas Graf struct fib6_config *cfg) 334386872cb5SThomas Graf { 334486872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 334586872cb5SThomas Graf 3346ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3347ca254490SDavid Ahern : RT6_TABLE_MAIN; 334886872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 334986872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 335086872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 335186872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 335286872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 335386872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 335486872cb5SThomas Graf 33555578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3356f1243c2dSBenjamin Thery 33574e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 33584e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 33594e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 336086872cb5SThomas Graf } 336186872cb5SThomas Graf 33625578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 33631da177e4SLinus Torvalds { 336486872cb5SThomas Graf struct fib6_config cfg; 33651da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 33661da177e4SLinus Torvalds int err; 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds switch (cmd) { 33691da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 33701da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3371af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 33721da177e4SLinus Torvalds return -EPERM; 33731da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 33741da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 33751da177e4SLinus Torvalds if (err) 33761da177e4SLinus Torvalds return -EFAULT; 33771da177e4SLinus Torvalds 33785578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 337986872cb5SThomas Graf 33801da177e4SLinus Torvalds rtnl_lock(); 33811da177e4SLinus Torvalds switch (cmd) { 33821da177e4SLinus Torvalds case SIOCADDRT: 3383333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 33841da177e4SLinus Torvalds break; 33851da177e4SLinus Torvalds case SIOCDELRT: 3386333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 33871da177e4SLinus Torvalds break; 33881da177e4SLinus Torvalds default: 33891da177e4SLinus Torvalds err = -EINVAL; 33901da177e4SLinus Torvalds } 33911da177e4SLinus Torvalds rtnl_unlock(); 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds return err; 33943ff50b79SStephen Hemminger } 33951da177e4SLinus Torvalds 33961da177e4SLinus Torvalds return -EINVAL; 33971da177e4SLinus Torvalds } 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds /* 34001da177e4SLinus Torvalds * Drop the packet on the floor 34011da177e4SLinus Torvalds */ 34021da177e4SLinus Torvalds 3403d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 34041da177e4SLinus Torvalds { 3405612f09e8SYOSHIFUJI Hideaki int type; 3406adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3407612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3408612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 34090660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 341045bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 34113bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 34123bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3413612f09e8SYOSHIFUJI Hideaki break; 3414612f09e8SYOSHIFUJI Hideaki } 3415612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3416612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 34173bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 34183bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3419612f09e8SYOSHIFUJI Hideaki break; 3420612f09e8SYOSHIFUJI Hideaki } 34213ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 34221da177e4SLinus Torvalds kfree_skb(skb); 34231da177e4SLinus Torvalds return 0; 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 34269ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 34279ce8ade0SThomas Graf { 3428612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 34299ce8ade0SThomas Graf } 34309ce8ade0SThomas Graf 3431ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 34321da177e4SLinus Torvalds { 3433adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3434612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 34379ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 34389ce8ade0SThomas Graf { 3439612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 34409ce8ade0SThomas Graf } 34419ce8ade0SThomas Graf 3442ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 34439ce8ade0SThomas Graf { 3444adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3445612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 34469ce8ade0SThomas Graf } 34479ce8ade0SThomas Graf 34481da177e4SLinus Torvalds /* 34491da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 34501da177e4SLinus Torvalds */ 34511da177e4SLinus Torvalds 34521da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 34531da177e4SLinus Torvalds const struct in6_addr *addr, 34548f031519SDavid S. Miller bool anycast) 34551da177e4SLinus Torvalds { 3456ca254490SDavid Ahern u32 tb_id; 3457c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 34584832c30dSDavid Ahern struct net_device *dev = idev->dev; 34595f02ce24SDavid Ahern struct rt6_info *rt; 34605f02ce24SDavid Ahern 34615f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3462a3300ef4SHannes Frederic Sowa if (!rt) 34631da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 34641da177e4SLinus Torvalds 34651da177e4SLinus Torvalds in6_dev_hold(idev); 34661da177e4SLinus Torvalds 346711d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3468d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3469d8d1f30bSChangli Gao rt->dst.output = ip6_output; 34701da177e4SLinus Torvalds rt->rt6i_idev = idev; 34711da177e4SLinus Torvalds 347294b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 34731da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 347458c4fb86SYOSHIFUJI Hideaki if (anycast) 347558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 347658c4fb86SYOSHIFUJI Hideaki else 34771da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 34781da177e4SLinus Torvalds 3479550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 34804e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 34811da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3482ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3483ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 34841da177e4SLinus Torvalds 34851da177e4SLinus Torvalds return rt; 34861da177e4SLinus Torvalds } 34871da177e4SLinus Torvalds 3488c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3489c3968a85SDaniel Walter struct arg_dev_net_ip { 3490c3968a85SDaniel Walter struct net_device *dev; 3491c3968a85SDaniel Walter struct net *net; 3492c3968a85SDaniel Walter struct in6_addr *addr; 3493c3968a85SDaniel Walter }; 3494c3968a85SDaniel Walter 3495c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3496c3968a85SDaniel Walter { 3497c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3498c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3499c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3500c3968a85SDaniel Walter 3501d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3502c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3503c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 350460006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3505c3968a85SDaniel Walter /* remove prefsrc entry */ 3506c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 350760006a48SWei Wang /* need to update cache as well */ 350860006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 350960006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3510c3968a85SDaniel Walter } 3511c3968a85SDaniel Walter return 0; 3512c3968a85SDaniel Walter } 3513c3968a85SDaniel Walter 3514c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3515c3968a85SDaniel Walter { 3516c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3517c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3518c3968a85SDaniel Walter .dev = ifp->idev->dev, 3519c3968a85SDaniel Walter .net = net, 3520c3968a85SDaniel Walter .addr = &ifp->addr, 3521c3968a85SDaniel Walter }; 35220c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3523c3968a85SDaniel Walter } 3524c3968a85SDaniel Walter 3525be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3526be7a010dSDuan Jiong 3527be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3528be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3529be7a010dSDuan Jiong { 3530be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3531be7a010dSDuan Jiong 35322b760fcfSWei Wang if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3533be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3534be7a010dSDuan Jiong return -1; 3535be7a010dSDuan Jiong } 3536b16cb459SWei Wang 3537b16cb459SWei Wang /* Further clean up cached routes in exception table. 3538b16cb459SWei Wang * This is needed because cached route may have a different 3539b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3540b16cb459SWei Wang */ 3541b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3542b16cb459SWei Wang 3543be7a010dSDuan Jiong return 0; 3544be7a010dSDuan Jiong } 3545be7a010dSDuan Jiong 3546be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3547be7a010dSDuan Jiong { 3548be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3549be7a010dSDuan Jiong } 3550be7a010dSDuan Jiong 35512127d95aSIdo Schimmel struct arg_netdev_event { 35522127d95aSIdo Schimmel const struct net_device *dev; 35534c981e28SIdo Schimmel union { 35542127d95aSIdo Schimmel unsigned int nh_flags; 35554c981e28SIdo Schimmel unsigned long event; 35564c981e28SIdo Schimmel }; 35572127d95aSIdo Schimmel }; 35582127d95aSIdo Schimmel 3559d7dedee1SIdo Schimmel static struct rt6_info *rt6_multipath_first_sibling(const struct rt6_info *rt) 3560d7dedee1SIdo Schimmel { 3561d7dedee1SIdo Schimmel struct rt6_info *iter; 3562d7dedee1SIdo Schimmel struct fib6_node *fn; 3563d7dedee1SIdo Schimmel 3564d7dedee1SIdo Schimmel fn = rcu_dereference_protected(rt->rt6i_node, 3565d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3566d7dedee1SIdo Schimmel iter = rcu_dereference_protected(fn->leaf, 3567d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3568d7dedee1SIdo Schimmel while (iter) { 3569d7dedee1SIdo Schimmel if (iter->rt6i_metric == rt->rt6i_metric && 3570d7dedee1SIdo Schimmel rt6_qualify_for_ecmp(iter)) 3571d7dedee1SIdo Schimmel return iter; 3572d7dedee1SIdo Schimmel iter = rcu_dereference_protected(iter->rt6_next, 3573d7dedee1SIdo Schimmel lockdep_is_held(&rt->rt6i_table->tb6_lock)); 3574d7dedee1SIdo Schimmel } 3575d7dedee1SIdo Schimmel 3576d7dedee1SIdo Schimmel return NULL; 3577d7dedee1SIdo Schimmel } 3578d7dedee1SIdo Schimmel 3579d7dedee1SIdo Schimmel static bool rt6_is_dead(const struct rt6_info *rt) 3580d7dedee1SIdo Schimmel { 3581d7dedee1SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD || 3582d7dedee1SIdo Schimmel (rt->rt6i_nh_flags & RTNH_F_LINKDOWN && 3583d7dedee1SIdo Schimmel rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 3584d7dedee1SIdo Schimmel return true; 3585d7dedee1SIdo Schimmel 3586d7dedee1SIdo Schimmel return false; 3587d7dedee1SIdo Schimmel } 3588d7dedee1SIdo Schimmel 3589d7dedee1SIdo Schimmel static int rt6_multipath_total_weight(const struct rt6_info *rt) 3590d7dedee1SIdo Schimmel { 3591d7dedee1SIdo Schimmel struct rt6_info *iter; 3592d7dedee1SIdo Schimmel int total = 0; 3593d7dedee1SIdo Schimmel 3594d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) 3595398958aeSIdo Schimmel total += rt->rt6i_nh_weight; 3596d7dedee1SIdo Schimmel 3597d7dedee1SIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) { 3598d7dedee1SIdo Schimmel if (!rt6_is_dead(iter)) 3599398958aeSIdo Schimmel total += iter->rt6i_nh_weight; 3600d7dedee1SIdo Schimmel } 3601d7dedee1SIdo Schimmel 3602d7dedee1SIdo Schimmel return total; 3603d7dedee1SIdo Schimmel } 3604d7dedee1SIdo Schimmel 3605d7dedee1SIdo Schimmel static void rt6_upper_bound_set(struct rt6_info *rt, int *weight, int total) 3606d7dedee1SIdo Schimmel { 3607d7dedee1SIdo Schimmel int upper_bound = -1; 3608d7dedee1SIdo Schimmel 3609d7dedee1SIdo Schimmel if (!rt6_is_dead(rt)) { 3610398958aeSIdo Schimmel *weight += rt->rt6i_nh_weight; 3611d7dedee1SIdo Schimmel upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, 3612d7dedee1SIdo Schimmel total) - 1; 3613d7dedee1SIdo Schimmel } 3614d7dedee1SIdo Schimmel atomic_set(&rt->rt6i_nh_upper_bound, upper_bound); 3615d7dedee1SIdo Schimmel } 3616d7dedee1SIdo Schimmel 3617d7dedee1SIdo Schimmel static void rt6_multipath_upper_bound_set(struct rt6_info *rt, int total) 3618d7dedee1SIdo Schimmel { 3619d7dedee1SIdo Schimmel struct rt6_info *iter; 3620d7dedee1SIdo Schimmel int weight = 0; 3621d7dedee1SIdo Schimmel 3622d7dedee1SIdo Schimmel rt6_upper_bound_set(rt, &weight, total); 3623d7dedee1SIdo Schimmel 3624d7dedee1SIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 3625d7dedee1SIdo Schimmel rt6_upper_bound_set(iter, &weight, total); 3626d7dedee1SIdo Schimmel } 3627d7dedee1SIdo Schimmel 3628d7dedee1SIdo Schimmel void rt6_multipath_rebalance(struct rt6_info *rt) 3629d7dedee1SIdo Schimmel { 3630d7dedee1SIdo Schimmel struct rt6_info *first; 3631d7dedee1SIdo Schimmel int total; 3632d7dedee1SIdo Schimmel 3633d7dedee1SIdo Schimmel /* In case the entire multipath route was marked for flushing, 3634d7dedee1SIdo Schimmel * then there is no need to rebalance upon the removal of every 3635d7dedee1SIdo Schimmel * sibling route. 3636d7dedee1SIdo Schimmel */ 3637d7dedee1SIdo Schimmel if (!rt->rt6i_nsiblings || rt->should_flush) 3638d7dedee1SIdo Schimmel return; 3639d7dedee1SIdo Schimmel 3640d7dedee1SIdo Schimmel /* During lookup routes are evaluated in order, so we need to 3641d7dedee1SIdo Schimmel * make sure upper bounds are assigned from the first sibling 3642d7dedee1SIdo Schimmel * onwards. 3643d7dedee1SIdo Schimmel */ 3644d7dedee1SIdo Schimmel first = rt6_multipath_first_sibling(rt); 3645d7dedee1SIdo Schimmel if (WARN_ON_ONCE(!first)) 3646d7dedee1SIdo Schimmel return; 3647d7dedee1SIdo Schimmel 3648d7dedee1SIdo Schimmel total = rt6_multipath_total_weight(first); 3649d7dedee1SIdo Schimmel rt6_multipath_upper_bound_set(first, total); 3650d7dedee1SIdo Schimmel } 3651d7dedee1SIdo Schimmel 36522127d95aSIdo Schimmel static int fib6_ifup(struct rt6_info *rt, void *p_arg) 36532127d95aSIdo Schimmel { 36542127d95aSIdo Schimmel const struct arg_netdev_event *arg = p_arg; 36552127d95aSIdo Schimmel const struct net *net = dev_net(arg->dev); 36562127d95aSIdo Schimmel 36571de178edSIdo Schimmel if (rt != net->ipv6.ip6_null_entry && rt->dst.dev == arg->dev) { 36582127d95aSIdo Schimmel rt->rt6i_nh_flags &= ~arg->nh_flags; 36591de178edSIdo Schimmel fib6_update_sernum_upto_root(dev_net(rt->dst.dev), rt); 3660d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 36611de178edSIdo Schimmel } 36622127d95aSIdo Schimmel 36632127d95aSIdo Schimmel return 0; 36642127d95aSIdo Schimmel } 36652127d95aSIdo Schimmel 36662127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) 36672127d95aSIdo Schimmel { 36682127d95aSIdo Schimmel struct arg_netdev_event arg = { 36692127d95aSIdo Schimmel .dev = dev, 36706802f3adSIdo Schimmel { 36712127d95aSIdo Schimmel .nh_flags = nh_flags, 36726802f3adSIdo Schimmel }, 36732127d95aSIdo Schimmel }; 36742127d95aSIdo Schimmel 36752127d95aSIdo Schimmel if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) 36762127d95aSIdo Schimmel arg.nh_flags |= RTNH_F_LINKDOWN; 36772127d95aSIdo Schimmel 36782127d95aSIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifup, &arg); 36792127d95aSIdo Schimmel } 36802127d95aSIdo Schimmel 36811de178edSIdo Schimmel static bool rt6_multipath_uses_dev(const struct rt6_info *rt, 36821de178edSIdo Schimmel const struct net_device *dev) 36831de178edSIdo Schimmel { 36841de178edSIdo Schimmel struct rt6_info *iter; 36851de178edSIdo Schimmel 36861de178edSIdo Schimmel if (rt->dst.dev == dev) 36871de178edSIdo Schimmel return true; 36881de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 36891de178edSIdo Schimmel if (iter->dst.dev == dev) 36901de178edSIdo Schimmel return true; 36911de178edSIdo Schimmel 36921de178edSIdo Schimmel return false; 36931de178edSIdo Schimmel } 36941de178edSIdo Schimmel 36951de178edSIdo Schimmel static void rt6_multipath_flush(struct rt6_info *rt) 36961de178edSIdo Schimmel { 36971de178edSIdo Schimmel struct rt6_info *iter; 36981de178edSIdo Schimmel 36991de178edSIdo Schimmel rt->should_flush = 1; 37001de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 37011de178edSIdo Schimmel iter->should_flush = 1; 37021de178edSIdo Schimmel } 37031de178edSIdo Schimmel 37041de178edSIdo Schimmel static unsigned int rt6_multipath_dead_count(const struct rt6_info *rt, 37051de178edSIdo Schimmel const struct net_device *down_dev) 37061de178edSIdo Schimmel { 37071de178edSIdo Schimmel struct rt6_info *iter; 37081de178edSIdo Schimmel unsigned int dead = 0; 37091de178edSIdo Schimmel 37101de178edSIdo Schimmel if (rt->dst.dev == down_dev || rt->rt6i_nh_flags & RTNH_F_DEAD) 37111de178edSIdo Schimmel dead++; 37121de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 37131de178edSIdo Schimmel if (iter->dst.dev == down_dev || 37141de178edSIdo Schimmel iter->rt6i_nh_flags & RTNH_F_DEAD) 37151de178edSIdo Schimmel dead++; 37161de178edSIdo Schimmel 37171de178edSIdo Schimmel return dead; 37181de178edSIdo Schimmel } 37191de178edSIdo Schimmel 37201de178edSIdo Schimmel static void rt6_multipath_nh_flags_set(struct rt6_info *rt, 37211de178edSIdo Schimmel const struct net_device *dev, 37221de178edSIdo Schimmel unsigned int nh_flags) 37231de178edSIdo Schimmel { 37241de178edSIdo Schimmel struct rt6_info *iter; 37251de178edSIdo Schimmel 37261de178edSIdo Schimmel if (rt->dst.dev == dev) 37271de178edSIdo Schimmel rt->rt6i_nh_flags |= nh_flags; 37281de178edSIdo Schimmel list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) 37291de178edSIdo Schimmel if (iter->dst.dev == dev) 37301de178edSIdo Schimmel iter->rt6i_nh_flags |= nh_flags; 37311de178edSIdo Schimmel } 37321de178edSIdo Schimmel 3733a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 37344c981e28SIdo Schimmel static int fib6_ifdown(struct rt6_info *rt, void *p_arg) 37351da177e4SLinus Torvalds { 37364c981e28SIdo Schimmel const struct arg_netdev_event *arg = p_arg; 37374c981e28SIdo Schimmel const struct net_device *dev = arg->dev; 37384c981e28SIdo Schimmel const struct net *net = dev_net(dev); 37398ed67789SDaniel Lezcano 37401de178edSIdo Schimmel if (rt == net->ipv6.ip6_null_entry) 374127c6fa73SIdo Schimmel return 0; 374227c6fa73SIdo Schimmel 374327c6fa73SIdo Schimmel switch (arg->event) { 374427c6fa73SIdo Schimmel case NETDEV_UNREGISTER: 37451de178edSIdo Schimmel return rt->dst.dev == dev ? -1 : 0; 374627c6fa73SIdo Schimmel case NETDEV_DOWN: 37471de178edSIdo Schimmel if (rt->should_flush) 374827c6fa73SIdo Schimmel return -1; 37491de178edSIdo Schimmel if (!rt->rt6i_nsiblings) 37501de178edSIdo Schimmel return rt->dst.dev == dev ? -1 : 0; 37511de178edSIdo Schimmel if (rt6_multipath_uses_dev(rt, dev)) { 37521de178edSIdo Schimmel unsigned int count; 37531de178edSIdo Schimmel 37541de178edSIdo Schimmel count = rt6_multipath_dead_count(rt, dev); 37551de178edSIdo Schimmel if (rt->rt6i_nsiblings + 1 == count) { 37561de178edSIdo Schimmel rt6_multipath_flush(rt); 37571de178edSIdo Schimmel return -1; 37581de178edSIdo Schimmel } 37591de178edSIdo Schimmel rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | 37601de178edSIdo Schimmel RTNH_F_LINKDOWN); 37611de178edSIdo Schimmel fib6_update_sernum(rt); 3762d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 37631de178edSIdo Schimmel } 37641de178edSIdo Schimmel return -2; 376527c6fa73SIdo Schimmel case NETDEV_CHANGE: 37661de178edSIdo Schimmel if (rt->dst.dev != dev || 37671de178edSIdo Schimmel rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) 376827c6fa73SIdo Schimmel break; 376927c6fa73SIdo Schimmel rt->rt6i_nh_flags |= RTNH_F_LINKDOWN; 3770d7dedee1SIdo Schimmel rt6_multipath_rebalance(rt); 377127c6fa73SIdo Schimmel break; 37722b241361SIdo Schimmel } 3773c159d30cSDavid S. Miller 37741da177e4SLinus Torvalds return 0; 37751da177e4SLinus Torvalds } 37761da177e4SLinus Torvalds 377727c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event) 37781da177e4SLinus Torvalds { 37794c981e28SIdo Schimmel struct arg_netdev_event arg = { 37808ed67789SDaniel Lezcano .dev = dev, 37816802f3adSIdo Schimmel { 37824c981e28SIdo Schimmel .event = event, 37836802f3adSIdo Schimmel }, 37848ed67789SDaniel Lezcano }; 37858ed67789SDaniel Lezcano 37864c981e28SIdo Schimmel fib6_clean_all(dev_net(dev), fib6_ifdown, &arg); 37874c981e28SIdo Schimmel } 37884c981e28SIdo Schimmel 37894c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event) 37904c981e28SIdo Schimmel { 37914c981e28SIdo Schimmel rt6_sync_down_dev(dev, event); 37924c981e28SIdo Schimmel rt6_uncached_list_flush_dev(dev_net(dev), dev); 37934c981e28SIdo Schimmel neigh_ifdown(&nd_tbl, dev); 37941da177e4SLinus Torvalds } 37951da177e4SLinus Torvalds 379695c96174SEric Dumazet struct rt6_mtu_change_arg { 37971da177e4SLinus Torvalds struct net_device *dev; 379895c96174SEric Dumazet unsigned int mtu; 37991da177e4SLinus Torvalds }; 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 38021da177e4SLinus Torvalds { 38031da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 38041da177e4SLinus Torvalds struct inet6_dev *idev; 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 38071da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 38081da177e4SLinus Torvalds We still use this lock to block changes 38091da177e4SLinus Torvalds caused by addrconf/ndisc. 38101da177e4SLinus Torvalds */ 38111da177e4SLinus Torvalds 38121da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 381338308473SDavid S. Miller if (!idev) 38141da177e4SLinus Torvalds return 0; 38151da177e4SLinus Torvalds 38161da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 38171da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 38181da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 38191da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 38201da177e4SLinus Torvalds */ 38211da177e4SLinus Torvalds /* 38221da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 38231da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 38241da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 38251da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 38261da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 38271da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 382867c408cfSAlexander Alemayhu PMTU discovery. 38291da177e4SLinus Torvalds */ 3830d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3831fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 38324b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3833f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 38342b760fcfSWei Wang if (dst_mtu(&rt->dst) >= arg->mtu || 3835d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 38364b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3837defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3838566cfd8fSSimon Arlott } 3839f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3840f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 38414b32b5adSMartin KaFai Lau } 38421da177e4SLinus Torvalds return 0; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 384595c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 38461da177e4SLinus Torvalds { 3847c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3848c71099acSThomas Graf .dev = dev, 3849c71099acSThomas Graf .mtu = mtu, 3850c71099acSThomas Graf }; 38511da177e4SLinus Torvalds 38520c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 3855ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 38565176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 385786872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3858ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 385986872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 386086872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 386151ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3862c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 386319e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 386419e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 386532bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3866622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 38673b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 386886872cb5SThomas Graf }; 386986872cb5SThomas Graf 387086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3871333c4301SDavid Ahern struct fib6_config *cfg, 3872333c4301SDavid Ahern struct netlink_ext_ack *extack) 38731da177e4SLinus Torvalds { 387486872cb5SThomas Graf struct rtmsg *rtm; 387586872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3876c78ba6d6SLubomir Rintel unsigned int pref; 387786872cb5SThomas Graf int err; 38781da177e4SLinus Torvalds 3879fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3880fceb6435SJohannes Berg NULL); 388186872cb5SThomas Graf if (err < 0) 388286872cb5SThomas Graf goto errout; 38831da177e4SLinus Torvalds 388486872cb5SThomas Graf err = -EINVAL; 388586872cb5SThomas Graf rtm = nlmsg_data(nlh); 388686872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 388786872cb5SThomas Graf 388886872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 388986872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 389086872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 389186872cb5SThomas Graf cfg->fc_flags = RTF_UP; 389286872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3893ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 389486872cb5SThomas Graf 3895ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3896ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3897b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3898b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 389986872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 390086872cb5SThomas Graf 3901ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3902ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3903ab79ad14SMaciej Żenczykowski 39041f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 39051f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 39061f56a01fSMartin KaFai Lau 3907fc1e64e1SDavid Ahern cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); 3908fc1e64e1SDavid Ahern 390915e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 391086872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 39113b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 391286872cb5SThomas Graf 391386872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 391467b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 391586872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 39161da177e4SLinus Torvalds } 391786872cb5SThomas Graf 391886872cb5SThomas Graf if (tb[RTA_DST]) { 391986872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 392086872cb5SThomas Graf 392186872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 392286872cb5SThomas Graf goto errout; 392386872cb5SThomas Graf 392486872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 39251da177e4SLinus Torvalds } 392686872cb5SThomas Graf 392786872cb5SThomas Graf if (tb[RTA_SRC]) { 392886872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 392986872cb5SThomas Graf 393086872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 393186872cb5SThomas Graf goto errout; 393286872cb5SThomas Graf 393386872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 39341da177e4SLinus Torvalds } 393586872cb5SThomas Graf 3936c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 393767b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3938c3968a85SDaniel Walter 393986872cb5SThomas Graf if (tb[RTA_OIF]) 394086872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 394186872cb5SThomas Graf 394286872cb5SThomas Graf if (tb[RTA_PRIORITY]) 394386872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 394486872cb5SThomas Graf 394586872cb5SThomas Graf if (tb[RTA_METRICS]) { 394686872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 394786872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 39481da177e4SLinus Torvalds } 394986872cb5SThomas Graf 395086872cb5SThomas Graf if (tb[RTA_TABLE]) 395186872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 395286872cb5SThomas Graf 395351ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 395451ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 395551ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 39569ed59592SDavid Ahern 39579ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3958c255bd68SDavid Ahern cfg->fc_mp_len, extack); 39599ed59592SDavid Ahern if (err < 0) 39609ed59592SDavid Ahern goto errout; 396151ebd318SNicolas Dichtel } 396251ebd318SNicolas Dichtel 3963c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3964c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3965c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3966c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3967c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3968c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3969c78ba6d6SLubomir Rintel } 3970c78ba6d6SLubomir Rintel 397119e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 397219e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 397319e42e45SRoopa Prabhu 39749ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 397519e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 397619e42e45SRoopa Prabhu 3977c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 39789ed59592SDavid Ahern if (err < 0) 39799ed59592SDavid Ahern goto errout; 39809ed59592SDavid Ahern } 39819ed59592SDavid Ahern 398232bc201eSXin Long if (tb[RTA_EXPIRES]) { 398332bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 398432bc201eSXin Long 398532bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 398632bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 398732bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 398832bc201eSXin Long } 398932bc201eSXin Long } 399032bc201eSXin Long 399186872cb5SThomas Graf err = 0; 399286872cb5SThomas Graf errout: 399386872cb5SThomas Graf return err; 39941da177e4SLinus Torvalds } 39951da177e4SLinus Torvalds 39966b9ea5a6SRoopa Prabhu struct rt6_nh { 39976b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 39986b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 39996b9ea5a6SRoopa Prabhu struct mx6_config mxc; 40006b9ea5a6SRoopa Prabhu struct list_head next; 40016b9ea5a6SRoopa Prabhu }; 40026b9ea5a6SRoopa Prabhu 40036b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 40046b9ea5a6SRoopa Prabhu { 40056b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 40066b9ea5a6SRoopa Prabhu 40076b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 40087d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 40096b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 40106b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 40116b9ea5a6SRoopa Prabhu } 40126b9ea5a6SRoopa Prabhu } 40136b9ea5a6SRoopa Prabhu 40146b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 40156b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 40166b9ea5a6SRoopa Prabhu { 40176b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 40186b9ea5a6SRoopa Prabhu int err = -EEXIST; 40196b9ea5a6SRoopa Prabhu 40206b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 40216b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 4022f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 40236b9ea5a6SRoopa Prabhu return err; 40246b9ea5a6SRoopa Prabhu } 40256b9ea5a6SRoopa Prabhu 40266b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 40276b9ea5a6SRoopa Prabhu if (!nh) 40286b9ea5a6SRoopa Prabhu return -ENOMEM; 40296b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 40306b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 40316b9ea5a6SRoopa Prabhu if (err) { 40326b9ea5a6SRoopa Prabhu kfree(nh); 40336b9ea5a6SRoopa Prabhu return err; 40346b9ea5a6SRoopa Prabhu } 40356b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 40366b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 40376b9ea5a6SRoopa Prabhu 40386b9ea5a6SRoopa Prabhu return 0; 40396b9ea5a6SRoopa Prabhu } 40406b9ea5a6SRoopa Prabhu 40413b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 40423b1137feSDavid Ahern struct rt6_info *rt_last, 40433b1137feSDavid Ahern struct nl_info *info, 40443b1137feSDavid Ahern __u16 nlflags) 40453b1137feSDavid Ahern { 40463b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 40473b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 40483b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 40493b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 40503b1137feSDavid Ahern * the list, find the first sibling of the last route appended 40513b1137feSDavid Ahern */ 40523b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 40533b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 40543b1137feSDavid Ahern struct rt6_info, 40553b1137feSDavid Ahern rt6i_siblings); 40563b1137feSDavid Ahern } 40573b1137feSDavid Ahern 40583b1137feSDavid Ahern if (rt) 40593b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 40603b1137feSDavid Ahern } 40613b1137feSDavid Ahern 4062333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 4063333c4301SDavid Ahern struct netlink_ext_ack *extack) 406451ebd318SNicolas Dichtel { 40653b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 40663b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 406751ebd318SNicolas Dichtel struct fib6_config r_cfg; 406851ebd318SNicolas Dichtel struct rtnexthop *rtnh; 40696b9ea5a6SRoopa Prabhu struct rt6_info *rt; 40706b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 40716b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 40723b1137feSDavid Ahern __u16 nlflags; 407351ebd318SNicolas Dichtel int remaining; 407451ebd318SNicolas Dichtel int attrlen; 40756b9ea5a6SRoopa Prabhu int err = 1; 40766b9ea5a6SRoopa Prabhu int nhn = 0; 40776b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 40786b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 40796b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 408051ebd318SNicolas Dichtel 40813b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 40823b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 40833b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 40843b1137feSDavid Ahern 408535f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 408651ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 408751ebd318SNicolas Dichtel 40886b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 40896b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 40906b9ea5a6SRoopa Prabhu */ 409151ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 409251ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 409351ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 409451ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 409551ebd318SNicolas Dichtel 409651ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 409751ebd318SNicolas Dichtel if (attrlen > 0) { 409851ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 409951ebd318SNicolas Dichtel 410051ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 410151ebd318SNicolas Dichtel if (nla) { 410267b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 410351ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 410451ebd318SNicolas Dichtel } 410519e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 410619e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 410719e42e45SRoopa Prabhu if (nla) 410819e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 410951ebd318SNicolas Dichtel } 41106b9ea5a6SRoopa Prabhu 4111333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 41128c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 41138c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 41148c5b83f0SRoopa Prabhu rt = NULL; 41156b9ea5a6SRoopa Prabhu goto cleanup; 41168c5b83f0SRoopa Prabhu } 41176b9ea5a6SRoopa Prabhu 4118398958aeSIdo Schimmel rt->rt6i_nh_weight = rtnh->rtnh_hops + 1; 4119398958aeSIdo Schimmel 41206b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 412151ebd318SNicolas Dichtel if (err) { 4122587fea74SWei Wang dst_release_immediate(&rt->dst); 41236b9ea5a6SRoopa Prabhu goto cleanup; 412451ebd318SNicolas Dichtel } 41256b9ea5a6SRoopa Prabhu 41266b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 412751ebd318SNicolas Dichtel } 41286b9ea5a6SRoopa Prabhu 41293b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 41303b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 41313b1137feSDavid Ahern * the full route when done 41323b1137feSDavid Ahern */ 41333b1137feSDavid Ahern info->skip_notify = 1; 41343b1137feSDavid Ahern 41356b9ea5a6SRoopa Prabhu err_nh = NULL; 41366b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 41373b1137feSDavid Ahern rt_last = nh->rt6_info; 4138333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 41393b1137feSDavid Ahern /* save reference to first route for notification */ 41403b1137feSDavid Ahern if (!rt_notif && !err) 41413b1137feSDavid Ahern rt_notif = nh->rt6_info; 41423b1137feSDavid Ahern 41436b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 41446b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 41456b9ea5a6SRoopa Prabhu if (err) { 41466b9ea5a6SRoopa Prabhu if (replace && nhn) 41476b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 41486b9ea5a6SRoopa Prabhu err_nh = nh; 41496b9ea5a6SRoopa Prabhu goto add_errout; 41506b9ea5a6SRoopa Prabhu } 41516b9ea5a6SRoopa Prabhu 41521a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 415327596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 415427596472SMichal Kubeček * we have already failed to add the first nexthop: 415527596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 415627596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 415727596472SMichal Kubeček * be added to it. 41581a72418bSNicolas Dichtel */ 415927596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 416027596472SMichal Kubeček NLM_F_REPLACE); 41616b9ea5a6SRoopa Prabhu nhn++; 41626b9ea5a6SRoopa Prabhu } 41636b9ea5a6SRoopa Prabhu 41643b1137feSDavid Ahern /* success ... tell user about new route */ 41653b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 41666b9ea5a6SRoopa Prabhu goto cleanup; 41676b9ea5a6SRoopa Prabhu 41686b9ea5a6SRoopa Prabhu add_errout: 41693b1137feSDavid Ahern /* send notification for routes that were added so that 41703b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 41713b1137feSDavid Ahern * coherent 41723b1137feSDavid Ahern */ 41733b1137feSDavid Ahern if (rt_notif) 41743b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 41753b1137feSDavid Ahern 41766b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 41776b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 41786b9ea5a6SRoopa Prabhu if (err_nh == nh) 41796b9ea5a6SRoopa Prabhu break; 4180333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 41816b9ea5a6SRoopa Prabhu } 41826b9ea5a6SRoopa Prabhu 41836b9ea5a6SRoopa Prabhu cleanup: 41846b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 4185587fea74SWei Wang if (nh->rt6_info) 4186587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 41876b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 41886b9ea5a6SRoopa Prabhu list_del(&nh->next); 41896b9ea5a6SRoopa Prabhu kfree(nh); 41906b9ea5a6SRoopa Prabhu } 41916b9ea5a6SRoopa Prabhu 41926b9ea5a6SRoopa Prabhu return err; 41936b9ea5a6SRoopa Prabhu } 41946b9ea5a6SRoopa Prabhu 4195333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 4196333c4301SDavid Ahern struct netlink_ext_ack *extack) 41976b9ea5a6SRoopa Prabhu { 41986b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 41996b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 42006b9ea5a6SRoopa Prabhu int remaining; 42016b9ea5a6SRoopa Prabhu int attrlen; 42026b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 42036b9ea5a6SRoopa Prabhu 42046b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 42056b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 42066b9ea5a6SRoopa Prabhu 42076b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 42086b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 42096b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 42106b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 42116b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 42126b9ea5a6SRoopa Prabhu 42136b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 42146b9ea5a6SRoopa Prabhu if (attrlen > 0) { 42156b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 42166b9ea5a6SRoopa Prabhu 42176b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 42186b9ea5a6SRoopa Prabhu if (nla) { 42196b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 42206b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 42216b9ea5a6SRoopa Prabhu } 42226b9ea5a6SRoopa Prabhu } 4223333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 42246b9ea5a6SRoopa Prabhu if (err) 42256b9ea5a6SRoopa Prabhu last_err = err; 42266b9ea5a6SRoopa Prabhu 422751ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 422851ebd318SNicolas Dichtel } 422951ebd318SNicolas Dichtel 423051ebd318SNicolas Dichtel return last_err; 423151ebd318SNicolas Dichtel } 423251ebd318SNicolas Dichtel 4233c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4234c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 42351da177e4SLinus Torvalds { 423686872cb5SThomas Graf struct fib6_config cfg; 423786872cb5SThomas Graf int err; 42381da177e4SLinus Torvalds 4239333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 424086872cb5SThomas Graf if (err < 0) 424186872cb5SThomas Graf return err; 424286872cb5SThomas Graf 424351ebd318SNicolas Dichtel if (cfg.fc_mp) 4244333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 42450ae81335SDavid Ahern else { 42460ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 4247333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 42481da177e4SLinus Torvalds } 42490ae81335SDavid Ahern } 42501da177e4SLinus Torvalds 4251c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 4252c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 42531da177e4SLinus Torvalds { 425486872cb5SThomas Graf struct fib6_config cfg; 425586872cb5SThomas Graf int err; 42561da177e4SLinus Torvalds 4257333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 425886872cb5SThomas Graf if (err < 0) 425986872cb5SThomas Graf return err; 426086872cb5SThomas Graf 426151ebd318SNicolas Dichtel if (cfg.fc_mp) 4262333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 426351ebd318SNicolas Dichtel else 4264333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 42651da177e4SLinus Torvalds } 42661da177e4SLinus Torvalds 4267beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 4268339bf98fSThomas Graf { 4269beb1afacSDavid Ahern int nexthop_len = 0; 4270beb1afacSDavid Ahern 4271beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4272beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 4273beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 4274beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 4275beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 4276beb1afacSDavid Ahern 4277beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 4278beb1afacSDavid Ahern } 4279beb1afacSDavid Ahern 4280339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 4281339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 4282339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 4283339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 4284339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 4285339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 4286339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 4287339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 4288339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 42896a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 4290ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 4291c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 429219e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 4293beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 4294beb1afacSDavid Ahern + nexthop_len; 4295beb1afacSDavid Ahern } 4296beb1afacSDavid Ahern 4297beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 42985be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 4299beb1afacSDavid Ahern { 4300f9d882eaSIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_DEAD) 4301f9d882eaSIdo Schimmel *flags |= RTNH_F_DEAD; 4302f9d882eaSIdo Schimmel 430344c9f2f2SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_LINKDOWN) { 4304beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 4305beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 4306beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 4307beb1afacSDavid Ahern } 4308beb1afacSDavid Ahern 4309beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 4310beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 4311beb1afacSDavid Ahern goto nla_put_failure; 4312beb1afacSDavid Ahern } 4313beb1afacSDavid Ahern 4314fc1e64e1SDavid Ahern *flags |= (rt->rt6i_nh_flags & RTNH_F_ONLINK); 4315fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 431661e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 431761e4d01eSIdo Schimmel 43185be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 43195be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 4320beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 4321beb1afacSDavid Ahern goto nla_put_failure; 4322beb1afacSDavid Ahern 4323beb1afacSDavid Ahern if (rt->dst.lwtstate && 4324beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 4325beb1afacSDavid Ahern goto nla_put_failure; 4326beb1afacSDavid Ahern 4327beb1afacSDavid Ahern return 0; 4328beb1afacSDavid Ahern 4329beb1afacSDavid Ahern nla_put_failure: 4330beb1afacSDavid Ahern return -EMSGSIZE; 4331beb1afacSDavid Ahern } 4332beb1afacSDavid Ahern 43335be083ceSDavid Ahern /* add multipath next hop */ 4334beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 4335beb1afacSDavid Ahern { 4336beb1afacSDavid Ahern struct rtnexthop *rtnh; 4337beb1afacSDavid Ahern unsigned int flags = 0; 4338beb1afacSDavid Ahern 4339beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 4340beb1afacSDavid Ahern if (!rtnh) 4341beb1afacSDavid Ahern goto nla_put_failure; 4342beb1afacSDavid Ahern 4343398958aeSIdo Schimmel rtnh->rtnh_hops = rt->rt6i_nh_weight - 1; 4344beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 4345beb1afacSDavid Ahern 43465be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 4347beb1afacSDavid Ahern goto nla_put_failure; 4348beb1afacSDavid Ahern 4349beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 4350beb1afacSDavid Ahern 4351beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4352beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4353beb1afacSDavid Ahern 4354beb1afacSDavid Ahern return 0; 4355beb1afacSDavid Ahern 4356beb1afacSDavid Ahern nla_put_failure: 4357beb1afacSDavid Ahern return -EMSGSIZE; 4358339bf98fSThomas Graf } 4359339bf98fSThomas Graf 4360191cd582SBrian Haley static int rt6_fill_node(struct net *net, 4361191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 43620d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 436315e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4364f8cfe2ceSDavid Ahern unsigned int flags) 43651da177e4SLinus Torvalds { 43664b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 43671da177e4SLinus Torvalds struct rtmsg *rtm; 43681da177e4SLinus Torvalds struct nlmsghdr *nlh; 4369e3703b3dSThomas Graf long expires; 43709e762a4aSPatrick McHardy u32 table; 43711da177e4SLinus Torvalds 437215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 437338308473SDavid S. Miller if (!nlh) 437426932566SPatrick McHardy return -EMSGSIZE; 43752d7202bfSThomas Graf 43762d7202bfSThomas Graf rtm = nlmsg_data(nlh); 43771da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 43781da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 43791da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 43801da177e4SLinus Torvalds rtm->rtm_tos = 0; 4381c71099acSThomas Graf if (rt->rt6i_table) 43829e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 4383c71099acSThomas Graf else 43849e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 43859e762a4aSPatrick McHardy rtm->rtm_table = table; 4386c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4387c78679e8SDavid S. Miller goto nla_put_failure; 4388ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 4389ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4390ef2c7d7bSNicolas Dichtel case -EINVAL: 4391ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4392ef2c7d7bSNicolas Dichtel break; 4393ef2c7d7bSNicolas Dichtel case -EACCES: 4394ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4395ef2c7d7bSNicolas Dichtel break; 4396b4949ab2SNicolas Dichtel case -EAGAIN: 4397b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4398b4949ab2SNicolas Dichtel break; 4399ef2c7d7bSNicolas Dichtel default: 44001da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4401ef2c7d7bSNicolas Dichtel break; 4402ef2c7d7bSNicolas Dichtel } 4403ef2c7d7bSNicolas Dichtel } 4404ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4405ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 44064ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 44074ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4408d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 44091da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 44101da177e4SLinus Torvalds else 44111da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 44121da177e4SLinus Torvalds rtm->rtm_flags = 0; 44131da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 44141da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 44151da177e4SLinus Torvalds 44161da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 44171da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 44181da177e4SLinus Torvalds 44191da177e4SLinus Torvalds if (dst) { 4420930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4421c78679e8SDavid S. Miller goto nla_put_failure; 44221da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 44231da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4424930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4425c78679e8SDavid S. Miller goto nla_put_failure; 44261da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 44271da177e4SLinus Torvalds if (src) { 4428930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4429c78679e8SDavid S. Miller goto nla_put_failure; 44301da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4431c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4432930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4433c78679e8SDavid S. Miller goto nla_put_failure; 44341da177e4SLinus Torvalds #endif 44357bc570c8SYOSHIFUJI Hideaki if (iif) { 44367bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 44377bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4438fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 44392cf75070SNikolay Aleksandrov 44407bc570c8SYOSHIFUJI Hideaki if (err == 0) 44417bc570c8SYOSHIFUJI Hideaki return 0; 4442fd61c6baSDavid Ahern if (err < 0) 44437bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 44447bc570c8SYOSHIFUJI Hideaki } else 44457bc570c8SYOSHIFUJI Hideaki #endif 4446c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4447c78679e8SDavid S. Miller goto nla_put_failure; 44487bc570c8SYOSHIFUJI Hideaki } else if (dst) { 44491da177e4SLinus Torvalds struct in6_addr saddr_buf; 4450c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4451930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4452c78679e8SDavid S. Miller goto nla_put_failure; 4453c3968a85SDaniel Walter } 4454c3968a85SDaniel Walter 4455c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4456c3968a85SDaniel Walter struct in6_addr saddr_buf; 44574e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4458930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4459c78679e8SDavid S. Miller goto nla_put_failure; 44601da177e4SLinus Torvalds } 44612d7202bfSThomas Graf 44624b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 44634b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 44644b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 44654b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 44662d7202bfSThomas Graf goto nla_put_failure; 44672d7202bfSThomas Graf 4468beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4469beb1afacSDavid Ahern goto nla_put_failure; 4470beb1afacSDavid Ahern 4471beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4472beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4473beb1afacSDavid Ahern */ 4474beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4475beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4476beb1afacSDavid Ahern struct nlattr *mp; 4477beb1afacSDavid Ahern 4478beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4479beb1afacSDavid Ahern if (!mp) 4480beb1afacSDavid Ahern goto nla_put_failure; 4481beb1afacSDavid Ahern 4482beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4483beb1afacSDavid Ahern goto nla_put_failure; 4484beb1afacSDavid Ahern 4485beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4486beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4487beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 448894f826b8SEric Dumazet goto nla_put_failure; 448994f826b8SEric Dumazet } 44902d7202bfSThomas Graf 4491beb1afacSDavid Ahern nla_nest_end(skb, mp); 4492beb1afacSDavid Ahern } else { 44935be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4494c78679e8SDavid S. Miller goto nla_put_failure; 4495beb1afacSDavid Ahern } 44968253947eSLi Wei 44978253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 449869cdf8f9SYOSHIFUJI Hideaki 449987a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4500e3703b3dSThomas Graf goto nla_put_failure; 45011da177e4SLinus Torvalds 4502c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4503c78ba6d6SLubomir Rintel goto nla_put_failure; 4504c78ba6d6SLubomir Rintel 450519e42e45SRoopa Prabhu 4506053c095aSJohannes Berg nlmsg_end(skb, nlh); 4507053c095aSJohannes Berg return 0; 45082d7202bfSThomas Graf 45092d7202bfSThomas Graf nla_put_failure: 451026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 451126932566SPatrick McHardy return -EMSGSIZE; 45121da177e4SLinus Torvalds } 45131da177e4SLinus Torvalds 45141b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 45151da177e4SLinus Torvalds { 45161da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 45171f17e2f2SDavid Ahern struct net *net = arg->net; 45181f17e2f2SDavid Ahern 45191f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 45201f17e2f2SDavid Ahern return 0; 45211da177e4SLinus Torvalds 45222d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 45232d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4524f8cfe2ceSDavid Ahern 4525f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4526f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4527f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4528f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4529f8cfe2ceSDavid Ahern return 1; 4530f8cfe2ceSDavid Ahern } 4531f8cfe2ceSDavid Ahern } 45321da177e4SLinus Torvalds 45331f17e2f2SDavid Ahern return rt6_fill_node(net, 4534191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 453515e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4536f8cfe2ceSDavid Ahern NLM_F_MULTI); 45371da177e4SLinus Torvalds } 45381da177e4SLinus Torvalds 4539c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4540c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45411da177e4SLinus Torvalds { 45423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4543ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 454418c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 454518c3a61cSRoopa Prabhu struct dst_entry *dst; 45461da177e4SLinus Torvalds struct rt6_info *rt; 4547ab364a6fSThomas Graf struct sk_buff *skb; 4548ab364a6fSThomas Graf struct rtmsg *rtm; 45494c9483b2SDavid S. Miller struct flowi6 fl6; 455018c3a61cSRoopa Prabhu bool fibmatch; 4551ab364a6fSThomas Graf 4552fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4553c21ef3e3SDavid Ahern extack); 4554ab364a6fSThomas Graf if (err < 0) 4555ab364a6fSThomas Graf goto errout; 4556ab364a6fSThomas Graf 4557ab364a6fSThomas Graf err = -EINVAL; 45584c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 455938b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 456038b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 456118c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4562ab364a6fSThomas Graf 4563ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4564ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4565ab364a6fSThomas Graf goto errout; 4566ab364a6fSThomas Graf 45674e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4568ab364a6fSThomas Graf } 4569ab364a6fSThomas Graf 4570ab364a6fSThomas Graf if (tb[RTA_DST]) { 4571ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4572ab364a6fSThomas Graf goto errout; 4573ab364a6fSThomas Graf 45744e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4575ab364a6fSThomas Graf } 4576ab364a6fSThomas Graf 4577ab364a6fSThomas Graf if (tb[RTA_IIF]) 4578ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4579ab364a6fSThomas Graf 4580ab364a6fSThomas Graf if (tb[RTA_OIF]) 458172331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4582ab364a6fSThomas Graf 45832e47b291SLorenzo Colitti if (tb[RTA_MARK]) 45842e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 45852e47b291SLorenzo Colitti 4586622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4587622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4588622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4589622ec2c9SLorenzo Colitti else 4590622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4591622ec2c9SLorenzo Colitti 4592ab364a6fSThomas Graf if (iif) { 4593ab364a6fSThomas Graf struct net_device *dev; 459472331bc0SShmulik Ladkani int flags = 0; 459572331bc0SShmulik Ladkani 4596121622dbSFlorian Westphal rcu_read_lock(); 4597121622dbSFlorian Westphal 4598121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4599ab364a6fSThomas Graf if (!dev) { 4600121622dbSFlorian Westphal rcu_read_unlock(); 4601ab364a6fSThomas Graf err = -ENODEV; 4602ab364a6fSThomas Graf goto errout; 4603ab364a6fSThomas Graf } 460472331bc0SShmulik Ladkani 460572331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 460672331bc0SShmulik Ladkani 460772331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 460872331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 460972331bc0SShmulik Ladkani 461018c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4611121622dbSFlorian Westphal 4612121622dbSFlorian Westphal rcu_read_unlock(); 461372331bc0SShmulik Ladkani } else { 461472331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 461572331bc0SShmulik Ladkani 461618c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 461718c3a61cSRoopa Prabhu } 461818c3a61cSRoopa Prabhu 461918c3a61cSRoopa Prabhu 462018c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 462118c3a61cSRoopa Prabhu if (rt->dst.error) { 462218c3a61cSRoopa Prabhu err = rt->dst.error; 462318c3a61cSRoopa Prabhu ip6_rt_put(rt); 462418c3a61cSRoopa Prabhu goto errout; 4625ab364a6fSThomas Graf } 46261da177e4SLinus Torvalds 46279d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 46289d6acb3bSWANG Cong err = rt->dst.error; 46299d6acb3bSWANG Cong ip6_rt_put(rt); 46309d6acb3bSWANG Cong goto errout; 46319d6acb3bSWANG Cong } 46329d6acb3bSWANG Cong 4633fba961abSDavid S. Miller if (fibmatch && rt->from) { 4634fba961abSDavid S. Miller struct rt6_info *ort = rt->from; 463558acfd71SIdo Schimmel 463658acfd71SIdo Schimmel dst_hold(&ort->dst); 463758acfd71SIdo Schimmel ip6_rt_put(rt); 463858acfd71SIdo Schimmel rt = ort; 463958acfd71SIdo Schimmel } 464058acfd71SIdo Schimmel 46411da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 464238308473SDavid S. Miller if (!skb) { 464394e187c0SAmerigo Wang ip6_rt_put(rt); 4644ab364a6fSThomas Graf err = -ENOBUFS; 4645ab364a6fSThomas Graf goto errout; 4646ab364a6fSThomas Graf } 46471da177e4SLinus Torvalds 4648d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 464918c3a61cSRoopa Prabhu if (fibmatch) 465018c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 465118c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 465218c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 465318c3a61cSRoopa Prabhu else 46544c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 465515e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4656f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 46571da177e4SLinus Torvalds if (err < 0) { 4658ab364a6fSThomas Graf kfree_skb(skb); 4659ab364a6fSThomas Graf goto errout; 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds 466215e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4663ab364a6fSThomas Graf errout: 46641da177e4SLinus Torvalds return err; 46651da177e4SLinus Torvalds } 46661da177e4SLinus Torvalds 466737a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 466837a1d361SRoopa Prabhu unsigned int nlm_flags) 46691da177e4SLinus Torvalds { 46701da177e4SLinus Torvalds struct sk_buff *skb; 46715578689aSDaniel Lezcano struct net *net = info->nl_net; 4672528c4cebSDenis V. Lunev u32 seq; 4673528c4cebSDenis V. Lunev int err; 46740d51aa80SJamal Hadi Salim 4675528c4cebSDenis V. Lunev err = -ENOBUFS; 467638308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 467786872cb5SThomas Graf 467819e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 467938308473SDavid S. Miller if (!skb) 468021713ebcSThomas Graf goto errout; 46811da177e4SLinus Torvalds 4682191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4683f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 468426932566SPatrick McHardy if (err < 0) { 468526932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 468626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 468726932566SPatrick McHardy kfree_skb(skb); 468826932566SPatrick McHardy goto errout; 468926932566SPatrick McHardy } 469015e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 46915578689aSDaniel Lezcano info->nlh, gfp_any()); 46921ce85fe4SPablo Neira Ayuso return; 469321713ebcSThomas Graf errout: 469421713ebcSThomas Graf if (err < 0) 46955578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 46961da177e4SLinus Torvalds } 46971da177e4SLinus Torvalds 46988ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4699351638e7SJiri Pirko unsigned long event, void *ptr) 47008ed67789SDaniel Lezcano { 4701351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4702c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 47038ed67789SDaniel Lezcano 4704242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4705242d3a49SWANG Cong return NOTIFY_OK; 4706242d3a49SWANG Cong 4707242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4708d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 47098ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 47108ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4711d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 47128ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4713d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 47148ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 47158ed67789SDaniel Lezcano #endif 471676da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 471776da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 471876da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 471976da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 472076da0704SWANG Cong */ 472112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4722242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 472312d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 472412d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4725242d3a49SWANG Cong #endif 47268ed67789SDaniel Lezcano } 47278ed67789SDaniel Lezcano 47288ed67789SDaniel Lezcano return NOTIFY_OK; 47298ed67789SDaniel Lezcano } 47308ed67789SDaniel Lezcano 47311da177e4SLinus Torvalds /* 47321da177e4SLinus Torvalds * /proc 47331da177e4SLinus Torvalds */ 47341da177e4SLinus Torvalds 47351da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 47361da177e4SLinus Torvalds 473733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 473833120b30SAlexey Dobriyan .open = ipv6_route_open, 473933120b30SAlexey Dobriyan .read = seq_read, 474033120b30SAlexey Dobriyan .llseek = seq_lseek, 47418d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 474233120b30SAlexey Dobriyan }; 474333120b30SAlexey Dobriyan 47441da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 47451da177e4SLinus Torvalds { 474669ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 47471da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 474869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 474969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 475081eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 475169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 475269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4753fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 475469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 47551da177e4SLinus Torvalds 47561da177e4SLinus Torvalds return 0; 47571da177e4SLinus Torvalds } 47581da177e4SLinus Torvalds 47591da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 47601da177e4SLinus Torvalds { 4761de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 476269ddb805SDaniel Lezcano } 476369ddb805SDaniel Lezcano 47649a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 47651da177e4SLinus Torvalds .open = rt6_stats_seq_open, 47661da177e4SLinus Torvalds .read = seq_read, 47671da177e4SLinus Torvalds .llseek = seq_lseek, 4768b6fcbdb4SPavel Emelyanov .release = single_release_net, 47691da177e4SLinus Torvalds }; 47701da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 47711da177e4SLinus Torvalds 47721da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 47731da177e4SLinus Torvalds 47741da177e4SLinus Torvalds static 4775fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 47761da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 47771da177e4SLinus Torvalds { 4778c486da34SLucian Adrian Grijincu struct net *net; 4779c486da34SLucian Adrian Grijincu int delay; 4780c486da34SLucian Adrian Grijincu if (!write) 4781c486da34SLucian Adrian Grijincu return -EINVAL; 4782c486da34SLucian Adrian Grijincu 4783c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4784c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 47858d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 47862ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 47871da177e4SLinus Torvalds return 0; 47881da177e4SLinus Torvalds } 47891da177e4SLinus Torvalds 4790fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 47911da177e4SLinus Torvalds { 47921da177e4SLinus Torvalds .procname = "flush", 47934990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 47941da177e4SLinus Torvalds .maxlen = sizeof(int), 479589c8b3a1SDave Jones .mode = 0200, 47966d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 47971da177e4SLinus Torvalds }, 47981da177e4SLinus Torvalds { 47991da177e4SLinus Torvalds .procname = "gc_thresh", 48009a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 48011da177e4SLinus Torvalds .maxlen = sizeof(int), 48021da177e4SLinus Torvalds .mode = 0644, 48036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48041da177e4SLinus Torvalds }, 48051da177e4SLinus Torvalds { 48061da177e4SLinus Torvalds .procname = "max_size", 48074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 48081da177e4SLinus Torvalds .maxlen = sizeof(int), 48091da177e4SLinus Torvalds .mode = 0644, 48106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48111da177e4SLinus Torvalds }, 48121da177e4SLinus Torvalds { 48131da177e4SLinus Torvalds .procname = "gc_min_interval", 48144990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 48151da177e4SLinus Torvalds .maxlen = sizeof(int), 48161da177e4SLinus Torvalds .mode = 0644, 48176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48181da177e4SLinus Torvalds }, 48191da177e4SLinus Torvalds { 48201da177e4SLinus Torvalds .procname = "gc_timeout", 48214990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 48221da177e4SLinus Torvalds .maxlen = sizeof(int), 48231da177e4SLinus Torvalds .mode = 0644, 48246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48251da177e4SLinus Torvalds }, 48261da177e4SLinus Torvalds { 48271da177e4SLinus Torvalds .procname = "gc_interval", 48284990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 48291da177e4SLinus Torvalds .maxlen = sizeof(int), 48301da177e4SLinus Torvalds .mode = 0644, 48316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48321da177e4SLinus Torvalds }, 48331da177e4SLinus Torvalds { 48341da177e4SLinus Torvalds .procname = "gc_elasticity", 48354990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 48361da177e4SLinus Torvalds .maxlen = sizeof(int), 48371da177e4SLinus Torvalds .mode = 0644, 4838f3d3f616SMin Zhang .proc_handler = proc_dointvec, 48391da177e4SLinus Torvalds }, 48401da177e4SLinus Torvalds { 48411da177e4SLinus Torvalds .procname = "mtu_expires", 48424990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 48431da177e4SLinus Torvalds .maxlen = sizeof(int), 48441da177e4SLinus Torvalds .mode = 0644, 48456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48461da177e4SLinus Torvalds }, 48471da177e4SLinus Torvalds { 48481da177e4SLinus Torvalds .procname = "min_adv_mss", 48494990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 48501da177e4SLinus Torvalds .maxlen = sizeof(int), 48511da177e4SLinus Torvalds .mode = 0644, 4852f3d3f616SMin Zhang .proc_handler = proc_dointvec, 48531da177e4SLinus Torvalds }, 48541da177e4SLinus Torvalds { 48551da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 48564990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 48571da177e4SLinus Torvalds .maxlen = sizeof(int), 48581da177e4SLinus Torvalds .mode = 0644, 48596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 48601da177e4SLinus Torvalds }, 4861f8572d8fSEric W. Biederman { } 48621da177e4SLinus Torvalds }; 48631da177e4SLinus Torvalds 48642c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4865760f2d01SDaniel Lezcano { 4866760f2d01SDaniel Lezcano struct ctl_table *table; 4867760f2d01SDaniel Lezcano 4868760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4869760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4870760f2d01SDaniel Lezcano GFP_KERNEL); 48715ee09105SYOSHIFUJI Hideaki 48725ee09105SYOSHIFUJI Hideaki if (table) { 48735ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4874c486da34SLucian Adrian Grijincu table[0].extra1 = net; 487586393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 48765ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 48775ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 48785ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 48795ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 48805ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 48815ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 48825ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 48839c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4884464dc801SEric W. Biederman 4885464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4886464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4887464dc801SEric W. Biederman table[0].procname = NULL; 48885ee09105SYOSHIFUJI Hideaki } 48895ee09105SYOSHIFUJI Hideaki 4890760f2d01SDaniel Lezcano return table; 4891760f2d01SDaniel Lezcano } 48921da177e4SLinus Torvalds #endif 48931da177e4SLinus Torvalds 48942c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4895cdb18761SDaniel Lezcano { 4896633d424bSPavel Emelyanov int ret = -ENOMEM; 48978ed67789SDaniel Lezcano 489886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 489986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4900f2fc6a54SBenjamin Thery 4901fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4902fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4903fc66f95cSEric Dumazet 49048ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 49058ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 49068ed67789SDaniel Lezcano GFP_KERNEL); 49078ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4908fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4909d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 491062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 491162fa8a84SDavid S. Miller ip6_template_metrics, true); 49128ed67789SDaniel Lezcano 49138ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4914feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 49158ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 49168ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 49178ed67789SDaniel Lezcano GFP_KERNEL); 491868fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 491968fffc67SPeter Zijlstra goto out_ip6_null_entry; 4920d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 492162fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 492262fa8a84SDavid S. Miller ip6_template_metrics, true); 49238ed67789SDaniel Lezcano 49248ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 49258ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 49268ed67789SDaniel Lezcano GFP_KERNEL); 492768fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 492868fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4929d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 493062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 493162fa8a84SDavid S. Miller ip6_template_metrics, true); 49328ed67789SDaniel Lezcano #endif 49338ed67789SDaniel Lezcano 4934b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4935b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4936b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4937b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4938b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4939b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4940b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4941b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4942b339a47cSPeter Zijlstra 49436891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 49446891a346SBenjamin Thery 49458ed67789SDaniel Lezcano ret = 0; 49468ed67789SDaniel Lezcano out: 49478ed67789SDaniel Lezcano return ret; 4948f2fc6a54SBenjamin Thery 494968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 495068fffc67SPeter Zijlstra out_ip6_prohibit_entry: 495168fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 495268fffc67SPeter Zijlstra out_ip6_null_entry: 495368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 495468fffc67SPeter Zijlstra #endif 4955fc66f95cSEric Dumazet out_ip6_dst_entries: 4956fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4957f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4958f2fc6a54SBenjamin Thery goto out; 4959cdb18761SDaniel Lezcano } 4960cdb18761SDaniel Lezcano 49612c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4962cdb18761SDaniel Lezcano { 49638ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 49648ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 49658ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 49668ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 49678ed67789SDaniel Lezcano #endif 496841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4969cdb18761SDaniel Lezcano } 4970cdb18761SDaniel Lezcano 4971d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4972d189634eSThomas Graf { 4973d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4974d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4975d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4976d189634eSThomas Graf #endif 4977d189634eSThomas Graf return 0; 4978d189634eSThomas Graf } 4979d189634eSThomas Graf 4980d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4981d189634eSThomas Graf { 4982d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4983ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4984ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4985d189634eSThomas Graf #endif 4986d189634eSThomas Graf } 4987d189634eSThomas Graf 4988cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4989cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4990cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 499150911411SKirill Tkhai .async = true, 4992cdb18761SDaniel Lezcano }; 4993cdb18761SDaniel Lezcano 4994c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4995c3426b47SDavid S. Miller { 4996c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4997c3426b47SDavid S. Miller 4998c3426b47SDavid S. Miller if (!bp) 4999c3426b47SDavid S. Miller return -ENOMEM; 5000c3426b47SDavid S. Miller inet_peer_base_init(bp); 5001c3426b47SDavid S. Miller net->ipv6.peers = bp; 5002c3426b47SDavid S. Miller return 0; 5003c3426b47SDavid S. Miller } 5004c3426b47SDavid S. Miller 5005c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 5006c3426b47SDavid S. Miller { 5007c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 5008c3426b47SDavid S. Miller 5009c3426b47SDavid S. Miller net->ipv6.peers = NULL; 501056a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 5011c3426b47SDavid S. Miller kfree(bp); 5012c3426b47SDavid S. Miller } 5013c3426b47SDavid S. Miller 50142b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 5015c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 5016c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 501785ca51b2SKirill Tkhai .async = true, 5018c3426b47SDavid S. Miller }; 5019c3426b47SDavid S. Miller 5020d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 5021d189634eSThomas Graf .init = ip6_route_net_init_late, 5022d189634eSThomas Graf .exit = ip6_route_net_exit_late, 502350911411SKirill Tkhai .async = true, 5024d189634eSThomas Graf }; 5025d189634eSThomas Graf 50268ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 50278ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 5028242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 50298ed67789SDaniel Lezcano }; 50308ed67789SDaniel Lezcano 50312f460933SWANG Cong void __init ip6_route_init_special_entries(void) 50322f460933SWANG Cong { 50332f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 50342f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 50352f460933SWANG Cong * manually for init_net */ 50362f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 50372f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 50382f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 50392f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 50402f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 50412f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 50422f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 50432f460933SWANG Cong #endif 50442f460933SWANG Cong } 50452f460933SWANG Cong 5046433d49c3SDaniel Lezcano int __init ip6_route_init(void) 50471da177e4SLinus Torvalds { 5048433d49c3SDaniel Lezcano int ret; 50498d0b94afSMartin KaFai Lau int cpu; 5050433d49c3SDaniel Lezcano 50519a7ec3a9SDaniel Lezcano ret = -ENOMEM; 50529a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 50539a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 50549a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 50559a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 5056c19a28e1SFernando Carrijo goto out; 505714e50e57SDavid S. Miller 5058fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 50598ed67789SDaniel Lezcano if (ret) 5060bdb3289fSDaniel Lezcano goto out_kmem_cache; 5061bdb3289fSDaniel Lezcano 5062c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 5063c3426b47SDavid S. Miller if (ret) 5064e8803b6cSDavid S. Miller goto out_dst_entries; 50652a0c451aSThomas Graf 50667e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 50677e52b33bSDavid S. Miller if (ret) 50687e52b33bSDavid S. Miller goto out_register_inetpeer; 5069c3426b47SDavid S. Miller 50705dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 50715dc121e9SArnaud Ebalard 5072e8803b6cSDavid S. Miller ret = fib6_init(); 5073433d49c3SDaniel Lezcano if (ret) 50748ed67789SDaniel Lezcano goto out_register_subsys; 5075433d49c3SDaniel Lezcano 5076433d49c3SDaniel Lezcano ret = xfrm6_init(); 5077433d49c3SDaniel Lezcano if (ret) 5078e8803b6cSDavid S. Miller goto out_fib6_init; 5079c35b7e72SDaniel Lezcano 5080433d49c3SDaniel Lezcano ret = fib6_rules_init(); 5081433d49c3SDaniel Lezcano if (ret) 5082433d49c3SDaniel Lezcano goto xfrm6_init; 50837e5449c2SDaniel Lezcano 5084d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 5085d189634eSThomas Graf if (ret) 5086d189634eSThomas Graf goto fib6_rules_init; 5087d189634eSThomas Graf 508816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 508916feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 509016feebcfSFlorian Westphal if (ret < 0) 509116feebcfSFlorian Westphal goto out_register_late_subsys; 509216feebcfSFlorian Westphal 509316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 509416feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 509516feebcfSFlorian Westphal if (ret < 0) 509616feebcfSFlorian Westphal goto out_register_late_subsys; 509716feebcfSFlorian Westphal 509816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 509916feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 510016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 510116feebcfSFlorian Westphal if (ret < 0) 5102d189634eSThomas Graf goto out_register_late_subsys; 5103433d49c3SDaniel Lezcano 51048ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 5105cdb18761SDaniel Lezcano if (ret) 5106d189634eSThomas Graf goto out_register_late_subsys; 51078ed67789SDaniel Lezcano 51088d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 51098d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 51108d0b94afSMartin KaFai Lau 51118d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 51128d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 51138d0b94afSMartin KaFai Lau } 51148d0b94afSMartin KaFai Lau 5115433d49c3SDaniel Lezcano out: 5116433d49c3SDaniel Lezcano return ret; 5117433d49c3SDaniel Lezcano 5118d189634eSThomas Graf out_register_late_subsys: 511916feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 5120d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5121433d49c3SDaniel Lezcano fib6_rules_init: 5122433d49c3SDaniel Lezcano fib6_rules_cleanup(); 5123433d49c3SDaniel Lezcano xfrm6_init: 5124433d49c3SDaniel Lezcano xfrm6_fini(); 51252a0c451aSThomas Graf out_fib6_init: 51262a0c451aSThomas Graf fib6_gc_cleanup(); 51278ed67789SDaniel Lezcano out_register_subsys: 51288ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 51297e52b33bSDavid S. Miller out_register_inetpeer: 51307e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 5131fc66f95cSEric Dumazet out_dst_entries: 5132fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 5133433d49c3SDaniel Lezcano out_kmem_cache: 5134f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 5135433d49c3SDaniel Lezcano goto out; 51361da177e4SLinus Torvalds } 51371da177e4SLinus Torvalds 51381da177e4SLinus Torvalds void ip6_route_cleanup(void) 51391da177e4SLinus Torvalds { 51408ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 5141d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 5142101367c2SThomas Graf fib6_rules_cleanup(); 51431da177e4SLinus Torvalds xfrm6_fini(); 51441da177e4SLinus Torvalds fib6_gc_cleanup(); 5145c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 51468ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 514741bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 5148f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 51491da177e4SLinus Torvalds } 5150